aboutsummaryrefslogtreecommitdiffstats
path: root/psp_out/race_flash_sector.py
blob: b41c5cad7c2a51ea5457d2958efc471a37c7c267 (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
"""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")