imgtool: add erased-val cli option

Add `-R`/`--erased-val` CLI option when creating images, which accepts
a value to be used for padding. This is useful when creating images
that run in devices that have flash erased to some value different
from 0xff (like STM32L0x and STM32L1x that have 0s).

Signed-off-by: Fabio Utzig <utzig@apache.org>
This commit is contained in:
Fabio Utzig 2019-10-17 11:11:46 -03:00 committed by Fabio Utzig
parent f5c5dd0d90
commit 9117fded47
2 changed files with 11 additions and 6 deletions

View File

@ -113,7 +113,7 @@ class Image():
def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
pad_header=False, pad=False, align=1, slot_size=0,
max_sectors=DEFAULT_MAX_SECTORS, overwrite_only=False,
endian="little", load_addr=0):
endian="little", load_addr=0, erased_val=0xff):
self.version = version or versmod.decode_version("0")
self.header_size = header_size
self.pad_header = pad_header
@ -125,6 +125,7 @@ class Image():
self.endian = endian
self.base_addr = None
self.load_addr = 0 if load_addr is None else load_addr
self.erased_val = 0xff if erased_val is None else int(erased_val)
self.payload = []
self.enckey = None
@ -160,7 +161,8 @@ class Image():
if self.base_addr:
# Adjust base_addr for new header
self.base_addr -= self.header_size
self.payload = (b'\000' * self.header_size) + self.payload
self.payload = bytes([self.erased_val] * self.header_size) + \
self.payload
self.check()
@ -347,8 +349,8 @@ class Image():
tsize = self._trailer_size(self.align, self.max_sectors,
self.overwrite_only, self.enckey)
padding = size - (len(self.payload) + tsize)
pbytes = b'\xff' * padding
pbytes += b'\xff' * (tsize - len(boot_magic))
pbytes = bytes([self.erased_val] * padding)
pbytes += bytes([self.erased_val] * (tsize - len(boot_magic)))
pbytes += boot_magic
self.payload += pbytes

View File

@ -182,6 +182,9 @@ class BasedIntParamType(click.ParamType):
@click.argument('outfile')
@click.argument('infile')
@click.option('-R', '--erased-val', type=click.Choice(['0', '0xff']),
required=False,
help='The value that is read back from erased flash.')
@click.option('-x', '--hex-addr', type=BasedIntParamType(), required=False,
help='Adjust address in hex output file.')
@click.option('-L', '--load-addr', type=BasedIntParamType(), required=False,
@ -214,12 +217,12 @@ class BasedIntParamType(click.ParamType):
.hex extension, otherwise binary format is used''')
def sign(key, align, version, header_size, pad_header, slot_size, pad,
max_sectors, overwrite_only, endian, encrypt, infile, outfile,
dependencies, load_addr, hex_addr):
dependencies, load_addr, hex_addr, erased_val):
img = image.Image(version=decode_version(version), header_size=header_size,
pad_header=pad_header, pad=pad, align=int(align),
slot_size=slot_size, max_sectors=max_sectors,
overwrite_only=overwrite_only, endian=endian,
load_addr=load_addr)
load_addr=load_addr, erased_val=erased_val)
img.load(infile)
key = load_key(key) if key else None
enckey = load_key(encrypt) if encrypt else None