1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "hdf_driver_loader.h"
10 #include "hdf_log.h"
11 #include "osal_mem.h"
12
HdfDriverEntryConstruct(int32_t * driverCount)13 static struct HdfDriverEntry *HdfDriverEntryConstruct(int32_t *driverCount)
14 {
15 int i;
16 *driverCount = (int32_t)(((uint8_t *)(HDF_DRIVER_END()) - (uint8_t *)(HDF_DRIVER_BEGIN())) / sizeof(size_t));
17 if (*driverCount <= 0) {
18 HDF_LOGE("%s: failed to hdf get device counts", __func__);
19 return NULL;
20 }
21 struct HdfDriverEntry *driverEntry = OsalMemCalloc(*driverCount * sizeof(struct HdfDriverEntry));
22 if (driverEntry == NULL) {
23 HDF_LOGE("%s: failed to alloc driver entry mem", __func__);
24 *driverCount = 0;
25 return NULL;
26 }
27 size_t *addrBegin = (size_t *)(HDF_DRIVER_BEGIN());
28 for (i = 0; i < *driverCount; i++) {
29 driverEntry[i] = *(struct HdfDriverEntry *)(*addrBegin);
30 addrBegin++;
31 }
32 return driverEntry;
33 }
34
HdfDriverLoaderGetDriverEntry(const struct HdfDeviceInfo * deviceInfo)35 struct HdfDriverEntry *HdfDriverLoaderGetDriverEntry(const struct HdfDeviceInfo *deviceInfo)
36 {
37 int i;
38 if ((deviceInfo == NULL) || (deviceInfo->moduleName == NULL) || (deviceInfo->svcName == NULL)) {
39 HDF_LOGE("%s: failed to get device entry, input deviceInfo is NULL", __func__);
40 return NULL;
41 }
42 static struct HdfDriverEntry *driverEntry = NULL;
43 static int32_t driverCount = 0;
44 if (driverEntry == NULL) {
45 driverEntry = HdfDriverEntryConstruct(&driverCount);
46 if (driverEntry == NULL) {
47 HDF_LOGE("%s: failed to construct driver entry", __func__);
48 return NULL;
49 }
50 }
51 for (i = 0; i < driverCount; i++) {
52 if (driverEntry == NULL) {
53 HDF_LOGE("%s: driver entry is null", __func__);
54 return NULL;
55 }
56 if (driverEntry[i].moduleName == NULL) {
57 HDF_LOGE("%s: driver entry module name is null", __func__);
58 continue;
59 }
60 if (strcmp(deviceInfo->moduleName, driverEntry[i].moduleName) == 0) {
61 return &driverEntry[i];
62 }
63 }
64 HDF_LOGE("failed to get device entry %s", deviceInfo->svcName);
65 return NULL;
66 }
67
68