#!/usr/bin/env python3 """Sweep DMCUB's PSP-owned windows (CW0 code, CW1 data) with the FINDING-10 oracle. FINDING-10 (`../AUDIT-DMCUB-05-REGSEQ.md`) gives one bit per inbox command, which is far too slow to dump 283 KB of CW0 bit-by-bit. Two tricks make a full sweep practical: 1. **Verify instead of read.** The oracle tests `(v & mask) == expected` for arbitrary mask/expected, so with `mask = 0xffffffff` and `expected =` the word from the signed image on disk, **one command covers a whole 32-bit word**: fast means "this word is exactly what public linux-firmware contains", slow means "this word differs". Only differing words cost 32 probes. At `iters = 300` a match costs ~0.16 ms. 2. **Classify instead of read** for CW1, where there is no ground truth: two commands per word test `== 0` and `== 0xffffffff`, which is enough to map the window's structure and find the words worth recovering in full. The point of the sweep is to answer: does the PSP-loaded image in the TMR differ from the public firmware file, and is there anything in these host-denied windows that is *not* public? Every address probed is clamped to the window tops reported by dmcub_bringup, so the firmware never touches unbacked memory (../../psp_out/CONSTRAINTS.md). usage: python3 dmcub_regseq_sweep.py --state state.json --apply --cw0 python3 dmcub_regseq_sweep.py --state state.json --apply --cw1 --stride 4 """ import argparse import json import os import struct import sys import time sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import dmcub_hwregs as H # noqa: E402 from dmcub_poc_regseq import RegSeq, addr_to_idx, frame_reg_wait # noqa: E402 CW0_BASE = 0x60000000 CW1_BASE = 0x61000000 class Sweeper: def __init__(self, rs, iters=300): self.rs = rs self.rs.iters = iters self.iters = iters self.n_cmd = 0 def calibrate(self, addr, truth): """match/mismatch separation on a word whose value we already know.""" idx = addr_to_idx(addr) _, t_ok = self.rs.submit_timed(frame_reg_wait(idx, 0xffffffff, truth, self.iters)) _, t_no = self.rs.submit_timed( frame_reg_wait(idx, 0xffffffff, truth ^ 0xffffffff, self.iters)) self.n_cmd += 2 if not (t_no > t_ok * 2 and (t_no - t_ok) > 0.0002): return None thr = t_ok + (t_no - t_ok) / 2.0 print(' calibration: match %.2f ms, mismatch %.2f ms -> threshold %.2f ms' % (1000 * t_ok, 1000 * t_no, 1000 * thr)) return thr def equals(self, addr, value, thr, tries=1): """True if the word at addr equals value. A single command is enough for the common case, but the fast path is only ~0.16 ms and host-side scheduling noise occasionally pushes it past the threshold. Measured false-positive rate on a full CW0 pass: 371/70979 = 0.5%% of words looked like mismatches and every one of them turned out to equal the image when read bit-by-bit. So a *mismatch* is only believed if it reproduces `tries` times; a single fast reading is conclusive for equality. """ for _ in range(max(1, tries)): _, t = self.rs.submit_timed( frame_reg_wait(addr_to_idx(addr), 0xffffffff, value, self.iters)) self.n_cmd += 1 if t < thr: return True return False def recover(self, addr, thr, tries=4): """Read a word, dual-polarity with retries. Returns (value, unreliable_bits). A single-polarity version of this produced garbage that looked like real data: sweeping the CW0 tail it reported words such as 0xffbbdcff / 0x06580007, which I nearly wrote up as TMR residue. Re-reading each bit in both polarities with retries showed the true contents are a stable 0xffffffff / 0x00000000. One jitter-slow reading silently clears a set bit, so cross-checking polarities is mandatory, not optional. """ val, bad = 0, [] for b in range(32): m = 1 << b def fast(expected): for _ in range(tries): _, t = self.rs.submit_timed( frame_reg_wait(addr_to_idx(addr), m, expected, self.iters)) self.n_cmd += 1 if t < thr: return True return False one, zero = fast(m), fast(0) if one == zero: bad.append(b) elif one: val |= m return val, bad def image_reader(path): raw = open(path, 'rb').read() ucode_off = struct.unpack_from('