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 #include "nvs.hpp"
15 #include "nvs_flash.h"
16 #include "nvs_storage.hpp"
17 #include "intrusive_list.h"
18 #include "nvs_platform.hpp"
19 #include "nvs_partition_manager.hpp"
20 #include "esp_partition.h"
21 #include "sdkconfig.h"
22 #include <functional>
23 #include "nvs_handle_simple.hpp"
24 #include "esp_err.h"
25
26 #ifdef LINUX_TARGET
27 #include "crc.h"
28 #define ESP_LOGD(...)
29 #else // LINUX_TARGET
30 #include <esp32/rom/crc.h>
31
32 // Uncomment this line to force output from this module
33 // #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
34 #include "esp_log.h"
35 static const char* TAG = "nvs";
36 #endif // ! LINUX_TARGET
37
38 class NVSHandleEntry : public intrusive_list_node<NVSHandleEntry> {
39 public:
NVSHandleEntry(nvs::NVSHandleSimple * handle,const char * part_name)40 NVSHandleEntry(nvs::NVSHandleSimple *handle, const char* part_name)
41 : nvs_handle(handle),
42 mHandle(++s_nvs_next_handle),
43 handle_part_name(part_name) { }
44
~NVSHandleEntry()45 ~NVSHandleEntry() {
46 delete nvs_handle;
47 }
48
49 nvs::NVSHandleSimple *nvs_handle;
50 nvs_handle_t mHandle;
51 const char* handle_part_name;
52 private:
53 static uint32_t s_nvs_next_handle;
54 };
55
56 uint32_t NVSHandleEntry::s_nvs_next_handle;
57
58 extern "C" void nvs_dump(const char *partName);
59
60 #ifndef LINUX_TARGET
61 SemaphoreHandle_t nvs::Lock::mSemaphore = nullptr;
62 #endif // ! LINUX_TARGET
63
64 using namespace std;
65 using namespace nvs;
66
67 static intrusive_list<NVSHandleEntry> s_nvs_handles;
68
lookup_storage_from_name(const char * name)69 static nvs::Storage* lookup_storage_from_name(const char *name)
70 {
71 return NVSPartitionManager::get_instance()->lookup_storage_from_name(name);
72 }
73
nvs_dump(const char * partName)74 extern "C" void nvs_dump(const char *partName)
75 {
76 Lock lock;
77 nvs::Storage* pStorage;
78
79 pStorage = lookup_storage_from_name(partName);
80 if (pStorage == nullptr) {
81 return;
82 }
83
84 pStorage->debugDump();
85 }
86
close_handles_and_deinit(const char * part_name)87 static esp_err_t close_handles_and_deinit(const char* part_name)
88 {
89 auto belongs_to_part = [=](NVSHandleEntry& e) -> bool {
90 return strncmp(e.nvs_handle->get_partition_name(), part_name, NVS_PART_NAME_MAX_SIZE) == 0;
91 };
92
93 auto it = find_if(begin(s_nvs_handles), end(s_nvs_handles), belongs_to_part);
94
95 while (it != end(s_nvs_handles)) {
96 s_nvs_handles.erase(it);
97 it = find_if(begin(s_nvs_handles), end(s_nvs_handles), belongs_to_part);
98 }
99
100 // Deinit partition
101 return NVSPartitionManager::get_instance()->deinit_partition(part_name);
102 }
103
nvs_flash_init_partition_ptr(const esp_partition_t * partition)104 extern "C" esp_err_t nvs_flash_init_partition_ptr(const esp_partition_t *partition)
105 {
106 Lock::init();
107 Lock lock;
108
109 if (partition == nullptr) {
110 return ESP_ERR_INVALID_ARG;
111 }
112
113 NVSPartition *part = new (std::nothrow) NVSPartition(partition);
114 if (part == nullptr) {
115 return ESP_ERR_NO_MEM;
116 }
117
118 esp_err_t init_res = NVSPartitionManager::get_instance()->init_custom(part,
119 partition->address / SPI_FLASH_SEC_SIZE,
120 partition->size / SPI_FLASH_SEC_SIZE);
121
122 if (init_res != ESP_OK) {
123 delete part;
124 }
125
126 return init_res;
127 }
128
129 #ifndef LINUX_TARGET
nvs_flash_init_partition(const char * part_name)130 extern "C" esp_err_t nvs_flash_init_partition(const char *part_name)
131 {
132 Lock::init();
133 Lock lock;
134
135 return NVSPartitionManager::get_instance()->init_partition(part_name);
136 }
137
nvs_flash_init(void)138 extern "C" esp_err_t nvs_flash_init(void)
139 {
140 #ifdef CONFIG_NVS_ENCRYPTION
141 esp_err_t ret = ESP_FAIL;
142 const esp_partition_t *key_part = esp_partition_find_first(
143 ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS, NULL);
144 if (key_part == NULL) {
145 ESP_LOGE(TAG, "CONFIG_NVS_ENCRYPTION is enabled, but no partition with subtype nvs_keys found in the partition table.");
146 return ret;
147 }
148
149 nvs_sec_cfg_t cfg = {};
150 ret = nvs_flash_read_security_cfg(key_part, &cfg);
151 if (ret == ESP_ERR_NVS_KEYS_NOT_INITIALIZED) {
152 ESP_LOGI(TAG, "NVS key partition empty, generating keys");
153 ret = nvs_flash_generate_keys(key_part, &cfg);
154 if (ret != ESP_OK) {
155 ESP_LOGE(TAG, "Failed to generate keys: [0x%02X] (%s)", ret, esp_err_to_name(ret));
156 return ret;
157 }
158 } else if (ret != ESP_OK) {
159 ESP_LOGE(TAG, "Failed to read NVS security cfg: [0x%02X] (%s)", ret, esp_err_to_name(ret));
160 return ret;
161 }
162
163 ret = nvs_flash_secure_init_partition(NVS_DEFAULT_PART_NAME, &cfg);
164 if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
165 ESP_LOGE(TAG, "Failed to initialize NVS partition: [0x%02X] (%s)", ret, esp_err_to_name(ret));
166 return ret;
167 }
168 ESP_LOGI(TAG, "NVS partition \"%s\" is encrypted.", NVS_DEFAULT_PART_NAME);
169 return ret;
170 #else // CONFIG_NVS_ENCRYPTION
171 return nvs_flash_init_partition(NVS_DEFAULT_PART_NAME);
172 #endif
173 }
174
175 #ifdef CONFIG_NVS_ENCRYPTION
nvs_flash_secure_init_partition(const char * part_name,nvs_sec_cfg_t * cfg)176 extern "C" esp_err_t nvs_flash_secure_init_partition(const char *part_name, nvs_sec_cfg_t* cfg)
177 {
178 Lock::init();
179 Lock lock;
180
181 return NVSPartitionManager::get_instance()->secure_init_partition(part_name, cfg);
182 }
183
nvs_flash_secure_init(nvs_sec_cfg_t * cfg)184 extern "C" esp_err_t nvs_flash_secure_init(nvs_sec_cfg_t* cfg)
185 {
186 return nvs_flash_secure_init_partition(NVS_DEFAULT_PART_NAME, cfg);
187 }
188 #endif
189
nvs_flash_erase_partition(const char * part_name)190 extern "C" esp_err_t nvs_flash_erase_partition(const char *part_name)
191 {
192 Lock::init();
193 Lock lock;
194
195 // if the partition is initialized, uninitialize it first
196 if (NVSPartitionManager::get_instance()->lookup_storage_from_name(part_name)) {
197 esp_err_t err = close_handles_and_deinit(part_name);
198
199 // only hypothetical/future case, deinit_partition() only fails if partition is uninitialized
200 if (err != ESP_OK) {
201 return err;
202 }
203 }
204
205 const esp_partition_t* partition = esp_partition_find_first(
206 ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, part_name);
207 if (partition == nullptr) {
208 return ESP_ERR_NOT_FOUND;
209 }
210
211 return esp_partition_erase_range(partition, 0, partition->size);
212 }
213
nvs_flash_erase_partition_ptr(const esp_partition_t * partition)214 extern "C" esp_err_t nvs_flash_erase_partition_ptr(const esp_partition_t *partition)
215 {
216 Lock::init();
217 Lock lock;
218
219 if (partition == nullptr) {
220 return ESP_ERR_INVALID_ARG;
221 }
222
223 // if the partition is initialized, uninitialize it first
224 if (NVSPartitionManager::get_instance()->lookup_storage_from_name(partition->label)) {
225 const esp_err_t err = close_handles_and_deinit(partition->label);
226
227 // only hypothetical/future case, deinit_partition() only fails if partition is uninitialized
228 if (err != ESP_OK) {
229 return err;
230 }
231 }
232
233 return esp_partition_erase_range(partition, 0, partition->size);
234 }
235
nvs_flash_erase(void)236 extern "C" esp_err_t nvs_flash_erase(void)
237 {
238 return nvs_flash_erase_partition(NVS_DEFAULT_PART_NAME);
239 }
240 #endif // ! LINUX_TARGET
241
nvs_flash_deinit_partition(const char * partition_name)242 extern "C" esp_err_t nvs_flash_deinit_partition(const char* partition_name)
243 {
244 Lock::init();
245 Lock lock;
246
247 return close_handles_and_deinit(partition_name);
248 }
249
nvs_flash_deinit(void)250 extern "C" esp_err_t nvs_flash_deinit(void)
251 {
252 return nvs_flash_deinit_partition(NVS_DEFAULT_PART_NAME);
253 }
254
nvs_find_ns_handle(nvs_handle_t c_handle,NVSHandleSimple ** handle)255 static esp_err_t nvs_find_ns_handle(nvs_handle_t c_handle, NVSHandleSimple** handle)
256 {
257 auto it = find_if(begin(s_nvs_handles), end(s_nvs_handles), [=](NVSHandleEntry& e) -> bool {
258 return e.mHandle == c_handle;
259 });
260 if (it == end(s_nvs_handles)) {
261 return ESP_ERR_NVS_INVALID_HANDLE;
262 }
263 *handle = it->nvs_handle;
264 return ESP_OK;
265 }
266
nvs_open_from_partition(const char * part_name,const char * name,nvs_open_mode_t open_mode,nvs_handle_t * out_handle)267 extern "C" esp_err_t nvs_open_from_partition(const char *part_name, const char* name, nvs_open_mode_t open_mode, nvs_handle_t *out_handle)
268 {
269 Lock lock;
270 ESP_LOGD(TAG, "%s %s %d", __func__, name, open_mode);
271
272 NVSHandleSimple *handle;
273 esp_err_t result = NVSPartitionManager::get_instance()->open_handle(part_name, name, open_mode, &handle);
274 if (result == ESP_OK) {
275 NVSHandleEntry *entry = new (std::nothrow) NVSHandleEntry(handle, part_name);
276 if (entry) {
277 s_nvs_handles.push_back(entry);
278 *out_handle = entry->mHandle;
279 } else {
280 delete handle;
281 return ESP_ERR_NO_MEM;
282 }
283 }
284
285 return result;
286 }
287
nvs_open(const char * name,nvs_open_mode_t open_mode,nvs_handle_t * out_handle)288 extern "C" esp_err_t nvs_open(const char* name, nvs_open_mode_t open_mode, nvs_handle_t *out_handle)
289 {
290 return nvs_open_from_partition(NVS_DEFAULT_PART_NAME, name, open_mode, out_handle);
291 }
292
nvs_close(nvs_handle_t handle)293 extern "C" void nvs_close(nvs_handle_t handle)
294 {
295 Lock lock;
296 ESP_LOGD(TAG, "%s %d", __func__, handle);
297 auto it = find_if(begin(s_nvs_handles), end(s_nvs_handles), [=](NVSHandleEntry& e) -> bool {
298 return e.mHandle == handle;
299 });
300 if (it == end(s_nvs_handles)) {
301 return;
302 }
303 s_nvs_handles.erase(it);
304 delete static_cast<NVSHandleEntry*>(it);
305 }
306
nvs_erase_key(nvs_handle_t c_handle,const char * key)307 extern "C" esp_err_t nvs_erase_key(nvs_handle_t c_handle, const char* key)
308 {
309 Lock lock;
310 ESP_LOGD(TAG, "%s %s\r\n", __func__, key);
311 NVSHandleSimple *handle;
312 auto err = nvs_find_ns_handle(c_handle, &handle);
313 if (err != ESP_OK) {
314 return err;
315 }
316
317 return handle->erase_item(key);
318 }
319
nvs_erase_all(nvs_handle_t c_handle)320 extern "C" esp_err_t nvs_erase_all(nvs_handle_t c_handle)
321 {
322 Lock lock;
323 ESP_LOGD(TAG, "%s\r\n", __func__);
324 NVSHandleSimple *handle;
325 auto err = nvs_find_ns_handle(c_handle, &handle);
326 if (err != ESP_OK) {
327 return err;
328 }
329
330 return handle->erase_all();
331 }
332
333 template<typename T>
nvs_set(nvs_handle_t c_handle,const char * key,T value)334 static esp_err_t nvs_set(nvs_handle_t c_handle, const char* key, T value)
335 {
336 Lock lock;
337 ESP_LOGD(TAG, "%s %s %d %d", __func__, key, sizeof(T), (uint32_t) value);
338 NVSHandleSimple *handle;
339 auto err = nvs_find_ns_handle(c_handle, &handle);
340 if (err != ESP_OK) {
341 return err;
342 }
343
344 return handle->set_item(key, value);
345 }
346
nvs_set_i8(nvs_handle_t handle,const char * key,int8_t value)347 extern "C" esp_err_t nvs_set_i8 (nvs_handle_t handle, const char* key, int8_t value)
348 {
349 return nvs_set(handle, key, value);
350 }
351
nvs_set_u8(nvs_handle_t handle,const char * key,uint8_t value)352 extern "C" esp_err_t nvs_set_u8 (nvs_handle_t handle, const char* key, uint8_t value)
353 {
354 return nvs_set(handle, key, value);
355 }
356
nvs_set_i16(nvs_handle_t handle,const char * key,int16_t value)357 extern "C" esp_err_t nvs_set_i16 (nvs_handle_t handle, const char* key, int16_t value)
358 {
359 return nvs_set(handle, key, value);
360 }
361
nvs_set_u16(nvs_handle_t handle,const char * key,uint16_t value)362 extern "C" esp_err_t nvs_set_u16 (nvs_handle_t handle, const char* key, uint16_t value)
363 {
364 return nvs_set(handle, key, value);
365 }
366
nvs_set_i32(nvs_handle_t handle,const char * key,int32_t value)367 extern "C" esp_err_t nvs_set_i32 (nvs_handle_t handle, const char* key, int32_t value)
368 {
369 return nvs_set(handle, key, value);
370 }
371
nvs_set_u32(nvs_handle_t handle,const char * key,uint32_t value)372 extern "C" esp_err_t nvs_set_u32 (nvs_handle_t handle, const char* key, uint32_t value)
373 {
374 return nvs_set(handle, key, value);
375 }
376
nvs_set_i64(nvs_handle_t handle,const char * key,int64_t value)377 extern "C" esp_err_t nvs_set_i64 (nvs_handle_t handle, const char* key, int64_t value)
378 {
379 return nvs_set(handle, key, value);
380 }
381
nvs_set_u64(nvs_handle_t handle,const char * key,uint64_t value)382 extern "C" esp_err_t nvs_set_u64 (nvs_handle_t handle, const char* key, uint64_t value)
383 {
384 return nvs_set(handle, key, value);
385 }
386
nvs_commit(nvs_handle_t c_handle)387 extern "C" esp_err_t nvs_commit(nvs_handle_t c_handle)
388 {
389 Lock lock;
390 // no-op for now, to be used when intermediate cache is added
391 NVSHandleSimple *handle;
392 auto err = nvs_find_ns_handle(c_handle, &handle);
393 if (err != ESP_OK) {
394 return err;
395 }
396 return handle->commit();
397 }
398
nvs_set_str(nvs_handle_t c_handle,const char * key,const char * value)399 extern "C" esp_err_t nvs_set_str(nvs_handle_t c_handle, const char* key, const char* value)
400 {
401 Lock lock;
402 ESP_LOGD(TAG, "%s %s %s", __func__, key, value);
403 NVSHandleSimple *handle;
404 auto err = nvs_find_ns_handle(c_handle, &handle);
405 if (err != ESP_OK) {
406 return err;
407 }
408 return handle->set_string(key, value);
409 }
410
nvs_set_blob(nvs_handle_t c_handle,const char * key,const void * value,size_t length)411 extern "C" esp_err_t nvs_set_blob(nvs_handle_t c_handle, const char* key, const void* value, size_t length)
412 {
413 Lock lock;
414 ESP_LOGD(TAG, "%s %s %d", __func__, key, length);
415 NVSHandleSimple *handle;
416 auto err = nvs_find_ns_handle(c_handle, &handle);
417 if (err != ESP_OK) {
418 return err;
419 }
420 return handle->set_blob(key, value, length);
421 }
422
423
424 template<typename T>
nvs_get(nvs_handle_t c_handle,const char * key,T * out_value)425 static esp_err_t nvs_get(nvs_handle_t c_handle, const char* key, T* out_value)
426 {
427 Lock lock;
428 ESP_LOGD(TAG, "%s %s %d", __func__, key, sizeof(T));
429 NVSHandleSimple *handle;
430 auto err = nvs_find_ns_handle(c_handle, &handle);
431 if (err != ESP_OK) {
432 return err;
433 }
434 return handle->get_item(key, *out_value);
435 }
436
nvs_get_i8(nvs_handle_t c_handle,const char * key,int8_t * out_value)437 extern "C" esp_err_t nvs_get_i8 (nvs_handle_t c_handle, const char* key, int8_t* out_value)
438 {
439 return nvs_get(c_handle, key, out_value);
440 }
441
nvs_get_u8(nvs_handle_t c_handle,const char * key,uint8_t * out_value)442 extern "C" esp_err_t nvs_get_u8 (nvs_handle_t c_handle, const char* key, uint8_t* out_value)
443 {
444 return nvs_get(c_handle, key, out_value);
445 }
446
nvs_get_i16(nvs_handle_t c_handle,const char * key,int16_t * out_value)447 extern "C" esp_err_t nvs_get_i16 (nvs_handle_t c_handle, const char* key, int16_t* out_value)
448 {
449 return nvs_get(c_handle, key, out_value);
450 }
451
nvs_get_u16(nvs_handle_t c_handle,const char * key,uint16_t * out_value)452 extern "C" esp_err_t nvs_get_u16 (nvs_handle_t c_handle, const char* key, uint16_t* out_value)
453 {
454 return nvs_get(c_handle, key, out_value);
455 }
456
nvs_get_i32(nvs_handle_t c_handle,const char * key,int32_t * out_value)457 extern "C" esp_err_t nvs_get_i32 (nvs_handle_t c_handle, const char* key, int32_t* out_value)
458 {
459 return nvs_get(c_handle, key, out_value);
460 }
461
nvs_get_u32(nvs_handle_t c_handle,const char * key,uint32_t * out_value)462 extern "C" esp_err_t nvs_get_u32 (nvs_handle_t c_handle, const char* key, uint32_t* out_value)
463 {
464 return nvs_get(c_handle, key, out_value);
465 }
466
nvs_get_i64(nvs_handle_t c_handle,const char * key,int64_t * out_value)467 extern "C" esp_err_t nvs_get_i64 (nvs_handle_t c_handle, const char* key, int64_t* out_value)
468 {
469 return nvs_get(c_handle, key, out_value);
470 }
471
nvs_get_u64(nvs_handle_t c_handle,const char * key,uint64_t * out_value)472 extern "C" esp_err_t nvs_get_u64 (nvs_handle_t c_handle, const char* key, uint64_t* out_value)
473 {
474 return nvs_get(c_handle, key, out_value);
475 }
476
nvs_get_str_or_blob(nvs_handle_t c_handle,nvs::ItemType type,const char * key,void * out_value,size_t * length)477 static esp_err_t nvs_get_str_or_blob(nvs_handle_t c_handle, nvs::ItemType type, const char* key, void* out_value, size_t* length)
478 {
479 Lock lock;
480 ESP_LOGD(TAG, "%s %s", __func__, key);
481 NVSHandleSimple *handle;
482 auto err = nvs_find_ns_handle(c_handle, &handle);
483 if (err != ESP_OK) {
484 return err;
485 }
486
487 size_t dataSize;
488 err = handle->get_item_size(type, key, dataSize);
489 if (err != ESP_OK) {
490 return err;
491 }
492
493 if (length == nullptr) {
494 return ESP_ERR_NVS_INVALID_LENGTH;
495 } else if (out_value == nullptr) {
496 *length = dataSize;
497 return ESP_OK;
498 } else if (*length < dataSize) {
499 *length = dataSize;
500 return ESP_ERR_NVS_INVALID_LENGTH;
501 }
502
503 *length = dataSize;
504 return handle->get_typed_item(type, key, out_value, dataSize);
505 }
506
nvs_get_str(nvs_handle_t c_handle,const char * key,char * out_value,size_t * length)507 extern "C" esp_err_t nvs_get_str(nvs_handle_t c_handle, const char* key, char* out_value, size_t* length)
508 {
509 return nvs_get_str_or_blob(c_handle, nvs::ItemType::SZ, key, out_value, length);
510 }
511
nvs_get_blob(nvs_handle_t c_handle,const char * key,void * out_value,size_t * length)512 extern "C" esp_err_t nvs_get_blob(nvs_handle_t c_handle, const char* key, void* out_value, size_t* length)
513 {
514 return nvs_get_str_or_blob(c_handle, nvs::ItemType::BLOB, key, out_value, length);
515 }
516
nvs_get_stats(const char * part_name,nvs_stats_t * nvs_stats)517 extern "C" esp_err_t nvs_get_stats(const char* part_name, nvs_stats_t* nvs_stats)
518 {
519 Lock lock;
520 nvs::Storage* pStorage;
521
522 if (nvs_stats == nullptr) {
523 return ESP_ERR_INVALID_ARG;
524 }
525 nvs_stats->used_entries = 0;
526 nvs_stats->free_entries = 0;
527 nvs_stats->total_entries = 0;
528 nvs_stats->namespace_count = 0;
529
530 pStorage = lookup_storage_from_name((part_name == nullptr) ? NVS_DEFAULT_PART_NAME : part_name);
531 if (pStorage == nullptr) {
532 return ESP_ERR_NVS_NOT_INITIALIZED;
533 }
534
535 if(!pStorage->isValid()){
536 return ESP_ERR_NVS_INVALID_STATE;
537 }
538
539 return pStorage->fillStats(*nvs_stats);
540 }
541
nvs_get_used_entry_count(nvs_handle_t c_handle,size_t * used_entries)542 extern "C" esp_err_t nvs_get_used_entry_count(nvs_handle_t c_handle, size_t* used_entries)
543 {
544 Lock lock;
545 if(used_entries == nullptr){
546 return ESP_ERR_INVALID_ARG;
547 }
548 *used_entries = 0;
549
550 NVSHandleSimple *handle;
551 auto err = nvs_find_ns_handle(c_handle, &handle);
552 if (err != ESP_OK) {
553 return err;
554 }
555
556 size_t used_entry_count;
557 err = handle->get_used_entry_count(used_entry_count);
558 if(err == ESP_OK){
559 *used_entries = used_entry_count;
560 }
561 return err;
562 }
563
564 #if (defined CONFIG_NVS_ENCRYPTION) && (!defined LINUX_TARGET)
565
nvs_flash_generate_keys(const esp_partition_t * partition,nvs_sec_cfg_t * cfg)566 extern "C" esp_err_t nvs_flash_generate_keys(const esp_partition_t* partition, nvs_sec_cfg_t* cfg)
567 {
568 auto err = esp_partition_erase_range(partition, 0, partition->size);
569 if(err != ESP_OK) {
570 return err;
571 }
572
573 for(uint8_t cnt = 0; cnt < NVS_KEY_SIZE; cnt++) {
574 /* Adjacent 16-byte blocks should be different */
575 if (((cnt / 16) & 1) == 0) {
576 cfg->eky[cnt] = 0xff;
577 cfg->tky[cnt] = 0xee;
578 } else {
579 cfg->eky[cnt] = 0x99;
580 cfg->tky[cnt] = 0x88;
581 }
582 }
583
584 /**
585 * Write key configuration without encryption engine (using raw partition write APIs).
586 * But the read is decrypted through flash encryption engine. This allows unique NVS encryption configuration,
587 * as flash encryption key is randomly generated per device.
588 */
589 err = esp_partition_write_raw(partition, 0, cfg->eky, NVS_KEY_SIZE);
590 if(err != ESP_OK) {
591 return err;
592 }
593
594 /* Write without encryption, see note above */
595 err = esp_partition_write_raw(partition, NVS_KEY_SIZE, cfg->tky, NVS_KEY_SIZE);
596 if(err != ESP_OK) {
597 return err;
598 }
599
600 err = esp_partition_read(partition, 0, cfg->eky, NVS_KEY_SIZE);
601 if(err != ESP_OK) {
602 return err;
603 }
604
605 err = esp_partition_read(partition, NVS_KEY_SIZE, cfg->tky, NVS_KEY_SIZE);
606 if(err != ESP_OK) {
607 return err;
608 }
609
610 uint32_t crc_calc = crc32_le(0xffffffff, cfg->eky, NVS_KEY_SIZE);
611 crc_calc = crc32_le(crc_calc, cfg->tky, NVS_KEY_SIZE);
612
613 uint8_t crc_wr[16];
614 memset(crc_wr, 0xff, sizeof(crc_wr));
615 memcpy(crc_wr, &crc_calc, 4);
616
617 err = esp_partition_write(partition, 2 * NVS_KEY_SIZE, crc_wr, sizeof(crc_wr));
618 if(err != ESP_OK) {
619 return err;
620 }
621
622 return ESP_OK;
623
624 }
625
nvs_flash_read_security_cfg(const esp_partition_t * partition,nvs_sec_cfg_t * cfg)626 extern "C" esp_err_t nvs_flash_read_security_cfg(const esp_partition_t* partition, nvs_sec_cfg_t* cfg)
627 {
628 uint8_t eky_raw[NVS_KEY_SIZE], tky_raw[NVS_KEY_SIZE];
629 uint32_t crc_raw, crc_read, crc_calc;
630
631 auto check_if_initialized = [](uint8_t* eky, uint8_t* tky, uint32_t crc) {
632 uint8_t cnt = 0;
633 while(cnt < NVS_KEY_SIZE && eky[cnt] == 0xff && tky[cnt] == 0xff) cnt++;
634
635 if(cnt == NVS_KEY_SIZE && crc == 0xffffffff) {
636 return false;
637 }
638 return true;
639 };
640
641 auto err = esp_partition_read_raw(partition, 0, eky_raw, NVS_KEY_SIZE);
642 if(err != ESP_OK) {
643 return err;
644 }
645
646 err = esp_partition_read_raw(partition, NVS_KEY_SIZE, tky_raw, NVS_KEY_SIZE);
647 if(err != ESP_OK) {
648 return err;
649 }
650
651 err = esp_partition_read_raw(partition, 2 * NVS_KEY_SIZE, &crc_raw, 4);
652 if(err != ESP_OK) {
653 return err;
654 }
655
656 if(!check_if_initialized(eky_raw, tky_raw, crc_raw)) {
657 /* This is an uninitialized key partition*/
658 return ESP_ERR_NVS_KEYS_NOT_INITIALIZED;
659 }
660
661 err = esp_partition_read(partition, 0, cfg->eky, NVS_KEY_SIZE);
662
663 if(err != ESP_OK) {
664 return err;
665 }
666
667 err = esp_partition_read(partition, NVS_KEY_SIZE, cfg->tky, NVS_KEY_SIZE);
668
669 if(err != ESP_OK) {
670 return err;
671 }
672
673 err = esp_partition_read(partition, 2 * NVS_KEY_SIZE, &crc_read, 4);
674
675 if(err != ESP_OK) {
676 return err;
677 }
678
679 crc_calc = crc32_le(0xffffffff, cfg->eky, NVS_KEY_SIZE);
680 crc_calc = crc32_le(crc_calc, cfg->tky, NVS_KEY_SIZE);
681
682 if(crc_calc != crc_read) {
683 if(!check_if_initialized(cfg->eky, cfg->tky, crc_read)) {
684 /* This is an uninitialized key partition*/
685 return ESP_ERR_NVS_KEYS_NOT_INITIALIZED;
686 }
687 return ESP_ERR_NVS_CORRUPT_KEY_PART;
688 }
689
690 return ESP_OK;
691 }
692
693 #endif
694
create_iterator(nvs::Storage * storage,nvs_type_t type)695 static nvs_iterator_t create_iterator(nvs::Storage *storage, nvs_type_t type)
696 {
697 nvs_iterator_t it = (nvs_iterator_t)calloc(1, sizeof(nvs_opaque_iterator_t));
698 if (it == nullptr) {
699 return nullptr;
700 }
701
702 it->storage = storage;
703 it->type = type;
704
705 return it;
706 }
707
nvs_entry_find(const char * part_name,const char * namespace_name,nvs_type_t type)708 extern "C" nvs_iterator_t nvs_entry_find(const char *part_name, const char *namespace_name, nvs_type_t type)
709 {
710 Lock lock;
711 nvs::Storage *pStorage;
712
713 pStorage = lookup_storage_from_name(part_name);
714 if (pStorage == nullptr) {
715 return nullptr;
716 }
717
718 nvs_iterator_t it = create_iterator(pStorage, type);
719 if (it == nullptr) {
720 return nullptr;
721 }
722
723 bool entryFound = pStorage->findEntry(it, namespace_name);
724 if (!entryFound) {
725 free(it);
726 return nullptr;
727 }
728
729 return it;
730 }
731
nvs_entry_next(nvs_iterator_t it)732 extern "C" nvs_iterator_t nvs_entry_next(nvs_iterator_t it)
733 {
734 Lock lock;
735 assert(it);
736
737 bool entryFound = it->storage->nextEntry(it);
738 if (!entryFound) {
739 free(it);
740 return nullptr;
741 }
742
743 return it;
744 }
745
nvs_entry_info(nvs_iterator_t it,nvs_entry_info_t * out_info)746 extern "C" void nvs_entry_info(nvs_iterator_t it, nvs_entry_info_t *out_info)
747 {
748 *out_info = it->entry_info;
749 }
750
nvs_release_iterator(nvs_iterator_t it)751 extern "C" void nvs_release_iterator(nvs_iterator_t it)
752 {
753 free(it);
754 }
755