aboutsummaryrefslogtreecommitdiffstats
path: root/psp_out/probe_vccc_0x26.py
blob: faf570b60c7608ea1acb7e03351fa4d370051ae1 (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
71
"""Probe GFX ring cmd 0x26 -> tee_cmd_dispatch v6==38 -> sysdrv_rpc_109E
-> sysdrv_vccc_request (the double-fetch / underflow candidate).

sysdrv_vccc_request reads its 132-byte request from cmd_buf + 928:
    host[0]  bit0 must be set
    host[4]  must == 128   (validated at 0xE211AC, RE-READ at 0xE2121A)
    host[36..44]  compared big-endian against the real chip id
    host[0x64..0x84]  expected HMAC

Note 928 + 132 = 1060 > 1024, so the request overruns the mapped command
buffer.  If validate_client_range rejects that, the handler is structurally
unreachable from the GFX ring and we should see NO status change no matter
what we write.  A status change proves we are reaching the handler.

READ-ONLY / non-destructive: only submits cmd 0x26.  Never touches 0x07.
"""
import ctypes, json, sys
from tinygrad import Device
from tinygrad.runtime.autogen.am import am

d = Device["AMD"]; adev = d.iface.dev_impl; psp = adev.psp
SZ = ctypes.sizeof(am.struct_psp_gfx_cmd_resp)
assert SZ == 1024, SZ

def alive(): return adev.reg(f"{psp.reg_pref}_81").read() != 0

def submit(patch: dict, cmd_id=0x26):
    """patch: {byte_offset: bytes}. Returns status (or -1 on timeout)."""
    # sentinel fill so "PSP never wrote" is distinguishable from "wrote 0"
    sent = bytes(((0xA5 ^ (i & 0xFF)) & 0xFF) for i in range(SZ))
    adev.vram.view(psp.cmd_paddr, SZ)[:] = sent
    c = am.struct_psp_gfx_cmd_resp(cmd_id=cmd_id)
    buf = bytearray(memoryview(c).cast('B'))
    for off, val in patch.items():
        buf[off:off+len(val)] = val
    try:
        psp._ring_submit(am.struct_psp_gfx_cmd_resp.from_buffer_copy(bytes(buf)))
        return 0
    except RuntimeError:
        r = bytes(adev.vram.view(psp.cmd_paddr, SZ)[:])
        return int.from_bytes(r[864:868], "little")
    except TimeoutError:
        return -1

def u32(v): return int(v & 0xFFFFFFFF).to_bytes(4, "little")

print(f"sOS alive at start: {alive()}")
tests = [
    ("baseline (all-zero cmd)",            {}),
    ("req[0]=1 (bit0 set)",                {928: u32(1)}),
    ("req[4]=128",                         {932: u32(128)}),
    ("req[0]=1, req[4]=128  (both gates)", {928: u32(1), 932: u32(128)}),
    ("req[0]=1, req[4]=127  (wrong len)",  {928: u32(1), 932: u32(127)}),
    ("req[0]=1, req[4]=128, chipid junk",  {928: u32(1), 932: u32(128), 964: b"\xde\xad\xbe\xef"*2}),
    ("req[0]=0 (bit0 clear)",              {928: u32(0), 932: u32(128)}),
]
res = {}
base = None
for name, patch in tests:
    if not alive():
        print("!! sOS dead -- aborting"); break
    s = submit(patch)
    res[name] = s
    if base is None: base = s
    tag = "TIMEOUT" if s < 0 else hex(s)
    mark = "" if s == base else "   <-- DIFFERS"
    print(f"  {name:38s} status={tag:>12s}{mark}")

print(f"\nsOS alive at end: {alive()}")
json.dump({k: v for k, v in res.items()}, open("/tmp/vccc_0x26.json", "w"), indent=1)
print("distinct statuses:", sorted(set(res.values())))