Android hal层模块加载流程——以指纹兼容为例

原创文章,转载请注明来处 

1、硬件抽象层的编写规范

Android系统的硬件抽象层以模块的形式来管理各个硬件访问接口。每一个硬件模块都对应有一个动态链接库文件。在系统内部,每一个硬件抽象层模块都使用结构体hw_module_t来描述,而硬件设备则使用结构体hw_device_t来描述

typedef struct fingerprint_module {
    struct hw_module_t common;
} fingerprint_module_t;

#define FINGERPRINT_HARDWARE_MODULE_ID "fingerprint"
#define HAL_MODULE_INFO_SYM_AS_STR "HMI"

fingerprint_module_t HAL_MODULE_INFO_SYM = {
    .common = {
        .tag                	= HARDWARE_MODULE_TAG,
        .module_api_version 	= FINGERPRINT_MODULE_API_VERSION_2_0,
        .hal_api_version    	= HARDWARE_HAL_API_VERSION,
        .id                 	= FINGERPRINT_HARDWARE_MODULE_ID,
        .name                   = "Demo Fingerprint HAL",
        .author             	= "The Android Open Source Project",
        .methods            	= &fingerprint_module_methods,
    },
};

2、硬件抽象层的加载过程

Android中实现调用HAL是通过hw_get_module实现的

int hw_get_module(const char *id, const struct hw_module_t **module)
{
	……
    /* First try a property specific to the class and possibly instance */
    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;
        }
    }
    /* 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) {
            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) {//这里默认加载system/lib/hw/xxx.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); //load函数是关键,调用load函数打开动态链接库
}
static int load(const char *id,
        const char *path,
        const struct hw_module_t **pHmi)
{
……
    handle = dlopen(path, RTLD_NOW);// 打开一个动态链接库,获取其句柄
……
    const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
    hmi = (struct hw_module_t *)dlsym(handle, sym);//根据动态链接库操作句柄与符号,返回符号对应的地址
	……
    /* Check that the id matches */
    if (strcmp(id, hmi->id) != 0) {//与所要求加载的硬件抽象层模块ID对比
        ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
        status = -EINVAL;
        goto done;
    }
	……
}

这里有个宏#define HAL_MODULE_INFO_SYM_AS_STR  "HMI"

其中 hmi = (struct hw_module_t *)dlsym(handle, sym);

这里是查找“HMI”这个导出符号,转换为一个hw_module_t结构体指针,获得了这个hw_module_t结构体指针之后,调用strcmp函数来验证加载得到的硬件抽象层模块ID是否与所要求加载的硬件抽象层模块ID一致。Hal层就是通过这种方式来加载动态库的。

3、hal层指纹库的兼容

知道了库的加载过程之后,对于不用指纹所用的不同库如何兼容这个问题就很好办了。

我们只需将不同指纹库的模块ID分别用不同的名字命名,利用不同指纹设备会在dev底下生成不同的设备节点来判断需要加载哪一个指纹库即可,代码如下

int64_t FingerprintDaemonProxy::openHal() {
    int err;
    const hw_module_t *hw_module = NULL;
    int i;
    const char *fingerprint_id = "fingerprint";
    int len = sizeof(fingerprint_list) / sizeof(fingerprint_dev_t);

    for (i = 0;i < len; i++) {
        if (!access(fingerprint_list[i].dev_name, F_OK)) {
            fingerprint_id = fingerprint_list[i].dev_id;
            property_set("sys.fingerprint.chip", fingerprint_list[i].chip_name);
            ALOG(LOG_VERBOSE, LOG_TAG, "Detect fingerprint id: %s ,chip_name:%s\n", fingerprint_id,fingerprint_list[i].chip_name);
        }
    }

    if (0 != (err = hw_get_module(fingerprint_id, &hw_module))) {
		ALOGE("Can't open fingerprint [%s] HW Module, error: %d", fingerprint_id, err);
        if (0 != (err = hw_get_module(FINGERPRINT_HARDWARE_MODULE_ID, &hw_module))) {
            ALOGE("Can't open fingerprint HW Module, error: %d", err);
            return 0;
        }
    }
    if (NULL == hw_module) {
        ALOGE("No valid fingerprint module");
        return 0;
}

typedef struct fingerprint_dev {
    const char *dev_name;
    const char *dev_id;
    const char *chip_name;
} fingerprint_dev_t;


fingerprint_dev_t fingerprint_list[] = {
    {
        .dev_name = "/dev/goodix_fp",
        .dev_id = "fingerprint",
        .chip_name = "goodix",
    },

    {
        .dev_name = "/dev/bl229x",
        .chip_name = "bl229x",
    },
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Strongbox是一种安全芯片(TEE)的HAL(Hardware Abstraction Layer)实现,用于提供安全的密钥存储和加密功能。下面是Android Strongbox HAL流程概述: 1. 初始化:Strongbox HAL在设备启动时进行初始化,包括与安全芯片建立通信通道,验证芯片的完整性和安全性等。 2. 密钥生成和存储:Strongbox HAL提供API供应用程序生成和存储密钥。在生成密钥时,应用程序可以指定密钥的类型、长度和属性。Strongbox HAL通过与安全芯片进行交互,在芯片内部生成和存储密钥对。 3. 加密和解密:应用程序可以通过Strongbox HAL调用加密和解密API来使用Strongbox芯片执行加密和解密操作。Strongbox HAL将请求传递给安全芯片,由芯片内部的安全执行环境(TEE)负责处理实际的加密和解密操作。 4. 密钥保护和访问控制:Strongbox HAL负责保护存储在Strongbox芯片中的密钥,并提供访问控制机制。只有授权的应用程序才能访问Strongbox芯片中的密钥,并且只能执行特定的操作,如加密、解密或签名。 5. 密钥生命周期管理:Strongbox HAL提供API用于管理密钥的生命周期。应用程序可以调用这些API来生成、导入、删除和销毁Strongbox芯片中的密钥。 6. 安全芯片状态监测:Strongbox HAL负责监测安全芯片的状态,包括验证芯片的完整性和安全性,并提供相应的错误处理机制。 总体而言,Android Strongbox HAL通过与安全芯片进行通信和交互,提供了安全的密钥存储和加密功能。它管理密钥的生成、存储和生命周期,并确保只有授权的应用程序可以访问Strongbox芯片中的密钥。同时,Strongbox HAL负责监测芯片的状态并提供错误处理机制,以确保系统安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值