hw_module_t 加載過程


 每一個HAL模塊都有一個ID值,以這些ID值為參數來調用硬件抽象層提供的函數hw_get_module就可以將指定的模塊加載到內存來,並且獲得 一個hw_module_t接口來打開相應的設備。 函數hw_get_module實現在hardware/libhardware /hardware.c文件中,如下所示:

     /** Base path of the hal modules */  

  1. #define HAL_LIBRARY_PATH1 "/system/lib/hw"  
  2. #define HAL_LIBRARY_PATH2 "/vendor/lib/hw"  
  3.   
  4. /** 
  5.  * There are a set of variant filename for modules. The form of the filename 
  6.  * is "<MODULE_ID>.variant.so" so for the led module the Dream variants  
  7.  * of base "ro.product.board", "ro.board.platform" and "ro.arch" would be: 
  8.  * 
  9.  * led.trout.so 
  10.  * led.msm7k.so 
  11.  * led.ARMV6.so 
  12.  * led.default.so 
  13.  */  
  14.   
  15. static const char *variant_keys[] = {  
  16.     "ro.hardware",  /* This goes first so that it can pick up a different file on the emulator. */  
  17.     "ro.product.board",  
  18.     "ro.board.platform",  
  19.     "ro.arch"  
  20. };  
  21.   
  22. static const int HAL_VARIANT_KEYS_COUNT =  
  23.     (sizeof(variant_keys)/sizeof(variant_keys[0]));  
  24.   
  25. ......  
  26.   
  27. int hw_get_module(const char *id, const struct hw_module_t **module)  
  28. {  
  29.     int status;  
  30.     int i;  
  31.     const struct hw_module_t *hmi = NULL;  
  32.     char prop[PATH_MAX];  
  33.     char path[PATH_MAX];  
  34.   
  35.     /* 
  36.      * Here we rely on the fact that calling dlopen multiple times on 
  37.      * the same .so will simply increment a refcount (and not load 
  38.      * a new copy of the library). 
  39.      * We also assume that dlopen() is thread-safe. 
  40.      */  
  41.   
  42.     /* Loop through the configuration variants looking for a module */  
  43.     for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {  
  44.         if (i < HAL_VARIANT_KEYS_COUNT) {  
  45.             if (property_get(variant_keys[i], prop, NULL) == 0) {  
  46.                 continue;  
  47.             }  
  48.   
  49.             snprintf(path, sizeof(path), "%s/%s.%s.so",  
  50.                     HAL_LIBRARY_PATH1, id, prop);  
  51.             if (access(path, R_OK) == 0) break;  
  52.   
  53.             snprintf(path, sizeof(path), "%s/%s.%s.so",  
  54.                      HAL_LIBRARY_PATH2, id, prop);  
  55.             if (access(path, R_OK) == 0) break;  
  56.         } else {  
  57.             snprintf(path, sizeof(path), "%s/%s.default.so",  
  58.                      HAL_LIBRARY_PATH1, id);  
  59.             if (access(path, R_OK) == 0) break;  
  60.         }  
  61.     }  
  62.   
  63.     status = -ENOENT;  
  64.     if (i < HAL_VARIANT_KEYS_COUNT+1) {  
  65.         /* load the module, if this fails, we're doomed, and we should not try 
  66.          * to load a different variant. */  
  67.         status = load(id, path, module);  
  68.     }  
  69.   
  70.     return status;  
  71. }  

        函數hw_get_module依次在目錄/system/lib /hw和/vendor/lib/hw中查找一個名稱為"<MODULE_ID>.variant.so"的文件,其 中,<MODULE_ID>是一個模塊ID,而variant表 示"ro.hardware"、"ro.product.board"、"ro.board.platform"和"ro.arch"四個系統屬性值之一。例如,對於Gralloc模塊來說,函數hw_get_module依次在目錄/system/lib/hw和/vendor/lib/hw中檢查是否存在以下四個文件: 

       gralloc.<ro.hardware>.so

       gralloc.<ro.product.board>.so

       gralloc.<ro.board.platform>.so

       gralloc.<ro.arch>.so
      

       只要其中的一個文件存在,  函數hw_get_module就會停止查找過程,並且調用另外一個函數load來將這個文件加載到內存中來。另一方面,如果在/system/lib/hw和/vendor/lib/hw中均不存這些文件,那么函數hw_get_module就會在目錄/system/lib/hw中查找是否存在一個名稱為gralloc.default.so的文件。如果存在的話,那么也會調用函數load將它加載到內存中來。函數load也是實現在文件hardware/libhardware/hardware.c文件中,如下所示:

 

static int load(const char *id,  
  1.         const char *path,  
  2.         const struct hw_module_t **pHmi)  
  3. {  
  4.     int status;  
  5.     void *handle;  
  6.     struct hw_module_t *hmi;  
  7.   
  8.     /* 
  9.      * load the symbols resolving undefined symbols before 
  10.      * dlopen returns. Since RTLD_GLOBAL is not or'd in with 
  11.      * RTLD_NOW the external symbols will not be global 
  12.      */  
  13.     handle = dlopen(path, RTLD_NOW);  
  14.     if (handle == NULL) {  
  15.         char const *err_str = dlerror();  
  16.         LOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");  
  17.         status = -EINVAL;  
  18.         goto done;  
  19.     }  
  20.   
  21.     /* Get the address of the struct hal_module_info. */  
  22.    每個模塊定義了一個hw_module_t或hw_module_t子類型變量HAL_MODULE_INFO_SYM,通過這個導出符號可以得到hw_module_t變量對象。
  23.     const char *sym = HAL_MODULE_INFO_SYM_AS_STR;  
  24.     hmi = (struct hw_module_t *)dlsym(handle, sym);  
  25.     if (hmi == NULL) {  
  26.         LOGE("load: couldn't find symbol %s", sym);  
  27.         status = -EINVAL;  
  28.         goto done;  
  29.     }  
  30.   
  31.     /* Check that the id matches */  
  32.     if (strcmp(id, hmi->id) != 0) {  
  33.         LOGE("load: id=%s != hmi->id=%s", id, hmi->id);  
  34.         status = -EINVAL;  
  35.         goto done;  
  36.     }  
  37.   
  38.     hmi->dso = handle;  
  39.   
  40.     /* success */  
  41.     status = 0;  
  42.   
  43.     done:  
  44.     if (status != 0) {  
  45.         hmi = NULL;  
  46.         if (handle != NULL) {  
  47.             dlclose(handle);  
  48.             handle = NULL;  
  49.         }  
  50.     } else {  
  51.         LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",  
  52.                 id, path, *pHmi, handle);  
  53.     }  
  54.   
  55.     *pHmi = hmi;  
  56.   
  57.     return status;  
  58. }  


        在Linux系統中,后綴名為"so"的文件為動態鏈接庫文件,可能通過函數dlopen來加載到內存中。硬件抽象層模塊編寫規范規定每一個硬件抽象層模塊都必須導出一個符號名稱為HAL_MODULE_INFO_SYM_AS_STR的符號,而且這個符號必須是用來描述一個類型為hw_module_t的結構體的。

 

        HAL_MODULE_INFO_SYM_AS_STR是一個宏,定義在文件hardware/libhardware/include/hardware/hardware.h文件中,如下所示:

 

 
  1. #define HAL_MODULE_INFO_SYM_AS_STR  "HMI"  

       將模塊加載到內存中來之后,就可以調用函數dlsym來獲得它所導出的符號HMI。由於這個符號指向的是一個hw_module_t結構體,因此,最后函 數load就可以強制地將這個符號轉換為一個hw_module_t結構體指針,並且保存在輸出參數pHmi中返回給調用者。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM