esp_https_ota.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <esp_http_client.h>
  8. #include <bootloader_common.h>
  9. #include "esp_app_desc.h"
  10. #include <sdkconfig.h>
  11. #include "esp_event.h"
  12. #include "esp_partition.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. ESP_EVENT_DECLARE_BASE(ESP_HTTPS_OTA_EVENT);
  17. /**
  18. * @brief Events generated by OTA process
  19. *
  20. * @note Expected data type for different OTA events:
  21. * - ESP_HTTPS_OTA_START : NULL
  22. * - ESP_HTTPS_OTA_CONNECTED : NULL
  23. * - ESP_HTTPS_OTA_GET_IMG_DESC : NULL
  24. * - ESP_HTTPS_OTA_VERIFY_CHIP_ID : esp_chip_id_t
  25. * - ESP_HTTPS_OTA_DECRYPT_CB : NULL
  26. * - ESP_HTTPS_OTA_WRITE_FLASH : int
  27. * - ESP_HTTPS_OTA_UPDATE_BOOT_PARTITION : esp_partition_subtype_t
  28. * - ESP_HTTPS_OTA_FINISH : NULL
  29. * - ESP_HTTPS_OTA_ABORT : NULL
  30. */
  31. typedef enum {
  32. ESP_HTTPS_OTA_START, /*!< OTA started */
  33. ESP_HTTPS_OTA_CONNECTED, /*!< Connected to server */
  34. ESP_HTTPS_OTA_GET_IMG_DESC, /*!< Read app description from image header */
  35. ESP_HTTPS_OTA_VERIFY_CHIP_ID, /*!< Verify chip id of new image */
  36. ESP_HTTPS_OTA_DECRYPT_CB, /*!< Callback to decrypt function */
  37. ESP_HTTPS_OTA_WRITE_FLASH, /*!< Flash write operation */
  38. ESP_HTTPS_OTA_UPDATE_BOOT_PARTITION, /*!< Boot partition update after successful ota update */
  39. ESP_HTTPS_OTA_FINISH, /*!< OTA finished */
  40. ESP_HTTPS_OTA_ABORT, /*!< OTA aborted */
  41. } esp_https_ota_event_t;
  42. typedef void *esp_https_ota_handle_t;
  43. typedef esp_err_t(*http_client_init_cb_t)(esp_http_client_handle_t);
  44. #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  45. typedef struct {
  46. const char *data_in; /*!< Pointer to data to be decrypted */
  47. size_t data_in_len; /*!< Input data length */
  48. char *data_out; /*!< Pointer to data decrypted using callback, this will be freed after data is written to flash */
  49. size_t data_out_len; /*!< Output data length */
  50. } decrypt_cb_arg_t;
  51. typedef esp_err_t(*decrypt_cb_t)(decrypt_cb_arg_t *args, void *user_ctx);
  52. #endif // CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  53. /**
  54. * @brief ESP HTTPS OTA configuration
  55. */
  56. typedef struct {
  57. const esp_http_client_config_t *http_config; /*!< ESP HTTP client configuration */
  58. http_client_init_cb_t http_client_init_cb; /*!< Callback after ESP HTTP client is initialised */
  59. bool bulk_flash_erase; /*!< Erase entire flash partition during initialization. By default flash partition is erased during write operation and in chunk of 4K sector size */
  60. bool partial_http_download; /*!< Enable Firmware image to be downloaded over multiple HTTP requests */
  61. int max_http_request_size; /*!< Maximum request size for partial HTTP download */
  62. #if CONFIG_ESP_HTTPS_OTA_DECRYPT_CB
  63. decrypt_cb_t decrypt_cb; /*!< Callback for external decryption layer */
  64. void *decrypt_user_ctx; /*!< User context for external decryption layer */
  65. #endif
  66. } esp_https_ota_config_t;
  67. #define ESP_ERR_HTTPS_OTA_BASE (0x9000)
  68. #define ESP_ERR_HTTPS_OTA_IN_PROGRESS (ESP_ERR_HTTPS_OTA_BASE + 1) /* OTA operation in progress */
  69. /**
  70. * @brief HTTPS OTA Firmware upgrade.
  71. *
  72. * This function allocates HTTPS OTA Firmware upgrade context, establishes HTTPS connection,
  73. * reads image data from HTTP stream and writes it to OTA partition and
  74. * finishes HTTPS OTA Firmware upgrade operation.
  75. * This API supports URL redirection, but if CA cert of URLs differ then it
  76. * should be appended to `cert_pem` member of `ota_config->http_config`.
  77. *
  78. * @param[in] ota_config pointer to esp_https_ota_config_t structure.
  79. *
  80. * @note This API handles the entire OTA operation, so if this API is being used
  81. * then no other APIs from `esp_https_ota` component should be called.
  82. * If more information and control is needed during the HTTPS OTA process,
  83. * then one can use `esp_https_ota_begin` and subsequent APIs. If this API returns
  84. * successfully, esp_restart() must be called to boot from the new firmware image.
  85. *
  86. * @return
  87. * - ESP_OK: OTA data updated, next reboot will use specified partition.
  88. * - ESP_FAIL: For generic failure.
  89. * - ESP_ERR_INVALID_ARG: Invalid argument
  90. * - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
  91. * - ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
  92. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  93. * - For other return codes, refer OTA documentation in esp-idf's app_update component.
  94. */
  95. esp_err_t esp_https_ota(const esp_https_ota_config_t *ota_config);
  96. /**
  97. * @brief Start HTTPS OTA Firmware upgrade
  98. *
  99. * This function initializes ESP HTTPS OTA context and establishes HTTPS connection.
  100. * This function must be invoked first. If this function returns successfully, then `esp_https_ota_perform` should be
  101. * called to continue with the OTA process and there should be a call to `esp_https_ota_finish` on
  102. * completion of OTA operation or on failure in subsequent operations.
  103. * This API supports URL redirection, but if CA cert of URLs differ then it
  104. * should be appended to `cert_pem` member of `http_config`, which is a part of `ota_config`.
  105. * In case of error, this API explicitly sets `handle` to NULL.
  106. *
  107. * @param[in] ota_config pointer to esp_https_ota_config_t structure
  108. * @param[out] handle pointer to an allocated data of type `esp_https_ota_handle_t`
  109. * which will be initialised in this function
  110. *
  111. * @note This API is blocking, so setting `is_async` member of `http_config` structure will
  112. * result in an error.
  113. *
  114. * @return
  115. * - ESP_OK: HTTPS OTA Firmware upgrade context initialised and HTTPS connection established
  116. * - ESP_FAIL: For generic failure.
  117. * - ESP_ERR_INVALID_ARG: Invalid argument (missing/incorrect config, certificate, etc.)
  118. * - For other return codes, refer documentation in app_update component and esp_http_client
  119. * component in esp-idf.
  120. */
  121. esp_err_t esp_https_ota_begin(const esp_https_ota_config_t *ota_config, esp_https_ota_handle_t *handle);
  122. /**
  123. * @brief Read image data from HTTP stream and write it to OTA partition
  124. *
  125. * This function reads image data from HTTP stream and writes it to OTA partition. This function
  126. * must be called only if esp_https_ota_begin() returns successfully.
  127. * This function must be called in a loop since it returns after every HTTP read operation thus
  128. * giving you the flexibility to stop OTA operation midway.
  129. *
  130. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  131. *
  132. * @return
  133. * - ESP_ERR_HTTPS_OTA_IN_PROGRESS: OTA update is in progress, call this API again to continue.
  134. * - ESP_OK: OTA update was successful
  135. * - ESP_FAIL: OTA update failed
  136. * - ESP_ERR_INVALID_ARG: Invalid argument
  137. * - ESP_ERR_INVALID_VERSION: Invalid chip revision in image header
  138. * - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
  139. * - ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
  140. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  141. * - For other return codes, refer OTA documentation in esp-idf's app_update component.
  142. */
  143. esp_err_t esp_https_ota_perform(esp_https_ota_handle_t https_ota_handle);
  144. /**
  145. * @brief Checks if complete data was received or not
  146. *
  147. * @note This API can be called just before esp_https_ota_finish() to validate if the complete image was indeed received.
  148. *
  149. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  150. *
  151. * @return
  152. * - false
  153. * - true
  154. */
  155. bool esp_https_ota_is_complete_data_received(esp_https_ota_handle_t https_ota_handle);
  156. /**
  157. * @brief Clean-up HTTPS OTA Firmware upgrade and close HTTPS connection
  158. *
  159. * This function closes the HTTP connection and frees the ESP HTTPS OTA context.
  160. * This function switches the boot partition to the OTA partition containing the
  161. * new firmware image.
  162. *
  163. * @note If this API returns successfully, esp_restart() must be called to
  164. * boot from the new firmware image
  165. * esp_https_ota_finish should not be called after calling esp_https_ota_abort
  166. *
  167. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  168. *
  169. * @return
  170. * - ESP_OK: Clean-up successful
  171. * - ESP_ERR_INVALID_STATE
  172. * - ESP_ERR_INVALID_ARG: Invalid argument
  173. * - ESP_ERR_OTA_VALIDATE_FAILED: Invalid app image
  174. */
  175. esp_err_t esp_https_ota_finish(esp_https_ota_handle_t https_ota_handle);
  176. /**
  177. * @brief Clean-up HTTPS OTA Firmware upgrade and close HTTPS connection
  178. *
  179. * This function closes the HTTP connection and frees the ESP HTTPS OTA context.
  180. *
  181. * @note esp_https_ota_abort should not be called after calling esp_https_ota_finish
  182. *
  183. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  184. *
  185. * @return
  186. * - ESP_OK: Clean-up successful
  187. * - ESP_ERR_INVALID_STATE: Invalid ESP HTTPS OTA state
  188. * - ESP_FAIL: OTA not started
  189. * - ESP_ERR_NOT_FOUND: OTA handle not found
  190. * - ESP_ERR_INVALID_ARG: Invalid argument
  191. */
  192. esp_err_t esp_https_ota_abort(esp_https_ota_handle_t https_ota_handle);
  193. /**
  194. * @brief Reads app description from image header. The app description provides information
  195. * like the "Firmware version" of the image.
  196. *
  197. * @note This API can be called only after esp_https_ota_begin() and before esp_https_ota_perform().
  198. * Calling this API is not mandatory.
  199. *
  200. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  201. * @param[out] new_app_info pointer to an allocated esp_app_desc_t structure
  202. *
  203. * @return
  204. * - ESP_ERR_INVALID_ARG: Invalid arguments
  205. * - ESP_ERR_INVALID_STATE: Invalid state to call this API. esp_https_ota_begin() not called yet.
  206. * - ESP_FAIL: Failed to read image descriptor
  207. * - ESP_OK: Successfully read image descriptor
  208. */
  209. esp_err_t esp_https_ota_get_img_desc(esp_https_ota_handle_t https_ota_handle, esp_app_desc_t *new_app_info);
  210. /**
  211. * @brief This function returns OTA image data read so far.
  212. *
  213. * @note This API should be called only if `esp_https_ota_perform()` has been called atleast once or
  214. * if `esp_https_ota_get_img_desc` has been called before.
  215. *
  216. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  217. *
  218. * @return
  219. * - -1 On failure
  220. * - total bytes read so far
  221. */
  222. int esp_https_ota_get_image_len_read(esp_https_ota_handle_t https_ota_handle);
  223. /**
  224. * @brief This function returns OTA image total size.
  225. *
  226. * @note This API should be called after esp_https_ota_begin() has been already called.
  227. * This can be used to create some sort of progress indication
  228. * (in combination with esp_https_ota_get_image_len_read())
  229. *
  230. * @param[in] https_ota_handle pointer to esp_https_ota_handle_t structure
  231. *
  232. * @return
  233. * - -1 On failure or chunked encoding
  234. * - total bytes of image
  235. */
  236. int esp_https_ota_get_image_size(esp_https_ota_handle_t https_ota_handle);
  237. #ifdef __cplusplus
  238. }
  239. #endif