esp_ota_ops.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_spi_flash.h"
  21. #ifdef __cplusplus
  22. extern "C"
  23. {
  24. #endif
  25. #define OTA_SIZE_UNKNOWN 0xffffffff /*!< Used for esp_ota_begin() if new image size is unknown */
  26. #define ESP_ERR_OTA_BASE 0x1500 /*!< Base error code for ota_ops api */
  27. #define ESP_ERR_OTA_PARTITION_CONFLICT (ESP_ERR_OTA_BASE + 0x01) /*!< Error if request was to write or erase the current running partition */
  28. #define ESP_ERR_OTA_SELECT_INFO_INVALID (ESP_ERR_OTA_BASE + 0x02) /*!< Error if OTA data partition contains invalid content */
  29. #define ESP_ERR_OTA_VALIDATE_FAILED (ESP_ERR_OTA_BASE + 0x03) /*!< Error if OTA app image is invalid */
  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 Commence an OTA update writing to the specified partition.
  39. * The specified partition is erased to the specified image size.
  40. *
  41. * If image size is not yet known, pass OTA_SIZE_UNKNOWN which will
  42. * cause the entire partition to be erased.
  43. *
  44. * On success, this function allocates memory that remains in use
  45. * until esp_ota_end() is called with the returned handle.
  46. *
  47. * @param partition Pointer to info for partition which will receive the OTA update. Required.
  48. * @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.
  49. * @param out_handle On success, returns a handle which should be used for subsequent esp_ota_write() and esp_ota_end() calls.
  50. * @return
  51. * - ESP_OK: OTA operation commenced successfully.
  52. * - ESP_ERR_INVALID_ARG: partition or out_handle arguments were NULL, or partition doesn't point to an OTA app partition.
  53. * - ESP_ERR_NO_MEM: Cannot allocate memory for OTA operation.
  54. * - ESP_ERR_OTA_PARTITION_CONFLICT: Partition holds the currently running firmware, cannot update in place.
  55. * - ESP_ERR_NOT_FOUND: Partition argument not found in partition table.
  56. * - ESP_ERR_OTA_SELECT_INFO_INVALID: The OTA data partition contains invalid data.
  57. * - ESP_ERR_INVALID_SIZE: Partition doesn't fit in configured flash size.
  58. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  59. */
  60. esp_err_t esp_ota_begin(const esp_partition_t* partition, size_t image_size, esp_ota_handle_t* out_handle);
  61. /**
  62. * @brief Write OTA update data to partition
  63. *
  64. * This function can be called multiple times as
  65. * data is received during the OTA operation. Data is written
  66. * sequentially to the partition.
  67. *
  68. * @param handle Handle obtained from esp_ota_begin
  69. * @param data Data buffer to write
  70. * @param size Size of data buffer in bytes.
  71. *
  72. * @return
  73. * - ESP_OK: Data was written to flash successfully.
  74. * - ESP_ERR_INVALID_ARG: handle is invalid.
  75. * - ESP_ERR_OTA_VALIDATE_FAILED: First byte of image contains invalid app image magic byte.
  76. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.
  77. * - ESP_ERR_OTA_SELECT_INFO_INVALID: OTA data partition has invalid contents
  78. */
  79. esp_err_t esp_ota_write(esp_ota_handle_t handle, const void* data, size_t size);
  80. /**
  81. * @brief Finish OTA update and validate newly written app image.
  82. *
  83. * @param handle Handle obtained from esp_ota_begin().
  84. *
  85. * @note After calling esp_ota_end(), the handle is no longer valid and any memory associated with it is freed (regardless of result).
  86. *
  87. * @return
  88. * - ESP_OK: Newly written OTA app image is valid.
  89. * - ESP_ERR_NOT_FOUND: OTA handle was not found.
  90. * - ESP_ERR_INVALID_ARG: Handle was never written to.
  91. * - 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.)
  92. * - ESP_ERR_INVALID_STATE: If flash encryption is enabled, this result indicates an internal error writing the final encrypted bytes to flash.
  93. */
  94. esp_err_t esp_ota_end(esp_ota_handle_t handle);
  95. /**
  96. * @brief Configure OTA data for a new boot partition
  97. *
  98. * @note If this function returns ESP_OK, calling esp_restart() will boot the newly configured app partition.
  99. *
  100. * @param partition Pointer to info for partition containing app image to boot.
  101. *
  102. * @return
  103. * - ESP_OK: OTA data updated, next reboot will use specified partition.
  104. * - ESP_ERR_INVALID_ARG: partition argument was NULL or didn't point to a valid OTA partition of type "app".
  105. * - ESP_ERR_OTA_VALIDATE_FAILED: Partition contained invalid app image. Also returned if secure boot is enabled and signature validation failed.
  106. * - ESP_ERR_NOT_FOUND: OTA data partition not found.
  107. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash erase or write failed.
  108. */
  109. esp_err_t esp_ota_set_boot_partition(const esp_partition_t* partition);
  110. /**
  111. * @brief Get partition info of currently configured boot app
  112. *
  113. * If esp_ota_set_boot_partition() has been called, the partition which was set by that function will be returned.
  114. *
  115. * If esp_ota_set_boot_partition() has not been called, the result is
  116. * equivalent to esp_ota_get_running_partition().
  117. *
  118. * @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.
  119. */
  120. const esp_partition_t* esp_ota_get_boot_partition(void);
  121. /**
  122. * @brief Get partition info of currently running app
  123. *
  124. * This function is different to esp_ota_get_boot_partition() in that
  125. * it ignores any change of selected boot partition caused by
  126. * esp_ota_set_boot_partition(). Only the app whose code is currently
  127. * running will have its partition information returned.
  128. *
  129. * @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.
  130. */
  131. const esp_partition_t* esp_ota_get_running_partition(void);
  132. /**
  133. * @brief Return the next OTA app partition which should be written with a new firmware.
  134. *
  135. * Call this function to find an OTA app partition which can be passed to esp_ota_begin().
  136. *
  137. * Finds next partition round-robin, starting from the current running partition.
  138. *
  139. * @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.
  140. *
  141. * @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.
  142. *
  143. */
  144. const esp_partition_t* esp_ota_get_next_update_partition(const esp_partition_t *start_from);
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif /* OTA_OPS_H */