esp_ota_ops.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _OTA_OPS_H
  7. #define _OTA_OPS_H
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include <stddef.h>
  11. #include "esp_err.h"
  12. #include "esp_partition.h"
  13. #include "esp_app_desc.h"
  14. #include "esp_bootloader_desc.h"
  15. #include "esp_flash_partitions.h"
  16. #include "soc/soc_caps.h"
  17. #ifdef __cplusplus
  18. extern "C"
  19. {
  20. #endif
  21. #define OTA_SIZE_UNKNOWN 0xffffffff /*!< Used for esp_ota_begin() if new image size is unknown */
  22. #define OTA_WITH_SEQUENTIAL_WRITES 0xfffffffe /*!< Used for esp_ota_begin() if new image size is unknown and erase can be done in incremental manner (assuming write operation is in continuous sequence) */
  23. #define ESP_ERR_OTA_BASE 0x1500 /*!< Base error code for ota_ops api */
  24. #define ESP_ERR_OTA_PARTITION_CONFLICT (ESP_ERR_OTA_BASE + 0x01) /*!< Error if request was to write or erase the current running partition */
  25. #define ESP_ERR_OTA_SELECT_INFO_INVALID (ESP_ERR_OTA_BASE + 0x02) /*!< Error if OTA data partition contains invalid content */
  26. #define ESP_ERR_OTA_VALIDATE_FAILED (ESP_ERR_OTA_BASE + 0x03) /*!< Error if OTA app image is invalid */
  27. #define ESP_ERR_OTA_SMALL_SEC_VER (ESP_ERR_OTA_BASE + 0x04) /*!< Error if the firmware has a secure version less than the running firmware. */
  28. #define ESP_ERR_OTA_ROLLBACK_FAILED (ESP_ERR_OTA_BASE + 0x05) /*!< Error if flash does not have valid firmware in passive partition and hence rollback is not possible */
  29. #define ESP_ERR_OTA_ROLLBACK_INVALID_STATE (ESP_ERR_OTA_BASE + 0x06) /*!< Error if current active firmware is still marked in pending validation state (ESP_OTA_IMG_PENDING_VERIFY), essentially first boot of firmware image post upgrade and hence firmware upgrade is not possible */
  30. /**
  31. * @brief Opaque handle for an application OTA update
  32. *
  33. * esp_ota_begin() returns a handle which is then used for subsequent
  34. * calls to esp_ota_write() and esp_ota_end().
  35. */
  36. typedef uint32_t esp_ota_handle_t;
  37. /**
  38. * @brief Return esp_app_desc structure. This structure includes app version.
  39. *
  40. * @note This API is present for backward compatibility reasons. Alternative function
  41. * with the same functionality is `esp_app_get_description`
  42. *
  43. * Return description for running app.
  44. * @return Pointer to esp_app_desc structure.
  45. */
  46. const esp_app_desc_t *esp_ota_get_app_description(void) __attribute__((deprecated("Please use esp_app_get_description instead")));
  47. /**
  48. * @brief Fill the provided buffer with SHA256 of the ELF file, formatted as hexadecimal, null-terminated.
  49. * If the buffer size is not sufficient to fit the entire SHA256 in hex plus a null terminator,
  50. * the largest possible number of bytes will be written followed by a null.
  51. *
  52. * @note This API is present for backward compatibility reasons. Alternative function
  53. * with the same functionality is `esp_app_get_elf_sha256`
  54. *
  55. * @param dst Destination buffer
  56. * @param size Size of the buffer
  57. * @return Number of bytes written to dst (including null terminator)
  58. */
  59. int esp_ota_get_app_elf_sha256(char* dst, size_t size) __attribute__((deprecated("Please use esp_app_get_elf_sha256 instead")));
  60. /**
  61. * @brief Commence an OTA update writing to the specified partition.
  62. * The specified partition is erased to the specified image size.
  63. *
  64. * If image size is not yet known, pass OTA_SIZE_UNKNOWN which will
  65. * cause the entire partition to be erased.
  66. *
  67. * On success, this function allocates memory that remains in use
  68. * until esp_ota_end() is called with the returned handle.
  69. *
  70. * Note: If the rollback option is enabled and the running application has the ESP_OTA_IMG_PENDING_VERIFY state then
  71. * it will lead to the ESP_ERR_OTA_ROLLBACK_INVALID_STATE error. Confirm the running app before to run download a new app,
  72. * use esp_ota_mark_app_valid_cancel_rollback() function for it (this should be done as early as possible when you first download a new application).
  73. *
  74. * @param partition Pointer to info for partition which will receive the OTA update. Required.
  75. * @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.
  76. * @param out_handle On success, returns a handle which should be used for subsequent esp_ota_write() and esp_ota_end() calls.
  77. * @return
  78. * - ESP_OK: OTA operation commenced successfully.
  79. * - ESP_ERR_INVALID_ARG: partition or out_handle arguments were NULL, or partition doesn't point to an OTA app partition.
  80. * - ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
  81. * - ESP_ERR_OTA_PARTITION_CONFLICT: Partition holds the currently running firmware, cannot update in place.
  82. * - ESP_ERR_NOT_FOUND: Partition argument not found in partition table.
  83. * - ESP_ERR_OTA_SELECT_INFO_INVALID: The OTA data partition contains invalid data.
  84. * - ESP_ERR_INVALID_SIZE: Partition doesn't fit in configured flash size.
  85. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  86. * - ESP_ERR_OTA_ROLLBACK_INVALID_STATE: If the running app has not confirmed state. Before performing an update, the application must be valid.
  87. */
  88. esp_err_t esp_ota_begin(const esp_partition_t* partition, size_t image_size, esp_ota_handle_t* out_handle);
  89. /**
  90. * @brief Write OTA update data to partition
  91. *
  92. * This function can be called multiple times as
  93. * data is received during the OTA operation. Data is written
  94. * sequentially to the partition.
  95. *
  96. * @param handle Handle obtained from esp_ota_begin
  97. * @param data Data buffer to write
  98. * @param size Size of data buffer in bytes.
  99. *
  100. * @return
  101. * - ESP_OK: Data was written to flash successfully.
  102. * - ESP_ERR_INVALID_ARG: handle is invalid.
  103. * - ESP_ERR_OTA_VALIDATE_FAILED: First byte of image contains invalid app image magic byte.
  104. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  105. * - ESP_ERR_OTA_SELECT_INFO_INVALID: OTA data partition has invalid contents
  106. */
  107. esp_err_t esp_ota_write(esp_ota_handle_t handle, const void* data, size_t size);
  108. /**
  109. * @brief Write OTA update data to partition at an offset
  110. *
  111. * This function can write data in non-contiguous manner.
  112. * If flash encryption is enabled, data should be 16 bytes aligned.
  113. *
  114. * @param handle Handle obtained from esp_ota_begin
  115. * @param data Data buffer to write
  116. * @param size Size of data buffer in bytes
  117. * @param offset Offset in flash partition
  118. *
  119. * @note While performing OTA, if the packets arrive out of order, esp_ota_write_with_offset() can be used to write data in non-contiguous manner.
  120. * Use of esp_ota_write_with_offset() in combination with esp_ota_write() is not recommended.
  121. *
  122. * @return
  123. * - ESP_OK: Data was written to flash successfully.
  124. * - ESP_ERR_INVALID_ARG: handle is invalid.
  125. * - ESP_ERR_OTA_VALIDATE_FAILED: First byte of image contains invalid app image magic byte.
  126. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  127. * - ESP_ERR_OTA_SELECT_INFO_INVALID: OTA data partition has invalid contents
  128. */
  129. esp_err_t esp_ota_write_with_offset(esp_ota_handle_t handle, const void *data, size_t size, uint32_t offset);
  130. /**
  131. * @brief Finish OTA update and validate newly written app image.
  132. *
  133. * @param handle Handle obtained from esp_ota_begin().
  134. *
  135. * @note After calling esp_ota_end(), the handle is no longer valid and any memory associated with it is freed (regardless of result).
  136. *
  137. * @return
  138. * - ESP_OK: Newly written OTA app image is valid.
  139. * - ESP_ERR_NOT_FOUND: OTA handle was not found.
  140. * - ESP_ERR_INVALID_ARG: Handle was never written to.
  141. * - 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.)
  142. * - ESP_ERR_INVALID_STATE: If flash encryption is enabled, this result indicates an internal error writing the final encrypted bytes to flash.
  143. */
  144. esp_err_t esp_ota_end(esp_ota_handle_t handle);
  145. /**
  146. * @brief Abort OTA update, free the handle and memory associated with it.
  147. *
  148. * @param handle obtained from esp_ota_begin().
  149. *
  150. * @return
  151. * - ESP_OK: Handle and its associated memory is freed successfully.
  152. * - ESP_ERR_NOT_FOUND: OTA handle was not found.
  153. */
  154. esp_err_t esp_ota_abort(esp_ota_handle_t handle);
  155. /**
  156. * @brief Configure OTA data for a new boot partition
  157. *
  158. * @note If this function returns ESP_OK, calling esp_restart() will boot the newly configured app partition.
  159. *
  160. * @param partition Pointer to info for partition containing app image to boot.
  161. *
  162. * @return
  163. * - ESP_OK: OTA data updated, next reboot will use specified partition.
  164. * - ESP_ERR_INVALID_ARG: partition argument was NULL or didn't point to a valid OTA partition of type "app".
  165. * - ESP_ERR_OTA_VALIDATE_FAILED: Partition contained invalid app image. Also returned if secure boot is enabled and signature validation failed.
  166. * - ESP_ERR_NOT_FOUND: OTA data partition not found.
  167. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash erase or write failed.
  168. */
  169. esp_err_t esp_ota_set_boot_partition(const esp_partition_t* partition);
  170. /**
  171. * @brief Get partition info of currently configured boot app
  172. *
  173. * If esp_ota_set_boot_partition() has been called, the partition which was set by that function will be returned.
  174. *
  175. * If esp_ota_set_boot_partition() has not been called, the result is usually the same as esp_ota_get_running_partition().
  176. * The two results are not equal if the configured boot partition does not contain a valid app (meaning that the running partition
  177. * will be an app that the bootloader chose via fallback).
  178. *
  179. * If the OTA data partition is not present or not valid then the result is the first app partition found in the
  180. * partition table. In priority order, this means: the factory app, the first OTA app slot, or the test app partition.
  181. *
  182. * Note that there is no guarantee the returned partition is a valid app. Use esp_image_verify(ESP_IMAGE_VERIFY, ...) to verify if the
  183. * returned partition contains a bootable image.
  184. *
  185. * @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.
  186. */
  187. const esp_partition_t* esp_ota_get_boot_partition(void);
  188. /**
  189. * @brief Get partition info of currently running app
  190. *
  191. * This function is different to esp_ota_get_boot_partition() in that
  192. * it ignores any change of selected boot partition caused by
  193. * esp_ota_set_boot_partition(). Only the app whose code is currently
  194. * running will have its partition information returned.
  195. *
  196. * The partition returned by this function may also differ from esp_ota_get_boot_partition() if the configured boot
  197. * partition is somehow invalid, and the bootloader fell back to a different app partition at boot.
  198. *
  199. * @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.
  200. */
  201. const esp_partition_t* esp_ota_get_running_partition(void);
  202. /**
  203. * @brief Return the next OTA app partition which should be written with a new firmware.
  204. *
  205. * Call this function to find an OTA app partition which can be passed to esp_ota_begin().
  206. *
  207. * Finds next partition round-robin, starting from the current running partition.
  208. *
  209. * @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.
  210. *
  211. * @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.
  212. *
  213. */
  214. const esp_partition_t* esp_ota_get_next_update_partition(const esp_partition_t *start_from);
  215. /**
  216. * @brief Returns esp_app_desc structure for app partition. This structure includes app version.
  217. *
  218. * Returns a description for the requested app partition.
  219. * @param[in] partition Pointer to app partition. (only app partition)
  220. * @param[out] app_desc Structure of info about app.
  221. * @return
  222. * - ESP_OK Successful.
  223. * - ESP_ERR_NOT_FOUND app_desc structure is not found. Magic word is incorrect.
  224. * - ESP_ERR_NOT_SUPPORTED Partition is not application.
  225. * - ESP_ERR_INVALID_ARG Arguments is NULL or if partition's offset exceeds partition size.
  226. * - ESP_ERR_INVALID_SIZE Read would go out of bounds of the partition.
  227. * - or one of error codes from lower-level flash driver.
  228. */
  229. esp_err_t esp_ota_get_partition_description(const esp_partition_t *partition, esp_app_desc_t *app_desc);
  230. /**
  231. * @brief Returns the description structure of the bootloader.
  232. *
  233. * @param[in] bootloader_partition Pointer to bootloader partition.
  234. * If NULL, then the current bootloader is used (the default location).
  235. * offset = CONFIG_BOOTLOADER_OFFSET_IN_FLASH,
  236. * size = CONFIG_PARTITION_TABLE_OFFSET - CONFIG_BOOTLOADER_OFFSET_IN_FLASH,
  237. * @param[out] desc Structure of info about bootloader.
  238. * @return
  239. * - ESP_OK Successful.
  240. * - ESP_ERR_NOT_FOUND Description structure is not found in the bootloader image. Magic byte is incorrect.
  241. * - ESP_ERR_INVALID_ARG Arguments is NULL.
  242. * - ESP_ERR_INVALID_SIZE Read would go out of bounds of the partition.
  243. * - or one of error codes from lower-level flash driver.
  244. */
  245. esp_err_t esp_ota_get_bootloader_description(const esp_partition_t *bootloader_partition, esp_bootloader_desc_t *desc);
  246. /**
  247. * @brief Returns number of ota partitions provided in partition table.
  248. *
  249. * @return
  250. * - Number of OTA partitions
  251. */
  252. uint8_t esp_ota_get_app_partition_count(void);
  253. /**
  254. * @brief This function is called to indicate that the running app is working well.
  255. *
  256. * @return
  257. * - ESP_OK: if successful.
  258. */
  259. esp_err_t esp_ota_mark_app_valid_cancel_rollback(void);
  260. /**
  261. * @brief This function is called to roll back to the previously workable app with reboot.
  262. *
  263. * If rollback is successful then device will reset else API will return with error code.
  264. * Checks applications on a flash drive that can be booted in case of rollback.
  265. * If the flash does not have at least one app (except the running app) then rollback is not possible.
  266. * @return
  267. * - ESP_FAIL: if not successful.
  268. * - ESP_ERR_OTA_ROLLBACK_FAILED: The rollback is not possible due to flash does not have any apps.
  269. */
  270. esp_err_t esp_ota_mark_app_invalid_rollback_and_reboot(void);
  271. /**
  272. * @brief Returns last partition with invalid state (ESP_OTA_IMG_INVALID or ESP_OTA_IMG_ABORTED).
  273. *
  274. * @return partition.
  275. */
  276. const esp_partition_t* esp_ota_get_last_invalid_partition(void);
  277. /**
  278. * @brief Returns state for given partition.
  279. *
  280. * @param[in] partition Pointer to partition.
  281. * @param[out] ota_state state of partition (if this partition has a record in otadata).
  282. * @return
  283. * - ESP_OK: Successful.
  284. * - ESP_ERR_INVALID_ARG: partition or ota_state arguments were NULL.
  285. * - ESP_ERR_NOT_SUPPORTED: partition is not ota.
  286. * - ESP_ERR_NOT_FOUND: Partition table does not have otadata or state was not found for given partition.
  287. */
  288. esp_err_t esp_ota_get_state_partition(const esp_partition_t *partition, esp_ota_img_states_t *ota_state);
  289. /**
  290. * @brief Erase previous boot app partition and corresponding otadata select for this partition.
  291. *
  292. * When current app is marked to as valid then you can erase previous app partition.
  293. * @return
  294. * - ESP_OK: Successful, otherwise ESP_ERR.
  295. */
  296. esp_err_t esp_ota_erase_last_boot_app_partition(void);
  297. /**
  298. * @brief Checks applications on the slots which can be booted in case of rollback.
  299. *
  300. * These applications should be valid (marked in otadata as not UNDEFINED, INVALID or ABORTED and crc is good) and be able booted,
  301. * and secure_version of app >= secure_version of efuse (if anti-rollback is enabled).
  302. *
  303. * @return
  304. * - True: Returns true if the slots have at least one app (except the running app).
  305. * - False: The rollback is not possible.
  306. */
  307. bool esp_ota_check_rollback_is_possible(void);
  308. #if SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS > 1 && (CONFIG_SECURE_BOOT_V2_ENABLED || __DOXYGEN__)
  309. /**
  310. * Secure Boot V2 public key indexes.
  311. */
  312. typedef enum {
  313. SECURE_BOOT_PUBLIC_KEY_INDEX_0, /*!< Points to the 0th index of the Secure Boot v2 public key */
  314. SECURE_BOOT_PUBLIC_KEY_INDEX_1, /*!< Points to the 1st index of the Secure Boot v2 public key */
  315. SECURE_BOOT_PUBLIC_KEY_INDEX_2 /*!< Points to the 2nd index of the Secure Boot v2 public key */
  316. } esp_ota_secure_boot_public_key_index_t;
  317. /**
  318. * @brief Revokes the old signature digest. To be called in the application after the rollback logic.
  319. *
  320. * Relevant for Secure boot v2 on ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6, ESP32-H2 where up to 3 key digests can be stored (Key \#N-1, Key \#N, Key \#N+1).
  321. * When key \#N-1 used to sign an app is invalidated, an OTA update is to be sent with an app signed with key \#N-1 & Key \#N.
  322. * After successfully booting the OTA app should call this function to revoke Key \#N-1.
  323. *
  324. * @param index - The index of the signature block to be revoked
  325. *
  326. * @return
  327. * - ESP_OK: If revocation is successful.
  328. * - ESP_ERR_INVALID_ARG: If the index of the public key to be revoked is incorrect.
  329. * - ESP_FAIL: If secure boot v2 has not been enabled.
  330. */
  331. esp_err_t esp_ota_revoke_secure_boot_public_key(esp_ota_secure_boot_public_key_index_t index);
  332. #endif /* SOC_EFUSE_SECURE_BOOT_KEY_DIGESTS > 1 */
  333. #ifdef __cplusplus
  334. }
  335. #endif
  336. #endif /* OTA_OPS_H */