2021-05-31 00:43:23 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-11-18 22:46:21 -05:00
|
|
|
|
2019-11-28 08:11:49 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "sdkconfig.h"
|
2018-11-18 22:46:21 -05:00
|
|
|
#include "esp_efuse.h"
|
|
|
|
#include "esp_efuse_utility.h"
|
|
|
|
#include "esp_efuse_table.h"
|
|
|
|
#include "esp_types.h"
|
|
|
|
#include "assert.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
2019-05-13 06:02:45 -04:00
|
|
|
#include "soc/efuse_periph.h"
|
2018-11-18 22:46:21 -05:00
|
|
|
#include "bootloader_random.h"
|
2019-07-28 23:35:00 -04:00
|
|
|
#include "soc/apb_ctrl_reg.h"
|
2019-06-18 07:34:05 -04:00
|
|
|
#include "sys/param.h"
|
2018-11-18 22:46:21 -05:00
|
|
|
|
2020-03-23 07:49:27 -04:00
|
|
|
static __attribute__((unused)) const char *TAG = "efuse";
|
2021-06-16 19:21:36 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD
|
|
|
|
#define APP_SEC_VER_SIZE_EFUSE_FIELD CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD
|
|
|
|
#else
|
|
|
|
#define APP_SEC_VER_SIZE_EFUSE_FIELD 16 // smallest possible size for all chips
|
|
|
|
#endif
|
2018-11-18 22:46:21 -05:00
|
|
|
|
|
|
|
// Reset efuse write registers
|
|
|
|
void esp_efuse_reset(void)
|
|
|
|
{
|
|
|
|
esp_efuse_utility_reset();
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
uint32_t esp_efuse_read_secure_version(void)
|
2018-12-18 09:42:10 -05:00
|
|
|
{
|
2019-06-18 07:34:05 -04:00
|
|
|
uint32_t secure_version = 0;
|
2021-06-16 19:21:36 -04:00
|
|
|
size_t size = MIN(APP_SEC_VER_SIZE_EFUSE_FIELD, esp_efuse_get_field_size(ESP_EFUSE_SECURE_VERSION));
|
2019-06-18 07:34:05 -04:00
|
|
|
esp_efuse_read_field_blob(ESP_EFUSE_SECURE_VERSION, &secure_version, size);
|
|
|
|
return __builtin_popcount(secure_version & ((1ULL << size) - 1));
|
2018-12-18 09:42:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool esp_efuse_check_secure_version(uint32_t secure_version)
|
|
|
|
{
|
|
|
|
uint32_t sec_ver_hw = esp_efuse_read_secure_version();
|
|
|
|
return secure_version >= sec_ver_hw;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_efuse_update_secure_version(uint32_t secure_version)
|
|
|
|
{
|
2021-06-16 19:21:36 -04:00
|
|
|
size_t size = MIN(APP_SEC_VER_SIZE_EFUSE_FIELD, esp_efuse_get_field_size(ESP_EFUSE_SECURE_VERSION));
|
|
|
|
if (size < secure_version) {
|
|
|
|
ESP_LOGE(TAG, "Max secure version is %d. Given %d version can not be written.", size, secure_version);
|
2018-12-18 09:42:10 -05:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2019-06-18 07:34:05 -04:00
|
|
|
esp_efuse_coding_scheme_t coding_scheme = esp_efuse_get_coding_scheme(ESP_EFUSE_SECURE_VERSION_NUM_BLOCK);
|
|
|
|
if (coding_scheme != EFUSE_CODING_SCHEME_NONE) {
|
|
|
|
ESP_LOGE(TAG, "Anti rollback is not supported with any coding scheme.");
|
2018-12-18 09:42:10 -05:00
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
uint32_t sec_ver_hw = esp_efuse_read_secure_version();
|
|
|
|
// If secure_version is the same as in eFuse field than it is ok just go out.
|
|
|
|
if (sec_ver_hw < secure_version) {
|
|
|
|
uint32_t num_bit_hw = (1ULL << sec_ver_hw) - 1;
|
|
|
|
uint32_t num_bit_app = (1ULL << secure_version) - 1;
|
|
|
|
// Repeated programming of programmed bits is strictly forbidden
|
|
|
|
uint32_t new_bits = num_bit_app - num_bit_hw; // get only new bits
|
2021-06-16 19:21:36 -04:00
|
|
|
esp_efuse_write_field_blob(ESP_EFUSE_SECURE_VERSION, &new_bits, size);
|
2018-12-18 09:42:10 -05:00
|
|
|
ESP_LOGI(TAG, "Anti-rollback is set. eFuse field is updated(%d).", secure_version);
|
|
|
|
} else if (sec_ver_hw > secure_version) {
|
|
|
|
ESP_LOGE(TAG, "Anti-rollback is not set. secure_version of app is lower that eFuse field(%d).", sec_ver_hw);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|