25
25
#include < crypto/CHIPCryptoPALPSA.h>
26
26
#endif
27
27
28
+ #ifdef CONFIG_FPROTECT
28
29
#include < fprotect.h>
30
+ #endif // if CONFIG_FPROTECT
31
+
32
+ #if defined(USE_PARTITION_MANAGER) && USE_PARTITION_MANAGER == 1
29
33
#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
+
30
42
#include < system/SystemError.h>
31
43
#include < zephyr/drivers/flash.h>
32
44
@@ -39,8 +51,8 @@ struct InternalFlashFactoryData
39
51
{
40
52
CHIP_ERROR GetFactoryDataPartition (uint8_t *& data, size_t & dataSize)
41
53
{
42
- data = reinterpret_cast <uint8_t *>(PM_FACTORY_DATA_ADDRESS );
43
- dataSize = PM_FACTORY_DATA_SIZE ;
54
+ data = reinterpret_cast <uint8_t *>(FACTORY_DATA_ADDRESS );
55
+ dataSize = FACTORY_DATA_SIZE ;
44
56
return CHIP_NO_ERROR;
45
57
}
46
58
@@ -54,16 +66,16 @@ struct InternalFlashFactoryData
54
66
// the application code at runtime anyway.
55
67
constexpr size_t FactoryDataBlockBegin ()
56
68
{
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);
69
+ // calculate the nearest multiple of CONFIG_FPROTECT_BLOCK_SIZE smaller than FACTORY_DATA_ADDRESS
70
+ return FACTORY_DATA_ADDRESS & (-CONFIG_FPROTECT_BLOCK_SIZE);
59
71
}
60
72
61
73
constexpr size_t FactoryDataBlockSize ()
62
74
{
63
75
// calculate the factory data end address rounded up to the nearest multiple of CONFIG_FPROTECT_BLOCK_SIZE
64
76
// and make sure we do not overlap with settings partition
65
77
constexpr size_t kFactoryDataBlockEnd =
66
- (PM_FACTORY_DATA_ADDRESS + PM_FACTORY_DATA_SIZE + CONFIG_FPROTECT_BLOCK_SIZE - 1 ) & (-CONFIG_FPROTECT_BLOCK_SIZE);
78
+ (FACTORY_DATA_ADDRESS + FACTORY_DATA_SIZE + CONFIG_FPROTECT_BLOCK_SIZE - 1 ) & (-CONFIG_FPROTECT_BLOCK_SIZE);
67
79
static_assert (kFactoryDataBlockEnd <= PM_SETTINGS_STORAGE_ADDRESS,
68
80
" FPROTECT memory block, which contains factory data"
69
81
" partition overlaps with the settings partition."
@@ -75,44 +87,94 @@ struct InternalFlashFactoryData
75
87
#undef TO_STR_IMPL
76
88
CHIP_ERROR ProtectFactoryDataPartitionAgainstWrite ()
77
89
{
90
+ #ifdef CONFIG_FPROTECT
78
91
int ret = fprotect_area (FactoryDataBlockBegin (), FactoryDataBlockSize ());
79
92
return System::MapErrorZephyr (ret);
93
+ #else
94
+ return CHIP_ERROR_NOT_IMPLEMENTED;
95
+ #endif // if CONFIG_FPROTECT
80
96
}
81
97
#else
82
98
CHIP_ERROR ProtectFactoryDataPartitionAgainstWrite () { return CHIP_ERROR_NOT_IMPLEMENTED; }
83
99
#endif
84
100
};
85
101
102
+ #if defined(USE_PARTITION_MANAGER) && USE_PARTITION_MANAGER == 1 && (defined(CONFIG_CHIP_QSPI_NOR) || defined(CONFIG_CHIP_SPI_NOR))
86
103
struct ExternalFlashFactoryData
87
104
{
88
105
CHIP_ERROR GetFactoryDataPartition (uint8_t *& data, size_t & dataSize)
89
106
{
90
- int ret = flash_read (mFlashDevice , PM_FACTORY_DATA_ADDRESS , mFactoryDataBuffer , PM_FACTORY_DATA_SIZE );
107
+ int ret = flash_read (mFlashDevice , FACTORY_DATA_ADDRESS , mFactoryDataBuffer , FACTORY_DATA_SIZE );
91
108
92
109
if (ret != 0 )
93
110
{
94
111
return CHIP_ERROR_READ_FAILED;
95
112
}
96
113
97
114
data = mFactoryDataBuffer ;
98
- dataSize = PM_FACTORY_DATA_SIZE ;
115
+ dataSize = FACTORY_DATA_SIZE ;
99
116
100
117
return CHIP_NO_ERROR;
101
118
}
102
119
103
120
CHIP_ERROR ProtectFactoryDataPartitionAgainstWrite () { return CHIP_ERROR_NOT_IMPLEMENTED; }
104
121
105
- const struct device * mFlashDevice = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
106
- uint8_t mFactoryDataBuffer [PM_FACTORY_DATA_SIZE];
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;
107
171
};
108
172
109
173
template <class FlashFactoryData >
110
- class FactoryDataProvider : public chip ::Credentials::DeviceAttestationCredentialsProvider,
111
- public CommissionableDataProvider,
112
- public DeviceInstanceInfoProvider
174
+ class FactoryDataProvider : public FactoryDataProviderBase
113
175
{
114
176
public:
115
- CHIP_ERROR Init ();
177
+ CHIP_ERROR Init () override ;
116
178
117
179
// ===== Members functions that implement the DeviceAttestationCredentialsProvider
118
180
CHIP_ERROR GetCertificationDeclaration (MutableByteSpan & outBuffer) override ;
@@ -147,33 +209,13 @@ class FactoryDataProvider : public chip::Credentials::DeviceAttestationCredentia
147
209
CHIP_ERROR GetProductPrimaryColor (app::Clusters::BasicInformation::ColorEnum * primaryColor) override ;
148
210
149
211
// ===== Members functions that are platform-specific
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);
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 ;
173
215
174
216
private:
175
- static constexpr uint16_t kFactoryDataPartitionSize = PM_FACTORY_DATA_SIZE ;
176
- static constexpr uint32_t kFactoryDataPartitionAddress = PM_FACTORY_DATA_ADDRESS ;
217
+ static constexpr uint16_t kFactoryDataPartitionSize = FACTORY_DATA_SIZE ;
218
+ static constexpr uint32_t kFactoryDataPartitionAddress = FACTORY_DATA_ADDRESS ;
177
219
static constexpr uint8_t kDACPrivateKeyLength = 32 ;
178
220
static constexpr uint8_t kDACPublicKeyLength = 65 ;
179
221
0 commit comments