"""Harness: the REAL cmd 0x0F entry path (what the driver actually invokes). hdcp_cmd0F_handler(req, resp) copies 0x244 bytes of the host request onto its own stack, then dispatches up to three subcommands. Crucially it contains a latch: ldr r1,[r2,#0x18] ; sess->err_state cmp r1,#7 beq ; with every response sub-status left at 100 So once ANY subcommand sets err_state = 7 (which subcmd12 does on essentially every failure path: bad length -> 109, bad flags -> 107, seq reject -> 108, MAC fail -> 106) the session is bricked for cmd 0x0F: later invocations return immediately, sub-status 100, and the subcommand handler never runs again. Request layout (relative to `req`, matching the driver's shm+0xC): +0x00 slot +0x04 desc1.id +0x08 desc1.len +0x0C desc2.id +0x10 desc2.len +0x14 desc3.id +0x1C payload[] (payload[0] must equal desc1.id) subcmd12 then sees msg = payload+1, len = desc1.len-1. """ import os import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) from pspemu import Component, CryptoService, AsdRpcService, consts # noqa: E402 SESS = consts.SESS TABLE_PTR = consts.HDCP["session_table_ptr"] # dword_40237C # TA entry points, resolved from the entry trampoline's literal pool at # 0x4042a0 (it dispatches on msg[0]: 1 create, 2 destroy, 3 open-session, # 4 close-session, 5 invoke). Selector 5 lands on 0x4013b4, the independently # known hdcp_invoke_entry, which cross-checks the decode. TA_CREATE = 0x40139C TA_INVOKE = 0x4013B4 CMD_OPEN_SESSION = 0x0E def setup(slots=8, crypto_overrun=False): """Run the TA's own init and use the TA's own session table. This used to allocate a table and patch `dword_40237C` to point at it. It no longer does: `TA_CreateEntryPoint` is executed and the table pointer is left at its image value (0x407000, the TA's zeroed BSS), so the TA is driven through the memory it actually uses rather than a harness fiction. """ c = Component("hdcp") c.set_rpc_services({consts.SERVICE_CRYPTO: CryptoService(overrun_uninitialised=crypto_overrun), consts.SERVICE_ASD: AsdRpcService(stub_status=0)}) c.run_function(TA_CREATE, (0, 0, 0, 0)) # real TA init table = c.rd32(TABLE_PTR) # image default, not ours return c, table def _invoke(c, cmd, sess_ctx=0): """Drive hdcp_invoke_entry with the ABI it actually requires. paramTypes low nibble must be 7 (MEMREF_INOUT), params[0].size must exceed 0x2817, and the command selector is the FIRST DWORD of that buffer -- not the GP `commandID` in R1, which this entry ignores entirely. Decoded in psp_out/FINDING-HOST-MAILBOX-AND-RING-IN-EMULATOR.md. """ shbuf = c.alloc(0x4000) c.write(shbuf, b"\x00" * 0x4000) c.wr32(shbuf, cmd) params = c.alloc(0x40) c.write(params, b"\x00" * 0x40) c.wr32(params + 0, shbuf) c.wr32(params + 4, 0x3000) return c.run_function(TA_INVOKE, (sess_ctx, 0, 7, params)) def open_slot(c, table, *, topology_type=6, mode=21, err_state=0): """Open a session through the TA's REAL open command (0x0E). `active`/`state` used to be written straight into the struct. They are now set by the firmware: one cmd 0x0E invoke takes a slot from (state=0, active=0) to (state=2, active=1) -- byte-for-byte what the old hand-written bypass produced, and nothing else in the 0x28c-byte session is touched by the open. The TA chooses the slot (it allocates sequentially from 1), so the caller does NOT get to name one. An earlier version of this let the caller request a slot and mirrored the bookkeeping onto it when the TA picked a different one -- which quietly reinstated the hand-written bypass for 2 of the 3 cases below and defeated the point of the change. `topology_type`, `mode` and `err_state` are still written here on purpose: they are protocol//attacker-supplied inputs to the case under test, not internal bookkeeping the harness is faking. Returns (slot_index, session_ptr). """ slot = real_open(c, table) if slot < 0: raise RuntimeError("cmd 0x0E did not open a session") s = table + consts.SESS_SIZE * slot c.wr32(s + SESS["topology_type"], topology_type) c.wr32(s + SESS["mode"], mode) c.wr32(s + SESS["err_state"], err_state) return slot, s def real_open(c, table, slots=8): """Issue cmd 0x0E and report which slot the TA opened (-1 if none).""" before = [(c.rd32(table + i * consts.SESS_SIZE), c.rd32(table + i * consts.SESS_SIZE + 8)) for i in range(slots)] _invoke(c, CMD_OPEN_SESSION) for i in range(slots): st = (c.rd32(table + i * consts.SESS_SIZE), c.rd32(table + i * consts.SESS_SIZE + 8)) if st != before[i] and st == (2, 1): return i return -1 def build_req(c, slot, n, *, seq=0, payload_byte=0x33): sub_len = 5 * n + 25 # subcmd12 branch-B length desc_len = sub_len + 1 # dispatch passes len-1 req = c.alloc(0x300) c.write(req, b"\x00" * 0x300) c.wr32(req + 0x00, slot) c.wr32(req + 0x04, 12) # desc1.id = subcommand 12 c.wr32(req + 0x08, desc_len) body = bytearray(desc_len) body[0] = 12 # payload[0] must match desc1.id m = memoryview(body)[1:] # this is subcmd12's `msg` m[0] = 0; m[1] = 0; m[2] = n & 0xFF; m[3] = 0 m[4] = 0; m[5] = 0 m[6] = seq & 0xFF; m[7] = (seq >> 8) & 0xFF; m[8] = (seq >> 16) & 0xFF for i in range(25, len(m)): m[i] = payload_byte c.write(req + 0x1C, bytes(body)) return req def invoke(c, req): resp = c.alloc(0x200) c.write(resp, b"\x00" * 0x200) try: rv = c.run_function(consts.HDCP["cmd0F_handler"], (req, resp)) fault = None except Exception as e: rv, fault = None, str(e) subs = [c.rd32(resp + off) for off in (20, 24, 28, 32, 36)] return dict(ret=rv, subs=subs, fault=fault) def main(): print("HDCP cmd 0x0F real entry path -- the err_state==7 latch\n") print("A) fresh session (err_state=0): subcommand actually runs") c, table = setup() slot, s = open_slot(c, table) r = invoke(c, build_req(c, slot, 40)) print(f" ret={r['ret']} subs={r['subs']} err_state={c.rd32(s+SESS['err_state'])} " f"rx_count={c.rd8(s+SESS['rx_count'])} fault={(r['fault'] or '')[:40]}") print("\nB) same session again (err_state now latched): handler short-circuits") r2 = invoke(c, build_req(c, slot, 90)) print(f" ret={r2['ret']} subs={r2['subs']} err_state={c.rd32(s+SESS['err_state'])} " f"rx_count={c.rd8(s+SESS['rx_count'])} fault={(r2['fault'] or '')[:40]}") print(" -> rx_count unchanged => subcmd12 was never called") print("\nC) pre-set err_state=7 on a fresh slot: identical short-circuit") c2, t2 = setup() slot2, s2 = open_slot(c2, t2, err_state=7) r3 = invoke(c2, build_req(c2, slot2, 90)) print(f" ret={r3['ret']} subs={r3['subs']} rx_count={c2.rd8(s2+SESS['rx_count'])}") print("\nD) control: fresh slot, N=90 in ONE shot (no prior failure)") c3, t3 = setup() slot3, s3 = open_slot(c3, t3) r4 = invoke(c3, build_req(c3, slot3, 90)) print(f" ret={r4['ret']} subs={r4['subs']} rx_count={c3.rd8(s3+SESS['rx_count'])} " f"fault={(r4['fault'] or 'none')[:60]}") if __name__ == "__main__": main()