Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 1 | # [Building and using MCUboot with Espressif's chips](#building-and-using-mcuboot-with-espressifs-chips) |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 2 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 3 | The MCUBoot Espressif's port depends on HAL (Hardware Abstraction Layer) sources based on ESP-IDF |
| 4 | or 3rd party frameworks as such as Zephyr-RTOS (`zephyrproject-rtos/hal_espressif/`) or NuttX RTOS |
| 5 | (`espressif/esp-hal-3rdparty`). Building the MCUboot Espressif's port and its features is platform |
| 6 | dependent, therefore, the system environment including toolchains, must be set accordingly. A |
| 7 | standalone build version means that ESP-IDF and its toolchain are used as source. For 3rd parties |
| 8 | framework, HAL path and toolchain must be set. |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 9 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 10 | Documentation about the MCUboot bootloader design, operation and features can be found in the |
| 11 | [design document](design.md). |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 12 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 13 | ## [SoC support availability](#soc-support-availability) |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 14 | |
| 15 | The current port is available for use in the following SoCs within the OSes: |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 16 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 17 | | | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 | ESP32-C2 | ESP32-C6 | ESP32-H2 | |
| 18 | | :----: | :-----: | :-----: | :-----: | :-----: | :---------: | :-----: | :-----: | |
Almir Okato | c1efa00 | 2023-08-09 14:47:30 -0300 | [diff] [blame] | 19 | | Zephyr | Supported | Supported | Supported | Supported | In progress | In progress | In progress | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 20 | | NuttX | Supported | Supported | Supported | Supported | In progress | In progress | In progress | |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 21 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 22 | Notice that any customization in the memory layout from the OS application must be done aware of |
| 23 | the bootloader own memory layout to avoid overlapping. More information on the section |
| 24 | [Memory map organization for OS compatibility](#memory-map-organization-for-os-compatibility). |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 25 | |
| 26 | ## [Installing requirements and dependencies](#installing-requirements-and-dependencies) |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 27 | |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 28 | The following instructions considers a MCUboot Espressif port standalone build. |
| 29 | |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 30 | 1. Install additional packages required for development with MCUboot: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 31 | |
| 32 | ```bash |
| 33 | cd ~/mcuboot # or to your directory where MCUboot is cloned |
| 34 | ``` |
| 35 | |
| 36 | ```bash |
| 37 | pip3 install --user -r scripts/requirements.txt |
| 38 | ``` |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 39 | |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 40 | 2. Update the Mbed TLS submodule required by MCUboot: |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 41 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 42 | ```bash |
| 43 | git submodule update --init --recursive ext/mbedtls |
| 44 | ``` |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 45 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 46 | 3. If ESP-IDF is the chosen option for use as HAL layer and the system already have ESP-IDF |
| 47 | installed, ensure that the environment is set: |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 48 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 49 | ```bash |
| 50 | <IDF_PATH>/install.sh |
| 51 | ``` |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 52 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 53 | ```bash |
| 54 | . <IDF_PATH>/export.sh |
| 55 | ``` |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 56 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 57 | --- |
| 58 | ***Note*** |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 59 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 60 | *If desirable, instructions for ESP-IDF installation can be found |
| 61 | [here](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html#manual-installation)* |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 62 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 63 | --- |
| 64 | |
| 65 | --- |
| 66 | ***Note*** |
| 67 | |
| 68 | *The other HALs mentioned above like `hal_espressif` from Zephyr RTOS or `esp-hal-3rdparty` |
| 69 | from NuttX RTOS environments also can be used for the bootloader standalone build, however as |
| 70 | eventually code revision may differ from what is currently expected, it is recommended using |
| 71 | them only within their RTOS build system.* |
| 72 | |
| 73 | --- |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 74 | |
| 75 | 4. If ESP-IDF is not installed and will not be used, install `esptool`: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 76 | |
| 77 | ```bash |
| 78 | pip3 install esptool |
| 79 | ``` |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 80 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 81 | ## [Building the bootloader itself](#building-the-bootloader-itself) |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 82 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 83 | The MCUboot Espressif port bootloader is built using the toolchain and tools provided by Espressif. |
| 84 | Additional configuration related to MCUboot features and slot partitioning may be made using the |
| 85 | `port/<TARGET>/bootloader.conf` file or passing a custom config file using the |
| 86 | `-DMCUBOOT_CONFIG_FILE` argument on the first step below. |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 87 | |
Francesco Servidio | 2fe449d | 2021-10-21 12:38:36 +0200 | [diff] [blame] | 88 | --- |
| 89 | ***Note*** |
| 90 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 91 | *Replace `<TARGET>` with the target ESP32 family (like `esp32`, `esp32s2` and others).* |
Francesco Servidio | 2fe449d | 2021-10-21 12:38:36 +0200 | [diff] [blame] | 92 | |
| 93 | --- |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 94 | |
Almir Okato | fa173df | 2022-04-19 01:10:30 -0300 | [diff] [blame] | 95 | 1. Compile and generate the BIN: |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 96 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 97 | ```bash |
| 98 | cmake -DCMAKE_TOOLCHAIN_FILE=tools/toolchain-<TARGET>.cmake -DMCUBOOT_TARGET=<TARGET> -DESP_HAL_PATH=<ESP_HAL_PATH> -DMCUBOOT_FLASH_PORT=<PORT> -B build -GNinja |
| 99 | ``` |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 100 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 101 | ```bash |
| 102 | ninja -C build/ |
| 103 | ``` |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 104 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 105 | --- |
| 106 | ***Note*** |
| 107 | |
| 108 | *If using ESP-IDF as HAL layer source, `ESP_HAL_PATH` can be ommited.* |
| 109 | |
| 110 | --- |
Almir Okato | 879348b | 2023-05-09 22:31:36 -0300 | [diff] [blame] | 111 | |
Almir Okato | fa173df | 2022-04-19 01:10:30 -0300 | [diff] [blame] | 112 | 2. Flash MCUboot in your device: |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 113 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 114 | ```bash |
| 115 | ninja -C build/ flash |
| 116 | ``` |
Almir Okato | fa173df | 2022-04-19 01:10:30 -0300 | [diff] [blame] | 117 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 118 | If `MCUBOOT_FLASH_PORT` arg was not passed to `cmake`, the default `PORT` for flashing will be |
| 119 | `/dev/ttyUSB0`. |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 120 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 121 | Alternatively: |
Almir Okato | 428e2e7 | 2021-08-11 10:52:10 -0300 | [diff] [blame] | 122 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 123 | ```bash |
| 124 | esptool.py -p <PORT> -b <BAUD> --before default_reset --after no_reset --chip <TARGET> write_flash --flash_mode dio --flash_size <FLASH_SIZE> --flash_freq 40m <BOOTLOADER_FLASH_OFFSET> build/mcuboot_<TARGET>.bin |
| 125 | ``` |
Sylvio Alves | d523094 | 2022-01-20 21:35:53 -0300 | [diff] [blame] | 126 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 127 | --- |
| 128 | ***Note*** |
Sylvio Alves | d523094 | 2022-01-20 21:35:53 -0300 | [diff] [blame] | 129 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 130 | You may adjust the port `<PORT>` (like `/dev/ttyUSB0`) and baud rate `<BAUD>` (like `2000000`) |
| 131 | according to the connection with your board. You can also skip `<PORT>` and `<BAUD>` parameters |
| 132 | so that esptool tries to automatically detect it. |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 133 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 134 | *`<FLASH_SIZE>` can be found using the command below:* |
Sylvio Alves | d523094 | 2022-01-20 21:35:53 -0300 | [diff] [blame] | 135 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 136 | ```bash |
| 137 | esptool.py -p <PORT> -b <BAUD> flash_id |
| 138 | ``` |
| 139 | |
| 140 | The output contains device information and its flash size: |
| 141 | |
| 142 | ``` |
| 143 | Detected flash size: 4MB |
| 144 | ``` |
| 145 | |
| 146 | *`<BOOTLOADER_FLASH_OFFSET>` value must follow one of the addresses below:* |
| 147 | |
| 148 | | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 | ESP32-C2 | ESP32-C6 | ESP32-H2 | |
| 149 | | :-----: | :-----: | :-----: | :-----: | :-----: | :-----: | :-----: | |
| 150 | | 0x1000 | 0x1000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | 0x0000 | |
| 151 | |
| 152 | --- |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 153 | |
Almir Okato | fa173df | 2022-04-19 01:10:30 -0300 | [diff] [blame] | 154 | 3. Reset your device |
| 155 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 156 | ## [Signing and flashing an application](#signing-and-flashing-an-application) |
| 157 | |
| 158 | 1. Images can be regularly signed with the `scripts/imgtool.py` script: |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 159 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 160 | ```bash |
| 161 | imgtool.py sign --align 4 -v 0 -H 32 --pad-header -S <SLOT_SIZE> <BIN_IN> <SIGNED_BIN> |
| 162 | ``` |
Sylvio Alves | d523094 | 2022-01-20 21:35:53 -0300 | [diff] [blame] | 163 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 164 | --- |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 165 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 166 | ***Note*** |
Sylvio Alves | d523094 | 2022-01-20 21:35:53 -0300 | [diff] [blame] | 167 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 168 | `<SLOT_SIZE>` is the size of the slot to be used. |
| 169 | Default slot0 size is `0x100000`, but it can change as per application flash partitions. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 170 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 171 | For Zephyr images, `--pad-header` is not needed as it already has the padding for MCUboot |
| 172 | header. |
Sylvio Alves | d523094 | 2022-01-20 21:35:53 -0300 | [diff] [blame] | 173 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 174 | --- |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 175 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 176 | :warning: ***ATTENTION*** |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 177 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 178 | *This is the basic signing needed for adding MCUboot headers and trailers. |
| 179 | For signing with a crypto key and guarantee the authenticity of the image being booted, see the |
| 180 | section [MCUboot image signature verification](#mcuboot-image-signature-verification) below.* |
| 181 | |
| 182 | --- |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 183 | |
| 184 | 2. Flash the signed application: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 185 | |
| 186 | ```bash |
| 187 | esptool.py -p <PORT> -b <BAUD> --before default_reset --after hard_reset --chip <TARGET> write_flash --flash_mode dio --flash_size <FLASH_SIZE> --flash_freq 40m <SLOT_OFFSET> <SIGNED_BIN> |
| 188 | ``` |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 189 | |
Almir Okato | 84da51b | 2022-11-25 01:25:41 -0300 | [diff] [blame] | 190 | # [Downgrade prevention](#downgrade-prevention) |
| 191 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 192 | Downgrade prevention (avoid updating of images to an older version) can be enabled using the |
| 193 | following configuration: |
Almir Okato | 84da51b | 2022-11-25 01:25:41 -0300 | [diff] [blame] | 194 | |
| 195 | ``` |
| 196 | CONFIG_ESP_DOWNGRADE_PREVENTION=y |
| 197 | ``` |
| 198 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 199 | MCUboot will then verify and compare the new image version number with the current one before |
| 200 | perform an update swap. |
Almir Okato | 84da51b | 2022-11-25 01:25:41 -0300 | [diff] [blame] | 201 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 202 | Version number is added to the image when signing it with `imgtool` (`-v` parameter, e.g. |
| 203 | `-v 1.0.0`). |
Almir Okato | 84da51b | 2022-11-25 01:25:41 -0300 | [diff] [blame] | 204 | |
| 205 | ### [Downgrade prevention with security counter](#downgrade-prevention-with-security-counter) |
| 206 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 207 | It is also possible to rely on a security counter, also added to the image when signing with |
| 208 | `imgtool` (`-s` parameter), apart from version number. This allows image downgrade at some extent, |
| 209 | since any update must have greater or equal security counter value. Enable using the following |
| 210 | configuration: |
Almir Okato | 84da51b | 2022-11-25 01:25:41 -0300 | [diff] [blame] | 211 | |
| 212 | ``` |
| 213 | CONFIG_ESP_DOWNGRADE_PREVENTION_SECURITY_COUNTER=y |
| 214 | ``` |
| 215 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 216 | E.g.: if the current image was signed using `-s 1` parameter, an eventual update image must have |
| 217 | been signed using security counter `-s 1` or greater. |
Almir Okato | 84da51b | 2022-11-25 01:25:41 -0300 | [diff] [blame] | 218 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 219 | # [Security Chain on Espressif port](#security-chain-on-espressif-port) |
| 220 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 221 | [MCUboot encrypted images](encrypted_images.md) do not provide full code confidentiality when only |
| 222 | external storage is available (see [Threat model](encrypted_images.md#threat-model)) since by |
| 223 | MCUboot design the image in Primary Slot, from where the image is executed, is stored plaintext. |
| 224 | Espressif chips have off-chip flash memory, so to ensure a security chain along with MCUboot image |
| 225 | signature verification, the hardware-assisted Secure Boot and Flash Encryption were made available |
| 226 | on the MCUboot Espressif port. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 227 | |
| 228 | ## [MCUboot image signature verification](#mcuboot-image-signature-verification) |
| 229 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 230 | The image that MCUboot is booting can be signed with 4 types of keys: RSA-2048, RSA-3072, EC256 and |
| 231 | ED25519. In order to enable the feature, the **bootloader** must be compiled with the following |
| 232 | configurations: |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 233 | |
| 234 | --- |
| 235 | ***Note*** |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 236 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 237 | *It is strongly recommended to generate a new signing key using `imgtool` instead of use the |
| 238 | existent samples.* |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 239 | |
| 240 | --- |
| 241 | |
| 242 | #### For EC256 algorithm use |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 243 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 244 | ``` |
| 245 | CONFIG_ESP_SIGN_EC256=y |
| 246 | |
| 247 | # Use Tinycrypt lib for EC256 or ED25519 signing |
| 248 | CONFIG_ESP_USE_TINYCRYPT=y |
| 249 | |
| 250 | CONFIG_ESP_SIGN_KEY_FILE=<YOUR_SIGNING_KEY.pem> |
| 251 | ``` |
| 252 | |
| 253 | #### For ED25519 algorithm use |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 254 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 255 | ``` |
| 256 | CONFIG_ESP_SIGN_ED25519=y |
| 257 | |
| 258 | # Use Tinycrypt lib for EC256 or ED25519 signing |
| 259 | CONFIG_ESP_USE_TINYCRYPT=y |
| 260 | |
| 261 | CONFIG_ESP_SIGN_KEY_FILE=<YOUR_SIGNING_KEY.pem> |
| 262 | ``` |
| 263 | |
| 264 | #### For RSA (2048 or 3072) algorithm use |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 265 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 266 | ``` |
| 267 | CONFIG_ESP_SIGN_RSA=y |
| 268 | # RSA_LEN is 2048 or 3072 |
| 269 | CONFIG_ESP_SIGN_RSA_LEN=<RSA_LEN> |
| 270 | |
| 271 | # Use Mbed TLS lib for RSA image signing |
| 272 | CONFIG_ESP_USE_MBEDTLS=y |
| 273 | |
| 274 | CONFIG_ESP_SIGN_KEY_FILE=<YOUR_SIGNING_KEY.pem> |
| 275 | ``` |
| 276 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 277 | Notice that the public key will be embedded in the bootloader code, since the hardware key storage |
| 278 | is not supported by Espressif port. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 279 | |
| 280 | ### [Signing the image](#signing-the-image) |
| 281 | |
| 282 | Now you need to sign the **image binary**, use the `imgtool` with `-k` parameter: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 283 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 284 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 285 | imgtool.py sign -k <YOUR_SIGNING_KEY.pem> --pad --pad-sig --align 4 -v 0 -H 32 --pad-header -S 0x00100000 <BIN_IN> <BIN_OUT> |
| 286 | ``` |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 287 | |
| 288 | If signing a Zephyr image, the `--pad-header` is not needed, as it already have the padding for |
| 289 | MCUboot header. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 290 | |
| 291 | |
| 292 | ## [Secure Boot](#secure-boot) |
| 293 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 294 | The Secure Boot implementation is based on |
| 295 | [IDF's Secure Boot V2](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/security/secure-boot-v2.html), |
| 296 | is hardware-assisted and RSA based, and has the role for ensuring that only authorized code will be |
| 297 | executed on the device. This is done through bootloader signature checking by the ROM bootloader. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 298 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 299 | ***Note***: ROM bootloader is the First Stage Bootloader, while the Espressif MCUboot port is the |
| 300 | Second Stage Bootloader. |
| 301 | |
| 302 | ***Note***: Currently on MCUboot Espressif Port, the Secure Boot V2 for ESP32-C2 is not supported |
| 303 | yet. |
Almir Okato | 68a2980 | 2023-08-02 01:01:19 -0300 | [diff] [blame] | 304 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 305 | ### [Building bootloader with Secure Boot](#building-bootloader-with-secure-boot) |
| 306 | |
| 307 | In order to build the bootloader with the feature on, the following configurations must be enabled: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 308 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 309 | ``` |
| 310 | CONFIG_SECURE_BOOT=1 |
| 311 | CONFIG_SECURE_BOOT_V2_ENABLED=1 |
| 312 | CONFIG_SECURE_SIGNED_ON_BOOT=1 |
| 313 | CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME=1 |
| 314 | CONFIG_SECURE_BOOT_SUPPORTS_RSA=1 |
| 315 | ``` |
| 316 | |
| 317 | --- |
| 318 | :warning: ***ATTENTION*** |
| 319 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 320 | *On development phase is recommended add the following configuration in order to keep the debugging |
| 321 | enabled and also to avoid any unrecoverable/permanent state change:* |
| 322 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 323 | ``` |
| 324 | CONFIG_SECURE_BOOT_ALLOW_JTAG=1 |
| 325 | CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=1 |
| 326 | |
| 327 | # Options for enabling eFuse emulation in Flash |
| 328 | CONFIG_EFUSE_VIRTUAL=1 |
| 329 | CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH=1 |
| 330 | ``` |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 331 | --- |
| 332 | |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 333 | --- |
| 334 | :warning: ***ATTENTION*** |
| 335 | |
| 336 | *You can disable UART Download Mode by adding the following configuration:* |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 337 | |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 338 | ``` |
| 339 | CONFIG_SECURE_DISABLE_ROM_DL_MODE=1 |
| 340 | ``` |
| 341 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 342 | *This may be suitable for __production__ builds. __After disabling UART Download Mode you will not |
| 343 | be able to flash other images through UART.__* |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 344 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 345 | *Otherwise, you can switch the UART ROM Download Mode to the Secure Download Mode. It will limit |
| 346 | the use of Download Mode functions to simple flash read, write and erase operations.* |
| 347 | |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 348 | ``` |
| 349 | CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE=1 |
| 350 | ``` |
| 351 | |
| 352 | *Once the device makes its first full boot, these configurations cannot be reverted* |
| 353 | |
| 354 | --- |
| 355 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 356 | Once the **bootloader image** is built, the resulting binary file is required to be signed with |
| 357 | `espsecure.py` tool. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 358 | |
| 359 | First create a signing key: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 360 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 361 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 362 | espsecure.py generate_signing_key --version 2 <BOOTLOADER_SIGNING_KEY.pem> |
| 363 | ``` |
| 364 | |
| 365 | Then sign the bootloader image: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 366 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 367 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 368 | espsecure.py sign_data --version 2 --keyfile <BOOTLOADER_SIGNING_KEY.pem> -o <BOOTLOADER_BIN_OUT> <BOOTLOADER_BIN_IN> |
| 369 | ``` |
| 370 | |
| 371 | --- |
| 372 | :warning: ***ATTENTION*** |
| 373 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 374 | *Once the bootloader is flashed and the device resets, the **first boot will enable Secure Boot** |
| 375 | and the bootloader and key **no longer can be modified**. So **ENSURE** that both bootloader and |
| 376 | key are correct and you did not forget anything before flashing.* |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 377 | |
| 378 | --- |
| 379 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 380 | Flash the bootloader as following, with `--after no_reset` flag, so you can reset the device only |
| 381 | when assured: |
| 382 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 383 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 384 | esptool.py -p <PORT> -b 2000000 --after no_reset --chip <ESP_CHIP> write_flash --flash_mode dio --flash_size <FLASH_SIZE> --flash_freq 40m <BOOTLOADER_FLASH_OFFSET> <SIGNED_BOOTLOADER_BIN> |
| 385 | ``` |
| 386 | |
| 387 | ### [Secure Boot Process](#secure-boot-process) |
| 388 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 389 | Secure boot uses a signature block appended to the bootloader image in order to verify the |
| 390 | authenticity. The signature block contains the RSA-3072 signature of that image and the RSA-3072 |
| 391 | public key. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 392 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 393 | On its **first boot** the Secure Boot is not enabled on the device eFuses yet, neither the key nor |
| 394 | digests. So the first boot will have the following process: |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 395 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 396 | 1. On startup, since it is the first boot, the ROM bootloader will not verify the bootloader image |
| 397 | (the Secure Boot bit in the eFuse is disabled) yet, so it proceeds to execute it (our MCUboot |
| 398 | bootloader port). |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 399 | 2. Bootloader calculates the SHA-256 hash digest of the public key and writes the result to eFuse. |
| 400 | 3. Bootloader validates the application images and prepare the booting process (MCUboot phase). |
| 401 | 4. Bootloader burns eFuse to enable Secure Boot V2. |
| 402 | 5. Bootloader proceeds to load the Primary image. |
| 403 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 404 | After that the Secure Boot feature is permanently enabled and on every next boot the ROM bootloader |
| 405 | will verify the MCUboot bootloader image. The process of an usual boot: |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 406 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 407 | 1. On startup, the ROM bootloader checks the Secure Boot enable bit in the eFuse. If it is enabled, |
| 408 | the boot will proceed as following. |
| 409 | 2. ROM bootloader verifies the bootloader's signature block integrity (magic number and CRC). |
| 410 | Interrupt boot if it fails. |
| 411 | 3. ROM bootloader verifies the bootloader image, interrupt boot if any step fails: |
| 412 | 1. Compare the SHA-256 hash digest of the public key embedded in the bootloader’s signature |
| 413 | block with the digest saved in the eFuses. |
| 414 | 2. Generate the application image digest and match it with the image digest in the signature |
| 415 | block. |
| 416 | 3. Use the public key to verify the signature of the bootloader image, using RSA-PSS with the |
| 417 | image digest calculated from previous step for comparison. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 418 | 4. ROM bootloader executes the bootloader image. |
| 419 | 5. Bootloader does the usual verification (MCUboot phase). |
| 420 | 6. Proceeds to boot the Primary image. |
| 421 | |
| 422 | ## [Flash Encryption](#flash-encryption) |
| 423 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 424 | The Espressif Flash Encryption is hardware-assisted, transparent to the MCUboot process and is an |
| 425 | additional security measure beyond MCUboot existent features. |
| 426 | The Flash Encryption implementation is also based on |
| 427 | [IDF](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/security/flash-encryption.html) |
| 428 | and is intended for encrypting off-chip flash memory contents, so it is protected against physical |
| 429 | reading. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 430 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 431 | When enabling the Flash Encryption, the user can encrypt the content either using a **device |
| 432 | generated key** (remains unknown and unreadable) or a **host generated key** (owner is responsible |
| 433 | for keeping the key private and safe). After the flash encryption gets enabled through eFuse |
| 434 | burning on the device, all read and write operations are decrypted/encrypted in runtime. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 435 | |
| 436 | ### [Building bootloader with Flash Encryption](#building-bootloader-with-flash-encryption) |
| 437 | |
| 438 | In order to build the bootloader with the feature on, the following configurations must be enabled: |
| 439 | |
| 440 | For **release mode**: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 441 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 442 | ``` |
| 443 | CONFIG_SECURE_FLASH_ENC_ENABLED=1 |
| 444 | CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE=1 |
| 445 | ``` |
| 446 | |
| 447 | For **development mode**: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 448 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 449 | ``` |
| 450 | CONFIG_SECURE_FLASH_ENC_ENABLED=1 |
| 451 | CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT=1 |
| 452 | ``` |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 453 | |
| 454 | --- |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 455 | :warning: ***ATTENTION*** |
| 456 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 457 | *On development phase is strongly recommended adding the following configuration in order to keep |
| 458 | the debugging enabled and also to avoid any unrecoverable/permanent state change:* |
| 459 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 460 | ``` |
| 461 | CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC=1 |
| 462 | CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_DEC=1 |
| 463 | CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=1 |
| 464 | CONFIG_SECURE_BOOT_ALLOW_JTAG=1 |
| 465 | |
| 466 | # Options for enabling eFuse emulation in Flash |
| 467 | CONFIG_EFUSE_VIRTUAL=1 |
| 468 | CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH=1 |
| 469 | ``` |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 470 | --- |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 471 | |
| 472 | --- |
| 473 | :warning: ***ATTENTION*** |
| 474 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 475 | *Unless the recommended flags for __DEVELOPMENT MODE__ were enabled, the actions made by Flash |
| 476 | Encryption process are __PERMANENT__.* \ |
| 477 | *Once the bootloader is flashed and the device resets, the __first boot will enable Flash |
| 478 | Encryption, encrypt the flash content including bootloader and image slots, burn the eFuses that no |
| 479 | longer can be modified__ and if device generated the key __it will not be recoverable__.* \ |
| 480 | *When on __RELEASE MODE__, __ENSURE__ that the application with an update agent is flashed before |
| 481 | reset the device.* |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 482 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 483 | *In the same way as Secure Boot feature, you can disable UART Download Mode by adding the following |
| 484 | configuration:* |
| 485 | |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 486 | ``` |
| 487 | CONFIG_SECURE_DISABLE_ROM_DL_MODE=1 |
| 488 | ``` |
| 489 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 490 | *This may be suitable for __production__ builds. __After disabling UART Download Mode you will not |
| 491 | be able to flash other images through UART.__* |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 492 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 493 | *Otherwise, you can switch the UART Download Mode to the Secure Download Mode. It will limit the |
| 494 | use of Download Mode functions to simple flash read, write and erase operations.* |
| 495 | |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 496 | ``` |
| 497 | CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE=1 |
| 498 | ``` |
| 499 | |
| 500 | *These configurations cannot be reverted after the device's first boot* |
| 501 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 502 | --- |
| 503 | |
Sylvio Alves | d523094 | 2022-01-20 21:35:53 -0300 | [diff] [blame] | 504 | ### [Signing the image when working with Flash Encryption](#signing-the-image-when-working-with-flash-encryption) |
| 505 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 506 | When enabling flash encryption, it is required to signed the image using 32-byte alignment: |
| 507 | `--align 32 --max-align 32`. |
Sylvio Alves | d523094 | 2022-01-20 21:35:53 -0300 | [diff] [blame] | 508 | |
| 509 | Command example: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 510 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 511 | ```bash |
Sylvio Alves | d523094 | 2022-01-20 21:35:53 -0300 | [diff] [blame] | 512 | imgtool.py sign -k <YOUR_SIGNING_KEY.pem> --pad --pad-sig --align 32 --max-align 32 -v 0 -H 32 --pad-header -S <SLOT_SIZE> <BIN_IN> <BIN_OUT> |
| 513 | ``` |
| 514 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 515 | ### [Device generated key](#device-generated-key) |
| 516 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 517 | First ensure that the application image is able to perform encrypted read and write operations to |
| 518 | the SPI Flash. Flash the bootloader and application normally: |
| 519 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 520 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 521 | esptool.py -p <PORT> -b 2000000 --after no_reset --chip <ESP_CHIP> write_flash --flash_mode dio --flash_size <FLASH_SIZE> --flash_freq 40m <BOOTLOADER_FLASH_OFFSET> <BOOTLOADER_BIN> |
| 522 | ``` |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 523 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 524 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 525 | esptool.py -p <PORT> -b 2000000 --after no_reset --chip <ESP_CHIP> write_flash --flash_mode dio --flash_size <FLASH_SIZE> --flash_freq 40m <PRIMARY_SLOT_FLASH_OFFSET> <APPLICATION_BIN> |
| 526 | ``` |
| 527 | |
| 528 | On the **first boot**, the bootloader will: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 529 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 530 | 1. Generate Flash Encryption key and write to eFuse. |
| 531 | 2. Encrypt flash in-place including bootloader, image primary/secondary slot and scratch. |
| 532 | 3. Burn eFuse to enable Flash Encryption. |
| 533 | 4. Reset system to ensure Flash Encryption cache resets properly. |
| 534 | |
| 535 | ### [Host generated key](#host-generated-key) |
| 536 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 537 | First ensure that the application image is able to perform encrypted read and write operations to |
| 538 | the SPI Flash. Also ensure that the **UART ROM Download Mode is not disabled** - or that the |
| 539 | **Secure Download Mode is enabled**. Before flashing, generate the encryption key using |
| 540 | `espsecure.py` tool: |
| 541 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 542 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 543 | espsecure.py generate_flash_encryption_key <FLASH_ENCRYPTION_KEY.bin> |
| 544 | ``` |
| 545 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 546 | Burn the key into the device's eFuse (keep a copy on the host), this action can be done **only |
| 547 | once**: |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 548 | |
| 549 | --- |
| 550 | :warning: ***ATTENTION*** |
| 551 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 552 | *eFuse emulation in Flash configuration options do not have any effect, so if the key burning |
| 553 | command below is used, it will actually burn the physical eFuse.* |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 554 | |
| 555 | --- |
| 556 | |
| 557 | - ESP32 |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 558 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 559 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 560 | espefuse.py --port PORT burn_key flash_encryption <FLASH_ENCRYPTION_KEY.bin> |
| 561 | ``` |
| 562 | |
Sylvio Alves | d8eff81 | 2022-01-31 23:16:06 -0300 | [diff] [blame] | 563 | - ESP32S2, ESP32C3 and ESP32S3 |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 564 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 565 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 566 | espefuse.py --port PORT burn_key BLOCK <FLASH_ENCRYPTION_KEY.bin> <KEYPURPOSE> |
| 567 | ``` |
| 568 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 569 | `BLOCK` is a free keyblock between `BLOCK_KEY0` and `BLOCK_KEY5`. And `KEYPURPOSE` is either |
| 570 | `XTS_AES_128_KEY`, `XTS_AES_256_KEY_1`, `XTS_AES_256_KEY_2` (AES XTS 256 is available only in |
| 571 | ESP32S2). |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 572 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 573 | Now, similar as the Device generated key, the bootloader and application can be flashed plaintext. |
| 574 | The **first boot** will encrypt the flash content using the host key burned in the eFuse instead |
| 575 | of generate a new one. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 576 | |
| 577 | Flashing the bootloader and application: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 578 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 579 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 580 | esptool.py -p <PORT> -b 2000000 --after no_reset --chip <ESP_CHIP> write_flash --flash_mode dio --flash_size <FLASH_SIZE> --flash_freq 40m <BOOTLOADER_FLASH_OFFSET> <BOOTLOADER_BIN> |
| 581 | ``` |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 582 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 583 | ```bash |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 584 | esptool.py -p <PORT> -b 2000000 --after no_reset --chip <ESP_CHIP> write_flash --flash_mode dio --flash_size <FLASH_SIZE> --flash_freq 40m <PRIMARY_SLOT_FLASH_OFFSET> <APPLICATION_BIN> |
| 585 | ``` |
| 586 | |
| 587 | On the **first boot**, the bootloader will: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 588 | |
| 589 | 1. Encrypt flash in-place including bootloader, image primary/secondary slot and scratch using the |
| 590 | written key. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 591 | 2. Burn eFuse to enable Flash Encryption. |
| 592 | 3. Reset system to ensure Flash Encryption cache resets properly. |
| 593 | |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 594 | Encrypting data on the host: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 595 | |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 596 | - ESP32 |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 597 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 598 | ```bash |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 599 | espsecure.py encrypt_flash_data --keyfile <FLASH_ENCRYPTION_KEY.bin> --address <FLASH_OFFSET> --output <OUTPUT_DATA> <INPUT_DATA> |
| 600 | ``` |
| 601 | |
| 602 | - ESP32-S2, ESP32-C3 and ESP32-S3 |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 603 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 604 | ```bash |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 605 | espsecure.py encrypt_flash_data --aes_xts --keyfile <FLASH_ENCRYPTION_KEY.bin> --address <FLASH_OFFSET> --output <OUTPUT_DATA> <INPUT_DATA> |
| 606 | ``` |
| 607 | |
| 608 | --- |
| 609 | ***Note*** |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 610 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 611 | OTA updates are required to be sent plaintext. The reason is that, as said before, after the Flash |
| 612 | Encryption is enabled all read/write operations are decrypted/encrypted in runtime, so as e.g. if |
| 613 | pre-encrypted data is sent for an OTA update, it would be wrongly double-encrypted when the update |
| 614 | agent writes to the flash. |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 615 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 616 | For updating with an image encrypted on the host, flash it through serial using `esptool.py` as |
| 617 | above. **UART ROM Download Mode must not be disabled**. |
Almir Okato | b365e23 | 2022-03-08 01:35:54 -0300 | [diff] [blame] | 618 | |
| 619 | --- |
| 620 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 621 | ## [Security Chain scheme](#security-chain-scheme) |
| 622 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 623 | Using the 3 features, Secure Boot, Image signature verification and Flash Encryption, a Security |
| 624 | Chain can be established so only trusted code is executed, and also the code and content residing |
| 625 | in the off-chip flash are protected against undesirable reading. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 626 | |
| 627 | The overall final process when all features are enabled: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 628 | |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 629 | 1. ROM bootloader validates the MCUboot bootloader using RSA signature verification. |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 630 | 2. MCUboot bootloader validates the image using the chosen algorithm EC256/RSA/ED25519. It also |
| 631 | validates an upcoming image when updating. |
Almir Okato | 39eb63d | 2022-01-05 18:31:54 -0300 | [diff] [blame] | 632 | 3. Flash Encryption guarantees that code and data are not exposed. |
| 633 | |
| 634 | ### [Size Limitation](#size-limitation) |
| 635 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 636 | When all 3 features are enable at same time, the bootloader size may exceed the fixed limit for |
| 637 | the ROM bootloader checking on the Espressif chips **depending on which algorithm** was chosen for |
| 638 | MCUboot image signing. The issue <https://github.com/mcu-tools/mcuboot/issues/1262> was created to |
| 639 | track this limitation. |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 640 | |
| 641 | ## [Multi image](#multi-image) |
| 642 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 643 | The multi image feature (currently limited to 2 images) allows the images to be updated separately |
| 644 | (each one has its own primary and secondary slot) by MCUboot. |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 645 | |
| 646 | The Espressif port bootloader handles the boot in two different approaches: |
| 647 | |
| 648 | ### [Host OS boots second image](#host-os-boots-second-image) |
| 649 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 650 | Host OS from the *first image* is responsible for booting the *second image*, therefore the |
| 651 | bootloader is aware of the second image regions and can update it, however it does not load |
| 652 | neither boots it. |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 653 | |
| 654 | Configuration example (`bootloader.conf`): |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 655 | |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 656 | ``` |
| 657 | CONFIG_ESP_BOOTLOADER_SIZE=0xF000 |
| 658 | CONFIG_ESP_MCUBOOT_WDT_ENABLE=y |
| 659 | |
| 660 | # Enables multi image, if it is not defined, its assumed |
| 661 | # only one updatable image |
| 662 | CONFIG_ESP_IMAGE_NUMBER=2 |
| 663 | |
| 664 | # Example of values to be used when multi image is enabled |
| 665 | # Notice that the OS layer and update agent must be aware |
| 666 | # of these regions |
| 667 | CONFIG_ESP_APPLICATION_SIZE=0x50000 |
| 668 | CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS=0x10000 |
| 669 | CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS=0x60000 |
| 670 | CONFIG_ESP_IMAGE1_PRIMARY_START_ADDRESS=0xB0000 |
| 671 | CONFIG_ESP_IMAGE1_SECONDARY_START_ADDRESS=0x100000 |
| 672 | CONFIG_ESP_SCRATCH_OFFSET=0x150000 |
| 673 | CONFIG_ESP_SCRATCH_SIZE=0x40000 |
| 674 | ``` |
| 675 | |
| 676 | ### [Multi boot](#multi-boot) |
| 677 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 678 | In the multi boot approach the bootloader is responsible for booting two different images in two |
| 679 | different CPUs, firstly the *second image* on the APP CPU and then the *first image* on the PRO |
| 680 | CPU (current CPU), it is also responsible for update both images as well. Thus multi boot will be |
| 681 | only supported by Espressif multi core chips - currently only ESP32 is implemented. |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 682 | |
| 683 | --- |
| 684 | ***Note*** |
| 685 | |
| 686 | *The host OSes in each CPU must handle how the resources are divided/controlled between then.* |
| 687 | |
| 688 | --- |
| 689 | |
| 690 | Configuration example: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 691 | |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 692 | ``` |
| 693 | CONFIG_ESP_BOOTLOADER_SIZE=0xF000 |
| 694 | CONFIG_ESP_MCUBOOT_WDT_ENABLE=y |
| 695 | |
| 696 | # Enables multi image, if it is not defined, its assumed |
| 697 | # only one updatable image |
| 698 | CONFIG_ESP_IMAGE_NUMBER=2 |
| 699 | |
| 700 | # Enables multi image boot on independent processors |
| 701 | # (main host OS is not responsible for booting the second image) |
| 702 | # Use only with CONFIG_ESP_IMAGE_NUMBER=2 |
| 703 | CONFIG_ESP_MULTI_PROCESSOR_BOOT=y |
| 704 | |
| 705 | # Example of values to be used when multi image is enabled |
| 706 | # Notice that the OS layer and update agent must be aware |
| 707 | # of these regions |
| 708 | CONFIG_ESP_APPLICATION_SIZE=0x50000 |
| 709 | CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS=0x10000 |
| 710 | CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS=0x60000 |
| 711 | CONFIG_ESP_IMAGE1_PRIMARY_START_ADDRESS=0xB0000 |
| 712 | CONFIG_ESP_IMAGE1_SECONDARY_START_ADDRESS=0x100000 |
| 713 | CONFIG_ESP_SCRATCH_OFFSET=0x150000 |
| 714 | CONFIG_ESP_SCRATCH_SIZE=0x40000 |
| 715 | ``` |
| 716 | |
| 717 | ### [Image version dependency](#image-version-dependency) |
| 718 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 719 | MCUboot allows version dependency check between the images when updating them. As `imgtool.py` |
| 720 | allows a version assigment when signing an image, it is also possible to add the version |
| 721 | dependency constraint: |
| 722 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 723 | ```bash |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 724 | imgtool.py sign --align 4 -v <VERSION> -d "(<IMAGE_INDEX>, <VERSION_DEPENDENCY>)" -H 32 --pad-header -S <SLOT_SIZE> <BIN_IN> <SIGNED_BIN> |
| 725 | ``` |
| 726 | |
| 727 | - `<VERSION>` defines the version of the image being signed. |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 728 | - `"(<IMAGE_INDEX>, <VERSION_DEPENDENCY>)"` defines the minimum version and from which image is |
| 729 | needed to satisfy the dependency. |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 730 | |
| 731 | --- |
| 732 | Example: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 733 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 734 | ```bash |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 735 | imgtool.py sign --align 4 -v 1.0.0 -d "(1, 0.0.1+0)" -H 32 --pad-header -S 0x100000 image0.bin image0-signed.bin |
| 736 | ``` |
| 737 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 738 | Supposing that the image 0 is being signed, its version is 1.0.0 and it depends on image 1 with |
| 739 | version at least 0.0.1+0. |
Almir Okato | a1d641d | 2022-02-21 19:31:46 -0300 | [diff] [blame] | 740 | |
| 741 | --- |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 742 | |
| 743 | ## [Serial recovery mode](#serial-recovery-mode) |
| 744 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 745 | Serial recovery mode allows management through MCUMGR (more information and how to install it: |
| 746 | <https://github.com/apache/mynewt-mcumgr-cli>) for communicating and uploading a firmware to the |
| 747 | device. |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 748 | |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 749 | Configuration example: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 750 | |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 751 | ``` |
| 752 | # Enables the MCUboot Serial Recovery, that allows the use of |
| 753 | # MCUMGR to upload a firmware through the serial port |
| 754 | CONFIG_ESP_MCUBOOT_SERIAL=y |
| 755 | # GPIO used to boot on Serial Recovery |
| 756 | CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT=32 |
| 757 | # GPIO input type (0 for Pull-down, 1 for Pull-up) |
| 758 | CONFIG_ESP_SERIAL_BOOT_GPIO_INPUT_TYPE=0 |
| 759 | # GPIO signal value |
| 760 | CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT_VAL=1 |
| 761 | # Delay time for identify the GPIO signal |
| 762 | CONFIG_ESP_SERIAL_BOOT_DETECT_DELAY_S=5 |
| 763 | # UART port used for serial communication |
| 764 | CONFIG_ESP_SERIAL_BOOT_UART_NUM=1 |
| 765 | # GPIO for Serial RX signal |
| 766 | CONFIG_ESP_SERIAL_BOOT_GPIO_RX=25 |
| 767 | # GPIO for Serial TX signal |
| 768 | CONFIG_ESP_SERIAL_BOOT_GPIO_TX=26 |
| 769 | ``` |
| 770 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 771 | When enabled, the bootloader checks the if the GPIO `<CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT>` |
| 772 | configured has the signal value `<CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT_VAL>` for approximately |
| 773 | `<CONFIG_ESP_SERIAL_BOOT_DETECT_DELAY_S>` seconds for entering the Serial recovery mode. Example: |
| 774 | a button configured on GPIO 32 pressed for 5 seconds. |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 775 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 776 | Serial mode then uses the UART port configured for communication |
| 777 | (`<CONFIG_ESP_SERIAL_BOOT_UART_NUM>`, pins `<CONFIG_ESP_SERIAL_BOOT_GPIO_RX>`, |
| 778 | `<CONFIG_ESP_SERIAL_BOOT_GPIO_RX>`). |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 779 | |
Almir Okato | 09cca38 | 2022-09-23 15:25:28 -0300 | [diff] [blame] | 780 | ### [Serial Recovery through USB JTAG Serial port](#serial-recovery-through-usb-jtag-serial-port) |
| 781 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 782 | Some chips, like ESP32-C3 and ESP32-S3 have an integrated USB JTAG Serial Controller that |
| 783 | implements a serial port (CDC) that can also be used for handling MCUboot Serial Recovery. |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 784 | More information about the USB pins and hardware configuration: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 785 | |
| 786 | - ESP32-C3: <https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-guides/usb-serial-jtag-console.html> |
| 787 | - ESP32-S3: <https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-guides/usb-serial-jtag-console.html> |
| 788 | - ESP32-C6: <https://docs.espressif.com/projects/esp-idf/en/latest/esp32c6/api-guides/usb-serial-jtag-console.html> |
| 789 | - ESP32-H2: <https://docs.espressif.com/projects/esp-idf/en/latest/esp32h2/api-guides/usb-serial-jtag-console.html> |
Almir Okato | 09cca38 | 2022-09-23 15:25:28 -0300 | [diff] [blame] | 790 | |
| 791 | Configuration example: |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 792 | |
Almir Okato | 09cca38 | 2022-09-23 15:25:28 -0300 | [diff] [blame] | 793 | ``` |
| 794 | # Use Serial through USB JTAG Serial port for Serial Recovery |
| 795 | CONFIG_ESP_MCUBOOT_SERIAL_USB_SERIAL_JTAG=y |
| 796 | # Use sector erasing (recommended) instead of entire image size |
| 797 | # erasing when uploading through Serial Recovery |
| 798 | CONFIG_ESP_MCUBOOT_ERASE_PROGRESSIVELY=y |
| 799 | # GPIO used to boot on Serial Recovery |
| 800 | CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT=5 |
| 801 | # GPIO input type (0 for Pull-down, 1 for Pull-up) |
| 802 | CONFIG_ESP_SERIAL_BOOT_GPIO_INPUT_TYPE=0 |
| 803 | # GPIO signal value |
| 804 | CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT_VAL=1 |
| 805 | # Delay time for identify the GPIO signal |
| 806 | CONFIG_ESP_SERIAL_BOOT_DETECT_DELAY_S=5 |
| 807 | ``` |
| 808 | |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 809 | --- |
| 810 | :warning: ***ATTENTION*** |
| 811 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 812 | *When working with Flash Encryption enabled, `CONFIG_ESP_MCUBOOT_ERASE_PROGRESSIVELY` must be |
| 813 | __disabled__, although it is recommended for common Serial Recovery usage* |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 814 | |
| 815 | --- |
| 816 | |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 817 | ### [MCUMGR image upload example](#mcumgr-image-upload-example) |
| 818 | |
| 819 | After entering the Serial recovery mode on the device, MCUMGR can be used as following: |
| 820 | |
| 821 | Configure the connection: |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 822 | ```bash |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 823 | mcumgr conn add esp type="serial" connstring="dev=<PORT>,baud=115200,mtu=256" |
| 824 | ``` |
| 825 | |
| 826 | Upload the image (the process may take some time): |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 827 | ```bash |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 828 | mcumgr -c esp image upload <IMAGE_BIN> |
| 829 | ``` |
| 830 | |
| 831 | Reset the device: |
Almir Okato | 8724081 | 2022-12-21 00:05:07 -0300 | [diff] [blame] | 832 | ```bash |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 833 | mcumgr -c esp reset |
| 834 | ``` |
| 835 | |
| 836 | --- |
| 837 | :warning: ***ATTENTION*** |
| 838 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 839 | *Serial recovery mode uploads the image to the PRIMARY_SLOT, therefore if the upload process gets |
| 840 | interrupted the image may be corrupted and unable to boot* |
Almir Okato | e8cbc0d | 2022-06-13 10:45:39 -0300 | [diff] [blame] | 841 | |
| 842 | --- |
Almir Okato | 4a05118 | 2023-08-09 14:50:04 -0300 | [diff] [blame] | 843 | |
| 844 | ## [Memory map organization for OS compatibility](#memory-map-organization-for-os-compatibility) |
| 845 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 846 | When adding support for this MCUboot port to an OS or even customizing an already supported |
| 847 | application memory layout, it is mandatory for the OS linker script to avoid overlaping on |
| 848 | `iram_loader_seg` and `dram_seg` bootloader RAM regions. Although part of the RAM becomes initially |
| 849 | unavailable, it is reclaimable by the OS after boot as heap. |
Almir Okato | 4a05118 | 2023-08-09 14:50:04 -0300 | [diff] [blame] | 850 | |
| 851 | Therefore, the application must be designed aware of the bootloader memory usage. |
| 852 | |
| 853 | --- |
| 854 | ***Note*** |
| 855 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 856 | *Mostly of the Espressif chips have a separation on the address space for the same physical memory |
| 857 | ammount: IRAM (accessed by the instruction bus) and DRAM (accessed by the data bus), which means |
| 858 | that they need to be accessed by different addresses ranges depending on type, but refer to the |
| 859 | same region. More information on the |
| 860 | [Espressif TRMs](https://www.espressif.com/en/support/documents/technical-documents?keys=&field_download_document_type_tid%5B%5D=963).* |
Almir Okato | 4a05118 | 2023-08-09 14:50:04 -0300 | [diff] [blame] | 861 | |
| 862 | --- |
| 863 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 864 | The following diagrams illustrate a memory organization from the bootloader point of view (notice |
| 865 | that the addresses and sizes may vary depending on the chip), they reflect the linker script |
| 866 | `boot/espressif/port/<TARGET>/ld/bootloader.ld`: |
Almir Okato | 4a05118 | 2023-08-09 14:50:04 -0300 | [diff] [blame] | 867 | |
| 868 | ### ESP32 |
| 869 | |
| 870 | #### ESP32 standard |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 871 | |
Almir Okato | 4a05118 | 2023-08-09 14:50:04 -0300 | [diff] [blame] | 872 | ``` |
| 873 | SRAM0 |
| 874 | IRAM ADDR / DRAM ADDR |
| 875 | * +--------+--------------+------+ 0x40070000 / --------- - SRAM0 START |
| 876 | * | ^ | |
| 877 | * | | PRO CPU Cache | *NOT CLAIMABLE BY OS RAM |
| 878 | * | v | |
| 879 | * +--------+--------------+------+ 0x40078000 / ---------- |
| 880 | * | ^ | |
| 881 | * | | | *NOT CLAIMABLE BY OS RAM |
| 882 | * | | iram_loader_seg | *Region usable as iram_loader_seg during boot |
| 883 | * | | (APP CPU Cache) | as APP CPU is not initialized yet |
| 884 | * | | | |
| 885 | * | v | |
| 886 | * +--------+--------------+------+ 0x40080000 / ---------- |
| 887 | * | ^ | |
| 888 | * | | FREE | *CLAIMABLE BY OS RAM |
| 889 | * | v | |
| 890 | * +------------------------------+ 0x40090000 / ---------- |
| 891 | * | ^ | |
| 892 | * | | iram_seg | *CLAIMABLE BY OS RAM |
| 893 | * | | | |
| 894 | * | v | |
| 895 | * +--------+--------------+------+ 0x40099000 / ---------- |
| 896 | * | | FREE | *CLAIMABLE BY OS RAM |
| 897 | * +------------------------------+ 0x4009FFFF / ---------- - SRAM0 END |
| 898 | |
| 899 | SRAM1 |
| 900 | IRAM ADDR / DRAM ADDR |
| 901 | * +------------------------------+ 0x400A0000 / 0x3FFFFFFF - SRAM1 START |
| 902 | * | ^ | |
| 903 | * | | | *** SHOULD NOT BE OVERLAPPED *** |
| 904 | * | | dram_seg | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 905 | * | v | |
| 906 | * +--------+--------------+------+ 0x400AB900 / 0x3FFF4700 |
| 907 | * | ^ | |
| 908 | * | | | |
| 909 | * | | | |
| 910 | * | | FREE | *CLAIMABLE BY OS RAM |
| 911 | * | | | |
| 912 | * | | | |
| 913 | * | v | |
| 914 | * +--------+--------------+------+ 0x400BFFFF / 0x3FFE0000 - SRAM1 END |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 915 | Note: On ESP32 the SRAM1 addresses are accessed in reverse order comparing Instruction |
| 916 | bus (IRAM) and Data bus (DRAM), but refer to the same location. See the TRM for more |
| 917 | information. |
Almir Okato | 4a05118 | 2023-08-09 14:50:04 -0300 | [diff] [blame] | 918 | |
| 919 | SRAM2 |
| 920 | IRAM ADDR / DRAM ADDR |
| 921 | * +--------+--------------+------+ ---------- / 0x3FFAE000 - SRAM2 START |
| 922 | * | | FREE | *CLAIMABLE BY OS RAM |
| 923 | * +--------+--------------+------+ ---------- / 0x3FFDFFFF - SRAM2 END |
| 924 | ``` |
| 925 | |
| 926 | #### ESP32 Multi Processor Boot |
| 927 | |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 928 | This is the linker script mapping when the `CONFIG_ESP_MULTI_PROCESSOR_BOOT` is enabled |
| 929 | ([Multi boot](#multi-boot)) since APP CPU Cache region cannot be used for `iram_loader_seg` region |
| 930 | as there would be conflict when the bootloader starts the APP CPU before jump to the main |
| 931 | application. |
Almir Okato | 4a05118 | 2023-08-09 14:50:04 -0300 | [diff] [blame] | 932 | |
| 933 | ``` |
| 934 | SRAM0 |
| 935 | IRAM ADDR / DRAM ADDR |
| 936 | * +--------+--------------+------+ 0x40070000 / --------- - SRAM0 START |
| 937 | * | ^ | |
| 938 | * | | | |
| 939 | * | | Cache | *Used by PRO CPU and APP CPU as Cache |
| 940 | * | | | |
| 941 | * | v | |
| 942 | * +--------+--------------+------+ 0x40080000 / ---------- |
| 943 | * | ^ | |
| 944 | * | | FREE | *CLAIMABLE BY OS RAM |
| 945 | * | v | |
| 946 | * +------------------------------+ 0x40090000 / ---------- |
| 947 | * | ^ | |
| 948 | * | | iram_seg | *CLAIMABLE BY OS RAM |
| 949 | * | | | |
| 950 | * | v | |
| 951 | * +--------+--------------+------+ 0x40099000 / ---------- |
| 952 | * | | FREE | *CLAIMABLE BY OS RAM |
| 953 | * +------------------------------+ 0x4009FFFF / ---------- - SRAM0 END |
| 954 | |
| 955 | SRAM1 |
| 956 | IRAM ADDR / DRAM ADDR |
| 957 | * +------------------------------+ 0x400A0000 / 0x3FFFFFFF - SRAM1 START |
| 958 | * | ^ | |
| 959 | * | | | *** SHOULD NOT BE OVERLAPPED *** |
| 960 | * | | dram_seg | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 961 | * | v | |
| 962 | * +--------+--------------+------+ 0x400AB900 / 0x3FFF4700 |
| 963 | * | ^ | |
| 964 | * | | | *** SHOULD NOT BE OVERLAPPED *** |
| 965 | * | | iram_loader_seg | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 966 | * | | | |
| 967 | * | v | |
| 968 | * +------------------------------+ 0x400B1E00 / 0x3FFEE200 |
| 969 | * | ^ | |
| 970 | * | | | |
| 971 | * | | FREE | *CLAIMABLE BY OS RAM |
| 972 | * | | | |
| 973 | * | v | |
| 974 | * +--------+--------------+------+ 0x400BFFFF / 0x3FFE0000 - SRAM1 END |
Fabio Utzig | b58962f | 2023-09-02 15:58:55 -0300 | [diff] [blame^] | 975 | Note: On ESP32 the SRAM1 addresses are accessed in reverse order comparing Instruction |
| 976 | bus (IRAM) and Data bus (DRAM), but refer to the same location. See the TRM for more |
| 977 | information. |
Almir Okato | 4a05118 | 2023-08-09 14:50:04 -0300 | [diff] [blame] | 978 | |
| 979 | SRAM2 |
| 980 | IRAM ADDR / DRAM ADDR |
| 981 | * +--------+--------------+------+ ---------- / 0x3FFAE000 - SRAM2 START |
| 982 | * | | FREE | *CLAIMABLE BY OS RAM |
| 983 | * +--------+--------------+------+ ---------- / 0x3FFDFFFF - SRAM2 END |
| 984 | ``` |
| 985 | |
| 986 | ### ESP32-S2 |
| 987 | |
| 988 | ``` |
| 989 | SRAM0 |
| 990 | IRAM ADDR / DRAM ADDR |
| 991 | * +--------+--------------+------+ 0x40020000 / 0x3FFB0000 - SRAM0 START |
| 992 | * | | FREE | *CLAIMABLE BY OS RAM |
| 993 | * +--------+--------------+------+ 0x40027FFF / 0x3FFB7FFF - SRAM0 END |
| 994 | |
| 995 | SRAM1 |
| 996 | IRAM ADDR / DRAM ADDR |
| 997 | * +--------+--------------+------+ 0x40028000 / 0x3FFB8000 - SRAM1 START |
| 998 | * | ^ | |
| 999 | * | | | |
| 1000 | * | | FREE | *CLAIMABLE BY OS RAM |
| 1001 | * | | | |
| 1002 | * | v | |
| 1003 | * +--------+--------------+------+ 0x40047000 / 0x3FFD7000 |
| 1004 | * | ^ | |
| 1005 | * | | | |
| 1006 | * | | | |
| 1007 | * | | iram_seg | *CLAIMABLE BY OS RAM |
| 1008 | * | | | |
| 1009 | * | | | |
| 1010 | * | v | |
| 1011 | * +------------------------------+ 0x40050000 / 0x3FFE0000 |
| 1012 | * | ^ | |
| 1013 | * | | | |
| 1014 | * | | | |
| 1015 | * | | iram_loader_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1016 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1017 | * | | | |
| 1018 | * | v | |
| 1019 | * +------------------------------+ 0x40056000 / 0x3FFE6000 |
| 1020 | * | ^ | |
| 1021 | * | | | |
| 1022 | * | | dram_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1023 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1024 | * | v | |
| 1025 | * +--------+--------------+------+ 0x4006FFFF / 0x3FFFFFFF - SRAM1 END |
| 1026 | ``` |
| 1027 | |
| 1028 | ### ESP32-S3 |
| 1029 | |
| 1030 | ``` |
| 1031 | SRAM0 |
| 1032 | IRAM ADDR / DRAM ADDR |
| 1033 | * +--------+--------------+------+ 0x40370000 / ---------- - SRAM0 START |
| 1034 | * | | FREE | *CLAIMABLE BY OS RAM |
| 1035 | * +--------+--------------+------+ 0x40377FFF / ---------- - SRAM0 END |
| 1036 | |
| 1037 | SRAM1 |
| 1038 | IRAM ADDR / DRAM ADDR |
| 1039 | * +--------+--------------+------+ 0x40378000 / 0x3FC88000 - SRAM1 START |
| 1040 | * | ^ | |
| 1041 | * | | | |
| 1042 | * | | FREE | *CLAIMABLE BY OS RAM |
| 1043 | * | | | |
| 1044 | * | v | |
| 1045 | * +--------+--------------+------+ 0x403B0000 / 0x3FCC0000 |
| 1046 | * | ^ | |
| 1047 | * | | | |
| 1048 | * | | | |
| 1049 | * | | iram_seg | *CLAIMABLE BY OS RAM |
| 1050 | * | | | |
| 1051 | * | | | |
| 1052 | * | v | |
| 1053 | * +------------------------------+ 0x403BA000 / 0x3FCCA000 |
| 1054 | * | ^ | |
| 1055 | * | | | |
| 1056 | * | | | |
| 1057 | * | | iram_loader_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1058 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1059 | * | | | |
| 1060 | * | v | |
| 1061 | * +------------------------------+ 0x403C0000 / 0x3FCD0000 |
| 1062 | * | ^ | |
| 1063 | * | | | |
| 1064 | * | | dram_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1065 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1066 | * | v | |
| 1067 | * +--------+--------------+------+ 0x403DFFFF / 0x3FCEFFFF - SRAM1 END |
| 1068 | |
| 1069 | SRAM2 |
| 1070 | IRAM ADDR / DRAM ADDR |
| 1071 | * +--------+--------------+------+ ---------- / 0x3FCF0000 - SRAM2 START |
| 1072 | * | | FREE | *CLAIMABLE BY OS RAM |
| 1073 | * +--------+--------------+------+ ---------- / 0x3FCFFFFF - SRAM2 END |
| 1074 | ``` |
| 1075 | |
| 1076 | ### ESP32-C2 |
| 1077 | |
| 1078 | ``` |
| 1079 | SRAM0 |
| 1080 | IRAM ADDR / DRAM ADDR |
| 1081 | * +--------+--------------+------+ 0x4037C000 / ---------- - SRAM0 START |
| 1082 | * | | FREE | *CLAIMABLE BY OS RAM |
| 1083 | * +--------+--------------+------+ 0x4037FFFF / ---------- - SRAM0 END |
| 1084 | |
| 1085 | SRAM1 |
| 1086 | IRAM ADDR / DRAM ADDR |
| 1087 | * +--------+--------------+------+ 0x40380000 / 0x3FCA0000 - SRAM1 START |
| 1088 | * | ^ | |
| 1089 | * | | | |
| 1090 | * | | | |
| 1091 | * | | FREE | *CLAIMABLE BY OS RAM |
| 1092 | * | | | |
| 1093 | * | | | |
| 1094 | * | v | |
| 1095 | * +--------+--------------+------+ 0x403A1370 / 0x3FCC1370 |
| 1096 | * | ^ | |
| 1097 | * | | | |
| 1098 | * | | | |
| 1099 | * | | iram_seg | *CLAIMABLE BY OS RAM |
| 1100 | * | | | |
| 1101 | * | | | |
| 1102 | * | v | |
| 1103 | * +------------------------------+ 0x403A9B70 / 0x3FCC9B70 |
| 1104 | * | ^ | |
| 1105 | * | | | |
| 1106 | * | | | |
| 1107 | * | | iram_loader_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1108 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1109 | * | | | |
| 1110 | * | v | |
| 1111 | * +------------------------------+ 0x403B0B70 / 0x3FCD0B70 |
| 1112 | * | ^ | |
| 1113 | * | | | |
| 1114 | * | | dram_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1115 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1116 | * | v | |
| 1117 | * +--------+--------------+------+ 0x403BFFFF / 0x3FCDFFFF - SRAM1 END |
| 1118 | ``` |
| 1119 | |
| 1120 | ### ESP32-C3 |
| 1121 | |
| 1122 | ``` |
| 1123 | SRAM0 |
| 1124 | IRAM ADDR / DRAM ADDR |
| 1125 | * +--------+--------------+------+ 0x4037C000 / ---------- - SRAM0 START |
| 1126 | * | | FREE | *CLAIMABLE BY OS RAM |
| 1127 | * +--------+--------------+------+ 0x4037FFFF / ---------- - SRAM0 END |
| 1128 | |
| 1129 | SRAM1 |
| 1130 | IRAM ADDR / DRAM ADDR |
| 1131 | * +--------+--------------+------+ 0x40380000 / 0x3FC80000 - SRAM1 START |
| 1132 | * | ^ | |
| 1133 | * | | | |
| 1134 | * | | | |
| 1135 | * | | FREE | *CLAIMABLE BY OS RAM |
| 1136 | * | | | |
| 1137 | * | | | |
| 1138 | * | v | |
| 1139 | * +--------+--------------+------+ 0x403C7000 / 0x3FCC7000 |
| 1140 | * | ^ | |
| 1141 | * | | | |
| 1142 | * | | | |
| 1143 | * | | iram_seg | *CLAIMABLE BY OS RAM |
| 1144 | * | | | |
| 1145 | * | | | |
| 1146 | * | v | |
| 1147 | * +------------------------------+ 0x403D0000 / 0x3FCD0000 |
| 1148 | * | ^ | |
| 1149 | * | | | |
| 1150 | * | | | |
| 1151 | * | | iram_loader_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1152 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1153 | * | | | |
| 1154 | * | v | |
| 1155 | * +------------------------------+ 0x403D5000 / 0x3FCD5000 |
| 1156 | * | ^ | |
| 1157 | * | | | |
| 1158 | * | | dram_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1159 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1160 | * | v | |
| 1161 | * +--------+--------------+------+ 0x403DFFFF / 0x3FCDFFFF - SRAM1 END |
| 1162 | ``` |
| 1163 | |
| 1164 | ### ESP32-C6 |
| 1165 | |
| 1166 | ``` |
| 1167 | IRAM ADDR / DRAM ADDR |
| 1168 | * +--------+--------------+------+ 0x40800000 / 0x40800000 - HP SRAM START |
| 1169 | * | ^ | |
| 1170 | * | | | |
| 1171 | * | | | |
| 1172 | * | | FREE | *CLAIMABLE BY OS RAM |
| 1173 | * | | | |
| 1174 | * | | | |
| 1175 | * | v | |
| 1176 | * +--------+--------------+------+ 0x40860610 / 0x40860610 |
| 1177 | * | ^ | |
| 1178 | * | | | |
| 1179 | * | | | |
| 1180 | * | | iram_seg | *CLAIMABLE BY OS RAM |
| 1181 | * | | | |
| 1182 | * | | | |
| 1183 | * | v | |
| 1184 | * +------------------------------+ 0x40869610 / 0x40869610 |
| 1185 | * | ^ | |
| 1186 | * | | | |
| 1187 | * | | | |
| 1188 | * | | iram_loader_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1189 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1190 | * | | | |
| 1191 | * | v | |
| 1192 | * +------------------------------+ 0x40870610 / 0x40870610 |
| 1193 | * | ^ | |
| 1194 | * | | | |
| 1195 | * | | dram_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1196 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1197 | * | v | |
| 1198 | * +--------+--------------+------+ 0x4087FFFF / 0x4087FFFF - HP SRAM END |
| 1199 | ``` |
| 1200 | |
| 1201 | ### ESP32-H2 |
| 1202 | |
| 1203 | ``` |
| 1204 | IRAM ADDR / DRAM ADDR |
| 1205 | * +--------+--------------+------+ 0x40800000 / 0x40800000 - HP SRAM START |
| 1206 | * | ^ | |
| 1207 | * | | | |
| 1208 | * | | | |
| 1209 | * | | FREE | *CLAIMABLE BY OS RAM |
| 1210 | * | | | |
| 1211 | * | | | |
| 1212 | * | v | |
| 1213 | * +--------+--------------+------+ 0x408317D0 / 0x408317D0 |
| 1214 | * | ^ | |
| 1215 | * | | | |
| 1216 | * | | | |
| 1217 | * | | iram_seg | *CLAIMABLE BY OS RAM |
| 1218 | * | | | |
| 1219 | * | | | |
| 1220 | * | v | |
| 1221 | * +------------------------------+ 0x40839FD0 / 0x40839FD0 |
| 1222 | * | ^ | |
| 1223 | * | | | |
| 1224 | * | | | |
| 1225 | * | | iram_loader_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1226 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1227 | * | | | |
| 1228 | * | v | |
| 1229 | * +------------------------------+ 0x40840FD0 / 0x40840FD0 |
| 1230 | * | ^ | |
| 1231 | * | | | |
| 1232 | * | | dram_seg | *** SHOULD NOT BE OVERLAPPED *** |
| 1233 | * | | | *** OS CAN RECLAIM IT AFTER BOOT LATER AS HEAP *** |
| 1234 | * | v | |
| 1235 | * +--------+--------------+------+ 0x4084FFFF / 0x4084FFFF - HP SRAM END |
| 1236 | ``` |