From cb6dcb6fc4b64aec68173e5040ebb3423cb43dd4 Mon Sep 17 00:00:00 2001 From: Dmitriy Shilin Date: Thu, 23 Apr 2020 12:19:06 +0300 Subject: [PATCH 1/2] nvs: allow to specify custom partition Closes https://github.com/espressif/esp-idf/pull/5172 --- components/nvs_flash/include/nvs_flash.h | 34 ++++++++++++++++++++++ components/nvs_flash/src/nvs_api.cpp | 37 ++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/components/nvs_flash/include/nvs_flash.h b/components/nvs_flash/include/nvs_flash.h index eafca2ce18..f2d2e00656 100644 --- a/components/nvs_flash/include/nvs_flash.h +++ b/components/nvs_flash/include/nvs_flash.h @@ -62,6 +62,20 @@ esp_err_t nvs_flash_init(void); */ esp_err_t nvs_flash_init_partition(const char *partition_label); +/** + * @brief Initialize NVS flash storage for the partition specified by partition pointer. + * + * @param[in] partition pointer to a partition obtained by the ESP partition API. + * + * @return + * - ESP_OK if storage was successfully initialized + * - ESP_ERR_NVS_NO_FREE_PAGES if the NVS storage contains no empty pages + * (which may happen if NVS partition was truncated) + * - ESP_ERR_INVALID_ARG in case partition is NULL + * - one of the error codes from the underlying flash storage driver + */ +esp_err_t nvs_flash_init_partition_ptr(const esp_partition_t *partition); + /** * @brief Deinitialize NVS storage for the default NVS partition * @@ -119,6 +133,26 @@ esp_err_t nvs_flash_erase(void); */ esp_err_t nvs_flash_erase_partition(const char *part_name); +/** + * @brief Erase custom partition. + * + * Erase all content of specified custom partition. + * + * @note + * If the partition is initialized, this function first de-initializes it. + * Afterwards, the partition has to be initialized again to be used. + * + * @param[in] partition pointer to a partition obtained by the ESP partition API. + * + * @return + * - ESP_OK on success + * - ESP_ERR_NOT_FOUND if there is no partition with the specified + * parameters in the partition table + * - ESP_ERR_INVALID_ARG in case partition is NULL + * - one of the error codes from the underlying flash storage driver + */ +esp_err_t nvs_flash_erase_partition_ptr(const esp_partition_t *partition); + /** * @brief Initialize the default NVS partition. * diff --git a/components/nvs_flash/src/nvs_api.cpp b/components/nvs_flash/src/nvs_api.cpp index 0357494168..37defc42cc 100644 --- a/components/nvs_flash/src/nvs_api.cpp +++ b/components/nvs_flash/src/nvs_api.cpp @@ -143,9 +143,9 @@ extern "C" esp_err_t nvs_flash_secure_init_custom(const char *partName, uint32_t } #endif -static esp_err_t close_handles_and_deinit(const char* part_name) +static esp_err_t close_handles_and_deinit(const char* partition_name) { - nvs::Storage* storage = lookup_storage_from_name(part_name); + nvs::Storage* storage = lookup_storage_from_name(partition_name); if (!storage) { return ESP_ERR_NVS_NOT_INITIALIZED; } @@ -164,7 +164,7 @@ static esp_err_t close_handles_and_deinit(const char* part_name) next++; if (it->mStoragePtr == storage) { ESP_LOGD(TAG, "Deleting handle %d (ns=%d) related to partition \"%s\" (missing call to nvs_close?)", - it->mHandle, it->mNsIndex, part_name); + it->mHandle, it->mNsIndex, partition_name); s_nvs_handles.erase(it); delete static_cast(it); } @@ -178,6 +178,20 @@ static esp_err_t close_handles_and_deinit(const char* part_name) return ESP_OK; } +extern "C" esp_err_t nvs_flash_init_partition_ptr(const esp_partition_t *partition) +{ + Lock::init(); + Lock lock; + + if (!partition) { + return ESP_ERR_INVALID_ARG; + } + + return nvs_flash_init_custom(partition->label, + partition->address / SPI_FLASH_SEC_SIZE, + partition->size / SPI_FLASH_SEC_SIZE); +} + #ifdef ESP_PLATFORM extern "C" esp_err_t nvs_flash_init_partition(const char *part_name) { @@ -257,6 +271,23 @@ extern "C" esp_err_t nvs_flash_erase_partition(const char *part_name) return esp_partition_erase_range(partition, 0, partition->size); } +extern "C" esp_err_t nvs_flash_erase_partition_ptr(const esp_partition_t *partition) +{ + Lock::init(); + Lock lock; + + if (!partition) { + return ESP_ERR_INVALID_ARG; + } + + // if the partition is initialized, uninitialize it first + if (lookup_storage_from_name(partition->label)) { + close_handles_and_deinit(partition->label); + } + + return esp_partition_erase_range(partition, 0, partition->size); +} + extern "C" esp_err_t nvs_flash_erase() { return nvs_flash_erase_partition(NVS_DEFAULT_PART_NAME); From f694f92230e74c9c8209788c4a741f49575f7cc5 Mon Sep 17 00:00:00 2001 From: Jakob Hasse Date: Tue, 26 May 2020 09:28:39 +0800 Subject: [PATCH 2/2] nvs: unit tests for new partition API * also changed while to for loop --- components/nvs_flash/src/nvs_api.cpp | 6 +- components/nvs_flash/test_nvs_host/Makefile | 1 + .../test_nvs_host/test_nvs_initialization.cpp | 68 +++++++++++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 components/nvs_flash/test_nvs_host/test_nvs_initialization.cpp diff --git a/components/nvs_flash/src/nvs_api.cpp b/components/nvs_flash/src/nvs_api.cpp index 37defc42cc..7722af0ddb 100644 --- a/components/nvs_flash/src/nvs_api.cpp +++ b/components/nvs_flash/src/nvs_api.cpp @@ -158,17 +158,13 @@ static esp_err_t close_handles_and_deinit(const char* partition_name) #endif /* Clean up handles related to the storage being deinitialized */ - auto it = s_nvs_handles.begin(); - auto next = it; - while(it != s_nvs_handles.end()) { - next++; + for (auto it = s_nvs_handles.begin(), next = it; it != s_nvs_handles.end() && next++; it = next) { if (it->mStoragePtr == storage) { ESP_LOGD(TAG, "Deleting handle %d (ns=%d) related to partition \"%s\" (missing call to nvs_close?)", it->mHandle, it->mNsIndex, partition_name); s_nvs_handles.erase(it); delete static_cast(it); } - it = next; } /* Finally delete the storage itself */ diff --git a/components/nvs_flash/test_nvs_host/Makefile b/components/nvs_flash/test_nvs_host/Makefile index 045a69d735..530c97e553 100644 --- a/components/nvs_flash/test_nvs_host/Makefile +++ b/components/nvs_flash/test_nvs_host/Makefile @@ -19,6 +19,7 @@ SOURCE_FILES = \ test_intrusive_list.cpp \ test_nvs.cpp \ test_nvs_storage.cpp \ + test_nvs_initialization.cpp \ crc.cpp \ main.cpp diff --git a/components/nvs_flash/test_nvs_host/test_nvs_initialization.cpp b/components/nvs_flash/test_nvs_host/test_nvs_initialization.cpp new file mode 100644 index 0000000000..c213564ea8 --- /dev/null +++ b/components/nvs_flash/test_nvs_host/test_nvs_initialization.cpp @@ -0,0 +1,68 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#include "catch.hpp" +#include "spi_flash_emulation.h" +#include "esp_partition.h" +#include "nvs.h" +#include "nvs_flash.h" +#include +#include + +TEST_CASE("nvs_flash_init_partition_ptr fails due to nullptr arg", "[nvs_custom_part]") +{ + const uint32_t NVS_FLASH_SECTOR = 6; + const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3; + SpiFlashEmulator emu(10); + + CHECK(nvs_flash_init_partition_ptr(nullptr) == ESP_ERR_INVALID_ARG); +} + +TEST_CASE("nvs_flash_init_partition_ptr inits one partition", "[nvs_custom_part]") +{ + const uint32_t NVS_FLASH_SECTOR = 6; + const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3; + SpiFlashEmulator emu(10); + nvs_handle_t out_handle; + esp_partition_t partition = {}; + strcpy(partition.label, "test"); + partition.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE; + partition.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE; + nvs_flash_deinit_partition("test"); + + CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_ERR_NVS_PART_NOT_FOUND); + + CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_OK); + CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_OK); + CHECK(nvs_flash_deinit_partition("test") == ESP_OK); +} + +TEST_CASE("nvs_flash_init_partition_ptr deinits one partition", "[nvs_custom_part]") +{ + const uint32_t NVS_FLASH_SECTOR = 6; + const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3; + SpiFlashEmulator emu(10); + nvs_handle_t out_handle; + esp_partition_t partition = {}; + strcpy(partition.label, "test"); + partition.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE; + partition.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE; + nvs_flash_deinit_partition("test"); + + CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_OK); + CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_OK); + + CHECK(nvs_flash_deinit_partition("test") == ESP_OK); + CHECK(nvs_open_from_partition("test", "test_ns", NVS_READWRITE, &out_handle) == ESP_ERR_NVS_PART_NOT_FOUND); +} +