aboutsummaryrefslogtreecommitdiffstats
path: root/hw/ta_fieldfuzz.py
blob: da2e1fecd00734ef2336cca950f2593f26543bf9 (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
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""Per-field mutation fuzz: one dword at a time, rest zero.

The earlier profiles filled every field at once, which a first-stage validator will usually
reject, so deeper code was never reached. This mutates ONE dword per invoke (offsets 4..0x80)
with a set of interesting values, leaving the rest zero, and reports any field/value that
changes the status, stalls, or writes past the declared window.

Controls: the unmutated baseline status per command is recorded first; a field is only
reported if it differs from that baseline.
"""
import glob, struct, sys, time
from tinygrad import Device
import tinygrad.runtime.autogen.am.am as am
adev = Device["AMD"].iface.dev_impl; psp = adev.psp
PAT=0xA5A5A5A5; DECL=0x4000; TOTAL=0x8000
VALUES=[0xFFFFFFFF, 0x80000000, 0x7FFFFFFF, 0x00001000, 0xFFFFFFF0]
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa, adev.paddr2mc(pa)
def load(blob):
    ap_,amc=alloc(0x80000); cp,cmc=alloc(TOTAL)
    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
    try:
        r=psp._ring_submit(c); return r.resp.status&0xFFFFFFFF, r.resp.session_id, cp
    except Exception as e:
        try: return int(str(e).strip().split()[-1])&0xFFFFFFFF,None,cp
        except Exception: return -1,None,cp
def inv(sid,shm,cid,off=None,val=0):
    buf=bytearray(TOTAL); struct.pack_into('<I',buf,0,cid)
    if off is not None: struct.pack_into('<I',buf,off,val)
    for o in range(DECL,TOTAL,4): struct.pack_into('<I',buf,o,PAT)   # guard region
    adev.vram.view(shm,TOTAL)[:]=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:
        r=psp._ring_submit(c); st=r.resp.status&0xFFFFFFFF
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
    dt=time.time()-t0
    adev.gmc.flush_hdp()
    out=bytes(adev.vram.view(shm,TOTAL)[:])
    bey=sum(1 for o in range(DECL,TOTAL,4) if struct.unpack_from('<I',out,o)[0]!=PAT)
    return st,dt,bey
TAS={'AUC':('ta_16-0-0-9_9e9044cf',{6}),'CSU':('ta_42-0-0-6_1c874c3c',set()),
     'FP':('ta_19-0-0-8_7e8be282',{7}),'GCP':('ta_22-0-0-77_7c426e7f',set()),
     'MERIT':('ta_24-0-0-70_5cd4afda',set()),'OC':('ta_50-0-0-1_cbd47c59',set()),
     'OPM':('ta_25-0-0-81_dc596ba6',set()),'XGMI':('ta_32-0-0-20_20a3d1a4',set())}
for name in sys.argv[1].split(','):
    stem,skip=TAS[name]
    cands=glob.glob('/root/psp/contall/'+stem+'*')
    st,sid,shm=load(open(cands[0],'rb').read())
    if st!=0 or sid is None: print('%-6s LOAD %#x'%(name,st)); continue
    findings=[]
    for cid in range(0,8):
        if cid in skip: continue
        base,_,_ = inv(sid,shm,cid)
        for off in range(4,0x80,4):
            for v in VALUES:
                s,dt,bey = inv(sid,shm,cid,off,v)
                if bey: findings.append(('OOB',cid,hex(off),hex(v),bey)); break
                if dt>5: findings.append(('STALL',cid,hex(off),hex(v))); break
                if s!=base: findings.append(('STATUS',cid,hex(off),hex(v),hex(base),hex(s))); break
            if findings and findings[-1][0] in ('OOB','STALL'): break
    print('%-6s %s'%(name, findings[:6] if findings else 'no field mutation changed behaviour'))