2018-11-19 03:46:21 +00:00
|
|
|
// Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "esp_efuse.h"
|
|
|
|
#include "esp_efuse_utility.h"
|
|
|
|
#include "esp_efuse_table.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
#include "esp_types.h"
|
2019-03-14 09:29:32 +00:00
|
|
|
#include "esp32/rom/efuse.h"
|
2018-11-19 03:46:21 +00:00
|
|
|
#include "assert.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
2019-05-13 10:02:45 +00:00
|
|
|
#include "soc/efuse_periph.h"
|
2018-11-19 03:46:21 +00:00
|
|
|
#include "bootloader_random.h"
|
2019-07-29 03:35:00 +00:00
|
|
|
#include "soc/apb_ctrl_reg.h"
|
2019-06-18 11:34:05 +00:00
|
|
|
#include "sys/param.h"
|
2018-11-19 03:46:21 +00:00
|
|
|
|
|
|
|
// Permanently update values written to the efuse write registers
|
|
|
|
void esp_efuse_burn_new_values(void)
|
|
|
|
{
|
|
|
|
esp_efuse_utility_burn_efuses();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset efuse write registers
|
|
|
|
void esp_efuse_reset(void)
|
|
|
|
{
|
|
|
|
esp_efuse_utility_reset();
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:10:35 +00:00
|
|
|
#ifdef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
|
2018-12-18 14:42:10 +00:00
|
|
|
|
|
|
|
#include "../include_bootloader/bootloader_flash.h"
|
|
|
|
#include "esp_flash_encrypt.h"
|
|
|
|
|
2019-06-18 11:34:05 +00:00
|
|
|
const static char *TAG = "efuse";
|
2018-12-18 14:42:10 +00:00
|
|
|
static uint32_t esp_efuse_flash_offset = 0;
|
|
|
|
static uint32_t esp_efuse_flash_size = 0;
|
2019-06-18 11:34:05 +00:00
|
|
|
|
2018-12-18 14:42:10 +00:00
|
|
|
void esp_efuse_init(uint32_t offset, uint32_t size)
|
|
|
|
{
|
|
|
|
esp_efuse_flash_offset = offset;
|
|
|
|
esp_efuse_flash_size = size;
|
|
|
|
}
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
static uint32_t emulate_secure_version_read(void)
|
2018-12-18 14:42:10 +00:00
|
|
|
{
|
|
|
|
uint32_t secure_version;
|
|
|
|
uint32_t offset = esp_efuse_flash_offset;
|
|
|
|
if (offset == 0) {
|
|
|
|
ESP_LOGE(TAG, "emulate secure_version can not be used");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const uint32_t *efuse_place_in_flash = bootloader_mmap(offset, esp_efuse_flash_size);
|
|
|
|
if (!efuse_place_in_flash) {
|
|
|
|
ESP_LOGE(TAG, "secure_version can not be read from (0x%x, 0x%x) flash", offset, esp_efuse_flash_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(&secure_version, efuse_place_in_flash, sizeof(uint32_t));
|
|
|
|
bootloader_munmap(efuse_place_in_flash);
|
|
|
|
secure_version = ~secure_version;
|
|
|
|
ESP_LOGV(TAG, "Read 0x%08x secure_version from flash", secure_version);
|
|
|
|
return secure_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emulate_secure_version_write(uint32_t secure_version)
|
|
|
|
{
|
|
|
|
uint32_t secure_version_wr = ~secure_version;
|
|
|
|
uint32_t offset = esp_efuse_flash_offset;
|
|
|
|
if (offset == 0) {
|
|
|
|
ESP_LOGE(TAG, "emulate secure_version can not be used");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
esp_err_t err = bootloader_flash_write(offset, &secure_version_wr, sizeof(secure_version_wr), false);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "secure_version can not be written to flash. err = 0x%x", err);
|
|
|
|
}
|
|
|
|
ESP_LOGV(TAG, "Write 0x%08x secure_version into flash", secure_version);
|
|
|
|
}
|
2019-06-18 11:34:05 +00:00
|
|
|
#endif // CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
|
2018-12-18 14:42:10 +00:00
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
uint32_t esp_efuse_read_secure_version(void)
|
2018-12-18 14:42:10 +00:00
|
|
|
{
|
2019-05-09 12:10:35 +00:00
|
|
|
#ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
|
2019-06-18 11:34:05 +00:00
|
|
|
uint32_t secure_version = 0;
|
|
|
|
int size = esp_efuse_get_field_size(ESP_EFUSE_SECURE_VERSION);
|
|
|
|
size = MIN(CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD, size);
|
2019-05-09 12:10:35 +00:00
|
|
|
#ifdef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
|
2018-12-18 14:42:10 +00:00
|
|
|
secure_version = emulate_secure_version_read();
|
|
|
|
#else
|
2019-06-18 11:34:05 +00:00
|
|
|
esp_efuse_read_field_blob(ESP_EFUSE_SECURE_VERSION, &secure_version, size);
|
2019-05-09 12:10:35 +00:00
|
|
|
#endif // CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
|
2018-12-18 14:42:10 +00:00
|
|
|
|
2019-06-18 11:34:05 +00:00
|
|
|
return __builtin_popcount(secure_version & ((1ULL << size) - 1));
|
2018-12-18 14:42:10 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:10:35 +00:00
|
|
|
#ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
|
2018-12-18 14:42:10 +00:00
|
|
|
static void write_anti_rollback(uint32_t new_bits)
|
|
|
|
{
|
2019-06-18 11:34:05 +00:00
|
|
|
int size = esp_efuse_get_field_size(ESP_EFUSE_SECURE_VERSION);
|
|
|
|
size = MIN(CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD, size);
|
2019-05-09 12:10:35 +00:00
|
|
|
#ifdef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
|
2018-12-18 14:42:10 +00:00
|
|
|
emulate_secure_version_write(new_bits);
|
|
|
|
#else
|
2019-06-18 11:34:05 +00:00
|
|
|
esp_efuse_write_field_blob(ESP_EFUSE_SECURE_VERSION, &new_bits, size);
|
2018-12-18 14:42:10 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2019-05-09 12:10:35 +00:00
|
|
|
#ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
|
|
|
|
if (CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD < secure_version) {
|
|
|
|
ESP_LOGE(TAG, "Max secure version is %d. Given %d version can not be written.", CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD, secure_version);
|
2018-12-18 14:42:10 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2019-05-09 12:10:35 +00:00
|
|
|
#ifndef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
|
2019-06-18 11:34:05 +00: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 14:42:10 +00:00
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
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
|
|
|
|
write_anti_rollback(new_bits);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2019-01-21 14:14:56 +00:00
|
|
|
|