blob: 247cb1042fc1ba6d17ef2d8ae3e64d79b50c94ad [file] [log] [blame]
Balint Matyi3bae8832020-06-22 15:34:12 +02001#! /usr/bin/env python3
2#
3# -----------------------------------------------------------------------------
Sherry Zhangae25f052021-05-13 14:49:48 +08004# Copyright (c) 2020-2021, Arm Limited. All rights reserved.
Balint Matyi3bae8832020-06-22 15:34:12 +02005#
6# SPDX-License-Identifier: BSD-3-Clause
7#
8# -----------------------------------------------------------------------------
9
10import re
11import os
12import sys
13import click
Raef Coles8efad882020-07-10 09:46:00 +010014
15# Add the cwd to the path so that if there is a version of imgtool in there then
16# it gets used over the system imgtool. Used so that imgtool from upstream
17# mcuboot is preferred over system imgtool
18cwd = os.getcwd()
19sys.path = [cwd] + sys.path
Balint Matyi3bae8832020-06-22 15:34:12 +020020import imgtool
21import imgtool.main
Raef Coles8efad882020-07-10 09:46:00 +010022
Balint Matyid9abb492020-06-22 08:35:52 +010023parser_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))
24sys.path.append(parser_path)
25import macro_parser
Balint Matyi3bae8832020-06-22 15:34:12 +020026
27sign_bin_size_re = re.compile(r"^\s*RE_SIGN_BIN_SIZE\s*=\s*(.*)")
28load_addr_re = re.compile(r"^\s*RE_IMAGE_LOAD_ADDRESS\s*=\s*(.*)")
Sherry Zhangae25f052021-05-13 14:49:48 +080029rom_fixed_re = re.compile(r"^\s*RE_IMAGE_ROM_FIXED\s*=\s*(.*)")
Balint Matyi3bae8832020-06-22 15:34:12 +020030
31#This works around Python 2 and Python 3 handling character encodings
32#differently. More information about this issue at
33#https://click.palletsprojects.com/en/5.x/python3
34os.environ['LC_ALL'] = 'C.UTF-8'
35os.environ['LANG'] = 'C.UTF-8'
36
37@click.argument('outfile')
38@click.argument('infile')
39@click.option('-R', '--erased-val', type=click.Choice(['0', '0xff']),
40 required=False, help='The value that is read back from erased '
41 'flash.')
42@click.option('-x', '--hex-addr', type=imgtool.main.BasedIntParamType(),
43 required=False, help='Adjust address in hex output file.')
44@click.option('--save-enctlv', default=False, is_flag=True,
45 help='When upgrading, save encrypted key TLVs instead of plain '
46 'keys. Enable when BOOT_SWAP_SAVE_ENCTLV config option '
47 'was set.')
48@click.option('-E', '--encrypt', metavar='filename',
49 help='Encrypt image using the provided public key')
50@click.option('-e', '--endian', type=click.Choice(['little', 'big']),
51 default='little', help="Select little or big endian")
52@click.option('--overwrite-only', default=False, is_flag=True,
53 help='Use overwrite-only instead of swap upgrades')
54@click.option('-M', '--max-sectors', type=int,
55 help='When padding allow for this amount of sectors (defaults '
56 'to 128)')
57@click.option('--confirm', default=False, is_flag=True,
58 help='When padding the image, mark it as confirmed')
59@click.option('--pad', default=False, is_flag=True,
60 help='Pad image to the size determined by --layout, adding '
61 'trailer magic')
62@click.option('-l', '--layout', help='The file containing the macros of the '
63 'slot sizes')
64@click.option('--pad-header', default=False, is_flag=True,
65 help='Adds --erased-val (defaults to 0xff) --header-size times '
66 'at the beginning of the image')
67@click.option('-H', '--header-size',
68 callback=imgtool.main.validate_header_size,
69 type=imgtool.main.BasedIntParamType(), required=True)
70@click.option('-d', '--dependencies', callback=imgtool.main.get_dependencies,
71 required=False, help='''Add dependence on another image, format:
72 "(<image_ID>,<image_version>), ... "''')
73@click.option('-s', '--security-counter',
74 callback=imgtool.main.validate_security_counter,
75 help='Specify the value of security counter. Use the `auto` '
76 'keyword to automatically generate it from the image version.')
77@click.option('-v', '--version', callback=imgtool.main.validate_version,
78 required=True)
79@click.option('--align', type=click.Choice(['1', '2', '4', '8']),
80 required=True)
81@click.option('--public-key-format', type=click.Choice(['hash', 'full']),
82 default='hash', help='In what format to add the public key to '
83 'the image manifest: full key or hash of the key.')
84@click.option('-k', '--key', metavar='filename')
85@click.command(help='''Create a signed or unsigned image\n
86 INFILE and OUTFILE are parsed as Intel HEX if the params have
87 .hex extension, otherwise binary format is used''')
88def wrap(key, align, version, header_size, pad_header, layout, pad, confirm,
89 max_sectors, overwrite_only, endian, encrypt, infile, outfile,
90 dependencies, hex_addr, erased_val, save_enctlv, public_key_format,
91 security_counter):
92
93 slot_size = macro_parser.evaluate_macro(layout, sign_bin_size_re, 0, 1)
94 load_addr = macro_parser.evaluate_macro(layout, load_addr_re, 0, 1)
Sherry Zhangae25f052021-05-13 14:49:48 +080095 rom_fixed = macro_parser.evaluate_macro(layout, rom_fixed_re, 0, 1)
Raef Coles8efad882020-07-10 09:46:00 +010096 if "_s" in layout:
Balint Matyi3bae8832020-06-22 15:34:12 +020097 boot_record = "SPE"
Raef Coles8efad882020-07-10 09:46:00 +010098 elif "_ns" in layout:
Balint Matyi3bae8832020-06-22 15:34:12 +020099 boot_record = "NSPE"
100 else:
101 boot_record = "NSPE_SPE"
102
103 img = imgtool.image.Image(version=imgtool.version.decode_version(version),
104 header_size=header_size, pad_header=pad_header,
105 pad=pad, confirm=confirm, align=int(align),
106 slot_size=slot_size, max_sectors=max_sectors,
107 overwrite_only=overwrite_only, endian=endian,
Sherry Zhangae25f052021-05-13 14:49:48 +0800108 load_addr=load_addr, rom_fixed=rom_fixed,
109 erased_val=erased_val,
Balint Matyi3bae8832020-06-22 15:34:12 +0200110 save_enctlv=save_enctlv,
111 security_counter=security_counter)
112
113 img.load(infile)
114 key = imgtool.main.load_key(key) if key else None
115 enckey = imgtool.main.load_key(encrypt) if encrypt else None
116 if enckey and key:
117 if (isinstance(key, imgtool.keys.RSA) and
118 not isinstance(enckey, imgtool.keys.RSAPublic)):
119 # FIXME
120 raise click.UsageError("Signing and encryption must use the same "
121 "type of key")
122
123 img.create(key, public_key_format, enckey, dependencies, boot_record)
124 img.save(outfile, hex_addr)
125
126
127if __name__ == '__main__':
128 wrap()