Răsfoiți Sursa

Add USB mass storage and an image-clone installer

Add a USB mass-storage class driver (Bulk-Only Transport + SCSI) on the
USB core's device-probe hook next to ASIX.  It parses class 8/subclass 6/
protocol 0x50 interfaces, uses the bulk endpoints on EHCI and xHCI, and
implements INQUIRY, TEST UNIT READY, READ CAPACITY(10), READ(10),
WRITE(10) and SYNCHRONIZE CACHE with BOT-reset recovery.  blk_scan()
registers the disks as usb0.. (BLK_USB), so storage, the VFS, format and
install can use them.  Allow several USB device probes, and stop the
xHCI bulk-IN path from re-arming the previous buffer so a consumer can
alternate CBW, data and CSW buffers.

Add the privileged install <target> [source] command.  It detects a
NebOS image source by the MBR plus the LBA-63 boot-sector signature
(preferring usb0 over an internal copy), copies the used sectors from
the MBR through the last partition, writes the source MBR last, flushes
and verifies the target by checksum, then remounts it.  A clone keeps
both boot paths of usb-efi.img (the legacy MBR chain and the ESP/UEFI
chain) and is byte-identical to the live image's used area; usb.img
clones are BIOS-only.  The copy holds the BKL so a USB source cannot be
re-enumerated mid-install.

Update AGENTS.md and the architecture pages.  Add
tools/qmp_install_test.py and make test-install: it boots the dual image
with a USB mass-storage copy of the source and a blank target, types
install ide1 through QMP, verifies the clone and boots the target in
BIOS and under OVMF.

Validated with make test-install and its --xhci variant, make test-uefi,
and the EHCI/xHCI QMP keyboard tests.  Limits: UHCI/OHCI have no bulk
path, xHCI USB3 SuperSpeed ports are not driven, and only BOT/SCSI
512-byte media is handled; the rest of the target stays unpartitioned.
Johannes Zellner 1 săptămână în urmă
părinte
comite
fae057fb95
20 a modificat fișierele cu 1469 adăugiri și 49 ștergeri
  1. 49 6
      AGENTS.md
  2. 15 3
      Makefile
  3. 11 0
      blkdev.c
  4. 1 1
      blkdev.h
  5. 15 0
      docs/boot.html
  6. 4 2
      docs/development.html
  7. 44 16
      docs/drivers.html
  8. 7 1
      docs/filesystems.html
  9. 2 2
      docs/index.html
  10. 11 1
      docs/userland.html
  11. 468 0
      install.c
  12. 2 0
      kernel.c
  13. 1 1
      syscall.h
  14. 10 0
      terminal.c
  15. 255 0
      tools/qmp_install_test.py
  16. 20 4
      usb.c
  17. 4 3
      usb.h
  18. 520 0
      usbmsc.c
  19. 25 0
      usbmsc.h
  20. 5 9
      xhci.c

+ 49 - 6
AGENTS.md

@@ -22,7 +22,7 @@ this file **and** the matching page:
 | `docs/boot.html` | BIOS/VESA, MBR, UEFI loader, kexec, boot info, image layout |
 | `docs/memory.html` | Address map, E820/bitmap allocator, paging, MTRR, user arenas |
 | `docs/cpu.html` | GDT/TSS, interrupts, ACPI/SMP, CFS-lite, BKL, idle/power |
-| `docs/drivers.html` | PCI, FDC/PATA/AHCI, USB core + UHCI/OHCI/EHCI/xHCI, HID, sensors |
+| `docs/drivers.html` | PCI, FDC/PATA/AHCI, USB core + UHCI/OHCI/EHCI/xHCI, HID, mass storage, sensors |
 | `docs/filesystems.html` | VFS/`fs_ops`, FAT, ramfs pack, `/share`, WebDAV |
 | `docs/graphics.html` | VBE/shadow, damage, blit, WM, canvas ABI, ngui, images |
 | `docs/network.html` | netdev, ARP/ICMP/UDP/DHCP/DNS, TCP, TLS, netconsole/netkexec |
@@ -65,6 +65,7 @@ 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 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)
@@ -107,7 +108,9 @@ make clean
   CHS-only USB BIOS can read.  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
   as partition 3 (type `0xEF`, LBA 18432); `BOOTX64.EFI` embeds the kernel +
-  gz pack.
+  gz pack.  The `install` command clones this used image (MBR through the
+  last partition) from a visible block device, so a cloned target keeps both
+  boot paths.
   `make flash` writes the dual image; `make flash FLASH_IMG=usb.img` writes
   the legacy-only image.  One BPB byte is cleared in the dual image (disk
   offset `0x7E16`, FATSize16): without that, UEFI firmware scans partition 1,
@@ -439,7 +442,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   see the panic section below.
 - **Filesystem**: `fat.c` (FAT12/16/32 read/write, device-parameterized),
   `vfs.c` (mounts every FAT device under `/`), `fatdetect.c`/`fatformat.c`,
-  `blkdev.c` (floppy + IDE + SATA + eMMC registry), `ata.c` (ATA PIO, either the
+  `blkdev.c` (floppy + IDE + SATA + eMMC + USB registry), `ata.c` (ATA PIO, either the
   legacy ports or the PCI IDE interface's native BARs — `ata_init_ports()`;
   multi-sector PIO waits for DRQ before every sector, since drives drop it
   between sectors, polls the command-block status rather than the alt-status
@@ -465,6 +468,16 @@ 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,
+  copies sectors 1..end in chunks, writes the source MBR **last**, flushes and
+  verifies by checksum before remounting the target.  The source can be
+  `usb0` (USB mass storage), IDE, SATA or eMMC; the target is any non-floppy
+  block device with enough sectors.  It reuses `format_busy`; the copy holds
+  the BKL so a USB source cannot be re-enumerated mid-install.  It installs
+  the flashed image, not runtime `cmds-dev` pack updates.
 - **`/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
@@ -591,6 +604,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   asynchronous schedule, up to 4 instances) and `xhci.c` (MMIO xHCI, USB2
   root ports; halt/HCH reset sequence and BIOS/OS handoff) implement it.
   `lsusb.c` — the `lsusb` command (lists every controller and its tree).
+  `usbmsc.c/h` — USB mass-storage class driver (BOT/SCSI) exposed as
+  `usb0..`; its device probe is registered next to ASIX and `blk_scan()`
+  registers the devices in the block registry.
   The devices work on every controller type and
   can be plugged in at any time (root-port connect-change watcher); `lsusb`
   re-enumerates the bus, after which `usbhid` re-inits itself via a scan
@@ -940,7 +956,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`, `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),
+`display`, `cpuinfo`, `mouseinfo`, `gfxbench`, `lsusb`, `touchpad`, `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`),
 `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), and `browser [url|file]` (disk app launcher).
 
 Network: `netinfo`, `netrecv`, `netsend`, `netip [a.b.c.d]`, `dhcp` (re-run the
@@ -1210,6 +1226,20 @@ so addresses do not collide.
   Devices plugged in after boot bind through `asix.c`, which calls
   `netdev_hotplug()` so the dongle becomes the active NIC when the boot
   probe found none.
+- **Mass storage** (`usbmsc.c/h`): the device probe claims a class 8 /
+  subclass 6 / protocol 0x50 (SCSI transparent, bulk-only) interface, parses
+  its bulk IN/OUT endpoints, sets the configuration and identifies the disk
+  with SCSI INQUIRY + READ CAPACITY.  Commands use BOT CBW/CSW with LUN 0,
+  512-byte blocks and at most 62 sectors per data stage; READ(10), WRITE(10),
+  SYNCHRONIZE CACHE, a BOT reset and endpoint-halt clear are implemented.
+  `blk_scan()` registers present devices as `usb0..` (`BLK_USB`), so
+  `storage`, the VFS mount path, `format` and `install` can use them.  Bulk
+  transfers exist only on EHCI and xHCI; a storage device on a UHCI/OHCI
+  companion is left unclaimed.  The xHCI bulk-IN path arms one transfer per
+  call and does not auto re-arm, so a consumer can alternate CBW, data and
+  CSW buffers.  The installer reads the live image from `usb0`; USB3
+  SuperSpeed ports (not driven by `xhci.c`) and UAS/4Kn sticks are not
+  usable.
 - **Devices** (`usbhid.c`): every HID (class 3) interface with an
   interrupt-IN endpoint gets its own interrupt slot (USB_HCD_INT_SLOTS = 6).
   Boot interfaces (subclass 1, protocol 2/1) are used as-is in boot
@@ -1414,7 +1444,7 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 7. `display` (needs a GPU snapshot) and `lsusb` (a 45-byte wrapper around
    the USB tree printer) are the last non-privileged commands still living in
    the kernel, plus the privileged ones (`peek`, `poke`, `dbg`, `serial`,
-   `kexec`, `reboot`, `poweroff`, `format`, `ps`/`kill`).
+   `kexec`, `reboot`, `poweroff`, `format`, `install`, `ps`/`kill`).
 8. The rootfs pack is inflated into one contiguous page-allocated run at
    boot, capped by `RAMFS_MAX_DECOMPRESSED` (8 MB); a larger pack fails to
    load (the USB rootfs partition is 8 MB, so this only bites if the cap is
@@ -1435,7 +1465,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
 11. The BKL serializes all kernel execution: a long FAT/disk operation, a
     non-network driver wait or a big shadow-buffer operation can stall syscalls
     on the other CPUs for its duration.  `bkl_pause()`/`bkl_resume()` is used
-    for serial/network waits, not yet for disk I/O.
+    for serial/network waits and the format raw-sector loop; the installer
+    holds the BKL for the copy so a USB source cannot be re-enumerated
+    mid-install.
 12. Kernel threads (kworker, kexec/payload, kernel commands) are pinned to
     the BSP.  Only ring-3 processes use the APs, so a single CPU-bound kernel
     driver still limits throughput.
@@ -1645,3 +1677,14 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     cache flush uses CMD6 FLUSH_CACHE when EXT_CSD CACHE_SIZE is non-zero.
     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/xHCI USB2 bulk only (no UHCI/OHCI bulk path, no USB3
+    SuperSpeed ports, no UAS or 4Kn), so a USB3-only port or an unsupported
+    stick leaves no source.  The copy holds the BKL and can freeze the UI for
+    its duration; a failed copy leaves an incomplete target.  IDE, SATA and
+    eMMC sources work without USB, but a target smaller than the image is
+    refused.

+ 15 - 3
Makefile

@@ -66,7 +66,7 @@ EFI_LDFLAGS = -nostdlib -Wl,--subsystem,10 -Wl,-e,efi_main \
               -Wl,--image-base,0x0 -Wl,--dynamicbase \
               -Wl,--enable-reloc-section -Wl,--no-insert-timestamp
 
-.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-ehci run-ehci-asix run-xhci run-xhci-asix run-net run-e1000 run-ahci test-usb run-serial floppy usb uefi run-uefi test-uefi flash dev net-dev cmds-dev net-cmds browser-dev gallery-dev compat-serve netmon monitor test-crypto test-images test-images-asan test-libc test-font test-font-asan fonts kernel-dev kernel-net-dev rootfs
+.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-ehci run-ehci-asix run-xhci run-xhci-asix run-net run-e1000 run-ahci test-usb test-install run-serial floppy usb uefi run-uefi test-uefi flash dev net-dev cmds-dev net-cmds browser-dev gallery-dev compat-serve netmon monitor test-crypto test-images test-images-asan test-libc test-font test-font-asan fonts kernel-dev kernel-net-dev rootfs
 
 all: floppy.img usb.img final.bin root.img
 
@@ -286,6 +286,12 @@ fatformat.o: fatformat.c fatformat.h blkdev.h io.h mm.h sched.h
 format.o: format.c blkdev.h disk.h fatformat.h io.h process.h sched.h vga.h vfs.h
 	$(CC) $(CFLAGS) -Os -o $@ $<
 
+install.o: install.c blkdev.h fatformat.h io.h process.h serial.h vga.h vfs.h
+	$(CC) $(CFLAGS) -Os -o $@ $<
+
+usbmsc.o: usbmsc.c usbmsc.h blkdev.h serial.h timer.h usb.h
+	$(CC) $(CFLAGS) -Os -o $@ $<
+
 mouse.o: mouse.c mouse.h bootinfo.h cirrus.h cursor_shape.h radeon.h timer.h touchpad.h usbhid.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -388,7 +394,7 @@ sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h nt
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o sdhci.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o inflate.o ramfs.o share.o payload.o
+kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbmsc.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o sdhci.o ata.o ahci.o fatdetect.o fatformat.o format.o install.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o inflate.o ramfs.o share.o payload.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $(filter-out kernel.ld,$^)
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -1073,6 +1079,12 @@ test-usb: floppy.img root.img
 	python3 tools/qmp_keyboard_test.py --xhci
 	python3 tools/qmp_keyboard_test.py --ehci
 
+# End-to-end installer test: boot the dual-boot USB image as an IDE disk,
+# attach the same image as a USB mass-storage source, type `install ide1`
+# on the terminal and verify + boot the cloned target.
+test-install: usb-efi.img
+	python3 tools/qmp_install_test.py
+
 scp-final: final.bin
 	scp final.bin root@192.168.0.136:
 	ssh root@192.168.0.136 dd if=final.bin of=/dev/fd0 bs=512
@@ -1174,4 +1186,4 @@ kernel-net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o disk.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o sdhci.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img hello.elf cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h libc.a libc/*.o malloc_test format_test math_test div64_test setjmp_test mujs_test js_browser.o $(MUJS_DIR)/mujs.o ufont.o ufont_ttf.o ufont_draw.o ufont_file.o ufont_test ufont_test_asan
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o disk.o serial.o serial_ring_buf.o serial_loop.o vga.o blit.o cpu.o thermal.o it87.o tpec.o ec.o cros_ec.o power_src.o power.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o intel.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o display.o mouse.o i2c.o touchpad.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbmsc.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o sdhci.o ata.o ahci.o fatdetect.o fatformat.o format.o install.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o ntp.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img hello.elf cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h libc.a libc/*.o malloc_test format_test math_test div64_test setjmp_test mujs_test js_browser.o $(MUJS_DIR)/mujs.o ufont.o ufont_ttf.o ufont_draw.o ufont_file.o ufont_test ufont_test_asan

+ 11 - 0
blkdev.c

@@ -8,6 +8,7 @@
 #include "sdhci.h"
 #include "serial.h"
 #include "timer.h"
+#include "usbmsc.h"
 
 blk_dev_t blk_devs[BLKDEV_MAX];
 unsigned int blk_dev_count;
@@ -154,6 +155,10 @@ void blk_scan(void)
 			}
 		}
 	}
+
+	/* USB mass storage: the bus scan ran before the first blk_scan;
+	 * register the devices it found. */
+	usbmsc_blk_scan();
 }
 
 static int name_eq(const char *a, const char *b)
@@ -188,6 +193,8 @@ int blk_read(blk_dev_t *d, unsigned int lba, void *buf, unsigned int count)
 		return ahci_read_sectors(d->unit, lba, buf, count);
 	if (d->kind == BLK_MMC)
 		return sdhci_read_sectors(lba, buf, count);
+	if (d->kind == BLK_USB)
+		return usbmsc_read(d->unit, lba, buf, count);
 	return ata_read_sectors(d->bus, d->unit, lba, buf, count);
 }
 
@@ -203,6 +210,8 @@ int blk_write(blk_dev_t *d, unsigned int lba, const void *buf, unsigned int coun
 		return ahci_write_sectors(d->unit, lba, buf, count);
 	if (d->kind == BLK_MMC)
 		return sdhci_write_sectors(lba, buf, count);
+	if (d->kind == BLK_USB)
+		return usbmsc_write(d->unit, lba, buf, count);
 	return ata_write_sectors(d->bus, d->unit, lba, buf, count);
 }
 
@@ -216,5 +225,7 @@ int blk_flush(blk_dev_t *d)
 		return ahci_flush(d->unit);
 	if (d->kind == BLK_MMC)
 		return sdhci_flush();
+	if (d->kind == BLK_USB)
+		return usbmsc_flush(d->unit);
 	return ata_flush(d->bus, d->unit);
 }

+ 1 - 1
blkdev.h

@@ -3,7 +3,7 @@
 #ifndef BLKDEV_H
 #define BLKDEV_H
 
-typedef enum { BLK_FLOPPY, BLK_IDE, BLK_SATA, BLK_MMC } blk_kind_t;
+typedef enum { BLK_FLOPPY, BLK_IDE, BLK_SATA, BLK_MMC, BLK_USB } blk_kind_t;
 
 typedef struct {
 	const char *name;          /* "floppy0","floppy1","ide0".."ide3",

+ 15 - 0
docs/boot.html

@@ -346,6 +346,20 @@
 				</tbody>
 			</table>
 		</div>
+
+		<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. 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 copy writes
+			the source MBR last, flushes and verifies the target. The rest of the
+			target stays unpartitioned, and runtime <code>cmds-dev</code> pack
+			updates are not part of the cloned image.
+		</p>
 	</section>
 
 	<section id="init">
@@ -405,6 +419,7 @@
 			<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>kernel.ld</code> — image limits</li>
 			<li><code>Makefile</code> — image sizes and layout rules</li>
 		</ul>

+ 4 - 2
docs/development.html

@@ -162,7 +162,7 @@
 					<tr><td><code>make all</code></td><td>Build <code>floppy.img</code>, <code>usb.img</code>, <code>final.bin</code> and <code>root.img</code>.</td></tr>
 					<tr><td><code>make run</code> / <code>run-smp</code></td><td>Boot the floppy in QEMU, optionally with four CPUs (Atom 330 topology).</td></tr>
 					<tr><td><code>make run-usb</code></td><td>Boot <code>usb.img</code> as an IDE disk.</td></tr>
-					<tr><td><code>make run-uhci</code> / <code>run-ehci</code> / <code>run-xhci</code></td><td>Exercise the USB backends with a mouse/keyboard (and storage on EHCI).</td></tr>
+					<tr><td><code>make run-uhci</code> / <code>run-ehci</code> / <code>run-xhci</code></td><td>Exercise the USB backends with a mouse/keyboard; the EHCI target also attaches a USB mass-storage disk, and the installer test covers EHCI and xHCI storage.</td></tr>
 					<tr><td><code>make run-ehci-asix</code> / <code>run-xhci-asix</code></td><td>Pass through a real ASIX AX88772 dongle.</td></tr>
 					<tr><td><code>make run-net</code></td><td>Add a PCI NE2000 on QEMU's slirp network with host port 8080 forwarded.</td></tr>
 					<tr><td><code>make run-ahci</code></td><td>Add an ICH9 AHCI controller and a 512 MB SATA disk.</td></tr>
@@ -175,6 +175,7 @@
 					<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>
 					<tr><td><code>make test-usb</code></td><td>Headless QEMU mouse/keyboard regression tests using QMP and screen dumps.</td></tr>
+					<tr><td><code>make test-install</code></td><td>Boots the dual image with a USB mass-storage source and a blank target, types <code>install ide1</code>, verifies the clone and boots the target in BIOS and UEFI.</td></tr>
 					<tr><td><code>make test-images</code> / <code>test-images-asan</code></td><td>Host image-decoder and blit tests, with an AddressSanitizer variant.</td></tr>
 					<tr><td><code>make test-crypto</code></td><td>Host vectors for SHA-256, ChaCha20-Poly1305, X25519 and TLS helpers.</td></tr>
 					<tr><td><code>make test-libc</code></td><td>Host tests for the ring-3 malloc, stdio/float formatting, math, 64-bit division helpers and setjmp/longjmp.</td></tr>
@@ -261,6 +262,7 @@ make net-cmds                   # replace /ramfs only</code></pre>
 		<h2>Tests</h2>
 		<ul>
 			<li><strong>USB regression.</strong> <code>make test-usb</code> runs QMP cursor and keyboard tests against UHCI, OHCI, EHCI and xHCI, including hotplug.</li>
+			<li><strong>Installer.</strong> <code>make test-install</code> boots the dual image with its USB mass-storage source and a blank target, types <code>install ide1</code> through QMP, verifies the clone and boots the target in BIOS and (under OVMF) UEFI.</li>
 			<li><strong>Image library.</strong> <code>make test-images</code> decodes a corpus of PNG/JPEG/GIF/BMP/TGA/PNM/ICO/PAM files and checks blits; the ASan variant catches memory errors.</li>
 			<li><strong>Crypto.</strong> <code>make test-crypto</code> runs host vectors for the freestanding crypto code.</li>
 			<li><strong>Ring-3 libc.</strong> <code>make test-libc</code> runs the malloc stress test (32 MB simulated arena with poisoned pages), exact <code>snprintf</code> float/string checks against the host, x87 math values, the 64-bit division helpers and a 32-bit setjmp/longjmp test.</li>
@@ -343,7 +345,7 @@ make net-cmds                   # replace /ramfs only</code></pre>
 			<li><code>dbg.c</code> — serial debug monitor</li>
 			<li><code>panic.c</code> — fatal exception dump</li>
 			<li><code>kernel.ld</code>, <code>ucmd.ld</code> — link limits</li>
-			<li><code>tools/</code> — QMP USB tests</li>
+			<li><code>tools/</code> — QMP USB and installer tests</li>
 			<li><code>AGENTS.md</code> — developer notes and maintenance rule</li>
 		</ul>
 	</section>

+ 44 - 16
docs/drivers.html

@@ -4,7 +4,7 @@
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>Drivers &amp; Buses | NebOS Architecture</title>
-<meta name="description" content="NebOS drivers: PCI enumeration, floppy/ATA/AHCI storage, the USB core with UHCI, OHCI, EHCI and xHCI, HID input, serial, board sensors and the panic path.">
+<meta name="description" content="NebOS drivers: PCI enumeration, floppy/ATA/AHCI storage, the USB core with UHCI, OHCI, EHCI and xHCI, HID input, USB mass storage, serial, board sensors and the panic path.">
 <link rel="icon" href="favicon.svg" type="image/svg+xml">
 <link rel="stylesheet" href="style.css">
 </head>
@@ -116,21 +116,22 @@
 		<h2>Storage</h2>
 		<p>
 			<span class="file">blkdev.c</span> is a small registry of block
-			devices. Each device exposes sector read/write plus geometry; FAT and
-			the format command sit above that interface and never talk to a
-			controller directly. Devices are named <code>floppy0</code>,
-			<code>ide0</code>, <code>sata0</code>, <code>mmc0</code> and so on; partitions append
+			devices. Each device exposes sector read/write plus geometry; FAT,
+			the format command and the installer sit above that interface and
+			never talk to a controller directly. Devices are named
+			<code>floppy0</code>, <code>ide0</code>, <code>sata0</code>,
+			<code>mmc0</code>, <code>usb0</code> and so on; partitions append
 			<code>p1</code>, <code>p2</code>.
 		</p>
 
 		<figure class="figure">
 			<svg viewBox="0 0 920 246" role="img" aria-labelledby="storage-title storage-desc">
 				<title id="storage-title">Storage software layers</title>
-				<desc id="storage-desc">The VFS and format tools use a block-device registry, which dispatches to the floppy, ATA PIO or AHCI driver, then to the physical disk.</desc>
+				<desc id="storage-desc">The VFS, format and install tools use a block-device registry, which dispatches to the floppy, ATA PIO, AHCI, SDHCI or USB mass-storage driver, then to the physical disk.</desc>
 
 				<rect class="svg-box blue" x="40" y="30" width="230" height="62" rx="9"/>
-				<text class="svg-text" x="60" y="57">VFS · FAT · format</text>
-				<text class="svg-sub" x="60" y="77">files, not sectors</text>
+				<text class="svg-text" x="60" y="57">VFS · FAT · format · install</text>
+				<text class="svg-sub" x="60" y="77">files, or a whole-image clone</text>
 
 				<rect class="svg-box accent" x="345" y="30" width="230" height="62" rx="9"/>
 				<text class="svg-text" x="365" y="57">blkdev registry</text>
@@ -138,7 +139,7 @@
 
 				<rect class="svg-box" x="650" y="30" width="230" height="62" rx="9"/>
 				<text class="svg-text" x="670" y="57">driver</text>
-				<text class="svg-sub" x="670" y="77">FDC · ATA · AHCI · SDHCI</text>
+				<text class="svg-sub" x="670" y="77">FDC · ATA · AHCI · SDHCI · USB</text>
 
 				<path class="svg-line blue" d="M270 61h67"/>
 				<path class="svg-line blue" d="M330 55l7 6-7 6"/>
@@ -154,8 +155,8 @@
 				<text class="svg-sub" x="365" y="189">PIO, legacy or native BARs</text>
 
 				<rect class="svg-box green" x="650" y="142" width="230" height="62" rx="9"/>
-				<text class="svg-text" x="670" y="169">sata0 · mmc0</text>
-				<text class="svg-sub" x="670" y="189">AHCI port · SDHCI PIO</text>
+				<text class="svg-text" x="670" y="169">sata0 · mmc0 · usb0</text>
+				<text class="svg-sub" x="670" y="189">AHCI · SDHCI · USB MSC</text>
 
 				<path class="svg-line" d="M175 92v42"/>
 				<path class="svg-line" d="M169 126l6 7 6-7"/>
@@ -167,8 +168,10 @@
 			<figcaption>
 				<strong>One narrow waist.</strong> Filesystems and tools only see
 				<code>struct blk_dev</code>. The floppy still needs IRQ 6, but ATA,
-				AHCI and SDHCI are polled; a FAT operation takes <code>sys_fs_busy</code>
-				so the scheduler does not switch away mid-transfer.
+				AHCI, SDHCI and USB MSC are polled; a FAT operation takes a
+				non-preemptible section and <code>format_busy</code> keeps a
+				destructive format/install exclusive. The installer clones the
+				MBR and every used sector of the source image.
 			</figcaption>
 		</figure>
 
@@ -228,7 +231,7 @@
 		<figure class="figure">
 			<svg viewBox="0 0 920 300" role="img" aria-labelledby="usb-title usb-desc">
 				<title id="usb-title">USB controller topology</title>
-				<desc id="usb-desc">EHCI handles high-speed root ports, UHCI and OHCI handle low and full speed devices, xHCI handles USB2 root ports. All feed the same USB core and HID layer.</desc>
+				<desc id="usb-desc">EHCI handles high-speed root ports, UHCI and OHCI handle low and full speed devices, xHCI handles USB2 root ports. All feed the same USB core, which drives HID input and USB mass storage.</desc>
 
 				<rect class="svg-box accent" x="30" y="24" width="200" height="64" rx="9"/>
 				<text class="svg-text" x="50" y="50">EHCI</text>
@@ -258,8 +261,9 @@
 				<path class="svg-line" d="M460 214v34"/>
 				<path class="svg-line" d="M454 240l6 7 6-7"/>
 
-				<rect class="svg-box" x="300" y="248" width="320" height="44" rx="9"/>
-				<text class="svg-text" x="320" y="275">usbhid · keyboard + mouse</text>
+				<rect class="svg-box" x="300" y="248" width="320" height="50" rx="9"/>
+				<text class="svg-text" x="320" y="266">usbhid · keyboard + mouse</text>
+				<text class="svg-text" x="320" y="286">usbmsc · mass storage</text>
 			</svg>
 			<figcaption>
 				<strong>Companions are not optional.</strong> EHCI is scanned first
@@ -292,6 +296,28 @@
 			full scan runs (<code>lsusb</code> also bumps the USB generation so
 			<code>usbhid</code> re-attaches).
 		</p>
+
+		<h3>Mass storage</h3>
+		<p>
+			<span class="file">usbmsc.c</span> is a device-level probe alongside
+			the ASIX Ethernet driver. It claims an interface with class 8,
+			subclass 6 and protocol 0x50, parses its bulk IN/OUT endpoints and
+			identifies the disk with SCSI INQUIRY + READ CAPACITY. Commands use
+			Bulk-Only Transport with LUN 0 and 512-byte blocks: READ(10),
+			WRITE(10), SYNCHRONIZE CACHE and a BOT reset/endpoint-halt recovery.
+			Present devices are registered as <code>usb0..</code>
+			(<code>BLK_USB</code>) by <code>blk_scan()</code>, so
+			<code>storage</code>, the VFS, <code>format</code> and
+			<code>install</code> can use them.
+		</p>
+		<p>
+			Bulk transfers exist on EHCI and xHCI only; a mass-storage device on
+			a UHCI/OHCI companion is left unclaimed, and the xHCI driver does not
+			drive USB3 SuperSpeed ports. This is what makes the installer's
+			<code>usb0</code> source possible on the supported hardware: it reads
+			the live image from the stick and clones its used sectors to the
+			target.
+		</p>
 	</section>
 
 	<section id="hid">
@@ -457,6 +483,8 @@
 			<li><code>ata.c</code>, <code>ahci.c</code> — PATA/SATA</li>
 			<li><code>usb.c</code>, <code>usb_hcd.h</code> — USB core</li>
 			<li><code>uhci.c</code>, <code>ohci.c</code>, <code>ehci.c</code>, <code>xhci.c</code></li>
+			<li><code>usbmsc.c</code>, <code>usbmsc.h</code> — USB BOT/SCSI mass storage</li>
+			<li><code>install.c</code> — live-image clone installer</li>
 			<li><code>usbhid.c</code>, <code>mouse.c</code>, <code>io.c</code> — input</li>
 			<li><code>i2c.c</code>, <code>i2c.h</code> — DesignWare/LPSS I2C master</li>
 			<li><code>touchpad.c</code>, <code>touchpad.h</code> — Elan HID-over-I2C touchpad</li>

+ 7 - 1
docs/filesystems.html

@@ -151,7 +151,11 @@
 			<span class="file">fat.c</span> supports FAT12, FAT16 and FAT32 on
 			any registered block device. It reads the BPB, validates the media
 			byte and mounts partitions as separate entries such as
-			<code>ide0p1</code> or <code>sata0p2</code>.
+			<code>ide0p1</code>, <code>sata0p2</code> or <code>usb0p1</code>.
+			USB mass-storage devices are registered by
+			<span class="file">usbmsc.c</span> and mount exactly like an
+			internal disk; the <code>install</code> command is separate and
+			clones raw image sectors without parsing FAT.
 		</p>
 		<ul>
 			<li><strong>Read/write.</strong> Read, write, delete, mkdir and rmdir are implemented; FAT32 allocation uses an FSInfo-seeded free-cluster hint.</li>
@@ -386,6 +390,8 @@
 			<li><code>inflate.c</code> — DEFLATE/gzip</li>
 			<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>
 		</ul>
 	</section>
 </main>

+ 2 - 2
docs/index.html

@@ -261,11 +261,11 @@
 					</tr>
 					<tr>
 						<td>Devices</td>
-						<td><span class="file">usb.c</span> <span class="file">uhci.c</span> <span class="file">ohci.c</span> <span class="file">ehci.c</span> <span class="file">xhci.c</span> <span class="file">ata.c</span> <span class="file">ahci.c</span></td>
+						<td><span class="file">usb.c</span> <span class="file">uhci.c</span> <span class="file">ohci.c</span> <span class="file">ehci.c</span> <span class="file">xhci.c</span> <span class="file">usbmsc.c</span> <span class="file">ata.c</span> <span class="file">ahci.c</span></td>
 					</tr>
 					<tr>
 						<td>Services</td>
-						<td><span class="file">sys_fs.c</span> <span class="file">sys_net.c</span> <span class="file">gfx.c</span> <span class="file">sys_wm.c</span> <span class="file">sys_info.c</span></td>
+						<td><span class="file">sys_fs.c</span> <span class="file">sys_net.c</span> <span class="file">gfx.c</span> <span class="file">sys_wm.c</span> <span class="file">sys_info.c</span> <span class="file">install.c</span></td>
 					</tr>
 					<tr>
 						<td>Userland</td>

+ 11 - 1
docs/userland.html

@@ -272,12 +272,21 @@
 				<tbody>
 					<tr><td>Shell</td><td><code>cd</code> <code>path</code> <code>help</code> <code>clear</code> <code>bootlog</code></td></tr>
 					<tr><td>Process / scheduler</td><td><code>ps</code> <code>sched</code> <code>renice</code> <code>kill</code> <code>idle</code> <code>cpufreq</code> <code>power</code></td></tr>
-					<tr><td>Hardware / debug</td><td><code>display</code> <code>gfxbench</code> <code>lsusb</code> <code>lspci</code> <code>peek</code> <code>poke</code> <code>format</code> <code>dbg</code> <code>serial</code></td></tr>
+					<tr><td>Hardware / debug</td><td><code>display</code> <code>gfxbench</code> <code>lsusb</code> <code>lspci</code> <code>peek</code> <code>poke</code> <code>format</code> <code>install</code> <code>dbg</code> <code>serial</code></td></tr>
 					<tr><td>Network control</td><td><code>netdev</code></td></tr>
 					<tr><td>Boot / power</td><td><code>reboot</code> <code>poweroff</code> <code>kexec</code></td></tr>
 				</tbody>
 			</table>
 		</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>),
+			copies the image's used sectors, writes the MBR last and verifies the
+			result. It installs the flashed image, so both the BIOS and UEFI boot
+			paths are cloned as-is.
+		</p>
 	</section>
 
 	<section id="pack">
@@ -393,6 +402,7 @@
 			<li><code>elf.c</code> — ELF parser</li>
 			<li><code>ucmd_term.c</code> — windowed shell, scrollback, PATH</li>
 			<li><code>terminal.c</code> — privileged kernel command registry</li>
+			<li><code>install.c</code> — privileged live-image clone installer</li>
 			<li><code>ucmd_lib.h</code> — user shim</li>
 			<li><code>libc/</code> — headers, malloc, string/stdlib, setjmp/errno, stdio/dtoa, math, time and the <code>SYS_MEM_*</code> backend</li>
 			<li><code>libc.a</code> — optional static ring-3 libc</li>

+ 468 - 0
install.c

@@ -0,0 +1,468 @@
+/* install: clone the live NebOS image onto a storage device.
+ *
+ * The source is the running install medium as a block device.  A USB stick
+ * needs the USB mass-storage driver (usb0); an internal image can be read
+ * from IDE/SATA/eMMC directly.  The installer copies the image's used
+ * sectors (MBR through the end of the last partition), writes the target's
+ * MBR last and verifies the copy with a checksum.  The cloned image keeps
+ * both boot paths: the MBR/legacy chain and (when present) the ESP/UEFI
+ * chain.
+ *
+ * Usage: install <target> [source]
+ */
+
+#include "blkdev.h"
+#include "fatformat.h"
+#include "io.h"
+#include "process.h"
+#include "serial.h"
+#include "vga.h"
+#include "vfs.h"
+
+#define INSTALL_NAME_MAX 16
+#define INSTALL_CHUNK    32u	/* 16 KiB copy buffer */
+#define INSTALL_DOT_LBA  2048u	/* one progress dot per 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
+#define ESP_TYPE         0xEFu
+
+static unsigned char sector0[512];
+static unsigned char copy_buf[INSTALL_CHUNK * 512];
+
+static unsigned int get16(const unsigned char *p)
+{
+	return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
+}
+
+static unsigned int get32(const unsigned char *p)
+{
+	return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
+	       ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
+}
+
+static void list_devices(void)
+{
+	unsigned int i;
+
+	io_putstr("devices:");
+	for (i = 0; i < blk_dev_count; i++) {
+		io_putchar(' ');
+		io_putstr(blk_devs[i].name);
+	}
+	io_putchar('\n');
+}
+
+static void put_size(unsigned int sectors)
+{
+	if (sectors % 2048u == 0) {
+		format_putdec(sectors / 2048u);
+		io_putstr(" MiB");
+	} else {
+		format_putdec(sectors);
+		io_putstr(" sectors");
+	}
+}
+
+/* Validate the NebOS image signature and return its used sector count.
+ * The image layout is the one the Makefile builds: an MBR whose first
+ * partition starts at LBA 63 and holds the boot sector at 63 + the kernel
+ * at 64, and a boot sector whose pack_lba points at the rootfs image.
+ * `mbr_out` (optional) receives the MBR read during validation. */
+static int check_nebos_image(blk_dev_t *d, unsigned int *used, int *has_esp,
+			     unsigned char *mbr_out)
+{
+	unsigned char mbr_local[512], boot[512];
+	unsigned char *mbr = mbr_out ? mbr_out : mbr_local;
+	unsigned int i, max_end = 0;
+	int esp = 0;
+
+	if (!d || !d->present || d->kind == BLK_FLOPPY)
+		return 0;
+	if (d->total_sectors < ROOTFS_LBA + 512u)
+		return 0;
+	if (blk_read(d, 0, mbr, 1) != 0)
+		return 0;
+	if (mbr[510] != 0x55 || mbr[511] != 0xAA)
+		return 0;
+
+	/* Partition 1: active, starts at LBA 63, holds boot + kernel. */
+	{
+		const unsigned char *e = mbr + MBR_PART_OFF;
+
+		if (e[0] != 0x80 || e[4] != 0x06 ||
+		    get32(e + 8) != BOOT_LBA || get32(e + 12) < 1024u)
+			return 0;
+	}
+
+	if (blk_read(d, BOOT_LBA, boot, 1) != 0)
+		return 0;
+	if (boot[510] != 0x55 || boot[511] != 0xAA)
+		return 0;
+	if (boot[3] != 'N' || boot[4] != 'E' || boot[5] != 'B' ||
+	    boot[6] != 'O' || boot[7] != 'S')
+		return 0;
+	if (get32(boot + PACK_LBA_OFF) != ROOTFS_LBA)
+		return 0;
+	if (get16(boot + KERNEL_LBA_OFF) != BOOT_LBA + 1u)
+		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. */
+	for (i = 0; i < 4; i++) {
+		const unsigned char *e = mbr + MBR_PART_OFF + i * MBR_PART_SIZE;
+		unsigned int start, count, end;
+
+		if (e[4] == 0xEF)
+			esp = 1;
+		if (e[4] == 0)
+			continue;
+		start = get32(e + 8);
+		count = get32(e + 12);
+		if (!count || start > d->total_sectors ||
+		    count > d->total_sectors - start)
+			return 0;
+		end = start + count;
+		if (end > max_end)
+			max_end = end;
+	}
+	if (max_end < ROOTFS_LBA + 512u)
+		return 0;
+
+	if (used)
+		*used = max_end;
+	if (has_esp)
+		*has_esp = esp;
+	return 1;
+}
+
+static blk_dev_t *find_source(blk_dev_t *target, const char *name,
+			      unsigned int *used)
+{
+	blk_dev_t *usb = 0, *other = 0;
+	unsigned int usb_used = 0, other_used = 0, u;
+	int esp;
+
+	if (name) {
+		blk_dev_t *d = blk_find(name);
+
+		if (!d || !d->present) {
+			io_putstr("install: source '");
+			io_putstr(name);
+			io_putstr("' is not present\n");
+			return 0;
+		}
+		if (d == target) {
+			io_putstr("install: source and target are the same device\n");
+			return 0;
+		}
+		if (!check_nebos_image(d, &u, &esp, 0)) {
+			io_putstr("install: '");
+			io_putstr(name);
+			io_putstr("' does not contain a NebOS image\n");
+			return 0;
+		}
+		*used = u;
+		return d;
+	}
+
+	for (u = 0; u < blk_dev_count; u++) {
+		blk_dev_t *d = &blk_devs[u];
+		unsigned int n;
+
+		if (d == target || !d->present || d->kind == BLK_FLOPPY)
+			continue;
+		if (!check_nebos_image(d, &n, &esp, 0))
+			continue;
+		if (d->kind == BLK_USB) {
+			if (!usb) {
+				usb = d;
+				usb_used = n;
+			}
+		} else if (!other) {
+			other = d;
+			other_used = n;
+		}
+	}
+	if (usb) {
+		*used = usb_used;
+		return usb;
+	}
+	if (other) {
+		*used = other_used;
+		return other;
+	}
+	return 0;
+}
+
+static unsigned int sum_bytes(unsigned int sum, const unsigned char *p,
+			      unsigned int n)
+{
+	while (n--)
+		sum += *p++;
+	return sum;
+}
+
+static void install_fail(const char *msg, unsigned int lba)
+{
+	io_putstr("install: ");
+	io_putstr(msg);
+	if (lba) {
+		io_putstr(" at lba ");
+		format_putdec(lba);
+	}
+	io_putchar('\n');
+	serial_puts(SERIAL_COM1, "install: failed at lba ");
+	serial_putdec(SERIAL_COM1, lba);
+	serial_puts(SERIAL_COM1, "\n");
+}
+
+void program_install_main(const char *arg)
+{
+	char target_name[INSTALL_NAME_MAX], source_name[INSTALL_NAME_MAX];
+	char source_dev[INSTALL_NAME_MAX];
+	blk_dev_t *target, *source;
+	unsigned int used, i, j, lba, sum, vsum;
+	int has_esp = 0, explicit_source = 0;
+	int k;
+
+	if (format_busy && process_is_alive(format_owner_pid)) {
+		io_putstr("install: another format/install is in progress\n");
+		return;
+	}
+	format_busy = 0;
+
+	if (!arg) {
+		io_putstr("install: usage install <target> [source]\n");
+		list_devices();
+		return;
+	}
+
+	/* Target = first word, optional source = second word. */
+	i = 0;
+	while (arg[i] == ' ')
+		i++;
+	j = 0;
+	while (arg[i] && arg[i] != ' ' && j < sizeof(target_name) - 1)
+		target_name[j++] = arg[i++];
+	target_name[j] = '\0';
+	while (arg[i] == ' ')
+		i++;
+	j = 0;
+	while (arg[i] && arg[i] != ' ' && j < sizeof(source_name) - 1)
+		source_name[j++] = arg[i++];
+	source_name[j] = '\0';
+	explicit_source = (source_name[0] != '\0');
+	while (arg[i] == ' ')
+		i++;
+	if (!target_name[0] || arg[i]) {
+		io_putstr("install: usage install <target> [source]\n");
+		return;
+	}
+
+	target = blk_find(target_name);
+	if (!target) {
+		io_putstr("install: unknown target '");
+		io_putstr(target_name);
+		io_putstr("'\n");
+		list_devices();
+		return;
+	}
+	if (target->kind == BLK_FLOPPY) {
+		io_putstr("install: a floppy is too small for the image\n");
+		return;
+	}
+	if (!target->present || target->total_sectors == 0) {
+		io_putstr("install: target ");
+		io_putstr(target->name);
+		io_putstr(" is not present\n");
+		return;
+	}
+
+	source = find_source(target, explicit_source ? source_name : 0, &used);
+	if (!source) {
+		if (!explicit_source) {
+			io_putstr("install: no NebOS install source found\n");
+			io_putstr("install: the USB stick must be visible as a block device\n");
+			list_devices();
+		}
+		return;
+	}
+	/* Recompute the flags for the chosen source (find_source only
+	 * returned the used length). */
+	check_nebos_image(source, &used, &has_esp, 0);
+	if (used > target->total_sectors) {
+		io_putstr("install: source image (");
+		put_size(used);
+		io_putstr(") does not fit on ");
+		io_putstr(target->name);
+		io_putstr(" (");
+		put_size(target->total_sectors);
+		io_putstr(")\n");
+		return;
+	}
+
+	io_putstr("install: source ");
+	io_putstr(source->name);
+	if (source->model[0]) {
+		io_putstr(" (");
+		io_putstr(source->model);
+		io_putchar(')');
+	}
+	io_putstr(", ");
+	put_size(used);
+	io_putstr(", ");
+	io_putstr(has_esp ? "BIOS + UEFI" : "BIOS only");
+	io_putchar('\n');
+	io_putstr("install: target ");
+	io_putstr(target->name);
+	if (target->model[0]) {
+		io_putstr(" (");
+		io_putstr(target->model);
+		io_putchar(')');
+	}
+	io_putstr(", ");
+	put_size(target->total_sectors);
+	io_putchar('\n');
+	for (i = 0; i + 1 < sizeof(source_dev) && source->name[i]; i++)
+		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();
+
+	/* The copy runs with the BKL held: a USB source transfer must not be
+	 * interrupted by a bus rescan.  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')
+			break;
+		if (k == 'n' || k == 'N') {
+			io_set_raw_keyboard(0);
+			io_putstr("aborted\n");
+			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
+	 * private copy of the source MBR. */
+	if (format_busy && process_is_alive(format_owner_pid)) {
+		io_putstr("install: another format/install is in progress\n");
+		return;
+	}
+	format_owner_pid = current_proc()->pid;
+	format_busy = 1;
+
+	target = blk_find(target_name);
+	source = blk_find(source_dev);
+	if (!target || !source || source == target ||
+	    !target->present || !source->present) {
+		io_putstr("install: device set changed; aborted\n");
+		goto fail;
+	}
+	if (used > target->total_sectors) {
+		io_putstr("install: source no longer fits; aborted\n");
+		goto fail;
+	}
+	if (!check_nebos_image(source, &used, &has_esp, sector0)) {
+		io_putstr("install: source changed; aborted\n");
+		goto fail;
+	}
+	if (used > target->total_sectors) {
+		io_putstr("install: source no longer fits; aborted\n");
+		goto fail;
+	}
+
+	serial_puts(SERIAL_COM1, "install: ");
+	serial_puts(SERIAL_COM1, source->name);
+	serial_puts(SERIAL_COM1, " -> ");
+	serial_puts(SERIAL_COM1, target->name);
+	serial_puts(SERIAL_COM1, ", ");
+	serial_putdec(SERIAL_COM1, used);
+	serial_puts(SERIAL_COM1, " sectors\n");
+
+	io_putstr("install: copying");
+	vga_fb_flush();
+
+	sum = sum_bytes(0, sector0, sizeof(sector0));
+	for (lba = 1; lba < used; lba += INSTALL_CHUNK) {
+		unsigned int n = used - lba;
+
+		if (n > INSTALL_CHUNK)
+			n = INSTALL_CHUNK;
+		if (blk_read(source, lba, copy_buf, n) != 0) {
+			install_fail("source read failed", lba);
+			goto fail;
+		}
+		sum = sum_bytes(sum, copy_buf, n * 512u);
+		if (blk_write(target, lba, copy_buf, n) != 0) {
+			install_fail("target write failed", lba);
+			goto fail;
+		}
+		if (lba >= INSTALL_DOT_LBA && lba % INSTALL_DOT_LBA < INSTALL_CHUNK)
+			io_putchar('.');
+	}
+
+	/* MBR last: an interrupted copy is not bootable. */
+	if (blk_write(target, 0, sector0, 1) != 0) {
+		install_fail("target MBR write failed", 0);
+		goto fail;
+	}
+	io_putchar('\n');
+
+	{
+		int fr = blk_flush(target);
+
+		if (fr != 0)
+			io_putstr("install: cache flush failed; verifying anyway\n");
+	}
+
+	io_putstr("install: verifying...\n");
+	vsum = 0;
+	for (lba = 0; lba < used; lba += INSTALL_CHUNK) {
+		unsigned int n = used - lba;
+
+		if (n > INSTALL_CHUNK)
+			n = INSTALL_CHUNK;
+		if (blk_read(target, lba, copy_buf, n) != 0) {
+			install_fail("target read-back failed", lba);
+			goto fail;
+		}
+		vsum = sum_bytes(vsum, copy_buf, n * 512u);
+	}
+	if (vsum != sum) {
+		install_fail("verification mismatch", 0);
+		goto fail;
+	}
+	io_putstr("install: verified ");
+	format_putdec(used);
+	io_putstr(" sectors\n");
+
+	vfs_remount_device(target);
+	format_busy = 0;
+	io_putstr("install: done; remove the install medium and reboot.\n");
+	serial_puts(SERIAL_COM1, "install: done\n");
+	return;
+
+fail:
+	vfs_remount_device(target);
+	format_busy = 0;
+	io_putstr("install: target is incomplete; do not boot it\n");
+	serial_puts(SERIAL_COM1, "install: failed\n");
+}

+ 2 - 0
kernel.c

@@ -27,6 +27,7 @@
 #include "tpec.h"
 #include "tss.h"
 #include "usbhid.h"
+#include "usbmsc.h"
 #include "wm.h"
 #include "serial.h"
 #include "smp.h"
@@ -449,6 +450,7 @@ void kernel_main(void)
 	 * scan; devices plugged in later are picked up by the hotplug
 	 * watcher in the WM loop. */
 	asix_register();
+	usbmsc_register();
 	usbhid_init();
 	boot_trace("usb");
 	vga_shadow_init();

+ 1 - 1
syscall.h

@@ -412,7 +412,7 @@ struct sys_storage_info {
 struct sys_blkdev {
 	char name[8];
 	char model[41];
-	unsigned int kind;       /* 0 = floppy, 1 = IDE, 2 = SATA, 3 = MMC */
+	unsigned int kind;       /* 0 = floppy, 1 = IDE, 2 = SATA, 3 = MMC, 4 = USB */
 	unsigned int present;
 	unsigned int total_sectors;
 };

+ 10 - 0
terminal.c

@@ -23,6 +23,7 @@ extern void program_lsusb_main(void);
 extern void program_touchpad_main(void);
 extern void program_netdev_main(const char *arg);
 extern void program_format_main(const char *arg);
+extern void program_install_main(const char *arg);
 extern void program_lspci_main(const char *filter);
 extern void program_peek_main(const char *arg);
 extern void program_poke_main(const char *arg);
@@ -60,6 +61,7 @@ struct kcmd {
 static int cmd_bootlog(struct term *t, const char *args);
 static int cmd_dbg(struct term *t, const char *args);
 static int cmd_format(struct term *t, const char *args);
+static int cmd_install(struct term *t, const char *args);
 static int cmd_help(struct term *t, const char *args);
 static int cmd_idle(struct term *t, const char *args);
 static int cmd_cpufreq(struct term *t, const char *args);
@@ -86,6 +88,7 @@ static const struct kcmd cmds[] = {
 	{ "touchpad",   "touchpad",                      CK_SPAWN, 0, program_touchpad_main, 0, 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 },
 	{ "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 },
@@ -207,6 +210,13 @@ static int cmd_format(struct term *t, const char *args)
 	return 0;
 }
 
+static int cmd_install(struct term *t, const char *args)
+{
+	(void)t;
+	program_install_main(args);
+	return 0;
+}
+
 /* `path` builtin: show or set the command search path. */
 
 

+ 255 - 0
tools/qmp_install_test.py

@@ -0,0 +1,255 @@
+#!/usr/bin/env python3
+"""End-to-end installer test (QEMU + QMP + USB mass storage).
+
+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).
+
+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).
+Exit code 0 = pass.
+"""
+import json
+import os
+import shutil
+import socket
+import subprocess
+import sys
+import tempfile
+import time
+
+ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+OVMF_CANDIDATES = [
+    os.environ.get("OVMF", ""),
+    "/usr/share/ovmf/x64/OVMF.4m.fd",
+    "/usr/share/OVMF/OVMF.4m.fd",
+]
+
+
+def qmp_recv(s):
+    buf = b""
+    while True:
+        chunk = s.recv(4096)
+        if not chunk:
+            raise RuntimeError("QMP connection closed")
+        buf += chunk
+        while b"\n" in buf:
+            line, buf = buf.split(b"\n", 1)
+            line = line.strip()
+            if line:
+                return json.loads(line)
+
+
+def qmp_cmd(s, execute, arguments=None):
+    req = {"execute": execute}
+    if arguments is not None:
+        req["arguments"] = arguments
+    s.sendall((json.dumps(req) + "\n").encode())
+    while True:
+        msg = qmp_recv(s)
+        if "return" in msg or "error" in msg:
+            if "error" in msg:
+                print("QMP error:", msg)
+            return msg
+
+
+def send_key(s, key):
+    qmp_cmd(s, "send-key", {"keys": [{"type": "qcode", "data": key}]})
+    time.sleep(0.15)
+
+
+def send_text(s, text):
+    for ch in text:
+        if ch == " ":
+            send_key(s, "spc")
+        elif ch == "\n":
+            send_key(s, "ret")
+        else:
+            send_key(s, ch)
+
+
+def rd32(p):
+    return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24)
+
+
+def image_used(data):
+    """Used image length from the MBR's highest partition end."""
+    if len(data) < 512 or data[510:512] != b"\x55\xaa":
+        return 0
+    used = 0
+    for i in range(4):
+        e = 0x1BE + i * 16
+        if data[e + 4] == 0:
+            continue
+        start = rd32(data[e + 8:e + 12])
+        count = rd32(data[e + 12:e + 16])
+        if not count or start + count > len(data) // 512:
+            return 0
+        used = max(used, start + count)
+    return used
+
+
+def image_has_esp(data):
+    for i in range(4):
+        if data[0x1BE + i * 16 + 4] == 0xEF:
+            return True
+    return False
+
+
+def serial_has(path, needle):
+    try:
+        with open(path, "rb") as f:
+            return needle.encode() in f.read()
+    except FileNotFoundError:
+        return False
+
+
+def wait_serial(path, needles, timeout):
+    deadline = time.time() + timeout
+    while time.time() < deadline:
+        try:
+            with open(path, "rb") as f:
+                data = f.read()
+        except FileNotFoundError:
+            data = b""
+        for n in needles:
+            if n.encode() in data:
+                return n
+        time.sleep(0.5)
+    return None
+
+
+def run_boot(image, out_log, uefi=False, timeout=45):
+    if uefi:
+        ovmf = next((p for p in OVMF_CANDIDATES if p and os.path.exists(p)), None)
+        if not ovmf:
+            return None
+        qemu = ["qemu-system-x86_64", "-M", "pc", "-m", "512M", "-bios", ovmf]
+    else:
+        qemu = ["qemu-system-i386", "-M", "pc", "-m", "512M"]
+    qemu += ["-boot", "c", "-drive", "file=%s,format=raw,if=ide" % image,
+             "-serial", "file:%s" % out_log, "-display", "none",
+             "-no-reboot"]
+    try:
+        subprocess.run(qemu, timeout=timeout, stdout=subprocess.DEVNULL,
+                       stderr=subprocess.DEVNULL)
+    except subprocess.TimeoutExpired:
+        pass
+    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)
+
+    if xhci:
+        # p3=0 keeps the whole xHCI bus at USB2 speeds; the kernel's xHCI
+        # driver deliberately does not drive USB3 SuperSpeed ports.
+        usb = ["-device", "qemu-xhci,p2=4,p3=0",
+               "-device", "usb-storage,drive=src",
+               "-device", "usb-mouse", "-device", "usb-kbd"]
+    else:
+        usb = ["-device", "usb-ehci,id=ehci0",
+               "-device", "piix3-usb-uhci,id=uhci0",
+               "-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 + [
+        "-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"]
+
+    proc = subprocess.Popen(qemu, stdout=subprocess.DEVNULL,
+                            stderr=subprocess.DEVNULL)
+    ok = False
+    try:
+        for _ in range(100):
+            if os.path.exists(sock):
+                break
+            time.sleep(0.1)
+        s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+        s.connect(sock)
+        qmp_recv(s)                  # greeting
+        qmp_cmd(s, "qmp_capabilities")
+        time.sleep(10.0)             # boot the live system
+
+        send_text(s, "install ide1\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")
+        try:
+            proc.wait(timeout=10)
+        except subprocess.TimeoutExpired:
+            pass
+
+        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")
+
+        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" %
+              (used, "ok" if clone else "MISMATCH"))
+
+        bios = run_boot(target, bios_log, uefi=False)
+        print("target BIOS boot:", "ok" if bios else "FAILED")
+        uefi = None
+        if image_has_esp(src_data):
+            uefi = run_boot(target, uefi_log, uefi=True)
+            if uefi is None:
+                print("target UEFI boot: skipped (no OVMF)")
+            else:
+                print("target UEFI boot:", "ok" if uefi else "FAILED")
+        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))
+    finally:
+        if proc.poll() is None:
+            proc.kill()
+            proc.wait()
+        if keep:
+            print("kept:", work)
+        else:
+            shutil.rmtree(work, ignore_errors=True)
+    return 0 if ok else 1
+
+
+if __name__ == "__main__":
+    sys.exit(main())

+ 20 - 4
usb.c

@@ -105,7 +105,9 @@ static unsigned int generation;
 static unsigned int next_addr[USB_MAX_CTRL];
 static const struct usb_hcd_ops *slot_ops[USB_HCD_INT_SLOTS];
 static usb_probe_fn current_probe;
-static usb_device_probe_fn device_probe;
+#define USB_MAX_DEVICE_PROBES 4
+static usb_device_probe_fn device_probes[USB_MAX_DEVICE_PROBES];
+static unsigned int n_device_probes;
 static int print_devices;
 static int scan_busy;
 
@@ -398,8 +400,14 @@ static void enumerate_device(unsigned int ctrl, unsigned int addr,
 	subcls  = dev_desc[5];
 	proto   = dev_desc[6];
 
-	if (device_probe)
-		device_probe(ctrl, addr, mps, low, port, vendor, product);
+	{
+		unsigned int i;
+
+		for (i = 0; i < n_device_probes; i++)
+			if (device_probes[i](ctrl, addr, mps, low, port,
+					     vendor, product))
+				break;
+	}
 
 	if (print_devices) {
 		indent(depth);
@@ -703,7 +711,15 @@ void usb_int_in_stop(unsigned int slot)
 
 void usb_register_device_probe(usb_device_probe_fn fn)
 {
-	device_probe = fn;
+	unsigned int i;
+
+	if (!fn)
+		return;
+	for (i = 0; i < n_device_probes; i++)
+		if (device_probes[i] == fn)
+			return;
+	if (n_device_probes < USB_MAX_DEVICE_PROBES)
+		device_probes[n_device_probes++] = fn;
 }
 
 int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,

+ 4 - 3
usb.h

@@ -56,9 +56,10 @@ int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		unsigned int mps, unsigned char *buf, unsigned int len);
 
 /* Device-level probe for drivers that do not use HID interfaces (the
- * USB Ethernet driver parses the whole configuration itself).  Return 1
- * when the device was claimed.  Called for every non-hub device during a
- * scan, before the per-interface HID probe. */
+ * USB Ethernet and mass-storage drivers parse the whole configuration
+ * themselves).  Return 1 when the device was claimed.  Several probes may
+ * register; each is called (in registration order) for every non-hub
+ * device during a scan, before the per-interface HID probe. */
 typedef int (*usb_device_probe_fn)(unsigned int ctrl, unsigned int addr,
 				   unsigned int mps, int low_speed,
 				   unsigned int port, unsigned int vendor,

+ 520 - 0
usbmsc.c

@@ -0,0 +1,520 @@
+/* USB mass-storage class driver: Bulk-Only Transport (BOT) + SCSI.
+ *
+ * The USB core's device probe sees every enumerated device; this driver
+ * parses the configuration for an interface with class 8 / subclass 6 /
+ * protocol 0x50 (SCSI transparent, bulk-only), claims its bulk IN/OUT
+ * endpoints and exposes the disk as `usb0..` once usbmsc_blk_scan() runs.
+ *
+ * Only EHCI and xHCI implement the bulk-transfer ops; UHCI/OHCI devices are
+ * left unclaimed (a full-speed stick on a companion controller cannot be
+ * used).  The block layer calls usbmsc_read/write/flush from blkdev.c.
+ */
+
+#include "usbmsc.h"
+#include "blkdev.h"
+#include "serial.h"
+#include "timer.h"
+#include "usb.h"
+
+#define USB_MSC_MAX		4
+/* Keep one SCSI data stage below EHCI's 0x7FFF-byte bulk-OUT limit:
+ * 62 sectors = 31,744 bytes. */
+#define MSC_CHUNK_SECTORS	62u
+#define MSC_BULK_CHUNK		2048u	/* EHCI's fixed bulk-IN buffer */
+#define MSC_TIMEOUT_MS		3000u
+#define MSC_CBW_SIG		0x43425355u	/* "USBC" LE */
+#define MSC_CSW_SIG		0x53425355u	/* "USBS" LE */
+
+struct usb_msc {
+	int present;
+	unsigned int ctrl, addr, mps, port;
+	int low;
+	unsigned int ep_in, ep_in_mps;	/* endpoint numbers (1..15) */
+	unsigned int ep_out, ep_out_mps;
+	unsigned int iface;
+	unsigned int sectors;
+	unsigned int tag;
+	char model[41];
+};
+
+static struct usb_msc msc[USB_MSC_MAX];
+static char msc_names[USB_MSC_MAX][6];
+static unsigned int msc_generation;
+
+static unsigned int rd32le(const unsigned char *p)
+{
+	return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
+	       ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
+}
+
+static unsigned int rd32be(const unsigned char *p)
+{
+	return ((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) |
+	       ((unsigned int)p[2] << 8) | (unsigned int)p[3];
+}
+
+static void put32le(unsigned char *p, unsigned int v)
+{
+	p[0] = (unsigned char)(v & 0xFF);
+	p[1] = (unsigned char)((v >> 8) & 0xFF);
+	p[2] = (unsigned char)((v >> 16) & 0xFF);
+	p[3] = (unsigned char)((v >> 24) & 0xFF);
+}
+
+static void put16be(unsigned char *p, unsigned int v)
+{
+	p[0] = (unsigned char)((v >> 8) & 0xFF);
+	p[1] = (unsigned char)(v & 0xFF);
+}
+
+static void put32be(unsigned char *p, unsigned int v)
+{
+	p[0] = (unsigned char)((v >> 24) & 0xFF);
+	p[1] = (unsigned char)((v >> 16) & 0xFF);
+	p[2] = (unsigned char)((v >> 8) & 0xFF);
+	p[3] = (unsigned char)(v & 0xFF);
+}
+
+static void msc_delay(unsigned int ms)
+{
+	unsigned int t0 = get_ticks_ms();
+
+	while (get_ticks_ms() - t0 < ms)
+		;
+}
+
+/* Bulk IN with a deadline.  Transfers larger than the host controller's
+ * internal bulk-IN buffer are split; EHCI's is 2048 bytes. */
+static int msc_bulk_in(struct usb_msc *m, unsigned char *buf, unsigned int len)
+{
+	unsigned int got = 0, t0 = get_ticks_ms();
+
+	while (got < len) {
+		unsigned int want = len - got;
+		int n;
+
+		if (want > MSC_BULK_CHUNK)
+			want = MSC_BULK_CHUNK;
+		n = usb_bulk_in(m->ctrl, m->addr, m->ep_in, m->ep_in_mps,
+				buf + got, want);
+		if (n < 0)
+			return -1;
+		if (n > 0) {
+			got += (unsigned int)n;
+			t0 = get_ticks_ms();
+			/* A short packet before the requested end means the
+			 * device ended the data stage early. */
+			if (got < len && (unsigned int)n < want)
+				return -1;
+		} else if (get_ticks_ms() - t0 > MSC_TIMEOUT_MS) {
+			return -1;
+		}
+	}
+	return 0;
+}
+
+static int msc_bulk_out(struct usb_msc *m, const unsigned char *buf,
+			unsigned int len)
+{
+	return usb_bulk_out(m->ctrl, m->addr, m->ep_out, m->ep_out_mps,
+			    buf, len);
+}
+
+/* BOT reset + clear both bulk endpoint halts. */
+static void msc_reset(struct usb_msc *m)
+{
+	usb_control_out(m->ctrl, m->addr, m->mps, m->low,
+			0x21, 0xFF, 0, (unsigned short)m->iface);
+	usb_control_out(m->ctrl, m->addr, m->mps, m->low,
+			0x02, 0x01, 0, (unsigned short)(0x80u | m->ep_in));
+	usb_control_out(m->ctrl, m->addr, m->mps, m->low,
+			0x02, 0x01, 0, (unsigned short)m->ep_out);
+	m->tag++;
+}
+
+static int bot_cmd(struct usb_msc *m, const unsigned char *cb,
+		   unsigned int cb_len, void *data, unsigned int data_len,
+		   int dir_in)
+{
+	unsigned char cbw[31];
+	unsigned char csw[13];
+	unsigned int tag = ++m->tag;
+
+	for (unsigned int i = 0; i < sizeof(cbw); i++)
+		cbw[i] = 0;
+	put32le(cbw, MSC_CBW_SIG);
+	put32le(cbw + 4, tag);
+	put32le(cbw + 8, data_len);
+	cbw[12] = (unsigned char)(dir_in ? 0x80 : 0x00);
+	cbw[13] = 0;			/* LUN 0 */
+	cbw[14] = (unsigned char)cb_len;
+	for (unsigned int i = 0; i < cb_len && i < 16; i++)
+		cbw[15 + i] = cb[i];
+
+	if (msc_bulk_out(m, cbw, sizeof(cbw)) != 0)
+		return -1;
+	if (data_len) {
+		if (dir_in) {
+			if (msc_bulk_in(m, (unsigned char *)data, data_len) != 0)
+				return -1;
+		} else if (msc_bulk_out(m, (const unsigned char *)data,
+					data_len) != 0) {
+			return -1;
+		}
+	}
+	if (msc_bulk_in(m, csw, sizeof(csw)) != 0)
+		return -1;
+	if (rd32le(csw) != MSC_CSW_SIG || rd32le(csw + 4) != tag)
+		return -1;
+	if (csw[12] != 0)
+		return -1;
+	if (rd32le(csw + 8) != 0)	/* residue */
+		return -1;
+	return 0;
+}
+
+static int msc_test_unit_ready(struct usb_msc *m)
+{
+	unsigned char cb[6] = { 0x00, 0, 0, 0, 0, 0 };
+
+	return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
+}
+
+static int msc_inquiry(struct usb_msc *m, unsigned char data[36])
+{
+	unsigned char cb[6] = { 0x12, 0, 0, 0, 36, 0 };
+
+	return bot_cmd(m, cb, sizeof(cb), data, 36, 1);
+}
+
+static int msc_read_capacity(struct usb_msc *m, unsigned int *sectors)
+{
+	unsigned char cb[10] = { 0x25, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+	unsigned char data[8];
+	unsigned int last, block;
+
+	if (bot_cmd(m, cb, sizeof(cb), data, sizeof(data), 1) != 0)
+		return -1;
+	last = rd32be(data);
+	block = rd32be(data + 4);
+	if (block != 512 || last == 0xFFFFFFFFu)
+		return -1;
+	*sectors = last + 1;
+	return 0;
+}
+
+static int msc_read10(struct usb_msc *m, unsigned int lba, unsigned int count,
+		      void *buf)
+{
+	unsigned char cb[10] = { 0x28, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+
+	put32be(cb + 2, lba);
+	put16be(cb + 7, count);
+	return bot_cmd(m, cb, sizeof(cb), buf, count * 512u, 1);
+}
+
+static int msc_write10(struct usb_msc *m, unsigned int lba, unsigned int count,
+		       const void *buf)
+{
+	unsigned char cb[10] = { 0x2A, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+
+	put32be(cb + 2, lba);
+	put16be(cb + 7, count);
+	return bot_cmd(m, cb, sizeof(cb), (void *)buf, count * 512u, 0);
+}
+
+static int msc_sync_cache(struct usb_msc *m)
+{
+	unsigned char cb[10] = { 0x35, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+
+	return bot_cmd(m, cb, sizeof(cb), 0, 0, 0);
+}
+
+/* Find and configure a bulk-only mass-storage interface. */
+static int msc_config(struct usb_msc *m)
+{
+	unsigned char hdr[9], cfg[256];
+	unsigned int total, i, config = 0;
+	unsigned int ep_in = 0, ep_out = 0, ep_in_mps = 0, ep_out_mps = 0;
+	unsigned int iface = 0;
+	int found = 0;
+
+	if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
+			   0x80, 0x06, 0x0200, 0, hdr, sizeof(hdr)) < 0)
+		return -1;
+	total = (unsigned int)hdr[2] | ((unsigned int)hdr[3] << 8);
+	if (total < 9 || total > sizeof(cfg))
+		return -1;
+	if (usb_control_in(m->ctrl, m->addr, m->mps, m->low,
+			   0x80, 0x06, 0x0200, 0, cfg, total) != (int)total)
+		return -1;
+	config = cfg[5];
+	if (!config)
+		return -1;
+
+	i = 0;
+	while (i + 2 <= total) {
+		unsigned int blen = cfg[i];
+		unsigned int btype = cfg[i + 1];
+
+		if (blen < 2 || i + blen > total)
+			break;
+		if (btype == 0x04 && blen >= 9) {	/* interface */
+			if (cfg[i + 5] == 8 && cfg[i + 6] == 6 &&
+			    cfg[i + 7] == 0x50) {
+				found = 1;
+				iface = cfg[i + 2];
+				ep_in = ep_out = 0;
+				ep_in_mps = ep_out_mps = 0;
+			} else {
+				found = 0;
+			}
+		} else if (btype == 0x05 && blen >= 7 && found) {
+			unsigned int epa = cfg[i + 2];
+			unsigned int attr = cfg[i + 3] & 3u;
+			unsigned int mps = ((unsigned int)cfg[i + 4] |
+					    ((unsigned int)cfg[i + 5] << 8)) &
+					   0x7FFu;
+
+			if (attr == 2) {
+				if (epa & 0x80) {
+					ep_in = epa & 0x0F;
+					ep_in_mps = mps;
+				} else {
+					ep_out = epa & 0x0F;
+					ep_out_mps = mps;
+				}
+			}
+		}
+		i += blen;
+	}
+	if (!found || !ep_in || !ep_out)
+		return -1;
+	if (usb_control_out(m->ctrl, m->addr, m->mps, m->low,
+			    0x00, 0x09, (unsigned short)config, 0) < 0)
+		return -1;
+
+	m->iface = iface;
+	m->ep_in = ep_in;
+	m->ep_in_mps = ep_in_mps ? ep_in_mps : 64;
+	m->ep_out = ep_out;
+	m->ep_out_mps = ep_out_mps ? ep_out_mps : 64;
+	return 0;
+}
+
+static void msc_set_model(struct usb_msc *m, const unsigned char data[36])
+{
+	unsigned int i, n = 0;
+
+	for (i = 0; i < 8; i++)
+		m->model[n++] = (char)data[8 + i];
+	m->model[n++] = ' ';
+	for (i = 0; i < 16; i++)
+		m->model[n++] = (char)data[16 + i];
+	while (n && (m->model[n - 1] == ' ' || m->model[n - 1] == '\0'))
+		n--;
+	m->model[n] = '\0';
+}
+
+static int device_probe(unsigned int ctrl, unsigned int addr,
+			unsigned int mps, int low_speed, unsigned int port,
+			unsigned int vendor, unsigned int product)
+{
+	struct usb_msc *m = 0;
+	unsigned char inquiry[36];
+	unsigned int i, sectors;
+	int slot = -1;
+
+	(void)vendor;
+	(void)product;
+
+	/* A new bus scan re-enumerates every device with fresh addresses;
+	 * forget the old set on its first probe. */
+	{
+		unsigned int gen = usb_generation();
+
+		if (gen != msc_generation) {
+			for (i = 0; i < USB_MSC_MAX; i++)
+				msc[i].present = 0;
+			msc_generation = gen;
+		}
+	}
+
+	for (i = 0; i < USB_MSC_MAX; i++)
+		if (msc[i].present && msc[i].ctrl == ctrl &&
+		    msc[i].addr == addr) {
+			slot = (int)i;
+			break;
+		}
+	if (slot < 0)
+		for (i = 0; i < USB_MSC_MAX; i++)
+			if (!msc[i].present) {
+				slot = (int)i;
+				break;
+			}
+	if (slot < 0)
+		return 0;
+
+	m = &msc[(unsigned int)slot];
+	m->present = 0;
+	m->ctrl = ctrl;
+	m->addr = addr;
+	m->mps = mps;
+	m->low = low_speed;
+	m->port = port;
+	m->tag = 0;
+	m->iface = 0;
+
+	if (msc_config(m) != 0)
+		return 0;
+
+	for (i = 0; i < 4; i++) {
+		if (msc_test_unit_ready(m) == 0)
+			break;
+		msc_delay(50);
+		msc_reset(m);
+	}
+	if (i == 4) {
+		serial_puts(SERIAL_COM1, "usbmsc: no bulk transfer path / not ready\n");
+		return 0;
+	}
+
+	if (msc_inquiry(m, inquiry) != 0) {
+		serial_puts(SERIAL_COM1, "usbmsc: inquiry failed\n");
+		msc_reset(m);
+		if (msc_inquiry(m, inquiry) != 0)
+			return 0;
+	}
+	if (msc_read_capacity(m, &sectors) != 0) {
+		serial_puts(SERIAL_COM1, "usbmsc: read capacity failed\n");
+		msc_reset(m);
+		if (msc_read_capacity(m, &sectors) != 0)
+			return 0;
+	}
+	if (sectors == 0)
+		return 0;
+
+	msc_set_model(m, inquiry);
+	m->sectors = sectors;
+	m->present = 1;
+
+	serial_puts(SERIAL_COM1, "usbmsc: ");
+	serial_puts(SERIAL_COM1, m->model);
+	serial_puts(SERIAL_COM1, " ");
+	serial_putdec(SERIAL_COM1, sectors);
+	serial_puts(SERIAL_COM1, " sectors on ctrl ");
+	serial_putdec(SERIAL_COM1, ctrl);
+	serial_puts(SERIAL_COM1, " addr ");
+	serial_putdec(SERIAL_COM1, addr);
+	serial_puts(SERIAL_COM1, "\n");
+	return 1;
+}
+
+void usbmsc_register(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < USB_MSC_MAX; i++) {
+		msc_names[i][0] = 'u';
+		msc_names[i][1] = 's';
+		msc_names[i][2] = 'b';
+		msc_names[i][3] = (char)('0' + i);
+		msc_names[i][4] = '\0';
+	}
+	usb_register_device_probe(device_probe);
+}
+
+void usbmsc_blk_scan(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < USB_MSC_MAX; i++) {
+		blk_dev_t *d;
+		unsigned int j;
+
+		if (!msc[i].present || blk_dev_count >= BLKDEV_MAX)
+			continue;
+		d = &blk_devs[blk_dev_count++];
+		d->name = msc_names[i];
+		d->kind = BLK_USB;
+		d->bus = 0;
+		d->unit = i;
+		d->present = 1;
+		d->total_sectors = msc[i].sectors;
+		for (j = 0; j < sizeof(d->model) - 1 && msc[i].model[j]; j++)
+			d->model[j] = msc[i].model[j];
+		d->model[j] = '\0';
+	}
+}
+
+static int msc_get(unsigned int unit, struct usb_msc **out)
+{
+	if (unit >= USB_MSC_MAX || !msc[unit].present)
+		return -1;
+	*out = &msc[unit];
+	return 0;
+}
+
+int usbmsc_read(unsigned int unit, unsigned int lba, void *buf,
+		unsigned int count)
+{
+	struct usb_msc *m;
+	unsigned char *p = (unsigned char *)buf;
+
+	if (msc_get(unit, &m) != 0)
+		return -1;
+	if (lba > m->sectors || count > m->sectors - lba)
+		return -1;
+	while (count) {
+		unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
+
+		if (msc_read10(m, lba, n, p) != 0) {
+			msc_reset(m);
+			if (msc_read10(m, lba, n, p) != 0)
+				return -1;
+		}
+		lba += n;
+		p += n * 512u;
+		count -= n;
+	}
+	return 0;
+}
+
+int usbmsc_write(unsigned int unit, unsigned int lba, const void *buf,
+		 unsigned int count)
+{
+	struct usb_msc *m;
+	const unsigned char *p = (const unsigned char *)buf;
+
+	if (msc_get(unit, &m) != 0)
+		return -1;
+	if (lba > m->sectors || count > m->sectors - lba)
+		return -1;
+	while (count) {
+		unsigned int n = count > MSC_CHUNK_SECTORS ? MSC_CHUNK_SECTORS : count;
+
+		if (msc_write10(m, lba, n, p) != 0) {
+			msc_reset(m);
+			if (msc_write10(m, lba, n, p) != 0)
+				return -1;
+		}
+		lba += n;
+		p += n * 512u;
+		count -= n;
+	}
+	return 0;
+}
+
+int usbmsc_flush(unsigned int unit)
+{
+	struct usb_msc *m;
+
+	if (msc_get(unit, &m) != 0)
+		return -1;
+	if (msc_sync_cache(m) != 0) {
+		msc_reset(m);
+		if (msc_sync_cache(m) != 0)
+			return -1;
+	}
+	return 0;
+}

+ 25 - 0
usbmsc.h

@@ -0,0 +1,25 @@
+/* USB mass-storage class driver (Bulk-Only Transport + SCSI).
+ *
+ * Devices are registered as `usb0..` in the block-device registry.  Only the
+ * controllers with a bulk-transfer path work: EHCI (high-speed) and xHCI
+ * (USB2 root ports).  UHCI/OHCI leave bulk_in/bulk_out NULL, so a stick on a
+ * full/low-speed companion controller is not usable.
+ */
+#ifndef USB_MSC_H
+#define USB_MSC_H
+
+/* Register the device probe; call before the first USB bus scan. */
+void usbmsc_register(void);
+
+/* Add the currently present MSC devices to the blkdev registry.  Called at
+ * the end of blk_scan(); the USB bus scan itself runs earlier at boot. */
+void usbmsc_blk_scan(void);
+
+/* Block I/O for BLK_USB devices (unit = usbmsc slot index). */
+int usbmsc_read(unsigned int unit, unsigned int lba, void *buf,
+		unsigned int count);
+int usbmsc_write(unsigned int unit, unsigned int lba, const void *buf,
+		 unsigned int count);
+int usbmsc_flush(unsigned int unit);
+
+#endif

+ 5 - 9
xhci.c

@@ -1126,15 +1126,11 @@ static int xhci_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		err = (unsigned int)b->error;
 		b->pending = 0;
 		b->error = 0;
-		if (!err && b->active) {
-			b->trb_len = len;
-			ring_queue(b->ring, XHCI_BULK_TRBS, &b->enq,
-				   &b->cycle, (unsigned int)buf, 0, len,
-				   TRB_TYPE(TRB_NORMAL) | TRB_IOC);
-			b->armed = 1;
-			__asm__ volatile ("" ::: "memory");
-			mmio_wr(db_off + 4 * b->slot, b->ep_id);
-		}
+		/* Do not re-arm here: the next call may use a different
+		 * buffer/length (the mass-storage driver alternates CBW,
+		 * data and CSW buffers).  The caller's next usb_bulk_in()
+		 * arms the new transfer with its own buffer. */
+		b->armed = 0;
 		spin_unlock_irqrestore(&xhci_lock, flags);
 		return err ? -1 : (int)n;
 	}