Skip to content

Commit c76fcda

Browse files
tx2rxasriot
andauthored
[ASR] add more basic info to factory data (project-chip#30123)
* [ASR] add more basic info to factory data * modify ASRFactoryDataProvider.c to C++ * rebase to master; update asr582x sdk --------- Co-authored-by: weicheng <weichengxu@asrmicro.com>
1 parent cd2cd09 commit c76fcda

16 files changed

+465
-61
lines changed

src/platform/ASR/ASRConfig.cpp

+12-23
Original file line numberDiff line numberDiff line change
@@ -221,46 +221,35 @@ bool ASRConfig::ConfigValueExists(Key key)
221221
CHIP_ERROR ASRConfig::ReadFactoryConfigValue(asr_matter_partition_t matter_partition, uint8_t * buf, size_t bufSize,
222222
size_t & outLen)
223223
{
224-
int32_t ret = 0;
224+
factory_error_t ret = asr_factory_config_read(matter_partition, buf, (uint32_t) bufSize, (uint32_t *) &outLen);
225225

226-
ret = asr_factory_config_read(matter_partition, buf, (uint32_t) bufSize, (uint32_t *) &outLen);
227-
228-
if (ret != 0)
229-
ChipLogProgress(DeviceLayer, "asr_factory_config_read: %d failed, ret = %d\n", matter_partition, static_cast<int>(ret));
230-
231-
if (ret == 0)
232-
{
233-
return CHIP_NO_ERROR;
234-
}
235-
else
226+
if (ret != FACTORY_NO_ERROR)
236227
{
228+
ChipLogProgress(DeviceLayer, "asr_factory_config_read: %d failed, err = %d\n", matter_partition, ret);
237229
outLen = 0;
238230
return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
239231
}
232+
return CHIP_NO_ERROR;
240233
}
241234

242235
CHIP_ERROR ASRConfig::ReadFactoryConfigValue(asr_matter_partition_t matter_partition, uint32_t & val)
243236
{
244-
int32_t ret = 0;
245237
uint8_t buf[4];
246-
size_t outlen = 0;
247-
248-
ret = asr_factory_config_read(matter_partition, buf, sizeof(uint32_t), (uint32_t *) &outlen);
238+
size_t outlen = 0;
239+
factory_error_t ret = asr_factory_config_read(matter_partition, buf, sizeof(uint32_t), (uint32_t *) &outlen);
249240

250241
if (outlen > sizeof(uint32_t))
251-
ret = -1;
242+
return CHIP_ERROR_BUFFER_TOO_SMALL;
252243

253-
if (ret != 0)
254-
ChipLogProgress(DeviceLayer, "asr_factory_config_read: %d failed, ret = %d\n", matter_partition, static_cast<int>(ret));
255-
256-
if (ret == 0)
244+
if (ret != FACTORY_NO_ERROR)
257245
{
258-
val = *((uint32_t *) buf);
259-
return CHIP_NO_ERROR;
246+
ChipLogProgress(DeviceLayer, "asr_factory_config_read: %d failed, err = %d\n", matter_partition, ret);
247+
return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
260248
}
261249
else
262250
{
263-
return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
251+
val = *((uint32_t *) buf);
252+
return CHIP_NO_ERROR;
264253
}
265254
}
266255
#endif

src/platform/ASR/ASRConfig.h

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#pragma once
2727

28-
#include "asr_factory_config.h"
28+
#include "ASRFactoryDataParser.h"
2929
#include <string.h>
3030

3131
namespace chip {
+277
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
/*
2+
*
3+
* Copyright (c) 2023 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "ASRFactoryDataParser.h"
19+
20+
#if CONFIG_ENABLE_ASR_FACTORY_DATA_PROVIDER
21+
/* Logic partition on flash devices for matter */
22+
const matter_partition_t asr_matter_partitions_table[] =
23+
{
24+
[ASR_VERSION_PARTITION] =
25+
{
26+
.partition_name = "version",
27+
},
28+
[ASR_CONFIG_PARTITION] =
29+
{
30+
.partition_name = "config",
31+
},
32+
[ASR_ITERATION_COUNT_PARTITION] =
33+
{
34+
.partition_name = "iteration-count",
35+
},
36+
[ASR_SALT_PARTITION] =
37+
{
38+
.partition_name = "salt",
39+
},
40+
[ASR_VERIFIER_PARTITION] =
41+
{
42+
.partition_name = "verifier",
43+
},
44+
[ASR_DISCRIMINATOR_PARTITION] =
45+
{
46+
.partition_name = "discriminator",
47+
},
48+
[ASR_DAC_CERT_PARTITION] =
49+
{
50+
.partition_name = "dac-cert",
51+
},
52+
[ASR_DAC_KEY_PARTITION] =
53+
{
54+
.partition_name = "dac-pri-key",
55+
},
56+
[ASR_DAC_PUB_KEY_PARTITION] =
57+
{
58+
.partition_name = "dac-pub-key",
59+
},
60+
[ASR_PAI_CERT_PARTITION] =
61+
{
62+
.partition_name = "pai-cert",
63+
},
64+
[ASR_CERT_DCLRN_PARTITION] =
65+
{
66+
.partition_name = "cert-dclrn",
67+
},
68+
[ASR_CHIP_ID_PARTITION] =
69+
{
70+
.partition_name = "chip-id",
71+
},
72+
#if CONFIG_ENABLE_ASR_FACTORY_DEVICE_INFO_PROVIDER
73+
[ASR_VENDOR_NAME_PARTITION] =
74+
{
75+
.partition_name = "vendor-name",
76+
},
77+
[ASR_VENDOR_ID_PARTITION] =
78+
{
79+
.partition_name = "vendor-id",
80+
},
81+
[ASR_PRODUCT_NAME_PARTITION] =
82+
{
83+
.partition_name = "product-name",
84+
},
85+
[ASR_PRODUCT_ID_PARTITION] =
86+
{
87+
.partition_name = "product-id",
88+
},
89+
[ASR_ROTATING_UNIQUE_ID_PARTITION] =
90+
{
91+
.partition_name = "rd-id-uid",
92+
},
93+
[ASR_MANUFACTURY_DATE_PARTITION] =
94+
{
95+
.partition_name = "mfg-date",
96+
},
97+
[ASR_SERIAL_NUMBER_PARTITION] =
98+
{
99+
.partition_name = "serial-num",
100+
},
101+
[ASR_HARDWARE_VERSION_PARTITION] =
102+
{
103+
.partition_name = "hardware-ver",
104+
},
105+
[ASR_HARDWARE_VERSION_STR_PARTITION] =
106+
{
107+
.partition_name = "hw-ver-str",
108+
},
109+
[ASR_PRODUCT_URL_PARTITION] =
110+
{
111+
.partition_name = "product-url",
112+
},
113+
[ASR_PRODUCT_LABEL_PARTITION] =
114+
{
115+
.partition_name = "product-label",
116+
},
117+
[ASR_PART_NUMBER_PARTITION] =
118+
{
119+
.partition_name = "part-number",
120+
},
121+
#endif
122+
[ASR_MATTER_PARTITION_MAX] =
123+
{
124+
.partition_name = NULL, //for end don't change,
125+
}
126+
};
127+
128+
static asr_tlv_context matter_tlv_ctx;
129+
130+
static factory_error_t asr_matter_find_by_name(const char * name, uint8_t * buf, uint32_t buf_len, uint32_t * out_len)
131+
{
132+
tlv_header_t tlv;
133+
134+
if (asr_tlv_find_by_name(&matter_tlv_ctx, &tlv, MIXCLASS_MATTERCONFIG_TAG,
135+
(char *) name)) // parse the data according to the tlv packaging tool
136+
{
137+
uint32_t value_len = tlv_htons(tlv->data_len);
138+
139+
value_len -= MAX_NAME_LEN;
140+
141+
if (value_len > buf_len)
142+
{
143+
return FACTORY_BUFFER_TOO_SMALL;
144+
}
145+
146+
*out_len = value_len;
147+
148+
memcpy(buf, tlv->data + MAX_NAME_LEN, value_len);
149+
150+
return FACTORY_NO_ERROR;
151+
}
152+
else
153+
{
154+
return FACTORY_VALUE_NOT_FOUND;
155+
}
156+
}
157+
158+
static uint32_t asr_find_factory_version()
159+
{
160+
uint8_t buf[4];
161+
size_t outlen = 0;
162+
uint32_t value = 0;
163+
if (FACTORY_NO_ERROR ==
164+
asr_matter_find_by_name((const char *) asr_matter_partitions_table[ASR_VERSION_PARTITION].partition_name, buf,
165+
sizeof(uint32_t), (uint32_t *) &outlen))
166+
{
167+
value = *(uint32_t *) buf;
168+
}
169+
return value;
170+
}
171+
172+
static uint32_t asr_find_factory_config()
173+
{
174+
uint8_t buf[4];
175+
size_t outlen = 0;
176+
uint32_t value = 0;
177+
if (FACTORY_NO_ERROR ==
178+
asr_matter_find_by_name((const char *) asr_matter_partitions_table[ASR_CONFIG_PARTITION].partition_name, buf,
179+
sizeof(uint32_t), (uint32_t *) &outlen))
180+
{
181+
value = *(uint32_t *) buf;
182+
}
183+
return value;
184+
}
185+
186+
static factory_error_t asr_factory_dac_prvkey_get(uint8_t * pRdBuf, uint32_t * pOutLen)
187+
{
188+
static constexpr uint32_t kDACPrivateKeySize = 32;
189+
if (NULL == pRdBuf || NULL == pOutLen)
190+
{
191+
return FACTORY_INVALID_INPUT;
192+
}
193+
194+
if (asr_find_factory_config() & ASR_CONFIG_MATTER_NO_KEY)
195+
{
196+
#if defined(CFG_PLF_RV32) || defined(CFG_PLF_DUET)
197+
uint32_t off_set;
198+
uint8_t ucCipher[kDACPrivateKeySize];
199+
tlv_area_header_t tlv_headr_p = (tlv_area_header_t) MATTER_FLASH_START_ADDR;
200+
off_set = tlv_headr_p->data_len + 3 * sizeof(uint32_t); // magic_num,crc32_value,data_len
201+
// get DAC cipher
202+
if (asr_flash_read(ASR_CONFIG_BASE, (uint32_t *) &off_set, (void *) ucCipher, kDACPrivateKeySize) == 0)
203+
{
204+
if (asr_factory_decrypt_dac_prvkey(ucCipher, kDACPrivateKeySize, pRdBuf, pOutLen) == 0)
205+
{
206+
return FACTORY_NO_ERROR;
207+
}
208+
}
209+
#endif
210+
return FACTORY_NOT_SUPPORTED;
211+
}
212+
else
213+
{
214+
return asr_matter_find_by_name((const char *) asr_matter_partitions_table[ASR_DAC_KEY_PARTITION].partition_name, pRdBuf,
215+
kDACPrivateKeySize, pOutLen);
216+
}
217+
}
218+
219+
static int asr_partition_members_count_cb(tlv_header_t tlv, void * arg1, void * arg2)
220+
{
221+
int * count = (int *) arg1;
222+
223+
*count = *count + 1;
224+
225+
return 0;
226+
}
227+
228+
static factory_error_t asr_partition_table_load(void)
229+
{
230+
int table_members = 0;
231+
232+
if (asr_tlv_poll_class_members(&matter_tlv_ctx, MIXCLASS_MATTERCONFIG_TAG, asr_partition_members_count_cb, &table_members,
233+
NULL))
234+
{
235+
if (table_members > 0)
236+
{
237+
if (asr_find_factory_version() == ASR_MATTER_FACTORY_VERSION)
238+
{
239+
return FACTORY_NO_ERROR;
240+
}
241+
else
242+
{
243+
return FACTORY_VERSION_MISMATCH;
244+
}
245+
}
246+
}
247+
return FACTORY_DATA_CHECK_FAILED;
248+
}
249+
250+
factory_error_t asr_factory_config_read(asr_matter_partition_t matter_partition, uint8_t * buf, uint32_t buf_len,
251+
uint32_t * out_len)
252+
{
253+
254+
if (matter_partition >= ASR_MATTER_PARTITION_MAX)
255+
{
256+
return FACTORY_INVALID_INPUT;
257+
}
258+
259+
if (matter_partition == ASR_DAC_KEY_PARTITION)
260+
{
261+
return asr_factory_dac_prvkey_get(buf, out_len);
262+
}
263+
264+
return asr_matter_find_by_name((const char *) asr_matter_partitions_table[matter_partition].partition_name, buf, buf_len,
265+
out_len);
266+
}
267+
268+
factory_error_t asr_factory_check()
269+
{
270+
if (asr_tlv_init(&matter_tlv_ctx, MATTER_FLASH_START_ADDR) != 0)
271+
{
272+
return FACTORY_DATA_INIT_FAILED;
273+
}
274+
275+
return asr_partition_table_load();
276+
}
277+
#endif // CONFIG_ENABLE_ASR_FACTORY_DATA_PROVIDER

0 commit comments

Comments
 (0)