From 10780562e2b9ed3ddc68e5bf3b15ea40d3f1050a Mon Sep 17 00:00:00 2001 From: Shubham Patil Date: Tue, 12 Apr 2022 10:13:57 +0530 Subject: [PATCH] [ESP32] Make partition name configurable for chip namespaces (#17233) --- config/esp32/components/chip/Kconfig | 21 +++++++++ src/platform/ESP32/CHIPDevicePlatformConfig.h | 3 ++ .../ESP32/ConfigurationManagerImpl.cpp | 9 ++++ src/platform/ESP32/ESP32Config.cpp | 46 +++++++++++++------ src/platform/ESP32/ESP32Config.h | 3 ++ src/platform/ESP32/ScopedNvsHandle.h | 4 +- 6 files changed, 70 insertions(+), 16 deletions(-) diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index a65d705ac2a211..3229d5d6eaa485 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -780,4 +780,25 @@ menu "CHIP Device Layer" The gn target of the external platform. endmenu + menu "Matter Manufacturing Options" + config CHIP_FACTORY_NAMESPACE_PARTITION_LABEL + string "chip-factory namespace partition label" + default "nvs" + help + Label of the partition to store key-values in the "chip-factory" namespace. + + config CHIP_CONFIG_NAMESPACE_PARTITION_LABEL + string "chip-config namespace partition label" + default "nvs" + help + Label of the partition to store key-values in the "chip-config" namespace. + + config CHIP_COUNTERS_NAMESPACE_PARTITION_LABEL + string "chip-counters namespace partition label" + default "nvs" + help + Label of the partition to store key-values in the "chip-counters" namespace. + + endmenu + endmenu diff --git a/src/platform/ESP32/CHIPDevicePlatformConfig.h b/src/platform/ESP32/CHIPDevicePlatformConfig.h index 9292c658db44e3..9770775158fccc 100644 --- a/src/platform/ESP32/CHIPDevicePlatformConfig.h +++ b/src/platform/ESP32/CHIPDevicePlatformConfig.h @@ -95,3 +95,6 @@ #define CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE #define CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE #define CHIP_DEVICE_CONFIG_LOG_PROVISIONING_HASH CONFIG_LOG_PROVISIONING_HASH +#define CHIP_DEVICE_CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL +#define CHIP_DEVICE_CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION_LABEL +#define CHIP_DEVICE_CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION_LABEL diff --git a/src/platform/ESP32/ConfigurationManagerImpl.cpp b/src/platform/ESP32/ConfigurationManagerImpl.cpp index 1bc21e1f4b826d..d7810afeedafc3 100644 --- a/src/platform/ESP32/ConfigurationManagerImpl.cpp +++ b/src/platform/ESP32/ConfigurationManagerImpl.cpp @@ -61,6 +61,15 @@ CHIP_ERROR ConfigurationManagerImpl::Init() CHIP_ERROR err; uint32_t rebootCount; + // Initialize the nvs partitions, + // nvs_flash_init_partition() will initialize the partition only if it is not already initialized. + esp_err_t esp_err = nvs_flash_init_partition(CHIP_DEVICE_CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION); + SuccessOrExit(MapConfigError(esp_err)); + esp_err = nvs_flash_init_partition(CHIP_DEVICE_CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION); + SuccessOrExit(MapConfigError(esp_err)); + esp_err = nvs_flash_init_partition(CHIP_DEVICE_CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION); + SuccessOrExit(MapConfigError(esp_err)); + // Force initialization of NVS namespaces if they doesn't already exist. err = ESP32Config::EnsureNamespace(ESP32Config::kConfigNamespace_ChipFactory); SuccessOrExit(err); diff --git a/src/platform/ESP32/ESP32Config.cpp b/src/platform/ESP32/ESP32Config.cpp index bed53cd74e9eae..996d91b111d5c5 100644 --- a/src/platform/ESP32/ESP32Config.cpp +++ b/src/platform/ESP32/ESP32Config.cpp @@ -85,12 +85,30 @@ const ESP32Config::Key ESP32Config::kCounterKey_TotalOperationalHours = { kConfi // Prefix used for NVS keys that contain Chip group encryption keys. const char ESP32Config::kGroupKeyNamePrefix[] = "gk-"; +const char * ESP32Config::GetPartitionLabelByNamespace(const char * ns) +{ + if (strcmp(ns, kConfigNamespace_ChipFactory) == 0) + { + return CHIP_DEVICE_CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION; + } + else if (strcmp(ns, kConfigNamespace_ChipConfig) == 0) + { + return CHIP_DEVICE_CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION; + } + else if (strcmp(ns, kConfigNamespace_ChipCounters)) + { + return CHIP_DEVICE_CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION; + } + + return NVS_DEFAULT_PART_NAME; +} + CHIP_ERROR ESP32Config::ReadConfigValue(Key key, bool & val) { ScopedNvsHandle handle; uint32_t intVal; - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY, GetPartitionLabelByNamespace(key.Namespace))); esp_err_t err = nvs_get_u32(handle, key.Name, &intVal); if (err == ESP_ERR_NVS_NOT_FOUND) @@ -108,7 +126,7 @@ CHIP_ERROR ESP32Config::ReadConfigValue(Key key, uint32_t & val) { ScopedNvsHandle handle; - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY, GetPartitionLabelByNamespace(key.Namespace))); esp_err_t err = nvs_get_u32(handle, key.Name, &val); if (err == ESP_ERR_NVS_NOT_FOUND) @@ -124,7 +142,7 @@ CHIP_ERROR ESP32Config::ReadConfigValue(Key key, uint64_t & val) { ScopedNvsHandle handle; - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY, GetPartitionLabelByNamespace(key.Namespace))); // Special case the MfrDeviceId value, optionally allowing it to be read as a blob containing // a 64-bit big-endian integer, instead of a u64 value. @@ -161,7 +179,7 @@ CHIP_ERROR ESP32Config::ReadConfigValueStr(Key key, char * buf, size_t bufSize, { ScopedNvsHandle handle; - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY, GetPartitionLabelByNamespace(key.Namespace))); outLen = bufSize; esp_err_t err = nvs_get_str(handle, key.Name, buf, &outLen); @@ -185,7 +203,7 @@ CHIP_ERROR ESP32Config::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSiz { ScopedNvsHandle handle; - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY, GetPartitionLabelByNamespace(key.Namespace))); outLen = bufSize; esp_err_t err = nvs_get_blob(handle, key.Name, buf, &outLen); @@ -207,7 +225,7 @@ CHIP_ERROR ESP32Config::WriteConfigValue(Key key, bool val) { ScopedNvsHandle handle; - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace))); ReturnMappedErrorOnFailure(nvs_set_u32(handle, key.Name, val ? 1 : 0)); // Commit the value to the persistent store. @@ -221,7 +239,7 @@ CHIP_ERROR ESP32Config::WriteConfigValue(Key key, uint32_t val) { ScopedNvsHandle handle; - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace))); ReturnMappedErrorOnFailure(nvs_set_u32(handle, key.Name, val)); // Commit the value to the persistent store. @@ -235,7 +253,7 @@ CHIP_ERROR ESP32Config::WriteConfigValue(Key key, uint64_t val) { ScopedNvsHandle handle; - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace))); ReturnMappedErrorOnFailure(nvs_set_u64(handle, key.Name, val)); // Commit the value to the persistent store. @@ -251,7 +269,7 @@ CHIP_ERROR ESP32Config::WriteConfigValueStr(Key key, const char * str) { ScopedNvsHandle handle; - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace))); ReturnMappedErrorOnFailure(nvs_set_str(handle, key.Name, str)); // Commit the value to the persistent store. @@ -283,7 +301,7 @@ CHIP_ERROR ESP32Config::WriteConfigValueBin(Key key, const uint8_t * data, size_ if (data != NULL) { - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace))); ReturnMappedErrorOnFailure(nvs_set_blob(handle, key.Name, data, dataLen)); // Commit the value to the persistent store. @@ -300,7 +318,7 @@ CHIP_ERROR ESP32Config::ClearConfigValue(Key key) { ScopedNvsHandle handle; - ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE)); + ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace))); esp_err_t err = nvs_erase_key(handle, key.Name); if (err == ESP_ERR_NVS_NOT_FOUND) @@ -337,14 +355,14 @@ CHIP_ERROR ESP32Config::EnsureNamespace(const char * ns) { ScopedNvsHandle handle; - CHIP_ERROR err = handle.Open(ns, NVS_READONLY); + CHIP_ERROR err = handle.Open(ns, NVS_READONLY, GetPartitionLabelByNamespace(ns)); if (err == CHIP_NO_ERROR) { return CHIP_NO_ERROR; } if (err == ESP32Utils::MapError(ESP_ERR_NVS_NOT_FOUND)) { - ReturnErrorOnFailure(handle.Open(ns, NVS_READWRITE)); + ReturnErrorOnFailure(handle.Open(ns, NVS_READWRITE, GetPartitionLabelByNamespace(ns))); ReturnMappedErrorOnFailure(nvs_commit(handle)); return CHIP_NO_ERROR; } @@ -355,7 +373,7 @@ CHIP_ERROR ESP32Config::ClearNamespace(const char * ns) { ScopedNvsHandle handle; - ReturnErrorOnFailure(handle.Open(ns, NVS_READWRITE)); + ReturnErrorOnFailure(handle.Open(ns, NVS_READWRITE, GetPartitionLabelByNamespace(ns))); ReturnMappedErrorOnFailure(nvs_erase_all(handle)); ReturnMappedErrorOnFailure(nvs_commit(handle)); return CHIP_NO_ERROR; diff --git a/src/platform/ESP32/ESP32Config.h b/src/platform/ESP32/ESP32Config.h index 0f303f43f8a152..bfbfe65a0869bb 100644 --- a/src/platform/ESP32/ESP32Config.h +++ b/src/platform/ESP32/ESP32Config.h @@ -106,6 +106,9 @@ class ESP32Config static CHIP_ERROR ClearNamespace(const char * ns); static void RunConfigUnitTest(void); + +private: + static const char * GetPartitionLabelByNamespace(const char * ns); }; struct ESP32Config::Key diff --git a/src/platform/ESP32/ScopedNvsHandle.h b/src/platform/ESP32/ScopedNvsHandle.h index c12c1e56fe581f..7023dea4756da6 100644 --- a/src/platform/ESP32/ScopedNvsHandle.h +++ b/src/platform/ESP32/ScopedNvsHandle.h @@ -34,9 +34,9 @@ class ScopedNvsHandle public: ScopedNvsHandle() : mIsOpen(false) {} ~ScopedNvsHandle() { Close(); } - CHIP_ERROR Open(const char * name, nvs_open_mode_t open_mode) + CHIP_ERROR Open(const char * name, nvs_open_mode_t open_mode, const char * partition_label = NVS_DEFAULT_PART_NAME) { - esp_err_t err = nvs_open(name, open_mode, &mHandle); + esp_err_t err = nvs_open_from_partition(partition_label, name, open_mode, &mHandle); if (err == ESP_OK) { mIsOpen = true;