1 // Copyright 2018 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 <assert.h>
16 #include "string.h"
17 #include "sdkconfig.h"
18 #include "esp_err.h"
19 #include "esp_log.h"
20 #if CONFIG_IDF_TARGET_ESP32
21 #include "esp32/rom/spi_flash.h"
22 #elif CONFIG_IDF_TARGET_ESP32S2
23 #include "esp32s2/rom/spi_flash.h"
24 #elif CONFIG_IDF_TARGET_ESP32S3
25 #include "esp32s3/rom/spi_flash.h"
26 #elif CONFIG_IDF_TARGET_ESP32C3
27 #include "esp32c3/rom/spi_flash.h"
28 #endif
29 #include "esp_rom_crc.h"
30 #include "esp_rom_gpio.h"
31 #include "esp_rom_sys.h"
32 #include "esp_flash_partitions.h"
33 #include "bootloader_flash_priv.h"
34 #include "bootloader_common.h"
35 #include "bootloader_utility.h"
36 #include "soc/gpio_periph.h"
37 #include "soc/rtc.h"
38 #include "soc/efuse_reg.h"
39 #include "hal/gpio_ll.h"
40 #include "esp_image_format.h"
41 #include "bootloader_sha.h"
42 #include "sys/param.h"
43
44 #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
45
46 static const char* TAG = "boot_comm";
47
bootloader_common_check_long_hold_gpio(uint32_t num_pin,uint32_t delay_sec)48 esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec)
49 {
50 esp_rom_gpio_pad_select_gpio(num_pin);
51 if (GPIO_PIN_MUX_REG[num_pin]) {
52 PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[num_pin]);
53 }
54 esp_rom_gpio_pad_pullup_only(num_pin);
55 uint32_t tm_start = esp_log_early_timestamp();
56 if (gpio_ll_get_level(&GPIO, num_pin) == 1) {
57 return GPIO_NOT_HOLD;
58 }
59 do {
60 if (gpio_ll_get_level(&GPIO, num_pin) != 0) {
61 return GPIO_SHORT_HOLD;
62 }
63 } while (delay_sec > ((esp_log_early_timestamp() - tm_start) / 1000L));
64 return GPIO_LONG_HOLD;
65 }
66
67 // Search for a label in the list. list = "nvs1, nvs2, otadata, nvs"; label = "nvs".
bootloader_common_label_search(const char * list,char * label)68 bool bootloader_common_label_search(const char *list, char *label)
69 {
70 if (list == NULL || label == NULL) {
71 return false;
72 }
73 const char *sub_list_start_like_label = strstr(list, label);
74 while (sub_list_start_like_label != NULL) {
75
76 // ["," or " "] + label + ["," or " " or "\0"]
77 // first character before the label found there must be a delimiter ["," or " "].
78 int idx_first = sub_list_start_like_label - list;
79 if (idx_first == 0 || (idx_first != 0 && (list[idx_first - 1] == ',' || list[idx_first - 1] == ' '))) {
80 // next character after the label found there must be a delimiter ["," or " " or "\0"].
81 int len_label = strlen(label);
82 if (sub_list_start_like_label[len_label] == 0 ||
83 sub_list_start_like_label[len_label] == ',' ||
84 sub_list_start_like_label[len_label] == ' ') {
85 return true;
86 }
87 }
88
89 // [start_delim] + label + [end_delim] was not found.
90 // Position is moving to next delimiter if it is not the end of list.
91 size_t pos_delim = strcspn(sub_list_start_like_label, ", ");
92 if (pos_delim == strlen(sub_list_start_like_label)) {
93 break;
94 }
95 sub_list_start_like_label = strstr(&sub_list_start_like_label[pos_delim], label);
96 }
97 return false;
98 }
99
bootloader_common_erase_part_type_data(const char * list_erase,bool ota_data_erase)100 bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase)
101 {
102 const esp_partition_info_t *partitions;
103 const char *marker;
104 esp_err_t err;
105 int num_partitions;
106 bool ret = true;
107
108 partitions = bootloader_mmap(ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
109 if (!partitions) {
110 ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", ESP_PARTITION_TABLE_OFFSET, ESP_PARTITION_TABLE_MAX_LEN);
111 return false;
112 }
113 ESP_LOGD(TAG, "mapped partition table 0x%x at 0x%x", ESP_PARTITION_TABLE_OFFSET, (intptr_t)partitions);
114
115 err = esp_partition_table_verify(partitions, true, &num_partitions);
116 if (err != ESP_OK) {
117 ESP_LOGE(TAG, "Failed to verify partition table");
118 ret = false;
119 } else {
120 ESP_LOGI(TAG, "## Label Usage Offset Length Cleaned");
121 for (int i = 0; i < num_partitions; i++) {
122 const esp_partition_info_t *partition = &partitions[i];
123 char label[sizeof(partition->label) + 1] = {0};
124 if (partition->type == PART_TYPE_DATA) {
125 bool fl_ota_data_erase = false;
126 if (ota_data_erase == true && partition->subtype == PART_SUBTYPE_DATA_OTA) {
127 fl_ota_data_erase = true;
128 }
129 // partition->label is not null-terminated string.
130 strncpy(label, (char *)&partition->label, sizeof(label) - 1);
131 if (fl_ota_data_erase == true || (bootloader_common_label_search(list_erase, label) == true)) {
132 err = bootloader_flash_erase_range(partition->pos.offset, partition->pos.size);
133 if (err != ESP_OK) {
134 ret = false;
135 marker = "err";
136 } else {
137 marker = "yes";
138 }
139 } else {
140 marker = "no";
141 }
142
143 ESP_LOGI(TAG, "%2d %-16s data %08x %08x [%s]", i, partition->label,
144 partition->pos.offset, partition->pos.size, marker);
145 }
146 }
147 }
148
149 bootloader_munmap(partitions);
150
151 return ret;
152 }
153
bootloader_common_get_sha256_of_partition(uint32_t address,uint32_t size,int type,uint8_t * out_sha_256)154 esp_err_t bootloader_common_get_sha256_of_partition (uint32_t address, uint32_t size, int type, uint8_t *out_sha_256)
155 {
156 if (out_sha_256 == NULL || size == 0) {
157 return ESP_ERR_INVALID_ARG;
158 }
159
160 if (type == PART_TYPE_APP) {
161 const esp_partition_pos_t partition_pos = {
162 .offset = address,
163 .size = size,
164 };
165 esp_image_metadata_t data;
166 if (esp_image_get_metadata(&partition_pos, &data) != ESP_OK) {
167 return ESP_ERR_IMAGE_INVALID;
168 }
169 if (data.image.hash_appended) {
170 memcpy(out_sha_256, data.image_digest, ESP_PARTITION_HASH_LEN);
171 return ESP_OK;
172 }
173 // If image doesn't have a appended hash then hash calculates for entire image.
174 size = data.image_len;
175 }
176 // If image is type by data then hash is calculated for entire image.
177 return bootloader_sha256_flash_contents(address, size, out_sha_256);
178 }
179
bootloader_common_vddsdio_configure(void)180 void bootloader_common_vddsdio_configure(void)
181 {
182 #if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V
183 rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
184 if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { // VDDSDIO regulator is enabled @ 1.8V
185 cfg.drefh = 3;
186 cfg.drefm = 3;
187 cfg.drefl = 3;
188 cfg.force = 1;
189 rtc_vddsdio_set_config(cfg);
190 esp_rom_delay_us(10); // wait for regulator to become stable
191 }
192 #endif // CONFIG_BOOTLOADER_VDDSDIO_BOOST
193 }
194
195
bootloader_common_get_reset_reason(int cpu_no)196 RESET_REASON bootloader_common_get_reset_reason(int cpu_no)
197 {
198 return rtc_get_reset_reason(cpu_no);
199 }
200