-
Notifications
You must be signed in to change notification settings - Fork 34
/
QSEEComFunc.c
363 lines (321 loc) · 11 KB
/
QSEEComFunc.c
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Copyright (C) 2016 Shane Francis / Jens Andersen
*
* 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 "QSEEComFunc.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#define LOG_TAG "QSEE_WRAPPER"
//#define LOG_NDEBUG 0
//#define USE_QSEE_WRAPPER 1
#ifdef USE_QSEE_WRAPPER
#define QSEE_LIBRARY "libDSEEComAPI.so"
#else
#define QSEE_LIBRARY "libQSEEComAPI.so"
#endif
#if PLATFORM_SDK_VERSION < 28
#include <cutils/log.h>
#else
#include <log/log.h>
#endif
//Forward declarations
static int qsee_load_trustlet(struct qsee_handle_t* qsee_handle,
struct QSEECom_handle **clnt_handle,
const char *path, const char *fname,
uint32_t sb_size);
char* qsee_error_strings(int err);
int32_t qcom_km_ion_dealloc(struct qcom_km_ion_info_t *handle);
int32_t qcom_km_ion_memalloc(struct qcom_km_ion_info_t *handle,
uint32_t size);
typedef struct {
void *libHandle;
} _priv_data_t;
int32_t qsee_open_handle(struct qsee_handle_t** ret_handle)
{
struct qsee_handle_t *handle = NULL;
_priv_data_t *data = NULL;
int32_t ret = -1;
ALOGD("Using Target Lib : %s\n", QSEE_LIBRARY);
data = (_priv_data_t*)malloc(sizeof(_priv_data_t));
if(data == NULL)
{
ALOGE("Error allocating memory: %s\n", strerror(errno));
goto exit;
}
data->libHandle = dlopen(QSEE_LIBRARY, RTLD_NOW);
if(data->libHandle == NULL)
{
ALOGE("Failed to load QSEECom API library: %s\n", strerror(errno));
goto exit_err_data;
}
ALOGD("Loaded QSEECom API library at %p\n", data->libHandle);
handle =(struct qsee_handle_t*)malloc(sizeof(struct qsee_handle_t));
if(handle == NULL)
{
ALOGE("Error allocating memory: %s\n", strerror(errno));
goto exit_err_dlhandle;
}
handle->_data = data;
ALOGD("Loaded QSEECom API library...\n");
// Setup internal functions
handle->ion_alloc = qcom_km_ion_memalloc;
handle->ion_free = qcom_km_ion_dealloc;
handle->load_trustlet = qsee_load_trustlet;
// Setup QSEECom Functions
// NOTE: dlsym doesn't allocate anything, so there's nothing extra to free from these calls!
handle->start_app = (start_app_def) dlsym(data->libHandle, "QSEECom_start_app");
if(handle->start_app == NULL)
{
ALOGE("Error loading QSEECom_start_app: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->shutdown_app = (shutdown_app_def) dlsym(data->libHandle, "QSEECom_shutdown_app");
if(handle->shutdown_app == NULL)
{
ALOGE("Error loading QSEECom_shutdown_app: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->load_external_elf = (load_external_elf_def) dlsym(data->libHandle, "QSEECom_load_external_elf");
if(handle->load_external_elf == NULL)
{
ALOGE("Error loading QSEECom_load_external_elf: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->unload_external_elf = (unload_external_elf_def) dlsym(data->libHandle, "QSEECom_unload_external_elf");
if(handle->unload_external_elf == NULL)
{
ALOGE("Error loading QSEECom_unload_external_elf: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->register_listener = (register_listener_def) dlsym(data->libHandle, "QSEECom_register_listener");
if(handle->register_listener == NULL)
{
ALOGE("Error loading QSEECom_register_listener: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->unregister_listener = (unregister_listener_def) dlsym(data->libHandle, "QSEECom_unregister_listener");
if(handle->unregister_listener == NULL)
{
ALOGE("Error loading QSEECom_unregister_listener: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->send_cmd = (send_cmd_def) dlsym(data->libHandle, "QSEECom_send_cmd");
if(handle->send_cmd == NULL)
{
ALOGE("Error loading QSEECom_send_cmd: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->send_modified_cmd = (send_modified_cmd_def) dlsym(data->libHandle, "QSEECom_send_modified_cmd");
if(handle->send_modified_cmd == NULL)
{
ALOGE("Error loading QSEECom_send_modified_cmd: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->receive_req = (receive_req_def) dlsym(data->libHandle, "QSEECom_receive_req");
if(handle->receive_req == NULL)
{
ALOGE("Error loading QSEECom_receive_req: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->send_resp = (send_resp_def) dlsym(data->libHandle, "QSEECom_send_resp");
if(handle->send_resp == NULL)
{
ALOGE("Error loading QSEECom_send_resp: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->set_bandwidth = (set_bandwidth_def) dlsym(data->libHandle, "QSEECom_set_bandwidth");
if(handle->set_bandwidth == NULL)
{
ALOGE("Error loading QSEECom_set_bandwidth: %s\n", strerror(errno));
goto exit_err_handle;
}
handle->app_load_query = (app_load_query_def) dlsym(data->libHandle, "QSEECom_app_load_query");
if(handle->app_load_query == NULL)
{
ALOGE("Error loading QSEECom_app_load_query: %s\n", strerror(errno));
goto exit_err_handle;
}
*ret_handle = handle;
return 0;
exit_err_handle:
if(handle != NULL) {
free(handle);
}
exit_err_dlhandle:
if(data->libHandle != NULL) {
dlclose(data->libHandle);
data->libHandle = NULL;
}
exit_err_data:
if(data != NULL)
free(data);
exit:
return ret;
}
int qsee_free_handle(struct qsee_handle_t** handle_ptr)
{
_priv_data_t *data = NULL;
struct qsee_handle_t *handle;
handle = *handle_ptr;
data = (_priv_data_t*)handle->_data;
dlclose(data->libHandle);
free(data);
free(handle);
*handle_ptr = NULL;
return 0;
}
int32_t qcom_km_ion_memalloc(struct qcom_km_ion_info_t *handle,
uint32_t size)
{
int32_t ret = 0;
int32_t iret = 0;
int32_t fd = 0;
unsigned char *v_addr;
struct ion_allocation_data ion_alloc_data;
int32_t ion_fd;
int32_t rc;
struct ion_fd_data ifd_data;
struct ion_handle_data handle_data;
/* open ION device for memory management
* O_DSYNC -> uncached memory
*/
if(handle == NULL) {
ALOGE("Error:: null handle received");
return -1;
}
ion_fd = open("/dev/ion", O_RDONLY | O_DSYNC);
if (ion_fd < 0) {
ALOGE("Error::Cannot open ION device");
return -1;
}
handle->ion_sbuffer = NULL;
handle->ifd_data_fd = 0;
/* Size of allocation */
ion_alloc_data.len = (size + 4095) & (~4095);
/* 4K aligned */
ion_alloc_data.align = 4096;
/* memory is allocated from EBI heap */
ion_alloc_data.heap_id_mask = ION_HEAP(ION_QSECOM_HEAP_ID);
/* Set the memory to be uncached */
ion_alloc_data.flags = 0;
/* IOCTL call to ION for memory request */
rc = ioctl(ion_fd, ION_IOC_ALLOC, &ion_alloc_data);
if (rc) {
ret = -1;
goto alloc_fail;
}
ifd_data.handle = ion_alloc_data.handle;
/* Call MAP ioctl to retrieve the ifd_data.fd file descriptor */
rc = ioctl(ion_fd, ION_IOC_MAP, &ifd_data);
if (rc) {
ret = -1;
goto ioctl_fail;
}
/* Make the ion mmap call */
v_addr = (unsigned char *)mmap(NULL, ion_alloc_data.len,
PROT_READ | PROT_WRITE,
MAP_SHARED, ifd_data.fd, 0);
if (v_addr == MAP_FAILED) {
ALOGE("Error::ION MMAP failed");
ret = -1;
goto map_fail;
}
handle->ion_fd = ion_fd;
handle->ifd_data_fd = ifd_data.fd;
handle->ion_sbuffer = v_addr;
handle->ion_alloc_handle.handle = ion_alloc_data.handle;
handle->sbuf_len = size;
return ret;
map_fail:
if (handle->ion_sbuffer != NULL) {
iret = munmap(handle->ion_sbuffer, ion_alloc_data.len);
if (iret)
ALOGE("Error::Failed to unmap memory for load image. ret = %d", ret);
}
ioctl_fail:
handle_data.handle = ion_alloc_data.handle;
if (handle->ifd_data_fd)
close(handle->ifd_data_fd);
iret = ioctl(ion_fd, ION_IOC_FREE, &handle_data);
if (iret) {
ALOGE("Error::ION FREE ioctl returned error = %d",iret);
}
alloc_fail:
if (ion_fd > 0)
close(ion_fd);
return ret;
}
int32_t qcom_km_ion_dealloc(struct qcom_km_ion_info_t *handle)
{
struct ion_handle_data handle_data;
int32_t ret = 0;
/* Deallocate the memory for the listener */
ret = munmap(handle->ion_sbuffer, (handle->sbuf_len + 4095) & (~4095));
if (ret) {
ALOGE("Error::Unmapping ION Buffer failed with ret = %d", ret);
}
handle_data.handle = handle->ion_alloc_handle.handle;
close(handle->ifd_data_fd);
ret = ioctl(handle->ion_fd, ION_IOC_FREE, &handle_data);
if (ret) {
ALOGE("Error::ION Memory FREE ioctl failed with ret = %d", ret);
}
close(handle->ion_fd);
return ret;
}
char* qsee_error_strings(int err)
{
switch (err)
{
case QSEECOM_LISTENER_REGISTER_FAIL:
return "QSEECom: Failed to register listener\n";
case QSEECOM_LISTENER_ALREADY_REGISTERED:
return "QSEECom: Listener already registered\n";
case QSEECOM_LISTENER_UNREGISTERED:
return "QSEECom: Listener unregistered\n";
case QSEECOM_APP_ALREADY_LOADED:
return "QSEECom: Trustlet already loaded\n";
case QSEECOM_APP_NOT_LOADED:
return "QSEECom: Trustlet not loaded\n";
case QSEECOM_APP_QUERY_FAILED:
return "QSEECom: Failed to query trustlet\n";
default:
return "QSEECom: Unknown error\n";
}
}
int qsee_load_trustlet(struct qsee_handle_t* qsee_handle,
struct QSEECom_handle **clnt_handle,
const char *path, const char *fname,
uint32_t sb_size)
{
int ret = 0;
char* errstr;
int sz = sb_size;
// Too small size causes failures, so force a reasonable size
if(sz < 1024) {
ALOGD("Warning: sb_size too small, increasing to avoid breakage");
sz = 1024;
}
ALOGE("Starting app %s\n", fname);
ret = qsee_handle->start_app(clnt_handle, path, fname, sz);
if (ret < 0) {
errstr = qsee_error_strings(ret);
ALOGE("Could not load app %s. Error: %s (%d)\n",
fname, errstr, ret);
} else
ALOGE("TZ App loaded : %s\n", fname);
return ret;
}