aboutsummaryrefslogtreecommitdiffstats
path: root/psp_out/probe_asd_as_ta.py
blob: fa60ce2e90660b8b2440f787d116806309988a6a (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
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Can the host invoke ASD DIRECTLY?

If yes, the reachability argument that killed several findings ("no TA emits
that opcode") collapses:
  asd_hdcp2_op_1AA  - integer overflow -> unbounded 5*N stack OOB read
  asd 0x301         - content-controlled write of up to 5120 bytes
LOAD_ASD(4) gave session_id 0 and every INVOKE returned psp_status 4. Both
cmd 1 and cmd 4 route to tee_load_ta, so try loading the ASD image as a normal
TA and invoking THAT session. Also try driving the opcode via frame[0] as well
as ta_cmd_id, since asd_rpc_dispatch reads its opcode from the frame.
"""
import ctypes,struct,time,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
    try:
        r=psp._ring_submit(c); return r.resp.session_id,cp,0
    except RuntimeError: return None,cp,stat()
def inv(sid,shm,frame0,tc):
    b=bytearray(S); struct.pack_into('<I',b,0,frame0)
    adev.vram.view(shm,S)[:]=bytes(b); 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
    t0=time.time()
    try: psp._ring_submit(ic); ps=0
    except RuntimeError: ps=stat()
    except TimeoutError: return "TIMEOUT",None,time.time()-t0
    return ps, bytes(adev.vram.view(shm,0x40)[:]), time.time()-t0
asdblob=open(P+'asd_full.bin','rb').read()
for nm,cid in (("LOAD_ASD(4)",am.GFX_CMD_ID_LOAD_ASD),("LOAD_TA(1)",am.GFX_CMD_ID_LOAD_TA)):
    sid,shm,err=load(cid,asdblob)
    print("\n=== %s -> session_id=%s (load err %s) ==="%(nm,sid,err))
    if sid is None: continue
    print("  opcode    via ta_cmd_id           via frame[0]")
    for op,note in ((0x300,'SRM read'),(0x301,'*** 5120B write ***'),(0x1AA,'*** 5*N OOB read ***'),
                    (0x1A7,'set_topology'),(0x100,'topology upd'),(0x200,'service'),(0x999,'bogus')):
        p1,o1,_=inv(sid,shm,0,op)          # opcode in ta_cmd_id only
        p2,o2,_=inv(sid,shm,op,op)         # opcode in frame[0] AND ta_cmd_id
        f1=o1[:8].hex() if o1 else '-'; f2=o2[:8].hex() if o2 else '-'
        print("  %#06x %-22s ps=%-10s %-18s ps=%-10s %s"%(op,note,
              p1 if isinstance(p1,str) else hex(p1), f1, p2 if isinstance(p2,str) else hex(p2), f2))
print("\nsos_alive:",psp.is_sos_alive())