xref: /aosp_15_r20/hardware/libhardware/hardware.c (revision e01b6f769022e40d0923dee176e8dc7cd1d52984)
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <hardware/hardware.h>
18 
19 #include <cutils/properties.h>
20 
21 #include <dlfcn.h>
22 #include <string.h>
23 #include <pthread.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 
30 #define LOG_TAG "HAL"
31 #include <log/log.h>
32 
33 #if !defined(__ANDROID_RECOVERY__) && defined(__ANDROID__)
34 #include <vndksupport/linker.h>
35 #endif
36 
37 #ifdef __ANDROID_APEX__
38 #include <android/apexsupport.h>
39 #endif
40 
41 /** Base path of the hal modules */
42 #if defined(__LP64__)
43 #define HAL_LIBRARY_SUBDIR "lib64/hw"
44 #else
45 #define HAL_LIBRARY_SUBDIR "lib/hw"
46 #endif
47 
48 #define HAL_LIBRARY_PATH1 "/system/" HAL_LIBRARY_SUBDIR
49 #define HAL_LIBRARY_PATH2 "/vendor/" HAL_LIBRARY_SUBDIR
50 #define HAL_LIBRARY_PATH3 "/odm/" HAL_LIBRARY_SUBDIR
51 
52 /**
53  * There are a set of variant filename for modules. The form of the filename
54  * is "<MODULE_ID>.variant.so" so for the led module the Dream variants
55  * of base "ro.product.board", "ro.board.platform" and "ro.arch" would be:
56  *
57  * led.trout.so
58  * led.msm7k.so
59  * led.ARMV6.so
60  * led.default.so
61  */
62 
63 static const char *variant_keys[] = {
64     "ro.hardware",  /* This goes first so that it can pick up a different
65                        file on the emulator. */
66     "ro.product.board",
67     "ro.board.platform",
68     "ro.arch"
69 };
70 
71 static const int HAL_VARIANT_KEYS_COUNT =
72     (sizeof(variant_keys)/sizeof(variant_keys[0]));
73 
74 /**
75  * Load the file defined by the variant and if successful
76  * return the dlopen handle and the hmi.
77  * @return 0 = success, !0 = failure.
78  */
load(const char * id,const char * path,const struct hw_module_t ** pHmi)79 static int load(const char *id,
80         const char *path,
81         const struct hw_module_t **pHmi)
82 {
83     int status = -EINVAL;
84     void *handle = NULL;
85     struct hw_module_t *hmi = NULL;
86 #if defined(__ANDROID_VNDK__) || defined(__ANDROID_APEX__)
87     const bool try_system = false;
88 #else
89     const bool try_system = true;
90 #endif
91 
92     /*
93      * load the symbols resolving undefined symbols before
94      * dlopen returns. Since RTLD_GLOBAL is not or'd in with
95      * RTLD_NOW the external symbols will not be global
96      */
97     if (try_system &&
98         strncmp(path, HAL_LIBRARY_PATH1, strlen(HAL_LIBRARY_PATH1)) == 0) {
99         /* If the library is in system partition, no need to check
100          * sphal namespace. Open it with dlopen.
101          */
102         handle = dlopen(path, RTLD_NOW);
103     } else {
104 #if defined(__ANDROID_RECOVERY__) || !defined(__ANDROID__) || defined(__ANDROID_APEX__)
105         handle = dlopen(path, RTLD_NOW);
106 #else
107         handle = android_load_sphal_library(path, RTLD_NOW);
108 #endif
109     }
110     if (handle == NULL) {
111         char const *err_str = dlerror();
112         ALOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
113         status = -EINVAL;
114         goto done;
115     }
116 
117     /* Get the address of the struct hal_module_info. */
118     const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
119     hmi = (struct hw_module_t *)dlsym(handle, sym);
120     if (hmi == NULL) {
121         ALOGE("load: couldn't find symbol %s", sym);
122         status = -EINVAL;
123         goto done;
124     }
125 
126     /* Check that the id matches */
127     if (strcmp(id, hmi->id) != 0) {
128         ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
129         status = -EINVAL;
130         goto done;
131     }
132 
133     hmi->dso = handle;
134 
135     /* success */
136     status = 0;
137 
138     done:
139     if (status != 0) {
140         hmi = NULL;
141         if (handle != NULL) {
142             dlclose(handle);
143             handle = NULL;
144         }
145     } else {
146         ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
147                 id, path, hmi, handle);
148     }
149 
150     *pHmi = hmi;
151 
152     return status;
153 }
154 
155 /*
156  * If path is in in_path.
157  */
path_in_path(const char * path,const char * in_path)158 static bool __attribute__ ((unused)) path_in_path(const char *path, const char *in_path) {
159     char real_path[PATH_MAX];
160     if (realpath(path, real_path) == NULL) return false;
161 
162     char real_in_path[PATH_MAX];
163     if (realpath(in_path, real_in_path) == NULL) return false;
164 
165     const size_t real_in_path_len = strlen(real_in_path);
166     if (strncmp(real_path, real_in_path, real_in_path_len) != 0) {
167         return false;
168     }
169 
170     return strlen(real_path) > real_in_path_len &&
171         real_path[real_in_path_len] == '/';
172 }
173 
174 /*
175  * Check if a HAL with given name and subname exists, if so return 0, otherwise
176  * otherwise return negative.  On success path will contain the path to the HAL.
177  */
hw_module_exists(char * path,size_t path_len,const char * name,const char * subname)178 static int hw_module_exists(char *path, size_t path_len, const char *name,
179                             const char *subname)
180 {
181 #ifdef __ANDROID_APEX__
182     // When used in VAPEX, it should look only into the same APEX because
183     // libhardware modules don't provide ABI stability.
184     if (__builtin_available(android __ANDROID_API_V__, *)) {
185         AApexInfo *apex_info;
186         if (AApexInfo_create(&apex_info) == AAPEXINFO_OK) {
187             snprintf(path, path_len, "/apex/%s/%s/%s.%s.so",
188                     AApexInfo_getName(apex_info), HAL_LIBRARY_SUBDIR, name, subname);
189             AApexInfo_destroy(apex_info);
190             if (access(path, R_OK) == 0)
191                 return 0;
192         }
193     }
194 #else // __ANDROID_APEX__
195     snprintf(path, path_len, "%s/%s.%s.so",
196              HAL_LIBRARY_PATH3, name, subname);
197     if (path_in_path(path, HAL_LIBRARY_PATH3) && access(path, R_OK) == 0)
198         return 0;
199 
200     snprintf(path, path_len, "%s/%s.%s.so",
201              HAL_LIBRARY_PATH2, name, subname);
202     if (path_in_path(path, HAL_LIBRARY_PATH2) && access(path, R_OK) == 0)
203         return 0;
204 
205 #ifndef __ANDROID_VNDK__
206     snprintf(path, path_len, "%s/%s.%s.so",
207              HAL_LIBRARY_PATH1, name, subname);
208     if (path_in_path(path, HAL_LIBRARY_PATH1) && access(path, R_OK) == 0)
209         return 0;
210 #endif
211 
212 #endif // __ANDROID_APEX__
213 
214     return -ENOENT;
215 }
216 
hw_get_module_by_class(const char * class_id,const char * inst,const struct hw_module_t ** module)217 int hw_get_module_by_class(const char *class_id, const char *inst,
218                            const struct hw_module_t **module)
219 {
220     int i = 0;
221     char prop[PATH_MAX] = {0};
222     char path[PATH_MAX] = {0};
223     char name[PATH_MAX] = {0};
224     char prop_name[PATH_MAX] = {0};
225 
226 
227     if (inst)
228         snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
229 #if defined(__ANDROID__)
230     else
231         strlcpy(name, class_id, PATH_MAX);
232 #else
233     else
234         snprintf(name, PATH_MAX, "%s", class_id);
235 #endif
236 
237     /*
238      * Here we rely on the fact that calling dlopen multiple times on
239      * the same .so will simply increment a refcount (and not load
240      * a new copy of the library).
241      * We also assume that dlopen() is thread-safe.
242      */
243 
244     /* First try a property specific to the class and possibly instance */
245     snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name);
246     if (property_get(prop_name, prop, NULL) > 0) {
247         if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
248             goto found;
249         }
250     }
251 
252     /* Loop through the configuration variants looking for a module */
253     for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
254         if (property_get(variant_keys[i], prop, NULL) == 0) {
255             continue;
256         }
257         if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
258             goto found;
259         }
260     }
261 
262     /* Nothing found, try the default */
263     if (hw_module_exists(path, sizeof(path), name, "default") == 0) {
264         goto found;
265     }
266 
267     return -ENOENT;
268 
269 found:
270     /* load the module, if this fails, we're doomed, and we should not try
271      * to load a different variant. */
272     return load(class_id, path, module);
273 }
274 
hw_get_module(const char * id,const struct hw_module_t ** module)275 int hw_get_module(const char *id, const struct hw_module_t **module)
276 {
277     return hw_get_module_by_class(id, NULL, module);
278 }
279