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.
This commit is contained in:
Jaroslav Burian
2024-07-10 16:18:49 +02:00
committed by Radim Karniš
parent d83dd3be6e
commit c984aa905d
2 changed files with 23 additions and 5 deletions

View File

@@ -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:

View File

@@ -667,6 +667,15 @@ class TestFlashing(EsptoolTestCase):
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"],