2021-05-09 22:35:07 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-04-16 11:35:41 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "sdkconfig.h"
|
2020-06-02 03:03:32 -04:00
|
|
|
#include "esp_attr.h"
|
2019-12-12 06:03:25 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "bootloader_init.h"
|
2020-07-12 15:23:12 -04:00
|
|
|
#include "bootloader_flash_priv.h"
|
2019-12-12 06:03:25 -05:00
|
|
|
#include "bootloader_flash_config.h"
|
2018-04-16 11:35:41 -04:00
|
|
|
#include "bootloader_random.h"
|
|
|
|
#include "bootloader_clock.h"
|
2019-05-07 04:36:37 -04:00
|
|
|
#include "bootloader_common.h"
|
2019-12-12 06:03:25 -05:00
|
|
|
#include "esp_flash_encrypt.h"
|
2021-12-13 23:38:15 -05:00
|
|
|
#include "esp_cpu.h"
|
2019-12-12 06:03:25 -05:00
|
|
|
#include "soc/rtc.h"
|
2019-12-26 03:30:03 -05:00
|
|
|
#include "hal/wdt_hal.h"
|
2022-05-25 15:16:15 -04:00
|
|
|
#include "hal/efuse_hal.h"
|
2022-12-13 12:16:56 -05:00
|
|
|
#include "esp_bootloader_desc.h"
|
2018-04-16 11:35:41 -04:00
|
|
|
|
2019-05-27 02:29:43 -04:00
|
|
|
static const char *TAG = "boot";
|
2018-04-16 11:35:41 -04:00
|
|
|
|
2020-06-02 03:03:32 -04:00
|
|
|
esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr;
|
2018-04-16 11:35:41 -04:00
|
|
|
|
2019-12-12 06:03:25 -05:00
|
|
|
void bootloader_clear_bss_section(void)
|
2018-04-16 11:35:41 -04:00
|
|
|
{
|
|
|
|
memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
|
2019-12-12 06:03:25 -05:00
|
|
|
}
|
2018-04-16 11:35:41 -04:00
|
|
|
|
2019-12-12 06:03:25 -05:00
|
|
|
esp_err_t bootloader_read_bootloader_header(void)
|
|
|
|
{
|
|
|
|
/* load bootloader image header */
|
|
|
|
if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &bootloader_image_hdr, sizeof(esp_image_header_t), true) != ESP_OK) {
|
2023-01-30 05:03:41 -05:00
|
|
|
ESP_EARLY_LOGE(TAG, "failed to load bootloader image header!");
|
2018-04-16 11:35:41 -04:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-12-12 06:03:25 -05:00
|
|
|
esp_err_t bootloader_check_bootloader_validity(void)
|
2018-04-16 11:35:41 -04:00
|
|
|
{
|
2022-03-17 09:58:15 -04:00
|
|
|
unsigned int revision = efuse_hal_chip_revision();
|
|
|
|
unsigned int major = revision / 100;
|
|
|
|
unsigned int minor = revision % 100;
|
2023-01-30 05:03:41 -05:00
|
|
|
ESP_EARLY_LOGI(TAG, "chip revision: v%d.%d", major, minor);
|
2019-12-12 06:03:25 -05:00
|
|
|
/* compare with the one set in bootloader image header */
|
|
|
|
if (bootloader_common_check_chip_validity(&bootloader_image_hdr, ESP_IMAGE_BOOTLOADER) != ESP_OK) {
|
2019-09-15 23:47:23 -04:00
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2019-12-12 06:03:25 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
2019-09-15 23:47:23 -04:00
|
|
|
|
2019-12-12 06:03:25 -05:00
|
|
|
void bootloader_config_wdt(void)
|
|
|
|
{
|
2019-12-26 03:30:03 -05:00
|
|
|
/*
|
|
|
|
* At this point, the flashboot protection of RWDT and MWDT0 will have been
|
|
|
|
* automatically enabled. We can disable flashboot protection as it's not
|
|
|
|
* needed anymore. If configured to do so, we also initialize the RWDT to
|
|
|
|
* protect the remainder of the bootloader process.
|
|
|
|
*/
|
|
|
|
//Disable RWDT flashboot protection.
|
2023-02-14 22:54:00 -05:00
|
|
|
wdt_hal_context_t rwdt_ctx = RWDT_HAL_CONTEXT_DEFAULT();
|
2022-07-08 04:46:11 -04:00
|
|
|
wdt_hal_write_protect_disable(&rwdt_ctx);
|
|
|
|
wdt_hal_set_flashboot_en(&rwdt_ctx, false);
|
|
|
|
wdt_hal_write_protect_enable(&rwdt_ctx);
|
2019-12-26 03:30:03 -05:00
|
|
|
|
2019-08-14 06:26:36 -04:00
|
|
|
#ifdef CONFIG_BOOTLOADER_WDT_ENABLE
|
2019-12-26 03:30:03 -05:00
|
|
|
//Initialize and start RWDT to protect the for bootloader if configured to do so
|
2023-01-30 05:03:41 -05:00
|
|
|
ESP_EARLY_LOGD(TAG, "Enabling RTCWDT(%d ms)", CONFIG_BOOTLOADER_WDT_TIME_MS);
|
2022-07-08 04:46:11 -04:00
|
|
|
wdt_hal_init(&rwdt_ctx, WDT_RWDT, 0, false);
|
2019-12-26 03:30:03 -05:00
|
|
|
uint32_t stage_timeout_ticks = (uint32_t)((uint64_t)CONFIG_BOOTLOADER_WDT_TIME_MS * rtc_clk_slow_freq_get_hz() / 1000);
|
2022-07-08 04:46:11 -04:00
|
|
|
wdt_hal_write_protect_disable(&rwdt_ctx);
|
|
|
|
wdt_hal_config_stage(&rwdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
|
|
|
|
wdt_hal_enable(&rwdt_ctx);
|
|
|
|
wdt_hal_write_protect_enable(&rwdt_ctx);
|
2018-07-26 05:07:36 -04:00
|
|
|
#endif
|
2019-12-26 03:30:03 -05:00
|
|
|
|
|
|
|
//Disable MWDT0 flashboot protection. But only after we've enabled the RWDT first so that there's not gap in WDT protection.
|
2022-07-08 04:46:11 -04:00
|
|
|
wdt_hal_context_t mwdt_ctx = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
|
|
|
|
wdt_hal_write_protect_disable(&mwdt_ctx);
|
|
|
|
wdt_hal_set_flashboot_en(&mwdt_ctx, false);
|
|
|
|
wdt_hal_write_protect_enable(&mwdt_ctx);
|
2018-04-16 11:35:41 -04:00
|
|
|
}
|
|
|
|
|
2019-12-12 06:03:25 -05:00
|
|
|
void bootloader_enable_random(void)
|
2018-04-16 11:35:41 -04:00
|
|
|
{
|
2023-01-30 05:03:41 -05:00
|
|
|
ESP_EARLY_LOGI(TAG, "Enabling RNG early entropy source...");
|
2019-12-12 06:03:25 -05:00
|
|
|
bootloader_random_enable();
|
2018-04-16 11:35:41 -04:00
|
|
|
}
|
|
|
|
|
2019-12-12 06:03:25 -05:00
|
|
|
void bootloader_print_banner(void)
|
2018-04-16 11:35:41 -04:00
|
|
|
{
|
2022-12-13 12:16:56 -05:00
|
|
|
if (CONFIG_BOOTLOADER_LOG_LEVEL >= ESP_LOG_INFO) {
|
|
|
|
const esp_bootloader_desc_t *desc = esp_bootloader_get_description();
|
|
|
|
ESP_EARLY_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", desc->idf_ver);
|
|
|
|
#ifdef CONFIG_BOOTLOADER_COMPILE_TIME_DATE
|
|
|
|
ESP_EARLY_LOGI(TAG, "compile time %s", desc->date_time);
|
2021-10-09 03:42:14 -04:00
|
|
|
#endif
|
2022-12-13 12:16:56 -05:00
|
|
|
}
|
2023-03-08 14:03:53 -05:00
|
|
|
|
|
|
|
#if CONFIG_FREERTOS_UNICORE
|
|
|
|
#if (SOC_CPU_CORES_NUM > 1)
|
|
|
|
ESP_EARLY_LOGW(TAG, "Unicore bootloader");
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
ESP_EARLY_LOGI(TAG, "Multicore bootloader");
|
|
|
|
#endif
|
2018-04-16 11:35:41 -04:00
|
|
|
}
|