esp_image_format.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifndef __ESP32_IMAGE_FORMAT_H
  14. #define __ESP32_IMAGE_FORMAT_H
  15. #include <stdbool.h>
  16. #include <esp_err.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. 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. uint8_t spi_mode; /* flash read mode (esp_image_spi_mode_t as uint8_t) */
  54. uint8_t spi_speed: 4; /* flash frequency (esp_image_spi_freq_t as uint8_t) */
  55. uint8_t spi_size: 4; /* flash chip size (esp_image_flash_size_t as uint8_t) */
  56. uint32_t entry_addr;
  57. uint8_t encrypt_flag; /* encrypt flag */
  58. uint8_t extra_header[15]; /* ESP32 additional header, unused by second bootloader */
  59. } esp_image_header_t;
  60. /* Header of binary image segment */
  61. typedef struct {
  62. uint32_t load_addr;
  63. uint32_t data_len;
  64. } esp_image_segment_header_t;
  65. /**
  66. * @brief Read an ESP image header from flash.
  67. *
  68. * If encryption is enabled, data will be transparently decrypted.
  69. *
  70. * @param src_addr Address in flash to load image header. Must be 4 byte aligned.
  71. * @param log_errors Log error output if image header appears invalid.
  72. * @param[out] image_header Pointer to an esp_image_header_t struture to be filled with data. If the function fails, contents are undefined.
  73. *
  74. * @return ESP_OK if image header was loaded, ESP_ERR_IMAGE_FLASH_FAIL
  75. * if a SPI flash error occurs, ESP_ERR_IMAGE_INVALID if the image header
  76. * appears invalid.
  77. */
  78. esp_err_t esp_image_load_header(uint32_t src_addr, bool log_errors, esp_image_header_t *image_header);
  79. /**
  80. * @brief Read the segment header and data offset of a segment in the image.
  81. *
  82. * If encryption is enabled, data will be transparently decrypted.
  83. *
  84. * @param index Index of the segment to load information for.
  85. * @param src_addr Base address in flash of the image.
  86. * @param[in] image_header Pointer to the flash image header, already loaded by @ref esp_image_load_header().
  87. * @param log_errors Log errors reading the segment header.
  88. * @param[out] segment_header Pointer to a segment header structure to be filled with data. If the function fails, contents are undefined.
  89. * @param[out] segment_data_offset Pointer to the data offset of the segment.
  90. *
  91. * @return ESP_OK if segment_header & segment_data_offset were loaded successfully, ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs, ESP_ERR_IMAGE_INVALID if the image header appears invalid, ESP_ERR_INVALID_ARG if the index is invalid.
  92. */
  93. esp_err_t esp_image_load_segment_header(uint8_t index, uint32_t src_addr, const esp_image_header_t *image_header, bool log_errors, esp_image_segment_header_t *segment_header, uint32_t *segment_data_offset);
  94. /**
  95. * @brief Non-cryptographically validate app image integrity. On success, length of image is provided to caller.
  96. *
  97. * If the image has a secure boot signature appended, the signature is not checked and this length is not included in the
  98. * output value.
  99. *
  100. * Image validation checks:
  101. * - Magic byte
  102. * - No single segment longer than 16MB
  103. * - Total image no longer than 16MB
  104. * - 8 bit image checksum is valid
  105. *
  106. * If flash encryption is enabled, the image will be tranpsarently decrypted.
  107. *
  108. * @param src_addr Offset of the start of the image in flash. Must be 4 byte aligned.
  109. * @param allow_decrypt If true and flash encryption is enabled, the image will be transparently decrypted.
  110. * @param log_errors Log errors verifying the image.
  111. * @param[out] length Length of the image, set to a value if the image is valid. Can be null.
  112. *
  113. * @return ESP_OK if image is valid, ESP_FAIL or ESP_ERR_IMAGE_INVALID on errors.
  114. *
  115. */
  116. esp_err_t esp_image_basic_verify(uint32_t src_addr, bool log_errors, uint32_t *length);
  117. typedef struct {
  118. uint32_t drom_addr;
  119. uint32_t drom_load_addr;
  120. uint32_t drom_size;
  121. uint32_t irom_addr;
  122. uint32_t irom_load_addr;
  123. uint32_t irom_size;
  124. } esp_image_flash_mapping_t;
  125. #endif