Skip to content

Commit c984aa9

Browse files
Dzarda7radimkarnis
authored andcommitted
fix: Erase non-aligned bytes with --no-stub
This enables writing to non-aligned address by way of erasing the bytes before it to the closest aligned address.
1 parent d83dd3b commit c984aa9

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

esptool/cmds.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,15 +560,24 @@ def write_flash(esp, args):
560560
print("Will flash %s uncompressed" % argfile.name)
561561
compress = False
562562

563-
if args.no_stub:
564-
print("Erasing flash...")
565-
image = pad_to(
566-
argfile.read(), esp.FLASH_ENCRYPTED_WRITE_ALIGN if encrypted else 4
567-
)
563+
image = argfile.read()
564+
568565
if len(image) == 0:
569566
print("WARNING: File %s is empty" % argfile.name)
570567
continue
571568

569+
image = pad_to(image, esp.FLASH_ENCRYPTED_WRITE_ALIGN if encrypted else 4)
570+
571+
if args.no_stub:
572+
print("Erasing flash...")
573+
574+
# It is not possible to write to not aligned addresses without stub,
575+
# so there are added 0xFF (erase) bytes at the beginning of the image
576+
# to align it.
577+
bytes_over = address % esp.FLASH_SECTOR_SIZE
578+
address -= bytes_over
579+
image = b"\xFF" * bytes_over + image
580+
572581
if not esp.secure_download_mode and not esp.get_secure_boot_enabled():
573582
image = _update_image_flash_params(esp, address, args, image)
574583
else:

test/test_esptool.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,15 @@ def test_erase_before_write(self):
667667
assert "Chip erase completed successfully" in output
668668
assert "Hash of data verified" in output
669669

670+
@pytest.mark.quick_test
671+
def test_flash_not_aligned_nostub(self):
672+
output = self.run_esptool("--no-stub write_flash 0x1 images/one_kb.bin")
673+
assert (
674+
"WARNING: Flash address 0x00000001 is not aligned to a 0x1000 byte flash sector. 0x1 bytes before this address will be erased."
675+
in output
676+
)
677+
assert "Hard resetting via RTS pin..." in output
678+
670679

671680
@pytest.mark.skipif(
672681
arg_chip in ["esp8266", "esp32"],

0 commit comments

Comments
 (0)