Skip to content

Commit

Permalink
fix(detect_chip): Select correct loader before further operations to …
Browse files Browse the repository at this point in the history
…avoid silent failures

If there is a return statement in finally block, it will prevent
unhandled exceptions from try block and all exceptions from except
block to be propagated.

Also, checking if esptool communicates with stub is performed earlier.

Closes #1025
  • Loading branch information
Jan Beran authored and radimkarnis committed Nov 4, 2024
1 parent 2fedfd1 commit 8897ff8
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions esptool/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ def detect_chip(
if detect_port.serial_port.startswith("rfc2217:"):
detect_port.USES_RFC2217 = True
detect_port.connect(connect_mode, connect_attempts, detecting=True)

def check_if_stub(instance):
print(f" {instance.CHIP_NAME}", end="")
if detect_port.sync_stub_detected:
instance = instance.STUB_CLASS(instance)
instance.sync_stub_detected = True
return instance

try:
print("Detecting chip type...", end="")
chip_id = detect_port.get_chip_id()
Expand All @@ -112,6 +120,7 @@ def detect_chip(
) # Dummy read to check Secure Download mode
except UnsupportedCommandError:
inst.secure_download_mode = True
inst = check_if_stub(inst)
inst._post_connect()
break
else:
Expand All @@ -137,6 +146,7 @@ def detect_chip(
for cls in ROM_LIST:
if chip_magic_value in cls.CHIP_DETECT_MAGIC_VALUE:
inst = cls(detect_port._port, baud, trace_enabled=trace_enabled)
inst = check_if_stub(inst)
inst._post_connect()
inst.check_chip_id()
break
Expand All @@ -148,14 +158,9 @@ def detect_chip(
"Probably this means Secure Download Mode is enabled, "
"autodetection will not work. Need to manually specify the chip."
)
finally:
if inst is not None:
print(" %s" % inst.CHIP_NAME, end="")
if detect_port.sync_stub_detected:
inst = inst.STUB_CLASS(inst)
inst.sync_stub_detected = True
print("") # end line
return inst
if inst is not None:
return inst

raise FatalError(
f"{err_msg} Failed to autodetect chip type."
"\nProbably it is unsupported by this version of esptool."
Expand Down

0 comments on commit 8897ff8

Please sign in to comment.