1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <string.h>
15 #include <stdint.h>
16 #include "sdkconfig.h"
17 #include "esp_attr.h"
18 #include "esp_log.h"
19 #include "bootloader_init.h"
20 #include "bootloader_flash_priv.h"
21 #include "bootloader_flash_config.h"
22 #include "bootloader_random.h"
23 #include "bootloader_clock.h"
24 #include "bootloader_common.h"
25 #include "esp_flash_encrypt.h"
26 #include "soc/cpu.h"
27 #include "soc/rtc.h"
28 #include "hal/wdt_hal.h"
29
30 static const char *TAG = "boot";
31
32 esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr;
33
bootloader_clear_bss_section(void)34 void bootloader_clear_bss_section(void)
35 {
36 memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
37 }
38
bootloader_read_bootloader_header(void)39 esp_err_t bootloader_read_bootloader_header(void)
40 {
41 /* load bootloader image header */
42 if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &bootloader_image_hdr, sizeof(esp_image_header_t), true) != ESP_OK) {
43 ESP_LOGE(TAG, "failed to load bootloader image header!");
44 return ESP_FAIL;
45 }
46 return ESP_OK;
47 }
48
bootloader_check_bootloader_validity(void)49 esp_err_t bootloader_check_bootloader_validity(void)
50 {
51 /* read chip revision from efuse */
52 uint8_t revision = bootloader_common_get_chip_revision();
53 ESP_LOGI(TAG, "chip revision: %d", revision);
54 /* compare with the one set in bootloader image header */
55 if (bootloader_common_check_chip_validity(&bootloader_image_hdr, ESP_IMAGE_BOOTLOADER) != ESP_OK) {
56 return ESP_FAIL;
57 }
58 return ESP_OK;
59 }
60
bootloader_config_wdt(void)61 void bootloader_config_wdt(void)
62 {
63 /*
64 * At this point, the flashboot protection of RWDT and MWDT0 will have been
65 * automatically enabled. We can disable flashboot protection as it's not
66 * needed anymore. If configured to do so, we also initialize the RWDT to
67 * protect the remainder of the bootloader process.
68 */
69 //Disable RWDT flashboot protection.
70 wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
71 wdt_hal_write_protect_disable(&rtc_wdt_ctx);
72 wdt_hal_set_flashboot_en(&rtc_wdt_ctx, false);
73 wdt_hal_write_protect_enable(&rtc_wdt_ctx);
74
75 #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
76 //Initialize and start RWDT to protect the for bootloader if configured to do so
77 ESP_LOGD(TAG, "Enabling RTCWDT(%d ms)", CONFIG_BOOTLOADER_WDT_TIME_MS);
78 wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
79 uint32_t stage_timeout_ticks = (uint32_t)((uint64_t)CONFIG_BOOTLOADER_WDT_TIME_MS * rtc_clk_slow_freq_get_hz() / 1000);
80 wdt_hal_write_protect_disable(&rtc_wdt_ctx);
81 wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
82 wdt_hal_enable(&rtc_wdt_ctx);
83 wdt_hal_write_protect_enable(&rtc_wdt_ctx);
84 #endif
85
86 //Disable MWDT0 flashboot protection. But only after we've enabled the RWDT first so that there's not gap in WDT protection.
87 wdt_hal_context_t wdt_ctx = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
88 wdt_hal_write_protect_disable(&wdt_ctx);
89 wdt_hal_set_flashboot_en(&wdt_ctx, false);
90 wdt_hal_write_protect_enable(&wdt_ctx);
91 }
92
bootloader_enable_random(void)93 void bootloader_enable_random(void)
94 {
95 ESP_LOGI(TAG, "Enabling RNG early entropy source...");
96 bootloader_random_enable();
97 }
98
bootloader_print_banner(void)99 void bootloader_print_banner(void)
100 {
101 ESP_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER);
102 ESP_LOGI(TAG, "compile time " __TIME__);
103 }
104