CC := gcc CFLAGS := -std=c99 -m32 -ffreestanding -O0 -g -Wall -Werror LD := ld NASMFLAGS := -f elf32 NASM := nasm OBJCOPY := objcopy MAGIC := $(shell printf '%x' `date +%s`) all: image.bin kernel.sym # truncate to correct number of sectors, we have # 512 (boot, stage 1) + N * 512 (N currenty is 3, stage 2) = 3072 for boot.bin # + M * 512 (M is currently 7) = 3144 for kernel.bin # + 1 * 512 = 512 for magic.bin # (M + N + 1 is the number of sectors to be read in stage 2, as stage 1 # loads only the first sector, and stage 1 loads 3 sectors of stage 2, # adapt NOF_LOAD_SECTORS to 37) # then we make sure the image has the size of a 1.44 MB floppy # (emulators like qemu do some guess work for CHS resolution based # on the size of the image) image.bin: boot.bin kernel.bin magic.bin cat boot.bin kernel.bin > image.tmp truncate -s 20480 image.tmp cat image.tmp magic.bin > image.bin truncate -s 1474560 image.bin boot.bin: boot.asm boot_gdt.asm stage1_functions.asm stage2_real_functions.asm stage2_pm_functions.asm stage2_switch_mode.asm stage2_a20.asm $(NASM) boot.asm -DMAGIC='"$(MAGIC)"' -f bin -o boot.bin kernel.bin: kernel.elf $(OBJCOPY) -O binary kernel.elf kernel.bin kernel.sym: kernel.elf $(OBJCOPY) --only-keep-debug kernel.elf kernel.sym kernel.elf: kernel.o kernel_asm.o console.o vgatext.o serial.o port.o port_asm.o interrupts.o interrupts_asm.o driver.o pci.o keyboard.o mouse.o string.o stdlib.o stdio.o setjmp.o $(LD) -o kernel.elf -N -n -Ttext 0x8400 --oformat elf32-i386 \ kernel.o kernel_asm.o console.o vgatext.o serial.o \ port.o port_asm.o interrupts.o interrupts_asm.o \ driver.o pci.o \ keyboard.o mouse.o \ string.o stdlib.o stdio.o setjmp.o magic.bin: magic.asm $(NASM) magic.asm -DMAGIC='"$(MAGIC)"' -f bin -o magic.bin kernel.o: kernel.c $(CC) $(CFLAGS) -c -o kernel.o kernel.c kernel_asm.o: kernel.asm $(NASM) kernel.asm $(NASMFLAGS) -o kernel_asm.o port.o: port.c port.h $(CC) $(CFLAGS) -c -o port.o port.c port_asm.o: port.asm $(NASM) port.asm $(NASMFLAGS) -o port_asm.o console.o: console.c console.h vgatext.h serial.h $(CC) $(CFLAGS) -c -o console.o console.c vgatext.o: vgatext.c vgatext.h $(CC) $(CFLAGS) -c -o vgatext.o vgatext.c serial.o: serial.c serial.h $(CC) $(CFLAGS) -c -o serial.o serial.c interrupts.o: interrupts.c interrupts.h $(CC) $(CFLAGS) -c -o interrupts.o interrupts.c interrupts_asm.o: interrupts.asm $(NASM) interrupts.asm $(NASMFLAGS) -o interrupts_asm.o driver.o: driver.c driver.h $(CC) $(CFLAGS) -c -o driver.o driver.c pci.o: pci.c pci.h $(CC) $(CFLAGS) -c -o pci.o pci.c keyboard.o: keyboard.c keyboard.h $(CC) $(CFLAGS) -c -o keyboard.o keyboard.c mouse.o: mouse.c mouse.h $(CC) $(CFLAGS) -c -o mouse.o mouse.c string.o: string.c string.h $(CC) $(CFLAGS) -c -o string.o string.c stdlib.o: stdlib.c stdlib.h $(CC) $(CFLAGS) -c -o stdlib.o stdlib.c stdio.o: stdio.c stdio.h $(CC) $(CFLAGS) -c -o stdio.o stdio.c setjmp.o: setjmp.asm $(NASM) setjmp.asm $(NASMFLAGS) -o setjmp.o clean: -rm -f boot.bin kernel.bin kernel.sym kernel.elf image.bin magic.bin *.o boot.map image.tmp run-qemu-hd: image.bin qemu-system-i386 -net nic,model=ne2k_pci -d guest_errors -m 32 -drive "file=image.bin,if=ide,format=raw" \ -serial file:serial.log run-qemu-usb: image.bin qemu-system-i386 -net nic,model=ne2k_pci -d guest_errors -m 32 -usb -usbdevice disk:/dev/sde \ -serial file:serial.log run-qemu: image.bin qemu-system-i386 -net nic,model=ne2k_pci -d guest_errors -m 32 -drive "file=image.bin,if=floppy,format=raw" \ -serial file:serial.log run-qemu-debug: image.bin qemu-system-i386 -net nic,model=ne2k_pci -S -s -d guest_errors -m 32 -drive "file=image.bin,if=floppy,format=raw" \ -serial file:serial.log run-bochs: bochs -q -f bochs.config 'boot:floppy' 'floppya: 1_44=image.bin, status=inserted' run-bochs-hd: bochs -q -f bochs.config 'boot:disk' 'ata0-master: type=disk, path=image.bin, mode=flat, cylinders=64, heads=4, spt=8, translation=none'