2021-05-09 22:35:07 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-09-29 02:58:17 -04:00
|
|
|
|
2020-07-08 04:42:50 -04:00
|
|
|
#include "string.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
2021-09-28 02:12:56 -04:00
|
|
|
#include "esp_rom_spiflash.h"
|
2020-07-08 04:42:50 -04:00
|
|
|
#include "esp_rom_crc.h"
|
|
|
|
#include "esp_rom_gpio.h"
|
|
|
|
#include "esp_flash_partitions.h"
|
|
|
|
#include "bootloader_flash.h"
|
|
|
|
#include "bootloader_common.h"
|
|
|
|
#include "soc/gpio_periph.h"
|
|
|
|
#include "soc/rtc.h"
|
|
|
|
#include "soc/efuse_reg.h"
|
2022-03-17 09:58:15 -04:00
|
|
|
#include "soc/chip_revision.h"
|
2022-05-25 15:16:15 -04:00
|
|
|
#include "hal/efuse_hal.h"
|
2022-03-17 09:58:15 -04:00
|
|
|
#include "hal/efuse_ll.h"
|
2020-07-08 04:42:50 -04:00
|
|
|
#include "hal/gpio_ll.h"
|
|
|
|
#include "esp_image_format.h"
|
|
|
|
#include "bootloader_sha.h"
|
|
|
|
#include "sys/param.h"
|
2020-09-29 02:58:17 -04:00
|
|
|
#include "bootloader_flash_priv.h"
|
2020-07-08 04:42:50 -04:00
|
|
|
|
|
|
|
#define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
|
2022-03-17 09:58:15 -04:00
|
|
|
#define IS_MAX_REV_SET(max_chip_rev_full) (((max_chip_rev_full) != 65535) && ((max_chip_rev_full) != 0))
|
2020-07-08 04:42:50 -04:00
|
|
|
|
|
|
|
static const char* TAG = "boot_comm";
|
|
|
|
|
|
|
|
uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s)
|
|
|
|
{
|
|
|
|
return esp_rom_crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s)
|
|
|
|
{
|
|
|
|
return s->ota_seq == UINT32_MAX || s->ota_state == ESP_OTA_IMG_INVALID || s->ota_state == ESP_OTA_IMG_ABORTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s)
|
|
|
|
{
|
|
|
|
return bootloader_common_ota_select_invalid(s) == false && s->crc == bootloader_common_ota_select_crc(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata)
|
|
|
|
{
|
|
|
|
if (two_otadata == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bool valid_two_otadata[2];
|
|
|
|
valid_two_otadata[0] = bootloader_common_ota_select_valid(&two_otadata[0]);
|
|
|
|
valid_two_otadata[1] = bootloader_common_ota_select_valid(&two_otadata[1]);
|
|
|
|
return bootloader_common_select_otadata(two_otadata, valid_two_otadata, true);
|
|
|
|
}
|
|
|
|
|
2022-11-04 13:32:38 -04:00
|
|
|
esp_err_t __attribute__((optimize("-Os"))) bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type)
|
2020-07-08 04:42:50 -04:00
|
|
|
{
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
esp_chip_id_t chip_id = CONFIG_IDF_FIRMWARE_CHIP_ID;
|
|
|
|
if (chip_id != img_hdr->chip_id) {
|
|
|
|
ESP_LOGE(TAG, "mismatch chip ID, expected %d, found %d", chip_id, img_hdr->chip_id);
|
|
|
|
err = ESP_FAIL;
|
2022-03-17 09:58:15 -04:00
|
|
|
} else {
|
2020-03-30 01:03:26 -04:00
|
|
|
#ifndef CONFIG_IDF_ENV_FPGA
|
2022-03-17 09:58:15 -04:00
|
|
|
unsigned revision = efuse_hal_chip_revision();
|
|
|
|
unsigned int major_rev = revision / 100;
|
|
|
|
unsigned int minor_rev = revision % 100;
|
|
|
|
unsigned min_rev = img_hdr->min_chip_rev_full;
|
|
|
|
if (type == ESP_IMAGE_BOOTLOADER || type == ESP_IMAGE_APPLICATION) {
|
|
|
|
if (!ESP_CHIP_REV_ABOVE(revision, min_rev)) {
|
|
|
|
ESP_LOGE(TAG, "Image requires chip rev >= v%d.%d, but chip is v%d.%d",
|
|
|
|
min_rev / 100, min_rev % 100,
|
|
|
|
major_rev, minor_rev);
|
|
|
|
err = ESP_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type == ESP_IMAGE_APPLICATION) {
|
|
|
|
unsigned max_rev = img_hdr->max_chip_rev_full;
|
|
|
|
if ((IS_MAX_REV_SET(max_rev) && (revision > max_rev) && !efuse_ll_get_disable_wafer_version_major())) {
|
|
|
|
ESP_LOGE(TAG, "Image requires chip rev <= v%d.%d, but chip is v%d.%d",
|
|
|
|
max_rev / 100, max_rev % 100,
|
|
|
|
major_rev, minor_rev);
|
|
|
|
err = ESP_FAIL;
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 01:03:26 -04:00
|
|
|
#endif // CONFIG_IDF_ENV_FPGA
|
2022-03-17 09:58:15 -04:00
|
|
|
}
|
2020-03-30 01:03:26 -04:00
|
|
|
|
2020-07-08 04:42:50 -04:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max)
|
|
|
|
{
|
|
|
|
if (two_otadata == NULL || valid_two_otadata == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int active_otadata = -1;
|
|
|
|
if (valid_two_otadata[0] && valid_two_otadata[1]) {
|
2020-11-16 23:48:35 -05:00
|
|
|
uint32_t condition = (max == true) ? MAX(two_otadata[0].ota_seq, two_otadata[1].ota_seq) : MIN(two_otadata[0].ota_seq, two_otadata[1].ota_seq);
|
2020-07-08 04:42:50 -04:00
|
|
|
if (condition == two_otadata[0].ota_seq) {
|
|
|
|
active_otadata = 0;
|
|
|
|
} else {
|
|
|
|
active_otadata = 1;
|
|
|
|
}
|
|
|
|
ESP_LOGD(TAG, "Both OTA copies are valid");
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
if (valid_two_otadata[i]) {
|
|
|
|
active_otadata = i;
|
|
|
|
ESP_LOGD(TAG, "Only otadata[%d] is valid", i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return active_otadata;
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:35:29 -04:00
|
|
|
#if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC )
|
|
|
|
|
2021-06-01 04:58:55 -04:00
|
|
|
#define RTC_RETAIN_MEM_ADDR (SOC_RTC_DRAM_HIGH - sizeof(rtc_retain_mem_t))
|
2021-01-16 01:40:01 -05:00
|
|
|
|
2022-08-26 07:40:06 -04:00
|
|
|
_Static_assert(RTC_RETAIN_MEM_ADDR >= SOC_RTC_DRAM_LOW, "rtc_retain_mem_t structure size is bigger than the RTC memory size. Consider reducing RTC reserved memory size.");
|
|
|
|
|
2021-01-16 01:40:01 -05:00
|
|
|
rtc_retain_mem_t *const rtc_retain_mem = (rtc_retain_mem_t *)RTC_RETAIN_MEM_ADDR;
|
|
|
|
|
2021-06-18 02:51:11 -04:00
|
|
|
#ifndef BOOTLOADER_BUILD
|
|
|
|
#include "heap_memory_layout.h"
|
2021-01-16 01:40:01 -05:00
|
|
|
/* The app needs to be told this memory is reserved, important if configured to use RTC memory as heap.
|
|
|
|
|
|
|
|
Note that keeping this macro here only works when other symbols in this file are referenced by the app, as
|
|
|
|
this feature is otherwise 100% part of the bootloader. However this seems to happen in all apps.
|
|
|
|
*/
|
|
|
|
SOC_RESERVE_MEMORY_REGION(RTC_RETAIN_MEM_ADDR, RTC_RETAIN_MEM_ADDR + sizeof(rtc_retain_mem_t), rtc_retain_mem);
|
|
|
|
#endif
|
2020-09-09 04:35:29 -04:00
|
|
|
|
2022-08-26 07:40:06 -04:00
|
|
|
static uint32_t rtc_retain_mem_size(void) {
|
|
|
|
#ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
|
|
|
|
/* A custom memory has been reserved by the user, do not consider this memory into CRC calculation as it may change without
|
|
|
|
* the have the user updating the CRC. Return the offset of the custom field, which is equivalent to size of the structure
|
|
|
|
* minus the size of everything after (including) `custom` */
|
|
|
|
return offsetof(rtc_retain_mem_t, custom);
|
|
|
|
#else
|
|
|
|
return sizeof(rtc_retain_mem_t) - sizeof(rtc_retain_mem->crc);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-09 04:35:29 -04:00
|
|
|
static bool check_rtc_retain_mem(void)
|
|
|
|
{
|
2022-08-26 07:40:06 -04:00
|
|
|
return esp_rom_crc32_le(UINT32_MAX, (uint8_t*)rtc_retain_mem, rtc_retain_mem_size()) == rtc_retain_mem->crc && rtc_retain_mem->crc != UINT32_MAX;
|
2020-09-09 04:35:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void update_rtc_retain_mem_crc(void)
|
|
|
|
{
|
2022-08-26 07:40:06 -04:00
|
|
|
rtc_retain_mem->crc = esp_rom_crc32_le(UINT32_MAX, (uint8_t*)rtc_retain_mem, rtc_retain_mem_size());
|
2020-09-09 04:35:29 -04:00
|
|
|
}
|
|
|
|
|
2022-09-02 07:23:45 -04:00
|
|
|
NOINLINE_ATTR void bootloader_common_reset_rtc_retain_mem(void)
|
2020-09-09 04:35:29 -04:00
|
|
|
{
|
2022-05-30 00:44:08 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wstringop-overflow"
|
|
|
|
#pragma GCC diagnostic ignored "-Warray-bounds"
|
2020-09-09 04:35:29 -04:00
|
|
|
memset(rtc_retain_mem, 0, sizeof(rtc_retain_mem_t));
|
2022-05-30 00:44:08 -04:00
|
|
|
#pragma GCC diagnostic pop
|
2020-09-09 04:35:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t bootloader_common_get_rtc_retain_mem_reboot_counter(void)
|
|
|
|
{
|
|
|
|
if (check_rtc_retain_mem()) {
|
|
|
|
return rtc_retain_mem->reboot_counter;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_partition_pos_t* bootloader_common_get_rtc_retain_mem_partition(void)
|
|
|
|
{
|
|
|
|
if (check_rtc_retain_mem()) {
|
|
|
|
return &rtc_retain_mem->partition;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bootloader_common_update_rtc_retain_mem(esp_partition_pos_t* partition, bool reboot_counter)
|
|
|
|
{
|
|
|
|
if (reboot_counter) {
|
|
|
|
if (!check_rtc_retain_mem()) {
|
|
|
|
bootloader_common_reset_rtc_retain_mem();
|
|
|
|
}
|
|
|
|
if (++rtc_retain_mem->reboot_counter == 0) {
|
|
|
|
// do not allow to overflow. Stop it.
|
|
|
|
--rtc_retain_mem->reboot_counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (partition != NULL) {
|
|
|
|
rtc_retain_mem->partition.offset = partition->offset;
|
|
|
|
rtc_retain_mem->partition.size = partition->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_rtc_retain_mem_crc();
|
|
|
|
}
|
|
|
|
|
|
|
|
rtc_retain_mem_t* bootloader_common_get_rtc_retain_mem(void)
|
|
|
|
{
|
|
|
|
return rtc_retain_mem;
|
|
|
|
}
|
|
|
|
#endif // defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC )
|