Skip to content

Commit bbdeb66

Browse files
committed
verify_flash: Make behaviour same as write_flash --verify
Specifically, when flashing offset 0 --flash_mode and --flash_size are applied via verify_flash, same as with write_flash. Closes #160
1 parent 22bba06 commit bbdeb66

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ You can verify an image in the flash by passing the `--verify` option to the `wr
116116

117117
Verification is not always necessary, the bootloader serial protocol includes a checksum and this is usually enough to guarantee accurate flashing.
118118

119-
NOTE: esptool.py may update the first 16 bytes (offset 0) of the ESP8266 flash when writing (see [Flash modes](#flash-modes)), to set the provided flash mode and flash size parameters. If this happens then the standalone `verify_flash` command may fail on these bytes (`write_flash --verify` accounts for this).
119+
NOTE: esptool.py may update the first 16 bytes (offset 0) of the ESP8266 flash when writing (see [Flash modes](#flash-modes)), to set the provided flash mode and flash size parameters. If running `verify_flash` for an image at offset 0, pass matching versions of any `--flash_mode` or `--flash_size` arguments that were used for `write_flash`.
120120

121121
### Manually assembling a firmware image
122122

esptool.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -879,24 +879,33 @@ def detect_flash_size(esp, args):
879879
print('Auto-detected Flash size:', args.flash_size)
880880

881881

882-
def write_flash(esp, args):
882+
def _get_flash_params(esp, args):
883+
""" Return binary flash parameters (bitstring length 2) for args """
883884
detect_flash_size(esp, args)
884885
flash_mode = {'qio':0, 'qout':1, 'dio':2, 'dout': 3}[args.flash_mode]
885886
flash_size_freq = {'4m':0x00, '2m':0x10, '8m':0x20, '16m':0x30, '32m':0x40, '16m-c1': 0x50, '32m-c1':0x60, '32m-c2':0x70}[args.flash_size]
886887
flash_size_freq += {'40m':0, '26m':1, '20m':2, '80m': 0xf}[args.flash_freq]
887-
flash_params = struct.pack(b'BB', flash_mode, flash_size_freq)
888+
return struct.pack(b'BB', flash_mode, flash_size_freq)
889+
890+
891+
def _update_image_flash_params(address, flash_params, image):
892+
""" Modify the flash mode & size bytes if this looks like an executable image """
893+
if address == 0 and (image[0] == b'\xe9' or image[0] == 0xE9): # python 2/3 compat
894+
print('Flash params set to 0x%04x' % struct.unpack(">H", flash_params))
895+
image = image[0:2] + flash_params + image[4:]
896+
return image
897+
888898

899+
def write_flash(esp, args):
900+
flash_params = _get_flash_params(esp, args)
889901
flasher = CesantaFlasher(esp, args.baud)
890902

891903
for address, argfile in args.addr_filename:
892904
image = argfile.read()
893905
argfile.seek(0) # rewind in case we need it again
894906
if address + len(image) > int(args.flash_size.split('m')[0]) * (1 << 17):
895907
print('WARNING: Unlikely to work as data goes beyond end of flash. Hint: Use --flash_size')
896-
# Fix sflash config data.
897-
if address == 0 and (image[0] == b'\xe9' or image[0] == 0xE9): # python 2/3 compat
898-
print('Flash params set to 0x%02x%02x' % (flash_mode, flash_size_freq))
899-
image = image[0:2] + flash_params + image[4:]
908+
image = _update_image_flash_params(address, flash_params, image)
900909
# Pad to sector size, which is the minimum unit of writing (erasing really).
901910
if len(image) % esp.ESP_FLASH_SECTOR != 0:
902911
image += b'\xff' * (esp.ESP_FLASH_SECTOR - (len(image) % esp.ESP_FLASH_SECTOR))
@@ -908,7 +917,7 @@ def write_flash(esp, args):
908917
print('Leaving...')
909918
if args.verify:
910919
print('Verifying just-written flash...')
911-
_verify_flash(flasher, args, flash_params)
920+
_verify_flash(esp, args, flasher)
912921
flasher.boot_fw()
913922

914923

@@ -1021,13 +1030,17 @@ def read_flash(esp, args):
10211030
open(args.filename, 'wb').write(data)
10221031

10231032

1024-
def _verify_flash(flasher, args, flash_params=None):
1033+
def _verify_flash(esp, args, flasher=None):
10251034
differences = False
1035+
flash_params = _get_flash_params(esp, args)
1036+
if flasher is None: # get flash params before launching flasher
1037+
flasher = CesantaFlasher(esp)
1038+
10261039
for address, argfile in args.addr_filename:
10271040
image = argfile.read()
10281041
argfile.seek(0) # rewind in case we need it again
1029-
if address == 0 and (image[0] == b'\xe9' or image[0] == 0xE9) and flash_params is not None:
1030-
image = image[0:2] + flash_params + image[4:]
1042+
1043+
image = _update_image_flash_params(address, flash_params, image)
10311044

10321045
image_size = len(image)
10331046
print('Verifying 0x%x (%d) bytes @ 0x%08x in flash against %s...' % (image_size, image_size, address, argfile.name))
@@ -1061,8 +1074,7 @@ def _verify_flash(flasher, args, flash_params=None):
10611074

10621075

10631076
def verify_flash(esp, args, flash_params=None):
1064-
flasher = CesantaFlasher(esp)
1065-
_verify_flash(flasher, args, flash_params)
1077+
_verify_flash(esp, args)
10661078

10671079

10681080
def version(args):
@@ -1139,7 +1151,7 @@ def add_spi_flash_subparsers(parent, auto_detect=False):
11391151
action=AddrFilenamePairAction)
11401152
add_spi_flash_subparsers(parser_write_flash, auto_detect=True)
11411153
parser_write_flash.add_argument('--no-progress', '-p', help='Suppress progress output', action="store_true")
1142-
parser_write_flash.add_argument('--verify', help='Verify just-written data (only necessary if very cautious, data is already CRCed', action='store_true')
1154+
parser_write_flash.add_argument('--verify', help='Verify just-written data on flash (recommended if concerned about flash integrity)', action='store_true')
11431155

11441156
subparsers.add_parser(
11451157
'run',
@@ -1193,6 +1205,7 @@ def add_spi_flash_subparsers(parent, auto_detect=False):
11931205
action=AddrFilenamePairAction)
11941206
parser_verify_flash.add_argument('--diff', '-d', help='Show differences',
11951207
choices=['no', 'yes'], default='no')
1208+
add_spi_flash_subparsers(parser_verify_flash, auto_detect=True)
11961209

11971210
subparsers.add_parser(
11981211
'erase_flash',

0 commit comments

Comments
 (0)