Makefile 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. SIMPLEBOOT=../src/simpleboot
  2. KERNEL?=mykernel
  3. OVMF?=/usr/share/qemu/bios-TianoCoreEFI.bin
  4. # compile a "Linux" kernel
  5. #LINUX=1
  6. # compile a Multiboot COFF/PE kernel (otherwise ELF)
  7. #PE=1
  8. # compile a Multiboot 32-bit kernel
  9. #MB32=1
  10. # compile a Multiboot 64-bit kernel for Raspberry Pi
  11. #RPI=1
  12. # test Symmetric MultiProcessor support
  13. #SMP=1
  14. # create El Torito hybrid disk image
  15. #CDROM=1
  16. CFLAGS?=-Wall -ffreestanding -fno-stack-protector -nostdlib -static -I..
  17. ifeq ($(MB32),)
  18. GCCFLG=-m64
  19. CLANGFLG=--target=x86_64-pc-win32-coff
  20. else
  21. GCCFLG=-m32
  22. CLANGFLG=--target=i386-pc-win32-coff
  23. endif
  24. ifneq ($(RPI),)
  25. SBFLAGS=-s 64
  26. endif
  27. ifeq ($(HI),)
  28. BASE=0x10
  29. else
  30. BASE=0xffffffffffe0
  31. endif
  32. ifneq ($(ROM),)
  33. SBFLAGS+=-r
  34. QEMUFLG=-option-rom sb_bios.rom
  35. endif
  36. ifneq ($(CDROM),)
  37. SBFLAGS+=-e
  38. endif
  39. all: disk.img
  40. # compile the kernel
  41. boot/$(KERNEL): kernel.c linux.c ../simpleboot.h
  42. @mkdir -p boot
  43. ifneq ($(RPI),)
  44. ifeq ($(PE),)
  45. clang --target=aarch64-elf -fno-strict-aliasing $(CFLAGS) -Wl,-Ttext=$(BASE)0000 -Wl,--omagic kernel.c -o boot/$(KERNEL)
  46. else
  47. clang --target=aarch64-win32-coff -fno-strict-aliasing $(CFLAGS) -c kernel.c -o kernel.o
  48. lld -flavor link -subsystem:console -nodefaultlib -base:$(BASE)0000 -entry:_start kernel.o -out:boot/$(KERNEL)
  49. @rm kernel.o
  50. endif
  51. else
  52. ifeq ($(LINUX),)
  53. ifeq ($(PE),)
  54. gcc $(GCCFLG) $(CFLAGS) -Wl,-Ttext=$(BASE)1000 kernel.c -o boot/$(KERNEL)
  55. else
  56. clang $(CLANGFLG) -fno-strict-aliasing $(CFLAGS) -c kernel.c -o kernel.o
  57. lld -flavor link -subsystem:console -nodefaultlib -base:$(BASE)0000 -entry:_start kernel.o -out:boot/$(KERNEL)
  58. @rm kernel.o
  59. endif
  60. else
  61. gcc $(CFLAGS) -Wl,-Ttext=$(BASE)0000 linux.c -o kernel.o
  62. strip --remove-section=.note.gnu.property --remove-section=.note.gnu.build-id --remove-section=.comment kernel.o
  63. objcopy -O binary kernel.o boot/$(KERNEL)
  64. @rm kernel.o
  65. endif
  66. endif
  67. # compile the simpleboot image generator
  68. $(SIMPLEBOOT):
  69. @make --no-print-directory -C ../src simpleboot
  70. # generate a bootable disk image with our kernel in it
  71. disk.img: boot/$(KERNEL) $(SIMPLEBOOT)
  72. ifneq ($(SMP),)
  73. @echo "multicore" > boot/simpleboot.cfg
  74. endif
  75. $(SIMPLEBOOT) $(SBFLAGS) -vv -k "$(KERNEL)" boot disk.img
  76. # mount the generated disk (requires root priviledge)
  77. mnt: disk.img
  78. @mkdir -p mnt || true
  79. sudo mount -o loop,offset=1048576,user,umask=000 disk.img mnt
  80. # test in a UEFI machine (as EFI CDROM)
  81. eficdrom: disk.img
  82. qemu-system-x86_64 -bios $(OVMF) -m 256 -cdrom disk.img -serial stdio
  83. @printf '\033[0m'
  84. # test in a UEFI machine
  85. efi: disk.img
  86. qemu-system-x86_64 -bios $(OVMF) -m 256 -drive file=disk.img,format=raw -serial stdio
  87. @printf '\033[0m'
  88. # test in a BIOS machine (as BIOS CDROM)
  89. cdrom: disk.img
  90. qemu-system-x86_64 -m 256 -cdrom disk.img -serial stdio
  91. # test in a BIOS machine
  92. qemu: disk.img
  93. ifeq ($(RPI),)
  94. qemu-system-x86_64 $(QEMUFLG) -m 256 -drive file=disk.img,format=raw -serial stdio -smp cpus=2
  95. else
  96. @make --no-print-directory rpi
  97. endif
  98. # test with another, non-qemu VM too
  99. bochs: disk.img
  100. bochs -f bochs.rc -q
  101. # test in a Raspberry Pi 3B
  102. rpi: disk.img
  103. @mkdir mnt && sudo mount -o loop,offset=1048576,user,umask=000 disk.img mnt
  104. qemu-system-aarch64 -M raspi3b -kernel mnt/KERNEL8.IMG -drive file=disk.img,if=sd,format=raw -serial stdio || true
  105. @sudo umount mnt && rmdir mnt
  106. # test with coreboot
  107. cb: disk.img
  108. qemu-system-x86_64 -bios ../distrib/coreboot.rom -drive file=disk.img,format=raw -serial stdio -smp cpus=2
  109. # debug in a UEFI machine (stop before executing)
  110. efidbg: disk.img
  111. qemu-system-x86_64 -s -S -d int -bios $(OVMF) -m 256 -drive file=disk.img,format=raw -serial stdio -smp cpus=2 || true
  112. @printf '\033[0m'
  113. # debug in a BIOS machine (stop before executing)
  114. qemudbg: disk.img
  115. qemu-system-x86_64 -s -S -d int -m 256 -drive file=disk.img,format=raw -serial stdio -smp cpus=2
  116. # start the debugger and attach it to the stopped VM
  117. gdb:
  118. gdb -w -x gdb.rc || true
  119. pkill qemu
  120. clean:
  121. @(test -d mnt && (sudo umount mnt || true) && rmdir mnt) || true
  122. rm -rf boot disk.img kernel.o *.rom 2>/dev/null || true
  123. distclean: clean