Prechádzať zdrojové kódy

Mount the ring-3 command pack as /ramfs and give the shell a PATH

Commands stop being kernel blobs: the ring-3 ELFs (plus hello) are packed by
the new mkpack tool into cmds.pack (as bin/<name>), embedded in kernel.bin as
before (cmds_pack.o) and mounted read-only at /ramfs, which the shell searches
through a per-terminal PATH.  Adding a command is now just adding
ucmd_<name>.c to UCMDS.

- mkpack.c: pack format NBPAK1 (header, per-file table, 4-byte aligned data).
  The per-command *_embed.o objects and the registry's CMD_USER rows/ucmds[]
  blobs are gone.
- ramfs.c/h: validate a pack, look up and list its files; mounted at boot
  (serial: "ramfs: pack ok, 26 files").  Writes return a read-only error.
- vfs.c/h: an fs_ops vtable (FAT and ramfs implementations) keeps sys_fs.c,
  run.c and the shell filesystem-agnostic; the mount listing carries type,
  size and the read-only flag, and fs_ops->raw_83 drives the 8.3 display in
  Tab completion.
- terminal.c: the registry holds only the builtins (cd, path, help, clear)
  and the kernel commands.  Dispatch is builtins -> PATH -> kernel commands,
  so a packed command can shadow a kernel one.  `path` shows/sets the search
  path (validated, default /ramfs/bin); help and completion list the PATH
  contents; hello is simply bin/hello.  A non-ELF on the PATH reports
  "not an executable".
- Relative paths work again: each process inherits the spawning terminal's
  working directory (struct process.cwd, SYS_GETCWD) and the file syscalls
  resolve relative paths against it; the shell passes arguments verbatim
  instead of pre-resolving them.
- process.c: control blocks are cleared when the table is allocated/grown and
  MMIO is only mapped into live slots.  kmalloc memory is not cleared and
  QEMU hides that (its RAM starts zeroed), so the board booted with a stale
  procs[i].cr3 and paging_map_mmio_cr3() faulted before the WM (EIP in that
  function, CR2 above the 128 MB identity map), which also silenced netmon.
  The fault is reproducible in QEMU by poisoning the process table, and the
  same dirty table boots with this fix.

Verified on QEMU: the shell/PATH/ramfs/FAT suite (help, path show/set and
shadowing, ls /ramfs and /ramfs/bin, read-only save/rm/mkdir, cat of a packed
ELF, FAT save/cat/rm/mkdir/rmdir, relative ls after cd, hello from the pack:
22/22 checks) and a clean serial boot; the board boots a flash image built
from this tree.

AGENTS.md: the shell/PATH and ring-3 command bullets, the VFS bullet, the
fresh-memory gotcha and the command lists.
Johannes Zellner 1 mesiac pred
rodič
commit
d1733c601b
26 zmenil súbory, kde vykonal 1665 pridanie a 607 odobranie
  1. 2 0
      .gitignore
  2. 41 27
      AGENTS.md
  3. 21 9
      Makefile
  4. 36 0
      fat.c
  5. 4 0
      fat.h
  6. 16 0
      kernel.c
  7. 153 0
      mkpack.c
  8. 26 5
      process.c
  9. 5 2
      process.h
  10. 251 0
      ramfs.c
  11. 38 0
      ramfs.h
  12. 15 22
      run.c
  13. 1 1
      switch.S
  14. 94 146
      sys_fs.c
  15. 12 0
      syscall.c
  16. 2 0
      syscall.h
  17. 382 368
      terminal.c
  18. 1 0
      terminal.h
  19. 5 0
      ucmd_lib.h
  20. 12 0
      ucmd_ls.c
  21. 4 0
      ucmd_mkdir.c
  22. 4 0
      ucmd_rm.c
  23. 4 0
      ucmd_rmdir.c
  24. 4 0
      ucmd_save.c
  25. 455 19
      vfs.c
  26. 77 8
      vfs.h

+ 2 - 0
.gitignore

@@ -2,7 +2,9 @@
 *.bin
 *.elf
 *.img
+*.pack
 send_serial
 kexec_send
 netkexec_send
 netmonitor
+mkpack

+ 41 - 27
AGENTS.md

@@ -99,22 +99,28 @@ below the run area), so adding code no longer moves any address.
 - **On-screen debug**: `kdbg.c/h` — framebuffer debug overlay (live status rows,
   heartbeat, boot info rows, exception banner) for machines without serial; see
   the kdbg section below.
-- **Shell**: `terminal.c` — history, line editing (Tab completes command
-  names from the registry table and VFS paths via `fat_list_dir`), and a
-  table-driven command registry (`cmds[]`: name, usage, spawn kind/handler)
-  that drives dispatch, `help` and completion.  Runs in a window
-  (`term_run(struct term *)`); reads keys from its per-terminal queue
+- **Shell**: `terminal.c` — history, line editing, and a table-driven command
+  registry (`cmds[]`: name, usage, spawn kind/handler) that holds only the
+  builtins (`cd`, `path`, `help`, `clear`) and the kernel commands.  Everything
+  else is a file on the per-terminal **PATH** (`struct term.path`, default
+  `/ramfs/bin`, `path` builtin to show/set): dispatch is builtins -> PATH ->
+  kernel commands, so a packed command can shadow a kernel one.  Tab
+  completion and `help` list the registry plus the PATH contents.  Runs in a
+  window (`term_run(struct term *)`); reads keys from its per-terminal queue
   (filled by the WM).
 - **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 registry), `ata.c` (ATA PIO, either the
   legacy ports or the PCI IDE interface's native BARs — `ata_init_ports()`),
-  `disk.c` (FDC + ISA DMA ch2, retries).  `sys_fs.c` is the file service for
-  ring-3 commands: whole-file read/write, index-based directory listing, mount
-  listing, unlink/mkdir/rmdir.  A FAT operation sets `sys_fs_busy`, which the
-  scheduler honours (no process switch during it): the FAT driver has static
-  state and the floppy still needs its IRQ.  So expect a brief system stall
-  (not ticks — the PIT keeps running) while a command reads or writes a disk.
+  `disk.c` (FDC + ISA DMA ch2, retries).  `vfs.c` exposes an `fs_ops` vtable
+  (FAT and the ramfs implement it) so the file service and the shell never
+  touch `fat_*` directly; `sys_fs.c` is the file service for ring-3 commands:
+  whole-file read/write, index-based directory listing, mount listing,
+  unlink/mkdir/rmdir (writes to the ramfs return a read-only error).  A FAT
+  operation sets `sys_fs_busy`, which the scheduler honours (no process switch
+  during it): the FAT driver has static state and the floppy still needs its
+  IRQ.  So expect a brief system stall (not ticks — the PIT keeps running)
+  while a command reads or writes a disk.
 - **SATA/AHCI**: `ahci.c/h` — polled AHCI driver (first controller only, PCI
   class `01:06:01`, no IRQs, no NCQ): port bring-up with COMRESET, IDENTIFY
   (shared `ata_identify_parse()` with `ata.c`) and READ/WRITE DMA EXT through
@@ -180,21 +186,22 @@ below the run area), so adding code no longer moves any address.
   from FAT.  Rebuilds are wired (`space.c` -> `space.bin` -> `space_embed.o` ->
   `kernel.elf`).  Note: full-screen programs draw over the WM, which keeps
   repainting its desktop — running them windowed/cleanly is a follow-up.
-- **Ring-3 commands**: `ucmd_*.c`, `ucmd_lib.h`, `ucmd.ld` — freestanding
-  ELFs linked at `USER_BASE` (`0x40000000`), built by the Makefile, embedded
-  in the kernel image (`*_embed.o`) and listed as `CMD_USER` rows in the
-  terminal's command registry (`cmds[]` + `ucmds[]`).  Moved so far:
-  `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`,
-  `mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`,
-  `ping`, `pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`,
-  `netloop`, `netreset`, `curl`.  They talk to the kernel through `int 0x80`
-  (`syscall.h`: putstr/ticks/sleep/exit, the whole-file service in
+- **Ring-3 commands**: `ucmd_*.c`, `ucmd_lib.h`, `ucmd.ld`, `mkpack.c`,
+  `ramfs.c/h` — freestanding ELFs linked at `USER_BASE` (`0x40000000`), packed
+  by `mkpack` into `cmds.pack` (as `bin/<name>`), embedded in the kernel
+  image (`cmds_pack.o`) and mounted read-only at `/ramfs`.  The shell finds
+  them through PATH (`/ramfs/bin` by default), loads the whole ELF through
+  the VFS and spawns it as a ring-3 process; `hello` is just `bin/hello`.
+  New command: add `ucmd_<name>.c` to UCMDS — the pack, the mount and the
+  PATH listing pick it up automatically.  They talk to the kernel through
+  `int 0x80` (`syscall.h`: putstr/ticks/sleep/exit, the whole-file service in
   `sys_fs.c`, the state snapshots in `sys_info.c` and the network service in
   `sys_net.c`) and inherit the terminal's screen, so their output lands in
-  the owning window.  New commands: add `ucmd_<name>.c`, a `CMD_USER` row in
-  `cmds[]`, and a blob in `ucmds[]` (the Makefile picks the file up via its
-  `ucmd_%` pattern rules).  The one argument is passed as a C string on the
-  top of the user stack (`process_create_user(..., arg)`), so
+  the owning window.  Each process inherits the spawning terminal's working
+  directory (`struct process.cwd`, exposed as `SYS_GETCWD`), and the file
+  syscalls resolve relative paths against it, so commands keep working after
+  `cd` (the shell passes arguments verbatim).  The one argument is passed as a
+  C string on the top of the user stack (`process_create_user(..., arg)`), so
   `_start(const char *arg)` needs no crt0.
 - **Network service**: `sys_net.c/h` — the NIC and the stack stay in the
   kernel; ring-3 commands get `SYS_NET_INFO`/config, the DNS and DHCP clients
@@ -247,9 +254,9 @@ filesystem commands operate inside a mount, e.g. `cat /floppy1/HELLO.TXT`).
 `cpuinfo`, `ls`, `cat`, `save`, `rm`, `mkdir`, `rmdir`, `meminfo`,
 `mouseinfo`, `storage`, `netinfo`, `netip`, `netdns`, `host`, `dhcp`, `ping`,
 `pong`, `netsend`, `netrecv`, `netlog`, `netconsole`, `netregs`, `netloop`,
-`netreset` and `curl` run as ring-3 processes (embedded `ucmd_*.c`); the rest
-are kernel commands spawned as processes, or shell builtins (`cd`, `clear`,
-`help`).
+`netreset`, `curl` and `hello` are files in `/ramfs/bin` (packed by the
+Makefile), found through the shell's PATH; the rest are kernel commands
+spawned as processes, or builtins (`cd`, `path`, `help`, `clear`).
 
 ## Serial hot-reload (kexec) workflow
 
@@ -557,6 +564,13 @@ Caveats:
 - **Paging is on but flat** — identity-mapped 4 MB pages (`paging.c`), so
   physical == virtual everywhere. Still no per-process address spaces or process
   isolation: everything runs ring-0 and shares the address space.
+- **Fresh memory is not zeroed** — `mm_alloc_page`/`kmalloc` hand out whatever
+  RAM contained; QEMU starts guest RAM cleared, real hardware does not, so a
+  structure whose fields are read before being written must be cleared on
+  allocation.  `clear_proc()` (`process.c`) exists for exactly this: a stale
+  `procs[i].cr3` made the boot-time MMIO mapping (`paging_map_mmio_cr3`) fault
+  on the board while QEMU was fine.  Anything above `MM_MAX_PHYS` (128 MB) is
+  also *unmapped*, so a wild pointer there is an instant #PF.
 - **All drawing goes into the shadow buffer** (`vga_put_pixel_rgb` etc.); a 30 Hz
   flush copies dirty rows to VRAM. Full-screen programs call `vga_clear_rgb`.
 - **FAT is read/write** — FAT12/16/32 read plus write/delete/mkdir/rmdir.

+ 21 - 9
Makefile

@@ -248,7 +248,7 @@ sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o hello_embed.o prog_embed.o space_embed.o ucmd_cpuinfo_embed.o ucmd_ls_embed.o ucmd_cat_embed.o ucmd_save_embed.o ucmd_rm_embed.o ucmd_mkdir_embed.o ucmd_rmdir_embed.o ucmd_meminfo_embed.o ucmd_mouseinfo_embed.o ucmd_storage_embed.o ucmd_netinfo_embed.o ucmd_netip_embed.o ucmd_netdns_embed.o ucmd_host_embed.o ucmd_dhcp_embed.o ucmd_netsend_embed.o ucmd_netrecv_embed.o ucmd_ping_embed.o ucmd_pong_embed.o ucmd_netlog_embed.o ucmd_netconsole_embed.o ucmd_netregs_embed.o ucmd_netloop_embed.o ucmd_netreset_embed.o ucmd_curl_embed.o
+kernel.elf: vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o prog_embed.o space_embed.o cmds_pack.o ramfs.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -329,10 +329,9 @@ space: space.bin
 
 prog: prog.bin
 
-# Ring-3 commands (see the command registry in terminal.c): built as freestanding
-# ELFs at USER_BASE and embedded in the kernel image so `make dev`/`net-dev`
-# deliver them too.  New command: ucmd_<name>.c + a CMD_USER row in cmds[] and a
-# blob in ucmds[].
+# Ring-3 commands: freestanding ELFs at USER_BASE, packed into cmds.pack and
+# mounted at /ramfs/bin (the shell's PATH default).  New command: add
+# ucmd_<name>.c to UCMDS; the pack is rebuilt automatically.
 UCMDS = ucmd_cpuinfo ucmd_ls ucmd_cat ucmd_save ucmd_rm ucmd_mkdir ucmd_rmdir \
         ucmd_meminfo ucmd_mouseinfo ucmd_storage \
         ucmd_netinfo ucmd_netip ucmd_netdns ucmd_host ucmd_dhcp \
@@ -341,7 +340,7 @@ UCMDS = ucmd_cpuinfo ucmd_ls ucmd_cat ucmd_save ucmd_rm ucmd_mkdir ucmd_rmdir \
 
 # Keep the linked ELFs around (make would treat them as disposable
 # intermediates between the pattern rules).
-.PRECIOUS: $(addprefix $(UCMDS),.elf)
+.PRECIOUS: $(addsuffix .elf,$(UCMDS))
 
 ucmd_%.o: ucmd_%.c ucmd_lib.h syscall.h
 	$(CC) $(CFLAGS) -o $@ $<
@@ -350,8 +349,21 @@ ucmd_%.elf: ucmd_%.o ucmd.ld
 	$(LD) -T ucmd.ld -nostdlib -m elf_i386 -o $@ $<
 	$(OBJCOPY) --strip-all $@ $@
 
-ucmd_%_embed.o: ucmd_%.elf
-	$(LD) -m elf_i386 -r -b binary -o $@ $<
+mkpack: mkpack.c
+	gcc -O2 -o $@ $<
+
+# Command pack: the ring-3 command ELFs plus hello, embedded in the kernel and
+# mounted at /ramfs (bin/).  See mkpack.c and ramfs.h.
+PACK_FILES = $(foreach f,$(UCMDS),bin/$(patsubst ucmd_%,%,$(f))=$(f).elf) \
+             bin/hello=hello.elf
+
+cmds.pack: $(addsuffix .elf,$(UCMDS)) hello.elf mkpack
+	./mkpack $@ $(PACK_FILES)
+
+cmds_pack.o: cmds.pack
+	$(LD) -m elf_i386 -r -b binary -o cmds_pack.o cmds.pack
+
+ramfs.o: ramfs.c ramfs.h serial.h
 
 space.o: space.c run_api.h
 	$(CC) $(CFLAGS) -o $@ $<
@@ -523,4 +535,4 @@ 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 process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o $(addprefix $(UCMDS),.o) $(addprefix $(UCMDS),.elf) $(addsuffix _embed.o,$(UCMDS)) bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o syscall_stub.o screen.o io.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o vga_copy.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o clock.o colors.o display.o mouse.o uhci.o ohci.o usb.o usbhid.o lsusb.o kdbg.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin hello.elf hello_embed.o prog_embed.o space_embed.o cmds.pack cmds_pack.o ramfs.o payload.o mkpack $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor

+ 36 - 0
fat.c

@@ -962,3 +962,39 @@ int fat_resolve_dir(fat_fs_t *fs, const char *path, unsigned int *dir_cluster)
 	*dir_cluster = dir;
 	return 0;
 }
+
+/* Stat a path: directory, then file.  See fat.h. */
+int fat_stat(fat_fs_t *fs, const char *path, unsigned int *size, int *is_dir)
+{
+	unsigned int dir, sz;
+	char name_8_3[12];
+
+	if (fat_resolve_dir(fs, path, &dir) == 0) {
+		if (size)
+			*size = 0;
+		if (is_dir)
+			*is_dir = 1;
+		return 0;
+	}
+
+	{
+		int r = fat_resolve_path(fs, path, &dir, name_8_3);
+		unsigned char attr;
+
+		if (r == -2)
+			return -2;
+		if (r != 0)
+			return -1;
+		r = fat_lookup(fs, dir, (const unsigned char *)name_8_3, 0, &sz,
+			       &attr, 0, 0);
+		if (r == -2)
+			return -2;
+		if (r != 0)
+			return -1;
+		if (size)
+			*size = sz;
+		if (is_dir)
+			*is_dir = (attr & 0x10) ? 1 : 0;
+		return 0;
+	}
+}

+ 4 - 0
fat.h

@@ -60,4 +60,8 @@ int fat_resolve_path(fat_fs_t *fs, const char *path, unsigned int *dir_cluster,
  * An empty path resolves to the root. */
 int fat_resolve_dir(fat_fs_t *fs, const char *path, unsigned int *dir_cluster);
 
+/* Stat a path: sets *is_dir and *size (file size; 0 for directories).
+ * Returns 0, -1 not found, -2 on I/O error. */
+int fat_stat(fat_fs_t *fs, const char *path, unsigned int *size, int *is_dir);
+
 #endif

+ 16 - 0
kernel.c

@@ -11,6 +11,7 @@
 #include "netlog.h"
 #include "paging.h"
 #include "process.h"
+#include "ramfs.h"
 #include "timer.h"
 #include "tss.h"
 #include "usbhid.h"
@@ -220,6 +221,21 @@ void kernel_main(void)
 	kdbg_init();
 	kdbg_status();
 	vfs_mount_all();
+	/* The command pack: read-only, mounted at /ramfs (see ramfs.c). */
+	{
+		extern unsigned char _binary_cmds_pack_start[];
+		extern unsigned char _binary_cmds_pack_end[];
+		unsigned int pack_len = (unsigned int)(_binary_cmds_pack_end -
+						       _binary_cmds_pack_start);
+		if (ramfs_init(_binary_cmds_pack_start, pack_len) != 0)
+			serial_puts(SERIAL_COM1, "ramfs: bad pack\n");
+		else {
+			serial_puts(SERIAL_COM1, "ramfs: pack ok, ");
+			serial_putdec(SERIAL_COM1, ramfs_count());
+			serial_puts(SERIAL_COM1, " files\n");
+			vfs_mount_ramfs();
+		}
+	}
 	/* Bring the NIC up before the WM loop starts, so an idle board can pick
 	 * up a network kexec (`make net-dev`) without any typing. */
 	netdev_active();

+ 153 - 0
mkpack.c

@@ -0,0 +1,153 @@
+/* mkpack: build the command pack that the kernel embeds and mounts at /ramfs.
+ *
+ * Usage: mkpack <out.pack> name=file [name=file ...]
+ *
+ * Format (little-endian):
+ *   offset 0    char magic[8] = "NBPAK1\0\0"
+ *   offset 8    u32 count
+ *   offset 12   u32 total_size
+ *   offset 16   count entries: { char path[32]; u32 offset; u32 size; }
+ *   ...         file data, each 4-byte aligned
+ * All offsets are relative to the start of the pack. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+static const char MAGIC[8] = { 'N', 'B', 'P', 'A', 'K', '1', '\0', '\0' };
+#define PATH_MAX_ 32
+
+struct hdr {
+	char magic[8];
+	uint32_t count;
+	uint32_t total;
+};
+
+struct ent {
+	char path[PATH_MAX_];
+	uint32_t off;
+	uint32_t size;
+};
+
+static void put32le(unsigned char *p, uint32_t v)
+{
+	p[0] = (unsigned char)(v & 0xFF);
+	p[1] = (unsigned char)((v >> 8) & 0xFF);
+	p[2] = (unsigned char)((v >> 16) & 0xFF);
+	p[3] = (unsigned char)((v >> 24) & 0xFF);
+}
+
+static unsigned char *read_file(const char *path, long *size)
+{
+	FILE *f = fopen(path, "rb");
+	unsigned char *data;
+	long n;
+
+	if (!f) {
+		fprintf(stderr, "mkpack: %s: cannot open\n", path);
+		exit(1);
+	}
+	fseek(f, 0, SEEK_END);
+	n = ftell(f);
+	fseek(f, 0, SEEK_SET);
+	if (n <= 0) {
+		fprintf(stderr, "mkpack: %s: empty\n", path);
+		exit(1);
+	}
+	data = malloc((size_t)n);
+	if (!data || fread(data, 1, (size_t)n, f) != (size_t)n) {
+		fprintf(stderr, "mkpack: %s: read failed\n", path);
+		exit(1);
+	}
+	fclose(f);
+	*size = n;
+	return data;
+}
+
+int main(int argc, char **argv)
+{
+	struct hdr h;
+	struct ent *ents;
+	unsigned char *out;
+	unsigned int i, off, total;
+	const char *eq;
+
+	if (argc < 3) {
+		fprintf(stderr, "usage: %s <out.pack> name=file [name=file ...]\n",
+			argv[0]);
+		return 1;
+	}
+
+	ents = calloc((size_t)(argc - 2), sizeof(*ents));
+	if (!ents) {
+		fprintf(stderr, "mkpack: out of memory\n");
+		return 1;
+	}
+
+	/* Reserve the header + table; data follows.  Build in memory: the
+	 * pack is a few hundred KB at most. */
+	off = (unsigned int)(sizeof(h) + (unsigned int)(argc - 2) * sizeof(*ents));
+	out = malloc(8u * 1024u * 1024u);
+	if (!out) {
+		fprintf(stderr, "mkpack: out of memory\n");
+		return 1;
+	}
+
+	for (i = 0; i < (unsigned int)(argc - 2); i++) {
+		unsigned char *data;
+		long n;
+		unsigned int name_len;
+
+		eq = strchr(argv[2 + i], '=');
+		if (!eq || eq == argv[2 + i]) {
+			fprintf(stderr, "mkpack: expected name=file, got '%s'\n",
+				argv[2 + i]);
+			return 1;
+		}
+		name_len = (unsigned int)(eq - argv[2 + i]);
+		if (name_len >= PATH_MAX_) {
+			fprintf(stderr, "mkpack: name too long: %.*s\n",
+				(int)name_len, argv[2 + i]);
+			return 1;
+		}
+		memcpy(ents[i].path, argv[2 + i], name_len);
+		ents[i].path[name_len] = '\0';
+
+		data = read_file(eq + 1, &n);
+		off = (off + 3u) & ~3u;              /* align each file */
+		if (off + (unsigned int)n > 8u * 1024u * 1024u) {
+			fprintf(stderr, "mkpack: pack too large\n");
+			return 1;
+		}
+		memcpy(out + off, data, (size_t)n);
+		ents[i].off = off;
+		ents[i].size = (uint32_t)n;
+		off += (unsigned int)n;
+		free(data);
+	}
+
+	total = off;
+	memcpy(h.magic, MAGIC, sizeof(MAGIC));
+	put32le((unsigned char *)&h.count, (uint32_t)(argc - 2));
+	put32le((unsigned char *)&h.total, total);
+	memcpy(out, &h, sizeof(h));
+	for (i = 0; i < (unsigned int)(argc - 2); i++) {
+		unsigned char *e = out + sizeof(h) + i * sizeof(*ents);
+		memcpy(e, ents[i].path, PATH_MAX_);
+		put32le(e + PATH_MAX_, ents[i].off);
+		put32le(e + PATH_MAX_ + 4, ents[i].size);
+	}
+
+	{
+		FILE *f = fopen(argv[1], "wb");
+		if (!f || fwrite(out, 1, total, f) != total) {
+			fprintf(stderr, "mkpack: %s: write failed\n", argv[1]);
+			return 1;
+		}
+		fclose(f);
+	}
+
+	fprintf(stderr, "mkpack: %s: %u files, %u bytes\n", argv[1],
+		(unsigned int)(argc - 2), total);
+	return 0;
+}

+ 26 - 5
process.c

@@ -61,6 +61,19 @@ static unsigned int stack_alloc(int *owned)
 	return 0;
 }
 
+/* Clear one control block.  kmalloc memory is not zeroed and QEMU hides that
+ * (guest RAM starts cleared), so a fresh slot would otherwise carry garbage in
+ * cr3/pid/state; schedule() and process_map_mmio_all() trust those fields and
+ * a garbage cr3 made the boot-time MMIO mapping fault on real hardware. */
+static void clear_proc(struct process *p)
+{
+	unsigned char *b = (unsigned char *)p;
+	unsigned int i;
+
+	for (i = 0; i < sizeof(*p); i++)
+		b[i] = 0;
+}
+
 static void name_copy(char *dst, const char *src)
 {
 	unsigned int i;
@@ -82,7 +95,7 @@ static int grow_proc_table(void)
 	for (i = 0; i < proc_cap; i++)
 		np[i] = procs[i];
 	for (i = proc_cap; i < new_cap; i++)
-		np[i].state = PROC_UNUSED;
+		clear_proc(&np[i]);
 	kfree(procs);
 	procs = np;
 	proc_cap = new_cap;
@@ -118,7 +131,7 @@ void process_init(void)
 	}
 	proc_cap = INITIAL_PROC_CAP;
 	for (i = 0; i < proc_cap; i++)
-		procs[i].state = PROC_UNUSED;
+		clear_proc(&procs[i]);
 
 	procs[0].state      = PROC_RUNNING;
 	procs[0].pid        = 0;
@@ -281,14 +294,13 @@ int process_create_ptr(const char *name, void (*entry)(void *), void *arg)
 	procs[slot].kstack_top = top;
 	name_copy(procs[slot].name, name);
 	procs[slot].screen = procs[current_pid].screen;
-
 	irq_restore(flags);
 	return (int)procs[slot].pid;
 }
 
 /* Load an ELF image into a fresh address space and run it as a ring-3 process. */
 int process_create_user(const char *name, const void *image, unsigned int size,
-			const char *arg)
+			const char *arg, const char *cwd)
 {
 	int slot, owned;
 	unsigned int base, top, *sp, cr3, entry;
@@ -313,7 +325,7 @@ int process_create_user(const char *name, const void *image, unsigned int size,
 	if (!entry) {
 		mm_free_page(cr3);
 		irq_restore(flags);
-		return -1;
+		return -2;   /* not a loadable ELF */
 	}
 
 	/* Map the user stack just below USER_STACK_TOP.  Keep the top page's
@@ -392,6 +404,13 @@ int process_create_user(const char *name, const void *image, unsigned int size,
 	procs[slot].kstack_top = top;
 	name_copy(procs[slot].name, name);
 	procs[slot].screen = procs[current_pid].screen;
+	{
+		unsigned int k;
+
+		for (k = 0; cwd && cwd[k] && k + 1 < sizeof(procs[slot].cwd); k++)
+			procs[slot].cwd[k] = cwd[k];
+		procs[slot].cwd[k] = '\0';
+	}
 
 	irq_restore(flags);
 	return (int)procs[slot].pid;
@@ -410,6 +429,8 @@ void process_map_mmio_all(unsigned int phys_base, unsigned int size)
 
 	paging_map_mmio(phys_base, size);
 	for (i = 0; i < proc_cap; i++) {
+		if (procs[i].state == PROC_UNUSED || procs[i].state == PROC_DEAD)
+			continue;
 		if (procs[i].cr3 && procs[i].cr3 != kernel_cr3)
 			paging_map_mmio_cr3(procs[i].cr3, phys_base, size);
 	}

+ 5 - 2
process.h

@@ -18,6 +18,7 @@
 #define TIMESLICE_TICKS 20 /* context switch every 20 ms (PIT @ 1 kHz) */
 
 #define PROC_NAME_LEN 12
+#define PROC_CWD_MAX  64
 
 struct text_screen;  /* forward decl (see screen.h) */
 
@@ -31,6 +32,7 @@ struct process {
 	unsigned int cr3;            /* physical address of this process's page dir */
 	unsigned int kstack_top;     /* ring-0 stack top (for TSS esp0) */
 	unsigned int wake_ms;        /* tick deadline when PROC_SLEEPING */
+	char cwd[PROC_CWD_MAX];      /* working directory (ring-3 commands) */
 };
 
 /* User-mode virtual address layout (ring-3 processes). */
@@ -49,9 +51,10 @@ int  process_create_arg(const char *name, void (*entry)(const char *),
 int  process_create_ptr(const char *name, void (*entry)(void *), void *arg);
 /* Load an ELF image into a fresh address space and run it as a ring-3
  * process, with `arg` (may be NULL) as the first C parameter of _start.
- * Returns the pid, or -1 on error. */
+ * Returns the pid, -1 when out of resources, or -2 when the image is not a
+ * loadable ELF. */
 int process_create_user(const char *name, const void *image, unsigned int size,
-			const char *arg);
+			const char *arg, const char *cwd);
 void process_set_screen(struct text_screen *screen);
 
 /* Map an MMIO range into the kernel's page directory and every live process

+ 251 - 0
ramfs.c

@@ -0,0 +1,251 @@
+/* ramfs: see ramfs.h.  The pack format is produced by mkpack.c:
+ *
+ *   char magic[8] = "NBPAK1";  u32 count;  u32 total;
+ *   count x { char path[32]; u32 off; u32 size; }   (offsets from pack start)
+ *   file data, 4-byte aligned
+ */
+
+#include "ramfs.h"
+#include "mm.h"
+#include "serial.h"
+
+struct ramfs_hdr {
+	char magic[8];
+	unsigned int count;
+	unsigned int total;
+};
+
+struct ramfs_ent {
+	char path[RAMFS_PATH_MAX];
+	unsigned int off;
+	unsigned int size;
+};
+
+static const unsigned char *pack_base;
+static unsigned int pack_size;
+static const struct ramfs_hdr *pack_hdr;
+static unsigned int pack_pages;   /* heap pages, when not embedded */
+
+static int str_eq(const char *a, const char *b)
+{
+	unsigned int i = 0;
+
+	while (a[i] && b[i] && a[i] == b[i])
+		i++;
+	return a[i] == '\0' && b[i] == '\0';
+}
+
+static unsigned int first_len(const char *path)
+{
+	unsigned int n = 0;
+
+	while (path[n] && path[n] != '/')
+		n++;
+	return n;
+}
+
+/* Same first component ("bin/a" vs "bin/b" -> 1)? */
+static int first_component_same(const char *a, const char *b)
+{
+	unsigned int i, na = first_len(a), nb = first_len(b);
+
+	if (na != nb)
+		return 0;
+	for (i = 0; i < na; i++)
+		if (a[i] != b[i])
+			return 0;
+	return 1;
+}
+
+/* Skip a leading "dir/" prefix; NULL when the entry is not inside `dir`. */
+static const char *strip_dir(const char *path, const char *dir)
+{
+	unsigned int i = 0;
+
+	if (dir[0] == '\0')
+		return path;
+	while (dir[i] && path[i] == dir[i])
+		i++;
+	if (dir[i] != '\0')
+		return 0;
+	if (path[i] == '/')
+		return path + i + 1;
+	return 0;
+}
+
+/* Basic name check: no empties, no "." / "..". */
+static int path_ok(const char *path)
+{
+	unsigned int i = 0, len = 0;
+
+	if (!path || !path[0])
+		return 0;
+	for (;;) {
+		char c = path[i];
+		if (c == '\0' || c == '/') {
+			if (len == 0)
+				return 0;
+			if (len == 1 && path[i - 1] == '.')
+				return 0;
+			if (len == 2 && path[i - 1] == '.' && path[i - 2] == '.')
+				return 0;
+			len = 0;
+			if (c == '\0')
+				return 1;
+		} else {
+			len++;
+		}
+		i++;
+	}
+}
+
+/* Shape + bounds check of a pack; NULL when it is not a valid pack. */
+static const struct ramfs_hdr *ramfs_validate(const void *pack, unsigned int size)
+{
+	const struct ramfs_hdr *h = (const struct ramfs_hdr *)pack;
+	const struct ramfs_ent *ents;
+	unsigned int i, need;
+
+	if (size < sizeof(*h))
+		return 0;
+	if (h->magic[0] != 'N' || h->magic[1] != 'B' || h->magic[2] != 'P' ||
+	    h->magic[3] != 'A' || h->magic[4] != 'K' || h->magic[5] != '1')
+		return 0;
+	if (h->count == 0 || h->count > RAMFS_MAX_FILES)
+		return 0;
+	need = (unsigned int)(sizeof(*h) + h->count * sizeof(*ents));
+	if (need > size || h->total > size || h->total < need)
+		return 0;
+
+	ents = (const struct ramfs_ent *)((const unsigned char *)pack + sizeof(*h));
+	for (i = 0; i < h->count; i++) {
+		if (!path_ok(ents[i].path))
+			return 0;
+		if (ents[i].off > size || ents[i].size > size - ents[i].off)
+			return 0;
+	}
+	return h;
+}
+
+int ramfs_init(const void *pack, unsigned int size)
+{
+	const struct ramfs_hdr *h = ramfs_validate(pack, size);
+
+	if (!h)
+		return -1;
+	pack_base = (const unsigned char *)pack;
+	pack_size = size;
+	pack_hdr = h;
+	pack_pages = 0;
+	return 0;
+}
+
+int ramfs_replace(const void *pack, unsigned int size)
+{
+	const struct ramfs_hdr *h = ramfs_validate(pack, size);
+	const unsigned char *old_base = pack_base;
+	unsigned int old_size = pack_size, old_pages = pack_pages;
+	unsigned int i;
+
+	if (!h)
+		return -1;
+
+	pack_base = (const unsigned char *)pack;
+	pack_size = size;
+	pack_hdr = h;
+	pack_pages = (size + 0xFFFu) >> 12;
+
+	/* Free a previously received pack. */
+	for (i = 0; i < old_pages; i++)
+		mm_free_page((unsigned int)old_base + i * 0x1000);
+	(void)old_size;
+	return 0;
+}
+
+const void *ramfs_lookup(const char *path, unsigned int *size)
+{
+	const struct ramfs_ent *ents;
+	unsigned int i;
+
+	if (!pack_hdr || !path_ok(path))
+		return 0;
+	ents = (const struct ramfs_ent *)(pack_base + sizeof(*pack_hdr));
+	for (i = 0; i < pack_hdr->count; i++) {
+		if (str_eq(ents[i].path, path)) {
+			if (size)
+				*size = ents[i].size;
+			return pack_base + ents[i].off;
+		}
+	}
+	return 0;
+}
+
+int ramfs_list(const char *dir, unsigned int index, struct ramfs_dirent *out)
+{
+	const struct ramfs_ent *ents;
+	unsigned int i, seen = 0;
+
+	if (!pack_hdr)
+		return 0;
+	ents = (const struct ramfs_ent *)(pack_base + sizeof(*pack_hdr));
+
+	for (i = 0; i < pack_hdr->count; i++) {
+		const char *name;
+		unsigned int j, len;
+
+		if (dir[0] == '\0') {
+			/* Root: the unique first components, reported as dirs. */
+			unsigned int dup = 0;
+
+			for (j = 0; j < i; j++) {
+				if (first_component_same(ents[i].path, ents[j].path)) {
+					dup = 1;
+					break;
+				}
+			}
+			if (dup)
+				continue;
+			len = first_len(ents[i].path);
+			if (len >= RAMFS_PATH_MAX)
+				continue;
+			if (seen != index) {
+				seen++;
+				continue;
+			}
+			for (j = 0; j < len; j++)
+				out->name[j] = ents[i].path[j];
+			out->name[len] = '\0';
+			out->size = 0;
+			out->is_dir = 1;
+			return 1;
+		}
+
+		name = strip_dir(ents[i].path, dir);
+		if (!name)
+			continue;
+		if (seen != index) {
+			seen++;
+			continue;
+		}
+		len = 0;
+		while (name[len] && name[len] != '/' && len < RAMFS_PATH_MAX - 1)
+			len++;
+		for (j = 0; j < len; j++)
+			out->name[j] = name[j];
+		out->name[len] = '\0';
+		out->size = ents[i].size;
+		out->is_dir = (name[len] == '/') ? 1 : 0;
+		return 1;
+	}
+	return 0;
+}
+
+unsigned int ramfs_count(void)
+{
+	return pack_hdr ? pack_hdr->count : 0;
+}
+
+unsigned int ramfs_pack_size(void)
+{
+	return pack_hdr ? pack_size : 0;
+}

+ 38 - 0
ramfs.h

@@ -0,0 +1,38 @@
+/* ramfs: read-only in-memory filesystem backed by the command pack (mkpack).
+ * The pack is kept where the loader put it (no copies); paths inside it are
+ * relative to the mount root (e.g. "bin/ls"). */
+#ifndef RAMFS_H
+#define RAMFS_H
+
+#define RAMFS_PATH_MAX 32
+#define RAMFS_MAX_FILES 128
+
+struct ramfs_dirent {
+	char name[RAMFS_PATH_MAX];
+	unsigned int size;
+	unsigned int is_dir;
+};
+
+/* Validate a pack and make it the ramfs contents.  Returns 0 on success. */
+int ramfs_init(const void *pack, unsigned int size);
+
+/* Replace the contents with a freshly received pack (page-allocated): the
+ * old pack is validated first, so a bad image leaves the current one in
+ * place; on success the previous heap copy is freed.  Returns 0 on success. */
+int ramfs_replace(const void *pack, unsigned int size);
+
+/* Look up one path (no leading '/', e.g. "bin/ls"); returns a pointer into
+ * the pack and its size, or NULL when absent. */
+const void *ramfs_lookup(const char *path, unsigned int *size);
+
+/* List one directory: "" for the mount root, "bin" for /ramfs/bin.  Returns
+ * 1 when an entry was filled, 0 at the end. */
+int ramfs_list(const char *dir, unsigned int index, struct ramfs_dirent *out);
+
+/* Number of files in the pack (0 when no pack is loaded). */
+unsigned int ramfs_count(void);
+
+/* Size of the loaded pack in bytes (0 when none). */
+unsigned int ramfs_pack_size(void);
+
+#endif

+ 15 - 22
run.c

@@ -1,7 +1,6 @@
 /* run command: load raw binary from a FAT filesystem to 0x80000 and execute.
  * Usage: run /<device>/<file>. */
 
-#include "fat.h"
 #include "fbtext.h"
 #include "io.h"
 #include "run_api.h"
@@ -88,11 +87,10 @@ static int run_embedded(const char *name)
 
 void program_run_main(const char *filename)
 {
-	fat_fs_t *fs;
-	const char *fs_path;
-	unsigned int dir_cluster;
-	char name_8_3[12];
-	int n;
+	struct vfs_file f;
+	struct vfs_mount *m = 0;
+	unsigned int i;
+	int r;
 
 	if (!filename || !filename[0]) {
 		io_putstr("run: usage run <name>|/<device>/<file>\n");
@@ -103,30 +101,25 @@ void program_run_main(const char *filename)
 	if (run_embedded(filename))
 		return;
 
-	if (vfs_resolve(filename, &fs, &fs_path) != 0) {
-		io_putstr("run: unknown device or invalid path\n");
-		return;
-	}
-
-	n = fat_resolve_path(fs, fs_path, &dir_cluster, name_8_3);
-	if (n == -2) {
-		io_putstr("run: disk read error (see above)\n");
-		return;
-	}
-	if (n != 0) {
+	/* Anything else comes from a mounted filesystem (FAT or the ramfs). */
+	r = vfs_load_file(filename, &f, &m);
+	if (r == VFS_ENOENT) {
 		io_putstr("run: file not found\n");
 		return;
 	}
-
-	n = fat_read_file_in(fs, dir_cluster, name_8_3, (void *)PROGRAM_LOAD_ADDR, PROGRAM_LOAD_MAX);
-	if (n == -2) {
+	if (r != 0) {
 		io_putstr("run: disk read error (see above)\n");
 		return;
 	}
-	if (n <= 0) {
-		io_putstr("run: file not found\n");
+	if (f.size == 0 || f.size > PROGRAM_LOAD_MAX) {
+		io_putstr("run: file too large\n");
+		vfs_release_file(m, &f);
 		return;
 	}
+	for (i = 0; i < f.size; i++)
+		((unsigned char *)PROGRAM_LOAD_ADDR)[i] =
+			((const unsigned char *)f.data)[i];
+	vfs_release_file(m, &f);
 
 	io_drain_keyboard();
 	{

+ 1 - 1
switch.S

@@ -30,7 +30,7 @@
 .equ PCB_ESP,   0
 .equ PCB_CR3,   32
 .equ PCB_KSTACK_TOP, 36
-.equ PCB_SIZE,  44      /* sizeof(struct process): 4+4+4+12+4+4+4+4+4 = 44 */
+.equ PCB_SIZE,  108     /* sizeof(struct process): 44 + cwd[64] */
 
 .equ TIMESLICE, 20
 .equ FLUSH_INTERVAL, 33 /* 1000Hz PIT / 30Hz flush ≈ 33 ticks */

+ 94 - 146
sys_fs.c

@@ -1,20 +1,19 @@
 /* File service for ring-3 commands (see syscall.h for the ABI).
  *
- * The VFS/FAT layer and the disk drivers stay in the kernel; these calls
- * expose whole-file operations on absolute paths.  Two properties shape how
- * they run:
+ * Filesystem-agnostic: paths resolve through the VFS (vfs.c), so FAT volumes
+ * and the ramfs behave the same.  Two properties shape how these run:
  *   - the FAT driver keeps state in static buffers, so no other process may
- *     run its FAT code concurrently;
+ *     run its code concurrently;
  *   - the floppy driver waits for IRQ6, so interrupts must stay enabled.
  * So instead of a lock we set `sys_fs_busy` for the duration: the scheduler
- * (schedule() in process.c) will not switch away from the running process
- * while it is set.  That keeps each operation atomic (and immune to Ctrl+C
- * killing the process mid-write) at the cost of a brief system stall during
- * disk I/O.  Buffers point into the caller's address space, which is also the
- * active one during the syscall, so no copy in/out is needed. */
+ * (process.c) will not switch away from the running process while it is set.
+ * That keeps each operation atomic (and immune to Ctrl+C killing the process
+ * mid-write) at the cost of a brief stall during disk I/O.  Buffers point
+ * into the caller's address space, which is also the active one during the
+ * syscall, so no copy in/out is needed. */
 
 #include "syscall.h"
-#include "fat.h"
+#include "process.h"
 #include "vfs.h"
 
 volatile int sys_fs_busy;
@@ -31,84 +30,52 @@ void sys_fs_exit(void)
 	sys_fs_busy = 0;
 }
 
-static void copy_bounded(char *dst, const char *src, unsigned int size)
+static int sys_err(int vfs_err)
 {
-	unsigned int i = 0;
-
-	while (i + 1 < size && src[i]) {
-		dst[i] = src[i];
-		i++;
+	switch (vfs_err) {
+	case VFS_EIO:    return SYS_EIO;
+	case VFS_ENOENT: return SYS_ENOENT;
+	case VFS_EROFS:  return SYS_EROFS;
+	default:         return SYS_EINVAL;
 	}
-	dst[i] = '\0';
 }
 
-static const char *fat_type_name(fat_type_t t)
+/* Resolve an absolute path to (mount, relative path); returns 0 or a SYS_E*
+ * code (mirrors the old "no such device" vs "no filesystem" distinction). */
+static int fs_resolve(const char *path, struct vfs_mount **m,
+		      const char **rel)
 {
-	switch (t) {
-	case FAT12: return "FAT12";
-	case FAT16: return "FAT16";
-	case FAT32: return "FAT32";
-	default:    return "FAT";
-	}
-}
+	char abs[VFS_PATH_MAX * 2];
 
-/* Resolve `path` to (filesystem, parent directory cluster, final 8.3 name).
- * Returns 0 or a SYS_E* code. */
-static int fs_parent(const char *path, fat_fs_t **fs_out,
-		     unsigned int *dir_cluster, char name_8_3[12])
-{
-	fat_fs_t *fs;
-	const char *fs_path;
-	int r;
-
-	if (!path || !path[0])
+	if (!path)
 		return SYS_ENODEV;
-	if (vfs_resolve(path, &fs, &fs_path) != 0)
-		return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
-	r = fat_resolve_path(fs, fs_path, dir_cluster, name_8_3);
-	if (r == -2)
-		return SYS_EIO;
-	if (r != 0)
-		return SYS_ENOENT;
-	*fs_out = fs;
-	return 0;
-}
 
-/* Resolve `path` to (filesystem, directory cluster). */
-static int fs_dir(const char *path, fat_fs_t **fs_out, unsigned int *dir_cluster)
-{
-	fat_fs_t *fs;
-	const char *fs_path;
-	int r;
-
-	if (!path || !path[0])
-		return SYS_ENODEV;
-	if (vfs_resolve(path, &fs, &fs_path) != 0)
+	/* Relative paths resolve against the calling process's cwd (inherited
+	 * from the terminal that spawned it); "" means the cwd itself. */
+	if (path[0] != '/') {
+		if (vfs_join_path(procs[current_pid].cwd, path, abs, sizeof(abs)) != 0)
+			return SYS_EINVAL;
+		path = abs;
+	}
+	if (path[0] == '/' && path[1] == '\0')
+		path = "/";     /* the root listing goes through vfs_resolve below */
+	if (vfs_resolve(path, m, rel) != 0)
 		return vfs_device_known(path) ? SYS_ENOFS : SYS_ENODEV;
-	r = fat_resolve_dir(fs, fs_path, dir_cluster);
-	if (r == -2)
-		return SYS_EIO;
-	if (r != 0)
-		return SYS_EINVAL;      /* exists, but not a directory */
-	*fs_out = fs;
 	return 0;
 }
 
 int sys_read_file(const char *path, void *buf, unsigned int max)
 {
-	fat_fs_t *fs;
-	unsigned int dir;
-	char name[12];
+	struct vfs_mount *m;
+	const char *rel;
 	int r;
 
 	sys_fs_enter();
-	r = fs_parent(path, &fs, &dir, name);
+	r = fs_resolve(path, &m, &rel);
 	if (r == 0) {
-		r = fat_read_file_in(fs, dir, name, buf, max);
-		if (r == -2)
-			r = SYS_EIO;
-		else if (r < 0)
-			r = SYS_ENOENT;
+		r = m->ops->read_file(m, rel, buf, max);
+		if (r < 0)
+			r = sys_err(r);
 	}
 	sys_fs_exit();
 	return r;
@@ -116,19 +83,16 @@ int sys_read_file(const char *path, void *buf, unsigned int max)
 
 int sys_write_file(const char *path, const void *buf, unsigned int len)
 {
-	fat_fs_t *fs;
-	unsigned int dir;
-	char name[12];
+	struct vfs_mount *m;
+	const char *rel;
 	int r;
 
 	sys_fs_enter();
-	r = fs_parent(path, &fs, &dir, name);
+	r = fs_resolve(path, &m, &rel);
 	if (r == 0) {
-		r = fat_write_file_in(fs, dir, name, buf, len);
-		if (r == -2)
-			r = SYS_EIO;
-		else if (r != 0)
-			r = SYS_EWRITE;
+		r = m->ops->write_file(m, rel, buf, len);
+		if (r < 0)
+			r = sys_err(r);
 	}
 	sys_fs_exit();
 	return r;
@@ -136,19 +100,16 @@ int sys_write_file(const char *path, const void *buf, unsigned int len)
 
 int sys_unlink(const char *path)
 {
-	fat_fs_t *fs;
-	unsigned int dir;
-	char name[12];
+	struct vfs_mount *m;
+	const char *rel;
 	int r;
 
 	sys_fs_enter();
-	r = fs_parent(path, &fs, &dir, name);
+	r = fs_resolve(path, &m, &rel);
 	if (r == 0) {
-		r = fat_delete_file_in(fs, dir, name);
-		if (r == -2)
-			r = SYS_EIO;
-		else if (r != 0)
-			r = SYS_ENOENT;
+		r = m->ops->unlink(m, rel);
+		if (r < 0)
+			r = sys_err(r);
 	}
 	sys_fs_exit();
 	return r;
@@ -156,19 +117,16 @@ int sys_unlink(const char *path)
 
 int sys_mkdir(const char *path)
 {
-	fat_fs_t *fs;
-	unsigned int dir;
-	char name[12];
+	struct vfs_mount *m;
+	const char *rel;
 	int r;
 
 	sys_fs_enter();
-	r = fs_parent(path, &fs, &dir, name);
+	r = fs_resolve(path, &m, &rel);
 	if (r == 0) {
-		r = fat_mkdir(fs, dir, name);
-		if (r == -2)
-			r = SYS_EIO;
-		else if (r != 0)
-			r = SYS_EWRITE;
+		r = m->ops->mkdir(m, rel);
+		if (r < 0)
+			r = sys_err(r);
 	}
 	sys_fs_exit();
 	return r;
@@ -176,79 +134,69 @@ int sys_mkdir(const char *path)
 
 int sys_rmdir(const char *path)
 {
-	fat_fs_t *fs;
-	unsigned int dir;
-	char name[12];
+	struct vfs_mount *m;
+	const char *rel;
 	int r;
 
 	sys_fs_enter();
-	r = fs_parent(path, &fs, &dir, name);
+	r = fs_resolve(path, &m, &rel);
 	if (r == 0) {
-		r = fat_rmdir(fs, dir, name);
-		if (r == -2)
-			r = SYS_EIO;
-		else if (r != 0)
-			r = SYS_EWRITE;
+		r = m->ops->rmdir(m, rel);
+		if (r < 0)
+			r = sys_err(r);
 	}
 	sys_fs_exit();
 	return r;
 }
 
-/* Directory listing: index-based, so the kernel keeps no per-process state.
- * `fat_list_dir` walks from the start each call; directories are small. */
-static unsigned int list_index;
-static unsigned int list_seen;
-static struct sys_dirent *list_out;
-static int list_done;
-
-static void dirent_cb(const char *name, int is_dir, unsigned int size)
-{
-	unsigned int i;
-
-	if (list_done || list_seen < list_index) {
-		list_seen++;
-		return;
-	}
-	for (i = 0; i < 11; i++)
-		list_out->name[i] = name[i] ? name[i] : ' ';
-	list_out->name[11] = '\0';
-	list_out->size = size;
-	list_out->is_dir = (unsigned int)is_dir;
-	list_done = 1;
-	list_seen++;
-}
-
 int sys_list_dir(const char *path, unsigned int index, struct sys_dirent *out)
 {
-	fat_fs_t *fs;
-	unsigned int dir;
+	struct vfs_mount *m;
+	const char *rel;
 	int r;
 
 	sys_fs_enter();
-	r = fs_dir(path, &fs, &dir);
+	r = fs_resolve(path, &m, &rel);
 	if (r == 0) {
-		list_index = index;
-		list_seen = 0;
-		list_out = out;
-		list_done = 0;
-		r = fat_list_dir(fs, dir, dirent_cb);
-		if (r == -2)
-			r = SYS_EIO;
-		else
-			r = list_done ? 1 : 0;   /* 1 = entry returned, 0 = end */
+		struct vfs_dirent d;
+		unsigned int i;
+
+		r = m->ops->list_dir(m, rel, index, &d);
+		if (r == 1) {
+			for (i = 0; i < sizeof(out->name) - 1 && d.name[i]; i++)
+				out->name[i] = d.name[i];
+			out->name[i] = '\0';
+			out->size = d.size;
+			out->is_dir = d.is_dir;
+			r = 1;
+		} else if (r < 0) {
+			r = sys_err(r);
+		} else {
+			r = 0;
+		}
 	}
 	sys_fs_exit();
 	return r;
 }
 
+static void copy_bounded(char *dst, const char *src, unsigned int size)
+{
+	unsigned int i = 0;
+
+	while (i + 1 < size && src[i]) {
+		dst[i] = src[i];
+		i++;
+	}
+	dst[i] = '\0';
+}
+
 int sys_list_mounts(unsigned int index, struct sys_mount *out)
 {
 	if (index >= vfs_mount_count)
 		return 0;
 	copy_bounded(out->name, vfs_mounts[index].name, sizeof(out->name));
-	copy_bounded(out->type, fat_type_name(vfs_mounts[index].fs.type),
-		     sizeof(out->type));
-	out->total_sectors = vfs_mounts[index].fs.total_sectors;
-	copy_bounded(out->label, vfs_mounts[index].fs.label, sizeof(out->label));
+	copy_bounded(out->type, vfs_mounts[index].type, sizeof(out->type));
+	out->total_sectors = vfs_mounts[index].total_sectors;
+	copy_bounded(out->label, vfs_mounts[index].label, sizeof(out->label));
 	return 1;
 }

+ 12 - 0
syscall.c

@@ -123,6 +123,18 @@ int syscall_dispatch(int num, int a1, int a2, int a3)
 		return sys_net_arp((const unsigned char *)a1, (unsigned char *)a2);
 	case SYS_NET_CONFIGURED:
 		return sys_net_configured();
+	case SYS_GETCWD: {
+		const char *cwd = procs[current_pid].cwd;
+		char *out = (char *)a1;
+		unsigned int i = 0, max = (unsigned int)a2;
+
+		if (!cwd || max == 0)
+			return -1;
+		for (i = 0; cwd[i] && i + 1 < max; i++)
+			out[i] = cwd[i];
+		out[i] = '\0';
+		return 0;
+	}
 	default:
 		return -1;
 	}

+ 2 - 0
syscall.h

@@ -67,6 +67,7 @@
 #define SYS_NET_NEXT_HOP  45  /* (dst[4], hop[4]) */
 #define SYS_NET_ARP       46  /* (ip[4], mac[6]) -> 0/-1/-2 (net_arp_resolve) */
 #define SYS_NET_CONFIGURED 47 /* () -> 1 when an IP address is set */
+#define SYS_GETCWD       48  /* (char *buf, unsigned max) -> 0; the caller's cwd */
 
 struct sys_netinfo {
 	unsigned int present;
@@ -88,6 +89,7 @@ struct sys_netinfo {
 #define SYS_ENOENT  (-4)     /* file/directory not found */
 #define SYS_EINVAL  (-5)     /* path is not valid for this operation */
 #define SYS_EWRITE  (-6)     /* write refused (full, or FAT rejected it) */
+#define SYS_EROFS   (-7)     /* read-only filesystem (ramfs) */
 
 /* One directory entry, raw 8.3 name exactly as stored (space padded). */
 struct sys_dirent {

+ 382 - 368
terminal.c

@@ -9,8 +9,6 @@
 #include "terminal.h"
 #include "timer.h"
 
-extern unsigned char _binary_hello_elf_start[];
-extern unsigned char _binary_hello_elf_end[];
 extern void program_reboot_main(void);
 extern void program_poweroff_main(void);
 extern void program_run_main(const char *filename);
@@ -48,15 +46,11 @@ static void term_print_prompt(struct term *t, int first)
 enum cmd_kind {
 	CK_SPAWN = 0,        /* spawn, no arguments */
 	CK_SPAWN_ARG,        /* spawn with the raw remainder of the line */
-	CK_SPAWN_PATH,       /* whole remainder, resolved against the cwd */
-	CK_SPAWN_FIRST,      /* first token, resolved against the cwd */
-	CK_SPAWN_SAVE,       /* first token resolved, the rest kept as text */
 	CK_CUSTOM            /* handled by a cmd_*() handler below */
 };
 
 #define CMD_REQARG 0x01  /* the argument must be non-empty */
-#define CMD_OPTARG 0x02  /* an empty argument is allowed (CK_SPAWN_PATH) */
-#define CMD_USER   0x04  /* spawn an embedded ring-3 ELF (ucmds[]) */
+#define CMD_BUILTIN 0x04 /* handled inline, before the PATH lookup */
 
 struct kcmd {
 	const char *name;               /* dispatch token */
@@ -74,9 +68,7 @@ static void term_cd(struct term *t, const char *arg);
 static void term_spawn(struct term *t, const char *name, void (*entry)(void));
 static void term_spawn_arg(struct term *t, const char *name,
 			   void (*entry)(const char *), const char *arg);
-static void term_resolve_first(struct term *t, const char *args,
-			       char *out, unsigned int out_size);
-static void cmd_spawn_user(struct term *t, const struct kcmd *c, const char *arg);
+static int term_path_spawn(struct term *t, const char *word, const char *args);
 
 static int cmd_cd(struct term *t, const char *args);
 static int cmd_clear(struct term *t, const char *args);
@@ -84,8 +76,8 @@ static int cmd_clock(struct term *t, const char *args);
 static int cmd_colors(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_hello(struct term *t, const char *args);
 static int cmd_help(struct term *t, const char *args);
+static int cmd_path(struct term *t, const char *args);
 static int cmd_kexec(struct term *t, const char *args);
 static int cmd_kill(struct term *t, const char *args);
 static int cmd_poweroff(struct term *t, const char *args);
@@ -94,164 +86,35 @@ static int cmd_reboot(struct term *t, const char *args);
 static int cmd_run(struct term *t, const char *args);
 static int cmd_serial(struct term *t, const char *args);
 
-/* Table order defines the `help` layout (printed as two columns). */
+/* Table order defines the `help` layout (printed as two columns).  Ring-3
+ * commands are not listed here: they are files on the PATH (see cmd_path). */
 static const struct kcmd cmds[] = {
-	{ "cd",         "cd [path]",                     CK_CUSTOM, 0, 0, 0, 0, cmd_cd },
-	{ "ls",         "ls [path]",                     CK_SPAWN_PATH, CMD_OPTARG | CMD_USER, 0, 0, 0, 0 },
-	{ "cat",        "cat <path>",                    CK_SPAWN_FIRST, CMD_USER, 0, 0, 0, 0 },
-	{ "save",       "save <path> <text>",            CK_SPAWN_SAVE, CMD_USER, 0, 0, 0, 0 },
-	{ "rm",         "rm <path>",                     CK_SPAWN_FIRST, CMD_USER, 0, 0, 0, 0 },
-	{ "mkdir",      "mkdir <path>",                  CK_SPAWN_FIRST, CMD_USER, 0, 0, 0, 0 },
-	{ "rmdir",      "rmdir <path>",                  CK_SPAWN_FIRST, CMD_USER, 0, 0, 0, 0 },
-	{ "hello",      "hello",                         CK_CUSTOM, 0, 0, 0, 0, cmd_hello },
-	{ "reboot",     "reboot",                        CK_CUSTOM, 0, 0, 0, 0, cmd_reboot },
-	{ "poweroff",   "poweroff",                      CK_CUSTOM, 0, 0, 0, 0, cmd_poweroff },
+	{ "cd",         "cd [path]",                     CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_cd },
+	{ "path",       "path [dir[:dir...]]",           CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_path },
+	{ "help",       "help",                          CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_help },
+	{ "clear",      "clear",                         CK_CUSTOM, CMD_BUILTIN, 0, 0, 0, cmd_clear },
 	{ "run",        "run <name>|<file>",             CK_CUSTOM, 0, 0, 0, 0, cmd_run },
 	{ "space",      "space",                         CK_SPAWN, 0, 0, program_run_embedded_main, "space", 0 },
 	{ "prog",       "prog",                          CK_SPAWN, 0, 0, program_run_embedded_main, "prog", 0 },
+	{ "reboot",     "reboot",                        CK_CUSTOM, 0, 0, 0, 0, cmd_reboot },
+	{ "poweroff",   "poweroff",                      CK_CUSTOM, 0, 0, 0, 0, cmd_poweroff },
 	{ "clock",      "clock",                         CK_CUSTOM, 0, 0, 0, 0, cmd_clock },
 	{ "colors",     "colors",                        CK_CUSTOM, 0, 0, 0, 0, cmd_colors },
 	{ "display",    "display",                       CK_SPAWN, 0, program_display_main, 0, 0, 0 },
-	{ "cpuinfo",    "cpuinfo",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
 	{ "lsusb",      "lsusb",                         CK_SPAWN, 0, program_lsusb_main, 0, 0, 0 },
-	{ "mouseinfo",  "mouseinfo",                     CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
-	{ "storage",    "storage",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
-	{ "meminfo",    "meminfo",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
 	{ "format",     "format <device>",               CK_CUSTOM, 0, 0, 0, 0, cmd_format },
 	{ "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 },
-	{ "netinfo",    "netinfo",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
-	{ "netrecv",    "netrecv",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
-	{ "netsend",    "netsend",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
-	{ "netip",      "netip [a.b.c.d]",               CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
-	{ "ping",       "ping <host|a.b.c.d>",           CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
-	{ "netlog",     "netlog <text>",                 CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
-	{ "netconsole", "netconsole [on|off|host <ip>]", CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
-	{ "dhcp",       "dhcp",                          CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
-	{ "netregs",    "netregs",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
-	{ "netloop",    "netloop",                       CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
-	{ "netreset",   "netreset",                      CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
-	{ "pong",       "pong",                          CK_SPAWN, CMD_USER, 0, 0, 0, 0 },
 	{ "ps",         "ps",                            CK_CUSTOM, 0, 0, 0, 0, cmd_ps },
 	{ "kill",       "kill <pid>",                    CK_CUSTOM, 0, 0, 0, 0, cmd_kill },
 	{ "kexec",      "kexec",                         CK_CUSTOM, 0, 0, 0, 0, cmd_kexec },
 	{ "dbg",        "dbg",                           CK_CUSTOM, 0, 0, 0, 0, cmd_dbg },
 	{ "serial",     "serial test|recv",              CK_CUSTOM, 0, 0, 0, 0, cmd_serial },
-	{ "clear",      "clear",                         CK_CUSTOM, 0, 0, 0, 0, cmd_clear },
-	{ "curl",       "curl <url>",                    CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
-	{ "netdns",     "netdns [a.b.c.d]",              CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
-	{ "host",       "host <name>",                   CK_SPAWN_ARG, CMD_USER, 0, 0, 0, 0 },
-	{ "help",       "help",                          CK_CUSTOM, 0, 0, 0, 0, cmd_help },
 };
 
 #define NCMDS (sizeof(cmds) / sizeof(cmds[0]))
 
-/* Embedded ring-3 command binaries (CK_USER rows above).  Built from
- * ucmd_*.c by the Makefile; more can be added here as they move out of the
- * kernel. */
-extern unsigned char _binary_ucmd_cpuinfo_elf_start[];
-extern unsigned char _binary_ucmd_cpuinfo_elf_end[];
-extern unsigned char _binary_ucmd_ls_elf_start[];
-extern unsigned char _binary_ucmd_ls_elf_end[];
-extern unsigned char _binary_ucmd_cat_elf_start[];
-extern unsigned char _binary_ucmd_cat_elf_end[];
-extern unsigned char _binary_ucmd_save_elf_start[];
-extern unsigned char _binary_ucmd_save_elf_end[];
-extern unsigned char _binary_ucmd_rm_elf_start[];
-extern unsigned char _binary_ucmd_rm_elf_end[];
-extern unsigned char _binary_ucmd_mkdir_elf_start[];
-extern unsigned char _binary_ucmd_mkdir_elf_end[];
-extern unsigned char _binary_ucmd_rmdir_elf_start[];
-extern unsigned char _binary_ucmd_rmdir_elf_end[];
-extern unsigned char _binary_ucmd_meminfo_elf_start[];
-extern unsigned char _binary_ucmd_meminfo_elf_end[];
-extern unsigned char _binary_ucmd_mouseinfo_elf_start[];
-extern unsigned char _binary_ucmd_mouseinfo_elf_end[];
-extern unsigned char _binary_ucmd_storage_elf_start[];
-extern unsigned char _binary_ucmd_storage_elf_end[];
-extern unsigned char _binary_ucmd_netinfo_elf_start[];
-extern unsigned char _binary_ucmd_netinfo_elf_end[];
-extern unsigned char _binary_ucmd_netip_elf_start[];
-extern unsigned char _binary_ucmd_netip_elf_end[];
-extern unsigned char _binary_ucmd_netdns_elf_start[];
-extern unsigned char _binary_ucmd_netdns_elf_end[];
-extern unsigned char _binary_ucmd_host_elf_start[];
-extern unsigned char _binary_ucmd_host_elf_end[];
-extern unsigned char _binary_ucmd_dhcp_elf_start[];
-extern unsigned char _binary_ucmd_dhcp_elf_end[];
-extern unsigned char _binary_ucmd_netsend_elf_start[];
-extern unsigned char _binary_ucmd_netsend_elf_end[];
-extern unsigned char _binary_ucmd_netrecv_elf_start[];
-extern unsigned char _binary_ucmd_netrecv_elf_end[];
-extern unsigned char _binary_ucmd_ping_elf_start[];
-extern unsigned char _binary_ucmd_ping_elf_end[];
-extern unsigned char _binary_ucmd_pong_elf_start[];
-extern unsigned char _binary_ucmd_pong_elf_end[];
-extern unsigned char _binary_ucmd_netlog_elf_start[];
-extern unsigned char _binary_ucmd_netlog_elf_end[];
-extern unsigned char _binary_ucmd_netconsole_elf_start[];
-extern unsigned char _binary_ucmd_netconsole_elf_end[];
-extern unsigned char _binary_ucmd_netregs_elf_start[];
-extern unsigned char _binary_ucmd_netregs_elf_end[];
-extern unsigned char _binary_ucmd_netloop_elf_start[];
-extern unsigned char _binary_ucmd_netloop_elf_end[];
-extern unsigned char _binary_ucmd_netreset_elf_start[];
-extern unsigned char _binary_ucmd_netreset_elf_end[];
-extern unsigned char _binary_ucmd_curl_elf_start[];
-extern unsigned char _binary_ucmd_curl_elf_end[];
-
-struct ucmd_blob {
-	const char *name;
-	const unsigned char *start;
-	const unsigned char *end;
-};
-
-static const struct ucmd_blob ucmds[] = {
-	{ "cpuinfo", _binary_ucmd_cpuinfo_elf_start, _binary_ucmd_cpuinfo_elf_end },
-	{ "ls",      _binary_ucmd_ls_elf_start,      _binary_ucmd_ls_elf_end },
-	{ "cat",     _binary_ucmd_cat_elf_start,     _binary_ucmd_cat_elf_end },
-	{ "save",    _binary_ucmd_save_elf_start,    _binary_ucmd_save_elf_end },
-	{ "rm",      _binary_ucmd_rm_elf_start,      _binary_ucmd_rm_elf_end },
-	{ "mkdir",   _binary_ucmd_mkdir_elf_start,   _binary_ucmd_mkdir_elf_end },
-	{ "rmdir",   _binary_ucmd_rmdir_elf_start,   _binary_ucmd_rmdir_elf_end },
-	{ "meminfo", _binary_ucmd_meminfo_elf_start, _binary_ucmd_meminfo_elf_end },
-	{ "mouseinfo", _binary_ucmd_mouseinfo_elf_start, _binary_ucmd_mouseinfo_elf_end },
-	{ "storage", _binary_ucmd_storage_elf_start, _binary_ucmd_storage_elf_end },
-	{ "netinfo", _binary_ucmd_netinfo_elf_start, _binary_ucmd_netinfo_elf_end },
-	{ "netip",   _binary_ucmd_netip_elf_start,   _binary_ucmd_netip_elf_end },
-	{ "netdns",  _binary_ucmd_netdns_elf_start,  _binary_ucmd_netdns_elf_end },
-	{ "host",    _binary_ucmd_host_elf_start,    _binary_ucmd_host_elf_end },
-	{ "dhcp",    _binary_ucmd_dhcp_elf_start,    _binary_ucmd_dhcp_elf_end },
-	{ "netsend",      _binary_ucmd_netsend_elf_start,              _binary_ucmd_netsend_elf_end },
-	{ "netrecv",      _binary_ucmd_netrecv_elf_start,              _binary_ucmd_netrecv_elf_end },
-	{ "ping",         _binary_ucmd_ping_elf_start,              _binary_ucmd_ping_elf_end },
-	{ "pong",         _binary_ucmd_pong_elf_start,              _binary_ucmd_pong_elf_end },
-	{ "netlog",       _binary_ucmd_netlog_elf_start,              _binary_ucmd_netlog_elf_end },
-	{ "netconsole",   _binary_ucmd_netconsole_elf_start,              _binary_ucmd_netconsole_elf_end },
-	{ "netregs",      _binary_ucmd_netregs_elf_start,              _binary_ucmd_netregs_elf_end },
-	{ "netloop",      _binary_ucmd_netloop_elf_start,              _binary_ucmd_netloop_elf_end },
-	{ "netreset",     _binary_ucmd_netreset_elf_start,              _binary_ucmd_netreset_elf_end },
-	{ "curl",         _binary_ucmd_curl_elf_start,                 _binary_ucmd_curl_elf_end },
-};
-
-
-static const struct ucmd_blob *ucmd_lookup(const char *name)
-{
-	unsigned int i;
-
-	for (i = 0; i < sizeof(ucmds) / sizeof(ucmds[0]); i++) {
-		const char *n = ucmds[i].name;
-		unsigned int j = 0;
-
-		while (n[j] && n[j] == name[j])
-			j++;
-		if (n[j] == '\0' && name[j] == '\0')
-			return &ucmds[i];
-	}
-	return 0;
-}
-
 /* Find the registry entry whose name matches the first word of the line. */
 static const struct kcmd *cmd_lookup(const char *line, unsigned int line_len)
 {
@@ -283,39 +146,32 @@ static void cmd_usage(const struct kcmd *c)
 	io_putchar('\n');
 }
 
-/* Copy the first token of `args` into `out` (0 when there is none). */
-static int cmd_first_token(const char *args, char *out, unsigned int out_size)
-{
-	unsigned int n = 0;
-
-	while (*args && *args != ' ') {
-		if (n + 1 >= out_size)
-			break;
-		out[n++] = *args++;
-	}
-	out[n] = '\0';
-	return n > 0;
-}
-
-/* Dispatch one registry entry.  Returns 1 when the handler already printed the
- * next prompt (clear), 0 otherwise. */
-static int term_run_command(struct term *t, const struct kcmd *c)
+/* Split the line into the command word and the trimmed remainder. */
+static void term_split_line(struct term *t, char *word, unsigned int wsize,
+			    char *args, unsigned int asize)
 {
-	char args[LINE_MAX + 1];
 	unsigned int i = 0, n = 0;
 
-	/* Skip the command word, capture the trimmed argument text. */
 	while (i < t->line_len && t->line[i] == ' ')
 		i++;
-	while (i < t->line_len && t->line[i] != ' ')
-		i++;
+	while (i < t->line_len && t->line[i] != ' ' && n + 1 < wsize)
+		word[n++] = t->line[i++];
+	word[n] = '\0';
 	while (i < t->line_len && t->line[i] == ' ')
 		i++;
-	while (i < t->line_len && n < LINE_MAX)
+	n = 0;
+	while (i < t->line_len && n + 1 < asize)
 		args[n++] = t->line[i++];
 	while (n > 0 && args[n - 1] == ' ')
 		n--;
 	args[n] = '\0';
+}
+
+/* Dispatch one registry entry.  Returns 1 when the handler already printed the
+ * next prompt (clear), 0 otherwise. */
+static int term_run_command(struct term *t, const struct kcmd *c,
+			    const char *args)
+{
 
 	if ((c->flags & CMD_REQARG) && args[0] == '\0') {
 		cmd_usage(c);
@@ -324,66 +180,14 @@ static int term_run_command(struct term *t, const struct kcmd *c)
 
 	switch (c->kind) {
 	case CK_SPAWN:
-		if (c->flags & CMD_USER)
-			cmd_spawn_user(t, c, c->fixed);
-		else if (c->fixed)
+		if (c->fixed)
 			term_spawn_arg(t, c->name, c->fn_arg, c->fixed);
 		else
 			term_spawn(t, c->name, c->fn);
 		break;
 	case CK_SPAWN_ARG:
-		if (c->flags & CMD_USER)
-			cmd_spawn_user(t, c, args);
-		else
-			term_spawn_arg(t, c->name, c->fn_arg, args);
-		break;
-	case CK_SPAWN_PATH: {
-		char resolved[VFS_PATH_MAX];
-
-		if (vfs_join_path(t->cwd, args, resolved, sizeof(resolved)) != 0) {
-			io_putstr(c->name);
-			io_putstr(": path too long\n");
-			break;
-		}
-		if (c->flags & CMD_USER)
-			cmd_spawn_user(t, c, resolved);
-		else
-			term_spawn_arg(t, c->name, c->fn_arg, resolved);
-		break;
-	}
-	case CK_SPAWN_FIRST: {
-		char tok[VFS_PATH_MAX];
-		char resolved[VFS_PATH_MAX];
-
-		if (!cmd_first_token(args, tok, sizeof(tok))) {
-			cmd_usage(c);
-			break;
-		}
-		if (vfs_join_path(t->cwd, tok, resolved, sizeof(resolved)) != 0) {
-			io_putstr(c->name);
-			io_putstr(": path too long\n");
-			break;
-		}
-		if (c->flags & CMD_USER)
-			cmd_spawn_user(t, c, resolved);
-		else
-			term_spawn_arg(t, c->name, c->fn_arg, resolved);
+		term_spawn_arg(t, c->name, c->fn_arg, args);
 		break;
-	}
-	case CK_SPAWN_SAVE: {
-		char resolved[LINE_MAX + VFS_PATH_MAX];
-
-		if (args[0] == '\0') {
-			cmd_usage(c);
-			break;
-		}
-		term_resolve_first(t, args, resolved, sizeof(resolved));
-		if (c->flags & CMD_USER)
-			cmd_spawn_user(t, c, resolved);
-		else
-			term_spawn_arg(t, c->name, c->fn_arg, resolved);
-		break;
-	}
 	case CK_CUSTOM:
 		return c->custom(t, args);
 	}
@@ -484,25 +288,142 @@ static int cmd_format(struct term *t, const char *args)
 	return 0;
 }
 
-static int cmd_hello(struct term *t, const char *args)
+/* `path` builtin: show or set the command search path. */
+static int path_entry_ok(const char *entry)
 {
-	int pid;
+	unsigned int size = 0;
+	int is_dir = 0;
 
-	(void)args;
-	pid = process_create_user("hello", _binary_hello_elf_start,
-		(unsigned int)(_binary_hello_elf_end - _binary_hello_elf_start), 0);
-	if (pid < 0)
-		io_putstr("error: too many processes\n");
-	else
-		t->fg_pid = pid;
+	if (entry[0] != '/')
+		return 0;
+	return vfs_stat(entry, &size, &is_dir) == 0 && is_dir;
+}
+
+static int cmd_path(struct term *t, const char *args)
+{
+	if (args[0] == '\0') {
+		io_putstr("path=");
+		io_putstr(t->path);
+		io_putchar('\n');
+		return 0;
+	}
+
+	/* Validate every colon-separated entry (absolute, existing dir) and
+	 * rebuild the path without stray spaces. */
+	{
+		char entry[VFS_PATH_MAX];
+		char normalized[VFS_PATH_MAX];
+		unsigned int i = 0, n = 0, o = 0;
+
+		for (;;) {
+			char c = args[i];
+
+			if (c == ':' || c == '\0') {
+				entry[n] = '\0';
+				if (n > 0) {
+					unsigned int k;
+
+					if (!path_entry_ok(entry)) {
+						io_putstr("path: not a directory: ");
+						io_putstr(entry);
+						io_putchar('\n');
+						return 0;
+					}
+					if (o > 0) {
+						if (o + 1 >= sizeof(normalized)) {
+							io_putstr("path: too long\n");
+							return 0;
+						}
+						normalized[o++] = ':';
+					}
+					for (k = 0; entry[k]; k++) {
+						if (o + 1 >= sizeof(normalized)) {
+							io_putstr("path: too long\n");
+							return 0;
+						}
+						normalized[o++] = entry[k];
+					}
+				}
+				n = 0;
+				if (c == '\0')
+					break;
+				i++;
+				continue;
+			}
+			if (!(c == ' ' && n == 0)) {
+				if (n + 1 >= sizeof(entry)) {
+					io_putstr("path: entry too long\n");
+					return 0;
+				}
+				entry[n++] = c;
+			}
+			i++;
+		}
+		if (o == 0) {
+			io_putstr("path: empty\n");
+			return 0;
+		}
+		normalized[o] = '\0';
+		for (i = 0; i + 1 < sizeof(t->path) && normalized[i]; i++)
+			t->path[i] = normalized[i];
+		t->path[i] = '\0';
+	}
+	io_putstr("path set to ");
+	io_putstr(t->path);
+	io_putchar('\n');
 	return 0;
 }
 
+/* List the command files found on the search path. */
+static void help_print_path(struct term *t)
+{
+	const char *p = t->path;
+
+	while (*p) {
+		char dir[VFS_PATH_MAX];
+		unsigned int n = 0, col = 0, i;
+
+		while (*p && *p != ':' && n + 1 < sizeof(dir))
+			dir[n++] = *p++;
+		dir[n] = '\0';
+		if (*p == ':')
+			p++;
+		if (dir[0] == '\0')
+			continue;
+
+		io_putstr(dir);
+		io_putstr(":\n");
+		for (i = 0; ; i++) {
+			struct vfs_dirent d;
+			unsigned int len = 0;
+			int r = vfs_list_dir(dir, i, &d);
+
+			if (r != 1)
+				break;
+			if (d.is_dir)
+				continue;
+			while (d.name[len])
+				len++;
+			if (col > 0 && col + 2 + len > t->screen.cols) {
+				io_putchar('\n');
+				col = 0;
+			}
+			if (col > 0) {
+				io_putstr("  ");
+				col += 2;
+			}
+			io_putstr(d.name);
+			col += len;
+		}
+		io_putchar('\n');
+	}
+}
+
 static int cmd_help(struct term *t, const char *args)
 {
-	(void)t;
 	(void)args;
 	help_print();
+	help_print_path(t);
 	return 0;
 }
 
@@ -685,11 +606,26 @@ static unsigned int tab_count;
 static unsigned int tab_lcp_len;
 static char tab_lcp[16];
 static int tab_single_dir;
-static fat_fs_t *tab_fs;
-static unsigned int tab_dir_cluster;
+static int tab_raw_83;                      /* current dir holds raw 8.3 names */
+static char tab_scan_path[VFS_PATH_MAX];    /* dir of the last tab_scan_dir() */
 static unsigned int tab_col;
 static unsigned int tab_width;
 
+/* Display form of a name: FAT's raw 8.3 gets "NAME    EXT" -> "name.ext",
+ * everything else (ramfs) is just lowercased. */
+static void tab_disp_name(const char *name, char *out)
+{
+	unsigned int i;
+
+	if (tab_raw_83) {
+		tab_display_name(name, out);
+		return;
+	}
+	for (i = 0; name[i] && i < 15; i++)
+		out[i] = lower_ch(name[i]);
+	out[i] = '\0';
+}
+
 static void tab_collect_cb(const char *name, int is_dir, unsigned int size)
 {
 	char disp[16];
@@ -698,7 +634,7 @@ static void tab_collect_cb(const char *name, int is_dir, unsigned int size)
 	(void)size;
 	if (name[0] == '.')                 /* "." / ".." */
 		return;
-	tab_display_name(name, disp);
+	tab_disp_name(name, disp);
 	if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
 		return;
 	n = 0;
@@ -746,7 +682,7 @@ static void tab_print_cb(const char *name, int is_dir, unsigned int size)
 	(void)size;
 	if (name[0] == '.')
 		return;
-	tab_display_name(name, disp);
+	tab_disp_name(name, disp);
 	if (!prefix_ci(disp, tab_prefix, tab_prefix_len))
 		return;
 	tab_print_one(disp, is_dir);
@@ -754,17 +690,30 @@ static void tab_print_cb(const char *name, int is_dir, unsigned int size)
 
 static int tab_scan_dir(const char *abs)
 {
-	fat_fs_t *fs;
-	const char *fs_path;
+	struct vfs_mount *m;
+	const char *rel;
+	unsigned int i;
 
-	if (vfs_resolve(abs, &fs, &fs_path) != 0)
+	if (vfs_resolve(abs, &m, &rel) != 0)
 		return 0;
-	if (fat_resolve_dir(fs, fs_path, &tab_dir_cluster) != 0)
-		return 0;
-	tab_fs = fs;
+	tab_raw_83 = m->ops->raw_83;
+	{
+		unsigned int j;
+
+		for (j = 0; abs[j] && j + 1 < sizeof(tab_scan_path); j++)
+			tab_scan_path[j] = abs[j];
+		tab_scan_path[j] = '\0';
+	}
 	tab_count = 0;
 	tab_lcp_len = 0;
-	fat_list_dir(fs, tab_dir_cluster, tab_collect_cb);
+	for (i = 0; ; i++) {
+		struct vfs_dirent d;
+		int r = m->ops->list_dir(m, rel, i, &d);
+
+		if (r != 1)
+			break;
+		tab_collect_cb(d.name, (int)d.is_dir, d.size);
+	}
 	return (int)tab_count;
 }
 
@@ -834,13 +783,110 @@ static void term_tab_ambiguous(struct term *t, int mounts)
 				tab_print_one(name, 1);
 		}
 	} else {
-		fat_list_dir(tab_fs, tab_dir_cluster, tab_print_cb);
+		for (i = 0; ; i++) {
+			struct vfs_dirent d;
+			int r = vfs_list_dir(tab_scan_path, i, &d);
+
+			if (r != 1)
+				break;
+			tab_print_cb(d.name, (int)d.is_dir, d.size);
+		}
 	}
 	io_putchar('\n');
 	term_print_prompt(t, 1);
 	redraw_line_clear(t->line, t->line_len, t->line_pos, t->line_start, t->line_len);
 }
 
+/* Command-name completion state: fed from the registry rows and the PATH. */
+struct compl_state {
+	const char *token;
+	unsigned int tl;
+	unsigned int count;
+	unsigned int lcp_len;
+	char lcp[16];
+	char best[16];
+};
+
+static void compl_add(struct compl_state *st, const char *name)
+{
+	unsigned int nl = 0, j;
+
+	while (name[nl])
+		nl++;
+	if (nl < st->tl || !prefix_ci(name, st->token, st->tl))
+		return;
+	if (st->count == 0) {
+		for (j = 0; j < nl && j < sizeof(st->lcp) - 1; j++)
+			st->lcp[j] = name[j];
+		st->lcp[j] = '\0';
+		st->lcp_len = j;
+		for (j = 0; j < nl && j < sizeof(st->best) - 1; j++)
+			st->best[j] = name[j];
+		st->best[j] = '\0';
+	} else {
+		for (j = 0; j < st->lcp_len && j < nl && st->lcp[j] == name[j]; j++)
+			;
+		st->lcp_len = j;
+	}
+	st->count++;
+}
+
+static void compl_print_one(const char *name, unsigned int tl, const char *token)
+{
+	char buf[16];
+	unsigned int nl = 0, j;
+
+	while (name[nl])
+		nl++;
+	if (nl < tl || !prefix_ci(name, token, tl))
+		return;
+	for (j = 0; j < nl && j < 15; j++)
+		buf[j] = name[j];
+	buf[j] = '\0';
+	tab_print_one(buf, 0);
+}
+
+static void compl_consider(struct compl_state *st, const char *name, int print)
+{
+	if (print)
+		compl_print_one(name, st->tl, st->token);
+	else
+		compl_add(st, name);
+}
+
+/* Visit every command-name candidate: registry rows + files on the PATH. */
+static void compl_visit(struct term *t, struct compl_state *st, int print)
+{
+	const char *p;
+	unsigned int i;
+
+	for (i = 0; i < NCMDS; i++)
+		compl_consider(st, cmds[i].name, print);
+
+	p = t->path;
+	while (*p) {
+		char dir[VFS_PATH_MAX];
+		unsigned int n = 0;
+
+		while (*p && *p != ':' && n + 1 < sizeof(dir))
+			dir[n++] = *p++;
+		dir[n] = '\0';
+		if (*p == ':')
+			p++;
+		if (dir[0] == '\0')
+			continue;
+		for (i = 0; ; i++) {
+			struct vfs_dirent d;
+			int r = vfs_list_dir(dir, i, &d);
+
+			if (r != 1)
+				break;
+			if (!d.is_dir)
+				compl_consider(st, d.name, print);
+		}
+	}
+}
+
 /* Complete the token at the cursor: command name in the first token, a VFS
  * path afterwards.  One match completes, several extend to the common
  * prefix or are listed below the line. */
@@ -849,7 +895,7 @@ static void term_complete(struct term *t)
 	char token[LINE_MAX + 1];
 	char ins[LINE_MAX + 1];
 	char dir[VFS_PATH_MAX];
-	unsigned int start = t->line_pos, end, tl, i, j;
+	unsigned int start = t->line_pos, end, tl, i;
 
 	while (start > 0 && t->line[start - 1] != ' ')
 		start--;
@@ -870,70 +916,42 @@ static void term_complete(struct term *t)
 				break;
 			}
 		if (first) {
-			unsigned int nitems = NCMDS;
-			unsigned int count = 0, lcp_len = 0, best = 0;
-			char lcp[16];
-
-			for (i = 0; i < nitems; i++) {
-				const char *n = cmds[i].name;
-				unsigned int nl = 0;
-
-				while (n[nl])
-					nl++;
-				if (nl < tl || !prefix_ci(n, token, tl))
-					continue;
-				if (count == 0) {
-					for (j = 0; j < nl && j < sizeof(lcp) - 1; j++)
-						lcp[j] = n[j];
-					lcp_len = j;
-					best = i;
-				} else {
-					for (j = 0; j < lcp_len && j < nl &&
-						     lcp[j] == n[j]; j++)
-						;
-					lcp_len = j;
-				}
-				count++;
-			}
-			if (count == 0)
+			struct compl_state st;
+			unsigned int j;
+
+			st.token = token;
+			st.tl = tl;
+			st.count = 0;
+			st.lcp_len = 0;
+			st.lcp[0] = '\0';
+			st.best[0] = '\0';
+			compl_visit(t, &st, 0);
+
+			if (st.count == 0)
 				return;
-			if (count == 1) {
-				const char *n = cmds[best].name;
+			if (st.count == 1) {
 				unsigned int nl = 0;
 
-				while (n[nl])
+				while (st.best[nl])
 					nl++;
 				for (j = 0; j < nl && j < LINE_MAX - 1; j++)
-					ins[j] = n[j];
+					ins[j] = st.best[j];
 				ins[j] = ' ';
 				ins[j + 1] = '\0';
 				term_replace(t, start, end, ins);
 				return;
 			}
-			if (lcp_len > tl) {
-				for (j = 0; j < lcp_len; j++)
-					ins[j] = lcp[j];
-				ins[lcp_len] = '\0';
+			if (st.lcp_len > tl) {
+				for (j = 0; j < st.lcp_len; j++)
+					ins[j] = st.lcp[j];
+				ins[st.lcp_len] = '\0';
 				term_replace(t, start, end, ins);
 				return;
 			}
 			io_putchar('\n');
 			tab_col = 0;
 			tab_width = t->screen.cols;
-			for (i = 0; i < nitems; i++) {
-				const char *n = cmds[i].name;
-				unsigned int nl = 0;
-				char name[16];
-
-				while (n[nl])
-					nl++;
-				if (nl < tl || !prefix_ci(n, token, tl))
-					continue;
-				for (j = 0; j < nl && j < 15; j++)
-					name[j] = n[j];
-				name[j] = '\0';
-				tab_print_one(name, 0);
-			}
+			compl_visit(t, &st, 1);
 			io_putchar('\n');
 			term_print_prompt(t, 1);
 			redraw_line_clear(t->line, t->line_len, t->line_pos,
@@ -941,7 +959,7 @@ static void term_complete(struct term *t)
 			return;
 		}
 	}
-
+	
 	/* Argument token -> VFS path. */
 	{
 		char dirpart[LINE_MAX + 1];
@@ -1051,9 +1069,8 @@ static void history_push(struct term *t, const char *line, unsigned int line_len
 static void term_cd(struct term *t, const char *arg)
 {
 	char abs[VFS_PATH_MAX];
-	fat_fs_t *fs;
-	const char *fs_path;
-	unsigned int dir;
+	unsigned int size = 0;
+	int is_dir = 0;
 	int r;
 	unsigned int i;
 
@@ -1074,16 +1091,12 @@ static void term_cd(struct term *t, const char *arg)
 		return;
 	}
 
-	if (vfs_resolve(abs, &fs, &fs_path) != 0) {
-		io_putstr("cd: no such directory\n");
-		return;
-	}
-	r = fat_resolve_dir(fs, fs_path, &dir);
-	if (r == -2) {
+	r = vfs_stat(abs, &size, &is_dir);
+	if (r == VFS_EIO) {
 		io_putstr("cd: disk error\n");
 		return;
 	}
-	if (r != 0) {
+	if (r != 0 || !is_dir) {
 		io_putstr("cd: no such directory\n");
 		return;
 	}
@@ -1093,42 +1106,6 @@ static void term_cd(struct term *t, const char *arg)
 	t->cwd[i] = '\0';
 }
 
-/* Resolve the first token (path) of `args` against t->cwd, rebuilding
- * "abs-path rest-of-args" into out. Used by `save`. */
-static void term_resolve_first(struct term *t, const char *args,
-			       char *out, unsigned int out_size)
-{
-	char path[VFS_PATH_MAX];
-	char abs[VFS_PATH_MAX];
-	unsigned int i = 0, j = 0;
-
-	while (args[i] == ' ')
-		i++;
-	while (args[i] && args[i] != ' ' && j < sizeof(path) - 1)
-		path[j++] = args[i++];
-	path[j] = '\0';
-
-	if (vfs_join_path(t->cwd, path, abs, sizeof(abs)) != 0) {
-		abs[0] = '/';
-		abs[1] = '\0';
-	}
-	while (args[i] == ' ')
-		i++;
-
-	j = 0;
-	{
-		unsigned int k = 0;
-		while (abs[k] && j + 1 < out_size)
-			out[j++] = abs[k++];
-	}
-	if (args[i]) {
-		if (j + 1 < out_size)
-			out[j++] = ' ';
-		while (args[i] && j + 1 < out_size)
-			out[j++] = args[i++];
-	}
-	out[j] = '\0';
-}
 
 /* Spawn a foreground command process; the caller (term_run's wait loop)
  * waits for it to finish and handles Ctrl+C. */
@@ -1151,23 +1128,47 @@ static void term_spawn_arg(struct term *t, const char *name,
 		t->fg_pid = pid;
 }
 
-/* Spawn an embedded ring-3 command (CMD_USER rows); `arg` may be NULL. */
-static void cmd_spawn_user(struct term *t, const struct kcmd *c, const char *arg)
+/* PATH search: try each directory in t->path for `word`; the first hit is
+ * loaded and spawned as a ring-3 process.  Returns 1 when the command was
+ * found (spawned, or reported as bad), 0 when no directory has it. */
+static int term_path_spawn(struct term *t, const char *word, const char *args)
 {
-	const struct ucmd_blob *b = ucmd_lookup(c->name);
-	int pid;
+	const char *p = t->path;
+
+	while (*p) {
+		char dir[VFS_PATH_MAX];
+		char full[VFS_PATH_MAX + LINE_MAX];
+		struct vfs_file f;
+		struct vfs_mount *m = 0;
+		unsigned int n = 0;
+		int r, pid;
+
+		while (*p && *p != ':' && n + 1 < sizeof(dir))
+			dir[n++] = *p++;
+		dir[n] = '\0';
+		if (*p == ':')
+			p++;
+		if (dir[0] == '\0')
+			continue;
+		if (vfs_join_path(dir, word, full, sizeof(full)) != 0)
+			continue;
+		r = vfs_load_file(full, &f, &m);
+		if (r != 0)
+			continue;
 
-	if (!b) {
-		io_putstr(c->name);
-		io_putstr(": no embedded image\n");
-		return;
+		pid = process_create_user(word, f.data, f.size, args, t->cwd);
+		vfs_release_file(m, &f);
+		if (pid == -2) {
+			io_putstr(word);
+			io_putstr(": not an executable\n");
+		} else if (pid < 0) {
+			io_putstr("error: too many processes\n");
+		} else {
+			t->fg_pid = pid;
+		}
+		return 1;
 	}
-	pid = process_create_user(c->name, b->start,
-		(unsigned int)(b->end - b->start), arg);
-	if (pid < 0)
-		io_putstr("error: too many processes\n");
-	else
-		t->fg_pid = pid;
+	return 0;
 }
 
 void term_run(struct term *t)
@@ -1177,6 +1178,14 @@ void term_run(struct term *t)
 	t->line[0]          = '\0';
 	t->history_browse   = 0;
 	t->fg_pid           = -1;
+	if (t->path[0] == '\0') {
+		const char *def = "/ramfs/bin";
+		unsigned int i;
+
+		for (i = 0; def[i] && i + 1 < sizeof(t->path); i++)
+			t->path[i] = def[i];
+		t->path[i] = '\0';
+	}
 
 	io_clear_screen();
 	term_print_prompt(t, 1);
@@ -1260,18 +1269,23 @@ void term_run(struct term *t)
 					history_push(t, t->line, t->line_len);
 					t->history_browse = 0;
 					{
-						const struct kcmd *c =
-							cmd_lookup(t->line, t->line_len);
-
-						if (c) {
-							skip_prompt = term_run_command(t, c);
-						} else {
-							unsigned int si = 0;
-
-							while (si < t->line_len && t->line[si] == ' ')
-								si++;
-							if (si < t->line_len)
-								io_putstr("command not found\n");
+						char word[LINE_MAX + 1];
+						char args[LINE_MAX + 1];
+						const struct kcmd *c;
+
+						term_split_line(t, word, sizeof(word),
+								args, sizeof(args));
+						c = cmd_lookup(t->line, t->line_len);
+
+						if (c && (c->flags & CMD_BUILTIN)) {
+							skip_prompt = term_run_command(t, c, args);
+						} else if (word[0] != '\0' &&
+							   term_path_spawn(t, word, args)) {
+							/* spawned from the search path */
+						} else if (c) {
+							skip_prompt = term_run_command(t, c, args);
+						} else if (word[0] != '\0') {
+							io_putstr("command not found\n");
 						}
 					}
 					t->line_len = 0;

+ 1 - 0
terminal.h

@@ -22,6 +22,7 @@ struct term {
 	unsigned int history_browse;
 	int fg_pid;                 /* foreground child pid, or -1 when none */
 	char cwd[VFS_PATH_MAX];     /* absolute current working directory */
+	char path[VFS_PATH_MAX];    /* command search path (colon separated) */
 	/* Per-terminal key queue: WM (slot 0) pushes, terminal process pops. */
 	int keys[KEY_BUF_SIZE];
 	unsigned int key_head;

+ 5 - 0
ucmd_lib.h

@@ -147,6 +147,11 @@ static inline void u_put_mac(const unsigned char mac[6])
 	}
 }
 
+static inline int u_getcwd(char *buf, unsigned int max)
+{
+	return u_syscall2(SYS_GETCWD, (int)buf, (int)max);
+}
+
 static inline unsigned int u_ticks_ms(void)
 {
 	return (unsigned int)u_syscall0(SYS_GET_TICKS_MS);

+ 12 - 0
ucmd_ls.c

@@ -3,6 +3,8 @@
 
 #include "ucmd_lib.h"
 
+#define VFS_PATH_MAX 64
+
 #define BUF_SIZE 12
 
 static void put_size_sectors(unsigned int sectors)
@@ -62,6 +64,16 @@ static void print_entry(const struct sys_dirent *d)
 
 void _start(const char *path)
 {
+	char cwd[VFS_PATH_MAX];
+
+	/* The shell passes arguments verbatim; an empty one means the current
+	 * working directory (and "/" is the mount listing). */
+	if (!path || !path[0]) {
+		if (u_getcwd(cwd, sizeof(cwd)) != 0)
+			cwd[0] = '\0';
+		path = cwd;
+	}
+
 	if (!path || !path[0] || (path[0] == '/' && path[1] == '\0')) {
 		struct sys_mount m;
 		unsigned int i = 0;

+ 4 - 0
ucmd_mkdir.c

@@ -24,6 +24,10 @@ void _start(const char *path)
 		u_puts("mkdir: invalid path\n");
 		u_exit(1);
 	}
+	if (r == SYS_EROFS) {
+		u_puts("mkdir: read-only filesystem\n");
+		u_exit(1);
+	}
 	if (r != 0)
 		u_exit(1);   /* the FAT layer printed its own reason */
 	u_puts("mkdir ok\n");

+ 4 - 0
ucmd_rm.c

@@ -24,6 +24,10 @@ void _start(const char *path)
 		u_puts("rm: not found\n");
 		u_exit(1);
 	}
+	if (r == SYS_EROFS) {
+		u_puts("rm: read-only filesystem\n");
+		u_exit(1);
+	}
 	if (r != 0) {
 		u_puts("rm: file not found\n");
 		u_exit(1);

+ 4 - 0
ucmd_rmdir.c

@@ -24,6 +24,10 @@ void _start(const char *path)
 		u_puts("rmdir: invalid path\n");
 		u_exit(1);
 	}
+	if (r == SYS_EROFS) {
+		u_puts("rmdir: read-only filesystem\n");
+		u_exit(1);
+	}
 	if (r != 0)
 		u_exit(1);   /* the FAT layer printed its own reason */
 	u_puts("rmdir ok\n");

+ 4 - 0
ucmd_save.c

@@ -43,6 +43,10 @@ void _start(const char *arg)
 		u_puts("save: unknown device or invalid path\n");
 		u_exit(1);
 	}
+	if (r == SYS_EROFS) {
+		u_puts("save: read-only filesystem\n");
+		u_exit(1);
+	}
 	if (r == SYS_EIO) {
 		u_puts("save: disk write error\n");
 		u_exit(1);

+ 455 - 19
vfs.c

@@ -1,8 +1,11 @@
-/* Virtual filesystem: mount every device with a valid FAT filesystem under `/`. */
+/* Virtual filesystem: mount every device with a valid FAT filesystem under `/`,
+ * and the command pack (ramfs) at /ramfs.  File operations go through the
+ * fs_ops vtable so sys_fs.c and the shell stay filesystem-agnostic. */
 
 #include "vfs.h"
 #include "fatdetect.h"
 #include "io.h"
+#include "ramfs.h"
 
 vfs_mount_t vfs_mounts[VFS_MAX_MOUNTS];
 unsigned int vfs_mount_count;
@@ -61,24 +64,410 @@ static const char *fat_name(fat_type_t t)
 	}
 }
 
-static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
+static void mem_copy(void *dst, const void *src, unsigned int n)
+{
+	unsigned char *d = (unsigned char *)dst;
+	const unsigned char *s = (const unsigned char *)src;
+
+	while (n--)
+		*d++ = *s++;
+}
+
+static void copy_bounded(char *dst, const char *src, unsigned int size)
+{
+	unsigned int i = 0;
+
+	while (i + 1 < size && src[i]) {
+		dst[i] = src[i];
+		i++;
+	}
+	dst[i] = '\0';
+}
+
+/* ---- FAT implementation ---- */
+
+/* One load buffer for the shell: only one command is loaded at a time and the
+ * ELF loader has copied the segments out before we return. */
+static unsigned char fat_load_buf[VFS_FILE_MAX];
+
+static int fat_op_read_file(struct vfs_mount *m, const char *path,
+			    void *buf, unsigned int max)
+{
+	unsigned int dir;
+	char name[12];
+	int r = fat_resolve_path(&m->fs, path, &dir, name);
+
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_ENOENT;
+	r = fat_read_file_in(&m->fs, dir, name, buf, max);
+	if (r == -2)
+		return VFS_EIO;
+	if (r < 0)
+		return VFS_ENOENT;
+	return r;
+}
+
+static int fat_op_stat(struct vfs_mount *m, const char *path,
+		       unsigned int *size, int *is_dir)
+{
+	int r = fat_stat(&m->fs, path, size, is_dir);
+
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_ENOENT;
+	return 0;
+}
+
+static int fat_op_load_file(struct vfs_mount *m, const char *path,
+			    struct vfs_file *out)
+{
+	unsigned int size = 0;
+	int is_dir = 0, r;
+
+	r = fat_op_stat(m, path, &size, &is_dir);
+	if (r != 0)
+		return r;
+	if (is_dir || size == 0 || size > sizeof(fat_load_buf))
+		return VFS_EINVAL;
+	r = fat_op_read_file(m, path, fat_load_buf, sizeof(fat_load_buf));
+	if (r < 0)
+		return r;
+	out->data = fat_load_buf;
+	out->size = (unsigned int)r;
+	return 0;
+}
+
+static void fat_op_release_file(struct vfs_mount *m, struct vfs_file *f)
+{
+	(void)m;
+	(void)f;
+}
+
+static int fat_op_write_file(struct vfs_mount *m, const char *path,
+			     const void *buf, unsigned int len)
+{
+	unsigned int dir;
+	char name[12];
+	int r = fat_resolve_path(&m->fs, path, &dir, name);
+
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_EINVAL;
+	r = fat_write_file_in(&m->fs, dir, name, buf, len);
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_EINVAL;
+	return 0;
+}
+
+static int fat_op_unlink(struct vfs_mount *m, const char *path)
+{
+	unsigned int dir;
+	char name[12];
+	int r = fat_resolve_path(&m->fs, path, &dir, name);
+
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_ENOENT;
+	r = fat_delete_file_in(&m->fs, dir, name);
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_ENOENT;
+	return 0;
+}
+
+static int fat_op_mkdir(struct vfs_mount *m, const char *path)
+{
+	unsigned int dir;
+	char name[12];
+	int r = fat_resolve_path(&m->fs, path, &dir, name);
+
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_EINVAL;
+	r = fat_mkdir(&m->fs, dir, name);
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_EINVAL;   /* the FAT layer printed its own reason */
+	return 0;
+}
+
+static int fat_op_rmdir(struct vfs_mount *m, const char *path)
+{
+	unsigned int dir;
+	char name[12];
+	int r = fat_resolve_path(&m->fs, path, &dir, name);
+
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_EINVAL;
+	r = fat_rmdir(&m->fs, dir, name);
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_EINVAL;
+	return 0;
+}
+
+/* Index-based listing: fat_list_dir walks from the start each call and we
+ * count past `index` entries.  Directories are small. */
+static unsigned int fat_list_index;
+static struct vfs_dirent *fat_list_out;
+static int fat_list_done;
+static unsigned int fat_list_seen;
+
+static void fat_list_cb(const char *name, int is_dir, unsigned int size)
 {
-	fat_fs_t fs;
 	unsigned int i;
 
-	if (vfs_mount_count >= VFS_MAX_MOUNTS)
+	if (fat_list_done || fat_list_seen < fat_list_index) {
+		fat_list_seen++;
 		return;
+	}
+	for (i = 0; i < 11; i++)
+		fat_list_out->name[i] = name[i] ? name[i] : ' ';
+	fat_list_out->name[11] = '\0';
+	fat_list_out->size = size;
+	fat_list_out->is_dir = (unsigned int)is_dir;
+	fat_list_done = 1;
+	fat_list_seen++;
+}
+
+static int fat_op_list_dir(struct vfs_mount *m, const char *path,
+			   unsigned int index, struct vfs_dirent *out)
+{
+	unsigned int dir;
+	int r = fat_resolve_dir(&m->fs, path, &dir);
+
+	if (r == -2)
+		return VFS_EIO;
+	if (r != 0)
+		return VFS_EINVAL;
+
+	fat_list_index = index;
+	fat_list_seen = 0;
+	fat_list_out = out;
+	fat_list_done = 0;
+	r = fat_list_dir(&m->fs, dir, fat_list_cb);
+	if (r == -2)
+		return VFS_EIO;
+	return fat_list_done ? 1 : 0;
+}
+
+static const struct fs_ops fat_ops = {
+	.raw_83 = 1,
+	.read_file = fat_op_read_file,
+	.load_file = fat_op_load_file,
+	.release_file = fat_op_release_file,
+	.stat = fat_op_stat,
+	.write_file = fat_op_write_file,
+	.unlink = fat_op_unlink,
+	.mkdir = fat_op_mkdir,
+	.rmdir = fat_op_rmdir,
+	.list_dir = fat_op_list_dir,
+};
+
+/* ---- ramfs implementation ---- */
+
+static int ramfs_op_read_file(struct vfs_mount *m, const char *path,
+			      void *buf, unsigned int max)
+{
+	const void *data;
+	unsigned int size = 0, n;
+
+	(void)m;
+	data = ramfs_lookup(path, &size);
+	if (!data)
+		return VFS_ENOENT;
+	n = size > max ? max : size;
+	mem_copy(buf, data, n);
+	return (int)n;
+}
+
+static int ramfs_op_load_file(struct vfs_mount *m, const char *path,
+			      struct vfs_file *out)
+{
+	const void *data;
+	unsigned int size = 0;
+
+	(void)m;
+	data = ramfs_lookup(path, &size);
+	if (!data)
+		return VFS_ENOENT;
+	out->data = data;
+	out->size = size;
+	return 0;
+}
+
+static void ramfs_op_release_file(struct vfs_mount *m, struct vfs_file *f)
+{
+	(void)m;
+	(void)f;
+}
+
+static int ramfs_op_stat(struct vfs_mount *m, const char *path,
+			 unsigned int *size, int *is_dir)
+{
+	const void *data;
+	unsigned int sz = 0;
+	struct ramfs_dirent e;
+
+	(void)m;
+	if (path[0] == '\0') {
+		if (size)
+			*size = 0;
+		if (is_dir)
+			*is_dir = 1;
+		return 0;
+	}
+	data = ramfs_lookup(path, &sz);
+	if (data) {
+		if (size)
+			*size = sz;
+		if (is_dir)
+			*is_dir = 0;
+		return 0;
+	}
+	if (ramfs_list(path, 0, &e) == 1) {
+		if (size)
+			*size = 0;
+		if (is_dir)
+			*is_dir = 1;
+		return 0;
+	}
+	return VFS_ENOENT;
+}
+
+static int ramfs_op_ro(struct vfs_mount *m, const char *path)
+{
+	(void)m;
+	(void)path;
+	return VFS_EROFS;
+}
+
+static int ramfs_op_write_file(struct vfs_mount *m, const char *path,
+			       const void *buf, unsigned int len)
+{
+	(void)buf;
+	(void)len;
+	return ramfs_op_ro(m, path);
+}
+
+static int ramfs_op_unlink(struct vfs_mount *m, const char *path)
+{
+	return ramfs_op_ro(m, path);
+}
+
+static int ramfs_op_mkdir(struct vfs_mount *m, const char *path)
+{
+	return ramfs_op_ro(m, path);
+}
+
+static int ramfs_op_rmdir(struct vfs_mount *m, const char *path)
+{
+	return ramfs_op_ro(m, path);
+}
+
+static int ramfs_op_list_dir(struct vfs_mount *m, const char *path,
+			     unsigned int index, struct vfs_dirent *out)
+{
+	struct ramfs_dirent e;
+
+	(void)m;
+	if (ramfs_list(path, index, &e) != 1)
+		return 0;
+	copy_bounded(out->name, e.name, sizeof(out->name));
+	out->size = e.size;
+	out->is_dir = e.is_dir;
+	return 1;
+}
+
+static const struct fs_ops ramfs_ops = {
+	.raw_83 = 0,
+	.read_file = ramfs_op_read_file,
+	.load_file = ramfs_op_load_file,
+	.release_file = ramfs_op_release_file,
+	.stat = ramfs_op_stat,
+	.write_file = ramfs_op_write_file,
+	.unlink = ramfs_op_unlink,
+	.mkdir = ramfs_op_mkdir,
+	.rmdir = ramfs_op_rmdir,
+	.list_dir = ramfs_op_list_dir,
+};
+
+/* ---- mounts ---- */
+
+static vfs_mount_t *mount_slot(const char *name)
+{
+	unsigned int i;
+
+	if (vfs_mount_count >= VFS_MAX_MOUNTS)
+		return 0;
+	{
+		vfs_mount_t *m = &vfs_mounts[vfs_mount_count];
+
+		for (i = 0; name[i] && i < sizeof(m->name) - 1; i++)
+			m->name[i] = name[i];
+		m->name[i] = '\0';
+		m->mounted = 1;
+		return m;
+	}
+}
+
+static void mount_one(blk_dev_t *dev, unsigned int part_lba, const char *name)
+{
+	fat_fs_t fs;
+	vfs_mount_t *m;
+
 	if (fat_mount(dev, part_lba, &fs) != 0)
 		return;
+	m = mount_slot(name);
+	if (!m)
+		return;
+	m->ops = &fat_ops;
+	m->fs = fs;
+	m->ro = 0;
+	m->total_sectors = fs.total_sectors;
+	copy_bounded(m->type, fat_name(fs.type), sizeof(m->type));
+	copy_bounded(m->label, fs.label, sizeof(m->label));
+	vfs_mount_count++;
+}
+
+void vfs_mount_ramfs(void)
+{
+	vfs_mount_t *m;
 
-	for (i = 0; name[i] && i < sizeof(vfs_mounts[vfs_mount_count].name) - 1; i++)
-		vfs_mounts[vfs_mount_count].name[i] = name[i];
-	vfs_mounts[vfs_mount_count].name[i] = '\0';
-	vfs_mounts[vfs_mount_count].fs = fs;
-	vfs_mounts[vfs_mount_count].mounted = 1;
+	if (ramfs_count() == 0)
+		return;
+	m = mount_slot("ramfs");
+	if (!m)
+		return;
+	m->ops = &ramfs_ops;
+	m->ro = 1;
+	m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
+	copy_bounded(m->type, "ramfs", sizeof(m->type));
+	m->label[0] = '\0';
 	vfs_mount_count++;
 }
 
+void vfs_refresh_ramfs(void)
+{
+	vfs_mount_t *m = vfs_find("ramfs");
+
+	if (m)
+		m->total_sectors = (ramfs_pack_size() + 511u) / 512u;
+}
+
 static void part_name(char *out, const char *base, unsigned int idx)
 {
 	unsigned int i = 0;
@@ -151,26 +540,30 @@ vfs_mount_t *vfs_find(const char *name)
 void vfs_list_root(void)
 {
 	unsigned int i;
+
 	for (i = 0; i < vfs_mount_count; i++) {
 		vfs_mount_t *m = &vfs_mounts[i];
-		fat_fs_t *fs = &m->fs;
+
 		io_putstr(m->name);
 		io_putstr("  ");
-		io_putstr(fat_name(fs->type));
+		io_putstr(m->type);
 		io_putchar(' ');
-		put_size_sectors(fs->total_sectors);
-		if (fs->label[0]) {
+		put_size_sectors(m->total_sectors);
+		if (m->label[0]) {
 			io_putstr("  \"");
-			io_putstr(fs->label);
+			io_putstr(m->label);
 			io_putchar('"');
 		}
+		if (m->ro)
+			io_putstr("  (ro)");
 		io_putchar('\n');
 	}
 }
 
-int vfs_resolve(const char *path, fat_fs_t **fs, const char **fs_path)
+int vfs_resolve(const char *path, struct vfs_mount **m_out,
+		const char **rel_out)
 {
-	unsigned int i = 0, j;
+	unsigned int i = 0, j = 0;
 	char name[16];
 	vfs_mount_t *m;
 
@@ -178,7 +571,6 @@ int vfs_resolve(const char *path, fat_fs_t **fs, const char **fs_path)
 		return -1;
 
 	i = 1;
-	j = 0;
 	while (path[i] && path[i] != '/' && j < sizeof(name) - 1)
 		name[j++] = path[i++];
 	name[j] = '\0';
@@ -190,11 +582,55 @@ int vfs_resolve(const char *path, fat_fs_t **fs, const char **fs_path)
 	if (!m)
 		return -1;
 
-	*fs = &m->fs;
-	*fs_path = path + i;
+	while (path[i] == '/')
+		i++;              /* the relative path has no leading '/' */
+
+	*m_out = m;
+	*rel_out = path + i;
 	return 0;
 }
 
+int vfs_load_file(const char *path, struct vfs_file *out,
+		  struct vfs_mount **m_out)
+{
+	struct vfs_mount *m;
+	const char *rel;
+	int r;
+
+	if (vfs_resolve(path, &m, &rel) != 0)
+		return VFS_ENOENT;
+	r = m->ops->load_file(m, rel, out);
+	if (r == 0 && m_out)
+		*m_out = m;
+	return r;
+}
+
+void vfs_release_file(struct vfs_mount *m, struct vfs_file *f)
+{
+	if (m && m->ops->release_file)
+		m->ops->release_file(m, f);
+}
+
+int vfs_stat(const char *path, unsigned int *size, int *is_dir)
+{
+	struct vfs_mount *m;
+	const char *rel;
+
+	if (vfs_resolve(path, &m, &rel) != 0)
+		return VFS_ENOENT;
+	return m->ops->stat(m, rel, size, is_dir);
+}
+
+int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out)
+{
+	struct vfs_mount *m;
+	const char *rel;
+
+	if (vfs_resolve(path, &m, &rel) != 0)
+		return VFS_ENOENT;
+	return m->ops->list_dir(m, rel, index, out);
+}
+
 int vfs_join_path(const char *cwd, const char *arg, char *out, unsigned int out_size)
 {
 	char buf[VFS_PATH_MAX * 2 + 2];

+ 77 - 8
vfs.h

@@ -1,15 +1,67 @@
-/* Virtual filesystem: mounts every device with a valid FAT filesystem under
- * `/` (e.g. /floppy0, /floppy1, /ide0p1). Provides path resolution so the
- * shell commands operate on the right filesystem. */
+/* Virtual filesystem: filesystems mounted under `/`.  Two implementations:
+ * FAT on block devices (probed by vfs_mount_all) and the read-only ramfs
+ * backed by the command pack (ramfs.c, mounted at /ramfs). */
 #ifndef VFS_H
 #define VFS_H
 
 #include "fat.h"
 
-typedef struct {
+/* Largest file the shell will load from a filesystem into a kernel buffer. */
+#define VFS_FILE_MAX (16 * 1024)
+
+struct vfs_mount;
+struct fs_ops;
+
+struct vfs_file {
+	const void *data;
+	unsigned int size;
+};
+
+/* One directory entry: `name` is the raw 8.3 field for FAT (space padded) or
+ * the plain file name for ramfs. */
+struct vfs_dirent {
+	char name[12];
+	unsigned int size;
+	unsigned int is_dir;
+};
+
+/* Per-filesystem error codes (0 = success; the read/list calls additionally
+ * return positive byte/entry counts).  Mapped to SYS_E* by sys_fs.c. */
+#define VFS_EIO    (-1)
+#define VFS_ENOENT (-2)
+#define VFS_EINVAL (-3)
+#define VFS_EROFS  (-4)
+
+/* Per-filesystem operations.  Paths are relative to the mount (no leading
+ * '/'); "" is the mount root.  Implementations never print; list_dir returns
+ * 1 (entry filled), 0 (end) or a negative VFS_E*. */
+struct fs_ops {
+	int raw_83;   /* dirent names are raw 8.3 (FAT) and need display fixing */
+	int (*read_file)(struct vfs_mount *m, const char *path,
+			 void *buf, unsigned int max);
+	int (*load_file)(struct vfs_mount *m, const char *path,
+			 struct vfs_file *out);
+	void (*release_file)(struct vfs_mount *m, struct vfs_file *f);
+	int (*stat)(struct vfs_mount *m, const char *path,
+		    unsigned int *size, int *is_dir);
+	int (*write_file)(struct vfs_mount *m, const char *path,
+			  const void *buf, unsigned int len);
+	int (*unlink)(struct vfs_mount *m, const char *path);
+	int (*mkdir)(struct vfs_mount *m, const char *path);
+	int (*rmdir)(struct vfs_mount *m, const char *path);
+	int (*list_dir)(struct vfs_mount *m, const char *path,
+			unsigned int index, struct vfs_dirent *out);
+};
+
+typedef struct vfs_mount {
 	char name[16];
-	fat_fs_t fs;
+	const struct fs_ops *ops;
 	int mounted;
+	int ro;
+	fat_fs_t fs;                 /* FAT mounts only */
+	unsigned int total_sectors;  /* for the mount listing */
+	char type[8];                /* "FAT12"/"FAT16"/"FAT32"/"ramfs" */
+	char label[12];
 } vfs_mount_t;
 
 #define VFS_MAX_MOUNTS 24
@@ -20,15 +72,32 @@ extern unsigned int vfs_mount_count;
 /* Scan devices and mount all detected FAT filesystems. */
 void vfs_mount_all(void);
 
+/* Mount the command pack (ramfs) at /ramfs; no-op when no pack is loaded. */
+void vfs_mount_ramfs(void);
+
+/* Update the ramfs mount's reported size after ramfs_replace(). */
+void vfs_refresh_ramfs(void);
+
 /* Find a mount by name (NULL if not found). */
 vfs_mount_t *vfs_find(const char *name);
 
 /* Print the mount list (for `ls /`). */
 void vfs_list_root(void);
 
-/* Resolve an absolute path "/<mount>/<path>" into (fs, fs_path). The fs_path
- * points into `path` (no copy). Returns 0 on success, -1 on error. */
-int vfs_resolve(const char *path, fat_fs_t **fs, const char **fs_path);
+/* Resolve an absolute path "/<mount>/<path>" into the mount and a path
+ * relative to it ("" for the mount root).  Returns 0 or -1. */
+int vfs_resolve(const char *path, struct vfs_mount **m,
+		const char **rel_path);
+
+/* Load a whole file (borrowed pointer for ramfs, the static buffer for FAT);
+ * release it with vfs_release_file() after use. */
+int vfs_load_file(const char *path, struct vfs_file *out,
+		  struct vfs_mount **m_out);
+void vfs_release_file(struct vfs_mount *m, struct vfs_file *f);
+
+/* Convenience wrappers for the shell and completion. */
+int vfs_stat(const char *path, unsigned int *size, int *is_dir);
+int vfs_list_dir(const char *path, unsigned int index, struct vfs_dirent *out);
 
 /* Join `arg` onto `cwd` (arg may be absolute or relative) and normalize the
  * result into `out` as an absolute VFS path. "." and duplicate slashes are