esp_image_format.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 an app image.
  47. *
  48. * If encryption is enabled, data will be transparently decrypted.
  49. *
  50. * @param mode Mode of operation (verify, silent verify, or load).
  51. * @param part Partition to load the app from.
  52. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  53. * 'start_addr' member should be set (to the start address of the image.)
  54. * Other fields will all be initialised by this function.
  55. *
  56. * Image validation checks:
  57. * - Magic byte.
  58. * - Partition smaller than 16MB.
  59. * - All segments & image fit in partition.
  60. * - 8 bit image checksum is valid.
  61. * - SHA-256 of image is valid (if image has this appended).
  62. * - (Signature) if signature verification is enabled.
  63. *
  64. * @return
  65. * - ESP_OK if verify or load was successful
  66. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  67. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  68. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  69. */
  70. esp_err_t esp_image_verify(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data);
  71. /**
  72. * @brief Verify and load an app image (available only in space of bootloader).
  73. *
  74. * If encryption is enabled, data will be transparently decrypted.
  75. *
  76. * @param part Partition to load the app from.
  77. * @param[inout] data Pointer to the image metadata structure which is be filled in by this function.
  78. * 'start_addr' member should be set (to the start address of the image.)
  79. * Other fields will all be initialised by this function.
  80. *
  81. * Image validation checks:
  82. * - Magic byte.
  83. * - Partition smaller than 16MB.
  84. * - All segments & image fit in partition.
  85. * - 8 bit image checksum is valid.
  86. * - SHA-256 of image is valid (if image has this appended).
  87. * - (Signature) if signature verification is enabled.
  88. *
  89. * @return
  90. * - ESP_OK if verify or load was successful
  91. * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs
  92. * - ESP_ERR_IMAGE_INVALID if the image appears invalid.
  93. * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid.
  94. */
  95. esp_err_t bootloader_load_image(const esp_partition_pos_t *part, esp_image_metadata_t *data);
  96. /**
  97. * @brief Verify the bootloader image.
  98. *
  99. * @param[out] If result is ESP_OK and this pointer is non-NULL, it
  100. * will be set to the length of the bootloader image.
  101. *
  102. * @return As per esp_image_load_metadata().
  103. */
  104. esp_err_t esp_image_verify_bootloader(uint32_t *length);
  105. /**
  106. * @brief Verify the bootloader image.
  107. *
  108. * @param[out] Metadata for the image. Only valid if result is ESP_OK.
  109. *
  110. * @return As per esp_image_load_metadata().
  111. */
  112. esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data);
  113. /**
  114. * @brief Get the flash size of the image
  115. *
  116. * @param app_flash_size The value configured in the image header
  117. * @return Actual size, in bytes.
  118. */
  119. int esp_image_get_flash_size(esp_image_flash_size_t app_flash_size);
  120. typedef struct {
  121. uint32_t drom_addr;
  122. uint32_t drom_load_addr;
  123. uint32_t drom_size;
  124. uint32_t irom_addr;
  125. uint32_t irom_load_addr;
  126. uint32_t irom_size;
  127. } esp_image_flash_mapping_t;
  128. #ifdef __cplusplus
  129. }
  130. #endif