• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "hcs_dm_parser.h"
17 #include <pthread.h>
18 #include "hcs_blob_if.h"
19 #include "hcs_blob_load.h"
20 #include "hcs_parser.h"
21 #include "hdf_log.h"
22 #include "osal_mem.h"
23 
24 #define HDF_LOG_TAG hcs_dm_parser
25 static char *g_hcsBlob = NULL;
26 static struct DeviceResourceNode *g_hcsTreeRoot = NULL;
27 static const char *g_hcsBlobPath = NULL;
28 static pthread_mutex_t g_getDmRootMutex = PTHREAD_MUTEX_INITIALIZER;
29 
ReleaseHcsTree(void)30 void ReleaseHcsTree(void)
31 {
32     OsalMemFree(g_hcsTreeRoot);
33     g_hcsTreeRoot = NULL;
34     OsalMemFree(g_hcsBlob);
35     g_hcsBlob = NULL;
36 }
37 
SetHcsBlobPath(const char * path)38 void SetHcsBlobPath(const char *path)
39 {
40     if (g_hcsBlobPath != NULL) {
41         OsalMemFree((void *)g_hcsBlobPath);
42     }
43     g_hcsBlobPath = strdup(path);
44 }
45 
CreateHcsToTree(void)46 static bool CreateHcsToTree(void)
47 {
48     bool ret = true;
49     do {
50         uint32_t length = OpenHcsBlobFile(g_hcsBlobPath, &g_hcsBlob);
51         if (length == 0) {
52             ret = false;
53             break;
54         }
55         if (!HcsCheckBlobFormat(g_hcsBlob, length)) {
56             ret = false;
57             break;
58         }
59         if (!HcsDecompile((const char *)g_hcsBlob, HBC_HEADER_LENGTH, &g_hcsTreeRoot)) {
60             ret = false;
61             break;
62         }
63     } while (0);
64 
65     if (!ret) {
66         ReleaseHcsTree();
67     }
68     return ret;
69 }
70 
HcsGetRootNode(void)71 const struct DeviceResourceNode *HcsGetRootNode(void)
72 {
73     if (g_hcsTreeRoot == NULL) {
74         pthread_mutex_lock(&g_getDmRootMutex);
75         if ((g_hcsTreeRoot == NULL) && !CreateHcsToTree()) {
76             pthread_mutex_unlock(&g_getDmRootMutex);
77             HDF_LOGE("%{public}s is failed", __func__);
78             return NULL;
79         }
80         pthread_mutex_unlock(&g_getDmRootMutex);
81     }
82     return g_hcsTreeRoot;
83 }