1 // Copyright 2019 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_HPP_
16 #define ESP_PARTITION_HPP_
17 
18 #include "esp_partition.h"
19 #include "intrusive_list.h"
20 #include "partition.hpp"
21 
22 #define ESP_ENCRYPT_BLOCK_SIZE 16
23 
24 #define PART_NAME_MAX_SIZE              16   /*!< maximum length of partition name (excluding null terminator) */
25 
26 namespace nvs {
27 
28 /**
29  * Implementation of Partition for NVS.
30  *
31  * It is implemented as an intrusive_list_node to easily store instances of it. NVSStorage and NVSPage take pointer
32  * references of this class to abstract their partition operations.
33  */
34 class NVSPartition : public Partition, public intrusive_list_node<NVSPartition> {
35 public:
36     /**
37      * Copy partition_name to mPartitionName and initialize mESPPartition.
38      *
39      * @param partition_name the name of the partition as in the partition table, must be non-NULL!
40      * @param partition an already initialized partition structure
41      */
42     NVSPartition(const esp_partition_t* partition);
43 
44     /**
45      * No need to de-initialize mESPPartition here, if you used esp_partition_find_first.
46      * Otherwise, the user is responsible for de-initializing it.
47      */
~NVSPartition()48     virtual ~NVSPartition() { }
49 
50     const char *get_partition_name() override;
51 
52     /**
53      * Look into \c esp_partition_read_raw for more details.
54      *
55      * @return
56      *      - ESP_OK on success
57      *      - other error codes from the esp_partition API
58      */
59     esp_err_t read_raw(size_t src_offset, void* dst, size_t size) override;
60 
61     /**
62      * Look into \c esp_partition_read for more details.
63      *
64      * @return
65      *      - ESP_OK on success
66      *      - ESP_ERR_INVALID_ARG if size isn't a multiple of ESP_ENCRYPT_BLOCK_SIZE
67      *      - other error codes from the esp_partition API
68      */
69     esp_err_t read(size_t src_offset, void* dst, size_t size) override;
70 
71     /**
72      * Look into \c esp_partition_write_raw for more details.
73      *
74      * @return
75      *      - ESP_OK on success
76      *      - error codes from the esp_partition API
77      */
78     esp_err_t write_raw(size_t dst_offset, const void* src, size_t size) override;
79 
80     /**
81      * Look into \c esp_partition_write for more details.
82      *
83      * @return
84      *      - ESP_OK on success
85      *      - ESP_ERR_INVALID_ARG if size isn't a multiple of ESP_ENCRYPT_BLOCK_SIZE
86      *      - other error codes from the esp_partition API
87      */
88     esp_err_t write(size_t dst_offset, const void* src, size_t size) override;
89 
90     /**
91      * Look into \c esp_partition_erase_range for more details.
92      *
93      * @return
94      *      - ESP_OK on success
95      *      - error codes from the esp_partition API
96      */
97     esp_err_t erase_range(size_t dst_offset, size_t size) override;
98 
99     /**
100      * @return the base address of the partition.
101      */
102     uint32_t get_address() override;
103 
104     /**
105      * @return the size of the partition in bytes.
106      */
107     uint32_t get_size() override;
108 
109 protected:
110     const esp_partition_t* mESPPartition;
111 };
112 
113 } // nvs
114 
115 #endif // ESP_PARTITION_HPP_
116