Skip to content

Commit d19413c

Browse files
committed
fix(write_flash): Make write flash mem independent
* Remove reading of magic register to avoid issues with different memory layouts.
1 parent 528f605 commit d19413c

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

esptool/cmds.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -905,10 +905,21 @@ def write_flash(
905905
raise # Reconnect limit reached
906906

907907
if esp.IS_STUB:
908+
# Get the "encrypted" flag for the last file flashed
909+
# Note: all_files list contains quadruplets like:
910+
# (address: int, filename: str | None, data: bytes, encrypted: bool)
911+
last_file_encrypted = all_files[-1][3]
912+
908913
# Stub only writes each block to flash after 'ack'ing the receive,
909-
# so do a final dummy operation which will not be 'ack'ed
914+
# so do a final operation which will not be 'ack'ed
910915
# until the last block has actually been written out to flash
911-
esp.read_reg(ESPLoader.CHIP_DETECT_MAGIC_REG_ADDR, timeout=timeout)
916+
if compress and not last_file_encrypted:
917+
esp.flash_defl_finish(reboot=False, timeout=timeout)
918+
else:
919+
esp.flash_finish(reboot=False, timeout=timeout)
920+
921+
# Skip sending flash_finish to ROM loader here,
922+
# as it causes the loader to exit and run user code
912923

913924
t = time.time() - t
914925
speed_msg = ""
@@ -948,22 +959,6 @@ def write_flash(
948959
"Cannot verify written data if encrypted or in secure download mode."
949960
)
950961

951-
if esp.IS_STUB:
952-
# skip sending flash_finish to ROM loader here,
953-
# as it causes the loader to exit and run user code
954-
esp.flash_begin(0, 0)
955-
956-
# Get the "encrypted" flag for the last file flashed
957-
# Note: all_files list contains quadruplets like:
958-
# (address: int, filename: str | None, data: bytes, encrypted: bool)
959-
last_file_encrypted = all_files[-1][3]
960-
961-
# Check whether the last file flashed was compressed or not
962-
if compress and not last_file_encrypted:
963-
esp.flash_defl_finish(False)
964-
else:
965-
esp.flash_finish(False)
966-
967962

968963
def read_mac(esp: ESPLoader) -> None:
969964
"""

esptool/loader.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,11 +1066,16 @@ def flash_encrypt_block(self, data, seq, timeout=DEFAULT_TIMEOUT):
10661066
else:
10671067
raise
10681068

1069-
def flash_finish(self, reboot=False):
1069+
def flash_finish(self, reboot=False, timeout=DEFAULT_TIMEOUT):
10701070
"""Leave flash mode and run/reboot"""
10711071
pkt = struct.pack("<I", int(not reboot))
10721072
# stub sends a reply to this command
1073-
self.check_command("leave flash download mode", self.ESP_CMDS["FLASH_END"], pkt)
1073+
self.check_command(
1074+
"leave flash download mode",
1075+
self.ESP_CMDS["FLASH_END"],
1076+
pkt,
1077+
timeout=timeout,
1078+
)
10741079

10751080
def run(self, reboot=False):
10761081
"""Run application code in flash"""
@@ -1400,15 +1405,18 @@ def flash_defl_block(self, data, seq, timeout=DEFAULT_TIMEOUT):
14001405
raise
14011406

14021407
@stub_and_esp32_function_only
1403-
def flash_defl_finish(self, reboot=False):
1408+
def flash_defl_finish(self, reboot=False, timeout=DEFAULT_TIMEOUT):
14041409
"""Leave compressed flash mode and run/reboot"""
14051410
if not reboot and not self.IS_STUB:
14061411
# skip sending flash_finish to ROM loader, as this
14071412
# exits the bootloader. Stub doesn't do this.
14081413
return
14091414
pkt = struct.pack("<I", int(not reboot))
14101415
self.check_command(
1411-
"leave compressed flash mode", self.ESP_CMDS["FLASH_DEFL_END"], pkt
1416+
"leave compressed flash mode",
1417+
self.ESP_CMDS["FLASH_DEFL_END"],
1418+
pkt,
1419+
timeout=timeout,
14121420
)
14131421
self.in_bootloader = False
14141422

0 commit comments

Comments
 (0)