2021-05-09 22:35:07 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-04-16 11:35:41 -04:00
|
|
|
#include <stdbool.h>
|
2018-04-23 08:19:04 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include "string.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
2019-06-07 07:57:47 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#include "esp32/rom/spi_flash.h"
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/rom/spi_flash.h"
|
2020-07-29 10:03:46 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
|
|
#include "esp32s3/rom/spi_flash.h"
|
2020-11-26 03:56:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
|
|
#include "esp32c3/rom/spi_flash.h"
|
2019-06-07 07:57:47 -04:00
|
|
|
#endif
|
2020-06-19 03:08:38 -04:00
|
|
|
#include "esp_rom_crc.h"
|
2020-06-19 00:00:58 -04:00
|
|
|
#include "esp_rom_gpio.h"
|
2020-07-21 01:07:34 -04:00
|
|
|
#include "esp_rom_sys.h"
|
2018-04-23 08:19:04 -04:00
|
|
|
#include "esp_flash_partitions.h"
|
2020-07-12 15:23:12 -04:00
|
|
|
#include "bootloader_flash_priv.h"
|
2018-04-23 08:19:04 -04:00
|
|
|
#include "bootloader_common.h"
|
2020-07-07 09:56:58 -04:00
|
|
|
#include "bootloader_utility.h"
|
2018-06-13 00:52:44 -04:00
|
|
|
#include "soc/gpio_periph.h"
|
2019-05-07 04:36:37 -04:00
|
|
|
#include "soc/rtc.h"
|
2019-09-26 22:58:30 -04:00
|
|
|
#include "soc/efuse_reg.h"
|
2020-06-19 00:00:58 -04:00
|
|
|
#include "hal/gpio_ll.h"
|
2018-05-30 05:08:00 -04:00
|
|
|
#include "esp_image_format.h"
|
|
|
|
#include "bootloader_sha.h"
|
2018-10-22 11:11:10 -04:00
|
|
|
#include "sys/param.h"
|
2018-05-30 05:08:00 -04:00
|
|
|
|
|
|
|
#define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
|
2018-04-23 08:19:04 -04:00
|
|
|
|
|
|
|
static const char* TAG = "boot_comm";
|
2017-01-29 22:29:50 -05:00
|
|
|
|
2018-04-23 08:19:04 -04:00
|
|
|
esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec)
|
|
|
|
{
|
2020-06-19 00:00:58 -04:00
|
|
|
esp_rom_gpio_pad_select_gpio(num_pin);
|
2018-06-13 00:52:44 -04:00
|
|
|
if (GPIO_PIN_MUX_REG[num_pin]) {
|
|
|
|
PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[num_pin]);
|
|
|
|
}
|
2020-06-19 00:00:58 -04:00
|
|
|
esp_rom_gpio_pad_pullup_only(num_pin);
|
2018-04-23 08:19:04 -04:00
|
|
|
uint32_t tm_start = esp_log_early_timestamp();
|
2020-06-19 00:00:58 -04:00
|
|
|
if (gpio_ll_get_level(&GPIO, num_pin) == 1) {
|
2018-04-23 08:19:04 -04:00
|
|
|
return GPIO_NOT_HOLD;
|
|
|
|
}
|
|
|
|
do {
|
2020-06-19 00:00:58 -04:00
|
|
|
if (gpio_ll_get_level(&GPIO, num_pin) != 0) {
|
2018-04-23 08:19:04 -04:00
|
|
|
return GPIO_SHORT_HOLD;
|
|
|
|
}
|
|
|
|
} while (delay_sec > ((esp_log_early_timestamp() - tm_start) / 1000L));
|
|
|
|
return GPIO_LONG_HOLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for a label in the list. list = "nvs1, nvs2, otadata, nvs"; label = "nvs".
|
|
|
|
bool bootloader_common_label_search(const char *list, char *label)
|
|
|
|
{
|
|
|
|
if (list == NULL || label == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const char *sub_list_start_like_label = strstr(list, label);
|
|
|
|
while (sub_list_start_like_label != NULL) {
|
|
|
|
|
|
|
|
// ["," or " "] + label + ["," or " " or "\0"]
|
|
|
|
// first character before the label found there must be a delimiter ["," or " "].
|
|
|
|
int idx_first = sub_list_start_like_label - list;
|
|
|
|
if (idx_first == 0 || (idx_first != 0 && (list[idx_first - 1] == ',' || list[idx_first - 1] == ' '))) {
|
|
|
|
// next character after the label found there must be a delimiter ["," or " " or "\0"].
|
|
|
|
int len_label = strlen(label);
|
|
|
|
if (sub_list_start_like_label[len_label] == 0 ||
|
|
|
|
sub_list_start_like_label[len_label] == ',' ||
|
|
|
|
sub_list_start_like_label[len_label] == ' ') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// [start_delim] + label + [end_delim] was not found.
|
|
|
|
// Position is moving to next delimiter if it is not the end of list.
|
2020-11-16 23:48:35 -05:00
|
|
|
size_t pos_delim = strcspn(sub_list_start_like_label, ", ");
|
2018-04-23 08:19:04 -04:00
|
|
|
if (pos_delim == strlen(sub_list_start_like_label)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sub_list_start_like_label = strstr(&sub_list_start_like_label[pos_delim], label);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase)
|
|
|
|
{
|
|
|
|
const esp_partition_info_t *partitions;
|
|
|
|
const char *marker;
|
|
|
|
esp_err_t err;
|
|
|
|
int num_partitions;
|
|
|
|
bool ret = true;
|
|
|
|
|
2018-04-19 00:42:26 -04:00
|
|
|
partitions = bootloader_mmap(ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
|
2018-04-23 08:19:04 -04:00
|
|
|
if (!partitions) {
|
2018-08-24 08:28:04 -04:00
|
|
|
ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
|
|
|
|
return false;
|
2018-04-23 08:19:04 -04:00
|
|
|
}
|
2018-04-19 00:42:26 -04:00
|
|
|
ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_OFFSET, (intptr_t)partitions);
|
2018-04-23 08:19:04 -04:00
|
|
|
|
2018-07-13 01:23:04 -04:00
|
|
|
err = esp_partition_table_verify(partitions, true, &num_partitions);
|
2018-04-23 08:19:04 -04:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to verify partition table");
|
|
|
|
ret = false;
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "## Label Usage Offset Length Cleaned");
|
|
|
|
for (int i = 0; i < num_partitions; i++) {
|
|
|
|
const esp_partition_info_t *partition = &partitions[i];
|
|
|
|
char label[sizeof(partition->label) + 1] = {0};
|
|
|
|
if (partition->type == PART_TYPE_DATA) {
|
|
|
|
bool fl_ota_data_erase = false;
|
|
|
|
if (ota_data_erase == true && partition->subtype == PART_SUBTYPE_DATA_OTA) {
|
|
|
|
fl_ota_data_erase = true;
|
|
|
|
}
|
|
|
|
// partition->label is not null-terminated string.
|
2018-05-24 07:32:34 -04:00
|
|
|
strncpy(label, (char *)&partition->label, sizeof(label) - 1);
|
2018-04-23 08:19:04 -04:00
|
|
|
if (fl_ota_data_erase == true || (bootloader_common_label_search(list_erase, label) == true)) {
|
2018-08-24 08:28:04 -04:00
|
|
|
err = bootloader_flash_erase_range(partition->pos.offset, partition->pos.size);
|
2018-04-23 08:19:04 -04:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
ret = false;
|
|
|
|
marker = "err";
|
|
|
|
} else {
|
|
|
|
marker = "yes";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
marker = "no";
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "%2d %-16s data %08x %08x [%s]", i, partition->label,
|
|
|
|
partition->pos.offset, partition->pos.size, marker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bootloader_munmap(partitions);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-05-30 05:08:00 -04:00
|
|
|
|
|
|
|
esp_err_t bootloader_common_get_sha256_of_partition (uint32_t address, uint32_t size, int type, uint8_t *out_sha_256)
|
|
|
|
{
|
|
|
|
if (out_sha_256 == NULL || size == 0) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == PART_TYPE_APP) {
|
|
|
|
const esp_partition_pos_t partition_pos = {
|
|
|
|
.offset = address,
|
|
|
|
.size = size,
|
|
|
|
};
|
|
|
|
esp_image_metadata_t data;
|
2021-04-27 06:49:37 -04:00
|
|
|
if (esp_image_get_metadata(&partition_pos, &data) != ESP_OK) {
|
2018-05-30 05:08:00 -04:00
|
|
|
return ESP_ERR_IMAGE_INVALID;
|
|
|
|
}
|
|
|
|
if (data.image.hash_appended) {
|
|
|
|
memcpy(out_sha_256, data.image_digest, ESP_PARTITION_HASH_LEN);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
// If image doesn't have a appended hash then hash calculates for entire image.
|
|
|
|
size = data.image_len;
|
|
|
|
}
|
|
|
|
// If image is type by data then hash is calculated for entire image.
|
2020-07-07 09:56:58 -04:00
|
|
|
return bootloader_sha256_flash_contents(address, size, out_sha_256);
|
2018-05-30 05:08:00 -04:00
|
|
|
}
|
2018-10-05 08:29:07 -04:00
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void bootloader_common_vddsdio_configure(void)
|
2019-05-07 04:36:37 -04:00
|
|
|
{
|
|
|
|
#if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V
|
|
|
|
rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
|
|
|
|
if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { // VDDSDIO regulator is enabled @ 1.8V
|
|
|
|
cfg.drefh = 3;
|
|
|
|
cfg.drefm = 3;
|
|
|
|
cfg.drefl = 3;
|
|
|
|
cfg.force = 1;
|
|
|
|
rtc_vddsdio_set_config(cfg);
|
2020-07-21 01:07:34 -04:00
|
|
|
esp_rom_delay_us(10); // wait for regulator to become stable
|
2019-05-07 04:36:37 -04:00
|
|
|
}
|
|
|
|
#endif // CONFIG_BOOTLOADER_VDDSDIO_BOOST
|
|
|
|
}
|
2019-07-05 06:18:58 -04:00
|
|
|
|
2019-09-26 22:58:30 -04:00
|
|
|
|
2019-12-12 06:03:25 -05:00
|
|
|
RESET_REASON bootloader_common_get_reset_reason(int cpu_no)
|
|
|
|
{
|
|
|
|
return rtc_get_reset_reason(cpu_no);
|
|
|
|
}
|