Makefile 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. timer.o: timer.S
  20. $(AS) $(ASFLAGS) -o $@ $<
  21. kernel.elf: kernel.o timer.o
  22. $(LD) $(KERNEL_LDFLAGS) -o $@ $^
  23. boot.bin: boot.elf
  24. $(OBJCOPY) -O binary --only-section=.boot $< $@.tmp
  25. dd if=$@.tmp of=$@ bs=1 count=510 2>/dev/null
  26. printf '\x55\xaa' >> $@
  27. rm -f $@.tmp
  28. kernel.bin: kernel.elf
  29. $(OBJCOPY) -O binary $< $@
  30. image.bin: boot.bin kernel.bin
  31. dd if=/dev/zero of=$@ bs=512 count=2880 2>/dev/null
  32. dd if=boot.bin of=$@ bs=512 conv=notrunc 2>/dev/null
  33. dd if=kernel.bin of=$@ bs=512 seek=1 conv=notrunc 2>/dev/null
  34. run: image.bin
  35. qemu-system-i386 -M pc -drive file=image.bin,format=raw 2>/dev/null || qemu-system-x86_64 -M pc -drive file=image.bin,format=raw
  36. clean:
  37. rm -f boot.o kernel.o timer.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin