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