#!/usr/bin/env python3 """Image source and command buffer aliasing the same memory. Never tested: every LOAD_TA so far used two disjoint allocations. If the loader copies the image out and then treats an overlapping region as the command buffer -- or writes command results over the image while authenticating -- aliasing is where that shows. Cases (health control after each): A disjoint (control) B cmd_buf == app_phy_addr exactly C cmd_buf inside the image (app + 0x1000) D app inside the cmd_buf region """ import glob, struct, sys, time sys.path.insert(0,'/root/psp/hw') from tinygrad import Device import tinygrad.runtime.autogen.am.am as am from ring_wrap import install adev=Device["AMD"].iface.dev_impl; psp=adev.psp submit=install(psp,adev) blob=open(glob.glob('/root/psp/contall/ta_42-0-0-6_1c874c3c*')[0],'rb').read() def alloc(n): pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa) def attempt(tag, same=False, cmd_delta=None, app_in_cmd=False): ap_,amc=alloc(0x100000); cp,cmc=alloc(0x20000) n=(len(blob)+3)&~3 adev.vram.view(ap_,n)[:]=blob+b'\x00'*(n-len(blob)); adev.gmc.flush_hdp() a=amc; b=cmc if same: b=amc elif cmd_delta is not None: b=amc+cmd_delta elif app_in_cmd: adev.vram.view(cp,n)[:]=blob+b'\x00'*(n-len(blob)); adev.gmc.flush_hdp() a=cmc; b=cmc 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=a&0xFFFFFFFF,a>>32; t.app_len=len(blob) t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=b&0xFFFFFFFF,b>>32; t.cmd_buf_len=0x4000 t0=time.time(); sid=None try: r=submit(c); st=r.resp.status&0xFFFFFFFF; sid=r.resp.session_id except Exception as e: try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF except Exception: st=-1 dt=time.time()-t0 extra='' if st==0 and sid is not None and dt<5: bb=bytearray(0x4000); struct.pack_into(' %#-10x %.1fs %s%s'%(tag,a,b,st,dt,extra, ' <<< STALL' if dt>5 else ''), flush=True) return dt>5 if attempt('A control: disjoint'): sys.exit(3) if attempt('B cmd_buf == app', same=True): sys.exit(3) if attempt('C cmd_buf inside image', cmd_delta=0x1000): sys.exit(3) if attempt('D app inside cmd region', app_in_cmd=True): sys.exit(3) attempt('HEALTH control: disjoint')