• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #pragma once
16 #include "esp_flash_partitions.h"
17 #include "esp_image_format.h"
18 #include "esp_app_format.h"
19 // RESET_REASON is declared in rom/rtc.h
20 #if CONFIG_IDF_TARGET_ESP32
21 #include "esp32/rom/rtc.h"
22 #elif CONFIG_IDF_TARGET_ESP32S2
23 #include "esp32s2/rom/rtc.h"
24 #elif CONFIG_IDF_TARGET_ESP32S3
25 #include "esp32s3/rom/rtc.h"
26 #elif CONFIG_IDF_TARGET_ESP32C3
27 #include "esp32c3/rom/rtc.h"
28 #endif
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /// Type of hold a GPIO in low state
35 typedef enum {
36     GPIO_LONG_HOLD  = 1,    /*!< The long hold GPIO */
37     GPIO_SHORT_HOLD = -1,   /*!< The short hold GPIO */
38     GPIO_NOT_HOLD   = 0     /*!< If the GPIO input is not low */
39 } esp_comm_gpio_hold_t;
40 
41 typedef enum {
42     ESP_IMAGE_BOOTLOADER,
43     ESP_IMAGE_APPLICATION
44 } esp_image_type;
45 
46 /**
47  * @brief Calculate crc for the OTA data select.
48  *
49  * @param[in] s The OTA data select.
50  * @return    Returns crc value.
51  */
52 uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s);
53 
54 /**
55  * @brief Verifies the validity of the OTA data select
56  *
57  * @param[in] s The OTA data select.
58  * @return    Returns true on valid, false otherwise.
59  */
60 bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s);
61 
62 /**
63  * @brief Returns true if OTADATA is not marked as bootable partition.
64  *
65  * @param[in] s The OTA data select.
66  * @return    Returns true if OTADATA invalid, false otherwise.
67  */
68 bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s);
69 
70 /**
71  * @brief Check if the GPIO input is a long hold or a short hold.
72  *
73  * Number of the GPIO input will be configured as an input with internal pull-up enabled.
74  * If the GPIO input is held low continuously for delay_sec period then it is a long hold.
75  * If the GPIO input is held low for less period then it is a short hold.
76  *
77  * @param[in] num_pin Number of the GPIO input.
78  * @param[in] delay_sec Input must be driven low for at least this long, continuously.
79  * @return esp_comm_gpio_hold_t Defines type of hold a GPIO in low state.
80  */
81 esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec);
82 
83 /**
84  * @brief Erase the partition data that is specified in the transferred list.
85  *
86  * @param[in] list_erase String containing a list of cleared partitions. Like this "nvs, phy". The string must be null-terminal.
87  * @param[in] ota_data_erase If true then the OTA data partition will be cleared (if there is it in partition table).
88  * @return    Returns true on success, false otherwise.
89  */
90 bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase);
91 
92 /**
93  * @brief Determines if the list contains the label
94  *
95  * @param[in] list  A string of names delimited by commas or spaces. Like this "nvs, phy, data". The string must be null-terminated.
96  * @param[in] label The substring that will be searched in the list.
97  * @return    Returns true if the list contains the label, false otherwise.
98  */
99 bool bootloader_common_label_search(const char *list, char *label);
100 
101 /**
102  * @brief Configure default SPI pin modes and drive strengths
103  *
104  * @param drv GPIO drive level (determined by clock frequency)
105  */
106 void bootloader_configure_spi_pins(int drv);
107 
108 /**
109  * @brief Calculates a sha-256 for a given partition or returns a appended digest.
110  *
111  * This function can be used to return the SHA-256 digest of application, bootloader and data partitions.
112  * For apps with SHA-256 appended to the app image, the result is the appended SHA-256 value for the app image content.
113  * The hash is verified before returning, if app content is invalid then the function returns ESP_ERR_IMAGE_INVALID.
114  * For apps without SHA-256 appended to the image, the result is the SHA-256 of all bytes in the app image.
115  * For other partition types, the result is the SHA-256 of the entire partition.
116  *
117  * @param[in]  address      Address of partition.
118  * @param[in]  size         Size of partition.
119  * @param[in]  type         Type of partition. For applications the type is 0, otherwise type is data.
120  * @param[out] out_sha_256  Returned SHA-256 digest for a given partition.
121  *
122  * @return
123  *          - ESP_OK: In case of successful operation.
124  *          - ESP_ERR_INVALID_ARG: The size was 0 or the sha_256 was NULL.
125  *          - ESP_ERR_NO_MEM: Cannot allocate memory for sha256 operation.
126  *          - ESP_ERR_IMAGE_INVALID: App partition doesn't contain a valid app image.
127  *          - ESP_FAIL: An allocation error occurred.
128  */
129 esp_err_t bootloader_common_get_sha256_of_partition(uint32_t address, uint32_t size, int type, uint8_t *out_sha_256);
130 
131 /**
132  * @brief Returns the number of active otadata.
133  *
134  * @param[in] two_otadata Pointer on array from two otadata structures.
135  *
136  * @return The number of active otadata (0 or 1).
137  *        - -1: If it does not have active otadata.
138  */
139 int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata);
140 
141 /**
142  * @brief Returns the number of active otadata.
143  *
144  * @param[in] two_otadata       Pointer on array from two otadata structures.
145  * @param[in] valid_two_otadata Pointer on array from two bools. True means select.
146  * @param[in] max               True - will select the maximum ota_seq number, otherwise the minimum.
147  *
148  * @return The number of active otadata (0 or 1).
149  *        - -1: If it does not have active otadata.
150  */
151 int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max);
152 
153 /**
154  * @brief Returns esp_app_desc structure for app partition. This structure includes app version.
155  *
156  * Returns a description for the requested app partition.
157  * @param[in] partition      App partition description.
158  * @param[out] app_desc      Structure of info about app.
159  * @return
160  *  - ESP_OK:                Successful.
161  *  - ESP_ERR_INVALID_ARG:   The arguments passed are not valid.
162  *  - ESP_ERR_NOT_FOUND:     app_desc structure is not found. Magic word is incorrect.
163  *  - ESP_FAIL:              mapping is fail.
164  */
165 esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc);
166 
167 /**
168  * @brief Get chip revision
169  *
170  * @return Chip revision number
171  */
172 uint8_t bootloader_common_get_chip_revision(void);
173 
174 /**
175  * @brief Get chip package
176  *
177  * @return Chip package number
178  */
179 uint32_t bootloader_common_get_chip_ver_pkg(void);
180 
181 /**
182  * @brief Query reset reason
183  *
184  * @param cpu_no CPU number
185  * @return reset reason enumeration
186  */
187 RESET_REASON bootloader_common_get_reset_reason(int cpu_no);
188 
189 /**
190  * @brief Check if the image (bootloader and application) has valid chip ID and revision
191  *
192  * @param[in] img_hdr: image header
193  * @param[in] type: image type, bootloader or application
194  * @return
195  *      - ESP_OK: image and chip are matched well
196  *      - ESP_FAIL: image doesn't match to the chip
197  */
198 esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type);
199 
200 /**
201  * @brief Configure VDDSDIO, call this API to rise VDDSDIO to 1.9V when VDDSDIO regulator is enabled as 1.8V mode.
202  */
203 void bootloader_common_vddsdio_configure(void);
204 
205 #if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC )
206 /**
207  * @brief Returns partition from rtc_retain_mem
208  *
209  * Uses to get the partition of application which was worked before to go to the deep sleep.
210  * This partition was stored in rtc_retain_mem.
211  * Note: This function operates the RTC FAST memory which available only for PRO_CPU.
212  *       Make sure that this function is used only PRO_CPU.
213  *
214  * @return partition: If rtc_retain_mem is valid.
215  *        - NULL: If it is not valid.
216  */
217 esp_partition_pos_t* bootloader_common_get_rtc_retain_mem_partition(void);
218 
219 /**
220  * @brief Update the partition and reboot_counter in rtc_retain_mem.
221  *
222  * This function saves the partition of application for fast booting from the deep sleep.
223  * An algorithm uses this partition to avoid reading the otadata and does not validate an image.
224  * Note: This function operates the RTC FAST memory which available only for PRO_CPU.
225  *       Make sure that this function is used only PRO_CPU.
226  *
227  * @param[in] partition      App partition description. Can be NULL, in this case rtc_retain_mem.partition is not updated.
228  * @param[in] reboot_counter If true then update reboot_counter.
229  *
230  */
231 void bootloader_common_update_rtc_retain_mem(esp_partition_pos_t* partition, bool reboot_counter);
232 
233 /**
234  * @brief Reset entire rtc_retain_mem.
235  *
236  * Note: This function operates the RTC FAST memory which available only for PRO_CPU.
237  *       Make sure that this function is used only PRO_CPU.
238  */
239 void bootloader_common_reset_rtc_retain_mem(void);
240 
241 /**
242  * @brief Returns reboot_counter from rtc_retain_mem
243  *
244  * The reboot_counter counts the number of reboots. Reset only when power is off.
245  * The very first launch of the application will be from 1.
246  * Overflow is not possible, it will stop at the value UINT16_MAX.
247  * Note: This function operates the RTC FAST memory which available only for PRO_CPU.
248  *       Make sure that this function is used only PRO_CPU.
249  *
250  * @return reboot_counter: 1..65535
251  *         - 0: If rtc_retain_mem is not valid.
252  */
253 uint16_t bootloader_common_get_rtc_retain_mem_reboot_counter(void);
254 
255 /**
256  * @brief Returns rtc_retain_mem
257  *
258  * Note: This function operates the RTC FAST memory which available only for PRO_CPU.
259  *       Make sure that this function is used only PRO_CPU.
260  *
261  * @return rtc_retain_mem
262  */
263 rtc_retain_mem_t* bootloader_common_get_rtc_retain_mem(void);
264 
265 #endif
266 
267 #ifdef __cplusplus
268 }
269 #endif
270