2018-04-16 11:35:41 -04:00
|
|
|
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
2017-01-29 22:29:50 -05: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 11:35:41 -04:00
|
|
|
//
|
2017-01-29 22:29:50 -05: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 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"
|
|
|
|
#include "rom/spi_flash.h"
|
2018-04-16 11:35:41 -04:00
|
|
|
#include "rom/crc.h"
|
2018-04-23 08:19:04 -04:00
|
|
|
#include "rom/ets_sys.h"
|
|
|
|
#include "rom/gpio.h"
|
2018-04-16 11:35:41 -04:00
|
|
|
#include "esp_flash_data_types.h"
|
2018-04-23 08:19:04 -04:00
|
|
|
#include "esp_secure_boot.h"
|
|
|
|
#include "esp_flash_partitions.h"
|
|
|
|
#include "bootloader_flash.h"
|
|
|
|
#include "bootloader_common.h"
|
2018-06-13 00:52:44 -04:00
|
|
|
#include "soc/gpio_periph.h"
|
2018-04-23 08:19:04 -04:00
|
|
|
|
|
|
|
static const char* TAG = "boot_comm";
|
2017-01-29 22:29:50 -05:00
|
|
|
|
2018-04-16 11:35:41 -04: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-29 22:29:50 -05:00
|
|
|
|
2018-04-16 11:35:41 -04:00
|
|
|
bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s)
|
|
|
|
{
|
|
|
|
return s->ota_seq != UINT32_MAX && s->crc == bootloader_common_ota_select_crc(s);
|
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)
|
|
|
|
{
|
|
|
|
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]);
|
|
|
|
}
|
2018-04-23 08:19:04 -04: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;
|
|
|
|
|
|
|
|
#ifdef CONFIG_SECURE_BOOT_ENABLED
|
|
|
|
if (esp_secure_boot_enabled()) {
|
|
|
|
ESP_LOGI(TAG, "Verifying partition table signature...");
|
2018-04-19 00:42:26 -04:00
|
|
|
err = esp_secure_boot_verify_signature(ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
|
2018-04-23 08:19:04 -04:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to verify partition table signature.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ESP_LOGD(TAG, "Partition table signature verified");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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-04-19 00:42:26 -04:00
|
|
|
ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
|
2018-04-23 08:19:04 -04:00
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
|
|
|
err = esp_partition_table_basic_verify(partitions, true, &num_partitions);
|
|
|
|
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)) {
|
|
|
|
err = esp_rom_spiflash_erase_area(partition->pos.offset, partition->pos.size);
|
|
|
|
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;
|
|
|
|
}
|