esp_image_format.h 7.5 KB

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