hardware.c 4.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <hardware/hardware.h>

#include <cutils/properties.h>

#include <dlfcn.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <limits.h>

#define LOG_TAG "HAL"
#include <utils/Log.h>

/** Base path of the hal modules */
Brian Swetland's avatar
Brian Swetland committed
31 32
#define HAL_LIBRARY_PATH1 "/system/lib/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
33 34 35 36 37 38 39 40 41 42 43 44

/**
 * There are a set of variant filename for modules. The form of the filename
 * is "<MODULE_ID>.variant.so" so for the led module the Dream variants 
 * of base "ro.product.board", "ro.board.platform" and "ro.arch" would be:
 *
 * led.trout.so
 * led.msm7k.so
 * led.ARMV6.so
 * led.default.so
 */

45 46 47
static const char *variant_keys[] = {
    "ro.hardware",  /* This goes first so that it can pick up a different
                       file on the emulator. */
48 49
    "ro.product.board",
    "ro.board.platform",
50
    "ro.arch"
51
};
52 53 54

static const int HAL_VARIANT_KEYS_COUNT =
    (sizeof(variant_keys)/sizeof(variant_keys[0]));
55 56

/**
57
 * Load the file defined by the variant and if successful
58 59 60 61
 * return the dlopen handle and the hmi.
 * @return 0 = success, !0 = failure.
 */
static int load(const char *id,
62 63
        const char *path,
        const struct hw_module_t **pHmi)
64 65 66
{
    int status;
    void *handle;
67
    struct hw_module_t *hmi;
68 69 70 71 72 73 74 75 76

    /*
     * 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();
77
        LOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
78 79 80 81 82 83
        status = -EINVAL;
        goto done;
    }

    /* Get the address of the struct hal_module_info. */
    const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
84
    hmi = (struct hw_module_t *)dlsym(handle, sym);
85 86 87 88 89 90 91 92 93 94 95 96
    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) {
        LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
        status = -EINVAL;
        goto done;
    }
97

98
    hmi->dso = handle;
99 100 101 102

    /* success */
    status = 0;

103
    done:
104 105 106 107 108 109
    if (status != 0) {
        hmi = NULL;
        if (handle != NULL) {
            dlclose(handle);
            handle = NULL;
        }
110 111
    } else {
        LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
112
                id, path, *pHmi, handle);
113 114 115 116 117 118 119 120 121 122 123
    }

    *pHmi = hmi;

    return status;
}

int hw_get_module(const char *id, const struct hw_module_t **module) 
{
    int status;
    int i;
124 125
    const struct hw_module_t *hmi = NULL;
    char prop[PATH_MAX];
126
    char path[PATH_MAX];
127 128 129 130 131 132 133 134

    /*
     * 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.
     */

135
    /* Loop through the configuration variants looking for a module */
136 137 138 139 140 141
    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;
            }
            snprintf(path, sizeof(path), "%s/%s.%s.so",
Brian Swetland's avatar
Brian Swetland committed
142 143 144 145 146 147
                    HAL_LIBRARY_PATH1, id, prop);
            if (access(path, R_OK) == 0) break;

            snprintf(path, sizeof(path), "%s/%s.%s.so",
                     HAL_LIBRARY_PATH2, id, prop);
            if (access(path, R_OK) == 0) break;
148 149
        } else {
            snprintf(path, sizeof(path), "%s/%s.default.so",
Brian Swetland's avatar
Brian Swetland committed
150 151
                     HAL_LIBRARY_PATH1, id);
            if (access(path, R_OK) == 0) break;
152
        }
153
    }
154

155
    status = -ENOENT;
156
    if (i < HAL_VARIANT_KEYS_COUNT+1) {
157 158 159
        /* load the module, if this fails, we're doomed, and we should not try
         * to load a different variant. */
        status = load(id, path, module);
160
    }
161

162 163
    return status;
}