David Brown | d2fcc21 | 2017-09-11 14:47:48 -0600 | [diff] [blame] | 1 | # Image tool |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 2 | |
| 3 | The Python program `scripts/imgtool.py` can be used to perform the |
| 4 | operations that are necessary to manage keys and sign images. Using |
| 5 | this script should be preferred to the manual steps described in |
| 6 | `doc/signed_images.md`. |
| 7 | |
| 8 | This program is written for Python3, and has several dependencies on |
Carles Cufi | f242901 | 2018-01-30 16:45:50 +0100 | [diff] [blame] | 9 | Python libraries. These can be installed using 'pip3': |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 10 | |
Carles Cufi | f242901 | 2018-01-30 16:45:50 +0100 | [diff] [blame] | 11 | pip3 install --user -r scripts/requirements.txt |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 12 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 13 | ## [Managing keys](#managing-keys) |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 14 | |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 15 | This tool currently supports rsa-2048, rsa-3072, ecdsa-p256 and ed25519 keys. |
| 16 | You can generate a keypair for one of these types using the 'keygen' command: |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 17 | |
| 18 | ./scripts/imgtool.py keygen -k filename.pem -t rsa-2048 |
| 19 | |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 20 | or use rsa-3072, ecdsa-p256, or ed25519 for the type. The key type used |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 21 | should match what MCUboot is configured to verify. |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 22 | |
| 23 | This key file is what is used to sign images, this file should be |
| 24 | protected, and not widely distributed. |
| 25 | |
David Brown | 31d29c8 | 2017-11-21 15:38:56 -0700 | [diff] [blame] | 26 | You can add the `-p` argument to `keygen`, which will cause it to |
| 27 | prompt for a password. You will need to enter this password in every |
| 28 | time you use the private key. |
| 29 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 30 | ## [Incorporating the public key into the code](#incorporating-the-public-key-into-the-code) |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 31 | |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 32 | There is a development key distributed with MCUboot that can be used |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 33 | for testing. Since this private key is widely distributed, it should |
| 34 | never be used for production. Once you have generated a production |
| 35 | key, as described above, you should replace the public key in the |
| 36 | bootloader with the generated one. |
| 37 | |
| 38 | For Zephyr, the keys live in the file `boot/zephyr/keys.c`. For |
| 39 | mynewt, follow the instructions in `doc/signed_images.md` to generate |
| 40 | the key file. |
| 41 | |
| 42 | ./scripts/imgtool.py getpub -k filename.pem |
| 43 | |
| 44 | will extract the public key from the given private key file, and |
| 45 | output it as a C data structure. You can replace or insert this code |
David Vincze | 25459bf | 2020-04-21 17:11:20 +0200 | [diff] [blame] | 46 | into the key file. However, when the `MCUBOOT_HW_KEY` config option is |
| 47 | enabled, this last step is unnecessary and can be skipped. |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 48 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 49 | ## [Signing images](#signing-images) |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 50 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 51 | Image signing takes an image in binary or Intel Hex format intended for the |
| 52 | primary slot and adds a header and trailer that the bootloader is expecting: |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 53 | |
Fabio Utzig | 5901fd5 | 2018-06-13 11:24:30 -0700 | [diff] [blame] | 54 | Usage: imgtool.py sign [OPTIONS] INFILE OUTFILE |
| 55 | |
| 56 | Create a signed or unsigned image |
| 57 | |
David Vincze | 25459bf | 2020-04-21 17:11:20 +0200 | [diff] [blame] | 58 | INFILE and OUTFILE are parsed as Intel HEX if the params have .hex |
| 59 | extension, otherwise binary format is used |
| 60 | |
Fabio Utzig | 5901fd5 | 2018-06-13 11:24:30 -0700 | [diff] [blame] | 61 | Options: |
| 62 | -k, --key filename |
David Vincze | 25459bf | 2020-04-21 17:11:20 +0200 | [diff] [blame] | 63 | --public-key-format [hash|full] |
| 64 | --align [1|2|4|8] [required] |
| 65 | -v, --version TEXT [required] |
| 66 | -s, --security-counter TEXT Specify the value of security counter. Use |
| 67 | the `auto` keyword to automatically generate |
| 68 | it from the image version. |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 69 | -d, --dependencies TEXT |
David Vincze | 25459bf | 2020-04-21 17:11:20 +0200 | [diff] [blame] | 70 | --pad-sig Add 0-2 bytes of padding to ECDSA signature |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame] | 71 | (for MCUboot <1.5) |
David Vincze | 25459bf | 2020-04-21 17:11:20 +0200 | [diff] [blame] | 72 | -H, --header-size INTEGER [required] |
| 73 | --pad-header Add --header-size zeroed bytes at the |
| 74 | beginning of the image |
| 75 | -S, --slot-size INTEGER Size of the slot where the image will be |
| 76 | written [required] |
| 77 | --pad Pad image to --slot-size bytes, adding |
| 78 | trailer magic |
| 79 | --confirm When padding the image, mark it as confirmed |
| 80 | -M, --max-sectors INTEGER When padding allow for this amount of |
| 81 | sectors (defaults to 128) |
| 82 | --boot-record sw_type Create CBOR encoded boot record TLV. The |
| 83 | sw_type represents the role of the software |
| 84 | component (e.g. CoFM for coprocessor |
| 85 | firmware). [max. 12 characters] |
| 86 | --overwrite-only Use overwrite-only instead of swap upgrades |
| 87 | -e, --endian [little|big] Select little or big endian |
| 88 | -E, --encrypt filename Encrypt image using the provided public key |
| 89 | --save-enctlv When upgrading, save encrypted key TLVs |
| 90 | instead of plain keys. Enable when |
| 91 | BOOT_SWAP_SAVE_ENCTLV config option was set. |
| 92 | -L, --load-addr INTEGER Load address for image when it should run |
| 93 | from RAM. |
| 94 | -x, --hex-addr INTEGER Adjust address in hex output file. |
| 95 | -R, --erased-val [0|0xff] The value that is read back from erased |
| 96 | flash. |
| 97 | -h, --help Show this message and exit. |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 98 | |
| 99 | The main arguments given are the key file generated above, a version |
| 100 | field to place in the header (1.2.3 for example), the alignment of the |
| 101 | flash device in question, and the header size. |
| 102 | |
| 103 | The header size depends on the operating system and the particular |
| 104 | flash device. For Zephyr, it will be configured as part of the build, |
Fabio Utzig | 5901fd5 | 2018-06-13 11:24:30 -0700 | [diff] [blame] | 105 | and will be a small power of two. By default, the Zephyr build system will |
| 106 | already prepended a zeroed header to the image. If another build system is |
| 107 | in use that does not automatically add this zeroed header, `--pad-header` can |
Mark Schulte | b88b2c4 | 2018-07-17 07:47:03 -0700 | [diff] [blame] | 108 | be passed and the `--header-size` will be added by imgtool. If `--pad-header` |
| 109 | is used with an Intel Hex file, `--header-size` bytes will be subtracted from |
| 110 | the load address (in Intel Hex terms, the Extended Linear Address record) to |
| 111 | adjust for the new bytes prepended to the file. The load address of all data |
| 112 | existing in the file should not change. |
Fabio Utzig | 5901fd5 | 2018-06-13 11:24:30 -0700 | [diff] [blame] | 113 | |
| 114 | The `--slot-size` argument is required and used to check that the firmware |
| 115 | does not overflow into the swap status area (metadata). If swap upgrades are |
| 116 | not being used, `--overwrite-only` can be passed to avoid adding the swap |
| 117 | status area size when calculating overflow. |
David Brown | e369fec | 2017-06-07 09:35:48 -0600 | [diff] [blame] | 118 | |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 119 | The optional `--pad` argument will place a trailer on the image that |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 120 | indicates that the image should be considered an upgrade. Writing this image |
| 121 | in the secondary slot will then cause the bootloader to upgrade to it. |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 122 | |
| 123 | A dependency can be specified in the following way: |
| 124 | `-d "(image_id, image_version)"`. The `image_id` is the number of the image |
| 125 | which the current image depends on. The `image_version` is the minimum version |
| 126 | of that image to satisfy compliance. For example `-d "(1, 1.2.3+0)"` means this |
| 127 | image depends on Image 1 which version has to be at least 1.2.3+0. |
David Vincze | 25459bf | 2020-04-21 17:11:20 +0200 | [diff] [blame] | 128 | |
| 129 | The `--public-key-format` argument can be used to distinguish where the public |
| 130 | key is stored for image authentication. The `hash` option is used by default, in |
| 131 | which case only the hash of the public key is added to the TLV area (the full |
| 132 | public key is incorporated into the bootloader). When the `full` option is used |
| 133 | instead, the TLV area will contain the whole public key and thus the bootloader |
| 134 | can be independent from the key(s). For more information on the additional |
| 135 | requirements of this option, see the [design](design.md) document. |