• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 
13 #include "load_init.h"
14 #include <stdlib.h>
15 #include <dlfcn.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <tee_log.h>
19 #include <priorities.h>
20 #include <tee_secfile_load_agent.h>
21 
22 static void *g_libtee = NULL;
23 
get_priority(void)24 int32_t get_priority(void)
25 {
26     char *prio_var = getenv("priority");
27     int32_t priority;
28 
29     if (prio_var != NULL) {
30         errno = 0;
31         priority = strtol(prio_var, NULL, 10); /* Convert priority to decimal */
32         if ((errno != 0) || (priority < PRIO_TEE_MIN) || (priority > PRIO_TEE_MAX)) {
33             tlogw("bad priority set, use default PRIO_TEE_TA\n");
34             priority = PRIO_TEE_TA;
35         }
36     } else {
37         tlogw("no priority set, use default PRIO_TEE_TA\n");
38         priority = PRIO_TEE_TA;
39     }
40 
41     return priority;
42 }
43 
extend_utables(void)44 int32_t extend_utables(void)
45 {
46     return 0;
47 }
48 
clear_libtee(void)49 void clear_libtee(void)
50 {
51     if (g_libtee == NULL) {
52         tloge("libtee handle is NULL\n");
53         return;
54     }
55 
56     dlclose(g_libtee);
57     g_libtee = NULL;
58 }
59 
get_libtee_handle(void)60 void *get_libtee_handle(void)
61 {
62     if (g_libtee == NULL) {
63         tloge("libtee handle is NULL\n");
64         return NULL;
65     }
66 
67     return g_libtee;
68 }
69 
ta_mt_dlopen(const char * name,int32_t flag)70 void *ta_mt_dlopen(const char *name, int32_t flag)
71 {
72     if (name == NULL) {
73         tloge("dlopen name is invalied\n");
74         return NULL;
75     }
76 
77     size_t length = strnlen(name, LIB_NAME_MAX);
78     if (length == 0 || length >= LIB_NAME_MAX) {
79         tloge("dlopen name length is invalied\n");
80         return NULL;
81     }
82 
83     g_libtee = dlopen(name, flag);
84     if (g_libtee == NULL) {
85         tloge("load library failed: %s\n", dlerror());
86         return NULL;
87     }
88 
89     return g_libtee;
90 }
91