blob: 758802250ed47342cf811f02e5557f1bb189b5c1 [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
Almir Okatofa173df2022-04-19 01:10:30 -030046The 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 `port/<TARGET>/bootloader.conf` file or passing a custom config file using the `-DMCUBOOT_CONFIG_FILE` argument on the first step below.
Almir Okato428e2e72021-08-11 10:52:10 -030047
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
Almir Okatofa173df2022-04-19 01:10:30 -0300551. Compile and generate the BIN:
Almir Okato428e2e72021-08-11 10:52:10 -030056
57```
Almir Okatofa173df2022-04-19 01:10:30 -030058cmake -DCMAKE_TOOLCHAIN_FILE=tools/toolchain-<TARGET>.cmake -DMCUBOOT_TARGET=<TARGET> -DMCUBOOT_FLASH_PORT=<PORT> -B build -GNinja
Almir Okatoe8cbc0d2022-06-13 10:45:39 -030059ninja -C build/
Almir Okato428e2e72021-08-11 10:52:10 -030060```
61
Almir Okatofa173df2022-04-19 01:10:30 -0300622. Flash MCUboot in your device:
Almir Okato428e2e72021-08-11 10:52:10 -030063
64```
Almir Okatofa173df2022-04-19 01:10:30 -030065ninja -C build/ flash
Almir Okato428e2e72021-08-11 10:52:10 -030066```
67
Almir Okatofa173df2022-04-19 01:10:30 -030068If `MCUBOOT_FLASH_PORT` arg was not passed to `cmake`, the default `PORT` for flashing will be `/dev/ttyUSB0`.
69
70Alternatively:
Almir Okato428e2e72021-08-11 10:52:10 -030071
72```
Almir Okatofa173df2022-04-19 01:10:30 -030073esptool.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
Almir Okato428e2e72021-08-11 10:52:10 -030074```
Sylvio Alvesd5230942022-01-20 21:35:53 -030075---
Almir Okatob365e232022-03-08 01:35:54 -030076***Note***
Almir Okatoa1d641d2022-02-21 19:31:46 -030077
78You 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 -030079You can also skip `<PORT>` and `<BAUD>` parameters so that esptool tries to automatically detect it.
Almir Okato428e2e72021-08-11 10:52:10 -030080
Sylvio Alvesd5230942022-01-20 21:35:53 -030081*`<FLASH_SIZE>` can be found using the command below:*
82```
83esptool.py -p <PORT> -b <BAUD> flash_id
84```
Almir Okatob365e232022-03-08 01:35:54 -030085The output contains device information and its flash size:
Sylvio Alvesd5230942022-01-20 21:35:53 -030086```
87Detected flash size: 4MB
88```
89
90
91*`<BOOTLOADER_FLASH_OFFSET>` value must follow one of the addresses below:*
Almir Okatob365e232022-03-08 01:35:54 -030092
Sylvio Alvesd8eff812022-01-31 23:16:06 -030093| ESP32 | ESP32-S2 | ESP32-C3 | ESP32-S3 |
94| :-----: | :-----: | :-----: | :-----: |
95| 0x1000 | 0x1000 | 0x0000 | 0x0000 |
Sylvio Alvesd5230942022-01-20 21:35:53 -030096
97---
Almir Okato39eb63d2022-01-05 18:31:54 -030098
Almir Okatofa173df2022-04-19 01:10:30 -0300993. Reset your device
100
Almir Okato39eb63d2022-01-05 18:31:54 -0300101## [Signing and flashing an application](#signing-and-flashing-an-application)
102
1031. Images can be regularly signed with the `scripts/imgtool.py` script:
104
105```
Sylvio Alvesd5230942022-01-20 21:35:53 -0300106imgtool.py sign --align 4 -v 0 -H 32 --pad-header -S <SLOT_SIZE> <BIN_IN> <SIGNED_BIN>
Almir Okato39eb63d2022-01-05 18:31:54 -0300107```
108
Sylvio Alvesd5230942022-01-20 21:35:53 -0300109---
110
Almir Okatob365e232022-03-08 01:35:54 -0300111***Note***
Almir Okatoa1d641d2022-02-21 19:31:46 -0300112
Almir Okatob365e232022-03-08 01:35:54 -0300113`<SLOT_SIZE>` is the size of the slot to be used.
Sylvio Alvesd5230942022-01-20 21:35:53 -0300114Default slot0 size is `0x100000`, but it can change as per application flash partitions.
115
116For Zephyr images, `--pad-header` is not needed as it already has the padding for MCUboot header.
Almir Okato39eb63d2022-01-05 18:31:54 -0300117
118---
Sylvio Alvesd5230942022-01-20 21:35:53 -0300119
Almir Okato39eb63d2022-01-05 18:31:54 -0300120:warning: ***ATTENTION***
121
122*This is the basic signing needed for adding MCUboot headers and trailers.
123For 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.*
124
125---
126
1272. Flash the signed application:
128
129```
130esptool.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>
131```
132
133# [Security Chain on Espressif port](#security-chain-on-espressif-port)
134
135[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.
136Espressif 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.
137
138## [MCUboot image signature verification](#mcuboot-image-signature-verification)
139
140The 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:
141
142---
143***Note***
Almir Okatoa1d641d2022-02-21 19:31:46 -0300144
Almir Okato39eb63d2022-01-05 18:31:54 -0300145*It is strongly recommended to generate a new signing key using `imgtool` instead of use the existent samples.*
146
147---
148
149#### For EC256 algorithm use
150```
151CONFIG_ESP_SIGN_EC256=y
152
153# Use Tinycrypt lib for EC256 or ED25519 signing
154CONFIG_ESP_USE_TINYCRYPT=y
155
156CONFIG_ESP_SIGN_KEY_FILE=<YOUR_SIGNING_KEY.pem>
157```
158
159#### For ED25519 algorithm use
160```
161CONFIG_ESP_SIGN_ED25519=y
162
163# Use Tinycrypt lib for EC256 or ED25519 signing
164CONFIG_ESP_USE_TINYCRYPT=y
165
166CONFIG_ESP_SIGN_KEY_FILE=<YOUR_SIGNING_KEY.pem>
167```
168
169#### For RSA (2048 or 3072) algorithm use
170```
171CONFIG_ESP_SIGN_RSA=y
172# RSA_LEN is 2048 or 3072
173CONFIG_ESP_SIGN_RSA_LEN=<RSA_LEN>
174
175# Use Mbed TLS lib for RSA image signing
176CONFIG_ESP_USE_MBEDTLS=y
177
178CONFIG_ESP_SIGN_KEY_FILE=<YOUR_SIGNING_KEY.pem>
179```
180
181Notice that the public key will be embedded in the bootloader code, since the hardware key storage is not supported by Espressif port.
182
183### [Signing the image](#signing-the-image)
184
185Now you need to sign the **image binary**, use the `imgtool` with `-k` parameter:
186
187```
188imgtool.py sign -k <YOUR_SIGNING_KEY.pem> --pad --pad-sig --align 4 -v 0 -H 32 --pad-header -S 0x00100000 <BIN_IN> <BIN_OUT>
189```
190If signing a Zephyr image, the `--pad-header` is not needed, as it already have the padding for MCUboot header.
191
192
193## [Secure Boot](#secure-boot)
194
195The 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. \
196***Note***: ROM bootloader is the First Stage Bootloader, while the Espressif MCUboot port is the Second Stage Bootloader.
197
198### [Building bootloader with Secure Boot](#building-bootloader-with-secure-boot)
199
200In order to build the bootloader with the feature on, the following configurations must be enabled:
201```
202CONFIG_SECURE_BOOT=1
203CONFIG_SECURE_BOOT_V2_ENABLED=1
204CONFIG_SECURE_SIGNED_ON_BOOT=1
205CONFIG_SECURE_SIGNED_APPS_RSA_SCHEME=1
206CONFIG_SECURE_BOOT_SUPPORTS_RSA=1
207```
208
209---
210:warning: ***ATTENTION***
211
212*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:*
213```
214CONFIG_SECURE_BOOT_ALLOW_JTAG=1
215CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=1
216
217# Options for enabling eFuse emulation in Flash
218CONFIG_EFUSE_VIRTUAL=1
219CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH=1
220```
221
222---
223
Almir Okatob365e232022-03-08 01:35:54 -0300224---
225:warning: ***ATTENTION***
226
227*You can disable UART Download Mode by adding the following configuration:*
228```
229CONFIG_SECURE_DISABLE_ROM_DL_MODE=1
230```
231
232*This may be suitable for **production** builds. **After disabling UART Download Mode you will not be able to flash other images through UART.***
233
234*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.*
235```
236CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE=1
237```
238
239*Once the device makes its first full boot, these configurations cannot be reverted*
240
241---
242
Almir Okato39eb63d2022-01-05 18:31:54 -0300243Once the **bootloader image** is built, the resulting binary file is required to be signed with `espsecure.py` tool.
244
245First create a signing key:
246```
247espsecure.py generate_signing_key --version 2 <BOOTLOADER_SIGNING_KEY.pem>
248```
249
250Then sign the bootloader image:
251```
252espsecure.py sign_data --version 2 --keyfile <BOOTLOADER_SIGNING_KEY.pem> -o <BOOTLOADER_BIN_OUT> <BOOTLOADER_BIN_IN>
253```
254
255---
256:warning: ***ATTENTION***
257
258*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.*
259
260---
261
262Flash the bootloader as following, with `--after no_reset` flag, so you can reset the device only when assured:
263```
264esptool.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>
265```
266
267### [Secure Boot Process](#secure-boot-process)
268
269Secure 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.
270
271On 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:
272
2731. 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).
2742. Bootloader calculates the SHA-256 hash digest of the public key and writes the result to eFuse.
2753. Bootloader validates the application images and prepare the booting process (MCUboot phase).
2764. Bootloader burns eFuse to enable Secure Boot V2.
2775. Bootloader proceeds to load the Primary image.
278
279After that the Secure Boot feature is permanently enabled and on every next boot the ROM bootloader will verify the MCUboot bootloader image.
280The process of an usual boot:
281
2821. On startup, the ROM bootloader checks the Secure Boot enable bit in the eFuse. If it is enabled, the boot will proceed as following.
2832. ROM bootloader verifies the bootloader's signature block integrity (magic number and CRC). Interrupt boot if it fails.
2843. ROM bootloader verifies the bootloader image, interrupt boot if any step fails.: \
2853.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. \
2863.2. Generate the application image digest and match it with the image digest in the signature block. \
2873.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.
2884. ROM bootloader executes the bootloader image.
2895. Bootloader does the usual verification (MCUboot phase).
2906. Proceeds to boot the Primary image.
291
292## [Flash Encryption](#flash-encryption)
293
294The Espressif Flash Encryption is hardware-assisted, transparent to the MCUboot process and is an additional security measure beyond MCUboot existent features.
295The 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.
296
297When 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.
298
299### [Building bootloader with Flash Encryption](#building-bootloader-with-flash-encryption)
300
301In order to build the bootloader with the feature on, the following configurations must be enabled:
302
303For **release mode**:
304```
305CONFIG_SECURE_FLASH_ENC_ENABLED=1
306CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE=1
307```
308
309For **development mode**:
310```
311CONFIG_SECURE_FLASH_ENC_ENABLED=1
312CONFIG_SECURE_FLASH_ENCRYPTION_MODE_DEVELOPMENT=1
313```
Almir Okatob365e232022-03-08 01:35:54 -0300314
315---
Almir Okato39eb63d2022-01-05 18:31:54 -0300316:warning: ***ATTENTION***
317
318*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:*
319```
320CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC=1
321CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_DEC=1
322CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE=1
323CONFIG_SECURE_BOOT_ALLOW_JTAG=1
324
325# Options for enabling eFuse emulation in Flash
326CONFIG_EFUSE_VIRTUAL=1
327CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH=1
328```
Almir Okatob365e232022-03-08 01:35:54 -0300329---
Almir Okato39eb63d2022-01-05 18:31:54 -0300330
331---
332:warning: ***ATTENTION***
333
334*Unless the recommended flags for **DEVELOPMENT MODE** were enabled, the actions made by Flash Encryption process are **PERMANENT**.* \
335*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**.* \
336*When on **RELEASE MODE**, **ENSURE** that the application with an update agent is flashed before reset the device.*
337
Almir Okatob365e232022-03-08 01:35:54 -0300338*In the same way as Secure Boot feature, you can disable UART Download Mode by adding the following configuration:*
339```
340CONFIG_SECURE_DISABLE_ROM_DL_MODE=1
341```
342
343*This may be suitable for **production** builds. **After disabling UART Download Mode you will not be able to flash other images through UART.***
344
345*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.*
346```
347CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE=1
348```
349
350*These configurations cannot be reverted after the device's first boot*
351
Almir Okato39eb63d2022-01-05 18:31:54 -0300352---
353
Sylvio Alvesd5230942022-01-20 21:35:53 -0300354### [Signing the image when working with Flash Encryption](#signing-the-image-when-working-with-flash-encryption)
355
356When enabling flash encryption, it is required to signed the image using 32-byte alignment: `--align 32 --max-align 32`.
357
358Command example:
359```
360imgtool.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>
361```
362
Almir Okato39eb63d2022-01-05 18:31:54 -0300363### [Device generated key](#device-generated-key)
364
365First ensure that the application image is able to perform encrypted read and write operations to the SPI Flash.
366Flash the bootloader and application normally:
367
368```
369esptool.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>
370```
371```
372esptool.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>
373```
374
375On the **first boot**, the bootloader will:
3761. Generate Flash Encryption key and write to eFuse.
3772. Encrypt flash in-place including bootloader, image primary/secondary slot and scratch.
3783. Burn eFuse to enable Flash Encryption.
3794. Reset system to ensure Flash Encryption cache resets properly.
380
381### [Host generated key](#host-generated-key)
382
Almir Okatob365e232022-03-08 01:35:54 -0300383First 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 -0300384Before flashing, generate the encryption key using `espsecure.py` tool:
385```
386espsecure.py generate_flash_encryption_key <FLASH_ENCRYPTION_KEY.bin>
387```
388
Almir Okatob365e232022-03-08 01:35:54 -0300389Burn 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 -0300390
391---
392:warning: ***ATTENTION***
393
Almir Okatob365e232022-03-08 01:35:54 -0300394*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 -0300395
396---
397
398- ESP32
399```
400espefuse.py --port PORT burn_key flash_encryption <FLASH_ENCRYPTION_KEY.bin>
401```
402
Sylvio Alvesd8eff812022-01-31 23:16:06 -0300403- ESP32S2, ESP32C3 and ESP32S3
Almir Okato39eb63d2022-01-05 18:31:54 -0300404```
405espefuse.py --port PORT burn_key BLOCK <FLASH_ENCRYPTION_KEY.bin> <KEYPURPOSE>
406```
407
408BLOCK 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).
409
410Now, 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.
411
412Flashing the bootloader and application:
413
414```
415esptool.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>
416```
417```
418esptool.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>
419```
420
421On the **first boot**, the bootloader will:
4221. Encrypt flash in-place including bootloader, image primary/secondary slot and scratch using the written key.
4232. Burn eFuse to enable Flash Encryption.
4243. Reset system to ensure Flash Encryption cache resets properly.
425
Almir Okatob365e232022-03-08 01:35:54 -0300426Encrypting data on the host:
427- ESP32
428```
429espsecure.py encrypt_flash_data --keyfile <FLASH_ENCRYPTION_KEY.bin> --address <FLASH_OFFSET> --output <OUTPUT_DATA> <INPUT_DATA>
430```
431
432- ESP32-S2, ESP32-C3 and ESP32-S3
433```
434espsecure.py encrypt_flash_data --aes_xts --keyfile <FLASH_ENCRYPTION_KEY.bin> --address <FLASH_OFFSET> --output <OUTPUT_DATA> <INPUT_DATA>
435```
436
437---
438***Note***
Almir Okatoe8cbc0d2022-06-13 10:45:39 -0300439
Almir Okatob365e232022-03-08 01:35:54 -0300440OTA 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.
441
442For 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**.
443
444---
445
Almir Okato39eb63d2022-01-05 18:31:54 -0300446## [Security Chain scheme](#security-chain-scheme)
447
448Using 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.
449
450The overall final process when all features are enabled:
4511. ROM bootloader validates the MCUboot bootloader using RSA signature verification.
4522. MCUboot bootloader validates the image using the chosen algorithm EC256/RSA/ED25519. It also validates an upcoming image when updating.
4533. Flash Encryption guarantees that code and data are not exposed.
454
455### [Size Limitation](#size-limitation)
456
Almir Okatoa1d641d2022-02-21 19:31:46 -0300457When 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.
458
459## [Multi image](#multi-image)
460
461The 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.
462
463The Espressif port bootloader handles the boot in two different approaches:
464
465### [Host OS boots second image](#host-os-boots-second-image)
466
467Host 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.
468
469Configuration example (`bootloader.conf`):
470```
471CONFIG_ESP_BOOTLOADER_SIZE=0xF000
472CONFIG_ESP_MCUBOOT_WDT_ENABLE=y
473
474# Enables multi image, if it is not defined, its assumed
475# only one updatable image
476CONFIG_ESP_IMAGE_NUMBER=2
477
478# Example of values to be used when multi image is enabled
479# Notice that the OS layer and update agent must be aware
480# of these regions
481CONFIG_ESP_APPLICATION_SIZE=0x50000
482CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS=0x10000
483CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS=0x60000
484CONFIG_ESP_IMAGE1_PRIMARY_START_ADDRESS=0xB0000
485CONFIG_ESP_IMAGE1_SECONDARY_START_ADDRESS=0x100000
486CONFIG_ESP_SCRATCH_OFFSET=0x150000
487CONFIG_ESP_SCRATCH_SIZE=0x40000
488```
489
490### [Multi boot](#multi-boot)
491
492In 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.
493
494---
495***Note***
496
497*The host OSes in each CPU must handle how the resources are divided/controlled between then.*
498
499---
500
501Configuration example:
502```
503CONFIG_ESP_BOOTLOADER_SIZE=0xF000
504CONFIG_ESP_MCUBOOT_WDT_ENABLE=y
505
506# Enables multi image, if it is not defined, its assumed
507# only one updatable image
508CONFIG_ESP_IMAGE_NUMBER=2
509
510# Enables multi image boot on independent processors
511# (main host OS is not responsible for booting the second image)
512# Use only with CONFIG_ESP_IMAGE_NUMBER=2
513CONFIG_ESP_MULTI_PROCESSOR_BOOT=y
514
515# Example of values to be used when multi image is enabled
516# Notice that the OS layer and update agent must be aware
517# of these regions
518CONFIG_ESP_APPLICATION_SIZE=0x50000
519CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS=0x10000
520CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS=0x60000
521CONFIG_ESP_IMAGE1_PRIMARY_START_ADDRESS=0xB0000
522CONFIG_ESP_IMAGE1_SECONDARY_START_ADDRESS=0x100000
523CONFIG_ESP_SCRATCH_OFFSET=0x150000
524CONFIG_ESP_SCRATCH_SIZE=0x40000
525```
526
527### [Image version dependency](#image-version-dependency)
528
529MCUboot 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:
530
531```
532imgtool.py sign --align 4 -v <VERSION> -d "(<IMAGE_INDEX>, <VERSION_DEPENDENCY>)" -H 32 --pad-header -S <SLOT_SIZE> <BIN_IN> <SIGNED_BIN>
533```
534
535- `<VERSION>` defines the version of the image being signed.
536- `"(<IMAGE_INDEX>, <VERSION_DEPENDENCY>)"` defines the minimum version and from which image is needed to satisfy the dependency.
537
538---
539Example:
540```
541imgtool.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
542```
543
544Supposing 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.
545
546---
Almir Okatoe8cbc0d2022-06-13 10:45:39 -0300547
548## [Serial recovery mode](#serial-recovery-mode)
549
550Serial recovery mode allows management through MCUMGR (more information and how to install it: https://github.com/apache/mynewt-mcumgr-cli) for communicating and uploading a firmware to the device.
551
552---
553***Note***
554
Almir Okato09cca382022-09-23 15:25:28 -0300555Supported on ESP32 and ESP32-C3.
Almir Okatoe8cbc0d2022-06-13 10:45:39 -0300556
557---
558
559Configuration example:
560```
561# Enables the MCUboot Serial Recovery, that allows the use of
562# MCUMGR to upload a firmware through the serial port
563CONFIG_ESP_MCUBOOT_SERIAL=y
564# GPIO used to boot on Serial Recovery
565CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT=32
566# GPIO input type (0 for Pull-down, 1 for Pull-up)
567CONFIG_ESP_SERIAL_BOOT_GPIO_INPUT_TYPE=0
568# GPIO signal value
569CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT_VAL=1
570# Delay time for identify the GPIO signal
571CONFIG_ESP_SERIAL_BOOT_DETECT_DELAY_S=5
572# UART port used for serial communication
573CONFIG_ESP_SERIAL_BOOT_UART_NUM=1
574# GPIO for Serial RX signal
575CONFIG_ESP_SERIAL_BOOT_GPIO_RX=25
576# GPIO for Serial TX signal
577CONFIG_ESP_SERIAL_BOOT_GPIO_TX=26
578```
579
580When enabled, the bootloader checks the if the GPIO `<CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT>` configured has the signal value `<CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT_VAL>` for approximately `<CONFIG_ESP_SERIAL_BOOT_DETECT_DELAY_S>` seconds for entering the Serial recovery mode. Example: a button configured on GPIO 32 pressed for 5 seconds.
581
582Serial mode then uses the UART port configured for communication (`<CONFIG_ESP_SERIAL_BOOT_UART_NUM>`, pins `<CONFIG_ESP_SERIAL_BOOT_GPIO_RX>`, `<CONFIG_ESP_SERIAL_BOOT_GPIO_RX>`).
583
Almir Okato09cca382022-09-23 15:25:28 -0300584### [Serial Recovery through USB JTAG Serial port](#serial-recovery-through-usb-jtag-serial-port)
585
586Some chips, like ESP32-C3, have an integrated USB JTAG Serial Controller that implements a serial port (CDC) that can also be used for handling MCUboot Serial Recovery.
587More information about the USB pins and hardware configuration on ESP32-C3: https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-guides/usb-serial-jtag-console.html.
588
589Configuration example:
590```
591# Use Serial through USB JTAG Serial port for Serial Recovery
592CONFIG_ESP_MCUBOOT_SERIAL_USB_SERIAL_JTAG=y
593# Use sector erasing (recommended) instead of entire image size
594# erasing when uploading through Serial Recovery
595CONFIG_ESP_MCUBOOT_ERASE_PROGRESSIVELY=y
596# GPIO used to boot on Serial Recovery
597CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT=5
598# GPIO input type (0 for Pull-down, 1 for Pull-up)
599CONFIG_ESP_SERIAL_BOOT_GPIO_INPUT_TYPE=0
600# GPIO signal value
601CONFIG_ESP_SERIAL_BOOT_GPIO_DETECT_VAL=1
602# Delay time for identify the GPIO signal
603CONFIG_ESP_SERIAL_BOOT_DETECT_DELAY_S=5
604```
605
Almir Okatoe8cbc0d2022-06-13 10:45:39 -0300606### [MCUMGR image upload example](#mcumgr-image-upload-example)
607
608After entering the Serial recovery mode on the device, MCUMGR can be used as following:
609
610Configure the connection:
611```
612mcumgr conn add esp type="serial" connstring="dev=<PORT>,baud=115200,mtu=256"
613```
614
615Upload the image (the process may take some time):
616```
617mcumgr -c esp image upload <IMAGE_BIN>
618```
619
620Reset the device:
621```
622mcumgr -c esp reset
623```
624
625---
626:warning: ***ATTENTION***
627
628*Serial recovery mode uploads the image to the PRIMARY_SLOT, therefore if the upload process gets interrupted the image may be corrupted and unable to boot*
629
630---