• 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 "driver_loader_full.h"
17 #include <dlfcn.h>
18 #include <errno.h>
19 #include <unistd.h>
20 #include <stdlib.h>
21 #include <limits.h>
22 #include "hdf_device.h"
23 #include "dev_attribute_serialize.h"
24 #include "hdf_device_node.h"
25 #include "hdf_log.h"
26 #include "hdf_object_manager.h"
27 #include "osal_mem.h"
28 #include "securec.h"
29 
30 #define DRIVER_DESC "driverDesc"
31 #define HDF_LOG_TAG driver_loader_full
32 #ifdef __ARM64__
33 #define DRIVER_PATH HDF_LIBRARY_DIR"64/"
34 #else
35 #define DRIVER_PATH HDF_LIBRARY_DIR"/"
36 #endif
37 
38 static struct DriverLoaderFull *g_fullLoader = NULL;
39 
HdfDriverLoaderGetDriver(const char * moduleName)40 struct HdfDriver *HdfDriverLoaderGetDriver(const char *moduleName)
41 {
42     char realPath[PATH_MAX] = { 0 };
43     char driverPath[PATH_MAX] = { 0 };
44     if (moduleName == NULL) {
45         return NULL;
46     }
47 
48     if (strcat_s(driverPath, sizeof(driverPath) - 1, DRIVER_PATH) != EOK) {
49         return NULL;
50     }
51 
52     if (strcat_s(driverPath, (sizeof(driverPath) - 1 - sizeof(DRIVER_PATH)), moduleName) != EOK) {
53         return NULL;
54     }
55 
56     if (realpath(driverPath, realPath) == NULL) {
57         HDF_LOGE("%{public}s no valid, errno:%{public}d", driverPath, errno);
58         return NULL;
59     }
60 
61     struct HdfDriver *driver = OsalMemAlloc(sizeof(struct HdfDriver));
62     if (driver == NULL) {
63         return NULL;
64     }
65 
66     void *handle = dlopen(realPath, RTLD_LAZY);
67     if (handle == NULL) {
68         HDF_LOGE("get driver entry failed, %{public}s load fail, %{public}s", realPath, dlerror());
69         OsalMemFree(driver);
70         return NULL;
71     }
72 
73     struct HdfDriverEntry **driverEntry = (struct HdfDriverEntry **)dlsym(handle, DRIVER_DESC);
74     if (driverEntry == NULL) {
75         HDF_LOGE("driver entry %{public}s dlsym failed", realPath);
76         dlclose(handle);
77         OsalMemFree(driver);
78         return NULL;
79     }
80 
81     driver->entry = *driverEntry;
82     driver->priv = handle;
83 
84     return driver;
85 }
86 
HdfDriverLoaderFullReclaimDriver(struct HdfDriver * driver)87 void HdfDriverLoaderFullReclaimDriver(struct HdfDriver *driver)
88 {
89     if (driver == NULL) {
90         return;
91     }
92 
93     if (driver->priv != NULL) {
94         dlclose(driver->priv);
95         driver->priv = NULL;
96     }
97 
98     OsalMemFree(driver);
99 }
100 
101 
HdfDriverLoaderFullConstruct(struct DriverLoaderFull * inst)102 void HdfDriverLoaderFullConstruct(struct DriverLoaderFull *inst)
103 {
104     struct HdfDriverLoader *pvtbl = (struct HdfDriverLoader *)inst;
105     pvtbl->super.GetDriver = HdfDriverLoaderGetDriver;
106     pvtbl->super.ReclaimDriver = HdfDriverLoaderFullReclaimDriver;
107 }
108 
HdfDriverLoaderFullCreate()109 struct HdfObject *HdfDriverLoaderFullCreate()
110 {
111     if (g_fullLoader == NULL) {
112         struct DriverLoaderFull *instance =
113             (struct DriverLoaderFull *)OsalMemCalloc(sizeof(struct DriverLoaderFull));
114         if (instance != NULL) {
115             HdfDriverLoaderFullConstruct(instance);
116             g_fullLoader = instance;
117         }
118     }
119     return (struct HdfObject *)g_fullLoader;
120 }
121 
HdfDriverLoaderFullRelease(struct HdfObject * object)122 void HdfDriverLoaderFullRelease(struct HdfObject *object)
123 {
124     struct DriverLoaderFull *instance = (struct DriverLoaderFull *)object;
125     if (instance == g_fullLoader) {
126         g_fullLoader = NULL;
127     }
128     if (instance != NULL) {
129         OsalMemFree(instance);
130     }
131 }
132 
HdfDriverLoaderGetInstance(void)133 struct IDriverLoader *HdfDriverLoaderGetInstance(void)
134 {
135     static struct IDriverLoader *instance = NULL;
136     if (instance == NULL) {
137         instance = (struct IDriverLoader *)HdfObjectManagerGetObject(HDF_OBJECT_ID_DRIVER_LOADER);
138     }
139     return instance;
140 }
141