"""Write one flash sector in the window after power-on, before the PSP gates ROM_SW. The ROM_SW engine is ungated only until the PSP boots. Cycle PCIe power and then retry the sector write immediately, as fast as possible, until it lands. """ import sys, time sys.path.insert(0, '/Users/user/src/tinygrad/extra/amdflash') from tinygrad.runtime.support.usb import USB3 IMAGE = sys.argv[1] SECTOR = int(sys.argv[2], 0) devs = USB3.list_devices(0xADD1, 0x0001) + USB3.list_devices(0x3801, 0x0001) dev, bus = devs[0] usb = USB3(dev) lt = lambda: usb.control_read(0xE4, 1, value=0xB450)[0] print("cycling PCIe power...") usb.control_write(0xF3, value=0, timeout=10000) time.sleep(6) usb.control_write(0xF3, value=1, timeout=10000) t0 = time.time() for _ in range(400): if lt() == 0x78: break time.sleep(0.01) print("link up at %.3fs" % (time.time() - t0)) del usb # release the handle so open_gpu can claim it from common import open_gpu from romless import Navi31SPI, GD25 # noqa data = open(IMAGE, 'rb').read() off = SECTOR * 0x1000 payload = data[off:off + 0x1000] for attempt in range(60): try: d = open_gpu(0, 'usb') spi = Navi31SPI(d) chip = GD25(spi) st = chip.status() chip.write_enable() print("attempt %d at t=%.3fs: engine RESPONDED (SR1=%#04x)" % (attempt, time.time()-t0, st)) break except Exception as e: if attempt % 10 == 0: print(" attempt %d t=%.3fs: %s" % (attempt, time.time()-t0, str(e)[:60])) time.sleep(0.05) else: print("never caught the ungated window")