Skip to content

Commit b7ff4e0

Browse files
committed
examples/optee: Correct use of shm reg id and flags
Previous version of optee example was using id during shm registration as input whereas it is an output variable. It was also specifying flags to dictate the behaviour whereas the latest implementation of the driver prohibits their use. This commit addresses those issues. Signed-off-by: George Poulios <[email protected]>
1 parent 30ef8ff commit b7ff4e0

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

examples/optee/optee_main.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
* Pre-processor definitions
4040
****************************************************************************/
4141

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+
4252
#define OPTEE_DEV "/dev/tee0"
4353

4454
#define PTA_DEVICE_ENUM { 0x7011a688, 0xddde, 0x4053, \
@@ -183,17 +193,16 @@ static int tee_shm_register(int fd, tee_shm_t *shm)
183193
return -EINVAL;
184194
}
185195

186-
shm->id = (int32_t)(uintptr_t)shm->ptr;
187-
188196
ioc_reg.addr = (uintptr_t)shm->ptr;
189197
ioc_reg.length = shm->size;
190-
ioc_reg.flags = TEE_SHM_REGISTER | TEE_SHM_SEC_REGISTER;
191-
ioc_reg.id = shm->id;
192198

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;
194203
}
195204

196-
#if 0 /* Not used */
205+
#ifdef USE_ALLOC_IOC
197206
static int tee_shm_mmap(int fd, tee_shm_t *shm, bool reg)
198207
{
199208
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)
235244

236245
return ret;
237246
}
238-
#endif
239247

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)
241250
{
242251
int ret = 0;
243252

@@ -265,6 +274,7 @@ static int tee_shm_alloc(int fd, tee_shm_t *shm, bool reg)
265274

266275
return ret;
267276
}
277+
#endif /* !USE_ALLOC_IOC */
268278

269279
static void tee_shm_free(tee_shm_t *shm)
270280
{
@@ -273,18 +283,11 @@ static void tee_shm_free(tee_shm_t *shm)
273283
return;
274284
}
275285

276-
if (shm->fd > 0)
277-
{
278-
/* Allocated via tee_shm_mmap() */
286+
tee_shm_free_buf(shm->ptr, shm->size);
279287

280-
munmap(shm->ptr, shm->size);
281-
close(shm->fd);
282-
}
283-
else
288+
if (shm->fd >= 0)
284289
{
285-
/* Allocated via tee_shm_alloc() */
286-
287-
free(shm->ptr);
290+
close(shm->fd);
288291
}
289292

290293
shm->ptr = NULL;
@@ -350,6 +353,7 @@ int main(int argc, FAR char *argv[])
350353
}
351354

352355
par0.attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
356+
par0.c = TEE_MEMREF_NULL;
353357

354358
ret = tee_invoke(fd, session, PTA_CMD_GET_DEVICES, &par0, 1);
355359
if (ret < 0)
@@ -372,7 +376,7 @@ int main(int argc, FAR char *argv[])
372376
par0.attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
373377
par0.a = 0;
374378
par0.b = shm.size;
375-
par0.c = (uintptr_t)shm.ptr;
379+
par0.c = shm.id;
376380

377381
ret = tee_invoke(fd, session, PTA_CMD_GET_DEVICES, &par0, 1);
378382
if (ret < 0)

0 commit comments

Comments
 (0)