Skip to content

Commit

Permalink
fix: Erase non-aligned bytes with --no-stub
Browse files Browse the repository at this point in the history
This enables writing to non-aligned address by way of
erasing the bytes before it to the closest aligned address.
  • Loading branch information
Dzarda7 authored and radimkarnis committed Jul 15, 2024
1 parent d83dd3b commit c984aa9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
19 changes: 14 additions & 5 deletions esptool/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,15 +560,24 @@ def write_flash(esp, args):
print("Will flash %s uncompressed" % argfile.name)
compress = False

if args.no_stub:
print("Erasing flash...")
image = pad_to(
argfile.read(), esp.FLASH_ENCRYPTED_WRITE_ALIGN if encrypted else 4
)
image = argfile.read()

if len(image) == 0:
print("WARNING: File %s is empty" % argfile.name)
continue

image = pad_to(image, esp.FLASH_ENCRYPTED_WRITE_ALIGN if encrypted else 4)

if args.no_stub:
print("Erasing flash...")

# It is not possible to write to not aligned addresses without stub,
# so there are added 0xFF (erase) bytes at the beginning of the image
# to align it.
bytes_over = address % esp.FLASH_SECTOR_SIZE
address -= bytes_over
image = b"\xFF" * bytes_over + image

if not esp.secure_download_mode and not esp.get_secure_boot_enabled():
image = _update_image_flash_params(esp, address, args, image)
else:
Expand Down
9 changes: 9 additions & 0 deletions test/test_esptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,15 @@ def test_erase_before_write(self):
assert "Chip erase completed successfully" in output
assert "Hash of data verified" in output

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


@pytest.mark.skipif(
arg_chip in ["esp8266", "esp32"],
Expand Down

0 comments on commit c984aa9

Please sign in to comment.