Repository navigation
usb: let the user set the gadget identity - #82
Merged
Merged
Conversation
Upstream asks for this five times (sipeed/NanoKVM#829, #745, #574, #356, #104): corporate USB policies and endpoint security refuse a device called "sipeed NanoKVM", and the identity was hard-coded in S03usbdev and S03usbhid. The USB settings page gets a USB identity section: the factory identity, a "generic composite device" preset (the factory IDs with the names Generic / USB Composite Device), or the four fields by hand. No preset carries another vendor's ID; a custom ID is the user's own, and the page says what it breaks: the serial console helper's udev rule on Linux hosts names the IDs. The serial number stays the chip UID (that rule is bound to it), bcdDevice stays the mode flag. The identity is part of the composition. The scripts read /boot/usb.vid, usb.pid (as before), usb.manufacturer and usb.product (new) at every start. The recovery init reads the manufacturer and product files already; it takes the IDs from its own usb.idVendor/usb.idProduct, and it lives in the checksummed initramfs, so aligning it waits for a platform build (the docs say so). The four files join the apply transaction: snapshotted, written by content (absent for the factory value), restored on failure, and the identity the gadget presents is read back from configfs in the verification. It is in the revision, so a stale tab cannot change it unseen. PUT takes an optional identity object; a client that sends none keeps the one in place. Tests: ID and string validation, defaults and the files an identity needs, reading /boot (a damaged file is the factory value), verification against a fake configfs, the transaction writing, keeping on rollback and removing the files; the web presets, matching and validation. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Upstream asks for this in five threads (sipeed/NanoKVM#829, #745, #574, #356, #104): corporate USB policies and endpoint security refuse a device called "sipeed NanoKVM", and the identity was hard-coded in
S03usbdevandS03usbhid.What the user gets
Settings → USB, below the manual section: USB identity with a preset select and four fields.
0x3346:0x1009,sipeed/NanoKVM.Generic/USB Composite Device— enough for the "it says NanoKVM" blocks.bcdDeviceis not exposed:hid.GetModereads it as the mode flag.The identity is part of the composition draft: Apply writes it with the rest and reconnects USB, and it is in the
revision, so a stale tab cannot change it unseen.How it is stored and applied
S03usbdevandS03usbhidalready read/boot/usb.vidand/boot/usb.pid; they now also read/boot/usb.manufacturerand/boot/usb.product(first line, a missing or empty file is the factory value). The recovery init (platform/boot/init) already reads the manufacturer and product files but takes the IDs from its ownusb.idVendor/usb.idProduct; it is insideinitramfs.cpio.zst, whichexpected.sha256pins, so aligning it waits for a platform build. Until then a recovery boot presents the factory IDs unless those two files are set by hand; the docs say so.S03usbdev(thelun.1block around line 218); this change is in the identity block around line 101, so whichever merges first, the other rebases trivially.usbCompositionFlagsand the apply transaction: snapshotted with the ten flags, written by content (absent for a factory value), restored on failure. The flags keep their by-presence semantics, sousb.disk0's image path is untouched.verifyUSBCompositionAtreadsidVendor,idProductand the two strings back from configfs after the start and compares them with the request.GET /api/vm/device/virtualaddsidentity(vendorId,productId,manufacturer,product,serialNumber,default);PUTtakes an optionalidentityobject, and a request without it (an older client) keeps the identity in place. IDs0x0001–0xffff, names 1–126 printable ASCII characters without leading or trailing spaces.What a custom ID breaks, and what it does not
docs/usb-composition.mdmatch3346:1009together with the serial number: a host set up with them stops matching a NanoKVM with other IDs. The warning on the page says so./etc/kvm/usb_container_id), not the IDs: unaffected.Checked
Go:
normalizeUSBID(forms, bounds,0x0000), string rules, defaults (the zero identity is the factory one), the files an identity needs, reading/bootwith a damaged file, verification against a fake configfs (old identity refused, new one passes), the transaction writing the files, keeping them when the apply back to the default fails, removing them when it succeeds, the revision; the existing transaction and rollback tests unchanged.go vet,go test -tags teststub ./service/vm/ ./proto/,go build ./....Shell:
tools/test-usb-profiles.sh,tools/test-usb-rebind.sh, shellcheck on the three scripts.Web: presets, matching and validation in
tests/usb-composition.test.mjs;tsc --noEmit, eslint, prettier, the i18n key test.On the stand (script only, no server build):
/boot/usb.manufacturer=Generic,/boot/usb.product=USB Composite Device, then the newS03usbdevstop+start→ configfsstrings/0x409/manufacturer=Generic,product=USB Composite Device, IDs unchanged, all six functions linked; on the Windows hostGet-PnpDeviceProperty … DEVPKEY_Device_BusReportedDeviceDesc=USB Composite Device. Files removed, the installed script'sstop+start→sipeed/NanoKVM, Windows reportsNanoKVMagain, the helper LUN (lun.1) back in place. Note for the reader: the script'srestartaction only rebinds the UDC and does not rewrite the descriptor; the server's apply usesstop+start, which does.Not covered
S03usbhidcarries the same two hunks but was grep- and syntax-checked only; the stand is in the normal profile.🤖 Generated with Claude Code