bootloader_common.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_data_types.h"
  16. #include "esp_image_format.h"
  17. #include "esp_image_format.h"
  18. /// Type of hold a GPIO in low state
  19. typedef enum {
  20. GPIO_LONG_HOLD = 1, /*!< The long hold GPIO */
  21. GPIO_SHORT_HOLD = -1, /*!< The short hold GPIO */
  22. GPIO_NOT_HOLD = 0 /*!< If the GPIO input is not low */
  23. } esp_comm_gpio_hold_t;
  24. typedef enum {
  25. ESP_IMAGE_BOOTLOADER,
  26. ESP_IMAGE_APPLICATION
  27. } esp_image_type;
  28. /**
  29. * @brief Calculate crc for the OTA data select.
  30. *
  31. * @param[in] s The OTA data select.
  32. * @return Returns crc value.
  33. */
  34. uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s);
  35. /**
  36. * @brief Verifies the validity of the OTA data select
  37. *
  38. * @param[in] s The OTA data select.
  39. * @return Returns true on valid, false otherwise.
  40. */
  41. bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s);
  42. /**
  43. * @brief Returns true if OTADATA is not marked as bootable partition.
  44. *
  45. * @param[in] s The OTA data select.
  46. * @return Returns true if OTADATA invalid, false otherwise.
  47. */
  48. bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s);
  49. /**
  50. * @brief Check if the GPIO input is a long hold or a short hold.
  51. *
  52. * Number of the GPIO input will be configured as an input with internal pull-up enabled.
  53. * If the GPIO input is held low continuously for delay_sec period then it is a long hold.
  54. * If the GPIO input is held low for less period then it is a short hold.
  55. *
  56. * @param[in] num_pin Number of the GPIO input.
  57. * @param[in] delay_sec Input must be driven low for at least this long, continuously.
  58. * @return esp_comm_gpio_hold_t Defines type of hold a GPIO in low state.
  59. */
  60. esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec);
  61. /**
  62. * @brief Erase the partition data that is specified in the transferred list.
  63. *
  64. * @param[in] list_erase String containing a list of cleared partitions. Like this "nvs, phy". The string must be null-terminal.
  65. * @param[in] ota_data_erase If true then the OTA data partition will be cleared (if there is it in partition table).
  66. * @return Returns true on success, false otherwise.
  67. */
  68. bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_data_erase);
  69. /**
  70. * @brief Determines if the list contains the label
  71. *
  72. * @param[in] list A string of names delimited by commas or spaces. Like this "nvs, phy, data". The string must be null-terminated.
  73. * @param[in] label The substring that will be searched in the list.
  74. * @return Returns true if the list contains the label, false otherwise.
  75. */
  76. bool bootloader_common_label_search(const char *list, char *label);
  77. /**
  78. * @brief Calculates a sha-256 for a given partition or returns a appended digest.
  79. *
  80. * This function can be used to return the SHA-256 digest of application, bootloader and data partitions.
  81. * For apps with SHA-256 appended to the app image, the result is the appended SHA-256 value for the app image content.
  82. * The hash is verified before returning, if app content is invalid then the function returns ESP_ERR_IMAGE_INVALID.
  83. * For apps without SHA-256 appended to the image, the result is the SHA-256 of all bytes in the app image.
  84. * For other partition types, the result is the SHA-256 of the entire partition.
  85. *
  86. * @param[in] address Address of partition.
  87. * @param[in] size Size of partition.
  88. * @param[in] type Type of partition. For applications the type is 0, otherwise type is data.
  89. * @param[out] out_sha_256 Returned SHA-256 digest for a given partition.
  90. *
  91. * @return
  92. * - ESP_OK: In case of successful operation.
  93. * - ESP_ERR_INVALID_ARG: The size was 0 or the sha_256 was NULL.
  94. * - ESP_ERR_NO_MEM: Cannot allocate memory for sha256 operation.
  95. * - ESP_ERR_IMAGE_INVALID: App partition doesn't contain a valid app image.
  96. * - ESP_FAIL: An allocation error occurred.
  97. */
  98. esp_err_t bootloader_common_get_sha256_of_partition(uint32_t address, uint32_t size, int type, uint8_t *out_sha_256);
  99. /**
  100. * @brief Returns the number of active otadata.
  101. *
  102. * @param[in] two_otadata Pointer on array from two otadata structures.
  103. *
  104. * @return The number of active otadata (0 or 1).
  105. * - -1: If it does not have active otadata.
  106. */
  107. int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata);
  108. /**
  109. * @brief Returns the number of active otadata.
  110. *
  111. * @param[in] two_otadata Pointer on array from two otadata structures.
  112. * @param[in] valid_two_otadata Pointer on array from two bools. True means select.
  113. * @param[in] max True - will select the maximum ota_seq number, otherwise the minimum.
  114. *
  115. * @return The number of active otadata (0 or 1).
  116. * - -1: If it does not have active otadata.
  117. */
  118. int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max);
  119. /**
  120. * @brief Returns esp_app_desc structure for app partition. This structure includes app version.
  121. *
  122. * Returns a description for the requested app partition.
  123. * @param[in] partition App partition description.
  124. * @param[out] app_desc Structure of info about app.
  125. * @return
  126. * - ESP_OK: Successful.
  127. * - ESP_ERR_INVALID_ARG: The arguments passed are not valid.
  128. * - ESP_ERR_NOT_FOUND: app_desc structure is not found. Magic word is incorrect.
  129. * - ESP_FAIL: mapping is fail.
  130. */
  131. esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc);
  132. /**
  133. * @brief Get chip revision
  134. *
  135. * @return Chip revision number
  136. */
  137. uint8_t bootloader_common_get_chip_revision(void);
  138. /**
  139. * @brief Check if the image (bootloader and application) has valid chip ID and revision
  140. *
  141. * @param[in] img_hdr: image header
  142. * @param[in] type: image type, bootloader or application
  143. * @return
  144. * - ESP_OK: image and chip are matched well
  145. * - ESP_FAIL: image doesn't match to the chip
  146. */
  147. esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type);
  148. /**
  149. * @brief Configure VDDSDIO, call this API to rise VDDSDIO to 1.9V when VDDSDIO regulator is enabled as 1.8V mode.
  150. */
  151. void bootloader_common_vddsdio_configure();