2021-11-24 21:48:11 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-11-24 22:32:40 -05:00
|
|
|
#include "esp_partition.h"
|
|
|
|
#include "nvs_partition_manager.hpp"
|
2020-04-26 20:51:31 -04:00
|
|
|
#include "nvs_partition_lookup.hpp"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NVS_ENCRYPTION
|
|
|
|
#include "nvs_encrypted_partition.hpp"
|
|
|
|
#endif // CONFIG_NVS_ENCRYPTION
|
2019-11-24 22:32:40 -05:00
|
|
|
|
|
|
|
namespace nvs {
|
|
|
|
|
|
|
|
NVSPartitionManager* NVSPartitionManager::instance = nullptr;
|
|
|
|
|
|
|
|
NVSPartitionManager* NVSPartitionManager::get_instance()
|
|
|
|
{
|
|
|
|
if (!instance) {
|
2019-12-06 01:36:28 -05:00
|
|
|
instance = new (std::nothrow) NVSPartitionManager();
|
2019-11-24 22:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ESP_PLATFORM
|
|
|
|
esp_err_t NVSPartitionManager::init_partition(const char *partition_label)
|
|
|
|
{
|
2020-04-26 20:51:31 -04:00
|
|
|
if (strlen(partition_label) > NVS_PART_NAME_MAX_SIZE) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t size;
|
2019-11-24 22:32:40 -05:00
|
|
|
Storage* mStorage;
|
|
|
|
|
|
|
|
mStorage = lookup_storage_from_name(partition_label);
|
|
|
|
if (mStorage) {
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(SPI_FLASH_SEC_SIZE != 0);
|
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
NVSPartition *p = nullptr;
|
2020-09-18 03:38:40 -04:00
|
|
|
esp_err_t result = partition_lookup::lookup_nvs_partition(partition_label, &p);
|
2020-04-26 20:51:31 -04:00
|
|
|
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = p->get_size();
|
|
|
|
|
|
|
|
result = init_custom(p, 0, size / SPI_FLASH_SEC_SIZE);
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
goto error;
|
2019-11-24 22:32:40 -05:00
|
|
|
}
|
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
nvs_partition_list.push_back(p);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
error:
|
|
|
|
delete p;
|
|
|
|
return result;
|
2019-11-24 22:32:40 -05:00
|
|
|
}
|
|
|
|
#endif // ESP_PLATFORM
|
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
esp_err_t NVSPartitionManager::init_custom(Partition *partition, uint32_t baseSector, uint32_t sectorCount)
|
2019-11-24 22:32:40 -05:00
|
|
|
{
|
2020-04-26 20:51:31 -04:00
|
|
|
Storage* new_storage = nullptr;
|
|
|
|
Storage* storage = lookup_storage_from_name(partition->get_partition_name());
|
|
|
|
if (storage == nullptr) {
|
|
|
|
new_storage = new (std::nothrow) Storage(partition);
|
2019-11-24 22:32:40 -05:00
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
if (new_storage == nullptr) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
2019-12-06 01:36:28 -05:00
|
|
|
|
2019-11-24 22:32:40 -05:00
|
|
|
storage = new_storage;
|
2020-04-26 20:51:31 -04:00
|
|
|
} else {
|
|
|
|
// if storage was initialized already, we don't need partition and hence delete it
|
|
|
|
for (auto it = nvs_partition_list.begin(); it != nvs_partition_list.end(); ++it) {
|
|
|
|
if (partition == it) {
|
|
|
|
nvs_partition_list.erase(it);
|
|
|
|
delete partition;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 22:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t err = storage->init(baseSector, sectorCount);
|
2020-04-26 20:51:31 -04:00
|
|
|
if (new_storage != nullptr) {
|
2019-11-24 22:32:40 -05:00
|
|
|
if (err == ESP_OK) {
|
|
|
|
nvs_storage_list.push_back(new_storage);
|
|
|
|
} else {
|
|
|
|
delete new_storage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_NVS_ENCRYPTION
|
2020-04-26 20:51:31 -04:00
|
|
|
#ifdef ESP_PLATFORM
|
2019-11-24 22:32:40 -05:00
|
|
|
esp_err_t NVSPartitionManager::secure_init_partition(const char *part_name, nvs_sec_cfg_t* cfg)
|
|
|
|
{
|
2020-04-26 20:51:31 -04:00
|
|
|
if (strlen(part_name) > NVS_PART_NAME_MAX_SIZE) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2019-11-24 22:32:40 -05:00
|
|
|
Storage* mStorage;
|
|
|
|
|
|
|
|
mStorage = lookup_storage_from_name(part_name);
|
2020-04-26 20:51:31 -04:00
|
|
|
if (mStorage != nullptr) {
|
2019-11-24 22:32:40 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
NVSPartition *p;
|
|
|
|
esp_err_t result;
|
|
|
|
if (cfg != nullptr) {
|
2020-09-18 03:38:40 -04:00
|
|
|
result = partition_lookup::lookup_nvs_encrypted_partition(part_name, cfg, &p);
|
2020-04-26 20:51:31 -04:00
|
|
|
} else {
|
2020-09-18 03:38:40 -04:00
|
|
|
result = partition_lookup::lookup_nvs_partition(part_name, &p);
|
2019-11-24 22:32:40 -05:00
|
|
|
}
|
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
if (result != ESP_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2019-11-24 22:32:40 -05:00
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
uint32_t size = p->get_size();
|
|
|
|
|
|
|
|
result = init_custom(p, 0, size / SPI_FLASH_SEC_SIZE);
|
|
|
|
if (result != ESP_OK) {
|
|
|
|
delete p;
|
|
|
|
return result;
|
2019-11-24 22:32:40 -05:00
|
|
|
}
|
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
nvs_partition_list.push_back(p);
|
|
|
|
|
|
|
|
return ESP_OK;
|
2019-11-24 22:32:40 -05:00
|
|
|
}
|
|
|
|
#endif // ESP_PLATFORM
|
2020-04-26 20:51:31 -04:00
|
|
|
#endif // CONFIG_NVS_ENCRYPTION
|
2019-11-24 22:32:40 -05:00
|
|
|
|
|
|
|
esp_err_t NVSPartitionManager::deinit_partition(const char *partition_label)
|
|
|
|
{
|
|
|
|
Storage* storage = lookup_storage_from_name(partition_label);
|
|
|
|
if (!storage) {
|
|
|
|
return ESP_ERR_NVS_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up handles related to the storage being deinitialized */
|
|
|
|
for (auto it = nvs_handles.begin(); it != nvs_handles.end(); ++it) {
|
|
|
|
if (it->mStoragePtr == storage) {
|
|
|
|
it->valid = false;
|
|
|
|
nvs_handles.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
/* Finally delete the storage and its partition */
|
2019-11-24 22:32:40 -05:00
|
|
|
nvs_storage_list.erase(storage);
|
|
|
|
delete storage;
|
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
for (auto it = nvs_partition_list.begin(); it != nvs_partition_list.end(); ++it) {
|
|
|
|
if (strcmp(it->get_partition_name(), partition_label) == 0) {
|
|
|
|
NVSPartition *p = it;
|
|
|
|
nvs_partition_list.erase(it);
|
|
|
|
delete p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 22:32:40 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t NVSPartitionManager::open_handle(const char *part_name,
|
|
|
|
const char *ns_name,
|
|
|
|
nvs_open_mode_t open_mode,
|
|
|
|
NVSHandleSimple** handle)
|
|
|
|
{
|
|
|
|
uint8_t nsIndex;
|
|
|
|
Storage* sHandle;
|
|
|
|
|
2021-11-24 21:48:11 -05:00
|
|
|
if (handle == nullptr) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2020-04-26 20:51:31 -04:00
|
|
|
if (nvs_storage_list.empty()) {
|
2019-11-24 22:32:40 -05:00
|
|
|
return ESP_ERR_NVS_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
sHandle = lookup_storage_from_name(part_name);
|
2020-04-26 20:51:31 -04:00
|
|
|
if (sHandle == nullptr) {
|
2019-11-24 22:32:40 -05:00
|
|
|
return ESP_ERR_NVS_PART_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t err = sHandle->createOrOpenNamespace(ns_name, open_mode == NVS_READWRITE, nsIndex);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-11-24 21:48:11 -05:00
|
|
|
NVSHandleSimple* new_handle = new (std::nothrow) NVSHandleSimple(open_mode==NVS_READONLY, nsIndex, sHandle);
|
|
|
|
if (new_handle == nullptr) {
|
2020-04-26 20:51:31 -04:00
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
2019-12-06 01:36:28 -05:00
|
|
|
|
2021-11-24 21:48:11 -05:00
|
|
|
nvs_handles.push_back(new_handle);
|
2019-11-24 22:32:40 -05:00
|
|
|
|
2021-11-24 21:48:11 -05:00
|
|
|
*handle = new_handle;
|
2019-11-24 22:32:40 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t NVSPartitionManager::close_handle(NVSHandleSimple* handle) {
|
|
|
|
for (auto it = nvs_handles.begin(); it != nvs_handles.end(); ++it) {
|
|
|
|
if (it == intrusive_list<NVSHandleSimple>::iterator(handle)) {
|
|
|
|
nvs_handles.erase(it);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_ERR_NVS_INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t NVSPartitionManager::open_handles_size()
|
|
|
|
{
|
|
|
|
return nvs_handles.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage* NVSPartitionManager::lookup_storage_from_name(const char* name)
|
|
|
|
{
|
|
|
|
auto it = find_if(begin(nvs_storage_list), end(nvs_storage_list), [=](Storage& e) -> bool {
|
|
|
|
return (strcmp(e.getPartName(), name) == 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (it == end(nvs_storage_list)) {
|
2020-04-26 20:51:31 -04:00
|
|
|
return nullptr;
|
2019-11-24 22:32:40 -05:00
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // nvs
|