1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
"""Is the N>=53 hang a DATA ABORT in the sOS crypto task?
Hypothesis: hdcp_verify_mac passes len = 5N+5 (270 > the 268-byte buffer) to the
service-1 crypto task via SVC 0xF2. That task dereferences the caller's buffer
for `len` bytes, faults, and sOS's fatal_exception_handler (@0xee4) parks it in
cpu_halt_spin() -- which is exactly what a permanent hang with a live sOS looks
like.
fatal_exception_handler publishes, before spinning:
[0x3010000+0xA4C] class<<28 | ... | (SPSR & 0x1F)<<8
[0x3010000+0xA50] faulting PC
[0x3010000+0xA54] DFSR/IFSR
[0x3010000+0xA58] DFAR/IFAR
[0x3200000+0x0A8] faulting PC
We never confirmed whether those land in the host-visible C2PMSG window. So:
snapshot ALL 128 C2PMSG registers before, after setup, and WHILE HUNG, and diff.
Any nonzero delta in the 14..29 range would be the fault dump AND would confirm
the mapping. A delta anywhere else is still a lead.
"""
import ctypes, struct, time
from tinygrad import Device
from tinygrad.runtime.autogen.am import am
from tinygrad.helpers import lo32, hi32
d=Device["AMD"]; adev=d.iface.dev_impl; psp=adev.psp
SZ=ctypes.sizeof(am.struct_psp_gfx_cmd_resp)
C2P=90112+64
def snap(): return [adev.rreg(C2P+n) for n in range(128)]
def stat(): return int.from_bytes(bytes(adev.vram.view(psp.cmd_paddr,SZ)[:])[864:868],"little")
def alloc(n):
pa=adev.mm.palloc(n,align=0x1000,zero=True); return pa,adev.paddr2mc(pa)
P='/Users/user/dev/amd-psp-re/psp_out/'
def load(cmd,blob):
ap,amc=alloc(0x40000); cp,cmc=alloc(0x8000)
adev.vram.view(ap,len(blob))[:]=blob; adev.gmc.flush_hdp()
adev.vram.view(psp.cmd_paddr,SZ)[:]=b"\x00"*SZ
c=am.struct_psp_gfx_cmd_resp(cmd_id=cmd); t=c.cmd.cmd_load_ta
t.app_phy_addr_hi,t.app_phy_addr_lo=hi32(amc),lo32(amc); t.app_len=len(blob)
t.cmd_buf_phy_addr_hi,t.cmd_buf_phy_addr_lo=hi32(cmc),lo32(cmc); t.cmd_buf_len=0x8000
r=psp._ring_submit(c); return r.resp.session_id,cp
base = snap()
print("boot snapshot: nonzero C2PMSG =", {n:hex(v) for n,v in enumerate(base) if v})
load(am.GFX_CMD_ID_LOAD_ASD, open(P+'asd_full.bin','rb').read())
dtm_sid,dtm_shm = load(am.GFX_CMD_ID_LOAD_TA, open(P+'dtm_full.bin','rb').read())
sid,shm = load(am.GFX_CMD_ID_LOAD_TA, open(P+'hdcp_full.bin','rb').read())
def inv(s,sh,buf,tc,timeout_note=""):
adev.vram.view(sh,0x8000)[:]=bytes(buf); adev.gmc.flush_hdp()
adev.vram.view(psp.cmd_paddr,SZ)[:]=b"\x00"*SZ
ic=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD); iv=ic.cmd.cmd_invoke_cmd
iv.session_id,iv.ta_cmd_id=s,tc
t0=time.time()
try: psp._ring_submit(ic); return 0, bytes(adev.vram.view(sh,0x2000)[:]), time.time()-t0
except RuntimeError: return stat(), bytes(adev.vram.view(sh,0x2000)[:]), time.time()-t0
except TimeoutError: return "TIMEOUT", None, time.time()-t0
def topo(h):
b=bytearray(0x8000); struct.pack_into('<I',b,0,2)
struct.pack_into('<10I',b,0x10,h,1,1,0,0,0,0,0,0,0)
ps,out,_=inv(dtm_sid,dtm_shm,b,2); return struct.unpack_from('<I',out,8)[0]
def hdcp(cmd,mut=None):
b=bytearray(0x8000); struct.pack_into('<I',b,0,cmd)
if mut: mut(b)
ps,out,dt=inv(sid,shm,b,cmd)
if out is None: return ps,None,None,dt
return ps, struct.unpack_from('<I',out,4)[0], struct.unpack_from('<I',out,0x1410)[0], dt
for h in range(8): topo(h)
setup = snap()
print("\nafter setup: delta vs boot =", {n:(hex(base[n]),hex(setup[n])) for n in range(128) if base[n]!=setup[n]})
def fire(slot,N):
a3=5*N+25; ln=a3+1
payload=bytearray(b'\x41'*(5*N))
def m(b):
struct.pack_into('<I',b,0xC,slot); struct.pack_into('<I',b,0x10,12)
struct.pack_into('<I',b,0x14,ln)
b[0x28]=12; b[0x29]=0; b[0x2A]=0; b[0x2B]=N&0xFF; b[0x2C]=0
b[0x2F]=0; b[0x30]=0; b[0x31]=0
b[0x42:0x42+len(payload)]=payload
return hdcp(0x0F,m)
ps,ret,slot,dt = hdcp(0x0E, lambda b: struct.pack_into('<I',b,0xC,0))
print("\n0x0E -> ta_ret=%s slot=%s" % (ret,slot))
print("\n--- N=52 (MAC len 265, known good) ---")
ps,r,_,dt = fire(slot,52); print(" psp=%s ta_ret=%s %.2fs" % (ps,r,dt))
pre = snap()
ps,ret,slot2,dt = hdcp(0x0E, lambda b: struct.pack_into('<I',b,0xC,0))
print("\n0x0E -> slot=%s" % slot2)
print("\n--- N=53 (MAC len 270, THE HANG) ---")
ps,r,_,dt = fire(slot2,53)
print(" psp=%s ta_ret=%s %.2fs" % (ps,r,dt))
post = snap()
print("\n=== C2PMSG delta ACROSS THE HANG (pre -> post) ===")
delta = {n:(hex(pre[n]),hex(post[n])) for n in range(128) if pre[n]!=post[n]}
for n,(a,b) in sorted(delta.items()):
tag = " <<< FAULT-DUMP WINDOW" if 14 <= n <= 29 else ""
print(" C2PMSG_%-3d %-12s -> %-12s%s" % (n,a,b,tag))
if not delta: print(" (no change anywhere in 0..127)")
print("\nfault window 14..29 now:", {n:hex(post[n]) for n in range(14,30) if post[n]} or "all zero")
print("sOS alive:", psp.is_sos_alive())
|