Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the loading principle of HAL layer library in Android

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about the loading principle of the HAL layer library in Android, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article, without saying much, follow the editor to have a look.

The origin of the Android HAL layer: as there are many manufacturers of mobile chips in the market, most of them are reluctant to disclose their own code in this respect, considering the design architecture, security, patents and other reasons of their own hardware, and because the hardware architecture of different manufacturers is different, and the adaptation development cycle is long, GOOGLE adds a HAL layer on top of the kernel, as long as each manufacturer implements the functional interface required by Android. It can be provided as a library without open source.

The question is, how does android implement universal calls to different Hardware Module?

Take loading the camera HAL layer library as an example:

# define CAMERA_HARDWARE_NODULE_ID "camera"

First, in void CameraService::onFirstRef ()

{

. A little.

Camera_module_t * rawModule

Int err = hw_get_module (CAMERA_HARDWARE_MODULE_ID, (const hw_module_t * *) & rawModule)

. A little.

}

Through the hw_get_module () function to get the camera_module_t pointer with the CAMERA_HARDWARE_MODULE_ID parameter to initialize and call CMAERA, let's look at the implementation of hw_get_module ():

Int hw_get_module (const char * id, const struct hw_module_t * module)

{

Return hw_get_module_by_class (id, NULL, module)

}

And hw_get_module () is through hw_get_module_by_class ():

Int hw_get_module_by_class (const char * class_id, const char * inst

Const struct hw_module_t * * module)

{

. Look at the core.

Return load (class_id, path, module)

}

Static int load (const char * id,const char * path,const struct hw_module_t * pHmi)

{

.

Handle = dlopen (path, RTLD_NOW)

If (handle = = NULL) {

Char const * err_str = dlerror ()

ALOGE ("load: module=%s\ n% s", path, err_str?err_str: "unknown")

Status =-EINVAL

Goto done

}

/ * Get the address of the struct hal_module_info. , /

Const char * sym = HAL_MODULE_INFO_SYM_AS_STR

Hmi = (struct hw_module_t *) dlsym (handle, sym)

If (hmi = = NULL) {

ALOGE ("load: couldn't find symbol% s", sym)

Status =-EINVAL

Goto done

}

/ * Check that the id matches * /

If (strcmp (id, hmi- > id)! = 0) {

ALOGE ("load: id=%s! = hmi- > id=%s", id, hmi- > id)

Status =-EINVAL

Goto done

}

There is a load function to find that it loads the camera.so library through dlopen (), queries the address of the HAL_MODULE_INFO_SYM_AS_STR global variable through dlsym (), obtains the HAL_MODULE_INFO_SYM_AS_STR variable through forced pointer conversion, and checks whether the incoming id is consistent with the obtained id. HAL_MODULE_INFO_SYM_AS_STR is a macro defined as follows:

# define HAL_MODULE_INFO_SYM_AS_STR "HMI"

In other words, there must be a global variable in the camera.so library with the name HMI data type struct hw_module_t.

We use the readelf tool that comes with linux to view the symbol table of camera.so:

JEFF$ readelf-s camera.msm8937.so

Symbol table '.dynsym' contains 336 entries:

Num: Value Size Type Bind Vis Ndx Name

0: 00000000 0 NOTYPE LOCAL DEFAULT UND

1: 00000000 0 FUNC GLOBAL DEFAULT UND _ cxa_finalize@LIBC (2)

2: 00000000 0 FUNC GLOBAL DEFAULT UND _ cxa_atexit@LIBC (2)

3: 00000000 0 FUNC GLOBAL DEFAULT UND _ register_atfork@LIBC (2)

4: 00000000 0 FUNC GLOBAL DEFAULT UND _ aeabi_memcpy8@LIBC_N (3)

5: 00000000 0 FUNC GLOBAL DEFAULT UND _ _ android_log_print

6: 00000000 0 FUNC GLOBAL DEFAULT UND _ gnu_Unwind_Find_exidx@LIBC_N (3)

7: 00000000 0 FUNC GLOBAL DEFAULT UND dladdr@LIBC (4)

.

187: 000112c7 16 OBJECT GLOBAL DEFAULT 15 _ ZN7android21SunmiCameraP

188: 00010eaa 17 OBJECT GLOBAL DEFAULT 15 _ ZN7android21SunmiCameraP

189: 00006a25 28 FUNC GLOBAL DEFAULT 13 start_recording

190: 00012af8 44 OBJECT WEAK DEFAULT 18 _ ZTVN7android12SortedVect

191: 0001134b 13 OBJECT GLOBAL DEFAULT 15 _ ZN7android21SunmiCameraP

192: 00006ab1 28 FUNC GLOBAL DEFAULT 13 cancel_auto_focus

193: 0001120d 10 OBJECT GLOBAL DEFAULT 15 _ ZN7android21SunmiCameraP

194: 00013158 176 OBJECT GLOBAL DEFAULT 23 HMI

195: 00006901 40 FUNC GLOBAL DEFAULT 13 get_camera_info

196: 00010e9a 16 OBJECT GLOBAL DEFAULT 15 _ ZN7android21SunmiCameraP

197: 0001114c 7 OBJECT GLOBAL DEFAULT 15 _ ZN7android21SunmiCameraP

198: 0000af79 20 FUNC GLOBAL DEFAULT 13 _ Z11mjpegDecodeiiPciS_i

199: 00006b05 28 FUNC GLOBAL DEFAULT 13 set_parameters

200: 0000791d 40 FUNC WEAK DEFAULT 13 _ ZNK7android12SortedVecto

201: 0001109b 9 OBJEC

You can see that there is indeed a variable with global type OBJECT and name HMI.

So who defined this variable?

We see that hardware/qcom/camera/qcamera2/QCamera2Hal.cpp has this definition:

Static hw_module_t camera_common = {

.tag = HARDWARE_MODULE_TAG

.module _ api_version = CAMERA_MODULE_API_VERSION_2_4

.hal _ api_version = HARDWARE_HAL_API_VERSION

.id = CAMERA_HARDWARE_MODULE_ID

.name = "QCamera Module"

.author = "Qualcomm Innovation Center Inc"

.methods = & qcamera::QCamera2Factory::mModuleMethods

.dso = NULL

.customers = {0}

}

Camera_module_t HAL_MODULE_INFO_SYM = {

.common = camera_common

.get _ number_of_cameras = qcamera::QCamera2Factory::get_number_of_cameras

.get _ camera_info = qcamera::QCamera2Factory::get_camera_info

.set _ callbacks = qcamera::QCamera2Factory::set_callbacks

.get _ vendor_tag_ops = qcamera::QCamera3VendorTags::get_vendor_tag_ops

.open _ legacy = qcamera::QCamera2Factory::open_legacy

.set _ torch_mode = qcamera::QCamera2Factory::set_torch_mode

.init = NULL

.customers = {0}

}

Typedef struct camera_module {

Hw_module_t common

Int (* get_number_of_cameras) (void)

Int (* get_camera_info) (int camera_id, struct camera_info * info)

Int (* set_callbacks) (const camera_module_callbacks_t * callbacks)

Void (* get_vendor_tag_ops) (vendor_tag_ops_t* ops)

Int (* open_legacy) (const struct hw_module_t* module, const char* id)

Uint32_t halVersion, struct hw_device_t** device)

Int (* set_torch_mode) (const char* camera_id, bool enabled)

Int (* init) ()

Void* reserved [5]

} camera_module_t

You can see that hw_module_t is the first variable of this structure. This definition has an advantage, which is similar to the inheritance effect of C++. Camera_module inherits from hw_module_t, and you can control camera_module through hw_module_t, so in theory, you should get hw_module_t through HAL_MODULE_INFO_SYM. A global search of HAL_MODULE_INFO_SYM finds that all HAL layer modules have a definition of HAL_MODULE_INFO_SYM, and that HAL_MODULE_INFO_SYM is actually a macro definition:

# define HAL_MODULE_INFO_SYM HMI

one

That is to say

Camera_module_t HAL_MODULE_INFO_SYM = {

one

Compilation is interpreted as:

Camera_module_t HMI = {

one

In this way, the generality of invocation in the entire HAL layer makes sense.

The above is the loading principle of the HAL layer library in Android. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report