Skip to content

Commit 4b40411

Browse files
committed
Merge branch 'feature/fix_load_efuses_from_flash_when_real_fe_is_on_v4.4' into 'release/v4.4'
efuse(virtual mode): Fix load_efuses_from_flash when FE is on (v4.4) See merge request espressif/esp-idf!22969
2 parents 60c57ad + b19d685 commit 4b40411

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

components/bootloader_support/include/esp_flash_encrypt.h

+8-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "esp_spi_flash.h"
1313
#endif
1414
#include "soc/efuse_periph.h"
15+
#include "hal/efuse_hal.h"
1516
#include "sdkconfig.h"
1617

1718
#ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
@@ -46,19 +47,15 @@ typedef enum {
4647
*/
4748
static inline /** @cond */ IRAM_ATTR /** @endcond */ bool esp_flash_encryption_enabled(void)
4849
{
50+
#ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
51+
return efuse_hal_flash_encryption_enabled();
52+
#else
53+
4954
uint32_t flash_crypt_cnt = 0;
5055
#if CONFIG_IDF_TARGET_ESP32
51-
#ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
52-
flash_crypt_cnt = REG_GET_FIELD(EFUSE_BLK0_RDATA0_REG, EFUSE_RD_FLASH_CRYPT_CNT);
53-
#else
54-
esp_efuse_read_field_blob(ESP_EFUSE_FLASH_CRYPT_CNT, &flash_crypt_cnt, ESP_EFUSE_FLASH_CRYPT_CNT[0]->bit_count);
55-
#endif
56+
esp_efuse_read_field_blob(ESP_EFUSE_FLASH_CRYPT_CNT, &flash_crypt_cnt, ESP_EFUSE_FLASH_CRYPT_CNT[0]->bit_count);
5657
#else
57-
#ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
58-
flash_crypt_cnt = REG_GET_FIELD(EFUSE_RD_REPEAT_DATA1_REG, EFUSE_SPI_BOOT_CRYPT_CNT);
59-
#else
60-
esp_efuse_read_field_blob(ESP_EFUSE_SPI_BOOT_CRYPT_CNT, &flash_crypt_cnt, ESP_EFUSE_SPI_BOOT_CRYPT_CNT[0]->bit_count);
61-
#endif
58+
esp_efuse_read_field_blob(ESP_EFUSE_SPI_BOOT_CRYPT_CNT, &flash_crypt_cnt, ESP_EFUSE_SPI_BOOT_CRYPT_CNT[0]->bit_count);
6259
#endif
6360
/* __builtin_parity is in flash, so we calculate parity inline */
6461
bool enabled = false;
@@ -69,6 +66,7 @@ static inline /** @cond */ IRAM_ATTR /** @endcond */ bool esp_flash_encryption_e
6966
flash_crypt_cnt >>= 1;
7067
}
7168
return enabled;
69+
#endif // CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
7270
}
7371

7472
/* @brief Update on-device flash encryption

components/bootloader_support/src/bootloader_flash.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <esp_flash_encrypt.h>
1111
#include "sdkconfig.h"
1212
#include "soc/soc_caps.h"
13+
#include "hal/efuse_hal.h"
1314

1415
#if CONFIG_IDF_TARGET_ESP32
1516
# include "soc/spi_struct.h"
@@ -36,7 +37,7 @@
3637
#endif
3738

3839
#ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
39-
#define ENCRYPTION_IS_VIRTUAL 1
40+
#define ENCRYPTION_IS_VIRTUAL (!efuse_hal_flash_encryption_enabled())
4041
#else
4142
#define ENCRYPTION_IS_VIRTUAL 0
4243
#endif

components/efuse/src/esp_efuse_utility.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ bool esp_efuse_utility_load_efuses_from_flash(void)
439439
}
440440
uint32_t efuses_in_flash[sizeof(virt_blocks)];
441441

442-
esp_err_t err = bootloader_flash_read(esp_efuse_flash_offset, &efuses_in_flash, sizeof(efuses_in_flash), true);
442+
esp_err_t err = bootloader_flash_read(esp_efuse_flash_offset, &efuses_in_flash, sizeof(efuses_in_flash), false);
443443
if (err != ESP_OK) {
444444
ESP_EARLY_LOGE(TAG, "Can not read eFuse partition from flash (err=0x%x)", err);
445445
abort();

components/hal/efuse_hal.c

+13
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,16 @@ IRAM_ATTR uint32_t efuse_hal_chip_revision(void)
1717
{
1818
return efuse_hal_get_major_chip_version() * 100 + efuse_hal_get_minor_chip_version();
1919
}
20+
21+
IRAM_ATTR bool efuse_hal_flash_encryption_enabled(void)
22+
{
23+
uint32_t flash_crypt_cnt = efuse_ll_get_flash_crypt_cnt();
24+
bool enabled = false;
25+
while (flash_crypt_cnt) {
26+
if (flash_crypt_cnt & 1) {
27+
enabled = !enabled;
28+
}
29+
flash_crypt_cnt >>= 1;
30+
}
31+
return enabled;
32+
}

components/hal/include/hal/efuse_hal.h

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ extern "C" {
2121
*/
2222
uint32_t efuse_hal_chip_revision(void);
2323

24+
/**
25+
* @brief Is flash encryption currently enabled in hardware?
26+
*
27+
* Flash encryption is enabled if the FLASH_CRYPT_CNT efuse has an odd number of bits set.
28+
*
29+
* @return true if flash encryption is enabled.
30+
*/
31+
bool efuse_hal_flash_encryption_enabled(void);
32+
2433
/**
2534
* @brief Returns major chip version
2635
*/

docs/en/api-reference/system/efuse.rst

+5
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ During startup, the eFuses are copied to RAM. All eFuse operations (read and wri
361361
In addition to the :ref:`CONFIG_EFUSE_VIRTUAL` option there is :ref:`CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH` option that adds a feature to keep eFuses in flash memory. To use this mode the partition_table should have the `efuse` partition. partition.csv: ``"efuse_em, data, efuse, , 0x2000,"``.
362362
During startup, the eFuses are copied from flash or, in case if flash is empty, from real eFuse to RAM and then update flash. This option allows keeping eFuses after reboots (possible to test secure_boot and flash_encryption features with this option).
363363

364+
Flash Encryption Testing
365+
""""""""""""""""""""""""
366+
367+
Flash Encryption (FE) is a hardware feature that requires the physical burning of eFuses: key and FLASH_CRYPT_CNT. If FE is not actually enabled then enabling the :ref:`CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH` option just gives testing possibilities and does not encrypt anything in the flash, even though the logs say encryption happens. The :cpp:func:`bootloader_flash_write` is adapted for this purpose. But if FE is already enabled on the chip and you run an application or bootloader created with the :ref:`CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH` option then the flash encryption/decryption operations will work properly (data are encrypted as it is written into an encrypted flash partition and decrypted when they are read from an encrypted partition).
368+
364369
espefuse.py
365370
^^^^^^^^^^^
366371

0 commit comments

Comments
 (0)