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 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include "esp_err.h"
17 #include "esp_types.h"
18 #include "sdkconfig.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #define ESP_PARTITION_MAGIC 0x50AA
25 #define ESP_PARTITION_MAGIC_MD5 0xEBEB
26 
27 #define PART_TYPE_APP 0x00
28 #define PART_SUBTYPE_FACTORY  0x00
29 #define PART_SUBTYPE_OTA_FLAG 0x10
30 #define PART_SUBTYPE_OTA_MASK 0x0f
31 #define PART_SUBTYPE_TEST     0x20
32 
33 #define PART_TYPE_DATA 0x01
34 #define PART_SUBTYPE_DATA_OTA 0x00
35 #define PART_SUBTYPE_DATA_RF  0x01
36 #define PART_SUBTYPE_DATA_WIFI 0x02
37 #define PART_SUBTYPE_DATA_NVS_KEYS 0x04
38 #define PART_SUBTYPE_DATA_EFUSE_EM 0x05
39 
40 #define PART_TYPE_END 0xff
41 #define PART_SUBTYPE_END 0xff
42 
43 #define PART_FLAG_ENCRYPTED (1<<0)
44 
45 /* Pre-partition table fixed flash offsets */
46 #define ESP_BOOTLOADER_DIGEST_OFFSET 0x0
47 #define ESP_BOOTLOADER_OFFSET CONFIG_BOOTLOADER_OFFSET_IN_FLASH  /* Offset of bootloader image. Has matching value in bootloader KConfig.projbuild file. */
48 #define ESP_PARTITION_TABLE_OFFSET CONFIG_PARTITION_TABLE_OFFSET /* Offset of partition table. Backwards-compatible name.*/
49 
50 #define ESP_PARTITION_TABLE_MAX_LEN 0xC00 /* Maximum length of partition table data */
51 #define ESP_PARTITION_TABLE_MAX_ENTRIES (ESP_PARTITION_TABLE_MAX_LEN / sizeof(esp_partition_info_t)) /* Maximum length of partition table data, including terminating entry */
52 
53 /// OTA_DATA states for checking operability of the app.
54 typedef enum {
55     ESP_OTA_IMG_NEW             = 0x0U,         /*!< Monitor the first boot. In bootloader this state is changed to ESP_OTA_IMG_PENDING_VERIFY. */
56     ESP_OTA_IMG_PENDING_VERIFY  = 0x1U,         /*!< First boot for this app was. If while the second boot this state is then it will be changed to ABORTED. */
57     ESP_OTA_IMG_VALID           = 0x2U,         /*!< App was confirmed as workable. App can boot and work without limits. */
58     ESP_OTA_IMG_INVALID         = 0x3U,         /*!< App was confirmed as non-workable. This app will not selected to boot at all. */
59     ESP_OTA_IMG_ABORTED         = 0x4U,         /*!< App could not confirm the workable or non-workable. In bootloader IMG_PENDING_VERIFY state will be changed to IMG_ABORTED. This app will not selected to boot at all. */
60     ESP_OTA_IMG_UNDEFINED       = 0xFFFFFFFFU,  /*!< Undefined. App can boot and work without limits. */
61 } esp_ota_img_states_t;
62 
63 /* OTA selection structure (two copies in the OTA data partition.)
64    Size of 32 bytes is friendly to flash encryption */
65 typedef struct {
66     uint32_t ota_seq;
67     uint8_t  seq_label[20];
68     uint32_t ota_state;
69     uint32_t crc; /* CRC32 of ota_seq field only */
70 } esp_ota_select_entry_t;
71 
72 
73 typedef struct {
74     uint32_t offset;
75     uint32_t size;
76 } esp_partition_pos_t;
77 
78 /* Structure which describes the layout of partition table entry.
79  * See docs/partition_tables.rst for more information about individual fields.
80  */
81 typedef struct {
82     uint16_t magic;
83     uint8_t  type;
84     uint8_t  subtype;
85     esp_partition_pos_t pos;
86     uint8_t  label[16];
87     uint32_t flags;
88 } esp_partition_info_t;
89 
90 /* @brief Verify the partition table
91  *
92  * @param partition_table Pointer to at least ESP_PARTITION_TABLE_MAX_ENTRIES of potential partition table data. (ESP_PARTITION_TABLE_MAX_LEN bytes.)
93  * @param log_errors Log errors if the partition table is invalid.
94  * @param num_partitions If result is ESP_OK, num_partitions is updated with total number of partitions (not including terminating entry).
95  *
96  * @return ESP_OK on success, ESP_ERR_INVALID_STATE if partition table is not valid.
97  */
98 esp_err_t esp_partition_table_verify(const esp_partition_info_t *partition_table, bool log_errors, int *num_partitions);
99 
100 
101 /**
102  * Check whether the region on the main flash is safe to write.
103  *
104  * @param addr Start address of the region
105  * @param size Size of the region
106  *
107  * @return true if the region is safe to write, otherwise false.
108  */
109 bool esp_partition_main_flash_region_safe(size_t addr, size_t size);
110 
111 #ifdef __cplusplus
112 }
113 #endif
114