Android HAL layer frame analysis (III)

Previous analysis of how android HAL layer is searching dynamic shared libraries hardware modules, in fact, is to find the shared library modueid.variant in "/ / vendor / lib / hw" these two paths "system / lib / hw /" or. after so, load the library by calling the load function.

Here we enter the load function to see how to achieve specific load shared libraries.

The following is a load function definition, also achieved in the /hardware/libhardware/hardware.c,

/**
 * Load the file defined by the variant and if successful
 * return the dlopen handle and the hmi.
 * @return 0 = success, !0 = failure.
 */
static int load(const char *id,
        const char *path,
        const struct hw_module_t **pHmi)
{//传入硬件模块id和库所在路径,获取到硬件模块结构体
    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 = (struct hw_module_t *)dlsym(handle, sym);//解析共享库
    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) {//匹配解析出硬件模块的id和传入我们实际想要得到的模块id是否一致
        LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
        status = -EINVAL;
        goto done;
    }

    hmi->dso = handle;  //将打开库得到句柄传给硬件模块的dso

    /* success */
    status = 0;

    done:
    if (status != 0) {
        hmi = NULL;
        if (handle != NULL) {
            dlclose(handle);
            handle = NULL;
        }
    } else {
        LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
                id, path, *pHmi, handle);
    }

    *pHmi = hmi;//将得到的module的结果通过第三个参数传给hw_module_t

    return status;
}

You can see the incoming load function of several parameters, the first parameter is the need to load the hardware id hardware module module corresponds to a dynamic library,

The second parameter is the dynamic inventory put the path in front hw_get_module function is part of the search base resulting path,

The third argument is that we need to get the hardware module structure, through which pass hw_get_module, hw_get_module jni function passed by parameter.

Line 19, first call dlopen open a shared library, the library function to find the path passing through the library, and open it returns a handle to the operating handle, then call dlsym function parses the open library, the following line 29, obtain hardware module structure contained in the library, and return it back. Therefore, hardware vendors or hardware transplant recipients have to go to the hardware module structure hw_module_t fill this and according to its own hardware-related hal of this architecture for use.

After parsing through dlsym get a hw_module_t, followed by the first 37 rows from the database structure obtained by parsing the incoming id id and compared to see if the same.

If the proof is to get the right consistency of the hardware module.

Finally, line 60, to a structure pointer passed hw_module_t third parameter passed hw_get_module function.

This, hw_get_module function get a hardware module structure hw_module_t.

With hw_module_t, you can open the device corresponding to the hardware modules by method open its internal hardware devices can be operated by methods of the structural body.

Published 407 original articles · won praise 150 · views 380 000 +

Guess you like

Origin blog.csdn.net/ds1130071727/article/details/104908908