feat(efuse): Move efuse-related init steps into the component

pull/13338/head
KonstantinKondrashov 2024-01-11 20:59:08 +08:00 zatwierdzone przez Konstantin Kondrashov
rodzic 1253ab6e27
commit f9800e0726
13 zmienionych plików z 178 dodań i 127 usunięć

Wyświetl plik

@ -32,10 +32,20 @@ list(APPEND srcs "src/esp_efuse_api.c"
"src/esp_efuse_utility.c"
"src/efuse_controller/keys/${type}/esp_efuse_api_key.c")
idf_component_register(SRCS "${srcs}"
if(BOOTLOADER_BUILD)
idf_component_register(SRCS "${srcs}"
PRIV_REQUIRES bootloader_support soc spi_flash
INCLUDE_DIRS "${include_dirs}"
PRIV_INCLUDE_DIRS "${private_include}")
else()
list(APPEND srcs "src/esp_efuse_startup.c")
idf_component_register(SRCS "${srcs}"
PRIV_REQUIRES bootloader_support soc spi_flash esp_system esp_partition esp_app_format
INCLUDE_DIRS "${include_dirs}"
PRIV_INCLUDE_DIRS "${private_include}")
# Forces the linker to include esp_efuse_startup.c
target_link_libraries(${COMPONENT_LIB} INTERFACE "-u esp_efuse_startup_include_func")
endif()
if(target)
set(TOOL_TARGET -t ${target})

Wyświetl plik

@ -0,0 +1,157 @@
/*
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "soc/soc_caps.h"
#include "hal/efuse_hal.h"
#include "rom/efuse.h"
#include "esp_efuse.h"
#include "esp_efuse_table.h"
#include "esp_check.h"
#include "esp_efuse_utility.h"
#include "esp_system.h"
#include "esp_flash_encrypt.h"
#include "esp_secure_boot.h"
#include "esp_log.h"
#include "esp_private/startup_internal.h"
#ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
#include "esp_partition.h"
#endif
#include "sdkconfig.h"
#if __has_include("esp_app_desc.h")
#include "esp_app_desc.h"
#endif
static __attribute__((unused)) const char *TAG = "efuse_init";
ESP_SYSTEM_INIT_FN(init_efuse_check, CORE, BIT(0), 1)
{
// (Only for C3): We check if the efuse BLOCK0 has certain coding errors then reset the chip.
if (esp_efuse_check_errors() != ESP_OK) {
esp_restart();
}
return ESP_OK;
}
// It comes after init_show_app_info to print the consistent application information.
ESP_SYSTEM_INIT_FN(init_efuse_show_app_info, CORE, BIT(0), 21)
{
if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) {
ESP_EARLY_LOGI(TAG, "Min chip rev: v%d.%d", CONFIG_ESP_REV_MIN_FULL / 100, CONFIG_ESP_REV_MIN_FULL % 100);
ESP_EARLY_LOGI(TAG, "Max chip rev: v%d.%d %s", CONFIG_ESP_REV_MAX_FULL / 100, CONFIG_ESP_REV_MAX_FULL % 100,
efuse_hal_get_disable_wafer_version_major() ? "(constraint ignored)" : "");
unsigned revision = efuse_hal_chip_revision();
ESP_EARLY_LOGI(TAG, "Chip rev: v%d.%d", revision / 100, revision % 100);
}
return ESP_OK;
}
#ifdef CONFIG_EFUSE_VIRTUAL
static void init_efuse_virtual(void)
{
ESP_LOGW(TAG, "eFuse virtual mode is enabled. If Secure boot or Flash encryption is enabled then it does not provide any security. FOR TESTING ONLY!");
#ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
// For efuse virtual mode we need to seed virtual efuses from flash
// esp_flash must be initialized in advance because here we read the efuse partition.
const esp_partition_t *efuse_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_EFUSE_EM, NULL);
if (efuse_partition) {
esp_efuse_init_virtual_mode_in_flash(efuse_partition->address, efuse_partition->size);
}
#else // !CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
// For efuse virtual mode we need to seed virtual efuses from efuse_regs.
esp_efuse_utility_update_virt_blocks();
#endif // !CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
}
#endif // CONFIG_EFUSE_VIRTUAL
static esp_err_t init_efuse_secure(void)
{
#if CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
// For anti-rollback case, recheck security version before we boot-up the current application
const esp_app_desc_t *desc = esp_app_get_description();
ESP_RETURN_ON_FALSE(esp_efuse_check_secure_version(desc->secure_version), ESP_FAIL, TAG, "Incorrect secure version of app");
#endif
#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
esp_flash_encryption_init_checks();
#endif
#if defined(CONFIG_SECURE_BOOT) || defined(CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT)
// Note: in some configs this may read flash, so placed after flash init
esp_secure_boot_init_checks();
#endif
#if SOC_EFUSE_ECDSA_USE_HARDWARE_K
if (esp_efuse_find_purpose(ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY, NULL)) {
// ECDSA key purpose block is present and hence permanently enable
// the hardware TRNG supplied k mode (most secure mode)
ESP_RETURN_ON_ERROR(esp_efuse_write_field_bit(ESP_EFUSE_ECDSA_FORCE_USE_HARDWARE_K), TAG, "Failed to enable hardware k mode");
}
#endif
#if CONFIG_SECURE_DISABLE_ROM_DL_MODE
// Permanently disable ROM download mode
ESP_RETURN_ON_ERROR(esp_efuse_disable_rom_download_mode(), TAG, "Failed to disable ROM download mode");
#endif
#if CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE
// Permanently disable ROM secure download mode
ESP_RETURN_ON_ERROR(esp_efuse_enable_rom_secure_download_mode(), TAG, "Failed to enable Secure Download mode");
#endif
#if CONFIG_ESP32_DISABLE_BASIC_ROM_CONSOLE
// ESP32 only: Permanently disable BASIC ROM Console feature
esp_efuse_disable_basic_rom_console();
#endif
return ESP_OK;
}
// Set efuse ROM_LOG_MODE on first boot
// For CONFIG_BOOT_ROM_LOG_ALWAYS_ON (default) or undefined (ESP32), leave
// ROM_LOG_MODE undefined (no need to call this function during startup)
#if CONFIG_BOOT_ROM_LOG_ALWAYS_OFF
#define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ALWAYS_OFF
#elif CONFIG_BOOT_ROM_LOG_ON_GPIO_LOW
#define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ON_GPIO_LOW
#elif CONFIG_BOOT_ROM_LOG_ON_GPIO_HIGH
#define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ON_GPIO_HIGH
#endif
#ifdef ROM_LOG_MODE
static esp_err_t init_efuse_rom_log(void)
{
// Applicable for any chips except ESP32: Permanently disable ROM startup logs
if (ets_efuse_get_uart_print_control() != ROM_LOG_MODE) {
esp_err_t error = esp_efuse_set_rom_log_scheme(ROM_LOG_MODE);
error = (error == ESP_ERR_NOT_SUPPORTED) ? ESP_OK : error;
ESP_RETURN_ON_ERROR(error, TAG, "Failed to set ROM log scheme");
}
return ESP_OK;
}
#endif // ROM_LOG_MODE
ESP_SYSTEM_INIT_FN(init_efuse, CORE, BIT(0), 140)
{
esp_err_t error = ESP_OK;
#ifdef CONFIG_EFUSE_VIRTUAL
init_efuse_virtual();
#endif
error = init_efuse_secure();
ESP_RETURN_ON_ERROR(error, TAG, "Failed in secure eFuse init");
#ifdef ROM_LOG_MODE
error = init_efuse_rom_log();
#endif
return error;
}
void esp_efuse_startup_include_func(void)
{
// Hook to force the linker to include this file
}

Wyświetl plik

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -23,14 +23,6 @@ static volatile unsigned s_burn_counter = 0;
// Array for emulate efuse registers.
#ifdef CONFIG_EFUSE_VIRTUAL
uint32_t virt_blocks[EFUSE_BLK_MAX][COUNT_EFUSE_REG_PER_BLOCK];
#ifndef BOOTLOADER_BUILD
#ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
/* Call the update function to seed virtual efuses during initialization */
__attribute__((constructor)) void esp_efuse_utility_update_virt_blocks(void);
#endif // CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
#endif // NOT BOOTLOADER_BUILD
#endif // CONFIG_EFUSE_VIRTUAL
extern const esp_efuse_range_addr_t range_read_addr_blocks[];

Wyświetl plik

@ -10,7 +10,6 @@
#include "esp_app_desc.h"
#include "sdkconfig.h"
#include "hal/efuse_hal.h"
#include "esp_log.h"
#include "esp_private/startup_internal.h"
@ -125,13 +124,6 @@ ESP_SYSTEM_INIT_FN(init_show_app_info, CORE, BIT(0), 20)
esp_app_get_elf_sha256(buf, sizeof(buf));
ESP_EARLY_LOGI(TAG, "ELF file SHA256: %s...", buf);
ESP_EARLY_LOGI(TAG, "ESP-IDF: %s", esp_app_desc.idf_ver);
// TODO: To be moved to the eFuse initialization routine
ESP_EARLY_LOGI(TAG, "Min chip rev: v%d.%d", CONFIG_ESP_REV_MIN_FULL / 100, CONFIG_ESP_REV_MIN_FULL % 100);
ESP_EARLY_LOGI(TAG, "Max chip rev: v%d.%d %s", CONFIG_ESP_REV_MAX_FULL / 100, CONFIG_ESP_REV_MAX_FULL % 100,
efuse_hal_get_disable_wafer_version_major() ? "(constraint ignored)" : "");
unsigned revision = efuse_hal_chip_revision();
ESP_EARLY_LOGI(TAG, "Chip rev: v%d.%d", revision / 100, revision % 100);
}
return ESP_OK;
}

Wyświetl plik

@ -67,9 +67,7 @@ else()
# [refactor-todo] requirements due to init code,
# should be removable once using component init functions
# link-time registration is used.
# [refactor-todo] esp_partition required for virtual efuse
# init code. Move to esp_efuse component.
bootloader_support efuse esp_partition esp_pm
bootloader_support esp_pm
LDFRAGMENTS "linker.lf" "app.lf")
add_subdirectory(port)

Wyświetl plik

@ -14,11 +14,9 @@
#include "esp_log.h"
#include "esp_chip_info.h"
#include "esp_efuse.h"
#include "esp_private/cache_err_int.h"
#include "esp_clk_internal.h"
#include "esp_rom_efuse.h"
#include "esp_rom_uart.h"
#include "esp_rom_sys.h"
#include "esp_rom_caps.h"
@ -499,9 +497,6 @@ void IRAM_ATTR call_start_cpu0(void)
extern void esp_config_l2_cache_mode(void);
esp_config_l2_cache_mode();
#endif
if (esp_efuse_check_errors() != ESP_OK) {
esp_restart();
}
#if ESP_ROM_NEEDS_SET_CACHE_MMU_SIZE
#if CONFIG_APP_BUILD_TYPE_ELF_RAM

Wyświetl plik

@ -8,7 +8,6 @@
#include "esp_system.h"
#include "esp_private/system_internal.h"
#include "esp_attr.h"
#include "esp_efuse.h"
#include "esp_log.h"
#include "esp_ipc_isr.h"
#include "sdkconfig.h"

Wyświetl plik

@ -9,7 +9,6 @@
#include "esp_system.h"
#include "esp_private/system_internal.h"
#include "esp_attr.h"
#include "esp_efuse.h"
#include "esp_log.h"
#include "riscv/rv_utils.h"
#include "esp_rom_uart.h"

Wyświetl plik

@ -9,7 +9,6 @@
#include "esp_system.h"
#include "esp_private/system_internal.h"
#include "esp_attr.h"
#include "esp_efuse.h"
#include "esp_log.h"
#include "riscv/rv_utils.h"
#include "esp_rom_uart.h"

Wyświetl plik

@ -9,7 +9,6 @@
#include "esp_system.h"
#include "esp_private/system_internal.h"
#include "esp_attr.h"
#include "esp_efuse.h"
#include "esp_log.h"
#include "esp32s2/rom/cache.h"
#include "esp_rom_uart.h"

Wyświetl plik

@ -17,11 +17,6 @@
#include "spi_flash_mmap.h"
#include "esp_flash_internal.h"
#include "esp_newlib.h"
#include "esp_efuse.h"
#include "esp_efuse_table.h"
#include "esp_flash_encrypt.h"
#include "esp_partition.h"
#include "esp_secure_boot.h"
#include "esp_xt_wdt.h"
#include "esp_cpu.h"
#include "esp_private/startup_internal.h"
@ -29,16 +24,11 @@
#include "hal/wdt_hal.h"
#include "hal/uart_types.h"
#include "hal/uart_ll.h"
#include "hal/efuse_hal.h"
#if CONFIG_SW_COEXIST_ENABLE || CONFIG_EXTERNAL_COEX_ENABLE
#include "private/esp_coexist_internal.h"
#endif
#if __has_include("esp_app_desc.h")
#include "esp_app_desc.h"
#endif
#if CONFIG_PM_ENABLE
#include "esp_pm.h"
#include "esp_private/pm_impl.h"
@ -132,88 +122,6 @@ ESP_SYSTEM_INIT_FN(init_flash, CORE, BIT(0), 130)
}
#endif // !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
#ifdef CONFIG_EFUSE_VIRTUAL
ESP_SYSTEM_INIT_FN(init_virtual_efuse, CORE, BIT(0), 140)
{
ESP_LOGW(TAG, "eFuse virtual mode is enabled. If Secure boot or Flash encryption is enabled then it does not provide any security. FOR TESTING ONLY!");
#ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
const esp_partition_t *efuse_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_EFUSE_EM, NULL);
if (efuse_partition) {
esp_efuse_init_virtual_mode_in_flash(efuse_partition->address, efuse_partition->size);
}
#endif
return ESP_OK;
}
#endif // CONFIG_EFUSE_VIRTUAL
ESP_SYSTEM_INIT_FN(init_secure, CORE, BIT(0), 150)
{
#if CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
// For anti-rollback case, recheck security version before we boot-up the current application
const esp_app_desc_t *desc = esp_app_get_description();
ESP_RETURN_ON_FALSE(esp_efuse_check_secure_version(desc->secure_version), ESP_FAIL, TAG, "Incorrect secure version of app");
#endif
#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
esp_flash_encryption_init_checks();
#endif
#if defined(CONFIG_SECURE_BOOT) || defined(CONFIG_SECURE_SIGNED_ON_UPDATE_NO_SECURE_BOOT)
// Note: in some configs this may read flash, so placed after flash init
esp_secure_boot_init_checks();
#endif
#if SOC_EFUSE_ECDSA_USE_HARDWARE_K
if (esp_efuse_find_purpose(ESP_EFUSE_KEY_PURPOSE_ECDSA_KEY, NULL)) {
// ECDSA key purpose block is present and hence permanently enable
// the hardware TRNG supplied k mode (most secure mode)
ESP_RETURN_ON_ERROR(esp_efuse_write_field_bit(ESP_EFUSE_ECDSA_FORCE_USE_HARDWARE_K), TAG, "Failed to enable hardware k mode");
}
#endif
#if CONFIG_SECURE_DISABLE_ROM_DL_MODE
ESP_RETURN_ON_ERROR(esp_efuse_disable_rom_download_mode(), TAG, "Failed to disable ROM download mode");
#endif
#if CONFIG_SECURE_ENABLE_SECURE_ROM_DL_MODE
ESP_RETURN_ON_ERROR(esp_efuse_enable_rom_secure_download_mode(), TAG, "Failed to enable Secure Download mode");
#endif
#if CONFIG_ESP32_DISABLE_BASIC_ROM_CONSOLE
esp_efuse_disable_basic_rom_console();
#endif
return ESP_OK;
}
// Set efuse ROM_LOG_MODE on first boot
//
// For CONFIG_BOOT_ROM_LOG_ALWAYS_ON (default) or undefined (ESP32), leave
// ROM_LOG_MODE undefined (no need to call this function during startup)
#if CONFIG_BOOT_ROM_LOG_ALWAYS_OFF
#define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ALWAYS_OFF
#elif CONFIG_BOOT_ROM_LOG_ON_GPIO_LOW
#define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ON_GPIO_LOW
#elif CONFIG_BOOT_ROM_LOG_ON_GPIO_HIGH
#define ROM_LOG_MODE ESP_EFUSE_ROM_LOG_ON_GPIO_HIGH
#endif
#ifdef ROM_LOG_MODE
ESP_SYSTEM_INIT_FN(init_rom_log, CORE, BIT(0), 160)
{
if (ets_efuse_get_uart_print_control() == ROM_LOG_MODE) {
return ESP_OK;
}
esp_err_t err = esp_efuse_set_rom_log_scheme(ROM_LOG_MODE);
if (err == ESP_ERR_NOT_SUPPORTED) {
err = ESP_OK;
}
ESP_RETURN_ON_ERROR(err, TAG, "Failed to set ROM log scheme");
return ESP_OK;
}
#endif // ROM_LOG_MODE
#if CONFIG_ESP_XT_WDT
ESP_SYSTEM_INIT_FN(init_xt_wdt, CORE, BIT(0), 170)
{

Wyświetl plik

@ -17,9 +17,12 @@
########### CORE startup stage ###########
# [refactor-todo]: move init calls into respective components
CORE: 1: init_efuse_check in components/efuse/src/esp_efuse_startup.c on BIT(0)
# Log some information about the system
CORE: 10: init_show_cpu_freq in components/esp_system/startup_funcs.c on BIT(0)
CORE: 20: init_show_app_info in components/esp_app_format/esp_app_desc.c on BIT(0)
CORE: 21: init_efuse_show_app_info in components/efuse/src/esp_efuse_startup.c on BIT(0)
# Initialize heap allocator. WARNING: This *needs* to happen *after* the app cpu has booted.
# If the heap allocator is initialized first, it will put free memory linked list items into
@ -52,9 +55,7 @@ CORE: 115: init_newlib_stdio in components/newlib/newlib_init.c on BIT(0)
CORE: 120: init_pthread in components/pthread/pthread.c on BIT(0)
CORE: 130: init_flash in components/esp_system/startup_funcs.c on BIT(0)
CORE: 140: init_virtual_efuse in components/esp_system/startup_funcs.c on BIT(0)
CORE: 150: init_secure in components/esp_system/startup_funcs.c on BIT(0)
CORE: 160: init_rom_log in components/esp_system/startup_funcs.c on BIT(0)
CORE: 140: init_efuse in components/efuse/src/esp_efuse_startup.c on BIT(0)
CORE: 170: init_xt_wdt in components/esp_system/startup_funcs.c on BIT(0)

Wyświetl plik

@ -40,8 +40,10 @@ set(extra_components_which_shouldnt_be_included
esp_app_format
# esp_bootloader_format is dependency of bootloader_support, app_update
esp_bootloader_format
# [refactor-todo]: efuse is a dependency of esp_hw_support, esp_system.
# Figure out if these components can exist without a dependency on efuse.
# [refactor-todo]:
# Figure out if the esp_hw_support component can exist without a dependency on efuse.
# efuse is used by the ADC calibration functions in esp_hw_support/adc_share_hw_ctrl.c,
# it could use the efuse hal (if virtual efuse mode is not used for tests).
# If not, see if esp_hw_support can provide minimal efuse component replacement in G1 build.
efuse
# esp_pm is pulled in by freertos, can be made a weak dependency