esp_ota_ops.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _OTA_OPS_H
  14. #define _OTA_OPS_H
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <stddef.h>
  18. #include "esp_err.h"
  19. #include "esp_partition.h"
  20. #include "esp_image_format.h"
  21. #include "esp_flash_data_types.h"
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. #define OTA_SIZE_UNKNOWN 0xffffffff /*!< Used for esp_ota_begin() if new image size is unknown */
  27. #define ESP_ERR_OTA_BASE 0x1500 /*!< Base error code for ota_ops api */
  28. #define ESP_ERR_OTA_PARTITION_CONFLICT (ESP_ERR_OTA_BASE + 0x01) /*!< Error if request was to write or erase the current running partition */
  29. #define ESP_ERR_OTA_SELECT_INFO_INVALID (ESP_ERR_OTA_BASE + 0x02) /*!< Error if OTA data partition contains invalid content */
  30. #define ESP_ERR_OTA_VALIDATE_FAILED (ESP_ERR_OTA_BASE + 0x03) /*!< Error if OTA app image is invalid */
  31. /**
  32. * @brief Opaque handle for an application OTA update
  33. *
  34. * esp_ota_begin() returns a handle which is then used for subsequent
  35. * calls to esp_ota_write() and esp_ota_end().
  36. */
  37. typedef uint32_t esp_ota_handle_t;
  38. /**
  39. * @brief Return esp_app_desc structure. This structure includes app version.
  40. *
  41. * Return description for running app.
  42. * @return Pointer to esp_app_desc structure.
  43. */
  44. const esp_app_desc_t *esp_ota_get_app_description(void);
  45. /**
  46. * @brief Commence an OTA update writing to the specified partition.
  47. * The specified partition is erased to the specified image size.
  48. *
  49. * If image size is not yet known, pass OTA_SIZE_UNKNOWN which will
  50. * cause the entire partition to be erased.
  51. *
  52. * On success, this function allocates memory that remains in use
  53. * until esp_ota_end() is called with the returned handle.
  54. *
  55. * @param partition Pointer to info for partition which will receive the OTA update. Required.
  56. * @param image_size Size of new OTA app image. Partition will be erased in order to receive this size of image. If 0 or OTA_SIZE_UNKNOWN, the entire partition is erased.
  57. * @param out_handle On success, returns a handle which should be used for subsequent esp_ota_write() and esp_ota_end() calls.
  58. * @return
  59. * - ESP_OK: OTA operation commenced successfully.
  60. * - ESP_ERR_INVALID_ARG: partition or out_handle arguments were NULL, or partition doesn't point to an OTA app partition.
  61. * - ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
  62. * - ESP_ERR_OTA_PARTITION_CONFLICT: Partition holds the currently running firmware, cannot update in place.
  63. * - ESP_ERR_NOT_FOUND: Partition argument not found in partition table.
  64. * - ESP_ERR_OTA_SELECT_INFO_INVALID: The OTA data partition contains invalid data.
  65. * - ESP_ERR_INVALID_SIZE: Partition doesn't fit in configured flash size.
  66. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  67. */
  68. esp_err_t esp_ota_begin(const esp_partition_t* partition, size_t image_size, esp_ota_handle_t* out_handle);
  69. /**
  70. * @brief Write OTA update data to partition
  71. *
  72. * This function can be called multiple times as
  73. * data is received during the OTA operation. Data is written
  74. * sequentially to the partition.
  75. *
  76. * @param handle Handle obtained from esp_ota_begin
  77. * @param data Data buffer to write
  78. * @param size Size of data buffer in bytes.
  79. *
  80. * @return
  81. * - ESP_OK: Data was written to flash successfully.
  82. * - ESP_ERR_INVALID_ARG: handle is invalid.
  83. * - ESP_ERR_OTA_VALIDATE_FAILED: First byte of image contains invalid app image magic byte.
  84. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  85. * - ESP_ERR_OTA_SELECT_INFO_INVALID: OTA data partition has invalid contents
  86. */
  87. esp_err_t esp_ota_write(esp_ota_handle_t handle, const void* data, size_t size);
  88. /**
  89. * @brief Finish OTA update and validate newly written app image.
  90. *
  91. * @param handle Handle obtained from esp_ota_begin().
  92. *
  93. * @note After calling esp_ota_end(), the handle is no longer valid and any memory associated with it is freed (regardless of result).
  94. *
  95. * @return
  96. * - ESP_OK: Newly written OTA app image is valid.
  97. * - ESP_ERR_NOT_FOUND: OTA handle was not found.
  98. * - ESP_ERR_INVALID_ARG: Handle was never written to.
  99. * - ESP_ERR_OTA_VALIDATE_FAILED: OTA image is invalid (either not a valid app image, or - if secure boot is enabled - signature failed to verify.)
  100. * - ESP_ERR_INVALID_STATE: If flash encryption is enabled, this result indicates an internal error writing the final encrypted bytes to flash.
  101. */
  102. esp_err_t esp_ota_end(esp_ota_handle_t handle);
  103. /**
  104. * @brief Configure OTA data for a new boot partition
  105. *
  106. * @note If this function returns ESP_OK, calling esp_restart() will boot the newly configured app partition.
  107. *
  108. * @param partition Pointer to info for partition containing app image to boot.
  109. *
  110. * @return
  111. * - ESP_OK: OTA data updated, next reboot will use specified partition.
  112. * - ESP_ERR_INVALID_ARG: partition argument was NULL or didn't point to a valid OTA partition of type "app".
  113. * - ESP_ERR_OTA_VALIDATE_FAILED: Partition contained invalid app image. Also returned if secure boot is enabled and signature validation failed.
  114. * - ESP_ERR_NOT_FOUND: OTA data partition not found.
  115. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash erase or write failed.
  116. */
  117. esp_err_t esp_ota_set_boot_partition(const esp_partition_t* partition);
  118. /**
  119. * @brief Get partition info of currently configured boot app
  120. *
  121. * If esp_ota_set_boot_partition() has been called, the partition which was set by that function will be returned.
  122. *
  123. * If esp_ota_set_boot_partition() has not been called, the result is usually the same as esp_ota_get_running_partition().
  124. * The two results are not equal if the configured boot partition does not contain a valid app (meaning that the running partition
  125. * will be an app that the bootloader chose via fallback).
  126. *
  127. * If the OTA data partition is not present or not valid then the result is the first app partition found in the
  128. * partition table. In priority order, this means: the factory app, the first OTA app slot, or the test app partition.
  129. *
  130. * Note that there is no guarantee the returned partition is a valid app. Use esp_image_verify(ESP_IMAGE_VERIFY, ...) to verify if the
  131. * returned partition contains a bootable image.
  132. *
  133. * @return Pointer to info for partition structure, or NULL if partition table is invalid or a flash read operation failed. Any returned pointer is valid for the lifetime of the application.
  134. */
  135. const esp_partition_t* esp_ota_get_boot_partition(void);
  136. /**
  137. * @brief Get partition info of currently running app
  138. *
  139. * This function is different to esp_ota_get_boot_partition() in that
  140. * it ignores any change of selected boot partition caused by
  141. * esp_ota_set_boot_partition(). Only the app whose code is currently
  142. * running will have its partition information returned.
  143. *
  144. * The partition returned by this function may also differ from esp_ota_get_boot_partition() if the configured boot
  145. * partition is somehow invalid, and the bootloader fell back to a different app partition at boot.
  146. *
  147. * @return Pointer to info for partition structure, or NULL if no partition is found or flash read operation failed. Returned pointer is valid for the lifetime of the application.
  148. */
  149. const esp_partition_t* esp_ota_get_running_partition(void);
  150. /**
  151. * @brief Return the next OTA app partition which should be written with a new firmware.
  152. *
  153. * Call this function to find an OTA app partition which can be passed to esp_ota_begin().
  154. *
  155. * Finds next partition round-robin, starting from the current running partition.
  156. *
  157. * @param start_from If set, treat this partition info as describing the current running partition. Can be NULL, in which case esp_ota_get_running_partition() is used to find the currently running partition. The result of this function is never the same as this argument.
  158. *
  159. * @return Pointer to info for partition which should be updated next. NULL result indicates invalid OTA data partition, or that no eligible OTA app slot partition was found.
  160. *
  161. */
  162. const esp_partition_t* esp_ota_get_next_update_partition(const esp_partition_t *start_from);
  163. /**
  164. * @brief Returns esp_app_desc structure for app partition. This structure includes app version.
  165. *
  166. * Returns a description for the requested app partition.
  167. * @param[in] partition Pointer to app partition. (only app partition)
  168. * @param[out] app_desc Structure of info about app.
  169. * @return
  170. * - ESP_OK Successful.
  171. * - ESP_ERR_NOT_FOUND app_desc structure is not found. Magic word is incorrect.
  172. * - ESP_ERR_NOT_SUPPORTED Partition is not application.
  173. * - ESP_ERR_INVALID_ARG Arguments is NULL or if partition's offset exceeds partition size.
  174. * - ESP_ERR_INVALID_SIZE Read would go out of bounds of the partition.
  175. * - or one of error codes from lower-level flash driver.
  176. */
  177. esp_err_t esp_ota_get_partition_description(const esp_partition_t *partition, esp_app_desc_t *app_desc);
  178. /**
  179. * @brief This function is called to indicate that the running app is working well.
  180. *
  181. * @return
  182. * - ESP_OK: if successful.
  183. */
  184. esp_err_t esp_ota_mark_app_valid_cancel_rollback();
  185. /**
  186. * @brief This function is called to roll back to the previously workable app with reboot.
  187. *
  188. * If rollback is successful then device will reset else API will return with error code.
  189. * @return
  190. * - ESP_FAIL: if not successful.
  191. */
  192. esp_err_t esp_ota_mark_app_invalid_rollback_and_reboot();
  193. /**
  194. * @brief Returns last partition with invalid state (ESP_OTA_IMG_INVALID or ESP_OTA_IMG_ABORTED).
  195. *
  196. * @return partition.
  197. */
  198. const esp_partition_t* esp_ota_get_last_invalid_partition();
  199. /**
  200. * @brief Returns state for given partition.
  201. *
  202. * @param[in] partition Pointer to partition.
  203. * @param[out] ota_state state of partition (if this partition has a record in otadata).
  204. * @return
  205. * - ESP_OK: Successful.
  206. * - ESP_ERR_INVALID_ARG: partition or ota_state arguments were NULL.
  207. * - ESP_ERR_NOT_SUPPORTED: partition is not ota.
  208. * - ESP_ERR_NOT_FOUND: Partition table does not have otadata or state was not found for given partition.
  209. */
  210. esp_err_t esp_ota_get_state_partition(const esp_partition_t *partition, esp_ota_img_states_t *ota_state);
  211. /**
  212. * @brief Erase previous boot app partition and corresponding otadata select for this partition.
  213. *
  214. * When current app is marked to as valid then you can erase previous app partition.
  215. * @return
  216. * - ESP_OK: Successful, otherwise ESP_ERR.
  217. */
  218. esp_err_t esp_ota_erase_last_boot_app_partition(void);
  219. #ifdef __cplusplus
  220. }
  221. #endif
  222. #endif /* OTA_OPS_H */