esp_image_format.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include <esp_err.h>
  9. #include "esp_flash_partitions.h"
  10. #include "esp_app_format.h"
  11. #include "esp_assert.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #define ESP_ERR_IMAGE_BASE 0x2000
  16. #define ESP_ERR_IMAGE_FLASH_FAIL (ESP_ERR_IMAGE_BASE + 1)
  17. #define ESP_ERR_IMAGE_INVALID (ESP_ERR_IMAGE_BASE + 2)
  18. /* Support for app/bootloader image parsing
  19. Can be compiled as part of app or bootloader code.
  20. */
  21. #define ESP_IMAGE_HASH_LEN 32 /* Length of the appended SHA-256 digest */
  22. /* Structure to hold on-flash image metadata */
  23. typedef struct {
  24. uint32_t start_addr; /* Start address of image */
  25. esp_image_header_t image; /* Header for entire image */
  26. esp_image_segment_header_t segments[ESP_IMAGE_MAX_SEGMENTS]; /* Per-segment header data */
  27. uint32_t segment_data[ESP_IMAGE_MAX_SEGMENTS]; /* Data offsets for each segment */
  28. uint32_t image_len; /* Length of image on flash, in bytes */
  29. uint8_t image_digest[32]; /* appended SHA-256 digest */
  30. } esp_image_metadata_t;
  31. typedef enum {
  32. ESP_IMAGE_VERIFY, /* Verify image contents, not load to memory, load metadata. Print errors. */
  33. ESP_IMAGE_VERIFY_SILENT, /* Verify image contents, not load to memory, load metadata. Don't print errors. */
  34. #ifdef BOOTLOADER_BUILD
  35. ESP_IMAGE_LOAD, /* Verify image contents, load to memory, load metadata. Print errors. */
  36. ESP_IMAGE_LOAD_NO_VALIDATE, /* Not verify image contents, load to memory, load metadata. Print errors. */
  37. #endif
  38. } esp_image_load_mode_t;
  39. typedef struct {
  40. esp_partition_pos_t partition; /*!< Partition of application which worked before goes to the deep sleep. */
  41. uint16_t reboot_counter; /*!< Reboot counter. Reset only when power is off. */
  42. union {
  43. struct {
  44. uint8_t factory_reset_state : 1; /* True when Factory reset has occurred */
  45. uint8_t reserve : 7; /* Reserve */
  46. };
  47. uint8_t val;
  48. } flags;
  49. uint8_t reserve; /*!< Reserve */
  50. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  51. uint8_t custom[CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE]; /*!< Reserve for custom propose */
  52. #endif
  53. uint32_t crc; /*!< Check sum crc32 */
  54. } rtc_retain_mem_t;
  55. ESP_STATIC_ASSERT(offsetof(rtc_retain_mem_t, crc) == sizeof(rtc_retain_mem_t) - sizeof(uint32_t), "CRC field must be the last field of rtc_retain_mem_t structure");
  56. #ifdef CONFIG_BOOTLOADER_RESERVE_RTC_MEM
  57. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  58. ESP_STATIC_ASSERT(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE % 4 == 0, "CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE must be a multiple of 4 bytes");
  59. /* The custom field must be the penultimate field */
  60. ESP_STATIC_ASSERT(offsetof(rtc_retain_mem_t, custom) == sizeof(rtc_retain_mem_t) - sizeof(uint32_t) - CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE,
  61. "custom field in rtc_retain_mem_t structure must be the field before the CRC one");
  62. #endif
  63. ESP_STATIC_ASSERT(CONFIG_BOOTLOADER_RESERVE_RTC_SIZE % 4 == 0, "CONFIG_BOOTLOADER_RESERVE_RTC_SIZE must be a multiple of 4 bytes");
  64. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  65. #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE)
  66. #else
  67. #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE)
  68. #endif
  69. ESP_STATIC_ASSERT(sizeof(rtc_retain_mem_t) <= ESP_BOOTLOADER_RESERVE_RTC, "Reserved RTC area must exceed size of rtc_retain_mem_t");
  70. #endif // CONFIG_BOOTLOADER_RESERVE_RTC_MEM
  71. /**
  72. * @brief Verify an app image.
  73. *
  74. * If encryption is enabled, data will be transparently decrypted.
  75. *
  76. * @param mode Mode of operation (verify, silent verify, or load).
  77. * @param part Partition to load the app from.
  78. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  79. * 'start_addr' member should be set (to the start address of the image.)
  80. * 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_verify(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data);
  97. /**
  98. * @brief Get metadata of app
  99. *
  100. * If encryption is enabled, data will be transparently decrypted.
  101. *
  102. * @param part Partition to load the app from.
  103. * @param[out] metadata Pointer to the image metadata structure which is be filled in by this function.
  104. * Fields will all be initialised by this function.
  105. *
  106. * @return
  107. * - ESP_OK if filling of metadata was successful
  108. */
  109. esp_err_t esp_image_get_metadata(const esp_partition_pos_t *part, esp_image_metadata_t *metadata);
  110. /**
  111. * @brief Verify and load an app image (available only in space of bootloader).
  112. *
  113. * If encryption is enabled, data will be transparently decrypted.
  114. *
  115. * @param part Partition to load the app from.
  116. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  117. * 'start_addr' member should be set (to the start address of the image.)
  118. * Other fields will all be initialised by this function.
  119. *
  120. * Image validation checks:
  121. * - Magic byte.
  122. * - Partition smaller than 16MB.
  123. * - All segments & image fit in partition.
  124. * - 8 bit image checksum is valid.
  125. * - SHA-256 of image is valid (if image has this appended).
  126. * - (Signature) if signature verification is enabled.
  127. *
  128. * @return
  129. * - ESP_OK if verify or load was successful
  130. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  131. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  132. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  133. */
  134. esp_err_t bootloader_load_image(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  135. /**
  136. * @brief Load an app image without verification (available only in space of bootloader).
  137. *
  138. * If encryption is enabled, data will be transparently decrypted.
  139. *
  140. * @param part Partition to load the app from.
  141. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  142. * 'start_addr' member should be set (to the start address of the image.)
  143. * Other fields will all be initialised by this function.
  144. *
  145. * @return
  146. * - ESP_OK if verify or load was successful
  147. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  148. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  149. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  150. */
  151. esp_err_t bootloader_load_image_no_verify(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  152. /**
  153. * @brief Verify the bootloader image.
  154. *
  155. * @param[out] If result is ESP_OK and this pointer is non-NULL, it
  156. * will be set to the length of the bootloader image.
  157. *
  158. * @return As per esp_image_load_metadata().
  159. */
  160. esp_err_t esp_image_verify_bootloader(uint32_t *length);
  161. /**
  162. * @brief Verify the bootloader image.
  163. *
  164. * @param[out] Metadata for the image. Only valid if result is ESP_OK.
  165. *
  166. * @return As per esp_image_load_metadata().
  167. */
  168. esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data);
  169. /**
  170. * @brief Get the flash size of the image
  171. *
  172. * @param app_flash_size The value configured in the image header
  173. * @return Actual size, in bytes.
  174. */
  175. int esp_image_get_flash_size(esp_image_flash_size_t app_flash_size);
  176. typedef struct {
  177. uint32_t drom_addr;
  178. uint32_t drom_load_addr;
  179. uint32_t drom_size;
  180. uint32_t irom_addr;
  181. uint32_t irom_load_addr;
  182. uint32_t irom_size;
  183. } esp_image_flash_mapping_t;
  184. #ifdef __cplusplus
  185. }
  186. #endif