bootloader_common.h 11 KB

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