Makefile 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Bootloader: 512-byte boot sector + kernel at sector 2. Builds image.bin for QEMU.
  2. CC = gcc
  3. AS = as
  4. LD = ld
  5. OBJCOPY = objcopy
  6. CFLAGS = -ffreestanding -fno-pie -m32 -nostdlib -fno-stack-protector \
  7. -fno-asynchronous-unwind-tables -Wall -Wextra -O2 -c
  8. ASFLAGS = --32
  9. BOOT_LDFLAGS = -T linker.ld -nostdlib -m elf_i386
  10. KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
  11. .PHONY: all clean run
  12. all: image.bin
  13. boot.o: boot.S
  14. $(AS) $(ASFLAGS) -o $@ $<
  15. kernel.o: kernel.c
  16. $(CC) $(CFLAGS) -o $@ $<
  17. boot.elf: boot.o
  18. $(LD) $(BOOT_LDFLAGS) -o $@ $<
  19. kernel.elf: kernel.o
  20. $(LD) $(KERNEL_LDFLAGS) -o $@ $<
  21. boot.bin: boot.elf
  22. $(OBJCOPY) -O binary --only-section=.boot $< $@.tmp
  23. dd if=$@.tmp of=$@ bs=1 count=510 2>/dev/null
  24. printf '\x55\xaa' >> $@
  25. rm -f $@.tmp
  26. kernel.bin: kernel.elf
  27. $(OBJCOPY) -O binary $< $@
  28. image.bin: boot.bin kernel.bin
  29. dd if=/dev/zero of=$@ bs=512 count=2880 2>/dev/null
  30. dd if=boot.bin of=$@ bs=512 conv=notrunc 2>/dev/null
  31. dd if=kernel.bin of=$@ bs=512 seek=1 conv=notrunc 2>/dev/null
  32. run: image.bin
  33. qemu-system-i386 -fda image.bin 2>/dev/null || qemu-system-x86_64 -fda image.bin
  34. clean:
  35. rm -f boot.o kernel.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin