add syscap napi.

Signed-off-by: yudechen <chenyude@huawei.com>
Change-Id: Id74d73b360a94be5b9b7f28de8e036539113d51a
This commit is contained in:
yudechen
2022-04-18 10:47:14 +08:00
parent 37e1afdfbf
commit 3e68d6ac1e
7 changed files with 458 additions and 158 deletions
+9 -3
View File
@@ -120,10 +120,16 @@ build_ext_component("generate_pcid") {
deps = [ ":syscap_tool_bin_linux" ]
exec_path = rebase_path(root_out_dir)
preload_path = rebase_path(preloader_output_dir)
cmd = "${exec_path}/clang_x64/developtools/syscap_codec/syscap_tool"
if (defined(ohos_lite)) {
cmd = "${exec_path}/clang_x64/syscap_tool"
} else {
cmd = "${exec_path}/clang_x64/developtools/syscap_codec/syscap_tool"
}
command = "chmod 777 $cmd"
command += " && $cmd -N -e -i ${preload_path}/${product_name}/system/etc/SystemCapability.json"
command += " && mv $exec_path/${product_name}.sc $exec_path/PCID.sc"
command += " && $cmd -P -e -i ${preload_path}/${product_name}/system/etc/SystemCapability.json"
if (defined(ohos_lite)) {
command += " && mkdir -p $exec_path/system/etc && cp $exec_path/PCID.sc $exec_path/system/etc/PCID.sc"
}
}
ohos_prebuilt_etc("PCID.sc") {
+1
View File
@@ -163,5 +163,6 @@ typedef struct ProductCompatibilityID {
} PCIDMain;
int32_t CreatePCID(char *inputFile, char *outDirPath);
int32_t DecodePCID(char *inputFile, char *outDirPath);
#endif
+165 -38
View File
@@ -19,6 +19,8 @@
#include <securec.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <securec.h>
#include "create_pcid.h"
#include "syscap_interface.h"
@@ -26,6 +28,7 @@
#define MAX_SYSCAP_STR_LEN 128
#define OS_SYSCAP_BYTES 120
#define BITS_OF_BYTE 8
#define PCID_MAIN_LEN 128
#define PRINT_ERR(...) \
do { \
@@ -33,7 +36,9 @@
printf(__VA_ARGS__); \
} while (0)
static SyscapWithNum arraySyscap[] = {
static char *inputFile = "/system/etc/PCID.sc";
static const SyscapWithNum arraySyscap[] = {
{"SystemCapability.Account.AppAccount", ACCOUNT_APPACCOUNT},
{"SystemCapability.Account.OsAccount", ACCOUNT_OSACCOUNT},
{"SystemCapability.Ace.AceEngineLite", ACE_ACEENGINELITE},
@@ -163,58 +168,126 @@ static SyscapWithNum arraySyscap[] = {
{"SystemCapability.UserIAM.UserIdm", USERIAM_USERIDM}
};
bool EncodeOsSyscap(int (*output)[32])
static void FreeContextBuffer(char *contextBuffer)
{
uint8_t *outputArray = (uint8_t *)malloc(sizeof(int) * PCID_OUT_BUFFER);
if (outputArray == NULL) {
PRINT_ERR("malloc failed.");
(void)free(contextBuffer);
}
static uint32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen)
{
uint32_t ret;
FILE *fp = NULL;
struct stat statBuf;
char *contextBuffer = NULL;
ret = stat(inputFile, &statBuf);
if (ret != 0) {
PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", inputFile, errno);
return -1;
}
if (!(statBuf.st_mode & S_IRUSR)) {
PRINT_ERR("don't have permission to read the file(%s)\n", inputFile);
return -1;
}
contextBuffer = (char *)malloc(statBuf.st_size + 1);
if (contextBuffer == NULL) {
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno);
return -1;
}
fp = fopen(inputFile, "rb");
if (fp == NULL) {
PRINT_ERR("open file(%s) failed, errno = %d\n", inputFile, errno);
FreeContextBuffer(contextBuffer);
return -1;
}
ret = fread(contextBuffer, statBuf.st_size, 1, fp);
if (ret != 1) {
PRINT_ERR("read file(%s) failed, errno = %d\n", inputFile, errno);
FreeContextBuffer(contextBuffer);
(void)fclose(fp);
return -1;
}
contextBuffer[statBuf.st_size] = '\0';
(void)fclose(fp);
*contextBufPtr = contextBuffer;
*bufferLen = statBuf.st_size + 1;
return 0;
}
bool EncodeOsSyscap(int **output)
{
int32_t ret;
int32_t res;
char *contextBuffer = NULL;
int *outputArray = NULL;
uint32_t bufferLen;
ret = GetFileContext(&contextBuffer, &bufferLen);
if (ret != 0) {
PRINT_ERR("GetFileContext failed, input file : rk3568.sc\n");
return false;
}
(void)memset_s(outputArray, sizeof(int) * PCID_OUT_BUFFER, 0, sizeof(int) * PCID_OUT_BUFFER);
uint16_t countBytes = PCID_OUT_BUFFER * sizeof(int);
for (uint16_t i = 0; i < countBytes; i++) {
outputArray[i] |= 0XFF;
outputArray = (int *)malloc(PCID_MAIN_LEN);
if (outputArray == NULL) {
PRINT_ERR("malloc buffer failed, size = %d\n", PCID_MAIN_LEN);
return false;
}
int ret = memcpy_s(*output, sizeof(int) * PCID_OUT_BUFFER, outputArray, sizeof(int) * PCID_OUT_BUFFER);
if (ret != 0) {
(void)memset_s(outputArray, PCID_MAIN_LEN, 0, PCID_MAIN_LEN);
res = memcpy_s(outputArray, PCID_MAIN_LEN, contextBuffer, PCID_MAIN_LEN);
if (res != 0) {
PRINT_ERR("memcpy_s failed.");
FreeContextBuffer(contextBuffer);
free(outputArray);
return false;
}
free(outputArray);
FreeContextBuffer(contextBuffer);
*output = outputArray;
return true;
}
bool EncodePrivateSyscap(char **output, int *outputLen)
{
static char syscapStr[MAX_SYSCAP_STR_LEN] = "Systemcapability.Ai.AiEngine";
int ret = strcpy_s(output, MAX_SYSCAP_STR_LEN, syscapStr);
int32_t ret;
int32_t res;
char *contextBuffer = NULL;
char *outputStr = NULL;
uint32_t bufferLen;
ret = GetFileContext(&contextBuffer, &bufferLen);
if (ret != 0) {
PRINT_ERR("strcpy_s failed.");
PRINT_ERR("GetFileContext failed, input file : rk3568.sc\n");
return false;
}
*outputLen = bufferLen - MAX_SYSCAP_STR_LEN - 1;
outputStr = (char *)malloc(*outputLen);
if (outputStr == NULL) {
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", *outputLen, errno);
return false;
}
(void)memset_s(outputStr, *outputLen, 0, *outputLen);
res = strncpy_s(outputStr, *outputLen + 1, contextBuffer + MAX_SYSCAP_STR_LEN, *outputLen);
if (res != 0) {
PRINT_ERR("strcpy_s failed.");
FreeContextBuffer(contextBuffer);
return false;
}
*outputLen = strlen(syscapStr);
FreeContextBuffer(contextBuffer);
*output = outputStr;
return true;
}
bool DecodeOsSyscap(int input[32], char ***output, int *outputCnt)
bool DecodeOsSyscap(int input[32], char (**output)[128], int *outputCnt)
{
errno_t nRet = 0;
const int headerWithInt = 2; // 2, int[2] of pcid header
int *tmp = NULL;
uint8_t osSyscap[OS_SYSCAP_BYTES];
uint16_t indexOfSyscap[OS_SYSCAP_BYTES * OS_SYSCAP_BYTES] = {0};
int countOfSyscap = 0, i, j, k = 0;
int countOfSyscap = 0, i, j;
tmp = input + headerWithInt;
nRet = memcpy_s(osSyscap, OS_SYSCAP_BYTES, tmp, OS_SYSCAP_BYTES);
if (nRet != EOK) {
PRINT_ERR("memcpy_s failed.");
*outputCnt = 0;
return false;
}
uint8_t *osSyscap = (uint8_t *)(input + 2); // 2, int[2] of pcid header
for (i = 0; i < OS_SYSCAP_BYTES; i++) {
for (j = 0; j < BITS_OF_BYTE; j++) {
@@ -223,9 +296,10 @@ bool DecodeOsSyscap(int input[32], char ***output, int *outputCnt)
}
}
}
*outputCnt = countOfSyscap;
char (*strSyscap)[MAX_SYSCAP_STR_LEN] = \
(char (*)[MAX_SYSCAP_STR_LEN])malloc(countOfSyscap * MAX_SYSCAP_STR_LEN);
char (*strSyscap)[MAX_SYSCAP_STR_LEN] = NULL;
strSyscap= (char (*)[MAX_SYSCAP_STR_LEN])malloc(countOfSyscap * MAX_SYSCAP_STR_LEN);
if (strSyscap == NULL) {
PRINT_ERR("malloc failed.");
*outputCnt = 0;
@@ -233,20 +307,73 @@ bool DecodeOsSyscap(int input[32], char ***output, int *outputCnt)
}
(void)memset_s(strSyscap, countOfSyscap * MAX_SYSCAP_STR_LEN, \
0, countOfSyscap * MAX_SYSCAP_STR_LEN);
char **strSyscapBak = (char **)strSyscap;
for (i = 0; i < countOfSyscap; i++) {
for (j = 0; j < sizeof(arraySyscap) / sizeof(SyscapWithNum); j++) {
if (arraySyscap[j].num == indexOfSyscap[i]) {
nRet = strcpy_s(strSyscap[k++], MAX_SYSCAP_STR_LEN - 1, arraySyscap[j].syscapStr);
}
if (nRet != EOK) {
PRINT_ERR("strcpy_s failed.");
*outputCnt = 0;
free(strSyscap);
return false;
nRet = strcpy_s(*strSyscap, MAX_SYSCAP_STR_LEN, arraySyscap[j].syscapStr);
if (nRet != EOK) {
printf("strcpy_s failed. error = %d\n", nRet);
*outputCnt = 0;
free(strSyscap);
return false;
}
strSyscap++;
break;
}
}
}
*output = (char **)strSyscap;
*output = (char (*)[MAX_SYSCAP_STR_LEN])strSyscapBak;
return true;
}
bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt)
{
char (*outputArray)[MAX_SYSCAP_STR_LEN] = NULL;
char *inputStr = input;
uint16_t bufferLen;
int syscapCnt = 0, ret;
while (*inputStr != '\0') {
if (*inputStr == ',') {
syscapCnt++;
}
inputStr++;
}
inputStr = input;
bufferLen = MAX_SYSCAP_STR_LEN * syscapCnt;
outputArray = (char (*)[MAX_SYSCAP_STR_LEN])malloc(bufferLen);
if (outputArray == NULL) {
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", bufferLen, errno);
syscapCnt = 0;
return false;
}
(void)memset_s(outputArray, bufferLen, 0, bufferLen);
char **outputArrayBak = (char **)outputArray;
char priSyscapStr[MAX_SYSCAP_STR_LEN - 17] = {0}; // 17. size of "SystemCapability."
char *tempPriSyscapStr = priSyscapStr;
while (*inputStr != '\0') {
if (*inputStr == ',') {
*tempPriSyscapStr = '\0';
ret = sprintf_s(*outputArray, MAX_SYSCAP_STR_LEN, "SystemCapability.%s", priSyscapStr);
if (ret == -1) {
PRINT_ERR("sprintf_s failed\n");
*outputCnt = 0;
free(outputArray);
return false;
}
tempPriSyscapStr = priSyscapStr;
outputArray++;
inputStr++;
continue;
}
*tempPriSyscapStr++ = *inputStr++;
}
*outputCnt = syscapCnt;
*output = (char (*)[MAX_SYSCAP_STR_LEN])outputArrayBak;
return true;
}
+3 -3
View File
@@ -34,10 +34,10 @@ typedef struct ProductCompatibilityIDHead {
} PCIDHead; // to do
bool EncodeOsSyscap(int (*output)[32]);
bool DecodeOsSyscap(int input[32], char ***output, int *outputCnt);
bool EncodeOsSyscap(int **output);
bool DecodeOsSyscap(int input[32], char (**output)[128], int *outputCnt);
bool EncodePrivateSyscap(char **output, int *outputLen);
bool DecodePrivateSyscap(char *input, char ***output, int *outputCnt);
bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt);
#ifdef __cplusplus
#if __cplusplus
+258 -104
View File
@@ -32,7 +32,8 @@
#define SINGLE_FEAT_LENGTH (32 * 8)
#define PER_SYSCAP_LEN_MAX 128
#define PRIVATE_SYSCAP_SIZE 1000
#define BITS_OF_BYTE 8
#define BITS_OF_ONE_BYTE 8
#define BYTES_OF_OS_SYSCAP 120
#define PRINT_ERR(...) \
do { \
@@ -40,10 +41,8 @@
printf(__VA_ARGS__); \
} while (0)
const char osSyscapStrHead[] = "SystemCapability.";
/* ensure sort by num */
SyscapWithNum arraySyscap[] = {
static SyscapWithNum arraySyscap[] = {
{"SystemCapability.Account.AppAccount", ACCOUNT_APPACCOUNT},
{"SystemCapability.Account.OsAccount", ACCOUNT_OSACCOUNT},
{"SystemCapability.Ace.AceEngineLite", ACE_ACEENGINELITE},
@@ -178,7 +177,7 @@ static void FreeContextBuffer(char *contextBuffer)
(void)free(contextBuffer);
}
static uint32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *bufferLen)
static uint32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *contextBufLen)
{
uint32_t ret;
FILE *fp = NULL;
@@ -216,11 +215,12 @@ static uint32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *
(void)fclose(fp);
*contextBufPtr = contextBuffer;
*bufferLen = statBuf.st_size + 1;
*contextBufLen = statBuf.st_size + 1;
return 0;
}
static int32_t ConvertedContextSaveAsFile(char *outDirPath, char *filename, char *convertedBuffer, uint32_t bufferLen)
static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, \
char *convertedBuffer, uint32_t contextBufLen)
{
int32_t ret;
FILE *fp = NULL;
@@ -250,7 +250,7 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, char *filename, char
return -1;
}
ret = fwrite(convertedBuffer, bufferLen, 1, fp);
ret = fwrite(convertedBuffer, contextBufLen, 1, fp);
if (ret != 1) {
PRINT_ERR("can`t write file(%s),errno = %d\n", fileFullPath, errno);
(void)fclose(fp);
@@ -274,132 +274,130 @@ static cJSON *CreateWholeSyscapJsonObj(void)
int32_t CreatePCID(char *inputFile, char *outDirPath)
{
int32_t ret, osCapSize, privateCapSize;
uint32_t bufferLen;
uint8_t sectorOfBits, posOfBits;
int32_t ret, i;
uint32_t contextBufLen, osCapSize, privateCapSize;
errno_t nRet = 0;
char *contextBuffer = NULL;
char *systemType = NULL;
char productName[NAME_MAX] = {0};
cJSON *cjsonObjectRoot = NULL;
cJSON *cjsonObjectPtr = NULL;
cJSON *osCapPtr = NULL;
cJSON *privateCapPtr = NULL;
cJSON *arrayItemPtr = NULL;
cJSON *jsonRootObj = NULL;
cJSON *jsonSyscapObj = NULL;
cJSON *jsonOsSyscapObj = NULL;
cJSON *jsonPriSyscapObj = NULL;
cJSON *jsonArrayItem = NULL;
cJSON *osCapIndex = NULL;
cJSON *allOsSyscapObj = CreateWholeSyscapJsonObj();
ret = GetFileContext(inputFile, &contextBuffer, &bufferLen);
ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen);
if (ret != 0) {
PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
return -1;
}
cjsonObjectRoot = cJSON_ParseWithLength(contextBuffer, bufferLen);
if (cjsonObjectRoot == NULL) {
jsonRootObj = cJSON_ParseWithLength(contextBuffer, contextBufLen);
if (jsonRootObj == NULL) {
PRINT_ERR("cJSON_Parse failed, context buffer is:\n%s\n", contextBuffer);
ret = -1;
goto FREE_CONVERT_OUT;
}
cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "syscap");
if (cjsonObjectPtr == NULL || !cJSON_IsObject(cjsonObjectPtr)) {
PRINT_ERR("get \"syscap\" object failed, cjsonObjectPtr = %p\n", cjsonObjectPtr);
jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "syscap");
if (jsonSyscapObj == NULL || !cJSON_IsObject(jsonSyscapObj)) {
PRINT_ERR("get \"syscap\" object failed, jsonSyscapObj = %p\n", jsonSyscapObj);
ret = -1;
goto FREE_CONVERT_OUT;
}
osCapPtr = cJSON_GetObjectItem(cjsonObjectPtr, "os");
if (osCapPtr == NULL || !cJSON_IsArray(osCapPtr)) {
PRINT_ERR("get \"os\" array failed, osCapPtr = %p\n", osCapPtr);
jsonOsSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "os");
if (jsonOsSyscapObj == NULL || !cJSON_IsArray(jsonOsSyscapObj)) {
PRINT_ERR("get \"os\" array failed, jsonOsSyscapObj = %p\n", jsonOsSyscapObj);
ret = -1;
goto FREE_CONVERT_OUT;
}
osCapSize = cJSON_GetArraySize(osCapPtr);
osCapSize = cJSON_GetArraySize(jsonOsSyscapObj);
if (osCapSize < 0) {
PRINT_ERR("get \"os\" array size failed\n");
ret = -1;
goto FREE_CONVERT_OUT;
}
privateCapPtr = cJSON_GetObjectItem(cjsonObjectPtr, "private");
if (privateCapPtr != NULL && cJSON_IsArray(privateCapPtr)) {
privateCapSize = cJSON_GetArraySize(privateCapPtr);
} else if (privateCapPtr == NULL) {
jsonPriSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "private");
if (jsonPriSyscapObj != NULL && cJSON_IsArray(jsonPriSyscapObj)) {
privateCapSize = cJSON_GetArraySize(jsonPriSyscapObj);
} else if (jsonPriSyscapObj == NULL) {
privateCapSize = 0;
} else {
PRINT_ERR("get \"private\" array failed, privateCapPtr = %p\n", privateCapPtr);
PRINT_ERR("get \"private\" array failed, jsonPriSyscapObj = %p\n", jsonPriSyscapObj);
ret = -1;
goto FREE_CONVERT_OUT;
}
if (privateCapSize * PER_SYSCAP_LEN_MAX > PRIVATE_SYSCAP_SIZE) {
uint16_t allPriSyscapStrLen = 0;
for (i = 0; i < privateCapSize; i++) {
jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, i);
allPriSyscapStrLen += strlen(strchr(jsonArrayItem->valuestring, '.') + 1);
allPriSyscapStrLen++; // for separator ','
}
if ((allPriSyscapStrLen + 1) > PRIVATE_SYSCAP_SIZE) {
PRINT_ERR("context of \"pri\" array is too many\n");
ret = -1;
goto FREE_CONVERT_OUT;
}
uint32_t PCIDBufferLen = sizeof(PCIDMain) + privateCapSize * PER_SYSCAP_LEN_MAX;
PCIDMain *PCIDBuffer = (PCIDMain *)malloc(PCIDBufferLen);
uint16_t PCIDLength = sizeof(PCIDMain) + allPriSyscapStrLen + 1;
PCIDMain *PCIDBuffer = (PCIDMain *)malloc(PCIDLength);
if (PCIDBuffer == NULL) {
PRINT_ERR("malloc for pcid buffer failed\n");
ret = -1;
goto FREE_CONVERT_OUT;
}
(void)memset_s(PCIDBuffer, PCIDBufferLen, 0, PCIDBufferLen);
for (int32_t i = 0; i < osCapSize; i++) {
arrayItemPtr = cJSON_GetArrayItem(osCapPtr, i);
ret = strncmp(arrayItemPtr->valuestring, osSyscapStrHead, strlen(osSyscapStrHead));
if (ret != 0) {
PRINT_ERR("context of \"os\" array is invaild\n");
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
cJSON *numOfOsSyscap = cJSON_GetObjectItem(allOsSyscapObj, arrayItemPtr->valuestring);
uint8_t sectorOfBit = (numOfOsSyscap->valueint) / BITS_OF_BYTE;
uint8_t posOfBit = (numOfOsSyscap->valueint) % BITS_OF_BYTE;
if (sectorOfBit >= MAX_OS_SYSCAP_NUM / BITS_OF_BYTE) {
(void)memset_s(PCIDBuffer, PCIDLength, 0, PCIDLength);
// process os syscap
for (i = 0; i < osCapSize; i++) {
jsonArrayItem = cJSON_GetArrayItem(jsonOsSyscapObj, i);
osCapIndex = cJSON_GetObjectItem(allOsSyscapObj, jsonArrayItem->valuestring);
sectorOfBits = (osCapIndex->valueint) / BITS_OF_ONE_BYTE;
posOfBits = (osCapIndex->valueint) % BITS_OF_ONE_BYTE;
if (sectorOfBits >= BYTES_OF_OS_SYSCAP) {
PRINT_ERR("num of \"os syscap\" is out of 960\n");
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
PCIDBuffer->osSyscap[sectorOfBit] |= 1 << (posOfBit);
PCIDBuffer->osSyscap[sectorOfBits] |= 1 << (posOfBits);
}
char *ptrPriSyscapPos = (char *)(PCIDBuffer + 1);
for (int32_t i = 0; i < privateCapSize; i++) {
arrayItemPtr = cJSON_GetArrayItem(privateCapPtr, i);
char *tmpPtrPriSyscapPos = ptrPriSyscapPos + strlen(arrayItemPtr->valuestring) + 1;
if ((PCIDMain *)tmpPtrPriSyscapPos > PCIDBuffer + \
sizeof(PCIDMain) + privateCapSize * PER_SYSCAP_LEN_MAX) {
break;
} else {
ret = memcpy_s(ptrPriSyscapPos, SINGLE_FEAT_LENGTH, \
arrayItemPtr->valuestring, strlen(arrayItemPtr->valuestring));
if (ret != 0) {
PRINT_ERR("context of \"pri\" array is invaild\n");
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
ptrPriSyscapPos += strlen(arrayItemPtr->valuestring) + 1;
// process private syscap
char *priSyscapHead = (char *)(PCIDBuffer + 1);
char *priSyscapStr = NULL;
for (i = 0; i < privateCapSize; i++) {
jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, i);
priSyscapStr = strchr(jsonArrayItem->valuestring, '.') + 1;
nRet = strcat_s(priSyscapHead, PRIVATE_SYSCAP_SIZE - 1, priSyscapStr);
nRet += strcat_s(priSyscapHead, PRIVATE_SYSCAP_SIZE - 1, ",");
if (nRet != EOK) {
PRINT_ERR("strcat_s \"pri\" string is failed\n");
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
}
cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "api_version");
if (cjsonObjectPtr == NULL || !cJSON_IsNumber(cjsonObjectPtr)) {
PRINT_ERR("get \"api_version\" failed, cjsonObjectPtr = %p\n", cjsonObjectPtr);
jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "api_version");
if (jsonSyscapObj == NULL || !cJSON_IsNumber(jsonSyscapObj)) {
PRINT_ERR("get \"api_version\" failed, jsonSyscapObj = %p\n", jsonSyscapObj);
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
PCIDBuffer->apiVersion = HtonsInter((uint16_t)cjsonObjectPtr->valueint);
PCIDBuffer->apiVersion = HtonsInter((uint16_t)jsonSyscapObj->valueint);
PCIDBuffer->apiVersionType = 0;
cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "system_type");
if (cjsonObjectPtr == NULL || !cJSON_IsString(cjsonObjectPtr)) {
PRINT_ERR("get \"system_type\" failed, cjsonObjectPtr = %p\n", cjsonObjectPtr);
jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "system_type");
if (jsonSyscapObj == NULL || !cJSON_IsString(jsonSyscapObj)) {
PRINT_ERR("get \"system_type\" failed, jsonSyscapObj = %p\n", jsonSyscapObj);
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
systemType = cjsonObjectPtr->valuestring;
systemType = jsonSyscapObj->valuestring;
PCIDBuffer->systemType = !strcmp(systemType, "mini") ? 0b001 :
(!strcmp(systemType, "small") ? 0b010 :
(!strcmp(systemType, "standard") ? 0b100 : 0));
@@ -409,39 +407,18 @@ int32_t CreatePCID(char *inputFile, char *outDirPath)
goto FREE_PCID_BUFFER_OUT;
}
cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "manufacturer_id");
if (cjsonObjectPtr == NULL || !cJSON_IsNumber(cjsonObjectPtr)) {
PRINT_ERR("get \"manufacturer_id\" failed, cjsonObjectPtr = %p\n", cjsonObjectPtr);
jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "manufacturer_id");
if (jsonSyscapObj == NULL || !cJSON_IsNumber(jsonSyscapObj)) {
PRINT_ERR("get \"manufacturer_id\" failed, jsonSyscapObj = %p\n", jsonSyscapObj);
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
PCIDBuffer->manufacturerID = HtonlInter((uint32_t)cjsonObjectPtr->valueint);
PCIDBuffer->manufacturerID = HtonlInter((uint32_t)jsonSyscapObj->valueint);
cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "product");
if (cjsonObjectPtr == NULL || !cJSON_IsString(cjsonObjectPtr)) {
PRINT_ERR("get \"product\" failed, cjsonObjectPtr = %p\n", cjsonObjectPtr);
ret = -1;
goto FREE_CONVERT_OUT;
}
ret = strncpy_s(productName, NAME_MAX, cjsonObjectPtr->valuestring, strlen(cjsonObjectPtr->valuestring));
const char pcidFileName[] = "PCID.sc";
ret = ConvertedContextSaveAsFile(outDirPath, pcidFileName, (char *)PCIDBuffer, PCIDLength);
if (ret != 0) {
PRINT_ERR("strncpy_s failed, source string:%s, len = %d, errno = %d\n",
cjsonObjectPtr->valuestring, (int32_t)strlen(cjsonObjectPtr->valuestring), errno);
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
ret = strncat_s(productName, NAME_MAX, ".sc", 4); // 4. '.' 's' 'c' '\0'
if (ret != 0) {
PRINT_ERR("strncat_s failed, (%s, %d, \".sc\", 4), errno = %d\n", productName, NAME_MAX, errno);
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
productName[NAME_MAX - 1] = '\0';
ret = ConvertedContextSaveAsFile(outDirPath, productName, (char *)PCIDBuffer, PCIDBufferLen);
if (ret != 0) {
PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, productName);
PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, pcidFileName);
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
@@ -452,4 +429,181 @@ FREE_CONVERT_OUT:
free(allOsSyscapObj);
FreeContextBuffer(contextBuffer);
return ret;
}
int32_t DecodePCID(char *inputFile, char *outDirPath)
{
int32_t ret, i, j;
errno_t nRet = 0;
char *contextBuffer = NULL;
uint8_t osSyscap[BYTES_OF_OS_SYSCAP] = {0};
uint16_t indexOfSyscap[960] = {0};
uint32_t contextBufLen, countOfSyscap = 0;
ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen);
if (ret != 0) {
PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
return -1;
}
PCIDMain *pcidMain = (PCIDMain *)contextBuffer;
/* api version */
if (pcidMain->apiVersionType != 0) {
PRINT_ERR("prase file failed, apiVersionType is invaild, input file : %s\n", inputFile);
ret = -1;
goto FREE_CONTEXT_OUT;
}
/* system type */
char *systemType = pcidMain->systemType == 0b001 ? "mini" :
(pcidMain->systemType == 0b010 ? "small" :
(pcidMain->systemType == 0b100 ? "standard" : NULL));
if (systemType == NULL) {
PRINT_ERR("prase file failed, systemType is invaild, %d\n", pcidMain->systemType);
ret = -1;
goto FREE_CONTEXT_OUT;
}
cJSON *capVectorPtr = cJSON_CreateArray();
if (capVectorPtr == NULL) {
PRINT_ERR("cJSON_CreateArray failed\n");
ret = -1;
goto FREE_CONTEXT_OUT;
}
nRet = memcpy_s(osSyscap, BYTES_OF_OS_SYSCAP, (uint8_t *)pcidMain + 8, BYTES_OF_OS_SYSCAP); // 8, bytes of pcid header
if (EOK != nRet) {
PRINT_ERR("memcpy_s failed.");
ret = -1;
goto FREE_VECTOR_OUT;
}
for (i = 0; i < BYTES_OF_OS_SYSCAP; i++) {
for (j = 0; j < BITS_OF_ONE_BYTE; j++) {
if (osSyscap[i] & (0x01 << j)) {
indexOfSyscap[countOfSyscap++] = i * BITS_OF_ONE_BYTE + j;
}
}
}
for (i = 0; i < countOfSyscap; i++) {
for (j = 0; j < sizeof(arraySyscap) / sizeof(SyscapWithNum); j++) {
if (arraySyscap[j].num == indexOfSyscap[i]) {
if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(arraySyscap[j].syscapStr))) {
printf("cJSON_AddItemToArray or cJSON_CreateString failed\n");
ret = -1;
goto FREE_VECTOR_OUT;
}
}
}
}
cJSON *sysCapObjectPtr = cJSON_CreateObject();
if (sysCapObjectPtr == NULL) {
PRINT_ERR("cJSON_CreateObject failed\n");
ret = -1;
goto FREE_CONTEXT_OUT;
}
if (!cJSON_AddItemToObject(sysCapObjectPtr, "os", capVectorPtr)) {
PRINT_ERR("cJSON_AddItemToObject failed\n");
ret = -1;
goto FREE_VECTOR_OUT;
}
// private syscap
capVectorPtr = cJSON_CreateArray();
if (capVectorPtr == NULL) {
PRINT_ERR("cJSON_CreateArray failed\n");
ret = -1;
goto FREE_SYSCAP_OUT;
}
char *ptrPrivateSyscap = (char *)(pcidMain + 1);
uint16_t privateSyscapLen = contextBufLen - sizeof(PCIDMain) - 1;
char priSyscapStr[PER_SYSCAP_LEN_MAX] = {0};
char *tempPriSyscapStr = priSyscapStr;
char fullPriSyscapStr[PER_SYSCAP_LEN_MAX] = {0};
if (privateSyscapLen < 0) {
PRINT_ERR("parse private syscap failed.");
ret = -1;
goto FREE_VECTOR_OUT;
} else if (privateSyscapLen == 0) {
goto SKIP_GET_PRIVATE;
}
while (*ptrPrivateSyscap != '\0') {
if (*ptrPrivateSyscap == ',') {
*tempPriSyscapStr = '\0';
ret = sprintf_s(fullPriSyscapStr, PER_SYSCAP_LEN_MAX, "SystemCapability.%s", priSyscapStr);
if (ret == -1) {
printf("sprintf_s failed\n");
goto FREE_VECTOR_OUT;
}
if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(fullPriSyscapStr))) {
printf("cJSON_AddItemToArray or cJSON_CreateString failed\n");
ret = -1;
goto FREE_VECTOR_OUT;
}
tempPriSyscapStr = priSyscapStr;
ptrPrivateSyscap++;
continue;
}
*tempPriSyscapStr++ = *ptrPrivateSyscap++;
}
if (!cJSON_AddItemToObject(sysCapObjectPtr, "private", capVectorPtr)) {
PRINT_ERR("cJSON_AddItemToObject failed\n");
ret = -1;
goto FREE_VECTOR_OUT;
}
SKIP_GET_PRIVATE:
capVectorPtr = NULL;
// create json root
cJSON *jsonRootObj = cJSON_CreateObject();
if (jsonRootObj == NULL) {
PRINT_ERR("cJSON_CreateObject failed\n");
ret = -1;
goto FREE_SYSCAP_OUT;
}
if (!cJSON_AddNumberToObject(jsonRootObj, "api_version", NtohsInter(pcidMain->apiVersion))) {
PRINT_ERR("cJSON_AddNumberToObject failed\n");
ret = -1;
goto FREE_ROOT_OUT;
}
if (!cJSON_AddNumberToObject(jsonRootObj, "manufacturer_id", NtohlInter(pcidMain->manufacturerID))) {
PRINT_ERR("cJSON_AddNumberToObject failed\n");
ret = -1;
goto FREE_ROOT_OUT;
}
if (!cJSON_AddStringToObject(jsonRootObj, "system_type", systemType)) {
PRINT_ERR("cJSON_AddStringToObject failed\n");
ret = -1;
goto FREE_ROOT_OUT;
}
if (!cJSON_AddItemToObject(jsonRootObj, "syscap", sysCapObjectPtr)) {
PRINT_ERR("cJSON_AddItemToObject failed\n");
ret = -1;
goto FREE_ROOT_OUT;
}
sysCapObjectPtr = NULL;
char *convertedBuffer = cJSON_Print(jsonRootObj);
const char outputFileName[] = "PCID.json";
ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, convertedBuffer, strlen(convertedBuffer));
if (ret != 0) {
PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, outputFileName);
goto FREE_CONVERT_OUT;
}
FREE_CONVERT_OUT:
free(convertedBuffer);
FREE_ROOT_OUT:
cJSON_Delete(jsonRootObj);
FREE_SYSCAP_OUT:
cJSON_Delete(sysCapObjectPtr);
FREE_VECTOR_OUT:
cJSON_Delete(capVectorPtr);
FREE_CONTEXT_OUT:
FreeContextBuffer(contextBuffer);
return ret;
}
+3 -10
View File
@@ -28,7 +28,6 @@ int main(int argc, char **argv)
char curpath[PATH_MAX] = {0};
int32_t rpcid = 0;
int32_t pcid = 0;
int32_t newpcid = 0;
int32_t encode = 0;
int32_t decode = 0;
int32_t help = 0;
@@ -40,7 +39,6 @@ int main(int argc, char **argv)
{"help", no_argument, 0, 'h' },
{"RPCID", no_argument, 0, 'R' },
{"PCID", no_argument, 0, 'P' },
{"newPCID", no_argument, 0, 'N' },
{"encode", no_argument, 0, 'e' },
{"decode", no_argument, 0, 'd' },
{"input", required_argument, 0, 'i' },
@@ -48,7 +46,7 @@ int main(int argc, char **argv)
{0, 0, 0, 0 }
};
int32_t flag = getopt_long(argc, argv, "hRPNedi:o:", long_options, &optIndex);
int32_t flag = getopt_long(argc, argv, "hRPedi:o:", long_options, &optIndex);
if (flag == -1) {
break;
}
@@ -65,9 +63,6 @@ int main(int argc, char **argv)
case 'P':
pcid = 1;
break;
case 'N':
newpcid = 1;
break;
case 'i':
inputfile = optarg;
break;
@@ -85,11 +80,9 @@ int main(int argc, char **argv)
} else if (rpcid && !pcid && !encode && decode && inputfile && !help) {
ret = RPCIDDecode(inputfile, outputpath);
} else if (!rpcid && pcid && encode && !decode && inputfile && !help) {
ret = PCIDEncode(inputfile, outputpath);
} else if (!rpcid && pcid && !encode && decode && inputfile && !help) {
ret = PCIDDecode(inputfile, outputpath);
} else if (!rpcid && !pcid && newpcid && encode && !decode && inputfile && !help) {
ret = CreatePCID(inputfile, outputpath);
} else if (!rpcid && pcid && !encode && decode && inputfile && !help) {
ret = DecodePCID(inputfile, outputpath);
} else {
printf("syscap_tool -R/P -e/d -i filepath [-o outpath]\n");
printf("-h, --help : how to use\n");
+19
View File
@@ -23,6 +23,7 @@
#include <sys/types.h>
#include <wait.h>
#include "syscap_tool.h"
#include "create_pcid.h"
char *g_testFilePcid = "{\n \"api_version\": 0,\n \"manufacturer_id\": 0,\n \"product\": \"rk3568\",\n" \
" \"syscap\": {\n \"os\": [\n \"SystemCapability.Account.AppAccount\",\n" \
@@ -61,6 +62,7 @@ int main(int argc, char **argv)
(void)unlink(pcidFileName);
(void)unlink(rpcidFileName);
(void)unlink("./rk3568.sc");
(void)unlink("./newPCID.sc");
(void)unlink("./rpcid.sc");
(void)unlink("./rk3568.json");
@@ -110,6 +112,22 @@ int main(int argc, char **argv)
}
passCnt++;
printf("pass\n");
printf("5.test pcid.json encode to new PCID.sc\n");
ret = CreatePCID("./newPCID.sc", "./");
if (ret != 0) {
printf(" error: new pcid.sc encode failed\n");
exit(passCnt);
}
passCnt++;
printf("pass\n");
printf("6.test new pcid.sc decode to pcid.json\n");
ret = PCIDDecode("./newPCID.sc", "./");
if (ret != 0) {
printf(" error: new pcid.sc decode failed\n");
exit(passCnt);
}
passCnt++;
printf("pass\n");
exit(passCnt);
}
(void)wait(&status);
@@ -117,6 +135,7 @@ int main(int argc, char **argv)
(void)unlink(pcidFileName);
(void)unlink(rpcidFileName);
(void)unlink("./rk3568.sc");
(void)unlink("./newPCID.sc");
(void)unlink("./rpcid.sc");
(void)unlink("./rk3568.json");