Android HAL層庫加載原理
Android HAL層的由來:由於市面做移動芯片的廠商很多,大部分廠商考慮到自己硬件的設計架構、安全、專利等方面原因,不願意公開自己的這方面代碼,也出於不同廠商硬件架構不太一樣,適配開發難度週期長,GOOGLE在kernel之上加了一個HAL層,只要各個廠商實現Android 所需要的功能接口,可以以庫的方式提供不用開源。
問題來了,android如何實現針對不同的Hardware Module進行通用性調用的呢?
以加載camera HAL層庫爲例:
#define CAMERA_HARDWARE_NODULE_ID "camera"
首先在 void CameraService::onFirstRef()
{
............略..............
camera_module_t *rawModule;
int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID, (const hw_module_t **)&rawModule);
............略..............
}
通過hw_get_module()函數以CAMERA_HARDWARE_MODULE_ID 參數獲得 camera_module_t 指針來初始化和調用CMAERA 我們再看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);
}
而hw_get_module()又是通過 hw_get_module_by_class():
int hw_get_module_by_class(const char *class_id, const char *inst,
const struct hw_module_t **module)
{
.................核心看......................
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;
}
.................................................................................
}
有load函數可發現,它是通過dlopen()加載camera.so庫,通過dlsym()查詢HAL_MODULE_INFO_SYM_AS_STR全局變量的地址,通過強制指針轉換可以獲得HAL_MODULE_INFO_SYM_AS_STR變量,並檢查傳進來的id和獲得id是否一致。HAL_MODULE_INFO_SYM_AS_STR是個宏定義如下:
#define HAL_MODULE_INFO_SYM_AS_STR "HMI"
也就是說camera.so庫中肯定有一個名字爲HMI 數據類型爲struct hw_module_t的全局變量。
我們通過linux自帶的readelf工具查看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
............................................................................................................................
可以看出來確實存在一個全局類型爲OBJECT 名字爲HMI的變量
那這個變量誰定義的呢?
我們看到hardware/qcom/camera/qcamera2/QCamera2Hal.cpp有這樣的定義:
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,
.reserved = {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,
.reserved = {0}
};
可以看到有一個static hw_module_t camera_common變量的定義,就是我們通過dlsym()要獲取的變量類型,但是這個變量也不叫 HMI 啊,我們再看這變量是被camera_module_t HAL_MODULE_INFO_SYM使用的,我們來看這個結構體定義:
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;
可以看出來 hw_module_t 是這個結構體的第一個變量,這樣定義有個好處,可以實現類似C++的繼承效果,camera_module繼承於hw_module_t,可以通過hw_module_t控制camera_module,所以外面理論上應該通過HAL_MODULE_INFO_SYM 獲得hw_module_t的,全局搜索HAL_MODULE_INFO_SYM 發現所有的HAL層模塊都會有一個HAL_MODULE_INFO_SYM的定義,還發現HAL_MODULE_INFO_SYM其實是個宏定義:
#define HAL_MODULE_INFO_SYM HMI
也就是說
camera_module_t HAL_MODULE_INFO_SYM = {
編譯解釋爲:
camera_module_t HMI = {
這樣整個HAL層調用通用性就解釋的通了。