hw_get_module详解

本文解析了Android系统中HAL层模块的加载过程,重点介绍了hw_get_module函数如何根据ID找到对应的HAL模块,并通过load函数完成模块加载。
hw_get_module是jni层获取HAL层module的接口函数,原型为:int hw_get_module(const char *id, const struct hw_module_t **module)
这个id是hal层注册时加入的,例如sensor的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 = "The Android Open Source Project",
        .methods = &sensors_module_methods,
    },
    .get_sensors_list = sensors__get_sensors_list,
};
#define SENSORS_HARDWARE_MODULE_ID "sensors"
hw_module_t是硬件模块结构,是HAL层的灵魂


下面来看hw_get_module是怎么去抓相应的HAL层硬件模块的
    for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
        if (i < HAL_VARIANT_KEYS_COUNT) {
            if (property_get(variant_keys[i], prop, NULL) == 0) {
                continue;
            }/*获取ro.hardware/ro.product.board/ro.board.platform/ro.arch等key的值*/
            snprintf(path, sizeof(path), "%s/%s.%s.so",
                     HAL_LIBRARY_PATH2, name, prop);/*这个name是之前传进来的id,在我们这里就是"sensor",prop为之前抓取到的key信息,这里为mt6575*/
            if (access(path, R_OK) == 0) break;     /*下面的这几个函数在给定的几个目录中搜索我们要加载的库sensors.mt6575.so*/


            snprintf(path, sizeof(path), "%s/%s.%s.so",
                     HAL_LIBRARY_PATH1, name, prop);
            if (access(path, R_OK) == 0) break;


            snprintf(path, sizeof(path), "%s/%s.%s.so",
                     HAL_LIBRARY_PATH3, name, prop);
            if (access(path, R_OK) == 0) break;
        } else { /*如果上面没有收到,则用默认库*/
            snprintf(path, sizeof(path), "%s/%s.default.so",
                     HAL_LIBRARY_PATH1, name);
            if (access(path, R_OK) == 0) break;


            snprintf(path, sizeof(path), "%s/%s.default.so",
                     HAL_LIBRARY_PATH3, name);
            if (access(path, R_OK) == 0) break;
        }
    }
    status = -ENOENT;
    if (i < HAL_VARIANT_KEYS_COUNT+1) {
        /* load the module, if this fails, we're doomed, and we should not try
         * to load a different variant. */
        status = load(class_id, path, module);
    }
找到这次需要加载的库后,即调用load函数
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();
        LOGE("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);/*获取hw_module_t 结构体*/
    if (hmi == NULL) {
        LOGE("load: couldn't find symbol %s", sym);
        status = -EINVAL;
        goto done;
    }
    /* Check that the id matches */
    if (strcmp(id, hmi->id) != 0) {/*对比hw_module_t结构体总的id和我们传进来的id是否一致,不一致的话直接退出*/
        LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
        status = -EINVAL;
        goto done;
    }
    hmi->dso = handle;
    /* success */
    status = 0;
    *pHmi = hmi;/*返回结果*/
    return status;
}


通过readelf x.so来看看这个库的地址布局
Symbol table '.dynsym' contains 40 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 000021d8   340 OBJECT  GLOBAL DEFAULT   15 sSensorList
     2: 00000000     0 FUNC    GLOBAL DEFAULT  UND __aeabi_unwind_cpp_pr0
     3: 00000000     0 FUNC    GLOBAL DEFAULT  UND __android_log_print
     4: 00000000     0 FUNC    GLOBAL DEFAULT  UND ioctl
     5: 00000000     0 FUNC    GLOBAL DEFAULT  UND __aeabi_ldivmod
     6: 00000000     0 FUNC    GLOBAL DEFAULT  UND __errno
     7: 00000000     0 FUNC    GLOBAL DEFAULT  UND __aeabi_unwind_cpp_pr1
     8: 00000000     0 FUNC    GLOBAL DEFAULT  UND malloc
     9: 00000000     0 FUNC    GLOBAL DEFAULT  UND memset
    10: 00000000     0 FUNC    GLOBAL DEFAULT  UND opendir
    11: 00000000     0 FUNC    GLOBAL DEFAULT  UND strcpy
    12: 00000000     0 FUNC    GLOBAL DEFAULT  UND strlen
    13: 00000000     0 FUNC    GLOBAL DEFAULT  UND open
    14: 00000000     0 FUNC    GLOBAL DEFAULT  UND strcmp
    15: 00000000     0 FUNC    GLOBAL DEFAULT  UND close
    16: 00000000     0 FUNC    GLOBAL DEFAULT  UND readdir
    17: 00000000     0 FUNC    GLOBAL DEFAULT  UND closedir
    18: 00000000     0 FUNC    GLOBAL DEFAULT  UND strerror
    19: 00000000     0 FUNC    GLOBAL DEFAULT  UND __stack_chk_fail
    20: 00000000     0 OBJECT  GLOBAL DEFAULT  UND __stack_chk_guard
    21: 00000000     0 FUNC    GLOBAL DEFAULT  UND free
    22: 00000000     0 FUNC    GLOBAL DEFAULT  UND memcpy
    23: 00000000     0 FUNC    GLOBAL DEFAULT  UND read
    24: 00002150   132 OBJECT  GLOBAL DEFAULT   15 HMI
    25: 00000000     0 FUNC    GLOBAL DEFAULT  UND __cxa_finalize
    26: 00002330     0 NOTYPE  GLOBAL DEFAULT   16 __dso_handle
    27: 00002000     0 NOTYPE  GLOBAL DEFAULT   11 __INIT_ARRAY__
    28: 00002008     0 NOTYPE  GLOBAL DEFAULT   12 __FINI_ARRAY__
    29: 000013d0     0 NOTYPE  GLOBAL DEFAULT  ABS __exidx_end
    30: 00001388     0 NOTYPE  GLOBAL DEFAULT  ABS __exidx_start
    31: 00002150     0 NOTYPE  GLOBAL DEFAULT   15 __data_start
    32: 0000232c     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
    33: 0000232c     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
    34: 0000232c     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start__
    35: 00002340     0 NOTYPE  GLOBAL DEFAULT  ABS _bss_end__
    36: 00002340     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_end__
    37: 00002340     0 NOTYPE  GLOBAL DEFAULT  ABS __end__
    38: 00002340     0 NOTYPE  GLOBAL DEFAULT  ABS _end
    39: 00080000     0 NOTYPE  GLOBAL DEFAULT  ABS _stack
上面的"HMI"段就是我们之前定义的,即是hw_module_t结构体,每个hw_module_t的定义中名字都叫HAL_MODULE_INFO_SYM
实际上他还是由宏定义为"HMI"的


所以综上,可以得出,一个HAL模块也有一个入口地址,就是我们这里的hw_module_t结构体,通过它就可以把我们定义的接口给上层使用
S32 origin_info_init(ORIGIN_FW_INFO *origin_info) { TP_HEAD_INFO origin_tp_head; FACTORY_INFO_DATA fac_info; U32 size; S32 ret; #ifdef CONFIG_LTE_MODULE_IN_UP_BIN MODULE_SPEC module_spec; LTE_PARAM_MODULE_INFO lte_module_info; #endif if (NULL == origin_info) { goto INIT_ERROR; } memset(origin_info, 0, sizeof(ORIGIN_FW_INFO)); memset(&origin_tp_head, 0, sizeof(TP_HEAD_INFO)); memset(&fac_info, 0, sizeof(FACTORY_INFO_DATA)); #ifdef CONFIG_LTE_MODULE_IN_UP_BIN memset(&module_spec, 0, sizeof(MODULE_SPEC)); memset(&lte_module_info, 0, sizeof(LTE_PARAM_MODULE_INFO)); #endif /* 读取 TP HEAD */ size = ds_read(TP_HEADER_PATH, &origin_tp_head, sizeof(TP_HEAD_INFO)); if (size < sizeof(TP_HEAD_INFO)) { UPGRADE_ERROR("Reading TP_HEAD failed."); goto INIT_ERROR; } /* 读取 fac_info */ size = ds_read(FACTORY_INFO_DATA_PATH, &fac_info, sizeof(FACTORY_INFO_DATA)); if (size < sizeof(FACTORY_INFO_DATA)) { UPGRADE_ERROR("Reading FACTORY_INFO_DATA failed."); goto INIT_ERROR; } /* init local partition offsets and lens */ origin_info->tphead_len = ntohl(origin_tp_head.tphead_len); #ifdef FDT_UPGRADE_SUPPORT origin_info->fdt_offset = ntohl(origin_tp_head.fdt_offset); //fdt origin_info->fdt_len = ntohl(origin_tp_head.fdt_len); #else origin_info->fdt_offset = 0; //fdt origin_info->fdt_len = 0; #endif origin_info->uboot_offset = ntohl(origin_tp_head.uboot_offset); origin_info->uboot_len = ntohl(origin_tp_head.uboot_len); origin_info->tphead_offset = ntohl(origin_tp_head.tphead_offset); origin_info->kernel_offset = ntohl(origin_tp_head.kernel_offset); origin_info->vendor_id = ntohs(origin_tp_head.vendor_id); origin_info->zone_code = ntohs(origin_tp_head.zone_code); /* init local partition CRCs */ #ifdef FDT_UPGRADE_SUPPORT memcpy(origin_info->fdt_crc, origin_tp_head.fdt_crc, CRC_LEN); //fdt #endif memcpy(origin_info->factory_info_crc, origin_tp_head.factory_info_crc, CRC_LEN); memcpy(origin_info->radio_crc, origin_tp_head.radio_crc, CRC_LEN); memcpy(origin_info->bootloader_crc, origin_tp_head.uboot_crc, CRC_LEN); memcpy(origin_info->kernel_and_romfs_crc, origin_tp_head.firmware_crc, CRC_LEN); memcpy(origin_info->fw_id, origin_tp_head.fw_id, FW_ID_LEN); /* get BL FwID number and init BL */ origin_info->bl_num = ntohl(origin_tp_head.fw_id_bl_num); memcpy(origin_info->bl_id, origin_tp_head.fw_id_bl, FW_ID_LEN * origin_info->bl_num); /* init hwid */ ret = hex_str_to_bytes(origin_info->hw_id, (char*)fac_info.hw_id, HW_ID_LEN); if (0 != ret) { goto INIT_ERROR; } /* init mac */ memcpy(origin_info->mac, fac_info.mac, MAC_ADDR_SIZE); /* init extFw dev_name: dev_name + _ + hw_ver */ strcpy((char*)origin_info->dev_name, (char*)fac_info.dev_name); size = strlen(fac_info.dev_name); *(origin_info->dev_name + size) = '_'; strcpy((char*)origin_info->dev_name + size + 1, (char*)fac_info.hw_ver); #ifdef CONFIG_LTE_MODULE_IN_UP_BIN /* init lte info */ size = ds_read(MODULE_SPEC_PATH, &module_spec, sizeof(MODULE_SPEC)); if (size < sizeof(MODULE_SPEC)) { UPGRADE_ERROR("Reading module_spec failed."); goto INIT_ERROR; } origin_info->lte_supported = atoi(module_spec.lte); if (origin_info->lte_supported) { size = ds_read(LTE_MODULE_INFO_PATH, &lte_module_info, sizeof(LTE_PARAM_MODULE_INFO)); if (size < sizeof(LTE_PARAM_MODULE_INFO)) { UPGRADE_ERROR("Reading lte module info failed."); goto INIT_ERROR; } strncpy((char *)origin_info->lte_vender, (const char *)lte_module_info.vender, sizeof(origin_info->lte_vender)); strncpy((char *)origin_info->lte_product, (const char *)lte_module_info.product, sizeof(origin_info->lte_product)); origin_info->lte_version = atoi(lte_module_info.version); } #endif return SLP_ENONE; INIT_ERROR: return SLP_ESYSTEM; }
最新发布
09-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值