Makefile 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 io.h terminal.h timer.h
  16. $(CC) $(CFLAGS) -o $@ $<
  17. io.o: io.c io.h
  18. $(CC) $(CFLAGS) -o $@ $<
  19. terminal.o: terminal.c terminal.h io.h rtc.h timer.h
  20. $(CC) $(CFLAGS) -o $@ $<
  21. hello.o: hello.c io.h
  22. $(CC) $(CFLAGS) -o $@ $<
  23. rtc.o: rtc.c rtc.h
  24. $(CC) $(CFLAGS) -o $@ $<
  25. boot.elf: boot.o
  26. $(LD) $(BOOT_LDFLAGS) -o $@ $<
  27. timer.o: timer.S
  28. $(AS) $(ASFLAGS) -o $@ $<
  29. kernel.elf: kernel.o timer.o io.o rtc.o terminal.o hello.o
  30. $(LD) $(KERNEL_LDFLAGS) -o $@ $^
  31. boot.bin: boot.elf
  32. $(OBJCOPY) -O binary --only-section=.boot $< $@.tmp
  33. dd if=$@.tmp of=$@ bs=1 count=510 2>/dev/null
  34. printf '\x55\xaa' >> $@
  35. rm -f $@.tmp
  36. kernel.bin: kernel.elf
  37. $(OBJCOPY) -O binary $< $@
  38. image.bin: boot.bin kernel.bin
  39. @KERNEL_SECTORS=$$(( ($$(wc -c < kernel.bin) + 511) / 512 )); \
  40. TOTAL_SECTORS=$$(( 1 + KERNEL_SECTORS )); \
  41. dd if=/dev/zero of=$@ bs=512 count=$$TOTAL_SECTORS 2>/dev/null; \
  42. dd if=boot.bin of=$@ bs=512 conv=notrunc 2>/dev/null; \
  43. dd if=kernel.bin of=$@ bs=512 seek=1 conv=notrunc 2>/dev/null
  44. run: image.bin
  45. 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
  46. clean:
  47. rm -f boot.o kernel.o timer.o io.o rtc.o terminal.o hello.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp image.bin