Add SNAP to input scripts

This commit is contained in:
Baptiste MOINE committed 2026-11-05 23:45:43 +01:00
1 parent 4177e98dfb
commit d5bf5922ed
5 files changed
+99 -16

No files matched your search

+2 -2
View File
@@ -29,7 +29,7 @@ slow or destructive.
- Replay deterministic button sequences via `--auto-pin-recovery` or - 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 `--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, 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 - 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 storage and gamepad gadgets enumerate on both the Erista (USB2) and Mariko
(XUSB) device controllers, and the PC reads a mass storage disk back, (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`. | | `--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). | | `--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. | | `--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: `<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, `<ms> SNAP <file>` saves the next displayed frame to `<file>` (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-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. |
+31 -11
View File
@@ -1,4 +1,5 @@
#include "sdl_display.h" #include "sdl_display.h"
#include <string>
#include "config_window.h" #include "config_window.h"
#include "console_window.h" #include "console_window.h"
#include "../emu_state.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); 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 // 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 // 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. // 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; uint32_t step = shift ? 4096 : 128;
switch (event.key.keysym.sym) { switch (event.key.keysym.sym) {
case SDLK_UP: 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); 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; break;
case SDLK_DOWN: 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); 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; break;
case SDLK_RETURN: case SDLK_RETURN:
if (!state->btn_power) if (!state->btn_power)
@@ -707,14 +733,8 @@ bool sdl_display_poll_events(EmuState *state, uc_engine *uc) {
if (event.type == SDL_KEYUP) { if (event.type == SDL_KEYUP) {
switch (event.key.keysym.sym) { switch (event.key.keysym.sym) {
case SDLK_UP: 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: case SDLK_DOWN:
if (state->btn_vol_down) // The press ends on its own, 150 ms of guest time after it began.
printf("[input] UP VOL_DOWN @emu_usec=%llu\n", (unsigned long long)state->emu_usec);
state->btn_vol_down = false;
break; break;
case SDLK_RETURN: case SDLK_RETURN:
if (state->btn_power) if (state->btn_power)
+25
View File
@@ -6,6 +6,7 @@
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
#include <deque> #include <deque>
#include <mutex>
#include <vector> #include <vector>
#include <string> #include <string>
@@ -215,6 +216,12 @@ struct EmuState {
std::atomic<bool> btn_vol_up{false}; std::atomic<bool> btn_vol_up{false};
std::atomic<bool> btn_vol_down{false}; std::atomic<bool> btn_vol_down{false};
std::atomic<bool> btn_power{false}; std::atomic<bool> 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<uint64_t> kbd_vol_up_until{0};
std::atomic<uint64_t> kbd_vol_down_until{0};
// Touchscreen state (FTS4/STMFTS stub on I2C3 @ slave 0x49). // Touchscreen state (FTS4/STMFTS stub on I2C3 @ slave 0x49).
// Updated by SDL mouse events; consumed by i2c3_*/STMFTS code on CPU thread. // 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); tc_q_tail.store(tail + 1);
return true; 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<std::string> snap_paths;
void snap_request(const std::string &path) {
std::lock_guard<std::mutex> lk(snap_mtx);
snap_paths.push_back(path);
}
bool snap_take(std::string *path) {
std::lock_guard<std::mutex> 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. // last_rot mirrors the rotation last applied by sdl_display_update.
// Read from the SDL event handler to invert the display→window transform. // Read from the SDL event handler to invert the display→window transform.
std::atomic<uint32_t> last_rot{0}; std::atomic<uint32_t> last_rot{0};
+29 -3
View File
@@ -17,6 +17,13 @@
// touches the screen at (x, y) in the 1280x720 landscape picture bdk's // touches the screen at (x, y) in the 1280x720 landscape picture bdk's
// touch driver reports (Nyx's coordinates), and lifts after hold_ms. // touch driver reports (Nyx's coordinates), and lifts after hold_ms.
// //
// <when> SNAP <file>
//
// writes the next frame the display shows to <file>, 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: // '#' 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" // rcm_emu payload.bin --input-script "3000 D, + D, + D, + D, + D, + D, + P"
@@ -42,7 +49,7 @@
namespace { namespace {
enum class Key { Power, VolUp, VolDown, Uart, UartFile, Tap }; enum class Key { Power, VolUp, VolDown, Uart, UartFile, Tap, Snap };
struct Event { struct Event {
uint64_t at_us = 0; // press time uint64_t at_us = 0; // press time
@@ -50,7 +57,7 @@ struct Event {
Key key = Key::Power; Key key = Key::Power;
bool pressed = false; bool pressed = false;
bool released = 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 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::Uart: return "UART";
case Key::UartFile: return "UARTFILE"; case Key::UartFile: return "UARTFILE";
case Key::Tap: return "TAP"; case Key::Tap: return "TAP";
case Key::Snap: return "SNAP";
default: return "VOL-"; 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 == "UART") { *out = Key::Uart; return true; }
if (s == "UARTFILE") { *out = Key::UartFile; return true; } if (s == "UARTFILE") { *out = Key::UartFile; return true; }
if (s == "TAP" || s == "T") { *out = Key::Tap; return true; } if (s == "TAP" || s == "T") { *out = Key::Tap; return true; }
if (s == "SNAP") { *out = Key::Snap; return true; }
return false; return false;
} }
@@ -175,7 +184,7 @@ bool parse_text(const std::string &text) {
return false; 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. // Everything after the keyword is the payload; keep interior spacing.
size_t kpos = raw.find(tok[1]); size_t kpos = raw.find(tok[1]);
std::string rest = (kpos == std::string::npos) 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. // Events are ordered by press time; stop at the first future one.
if (!ev.pressed && now < ev.at_us) break; 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 // 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 // (index 1 = UART_B, the port payloads use for their debug console) and
// retire the event immediately. // retire the event immediately.
+12
View File
@@ -719,6 +719,18 @@ int main(int argc, char *argv[]) {
// Scripted button input (--input-script). // Scripted button input (--input-script).
input_script_tick(state); input_script_tick(state);
// Keyboard VOL presses end after their fixed guest duration.
for (int k = 0; k < 2; k++) {
std::atomic<uint64_t> &until = k ? state.kbd_vol_down_until : state.kbd_vol_up_until;
std::atomic<bool> &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). // Touch injection for the Nyx GUI popup (eMMC Issues Warning).
// Nyx initializes after the IPL stage; we inject a tap once enough // Nyx initializes after the IPL stage; we inject a tap once enough