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

+13 -1
View File
@@ -139,6 +139,7 @@ void reset_to_defaults(EmuState *s) {
X(usb_pd_voltage_mv, "usb_pd", "voltage_mv", 0) \
X(usb_pd_amperage_ma, "usb_pd", "amperage_ma", 0) \
X(usb_host, "usb", "host", 0) \
X(usb_nbd_port, "usb", "nbd_port", 0) \
/* SoC / PMIC */ \
X(pmic_otp, "soc", "pmic_otp", 0) \
X(is_mariko, "soc", "is_mariko", 0) \
@@ -652,7 +653,18 @@ void build_ui(EmuState *state) {
if (ImGui::Checkbox("PC on the USB-C port (--usb-host)", &host)) {
state->usb_host.store(host);
}
ImGui::TextDisabled("Mass storage is read back and ejected; HID is polled.");
// Mass storage served over NBD instead (--usb-host nbd[:port]);
// takes effect when the next disk is attached.
bool nbd = state->usb_nbd_port.load() != 0;
if (ImGui::Checkbox("Serve mass storage over NBD", &nbd)) {
state->usb_nbd_port.store(nbd ? 10809 : 0);
}
if (nbd) {
atomic_slider_int<uint16_t>("NBD port", state->usb_nbd_port, 1024, 65535, "%d");
ImGui::TextDisabled("127.0.0.1 only; the disk is ejected when the client disconnects.");
} else {
ImGui::TextDisabled("Mass storage is read back, written back unchanged and ejected;\nHID is polled.");
}
}
if (ImGui::CollapsingHeader("Display")) {