esp_https_ota.h 11 KB

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