#!/usr/bin/env python3 """Detect the unsigned VCN write by diffing the VCN MMIO window. No register names needed: snapshot the window, perform a type-49 write carrying a distinctive value, snapshot again, diff. A register holding our magic proves (a) the writes really happen and (b) the index -> register mapping. Controls: * a no-write baseline diff (snapshot twice with no command in between), to measure how much the window moves on its own; * a rejected command (missing index 195) between snapshots, which must not produce our magic. Progress to /tmp/vcndet.txt. """ import struct, time from tinygrad import Device import tinygrad.runtime.autogen.am.am as am OUT = "/tmp/vcndet.txt" MAGIC = 0xA5A50000 def say(s): with open(OUT, "a") as f: f.write(s + "\n") print(s, flush=True) adev = Device["AMD"].iface.dev_impl psp = adev.psp hb = adev.reg("regMP0_SMN_C2PMSG_81") def alive(): a = hb.read(); time.sleep(0.25); return hb.read() != a # VCN MMIO base from the card's own IP discovery BASE, END = 0x7800, 0x7F00 def snap(): out = {} for r in range(BASE, END): try: out[r] = adev.rreg(r) except Exception: pass return out def diff(a, b): return {k: (a.get(k), b.get(k)) for k in b if a.get(k) != b.get(k)} def submit(payload, fw_type=49): psp._prep_msg1(memoryview(bytes(payload))) cmd = am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_LOAD_IP_FW) cmd.cmd.cmd_load_ip_fw.fw_phy_addr_lo = psp.msg1_addr & 0xFFFFFFFF cmd.cmd.cmd_load_ip_fw.fw_phy_addr_hi = psp.msg1_addr >> 32 cmd.cmd.cmd_load_ip_fw.fw_size = len(payload) cmd.cmd.cmd_load_ip_fw.fw_type = fw_type try: return psp._ring_submit(cmd).resp.status & 0xFFFFFFFF except RuntimeError as e: return int(str(e).rsplit(" ", 1)[-1]) & 0xFFFFFFFF def ent(i, v): return struct.pack("