39
39
* Pre-processor definitions
40
40
****************************************************************************/
41
41
42
+ #undef USE_ALLOC_IOC
43
+
44
+ #ifdef USE_ALLOC_IOC
45
+ # define tee_shm_alloc tee_shm_mmap
46
+ # define tee_shm_free_buf (p , s ) munmap(p, s)
47
+ #else
48
+ # define tee_shm_alloc tee_shm_malloc
49
+ # define tee_shm_free_buf (p , s ) ((void)s, free(p))
50
+ #endif
51
+
42
52
#define OPTEE_DEV "/dev/tee0"
43
53
44
54
#define PTA_DEVICE_ENUM { 0x7011a688, 0xddde, 0x4053, \
@@ -183,17 +193,16 @@ static int tee_shm_register(int fd, tee_shm_t *shm)
183
193
return - EINVAL ;
184
194
}
185
195
186
- shm -> id = (int32_t )(uintptr_t )shm -> ptr ;
187
-
188
196
ioc_reg .addr = (uintptr_t )shm -> ptr ;
189
197
ioc_reg .length = shm -> size ;
190
- ioc_reg .flags = TEE_SHM_REGISTER | TEE_SHM_SEC_REGISTER ;
191
- ioc_reg .id = shm -> id ;
192
198
193
- return ioctl (fd , TEE_IOC_SHM_REGISTER , (unsigned long )& ioc_reg );
199
+ shm -> fd = ioctl (fd , TEE_IOC_SHM_REGISTER , (unsigned long )& ioc_reg );
200
+ shm -> id = ioc_reg .id ;
201
+
202
+ return shm -> fd < 0 ? shm -> fd : 0 ;
194
203
}
195
204
196
- #if 0 /* Not used */
205
+ #ifdef USE_ALLOC_IOC
197
206
static int tee_shm_mmap (int fd , tee_shm_t * shm , bool reg )
198
207
{
199
208
struct tee_ioctl_shm_alloc_data ioc_alloc ;
@@ -235,9 +244,9 @@ static int tee_shm_mmap(int fd, tee_shm_t *shm, bool reg)
235
244
236
245
return ret ;
237
246
}
238
- #endif
239
247
240
- static int tee_shm_alloc (int fd , tee_shm_t * shm , bool reg )
248
+ #else /* !USE_ALLOC_IOC */
249
+ static int tee_shm_malloc (int fd , tee_shm_t * shm , bool reg )
241
250
{
242
251
int ret = 0 ;
243
252
@@ -265,6 +274,7 @@ static int tee_shm_alloc(int fd, tee_shm_t *shm, bool reg)
265
274
266
275
return ret ;
267
276
}
277
+ #endif /* !USE_ALLOC_IOC */
268
278
269
279
static void tee_shm_free (tee_shm_t * shm )
270
280
{
@@ -273,18 +283,11 @@ static void tee_shm_free(tee_shm_t *shm)
273
283
return ;
274
284
}
275
285
276
- if (shm -> fd > 0 )
277
- {
278
- /* Allocated via tee_shm_mmap() */
286
+ tee_shm_free_buf (shm -> ptr , shm -> size );
279
287
280
- munmap (shm -> ptr , shm -> size );
281
- close (shm -> fd );
282
- }
283
- else
288
+ if (shm -> fd >= 0 )
284
289
{
285
- /* Allocated via tee_shm_alloc() */
286
-
287
- free (shm -> ptr );
290
+ close (shm -> fd );
288
291
}
289
292
290
293
shm -> ptr = NULL ;
@@ -350,6 +353,7 @@ int main(int argc, FAR char *argv[])
350
353
}
351
354
352
355
par0 .attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT ;
356
+ par0 .c = TEE_MEMREF_NULL ;
353
357
354
358
ret = tee_invoke (fd , session , PTA_CMD_GET_DEVICES , & par0 , 1 );
355
359
if (ret < 0 )
@@ -372,7 +376,7 @@ int main(int argc, FAR char *argv[])
372
376
par0 .attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT ;
373
377
par0 .a = 0 ;
374
378
par0 .b = shm .size ;
375
- par0 .c = ( uintptr_t ) shm .ptr ;
379
+ par0 .c = shm .id ;
376
380
377
381
ret = tee_invoke (fd , session , PTA_CMD_GET_DEVICES , & par0 , 1 );
378
382
if (ret < 0 )
0 commit comments