1 /*
2 * Copyright (C) 2022-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <errno.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <securec.h>
23 #include <limits.h>
24 #include "cJSON.h"
25 #include "syscap_tool.h"
26 #include "endian_internal.h"
27 #include "syscap_interface.h"
28 #include "context_tool.h"
29 #include "common_method.h"
30
31 #ifdef SYSCAP_DEFINE_EXTERN_ENABLE
32 #include "syscap_define_custom.h"
33 #else
34 #include "syscap_define.h"
35 #endif
36
37 #define OS_SYSCAP_BYTES 120
38 #define SYSCAP_PREFIX_LEN 17
39 #define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN)
40 #define RPCID_OUT_BUFFER 32
41 #define PCID_OUT_BUFFER RPCID_OUT_BUFFER
42 #define UINT8_BIT 8
43 #define INT_BIT 32
44 #define U32_TO_STR_MAX_LEN 11
45
46 #define FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID 1
47 #define FREE_MALLOC_OSSYSCAP_AFTER_DECODE_RPCID 2
48 #define FREE_WHOLE_SYSCAP_AFTER_DECODE_RPCID 3
49 #define FREE_RPCID_ROOT_AFTER_DECODE_RPCID 4
50 #define FREE_CONTEXT_OUT_AFTER_DECODE_RPCID 5
51
52 typedef struct ProductCompatibilityID {
53 uint16_t apiVersion : 15;
54 uint16_t apiVersionType : 1;
55 uint16_t systemType : 3;
56 uint16_t reserved : 13;
57 uint32_t manufacturerID;
58 uint8_t osSyscap[OS_SYSCAP_BYTES];
59 } PCIDMain;
60
61 static const char *PCID_PATH = "/system/etc/pcid.sc";
62
63 struct FreeAfterDecodeRpcidInfo {
64 char *priSyscap;
65 char *contextBuffer;
66 cJSON *sysCapDefine;
67 cJSON *rpcidRoot;
68 uint16_t *osSysCapIndex;
69 int32_t sysCapArraySize;
70 };
71
72 struct PcidPriSyscapInfo {
73 uint32_t rpcidPriSyscapLen;
74 uint32_t pcidPriSyscapLen;
75 char *rpcidPriSyscap;
76 char *pcidPriSyscap;
77 uint16_t ossyscapFlag;
78 int32_t ret;
79 };
80
EncodeOsSyscap(char * output,int len)81 bool EncodeOsSyscap(char *output, int len)
82 {
83 int32_t ret;
84 int32_t res;
85 char *contextBuffer = NULL;
86 uint32_t bufferLen;
87
88 if (len != PCID_MAIN_BYTES) {
89 PRINT_ERR("Os Syscap input len(%d) must be equal to 128.\n", len);
90 return false;
91 }
92
93 ret = GetFileContext(PCID_PATH, &contextBuffer, &bufferLen);
94 if (ret != 0) {
95 PRINT_ERR("GetFileContext failed, input file : /system/etc/pcid.sc\n");
96 return false;
97 }
98
99 res = memcpy_s(output, PCID_MAIN_BYTES, contextBuffer, PCID_MAIN_BYTES);
100 if (res != 0) {
101 PRINT_ERR("memcpy_s failed.");
102 FreeContextBuffer(contextBuffer);
103 return false;
104 }
105
106 FreeContextBuffer(contextBuffer);
107 return true;
108 }
109
EncodePrivateSyscap(char ** output,int * outputLen)110 bool EncodePrivateSyscap(char **output, int *outputLen)
111 {
112 int32_t ret;
113 char *contextBuffer = NULL;
114 char *outputStr = NULL;
115 uint32_t bufferLen;
116
117 ret = GetFileContext(PCID_PATH, &contextBuffer, &bufferLen);
118 if (ret != 0) {
119 PRINT_ERR("GetFileContext failed, input file : /system/etc/pcid.sc\n");
120 return false;
121 }
122
123 if (bufferLen < (PCID_MAIN_BYTES + 1) || bufferLen > INT32_MAX) {
124 PRINT_ERR("Parameter bufferLen out of range.");
125 FreeContextBuffer(contextBuffer);
126 return false;
127 }
128 uint32_t priLen = bufferLen - PCID_MAIN_BYTES - 1;
129 if ((int)priLen <= 0) {
130 *outputLen = 0;
131 FreeContextBuffer(contextBuffer);
132 return false;
133 }
134 outputStr = (char *)calloc(priLen, sizeof(char));
135 if (outputStr == NULL) {
136 PRINT_ERR("malloc buffer failed, size = %u, errno = %d\n", priLen, errno);
137 *outputLen = 0;
138 FreeContextBuffer(contextBuffer);
139 return false;
140 }
141
142 ret = strncpy_s(outputStr, priLen, contextBuffer + PCID_MAIN_BYTES, priLen - 1);
143 if (ret != 0) {
144 PRINT_ERR("strcpy_s failed.");
145 FreeContextBuffer(contextBuffer);
146 free(outputStr);
147 *outputLen = 0;
148 return false;
149 }
150
151 FreeContextBuffer(contextBuffer);
152 *outputLen = (int)strlen(outputStr);
153 *output = outputStr;
154 return true;
155 }
156
DecodeOsSyscap(const char input[PCID_MAIN_BYTES],char (** output)[SINGLE_SYSCAP_LEN],int * outputCnt)157 bool DecodeOsSyscap(const char input[PCID_MAIN_BYTES], char (**output)[SINGLE_SYSCAP_LEN], int *outputCnt)
158 {
159 errno_t nRet = 0;
160 uint16_t indexOfSyscap[CHAR_BIT * OS_SYSCAP_BYTES] = {0};
161 uint16_t countOfSyscap = 0;
162 uint16_t i, j;
163
164 uint8_t *osSyscap = (uint8_t *)(input + 8); // 8, int[2] of pcid header
165
166 for (i = 0; i < OS_SYSCAP_BYTES; i++) {
167 for (j = 0; j < CHAR_BIT; j++) {
168 if (osSyscap[i] & (0x01 << j)) {
169 indexOfSyscap[countOfSyscap++] = i * CHAR_BIT + j;
170 }
171 }
172 }
173
174 *outputCnt = countOfSyscap;
175 char (*strSyscap)[SINGLE_SYSCAP_LEN] = NULL;
176 strSyscap = (char (*)[SINGLE_SYSCAP_LEN])malloc(countOfSyscap * SINGLE_SYSCAP_LEN);
177 if (strSyscap == NULL) {
178 PRINT_ERR("malloc failed.");
179 *outputCnt = 0;
180 return false;
181 }
182 (void)memset_s(strSyscap, countOfSyscap * SINGLE_SYSCAP_LEN, 0, countOfSyscap * SINGLE_SYSCAP_LEN);
183 *output = strSyscap;
184
185 for (i = 0; i < countOfSyscap; i++) {
186 for (j = 0; j < sizeof(g_arraySyscap) / sizeof(SyscapWithNum); j++) {
187 if (g_arraySyscap[j].num != indexOfSyscap[i]) {
188 continue;
189 }
190 nRet = strcpy_s(*strSyscap, SINGLE_SYSCAP_LEN, g_arraySyscap[j].str);
191 if (nRet != EOK) {
192 printf("strcpy_s failed. error = %d\n", nRet);
193 *outputCnt = 0;
194 free(strSyscap);
195 strSyscap = NULL;
196 return false;
197 }
198 strSyscap++;
199 break;
200 }
201 }
202
203 return true;
204 }
205
GetPriSyscapCount(char * input)206 int32_t GetPriSyscapCount(char *input)
207 {
208 int32_t syscapCnt = 0;
209
210 char *inputPos = input;
211 while (*inputPos != '\0') {
212 if (*inputPos == ',') {
213 syscapCnt++;
214 }
215 inputPos++;
216 }
217
218 return syscapCnt;
219 }
220
DecodePrivateSyscap(char * input,char (** output)[SINGLE_SYSCAP_LEN],int * outputCnt)221 bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *outputCnt)
222 {
223 char *inputPos = input;
224 char (*outputArray)[SINGLE_SYSCAP_LEN] = NULL;
225
226 if (input == NULL) {
227 return false;
228 }
229
230 int syscapCnt = GetPriSyscapCount(inputPos);
231 *outputCnt = syscapCnt;
232 if (syscapCnt == 0) {
233 return true;
234 }
235
236 int bufferLen = SINGLE_SYSCAP_LEN * syscapCnt;
237 outputArray = (char (*)[SINGLE_SYSCAP_LEN])malloc(bufferLen);
238 if (outputArray == NULL) {
239 return false;
240 }
241 (void)memset_s(outputArray, bufferLen, 0, bufferLen);
242
243 *output = outputArray;
244 inputPos = input;
245 char buffer[SINGLE_FEAT_LEN] = {0};
246 char *bufferPos = buffer;
247 while (*inputPos != '\0') {
248 if (*inputPos == ',') {
249 *bufferPos = '\0';
250 if (sprintf_s(*outputArray, SINGLE_SYSCAP_LEN, "SystemCapability.%s", buffer) == -1) {
251 free(outputArray);
252 outputArray = NULL;
253 return false;
254 }
255 bufferPos = buffer;
256 outputArray++;
257 inputPos++;
258 continue;
259 }
260 *bufferPos++ = *inputPos++;
261 }
262
263 return true;
264 }
265
SetOsSysCapBitMap(uint8_t * out,uint16_t outLen,const uint16_t * index,uint16_t indexLen)266 static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, const uint16_t *index, uint16_t indexLen)
267 {
268 uint16_t sector, pos;
269
270 if (outLen != OS_SYSCAP_BYTES) {
271 PRINT_ERR("Input array error.\n");
272 return -1;
273 }
274
275 for (uint16_t i = 0; i < indexLen; i++) {
276 sector = index[i] / UINT8_BIT;
277 pos = index[i] % UINT8_BIT;
278 if (sector >= OS_SYSCAP_BYTES) {
279 PRINT_ERR("Syscap num(%u) out of range(120).\n", sector);
280 return -1;
281 }
282 out[sector] |= (1 << pos);
283 }
284 return 0;
285 }
286
ParseRpcidToJson(char * input,uint32_t inputLen,cJSON * rpcidJson)287 static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson)
288 {
289 uint32_t i;
290 int32_t ret = 0;
291 uint16_t sysCapLength = NtohsInter(*(uint16_t *)(input + sizeof(uint32_t)));
292 uint16_t sysCapCount = sysCapLength / SINGLE_FEAT_LEN;
293 char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t);
294 RPCIDHead *rpcidHeader = (RPCIDHead *)input;
295 cJSON *sysCapJson = cJSON_CreateArray();
296 if (sysCapJson == NULL) {
297 PRINT_ERR("Get sysCapJson failed, sysCapJson is empty.\n");
298 ret = -1;
299 }
300 for (i = 0; i < sysCapCount; i++) {
301 char *temp = sysCapBegin + i * SINGLE_FEAT_LEN;
302 if (strlen(temp) >= SINGLE_FEAT_LEN) {
303 PRINT_ERR("Get SysCap failed, string length too long.\n");
304 ret = -1;
305 goto FREE_SYSCAP_OUT;
306 }
307 char buffer[SINGLE_SYSCAP_LEN] = "SystemCapability.";
308
309 ret = strncat_s(buffer, sizeof(buffer), temp, SINGLE_FEAT_LEN);
310 if (ret != EOK) {
311 PRINT_ERR("strncat_s failed.\n");
312 goto FREE_SYSCAP_OUT;
313 }
314
315 if (!cJSON_AddItemToArray(sysCapJson, cJSON_CreateString(buffer))) {
316 PRINT_ERR("Add syscap string to json failed.\n");
317 ret = -1;
318 goto FREE_SYSCAP_OUT;
319 }
320 }
321
322 if (!cJSON_AddNumberToObject(rpcidJson, "api_version", NtohsInter(rpcidHeader->apiVersion))) {
323 PRINT_ERR("Add api_version to json failed.\n");
324 ret = -1;
325 goto FREE_SYSCAP_OUT;
326 }
327 if (!cJSON_AddItemToObject(rpcidJson, "syscap", sysCapJson)) {
328 PRINT_ERR("Add syscap to json failed.\n");
329 ret = -1;
330 goto FREE_SYSCAP_OUT;
331 }
332 FREE_SYSCAP_OUT:
333 cJSON_Delete(sysCapJson);
334 return ret;
335 }
336
TransStringFormatAndSaveSyscap(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo,cJSON * sysCapArray,const char * inputFile)337 static int32_t TransStringFormatAndSaveSyscap(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo,
338 cJSON *sysCapArray, const char *inputFile)
339 {
340 // trans to string format
341 sysCapArray = cJSON_GetObjectItem(freeAfterDecodeRpcidInfo.rpcidRoot, "syscap");
342 if (sysCapArray == NULL || !cJSON_IsArray(sysCapArray)) {
343 PRINT_ERR("Get syscap failed. Input file: %s\n", inputFile);
344 return -1;
345 }
346 freeAfterDecodeRpcidInfo.sysCapArraySize = cJSON_GetArraySize(sysCapArray);
347 if (freeAfterDecodeRpcidInfo.sysCapArraySize < 0) {
348 PRINT_ERR("Get syscap size failed. Input file: %s\n", inputFile);
349 return -1;
350 }
351 // malloc for save os syscap index
352 freeAfterDecodeRpcidInfo.osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t)
353 * freeAfterDecodeRpcidInfo.sysCapArraySize);
354 if (freeAfterDecodeRpcidInfo.osSysCapIndex == NULL) {
355 PRINT_ERR("malloc failed.\n");
356 return -1;
357 }
358 return 0;
359 }
360
PrintResultToOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo,char * outBuffer,char * priSyscapArray,uint16_t indexOs,uint16_t indexPri)361 static void PrintResultToOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, char *outBuffer,
362 char *priSyscapArray, uint16_t indexOs, uint16_t indexPri)
363 {
364 int32_t ret = 0;
365 uint32_t i;
366 uint32_t outUint[RPCID_OUT_BUFFER] = {0};
367 outUint[0] = *(uint32_t *)freeAfterDecodeRpcidInfo.contextBuffer;
368 outUint[1] = *(uint32_t *)(freeAfterDecodeRpcidInfo.contextBuffer + sizeof(uint32_t));
369 uint8_t *osOutUint = (uint8_t *)(outUint + 2);
370 if (SetOsSysCapBitMap(osOutUint, 120, freeAfterDecodeRpcidInfo.osSysCapIndex, indexOs)) { // 120, len of osOutUint
371 PRINT_ERR("Set os syscap bit map failed.\n");
372 return;
373 }
374
375 uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER + SINGLE_SYSCAP_LEN * indexPri;
376 outBuffer = (char *)malloc(outBufferLen);
377 if (outBuffer == NULL) {
378 PRINT_ERR("malloc(%u) failed.\n", outBufferLen);
379 return;
380 }
381 (void)memset_s(outBuffer, outBufferLen, 0, outBufferLen);
382
383 ret = sprintf_s(outBuffer, outBufferLen, "%u", outUint[0]);
384 if (ret == -1) {
385 PRINT_ERR("sprintf_s failed.\n");
386 free(outBuffer);
387 return;
388 }
389 for (i = 1; i < RPCID_OUT_BUFFER; i++) {
390 ret = sprintf_s(outBuffer, outBufferLen, "%s,%u", outBuffer, outUint[i]);
391 if (ret == -1) {
392 PRINT_ERR("sprintf_s failed.\n");
393 free(outBuffer);
394 return;
395 }
396 }
397
398 for (i = 0; i < indexPri; i++) {
399 ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, priSyscapArray + i * SINGLE_SYSCAP_LEN);
400 if (ret == -1) {
401 PRINT_ERR("sprintf_s failed.\n");
402 free(outBuffer);
403 return;
404 }
405 }
406 }
407
PartSysCapAndOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo,char * outBuffer,char * priSyscapArray,cJSON * sysCapArray)408 static void PartSysCapAndOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, char *outBuffer,
409 char *priSyscapArray, cJSON *sysCapArray)
410 {
411 uint32_t i;
412 int32_t ret = 0;
413 uint16_t indexOs = 0;
414 uint16_t indexPri = 0;
415 cJSON *cJsonTemp = NULL;
416
417 freeAfterDecodeRpcidInfo.sysCapDefine = CreateWholeSyscapJsonObj();
418 (void)memset_s(priSyscapArray, freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN,
419 0, freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN);
420 freeAfterDecodeRpcidInfo.priSyscap = priSyscapArray;
421 // part os syscap and ptivate syscap
422 for (i = 0; i < (uint32_t)freeAfterDecodeRpcidInfo.sysCapArraySize; i++) {
423 cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i);
424 if (cJsonItem->valuestring == NULL) {
425 cJSON_Delete(cJsonItem);
426 return;
427 }
428
429 cJsonTemp = cJSON_GetObjectItem(freeAfterDecodeRpcidInfo.sysCapDefine, cJsonItem->valuestring);
430 if (cJsonTemp != NULL && cJSON_IsNumber(cJsonTemp)) {
431 freeAfterDecodeRpcidInfo.osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint);
432 } else {
433 ret = strcpy_s(freeAfterDecodeRpcidInfo.priSyscap, SINGLE_SYSCAP_LEN, cJsonItem->valuestring);
434 if (ret != EOK) {
435 PRINT_ERR("strcpy_s failed.\n");
436 return;
437 }
438 priSyscapArray += SINGLE_SYSCAP_LEN;
439 indexPri++;
440 }
441 }
442 PrintResultToOutBuffer(freeAfterDecodeRpcidInfo, outBuffer, priSyscapArray, indexOs, indexPri);
443 }
444
FreeAfterDecodeRpcidToString(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo,int32_t type,char * outBuffer)445 static char *FreeAfterDecodeRpcidToString(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, int32_t type,
446 char *outBuffer)
447 {
448 switch (type) {
449 case FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID:
450 SafeFree(freeAfterDecodeRpcidInfo.priSyscap);
451 free(freeAfterDecodeRpcidInfo.osSysCapIndex);
452 cJSON_Delete(freeAfterDecodeRpcidInfo.sysCapDefine);
453 cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot);
454 FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer);
455 break;
456 case FREE_MALLOC_OSSYSCAP_AFTER_DECODE_RPCID:
457 free(freeAfterDecodeRpcidInfo.osSysCapIndex);
458 cJSON_Delete(freeAfterDecodeRpcidInfo.sysCapDefine);
459 cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot);
460 FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer);
461 break;
462 case FREE_WHOLE_SYSCAP_AFTER_DECODE_RPCID:
463 cJSON_Delete(freeAfterDecodeRpcidInfo.sysCapDefine);
464 cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot);
465 FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer);
466 break;
467 case FREE_RPCID_ROOT_AFTER_DECODE_RPCID:
468 cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot);
469 FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer);
470 break;
471 case FREE_CONTEXT_OUT_AFTER_DECODE_RPCID:
472 default:
473 FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer);
474 }
475 return outBuffer;
476 }
477
DecodeRpcidToStringFormat(const char * inputFile)478 char *DecodeRpcidToStringFormat(const char *inputFile)
479 {
480 int32_t ret = 0;
481 uint32_t bufferLen;
482 char *priSyscapArray = NULL;
483 char *outBuffer = NULL;
484 cJSON *sysCapArray = NULL;
485
486 struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo;
487 freeAfterDecodeRpcidInfo.priSyscap = NULL;
488 freeAfterDecodeRpcidInfo.osSysCapIndex = 0;
489 freeAfterDecodeRpcidInfo.sysCapDefine = NULL;
490 freeAfterDecodeRpcidInfo.rpcidRoot = NULL;
491 freeAfterDecodeRpcidInfo.contextBuffer = NULL;
492 freeAfterDecodeRpcidInfo.sysCapArraySize = 0;
493
494 // check rpcid.sc
495 if (CheckRpcidFormat(inputFile, &freeAfterDecodeRpcidInfo.contextBuffer, &bufferLen)) {
496 PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile);
497 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_CONTEXT_OUT_AFTER_DECODE_RPCID, outBuffer);
498 }
499
500 // parse rpcid to json
501 freeAfterDecodeRpcidInfo.rpcidRoot = cJSON_CreateObject();
502 if (freeAfterDecodeRpcidInfo.rpcidRoot == NULL) {
503 PRINT_ERR("Failed to create cJSON object for rpcidRoot\n");
504 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_CONTEXT_OUT_AFTER_DECODE_RPCID, outBuffer);
505 }
506
507 if (ParseRpcidToJson(freeAfterDecodeRpcidInfo.contextBuffer, bufferLen, freeAfterDecodeRpcidInfo.rpcidRoot)) {
508 PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile);
509 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_RPCID_ROOT_AFTER_DECODE_RPCID, outBuffer);
510 }
511 ret = TransStringFormatAndSaveSyscap(freeAfterDecodeRpcidInfo, sysCapArray, inputFile);
512 if (ret == -1) {
513 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_WHOLE_SYSCAP_AFTER_DECODE_RPCID, outBuffer);
514 }
515
516 (void)memset_s(freeAfterDecodeRpcidInfo.osSysCapIndex, sizeof(uint16_t) * freeAfterDecodeRpcidInfo
517 .sysCapArraySize, 0, sizeof(uint16_t) * freeAfterDecodeRpcidInfo.sysCapArraySize);
518 // malloc for save private syscap string
519 priSyscapArray = (char *)malloc(freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN);
520 if (priSyscapArray == NULL) {
521 PRINT_ERR("malloc(%u) failed.\n", (uint32_t)freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN);
522 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_MALLOC_OSSYSCAP_AFTER_DECODE_RPCID,
523 outBuffer);
524 }
525
526 PartSysCapAndOutBuffer(freeAfterDecodeRpcidInfo, outBuffer, priSyscapArray, sysCapArray);
527 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID, outBuffer);
528 }
529
CopySyscopToRet(struct PcidPriSyscapInfo * pcidPriSyscapInfo,const size_t allSyscapNum,char * tempSyscap,uint32_t i,uint8_t k)530 static int32_t CopySyscopToRet(struct PcidPriSyscapInfo *pcidPriSyscapInfo, const size_t allSyscapNum,
531 char *tempSyscap, uint32_t i, uint8_t k)
532 {
533 uint32_t pos = (i - 2) * INT_BIT + k;
534 uint32_t t;
535 for (t = 0; t < allSyscapNum; t++) {
536 if (g_arraySyscap[t].num == pos) {
537 break;
538 }
539 }
540 if (t == allSyscapNum) {
541 return -1;
542 }
543 pcidPriSyscapInfo->ret = strcpy_s(tempSyscap, sizeof(char) * SINGLE_SYSCAP_LEN, g_arraySyscap[t].str);
544 // 2, header of pcid & rpcid
545 if (pcidPriSyscapInfo->ret != EOK) {
546 return -1;
547 }
548 return 0;
549 }
550
CheckPcidEachBit(struct PcidPriSyscapInfo * pcidPriSyscapInfo,CompareError * result,const size_t allSyscapNum,uint32_t i,uint32_t blockBits)551 static int32_t CheckPcidEachBit(struct PcidPriSyscapInfo *pcidPriSyscapInfo, CompareError *result,
552 const size_t allSyscapNum, uint32_t i, uint32_t blockBits)
553 {
554 int32_t ret = 0;
555 for (uint8_t k = 0; k < INT_BIT; k++) {
556 if (blockBits & (1U << k)) {
557 char *tempSyscap = (char *)malloc(sizeof(char) * SINGLE_SYSCAP_LEN);
558 if (tempSyscap == NULL) {
559 PRINT_ERR("malloc failed.\n");
560 FreeCompareError(result);
561 return -1;
562 }
563 ret = CopySyscopToRet(pcidPriSyscapInfo, allSyscapNum, tempSyscap, i, k);
564 if (ret != EOK) {
565 PRINT_ERR("strcpy_s failed.\n");
566 FreeCompareError(result);
567 free(tempSyscap);
568 return -1;
569 }
570 result->syscap[pcidPriSyscapInfo->ossyscapFlag++] = tempSyscap;
571 }
572 }
573 return ret;
574 }
575
ComparePcidWithOsSyscap(struct PcidPriSyscapInfo * pcidPriSyscapInfo,const uint32_t * pcidOsAarry,const uint32_t * rpcidOsAarry,CompareError * result,const size_t allSyscapNum)576 static int32_t ComparePcidWithOsSyscap(struct PcidPriSyscapInfo *pcidPriSyscapInfo,
577 const uint32_t *pcidOsAarry, const uint32_t *rpcidOsAarry, CompareError *result,
578 const size_t allSyscapNum)
579 {
580 uint32_t i;
581 int32_t ret = 0;
582
583 for (i = 2; i < PCID_OUT_BUFFER; i++) { // 2, header of pcid & rpcid
584 uint32_t blockBits = (pcidOsAarry[i] ^ rpcidOsAarry[i]) & rpcidOsAarry[i];
585 if (!blockBits) {
586 continue;
587 }
588 ret = CheckPcidEachBit(pcidPriSyscapInfo, result, allSyscapNum, i, blockBits);
589 if (ret == -1) {
590 return -1;
591 }
592 }
593 return 0;
594 }
595
ComparePcidWithPriSyscap(struct PcidPriSyscapInfo pcidPriSyscapInfo,CompareError * result,uint16_t versionFlag)596 static int32_t ComparePcidWithPriSyscap(struct PcidPriSyscapInfo pcidPriSyscapInfo, CompareError *result,
597 uint16_t versionFlag)
598 {
599 uint32_t i, j;
600 uint16_t prisyscapFlag = 0;
601 uint32_t retFlag = 0;
602 bool priSysFound = false;
603
604 for (i = 0; i < pcidPriSyscapInfo.rpcidPriSyscapLen; i++) {
605 for (j = 0; j < pcidPriSyscapInfo.pcidPriSyscapLen; j++) {
606 if (strcmp(pcidPriSyscapInfo.rpcidPriSyscap + SINGLE_SYSCAP_LEN * i,
607 pcidPriSyscapInfo.pcidPriSyscap + SINGLE_SYSCAP_LEN * j) == 0) {
608 priSysFound = true;
609 break;
610 }
611 }
612 if (priSysFound != true) {
613 char *temp = (char *)malloc(sizeof(char) * SINGLE_SYSCAP_LEN);
614 if (temp == NULL) {
615 PRINT_ERR("malloc failed.\n");
616 FreeCompareError(result);
617 return -1;
618 }
619 pcidPriSyscapInfo.ret = strcpy_s(temp, sizeof(char) * SINGLE_SYSCAP_LEN,
620 pcidPriSyscapInfo.rpcidPriSyscap + SINGLE_SYSCAP_LEN * i);
621 if (pcidPriSyscapInfo.ret != EOK) {
622 FreeCompareError(result);
623 PRINT_ERR("strcpy_s failed.\n");
624 free(temp);
625 return -1;
626 }
627 if (pcidPriSyscapInfo.ossyscapFlag + prisyscapFlag >= MAX_MISS_SYSCAP) {
628 FreeCompareError(result);
629 PRINT_ERR("array index out of bounds.\n");
630 free(temp);
631 return -1;
632 }
633 result->syscap[pcidPriSyscapInfo.ossyscapFlag + prisyscapFlag] = temp;
634 ++prisyscapFlag;
635 }
636 priSysFound = false;
637 }
638
639 if (versionFlag > 0) {
640 retFlag |= 1U << 0;
641 }
642 if (pcidPriSyscapInfo.ossyscapFlag > 0 || prisyscapFlag > 0) {
643 retFlag |= 1U << 1;
644 result->missSyscapNum = pcidPriSyscapInfo.ossyscapFlag + prisyscapFlag;
645 }
646 return (int32_t)retFlag;
647 }
648
ComparePcidString(const char * pcidString,const char * rpcidString,CompareError * result)649 int32_t ComparePcidString(const char *pcidString, const char *rpcidString, CompareError *result)
650 {
651 uint16_t versionFlag = 0;
652 int32_t errorFlag = 0;
653 struct PcidPriSyscapInfo pcidPriSyscapInfo;
654 pcidPriSyscapInfo.pcidPriSyscap = NULL;
655 pcidPriSyscapInfo.rpcidPriSyscap = NULL;
656 pcidPriSyscapInfo.pcidPriSyscapLen = 0;
657 pcidPriSyscapInfo.rpcidPriSyscapLen = 0;
658 pcidPriSyscapInfo.ossyscapFlag = 0;
659 pcidPriSyscapInfo.ret = 0;
660 uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0};
661 uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0};
662 const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
663
664 pcidPriSyscapInfo.ret = SeparateSyscapFromString(pcidString, pcidOsAarry, PCID_OUT_BUFFER,
665 &pcidPriSyscapInfo.pcidPriSyscap, &pcidPriSyscapInfo.pcidPriSyscapLen);
666 pcidPriSyscapInfo.ret += SeparateSyscapFromString(rpcidString, rpcidOsAarry, RPCID_OUT_BUFFER,
667 &pcidPriSyscapInfo.rpcidPriSyscap, &pcidPriSyscapInfo.rpcidPriSyscapLen);
668 if (pcidPriSyscapInfo.ret != 0) {
669 PRINT_ERR("Separate syscap from string failed. ret = %d\n", pcidPriSyscapInfo.ret);
670 return -1;
671 }
672 result->missSyscapNum = 0;
673 // compare version
674 uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsAarry)->apiVersion);
675 uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion);
676 if (pcidVersion < rpcidVersion) {
677 result->targetApiVersion = rpcidVersion;
678 versionFlag = 1;
679 }
680
681 // compare os sysscap
682 errorFlag = ComparePcidWithOsSyscap(&pcidPriSyscapInfo, pcidOsAarry, rpcidOsAarry, result, allSyscapNum);
683 if (errorFlag == -1) {
684 return errorFlag;
685 }
686
687 // compare pri syscap
688 return ComparePcidWithPriSyscap(pcidPriSyscapInfo, result, versionFlag);
689 }
690
FreeCompareError(CompareError * result)691 int32_t FreeCompareError(CompareError *result)
692 {
693 if (result == NULL) {
694 return 0;
695 }
696 for (int i = 0; i < result->missSyscapNum; i++) {
697 free(result->syscap[i]);
698 result->syscap[i] = NULL;
699 }
700 result->missSyscapNum = 0;
701 result->targetApiVersion = 0;
702 return 0;
703 }