| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  | // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software
 | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS,
 | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and
 | 
					
						
							|  |  |  | // limitations under the License.
 | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <stdbool.h>
 | 
					
						
							|  |  |  | #include <esp_err.h>
 | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  | #include "esp_flash_partitions.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-29 12:25:50 +08:00
										 |  |  | #include "esp_app_format.h"
 | 
					
						
							| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 11:02:15 +10:00
										 |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  | #define ESP_ERR_IMAGE_BASE       0x2000
 | 
					
						
							|  |  |  | #define ESP_ERR_IMAGE_FLASH_FAIL (ESP_ERR_IMAGE_BASE + 1)
 | 
					
						
							|  |  |  | #define ESP_ERR_IMAGE_INVALID    (ESP_ERR_IMAGE_BASE + 2)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Support for app/bootloader image parsing
 | 
					
						
							|  |  |  |    Can be compiled as part of app or bootloader code. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 00:31:58 +10:00
										 |  |  | #define ESP_IMAGE_HASH_LEN 32 /* Length of the appended SHA-256 digest */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  | /* Structure to hold on-flash image metadata */ | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |   uint32_t start_addr;   /* Start address of image */ | 
					
						
							|  |  |  |   esp_image_header_t image; /* Header for entire image */ | 
					
						
							|  |  |  |   esp_image_segment_header_t segments[ESP_IMAGE_MAX_SEGMENTS]; /* Per-segment header data */ | 
					
						
							|  |  |  |   uint32_t segment_data[ESP_IMAGE_MAX_SEGMENTS]; /* Data offsets for each segment */ | 
					
						
							| 
									
										
										
										
											2017-06-27 17:25:30 +10:00
										 |  |  |   uint32_t image_len; /* Length of image on flash, in bytes */ | 
					
						
							| 
									
										
										
										
											2018-05-30 14:08:00 +05:00
										 |  |  |   uint8_t image_digest[32]; /* appended SHA-256 digest */ | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  | } esp_image_metadata_t; | 
					
						
							| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  | typedef enum { | 
					
						
							| 
									
										
										
										
											2019-04-12 15:32:47 -05:00
										 |  |  |     ESP_IMAGE_VERIFY,            /* Verify image contents, not load to memory, load metadata. Print errors. */ | 
					
						
							|  |  |  |     ESP_IMAGE_VERIFY_SILENT,     /* Verify image contents, not load to memory, load metadata. Don't print errors. */ | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  | #ifdef BOOTLOADER_BUILD
 | 
					
						
							| 
									
										
										
										
											2019-04-12 15:32:47 -05:00
										 |  |  |     ESP_IMAGE_LOAD,              /* Verify image contents, load to memory, load metadata. Print errors. */ | 
					
						
							|  |  |  |     ESP_IMAGE_LOAD_NO_VALIDATE,  /* Not verify image contents, load to memory, load metadata. Print errors. */ | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } esp_image_load_mode_t; | 
					
						
							| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-05 18:18:58 +08:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     esp_partition_pos_t partition;  /*!< Partition of application which worked before goes to the deep sleep. */ | 
					
						
							|  |  |  |     uint16_t reboot_counter;        /*!< Reboot counter. Reset only when power is off. */ | 
					
						
							|  |  |  |     uint16_t reserve;               /*!< Reserve */ | 
					
						
							|  |  |  | #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
 | 
					
						
							|  |  |  |     uint8_t custom[CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE]; /*!< Reserve for custom propose */ | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |     uint32_t crc;                   /*!< Check sum crc32 */ | 
					
						
							|  |  |  | } rtc_retain_mem_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
 | 
					
						
							|  |  |  | _Static_assert(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE % 4 == 0, "CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE must be a multiple of 4 bytes"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) || defined(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC)
 | 
					
						
							|  |  |  | _Static_assert(CONFIG_BOOTLOADER_RESERVE_RTC_SIZE % 4 == 0, "CONFIG_BOOTLOADER_RESERVE_RTC_SIZE must be a multiple of 4 bytes"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC
 | 
					
						
							|  |  |  | #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE)
 | 
					
						
							|  |  |  | #elif defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP)
 | 
					
						
							|  |  |  | #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined(CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP) || defined(CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC)
 | 
					
						
							|  |  |  | _Static_assert(sizeof(rtc_retain_mem_t) <= ESP_BOOTLOADER_RESERVE_RTC, "Reserved RTC area must exceed size of rtc_retain_mem_t"); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-30 14:08:00 +05:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @brief Verify an app image. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * If encryption is enabled, data will be transparently decrypted. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param mode Mode of operation (verify, silent verify, or load). | 
					
						
							|  |  |  |  * @param part Partition to load the app from. | 
					
						
							|  |  |  |  * @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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Image validation checks: | 
					
						
							|  |  |  |  * - Magic byte. | 
					
						
							|  |  |  |  * - Partition smaller than 16MB. | 
					
						
							|  |  |  |  * - All segments & image fit in partition. | 
					
						
							|  |  |  |  * - 8 bit image checksum is valid. | 
					
						
							|  |  |  |  * - SHA-256 of image is valid (if image has this appended). | 
					
						
							|  |  |  |  * - (Signature) if signature verification is enabled. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @return | 
					
						
							|  |  |  |  * - ESP_OK if verify or load was successful | 
					
						
							|  |  |  |  * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs | 
					
						
							|  |  |  |  * - ESP_ERR_IMAGE_INVALID if the image appears invalid. | 
					
						
							|  |  |  |  * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | esp_err_t esp_image_verify(esp_image_load_mode_t mode, const esp_partition_pos_t *part, esp_image_metadata_t *data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @brief Verify and load an app image (available only in space of bootloader). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * If encryption is enabled, data will be transparently decrypted. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param part Partition to load the app from. | 
					
						
							|  |  |  |  * @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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Image validation checks: | 
					
						
							|  |  |  |  * - Magic byte. | 
					
						
							|  |  |  |  * - Partition smaller than 16MB. | 
					
						
							|  |  |  |  * - All segments & image fit in partition. | 
					
						
							|  |  |  |  * - 8 bit image checksum is valid. | 
					
						
							|  |  |  |  * - SHA-256 of image is valid (if image has this appended). | 
					
						
							|  |  |  |  * - (Signature) if signature verification is enabled. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @return | 
					
						
							|  |  |  |  * - ESP_OK if verify or load was successful | 
					
						
							|  |  |  |  * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs | 
					
						
							|  |  |  |  * - ESP_ERR_IMAGE_INVALID if the image appears invalid. | 
					
						
							|  |  |  |  * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | esp_err_t bootloader_load_image(const esp_partition_pos_t *part, esp_image_metadata_t *data); | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-12 15:32:47 -05:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @brief Load an app image without verification (available only in space of bootloader). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * If encryption is enabled, data will be transparently decrypted. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param part Partition to load the app from. | 
					
						
							|  |  |  |  * @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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @return | 
					
						
							|  |  |  |  * - ESP_OK if verify or load was successful | 
					
						
							|  |  |  |  * - ESP_ERR_IMAGE_FLASH_FAIL if a SPI flash error occurs | 
					
						
							|  |  |  |  * - ESP_ERR_IMAGE_INVALID if the image appears invalid. | 
					
						
							|  |  |  |  * - ESP_ERR_INVALID_ARG if the partition or data pointers are invalid. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | esp_err_t bootloader_load_image_no_verify(const esp_partition_pos_t *part, esp_image_metadata_t *data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @brief Verify the bootloader image. | 
					
						
							| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  |  * @param[out] If result is ESP_OK and this pointer is non-NULL, it | 
					
						
							|  |  |  |  * will be set to the length of the bootloader image. | 
					
						
							| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  |  * @return As per esp_image_load_metadata(). | 
					
						
							| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-06-16 16:30:21 +10:00
										 |  |  | esp_err_t esp_image_verify_bootloader(uint32_t *length); | 
					
						
							| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-30 00:31:58 +10:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @brief Verify the bootloader image. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param[out] Metadata for the image. Only valid if result is ESP_OK. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @return As per esp_image_load_metadata(). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | esp_err_t esp_image_verify_bootloader_data(esp_image_metadata_t *data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-02 10:41:58 +11:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     uint32_t drom_addr; | 
					
						
							|  |  |  |     uint32_t drom_load_addr; | 
					
						
							|  |  |  |     uint32_t drom_size; | 
					
						
							|  |  |  |     uint32_t irom_addr; | 
					
						
							|  |  |  |     uint32_t irom_load_addr; | 
					
						
							|  |  |  |     uint32_t irom_size; | 
					
						
							|  |  |  | } esp_image_flash_mapping_t; | 
					
						
							| 
									
										
										
										
											2019-06-26 11:02:15 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef __cplusplus
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 |