Skip to content

Add GlobalPlatform API client library (libteec) and example app (optee_gp) #3074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions examples/optee/optee_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
* Pre-processor definitions
****************************************************************************/

#undef USE_ALLOC_IOC

#ifdef USE_ALLOC_IOC
# define tee_shm_alloc tee_shm_mmap
# define tee_shm_free_buf(p, s) munmap(p, s)
#else
# define tee_shm_alloc tee_shm_malloc
# define tee_shm_free_buf(p, s) ((void)s, free(p))
#endif

#define OPTEE_DEV "/dev/tee0"

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

shm->id = (int32_t)(uintptr_t)shm->ptr;

ioc_reg.addr = (uintptr_t)shm->ptr;
ioc_reg.length = shm->size;
ioc_reg.flags = TEE_SHM_REGISTER | TEE_SHM_SEC_REGISTER;
ioc_reg.id = shm->id;

return ioctl(fd, TEE_IOC_SHM_REGISTER, (unsigned long)&ioc_reg);
shm->fd = ioctl(fd, TEE_IOC_SHM_REGISTER, (unsigned long)&ioc_reg);
shm->id = ioc_reg.id;

return shm->fd < 0 ? shm->fd : 0;
}

#if 0 /* Not used */
#ifdef USE_ALLOC_IOC
static int tee_shm_mmap(int fd, tee_shm_t *shm, bool reg)
{
struct tee_ioctl_shm_alloc_data ioc_alloc;
Expand Down Expand Up @@ -235,9 +244,9 @@ static int tee_shm_mmap(int fd, tee_shm_t *shm, bool reg)

return ret;
}
#endif

static int tee_shm_alloc(int fd, tee_shm_t *shm, bool reg)
#else /* !USE_ALLOC_IOC */
static int tee_shm_malloc(int fd, tee_shm_t *shm, bool reg)
{
int ret = 0;

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

return ret;
}
#endif /* !USE_ALLOC_IOC */

static void tee_shm_free(tee_shm_t *shm)
{
Expand All @@ -273,18 +283,11 @@ static void tee_shm_free(tee_shm_t *shm)
return;
}

if (shm->fd > 0)
{
/* Allocated via tee_shm_mmap() */
tee_shm_free_buf(shm->ptr, shm->size);

munmap(shm->ptr, shm->size);
close(shm->fd);
}
else
if (shm->fd >= 0)
{
/* Allocated via tee_shm_alloc() */

free(shm->ptr);
close(shm->fd);
}

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

par0.attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
par0.c = TEE_MEMREF_NULL;

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

ret = tee_invoke(fd, session, PTA_CMD_GET_DEVICES, &par0, 1);
if (ret < 0)
Expand Down
33 changes: 33 additions & 0 deletions examples/optee_gp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ##############################################################################
# apps/examples/optee_gp/CMakeLists.txt
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you 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.
#
# ##############################################################################

if(CONFIG_EXAMPLES_OPTEE_GP)
nuttx_add_application(
NAME
${CONFIG_EXAMPLES_OPTEE_GP_PROGNAME}
SRCS
optee_gp_main.c
STACKSIZE
${CONFIG_EXAMPLES_OPTEE_GP_STACKSIZE}
PRIORITY
${CONFIG_EXAMPLES_OPTEE_GP_PRIORITY})
endif()
30 changes: 30 additions & 0 deletions examples/optee_gp/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#

config EXAMPLES_OPTEE_GP
tristate "OP-TEE GP API client example"
depends on LIBTEEC
default n
---help---
Enable the OP-TEE GP API client example which uses libteec

if EXAMPLES_OPTEE

config EXAMPLES_OPTEE_GP_PROGNAME
string "Program name"
default "optee_gp"
---help---
This is the name of the program that will be used when the NSH ELF
program is installed.

config EXAMPLES_OPTEE_GP_PRIORITY
int "OP-TEE GP task priority"
default 100

config EXAMPLES_OPTEE_GP_STACKSIZE
int "OP-TEE GP stack size"
default DEFAULT_TASK_STACKSIZE

endif
25 changes: 25 additions & 0 deletions examples/optee_gp/Make.defs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
############################################################################
# apps/examples/optee_gp/Make.defs
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you 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.
#
############################################################################

ifneq ($(CONFIG_EXAMPLES_OPTEE_GP),)
CONFIGURED_APPS += $(APPDIR)/examples/optee_gp
endif
34 changes: 34 additions & 0 deletions examples/optee_gp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
############################################################################
# apps/examples/optee_gp/Makefile
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you 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 $(APPDIR)/Make.defs

# OP-TEE GP API client built-in application info

PROGNAME = $(CONFIG_EXAMPLES_OPTEE_GP_PROGNAME)
PRIORITY = $(CONFIG_EXAMPLES_OPTEE_GP_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_OPTEE_GP_STACKSIZE)
MODULE = $(CONFIG_EXAMPLES_OPTEE_GP)

MAINSRC = optee_gp_main.c

include $(APPDIR)/Application.mk
Loading
Loading