aboutsummaryrefslogtreecommitdiffstats
path: root/hw/ras_len0_wedge.py
blob: d896cc554c8b652f47036fd906aa14a2b819ccf1 (plain) (blame)
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
#!/usr/bin/env python3
"""cmd_buf_len=0: the loader accepts it. Does the TA's own length check matter then?

HARD builds reject params[0].size < 0x30 before touching the buffer, OPEN builds do not.
If len=0 wedges on OPEN and errors cleanly on HARD, the wedge is attributable to the
missing check rather than to the loader.

Run as: len0.py <ta-tag>.  One TA per boot; afterwards a normal load is attempted to
report whether the PSP is still alive.
"""
import glob, struct, sys
sys.path.insert(0,'/root/psp/hw')
from tinygrad import Device
import tinygrad.runtime.autogen.am.am as am
from ring_wrap import install
tag=sys.argv[1]
adev=Device["AMD"].iface.dev_impl; submit=install(adev.psp,adev)
ARENA=0x10000; PAT=0x5A5A5A5A
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
def load(t,L):
    base=open(glob.glob('/root/psp/contall/%s*'%t)[0],'rb').read()
    ap_,amc=alloc(0x80000); cp,cmc=alloc(ARENA)
    n=(len(base)+3)&~3
    adev.vram.view(ap_,n)[:]=base+b'\x00'*(n-len(base))
    adev.vram.view(cp,ARENA)[:]=bytes(struct.pack('<I',PAT)*(ARENA//4)); adev.gmc.flush_hdp()
    c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_LOAD_TA); q=c.cmd.cmd_load_ta
    q.app_phy_addr_lo,q.app_phy_addr_hi=amc&0xFFFFFFFF,amc>>32; q.app_len=len(base)
    q.cmd_buf_phy_addr_lo,q.cmd_buf_phy_addr_hi=cmc&0xFFFFFFFF,cmc>>32; q.cmd_buf_len=L
    try: r=submit(c)
    except Exception as e: return None,cp,'exc %s'%str(e)[:40]
    st=r.resp.status&0xFFFFFFFF
    return (r.resp.session_id if st==0 else None),cp,('ok sid=%d'%r.resp.session_id if st==0 else '%#x'%st)
sid,cp,how=load(tag,0)
print('%-26s load(len=0) -> %s'%(tag,how), flush=True)
if sid is not None:
    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,0
    try: print('   invoke -> %#x'%(submit(c).resp.status&0xFFFFFFFF), flush=True)
    except Exception as e: print('   invoke -> EXCEPTION %s'%str(e)[:70], flush=True)
_,_,how2=load('ta_42-0-0-6_1c874c3c',0x1000)
print('   PSP alive afterwards? normal CSU load -> %s'%how2, flush=True)