SPI flash dump + restore tools (#1654)

* dump

* restore

* fix

* update

* update

* cleanup

---------

Co-authored-by: Comma Device <device@comma.ai>
This commit is contained in:
Adeeb Shihadeh 2023-09-12 20:04:54 -07:00 committed by GitHub
parent d4e63daf34
commit 047d36398d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 1 deletions

View File

@ -1,5 +1,5 @@
from .python.constants import McuType, BASEDIR, FW_PATH, USBPACKET_MAX_SIZE # noqa: F401
from .python.spi import PandaSpiException, PandaProtocolMismatch # noqa: F401
from .python.spi import PandaSpiException, PandaProtocolMismatch, STBootloaderSPIHandle # noqa: F401
from .python.serial import PandaSerial # noqa: F401
from .python.canhandle import CanHandle # noqa: F401
from .python import (Panda, PandaDFU, # noqa: F401

32
tests/read_flash_spi.py Executable file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env python3
from panda import Panda, PandaDFU
if __name__ == "__main__":
try:
from openpilot.system.hardware import HARDWARE
HARDWARE.recover_internal_panda()
Panda.wait_for_dfu(None, 5)
except Exception:
pass
p = PandaDFU(None)
cfg = p.get_mcu_type().config
def readmem(addr, length, fn):
print(f"reading {hex(addr)} {hex(length)} bytes to {fn}")
max_size = 255
with open(fn, "wb") as f:
to_read = length
while to_read > 0:
l = min(to_read, max_size)
dat = p._handle.read(addr, l)
assert len(dat) == l
f.write(dat)
to_read -= len(dat)
addr += len(dat)
addr = cfg.bootstub_address
for i, sector_size in enumerate(cfg.sector_sizes):
readmem(addr, sector_size, f"sector_{i}.bin")
addr += sector_size

27
tests/restore_flash_spi.py Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env python3
from panda import Panda, PandaDFU, STBootloaderSPIHandle
if __name__ == "__main__":
try:
from openpilot.system.hardware import HARDWARE
HARDWARE.recover_internal_panda()
Panda.wait_for_dfu(None, 5)
except Exception:
pass
p = PandaDFU(None)
assert isinstance(p._handle, STBootloaderSPIHandle)
cfg = p.get_mcu_type().config
print("restoring from backup...")
addr = cfg.bootstub_address
for i, sector_size in enumerate(cfg.sector_sizes):
print(f"- sector #{i}")
p._handle.erase_sector(i)
with open(f"sector_{i}.bin", "rb") as f:
dat = f.read()
assert len(dat) == sector_size
p._handle.program(addr, dat)
addr += len(dat)
p.reset()