1 /*
2 * Copyright (C) 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 <stdint.h>
17 #include <stdio.h>
18 #include <stdbool.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <sys/stat.h>
24 #include "securec.h"
25 #include "endian_internal.h"
26 #include "cJSON.h"
27 #include "create_pcid.h"
28 #include "syscap_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 typedef struct RequiredProductCompatibilityIDHead {
37 uint16_t apiVersion : 15;
38 uint16_t apiVersionType : 1;
39 } RPCIDHead;
40
41 #define SYSCAP_PREFIX_LEN 17
42 #define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN)
43 #define UINT8_BIT 8
44 #define INT_BIT 32
45 #define RPCID_OUT_BUFFER 32
46 #define PCID_OUT_BUFFER RPCID_OUT_BUFFER
47 #define BYTES_OF_OS_SYSCAP 120
48 #define U32_TO_STR_MAX_LEN 11
49 #define STRING_FORMAT_LEN_MAX 1024
50
51 #define PRINT_ERR(...) \
52 do { \
53 printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \
54 printf(__VA_ARGS__); \
55 } while (0)
56
FreeContextBuffer(char * contextBuffer)57 static void FreeContextBuffer(char *contextBuffer)
58 {
59 (void)free(contextBuffer);
60 }
61
GetFileContext(char * inputFile,char ** contextBufPtr,uint32_t * bufferLen)62 static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *bufferLen)
63 {
64 int32_t ret;
65 FILE *fp = NULL;
66 struct stat statBuf;
67 char *contextBuffer = NULL;
68 char path[PATH_MAX + 1] = {0x00};
69
70 #ifdef _POSIX_
71 if (strlen(inputFile) > PATH_MAX || strncpy_s(path, PATH_MAX, inputFile, strlen(inputFile)) != EOK) {
72 PRINT_ERR("get path(%s) failed\n", inputFile);
73 return -1;
74 }
75 #else
76 if (strlen(inputFile) > PATH_MAX || realpath(inputFile, path) == NULL) {
77 PRINT_ERR("get file(%s) real path failed\n", inputFile);
78 return -1;
79 }
80 #endif
81
82 ret = stat(path, &statBuf);
83 if (ret != 0) {
84 PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", path, errno);
85 return -1;
86 }
87 if (!(statBuf.st_mode & S_IRUSR)) {
88 PRINT_ERR("don't have permission to read the file(%s)\n", path);
89 return -1;
90 }
91 contextBuffer = (char *)malloc(statBuf.st_size + 1);
92 if (contextBuffer == NULL) {
93 PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno);
94 return -1;
95 }
96 fp = fopen(path, "rb");
97 if (fp == NULL) {
98 PRINT_ERR("open file(%s) failed, errno = %d\n", path, errno);
99 FreeContextBuffer(contextBuffer);
100 return -1;
101 }
102 size_t retFread = fread(contextBuffer, statBuf.st_size, 1, fp);
103 if (retFread != 1) {
104 PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno);
105 FreeContextBuffer(contextBuffer);
106 (void)fclose(fp);
107 return -1;
108 }
109 contextBuffer[statBuf.st_size] = '\0';
110 (void)fclose(fp);
111
112 *contextBufPtr = contextBuffer;
113 *bufferLen = statBuf.st_size + 1;
114 return 0;
115 }
116
ConvertedContextSaveAsFile(char * outDirPath,const char * filename,char * convertedBuffer,size_t bufferLen)117 static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename,
118 char *convertedBuffer, size_t bufferLen)
119 {
120 int32_t ret;
121 FILE *fp = NULL;
122 char path[PATH_MAX + 1] = {0x00};
123
124 #ifdef _POSIX_
125 if (strlen(outDirPath) >= PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) {
126 PRINT_ERR("get path(%s) failed\n", outDirPath);
127 return -1;
128 }
129 #else
130 if (strlen(outDirPath) >= PATH_MAX || realpath(outDirPath, path) == NULL) {
131 PRINT_ERR("get file(%s) real path failed\n", outDirPath);
132 return -1;
133 }
134 #endif
135
136 int32_t pathLen = strlen(path);
137 if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') {
138 path[pathLen] = '/';
139 }
140
141 if (strlen(filename) + 1 > PATH_MAX) {
142 PRINT_ERR("filename(%s) too long.\n", filename);
143 return -1;
144 }
145 ret = strncat_s(path, PATH_MAX, filename, strlen(filename));
146 if (ret != 0) {
147 PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n",
148 path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno);
149 return -1;
150 }
151
152 fp = fopen(path, "wb");
153 if (fp == NULL) {
154 PRINT_ERR("can`t create file(%s), errno = %d\n", path, errno);
155 return -1;
156 }
157
158 size_t retFwrite = fwrite(convertedBuffer, bufferLen, 1, fp);
159 if (retFwrite != 1) {
160 PRINT_ERR("can`t write file(%s),errno = %d\n", path, errno);
161 (void)fclose(fp);
162 return -1;
163 }
164
165 (void)fclose(fp);
166
167 return 0;
168 }
169
CreateWholeSyscapJsonObj(void)170 static cJSON *CreateWholeSyscapJsonObj(void)
171 {
172 size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
173 cJSON *root = cJSON_CreateObject();
174 for (size_t i = 0; i < allSyscapNum; i++) {
175 cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num));
176 }
177 return root;
178 }
179
RPCIDEncode(char * inputFile,char * outputPath)180 int32_t RPCIDEncode(char *inputFile, char *outputPath)
181 {
182 int32_t ret;
183 char *contextBuffer = NULL;
184 uint32_t bufferLen, sysCapSize;
185 char *convertedBuffer = NULL;
186 uint32_t convertedBufLen = sizeof(RPCIDHead);
187 RPCIDHead *headPtr = NULL;
188 char *fillTmpPtr = NULL;
189 cJSON *cjsonObjectRoot = NULL;
190 cJSON *apiVerItem = NULL;
191 cJSON *sysCapPtr = NULL;
192 cJSON *arrayItemPtr = NULL;
193
194 ret = GetFileContext(inputFile, &contextBuffer, &bufferLen);
195 if (ret != 0) {
196 PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
197 return ret;
198 }
199
200 cjsonObjectRoot = cJSON_ParseWithLength(contextBuffer, bufferLen);
201 if (cjsonObjectRoot == NULL) {
202 PRINT_ERR("cJSON_Parse failed, context buffer is:\n%s\n", contextBuffer);
203 ret = -1;
204 goto FREE_CONTEXT_OUT;
205 }
206
207 sysCapPtr = cJSON_GetObjectItem(cjsonObjectRoot, "syscap");
208 if (sysCapPtr == NULL || !cJSON_IsArray(sysCapPtr)) {
209 PRINT_ERR("get \"syscap\" object failed.\n");
210 ret = -1;
211 goto FREE_CONTEXT_OUT;
212 }
213
214 ret = cJSON_GetArraySize(sysCapPtr);
215 if (ret < 0) {
216 PRINT_ERR("get \"syscap\" array size failed\n");
217 ret = -1;
218 goto FREE_CONTEXT_OUT;
219 }
220 sysCapSize = (uint32_t)ret;
221 // 2, to save SysCaptype & SysCapLength
222 convertedBufLen += (2 * sizeof(uint16_t) + sysCapSize * SINGLE_FEAT_LEN);
223
224 convertedBuffer = (char *)malloc(convertedBufLen);
225 if (convertedBuffer == NULL) {
226 PRINT_ERR("malloc failed\n");
227 ret = -1;
228 goto FREE_CONTEXT_OUT;
229 }
230 (void)memset_s(convertedBuffer, convertedBufLen, 0, convertedBufLen);
231
232 headPtr = (RPCIDHead *)convertedBuffer;
233 apiVerItem = cJSON_GetObjectItem(cjsonObjectRoot, "api_version");
234 if (apiVerItem == NULL || !cJSON_IsNumber(apiVerItem)) {
235 PRINT_ERR("get \"api_version\" failed\n");
236 ret = -1;
237 goto FREE_CONVERT_OUT;
238 }
239 headPtr->apiVersion = HtonsInter((uint16_t)apiVerItem->valueint);
240 headPtr->apiVersionType = 1;
241
242 fillTmpPtr = convertedBuffer + sizeof(RPCIDHead);
243
244 *(uint16_t *)fillTmpPtr = HtonsInter(2); // 2, SysCap Type, 2: request Cap
245 fillTmpPtr += sizeof(uint16_t);
246 // fill osCap Length
247 *(uint16_t *)fillTmpPtr = HtonsInter((uint16_t)(sysCapSize * SINGLE_FEAT_LEN));
248 fillTmpPtr += sizeof(uint16_t);
249 for (uint32_t i = 0; i < sysCapSize; i++) {
250 arrayItemPtr = cJSON_GetArrayItem(sysCapPtr, (int)i);
251 char *pointPos = strchr(arrayItemPtr->valuestring, '.');
252 if (pointPos == NULL) {
253 PRINT_ERR("context of \"syscap\" array is invalid\n");
254 ret = -1;
255 goto FREE_CONVERT_OUT;
256 }
257 ret = strncmp(arrayItemPtr->valuestring, "SystemCapability.", pointPos - arrayItemPtr->valuestring + 1);
258 if (ret != 0) {
259 PRINT_ERR("context of \"syscap\" array is invalid\n");
260 ret = -1;
261 goto FREE_CONVERT_OUT;
262 }
263
264 ret = memcpy_s(fillTmpPtr, SINGLE_FEAT_LEN, pointPos + 1, strlen(pointPos + 1));
265 if (ret != 0) {
266 PRINT_ERR("context of \"syscap\" array is invalid\n");
267 ret = -1;
268 goto FREE_CONVERT_OUT;
269 }
270 fillTmpPtr += SINGLE_FEAT_LEN;
271 }
272
273 ret = ConvertedContextSaveAsFile(outputPath, "RPCID.sc", convertedBuffer, convertedBufLen);
274 if (ret != 0) {
275 PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.sc\n", outputPath);
276 goto FREE_CONVERT_OUT;
277 }
278
279 FREE_CONVERT_OUT:
280 free(convertedBuffer);
281 FREE_CONTEXT_OUT:
282 FreeContextBuffer(contextBuffer);
283 return ret;
284 }
285
ParseRpcidToJson(char * input,uint32_t inputLen,cJSON * rpcidJson)286 static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson)
287 {
288 uint32_t i;
289 int32_t ret = 0;
290 uint16_t sysCapLength = NtohsInter(*(uint16_t *)(input + sizeof(uint32_t)));
291 if (sysCapLength > inputLen - sizeof(uint32_t)) {
292 PRINT_ERR("Get sysCapLength(%u) error, inputLen = %u\n", sysCapLength, inputLen);
293 return -1;
294 }
295 uint16_t sysCapCount = sysCapLength / SINGLE_FEAT_LEN;
296 char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t);
297 RPCIDHead *rpcidHeader = (RPCIDHead *)input;
298 cJSON *sysCapJson = cJSON_CreateArray();
299 for (i = 0; i < sysCapCount; i++) {
300 char *temp = sysCapBegin + i * SINGLE_FEAT_LEN;
301 if (strlen(temp) >= SINGLE_FEAT_LEN || strlen(temp) == 0) {
302 PRINT_ERR("Get SysCap failed, string length(%u) error.\n", (uint32_t)strlen(temp));
303 ret = -1;
304 goto FREE_SYSCAP_OUT;
305 }
306 char buffer[SINGLE_SYSCAP_LEN] = "SystemCapability.";
307
308 ret = strncat_s(buffer, sizeof(buffer), temp, SINGLE_FEAT_LEN);
309 if (ret != EOK) {
310 PRINT_ERR("strncat_s failed.\n");
311 goto FREE_SYSCAP_OUT;
312 }
313
314 if (!cJSON_AddItemToArray(sysCapJson, cJSON_CreateString(buffer))) {
315 PRINT_ERR("Add syscap string to json failed.\n");
316 ret = -1;
317 goto FREE_SYSCAP_OUT;
318 }
319 }
320
321 if (!cJSON_AddNumberToObject(rpcidJson, "api_version", NtohsInter(rpcidHeader->apiVersion))) {
322 PRINT_ERR("Add api_version to json failed.\n");
323 ret = -1;
324 goto FREE_SYSCAP_OUT;
325 }
326 if (!cJSON_AddItemToObject(rpcidJson, "syscap", sysCapJson)) {
327 PRINT_ERR("Add syscap to json failed.\n");
328 ret = -1;
329 goto FREE_SYSCAP_OUT;
330 }
331
332 return 0;
333 FREE_SYSCAP_OUT:
334 cJSON_Delete(sysCapJson);
335 return ret;
336 }
337
CheckRpcidFormat(char * inputFile,char ** buffer,uint32_t * len)338 static int32_t CheckRpcidFormat(char *inputFile, char **buffer, uint32_t *len)
339 {
340 uint32_t bufferLen;
341 uint16_t sysCaptype, sysCapLength;
342 char *contextBuffer = NULL;
343 RPCIDHead *rpcidHeader = NULL;
344
345 if (GetFileContext(inputFile, &contextBuffer, &bufferLen)) {
346 PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
347 return -1;
348 }
349 if (bufferLen < (2 * sizeof(uint32_t))) { // 2, header of rpcid.sc
350 PRINT_ERR("Parse file failed(format is invalid), input file : %s\n", inputFile);
351 return -1;
352 }
353 rpcidHeader = (RPCIDHead *)contextBuffer;
354 if (rpcidHeader->apiVersionType != 1) {
355 PRINT_ERR("Parse file failed(apiVersionType != 1), input file : %s\n", inputFile);
356 return -1;
357 }
358 sysCaptype = NtohsInter(*(uint16_t *)(rpcidHeader + 1));
359 if (sysCaptype != 2) { // 2, app syscap type
360 PRINT_ERR("Parse file failed(sysCaptype != 2), input file : %s\n", inputFile);
361 return -1;
362 }
363 sysCapLength = NtohsInter(*(uint16_t *)((char *)(rpcidHeader + 1) + sizeof(uint16_t)));
364 if (bufferLen < sizeof(RPCIDHead) + sizeof(uint32_t) + sysCapLength) {
365 PRINT_ERR("Parse file failed(SysCap length exceeded), input file : %s\n", inputFile);
366 return -1;
367 }
368
369 *buffer = contextBuffer;
370 *len = bufferLen;
371 return 0;
372 }
373
RPCIDDecode(char * inputFile,char * outputPath)374 int32_t RPCIDDecode(char *inputFile, char *outputPath)
375 {
376 int32_t ret = 0;
377 char *contextBuffer = NULL;
378 char *convertedBuffer = NULL;
379 uint32_t bufferLen;
380
381 // check rpcid.sc
382 if (CheckRpcidFormat(inputFile, &contextBuffer, &bufferLen)) {
383 PRINT_ERR("Check rpcid.sc format failed. Input failed: %s\n", inputFile);
384 goto FREE_CONTEXT_OUT;
385 }
386
387 // parse rpcid to json
388 cJSON *rpcidRoot = cJSON_CreateObject();
389 if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot) != 0) {
390 PRINT_ERR("Prase rpcid to json failed. Input failed: %s\n", inputFile);
391 goto FREE_RPCID_ROOT;
392 }
393
394 // save to json file
395 convertedBuffer = cJSON_Print(rpcidRoot);
396 ret = ConvertedContextSaveAsFile(outputPath, "RPCID.json", convertedBuffer, strlen(convertedBuffer));
397 if (ret != 0) {
398 PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.json\n", outputPath);
399 goto FREE_RPCID_ROOT;
400 }
401
402 FREE_RPCID_ROOT:
403 cJSON_Delete(rpcidRoot);
404 FREE_CONTEXT_OUT:
405 FreeContextBuffer(contextBuffer);
406 return ret;
407 }
408
SetOsSysCapBitMap(uint8_t * out,uint16_t outLen,const uint16_t * index,uint16_t indexLen)409 static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, const uint16_t *index, uint16_t indexLen)
410 {
411 uint16_t sector, pos;
412
413 if (outLen != BYTES_OF_OS_SYSCAP) {
414 PRINT_ERR("Input array error.\n");
415 return -1;
416 }
417
418 for (uint16_t i = 0; i < indexLen; i++) {
419 sector = index[i] / UINT8_BIT;
420 pos = index[i] % UINT8_BIT;
421 if (sector >= BYTES_OF_OS_SYSCAP) {
422 PRINT_ERR("Syscap num(%u) out of range(120).\n", sector);
423 return -1;
424 }
425 out[sector] |= (1 << pos);
426 }
427 return 0;
428 }
429
EncodeRpcidscToString(char * inputFile,char * outDirPath)430 int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath)
431 {
432 int32_t ret = 0;
433 int32_t sysCapArraySize;
434 uint32_t bufferLen;
435 uint16_t indexPri = 0;
436 uint16_t *osSysCapIndex;
437 char *contextBuffer = NULL;
438 char *priSyscapArray = NULL;
439 char *priSyscap = NULL;
440 cJSON *cJsonTemp = NULL;
441 cJSON *rpcidRoot = NULL;
442 cJSON *sysCapDefine = NULL;
443 cJSON *sysCapArray = NULL;
444
445 // check rpcid.sc
446 if (CheckRpcidFormat(inputFile, &contextBuffer, &bufferLen) != 0) {
447 PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile);
448 goto FREE_CONTEXT_OUT;
449 }
450
451 // parse rpcid to json
452 rpcidRoot = cJSON_CreateObject();
453 if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot) != 0) {
454 PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile);
455 goto FREE_RPCID_ROOT;
456 }
457
458 // trans to string format
459 sysCapDefine = CreateWholeSyscapJsonObj();
460 sysCapArray = cJSON_GetObjectItem(rpcidRoot, "syscap");
461 if (sysCapArray == NULL || !cJSON_IsArray(sysCapArray)) {
462 PRINT_ERR("Get syscap failed. Input file: %s\n", inputFile);
463 goto FREE_WHOLE_SYSCAP;
464 }
465 sysCapArraySize = cJSON_GetArraySize(sysCapArray);
466 if (sysCapArraySize < 0) {
467 PRINT_ERR("Get syscap size failed. Input file: %s\n", inputFile);
468 goto FREE_WHOLE_SYSCAP;
469 }
470 // malloc for save os syscap index
471 osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t) * sysCapArraySize);
472 if (osSysCapIndex == NULL) {
473 PRINT_ERR("malloc failed.\n");
474 goto FREE_WHOLE_SYSCAP;
475 }
476 (void)memset_s(osSysCapIndex, sizeof(uint16_t) * sysCapArraySize,
477 0, sizeof(uint16_t) * sysCapArraySize);
478 // malloc for save private syscap string
479 priSyscapArray = (char *)malloc((uint32_t)sysCapArraySize * SINGLE_SYSCAP_LEN);
480 if (priSyscapArray == NULL) {
481 PRINT_ERR("malloc(%d) failed.\n", sysCapArraySize * SINGLE_SYSCAP_LEN);
482 goto FREE_MALLOC_OSSYSCAP;
483 }
484 (void)memset_s(priSyscapArray, (size_t)(sysCapArraySize * SINGLE_SYSCAP_LEN),
485 0, (size_t)(sysCapArraySize * SINGLE_SYSCAP_LEN));
486 priSyscap = priSyscapArray;
487 // part os syscap and ptivate syscap
488 uint16_t indexOs = 0;
489 for (int i = 0; i < sysCapArraySize; i++) {
490 cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i);
491 cJsonTemp = cJSON_GetObjectItem(sysCapDefine, cJsonItem->valuestring);
492 if (cJsonTemp != NULL) {
493 osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint);
494 } else {
495 ret = strcpy_s(priSyscap, SINGLE_SYSCAP_LEN, cJsonItem->valuestring);
496 if (ret != EOK) {
497 PRINT_ERR("strcpy_s failed.\n");
498 goto FREE_MALLOC_PRISYSCAP;
499 }
500 priSyscap += SINGLE_SYSCAP_LEN;
501 indexPri++;
502 }
503 }
504 uint32_t outUint[RPCID_OUT_BUFFER] = {0};
505 outUint[0] = *(uint32_t *)contextBuffer;
506 outUint[1] = *(uint32_t *)(contextBuffer + sizeof(uint32_t));
507 uint8_t *osOutUint = (uint8_t *)(outUint + 2);
508 if (SetOsSysCapBitMap(osOutUint, 120, osSysCapIndex, indexOs) != 0) { // 120, len of osOutUint
509 PRINT_ERR("Set os syscap bit map failed.\n");
510 goto FREE_MALLOC_PRISYSCAP;
511 }
512
513 uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER
514 + SINGLE_SYSCAP_LEN * indexPri;
515 char *outBuffer = (char *)malloc(outBufferLen);
516 if (outBuffer == NULL) {
517 PRINT_ERR("malloc(%u) failed.\n", outBufferLen);
518 goto FREE_MALLOC_PRISYSCAP;
519 }
520 (void)memset_s(outBuffer, outBufferLen, 0, outBufferLen);
521
522 ret = sprintf_s(outBuffer, outBufferLen, "%u", outUint[0]);
523 if (ret == -1) {
524 PRINT_ERR("sprintf_s failed.\n");
525 goto FREE_OUTBUFFER;
526 }
527 for (int i = 1; i < RPCID_OUT_BUFFER; i++) {
528 ret = sprintf_s(outBuffer, outBufferLen, "%s,%u", outBuffer, outUint[i]);
529 if (ret == -1) {
530 PRINT_ERR("sprintf_s failed.\n");
531 goto FREE_OUTBUFFER;
532 }
533 }
534
535 for (uint16_t i = 0; i < indexPri; i++) {
536 ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer,
537 priSyscapArray + i * SINGLE_SYSCAP_LEN);
538 if (ret == -1) {
539 PRINT_ERR("sprintf_s failed.\n");
540 goto FREE_OUTBUFFER;
541 }
542 }
543
544 const char outputFilename[] = "RPCID.txt";
545 ret = ConvertedContextSaveAsFile(outDirPath, outputFilename, outBuffer, strlen(outBuffer));
546 if (ret != 0) {
547 PRINT_ERR("Save to txt file failed. Output path:%s/%s\n", outDirPath, outputFilename);
548 goto FREE_OUTBUFFER;
549 }
550
551 FREE_OUTBUFFER:
552 free(outBuffer);
553 FREE_MALLOC_PRISYSCAP:
554 free(priSyscapArray);
555 FREE_MALLOC_OSSYSCAP:
556 free(osSysCapIndex);
557 FREE_WHOLE_SYSCAP:
558 cJSON_Delete(sysCapDefine);
559 FREE_RPCID_ROOT:
560 cJSON_Delete(rpcidRoot);
561 FREE_CONTEXT_OUT:
562 FreeContextBuffer(contextBuffer);
563 return ret;
564 }
565
CopyInputString(const char * inputString)566 char *CopyInputString(const char *inputString)
567 {
568 if (inputString == NULL || *inputString == '\0') {
569 PRINT_ERR("inputString is null or empty.\n");
570 return NULL;
571 }
572 size_t inputLen = strlen(inputString);
573 if (inputLen > STRING_FORMAT_LEN_MAX) {
574 PRINT_ERR("input string too long(%zu).\n", inputLen);
575 return NULL;
576 }
577 char *input = (char *)malloc(inputLen + 1);
578 if (input == NULL) {
579 PRINT_ERR("malloc failed.\n");
580 return NULL;
581 }
582 int32_t ret = strcpy_s(input, inputLen + 1, inputString);
583 if (ret != EOK) {
584 PRINT_ERR("strcpy_s failed.\n");
585 free(input);
586 return NULL;
587 }
588 input[inputLen] = '\0';
589 return input;
590 }
591
GetPriSyscapData(char * input,char ** priSyscap,uint32_t * priSyscapLen)592 int32_t GetPriSyscapData(char *input, char **priSyscap, uint32_t *priSyscapLen)
593 {
594 // count private syscaps
595 uint32_t count = 0;
596 for (uint32_t i = 0; *(input + i) != '\0'; i++) {
597 if (*(input + i) == ',') {
598 count++;
599 }
600 }
601 count++;
602 // get private syscap string
603 char *priSysCapOut = (char *)malloc(SINGLE_SYSCAP_LEN * count);
604 if (priSysCapOut == NULL) {
605 PRINT_ERR("sscanf_s failed.\n");
606 return -1;
607 }
608 (void)memset_s(priSysCapOut, SINGLE_SYSCAP_LEN * count, 0, SINGLE_SYSCAP_LEN * count);
609 char *private = priSysCapOut;
610
611 char *tok = NULL;
612 char *temp = strtok_r(input, ",", &tok);
613 while (temp) {
614 int ret = strncpy_s(private, SINGLE_SYSCAP_LEN,
615 temp, SINGLE_SYSCAP_LEN - 1);
616 if (ret != EOK) {
617 PRINT_ERR("strncpy_s failed.\n");
618 free(priSysCapOut);
619 return -1;
620 }
621 temp = strtok_r(NULL, ",", &tok);
622 private += SINGLE_SYSCAP_LEN;
623 }
624
625 *priSyscap = priSysCapOut;
626 *priSyscapLen = count;
627 return 0;
628 }
629
SeparateSyscapFromString(const char * inputString,uint32_t * osArray,uint32_t osArraySize,char ** priSyscap,uint32_t * priSyscapLen)630 int32_t SeparateSyscapFromString(const char *inputString, uint32_t *osArray, uint32_t osArraySize,
631 char **priSyscap, uint32_t *priSyscapLen)
632 {
633 if (osArraySize != PCID_OUT_BUFFER) {
634 return -1;
635 }
636
637 // copy origin string
638 char *input = CopyInputString(inputString);
639 if (input == NULL) {
640 return -1;
641 }
642
643 // get os syscap data
644 for (uint32_t i = 0; i < PCID_OUT_BUFFER; i++) {
645 if (sscanf_s(input, "%u,%s", &osArray[i], input, strlen(input)) == -1) {
646 PRINT_ERR("sscanf_s failed.\n");
647 free(input);
648 return -1;
649 }
650 }
651
652 // get private syscap data
653 if (GetPriSyscapData(input, priSyscap, priSyscapLen) != 0) {
654 free(input);
655 return -1;
656 }
657
658 free(input);
659 return 0;
660 }
661
GetSyscapByIndex(uint32_t index)662 int32_t GetSyscapByIndex(uint32_t index)
663 {
664 const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
665 for (uint32_t i = 0; i < allSyscapNum; i++) {
666 if (g_arraySyscap[i].num == index) {
667 return i;
668 }
669 }
670 return -1;
671 }
672
CompareOsSyscap(const uint32_t pcidOsArray[],const uint32_t rpcidOsAarry[])673 int32_t CompareOsSyscap(const uint32_t pcidOsArray[], const uint32_t rpcidOsAarry[])
674 {
675 int32_t ossyscapFlag = 0;
676 const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
677
678 for (uint32_t i = 2; i < PCID_OUT_BUFFER; i++) { // 2, header of pcid & rpcid
679 uint32_t blockBits = (pcidOsArray[i] ^ rpcidOsAarry[i]) & rpcidOsAarry[i];
680 if (!blockBits) {
681 continue;
682 }
683 for (uint8_t k = 0; k < INT_BIT; k++) {
684 if (!(blockBits & (1U << k))) {
685 continue;
686 }
687 // 2, header of pcid & rpcid
688 size_t pos = (size_t)((i - 2) * INT_BIT + k);
689 if (pos >= allSyscapNum) {
690 break;
691 }
692 printf("Missing: %s\n", g_arraySyscap[GetSyscapByIndex(pos)].str);
693 ossyscapFlag += 1;
694 }
695 }
696 return ossyscapFlag;
697 }
698
ComparePriSyscap(char * pcid,char * rpcid,uint32_t pcidLen,uint32_t rpcidLen)699 int32_t ComparePriSyscap(char *pcid, char *rpcid, uint32_t pcidLen, uint32_t rpcidLen)
700 {
701 uint32_t i, j;
702 bool priSysFound = false;
703 int32_t prisyscapFlag = 0;
704
705 for (i = 0; i < rpcidLen; i++) {
706 for (j = 0; j < pcidLen; j++) {
707 if (strcmp(rpcid + SINGLE_SYSCAP_LEN * i,
708 pcid + SINGLE_SYSCAP_LEN * j) == 0) {
709 priSysFound = true;
710 break;
711 }
712 }
713 if (priSysFound != true) {
714 printf("Missing: %s\n", rpcid + SINGLE_SYSCAP_LEN * i);
715 prisyscapFlag += 1;
716 }
717 priSysFound = false;
718 }
719
720 return prisyscapFlag;
721 }
722
CompoareVersion(uint32_t * pcidOsArray,uint32_t * rpcidOsAarry)723 int32_t CompoareVersion(uint32_t *pcidOsArray, uint32_t *rpcidOsAarry)
724 {
725 int32_t versionFlag = 0;
726 uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsArray)->apiVersion);
727 uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion);
728 if (pcidVersion < rpcidVersion) {
729 printf("ERROR: Pcid version(%u) less than rpcid version(%u).\n", pcidVersion, rpcidVersion);
730 versionFlag = 1;
731 }
732 return versionFlag;
733 }
734
ComparePcidWithRpcidString(char * pcidFile,char * rpcidFile,uint32_t type)735 int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t type)
736 {
737 int32_t ret;
738 char *pcidContent = NULL;
739 char *rpcidContent = NULL;
740 char *pcidPriSyscap = NULL;
741 char *rpcidPriSyscap = NULL;
742 uint32_t pcidContentLen, rpcidContentLen, pcidPriSyscapLen, rpcidPriSyscapLen;
743 uint32_t pcidOsArray[PCID_OUT_BUFFER] = {0};
744 uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0};
745
746 if (type == TYPE_FILE) {
747 if (GetFileContext(pcidFile, &pcidContent, &pcidContentLen)) {
748 PRINT_ERR("Get pcid file context failed, input file : %s\n", pcidFile);
749 return -1;
750 }
751 if (GetFileContext(rpcidFile, &rpcidContent, &rpcidContentLen)) {
752 PRINT_ERR("Get rpcid file context failed, input file : %s\n", rpcidFile);
753 free(pcidContent);
754 return -1;
755 }
756 } else if (type == TYPE_STRING) {
757 pcidContent = pcidFile;
758 rpcidContent = rpcidFile;
759 } else {
760 PRINT_ERR("Input file type error, type=%u\n", type);
761 return -1;
762 }
763
764 ret = SeparateSyscapFromString(pcidContent, pcidOsArray, PCID_OUT_BUFFER,
765 &pcidPriSyscap, &pcidPriSyscapLen);
766 ret += SeparateSyscapFromString(rpcidContent, rpcidOsAarry, RPCID_OUT_BUFFER,
767 &rpcidPriSyscap, &rpcidPriSyscapLen);
768 if (ret != 0) {
769 PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret);
770 return -1;
771 }
772
773 int32_t versionFlag = CompoareVersion(pcidOsArray, rpcidOsAarry);
774 int32_t ossyscapFlag = CompareOsSyscap(pcidOsArray, rpcidOsAarry);
775 int32_t prisyscapFlag = ComparePriSyscap(pcidPriSyscap, rpcidPriSyscap, pcidPriSyscapLen, rpcidPriSyscapLen);
776 if (!versionFlag && !ossyscapFlag && !prisyscapFlag) {
777 printf("Succeed! The pcid meets the rpcid.\n");
778 } else {
779 printf("Fail! The pcid does not meet the rpcid\n");
780 }
781 return 0;
782 }
783