Skip to content

Commit 336f938

Browse files
committed
fix(bootloader): self encryption workflow in bootloader not working on C5
Added explicit wait for key manager state to be idle before configuring the register for flash encryption key usage from efuse. This now ensures that flash contents are encrypted using efuse programmed key. Also refactored code a bit to move into target specific directory.
1 parent 216e653 commit 336f938

File tree

4 files changed

+68
-33
lines changed

4 files changed

+68
-33
lines changed

components/bootloader_support/include/esp_flash_encrypt.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,17 @@ void esp_flash_encryption_init_checks(void);
180180
/** @brief Set all secure eFuse features related to flash encryption
181181
*
182182
* @return
183-
* - ESP_OK - Successfully
183+
* - ESP_OK - On success
184184
*/
185185
esp_err_t esp_flash_encryption_enable_secure_features(void);
186+
187+
/** @brief Enable the key manager for flash encryption
188+
*
189+
* @return
190+
* - ESP_OK - On success
191+
*/
192+
esp_err_t esp_flash_encryption_enable_key_mgr(void);
193+
186194
#endif /* BOOTLOADER_BUILD && CONFIG_SECURE_FLASH_ENC_ENABLED */
187195

188196
/** @brief Returns the verification status for all physical security features of flash encryption in release mode

components/bootloader_support/src/esp32c5/flash_encryption_secure_features.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include "esp_efuse_table.h"
1212
#include "esp_log.h"
1313
#include "sdkconfig.h"
14+
#include "soc/keymng_reg.h"
15+
#include "soc/pcr_reg.h"
16+
#include "soc/pcr_struct.h"
1417

1518
static __attribute__((unused)) const char *TAG = "flash_encrypt";
1619

@@ -58,3 +61,31 @@ esp_err_t esp_flash_encryption_enable_secure_features(void)
5861

5962
return ESP_OK;
6063
}
64+
65+
// TODO: Update to use LL APIs once key manager support added in IDF-8621
66+
esp_err_t esp_flash_encryption_enable_key_mgr(void)
67+
{
68+
// Set the force power down bit to 0 to enable key manager
69+
PCR.km_pd_ctrl.km_mem_force_pd = 0;
70+
// Reset the key manager
71+
PCR.km_conf.km_clk_en = 1;
72+
PCR.km_conf.km_rst_en = 1;
73+
PCR.km_conf.km_rst_en = 0;
74+
75+
// Wait for key manager to be ready
76+
while (!PCR.km_conf.km_ready) {
77+
};
78+
79+
// Wait for key manager state machine to be idle
80+
while (REG_READ(KEYMNG_STATE_REG) != 0) {
81+
};
82+
83+
// Set the key manager to use efuse key
84+
REG_SET_FIELD(KEYMNG_STATIC_REG, KEYMNG_USE_EFUSE_KEY, 2);
85+
86+
// Reset MSPI to re-load the flash encryption key
87+
REG_SET_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
88+
REG_CLR_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
89+
90+
return ESP_OK;
91+
}

components/bootloader_support/src/esp32p4/flash_encryption_secure_features.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -11,6 +11,8 @@
1111
#include "esp_efuse_table.h"
1212
#include "esp_log.h"
1313
#include "sdkconfig.h"
14+
#include "hal/key_mgr_ll.h"
15+
#include "hal/mspi_timing_tuning_ll.h"
1416

1517
static __attribute__((unused)) const char *TAG = "flash_encrypt";
1618

@@ -48,3 +50,22 @@ esp_err_t esp_flash_encryption_enable_secure_features(void)
4850

4951
return ESP_OK;
5052
}
53+
54+
esp_err_t esp_flash_encryption_enable_key_mgr(void)
55+
{
56+
// Enable and reset key manager
57+
// To suppress build errors about spinlock's __DECLARE_RCC_ATOMIC_ENV
58+
int __DECLARE_RCC_ATOMIC_ENV __attribute__ ((unused));
59+
key_mgr_ll_enable_bus_clock(true);
60+
key_mgr_ll_enable_peripheral_clock(true);
61+
key_mgr_ll_reset_register();
62+
63+
while (key_mgr_ll_get_state() != ESP_KEY_MGR_STATE_IDLE) {
64+
};
65+
66+
// Force Key Manager to use eFuse key for XTS-AES operation
67+
key_mgr_ll_set_key_usage(ESP_KEY_MGR_XTS_AES_128_KEY, ESP_KEY_MGR_USE_EFUSE_KEY);
68+
_mspi_timing_ll_reset_mspi();
69+
70+
return ESP_OK;
71+
}

components/bootloader_support/src/flash_encryption/flash_encrypt.c

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,7 @@
1515
#include "esp_efuse_table.h"
1616
#include "esp_log.h"
1717
#include "hal/wdt_hal.h"
18-
19-
// Need to remove check and merge accordingly for ESP32C5 once key manager support added in IDF-8621
20-
#if SOC_KEY_MANAGER_FE_KEY_DEPLOY || CONFIG_IDF_TARGET_ESP32C5
21-
#if CONFIG_IDF_TARGET_ESP32C5
22-
#include "soc/keymng_reg.h"
23-
#include "soc/pcr_reg.h"
24-
#else /* CONFIG_IDF_TARGET_ESP32C5 */
25-
#include "hal/key_mgr_ll.h"
26-
#include "hal/mspi_timing_tuning_ll.h"
27-
#endif /* !CONFIG_IDF_TARGET_ESP32C5 */
28-
#endif /* SOC_KEY_MANAGER_FE_KEY_DEPLOY */
18+
#include "sdkconfig.h"
2919

3020
#ifdef CONFIG_SOC_EFUSE_CONSISTS_OF_ONE_KEY_BLOCK
3121
#include "soc/sensitive_reg.h"
@@ -221,26 +211,6 @@ static esp_err_t check_and_generate_encryption_keys(void)
221211
}
222212
ESP_LOGI(TAG, "Using pre-loaded flash encryption key in efuse");
223213
}
224-
// Need to remove check for ESP32C5 and merge accordingly once key manager support added in IDF-8621
225-
#if SOC_KEY_MANAGER_FE_KEY_DEPLOY || CONFIG_IDF_TARGET_ESP32C5
226-
#if CONFIG_IDF_TARGET_ESP32C5
227-
REG_SET_FIELD(KEYMNG_STATIC_REG, KEYMNG_USE_EFUSE_KEY, 2);
228-
REG_SET_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
229-
REG_CLR_BIT(PCR_MSPI_CLK_CONF_REG, PCR_MSPI_AXI_RST_EN);
230-
#else /* CONFIG_IDF_TARGET_ESP32C5 */
231-
// Enable and reset key manager
232-
// To suppress build errors about spinlock's __DECLARE_RCC_ATOMIC_ENV
233-
int __DECLARE_RCC_ATOMIC_ENV __attribute__ ((unused));
234-
key_mgr_ll_enable_bus_clock(true);
235-
key_mgr_ll_enable_peripheral_clock(true);
236-
key_mgr_ll_reset_register();
237-
while (key_mgr_ll_get_state() != ESP_KEY_MGR_STATE_IDLE) {
238-
};
239-
// Force Key Manager to use eFuse key for XTS-AES operation
240-
key_mgr_ll_set_key_usage(ESP_KEY_MGR_XTS_AES_128_KEY, ESP_KEY_MGR_USE_EFUSE_KEY);
241-
_mspi_timing_ll_reset_mspi();
242-
#endif /* !CONFIG_IDF_TARGET_ESP32C5 */
243-
#endif /* SOC_KEY_MANAGER_FE_KEY_DEPLOY */
244214

245215
return ESP_OK;
246216
}
@@ -288,6 +258,11 @@ esp_err_t esp_flash_encrypt_contents(void)
288258
REG_WRITE(SENSITIVE_XTS_AES_KEY_UPDATE_REG, 1);
289259
#endif
290260

261+
// TODO: Remove C5 target config after key manager LL support- see IDF-8621
262+
#if CONFIG_SOC_KEY_MANAGER_FE_KEY_DEPLOY || CONFIG_IDF_TARGET_ESP32C5
263+
esp_flash_encryption_enable_key_mgr();
264+
#endif
265+
291266
err = encrypt_bootloader();
292267
if (err != ESP_OK) {
293268
return err;

0 commit comments

Comments
 (0)