2018-04-16 15:35:41 +00:00
|
|
|
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
2017-01-30 03:29:50 +00:00
|
|
|
//
|
|
|
|
// 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
|
2018-04-16 15:35:41 +00:00
|
|
|
//
|
2017-01-30 03:29:50 +00:00
|
|
|
// 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.
|
2018-04-16 15:35:41 +00:00
|
|
|
#include <stdbool.h>
|
2018-04-23 12:19:04 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include "string.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
2019-03-14 09:29:32 +00:00
|
|
|
#include "esp32/rom/crc.h"
|
|
|
|
#include "esp32/rom/gpio.h"
|
2018-04-23 12:19:04 +00:00
|
|
|
#include "esp_secure_boot.h"
|
|
|
|
#include "esp_flash_partitions.h"
|
|
|
|
#include "bootloader_flash.h"
|
|
|
|
#include "bootloader_common.h"
|
2018-06-13 04:52:44 +00:00
|
|
|
#include "soc/gpio_periph.h"
|
2019-05-07 08:36:37 +00:00
|
|
|
#include "soc/rtc.h"
|
2019-09-27 02:58:30 +00:00
|
|
|
#include "soc/efuse_reg.h"
|
|
|
|
#include "soc/apb_ctrl_reg.h"
|
2018-05-30 09:08:00 +00:00
|
|
|
#include "esp_image_format.h"
|
|
|
|
#include "bootloader_sha.h"
|
2018-10-22 15:11:10 +00:00
|
|
|
#include "sys/param.h"
|
2018-05-30 09:08:00 +00:00
|
|
|
|
|
|
|
#define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
|
2018-04-23 12:19:04 +00:00
|
|
|
|
|
|
|
static const char* TAG = "boot_comm";
|
2017-01-30 03:29:50 +00:00
|
|
|
|
2018-04-16 15:35:41 +00:00
|
|
|
uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s)
|
|
|
|
{
|
|
|
|
return crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4);
|
|
|
|
}
|
2017-01-30 03:29:50 +00:00
|
|
|
|
2018-10-22 15:11:10 +00:00
|
|
|
bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s)
|
|
|
|
{
|
2018-10-23 12:27:32 +00:00
|
|
|
return s->ota_seq == UINT32_MAX || s->ota_state == ESP_OTA_IMG_INVALID || s->ota_state == ESP_OTA_IMG_ABORTED;
|
2018-10-22 15:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 15:35:41 +00:00
|
|
|
bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s)
|
|
|
|
{
|
2018-10-22 15:11:10 +00:00
|
|
|
return bootloader_common_ota_select_invalid(s) == false && s->crc == bootloader_common_ota_select_crc(s);
|
2017-01-30 03:29:50 +00:00
|
|
|
}
|
2018-04-23 12:19:04 +00:00
|
|
|
|
|
|
|
esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec)
|
|
|
|
{
|
|
|
|
gpio_pad_select_gpio(num_pin);
|
2018-06-13 04:52:44 +00:00
|
|
|
if (GPIO_PIN_MUX_REG[num_pin]) {
|
|
|
|
PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[num_pin]);
|
|
|
|
}
|
2018-04-23 12:19:04 +00:00
|
|
|
gpio_pad_pullup(num_pin);
|
|
|
|
uint32_t tm_start = esp_log_early_timestamp();
|
|
|
|
if (GPIO_INPUT_GET(num_pin) == 1) {
|
|
|
|
return GPIO_NOT_HOLD;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if (GPIO_INPUT_GET(num_pin) != 0) {
|
|
|
|
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.
|
|
|
|
int pos_delim = strcspn(sub_list_start_like_label, ", ");
|
|
|
|
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 04:42:26 +00:00
|
|
|
partitions = bootloader_mmap(ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
|
2018-04-23 12:19:04 +00:00
|
|
|
if (!partitions) {
|
2018-08-24 12:28:04 +00: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 12:19:04 +00:00
|
|
|
}
|
2018-04-19 04:42:26 +00:00
|
|
|
ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_OFFSET, (intptr_t)partitions);
|
2018-04-23 12:19:04 +00:00
|
|
|
|
2018-07-13 05:23:04 +00:00
|
|
|
err = esp_partition_table_verify(partitions, true, &num_partitions);
|
2018-04-23 12:19:04 +00: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 11:32:34 +00:00
|
|
|
strncpy(label, (char *)&partition->label, sizeof(label) - 1);
|
2018-04-23 12:19:04 +00:00
|
|
|
if (fl_ota_data_erase == true || (bootloader_common_label_search(list_erase, label) == true)) {
|
2018-08-24 12:28:04 +00:00
|
|
|
err = bootloader_flash_erase_range(partition->pos.offset, partition->pos.size);
|
2018-04-23 12:19:04 +00: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 09:08:00 +00: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;
|
|
|
|
// Function esp_image_verify() verifies and fills the structure data.
|
|
|
|
// here important to get: image_digest, image_len, hash_appended.
|
|
|
|
if (esp_image_verify(ESP_IMAGE_VERIFY_SILENT, &partition_pos, &data) != ESP_OK) {
|
|
|
|
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.
|
|
|
|
const void *partition_bin = bootloader_mmap(address, size);
|
|
|
|
if (partition_bin == NULL) {
|
|
|
|
ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", address, size);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
bootloader_sha256_handle_t sha_handle = bootloader_sha256_start();
|
|
|
|
if (sha_handle == NULL) {
|
|
|
|
bootloader_munmap(partition_bin);
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
bootloader_sha256_data(sha_handle, partition_bin, size);
|
|
|
|
bootloader_sha256_finish(sha_handle, out_sha_256);
|
|
|
|
|
|
|
|
bootloader_munmap(partition_bin);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2018-10-05 12:29:07 +00:00
|
|
|
|
2019-02-13 09:32:23 +00:00
|
|
|
int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max)
|
2018-10-22 15:11:10 +00:00
|
|
|
{
|
2019-02-13 09:32:23 +00:00
|
|
|
if (two_otadata == NULL || valid_two_otadata == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-10-22 15:11:10 +00:00
|
|
|
int active_otadata = -1;
|
2019-02-13 09:32:23 +00:00
|
|
|
if (valid_two_otadata[0] && valid_two_otadata[1]) {
|
|
|
|
int 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);
|
|
|
|
if (condition == two_otadata[0].ota_seq) {
|
2018-10-22 15:11:10 +00:00
|
|
|
active_otadata = 0;
|
|
|
|
} else {
|
|
|
|
active_otadata = 1;
|
|
|
|
}
|
|
|
|
ESP_LOGD(TAG, "Both OTA copies are valid");
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
2019-02-13 09:32:23 +00:00
|
|
|
if (valid_two_otadata[i]) {
|
2018-10-22 15:11:10 +00:00
|
|
|
active_otadata = i;
|
|
|
|
ESP_LOGD(TAG, "Only otadata[%d] is valid", i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return active_otadata;
|
|
|
|
}
|
|
|
|
|
2019-02-13 09:32:23 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:29:07 +00:00
|
|
|
esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc)
|
|
|
|
{
|
|
|
|
if (partition == NULL || app_desc == NULL || partition->offset == 0) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *image = bootloader_mmap(partition->offset, partition->size);
|
|
|
|
if (image == NULL) {
|
|
|
|
ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, partition->size);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(app_desc, image + sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t), sizeof(esp_app_desc_t));
|
|
|
|
bootloader_munmap(image);
|
|
|
|
|
|
|
|
if (app_desc->magic_word != ESP_APP_DESC_MAGIC_WORD) {
|
|
|
|
return ESP_ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2019-05-07 08:36:37 +00:00
|
|
|
|
|
|
|
void bootloader_common_vddsdio_configure()
|
|
|
|
{
|
|
|
|
#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);
|
|
|
|
ets_delay_us(10); // wait for regulator to become stable
|
|
|
|
}
|
|
|
|
#endif // CONFIG_BOOTLOADER_VDDSDIO_BOOST
|
|
|
|
}
|
2019-09-16 03:47:23 +00:00
|
|
|
|
2019-09-27 02:58:30 +00:00
|
|
|
#ifdef CONFIG_IDF_TARGET_ESP32
|
|
|
|
uint8_t bootloader_common_get_chip_revision(void)
|
|
|
|
{
|
|
|
|
uint8_t eco_bit0, eco_bit1, eco_bit2;
|
|
|
|
eco_bit0 = (REG_READ(EFUSE_BLK0_RDATA3_REG) & 0xF000) >> 15;
|
|
|
|
eco_bit1 = (REG_READ(EFUSE_BLK0_RDATA5_REG) & 0x100000) >> 20;
|
|
|
|
eco_bit2 = (REG_READ(APB_CTRL_DATE_REG) & 0x80000000) >> 31;
|
|
|
|
uint32_t combine_value = (eco_bit2 << 2) | (eco_bit1 << 1) | eco_bit0;
|
|
|
|
uint8_t chip_ver = 0;
|
|
|
|
switch (combine_value) {
|
|
|
|
case 0:
|
|
|
|
chip_ver = 0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
chip_ver = 1;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
chip_ver = 2;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
chip_ver = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
chip_ver = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return chip_ver;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-10-11 09:05:22 +00:00
|
|
|
esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type)
|
2019-09-16 03:47:23 +00: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) {
|
2019-10-11 09:05:22 +00:00
|
|
|
ESP_LOGE(TAG, "mismatch chip ID, expected %d, found %d", chip_id, img_hdr->chip_id);
|
2019-09-16 03:47:23 +00:00
|
|
|
err = ESP_FAIL;
|
|
|
|
}
|
2019-09-27 02:58:30 +00:00
|
|
|
uint8_t revision = bootloader_common_get_chip_revision();
|
2019-09-16 03:47:23 +00:00
|
|
|
if (revision < img_hdr->min_chip_rev) {
|
2019-10-11 09:05:22 +00:00
|
|
|
ESP_LOGE(TAG, "can't run on lower chip revision, expected %d, found %d", revision, img_hdr->min_chip_rev);
|
2019-09-16 03:47:23 +00:00
|
|
|
err = ESP_FAIL;
|
|
|
|
} else if (revision != img_hdr->min_chip_rev) {
|
2019-10-11 09:05:22 +00:00
|
|
|
ESP_LOGI(TAG, "chip revision: %d, min. %s chip revision: %d", revision, type == ESP_IMAGE_BOOTLOADER ? "bootloader" : "application", img_hdr->min_chip_rev);
|
2019-09-16 03:47:23 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|