• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <stdio.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <limits.h>
22 #include <sys/stat.h>
23 #include "securec.h"
24 #include "cJSON.h"
25 #include "endian_internal.h"
26 #include "create_pcid.h"
27 #include "context_tool.h"
28 #include "common_method.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 SYSCAP_PREFIX_LEN 17
37 #define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN)
38 #define PCID_OUT_BUFFER 32
39 #define PRIVATE_SYSCAP_SIZE 1000
40 #define UINT8_BIT 8
41 
42 #define U32_TO_STR_MAX_LEN 11
43 #define FREE_CREATE_PCID_BUFFER_OUT 1
44 
45 #define FREE_DECODE_PCID_CONVERT_OUT 1
46 #define FREE_DECODE_PCID_ROOT_OUT 2
47 #define FREE_DECODE_PCID_SYSCAP_OUT 3
48 #define FREE_DECODE_PCID_CONTEXT_OUT 4
49 
50 #define ENCODE_PCID_OUTPUT 1
51 #define FREE_ENCODE_PCID_OUT 2
52 #define FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT 3
53 #define FREE_ENCODE_PCID_CONTEXT_OUT 4
54 
55 struct FreeEncodePcidInfo {
56     char *output;
57     char *priSyscapFull;
58     char *contextBuffer;
59     char *outDirPathFinal;
60 };
61 
62 struct FreeDecodePcidJsonInfo {
63     char *strJson;
64     char *contextBuffer;
65     cJSON *jsonRootObj;
66     cJSON *sysCapObj;
67     int32_t flag;
68 };
69 
SetOsSyscap(PCIDMain * pcidBuffer,uint32_t osCapSize,const cJSON * jsonOsSyscapObj,const cJSON * allOsSyscapObj)70 int32_t SetOsSyscap(PCIDMain *pcidBuffer, uint32_t osCapSize,
71                     const cJSON *jsonOsSyscapObj, const cJSON *allOsSyscapObj)
72 {
73     int32_t sectorOfBits, posOfBits;
74     cJSON *jsonArrayItem = NULL;
75     cJSON *osCapIndex = NULL;
76 
77     for (uint32_t i = 0; i < osCapSize; i++) {
78         jsonArrayItem = cJSON_GetArrayItem(jsonOsSyscapObj, (int)i);
79         if (jsonArrayItem == NULL || !cJSON_IsString(jsonArrayItem)) {
80             PRINT_ERR("Get jsonArrayItem failed.");
81             return -1;
82         }
83         osCapIndex = cJSON_GetObjectItem(allOsSyscapObj, jsonArrayItem->valuestring);
84         if (osCapIndex == NULL) {
85             PRINT_ERR("can't find the syscap: %s, please add it in syscap_define.h.\n", jsonArrayItem->valuestring);
86             return -1;
87         }
88         if (!cJSON_IsNumber(osCapIndex)) {
89             PRINT_ERR("Get osCapIndex failed.");
90             return -1;
91         }
92         sectorOfBits = (osCapIndex->valueint) / UINT8_BIT;
93         posOfBits = (osCapIndex->valueint) % UINT8_BIT;
94         if (sectorOfBits >= OS_SYSCAP_BYTES) {
95             PRINT_ERR("num of \"os syscap\" is out of 960\n");
96             return -1;
97         }
98         pcidBuffer->osSyscap[sectorOfBits] |= 1 << (posOfBits);
99     }
100 
101     return 0;
102 }
103 
SetPriSyscap(PCIDMain * pcidBuffer,cJSON * jsonPriSyscapObj,uint32_t privateCapSize,uint16_t allPriSyscapStrLen)104 int32_t SetPriSyscap(PCIDMain *pcidBuffer, cJSON *jsonPriSyscapObj,
105                      uint32_t privateCapSize, uint16_t allPriSyscapStrLen)
106 {
107     char *priSyscapHead = (char *)(pcidBuffer + 1);
108     char *priSyscapStr = NULL;
109     for (uint32_t i = 0; i < privateCapSize; i++) {
110         cJSON *jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, (int)i);
111         if (jsonArrayItem == NULL || !cJSON_IsString(jsonArrayItem)) {
112             PRINT_ERR("get jsonArrayItem failed!");
113             return -1;
114         }
115         priSyscapStr = strchr(jsonArrayItem->valuestring, '.');
116         if (priSyscapStr == NULL) {
117             PRINT_ERR("get priSyscapStr failed!");
118             return -1;
119         }
120         priSyscapStr += 1;
121         errno_t nRet = strcat_s(priSyscapHead, allPriSyscapStrLen + 1, priSyscapStr);
122         nRet += strcat_s(priSyscapHead, allPriSyscapStrLen + 1, ",");
123         if (nRet != EOK) {
124             PRINT_ERR("strcat_s \"pri\" string is failed\n");
125             return -1;
126         }
127     }
128     return 0;
129 }
130 
SetPCIDHeader(PCIDMain * pcidBuffer,const cJSON * jsonRootObj)131 int32_t SetPCIDHeader(PCIDMain *pcidBuffer, const cJSON *jsonRootObj)
132 {
133     cJSON *jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "api_version");
134     if (jsonSyscapObj == NULL || !cJSON_IsNumber(jsonSyscapObj)) {
135         PRINT_ERR("get \"api_version\" failed\n");
136         return -1;
137     }
138     pcidBuffer->apiVersion = HtonsInter((uint16_t)jsonSyscapObj->valueint);
139     pcidBuffer->apiVersionType = 0;
140 
141     jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "system_type");
142     if (jsonSyscapObj == NULL || !cJSON_IsString(jsonSyscapObj)) {
143         PRINT_ERR("get \"system_type\" failed\n");
144         return -1;
145     }
146     char *systemType = jsonSyscapObj->valuestring;
147     pcidBuffer->systemType = !strcmp(systemType, "mini") ? 0b001 :
148                              (!strcmp(systemType, "small") ? 0b010 :
149                               (!strcmp(systemType, "standard") ? 0b100 : 0));
150     if (pcidBuffer->systemType == 0) {
151         PRINT_ERR("\"system_type\" is invaild, systemType = \"%s\"\n", systemType);
152         return -1;
153     }
154 
155     jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "manufacturer_id");
156     if (jsonSyscapObj == NULL || !cJSON_IsNumber(jsonSyscapObj)) {
157         PRINT_ERR("get \"manufacturer_id\" failed\n");
158         return -1;
159     }
160     pcidBuffer->manufacturerID = HtonlInter((uint32_t)jsonSyscapObj->valueint);
161 
162     return 0;
163 }
164 
GetOsAndPriSyscapSize(const cJSON * osSyscap,const cJSON * priSyscap,uint32_t * osCapSize,uint32_t * privateCapSize)165 int32_t GetOsAndPriSyscapSize(const cJSON *osSyscap, const cJSON *priSyscap,
166                               uint32_t *osCapSize, uint32_t *privateCapSize)
167 {
168     *osCapSize = 0;
169     *privateCapSize = 0;
170 
171     // get os syscap size
172     if (osSyscap == NULL || !cJSON_IsArray(osSyscap)) {
173         PRINT_ERR("get \"os\" array failed\n");
174         return -1;
175     }
176     int32_t ret = cJSON_GetArraySize(osSyscap);
177     if (ret < 0) {
178         PRINT_ERR("get \"os\" array size failed\n");
179         return -1;
180     }
181     *osCapSize = (uint32_t)ret;
182 
183     // get private syscap size
184     if (priSyscap != NULL && cJSON_IsArray(priSyscap)) {
185         ret = cJSON_GetArraySize(priSyscap);
186         if (ret < 0) {
187             PRINT_ERR("get \"private syscap\" array size failed\n");
188             return -1;
189         }
190         *privateCapSize = (uint32_t)ret;
191     } else if (priSyscap == NULL) {
192         *privateCapSize = 0;
193     } else {
194         PRINT_ERR("get \"private\" array failed\n");
195         return -1;
196     }
197 
198     return 0;
199 }
200 
GetPriSyscapLen(uint32_t privateCapSize,cJSON * jsonPriSyscapObj,uint16_t * len)201 int32_t GetPriSyscapLen(uint32_t privateCapSize, cJSON *jsonPriSyscapObj, uint16_t *len)
202 {
203     for (uint32_t i = 0; i < privateCapSize; i++) {
204         cJSON *jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, (int)i);
205         if (jsonArrayItem == NULL || !cJSON_IsString(jsonArrayItem)) {
206             PRINT_ERR("Get jsonArrayItem failed.");
207             return -1;
208         }
209         *len += (uint16_t)strlen(strchr(jsonArrayItem->valuestring, '.') + 1);
210         (*len)++; // for separator ','
211     }
212     if ((*len + 1) > PRIVATE_SYSCAP_SIZE) {
213         PRINT_ERR("context of \"pri\" array is too many.\n");
214         return -1;
215     }
216     return 0;
217 }
218 
CheckConvertedContextSaveAsFile(char * outDirPath,PCIDMain * pcidBuffer,uint16_t pcidLength,int32_t ret)219 static int32_t CheckConvertedContextSaveAsFile(char *outDirPath, PCIDMain *pcidBuffer, uint16_t pcidLength, int32_t ret)
220 {
221     const char pcidFileName[] = "pcid.sc";
222     ret = ConvertedContextSaveAsFile(outDirPath, pcidFileName, (char *)pcidBuffer, pcidLength);
223     if (ret != 0) {
224         PRINT_ERR("Save as file failed, outDirPath:%s, filename:%s\n", outDirPath, pcidFileName);
225     }
226     return ret;
227 }
228 
FreeAfterCreatePCID(PCIDMain * pcidBuffer,cJSON * allOsSyscapObj,char * contextBuffer,int32_t type,int32_t ret)229 static int32_t FreeAfterCreatePCID(PCIDMain *pcidBuffer, cJSON *allOsSyscapObj, char *contextBuffer,
230     int32_t type, int32_t ret)
231 {
232     if (type == FREE_CREATE_PCID_BUFFER_OUT) {
233         free(pcidBuffer);
234     }
235     cJSON_Delete(allOsSyscapObj);
236     FreeContextBuffer(contextBuffer);
237     return ret;
238 }
239 
PreFreeAfterCreatePCID(PCIDMain * pcidBuffer,cJSON * allOsSyscapObj,cJSON * jsonRootObj,char * contextBuffer,int32_t type)240 static int32_t PreFreeAfterCreatePCID(PCIDMain *pcidBuffer, cJSON *allOsSyscapObj, cJSON *jsonRootObj,
241     char *contextBuffer, int32_t type)
242 {
243     cJSON_Delete(jsonRootObj);
244     return FreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, contextBuffer, type, -1);
245 }
246 
CreatePCID(char * inputFile,char * outDirPath)247 int32_t CreatePCID(char *inputFile, char *outDirPath)
248 {
249     uint32_t privateCapSize, osCapSize;
250     uint32_t contextBufLen;
251     char *contextBuffer = NULL;
252 
253     cJSON *allOsSyscapObj = CreateWholeSyscapJsonObj();
254     int32_t ret = CheckFileAndGetFileContext(inputFile, &contextBuffer, (uint32_t *)&contextBufLen);
255     if (ret != 0) {
256         return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, -1);
257     }
258 
259     cJSON *jsonRootObj = cJSON_ParseWithLength(contextBuffer, contextBufLen);
260     if (jsonRootObj == NULL) {
261         PRINT_ERR("cJSON_Parse failed, context buffer is:\n%s\n", contextBuffer);
262         return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0);
263     }
264 
265     cJSON *jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "syscap");
266     if (jsonSyscapObj == NULL || !cJSON_IsObject(jsonSyscapObj)) {
267         PRINT_ERR("get \"syscap\" object failed\n");
268         return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0);
269     }
270 
271     cJSON *jsonOsSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "os");
272     cJSON *jsonPriSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "private");
273 
274     ret = GetOsAndPriSyscapSize(jsonOsSyscapObj, jsonPriSyscapObj, &osCapSize, &privateCapSize);
275     if (ret != 0) {
276         return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0);
277     }
278 
279     uint16_t allPriSyscapStrLen = 0;
280     ret = GetPriSyscapLen(privateCapSize, jsonPriSyscapObj, &allPriSyscapStrLen);
281     if (ret != 0) {
282         return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0);
283     }
284 
285     uint16_t pcidLength = sizeof(PCIDMain) + allPriSyscapStrLen + 1;
286     PCIDMain *pcidBuffer = (PCIDMain *)malloc(pcidLength);
287     if (pcidBuffer == NULL) {
288         PRINT_ERR("malloc for pcid buffer failed\n");
289         return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0);
290     }
291     (void)memset_s(pcidBuffer, pcidLength, 0, pcidLength);
292 
293     ret = SetOsSyscap(pcidBuffer, osCapSize, jsonOsSyscapObj, allOsSyscapObj);
294     ret += SetPriSyscap(pcidBuffer, jsonPriSyscapObj, privateCapSize, allPriSyscapStrLen);
295     ret += SetPCIDHeader(pcidBuffer, jsonRootObj);
296     if (ret != 0) {
297         return PreFreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, jsonRootObj, contextBuffer,
298             FREE_CREATE_PCID_BUFFER_OUT);
299     }
300 
301     ret = CheckConvertedContextSaveAsFile(outDirPath, pcidBuffer, pcidLength, ret);
302     cJSON_Delete(jsonRootObj);
303     return FreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, contextBuffer, FREE_CREATE_PCID_BUFFER_OUT, ret);
304 }
305 
GetOsSyscap(PCIDMain * pcidMain,cJSON * sysCapObject)306 int32_t GetOsSyscap(PCIDMain *pcidMain, cJSON *sysCapObject)
307 {
308     uint32_t i, j, countOfSyscap = 0;
309     uint8_t osSyscap[OS_SYSCAP_BYTES] = {0};
310     uint16_t indexOfSyscap[OS_SYSCAP_BYTES * UINT8_BIT] = {0};
311 
312     cJSON *capVectorPtr = cJSON_CreateArray();
313     if (capVectorPtr == NULL) {
314         PRINT_ERR("cJSON_CreateArray failed\n");
315         return -1;
316     }
317 
318     // 8, bytes of pcid header
319     errno_t nRet = memcpy_s(osSyscap, OS_SYSCAP_BYTES, (uint8_t *)pcidMain + 8, OS_SYSCAP_BYTES);
320     if (nRet != EOK) {
321         PRINT_ERR("memcpy_s failed.");
322         cJSON_Delete(capVectorPtr);
323         return -1;
324     }
325 
326     for (i = 0; i < OS_SYSCAP_BYTES; i++) {
327         for (j = 0; j < UINT8_BIT; j++) {
328             if (osSyscap[i] & (0x01 << j)) {
329                 indexOfSyscap[countOfSyscap++] = i * UINT8_BIT + j;
330             }
331         }
332     }
333     for (i = 0; i < countOfSyscap; i++) {
334         for (j = 0; j < sizeof(g_arraySyscap) / sizeof(SyscapWithNum); j++) {
335             if (g_arraySyscap[j].num != indexOfSyscap[i]) {
336                 continue;
337             }
338             if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(g_arraySyscap[j].str))) {
339                 printf("cJSON_AddItemToArray or cJSON_CreateString failed\n");
340                 cJSON_Delete(capVectorPtr);
341                 return -1;
342             }
343         }
344     }
345 
346     if (!cJSON_AddItemToObject(sysCapObject, "os", capVectorPtr)) {
347         PRINT_ERR("cJSON_AddItemToObject failed\n");
348         cJSON_Delete(capVectorPtr);
349         return -1;
350     }
351 
352     return 0;
353 }
354 
GetPriSyscapResult(cJSON * capVectorPtr,int32_t ret)355 static int32_t GetPriSyscapResult(cJSON *capVectorPtr, int32_t ret)
356 {
357     cJSON_Delete(capVectorPtr);
358     return ret;
359 }
360 
GetPriSyscap(PCIDMain * pcidMain,cJSON * sysCapObject,int32_t contextBufLen)361 static int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, int32_t contextBufLen)
362 {
363     cJSON *capVectorPtr = cJSON_CreateArray();
364     if (capVectorPtr == NULL) {
365         PRINT_ERR("cJSON_CreateArray failed\n");
366         return -1;
367     }
368 
369     int32_t privateSyscapLen = contextBufLen - sizeof(PCIDMain) - 1;
370     if (privateSyscapLen < 0 || privateSyscapLen > INT32_MAX) {
371         PRINT_ERR("parse private syscap failed.");
372         return GetPriSyscapResult(capVectorPtr, -1);
373     } else if (privateSyscapLen == 0) {
374         return GetPriSyscapResult(capVectorPtr, 0);
375     }
376 
377     char fullCapStr[SINGLE_SYSCAP_LEN] = {0};
378     char priSyscapStr[SINGLE_SYSCAP_LEN] = {0};
379     char *tempPriSyscapStr = priSyscapStr;
380     char *ptrPrivateSyscap = (char *)(pcidMain + 1);
381     while (*ptrPrivateSyscap != '\0') {
382         size_t currentLen = tempPriSyscapStr - priSyscapStr;
383         if (currentLen >= SINGLE_SYSCAP_LEN - 1) {
384             printf("Syscap name exceeds maximum length\n");
385             return GetPriSyscapResult(capVectorPtr, -1);
386         }
387         if (*ptrPrivateSyscap == ',') {
388             *tempPriSyscapStr = '\0';
389             int32_t ret = sprintf_s(fullCapStr, SINGLE_SYSCAP_LEN, "SystemCapability.%s", priSyscapStr);
390             if (ret == -1) {
391                 printf("sprintf_s failed\n");
392                 return GetPriSyscapResult(capVectorPtr, -1);
393             }
394             if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(fullCapStr))) {
395                 printf("cJSON_AddItemToArray or cJSON_CreateString failed\n");
396                 return GetPriSyscapResult(capVectorPtr, -1);
397             }
398             tempPriSyscapStr = priSyscapStr;
399             ptrPrivateSyscap++;
400             continue;
401         }
402         *tempPriSyscapStr++ = *ptrPrivateSyscap++;
403     }
404     if (!cJSON_AddItemToObject(sysCapObject, "private", capVectorPtr)) {
405         PRINT_ERR("cJSON_AddItemToObject failed\n");
406         return GetPriSyscapResult(capVectorPtr, -1);
407     }
408     return 0;
409 }
410 
CheckSysCapObj(struct FreeDecodePcidJsonInfo freePcidJsonInfo,PCIDMain * pcidMain,uint32_t contextBufLen,int32_t ret)411 static int32_t CheckSysCapObj(struct FreeDecodePcidJsonInfo freePcidJsonInfo, PCIDMain *pcidMain,
412         uint32_t contextBufLen, int32_t ret)
413 {
414     if (freePcidJsonInfo.sysCapObj == NULL) {
415         PRINT_ERR("cJSON_CreateObject failed\n");
416         freePcidJsonInfo.flag = -1;
417         return -1;
418     }
419     if (GetOsSyscap(pcidMain, freePcidJsonInfo.sysCapObj) != 0) {
420         freePcidJsonInfo.flag = -1;
421         return ret;
422     }
423     if (GetPriSyscap(pcidMain, freePcidJsonInfo.sysCapObj, contextBufLen) != 0) {
424         freePcidJsonInfo.flag = -1;
425     }
426     return ret;
427 }
428 
CheckJsonRootObj(struct FreeDecodePcidJsonInfo freePcidJsonInfo,PCIDMain * pcidMain,char * systemType)429 static int32_t CheckJsonRootObj(struct FreeDecodePcidJsonInfo freePcidJsonInfo, PCIDMain *pcidMain, char *systemType)
430 {
431     if (!cJSON_AddNumberToObject(freePcidJsonInfo.jsonRootObj, "api_version", NtohsInter(pcidMain->apiVersion))) {
432         PRINT_ERR("cJSON_AddNumberToObject failed\n");
433         return -1;
434     }
435     if (!cJSON_AddNumberToObject(freePcidJsonInfo.jsonRootObj, "manufacturer_id",
436                                  NtohlInter(pcidMain->manufacturerID))) {
437         PRINT_ERR("cJSON_AddNumberToObject failed\n");
438         return -1;
439     }
440     if (!cJSON_AddStringToObject(freePcidJsonInfo.jsonRootObj, "system_type", systemType)) {
441         PRINT_ERR("cJSON_AddStringToObject failed\n");
442         return -1;
443     }
444     if (!cJSON_AddItemToObject(freePcidJsonInfo.jsonRootObj, "syscap", freePcidJsonInfo.sysCapObj)) {
445         PRINT_ERR("cJSON_AddItemToObject failed\n");
446         return -1;
447     }
448     return 0;
449 }
450 
FreeAfterDecodePCID(struct FreeDecodePcidJsonInfo freePcidJsonInfo,int32_t type,int32_t ret)451 static int32_t FreeAfterDecodePCID(struct FreeDecodePcidJsonInfo freePcidJsonInfo, int32_t type, int32_t ret)
452 {
453     switch (type) {
454         case FREE_DECODE_PCID_CONVERT_OUT:
455             cJSON_free(freePcidJsonInfo.strJson);
456             cJSON_Delete(freePcidJsonInfo.jsonRootObj);
457             FreeContextBuffer(freePcidJsonInfo.contextBuffer);
458             break;
459         case FREE_DECODE_PCID_ROOT_OUT:
460             cJSON_Delete(freePcidJsonInfo.jsonRootObj);
461             cJSON_Delete(freePcidJsonInfo.sysCapObj);
462             FreeContextBuffer(freePcidJsonInfo.contextBuffer);
463             break;
464         case FREE_DECODE_PCID_SYSCAP_OUT:
465             cJSON_Delete(freePcidJsonInfo.sysCapObj);
466             FreeContextBuffer(freePcidJsonInfo.contextBuffer);
467             break;
468         case FREE_DECODE_PCID_CONTEXT_OUT:
469             cJSON_Delete(freePcidJsonInfo.sysCapObj);
470             FreeContextBuffer(freePcidJsonInfo.contextBuffer);
471             break;
472         default:
473             FreeContextBuffer(freePcidJsonInfo.contextBuffer);
474     }
475     return ret;
476 }
477 
DecodePCID(char * inputFile,char * outDirPath)478 int32_t DecodePCID(char *inputFile, char *outDirPath)
479 {
480     int32_t ret = 0;
481     uint32_t contextBufLen;
482     struct FreeDecodePcidJsonInfo freePcidJsonInfo;
483     freePcidJsonInfo.strJson = NULL;
484     freePcidJsonInfo.contextBuffer = NULL;
485     freePcidJsonInfo.jsonRootObj = NULL;
486     freePcidJsonInfo.sysCapObj = NULL;
487     freePcidJsonInfo.flag = 0;
488 
489     ret = CheckFileAndGetFileContext(inputFile, &freePcidJsonInfo.contextBuffer, (uint32_t *)&contextBufLen);
490     if (ret != 0) {
491         return -1;
492     }
493 
494     PCIDMain *pcidMain = (PCIDMain *)freePcidJsonInfo.contextBuffer;
495 
496     /* api version */
497     if (pcidMain->apiVersionType != 0) {
498         PRINT_ERR("Prase file failed, apiVersionType is invaild, input file : %s\n", inputFile);
499         return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONTEXT_OUT, -1);
500     }
501 
502     /* system type */
503     char *systemType = pcidMain->systemType == 0b001 ? "mini" :(pcidMain->systemType == 0b010 ? "small" :
504                         (pcidMain->systemType == 0b100 ? "standard" : NULL));
505     if (systemType == NULL) {
506         PRINT_ERR("prase file failed, systemType is invaild, %u\n", pcidMain->systemType);
507         return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONTEXT_OUT, -1);
508     }
509 
510     /* syscap */
511     freePcidJsonInfo.sysCapObj = cJSON_CreateObject();
512     ret = CheckSysCapObj(freePcidJsonInfo, pcidMain, contextBufLen, ret);
513     if (freePcidJsonInfo.flag == -1) {
514         return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONTEXT_OUT, ret);
515     }
516 
517     // create json root
518     freePcidJsonInfo.jsonRootObj = cJSON_CreateObject();
519     if (freePcidJsonInfo.jsonRootObj == NULL) {
520         PRINT_ERR("cJSON_CreateObject failed\n");
521         return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_SYSCAP_OUT, -1);
522     }
523 
524     ret = CheckJsonRootObj(freePcidJsonInfo, pcidMain, systemType);
525     if (ret == -1) {
526         return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_ROOT_OUT, ret);
527     }
528 
529     freePcidJsonInfo.strJson = cJSON_Print(freePcidJsonInfo.jsonRootObj);
530     if (freePcidJsonInfo.strJson  == NULL) {
531         return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_ROOT_OUT, -1);
532     }
533 
534     const char outputFileName[] = "pcid.json";
535     ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, freePcidJsonInfo.strJson,
536                                      strlen(freePcidJsonInfo.strJson));
537     if (ret != 0) {
538         PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, outputFileName);
539     }
540     return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONVERT_OUT, ret);
541 }
542 
543 #define U32_TO_STR_MAX_LEN 11
544 #define OS_SYSCAP_NUM 30
545 #define PCID_HEADER 2
ParseStringSyscap(char * input,uint32_t * osSyscap,uint32_t osSyscapNum,uint32_t * header,uint32_t headerLen)546 static int32_t ParseStringSyscap(char *input, uint32_t *osSyscap, uint32_t osSyscapNum,
547                                  uint32_t *header, uint32_t headerLen)
548 {
549     int32_t ret;
550     uint32_t tempNum;
551     uint32_t i = 0;
552     size_t inputLen = strlen(input);
553 
554     if (osSyscapNum != OS_SYSCAP_NUM || headerLen != PCID_HEADER) {
555         PRINT_ERR("Input osSyscapNum(%u) or headerLen(%u) error.\n", osSyscapNum, headerLen);
556         return -1;
557     }
558 
559     if (sscanf_s(input, "%u,%u,%s", &header[0], &header[1], input, inputLen) != 3) { // 3, return val of "%u,%u,%s"
560         PRINT_ERR("Get pcid header failed.\n");
561         return -1;
562     }
563 
564     while ((ret = sscanf_s(input, "%u,%s", &tempNum, input, inputLen)) > 0) {
565         osSyscap[i++] = tempNum;
566         if (i >= OS_SYSCAP_NUM) {
567             break;
568         }
569     }
570     if (ret == -1) {
571         PRINT_ERR("sscanf_s failed, i = %u.\n", i);
572         return -1;
573     }
574 
575     if (strlen(input) <= 1) {
576         *input = '\0';
577     }
578 
579     return 0;
580 }
581 
AddHeaderToJsonObj(uint32_t * pcidHeader,uint32_t pcidHeaderLen,cJSON * rootObj)582 static int32_t AddHeaderToJsonObj(uint32_t *pcidHeader, uint32_t pcidHeaderLen, cJSON *rootObj)
583 {
584     if (pcidHeaderLen != PCID_HEADER) {
585         PRINT_ERR("input pcidHeader(%u) error.\n", pcidHeaderLen);
586         return -1;
587     }
588 
589     PCIDHeader *header = (PCIDHeader *)pcidHeader;
590     // trans system type to string
591     char *systemType = header->systemType  == 0b001 ? "mini" :
592                        (header->systemType == 0b010 ? "small" :
593                        (header->systemType == 0b100 ? "standard" : NULL));
594     if (systemType == NULL) {
595         PRINT_ERR("prase system type failed.\n");
596         return -1;
597     }
598 
599     // add to json
600     if (!cJSON_AddNumberToObject(rootObj, "api_version", NtohsInter(header->apiVersion))) {
601         PRINT_ERR("add api_version(%u) to json object failed.\n", NtohsInter(header->apiVersion));
602         return -1;
603     }
604     if (!cJSON_AddNumberToObject(rootObj, "manufacturer_id", NtohlInter(header->manufacturerID))) {
605         PRINT_ERR("add manufacturer_id(%u) to json object failed\n", NtohlInter(header->manufacturerID));
606         return -1;
607     }
608     if (!cJSON_AddStringToObject(rootObj, "system_type", systemType)) {
609         PRINT_ERR("add system_type(%s) to json object failed\n", systemType);
610         return -1;
611     }
612     return 0;
613 }
614 
AddOsSyscapToJsonObj(uint32_t * osSyscapArray,uint32_t osSyscapArrayLen,cJSON * sysCapObj)615 static int32_t AddOsSyscapToJsonObj(uint32_t *osSyscapArray, uint32_t osSyscapArrayLen, cJSON *sysCapObj)
616 {
617     cJSON *sysCapArray = cJSON_CreateArray();
618     if (sysCapArray == NULL) {
619         PRINT_ERR("Create cJSON array failed.\n");
620         return -1;
621     }
622 
623     if (osSyscapArrayLen != OS_SYSCAP_NUM) {
624         PRINT_ERR("Input os syscap array len error.\n");
625         cJSON_Delete(sysCapArray);
626         return -1;
627     }
628     uint8_t *osSysCapArrayUint8 = (uint8_t *)osSyscapArray;
629 
630     uint32_t i, j;
631     uint32_t osSyscapCount = 0;
632     uint16_t index[OS_SYSCAP_BYTES * UINT8_BIT] = {0};
633     for (i = 0; i < OS_SYSCAP_BYTES; i++) {
634         for (j = 0; j < UINT8_BIT; j++) {
635             if (osSysCapArrayUint8[i] & (0x01 << j)) {
636                 index[osSyscapCount++] = i * UINT8_BIT + j;
637             }
638         }
639     }
640 
641     for (i = 0; i < osSyscapCount; i++) {
642         for (j = 0; j < sizeof(g_arraySyscap) / sizeof(SyscapWithNum); j++) {
643             if (index[i] != g_arraySyscap[j].num) {
644                 continue;
645             }
646             if (!cJSON_AddItemToArray(sysCapArray, cJSON_CreateString(g_arraySyscap[j].str))) {
647                 PRINT_ERR("Add os syscap string to json failed.\n");
648                 cJSON_Delete(sysCapArray);
649                 return -1;
650             }
651             break;
652         }
653     }
654 
655     if (!cJSON_AddItemToObject(sysCapObj, "os", sysCapArray)) {
656         PRINT_ERR("Add os syscap item to json object failed.\n");
657         cJSON_Delete(sysCapArray);
658         return -1;
659     }
660     return 0;
661 }
662 
AddPriSyscapToJsonObj(char * priSyscapString,uint32_t priSyscapStringLen,cJSON * sysCapObj)663 static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapStringLen, cJSON *sysCapObj)
664 {
665     char *token = NULL;
666 
667     cJSON *sysCapArray = cJSON_CreateArray();
668     if (sysCapArray == NULL) {
669         PRINT_ERR("Create cJSON array failed.\n");
670         return -1;
671     }
672     if (priSyscapStringLen == 0) {
673         if (!cJSON_AddItemToObject(sysCapObj, "private", sysCapArray)) {
674             PRINT_ERR("Add private syscap array to json failed.\n");
675             cJSON_Delete(sysCapArray);
676             return -1;
677         }
678         cJSON_Delete(sysCapArray);
679         return 0;
680     }
681 
682     token = strtok(priSyscapString, ",");
683     while (token != NULL) {
684         if (!cJSON_AddItemToArray(sysCapArray, cJSON_CreateString(token))) {
685             PRINT_ERR("Add private syscap string to json failed.\n");
686             cJSON_Delete(sysCapArray);
687             return -1;
688         }
689         token = strtok(NULL, ",");
690     }
691     if (!cJSON_AddItemToObject(sysCapObj, "private", sysCapArray)) {
692         PRINT_ERR("Add private syscap array to json failed.\n");
693         cJSON_Delete(sysCapArray);
694         return -1;
695     }
696     return 0;
697 }
698 
GetSyscapStr(char * input,char const * priSyscapStr,uint32_t * osSyscap,uint32_t * pcidHeader)699 static int32_t GetSyscapStr(char *input, char const *priSyscapStr, uint32_t* osSyscap, uint32_t *pcidHeader)
700 {
701     if (input == NULL) {
702         PRINT_ERR("inputFile is null.\n");
703         return -1;
704     }
705     char *ctx = NULL;
706     uint32_t fileContextLen;
707     if (GetFileContext(input, &ctx, (uint32_t *)&fileContextLen) != 0) {
708         PRINT_ERR("GetFileContext failed, input file : %s\n", input);
709         return -1;
710     }
711     if (ParseStringSyscap(ctx, osSyscap, OS_SYSCAP_NUM, pcidHeader, PCID_HEADER) != 0) {
712         PRINT_ERR("Parse string syscap failed.\n");
713         free(ctx);
714         return -1;
715     }
716     priSyscapStr = ctx;
717     return 0;
718 }
719 
DecodeStringPCIDToJson(char * input,char * outDirPath)720 int32_t DecodeStringPCIDToJson(char *input, char *outDirPath)
721 {
722     int32_t ret = -1;
723     uint32_t osSyscap[OS_SYSCAP_NUM] = {0};
724     uint32_t pcidHeader[PCID_HEADER];
725     char *priSyscapStr = NULL;
726     char *jsonBuffer = NULL;
727 
728     ret = GetSyscapStr(input, priSyscapStr, osSyscap, pcidHeader);
729     if (ret == -1) {
730         return ret;
731     }
732 
733     // add to json object
734     cJSON *sysCapObj = cJSON_CreateObject();
735     cJSON *rootObj = cJSON_CreateObject();
736     if (sysCapObj == NULL || rootObj == NULL) {
737         PRINT_ERR("Failed to create cJSON objects.\n");
738         goto FAILED;
739     }
740 
741     if (!cJSON_AddItemToObject(rootObj, "syscap", sysCapObj)) {
742         PRINT_ERR("Add syscap to json failed.\n");
743         goto FAILED;
744     }
745     if (AddHeaderToJsonObj(pcidHeader, PCID_HEADER, rootObj) != 0) {
746         PRINT_ERR("Add header to json object failed.\n");
747         goto FAILED;
748     }
749     if (AddOsSyscapToJsonObj(osSyscap, OS_SYSCAP_NUM, sysCapObj) != 0) {
750         PRINT_ERR("Add os syscap json object failed.\n");
751         goto FAILED;
752     }
753 
754     if (AddPriSyscapToJsonObj(priSyscapStr, (uint32_t) strlen(priSyscapStr), sysCapObj) != 0) {
755         PRINT_ERR("Add private syscap json object failed.\n");
756         goto FAILED;
757     }
758     // save as json file
759     jsonBuffer = cJSON_Print(rootObj);
760     if (jsonBuffer == NULL) {
761         PRINT_ERR("json buffer is null.\n");
762         goto FAILED;
763     }
764     const char outputFileName[] = "pcid.json";
765     if (ConvertedContextSaveAsFile(outDirPath, outputFileName, jsonBuffer, strlen(jsonBuffer)) != 0) {
766         PRINT_ERR("Save as json file failed.\n");
767         goto FAILED;
768     }
769     ret = 0;
770 
771 FAILED:
772     cJSON_free(jsonBuffer);
773     SafeFree(priSyscapStr);
774     cJSON_Delete(sysCapObj);
775     cJSON_Delete(rootObj);
776     return ret;
777 }
778 
FreeAfterEncodePCID(struct FreeEncodePcidInfo freePcidInfo,int32_t type,int32_t ret)779 static int32_t FreeAfterEncodePCID(struct FreeEncodePcidInfo freePcidInfo, int32_t type, int32_t ret)
780 {
781     switch (type) {
782         case FREE_ENCODE_PCID_OUT:
783             free(freePcidInfo.output);
784             free(freePcidInfo.priSyscapFull);
785             free(freePcidInfo.contextBuffer);
786             break;
787         case FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT:
788             free(freePcidInfo.priSyscapFull);
789             free(freePcidInfo.contextBuffer);
790             break;
791         case FREE_ENCODE_PCID_CONTEXT_OUT:
792         default:
793             free(freePcidInfo.contextBuffer);
794     }
795     return ret;
796 }
797 
GetEncodePCIDOut(uint16_t priSyscapCount,uint32_t privateSyscapLen,uint32_t * mainSyscap,struct FreeEncodePcidInfo freePcidInfo,int32_t ret)798 static int32_t GetEncodePCIDOut(uint16_t priSyscapCount, uint32_t privateSyscapLen, uint32_t *mainSyscap,
799         struct FreeEncodePcidInfo freePcidInfo, int32_t ret)
800 {
801     // 17, size of "SystemCapability."
802     uint32_t outputLen = U32_TO_STR_MAX_LEN * PCID_OUT_BUFFER + 17 * priSyscapCount + privateSyscapLen + 1;
803     char *output = NULL;
804     uint32_t i;
805     output = (char *)malloc(outputLen);
806     if (output == NULL) {
807         PRINT_ERR("malloc failed\n");
808         return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT, ret);
809     }
810     (void)memset_s(output, outputLen, 0, outputLen);
811     ret = sprintf_s(output, outputLen, "%u", mainSyscap[0]);
812     if (ret == -1) {
813         PRINT_ERR("sprintf_s failed\n");
814         free(output);
815         return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret);
816     }
817     for (i = 1; i < PCID_OUT_BUFFER; i++) {
818         ret = sprintf_s(output, outputLen, "%s,%u", output, mainSyscap[i]);
819         if (ret == -1) {
820             PRINT_ERR("sprintf_s failed\n");
821             free(output);
822             return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret);
823         }
824     }
825     for (i = 0; i < priSyscapCount; i++) {
826         ret = sprintf_s(output, outputLen, "%s,%s", output, freePcidInfo.priSyscapFull + i * SINGLE_SYSCAP_LEN);
827         if (ret == -1) {
828             PRINT_ERR("sprintf_s failed\n");
829             free(output);
830             return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret);
831         }
832     }
833     // save as file
834     const char outputFileName[] = "pcid.txt";
835     ret = ConvertedContextSaveAsFile(freePcidInfo.outDirPathFinal, outputFileName, output, strlen(output));
836     if (ret != 0) {
837         PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n",
838                   freePcidInfo.outDirPathFinal, outputFileName);
839     }
840     free(output);
841     return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret);
842 }
843 
CheckPrivateSyCap(struct FreeEncodePcidInfo freePcidInfo,uint32_t privateSyscapLen,char * privateSyscap,int32_t ret)844 static int32_t CheckPrivateSyCap(struct FreeEncodePcidInfo freePcidInfo, uint32_t privateSyscapLen,
845     char *privateSyscap, int32_t ret)
846 {
847     uint32_t i, j;
848     char tempSyscap[SINGLE_SYSCAP_LEN] = {0};
849     char *temp = tempSyscap;
850     for (i = 0, j = 0; i < privateSyscapLen; i++) {
851         if (*privateSyscap == ',') {
852             *temp = '\0';
853             ret = sprintf_s(freePcidInfo.priSyscapFull + j * SINGLE_SYSCAP_LEN, SINGLE_SYSCAP_LEN,
854                             "SystemCapability.%s", tempSyscap);
855             if (ret == -1) {
856                 PRINT_ERR("sprintf_s failed\n");
857                 return ret;
858             }
859             temp = tempSyscap;
860             privateSyscap++;
861             j++;
862             continue;
863         }
864         *temp++ = *privateSyscap++;
865     }
866     return ret;
867 }
868 
EncodePcidscToString(char * inputFile,char * outDirPath)869 int32_t EncodePcidscToString(char *inputFile, char *outDirPath)
870 {
871     int32_t ret = 0;
872     uint32_t bufferLen, privateSyscapLen;
873     uint32_t i;
874     uint32_t *mainSyscap = NULL;
875     uint16_t priSyscapCount = 0;
876 
877     char *privateSyscap = NULL;
878     struct FreeEncodePcidInfo freePcidInfo;
879     freePcidInfo.contextBuffer = NULL;
880     freePcidInfo.priSyscapFull = NULL;
881     freePcidInfo.output = NULL;
882     freePcidInfo.outDirPathFinal = outDirPath;
883     PCIDMain *pcidMain = NULL;
884 
885     ret = CheckFileAndGetFileContext(inputFile, &freePcidInfo.contextBuffer, (uint32_t *)&bufferLen);
886     if (ret != 0) {
887         return -1;
888     }
889 
890     if (bufferLen > 1128) { // 1128, max size of pcid.sc
891         PRINT_ERR("Input pcid file too large, pcid file size: %u\n", bufferLen);
892         return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_CONTEXT_OUT, ret);
893     }
894 
895     pcidMain = (PCIDMain *)freePcidInfo.contextBuffer;
896     privateSyscap = (char *)(pcidMain + 1);
897     privateSyscapLen = strlen(privateSyscap);
898 
899     // process os syscap
900     mainSyscap = (uint32_t *)pcidMain;
901 
902     // process private syscap
903     for (i = 0; i < privateSyscapLen; i++) {
904         if (privateSyscap[i] == ',') {
905             priSyscapCount++;
906         }
907     }
908     if (priSyscapCount == 0) {
909         return GetEncodePCIDOut(priSyscapCount, privateSyscapLen, mainSyscap,  freePcidInfo, ret);
910     }
911     freePcidInfo.priSyscapFull = (char *)malloc(priSyscapCount * SINGLE_SYSCAP_LEN);
912     if (freePcidInfo.priSyscapFull == NULL) {
913         PRINT_ERR("malloc failed\n");
914         return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_CONTEXT_OUT, ret);
915     }
916     (void)memset_s(freePcidInfo.priSyscapFull, priSyscapCount * SINGLE_SYSCAP_LEN,
917                    0, priSyscapCount * SINGLE_SYSCAP_LEN);
918 
919     ret = CheckPrivateSyCap(freePcidInfo, privateSyscapLen, privateSyscap, ret);
920     if (ret == -1) {
921         return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT, ret);
922     }
923 
924     // output
925     return GetEncodePCIDOut(priSyscapCount, privateSyscapLen, mainSyscap,  freePcidInfo, ret);
926 }