esp_image_format.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 an app image.
  70. *
  71. * If encryption is enabled, data will be transparently decrypted.
  72. *
  73. * @param mode Mode of operation (verify, silent verify, or load).
  74. * @param part Partition to load the app from.
  75. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  76. * 'start_addr' member should be set (to the start address of the image.)
  77. * Other fields will all be initialised by this function.
  78. *
  79. * Image validation checks:
  80. * - Magic byte.
  81. * - Partition smaller than 16MB.
  82. * - All segments & image fit in partition.
  83. * - 8 bit image checksum is valid.
  84. * - SHA-256 of image is valid (if image has this appended).
  85. * - (Signature) if signature verification is enabled.
  86. *
  87. * @return
  88. * - ESP_OK if verify or load was successful
  89. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  90. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  91. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  92. */
  93. esp_err_t esp_image_verify(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data);
  94. /**
  95. * @brief Get metadata of app
  96. *
  97. * If encryption is enabled, data will be transparently decrypted.
  98. *
  99. * @param part Partition to load the app from.
  100. * @param[out] metadata Pointer to the image metadata structure which is be filled in by this function.
  101. * Fields will all be initialised by this function.
  102. *
  103. * @return
  104. * - ESP_OK if filling of metadata was successful
  105. */
  106. esp_err_t esp_image_get_metadata(const esp_partition_pos_t *part, esp_image_metadata_t *metadata);
  107. /**
  108. * @brief Verify and load an app image (available only in space of bootloader).
  109. *
  110. * If encryption is enabled, data will be transparently decrypted.
  111. *
  112. * @param part Partition to load the app from.
  113. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  114. * 'start_addr' member should be set (to the start address of the image.)
  115. * Other fields will all be initialised by this function.
  116. *
  117. * Image validation checks:
  118. * - Magic byte.
  119. * - Partition smaller than 16MB.
  120. * - All segments & image fit in partition.
  121. * - 8 bit image checksum is valid.
  122. * - SHA-256 of image is valid (if image has this appended).
  123. * - (Signature) if signature verification is enabled.
  124. *
  125. * @return
  126. * - ESP_OK if verify or load was successful
  127. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  128. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  129. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  130. */
  131. esp_err_t bootloader_load_image(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  132. /**
  133. * @brief Load an app image without verification (available only in space of bootloader).
  134. *
  135. * If encryption is enabled, data will be transparently decrypted.
  136. *
  137. * @param part Partition to load the app from.
  138. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  139. * 'start_addr' member should be set (to the start address of the image.)
  140. * Other fields will all be initialised by this function.
  141. *
  142. * @return
  143. * - ESP_OK if verify or load was successful
  144. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  145. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  146. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  147. */
  148. esp_err_t bootloader_load_image_no_verify(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  149. /**
  150. * @brief Verify the bootloader image.
  151. *
  152. * @param[out] If result is ESP_OK and this pointer is non-NULL, it
  153. * will be set to the length of the bootloader image.
  154. *
  155. * @return As per esp_image_load_metadata().
  156. */
  157. esp_err_t esp_image_verify_bootloader(uint32_t *length);
  158. /**
  159. * @brief Verify the bootloader image.
  160. *
  161. * @param[out] Metadata for the image. Only valid if result is ESP_OK.
  162. *
  163. * @return As per esp_image_load_metadata().
  164. */
  165. esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data);
  166. /**
  167. * @brief Get the flash size of the image
  168. *
  169. * @param app_flash_size The value configured in the image header
  170. * @return Actual size, in bytes.
  171. */
  172. int esp_image_get_flash_size(esp_image_flash_size_t app_flash_size);
  173. typedef struct {
  174. uint32_t drom_addr;
  175. uint32_t drom_load_addr;
  176. uint32_t drom_size;
  177. uint32_t irom_addr;
  178. uint32_t irom_load_addr;
  179. uint32_t irom_size;
  180. } esp_image_flash_mapping_t;
  181. #ifdef __cplusplus
  182. }
  183. #endif