Explorar o código

Decouple the command pack into a bootable rootfs

The pack no longer lives in kernel .rodata.  mkrootfs wraps the gzip pack
in a NBROOT1 rootfs image; the legacy stage copies it into high RAM with
INT 15h AH=87h, the UEFI loader embeds it, and kexec stages it.  mm_init
reserves the boot region and ramfs_init copies it into page-allocated
memory before the region is freed, leaving ~134 KB of kernel headroom
below the 0x7F000 wall.

Flashed images now carry the rootfs: usb.img gains an 8 MB partition 2
(type 0x7F, LBA 131072) and usb-efi.img a third ESP at LBA 147456; the
floppy and final images append the same header + pack.

Reloads carry kernel + rootfs atomically: KXE2 over serial and KXN2 over
UDP add kernel_size/pack_size and stage both; pack_size 0 keeps the
running rootfs (make kernel-dev/kernel-net-dev).  cmds-dev/net-cmds keep
the KXNP runtime raw-pack replacement.  The old KXEC/KXNU magics are
ignored, so the first new kernel needs a flash or the updated bootstrap.

The pack now contains every command plus bin/browser.app and
bin/gallery.app (the launchers prefer /share, then the pack, then FAT
disks), so filesystem spawns still work.
Johannes Zellner hai 2 semanas
pai
achega
5256b5b0ce
Modificáronse 29 ficheiros con 1376 adicións e 357 borrados
  1. 1 0
      .gitignore
  2. 149 94
      AGENTS.md
  3. 96 33
      Makefile
  4. 7 0
      boot.S
  5. 24 9
      bootinfo.h
  6. 17 6
      bootstrap.c
  7. 71 10
      efi/main.c
  8. 9 1
      efi/payload.S
  9. 28 16
      kernel.c
  10. 95 27
      kexec.c
  11. 8 5
      kexec.h
  12. 106 62
      kexec_send.c
  13. 134 0
      mkrootfs.c
  14. 32 0
      mm.c
  15. 3 0
      mm.h
  16. 31 10
      netkexec.c
  17. 1 1
      netkexec.h
  18. 74 38
      netkexec_send.c
  19. 5 5
      payload.c
  20. 37 8
      ramfs.c
  21. 8 2
      ramfs.h
  22. 34 0
      rootfs.h
  23. 2 2
      run.c
  24. 6 4
      ucmd_browser.c
  25. 7 4
      ucmd_gallery.c
  26. 387 15
      vesa_stage.S
  27. 2 2
      vfs.c
  28. 1 1
      wm.c
  29. 1 2
      xhci.c

+ 1 - 0
.gitignore

@@ -13,3 +13,4 @@ mkpack
 tls_test
 img_test
 img_test_asan
+mkrootfs

+ 149 - 94
AGENTS.md

@@ -19,6 +19,11 @@ make run-ahci   # like `make run` + an ICH9 AHCI controller and a 512 MB SATA di
 make uefi       # build BOOTX64.EFI and the dual-boot usb-efi.img
 make run-uefi   # boot usb-efi.img under OVMF (x86-64 UEFI; serial log in /tmp/opencode)
 make test-uefi  # headless OVMF boot test (greps the serial log for the boot line)
+make rootfs     # build cmds.img (the 8 MB rootfs partition image)
+make dev        # serial kernel + rootfs reload (KXE2)
+make kernel-dev # serial kernel-only reload (keeps the running rootfs)
+make net-dev    # network kernel + rootfs reload (KXN2)
+make kernel-net-dev
 make flash      # write the dual-boot image (legacy BIOS + UEFI) to a USB stick
 make test-usb   # headless USB mouse/keyboard regression tests (QEMU + python3)
 make test-images  # host image-decoder + blit tests (ASan variant: test-images-asan)
@@ -27,24 +32,32 @@ make clean
 
 - Build flags (in `Makefile`): `-m32 -march=i586 -ffreestanding -fno-pie -nostdlib
   -fno-stack-protector -mno-sse -mno-sse2 -mno-mmx -O2`.
+- The command pack is the system rootfs: `cmds.pack` (NBPAK1), gzip-compressed
+  to `cmds_pack_gz.bin`, wrapped by `mkrootfs` with a one-sector `NBROOT1`
+  header into `rootfs.bin` (floppy append) and `cmds.img` (the 8 MB USB
+  partition image).  The kernel itself no longer carries the pack; the boot
+  path loads it into RAM and `ramfs_init` takes a private copy (`bin/<name>`
+  mounted at `/ramfs`).
 - `root.img` is a second FAT12 floppy holding user binaries (`PROG.BIN`, `SPACE.BIN`),
-  the external browser (`BROWSER`) and its `DEMO.HTM`/`PIC.BMP` test page, plus
-  a few test files; it requires `mtools` to build.
+  the disk fallback copies of the browser/gallery (`BROWSER`, `GALLERY`) and
+  its `DEMO.HTM`/`PIC.BMP` test page, plus a few test files; it requires
+  `mtools` to build.
 - `make send PROGRAM=prog` transfers a binary to a real machine over serial.
 - UEFI needs `x86_64-w64-mingw32-gcc` (the 64-bit loader) and, for the QEMU
   targets, an OVMF firmware (`OVMF=/path/to/OVMF.4m.fd` overrides the default).
   `make all` does not build the UEFI image, so a machine without mingw still
   builds the legacy floppy/USB images normally.
-- `usb.img` remains the legacy-only image.  `make flash` uses `usb-efi.img`,
-  which is `usb.img` plus an 8 MB FAT16 ESP in a new third MBR partition
-  (type `0xEF`, LBA 131072).  The MBR boot code, partition entries 1/2 and the
-  active partition 1 are unchanged except for one BPB byte: FATSize16 is
-  cleared in the dual image (disk offset `0x7E16`).  Without that, UEFI
-  firmware scans the partition, sees the FAT16-looking boot sector and mounts
-  the raw kernel blob as a volume, writing FAT/root pages over the kernel at
-  LBA 64.  `boot.S` and the BIOS never read FATSize16.  Legacy boot from the
-  patched image is tested; `make flash FLASH_IMG=usb.img` flashes the
-  untouched legacy-only image.  `make flash` needs the mingw toolchain because
+- `usb.img` is 72 MB: partition 1 (type `0x06`, LBA 63) is the raw kernel
+  blob and partition 2 (type `0x7F`, LBA 131072) is the 8 MB rootfs image
+  (`cmds.img`).  The boot sector's `pack_lba` field points the VESA stage at
+  partition 2.  `usb-efi.img` (80 MB) adds an 8 MB FAT16 ESP as partition 3
+  (type `0xEF`, LBA 147456); `BOOTX64.EFI` embeds the kernel + gz pack.
+  `make flash` writes the dual image; `make flash FLASH_IMG=usb.img` writes
+  the legacy-only image.  One BPB byte is cleared in the dual image (disk
+  offset `0x7E16`, FATSize16): without that, UEFI firmware scans partition 1,
+  sees the FAT16-looking boot sector and mounts the raw kernel blob as a
+  volume, writing FAT/root pages over the kernel at LBA 64.  `boot.S` and the
+  BIOS never read FATSize16.  `make flash` needs the mingw toolchain because
   it builds `usb-efi.img`.
 
 ## Boot chain
@@ -52,9 +65,13 @@ make clean
 1. `boot.S` — 512-byte boot sector (FAT16 BPB). Loads contiguous kernel sectors
    to `0x7E00` (CHS for floppy, INT 13h AH=42h LBA for hard disk), jumps there.
 2. `vesa_stage.S` — real-mode VESA stage. Probes INT 15h E820 into `0x1000`,
-   picks best VBE mode (≥16 bpp, LFB, fits VRAM, ≤1280px), writes mode info to
-   `0x600`, enables A20, enters protected mode, jumps to `kernel_entry`
-   (`entry.S`, re-points ESP to the current stack top, then `kernel_main`).
+   enables A20, reads the `NBROOT1` rootfs header at the boot sector's
+   `pack_lba` field and copies the gzip pack to the top of RAM below 16 MB
+   with INT 15h AH=87h (no mode switching), picks the best VBE mode
+   (≥16 bpp, LFB, fits VRAM, ≤1280px), writes mode info to `0x600` and the
+   12-dword `struct boot_info` (magic/checksum/rsdp/flags/pack range) to
+   `0x500`, enters protected mode and jumps to `kernel_entry` (`entry.S`,
+   re-points ESP to the current stack top, then `kernel_main`).
 3. `kernel.c` — `kernel_main` zeroes BSS then inits serial → memory/paging →
    ACPI/MADT → I/O → idle → processes → timer (PIT; 1 kHz busy, 100 Hz idle + IDT) → SMP AP
    bring-up → mouse → VGA shadow buffer → terminal.
@@ -63,11 +80,12 @@ make clean
 5. **UEFI (`efi/`)** — an x86-64 PE32+ `BOOTX64.EFI` loads from the ESP. It
    picks a GOP mode, converts `GetMemoryMap` into the E820 map at `0x1000`,
    stages `kernel.bin` below 4 GB, fills `0x500` (boot info) and `0x600`
-   (framebuffer), calls `ExitBootServices`, copies a 192-byte trampoline to
-   `0x800`, leaves IA-32e mode (`CR0.PG`, `EFER.LME`, `CR4.PAE/PGE/PCIDE/
-   SMEP/SMAP/LA57`), copies the kernel to `0x7E00` and jumps to
-   `kernel_entry`.  The BIOS boot sector / VESA stage are not executed on this
-   path; the legacy chain is unchanged.
+   (framebuffer), calls `ExitBootServices`, copies the embedded gz rootfs to
+   the top of RAM below 128 MB, copies a 192-byte trampoline to `0x800`,
+   leaves IA-32e mode (`CR0.PG`, `EFER.LME`, `CR4.PAE/PGE/PCIDE/SMEP/SMAP/
+   LA57`), copies the kernel to `0x7E00` and jumps to `kernel_entry`.  The
+   BIOS boot sector / VESA stage are not executed on this path; the legacy
+   chain is separate (it loads the same gz pack from the rootfs image).
 
 SMP is ACPI-only: RSDP → RSDT/XSDT → MADT, then `LAPIC` + `INIT-SIPI-SIPI`.
 There is deliberately no Intel MP-table parser.  If ACPI, a usable MADT, the
@@ -79,7 +97,7 @@ a Pentium 1 (and a Pentium II board without ACPI) on the UP path.
 
 | Address    | Purpose |
 |-----------|---------|
-| `0x500`   | UEFI boot info (`struct boot_info`, `bootinfo.h`): magic/checksum, ACPI RSDP, EFI/RGBX flags; `vesa_stage.S` clears the magic on BIOS boot |
+| `0x500`   | Boot info (`struct boot_info`, `bootinfo.h`, 12 dwords): magic/checksum, ACPI RSDP, EFI/RGBX flags, and the boot-time rootfs pack `addr`/`size`; written by `vesa_stage.S`, the UEFI loader or kexec |
 | `0x600`   | Boot framebuffer info (`struct vga_boot_fb_info`, see `vga.h`; written by `vesa_stage.S` or the UEFI loader) |
 | `0x7C00`  | Boot sector load address; also the UEFI handoff stack top |
 | `0x7E00`  | Kernel load / VESA-stage entry (`kernel.ld`); the UEFI loader copies the kernel here and jumps to `kernel_entry` |
@@ -91,12 +109,20 @@ a Pentium 1 (and a Pentium II board without ACPI) on the UP path.
 | `0x6000`  | AP real/protected-mode trampoline (`ap_tramp.S`/`ap_tramp.ld`; SIPI vector 0x06, image ends below `0x6C00`) |
 | `0x6C00`  | AP trampoline real-mode stack top (grows down) |
 | `0x6F00`  | AP startup info block (CR3, stack top, C entry, CPU index) |
-| `_end`    | Kernel end (linker symbol; `kernel.ld` ASSERTs it stays below `0x7F000`) |
+| `_end`    | Kernel end (linker symbol; `kernel.ld` ASSERTs it stays below `0x7F000`); the command pack is no longer in the image, so this has ~130 KB of headroom |
 | (in-image) | Bootstrap/protected-mode stack: 8 KB `.stack` NOLOAD section, `__boot_stack_top`/`__boot_stack_bottom` (travels with the kernel) |
 | `0x7FF00` | Program API struct (`PROGRAM_API_ADDR`, `run_api.h`) |
 | `0x80000` | Legacy ring-0 program load area (`run.c`, `serial_recv.c`, `user.ld`; max 0x6000, inside the reserved low 1 MB) |
 | `0x100000`| VGA shadow buffer (`VGA_SHADOW_BASE`, `vga.h`) |
 
+The boot-time rootfs pack is also dynamic: the legacy stage puts it at the
+top of RAM below 16 MB (the AH=87h 24-bit descriptor limit), the UEFI loader
+at the top of RAM below 128 MB, and kexec leaves it in the staging buffer
+(≥ `0x900000`).  `mm_init` reserves `[pack_addr, pack_addr+pack_size)` from
+boot info; `ramfs_init` copies/inflates it into fresh pages and
+`mm_free_range()` releases the boot region.  The placement is always above
+the 1..9 MB VGA shadow reserve.
+
 Everything else is dynamic: process stacks and the kexec staging buffer come
 from the frame allocator (≥ `0x900000`), and the fallback stack pool is a
 static BSS array.  The UEFI loader's kernel staging buffer comes from
@@ -153,14 +179,15 @@ run area), so adding code no longer moves any address.
   fallback pool (a static BSS array; no fixed address).
 - **UEFI boot**: `bootinfo.h` (the `0x500` block and its checksum/accessors),
   `efi/efi.h` (minimal UEFI types and table/protocol layouts), `efi/main.c`
-  (GOP mode choice, `GetMemoryMap` → E820, staging below 4 GB, boot-info/
-  framebuffer blocks, `ExitBootServices`), `efi/tramp.S` + `efi/tramp.ld` (the
-  `0x800` long-mode exit, kernel copy and jump) and `efi/payload.S` (embeds
-  `kernel.bin` and the trampoline in `BOOTX64.EFI`).  `vesa_stage.S` clears
-  the boot-info magic; `vga.c`/`panic.c` honour the RGBX flag and skip the
-  VBE-only ATI CRTC reset / NVIDIA stolen-VRAM alias under EFI; `mouse.c`
-  forces the software cursor; `acpi.c` prefers the passed RSDP and maps its
-  page before validating it.
+  (GOP mode choice, `GetMemoryMap` → E820, staging below 4 GB, rootfs copy,
+  boot-info/framebuffer blocks, `ExitBootServices`), `efi/tramp.S` +
+  `efi/tramp.ld` (the `0x800` long-mode exit, kernel copy and jump) and
+  `efi/payload.S` (embeds `kernel.bin`, `cmds_pack_gz.bin` and the trampoline
+  in `BOOTX64.EFI`).  The legacy `vesa_stage.S` writes the same v2 boot info
+  (with `pack_addr`/`pack_size`); `vga.c`/`panic.c` honour the RGBX flag and
+  skip the VBE-only ATI CRTC reset / NVIDIA stolen-VRAM alias under EFI;
+  `mouse.c` forces the software cursor; `acpi.c` prefers the passed RSDP and
+  maps its page before validating it.
 - **Paging**: `paging.c/h` — identity-maps RAM (4 MB pages via CR4.PSE) + the
   LFB (uncacheable) and enables CR0.PG/WP. Each process gets a page directory
   cloned from the kernel identity map (kernel stacks are mapped everywhere, so
@@ -390,9 +417,9 @@ run area), so adding code no longer moves any address.
   and show whether the fans turn.  Nothing is written to the fan control.
 - **User programs**: `run.c`, `run_api.h`, `prog.c`, `space.c` — raw binaries at
   `0x80000`, API function-pointer struct at `0x7FF00`.  `prog`/`space` are
-  **embedded in the kernel image** (`prog_embed.o`/`space_embed.o`, like
-  `hello.elf`), so `make dev`/`make net-dev` update them too and a board with
-  no readable filesystem can still run them: `run <name>` and the `prog`/
+  **embedded in the kernel image** (`prog_embed.o`/`space_embed.o`), so
+  `make dev`/`make net-dev` update them too and a board with no readable
+  filesystem can still run them: `run <name>` and the `prog`/
   `space` commands use the embedded copy, `run /<device>/<file>` still loads
   from FAT.  Rebuilds are wired (`space.c` -> `space.bin` -> `space_embed.o` ->
   `kernel.elf`).  Note: `run`/`prog`/`space` still draw over the WM (which keeps
@@ -409,13 +436,18 @@ run area), so adding code no longer moves any address.
   canvas mapping at `USER_GFX_BASE` (`0x50000000`) and the WM mappings are
   separate.
 - **Ring-3 commands**: `ucmd_*.c`, `ucmd_lib.h`, `ucmd.ld`, `mkpack.c`,
-  `ramfs.c/h` — freestanding ELFs linked at `USER_BASE` (`0x40000000`), packed
-  by `mkpack` into `cmds.pack` (as `bin/<name>`), embedded in the kernel
-  image (`cmds_pack.o`) and mounted read-only at `/ramfs`.  The shell finds
-  them through PATH (`/ramfs/bin` by default), loads the whole ELF through
-  the VFS and spawns it as a ring-3 process; `hello` is just `bin/hello`.
-  New command: add `ucmd_<name>.c` to UCMDS — the pack, the mount and the
-  PATH listing pick it up automatically.  They talk to the kernel through
+  `rootfs.h`, `mkrootfs.c`, `ramfs.c/h` — freestanding ELFs linked at
+  `USER_BASE` (`0x40000000`), packed by `mkpack` into `cmds.pack` (as
+  `bin/<name>`), gzipped to `cmds_pack_gz.bin` and wrapped by `mkrootfs`
+  with a `NBROOT1` header.  The pack is the system rootfs: the boot path
+  loads it into high RAM (legacy AH=87h / UEFI / kexec) and `ramfs_init`
+  takes a private page-allocated copy, mounted read-only at `/ramfs`.  The
+  shell finds them through PATH (`/ramfs/bin` by default), loads the whole
+  ELF through the VFS and spawns it as a ring-3 process; `hello` is just
+  `bin/hello`.  New command: add `ucmd_<name>.c` to UCMDS — the pack, the
+  mount and the PATH listing pick it up automatically.  `SYS_SPAWN` still
+  loads ELFs from any VFS mount, so commands can also live on `/share` or a
+  FAT disk (`path /share/bin`, or an absolute path).  They talk to the kernel through
   `int 0x80` (`syscall.h`: putstr/ticks/sleep/exit, the whole-file service in
   `sys_fs.c`, the state snapshots in `sys_info.c` and the network service in
   `sys_net.c`) and inherit the terminal's screen, so their output lands in
@@ -435,19 +467,19 @@ run area), so adding code no longer moves any address.
 - **Gallery**: `gallery.c` is the ngui widget gallery: typography and
   truncation, the drawing primitives and clip stack, the theme palette, all
   nine icons, live buttons/icon buttons, text fields with event feedback, a
-  nested draggable scroll pane and the `ngui_cut_*` layout helpers.  It is a
-  disk app (`GALLERY` on `root.img`, or `/share`/FAT mounts) like the browser
-  because the embedded command pack is tight against the `0x7F000` wall;
-  `ucmd_gallery.c` is the tiny `bin/gallery` launcher, and `make gallery-dev`
-  uploads the real app to a board's `/share`.
-- **Browser**: `ucmd_browser.c` is a tiny `bin/browser` launcher; the real
-  browser is the external `BROWSER` on `root.img` (also looked up in `/share`
-  and on IDE/SATA FAT mounts) because it links ngui, the full image library,
-  inflate, TLS and uhttp and would not fit the compressed command pack.  The
-  launcher forwards its argument and cwd; `make run`/`run-net` attach
-  `root.img`, and a netbooted board can upload `browser.elf` to `/share`
-  (`make browser-dev` uses the board's `websrv`; stop `websrv` before browsing
-  HTTP, because it owns the NIC).
+  nested draggable scroll pane and the `ngui_cut_*` layout helpers.  The real
+  ELF ships in the pack as `bin/gallery.app`; `ucmd_gallery.c` is the tiny
+  `bin/gallery` launcher, which prefers `/share/GALLERY` (development upload
+  via `make gallery-dev`), then the pack copy, then the `GALLERY` disk
+  fallback on `root.img`/FAT mounts.
+- **Browser**: the real browser ships in the pack as `bin/browser.app`
+  (it links ngui, the full image library, inflate, TLS and uhttp, but the
+  rootfs is no longer constrained by the kernel's low-memory window).
+  `ucmd_browser.c` is the tiny `bin/browser` launcher; it prefers
+  `/share/BROWSER` (`make browser-dev` uploads it via the board's `websrv`;
+  stop `websrv` before browsing HTTP, because it owns the NIC), then the pack
+  copy, then `BROWSER` on `root.img`/IDE/SATA fat mounts.  The launcher
+  forwards its argument and cwd.
   `uhttp.c/h` is the shared ring-3 HTTP client extracted from curl: URL
   parsing/resolution, redirects, chunked decoding, timeouts and TLS; curl and
   the browser both use it.  `browser_html.c/h` is a tolerant HTML tree + the
@@ -528,7 +560,7 @@ run area), so adding code no longer moves any address.
   HTTP/1.1 + WebDAV from the VFS (`/` lists mounts, `/ramfs` is read-only,
   FAT mounts accept only names that round-trip to 8.3, `/share` is the
   writable target).  While listening, `SYS_TCP_ACCEPT` also answers ARP/ICMP
-  and captures a KXNU/KXNP header through `netkexec_capture_frame()`, so
+  and captures a KXN2/KXNP header through `netkexec_capture_frame()`, so
   `make net-dev`/`make net-cmds` interrupts the server cleanly instead of
   waiting for header retries.  The body is buffered whole (4 MB, or 1 MB when
   the heap alloc fails) from the per-process arena; the TCP receive path sends
@@ -589,9 +621,9 @@ Develop without reflashing the floppy. The serial link (COM1 ↔ `/dev/ttyUSB0`)
 runs at **115200 baud**. Two paths exist:
 
 - **Auto-detect** (the normal loop) — the WM loop watches the serial
-  ring buffer for the `"KXEC"` magic and auto-spawns the reload, so `make dev`
-  alone boots a fresh kernel. The manual `kexec` command is kept as a fallback
-  that waits for the same image.
+  ring buffer for the `"KXE2"` magic and auto-spawns the reload, so `make dev`
+  alone boots a fresh kernel + rootfs. The manual `kexec` command is kept as a
+  fallback that waits for the same image.
 - **`bootstrap`** (one-time, zero-reflash) — a ring-0 program loaded through the
   existing `serial recv` + `run serial` path that switches the UART to 115200,
   receives a new kernel, and jumps into it. Used only to install the first new
@@ -600,17 +632,23 @@ runs at **115200 baud**. Two paths exist:
   that is where the old build's `serial recv` writes it.
 
 Wire protocol (host → target, sent immediately; no handshake):
-`[magic "KXEC"][entry u32 LE][size u32 LE][data][checksum u8]`.
-The command pack has its own frame (`make cmds-dev`), the same layout with a
-`"KXNP"` magic (the entry field is ignored), which replaces /ramfs in place
-(see below).  On SMP the APs are INIT-parked just before the jump, and the
-serial receive drops the BKL while it waits for bytes so the WM and the other
-CPU's syscalls stay responsive during a `make dev`.
+`[magic "KXE2"][entry u32 LE][kernel_size u32 LE][pack_size u32 LE]`
+`[kernel][rootfs gzip][checksum u8]`.
+`pack_size == 0` tells the receiver to keep the running rootfs (kernel-only
+reload). A full reload stages both in one page-allocated buffer, copies only
+`kernel_size` bytes over `0x7E00`, and publishes the pack range in boot info;
+the new kernel copies/inflates it and frees the staging tail. The rootfs-only
+runtime update (`make cmds-dev`) keeps the old framing
+`[magic "KXNP"][entry u32][size u32][raw pack][checksum u8]` and replaces
+`/ramfs` in place (see below). On SMP the APs are INIT-parked just before the
+jump, and the serial receive drops the BKL while it waits for bytes so the WM
+and the other CPU's syscalls stay responsive during a `make dev`.
 
 **Every iteration:**
 ```sh
-make dev            # builds kernel.bin (which carries cmds.pack), sends
-make cmds-dev       # optional: push just the command pack to a running board
+make dev            # kernel.bin + cmds_pack_gz.bin (atomic; ~46 s at 115200)
+make kernel-dev     # kernel only; the new kernel keeps the running rootfs
+make cmds-dev       # runtime raw-pack update, no reboot
 ```
 The reload is automatic. On boot the kernel prints `Nebs OS v<version> booted`
 to COM1, so a serial monitor can confirm the new build headlessly:
@@ -631,23 +669,31 @@ make bootstrap                                   # builds bootstrap.bin
 make dev                                         # 115200
 ```
 After the first kexec the machine is at 115200, and `make dev` is all you need
-from then on.
+from then on.  `make bootstrap` is built from the new tree and speaks `KXE2`;
+it stages only the kernel and discards the rootfs bytes, so run `make dev`
+again (or `make cmds-dev`) to install a pack afterwards.
 
 Notes: the kexec path rebuilds a flat GDT at `0x900` before jumping (the old GDT
 lives inside the overwritten kernel region), so it's safe to edit any file,
 including `vesa_stage.S`/`boot.S` layout — but the video *mode* itself is only
 re-applied on a full floppy/USB boot (C-light reuses the current LFB).
 
-## Command pack hot-reload (cmds-dev / net-cmds)
+## Command pack rootfs (cmds.pack / cmds.img / cmds-dev)
 
 The commands live in `cmds.pack` (`bin/<name>` entries, built by `mkpack`),
-which is gzip-compressed at build time (`cmds_pack_gz.bin`) and embedded in
-`kernel.bin`; `ramfs_init()` inflates it at boot with `inflate.c` and mounts it
-read-only at `/ramfs`.  The compression keeps the kernel inside its
-low-memory window (`_end <= 0x7F000`) now that the image viewer adds decoders.
-`cmds-dev`/`net-cmds` still send the *raw* `cmds.pack`, which
-`ramfs_replace()` validates and installs directly.  A running board can
-replace the pack without a kernel reload:
+gzip-compressed to `cmds_pack_gz.bin` and wrapped by `mkrootfs` with a
+one-sector `NBROOT1` header (magic, version, exact `pack_bytes`, checksum,
+gzip flag).  The boot path loads the gz pack into high RAM — the legacy stage
+via INT 15h AH=87h, the UEFI loader via an embedded copy, kexec via its
+staging buffer — and `ramfs_init()` inflates it into page-allocated memory and
+mounts it read-only at `/ramfs`.  The kernel image no longer contains it, so
+the pack can grow without touching `_end <= 0x7F000`.
+
+`rootfs.bin` is the floppy append (header + pack); `cmds.img` is the 8 MB
+rootfs partition image written to USB partition 2.  `cmds-dev`/`net-cmds`
+send the *raw* `cmds.pack`, which `ramfs_replace()` validates and installs
+directly in the running kernel.  A running board can replace the pack without
+a kernel reload:
 
 ```sh
 make cmds-dev       # serial:  ./kexec_send  --payload $(SERIAL_DEV) $(SERIAL_BAUD) cmds.pack
@@ -658,10 +704,12 @@ The pack is received into page-allocated memory (magic "KXNP" over serial,
 the same stop-and-wait protocol with a "KXNP" header over UDP), validated
 *before* the swap, so a truncated or corrupt transfer leaves the running
 commands untouched; a good one is installed by `ramfs_replace()` and the
-previous received copy is freed.  Existing processes are unaffected (they
-already loaded their ELF), and the next spawn picks up the new files.  The
-WM idle loop auto-detects both the kernel (KXEC/KXNU) and the pack (KXNP)
-transfers, so a plain `make cmds-dev`/`make net-cmds` is enough.
+previous heap copy is freed.  Existing processes are unaffected (they already
+loaded their ELF), and the next spawn picks up the new files.  The WM idle
+loop auto-detects the kernel/rootfs (KXE2/KXN2) and the runtime pack (KXNP)
+transfers, so a plain `make cmds-dev`/`make net-cmds` is enough.  Runtime
+updates are RAM-only; a cold boot uses the flashed rootfs (USB partition 2 or
+the floppy append), so reflash after changing the bundle.
 
 ## Network hot-reload (net-dev) workflow
 
@@ -669,17 +717,24 @@ For boards without a serial console (the ASUS AT3IONT-I), the same reload runs
 over the network instead:
 
 ```sh
-make net-dev                    # pushes kernel.bin to BOARD_IP (default 192.168.178.240)
+make net-dev                    # kernel + rootfs to BOARD_IP (default 192.168.178.240)
 make net-dev BOARD_IP=192.168.178.42
+make kernel-net-dev             # kernel only; keeps the running rootfs
 ```
 
 The board must be idle at a shell prompt: the WM loop polls the NIC, answers
-ARP/ICMP for its IP, and auto-detects the `"KXNU"` header datagram (UDP port
-19288).  The host tool (`netkexec_send.c`) then does a stop-and-wait transfer:
-the board acks every datagram with the next expected offset, verifies the
-32-bit checksum and jumps.  ~130 KB takes about a second on a wired LAN.
-While a net command owns the receiver (`netrecv`/`ping`/`netloop`)
-or a kexec is in progress, the auto-detect pauses.
+ARP/ICMP for its IP, and auto-detects the `"KXN2"` header datagram (UDP port
+19288).  The header is `magic, entry, kernel_size, pack_size, checksum`; the
+host tool (`netkexec_send.c`) sends the kernel then the gz rootfs as one
+stop-and-wait stream, the board acks every datagram with the next expected
+offset, verifies the 32-bit checksum, writes boot info for the staging tail
+and jumps.  `pack_size == 0` keeps the running rootfs.  ~480 KB takes a
+couple of seconds on a wired LAN.  While a net command owns the receiver
+(`netrecv`/`ping`/`netloop`) or a kexec is in progress, the auto-detect pauses.
+
+The old `KXNU`/`KXEC` magics are deliberately not understood by this kernel
+(and it uses `KXN2`/`KXE2`), so a board still running a pre-rootfs build must
+be flashed (or bootstrapped) once; the new `make net-dev` is then atomic.
 
 The board's address comes from DHCP when a server is present (the DHCP client
 runs at boot; `netinfo` shows ip/mask/gateway and the lease), so use
@@ -938,10 +993,10 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
    the USB tree printer) are the last non-privileged commands still living in
    the kernel, plus the privileged ones (`peek`, `poke`, `dbg`, `serial`,
    `kexec`, `reboot`, `poweroff`, `format`, `run`/`prog`/`space`, `ps`/`kill`).
-9. Every ring-3 command is a full ELF whose LOAD segment starts at file
-   offset 0x1000 (the linker's page alignment), so ~4 KB of each ~8 KB blob is
-   padding: 25 commands carry ~100 KB of it.  Linking them with
-   `-z max-page-size=0x100` would remove most of that.
+9. The rootfs pack is inflated into one contiguous page-allocated run at
+   boot, capped by `RAMFS_MAX_DECOMPRESSED` (8 MB); a larger pack fails to
+   load (the USB rootfs partition is 8 MB, so this only bites if the cap is
+   raised without a bigger partition).
 10. SMP is **ACPI/MADT only**: no Intel MP-table fallback, so a Pentium II SMP
     board that exposes only MP tables stays UP (deliberate; there is no test
     hardware).
@@ -992,9 +1047,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     charged against the 128 MB identity-mapped window.
 
 22. The TCP backlog's early-request buffers cost 8 KB of kernel BSS
-    (8 slots x 1 KB), and the FAT listing cache another 5 KB; `_end` is
-    within roughly 30 KB of the `0x7F000` wall.  Adding more kernel buffers
-    or commands should be paired with reclaiming space elsewhere.
+    (8 slots x 1 KB), and the FAT listing cache another 5 KB.  The command
+    pack is out of the kernel now, so `_end` keeps ~130 KB of headroom below
+    the `0x7F000` wall; the remaining limit is kernel code/buffers only.
 
 23. Browser limits: no JavaScript, forms, flex/grid, CSS custom properties
     (`var(--...)`), tables with column widths, absolute/fixed positioning,
@@ -1041,5 +1096,5 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     IOAPIC setup: OVMF delivers the ISA IRQs through the 8259 path, but a
     platform that routes them through the IOAPIC may need a mask-all step after
     ACPI init.  UEFI runtime services are not used, so `reboot`/`poweroff` still
-    use the legacy ports.  The hybrid image is 72 MB because the ESP is a
-    third partition after the untouched 64 MB legacy layout.
+    use the legacy ports.  The image is 80 MB: the 64 MB kernel partition,
+    the 8 MB rootfs partition and the 8 MB ESP.

+ 96 - 33
Makefile

@@ -24,7 +24,7 @@ EFI_LDFLAGS = -nostdlib -Wl,--subsystem,10 -Wl,-e,efi_main \
               -Wl,--image-base,0x0 -Wl,--dynamicbase \
               -Wl,--enable-reloc-section -Wl,--no-insert-timestamp
 
-.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-xhci run-xhci-asix run-net run-e1000 run-ahci test-usb run-serial floppy usb uefi run-uefi test-uefi flash space prog bootstrap dev net-dev cmds-dev net-cmds browser-dev gallery-dev netmon monitor test-crypto test-images test-images-asan
+.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-xhci run-xhci-asix run-net run-e1000 run-ahci test-usb run-serial floppy usb uefi run-uefi test-uefi flash space prog bootstrap dev net-dev cmds-dev net-cmds browser-dev gallery-dev netmon monitor test-crypto test-images test-images-asan kernel-dev kernel-net-dev rootfs
 
 all: floppy.img usb.img final.bin root.img
 
@@ -142,7 +142,7 @@ mtrr.o: mtrr.c mtrr.h serial.h
 gfx.o: gfx.c gfx.h mm.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-sys_gfx.o: sys_gfx.c sys_gfx.h gfx.h paging.h process.h wm.h syscall.h
+sys_gfx.o: sys_gfx.c sys_gfx.h gfx.h paging.h process.h wm.h wm_abi.h syscall.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 display.o: display.c io.h vga.h gpu.h ati.h nvidia.h radeon.h mouse.h fbtext.h
@@ -286,7 +286,7 @@ ap_tramp.bin: ap_tramp.elf
 ap_tramp_embed.o: ap_tramp.bin
 	$(LD) -m elf_i386 -r -b binary -o ap_tramp_embed.o ap_tramp.bin
 
-process.o: process.c process.h sched.h io.h kmalloc.h mm.h serial.h smp.h idle.h timer.h
+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
 	$(CC) $(CFLAGS) -o $@ $<
 
 mm.o: mm.c mm.h
@@ -325,7 +325,7 @@ sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h pr
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o prog_embed.o space_embed.o inflate.o cmds_pack.o ramfs.o share.o payload.o
+kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o prog_embed.o space_embed.o inflate.o ramfs.o share.o payload.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -353,27 +353,51 @@ kernel.bin: kernel.elf
 # the 2>/dev/null below.
 # 62 = kernel_sectors field (0x3E)
 # 64 = kernel_lba field (0x40, word; LBA 64 fits in the low byte)
+# 66 = pack_lba field (0x42, dword; rootfs image LBA)
 # 26/28/32 = BPB: heads (0x1A, word), hidden sectors (0x1C, dword),
 #            total sectors 32-bit (0x20, dword)
 KERNEL_SECTORS_OFFSET = 62
 BOOT_SECTOR_START_OFFSET = 64
+PACK_LBA_OFFSET = 66
 BPB_HEADS_OFFSET = 26
 BPB_HIDDEN_OFFSET = 28
 BPB_TOTAL_OFFSET = 32
 
-floppy.img: boot.bin kernel.bin
+# USB rootfs partition (type 0x7F) and UEFI ESP (type 0xEF).
+ROOTFS_LBA     = 131072
+ROOTFS_SECTORS = 16384
+ESP_LBA        = 147456
+
+# Patch a little-endian 16/32-bit value into a boot-sector copy.  Usage:
+#   $(call patch16,file,offset,value)
+# (shell snippets are inlined below because make functions cannot easily
+# expand `$$(...)` arithmetic; the helpers keep the rules readable.)
+patch32 = printf "$$(printf '\\x%02x\\x%02x\\x%02x\\x%02x' \
+	$$(( ( ($(2)) >> 0) & 0xFF )) $$(( ( ($(2)) >> 8) & 0xFF )) \
+	$$(( ( ($(2)) >> 16) & 0xFF )) $$(( ( ($(2)) >> 24) & 0xFF )))" | \
+	dd of=$(1) bs=1 seek=$(3) conv=notrunc 2>/dev/null
+patch16 = printf "$$(printf '\\x%02x\\x%02x' \
+	$$(( ( ($(2)) >> 0) & 0xFF )) $$(( ( ($(2)) >> 8) & 0xFF )))" | \
+	dd of=$(1) bs=1 seek=$(3) conv=notrunc 2>/dev/null
+
+floppy.img: boot.bin kernel.bin rootfs.bin
 	SECTORS=$$(( ($$(stat -c%s kernel.bin) + 511) / 512 )); \
 	cp boot.bin $@.boot.tmp; \
-	printf "$$(printf '\\x%02x\\x%02x' $$((SECTORS & 0xFF)) $$((SECTORS >> 8)))" | dd of=$@.boot.tmp bs=1 seek=$(KERNEL_SECTORS_OFFSET) conv=notrunc 2>/dev/null; \
+	$(call patch16,$@.boot.tmp,$$SECTORS,$(KERNEL_SECTORS_OFFSET)); \
+	$(call patch32,$@.boot.tmp,1 + SECTORS,$(PACK_LBA_OFFSET)); \
 	cat $@.boot.tmp kernel.bin > $@; \
+	PAD=$$(( SECTORS * 512 - $$(stat -c%s kernel.bin) )); \
+	dd if=/dev/zero bs=1 count=$$PAD 2>/dev/null >> $@; \
+	cat rootfs.bin >> $@; \
 	truncate -s 1474560 $@; \
 	rm -f $@.boot.tmp
 
-usb.img: mbr.bin boot.bin kernel.bin
+usb.img: mbr.bin boot.bin kernel.bin cmds.img
 	SECTORS=$$(( ($$(stat -c%s kernel.bin) + 511) / 512 )); \
 	cp boot.bin $@.boot.tmp; \
-	printf "$$(printf '\\x%02x\\x%02x' $$((SECTORS & 0xFF)) $$((SECTORS >> 8)))" | dd of=$@.boot.tmp bs=1 seek=$(KERNEL_SECTORS_OFFSET) conv=notrunc 2>/dev/null; \
+	$(call patch16,$@.boot.tmp,$$SECTORS,$(KERNEL_SECTORS_OFFSET)); \
 	printf '\x40' | dd of=$@.boot.tmp bs=1 seek=$(BOOT_SECTOR_START_OFFSET) conv=notrunc 2>/dev/null; \
+	$(call patch32,$@.boot.tmp,$(ROOTFS_LBA),$(PACK_LBA_OFFSET)); \
 	printf '\xff\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_HEADS_OFFSET) conv=notrunc 2>/dev/null; \
 	printf '\x3f\x00\x00\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_HIDDEN_OFFSET) conv=notrunc 2>/dev/null; \
 	printf '\x08\xb7\x01\x00' | dd of=$@.boot.tmp bs=1 seek=$(BPB_TOTAL_OFFSET) conv=notrunc 2>/dev/null; \
@@ -381,16 +405,24 @@ usb.img: mbr.bin boot.bin kernel.bin
 	dd if=/dev/zero bs=512 count=62 2>/dev/null >> $@; \
 	cat $@.boot.tmp kernel.bin >> $@; \
 	truncate -s 64M $@; \
+	dd if=cmds.img of=$@ bs=512 seek=$(ROOTFS_LBA) conv=notrunc 2>/dev/null; \
+	truncate -s 72M $@; \
+	printf '\x00\xfe\xff\xff\x7f\xfe\xff\xff\x00\x00\x02\x00\x00\x40\x00\x00' | \
+		dd of=$@ bs=1 seek=462 conv=notrunc 2>/dev/null; \
 	rm -f $@.boot.tmp
 
 image.bin: floppy.img
 	cp floppy.img image.bin
 
-final.bin: boot.bin kernel.bin
+final.bin: boot.bin kernel.bin rootfs.bin
 	SECTORS=$$(( ($$(stat -c%s kernel.bin) + 511) / 512 )); \
 	cp boot.bin $@.boot.tmp; \
-	printf "$$(printf '\\x%02x\\x%02x' $$((SECTORS & 0xFF)) $$((SECTORS >> 8)))" | dd of=$@.boot.tmp bs=1 seek=$(KERNEL_SECTORS_OFFSET) conv=notrunc 2>/dev/null; \
+	$(call patch16,$@.boot.tmp,$$SECTORS,$(KERNEL_SECTORS_OFFSET)); \
+	$(call patch32,$@.boot.tmp,1 + SECTORS,$(PACK_LBA_OFFSET)); \
 	cat $@.boot.tmp kernel.bin > $@; \
+	PAD=$$(( SECTORS * 512 - $$(stat -c%s kernel.bin) )); \
+	dd if=/dev/zero bs=1 count=$$PAD 2>/dev/null >> $@; \
+	cat rootfs.bin >> $@; \
 	rm -f $@.boot.tmp
 
 # ---- UEFI loader and dual-boot USB image -------------------------------
@@ -409,7 +441,7 @@ efi/efi_entry.h: kernel.elf
 efi/main.o: efi/main.c efi/efi.h bootinfo.h vga.h efi/efi_entry.h
 	$(EFI_CC) $(EFI_CFLAGS) -c -o $@ $<
 
-efi/payload.o: efi/payload.S kernel.bin efi/tramp.bin
+efi/payload.o: efi/payload.S kernel.bin cmds_pack_gz.bin efi/tramp.bin
 	$(EFI_AS) --64 -o $@ $<
 
 efi/tramp.o: efi/tramp.S
@@ -424,7 +456,7 @@ efi/tramp.bin: efi/tramp.elf
 BOOTX64.EFI: efi/main.o efi/payload.o
 	$(EFI_CC) $(EFI_LDFLAGS) -o $@ efi/main.o efi/payload.o
 
-ESP_LBA     = 131072
+ESP_LBA     = 147456
 ESP_SECTORS = 16384
 
 esp.img: BOOTX64.EFI
@@ -442,7 +474,9 @@ usb-efi.img: usb.img esp.img
 	# the kernel at LBA 64.  Clear BPB FATSize16 so the firmware rejects the
 	# fake volume; boot.S and the BIOS never read that field.
 	printf '\x00\x00' | dd of=$@ bs=1 seek=$$((63 * 512 + 0x16)) conv=notrunc 2>/dev/null
-	printf '\x00\x28\x21\x08\xef\x2d\x24\x09\x00\x00\x02\x00\x00\x40\x00\x00' | \
+	# Partition 3: ESP at LBA 147456, type 0xEF, 8 MB (partition 2 is the
+	# rootfs at LBA 131072 written by usb.img).
+	printf '\x00\xfe\xff\xff\xef\xfe\xff\xff\x00\x40\x02\x00\x00\x40\x00\x00' | \
 		dd of=$@ bs=1 seek=478 conv=notrunc 2>/dev/null
 	dd if=esp.img of=$@ bs=512 seek=$(ESP_LBA) conv=notrunc 2>/dev/null
 	truncate -s $$(( ($(ESP_LBA) + $(ESP_SECTORS)) * 512 )) $@
@@ -555,9 +589,9 @@ ucmd_mines.elf: ucmd_mines.o ngui.o font_8x16.o ucmd.ld
 	$(LD) -T ucmd.ld -z max-page-size=0x100 -nostdlib -m elf_i386 -o $@ $(filter %.o,$^)
 	$(OBJCOPY) --strip-all $@ $@
 
-# gallery: a live showcase of every ngui widget and primitive.  It is a disk
-# app because the embedded command pack sits close to its low-memory wall;
-# ucmd_gallery.c (in UCMDS) is the tiny PATH launcher for it.
+# gallery: a live showcase of every ngui widget and primitive.  The real ELF
+# ships in the rootfs pack as bin/gallery.app; ucmd_gallery.c (in UCMDS) is
+# the tiny PATH launcher (which also accepts /share/GALLERY).
 gallery.o: gallery.c ucmd_lib.h syscall.h wm_abi.h ngui.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -652,21 +686,35 @@ test-images-asan: $(IMGTEST_SRCS) $(IMGTEST_DEPS)
 mkpack: mkpack.c
 	gcc -O2 -o $@ $<
 
-# Command pack: the ring-3 command ELFs plus hello, embedded in the kernel and
-# mounted at /ramfs (bin/).  See mkpack.c and ramfs.h.
+# Command pack (the system rootfs): the ring-3 command ELFs plus hello and the
+# browser/gallery apps, packed into bin/<name>.  See mkpack.c, rootfs.h and
+# ramfs.h.
 PACK_FILES = $(foreach f,$(UCMDS),bin/$(patsubst ucmd_%,%,$(f))=$(f).elf) \
-             bin/hello=hello.elf
+             bin/hello=hello.elf \
+             bin/browser.app=browser.elf \
+             bin/gallery.app=gallery.elf
 
-cmds.pack: $(addsuffix .elf,$(UCMDS)) hello.elf mkpack
+cmds.pack: $(addsuffix .elf,$(UCMDS)) hello.elf browser.elf gallery.elf mkpack
 	./mkpack $@ $(PACK_FILES)
 
-# The embedded pack is gzip-compressed to stay inside the kernel's low-memory
-# window; the raw cmds.pack is still what cmds-dev/net-cmds send.
+# The boot-time rootfs is gzip-compressed; the raw cmds.pack is still what
+# cmds-dev/net-cmds send for a runtime replacement.
 cmds_pack_gz.bin: cmds.pack
 	gzip -9 -n -c $< > $@
 
-cmds_pack.o: cmds_pack_gz.bin
-	$(LD) -m elf_i386 -r -b binary -o cmds_pack.o cmds_pack_gz.bin
+mkrootfs: mkrootfs.c rootfs.h
+	gcc -O2 -o $@ $<
+
+# Rootfs image: one NBROOT1 header sector + the gzip pack (rootfs.h).
+# rootfs.bin is appended directly to the floppy images; cmds.img is the
+# 8 MB image flashed into the USB rootfs partition.
+rootfs.bin: cmds_pack_gz.bin mkrootfs
+	./mkrootfs $@ cmds_pack_gz.bin
+
+rootfs: cmds.img
+
+cmds.img: cmds_pack_gz.bin mkrootfs
+	./mkrootfs $@ cmds_pack_gz.bin $$(( $(ROOTFS_SECTORS) * 512 ))
 
 payload.o: payload.c payload.h io.h mm.h ramfs.h serial.h vfs.h
 	$(CC) $(CFLAGS) -o $@ $<
@@ -862,10 +910,17 @@ netmonitor: netmonitor.c
 
 bootstrap: bootstrap.bin
 
-dev: kernel.bin kexec_send
+dev: kernel.bin cmds_pack_gz.bin kexec_send
+	@entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
+	if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
+	echo "Sending kernel.bin + cmds_pack_gz.bin (entry 0x$$entry) via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."; \
+	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin cmds_pack_gz.bin
+
+# Kernel-only fast path: the board keeps its running rootfs.
+kernel-dev: kernel.bin kexec_send
 	@entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
 	if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
-	echo "Sending kernel.bin (entry 0x$$entry) via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."; \
+	echo "Sending kernel.bin only (entry 0x$$entry) via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."; \
 	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
 
 # Push just the command pack (cmds.pack) to a running board: no kernel
@@ -879,14 +934,15 @@ net-cmds: cmds.pack netkexec_send
 	@echo "Sending cmds.pack to $(BOARD_IP):$(NETKEXEC_PORT)..."
 	./netkexec_send --payload $(BOARD_IP) $(NETKEXEC_PORT) cmds.pack
 
-# The browser is a disk app, not part of the pack.  Upload it to the board's
-# writable /share (the board must be running `websrv`; stop it before browsing
-# HTTP, since the WebDAV server owns the NIC).
+# Development override: upload the real browser to the board's writable
+# /share; the bin/browser launcher prefers it over the pack copy.  The board
+# must be running `websrv`; stop it before browsing HTTP, since the WebDAV
+# server owns the NIC.
 browser-dev: browser.elf
 	@echo "Uploading browser.elf to http://$(BOARD_IP):8080/share/BROWSER..."
 	curl -f -T browser.elf http://$(BOARD_IP):8080/share/BROWSER
 
-# gallery follows the same disk-app model as the browser.
+# gallery follows the same development-override model.
 gallery-dev: gallery.elf
 	@echo "Uploading gallery.elf to http://$(BOARD_IP):8080/share/GALLERY..."
 	curl -f -T gallery.elf http://$(BOARD_IP):8080/share/GALLERY
@@ -903,11 +959,18 @@ BOARD_IP ?= 192.168.178.240
 NETLOG_PORT ?= 19290
 NETKEXEC_PORT ?= 19288
 
-net-dev: kernel.bin netkexec_send
+net-dev: kernel.bin cmds_pack_gz.bin netkexec_send
+	@entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
+	if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
+	echo "Sending kernel.bin + cmds_pack_gz.bin (entry 0x$$entry) to $(BOARD_IP):$(NETKEXEC_PORT)..."; \
+	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin cmds_pack_gz.bin
+
+# Kernel-only fast path: the board keeps its running rootfs.
+kernel-net-dev: kernel.bin netkexec_send
 	@entry=$$(nm kernel.elf | awk '/ T kernel_entry$$/{print $$1}'); \
 	if [ -z "$$entry" ]; then echo "error: kernel_entry symbol not found"; exit 1; fi; \
-	echo "Sending kernel.bin (entry 0x$$entry) to $(BOARD_IP):$(NETKEXEC_PORT)..."; \
+	echo "Sending kernel.bin only (entry 0x$$entry) to $(BOARD_IP):$(NETKEXEC_PORT)..."; \
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o cmds.pack cmds_pack_gz.bin cmds_pack.o 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
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h

+ 7 - 0
boot.S

@@ -40,7 +40,14 @@ kernel_sectors:
 kernel_lba:
 	.word 1
 
+	/* Rootfs image LBA (see rootfs.h).  Patched by the image builders:
+	 * floppy/final.bin point it just past the padded kernel, usb.img at
+	 * its rootfs partition. */
 	.org 0x42
+pack_lba:
+	.long 1
+
+	.org 0x46
 loader_start:
 	xorw  %ax, %ax
 	movw  %ax, %ds

+ 24 - 9
bootinfo.h

@@ -1,18 +1,21 @@
 /* Boot information block at 0x500.
  *
- * The legacy VESA stage (vesa_stage.S) does not fill this in; it clears the
- * magic so a BIOS boot takes exactly the old code paths.  The UEFI loader
- * fills it in with the ACPI RSDP address, the boot source and the GOP pixel
- * order.  A checksum over the eight dwords (with the checksum field set so
- * the sum is zero) keeps stale low memory from being mistaken for a valid
- * block after a kexec or a legacy boot.
+ * Written by the legacy VESA stage, the UEFI loader or the kexec receiver,
+ * and read by the kernel very early.  It carries the ACPI RSDP, the boot
+ * source / GOP pixel order and the physical location of the command-pack
+ * rootfs that the boot path copied into high memory (see ramfs.c).
+ *
+ * A checksum over the twelve dwords (with the checksum field set so the sum
+ * is zero) keeps stale low memory from being mistaken for a valid block
+ * after a kexec or a legacy boot.
  */
 #ifndef BOOTINFO_H
 #define BOOTINFO_H
 
 #define BOOT_INFO_ADDR    0x500
 #define BOOT_INFO_MAGIC   0x4E425349u   /* "NBSI" */
-#define BOOT_INFO_VERSION 1u
+#define BOOT_INFO_VERSION 2u
+#define BOOT_INFO_WORDS   12u
 
 #define BOOTINFO_FLAG_EFI  0x00000001u
 #define BOOTINFO_FLAG_RGBX 0x00000002u  /* GOP 32bpp byte order R,G,B,X */
@@ -23,7 +26,9 @@ struct boot_info {
 	unsigned int checksum;
 	unsigned int rsdp;          /* physical ACPI RSDP address, 0 = scan */
 	unsigned int flags;
-	unsigned int reserved[3];
+	unsigned int pack_addr;     /* boot-time rootfs pack, 0 = none */
+	unsigned int pack_size;     /* exact pack bytes (gzip or raw) */
+	unsigned int reserved[5];
 } __attribute__((packed));
 
 /* Fixed low-memory pointer, hidden from -Warray-bounds like the other boot
@@ -44,7 +49,7 @@ static inline int bootinfo_valid(void)
 
 	if (p[0] != BOOT_INFO_MAGIC || p[1] != BOOT_INFO_VERSION)
 		return 0;
-	for (i = 0; i < 8; i++)
+	for (i = 0; i < BOOT_INFO_WORDS; i++)
 		sum += p[i];
 	return sum == 0;
 }
@@ -69,4 +74,14 @@ static inline unsigned int bootinfo_rsdp(void)
 	return bootinfo_valid() ? bootinfo_words()[3] : 0;
 }
 
+static inline unsigned int bootinfo_pack_addr(void)
+{
+	return bootinfo_valid() ? bootinfo_words()[5] : 0;
+}
+
+static inline unsigned int bootinfo_pack_size(void)
+{
+	return bootinfo_valid() ? bootinfo_words()[6] : 0;
+}
+
 #endif

+ 17 - 6
bootstrap.c

@@ -8,18 +8,23 @@
  * directly from our own code without a relocated trampoline.
  *
  * Wire protocol (host -> us), after we emit 'R':
- *   4 bytes: magic "KXEC" (LE)
+ *   4 bytes: magic "KXE2" (LE)
  *   4 bytes: entry address (LE)
- *   4 bytes: size (LE)
+ *   4 bytes: kernel_size (LE)
+ *   4 bytes: pack_size (LE)      -- consumed but not stored (see below)
  *   N bytes: kernel.bin data
- *   1 byte:  checksum (sum of data bytes mod 256)
+ *   M bytes: rootfs pack data
+ *   1 byte:  checksum (sum of both payloads mod 256)
+ *
+ * The bootstrap only installs the kernel; the rootfs pack arrives with the
+ * next normal `make dev`/`make net-dev` (or `make cmds-dev`).
  */
 
 #define UART_BASE        0x3F8
 #define STAGE_ADDR       0x58000
 #define KERNEL_LOAD_ADDR 0x7E00
 #define BOOT_STACK       0x49800
-#define MAGIC            0x4345584B /* "KXEC" little-endian */
+#define MAGIC            0x3245584B /* "KXE2" little-endian */
 #define MAX_SIZE         0x3C200
 
 static inline void outb(unsigned short port, unsigned char v)
@@ -104,7 +109,7 @@ static void setup_flat_gdt(void)
 void _start(void)
 {
 	unsigned char c, sum = 0, chk;
-	unsigned int magic, entry, size, i;
+	unsigned int magic, entry, size, pack_size, i;
 
 	/* Stop the PIT (and all IRQs) so the kernel's serial_poll stops
 	 * competing with us for UART bytes. */
@@ -129,8 +134,10 @@ void _start(void)
 	magic = read32le();
 	entry = read32le();
 	size  = read32le();
+	pack_size = read32le();
 
-	if (magic != MAGIC || size == 0 || size > MAX_SIZE) {
+	if (magic != MAGIC || size == 0 || size > MAX_SIZE ||
+	    pack_size > 0x400000) {
 		outb(UART_BASE + 0, 'E');
 		halt();
 	}
@@ -141,6 +148,10 @@ void _start(void)
 		sum += c;
 	}
 
+	/* Consume the rootfs bytes; the kernel gets them on the next reload. */
+	for (i = 0; i < pack_size; i++)
+		sum += read_byte();
+
 	chk = read_byte();
 	if (chk != sum) {
 		outb(UART_BASE + 0, 'E');

+ 71 - 10
efi/main.c

@@ -33,6 +33,8 @@
 
 extern unsigned char kernel_image_start[];
 extern unsigned char kernel_image_end[];
+extern unsigned char pack_image_start[];
+extern unsigned char pack_image_end[];
 extern unsigned char tramp_blob_start[];
 extern unsigned char tramp_blob_end[];
 
@@ -433,34 +435,84 @@ static void build_e820(EFI_MEMORY_DESCRIPTOR *map, UINTN map_size,
 
 /* ---- boot structures --------------------------------------------------- */
 
-static void write_bootinfo(UINT32 rsdp, int have_fb, int rgbx)
+/* Copy the embedded rootfs pack to the top of RAM (below the 128 MB
+ * identity window and above the 1..9 MB VGA shadow reserve), matching the
+ * legacy stage's placement.  Returns the address, or 0 when it does not fit.
+ * Runs after ExitBootServices, so only plain memory accesses. */
+static UINT32 copy_pack(UINT32 size)
+{
+	volatile UINT32 *map = (volatile UINT32 *)E820_ADDR;
+	volatile struct e820_entry_disk *entries =
+		(volatile struct e820_entry_disk *)(E820_ADDR + 8);
+	UINT32 need, count, i, best = 0;
+	UINT32 p;
+
+	if (size == 0 || size > 0x400000u)
+		return 0;
+	need = (size + PAGE_SIZE_ - 1) & ~(PAGE_SIZE_ - 1);
+	if (map[0] != 0xE820E820u)
+		return 0;
+	count = map[1];
+	if (count > 32)
+		count = 32;
+
+	for (i = 0; i < count; i++) {
+		UINT64 base = entries[i].base;
+		UINT64 end = base + entries[i].length;
+		UINT32 a;
+
+		if (entries[i].type != 1 || base >= MAX_PHYS)
+			continue;
+		if (end > MAX_PHYS)
+			end = MAX_PHYS;
+		if (end < base + need)
+			continue;
+		a = (UINT32)(end - need) & ~(PAGE_SIZE_ - 1);
+		if (a < 0x900000u || a <= best)
+			continue;
+		best = a;
+	}
+	if (!best)
+		return 0;
+
+	for (p = 0; p < size; p++)
+		((UINT8 *)(UINTN)best)[p] = pack_image_start[p];
+	return best;
+}
+
+static void write_bootinfo(UINT32 rsdp, int have_fb, int rgbx,
+			   UINT32 pack_addr, UINT32 pack_size)
 {
 	volatile struct boot_info *bi =
 		(volatile struct boot_info *)BOOT_INFO_ADDR;
-	UINT32 words[8];
+	UINT32 words[BOOT_INFO_WORDS];
 	unsigned int i;
 	UINT32 sum = 0;
 
 	__asm__ volatile ("" : "+r"(bi));
 
+	for (i = 0; i < BOOT_INFO_WORDS; i++)
+		words[i] = 0;
 	words[0] = BOOT_INFO_MAGIC;
 	words[1] = BOOT_INFO_VERSION;
-	words[2] = 0;
 	words[3] = rsdp;
 	words[4] = BOOTINFO_FLAG_EFI |
 		((have_fb && rgbx) ? BOOTINFO_FLAG_RGBX : 0);
-	words[5] = words[6] = words[7] = 0;
-	for (i = 0; i < 8; i++)
+	words[5] = pack_addr;
+	words[6] = pack_size;
+	for (i = 0; i < BOOT_INFO_WORDS; i++)
 		sum += words[i];
-	words[2] = 0u - sum;   /* checksum over the eight dwords is zero */
+	words[2] = 0u - sum;   /* the sum over the block is zero */
 
 	bi->magic = words[0];
 	bi->version = words[1];
 	bi->checksum = words[2];
 	bi->rsdp = words[3];
 	bi->flags = words[4];
-	for (i = 0; i < 3; i++)
-		bi->reserved[i] = words[5 + i];
+	bi->pack_addr = words[5];
+	bi->pack_size = words[6];
+	for (i = 0; i < 5; i++)
+		bi->reserved[i] = words[7 + i];
 }
 
 static void write_fbinfo(const struct fb_saved *fb)
@@ -524,6 +576,7 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
 	UINT32 desc_ver = 0;
 	UINT32 rsdp;
 	UINT32 kernel_size;
+	UINT32 pack_addr = 0, pack_size;
 	UINTN pages, i, tries;
 	EFI_STATUS status;
 
@@ -539,11 +592,14 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
 	kernel_size = (UINT32)(kernel_image_end - kernel_image_start);
 	if (kernel_size == 0 || kernel_size > KERNEL_MAX)
 		fail_before(st, "kernel image size invalid");
+	pack_size = (UINT32)(pack_image_end - pack_image_start);
 	com1_puts("nebos-efi: kernel ");
 	com1_dec(kernel_size);
 	com1_puts(" bytes, entry 0x");
 	com1_hex((UINT64)KERNEL_ENTRY, 8);
-	com1_puts("\n");
+	com1_puts(", rootfs ");
+	com1_dec(pack_size);
+	com1_puts(" bytes\n");
 
 	/* Video: prefer BGRX (the byte order the kernel's shadow buffer uses);
 	 * RGBX is handled by a swizzling flush. */
@@ -623,7 +679,12 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
 		fail_after("memory map is empty");
 	build_e820((EFI_MEMORY_DESCRIPTOR *)memmap_buf, map_size, desc_size,
 		   &fb);
-	write_bootinfo(rsdp, fb.valid, fb.rgbx);
+	pack_addr = copy_pack(pack_size);
+	com1_puts("nebos-efi: rootfs at 0x");
+	com1_hex(pack_addr, 8);
+	com1_puts("\n");
+	write_bootinfo(rsdp, fb.valid, fb.rgbx, pack_addr,
+		       pack_addr ? pack_size : 0);
 	write_fbinfo(&fb);
 	copy_trampoline();
 

+ 9 - 1
efi/payload.S

@@ -1,4 +1,5 @@
-/* Flat kernel image and the 0x800 trampoline, embedded in BOOTX64.EFI. */
+/* Flat kernel image, the boot rootfs pack and the 0x800 trampoline, embedded
+ * in BOOTX64.EFI. */
 
 .section .data,"dw"
 .globl kernel_image_start
@@ -8,6 +9,13 @@ kernel_image_start:
 .incbin "kernel.bin"
 kernel_image_end:
 
+.globl pack_image_start
+.globl pack_image_end
+.align 16
+pack_image_start:
+.incbin "cmds_pack_gz.bin"
+pack_image_end:
+
 .align 16
 .globl tramp_blob_start
 .globl tramp_blob_end

+ 28 - 16
kernel.c

@@ -3,6 +3,7 @@
 #include "acpi.h"
 #include "asix.h"
 #include "bootcon.h"
+#include "bootinfo.h"
 #include "cpu.h"
 #include "idle.h"
 #include "io.h"
@@ -399,27 +400,38 @@ void kernel_main(void)
 	boot_trace("vga");
 	bootcon_fb_info();
 
-	/* The command pack: read-only, mounted at /ramfs (see ramfs.c).  This
-	 * happens before the block-device scan because the WM and terminal are
-	 * loaded from /ramfs, not from a disk. */
+	/* The command pack: read-only, mounted at /ramfs (see ramfs.c).  The
+	 * boot path copied it into high RAM (the legacy stage, the UEFI loader
+	 * or kexec); ramfs_init takes its own copy so the boot region can be
+	 * released.  The mount is created even without a pack, so a running
+	 * board can be recovered with `make cmds-dev`/`make net-cmds`. */
 	{
-		extern unsigned char _binary_cmds_pack_gz_bin_start[];
-		extern unsigned char _binary_cmds_pack_gz_bin_end[];
-		unsigned int pack_len =
-			(unsigned int)(_binary_cmds_pack_gz_bin_end -
-				       _binary_cmds_pack_gz_bin_start);
-		if (ramfs_init(_binary_cmds_pack_gz_bin_start, pack_len) != 0)
-			serial_puts(SERIAL_COM1, "ramfs: bad pack\n");
+		unsigned int pack_addr = bootinfo_pack_addr();
+		unsigned int pack_len = bootinfo_pack_size();
+		int ok = 0;
+
+		if (!pack_addr || !pack_len)
+			serial_puts(SERIAL_COM1, "ramfs: no boot pack\n");
 		else {
-			serial_puts(SERIAL_COM1, "ramfs: pack ok, ");
-			serial_putdec(SERIAL_COM1, ramfs_count());
-			serial_puts(SERIAL_COM1, " files, ");
+			serial_puts(SERIAL_COM1, "ramfs: boot pack at ");
+			serial_puthex(SERIAL_COM1, pack_addr);
+			serial_puts(SERIAL_COM1, ", ");
 			serial_putdec(SERIAL_COM1, pack_len);
-			serial_puts(SERIAL_COM1, " -> ");
-			serial_putdec(SERIAL_COM1, ramfs_pack_size());
 			serial_puts(SERIAL_COM1, " bytes\n");
-			vfs_mount_ramfs();
+			ok = ramfs_init((const void *)pack_addr, pack_len) == 0;
+			if (!ok)
+				serial_puts(SERIAL_COM1, "ramfs: bad boot pack\n");
+			else {
+				serial_puts(SERIAL_COM1, "ramfs: pack ok, ");
+				serial_putdec(SERIAL_COM1, ramfs_count());
+				serial_puts(SERIAL_COM1, " files, ");
+				serial_putdec(SERIAL_COM1, ramfs_pack_size());
+				serial_puts(SERIAL_COM1, " bytes\n");
+			}
+			mm_free_range(pack_addr, pack_len);
 		}
+		(void)ok;
+		vfs_mount_ramfs();
 	}
 	boot_trace("ramfs");
 	boot_val("ramfs files", ramfs_count());

+ 95 - 27
kexec.c

@@ -1,4 +1,4 @@
-/* kexec: receive a new kernel.bin over serial and jump into it.
+/* kexec: receive a new kernel + rootfs pack over serial and jump into it.
  *
  * C-light reload: we do NOT return to real mode or re-run the VESA stage.
  * Instead we reuse the current framebuffer (the boot FB info at 0x600 and the
@@ -14,20 +14,24 @@
  * command is kept as a fallback that waits for the same image.
  *
  * Wire protocol (host -> us):
- *   4 bytes: magic "KXEC" (LE)
+ *   4 bytes: magic "KXE2" (LE)    -- new magic: old kernels ignore it
  *   4 bytes: entry address (LE)   -- new kernel_entry VMA
- *   4 bytes: size (LE)            -- kernel.bin byte count
+ *   4 bytes: kernel_size (LE)     -- kernel.bin byte count
+ *   4 bytes: pack_size (LE)       -- rootfs gzip byte count (0 = keep running)
  *   N bytes: kernel.bin data
- *   1 byte:  checksum (sum of data bytes mod 256)
+ *   M bytes: rootfs pack data
+ *   1 byte:  checksum (sum of both payloads mod 256)
  */
 
 #include "kexec.h"
+#include "bootinfo.h"
 #include "io.h"
 #include "mm.h"
+#include "ramfs.h"
 #include "serial.h"
 #include "smp.h"
 
-#define KEXEC_MAGIC 0x4345584B /* "KXEC" little-endian */
+#define KEXEC_MAGIC 0x3245584B /* "KXE2" little-endian */
 
 /* Staging buffer for the received image: allocated from the frame allocator
  * when the header arrives, so no fixed low-memory region is needed.  It sits
@@ -73,7 +77,7 @@ int kexec_pending(void)
 {
 	unsigned char b, x, e, c;
 
-	/* Scan for the "KXEC" magic, draining any leading bytes that can't begin
+	/* Scan for the "KXE2" magic, draining any leading bytes that can't begin
 	 * it. This self-heals after a stray/partial/overflowed transfer, so stale
 	 * bytes in the ring buffer can never permanently block detection. */
 	for (;;) {
@@ -86,8 +90,8 @@ int kexec_pending(void)
 		/* First byte is 'K'. Report pending only once all 4 bytes are here. */
 		if (serial_ring_peek(1, &x) && serial_ring_peek(2, &e) &&
 		    serial_ring_peek(3, &c)) {
-			if (x == 'X' && e == 'E' && c == 'C')
-				return 1;         /* "KXEC" found */
+			if (x == 'X' && e == 'E' && c == '2')
+				return 1;         /* "KXE2" found */
 			(void)serial_ring_getc(); /* false 'K' start, drop it */
 			continue;
 		}
@@ -111,9 +115,47 @@ static int kexec_fail(const char *msg)
 	return -1;
 }
 
-static int kexec_recv(unsigned int *entry, unsigned int *size)
+/* Publish the boot-info block the new kernel reads.  The pack source is the
+ * staging tail for a full reload, or the running ramfs for a kernel-only one;
+ * ramfs_init() takes its own copy and releases the region. */
+static void kexec_write_bootinfo(unsigned int pack_addr, unsigned int pack_size)
 {
-	unsigned int i, magic = 0, n;
+	volatile struct boot_info *bi =
+		(volatile struct boot_info *)BOOT_INFO_ADDR;
+	unsigned int words[BOOT_INFO_WORDS];
+	unsigned int rsdp = 0, flags = 0, sum = 0, i;
+
+	if (bootinfo_valid()) {
+		rsdp = bootinfo_rsdp();
+		flags = bootinfo_flags();
+	}
+	for (i = 0; i < BOOT_INFO_WORDS; i++)
+		words[i] = 0;
+	words[0] = BOOT_INFO_MAGIC;
+	words[1] = BOOT_INFO_VERSION;
+	words[3] = rsdp;
+	words[4] = flags;
+	words[5] = pack_addr;
+	words[6] = pack_size;
+	for (i = 0; i < BOOT_INFO_WORDS; i++)
+		sum += words[i];
+	words[2] = 0u - sum;
+
+	bi->magic = words[0];
+	bi->version = words[1];
+	bi->checksum = words[2];
+	bi->rsdp = words[3];
+	bi->flags = words[4];
+	bi->pack_addr = words[5];
+	bi->pack_size = words[6];
+	for (i = 0; i < 5; i++)
+		bi->reserved[i] = words[7 + i];
+}
+
+static int kexec_recv(unsigned int *entry, unsigned int *kernel_size,
+		      unsigned int *pack_size)
+{
+	unsigned int i, magic = 0, n, total;
 	unsigned char c, sum = 0, chk;
 	volatile unsigned char *dst;
 
@@ -132,22 +174,33 @@ static int kexec_recv(unsigned int *entry, unsigned int *size)
 		*entry |= (unsigned int)c << (8 * i);
 	}
 
-	*size = 0;
+	*kernel_size = 0;
 	for (i = 0; i < 4; i++) {
 		if (!serial_ring_wait(5000, &c))
-			return kexec_fail("kexec: timeout (size)\n");
-		*size |= (unsigned int)c << (8 * i);
+			return kexec_fail("kexec: timeout (kernel size)\n");
+		*kernel_size |= (unsigned int)c << (8 * i);
 	}
-	if (*size == 0 || *size > KEXEC_MAX)
+	*pack_size = 0;
+	for (i = 0; i < 4; i++) {
+		if (!serial_ring_wait(5000, &c))
+			return kexec_fail("kexec: timeout (pack size)\n");
+		*pack_size |= (unsigned int)c << (8 * i);
+	}
+
+	if (*kernel_size == 0 || *kernel_size > KEXEC_MAX ||
+	    *pack_size > KEXEC_MAX ||
+	    *kernel_size + *pack_size < *kernel_size ||
+	    *kernel_size + *pack_size > KEXEC_MAX)
 		return kexec_fail("kexec: bad size\n");
-	if (!kexec_stage_alloc(*size))
+	total = *kernel_size + *pack_size;
+	if (!kexec_stage_alloc(total))
 		return kexec_fail("kexec: out of memory (staging)\n");
 	dst = (volatile unsigned char *)kexec_stage;
 
-	kexec_total = *size;
+	kexec_total = total;
 	kexec_progress = 0;
 
-	for (n = 0; n < *size; n++) {
+	for (n = 0; n < total; n++) {
 		if (!serial_ring_wait(5000, &c)) {
 			kexec_stage_free();
 			return kexec_fail("kexec: timeout (data)\n");
@@ -174,32 +227,46 @@ static int kexec_recv(unsigned int *entry, unsigned int *size)
 
 void program_kexec_main(void)
 {
-	unsigned int entry = 0, size = 0;
+	unsigned int entry = 0, kernel_size = 0, pack_size = 0;
 
-	io_putstr("kexec: waiting for kernel image over serial...\n");
-	if (kexec_recv(&entry, &size) != 0) {
+	io_putstr("kexec: waiting for kernel/rootfs over serial...\n");
+	if (kexec_recv(&entry, &kernel_size, &pack_size) != 0) {
 		io_putstr("kexec: aborted\n");
 		kexec_busy = 0;
 		return;
 	}
 
-	io_putstr("kexec: received ");
-	io_puthex(size);
+	io_putstr("kexec: received kernel ");
+	io_puthex(kernel_size);
+	io_putstr(" bytes, rootfs ");
+	io_puthex(pack_size);
 	io_putstr(" bytes, entry ");
 	io_puthex(entry);
 	io_putstr("\n");
 
-	kexec_jump(entry, size);
+	if (pack_size)
+		kexec_jump(entry, kernel_size, kexec_stage + kernel_size,
+			   pack_size);
+	else
+		kexec_jump(entry, kernel_size, 0, 0);
 }
 
 /* Relocate the trampoline to 0x800 (it survives the kernel overwrite) and
- * jump to it with the staging/load/size/entry arguments.  Never returns. */
-void kexec_jump(unsigned int entry, unsigned int size)
+ * jump to it with the staging/load/size/entry arguments.  Never returns.
+ * A zero pack_size keeps the running rootfs. */
+void kexec_jump(unsigned int entry, unsigned int kernel_size,
+		unsigned int pack_addr, unsigned int pack_size)
 {
+	if (pack_size == 0) {
+		pack_addr = (unsigned int)ramfs_pack_base();
+		pack_size = ramfs_pack_size();
+	}
+
 	/* Park the APs first: the new kernel image is about to overwrite the
 	 * kernel they are executing.  INIT puts them back in real mode where
 	 * they wait for the new kernel's SIPI. */
 	smp_stop_aps();
+	kexec_write_bootinfo(pack_addr, pack_size);
 
 	/* Relocate the trampoline to 0x800 so it survives the kernel overwrite. */
 	{
@@ -217,10 +284,11 @@ void kexec_jump(unsigned int entry, unsigned int size)
 	/* Serialize so the freshly-written trampoline is visible to fetch. */
 	__asm__ volatile ("cpuid" ::: "eax", "ebx", "ecx", "edx");
 
-	/* Jump to the trampoline with args in eax/ebx/ecx/edx. Never returns. */
+	/* Jump to the trampoline with args in eax/ebx/ecx/edx.  Only the kernel
+	 * bytes are copied; the pack stays in the staging buffer. Never returns. */
 	__asm__ volatile (
 		"jmp *%4"
 		:
-		: "a"(kexec_stage), "b"(KEXEC_LOAD), "c"(size), "d"(entry), "r"(KEXEC_TRAMP)
+		: "a"(kexec_stage), "b"(KEXEC_LOAD), "c"(kernel_size), "d"(entry), "r"(KEXEC_TRAMP)
 		: "memory");
 }

+ 8 - 5
kexec.h

@@ -21,9 +21,12 @@ void kexec_stage_free(void);
 /* Physical base of the staging buffer holding the received image. */
 extern unsigned int kexec_stage;
 
-/* Copy the staged image (kexec_stage, `size` bytes) over the kernel region,
- * relocate the trampoline and jump to `entry`.  Never returns. */
-void kexec_jump(unsigned int entry, unsigned int size);
+/* Copy the staged kernel (`kernel_size` bytes) over the kernel region,
+ * relocate the trampoline and jump to `entry`.  `pack_addr`/`pack_size`
+ * describe the boot-time rootfs source; a zero pack_size means "keep the
+ * running ramfs".  Never returns. */
+void kexec_jump(unsigned int entry, unsigned int kernel_size,
+		unsigned int pack_addr, unsigned int pack_size);
 
 /* Non-zero while a kexec receive is in progress (prevents double-trigger). */
 extern volatile int kexec_busy;
@@ -32,8 +35,8 @@ extern volatile int kexec_busy;
 extern volatile unsigned int kexec_progress;
 extern volatile unsigned int kexec_total;
 
-/* Returns 1 if the serial ring buffer starts with the "KXEC" magic, i.e. the
- * host has begun sending a new kernel image. */
+/* Returns 1 if the serial ring buffer starts with the "KXE2" magic, i.e. the
+ * host has begun sending a new kernel/rootfs image. */
 int kexec_pending(void);
 
 #endif

+ 106 - 62
kexec_send.c

@@ -1,13 +1,19 @@
-/* Host-side sender for nebos `kexec` / bootstrap.
+/* Host-side sender for nebos `kexec` / `bootstrap` / `cmds-dev`.
  *
- * Protocol: send immediately (no handshake; the kernel auto-detects the magic):
- *   4 bytes: magic "KXEC" (LE)
+ * Kernel + rootfs protocol (serial, no handshake; the kernel auto-detects it):
+ *   4 bytes: magic "KXE2" (LE)
  *   4 bytes: entry address (LE)
- *   4 bytes: size (LE)
- *   N bytes: kernel.bin data
- *   1 byte:  checksum (sum of data bytes mod 256)
+ *   4 bytes: kernel_size (LE)
+ *   4 bytes: pack_size (LE)      -- 0 keeps the running rootfs
+ *   N bytes: kernel.bin
+ *   M bytes: rootfs pack (cmds_pack_gz.bin; may be omitted)
+ *   1 byte:  checksum (sum of both payloads mod 256)
  *
- * Usage: kexec_send <device> <baud> <entry_hex> <file>
+ * Runtime pack replacement (`--payload`) keeps the old 12-byte framing:
+ *   "KXNP", 0, pack_size, pack data, checksum.
+ *
+ * Usage: kexec_send <device> <baud> <entry_hex> <kernel> [pack]
+ *        kexec_send --payload <device> <baud> <file>
  */
 
 #include <stdio.h>
@@ -26,17 +32,46 @@ static void put32le(unsigned char *p, uint32_t v)
 	p[3] = (unsigned char)((v >> 24) & 0xFF);
 }
 
+static unsigned char *read_file(const char *path, long *size)
+{
+	FILE *fp = fopen(path, "rb");
+	unsigned char *data;
+	long n;
+
+	if (!fp) {
+		perror(path);
+		exit(1);
+	}
+	fseek(fp, 0, SEEK_END);
+	n = ftell(fp);
+	fseek(fp, 0, SEEK_SET);
+	if (n <= 0) {
+		fprintf(stderr, "%s: empty file\n", path);
+		fclose(fp);
+		exit(1);
+	}
+	data = malloc((size_t)n);
+	if (!data || fread(data, 1, (size_t)n, fp) != (size_t)n) {
+		fprintf(stderr, "%s: read failed\n", path);
+		fclose(fp);
+		free(data);
+		exit(1);
+	}
+	fclose(fp);
+	*size = n;
+	return data;
+}
+
 int main(int argc, char *argv[])
 {
 	int fd;
-	FILE *fp;
-	unsigned char *data;
-	long size;
+	unsigned char *kdata, *pdata = 0;
+	long ksize, psize = 0;
 	unsigned int cksum = 0;
-	unsigned char hdr[12];
+	unsigned char hdr[16];
 	struct termios tty;
 	uint32_t entry;
-	const char *dev, *baud, *file;
+	const char *dev, *baud, *file, *pack_file = 0;
 	int payload = 0;
 
 	if (argc > 1 && strcmp(argv[1], "--payload") == 0) {
@@ -51,55 +86,44 @@ int main(int argc, char *argv[])
 		file = argv[4];
 		entry = 0;
 	} else {
-		if (argc != 5) {
-			fprintf(stderr, "Usage: %s <device> <baud> <entry_hex> <file>\n",
-				argv[0]);
+		if (argc != 5 && argc != 6) {
+			fprintf(stderr, "Usage: %s <device> <baud> <entry_hex> "
+				"<kernel> [pack]\n", argv[0]);
 			return 1;
 		}
 		dev = argv[1];
 		baud = argv[2];
 		entry = (uint32_t)strtoul(argv[3], NULL, 16);
 		file = argv[4];
+		if (argc == 6)
+			pack_file = argv[5];
 	}
 
-	fp = fopen(file, "rb");
-	if (!fp) {
-		perror("fopen");
-		return 1;
-	}
-	fseek(fp, 0, SEEK_END);
-	size = ftell(fp);
-	fseek(fp, 0, SEEK_SET);
-
-	data = malloc((size_t)size);
-	if (!data) {
-		perror("malloc");
-		fclose(fp);
-		return 1;
-	}
-	if (fread(data, 1, (size_t)size, fp) != (size_t)size) {
-		perror("fread");
-		fclose(fp);
-		free(data);
-		return 1;
-	}
-	fclose(fp);
+	kdata = read_file(file, &ksize);
+	if (pack_file)
+		pdata = read_file(pack_file, &psize);
 
-	for (long i = 0; i < size; i++)
-		cksum += data[i];
+	for (long i = 0; i < ksize; i++)
+		cksum += kdata[i];
+	for (long i = 0; i < psize; i++)
+		cksum += pdata[i];
 	cksum &= 0xFF;
 
 	fd = open(dev, O_RDWR | O_NOCTTY);
 	if (fd < 0) {
 		perror("open");
-		free(data);
+		if (pdata)
+			free(pdata);
+		free(kdata);
 		return 1;
 	}
 
 	memset(&tty, 0, sizeof(tty));
 	if (tcgetattr(fd, &tty) != 0) {
 		perror("tcgetattr");
-		free(data);
+		if (pdata)
+			free(pdata);
+		free(kdata);
 		close(fd);
 		return 1;
 	}
@@ -115,47 +139,67 @@ int main(int argc, char *argv[])
 	tty.c_cc[VTIME] = 0;
 	if (tcsetattr(fd, TCSANOW, &tty) != 0) {
 		perror("tcsetattr");
-		free(data);
+		if (pdata)
+			free(pdata);
+		free(kdata);
 		close(fd);
 		return 1;
 	}
 
 	if (payload)
-		fprintf(stderr, "Sending %ld bytes (command pack)...\n", size);
+		fprintf(stderr, "Sending %ld bytes (command pack)...\n", ksize);
 	else
-		fprintf(stderr, "Sending %ld bytes (entry 0x%08x)...\n", size, entry);
+		fprintf(stderr, "Sending kernel %ld + rootfs %ld bytes "
+			"(entry 0x%08x)...\n", ksize, psize, entry);
 
-	memcpy(hdr, payload ? "KXNP" : "KXEC", 4);
-	put32le(hdr + 4, entry);
-	put32le(hdr + 8, (uint32_t)size);
-
-	if (write(fd, hdr, 12) != 12) {
-		perror("write header");
-		free(data);
-		close(fd);
-		return 1;
+	if (payload) {
+		memcpy(hdr, "KXNP", 4);
+		put32le(hdr + 4, 0);
+		put32le(hdr + 8, (uint32_t)ksize);
+		if (write(fd, hdr, 12) != 12) {
+			perror("write header");
+			goto fail;
+		}
+	} else {
+		memcpy(hdr, "KXE2", 4);
+		put32le(hdr + 4, entry);
+		put32le(hdr + 8, (uint32_t)ksize);
+		put32le(hdr + 12, (uint32_t)psize);
+		if (write(fd, hdr, 16) != 16) {
+			perror("write header");
+			goto fail;
+		}
 	}
 	tcdrain(fd);
 
-	if (write(fd, data, (size_t)size) != size) {
-		perror("write data");
-		free(data);
-		close(fd);
-		return 1;
+	if (write(fd, kdata, (size_t)ksize) != ksize) {
+		perror("write kernel");
+		goto fail;
+	}
+	if (pdata && write(fd, pdata, (size_t)psize) != psize) {
+		perror("write rootfs");
+		goto fail;
 	}
 	tcdrain(fd);
 
 	hdr[0] = (unsigned char)cksum;
 	if (write(fd, hdr, 1) != 1) {
 		perror("write checksum");
-		free(data);
-		close(fd);
-		return 1;
+		goto fail;
 	}
 	tcdrain(fd);
 
 	fprintf(stderr, "Done.\n");
-	free(data);
+	if (pdata)
+		free(pdata);
+	free(kdata);
 	close(fd);
 	return 0;
+
+fail:
+	if (pdata)
+		free(pdata);
+	free(kdata);
+	close(fd);
+	return 1;
 }

+ 134 - 0
mkrootfs.c

@@ -0,0 +1,134 @@
+/* mkrootfs: wrap a gzip command pack in a bootable rootfs image.
+ *
+ * Usage: mkrootfs <out.img> <pack.gz> [size]
+ *
+ * The image starts with a struct rootfs_hdr (rootfs.h) in its first 512-byte
+ * sector and the pack at byte 512.  A non-zero size pads the image to that
+ * many bytes (the USB rootfs partition); size 0 keeps it minimal (the floppy
+ * image appends the header + pack directly after the kernel).
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "rootfs.h"
+
+static void put32le(unsigned char *p, uint32_t v)
+{
+	p[0] = (unsigned char)(v & 0xFF);
+	p[1] = (unsigned char)((v >> 8) & 0xFF);
+	p[2] = (unsigned char)((v >> 16) & 0xFF);
+	p[3] = (unsigned char)((v >> 24) & 0xFF);
+}
+
+int main(int argc, char **argv)
+{
+	unsigned char hdr[ROOTFS_HDR_SIZE];
+	const char *out_path, *pack_path;
+	unsigned long size = 0, total;
+	unsigned char *pack;
+	uint32_t csum = 0;
+	FILE *f;
+	long n;
+	unsigned long i;
+
+	if (argc != 3 && argc != 4) {
+		fprintf(stderr, "usage: %s <out.img> <pack.gz> [size]\n",
+			argv[0]);
+		return 1;
+	}
+	out_path = argv[1];
+	pack_path = argv[2];
+	if (argc == 4) {
+		char *end;
+
+		size = strtoul(argv[3], &end, 0);
+		if (*end || size == 0) {
+			fprintf(stderr, "mkrootfs: bad size '%s'\n", argv[3]);
+			return 1;
+		}
+	}
+
+	f = fopen(pack_path, "rb");
+	if (!f) {
+		fprintf(stderr, "mkrootfs: %s: cannot open\n", pack_path);
+		return 1;
+	}
+	fseek(f, 0, SEEK_END);
+	n = ftell(f);
+	fseek(f, 0, SEEK_SET);
+	if (n <= 0) {
+		fprintf(stderr, "mkrootfs: %s: empty\n", pack_path);
+		fclose(f);
+		return 1;
+	}
+	pack = malloc((size_t)n);
+	if (!pack || fread(pack, 1, (size_t)n, f) != (size_t)n) {
+		fprintf(stderr, "mkrootfs: %s: read failed\n", pack_path);
+		fclose(f);
+		free(pack);
+		return 1;
+	}
+	fclose(f);
+
+	for (i = 0; i < (unsigned long)n; i++)
+		csum += pack[i];
+
+	total = ROOTFS_HDR_SIZE + (unsigned long)n;
+	if (size && size < total) {
+		fprintf(stderr, "mkrootfs: size %lu is smaller than the image "
+			"(%lu bytes)\n", size, total);
+		free(pack);
+		return 1;
+	}
+
+	memset(hdr, 0, sizeof(hdr));
+	memcpy(hdr, ROOTFS_MAGIC, 7);
+	put32le(hdr + 8, ROOTFS_VERSION);
+	put32le(hdr + 12, (uint32_t)n);
+	put32le(hdr + 16, csum);
+	put32le(hdr + 20, ROOTFS_FLAG_GZIP);
+
+	f = fopen(out_path, "wb");
+	if (!f) {
+		fprintf(stderr, "mkrootfs: %s: cannot create\n", out_path);
+		free(pack);
+		return 1;
+	}
+	if (fwrite(hdr, 1, sizeof(hdr), f) != sizeof(hdr) ||
+	    fwrite(pack, 1, (size_t)n, f) != (size_t)n) {
+		fprintf(stderr, "mkrootfs: %s: write failed\n", out_path);
+		fclose(f);
+		free(pack);
+		return 1;
+	}
+	free(pack);
+
+	if (size) {
+		static const unsigned char zero[4096];
+
+		for (i = total; i < size; ) {
+			unsigned long chunk = size - i;
+
+			if (chunk > sizeof(zero))
+				chunk = sizeof(zero);
+			if (fwrite(zero, 1, chunk, f) != chunk) {
+				fprintf(stderr, "mkrootfs: %s: pad failed\n",
+					out_path);
+				fclose(f);
+				return 1;
+			}
+			i += chunk;
+		}
+	}
+
+	if (fclose(f) != 0) {
+		fprintf(stderr, "mkrootfs: %s: close failed\n", out_path);
+		return 1;
+	}
+
+	fprintf(stderr, "mkrootfs: %s: pack %ld bytes, total %lu bytes\n",
+		out_path, n, size ? size : total);
+	return 0;
+}

+ 32 - 0
mm.c

@@ -1,6 +1,7 @@
 /* Physical memory manager: parse the E820 map and hand out 4 KB pages. */
 
 #include "mm.h"
+#include "bootinfo.h"
 #include "serial.h"
 
 /* One bit per 4 KB page; bit set = in use. */
@@ -109,6 +110,19 @@ void mm_init(void)
 	mark_used(0, MM_RESERVE_LOW);
 	mark_used(MM_SHADOW_BASE, MM_SHADOW_RESERVE);
 
+	/* The boot path copied the command-pack rootfs into high RAM before
+	 * the kernel ran.  Keep it away from the allocator until ramfs_init()
+	 * has taken its own copy. */
+	{
+		unsigned int pa = bootinfo_pack_addr();
+		unsigned int ps = bootinfo_pack_size();
+
+		if (pa >= MM_RESERVE_LOW && pa < MM_MAX_PHYS && ps != 0 &&
+		    ps <= MM_MAX_PHYS && pa + ps > pa &&
+		    pa + ps <= MM_MAX_PHYS)
+			mark_used(pa, ps);
+	}
+
 	/* Count free pages. */
 	free_pages = 0;
 	for (i = 0; i < MM_BITMAP_BITS; i++) {
@@ -188,6 +202,24 @@ void mm_free_page(unsigned int addr)
 	irq_restore(flags);
 }
 
+/* Return a boot-time reservation (the rootfs pack region) to the allocator.
+ * Low memory and the VGA shadow reserve always stay reserved. */
+void mm_free_range(unsigned int base, unsigned int size)
+{
+	unsigned int end, p;
+
+	if (base == 0 || size == 0)
+		return;
+	base &= ~(PAGE_SIZE - 1u);
+	if (base + size < base)
+		return;
+	end = (base + size + PAGE_SIZE - 1u) & ~(PAGE_SIZE - 1u);
+	if (base < MM_SHADOW_BASE + MM_SHADOW_RESERVE)
+		return;
+	for (p = base; p < end; p += PAGE_SIZE)
+		mm_free_page(p);
+}
+
 unsigned int mm_free_pages(void)
 {
 	return free_pages;

+ 3 - 0
mm.h

@@ -50,6 +50,9 @@ void mm_init(void);
 unsigned int mm_alloc_page(void);
 void mm_free_page(unsigned int addr);
 
+/* Free a page-aligned boot-time reservation (never low 1 MB / the shadow). */
+void mm_free_range(unsigned int base, unsigned int size);
+
 /* Return n contiguous pages (base address), or 0 if unavailable. */
 unsigned int mm_alloc_pages(unsigned int n);
 

+ 31 - 10
netkexec.c

@@ -5,11 +5,14 @@
  * Protocol (little-endian; UDP to NETKEXEC_PORT; the board replies to the
  * sender's IP/MAC/port):
  *
- *   host -> board  header   "KXNU" entry size csum          (16 bytes)
+ *   host -> board  header   "KXN2" entry kernel_size pack_size csum (20 bytes)
  *   host -> board  data     "KXND" offset len payload...    (12 + len)
  *   board -> host  ack      "KXNA" next_offset status       (12 bytes)
  *                           status 0 = keep going, 1 = done, 2 = bad checksum
  *
+ * The runtime pack replacement keeps the old 16-byte "KXNP" header
+ * (magic, entry, size, csum) with the raw NBPAK1 pack as data.
+ *
  * The board auto-detects the header from the WM loop (netkexec_pending());
  * program_netkexec_main() then runs stop-and-wait: every datagram is acked
  * with the next expected offset, so a lost or duplicated datagram costs one
@@ -31,7 +34,7 @@
 #define NETKEXEC_PORT       19288
 #define NETKEXEC_DATA_MAX   1400
 
-#define X_MAGIC_HDR   0x554E584Bu   /* "KXNU" */
+#define X_MAGIC_HDR   0x324E584Bu   /* "KXN2": kernel + rootfs */
 #define X_MAGIC_PAY   0x504E584Bu   /* "KXNP": a command pack for /ramfs */
 #define X_MAGIC_DATA  0x444E584Bu   /* "KXND" */
 #define X_MAGIC_ACK   0x414E584Bu   /* "KXNA" */
@@ -68,6 +71,7 @@ static void net_payload_free(void)
 static int have_header;
 static int payload_mode;         /* header was KXNP: replace the ramfs instead */
 static unsigned int x_entry, x_size, x_csum;
+static unsigned int x_kernel_size, x_pack_size;
 static unsigned char peer_mac[6], peer_ip[4];
 static unsigned int peer_port;
 
@@ -173,7 +177,7 @@ int netkexec_pending(void)
 	return 0;
 }
 
-/* Validate one received frame as a KXNU/KXNP header and stash the transfer.
+/* Validate one received frame as a KXN2/KXNP header and stash the transfer.
  * Returns 1 when it was captured.  The WebDAV server calls this while it
  * owns the receiver, so `make net-dev` hands the NIC back without waiting
  * for the host's header retransmits. */
@@ -195,12 +199,26 @@ int netkexec_capture_frame(const unsigned char *frame, unsigned int len)
 		return 0;
 
 	x_entry = rd32(payload + 4);
-	x_size = rd32(payload + 8);
-	x_csum = rd32(payload + 12);
+	if (magic == X_MAGIC_PAY) {
+		x_size = rd32(payload + 8);
+		x_csum = rd32(payload + 12);
+		x_kernel_size = 0;
+		x_pack_size = 0;
+		payload_mode = 1;
+	} else {
+		if (plen < 20)
+			return 0;
+		x_kernel_size = rd32(payload + 8);
+		x_pack_size = rd32(payload + 12);
+		x_csum = rd32(payload + 16);
+		x_size = x_kernel_size + x_pack_size;
+		if (x_kernel_size == 0 || x_pack_size > KEXEC_MAX ||
+		    x_size < x_kernel_size || x_size > KEXEC_MAX)
+			return 0;
+		payload_mode = 0;
+	}
 	if (x_size == 0 || x_size > KEXEC_MAX)
 		return 0;
-
-	payload_mode = (magic == X_MAGIC_PAY);
 	for (i = 0; i < 6; i++)
 		peer_mac[i] = p_mac[i];
 	for (i = 0; i < 4; i++)
@@ -346,11 +364,14 @@ void program_netkexec_main(void)
 		return;
 	}
 
-	io_putstr("\nnetkexec: received ");
-	io_puthex(x_size);
+	io_putstr("\nnetkexec: received kernel ");
+	io_puthex(x_kernel_size);
+	io_putstr(" bytes, rootfs ");
+	io_puthex(x_pack_size);
 	io_putstr(" bytes, entry ");
 	io_puthex(x_entry);
 	io_putstr(" -> jumping\n");
 
-	kexec_jump(x_entry, x_size);     /* never returns */
+	kexec_jump(x_entry, x_kernel_size, kexec_stage + x_kernel_size,
+		   x_pack_size);          /* never returns */
 }

+ 1 - 1
netkexec.h

@@ -9,7 +9,7 @@
 int netkexec_pending(void);
 
 /* Feed one frame received by another poller (the WebDAV server).  Returns 1
- * when it was a KXNU/KXNP header; netkexec_pending() then reports it without
+ * when it was a KXN2/KXNP header; netkexec_pending() then reports it without
  * polling the NIC again. */
 int netkexec_capture_frame(const unsigned char *frame, unsigned int len);
 

+ 74 - 38
netkexec_send.c

@@ -1,7 +1,8 @@
-/* Push kernel.bin to a Nebs OS board over UDP (the board's WM loop
- * auto-detects the header and jumps into the new kernel).
+/* Push a kernel + rootfs pack to a Nebs OS board over UDP (the board's WM
+ * loop auto-detects the header and jumps into the new kernel).
  *
- *   netkexec_send <board-ip> <port> <entry-hex> <file>
+ *   netkexec_send <board-ip> <port> <entry-hex> <kernel> [pack]
+ *   netkexec_send --payload <board-ip> <port> <pack>
  *
  * Protocol: see netkexec.c.  Stop-and-wait: every datagram (except the
  * header) is acked with the next expected offset; lost datagrams are
@@ -20,8 +21,8 @@
 #include <unistd.h>
 
 #define DATA_MAX   1400
-#define MAGIC_HDR  0x554E584Bu   /* "KXNU" */
-#define MAGIC_PAY   0x504E584Bu   /* "KXNP" */
+#define MAGIC_HDR  0x324E584Bu   /* "KXN2" (kernel + rootfs) */
+#define MAGIC_PAY  0x504E584Bu   /* "KXNP" (runtime pack replacement) */
 #define MAGIC_DATA 0x444E584Bu   /* "KXND" */
 #define MAGIC_ACK  0x414E584Bu   /* "KXNA" */
 #define ACK_DONE   1
@@ -69,14 +70,42 @@ static long wait_ack(int fd, unsigned int timeout_ms, uint32_t *status)
 	}
 }
 
+static unsigned char *read_file(const char *path, long *size)
+{
+	FILE *f = fopen(path, "rb");
+	unsigned char *data;
+	long n;
+
+	if (!f) {
+		fprintf(stderr, "netkexec_send: %s: %s\n", path, strerror(errno));
+		exit(1);
+	}
+	fseek(f, 0, SEEK_END);
+	n = ftell(f);
+	fseek(f, 0, SEEK_SET);
+	if (n <= 0) {
+		fprintf(stderr, "netkexec_send: %s: empty file\n", path);
+		fclose(f);
+		exit(1);
+	}
+	data = malloc((size_t)n);
+	if (!data || fread(data, 1, (size_t)n, f) != (size_t)n) {
+		fprintf(stderr, "netkexec_send: %s: read failed\n", path);
+		fclose(f);
+		free(data);
+		exit(1);
+	}
+	fclose(f);
+	*size = n;
+	return data;
+}
+
 int main(int argc, char **argv)
 {
-	const char *ip, *path;
-	unsigned int port;
-	unsigned int entry;
-	unsigned char *data, *pkt;
-	long size;
-	FILE *f;
+	const char *ip, *kernel_path, *pack_path = 0;
+	unsigned int port, entry;
+	unsigned char *kdata, *pdata = 0, *data, *pkt;
+	long ksize, psize = 0, size;
 	unsigned int offset = 0, sum = 0;
 	int fd, i, fails = 0;
 	int payload = 0;
@@ -92,41 +121,39 @@ int main(int argc, char **argv)
 		payload = 1;
 		ip = argv[2];
 		port = (unsigned int)strtoul(argv[3], NULL, 0);
-		path = argv[4];
+		kernel_path = argv[4];
 		entry = 0;
 	} else {
-		if (argc != 5) {
+		if (argc != 5 && argc != 6) {
 			fprintf(stderr,
-				"usage: %s <board-ip> <port> <entry-hex> <file>\n",
-				argv[0]);
+				"usage: %s <board-ip> <port> <entry-hex> "
+				"<kernel> [pack]\n", argv[0]);
 			return 2;
 		}
 		ip = argv[1];
 		port = (unsigned int)strtoul(argv[2], NULL, 0);
 		entry = (unsigned int)strtoul(argv[3], NULL, 16);
-		path = argv[4];
+		kernel_path = argv[4];
+		if (argc == 6)
+			pack_path = argv[5];
 	}
 
-	f = fopen(path, "rb");
-	if (!f) {
-		fprintf(stderr, "netkexec_send: %s: %s\n", path, strerror(errno));
-		return 1;
-	}
-	fseek(f, 0, SEEK_END);
-	size = ftell(f);
-	fseek(f, 0, SEEK_SET);
+	kdata = read_file(kernel_path, &ksize);
+	if (pack_path)
+		pdata = read_file(pack_path, &psize);
+	size = ksize + psize;
 	if (size <= 0 || size > 0x400000) {   /* matches kexec.h KEXEC_MAX */
-		fprintf(stderr, "netkexec_send: bad size %ld\n", size);
-		fclose(f);
+		fprintf(stderr, "netkexec_send: bad total size %ld\n", size);
 		return 1;
 	}
 	data = malloc((size_t)size);
-	if (!data || fread(data, 1, (size_t)size, f) != (size_t)size) {
-		fprintf(stderr, "netkexec_send: read failed\n");
-		fclose(f);
+	if (!data) {
+		fprintf(stderr, "netkexec_send: out of memory\n");
 		return 1;
 	}
-	fclose(f);
+	memcpy(data, kdata, (size_t)ksize);
+	if (psize)
+		memcpy(data + ksize, pdata, (size_t)psize);
 	for (i = 0; i < size; i++)
 		sum += data[i];
 
@@ -143,24 +170,30 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	pkt = malloc(12 + DATA_MAX);
+	pkt = malloc(20 + DATA_MAX);
 	if (payload)
 		printf("netkexec: %s:%u, %ld bytes, command pack for /ramfs\n",
 		       ip, port, size);
 	else
-		printf("netkexec: %s:%u, %ld bytes, entry 0x%08x\n",
-		       ip, port, size, entry);
+		printf("netkexec: %s:%u, kernel %ld + rootfs %ld bytes, "
+		       "entry 0x%08x\n", ip, port, ksize, psize, entry);
 
 	/* Header first: the board answers with the first expected offset. */
 	wr32(pkt, payload ? MAGIC_PAY : MAGIC_HDR);
 	wr32(pkt + 4, entry);
-	wr32(pkt + 8, (uint32_t)size);
-	wr32(pkt + 12, (uint32_t)sum);
+	if (payload) {
+		wr32(pkt + 8, (uint32_t)size);
+		wr32(pkt + 12, (uint32_t)sum);
+	} else {
+		wr32(pkt + 8, (uint32_t)ksize);
+		wr32(pkt + 12, (uint32_t)psize);
+		wr32(pkt + 16, (uint32_t)sum);
+	}
 	for (i = 0; i < 20; i++) {
 		uint32_t status;
 		long ack;
 
-		if (send_packet(fd, &dst, pkt, 16) != 0) {
+		if (send_packet(fd, &dst, pkt, payload ? 16 : 20) != 0) {
 			perror("sendto");
 			return 1;
 		}
@@ -177,7 +210,7 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	/* Stop-and-wait data transfer. */
+	/* Stop-and-wait data transfer (kernel bytes, then rootfs bytes). */
 	while (offset < (unsigned int)size) {
 		unsigned int chunk = (unsigned int)size - offset;
 		uint32_t status = 0;
@@ -217,10 +250,13 @@ int main(int argc, char **argv)
 		printf("\rnetkexec: 100%% (%ld bytes) - pack accepted, /ramfs replaced\n",
 		       size);
 	else
-		printf("\rnetkexec: 100%% (%ld bytes) - image accepted, board is jumping\n",
+		printf("\rnetkexec: 100%% (%ld bytes) - kernel+rootfs accepted, board is jumping\n",
 		       size);
 	close(fd);
 	free(pkt);
 	free(data);
+	if (pdata)
+		free(pdata);
+	free(kdata);
 	return 0;
 }

+ 5 - 5
payload.c

@@ -40,11 +40,11 @@ int payload_pending(void)
 		    serial_ring_peek(3, &p)) {
 			if (x == 'X' && n == 'N' && p == 'P')
 				return 1;
-			/* A kernel image ("KXEC") belongs to kexec_pending():
-			 * leave it untouched (the WM checks this detector
-			 * first, so consuming the 'K' here would eat the
-			 * reload stream). */
-			if (x == 'X' && n == 'E' && p == 'C')
+			/* A kernel image ("KXE2", formerly "KXEC")
+			 * belongs to kexec_pending(): leave it untouched
+			 * (the WM checks this detector first, so consuming
+			 * the 'K' here would eat the reload stream). */
+			if (x == 'X' && n == 'E' && (p == '2' || p == 'C'))
 				return 0;
 			(void)serial_ring_getc();
 			continue;

+ 37 - 8
ramfs.c

@@ -134,16 +134,23 @@ int ramfs_init(const void *pack, unsigned int size)
 {
 	const struct ramfs_hdr *h;
 	const unsigned char *p = (const unsigned char *)pack;
+	unsigned int pages;
+
+	if (!pack || size < sizeof(struct ramfs_hdr) ||
+	    size > RAMFS_MAX_DECOMPRESSED)
+		return -1;
 
 	/* The embedded pack is gzip-compressed (the same inflate code that the
-	 * PNG decoder uses); a raw pack is still accepted, which is what the
-	 * cmds-dev path passes to ramfs_replace(). */
+	 * PNG decoder uses); a raw pack is still accepted, which is what a
+	 * kexec reload passes when it carries the running rootfs over.  Either
+	 * way the pack is copied into page-allocated memory, so the boot-time
+	 * region can be released. */
 	if (size >= 18 && p[0] == 0x1F && p[1] == 0x8B) {
 		unsigned int isize = (unsigned int)p[size - 4] |
 				     ((unsigned int)p[size - 3] << 8) |
 				     ((unsigned int)p[size - 2] << 16) |
 				     ((unsigned int)p[size - 1] << 24);
-		unsigned int pages, wpages, olen = 0;
+		unsigned int wpages, olen = 0;
 		unsigned char *out, *ws;
 
 		if (isize == 0 || isize > RAMFS_MAX_DECOMPRESSED)
@@ -192,13 +199,27 @@ int ramfs_init(const void *pack, unsigned int size)
 		return 0;
 	}
 
-	h = ramfs_validate(pack, size);
-	if (!h)
-		return -1;
-	pack_base = (const unsigned char *)pack;
+	/* Raw pack: copy it so the caller can release the boot region. */
+	pages = (size + 0xFFF) >> 12;
+	{
+		unsigned char *copy = (unsigned char *)mm_alloc_pages(pages);
+		unsigned int i;
+
+		if (!copy)
+			return -1;
+		for (i = 0; i < size; i++)
+			copy[i] = p[i];
+		h = ramfs_validate(copy, size);
+		if (!h) {
+			for (i = 0; i < pages; i++)
+				mm_free_page((unsigned int)copy + i * 0x1000);
+			return -1;
+		}
+		pack_base = copy;
+	}
 	pack_size = size;
 	pack_hdr = h;
-	pack_pages = 0;
+	pack_pages = pages;
 	return 0;
 }
 
@@ -311,3 +332,11 @@ unsigned int ramfs_pack_size(void)
 {
 	return pack_hdr ? pack_size : 0;
 }
+
+/* Base of the owned pack (NULL when there is no pack or it is not ours).
+ * A kexec reload passes this to the new kernel so a kernel-only update can
+ * keep the commands. */
+const void *ramfs_pack_base(void)
+{
+	return pack_pages ? pack_base : 0;
+}

+ 8 - 2
ramfs.h

@@ -1,5 +1,6 @@
 /* ramfs: read-only in-memory filesystem backed by the command pack (mkpack).
- * The pack is kept where the loader put it (no copies); paths inside it are
+ * The pack is copied into page-allocated memory at boot (see ramfs_init), so
+ * the boot path's copy in high RAM can be released.  Paths inside it are
  * relative to the mount root (e.g. "bin/ls"). */
 #ifndef RAMFS_H
 #define RAMFS_H
@@ -13,7 +14,9 @@ struct ramfs_dirent {
 	unsigned int is_dir;
 };
 
-/* Validate a pack and make it the ramfs contents.  Returns 0 on success. */
+/* Validate a pack and make it the ramfs contents.  The pack may be gzip or
+ * raw; it is copied/inflated into page-allocated memory.  Returns 0 on
+ * success. */
 int ramfs_init(const void *pack, unsigned int size);
 
 /* Replace the contents with a freshly received pack (page-allocated): the
@@ -35,4 +38,7 @@ unsigned int ramfs_count(void);
 /* Size of the loaded pack in bytes (0 when none). */
 unsigned int ramfs_pack_size(void);
 
+/* Owned pack base (NULL when there is no pack), for kexec handoff. */
+const void *ramfs_pack_base(void);
+
 #endif

+ 34 - 0
rootfs.h

@@ -0,0 +1,34 @@
+/* Rootfs image format (see mkrootfs.c).
+ *
+ * The command pack is the system rootfs: a gzip-compressed NBPAK1 pack with
+ * a one-sector header, so the boot loader can find its length and checksum
+ * without a filesystem.  The header is sector 0 of the image (or of the
+ * rootfs partition); the pack starts at byte 512.
+ *
+ *   0   char magic[8]        "NBROOT1\0"
+ *   8   u32 version
+ *   12  u32 pack_bytes       exact gzip length (the ISIZE trailer matters)
+ *   16  u32 pack_csum        sum of the pack bytes, mod 2^32
+ *   20  u32 flags            ROOTFS_FLAG_GZIP
+ *   ... zero padding to 512
+ */
+#ifndef ROOTFS_H
+#define ROOTFS_H
+
+#define ROOTFS_MAGIC         "NBROOT1"
+#define ROOTFS_VERSION       1u
+#define ROOTFS_FLAG_GZIP     0x00000001u
+
+#define ROOTFS_HDR_SIZE      512u
+#define ROOTFS_PACK_OFFSET   512u
+
+struct rootfs_hdr {
+	char magic[8];
+	unsigned int version;
+	unsigned int pack_bytes;
+	unsigned int pack_csum;
+	unsigned int flags;
+	unsigned int reserved[(ROOTFS_HDR_SIZE - 24) / 4];
+} __attribute__((packed));
+
+#endif

+ 2 - 2
run.c

@@ -12,8 +12,8 @@
 #define PROGRAM_LOAD_ADDR 0x80000
 #define PROGRAM_LOAD_MAX  0x6000   /* up to 0x86000, below the VGA window */
 
-/* User programs embedded in the kernel image (like hello.elf): they travel
- * with `make dev`/`make net-dev`, so a board with no readable filesystem can
+/* User programs embedded in the kernel image: they travel with
+ * `make dev`/`make net-dev`, so a board with no readable filesystem can
  * still run them.  Rebuilt from prog.c/space.c by the Makefile. */
 extern unsigned char _binary_prog_bin_start[], _binary_prog_bin_end[];
 extern unsigned char _binary_space_bin_start[], _binary_space_bin_end[];

+ 6 - 4
ucmd_browser.c

@@ -1,6 +1,6 @@
-/* browser launcher: the real browser is a large disk app (BROWSER on a FAT
- * mount) because it links ngui, the full image library, TLS and uhttp.  This
- * tiny command keeps `browser` on PATH and in the WM's Web button. */
+/* browser launcher: the real browser ships in the rootfs pack as
+ * bin/browser.app (and can be overridden by /share/BROWSER or a FAT mount).
+ * This tiny command keeps `browser` on PATH and in the WM's Web button. */
 
 #include "ucmd_lib.h"
 
@@ -10,6 +10,8 @@
 static const char *const candidates[] = {
 	"/share/BROWSER",
 	"/share/BROWSER.ELF",
+	/* The copy shipped in the command-pack rootfs. */
+	"/ramfs/bin/browser.app",
 	"/floppy1/BROWSER",
 	"/floppy1/BROWSER.ELF",
 	"/ide0p1/BROWSER",
@@ -47,7 +49,7 @@ void _start(const char *arg)
 			u_exit(0);
 		}
 	}
-	u_puts("browser: BROWSER is not installed on a mounted disk\n");
+	u_puts("browser: BROWSER is not installed in the pack or on a disk\n");
 	u_puts("  put browser.elf on the root floppy or /share\n");
 	u_exit(1);
 }

+ 7 - 4
ucmd_gallery.c

@@ -1,12 +1,15 @@
-/* gallery launcher: the real app is a disk image (GALLERY) because the
- * kernel's embedded command pack is close to its low-memory wall.  This tiny
- * command keeps `gallery` on PATH; the app itself links ngui and the font. */
+/* gallery launcher: the real app ships in the rootfs pack as
+ * bin/gallery.app (and can be overridden by /share/GALLERY or a FAT mount).
+ * This tiny command keeps `gallery` on PATH; the app links ngui and the
+ * font. */
 
 #include "ucmd_lib.h"
 
 static const char *const candidates[] = {
 	"/share/GALLERY",
 	"/share/GALLERY.ELF",
+	/* The copy shipped in the command-pack rootfs. */
+	"/ramfs/bin/gallery.app",
 	"/floppy1/GALLERY",
 	"/floppy1/GALLERY.ELF",
 	"/ide0p1/GALLERY",
@@ -44,7 +47,7 @@ void _start(const char *arg)
 			u_exit(0);
 		}
 	}
-	u_puts("gallery: GALLERY is not installed on a mounted disk\n");
+	u_puts("gallery: GALLERY is not installed in the pack or on a disk\n");
 	u_puts("  put gallery.elf on the root floppy or /share\n");
 	u_exit(1);
 }

+ 387 - 15
vesa_stage.S

@@ -2,10 +2,11 @@
  * VESA real-mode stage: runs at 0x7E00 before protected mode.
  *
  * Called by the boot sector in real mode (CS=0, IP=0x7E00).
- * Walks the VBE mode list from the BIOS, picks the best mode
- * (highest resolution, >= 16 bpp, LFB, fits in VRAM), writes
- * the mode info to 0x600, enables A20, enters PM, jumps to
- * kernel_main.
+ * Probes the E820 map, enables A20, copies the command-pack rootfs into
+ * high memory (INT 15h AH=87h, so the CPU never leaves real mode), walks
+ * the VBE mode list and picks the best mode (highest resolution, >= 16 bpp,
+ * LFB, fits in VRAM), writes the mode info to 0x600 and the boot info to
+ * 0x500, then enters protected mode and jumps to kernel_entry.
  *
  * If no VESA mode works, boots in text mode (valid=0 at 0x600).
  */
@@ -18,13 +19,25 @@
 .equ E820_MAGIC,    0xE820E820
 .equ E820_BUF_END,  0x1008 + 32*24   /* 32 entries * 24 bytes */
 
+/* Rootfs/boot-info constants (must match rootfs.h / bootinfo.h). */
+.equ ROOTFS_VERSION,   1
+.equ PACK_MAX,         0x400000      /* 4 MB gzip payload cap */
+.equ PACK_MIN_ADDR,    0x900000      /* just above the VGA shadow reserve */
+.equ PACK_TOP_ADDR,    0x1000000     /* INT 15h AH=87h has a 24-bit base */
+.equ BOOT_INFO_MAGIC,  0x4E425349
+.equ BOOT_INFO_VERSION, 2
+.equ BOOT_INFO_WORDS,  12
+
 _vesa_stage:
 	xorw  %ax, %ax
 	movw  %ax, %ds
 	movw  %ax, %es
+	cld
+	movb  %dl, stage_drive
 
 	/* No UEFI boot-info block on the BIOS path (see bootinfo.h).  The boot
-	 * sector used 0x500 as its disk-address packet, so clear the magic. */
+	 * sector used 0x500 as its disk-address packet, so clear the magic;
+	 * write_bootinfo fills in the real block before the PM jump. */
 	movl  $0, 0x500
 
 	/* ---- Probe INT 15h E820 memory map into 0:0x1000 ----
@@ -57,6 +70,110 @@ e820_loop:
 	jmp   e820_loop
 e820_done:
 
+	/* ---- Rootfs: copy the command pack into high RAM ----
+	 * The pack is a gzip blob behind a one-sector NBROOT1 header (rootfs.h).
+	 * The destination is the top of RAM below 16 MB (the AH=87h block move
+	 * uses 24-bit descriptor bases), well above the 1..9 MB shadow reserve. */
+	call  enable_a20
+	xorw  %ax, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+
+	movw  $msg_pack, %si
+	call  print_str
+
+	movl  0x7C42, %eax            /* boot-sector pack_lba field */
+	movl  %eax, pack_lba
+	movl  $0, pack_addr
+	movl  $0, pack_bytes
+	testl %eax, %eax
+	jz    pack_fail
+
+	movl  pack_lba, %eax
+	movw  $1, %cx
+	call  read_sectors
+	jc    pack_fail
+
+	/* Validate the header at 0x80000: "NBROOT1\0", version 1. */
+	movl  $0x80000, %esi
+	movl  0(%esi), %eax
+	cmpl  $0x4F52424E, %eax       /* "NBRO" */
+	jne   pack_fail
+	movl  4(%esi), %eax
+	cmpl  $0x0031544F, %eax       /* "OT1\0" */
+	jne   pack_fail
+	cmpl  $ROOTFS_VERSION, 8(%esi)
+	jne   pack_fail
+	movl  12(%esi), %eax          /* pack_bytes */
+	testl %eax, %eax
+	jz    pack_fail
+	cmpl  $PACK_MAX, %eax
+	ja    pack_fail
+	movl  %eax, pack_bytes
+
+	call  compute_pack_addr
+	testl %eax, %eax
+	jz    pack_noroom
+	movl  %eax, pack_addr
+
+	movl  $0, pack_off
+load_loop:
+	movl  pack_off, %eax
+	cmpl  pack_bytes, %eax
+	jae   load_ok
+	movl  pack_bytes, %ecx
+	subl  %eax, %ecx
+	addl  $511, %ecx
+	shrl  $9, %ecx
+	cmpl  $64, %ecx
+	jbe   1f
+	movl  $64, %ecx
+1:	movl  %ecx, chunk_sectors
+	movl  pack_off, %eax
+	shrl  $9, %eax
+	addl  pack_lba, %eax
+	incl  %eax                    /* pack data starts after the header */
+	call  read_sectors
+	jc    pack_fail
+	movl  chunk_sectors, %eax
+	shll  $9, %eax
+	movl  %eax, copy_len
+	movl  pack_addr, %eax
+	addl  pack_off, %eax
+	movl  %eax, copy_dst
+	call  bios_copy
+	jc    pack_fail
+	movl  chunk_sectors, %eax
+	shll  $9, %eax
+	addl  %eax, pack_off
+	movw  $msg_dot, %si
+	call  print_str
+	jmp   load_loop
+
+load_ok:
+	movw  $msg_ok, %si
+	call  print_str
+	jmp   pack_done
+
+pack_noroom:
+	movw  $msg_noroom, %si
+	call  print_str
+	jmp   pack_clear
+
+pack_fail:
+	movw  $msg_fail, %si
+	call  print_str
+pack_clear:
+	movl  $0, pack_addr
+	movl  $0, pack_bytes
+pack_done:
+
+	/* read_sectors leaves ES at the staging segment; the VBE calls below
+	 * expect a normal zero segment. */
+	xorw  %ax, %ax
+	movw  %ax, %ds
+	movw  %ax, %es
+
 	/* ---- VBE Controller Info into 0:0x2000 ---- */
 	movw  $0x2000, %di
 	movw  $0x4256, (%di)	/* "VB" */
@@ -179,15 +296,21 @@ walk_done:
 	movw  %ax, 10(%bx)		/* lfb_base hi */
 	movw  vram_64k, %ax
 	movw  %ax, 12(%bx)		/* vram_64k */
-	jmp   enter_pm
+	jmp   stage_finish
 
 vesa_no_vbe:
 	/* No VESA: stay in text mode, mark fb info invalid. */
 	movw  $0x600, %bx
 	movb  $0, (%bx)
 
-enter_pm:
-	/* Enable A20 gate. */
+stage_finish:
+	/* Publish the boot info after the last BIOS call that could touch
+	 * 0x500, then enter protected mode. */
+	call  write_bootinfo
+	jmp   enter_pm
+
+/* ---- A20 ---------------------------------------------------------------- */
+enable_a20:
 	movw  $0x2401, %ax
 	int   $0x15
 	call  a20_wait
@@ -203,7 +326,218 @@ enter_pm:
 	orb   $0x02, %al
 	andb  $0xFE, %al
 	outb  %al, $0x92
-1:
+1:	ret
+
+a20_wait:
+	inb   $0x64, %al
+	testb $0x02, %al
+	jnz   a20_wait
+	ret
+
+/* ---- Read sectors via INT 13h ------------------------------------------
+ * in:  eax = LBA, cx = sector count
+ * out: data at 0x80000, CF set on failure
+ * Clobbers all general registers. */
+read_sectors:
+	movl  %eax, rd_lba
+	movw  %cx, rd_left
+	movw  $0, rd_off
+	movw  $0, rd_tries
+read_retry:
+	movw  rd_off, %bx
+	xorw  %ax, %ax
+	movw  %ax, %ds
+	movw  $0x8000, %ax
+	movw  %ax, %es
+	movb  stage_drive, %dl
+	cmpb  $0x80, %dl
+	jb    read_chs
+
+	/* EDD LBA read */
+	movw  $0x10, 0x500
+	movw  $1, 0x502
+	movw  %bx, 0x504
+	movw  $0x8000, 0x506
+	movl  rd_lba, %eax
+	movl  %eax, 0x508
+	movl  $0, 0x50C
+	movw  $0x500, %si
+	movb  $0x42, %ah
+	jmp   read_int
+
+read_chs:
+	movw  rd_lba, %ax
+	xorw  %dx, %dx
+	movw  $18, %di
+	divw  %di
+	movb  %dl, %cl
+	incb  %cl
+	xorw  %dx, %dx
+	movw  $2, %di
+	divw  %di
+	movb  %dl, %dh
+	movb  %al, %ch
+	movb  stage_drive, %dl
+	movb  $0x02, %ah
+	movb  $1, %al
+	movw  rd_off, %bx
+
+read_int:
+	int   $0x13
+	jnc   read_ok
+	/* Reset and retry the same sector a few times. */
+	pushw %cx
+	movb  stage_drive, %dl
+	xorw  %ax, %ax
+	int   $0x13
+	popw  %cx
+	incw  rd_tries
+	cmpw  $3, rd_tries
+	jb    read_retry
+	stc
+	ret
+
+read_ok:
+	incl  rd_lba
+	addw  $0x200, rd_off
+	decw  rd_left
+	jnz   read_retry
+	clc
+	ret
+
+/* ---- Pick the pack destination from the E820 map -----------------------
+ * The AH=87h block move uses 24-bit bases, so stay below 16 MB.
+ * out: eax = 4 KB-aligned destination (0 when there is no room) */
+compute_pack_addr:
+	movl  pack_bytes, %ebx
+	addl  $0xFFF, %ebx
+	andl  $0xFFFFF000, %ebx
+	movl  $0, best_addr
+
+	cmpl  $E820_MAGIC, 0x1000
+	jne   pack_addr_fallback
+	movl  0x1004, %ecx
+	testl %ecx, %ecx
+	jz    pack_addr_fallback
+	movl  $0x1008, %esi
+
+pack_addr_loop:
+	cmpl  $1, 16(%esi)            /* E820_RAM */
+	jne   1f
+	movl  0(%esi), %eax           /* base low */
+	cmpl  $PACK_TOP_ADDR, %eax
+	jae   1f
+	movl  4(%esi), %edx           /* base high */
+	testl %edx, %edx
+	jnz   1f
+	addl  8(%esi), %eax           /* end = base + length low */
+	jc    2f
+	cmpl  $PACK_TOP_ADDR, %eax
+	jbe   3f
+2:	movl  $PACK_TOP_ADDR, %eax
+3:	/* Need [end - need, end) to fit inside the entry. */
+	movl  0(%esi), %edx
+	addl  %ebx, %edx
+	jc    1f
+	cmpl  %edx, %eax
+	jb    1f
+	subl  %ebx, %eax
+	andl  $0xFFFFF000, %eax
+	cmpl  $PACK_MIN_ADDR, %eax
+	jb    1f
+	cmpl  best_addr, %eax
+	jbe   1f
+	movl  %eax, best_addr
+1:	addl  $24, %esi
+	decl  %ecx
+	jnz   pack_addr_loop
+	movl  best_addr, %eax
+	ret
+
+pack_addr_fallback:
+	/* No usable E820 map: matches mm_init's 16 MB fallback. */
+	movl  $PACK_TOP_ADDR, %eax
+	cmpl  %ebx, %eax
+	jbe   1f
+	subl  %ebx, %eax
+	andl  $0xFFFFF000, %eax
+	cmpl  $PACK_MIN_ADDR, %eax
+	jb    1f
+	ret
+1:	xorl  %eax, %eax
+	ret
+
+/* ---- Copy a chunk to high memory with INT 15h AH=87h -------------------
+ * in: copy_dst (24-bit), copy_len; source is 0x80000.
+ * Copies in 16-bit words; returns with CF set on failure. */
+bios_copy:
+	movl  copy_dst, %eax
+	movw  %ax, bios_gdt + 0x1A      /* dest base 15:0 */
+	shrl  $16, %eax
+	movb  %al, bios_gdt + 0x1C      /* dest base 23:16 */
+	movl  copy_len, %ecx
+	shrl  $1, %ecx                  /* AH=87h counts words */
+	jz    bios_copy_done
+	xorw  %ax, %ax
+	movw  %ax, %es
+	movw  $bios_gdt, %si
+	movw  $0x8700, %ax
+	int   $0x15
+	jc    bios_copy_fail
+bios_copy_done:
+	clc
+	ret
+bios_copy_fail:
+	stc
+	ret
+
+/* ---- Print a NUL-terminated string with the BIOS teletype ------------- */
+print_str:
+	pushw %ax
+	pushw %bx
+	movb  $0x0E, %ah
+	xorw  %bx, %bx
+1:	lodsb
+	testb %al, %al
+	jz    2f
+	int   $0x10
+	jmp   1b
+2:	popw  %bx
+	popw  %ax
+	ret
+
+/* ---- Boot-info block at 0x500 (bootinfo.h) ----------------------------
+ * twelve dwords: magic, version, checksum, rsdp, flags, pack_addr,
+ * pack_size, reserved[5]; the checksum makes the sum zero. */
+write_bootinfo:
+	xorw  %ax, %ax
+	movw  %ax, %ds
+	movl  $BOOT_INFO_MAGIC, 0x500
+	movl  $BOOT_INFO_VERSION, 0x504
+	movl  $0, 0x508
+	movl  $0, 0x50C
+	movl  $0, 0x510
+	movl  pack_addr, %eax
+	movl  %eax, 0x514
+	movl  pack_bytes, %eax
+	movl  %eax, 0x518
+	movl  $0, 0x51C
+	movl  $0, 0x520
+	movl  $0, 0x524
+	movl  $0, 0x528
+	movl  $0, 0x52C
+	xorl  %eax, %eax
+	movl  $0x500, %esi
+	movl  $BOOT_INFO_WORDS, %ecx
+1:	addl  (%esi), %eax
+	addl  $4, %esi
+	loop  1b
+	negl  %eax
+	movl  %eax, 0x508
+	ret
+
+/* ---- Protected-mode entry ---------------------------------------------- */
+enter_pm:
 	cli
 	lgdt  stage2_gdtr
 	movl  %cr0, %eax
@@ -213,12 +547,6 @@ enter_pm:
 	.long stage2_pm
 	.word 0x08
 
-a20_wait:
-	inb   $0x64, %al
-	testb $0x02, %al
-	jnz   a20_wait
-	ret
-
 .code32
 stage2_pm:
 	movw  $0x10, %ax
@@ -242,6 +570,50 @@ stage2_gdtr:
 	.word stage2_gdt_end - stage2_gdt - 1
 	.long stage2_gdt
 
+/* ---- INT 15h AH=87h descriptor table (48 bytes) -----------------------
+ * 16 zero bytes, source descriptor at 0x10 (0x80000, 64 KB), destination
+ * at 0x18 (patched per chunk), 16 zero bytes. */
+.align 16
+bios_gdt:
+	.quad 0
+	.quad 0
+	/* source: limit 0xFFFF, base 0x80000, access 0x93, 16-bit data */
+	.word 0xFFFF
+	.word 0x0000
+	.byte 0x08
+	.byte 0x93
+	.word 0x0000
+	/* destination: limit 0xFFFF, base patched, access 0x93 */
+	.word 0xFFFF
+	.word 0x0000
+	.byte 0x00
+	.byte 0x93
+	.word 0x0000
+	.quad 0
+	.quad 0
+
+/* ---- Stage variables --------------------------------------------------- */
+.align 4
+stage_drive:    .byte 0
+pack_lba:       .long 0
+pack_bytes:     .long 0
+pack_addr:      .long 0
+pack_off:       .long 0
+chunk_sectors:  .long 0
+copy_dst:       .long 0
+copy_len:       .long 0
+best_addr:      .long 0
+rd_lba:         .long 0
+rd_off:         .word 0
+rd_left:        .word 0
+rd_tries:       .word 0
+
+msg_pack:       .asciz "Loading commands... "
+msg_dot:        .asciz "."
+msg_ok:         .asciz " ok\r\n"
+msg_noroom:     .asciz " skipped (no room)\r\n"
+msg_fail:       .asciz " failed\r\n"
+
 .align 2
 vram_64k:
 	.word 0

+ 2 - 2
vfs.c

@@ -586,8 +586,8 @@ void vfs_mount_ramfs(void)
 {
 	vfs_mount_t *m;
 
-	if (ramfs_count() == 0)
-		return;
+	/* Mount even when the pack is empty: the runtime `cmds-dev` path can
+	 * install a pack later and vfs_refresh_ramfs() then updates the mount. */
 	m = mount_slot("ramfs");
 	if (!m)
 		return;

+ 1 - 1
wm.c

@@ -1840,7 +1840,7 @@ void wm_handle_kexec(void)
 	if (kexec_busy)
 		return;
 	/* A command pack for /ramfs (checked before kexec: both start with
-	 * 'K', and the kexec detector drops anything that is not KXEC). */
+	 * 'K', and the kexec detector drops anything that is not KXE2). */
 	if (payload_pending()) {
 		kexec_busy = 1;
 		if (process_create("payload", program_payload_main) < 0)

+ 1 - 2
xhci.c

@@ -7,8 +7,7 @@
  * are implemented, which is exactly what usbhid.c uses.
  *
  * The schedule is polled, like the rest of the kernel.  Rings and contexts
- * are page-allocated so the driver does not grow the kernel BSS into the
- * 0x7F000 wall. */
+ * are page-allocated so the driver does not grow the kernel BSS. */
 
 #include "mm.h"
 #include "paging.h"