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