mirror of
https://github.com/Creased/RCM-Emulator.git
synced 2026-12-06 02:48:11 +01:00
Security Engine: CTR ignored its input and returned the raw keystream; CBC encrypt skipped the IV on the first block and saved the wrong chaining value, so every CMAC over more than one block was wrong; OFB was broken. The datapath now follows INPUT_SEL/XOR_POS/VCTRAM_SEL the way bdk drives it, and matches OpenSSL for ECB, CBC, OFB, CTR and CMAC. SHA-256 works in parts (SHA_CONTINUE), SE_OP_CTX_SAVE reads keys back as se_aes_ctx_get_keys() expects, unwrap honours the word quad, guest block counts and LL sizes are bounded, and unreachable buffers end the op with an error. Soft reboot now resets the whole SoC (GPIO/pinmux cache, UARTs, I2C, PMC, CAR, flow controller, SE, touch, SDMMC, display, ACTMON, ...) while keeping what hardware keeps (PMC SCRATCH0, the RTC, and the PMIC unless the reboot is a power cycle). DRAM gets fresh zero pages instead of a memset that made all 2 GB resident; stale BPMP translations are dropped; CPSR returns to SVC; scripts restart. Execution: a branch to address 0 no longer hangs the emulator (until=0 made PC 0 an exit); bdk's reset-then-bpmp_halt() reboots instead of exiting; an untimed halt with CPU0 running parks the BPMP and lets CPU0 continue; CPU0 starts in the same batch it is released in; CPU0 drops translations of pages the BPMP rewrote; shared models no longer see time run backwards. TRM cross-check: RST_CPUG_CMPLX resets to 0x2000feef and CPU0 runs on CPURESET0/CORERESET0/L2RESET/NONCPURESET (bit names corrected); RAM repair clears REQ and sets STS, and is no longer a run condition; refused releases are re-evaluated; MSELECT ENABLE_PCIE_APERTURE gates PCIe; RST_DEVICES_W/Y direct writes land; DL_UP is read-only; the whole config aperture and memory window decode; OSC_CTRL reports 38.4 MHz; PLLE/PLLREFE no longer get bit 27 forced into a divider; PMC is a R/W register file; RTC SECONDS counts; FLOW halt modes follow 17.2.2 and STOP_UNTIL_IRQ wakes on the armed TMR; UART IIR/IER/ loopback MSR; I2C NOACK code and empty I2C4/I2C6 buses; pinmux reset values; the COP ACTMON counts halt time. Peripherals: MAX77620 LDO power-good follows the rail (hwtest's gamecard probe no longer fails), the Erista GPU MAX77621 answers at 0x1C, regulators ACK per generation, RTC CONTROL is binary/24h, PS3 card detect reads "slot empty", packet-mode I2C parses 4 bytes per word, rawnand I/O splits at part boundaries, eMMC returns to its user area on CMD0, ADMA2 descriptors decode per spec, SD CMD6/ACMD13 data reaches the programmed buffer, ERR_INTERRUPT is reported, R1 follows block commands. Frontend and tooling: touch events queue instead of overwriting each other, map through rotation overrides and bdk's edge offset, and use finger ID 0; a scripted press is always seen before its release; zeroed fuses persist in the ini; dropped UTF-8 paths open on Windows; bad --input-script is an error; test scripts check exit status and missing arguments; make_sd.py quotes names and picks a FAT32-valid cluster size; the Dockerfile marks the mount safe for git. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YYewwDPujjo5B2xRyC5Grb
309 lines
9.6 KiB
C++
309 lines
9.6 KiB
C++
// Scripted button input - see input_script.h.
|
|
//
|
|
// Syntax (identical for a file and for an inline spec; in a file one event per
|
|
// line, inline they're separated by ',' or ';'):
|
|
//
|
|
// <when> <KEY> [hold_ms]
|
|
//
|
|
// <when> absolute time in ms ("3000"), or "+N" for N ms after the
|
|
// previous event was released. Bare "+" means "+400".
|
|
// <KEY> POWER | PWR | P
|
|
// VOL_DOWN | VOLDOWN | DOWN | D
|
|
// VOL_UP | VOLUP | UP | U
|
|
// hold_ms how long to hold the button, default 200 ms.
|
|
//
|
|
// '#' 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"
|
|
//
|
|
// or the same thing in a file:
|
|
//
|
|
// # pick the 7th menu entry
|
|
// 3000 VOL_DOWN
|
|
// + VOL_DOWN
|
|
// ...
|
|
// + POWER
|
|
|
|
#include "input_script.h"
|
|
#include "emu_state.h"
|
|
|
|
#include <cctype>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
enum class Key { Power, VolUp, VolDown, Uart, UartFile };
|
|
|
|
struct Event {
|
|
uint64_t at_us = 0; // press time
|
|
uint64_t until_us = 0; // release time
|
|
Key key = Key::Power;
|
|
bool pressed = false;
|
|
bool released = false;
|
|
std::string data; // UART / UARTFILE payload
|
|
};
|
|
|
|
std::vector<Event> g_events;
|
|
bool g_active = false;
|
|
size_t g_done = 0;
|
|
|
|
constexpr uint64_t kDefaultHoldMs = 200;
|
|
constexpr uint64_t kDefaultGapMs = 400;
|
|
|
|
const char *key_name(Key k) {
|
|
switch (k) {
|
|
case Key::Power: return "POWER";
|
|
case Key::VolUp: return "VOL+";
|
|
case Key::Uart: return "UART";
|
|
case Key::UartFile: return "UARTFILE";
|
|
default: return "VOL-";
|
|
}
|
|
}
|
|
|
|
bool parse_key(const std::string &tok, Key *out) {
|
|
std::string s;
|
|
for (char c : tok)
|
|
s.push_back((char)toupper((unsigned char)c));
|
|
if (s == "POWER" || s == "PWR" || s == "P") { *out = Key::Power; return true; }
|
|
if (s == "VOL_DOWN" || s == "VOLDOWN" || s == "DOWN" || s == "D") {
|
|
*out = Key::VolDown; return true;
|
|
}
|
|
if (s == "VOL_UP" || s == "VOLUP" || s == "UP" || s == "U") {
|
|
*out = Key::VolUp; return true;
|
|
}
|
|
// Feed bytes into the UART_B receive FIFO rather than pressing a button.
|
|
// UART pushes the rest of the line literally (with \n / \r escapes);
|
|
// UARTFILE pushes a file's raw contents, for binary protocol work.
|
|
if (s == "UART") { *out = Key::Uart; return true; }
|
|
if (s == "UARTFILE") { *out = Key::UartFile; return true; }
|
|
return false;
|
|
}
|
|
|
|
// Expand the small escape set we accept in a literal UART payload.
|
|
std::string unescape(const std::string &in) {
|
|
std::string out;
|
|
for (size_t i = 0; i < in.size(); i++) {
|
|
if (in[i] == '\\' && i + 1 < in.size()) {
|
|
char c = in[++i];
|
|
if (c == 'n') { out.push_back('\n'); continue; }
|
|
if (c == 'r') { out.push_back('\r'); continue; }
|
|
if (c == 't') { out.push_back('\t'); continue; }
|
|
if (c == '0') { out.push_back('\0'); continue; }
|
|
out.push_back(c);
|
|
continue;
|
|
}
|
|
out.push_back(in[i]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// Split on newlines, ',' and ';'. Strip '#' comments.
|
|
std::vector<std::string> split_events(const std::string &text) {
|
|
std::vector<std::string> out;
|
|
std::string cur;
|
|
bool in_comment = false;
|
|
for (char c : text) {
|
|
if (c == '#') in_comment = true;
|
|
if (c == '\n') { in_comment = false; }
|
|
if (in_comment) { if (c != '\n') continue; }
|
|
if (c == '\n' || c == ',' || c == ';') {
|
|
out.push_back(cur);
|
|
cur.clear();
|
|
} else {
|
|
cur.push_back(c);
|
|
}
|
|
}
|
|
out.push_back(cur);
|
|
return out;
|
|
}
|
|
|
|
bool parse_text(const std::string &text) {
|
|
uint64_t prev_release_us = 0;
|
|
int lineno = 0;
|
|
for (const std::string &raw : split_events(text)) {
|
|
lineno++;
|
|
// Tokenize on whitespace.
|
|
std::vector<std::string> tok;
|
|
{
|
|
std::string t;
|
|
for (char c : raw) {
|
|
if (isspace((unsigned char)c)) {
|
|
if (!t.empty()) { tok.push_back(t); t.clear(); }
|
|
} else {
|
|
t.push_back(c);
|
|
}
|
|
}
|
|
if (!t.empty()) tok.push_back(t);
|
|
}
|
|
if (tok.empty()) continue; // blank / comment-only
|
|
|
|
if (tok.size() < 2) {
|
|
fprintf(stderr, "[input-script] entry %d: expected '<when> <KEY> [hold_ms]', got '%s'\n",
|
|
lineno, raw.c_str());
|
|
return false;
|
|
}
|
|
|
|
Event ev;
|
|
const std::string &when = tok[0];
|
|
if (when[0] == '+') {
|
|
uint64_t gap = (when.size() > 1) ? strtoull(when.c_str() + 1, nullptr, 10)
|
|
: kDefaultGapMs;
|
|
ev.at_us = prev_release_us + gap * 1000ULL;
|
|
} else {
|
|
if (!isdigit((unsigned char)when[0])) {
|
|
fprintf(stderr, "[input-script] entry %d: bad time '%s'\n", lineno, when.c_str());
|
|
return false;
|
|
}
|
|
ev.at_us = strtoull(when.c_str(), nullptr, 10) * 1000ULL;
|
|
}
|
|
|
|
if (!parse_key(tok[1], &ev.key)) {
|
|
fprintf(stderr, "[input-script] entry %d: unknown key '%s'\n", lineno, tok[1].c_str());
|
|
return false;
|
|
}
|
|
|
|
if (ev.key == Key::Uart || ev.key == Key::UartFile) {
|
|
// Everything after the keyword is the payload; keep interior spacing.
|
|
size_t kpos = raw.find(tok[1]);
|
|
std::string rest = (kpos == std::string::npos)
|
|
? std::string()
|
|
: raw.substr(kpos + tok[1].size());
|
|
size_t b = rest.find_first_not_of(" \t");
|
|
rest = (b == std::string::npos) ? std::string() : rest.substr(b);
|
|
while (!rest.empty() && (rest.back() == ' ' || rest.back() == '\t' ||
|
|
rest.back() == '\r'))
|
|
rest.pop_back();
|
|
ev.data = (ev.key == Key::Uart) ? unescape(rest) : rest;
|
|
ev.until_us = ev.at_us; // instantaneous, nothing to release
|
|
prev_release_us = ev.at_us;
|
|
g_events.push_back(ev);
|
|
continue;
|
|
}
|
|
|
|
uint64_t hold_ms = (tok.size() >= 3) ? strtoull(tok[2].c_str(), nullptr, 10)
|
|
: kDefaultHoldMs;
|
|
if (hold_ms == 0) hold_ms = kDefaultHoldMs;
|
|
ev.until_us = ev.at_us + hold_ms * 1000ULL;
|
|
prev_release_us = ev.until_us;
|
|
g_events.push_back(ev);
|
|
}
|
|
return !g_events.empty();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool input_script_load(const char *spec) {
|
|
if (!spec || !*spec) return false;
|
|
g_events.clear();
|
|
g_done = 0;
|
|
|
|
std::string text;
|
|
if (FILE *f = fopen(spec, "rb")) {
|
|
char buf[4096];
|
|
size_t n;
|
|
while ((n = fread(buf, 1, sizeof(buf), f)) > 0)
|
|
text.append(buf, n);
|
|
fclose(f);
|
|
printf("[input-script] loaded from file: %s\n", spec);
|
|
} else {
|
|
text = spec; // treat as an inline spec
|
|
printf("[input-script] inline spec: %s\n", spec);
|
|
}
|
|
|
|
if (!parse_text(text)) {
|
|
fprintf(stderr, "[input-script] no usable events; script disabled\n");
|
|
g_events.clear();
|
|
return false;
|
|
}
|
|
|
|
g_active = true;
|
|
printf("[input-script] %zu event(s) queued\n", g_events.size());
|
|
return true;
|
|
}
|
|
|
|
bool input_script_active() { return g_active; }
|
|
|
|
void input_script_tick(EmuState &state) {
|
|
if (!g_active || g_done >= g_events.size()) return;
|
|
|
|
uint64_t now = state.emu_usec;
|
|
for (size_t i = g_done; i < g_events.size(); i++) {
|
|
Event &ev = g_events[i];
|
|
// Events are ordered by press time; stop at the first future one.
|
|
if (!ev.pressed && now < ev.at_us) break;
|
|
|
|
// 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.
|
|
if (ev.key == Key::Uart || ev.key == Key::UartFile) {
|
|
if (now >= ev.at_us && !ev.pressed) {
|
|
std::string bytes = ev.data;
|
|
if (ev.key == Key::UartFile) {
|
|
bytes.clear();
|
|
if (FILE *f = fopen(ev.data.c_str(), "rb")) {
|
|
char buf[4096];
|
|
size_t n;
|
|
while ((n = fread(buf, 1, sizeof(buf), f)) > 0)
|
|
bytes.append(buf, n);
|
|
fclose(f);
|
|
} else {
|
|
fprintf(stderr, "[input-script] cannot open UARTFILE '%s'\n",
|
|
ev.data.c_str());
|
|
}
|
|
}
|
|
for (unsigned char ch : bytes)
|
|
state.uart_rx_fifo[1].push_back(ch);
|
|
ev.pressed = ev.released = true;
|
|
printf("[input-script] UART_B <- %zu byte(s) @%llu us (%zu/%zu)\n",
|
|
bytes.size(), (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;
|
|
}
|
|
break;
|
|
}
|
|
|
|
std::atomic<bool> *btn = (ev.key == Key::Power) ? &state.btn_power
|
|
: (ev.key == Key::VolUp) ? &state.btn_vol_up
|
|
: &state.btn_vol_down;
|
|
if (!ev.pressed && now >= ev.at_us) {
|
|
btn->store(true);
|
|
ev.pressed = true;
|
|
printf("[input-script] %s down @%llu us (%zu/%zu)\n", key_name(ev.key),
|
|
(unsigned long long)now, i + 1, g_events.size());
|
|
fflush(stdout);
|
|
// Even if the release is already due - a FLOW_CTLR sleep can jump the
|
|
// clock past it within one batch - the payload has to run with the
|
|
// button down at least once, or it never sees the press.
|
|
break;
|
|
}
|
|
if (ev.pressed && !ev.released && now >= ev.until_us) {
|
|
btn->store(false);
|
|
ev.released = true;
|
|
if (i == g_done) {
|
|
g_done++;
|
|
if (g_done == g_events.size())
|
|
printf("[input-script] sequence complete\n");
|
|
}
|
|
}
|
|
if (!ev.released) break; // keep this event current until it's let go
|
|
}
|
|
}
|
|
|
|
void input_script_restart(EmuState &state) {
|
|
for (Event &ev : g_events)
|
|
ev.pressed = ev.released = false;
|
|
g_done = 0;
|
|
state.btn_power = false;
|
|
state.btn_vol_up = false;
|
|
state.btn_vol_down = false;
|
|
}
|