Skip to content

Commit 8897ff8

Browse files
Honza0297radimkarnis
authored andcommitted
fix(detect_chip): Select correct loader before further operations to 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 espressif#1025
1 parent 2fedfd1 commit 8897ff8

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

esptool/cmds.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ def detect_chip(
9797
if detect_port.serial_port.startswith("rfc2217:"):
9898
detect_port.USES_RFC2217 = True
9999
detect_port.connect(connect_mode, connect_attempts, detecting=True)
100+
101+
def check_if_stub(instance):
102+
print(f" {instance.CHIP_NAME}", end="")
103+
if detect_port.sync_stub_detected:
104+
instance = instance.STUB_CLASS(instance)
105+
instance.sync_stub_detected = True
106+
return instance
107+
100108
try:
101109
print("Detecting chip type...", end="")
102110
chip_id = detect_port.get_chip_id()
@@ -112,6 +120,7 @@ def detect_chip(
112120
) # Dummy read to check Secure Download mode
113121
except UnsupportedCommandError:
114122
inst.secure_download_mode = True
123+
inst = check_if_stub(inst)
115124
inst._post_connect()
116125
break
117126
else:
@@ -137,6 +146,7 @@ def detect_chip(
137146
for cls in ROM_LIST:
138147
if chip_magic_value in cls.CHIP_DETECT_MAGIC_VALUE:
139148
inst = cls(detect_port._port, baud, trace_enabled=trace_enabled)
149+
inst = check_if_stub(inst)
140150
inst._post_connect()
141151
inst.check_chip_id()
142152
break
@@ -148,14 +158,9 @@ def detect_chip(
148158
"Probably this means Secure Download Mode is enabled, "
149159
"autodetection will not work. Need to manually specify the chip."
150160
)
151-
finally:
152-
if inst is not None:
153-
print(" %s" % inst.CHIP_NAME, end="")
154-
if detect_port.sync_stub_detected:
155-
inst = inst.STUB_CLASS(inst)
156-
inst.sync_stub_detected = True
157-
print("") # end line
158-
return inst
161+
if inst is not None:
162+
return inst
163+
159164
raise FatalError(
160165
f"{err_msg} Failed to autodetect chip type."
161166
"\nProbably it is unsupported by this version of esptool."

0 commit comments

Comments
 (0)