esp_image_format.h 7.5 KB

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