Merge branch 'feature/nvs_erase_check_init' into 'master'

NVS Flash: prevent erasing initialized partition

See merge request espressif/esp-idf!7615
This commit is contained in:
Ivan Grokhotkov 2020-02-21 17:47:45 +08:00
commit 5ccfc332ea
4 changed files with 34 additions and 10 deletions

View file

@ -87,26 +87,28 @@ esp_err_t nvs_flash_deinit_partition(const char* partition_label);
/** /**
* @brief Erase the default NVS partition * @brief Erase the default NVS partition
* *
* This function erases all contents of the default NVS partition (one with label "nvs") * Erases all contents of the default NVS partition (one with label "nvs"), which must be uninitialized.
* *
* @return * @return
* - ESP_OK on success * - ESP_OK on success
* - ESP_ERR_NOT_FOUND if there is no NVS partition labeled "nvs" in the * - ESP_ERR_NOT_FOUND if there is no NVS partition labeled "nvs" in the
* partition table * partition table
* - ESP_ERR_NVS_INVALID_STATE if the default partition is initialized already
*/ */
esp_err_t nvs_flash_erase(void); esp_err_t nvs_flash_erase(void);
/** /**
* @brief Erase specified NVS partition * @brief Erase specified NVS partition
* *
* This function erases all contents of specified NVS partition * Erase all content of a specified uninitialized NVS partition
* *
* @param[in] part_name Name (label) of the partition to be erased * @param[in] part_name Name (label) of an uninitialized partition which should be erased
* *
* @return * @return
* - ESP_OK on success * - ESP_OK on success
* - ESP_ERR_NOT_FOUND if there is no NVS partition with the specified name * - ESP_ERR_NOT_FOUND if there is no NVS partition with the specified name
* in the partition table * in the partition table
* - ESP_ERR_NVS_INVALID_STATE if the partition with part_name is initialized already
*/ */
esp_err_t nvs_flash_erase_partition(const char *part_name); esp_err_t nvs_flash_erase_partition(const char *part_name);

View file

@ -95,7 +95,7 @@ extern "C" esp_err_t nvs_flash_init_custom(const char *partName, uint32_t baseSe
{ {
ESP_LOGD(TAG, "nvs_flash_init_custom partition=%s start=%d count=%d", partName, baseSector, sectorCount); ESP_LOGD(TAG, "nvs_flash_init_custom partition=%s start=%d count=%d", partName, baseSector, sectorCount);
return nvs::NVSPartitionManager::get_instance()->init_custom(partName, baseSector, sectorCount); return NVSPartitionManager::get_instance()->init_custom(partName, baseSector, sectorCount);
} }
#ifdef CONFIG_NVS_ENCRYPTION #ifdef CONFIG_NVS_ENCRYPTION
@ -125,7 +125,7 @@ extern "C" esp_err_t nvs_flash_init_partition(const char *part_name)
Lock::init(); Lock::init();
Lock lock; Lock lock;
return nvs::NVSPartitionManager::get_instance()->init_partition(part_name); return NVSPartitionManager::get_instance()->init_partition(part_name);
} }
extern "C" esp_err_t nvs_flash_init(void) extern "C" esp_err_t nvs_flash_init(void)
@ -163,6 +163,10 @@ extern "C" esp_err_t nvs_flash_secure_init(nvs_sec_cfg_t* cfg)
extern "C" esp_err_t nvs_flash_erase_partition(const char *part_name) extern "C" esp_err_t nvs_flash_erase_partition(const char *part_name)
{ {
if (NVSPartitionManager::get_instance()->lookup_storage_from_name(part_name)) {
return ESP_ERR_NVS_INVALID_STATE;
}
const esp_partition_t* partition = esp_partition_find_first( const esp_partition_t* partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, part_name); ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, part_name);
if (partition == NULL) { if (partition == NULL) {
@ -187,7 +191,7 @@ extern "C" esp_err_t nvs_flash_deinit_partition(const char* partition_name)
s_nvs_handles.clearAndFreeNodes(); s_nvs_handles.clearAndFreeNodes();
// Deinit partition // Deinit partition
return nvs::NVSPartitionManager::get_instance()->deinit_partition(partition_name); return NVSPartitionManager::get_instance()->deinit_partition(partition_name);
} }
extern "C" esp_err_t nvs_flash_deinit(void) extern "C" esp_err_t nvs_flash_deinit(void)
@ -213,7 +217,7 @@ extern "C" esp_err_t nvs_open_from_partition(const char *part_name, const char*
ESP_LOGD(TAG, "%s %s %d", __func__, name, open_mode); ESP_LOGD(TAG, "%s %s %d", __func__, name, open_mode);
NVSHandleSimple *handle; NVSHandleSimple *handle;
esp_err_t result = nvs::NVSPartitionManager::get_instance()->open_handle(part_name, name, open_mode, &handle); esp_err_t result = NVSPartitionManager::get_instance()->open_handle(part_name, name, open_mode, &handle);
if (result == ESP_OK) { if (result == ESP_OK) {
NVSHandleEntry *entry = new (std::nothrow) NVSHandleEntry(handle, part_name); NVSHandleEntry *entry = new (std::nothrow) NVSHandleEntry(handle, part_name);
if (entry) { if (entry) {

View file

@ -18,6 +18,24 @@
static const char* TAG = "test_nvs"; static const char* TAG = "test_nvs";
TEST_CASE("flash erase fails on initialized partition", "[nvs]")
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_LOGW(TAG, "nvs_flash_init failed (0x%x), erasing partition and retrying", err);
const esp_partition_t* nvs_partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
assert(nvs_partition && "partition table must have an NVS partition");
ESP_ERROR_CHECK( esp_partition_erase_range(nvs_partition, 0, nvs_partition->size) );
err = nvs_flash_init();
}
ESP_ERROR_CHECK( err );
TEST_ASSERT_EQUAL(ESP_ERR_NVS_INVALID_STATE, nvs_flash_erase());
nvs_flash_deinit();
}
// test could have different output on host tests // test could have different output on host tests
TEST_CASE("nvs deinit with open handle", "[nvs]") TEST_CASE("nvs deinit with open handle", "[nvs]")
{ {
@ -121,8 +139,8 @@ TEST_CASE("calculate used and free space", "[nvs]")
// erase if have any namespace // erase if have any namespace
TEST_ESP_OK(nvs_get_stats(NULL, &stat1)); TEST_ESP_OK(nvs_get_stats(NULL, &stat1));
if(stat1.namespace_count != 0) { if(stat1.namespace_count != 0) {
TEST_ESP_OK(nvs_flash_erase());
TEST_ESP_OK(nvs_flash_deinit()); TEST_ESP_OK(nvs_flash_deinit());
TEST_ESP_OK(nvs_flash_erase());
TEST_ESP_OK(nvs_flash_init()); TEST_ESP_OK(nvs_flash_init());
} }
@ -235,8 +253,8 @@ TEST_CASE("calculate used and free space", "[nvs]")
nvs_close(handle_3); nvs_close(handle_3);
TEST_ESP_OK(nvs_flash_erase());
TEST_ESP_OK(nvs_flash_deinit()); TEST_ESP_OK(nvs_flash_deinit());
TEST_ESP_OK(nvs_flash_erase());
} }
TEST_CASE("check for memory leaks in nvs_set_blob", "[nvs]") TEST_CASE("check for memory leaks in nvs_set_blob", "[nvs]")

View file

@ -336,7 +336,7 @@ example_test_012:
UT_001: UT_001:
extends: .unit_test_template extends: .unit_test_template
parallel: 33 parallel: 34
tags: tags:
- ESP32_IDF - ESP32_IDF
- UT_T1_1 - UT_T1_1