esp_image_format.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #define ESP_ERR_IMAGE_BASE 0x2000
  18. #define ESP_ERR_IMAGE_FLASH_FAIL (ESP_ERR_IMAGE_BASE + 1)
  19. #define ESP_ERR_IMAGE_INVALID (ESP_ERR_IMAGE_BASE + 2)
  20. /* Support for app/bootloader image parsing
  21. Can be compiled as part of app or bootloader code.
  22. */
  23. /* SPI flash mode, used in esp_image_header_t */
  24. typedef enum {
  25. ESP_IMAGE_SPI_MODE_QIO,
  26. ESP_IMAGE_SPI_MODE_QOUT,
  27. ESP_IMAGE_SPI_MODE_DIO,
  28. ESP_IMAGE_SPI_MODE_DOUT,
  29. ESP_IMAGE_SPI_MODE_FAST_READ,
  30. ESP_IMAGE_SPI_MODE_SLOW_READ
  31. } esp_image_spi_mode_t;
  32. /* SPI flash clock frequency */
  33. typedef enum {
  34. ESP_IMAGE_SPI_SPEED_40M,
  35. ESP_IMAGE_SPI_SPEED_26M,
  36. ESP_IMAGE_SPI_SPEED_20M,
  37. ESP_IMAGE_SPI_SPEED_80M = 0xF
  38. } esp_image_spi_freq_t;
  39. /* Supported SPI flash sizes */
  40. typedef enum {
  41. ESP_IMAGE_FLASH_SIZE_1MB = 0,
  42. ESP_IMAGE_FLASH_SIZE_2MB,
  43. ESP_IMAGE_FLASH_SIZE_4MB,
  44. ESP_IMAGE_FLASH_SIZE_8MB,
  45. ESP_IMAGE_FLASH_SIZE_16MB,
  46. ESP_IMAGE_FLASH_SIZE_MAX
  47. } esp_image_flash_size_t;
  48. #define ESP_IMAGE_HEADER_MAGIC 0xE9
  49. /* Main header of binary image */
  50. typedef struct {
  51. uint8_t magic;
  52. uint8_t segment_count;
  53. /* flash read mode (esp_image_spi_mode_t as uint8_t) */
  54. uint8_t spi_mode;
  55. /* flash frequency (esp_image_spi_freq_t as uint8_t) */
  56. uint8_t spi_speed: 4;
  57. /* flash chip size (esp_image_flash_size_t as uint8_t) */
  58. uint8_t spi_size: 4;
  59. uint32_t entry_addr;
  60. /* WP pin when SPI pins set via efuse (read by ROM bootloader, the IDF bootloader uses software to configure the WP
  61. * pin and sets this field to 0xEE=disabled) */
  62. uint8_t wp_pin;
  63. /* Drive settings for the SPI flash pins (read by ROM bootloader) */
  64. uint8_t spi_pin_drv[3];
  65. /* Reserved bytes in ESP32 additional header space, currently unused */
  66. uint8_t reserved[11];
  67. /* If 1, a SHA256 digest "simple hash" (of the entire image) is appended after the checksum. Included in image length. This digest
  68. * is separate to secure boot and only used for detecting corruption. For secure boot signed images, the signature
  69. * is appended after this (and the simple hash is included in the signed data). */
  70. uint8_t hash_appended;
  71. } __attribute__((packed)) esp_image_header_t;
  72. _Static_assert(sizeof(esp_image_header_t) == 24, "binary image header should be 24 bytes");
  73. #define ESP_IMAGE_HASH_LEN 32 /* Length of the appended SHA-256 digest */
  74. /* Header of binary image segment */
  75. typedef struct {
  76. uint32_t load_addr;
  77. uint32_t data_len;
  78. } esp_image_segment_header_t;
  79. #define ESP_IMAGE_MAX_SEGMENTS 16
  80. /* Structure to hold on-flash image metadata */
  81. typedef struct {
  82. uint32_t start_addr; /* Start address of image */
  83. esp_image_header_t image; /* Header for entire image */
  84. esp_image_segment_header_t segments[ESP_IMAGE_MAX_SEGMENTS]; /* Per-segment header data */
  85. uint32_t segment_data[ESP_IMAGE_MAX_SEGMENTS]; /* Data offsets for each segment */
  86. uint32_t image_len; /* Length of image on flash, in bytes */
  87. uint8_t image_digest[32]; /* appended SHA-256 digest */
  88. } esp_image_metadata_t;
  89. /* Mode selection for esp_image_load() */
  90. typedef enum {
  91. ESP_IMAGE_VERIFY, /* Verify image contents, load metadata. Print errors. */
  92. ESP_IMAGE_VERIFY_SILENT, /* Verify image contents, load metadata. Don't print errors. */
  93. #ifdef BOOTLOADER_BUILD
  94. ESP_IMAGE_LOAD, /* Verify image contents, load to memory. Print errors. */
  95. #endif
  96. } esp_image_load_mode_t;
  97. /**
  98. * @brief Verify and (optionally, in bootloader mode) load an app image.
  99. *
  100. * This name is deprecated and is included for compatibility with the ESP-IDF v3.x API.
  101. * It will be removed in V4.0 version.
  102. * Function has been renamed to esp_image_verify().
  103. * Use function esp_image_verify() to verify a image. And use function bootloader_load_image() to load image from a bootloader space.
  104. *
  105. * If encryption is enabled, data will be transparently decrypted.
  106. *
  107. * @param mode Mode of operation (verify, silent verify, or load).
  108. * @param part Partition to load the app from.
  109. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function. 'start_addr' member should be set (to the start address of the image.) Other fields will all be initialised by this function.
  110. *
  111. * Image validation checks:
  112. * - Magic byte.
  113. * - Partition smaller than 16MB.
  114. * - All segments & image fit in partition.
  115. * - 8 bit image checksum is valid.
  116. * - SHA-256 of image is valid (if image has this appended).
  117. * - (Signature) if signature verification is enabled.
  118. *
  119. * @return
  120. * - ESP_OK if verify or load was successful
  121. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  122. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  123. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  124. */
  125. esp_err_t esp_image_load(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data) __attribute__((deprecated));
  126. /**
  127. * @brief Verify an app image.
  128. *
  129. * If encryption is enabled, data will be transparently decrypted.
  130. *
  131. * @param mode Mode of operation (verify, silent verify, or load).
  132. * @param part Partition to load the app from.
  133. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  134. * 'start_addr' member should be set (to the start address of the image.)
  135. * Other fields will all be initialised by this function.
  136. *
  137. * Image validation checks:
  138. * - Magic byte.
  139. * - Partition smaller than 16MB.
  140. * - All segments & image fit in partition.
  141. * - 8 bit image checksum is valid.
  142. * - SHA-256 of image is valid (if image has this appended).
  143. * - (Signature) if signature verification is enabled.
  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 esp_image_verify(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data);
  152. /**
  153. * @brief Verify and load an app image (available only in space of bootloader).
  154. *
  155. * If encryption is enabled, data will be transparently decrypted.
  156. *
  157. * @param part Partition to load the app from.
  158. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  159. * 'start_addr' member should be set (to the start address of the image.)
  160. * Other fields will all be initialised by this function.
  161. *
  162. * Image validation checks:
  163. * - Magic byte.
  164. * - Partition smaller than 16MB.
  165. * - All segments & image fit in partition.
  166. * - 8 bit image checksum is valid.
  167. * - SHA-256 of image is valid (if image has this appended).
  168. * - (Signature) if signature verification is enabled.
  169. *
  170. * @return
  171. * - ESP_OK if verify or load was successful
  172. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  173. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  174. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  175. */
  176. esp_err_t bootloader_load_image(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  177. /**
  178. * @brief Verify the bootloader image.
  179. *
  180. * @param[out] If result is ESP_OK and this pointer is non-NULL, it
  181. * will be set to the length of the bootloader image.
  182. *
  183. * @return As per esp_image_load_metadata().
  184. */
  185. esp_err_t esp_image_verify_bootloader(uint32_t *length);
  186. /**
  187. * @brief Verify the bootloader image.
  188. *
  189. * @param[out] Metadata for the image. Only valid if result is ESP_OK.
  190. *
  191. * @return As per esp_image_load_metadata().
  192. */
  193. esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data);
  194. typedef struct {
  195. uint32_t drom_addr;
  196. uint32_t drom_load_addr;
  197. uint32_t drom_size;
  198. uint32_t irom_addr;
  199. uint32_t irom_load_addr;
  200. uint32_t irom_size;
  201. } esp_image_flash_mapping_t;