google爲了保護硬件廠商的信息,在android中添加了一層HAL層。查看HAL的編寫方法的過程當中,發現整個模塊沒有一個入口。通常應用程序有main函數,可讓加載器進行加載執行,而對於動態連接庫,咱們能夠對庫中導出的任何符號進行調用。android
android中的HAL須要上層的函數對其進行加載調用,android的HAL加載器是如何實現對不一樣的Hardware Module進行通用性的調用的呢?數組
查看源碼的過程當中發現android中實現調用HAL是經過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);
}
這是其函數原型,id會指定Hardware的id,這是一個字符串,好比咱們比較熟悉的sensor的id是#define SENSORS_HARDWARE_MODULE_ID "sensors",若是找到了對應的hw_module_t結構體,會將其指針放入*module中。看看它的實現。oop
int hw_get_module_by_class(const char *class_id, const char *inst,
const struct hw_module_t **module)
{
int i;
char prop[PATH_MAX];
char path[PATH_MAX];
char name[PATH_MAX];
char prop_name[PATH_MAX];
if (inst)
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
else
strlcpy(name, class_id, PATH_MAX);//拷貝hardware id,name爲sensor
/*
* Here we rely on the fact that calling dlopen multiple times on
* the same .so will simply increment a refcount (and not load
* a new copy of the library).
* We also assume that dlopen() is thread-safe.
*/
/* First try a property specific to the class and possibly instance */
snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name);//先嚐試獲取ro.hardware.sensor的屬性值
if (property_get(prop_name, prop, NULL) > 0) {
if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
goto found;
}
}
/* Loop through the configuration variants looking for a module */
for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
if (property_get(variant_keys[i], prop, NULL) == 0) {//獲取數組variant_keys裏面的屬性值
continue;
}
if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
goto found;
}
}
/* Nothing found, try the default */
if (hw_module_exists(path, sizeof(path), name, "default") == 0) {//這裏默認加載sensor.default.so
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以下:
static int hw_module_exists(char *path, size_t path_len, const char *name,
const char *subname)
{
snprintf(path, path_len, "%s/%s.%s.so",//查找動態連接庫的兩個路徑 vendor/lib/hw/ system/lib/hw
HAL_LIBRARY_PATH2, name, subname);
if (access(path, R_OK) == 0)
return 0;
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH1, name, subname);
if (access(path, R_OK) == 0)
return 0;
return -ENOENT;
}
variant_keys定義以下:this
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",
"ro.btstack"
};
上述代碼主要是獲取動態連接庫的路徑,並調用load函數去打開指定路徑下的庫文件,load函數是關鍵所在。好,那咱們就來解開load函數的神祕面紗!!!google
static int load(const char *id,
const char *path,
const struct hw_module_t **pHmi)
{
int status;
void *handle;
struct hw_module_t *hmi;
/*
* 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
*/
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就是模塊結構的符號標誌
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;
}
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);
}
*pHmi = hmi;//返回找到的模塊結構,賦值給module
return status;
}
HAL_MODULE_INFO_SYM_AS_STR這個宏很重要:指針
/**
* Name of the hal_module_info as a string
*/
#define HAL_MODULE_INFO_SYM_AS_STR "HMI"
其中 hmi = (struct hw_module_t *)dlsym(handle, sym);這裏是查找「HMI」這個導出符號,並獲取其地址。爲何根據「HMI」這個導出符號,就能夠從動態連接庫中找到結構體hw_module_t呢?
其實,咱們看一下動態連接庫的格式:orm
ELF頭在文件的開始,保存了路線圖,描述了該文件的組織狀況。sections保存着object 文件的信息,從鏈接角度看:包括指令,數據,符號表,重定位信息等等ip
因此說,咱們可使用readelf命令去查看相應的符號信息,就一目瞭然了!ci
365行咱們發現,名字就是「HMI」,對應於hw_module_t結構體。再去對照一下HAL的代碼。
struct sensors_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = SENSORS_HARDWARE_MODULE_ID,
.name = "MTK SENSORS Module",
.author = "Mediatek",
.methods = &sensors_module_methods,
},
.get_sensors_list = sensors__get_sensors_list,
};
這裏定義了一個名爲HAL_MODULE_INFO_SYM的sensors_module_t的結構體,common成員爲hw_module_t類型。注意這裏的HAL_MODULE_INFO_SYM變量必須爲這個名字,這樣編譯器纔會將這個結構體的導出符號變爲「HMI」,這樣這個結構體才能被dlsym函數找到! 如此,咱們知道了andriod HAL模塊也有一個通用的入口地址,這個入口地址就是HAL_MODULE_INFO_SYM變量,經過它,咱們能夠訪問到HAL模塊中的全部想要外部訪問到的方法