aboutsummaryrefslogtreecommitdiffstats
path: root/psp_out/validate_disclosure_detector.py
blob: e9581127b39696fa20c34da515f3c242819d05ef (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
56
57
58
59
60
61
"""Validate the disclosure detector before trusting its negative.
1) How many bytes do the TAs actually write back? (if ~0, the scan is vacuous)
2) Does classify() actually fire on a known PSP-range value? (ground truth)
"""
import ctypes,struct,collections,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/'; SHMSZ=0x8000; MARK=0x5A
RANGES=[(0x00020000,0x00070000,'sOS'),(0x00400000,0x00412000,'HDCP'),
        (0x00D00000,0x00D50000,'ASD'),(0x00E10000,0x00E80000,'SYSDRV'),(0x0020A000,0x0020C000,'TAstage')]
def classify(v):
    for lo,hi,nm in RANGES:
        if lo<=v<hi: return nm
    return None
# --- 2) ground truth on the classifier ---
print("=== classifier ground truth ===")
for v,exp in [(0x00401ab0,'HDCP'),(0x00d0f960,'ASD'),(0x00e1597c,'SYSDRV'),(0x00016f28,None),
              (0x0002b4c0,'sOS'),(0x0020a100,'TAstage'),(0x5a5a5a5a,None),(0xdeadbeef,None)]:
    got=classify(v); ok='OK' if got==exp else 'FAIL'
    print("   %#010x -> %-8s expect %-8s %s"%(v,got,exp,ok))
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(SHMSZ)
    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=SHMSZ
    return psp._ring_submit(c).resp.session_id, cp
def inv(sid,shm,buf,tc):
    adev.vram.view(shm,SHMSZ)[:]=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)
    except RuntimeError: pass
    except TimeoutError: return None
    return bytes(adev.vram.view(shm,SHMSZ)[:])
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())
def topo(h):
    b=bytearray(SHMSZ); 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,dsh,b,2)
for h in range(8): topo(h)
print("\n=== how many bytes does each TA actually write back? (marker-filled buffer) ===")
print(" ta    cmd   bytes_written   first..last     sample")
for name,sid,shm,cmds in (('hdcp',hdcp,hsh,range(1,16)),('dtm',dtm,dsh,range(1,8))):
    for cmd in cmds:
        b=bytearray([MARK])*SHMSZ; struct.pack_into('<I',b,0,cmd)
        sent=bytes(b); out=inv(sid,shm,b,cmd)
        if out is None: print("  %-5s %#04x TIMEOUT"%(name,cmd)); continue
        diff=[i for i in range(SHMSZ) if sent[i]!=out[i]]
        if diff:
            print("  %-5s %#04x  %6d bytes  %#06x..%#06x  %s"%(name,cmd,len(diff),diff[0],diff[-1],out[diff[0]:diff[0]+16].hex()))
        else:
            print("  %-5s %#04x  %6d bytes"%(name,cmd,0))
print("sos_alive:",psp.is_sos_alive())