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 image.bin: boot.bin kernel.bin magic.bin cat boot.bin kernel.bin > image.tmp # truncate to correct number of sectors, we have # 512 (boot, stage 1) + N * 512 (N currenty is 3, stage 2) = 2048 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, so adapt NOF_LOAD_SECTORS to 16) truncate -s 8192 image.tmp cat image.tmp magic.bin > image.bin boot.bin: boot.asm gdt.asm stage1_functions.asm stage2_functions.asm switch_mode.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 console.o vga.o serial.o port.o port_asm.o interrupts.o interrupts_asm.o string.o stdlib.o $(LD) -o kernel.elf -N -n -Ttext 0x8400 --oformat elf32-i386 \ kernel.o console.o vga.o serial.o port.o port_asm.o \ interrupts.o interrupts_asm.o string.o stdlib.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 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 vga.h serial.h $(CC) $(CFLAGS) -c -o console.o console.c vga.o: vga.c vga.h $(CC) $(CFLAGS) -c -o vga.o vga.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 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 clean: -rm -f boot.bin kernel.bin kernel.sym kernel.elf image.bin magic.bin *.o boot.map image.tmp run-qemu: image.bin qemu-system-i386 -d guest_errors -m 32 -drive "file=image.bin,if=ide,format=raw" \ -serial file:serial.log run-qemu-debug: image.bin qemu-system-i386 -S -s -d guest_errors -m 32 -drive "file=image.bin,if=ide,format=raw" \ -serial file:serial.log run-bochs: bochs -q -f bochs.config 'boot:floppy' 'floppya: 1_44=image.bin, status=inserted'