Let the USB host write, and serve a gadget's disk over NBD

The emulated PC only ever read a mass storage disk and then ejected it, so
the write path through a gadget went untested and the disk could not be
used from the machine running the emulator.

The default session now asks MODE SENSE(6) for the write-protect bit and,
on a writable disk whose read checked out, writes the last 8 sectors back
as read (WRITE(10), SYNCHRONIZE CACHE) and reads them again.

--usb-host nbd[:port] (or [usb] nbd_port, or the config window) locks the
medium and serves the disk on 127.0.0.1 with a small non-blocking NBD
server (t210/nbd.cpp: fixed newstyle handshake, READ/WRITE/FLUSH/DISC).
Requests become READ(10)/WRITE(10)s of up to 64 KiB, a quiet client gets
a TEST UNIT READY every second, a write-protected disk is exported
read-only, and when the client disconnects the host ejects the disk.

Serving Nyx exposed a controller model bug: a device transfer shorter than
the host's request was taken as a short packet, so bdk's 64 KiB READ(10),
sent as two 32 KiB transfers, lost the second half. Only a short or
zero-length packet (the USB2 controller's own ZLP unless the queue head
disables it) now ends the host's transfer.

tests/usb grows a write-protected scenario, WRITE(10), MODE SENSE and
SYNCHRONIZE CACHE on its RAM disk (reads sent in 32 KiB transfers, as bdk
does), and three NBD runs driven by a Python client. Checked with Nyx SD
UMS on Erista and Mariko: nbdinfo negotiates, qemu-img convert copies the
256 MiB disk byte for byte, and a qemu-io write lands in the SD image.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YYewwDPujjo5B2xRyC5Grb
This commit is contained in:
Claude committed 2026-09-26 11:09:57 +00:00
1 parent 0c7f12a83b
commit f84caeaacc
13 files changed
+1183 -67

No files matched your search

+21 -2
View File
@@ -358,9 +358,28 @@ int main(int argc, char *argv[]) {
}
} else if (strcmp(argv[i], "--usb-host") == 0) {
// A PC on the USB-C port: it enumerates whatever gadget the
// payload brings up and, for mass storage, reads it back.
// payload brings up and, for mass storage, reads it back - or,
// given "nbd[:port]", serves the disk over NBD.
state.usb_host = true;
printf("[emu] USB host connected [overrides ini]\n");
if (i + 1 < argc && strncmp(argv[i + 1], "nbd", 3) == 0 &&
(argv[i + 1][3] == 0 || argv[i + 1][3] == ':')) {
const char *arg = argv[++i];
unsigned long port = 10809; // the NBD port
char *end = nullptr;
if (arg[3] == ':')
port = strtoul(arg + 4, &end, 10);
if (port == 0 || port > 65535 || (end && *end)) {
fprintf(stderr, "[emu] Bad --usb-host value '%s'; expected"
" nbd or nbd:<port>\n", arg);
port = 10809;
}
state.usb_nbd_port = (uint16_t)port;
printf("[emu] USB host connected, serving mass storage over"
" NBD on port %lu [overrides ini]\n", port);
} else {
state.usb_nbd_port = 0;
printf("[emu] USB host connected [overrides ini]\n");
}
} else if (strcmp(argv[i], "--wifi-radio") == 0 && i + 1 < argc) {
// WLAN half of the same package, on PCIe. 'faulty' means
// something different here from the Bluetooth side: the link