Skip to content

Commit 48b0f6d

Browse files
danieldegrassenordicjm
authored andcommitted
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 2b9f2a2 commit 48b0f6d

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
@@ -275,8 +275,8 @@ def parse_uuid(namespace, value):
275275
class Image:
276276

277277
def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
278-
pad_header=False, pad=False, confirm=False, align=1,
279-
slot_size=0, max_sectors=DEFAULT_MAX_SECTORS,
278+
pad_header=False, pad=False, confirm=False, test=False,
279+
align=1, slot_size=0, max_sectors=DEFAULT_MAX_SECTORS,
280280
overwrite_only=False, endian="little", load_addr=0,
281281
rom_fixed=None, erased_val=None, save_enctlv=False,
282282
security_counter=None, max_align=None,
@@ -293,6 +293,7 @@ def __init__(self, version=None, header_size=IMAGE_HEADER_SIZE,
293293
self.pad_header = pad_header
294294
self.pad = pad
295295
self.confirm = confirm
296+
self.test = test
296297
self.align = align
297298
self.slot_size = slot_size
298299
self.max_sectors = max_sectors
@@ -423,12 +424,14 @@ def save(self, path, hex_addr=None):
423424
self.save_enctlv,
424425
self.enctlv_len)
425426
trailer_addr = (self.base_addr + self.slot_size) - trailer_size
426-
if self.confirm and not self.overwrite_only:
427+
if (self.test or self.confirm) and not self.overwrite_only:
427428
magic_align_size = align_up(len(self.boot_magic),
428429
self.max_align)
429430
image_ok_idx = -(magic_align_size + self.max_align)
431+
# If test is set, we leave image_ok at the erased value
430432
flag = bytearray([self.erased_val] * self.max_align)
431-
flag[0] = 0x01 # image_ok = 0x01
433+
if self.confirm:
434+
flag[0] = 0x01 # image_ok = 0x01
432435
h.puts(trailer_addr + trailer_size + image_ok_idx,
433436
bytes(flag))
434437
h.puts(trailer_addr + (trailer_size - len(self.boot_magic)),
@@ -865,11 +868,13 @@ def pad_to(self, size):
865868
pbytes = bytearray([self.erased_val] * padding)
866869
pbytes += bytearray([self.erased_val] * (tsize - len(self.boot_magic)))
867870
pbytes += self.boot_magic
868-
if self.confirm and not self.overwrite_only:
871+
if (self.test or self.confirm) and not self.overwrite_only:
869872
magic_size = 16
870873
magic_align_size = align_up(magic_size, self.max_align)
871874
image_ok_idx = -(magic_align_size + self.max_align)
872-
pbytes[image_ok_idx] = 0x01 # image_ok = 0x01
875+
# If test is set, set leave image_ok at the erased value
876+
if self.confirm:
877+
pbytes[image_ok_idx] = 0x01 # image_ok = 0x01
873878
self.payload += pbytes
874879

875880
@staticmethod

scripts/imgtool/main.py

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

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

0 commit comments

Comments
 (0)