1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "hcs_parser.h"
10 #include "hcs_blob_if.h"
11 #include "hcs_generate_tree.h"
12 #include "hdf_log.h"
13 #include "osal_mem.h"
14
15 #define HDF_LOG_TAG hcs_parser
16
GetHcsTreeSize(const char * blob,int32_t nodeLength)17 static int32_t GetHcsTreeSize(const char *blob, int32_t nodeLength)
18 {
19 return GenerateCfgTree(blob, nodeLength, NULL, NULL);
20 }
21
HcsDecompile(const char * hcsBlob,uint32_t offset,struct DeviceResourceNode ** root)22 bool HcsDecompile(const char *hcsBlob, uint32_t offset, struct DeviceResourceNode **root)
23 {
24 int32_t nodeLength = HcsGetNodeLength(hcsBlob + offset);
25 int32_t treeMemLength;
26 char *treeMem = NULL;
27 int32_t treeLayer;
28 if (nodeLength < 0) {
29 HDF_LOGE("%s failed, HcsGetNodeLength error", __func__);
30 return false;
31 }
32
33 treeMemLength = GetHcsTreeSize(hcsBlob + offset, nodeLength);
34 if (treeMemLength <= 0) {
35 HDF_LOGE("%s failed, GetHcsTreeSize error, treeMemLength = %d", __func__, treeMemLength);
36 return false;
37 }
38
39 treeMem = (char *)OsalMemCalloc(treeMemLength);
40 if (treeMem == NULL) {
41 HDF_LOGE("%s failed, OsalMemCalloc error", __func__);
42 return false;
43 }
44 treeLayer = GenerateCfgTree(hcsBlob + offset, nodeLength, treeMem, root);
45 if (treeLayer <= 0) {
46 HDF_LOGE("%s failed, the treeLayer is %d", __func__, treeLayer);
47 OsalMemFree(treeMem);
48 *root = NULL;
49 return false;
50 }
51 return true;
52 }