bootloader_common.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2023 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. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. // Type of hold a GPIO in low state
  13. typedef enum {
  14. GPIO_LONG_HOLD = 1, /*!< The long hold GPIO */
  15. GPIO_SHORT_HOLD = -1, /*!< The short hold GPIO */
  16. GPIO_NOT_HOLD = 0 /*!< If the GPIO input is not low */
  17. } esp_comm_gpio_hold_t;
  18. typedef enum {
  19. ESP_IMAGE_BOOTLOADER,
  20. ESP_IMAGE_APPLICATION
  21. } esp_image_type;
  22. /**
  23. * @brief Read ota_info partition and fill array from two otadata structures.
  24. *
  25. * @param[in] ota_info It is a pointer to the OTA data partition.
  26. * The "otadata" partition (Type = "data" and SubType = "ota")
  27. * is defined in the CSV partition table.
  28. * @param[out] two_otadata Pointer to array of OTA selection structure.
  29. * @return - ESP_OK: On success
  30. * - ESP_ERR_NOT_FOUND: Partition table does not have otadata partition
  31. * - ESP_FAIL: On failure
  32. */
  33. esp_err_t bootloader_common_read_otadata(const esp_partition_pos_t *ota_info, esp_ota_select_entry_t *two_otadata);
  34. /**
  35. * @brief Calculate crc for the OTA data select.
  36. *
  37. * @param[in] s The OTA data select.
  38. * @return Returns crc value.
  39. */
  40. uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s);
  41. /**
  42. * @brief Verifies the validity of the OTA data select
  43. *
  44. * @param[in] s The OTA data select.
  45. * @return Returns true on valid, false otherwise.
  46. */
  47. bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s);
  48. /**
  49. * @brief Returns true if OTADATA is not marked as bootable partition.
  50. *
  51. * @param[in] s The OTA data select.
  52. * @return Returns true if OTADATA invalid, false otherwise.
  53. */
  54. bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s);
  55. /**
  56. * @brief Check if a GPIO input is held low for a long period, short period, or not
  57. * at all.
  58. *
  59. * This function will configure the specified GPIO as an input with internal pull-up enabled.
  60. *
  61. * If the GPIO input is held low continuously for delay_sec period then it is a long hold.
  62. * If the GPIO input is held low for less period then it is a short hold.
  63. *
  64. * @param[in] num_pin Number of the GPIO input.
  65. * @param[in] delay_sec Input must be driven low for at least this long, continuously.
  66. * @return esp_comm_gpio_hold_t Type of low level hold detected, if any.
  67. */
  68. esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec);
  69. /**
  70. * @brief Check if a GPIO input is held low or high for a long period, short period, or not
  71. * at all.
  72. *
  73. * This function will configure the specified GPIO as an input with internal pull-up enabled.
  74. *
  75. * If the GPIO input is held at 'level' continuously for delay_sec period then it is a long hold.
  76. * If the GPIO input is held at 'level' for less period then it is a short hold.
  77. *
  78. * @param[in] num_pin Number of the GPIO input.
  79. * @param[in] delay_sec Input must be driven to 'level' for at least this long, continuously.
  80. * @param[in] level Input pin level to trigger on hold
  81. * @return esp_comm_gpio_hold_t Type of hold detected, if any.
  82. */
  83. esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio_level(uint32_t num_pin, uint32_t delay_sec, bool level);
  84. /**
  85. * @brief Erase the partition data that is specified in the transferred list.
  86. *
  87. * @param[in] list_erase String containing a list of cleared partitions. Like this "nvs, phy". The string must be null-terminal.
  88. * @param[in] ota_data_erase If true then the OTA data partition will be cleared (if there is it in partition table).
  89. * @return Returns true on success, false otherwise.
  90. */
  91. bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase);
  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. * @brief Configure default SPI pin modes and drive strengths
  102. *
  103. * @param drv GPIO drive level (determined by clock frequency)
  104. */
  105. void bootloader_configure_spi_pins(int drv);
  106. /**
  107. * @brief Calculates a sha-256 for a given partition or returns a appended digest.
  108. *
  109. * This function can be used to return the SHA-256 digest of application, bootloader and data partitions.
  110. * For apps with SHA-256 appended to the app image, the result is the appended SHA-256 value for the app image content.
  111. * The hash is verified before returning, if app content is invalid then the function returns ESP_ERR_IMAGE_INVALID.
  112. * For apps without SHA-256 appended to the image, the result is the SHA-256 of all bytes in the app image.
  113. * For other partition types, the result is the SHA-256 of the entire partition.
  114. *
  115. * @param[in] address Address of partition.
  116. * @param[in] size Size of partition.
  117. * @param[in] type Type of partition. For applications the type is 0, otherwise type is data.
  118. * @param[out] out_sha_256 Returned SHA-256 digest for a given partition.
  119. *
  120. * @return
  121. * - ESP_OK: In case of successful operation.
  122. * - ESP_ERR_INVALID_ARG: The size was 0 or the sha_256 was NULL.
  123. * - ESP_ERR_NO_MEM: Cannot allocate memory for sha256 operation.
  124. * - ESP_ERR_IMAGE_INVALID: App partition doesn't contain a valid app image.
  125. * - ESP_FAIL: An allocation error occurred.
  126. */
  127. esp_err_t bootloader_common_get_sha256_of_partition(uint32_t address, uint32_t size, int type, uint8_t *out_sha_256);
  128. /**
  129. * @brief Returns the number of active otadata.
  130. *
  131. * @param[in] two_otadata Pointer on array from two otadata structures.
  132. *
  133. * @return The number of active otadata (0 or 1).
  134. * - -1: If it does not have active otadata.
  135. */
  136. int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata);
  137. /**
  138. * @brief Returns the number of active otadata.
  139. *
  140. * @param[in] two_otadata Pointer on array from two otadata structures.
  141. * @param[in] valid_two_otadata Pointer on array from two bools. True means select.
  142. * @param[in] max True - will select the maximum ota_seq number, otherwise the minimum.
  143. *
  144. * @return The number of active otadata (0 or 1).
  145. * - -1: If it does not have active otadata.
  146. */
  147. int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max);
  148. /**
  149. * @brief Get chip package
  150. *
  151. * @return Chip package number
  152. */
  153. uint32_t bootloader_common_get_chip_ver_pkg(void);
  154. /**
  155. * @brief Check if the image (bootloader and application) has valid chip ID and revision
  156. *
  157. * @param[in] img_hdr: image header
  158. * @param[in] type: image type, bootloader or application
  159. * @return
  160. * - ESP_OK: image and chip are matched well
  161. * - ESP_FAIL: image doesn't match to the chip
  162. */
  163. esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type);
  164. /**
  165. * @brief Configure VDDSDIO, call this API to rise VDDSDIO to 1.9V when VDDSDIO regulator is enabled as 1.8V mode.
  166. */
  167. void bootloader_common_vddsdio_configure(void);
  168. #if CONFIG_BOOTLOADER_RESERVE_RTC_MEM
  169. /**
  170. * @brief Returns partition from rtc_retain_mem
  171. *
  172. * Uses to get the partition of application which was worked before to go to the deep sleep.
  173. * This partition was stored in rtc_retain_mem.
  174. * Note: This function operates the RTC FAST memory which available only for PRO_CPU.
  175. * Make sure that this function is used only PRO_CPU.
  176. *
  177. * @return partition: If rtc_retain_mem is valid.
  178. * - NULL: If it is not valid.
  179. */
  180. esp_partition_pos_t* bootloader_common_get_rtc_retain_mem_partition(void);
  181. /**
  182. * @brief Update the partition and reboot_counter in rtc_retain_mem.
  183. *
  184. * This function saves the partition of application for fast booting from the deep sleep.
  185. * An algorithm uses this partition to avoid reading the otadata and does not validate an image.
  186. * Note: This function operates the RTC FAST memory which available only for PRO_CPU.
  187. * Make sure that this function is used only PRO_CPU.
  188. *
  189. * @param[in] partition App partition description. Can be NULL, in this case rtc_retain_mem.partition is not updated.
  190. * @param[in] reboot_counter If true then update reboot_counter.
  191. *
  192. */
  193. void bootloader_common_update_rtc_retain_mem(esp_partition_pos_t* partition, bool reboot_counter);
  194. /**
  195. * @brief Reset entire rtc_retain_mem.
  196. *
  197. * Note: This function operates the RTC FAST memory which available only for PRO_CPU.
  198. * Make sure that this function is used only PRO_CPU.
  199. */
  200. void bootloader_common_reset_rtc_retain_mem(void);
  201. /**
  202. * @brief Returns reboot_counter from rtc_retain_mem
  203. *
  204. * The reboot_counter counts the number of reboots. Reset only when power is off.
  205. * The very first launch of the application will be from 1.
  206. * Overflow is not possible, it will stop at the value UINT16_MAX.
  207. * Note: This function operates the RTC FAST memory which available only for PRO_CPU.
  208. * Make sure that this function is used only PRO_CPU.
  209. *
  210. * @return reboot_counter: 1..65535
  211. * - 0: If rtc_retain_mem is not valid.
  212. */
  213. uint16_t bootloader_common_get_rtc_retain_mem_reboot_counter(void);
  214. /**
  215. * @brief Returns True if Factory reset has happened
  216. *
  217. * Reset the status after reading it.
  218. *
  219. * @return True: Factory reset has happened
  220. * False: No Factory reset
  221. */
  222. bool bootloader_common_get_rtc_retain_mem_factory_reset_state(void);
  223. /**
  224. * @brief Sets Factory reset status
  225. */
  226. void bootloader_common_set_rtc_retain_mem_factory_reset_state(void);
  227. /**
  228. * @brief Returns rtc_retain_mem
  229. *
  230. * Note: This function operates the RTC FAST memory which available only for PRO_CPU.
  231. * Make sure that this function is used only PRO_CPU.
  232. *
  233. * @return rtc_retain_mem
  234. */
  235. rtc_retain_mem_t* bootloader_common_get_rtc_retain_mem(void);
  236. #endif // CONFIG_BOOTLOADER_RESERVE_RTC_MEM
  237. #ifdef __cplusplus
  238. }
  239. #endif