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