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