Skip to content

Commit 3e5bae7

Browse files
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 <[email protected]>
1 parent 5e1be19 commit 3e5bae7

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

scripts/imgtool/image.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ def parse_uuid(namespace, value):
283283
class Image:
284284

285285
def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
286-
pad_header=False, pad=False, confirm=False, align=1,
287-
slot_size=0, max_sectors=DEFAULT_MAX_SECTORS,
286+
pad_header=False, pad=False, confirm=False, test=False,
287+
align=1, slot_size=0, max_sectors=DEFAULT_MAX_SECTORS,
288288
overwrite_only=False, endian="little", load_addr=0,
289289
rom_fixed=None, erased_val=None, save_enctlv=False,
290290
security_counter=None, max_align=None,
@@ -301,6 +301,7 @@ def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
301301
self.pad_header = pad_header
302302
self.pad = pad
303303
self.confirm = confirm
304+
self.test = test
304305
self.align = align
305306
self.slot_size = slot_size
306307
self.max_sectors = max_sectors
@@ -431,12 +432,14 @@ def save(self, path, hex_addr=None):
431432
self.save_enctlv,
432433
self.enctlv_len)
433434
trailer_addr = (self.base_addr + self.slot_size) - trailer_size
434-
if self.confirm and not self.overwrite_only:
435+
if (self.test or self.confirm) and not self.overwrite_only:
435436
magic_align_size = align_up(len(self.boot_magic),
436437
self.max_align)
437438
image_ok_idx = -(magic_align_size + self.max_align)
439+
# If test is set, we leave image_ok at the erased value
438440
flag = bytearray([self.erased_val] * self.max_align)
439-
flag[0] = 0x01 # image_ok = 0x01
441+
if self.confirm:
442+
flag[0] = 0x01 # image_ok = 0x01
440443
h.puts(trailer_addr + trailer_size + image_ok_idx,
441444
bytes(flag))
442445
h.puts(trailer_addr + (trailer_size - len(self.boot_magic)),
@@ -877,11 +880,13 @@ def pad_to(self, size):
877880
pbytes = bytearray([self.erased_val] * padding)
878881
pbytes += bytearray([self.erased_val] * (tsize - len(self.boot_magic)))
879882
pbytes += self.boot_magic
880-
if self.confirm and not self.overwrite_only:
883+
if (self.test or self.confirm) and not self.overwrite_only:
881884
magic_size = 16
882885
magic_align_size = align_up(magic_size, self.max_align)
883886
image_ok_idx = -(magic_align_size + self.max_align)
884-
pbytes[image_ok_idx] = 0x01 # image_ok = 0x01
887+
# If test is set, set leave image_ok at the erased value
888+
if self.confirm:
889+
pbytes[image_ok_idx] = 0x01 # image_ok = 0x01
885890
self.payload += pbytes
886891

887892
@staticmethod

scripts/imgtool/main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,9 @@ def convert(self, value, param, ctx):
416416
@click.option('--confirm', default=False, is_flag=True,
417417
help='When padding the image, mark it as confirmed (implies '
418418
'--pad)')
419+
@click.option('--test', default=False, is_flag=True,
420+
help='When padding the image, mark it for a test swap (implies '
421+
'--pad)')
419422
@click.option('--pad', default=False, is_flag=True,
420423
help='Pad image to --slot-size bytes, adding trailer magic')
421424
@click.option('-S', '--slot-size', type=BasedIntParamType(), required=True,
@@ -478,20 +481,20 @@ def convert(self, value, param, ctx):
478481
@click.option('--cid', default=None, required=False,
479482
help='Unique image class identifier, format: (<raw_uuid>|<image_class_name>)')
480483
def sign(key, public_key_format, align, version, pad_sig, header_size,
481-
pad_header, slot_size, pad, confirm, max_sectors, overwrite_only,
484+
pad_header, slot_size, pad, confirm, test, max_sectors, overwrite_only,
482485
endian, encrypt_keylen, encrypt, compression, infile, outfile,
483486
dependencies, load_addr, hex_addr, erased_val, save_enctlv,
484487
security_counter, boot_record, custom_tlv, rom_fixed, max_align,
485488
clear, fix_sig, fix_sig_pubkey, sig_out, user_sha, hmac_sha, is_pure,
486489
vector_to_sign, non_bootable, vid, cid):
487490

488-
if confirm:
491+
if confirm or test:
489492
# Confirmed but non-padded images don't make much sense, because
490493
# otherwise there's no trailer area for writing the confirmed status.
491494
pad = True
492495
img = image.Image(version=decode_version(version), header_size=header_size,
493496
pad_header=pad_header, pad=pad, confirm=confirm,
494-
align=int(align), slot_size=slot_size,
497+
test=test, align=int(align), slot_size=slot_size,
495498
max_sectors=max_sectors, overwrite_only=overwrite_only,
496499
endian=endian, load_addr=load_addr, rom_fixed=rom_fixed,
497500
erased_val=erased_val, save_enctlv=save_enctlv,

0 commit comments

Comments
 (0)