Skip to content

Commit 5035d3f

Browse files
committed
Revert "[nrfconnect] Introduced several improvements for the factory data management (project-chip#33195)"
This reverts commit 07e3098. Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent 6555ef7 commit 5035d3f

File tree

4 files changed

+47
-108
lines changed

4 files changed

+47
-108
lines changed

config/nrfconnect/chip-module/Kconfig

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ config CHIP_MALLOC_SYS_HEAP
106106
config CHIP_FACTORY_DATA
107107
bool "Factory data provider"
108108
select ZCBOR
109+
imply FPROTECT
109110
help
110111
Enables the default nRF Connect factory data provider implementation that
111112
supports reading the factory data encoded in the CBOR format from the
@@ -148,7 +149,7 @@ config CHIP_FACTORY_DATA_WRITE_PROTECT
148149
bool "Enable Factory Data write protection"
149150
select FPROTECT
150151
depends on CHIP_FACTORY_DATA
151-
default y
152+
default y if CHIP_FACTORY_DATA
152153
help
153154
Enables the write protection of the Factory Data partition in the flash memory.
154155
This is a recommended feature, but it requires the Settings partition size to be

config/nrfconnect/chip-module/generate_factory_data.cmake

+7-24
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,9 @@ set(factory_data_output_path ${output_path}/${factory_data_target})
129129
string(APPEND script_args "-o \"${factory_data_output_path}\"\n")
130130
string(APPEND script_args "-s \"${schema_path}\"\n")
131131

132-
# Add optional offset and size arguments to generate .hex file as well as .json.
133-
if(CONFIG_PARTITION_MANAGER_ENABLED)
134-
string(APPEND script_args "--offset $<TARGET_PROPERTY:partition_manager,PM_FACTORY_DATA_ADDRESS>\n")
135-
string(APPEND script_args "--size $<TARGET_PROPERTY:partition_manager,PM_FACTORY_DATA_OFFSET>\n")
136-
else()
137-
dt_alias(factory_data_alias PROPERTY "factory-data")
138-
dt_node_exists(factory_data_exists PATH "${factory_data_alias}")
139-
if(NOT ${factory_data_exists})
140-
message(FATAL_ERROR "factory-data alias does not exist in DTS")
141-
endif()
142-
dt_reg_addr(factory_data_addr PATH ${factory_data_alias})
143-
dt_reg_size(factory_data_size PATH ${factory_data_alias})
144-
string(APPEND script_args "--offset ${factory_data_addr}\n")
145-
string(APPEND script_args "--size ${factory_data_size}\n")
146-
endif()
132+
# Add optional offset and size arguments to generate both .hex and .json files.
133+
string(APPEND script_args "--offset $<TARGET_PROPERTY:partition_manager,PM_FACTORY_DATA_ADDRESS>\n")
134+
string(APPEND script_args "--size $<TARGET_PROPERTY:partition_manager,PM_FACTORY_DATA_OFFSET>\n")
147135

148136
# execute first script to create a JSON file
149137
separate_arguments(separated_script_args NATIVE_COMMAND ${script_args})
@@ -187,15 +175,10 @@ nrfconnect_create_factory_data(factory_data
187175
${FACTORY_DATA_SCHEMA_PATH}
188176
${OUTPUT_FILE_PATH})
189177

190-
if(CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE)
191-
if(CONFIG_PARTITION_MANAGER_ENABLED)
192-
# set custom target for merging factory_data hex file
193-
set_property(GLOBAL PROPERTY factory_data_PM_HEX_FILE ${OUTPUT_FILE_PATH}/factory_data.hex)
194-
set_property(GLOBAL PROPERTY factory_data_PM_TARGET factory_data)
195-
else()
196-
set_property(GLOBAL APPEND PROPERTY HEX_FILES_TO_MERGE ${OUTPUT_FILE_PATH}/factory_data.hex ${OUTPUT_FILE_PATH}/zephyr.hex)
197-
set_property(TARGET runners_yaml_props_target PROPERTY hex_file ${OUTPUT_FILE_PATH}/merged.hex)
198-
endif()
178+
if(CONFIG_CHIP_FACTORY_DATA_MERGE_WITH_FIRMWARE)
179+
# set custom target for merging factory_data hex file
180+
set_property(GLOBAL PROPERTY factory_data_PM_HEX_FILE ${OUTPUT_FILE_PATH}/factory_data.hex)
181+
set_property(GLOBAL PROPERTY factory_data_PM_TARGET factory_data)
199182
endif()
200183

201184

src/platform/nrfconnect/FactoryDataProvider.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,7 @@ CHIP_ERROR FactoryDataProvider<FlashFactoryData>::GetUserKey(const char * userKe
432432

433433
// Fully instantiate the template class in whatever compilation unit includes this file.
434434
template class FactoryDataProvider<InternalFlashFactoryData>;
435-
#if defined(USE_PARTITION_MANAGER) && USE_PARTITION_MANAGER == 1 && (defined(CONFIG_CHIP_QSPI_NOR) || defined(CONFIG_CHIP_SPI_NOR))
436435
template class FactoryDataProvider<ExternalFlashFactoryData>;
437-
#endif // if defined(USE_PARTITION_MANAGER) && USE_PARTITION_MANAGER == 1 (defined(CONFIG_CHIP_QSPI_NOR) ||
438-
// defined(CONFIG_CHIP_SPI_NOR))
439436

440437
} // namespace DeviceLayer
441438
} // namespace chip

src/platform/nrfconnect/FactoryDataProvider.h

+38-80
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,8 @@
2525
#include <crypto/CHIPCryptoPALPSA.h>
2626
#endif
2727

28-
#ifdef CONFIG_FPROTECT
2928
#include <fprotect.h>
30-
#endif // if CONFIG_FPROTECT
31-
32-
#if defined(USE_PARTITION_MANAGER) && USE_PARTITION_MANAGER == 1
3329
#include <pm_config.h>
34-
#define FACTORY_DATA_ADDRESS PM_FACTORY_DATA_ADDRESS
35-
#define FACTORY_DATA_SIZE PM_FACTORY_DATA_SIZE
36-
#else
37-
#include <zephyr/storage/flash_map.h>
38-
#define FACTORY_DATA_SIZE DT_REG_SIZE(DT_ALIAS(factory_data))
39-
#define FACTORY_DATA_ADDRESS DT_REG_ADDR(DT_ALIAS(factory_data))
40-
#endif // if defined(USE_PARTITION_MANAGER) && USE_PARTITION_MANAGER == 1
41-
4230
#include <system/SystemError.h>
4331
#include <zephyr/drivers/flash.h>
4432

@@ -51,8 +39,8 @@ struct InternalFlashFactoryData
5139
{
5240
CHIP_ERROR GetFactoryDataPartition(uint8_t *& data, size_t & dataSize)
5341
{
54-
data = reinterpret_cast<uint8_t *>(FACTORY_DATA_ADDRESS);
55-
dataSize = FACTORY_DATA_SIZE;
42+
data = reinterpret_cast<uint8_t *>(PM_FACTORY_DATA_ADDRESS);
43+
dataSize = PM_FACTORY_DATA_SIZE;
5644
return CHIP_NO_ERROR;
5745
}
5846

@@ -66,16 +54,16 @@ struct InternalFlashFactoryData
6654
// the application code at runtime anyway.
6755
constexpr size_t FactoryDataBlockBegin()
6856
{
69-
// calculate the nearest multiple of CONFIG_FPROTECT_BLOCK_SIZE smaller than FACTORY_DATA_ADDRESS
70-
return FACTORY_DATA_ADDRESS & (-CONFIG_FPROTECT_BLOCK_SIZE);
57+
// calculate the nearest multiple of CONFIG_FPROTECT_BLOCK_SIZE smaller than PM_FACTORY_DATA_ADDRESS
58+
return PM_FACTORY_DATA_ADDRESS & (-CONFIG_FPROTECT_BLOCK_SIZE);
7159
}
7260

7361
constexpr size_t FactoryDataBlockSize()
7462
{
7563
// calculate the factory data end address rounded up to the nearest multiple of CONFIG_FPROTECT_BLOCK_SIZE
7664
// and make sure we do not overlap with settings partition
7765
constexpr size_t kFactoryDataBlockEnd =
78-
(FACTORY_DATA_ADDRESS + FACTORY_DATA_SIZE + CONFIG_FPROTECT_BLOCK_SIZE - 1) & (-CONFIG_FPROTECT_BLOCK_SIZE);
66+
(PM_FACTORY_DATA_ADDRESS + PM_FACTORY_DATA_SIZE + CONFIG_FPROTECT_BLOCK_SIZE - 1) & (-CONFIG_FPROTECT_BLOCK_SIZE);
7967
static_assert(kFactoryDataBlockEnd <= PM_SETTINGS_STORAGE_ADDRESS,
8068
"FPROTECT memory block, which contains factory data"
8169
"partition overlaps with the settings partition."
@@ -87,94 +75,44 @@ struct InternalFlashFactoryData
8775
#undef TO_STR_IMPL
8876
CHIP_ERROR ProtectFactoryDataPartitionAgainstWrite()
8977
{
90-
#ifdef CONFIG_FPROTECT
9178
int ret = fprotect_area(FactoryDataBlockBegin(), FactoryDataBlockSize());
9279
return System::MapErrorZephyr(ret);
93-
#else
94-
return CHIP_ERROR_NOT_IMPLEMENTED;
95-
#endif // if CONFIG_FPROTECT
9680
}
9781
#else
9882
CHIP_ERROR ProtectFactoryDataPartitionAgainstWrite() { return CHIP_ERROR_NOT_IMPLEMENTED; }
9983
#endif
10084
};
10185

102-
#if defined(USE_PARTITION_MANAGER) && USE_PARTITION_MANAGER == 1 && (defined(CONFIG_CHIP_QSPI_NOR) || defined(CONFIG_CHIP_SPI_NOR))
10386
struct ExternalFlashFactoryData
10487
{
10588
CHIP_ERROR GetFactoryDataPartition(uint8_t *& data, size_t & dataSize)
10689
{
107-
int ret = flash_read(mFlashDevice, FACTORY_DATA_ADDRESS, mFactoryDataBuffer, FACTORY_DATA_SIZE);
90+
int ret = flash_read(mFlashDevice, PM_FACTORY_DATA_ADDRESS, mFactoryDataBuffer, PM_FACTORY_DATA_SIZE);
10891

10992
if (ret != 0)
11093
{
11194
return CHIP_ERROR_READ_FAILED;
11295
}
11396

11497
data = mFactoryDataBuffer;
115-
dataSize = FACTORY_DATA_SIZE;
98+
dataSize = PM_FACTORY_DATA_SIZE;
11699

117100
return CHIP_NO_ERROR;
118101
}
119102

120103
CHIP_ERROR ProtectFactoryDataPartitionAgainstWrite() { return CHIP_ERROR_NOT_IMPLEMENTED; }
121104

122-
const struct device * mFlashDevice = DEVICE_DT_GET(DT_CHOSEN(nordic_pm_ext_flash));
123-
uint8_t mFactoryDataBuffer[FACTORY_DATA_SIZE];
124-
};
125-
#endif // if defined(USE_PARTITION_MANAGER) && USE_PARTITION_MANAGER == 1 && (defined(CONFIG_CHIP_QSPI_NOR) ||
126-
// defined(CONFIG_CHIP_SPI_NOR))
127-
128-
class FactoryDataProviderBase : public chip::Credentials::DeviceAttestationCredentialsProvider,
129-
public CommissionableDataProvider,
130-
public DeviceInstanceInfoProvider
131-
{
132-
public:
133-
/**
134-
* @brief Perform all operations needed to initialize factory data provider.
135-
*
136-
* @returns CHIP_NO_ERROR in case of a success, specific error code otherwise
137-
*/
138-
virtual CHIP_ERROR Init() = 0;
139-
140-
/**
141-
* @brief Get the EnableKey as MutableByteSpan
142-
*
143-
* @param enableKey MutableByteSpan object to obtain EnableKey
144-
* @returns
145-
* CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if factory data does not contain enable_key field, or the value cannot be read
146-
* out. CHIP_ERROR_BUFFER_TOO_SMALL if provided MutableByteSpan is too small
147-
*/
148-
virtual CHIP_ERROR GetEnableKey(MutableByteSpan & enableKey) = 0;
149-
150-
/**
151-
* @brief Get the user data in CBOR format as MutableByteSpan
152-
*
153-
* @param userData MutableByteSpan object to obtain all user data in CBOR format
154-
* @returns
155-
* CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if factory data does not contain user field, or the value cannot be read out.
156-
* CHIP_ERROR_BUFFER_TOO_SMALL if provided MutableByteSpan is too small
157-
*/
158-
virtual CHIP_ERROR GetUserData(MutableByteSpan & userData) = 0;
159-
160-
/**
161-
* @brief Try to find user data key and return its value
162-
*
163-
* @param userKey A key name to be found
164-
* @param buf Buffer to store value of found key
165-
* @param len Length of the buffer. This value will be updated to the actual value if the key is read.
166-
* @returns
167-
* CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if factory data does not contain user key field, or the value cannot be read
168-
* out. CHIP_ERROR_BUFFER_TOO_SMALL if provided buffer length is too small
169-
*/
170-
virtual CHIP_ERROR GetUserKey(const char * userKey, void * buf, size_t & len) = 0;
105+
const struct device * mFlashDevice = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
106+
uint8_t mFactoryDataBuffer[PM_FACTORY_DATA_SIZE];
171107
};
172108

173109
template <class FlashFactoryData>
174-
class FactoryDataProvider : public FactoryDataProviderBase
110+
class FactoryDataProvider : public chip::Credentials::DeviceAttestationCredentialsProvider,
111+
public CommissionableDataProvider,
112+
public DeviceInstanceInfoProvider
175113
{
176114
public:
177-
CHIP_ERROR Init() override;
115+
CHIP_ERROR Init();
178116

179117
// ===== Members functions that implement the DeviceAttestationCredentialsProvider
180118
CHIP_ERROR GetCertificationDeclaration(MutableByteSpan & outBuffer) override;
@@ -209,13 +147,33 @@ class FactoryDataProvider : public FactoryDataProviderBase
209147
CHIP_ERROR GetProductPrimaryColor(app::Clusters::BasicInformation::ColorEnum * primaryColor) override;
210148

211149
// ===== Members functions that are platform-specific
212-
CHIP_ERROR GetEnableKey(MutableByteSpan & enableKey) override;
213-
CHIP_ERROR GetUserData(MutableByteSpan & userData) override;
214-
CHIP_ERROR GetUserKey(const char * userKey, void * buf, size_t & len) override;
150+
CHIP_ERROR GetEnableKey(MutableByteSpan & enableKey);
151+
152+
/**
153+
* @brief Get the user data in CBOR format as MutableByteSpan
154+
*
155+
* @param userData MutableByteSpan object to obtain all user data in CBOR format
156+
* @returns
157+
* CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if factory data does not contain user field, or the value cannot be read out.
158+
* CHIP_ERROR_BUFFER_TOO_SMALL if provided MutableByteSpan is too small
159+
*/
160+
CHIP_ERROR GetUserData(MutableByteSpan & userData);
161+
162+
/**
163+
* @brief Try to find user data key and return its value
164+
*
165+
* @param userKey A key name to be found
166+
* @param buf Buffer to store value of found key
167+
* @param len Length of the buffer. This value will be updated to the actual value if the key is read.
168+
* @returns
169+
* CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if factory data does not contain user field, or the value cannot be read out.
170+
* CHIP_ERROR_BUFFER_TOO_SMALL if provided buffer length is too small
171+
*/
172+
CHIP_ERROR GetUserKey(const char * userKey, void * buf, size_t & len);
215173

216174
private:
217-
static constexpr uint16_t kFactoryDataPartitionSize = FACTORY_DATA_SIZE;
218-
static constexpr uint32_t kFactoryDataPartitionAddress = FACTORY_DATA_ADDRESS;
175+
static constexpr uint16_t kFactoryDataPartitionSize = PM_FACTORY_DATA_SIZE;
176+
static constexpr uint32_t kFactoryDataPartitionAddress = PM_FACTORY_DATA_ADDRESS;
219177
static constexpr uint8_t kDACPrivateKeyLength = 32;
220178
static constexpr uint8_t kDACPublicKeyLength = 65;
221179

0 commit comments

Comments
 (0)