blob: e72384f5641c12ae0d46755b278f8d1deab61d36 [file] [log] [blame] [view]
Almir Okato39eb63d2022-01-05 18:31:54 -03001# [Building and using MCUboot with Espressif's chips](#building-and-using-mcuboot-with-espressifs-chips)
Almir Okato428e2e72021-08-11 10:52:10 -03002
3The Espressif port is build on top of ESP-IDF HAL, therefore it is required in order to build MCUboot for Espressif SoCs.
4
5Documentation about the MCUboot bootloader design, operation and features can be found in the [design document](design.md).
6
Almir Okato39eb63d2022-01-05 18:31:54 -03007## [SoC support availability](#soc-support-availability)
Almir Okato428e2e72021-08-11 10:52:10 -03008
9The current port is available for use in the following SoCs within the OSes:
Almir Okato428e2e72021-08-11 10:52:10 -030010
Sylvio Alvesd8eff812022-01-31 23:16:06 -030011| | ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 |
12| :-----: | :-----: | :-----: | :-----: | :-----: |
13| Zephyr | Supported | Supported | Supported | WIP |
14| NuttX | Supported | Supported | Supported | WIP |
Almir Okato39eb63d2022-01-05 18:31:54 -030015
16## [Installing requirements and dependencies](#installing-requirements-and-dependencies)
Almir Okato428e2e72021-08-11 10:52:10 -030017
181. Install additional packages required for development with MCUboot:
19
20```
Francesco Servidio4ff0c182021-10-20 15:27:16 +020021 cd ~/mcuboot # or to your directory where MCUboot is cloned
Almir Okato428e2e72021-08-11 10:52:10 -030022 pip3 install --user -r scripts/requirements.txt
23```
24
252. Update the submodules needed by the Espressif port. This may take a while.
26
27```
28git submodule update --init --recursive --checkout boot/espressif/hal/esp-idf
29```
30
Francesco Servidio582367c2021-10-20 15:36:45 +0200313. Next, get the Mbed TLS submodule required by MCUboot.
Almir Okato428e2e72021-08-11 10:52:10 -030032```
33git submodule update --init --recursive ext/mbedtls
34```
35
364. Now we need to install IDF dependencies and set environment variables. This step may take some time:
37```
38cd boot/espressif/hal/esp-idf
39./install.sh
40. ./export.sh
41cd ../..
42```
43
Almir Okato39eb63d2022-01-05 18:31:54 -030044## [Building the bootloader itself](#building-the-bootloader-itself)
Almir Okato428e2e72021-08-11 10:52:10 -030045
46The MCUboot Espressif port bootloader is built using the toolchain and tools provided by ESP-IDF. Additional configuration related to MCUboot features and slot partitioning may be made using the `bootloader.conf`.
47
Francesco Servidio2fe449d2021-10-21 12:38:36 +020048---
49***Note***
50
Almir Okato39eb63d2022-01-05 18:31:54 -030051*Replace `<TARGET>` with the target ESP32 family (like `esp32`, `esp32s2` and others).*
Francesco Servidio2fe449d2021-10-21 12:38:36 +020052
53---
Almir Okato428e2e72021-08-11 10:52:10 -030054
551. Compile and generate the ELF:
56
57```
Almir Okato39eb63d2022-01-05 18:31:54 -030058cmake -DCMAKE_TOOLCHAIN_FILE=tools/toolchain-<TARGET>.cmake -DMCUBOOT_TARGET=<TARGET> -B build -GNinja
Almir Okato428e2e72021-08-11 10:52:10 -030059cmake --build build/
60```
61
622. Convert the ELF to the final bootloader image, ready to be flashed:
63
64```
Almir Okato39eb63d2022-01-05 18:31:54 -030065esptool.py --chip <TARGET> elf2image --flash_mode dio --flash_freq 40m --flash_size <FLASH_SIZE> -o build/mcuboot_<TARGET>.bin build/mcuboot_<TARGET>.elf
Almir Okato428e2e72021-08-11 10:52:10 -030066```
67
Almir Okato39eb63d2022-01-05 18:31:54 -0300683. Flash MCUboot in your device:
Almir Okato428e2e72021-08-11 10:52:10 -030069
70```
Almir Okato39eb63d2022-01-05 18:31:54 -030071esptool.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 <BOOTLOADER_FLASH_OFFSET> build/mcuboot_<TARGET>.bin
Almir Okato428e2e72021-08-11 10:52:10 -030072```
Sylvio Alvesd5230942022-01-20 21:35:53 -030073---
Almir Okatob365e232022-03-08 01:35:54 -030074***Note***
Almir Okatoa1d641d2022-02-21 19:31:46 -030075
76You may adjust the port `<PORT>` (like `/dev/ttyUSB0`) and baud rate `<BAUD>` (like `2000000`) according to the connection with your board.
Sylvio Alvesd5230942022-01-20 21:35:53 -030077You can also skip `<PORT>` and `<BAUD>` parameters so that esptool tries to automatically detect it.
Almir Okato428e2e72021-08-11 10:52:10 -030078
Sylvio Alvesd5230942022-01-20 21:35:53 -030079*`<FLASH_SIZE>` can be found using the command below:*
80```
81esptool.py -p <PORT> -b <BAUD> flash_id
82```
Almir Okatob365e232022-03-08 01:35:54 -030083The output contains device information and its flash size:
Sylvio Alvesd5230942022-01-20 21:35:53 -030084```
85Detected flash size: 4MB
86```
87
88
89*`<BOOTLOADER_FLASH_OFFSET>` value must follow one of the addresses below:*
Almir Okatob365e232022-03-08 01:35:54 -030090
Sylvio Alvesd8eff812022-01-31 23:16:06 -030091| ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 |
92| :-----: | :-----: | :-----: | :-----: |
93| 0x1000 | 0x1000 | 0x0000 | 0x0000 |
Sylvio Alvesd5230942022-01-20 21:35:53 -030094
95---
Almir Okato39eb63d2022-01-05 18:31:54 -030096
97## [Signing and flashing an application](#signing-and-flashing-an-application)
98
991. Images can be regularly signed with the `scripts/imgtool.py` script:
100
101```
Sylvio Alvesd5230942022-01-20 21:35:53 -0300102imgtool.py sign --align 4 -v 0 -H 32 --pad-header -S <SLOT_SIZE> <BIN_IN> <SIGNED_BIN>
Almir Okato39eb63d2022-01-05 18:31:54 -0300103```
104
Sylvio Alvesd5230942022-01-20 21:35:53 -0300105---
106
Almir Okatob365e232022-03-08 01:35:54 -0300107***Note***
Almir Okatoa1d641d2022-02-21 19:31:46 -0300108
Almir Okatob365e232022-03-08 01:35:54 -0300109`<SLOT_SIZE>` is the size of the slot to be used.
Sylvio Alvesd5230942022-01-20 21:35:53 -0300110Default slot0 size is `0x100000`, but it can change as per application flash partitions.
111
112For Zephyr images, `--pad-header` is not needed as it already has the padding for MCUboot header.
Almir Okato39eb63d2022-01-05 18:31:54 -0300113
114---
Sylvio Alvesd5230942022-01-20 21:35:53 -0300115
Almir Okato39eb63d2022-01-05 18:31:54 -0300116:warning: ***ATTENTION***
117
118*This is the basic signing needed for adding MCUboot headers and trailers.
119For signing with a crypto key and guarantee the authenticity of the image being booted, see the section [MCUboot image signature verification](#mcuboot-image-signature-verification) below.*
120
121---
122
1232. Flash the signed application:
124
125```
126esptool.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>
127```
128
129# [Security Chain on Espressif port](#security-chain-on-espressif-port)
130
131[MCUboot encrypted images](encrypted_images.md) do not provide full code confidentiality when only external storage is available (see [Threat model](encrypted_images.md#threat-model)) since by MCUboot design the image in Primary Slot, from where the image is executed, is stored plaintext.
132Espressif chips have off-chip flash memory, so to ensure a security chain along with MCUboot image signature verification, the hardware-assisted Secure Boot and Flash Encryption were made available on the MCUboot Espressif port.
133
134## [MCUboot image signature verification](#mcuboot-image-signature-verification)
135
136The image that MCUboot is booting can be signed with 4 types of keys: RSA-2048, RSA-3072, EC256 and ED25519. In order to enable the feature, the **bootloader** must be compiled with the following configurations:
137
138---
139***Note***
Almir Okatoa1d641d2022-02-21 19:31:46 -0300140
Almir Okato39eb63d2022-01-05 18:31:54 -0300141*It is strongly recommended to generate a new signing key using `imgtool` instead of use the existent samples.*
142
143---
144
145#### For EC256 algorithm use
146```
147CONFIG_ESP_SIGN_EC256=y
148
149# Use Tinycrypt lib for EC256 or ED25519 signing
150CONFIG_ESP_USE_TINYCRYPT=y
151
152CONFIG_ESP_SIGN_KEY_FILE=<YOUR_SIGNING_KEY.pem>
153```
154
155#### For ED25519 algorithm use
156```
157CONFIG_ESP_SIGN_ED25519=y
158
159# Use Tinycrypt lib for EC256 or ED25519 signing
160CONFIG_ESP_USE_TINYCRYPT=y
161
162CONFIG_ESP_SIGN_KEY_FILE=<YOUR_SIGNING_KEY.pem>
163```
164
165#### For RSA (2048 or 3072) algorithm use
166```
167CONFIG_ESP_SIGN_RSA=y
168# RSA_LEN is 2048 or 3072
169CONFIG_ESP_SIGN_RSA_LEN=<RSA_LEN>
170
171# Use Mbed TLS lib for RSA image signing
172CONFIG_ESP_USE_MBEDTLS=y
173
174CONFIG_ESP_SIGN_KEY_FILE=<YOUR_SIGNING_KEY.pem>
175```
176
177Notice that the public key will be embedded in the bootloader code, since the hardware key storage is not supported by Espressif port.
178
179### [Signing the image](#signing-the-image)
180
181Now you need to sign the **image binary**, use the `imgtool` with `-k` parameter:
182
183```
184imgtool.py sign -k <YOUR_SIGNING_KEY.pem> --pad --pad-sig --align 4 -v 0 -H 32 --pad-header -S 0x00100000 <BIN_IN> <BIN_OUT>
185```
186If signing a Zephyr image, the `--pad-header` is not needed, as it already have the padding for MCUboot header.
187
188
189## [Secure Boot](#secure-boot)
190
191The Secure Boot implementation is based on [IDF's Secure Boot V2](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/security/secure-boot-v2.html), is hardware-assisted and RSA based, and has the role for ensuring that only authorized code will be executed on the device. This is done through bootloader signature checking by the ROM bootloader. \
192***Note***: ROM bootloader is the First Stage Bootloader, while the Espressif MCUboot port is the Second Stage Bootloader.
193
194### [Building bootloader with Secure Boot](#building-bootloader-with-secure-boot)
195
196In order to build the bootloader with the feature on, the following configurations must be enabled:
197```
198CONFIG_SECURE_BOOT=1
199CONFIG_SECURE_BOOT_V2_ENABLED=1
200CONFIG_SECURE_SIGNED_ON_BOOT=1
201CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME=1
202CONFIG_SECURE_BOOT_SUPPORTS_RSA=1
203```
204
205---
206:warning: ***ATTENTION***
207
208*On development phase is recommended add the following configuration in order to keep the debugging enabled and also to avoid any unrecoverable/permanent state change:*
209```
210CONFIG_SECURE_BOOT_ALLOW_JTAG=1
211CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=1
212
213# Options for enabling eFuse emulation in Flash
214CONFIG_EFUSE_VIRTUAL=1
215CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH=1
216```
217
218---
219
Almir Okatob365e232022-03-08 01:35:54 -0300220---
221:warning: ***ATTENTION***
222
223*You can disable UART Download Mode by adding the following configuration:*
224```
225CONFIG_SECURE_DISABLE_ROM_DL_MODE=1
226```
227
228*This may be suitable for **production** builds. **After disabling UART Download Mode you will not be able to flash other images through UART.***
229
230*Otherwise, you can switch the UART ROM Download Mode to the Secure Download Mode. It will limit the use of Download Mode functions to simple flash read, write and erase operations.*
231```
232CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE=1
233```
234
235*Once the device makes its first full boot, these configurations cannot be reverted*
236
237---
238
Almir Okato39eb63d2022-01-05 18:31:54 -0300239Once the **bootloader image** is built, the resulting binary file is required to be signed with `espsecure.py` tool.
240
241First create a signing key:
242```
243espsecure.py generate_signing_key --version 2 <BOOTLOADER_SIGNING_KEY.pem>
244```
245
246Then sign the bootloader image:
247```
248espsecure.py sign_data --version 2 --keyfile <BOOTLOADER_SIGNING_KEY.pem> -o <BOOTLOADER_BIN_OUT> <BOOTLOADER_BIN_IN>
249```
250
251---
252:warning: ***ATTENTION***
253
254*Once the bootloader is flashed and the device resets, the **first boot will enable Secure Boot** and the bootloader and key **no longer can be modified**. So **ENSURE** that both bootloader and key are correct and you did not forget anything before flashing.*
255
256---
257
258Flash the bootloader as following, with `--after no_reset` flag, so you can reset the device only when assured:
259```
260esptool.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>
261```
262
263### [Secure Boot Process](#secure-boot-process)
264
265Secure boot uses a signature block appended to the bootloader image in order to verify the authenticity. The signature block contains the RSA-3072 signature of that image and the RSA-3072 public key.
266
267On its **first boot** the Secure Boot is not enabled on the device eFuses yet, neither the key nor digests. So the first boot will have the following process:
268
2691. On startup, since it is the first boot, the ROM bootloader will not verify the bootloader image (the Secure Boot bit in the eFuse is disabled) yet, so it proceeds to execute it (our MCUboot bootloader port).
2702. Bootloader calculates the SHA-256 hash digest of the public key and writes the result to eFuse.
2713. Bootloader validates the application images and prepare the booting process (MCUboot phase).
2724. Bootloader burns eFuse to enable Secure Boot V2.
2735. Bootloader proceeds to load the Primary image.
274
275After that the Secure Boot feature is permanently enabled and on every next boot the ROM bootloader will verify the MCUboot bootloader image.
276The process of an usual boot:
277
2781. On startup, the ROM bootloader checks the Secure Boot enable bit in the eFuse. If it is enabled, the boot will proceed as following.
2792. ROM bootloader verifies the bootloader's signature block integrity (magic number and CRC). Interrupt boot if it fails.
2803. ROM bootloader verifies the bootloader image, interrupt boot if any step fails.: \
2813.1. Compare the SHA-256 hash digest of the public key embedded in the bootloader’s signature block with the digest saved in the eFuses. \
2823.2. Generate the application image digest and match it with the image digest in the signature block. \
2833.3. Use the public key to verify the signature of the bootloader image, using RSA-PSS with the image digest calculated from previous step for comparison.
2844. ROM bootloader executes the bootloader image.
2855. Bootloader does the usual verification (MCUboot phase).
2866. Proceeds to boot the Primary image.
287
288## [Flash Encryption](#flash-encryption)
289
290The Espressif Flash Encryption is hardware-assisted, transparent to the MCUboot process and is an additional security measure beyond MCUboot existent features.
291The Flash Encryption implementation is also based on [IDF](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/security/flash-encryption.html) and is intended for encrypting off-chip flash memory contents, so it is protected against physical reading.
292
293When enabling the Flash Encryption, the user can encrypt the content either using a **device generated key** (remains unknown and unreadable) or a **host generated key** (owner is responsible for keeping the key private and safe). After the flash encryption gets enabled through eFuse burning on the device, all read and write operations are decrypted/encrypted in runtime.
294
295### [Building bootloader with Flash Encryption](#building-bootloader-with-flash-encryption)
296
297In order to build the bootloader with the feature on, the following configurations must be enabled:
298
299For **release mode**:
300```
301CONFIG_SECURE_FLASH_ENC_ENABLED=1
302CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE=1
303```
304
305For **development mode**:
306```
307CONFIG_SECURE_FLASH_ENC_ENABLED=1
308CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT=1
309```
Almir Okatob365e232022-03-08 01:35:54 -0300310
311---
Almir Okato39eb63d2022-01-05 18:31:54 -0300312:warning: ***ATTENTION***
313
314*On development phase is strongly recommended adding the following configuration in order to keep the debugging enabled and also to avoid any unrecoverable/permanent state change:*
315```
316CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC=1
317CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_DEC=1
318CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=1
319CONFIG_SECURE_BOOT_ALLOW_JTAG=1
320
321# Options for enabling eFuse emulation in Flash
322CONFIG_EFUSE_VIRTUAL=1
323CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH=1
324```
Almir Okatob365e232022-03-08 01:35:54 -0300325---
Almir Okato39eb63d2022-01-05 18:31:54 -0300326
327---
328:warning: ***ATTENTION***
329
330*Unless the recommended flags for **DEVELOPMENT MODE** were enabled, the actions made by Flash Encryption process are **PERMANENT**.* \
331*Once the bootloader is flashed and the device resets, the **first boot will enable Flash Encryption, encrypt the flash content including bootloader and image slots, burn the eFuses that no longer can be modified** and if device generated the key **it will not be recoverable**.* \
332*When on **RELEASE MODE**, **ENSURE** that the application with an update agent is flashed before reset the device.*
333
Almir Okatob365e232022-03-08 01:35:54 -0300334*In the same way as Secure Boot feature, you can disable UART Download Mode by adding the following configuration:*
335```
336CONFIG_SECURE_DISABLE_ROM_DL_MODE=1
337```
338
339*This may be suitable for **production** builds. **After disabling UART Download Mode you will not be able to flash other images through UART.***
340
341*Otherwise, you can switch the UART Download Mode to the Secure Download Mode. It will limit the use of Download Mode functions to simple flash read, write and erase operations.*
342```
343CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE=1
344```
345
346*These configurations cannot be reverted after the device's first boot*
347
Almir Okato39eb63d2022-01-05 18:31:54 -0300348---
349
Sylvio Alvesd5230942022-01-20 21:35:53 -0300350### [Signing the image when working with Flash Encryption](#signing-the-image-when-working-with-flash-encryption)
351
352When enabling flash encryption, it is required to signed the image using 32-byte alignment: `--align 32 --max-align 32`.
353
354Command example:
355```
356imgtool.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>
357```
358
Almir Okato39eb63d2022-01-05 18:31:54 -0300359### [Device generated key](#device-generated-key)
360
361First ensure that the application image is able to perform encrypted read and write operations to the SPI Flash.
362Flash the bootloader and application normally:
363
364```
365esptool.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>
366```
367```
368esptool.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>
369```
370
371On the **first boot**, the bootloader will:
3721. Generate Flash Encryption key and write to eFuse.
3732. Encrypt flash in-place including bootloader, image primary/secondary slot and scratch.
3743. Burn eFuse to enable Flash Encryption.
3754. Reset system to ensure Flash Encryption cache resets properly.
376
377### [Host generated key](#host-generated-key)
378
Almir Okatob365e232022-03-08 01:35:54 -0300379First ensure that the application image is able to perform encrypted read and write operations to the SPI Flash. Also ensure that the **UART ROM Download Mode is not disabled** - or that the **Secure Download Mode is enabled**.
Almir Okato39eb63d2022-01-05 18:31:54 -0300380Before flashing, generate the encryption key using `espsecure.py` tool:
381```
382espsecure.py generate_flash_encryption_key <FLASH_ENCRYPTION_KEY.bin>
383```
384
Almir Okatob365e232022-03-08 01:35:54 -0300385Burn the key into the device's eFuse (keep a copy on the host), this action can be done **only once**:
Almir Okato39eb63d2022-01-05 18:31:54 -0300386
387---
388:warning: ***ATTENTION***
389
Almir Okatob365e232022-03-08 01:35:54 -0300390*eFuse emulation in Flash configuration options do not have any effect, so if the key burning command below is used, it will actually burn the physical eFuse.*
Almir Okato39eb63d2022-01-05 18:31:54 -0300391
392---
393
394- ESP32
395```
396espefuse.py --port PORT burn_key flash_encryption <FLASH_ENCRYPTION_KEY.bin>
397```
398
Sylvio Alvesd8eff812022-01-31 23:16:06 -0300399- ESP32S2, ESP32C3 and ESP32S3
Almir Okato39eb63d2022-01-05 18:31:54 -0300400```
401espefuse.py --port PORT burn_key BLOCK <FLASH_ENCRYPTION_KEY.bin> <KEYPURPOSE>
402```
403
404BLOCK is a free keyblock between BLOCK_KEY0 and BLOCK_KEY5. And KEYPURPOSE is either XTS_AES_128_KEY, XTS_AES_256_KEY_1, XTS_AES_256_KEY_2 (AES XTS 256 is available only in ESP32S2).
405
406Now, similar as the Device generated key, the bootloader and application can be flashed plaintext. The **first boot** will encrypt the flash content using the host key burned in the eFuse instead of generate a new one.
407
408Flashing the bootloader and application:
409
410```
411esptool.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>
412```
413```
414esptool.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>
415```
416
417On the **first boot**, the bootloader will:
4181. Encrypt flash in-place including bootloader, image primary/secondary slot and scratch using the written key.
4192. Burn eFuse to enable Flash Encryption.
4203. Reset system to ensure Flash Encryption cache resets properly.
421
Almir Okatob365e232022-03-08 01:35:54 -0300422Encrypting data on the host:
423- ESP32
424```
425espsecure.py encrypt_flash_data --keyfile <FLASH_ENCRYPTION_KEY.bin> --address <FLASH_OFFSET> --output <OUTPUT_DATA> <INPUT_DATA>
426```
427
428- ESP32-S2, ESP32-C3 and ESP32-S3
429```
430espsecure.py encrypt_flash_data --aes_xts --keyfile <FLASH_ENCRYPTION_KEY.bin> --address <FLASH_OFFSET> --output <OUTPUT_DATA> <INPUT_DATA>
431```
432
433---
434***Note***
435OTA updates are required to be sent plaintext. The reason is that, as said before, after the Flash Encryption is enabled all read/write operations are decrypted/encrypted in runtime, so as e.g. if pre-encrypted data is sent for an OTA update, it would be wrongly double-encrypted when the update agent writes to the flash.
436
437For updating with an image encrypted on the host, flash it through serial using `esptool.py` as above. **UART ROM Download Mode must not be disabled**.
438
439---
440
Almir Okato39eb63d2022-01-05 18:31:54 -0300441## [Security Chain scheme](#security-chain-scheme)
442
443Using the 3 features, Secure Boot, Image signature verification and Flash Encryption, a Security Chain can be established so only trusted code is executed, and also the code and content residing in the off-chip flash are protected against undesirable reading.
444
445The overall final process when all features are enabled:
4461. ROM bootloader validates the MCUboot bootloader using RSA signature verification.
4472. MCUboot bootloader validates the image using the chosen algorithm EC256/RSA/ED25519. It also validates an upcoming image when updating.
4483. Flash Encryption guarantees that code and data are not exposed.
449
450### [Size Limitation](#size-limitation)
451
Almir Okatoa1d641d2022-02-21 19:31:46 -0300452When all 3 features are enable at same time, the bootloader size may exceed the fixed limit for the ROM bootloader checking on the Espressif chips **depending on which algorithm** was chosen for MCUboot image signing. The issue https://github.com/mcu-tools/mcuboot/issues/1262 was created to track this limitation.
453
454## [Multi image](#multi-image)
455
456The multi image feature (currently limited to 2 images) allows the images to be updated separately (each one has its own primary and secondary slot) by MCUboot.
457
458The Espressif port bootloader handles the boot in two different approaches:
459
460### [Host OS boots second image](#host-os-boots-second-image)
461
462Host OS from the *first image* is responsible for booting the *second image*, therefore the bootloader is aware of the second image regions and can update it, however it does not load neither boots it.
463
464Configuration example (`bootloader.conf`):
465```
466CONFIG_ESP_BOOTLOADER_SIZE=0xF000
467CONFIG_ESP_MCUBOOT_WDT_ENABLE=y
468
469# Enables multi image, if it is not defined, its assumed
470# only one updatable image
471CONFIG_ESP_IMAGE_NUMBER=2
472
473# Example of values to be used when multi image is enabled
474# Notice that the OS layer and update agent must be aware
475# of these regions
476CONFIG_ESP_APPLICATION_SIZE=0x50000
477CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS=0x10000
478CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS=0x60000
479CONFIG_ESP_IMAGE1_PRIMARY_START_ADDRESS=0xB0000
480CONFIG_ESP_IMAGE1_SECONDARY_START_ADDRESS=0x100000
481CONFIG_ESP_SCRATCH_OFFSET=0x150000
482CONFIG_ESP_SCRATCH_SIZE=0x40000
483```
484
485### [Multi boot](#multi-boot)
486
487In the multi boot approach the bootloader is responsible for booting two different images in two different CPUs, firstly the *second image* on the APP CPU and then the *first image* on the PRO CPU (current CPU), it is also responsible for update both images as well. Thus multi boot will be only supported by Espressif multi core chips - currently only ESP32 is implemented.
488
489---
490***Note***
491
492*The host OSes in each CPU must handle how the resources are divided/controlled between then.*
493
494---
495
496Configuration example:
497```
498CONFIG_ESP_BOOTLOADER_SIZE=0xF000
499CONFIG_ESP_MCUBOOT_WDT_ENABLE=y
500
501# Enables multi image, if it is not defined, its assumed
502# only one updatable image
503CONFIG_ESP_IMAGE_NUMBER=2
504
505# Enables multi image boot on independent processors
506# (main host OS is not responsible for booting the second image)
507# Use only with CONFIG_ESP_IMAGE_NUMBER=2
508CONFIG_ESP_MULTI_PROCESSOR_BOOT=y
509
510# Example of values to be used when multi image is enabled
511# Notice that the OS layer and update agent must be aware
512# of these regions
513CONFIG_ESP_APPLICATION_SIZE=0x50000
514CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS=0x10000
515CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS=0x60000
516CONFIG_ESP_IMAGE1_PRIMARY_START_ADDRESS=0xB0000
517CONFIG_ESP_IMAGE1_SECONDARY_START_ADDRESS=0x100000
518CONFIG_ESP_SCRATCH_OFFSET=0x150000
519CONFIG_ESP_SCRATCH_SIZE=0x40000
520```
521
522### [Image version dependency](#image-version-dependency)
523
524MCUboot allows version dependency check between the images when updating them. As `imgtool.py` allows a version assigment when signing an image, it is also possible to add the version dependency constraint:
525
526```
527imgtool.py sign --align 4 -v <VERSION> -d "(<IMAGE_INDEX>, <VERSION_DEPENDENCY>)" -H 32 --pad-header -S <SLOT_SIZE> <BIN_IN> <SIGNED_BIN>
528```
529
530- `<VERSION>` defines the version of the image being signed.
531- `"(<IMAGE_INDEX>, <VERSION_DEPENDENCY>)"` defines the minimum version and from which image is needed to satisfy the dependency.
532
533---
534Example:
535```
536imgtool.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
537```
538
539Supposing that the image 0 is being signed, its version is 1.0.0 and it depends on image 1 with version at least 0.0.1+0.
540
541---