1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <string.h>
7 #include <stdint.h>
8 #include "sdkconfig.h"
9 #include "esp_attr.h"
10 #include "esp_log.h"
11 #include "bootloader_init.h"
12 #include "bootloader_flash_priv.h"
13 #include "bootloader_flash_config.h"
14 #include "bootloader_random.h"
15 #include "bootloader_clock.h"
16 #include "bootloader_common.h"
17 #include "esp_flash_encrypt.h"
18 #include "esp_cpu.h"
19 #include "soc/rtc.h"
20 #include "hal/wdt_hal.h"
21 #include "hal/efuse_hal.h"
22
23 static const char *TAG = "boot";
24
25 esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr;
26
bootloader_clear_bss_section(void)27 void bootloader_clear_bss_section(void)
28 {
29 memset(&_bss_start, 0, ((unsigned*)&_bss_end - (unsigned*)&_bss_start) * sizeof(&_bss_start));
30 }
31
bootloader_read_bootloader_header(void)32 esp_err_t bootloader_read_bootloader_header(void)
33 {
34 /* load bootloader image header */
35 if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &bootloader_image_hdr, sizeof(esp_image_header_t), true) != ESP_OK) {
36 ESP_EARLY_LOGE(TAG, "failed to load bootloader image header!");
37 return ESP_FAIL;
38 }
39 return ESP_OK;
40 }
41
bootloader_check_bootloader_validity(void)42 esp_err_t bootloader_check_bootloader_validity(void)
43 {
44 unsigned int revision = efuse_hal_chip_revision();
45 unsigned int major = revision / 100;
46 unsigned int minor = revision % 100;
47 ESP_EARLY_LOGI(TAG, "chip revision: v%d.%d", major, minor);
48 /* compare with the one set in bootloader image header */
49 if (bootloader_common_check_chip_validity(&bootloader_image_hdr, ESP_IMAGE_BOOTLOADER) != ESP_OK) {
50 return ESP_FAIL;
51 }
52 return ESP_OK;
53 }
54
bootloader_config_wdt(void)55 void bootloader_config_wdt(void)
56 {
57 /*
58 * At this point, the flashboot protection of RWDT and MWDT0 will have been
59 * automatically enabled. We can disable flashboot protection as it's not
60 * needed anymore. If configured to do so, we also initialize the RWDT to
61 * protect the remainder of the bootloader process.
62 */
63 //Disable RWDT flashboot protection.
64 wdt_hal_context_t rwdt_ctx = RWDT_HAL_CONTEXT_DEFAULT();
65 wdt_hal_write_protect_disable(&rwdt_ctx);
66 wdt_hal_set_flashboot_en(&rwdt_ctx, false);
67 wdt_hal_write_protect_enable(&rwdt_ctx);
68
69 #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
70 //Initialize and start RWDT to protect the for bootloader if configured to do so
71 ESP_EARLY_LOGD(TAG, "Enabling RTCWDT(%d ms)", CONFIG_BOOTLOADER_WDT_TIME_MS);
72 wdt_hal_init(&rwdt_ctx, WDT_RWDT, 0, false);
73 uint32_t stage_timeout_ticks = (uint32_t)((uint64_t)CONFIG_BOOTLOADER_WDT_TIME_MS * rtc_clk_slow_freq_get_hz() / 1000);
74 wdt_hal_write_protect_disable(&rwdt_ctx);
75 wdt_hal_config_stage(&rwdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
76 wdt_hal_enable(&rwdt_ctx);
77 wdt_hal_write_protect_enable(&rwdt_ctx);
78 #endif
79
80 //Disable MWDT0 flashboot protection. But only after we've enabled the RWDT first so that there's not gap in WDT protection.
81 wdt_hal_context_t mwdt_ctx = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
82 wdt_hal_write_protect_disable(&mwdt_ctx);
83 wdt_hal_set_flashboot_en(&mwdt_ctx, false);
84 wdt_hal_write_protect_enable(&mwdt_ctx);
85 }
86
bootloader_enable_random(void)87 void bootloader_enable_random(void)
88 {
89 ESP_EARLY_LOGI(TAG, "Enabling RNG early entropy source...");
90 bootloader_random_enable();
91 }
92
bootloader_print_banner(void)93 void bootloader_print_banner(void)
94 {
95 #ifdef CONFIG_MCUBOOT
96 ESP_EARLY_LOGI(TAG, "MCUboot 2nd stage bootloader");
97 #else
98 #ifdef CONFIG_BOOTLOADER_MCUBOOT
99 ESP_EARLY_LOGI(TAG, "MCUboot Application image");
100 #else /* ifdef CONFIG_ESP_SIMPLE_BOOT */
101 ESP_EARLY_LOGI(TAG, "ESP Simple boot");
102 #endif
103 #endif
104
105 #ifndef CONFIG_APP_REPRODUCIBLE_BUILD
106 ESP_EARLY_LOGI(TAG, "compile time " __DATE__ " " __TIME__);
107 #endif
108
109 #if CONFIG_FREERTOS_UNICORE
110 #if (SOC_CPU_CORES_NUM > 1)
111 ESP_EARLY_LOGW(TAG, "Unicore bootloader");
112 #endif
113 #else
114 ESP_EARLY_LOGI(TAG, "Multicore bootloader");
115 #endif
116 }
117