esp_image_format.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /* Mode selection for esp_image_load() */
  38. typedef enum {
  39. ESP_IMAGE_VERIFY, /* Verify image contents, load metadata. Print errors. */
  40. ESP_IMAGE_VERIFY_SILENT, /* Verify image contents, load metadata. Don't print errors. */
  41. #ifdef BOOTLOADER_BUILD
  42. ESP_IMAGE_LOAD, /* Verify image contents, load to memory. Print errors. */
  43. #endif
  44. } esp_image_load_mode_t;
  45. /**
  46. * @brief Verify and (optionally, in bootloader mode) load an app image.
  47. *
  48. * This name is deprecated and is included for compatibility with the ESP-IDF v3.x API.
  49. * It will be removed in V4.0 version.
  50. * Function has been renamed to esp_image_verify().
  51. * Use function esp_image_verify() to verify a image. And use function bootloader_load_image() to load image from a bootloader space.
  52. *
  53. * If encryption is enabled, data will be transparently decrypted.
  54. *
  55. * @param mode Mode of operation (verify, silent verify, or load).
  56. * @param part Partition to load the app from.
  57. * @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.
  58. *
  59. * Image validation checks:
  60. * - Magic byte.
  61. * - Partition smaller than 16MB.
  62. * - All segments & image fit in partition.
  63. * - 8 bit image checksum is valid.
  64. * - SHA-256 of image is valid (if image has this appended).
  65. * - (Signature) if signature verification is enabled.
  66. *
  67. * @return
  68. * - ESP_OK if verify or load was successful
  69. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  70. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  71. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  72. */
  73. 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));
  74. /**
  75. * @brief Verify an app image.
  76. *
  77. * If encryption is enabled, data will be transparently decrypted.
  78. *
  79. * @param mode Mode of operation (verify, silent verify, or load).
  80. * @param part Partition to load the app from.
  81. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  82. * 'start_addr' member should be set (to the start address of the image.)
  83. * Other fields will all be initialised by this function.
  84. *
  85. * Image validation checks:
  86. * - Magic byte.
  87. * - Partition smaller than 16MB.
  88. * - All segments & image fit in partition.
  89. * - 8 bit image checksum is valid.
  90. * - SHA-256 of image is valid (if image has this appended).
  91. * - (Signature) if signature verification is enabled.
  92. *
  93. * @return
  94. * - ESP_OK if verify or load was successful
  95. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  96. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  97. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  98. */
  99. esp_err_t esp_image_verify(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data);
  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 Verify the bootloader image.
  127. *
  128. * @param[out] If result is ESP_OK and this pointer is non-NULL, it
  129. * will be set to the length of the bootloader image.
  130. *
  131. * @return As per esp_image_load_metadata().
  132. */
  133. esp_err_t esp_image_verify_bootloader(uint32_t *length);
  134. /**
  135. * @brief Verify the bootloader image.
  136. *
  137. * @param[out] Metadata for the image. Only valid if result is ESP_OK.
  138. *
  139. * @return As per esp_image_load_metadata().
  140. */
  141. esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data);
  142. typedef struct {
  143. uint32_t drom_addr;
  144. uint32_t drom_load_addr;
  145. uint32_t drom_size;
  146. uint32_t irom_addr;
  147. uint32_t irom_load_addr;
  148. uint32_t irom_size;
  149. } esp_image_flash_mapping_t;
  150. #ifdef __cplusplus
  151. }
  152. #endif