1 /*
2 * SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <stdbool.h>
7 #include <assert.h>
8 #include "string.h"
9 #include "sdkconfig.h"
10 #include "esp_err.h"
11 #include "esp_log.h"
12 #include "esp_rom_spiflash.h"
13 #include "esp_rom_crc.h"
14 #include "esp_rom_gpio.h"
15 #include "esp_rom_sys.h"
16 #include "esp_flash_partitions.h"
17 #include "bootloader_flash_priv.h"
18 #include "bootloader_common.h"
19 #include "bootloader_utility.h"
20 #include "soc/gpio_periph.h"
21 #include "soc/rtc.h"
22 #include "soc/efuse_reg.h"
23 #include "hal/gpio_ll.h"
24 #include "esp_image_format.h"
25 #include "bootloader_sha.h"
26 #include "sys/param.h"
27
28 #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */
29
bootloader_common_check_long_hold_gpio(uint32_t num_pin,uint32_t delay_sec)30 esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec)
31 {
32 return bootloader_common_check_long_hold_gpio_level(num_pin, delay_sec, false);
33 }
34
bootloader_common_check_long_hold_gpio_level(uint32_t num_pin,uint32_t delay_sec,bool level)35 esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio_level(uint32_t num_pin, uint32_t delay_sec, bool level)
36 {
37 esp_rom_gpio_pad_select_gpio(num_pin);
38 if (GPIO_PIN_MUX_REG[num_pin]) {
39 PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[num_pin]);
40 }
41 esp_rom_gpio_pad_pullup_only(num_pin);
42 uint32_t tm_start = esp_log_early_timestamp();
43 if (gpio_ll_get_level(&GPIO, num_pin) != level) {
44 return GPIO_NOT_HOLD;
45 }
46 do {
47 if (gpio_ll_get_level(&GPIO, num_pin) != level) {
48 return GPIO_SHORT_HOLD;
49 }
50 } while (delay_sec > ((esp_log_early_timestamp() - tm_start) / 1000L));
51 return GPIO_LONG_HOLD;
52 }
53
54 // Search for a label in the list. list = "nvs1, nvs2, otadata, nvs"; label = "nvs".
bootloader_common_label_search(const char * list,char * label)55 bool bootloader_common_label_search(const char *list, char *label)
56 {
57 if (list == NULL || label == NULL) {
58 return false;
59 }
60 const char *sub_list_start_like_label = strstr(list, label);
61 while (sub_list_start_like_label != NULL) {
62
63 // ["," or " "] + label + ["," or " " or "\0"]
64 // first character before the label found there must be a delimiter ["," or " "].
65 int idx_first = sub_list_start_like_label - list;
66 if (idx_first == 0 || (idx_first != 0 && (list[idx_first - 1] == ',' || list[idx_first - 1] == ' '))) {
67 // next character after the label found there must be a delimiter ["," or " " or "\0"].
68 int len_label = strlen(label);
69 if (sub_list_start_like_label[len_label] == 0 ||
70 sub_list_start_like_label[len_label] == ',' ||
71 sub_list_start_like_label[len_label] == ' ') {
72 return true;
73 }
74 }
75
76 // [start_delim] + label + [end_delim] was not found.
77 // Position is moving to next delimiter if it is not the end of list.
78 size_t pos_delim = strcspn(sub_list_start_like_label, ", ");
79 if (pos_delim == strlen(sub_list_start_like_label)) {
80 break;
81 }
82 sub_list_start_like_label = strstr(&sub_list_start_like_label[pos_delim], label);
83 }
84 return false;
85 }
86
bootloader_common_vddsdio_configure(void)87 void bootloader_common_vddsdio_configure(void)
88 {
89 #if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V
90 rtc_vddsdio_config_t cfg = rtc_vddsdio_get_config();
91 if (cfg.enable == 1 && cfg.tieh == RTC_VDDSDIO_TIEH_1_8V) { // VDDSDIO regulator is enabled @ 1.8V
92 cfg.drefh = 3;
93 cfg.drefm = 3;
94 cfg.drefl = 3;
95 cfg.force = 1;
96 rtc_vddsdio_set_config(cfg);
97 esp_rom_delay_us(10); // wait for regulator to become stable
98 }
99 #endif // CONFIG_BOOTLOADER_VDDSDIO_BOOST
100 }
101