bootloader_common.h 9.7 KB

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