1 #include "esp_partition.h"
2 #include "nvs_partition_lookup.hpp"
3
4 #ifdef CONFIG_NVS_ENCRYPTION
5 #include "nvs_encrypted_partition.hpp"
6 #endif // CONFIG_NVS_ENCRYPTION
7
8 namespace nvs {
9
10 namespace partition_lookup {
11
lookup_nvs_partition(const char * label,NVSPartition ** p)12 esp_err_t lookup_nvs_partition(const char* label, NVSPartition **p)
13 {
14 const esp_partition_t* esp_partition = esp_partition_find_first(
15 ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, label);
16
17 if (esp_partition == nullptr) {
18 return ESP_ERR_NOT_FOUND;
19 }
20
21 if (esp_partition->encrypted) {
22 return ESP_ERR_NVS_WRONG_ENCRYPTION;
23 }
24
25 NVSPartition *partition = new (std::nothrow) NVSPartition(esp_partition);
26 if (partition == nullptr) {
27 return ESP_ERR_NO_MEM;
28 }
29
30 *p = partition;
31
32 return ESP_OK;
33 }
34
35 #ifdef CONFIG_NVS_ENCRYPTION
lookup_nvs_encrypted_partition(const char * label,nvs_sec_cfg_t * cfg,NVSPartition ** p)36 esp_err_t lookup_nvs_encrypted_partition(const char* label, nvs_sec_cfg_t* cfg, NVSPartition **p)
37 {
38 const esp_partition_t* esp_partition = esp_partition_find_first(
39 ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, label);
40
41 if (esp_partition == nullptr) {
42 return ESP_ERR_NOT_FOUND;
43 }
44
45 if (esp_partition->encrypted) {
46 return ESP_ERR_NVS_WRONG_ENCRYPTION;
47 }
48
49 NVSEncryptedPartition *enc_p = new (std::nothrow) NVSEncryptedPartition(esp_partition);
50 if (enc_p == nullptr) {
51 return ESP_ERR_NO_MEM;
52 }
53
54 esp_err_t result = enc_p->init(cfg);
55 if (result != ESP_OK) {
56 delete enc_p;
57 return result;
58 }
59
60 *p = enc_p;
61
62 return ESP_OK;
63 }
64
65 #endif // CONFIG_NVS_ENCRYPTION
66
67 } // partition_lookup
68
69 } // nvs
70