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