From d5bf5922eda8f8a98c04e9677722b8b754bf877a Mon Sep 17 00:00:00 2001 From: Baptiste MOINE Date: Thu, 5 Nov 2026 23:45:43 +0100 Subject: [PATCH] Add SNAP to input scripts --- README.md | 4 ++-- display/sdl_display.cpp | 42 ++++++++++++++++++++++++++++++----------- emu_state.h | 25 ++++++++++++++++++++++++ input_script.cpp | 32 ++++++++++++++++++++++++++++--- main.cpp | 12 ++++++++++++ 5 files changed, 99 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index bc5dc3c..84f8785 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ slow or destructive. - Replay deterministic button sequences via `--auto-pin-recovery` or `--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 - any payload. + any payload, and can save the screen at a given point of a run. - 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, @@ -322,7 +322,7 @@ needed. | `--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`. | | `--fault` | `NAME[,NAME…]` \| `list` | Break a chip or a rail; repeatable. A **chip** fault (`max77812`, `max77621_cpu`, `max77621_gpu`, `bq24193`, `max17050`, `tmp451`, `bm92t36`) makes the part NAK its I2C address, refuse writes and take every rail it supplies down with it. A **rail** fault (`vdd_cpu`) leaves the regulator answering and reading back as enabled while its output never comes up, so only what the rail feeds notices: the CPU RAM repair never completes and CPU0 cannot run. `list` prints the names and exits. Faults survive a payload reboot, like the hardware they model, and add to `[faults]` in the ini (`NAME=dead`). See [DESIGN.md](DESIGN.md#fault-injection). | | `--usb-host` | `nbd[:port]` (optional) | 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), its last 8 sectors are written back unchanged and read again unless it is write-protected, and it is ejected; HID reports are polled. With `nbd` the disk is served over NBD on 127.0.0.1 (port 10809 by default) until the client disconnects, then ejected - see [Mounting a gadget's disk](#mounting-a-gadgets-disk). Logged as `[usb-host]`; `RCM_USB_TRACE=1` traces every transfer. Overrides `[usb] host` and `[usb] nbd_port` in the ini. | -| `--input-script` | file or spec | Scripted input keyed to emulated time: ` P\|U\|D [hold_ms]` presses POWER / VOL+ / VOL-, ` TAP [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: ` P\|U\|D [hold_ms]` presses POWER / VOL+ / VOL-, ` TAP [hold_ms]` taps the touchscreen at (x, y) in the 1280x720 picture, ` SNAP ` saves the next displayed frame to `` (raw B, G, R, A like `last_fb.rgba`, size in the matching `.meta`), `+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-te-script` | (none) | Drive `recover_pin.te` in TegraExplorer without input. | diff --git a/display/sdl_display.cpp b/display/sdl_display.cpp index 00bc449..41bed1e 100644 --- a/display/sdl_display.cpp +++ b/display/sdl_display.cpp @@ -1,4 +1,5 @@ #include "sdl_display.h" +#include #include "config_window.h" #include "console_window.h" #include "../emu_state.h" @@ -475,6 +476,25 @@ void sdl_display_update(EmuState *state, uc_engine *uc) { SDL_UpdateTexture(texture, nullptr, g_frame.data(), out_w * 4); } + // Snapshots the input script asked for, from this frame. + std::string snap; + while (!g_frame.empty() && state->snap_take(&snap)) { + if (FILE *f = fopen(snap.c_str(), "wb")) { + fwrite(g_frame.data(), 1, g_frame.size() * 4, f); + fclose(f); + std::string meta = snap; + if (meta.size() > 5 && meta.compare(meta.size() - 5, 5, ".rgba") == 0) + meta.resize(meta.size() - 5); + meta += ".meta"; + if (FILE *fm = fopen(meta.c_str(), "w")) { + fprintf(fm, "%u %u\n", out_w, out_h); + fclose(fm); + } + printf("[snap] %s (%ux%u)\n", snap.c_str(), out_w, out_h); + fflush(stdout); + } + } + // Snapshot for PNG conversion - at most once a second (~60 frames), and // only when the frame has changed since the last one written, so an idle // payload does not rewrite a 3.5 MB file every second. @@ -632,14 +652,20 @@ bool sdl_display_poll_events(EmuState *state, uc_engine *uc) { uint32_t step = shift ? 4096 : 128; switch (event.key.keysym.sym) { case SDLK_UP: - if (!state->btn_vol_up) + // One press of 150 ms guest time per keydown (the host's + // auto-repeat makes more while the key is held). + if (!state->btn_vol_up) { printf("[input] DOWN VOL_UP @emu_usec=%llu\n", (unsigned long long)state->emu_usec); - state->btn_vol_up = true; + state->kbd_vol_up_until = state->emu_usec + 150000; + state->btn_vol_up = true; + } break; case SDLK_DOWN: - if (!state->btn_vol_down) + if (!state->btn_vol_down) { printf("[input] DOWN VOL_DOWN @emu_usec=%llu\n", (unsigned long long)state->emu_usec); - state->btn_vol_down = true; + state->kbd_vol_down_until = state->emu_usec + 150000; + state->btn_vol_down = true; + } break; case SDLK_RETURN: if (!state->btn_power) @@ -707,14 +733,8 @@ bool sdl_display_poll_events(EmuState *state, uc_engine *uc) { if (event.type == SDL_KEYUP) { switch (event.key.keysym.sym) { case SDLK_UP: - if (state->btn_vol_up) - printf("[input] UP VOL_UP @emu_usec=%llu\n", (unsigned long long)state->emu_usec); - state->btn_vol_up = false; - break; case SDLK_DOWN: - if (state->btn_vol_down) - printf("[input] UP VOL_DOWN @emu_usec=%llu\n", (unsigned long long)state->emu_usec); - state->btn_vol_down = false; + // The press ends on its own, 150 ms of guest time after it began. break; case SDLK_RETURN: if (state->btn_power) diff --git a/emu_state.h b/emu_state.h index db8de9d..8773bfb 100644 --- a/emu_state.h +++ b/emu_state.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -215,6 +216,12 @@ struct EmuState { std::atomic btn_vol_up{false}; std::atomic btn_vol_down{false}; std::atomic btn_power{false}; + // A keyboard VOL press ends at this guest time (0: none running). The + // arrow keys press for a fixed guest duration: guest time outruns the + // wall clock while the guest sleeps, so a held key's wall time means + // little to the guest. + std::atomic kbd_vol_up_until{0}; + std::atomic kbd_vol_down_until{0}; // Touchscreen state (FTS4/STMFTS stub on I2C3 @ slave 0x49). // Updated by SDL mouse events; consumed by i2c3_*/STMFTS code on CPU thread. @@ -250,6 +257,24 @@ struct EmuState { tc_q_tail.store(tail + 1); return true; } + + // Framebuffer snapshots asked for by the input script (SNAP), written by + // the display thread from the next frame it shows. CPU thread produces, + // display thread consumes. + std::mutex snap_mtx; + std::vector snap_paths; + void snap_request(const std::string &path) { + std::lock_guard lk(snap_mtx); + snap_paths.push_back(path); + } + bool snap_take(std::string *path) { + std::lock_guard lk(snap_mtx); + if (snap_paths.empty()) + return false; + *path = snap_paths.front(); + snap_paths.erase(snap_paths.begin()); + return true; + } // last_rot mirrors the rotation last applied by sdl_display_update. // Read from the SDL event handler to invert the display→window transform. std::atomic last_rot{0}; diff --git a/input_script.cpp b/input_script.cpp index 163c174..d6f93b9 100644 --- a/input_script.cpp +++ b/input_script.cpp @@ -17,6 +17,13 @@ // touches the screen at (x, y) in the 1280x720 landscape picture bdk's // touch driver reports (Nyx's coordinates), and lifts after hold_ms. // +// SNAP +// +// writes the next frame the display shows to , raw like last_fb.rgba +// (B, G, R, A bytes) with its size in the matching .meta. For screenshots +// at a known point of a run: guest time runs faster than the wall clock, +// so the once-a-second last_fb.rgba cannot be timed from outside. +// // '#' starts a comment. Example - walk 6 entries down a menu and select: // // rcm_emu payload.bin --input-script "3000 D, + D, + D, + D, + D, + D, + P" @@ -42,7 +49,7 @@ namespace { -enum class Key { Power, VolUp, VolDown, Uart, UartFile, Tap }; +enum class Key { Power, VolUp, VolDown, Uart, UartFile, Tap, Snap }; struct Event { uint64_t at_us = 0; // press time @@ -50,7 +57,7 @@ struct Event { Key key = Key::Power; bool pressed = false; bool released = false; - std::string data; // UART / UARTFILE payload + std::string data; // UART / UARTFILE payload, SNAP file uint16_t x = 0, y = 0; // TAP: panel-raw coordinates }; @@ -68,6 +75,7 @@ const char *key_name(Key k) { case Key::Uart: return "UART"; case Key::UartFile: return "UARTFILE"; case Key::Tap: return "TAP"; + case Key::Snap: return "SNAP"; default: return "VOL-"; } } @@ -89,6 +97,7 @@ bool parse_key(const std::string &tok, Key *out) { if (s == "UART") { *out = Key::Uart; return true; } if (s == "UARTFILE") { *out = Key::UartFile; return true; } if (s == "TAP" || s == "T") { *out = Key::Tap; return true; } + if (s == "SNAP") { *out = Key::Snap; return true; } return false; } @@ -175,7 +184,7 @@ bool parse_text(const std::string &text) { return false; } - if (ev.key == Key::Uart || ev.key == Key::UartFile) { + if (ev.key == Key::Uart || ev.key == Key::UartFile || ev.key == Key::Snap) { // Everything after the keyword is the payload; keep interior spacing. size_t kpos = raw.find(tok[1]); std::string rest = (kpos == std::string::npos) @@ -261,6 +270,23 @@ void input_script_tick(EmuState &state) { // Events are ordered by press time; stop at the first future one. if (!ev.pressed && now < ev.at_us) break; + // SNAP is one-shot too: hand the file name to the display thread. + if (ev.key == Key::Snap) { + if (now < ev.at_us) + break; + state.snap_request(ev.data); + ev.pressed = ev.released = true; + printf("[input-script] SNAP %s @%llu us (%zu/%zu)\n", ev.data.c_str(), + (unsigned long long)now, i + 1, g_events.size()); + fflush(stdout); + if (i == g_done) { + g_done++; + if (g_done == g_events.size()) + printf("[input-script] sequence complete\n"); + } + continue; + } + // UART events are one-shot: push the bytes into UART_B's receive FIFO // (index 1 = UART_B, the port payloads use for their debug console) and // retire the event immediately. diff --git a/main.cpp b/main.cpp index 9c62eb8..1fd8ae9 100644 --- a/main.cpp +++ b/main.cpp @@ -719,6 +719,18 @@ int main(int argc, char *argv[]) { // Scripted button input (--input-script). input_script_tick(state); + // Keyboard VOL presses end after their fixed guest duration. + for (int k = 0; k < 2; k++) { + std::atomic &until = k ? state.kbd_vol_down_until : state.kbd_vol_up_until; + std::atomic &btn = k ? state.btn_vol_down : state.btn_vol_up; + uint64_t u = until.load(); + if (u && state.emu_usec >= u) { + until = 0; + btn = false; + printf("[input] UP %s @emu_usec=%llu\n", k ? "VOL_DOWN" : "VOL_UP ", + (unsigned long long)state.emu_usec); + } + } // Touch injection for the Nyx GUI popup (eMMC Issues Warning). // Nyx initializes after the IPL stage; we inject a tap once enough