blob: 7e171052afb88fa2a200b14909ffa4bee9620040 [file] [log] [blame] [view]
David Brownd2fcc212017-09-11 14:47:48 -06001# Image tool
David Browne369fec2017-06-07 09:35:48 -06002
3The Python program `scripts/imgtool.py` can be used to perform the
4operations that are necessary to manage keys and sign images. Using
5this script should be preferred to the manual steps described in
6`doc/signed_images.md`.
7
8This program is written for Python3, and has several dependencies on
Carles Cufif2429012018-01-30 16:45:50 +01009Python libraries. These can be installed using 'pip3':
David Browne369fec2017-06-07 09:35:48 -060010
Carles Cufif2429012018-01-30 16:45:50 +010011 pip3 install --user -r scripts/requirements.txt
David Browne369fec2017-06-07 09:35:48 -060012
13## Managing keys
14
Fabio Utzig8101d1f2019-05-09 15:03:22 -030015This tool currently supports rsa-2048, rsa-3072, ecdsa-p256 and ed25519 keys.
16You can generate a keypair for one of these types using the 'keygen' command:
David Browne369fec2017-06-07 09:35:48 -060017
18 ./scripts/imgtool.py keygen -k filename.pem -t rsa-2048
19
Fabio Utzig8101d1f2019-05-09 15:03:22 -030020or use rsa-3072, ecdsa-p256, or ed25519 for the type. The key type used
21should match what mcuboot is configured to verify.
David Browne369fec2017-06-07 09:35:48 -060022
23This key file is what is used to sign images, this file should be
24protected, and not widely distributed.
25
David Brown31d29c82017-11-21 15:38:56 -070026You can add the `-p` argument to `keygen`, which will cause it to
27prompt for a password. You will need to enter this password in every
28time you use the private key.
29
David Browne369fec2017-06-07 09:35:48 -060030## Incorporating the public key into the code
31
32There is a development key distributed with mcuboot that can be used
33for testing. Since this private key is widely distributed, it should
34never be used for production. Once you have generated a production
35key, as described above, you should replace the public key in the
36bootloader with the generated one.
37
38For Zephyr, the keys live in the file `boot/zephyr/keys.c`. For
39mynewt, follow the instructions in `doc/signed_images.md` to generate
40the key file.
41
42 ./scripts/imgtool.py getpub -k filename.pem
43
44will extract the public key from the given private key file, and
45output it as a C data structure. You can replace or insert this code
46into the key file.
47
48## Signing images
49
David Vincze2d736ad2019-02-18 11:50:22 +010050Image signing takes an image in binary or Intel Hex format intended for the
51primary slot and adds a header and trailer that the bootloader is expecting:
David Browne369fec2017-06-07 09:35:48 -060052
Fabio Utzig5901fd52018-06-13 11:24:30 -070053 Usage: imgtool.py sign [OPTIONS] INFILE OUTFILE
54
55 Create a signed or unsigned image
56
57 Options:
58 -k, --key filename
59 --align [1|2|4|8] [required]
60 -v, --version TEXT [required]
David Vinczee32483f2019-06-13 10:46:24 +020061 -d, --dependencies TEXT
Fabio Utzig5901fd52018-06-13 11:24:30 -070062 -H, --header-size INTEGER [required]
Fabio Utzigcdfa11a2018-10-01 09:45:54 -030063 --pad-header Add --header-size zeroed bytes at the beginning
Fabio Utzig5901fd52018-06-13 11:24:30 -070064 of the image
David Vincze2d736ad2019-02-18 11:50:22 +010065 -S, --slot-size INTEGER Size of the slot where the image will be
66 written [required]
Fabio Utzig5901fd52018-06-13 11:24:30 -070067 --pad Pad image to --slot-size bytes, adding trailer
68 magic
69 -M, --max-sectors INTEGER When padding allow for this amount of sectors
70 (defaults to 128)
71 --overwrite-only Use overwrite-only instead of swap upgrades
Fabio Utzigcdfa11a2018-10-01 09:45:54 -030072 -e, --endian [little|big] Select little or big endian
73 -E, --encrypt filename Encrypt image using the provided public key
Fabio Utzig5901fd52018-06-13 11:24:30 -070074 -h, --help Show this message and exit.
David Browne369fec2017-06-07 09:35:48 -060075
76The main arguments given are the key file generated above, a version
77field to place in the header (1.2.3 for example), the alignment of the
78flash device in question, and the header size.
79
80The header size depends on the operating system and the particular
81flash device. For Zephyr, it will be configured as part of the build,
Fabio Utzig5901fd52018-06-13 11:24:30 -070082and will be a small power of two. By default, the Zephyr build system will
83already prepended a zeroed header to the image. If another build system is
84in use that does not automatically add this zeroed header, `--pad-header` can
Mark Schulteb88b2c42018-07-17 07:47:03 -070085be passed and the `--header-size` will be added by imgtool. If `--pad-header`
86is used with an Intel Hex file, `--header-size` bytes will be subtracted from
87the load address (in Intel Hex terms, the Extended Linear Address record) to
88adjust for the new bytes prepended to the file. The load address of all data
89existing in the file should not change.
Fabio Utzig5901fd52018-06-13 11:24:30 -070090
91The `--slot-size` argument is required and used to check that the firmware
92does not overflow into the swap status area (metadata). If swap upgrades are
93not being used, `--overwrite-only` can be passed to avoid adding the swap
94status area size when calculating overflow.
David Browne369fec2017-06-07 09:35:48 -060095
Fabio Utzigcdfa11a2018-10-01 09:45:54 -030096The optional `--pad` argument will place a trailer on the image that
David Vincze2d736ad2019-02-18 11:50:22 +010097indicates that the image should be considered an upgrade. Writing this image
98in the secondary slot will then cause the bootloader to upgrade to it.
David Vinczee32483f2019-06-13 10:46:24 +020099
100A dependency can be specified in the following way:
101`-d "(image_id, image_version)"`. The `image_id` is the number of the image
102which the current image depends on. The `image_version` is the minimum version
103of that image to satisfy compliance. For example `-d "(1, 1.2.3+0)"` means this
104image depends on Image 1 which version has to be at least 1.2.3+0.