python: catch exceptions in DFU (#2084)

* try

* also this
This commit is contained in:
Maxime Desroches 2024-11-20 19:22:14 -08:00 committed by GitHub
parent 422e3e0c0f
commit 998a639360
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 6 deletions

View File

@ -100,11 +100,14 @@ class PandaDFU:
def st_serial_to_dfu_serial(st: str, mcu_type: McuType = McuType.F4): def st_serial_to_dfu_serial(st: str, mcu_type: McuType = McuType.F4):
if st is None or st == "none": if st is None or st == "none":
return None return None
try:
uid_base = struct.unpack("H" * 6, bytes.fromhex(st)) uid_base = struct.unpack("H" * 6, bytes.fromhex(st))
if mcu_type == McuType.H7: 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") 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: 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") 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: def get_mcu_type(self) -> McuType:
return self._mcu_type return self._mcu_type

View File

@ -402,7 +402,8 @@ class STBootloaderSPIHandle(BaseSTBootloaderHandle):
def get_chip_id(self) -> int: def get_chip_id(self) -> int:
r = self._cmd(0x02, read_bytes=3) 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]) return ((r[1] << 8) + r[2])
def go_cmd(self, address: int) -> None: def go_cmd(self, address: int) -> None: