Skip to content

Commit c7d723f

Browse files
danieldegrassede-nordic
authored andcommitted
[nrf fromtree] imgtool: support producing images in test mode
Add --test flag, which allows users to append a trailer that marks the image as ready for a test swap. This can be used for cases where the user wants to load an image to flash that MCUBoot will boot in test mode after system reset. Signed-off-by: Daniel DeGrasse <ddegrasse@tenstorrent.com> (cherry picked from commit 48b0f6d) Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
1 parent b709feb commit c7d723f

2 files changed

Lines changed: 17 additions & 9 deletions

File tree

scripts/imgtool/image.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ def __repr__(self):
349349
class Image:
350350

351351
def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
352-
pad_header=False, pad=False, confirm=False, align=1,
353-
slot_size=0, max_sectors=DEFAULT_MAX_SECTORS,
352+
pad_header=False, pad=False, confirm=False, test=False,
353+
align=1, slot_size=0, max_sectors=DEFAULT_MAX_SECTORS,
354354
overwrite_only=False, endian="little", load_addr=0,
355355
rom_fixed=None, erased_val=None, save_enctlv=False,
356356
security_counter=None, max_align=None,
@@ -368,6 +368,7 @@ def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
368368
self.pad_header = pad_header
369369
self.pad = pad
370370
self.confirm = confirm
371+
self.test = test
371372
self.align = align
372373
self.slot_size = slot_size
373374
self.max_sectors = max_sectors
@@ -506,12 +507,14 @@ def save(self, path, hex_addr=None):
506507
self.save_enctlv,
507508
self.enctlv_len)
508509
trailer_addr = (self.base_addr + self.slot_size) - trailer_size
509-
if self.confirm and not self.overwrite_only:
510+
if (self.test or self.confirm) and not self.overwrite_only:
510511
magic_align_size = align_up(len(self.boot_magic),
511512
self.max_align)
512513
image_ok_idx = -(magic_align_size + self.max_align)
514+
# If test is set, we leave image_ok at the erased value
513515
flag = bytearray([self.erased_val] * self.max_align)
514-
flag[0] = 0x01 # image_ok = 0x01
516+
if self.confirm:
517+
flag[0] = 0x01 # image_ok = 0x01
515518
h.puts(trailer_addr + trailer_size + image_ok_idx,
516519
bytes(flag))
517520
h.puts(trailer_addr + (trailer_size - len(self.boot_magic)),
@@ -950,11 +953,13 @@ def pad_to(self, size):
950953
pbytes = bytearray([self.erased_val] * padding)
951954
pbytes += bytearray([self.erased_val] * (tsize - len(self.boot_magic)))
952955
pbytes += self.boot_magic
953-
if self.confirm and not self.overwrite_only:
956+
if (self.test or self.confirm) and not self.overwrite_only:
954957
magic_size = 16
955958
magic_align_size = align_up(magic_size, self.max_align)
956959
image_ok_idx = -(magic_align_size + self.max_align)
957-
pbytes[image_ok_idx] = 0x01 # image_ok = 0x01
960+
# If test is set, set leave image_ok at the erased value
961+
if self.confirm:
962+
pbytes[image_ok_idx] = 0x01 # image_ok = 0x01
958963
self.payload += pbytes
959964

960965
@staticmethod

scripts/imgtool/main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ def convert(self, value, param, ctx):
388388
@click.option('--confirm', default=False, is_flag=True,
389389
help='When padding the image, mark it as confirmed (implies '
390390
'--pad)')
391+
@click.option('--test', default=False, is_flag=True,
392+
help='When padding the image, mark it for a test swap (implies '
393+
'--pad)')
391394
@click.option('--pad', default=False, is_flag=True,
392395
help='Pad image to --slot-size bytes, adding trailer magic')
393396
@click.option('-S', '--slot-size', type=BasedIntParamType(), required=True,
@@ -464,21 +467,21 @@ def convert(self, value, param, ctx):
464467
@click.option('--compression-lzma-preset', type=int, default=9,
465468
help='LZMA - compression level preset', show_default=True)
466469
def sign(key, public_key_format, align, version, pad_sig, header_size,
467-
pad_header, slot_size, pad, confirm, max_sectors, overwrite_only,
470+
pad_header, slot_size, pad, confirm, test, max_sectors, overwrite_only,
468471
endian, encrypt_keylen, encrypt, compression, infile, outfile,
469472
dependencies, load_addr, hex_addr, erased_val, save_enctlv,
470473
security_counter, boot_record, custom_tlv, custom_tlv_file, rom_fixed, max_align,
471474
clear, fix_sig, fix_sig_pubkey, sig_out, user_sha, hmac_sha, is_pure,
472475
vector_to_sign, non_bootable, vid, cid, edt_config, manifest,
473476
compression_lzma_dictsize, compression_lzma_pb, compression_lzma_lc, compression_lzma_lp, compression_lzma_preset):
474477

475-
if confirm:
478+
if confirm or test:
476479
# Confirmed but non-padded images don't make much sense, because
477480
# otherwise there's no trailer area for writing the confirmed status.
478481
pad = True
479482
img = image.Image(version=decode_version(version), header_size=header_size,
480483
pad_header=pad_header, pad=pad, confirm=confirm,
481-
align=int(align), slot_size=slot_size,
484+
test=test, align=int(align), slot_size=slot_size,
482485
max_sectors=max_sectors, overwrite_only=overwrite_only,
483486
endian=endian, load_addr=load_addr, rom_fixed=rom_fixed,
484487
erased_val=erased_val, save_enctlv=save_enctlv,

0 commit comments

Comments
 (0)