Skip to content

Commit 1693449

Browse files
committed
fix(write_flash): Verify if files will fit against the real flash size when possible
1 parent 28556fb commit 1693449

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

esptool/cmds.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,32 @@ def write_flash(esp, args):
478478
"Use --force to override the warning."
479479
)
480480

481-
# verify file sizes fit in flash
482-
flash_end = flash_size_bytes(
483-
detect_flash_size(esp) if args.flash_size == "keep" else args.flash_size
481+
set_flash_size = (
482+
flash_size_bytes(args.flash_size)
483+
if args.flash_size not in ["detect", "keep"]
484+
else None
484485
)
485-
if flash_end is not None: # Not in secure download mode
486+
if esp.secure_download_mode:
487+
flash_end = set_flash_size
488+
else: # Check against real flash chip size if not in SDM
489+
flash_end_str = detect_flash_size(esp)
490+
flash_end = flash_size_bytes(flash_end_str)
491+
if set_flash_size and set_flash_size > flash_end:
492+
print(
493+
f"WARNING: Set --flash_size {args.flash_size} "
494+
f"is larger than the available flash size of {flash_end_str}."
495+
)
496+
497+
# Verify file sizes fit in the set --flash_size, or real flash size if smaller
498+
flash_end = min(set_flash_size, flash_end) if set_flash_size else flash_end
499+
if flash_end is not None:
486500
for address, argfile in args.addr_filename:
487501
argfile.seek(0, os.SEEK_END)
488502
if address + argfile.tell() > flash_end:
489503
raise FatalError(
490-
"File %s (length %d) at offset %d "
491-
"will not fit in %d bytes of flash. "
492-
"Use --flash_size argument, or change flashing address."
493-
% (argfile.name, argfile.tell(), address, flash_end)
504+
f"File {argfile.name} (length {argfile.tell()}) at offset "
505+
f"{address} will not fit in {flash_end} bytes of flash. "
506+
"Change the --flash_size argument, or flashing address."
494507
)
495508
argfile.seek(0)
496509

@@ -955,9 +968,7 @@ def get_key_from_value(dict, val):
955968
ESP8266V2FirmwareImage.IMAGE_V2_MAGIC,
956969
]:
957970
raise FatalError(
958-
"This is not a valid image " "(invalid magic number: {:#x})".format(
959-
magic
960-
)
971+
f"This is not a valid image (invalid magic number: {magic:#x})"
961972
)
962973

963974
if args.chip == "auto":

esptool/loader.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,8 @@ def connect(
713713
mode = "no_reset" # not possible to toggle DTR/RTS over a TCP socket
714714
print(
715715
"Note: It's not possible to reset the chip over a TCP socket. "
716-
"Resetting to bootloader has been disabled."
716+
"Automatic resetting to bootloader has been disabled, "
717+
"reset the chip manually."
717718
)
718719

719720
print("Connecting...", end="")

0 commit comments

Comments
 (0)