1 /*
2 * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <stdbool.h>
7 #include "esp_log.h"
8 #include "esp_rom_sys.h"
9 #include "bootloader_init.h"
10 #include "bootloader_utility.h"
11 #include "bootloader_common.h"
12 #include "bootloader_hooks.h"
13
14 static const char *TAG = "boot";
15
16 static int select_partition_number(bootloader_state_t *bs);
17 static int selected_boot_partition(const bootloader_state_t *bs);
18
19 /*
20 * We arrive here after the ROM bootloader finished loading this second stage bootloader from flash.
21 * The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset.
22 * We do have a stack, so we can do the initialization in C.
23 */
call_start_cpu0(void)24 void __attribute__((noreturn)) call_start_cpu0(void)
25 {
26 // (0. Call the before-init hook, if available)
27 if (bootloader_before_init) {
28 bootloader_before_init();
29 }
30
31 // 1. Hardware initialization
32 if (bootloader_init() != ESP_OK) {
33 bootloader_reset();
34 }
35
36 // (1.1 Call the after-init hook, if available)
37 if (bootloader_after_init) {
38 bootloader_after_init();
39 }
40
41 #ifdef CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP
42 // If this boot is a wake up from the deep sleep then go to the short way,
43 // try to load the application which worked before deep sleep.
44 // It skips a lot of checks due to it was done before (while first boot).
45 bootloader_utility_load_boot_image_from_deep_sleep();
46 // If it is not successful try to load an application as usual.
47 #endif
48
49 // 2. Select the number of boot partition
50 bootloader_state_t bs = {0};
51 int boot_index = select_partition_number(&bs);
52 if (boot_index == INVALID_INDEX) {
53 bootloader_reset();
54 }
55
56 // 3. Load the app image for booting
57 bootloader_utility_load_boot_image(&bs, boot_index);
58 }
59
60 // Select the number of boot partition
select_partition_number(bootloader_state_t * bs)61 static int select_partition_number(bootloader_state_t *bs)
62 {
63 // 1. Load partition table
64 if (!bootloader_utility_load_partition_table(bs)) {
65 ESP_LOGE(TAG, "load partition table error!");
66 return INVALID_INDEX;
67 }
68
69 // 2. Select the number of boot partition
70 return selected_boot_partition(bs);
71 }
72
73 /*
74 * Selects a boot partition.
75 * The conditions for switching to another firmware are checked.
76 */
selected_boot_partition(const bootloader_state_t * bs)77 static int selected_boot_partition(const bootloader_state_t *bs)
78 {
79 int boot_index = bootloader_utility_get_selected_boot_partition(bs);
80 if (boot_index == INVALID_INDEX) {
81 return boot_index; // Unrecoverable failure (not due to corrupt ota data or bad partition contents)
82 }
83 if (esp_rom_get_reset_reason(0) != RESET_REASON_CORE_DEEP_SLEEP) {
84 // Factory firmware.
85 #ifdef CONFIG_BOOTLOADER_FACTORY_RESET
86 bool reset_level = false;
87 #if CONFIG_BOOTLOADER_FACTORY_RESET_PIN_HIGH
88 reset_level = true;
89 #endif
90 if (bootloader_common_check_long_hold_gpio_level(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET, CONFIG_BOOTLOADER_HOLD_TIME_GPIO, reset_level) == GPIO_LONG_HOLD) {
91 ESP_LOGI(TAG, "Detect a condition of the factory reset");
92 bool ota_data_erase = false;
93 #ifdef CONFIG_BOOTLOADER_OTA_DATA_ERASE
94 ota_data_erase = true;
95 #endif
96 const char *list_erase = CONFIG_BOOTLOADER_DATA_FACTORY_RESET;
97 ESP_LOGI(TAG, "Data partitions to erase: %s", list_erase);
98 if (bootloader_common_erase_part_type_data(list_erase, ota_data_erase) == false) {
99 ESP_LOGE(TAG, "Not all partitions were erased");
100 }
101 #ifdef CONFIG_BOOTLOADER_RESERVE_RTC_MEM
102 bootloader_common_set_rtc_retain_mem_factory_reset_state();
103 #endif
104 return bootloader_utility_get_selected_boot_partition(bs);
105 }
106 #endif // CONFIG_BOOTLOADER_FACTORY_RESET
107 // TEST firmware.
108 #ifdef CONFIG_BOOTLOADER_APP_TEST
109 bool app_test_level = false;
110 #if CONFIG_BOOTLOADER_APP_TEST_PIN_HIGH
111 app_test_level = true;
112 #endif
113 if (bootloader_common_check_long_hold_gpio_level(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST, CONFIG_BOOTLOADER_HOLD_TIME_GPIO, app_test_level) == GPIO_LONG_HOLD) {
114 ESP_LOGI(TAG, "Detect a boot condition of the test firmware");
115 if (bs->test.offset != 0) {
116 boot_index = TEST_APP_INDEX;
117 return boot_index;
118 } else {
119 ESP_LOGE(TAG, "Test firmware is not found in partition table");
120 return INVALID_INDEX;
121 }
122 }
123 #endif // CONFIG_BOOTLOADER_APP_TEST
124 // Customer implementation.
125 // if (gpio_pin_1 == true && ...){
126 // boot_index = required_boot_partition;
127 // } ...
128 }
129 return boot_index;
130 }
131
132 // Return global reent struct if any newlib functions are linked to bootloader
__getreent(void)133 struct _reent *__getreent(void)
134 {
135 return _GLOBAL_REENT;
136 }
137