Просмотр исходного кода

Speed up boot to the terminal and keep storage/lsusb from stalling

The terminal is on screen ~1.5 s after QEMU starts instead of ~5.3 s
(the kernel now starts at ~0.8 s instead of ~2.2 s).  Tested on target.

- boot.S: EDD reads up to 64 sectors per INT 13h into ES:0 (was one
  sector per call, ~700 calls for the kernel); a refused multi-sector
  read falls back to one sector per call.  CHS stays single-sector.
- vesa_stage.S: one INT 13h per 16 KB rootfs chunk (32 sectors over EDD,
  up to the end of the track over CHS) instead of ~3600 single-sector
  calls; same single-sector fallback.
- inflate.c: the whole-buffer helpers (ramfs pack, image decoders) use
  10-bit Huffman lookup tables, decode into the output buffer and
  checksum once: the 3.7 MB pack inflates in ~0.25 s instead of ~1.1 s.
  The streaming decoder is unchanged.
- e1000e.c: skip the PCIe GIO-master handshake on the PCI 82540EM; its
  status bit never clears in QEMU and cost the full 1 s timeout.
- disk.c: skip the floppy probe when CMOS register 0x10 says no drive.
- kernel.c: the boot storage scan (and the /data WiFi autoconnect after
  the boot DHCP) runs in a "bootscan" kernel thread; boot_ready is set
  right after the WM starts.  /share is mounted with /ramfs at boot.
- sys_fs.c/vfs.c: /ramfs and /share operations and spawns from them no
  longer wait for a rescan; the storage rescan runs preemptible and its
  floppy/ATA/AHCI/SDHCI wait loops sleep (vfs_scan_poll/relax) for the
  scan's owner, so `storage` no longer freezes the WM.
- usb.c and the HCDs: usb_delay_ms() sleeps inside a scan owned by a
  sleepable process (lsusb, the kworker's HID rescan/hotplug); other
  contexts are refused controller access while it sleeps
  (usb_scan_foreign), and usbhid_poll skips its tick.
- sys_wm.c: COM1 prints "wm: first window frame (term) at N ms" as the
  boot-time measurement end point.

AGENTS.md and docs/ (boot, filesystems, drivers, network, development)
updated.  test-usb, test-install, test-uefi, test-images, test-libc and
test-js pass.
Johannes Zellner 1 неделя назад
Родитель
Сommit
852e70701c
29 измененных файлов с 844 добавлено и 169 удалено
  1. 45 7
      AGENTS.md
  2. 9 6
      ahci.c
  3. 8 1
      ata.c
  4. 37 14
      boot.S
  5. 27 5
      disk.c
  6. 18 5
      docs/boot.html
  7. 1 0
      docs/development.html
  8. 11 1
      docs/drivers.html
  9. 6 1
      docs/filesystems.html
  10. 1 1
      docs/network.html
  11. 11 6
      e1000e.c
  12. 2 3
      ehci.c
  13. 367 50
      inflate.c
  14. 73 30
      kernel.c
  15. 2 3
      ohci.c
  16. 3 1
      sdhci.c
  17. 39 1
      sys_fs.c
  18. 2 0
      sys_fs.h
  19. 1 3
      sys_info.c
  20. 21 0
      sys_wm.c
  21. 1 1
      syscall.c
  22. 2 3
      uhci.c
  23. 55 13
      usb.c
  24. 10 0
      usb.h
  25. 4 0
      usbhid.c
  26. 39 10
      vesa_stage.S
  27. 32 0
      vfs.c
  28. 15 0
      vfs.h
  29. 2 4
      xhci.c

+ 45 - 7
AGENTS.md

@@ -141,12 +141,17 @@ make clean
 ## Boot chain
 
 1. `boot.S` — 512-byte boot sector (FAT16 BPB). Loads contiguous kernel sectors
-   to `0x7E00` (CHS for floppy, INT 13h AH=42h LBA for hard disk), jumps there.
+   to `0x7E00` (CHS for floppy, one sector per call; INT 13h AH=42h LBA for
+   hard disk, up to 64 sectors per call into `ES:0` with `ES` advanced, so the
+   offset never wraps), jumps there with the boot drive still in DL.
 2. `vesa_stage.S` — real-mode VESA stage. Probes INT 15h E820 into `0x1000`,
    enables A20, reads the `NBROOT1` rootfs header at the boot sector's
    `pack_lba` field and copies the gzip pack directly to `PACK_HIGH_ADDR`
    (`0x900000`, above the 8 MB VGA-shadow reserve) in 16 KB chunks through
-   the `0x3000` staging buffer with a temporary flat protected-mode
+   the `0x3000` staging buffer — one INT 13h per chunk (32 sectors over EDD,
+   up to the end of the track over CHS; a refused multi-sector request falls
+   back to one sector per call; the old per-sector loop made ~3600 BIOS calls
+   and took ~1.5 s in QEMU) — with a temporary flat protected-mode
    `rep movsb` (`pm_copy`).  The X201 BIOS's INT 15h AH=87h block move
    silently copies zeros to this range, which left the kernel with an empty
    pack; the CPU copy is used instead.  `paging_prepare_cpu()` still clears
@@ -164,8 +169,14 @@ make clean
    used); fixed disks use EDD LBA.
 3. `kernel.c` — `kernel_main` zeroes BSS then inits serial → memory/paging →
    ACPI/MADT → I/O → power → idle → processes → timer (PIT; 1 kHz busy,
-   100 Hz idle + IDT) → SMP AP bring-up → mouse → VGA shadow buffer →
-   terminal.
+   100 Hz idle + IDT) → SMP AP bring-up → mouse/USB → VGA shadow buffer →
+   ramfs inflate → WM → NIC probe → kworker → the `bootscan` kernel thread
+   (storage scan + FAT mounts, then the `/data` WiFi autoconnect once the
+   boot DHCP finished) → `wm_server_boot_ready()` immediately, so the first
+   terminal starts while the disks are still probed → DHCP.  COM1 prints
+   `wm: first window frame (term) at N ms` (uptime) when the first app
+   window presents: the end point of a boot-time measurement (~0.65 s after
+   the kernel starts on QEMU's USB image).
 4. `mbr.S` — MBR (USB/HDD boot); loads the active partition's boot sector
    (partition 1 at LBA 63).
 5. **UEFI (`efi/`)** — an x86-64 PE32+ `BOOTX64.EFI` loads from the ESP. It
@@ -521,6 +532,17 @@ 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.
+  A rescan (`vfs_mount_all()` from the boot `bootscan` thread or the
+  `storage` command via `sys_fs_scan()`) re-adds `/ramfs` and `/share`
+  first; their operations (and SYS_SPAWN of a RAM path,
+  `sys_fs_enter_path()`) do not wait for `vfs_scan_busy`
+  (`vfs_mount_is_ram()`), only disk paths do.  The scan runs preemptible;
+  the floppy/ATA/AHCI/SDHCI wait loops call `vfs_scan_poll(t0)` /
+  `vfs_scan_relax(ms)`, which sleep (dropping the BKL) only for the scan's
+  owning process, with interrupts on and preemption enabled, and only once
+  a wait passes 2 ms; at boot in the idle context and in normal I/O they
+  are no-ops.  So `storage` no longer freezes the WM.  `disk_probe()` skips
+  a floppy whose CMOS drive type (register `0x10`) is 0.
 - **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`
@@ -589,7 +611,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   82577LM/82578/82579; polled, PCI INTx disabled before MMIO, PCH D0 and
   no BAR sizing so the ME/AMT cannot leave the BAR undecoded; skips the
   global MAC reset while the link is up, because the PCH/ME wedges on the
-  CTRL_RST access; extended RX descriptors only when `RFCTL.EXTEN` reads
+  CTRL_RST access; the PCIe GIO-master-disable handshake is skipped on the
+  conventional-PCI 82540EM, whose STATUS bit 19 never clears in QEMU and
+  used to cost the full 1 s timeout at every boot; extended RX descriptors only when `RFCTL.EXTEN` reads
   back set; the 82540EM (`0x100E`) is pinned to the legacy write-back
   layout, because it has no RFCTL but QEMU stores the register, so EXTEN
   reads back set while the device still writes legacy descriptors),
@@ -696,7 +720,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   ("wifi")`), runs DHCP and leaves `wifi __keep` running for group rekeys.
   A successful menu/CLI connect also writes the SSID and passphrase to
   `/data/WIFI.CFG` (8.3, plaintext, BSSID zeroed so a mesh/repeater AP works);
-  after the boot DHCP attempt `kernel_main` spawns
+  after the storage scan and the boot DHCP attempt the `bootscan` thread spawns
   `/ramfs/bin/wifi autoconnect` when that file exists.  Autoconnect leaves a
   wired link that is physically up alone and only tries WiFi when no wired
   link is present; an already-linked WiFi card is also left alone.  `/data`
@@ -854,6 +878,14 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   and USB3 root ports; halt/HCH reset sequence and BIOS/OS handoff)
   implement it.
   `lsusb.c` — the `lsusb` command (lists every controller and its tree).
+  Every HCD delay goes through `usb_delay_ms()`: inside a scan
+  (`usb_scan`/`usb_hotplug_poll`) owned by a sleepable process — `lsusb`,
+  the kworker's HID rescan/hotplug enumeration — it sleeps and drops the
+  BKL, so port resets no longer stall the system; elsewhere it busy-waits.
+  While such a scan sleeps, `usb_scan_foreign()` makes the `usb.c`
+  wrappers refuse other contexts (control/bulk transfers fail, interrupt
+  polls return 0, `usb_port_connected` reports 1) and `usbhid_poll` skips
+  its tick; USB NICs, the RTL8821AU and USB disks see a transient error.
   `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.
@@ -1298,7 +1330,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   8-bit alpha plane.  The codecs are independent of the OS and ngui: they take
   an input buffer plus a caller-provided `struct img_mem` arena, so a browser
   can link the same objects and decode HTTP bodies.  `inflate.c/h` (DEFLATE,
-  zlib, gzip, CRC32/Adler32) is shared with the boot pack.  `img_sniff()` is a
+  zlib, gzip, CRC32/Adler32) is shared with the boot pack.  Its whole-buffer
+  helpers (`inflate_gzip_mem`/`_zlib_mem`/`_raw_mem`, used by `ramfs_init`
+  and the image decoders) are a separate fast path: 10-bit Huffman lookup
+  tables in the caller's `struct inflate` workspace, the output buffer as
+  the LZ77 history, and one checksum pass at the end (the 3.7 MB pack went
+  from ~1.1 s to ~0.25 s in QEMU).  The streaming API (`inflate_gzip`
+  with a source/sink and progress hook) keeps the bit-at-a-time decoder.  `img_sniff()` is a
   cheap header probe; `img_decode()` returns `struct img_image`; `img_draw()`
   is a clipped, scaled (box-filter down, bilinear up) RGB565 blit with alpha
   over a checkerboard or over the destination (for a browser).  Progressive

+ 9 - 6
ahci.c

@@ -10,6 +10,7 @@
  * linker bound, so nothing large may go into BSS. */
 
 #include "ahci.h"
+#include "vfs.h"
 #include "ata.h"
 #include "mm.h"
 #include "paging.h"
@@ -110,7 +111,7 @@ static void delay_ms(unsigned int ms)
 {
 	unsigned int t0 = get_ticks_ms();
 	while (get_ticks_ms() - t0 < ms)
-		;
+		vfs_scan_poll(t0);
 }
 
 /* ---- PCI discovery ---- */
@@ -189,14 +190,14 @@ static void ahci_port_init(unsigned int port)
 		pwr(port, P_CMD, c & ~CMD_ST);
 		t0 = get_ticks_ms();
 		while ((prd(port, P_CMD) & CMD_CR) && get_ticks_ms() - t0 < 500)
-			;
+			vfs_scan_poll(t0);
 	}
 	c = prd(port, P_CMD);
 	if (c & CMD_FRE) {
 		pwr(port, P_CMD, c & ~CMD_FRE);
 		t0 = get_ticks_ms();
 		while ((prd(port, P_CMD) & CMD_FR) && get_ticks_ms() - t0 < 500)
-			;
+			vfs_scan_poll(t0);
 	}
 
 	pwr(port, P_CLB, ahci.cl);
@@ -211,20 +212,20 @@ static void ahci_port_init(unsigned int port)
 	pwr(port, P_CMD, prd(port, P_CMD) | CMD_FRE);
 	t0 = get_ticks_ms();
 	while (!(prd(port, P_CMD) & CMD_FR) && get_ticks_ms() - t0 < 500)
-		;
+		vfs_scan_poll(t0);
 
 	/* Cold plug: reset the link if it is not established yet. */
 	if ((prd(port, P_SSTS) & 0x0Fu) != 3) {
 		pwr(port, P_SCTL, 1);   /* DET = 1: initiate COMRESET */
 		t0 = get_ticks_ms();
 		while ((prd(port, P_SSTS) & 0x0Fu) != 3 && get_ticks_ms() - t0 < 1000)
-			;
+			vfs_scan_poll(t0);
 	}
 
 	pwr(port, P_CMD, prd(port, P_CMD) | CMD_ST);
 	t0 = get_ticks_ms();
 	while (!(prd(port, P_CMD) & CMD_CR) && get_ticks_ms() - t0 < 500)
-		;
+		vfs_scan_poll(t0);
 }
 
 int ahci_init(void)
@@ -324,6 +325,7 @@ static int ahci_issue(unsigned int port, const unsigned char fis[20], int write,
 	/* Wait for the port to be idle (previous command finished). */
 	t0 = get_ticks_ms();
 	while (prd(port, P_CI) & 1u) {
+		vfs_scan_poll(t0);
 		if (get_ticks_ms() - t0 > 4000)
 			return -1;
 	}
@@ -353,6 +355,7 @@ static int ahci_issue(unsigned int port, const unsigned char fis[20], int write,
 
 	t0 = get_ticks_ms();
 	while (prd(port, P_CI) & 1u) {
+		vfs_scan_poll(t0);
 		if (get_ticks_ms() - t0 > 4000) {
 #if AHCI_DEBUG
 			serial_puts(SERIAL_COM1, "ahci: command timeout\n");

+ 8 - 1
ata.c

@@ -6,6 +6,7 @@
  * (nVidia MCP79 "compatibility mode" and friends). */
 
 #include "ata.h"
+#include "vfs.h"
 #include "pci.h"
 #include "serial.h"
 #include "timer.h"
@@ -184,6 +185,7 @@ static int ata_wait_not_busy(unsigned short base, unsigned int timeout_ms)
 {
 	unsigned int t0 = get_ticks_ms();
 	while (get_ticks_ms() - t0 < timeout_ms) {
+		vfs_scan_poll(t0);
 		if (!(port_inb(base + REG_CMD) & SR_BSY))
 			return 0;
 	}
@@ -201,7 +203,7 @@ static void ata_reset_channel(unsigned short base, unsigned short ctrl)
 	port_outb(ctrl, 0x04);                    /* SRST */
 	t0 = get_ticks_ms();
 	while (get_ticks_ms() - t0 < 10)
-		;
+		vfs_scan_poll(t0);
 	/* nIEN=1: the driver polls, so keep the drive's IRQ line quiet
 	 * instead of leaving a masked level-triggered interrupt asserted. */
 	port_outb(ctrl, 0x02);
@@ -209,6 +211,8 @@ static void ata_reset_channel(unsigned short base, unsigned short ctrl)
 	while (get_ticks_ms() - t0 < 1000) {
 		unsigned char st = port_inb(base + REG_CMD);
 
+		vfs_scan_poll(t0);
+
 		if (!(st & SR_BSY) || st == 0x00 || st == 0xFF)
 			break;
 	}
@@ -219,6 +223,7 @@ static int ata_wait_drq(unsigned short base, unsigned int timeout_ms)
 {
 	unsigned int t0 = get_ticks_ms();
 	while (get_ticks_ms() - t0 < timeout_ms) {
+		vfs_scan_poll(t0);
 		unsigned char st = port_inb(base + REG_CMD);
 		if (st & SR_ERR)
 			return -1;
@@ -237,6 +242,7 @@ static int ata_wait_idle(unsigned short base, unsigned int timeout_ms)
 	unsigned int t0 = get_ticks_ms();
 
 	while (get_ticks_ms() - t0 < timeout_ms) {
+		vfs_scan_poll(t0);
 		unsigned char st = port_inb(base + REG_CMD);
 
 		if (st & SR_ERR)
@@ -280,6 +286,7 @@ static int ata_identify(int bus, int unit, unsigned short *id)
 			break;
 		if (get_ticks_ms() - t0 > ATA_TIMEOUT_MS)
 			return -1;
+		vfs_scan_poll(t0);
 	}
 	if (!(st & SR_DRQ))
 		return -1;

+ 37 - 14
boot.S

@@ -68,31 +68,40 @@ load_kernel:
 	movw  kernel_lba, %si
 	movw  $0x07e0, %ax
 	movw  %ax, %es
-	xorw  %bx, %bx
 
+	/* EDD reads up to 64 sectors per INT 13h into ES:0 and then advances
+	 * ES, so the offset never wraps (one call per sector made the kernel
+	 * load ~700 BIOS calls).  CHS (floppy) stays at one sector: a longer
+	 * transfer could cross a 64 KB ISA DMA boundary. */
 load_loop:
-	cmpw  $0, %cx
+	testw %cx, %cx
 	jz    load_done
 	pushw %cx
 	pushw %si
+	movw  $1, %ax
 	movb  boot_drive, %dl
 	cmpb  $0x80, %dl
 	jb    read_chs
+	movw  %cx, %ax
+	cmpw  chunk_max, %ax
+	jbe   1f
+	movw  chunk_max, %ax
+1:	movw  %ax, cur_count
+	movw  %ax, 0x502
 	xorw  %ax, %ax
+	movw  %ax, 0x504
 	movw  %ax, 0x50a
 	movw  %ax, 0x50c
 	movw  %ax, 0x50e
 	movb  $0x10, 0x500
 	movb  %al, 0x501
-	incw  %ax
-	movw  %ax, 0x502
-	movw  %bx, 0x504
 	movw  %es, 0x506
 	movw  %si, 0x508
 	movw  $0x500, %si
 	movb  $0x42, %ah
 	jmp   do_int13
 read_chs:
+	movw  %ax, cur_count
 	movw  %si, %ax
 	xorw  %dx, %dx
 	movw  $18, %di
@@ -107,19 +116,29 @@ read_chs:
 	movb  boot_drive, %dl
 	movb  $0x02, %ah
 	movb  $1, %al
+	xorw  %bx, %bx
 do_int13:
 	int   $0x13
 	popw  %si
 	popw  %cx
-	jc    read_error
-	addw  $0x200, %bx
-	jnc   1f
-	movw  %es, %ax
-	addw  $0x1000, %ax
-	movw  %ax, %es
-1:	incw  %si
-	decw  %cx
-	jnz   load_loop
+	jnc   2f
+	/* A BIOS that refuses a multi-sector EDD read: reset the drive and
+	 * continue one sector per call (the old behaviour). */
+	cmpw  $1, cur_count
+	je    read_error
+	movw  $1, chunk_max
+	movb  boot_drive, %dl
+	xorw  %ax, %ax
+	int   $0x13
+	jmp   load_loop
+2:	movw  cur_count, %ax
+	addw  %ax, %si
+	subw  %ax, %cx
+	shlw  $5, %ax
+	movw  %es, %bx            /* not DX: DL carries the boot drive */
+	addw  %ax, %bx            /* into the VESA stage */
+	movw  %bx, %es
+	jmp   load_loop
 
 load_done:
 	/* Second marker: the whole kernel image was read without an INT 13h
@@ -132,6 +151,10 @@ load_done:
 
 boot_drive:
 	.byte 0
+chunk_max:
+	.word 64
+cur_count:
+	.word 0
 
 /* INT 13h error: say so instead of halting silently. */
 read_error:

+ 27 - 5
disk.c

@@ -1,6 +1,7 @@
 /* Disk layer: read/write sectors via direct FDC + ISA DMA in protected mode. */
 
 #include "disk.h"
+#include "vfs.h"
 #include "io.h"
 #include "timer.h"
 
@@ -81,6 +82,7 @@ static int fdc_wait_rqm(void)
 	while ((get_ticks_ms() - t0) < 500) {
 		if (inb(FDC_MSR) & MSR_RQM)
 			return 0;
+		vfs_scan_poll(t0);
 	}
 	return -1;
 }
@@ -111,7 +113,8 @@ static int fdc_wait_irq(void)
 	while (!fdc_irq_fired) {
 		if ((get_ticks_ms() - t0) >= irq_timeout_ms)
 			return -1;
-		__asm__ volatile ("hlt");
+		if (!vfs_scan_relax(1))
+			__asm__ volatile ("hlt");
 	}
 	return 0;
 }
@@ -130,6 +133,7 @@ static int fdc_controller_alive(void)
 			return 1;
 		if ((get_ticks_ms() - t0) >= 200)
 			return 0;
+		vfs_scan_poll(t0);
 	}
 }
 
@@ -174,7 +178,8 @@ static void fdc_motor_on(void)
 	{
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < MOTOR_DELAY_MS)
-			__asm__ volatile ("hlt");
+			if (!vfs_scan_relax(MOTOR_DELAY_MS))
+				__asm__ volatile ("hlt");
 	}
 }
 
@@ -194,7 +199,7 @@ static int fdc_reset(void)
 	{
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < 10)
-			;
+			vfs_scan_poll(t0);
 	}
 
 	/* Release reset: controller on, DMA/IRQ enabled, no motor.
@@ -269,7 +274,7 @@ static int fdc_seek(unsigned int cylinder, unsigned int head)
 	{
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < SEEK_SETTLE_MS)
-			;
+			vfs_scan_poll(t0);
 	}
 	return 0;
 }
@@ -491,7 +496,7 @@ static int fdc_init_once(void)
 	{
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < 10)
-			;
+			vfs_scan_poll(t0);
 	}
 
 	fdc_irq_fired = 0;
@@ -554,12 +559,29 @@ void disk_set_drive(unsigned int drive)
 /* Quick presence/media probe used by the boot scan.  One command with a short
  * timeout, never the ten-retry recovery loop: an empty drive must not hold the
  * UI back.  Normal I/O after a successful mount still gets full retries. */
+/* CMOS register 0x10 holds the BIOS's floppy drive types (high nibble
+ * drive 0, low nibble drive 1; 0 = not installed).  Every AT-compatible
+ * BIOS fills it in.  Without this gate a machine whose FDC exists but has
+ * no drive (QEMU without -fda, most laptops/boards with a Super I/O FDC)
+ * paid the 300 ms motor spin-up plus a probe timeout per drive at boot. */
+static int cmos_floppy_type(unsigned int drive)
+{
+	unsigned char v;
+
+	outb(0x70, 0x80 | 0x10);        /* bit 7: NMI disabled while reading */
+	v = inb(0x71);
+	outb(0x70, 0x00);
+	return drive == 0 ? (v >> 4) & 0x0F : v & 0x0F;
+}
+
 int disk_probe(unsigned int drive)
 {
 	int rc;
 
 	if (drive > 1 || fdc_absent)
 		return 0;
+	if (cmos_floppy_type(drive) == 0)
+		return 0;
 
 	irq_timeout_ms = PROBE_TIMEOUT_MS;
 	disk_set_drive(drive);

+ 18 - 5
docs/boot.html

@@ -145,8 +145,11 @@
 		<p>
 			The boot sector carries a FAT16 BPB, loads the contiguous kernel
 			directly after itself to <code>0x7E00</code>, and jumps there.
-			Floppy drives use CHS; fixed disks use <code>INT 13h AH=42h</code>
-			(EDD LBA). The sector count and the rootfs LBA are patched into the
+			Floppy drives use CHS, one sector per call; fixed disks use
+			<code>INT 13h AH=42h</code> (EDD LBA) with up to 64 sectors per call
+			into <code>ES:0</code>, advancing <code>ES</code> so the offset never
+			wraps (one call per sector used to make the kernel load ~700 BIOS
+			calls). The sector count and the rootfs LBA are patched into the
 			boot sector by the Makefile.
 		</p>
 		<p>
@@ -163,7 +166,7 @@
 		<ol>
 			<li>Probe the <code>INT 15h</code> E820 memory map into <code>0x1000</code>.</li>
 			<li>Enable A20 and read the <code>NBROOT1</code> rootfs header at the boot sector's <code>pack_lba</code> field.</li>
-			<li>Copy the compressed pack directly to <code>0x900000</code> above the 8 MB VGA-shadow reserve, in 16 KB staging chunks at <code>0x3000</code>, using a temporary flat protected-mode <code>rep movsb</code>. The X201 BIOS's <code>INT 15h AH=87h</code> block move silently copies zeros to this range, so the CPU copy is used instead.</li>
+			<li>Copy the compressed pack directly to <code>0x900000</code> above the 8 MB VGA-shadow reserve, in 16 KB staging chunks at <code>0x3000</code> (one <code>INT 13h</code> per chunk: 32 sectors over EDD, or up to the end of the track over CHS, which never crosses a 64 KB DMA boundary; a BIOS that refuses a multi-sector request falls back to one sector per call), using a temporary flat protected-mode <code>rep movsb</code>. The X201 BIOS's <code>INT 15h AH=87h</code> block move silently copies zeros to this range, so the CPU copy is used instead.</li>
 			<li>Pick the best VBE mode: at least 16 bpp, a linear framebuffer, it must fit the card's VRAM, and the width must be at most 1280 pixels. Mode information is written to <code>0x600</code>; the 12-dword boot-info block goes to <code>0x500</code>.</li>
 			<li>Enter protected mode and jump to <code>kernel_entry</code>.</li>
 		</ol>
@@ -399,11 +402,21 @@
 			<li>Create the process table, then start the PIT and verify it ticks.</li>
 			<li>Start APs through INIT-SIPI-SIPI (they idle until scheduling is enabled).</li>
 			<li>Initialise PS/2, USB HID and the VGA shadow buffer.</li>
-			<li>Inflate the command pack into <code>/ramfs</code>; start the WM and the first frame.</li>
+			<li>Inflate the command pack into <code>/ramfs</code> (table-driven whole-buffer inflate); start the WM and the first frame.</li>
 			<li>Probe the NIC, restore retained network config, start the kworker.</li>
-			<li>Mount FAT devices; call <code>wm_server_boot_ready()</code> so the WM spawns the first terminal; run DHCP if needed.</li>
+			<li>Start the <code>bootscan</code> kernel thread (storage probe + FAT mounts, then the <code>/data</code> WiFi autoconnect once the boot DHCP is done) and publish <code>vfs_scan_busy</code>; call <code>wm_server_boot_ready()</code> at once so the WM spawns the first terminal while the disks are probed; run DHCP if needed.</li>
 			<li>Enable scheduling on the APs and enter the kernel idle loop.</li>
 		</ol>
+		<p>
+			The terminal no longer waits for storage: <code>/ramfs</code> and
+			<code>/share</code> operations (including spawning commands from
+			<code>/ramfs/bin</code>) skip the scan wait, a disk path waits for it,
+			and the scan thread sleeps through its long probe timeouts.  The
+			end point of a boot measurement is the one-shot COM1 line
+			<code>wm: first window frame (term) at N ms</code> (uptime), written
+			by <span class="file">sys_wm.c</span> when the first application
+			window presents.
+		</p>
 		<p>
 			The boot trace lines written along the way (<code>boot_trace</code>,
 			<code>boot_val</code>) go to the framebuffer console and COM1, so a

+ 1 - 0
docs/development.html

@@ -302,6 +302,7 @@ make net-cmds                   # replace /ramfs only</code></pre>
 		<h2>Debugging</h2>
 		<ul>
 			<li><strong>Serial log.</strong> <code>make monitor</code> shows the boot trace and printf-style driver output. The <code>NebOS v… booted</code> line is written before the framebuffer is ready.</li>
+			<li><strong>Boot time.</strong> Timestamp COM1 on the host (QEMU <code>-serial stdio</code>): the kernel starts at <code>NebOS v… booted</code>, the desktop at <code>wm: first frame</code>, and the terminal is on screen at <code>wm: first window frame (term) at N ms</code> (kernel uptime).  On QEMU the USB image reaches it about 0.65 s after the kernel starts.</li>
 			<li><strong>Network console.</strong> <code>make netmon</code> listens to UDP 19290 for serial and terminal output; <code>netconsole host &lt;ip&gt;</code> narrows the target.</li>
 			<li><strong>dbg.</strong> The ring-0 serial monitor accepts <code>r addr len</code>, <code>w addr b0 …</code>, <code>i port</code>, <code>o port val</code>, <code>h</code> and <code>q</code>.</li>
 			<li><strong>Panic.</strong> A ring-0 fault dumps vector, error code, registers, CRs, EIP bytes and stack candidates to the LFB and COM1. Resolve addresses with <code>nm kernel.elf</code> / <code>addr2line -e kernel.elf</code>.</li>

+ 11 - 1
docs/drivers.html

@@ -297,7 +297,17 @@
 			recursively. This is what makes the X201's internal PCH hubs work.
 			Hotplug watches root ports; a device behind a hub is only found when a
 			full scan runs (<code>lsusb</code> also bumps the USB generation so
-			<code>usbhid</code> re-attaches).
+			<code>usbhid</code> re-attaches).  Every HCD delay goes through
+			<code>usb_delay_ms()</code>: inside a scan owned by a sleepable
+			process (<code>lsusb</code>, the kworker's HID rescan/hotplug) it
+			sleeps and drops the BKL, so port resets and settle times no longer
+			freeze the system.  While such a scan sleeps, the
+			<span class="file">usb.c</span> wrappers refuse controller access
+			from any other context (<code>usb_scan_foreign()</code>: control and
+			bulk transfers fail, interrupt polls return no data, ports read as
+			connected) and <code>usbhid_poll</code> skips its tick.  A USB NIC,
+			the RTL8821AU or a USB disk therefore sees a transient error during a
+			bus scan; the scan re-enumerates those devices anyway.
 		</p>
 
 		<h3>Mass storage</h3>

+ 6 - 1
docs/filesystems.html

@@ -165,7 +165,7 @@
 			<li><strong>Read/write.</strong> Read, write, delete, mkdir and rmdir are implemented; FAT32 allocation uses an FSInfo-seeded free-cluster hint.</li>
 			<li><strong>8.3 names.</strong> The on-disk name is the canonical one. The shell and <code>files</code> display the space-padded form; a long name cannot be created.</li>
 			<li><strong>Directory cache.</strong> The last FAT directory's first 64 entries are cached in <code>vfs.c</code>, so WebDAV's index sequence is one walk instead of N. Any write invalidates the cache.</li>
-			<li><strong>Serialization.</strong> A file operation runs in a non-preemptible section because the FAT driver has static state and the floppy needs IRQ 6; <code>vfs_scan_busy</code>/<code>format_busy</code> make it sleep first while a mount scan or a format owns the disks. A format releases the BKL for its raw-sector loop, and only disk-backed mounts wait for it, so <code>/ramfs</code> and <code>/share</code> stay usable.</li>
+			<li><strong>Serialization.</strong> A file operation runs in a non-preemptible section because the FAT driver has static state and the floppy needs IRQ 6; <code>vfs_scan_busy</code>/<code>format_busy</code> make it sleep first while a mount scan or a format owns the disks. A format releases the BKL for its raw-sector loop, and only disk-backed mounts wait for it, so <code>/ramfs</code> and <code>/share</code> stay usable.  A rescan (the <code>bootscan</code> thread or the <code>storage</code> command) re-adds <code>/ramfs</code>/<code>/share</code> first and is skipped by their operations too (<code>vfs_mount_is_ram</code>); it runs preemptible, and the floppy/ATA/AHCI/SDHCI wait loops call <code>vfs_scan_poll</code>/<code>vfs_scan_relax</code>, which sleep (dropping the BKL) once a wait passes 2 ms and only for the scan's owning process, so <code>storage</code> no longer freezes the WM.  The floppy probe is skipped for a drive whose CMOS type (register <code>0x10</code>) is 0.</li>
 		</ul>
 		<p>
 			<span class="file">fatdetect.c</span> and <span class="file">fatformat.c</span>
@@ -189,6 +189,11 @@
 			one-sector <code>NBROOT1</code> header. At boot the path loads the
 			pack into RAM and <span class="file">ramfs.c</span> inflates it into a
 			private page-allocated copy mounted read-only at <code>/ramfs</code>.
+			The whole-buffer helpers in <span class="file">inflate.c</span>
+			(<code>inflate_gzip_mem</code> and friends, also used by the image
+			decoders) decode with 10-bit Huffman lookup tables straight into the
+			output buffer and checksum once at the end, about five times faster
+			than the bit-at-a-time streaming decoder that HTTP bodies still use.
 		</p>
 
 		<figure class="figure">

+ 1 - 1
docs/network.html

@@ -196,7 +196,7 @@
 			A successful <code>wifi connect</code> (from the CLI or the menu) also saves
 			the SSID and passphrase to <code>/data/WIFI.CFG</code> on the
 			persistent <code>NEBOSDATA</code> partition. After the boot DHCP
-			attempt <code>kernel_main</code> spawns
+			attempt and the storage scan, the <code>bootscan</code> thread spawns
 			<code>wifi autoconnect</code> when that file exists. It samples the
 			active NIC's <code>link_up()</code> state: a wired cable that is
 			physically up keeps the Ethernet path, while no wired link lets

+ 11 - 6
e1000e.c

@@ -1329,12 +1329,17 @@ static int hw_reset(void)
 	 * PCIe master first; clearing RCTL while DMA is active can wedge the
 	 * receive packet buffer on these parts. */
 	ctrl = r32(REG_CTRL);
-	w32(REG_CTRL, ctrl | CTRL_GIO_MASTER_DISABLE);
-	t0 = get_ticks_ms();
-	while ((r32(REG_STATUS) & STATUS_GIO_MASTER_ENABLE) &&
-	       !timed_out(t0, 1000))
-		;
-	delay_ms(5);
+	/* The GIO master handshake only exists on PCIe parts.  The 82540EM
+	 * is conventional PCI: QEMU keeps STATUS bit 19 set forever, so the
+	 * wait used to burn its whole 1 s timeout on every boot. */
+	if (device_id != 0x100E) {
+		w32(REG_CTRL, ctrl | CTRL_GIO_MASTER_DISABLE);
+		t0 = get_ticks_ms();
+		while ((r32(REG_STATUS) & STATUS_GIO_MASTER_ENABLE) &&
+		       !timed_out(t0, 1000))
+			;
+		delay_ms(5);
+	}
 
 	hw_stop();
 	delay_ms(5);

+ 2 - 3
ehci.c

@@ -17,6 +17,7 @@
  * Descriptors (frame list, QHs, qTDs, bounce buffers) come from the page
  * allocator, so the driver does not grow the kernel BSS. */
 
+#include "usb.h"
 #include "ehci.h"
 #include "io.h"
 #include "mm.h"
@@ -290,9 +291,7 @@ static unsigned int port_reg(struct ehci_ctrl *c, unsigned int port)
 
 static void delay_ms(unsigned int ms)
 {
-	unsigned int t0 = get_ticks_ms();
-	while (get_ticks_ms() - t0 < ms)
-		;
+	usb_delay_ms(ms);    /* sleeps inside a sleepable scan */
 }
 
 static void zero_region(unsigned int addr, unsigned int len)

+ 367 - 50
inflate.c

@@ -11,7 +11,7 @@
 #define MAXDCODES 30
 #define MAXCODES  (MAXLCODES + MAXDCODES)
 #define FIXLCODES 288
-#define FASTBITS  9
+#define FASTBITS  10
 
 static unsigned int crc_table[256];
 static int crc_table_ready;
@@ -580,82 +580,399 @@ int inflate_gzip(struct inflate *z, unsigned int *out_len)
 	return 0;
 }
 
-/* ---- whole-buffer helpers ------------------------------------------- */
+/* ---- whole-buffer fast path ----------------------------------------
+ *
+ * The streaming decoder above reads one bit per Huffman step and stages
+ * every byte in the 32 KB ring.  When the whole input and output are in
+ * memory (the boot rootfs, PNG/HTTP bodies decoded into one buffer) that
+ * is needlessly slow: here the output buffer itself is the LZ77 history,
+ * the bit buffer is refilled a byte at a time up to 32 bits, and codes of
+ * up to FASTBITS are resolved with one table lookup.  Longer codes fall
+ * back to the canonical walk on the bits already in the buffer.  Only the
+ * checksum the container needs is computed, once, over the finished
+ * output.  The tables live in the caller's struct-inflate workspace. */
+
+#define FAST_SIZE (1u << FASTBITS)
+
+struct fhuff {
+	struct inflate_huff h;
+	unsigned short fast[FAST_SIZE];     /* (sym << 4) | len, 0 = slow */
+};
+
+struct fz {
+	const unsigned char *in, *in_end;
+	unsigned int bitbuf;
+	int bitcnt;
+	unsigned int over;                  /* zero bytes padded past the end */
+	unsigned char *out, *out_start, *out_end;
+};
 
-struct mem_sink {
-	unsigned char *dst;
-	unsigned int cap, pos;
+struct fz_ws {
+	struct fhuff lit, dist;
 };
 
-static int mem_write(void *ctx, const unsigned char *data, unsigned int len)
+static void fz_refill(struct fz *f)
+{
+	while (f->bitcnt <= 24) {
+		unsigned int b = 0;
+
+		if (f->in < f->in_end)
+			b = *f->in++;
+		else
+			f->over++;
+		f->bitbuf |= b << f->bitcnt;
+		f->bitcnt += 8;
+	}
+}
+
+static int fz_bits(struct fz *f, int n)
+{
+	unsigned int v;
+
+	if (f->bitcnt < n)
+		fz_refill(f);
+	v = f->bitbuf & ((1u << n) - 1u);
+	f->bitbuf >>= n;
+	f->bitcnt -= n;
+	return (int)v;
+}
+
+static int fhuff_build(struct fhuff *fh, const unsigned char *lengths, int n)
 {
-	struct mem_sink *m = (struct mem_sink *)ctx;
+	unsigned int next[MAXBITS + 2];
+	int r, len, sym, i;
 
-	if (len > m->cap - m->pos)
+	r = huff_build(&fh->h, lengths, n);
+	if (r < 0)
 		return -1;
+	for (i = 0; i < (int)FAST_SIZE; i++)
+		fh->fast[i] = 0;
 	{
-		unsigned int i;
+		/* next[len] = first canonical code of length len. */
+		unsigned int c = 0;
+
+		next[1] = 0;
+		for (len = 1; len < MAXBITS; len++) {
+			c = (c + fh->h.count[len]) << 1;
+			next[len + 1] = c;
+		}
+	}
+	for (sym = 0; sym < n; sym++) {
+		unsigned int c, rev = 0;
+		int l = lengths[sym], k;
+
+		if (!l)
+			continue;
+		c = next[l]++;
+		if (l > FASTBITS)
+			continue;
+		for (k = 0; k < l; k++)
+			rev |= ((c >> k) & 1u) << (l - 1 - k);
+		for (; rev < FAST_SIZE; rev += 1u << l)
+			fh->fast[rev] = (unsigned short)((sym << 4) | l);
+	}
+	return r;
+}
+
+static int fz_decode(struct fz *f, const struct fhuff *fh)
+{
+	unsigned int e;
+	int len, code = 0, first = 0, index = 0;
+
+	if (f->bitcnt < MAXBITS)
+		fz_refill(f);
+	e = fh->fast[f->bitbuf & (FAST_SIZE - 1u)];
+	if (e) {
+		f->bitbuf >>= e & 15u;
+		f->bitcnt -= (int)(e & 15u);
+		return (int)(e >> 4);
+	}
+	for (len = 1; len <= MAXBITS; len++) {
+		int count;
+
+		code |= (int)(f->bitbuf & 1u);
+		f->bitbuf >>= 1;
+		f->bitcnt--;
+		count = fh->h.count[len];
+		if (code - count < first)
+			return fh->h.symbol[index + (code - first)];
+		index += count;
+		first += count;
+		first <<= 1;
+		code <<= 1;
+	}
+	return -1;
+}
+
+static int fz_codes(struct fz *f, const struct fhuff *lit,
+		    const struct fhuff *dist)
+{
+	for (;;) {
+		int sym = fz_decode(f, lit);
+		unsigned int len, d;
+		unsigned char *out, *src;
+
+		if (sym < 256) {
+			if (sym < 0 || f->out >= f->out_end)
+				return -1;
+			*f->out++ = (unsigned char)sym;
+			continue;
+		}
+		if (sym == 256)
+			return f->over > 4 ? -1 : 0;
+		sym -= 257;
+		if (sym >= 29)
+			return -1;
+		len = len_base[sym];
+		if (len_ext[sym])
+			len += (unsigned int)fz_bits(f, len_ext[sym]);
+		sym = fz_decode(f, dist);
+		if (sym < 0 || sym >= 30)
+			return -1;
+		d = dist_base[sym];
+		if (dist_ext[sym])
+			d += (unsigned int)fz_bits(f, dist_ext[sym]);
+		out = f->out;
+		if (d > (unsigned int)(out - f->out_start) ||
+		    len > (unsigned int)(f->out_end - out))
+			return -1;
+		src = out - d;
+		if (d >= 4) {
+			while (len >= 4) {
+				out[0] = src[0]; out[1] = src[1];
+				out[2] = src[2]; out[3] = src[3];
+				out += 4; src += 4; len -= 4;
+			}
+		}
+		while (len--)
+			*out++ = *src++;
+		f->out = out;
+	}
+}
 
-		for (i = 0; i < len; i++)
-			m->dst[m->pos + i] = data[i];
+static int fz_blocks(struct fz *f, struct fz_ws *ws)
+{
+	static const unsigned char order[19] = {
+		16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
+	};
+	unsigned char lengths[FIXLCODES + MAXDCODES];
+	int last, type, i;
+
+	do {
+		last = fz_bits(f, 1);
+		type = fz_bits(f, 2);
+		if (type == 0) {
+			unsigned int len, nlen;
+
+			fz_bits(f, f->bitcnt & 7);
+			len = (unsigned int)fz_bits(f, 16);
+			nlen = (unsigned int)fz_bits(f, 16);
+			if ((len ^ nlen) != 0xFFFFu ||
+			    len > (unsigned int)(f->out_end - f->out))
+				return -1;
+			/* Return the whole buffered bytes to the input and
+			 * copy the stored data straight from it. */
+			{
+				unsigned int back = (unsigned int)f->bitcnt >> 3;
+
+				if (back < f->over)
+					return -1;
+				f->in -= back - f->over;
+				f->bitbuf = 0;
+				f->bitcnt = 0;
+				f->over = 0;
+			}
+			if (len > (unsigned int)(f->in_end - f->in))
+				return -1;
+			while (len--)
+				*f->out++ = *f->in++;
+		} else if (type == 1) {
+			for (i = 0; i < 144; i++)
+				lengths[i] = 8;
+			for (; i < 256; i++)
+				lengths[i] = 9;
+			for (; i < 280; i++)
+				lengths[i] = 7;
+			for (; i < 288; i++)
+				lengths[i] = 8;
+			for (i = 0; i < 30; i++)
+				lengths[288 + i] = 5;
+			if (fhuff_build(&ws->lit, lengths, 288) < 0 ||
+			    fhuff_build(&ws->dist, lengths + 288, 30) < 0 ||
+			    fz_codes(f, &ws->lit, &ws->dist))
+				return -1;
+		} else if (type == 2) {
+			int hlit = fz_bits(f, 5) + 257;
+			int hdist = fz_bits(f, 5) + 1;
+			int hclen = fz_bits(f, 4) + 4;
+			int total = hlit + hdist;
+
+			if (hlit > MAXLCODES || hdist > MAXDCODES)
+				return -1;
+			for (i = 0; i < 19; i++)
+				lengths[i] = 0;
+			for (i = 0; i < hclen; i++)
+				lengths[order[i]] = (unsigned char)fz_bits(f, 3);
+			if (fhuff_build(&ws->lit, lengths, 19) < 0)
+				return -1;
+			for (i = 0; i < total; ) {
+				int sym = fz_decode(f, &ws->lit), rep, val;
+
+				if (sym < 0)
+					return -1;
+				if (sym < 16) {
+					lengths[i++] = (unsigned char)sym;
+					continue;
+				}
+				if (sym == 16) {
+					if (i == 0)
+						return -1;
+					rep = 3 + fz_bits(f, 2);
+					val = lengths[i - 1];
+				} else if (sym == 17) {
+					rep = 3 + fz_bits(f, 3);
+					val = 0;
+				} else {
+					rep = 11 + fz_bits(f, 7);
+					val = 0;
+				}
+				if (i + rep > total)
+					return -1;
+				while (rep--)
+					lengths[i++] = (unsigned char)val;
+			}
+			if (fhuff_build(&ws->lit, lengths, hlit) < 0 ||
+			    fhuff_build(&ws->dist, lengths + hlit, hdist) < 0 ||
+			    fz_codes(f, &ws->lit, &ws->dist))
+				return -1;
+		} else {
+			return -1;
+		}
+		if (f->over > 4)
+			return -1;
+	} while (!last);
+
+	/* Byte-align and hand the whole bytes still buffered back to the
+	 * input, so the container trailer is read from memory. */
+	fz_bits(f, f->bitcnt & 7);
+	{
+		unsigned int back = (unsigned int)f->bitcnt >> 3;
+
+		if (back < f->over)
+			return -1;          /* consumed padding: truncated */
+		f->in -= back - f->over;
+		f->bitbuf = 0;
+		f->bitcnt = 0;
+		f->over = 0;
 	}
-	m->pos += len;
 	return 0;
 }
 
-static int mem_inflate(const void *src, unsigned int len, void *dst,
-		       unsigned int cap, unsigned int *out_len, void *ws,
-		       int gzip)
-{
-	struct inflate *z = (struct inflate *)ws;
-	struct inflate_src s;
-	struct inflate_sink sink;
-	struct mem_sink m;
-
-	s.p = (const unsigned char *)src;
-	s.len = len;
-	s.more = 0;
-	s.ctx = 0;
-	m.dst = (unsigned char *)dst;
-	m.cap = cap;
-	m.pos = 0;
-	sink.write = mem_write;
-	sink.ctx = &m;
-	inflate_init(z, &s, &sink);
-	if (gzip ? inflate_gzip(z, out_len) : inflate_zlib(z, out_len))
+/* container: 0 raw, 1 zlib, 2 gzip */
+static int fz_inflate(const void *src, unsigned int len, void *dst,
+		      unsigned int cap, unsigned int *out_len, void *ws,
+		      int container)
+{
+	struct fz f;
+	const unsigned char *p = (const unsigned char *)src;
+	const unsigned char *end = p + len;
+	unsigned int n;
+
+	if (container == 2) {
+		unsigned int flg;
+
+		if (len < 18 || p[0] != 0x1F || p[1] != 0x8B || p[2] != 8)
+			return -1;
+		flg = p[3];
+		p += 10;
+		if (flg & 0x04) {
+			if (end - p < 2)
+				return -1;
+			n = p[0] | ((unsigned int)p[1] << 8);
+			p += 2;
+			if ((unsigned int)(end - p) < n)
+				return -1;
+			p += n;
+		}
+		if (flg & 0x08) {
+			while (p < end && *p)
+				p++;
+			p++;
+		}
+		if (flg & 0x10) {
+			while (p < end && *p)
+				p++;
+			p++;
+		}
+		if (flg & 0x02)
+			p += 2;
+		if (p >= end)
+			return -1;
+	} else if (container == 1) {
+		if (len < 6 || (p[0] & 0x0F) != 8 || (p[0] >> 4) > 7 ||
+		    (((unsigned int)p[0] << 8) | p[1]) % 31 != 0 ||
+		    (p[1] & 0x20))
+			return -1;
+		p += 2;
+	}
+
+	f.in = p;
+	f.in_end = end;
+	f.bitbuf = 0;
+	f.bitcnt = 0;
+	f.over = 0;
+	f.out = f.out_start = (unsigned char *)dst;
+	f.out_end = f.out_start + cap;
+	if (fz_blocks(&f, (struct fz_ws *)ws))
 		return -1;
+	n = (unsigned int)(f.out - f.out_start);
+
+	if (container == 2) {
+		unsigned int crc, size;
+
+		if (f.in_end - f.in < 8)
+			return -1;
+		crc_table_init();
+		crc = crc_update(0xFFFFFFFFu, f.out_start, n) ^ 0xFFFFFFFFu;
+		if (crc != (f.in[0] | ((unsigned int)f.in[1] << 8) |
+			    ((unsigned int)f.in[2] << 16) |
+			    ((unsigned int)f.in[3] << 24)))
+			return -1;
+		size = f.in[4] | ((unsigned int)f.in[5] << 8) |
+		       ((unsigned int)f.in[6] << 16) |
+		       ((unsigned int)f.in[7] << 24);
+		if (size != n)
+			return -1;
+	} else if (container == 1) {
+		unsigned int a = 1, b = 0;
+
+		if (f.in_end - f.in < 4)
+			return -1;
+		adler_update(&a, &b, f.out_start, n);
+		if (((b << 16) | a) != (((unsigned int)f.in[0] << 24) |
+					((unsigned int)f.in[1] << 16) |
+					((unsigned int)f.in[2] << 8) | f.in[3]))
+			return -1;
+	}
+	if (out_len)
+		*out_len = n;
 	return 0;
 }
 
 int inflate_zlib_mem(const void *src, unsigned int len, void *dst,
 		     unsigned int cap, unsigned int *out_len, void *ws)
 {
-	return mem_inflate(src, len, dst, cap, out_len, ws, 0);
+	return fz_inflate(src, len, dst, cap, out_len, ws, 1);
 }
 
 int inflate_gzip_mem(const void *src, unsigned int len, void *dst,
 		     unsigned int cap, unsigned int *out_len, void *ws)
 {
-	return mem_inflate(src, len, dst, cap, out_len, ws, 1);
+	return fz_inflate(src, len, dst, cap, out_len, ws, 2);
 }
 
 int inflate_raw_mem(const void *src, unsigned int len, void *dst,
 		    unsigned int cap, unsigned int *out_len, void *ws)
 {
-	struct inflate *z = (struct inflate *)ws;
-	struct inflate_src s;
-	struct inflate_sink sink;
-	struct mem_sink m;
-
-	s.p = (const unsigned char *)src;
-	s.len = len;
-	s.more = 0;
-	s.ctx = 0;
-	m.dst = (unsigned char *)dst;
-	m.cap = cap;
-	m.pos = 0;
-	sink.write = mem_write;
-	sink.ctx = &m;
-	inflate_init(z, &s, &sink);
-	return inflate_raw(z, out_len);
+	return fz_inflate(src, len, dst, cap, out_len, ws, 0);
 }

+ 73 - 30
kernel.c

@@ -368,11 +368,54 @@ static int net_restore_boot_config(void)
 	return dns[0] || dns[1] || dns[2] || dns[3];
 }
 
+/* Saved WiFi credentials on the writable /data volume: let the normal
+ * supplicant reconnect in the background.  It leaves a wired link that is
+ * physically up alone, and a missing /data or wifi helper is harmless. */
+static void boot_wifi_autoconnect(void)
+{
+	unsigned int sz = 0;
+	int is_dir = 0;
+
+	if (vfs_stat(WIFI_CRED_PATH, &sz, &is_dir) == 0 &&
+	    !is_dir && sz > 0) {
+		struct vfs_file f;
+		struct vfs_mount *m = 0;
+
+		if (vfs_load_file("/ramfs/bin/wifi", &f, &m) == 0) {
+			if (process_create_user("wifi", f.data, f.size,
+						"autoconnect", "/") > 0)
+				serial_puts(SERIAL_COM1,
+					    "wifi: autoconnect started\n");
+			vfs_release_file(m, &f);
+		}
+	}
+}
+
+/* Boot-time storage scan in its own kernel thread.  A process (unlike the
+ * idle context kernel_main runs in) may sleep, so the probes' long waits
+ * (floppy motor, ATA/AHCI timeouts) go through vfs_scan_relax() and the WM
+ * and the first terminal run meanwhile.  Disk file operations wait for
+ * vfs_scan_busy; /ramfs and /share do not.  The WiFi autoconnect needs
+ * /data, so it runs here after the mount. */
+static volatile int boot_dhcp_done;
+
+static void boot_storage_main(void)
+{
+	vfs_mount_all();
+	boot_trace("storage");
+	/* As before, WiFi only starts after the boot DHCP attempt on the
+	 * wired NIC, so two DHCP clients never overlap. */
+	while (!boot_dhcp_done)
+		process_sleep_ms(20);
+	boot_wifi_autoconnect();
+	boot_trace("wifi auto");
+}
+
 void kernel_main(void);
 void kernel_main(void)
 {
 	unsigned char *p = __bss_start;
-	int net_kept;
+	int net_kept, storage_inline;
 	while (p < __bss_end)
 		*p++ = 0;
 
@@ -519,6 +562,10 @@ void kernel_main(void)
 		}
 		(void)ok;
 		vfs_mount_ramfs();
+		/* /share too: the WM and the first terminal may use it while the
+		 * bootscan thread is still probing the disks, and a path that
+		 * does not resolve yet would wait for the whole scan. */
+		vfs_mount_share();
 	}
 	boot_trace("ramfs");
 	boot_val("ramfs files", ramfs_count());
@@ -555,14 +602,28 @@ void kernel_main(void)
 	}
 	boot_trace("kworker");
 
-	/* Slow storage bring-up happens behind the usable desktop now. */
+	/* Slow storage bring-up happens behind the usable desktop: publish the
+	 * scan (disk paths wait for it) before the thread even runs, so no
+	 * command can see the half-built boot mount table.  Without a thread
+	 * slot, scan inline as before. */
 	boot_trace("storage begin");
-	vfs_mount_all();
-	boot_trace("storage");
+	storage_inline = 0;
+	{
+		int pid = process_create("bootscan", boot_storage_main);
+
+		if (pid > 0) {
+			vfs_scan_owner_pid = (unsigned int)pid;
+			vfs_scan_busy = 1;
+		} else {
+			vfs_mount_all();
+			boot_trace("storage");
+			storage_inline = 1;
+		}
+	}
 
-	/* The WM spawns the first terminal once it sees boot_ready: while the
-	 * boot scan was rebuilding the mount table, no user command could touch
-	 * the disks.  The desktop itself was already painted from /ramfs. */
+	/* The WM spawns the first terminal once it sees boot_ready.  The
+	 * terminal and every /ramfs command load without waiting for the
+	 * storage scan; a disk path waits for it (vfs_scan_busy). */
 #ifndef NOWM
 	wm_server_boot_ready();
 	boot_trace("terminal ready");
@@ -584,30 +645,12 @@ void kernel_main(void)
 		boot_trace(net_kept ? "dhcp skipped" :
 			   (linked ? "dhcp" : "dhcp no link"));
 	}
-
-	/* If the writable /data volume has saved WiFi credentials, let the
-	 * normal supplicant reconnect in the background.  It leaves a wired
-	 * link that is physically up alone, and a missing /data or wifi
-	 * helper is harmless. */
-	{
-		unsigned int sz = 0;
-		int is_dir = 0;
-
-		if (vfs_stat(WIFI_CRED_PATH, &sz, &is_dir) == 0 &&
-		    !is_dir && sz > 0) {
-			struct vfs_file f;
-			struct vfs_mount *m = 0;
-
-			if (vfs_load_file("/ramfs/bin/wifi", &f, &m) == 0) {
-				if (process_create_user("wifi", f.data, f.size,
-							"autoconnect", "/") > 0)
-					serial_puts(SERIAL_COM1,
-						    "wifi: autoconnect started\n");
-				vfs_release_file(m, &f);
-			}
-		}
+	boot_dhcp_done = 1;
+	if (storage_inline) {
+		boot_wifi_autoconnect();
+		boot_trace("wifi auto");
 	}
-	boot_trace("wifi auto");
+
 
 	/* Dump the active NIC's counters after the boot DHCP attempt.  With
 	 * the ASIX as the management link this appears in netmon even when

+ 2 - 3
ohci.c

@@ -10,6 +10,7 @@
    Up to four controller instances (MCP79 exposes four of them); each PCI
    function is one root hub with its own device address space. */
 
+#include "usb.h"
 #include "ohci.h"
 #include "usb_hcd.h"
 #include "pci.h"
@@ -168,9 +169,7 @@ static void wr(struct ohci_ctrl *c, unsigned int ofs, unsigned int val)
 
 static void delay_ms(unsigned int ms)
 {
-	unsigned int t0 = get_ticks_ms();
-	while (get_ticks_ms() - t0 < ms)
-		;
+	usb_delay_ms(ms);    /* sleeps inside a sleepable scan */
 }
 
 static unsigned int port_reg(unsigned int port)

+ 3 - 1
sdhci.c

@@ -15,6 +15,7 @@
  * the card.
  */
 #include "sdhci.h"
+#include "vfs.h"
 #include "paging.h"
 #include "pci.h"
 #include "process.h"
@@ -145,7 +146,7 @@ static void tiny_delay(unsigned int ms)
 	unsigned int t0 = get_ticks_ms();
 
 	while (get_ticks_ms() - t0 < ms)
-		;
+		vfs_scan_poll(t0);
 }
 
 static int wait_state(unsigned int mask, int set, unsigned int ms)
@@ -159,6 +160,7 @@ static int wait_state(unsigned int mask, int set, unsigned int ms)
 			return 0;
 		if (get_ticks_ms() - t0 > ms)
 			return -1;
+		vfs_scan_poll(t0);
 	}
 }
 

+ 39 - 1
sys_fs.c

@@ -49,6 +49,9 @@ static void fs_scan_wait(void)
 	}
 }
 
+static int fs_resolve(const char *path, struct vfs_mount **m,
+		      char *rel, unsigned int rel_size);
+
 /* The disk may need its IRQ (floppy), and the FAT driver's static state must
  * not be preempted in the middle of an operation. */
 static void fs_preempt_enter(void)
@@ -67,6 +70,35 @@ void sys_fs_enter(void)
 	fs_preempt_enter();
 }
 
+/* sys_fs_enter() for an ELF load: a RAM-backed path (the usual
+ * /ramfs/bin/<cmd>) neither waits for a rescan nor for a format, so the WM
+ * and terminals can launch commands while `storage` probes the disks. */
+void sys_fs_enter_path(const char *path)
+{
+	struct vfs_mount *m;
+	char rel[VFS_PATH_MAX];
+
+	if (!(path && fs_resolve(path, &m, rel, sizeof(rel)) == 0 &&
+	      vfs_mount_is_ram(m))) {
+		fs_format_wait();
+		fs_scan_wait();
+	}
+	fs_preempt_enter();
+}
+
+/* The `storage` rescan: wait for a format or another scan, but do NOT
+ * disable preemption.  The scan publishes vfs_scan_busy (disk file
+ * operations wait for it) and its probes sleep through vfs_scan_relax(),
+ * so the rest of the system keeps running while the disks are probed. */
+void sys_fs_scan(void)
+{
+	fs_format_wait();
+	fs_scan_wait();
+	__asm__ volatile ("sti");
+	vfs_mount_all();
+	__asm__ volatile ("cli");
+}
+
 void sys_fs_exit(void)
 {
 	__asm__ volatile ("cli");
@@ -115,8 +147,14 @@ static int sys_fs_begin(const char *path, struct vfs_mount **m,
 {
 	int r;
 
-	fs_scan_wait();
+	/* /ramfs and /share are re-added first by a rescan and stay valid
+	 * while it sleeps: only disk paths (and paths that do not resolve
+	 * yet, which may be a disk being re-mounted) wait for the scan. */
 	r = fs_resolve(path, m, rel, rel_size);
+	if (vfs_scan_busy && (r != 0 || !vfs_mount_is_ram(*m))) {
+		fs_scan_wait();
+		r = fs_resolve(path, m, rel, rel_size);
+	}
 	if (r != 0)
 		return r;
 	if (vfs_mount_is_disk(*m)) {

+ 2 - 0
sys_fs.h

@@ -7,6 +7,8 @@
 /* Enter/leave the fs non-preemptible section (used by other disk-touching
  * services, e.g. the storage info refresh).  See sys_fs.c. */
 void sys_fs_enter(void);
+void sys_fs_enter_path(const char *path);
+void sys_fs_scan(void);
 void sys_fs_exit(void);
 
 int sys_read_file(const char *path, void *buf, unsigned int max);

+ 1 - 3
sys_info.c

@@ -102,9 +102,7 @@ int sys_mouseinfo(struct sys_mouseinfo *out)
 
 int sys_storage_info(struct sys_storage_info *out)
 {
-	sys_fs_enter();                 /* vfs_mount_all() reads disks */
-	vfs_mount_all();
-	sys_fs_exit();
+	sys_fs_scan();                  /* rescan; sleeps in the probes */
 
 	out->ahci_present = ahci_controller_present() ? 1 : 0;
 	out->blkdev_count = blk_dev_count;

+ 21 - 0
sys_wm.c

@@ -6,6 +6,8 @@
 #include "paging.h"
 #include "process.h"
 #include "wm.h"
+#include "serial.h"
+#include "timer.h"
 
 static int user_ok(const void *p, unsigned int size, int write)
 {
@@ -164,10 +166,29 @@ int sys_win_create(const struct win_create *in, struct win_info *out)
 	return r;
 }
 
+/* One serial line when the first application window (normally the boot
+ * terminal) presents a frame: the end point of the boot-time measurement
+ * ("[boot] ..." lines are relative to the PIT start, this is uptime). */
+static void boot_first_present(void)
+{
+	static int done;
+
+	if (done)
+		return;
+	done = 1;
+	serial_puts(SERIAL_COM1, "wm: first window frame (");
+	serial_puts(SERIAL_COM1, current_proc()->name);
+	serial_puts(SERIAL_COM1, ") at ");
+	serial_putdec(SERIAL_COM1, get_ticks_ms());
+	serial_puts(SERIAL_COM1, " ms\n");
+}
+
 int sys_win_present(const struct wm_win_present *in)
 {
 	struct wm_win_present p;
 
+	boot_first_present();
+
 	if (!in)
 		return wm_server_win_present(0);
 	if (!user_ok(in, sizeof(p), 0))

+ 1 - 1
syscall.c

@@ -377,7 +377,7 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		/* A FAT load reads the floppy through IRQ 6, so open the
 		 * syscall's interrupts-off section for the duration (the same
 		 * window SYS_READ_FILE uses). */
-		sys_fs_enter();
+		sys_fs_enter_path(path);
 		lr = vfs_load_file(path, &f, &m);
 		sys_fs_exit();
 		if (lr != 0)

+ 2 - 3
uhci.c

@@ -8,6 +8,7 @@
    fixed low-memory address); among several PCI functions we pick the one
    whose root port has a device attached. */
 
+#include "usb.h"
 #include "pci.h"
 #include "serial.h"
 #include "timer.h"
@@ -120,9 +121,7 @@ static void reg_writel(unsigned int reg, unsigned int val)
 
 static void delay_ms(unsigned int ms)
 {
-	unsigned int t0 = get_ticks_ms();
-	while (get_ticks_ms() - t0 < ms)
-		;
+	usb_delay_ms(ms);    /* sleeps inside a sleepable scan */
 }
 
 static void td_set(struct uhci_td *td, unsigned int link, unsigned int status,

+ 55 - 13
usb.c

@@ -3,6 +3,8 @@
    usb_hcd_ops vtable (uhci.c / ohci.c / ehci.c / xhci.c). */
 
 #include "io.h"
+#include "process.h"
+#include "sched.h"
 #include "timer.h"
 #include "usb.h"
 #include "usb_hcd.h"
@@ -110,6 +112,43 @@ 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;
+static unsigned int scan_owner_pid;     /* pid that set scan_busy */
+
+static unsigned int cur_pid(void)
+{
+	struct process *p = current_proc();
+
+	return p ? p->pid : 0;
+}
+
+static void scan_begin(void)
+{
+	scan_busy = 1;
+	scan_owner_pid = cur_pid();
+}
+
+int usb_scan_foreign(void)
+{
+	return scan_busy && cur_pid() != scan_owner_pid;
+}
+
+void usb_delay_ms(unsigned int ms)
+{
+	unsigned int t0, flags;
+
+	__asm__ volatile ("pushfl; popl %0" : "=r"(flags));
+	/* Only the scan owner sleeps, never the idle/boot context (pid 0),
+	 * a non-preemptible section or an interrupts-off syscall (SYS_WIFI
+	 * runs with IF clear). */
+	if (scan_busy && scan_owner_pid && cur_pid() == scan_owner_pid &&
+	    !this_cpu()->preempt_count && (flags & 0x200u)) {
+		process_sleep_ms(ms ? ms : 1);
+		return;
+	}
+	t0 = get_ticks_ms();
+	while (get_ticks_ms() - t0 < ms)
+		;
+}
 
 static unsigned char dev_desc[18];
 static unsigned char desc8[64];
@@ -145,9 +184,7 @@ static void put_hex(unsigned int n, unsigned int digits)
 
 static void msleep(unsigned int ms)
 {
-	unsigned int t0 = get_ticks_ms();
-	while (get_ticks_ms() - t0 < ms)
-		;
+	usb_delay_ms(ms);
 }
 
 static void indent(unsigned int depth)
@@ -196,7 +233,7 @@ int usb_control_in(unsigned int ctrl, unsigned int addr, unsigned int mps,
 	const struct usb_hcd_ops *ops;
 	unsigned char setup[8];
 
-	if (ctrl >= n_usb_ctrls)
+	if (ctrl >= n_usb_ctrls || usb_scan_foreign())
 		return -1;
 	ops = usb_ctrls[ctrl].ops;
 	setup[0] = rt;
@@ -218,7 +255,7 @@ int usb_control_out(unsigned int ctrl, unsigned int addr, unsigned int mps,
 	const struct usb_hcd_ops *ops;
 	unsigned char setup[8];
 
-	if (ctrl >= n_usb_ctrls)
+	if (ctrl >= n_usb_ctrls || usb_scan_foreign())
 		return -1;
 	ops = usb_ctrls[ctrl].ops;
 	setup[0] = rt;
@@ -242,7 +279,7 @@ int usb_control_out_data(unsigned int ctrl, unsigned int addr,
 	const struct usb_hcd_ops *ops;
 	unsigned char setup[8];
 
-	if (ctrl >= n_usb_ctrls)
+	if (ctrl >= n_usb_ctrls || usb_scan_foreign())
 		return -1;
 	ops = usb_ctrls[ctrl].ops;
 	setup[0] = rt;
@@ -671,6 +708,8 @@ int usb_port_connected(unsigned int ctrl, unsigned int port)
 {
 	if (ctrl >= n_usb_ctrls)
 		return 0;
+	if (usb_scan_foreign())
+		return 1;       /* do not detach anything mid-scan */
 	return usb_ctrls[ctrl].ops->port_connected(usb_ctrls[ctrl].local,
 						   port);
 }
@@ -681,7 +720,8 @@ int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
 	const struct usb_hcd_ops *ops;
 	int r;
 
-	if (ctrl >= n_usb_ctrls || slot >= USB_HCD_INT_SLOTS)
+	if (ctrl >= n_usb_ctrls || slot >= USB_HCD_INT_SLOTS ||
+	    usb_scan_foreign())
 		return -1;
 	ops = usb_ctrls[ctrl].ops;
 	if (slot_ops[slot]) {           /* stale owner from another HCD */
@@ -696,14 +736,14 @@ int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
 
 int usb_int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len)
 {
-	if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot])
+	if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot] || usb_scan_foreign())
 		return 0;
 	return slot_ops[slot]->int_in_poll(slot, buf, len);
 }
 
 void usb_int_in_stop(unsigned int slot)
 {
-	if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot])
+	if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot] || usb_scan_foreign())
 		return;
 	slot_ops[slot]->int_in_stop(slot);
 	slot_ops[slot] = 0;
@@ -726,7 +766,8 @@ int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		 unsigned int mps, const unsigned char *buf,
 		 unsigned int len)
 {
-	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_out)
+	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_out ||
+	    usb_scan_foreign())
 		return -1;
 	return usb_ctrls[ctrl].ops->bulk_out(usb_ctrls[ctrl].local, addr, ep,
 					     mps, buf, len);
@@ -735,7 +776,8 @@ int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
 int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		unsigned int mps, unsigned char *buf, unsigned int len)
 {
-	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_in)
+	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_in ||
+	    usb_scan_foreign())
 		return -1;
 	return usb_ctrls[ctrl].ops->bulk_in(usb_ctrls[ctrl].local, addr, ep,
 					    mps, buf, len);
@@ -758,7 +800,7 @@ int usb_scan(usb_probe_fn probe)
 	if (usb_hcd_init() != 0)
 		return -1;
 
-	scan_busy = 1;
+	scan_begin();
 	generation++;
 	current_probe = probe;
 	print_devices = (probe == 0);
@@ -832,7 +874,7 @@ int usb_hotplug_poll(usb_probe_fn probe)
 		return 0;
 	last = now;
 
-	scan_busy = 1;
+	scan_begin();
 	current_probe = probe;
 	for (c = 0; c < n_usb_ctrls; c++) {
 		const struct usb_hcd_ops *ops = usb_ctrls[c].ops;

+ 10 - 0
usb.h

@@ -22,6 +22,16 @@ const char *usb_hcd_name_ctrl(unsigned int ctrl);
    completed scan bumps the generation counter. */
 int usb_scan(usb_probe_fn probe);
 
+/* Delay used by the USB core and every HCD.  Inside a bus scan owned by a
+ * sleepable process (lsusb, the kworker's HID rescan) it sleeps, which
+ * drops the BKL so the WM and other processes keep running through port
+ * resets and settle times; otherwise it busy-waits as before. */
+void usb_delay_ms(unsigned int ms);
+/* 1 while another context's scan owns the controllers (it is sleeping in
+ * usb_delay_ms).  The transfer wrappers then refuse access; pollers such as
+ * usbhid skip their tick. */
+int usb_scan_foreign(void);
+
 /* Cheap periodic watcher: enumerates devices on root ports whose connect
    state changed since the last call (internally throttled).  Returns the
    number of ports with a new device. */

+ 4 - 0
usbhid.c

@@ -386,6 +386,10 @@ int usbhid_poll(unsigned int *buttons, int *dx, int *dy, int *scroll)
 		*scroll = 0;
 	if (!hcd_ok)
 		return 0;
+	/* Another context's scan is sleeping between port resets: the
+	 * controllers are its until it finishes. */
+	if (usb_scan_foreign())
+		return 0;
 
 	/* A full bus re-scan (`lsusb`) invalidates every handle. */
 	if (usb_generation() != gen) {

+ 39 - 10
vesa_stage.S

@@ -390,14 +390,26 @@ a20_wait:
  * out: data at 0x3000, CF set on failure
  * Clobbers all general registers. */
 read_sectors:
+	/* One INT 13h per run of sectors, not per sector: EDD transfers the
+	 * whole chunk (<= 32 sectors into the 16 KB 0x3000 buffer), CHS
+	 * transfers up to the end of the current track (the buffer never
+	 * crosses a 64 KB ISA DMA boundary).  The old per-sector loop made
+	 * ~3600 BIOS calls for a 1.8 MB rootfs.  A BIOS that refuses a
+	 * multi-sector request falls back to one sector per call for the
+	 * rest of this read, so the old behaviour is the failure path. */
 	movl  %eax, rd_lba
 	movw  %cx, rd_left
 	movw  $0, rd_off
 	movw  $0, rd_tries
+	movb  $0, rd_single
 read_retry:
-	movw  rd_off, %bx
 	xorw  %ax, %ax
 	movw  %ax, %ds
+	movw  rd_left, %ax
+	cmpb  $0, rd_single
+	je    1f
+	movw  $1, %ax
+1:	movw  %ax, rd_count
 	movw  $0x0300, %ax
 	movw  %ax, %es
 	movb  stage_drive, %dl
@@ -407,7 +419,9 @@ read_retry:
 read_lba:
 	/* EDD LBA read */
 	movw  $0x10, 0x500
-	movw  $1, 0x502
+	movw  rd_count, %ax
+	movw  %ax, 0x502
+	movw  rd_off, %bx
 	movw  %bx, 0x504
 	movw  $0x0300, 0x506
 	movl  rd_lba, %eax
@@ -421,8 +435,13 @@ read_chs:
 	movw  rd_lba, %ax
 	xorw  %dx, %dx
 	movw  $18, %di
-	divw  %di
-	movb  %dl, %cl
+	divw  %di                     /* ax = track index, dx = sector - 1 */
+	movw  $18, %bx
+	subw  %dx, %bx                /* sectors left on this track */
+	cmpw  rd_count, %bx
+	jae   2f
+	movw  %bx, rd_count
+2:	movb  %dl, %cl
 	incb  %cl
 	xorw  %dx, %dx
 	movw  $2, %di
@@ -431,28 +450,36 @@ read_chs:
 	movb  %al, %ch
 	movb  stage_drive, %dl
 	movb  $0x02, %ah
-	movb  $1, %al
+	movb  rd_count, %al
 	movw  rd_off, %bx
 
 read_int:
 	int   $0x13
 	jnc   read_ok
-	/* Reset and retry the same sector a few times. */
+	/* Reset the drive.  A refused multi-sector request switches to the
+	 * single-sector path; a single sector gets a few retries. */
 	pushw %cx
 	movb  stage_drive, %dl
 	xorw  %ax, %ax
 	int   $0x13
 	popw  %cx
-	incw  rd_tries
+	cmpw  $1, rd_count
+	je    3f
+	movb  $1, rd_single
+	jmp   read_retry
+3:	incw  rd_tries
 	cmpw  $3, rd_tries
 	jb    read_retry
 	stc
 	ret
 
 read_ok:
-	incl  rd_lba
-	addw  $0x200, rd_off
-	decw  rd_left
+	movzwl rd_count, %eax
+	addl  %eax, rd_lba
+	shlw  $9, %ax
+	addw  %ax, rd_off
+	movw  rd_count, %ax
+	subw  %ax, rd_left
 	jnz   read_retry
 	clc
 	ret
@@ -624,6 +651,8 @@ copy_len:       .long 0
 rd_lba:         .long 0
 rd_off:         .word 0
 rd_left:        .word 0
+rd_count:       .word 0
+rd_single:      .byte 0
 rd_tries:       .word 0
 
 msg_pack:       .asciz "Loading rootfs [d="

+ 32 - 0
vfs.c

@@ -7,6 +7,7 @@
 #include "io.h"
 #include "kmalloc.h"
 #include "process.h"
+#include "sched.h"
 #include "ramfs.h"
 #include "rootfs.h"
 #include "serial.h"
@@ -398,6 +399,32 @@ int vfs_mount_is_disk(const vfs_mount_t *m)
 	return m && m->ops == &fat_ops;
 }
 
+void vfs_scan_poll(unsigned int t0)
+{
+	if (vfs_scan_busy && vfs_scan_owner_pid && get_ticks_ms() - t0 >= 2)
+		vfs_scan_relax(1);
+}
+
+int vfs_scan_relax(unsigned int ms)
+{
+	struct process *p;
+
+	if (!vfs_scan_busy || !vfs_scan_owner_pid)
+		return 0;
+	p = current_proc();
+	if (!p || p->pid != vfs_scan_owner_pid || this_cpu()->preempt_count)
+		return 0;
+	{
+		unsigned int flags;
+
+		__asm__ volatile ("pushfl; popl %0" : "=r"(flags));
+		if (!(flags & 0x200u))
+			return 0;       /* interrupts off: cannot sleep */
+	}
+	process_sleep_ms(ms ? ms : 1);
+	return 1;
+}
+
 /* ---- ramfs implementation ---- */
 
 static int ramfs_op_read_file(struct vfs_mount *m, const char *path,
@@ -539,6 +566,11 @@ static const struct fs_ops ramfs_ops = {
 	.list_dir = ramfs_op_list_dir,
 };
 
+int vfs_mount_is_ram(const vfs_mount_t *m)
+{
+	return m && (m->ops == &ramfs_ops || m->ops == &share_ops);
+}
+
 /* ---- mounts ---- */
 
 static vfs_mount_t *mount_slot(const char *name, const char *dev)

+ 15 - 0
vfs.h

@@ -87,6 +87,21 @@ void vfs_remount_device(blk_dev_t *d);
 extern volatile int vfs_scan_busy;
 extern volatile unsigned int vfs_scan_owner_pid;
 
+/* 1 for the RAM-backed mounts (/ramfs, /share).  They are re-added first and
+ * stay valid while a rescan sleeps, so their file operations do not wait. */
+int vfs_mount_is_ram(const vfs_mount_t *m);
+
+/* Called from the wait loops of the storage probes (floppy, ATA, AHCI,
+ * SDHCI).  When the scan belongs to a sleepable process (the `storage`
+ * command) it sleeps for `ms` instead of spinning, which drops the BKL so
+ * the WM and other processes keep running; at boot (idle context) and in
+ * normal I/O it is a no-op and the caller keeps busy-waiting. */
+int vfs_scan_relax(unsigned int ms);
+/* For a driver wait loop that started at tick t0: once it has waited 2 ms
+ * or more, relax (sleep 1 ms) if a sleepable rescan owns the disks.  Fast
+ * handshakes and normal I/O never sleep. */
+void vfs_scan_poll(unsigned int t0);
+
 /* Mount the command pack (ramfs) at /ramfs; no-op when no pack is loaded. */
 void vfs_mount_ramfs(void);
 

+ 2 - 4
xhci.c

@@ -10,6 +10,7 @@
  * The schedule is polled, like the rest of the kernel.  Rings and contexts
  * are page-allocated so the driver does not grow the kernel BSS. */
 
+#include "usb.h"
 #include "mm.h"
 #include "paging.h"
 #include "pci.h"
@@ -266,10 +267,7 @@ static void dbg_dec(const char *s, unsigned int v) { (void)s; (void)v; }
 
 static void delay_ms(unsigned int ms)
 {
-	unsigned int t0 = get_ticks_ms();
-
-	while (get_ticks_ms() - t0 < ms)
-		;
+	usb_delay_ms(ms);    /* sleeps inside a sleepable scan */
 }
 
 /* ---- MMIO helpers ------------------------------------------------------ */