esp_image_format.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. /**
  50. * @brief ESP chip ID
  51. *
  52. */
  53. typedef enum {
  54. ESP_CHIP_ID_ESP32 = 0x0000, /*!< chip ID: ESP32 */
  55. ESP_CHIP_ID_INVALID = 0xFFFF /*!< Invalid chip ID (we defined it to make sure the esp_chip_id_t is 2 bytes size) */
  56. } __attribute__((packed)) esp_chip_id_t;
  57. /** @cond */
  58. _Static_assert(sizeof(esp_chip_id_t) == 2, "esp_chip_id_t should be 16 bit");
  59. /** @endcond */
  60. /* Main header of binary image */
  61. typedef struct {
  62. uint8_t magic;
  63. uint8_t segment_count;
  64. /* flash read mode (esp_image_spi_mode_t as uint8_t) */
  65. uint8_t spi_mode;
  66. /* flash frequency (esp_image_spi_freq_t as uint8_t) */
  67. uint8_t spi_speed: 4;
  68. /* flash chip size (esp_image_flash_size_t as uint8_t) */
  69. uint8_t spi_size: 4;
  70. uint32_t entry_addr;
  71. /* WP pin when SPI pins set via efuse (read by ROM bootloader, the IDF bootloader uses software to configure the WP
  72. * pin and sets this field to 0xEE=disabled) */
  73. uint8_t wp_pin;
  74. /* Drive settings for the SPI flash pins (read by ROM bootloader) */
  75. uint8_t spi_pin_drv[3];
  76. esp_chip_id_t chip_id; /*!< Chip identification number */
  77. uint8_t min_chip_rev; /*!< Minimum chip revision supported by image */
  78. uint8_t reserved[8]; /*!< Reserved bytes in additional header space, currently unused */
  79. /* If 1, a SHA256 digest "simple hash" (of the entire image) is appended after the checksum. Included in image length. This digest
  80. * is separate to secure boot and only used for detecting corruption. For secure boot signed images, the signature
  81. * is appended after this (and the simple hash is included in the signed data). */
  82. uint8_t hash_appended;
  83. } __attribute__((packed)) esp_image_header_t;
  84. _Static_assert(sizeof(esp_image_header_t) == 24, "binary image header should be 24 bytes");
  85. #define ESP_IMAGE_HASH_LEN 32 /* Length of the appended SHA-256 digest */
  86. /* Header of binary image segment */
  87. typedef struct {
  88. uint32_t load_addr;
  89. uint32_t data_len;
  90. } esp_image_segment_header_t;
  91. #define ESP_APP_DESC_MAGIC_WORD 0xABCD5432 /*!< The magic word for the esp_app_desc structure that is in DROM. */
  92. /**
  93. * @brief Description about application.
  94. */
  95. typedef struct {
  96. uint32_t magic_word; /*!< Magic word ESP_APP_DESC_MAGIC_WORD */
  97. uint32_t secure_version; /*!< Secure version */
  98. uint32_t reserv1[2]; /*!< --- */
  99. char version[32]; /*!< Application version */
  100. char project_name[32]; /*!< Project name */
  101. char time[16]; /*!< Compile time */
  102. char date[16]; /*!< Compile date*/
  103. char idf_ver[32]; /*!< Version IDF */
  104. uint8_t app_elf_sha256[32]; /*!< sha256 of elf file */
  105. uint32_t reserv2[20]; /*!< --- */
  106. } esp_app_desc_t;
  107. _Static_assert(sizeof(esp_app_desc_t) == 256, "esp_app_desc_t should be 256 bytes");
  108. #define ESP_IMAGE_MAX_SEGMENTS 16
  109. /* Structure to hold on-flash image metadata */
  110. typedef struct {
  111. uint32_t start_addr; /* Start address of image */
  112. esp_image_header_t image; /* Header for entire image */
  113. esp_image_segment_header_t segments[ESP_IMAGE_MAX_SEGMENTS]; /* Per-segment header data */
  114. uint32_t segment_data[ESP_IMAGE_MAX_SEGMENTS]; /* Data offsets for each segment */
  115. uint32_t image_len; /* Length of image on flash, in bytes */
  116. uint8_t image_digest[32]; /* appended SHA-256 digest */
  117. } esp_image_metadata_t;
  118. /* Mode selection for esp_image_load() */
  119. typedef enum {
  120. ESP_IMAGE_VERIFY, /* Verify image contents, load metadata. Print errors. */
  121. ESP_IMAGE_VERIFY_SILENT, /* Verify image contents, load metadata. Don't print errors. */
  122. #ifdef BOOTLOADER_BUILD
  123. ESP_IMAGE_LOAD, /* Verify image contents, load to memory. Print errors. */
  124. #endif
  125. } esp_image_load_mode_t;
  126. /**
  127. * @brief Verify and (optionally, in bootloader mode) load an app image.
  128. *
  129. * This name is deprecated and is included for compatibility with the ESP-IDF v3.x API.
  130. * It will be removed in V4.0 version.
  131. * Function has been renamed to esp_image_verify().
  132. * Use function esp_image_verify() to verify a image. And use function bootloader_load_image() to load image from a bootloader space.
  133. *
  134. * If encryption is enabled, data will be transparently decrypted.
  135. *
  136. * @param mode Mode of operation (verify, silent verify, or load).
  137. * @param part Partition to load the app from.
  138. * @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.
  139. *
  140. * Image validation checks:
  141. * - Magic byte.
  142. * - Partition smaller than 16MB.
  143. * - All segments & image fit in partition.
  144. * - 8 bit image checksum is valid.
  145. * - SHA-256 of image is valid (if image has this appended).
  146. * - (Signature) if signature verification is enabled.
  147. *
  148. * @return
  149. * - ESP_OK if verify or load was successful
  150. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  151. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  152. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  153. */
  154. 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));
  155. /**
  156. * @brief Verify an app image.
  157. *
  158. * If encryption is enabled, data will be transparently decrypted.
  159. *
  160. * @param mode Mode of operation (verify, silent verify, or load).
  161. * @param part Partition to load the app from.
  162. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  163. * 'start_addr' member should be set (to the start address of the image.)
  164. * Other fields will all be initialised by this function.
  165. *
  166. * Image validation checks:
  167. * - Magic byte.
  168. * - Partition smaller than 16MB.
  169. * - All segments & image fit in partition.
  170. * - 8 bit image checksum is valid.
  171. * - SHA-256 of image is valid (if image has this appended).
  172. * - (Signature) if signature verification is enabled.
  173. *
  174. * @return
  175. * - ESP_OK if verify or load was successful
  176. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  177. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  178. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  179. */
  180. esp_err_t esp_image_verify(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data);
  181. /**
  182. * @brief Verify and load an app image (available only in space of bootloader).
  183. *
  184. * If encryption is enabled, data will be transparently decrypted.
  185. *
  186. * @param part Partition to load the app from.
  187. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  188. * 'start_addr' member should be set (to the start address of the image.)
  189. * Other fields will all be initialised by this function.
  190. *
  191. * Image validation checks:
  192. * - Magic byte.
  193. * - Partition smaller than 16MB.
  194. * - All segments & image fit in partition.
  195. * - 8 bit image checksum is valid.
  196. * - SHA-256 of image is valid (if image has this appended).
  197. * - (Signature) if signature verification is enabled.
  198. *
  199. * @return
  200. * - ESP_OK if verify or load was successful
  201. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  202. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  203. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  204. */
  205. esp_err_t bootloader_load_image(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  206. /**
  207. * @brief Verify the bootloader image.
  208. *
  209. * @param[out] If result is ESP_OK and this pointer is non-NULL, it
  210. * will be set to the length of the bootloader image.
  211. *
  212. * @return As per esp_image_load_metadata().
  213. */
  214. esp_err_t esp_image_verify_bootloader(uint32_t *length);
  215. /**
  216. * @brief Verify the bootloader image.
  217. *
  218. * @param[out] Metadata for the image. Only valid if result is ESP_OK.
  219. *
  220. * @return As per esp_image_load_metadata().
  221. */
  222. esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data);
  223. typedef struct {
  224. uint32_t drom_addr;
  225. uint32_t drom_load_addr;
  226. uint32_t drom_size;
  227. uint32_t irom_addr;
  228. uint32_t irom_load_addr;
  229. uint32_t irom_size;
  230. } esp_image_flash_mapping_t;