esp_image_format.h 7.9 KB

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