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
|
#!/usr/bin/env python3
"""Declare a cmd_buf window LARGER than the real allocation: does the PSP write past it?
Pass 97 showed an UNDERSIZED window hangs rather than writing out of bounds. The opposite
case is the corruption shape and is untested: LOAD_TA accepts cmd_buf_len far larger than the
allocation (pass 86), so if the PSP trusts that number, a command with large output writes
past memory the host actually allocated.
Method: allocate 0x20000 and fill it all with a guard pattern; treat the first 0x8000 as the
"buffer" but declare cmd_buf_len = 0x100000; invoke commands known to produce output; then
check whether anything beyond 0x8000 changed.
Control: writes inside the first 0x8000 must occur, else the command did nothing and the
result is void.
"""
import glob, struct, sys, time
sys.path.insert(0,'/root/psp/hw')
from tinygrad import Device
import tinygrad.runtime.autogen.am.am as am
from ring_wrap import install
adev=Device["AMD"].iface.dev_impl; psp=adev.psp
submit=install(psp,adev)
PAT=0x5A5A5A5A; REAL=0x20000; DECL=0x100000
def alloc(n):
pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
def test(tag, stem, cmds):
blob=open(glob.glob('/root/psp/contall/'+stem+'*')[0],'rb').read()
ap_,amc=alloc(0x80000); cp,cmc=alloc(REAL)
n=(len(blob)+3)&~3
adev.vram.view(ap_,n)[:]=blob+b'\x00'*(n-len(blob)); adev.gmc.flush_hdp()
c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_LOAD_TA); t=c.cmd.cmd_load_ta
t.app_phy_addr_lo,t.app_phy_addr_hi=amc&0xFFFFFFFF,amc>>32; t.app_len=len(blob)
t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=cmc&0xFFFFFFFF,cmc>>32
t.cmd_buf_len=DECL # far larger than REAL
try: r=submit(c); st=r.resp.status&0xFFFFFFFF; sid=r.resp.session_id
except Exception as e: print('%-6s load failed'%tag, flush=True); return
print('%-6s load %#x sid=%s (declared %#x, real alloc %#x)'%(tag,st,sid,DECL,REAL), flush=True)
for cid in cmds:
buf=bytearray(struct.pack('<I',PAT)*(REAL//4)); struct.pack_into('<I',buf,0,cid)
adev.vram.view(cp,REAL)[:]=bytes(buf); adev.gmc.flush_hdp()
c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD)
iv=c.cmd.cmd_invoke_cmd; iv.session_id,iv.ta_cmd_id=sid,cid
t0=time.time()
try: s=submit(c).resp.status&0xFFFFFFFF
except Exception as e:
try: s=int(str(e).strip().split()[-1])&0xFFFFFFFF
except Exception: s=-1
dt=time.time()-t0
adev.gmc.flush_hdp()
out=bytes(adev.vram.view(cp,REAL)[:])
ins=sum(1 for o in range(4,0x8000,4) if struct.unpack_from('<I',out,o)[0]!=PAT)
bey=[(o,struct.unpack_from('<I',out,o)[0]) for o in range(0x8000,REAL,4)
if struct.unpack_from('<I',out,o)[0]!=PAT]
print(' cmd %-2d -> %#-11x %4.1fs in-window=%-4d beyond-0x8000=%d %s'
%(cid,s,dt,ins,len(bey),bey[:2] if bey else ''), flush=True)
if dt>5: print(' (stalled, aborting)', flush=True); return
test('AUC','ta_16-0-0-9_9e9044cf',[5])
test('CSU','ta_42-0-0-6_1c874c3c',[1,2])
test('OC','ta_50-0-0-1_cbd47c59',[1,2])
|