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 
49 #if defined(CONFIG_SOC_SERIES_ESP32)
50     if (major < 3) {
51         ESP_EARLY_LOGE(TAG, "You are using ESP32 chip revision (%d) that is unsupported. While it may work, it could cause unexpected behavior or issues.", major);
52         ESP_EARLY_LOGE(TAG, "Proceeding with this ESP32 chip revision is not recommended unless you fully understand the potential risk and limitations.");
53 #if !defined(CONFIG_ESP32_USE_UNSUPPORTED_REVISION)
54         ESP_EARLY_LOGE(TAG, "If you choose to continue, please enable the 'CONFIG_ESP32_USE_UNSUPPORTED_REVISION' in your project configuration.");
55         return ESP_FAIL;
56 #endif
57     }
58 #endif
59     /* compare with the one set in bootloader image header */
60     if (bootloader_common_check_chip_validity(&bootloader_image_hdr, ESP_IMAGE_BOOTLOADER) != ESP_OK) {
61         return ESP_FAIL;
62     }
63     return ESP_OK;
64 }
65 
bootloader_config_wdt(void)66 void bootloader_config_wdt(void)
67 {
68     /*
69      * At this point, the flashboot protection of RWDT and MWDT0 will have been
70      * automatically enabled. We can disable flashboot protection as it's not
71      * needed anymore. If configured to do so, we also initialize the RWDT to
72      * protect the remainder of the bootloader process.
73      */
74     //Disable RWDT flashboot protection.
75     wdt_hal_context_t rwdt_ctx = RWDT_HAL_CONTEXT_DEFAULT();
76     wdt_hal_write_protect_disable(&rwdt_ctx);
77     wdt_hal_set_flashboot_en(&rwdt_ctx, false);
78     wdt_hal_write_protect_enable(&rwdt_ctx);
79 
80 #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
81     //Initialize and start RWDT to protect the  for bootloader if configured to do so
82     ESP_EARLY_LOGD(TAG, "Enabling RTCWDT(%d ms)", CONFIG_BOOTLOADER_WDT_TIME_MS);
83     wdt_hal_init(&rwdt_ctx, WDT_RWDT, 0, false);
84     uint32_t stage_timeout_ticks = (uint32_t)((uint64_t)CONFIG_BOOTLOADER_WDT_TIME_MS * rtc_clk_slow_freq_get_hz() / 1000);
85     wdt_hal_write_protect_disable(&rwdt_ctx);
86     wdt_hal_config_stage(&rwdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
87     wdt_hal_enable(&rwdt_ctx);
88     wdt_hal_write_protect_enable(&rwdt_ctx);
89 #endif
90 
91     //Disable MWDT0 flashboot protection. But only after we've enabled the RWDT first so that there's not gap in WDT protection.
92     wdt_hal_context_t mwdt_ctx = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
93     wdt_hal_write_protect_disable(&mwdt_ctx);
94     wdt_hal_set_flashboot_en(&mwdt_ctx, false);
95     wdt_hal_write_protect_enable(&mwdt_ctx);
96 }
97 
bootloader_enable_random(void)98 void bootloader_enable_random(void)
99 {
100     ESP_EARLY_LOGI(TAG, "Enabling RNG early entropy source...");
101     bootloader_random_enable();
102 }
103 
bootloader_print_banner(void)104 void bootloader_print_banner(void)
105 {
106 #ifdef CONFIG_MCUBOOT
107     ESP_EARLY_LOGI(TAG, "MCUboot 2nd stage bootloader");
108 #else
109 #ifdef CONFIG_BOOTLOADER_MCUBOOT
110     ESP_EARLY_LOGI(TAG, "MCUboot Application image");
111 #else /* ifdef CONFIG_ESP_SIMPLE_BOOT */
112     ESP_EARLY_LOGI(TAG, "ESP Simple boot");
113 #endif
114 #endif
115 
116 #ifndef CONFIG_APP_REPRODUCIBLE_BUILD
117     ESP_EARLY_LOGI(TAG, "compile time " __DATE__ " " __TIME__);
118 #endif
119 
120 #if CONFIG_FREERTOS_UNICORE
121 #if (SOC_CPU_CORES_NUM > 1)
122     ESP_EARLY_LOGW(TAG, "Unicore bootloader");
123 #endif
124 #else
125     ESP_EARLY_LOGI(TAG, "Multicore bootloader");
126 #endif
127 }
128