Makefile 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. # Bootloader: 512-byte boot sector + contiguous kernel at sector 1. Builds image.bin for QEMU.
  2. CC = gcc
  3. AS = as
  4. LD = ld
  5. OBJCOPY = objcopy
  6. # -march=i586: first-gen Pentium (P5); avoids CMOV etc. (P6+)
  7. CFLAGS = -ffreestanding -fno-pie -m32 -march=i586 -nostdlib -fno-stack-protector \
  8. -fno-asynchronous-unwind-tables -mno-sse -mno-sse2 -mno-mmx \
  9. -Wall -Wextra -O2 -c
  10. ASFLAGS = --32 -I.
  11. BOOT_LDFLAGS = -T linker.ld -nostdlib -m elf_i386
  12. KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
  13. # NOWM=1 builds a diagnostic kernel that boots to the console without the
  14. # window manager (make nowm).
  15. NOWM ?= 0
  16. ifeq ($(NOWM),1)
  17. CFLAGS += -DNOWM
  18. endif
  19. # UEFI loader: a PE32+ x86-64 EFI application built with the mingw toolchain.
  20. # The kernel itself stays 32-bit; the loader leaves long mode before jumping.
  21. EFI_CC ?= x86_64-w64-mingw32-gcc
  22. EFI_AS ?= x86_64-w64-mingw32-as
  23. EFI_CFLAGS = -ffreestanding -fno-stack-protector -mno-red-zone -fno-builtin \
  24. -fno-asynchronous-unwind-tables -fno-unwind-tables -O2 \
  25. -Wall -Wextra -I. -Iefi
  26. EFI_LDFLAGS = -nostdlib -Wl,--subsystem,10 -Wl,-e,efi_main \
  27. -Wl,--image-base,0x0 -Wl,--dynamicbase \
  28. -Wl,--enable-reloc-section -Wl,--no-insert-timestamp
  29. .PHONY: all clean run run-smp run-floppy run-usb run-uhci run-xhci run-xhci-asix run-net run-e1000 run-ahci test-usb run-serial floppy usb uefi run-uefi test-uefi flash space prog bootstrap dev net-dev cmds-dev net-cmds browser-dev gallery-dev netmon monitor test-crypto test-images test-images-asan kernel-dev kernel-net-dev rootfs
  30. all: floppy.img usb.img final.bin root.img
  31. boot.o: boot.S
  32. $(AS) $(ASFLAGS) -o $@ $<
  33. kernel.o: kernel.c acpi.h cpu.h idle.h io.h lapic.h mouse.h panic.h process.h sched.h smp.h terminal.h timer.h serial.h tss.h usbhid.h version.h vga.h
  34. $(CC) $(CFLAGS) -o $@ $<
  35. screen.o: screen.c screen.h
  36. $(CC) $(CFLAGS) -o $@ $<
  37. io.o: io.c io.h fbtext.h process.h screen.h tty.h
  38. $(CC) $(CFLAGS) -o $@ $<
  39. tty.o: tty.c tty.h mm.h netlog.h process.h
  40. $(CC) $(CFLAGS) -o $@ $<
  41. font_8x8.o: font_8x8.c font_8x8.h
  42. $(CC) $(CFLAGS) -o $@ $<
  43. font_8x16.o: font_8x16.c font_8x16.h
  44. $(CC) $(CFLAGS) -o $@ $<
  45. fbtext.o: fbtext.c fbtext.h font_8x8.h vga.h
  46. $(CC) $(CFLAGS) -o $@ $<
  47. terminal.o: terminal.c terminal.h screen.h vfs.h fat.h blkdev.h io.h dbg.h kexec.h process.h timer.h
  48. $(CC) $(CFLAGS) -Os -o $@ $<
  49. wm.o: wm.c wm.h wm_abi.h terminal.h screen.h fbtext.h idle.h io.h mouse.h process.h paging.h rtc.h timer.h version.h vga.h vfs.h
  50. $(CC) $(CFLAGS) -o $@ $<
  51. sys_wm.o: sys_wm.c sys_wm.h wm_abi.h wm.h paging.h process.h
  52. $(CC) $(CFLAGS) -o $@ $<
  53. hello.o: hello.c
  54. $(CC) $(CFLAGS) -o $@ $<
  55. reboot.o: reboot.c
  56. $(CC) $(CFLAGS) -o $@ $<
  57. poweroff.o: poweroff.c
  58. $(CC) $(CFLAGS) -o $@ $<
  59. disk.o: disk.c disk.h io.h timer.h
  60. $(CC) $(CFLAGS) -Os -o $@ $<
  61. fat.o: fat.c fat.h blkdev.h io.h
  62. $(CC) $(CFLAGS) -Os -o $@ $<
  63. run.o: run.c fat.h io.h fbtext.h run_api.h timer.h vga.h vfs.h
  64. $(CC) $(CFLAGS) -o $@ $<
  65. rtc.o: rtc.c rtc.h
  66. $(CC) $(CFLAGS) -o $@ $<
  67. serial.o: serial.c serial.h serial_ring_buf.h pci.h idle.h sched.h timer.h
  68. $(CC) $(CFLAGS) -o $@ $<
  69. serial_ring_buf.o: serial_ring_buf.c serial_ring_buf.h
  70. $(CC) $(CFLAGS) -o $@ $<
  71. serial_loop.o: serial_loop.c serial.h io.h timer.h
  72. $(CC) $(CFLAGS) -o $@ $<
  73. bin_recv.o: bin_recv.c bin_recv.h serial.h io.h timer.h
  74. $(CC) $(CFLAGS) -o $@ $<
  75. serial_recv.o: serial_recv.c serial.h bin_recv.h io.h
  76. $(CC) $(CFLAGS) -o $@ $<
  77. vga.o: vga.c vga.h bootinfo.h blit.h pci.h gpu.h ati.h process.h
  78. $(CC) $(CFLAGS) -o $@ $<
  79. blit.o: blit.c blit.h vga.h gfx.h cpu.h
  80. $(CC) $(CFLAGS) -o $@ $<
  81. cpu.o: cpu.c cpu.h
  82. $(CC) $(CFLAGS) -o $@ $<
  83. thermal.o: thermal.c thermal.h cpu.h sched.h serial.h
  84. $(CC) $(CFLAGS) -o $@ $<
  85. it87.o: it87.c it87.h pci.h
  86. $(CC) $(CFLAGS) -o $@ $<
  87. gfxbench.o: gfxbench.c blit.h cpu.h fbtext.h gfx.h io.h mm.h timer.h vga.h
  88. $(CC) $(CFLAGS) -o $@ $<
  89. vga_copy.o: vga_copy.S
  90. $(AS) $(ASFLAGS) -o $@ $<
  91. gpu.o: gpu.c gpu.h pci.h ati.h cirrus.h nvidia.h
  92. $(CC) $(CFLAGS) -o $@ $<
  93. ati.o: ati.c ati.h pci.h radeon.h
  94. $(CC) $(CFLAGS) -o $@ $<
  95. cirrus.o: cirrus.c cirrus.h cursor_shape.h pci.h vga.h
  96. $(CC) $(CFLAGS) -o $@ $<
  97. nvidia.o: nvidia.c nvidia.h nv50.h pci.h process.h serial.h
  98. $(CC) $(CFLAGS) -Os -o $@ $<
  99. nv50.o: nv50.c nv50.h nvidia.h serial.h
  100. $(CC) $(CFLAGS) -Os -o $@ $<
  101. radeon.o: radeon.c radeon.h cursor_shape.h pci.h paging.h serial.h vga.h
  102. $(CC) $(CFLAGS) -o $@ $<
  103. mtrr.o: mtrr.c mtrr.h serial.h
  104. $(CC) $(CFLAGS) -o $@ $<
  105. gfx.o: gfx.c gfx.h mm.h vga.h
  106. $(CC) $(CFLAGS) -o $@ $<
  107. sys_gfx.o: sys_gfx.c sys_gfx.h gfx.h paging.h process.h wm.h wm_abi.h syscall.h
  108. $(CC) $(CFLAGS) -o $@ $<
  109. display.o: display.c io.h vga.h gpu.h ati.h nvidia.h radeon.h mouse.h fbtext.h
  110. $(CC) $(CFLAGS) -o $@ $<
  111. uhci.o: uhci.c uhci.h usb_hcd.h pci.h serial.h timer.h
  112. $(CC) $(CFLAGS) -Os -o $@ $<
  113. ohci.o: ohci.c ohci.h usb_hcd.h pci.h paging.h mm.h serial.h timer.h
  114. $(CC) $(CFLAGS) -Os -o $@ $<
  115. xhci.o: xhci.c xhci.h usb_hcd.h pci.h paging.h mm.h serial.h timer.h spinlock.h
  116. $(CC) $(CFLAGS) -Os -o $@ $<
  117. asix.o: asix.c asix.h netdev.h usb.h io.h serial.h timer.h
  118. $(CC) $(CFLAGS) -Os -o $@ $<
  119. usb.o: usb.c usb.h usb_hcd.h io.h serial.h timer.h
  120. $(CC) $(CFLAGS) -Os -o $@ $<
  121. usbhid.o: usbhid.c usbhid.h io.h usb.h usb_hcd.h serial.h timer.h
  122. $(CC) $(CFLAGS) -o $@ $<
  123. lsusb.o: lsusb.c io.h usb.h
  124. $(CC) $(CFLAGS) -Os -o $@ $<
  125. panic.o: panic.c panic.h bootinfo.h font_8x8.h process.h serial.h timer.h vga.h
  126. $(CC) $(CFLAGS) -o $@ $<
  127. bootcon.o: bootcon.c bootcon.h bootinfo.h font_8x8.h vga.h
  128. $(CC) $(CFLAGS) -o $@ $<
  129. lspci.o: lspci.c io.h pci.h
  130. $(CC) $(CFLAGS) -Os -o $@ $<
  131. peek.o: peek.c io.h
  132. $(CC) $(CFLAGS) -Os -o $@ $<
  133. ne2000.o: ne2000.c ne2000.h pci.h timer.h io.h
  134. $(CC) $(CFLAGS) -Os -o $@ $<
  135. r8168.o: r8168.c r8168.h netdev.h io.h mm.h paging.h pci.h process.h serial.h timer.h
  136. $(CC) $(CFLAGS) -Os -o $@ $<
  137. e1000e.o: e1000e.c e1000e.h io.h mm.h netdev.h paging.h pci.h process.h serial.h timer.h
  138. $(CC) $(CFLAGS) -Os -o $@ $<
  139. netkexec.o: netkexec.c netkexec.h io.h kexec.h net.h netdev.h timer.h
  140. $(CC) $(CFLAGS) -o $@ $<
  141. netlog.o: netlog.c netlog.h net.h netdev.h timer.h
  142. $(CC) $(CFLAGS) -o $@ $<
  143. dhcp.o: dhcp.c dhcp.h io.h net.h netdev.h timer.h
  144. $(CC) $(CFLAGS) -o $@ $<
  145. dns.o: dns.c dns.h io.h timer.h netdev.h net.h
  146. $(CC) $(CFLAGS) -o $@ $<
  147. tcp.o: tcp.c tcp.h io.h timer.h netdev.h net.h
  148. $(CC) $(CFLAGS) -o $@ $<
  149. netdev.o: netdev.c netdev.h ne2000.h r8168.h e1000e.h
  150. $(CC) $(CFLAGS) -o $@ $<
  151. net.o: net.c net.h io.h netdev.h
  152. $(CC) $(CFLAGS) -o $@ $<
  153. blkdev.o: blkdev.c blkdev.h ata.h ahci.h disk.h
  154. $(CC) $(CFLAGS) -Os -o $@ $<
  155. ata.o: ata.c ata.h pci.h serial.h timer.h
  156. $(CC) $(CFLAGS) -o $@ $<
  157. ahci.o: ahci.c ahci.h ata.h mm.h paging.h pci.h process.h serial.h timer.h
  158. $(CC) $(CFLAGS) -o $@ $<
  159. fatdetect.o: fatdetect.c fatdetect.h fat.h blkdev.h
  160. $(CC) $(CFLAGS) -Os -o $@ $<
  161. fatformat.o: fatformat.c fatformat.h blkdev.h io.h mm.h sched.h
  162. $(CC) $(CFLAGS) -Os -o $@ $<
  163. format.o: format.c blkdev.h disk.h fatformat.h io.h process.h sched.h vga.h vfs.h
  164. $(CC) $(CFLAGS) -Os -o $@ $<
  165. mouse.o: mouse.c mouse.h bootinfo.h cirrus.h cursor_shape.h radeon.h timer.h usbhid.h vga.h
  166. $(CC) $(CFLAGS) -o $@ $<
  167. idle.o: idle.c idle.h
  168. $(CC) $(CFLAGS) -o $@ $<
  169. kexec.o: kexec.c kexec.h io.h serial.h smp.h
  170. $(CC) $(CFLAGS) -o $@ $<
  171. kexec_tramp.o: kexec_tramp.S
  172. $(AS) $(ASFLAGS) -o $@ $<
  173. dbg.o: dbg.c dbg.h serial.h
  174. $(CC) $(CFLAGS) -Os -o $@ $<
  175. vfs.o: vfs.c vfs.h fat.h fatdetect.h blkdev.h io.h
  176. $(CC) $(CFLAGS) -Os -o $@ $<
  177. boot.elf: boot.o
  178. $(LD) $(BOOT_LDFLAGS) -o $@ $<
  179. vesa_stage.o: vesa_stage.S
  180. $(AS) $(ASFLAGS) -o $@ $<
  181. timer.o: timer.S irqret.inc
  182. $(AS) $(ASFLAGS) -o $@ $<
  183. switch.o: switch.S irqret.inc
  184. $(AS) $(ASFLAGS) -o $@ $<
  185. entry.o: entry.S
  186. $(AS) $(ASFLAGS) -o $@ $<
  187. sched.o: sched.c sched.h process.h paging.h smp.h spinlock.h sys_fs.h thermal.h timer.h tss.h vga.h
  188. $(CC) $(CFLAGS) -o $@ $<
  189. acpi.o: acpi.c acpi.h bootinfo.h paging.h serial.h
  190. $(CC) $(CFLAGS) -o $@ $<
  191. lapic.o: lapic.c lapic.h timer.h
  192. $(CC) $(CFLAGS) -o $@ $<
  193. smp.o: smp.c smp.h acpi.h idle.h lapic.h paging.h process.h sched.h serial.h timer.h tss.h
  194. $(CC) $(CFLAGS) -o $@ $<
  195. ap_tramp.o: ap_tramp.S
  196. $(AS) $(ASFLAGS) -o $@ $<
  197. ap_tramp.elf: ap_tramp.o ap_tramp.ld
  198. $(LD) -T ap_tramp.ld -nostdlib -m elf_i386 -o $@ $<
  199. ap_tramp.bin: ap_tramp.elf
  200. $(OBJCOPY) -O binary $< $@
  201. ap_tramp_embed.o: ap_tramp.bin
  202. $(LD) -m elf_i386 -r -b binary -o ap_tramp_embed.o ap_tramp.bin
  203. process.o: process.c process.h sched.h io.h kmalloc.h mm.h serial.h smp.h idle.h timer.h wm_abi.h
  204. $(CC) $(CFLAGS) -o $@ $<
  205. mm.o: mm.c mm.h
  206. $(CC) $(CFLAGS) -o $@ $<
  207. kmalloc.o: kmalloc.c kmalloc.h mm.h
  208. $(CC) $(CFLAGS) -o $@ $<
  209. paging.o: paging.c paging.h mm.h mtrr.h vga.h serial.h
  210. $(CC) $(CFLAGS) -o $@ $<
  211. elf.o: elf.c elf.h mm.h paging.h
  212. $(CC) $(CFLAGS) -o $@ $<
  213. tss.o: tss.c tss.h sched.h
  214. $(CC) $(CFLAGS) -o $@ $<
  215. syscall.o: syscall.c syscall.h io.h process.h timer.h sys_wm.h wm_abi.h
  216. $(CC) $(CFLAGS) -o $@ $<
  217. sys_fs.o: sys_fs.c sys_fs.h syscall.h fat.h vfs.h
  218. $(CC) $(CFLAGS) -o $@ $<
  219. sys_mem.o: sys_mem.c sys_mem.h mm.h paging.h process.h
  220. $(CC) $(CFLAGS) -o $@ $<
  221. inflate.o: inflate.c inflate.h
  222. $(CC) $(CFLAGS) -o $@ $<
  223. sys_info.o: sys_info.c sys_info.h syscall.h ahci.h blkdev.h it87.h mm.h mouse.h thermal.h usbhid.h vfs.h
  224. $(CC) $(CFLAGS) -o $@ $<
  225. sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h process.h sched.h
  226. $(CC) $(CFLAGS) -o $@ $<
  227. syscall_stub.o: syscall_stub.S irqret.inc
  228. $(AS) $(ASFLAGS) -o $@ $<
  229. kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o prog_embed.o space_embed.o inflate.o ramfs.o share.o payload.o
  230. $(LD) $(KERNEL_LDFLAGS) -o $@ $(filter-out kernel.ld,$^)
  231. MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
  232. mbr.o: mbr.S
  233. $(AS) $(ASFLAGS) -o $@ $<
  234. mbr.elf: mbr.o
  235. $(LD) $(MBR_LDFLAGS) -o $@ $<
  236. mbr.bin: mbr.elf
  237. $(OBJCOPY) -O binary $< $@
  238. boot.bin: boot.elf
  239. $(OBJCOPY) -O binary --only-section=.boot $< $@.tmp
  240. dd if=$@.tmp of=$@ bs=1 count=510 2>/dev/null
  241. printf '\x55\xaa' >> $@
  242. rm -f $@.tmp
  243. kernel.bin: kernel.elf
  244. $(OBJCOPY) -O binary $< $@
  245. # Byte offsets inside boot.bin (see boot.S). Keep decimal: dd's seek= does not
  246. # parse 0x… literals (it silently treats them as 0) and the error is hidden by
  247. # the 2>/dev/null below.
  248. # 62 = kernel_sectors field (0x3E)
  249. # 64 = kernel_lba field (0x40, word; LBA 64 fits in the low byte)
  250. # 66 = pack_lba field (0x42, dword; rootfs image LBA)
  251. # 26/28/32 = BPB: heads (0x1A, word), hidden sectors (0x1C, dword),
  252. # total sectors 32-bit (0x20, dword)
  253. KERNEL_SECTORS_OFFSET = 62
  254. BOOT_SECTOR_START_OFFSET = 64
  255. PACK_LBA_OFFSET = 66
  256. BPB_HEADS_OFFSET = 26
  257. BPB_HIDDEN_OFFSET = 28
  258. BPB_TOTAL_OFFSET = 32
  259. # USB rootfs partition (type 0x7F) and UEFI ESP (type 0xEF). The rootfs
  260. # sits immediately behind the kernel (LBA 532), so even a CHS-only USB BIOS
  261. # finds the header and the 242 KB gzip pack inside the first 1.44 MB.
  262. ROOTFS_LBA = 532
  263. ROOTFS_SECTORS = 16384
  264. ESP_LBA = 16916
  265. # Patch a little-endian 16/32-bit value into a boot-sector copy. Usage:
  266. # $(call patch16,file,offset,value)
  267. # (shell snippets are inlined below because make functions cannot easily
  268. # expand `$$(...)` arithmetic; the helpers keep the rules readable.)
  269. patch32 = printf "$$(printf '\\x%02x\\x%02x\\x%02x\\x%02x' \
  270. $$(( ( ($(2)) >> 0) & 0xFF )) $$(( ( ($(2)) >> 8) & 0xFF )) \
  271. $$(( ( ($(2)) >> 16) & 0xFF )) $$(( ( ($(2)) >> 24) & 0xFF )))" | \
  272. dd of=$(1) bs=1 seek=$(3) conv=notrunc 2>/dev/null
  273. patch16 = printf "$$(printf '\\x%02x\\x%02x' \
  274. $$(( ( ($(2)) >> 0) & 0xFF )) $$(( ( ($(2)) >> 8) & 0xFF )))" | \
  275. dd of=$(1) bs=1 seek=$(3) conv=notrunc 2>/dev/null
  276. floppy.img: boot.bin kernel.bin rootfs.bin
  277. SECTORS=$$(( ($$(stat -c%s kernel.bin) + 511) / 512 )); \
  278. cp boot.bin $@.boot.tmp; \
  279. $(call patch16,$@.boot.tmp,$$SECTORS,$(KERNEL_SECTORS_OFFSET)); \
  280. $(call patch32,$@.boot.tmp,1 + SECTORS,$(PACK_LBA_OFFSET)); \
  281. cat $@.boot.tmp kernel.bin > $@; \
  282. PAD=$$(( SECTORS * 512 - $$(stat -c%s kernel.bin) )); \
  283. dd if=/dev/zero bs=1 count=$$PAD 2>/dev/null >> $@; \
  284. cat rootfs.bin >> $@; \
  285. truncate -s 1474560 $@; \
  286. rm -f $@.boot.tmp
  287. usb.img: mbr.bin boot.bin kernel.bin cmds.img
  288. SECTORS=$$(( ($$(stat -c%s kernel.bin) + 511) / 512 )); \
  289. cp boot.bin $@.boot.tmp; \
  290. $(call patch16,$@.boot.tmp,$$SECTORS,$(KERNEL_SECTORS_OFFSET)); \
  291. printf '\x40' | dd of=$@.boot.tmp bs=1 seek=$(BOOT_SECTOR_START_OFFSET) conv=notrunc 2>/dev/null; \
  292. $(call patch32,$@.boot.tmp,$(ROOTFS_LBA),$(PACK_LBA_OFFSET)); \
  293. printf '\xff\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_HEADS_OFFSET) conv=notrunc 2>/dev/null; \
  294. printf '\x3f\x00\x00\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_HIDDEN_OFFSET) conv=notrunc 2>/dev/null; \
  295. printf '\xd5\x01\x00\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_TOTAL_OFFSET) conv=notrunc 2>/dev/null; \
  296. cat mbr.bin > $@; \
  297. dd if=/dev/zero bs=512 count=62 2>/dev/null >> $@; \
  298. cat $@.boot.tmp kernel.bin >> $@; \
  299. truncate -s 9M $@; \
  300. dd if=cmds.img of=$@ bs=512 seek=$(ROOTFS_LBA) conv=notrunc 2>/dev/null; \
  301. printf '\x00\x08\x1d\x00\x7f\x0d\x20\x01\x14\x02\x00\x00\x00\x40\x00\x00' | \
  302. dd of=$@ bs=1 seek=462 conv=notrunc 2>/dev/null; \
  303. rm -f $@.boot.tmp
  304. image.bin: floppy.img
  305. cp floppy.img image.bin
  306. final.bin: boot.bin kernel.bin rootfs.bin
  307. SECTORS=$$(( ($$(stat -c%s kernel.bin) + 511) / 512 )); \
  308. cp boot.bin $@.boot.tmp; \
  309. $(call patch16,$@.boot.tmp,$$SECTORS,$(KERNEL_SECTORS_OFFSET)); \
  310. $(call patch32,$@.boot.tmp,1 + SECTORS,$(PACK_LBA_OFFSET)); \
  311. cat $@.boot.tmp kernel.bin > $@; \
  312. PAD=$$(( SECTORS * 512 - $$(stat -c%s kernel.bin) )); \
  313. dd if=/dev/zero bs=1 count=$$PAD 2>/dev/null >> $@; \
  314. cat rootfs.bin >> $@; \
  315. rm -f $@.boot.tmp
  316. # ---- UEFI loader and dual-boot USB image -------------------------------
  317. #
  318. # The legacy image (usb.img) is left byte-for-byte alone. usb-efi.img copies
  319. # it, adds a third MBR partition (type 0xEF, starting at the old 64 MB end)
  320. # and writes a small FAT16 ESP there. BIOS booting still uses the active
  321. # partition 1 at LBA 63; UEFI finds BOOTX64.EFI in the ESP.
  322. efi/efi_entry.h: kernel.elf
  323. @mkdir -p efi
  324. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  325. if [ -z "$$entry" ]; then echo "efi: kernel_entry not found" >&2; exit 1; fi; \
  326. printf '#define KERNEL_ENTRY 0x%s\n' "$$entry" > $@
  327. efi/main.o: efi/main.c efi/efi.h bootinfo.h vga.h efi/efi_entry.h
  328. $(EFI_CC) $(EFI_CFLAGS) -c -o $@ $<
  329. efi/payload.o: efi/payload.S kernel.bin cmds_pack_gz.bin efi/tramp.bin
  330. $(EFI_AS) --64 -o $@ $<
  331. efi/tramp.o: efi/tramp.S
  332. $(AS) --64 -o $@ $<
  333. efi/tramp.elf: efi/tramp.o efi/tramp.ld
  334. $(LD) -T efi/tramp.ld -nostdlib -m elf_x86_64 -o $@ $<
  335. efi/tramp.bin: efi/tramp.elf
  336. $(OBJCOPY) -O binary $< $@
  337. BOOTX64.EFI: efi/main.o efi/payload.o
  338. $(EFI_CC) $(EFI_LDFLAGS) -o $@ efi/main.o efi/payload.o
  339. ESP_SECTORS = 16384
  340. esp.img: BOOTX64.EFI
  341. truncate -s $$(( $(ESP_SECTORS) * 512 )) $@
  342. mkfs.fat -F 16 -s 2 -n NEBOSESP $@
  343. mmd -i $@ ::/EFI
  344. mmd -i $@ ::/EFI/BOOT
  345. mcopy -i $@ BOOTX64.EFI ::/EFI/BOOT/BOOTX64.EFI
  346. usb-efi.img: usb.img esp.img
  347. cp usb.img $@
  348. # The legacy partition is a raw kernel blob with a FAT16-looking BPB (the
  349. # BIOS only needs the boot sector and LBA 64). UEFI firmware scans FAT
  350. # partitions and would mount it as a volume, writing FAT/root pages over
  351. # the kernel at LBA 64. Clear BPB FATSize16 so the firmware rejects the
  352. # fake volume; boot.S and the BIOS never read that field.
  353. printf '\x00\x00' | dd of=$@ bs=1 seek=$$((63 * 512 + 0x16)) conv=notrunc 2>/dev/null
  354. # Partition 3: ESP at LBA 16916, type 0xEF, 8 MB (partition 2 is the
  355. # rootfs at LBA 532 written by usb.img).
  356. printf '\x00\xfe\xff\xff\xef\xfe\xff\xff\x14\x42\x00\x00\x00\x40\x00\x00' | \
  357. dd of=$@ bs=1 seek=478 conv=notrunc 2>/dev/null
  358. dd if=esp.img of=$@ bs=512 seek=$(ESP_LBA) conv=notrunc 2>/dev/null
  359. truncate -s $$(( ($(ESP_LBA) + $(ESP_SECTORS)) * 512 )) $@
  360. uefi: usb-efi.img
  361. OVMF ?= $(firstword $(wildcard /usr/share/ovmf/x64/OVMF.4m.fd /usr/share/OVMF/OVMF.4m.fd))
  362. # root.img is deliberately not attached: a running QEMU may hold its write
  363. # lock, and the UEFI boot test does not need a second filesystem.
  364. run-uefi: usb-efi.img
  365. @test -n "$(OVMF)" || { echo "OVMF not found; set OVMF=/path/to/OVMF.4m.fd" >&2; exit 1; }
  366. qemu-system-x86_64 -M pc -m 256 -smp 4 -bios $(OVMF) -snapshot \
  367. -drive file=usb-efi.img,format=raw,if=ide \
  368. -device pci-ohci -device usb-mouse \
  369. -device ne2k_pci,netdev=n0 -netdev user,id=n0 \
  370. -serial file:/tmp/opencode/nebos-uefi-serial.log
  371. test-uefi: usb-efi.img
  372. @test -n "$(OVMF)" || { echo "OVMF not found; set OVMF=/path/to/OVMF.4m.fd" >&2; exit 1; }
  373. @rm -f /tmp/opencode/nebos-uefi-serial.log
  374. timeout 40 qemu-system-x86_64 -M pc -m 256 -smp 4 -bios $(OVMF) -snapshot \
  375. -drive file=usb-efi.img,format=raw,if=ide \
  376. -device pci-ohci -device usb-mouse \
  377. -device ne2k_pci,netdev=n0 -netdev user,id=n0 \
  378. -display none -serial file:/tmp/opencode/nebos-uefi-serial.log \
  379. -no-reboot >/dev/null 2>&1 || true
  380. @grep -q "booted" /tmp/opencode/nebos-uefi-serial.log && \
  381. echo "UEFI boot: OK (/tmp/opencode/nebos-uefi-serial.log)" || \
  382. { echo "UEFI boot: FAILED"; tail -c 4000 /tmp/opencode/nebos-uefi-serial.log; exit 1; }
  383. prog.o: prog.c run_api.h
  384. $(CC) $(CFLAGS) -o $@ $<
  385. prog.elf: prog.o
  386. $(LD) -T user.ld -nostdlib -m elf_i386 -o $@ $<
  387. prog.bin: prog.elf
  388. $(OBJCOPY) -O binary $< $@
  389. space: space.bin
  390. prog: prog.bin
  391. # Ring-3 commands: freestanding ELFs at USER_BASE, packed into cmds.pack and
  392. # mounted at /ramfs/bin (the shell's PATH default). New command: add
  393. # ucmd_<name>.c to UCMDS; the pack is rebuilt automatically.
  394. UCMDS = ucmd_cpuinfo ucmd_ls ucmd_cat ucmd_save ucmd_rm ucmd_mkdir ucmd_rmdir \
  395. ucmd_meminfo ucmd_mouseinfo ucmd_storage ucmd_top \
  396. ucmd_clock ucmd_colors ucmd_edit ucmd_wm ucmd_term ucmd_spin \
  397. ucmd_netinfo ucmd_netip ucmd_netdns ucmd_host ucmd_dhcp \
  398. ucmd_netsend ucmd_netrecv ucmd_ping ucmd_pong ucmd_netlog \
  399. ucmd_netconsole ucmd_netregs ucmd_netloop ucmd_netreset ucmd_curl \
  400. ucmd_files ucmd_view ucmd_sensors ucmd_mines ucmd_gallery \
  401. ucmd_websrv ucmd_browser
  402. # Keep the linked ELFs around (make would treat them as disposable
  403. # intermediates between the pattern rules).
  404. .PRECIOUS: $(addsuffix .elf,$(UCMDS))
  405. ucmd_%.o: ucmd_%.c ucmd_lib.h syscall.h
  406. $(CC) $(CFLAGS) -o $@ $<
  407. ucmd_%.elf: ucmd_%.o ucmd.ld
  408. $(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $<
  409. $(OBJCOPY) --strip-all $@ $@
  410. # edit draws text itself, so it links the kernel's 8x8 font into its ELF.
  411. ucmd_edit.o: ucmd_edit.c ucmd_lib.h syscall.h font_8x16.h
  412. $(CC) $(CFLAGS) -o $@ $<
  413. ucmd_edit.elf: ucmd_edit.o font_8x16.o ucmd.ld
  414. $(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
  415. $(OBJCOPY) --strip-all $@ $@
  416. # The ring-3 WM draws its own chrome and terminal cells.
  417. ucmd_wm.o: ucmd_wm.c ucmd_lib.h syscall.h wm_abi.h uwm_lib.h version.h
  418. $(CC) $(CFLAGS) -o $@ $<
  419. uwm_lib.o: uwm_lib.c uwm_lib.h font_8x16.h
  420. $(CC) $(CFLAGS) -o $@ $<
  421. ucmd_wm.elf: ucmd_wm.o uwm_lib.o font_8x16.o ucmd.ld
  422. $(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
  423. $(OBJCOPY) --strip-all $@ $@
  424. ucmd_term.o: ucmd_term.c ucmd_lib.h syscall.h wm_abi.h uwm_lib.h io.h vfs.h
  425. $(CC) $(CFLAGS) -o $@ $<
  426. ucmd_term.elf: ucmd_term.o uwm_lib.o font_8x16.o ucmd.ld
  427. $(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
  428. $(OBJCOPY) --strip-all $@ $@
  429. # ngui: the userland immediate-mode GUI toolkit and its apps.
  430. ngui.o: ngui.c ngui.h ucmd_lib.h syscall.h wm_abi.h font_8x16.h
  431. $(CC) $(CFLAGS) -o $@ $<
  432. ucmd_files.o: ucmd_files.c ucmd_lib.h syscall.h wm_abi.h ngui.h img.h
  433. $(CC) $(CFLAGS) -o $@ $<
  434. ucmd_files.elf: ucmd_files.o ngui.o font_8x16.o ucmd.ld
  435. $(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
  436. $(OBJCOPY) --strip-all $@ $@
  437. # mines is a second ngui canvas app; it links the same toolkit.
  438. ucmd_mines.o: ucmd_mines.c ucmd_lib.h syscall.h wm_abi.h ngui.h
  439. $(CC) $(CFLAGS) -o $@ $<
  440. ucmd_mines.elf: ucmd_mines.o ngui.o font_8x16.o ucmd.ld
  441. $(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
  442. $(OBJCOPY) --strip-all $@ $@
  443. # gallery: a live showcase of every ngui widget and primitive. The real ELF
  444. # ships in the rootfs pack as bin/gallery.app; ucmd_gallery.c (in UCMDS) is
  445. # the tiny PATH launcher (which also accepts /share/GALLERY).
  446. gallery.o: gallery.c ucmd_lib.h syscall.h wm_abi.h ngui.h
  447. $(CC) $(CFLAGS) -o $@ $<
  448. gallery.elf: gallery.o ngui.o font_8x16.o ucmd.ld
  449. $(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
  450. $(OBJCOPY) --strip-all $@ $@
  451. # Image library: pure freestanding decoders + RGB565 compositing. Only the
  452. # viewer links them today; a browser can link the same objects later.
  453. IMGLIB_OBJS = img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o \
  454. img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o
  455. $(IMGLIB_OBJS): %.o: %.c img.h img_internal.h inflate.h ucmd_lib.h syscall.h
  456. $(CC) $(CFLAGS) -o $@ $<
  457. # The integer IDCT deliberately relies on 32-bit wrap-around.
  458. img_jpeg.o: CFLAGS += -fwrapv
  459. ucmd_view.o: ucmd_view.c ucmd_lib.h syscall.h wm_abi.h ngui.h img.h img_file.h
  460. $(CC) $(CFLAGS) -o $@ $<
  461. ucmd_view.elf: ucmd_view.o $(IMGLIB_OBJS) inflate.o ngui.o font_8x16.o ucmd.ld
  462. $(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
  463. $(OBJCOPY) --strip-all $@ $@
  464. # TLS 1.3 client library (ring-3, linked into ucmd_curl; see tls.h). The
  465. # crypto files are pure freestanding C, so tls_test can exercise them on
  466. # the host (make test-crypto).
  467. TLSLIB_OBJS = tls_sha256.o tls_chacha20.o tls_x25519.o tls_rng.o tls.o
  468. # Shared ring-3 HTTP/TLS client (curl and the browser).
  469. uhttp.o: uhttp.c uhttp.h ucmd_lib.h tls.h
  470. $(CC) $(CFLAGS) -o $@ $<
  471. ucmd_curl.elf: ucmd_curl.o uhttp.o $(TLSLIB_OBJS) ucmd.ld
  472. $(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter-out ucmd.ld,$^)
  473. $(OBJCOPY) --strip-all $@ $@
  474. # browser: the real app is a disk image (BROWSER) because it links ngui, the
  475. # image library, TLS and uhttp. ucmd_browser.c is the tiny PATH launcher.
  476. browser_html.o: browser_html.c browser_html.h
  477. $(CC) $(CFLAGS) -o $@ $<
  478. browser.o: browser.c ucmd_lib.h syscall.h wm_abi.h ngui.h browser_html.h uhttp.h img.h inflate.h
  479. $(CC) $(CFLAGS) -o $@ $<
  480. browser.elf: browser.o browser_html.o uhttp.o ngui.o font_8x16.o $(IMGLIB_OBJS) inflate.o $(TLSLIB_OBJS) ucmd.ld
  481. $(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter-out ucmd.ld,$^)
  482. $(OBJCOPY) --strip-all $@ $@
  483. tls_sha256.o: tls_sha256.c tls_sha256.h
  484. $(CC) $(CFLAGS) -o $@ $<
  485. tls_chacha20.o: tls_chacha20.c tls_chacha20.h
  486. $(CC) $(CFLAGS) -o $@ $<
  487. tls_x25519.o: tls_x25519.c tls_x25519.h
  488. $(CC) $(CFLAGS) -o $@ $<
  489. tls_rng.o: tls_rng.c tls_rng.h tls_sha256.h ucmd_lib.h syscall.h
  490. $(CC) $(CFLAGS) -o $@ $<
  491. tls.o: tls.c tls.h tls_sha256.h tls_chacha20.h tls_x25519.h tls_rng.h \
  492. ucmd_lib.h syscall.h
  493. $(CC) $(CFLAGS) -o $@ $<
  494. tls_test: tls_test.c tls_sha256.c tls_chacha20.c tls_x25519.c \
  495. tls_sha256.h tls_chacha20.h tls_x25519.h
  496. gcc -O2 -I. -o $@ tls_test.c tls_sha256.c tls_chacha20.c tls_x25519.c
  497. test-crypto: tls_test
  498. ./tls_test
  499. # Host image-library tests (no kernel needed). The corpus and expected hashes
  500. # are committed; tools/gen_img_tests.py regenerates them.
  501. IMGTEST_SRCS = img_test.c img.c img_mem.c img_blit.c img_png.c img_bmp.c \
  502. img_gif.c img_gif_anim.c img_jpeg.c img_tga.c img_pnm.c img_ico.c img_pam.c inflate.c
  503. IMGTEST_DEPS = img.h img_internal.h inflate.h testdata/images/expected.h
  504. img_test: $(IMGTEST_SRCS) $(IMGTEST_DEPS)
  505. gcc -O2 -fwrapv -Wall -Wextra -I. -o $@ $(IMGTEST_SRCS)
  506. test-images: img_test
  507. ./img_test
  508. test-images-asan: $(IMGTEST_SRCS) $(IMGTEST_DEPS)
  509. # -fwrapv: the integer IDCT deliberately relies on 32-bit wrap-around.
  510. gcc -O1 -g -fwrapv -fsanitize=address,undefined -I. -o img_test_asan $(IMGTEST_SRCS)
  511. ./img_test_asan
  512. mkpack: mkpack.c
  513. gcc -O2 -o $@ $<
  514. # Command pack (the system rootfs): the ring-3 command ELFs plus hello and the
  515. # browser/gallery apps, packed into bin/<name>. See mkpack.c, rootfs.h and
  516. # ramfs.h.
  517. PACK_FILES = $(foreach f,$(UCMDS),bin/$(patsubst ucmd_%,%,$(f))=$(f).elf) \
  518. bin/hello=hello.elf \
  519. bin/browser.app=browser.elf \
  520. bin/gallery.app=gallery.elf
  521. cmds.pack: $(addsuffix .elf,$(UCMDS)) hello.elf browser.elf gallery.elf mkpack
  522. ./mkpack $@ $(PACK_FILES)
  523. # The boot-time rootfs is gzip-compressed; the raw cmds.pack is still what
  524. # cmds-dev/net-cmds send for a runtime replacement.
  525. cmds_pack_gz.bin: cmds.pack
  526. gzip -9 -n -c $< > $@
  527. mkrootfs: mkrootfs.c rootfs.h
  528. gcc -O2 -o $@ $<
  529. # Rootfs image: one NBROOT1 header sector + the gzip pack (rootfs.h).
  530. # rootfs.bin is appended directly to the floppy images; cmds.img is the
  531. # 8 MB image flashed into the USB rootfs partition.
  532. rootfs.bin: cmds_pack_gz.bin mkrootfs
  533. ./mkrootfs $@ cmds_pack_gz.bin
  534. rootfs: cmds.img
  535. # Diagnostic: no WM, boot console stays visible. Rebuild normally with
  536. # `make -B ...` afterwards (the objects carry -DNOWM).
  537. nowm:
  538. $(MAKE) NOWM=1 -B kernel.elf kernel.bin floppy.img usb.img usb-efi.img final.bin
  539. @echo "nowm: WM disabled; flash usb.img or usb-efi.img."
  540. cmds.img: cmds_pack_gz.bin mkrootfs
  541. ./mkrootfs $@ cmds_pack_gz.bin $$(( $(ROOTFS_SECTORS) * 512 ))
  542. payload.o: payload.c payload.h io.h mm.h ramfs.h serial.h vfs.h
  543. $(CC) $(CFLAGS) -o $@ $<
  544. ramfs.o: ramfs.c ramfs.h serial.h inflate.h mm.h
  545. $(CC) $(CFLAGS) -o $@ $<
  546. space.o: space.c run_api.h
  547. $(CC) $(CFLAGS) -o $@ $<
  548. space.elf: space.o
  549. $(LD) -T user.ld -nostdlib -m elf_i386 -o $@ $<
  550. space.bin: space.elf
  551. $(OBJCOPY) -O binary $< $@
  552. hello.elf: hello.o
  553. $(LD) -T hello.ld -nostdlib -m elf_i386 -o $@ $<
  554. $(OBJCOPY) --strip-all $@ $@
  555. hello_embed.o: hello.elf
  556. $(LD) -m elf_i386 -r -b binary -o hello_embed.o hello.elf
  557. # User programs ship inside the kernel image so `make dev`/`make net-dev`
  558. # update them too (boards without a readable filesystem can still run them).
  559. prog_embed.o: prog.bin
  560. $(LD) -m elf_i386 -r -b binary -o prog_embed.o prog.bin
  561. space_embed.o: space.bin
  562. $(LD) -m elf_i386 -r -b binary -o space_embed.o space.bin
  563. bootstrap.o: bootstrap.c
  564. $(CC) $(CFLAGS) -o $@ $<
  565. bootstrap.elf: bootstrap.o bootstrap.ld
  566. $(LD) -T bootstrap.ld -nostdlib -m elf_i386 -o $@ $<
  567. bootstrap.bin: bootstrap.elf
  568. $(OBJCOPY) -O binary $< $@
  569. root.img: prog.bin space.bin browser.elf gallery.elf testdata/browser/demo.html testdata/images/rgb.bmp
  570. dd if=/dev/zero of=$@ bs=512 count=2880 2>/dev/null
  571. mkfs.fat -F 12 -n "ROOTDISK" $@
  572. echo "Hello from the second floppy!" | mcopy -i $@ - ::HELLO.TXT
  573. echo "This is a test file on drive 1." | mcopy -i $@ - ::TEST.TXT
  574. mcopy -i $@ prog.bin ::PROG.BIN
  575. mcopy -i $@ space.bin ::SPACE.BIN
  576. mcopy -i $@ browser.elf ::BROWSER
  577. mcopy -i $@ gallery.elf ::GALLERY
  578. mcopy -i $@ testdata/browser/demo.html ::DEMO.HTM
  579. mcopy -i $@ testdata/images/rgb.bmp ::PIC.BMP
  580. run: image.bin root.img
  581. $(MAKE) run-floppy
  582. # Four logical CPUs (the Atom 330 topology). UP is still the default so a
  583. # machine without ACPI/MADT boots exactly as before.
  584. run-smp: floppy.img root.img
  585. qemu-system-i386 -M pc -smp 4 -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
  586. qemu-system-x86_64 -M pc -smp 4 -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
  587. floppy: floppy.img
  588. usb: usb.img
  589. # Write the dual-boot USB image (legacy BIOS + UEFI) to a stick; prompts
  590. # before destroying anything.
  591. # make flash USB=/dev/sdX FORCE=1 skips the removable-device check.
  592. # make flash FLASH_IMG=usb.img legacy-only image
  593. # The UEFI half needs the mingw toolchain (EFI_CC); the legacy half does not.
  594. FLASH_IMG ?= usb-efi.img
  595. flash: $(FLASH_IMG)
  596. @./flash.sh "$(USB)" "$(FORCE)" "$(FLASH_IMG)"
  597. run-floppy: floppy.img root.img
  598. qemu-system-i386 -M pc -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
  599. qemu-system-x86_64 -M pc -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
  600. run-usb: usb.img root.img
  601. qemu-system-i386 -M pc -boot c -device pci-ohci -device usb-mouse -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
  602. qemu-system-x86_64 -M pc -boot c -device pci-ohci -device usb-mouse -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy
  603. # UHCI variant (machines with an Intel/VIA-style controller, and the
  604. # regression test for the UHCI backend of the USB core).
  605. run-uhci: floppy.img root.img
  606. qemu-system-i386 -M pc -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
  607. qemu-system-x86_64 -M pc -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
  608. # xHCI variant (Braswell/Intel PCH and other USB 3.0-only machines).
  609. run-xhci: floppy.img root.img
  610. qemu-system-i386 -M pc -device qemu-xhci -device usb-mouse -device usb-kbd -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
  611. qemu-system-x86_64 -M pc -device qemu-xhci -device usb-mouse -device usb-kbd -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
  612. # Passthrough of the real ASIX AX88772 dongle (requires the host's asix
  613. # driver to be unbound and permission on /dev/bus/usb/...; typically run with
  614. # sudo). `-nic none` keeps the QEMU e1000 out of the way so the USB NIC is
  615. # the only netdev.
  616. ASIX_HOST ?= vendorid=0x0b95,productid=0x7720
  617. run-xhci-asix: floppy.img root.img
  618. qemu-system-i386 -M pc -nic none -device qemu-xhci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
  619. qemu-system-x86_64 -M pc -nic none -device qemu-xhci -device usb-host,$(ASIX_HOST) -serial file:/tmp/opencode/serial-asix.log -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
  620. # Network variant: a PCI NE2000 (RTL8029, the driver's QEMU match) on QEMU's
  621. # user-mode network (slirp). DHCP/DNS come from slirp, the host is 10.0.2.2
  622. # (e.g. `python3 -m http.server` + `curl http://10.0.2.2:8000/` in a window).
  623. run-net: floppy.img root.img
  624. qemu-system-i386 -M pc -device pci-ohci -device usb-mouse -device ne2k_pci,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
  625. qemu-system-x86_64 -M pc -device pci-ohci -device usb-mouse -device ne2k_pci,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
  626. # Intel e1000 variant: QEMU's 82540EM exercises the e1000e driver's MAC/DMA
  627. # path. (The X201's 82577LM uses the same registers; only its PCH PHY is
  628. # different.) usb-kbd is there for headless QMP `send-key` typing.
  629. run-e1000: floppy.img root.img
  630. qemu-system-i386 -M pc -device pci-ohci -device usb-mouse -device usb-kbd \
  631. -device e1000,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 \
  632. -drive file=floppy.img,format=raw,if=floppy \
  633. -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
  634. qemu-system-x86_64 -M pc -device pci-ohci -device usb-mouse -device usb-kbd \
  635. -device e1000,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 \
  636. -drive file=floppy.img,format=raw,if=floppy \
  637. -drive file=root.img,format=raw,if=floppy
  638. # SATA/AHCI variant: an ICH9 AHCI controller with a test disk on port 0.
  639. # The disk image lives outside the repo and is created on first use; boot
  640. # stays on the floppy (order=a), the SATA disk shows up as sata0.
  641. SATA_IMG ?= /tmp/opencode/nebos-sata.img
  642. run-ahci: floppy.img root.img
  643. @test -f $(SATA_IMG) || truncate -s 512M $(SATA_IMG)
  644. qemu-system-i386 -M pc -boot order=a -device pci-ohci -device usb-mouse \
  645. -drive file=floppy.img,format=raw,if=floppy \
  646. -drive file=root.img,format=raw,if=floppy \
  647. -device ich9-ahci,id=sata \
  648. -drive file=$(SATA_IMG),format=raw,if=none,id=satadisk \
  649. -device ide-hd,drive=satadisk,bus=sata.0 2>/dev/null || \
  650. qemu-system-x86_64 -M pc -boot order=a -device pci-ohci -device usb-mouse \
  651. -drive file=floppy.img,format=raw,if=floppy \
  652. -drive file=root.img,format=raw,if=floppy \
  653. -device ich9-ahci,id=sata \
  654. -drive file=$(SATA_IMG),format=raw,if=none,id=satadisk \
  655. -device ide-hd,drive=satadisk,bus=sata.0
  656. # Headless USB HID regression tests (QEMU + python3): cursor movement via
  657. # injected USB mouse reports and keyboard input via injected key events,
  658. # including hotplug. See tools/*.py.
  659. test-usb: floppy.img root.img
  660. python3 tools/qmp_cursor_test.py
  661. python3 tools/qmp_cursor_test.py --uhci
  662. python3 tools/qmp_cursor_test.py --uhci72
  663. python3 tools/qmp_cursor_test.py --hotplug
  664. python3 tools/qmp_cursor_test.py --xhci
  665. python3 tools/qmp_keyboard_test.py
  666. python3 tools/qmp_keyboard_test.py --hotplug
  667. python3 tools/qmp_keyboard_test.py --xhci
  668. scp-final: final.bin
  669. scp final.bin root@192.168.0.136:
  670. ssh root@192.168.0.136 dd if=final.bin of=/dev/fd0 bs=512
  671. scp-root: root.img
  672. scp root.img root@192.168.0.136:
  673. ssh root@192.168.0.136 dd if=root.img of=/dev/fd0 bs=512
  674. SERIAL_DEV ?= /dev/ttyUSB0
  675. SERIAL_BAUD ?= 115200
  676. SZ ?= /usr/bin/lrzsz-sz
  677. run-serial:
  678. @echo "Starting serial monitor on $(SERIAL_DEV) at $(SERIAL_BAUD) baud..."
  679. @echo "Type 'serial test' or 'serial recv' in the nebos terminal."
  680. @echo "Press Ctrl-A Ctrl-X to exit picocom."
  681. picocom $(SERIAL_DEV) --baud $(SERIAL_BAUD)
  682. monitor:
  683. @echo "Watching $(SERIAL_DEV) @ $(SERIAL_BAUD) baud (Ctrl-A Ctrl-X to exit)."
  684. @echo "Each 'make dev' reload prints: Nebs OS v<version> booted"
  685. picocom $(SERIAL_DEV) --baud $(SERIAL_BAUD)
  686. send: $(PROGRAM).bin send_serial
  687. @echo "Sending $(PROGRAM).bin via serial..."
  688. ./send_serial $(SERIAL_DEV) $(SERIAL_BAUD) $(PROGRAM).bin
  689. send_serial: send_serial.c
  690. gcc -O2 -o $@ $<
  691. kexec_send: kexec_send.c
  692. gcc -O2 -o $@ $<
  693. netkexec_send: netkexec_send.c
  694. gcc -O2 -o $@ $<
  695. netmonitor: netmonitor.c
  696. gcc -O2 -o $@ $<
  697. bootstrap: bootstrap.bin
  698. dev: kernel.bin cmds_pack_gz.bin kexec_send
  699. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  700. if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
  701. echo "Sending kernel.bin + cmds_pack_gz.bin (entry 0x$$entry) via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."; \
  702. ./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin cmds_pack_gz.bin
  703. # Kernel-only fast path: the board keeps its running rootfs.
  704. kernel-dev: kernel.bin kexec_send
  705. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  706. if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
  707. echo "Sending kernel.bin only (entry 0x$$entry) via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."; \
  708. ./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
  709. # Push just the command pack (cmds.pack) to a running board: no kernel
  710. # reload, the /ramfs contents are replaced in place. The board must be idle
  711. # at a prompt (the WM loop auto-detects the transfer).
  712. cmds-dev: cmds.pack kexec_send
  713. @echo "Sending cmds.pack via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."
  714. ./kexec_send --payload $(SERIAL_DEV) $(SERIAL_BAUD) cmds.pack
  715. net-cmds: cmds.pack netkexec_send
  716. @echo "Sending cmds.pack to $(BOARD_IP):$(NETKEXEC_PORT)..."
  717. ./netkexec_send --payload $(BOARD_IP) $(NETKEXEC_PORT) cmds.pack
  718. # Development override: upload the real browser to the board's writable
  719. # /share; the bin/browser launcher prefers it over the pack copy. The board
  720. # must be running `websrv`; stop it before browsing HTTP, since the WebDAV
  721. # server owns the NIC.
  722. browser-dev: browser.elf
  723. @echo "Uploading browser.elf to http://$(BOARD_IP):8080/share/BROWSER..."
  724. curl -f -T browser.elf http://$(BOARD_IP):8080/share/BROWSER
  725. # gallery follows the same development-override model.
  726. gallery-dev: gallery.elf
  727. @echo "Uploading gallery.elf to http://$(BOARD_IP):8080/share/GALLERY..."
  728. curl -f -T gallery.elf http://$(BOARD_IP):8080/share/GALLERY
  729. # Show the board's netlog stream (kernel debug + terminal text) like `make
  730. # monitor` does over serial.
  731. netmon: netmonitor
  732. @echo "Listening for netlog on UDP $(NETLOG_PORT) (Ctrl-C to exit)."
  733. ./netmonitor $(NETLOG_PORT)
  734. # Push a fresh kernel over Ethernet (the board auto-detects it from its idle
  735. # loop; no serial and no USB stick involved).
  736. BOARD_IP ?= 192.168.178.240
  737. NETLOG_PORT ?= 19290
  738. NETKEXEC_PORT ?= 19288
  739. net-dev: kernel.bin cmds_pack_gz.bin netkexec_send
  740. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  741. if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
  742. echo "Sending kernel.bin + cmds_pack_gz.bin (entry 0x$$entry) to $(BOARD_IP):$(NETKEXEC_PORT)..."; \
  743. ./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin cmds_pack_gz.bin
  744. # Kernel-only fast path: the board keeps its running rootfs.
  745. kernel-net-dev: kernel.bin netkexec_send
  746. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  747. if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
  748. echo "Sending kernel.bin only (entry 0x$$entry) to $(BOARD_IP):$(NETKEXEC_PORT)..."; \
  749. ./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
  750. clean:
  751. rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h