esp_https_ota.h 11 KB

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