Makefile 1.5 KB

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