Android--hw_get_module解析

来源:互联网 发布:centos root密码忘记了 编辑:程序博客网 时间:2024/05/26 02:54

作者:苗老师,华清远见嵌入式学院讲师。

我们知道,google为了保护硬件厂商的信息,在Android中添加了一层,也就是大名鼎鼎的HAL层。

在看HAL的编写方法的过程中,会发现整个模块貌似没有一个入口。一般说来模块都要有个入口,比如应用程序有main函数,可以为加载器进行加载执行,dll文件有dllmain,而对于我们自己写的动态链接库,我们可以对库中导出的任何符号进行调用。

问题来了,Android中的HAL是比较具有通用性的,需要上层的函数对其进行加载调用,Android的HAL加载器是如何实现对不同的Hardware Module进行通用性的调用的呢?

带着这个疑问查看Android源码,会发现Android中实现调用HAL是通过hw_get_module实现的。

[cpp] view plaincopy

        1. int hw_get_module(const char *id, const struct hw_module_t **module);

这是其函数原型,id会指定Hardware的id,这是一个字符串,比如我们比较熟悉的led的id是
        #define SENSORS_HARDWARE_MODULE_ID “led”,如果找到了对应的hw_module_t结构体,会将其指针放入*module中。看看它的实现。

[cpp] view plaincopy

        1. 124 int hw_get_module(const char *id, const struct hw_module_t **module)
        2. 125 {
        3. 126        int status;
        4. 127        int i;
        5. 128        const struct hw_module_t *hmi = NULL;
        6. 129        char prop[PATH_MAX];
        7. 130        char path[PATH_MAX];
        8. 131
        9. 132        /*
        10. 133        * Here we rely on the fact that calling dlopen multiple times on
        11. 134        * the same .so will simply increment a refcount (and not load
        12. 135        * a new copy of the library).
        13. 136        * We also assume that dlopen() is thread-safe.
        14. 137        */
        15. 138
        16. 139        /* Loop through the configuration variants looking for a module */
        17. 140        for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {
        18. 141                if (i < HAL_VARIANT_KEYS_COUNT) {
        19. 142                        if (property_get(variant_keys[i], prop, NULL) == 0) { //获取数组variant_keys里的属性值
        20.
        21. 143                                continue;
        22. 144                        }
        23. 145                        snprintf(path, sizeof(path), "%s/%s.%s.so",
        24. 146                        HAL_LIBRARY_PATH, id, prop);//如果开发板叫做fs100,这里就加载system/lib/hw/led.fs100.so
        25. 147                } else { snprintf(path, sizeof(path), "%s/%s.default.so",
        26. 149                        HAL_LIBRARY_PATH, id);//这里默认加载system/lib/hw/led.default.so
        27. 150                }
        28. 151                if (access(path, R_OK)) {
        29. 152                        continue;
        30. 153                }
        31. 154                /* we found a library matching this id/variant */
        32. 155                break;
        33. 156        }
        34. 157
        35. 158        status = -ENOENT;
        36. 159        if (i < HAL_VARIANT_KEYS_COUNT+1) {
        37. 160                /* load the module, if this fails, we're doomed, and we should not try
        38. 161                * to load a different variant. */
        39. 162                status = load(id, path, module);//load函数是关键,调用load函数打开动态链接库
        40. 163        }
        41. 164
        42. 165        return status;
        43. 166 }

上述代码主要是获取动态链接库的路径,并调用load函数去打开指定路径下的库文件,load函数是关键所在。

好,那我们就来解开load函数的神秘面纱!!!

[cpp] view plaincopy

        1. 65 static int load(const char *id,
        2. 66                const char *path,
        3. 67                const struct hw_module_t **pHmi)
        4. 68 {
        5. 69        int status;
        6. 70        void *handle;
        7. 71        struct hw_module_t *hmi;
        8. 72
        9. 73        /*
        10. 74        * load the symbols resolving undefined symbols before
        11. 75        * dlopen returns. Since RTLD_GLOBAL is not or'd in with
        12. 76        * RTLD_NOW the external symbols will not be global
        13. 77        */
        14. 78        handle = dlopen(path, RTLD_NOW);
        15. 79        if (handle == NULL) {
        16. 80                char const *err_str = dlerror();
        17. 81                LOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
        18. 82                status = -EINVAL;
        19. 83                goto done;
        20. 84        }
        21. 85
        22. 86        /* Get the address of the struct hal_module_info. */
        23. 87        const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
        24. 88        hmi = (struct hw_module_t *)dlsym(handle, sym);
        25. 89        if (hmi == NULL) {
        26. 90                LOGE("load: couldn't find symbol %s", sym);
        27. 91                status = -EINVAL;
        28. 92                goto done;
        29. 93        }
        30. 94
        31. 95        /* Check that the id matches */
        32. 96        if (strcmp(id, hmi->id) != 0) {
        33. 97                LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
        34. 98                status = -EINVAL;
        35. 99                goto done;
        36. 100        }
        37. 101
        38. 102        hmi->dso = handle;
        39. 103
        40. 104        /* success */
        41. 105        status = 0;
        42. 106
        43. 93        }
        44. 94
        45. 95        /* Check that the id matches */
        46. 96        if (strcmp(id, hmi->id) != 0) {
        47. 97                LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
        48. 98                status = -EINVAL;
        49. 99                goto done;
        50. 100        }
        51. 101
        52. 102        hmi->dso = handle;
        53. 103
        54. 104        /* success */
        55. 105        status = 0;
        56. 106
        57. 107        done:
        58. 108        if (status != 0) {
        59. 109                hmi = NULL;
        60. 110                if (handle != NULL) {
        61. 111                        dlclose(handle);
        62. 112                        handle = NULL;
        63. 113                }
        64. 114        } else {
        65. 115                LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
        66. 116                        id, path, *pHmi, handle);
        67. 117        }
        68. 118
        69. 119        *pHmi = hmi;
        70. 120
        71. 121        return status;
        72. 122 }

这里有一个宏HAL_MODULE_INFO_SYM_AS_STR需要注意:

[cpp] view plaincopy

        1. #define HAL_MODULE_INFO_SYM_AS_STR "HMI"

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

这里是查找“HMI”这个导出符号,并获取其地址。

看到这里,我们不禁要问,为什么根据“HMI”这个导出符号,就可以从动态链接库中找到结构体hw_module_t呢??

我们知道,ELF = Executable and Linkable Format,可执行连接格式,是UNIX系统实验室(USL)作为应用程序二进制接口(Application Binary Interface,ABI)而开发和发布的,扩展名为elf。一个ELF头在文件的开始,保存了路线图(road map),描述了该文件的组织情况。sections保存着object 文件的信息,从连接角度看:包括指令,数据,符号表,重定位信息等等。我们的led.default.so就是一个elf格式的文件。

[cpp] view plaincopy

        1. linux@ubuntu:~/eclair_2.1_farsight/out/target/product/fs100/system/lib/hw$ file led.default.so
        2. led.default.so: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked, stripped

所以说,我们可以使用unix给我们提供的readelf命令去查看相应的符号信息,就一目了然了!

[cpp] view plaincopy

        1. linux@ubuntu:~/eclair_2.1_farsight/out/target/product/fs100/system/lib/hw$ readelf -s led.default.so
        2.
        3. Symbol table '.dynsym' contains 25 entries:
        4.                Num:            Value        Size        Type                 Bind                          Vis                  Ndx  Name
        5.                0:           00000000        0             NOTYPE        LOCAL                DEFAULT            UND
        6.                1:           000004c8        0             SECTION       LOCAL                DEFAULT            7
        7.                2:           00001000        0             SECTION       LOCAL               DEFAULT            11
        8.                3:           00000000        0             FUNC              GLOBAL            DEFAULT            UND ioctl
        9.                4:           000006d4        0             NOTYPE         GLOBAL            DEFAULT            ABS __exidx_end
        10.                5:         00000000        0            FUNC              GLOBAL             DEFAULT            UND __aeabi_unwind_cpp_pr0
        11.                6:         00001178        0            NOTYPE         GLOBAL             DEFAULT           ABS _bss_end__
        12.                7:         00000000        0            FUNC              GLOBAL             DEFAULT           UND malloc
        13.                8:         00001174        0            NOTYPE         GLOBAL             DEFAULT           ABS __bss_start__
        14.                9:         00000000        0            FUNC              GLOBAL             DEFAULT           UND __android_log_print
        15.                10:       000006ab        0            NOTYPE         GLOBAL             DEFAULT           ABS __exidx_start
        16.                11:       00001174        4            OBJECT          GLOBAL             DEFAULT           15 fd
        17.                12:       000005d5        60           FUNC             GLOBAL             DEFAULT           7 led_set_off
        18.                13:       00001178        0            NOTYPE          GLOBAL            DEFAULT            ABS __bss_end__
        19.                14:        00001174        0           NOTYPE          GLOBAL            DEFAULT            ABS __bss_start
        20.                15:       00000000        0            FUNC               GLOBAL            DEFAULT            UND memset
        21.                16:       00001178        0           NOTYPE           GLOBAL            DEFAULT            ABS __end__
        22.                17:       00001174        0           NOTYPE           GLOBAL            DEFAULT            ABS _edata
        23.                18:       00001178        0           NOTYPE           GLOBAL            DEFAULT            ABS _end
        24.                19:       00000000        0           FUNC                GLOBAL            DEFAULT            UND open
        25.                20:       00080000        0           NOTYPE           GLOBAL            DEFAULT            ABS _stack
        26.                21:       00001000        128      OBJECT            GLOBAL            DEFAULT            11 HMI
        27.                22:       00001170        0           NOTYPE           GLOBAL            DEFAULT            14 __data_start
        28.                23:       00000000        0           FUNC                GLOBAL            DEFAULT            UND close
        29.                24:       00000000        0           FUNC                GLOBAL            DEFAULT            UND free

在21行我们发现,名字就是“HMI”,对应于hw_module_t结构体。再去对照一下HAL的代码。

[cpp] view plaincopy

       1. const struct led_module_t HAL_MODULE_INFO_SYM = {
        2.        common: {
        3.                tag: HARDWARE_MODULE_TAG,
        4.                version_major: 1,
        5.                version_minor: 0,
        6.                id: LED_HARDWARE_MODULE_ID,
        7.                name: "led HAL module",
        8.                author: "farsight",
        9.                methods: &led_module_methods,
        10.        },
        11.
        12. };

这里定义了一个名为HAL_MODULE_INFO_SYM的copybit_module_t的结构体,common成员为hw_module_t类型。注意这里的HAL_MODULE_INFO_SYM变量必须为这个名字,这样编译器才会将这个结构体的导出符号变为“HMI”,这样这个结构体才能被dlsym函数找到!

综上,我们知道了andriod HAL模块也有一个通用的入口地址,这个入口地址就是HAL_MODULE_INFO_SYM变量,通过它,我们可以访问到HAL模块中的所有想要外部访问到的方法。

文章来源:华清远见嵌入式学院,原文地址:http://www.embedu.org/Column/Column733.htm

更多相关嵌入式免费资料查看华清远见讲师博文>>

原创粉丝点击