diff --git a/openpilot/selfdrive/pandad/pandad.py b/openpilot/selfdrive/pandad/pandad.py index ded76e58cf..c673aaa1f8 100755 --- a/openpilot/selfdrive/pandad/pandad.py +++ b/openpilot/selfdrive/pandad/pandad.py @@ -8,6 +8,7 @@ import subprocess from pathlib import Path from panda import Panda, PandaDFU, PandaProtocolMismatch, McuType, FW_PATH +from panda.python.legacy_spi import migrate_legacy_tres_firmware from openpilot.common.basedir import BASEDIR from openpilot.common.params import Params from openpilot.common.hardware import HARDWARE @@ -36,7 +37,19 @@ def flash_panda(panda_serial: str, choice: FirmwareChoice = DEFAULT_FIRMWARE_CHO if panda.bootstub or panda_signature != fw_signature: cloudlog.info("Panda firmware out of date, update required") - panda.flash(fn=fw_path) + if not panda.bootstub and panda.get_type() == Panda.HW_TYPE_TRES and panda.is_connected_spi() and HARDWARE.get_device_type() == "tizi": + panda.reset(enter_bootstub=True, reconnect=False) + if Panda.wait_for_panda(panda_serial, timeout=1): + panda.reconnect() + if not panda.bootstub: + raise PandaProtocolMismatch("TRES did not enter bootstub for firmware update") + panda.flash(fn=fw_path) + elif migrate_legacy_tres_firmware(fw_path, expected_serial=panda_serial): + panda.reconnect() + else: + raise PandaProtocolMismatch("TRES bootstub could not be reached") + else: + panda.flash(fn=fw_path) cloudlog.info("Done flashing") if panda.bootstub: @@ -124,6 +137,7 @@ def main() -> None: cloudlog.exception("pandad.uncaught_exception") count = 0 + legacy_migration_attempted = False while not do_exit: try: cloudlog.event("pandad.flash_and_connect", count=count) @@ -142,9 +156,21 @@ def main() -> None: wait_for_internal_panda() panda_serials = Panda.list() + choice = None + if not panda_serials and not legacy_migration_attempted and HARDWARE.get_device_type() == "tizi": + legacy_migration_attempted = True + HARDWARE.reset_internal_panda() + time.sleep(0.2) + choice = startup_choice(Params()) + filename = firmware_path(FW_PATH, McuType.H7.config.app_fn, choice) + if (serial := migrate_legacy_tres_firmware(filename)) is not None: + if not Panda.wait_for_panda(serial, timeout=5): + raise PandaProtocolMismatch("Legacy Panda migration did not start current firmware") + panda_serials = Panda.list() if len(panda_serials): cloudlog.info(f"{len(panda_serials)} panda found, connecting - {panda_serials}") - choice = startup_choice(Params()) + if choice is None: + choice = startup_choice(Params()) if len(set(panda_serials)) != len(panda_serials): raise ValueError('Duplicate Panda hardware serial') selected_firmware = {serial: flash_panda(serial, choice) for serial in panda_serials} diff --git a/panda/python/legacy_spi.py b/panda/python/legacy_spi.py new file mode 100644 index 0000000000..f40922d49e --- /dev/null +++ b/panda/python/legacy_spi.py @@ -0,0 +1,96 @@ +import struct +import time +from functools import reduce +from operator import xor +from pathlib import Path + +from panda import McuType, Panda +from panda.python.spi import PandaSpiException, SpiDevice + + +FLASHER_MAGIC = b"\xde\xad\xd0\x0d" +MAX_RESPONSE = 64 + + +class LegacySpiFlasher: + """Use the previous SPI framing only to identify and migrate an internal TRES Panda.""" + + def __init__(self, spi): + self.spi = spi + + @staticmethod + def _frame(data): + return data + bytes([reduce(xor, data, 0xab)]) + + def _wait_ack(self, expected, fill, length): + deadline = time.monotonic() + 0.25 + while time.monotonic() < deadline: + reply = self.spi.xfer(bytes([fill]) * length) + if reply[0] == expected: + return reply + if reply[0] == 0x1f: + raise PandaSpiException("Legacy SPI request was rejected") + raise PandaSpiException(f"Legacy SPI acknowledgment {expected:#x} timed out") + + def _exchange(self, endpoint, payload, expect_disconnect=False): + header = struct.pack(" MAX_RESPONSE or reduce(xor, reply[:size + 4], 0xab): + raise PandaSpiException("Legacy SPI response length or checksum invalid") + return reply[3:size + 3] + + def controlRead(self, request_type, request, value, index, length): + return self._exchange(0, struct.pack(" str | None: + """Return the migrated TRES serial, or None if the previous SPI framing is absent.""" + device = SpiDevice() + with device.acquire() as spi: + flasher = LegacySpiFlasher(spi) + try: + if not flasher.is_tres(): + return None + except PandaSpiException: + return None + serial = flasher.controlRead(Panda.REQUEST_IN, 0xc3, 0, 0, 12) + if len(serial) != 12 or (expected_serial is not None and serial.hex() != expected_serial): + raise PandaSpiException("Legacy TRES identity could not be verified") + code = Path(filename).read_bytes() + size = int.from_bytes(code[:4], "little") + if size <= 8 or size + 128 != len(code) or len(code) % 4 or len(code) >= 6 * 0x20000: + raise ValueError("Invalid legacy migration image layout") + if not Panda.flasher_present(flasher): + flasher.controlWrite(Panda.REQUEST_IN, 0xd1, 1, 0, b"", expect_disconnect=True) + time.sleep(0.5) + if not flasher.is_tres() or not Panda.flasher_present(flasher): + raise PandaSpiException("Legacy TRES bootstub could not be verified") + if flasher.controlRead(Panda.REQUEST_IN, 0xc3, 0, 0, 12) != serial: + raise PandaSpiException("Legacy TRES identity changed before flashing") + Panda.flash_static(flasher, code, McuType.H7) + return serial.hex() diff --git a/prebuilt.json b/prebuilt.json index c7c600b8f7..5b6b13dcc8 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "db1d5a25c408fec282b882fa32e1172b9e68517d21585fc7a2dea77954f91e2f", "schema_version": 1} +{"protected_inventory_sha256": "b231c85afbb4eb1b7b47390b71499697f7f4f46f8ad758dfb64ed1f09d4a71f0", "schema_version": 1}