• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_flash.h"
23 #include "esp_spi_flash.h"
24 
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * @file esp_partition.h
32  * @brief Partition APIs
33  */
34 
35 
36 /**
37  * @brief Partition type
38  *
39  * @note Partition types with integer value 0x00-0x3F are reserved for partition types defined by ESP-IDF.
40  * Any other integer value 0x40-0xFE can be used by individual applications, without restriction.
41  *
42  * @internal Keep this enum in sync with PartitionDefinition class gen_esp32part.py @endinternal
43  *
44  */
45 typedef enum {
46     ESP_PARTITION_TYPE_APP = 0x00,       //!< Application partition type
47     ESP_PARTITION_TYPE_DATA = 0x01,      //!< Data partition type
48 } esp_partition_type_t;
49 
50 /**
51  * @brief Partition subtype
52  *
53  * @note These ESP-IDF-defined partition subtypes apply to partitions of type ESP_PARTITION_TYPE_APP
54  * and ESP_PARTITION_TYPE_DATA.
55  *
56  * Application-defined partition types (0x40-0xFE) can set any numeric subtype value.
57  *
58  * @internal Keep this enum in sync with PartitionDefinition class gen_esp32part.py @endinternal
59  */
60 typedef enum {
61     ESP_PARTITION_SUBTYPE_APP_FACTORY = 0x00,                                 //!< Factory application partition
62     ESP_PARTITION_SUBTYPE_APP_OTA_MIN = 0x10,                                 //!< Base for OTA partition subtypes
63     ESP_PARTITION_SUBTYPE_APP_OTA_0 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 0,  //!< OTA partition 0
64     ESP_PARTITION_SUBTYPE_APP_OTA_1 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 1,  //!< OTA partition 1
65     ESP_PARTITION_SUBTYPE_APP_OTA_2 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 2,  //!< OTA partition 2
66     ESP_PARTITION_SUBTYPE_APP_OTA_3 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 3,  //!< OTA partition 3
67     ESP_PARTITION_SUBTYPE_APP_OTA_4 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 4,  //!< OTA partition 4
68     ESP_PARTITION_SUBTYPE_APP_OTA_5 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 5,  //!< OTA partition 5
69     ESP_PARTITION_SUBTYPE_APP_OTA_6 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 6,  //!< OTA partition 6
70     ESP_PARTITION_SUBTYPE_APP_OTA_7 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 7,  //!< OTA partition 7
71     ESP_PARTITION_SUBTYPE_APP_OTA_8 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 8,  //!< OTA partition 8
72     ESP_PARTITION_SUBTYPE_APP_OTA_9 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 9,  //!< OTA partition 9
73     ESP_PARTITION_SUBTYPE_APP_OTA_10 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 10,//!< OTA partition 10
74     ESP_PARTITION_SUBTYPE_APP_OTA_11 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 11,//!< OTA partition 11
75     ESP_PARTITION_SUBTYPE_APP_OTA_12 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 12,//!< OTA partition 12
76     ESP_PARTITION_SUBTYPE_APP_OTA_13 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 13,//!< OTA partition 13
77     ESP_PARTITION_SUBTYPE_APP_OTA_14 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 14,//!< OTA partition 14
78     ESP_PARTITION_SUBTYPE_APP_OTA_15 = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 15,//!< OTA partition 15
79     ESP_PARTITION_SUBTYPE_APP_OTA_MAX = ESP_PARTITION_SUBTYPE_APP_OTA_MIN + 16,//!< Max subtype of OTA partition
80     ESP_PARTITION_SUBTYPE_APP_TEST = 0x20,                                    //!< Test application partition
81 
82     ESP_PARTITION_SUBTYPE_DATA_OTA = 0x00,                                    //!< OTA selection partition
83     ESP_PARTITION_SUBTYPE_DATA_PHY = 0x01,                                    //!< PHY init data partition
84     ESP_PARTITION_SUBTYPE_DATA_NVS = 0x02,                                    //!< NVS partition
85     ESP_PARTITION_SUBTYPE_DATA_COREDUMP = 0x03,                               //!< COREDUMP partition
86     ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS = 0x04,                               //!< Partition for NVS keys
87     ESP_PARTITION_SUBTYPE_DATA_EFUSE_EM = 0x05,                               //!< Partition for emulate eFuse bits
88 
89     ESP_PARTITION_SUBTYPE_DATA_ESPHTTPD = 0x80,                               //!< ESPHTTPD partition
90     ESP_PARTITION_SUBTYPE_DATA_FAT = 0x81,                                    //!< FAT partition
91     ESP_PARTITION_SUBTYPE_DATA_SPIFFS = 0x82,                                 //!< SPIFFS partition
92 
93     ESP_PARTITION_SUBTYPE_ANY = 0xff,                                         //!< Used to search for partitions with any subtype
94 } esp_partition_subtype_t;
95 
96 /**
97  * @brief Convenience macro to get esp_partition_subtype_t value for the i-th OTA partition
98  */
99 #define ESP_PARTITION_SUBTYPE_OTA(i) ((esp_partition_subtype_t)(ESP_PARTITION_SUBTYPE_APP_OTA_MIN + ((i) & 0xf)))
100 
101 /**
102  * @brief Opaque partition iterator type
103  */
104 typedef struct esp_partition_iterator_opaque_* esp_partition_iterator_t;
105 
106 /**
107  * @brief partition information structure
108  *
109  * This is not the format in flash, that format is esp_partition_info_t.
110  *
111  * However, this is the format used by this API.
112  */
113 typedef struct {
114     esp_flash_t* flash_chip;            /*!< SPI flash chip on which the partition resides */
115     esp_partition_type_t type;          /*!< partition type (app/data) */
116     esp_partition_subtype_t subtype;    /*!< partition subtype */
117     uint32_t address;                   /*!< starting address of the partition in flash */
118     uint32_t size;                      /*!< size of the partition, in bytes */
119     char label[17];                     /*!< partition label, zero-terminated ASCII string */
120     bool encrypted;                     /*!< flag is set to true if partition is encrypted */
121 } esp_partition_t;
122 
123 /**
124  * @brief Find partition based on one or more parameters
125  *
126  * @param type Partition type, one of esp_partition_type_t values or an 8-bit unsigned integer
127  * @param subtype Partition subtype, one of esp_partition_subtype_t values or an 8-bit unsigned integer.
128  *                To find all partitions of given type, use ESP_PARTITION_SUBTYPE_ANY.
129  * @param label (optional) Partition label. Set this value if looking
130  *             for partition with a specific name. Pass NULL otherwise.
131  *
132  * @return iterator which can be used to enumerate all the partitions found,
133  *         or NULL if no partitions were found.
134  *         Iterator obtained through this function has to be released
135  *         using esp_partition_iterator_release when not used any more.
136  */
137 esp_partition_iterator_t esp_partition_find(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label);
138 
139 /**
140  * @brief Find first partition based on one or more parameters
141  *
142  * @param type Partition type, one of esp_partition_type_t values or an 8-bit unsigned integer
143  * @param subtype Partition subtype, one of esp_partition_subtype_t values or an 8-bit unsigned integer
144  *                To find all partitions of given type, use ESP_PARTITION_SUBTYPE_ANY.
145  * @param label (optional) Partition label. Set this value if looking
146  *             for partition with a specific name. Pass NULL otherwise.
147  *
148  * @return pointer to esp_partition_t structure, or NULL if no partition is found.
149  *         This pointer is valid for the lifetime of the application.
150  */
151 const esp_partition_t* esp_partition_find_first(esp_partition_type_t type, esp_partition_subtype_t subtype, const char* label);
152 
153 /**
154  * @brief Get esp_partition_t structure for given partition
155  *
156  * @param iterator  Iterator obtained using esp_partition_find. Must be non-NULL.
157  *
158  * @return pointer to esp_partition_t structure. This pointer is valid for the lifetime
159  *         of the application.
160  */
161 const esp_partition_t* esp_partition_get(esp_partition_iterator_t iterator);
162 
163 /**
164  * @brief Move partition iterator to the next partition found
165  *
166  * Any copies of the iterator will be invalid after this call.
167  *
168  * @param iterator Iterator obtained using esp_partition_find. Must be non-NULL.
169  *
170  * @return NULL if no partition was found, valid esp_partition_iterator_t otherwise.
171  */
172 esp_partition_iterator_t esp_partition_next(esp_partition_iterator_t iterator);
173 
174 /**
175  * @brief Release partition iterator
176  *
177  * @param iterator Iterator obtained using esp_partition_find. Must be non-NULL.
178  *
179  */
180 void esp_partition_iterator_release(esp_partition_iterator_t iterator);
181 
182 /**
183  * @brief Verify partition data
184  *
185  * Given a pointer to partition data, verify this partition exists in the partition table (all fields match.)
186  *
187  * This function is also useful to take partition data which may be in a RAM buffer and convert it to a pointer to the
188  * permanent partition data stored in flash.
189  *
190  * Pointers returned from this function can be compared directly to the address of any pointer returned from
191  * esp_partition_get(), as a test for equality.
192  *
193  * @param partition Pointer to partition data to verify. Must be non-NULL. All fields of this structure must match the
194  * partition table entry in flash for this function to return a successful match.
195  *
196  * @return
197  * - If partition not found, returns NULL.
198  * - If found, returns a pointer to the esp_partition_t structure in flash. This pointer is always valid for the lifetime of the application.
199  */
200 const esp_partition_t *esp_partition_verify(const esp_partition_t *partition);
201 
202 /**
203  * @brief Read data from the partition
204  *
205  * Partitions marked with an encryption flag will automatically be
206  * be read and decrypted via a cache mapping.
207  *
208  * @param partition Pointer to partition structure obtained using
209  *                  esp_partition_find_first or esp_partition_get.
210  *                  Must be non-NULL.
211  * @param dst Pointer to the buffer where data should be stored.
212  *            Pointer must be non-NULL and buffer must be at least 'size' bytes long.
213  * @param src_offset Address of the data to be read, relative to the
214  *                   beginning of the partition.
215  * @param size Size of data to be read, in bytes.
216  *
217  * @return ESP_OK, if data was read successfully;
218  *         ESP_ERR_INVALID_ARG, if src_offset exceeds partition size;
219  *         ESP_ERR_INVALID_SIZE, if read would go out of bounds of the partition;
220  *         or one of error codes from lower-level flash driver.
221  */
222 esp_err_t esp_partition_read(const esp_partition_t* partition,
223                              size_t src_offset, void* dst, size_t size);
224 
225 /**
226  * @brief Write data to the partition
227  *
228  * Before writing data to flash, corresponding region of flash needs to be erased.
229  * This can be done using esp_partition_erase_range function.
230  *
231  * Partitions marked with an encryption flag will automatically be
232  * written via the spi_flash_write_encrypted() function. If writing to
233  * an encrypted partition, all write offsets and lengths must be
234  * multiples of 16 bytes. See the spi_flash_write_encrypted() function
235  * for more details. Unencrypted partitions do not have this
236  * restriction.
237  *
238  * @param partition Pointer to partition structure obtained using
239  *                  esp_partition_find_first or esp_partition_get.
240  *                  Must be non-NULL.
241  * @param dst_offset Address where the data should be written, relative to the
242  *                   beginning of the partition.
243  * @param src Pointer to the source buffer.  Pointer must be non-NULL and
244  *            buffer must be at least 'size' bytes long.
245  * @param size Size of data to be written, in bytes.
246  *
247  * @note Prior to writing to flash memory, make sure it has been erased with
248  *       esp_partition_erase_range call.
249  *
250  * @return ESP_OK, if data was written successfully;
251  *         ESP_ERR_INVALID_ARG, if dst_offset exceeds partition size;
252  *         ESP_ERR_INVALID_SIZE, if write would go out of bounds of the partition;
253  *         or one of error codes from lower-level flash driver.
254  */
255 esp_err_t esp_partition_write(const esp_partition_t* partition,
256                               size_t dst_offset, const void* src, size_t size);
257 
258 /**
259  * @brief Read data from the partition without any transformation/decryption.
260  *
261  * @note This function is essentially the same as \c esp_partition_read() above.
262  *       It just never decrypts data but returns it as is.
263  *
264  * @param partition Pointer to partition structure obtained using
265  *                  esp_partition_find_first or esp_partition_get.
266  *                  Must be non-NULL.
267  * @param dst Pointer to the buffer where data should be stored.
268  *            Pointer must be non-NULL and buffer must be at least 'size' bytes long.
269  * @param src_offset Address of the data to be read, relative to the
270  *                   beginning of the partition.
271  * @param size Size of data to be read, in bytes.
272  *
273  * @return ESP_OK, if data was read successfully;
274  *         ESP_ERR_INVALID_ARG, if src_offset exceeds partition size;
275  *         ESP_ERR_INVALID_SIZE, if read would go out of bounds of the partition;
276  *         or one of error codes from lower-level flash driver.
277  */
278 esp_err_t esp_partition_read_raw(const esp_partition_t* partition,
279                                  size_t src_offset, void* dst, size_t size);
280 
281 /**
282  * @brief Write data to the partition without any transformation/encryption.
283  *
284  * @note This function is essentially the same as \c esp_partition_write() above.
285  *       It just never encrypts data but writes it as is.
286  *
287  * Before writing data to flash, corresponding region of flash needs to be erased.
288  * This can be done using esp_partition_erase_range function.
289  *
290  * @param partition Pointer to partition structure obtained using
291  *                  esp_partition_find_first or esp_partition_get.
292  *                  Must be non-NULL.
293  * @param dst_offset Address where the data should be written, relative to the
294  *                   beginning of the partition.
295  * @param src Pointer to the source buffer.  Pointer must be non-NULL and
296  *            buffer must be at least 'size' bytes long.
297  * @param size Size of data to be written, in bytes.
298  *
299  * @note Prior to writing to flash memory, make sure it has been erased with
300  *       esp_partition_erase_range call.
301  *
302  * @return ESP_OK, if data was written successfully;
303  *         ESP_ERR_INVALID_ARG, if dst_offset exceeds partition size;
304  *         ESP_ERR_INVALID_SIZE, if write would go out of bounds of the partition;
305  *         or one of the error codes from lower-level flash driver.
306  */
307 esp_err_t esp_partition_write_raw(const esp_partition_t* partition,
308                                   size_t dst_offset, const void* src, size_t size);
309 
310 /**
311  * @brief Erase part of the partition
312  *
313  * @param partition Pointer to partition structure obtained using
314  *                  esp_partition_find_first or esp_partition_get.
315  *                  Must be non-NULL.
316  * @param offset Offset from the beginning of partition where erase operation
317  *               should start. Must be aligned to 4 kilobytes.
318  * @param size Size of the range which should be erased, in bytes.
319  *                   Must be divisible by 4 kilobytes.
320  *
321  * @return ESP_OK, if the range was erased successfully;
322  *         ESP_ERR_INVALID_ARG, if iterator or dst are NULL;
323  *         ESP_ERR_INVALID_SIZE, if erase would go out of bounds of the partition;
324  *         or one of error codes from lower-level flash driver.
325  */
326 esp_err_t esp_partition_erase_range(const esp_partition_t* partition,
327                                     size_t offset, size_t size);
328 
329 /**
330  * @brief Configure MMU to map partition into data memory
331  *
332  * Unlike spi_flash_mmap function, which requires a 64kB aligned base address,
333  * this function doesn't impose such a requirement.
334  * If offset results in a flash address which is not aligned to 64kB boundary,
335  * address will be rounded to the lower 64kB boundary, so that mapped region
336  * includes requested range.
337  * Pointer returned via out_ptr argument will be adjusted to point to the
338  * requested offset (not necessarily to the beginning of mmap-ed region).
339  *
340  * To release mapped memory, pass handle returned via out_handle argument to
341  * spi_flash_munmap function.
342  *
343  * @param partition Pointer to partition structure obtained using
344  *                  esp_partition_find_first or esp_partition_get.
345  *                  Must be non-NULL.
346  * @param offset Offset from the beginning of partition where mapping should start.
347  * @param size Size of the area to be mapped.
348  * @param memory  Memory space where the region should be mapped
349  * @param out_ptr  Output, pointer to the mapped memory region
350  * @param out_handle  Output, handle which should be used for spi_flash_munmap call
351  *
352  * @return ESP_OK, if successful
353  */
354 esp_err_t esp_partition_mmap(const esp_partition_t* partition, size_t offset, size_t size,
355                              spi_flash_mmap_memory_t memory,
356                              const void** out_ptr, spi_flash_mmap_handle_t* out_handle);
357 
358 /**
359  * @brief Get SHA-256 digest for required partition.
360  *
361  * For apps with SHA-256 appended to the app image, the result is the appended SHA-256 value for the app image content.
362  * The hash is verified before returning, if app content is invalid then the function returns ESP_ERR_IMAGE_INVALID.
363  * For apps without SHA-256 appended to the image, the result is the SHA-256 of all bytes in the app image.
364  * For other partition types, the result is the SHA-256 of the entire partition.
365  *
366  * @param[in]  partition    Pointer to info for partition containing app or data. (fields: address, size and type, are required to be filled).
367  * @param[out] sha_256      Returned SHA-256 digest for a given partition.
368  *
369  * @return
370  *          - ESP_OK: In case of successful operation.
371  *          - ESP_ERR_INVALID_ARG: The size was 0 or the sha_256 was NULL.
372  *          - ESP_ERR_NO_MEM: Cannot allocate memory for sha256 operation.
373  *          - ESP_ERR_IMAGE_INVALID: App partition doesn't contain a valid app image.
374  *          - ESP_FAIL: An allocation error occurred.
375  */
376 esp_err_t esp_partition_get_sha256(const esp_partition_t *partition, uint8_t *sha_256);
377 
378 /**
379  * @brief Check for the identity of two partitions by SHA-256 digest.
380  *
381  * @param[in] partition_1 Pointer to info for partition 1 containing app or data. (fields: address, size and type, are required to be filled).
382  * @param[in] partition_2 Pointer to info for partition 2 containing app or data. (fields: address, size and type, are required to be filled).
383  *
384  * @return
385  *         - True:  In case of the two firmware is equal.
386  *         - False: Otherwise
387  */
388 bool esp_partition_check_identity(const esp_partition_t *partition_1, const esp_partition_t *partition_2);
389 
390 /**
391  * @brief Register a partition on an external flash chip
392  *
393  * This API allows designating certain areas of external flash chips (identified by the esp_flash_t structure)
394  * as partitions. This allows using them with components which access SPI flash through the esp_partition API.
395  *
396  * @param flash_chip  Pointer to the structure identifying the flash chip
397  * @param offset  Address in bytes, where the partition starts
398  * @param size  Size of the partition in bytes
399  * @param label  Partition name
400  * @param type  One of the partition types (ESP_PARTITION_TYPE_*), or an integer. Note that applications can not be booted from external flash
401  *              chips, so using ESP_PARTITION_TYPE_APP is not supported.
402  * @param subtype  One of the partition subtypes (ESP_PARTITION_SUBTYPE_*), or an integer.
403  * @param[out] out_partition  Output, if non-NULL, receives the pointer to the resulting esp_partition_t structure
404  * @return
405  *      - ESP_OK on success
406  *      - ESP_ERR_NOT_SUPPORTED if CONFIG_CONFIG_SPI_FLASH_USE_LEGACY_IMPL is enabled
407  *      - ESP_ERR_NO_MEM if memory allocation has failed
408  *      - ESP_ERR_INVALID_ARG if the new partition overlaps another partition on the same flash chip
409  *      - ESP_ERR_INVALID_SIZE if the partition doesn't fit into the flash chip size
410  */
411 esp_err_t esp_partition_register_external(esp_flash_t* flash_chip, size_t offset, size_t size,
412                                      const char* label, esp_partition_type_t type, esp_partition_subtype_t subtype,
413                                      const esp_partition_t** out_partition);
414 
415 /**
416  * @brief Deregister the partition previously registered using esp_partition_register_external
417  * @param partition  pointer to the partition structure obtained from esp_partition_register_external,
418  * @return
419  *      - ESP_OK on success
420  *      - ESP_ERR_NOT_FOUND if the partition pointer is not found
421  *      - ESP_ERR_INVALID_ARG if the partition comes from the partition table
422  *      - ESP_ERR_INVALID_ARG if the partition was not registered using
423  *        esp_partition_register_external function.
424  */
425 esp_err_t esp_partition_deregister_external(const esp_partition_t* partition);
426 
427 #ifdef __cplusplus
428 }
429 #endif
430 
431 
432 #endif /* __ESP_PARTITION_H__ */
433