Răsfoiți Sursa

Replace the command pack over serial or UDP (cmds-dev / net-cmds)

The ramfs contents can now be updated on a running board without a kernel
reload: the pack is received into page-allocated memory, validated and swapped
in, and a bad transfer leaves the current commands untouched.

- payload.c/h: the serial receiver.  Frame: "KXNP" + entry + size (the same
  framing as KXEC), data, checksum.  payload_pending() runs before
  kexec_pending() in the WM idle loop and returns without consuming a KXEC
  stream, so `make dev` keeps reloading the kernel.
- netkexec.c: a KXNP header switches the existing stop-and-wait UDP transfer
  into payload mode (no jump); the staging pages become the ramfs's own copy
  on success and are freed only when the pack is rejected.
- ramfs_replace() validates before swapping, keeps the old pack on failure and
  frees the previous received copy; vfs_refresh_ramfs() updates the mount's
  reported size.  The serial payload receiver clears kexec_busy when it exits.
- Host side: kexec_send --payload, netkexec_send --payload and the Makefile
  targets cmds-dev (serial) and net-cmds (network).
- wm.c: the pack transfer is auto-detected like a kernel reload.

Verified on QEMU: a garbage pack (valid checksum) is refused with the running
commands intact; a v2 pack containing only a modified hello installs ("pack
installed (1 files)"), newly spawned hello prints the new text while cpuinfo
disappears from the PATH, and pushing the standard pack back restores
everything (6/6 checks over the serial socket, 6/6 over UDP through the real
netkexec_send --payload, and the same install path through kexec_send
--payload over a pty).  kexec over serial still reloads (make dev) and make
test-usb stays green (6/6).

AGENTS.md: the cmds-dev/net-cmds section, the wire-format note and the
serial-chardev gotcha.
Johannes Zellner 1 lună în urmă
părinte
comite
aca81e3850
8 a modificat fișierele cu 369 adăugiri și 31 ștergeri
  1. 29 1
      AGENTS.md
  2. 17 2
      Makefile
  3. 33 12
      kexec_send.c
  4. 75 6
      netkexec.c
  5. 38 10
      netkexec_send.c
  6. 156 0
      payload.c
  7. 12 0
      payload.h
  8. 9 0
      wm.c

+ 29 - 1
AGENTS.md

@@ -276,10 +276,14 @@ runs at **115200 baud**. Two paths exist:
 
 Wire protocol (host → target, sent immediately; no handshake):
 `[magic "KXEC"][entry u32 LE][size u32 LE][data][checksum u8]`.
+The command pack has its own frame (`make cmds-dev`), the same layout with a
+`"KXNP"` magic (the entry field is ignored), which replaces /ramfs in place
+(see below).
 
 **Every iteration:**
 ```sh
-make dev            # builds kernel.bin, computes kernel_entry via nm, sends
+make dev            # builds kernel.bin (which carries cmds.pack), sends
+make cmds-dev       # optional: push just the command pack to a running board
 ```
 The reload is automatic. On boot the kernel prints `Nebs OS v<version> booted`
 to COM1, so a serial monitor can confirm the new build headlessly:
@@ -307,6 +311,26 @@ lives inside the overwritten kernel region), so it's safe to edit any file,
 including `vesa_stage.S`/`boot.S` layout — but the video *mode* itself is only
 re-applied on a full floppy/USB boot (C-light reuses the current LFB).
 
+## Command pack hot-reload (cmds-dev / net-cmds)
+
+The commands live in `cmds.pack` (`bin/<name>` entries, built by `mkpack`),
+which is embedded in `kernel.bin` and mounted read-only at `/ramfs`.  A
+running board can replace it without a kernel reload:
+
+```sh
+make cmds-dev       # serial:  ./kexec_send  --payload $(SERIAL_DEV) $(SERIAL_BAUD) cmds.pack
+make net-cmds       # network: ./netkexec_send --payload $(BOARD_IP) $(NETKEXEC_PORT) cmds.pack
+```
+
+The pack is received into page-allocated memory (magic "KXNP" over serial,
+the same stop-and-wait protocol with a "KXNP" header over UDP), validated
+*before* the swap, so a truncated or corrupt transfer leaves the running
+commands untouched; a good one is installed by `ramfs_replace()` and the
+previous received copy is freed.  Existing processes are unaffected (they
+already loaded their ELF), and the next spawn picks up the new files.  The
+WM idle loop auto-detects both the kernel (KXEC/KXNU) and the pack (KXNP)
+transfers, so a plain `make cmds-dev`/`make net-cmds` is enough.
+
 ## Network hot-reload (net-dev) workflow
 
 For boards without a serial console (the ASUS AT3IONT-I), the same reload runs
@@ -486,6 +510,10 @@ QEMU gotchas that bit once:
 - `pkill -f qemu-system-i386` matches *your own shell command line* (it contains
   the string) and kills your script; use `pkill qemu` (process-name match) or
   kill by PID.
+- When a harness drives a `-serial unix:...` chardev, connect **before** the
+  guest boots (immediately after QEMU starts): QEMU discards guest output
+  written while no client is attached, so a late-connecting reader misses the
+  boot log (kexec tests that wait for it then mis-report a failed reload).
 
 ## On-screen debug overlay (`kdbg`)
 

+ 17 - 2
Makefile

@@ -13,7 +13,7 @@ ASFLAGS  = --32 -I.
 BOOT_LDFLAGS   = -T linker.ld -nostdlib -m elf_i386
 KERNEL_LDFLAGS = -T kernel.ld -nostdlib -m elf_i386
 
-.PHONY: all clean run run-floppy run-usb run-uhci run-net run-ahci test-usb run-serial floppy usb flash space prog bootstrap dev net-dev netmon monitor
+.PHONY: all clean run run-floppy run-usb run-uhci run-net run-ahci test-usb run-serial floppy usb flash space prog bootstrap dev net-dev cmds-dev net-cmds netmon monitor
 
 all: floppy.img usb.img final.bin root.img
 
@@ -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 prog_embed.o space_embed.o cmds_pack.o ramfs.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 payload.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -363,7 +363,11 @@ cmds.pack: $(addsuffix .elf,$(UCMDS)) hello.elf mkpack
 cmds_pack.o: cmds.pack
 	$(LD) -m elf_i386 -r -b binary -o cmds_pack.o cmds.pack
 
+payload.o: payload.c payload.h io.h mm.h ramfs.h serial.h vfs.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 ramfs.o: ramfs.c ramfs.h serial.h
+	$(CC) $(CFLAGS) -o $@ $<
 
 space.o: space.c run_api.h
 	$(CC) $(CFLAGS) -o $@ $<
@@ -516,6 +520,17 @@ dev: kernel.bin kexec_send
 	echo "Sending kernel.bin (entry 0x$$entry) via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."; \
 	./kexec_send $(SERIAL_DEV) $(SERIAL_BAUD) $$entry kernel.bin
 
+# Push just the command pack (cmds.pack) to a running board: no kernel
+# reload, the /ramfs contents are replaced in place.  The board must be idle
+# at a prompt (the WM loop auto-detects the transfer).
+cmds-dev: cmds.pack kexec_send
+	@echo "Sending cmds.pack via $(SERIAL_DEV) @ $(SERIAL_BAUD)..."
+	./kexec_send --payload $(SERIAL_DEV) $(SERIAL_BAUD) cmds.pack
+
+net-cmds: cmds.pack netkexec_send
+	@echo "Sending cmds.pack to $(BOARD_IP):$(NETKEXEC_PORT)..."
+	./netkexec_send --payload $(BOARD_IP) $(NETKEXEC_PORT) cmds.pack
+
 # Show the board's netlog stream (kernel debug + terminal text) like `make
 # monitor` does over serial.
 netmon: netmonitor

+ 33 - 12
kexec_send.c

@@ -36,15 +36,33 @@ int main(int argc, char *argv[])
 	unsigned char hdr[12];
 	struct termios tty;
 	uint32_t entry;
-
-	if (argc != 5) {
-		fprintf(stderr, "Usage: %s <device> <baud> <entry_hex> <file>\n", argv[0]);
-		return 1;
+	const char *dev, *baud, *file;
+	int payload = 0;
+
+	if (argc > 1 && strcmp(argv[1], "--payload") == 0) {
+		if (argc != 5) {
+			fprintf(stderr, "Usage: %s --payload <device> <baud> <file>\n",
+				argv[0]);
+			return 1;
+		}
+		payload = 1;
+		dev = argv[2];
+		baud = argv[3];
+		file = argv[4];
+		entry = 0;
+	} else {
+		if (argc != 5) {
+			fprintf(stderr, "Usage: %s <device> <baud> <entry_hex> <file>\n",
+				argv[0]);
+			return 1;
+		}
+		dev = argv[1];
+		baud = argv[2];
+		entry = (uint32_t)strtoul(argv[3], NULL, 16);
+		file = argv[4];
 	}
 
-	entry = (uint32_t)strtoul(argv[3], NULL, 16);
-
-	fp = fopen(argv[4], "rb");
+	fp = fopen(file, "rb");
 	if (!fp) {
 		perror("fopen");
 		return 1;
@@ -71,7 +89,7 @@ int main(int argc, char *argv[])
 		cksum += data[i];
 	cksum &= 0xFF;
 
-	fd = open(argv[1], O_RDWR | O_NOCTTY);
+	fd = open(dev, O_RDWR | O_NOCTTY);
 	if (fd < 0) {
 		perror("open");
 		free(data);
@@ -85,8 +103,8 @@ int main(int argc, char *argv[])
 		close(fd);
 		return 1;
 	}
-	cfsetispeed(&tty, (speed_t)atoi(argv[2]));
-	cfsetospeed(&tty, (speed_t)atoi(argv[2]));
+	cfsetispeed(&tty, (speed_t)atoi(baud));
+	cfsetospeed(&tty, (speed_t)atoi(baud));
 	tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
 	tty.c_cflag |= CLOCAL | CREAD;
 	tty.c_cflag &= ~(PARENB | PARODD | CRTSCTS | CSTOPB);
@@ -102,9 +120,12 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	fprintf(stderr, "Sending %ld bytes (entry 0x%08x)...\n", size, entry);
+	if (payload)
+		fprintf(stderr, "Sending %ld bytes (command pack)...\n", size);
+	else
+		fprintf(stderr, "Sending %ld bytes (entry 0x%08x)...\n", size, entry);
 
-	memcpy(hdr, "KXEC", 4);
+	memcpy(hdr, payload ? "KXNP" : "KXEC", 4);
 	put32le(hdr + 4, entry);
 	put32le(hdr + 8, (uint32_t)size);
 

+ 75 - 6
netkexec.c

@@ -24,11 +24,15 @@
 #include "serial.h"
 #include "timer.h"
 #include "netkexec.h"
+#include "mm.h"
+#include "ramfs.h"
+#include "vfs.h"
 
 #define NETKEXEC_PORT       19288
 #define NETKEXEC_DATA_MAX   1400
 
 #define X_MAGIC_HDR   0x554E584Bu   /* "KXNU" */
+#define X_MAGIC_PAY   0x504E584Bu   /* "KXNP": a command pack for /ramfs */
 #define X_MAGIC_DATA  0x444E584Bu   /* "KXND" */
 #define X_MAGIC_ACK   0x414E584Bu   /* "KXNA" */
 
@@ -40,8 +44,29 @@ static unsigned char rxbuf[1520];       /* one full Ethernet frame */
 static unsigned char replybuf[1520];
 static unsigned char ackbuf[12];
 
+/* Page-allocated staging for a payload transfer (freed after the swap). */
+static unsigned int pay_buf;
+static unsigned int pay_size;
+
+static int net_payload_alloc(unsigned int size)
+{
+	pay_size = size;
+	pay_buf = mm_alloc_pages((size + 0xFFFu) >> 12);
+	return pay_buf != 0;
+}
+
+static void net_payload_free(void)
+{
+	unsigned int i;
+
+	for (i = 0; i * 0x1000 < pay_size; i++)
+		mm_free_page(pay_buf + i * 0x1000);
+	pay_buf = 0;
+}
+
 /* Pending transfer (filled by netkexec_pending). */
 static int have_header;
+static int payload_mode;         /* header was KXNP: replace the ramfs instead */
 static unsigned int x_entry, x_size, x_csum;
 static unsigned char peer_mac[6], peer_ip[4];
 static unsigned int peer_port;
@@ -148,11 +173,17 @@ int netkexec_pending(void)
 			nd->tx(replybuf, (unsigned int)rlen);
 		return 0;
 	}
-	if (plen < 16 || rd32(payload) != X_MAGIC_HDR)
-		return 0;
-	if (plen < 16 || rd32(payload) != X_MAGIC_HDR)
+	if (plen < 16)
 		return 0;
+	{
+		/* KXNU: kernel image (jump after the checksum); KXNP: a command
+		 * pack for /ramfs (swap it in and stay running). */
+		unsigned int magic = rd32(payload);
 
+		if (magic != X_MAGIC_HDR && magic != X_MAGIC_PAY)
+			return 0;
+		payload_mode = (magic == X_MAGIC_PAY);
+	}
 	x_entry = rd32(payload + 4);
 	x_size = rd32(payload + 8);
 	x_csum = rd32(payload + 12);
@@ -173,9 +204,9 @@ static void send_ack(unsigned int next_offset, unsigned int status)
 		     ackbuf, sizeof(ackbuf));
 }
 
-static int netkexec_recv(void)
+static int netkexec_recv(unsigned int stage_base)
 {
-	volatile unsigned char *stage = (volatile unsigned char *)kexec_stage;
+	volatile unsigned char *stage = (volatile unsigned char *)stage_base;
 	unsigned int offset = 0, retries = 0, sum = 0;
 	unsigned int deadline = get_ticks_ms() + 3000;
 
@@ -247,6 +278,44 @@ void program_netkexec_main(void)
 	io_puthex(x_size);
 	io_putstr(" bytes over UDP...\n");
 
+	if (payload_mode) {
+		unsigned int buf = 0;
+
+		if (!net_payload_alloc(x_size)) {
+			io_putstr("netkexec: out of memory (staging)\n");
+			netdev_release();
+			kexec_busy = 0;
+			return;
+		}
+		buf = pay_buf;
+
+		if (netkexec_recv(buf) != 0) {
+			io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
+			net_payload_free();
+			netdev_release();
+			kexec_busy = 0;
+			return;
+		}
+
+		io_putstr("\nnetkexec: received ");
+		io_puthex(x_size);
+		io_putstr(" bytes, ");
+		if (ramfs_replace((const void *)buf, x_size) != 0) {
+			io_putstr("not a valid pack (unchanged)\n");
+			net_payload_free();     /* the ramfs kept the old pack */
+		} else {
+			vfs_refresh_ramfs();
+			io_putstr("pack installed (");
+			io_puthex(ramfs_count());
+			io_putstr(" files)\n");
+			pay_buf = 0;            /* ownership moved to the ramfs */
+			pay_size = 0;
+		}
+		netdev_release();
+		kexec_busy = 0;
+		return;
+	}
+
 	if (!kexec_stage_alloc(x_size)) {
 		io_putstr("netkexec: out of memory (staging)\n");
 		netdev_release();
@@ -254,7 +323,7 @@ void program_netkexec_main(void)
 		return;
 	}
 
-	if (netkexec_recv() != 0) {
+	if (netkexec_recv(kexec_stage) != 0) {
 		io_putstr("\nnetkexec: aborted (timeout or checksum)\n");
 		kexec_stage_free();
 		netdev_release();

+ 38 - 10
netkexec_send.c

@@ -21,6 +21,7 @@
 
 #define DATA_MAX   1400
 #define MAGIC_HDR  0x554E584Bu   /* "KXNU" */
+#define MAGIC_PAY   0x504E584Bu   /* "KXNP" */
 #define MAGIC_DATA 0x444E584Bu   /* "KXND" */
 #define MAGIC_ACK  0x414E584Bu   /* "KXNA" */
 #define ACK_DONE   1
@@ -78,16 +79,33 @@ int main(int argc, char **argv)
 	FILE *f;
 	unsigned int offset = 0, sum = 0;
 	int fd, i, fails = 0;
+	int payload = 0;
 	struct sockaddr_in dst;
 
-	if (argc != 5) {
-		fprintf(stderr, "usage: %s <board-ip> <port> <entry-hex> <file>\n", argv[0]);
-		return 2;
+	/* --payload: replace the command pack (/ramfs) instead of the kernel. */
+	if (argc > 1 && strcmp(argv[1], "--payload") == 0) {
+		if (argc != 5) {
+			fprintf(stderr, "usage: %s --payload <board-ip> <port> <file>\n",
+				argv[0]);
+			return 2;
+		}
+		payload = 1;
+		ip = argv[2];
+		port = (unsigned int)strtoul(argv[3], NULL, 0);
+		path = argv[4];
+		entry = 0;
+	} else {
+		if (argc != 5) {
+			fprintf(stderr,
+				"usage: %s <board-ip> <port> <entry-hex> <file>\n",
+				argv[0]);
+			return 2;
+		}
+		ip = argv[1];
+		port = (unsigned int)strtoul(argv[2], NULL, 0);
+		entry = (unsigned int)strtoul(argv[3], NULL, 16);
+		path = argv[4];
 	}
-	ip = argv[1];
-	port = (unsigned int)strtoul(argv[2], NULL, 0);
-	entry = (unsigned int)strtoul(argv[3], NULL, 16);
-	path = argv[4];
 
 	f = fopen(path, "rb");
 	if (!f) {
@@ -126,10 +144,15 @@ int main(int argc, char **argv)
 	}
 
 	pkt = malloc(12 + DATA_MAX);
-	printf("netkexec: %s:%u, %ld bytes, entry 0x%08x\n", ip, port, size, entry);
+	if (payload)
+		printf("netkexec: %s:%u, %ld bytes, command pack for /ramfs\n",
+		       ip, port, size);
+	else
+		printf("netkexec: %s:%u, %ld bytes, entry 0x%08x\n",
+		       ip, port, size, entry);
 
 	/* Header first: the board answers with the first expected offset. */
-	wr32(pkt, MAGIC_HDR);
+	wr32(pkt, payload ? MAGIC_PAY : MAGIC_HDR);
 	wr32(pkt + 4, entry);
 	wr32(pkt + 8, (uint32_t)size);
 	wr32(pkt + 12, (uint32_t)sum);
@@ -190,7 +213,12 @@ int main(int argc, char **argv)
 		       offset, size);
 		fflush(stdout);
 	}
-	printf("\rnetkexec: 100%% (%ld bytes) - image accepted, board is jumping\n", size);
+	if (payload)
+		printf("\rnetkexec: 100%% (%ld bytes) - pack accepted, /ramfs replaced\n",
+		       size);
+	else
+		printf("\rnetkexec: 100%% (%ld bytes) - image accepted, board is jumping\n",
+		       size);
 	close(fd);
 	free(pkt);
 	free(data);

+ 156 - 0
payload.c

@@ -0,0 +1,156 @@
+/* payload: replace the command pack (ramfs) over serial at runtime.
+ *
+ * Wire format (same framing as kexec.c, the entry field is unused):
+ *   4 bytes: magic "KXNP" (LE)
+ *   4 bytes: entry (LE, 0)
+ *   4 bytes: size (LE)            -- pack byte count
+ *   N bytes: pack data
+ *   1 byte:  checksum (sum of data bytes mod 256)
+ *
+ * The pack is received into page-allocated memory and validated before the
+ * swap (ramfs_replace), so a truncated or corrupt transfer leaves the running
+ * commands untouched; the previous received copy is freed on success.  The WM
+ * idle loop detects the magic like it does for kexec, so `make cmds-dev` on
+ * the host alone triggers it. */
+#include "payload.h"
+#include "io.h"
+#include "kexec.h"
+#include "mm.h"
+#include "ramfs.h"
+#include "serial.h"
+#include "vfs.h"
+
+#define PAYLOAD_MAGIC 0x504E584B /* "KXNP" little-endian */
+#define PAYLOAD_MAX   0x400000   /* sanity cap, like KEXEC_MAX */
+
+int payload_pending(void)
+{
+	unsigned char b, x, n, p;
+
+	/* Scan for the "KXNP" magic, draining bytes that cannot begin it (same
+	 * self-healing as kexec_pending). */
+	for (;;) {
+		if (!serial_ring_peek(0, &b))
+			return 0;
+		if (b != 'K') {
+			(void)serial_ring_getc();
+			continue;
+		}
+		if (serial_ring_peek(1, &x) && serial_ring_peek(2, &n) &&
+		    serial_ring_peek(3, &p)) {
+			if (x == 'X' && n == 'N' && p == 'P')
+				return 1;
+			/* A kernel image ("KXEC") belongs to kexec_pending():
+			 * leave it untouched (the WM checks this detector
+			 * first, so consuming the 'K' here would eat the
+			 * reload stream). */
+			if (x == 'X' && n == 'E' && p == 'C')
+				return 0;
+			(void)serial_ring_getc();
+			continue;
+		}
+		return 0;
+	}
+}
+
+static unsigned char header[12];
+
+static void free_span(unsigned int base, unsigned int size)
+{
+	unsigned int i, pages = (size + 0xFFFu) >> 12;
+
+	for (i = 0; i < pages; i++)
+		mm_free_page(base + i * 0x1000);
+}
+
+static int payload_recv(unsigned int *size, unsigned int *buf)
+{
+	unsigned int i, n, magic = 0;
+	unsigned char c, sum = 0, chk;
+	volatile unsigned char *dst;
+
+	for (i = 0; i < sizeof(header); i++) {
+		if (!serial_ring_wait(5000, &header[i]))
+			goto fail_header;
+		if (i < 4)
+			magic |= (unsigned int)header[i] << (8 * i);
+	}
+	if (magic != PAYLOAD_MAGIC)
+		goto fail_magic;
+
+	*size = (unsigned int)header[8] | ((unsigned int)header[9] << 8) |
+		((unsigned int)header[10] << 16) | ((unsigned int)header[11] << 24);
+	if (*size == 0 || *size > PAYLOAD_MAX)
+		goto fail_size;
+
+	*buf = mm_alloc_pages((*size + 0xFFFu) >> 12);
+	if (!*buf)
+		goto fail_mem;
+	dst = (volatile unsigned char *)*buf;
+
+	for (n = 0; n < *size; n++) {
+		if (!serial_ring_wait(5000, &c)) {
+			free_span(*buf, *size);
+			goto fail_data;
+		}
+		dst[n] = c;
+		sum += c;
+	}
+	if (!serial_ring_wait(5000, &chk)) {
+		free_span(*buf, *size);
+		goto fail_sum;
+	}
+	if (chk != sum) {
+		free_span(*buf, *size);
+		goto fail_csum;
+	}
+	return 0;
+
+fail_header:
+	io_putstr("payload: timeout (header)\n");
+	return -1;
+fail_magic:
+	io_putstr("payload: bad magic\n");
+	return -1;
+fail_size:
+	io_putstr("payload: bad size\n");
+	return -1;
+fail_mem:
+	io_putstr("payload: out of memory\n");
+	return -1;
+fail_data:
+	io_putstr("payload: timeout (data)\n");
+	return -1;
+fail_sum:
+	io_putstr("payload: timeout (checksum)\n");
+	return -1;
+fail_csum:
+	io_putstr("payload: checksum mismatch\n");
+	return -1;
+}
+
+void program_payload_main(void)
+{
+	unsigned int size = 0, buf = 0;
+
+	io_putstr("payload: waiting for command pack over serial...\n");
+	if (payload_recv(&size, &buf) != 0) {
+		io_putstr("payload: aborted (pack unchanged)\n");
+		kexec_busy = 0;
+		return;
+	}
+
+	io_putstr("payload: received ");
+	io_puthex(size);
+	io_putstr(" bytes, ");
+	if (ramfs_replace((const void *)buf, size) != 0) {
+		io_putstr("not a valid pack (unchanged)\n");
+		free_span(buf, size);
+	} else {
+		vfs_refresh_ramfs();
+		io_putstr("pack installed (");
+		io_puthex(ramfs_count());
+		io_putstr(" files)\n");
+	}
+	kexec_busy = 0;
+}

+ 12 - 0
payload.h

@@ -0,0 +1,12 @@
+/* payload: replace the command pack over serial at runtime (see payload.c). */
+#ifndef PAYLOAD_H
+#define PAYLOAD_H
+
+/* Returns 1 when the serial ring buffer starts with the "KXNP" magic. */
+int payload_pending(void);
+
+/* Receive + validate + swap the pack; never returns when the board jumps
+ * (it does not -- unlike kexec this only replaces /ramfs). */
+void program_payload_main(void);
+
+#endif

+ 9 - 0
wm.c

@@ -11,6 +11,7 @@
 #include "mouse.h"
 #include "netkexec.h"
 #include "netlog.h"
+#include "payload.h"
 #include "process.h"
 #include "rtc.h"
 #include "timer.h"
@@ -599,6 +600,14 @@ static void wm_handle_kexec(void)
 {
 	if (kexec_busy)
 		return;
+	/* A command pack for /ramfs (checked before kexec: both start with
+	 * 'K', and the kexec detector drops anything that is not KXEC). */
+	if (payload_pending()) {
+		kexec_busy = 1;
+		if (process_create("payload", program_payload_main) < 0)
+			kexec_busy = 0;
+		return;
+	}
 	if (kexec_pending()) {
 		kexec_busy = 1;
 		if (process_create("kexec", program_kexec_main) < 0)