Преглед изворни кода

Add persistent /data and self/net installs

Installed disks get a fourth FAT partition (NEBOSDATA) after the ESP,
mounted at /data.  The installer creates it on a first install, preserves
it on later installs, and merges its MBR entry into the cloned table.

Make the UEFI loader read the installed P1/P2 image instead of always
using the kernel and pack embedded in BOOTX64.EFI, so both boot paths
consume the same install and later updates do not rewrite the ESP.

Add `install <target> self` and the KXI2 net-install path.  Both write
P2 then the P1 kernel, patch the exact kernel_sectors, verify the result
and preserve /data; net-install acks ACK_INSTALLED before jumping.  The
full pack carries install/kernel.bin and the kernel retains the gzip boot
image for self-update.

Clear the fake FAT BPB from the P1 boot sector and skip the NebOS boot
partition in VFS, so it is no longer mounted as a fake volume.

Update AGENTS.md and the architecture pages, and extend the QEMU
installer test to cover reinstall, self-update, /data preservation and
the UEFI disk-read path.
Johannes Zellner пре 1 недеља
родитељ
комит
b724a4052f
26 измењених фајлова са 1711 додато и 339 уклоњено
  1. 107 46
      AGENTS.md
  2. 26 19
      Makefile
  3. 6 20
      boot.S
  4. 35 24
      docs/boot.html
  5. 16 2
      docs/development.html
  6. 36 8
      docs/filesystems.html
  7. 1 1
      docs/index.html
  8. 11 5
      docs/network.html
  9. 12 7
      docs/userland.html
  10. 67 3
      efi/efi.h
  11. 206 24
      efi/main.c
  12. 109 51
      fatformat.c
  13. 7 0
      fatformat.h
  14. 595 41
      install.c
  15. 14 0
      install.h
  16. 22 5
      kernel.c
  17. 74 8
      netkexec.c
  18. 2 2
      netkexec.h
  19. 86 6
      netkexec_send.c
  20. 37 0
      ramfs.c
  21. 7 0
      ramfs.h
  22. 7 0
      rootfs.h
  23. 1 1
      terminal.c
  24. 167 50
      tools/qmp_install_test.py
  25. 59 16
      vfs.c
  26. 1 0
      vfs.h

+ 107 - 46
AGENTS.md

@@ -66,12 +66,13 @@ 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 test-install # USB-storage installer test: clone + BIOS/UEFI target boot
+make test-install # installer test: clone, reinstall, install self, /data, BIOS/UEFI boot
 make rootfs     # build cmds.img (the 8 MB rootfs partition image)
 make nowm       # diagnostic kernel with no WM; the boot console stays visible
 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 net-dev    # network kernel + rootfs reload (KXN2, RAM only)
+make net-install # persist kernel + full rootfs to P1/P2 (KXI2, keeps /data)
 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)
@@ -114,7 +115,7 @@ make clean
 - `usb.img` is 9 MB: partition 1 (type `0x06`, LBA 63, 1024 sectors) is the
   boot sector + kernel and partition 2 (type `0x7F`, LBA 1088) is the 8 MB
   rootfs image (`cmds.img`), 512 KB behind the boot sector.  With the WiFi
-  firmware the gzip pack is ~1 MB, so it now ends past the first 1.44 MB: a
+  firmware the gzip pack is ~1.3 MB, so it now ends past the first 1.44 MB: a
   BIOS that boots the stick as a CHS-only floppy can no longer read all of
   it (fixed-disk LBA boots are fine).  The boot sector's `pack_lba` field points the
   VESA stage at partition 2.  `usb-efi.img` (17 MB) adds an 8 MB FAT16 ESP
@@ -270,7 +271,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `efi/efi.h` (minimal UEFI types and table/protocol layouts), `efi/main.c`
   (GOP mode choice, `GetMemoryMap` → E820, kernel staging below 4 GB and
   rootfs pack in loader-data pages below 512 MB, boot-info/framebuffer
-  blocks, `ExitBootServices`), `efi/tramp.S` +
+  blocks, `ExitBootServices`).  The loader prefers the raw installed image:
+  it locates the parent whole-disk Block I/O handle from the ESP device path,
+  reads the P1 boot sector (kernel_lba/kernel_sectors/pack_lba), then reads
+  the kernel from P1 and the NBROOT1 gzip pack from P2.  The kernel and pack
+  embedded in `BOOTX64.EFI` are only a fallback when the disk image is
+  missing or invalid, so `install`/`net-install` update both boot paths
+  without rewriting the ESP.  `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
@@ -503,21 +510,27 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   64 entries of the last FAT directory (`fat_cache` in `vfs.c`), so the
   WebDAV server's index 0..N-1 sequence is one directory walk instead of N;
   any write/mkdir/rmdir/unlink invalidates it.
-- **Installer**: `install.c` — the privileged `install <target> [source]`
-  command clones the live NebOS image.  It detects a source by the MBR plus
-  the LBA-63 boot-sector signature (`NEBOS` OEM string, `kernel_lba` 64,
-  `pack_lba` 1088), computes the used length from the highest partition end,
-  allocates a page buffer for the whole image and reads the source into RAM
-  first (LBA 1..62 are synthesized as zeros instead of read).  It then writes
-  the target from RAM in chunks with the source no longer active, writes the
-  MBR **last**, flushes and verifies by checksum before remounting the
-  target.  A source-read failure therefore leaves the target untouched.  The
-  source read keeps the BKL (a USB3 stick must stay active across chunks);
-  the target writes release it per chunk so the WM/terminal stay responsive.
-  It reuses `format_busy`.  The source can be `usb0` (USB mass storage), IDE,
-  SATA or eMMC; the target is any non-floppy block device with enough
-  sectors.  It installs the flashed image, not runtime `cmds-dev` pack
-  updates.
+- **Installer**: `install.c/h` — the privileged `install <target> [source|self]`
+  command clones the live NebOS image or writes the running image.  A clone
+  detects a source by the MBR plus the LBA-63 boot-sector signature (`NEBOS`
+  OEM string, `kernel_lba` 64, `pack_lba` 1088), computes the used length from
+  the end of P1..P3 (P4 is the data volume and is never cloned), allocates a
+  page buffer for the whole image and reads the source into RAM first (LBA
+  1..62 are synthesized as zeros instead of read).  It prepares/keeps P4,
+  writes the target from RAM in chunks with the source no longer active,
+  merges the target's P4 entry into the source MBR, writes the MBR **last**,
+  flushes and verifies by checksum before remounting the target.  A
+  source-read failure therefore leaves the target untouched.  The source read
+  keeps the BKL (a USB3 stick must stay active across chunks); the target
+  writes release it per chunk so the WM/terminal stay responsive.  It reuses
+  `format_busy`.  The source can be `usb0` (USB mass storage), IDE, SATA or
+  eMMC; the target is any non-floppy block device with enough sectors.
+  `install <target> self` writes the retained gzip boot image and the
+  kernel bundled at `/ramfs/install/kernel.bin` to P1/P2, patches the boot
+  sector's exact `kernel_sectors`, keeps an existing P4 and creates/formats
+  it when absent.  It refuses a raw runtime pack (`cmds-dev`/`net-cmds`/
+  `kernel-dev`); use `net-install` for that.  `target=auto` picks the only
+  non-USB NebOS disk.
 - **`/share`**: `share.c/h` — writable, page-backed in-memory filesystem for
   network uploads (the netbooted board may have no writable disk).  Entries
   are full relative paths (like the pack ramfs) plus explicit directory
@@ -528,6 +541,14 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `files` GUI.  `SYS_FS_STAT` exposes size/dir/read-only, and `sys_dirent`
   carries a display name (64 bytes) plus `raw_83` so FAT's space-padded
   fields and the name-string filesystems share one ABI.
+- **`/data`**: the installed disk's partition 4 is a writable FAT volume with
+  the label `NEBOSDATA`, created by `install`/`install self` at LBA 34816
+  (after P1 kernel, P2 rootfs pack and P3 ESP) and filling the rest of the
+  disk.  `vfs.c` mounts the first volume with that label as `/data` (not as
+  `<dev>p4`); the mount table records its source device so remount/unmount
+  works.  Files are 8.3 only until FAT LFN write support exists.  A reinstall
+  replaces P1..P3 and keeps P4; `net-install` never touches it.  The data
+  volume is not available on the live USB unless that stick was installed.
 - **SATA/AHCI**: `ahci.c/h` — polled AHCI driver (first controller only, PCI
   class `01:06:01`, no IRQs, no NCQ): port bring-up with COMRESET, IDENTIFY
   (shared `ata_identify_parse()` with `ata.c`) and READ/WRITE DMA EXT through
@@ -1165,7 +1186,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
 
 `cd [path]`, `ls [path]`, `cat <path>`, `save <path> <text>`, `rm <path>`,
 `mkdir <path>`, `rmdir <path>`, `hello`, `reboot`, `poweroff`,
-`display`, `cpuinfo`, `mouseinfo`, `gfxbench`, `lsusb`, `touchpad [rightclick off|tap|corner|both] [tapclick on|off] [scroll on|off|natural|traditional]`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `meminfo`, `top`, `sched`, `format <device> [size]` (binary units, e.g. `format ide0 100mb` or `1gb`; no size formats the whole disk), `install <target> [source]` (clone the live image; auto-detects the source, preferring `usb0`),
+`display`, `cpuinfo`, `mouseinfo`, `gfxbench`, `lsusb`, `touchpad [rightclick off|tap|corner|both] [tapclick on|off] [scroll on|off|natural|traditional]`, `lspci [id]`, `peek <addr> [len]`, `poke <addr> <val>`, `storage`, `meminfo`, `top`, `sched`, `format <device> [size]` (binary units, e.g. `format ide0 100mb` or `1gb`; no size formats the whole disk), `install <target> [source|self]` (clone the live image, or write the running image; auto-detects the source, preferring `usb0`; `auto` targets the only non-USB NebOS disk),
 `ps`, `renice <pid> <weight>`, `kill <pid>`, `idle [auto|hlt|c1e|c3|c6]`, `power`, `powerinfo`, `cpufreq [auto|min|max|off|<pstate>]`, `spin [ms]`, `help`, `clear`, `serial test`, `kexec`, `dbg`, `view <file>` (image viewer; PNG, baseline/progressive JPEG, animated GIF, BMP, TGA, PNM, ICO, PAM), `browser [url|file]` (disk app launcher), and `chat [config-path]` (Cloudron Chat client).
 
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
@@ -1260,10 +1281,13 @@ inflates it into page-allocated memory and mounts it read-only at `/ramfs`.
 The kernel image no longer contains it, so the pack no longer moves `_end`;
 the high-memory legacy path supports up to 2 MB (known issue 8).  The UI
 font subsets live in the pack under `fonts/`, the WiFi firmware under
-`firmware/`.  The floppy images use `cmds-lite.pack` (no firmware).
+`firmware/`, and the flat kernel at `install/kernel.bin` (full pack only;
+`install self` reads it).  The floppy images use `cmds-lite.pack` (no
+firmware and no `install/kernel.bin`, to stay inside 1.44 MB).
 
-`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`
+`rootfs.bin` is the floppy append (header + lite pack); `rootfs-full.bin`
+is the header + full gzip pack used by `make net-install`; `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:
@@ -1306,9 +1330,21 @@ couple of seconds on a wired LAN.  While a raw command owns the receiver
 (`netrecv`/`ping`/`netloop`), a TCP client or stack DNS/ARP wait is active,
 or a kexec is in progress, the auto-detect pauses.
 
+The persistent variant uses a `"KXI2"` header:
+`magic, entry, kernel_size, pack_size, checksum, target[16]`, with the kernel
+followed by a full `NBROOT1` rootfs image (`rootfs-full.bin`).  The board
+writes P2, then the P1 kernel, patches the P1 boot sector's exact
+`kernel_sectors`, verifies the result, acks `ACK_INSTALLED`, and jumps into
+the new kernel.  It never touches P4 or the MBR.  `NETINSTALL_TARGET ?= auto`
+picks the only non-USB NebOS disk; override it with `mmc0`, `sata0`, etc.
+`install self` refuses a raw runtime pack, so after `cmds-dev`/`net-cmds`/
+`kernel-dev` use `make net-install` to persist.
+The UEFI loader reads the same P1/P2 image, so no ESP rewrite is needed after
+the first full install of this layout.
+
 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 once; the new `make net-dev` is then atomic.
+(and it uses `KXN2`/`KXE2`/`KXI2`), so a board still running a pre-rootfs
+build must be flashed 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
@@ -1670,12 +1706,13 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
    compressed pack directly to `0x900000` with INT 15h AH=87h, so the cap is
    `PACK_MAX` 2 MB (raised from 1 MB for the WiFi firmware).  The browser and
    its ring-3 dependencies are compiled with function sections and linked
-   with `--gc-sections`; the current full pack with the UI fonts and the
-   1,036,312-byte WiFi firmware is ~982 KiB gzip (1005719 bytes), inflating
-   to ~2 MB, which adds roughly half a second to the boot; the floppy lite
-   pack is ~540 KiB.  `libdeflate-gzip -12` is still preferred.  Over
-   115200 serial a raw `cmds-dev` pack now takes about three minutes; use
-   `net-cmds`.
+   with `--gc-sections`; the current full pack with the UI fonts, the
+   1,036,312-byte WiFi firmware and `install/kernel.bin` is ~1.26 MiB gzip
+   (~1.33 MB), inflating to ~2.6 MB, which adds about half a second to the
+   boot; the floppy lite pack is ~680 KiB gzip and deliberately omits
+   `install/kernel.bin`.  `libdeflate-gzip -12` is still preferred.  Over
+   115200 serial a raw `cmds-dev` pack (2.6 MB) now takes about three
+   minutes; use `net-cmds`.
 9. 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).
@@ -1731,9 +1768,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 
 21. 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.  The command
-    pack is out of the kernel now; after the WiFi driver `_end` is ~0x88750,
-    ~31 KB below the `0x90000` wall, and the remaining limit is kernel
-    code/buffers only.
+    pack is out of the kernel now; after the WiFi/netmenu/install code `_end`
+    is ~0x8c000, ~16 KB below the `0x90000` wall, and the remaining limit is
+    kernel code/buffers only.
 
 22. Browser limits: JavaScript runs inline and external classic scripts on
     MuJS with the DOM surface described above (creation/mutation/selectors/
@@ -1927,17 +1964,21 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     The eMMC contains the stock ChromeOS image, so `format mmc0` is
     destructive; the board boots from USB.
 
-32. Installer limits: `install` clones the live image's used sectors (~17 MB
-    for `usb-efi.img`, ~8.5 MB for `usb.img`), so the rest of the target stays
-    unpartitioned and runtime `cmds-dev` pack updates are not part of the
-    clone.  The source must be visible as a `blkdev`: USB mass storage is
-    BOT/SCSI on EHCI and xHCI bulk, including direct USB3 root-port devices
-    (no UHCI/OHCI bulk path, no USB3 hubs, no UAS or 4Kn), so an unsupported
-    stick or controller leaves no source.  The installer needs ~17 MB of
-    free RAM for the source image (less for `usb.img`); a source-read failure
-    leaves the target untouched, while a target-write failure can leave it
-    incomplete (the MBR is written last).  IDE, SATA and eMMC sources work
-    without USB, but a target smaller than the image is refused.
+32. Installer limits: `install` clones P1..P3 from the live image (~17 MB
+    for `usb-efi.img`, ~8.5 MB for `usb.img`); the rest of the target becomes
+    the `/data` partition and is not part of the clone.  The source must be
+    visible as a `blkdev`: USB mass storage is BOT/SCSI on EHCI and xHCI bulk,
+    including direct USB3 root-port devices (no UHCI/OHCI bulk path, no USB3
+    hubs, no UAS or 4Kn), so an unsupported stick or controller leaves no
+    source.  The installer needs ~17 MB of free RAM for the source image
+    (less for `usb.img`); a source-read failure leaves the target untouched,
+    while a target-write failure can leave it incomplete (the MBR is written
+    last).  IDE, SATA and eMMC sources work without USB, but a target smaller
+    than the image (plus a 1 MiB data partition) is refused.
+    `install self` requires the full pack's `/ramfs/install/kernel.bin` and a
+    gzip boot image; it refuses a raw runtime pack (use `net-install`).
+    `net-install` needs an already-installed target (it does not create the
+    boot sector/ESP) and a host that can reach the board.
 
 33. USB tethering is RNDIS only (`rndis.c`): Android phones and QEMU
     `usb-net` work, but CDC-ECM/CDC-NCM (many iPhones and some Android
@@ -1988,7 +2029,7 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     throughput is CPU-bound.  The driver was written against the Linux iwlwifi
     sources without hardware in the loop; expect bring-up debugging on the
     Chromebook.  The kernel budget is tight: WiFi added ~20 KB, `_end` is
-    ~0x89710 (27 KB below the `0x90000` wall).
+    ~0x8c000 (~16 KB below the `0x90000` wall).
 
 36. Network menu limits (`ucmd_netmenu.c`): building the device list probes
     every driver, so opening the menu on a machine with an unconfigured USB
@@ -2003,3 +2044,23 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     cancel a connection attempt.  Saved credentials live in plaintext
     `/data/WIFI.CFG` (8.3), hold one network, and are retried at boot; use
     `wifi forget` to remove them.  There is no menu button for forgetting yet.
+
+37. `/data` limits: the data partition is a plain FAT volume, so file names
+    are 8.3 until `fat.c` gains LFN write support; there is no fsck, resize or
+    format-preserving migration.  It starts at the fixed LBA 34816 (after the
+    8 MB ESP), so a BIOS-only install leaves an unpartitioned ~8.5 MB gap and
+    the volume is not resized if the disk is later cloned to a larger one.
+    The first install formats the rest of the disk and is destructive; old
+    ChromeOS/other partitions are overwritten.  An existing `NEBOSDATA`
+    volume is identified by its label and fixed start LBA, so changing the
+    label makes the installer treat it as foreign and reformat it.  The
+    partition is mounted read/write by every user, like the rest of this
+    single-user OS.  `install self` writes the gzip image the system booted
+    from; a runtime raw pack replacement (`cmds-dev`/`net-cmds`/`kernel-dev`)
+    is not retained for self-update, and `net-install` must be used instead.
+    The initial install of this layout must still come from USB so the ESP
+    gets the disk-reading `BOOTX64.EFI`; later updates do not touch the ESP.
+    A power loss during `install self`/`net-install` can leave P1/P2 in an
+    intermediate state: P2 is written first, then the kernel, then the boot
+    sector; there is no A/B slot or journal.  A later full `install` repairs
+    it, and P4 is never touched by these updates.

Разлика између датотеке није приказан због своје велике величине
+ 26 - 19
Makefile


+ 6 - 20
boot.S

@@ -14,27 +14,13 @@ _start:
 	.byte 0xEB
 	.byte loader_start - _start - 2
 	.byte 0x90
-	/* ---- BPB (BIOS Parameter Block) ---- */
+	/* OEM name, then a deliberately empty BPB.  This sector is not a FAT
+	 * volume: the old FAT-shaped BPB made VFS mount the raw kernel
+	 * partition as a fake FAT32 volume, and UEFI firmware tried to mount
+	 * it.  The BIOS only needs the fields below (kernel_sectors at 0x3E,
+	 * kernel_lba, pack_lba), so bytes 0x0B..0x3D stay zero. */
 	.ascii "NEBOS   "        /* OEM name */
-	.word 512                /* bytes per sector */
-	.byte 4                  /* sectors per cluster */
-	.word 1                  /* reserved sectors */
-	.byte 2                  /* number of FATs */
-	.word 512                /* root entries */
-	.word 0                  /* total sectors 16-bit (use 32-bit) */
-	.byte 0xF8               /* media descriptor (hard disk) */
-	.word 128                /* sectors per FAT */
-	.word 63                 /* sectors per track */
-	.word 128                /* number of heads */
-	.long 0                  /* hidden sectors */
-	.long 131072             /* total sectors 32-bit */
-	/* ---- Extended BPB ---- */
-	.byte 0x80               /* drive number */
-	.byte 0x00               /* reserved */
-	.byte 0x29               /* boot signature */
-	.long 0x12345678         /* volume ID */
-	.ascii "NEBOS      "     /* volume label */
-	.ascii "FAT16   "        /* filesystem type */
+	.space 0x3E - 0x0B, 0
 
 	.org 0x3E
 kernel_sectors:

+ 35 - 24
docs/boot.html

@@ -197,15 +197,18 @@
 		<ol>
 			<li>Selects a GOP mode with BGRX or RGBX 32bpp, width at most 1280, <code>pitch × height</code> at most 8 MB, and a framebuffer below 4 GB.</li>
 			<li>Calls <code>GetMemoryMap</code> and converts the firmware map into the kernel's E820 format at <code>0x1000</code>.</li>
-			<li>Stages the kernel image below 4 GB and writes the boot-info and framebuffer blocks.</li>
-			<li>Allocates the rootfs pack buffer as loader data below the 512 MB identity window while boot services are still live, copies the embedded pack into it, calls <code>ExitBootServices</code>, then copies a 192-byte trampoline to <code>0x800</code>.</li>
+			<li>Locates the parent whole-disk Block I/O handle from the ESP device path, reads the P1 boot sector and P2 <code>NBROOT1</code> header, and stages the kernel below 4 GB and the gzip pack below the 512 MB identity window. The kernel/pack embedded in <code>BOOTX64.EFI</code> are a fallback only.</li>
+			<li>Writes the boot-info and framebuffer blocks, calls <code>ExitBootServices</code>, then copies a 192-byte trampoline to <code>0x800</code>.</li>
 			<li>Leaves IA-32e mode (clears <code>CR0.PG</code>, <code>EFER.LME</code>, <code>CR4.PAE/PGE/PCIDE/SMEP/SMAP/LA57</code>), copies the kernel to <code>0x7E00</code>, and jumps to <code>kernel_entry</code>.</li>
 		</ol>
 		<p>
-			<span class="file">efi/payload.S</span> embeds the compressed
-			kernel, the gzip rootfs pack and the trampoline inside
-			<code>BOOTX64.EFI</code>, so the ESP is self-contained. Secure Boot
-			must be off; the loader is unsigned.
+			<span class="file">efi/payload.S</span> still embeds the kernel,
+			the gzip rootfs pack and the trampoline inside
+			<code>BOOTX64.EFI</code>, but the installed P1/P2 image wins when it
+			is valid. That makes the ESP a one-time artifact: later
+			<code>install</code>/<code>net-install</code>/<code>install self</code>
+			updates touch only P1/P2, and UEFI and legacy boots run the same
+			build. Secure Boot must be off; the loader is unsigned.
 		</p>
 		<div class="warn">
 			<p><strong>No GOP, no video.</strong> If the loader cannot satisfy the
@@ -283,7 +286,7 @@
 			and rootfs. The floppy images append the <em>lite</em> pack
 			(<code>rootfs.bin</code> from <code>cmds-lite.pack</code>, everything
 			but <code>firmware/*</code>) so they still fit 1.44 MB. The USB
-			rootfs partition carries the full pack (about 1 MB gzip with the
+			rootfs partition carries the full pack (about 1.3 MB gzip with the
 			WiFi firmware), which now ends past the first 1.44 MB: a BIOS that
 			boots the stick as a CHS-only floppy can no longer read all of it.
 		</p>
@@ -330,10 +333,9 @@
 				<text class="svg-tiny" x="160" y="334">Schematic, not to scale. Floppy images carry the lite pack (no firmware); the USB rootfs carries the full pack.</text>
 			</svg>
 			<figcaption>
-				<strong>Three images, one source tree.</strong> The dual-boot image
-				clears one BPB byte in the fake FAT16 header of partition 1 so UEFI
-				firmware does not mount the raw kernel blob as a volume and overwrite
-				it.
+				<strong>Three images, one source tree.</strong> Partition 1 has
+				an empty BPB, so neither UEFI firmware nor VFS mounts the raw
+				kernel blob as a FAT volume and overwrites it.
 			</figcaption>
 		</figure>
 
@@ -355,17 +357,25 @@
 		<h3>Installing to a disk</h3>
 		<p>
 			The live system's <code>install &lt;target&gt; [source]</code>
-			command clones the image's used sectors (MBR through the end of the
-			last partition) onto a target disk. It finds the source by the MBR
-			plus the LBA-63 boot-sector signature and prefers <code>usb0</code>,
-			the USB mass-storage view of the live stick. It reads the whole
-			source image into RAM first (the unused LBA 1..62 gap is synthesized
-			as zeros), then writes the target from RAM, writes the MBR last,
-			flushes and verifies it. A source-read failure leaves the target
-			untouched. A clone of <code>usb-efi.img</code> keeps the legacy MBR
-			chain and the ESP/UEFI chain; a clone of <code>usb.img</code> is
-			BIOS-only. The rest of the target stays unpartitioned, and runtime
-			<code>cmds-dev</code> pack updates are not part of the cloned image.
+			command clones P1..P3 (MBR through the end of the ESP) onto a target
+			disk. It finds the source by the MBR plus the LBA-63 boot-sector
+			signature and prefers <code>usb0</code>, the USB mass-storage view
+			of the live stick. It reads the whole source image into RAM first
+			(the unused LBA 1..62 gap is synthesized as zeros), prepares or
+			keeps partition 4, writes the target from RAM, merges the data
+			entry into the source MBR, writes the MBR last, flushes and verifies
+			it. A source-read failure leaves the target untouched.
+		</p>
+		<p>
+			Partition 4 starts at LBA 34816 and fills the rest of the disk as a
+			writable FAT volume labelled <code>NEBOSDATA</code>, mounted at
+			<code>/data</code>. A first install creates and formats it; a later
+			install or <code>install &lt;target&gt; self</code> keeps it. The
+			data volume is not part of the flashed image, so <code>usb.img</code>
+			and <code>usb-efi.img</code> remain 9 MB and 17 MB. A clone of
+			<code>usb-efi.img</code> keeps the legacy MBR chain and the ESP/UEFI
+			chain; a clone of <code>usb.img</code> is BIOS-only. Runtime
+			<code>cmds-dev</code> pack updates are not part of the clone.
 		</p>
 	</section>
 
@@ -405,7 +415,7 @@
 		<h2>Failure modes to know</h2>
 		<ul>
 			<li><strong>No VBE mode.</strong> The legacy stage cannot produce a framebuffer; the kernel may start headless or the boot stops in the stage.</li>
-			<li><strong>Pack larger than the cap.</strong> The legacy BIOS path caps the compressed pack at 2 MB <code>PACK_MAX</code> (<code>CMDS_GZ_MAX</code> in the Makefile); UEFI and kexec can carry larger packs up to the 8 MB ramfs cap. The WiFi firmware makes the full pack about 1 MB, and inflating it adds roughly half a second to the boot.</li>
+			<li><strong>Pack larger than the cap.</strong> The legacy BIOS path caps the compressed pack at 2 MB <code>PACK_MAX</code> (<code>CMDS_GZ_MAX</code> in the Makefile); UEFI and kexec can carry larger packs up to the 8 MB ramfs cap. The WiFi firmware and the bundled install kernel make the full pack about 1.3 MB, and inflating it adds roughly half a second to the boot.</li>
 			<li><strong>PIT not ticking.</strong> <code>kernel_main</code> prints <code>*** PIT NOT TICKING ***</code> and halts; no process scheduling is possible.</li>
 			<li><strong>UEFI Secure Boot.</strong> The unsigned <code>BOOTX64.EFI</code> is rejected before any NebOS code runs.</li>
 			<li><strong>Stale boot info.</strong> The checksum makes an invalid <code>0x500</code> block read as absent rather than as a bogus pack or RSDP.</li>
@@ -426,7 +436,8 @@
 			<li><code>kexec.c</code>, <code>kexec_tramp.S</code> — serial reload</li>
 			<li><code>netkexec.c</code> — network reload</li>
 			<li><code>kernel.c</code> — init order</li>
-			<li><code>install.c</code> — live-image clone installer</li>
+			<li><code>install.c</code>, <code>install.h</code> — clone, self-update and net-install writer</li>
+			<li><code>rootfs.h</code> — installed image geometry</li>
 			<li><code>kernel.ld</code> — image limits</li>
 			<li><code>Makefile</code> — image sizes and layout rules</li>
 		</ul>

+ 16 - 2
docs/development.html

@@ -172,7 +172,8 @@
 					<tr><td><code>make rootfs</code></td><td>Rebuild <code>cmds.img</code> only.</td></tr>
 					<tr><td><code>make nowm</code></td><td>Diagnostic kernel with the WM disabled; the boot console stays visible.</td></tr>
 					<tr><td><code>make dev</code> / <code>kernel-dev</code></td><td>Serial reload of kernel + rootfs, or kernel only (keep the running rootfs).</td></tr>
-					<tr><td><code>make net-dev</code> / <code>kernel-net-dev</code></td><td>Network reload of kernel + rootfs, or kernel only.</td></tr>
+					<tr><td><code>make net-dev</code> / <code>kernel-net-dev</code></td><td>RAM-only network reload of kernel + rootfs, or kernel only.</td></tr>
+					<tr><td><code>make net-install</code></td><td>Persist kernel + full rootfs to P1/P2 over UDP, keeping <code>/data</code>; <code>NETINSTALL_TARGET=auto</code> selects the internal NebOS disk.</td></tr>
 					<tr><td><code>make cmds-dev</code> / <code>net-cmds</code></td><td>Replace <code>/ramfs</code> at runtime over serial or UDP.</td></tr>
 					<tr><td><code>make monitor</code> / <code>netmon</code></td><td>Open the serial console, or listen to the UDP console mirror.</td></tr>
 					<tr><td><code>make flash</code></td><td>Write the dual-boot image to a USB stick (needs mingw and prompts for the device).</td></tr>
@@ -231,9 +232,22 @@
 			every datagram with the next expected offset and verifies a 32-bit
 			checksum before jumping.
 		</p>
-		<pre><code>make net-dev                    # kernel + rootfs to 192.168.178.240
+		<p>
+			<code>make net-install</code> uses <code>KXI2</code> instead. It
+			sends the kernel plus a full <code>NBROOT1</code> rootfs image
+			(<code>rootfs-full.bin</code>) and a 16-byte target name; the board
+			writes P2, the P1 kernel, patches the exact
+			<code>kernel_sectors</code>, verifies the result, acks
+			<code>ACK_INSTALLED</code> and jumps. It never touches P4
+			(<code>/data</code>) or the MBR, so it is the persistent counterpart
+			to the RAM-only <code>net-dev</code>. <code>NETINSTALL_TARGET=auto</code>
+			selects the only non-USB NebOS disk.
+		</p>
+		<pre><code>make net-dev                    # RAM-only kernel + rootfs to 192.168.178.240
 make net-dev BOARD_IP=192.168.178.42
 make kernel-net-dev             # kernel only; keep the rootfs
+make net-install                # persist kernel + full rootfs, keep /data
+make net-install NETINSTALL_TARGET=sata0
 make net-cmds                   # replace /ramfs only</code></pre>
 		<p>
 			The board must be idle at a shell prompt. The WM loop answers

+ 36 - 8
docs/filesystems.html

@@ -4,7 +4,7 @@
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>Storage &amp; Filesystems | NebOS Architecture</title>
-<meta name="description" content="NebOS filesystems: the VFS fs_ops layer, FAT12/16/32, the command-pack ramfs pipeline, the writable /share filesystem and the WebDAV view of the VFS.">
+<meta name="description" content="NebOS filesystems: the VFS fs_ops layer, FAT12/16/32, the command-pack ramfs pipeline, the writable /share and persistent /data filesystems, and the WebDAV view of the VFS.">
 <link rel="icon" href="favicon.svg" type="image/svg+xml">
 <link rel="stylesheet" href="style.css">
 </head>
@@ -38,12 +38,15 @@
 			The kernel keeps filesystem policy thin: a mount table and an
 			<code>fs_ops</code> vtable. FAT provides persistent read/write storage,
 			the command-pack ramfs is a read-only system image, and
-			<code>/share</code> is a writable RAM filesystem for network uploads.
+			<code>/share</code> is a writable RAM filesystem for network uploads;
+			an installed disk adds a persistent FAT data volume at
+			<code>/data</code>.
 		</p>
 		<div class="meta">
 			<span class="pill">FAT12/16/32</span>
 			<span class="pill">read-only ramfs</span>
 			<span class="pill">/share · 16 MB</span>
+			<span class="pill">/data · persistent</span>
 			<span class="pill">WebDAV</span>
 		</div>
 	</div>
@@ -55,6 +58,7 @@
 			<li><a href="#fat">FAT</a></li>
 			<li><a href="#ramfs">ramfs &amp; the command pack</a></li>
 			<li><a href="#share">/share</a></li>
+			<li><a href="#data">/data</a></li>
 			<li><a href="#sysfs">File service ABI</a></li>
 			<li><a href="#webdav">WebDAV</a></li>
 			<li><a href="#capacity">Capacity chart</a></li>
@@ -255,7 +259,9 @@
 					<tr><td><code>cmds_pack_gz.bin</code></td><td>gzip</td><td>Compressed pack; <code>libdeflate-gzip -12</code> when available.</td></tr>
 					<tr><td><code>rootfs.bin</code></td><td><code>NBROOT1</code> + gzip</td><td>Floppy append (header + pack).</td></tr>
 					<tr><td><code>cmds.img</code></td><td><code>NBROOT1</code> padded to 8 MB</td><td>USB rootfs partition image.</td></tr>
+					<tr><td><code>rootfs-full.bin</code></td><td><code>NBROOT1</code> + full gzip</td><td>Network persistent-install payload.</td></tr>
 					<tr><td><code>/share</code></td><td>RAM entries</td><td>Writable upload target; not part of the pack.</td></tr>
+					<tr><td><code>/data</code></td><td>FAT32/16, label <code>NEBOSDATA</code></td><td>Partition 4 on the installed disk; persistent user files.</td></tr>
 				</tbody>
 			</table>
 		</div>
@@ -286,6 +292,26 @@
 		</ul>
 	</section>
 
+	<section id="data">
+		<h2>/data</h2>
+		<p>
+			The installer creates a fourth partition after the 8 MB ESP and
+			formats it as FAT with the label <code>NEBOSDATA</code>. It fills
+			the rest of the disk and is mounted at <code>/data</code>; the VFS
+			recognises the label in the FAT boot sector, not the device name, so
+			the path is stable on <code>mmc0</code>, <code>sata0</code> or
+			<code>ide0</code>.
+		</p>
+		<ul>
+			<li><strong>Persistent.</strong> A reinstall or <code>install &lt;target&gt; self</code> replaces the OS partitions (P1 kernel, P2 rootfs pack, P3 ESP) and keeps P4. <code>net-install</code> writes only P1/P2.</li>
+			<li><strong>First install is destructive.</strong> Creating P4 formats the rest of the target disk; an existing <code>NEBOSDATA</code> volume is detected and left untouched.</li>
+			<li><strong>8.3 names.</strong> The FAT driver creates and reads short names only; long names need VFAT LFN write support.</li>
+			<li><strong>WiFi credentials.</strong> A successful WiFi connect stores the SSID/passphrase in <code>WIFI.CFG</code>; the boot-time <code>wifi autoconnect</code> helper reads it. The file is plaintext and 8.3, like the rest of the volume.</li>
+			<li><strong>Fixed start.</strong> P4 starts at LBA 34816. A BIOS-only install leaves the space between P2 and P4 unpartitioned, and a later disk clone does not resize P4.</li>
+			<li><strong>No fsck.</strong> Like the other FAT mounts, the volume has no journal, repair tool or permissions; it is writable by every process.</li>
+		</ul>
+	</section>
+
 	<section id="sysfs">
 		<h2>File service ABI</h2>
 		<p>
@@ -318,7 +344,8 @@
 			built on the kernel TCP listener. It serves the VFS:
 			<code>/</code> lists mounts, <code>/ramfs</code> is read-only, FAT
 			mounts accept only names that round-trip to 8.3, and
-			<code>/share</code> is the writable target.
+			<code>/share</code> and <code>/data</code> are the writable
+			targets (RAM and persistent respectively).
 		</p>
 		<ul>
 			<li><strong>Concurrency.</strong> One active connection plus a small backlog (three backlog/closing slots in the current TCP server). Parallel clients queue with a zero window until the active request finishes.</li>
@@ -338,7 +365,7 @@
 	<section id="capacity">
 		<h2>Capacity chart</h2>
 		<figure class="figure">
-			<div class="bars" role="img" aria-label="Capacity limits: /share 16 MB, ramfs decompressed pack 8 MB, USB rootfs image 8 MB, legacy compressed pack 1 MB. Bar widths are relative to 16 MB.">
+			<div class="bars" role="img" aria-label="Capacity limits: /share 16 MB, ramfs decompressed pack 8 MB, USB rootfs image 8 MB, current legacy compressed pack about 1.3 MB. Bar widths are relative to 16 MB.">
 				<div class="bar-row">
 					<span class="bar-label">/share</span>
 					<div class="bar-track"><span class="bar-fill green" style="--w:100%"></span></div>
@@ -356,8 +383,8 @@
 				</div>
 				<div class="bar-row">
 					<span class="bar-label">legacy pack</span>
-					<div class="bar-track"><span class="bar-fill" style="--w:6.25%"></span></div>
-					<span class="bar-value">1 MB</span>
+					<div class="bar-track"><span class="bar-fill" style="--w:8.1%"></span></div>
+					<span class="bar-value">~1.3 MB</span>
 				</div>
 			</div>
 			<figcaption>
@@ -373,7 +400,7 @@
 		<ul>
 			<li><strong>Whole-file service.</strong> Commands read a complete file into memory; there is no stream API for general files.</li>
 			<li><strong>FAT names.</strong> No long filename creation or lookup. WebDAV refuses names that cannot be represented exactly in 8.3.</li>
-			<li><strong>ramfs is read-only.</strong> Writes return <code>SYS_EROFS</code>; use <code>/share</code> for mutable data.</li>
+			<li><strong>ramfs is read-only.</strong> Writes return <code>SYS_EROFS</code>; use <code>/share</code> for volatile data and <code>/data</code> for persistent files.</li>
 			<li><strong>Pack volatile at runtime.</strong> Runtime pack replacement is RAM only; a cold boot uses the flashed rootfs.</li>
 			<li><strong>Directory listing cache.</strong> Only the first 64 entries of the last FAT directory are cached; a very large directory still costs a walk.</li>
 		</ul>
@@ -392,7 +419,8 @@
 			<li><code>ucmd_websrv.c</code> — HTTP/WebDAV</li>
 			<li><code>fatdetect.c</code>, <code>fatformat.c</code> — format</li>
 			<li><code>usbmsc.c</code>, <code>usbmsc.h</code> — USB mass-storage block devices</li>
-			<li><code>install.c</code> — live-image clone installer</li>
+			<li><code>install.c</code>, <code>install.h</code> — clone, self-update and net-install writer</li>
+			<li><code>rootfs.h</code> — installed image geometry and NBROOT1 header</li>
 		</ul>
 	</section>
 </main>

+ 1 - 1
docs/index.html

@@ -207,7 +207,7 @@
 			</a>
 			<a class="card" href="filesystems.html">
 				<h3>Storage &amp; filesystems</h3>
-				<p>VFS vtable, FAT12/16/32, the ramfs pack pipeline, <code>/share</code> and the WebDAV view of the VFS.</p>
+				<p>VFS vtable, FAT12/16/32, the ramfs pack pipeline, persistent <code>/data</code>, <code>/share</code> and the WebDAV view of the VFS.</p>
 				<span class="card-tag">filesystems.html</span>
 			</a>
 			<a class="card" href="graphics.html">

+ 11 - 5
docs/network.html

@@ -369,11 +369,17 @@
 		</p>
 		<p>
 			<span class="file">netkexec.c</span> receives a new kernel/rootfs over
-			UDP port 19288 using <code>KXN2</code>, and a runtime command pack
-			using <code>KXNP</code>. Datagrams are acknowledged with the next
-			expected offset and verified with a 32-bit checksum before the jump
-			or pack swap. On the host, <code>make netmon</code> and
-			<code>make net-dev</code> wrap these protocols.
+			UDP port 19288 using <code>KXN2</code>, a runtime command pack using
+			<code>KXNP</code>, and a persistent install using <code>KXI2</code>.
+			Datagrams are acknowledged with the next expected offset and verified
+			with a 32-bit checksum. <code>KXN2</code>/<code>KXNP</code> jump or
+			swap only in RAM. <code>KXI2</code> carries a 16-byte target name;
+			the board writes P2, then the P1 kernel, patches the exact
+			<code>kernel_sectors</code>, verifies the disk, sends
+			<code>ACK_INSTALLED</code>/<code>ACK_INSTALL_FAIL</code>, and jumps.
+			It never touches P4 (<code>/data</code>) or the MBR. On the host,
+			<code>make netmon</code>, <code>make net-dev</code> and
+			<code>make net-install</code> wrap these protocols.
 		</p>
 	</section>
 

+ 12 - 7
docs/userland.html

@@ -283,13 +283,18 @@
 		</div>
 		<p>
 			<code>format &lt;device&gt; [size]</code> creates a FAT filesystem.
-			<code>install &lt;target&gt; [source]</code> clones the live NebOS
-			image to a target disk: it auto-detects a source whose MBR and LBA-63
-			boot sector carry the NebOS signature (preferring <code>usb0</code>),
-			reads the source image into RAM, then writes the target from RAM,
-			writes the MBR last and verifies the result. A source-read failure
-			leaves the target untouched. It installs the flashed image, so both
-			the BIOS and UEFI boot paths are cloned as-is.
+			<code>install &lt;target&gt; [source|self]</code> installs NebOS.
+			With a source it auto-detects a disk whose MBR and LBA-63 boot
+			sector carry the NebOS signature (preferring <code>usb0</code>),
+			reads P1..P3 into RAM, creates or keeps the <code>NEBOSDATA</code>
+			partition at LBA 34816, writes the target from RAM, merges the data
+			entry into the MBR, writes the MBR last and verifies the result. A
+			source-read failure leaves the target untouched. <code>self</code>
+			writes the running gzip pack and its bundled kernel without a USB
+			stick; <code>auto</code> picks the only non-USB NebOS disk. The
+			data partition survives both forms, while the P1..P3 OS image and
+			the app pack are replaced. Both the BIOS and UEFI paths are cloned
+			on a full install; a later self-update leaves the ESP alone.
 		</p>
 	</section>
 

+ 67 - 3
efi/efi.h

@@ -51,6 +51,53 @@ typedef struct {
 	void    *VendorTable;
 } EFI_CONFIGURATION_TABLE;
 
+/* EFI_DEVICE_PATH_PROTOCOL: type/subtype/2-byte little-endian length. */
+typedef struct {
+	UINT8 Type;
+	UINT8 SubType;
+	UINT8 Length[2];
+} __attribute__((packed)) EFI_DEVICE_PATH_PROTOCOL;
+
+typedef UINT64 EFI_LBA;
+
+typedef struct {
+	UINT32 MediaId;
+	BOOLEAN RemovableMedia;
+	BOOLEAN MediaPresent;
+	BOOLEAN LogicalPartition;
+	BOOLEAN ReadOnly;
+	BOOLEAN WriteCaching;
+	UINT32 BlockSize;
+	UINT32 IoAlign;
+	EFI_LBA LastBlock;
+} EFI_BLOCK_IO_MEDIA;
+
+typedef struct {
+	UINT64 Revision;
+	EFI_BLOCK_IO_MEDIA *Media;
+	EFI_STATUS (*Reset)(void *This, BOOLEAN ExtendedVerification);
+	EFI_STATUS (*ReadBlocks)(void *This, UINT32 MediaId, EFI_LBA Lba,
+				 UINTN BufferSize, void *Buffer);
+	void *WriteBlocks;
+	void *FlushBlocks;
+} EFI_BLOCK_IO_PROTOCOL;
+
+typedef struct {
+	UINT32 Revision;
+	EFI_HANDLE ParentHandle;
+	void *SystemTable;
+	EFI_HANDLE DeviceHandle;
+	EFI_DEVICE_PATH_PROTOCOL *FilePath;
+	void *Reserved;
+	UINT32 LoadOptionsSize;
+	void *LoadOptions;
+	void *ImageBase;
+	UINT64 ImageSize;
+	UINT32 ImageCodeType;
+	UINT32 ImageDataType;
+	void *Unload;
+} EFI_LOADED_IMAGE_PROTOCOL;
+
 typedef struct {
 	void *Reset;
 	EFI_STATUS (*OutputString)(void *This, CHAR16 *String);
@@ -88,11 +135,16 @@ typedef struct {
 	void *InstallProtocolInterface;
 	void *ReinstallProtocolInterface;
 	void *UninstallProtocolInterface;
-	void *HandleProtocol;
+	EFI_STATUS (*HandleProtocol)(EFI_HANDLE Handle, EFI_GUID *Protocol,
+				     void **Interface);
 	void *Reserved;
 	void *RegisterProtocolNotify;
-	void *LocateHandle;
-	void *LocateDevicePath;
+	EFI_STATUS (*LocateHandle)(UINT32 SearchType, EFI_GUID *Protocol,
+				   void *SearchKey, UINTN *BufferSize,
+				   EFI_HANDLE *Buffer);
+	EFI_STATUS (*LocateDevicePath)(EFI_GUID *Protocol,
+				       EFI_DEVICE_PATH_PROTOCOL **DevicePath,
+				       EFI_HANDLE *Device);
 	void *InstallConfigurationTable;
 	void *LoadImage;
 	void *StartImage;
@@ -191,5 +243,17 @@ static const EFI_GUID efi_acpi10_guid = {
 	0xEB9D2D30u, 0x2D88, 0x11D3,
 	{ 0x9A, 0x16, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D }
 };
+static const EFI_GUID efi_loaded_image_guid = {
+	0x5B1B31A1u, 0x9562, 0x11D2,
+	{ 0x8E, 0x3F, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }
+};
+static const EFI_GUID efi_device_path_guid = {
+	0x09576E91u, 0x6D3F, 0x11D2,
+	{ 0x8E, 0x39, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }
+};
+static const EFI_GUID efi_block_io_guid = {
+	0x964E5B21u, 0x6459, 0x11D2,
+	{ 0x8E, 0x39, 0x00, 0xA0, 0xC9, 0x69, 0x72, 0x3B }
+};
 
 #endif

+ 206 - 24
efi/main.c

@@ -526,6 +526,136 @@ static void copy_trampoline(void)
 	com1_puts("\n");
 }
 
+/* ---- disk boot image ---------------------------------------------------- */
+
+static UINT8 io_buf[512] __attribute__((aligned(4096)));
+
+static UINT32 rd16(const UINT8 *p)
+{
+	return (UINT32)p[0] | ((UINT32)p[1] << 8);
+}
+
+static UINT32 rd32(const UINT8 *p)
+{
+	return (UINT32)p[0] | ((UINT32)p[1] << 8) |
+	       ((UINT32)p[2] << 16) | ((UINT32)p[3] << 24);
+}
+
+static int disk_read(EFI_BLOCK_IO_PROTOCOL *bio, UINT64 lba, void *buf,
+		     UINT32 bytes)
+{
+	if (!bio || !bio->Media || !bytes || (bytes & 511u))
+		return -1;
+	if (bio->Media->BlockSize != 512)
+		return -1;
+	if (lba > bio->Media->LastBlock)
+		return -1;
+	if ((UINT64)(bytes / 512u) > bio->Media->LastBlock - lba + 1)
+		return -1;
+	if (bio->ReadBlocks(bio, bio->Media->MediaId, lba, bytes, buf))
+		return -1;
+	return 0;
+}
+
+/* The ESP is a partition; the kernel and rootfs live at absolute disk LBAs,
+ * so strip the HD node and locate the parent whole-disk Block I/O handle. */
+static EFI_BLOCK_IO_PROTOCOL *disk_block_io(EFI_HANDLE image,
+					    EFI_BOOT_SERVICES *bs)
+{
+	static UINT8 path[512];
+	EFI_LOADED_IMAGE_PROTOCOL *li = 0;
+	EFI_DEVICE_PATH_PROTOCOL *dp = 0, *p, *last = 0, *tp;
+	EFI_BLOCK_IO_PROTOCOL *bio = 0;
+	EFI_HANDLE handle = 0;
+	UINTN len = 0, total = 0;
+	UINT32 i;
+	EFI_STATUS st;
+
+	if (bs->HandleProtocol(image, (EFI_GUID *)&efi_loaded_image_guid,
+			       (void **)&li) || !li || !li->DeviceHandle)
+		return 0;
+	if (bs->HandleProtocol(li->DeviceHandle,
+			       (EFI_GUID *)&efi_device_path_guid,
+			       (void **)&dp) || !dp)
+		return 0;
+
+	p = dp;
+	for (;;) {
+		UINTN nl = (UINTN)p->Length[0] | ((UINTN)p->Length[1] << 8);
+
+		if (p->Type == 0x7F && p->SubType == 0xFF)
+			break;
+		if (nl < 4 || total + nl > sizeof(path))
+			return 0;
+		last = p;
+		total += nl;
+		p = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)p + nl);
+	}
+	if (!last || last->Type != 0x04 || last->SubType != 0x01)
+		return 0;
+
+	len = (UINTN)((UINT8 *)last - (UINT8 *)dp);
+	if (len + 4 > sizeof(path))
+		return 0;
+	for (i = 0; i < len; i++)
+		path[i] = ((UINT8 *)dp)[i];
+	path[len] = 0x7F;
+	path[len + 1] = 0xFF;
+	path[len + 2] = 4;
+	path[len + 3] = 0;
+
+	tp = (EFI_DEVICE_PATH_PROTOCOL *)path;
+	st = bs->LocateDevicePath((EFI_GUID *)&efi_block_io_guid, &tp, &handle);
+	if (st || !handle)
+		return 0;
+	if (bs->HandleProtocol(handle, (EFI_GUID *)&efi_block_io_guid,
+			       (void **)&bio) || !bio || !bio->Media)
+		return 0;
+	if (bio->Media->LogicalPartition || bio->Media->BlockSize != 512)
+		return 0;
+	return bio;
+}
+
+/* Read P1's boot sector and P2's NBROOT1 header.  Returns the kernel size in
+ * bytes and the pack size, or 0 when this is not a NebOS layout. */
+static int disk_sizes(EFI_BLOCK_IO_PROTOCOL *bio, UINT32 *kernel_lba,
+		      UINT32 *kernel_bytes, UINT32 *pack_lba,
+		      UINT32 *pack_bytes)
+{
+	UINT32 ks, kl, pl, pb;
+
+	if (disk_read(bio, 63, io_buf, 512) != 0)
+		return 0;
+	if (io_buf[510] != 0x55 || io_buf[511] != 0xAA)
+		return 0;
+	if (io_buf[3] != 'N' || io_buf[4] != 'E' || io_buf[5] != 'B' ||
+	    io_buf[6] != 'O' || io_buf[7] != 'S')
+		return 0;
+	ks = rd16(io_buf + 0x3E);
+	kl = rd16(io_buf + 0x40);
+	pl = rd32(io_buf + 0x42);
+	if (!ks || kl < 1 || pl <= kl || kl + ks > pl)
+		return 0;
+	if ((UINT64)ks * 512u > KERNEL_MAX)
+		return 0;
+
+	if (disk_read(bio, pl, io_buf, 512) != 0)
+		return 0;
+	if (io_buf[0] != 'N' || io_buf[1] != 'B' || io_buf[2] != 'R' ||
+	    io_buf[3] != 'O' || io_buf[4] != 'O' || io_buf[5] != 'T' ||
+	    io_buf[6] != '1' || rd32(io_buf + 8) != 1)
+		return 0;
+	pb = rd32(io_buf + 12);
+	if (!pb || pb > 0x800000u || !(rd32(io_buf + 20) & 0x1u))
+		return 0;
+
+	*kernel_lba = kl;
+	*kernel_bytes = ks * 512u;
+	*pack_lba = pl;
+	*pack_bytes = pb;
+	return 1;
+}
+
 /* ---- entry ------------------------------------------------------------- */
 
 EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
@@ -539,8 +669,12 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
 	UINTN map_size, map_key = 0, desc_size = 0;
 	UINT32 desc_ver = 0;
 	UINT32 rsdp;
-	UINT32 kernel_size;
-	UINT32 pack_addr = 0, pack_size;
+	EFI_BLOCK_IO_PROTOCOL *bio;
+	UINT32 kernel_size, pack_size;
+	UINT32 embed_ksize, embed_psize, disk_ksize = 0, disk_psize = 0;
+	UINT32 disk_kl = 0, disk_pl = 0, alloc_ksize, alloc_psize;
+	UINT32 pack_addr = 0;
+	int disk_ok = 0, from_disk = 0;
 	UINTN pages, i, tries;
 	EFI_STATUS status;
 
@@ -553,16 +687,20 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
 
 	bs->SetWatchdogTimer(0, 0, 0, 0);
 
-	kernel_size = (UINT32)(kernel_image_end - kernel_image_start);
-	if (kernel_size == 0 || kernel_size > KERNEL_MAX)
+	embed_ksize = (UINT32)(kernel_image_end - kernel_image_start);
+	embed_psize = (UINT32)(pack_image_end - pack_image_start);
+	if (embed_ksize == 0 || embed_ksize > 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);
+	if (embed_psize == 0 || embed_psize > 0x400000u)
+		fail_before(st, "rootfs image size invalid");
+	kernel_size = embed_ksize;
+	pack_size = embed_psize;
+	com1_puts("nebos-efi: embedded kernel ");
+	com1_dec(embed_ksize);
 	com1_puts(" bytes, entry 0x");
 	com1_hex((UINT64)KERNEL_ENTRY, 8);
 	com1_puts(", rootfs ");
-	com1_dec(pack_size);
+	com1_dec(embed_psize);
 	com1_puts(" bytes\n");
 
 	/* Video: prefer BGRX (the byte order the kernel's shadow buffer uses);
@@ -599,38 +737,82 @@ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *st)
 		com1_puts("\n");
 	}
 
+	/* Prefer the installed P1/P2 image so persistent updates and `install`
+	 * take effect for both boot paths.  A missing/corrupt disk image falls
+	 * back to the kernel/rootfs embedded in BOOTX64.EFI. */
+	bio = disk_block_io(image, bs);
+	if (bio && disk_sizes(bio, &disk_kl, &disk_ksize, &disk_pl,
+			      &disk_psize)) {
+		disk_ok = 1;
+		com1_puts("nebos-efi: disk image: kernel ");
+		com1_dec(disk_ksize);
+		com1_puts(" bytes at lba ");
+		com1_dec(disk_kl);
+		com1_puts(", rootfs ");
+		com1_dec(disk_psize);
+		com1_puts(" bytes at lba ");
+		com1_dec(disk_pl);
+		com1_puts("\n");
+	} else {
+		com1_puts("nebos-efi: no usable disk image; using embedded\n");
+	}
+
+	alloc_ksize = disk_ok && disk_ksize > embed_ksize ?
+		      disk_ksize : embed_ksize;
+	alloc_psize = disk_ok && disk_psize > embed_psize ?
+		      disk_psize : embed_psize;
+	alloc_psize = (alloc_psize + 511u) & ~511u;
+
 	/* Stage the kernel below 4 GB; the low-memory trampoline copies it to
 	 * 0x7E00 after ExitBootServices, when low memory is ours. */
-	pages = (kernel_size + PAGE_SIZE_ - 1) / PAGE_SIZE_;
+	pages = (alloc_ksize + PAGE_SIZE_ - 1) / PAGE_SIZE_;
 	status = bs->AllocatePages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
 				   pages, &staging);
 	if (status || staging > 0xFFFFFFFFull || staging < 0x100000ull ||
-	    staging + kernel_size > 0x100000000ull)
+	    staging + alloc_ksize > 0x100000000ull)
 		fail_before(st, "cannot allocate kernel staging buffer");
-	for (i = 0; i < kernel_size; i++)
-		((UINT8 *)(UINTN)staging)[i] = kernel_image_start[i];
-	com1_puts("nebos-efi: staged at 0x");
-	com1_hex(staging, 8);
-	com1_puts("\n");
 
 	/* Rootfs pack: allocate a writable loader-data buffer below the
 	 * kernel's 512 MB identity window (and above the low 1 MB it
-	 * reserves) and copy the embedded image into it while boot services
-	 * are still available.  It must be below MAX_PHYS: the kernel only
+	 * reserves).  It must be below MAX_PHYS: the kernel only
 	 * identity-maps that much and ramfs_init() reads it after paging is
 	 * enabled. */
-	if (pack_size == 0 || pack_size > 0x400000u)
-		fail_before(st, "rootfs image size invalid");
-	pages = (pack_size + PAGE_SIZE_ - 1) / PAGE_SIZE_;
+	pages = (alloc_psize + PAGE_SIZE_ - 1) / PAGE_SIZE_;
 	pack_buf = MAX_PHYS;
 	status = bs->AllocatePages(EFI_ALLOCATE_MAX_ADDRESS, EFI_LOADER_DATA,
 				   pages, &pack_buf);
 	if (status || pack_buf < 0x100000ull ||
-	    pack_buf + pack_size > MAX_PHYS)
+	    pack_buf + alloc_psize > MAX_PHYS)
 		fail_before(st, "cannot allocate rootfs buffer");
-	for (i = 0; i < pack_size; i++)
-		((UINT8 *)(UINTN)pack_buf)[i] = pack_image_start[i];
-	com1_puts("nebos-efi: rootfs staged at 0x");
+
+	if (disk_ok) {
+		UINT32 pread = (disk_psize + 511u) & ~511u;
+
+		if (disk_read(bio, disk_kl, (void *)(UINTN)staging,
+			      disk_ksize) == 0 &&
+		    pread <= alloc_psize &&
+		    disk_read(bio, disk_pl + 1u, (void *)(UINTN)pack_buf,
+			      pread) == 0) {
+			kernel_size = disk_ksize;
+			pack_size = disk_psize;
+			from_disk = 1;
+			com1_puts("nebos-efi: kernel + rootfs loaded from disk\n");
+		} else {
+			com1_puts("nebos-efi: disk read failed; using embedded\n");
+		}
+	}
+	if (!from_disk) {
+		for (i = 0; i < embed_ksize; i++)
+			((UINT8 *)(UINTN)staging)[i] = kernel_image_start[i];
+		for (i = 0; i < embed_psize; i++)
+			((UINT8 *)(UINTN)pack_buf)[i] = pack_image_start[i];
+		kernel_size = embed_ksize;
+		pack_size = embed_psize;
+		com1_puts("nebos-efi: using embedded kernel + rootfs\n");
+	}
+	com1_puts("nebos-efi: staged at 0x");
+	com1_hex(staging, 8);
+	com1_puts(", rootfs at 0x");
 	com1_hex(pack_buf, 8);
 	com1_puts("\n");
 

+ 109 - 51
fatformat.c

@@ -163,11 +163,27 @@ static void build_boot_sector(unsigned char *s, unsigned int type, unsigned int
 			      unsigned int reserved, unsigned int num_fats,
 			      unsigned int root_entries, unsigned int sec_per_fat,
 			      unsigned int fs_sectors, unsigned int hidden,
-			      unsigned int media)
+			      unsigned int media, const char *label)
 {
 	unsigned int i;
 	zero_sector(s);
 
+	/* Volume label: an 11-byte, space-padded field (no dot).  A NULL label
+	 * keeps the old blank behaviour used by the `format` command. */
+	{
+		unsigned int off = (type == 32) ? 0x47 : 0x2B;
+
+		for (i = 0; i < 11; i++) {
+			char c = label ? label[i] : '\0';
+
+			if (c == '\0')
+				break;
+			s[off + i] = (unsigned char)c;
+		}
+		for (; i < 11; i++)
+			s[off + i] = ' ';
+	}
+
 	s[0] = 0xEB;
 	s[1] = (type == 32) ? 0x58 : 0x3C;
 	s[2] = 0x90;
@@ -197,14 +213,12 @@ static void build_boot_sector(unsigned char *s, unsigned int type, unsigned int
 		s[0x40] = 0x80;                         /* drive number */
 		s[0x42] = 0x29;                         /* boot signature */
 		put32(s + 0x43, 0x12345678);            /* volume id */
-		for (i = 0; i < 11; i++) s[0x47 + i] = ' ';
 		s[0x52] = 'F'; s[0x53] = 'A'; s[0x54] = 'T'; s[0x55] = '3';
 		s[0x56] = '2'; s[0x57] = ' '; s[0x58] = ' '; s[0x59] = ' ';
 	} else {
 		s[0x24] = (hidden == 0) ? 0x00 : 0x80;  /* drive number */
 		s[0x26] = 0x29;                         /* boot signature */
 		put32(s + 0x27, 0x12345678);            /* volume id */
-		for (i = 0; i < 11; i++) s[0x2B + i] = ' ';
 		s[0x36] = 'F'; s[0x37] = 'A'; s[0x38] = 'T'; s[0x39] = '1';
 		s[0x3A] = (type == 12) ? '2' : '6';
 		s[0x3B] = ' '; s[0x3C] = ' '; s[0x3D] = ' ';
@@ -243,19 +257,23 @@ static void build_fsinfo_sector(unsigned char *s)
 	put32(s + 0x1FC, 0xAA550000u);   /* TrailSig */
 }
 
-int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
+/* Format one FAT region.  `write_mbr` is set only for whole-disk formats;
+ * partition formats leave the MBR to the caller (the installer merges a
+ * preserved partition-4 entry). */
+static int fat_format_region(blk_dev_t *d, unsigned int part_start,
+			     unsigned int fs_sectors, int write_mbr,
+			     int is_floppy, const char *label,
+			     unsigned int *type_out)
 {
 	static unsigned char sec[SEC_SIZE];
 	static unsigned char fat_sec[SEC_SIZE];
 	static unsigned char zero[SEC_SIZE];
 	unsigned char *zbuf;
 	unsigned int zchunk, zpages;
-	unsigned int fs_sectors, part_start;
 	unsigned int type, spc, root_entries;
 	unsigned int reserved, num_fats = 2, sec_per_fat, root_dir_sectors;
 	unsigned int clusters, fat_start, root_start, data_start;
 	unsigned int media;
-	int is_floppy;
 	unsigned int i;
 	int rc = -1;
 
@@ -266,45 +284,7 @@ int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
 		format_puts("format: no device\n");
 		return -1;
 	}
-	is_floppy = (d->kind == BLK_FLOPPY);
-
-	if (is_floppy) {
-		if (limit_sectors) {
-			format_puts("format: a floppy is always the full 1440 KiB\n");
-			return -1;
-		}
-		fs_sectors = 2880;
-		part_start = 0;
-		media = 0xF0;
-	} else {
-		unsigned int avail;
-
-		if (d->total_sectors < (2048 + 64)) {
-			format_puts("format: disk too small (");
-			format_putdec(d->total_sectors);
-			format_puts(" sectors)\n");
-			return -1;
-		}
-		part_start = 2048;
-		avail = d->total_sectors - part_start;
-		fs_sectors = avail;
-		if (limit_sectors) {
-			if (limit_sectors > avail) {
-				format_puts("format: size exceeds device (");
-				format_putdec(avail / 2048);
-				format_puts(" MiB available)\n");
-				return -1;
-			}
-			fs_sectors = limit_sectors;
-		}
-		if (fs_sectors < 64) {
-			format_puts("format: partition too small (");
-			format_putdec(fs_sectors);
-			format_puts(" sectors)\n");
-			return -1;
-		}
-		media = 0xF8;
-	}
+	media = is_floppy ? 0xF0 : 0xF8;
 
 	choose_layout(fs_sectors, &type, &spc, &root_entries);
 	reserved = (type == 32) ? 32 : 1;
@@ -333,13 +313,13 @@ int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
 	format_puts("format: ");
 	format_puts(d->name);
 	format_puts(": ");
-	format_putdec(d->total_sectors);
+	format_putdec(fs_sectors);
 	format_puts(" sectors");
 	if (is_floppy) {
 		format_puts(" (1440 KiB)");
-	} else if (d->total_sectors >= 2048) {
+	} else if (fs_sectors >= 2048) {
 		format_puts(" (");
-		format_putdec(d->total_sectors / 2048);
+		format_putdec(fs_sectors / 2048);
 		format_puts(" MiB)");
 	}
 	format_puts(", FAT");
@@ -384,8 +364,8 @@ int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
 		}
 	}
 
-	/* 1. MBR (IDE/SATA only). */
-	if (!is_floppy) {
+	/* 1. MBR (whole-disk formats only). */
+	if (write_mbr) {
 		unsigned char *e;
 		fmt_stage = "mbr";
 		zero_sector(sec);
@@ -405,7 +385,7 @@ int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
 	/* 2. Reserved area + boot sector (+ FSInfo/backup for FAT32). */
 	fmt_stage = "reserved";
 	build_boot_sector(sec, type, spc, reserved, num_fats, root_entries,
-			  sec_per_fat, fs_sectors, part_start, media);
+			  sec_per_fat, fs_sectors, part_start, media, label);
 	if (write_zeros(d, part_start, reserved, zbuf, zchunk) != 0)
 		goto out;
 	fmt_stage = "boot sector";
@@ -447,6 +427,8 @@ int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
 	}
 
 	rc = 0;
+	if (type_out)
+		*type_out = type;
 
 out:
 	if (zpages) {
@@ -460,6 +442,82 @@ out:
 	return rc;
 }
 
+int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors)
+{
+	unsigned int fs_sectors, part_start;
+	int is_floppy;
+
+	if (!d) {
+		format_puts("format: no device\n");
+		return -1;
+	}
+	is_floppy = (d->kind == BLK_FLOPPY);
+
+	if (is_floppy) {
+		if (limit_sectors) {
+			format_puts("format: a floppy is always the full 1440 KiB\n");
+			return -1;
+		}
+		fs_sectors = 2880;
+		part_start = 0;
+	} else {
+		unsigned int avail;
+
+		if (d->total_sectors < (2048 + 64)) {
+			format_puts("format: disk too small (");
+			format_putdec(d->total_sectors);
+			format_puts(" sectors)\n");
+			return -1;
+		}
+		part_start = 2048;
+		avail = d->total_sectors - part_start;
+		fs_sectors = avail;
+		if (limit_sectors) {
+			if (limit_sectors > avail) {
+				format_puts("format: size exceeds device (");
+				format_putdec(avail / 2048);
+				format_puts(" MiB available)\n");
+				return -1;
+			}
+			fs_sectors = limit_sectors;
+		}
+		if (fs_sectors < 64) {
+			format_puts("format: partition too small (");
+			format_putdec(fs_sectors);
+			format_puts(" sectors)\n");
+			return -1;
+		}
+	}
+
+	return fat_format_region(d, part_start, fs_sectors, !is_floppy,
+				 is_floppy, 0, 0);
+}
+
+int fat_format_partition(blk_dev_t *d, unsigned int part_lba,
+			 unsigned int sectors, const char *label,
+			 unsigned int *type_out)
+{
+	if (!d) {
+		format_puts("format: no device\n");
+		return -1;
+	}
+	if (d->kind == BLK_FLOPPY) {
+		format_puts("format: cannot format a floppy partition\n");
+		return -1;
+	}
+	if (part_lba == 0 || sectors < 64 ||
+	    part_lba > d->total_sectors ||
+	    sectors > d->total_sectors - part_lba) {
+		format_puts("format: partition range invalid (");
+		format_putdec(part_lba);
+		format_puts("+");
+		format_putdec(sectors);
+		format_puts(")\n");
+		return -1;
+	}
+	return fat_format_region(d, part_lba, sectors, 0, 0, label, type_out);
+}
+
 int fat_format(blk_dev_t *d)
 {
 	return fat_format_limited(d, 0);

+ 7 - 0
fatformat.h

@@ -11,6 +11,13 @@
 int fat_format(blk_dev_t *d);
 int fat_format_limited(blk_dev_t *d, unsigned int limit_sectors);
 
+/* Format one partition region without touching the MBR.  `label` is an
+ * 11-byte FAT volume label (NULL keeps it blank); `type_out` receives the
+ * chosen FAT type (12/16/32) so the caller can build the MBR entry. */
+int fat_format_partition(blk_dev_t *d, unsigned int part_lba,
+			 unsigned int sectors, const char *label,
+			 unsigned int *type_out);
+
 /* Decimal helper for the format messages (terminal + netlog mirror). */
 void format_putdec(unsigned int v);
 

+ 595 - 41
install.c

@@ -12,10 +12,14 @@
  */
 
 #include "blkdev.h"
+#include "fatdetect.h"
 #include "fatformat.h"
+#include "install.h"
 #include "io.h"
 #include "mm.h"
 #include "process.h"
+#include "ramfs.h"
+#include "rootfs.h"
 #include "sched.h"
 #include "serial.h"
 #include "vga.h"
@@ -24,11 +28,10 @@
 #define INSTALL_NAME_MAX 16
 #define INSTALL_CHUNK    32u	/* 16 KiB copy buffer */
 #define INSTALL_DOT_LBA  2048u	/* one progress dot per MiB */
+#define DATA_MIN_SECTORS 2048u	/* /data needs at least 1 MiB */
 
 #define MBR_PART_OFF     0x1BE
 #define MBR_PART_SIZE    16
-#define BOOT_LBA         63u
-#define ROOTFS_LBA       1088u
 #define PACK_LBA_OFF     0x42
 #define KERNEL_LBA_OFF   0x40
 #define KERNEL_SEC_OFF   0x3E
@@ -134,11 +137,12 @@ static int check_nebos_image(blk_dev_t *d, unsigned int *used, int *has_esp,
 		const unsigned char *e = mbr + MBR_PART_OFF;
 
 		if (e[0] != 0x80 || e[4] != 0x06 ||
-		    get32(e + 8) != BOOT_LBA || get32(e + 12) < 1024u)
+		    get32(e + 8) != NEBOS_BOOT_LBA ||
+		    get32(e + 12) < 1024u)
 			return 0;
 	}
 
-	if (blk_read(d, BOOT_LBA, boot, 1) != 0)
+	if (blk_read(d, NEBOS_BOOT_LBA, boot, 1) != 0)
 		return 0;
 	if (boot[510] != 0x55 || boot[511] != 0xAA)
 		return 0;
@@ -147,14 +151,15 @@ static int check_nebos_image(blk_dev_t *d, unsigned int *used, int *has_esp,
 		return 0;
 	if (get32(boot + PACK_LBA_OFF) != ROOTFS_LBA)
 		return 0;
-	if (get16(boot + KERNEL_LBA_OFF) != BOOT_LBA + 1u)
+	if (get16(boot + KERNEL_LBA_OFF) != NEBOS_KERNEL_LBA)
 		return 0;
 	if (get16(boot + KERNEL_SEC_OFF) == 0)
 		return 0;
 
-	/* Used length: the end of the highest non-empty partition.  Require
-	 * the rootfs area to be covered, and reject an entry that claims more
-	 * sectors than the source has. */
+	/* Used length: the end of the highest OS partition.  Partition 4 is
+	 * the persistent data volume: it is not part of the cloned image and
+	 * may legitimately extend to the end of the disk.  A source entry may
+	 * not cross into the data area, though. */
 	for (i = 0; i < 4; i++) {
 		const unsigned char *e = mbr + MBR_PART_OFF + i * MBR_PART_SIZE;
 		unsigned int start, count, end;
@@ -168,7 +173,11 @@ static int check_nebos_image(blk_dev_t *d, unsigned int *used, int *has_esp,
 		if (!count || start > d->total_sectors ||
 		    count > d->total_sectors - start)
 			return 0;
+		if (start >= NEBOS_DATA_LBA)
+			continue;
 		end = start + count;
+		if (end > NEBOS_DATA_LBA)
+			return 0;
 		if (end > max_end)
 			max_end = end;
 	}
@@ -263,18 +272,556 @@ static void install_fail(const char *msg, unsigned int lba)
 	serial_puts(SERIAL_COM1, "\n");
 }
 
+static void put16(unsigned char *p, unsigned int v)
+{
+	p[0] = (unsigned char)v;
+	p[1] = (unsigned char)(v >> 8);
+}
+
+static void put32(unsigned char *p, unsigned int v)
+{
+	p[0] = (unsigned char)v;
+	p[1] = (unsigned char)(v >> 8);
+	p[2] = (unsigned char)(v >> 16);
+	p[3] = (unsigned char)(v >> 24);
+}
+
+static void copy_bytes(unsigned char *dst, const unsigned char *src,
+		       unsigned int n)
+{
+	unsigned int i;
+
+	for (i = 0; i < n; i++)
+		dst[i] = src[i];
+}
+
+static void zero_bytes(unsigned char *dst, unsigned int n)
+{
+	unsigned int i;
+
+	for (i = 0; i < n; i++)
+		dst[i] = 0;
+}
+
+static int str_eq(const char *a, const char *b)
+{
+	unsigned int i = 0;
+
+	while (a[i] && b[i] && a[i] == b[i])
+		i++;
+	return a[i] == '\0' && b[i] == '\0';
+}
+
+/* The persistent data volume is recognised by its fixed LBA and label. */
+static int detect_data_partition(blk_dev_t *d, const unsigned char *mbr,
+				 unsigned char *entry)
+{
+	const unsigned char *e = mbr + MBR_PART_OFF + 3 * MBR_PART_SIZE;
+	unsigned int start, count;
+	fat_fs_t fs;
+
+	if (!mbr_is_fat_type(e[4]))
+		return 0;
+	start = get32(e + 8);
+	count = get32(e + 12);
+	if (start != NEBOS_DATA_LBA || count < DATA_MIN_SECTORS)
+		return 0;
+	if (start > d->total_sectors || count > d->total_sectors - start)
+		return 0;
+	if (fat_mount(d, start, &fs) != 0)
+		return 0;
+	if (!str_eq(fs.label, "NEBOSDATA"))
+		return 0;
+	if (entry)
+		copy_bytes(entry, e, MBR_PART_SIZE);
+	return 1;
+}
+
+/* Detect and, when absent, create the data partition.  The caller owns the
+ * target MBR buffer and writes it last.  `created` says whether the entry is
+ * new (and therefore whether the MBR must be rewritten). */
+static int ensure_data_partition(blk_dev_t *d, const unsigned char *mbr,
+				 unsigned char *entry, int *created)
+{
+	unsigned int sectors, type = 0;
+
+	*created = 0;
+	if (detect_data_partition(d, mbr, entry)) {
+		io_putstr("install: /data partition present; keeping it\n");
+		return 1;
+	}
+	if (d->total_sectors <= NEBOS_DATA_LBA ||
+	    d->total_sectors - NEBOS_DATA_LBA < DATA_MIN_SECTORS) {
+		io_putstr("install: target is too small for a /data partition\n");
+		return 0;
+	}
+	sectors = d->total_sectors - NEBOS_DATA_LBA;
+	io_putstr("install: creating /data partition (");
+	put_size(sectors);
+	io_putstr(")...\n");
+	/* The installer's format path owns the disks through format_busy, and
+	 * the FAT loop releases the BKL per chunk.  Dropping it entirely (as
+	 * `format` does) avoids the format_progress sleeps holding it. */
+	bkl_unlock();
+	if (fat_format_partition(d, NEBOS_DATA_LBA, sectors, "NEBOSDATA",
+				 &type) != 0) {
+		bkl_lock();
+		io_putstr("install: /data format failed\n");
+		return 0;
+	}
+	bkl_lock();
+
+	zero_bytes(entry, MBR_PART_SIZE);
+	entry[1] = 0xFE;
+	entry[2] = 0xFF;
+	entry[3] = 0xFF;
+	entry[4] = (type == 32) ? 0x0C : ((type == 16) ? 0x0E : 0x01);
+	entry[5] = 0xFE;
+	entry[6] = 0xFF;
+	entry[7] = 0xFF;
+	put32(entry + 8, NEBOS_DATA_LBA);
+	put32(entry + 12, sectors);
+	*created = 1;
+	io_putstr("install: /data FAT");
+	format_putdec(type);
+	io_putstr(", ");
+	put_size(sectors);
+	io_putchar('\n');
+	return 1;
+}
+
+static blk_dev_t *resolve_target(const char *name)
+{
+	blk_dev_t *found = 0;
+	unsigned int i;
+
+	if (!str_eq(name, "auto")) {
+		found = blk_find(name);
+		if (!found) {
+			io_putstr("install: unknown target '");
+			io_putstr(name);
+			io_putstr("'\n");
+			list_devices();
+		}
+		return found;
+	}
+
+	for (i = 0; i < blk_dev_count; i++) {
+		blk_dev_t *d = &blk_devs[i];
+
+		if (!d->present || d->kind == BLK_FLOPPY ||
+		    d->kind == BLK_USB)
+			continue;
+		if (!check_nebos_image(d, 0, 0, 0))
+			continue;
+		if (found) {
+			io_putstr("install: auto found more than one NebOS disk; "
+				  "name the target explicitly\n");
+			return 0;
+		}
+		found = d;
+	}
+	if (!found)
+		io_putstr("install: auto found no installed NebOS disk\n");
+	return found;
+}
+
+static int ask_continue(blk_dev_t *target, int have_data)
+{
+	int k;
+
+	if (have_data) {
+		io_putstr("install: this replaces the OS on ");
+		io_putstr(target->name);
+		io_putstr("; files on /data are kept. Continue? (y/n) ");
+	} else {
+		io_putstr("install: this destroys all data on ");
+		io_putstr(target->name);
+		io_putstr("? (y/n) ");
+	}
+	vga_fb_flush();
+
+	/* The confirmation loop yields so the kworker can poll the USB HID
+	 * keyboard. */
+	io_set_raw_keyboard(1);
+	for (;;) {
+		k = io_poll_key_with_hw();
+		if (k == 'y' || k == 'Y') {
+			io_set_raw_keyboard(0);
+			io_putchar('\n');
+			return 1;
+		}
+		if (k == 'n' || k == 'N') {
+			io_set_raw_keyboard(0);
+			io_putstr("aborted\n");
+			return 0;
+		}
+		process_sleep_ms(10);
+	}
+}
+
+/* Write `bytes` from p at lba, padding the final partial sector with
+ * zeros.  Chunks release the BKL like the rest of the installer. */
+static int write_region(blk_dev_t *d, unsigned int lba,
+			const unsigned char *p, unsigned int bytes,
+			const char *what)
+{
+	unsigned int full = bytes / 512u, off = 0, n;
+
+	while (off < full) {
+		n = full - off;
+		if (n > INSTALL_CHUNK)
+			n = INSTALL_CHUNK;
+		if (write_chunk(d, lba + off, p + off * 512u, n) != 0) {
+			install_fail(what, lba + off);
+			return -1;
+		}
+		off += n;
+	}
+	if (bytes % 512u) {
+		unsigned int rem = bytes % 512u, i;
+
+		for (i = 0; i < rem; i++)
+			copy_buf[i] = p[full * 512u + i];
+		for (; i < 512u; i++)
+			copy_buf[i] = 0;
+		if (write_chunk(d, lba + full, copy_buf, 1) != 0) {
+			install_fail(what, lba + full);
+			return -1;
+		}
+	}
+	return 0;
+}
+
+static int verify_region(blk_dev_t *d, unsigned int lba,
+			 const unsigned char *p, unsigned int bytes,
+			 const char *what)
+{
+	unsigned int off = 0, sum = sum_bytes(0, p, bytes), vsum = 0, n;
+
+	while (off + 512u <= bytes) {
+		n = (bytes - off) / 512u;
+		if (n > INSTALL_CHUNK)
+			n = INSTALL_CHUNK;
+		if (read_chunk(d, lba + off / 512u, copy_buf, n) != 0) {
+			install_fail("target read-back failed",
+				     lba + off / 512u);
+			return -1;
+		}
+		vsum = sum_bytes(vsum, copy_buf, n * 512u);
+		off += n * 512u;
+	}
+	if (off < bytes) {
+		if (read_chunk(d, lba + off / 512u, copy_buf, 1) != 0) {
+			install_fail("target read-back failed",
+				     lba + off / 512u);
+			return -1;
+		}
+		vsum = sum_bytes(vsum, copy_buf, bytes - off);
+	}
+	if (vsum != sum) {
+		install_fail(what, 0);
+		return -1;
+	}
+	return 0;
+}
+
+/* Shared P1/P2 writer for `install self` and the network install path. */
+static int write_os_image(blk_dev_t *d, const void *kernel,
+			  unsigned int ksize, const struct rootfs_hdr *hdr,
+			  const void *pack, unsigned int psize,
+			  int ensure_data)
+{
+	unsigned char boot[512], mbr[512], data_entry[MBR_PART_SIZE];
+	unsigned int used = 0, ksectors;
+	int has_esp = 0, created = 0;
+
+	if (!d || !d->present || d->kind == BLK_FLOPPY || !kernel || !ksize ||
+	    !hdr || !pack || !psize)
+		return -1;
+	if (hdr->version != ROOTFS_VERSION || hdr->pack_bytes != psize) {
+		io_putstr("install: bad rootfs header\n");
+		return -1;
+	}
+	if (hdr->flags & ~ROOTFS_FLAG_GZIP) {
+		io_putstr("install: unsupported rootfs flags\n");
+		return -1;
+	}
+	ksectors = (ksize + 511u) / 512u;
+	if (ksectors == 0 ||
+	    ksectors > ROOTFS_LBA - NEBOS_KERNEL_LBA) {
+		io_putstr("install: kernel does not fit partition 1\n");
+		return -1;
+	}
+	if (psize + ROOTFS_HDR_SIZE > ROOTFS_SECTORS * 512u) {
+		io_putstr("install: rootfs does not fit partition 2\n");
+		return -1;
+	}
+	if (!check_nebos_image(d, &used, &has_esp, mbr)) {
+		io_putstr("install: target is not an installed NebOS disk\n");
+		io_putstr("install: run `install <target> <source>` from USB first\n");
+		return -1;
+	}
+	if (blk_read(d, NEBOS_BOOT_LBA, boot, 1) != 0) {
+		io_putstr("install: cannot read the target boot sector\n");
+		return -1;
+	}
+	if (format_busy && process_is_alive(format_owner_pid)) {
+		io_putstr("install: another format/install is in progress\n");
+		return -1;
+	}
+	format_owner_pid = current_proc()->pid;
+	format_busy = 1;
+
+	/* Prepare the data partition before touching P1/P2: a format failure
+	 * then leaves the old OS bootable. */
+	if (ensure_data &&
+	    !ensure_data_partition(d, mbr, data_entry, &created))
+		goto fail;
+
+	io_putstr("install: writing rootfs...\n");
+	if (write_chunk(d, ROOTFS_LBA, hdr, 1) != 0 ||
+	    write_region(d, ROOTFS_LBA + 1u, (const unsigned char *)pack,
+			 psize, "rootfs write failed") != 0)
+		goto fail;
+	io_putstr("install: writing kernel...\n");
+	if (write_region(d, NEBOS_KERNEL_LBA, (const unsigned char *)kernel,
+			 ksize, "kernel write failed") != 0)
+		goto fail;
+
+	/* The installed boot sector carries only the NebOS header fields. */
+	zero_bytes(boot + 0x0B, 0x3E - 0x0B);
+	put16(boot + KERNEL_SEC_OFF, ksectors);
+	if (write_chunk(d, NEBOS_BOOT_LBA, boot, 1) != 0) {
+		install_fail("boot sector write failed", NEBOS_BOOT_LBA);
+		goto fail;
+	}
+
+	if (ensure_data && created) {
+		copy_bytes(mbr + MBR_PART_OFF + 3 * MBR_PART_SIZE,
+			   data_entry, MBR_PART_SIZE);
+		if (write_chunk(d, 0, mbr, 1) != 0) {
+			install_fail("MBR write failed", 0);
+			goto fail;
+		}
+	}
+
+	{
+		int fr;
+
+		bkl_pause();
+		fr = blk_flush(d);
+		bkl_resume();
+		if (fr != 0)
+			io_putstr("install: cache flush failed; verifying anyway\n");
+	}
+
+	io_putstr("install: verifying...\n");
+	if (verify_region(d, ROOTFS_LBA, (const unsigned char *)hdr,
+			  ROOTFS_HDR_SIZE, "rootfs header verify failed") != 0 ||
+	    verify_region(d, ROOTFS_LBA + 1u, (const unsigned char *)pack,
+			  psize, "rootfs verify failed") != 0 ||
+	    verify_region(d, NEBOS_KERNEL_LBA, (const unsigned char *)kernel,
+			  ksize, "kernel verify failed") != 0)
+		goto fail;
+	if (read_chunk(d, NEBOS_BOOT_LBA, copy_buf, 1) != 0) {
+		install_fail("boot sector read-back failed", NEBOS_BOOT_LBA);
+		goto fail;
+	}
+	{
+		unsigned int i;
+
+		for (i = 0; i < 512u; i++) {
+			if (copy_buf[i] != boot[i]) {
+				install_fail("boot sector verify failed",
+					     NEBOS_BOOT_LBA);
+				goto fail;
+			}
+		}
+	}
+	if (created) {
+		if (read_chunk(d, 0, copy_buf, 1) != 0) {
+			install_fail("MBR read-back failed", 0);
+			goto fail;
+		}
+		{
+			unsigned int i;
+
+			for (i = 0; i < 512u; i++) {
+				if (copy_buf[i] != mbr[i]) {
+					install_fail("MBR verify failed", 0);
+					goto fail;
+				}
+			}
+		}
+	}
+
+	if (created)
+		vfs_remount_device(d);
+
+	format_busy = 0;
+	io_putstr("install: verified; ");
+	put_size(used);
+	io_putstr(" image written to ");
+	io_putstr(d->name);
+	if (ensure_data)
+		io_putstr(", /data kept");
+	io_putchar('\n');
+	serial_puts(SERIAL_COM1, "install: image written\n");
+	return 0;
+
+fail:
+	format_busy = 0;
+	io_putstr("install: image update failed\n");
+	serial_puts(SERIAL_COM1, "install: image update failed\n");
+	return -1;
+}
+
+static int install_self(const char *target_name)
+{
+	struct vfs_file kf;
+	struct vfs_mount *km = 0;
+	const void *pack = 0;
+	unsigned int psize = 0;
+	int gz = 0, r;
+	struct rootfs_hdr hdr;
+	blk_dev_t *target;
+	unsigned char mbr[512];
+	int have_data = 0;
+
+	if (vfs_load_file("/ramfs/install/kernel.bin", &kf, &km) != 0) {
+		io_putstr("install: /ramfs/install/kernel.bin is missing\n");
+		io_putstr("install: the floppy/lite pack has no running kernel image\n");
+		return -1;
+	}
+	if (!ramfs_boot_image(&pack, &psize, &gz) || !pack || !psize) {
+		io_putstr("install: no boot-time rootfs image is retained\n");
+		vfs_release_file(km, &kf);
+		return -1;
+	}
+	if (!gz) {
+		io_putstr("install: the running pack was replaced at runtime (raw)\n");
+		io_putstr("install: use `make net-install` to persist it\n");
+		vfs_release_file(km, &kf);
+		return -1;
+	}
+	if (psize + ROOTFS_HDR_SIZE > ROOTFS_SECTORS * 512u) {
+		io_putstr("install: running rootfs is too large for partition 2\n");
+		vfs_release_file(km, &kf);
+		return -1;
+	}
+
+	target = resolve_target(target_name);
+	if (!target) {
+		vfs_release_file(km, &kf);
+		return -1;
+	}
+	if (!target->present || target->kind == BLK_FLOPPY ||
+	    target->total_sectors == 0) {
+		io_putstr("install: target is not usable\n");
+		vfs_release_file(km, &kf);
+		return -1;
+	}
+	if (!check_nebos_image(target, 0, 0, mbr)) {
+		io_putstr("install: target is not an installed NebOS disk\n");
+		io_putstr("install: run `install <target> <source>` from USB first\n");
+		vfs_release_file(km, &kf);
+		return -1;
+	}
+	have_data = detect_data_partition(target, mbr, 0);
+
+	io_putstr("install: source running image, kernel ");
+	put_size((kf.size + 511u) / 512u);
+	io_putstr(", rootfs ");
+	put_size((psize + 511u) / 512u);
+	io_putstr(gz ? " gzip" : " raw");
+	io_putchar('\n');
+	io_putstr("install: target ");
+	io_putstr(target->name);
+	if (target->model[0]) {
+		io_putstr(" (");
+		io_putstr(target->model);
+		io_putchar(')');
+	}
+	io_putchar('\n');
+	if (!ask_continue(target, have_data)) {
+		vfs_release_file(km, &kf);
+		return -1;
+	}
+
+	zero_bytes((unsigned char *)&hdr, sizeof(hdr));
+	copy_bytes((unsigned char *)hdr.magic, (const unsigned char *)ROOTFS_MAGIC,
+		   7);
+	hdr.version = ROOTFS_VERSION;
+	hdr.pack_bytes = psize;
+	hdr.pack_csum = sum_bytes(0, (const unsigned char *)pack, psize);
+	hdr.flags = ROOTFS_FLAG_GZIP;
+
+	r = write_os_image(target, kf.data, kf.size, &hdr, pack, psize, 1);
+	vfs_release_file(km, &kf);
+	if (r == 0) {
+		io_putstr("install: done; /data kept or created.\n");
+		serial_puts(SERIAL_COM1, "install: self done\n");
+	}
+	return r;
+}
+
+int install_ram_image(const char *target, const void *kernel,
+		      unsigned int ksize, const void *rootfs,
+		      unsigned int rsize)
+{
+	const struct rootfs_hdr *hdr;
+	const unsigned char *pack;
+	unsigned int psize;
+	blk_dev_t *d;
+
+	if (!rootfs || rsize < ROOTFS_HDR_SIZE) {
+		io_putstr("install: rootfs image too short\n");
+		return -1;
+	}
+	hdr = (const struct rootfs_hdr *)rootfs;
+	if (hdr->magic[0] != 'N' || hdr->magic[1] != 'B' ||
+	    hdr->magic[2] != 'R' || hdr->magic[3] != 'O' ||
+	    hdr->magic[4] != 'O' || hdr->magic[5] != 'T' ||
+	    hdr->magic[6] != '1' || hdr->version != ROOTFS_VERSION) {
+		io_putstr("install: bad rootfs image\n");
+		return -1;
+	}
+	psize = hdr->pack_bytes;
+	if (!psize || psize > rsize - ROOTFS_HDR_SIZE ||
+	    !(hdr->flags & ROOTFS_FLAG_GZIP)) {
+		io_putstr("install: bad rootfs length or encoding\n");
+		return -1;
+	}
+	d = resolve_target(target);
+	if (!d) {
+		io_putstr("install: no network install target\n");
+		return -1;
+	}
+	pack = (const unsigned char *)rootfs + ROOTFS_HDR_SIZE;
+	io_putstr("install: net update to ");
+	io_putstr(d->name);
+	io_putstr(" (kernel ");
+	put_size((ksize + 511u) / 512u);
+	io_putstr(", rootfs ");
+	put_size((psize + 511u) / 512u);
+	io_putstr(")\n");
+	return write_os_image(d, kernel, ksize, hdr, pack, psize, 0);
+}
+
 void program_install_main(const char *arg)
 {
 	char target_name[INSTALL_NAME_MAX], source_name[INSTALL_NAME_MAX];
-	char source_dev[INSTALL_NAME_MAX];
+	char source_dev[INSTALL_NAME_MAX], target_dev[INSTALL_NAME_MAX];
 	blk_dev_t *target, *source;
 	blk_dev_t src_copy, dst_copy;
 	unsigned char *image = 0;
+	unsigned char target_mbr[512], data_entry[MBR_PART_SIZE];
 	unsigned int image_pages = 0;
 	int have_copies = 0;
 	unsigned int used, i, j, lba, sum, vsum;
 	int has_esp = 0, explicit_source = 0;
-	int k;
+	int data_created = 0;
 
 	if (format_busy && process_is_alive(format_owner_pid)) {
 		io_putstr("install: another format/install is in progress\n");
@@ -283,7 +830,7 @@ void program_install_main(const char *arg)
 	format_busy = 0;
 
 	if (!arg) {
-		io_putstr("install: usage install <target> [source]\n");
+		io_putstr("install: usage install <target> [source|self]\n");
 		list_devices();
 		return;
 	}
@@ -306,18 +853,18 @@ void program_install_main(const char *arg)
 	while (arg[i] == ' ')
 		i++;
 	if (!target_name[0] || arg[i]) {
-		io_putstr("install: usage install <target> [source]\n");
+		io_putstr("install: usage install <target> [source|self]\n");
 		return;
 	}
 
-	target = blk_find(target_name);
-	if (!target) {
-		io_putstr("install: unknown target '");
-		io_putstr(target_name);
-		io_putstr("'\n");
-		list_devices();
+	if (str_eq(source_name, "self")) {
+		install_self(target_name);
 		return;
 	}
+
+	target = resolve_target(target_name);
+	if (!target)
+		return;
 	if (target->kind == BLK_FLOPPY) {
 		io_putstr("install: a floppy is too small for the image\n");
 		return;
@@ -328,6 +875,9 @@ void program_install_main(const char *arg)
 		io_putstr(" is not present\n");
 		return;
 	}
+	for (i = 0; i + 1 < sizeof(target_dev) && target->name[i]; i++)
+		target_dev[i] = target->name[i];
+	target_dev[i] = '\0';
 
 	source = find_source(target, explicit_source ? source_name : 0, &used);
 	if (!source) {
@@ -378,28 +928,16 @@ void program_install_main(const char *arg)
 		source_dev[i] = source->name[i];
 	source_dev[i] = '\0';
 
-	io_putstr("install: this destroys all data on ");
-	io_putstr(target->name);
-	io_putstr("? (y/n) ");
-	vga_fb_flush();
+	{
+		int have_data = 0;
 
-	/* The confirmation loop yields so the kworker can poll the USB HID
-	 * keyboard.  The copy then releases the BKL per chunk; do not run
-	 * lsusb (a bus rescan) while an install is in progress. */
-	io_set_raw_keyboard(1);
-	for (;;) {
-		k = io_poll_key_with_hw();
-		if (k == 'y' || k == 'Y')
-			break;
-		if (k == 'n' || k == 'N') {
-			io_set_raw_keyboard(0);
-			io_putstr("aborted\n");
+		if (blk_read(target, 0, target_mbr, 1) == 0)
+			have_data = detect_data_partition(target, target_mbr, 0);
+		if (have_data)
+			io_putstr("install: /data partition found; keeping it\n");
+		if (!ask_continue(target, have_data))
 			return;
-		}
-		process_sleep_ms(10);
 	}
-	io_set_raw_keyboard(0);
-	io_putchar('\n');
 
 	/* The prompt yielded; another command may have rescanned the block
 	 * devices or started a format.  Re-find both by name and take a
@@ -411,7 +949,7 @@ void program_install_main(const char *arg)
 	format_owner_pid = current_proc()->pid;
 	format_busy = 1;
 
-	target = blk_find(target_name);
+	target = blk_find(target_dev);
 	source = blk_find(source_dev);
 	if (!target || !source || source == target ||
 	    !target->present || !source->present) {
@@ -455,16 +993,16 @@ void program_install_main(const char *arg)
 	for (i = 0; i < sizeof(sector0); i++)
 		image[i] = sector0[i];
 	/* LBA 1..62 are the unused zero gap (partition 1 starts at 63). */
-	for (i = sizeof(sector0); i < BOOT_LBA * 512u; i++)
+	for (i = sizeof(sector0); i < NEBOS_BOOT_LBA * 512u; i++)
 		image[i] = 0;
-	sum = sum_bytes(0, sector0, sizeof(sector0));
+	sum = 0;
 
 	io_putstr("install: reading source");
 	vga_fb_flush();
 
 	/* Keep the BKL here: the USB source must stay active across all
 	 * chunks.  The target writes below release it per chunk. */
-	for (lba = BOOT_LBA; lba < used; lba += INSTALL_CHUNK) {
+	for (lba = NEBOS_BOOT_LBA; lba < used; lba += INSTALL_CHUNK) {
 		unsigned int n = used - lba;
 
 		if (n > INSTALL_CHUNK)
@@ -479,6 +1017,20 @@ void program_install_main(const char *arg)
 	}
 	io_putchar('\n');
 
+	/* The data partition is prepared before the target is touched.  It is
+	 * outside the cloned P1..P3 range; an existing one is left untouched. */
+	if (blk_read(&dst_copy, 0, target_mbr, 1) != 0)
+		zero_bytes(target_mbr, sizeof(target_mbr));
+	if (!ensure_data_partition(&dst_copy, target_mbr, data_entry,
+				   &data_created))
+		goto fail;
+	copy_bytes(image, sector0, 512);
+	copy_bytes(image + MBR_PART_OFF + 3 * MBR_PART_SIZE, data_entry,
+		   MBR_PART_SIZE);
+	/* Include the final MBR (with the merged data entry) and the zero gap
+	 * in the checksum.  `sum` currently covers BOOT_LBA..used. */
+	sum = sum_bytes(0, image, NEBOS_BOOT_LBA * 512u) + sum;
+
 	io_putstr("install: writing target");
 	for (lba = 1; lba < used; lba += INSTALL_CHUNK) {
 		unsigned int n = used - lba;
@@ -536,6 +1088,8 @@ void program_install_main(const char *arg)
 	vfs_remount_device(&dst_copy);
 	format_busy = 0;
 	io_putstr("install: done; remove the install medium and reboot.\n");
+	io_putstr(data_created ? "install: created /data on partition 4.\n"
+			      : "install: kept the existing /data partition.\n");
 	serial_puts(SERIAL_COM1, "install: done\n");
 	return;
 

+ 14 - 0
install.h

@@ -0,0 +1,14 @@
+/* Installer internals shared by the `install` command and the network
+ * persistent-update path. */
+#ifndef INSTALL_H
+#define INSTALL_H
+
+/* Write a kernel and an NBROOT1 rootfs image to an installed NebOS disk.
+ * `rootfs` points at a 512-byte header followed by `rsize - 512` pack bytes.
+ * Used by `install <target> self` and by the KXI2 net-install path; the
+ * target's partition 4 is preserved.  Returns 0 on success. */
+int install_ram_image(const char *target, const void *kernel,
+		      unsigned int ksize, const void *rootfs,
+		      unsigned int rsize);
+
+#endif

+ 22 - 5
kernel.c

@@ -476,27 +476,44 @@ void kernel_main(void)
 	{
 		unsigned int pack_addr = bootinfo_pack_addr();
 		unsigned int pack_len = bootinfo_pack_size();
-		int ok = 0;
+		int ok = 0, gz = 0;
 
 		if (!pack_addr || !pack_len)
 			serial_puts(SERIAL_COM1, "ramfs: no boot pack\n");
 		else {
+			const unsigned char *p = (const unsigned char *)pack_addr;
+
+			gz = pack_len >= 2 && p[0] == 0x1F && p[1] == 0x8B;
 			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, " bytes\n");
+			serial_puts(SERIAL_COM1, gz ? " bytes (gzip)\n" :
+						      " bytes\n");
+			if (gz)
+				ramfs_set_boot_image((const void *)pack_addr,
+						     pack_len, 1);
 			ok = ramfs_init((const void *)pack_addr, pack_len) == 0;
-			if (!ok)
+			if (!ok) {
 				serial_puts(SERIAL_COM1, "ramfs: bad boot pack\n");
-			else {
+				ramfs_set_boot_image(0, 0, 0);
+				mm_free_range(pack_addr, pack_len);
+			} 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");
+				if (!gz) {
+					/* ramfs has its own copy now; keep that
+					 * as the self-update image. */
+					ramfs_set_boot_image(ramfs_pack_base(),
+							     ramfs_pack_size(), 0);
+					mm_free_range(pack_addr, pack_len);
+				}
+				/* A gzip boot image stays reserved so
+				 * `install self` can write it to P2. */
 			}
-			mm_free_range(pack_addr, pack_len);
 		}
 		(void)ok;
 		vfs_mount_ramfs();

+ 74 - 8
netkexec.c

@@ -9,10 +9,16 @@
  *   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
+ *                           status 3 = persisted, 4 = persist failed
  *
  * The runtime pack replacement keeps the old 16-byte "KXNP" header
  * (magic, entry, size, csum) with the raw NBPAK1 pack as data.
  *
+ * The persistent install uses "KXI2": the KXN2 header plus a 16-byte target
+ * name, and a full NBROOT1 rootfs image (header + gzip pack) instead of the
+ * raw gzip payload.  After the receive the board writes P2/P1, acks
+ * ACK_INSTALLED/ACK_INSTALL_FAIL and then jumps with the gzip part.
+ *
  * 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
@@ -20,6 +26,7 @@
  * the host can push without knowing whether the board is up yet.
  */
 
+#include "install.h"
 #include "io.h"
 #include "kexec.h"
 #include "net.h"
@@ -32,6 +39,7 @@
 #include "netkexec.h"
 #include "mm.h"
 #include "ramfs.h"
+#include "rootfs.h"
 #include "vfs.h"
 
 #define NETKEXEC_PORT       19288
@@ -39,12 +47,15 @@
 
 #define X_MAGIC_HDR   0x324E584Bu   /* "KXN2": kernel + rootfs */
 #define X_MAGIC_PAY   0x504E584Bu   /* "KXNP": a command pack for /ramfs */
+#define X_MAGIC_INST  0x3249584Bu   /* "KXI2": persist kernel + rootfs */
 #define X_MAGIC_DATA  0x444E584Bu   /* "KXND" */
 #define X_MAGIC_ACK   0x414E584Bu   /* "KXNA" */
 
-#define ACK_CONTINUE  0
-#define ACK_DONE      1
-#define ACK_BADSUM    2
+#define ACK_CONTINUE     0
+#define ACK_DONE         1
+#define ACK_BADSUM       2
+#define ACK_INSTALLED    3
+#define ACK_INSTALL_FAIL 4
 
 static unsigned char rxbuf[1520];       /* one full Ethernet frame */
 static unsigned char replybuf[1520];
@@ -73,6 +84,8 @@ static void net_payload_free(void)
 /* Pending transfer (filled by netkexec_pending). */
 static int have_header;
 static int payload_mode;         /* header was KXNP: replace the ramfs instead */
+static int install_mode;         /* header was KXI2: persist to a block device */
+static char x_install_target[16];
 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];
@@ -205,10 +218,13 @@ int netkexec_capture_frame(const unsigned char *frame, unsigned int len)
 		return 0;
 
 	magic = rd32(payload);
-	if (magic != X_MAGIC_HDR && magic != X_MAGIC_PAY)
+	if (magic != X_MAGIC_HDR && magic != X_MAGIC_PAY &&
+	    magic != X_MAGIC_INST)
 		return 0;
 
 	x_entry = rd32(payload + 4);
+	payload_mode = 0;
+	install_mode = 0;
 	if (magic == X_MAGIC_PAY) {
 		x_size = rd32(payload + 8);
 		x_csum = rd32(payload + 12);
@@ -216,7 +232,7 @@ int netkexec_capture_frame(const unsigned char *frame, unsigned int len)
 		x_pack_size = 0;
 		payload_mode = 1;
 	} else {
-		if (plen < 20)
+		if (plen < (magic == X_MAGIC_INST ? 36u : 20u))
 			return 0;
 		x_kernel_size = rd32(payload + 8);
 		x_pack_size = rd32(payload + 12);
@@ -225,7 +241,21 @@ int netkexec_capture_frame(const unsigned char *frame, unsigned int len)
 		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 (magic == X_MAGIC_INST) {
+			unsigned int t;
+
+			for (t = 0; t < sizeof(x_install_target) - 1; t++) {
+				char c = (char)payload[20 + t];
+
+				if (c == '\0')
+					break;
+				x_install_target[t] = c;
+			}
+			x_install_target[t] = '\0';
+			if (!x_install_target[0])
+				return 0;
+			install_mode = 1;
+		}
 	}
 	if (x_size == 0 || x_size > KEXEC_MAX)
 		return 0;
@@ -324,7 +354,8 @@ static int netkexec_recv(unsigned int stage_base)
 			if ((offset & 0x3FFF) == 0)
 				io_putchar('.');
 		}
-		send_ack(offset, ACK_CONTINUE);   /* also acks duplicates */
+		if (offset < x_size)
+			send_ack(offset, ACK_CONTINUE);  /* also acks duplicates */
 	}
 
 	if (sum != x_csum) {
@@ -413,8 +444,43 @@ void program_netkexec_main(void)
 	io_puthex(x_pack_size);
 	io_putstr(" bytes, entry ");
 	io_puthex(x_entry);
-	io_putstr(" -> jumping\n");
+	io_putchar('\n');
+
+	if (install_mode) {
+		const struct rootfs_hdr *rh =
+			(const struct rootfs_hdr *)(kexec_stage + x_kernel_size);
+		int r;
+
+		if (x_pack_size < ROOTFS_HDR_SIZE ||
+		    rh->pack_bytes > x_pack_size - ROOTFS_HDR_SIZE) {
+			io_putstr("netinstall: bad rootfs image\n");
+			send_ack(x_size, ACK_INSTALL_FAIL);
+			kexec_stage_free();
+			netdev_release();
+			kexec_busy = 0;
+			return;
+		}
+		r = install_ram_image(x_install_target,
+				      (const void *)kexec_stage, x_kernel_size,
+				      rh, x_pack_size);
+		if (r != 0) {
+			io_putstr("netinstall: failed; disk not updated\n");
+			send_ack(x_size, ACK_INSTALL_FAIL);
+			kexec_stage_free();
+			netdev_release();
+			kexec_busy = 0;
+			return;
+		}
+		send_ack(x_size, ACK_INSTALLED);
+		io_putstr("netinstall: persisted to ");
+		io_putstr(x_install_target);
+		io_putstr(" -> jumping\n");
+		kexec_jump(x_entry, x_kernel_size,
+			   (unsigned int)rh + ROOTFS_HDR_SIZE,
+			   rh->pack_bytes);
+	}
 
+	io_putstr(" -> jumping\n");
 	kexec_jump(x_entry, x_kernel_size, kexec_stage + x_kernel_size,
 		   x_pack_size);          /* never returns */
 }

+ 2 - 2
netkexec.h

@@ -9,8 +9,8 @@
 int netkexec_pending(void);
 
 /* Feed one frame received by another poller (the WebDAV server).  Returns 1
- * when it was a KXN2/KXNP header; netkexec_pending() then reports it without
- * polling the NIC again. */
+ * when it was a KXN2/KXNP/KXI2 header; netkexec_pending() then reports it
+ * without polling the NIC again. */
 int netkexec_capture_frame(const unsigned char *frame, unsigned int len);
 
 /* Process entry: receive the pending image into the kexec staging buffer,

+ 86 - 6
netkexec_send.c

@@ -23,9 +23,12 @@
 #define DATA_MAX   1400
 #define MAGIC_HDR  0x324E584Bu   /* "KXN2" (kernel + rootfs) */
 #define MAGIC_PAY  0x504E584Bu   /* "KXNP" (runtime pack replacement) */
+#define MAGIC_INST 0x3249584Bu   /* "KXI2" (persist kernel + rootfs) */
 #define MAGIC_DATA 0x444E584Bu   /* "KXND" */
 #define MAGIC_ACK  0x414E584Bu   /* "KXNA" */
-#define ACK_DONE   1
+#define ACK_DONE          1
+#define ACK_INSTALLED     3
+#define ACK_INSTALL_FAIL  4
 
 static uint32_t rd32(const unsigned char *p)
 {
@@ -108,7 +111,8 @@ int main(int argc, char **argv)
 	long ksize, psize = 0, size;
 	unsigned int offset = 0, sum = 0;
 	int fd, i, fails = 0;
-	int payload = 0;
+	int payload = 0, install = 0, installed = 0, install_failed = 0;
+	const char *install_target = 0;
 	struct sockaddr_in dst;
 
 	/* --payload: replace the command pack (/ramfs) instead of the kernel. */
@@ -123,6 +127,22 @@ int main(int argc, char **argv)
 		port = (unsigned int)strtoul(argv[3], NULL, 0);
 		kernel_path = argv[4];
 		entry = 0;
+	} else if (argc > 1 && strcmp(argv[1], "--install") == 0) {
+		/* Persist kernel + NBROOT1 rootfs image to a NebOS disk. */
+		if (argc != 8) {
+			fprintf(stderr,
+				"usage: %s --install <target|auto> <board-ip> "
+				"<port> <entry-hex> <kernel> <rootfs-image>\n",
+				argv[0]);
+			return 2;
+		}
+		install = 1;
+		install_target = argv[2];
+		ip = argv[3];
+		port = (unsigned int)strtoul(argv[4], NULL, 0);
+		entry = (unsigned int)strtoul(argv[5], NULL, 16);
+		kernel_path = argv[6];
+		pack_path = argv[7];
 	} else {
 		if (argc != 5 && argc != 6) {
 			fprintf(stderr,
@@ -178,16 +198,20 @@ int main(int argc, char **argv)
 		return 1;
 	}
 
-	pkt = malloc(20 + DATA_MAX);
+	pkt = malloc(36 + DATA_MAX);
 	if (payload)
 		printf("netkexec: %s:%u, %ld bytes, command pack for /ramfs\n",
 		       ip, port, size);
+	else if (install)
+		printf("netkexec: %s:%u, persist to %s: kernel %ld + "
+		       "rootfs %ld bytes\n", ip, port, install_target,
+		       ksize, psize);
 	else
 		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, payload ? MAGIC_PAY : (install ? MAGIC_INST : MAGIC_HDR));
 	wr32(pkt + 4, entry);
 	if (payload) {
 		wr32(pkt + 8, (uint32_t)size);
@@ -196,12 +220,21 @@ int main(int argc, char **argv)
 		wr32(pkt + 8, (uint32_t)ksize);
 		wr32(pkt + 12, (uint32_t)psize);
 		wr32(pkt + 16, (uint32_t)sum);
+		if (install) {
+			size_t tl = strlen(install_target);
+
+			if (tl > 15)
+				tl = 15;
+			memset(pkt + 20, 0, 16);
+			memcpy(pkt + 20, install_target, tl);
+		}
 	}
 	for (i = 0; i < 20; i++) {
 		uint32_t status;
 		long ack;
 
-		if (send_packet(fd, &dst, pkt, payload ? 16 : 20) != 0) {
+		if (send_packet(fd, &dst, pkt,
+				payload ? 16 : (install ? 36 : 20)) != 0) {
 			perror("sendto");
 			return 1;
 		}
@@ -244,6 +277,17 @@ int main(int argc, char **argv)
 			continue;                  /* retransmit */
 		}
 		fails = 0;
+		if ((unsigned int)ack == (unsigned int)size &&
+		    status == ACK_INSTALLED) {
+			offset = (unsigned int)ack;
+			installed = 1;
+			break;
+		}
+		if ((unsigned int)ack == (unsigned int)size &&
+		    status == ACK_INSTALL_FAIL) {
+			install_failed = 1;
+			break;
+		}
 		if (status == ACK_DONE && (unsigned int)ack == (unsigned int)size) {
 			offset = (unsigned int)ack;
 			break;
@@ -257,7 +301,43 @@ int main(int argc, char **argv)
 	if (payload)
 		printf("\rnetkexec: 100%% (%ld bytes) - pack accepted, /ramfs replaced\n",
 		       size);
-	else
+	else if (install) {
+		uint32_t status = 0;
+		long ack;
+		int tries;
+
+		if (install_failed) {
+			fprintf(stderr, "netkexec: board reported install failure\n");
+			return 1;
+		}
+		printf("\rnetkexec: 100%% (%ld bytes) - board is writing the disk...\n",
+		       size);
+		fflush(stdout);
+		/* The board may still have an ACK_CONTINUE/ACK_DONE in flight
+		 * from the receive loop; drain those and wait for the final
+		 * install status. */
+		for (tries = 0; !installed && tries < 6; tries++) {
+			ack = wait_ack(fd, 10000, &status);
+			if (ack < 0)
+				break;
+			if (status == ACK_INSTALLED) {
+				installed = 1;
+				break;
+			}
+			if (status == ACK_INSTALL_FAIL) {
+				fprintf(stderr,
+					"netkexec: board reported install failure\n");
+				return 1;
+			}
+			/* ACK_CONTINUE, ACK_DONE or ACK_BADSUM: keep draining. */
+		}
+		if (!installed) {
+			fprintf(stderr, "netkexec: no install status from the board\n");
+			return 1;
+		}
+		printf("netkexec: installed to %s; board is jumping\n",
+		       install_target);
+	} else
 		printf("\rnetkexec: 100%% (%ld bytes) - kernel+rootfs accepted, board is jumping\n",
 		       size);
 	close(fd);

+ 37 - 0
ramfs.c

@@ -29,6 +29,33 @@ static unsigned int pack_size;
 static const struct ramfs_hdr *pack_hdr;
 static unsigned int pack_pages;   /* heap pages, when not embedded */
 
+/* Boot-time rootfs image, retained for `install self`. */
+static const unsigned char *boot_base;
+static unsigned int boot_size;
+static int boot_gzip;
+static int boot_owned;
+
+void ramfs_set_boot_image(const void *base, unsigned int size, int gzip)
+{
+	boot_base = (const unsigned char *)base;
+	boot_size = size;
+	boot_gzip = gzip;
+	boot_owned = gzip ? 1 : 0;
+}
+
+int ramfs_boot_image(const void **base, unsigned int *size, int *gzip)
+{
+	if (!boot_base || !boot_size)
+		return 0;
+	if (base)
+		*base = boot_base;
+	if (size)
+		*size = boot_size;
+	if (gzip)
+		*gzip = boot_gzip;
+	return 1;
+}
+
 static int str_eq(const char *a, const char *b)
 {
 	unsigned int i = 0;
@@ -238,6 +265,16 @@ int ramfs_replace(const void *pack, unsigned int size)
 	pack_hdr = h;
 	pack_pages = (size + 0xFFFu) >> 12;
 
+	/* A runtime pack replacement no longer matches the boot image that
+	 * `install self` would write, so drop (and, for a gzip boot image,
+	 * release) it. */
+	if (boot_owned && boot_base && boot_base != (const unsigned char *)pack)
+		mm_free_range((unsigned int)boot_base, boot_size);
+	boot_base = 0;
+	boot_size = 0;
+	boot_gzip = 0;
+	boot_owned = 0;
+
 	/* Free a previously received pack. */
 	for (i = 0; i < old_pages; i++)
 		mm_free_page((unsigned int)old_base + i * 0x1000);

+ 7 - 0
ramfs.h

@@ -41,4 +41,11 @@ unsigned int ramfs_pack_size(void);
 /* Owned pack base (NULL when there is no pack), for kexec handoff. */
 const void *ramfs_pack_base(void);
 
+/* The rootfs image the kernel booted from.  A gzip image is retained in its
+ * boot region for `install self`; a raw image points at the ramfs copy after
+ * ramfs_init().  ramfs_replace() invalidates it because the runtime pack no
+ * longer matches the boot image. */
+void ramfs_set_boot_image(const void *base, unsigned int size, int gzip);
+int ramfs_boot_image(const void **base, unsigned int *size, int *gzip);
+
 #endif

+ 7 - 0
rootfs.h

@@ -22,6 +22,13 @@
 #define ROOTFS_HDR_SIZE      512u
 #define ROOTFS_PACK_OFFSET   512u
 
+/* Installed image geometry (sector numbers are absolute disk LBAs). */
+#define NEBOS_BOOT_LBA       63u
+#define NEBOS_KERNEL_LBA     64u
+#define ROOTFS_LBA           1088u
+#define ROOTFS_SECTORS       16384u
+#define NEBOS_DATA_LBA       34816u
+
 struct rootfs_hdr {
 	char magic[8];
 	unsigned int version;

+ 1 - 1
terminal.c

@@ -88,7 +88,7 @@ static const struct kcmd cmds[] = {
 	{ "touchpad",   "touchpad [rightclick|tapclick|scroll]", CK_SPAWN_ARG, 0, 0, program_touchpad_main, 0, 0 },
 	{ "netdev",     "netdev [name]",                 CK_SPAWN_ARG, 0, 0, program_netdev_main, 0, 0 },
 	{ "format",     "format <device> [size]",        CK_CUSTOM, 0, 0, 0, 0, cmd_format },
-	{ "install",    "install <target> [source]",     CK_CUSTOM, 0, 0, 0, 0, cmd_install },
+	{ "install",    "install <target> [source|self]", CK_CUSTOM, 0, 0, 0, 0, cmd_install },
 	{ "lspci",      "lspci [id]",                    CK_SPAWN_ARG, 0, 0, program_lspci_main, 0, 0 },
 	{ "peek",       "peek <addr> [len]",             CK_SPAWN_ARG, 0, 0, program_peek_main, 0, 0 },
 	{ "poke",       "poke <addr> <val>",             CK_SPAWN_ARG, CMD_REQARG, 0, program_poke_main, 0, 0 },

+ 167 - 50
tools/qmp_install_test.py

@@ -4,16 +4,16 @@
 Boots the live USB image as an IDE disk, attaches a copy of the same image as
 a USB mass-storage device (the installer's source), adds a blank target disk,
 types `install ide1` on the terminal through QMP and confirms it.  The test
-then checks that the target is a byte-identical clone of the image's used
-sectors and boots the target in BIOS (and UEFI when the image has an ESP and
-OVMF is available).
+then checks the cloned P1..P3, the new /data partition (partition 4), writes a
+file into /data, runs a second install (which must keep it), and boots the
+target in BIOS and UEFI.
 
 Options:
   --xhci      put the USB source on an xHCI controller instead of EHCI/UHCI
   --keep      keep the temporary directory (target image and logs)
 
-Requires: usb-efi.img (or usb.img), python3, QEMU, and for the UEFI boot an
-OVMF firmware (OVMF=/path or the usual distribution paths).
+Requires: usb-efi.img (or usb.img), python3, QEMU, mtools, and for the UEFI
+boot an OVMF firmware (OVMF=/path or the usual distribution paths).
 Exit code 0 = pass.
 """
 import json
@@ -32,6 +32,9 @@ OVMF_CANDIDATES = [
     "/usr/share/OVMF/OVMF.4m.fd",
 ]
 
+DATA_LBA = 34816
+DATA_OFF = DATA_LBA * 512
+
 
 def qmp_recv(s):
     buf = b""
@@ -145,28 +148,7 @@ def run_boot(image, out_log, uefi=False, timeout=45):
     return serial_has(out_log, "booted")
 
 
-def main():
-    xhci = "--xhci" in sys.argv
-    keep = "--keep" in sys.argv
-
-    dual = os.path.join(ROOT, "usb-efi.img")
-    legacy = os.path.join(ROOT, "usb.img")
-    image = dual if os.path.exists(dual) else legacy
-    if not os.path.exists(image):
-        print("usb.img/usb-efi.img missing - run `make all`/`make uefi` first")
-        return 1
-
-    work = tempfile.mkdtemp(prefix="nebos-install-")
-    src = os.path.join(work, "source.img")
-    target = os.path.join(work, "target.img")
-    live_log = os.path.join(work, "live.log")
-    bios_log = os.path.join(work, "bios.log")
-    uefi_log = os.path.join(work, "uefi.log")
-    sock = os.path.join(work, "qmp.sock")
-    shutil.copyfile(image, src)
-    with open(target, "wb") as f:
-        f.truncate(32 * 1024 * 1024)
-
+def live_qemu(image, src, target, xhci, sock, log):
     if xhci:
         # qemu-xhci's first port is SuperSpeed: this exercises the USB3
         # path used by the Chromebook's USB 3.0 ports.  The HID devices
@@ -180,71 +162,206 @@ def main():
                "-device", "usb-storage,bus=ehci0.0,drive=src",
                "-device", "usb-mouse,bus=uhci0.0",
                "-device", "usb-kbd,bus=uhci0.0"]
-    qemu = ["qemu-system-i386", "-M", "pc", "-m", "512M", "-boot", "c"] + usb + [
+    return ["qemu-system-i386", "-M", "pc", "-m", "512M", "-boot", "c"] + usb + [
         "-drive", "if=none,id=src,file=%s,format=raw,readonly=on" % src,
         "-drive", "file=%s,format=raw,if=ide" % image,
         "-drive", "file=%s,format=raw,if=ide" % target,
         "-display", "none", "-qmp", "unix:%s,server,nowait" % sock,
-        "-serial", "file:%s" % live_log, "-no-reboot"]
+        "-serial", "file:%s" % log, "-no-reboot"]
 
-    proc = subprocess.Popen(qemu, stdout=subprocess.DEVNULL,
-                            stderr=subprocess.DEVNULL)
-    ok = False
+
+def run_live_install(image, src, target, xhci, sock, log, timeout=300,
+                     cmd="install ide1"):
+    """Boot the live image, run one install command, confirm, and return."""
+    qemu = live_qemu(image, src, target, xhci, sock, log)
+    qemu_log = log + ".qemu"
+    qerr = open(qemu_log, "wb")
+    proc = subprocess.Popen(qemu, stdout=qerr, stderr=subprocess.STDOUT)
     try:
         for _ in range(100):
-            if os.path.exists(sock):
+            if os.path.exists(sock) or proc.poll() is not None:
                 break
             time.sleep(0.1)
         s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-        s.connect(sock)
+        try:
+            s.connect(sock)
+        except OSError:
+            qerr.flush()
+            print("QEMU failed to start (see %s):" % qemu_log)
+            print(open(qemu_log, "rb").read().decode("latin-1", "replace"))
+            raise
         qmp_recv(s)                  # greeting
         qmp_cmd(s, "qmp_capabilities")
         time.sleep(10.0)             # boot the live system
 
-        send_text(s, "install ide1\n")
+        send_text(s, cmd + "\n")
         time.sleep(1.5)
         for _ in range(4):           # the prompt may not be in raw mode yet
             send_key(s, "y")
             time.sleep(0.7)
 
-        result = wait_serial(live_log, ["install: done", "install: failed"], 120)
-        qmp_cmd(s, "quit")
+        result = wait_serial(log, ["install: done", "install: self done",
+                                   "install: failed"], timeout)
+        s.settimeout(2)
+        try:
+            qmp_cmd(s, "quit")
+        except Exception:
+            pass
+        s.close()
         try:
             proc.wait(timeout=10)
         except subprocess.TimeoutExpired:
-            pass
+            proc.terminate()
+            proc.wait(timeout=5)
+        return result
+    finally:
+        if proc.poll() is None:
+            proc.kill()
+            proc.wait()
+        qerr.close()
+
+
+def mcopy_to_p4(target, src_file, dest):
+    return subprocess.run(
+        ["mcopy", "-i", "%s@@%d" % (target, DATA_OFF), src_file, "::/" + dest],
+        stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0
+
+
+def mcopy_from_p4(target, src, out_file):
+    return subprocess.run(
+        ["mcopy", "-i", "%s@@%d" % (target, DATA_OFF), "::/" + src, out_file],
+        stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode == 0
 
-        msc = serial_has(live_log, "usbmsc:")
-        used_line = serial_has(live_log, "install: usb0 -> ide1")
-        print("usbmsc source:", "ok" if msc else "MISSING")
-        print("installer used usb0:", "ok" if used_line else "NO")
-        print("install result:", result or "TIMEOUT")
+
+def p4_label(data):
+    if len(data) < DATA_OFF + 512:
+        return ""
+    boot = data[DATA_OFF:DATA_OFF + 512]
+    # FAT12/16 label at 0x2B, FAT32 at 0x47.  Our test target is small, so
+    # the formatter chooses FAT16; accept either offset.
+    for off in (0x2B, 0x47):
+        label = boot[off:off + 11].decode("latin-1").rstrip()
+        if label:
+            return label
+    return ""
+
+
+def main():
+    xhci = "--xhci" in sys.argv
+    keep = "--keep" in sys.argv
+
+    dual = os.path.join(ROOT, "usb-efi.img")
+    legacy = os.path.join(ROOT, "usb.img")
+    image = dual if os.path.exists(dual) else legacy
+    if not os.path.exists(image):
+        print("usb.img/usb-efi.img missing - run `make all`/`make uefi` first")
+        return 1
+
+    work = tempfile.mkdtemp(prefix="nebos-install-")
+    live = os.path.join(work, "live.img")
+    src = os.path.join(work, "source.img")
+    target = os.path.join(work, "target.img")
+    live1_log = os.path.join(work, "live1.log")
+    live2_log = os.path.join(work, "live2.log")
+    live3_log = os.path.join(work, "live3.log")
+    bios_log = os.path.join(work, "bios.log")
+    uefi_log = os.path.join(work, "uefi.log")
+    sock1 = os.path.join(work, "qmp1.sock")
+    sock2 = os.path.join(work, "qmp2.sock")
+    sock3 = os.path.join(work, "qmp3.sock")
+    keep_file = os.path.join(work, "KEEP.TXT")
+    out_file = os.path.join(work, "KEEP.OUT")
+    shutil.copyfile(image, live)
+    shutil.copyfile(image, src)
+    with open(target, "wb") as f:
+        f.truncate(32 * 1024 * 1024)
+    with open(keep_file, "w") as f:
+        f.write("persistent data\n")
+
+    ok = False
+    try:
+        result1 = run_live_install(live, src, target, xhci, sock1, live1_log)
+        print("install 1:", result1 or "TIMEOUT")
 
         src_data = open(src, "rb").read()
         used = image_used(src_data)
         tgt_data = open(target, "rb").read()
-        clone = used and tgt_data[:used * 512] == src_data[:used * 512]
-        print("image used: %d sectors, clone: %s" %
+        p1_mbr = tgt_data[0x1BE:0x1BE + 48] == src_data[0x1BE:0x1BE + 48]
+        clone = used and tgt_data[512:used * 512] == src_data[512:used * 512]
+        print("image used: %d sectors, P1-P3 clone: %s" %
               (used, "ok" if clone else "MISMATCH"))
 
+        p4 = tgt_data[0x1BE + 3 * 16:0x1BE + 4 * 16]
+        p4_start = rd32(p4[8:12])
+        p4_count = rd32(p4[12:16])
+        label = p4_label(tgt_data)
+        print("partition 4: start=%d count=%d label=%r" %
+              (p4_start, p4_count, label))
+        p4_ok = (p4_start == DATA_LBA and p4_count >= 2048 and
+                 label == "NEBOSDATA")
+        print("partition 4 layout:", "ok" if p4_ok else "BAD")
+
+        p4_boot_before = tgt_data[DATA_OFF:DATA_OFF + 512]
+        file_written = mcopy_to_p4(target, keep_file, "KEEP.TXT")
+        print("write /data/KEEP.TXT:", "ok" if file_written else "FAILED")
+
+        result2 = run_live_install(live, src, target, xhci, sock2, live2_log)
+        print("install 2:", result2 or "TIMEOUT")
+
+        tgt_data = open(target, "rb").read()
+        clone2 = used and tgt_data[512:used * 512] == src_data[512:used * 512]
+        p4_boot_after = tgt_data[DATA_OFF:DATA_OFF + 512]
+        p4_kept = p4_boot_before == p4_boot_after
+        file_ok = mcopy_from_p4(target, "KEEP.TXT", out_file)
+        file_data = ""
+        if file_ok:
+            file_data = open(out_file).read()
+        print("P1-P3 clone after reinstall:", "ok" if clone2 else "MISMATCH")
+        print("P4 boot sector kept:", "ok" if p4_kept else "CHANGED")
+        print("KEEP.TXT kept:", "ok" if file_data == "persistent data\n" else "FAILED")
+
+        # `install <target> self` writes the running pack/kernel, with no
+        # source disk.  It must also keep P4 and the file.
+        p4_boot_self = tgt_data[DATA_OFF:DATA_OFF + 512]
+        result3 = run_live_install(live, src, target, xhci, sock3,
+                                  live3_log, cmd="install ide1 self")
+        print("install self:", result3 or "TIMEOUT")
+        tgt_data = open(target, "rb").read()
+        clone3 = used and tgt_data[512:used * 512] == src_data[512:used * 512]
+        p4_kept3 = p4_boot_self == tgt_data[DATA_OFF:DATA_OFF + 512]
+        file_ok3 = mcopy_from_p4(target, "KEEP.TXT", out_file)
+        file_data3 = open(out_file).read() if file_ok3 else ""
+        print("P1-P3 clone after self:", "ok" if clone3 else "MISMATCH")
+        print("P4 boot sector after self:", "ok" if p4_kept3 else "CHANGED")
+        print("KEEP.TXT after self:", "ok" if file_data3 == "persistent data\n"
+              else "FAILED")
+
         bios = run_boot(target, bios_log, uefi=False)
         print("target BIOS boot:", "ok" if bios else "FAILED")
+        print("target /data mount:", "ok" if serial_has(bios_log, "vfs: data FAT")
+              else "MISSING")
         uefi = None
+        disk_boot = False
         if image_has_esp(src_data):
             uefi = run_boot(target, uefi_log, uefi=True)
+            disk_boot = serial_has(uefi_log, "kernel + rootfs loaded from disk")
             if uefi is None:
                 print("target UEFI boot: skipped (no OVMF)")
             else:
                 print("target UEFI boot:", "ok" if uefi else "FAILED")
+                print("UEFI disk image:", "ok" if disk_boot else "MISSING")
         else:
             print("target UEFI boot: skipped (source has no ESP)")
 
-        ok = bool(msc and used_line and result == "install: done" and clone
-                  and bios and (uefi is None or uefi))
+        ok = bool(result1 == "install: done" and result2 == "install: done" and
+                  result3 in ("install: done", "install: self done") and
+                  clone and clone2 and clone3 and p1_mbr and p4_ok and
+                  p4_kept and p4_kept3 and file_ok and
+                  file_data == "persistent data\n" and file_ok3 and
+                  file_data3 == "persistent data\n" and
+                  bios and serial_has(bios_log, "vfs: data FAT") and
+                  (uefi is None or (uefi and disk_boot)))
     finally:
-        if proc.poll() is None:
-            proc.kill()
-            proc.wait()
         if keep:
             print("kept:", work)
         else:

+ 59 - 16
vfs.c

@@ -8,6 +8,7 @@
 #include "kmalloc.h"
 #include "process.h"
 #include "ramfs.h"
+#include "rootfs.h"
 #include "serial.h"
 #include "share.h"
 #include "timer.h"
@@ -540,7 +541,7 @@ static const struct fs_ops ramfs_ops = {
 
 /* ---- mounts ---- */
 
-static vfs_mount_t *mount_slot(const char *name)
+static vfs_mount_t *mount_slot(const char *name, const char *dev)
 {
 	unsigned int i;
 
@@ -552,15 +553,54 @@ static vfs_mount_t *mount_slot(const char *name)
 		for (i = 0; name[i] && i < sizeof(m->name) - 1; i++)
 			m->name[i] = name[i];
 		m->name[i] = '\0';
+		m->dev[0] = '\0';
+		if (dev) {
+			for (i = 0; dev[i] && i < sizeof(m->dev) - 1; i++)
+				m->dev[i] = dev[i];
+			m->dev[i] = '\0';
+		}
 		m->mounted = 1;
 		return m;
 	}
 }
 
+static int name_eq(const char *a, const char *b)
+{
+	unsigned int i = 0;
+
+	while (a[i] && b[i] && a[i] == b[i])
+		i++;
+	return a[i] == '\0' && b[i] == '\0';
+}
+
+/* The P1 boot sector keeps only the NebOS header fields; a real FAT volume
+ * never carries these values.  Older installed media still has the original
+ * FAT-looking BPB (before boot.S was changed), so skip it explicitly instead
+ * of mounting the raw kernel partition as a fake filesystem. */
+static int is_nebos_boot_partition(blk_dev_t *d, unsigned int start_lba)
+{
+	unsigned char s[512];
+	unsigned int i;
+
+	if (blk_read(d, start_lba, s, 1) != 0)
+		return 0;
+	if (s[510] != 0x55 || s[511] != 0xAA)
+		return 0;
+	if (s[3] != 'N' || s[4] != 'E' || s[5] != 'B' || s[6] != 'O' ||
+	    s[7] != 'S')
+		return 0;
+	if ((s[0x40] | (s[0x41] << 8)) != NEBOS_KERNEL_LBA)
+		return 0;
+	i = s[0x42] | (s[0x43] << 8) | (s[0x44] << 16) |
+	    ((unsigned int)s[0x45] << 24);
+	return i == ROOTFS_LBA;
+}
+
 static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
 {
 	fat_fs_t fs;
 	vfs_mount_t *m;
+	const char *use_name = name;
 	int r;
 
 	fat_cache_clear();
@@ -572,7 +612,11 @@ static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
 		serial_puts(SERIAL_COM1, " no FAT\n");
 		return;
 	}
-	m = mount_slot(name);
+	/* The labelled data partition has a stable path independent of the
+	 * device name.  Only the first NEBOSDATA volume becomes /data. */
+	if (name_eq(fs.label, "NEBOSDATA") && !vfs_find("data"))
+		use_name = "data";
+	m = mount_slot(use_name, dev->name);
 	if (!m)
 		return;
 	m->ops = &fat_ops;
@@ -583,7 +627,7 @@ static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
 	copy_bounded(m->label, fs.label, sizeof(m->label));
 	vfs_mount_count++;
 	serial_puts(SERIAL_COM1, "vfs: ");
-	serial_puts(SERIAL_COM1, name);
+	serial_puts(SERIAL_COM1, use_name);
 	serial_puts(SERIAL_COM1, " ");
 	serial_puts(SERIAL_COM1, fat_name(fs.type));
 	serial_puts(SERIAL_COM1, "\n");
@@ -595,7 +639,7 @@ void vfs_mount_ramfs(void)
 
 	/* 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");
+	m = mount_slot("ramfs", 0);
 	if (!m)
 		return;
 	m->ops = &ramfs_ops;
@@ -622,7 +666,7 @@ void vfs_mount_share(void)
 
 	if (share_init() != 0)
 		return;
-	m = mount_slot("share");
+	m = mount_slot("share", 0);
 	if (!m)
 		return;
 	m->ops = &share_ops;
@@ -667,6 +711,10 @@ static void mount_device(blk_dev_t *d, unsigned char *buf)
 		if (!parts[j].is_fat || parts[j].start_lba == 0 ||
 		    parts[j].sectors == 0)
 			continue;
+		/* The active NebOS partition holds the raw boot sector and
+		 * kernel, not a filesystem (old images still look like FAT). */
+		if (is_nebos_boot_partition(d, parts[j].start_lba))
+			continue;
 		any_fat = 1;
 		part_name(pname, d->name, parts[j].index);
 		mount_one(d, parts[j].start_lba, pname);
@@ -675,16 +723,11 @@ static void mount_device(blk_dev_t *d, unsigned char *buf)
 		mount_one(d, 0, d->name);
 }
 
-/* Mount names for a device are the device itself or "<device>pN". */
-static int mount_belongs_to(const char *mount, const char *dev)
+/* Mounts remember their source device, so /data can be named independently
+ * of <device>pN and still be found by a remount. */
+static int mount_belongs_to(const vfs_mount_t *m, const char *dev)
 {
-	unsigned int i = 0;
-
-	while (dev[i] && mount[i] == dev[i])
-		i++;
-	if (dev[i] != '\0')
-		return 0;
-	return mount[i] == '\0' || mount[i] == 'p';
+	return m->dev[0] != '\0' && name_eq(m->dev, dev);
 }
 
 static void unmount_device(const char *dev)
@@ -692,7 +735,7 @@ static void unmount_device(const char *dev)
 	unsigned int i, j;
 
 	for (i = 0; i < vfs_mount_count; ) {
-		if (mount_belongs_to(vfs_mounts[i].name, dev)) {
+		if (mount_belongs_to(&vfs_mounts[i], dev)) {
 			for (j = i + 1; j < vfs_mount_count; j++)
 				vfs_mounts[j - 1] = vfs_mounts[j];
 			vfs_mount_count--;
@@ -745,7 +788,7 @@ static int device_is_mounted(const char *dev)
 	unsigned int i;
 
 	for (i = 0; i < vfs_mount_count; i++)
-		if (mount_belongs_to(vfs_mounts[i].name, dev))
+		if (mount_belongs_to(&vfs_mounts[i], dev))
 			return 1;
 	return 0;
 }

+ 1 - 0
vfs.h

@@ -60,6 +60,7 @@ struct fs_ops {
 
 typedef struct vfs_mount {
 	char name[16];
+	char dev[16];                /* block device name for pN mounts */
 	const struct fs_ops *ops;
 	int mounted;
 	int ro;

Неке датотеке нису приказане због велике количине промена