From 0f99d8cf597fe1f83e8da45e19b1942a3f34eab4 Mon Sep 17 00:00:00 2001 From: Sid Hsu Date: Mon, 8 Aug 2022 15:05:40 +0800 Subject: [PATCH] [Infineon][SVE] Fix CYW30739 KVS read/write methods. * Write methods should copy data to RAM before calling the platform API which doesn't support writing data from the flash. * Read methods should return CHIP_ERROR_BUFFER_TOO_SMALL if the specified entry exists but the given buffer size is zero. --- src/platform/CYW30739/CYW30739Config.cpp | 29 +++++++++++++++---- src/platform/CYW30739/CYW30739Config.h | 3 ++ .../CYW30739/KeyValueStoreManagerImpl.cpp | 1 + 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/platform/CYW30739/CYW30739Config.cpp b/src/platform/CYW30739/CYW30739Config.cpp index 043350c157c066..2d84fd0703c798 100644 --- a/src/platform/CYW30739/CYW30739Config.cpp +++ b/src/platform/CYW30739/CYW30739Config.cpp @@ -23,6 +23,7 @@ /* this file behaves like a config.h, comes first */ #include +#include #include #include @@ -78,12 +79,7 @@ template CHIP_ERROR CYW30739Config::ReadConfigValue(Key key, uint64_t & val); template CHIP_ERROR CYW30739Config::WriteConfigValue(Key key, T val) { - wiced_result_t result; - uint16_t write_count = wiced_hal_write_nvram(PLATFORM_NVRAM_VSID_MATTER_BASE + key, sizeof(val), (uint8_t *) &val, &result); - if (result == WICED_SUCCESS && write_count == sizeof(val)) - return CHIP_NO_ERROR; - else - return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND; + return WriteConfigValueBin(key, &val, sizeof(val)); } template CHIP_ERROR CYW30739Config::WriteConfigValue(Key key, bool val); @@ -111,6 +107,22 @@ CHIP_ERROR CYW30739Config::WriteConfigValueBin(Key key, const void * data, size_ if (dataLen == 0) return CHIP_NO_ERROR; + if (IsDataFromFlash(data)) + { + /* + * Copy data to RAM before calling the platform API + * which doesn't support writing data from the flash. + */ + using namespace chip::Platform; + std::unique_ptr buffer(MemoryAlloc(dataLen), &MemoryFree); + if (!buffer) + { + return CHIP_ERROR_NO_MEMORY; + } + memcpy(buffer.get(), data, dataLen); + return WriteConfigValueBin(key, buffer.get(), dataLen); + } + wiced_result_t result; const uint16_t write_count = wiced_hal_write_nvram(PLATFORM_NVRAM_VSID_MATTER_BASE + key, dataLen, (uint8_t *) data, &result); if (result == WICED_SUCCESS && write_count == dataLen) @@ -147,6 +159,11 @@ CHIP_ERROR CYW30739Config::FactoryResetConfig(void) void CYW30739Config::RunConfigUnitTest(void) {} +bool CYW30739Config::IsDataFromFlash(const void * data) +{ + return reinterpret_cast(FLASH_BASE_ADDRESS) <= data && data < reinterpret_cast(FLASH_BASE_ADDRESS + FLASH_SIZE); +} + } // namespace Internal } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/CYW30739/CYW30739Config.h b/src/platform/CYW30739/CYW30739Config.h index 21766b19d11a1c..a2f318a1ed6017 100644 --- a/src/platform/CYW30739/CYW30739Config.h +++ b/src/platform/CYW30739/CYW30739Config.h @@ -110,6 +110,9 @@ class CYW30739Config static bool ConfigValueExists(Key key); static CHIP_ERROR FactoryResetConfig(void); static void RunConfigUnitTest(void); + +private: + static bool IsDataFromFlash(const void * data); }; } // namespace Internal diff --git a/src/platform/CYW30739/KeyValueStoreManagerImpl.cpp b/src/platform/CYW30739/KeyValueStoreManagerImpl.cpp index 5969d7a417746e..00e7a063614bb9 100644 --- a/src/platform/CYW30739/KeyValueStoreManagerImpl.cpp +++ b/src/platform/CYW30739/KeyValueStoreManagerImpl.cpp @@ -82,6 +82,7 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t entry = FindEntry(key); VerifyOrExit(entry != nullptr, err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND); + VerifyOrExit(value_size != 0, err = CHIP_ERROR_BUFFER_TOO_SMALL); size_t byte_count; err = CYW30739Config::ReadConfigValueBin(entry->GetValueConfigKey(), value, value_size, byte_count);