Makefile 1.5 KB

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