15
15
#include "esp_efuse_table.h"
16
16
#include "esp_log.h"
17
17
#include "hal/wdt_hal.h"
18
+ #include "soc/soc_caps.h"
18
19
19
20
#include "esp_mcuboot_image.h"
20
21
@@ -167,6 +168,26 @@ static esp_err_t initialise_flash_encryption(void)
167
168
return err ;
168
169
}
169
170
171
+ #if defined(SOC_SUPPORTS_SECURE_DL_MODE ) && defined(CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE )
172
+ ESP_LOGI (TAG , "Enabling Secure Download mode..." );
173
+ err = esp_efuse_enable_rom_secure_download_mode ();
174
+ if (err != ESP_OK ) {
175
+ ESP_LOGE (TAG , "Could not enable Secure Download mode..." );
176
+ esp_efuse_batch_write_cancel ();
177
+ return err ;
178
+ }
179
+ #elif CONFIG_SECURE_DISABLE_ROM_DL_MODE
180
+ ESP_LOGI (TAG , "Disable ROM Download mode..." );
181
+ err = esp_efuse_disable_rom_download_mode ();
182
+ if (err != ESP_OK ) {
183
+ ESP_LOGE (TAG , "Could not disable ROM Download mode..." );
184
+ esp_efuse_batch_write_cancel ();
185
+ return err ;
186
+ }
187
+ #else
188
+ ESP_LOGW (TAG , "UART ROM Download mode kept enabled - SECURITY COMPROMISED" );
189
+ #endif
190
+
170
191
err = esp_efuse_batch_write_commit ();
171
192
if (err != ESP_OK ) {
172
193
ESP_LOGE (TAG , "Error programming security eFuses (err=0x%x)." , err );
@@ -213,7 +234,7 @@ static esp_err_t encrypt_flash_contents(uint32_t flash_crypt_cnt, bool flash_cry
213
234
* This will need changes when implementing multi-slot support
214
235
*/
215
236
ESP_LOGI (TAG , "Encrypting remaining flash..." );
216
- uint32_t region_addr = CONFIG_ESP_APPLICATION_SECONDARY_START_ADDRESS ;
237
+ uint32_t region_addr = CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS ;
217
238
size_t region_size = CONFIG_ESP_APPLICATION_SIZE ;
218
239
err = esp_flash_encrypt_region (region_addr , region_size );
219
240
if (err != ESP_OK ) {
@@ -226,6 +247,21 @@ static esp_err_t encrypt_flash_contents(uint32_t flash_crypt_cnt, bool flash_cry
226
247
return err ;
227
248
}
228
249
250
+ #if defined(CONFIG_ESP_IMAGE_NUMBER ) && (CONFIG_ESP_IMAGE_NUMBER == 2 )
251
+ region_addr = CONFIG_ESP_IMAGE1_PRIMARY_START_ADDRESS ;
252
+ region_size = CONFIG_ESP_APPLICATION_SIZE ;
253
+ err = esp_flash_encrypt_region (region_addr , region_size );
254
+ if (err != ESP_OK ) {
255
+ return err ;
256
+ }
257
+ region_addr = CONFIG_ESP_IMAGE1_SECONDARY_START_ADDRESS ;
258
+ region_size = CONFIG_ESP_APPLICATION_SIZE ;
259
+ err = esp_flash_encrypt_region (region_addr , region_size );
260
+ if (err != ESP_OK ) {
261
+ return err ;
262
+ }
263
+ #endif
264
+
229
265
#ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
230
266
// Go straight to max, permanently enabled
231
267
ESP_LOGI (TAG , "Setting CRYPT_CNT for permanent encryption" );
@@ -288,20 +324,20 @@ static esp_err_t encrypt_primary_slot(void)
288
324
/* Check if the slot is plaintext or encrypted, 0x20 offset is for skipping
289
325
* MCUboot header
290
326
*/
291
- err = bootloader_flash_read (CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS + 0x20 ,
327
+ err = bootloader_flash_read (CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS + 0x20 ,
292
328
& img_header , sizeof (esp_image_load_header_t ), true);
293
329
if (err != ESP_OK ) {
294
330
ESP_LOGE (TAG , "Failed to read slot img header" );
295
331
return err ;
296
332
} else {
297
- err = verify_img_header (CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS ,
333
+ err = verify_img_header (CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS ,
298
334
& img_header , true);
299
335
}
300
336
301
337
if (err == ESP_OK ) {
302
338
ESP_LOGI (TAG , "Encrypting primary slot..." );
303
339
304
- err = esp_flash_encrypt_region (CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS ,
340
+ err = esp_flash_encrypt_region (CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS ,
305
341
CONFIG_ESP_APPLICATION_SIZE );
306
342
if (err != ESP_OK ) {
307
343
ESP_LOGE (TAG , "Failed to encrypt slot in place: 0x%x" , err );
0 commit comments