Learning about the HAL-related library search mechanism and principles in Android

Source: Internet
Author: User

This article is a summary of the source code. Please specify the source for the transfer. Thank you.

Welcome to your discussion. Qq: 1037701636 email: gzzaigcn2012@gmail.com

Android source code Version: 4.2.2; hardware platform A31

What I have to mention when introducing the FrameWork is that HAL (Hardware Abstraction Layer) is generally used to interact with characteristic hardware platforms, so the main difference between different android platforms is that in this part, the benefit of HAL is that a FrameWork can call different HAL, but the relevant HAL must meet certain interface specifications. On the other hand, HAL can shield the application code related to underlying hardware operations.

There have been a lot of introductions to HAL on the Internet. Here I will share with you the HAL layer specifications I have learned in depth.

The core APIs are hw_get_module (), hw_get_module_by_class (), and load (). The core is hw_get_module_by_class (). Let's look at the implementation process code:

Int hw_get_module_by_class (const char * class_id, const char * inst, const struct hw_module_t ** module) {int status; int I; const struct hw_module_t * hmi = NULL; char prop [PATH_MAX]; char path [PATH_MAX]; char name [PATH_MAX]; if (inst) snprintf (name, PATH_MAX, "% s. % s ", class_id, inst); else strlcpy (name, class_id, PATH_MAX);/** Here we rely on the fact that calling dlopen multiple times on * the same. so will simply increment a refcount (and not load * a new copy of the library ). * We also assume that dlopen () is thread-safe. * // * Loop through the configuration variants looking for a module */for (I = 0; I
 
  

Step 1: Focus on the variable variant_keys and obtain the function property_get () through system properties () to obtain the values maintained by the following four attribute variables in the memory system attributes. (The init process mainly creates and maintains the system attributes)

static const char *variant_keys[] = {    "ro.hardware",  /* This goes first so that it can pick up a different                       file on the emulator. */    "ro.product.board",    "ro.board.platform",    "ro.arch"};

Let's first look at ro. hardware, which generally specifies the system board-level hardware, such as sun6i here. Where can this attribute variable be set? The first thing that appears in the init process is to call it as follows:

1. get_hardware_name () --> fd = open ("/proc/cpuinfo", O_RDONLY): Search for the hardware field in cpuinfo. If yes, it is saved in hardware.

2. process_kernel_cmdline --> export_kernel_boot_props (): query the attribute values set during boot. The following code is available for hardware:

 /* if this was given on kernel command line, override what we read     * before (e.g. from /proc/cpuinfo), if anything */    pval = property_get("ro.boot.hardware");    if (pval)        strlcpy(hardware, pval, sizeof(hardware));    property_set("ro.hardware", hardware);

If ro. boot. hardware is set during boot, hardware reloads the values read from/proc/cpuinfo, so that the hardware system attribute values of the entire ro. hardware are configured.

For "ro. product. board "," ro. board. platform "," ro. arch "these three Property variables need to be concentrated on the property files of android. The property files in the android system include the following:

Directory of the basic properties file for android: # define PROP_PATH_RAMDISK_DEFAULT "/default. prop"
# Define PROP_PATH_SYSTEM_BUILD "/system/build. prop"
# Define PROP_PATH_SYSTEM_DEFAULT "/system/default. prop"
# Define PROP_PATH_LOCAL_OVERRIDE "/data/local. prop" the above files will be further parsed in the init process and the following code will be noted:
Queue_builtin_action (property_service_init_action, "property_service_init"); // initialize the property service
The initialization action of the property service is added to the action queue for execution. Execute the execute_one_command function to call back the property_service_init_action in the registered action, here we can see that the Attribute Service reads and maintains data from the file to the memory:
void start_property_service(void){    int fd;    load_properties_from_file(PROP_PATH_SYSTEM_BUILD);    load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT);
You can see that the prop attribute file under different paths will be parsed, including the buil to be introduced below. prop let's take a look at/system/build. prop is compiled by the Android build/core/Makefile and Shell script build/tools/buildinfo. sh is generated by combining the platform-related variables in the entire compilation and configuration environment, which can be viewed in the device information of the Android system;
3 ro. build. id = JDQ39 4 ro. build. display. id = fiber_3g-eng 4.2.2 JDQ39 20140110 test-keys 5 ro. build. version. incremental = 20140110 6 ro. build. version. sdk = 17 7 ro. build. version. codename = REL 8 ro. build. version. release = 4.2.2 9 ro. build. date = January 10, 2014 Friday 16:03:07 CST 10 ro. build. date. utc = 1389340987 11 ro. build. type = eng 12 ro. build. user = root 13 ro. build. host = linux 14 ro. build. tags = test-keys 15 ro. product. model = Softwinner 16 ro. product. brand = Softwinner 17 ro. product. name = maid 18 ro. product. device = fiber-3g 19 ro. product. board = exdroid 20 ro. product. cpu. abi = armeabi-v7a 21 ro. product. cpu. abi2 = armeabi 22 ro. product. manufacturer = unknown 23 ro. product. locale. language = en 24 ro. product. locale. region = US 25 ro. wifi. channels = 26 ro. board. platform = fiber ....
As described above, we have already covered and jumped a lot of content. We will return to the hw_get_module_by_class function. Below we will determine the directories in which the search will take priority, you can see the following two main paths:/** Base path of the hal modules */
# Define HAL_LIBRARY_PATH1 "/system/lib/hw"
# Define HAL_LIBRARY_PATH2 "/vendor/lib/hw", we can see that the current hal library can only be placed below this, otherwise the program cannot be found to run normally, we have to write Android at compilation. add the cp command in mk to compile the current database and copy it to the preceding two directories. Generally, libraries closer to the hardware platform are stored in/vendor/lib/hw, such as gralloc. sun6i. so hwcomposer. sun6i. so is found in this way. Step2. after the search is completed and the path of the corresponding database is located, the load function is dynamically loaded as follows, mainly loading the database file to the address where the HMI is located: # define HAL_MODULE_INFO_SYM_AS_STR "HMI" handle = dlopen (path, RTLD_NOW); hmi = (struct hw_module_t *) dlsym (handle, sym ); in this way, the information of struct hal_module_info in the current library file is obtained, that is, hw_m odule_t (an internal open function of hw_module_methods_t * methods provides a device operation interface hw_devices_t to FW ), after the final result is returned, you can use it to operate the encapsulated functions in the relevant HAL.
The focus of this article is to analyze the HAL search and positioning process, so that you can customize your own Android.
Handle = dlopen (path, RTLD_NOW );




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.