一、HAL模块的命名规范
每个模块对应的动态链接库的名字是遵循HAL的命名规范的,这样厂商在开发HAL模块只需要按照这个规范命名就可以了。
硬件抽象模块的动态链接库文件名命名规范定义在:/hardware/libhardware/hardware.c:
static const char *variant_keys[] = {
"ro.hardware", /* This goes first so that it can pick up a different
file on the emulator. */
"ro.product.board",
"ro.board.platform",
"ro.arch"
};
//后面会用到
static const int HAL_VARIANT_KEYS_COUNT =
(sizeof(variant_keys)/sizeof(variant_keys[0]));
HAL会按照variant_keys[]定义的属性名称的顺序逐一来读取属性值prop,若值存在,则查找对应的<MODULE_ID>.<prop>.so是否存在。
如果没有读取到任何属性值,则使用<MODULE_ID>.default.so 作为默认的动态链接库文件名来加载硬件模块。 在代码中,获取上面属性数组里面的属性值的方法是通过property_get(prop_name, prop, NULL)来实现的,通过属性名称得到对应的属性值。
我们也可以通过命令行来查看相应的属性名称的属性值:
adb shell
su
getprop ro.hardware
getprop ro.product.board
getprop ro.board.platform
getprop ro.arch
二、HAL模块的存放路径规范
HAL模块的存放路径是有规范的。HAL规定了2个硬件模块动态共享库的存放路径,定义在/hardware/libhardware/hardware.c:
/** Base path of the hal modules */
#if defined(__LP64__)
#define HAL_LIBRARY_PATH1 "/system/lib64/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib64/hw"
#else
#define HAL_LIBRARY_PATH1 "/system/lib/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
#endif
所以,硬件模块的共享库必须放在/system/lib/hw 或者 /vendor/lib/hw 这2个路径下的其中一个。
HAL在加载所需的共享库的时候,会先检查HAL_LIBRARY_PATH2路径下面是否存在目标库;如果没有,继续检查HAL_LIBRARY_PATH1路径下面是否存在。具体实现在函数hw_module_exists, /hardware/libhardware/hardware.c,这个后面具体分析。
三、HAL模块匹配和加载流程
应用打开HAL库的入口函数为hw_get_module,具体实现在文件hardware/libhardware/hardware.c,下面我们具体来分析。
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)
{
int i = 0;
char prop[PATH_MAX] = {0};
char path[PATH_MAX] = {0};
char name[PATH_MAX] = {0};
char prop_name[PATH_MAX] = {0};
//根据id生成module name,这里inst为NULL
// 假如id为gps,所以name为gps
if (inst)
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
else
strlcpy(name, class_id, PATH_MAX);
// 得到属性名prop_name为ro.hardware.gps
snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name);
// 通过这个属性名称得到对应的属性值
if (property_get(prop_name, prop, NULL) > 0) {
//检查目标模块共享库是否存在
if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
goto found; //存在,找到了
}
}
//逐一查询variant_keys数组定义的属性名称对应的属性值
for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
if (property_get(variant_keys[i], prop, NULL) == 0) {
continue;
}
//检查目标模块共享库是否存在
if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
goto found;
}
}
//没有找到,尝试默认variant名称为default的共享库
/* Nothing found, try the default */
if (hw_module_exists(path, sizeof(path), name, "default") == 0) {
goto found;
}
return -ENOENT;
found:
/* load the module, if this fails, we're doomed, and we should not try
* to load a different variant. */
return load(class_id, path, module); //执行加载和解析共享库的工作
}
下面看看hw_module_exists方法
// path:就是上面指定的两个路径
// name:其实对应上面提到的MODULE_ID,也就是模块名称
// subname: 对应从上面提到的属性值prop
static int hw_module_exists(char *path, size_t path_len, const char *name,
const char *subname)
{
//检查/vendor/lib/hw路径下是否存在目标模块
// 假如模块名称为gps, 属性值为msm8974,所以模块名为:/vendor/lib/hw/gps.msm8974.so
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH2, name, subname);
if (access(path, R_OK) == 0)
return 0;
//检查/system/lib/hw路径下是否存在目标模块
//假如模块名称为gps, 属性值为msm8974,所以模块名为:/system/lib/hw/gps.msm8974.so
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH1, name, subname);
if (access(path, R_OK) == 0)
return 0;
return -ENOENT;
}
找到对应的HAL so库之后,下面就是下载这个库,下面看看load(class_id, path, module)方法。
static int load(const char *id,
const char *path,
const struct hw_module_t **pHmi)
{
int status = -EINVAL;
void *handle = NULL;
struct hw_module_t *hmi = NULL;
/*
* load the symbols resolving undefined symbols before
* dlopen returns. Since RTLD_GLOBAL is not or'd in with
* RTLD_NOW the external symbols will not be global
*/
//使用dlopen打开path定义的目标共享库,得到库文件的句柄handle
handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
//出错,通过dlerror获取错误信息
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"
//使用dlsym找到符号为“HMI”的地址,这里应该是hw_module_t结构体的地址;并且赋给hmi
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 */
//检查模块id是否匹配
if (strcmp(id, hmi->id) != 0) {
ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
status = -EINVAL;
goto done;
}
//保存共享库文件的句柄
hmi->dso = handle;
/* success */
status = 0;
done:
if (status != 0) {
hmi = NULL;
if (handle != NULL) {
dlclose(handle);
handle = NULL;
}
} else {
ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
id, path, *pHmi, handle);
}
//返回得到的hw_module_t结构体的指针
*pHmi = hmi;
return status;
}
参考文献:
https://blog.csdn.net/yangwen123/article/details/12040909
https://www.jianshu.com/p/0d155f267589