Makefile 43 KB

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