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 free(freeAfterDecodeRpcidInfo.osSysCapIndex);
359 return 0;
360 }
361
PrintResultToOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, char *outBuffer, char *priSyscapArray, uint16_t indexOs, uint16_t indexPri)362 static void PrintResultToOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, char *outBuffer,
363 char *priSyscapArray, uint16_t indexOs, uint16_t indexPri)
364 {
365 int32_t ret = 0;
366 uint32_t i;
367 uint32_t outUint[RPCID_OUT_BUFFER] = {0};
368 outUint[0] = *(uint32_t *)freeAfterDecodeRpcidInfo.contextBuffer;
369 outUint[1] = *(uint32_t *)(freeAfterDecodeRpcidInfo.contextBuffer + sizeof(uint32_t));
370 uint8_t *osOutUint = (uint8_t *)(outUint + 2);
371 if (SetOsSysCapBitMap(osOutUint, 120, freeAfterDecodeRpcidInfo.osSysCapIndex, indexOs)) { // 120, len of osOutUint
372 PRINT_ERR("Set os syscap bit map failed.\n");
373 return;
374 }
375
376 uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER + SINGLE_SYSCAP_LEN * indexPri;
377 outBuffer = (char *)malloc(outBufferLen);
378 if (outBuffer == NULL) {
379 PRINT_ERR("malloc(%u) failed.\n", outBufferLen);
380 return;
381 }
382 (void)memset_s(outBuffer, outBufferLen, 0, outBufferLen);
383
384 ret = sprintf_s(outBuffer, outBufferLen, "%u", outUint[0]);
385 if (ret == -1) {
386 PRINT_ERR("sprintf_s failed.\n");
387 free(outBuffer);
388 return;
389 }
390 for (i = 1; i < RPCID_OUT_BUFFER; i++) {
391 ret = sprintf_s(outBuffer, outBufferLen, "%s,%u", outBuffer, outUint[i]);
392 if (ret == -1) {
393 PRINT_ERR("sprintf_s failed.\n");
394 free(outBuffer);
395 return;
396 }
397 }
398
399 for (i = 0; i < indexPri; i++) {
400 ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, priSyscapArray + i * SINGLE_SYSCAP_LEN);
401 if (ret == -1) {
402 PRINT_ERR("sprintf_s failed.\n");
403 free(outBuffer);
404 return;
405 }
406 }
407 }
408
PartSysCapAndOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, char *outBuffer, char *priSyscapArray, cJSON *sysCapArray)409 static void PartSysCapAndOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, char *outBuffer,
410 char *priSyscapArray, cJSON *sysCapArray)
411 {
412 uint32_t i;
413 int32_t ret = 0;
414 uint16_t indexOs = 0;
415 uint16_t indexPri = 0;
416 cJSON *cJsonTemp = NULL;
417
418 freeAfterDecodeRpcidInfo.sysCapDefine = CreateWholeSyscapJsonObj();
419 (void)memset_s(priSyscapArray, freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN,
420 0, freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN);
421 freeAfterDecodeRpcidInfo.priSyscap = priSyscapArray;
422 // part os syscap and ptivate syscap
423 for (i = 0; i < (uint32_t)freeAfterDecodeRpcidInfo.sysCapArraySize; i++) {
424 cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i);
425 if (cJsonItem->valuestring == NULL) {
426 cJSON_Delete(cJsonItem);
427 return;
428 }
429
430 cJsonTemp = cJSON_GetObjectItem(freeAfterDecodeRpcidInfo.sysCapDefine, cJsonItem->valuestring);
431 if (cJsonTemp != NULL && cJSON_IsNumber(cJsonTemp)) {
432 freeAfterDecodeRpcidInfo.osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint);
433 } else {
434 ret = strcpy_s(freeAfterDecodeRpcidInfo.priSyscap, SINGLE_SYSCAP_LEN, cJsonItem->valuestring);
435 if (ret != EOK) {
436 PRINT_ERR("strcpy_s failed.\n");
437 return;
438 }
439 priSyscapArray += SINGLE_SYSCAP_LEN;
440 indexPri++;
441 }
442 }
443 PrintResultToOutBuffer(freeAfterDecodeRpcidInfo, outBuffer, priSyscapArray, indexOs, indexPri);
444 }
445
FreeAfterDecodeRpcidToString(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, int32_t type, char *outBuffer)446 static char *FreeAfterDecodeRpcidToString(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, int32_t type,
447 char *outBuffer)
448 {
449 switch (type) {
450 case FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID:
451 SafeFree(freeAfterDecodeRpcidInfo.priSyscap);
452 free(freeAfterDecodeRpcidInfo.osSysCapIndex);
453 cJSON_Delete(freeAfterDecodeRpcidInfo.sysCapDefine);
454 cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot);
455 FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer);
456 break;
457 case FREE_MALLOC_OSSYSCAP_AFTER_DECODE_RPCID:
458 free(freeAfterDecodeRpcidInfo.osSysCapIndex);
459 cJSON_Delete(freeAfterDecodeRpcidInfo.sysCapDefine);
460 cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot);
461 FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer);
462 break;
463 case FREE_WHOLE_SYSCAP_AFTER_DECODE_RPCID:
464 cJSON_Delete(freeAfterDecodeRpcidInfo.sysCapDefine);
465 cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot);
466 FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer);
467 break;
468 case FREE_RPCID_ROOT_AFTER_DECODE_RPCID:
469 cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot);
470 FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer);
471 break;
472 case FREE_CONTEXT_OUT_AFTER_DECODE_RPCID:
473 default:
474 FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer);
475 }
476 return outBuffer;
477 }
478
DecodeRpcidToStringFormat(const char *inputFile)479 char *DecodeRpcidToStringFormat(const char *inputFile)
480 {
481 int32_t ret = 0;
482 uint32_t bufferLen;
483 char *priSyscapArray = NULL;
484 char *outBuffer = NULL;
485 cJSON *sysCapArray = NULL;
486
487 struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo;
488 freeAfterDecodeRpcidInfo.priSyscap = NULL;
489 freeAfterDecodeRpcidInfo.osSysCapIndex = 0;
490 freeAfterDecodeRpcidInfo.sysCapDefine = NULL;
491 freeAfterDecodeRpcidInfo.rpcidRoot = NULL;
492 freeAfterDecodeRpcidInfo.contextBuffer = NULL;
493 freeAfterDecodeRpcidInfo.sysCapArraySize = 0;
494
495 // check rpcid.sc
496 if (CheckRpcidFormat(inputFile, &freeAfterDecodeRpcidInfo.contextBuffer, &bufferLen)) {
497 PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile);
498 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_CONTEXT_OUT_AFTER_DECODE_RPCID, outBuffer);
499 }
500
501 // parse rpcid to json
502 freeAfterDecodeRpcidInfo.rpcidRoot = cJSON_CreateObject();
503 if (freeAfterDecodeRpcidInfo.rpcidRoot == NULL) {
504 PRINT_ERR("Failed to create cJSON object for rpcidRoot\n");
505 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_CONTEXT_OUT_AFTER_DECODE_RPCID, outBuffer);
506 }
507
508 if (ParseRpcidToJson(freeAfterDecodeRpcidInfo.contextBuffer, bufferLen, freeAfterDecodeRpcidInfo.rpcidRoot)) {
509 PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile);
510 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_RPCID_ROOT_AFTER_DECODE_RPCID, outBuffer);
511 }
512 ret = TransStringFormatAndSaveSyscap(freeAfterDecodeRpcidInfo, sysCapArray, inputFile);
513 if (ret == -1) {
514 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_WHOLE_SYSCAP_AFTER_DECODE_RPCID, outBuffer);
515 }
516
517 (void)memset_s(freeAfterDecodeRpcidInfo.osSysCapIndex, sizeof(uint16_t) * freeAfterDecodeRpcidInfo
518 .sysCapArraySize, 0, sizeof(uint16_t) * freeAfterDecodeRpcidInfo.sysCapArraySize);
519 // malloc for save private syscap string
520 priSyscapArray = (char *)malloc(freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN);
521 if (priSyscapArray == NULL) {
522 PRINT_ERR("malloc(%u) failed.\n", (uint32_t)freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN);
523 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_MALLOC_OSSYSCAP_AFTER_DECODE_RPCID,
524 outBuffer);
525 }
526
527 PartSysCapAndOutBuffer(freeAfterDecodeRpcidInfo, outBuffer, priSyscapArray, sysCapArray);
528 return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID, outBuffer);
529 }
530
CopySyscopToRet(struct PcidPriSyscapInfo *pcidPriSyscapInfo, const size_t allSyscapNum, char *tempSyscap, uint32_t i, uint8_t k)531 static int32_t CopySyscopToRet(struct PcidPriSyscapInfo *pcidPriSyscapInfo, const size_t allSyscapNum,
532 char *tempSyscap, uint32_t i, uint8_t k)
533 {
534 uint32_t pos = (i - 2) * INT_BIT + k;
535 uint32_t t;
536 for (t = 0; t < allSyscapNum; t++) {
537 if (g_arraySyscap[t].num == pos) {
538 break;
539 }
540 }
541 if (t == allSyscapNum) {
542 return -1;
543 }
544 pcidPriSyscapInfo->ret = strcpy_s(tempSyscap, sizeof(char) * SINGLE_SYSCAP_LEN, g_arraySyscap[t].str);
545 // 2, header of pcid & rpcid
546 if (pcidPriSyscapInfo->ret != EOK) {
547 return -1;
548 }
549 return 0;
550 }
551
CheckPcidEachBit(struct PcidPriSyscapInfo *pcidPriSyscapInfo, CompareError *result, const size_t allSyscapNum, uint32_t i, uint32_t blockBits)552 static int32_t CheckPcidEachBit(struct PcidPriSyscapInfo *pcidPriSyscapInfo, CompareError *result,
553 const size_t allSyscapNum, uint32_t i, uint32_t blockBits)
554 {
555 int32_t ret = 0;
556 for (uint8_t k = 0; k < INT_BIT; k++) {
557 if (blockBits & (1U << k)) {
558 char *tempSyscap = (char *)malloc(sizeof(char) * SINGLE_SYSCAP_LEN);
559 if (tempSyscap == NULL) {
560 PRINT_ERR("malloc failed.\n");
561 FreeCompareError(result);
562 return -1;
563 }
564 ret = CopySyscopToRet(pcidPriSyscapInfo, allSyscapNum, tempSyscap, i, k);
565 if (ret != EOK) {
566 PRINT_ERR("strcpy_s failed.\n");
567 FreeCompareError(result);
568 free(tempSyscap);
569 return -1;
570 }
571 result->syscap[pcidPriSyscapInfo->ossyscapFlag++] = tempSyscap;
572 }
573 }
574 return ret;
575 }
576
ComparePcidWithOsSyscap(struct PcidPriSyscapInfo *pcidPriSyscapInfo, const uint32_t *pcidOsAarry, const uint32_t *rpcidOsAarry, CompareError *result, const size_t allSyscapNum)577 static int32_t ComparePcidWithOsSyscap(struct PcidPriSyscapInfo *pcidPriSyscapInfo,
578 const uint32_t *pcidOsAarry, const uint32_t *rpcidOsAarry, CompareError *result,
579 const size_t allSyscapNum)
580 {
581 uint32_t i;
582 int32_t ret = 0;
583
584 for (i = 2; i < PCID_OUT_BUFFER; i++) { // 2, header of pcid & rpcid
585 uint32_t blockBits = (pcidOsAarry[i] ^ rpcidOsAarry[i]) & rpcidOsAarry[i];
586 if (!blockBits) {
587 continue;
588 }
589 ret = CheckPcidEachBit(pcidPriSyscapInfo, result, allSyscapNum, i, blockBits);
590 if (ret == -1) {
591 return -1;
592 }
593 }
594 return 0;
595 }
596
ComparePcidWithPriSyscap(struct PcidPriSyscapInfo pcidPriSyscapInfo, CompareError *result, uint16_t versionFlag)597 static int32_t ComparePcidWithPriSyscap(struct PcidPriSyscapInfo pcidPriSyscapInfo, CompareError *result,
598 uint16_t versionFlag)
599 {
600 uint32_t i, j;
601 uint16_t prisyscapFlag = 0;
602 uint32_t retFlag = 0;
603 bool priSysFound = false;
604
605 for (i = 0; i < pcidPriSyscapInfo.rpcidPriSyscapLen; i++) {
606 for (j = 0; j < pcidPriSyscapInfo.pcidPriSyscapLen; j++) {
607 if (strcmp(pcidPriSyscapInfo.rpcidPriSyscap + SINGLE_SYSCAP_LEN * i,
608 pcidPriSyscapInfo.pcidPriSyscap + SINGLE_SYSCAP_LEN * j) == 0) {
609 priSysFound = true;
610 break;
611 }
612 }
613 if (priSysFound != true) {
614 char *temp = (char *)malloc(sizeof(char) * SINGLE_SYSCAP_LEN);
615 if (temp == NULL) {
616 PRINT_ERR("malloc failed.\n");
617 FreeCompareError(result);
618 return -1;
619 }
620 pcidPriSyscapInfo.ret = strcpy_s(temp, sizeof(char) * SINGLE_SYSCAP_LEN,
621 pcidPriSyscapInfo.rpcidPriSyscap + SINGLE_SYSCAP_LEN * i);
622 if (pcidPriSyscapInfo.ret != EOK) {
623 FreeCompareError(result);
624 PRINT_ERR("strcpy_s failed.\n");
625 free(temp);
626 return -1;
627 }
628 if (pcidPriSyscapInfo.ossyscapFlag + prisyscapFlag >= MAX_MISS_SYSCAP) {
629 FreeCompareError(result);
630 PRINT_ERR("array index out of bounds.\n");
631 free(temp);
632 return -1;
633 }
634 result->syscap[pcidPriSyscapInfo.ossyscapFlag + prisyscapFlag] = temp;
635 ++prisyscapFlag;
636 }
637 priSysFound = false;
638 }
639
640 if (versionFlag > 0) {
641 retFlag |= 1U << 0;
642 }
643 if (pcidPriSyscapInfo.ossyscapFlag > 0 || prisyscapFlag > 0) {
644 retFlag |= 1U << 1;
645 result->missSyscapNum = pcidPriSyscapInfo.ossyscapFlag + prisyscapFlag;
646 }
647 return (int32_t)retFlag;
648 }
649
ComparePcidString(const char *pcidString, const char *rpcidString, CompareError *result)650 int32_t ComparePcidString(const char *pcidString, const char *rpcidString, CompareError *result)
651 {
652 uint16_t versionFlag = 0;
653 int32_t errorFlag = 0;
654 struct PcidPriSyscapInfo pcidPriSyscapInfo;
655 pcidPriSyscapInfo.pcidPriSyscap = NULL;
656 pcidPriSyscapInfo.rpcidPriSyscap = NULL;
657 pcidPriSyscapInfo.pcidPriSyscapLen = 0;
658 pcidPriSyscapInfo.rpcidPriSyscapLen = 0;
659 pcidPriSyscapInfo.ossyscapFlag = 0;
660 pcidPriSyscapInfo.ret = 0;
661 uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0};
662 uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0};
663 const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
664
665 pcidPriSyscapInfo.ret = SeparateSyscapFromString(pcidString, pcidOsAarry, PCID_OUT_BUFFER,
666 &pcidPriSyscapInfo.pcidPriSyscap, &pcidPriSyscapInfo.pcidPriSyscapLen);
667 pcidPriSyscapInfo.ret += SeparateSyscapFromString(rpcidString, rpcidOsAarry, RPCID_OUT_BUFFER,
668 &pcidPriSyscapInfo.rpcidPriSyscap, &pcidPriSyscapInfo.rpcidPriSyscapLen);
669 if (pcidPriSyscapInfo.ret != 0) {
670 PRINT_ERR("Separate syscap from string failed. ret = %d\n", pcidPriSyscapInfo.ret);
671 return -1;
672 }
673 result->missSyscapNum = 0;
674 // compare version
675 uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsAarry)->apiVersion);
676 uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion);
677 if (pcidVersion < rpcidVersion) {
678 result->targetApiVersion = rpcidVersion;
679 versionFlag = 1;
680 }
681
682 // compare os sysscap
683 errorFlag = ComparePcidWithOsSyscap(&pcidPriSyscapInfo, pcidOsAarry, rpcidOsAarry, result, allSyscapNum);
684 if (errorFlag == -1) {
685 return errorFlag;
686 }
687
688 // compare pri syscap
689 return ComparePcidWithPriSyscap(pcidPriSyscapInfo, result, versionFlag);
690 }
691
FreeCompareError(CompareError *result)692 int32_t FreeCompareError(CompareError *result)
693 {
694 if (result == NULL) {
695 return 0;
696 }
697 for (int i = 0; i < result->missSyscapNum; i++) {
698 free(result->syscap[i]);
699 result->syscap[i] = NULL;
700 }
701 result->missSyscapNum = 0;
702 result->targetApiVersion = 0;
703 return 0;
704 }