Makefile 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. # NebOS top-level build.
  2. #
  3. # Source tree:
  4. # boot/ real-mode boot sector, MBR, VESA stage, UEFI loader (boot/efi)
  5. # kernel/ the 32-bit kernel (kernel.ld, drivers, services)
  6. # common/ freestanding code linked into both the kernel and userland
  7. # include/ ABI headers shared by boot, kernel, userland and host tools
  8. # user/apps/<n>/ one directory per ring-3 program (ucmd_<n>.c + its modules)
  9. # user/lib/<l>/ ring-3 libraries (libc, ngui, ufont, img, tls, uhttp, ...)
  10. # tools/ host-side tools (mkpack, mkrootfs, kexec_send, ...)
  11. # host/ Linux SDL3 browser harness
  12. #
  13. # Intermediate objects go to build/ (mirroring the source paths). The images
  14. # and the artifacts the dev workflow uses (kernel.elf, kernel.bin, floppy.img,
  15. # usb.img, usb-efi.img, BOOTX64.EFI, cmds.pack, browser.elf, ...) stay at the
  16. # top level, so every make target and command line works as before.
  17. CC = gcc
  18. AS = as
  19. LD = ld
  20. OBJCOPY = objcopy
  21. B = build
  22. # -march=i586: first-gen Pentium (P5); avoids CMOV etc. (P6+)
  23. CFLAGS = -ffreestanding -fno-pie -m32 -march=i586 -nostdlib -fno-stack-protector \
  24. -fno-asynchronous-unwind-tables -mno-sse -mno-sse2 -mno-mmx \
  25. -Wall -Wextra -O2 -c
  26. # Ring-3 objects are compiled with function sections so the browser link can
  27. # drop code it never reaches; the legacy rootfs pack is close to its cap.
  28. SECTION_CFLAGS = -ffunction-sections -fdata-sections
  29. # Header dependencies are generated by the compiler (build/**/*.d).
  30. DEPFLAGS = -MMD -MP
  31. ASFLAGS = --32
  32. # NOWM=1 builds a diagnostic kernel that boots to the console without the
  33. # window manager (make nowm).
  34. NOWM ?= 0
  35. ifeq ($(NOWM),1)
  36. CFLAGS += -DNOWM
  37. endif
  38. # Include paths. The kernel sees only its own headers plus the shared ABI and
  39. # common code; ring-3 code sees the user libraries. Quoted includes of a
  40. # file's own directory need no -I (e.g. an app's private headers).
  41. USER_LIB_DIRS = $(sort $(dir $(wildcard user/lib/*/)))
  42. USER_LIB_DIRS := $(filter-out user/lib/libc/,$(USER_LIB_DIRS))
  43. KERNEL_INC = -Iinclude -Icommon -Ikernel
  44. USER_INC = -Iinclude -Icommon -Iuser $(patsubst %/,-I%,$(USER_LIB_DIRS))
  45. LIBC_INC = -Iuser/lib/libc/include
  46. # QuickJS (third_party/quickjs, see its VENDOR file) is the browser's
  47. # JavaScript engine. -nostdinc keeps host headers from leaking into the
  48. # freestanding build; __nebos__ selects the local patches.
  49. QJS_DIR = third_party/quickjs
  50. QJS_CFLAGS = $(CFLAGS) -O1 $(SECTION_CFLAGS) -nostdinc $(LIBC_INC) \
  51. -I$(QJS_DIR) -fno-builtin -w -D__nebos__ -U__linux__ \
  52. -U__gnu_linux__ -U__unix__ -U__GLIBC__ -DNDEBUG \
  53. -DCONFIG_VERSION='"2026-06-04"'
  54. QJS_OBJS = $(addprefix $(B)/$(QJS_DIR)/,quickjs.o cutils.o dtoa.o \
  55. libregexp.o libunicode.o)
  56. # UEFI loader: a PE32+ x86-64 EFI application built with the mingw toolchain.
  57. # The kernel itself stays 32-bit; the loader leaves long mode before jumping.
  58. EFI_CC ?= x86_64-w64-mingw32-gcc
  59. EFI_AS ?= x86_64-w64-mingw32-as
  60. EFI_CFLAGS = -ffreestanding -fno-stack-protector -mno-red-zone -fno-builtin \
  61. -fno-asynchronous-unwind-tables -fno-unwind-tables -O2 \
  62. -Wall -Wextra -Iinclude -Ikernel -Iboot/efi -I$(B)/boot/efi
  63. EFI_LDFLAGS = -nostdlib -Wl,--subsystem,10 -Wl,-e,efi_main \
  64. -Wl,--image-base,0x0 -Wl,--dynamicbase \
  65. -Wl,--enable-reloc-section -Wl,--no-insert-timestamp
  66. .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-rndis run-e1000 run-ahci run-amd test-amd test-usb test-install run-serial floppy usb uefi run-uefi test-uefi flash dev net-dev net-install cmds-dev net-cmds browser-dev gallery-dev compat-serve netmon monitor test-crypto test-wpa test-ccmp test-images test-images-asan test-libc test-js test-font test-font-asan test-svg test-touchpad test-chat test-chat-e2e test-wifi-store test-cursors fonts appicons kernel-dev kernel-net-dev rootfs nowm
  67. all: floppy.img usb.img final.bin root.img image.bin
  68. # ---- Generic compile rules -------------------------------------------------
  69. #
  70. # Per-object variations are target-specific variables on the .o:
  71. # OPT optimisation override appended after CFLAGS' -O2 (-Os, -Oz)
  72. # XFLAGS extra flags (function sections, libc headers, -fwrapv, ...)
  73. $(B)/kernel/%.o: kernel/%.c
  74. @mkdir -p $(@D)
  75. $(CC) $(CFLAGS) $(OPT) $(XFLAGS) $(KERNEL_INC) $(DEPFLAGS) -o $@ $<
  76. $(B)/common/%.o: common/%.c
  77. @mkdir -p $(@D)
  78. $(CC) $(CFLAGS) $(SECTION_CFLAGS) -Icommon $(DEPFLAGS) -o $@ $<
  79. # Ring-3 C: by default the libc headers are visible (the old ucmd_%.o rule);
  80. # objects that were always built freestanding clear XFLAGS below.
  81. $(B)/user/%.o: XFLAGS = $(LIBC_INC)
  82. $(B)/user/%.o: user/%.c
  83. @mkdir -p $(@D)
  84. $(CC) $(CFLAGS) $(OPT) $(XFLAGS) $(USER_INC) $(DEPFLAGS) -o $@ $<
  85. $(B)/%.o: %.S
  86. @mkdir -p $(@D)
  87. $(AS) $(ASFLAGS) -I$(<D) -o $@ $<
  88. $(B)/$(QJS_DIR)/%.o: $(QJS_DIR)/%.c
  89. @mkdir -p $(@D)
  90. $(CC) $(QJS_CFLAGS) $(DEPFLAGS) -o $@ $<
  91. # ---- Kernel ----------------------------------------------------------------
  92. # Link order is part of the image layout: keep it stable. vesa_stage comes
  93. # from boot/ (it is the 0x7E00 entry), font_8x16/inflate from common/.
  94. KERNEL_LINK_ORDER = vesa_stage entry kernel timer switch sched process init mm \
  95. kmalloc paging elf tss syscall sys_wm syscall_stub screen io tty rtc \
  96. terminal wm reboot poweroff disk fat serial serial_ring_buf serial_loop \
  97. vga blit cpu thermal it87 tpec ec cros_ec power_src power vga_copy \
  98. gfxbench gpu backlight ati cirrus intel nvidia nv50 radeon mtrr \
  99. font_8x8 font_8x16 fbtext gfx display cursor mouse i2c touchpad \
  100. touchpad_gesture uhci ohci ehci xhci asix rndis iwl_trans iwl_mvm \
  101. iwl_dvm wifi_fw rtl8821au wifi ccmp usb usbmsc usbhid lsusb panic \
  102. bootcon blkdev sdhci ata ahci fatdetect fatformat format install vfs \
  103. sys_fs sys_info sys_mem sys_net idle kexec kexec_tramp dbg lspci peek \
  104. ne2000 r8168 e1000e netdev net netcore netkexec netlog dhcp dns ntp tcp \
  105. acpi ioapic lapic smp ap_tramp_embed inflate ramfs share payload
  106. kobj = $(if $(filter vesa_stage,$(1)),$(B)/boot/$(1).o,$(if $(filter \
  107. font_8x16 inflate,$(1)),$(B)/common/$(1).o,$(B)/kernel/$(1).o))
  108. KERNEL_OBJS = $(foreach n,$(KERNEL_LINK_ORDER),$(call kobj,$(n)))
  109. # Size-sensitive kernel objects build -Os. wm/sys_wm carry the settings
  110. # ABI; at -O2 their extra .text/.data pushed the .data end across a page
  111. # boundary and moved the 4 KB-aligned BSS by a whole page.
  112. KERNEL_OS = terminal disk fat nvidia nv50 uhci ohci ehci xhci asix rndis \
  113. iwl_trans iwl_mvm iwl_dvm wifi_fw rtl8821au wifi ccmp usb lsusb lspci \
  114. peek ne2000 r8168 blkdev fatdetect fatformat format install usbmsc dbg vfs \
  115. wm sys_wm
  116. $(addprefix $(B)/kernel/,$(addsuffix .o,$(KERNEL_OS))): OPT = -Os
  117. $(B)/kernel/e1000e.o: OPT = -Oz
  118. # The ACPI table/S5 parser and the poweroff path are cold boot/shutdown code
  119. # and the kernel is close to the low-memory wall; squeeze them hard.
  120. $(B)/kernel/acpi.o: OPT = -Oz
  121. $(B)/kernel/poweroff.o: OPT = -Oz
  122. $(B)/kernel/reboot.o: OPT = -Oz
  123. $(B)/kernel/terminal.o: OPT = -Oz
  124. # process.o is the largest -O2 object and not on a drawing path; -Oz keeps
  125. # the kernel under the 0x90000 wall now that S5/reset live in it.
  126. $(B)/kernel/process.o: OPT = -Oz
  127. # The AP trampoline is linked at 0x6000 and embedded as a binary blob; ld
  128. # names its symbols after the input path, so run it inside the build dir
  129. # (smp.c expects _binary_ap_tramp_bin_start).
  130. $(B)/kernel/ap_tramp.elf: $(B)/kernel/ap_tramp.o kernel/ap_tramp.ld
  131. $(LD) -T kernel/ap_tramp.ld -nostdlib -m elf_i386 -o $@ $<
  132. $(B)/kernel/ap_tramp.bin: $(B)/kernel/ap_tramp.elf
  133. $(OBJCOPY) -O binary $< $@
  134. $(B)/kernel/ap_tramp_embed.o: $(B)/kernel/ap_tramp.bin
  135. cd $(B)/kernel && $(LD) -m elf_i386 -r -b binary -o ap_tramp_embed.o ap_tramp.bin
  136. kernel.elf: kernel/kernel.ld $(KERNEL_OBJS)
  137. $(LD) -T kernel/kernel.ld -nostdlib -m elf_i386 -o $@ $(KERNEL_OBJS)
  138. kernel.bin: kernel.elf
  139. $(OBJCOPY) -O binary $< $@
  140. # ---- Boot sector and MBR ---------------------------------------------------
  141. $(B)/boot/boot.elf: $(B)/boot/boot.o boot/linker.ld
  142. $(LD) -T boot/linker.ld -nostdlib -m elf_i386 -o $@ $<
  143. $(B)/boot/mbr.elf: $(B)/boot/mbr.o boot/mbr.ld
  144. $(LD) -T boot/mbr.ld -nostdlib -m elf_i386 -o $@ $<
  145. mbr.bin: $(B)/boot/mbr.elf
  146. $(OBJCOPY) -O binary $< $@
  147. boot.bin: $(B)/boot/boot.elf
  148. $(OBJCOPY) -O binary --only-section=.boot $< $@.tmp
  149. dd if=$@.tmp of=$@ bs=1 count=510 2>/dev/null
  150. printf '\x55\xaa' >> $@
  151. rm -f $@.tmp
  152. # Byte offsets inside boot.bin (see boot.S). Keep decimal: dd's seek= does not
  153. # parse 0x… literals (it silently treats them as 0) and the error is hidden by
  154. # the 2>/dev/null below.
  155. # 62 = kernel_sectors field (0x3E)
  156. # 64 = kernel_lba field (0x40, word; LBA 64 fits in the low byte)
  157. # 66 = pack_lba field (0x42, dword; rootfs image LBA)
  158. KERNEL_SECTORS_OFFSET = 62
  159. BOOT_SECTOR_START_OFFSET = 64
  160. PACK_LBA_OFFSET = 66
  161. # USB rootfs partition (type 0x7F) and UEFI ESP (type 0xEF). Partition 1
  162. # has room for a 512 KB kernel; the rootfs starts after it at LBA 1088.
  163. # The header and the 242 KB gzip pack are still inside the first 1.44 MB.
  164. ROOTFS_LBA = 1088
  165. ROOTFS_SECTORS = 16384
  166. ESP_LBA = 18432
  167. # Patch a little-endian 16/32-bit value into a boot-sector copy. Usage:
  168. # $(call patch16,file,offset,value)
  169. # (shell snippets are inlined below because make functions cannot easily
  170. # expand `$$(...)` arithmetic; the helpers keep the rules readable.)
  171. patch32 = printf "$$(printf '\\x%02x\\x%02x\\x%02x\\x%02x' \
  172. $$(( ( ($(2)) >> 0) & 0xFF )) $$(( ( ($(2)) >> 8) & 0xFF )) \
  173. $$(( ( ($(2)) >> 16) & 0xFF )) $$(( ( ($(2)) >> 24) & 0xFF )))" | \
  174. dd of=$(1) bs=1 seek=$(3) conv=notrunc 2>/dev/null
  175. patch16 = printf "$$(printf '\\x%02x\\x%02x' \
  176. $$(( ( ($(2)) >> 0) & 0xFF )) $$(( ( ($(2)) >> 8) & 0xFF )))" | \
  177. dd of=$(1) bs=1 seek=$(3) conv=notrunc 2>/dev/null
  178. floppy.img: boot.bin kernel.bin rootfs.bin
  179. SECTORS=$$(( ($$(stat -c%s kernel.bin) + 511) / 512 )); \
  180. cp boot.bin $@.boot.tmp; \
  181. $(call patch16,$@.boot.tmp,$$SECTORS,$(KERNEL_SECTORS_OFFSET)); \
  182. $(call patch32,$@.boot.tmp,1 + SECTORS,$(PACK_LBA_OFFSET)); \
  183. cat $@.boot.tmp kernel.bin > $@; \
  184. PAD=$$(( SECTORS * 512 - $$(stat -c%s kernel.bin) )); \
  185. dd if=/dev/zero bs=1 count=$$PAD 2>/dev/null >> $@; \
  186. cat rootfs.bin >> $@; \
  187. SIZE=$$(stat -c%s $@); \
  188. if [ $$SIZE -gt 1474560 ]; then \
  189. echo "error: floppy.img is $$SIZE bytes; max is 1474560"; \
  190. rm -f $@; exit 1; \
  191. fi; \
  192. truncate -s 1474560 $@; \
  193. rm -f $@.boot.tmp
  194. usb.img: mbr.bin boot.bin kernel.bin cmds.img
  195. SECTORS=$$(( ($$(stat -c%s kernel.bin) + 511) / 512 )); \
  196. cp boot.bin $@.boot.tmp; \
  197. $(call patch16,$@.boot.tmp,$$SECTORS,$(KERNEL_SECTORS_OFFSET)); \
  198. printf '\x40' | dd of=$@.boot.tmp bs=1 seek=$(BOOT_SECTOR_START_OFFSET) conv=notrunc 2>/dev/null; \
  199. $(call patch32,$@.boot.tmp,$(ROOTFS_LBA),$(PACK_LBA_OFFSET)); \
  200. cat mbr.bin > $@; \
  201. dd if=/dev/zero bs=512 count=62 2>/dev/null >> $@; \
  202. cat $@.boot.tmp kernel.bin >> $@; \
  203. truncate -s 9M $@; \
  204. dd if=cmds.img of=$@ bs=512 seek=$(ROOTFS_LBA) conv=notrunc 2>/dev/null; \
  205. printf '\x00\x11\x12\x00\x7f\x16\x15\x01\x40\x04\x00\x00\x00\x40\x00\x00' | \
  206. dd of=$@ bs=1 seek=462 conv=notrunc 2>/dev/null; \
  207. rm -f $@.boot.tmp
  208. image.bin: floppy.img
  209. cp floppy.img image.bin
  210. final.bin: boot.bin kernel.bin rootfs.bin
  211. SECTORS=$$(( ($$(stat -c%s kernel.bin) + 511) / 512 )); \
  212. cp boot.bin $@.boot.tmp; \
  213. $(call patch16,$@.boot.tmp,$$SECTORS,$(KERNEL_SECTORS_OFFSET)); \
  214. $(call patch32,$@.boot.tmp,1 + SECTORS,$(PACK_LBA_OFFSET)); \
  215. cat $@.boot.tmp kernel.bin > $@; \
  216. PAD=$$(( SECTORS * 512 - $$(stat -c%s kernel.bin) )); \
  217. dd if=/dev/zero bs=1 count=$$PAD 2>/dev/null >> $@; \
  218. cat rootfs.bin >> $@; \
  219. rm -f $@.boot.tmp
  220. # ---- UEFI loader and dual-boot USB image -------------------------------
  221. #
  222. # The legacy image (usb.img) is left byte-for-byte alone. usb-efi.img copies
  223. # it, adds a third MBR partition (type 0xEF, starting at the old 64 MB end)
  224. # and writes a small FAT16 ESP there. BIOS booting still uses the active
  225. # partition 1 at LBA 63; UEFI finds BOOTX64.EFI in the ESP.
  226. $(B)/boot/efi/efi_entry.h: kernel.elf
  227. @mkdir -p $(@D)
  228. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  229. if [ -z "$$entry" ]; then echo "efi: kernel_entry not found" >&2; exit 1; fi; \
  230. printf '#define KERNEL_ENTRY 0x%s\n' "$$entry" > $@
  231. $(B)/boot/efi/main.o: boot/efi/main.c $(B)/boot/efi/efi_entry.h
  232. @mkdir -p $(@D)
  233. $(EFI_CC) $(EFI_CFLAGS) $(DEPFLAGS) -c -o $@ $<
  234. # payload.S embeds kernel.bin and cmds_pack_gz.bin (top level) and tramp.bin.
  235. $(B)/boot/efi/payload.o: boot/efi/payload.S kernel.bin cmds_pack_gz.bin $(B)/boot/efi/tramp.bin
  236. $(EFI_AS) --64 -I$(B)/boot/efi -o $@ $<
  237. $(B)/boot/efi/tramp.o: boot/efi/tramp.S
  238. @mkdir -p $(@D)
  239. $(AS) --64 -o $@ $<
  240. $(B)/boot/efi/tramp.elf: $(B)/boot/efi/tramp.o boot/efi/tramp.ld
  241. $(LD) -T boot/efi/tramp.ld -nostdlib -m elf_x86_64 -o $@ $<
  242. $(B)/boot/efi/tramp.bin: $(B)/boot/efi/tramp.elf
  243. $(OBJCOPY) -O binary $< $@
  244. BOOTX64.EFI: $(B)/boot/efi/main.o $(B)/boot/efi/payload.o
  245. $(EFI_CC) $(EFI_LDFLAGS) -o $@ $^
  246. ESP_SECTORS = 16384
  247. esp.img: BOOTX64.EFI
  248. truncate -s $$(( $(ESP_SECTORS) * 512 )) $@
  249. mkfs.fat -F 16 -s 2 -n NEBOSESP $@
  250. mmd -i $@ ::/EFI
  251. mmd -i $@ ::/EFI/BOOT
  252. mcopy -i $@ BOOTX64.EFI ::/EFI/BOOT/BOOTX64.EFI
  253. usb-efi.img: usb.img esp.img
  254. cp usb.img $@
  255. # The legacy partition is a raw kernel blob with a deliberately empty
  256. # BPB (boot.S), so neither UEFI firmware nor VFS mistakes it for FAT.
  257. # Partition 3: ESP at LBA 18432, type 0xEF, 8 MB (partition 2 is the
  258. # rootfs at LBA 1088 written by usb.img).
  259. printf '\x00\xfe\xff\xff\xef\xfe\xff\xff\x00\x48\x00\x00\x00\x40\x00\x00' | \
  260. dd of=$@ bs=1 seek=478 conv=notrunc 2>/dev/null
  261. dd if=esp.img of=$@ bs=512 seek=$(ESP_LBA) conv=notrunc 2>/dev/null
  262. truncate -s $$(( ($(ESP_LBA) + $(ESP_SECTORS)) * 512 )) $@
  263. uefi: usb-efi.img
  264. # ---- Ring-3 libraries ------------------------------------------------------
  265. U = $(B)/user
  266. UL = $(B)/user/lib
  267. # libc: a static archive linked only into commands that use it. The archive
  268. # means apps that never reference malloc/string keep paying nothing, and
  269. # --gc-sections drops unused functions from the members that are pulled.
  270. LIBC_OBJS = $(addprefix $(UL)/libc/,malloc.o string.o strdup.o stdlib.o \
  271. ucmd_mem.o setjmp.o errno.o div64.o dtoa.o stdio.o \
  272. stdio_file.o math.o time.o)
  273. LIBC_A = $(UL)/libc/libc.a
  274. $(filter-out %/setjmp.o,$(LIBC_OBJS)): OPT = -Os
  275. $(filter-out %/setjmp.o,$(LIBC_OBJS)): XFLAGS = $(SECTION_CFLAGS) -fno-builtin $(LIBC_INC)
  276. $(LIBC_A): $(LIBC_OBJS)
  277. rm -f $@
  278. ar rcs $@ $^
  279. # 8x16 bitmap font and DEFLATE: shared with the kernel (common/).
  280. FONT16 = $(B)/common/font_8x16.o
  281. INFLATE = $(B)/common/inflate.o
  282. # Fixed-point TrueType text engine, linked only into GUI commands.
  283. UFONT_OBJS = $(addprefix $(UL)/ufont/,ufont.o ufont_ttf.o ufont_draw.o ufont_file.o)
  284. $(UFONT_OBJS): OPT = -Os
  285. $(UFONT_OBJS): XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
  286. $(UL)/ufont/ufont_ttf.o: OPT = -O2
  287. # WOFF 1.0 unwrapping needs inflate, so only the browser links it.
  288. UFONT_WOFF = $(UL)/ufont/ufont_woff.o
  289. $(UFONT_WOFF): OPT = -Os
  290. $(UFONT_WOFF): XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
  291. # ngui: the userland immediate-mode GUI toolkit. -Os: every GUI app links
  292. # the whole toolkit, and the rootfs pack has a cap.
  293. NGUI = $(UL)/ngui/ngui.o
  294. $(NGUI): OPT = -Os
  295. $(NGUI): XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
  296. NGUI_APP = $(NGUI) $(FONT16) $(UFONT_OBJS)
  297. # Window-manager/terminal drawing helpers (wm and term).
  298. UWM = $(UL)/uwm/uwm_lib.o
  299. $(UWM): OPT = -Os
  300. $(UWM): XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
  301. # Image library: pure freestanding decoders + RGB565 compositing.
  302. IMGLIB_OBJS = $(addprefix $(UL)/img/,img.o img_mem.o img_blit.o img_png.o \
  303. img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o \
  304. img_pam.o img_gif_anim.o img_file.o)
  305. IMG_DECODE_OBJS = $(addprefix $(UL)/img/,img.o img_mem.o img_blit.o img_png.o \
  306. img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o \
  307. img_pam.o)
  308. $(IMGLIB_OBJS): XFLAGS = $(SECTION_CFLAGS)
  309. # The integer IDCT deliberately relies on 32-bit wrap-around.
  310. $(UL)/img/img_jpeg.o: XFLAGS = $(SECTION_CFLAGS) -fwrapv
  311. # TLS 1.3/1.2 client library (curl, chat and the browser; see tls.h). The
  312. # crypto files are pure freestanding C, so tls_test exercises them on the
  313. # host (make test-crypto). TLS 1.2 adds AES-128-GCM (on wpa_crypto.c's AES)
  314. # and P-256 ECDH.
  315. TLSLIB_OBJS = $(addprefix $(UL)/tls/,tls_sha256.o tls_chacha20.o tls_x25519.o \
  316. tls_gcm.o tls_p256.o wpa_crypto.o tls_rng.o tls.o)
  317. $(TLSLIB_OBJS): XFLAGS = $(SECTION_CFLAGS)
  318. $(UL)/tls/wpa_crypto.o: OPT = -O2
  319. # Shared HTTP/TLS client (curl, the browser and chat), its cookie jar and the
  320. # JSON parser. The jar and the parser have host tests (make test-chat).
  321. UHTTP_OBJS = $(UL)/uhttp/uhttp.o $(UL)/uhttp/uhttp_cookie.o
  322. UJSON = $(UL)/ujson/ujson.o
  323. $(UHTTP_OBJS) $(UJSON): XFLAGS = $(SECTION_CFLAGS)
  324. $(UL)/uhttp/uhttp_cookie.o $(UJSON): OPT = -Os
  325. # WiFi saved-network list (wifi, netmenu) and the WPA2-PSK supplicant.
  326. WIFI_STORE = $(UL)/wifi/wifi_store.o
  327. WPA_OBJS = $(UL)/wifi/wpa.o $(UL)/tls/wpa_crypto.o
  328. $(UL)/wifi/wpa.o: OPT = -Os
  329. $(UL)/wifi/wpa.o: XFLAGS = $(SECTION_CFLAGS)
  330. # ---- Ring-3 programs -------------------------------------------------------
  331. #
  332. # Every program lives in user/apps/<name>/ with its entry file ucmd_<name>.c
  333. # and is packed as bin/<name>. New command: create user/apps/<name>/
  334. # ucmd_<name>.c and add <name> to UCMDS (the order is the pack order). A
  335. # single-file command needs nothing else; one that links libraries gets an
  336. # explicit .elf rule below.
  337. UCMDS = cpuinfo ls cat save rm mkdir rmdir \
  338. meminfo mouseinfo storage top process bootscan \
  339. clock colors edit wm term spin \
  340. netinfo netip netdns host dhcp \
  341. netsend netrecv ping pong netlog \
  342. netconsole netregs netloop netreset net100 netmac curl \
  343. files view sensors powerinfo mines solitaire \
  344. settime date gallery websrv browser chat \
  345. wifi netmenu settings disks membench init service
  346. app = $(U)/apps/$(1)/ucmd_$(1)
  347. UCMD_ELFS = $(foreach a,$(UCMDS),$(call app,$(a)).elf)
  348. .PRECIOUS: $(UCMD_ELFS)
  349. UCMD_LD = user/ucmd.ld
  350. # Link a ring-3 ELF at USER_BASE, dropping unreferenced sections.
  351. ULINK = $(LD) -T $(UCMD_LD) -z max-page-size=0x100 --gc-sections -nostdlib \
  352. -m elf_i386 -o $@ $(filter-out $(UCMD_LD),$^) && \
  353. $(OBJCOPY) --strip-all $@ $@
  354. # Single-file commands: no libc, no section GC.
  355. $(U)/apps/%.elf: $(U)/apps/%.o $(UCMD_LD)
  356. $(LD) -T $(UCMD_LD) -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $<
  357. $(OBJCOPY) --strip-all $@ $@
  358. # Objects that were always compiled without the libc headers.
  359. $(call app,edit).o $(call app,wm).o $(call app,files).o $(call app,mines).o \
  360. $(call app,settime).o $(call app,solitaire).o $(U)/apps/gallery/gallery.o \
  361. $(U)/apps/hello/hello.o: XFLAGS =
  362. $(call app,settime).o $(call app,solitaire).o: OPT = -Os
  363. # Both link with --gc-sections; the section-per-function build lets the pack
  364. # drop their unreachable helpers (the rootfs cap is tight).
  365. $(call app,wm).o: XFLAGS = $(SECTION_CFLAGS)
  366. $(call app,view).o: XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
  367. $(call app,wm).o $(call app,view).o: OPT = -Os
  368. # membench times memcpy/memset over a libc heap buffer.
  369. $(call app,membench).elf: $(call app,membench).o $(UCMD_LD) $(LIBC_A)
  370. $(ULINK)
  371. # websrv buffers its whole PUT/GET body on the libc heap.
  372. $(call app,websrv).elf: $(call app,websrv).o $(UCMD_LD) $(LIBC_A)
  373. $(ULINK)
  374. # edit draws text itself, so it links the 8x16 font into its ELF.
  375. $(call app,edit).elf: $(call app,edit).o $(FONT16) $(UCMD_LD)
  376. $(LD) -T $(UCMD_LD) -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
  377. $(OBJCOPY) --strip-all $@ $@
  378. # The ring-3 WM draws its own chrome; term renders its own cells. term also
  379. # reads the kernel's key codes (io.h) and VFS_PATH_MAX (vfs.h).
  380. $(call app,term).o: XFLAGS = $(LIBC_INC) -Ikernel
  381. $(call app,wm).elf: $(call app,wm).o $(UWM) $(FONT16) $(UFONT_OBJS) $(UCMD_LD) $(LIBC_A)
  382. $(ULINK)
  383. $(call app,term).elf: $(call app,term).o $(UWM) $(FONT16) $(UFONT_OBJS) $(UCMD_LD) $(LIBC_A)
  384. $(ULINK)
  385. # ngui canvas apps.
  386. $(call app,files).elf: $(call app,files).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  387. $(ULINK)
  388. # Task manager: process table with CPU, memory and network history graphs.
  389. $(call app,process).elf: $(call app,process).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  390. $(ULINK)
  391. # Boot scan window: live tail of the bootscan kernel thread's serial output.
  392. $(call app,bootscan).elf: $(call app,bootscan).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  393. $(ULINK)
  394. $(call app,bootscan).o: OPT = -Os
  395. $(call app,mines).elf: $(call app,mines).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  396. $(ULINK)
  397. $(call app,settime).elf: $(call app,settime).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  398. $(ULINK)
  399. $(call app,solitaire).elf: $(call app,solitaire).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  400. $(ULINK)
  401. $(call app,view).elf: $(call app,view).o $(IMGLIB_OBJS) $(INFLATE) $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  402. $(ULINK)
  403. $(call app,curl).elf: $(call app,curl).o $(UHTTP_OBJS) $(IMG_DECODE_OBJS) $(INFLATE) $(TLSLIB_OBJS) $(UCMD_LD) $(LIBC_A)
  404. $(ULINK)
  405. $(call app,wifi).elf: $(call app,wifi).o $(WIFI_STORE) $(WPA_OBJS) $(UL)/tls/tls_rng.o $(UL)/tls/tls_sha256.o $(UCMD_LD) $(LIBC_A)
  406. $(ULINK)
  407. # Control panel: battery/clock summary, brightness and the Settings pills.
  408. $(call app,netmenu).elf: $(call app,netmenu).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  409. $(ULINK)
  410. # Settings: sidebar sections, network/WiFi management, clock, power,
  411. # appearance (wallpaper picker) and window-manager preferences.
  412. $(call app,settings).o: OPT = -Os
  413. $(call app,settings).o: XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
  414. $(call app,settings).elf: $(call app,settings).o $(WIFI_STORE) $(IMG_DECODE_OBJS) $(UL)/img/img_file.o $(INFLATE) $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  415. $(ULINK)
  416. # Storage manager: devices, partitions, mount policies, format/install.
  417. $(call app,disks).elf: $(call app,disks).o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  418. $(ULINK)
  419. # Init manager and its CLI/GUI client. The shared service table/state/control
  420. # code is linked into both binaries.
  421. SERVICE_COMMON = $(U)/apps/service/service_proto.o \
  422. $(U)/apps/service/service_defs.o
  423. $(call app,init).elf: $(call app,init).o $(SERVICE_COMMON) $(UCMD_LD) $(LIBC_A)
  424. $(ULINK)
  425. $(call app,service).elf: $(call app,service).o $(SERVICE_COMMON) $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  426. $(ULINK)
  427. $(call app,init).o $(call app,service).o $(SERVICE_COMMON): OPT = -Os
  428. $(call app,init).o $(call app,service).o $(SERVICE_COMMON): XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
  429. # chat: native Cloudron Chat client (ngui + uhttp/TLS + cookie jar + JSON +
  430. # the native OIDC login driver).
  431. CHAT_LOGIN = $(U)/apps/chat/chat_login.o
  432. $(CHAT_LOGIN): OPT = -Os
  433. $(CHAT_LOGIN): XFLAGS = $(SECTION_CFLAGS)
  434. $(call app,chat).o: OPT = -Os
  435. $(call app,chat).o: XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
  436. $(call app,chat).elf: $(call app,chat).o $(CHAT_LOGIN) $(UJSON) $(UHTTP_OBJS) $(NGUI_APP) $(IMG_DECODE_OBJS) $(INFLATE) $(TLSLIB_OBJS) $(UCMD_LD) $(LIBC_A)
  437. $(ULINK)
  438. # gallery: a live showcase of every ngui widget and primitive. The real ELF
  439. # ships in the rootfs pack as bin/gallery.app; ucmd_gallery.c (in UCMDS) is
  440. # the tiny PATH launcher (which also accepts /share/GALLERY).
  441. gallery.elf: $(U)/apps/gallery/gallery.o $(NGUI_APP) $(UCMD_LD) $(LIBC_A)
  442. $(ULINK)
  443. # browser: the real app ships as bin/browser.app in the full pack (and
  444. # BROWSER on root.img, which floppy boots use)
  445. # because it links ngui, the image library, TLS, uhttp and QuickJS;
  446. # ucmd_browser.c is the tiny PATH launcher. Compiled with function sections
  447. # and linked with --gc-sections: unreachable image, TLS and UI paths are
  448. # dropped.
  449. BR = $(U)/apps/browser
  450. BROWSER_OBJS = $(BR)/browser.o $(BR)/browser_html.o $(BR)/svg_raster.o \
  451. $(BR)/js_browser.o $(BR)/js_mu.o
  452. $(BROWSER_OBJS): OPT = -Os
  453. $(BROWSER_OBJS): XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
  454. $(BR)/browser_html.o: XFLAGS = $(SECTION_CFLAGS)
  455. $(BR)/js_mu.o: XFLAGS = $(SECTION_CFLAGS) -Wno-unused-parameter $(LIBC_INC) -I$(QJS_DIR)
  456. browser.elf: $(BR)/browser.o $(BR)/browser_html.o $(BR)/svg_raster.o $(BR)/js_browser.o $(BR)/js_prelude.o $(UHTTP_OBJS) $(NGUI_APP) $(UFONT_WOFF) $(IMGLIB_OBJS) $(INFLATE) $(TLSLIB_OBJS) $(BR)/js_mu.o $(QJS_OBJS) $(UCMD_LD) $(LIBC_A)
  457. $(ULINK)
  458. # hello: the first ring-3 test program (its own linker script).
  459. $(U)/apps/hello/hello.elf: $(U)/apps/hello/hello.o user/apps/hello/hello.ld
  460. $(LD) -T user/apps/hello/hello.ld -nostdlib -m elf_i386 -o $@ $<
  461. $(OBJCOPY) --strip-all $@ $@
  462. # ---- Linux host browser harness (browser-host / host-curl) -------------------
  463. #
  464. # The browser talks to the same small SYS_* ABI as on NebOS; host/syscall.c
  465. # implements it with Linux files, BSD sockets, fork/exec and an SDL3 window.
  466. # The target images and browser.elf are not involved. Build 32-bit so the
  467. # pointer-through-int syscall ABI and the ring-3 allocator behave like the
  468. # board. SDL3 is only a development dependency for the window backend.
  469. HOST_CC ?= gcc
  470. HOST_BUILD = host/build
  471. HOST_CFLAGS = -m32 -O1 -g -Wall -Wextra -Wno-unused-parameter \
  472. -Wno-sign-compare -Wno-unused-function -Wno-format-truncation \
  473. -fno-strict-aliasing -fno-omit-frame-pointer $(USER_INC) \
  474. -Ihost/include -Ithird_party/quickjs -I/usr/include/SDL3 \
  475. -DUCMD_HOST -DJS_MU_HOST -D__nebos__ -D_GNU_SOURCE \
  476. -DCONFIG_VERSION='"2026-06-04"'
  477. # The ring-3 allocator is reused on the host: rename its entry points so
  478. # glibc and SDL keep their own malloc. libc/malloc.c and every app object
  479. # must use the same set.
  480. HOST_APP_MALLOC_DEFS = -Dmalloc=libc_malloc -Dfree=libc_free \
  481. -Dcalloc=libc_calloc -Drealloc=libc_realloc \
  482. -Dmalloc_usable_size=libc_malloc_usable_size \
  483. -Dheap_reset=libc_heap_reset
  484. HOST_APP_CFLAGS = $(HOST_CFLAGS) $(HOST_APP_MALLOC_DEFS)
  485. HOST_LDFLAGS = -m32 -no-pie -L/usr/lib32
  486. HOST_LDLIBS = -lSDL3 -lm -lpthread -ldl
  487. HOST_IMGLIB_SRCS = $(addprefix user/lib/img/,img.c img_mem.c img_blit.c \
  488. img_png.c img_bmp.c img_gif.c img_jpeg.c img_tga.c img_pnm.c \
  489. img_ico.c img_pam.c img_gif_anim.c img_file.c)
  490. HOST_TLS_SRCS = $(addprefix user/lib/tls/,tls_sha256.c tls_chacha20.c \
  491. tls_x25519.c tls_gcm.c tls_p256.c wpa_crypto.c tls_rng.c tls.c)
  492. HOST_QUERY_SRCS = $(addprefix $(QJS_DIR)/,quickjs.c cutils.c dtoa.c \
  493. libregexp.c libunicode.c)
  494. HOST_UHTTP_SRCS = user/lib/uhttp/uhttp.c user/lib/uhttp/uhttp_cookie.c
  495. HOST_BROWSER_APP_SRCS = $(addprefix user/apps/browser/,browser.c \
  496. browser_html.c svg_raster.c js_browser.c) \
  497. $(HOST_UHTTP_SRCS) user/lib/ngui/ngui.c \
  498. common/font_8x16.c \
  499. $(addprefix user/lib/ufont/,ufont.c ufont_ttf.c \
  500. ufont_draw.c ufont_file.c ufont_woff.c) \
  501. $(HOST_IMGLIB_SRCS) common/inflate.c $(HOST_TLS_SRCS) \
  502. user/apps/browser/js_mu.c user/lib/libc/malloc.c \
  503. $(HOST_QUERY_SRCS)
  504. HOST_CURL_APP_SRCS = user/apps/curl/ucmd_curl.c $(HOST_UHTTP_SRCS) \
  505. $(HOST_IMGLIB_SRCS) common/inflate.c $(HOST_TLS_SRCS) \
  506. user/lib/libc/malloc.c
  507. HOST_COMMON_SRCS = host/host.c host/syscall.c host/fs.c host/net.c \
  508. host/proc.c host/heap.c host/window.c \
  509. host/window_sdl3.c host/window_headless.c
  510. HOST_COMMON_OBJS = $(addprefix $(HOST_BUILD)/,$(HOST_COMMON_SRCS:.c=.o))
  511. HOST_BROWSER_APP_OBJS = $(addprefix $(HOST_BUILD)/,$(HOST_BROWSER_APP_SRCS:.c=.o))
  512. HOST_CURL_APP_OBJS = $(addprefix $(HOST_BUILD)/,$(HOST_CURL_APP_SRCS:.c=.o))
  513. HOST_BROWSER_OBJS = $(HOST_BUILD)/host/main.o $(HOST_COMMON_OBJS) \
  514. $(HOST_BROWSER_APP_OBJS) $(HOST_BUILD)/js_prelude.o
  515. HOST_CURL_OBJS = $(HOST_BUILD)/host/curl_main.o $(HOST_COMMON_OBJS) \
  516. $(HOST_CURL_APP_OBJS)
  517. $(HOST_BUILD)/%.o: %.c
  518. @mkdir -p $(dir $@)
  519. $(HOST_CC) $(HOST_APP_CFLAGS) $(HOST_EXTRA) $(DEPFLAGS) -c -o $@ $<
  520. # Host support files use glibc malloc directly, never the renamed arena.
  521. $(HOST_BUILD)/host/%.o: host/%.c
  522. @mkdir -p $(dir $@)
  523. $(HOST_CC) $(HOST_CFLAGS) $(DEPFLAGS) -c -o $@ $<
  524. $(HOST_BUILD)/js_prelude.o: user/apps/browser/js_prelude.S $(BR)/js_prelude.min.js
  525. @mkdir -p $(dir $@)
  526. $(HOST_CC) -m32 -Wa,-I$(BR) -c -o $@ $<
  527. $(HOST_BUILD)/user/apps/browser/browser.o: HOST_EXTRA = -D_start=browser_start
  528. $(HOST_BUILD)/user/apps/curl/ucmd_curl.o: HOST_EXTRA = -D_start=curl_start
  529. browser-host: host-curl $(HOST_BROWSER_OBJS) | icons/adwaita16.icn
  530. $(HOST_CC) $(HOST_LDFLAGS) -o $@ $(filter-out host-curl,$^) $(HOST_LDLIBS)
  531. host-curl: $(HOST_CURL_OBJS)
  532. $(HOST_CC) $(HOST_LDFLAGS) -o $@ $^ $(HOST_LDLIBS)
  533. # ---- Host tests (no kernel needed) -----------------------------------------
  534. UFONT_TEST_SRCS = $(addprefix user/lib/ufont/,ufont_test.c ufont.c ufont_ttf.c ufont_draw.c ufont_woff.c) common/inflate.c
  535. ufont_test: $(UFONT_TEST_SRCS) user/lib/ufont/ufont.h user/lib/ufont/ufont_internal.h fonts/emoji.ttf testdata/fonts/sans.woff
  536. gcc -O2 -Wall -Wextra -Iinclude -Icommon -o $@ $(UFONT_TEST_SRCS)
  537. test-font: ufont_test
  538. ./ufont_test
  539. test-font-asan: $(UFONT_TEST_SRCS) user/lib/ufont/ufont.h user/lib/ufont/ufont_internal.h fonts/emoji.ttf testdata/fonts/sans.woff
  540. gcc -O1 -g -fsanitize=address,undefined -Iinclude -Icommon -o ufont_test_asan $(UFONT_TEST_SRCS)
  541. ./ufont_test_asan
  542. fonts:
  543. tools/subset_fonts.sh
  544. SVG_SRCS = user/apps/browser/svg_test.c user/apps/browser/svg_raster.c
  545. svg_test: $(SVG_SRCS) user/apps/browser/svg_raster.h
  546. gcc -O1 -g -Wall -Wextra -fsanitize=address,undefined -o $@ $(SVG_SRCS) -lm
  547. test-svg: svg_test
  548. ./svg_test
  549. touchpad_gesture_test: kernel/touchpad_gesture_test.c kernel/touchpad_gesture.c kernel/touchpad_gesture.h
  550. gcc -O2 -Wall -Wextra -o $@ kernel/touchpad_gesture_test.c kernel/touchpad_gesture.c
  551. test-touchpad: touchpad_gesture_test
  552. ./touchpad_gesture_test
  553. # Host tests for the chat client's pure modules: JSON, cookie jar and the
  554. # login page/reply/config parsers.
  555. CHAT_TEST_CFLAGS = -O1 -g -Wall -Wextra -fsanitize=address,undefined $(USER_INC)
  556. ujson_test: user/lib/ujson/ujson_test.c user/lib/ujson/ujson.c user/lib/ujson/ujson.h
  557. gcc $(CHAT_TEST_CFLAGS) -o $@ user/lib/ujson/ujson_test.c user/lib/ujson/ujson.c
  558. uhttp_cookie_test: user/lib/uhttp/uhttp_cookie_test.c user/lib/uhttp/uhttp_cookie.c user/lib/uhttp/uhttp_cookie.h
  559. gcc $(CHAT_TEST_CFLAGS) -o $@ user/lib/uhttp/uhttp_cookie_test.c user/lib/uhttp/uhttp_cookie.c
  560. chat_login_test: user/apps/chat/chat_login_test.c user/apps/chat/chat_login.c user/apps/chat/chat_login.h user/lib/ujson/ujson.c
  561. gcc $(CHAT_TEST_CFLAGS) -DCHAT_LOGIN_PURE -o $@ user/apps/chat/chat_login_test.c user/apps/chat/chat_login.c user/lib/ujson/ujson.c
  562. test-chat: ujson_test uhttp_cookie_test chat_login_test
  563. ./ujson_test
  564. ./uhttp_cookie_test
  565. ./chat_login_test
  566. # QEMU end-to-end chat test against tools/mock_chat.py (ports 18080/18081 on
  567. # the host, 10.0.2.2 in the guest). CHAT_E2E=--totp|--consent|--passkey|
  568. # --wrong selects a login variant; screenshots land in chat-test-out/.
  569. CHAT_E2E ?=
  570. test-chat-e2e: floppy.img root.img
  571. python3 tools/qmp_chat_test.py $(CHAT_E2E)
  572. TLS_TEST_SRCS = $(addprefix user/lib/tls/,tls_test.c tls_sha256.c tls_chacha20.c \
  573. tls_x25519.c tls_gcm.c tls_p256.c wpa_crypto.c)
  574. tls_test: $(TLS_TEST_SRCS) $(wildcard user/lib/tls/*.h)
  575. gcc -O2 -o $@ $(TLS_TEST_SRCS)
  576. test-crypto: tls_test
  577. ./tls_test
  578. wpa_test: user/lib/wifi/wpa_test.c user/lib/wifi/wpa.c user/lib/tls/wpa_crypto.c user/lib/wifi/wpa.h user/lib/tls/wpa_crypto.h
  579. gcc -O2 -Wall -Wextra -Iuser/lib/tls -o $@ user/lib/wifi/wpa_test.c user/lib/wifi/wpa.c user/lib/tls/wpa_crypto.c
  580. test-wpa: wpa_test
  581. ./wpa_test
  582. wifi_store_test: user/lib/wifi/wifi_store_test.c user/lib/wifi/wifi_store.c user/lib/wifi/wifi_store.h include/wifi_cfg.h include/syscall.h
  583. gcc -m32 -O2 -Wall -Wextra -Iinclude -Iuser -o $@ user/lib/wifi/wifi_store_test.c
  584. test-wifi-store: wifi_store_test
  585. ./wifi_store_test
  586. ccmp_test: kernel/ccmp_test.c kernel/ccmp.c kernel/ccmp.h
  587. gcc -O2 -Wall -Wextra -o $@ kernel/ccmp_test.c
  588. test-ccmp: ccmp_test
  589. ./ccmp_test
  590. # Host image-library tests. The corpus and expected hashes are committed;
  591. # tools/gen_img_tests.py regenerates them.
  592. IMGLIB_SRCS = $(addprefix user/lib/img/,img.c img_mem.c img_blit.c \
  593. img_png.c img_bmp.c img_gif.c img_gif_anim.c img_jpeg.c img_tga.c \
  594. img_pnm.c img_ico.c img_pam.c) common/inflate.c
  595. IMGTEST_SRCS = user/lib/img/img_test.c $(IMGLIB_SRCS)
  596. IMGTEST_DEPS = $(wildcard user/lib/img/*.h) common/inflate.h testdata/images/expected.h
  597. IMGTEST_INC = -I. -Icommon -Iinclude -Iuser
  598. img_test: $(IMGTEST_SRCS) $(IMGTEST_DEPS)
  599. gcc -O2 -fwrapv -Wall -Wextra $(IMGTEST_INC) -o $@ $(IMGTEST_SRCS)
  600. # mkappicons decodes the committed 96 px app icon PNGs with the same image
  601. # library and writes the NBAPP1 bundle (include/app_icon_abi.h).
  602. mkappicons: tools/mkappicons.c include/app_icon_abi.h $(IMGLIB_SRCS) $(IMGTEST_DEPS)
  603. gcc -O2 -fwrapv -Wall -Wextra $(IMGTEST_INC) -o $@ tools/mkappicons.c $(IMGLIB_SRCS)
  604. test-images: img_test
  605. ./img_test
  606. test-images-asan: $(IMGTEST_SRCS) $(IMGTEST_DEPS)
  607. # -fwrapv: the integer IDCT deliberately relies on 32-bit wrap-around.
  608. gcc -O1 -g -fwrapv -fsanitize=address,undefined $(IMGTEST_INC) -o img_test_asan $(IMGTEST_SRCS)
  609. ./img_test_asan
  610. # Host tests for the ring-3 libc. The malloc/free names are renamed so the
  611. # host libc (printf, rand, write) keeps working normally.
  612. HOST_MALLOC_DEFS = -Dmalloc=libc_malloc -Dfree=libc_free \
  613. -Dcalloc=libc_calloc -Drealloc=libc_realloc \
  614. -Dheap_reset=libc_heap_reset
  615. LT = $(B)/host-test/libc
  616. LSRC = user/lib/libc
  617. $(LT)/malloc_host.o: $(LSRC)/malloc.c
  618. @mkdir -p $(@D)
  619. gcc -O2 -Wall -Wextra $(LIBC_INC) -fno-builtin $(HOST_MALLOC_DEFS) $(DEPFLAGS) -c -o $@ $<
  620. $(LT)/test_host_mem.o: $(LSRC)/test_host_mem.c
  621. @mkdir -p $(@D)
  622. gcc -O2 -Wall -Wextra $(DEPFLAGS) -c -o $@ $<
  623. malloc_test: $(LSRC)/test_malloc.c $(LT)/malloc_host.o $(LT)/test_host_mem.o
  624. gcc -O2 -Wall -Wextra -o $@ $^
  625. $(LT)/stdio_host.o: $(LSRC)/stdio.c
  626. @mkdir -p $(@D)
  627. gcc -O2 -Wall -Wextra $(LIBC_INC) -fno-builtin $(DEPFLAGS) -c -o $@ $<
  628. $(LT)/dtoa_host.o: $(LSRC)/dtoa.c
  629. @mkdir -p $(@D)
  630. gcc -O2 -Wall -Wextra $(LIBC_INC) -fno-builtin $(DEPFLAGS) -c -o $@ $<
  631. $(LT)/math_host.o: $(LSRC)/math.c
  632. @mkdir -p $(@D)
  633. gcc -O2 -Wall -Wextra $(LIBC_INC) -fno-builtin $(DEPFLAGS) -c -o $@ $<
  634. $(LT)/div64_host.o: $(LSRC)/div64.c
  635. @mkdir -p $(@D)
  636. gcc -O2 -Wall -Wextra $(DEPFLAGS) -c -o $@ $<
  637. format_test: $(LSRC)/test_format.c $(LT)/stdio_host.o $(LT)/dtoa_host.o
  638. gcc -O2 -Wall -Wextra -Wno-format-truncation $(LIBC_INC) -o $@ $^
  639. math_test: $(LSRC)/test_math.c $(LT)/math_host.o
  640. gcc -O2 -Wall -Wextra -o $@ $^ -lm
  641. div64_test: $(LSRC)/test_div64.c $(LT)/div64_host.o
  642. gcc -O2 -Wall -Wextra -o $@ $^
  643. setjmp_test: $(LSRC)/test_setjmp.c $(UL)/libc/setjmp.o
  644. gcc -m32 -O2 -Wall -Wextra $(LIBC_INC) -o $@ $^
  645. test-libc: malloc_test format_test math_test div64_test setjmp_test
  646. ./malloc_test
  647. ./format_test
  648. ./math_test
  649. ./div64_test
  650. ./setjmp_test
  651. QJS_HOST_SRCS = $(addprefix $(QJS_DIR)/,quickjs.c cutils.c dtoa.c libregexp.c libunicode.c)
  652. JS_TEST_SRCS = user/apps/browser/js_test.c user/apps/browser/js_mu.c
  653. js_test: $(JS_TEST_SRCS) user/apps/browser/js_mu.h $(QJS_HOST_SRCS) $(wildcard $(QJS_DIR)/*.h)
  654. gcc -O1 -g -w -fsanitize=address,undefined -fno-sanitize=shift -Iinclude -Iuser -I$(QJS_DIR) -D__nebos__ -DJS_MU_HOST \
  655. -DCONFIG_VERSION='"2026-06-04"' -o $@ $(JS_TEST_SRCS) $(QJS_HOST_SRCS) -lm
  656. test-js: js_test
  657. ./js_test
  658. # ---- Host tools ------------------------------------------------------------
  659. mkpack: tools/mkpack.c
  660. gcc -O2 -o $@ $<
  661. mkrootfs: tools/mkrootfs.c include/rootfs.h
  662. gcc -O2 -Iinclude -o $@ $<
  663. mkrtwtbl: tools/mkrtwtbl.c include/rtl8821a_tbl.h third_party/rtw88/rtw8821a_table.inc
  664. gcc -O2 -Wall -Wextra -I. -Iinclude -o $@ $<
  665. kexec_send: tools/kexec_send.c
  666. gcc -O2 -o $@ $<
  667. netkexec_send: tools/netkexec_send.c
  668. gcc -O2 -o $@ $<
  669. netmonitor: tools/netmonitor.c
  670. gcc -O2 -o $@ $<
  671. mkicons: tools/mkicons.c include/icon_abi.h user/apps/browser/svg_raster.c user/apps/browser/svg_raster.h
  672. gcc -O2 -Wall -Wextra -Iinclude -o $@ tools/mkicons.c user/apps/browser/svg_raster.c -lm
  673. jsstrip: tools/jsstrip.c
  674. gcc -O2 -Wall -Wextra -o $@ $<
  675. # The browser prelude is embedded without its comments and indentation
  676. # (tools/jsstrip.c): ~3.5 KB of gzip in the rootfs pack.
  677. $(BR)/js_prelude.min.js: user/apps/browser/js_prelude.js jsstrip
  678. @mkdir -p $(@D)
  679. ./jsstrip $< $@
  680. $(BR)/js_prelude.o: user/apps/browser/js_prelude.S $(BR)/js_prelude.min.js
  681. @mkdir -p $(@D)
  682. $(AS) $(ASFLAGS) -I$(BR) -o $@ $<
  683. mkcursors: tools/mkcursors.c include/cursor_abi.h
  684. gcc -O2 -Wall -Wextra -Iinclude -o $@ $<
  685. cursor_test: tools/cursor_test.c include/cursor_abi.h
  686. gcc -O1 -g -Wall -Wextra -fsanitize=address,undefined -Iinclude -o $@ $<
  687. # ---- Command pack (the system rootfs) ---------------------------------------
  688. #
  689. # The ring-3 command ELFs plus hello and the browser/gallery apps, packed
  690. # into bin/<name>. See tools/mkpack.c, include/rootfs.h and kernel/ramfs.h.
  691. FONT_FILES = fonts/sans.ttf fonts/sans-b.ttf fonts/sans-i.ttf \
  692. fonts/sans-bi.ttf fonts/mono.ttf fonts/emoji.ttf
  693. # Mouse cursors: the Adwaita theme's 24 px images (assets/cursors/adwaita/,
  694. # LGPL-3 / CC-BY-SA-3.0) converted to one NBCUR1 bundle (include/cursor_abi.h)
  695. # that kernel/cursor.c loads from /ramfs/cursors/adwaita24.cur. Full pack
  696. # only (~18 KB gzip): the floppy lite pack has no room, and its kernel keeps
  697. # the built-in 16x16 arrow.
  698. CURSOR_THEME = assets/cursors/adwaita
  699. CURSOR_FILE = cursors/adwaita24.cur
  700. $(CURSOR_FILE): mkcursors $(filter-out %/README %/COPYING%,$(wildcard $(CURSOR_THEME)/*))
  701. @mkdir -p cursors
  702. ./mkcursors $(CURSOR_THEME) 24 $@
  703. # Defined after CURSOR_FILE: make expands a rule's prerequisites at once.
  704. test-cursors: cursor_test $(CURSOR_FILE)
  705. ./cursor_test $(CURSOR_FILE)
  706. # UI icons: the Adwaita symbolic SVGs (assets/icons/adwaita/, LGPL-3 /
  707. # CC-BY-SA-3.0) rasterized by mkicons (with the browser's svg_raster.c) into
  708. # one NBICO1 bundle of 16 px coverage masks (include/icon_abi.h) that ngui
  709. # loads from /ramfs/icons/adwaita16.icn and tints. ~3.3 KB (~0.8 KB gzip),
  710. # so the floppy lite pack carries it as well.
  711. ICON_THEME = assets/icons/adwaita
  712. ICON_FILE = icons/adwaita16.icn
  713. $(ICON_FILE): mkicons $(wildcard $(ICON_THEME)/*.svg)
  714. @mkdir -p icons
  715. ./mkicons $(ICON_THEME) 16 $@
  716. # Full-colour app icons: upstream GNOME app SVGs and their committed 96 px
  717. # PNG renders (assets/icons/apps/) converted by mkappicons into one NBAPP1
  718. # bundle (include/app_icon_abi.h). The WM loads it from
  719. # /ramfs/icons/apps96.icn; both the full and the floppy lite pack carry it.
  720. # ~415 KB inflated, ~30 KB in the gzip pack. render_appicons.sh is the
  721. # maintainer-only SVG -> PNG step and is not part of the build.
  722. APPICON_THEME = assets/icons/apps
  723. APPICON_FILE = icons/apps96.icn
  724. appicons: $(APPICON_FILE)
  725. $(APPICON_FILE): mkappicons $(filter-out %/README %/COPYING%,$(wildcard $(APPICON_THEME)/*.png)) include/app_icon_abi.h
  726. @mkdir -p icons
  727. ./mkappicons $(APPICON_THEME) 96 $@
  728. HELLO_ELF = $(U)/apps/hello/hello.elf
  729. PACK_FILES = $(foreach a,$(UCMDS),bin/$(a)=$(call app,$(a)).elf) \
  730. bin/hello=$(HELLO_ELF) \
  731. bin/gallery.app=gallery.elf \
  732. $(foreach f,$(FONT_FILES),$(f)=$(f)) \
  733. $(ICON_FILE)=$(ICON_FILE) \
  734. $(APPICON_FILE)=$(APPICON_FILE)
  735. PACK_DEPS = $(UCMD_ELFS) $(HELLO_ELF) browser.elf gallery.elf $(FONT_FILES) \
  736. $(ICON_FILE) $(APPICON_FILE) mkpack
  737. PACK_CURSOR_FILES = $(CURSOR_FILE)=$(CURSOR_FILE)
  738. # The browser (~500 KB gzip) is in the full pack only: a floppy boot runs it
  739. # from BROWSER on root.img, which the bin/browser launcher already tries.
  740. PACK_BROWSER_FILES = bin/browser.app=browser.elf
  741. # `install self` reads the flat kernel from the full pack. The lite/floppy
  742. # pack deliberately omits it (no room on a 1.44 MB disk).
  743. PACK_INSTALL_FILES = install/kernel.bin=kernel.bin
  744. # WiFi firmware (kernel/iwl_trans.c): the Intel 7265D/3165 (Chromebook, MVM)
  745. # and the 6200/6300 (ThinkPad X201, DVM). Taken from the host's
  746. # linux-firmware (`.zst` or plain); WIFI_FW_7265D / WIFI_FW_6000 override the
  747. # search. A machine without one still builds: the pack then has no
  748. # firmware/ entry for that card and `wifi` reports it missing.
  749. fw_search = $(firstword $(wildcard \
  750. /usr/lib/firmware/intel/iwlwifi/$(1).zst /usr/lib/firmware/$(1).zst \
  751. /usr/lib/firmware/intel/iwlwifi/$(1) /usr/lib/firmware/$(1) \
  752. /lib/firmware/$(1)))
  753. WIFI_FW_7265D ?= $(call fw_search,iwlwifi-7265D-29.ucode)
  754. WIFI_FW_6000 ?= $(call fw_search,iwlwifi-6000-4.ucode)
  755. WIFI_FW_FILE =
  756. PACK_FW_FILES =
  757. # $(1) = file name in the pack, $(2) = host source.
  758. define wifi_fw_rule
  759. ifneq ($(2),)
  760. WIFI_FW_FILE += firmware/$(1)
  761. PACK_FW_FILES += firmware/$(1)=firmware/$(1)
  762. firmware/$(1): $(2)
  763. @mkdir -p firmware
  764. case "$$<" in *.zst) zstd -q -d -c "$$<" > $$@ ;; *) cp "$$<" $$@ ;; esac
  765. else
  766. $$(info note: $(1) not found; building the pack without it)
  767. endif
  768. endef
  769. $(eval $(call wifi_fw_rule,iwlwifi-7265D-29.ucode,$(WIFI_FW_7265D)))
  770. $(eval $(call wifi_fw_rule,iwlwifi-6000-4.ucode,$(WIFI_FW_6000)))
  771. # Realtek RTL8821AU USB sticks (kernel/rtl8821au.c): the rtw88 8051 firmware
  772. # from linux-firmware (WIFI_FW_8821A overrides the search) and the register
  773. # tables, generated from third_party/rtw88 by the host tool mkrtwtbl. Both
  774. # are only in the full pack, like the Intel firmware.
  775. WIFI_FW_8821A ?= $(firstword $(wildcard \
  776. /usr/lib/firmware/rtw88/rtw8821a_fw.bin.zst \
  777. /usr/lib/firmware/rtw88/rtw8821a_fw.bin \
  778. /lib/firmware/rtw88/rtw8821a_fw.bin))
  779. $(eval $(call wifi_fw_rule,rtw8821a_fw.bin,$(WIFI_FW_8821A)))
  780. WIFI_FW_FILE += firmware/rtl8821a.tbl
  781. PACK_FW_FILES += firmware/rtl8821a.tbl=firmware/rtl8821a.tbl
  782. firmware/rtl8821a.tbl: mkrtwtbl
  783. @mkdir -p firmware
  784. ./mkrtwtbl $@
  785. cmds.pack: $(PACK_DEPS) $(WIFI_FW_FILE) $(CURSOR_FILE) kernel.bin
  786. ./mkpack $@ $(PACK_FILES) $(PACK_BROWSER_FILES) $(PACK_CURSOR_FILES) \
  787. $(PACK_INSTALL_FILES) \
  788. $(PACK_FW_FILES)
  789. # The floppy images cannot hold the ~1 MB firmware: they get the same pack
  790. # without firmware/* (and without the cursor bundle or bin/browser.app).
  791. cmds-lite.pack: $(PACK_DEPS)
  792. ./mkpack $@ $(PACK_FILES)
  793. # The boot-time rootfs is gzip-compressed; the raw cmds.pack is still what
  794. # cmds-dev/net-cmds send for a runtime replacement. Prefer libdeflate's
  795. # level 12 when it is installed: it saves about 8 KB over gzip -9 on the
  796. # current pack. The high-memory rootfs loader supports up to 2 MB; the
  797. # floppy images use the firmware-less lite pack (rootfs.bin).
  798. CMDS_GZ = $(shell command -v libdeflate-gzip >/dev/null 2>&1 && \
  799. echo "libdeflate-gzip -12 -c" || echo "gzip -9 -n -c")
  800. # The compressed rootfs cap: PACK_MAX in boot/vesa_stage.S (the legacy stage
  801. # copies the pack to 0x900000..0xD00000) and the embedded cap in
  802. # boot/efi/main.c must match.
  803. CMDS_GZ_MAX = 4194304
  804. cmds_lite_gz.bin: cmds-lite.pack
  805. $(CMDS_GZ) $< > $@
  806. cmds_pack_gz.bin: cmds.pack
  807. $(CMDS_GZ) $< > $@
  808. @sz=$$(stat -c%s $@); \
  809. if [ $$sz -gt $(CMDS_GZ_MAX) ]; then \
  810. echo "error: cmds_pack_gz.bin is $$sz bytes; the legacy rootfs cap is $(CMDS_GZ_MAX)"; \
  811. rm -f $@; exit 1; \
  812. fi
  813. # Rootfs image: one NBROOT1 header sector + the gzip pack (rootfs.h).
  814. # rootfs.bin (the lite pack, no firmware) is appended to the floppy images;
  815. # cmds.img is the 8 MB image flashed into the USB rootfs partition.
  816. rootfs.bin: cmds_lite_gz.bin mkrootfs
  817. ./mkrootfs $@ cmds_lite_gz.bin
  818. # Full NBROOT1 image (header + gzip pack, no partition padding) for the
  819. # network install path. `install self` uses the retained boot image instead.
  820. rootfs-full.bin: cmds_pack_gz.bin mkrootfs
  821. ./mkrootfs $@ cmds_pack_gz.bin
  822. rootfs: cmds.img
  823. cmds.img: cmds_pack_gz.bin mkrootfs
  824. ./mkrootfs $@ cmds_pack_gz.bin $$(( $(ROOTFS_SECTORS) * 512 ))
  825. # Diagnostic: no WM, boot console stays visible. Rebuild normally with
  826. # `make -B ...` afterwards (the objects carry -DNOWM).
  827. nowm:
  828. $(MAKE) NOWM=1 -B kernel.elf kernel.bin floppy.img usb.img usb-efi.img final.bin
  829. @echo "nowm: WM disabled; flash usb.img or usb-efi.img."
  830. root.img: browser.elf gallery.elf testdata/browser/demo.html testdata/browser/js.html testdata/browser/jsext.js testdata/images/rgb.bmp
  831. dd if=/dev/zero of=$@ bs=512 count=2880 2>/dev/null
  832. mkfs.fat -F 12 -n "ROOTDISK" $@
  833. echo "Hello from the second floppy!" | mcopy -i $@ - ::HELLO.TXT
  834. echo "This is a test file on drive 1." | mcopy -i $@ - ::TEST.TXT
  835. mcopy -i $@ browser.elf ::BROWSER
  836. mcopy -i $@ gallery.elf ::GALLERY
  837. mcopy -i $@ testdata/browser/demo.html ::DEMO.HTM
  838. mcopy -i $@ testdata/browser/js.html ::JS.HTM
  839. mcopy -i $@ testdata/browser/jsext.js ::JSEXT.JS
  840. mcopy -i $@ testdata/images/rgb.bmp ::PIC.BMP
  841. OVMF ?= $(firstword $(wildcard /usr/share/ovmf/x64/OVMF.4m.fd /usr/share/OVMF/OVMF.4m.fd))
  842. # Guest RAM for the QEMU targets, matching the 1 GB kernel ceiling (phys.h);
  843. # override with e.g. `make run QEMU_MEM=512M` (RAM above 1 GB is ignored).
  844. QEMU_MEM ?= 1G
  845. # root.img is deliberately not attached: a running QEMU may hold its write
  846. # lock, and the UEFI boot test does not need a second filesystem.
  847. run-uefi: usb-efi.img
  848. @test -n "$(OVMF)" || { echo "OVMF not found; set OVMF=/path/to/OVMF.4m.fd" >&2; exit 1; }
  849. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -smp 4 -bios $(OVMF) -snapshot \
  850. -drive file=usb-efi.img,format=raw,if=ide \
  851. -device pci-ohci -device usb-mouse \
  852. -device ne2k_pci,netdev=n0 -netdev user,id=n0 \
  853. -serial file:/tmp/opencode/nebos-uefi-serial.log
  854. test-uefi: usb-efi.img
  855. @test -n "$(OVMF)" || { echo "OVMF not found; set OVMF=/path/to/OVMF.4m.fd" >&2; exit 1; }
  856. @rm -f /tmp/opencode/nebos-uefi-serial.log
  857. timeout 40 qemu-system-x86_64 -M pc -m $(QEMU_MEM) -smp 4 -bios $(OVMF) -snapshot \
  858. -drive file=usb-efi.img,format=raw,if=ide \
  859. -device pci-ohci -device usb-mouse \
  860. -device ne2k_pci,netdev=n0 -netdev user,id=n0 \
  861. -display none -serial file:/tmp/opencode/nebos-uefi-serial.log \
  862. -no-reboot >/dev/null 2>&1 || true
  863. @grep -q "booted" /tmp/opencode/nebos-uefi-serial.log && \
  864. echo "UEFI boot: OK (/tmp/opencode/nebos-uefi-serial.log)" || \
  865. { echo "UEFI boot: FAILED"; tail -c 4000 /tmp/opencode/nebos-uefi-serial.log; exit 1; }
  866. run: image.bin root.img
  867. $(MAKE) run-floppy
  868. # Four logical CPUs (the Atom 330 topology). UP is still the default so a
  869. # machine without ACPI/MADT boots exactly as before.
  870. run-smp: floppy.img root.img
  871. qemu-system-i386 -M pc -m $(QEMU_MEM) -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 || \
  872. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -smp 4 -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
  873. floppy: floppy.img
  874. usb: usb.img
  875. # Write the dual-boot USB image (legacy BIOS + UEFI) to a stick; prompts
  876. # before destroying anything.
  877. # make flash USB=/dev/sdX FORCE=1 skips the removable-device check.
  878. # make flash FLASH_IMG=usb.img legacy-only image
  879. # The UEFI half needs the mingw toolchain (EFI_CC); the legacy half does not.
  880. FLASH_IMG ?= usb-efi.img
  881. flash: $(FLASH_IMG)
  882. @tools/flash.sh "$(USB)" "$(FORCE)" "$(FLASH_IMG)"
  883. run-floppy: floppy.img root.img
  884. qemu-system-i386 -M pc -m $(QEMU_MEM) -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 || \
  885. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -device pci-ohci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
  886. run-usb: usb.img root.img
  887. qemu-system-i386 -M pc -m $(QEMU_MEM) -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 || \
  888. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -boot c -device pci-ohci -device usb-mouse -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy
  889. # UHCI variant (machines with an Intel/VIA-style controller, and the
  890. # regression test for the UHCI backend of the USB core).
  891. run-uhci: floppy.img root.img
  892. qemu-system-i386 -M pc -m $(QEMU_MEM) -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 || \
  893. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -device piix3-usb-uhci -device usb-mouse -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
  894. # xHCI variant (Braswell/Intel PCH and other USB 3.0-only machines).
  895. run-xhci: floppy.img root.img
  896. qemu-system-i386 -M pc -m $(QEMU_MEM) -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 || \
  897. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -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
  898. # RNDIS variant: QEMU's usb-net is a full-speed RNDIS function. It sits on
  899. # xHCI (QEMU's USB2 root ports), and `-nic none` keeps the PCI e1000 out of
  900. # the way so the phone/tethering link is the active NIC.
  901. run-rndis: floppy.img root.img
  902. qemu-system-i386 -M pc -m $(QEMU_MEM) -nic none -device qemu-xhci -device usb-mouse -device usb-kbd -device usb-net,netdev=n1 -netdev user,id=n1 -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
  903. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -nic none -device qemu-xhci -device usb-mouse -device usb-kbd -device usb-net,netdev=n1 -netdev user,id=n1 -drive file=floppy.img,format=raw,if=floppy -drive file=root.img,format=raw,if=floppy
  904. # EHCI variant (USB 2.0 high-speed root ports). usb-storage is high-speed
  905. # and lands on EHCI; the mouse/keyboard are full-speed and use the UHCI
  906. # companion, exercising the multi-controller USB core.
  907. run-ehci: floppy.img root.img
  908. qemu-system-i386 -M pc -m $(QEMU_MEM) -boot a -device usb-ehci,id=ehci0 -device piix3-usb-uhci,id=uhci0 \
  909. -device usb-storage,bus=ehci0.0,drive=usbstick \
  910. -drive if=none,id=usbstick,file=root.img,format=raw \
  911. -device usb-mouse,bus=uhci0.0 -device usb-kbd,bus=uhci0.0 \
  912. -drive file=floppy.img,format=raw,if=floppy 2>/dev/null || \
  913. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -boot a -device usb-ehci,id=ehci0 -device piix3-usb-uhci,id=uhci0 \
  914. -device usb-storage,bus=ehci0.0,drive=usbstick \
  915. -drive if=none,id=usbstick,file=root.img,format=raw \
  916. -device usb-mouse,bus=uhci0.0 -device usb-kbd,bus=uhci0.0 \
  917. -drive file=floppy.img,format=raw,if=floppy
  918. # Passthrough of the real ASIX AX88772 dongle (requires the host's asix
  919. # driver to be unbound and permission on /dev/bus/usb/...; typically run with
  920. # sudo). `-nic none` keeps the QEMU e1000 out of the way so the USB NIC is
  921. # the only netdev.
  922. ASIX_HOST ?= vendorid=0x0b95,productid=0x7720
  923. run-ehci-asix: floppy.img root.img
  924. qemu-system-i386 -M pc -m $(QEMU_MEM) -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 || \
  925. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -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
  926. run-xhci-asix: floppy.img root.img
  927. qemu-system-i386 -M pc -m $(QEMU_MEM) -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 || \
  928. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -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
  929. # Network variant: a PCI NE2000 (RTL8029, the driver's QEMU match) on QEMU's
  930. # user-mode network (slirp). DHCP/DNS come from slirp, the host is 10.0.2.2
  931. # (e.g. `python3 -m http.server` + `curl http://10.0.2.2:8000/` in a window).
  932. run-net: floppy.img root.img
  933. qemu-system-i386 -M pc -m $(QEMU_MEM) -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 || \
  934. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -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
  935. # Intel e1000 variant: QEMU's 82540EM exercises the e1000e driver's MAC/DMA
  936. # path. (The X201's 82577LM uses the same registers; only its PCH PHY is
  937. # different.) usb-kbd is there for headless QMP `send-key` typing.
  938. run-e1000: floppy.img root.img
  939. qemu-system-i386 -M pc -m $(QEMU_MEM) -device pci-ohci -device usb-mouse -device usb-kbd \
  940. -device e1000,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 \
  941. -drive file=floppy.img,format=raw,if=floppy \
  942. -drive file=root.img,format=raw,if=floppy 2>/dev/null || \
  943. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -device pci-ohci -device usb-mouse -device usb-kbd \
  944. -device e1000,netdev=n0 -netdev user,id=n0,hostfwd=tcp::8080-:8080 \
  945. -drive file=floppy.img,format=raw,if=floppy \
  946. -drive file=root.img,format=raw,if=floppy
  947. # SATA/AHCI variant: an ICH9 AHCI controller with a test disk on port 0.
  948. # The disk image lives outside the repo and is created on first use; boot
  949. # stays on the floppy (order=a), the SATA disk shows up as sata0.
  950. SATA_IMG ?= /tmp/opencode/nebos-sata.img
  951. run-ahci: floppy.img root.img
  952. @test -f $(SATA_IMG) || truncate -s 512M $(SATA_IMG)
  953. qemu-system-i386 -M pc -m $(QEMU_MEM) -boot order=a -device pci-ohci -device usb-mouse \
  954. -drive file=floppy.img,format=raw,if=floppy \
  955. -drive file=root.img,format=raw,if=floppy \
  956. -device ich9-ahci,id=sata \
  957. -drive file=$(SATA_IMG),format=raw,if=none,id=satadisk \
  958. -device ide-hd,drive=satadisk,bus=sata.0 2>/dev/null || \
  959. qemu-system-x86_64 -M pc -m $(QEMU_MEM) -boot order=a -device pci-ohci -device usb-mouse \
  960. -drive file=floppy.img,format=raw,if=floppy \
  961. -drive file=root.img,format=raw,if=floppy \
  962. -device ich9-ahci,id=sata \
  963. -drive file=$(SATA_IMG),format=raw,if=none,id=satadisk \
  964. -device ide-hd,drive=satadisk,bus=sata.0
  965. # AMD Piledriver regression: QEMU's Opteron_G5 is the FX-6350's family 15h
  966. # model 2 CPU. Exercises the AMD CPUID/MTRR/xAPIC paths, the AMD thermal
  967. # probe, the IOAPIC mask and six-core ACPI SMP. QEMU has no SB950 or
  968. # RTL8111E, so the e1000/OHCI devices cover the rest of the boot path.
  969. run-amd: floppy.img root.img
  970. qemu-system-x86_64 -M pc -cpu Opteron_G5 -smp 6 -m $(QEMU_MEM) \
  971. -device pci-ohci -device usb-mouse -device usb-kbd \
  972. -device e1000,netdev=n0 -netdev user,id=n0 \
  973. -drive file=floppy.img,format=raw,if=floppy \
  974. -drive file=root.img,format=raw,if=floppy
  975. test-amd: floppy.img root.img
  976. @rm -f /tmp/opencode/nebos-amd-serial.log
  977. timeout 40 qemu-system-x86_64 -M pc -cpu Opteron_G5 -smp 6 -m $(QEMU_MEM) \
  978. -device pci-ohci -device usb-mouse -device usb-kbd \
  979. -device e1000,netdev=n0 -netdev user,id=n0 \
  980. -drive file=floppy.img,format=raw,if=floppy \
  981. -drive file=root.img,format=raw,if=floppy \
  982. -display none -serial file:/tmp/opencode/nebos-amd-serial.log \
  983. -no-reboot >/dev/null 2>&1 || true
  984. @grep -q "booted" /tmp/opencode/nebos-amd-serial.log && \
  985. grep -q "smp cpus: 6" /tmp/opencode/nebos-amd-serial.log && \
  986. grep -q "ioapic: masked" /tmp/opencode/nebos-amd-serial.log && \
  987. grep -q "thermal: AMD family 15h" /tmp/opencode/nebos-amd-serial.log && \
  988. ! grep -q "PIT NOT TICKING" /tmp/opencode/nebos-amd-serial.log && \
  989. echo "AMD boot: OK (/tmp/opencode/nebos-amd-serial.log)" || \
  990. { echo "AMD boot: FAILED"; tail -c 4000 /tmp/opencode/nebos-amd-serial.log; exit 1; }
  991. # Headless USB HID regression tests (QEMU + python3): cursor movement via
  992. # injected USB mouse reports and keyboard input via injected key events,
  993. # including hotplug. See tools/*.py.
  994. test-usb: floppy.img root.img
  995. python3 tools/qmp_cursor_test.py
  996. python3 tools/qmp_cursor_test.py --uhci
  997. python3 tools/qmp_cursor_test.py --uhci72
  998. python3 tools/qmp_cursor_test.py --hotplug
  999. python3 tools/qmp_cursor_test.py --xhci
  1000. python3 tools/qmp_cursor_test.py --ehci
  1001. python3 tools/qmp_keyboard_test.py
  1002. python3 tools/qmp_keyboard_test.py --hotplug
  1003. python3 tools/qmp_keyboard_test.py --xhci
  1004. python3 tools/qmp_keyboard_test.py --ehci
  1005. # End-to-end installer test: boot the dual-boot USB image as an IDE disk,
  1006. # attach the same image as a USB mass-storage source, type `install ide1`
  1007. # on the terminal and verify + boot the cloned target.
  1008. test-install: usb-efi.img
  1009. python3 tools/qmp_install_test.py
  1010. scp-final: final.bin
  1011. scp final.bin root@192.168.0.136:
  1012. ssh root@192.168.0.136 dd if=final.bin of=/dev/fd0 bs=512
  1013. scp-root: root.img
  1014. scp root.img root@192.168.0.136:
  1015. ssh root@192.168.0.136 dd if=root.img of=/dev/fd0 bs=512
  1016. SERIAL_DEV ?= /dev/ttyUSB0
  1017. SERIAL_BAUD ?= 115200
  1018. run-serial:
  1019. @echo "Starting serial monitor on $(SERIAL_DEV) at $(SERIAL_BAUD) baud..."
  1020. @echo "Type 'serial test' in the nebos terminal."
  1021. @echo "Press Ctrl-A Ctrl-X to exit picocom."
  1022. picocom $(SERIAL_DEV) --baud $(SERIAL_BAUD)
  1023. monitor:
  1024. @echo "Watching $(SERIAL_DEV) @ $(SERIAL_BAUD) baud (Ctrl-A Ctrl-X to exit)."
  1025. @echo "Each 'make dev' reload prints: NebOS v<version> booted"
  1026. picocom $(SERIAL_DEV) --baud $(SERIAL_BAUD)
  1027. dev: kernel.bin cmds_pack_gz.bin kexec_send
  1028. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  1029. if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
  1030. echo "Sending kernel.bin + cmds_pack_gz.bin (entry 0x$$entry) via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."; \
  1031. ./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin cmds_pack_gz.bin
  1032. # Kernel-only fast path: the board keeps its running rootfs.
  1033. kernel-dev: kernel.bin kexec_send
  1034. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  1035. if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
  1036. echo "Sending kernel.bin only (entry 0x$$entry) via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."; \
  1037. ./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
  1038. # Push just the command pack (cmds.pack) to a running board: no kernel
  1039. # reload, the /ramfs contents are replaced in place. The board must be idle
  1040. # at a prompt (the WM loop auto-detects the transfer).
  1041. cmds-dev: cmds.pack kexec_send
  1042. @echo "Sending cmds.pack via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."
  1043. ./kexec_send --payload $(SERIAL_DEV) $(SERIAL_BAUD) cmds.pack
  1044. net-cmds: cmds.pack netkexec_send
  1045. @echo "Sending cmds.pack to $(BOARD_IP):$(NETKEXEC_PORT)..."
  1046. ./netkexec_send --payload $(BOARD_IP) $(NETKEXEC_PORT) cmds.pack
  1047. # Development override: upload the real browser to the board's writable
  1048. # /share; the bin/browser launcher prefers it over the pack copy. The board
  1049. # must be running `websrv`; stop it before browsing HTTP, since the WebDAV
  1050. # server owns the NIC.
  1051. browser-dev: browser.elf
  1052. @echo "Uploading browser.elf to http://$(BOARD_IP):8080/share/BROWSER..."
  1053. curl -f -T browser.elf http://$(BOARD_IP):8080/share/BROWSER
  1054. # gallery follows the same development-override model.
  1055. gallery-dev: gallery.elf
  1056. @echo "Uploading gallery.elf to http://$(BOARD_IP):8080/share/GALLERY..."
  1057. curl -f -T gallery.elf http://$(BOARD_IP):8080/share/GALLERY
  1058. # Browser compatibility ladder: serve testdata/browser/compat so the board
  1059. # or a QEMU guest can open http://<host>:$(COMPAT_PORT)/.
  1060. COMPAT_PORT ?= 8000
  1061. compat-serve:
  1062. @echo "Serving the browser compatibility ladder on port $(COMPAT_PORT)"
  1063. python3 -m http.server -d testdata/browser/compat $(COMPAT_PORT)
  1064. # Show the board's netlog stream (kernel debug + terminal text) like `make
  1065. # monitor` does over serial.
  1066. netmon: netmonitor
  1067. @echo "Listening for netlog on UDP $(NETLOG_PORT) (Ctrl-C to exit)."
  1068. ./netmonitor $(NETLOG_PORT)
  1069. # Push a fresh kernel over Ethernet (the board auto-detects it from its idle
  1070. # loop; no serial and no USB stick involved).
  1071. BOARD_IP ?= 192.168.178.240
  1072. NETLOG_PORT ?= 19290
  1073. NETKEXEC_PORT ?= 19288
  1074. net-dev: kernel.bin cmds_pack_gz.bin netkexec_send
  1075. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  1076. if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
  1077. echo "Sending kernel.bin + cmds_pack_gz.bin (entry 0x$$entry) to $(BOARD_IP):$(NETKEXEC_PORT)..."; \
  1078. ./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin cmds_pack_gz.bin
  1079. # Kernel-only fast path: the board keeps its running rootfs.
  1080. kernel-net-dev: kernel.bin netkexec_send
  1081. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  1082. if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
  1083. echo "Sending kernel.bin only (entry 0x$$entry) to $(BOARD_IP):$(NETKEXEC_PORT)..."; \
  1084. ./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
  1085. # Persistent update: write P1/P2 on the target disk (P4/data is untouched),
  1086. # then jump into the new kernel. TARGET=auto picks the only non-USB NebOS
  1087. # disk; use NETINSTALL_TARGET=mmc0/sata0/... to override.
  1088. NETINSTALL_TARGET ?= auto
  1089. net-install: kernel.bin rootfs-full.bin netkexec_send
  1090. @entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
  1091. if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
  1092. echo "Persisting kernel.bin + rootfs-full.bin to $(NETINSTALL_TARGET) on $(BOARD_IP):$(NETKEXEC_PORT)..."; \
  1093. ./netkexec_send --install $(NETINSTALL_TARGET) $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin rootfs-full.bin
  1094. clean:
  1095. rm -rf $(B) host/build
  1096. rm -f kernel.elf kernel.bin boot.bin mbr.bin boot.bin.tmp image.bin \
  1097. floppy.img usb.img usb-efi.img esp.img final.bin root.img \
  1098. BOOTX64.EFI browser.elf gallery.elf *.boot.tmp \
  1099. cmds.pack cmds-lite.pack cmds_pack_gz.bin cmds_lite_gz.bin \
  1100. rootfs.bin rootfs-full.bin cmds.img \
  1101. firmware/*.ucode firmware/*.tbl firmware/*.bin cursors/*.cur icons/*.icn \
  1102. mkpack mkrootfs mkrtwtbl mkcursors mkicons mkappicons jsstrip cursor_test kexec_send netkexec_send netmonitor \
  1103. browser-host host-curl \
  1104. ufont_test ufont_test_asan svg_test touchpad_gesture_test \
  1105. ujson_test uhttp_cookie_test chat_login_test tls_test wpa_test \
  1106. wifi_store_test ccmp_test img_test img_test_asan malloc_test \
  1107. format_test math_test div64_test setjmp_test js_test
  1108. # Compiler-generated header dependencies.
  1109. -include $(shell find $(B) host/build -name '*.d' 2>/dev/null)