Skip to content

Commit f1dae0d

Browse files
sachin-billoreespressif-bot
authored andcommitted
ESP32 SecureBoot V2: eFuse write operations are updated to use the eFuse Manager APIs
Closes IDF-2034 Closes espressif/esp-idf#5771
1 parent 3eac0ec commit f1dae0d

File tree

4 files changed

+71
-22
lines changed

4 files changed

+71
-22
lines changed

components/bootloader_support/src/esp32/secure_boot.c

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "esp_secure_boot.h"
3535
#include "esp_flash_encrypt.h"
3636
#include "esp_efuse.h"
37+
#include "esp_efuse_table.h"
3738

3839
/* The following API implementations are used only when called
3940
* from the bootloader code.
@@ -290,16 +291,19 @@ static esp_err_t secure_boot_v2_digest_generate(uint32_t flash_offset, uint32_t
290291

291292
esp_err_t esp_secure_boot_v2_permanently_enable(const esp_image_metadata_t *image_data)
292293
{
293-
uint32_t new_wdata0 = 0;
294-
uint32_t new_wdata6 = 0;
295-
296294
ESP_LOGI(TAG, "enabling secure boot v2...");
297295
esp_err_t ret;
298296
if (esp_secure_boot_enabled()) {
299297
ESP_LOGI(TAG, "secure boot v2 is already enabled. Continuing..");
300298
return ESP_OK;
301299
}
302300

301+
ret = esp_efuse_batch_write_begin();
302+
if (ret != ESP_OK) {
303+
ESP_LOGE(TAG, "Error batch programming security eFuses.");
304+
return ret;
305+
}
306+
303307
uint32_t coding_scheme = REG_GET_FIELD(EFUSE_BLK0_RDATA6_REG, EFUSE_CODING_SCHEME);
304308
if (coding_scheme != EFUSE_CODING_SCHEME_VAL_NONE) {
305309
ESP_LOGE(TAG, "No coding schemes are supported in secure boot v2.(Detected scheme: 0x%x)", coding_scheme);
@@ -336,14 +340,19 @@ esp_err_t esp_secure_boot_v2_permanently_enable(const esp_image_metadata_t *imag
336340
}
337341

338342
ESP_LOGI(TAG, "Burning public key hash to efuse.");
339-
uint32_t *boot_public_key_digest_ptr = (uint32_t *) boot_pub_key_digest;
340-
for (int i = 0; i < 8 ; i++) {
341-
REG_WRITE(EFUSE_BLK2_WDATA0_REG + 4 * i, boot_public_key_digest_ptr[i]);
342-
ESP_LOGD(TAG, "EFUSE_BLKx_WDATA%d_REG = 0x%08x", i, boot_public_key_digest_ptr[i]);
343+
ret = esp_efuse_write_block(EFUSE_BLK2, boot_pub_key_digest, 0, (DIGEST_LEN * 8));
344+
if (ret != ESP_OK) {
345+
ESP_LOGE(TAG, "Writing public key hash to efuse failed.");
346+
return ret;
343347
}
344348

345349
ESP_LOGI(TAG, "Write protecting public key digest...");
346-
new_wdata0 |= EFUSE_WR_DIS_BLK2;
350+
ret = esp_efuse_set_write_protect(EFUSE_BLK2);
351+
if (ret != ESP_OK) {
352+
ESP_LOGE(TAG, "Write protecting public key digest...failed.");
353+
return ret;
354+
}
355+
347356
efuse_key_write_protected = true;
348357
efuse_key_read_protected = false;
349358
} else {
@@ -375,26 +384,38 @@ esp_err_t esp_secure_boot_v2_permanently_enable(const esp_image_metadata_t *imag
375384
ESP_LOGI(TAG, "blowing secure boot efuse...");
376385
ESP_LOGD(TAG, "before updating, EFUSE_BLK0_RDATA6 %x", REG_READ(EFUSE_BLK0_RDATA6_REG));
377386

378-
new_wdata6 |= EFUSE_RD_ABS_DONE_1;
387+
ret = esp_efuse_write_field_bit(ESP_EFUSE_ABS_DONE_1);
388+
if (ret != ESP_OK) {
389+
ESP_LOGE(TAG, "Blowing secure boot efuse...failed.");
390+
return ret;
391+
}
379392

380393
#ifndef CONFIG_SECURE_BOOT_ALLOW_JTAG
381394
ESP_LOGI(TAG, "Disable JTAG...");
382-
new_wdata6 |= EFUSE_RD_DISABLE_JTAG;
395+
ret = esp_efuse_write_field_bit(ESP_EFUSE_DISABLE_JTAG);
396+
if (ret != ESP_OK) {
397+
ESP_LOGE(TAG, "Disable JTAG...failed.");
398+
return ret;
399+
}
383400
#else
384401
ESP_LOGW(TAG, "Not disabling JTAG - SECURITY COMPROMISED");
385402
#endif
386403

387404
#ifndef CONFIG_SECURE_BOOT_ALLOW_ROM_BASIC
388405
ESP_LOGI(TAG, "Disable ROM BASIC interpreter fallback...");
389-
new_wdata6 |= EFUSE_RD_CONSOLE_DEBUG_DISABLE;
406+
ret = esp_efuse_write_field_bit(ESP_EFUSE_CONSOLE_DEBUG_DISABLE);
407+
if (ret != ESP_OK) {
408+
ESP_LOGE(TAG, "Disable ROM BASIC interpreter fallback...failed.");
409+
return ret;
410+
}
390411
#else
391412
ESP_LOGW(TAG, "Not disabling ROM BASIC fallback - SECURITY COMPROMISED");
392413
#endif
393414

394415
#ifdef CONFIG_SECURE_DISABLE_ROM_DL_MODE
395416
ESP_LOGI(TAG, "Disable ROM Download mode...");
396-
esp_err_t err = esp_efuse_disable_rom_download_mode();
397-
if (err != ESP_OK) {
417+
ret = esp_efuse_disable_rom_download_mode();
418+
if (ret != ESP_OK) {
398419
ESP_LOGE(TAG, "Could not disable ROM Download mode...");
399420
return ESP_FAIL;
400421
}
@@ -411,15 +432,21 @@ esp_err_t esp_secure_boot_v2_permanently_enable(const esp_image_metadata_t *imag
411432
#endif
412433
if (rd_dis_now) {
413434
ESP_LOGI(TAG, "Prevent read disabling of additional efuses...");
414-
new_wdata0 |= EFUSE_WR_DIS_RD_DIS;
435+
ret = esp_efuse_write_field_bit(ESP_EFUSE_WR_DIS_EFUSE_RD_DISABLE);
436+
if (ret != ESP_OK) {
437+
ESP_LOGE(TAG, "Prevent read disabling of additional efuses...failed.");
438+
return ret;
439+
}
415440
}
416441
#else
417442
ESP_LOGW(TAG, "Allowing read disabling of additional efuses - SECURITY COMPROMISED");
418443
#endif
419444

420-
REG_WRITE(EFUSE_BLK0_WDATA0_REG, new_wdata0);
421-
REG_WRITE(EFUSE_BLK0_WDATA6_REG, new_wdata6);
422-
esp_efuse_burn_new_values();
445+
ret = esp_efuse_batch_write_commit();
446+
if (ret != ESP_OK) {
447+
ESP_LOGE(TAG, "Error programming security eFuses.");
448+
return ret;
449+
}
423450
uint32_t after = REG_READ(EFUSE_BLK0_RDATA6_REG);
424451
ESP_LOGD(TAG, "after updating, EFUSE_BLK0_RDATA0 0x%08x EFUSE_BLK0_RDATA6 0x%08x",
425452
REG_READ(EFUSE_BLK0_RDATA0_REG), after);

components/efuse/esp32/esp_efuse_table.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <assert.h>
1818
#include "esp_efuse_table.h"
1919

20-
// md5_digest_table 11b691b6fa8546a3862a7a876be5f758
20+
// md5_digest_table 8c9f6537b47cc5b26a1a5896158c612a
2121
// This file was generated from the file esp_efuse_table.csv. DO NOT CHANGE THIS FILE MANUALLY.
2222
// If you want to change some fields, you need to change esp_efuse_table.csv file
2323
// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.
@@ -64,7 +64,11 @@ static const esp_efuse_desc_t SECURE_BOOT_KEY[] = {
6464
};
6565

6666
static const esp_efuse_desc_t ABS_DONE_0[] = {
67-
{EFUSE_BLK0, 196, 1}, // Secure boot is enabled for bootloader image. EFUSE_RD_ABS_DONE_0,
67+
{EFUSE_BLK0, 196, 1}, // Secure boot V1 is enabled for bootloader image. EFUSE_RD_ABS_DONE_0,
68+
};
69+
70+
static const esp_efuse_desc_t ABS_DONE_1[] = {
71+
{EFUSE_BLK0, 197, 1}, // Secure boot V2 is enabled for bootloader image. EFUSE_RD_ABS_DONE_1,
6872
};
6973

7074
static const esp_efuse_desc_t ENCRYPT_FLASH_KEY[] = {
@@ -103,6 +107,10 @@ static const esp_efuse_desc_t UART_DOWNLOAD_DIS[] = {
103107
{EFUSE_BLK0, 27, 1}, // Disable UART download mode. Valid for ESP32 V3 and newer,
104108
};
105109

110+
static const esp_efuse_desc_t WR_DIS_EFUSE_RD_DISABLE[] = {
111+
{EFUSE_BLK0, 0, 1}, // Write protection for EFUSE_RD_DISABLE,
112+
};
113+
106114
static const esp_efuse_desc_t WR_DIS_FLASH_CRYPT_CNT[] = {
107115
{EFUSE_BLK0, 2, 1}, // Flash encrypt. Write protection FLASH_CRYPT_CNT,
108116
};
@@ -235,7 +243,12 @@ const esp_efuse_desc_t* ESP_EFUSE_SECURE_BOOT_KEY[] = {
235243
};
236244

237245
const esp_efuse_desc_t* ESP_EFUSE_ABS_DONE_0[] = {
238-
&ABS_DONE_0[0], // Secure boot is enabled for bootloader image. EFUSE_RD_ABS_DONE_0
246+
&ABS_DONE_0[0], // Secure boot V1 is enabled for bootloader image. EFUSE_RD_ABS_DONE_0
247+
NULL
248+
};
249+
250+
const esp_efuse_desc_t* ESP_EFUSE_ABS_DONE_1[] = {
251+
&ABS_DONE_1[0], // Secure boot V2 is enabled for bootloader image. EFUSE_RD_ABS_DONE_1
239252
NULL
240253
};
241254

@@ -284,6 +297,11 @@ const esp_efuse_desc_t* ESP_EFUSE_UART_DOWNLOAD_DIS[] = {
284297
NULL
285298
};
286299

300+
const esp_efuse_desc_t* ESP_EFUSE_WR_DIS_EFUSE_RD_DISABLE[] = {
301+
&WR_DIS_EFUSE_RD_DISABLE[0], // Write protection for EFUSE_RD_DISABLE
302+
NULL
303+
};
304+
287305
const esp_efuse_desc_t* ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT[] = {
288306
&WR_DIS_FLASH_CRYPT_CNT[0], // Flash encrypt. Write protection FLASH_CRYPT_CNT
289307
NULL

components/efuse/esp32/esp_efuse_table.csv

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ MAC_CUSTOM_VER, EFUSE_BLK3, 184, 8, Custom MAC version
2929
# Security boot #
3030
#################
3131
SECURE_BOOT_KEY, EFUSE_BLK2, 0, MAX_BLK_LEN, Security boot. Key. (length = "None" - 256. "3/4" - 192. "REPEAT" - 128)
32-
ABS_DONE_0, EFUSE_BLK0, 196, 1, Secure boot is enabled for bootloader image. EFUSE_RD_ABS_DONE_0
32+
ABS_DONE_0, EFUSE_BLK0, 196, 1, Secure boot V1 is enabled for bootloader image. EFUSE_RD_ABS_DONE_0
33+
ABS_DONE_1, EFUSE_BLK0, 197, 1, Secure boot V2 is enabled for bootloader image. EFUSE_RD_ABS_DONE_1
3334

3435
# Flash encrypt #
3536
#################
@@ -48,6 +49,7 @@ UART_DOWNLOAD_DIS, EFUSE_BLK0, 27, 1, Disable UART download mode.
4849

4950
# Write protection #
5051
####################
52+
WR_DIS_EFUSE_RD_DISABLE,EFUSE_BLK0, 0, 1, Write protection for EFUSE_RD_DISABLE
5153
WR_DIS_FLASH_CRYPT_CNT, EFUSE_BLK0, 2, 1, Flash encrypt. Write protection FLASH_CRYPT_CNT, UART_DOWNLOAD_DIS. EFUSE_WR_DIS_FLASH_CRYPT_CNT
5254
WR_DIS_BLK1, EFUSE_BLK0, 7, 1, Flash encrypt. Write protection encryption key. EFUSE_WR_DIS_BLK1
5355
WR_DIS_BLK2, EFUSE_BLK0, 8, 1, Security boot. Write protection security key. EFUSE_WR_DIS_BLK2

components/efuse/esp32/include/esp_efuse_table.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extern "C" {
1717
#endif
1818

1919

20-
// md5_digest_table 11b691b6fa8546a3862a7a876be5f758
20+
// md5_digest_table 8c9f6537b47cc5b26a1a5896158c612a
2121
// This file was generated from the file esp_efuse_table.csv. DO NOT CHANGE THIS FILE MANUALLY.
2222
// If you want to change some fields, you need to change esp_efuse_table.csv file
2323
// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.
@@ -31,6 +31,7 @@ extern const esp_efuse_desc_t* ESP_EFUSE_MAC_CUSTOM[];
3131
extern const esp_efuse_desc_t* ESP_EFUSE_MAC_CUSTOM_VER[];
3232
extern const esp_efuse_desc_t* ESP_EFUSE_SECURE_BOOT_KEY[];
3333
extern const esp_efuse_desc_t* ESP_EFUSE_ABS_DONE_0[];
34+
extern const esp_efuse_desc_t* ESP_EFUSE_ABS_DONE_1[];
3435
extern const esp_efuse_desc_t* ESP_EFUSE_ENCRYPT_FLASH_KEY[];
3536
extern const esp_efuse_desc_t* ESP_EFUSE_ENCRYPT_CONFIG[];
3637
extern const esp_efuse_desc_t* ESP_EFUSE_DISABLE_DL_ENCRYPT[];
@@ -40,6 +41,7 @@ extern const esp_efuse_desc_t* ESP_EFUSE_FLASH_CRYPT_CNT[];
4041
extern const esp_efuse_desc_t* ESP_EFUSE_DISABLE_JTAG[];
4142
extern const esp_efuse_desc_t* ESP_EFUSE_CONSOLE_DEBUG_DISABLE[];
4243
extern const esp_efuse_desc_t* ESP_EFUSE_UART_DOWNLOAD_DIS[];
44+
extern const esp_efuse_desc_t* ESP_EFUSE_WR_DIS_EFUSE_RD_DISABLE[];
4345
extern const esp_efuse_desc_t* ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT[];
4446
extern const esp_efuse_desc_t* ESP_EFUSE_WR_DIS_BLK1[];
4547
extern const esp_efuse_desc_t* ESP_EFUSE_WR_DIS_BLK2[];

0 commit comments

Comments
 (0)