blob: 03be0c104b5e7db46bd73cae1c162d0c243efd47 [file] [log] [blame]
Joakim Bech8e5c5b32018-10-25 08:18:32 +02001.. _build_trusted_applications:
2
3####################
4Trusted Applications
5####################
6This document tells how to implement a Trusted Application for OP-TEE, using
7OP-TEE's so called `TA-devkit` to both build and sign the Trusted Application
8binary. In this document, a `Trusted Application` running in the OP-TEE os is
9referred to as a `TA`.
10
11TA Mandatory files
12******************
13The Makefile for a Trusted Application must be written to rely on OP-TEE
14TA-devkit resources in order to successfully build the target application.
15TA-devkit is built when one builds :ref:`optee_os`.
16
17.. todo::
18
19 Joakim: We need to add CMake instructions also.
20
21To build a TA, one must provide:
22
23 - **Makefile**, a make file that should set some configuration variables and
24 include the TA-devkit make file.
25
26 - **sub.mk**, a make file that lists the sources to build (local source
27 files, subdirectories to parse, source file specific build directives).
28
29 - **user_ta_header_defines.h**, a specific ANSI-C header file to define most
30 of the TA properties.
31
32 - A implementation of at least the TA entry points, as extern functions:
33 ``TA_CreateEntryPoint()``, ``TA_DestroyEntryPoint()``,
34 ``TA_OpenSessionEntryPoint()``, ``TA_CloseSessionEntryPoint()``,
35 ``TA_InvokeCommandEntryPoint()``
36
37TA file layout example
38======================
39As an example, :ref:`hello_world` looks like this:
40
41.. code-block:: none
42
43 hello_world/
44 ├── ...
45 └── ta
46 ├── Makefile BINARY=<uuid>
47 ├── Android.mk Android way to invoke the Makefile
48 ├── sub.mk srcs-y += hello_world_ta.c
49 ├── include
50 │   └── hello_world_ta.h Header exported to non-secure: TA commands API
51 ├── hello_world_ta.c Implementaion of TA entry points
52 └── user_ta_header_defines.h TA_UUID, TA_FLAGS, TA_DATA/STACK_SIZE, ...
53
54TA Makefile Basics
55******************
56Required variables
57==================
58The main TA-devkit make file is located in in :ref:`optee_os` at
59``ta/mk/ta_dev_kit.mk``. The make file supports make targets such as ``all`` and
60``clean`` to build a TA or a library and clean the built objects.
61
62The make file expects a couple of configuration variables:
63
64TA_DEV_KIT_DIR
65 Base directory of the TA-devkit. Used the TA-devkit itself to locate its tools.
66
67BINARY and LIBNAME
68 These are exclusive, meaning that you cannot use both at the same time. If
69 building a TA, ``BINARY`` shall provide the TA filename used to load the TA.
70 The built and signed TA binary file will be named ``${BINARY}.ta``. In
71 native OP-TEE, it is the TA UUID, used by tee-supplicant to identify TAs. If
72 one is building a static library (that will be later linked by a TA), then
73 ``LIBNAME`` shall provide the name of the library. The generated library
74 binary file will be named ``lib${LIBNAME}.a``
75
76CROSS_COMPILE and CROSS_COMPILE32
77 Cross compiler for the TA or the library source files. ``CROSS_COMPILE32``
78 is optional. It allows to target AArch32 builds on AArch64 capable systems.
79 On AArch32 systems, ``CROSS_COMPILE32`` defaults to ``CROSS_COMPILE``.
80
81Optional variables
82==================
83Some optional configuration variables can be supported, for example:
84
85O
86 Base directory for build objects filetree. If not set, TA-devkit defaults to
87 **./out** from the TA source tree base directory.
88
89Example Makefile
90================
91A typical Makefile for a TA looks something like this
92
93.. code-block:: Makefile
94
95 # Append specific configuration to the C source build (here log=info)
96 # The UUID for the Trusted Application
97 BINARY=8aaaf200-2450-11e4-abe2-0002a5d5c51b
98
99 # Source the TA-devkit make file
100 include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk
101
102.. _build_trusted_applications_submk:
103
104sub.mk directives
105=================
106The make file expects that current directory contains a file ``sub.mk`` that is
107the entry point for listing the source files to build and other specific build
108directives. Here are a couple of examples of directives one can implement in a
109sub.mk make file:
110
111.. code-block:: Makefile
112
113 # Adds /hello_world_ta.c from current directory to the list of the source
114 # file to build and link.
115 srcs-y += hello_world_ta.c
116
117 # Includes path **./include/** from the current directory to the include
118 # path.
119 global-incdirs-y += include/
120
121 # Adds directive -Wno-strict-prototypes only to the file hello_world_ta.c
122 cflags-hello_world_ta.c-y += -Wno-strict-prototypes
123
124 # Removes directive -Wno-strict-prototypes from the build directives for
125 # hello_world_ta.c only.
126 cflags-remove-hello_world_ta.c-y += -Wno-strict-prototypes
127
128 # Adds the static library foo to the list of the linker directive -lfoo.
129 libnames += foo
130
131 # Adds the directory path to the libraries pathes list. Archive file
132 # libfoo.a is expectd in this directory.
133 libdirs += path/to/libfoo/install/directory
134
135 # Adds the static library binary to the TA build dependencies.
136 libdeps += path/to/greatlib/libgreatlib.a
137
138Android Build Environment
139*************************
140.. todo::
141
142 Joakim: Move this to the AOSP page?
143
144OP-TEE's TA-devkit supports building in an Android build environment. One can
145write an ``Android.mk`` file for the TA (stored side by side with the Makefile).
146Android's build system will parse the ``Android.mk`` file for the TA which in
147turn will parse a TA-devkit Android make file to locate TA build resources. Then
148the Android build will execute a ``make`` command to built the TA through its
149generic Makefile file.
150
151A typical ``Android.mk`` file for a TA looks like this (``Android.mk`` for
152:ref:`hello_world` is used as an example here).
153
154.. code-block:: Makefile
155
156 # Define base path for the TA sources filetree
157 LOCAL_PATH := $(call my-dir)
158
159 # Define the module name as the signed TA binary filename.
160 local_module := 8aaaf200-2450-11e4-abe2-0002a5d5c51b.ta
161
162 # Include the devikt Android mak script
163 include $(OPTEE_OS_DIR)/mk/aosp_optee.mk
164
165TA Mandatory Entry Points
166*************************
167A TA must implement a couple of mandatory entry points, these are:
168
169.. code-block:: c
170
171 TEE_Result TA_CreateEntryPoint(void)
172 {
173 /* Allocate some resources, init something, ... */
174 ...
175
176 /* Return with a status */
177 return TEE_SUCCESS;
178 }
179
180 void TA_DestroyEntryPoint(void)
181 {
182 /* Release resources if required before TA destruction */
183 ...
184 }
185
186 TEE_Result TA_OpenSessionEntryPoint(uint32_t ptype,
187 TEE_Param param[4],
188 void **session_id_ptr)
189 {
190 /* Check client identity, and alloc/init some session resources if any */
191 ...
192
193 /* Return with a status */
194 return TEE_SUCCESS;
195 }
196
197 void TA_CloseSessionEntryPoint(void *sess_ptr)
198 {
199 /* check client and handle session resource release, if any */
200 ...
201 }
202
203 TEE_Result TA_InvokeCommandEntryPoint(void *session_id,
204 uint32_t command_id,
205 uint32_t parameters_type,
206 TEE_Param parameters[4])
207 {
208 /* Decode the command and process execution of the target service */
209 ...
210
211 /* Return with a status */
212 return TEE_SUCCESS;
213 }
214
215.. _build_ta_properties:
216
217TA Properties
218*************
219Trusted Application properties shall be defined in a header file named
220``user_ta_header_defines.h``, which should contain:
221
222 - ``TA_UUID`` defines the TA uuid value
223 - ``TA_FLAGS`` define some of the TA properties
224 - ``TA_STACK_SIZE`` defines the RAM size to be reserved for TA stack
225 - ``TA_DATA_SIZE`` defines the RAM size to be reserved for TA heap (TEE_Malloc()
226 pool)
227
228Refer to :ref:`ta_properties` to understand how to configure these macros.
229
230.. _user_ta_header_defines_h:
231
232Example of a property header file
233=================================
234
235.. code-block:: c
236
237 #ifndef USER_TA_HEADER_DEFINES_H
238 #define USER_TA_HEADER_DEFINES_H
239
240 #define TA_UUID
241 { 0x8aaaf200, 0x2450, 0x11e4, \
242 { 0xab, 0xe2, 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b} }
243
244 #define TA_FLAGS (TA_FLAG_EXEC_DDR | \
245 TA_FLAG_SINGLE_INSTANCE | \
246 TA_FLAG_MULTI_SESSION)
247 #define TA_STACK_SIZE (2 * 1024)
248 #define TA_DATA_SIZE (32 * 1024)
249
250 #define TA_CURRENT_TA_EXT_PROPERTIES \
251 { "gp.ta.description", USER_TA_PROP_TYPE_STRING, "Foo TA for some purpose." }, \
252 { "gp.ta.version", USER_TA_PROP_TYPE_U32, &(const uint32_t){ 0x0100 } }
253
254 #endif /* USER_TA_HEADER_DEFINES_H */
255
256.. note::
257
258 It is recommended to use the ``TA_CURRENT_TA_EXT_PROPERTIES`` as above to
259 define extra properties of the TA.
260
261Checking TA parameters
262**********************
263GlobalPlatforms TEE Client APIs ``TEEC_InvokeCommand()`` and
264``TEE_OpenSession()`` allow clients to invoke a TA with some invocation
265parameters: values or references to memory buffers. It is mandatory that TA's
266verify the parameters types before using the parameters themselves. For this a
267TA can rely on the macro ``TEE_PARAM_TYPE_GET(param_type, param_index)`` to get
268the type of a parameter and check its value according to the expected parameter.
269
270For example, if a TA expects that command ID 0 comes with ``params[0]`` being a
271input value, ``params[1]`` being a output value, and ``params[2]`` being a
272in/out memory reference (buffer), then the TA should implemented the following
273sequence:
274
275.. code-block:: c
276
277 TEE_Result handle_command_0(void *session, uint32_t cmd_id,
278 uint32_t param_types, TEE_Param params[4])
279 {
280 if ((TEE_PARAM_TYPE_GET(param_types, 0) != TEE_PARAM_TYPE_VALUE_IN) ||
281 (TEE_PARAM_TYPE_GET(param_types, 1) != TEE_PARAM_TYPE_VALUE_OUT) ||
282 (TEE_PARAM_TYPE_GET(param_types, 2) != TEE_PARAM_TYPE_MEMREF_INOUT) ||
283 (TEE_PARAM_TYPE_GET(param_types, 3) != TEE_PARAM_TYPE_NONE)) {
284 return TEE_ERROR_BAD_PARAMETERS
285 }
286
287 /* process command */
288 ...
289 }
290
291 TEE_Result TA_InvokeCommandEntryPoint(void *session, uint32_t command_id,
292 uint32_t param_types, TEE_Param params[4])
293 {
294 switch (command_id) {
295 case 0:
296 return handle_command_0(session, param_types, params);
297
298 default:
299 return TEE_ERROR_NOT_SUPPORTED;
300 }
301 }