From 998a63936087a3d90cd29df88375d786491a78f8 Mon Sep 17 00:00:00 2001 From: Maxime Desroches Date: Wed, 20 Nov 2024 19:22:14 -0800 Subject: [PATCH] python: catch exceptions in DFU (#2084) * try * also this --- python/dfu.py | 13 ++++++++----- python/spi.py | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/python/dfu.py b/python/dfu.py index 9beba45e5..4394d2502 100644 --- a/python/dfu.py +++ b/python/dfu.py @@ -100,11 +100,14 @@ class PandaDFU: def st_serial_to_dfu_serial(st: str, mcu_type: McuType = McuType.F4): if st is None or st == "none": return None - uid_base = struct.unpack("H" * 6, bytes.fromhex(st)) - if mcu_type == McuType.H7: - return binascii.hexlify(struct.pack("!HHH", uid_base[1] + uid_base[5], uid_base[0] + uid_base[4], uid_base[3])).upper().decode("utf-8") - else: - return binascii.hexlify(struct.pack("!HHH", uid_base[1] + uid_base[5], uid_base[0] + uid_base[4] + 0xA, uid_base[3])).upper().decode("utf-8") + try: + uid_base = struct.unpack("H" * 6, bytes.fromhex(st)) + if mcu_type == McuType.H7: + return binascii.hexlify(struct.pack("!HHH", uid_base[1] + uid_base[5], uid_base[0] + uid_base[4], uid_base[3])).upper().decode("utf-8") + else: + return binascii.hexlify(struct.pack("!HHH", uid_base[1] + uid_base[5], uid_base[0] + uid_base[4] + 0xA, uid_base[3])).upper().decode("utf-8") + except struct.error: + return None def get_mcu_type(self) -> McuType: return self._mcu_type diff --git a/python/spi.py b/python/spi.py index 5be1e56b6..2b086dc2b 100644 --- a/python/spi.py +++ b/python/spi.py @@ -402,7 +402,8 @@ class STBootloaderSPIHandle(BaseSTBootloaderHandle): def get_chip_id(self) -> int: r = self._cmd(0x02, read_bytes=3) - assert r[0] == 1 # response length - 1 + if r[0] != 1: # response length - 1 + raise PandaSpiException("incorrect response length") return ((r[1] << 8) + r[2]) def go_cmd(self, address: int) -> None: