Skip to content

Commit 67db0af

Browse files
gpouliosxiaoxiang781216
authored andcommitted
tee/libteec: Add optee_client/libteec library
Support for downloading, patching and linking against optee_client/libteec by NuttX apps. Defaults to version 4.6.0. Enabled with CONFIG_LIBTEEC. More info: - https://github.com/OP-TEE/optee_client - https://optee.readthedocs.io/en/latest/architecture/globalplatform_api.html#tee-client-api - https://globalplatform.org/specs-library/?filter-committee=tee (GPD_SPE_007) Signed-off-by: George Poulios <[email protected]>
1 parent 677089b commit 67db0af

10 files changed

+322
-0
lines changed

tee/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Kconfig

tee/CMakeLists.txt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ##############################################################################
2+
# apps/tee/CMakeLists.txt
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
7+
# license agreements. See the NOTICE file distributed with this work for
8+
# additional information regarding copyright ownership. The ASF licenses this
9+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
10+
# use this file except in compliance with the License. You may obtain a copy of
11+
# the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
# License for the specific language governing permissions and limitations under
19+
# the License.
20+
#
21+
# ##############################################################################
22+
23+
nuttx_add_subdirectory()
24+
25+
nuttx_generate_kconfig(MENUDESC "TEE Libraries Support")

tee/Make.defs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
############################################################################
2+
# apps/tee/Make.defs
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
include $(wildcard $(APPDIR)/tee/*/Make.defs)

tee/Makefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# apps/tee/Makefile
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
MENUDESC = "TEE Libraries Support"
22+
23+
include $(APPDIR)/Directory.mk

tee/libteec/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/optee_client
2+
/*.zip
3+
/*.tar.gz

tee/libteec/0001-libteec-NuttX.patch

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
From 70a12eb84a1276cad15bc2ac867ffad513d5c732 Mon Sep 17 00:00:00 2001
2+
From: George Poulios <[email protected]>
3+
Date: Sun, 11 May 2025 00:44:22 +0300
4+
Subject: [PATCH] libteec: NuttX patches
5+
6+
Fix use of gettid() syscall in teec_trace.c and
7+
replace include of linux/tee.h with nuttx/tee.h.
8+
9+
Signed-off-by: George Poulios <[email protected]>
10+
---
11+
libteec/src/tee_client_api.c | 6 +++++-
12+
libteec/src/teec_trace.c | 2 +-
13+
2 files changed, 6 insertions(+), 2 deletions(-)
14+
15+
diff --git a/libteec/src/tee_client_api.c b/libteec/src/tee_client_api.c
16+
index 512fdac..b54e0b6 100644
17+
--- a/libteec/src/tee_client_api.c
18+
+++ b/libteec/src/tee_client_api.c
19+
@@ -44,7 +44,11 @@
20+
#ifndef __aligned
21+
#define __aligned(x) __attribute__((__aligned__(x)))
22+
#endif
23+
-#include <linux/tee.h>
24+
+#ifdef __NuttX__
25+
+# include <nuttx/tee.h>
26+
+#else
27+
+# include <linux/tee.h>
28+
+#endif
29+
30+
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
31+
32+
diff --git a/libteec/src/teec_trace.c b/libteec/src/teec_trace.c
33+
index 7194c8c..025cc4b 100644
34+
--- a/libteec/src/teec_trace.c
35+
+++ b/libteec/src/teec_trace.c
36+
@@ -75,7 +75,7 @@ void _dprintf(const char *function, int line, int level, const char *prefix,
37+
va_list ap;
38+
39+
if (function) {
40+
- int thread_id = syscall(SYS_gettid);
41+
+ int thread_id = (int)gettid();
42+
43+
n = snprintf(msg, sizeof(msg), "%s [%d] %s:%s:%d: ",
44+
trace_level_strings[level], thread_id, prefix,
45+
--
46+
2.34.1
47+

tee/libteec/CMakeLists.txt

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# ##############################################################################
2+
# apps/tee/libteec/CMakeLists.txt
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Copyright (C) 2023 Xiaomi Corporation
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
9+
# use this file except in compliance with the License. You may obtain a copy of
10+
# the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17+
# License for the specific language governing permissions and limitations under
18+
# the License.
19+
#
20+
# ##############################################################################
21+
22+
if(CONFIG_LIBTEEC)
23+
24+
set(OPTEE_CLIENT_DIR ${CMAKE_CURRENT_LIST_DIR}/optee_client)
25+
26+
if(NOT EXISTS ${OPTEE_CLIENT_DIR})
27+
FetchContent_Declare(
28+
optee_client_fetch
29+
URL https://github.com/OP-TEE/optee_client/archive/refs/tags/${CONFIG_LIBTEEC_VERSION}.tar.gz
30+
SOURCE_DIR
31+
${OPTEE_CLIENT_DIR}
32+
BINARY_DIR
33+
${CMAKE_BINARY_DIR}/tee/libteec/optee_client
34+
PATCH_COMMAND patch -p1 -d ${OPTEE_CLIENT_DIR} <
35+
${CMAKE_CURRENT_LIST_DIR}/0001-libteec-NuttX.patch
36+
DOWNLOAD_NO_PROGRESS true
37+
TIMEOUT 30)
38+
39+
FetchContent_GetProperties(optee_client_fetch)
40+
41+
if(NOT optee_client_fetch_POPULATED)
42+
FetchContent_Populate(optee_client_fetch)
43+
endif()
44+
endif()
45+
46+
set_property(
47+
TARGET nuttx
48+
APPEND
49+
PROPERTY NUTTX_INCLUDE_DIRECTORIES ${OPTEE_CLIENT_DIR}/libteec/include)
50+
51+
nuttx_add_library(libteec STATIC)
52+
53+
target_sources(libteec PRIVATE optee_client/libteec/src/tee_client_api.c
54+
optee_client/libteec/src/teec_trace.c)
55+
target_include_directories(libteec
56+
PRIVATE ${OPTEE_CLIENT_DIR}/libteec/include)
57+
target_compile_definitions(libteec PRIVATE BINARY_PREFIX=\"TEEC\")
58+
59+
endif()

tee/libteec/Kconfig

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
############################################################################
2+
# apps/tee/libteec/Kconfig
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Copyright (C) 2023 Xiaomi Corporation
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
############################################################################
21+
22+
config LIBTEEC
23+
bool "TEE client library (libteec)"
24+
default n
25+
---help---
26+
Enable libteec from https://github.com/OP-TEE/optee_client. This
27+
is OP-TEE project's implementation of GlobalPlatform's TEE client
28+
API specification v1.0 (GPD_SPE_007):
29+
https://globalplatform.org/specs-library/?filter-committee=tee
30+
The TEE Client API describes and defines how a client running in
31+
a rich operating environment (REE, in this case NuttX) should
32+
communicate with the Trusted Execution Environment (TEE) and its
33+
Trusted Applications (TAs). The library provides a
34+
well-established and easy-to-use interface abstracting away much
35+
of the details of the underlying subsystems. For more information
36+
please refer to:
37+
https://optee.readthedocs.io/en/latest/architecture/globalplatform_api.html#tee-client-api
38+
39+
if LIBTEEC
40+
41+
config LIBTEEC_VERSION
42+
string "optee_client version (4.6.0)"
43+
default "4.6.0"
44+
45+
endif # LIBTEEC

tee/libteec/Make.defs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
############################################################################
2+
# apps/tee/libteec/Make.defs
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Copyright (C) 2023 Xiaomi Corporation
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
############################################################################
21+
22+
ifneq ($(CONFIG_LIBTEEC),)
23+
CONFIGURED_APPS += $(APPDIR)/tee/libteec
24+
25+
FLAGS += ${INCDIR_PREFIX}$(APPDIR)/tee/libteec/optee_client/libteec/include
26+
FLAGS += ${DEFINE_PREFIX}BINARY_PREFIX="\"TEEC\""
27+
28+
ifneq ($(CONFIG_DEBUG_INFO),)
29+
FLAGS += ${DEFINE_PREFIX}DEBUGLEVEL=3
30+
else ifneq ($(CONFIG_DEBUG_WARN),)
31+
FLAGS += ${DEFINE_PREFIX}DEBUGLEVEL=2
32+
else ifneq ($(CONFIG_DEBUG_ERROR),)
33+
FLAGS += ${DEFINE_PREFIX}DEBUGLEVEL=1
34+
else
35+
# the default DEBUGLEVEL are 1(with error level)
36+
FLAGS += ${DEFINE_PREFIX}DEBUGLEVEL=1
37+
endif
38+
39+
AFLAGS += $(FLAGS)
40+
CFLAGS += $(FLAGS)
41+
CXXFLAGS += $(FLAGS)
42+
43+
DEPPATH += --dep-path libteec
44+
VPATH += :libteec
45+
46+
endif

tee/libteec/Makefile

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
############################################################################
2+
# apps/tee/libteec/Makefile
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
# Copyright (C) 2023 Xiaomi Corporation
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
############################################################################
21+
22+
include $(APPDIR)/Make.defs
23+
24+
LIBTEEC_VERSION = $(patsubst "%",%,$(strip $(CONFIG_LIBTEEC_VERSION)))
25+
LIBTEEC_URL ?= "https://github.com/OP-TEE/optee_client/archive/refs/tags"
26+
LIBTEEC_ZIP = $(LIBTEEC_VERSION).zip
27+
LIBTEEC_UNPACKNAME = optee_client
28+
UNPACK ?= unzip -q -o
29+
30+
CSRCS += optee_client/libteec/src/tee_client_api.c
31+
CSRCS += optee_client/libteec/src/teec_trace.c
32+
33+
$(LIBTEEC_ZIP):
34+
@echo "Downloading: $(LIBTEEC_URL)/$(LIBTEEC_ZIP)"
35+
$(Q) $(call DOWNLOAD,$(LIBTEEC_URL),$(LIBTEEC_ZIP))
36+
37+
$(LIBTEEC_UNPACKNAME): $(LIBTEEC_ZIP)
38+
@echo "Unpacking: $(LIBTEEC_ZIP) -> $(LIBTEEC_UNPACKNAME)"
39+
$(Q) $(UNPACK) $(LIBTEEC_ZIP)
40+
$(Q) mv $(LIBTEEC_UNPACKNAME)-$(LIBTEEC_VERSION) $(LIBTEEC_UNPACKNAME)
41+
$(Q) echo "Patching $(LIBTEEC_UNPACKNAME)"
42+
$(Q) patch -p1 -d $(LIBTEEC_UNPACKNAME) < 0001-libteec-NuttX.patch
43+
$(Q) touch $(LIBTEEC_UNPACKNAME)
44+
45+
ifeq ($(wildcard $(LIBTEEC_UNPACKNAME)/.git),)
46+
context:: $(LIBTEEC_UNPACKNAME)
47+
48+
distclean::
49+
$(Q) rm -rf $(LIBTEEC_UNPACKNAME)
50+
endif
51+
52+
include $(APPDIR)/Application.mk

0 commit comments

Comments
 (0)