mirror of
https://github.com/Creased/RCM-Emulator.git
synced 2026-12-06 02:48:11 +01:00
Model USB device mode, with an emulated PC on the cable
Both USB controllers were plain register caches: USBCMD.RESET never cleared, so bdk's usb_device_init() timed out and none of Nyx's USB tools could even wait for a cable. - The Erista USB2 controller (USB1, 0x7D000000, bdk usbd.c): queue heads at ENDPOINTLISTADDR (bdk's are in the controller at +0x1000), transfer descriptors in DRAM, ENDPTPRIME / ENDPTSTATUS / ENDPTCOMPLETE, SETUP packets in queue head 0 with ENDPTSETUPSTAT, a self-clearing controller reset and the PHY's clock-valid bit. Byte and halfword accesses work, since bdk memcpy()s SETUP packets out. - The Mariko XUSB device controller (0x700D0000, bdk xusbd.c): endpoint contexts and TRB rings with Link TRBs and cycle bits, the doorbell, a two-segment event ring with Port Status Change, Setup and Transfer events, EP_HALT / EP_STCHG, EP_RELOAD, and PORTSC's write-1-to-clear changes and link-state writes. - The host (--usb-host, [usb] host, or the config window) plays the PC on whichever controller attaches: debounce, bus reset at high speed, enumeration with SET_ADDRESS recovery, then after 100 ms for its class driver to bind - bdk's XUSB driver stalls an early GET_MAX_LUN - the class. For mass storage it runs Bulk-Only Transport: GET_MAX_LUN, INQUIRY, TEST UNIT READY with REQUEST SENSE for the unit attention, READ CAPACITY, READ(10) of the first 64 and last 8 blocks with their CRC32s, checked against the SD image when the size matches, then allow removal, eject, and poll until the gadget detaches. For HID it reads the report descriptor, sets idle 0 and polls the interrupt endpoint. It only reads, and it runs on the controllers' register accesses, so runs repeat. [usb-host] lines log it; RCM_USB_TRACE=1 logs every descriptor, TRB and doorbell. With hekate 6.5.3, Nyx's SD card mass storage enumerates as 11EC:A7E0, reads back identical to the SD image and ends at "Disk ejected" on both Erista and Mariko; the gamepad enumerates and serves its report descriptor (with no Joy-Cons it has nothing to report). Without a host Nyx waits and reports a timeout, as with no cable. tests/usb/ runs a mass storage gadget over a RAM disk on each controller and a HID gadget on the USB2 one, checks what the host logs - the enumeration, the SCSI commands, CRC32s equal to the payload's, the eject and the detach - and a run with nothing on the cable. The Windows pread/pwrite shim moves from mmio.cpp to platform.h, as the host reads the SD image too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YYewwDPujjo5B2xRyC5Grb
This commit is contained in:
16 files changed
+2342
-34
No files matched your search
@@ -45,14 +45,16 @@ jobs:
|
|||||||
grep -q "RCM Payload Emulator" out.txt \
|
grep -q "RCM Payload Emulator" out.txt \
|
||||||
|| { echo "banner missing - binary did not start"; exit 1; }
|
|| { echo "banner missing - binary did not start"; exit 1; }
|
||||||
|
|
||||||
- name: CCPLEX, SE and display regression tests
|
- name: CCPLEX, SE, USB and display regression tests
|
||||||
# Small self-contained payloads. One boots CPU0 through bdk's
|
# Small self-contained payloads. One boots CPU0 through bdk's
|
||||||
# ccplex_boot_cpu0() register sequence: an unpowered release must be
|
# ccplex_boot_cpu0() register sequence: an unpowered release must be
|
||||||
# refused, a powered one must run an AArch64 blob at EL3, and WFE must
|
# refused, a powered one must run an AArch64 blob at EL3, and WFE must
|
||||||
# park the core. One runs the Security Engine's RSA, SHA-256, AES and
|
# park the core. One runs the Security Engine's RSA, SHA-256, AES and
|
||||||
# RNG against vectors computed in Python. The others drive the
|
# RNG against vectors computed in Python. One brings up USB gadgets on
|
||||||
# display controller and VIC in six scenarios, and the frame the
|
# both device controllers for the --usb-host PC to enumerate and read
|
||||||
# emulator shows must match the picture drawn, pixel for pixel.
|
# back. The others drive the display controller and VIC in six
|
||||||
|
# scenarios, and the frame the emulator shows must match the picture
|
||||||
|
# drawn, pixel for pixel.
|
||||||
run: make test
|
run: make test
|
||||||
|
|
||||||
- name: hwtest end to end
|
- name: hwtest end to end
|
||||||
@@ -71,6 +73,7 @@ jobs:
|
|||||||
path: |
|
path: |
|
||||||
tests/ccplex/out.log
|
tests/ccplex/out.log
|
||||||
tests/se/out.log
|
tests/se/out.log
|
||||||
|
tests/usb/out_*.log
|
||||||
tests/hwtest/out.log
|
tests/hwtest/out.log
|
||||||
if-no-files-found: ignore
|
if-no-files-found: ignore
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ last_fb.rgba
|
|||||||
last_fb.meta
|
last_fb.meta
|
||||||
last_fb*.png
|
last_fb*.png
|
||||||
out.log
|
out.log
|
||||||
|
out_*.log
|
||||||
out*.txt
|
out*.txt
|
||||||
emu_run*.log
|
emu_run*.log
|
||||||
emu_capture*.txt
|
emu_capture*.txt
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ flowchart TB
|
|||||||
mmio --> dc["<b>DC + VIC</b> (inline)<br/>window registers A-D<br/>VIC compose, flip/transpose"]
|
mmio --> dc["<b>DC + VIC</b> (inline)<br/>window registers A-D<br/>VIC compose, flip/transpose"]
|
||||||
mmio --> stubs["<b>inline stubs</b><br/>GPIO, PMC, TSEC, KFUSE,<br/>PWM, CLK, TMR, FUSE, UART"]
|
mmio --> stubs["<b>inline stubs</b><br/>GPIO, PMC, TSEC, KFUSE,<br/>PWM, CLK, TMR, FUSE, UART"]
|
||||||
mmio --> pcie["<b>pcie</b><br/>AFI, root ports, CYW4356<br/>(CPU-complex master only)"]
|
mmio --> pcie["<b>pcie</b><br/>AFI, root ports, CYW4356<br/>(CPU-complex master only)"]
|
||||||
|
mmio --> usb["<b>usb</b><br/>USB2 + XUSB device controllers<br/>and the --usb-host PC"]
|
||||||
|
|
||||||
cfg -.->|writes atomics| state["<b>EmuState</b><br/>(emu_state.h)"]
|
cfg -.->|writes atomics| state["<b>EmuState</b><br/>(emu_state.h)"]
|
||||||
con -.->|RX inject / TX log| state
|
con -.->|RX inject / TX log| state
|
||||||
@@ -529,6 +530,62 @@ All TX append / RX pop is single-threaded -- both the CPU emulation and ImGui
|
|||||||
rendering run on the main thread between SDL polls -- so no locking is
|
rendering run on the main thread between SDL polls -- so no locking is
|
||||||
needed. The 64 KB TX log trim happens in-place during the write hook.
|
needed. The 64 KB TX log trim happens in-place during the write hook.
|
||||||
|
|
||||||
|
### USB device mode (`t210/usb.cpp`)
|
||||||
|
|
||||||
|
bdk runs its gadgets - Nyx's USB mass storage for the SD card, eMMC and
|
||||||
|
emuMMC, and its Joy-Con gamepad - on one of two device controllers, and both
|
||||||
|
are modelled:
|
||||||
|
|
||||||
|
- **Erista: the USB2 controller USB1** (0x7D000000, bdk `usbd.c`), an
|
||||||
|
EHCI-derived device controller. Each endpoint half has a 64-byte queue
|
||||||
|
head at `ENDPOINTLISTADDR` (bdk keeps them in the controller's own memory
|
||||||
|
at +0x1000) and a chain of transfer descriptors in DRAM. `ENDPTPRIME`
|
||||||
|
loads the first descriptor and sets its bit in `ENDPTSTATUS` until the
|
||||||
|
transfer retires; each descriptor it reaches is written back with the
|
||||||
|
bytes it did not move, and the endpoint's bit set in `ENDPTCOMPLETE`. A
|
||||||
|
SETUP lands in queue head 0 with `ENDPTSETUPSTAT` bit 0. `USBCMD.RESET`
|
||||||
|
resets the controller and clears itself; `PHY_CLK_VALID` follows the
|
||||||
|
UTMI PHY's enable and reset. bdk copies SETUP packets out with `memcpy()`,
|
||||||
|
so byte and halfword accesses are handled.
|
||||||
|
- **Mariko: the XUSB device controller** (0x700D0000, bdk `xusbd.c`), the
|
||||||
|
device side of xHCI. Endpoints are device context indexes (0 control, 2
|
||||||
|
bulk OUT, 3 bulk IN), each with a context (the dequeue pointer and DCS in
|
||||||
|
word 2) and a ring of TRBs the driver fills and closes with a Link TRB,
|
||||||
|
ringing the doorbell. The controller posts events on a two-segment event
|
||||||
|
ring, flipping its cycle bit per pass and raising `ST.IP`: a Port Status
|
||||||
|
Change on a bus reset, a Setup event with a sequence number per SETUP, a
|
||||||
|
Transfer event with the residue and SUCCESS or SHORT_PKT per TRB.
|
||||||
|
`EP_HALT` halts an endpoint (`EP_STCHG` reports it), `EP_RELOAD` reloads a
|
||||||
|
context, and a `PORTSC` write with LWS and RxDetect arms the port.
|
||||||
|
|
||||||
|
**The host.** `--usb-host` (or `[usb] host`, or the config window's USB
|
||||||
|
device mode box) plugs in a PC; without it nothing is on the cable and a
|
||||||
|
gadget waits for a connection until it gives up, as with no cable. The host
|
||||||
|
sees whichever controller attaches, waits out the attach debounce, resets
|
||||||
|
the bus at high speed and enumerates: device descriptor, `SET_ADDRESS` (with
|
||||||
|
its 2 ms recovery), configuration, strings, `SET_CONFIGURATION`. It then gives
|
||||||
|
the class driver 100 ms to bind, as a PC does - bdk's XUSB driver stalls a
|
||||||
|
`GET_MAX_LUN` that arrives before the gadget asks for it - and runs the
|
||||||
|
class:
|
||||||
|
|
||||||
|
- *Mass storage (Bulk-Only Transport):* `GET_MAX_LUN`, INQUIRY, TEST UNIT
|
||||||
|
READY (answering the first unit attention with REQUEST SENSE), READ
|
||||||
|
CAPACITY, READ(10) of the first 64 and the last 8 blocks, each logged with
|
||||||
|
its CRC32 and checked against the SD image when the disk is its size; then
|
||||||
|
PREVENT ALLOW MEDIUM REMOVAL, an eject (START STOP UNIT, LoEj) and TEST
|
||||||
|
UNIT READY every 500 ms until the gadget detaches, as Linux does. It only
|
||||||
|
reads.
|
||||||
|
- *HID:* the report descriptor, `SET_IDLE` with an idle rate of 0 (report on
|
||||||
|
change), then the interrupt IN endpoint is polled. Nyx's gamepad has
|
||||||
|
nothing to report without Joy-Cons.
|
||||||
|
|
||||||
|
The host advances on every access to either controller - it runs as far as
|
||||||
|
the device and the emulated clock allow - so it needs no thread or main-loop
|
||||||
|
hook and runs repeat exactly. It logs `[usb-host]` lines; `RCM_USB_TRACE=1`
|
||||||
|
also logs each transfer descriptor, TRB and doorbell. Nyx's SD card mass
|
||||||
|
storage reads back identical to the image and reports "Disk ejected" on
|
||||||
|
both SoCs.
|
||||||
|
|
||||||
### Other peripherals (mostly stubs)
|
### Other peripherals (mostly stubs)
|
||||||
|
|
||||||
- **CAR and MC.** Registers without behaviour of their own read back what
|
- **CAR and MC.** Registers without behaviour of their own read back what
|
||||||
@@ -722,8 +779,11 @@ emulated second.
|
|||||||
refusal, the EL3 entry, a core released with its clock stopped, the
|
refusal, the EL3 entry, a core released with its clock stopped, the
|
||||||
mailbox, WFE parking and the reset;
|
mailbox, WFE parking and the reset;
|
||||||
`tests/se/`, which runs the SE's RSA, SHA-256, AES and RNG against vectors
|
`tests/se/`, which runs the SE's RSA, SHA-256, AES and RNG against vectors
|
||||||
computed in Python (`gen_vectors.py` writes `vectors.h`); and
|
computed in Python (`gen_vectors.py` writes `vectors.h`); `tests/usb/`, a
|
||||||
`tests/display/`, one payload built per scenario that drives the display
|
mass storage gadget over a RAM disk on each device controller and a HID
|
||||||
|
gadget, which the `--usb-host` PC must enumerate, read back (the CRC32s it
|
||||||
|
logs match the payload's) and see detach, plus a run with nothing on the
|
||||||
|
cable; and `tests/display/`, one payload built per scenario that drives the display
|
||||||
controller and VIC the way bdk does (a pitch window; Nyx's VIC turn into
|
controller and VIC the way bdk does (a pitch window; Nyx's VIC turn into
|
||||||
pitch and into block-linear surfaces; a block-linear VIC source; the DC's
|
pitch and into block-linear surfaces; a block-linear VIC source; the DC's
|
||||||
own column scan with an address offset; a mirrored window with window D
|
own column scan with an address offset; a mirrored window with window D
|
||||||
@@ -747,6 +807,10 @@ don't repeat the diagnosis:
|
|||||||
real console's value taken after a payload had brought the display up, so
|
real console's value taken after a payload had brought the display up, so
|
||||||
`display_init()` found DISP1 already clocked at RCM entry. Fix: the bank
|
`display_init()` found DISP1 already clocked at RCM entry. Fix: the bank
|
||||||
starts at the TRM reset value and follows the payload's writes.
|
starts at the TRM reset value and follows the payload's writes.
|
||||||
|
- **Every USB gadget failed to start.** The USB controllers were plain
|
||||||
|
register caches, so `USBCMD.RESET` never cleared and bdk's
|
||||||
|
`usb_device_init()` timed out; Nyx's USB tools could not even wait for a
|
||||||
|
cable. Fix: both device controllers are modelled, with a PC to plug in.
|
||||||
- **Minerva hung Nyx** (hekate 6.5.3 with the stock `bootloader/` folder
|
- **Minerva hung Nyx** (hekate 6.5.3 with the stock `bootloader/` folder
|
||||||
never reached its GUI). The emulator planted hekate's "watchdog fired"
|
never reached its GUI). The emulator planted hekate's "watchdog fired"
|
||||||
cookie in IRAM so the IPL would skip Minerva; Nyx then trained the DRAM
|
cookie in IRAM so the IPL would skip Minerva; Nyx then trained the DRAM
|
||||||
|
|||||||
@@ -102,6 +102,12 @@ tests/se/payload.bin: tests/se/payload.c tests/se/vectors.h tests/se/link.ld
|
|||||||
$(TEST_CC) $(TEST_CFLAGS) -T tests/se/link.ld -o tests/se/payload.elf $<
|
$(TEST_CC) $(TEST_CFLAGS) -T tests/se/link.ld -o tests/se/payload.elf $<
|
||||||
$(TEST_OBJCOPY) -O binary tests/se/payload.elf $@
|
$(TEST_OBJCOPY) -O binary tests/se/payload.elf $@
|
||||||
|
|
||||||
|
USB_PAYLOADS = tests/usb/payload_1.bin tests/usb/payload_2.bin tests/usb/payload_3.bin
|
||||||
|
|
||||||
|
tests/usb/payload_%.bin: tests/usb/payload.c tests/usb/link.ld
|
||||||
|
$(TEST_CC) $(TEST_CFLAGS) -DSCENARIO=$* -T tests/usb/link.ld -o tests/usb/payload_$*.elf $<
|
||||||
|
$(TEST_OBJCOPY) -O binary tests/usb/payload_$*.elf $@
|
||||||
|
|
||||||
DISPLAY_SCENARIOS = 1 2 3 4 5 6
|
DISPLAY_SCENARIOS = 1 2 3 4 5 6
|
||||||
DISPLAY_PAYLOADS = $(DISPLAY_SCENARIOS:%=tests/display/payload_%.bin)
|
DISPLAY_PAYLOADS = $(DISPLAY_SCENARIOS:%=tests/display/payload_%.bin)
|
||||||
|
|
||||||
@@ -109,14 +115,16 @@ tests/display/payload_%.bin: tests/display/payload.c tests/display/link.ld
|
|||||||
$(TEST_CC) $(TEST_CFLAGS) -DSCENARIO=$* -T tests/display/link.ld -o tests/display/payload_$*.elf $<
|
$(TEST_CC) $(TEST_CFLAGS) -DSCENARIO=$* -T tests/display/link.ld -o tests/display/payload_$*.elf $<
|
||||||
$(TEST_OBJCOPY) -O binary tests/display/payload_$*.elf $@
|
$(TEST_OBJCOPY) -O binary tests/display/payload_$*.elf $@
|
||||||
|
|
||||||
test: $(OUTPUT) tests/ccplex/payload.bin tests/se/payload.bin $(DISPLAY_PAYLOADS)
|
test: $(OUTPUT) tests/ccplex/payload.bin tests/se/payload.bin $(USB_PAYLOADS) $(DISPLAY_PAYLOADS)
|
||||||
sh tests/ccplex/run.sh ./$(OUTPUT) tests/ccplex/payload.bin
|
sh tests/ccplex/run.sh ./$(OUTPUT) tests/ccplex/payload.bin
|
||||||
sh tests/se/run.sh ./$(OUTPUT) tests/se/payload.bin
|
sh tests/se/run.sh ./$(OUTPUT) tests/se/payload.bin
|
||||||
|
sh tests/usb/run.sh ./$(OUTPUT) tests/usb
|
||||||
sh tests/display/run.sh ./$(OUTPUT) tests/display
|
sh tests/display/run.sh ./$(OUTPUT) tests/display
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OUTPUT) $(OBJS) $(OBJS:.o=.d) \
|
rm -f $(OUTPUT) $(OBJS) $(OBJS:.o=.d) \
|
||||||
tests/ccplex/payload.elf tests/ccplex/payload.bin tests/ccplex/out.log \
|
tests/ccplex/payload.elf tests/ccplex/payload.bin tests/ccplex/out.log \
|
||||||
tests/se/payload.elf tests/se/payload.bin tests/se/out.log \
|
tests/se/payload.elf tests/se/payload.bin tests/se/out.log \
|
||||||
|
tests/usb/payload_*.elf tests/usb/payload_*.bin tests/usb/out_*.log \
|
||||||
tests/display/payload_*.elf tests/display/payload_*.bin \
|
tests/display/payload_*.elf tests/display/payload_*.bin \
|
||||||
$(WIN_OUT) $(WIN_OBJS) $(WIN_OBJS:.o=.d)
|
$(WIN_OUT) $(WIN_OBJS) $(WIN_OBJS:.o=.d)
|
||||||
@@ -30,8 +30,12 @@ slow or destructive.
|
|||||||
`--auto-te-script`, so a payload flow can be exercised from a CI run or a
|
`--auto-te-script`, so a payload flow can be exercised from a CI run or a
|
||||||
one-shot script. `--input-script` scripts buttons and touchscreen taps for
|
one-shot script. `--input-script` scripts buttons and touchscreen taps for
|
||||||
any payload.
|
any payload.
|
||||||
|
- Plug an emulated PC into the USB-C port (`--usb-host`): Nyx's USB mass
|
||||||
|
storage and gamepad gadgets enumerate on both the Erista (USB2) and Mariko
|
||||||
|
(XUSB) device controllers, and the PC reads a mass storage disk back,
|
||||||
|
checks it against the SD image and ejects it.
|
||||||
- Tweak emulated hardware live (battery, charger, thermal, USB-PD, PMIC,
|
- Tweak emulated hardware live (battery, charger, thermal, USB-PD, PMIC,
|
||||||
fuses, SD insertion) from a side window. See [Live hardware tweaks](#live-hardware-tweaks).
|
fuses, SD insertion, the USB host) from a side window. See [Live hardware tweaks](#live-hardware-tweaks).
|
||||||
- Inspect each Tegra UART port (TX history) and inject keystrokes into the
|
- Inspect each Tegra UART port (TX history) and inject keystrokes into the
|
||||||
payload's RX FIFO from a separate console window. See [UART console](#uart-console).
|
payload's RX FIFO from a separate console window. See [UART console](#uart-console).
|
||||||
- Read real eMMC (embedded MMC) dumps (`BOOT0`, multi-part `rawnand.bin.NN`)
|
- Read real eMMC (embedded MMC) dumps (`BOOT0`, multi-part `rawnand.bin.NN`)
|
||||||
@@ -177,15 +181,20 @@ real consoles. Details in [DESIGN.md](DESIGN.md#ccplex-cpu0).
|
|||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
make test # CCPLEX, SE + display regression payloads (arm-none-eabi-gcc, python3)
|
make test # CCPLEX, SE, USB + display regression payloads (arm-none-eabi-gcc, python3)
|
||||||
tests/hwtest/build.sh build-hwtest # build hwtest-rcm (+ gcc-aarch64-linux-gnu)
|
tests/hwtest/build.sh build-hwtest # build hwtest-rcm (+ gcc-aarch64-linux-gnu)
|
||||||
tests/hwtest/run.sh ./rcm_emu build-hwtest/hwtest-rcm/build/hwtest.bin
|
tests/hwtest/run.sh ./rcm_emu build-hwtest/hwtest-rcm/build/hwtest.bin
|
||||||
```
|
```
|
||||||
|
|
||||||
The first boots CPU0 from a small self-contained payload and checks the
|
`make test` runs small self-contained payloads: one boots CPU0 and checks the
|
||||||
refusal of an unpowered release, the EL3 entry, the mailbox, WFE parking and
|
refusal of an unpowered release, the EL3 entry, the mailbox, a stopped clock,
|
||||||
the reset. The second runs hwtest-rcm's whole hardware sweep headless and
|
WFE parking and the reset; one checks the Security Engine's RSA, SHA-256, AES
|
||||||
checks that the CPU0-driven Wi-Fi probe enumerates the endpoint. CI runs both.
|
and RNG against vectors computed in Python; one brings up a USB mass storage
|
||||||
|
gadget on each device controller (and a HID gadget) for the `--usb-host` PC to
|
||||||
|
enumerate and read back; one drives the display controller and VIC and
|
||||||
|
compares the frame pixel for pixel. The hwtest scripts run hwtest-rcm's whole
|
||||||
|
hardware sweep headless and check that the CPU0-driven Wi-Fi probe enumerates
|
||||||
|
the endpoint. CI runs both.
|
||||||
|
|
||||||
## Live hardware tweaks
|
## Live hardware tweaks
|
||||||
|
|
||||||
@@ -279,6 +288,7 @@ needed.
|
|||||||
| `--oem` | `erista` \| `mariko` | Switch SoC generation. Drives `APB_MISC_GP_HIDREV` so Hekate's `h_cfg.t210b01` matches and pkg1 identification skips the right OEM header. Default `erista`. |
|
| `--oem` | `erista` \| `mariko` | Switch SoC generation. Drives `APB_MISC_GP_HIDREV` so Hekate's `h_cfg.t210b01` matches and pkg1 identification skips the right OEM header. Default `erista`. |
|
||||||
| `--bt-radio` | `healthy` \| `faulty` \| `absent` | Broadcom CYW4356 behaviour on UART-D. `healthy` powers up on the `BT_REG_ON` edge, holds `BT_HOST_WAKE` high, asserts `RTS_N` and answers HCI; `faulty` reproduces the 2110-1118 console (module fitted, never leaves POR); `absent` additionally leaves `BT_UART_RXD` in a break condition. Overrides `[bluetooth] radio` in the ini. Default `healthy`. |
|
| `--bt-radio` | `healthy` \| `faulty` \| `absent` | Broadcom CYW4356 behaviour on UART-D. `healthy` powers up on the `BT_REG_ON` edge, holds `BT_HOST_WAKE` high, asserts `RTS_N` and answers HCI; `faulty` reproduces the 2110-1118 console (module fitted, never leaves POR); `absent` additionally leaves `BT_UART_RXD` in a break condition. Overrides `[bluetooth] radio` in the ini. Default `healthy`. |
|
||||||
| `--wifi-radio` | `healthy` \| `faulty` \| `absent` | The WLAN half of the same CYW4356: a PCI Express endpoint on root port 1, reachable only from CPU0 (the BPMP reads all ones, as on silicon). `healthy` trains the link, enumerates as `14E4:43EC` and answers a ChipCommon ChipID read with `0x4356`; `faulty` still trains and enumerates but reads all ones on the backplane (live PCIe front-end, dead radio die); `absent` never leaves detect. The model also enforces the datasheet's power-up ordering, so a payload that releases PERST# too early gets a link that stays down and a `[pcie]` line naming the reason. Overrides `[wifi] radio` in the ini. Default `healthy`. |
|
| `--wifi-radio` | `healthy` \| `faulty` \| `absent` | The WLAN half of the same CYW4356: a PCI Express endpoint on root port 1, reachable only from CPU0 (the BPMP reads all ones, as on silicon). `healthy` trains the link, enumerates as `14E4:43EC` and answers a ChipCommon ChipID read with `0x4356`; `faulty` still trains and enumerates but reads all ones on the backplane (live PCIe front-end, dead radio die); `absent` never leaves detect. The model also enforces the datasheet's power-up ordering, so a payload that releases PERST# too early gets a link that stays down and a `[pcie]` line naming the reason. Overrides `[wifi] radio` in the ini. Default `healthy`. |
|
||||||
|
| `--usb-host` | (none) | A PC on the USB-C port. It enumerates whatever USB gadget the payload brings up; a mass storage disk is read back (checked against the SD image), then ejected, and HID reports are polled. Logged as `[usb-host]`; `RCM_USB_TRACE=1` traces every transfer. Overrides `[usb] host` in the ini. |
|
||||||
| `--input-script` | file or spec | Scripted input keyed to emulated time: `<ms> P\|U\|D [hold_ms]` presses POWER / VOL+ / VOL-, `<ms> TAP <x> <y> [hold_ms]` taps the touchscreen at (x, y) in the 1280x720 picture, `+N` times an event N ms after the previous one. Events are separated by `,`, `;` or newlines. |
|
| `--input-script` | file or spec | Scripted input keyed to emulated time: `<ms> P\|U\|D [hold_ms]` presses POWER / VOL+ / VOL-, `<ms> TAP <x> <y> [hold_ms]` taps the touchscreen at (x, y) in the 1280x720 picture, `+N` times an event N ms after the previous one. Events are separated by `,`, `;` or newlines. |
|
||||||
| `--auto-pin-recovery` | (none) | Drive the Lockpick PIN-recovery menu without user input. |
|
| `--auto-pin-recovery` | (none) | Drive the Lockpick PIN-recovery menu without user input. |
|
||||||
| `--auto-te-script` | (none) | Drive `recover_pin.te` in TegraExplorer without input. |
|
| `--auto-te-script` | (none) | Drive `recover_pin.te` in TegraExplorer without input. |
|
||||||
@@ -291,13 +301,14 @@ flowchart LR
|
|||||||
root --> top["main.cpp<br/>emu_state.h<br/>Makefile<br/>Dockerfile<br/>README.md / DESIGN.md"]
|
root --> top["main.cpp<br/>emu_state.h<br/>Makefile<br/>Dockerfile<br/>README.md / DESIGN.md"]
|
||||||
root --> t210["t210/<br/>(SoC peripheral models)"]
|
root --> t210["t210/<br/>(SoC peripheral models)"]
|
||||||
root --> display["display/"]
|
root --> display["display/"]
|
||||||
root --> tests["tests/<br/>ccplex/, se/, display/, hwtest/"]
|
root --> tests["tests/<br/>ccplex/, se/, usb/, display/, hwtest/"]
|
||||||
|
|
||||||
t210 --> mmio["mmio.{h,cpp}<br/>memory_map.h<br/>regcache.h, tegra_bl.h"]
|
t210 --> mmio["mmio.{h,cpp}<br/>memory_map.h<br/>regcache.h, tegra_bl.h"]
|
||||||
t210 --> cpus["bpmp.{h,cpp} BPMP clock<br/>ccplex.{h,cpp} CPU0 (A57)"]
|
t210 --> cpus["bpmp.{h,cpp} BPMP clock<br/>ccplex.{h,cpp} CPU0 (A57)"]
|
||||||
t210 --> pcie["pcie.{h,cpp}<br/>root complex + CYW4356"]
|
t210 --> pcie["pcie.{h,cpp}<br/>root complex + CYW4356"]
|
||||||
t210 --> se["se_engine.{h,cpp}<br/>AES-128, SHA-256, RSA, RNG"]
|
t210 --> se["se_engine.{h,cpp}<br/>AES-128, SHA-256, RSA, RNG"]
|
||||||
t210 --> i2c["i2c3.{h,cpp}<br/>STMFTS / FTS4 touch"]
|
t210 --> i2c["i2c3.{h,cpp}<br/>STMFTS / FTS4 touch"]
|
||||||
|
t210 --> usb["usb.{h,cpp}<br/>USB device controllers + host PC"]
|
||||||
|
|
||||||
display --> sdl["sdl_display.{h,cpp}<br/>DC window scan-out<br/>+ SDL2"]
|
display --> sdl["sdl_display.{h,cpp}<br/>DC window scan-out<br/>+ SDL2"]
|
||||||
```
|
```
|
||||||
@@ -321,6 +332,9 @@ For implementation details, see [DESIGN.md](DESIGN.md).
|
|||||||
seed, so runs repeat.
|
seed, so runs repeat.
|
||||||
- **Single-threaded.** Both cores run in batches on the main thread between
|
- **Single-threaded.** Both cores run in batches on the main thread between
|
||||||
SDL event polls. That is also what keeps them deterministic.
|
SDL event polls. That is also what keeps them deterministic.
|
||||||
|
- **The USB host is scripted.** `--usb-host` runs a fixed session - enumerate,
|
||||||
|
read back, eject - rather than bridging the gadget to the machine running
|
||||||
|
the emulator, so the disk cannot be mounted on it.
|
||||||
|
|
||||||
## Acknowledgements
|
## Acknowledgements
|
||||||
|
|
||||||
|
|||||||
@@ -138,6 +138,7 @@ void reset_to_defaults(EmuState *s) {
|
|||||||
X(usb_pd_inserted, "usb_pd", "inserted", 0) \
|
X(usb_pd_inserted, "usb_pd", "inserted", 0) \
|
||||||
X(usb_pd_voltage_mv, "usb_pd", "voltage_mv", 0) \
|
X(usb_pd_voltage_mv, "usb_pd", "voltage_mv", 0) \
|
||||||
X(usb_pd_amperage_ma, "usb_pd", "amperage_ma", 0) \
|
X(usb_pd_amperage_ma, "usb_pd", "amperage_ma", 0) \
|
||||||
|
X(usb_host, "usb", "host", 0) \
|
||||||
/* SoC / PMIC */ \
|
/* SoC / PMIC */ \
|
||||||
X(pmic_otp, "soc", "pmic_otp", 0) \
|
X(pmic_otp, "soc", "pmic_otp", 0) \
|
||||||
X(is_mariko, "soc", "is_mariko", 0) \
|
X(is_mariko, "soc", "is_mariko", 0) \
|
||||||
@@ -645,6 +646,15 @@ void build_ui(EmuState *state) {
|
|||||||
atomic_slider_int<uint16_t>("PDO amperage (mA)", state->usb_pd_amperage_ma, 500, 3000, "%d mA");
|
atomic_slider_int<uint16_t>("PDO amperage (mA)", state->usb_pd_amperage_ma, 500, 3000, "%d mA");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ImGui::CollapsingHeader("USB device mode")) {
|
||||||
|
// The PC in t210/usb.cpp: it enumerates a gadget the payload brings up.
|
||||||
|
bool host = state->usb_host.load();
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
|
||||||
if (ImGui::CollapsingHeader("Display")) {
|
if (ImGui::CollapsingHeader("Display")) {
|
||||||
int bl = (int)state->backlight;
|
int bl = (int)state->backlight;
|
||||||
if (ImGui::SliderInt("Backlight", &bl, 0, 255)) {
|
if (ImGui::SliderInt("Backlight", &bl, 0, 255)) {
|
||||||
|
|||||||
@@ -320,6 +320,10 @@ struct EmuState {
|
|||||||
// SD card insertion (GPIO Port Z bit 1 = 0 means inserted).
|
// SD card insertion (GPIO Port Z bit 1 = 0 means inserted).
|
||||||
std::atomic<bool> sd_inserted{true};
|
std::atomic<bool> sd_inserted{true};
|
||||||
|
|
||||||
|
// A PC on the USB-C port (--usb-host): it enumerates whatever gadget the
|
||||||
|
// payload brings up (t210/usb.cpp). Off, nothing is on the cable.
|
||||||
|
std::atomic<bool> usb_host{false};
|
||||||
|
|
||||||
// SD card identity (returned for SDMMC1 CMD2 ALL_SEND_CID).
|
// SD card identity (returned for SDMMC1 CMD2 ALL_SEND_CID).
|
||||||
// Hekate parses these out of the R2 response per bdk/storage/sdmmc.c
|
// Hekate parses these out of the R2 response per bdk/storage/sdmmc.c
|
||||||
// _sd_storage_parse_cid; the I2C handler builds the 16-byte CID payload
|
// _sd_storage_parse_cid; the I2C handler builds the 16-byte CID payload
|
||||||
|
|||||||
@@ -356,6 +356,11 @@ int main(int argc, char *argv[]) {
|
|||||||
fprintf(stderr, "[emu] Unknown --bt-radio value '%s'; expected"
|
fprintf(stderr, "[emu] Unknown --bt-radio value '%s'; expected"
|
||||||
" 'healthy', 'faulty' or 'absent'\n", mode);
|
" 'healthy', 'faulty' or 'absent'\n", mode);
|
||||||
}
|
}
|
||||||
|
} 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.
|
||||||
|
state.usb_host = true;
|
||||||
|
printf("[emu] USB host connected [overrides ini]\n");
|
||||||
} else if (strcmp(argv[i], "--wifi-radio") == 0 && i + 1 < argc) {
|
} else if (strcmp(argv[i], "--wifi-radio") == 0 && i + 1 < argc) {
|
||||||
// WLAN half of the same package, on PCIe. 'faulty' means
|
// WLAN half of the same package, on PCIe. 'faulty' means
|
||||||
// something different here from the Bluetooth side: the link
|
// something different here from the Bluetooth side: the link
|
||||||
|
|||||||
+20
@@ -36,6 +36,26 @@ static inline int64_t file_size64(int fd) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pread/pwrite are POSIX and MinGW does not have them. Seek-then-read is
|
||||||
|
// equivalent here: the emulated storage is touched only from the CPU thread,
|
||||||
|
// so the atomicity real pread() buys against a shared file offset is not in
|
||||||
|
// play.
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <io.h>
|
||||||
|
static inline ssize_t pread(int fd, void *buf, size_t n, long long off) {
|
||||||
|
if (_lseeki64(fd, off, SEEK_SET) < 0)
|
||||||
|
return -1;
|
||||||
|
return _read(fd, buf, (unsigned int)n);
|
||||||
|
}
|
||||||
|
static inline ssize_t pwrite(int fd, const void *buf, size_t n, long long off) {
|
||||||
|
if (_lseeki64(fd, off, SEEK_SET) < 0)
|
||||||
|
return -1;
|
||||||
|
return _write(fd, buf, (unsigned int)n);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
// Large zero-filled memory that only becomes resident where it is touched
|
// Large zero-filled memory that only becomes resident where it is touched
|
||||||
// (platform.cpp). zeroed_reset() makes all of it zero again, at the same
|
// (platform.cpp). zeroed_reset() makes all of it zero again, at the same
|
||||||
// address, without touching it.
|
// address, without touching it.
|
||||||
|
|||||||
@@ -193,6 +193,16 @@ constexpr uint64_t SE_SIZE = 0x2000;
|
|||||||
constexpr uint64_t MSELECT_BASE = 0x50060000;
|
constexpr uint64_t MSELECT_BASE = 0x50060000;
|
||||||
constexpr uint64_t MSELECT_SIZE = 0x1000;
|
constexpr uint64_t MSELECT_SIZE = 0x1000;
|
||||||
|
|
||||||
|
// USB2 controller USB1 ("OTG"): the device controller bdk's usbd.c drives on
|
||||||
|
// Erista. Registers, the UTMI PHY at +0x400/+0x800, queue heads at +0x1000.
|
||||||
|
constexpr uint64_t USB_OTG_BASE = 0x7D000000;
|
||||||
|
constexpr uint64_t USB_OTG_SIZE = 0x4000;
|
||||||
|
|
||||||
|
// XUSB device controller: bdk's xusbd.c on Mariko. XHCI registers, then its
|
||||||
|
// PCI configuration space at +0x8000 and device registers at +0x9000.
|
||||||
|
constexpr uint64_t XUSB_DEV_BASE = 0x700D0000;
|
||||||
|
constexpr uint64_t XUSB_DEV_SIZE = 0xA000;
|
||||||
|
|
||||||
// XUSB pad controller. Owns the UPHY SERDES lanes shared by PCIe (pcie-0..4)
|
// XUSB pad controller. Owns the UPHY SERDES lanes shared by PCIe (pcie-0..4)
|
||||||
// and USB 3.0 SuperSpeed (pcie-5/6 on this board), plus UPHY PLL P0.
|
// and USB 3.0 SuperSpeed (pcie-5/6 on this board), plus UPHY PLL P0.
|
||||||
constexpr uint64_t XUSB_PADCTL_BASE = 0x7009F000;
|
constexpr uint64_t XUSB_PADCTL_BASE = 0x7009F000;
|
||||||
|
|||||||
+12
-20
@@ -8,6 +8,7 @@
|
|||||||
#include "bpmp.h"
|
#include "bpmp.h"
|
||||||
#include "ccplex.h"
|
#include "ccplex.h"
|
||||||
#include "pcie.h"
|
#include "pcie.h"
|
||||||
|
#include "usb.h"
|
||||||
#include "regcache.h"
|
#include "regcache.h"
|
||||||
#include "se_engine.h"
|
#include "se_engine.h"
|
||||||
#include "tegra_bl.h"
|
#include "tegra_bl.h"
|
||||||
@@ -24,26 +25,6 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
// pread/pwrite are POSIX and MinGW does not have them, so the SD/eMMC image
|
|
||||||
// access below would not compile for Windows. Seek-then-read is equivalent
|
|
||||||
// here: the emulated storage is touched only from the CPU thread, so the
|
|
||||||
// atomicity real pread() buys against a shared file offset is not in play.
|
|
||||||
// Kept next to the includes rather than in a header because these four call
|
|
||||||
// sites are the only users in the tree.
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <io.h>
|
|
||||||
static inline ssize_t pread(int fd, void *buf, size_t n, long long off) {
|
|
||||||
if (_lseeki64(fd, off, SEEK_SET) < 0)
|
|
||||||
return -1;
|
|
||||||
return _read(fd, buf, (unsigned int)n);
|
|
||||||
}
|
|
||||||
static inline ssize_t pwrite(int fd, const void *buf, size_t n, long long off) {
|
|
||||||
if (_lseeki64(fd, off, SEEK_SET) < 0)
|
|
||||||
return -1;
|
|
||||||
return _write(fd, buf, (unsigned int)n);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define BIT(n) (1U << (n))
|
#define BIT(n) (1U << (n))
|
||||||
|
|
||||||
static RegCache mmio_regs;
|
static RegCache mmio_regs;
|
||||||
@@ -4618,6 +4599,10 @@ uint32_t mmio_bus_read(EmuState *state, uint64_t address, unsigned size) {
|
|||||||
if (address >= XUSB_PADCTL_BASE &&
|
if (address >= XUSB_PADCTL_BASE &&
|
||||||
address < XUSB_PADCTL_BASE + XUSB_PADCTL_SIZE)
|
address < XUSB_PADCTL_BASE + XUSB_PADCTL_SIZE)
|
||||||
return padctl_read(state, address);
|
return padctl_read(state, address);
|
||||||
|
if (address >= USB_OTG_BASE && address < USB_OTG_BASE + USB_OTG_SIZE)
|
||||||
|
return usb2d_read(state, address, size);
|
||||||
|
if (address >= XUSB_DEV_BASE && address < XUSB_DEV_BASE + XUSB_DEV_SIZE)
|
||||||
|
return xusbd_read(state, address, size);
|
||||||
if (address >= MSELECT_BASE && address < MSELECT_BASE + MSELECT_SIZE)
|
if (address >= MSELECT_BASE && address < MSELECT_BASE + MSELECT_SIZE)
|
||||||
return mselect_read(state, address);
|
return mselect_read(state, address);
|
||||||
if (address >= VIC_BASE && address < VIC_BASE + VIC_SIZE)
|
if (address >= VIC_BASE && address < VIC_BASE + VIC_SIZE)
|
||||||
@@ -4777,6 +4762,10 @@ void mmio_bus_write(EmuState *state, uint64_t address, unsigned size,
|
|||||||
} else if (address >= XUSB_PADCTL_BASE &&
|
} else if (address >= XUSB_PADCTL_BASE &&
|
||||||
address < XUSB_PADCTL_BASE + XUSB_PADCTL_SIZE) {
|
address < XUSB_PADCTL_BASE + XUSB_PADCTL_SIZE) {
|
||||||
padctl_write(state, address, val);
|
padctl_write(state, address, val);
|
||||||
|
} else if (address >= USB_OTG_BASE && address < USB_OTG_BASE + USB_OTG_SIZE) {
|
||||||
|
usb2d_write(state, address, size, val);
|
||||||
|
} else if (address >= XUSB_DEV_BASE && address < XUSB_DEV_BASE + XUSB_DEV_SIZE) {
|
||||||
|
xusbd_write(state, address, size, val);
|
||||||
} else if (address >= MSELECT_BASE && address < MSELECT_BASE + MSELECT_SIZE) {
|
} else if (address >= MSELECT_BASE && address < MSELECT_BASE + MSELECT_SIZE) {
|
||||||
mselect_write(state, address, val);
|
mselect_write(state, address, val);
|
||||||
} else if ((address >= SDMMC1_BASE && address < SDMMC1_BASE + 0x200) ||
|
} else if ((address >= SDMMC1_BASE && address < SDMMC1_BASE + 0x200) ||
|
||||||
@@ -4947,6 +4936,8 @@ const struct {
|
|||||||
{SYSCTR0_BASE, SYSCTR0_SIZE},
|
{SYSCTR0_BASE, SYSCTR0_SIZE},
|
||||||
{BPMP_CACHE_BASE, BPMP_CACHE_SIZE},
|
{BPMP_CACHE_BASE, BPMP_CACHE_SIZE},
|
||||||
{XUSB_PADCTL_BASE, XUSB_PADCTL_SIZE},
|
{XUSB_PADCTL_BASE, XUSB_PADCTL_SIZE},
|
||||||
|
{USB_OTG_BASE, USB_OTG_SIZE},
|
||||||
|
{XUSB_DEV_BASE, XUSB_DEV_SIZE},
|
||||||
{MSELECT_BASE, MSELECT_SIZE},
|
{MSELECT_BASE, MSELECT_SIZE},
|
||||||
// PCIe. T210 puts the root complex at the BOTTOM of the address map,
|
// PCIe. T210 puts the root complex at the BOTTOM of the address map,
|
||||||
// below every other peripheral.
|
// below every other peripheral.
|
||||||
@@ -5056,6 +5047,7 @@ void mmio_soft_reset(EmuState *state, bool power_cycle) {
|
|||||||
pcie_reset(state);
|
pcie_reset(state);
|
||||||
se_engine_reset();
|
se_engine_reset();
|
||||||
i2c3_reset(state);
|
i2c3_reset(state);
|
||||||
|
usb_reset(state);
|
||||||
|
|
||||||
mmio_regs.clear();
|
mmio_regs.clear();
|
||||||
for (auto &u : uart_ports)
|
for (auto &u : uart_ports)
|
||||||
|
|||||||
+1464
File diff suppressed because it is too large.
Load diff
+18
@@ -0,0 +1,18 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
struct EmuState;
|
||||||
|
|
||||||
|
// USB device mode (see usb.cpp): the Erista USB2 device controller, and the
|
||||||
|
// PC a --usb-host run plugs into it.
|
||||||
|
uint32_t usb2d_read(EmuState *state, uint64_t addr, unsigned size);
|
||||||
|
void usb2d_write(EmuState *state, uint64_t addr, unsigned size, uint32_t val);
|
||||||
|
|
||||||
|
// The Mariko XUSB device controller (XHCI registers, PCI config at +0x8000,
|
||||||
|
// the device block at +0x9000).
|
||||||
|
uint32_t xusbd_read(EmuState *state, uint64_t addr, unsigned size);
|
||||||
|
void xusbd_write(EmuState *state, uint64_t addr, unsigned size, uint32_t val);
|
||||||
|
|
||||||
|
// SoC reset: the controller back to its reset state, the host unplugged.
|
||||||
|
void usb_reset(EmuState *state);
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
/* Loaded and entered at 0x40010000, like any RCM payload. */
|
||||||
|
ENTRY(_start)
|
||||||
|
SECTIONS {
|
||||||
|
. = 0x40010000;
|
||||||
|
.text : { KEEP(*(.text.start)) *(.text*) }
|
||||||
|
.rodata : { *(.rodata*) }
|
||||||
|
.data : { *(.data*) }
|
||||||
|
.bss : { *(.bss*) *(COMMON) }
|
||||||
|
/DISCARD/ : { *(.comment) *(.note*) *(.ARM.attributes) *(.ARM.exidx*) }
|
||||||
|
}
|
||||||
@@ -0,0 +1,613 @@
|
|||||||
|
/*
|
||||||
|
* USB device mode regression payload.
|
||||||
|
*
|
||||||
|
* A freestanding BPMP payload that brings up a small USB mass storage gadget
|
||||||
|
* - one LUN, a 128-block RAM disk - on one of the two device controllers,
|
||||||
|
* with the register sequences bdk's drivers use, and serves whatever the
|
||||||
|
* emulator's --usb-host PC asks. Built once per scenario with -DSCENARIO=n:
|
||||||
|
*
|
||||||
|
* 1 the USB2 controller USB1 (bdk usbd.c, Erista): queue heads in the
|
||||||
|
* controller at +0x1000, transfer descriptors in DRAM
|
||||||
|
* 2 the XUSB device controller (bdk xusbd.c, Mariko): event ring,
|
||||||
|
* transfer rings and endpoint contexts in IRAM
|
||||||
|
* 3 the USB2 controller again, as a HID gadget: after the report
|
||||||
|
* descriptor and SET_IDLE it sends three interrupt IN reports
|
||||||
|
*
|
||||||
|
* Either way the gadget answers the standard requests and GET_MAX_LUN, then
|
||||||
|
* the Bulk-Only Transport: INQUIRY, TEST UNIT READY, REQUEST SENSE, READ
|
||||||
|
* CAPACITY, READ(10), PREVENT ALLOW MEDIUM REMOVAL and START STOP UNIT. When
|
||||||
|
* the host ejects, it stops the controller and powers off. It prints the
|
||||||
|
* CRC32 of the disk ranges the host reads, which tests/usb/run.sh compares
|
||||||
|
* with what the host logs. Run without --usb-host, scenario 1 sees no bus
|
||||||
|
* reset and says so.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef SCENARIO
|
||||||
|
#define SCENARIO 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef unsigned int u32;
|
||||||
|
typedef unsigned short u16;
|
||||||
|
typedef unsigned char u8;
|
||||||
|
|
||||||
|
#define REG(a) (*(volatile u32 *)(a))
|
||||||
|
#define B(a) (*(volatile u8 *)(a))
|
||||||
|
|
||||||
|
#define TIMERUS 0x60005010u
|
||||||
|
#define UARTB_THR 0x70006040u
|
||||||
|
#define I2C5 0x7000D000u
|
||||||
|
|
||||||
|
#define DISK 0xC0000000u /* 128 blocks of 512 bytes */
|
||||||
|
#define DISK_LBAS 128u
|
||||||
|
#define EP0BUF 0xC0110000u
|
||||||
|
#define CMDBUF 0xC0111000u /* CBW / CSW / small replies */
|
||||||
|
|
||||||
|
static void putc_(char c) { REG(UARTB_THR) = (u32)(unsigned char)c; }
|
||||||
|
|
||||||
|
static void puts_(const char *s) {
|
||||||
|
while (*s)
|
||||||
|
putc_(*s++);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void puthex(u32 v) {
|
||||||
|
for (int i = 28; i >= 0; i -= 4)
|
||||||
|
putc_("0123456789ABCDEF"[(v >> i) & 0xF]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void i2c5_write(u32 dev, u32 reg, u32 val) {
|
||||||
|
REG(I2C5 + 0x04) = dev << 1;
|
||||||
|
REG(I2C5 + 0x0C) = reg | (val << 8);
|
||||||
|
REG(I2C5 + 0x00) = (1u << 1) | (1u << 9);
|
||||||
|
}
|
||||||
|
|
||||||
|
static __attribute__((noreturn)) void finish(const char *msg) {
|
||||||
|
puts_(msg);
|
||||||
|
i2c5_write(0x3C, 0x41, 0x02); /* MAX77620 ONOFFCNFG1: PWR_OFF */
|
||||||
|
for (;;)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void copy(u32 dst, const u8 *src, u32 n) {
|
||||||
|
for (u32 i = 0; i < n; i++)
|
||||||
|
B(dst + i) = src[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
#if SCENARIO != 3
|
||||||
|
static void zero(u32 dst, u32 n) {
|
||||||
|
for (u32 i = 0; i < n; i += 4)
|
||||||
|
REG(dst + i) = 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static u32 crc32(u32 addr, u32 n) {
|
||||||
|
u32 c = 0xFFFFFFFFu;
|
||||||
|
for (u32 i = 0; i < n; i++) {
|
||||||
|
c ^= B(addr + i);
|
||||||
|
for (int k = 0; k < 8; k++)
|
||||||
|
c = (c >> 1) ^ (0xEDB88320u & (0u - (c & 1)));
|
||||||
|
}
|
||||||
|
return ~c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---- Descriptors --------------------------------------------------------- */
|
||||||
|
|
||||||
|
static const u8 dev_desc[18] = {
|
||||||
|
18, 1, 0x00, 0x02, 0, 0, 0, 64,
|
||||||
|
0x09, 0x12, 0x57, 0x7E, /* 1209:7E57 */
|
||||||
|
0x00, 0x01, 1, 2, 3, 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
#if SCENARIO == 3
|
||||||
|
static const u8 hid_report[21] = {
|
||||||
|
0x06, 0x00, 0xFF, 0x09, 0x01, 0xA1, 0x01, /* vendor page, application */
|
||||||
|
0x15, 0x00, 0x26, 0xFF, 0x00, 0x75, 0x08, 0x95, 0x08,
|
||||||
|
0x09, 0x01, 0x81, 0x02, 0xC0, /* 8 bytes of input */
|
||||||
|
};
|
||||||
|
|
||||||
|
static const u8 cfg_desc[34] = {
|
||||||
|
9, 2, 34, 0, 1, 1, 0, 0x80, 50,
|
||||||
|
9, 4, 0, 0, 1, 0x03, 0x00, 0x00, 0, /* HID */
|
||||||
|
9, 0x21, 0x11, 0x01, 0, 1, 0x22, 21, 0, /* HID descriptor */
|
||||||
|
7, 5, 0x81, 3, 8, 0, 4, /* interrupt IN, 8 bytes */
|
||||||
|
};
|
||||||
|
#else
|
||||||
|
static const u8 cfg_desc[32] = {
|
||||||
|
9, 2, 32, 0, 1, 1, 0, 0x80, 50,
|
||||||
|
9, 4, 0, 0, 2, 0x08, 0x06, 0x50, 0, /* mass storage, SCSI, BOT */
|
||||||
|
7, 5, 0x81, 2, 0x00, 0x02, 0, /* bulk IN, 512 */
|
||||||
|
7, 5, 0x01, 2, 0x00, 0x02, 0, /* bulk OUT, 512 */
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const u8 str_lang[4] = {4, 3, 0x09, 0x04};
|
||||||
|
static const char *const strs[3] = {"RCM-Emulator", "USB test disk", "0001"};
|
||||||
|
|
||||||
|
#if SCENARIO != 3
|
||||||
|
static const u8 inquiry[36] = {
|
||||||
|
0x00, 0x80, 0x02, 0x02, 31, 0, 0, 0,
|
||||||
|
'R', 'C', 'M', 'E', 'M', 'U', ' ', ' ',
|
||||||
|
'T', 'e', 's', 't', ' ', 'd', 'i', 's', 'k', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||||
|
'1', '.', '0', '0',
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ---- Transport ------------------------------------------------------------ */
|
||||||
|
/*
|
||||||
|
* What the gadget needs from a controller:
|
||||||
|
* poll_setup(pkt) a SETUP packet, if one came (1) or not (0)
|
||||||
|
* ep0_in(len) send EP0BUF[0..len), then the status stage
|
||||||
|
* ep0_status() a no-data request's status stage
|
||||||
|
* ep0_stall() reject the request
|
||||||
|
* set_address(a) after its status stage
|
||||||
|
* configure() bring up the bulk endpoints
|
||||||
|
* bulk(in, buf, len) one bulk transfer; returns bytes moved
|
||||||
|
* stop() detach
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if SCENARIO != 2
|
||||||
|
/* ---- USB2 controller USB1 (bdk usbd.c) ---- */
|
||||||
|
|
||||||
|
#define USB 0x7D000000u
|
||||||
|
#define U(o) REG(USB + (o))
|
||||||
|
#define USBCMD 0x130
|
||||||
|
#define USBSTS 0x134
|
||||||
|
#define DEVADDR 0x144
|
||||||
|
#define EPLIST 0x148
|
||||||
|
#define USBMODE 0x1F8
|
||||||
|
#define SETUPST 0x208
|
||||||
|
#define PRIME 0x20C
|
||||||
|
#define FLUSH 0x210
|
||||||
|
#define STATUS 0x214
|
||||||
|
#define COMPL 0x218
|
||||||
|
#define EPCTRL(n) (0x21C + 4 * (n))
|
||||||
|
#define SUSP 0x400
|
||||||
|
#define QHB (USB + 0x1000)
|
||||||
|
#define QH(i) (QHB + 64 * (i))
|
||||||
|
#define DTD 0xC0100000u /* 4 dTDs per queue head, 32 bytes each */
|
||||||
|
|
||||||
|
static void init(void) {
|
||||||
|
U(SUSP) |= (1u << 12) | (1u << 11); /* UTMIP_PHY_ENB, UTMIP_RESET */
|
||||||
|
U(SUSP) &= ~(1u << 11);
|
||||||
|
while (!(U(SUSP) & (1u << 7))) /* PHY_CLK_VALID */
|
||||||
|
;
|
||||||
|
U(USBCMD) |= 1u << 1; /* RESET */
|
||||||
|
while (U(USBCMD) & (1u << 1))
|
||||||
|
;
|
||||||
|
U(USBMODE) = 2; /* device */
|
||||||
|
for (u32 i = 0; i < 4; i++) {
|
||||||
|
for (u32 w = 0; w < 16; w++)
|
||||||
|
REG(QH(i) + 4 * w) = 0;
|
||||||
|
REG(QH(i) + 8) = 1; /* no dTD */
|
||||||
|
}
|
||||||
|
REG(QH(0)) = 64u << 16 | 1u << 15; /* EP0 OUT: 64, IOS */
|
||||||
|
REG(QH(1)) = 64u << 16;
|
||||||
|
U(EPLIST) = QHB;
|
||||||
|
U(USBCMD) |= 1; /* RUN: attach */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* One transfer on queue head idx (2 * ep + in), in up to four 16 KiB dTDs as
|
||||||
|
* bdk builds them. Returns the bytes moved, or -1 if it never completed. */
|
||||||
|
static int xfer(u32 idx, u32 buf, u32 len) {
|
||||||
|
u32 base = DTD + idx * 4 * 32, off = 0, n = 0;
|
||||||
|
do {
|
||||||
|
u32 d = base + n * 32, sz = len - off > 16384 ? 16384 : len - off;
|
||||||
|
REG(d) = 1;
|
||||||
|
REG(d + 4) = sz << 16 | 0x80; /* bytes, ACTIVE */
|
||||||
|
for (u32 p = 0; p < 5; p++)
|
||||||
|
REG(d + 8 + 4 * p) = buf ? buf + off + 4096 * p : 0;
|
||||||
|
if (n)
|
||||||
|
REG(base + (n - 1) * 32) = d;
|
||||||
|
off += sz;
|
||||||
|
n++;
|
||||||
|
} while (off < len && n < 4);
|
||||||
|
REG(QH(idx) + 8) = base;
|
||||||
|
REG(QH(idx) + 0xC) = 0;
|
||||||
|
u32 bit = idx & 1 ? 1u << (16 + idx / 2) : 1u << (idx / 2);
|
||||||
|
U(PRIME) = bit;
|
||||||
|
u32 start = REG(TIMERUS);
|
||||||
|
while ((U(PRIME) | U(STATUS)) & bit)
|
||||||
|
if (REG(TIMERUS) - start > 5000000)
|
||||||
|
return -1;
|
||||||
|
U(COMPL) = bit;
|
||||||
|
u32 left = 0;
|
||||||
|
for (u32 i = 0; i < n; i++)
|
||||||
|
left += (REG(base + i * 32 + 4) >> 16) & 0x7FFF;
|
||||||
|
return (int)(len - left);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int poll_setup(u8 *pkt) {
|
||||||
|
u32 sts = U(USBSTS);
|
||||||
|
U(USBSTS) = sts;
|
||||||
|
if (sts & (1u << 6)) { /* bus reset */
|
||||||
|
U(DEVADDR) = 0;
|
||||||
|
U(SETUPST) = U(SETUPST);
|
||||||
|
U(COMPL) = U(COMPL);
|
||||||
|
U(FLUSH) = 0xFFFFFFFFu;
|
||||||
|
}
|
||||||
|
if (!(U(SETUPST) & 1))
|
||||||
|
return 0;
|
||||||
|
U(SETUPST) = 1;
|
||||||
|
u32 lo = REG(QH(0) + 0x28), hi = REG(QH(0) + 0x2C);
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
pkt[i] = lo >> (8 * i);
|
||||||
|
pkt[4 + i] = hi >> (8 * i);
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ep0_in(u32 len) {
|
||||||
|
xfer(1, EP0BUF, len);
|
||||||
|
xfer(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ep0_status(void) { xfer(1, 0, 0); }
|
||||||
|
|
||||||
|
static void ep0_stall(void) { U(EPCTRL(0)) |= 1u | 1u << 16; }
|
||||||
|
|
||||||
|
static void set_address(u32 a) { U(DEVADDR) = a << 25; }
|
||||||
|
|
||||||
|
static void configure(void) {
|
||||||
|
for (u32 i = 2; i < 4; i++) {
|
||||||
|
for (u32 w = 0; w < 16; w++)
|
||||||
|
REG(QH(i) + 4 * w) = 0;
|
||||||
|
REG(QH(i)) = 512u << 16;
|
||||||
|
REG(QH(i) + 8) = 1;
|
||||||
|
}
|
||||||
|
U(EPCTRL(1)) = 2u << 2 | 1u << 7 | 2u << 18 | 1u << 23; /* bulk RX/TX on */
|
||||||
|
}
|
||||||
|
|
||||||
|
static int bulk(int in, u32 buf, u32 len) { return xfer(in ? 3 : 2, buf, len); }
|
||||||
|
|
||||||
|
static void stop(void) {
|
||||||
|
U(USBCMD) &= ~1u;
|
||||||
|
(void)U(USBCMD); /* let the host see it go */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Without a host nothing resets the bus: say so after half a second. */
|
||||||
|
static int bus_reset_seen(void) {
|
||||||
|
u32 start = REG(TIMERUS);
|
||||||
|
while (REG(TIMERUS) - start < 500000)
|
||||||
|
if (U(USBSTS) & (1u << 6))
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
/* ---- XUSB device controller (bdk xusbd.c) ---- */
|
||||||
|
|
||||||
|
#define XU 0x700D0000u
|
||||||
|
#define X(o) REG(XU + (o))
|
||||||
|
#define X_DB 0x04
|
||||||
|
#define X_ERSTSZ 0x08
|
||||||
|
#define X_ERST0 0x10
|
||||||
|
#define X_ERST1 0x18
|
||||||
|
#define X_ERDP 0x20
|
||||||
|
#define X_EREP 0x28
|
||||||
|
#define X_CTRL 0x30
|
||||||
|
#define X_ST 0x34
|
||||||
|
#define X_IMOD 0x38
|
||||||
|
#define X_PORTSC 0x3C
|
||||||
|
#define X_ECP 0x40
|
||||||
|
#define X_HALT 0x50
|
||||||
|
#define X_RELOAD 0x58
|
||||||
|
|
||||||
|
#define XMEM 0x40030000u
|
||||||
|
#define SEG0 (XMEM)
|
||||||
|
#define SEG1 (XMEM + 0x100)
|
||||||
|
#define CTX (XMEM + 0x500) /* four 64-byte endpoint contexts */
|
||||||
|
|
||||||
|
struct ring { u32 base, idx, pcs; };
|
||||||
|
static struct ring rings[4]; /* by context index: 0 control, 2 OUT, 3 IN */
|
||||||
|
static u32 evt_deq, ccs, seq;
|
||||||
|
|
||||||
|
static void ring_init(u32 dci, u32 base) {
|
||||||
|
struct ring *r = &rings[dci];
|
||||||
|
r->base = base;
|
||||||
|
r->idx = 0;
|
||||||
|
r->pcs = 1;
|
||||||
|
zero(base, 16 * 16);
|
||||||
|
REG(base + 15 * 16) = base; /* Link TRB back to the start */
|
||||||
|
REG(base + 15 * 16 + 12) = 6u << 10 | 2; /* type LINK, toggle cycle */
|
||||||
|
u32 ctx = CTX + 64 * dci;
|
||||||
|
zero(ctx, 64);
|
||||||
|
REG(ctx) = 1; /* EP_RUNNING */
|
||||||
|
REG(ctx + 8) = base | 1; /* dequeue pointer, DCS */
|
||||||
|
}
|
||||||
|
|
||||||
|
static void queue(u32 dci, u32 w0, u32 w2, u32 w3) {
|
||||||
|
struct ring *r = &rings[dci];
|
||||||
|
u32 t = r->base + r->idx * 16;
|
||||||
|
REG(t) = w0;
|
||||||
|
REG(t + 4) = 0;
|
||||||
|
REG(t + 8) = w2;
|
||||||
|
REG(t + 12) = w3 | r->pcs;
|
||||||
|
if (++r->idx == 15) {
|
||||||
|
REG(r->base + 15 * 16 + 12) = 6u << 10 | 2 | r->pcs;
|
||||||
|
r->idx = 0;
|
||||||
|
r->pcs ^= 1;
|
||||||
|
}
|
||||||
|
X(X_DB) = dci << 8 | (dci ? 0 : seq << 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init(void) {
|
||||||
|
zero(XMEM, 0x600);
|
||||||
|
X(X_ERST0) = SEG0;
|
||||||
|
X(X_ERST1) = SEG1;
|
||||||
|
X(X_ERSTSZ) = 16u << 16 | 16;
|
||||||
|
X(X_EREP) = SEG0 | 1;
|
||||||
|
X(X_ERDP) = SEG0;
|
||||||
|
evt_deq = SEG0;
|
||||||
|
ccs = 1;
|
||||||
|
ring_init(0, XMEM + 0x200);
|
||||||
|
X(X_CTRL) |= 1u << 4 | 1u << 1; /* IE, LSE */
|
||||||
|
X(X_ECP) = CTX;
|
||||||
|
X(X_IMOD) = 0;
|
||||||
|
X(X_PORTSC) = (X(X_PORTSC) & ~(0xFu << 5)) | 1u << 16 | 5u << 5; /* RxDetect */
|
||||||
|
X(X_CTRL) |= 1u << 31; /* ENABLE */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The next event, if any. Polling ST lets the host run. */
|
||||||
|
static int next_event(u32 *ev) {
|
||||||
|
(void)X(X_ST);
|
||||||
|
if ((REG(evt_deq + 12) & 1) != ccs)
|
||||||
|
return 0;
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
ev[i] = REG(evt_deq + 4 * i);
|
||||||
|
if (evt_deq == SEG0 + 15 * 16)
|
||||||
|
evt_deq = SEG1;
|
||||||
|
else if (evt_deq == SEG1 + 15 * 16) {
|
||||||
|
evt_deq = SEG0;
|
||||||
|
ccs ^= 1;
|
||||||
|
} else
|
||||||
|
evt_deq += 16;
|
||||||
|
X(X_ERDP) = evt_deq;
|
||||||
|
X(X_ST) = 1u << 4; /* IP, write 1 to clear */
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void port_change(void) {
|
||||||
|
u32 ps = X(X_PORTSC);
|
||||||
|
X(X_PORTSC) = ps; /* write the change bits back */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Wait for the Transfer event of endpoint dci; the bytes it left over. */
|
||||||
|
static int wait_xfer(u32 dci) {
|
||||||
|
u32 ev[4], start = REG(TIMERUS);
|
||||||
|
for (;;) {
|
||||||
|
if (REG(TIMERUS) - start > 5000000)
|
||||||
|
return -1;
|
||||||
|
if (!next_event(ev))
|
||||||
|
continue;
|
||||||
|
u32 type = ev[3] >> 10 & 0x3F;
|
||||||
|
if (type == 34)
|
||||||
|
port_change();
|
||||||
|
else if (type == 32 && (ev[3] >> 16 & 0x1F) == dci)
|
||||||
|
return (int)(ev[2] & 0xFFFFFF);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int poll_setup(u8 *pkt) {
|
||||||
|
u32 ev[4];
|
||||||
|
if (!next_event(ev))
|
||||||
|
return 0;
|
||||||
|
u32 type = ev[3] >> 10 & 0x3F;
|
||||||
|
if (type == 34) {
|
||||||
|
port_change();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (type != 63)
|
||||||
|
return 0;
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
pkt[i] = ev[0] >> (8 * i);
|
||||||
|
pkt[4 + i] = ev[1] >> (8 * i);
|
||||||
|
}
|
||||||
|
seq = ev[2] & 0xFFFF;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ep0_in(u32 len) {
|
||||||
|
queue(0, EP0BUF, len, 3u << 10 | 1u << 16 | 1u << 5); /* Data IN, IOC */
|
||||||
|
wait_xfer(0);
|
||||||
|
queue(0, 0, 0, 4u << 10 | 1u << 5); /* Status OUT */
|
||||||
|
wait_xfer(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ep0_status(void) {
|
||||||
|
queue(0, 0, 0, 4u << 10 | 1u << 16 | 1u << 5); /* Status IN */
|
||||||
|
wait_xfer(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ep0_stall(void) { X(X_HALT) |= 1; }
|
||||||
|
|
||||||
|
static void set_address(u32 a) { X(X_CTRL) = (X(X_CTRL) & 0x80FFFFFFu) | a << 24; }
|
||||||
|
|
||||||
|
static void configure(void) {
|
||||||
|
ring_init(2, XMEM + 0x300);
|
||||||
|
ring_init(3, XMEM + 0x400);
|
||||||
|
X(X_RELOAD) = 0xC;
|
||||||
|
while (X(X_RELOAD) & 0xC)
|
||||||
|
;
|
||||||
|
X(X_HALT) &= ~0xCu;
|
||||||
|
X(X_CTRL) |= 1; /* RUN */
|
||||||
|
}
|
||||||
|
|
||||||
|
static int bulk(int in, u32 buf, u32 len) {
|
||||||
|
u32 dci = in ? 3 : 2;
|
||||||
|
queue(dci, buf, len, 1u << 10 | 1u << 5 | 1u << 2); /* Normal, IOC, ISP */
|
||||||
|
int left = wait_xfer(dci);
|
||||||
|
return left < 0 ? -1 : (int)(len - left);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void stop(void) {
|
||||||
|
X(X_CTRL) = 0;
|
||||||
|
(void)X(X_CTRL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int bus_reset_seen(void) { return 1; }
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ---- The gadget ----------------------------------------------------------- */
|
||||||
|
|
||||||
|
static int configured, lun_asked, report_asked, idle_set;
|
||||||
|
|
||||||
|
static void control(const u8 *p) {
|
||||||
|
u32 bm = p[0], req = p[1], val = p[2] | p[3] << 8, len = p[6] | p[7] << 8;
|
||||||
|
u32 n = 0;
|
||||||
|
if (bm == 0x80 && req == 6) { /* GET_DESCRIPTOR */
|
||||||
|
u32 type = val >> 8, idx = val & 0xFF;
|
||||||
|
if (type == 1) {
|
||||||
|
copy(EP0BUF, dev_desc, n = 18);
|
||||||
|
} else if (type == 2) {
|
||||||
|
copy(EP0BUF, cfg_desc, n = sizeof(cfg_desc));
|
||||||
|
} else if (type == 3 && idx == 0) {
|
||||||
|
copy(EP0BUF, str_lang, n = 4);
|
||||||
|
} else if (type == 3 && idx <= 3) {
|
||||||
|
const char *s = strs[idx - 1];
|
||||||
|
n = 2;
|
||||||
|
while (*s) {
|
||||||
|
B(EP0BUF + n) = *s++;
|
||||||
|
B(EP0BUF + n + 1) = 0;
|
||||||
|
n += 2;
|
||||||
|
}
|
||||||
|
B(EP0BUF) = n;
|
||||||
|
B(EP0BUF + 1) = 3;
|
||||||
|
} else {
|
||||||
|
ep0_stall();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ep0_in(n < len ? n : len);
|
||||||
|
} else if (bm == 0x00 && req == 5) { /* SET_ADDRESS */
|
||||||
|
ep0_status();
|
||||||
|
set_address(val & 0x7F);
|
||||||
|
} else if (bm == 0x00 && req == 9) { /* SET_CONFIGURATION */
|
||||||
|
configure();
|
||||||
|
ep0_status();
|
||||||
|
configured = 1;
|
||||||
|
puts_("usb test: configured\n");
|
||||||
|
} else if (bm == 0xA1 && req == 0xFE) { /* GET_MAX_LUN */
|
||||||
|
B(EP0BUF) = 0;
|
||||||
|
ep0_in(1);
|
||||||
|
lun_asked = 1;
|
||||||
|
#if SCENARIO == 3
|
||||||
|
} else if (bm == 0x81 && req == 6 && (val >> 8) == 0x22) { /* HID report */
|
||||||
|
copy(EP0BUF, hid_report, n = sizeof(hid_report));
|
||||||
|
ep0_in(n < len ? n : len);
|
||||||
|
report_asked = 1;
|
||||||
|
} else if (bm == 0x21 && req == 0x0A) { /* SET_IDLE */
|
||||||
|
ep0_status();
|
||||||
|
idle_set = 1;
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
ep0_stall();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if SCENARIO != 3
|
||||||
|
static void put_be32(u32 a, u32 v) {
|
||||||
|
B(a) = v >> 24;
|
||||||
|
B(a + 1) = v >> 16;
|
||||||
|
B(a + 2) = v >> 8;
|
||||||
|
B(a + 3) = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Serve Bulk-Only Transport commands until the host ejects the disk. */
|
||||||
|
static void bot(void) {
|
||||||
|
u32 sense = 6, asc = 0x29; /* a unit attention first, like a real disk */
|
||||||
|
for (;;) {
|
||||||
|
int n = bulk(0, CMDBUF, 512);
|
||||||
|
if (n != 31 || REG(CMDBUF) != 0x43425355u) /* "USBC" */
|
||||||
|
finish("usb test: FAIL bad CBW\n");
|
||||||
|
u32 tag = REG(CMDBUF + 4), want = REG(CMDBUF + 8);
|
||||||
|
u8 op = B(CMDBUF + 15);
|
||||||
|
u32 cdb = CMDBUF + 15, status = 0, sent = 0, eject = 0;
|
||||||
|
switch (op) {
|
||||||
|
case 0x12: /* INQUIRY */
|
||||||
|
copy(CMDBUF + 0x100, inquiry, 36);
|
||||||
|
sent = bulk(1, CMDBUF + 0x100, want < 36 ? want : 36);
|
||||||
|
break;
|
||||||
|
case 0x00: /* TEST UNIT READY */
|
||||||
|
status = sense ? 1 : 0;
|
||||||
|
break;
|
||||||
|
case 0x03: /* REQUEST SENSE */
|
||||||
|
zero(CMDBUF + 0x100, 20);
|
||||||
|
B(CMDBUF + 0x100) = 0x70;
|
||||||
|
B(CMDBUF + 0x102) = sense;
|
||||||
|
B(CMDBUF + 0x107) = 10;
|
||||||
|
B(CMDBUF + 0x10C) = asc;
|
||||||
|
sense = asc = 0;
|
||||||
|
sent = bulk(1, CMDBUF + 0x100, want < 18 ? want : 18);
|
||||||
|
break;
|
||||||
|
case 0x25: /* READ CAPACITY(10) */
|
||||||
|
put_be32(CMDBUF + 0x100, DISK_LBAS - 1);
|
||||||
|
put_be32(CMDBUF + 0x104, 512);
|
||||||
|
sent = bulk(1, CMDBUF + 0x100, 8);
|
||||||
|
break;
|
||||||
|
case 0x28: { /* READ(10) */
|
||||||
|
u32 lba = (u32)B(cdb + 2) << 24 | B(cdb + 3) << 16 | B(cdb + 4) << 8 |
|
||||||
|
B(cdb + 5);
|
||||||
|
u32 cnt = B(cdb + 7) << 8 | B(cdb + 8);
|
||||||
|
if (lba + cnt > DISK_LBAS)
|
||||||
|
status = 1;
|
||||||
|
else
|
||||||
|
sent = bulk(1, DISK + lba * 512, cnt * 512);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 0x1E: /* PREVENT ALLOW MEDIUM REMOVAL */
|
||||||
|
break;
|
||||||
|
case 0x1B: /* START STOP UNIT */
|
||||||
|
eject = B(cdb + 4) & 2;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
status = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
REG(CMDBUF + 0x200) = 0x53425355u; /* "USBS" */
|
||||||
|
REG(CMDBUF + 0x204) = tag;
|
||||||
|
REG(CMDBUF + 0x208) = want - sent;
|
||||||
|
B(CMDBUF + 0x20C) = status;
|
||||||
|
bulk(1, CMDBUF + 0x200, 13);
|
||||||
|
if (eject)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
__attribute__((section(".text.start"), noreturn))
|
||||||
|
void _start(void) {
|
||||||
|
for (u32 i = 0; i < DISK_LBAS * 512; i++)
|
||||||
|
B(DISK + i) = (u8)((i * 7) ^ (i >> 9));
|
||||||
|
puts_("usb test: disk crc32 first ");
|
||||||
|
puthex(crc32(DISK, 64 * 512));
|
||||||
|
puts_(" last ");
|
||||||
|
puthex(crc32(DISK + (DISK_LBAS - 8) * 512, 8 * 512));
|
||||||
|
putc_('\n');
|
||||||
|
|
||||||
|
init();
|
||||||
|
if (!bus_reset_seen()) {
|
||||||
|
stop();
|
||||||
|
finish("usb test: no bus reset, nothing on the cable\n");
|
||||||
|
}
|
||||||
|
u8 pkt[8];
|
||||||
|
u32 start = REG(TIMERUS);
|
||||||
|
while (!(configured && (SCENARIO == 3 ? report_asked && idle_set : lun_asked))) {
|
||||||
|
if (poll_setup(pkt))
|
||||||
|
control(pkt);
|
||||||
|
if (REG(TIMERUS) - start > 10000000)
|
||||||
|
finish("usb test: FAIL enumeration timed out\n");
|
||||||
|
}
|
||||||
|
#if SCENARIO == 3
|
||||||
|
for (u32 k = 1; k <= 3; k++) {
|
||||||
|
for (u32 i = 0; i < 8; i++)
|
||||||
|
B(CMDBUF + i) = (u8)(k * 0x10 + i);
|
||||||
|
if (bulk(1, CMDBUF, 8) != 8)
|
||||||
|
finish("usb test: FAIL report not taken\n");
|
||||||
|
}
|
||||||
|
puts_("usb test: hid reports sent\n");
|
||||||
|
#else
|
||||||
|
bot();
|
||||||
|
puts_("usb test: ejected\n");
|
||||||
|
#endif
|
||||||
|
stop();
|
||||||
|
finish("usb test: done\n");
|
||||||
|
}
|
||||||
Executable
+72
@@ -0,0 +1,72 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# USB device mode regression test: tests/usb/payload.c brings up a mass
|
||||||
|
# storage gadget on each device controller and the emulator's --usb-host PC
|
||||||
|
# enumerates it, reads it back and ejects it; without a host the gadget must
|
||||||
|
# see nothing on the cable. Usage: tests/usb/run.sh [rcm_emu] [payload dir]
|
||||||
|
EMU=${1:-./rcm_emu}
|
||||||
|
DIR=${2:-tests/usb}
|
||||||
|
fail=0
|
||||||
|
|
||||||
|
run() {
|
||||||
|
log=$DIR/out_$1.log
|
||||||
|
shift
|
||||||
|
SDL_VIDEODRIVER=dummy RCM_EMU_NO_AUDIO=1 timeout 60 "$EMU" "$@" > "$log" 2>&1
|
||||||
|
echo $?
|
||||||
|
}
|
||||||
|
|
||||||
|
expect() {
|
||||||
|
if grep -q -- "$2" "$1"; then
|
||||||
|
echo " ok $3"
|
||||||
|
else
|
||||||
|
echo " FAIL $3 (missing: $2)"
|
||||||
|
fail=1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
check_host() {
|
||||||
|
log=$1
|
||||||
|
name=$2
|
||||||
|
expect "$log" '\[usb-host\] device attached' "$name: the host sees the device attach"
|
||||||
|
expect "$log" 'high-speed device 1209:7E57 "RCM-Emulator" "USB test disk" serial "0001", address 1' \
|
||||||
|
"$name: enumerated, strings read, address set"
|
||||||
|
expect "$log" 'configuration 1: mass storage, bulk-only (EP 81 IN, EP 01 OUT, 512-byte packets)' \
|
||||||
|
"$name: configuration parsed"
|
||||||
|
expect "$log" 'LUN 0: "RCMEMU Test disk ", removable' "$name: INQUIRY"
|
||||||
|
expect "$log" 'TEST UNIT READY: sense key 6, ASC 29/00' "$name: unit attention, then REQUEST SENSE"
|
||||||
|
expect "$log" '128 blocks of 512 bytes' "$name: READ CAPACITY"
|
||||||
|
first=$(sed -n 's/.*disk crc32 first \([0-9A-F]*\) last.*/\1/p' "$log")
|
||||||
|
last=$(sed -n 's/.*disk crc32 first [0-9A-F]* last \([0-9A-F]*\).*/\1/p' "$log")
|
||||||
|
expect "$log" "read blocks 0-63 (crc32 $first)" "$name: READ(10) of blocks 0-63 returns the disk"
|
||||||
|
expect "$log" "read blocks 120-127 (crc32 $last)" "$name: READ(10) of the last 8 blocks returns the disk"
|
||||||
|
expect "$log" 'usb test: ejected' "$name: START STOP UNIT ejects"
|
||||||
|
expect "$log" '\[usb-host\] device detached' "$name: the host sees the device go"
|
||||||
|
expect "$log" 'usb test: done' "$name: the payload ran to its end"
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "usb test:"
|
||||||
|
st=$(run 1 "$DIR/payload_1.bin" --usb-host)
|
||||||
|
check_host "$DIR/out_1.log" "USB2 controller"
|
||||||
|
[ "$st" -eq 0 ] || { echo " FAIL USB2 controller: emulator exit $st"; fail=1; }
|
||||||
|
|
||||||
|
st=$(run 2 "$DIR/payload_2.bin" --usb-host --oem mariko)
|
||||||
|
check_host "$DIR/out_2.log" "XUSB controller"
|
||||||
|
[ "$st" -eq 0 ] || { echo " FAIL XUSB controller: emulator exit $st"; fail=1; }
|
||||||
|
|
||||||
|
st=$(run 3 "$DIR/payload_3.bin" --usb-host)
|
||||||
|
log=$DIR/out_3.log
|
||||||
|
expect "$log" 'configuration 1: HID (EP 81 IN, interrupt every 4)' "HID gadget: configuration parsed"
|
||||||
|
expect "$log" 'HID report descriptor: 21 bytes' "HID gadget: report descriptor read"
|
||||||
|
expect "$log" 'SET_IDLE done; polling EP 81 for reports' "HID gadget: SET_IDLE"
|
||||||
|
expect "$log" 'HID report 1: 10 11 12 13 14 15 16 17' "HID gadget: first interrupt report"
|
||||||
|
expect "$log" 'HID report 3: 30 31 32 33 34 35 36 37' "HID gadget: third interrupt report"
|
||||||
|
expect "$log" '\[usb-host\] device detached' "HID gadget: the host sees the device go"
|
||||||
|
[ "$st" -eq 0 ] || { echo " FAIL HID gadget: emulator exit $st"; fail=1; }
|
||||||
|
|
||||||
|
st=$(run 0 "$DIR/payload_1.bin")
|
||||||
|
expect "$DIR/out_0.log" 'usb test: no bus reset, nothing on the cable' "no host: the gadget waits on an empty cable"
|
||||||
|
if grep -q '\[usb-host\]' "$DIR/out_0.log"; then
|
||||||
|
echo " FAIL no host: the host logged activity"
|
||||||
|
fail=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
[ $fail -eq 0 ] && echo "usb test: PASS" || { echo "usb test: FAIL (logs: $DIR/out_*.log)"; exit 1; }
|
||||||
Reference in new issue
Block a user