]> granicus.if.org Git - esp-idf/blob - components/spi_flash/include/esp_partition.h
Merge branch 'bugfix/nvs_leaks' into 'master'
[esp-idf] / components / spi_flash / include / esp_partition.h
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
15 #ifndef __ESP_PARTITION_H__
16 #define __ESP_PARTITION_H__
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <stddef.h>
21 #include "esp_err.h"
22 #include "esp_spi_flash.h"
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 typedef enum {
29     ESP_PARTITION_TYPE_APP = 0x00,
30     ESP_PARTITION_TYPE_DATA = 0x01,
31     ESP_PARTITION_TYPE_FILESYSTEM = 0x02,
32 } esp_partition_type_t;
33
34 typedef enum {
35     ESP_PARTITION_SUBTYPE_APP_FACTORY = 0x00,
36     ESP_PARTITION_SUBTYPE_APP_OTA_MIN = 0x10,
37     ESP_PARTITION_SUBTYPE_APP_OTA_0 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 0,
38     ESP_PARTITION_SUBTYPE_APP_OTA_1 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 1,
39     ESP_PARTITION_SUBTYPE_APP_OTA_2 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 2,
40     ESP_PARTITION_SUBTYPE_APP_OTA_3 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 3,
41     ESP_PARTITION_SUBTYPE_APP_OTA_4 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 4,
42     ESP_PARTITION_SUBTYPE_APP_OTA_5 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 5,
43     ESP_PARTITION_SUBTYPE_APP_OTA_6 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 6,
44     ESP_PARTITION_SUBTYPE_APP_OTA_7 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 7,
45     ESP_PARTITION_SUBTYPE_APP_OTA_8 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 8,
46     ESP_PARTITION_SUBTYPE_APP_OTA_9 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 9,
47     ESP_PARTITION_SUBTYPE_APP_OTA_10 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 10,
48     ESP_PARTITION_SUBTYPE_APP_OTA_11 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 11,
49     ESP_PARTITION_SUBTYPE_APP_OTA_12 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 12,
50     ESP_PARTITION_SUBTYPE_APP_OTA_13 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 13,
51     ESP_PARTITION_SUBTYPE_APP_OTA_14 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 14,
52     ESP_PARTITION_SUBTYPE_APP_OTA_15 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 15,
53     ESP_PARTITION_SUBTYPE_APP_OTA_MAX = 15,
54     ESP_PARTITION_SUBTYPE_APP_TEST = 0x20,
55
56     ESP_PARTITION_SUBTYPE_DATA_OTA = 0x00,
57     ESP_PARTITION_SUBTYPE_DATA_RF =  0x01,
58     ESP_PARTITION_SUBTYPE_DATA_NVS = 0x02,
59
60     ESP_PARTITION_SUBTYPE_FILESYSTEM_ESPHTTPD = 0x00,
61     ESP_PARTITION_SUBTYPE_FILESYSTEM_FAT = 0x01,
62     ESP_PARTITION_SUBTYPE_FILESYSTEM_SPIFFS = 0x02,
63
64     ESP_PARTITION_SUBTYPE_ANY = 0xff,
65 } esp_partition_subtype_t;
66
67 #define ESP_PARTITION_SUBTYPE_OTA(i) ((esp_partition_subtype_t)(ESP_PARTITION_SUBTYPE_APP_OTA_MIN + ((i) & 0xf)))
68
69
70 typedef struct esp_partition_iterator_opaque_* esp_partition_iterator_t;
71
72 typedef struct {
73     esp_partition_type_t type;
74     esp_partition_subtype_t subtype;
75     uint32_t address;
76     uint32_t size;
77     char label[17];
78     bool encrypted;
79 } esp_partition_t;
80
81 /**
82  * @brief Find partition based on one or more parameters
83  *
84  * @param type Partition type, one of esp_partition_type_t values
85  * @param subtype Partition subtype, one of esp_partition_subtype_t values.
86  *                To find all partitions of given type, use
87  *                ESP_PARTITION_SUBTYPE_ANY.
88  * @param label (optional) Partition label. Set this value if looking
89  *             for partition with a specific name. Pass NULL otherwise.
90  *
91  * @return iterator which can be used to enumerate all the partitions found,
92  *         or NULL if no partitions were found.
93  *         Iterator obtained through this function has to be released
94  *         using esp_partition_iterator_release when not used any more.
95  */
96 esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label);
97
98 /**
99  * @brief Find first partition based on one or more parameters
100  *
101  * @param type Partition type, one of esp_partition_type_t values
102  * @param subtype Partition subtype, one of esp_partition_subtype_t values.
103  *                To find all partitions of given type, use
104  *                ESP_PARTITION_SUBTYPE_ANY.
105  * @param label (optional) Partition label. Set this value if looking
106  *             for partition with a specific name. Pass NULL otherwise.
107  *
108  * @return pointer to esp_partition_t structure, or NULL if no partition is found.
109  *         This pointer is valid for the lifetime of the application.
110  */
111 const esp_partition_t* esp_partition_find_first(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label);
112
113 /**
114  * @brief Get esp_partition_t structure for given partition
115  *
116  * @param iterator  Iterator obtained using esp_partition_find. Must be non-NULL.
117  *
118  * @return pointer to esp_partition_t structure. This pointer is valid for the lifetime
119  *         of the application.
120  */
121 const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator);
122
123 /**
124  * @brief Move partition iterator to the next partition found
125  *
126  * Any copies of the iterator will be invalid after this call.
127  *
128  * @param iterator Iterator obtained using esp_partition_find. Must be non-NULL.
129  *
130  * @return NULL if no partition was found, valid esp_partition_iterator_t otherwise.
131  */
132 esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t iterator);
133
134 /**
135  * @brief Release partition iterator
136  *
137  * @param iterator Iterator obtained using esp_partition_find. Must be non-NULL.
138  *
139  */
140 void esp_partition_iterator_release(esp_partition_iterator_t iterator);
141
142 /**
143  * @brief Read data from the partition
144  *
145  * @param partition Pointer to partition structure obtained using
146  *                  esp_partition_find_first or esp_partition_get.
147  *                  Must be non-NULL.
148  * @param dst Pointer to the buffer where data should be stored.
149  *            Pointer must be non-NULL and buffer must be at least 'size' bytes long.
150  * @param src_offset Address of the data to be read, relative to the
151  *                   beginning of the partition.
152  * @param size Size of data to be read, in bytes.
153  *
154  * @return ESP_OK, if data was read successfully;
155  *         ESP_ERR_INVALID_ARG, if src_offset exceeds partition size;
156  *         ESP_ERR_INVALID_SIZE, if read would go out of bounds of the partition;
157  *         or one of error codes from lower-level flash driver.
158  */
159 esp_err_t esp_partition_read(const esp_partition_t* partition,
160                              size_t src_offset, void* dst, size_t size);
161
162 /**
163  * @brief Write data to the partition
164  *
165  * Before writing data to flash, corresponding region of flash needs to be erased.
166  * This can be done using esp_partition_erase_range function.
167  *
168  * @param partition Pointer to partition structure obtained using
169  *                  esp_partition_find_first or esp_partition_get.
170  *                  Must be non-NULL.
171  * @param dst_offset Address where the data should be written, relative to the
172  *                   beginning of the partition.
173  * @param src Pointer to the source buffer.  Pointer must be non-NULL and
174  *            buffer must be at least 'size' bytes long.
175  * @param size Size of data to be written, in bytes.
176  *
177  * @note Prior to writing to flash memory, make sure it has been erased with
178  *       esp_partition_erase_range call.
179  *
180  * @return ESP_OK, if data was written successfully;
181  *         ESP_ERR_INVALID_ARG, if dst_offset exceeds partition size;
182  *         ESP_ERR_INVALID_SIZE, if write would go out of bounds of the partition;
183  *         or one of error codes from lower-level flash driver.
184  */
185 esp_err_t esp_partition_write(const esp_partition_t* partition,
186                              size_t dst_offset, const void* src, size_t size);
187
188 /**
189  * @brief Erase part of the partition
190  *
191  * @param partition Pointer to partition structure obtained using
192  *                  esp_partition_find_first or esp_partition_get.
193  *                  Must be non-NULL.
194  * @param start_addr Address where erase operation should start. Must be aligned
195  *                   to 4 kilobytes.
196  * @param size Size of the range which should be erased, in bytes.
197  *                   Must be divisible by 4 kilobytes.
198  *
199  * @return ESP_OK, if the range was erased successfully;
200  *         ESP_ERR_INVALID_ARG, if iterator or dst are NULL;
201  *         ESP_ERR_INVALID_SIZE, if erase would go out of bounds of the partition;
202  *         or one of error codes from lower-level flash driver.
203  */
204 esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
205                                     uint32_t start_addr, uint32_t size);
206
207 /**
208  * @brief Configure MMU to map partition into data memory
209  *
210  * Unlike spi_flash_mmap function, which requires a 64kB aligned base address,
211  * this function doesn't impose such a requirement.
212  * If offset results in a flash address which is not aligned to 64kB boundary,
213  * address will be rounded to the lower 64kB boundary, so that mapped region
214  * includes requested range.
215  * Pointer returned via out_ptr argument will be adjusted to point to the
216  * requested offset (not necessarily to the beginning of mmap-ed region).
217  *
218  * To release mapped memory, pass handle returned via out_handle argument to
219  * spi_flash_munmap function.
220  *
221  * @param partition Pointer to partition structure obtained using
222  *                  esp_partition_find_first or esp_partition_get.
223  *                  Must be non-NULL.
224  * @param offset Offset from the beginning of partition where mapping should start.
225  * @param size Size of the area to be mapped.
226  * @param memory  Memory space where the region should be mapped
227  * @param out_ptr  Output, pointer to the mapped memory region
228  * @param out_handle  Output, handle which should be used for spi_flash_munmap call
229  *
230  * @return ESP_OK, if successful
231  */
232 esp_err_t esp_partition_mmap(const esp_partition_t* partition, uint32_t offset, uint32_t size,
233                              spi_flash_mmap_memory_t memory,
234                              const void** out_ptr, spi_flash_mmap_handle_t* out_handle);
235
236
237 #ifdef __cplusplus
238 }
239 #endif
240
241
242 #endif /* __ESP_PARTITION_H__ */