1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #pragma once
7 
8 #include "esp_err.h"
9 #include "esp_types.h"
10 #include "sdkconfig.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #define ESP_PARTITION_MAGIC 0x50AA
17 #define ESP_PARTITION_MAGIC_MD5 0xEBEB
18 
19 #define PART_TYPE_APP 0x00
20 #define PART_SUBTYPE_FACTORY  0x00
21 #define PART_SUBTYPE_OTA_FLAG 0x10
22 #define PART_SUBTYPE_OTA_MASK 0x0f
23 #define PART_SUBTYPE_TEST     0x20
24 
25 #define PART_TYPE_DATA 0x01
26 #define PART_SUBTYPE_DATA_OTA 0x00
27 #define PART_SUBTYPE_DATA_RF  0x01
28 #define PART_SUBTYPE_DATA_WIFI 0x02
29 #define PART_SUBTYPE_DATA_NVS_KEYS 0x04
30 #define PART_SUBTYPE_DATA_EFUSE_EM 0x05
31 
32 #define PART_TYPE_END 0xff
33 #define PART_SUBTYPE_END 0xff
34 
35 #define PART_FLAG_ENCRYPTED (1<<0)
36 
37 /* The md5sum value is found this many bytes after the ESP_PARTITION_MAGIC_MD5 offset */
38 #define ESP_PARTITION_MD5_OFFSET 16
39 
40 #ifdef __ZEPHYR__
41 #include <zephyr/storage/flash_map.h>
42 #define CONFIG_BOOTLOADER_OFFSET_IN_FLASH FIXED_PARTITION_OFFSET(boot_partition)  /* Offset of bootloader image. */
43 #endif /* __ZEPHYR__ */
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