esp_image_format.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #pragma once
  14. #include <stdbool.h>
  15. #include <esp_err.h>
  16. #include "esp_flash_partitions.h"
  17. #include "esp_app_format.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define ESP_ERR_IMAGE_BASE 0x2000
  22. #define ESP_ERR_IMAGE_FLASH_FAIL (ESP_ERR_IMAGE_BASE + 1)
  23. #define ESP_ERR_IMAGE_INVALID (ESP_ERR_IMAGE_BASE + 2)
  24. /* Support for app/bootloader image parsing
  25. Can be compiled as part of app or bootloader code.
  26. */
  27. #define ESP_IMAGE_HASH_LEN 32 /* Length of the appended SHA-256 digest */
  28. /* Structure to hold on-flash image metadata */
  29. typedef struct {
  30. uint32_t start_addr; /* Start address of image */
  31. esp_image_header_t image; /* Header for entire image */
  32. esp_image_segment_header_t segments[ESP_IMAGE_MAX_SEGMENTS]; /* Per-segment header data */
  33. uint32_t segment_data[ESP_IMAGE_MAX_SEGMENTS]; /* Data offsets for each segment */
  34. uint32_t image_len; /* Length of image on flash, in bytes */
  35. uint8_t image_digest[32]; /* appended SHA-256 digest */
  36. } esp_image_metadata_t;
  37. typedef enum {
  38. ESP_IMAGE_VERIFY, /* Verify image contents, not load to memory, load metadata. Print errors. */
  39. ESP_IMAGE_VERIFY_SILENT, /* Verify image contents, not load to memory, load metadata. Don't print errors. */
  40. #ifdef BOOTLOADER_BUILD
  41. ESP_IMAGE_LOAD, /* Verify image contents, load to memory, load metadata. Print errors. */
  42. ESP_IMAGE_LOAD_NO_VALIDATE, /* Not verify image contents, load to memory, load metadata. Print errors. */
  43. #endif
  44. } esp_image_load_mode_t;
  45. typedef struct {
  46. esp_partition_pos_t partition; /*!< Partition of application which worked before goes to the deep sleep. */
  47. uint16_t reboot_counter; /*!< Reboot counter. Reset only when power is off. */
  48. uint16_t reserve; /*!< Reserve */
  49. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  50. uint8_t custom[CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE]; /*!< Reserve for custom propose */
  51. #endif
  52. uint32_t crc; /*!< Check sum crc32 */
  53. } rtc_retain_mem_t;
  54. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  55. _Static_assert(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE % 4 == 0, "CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE must be a multiple of 4 bytes");
  56. #endif
  57. #if defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) || defined(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC)
  58. _Static_assert(CONFIG_BOOTLOADER_RESERVE_RTC_SIZE % 4 == 0, "CONFIG_BOOTLOADER_RESERVE_RTC_SIZE must be a multiple of 4 bytes");
  59. #endif
  60. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  61. #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE)
  62. #elif defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP)
  63. #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE)
  64. #endif
  65. #if defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) || defined(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC)
  66. _Static_assert(sizeof(rtc_retain_mem_t) <= ESP_BOOTLOADER_RESERVE_RTC, "Reserved RTC area must exceed size of rtc_retain_mem_t");
  67. #endif
  68. /**
  69. * @brief Verify and (optionally, in bootloader mode) load an app image.
  70. *
  71. * This name is deprecated and is included for compatibility with the ESP-IDF v3.x API.
  72. * It will be removed in V4.0 version.
  73. * Function has been renamed to esp_image_verify().
  74. * Use function esp_image_verify() to verify a image. And use function bootloader_load_image() to load image from a bootloader space.
  75. *
  76. * If encryption is enabled, data will be transparently decrypted.
  77. *
  78. * @param mode Mode of operation (verify, silent verify, or load).
  79. * @param part Partition to load the app from.
  80. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function. 'start_addr' member should be set (to the start address of the image.) Other fields will all be initialised by this function.
  81. *
  82. * Image validation checks:
  83. * - Magic byte.
  84. * - Partition smaller than 16MB.
  85. * - All segments & image fit in partition.
  86. * - 8 bit image checksum is valid.
  87. * - SHA-256 of image is valid (if image has this appended).
  88. * - (Signature) if signature verification is enabled.
  89. *
  90. * @return
  91. * - ESP_OK if verify or load was successful
  92. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  93. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  94. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  95. */
  96. esp_err_t esp_image_load(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data) __attribute__((deprecated));
  97. /**
  98. * @brief Verify an app image.
  99. *
  100. * If encryption is enabled, data will be transparently decrypted.
  101. *
  102. * @param mode Mode of operation (verify, silent verify, or load).
  103. * @param part Partition to load the app from.
  104. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  105. * 'start_addr' member should be set (to the start address of the image.)
  106. * Other fields will all be initialised by this function.
  107. *
  108. * Image validation checks:
  109. * - Magic byte.
  110. * - Partition smaller than 16MB.
  111. * - All segments & image fit in partition.
  112. * - 8 bit image checksum is valid.
  113. * - SHA-256 of image is valid (if image has this appended).
  114. * - (Signature) if signature verification is enabled.
  115. *
  116. * @return
  117. * - ESP_OK if verify or load was successful
  118. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  119. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  120. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  121. */
  122. esp_err_t esp_image_verify(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data);
  123. /**
  124. * @brief Verify and load an app image (available only in space of bootloader).
  125. *
  126. * If encryption is enabled, data will be transparently decrypted.
  127. *
  128. * @param part Partition to load the app from.
  129. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  130. * 'start_addr' member should be set (to the start address of the image.)
  131. * Other fields will all be initialised by this function.
  132. *
  133. * Image validation checks:
  134. * - Magic byte.
  135. * - Partition smaller than 16MB.
  136. * - All segments & image fit in partition.
  137. * - 8 bit image checksum is valid.
  138. * - SHA-256 of image is valid (if image has this appended).
  139. * - (Signature) if signature verification is enabled.
  140. *
  141. * @return
  142. * - ESP_OK if verify or load was successful
  143. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  144. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  145. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  146. */
  147. esp_err_t bootloader_load_image(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  148. /**
  149. * @brief Load an app image without verification (available only in space of bootloader).
  150. *
  151. * If encryption is enabled, data will be transparently decrypted.
  152. *
  153. * @param part Partition to load the app from.
  154. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  155. * 'start_addr' member should be set (to the start address of the image.)
  156. * Other fields will all be initialised by this function.
  157. *
  158. * @return
  159. * - ESP_OK if verify or load was successful
  160. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  161. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  162. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  163. */
  164. esp_err_t bootloader_load_image_no_verify(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  165. /**
  166. * @brief Verify the bootloader image.
  167. *
  168. * @param[out] If result is ESP_OK and this pointer is non-NULL, it
  169. * will be set to the length of the bootloader image.
  170. *
  171. * @return As per esp_image_load_metadata().
  172. */
  173. esp_err_t esp_image_verify_bootloader(uint32_t *length);
  174. /**
  175. * @brief Verify the bootloader image.
  176. *
  177. * @param[out] Metadata for the image. Only valid if result is ESP_OK.
  178. *
  179. * @return As per esp_image_load_metadata().
  180. */
  181. esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data);
  182. typedef struct {
  183. uint32_t drom_addr;
  184. uint32_t drom_load_addr;
  185. uint32_t drom_size;
  186. uint32_t irom_addr;
  187. uint32_t irom_load_addr;
  188. uint32_t irom_size;
  189. } esp_image_flash_mapping_t;
  190. #ifdef __cplusplus
  191. }
  192. #endif