From 1d62999ba2b6cc9f10bfe275a2b4e23da65c4f31 Mon Sep 17 00:00:00 2001 From: Alex Tsitsiura Date: Thu, 24 Nov 2022 21:01:03 +0200 Subject: [PATCH] [Telink] Allow to erase entire NVS on factory reset by default; Fix IDM-7.1 test. (Cherry-pick #23434 & #23676) (#23677) * [Zephyr] Allow to erase entire NVS on factory reset (#23434) * [Zephyr] Allow to erase entire NVS on factory reset Currently, the default factory reset implementation simply removes Matter (and OpenThread) settings instead of formatting the non-volatile storage partition. The latter approach may be advisable in the cases where no settings need to survive a factory reset, as it is expected to work properly regardless of the state of the storage partition. Additionally, it will regain the original storage performance if it has been loaded with a lot of stale data. Signed-off-by: Damian Krolik * [Telink] Allow to erase entire NVS on factory reset by default; Fix IDM-7.1 test. Signed-off-by: Damian Krolik --- config/telink/chip-module/Kconfig | 5 ++++ config/zephyr/Kconfig | 11 ++++++++ .../Zephyr/ConfigurationManagerImpl.cpp | 27 ++++++++++++++++--- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/config/telink/chip-module/Kconfig b/config/telink/chip-module/Kconfig index 366c43d8eac6db..f216280df7e583 100644 --- a/config/telink/chip-module/Kconfig +++ b/config/telink/chip-module/Kconfig @@ -266,3 +266,8 @@ config CHIP_CERTIFiCATION_DECLARATION_OTA_IMAGE_ID for sending it via OTA Software Update purposes. endif + +# See config/zephyr/Kconfig for full definition +config CHIP_FACTORY_RESET_ERASE_NVS + bool + default y diff --git a/config/zephyr/Kconfig b/config/zephyr/Kconfig index fd779c62e319f3..57770b686a6b50 100644 --- a/config/zephyr/Kconfig +++ b/config/zephyr/Kconfig @@ -196,6 +196,17 @@ config CHIP_OPERATIONAL_TIME_SAVE_INTERVAL precisely operation time in case of device reboot and maximizing flash memory lifetime. +config CHIP_FACTORY_RESET_ERASE_NVS + bool "Erase NVS flash pages on factory reset" + depends on SETTINGS_NVS + help + When factory reset is requested, erase flash pages occupied by non-volatile storage + instead of removing Matter-related settings only. This provides a more robust + factory reset mechanism and allows to regain the original storage performance if any + firmware issue has brought it to an unexpected state. For this reason, it is + advisable to set this option if all configuration, including device-specific + entries, is supposed to be cleared on a factory reset. + config CHIP_MALLOC_SYS_HEAP bool "Memory allocator based on Zephyr sys_heap" imply SYS_HEAP_RUNTIME_STATS diff --git a/src/platform/Zephyr/ConfigurationManagerImpl.cpp b/src/platform/Zephyr/ConfigurationManagerImpl.cpp index bd19057e65d3bf..107579afbfbb03 100644 --- a/src/platform/Zephyr/ConfigurationManagerImpl.cpp +++ b/src/platform/Zephyr/ConfigurationManagerImpl.cpp @@ -32,6 +32,11 @@ #include #include +#ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS +#include +#include +#endif + namespace chip { namespace DeviceLayer { @@ -168,14 +173,30 @@ void ConfigurationManagerImpl::RunConfigUnitTest(void) void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg) { ChipLogProgress(DeviceLayer, "Performing factory reset"); + +#ifdef CONFIG_CHIP_FACTORY_RESET_ERASE_NVS + void * storage = nullptr; + int status = settings_storage_get(&storage); + + if (status == 0) + { + status = nvs_clear(static_cast(storage)); + } + + if (status) + { + ChipLogError(DeviceLayer, "Factory reset failed: %d", status); + } +#else const CHIP_ERROR err = PersistedStorage::KeyValueStoreMgrImpl().DoFactoryReset(); if (err != CHIP_NO_ERROR) + { ChipLogError(DeviceLayer, "Factory reset failed: %" CHIP_ERROR_FORMAT, err.Format()); + } -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - ThreadStackMgr().ErasePersistentInfo(); -#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD + ConnectivityMgr().ErasePersistentInfo(); +#endif PlatformMgr().Shutdown(); }