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