esp_image_format.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. uint16_t reserve; /*!< Reserve */
  43. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  44. uint8_t custom[CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE]; /*!< Reserve for custom propose */
  45. #endif
  46. uint32_t crc; /*!< Check sum crc32 */
  47. } rtc_retain_mem_t;
  48. 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");
  49. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  50. ESP_STATIC_ASSERT(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE % 4 == 0, "CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE must be a multiple of 4 bytes");
  51. /* The custom field must be the penultimate field */
  52. ESP_STATIC_ASSERT(offsetof(rtc_retain_mem_t, custom) == sizeof(rtc_retain_mem_t) - sizeof(uint32_t) - CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE,
  53. "custom field in rtc_retain_mem_t structure must be the field before the CRC one");
  54. #endif
  55. #if defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) || defined(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC)
  56. ESP_STATIC_ASSERT(CONFIG_BOOTLOADER_RESERVE_RTC_SIZE % 4 == 0, "CONFIG_BOOTLOADER_RESERVE_RTC_SIZE must be a multiple of 4 bytes");
  57. #endif
  58. #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
  59. #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE)
  60. #elif defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP)
  61. #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE)
  62. #endif
  63. #if defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) || defined(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC)
  64. ESP_STATIC_ASSERT(sizeof(rtc_retain_mem_t) <= ESP_BOOTLOADER_RESERVE_RTC, "Reserved RTC area must exceed size of rtc_retain_mem_t");
  65. #endif
  66. /**
  67. * @brief Verify an app image.
  68. *
  69. * If encryption is enabled, data will be transparently decrypted.
  70. *
  71. * @param mode Mode of operation (verify, silent verify, or load).
  72. * @param part Partition to load the app from.
  73. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  74. * 'start_addr' member should be set (to the start address of the image.)
  75. * Other fields will all be initialised by this function.
  76. *
  77. * Image validation checks:
  78. * - Magic byte.
  79. * - Partition smaller than 16MB.
  80. * - All segments & image fit in partition.
  81. * - 8 bit image checksum is valid.
  82. * - SHA-256 of image is valid (if image has this appended).
  83. * - (Signature) if signature verification is enabled.
  84. *
  85. * @return
  86. * - ESP_OK if verify or load was successful
  87. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  88. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  89. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  90. */
  91. esp_err_t esp_image_verify(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data);
  92. /**
  93. * @brief Get metadata of app
  94. *
  95. * If encryption is enabled, data will be transparently decrypted.
  96. *
  97. * @param part Partition to load the app from.
  98. * @param[out] metadata Pointer to the image metadata structure which is be filled in by this function.
  99. * Fields will all be initialised by this function.
  100. *
  101. * @return
  102. * - ESP_OK if filling of metadata was successful
  103. */
  104. esp_err_t esp_image_get_metadata(const esp_partition_pos_t *part, esp_image_metadata_t *metadata);
  105. /**
  106. * @brief Verify and load an app image (available only in space of bootloader).
  107. *
  108. * If encryption is enabled, data will be transparently decrypted.
  109. *
  110. * @param part Partition to load the app from.
  111. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  112. * 'start_addr' member should be set (to the start address of the image.)
  113. * Other fields will all be initialised by this function.
  114. *
  115. * Image validation checks:
  116. * - Magic byte.
  117. * - Partition smaller than 16MB.
  118. * - All segments & image fit in partition.
  119. * - 8 bit image checksum is valid.
  120. * - SHA-256 of image is valid (if image has this appended).
  121. * - (Signature) if signature verification is enabled.
  122. *
  123. * @return
  124. * - ESP_OK if verify or load was successful
  125. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  126. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  127. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  128. */
  129. esp_err_t bootloader_load_image(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  130. /**
  131. * @brief Load an app image without verification (available only in space of bootloader).
  132. *
  133. * If encryption is enabled, data will be transparently decrypted.
  134. *
  135. * @param part Partition to load the app from.
  136. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  137. * 'start_addr' member should be set (to the start address of the image.)
  138. * Other fields will all be initialised by this function.
  139. *
  140. * @return
  141. * - ESP_OK if verify or load was successful
  142. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  143. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  144. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  145. */
  146. esp_err_t bootloader_load_image_no_verify(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  147. /**
  148. * @brief Verify the bootloader image.
  149. *
  150. * @param[out] If result is ESP_OK and this pointer is non-NULL, it
  151. * will be set to the length of the bootloader image.
  152. *
  153. * @return As per esp_image_load_metadata().
  154. */
  155. esp_err_t esp_image_verify_bootloader(uint32_t *length);
  156. /**
  157. * @brief Verify the bootloader image.
  158. *
  159. * @param[out] Metadata for the image. Only valid if result is ESP_OK.
  160. *
  161. * @return As per esp_image_load_metadata().
  162. */
  163. esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data);
  164. /**
  165. * @brief Get the flash size of the image
  166. *
  167. * @param app_flash_size The value configured in the image header
  168. * @return Actual size, in bytes.
  169. */
  170. int esp_image_get_flash_size(esp_image_flash_size_t app_flash_size);
  171. typedef struct {
  172. uint32_t drom_addr;
  173. uint32_t drom_load_addr;
  174. uint32_t drom_size;
  175. uint32_t irom_addr;
  176. uint32_t irom_load_addr;
  177. uint32_t irom_size;
  178. } esp_image_flash_mapping_t;
  179. #ifdef __cplusplus
  180. }
  181. #endif