blob: 9343aab2d458efa85b7503dc4282e436c581bcef [file] [log] [blame]
Edison Ai1b7e8452019-12-16 10:03:52 +08001#######################
2Adding Secure Partition
3#######################
4
Edison Ai1b7e8452019-12-16 10:03:52 +08005***********************
6Terms and abbreviations
7***********************
8This document uses the following terms and abbreviations.
9
10.. table:: term table
11 :widths: auto
12
13 ================== ==================================
14 **Term** **Meaning**
15 ================== ==================================
16 FF Firmware Framework
17 ID Identifier
18 IPC Interprocess communication
19 IPC model The secure IPC framework
20 irqs Interrupt requests
21 Library model The secure function call framework
22 MMIO Memory Mapped I/O
23 PSA Platform Security Architecture
24 RoT Root of Trust
25 SID RoT Service ID
26 SP Secure Partition
27 SPM Secure Partition Manager
28 TF-M Trusted firmware M
29 ================== ==================================
30
31************
32Introduction
33************
34Secure Partition is an execution environment that provides the following
35functions to Root of Trust (RoT) Services:
36
37- Access to resources, protection of its own code and data.
38- Mechanisms to interact with other components in the system.
39
40Each Secure Partition is a single thread of execution and the smallest unit of
41isolation.
42
43This document mainly describes how to add a secure partition in TF-M and
44focuses on the configuration, manifest, implement rules. The actual
45source-level implementation is not included in this document.
46
47.. Note::
48 If not otherwise specified, the steps are identical for library and IPC
49 model.
50
51 The IPC model conforms the *PSA Firmware Framework (FF) v 1.0.0*. Refer to
52 `PSA Firmware Framework specification`_ for details.
53
54*******
55Process
56*******
57The main steps to add a secure partition are as follows:
58
59- `Add source folder`_
60- `Add manifest`_
61- `Add configuration`_
Edison Ai1b7e8452019-12-16 10:03:52 +080062- `Implement the RoT services`_
63
64Add source folder
65=================
Ken Liu738a4b02020-06-04 14:52:38 +080066Add a source folder under ``<TF-M base folder>/secure_fw/partitions`` for the new
Edison Ai1b7e8452019-12-16 10:03:52 +080067secure partition (Let's take EXAMPLE as the folder name):
68
69This folder should include those parts:
70
71- Manifest file: EXAMPLE.yaml
72- CMake configuration files
73- Source code files
74
75Add manifest
76============
77Each Secure Partition must have resource requirements declared in a manifest
78file. The Secure Partition Manager (SPM) uses the manifest file to assemble and
79allocate resources within the SPE. The manifest includes the following:
80
81- A Secure Partition name.
82- A list of implemented RoT Services.
83- Access to other RoT Services.
84- Memory requirements.
85- Scheduling hints.
86- Peripheral memory-mapped I/O regions and interrupts.
87
88.. Note::
89 The current manifest format in TF-M is "yaml" which is different from the
90 requirement of PSA FF.
91
92Here is a manifest reference example for the IPC model, please refer to
93`Library model support`_ for the library extend:
94
95.. code-block:: yaml
96
97 {
98 "psa_framework_version": 1.0,
99 "name": "TFM_SP_EXAMPLE",
100 "type": "PSA-ROT",
101 "priority": "HIGH",
Boris Deleticfb231172020-08-06 22:46:53 +0100102 "entry_point": "example_main",
Edison Ai1b7e8452019-12-16 10:03:52 +0800103 "stack_size": "0x0200",
104 "services" : [
105 {
106 "name": "ROT_A",
107 "sid": "0x0000F000",
108 "non_secure_clients": true,
109 "version": 1,
110 "version_policy": "STRICT"
111 }
112 ],
113 "mmio_regions": [
114 {
115 "name": "TFM_PERIPHERAL_A",
116 "permission": "READ-WRITE"
117 }
118 ],
119 "irqs": [
120 {
121 "source": "TFM_A_IRQ",
122 "signal": "SPM_CORE_A_IRQ",
123 "tfm_irq_priority": 64,
124 }
125 ],
126 "linker_pattern": {
127 "object_list": [
128 "*EXAMPLE.*"
129 ]
130 }
131 }
132
133Secure Partition ID Distribution
134--------------------------------
135Every Secure Partition has an identifier (ID). TF-M will generate a header file
136that includes definitions of the Secure Partition IDs. The header file is
137``<TF-M base folder>/interface/include/psa_manifest/pid.h``. Each definition
138uses the ``name`` attribute in the manifest as its name and the value is
139allocated by SPM.
140
141.. code-block:: c
142
143 #define name id-value
144
145Here is the Secure Partition ID table used in TF-M.
146
147.. table:: PID table
148 :widths: auto
149
150 =============================== =================
151 **Partition name** **Partition ID**
152 =============================== =================
153 Reserved 0-255
Kevin Pengc6d74502020-03-04 16:55:37 +0800154 TFM_SP_PS 256
Edison Ai1b7e8452019-12-16 10:03:52 +0800155 TFM_SP_ITS 257
156 TFM_SP_AUDIT_LOG 258
157 TFM_SP_CRYPTO 259
158 TFM_SP_PLATFORM 260
159 TFM_SP_INITIAL_ATTESTATION 261
160 TFM_SP_CORE_TEST 262
161 TFM_SP_CORE_TEST_2 263
162 TFM_SP_SECURE_TEST_PARTITION 264
163 TFM_SP_IPC_SERVICE_TEST 265
164 TFM_SP_IPC_CLIENT_TEST 266
165 TFM_IRQ_TEST_1 267
Kevin Pengc6d74502020-03-04 16:55:37 +0800166 TFM_SP_PS_TEST 268
Edison Ai1b7e8452019-12-16 10:03:52 +0800167 =============================== =================
168
169About where to add the definition, please refer to the chapter `Add
170configuration`_.
171
172RoT Service ID (SID) Distribution
173---------------------------------
174An RoT Service is identified by its RoT Service ID (SID). A SID is a 32-bit
175number that is associated with a symbolic name in the Secure Partition
176manifest. The bits [31:12] uniquely identify the vendor of the RoT Service.
177The remaining bits [11:0] can be used at the discretion of the vendor.
178
179Here is the RoT Service ID table used in TF-M.
180
181.. table:: SID table
182 :widths: auto
183
184 =========================== ====================== ========================
185 **Services** **Vendor ID(20 bits)** **Function ID(12 bits)**
186 =========================== ====================== ========================
187 audit_logging 0x00000 0x000-0x01F
188 initial_attestation 0x00000 0x020-0x03F
189 platform 0x00000 0x040-0x05F
Sherry Zhang07b42412021-01-07 14:19:41 +0800190 protected_storage 0x00000 0x060-0x06F
191 internal_trusted_storage 0x00000 0x070-0x07F
Edison Ai1b7e8452019-12-16 10:03:52 +0800192 crypto 0x00000 0x080-0x09F
Sherry Zhang07b42412021-01-07 14:19:41 +0800193 firmware_update 0x00000 0x0A0-0x0BF
Edison Ai1b7e8452019-12-16 10:03:52 +0800194 test_secure_service 0x0000F 0x000-0x01F
195 core_test 0x0000F 0x020-0x03F
196 core_test_2 0x0000F 0x040-0x05F
197 tfm_ipc_client 0x0000F 0x060-0x07F
198 tfm_ipc_service 0x0000F 0x080-0x09F
199 tfm_irq_test_service_1 0x0000F 0x0A0-0x0BF
Kevin Pengc6d74502020-03-04 16:55:37 +0800200 tfm_ps_test_service 0x0000F 0x0C0-0x0DF
Edison Ai1b7e8452019-12-16 10:03:52 +0800201 =========================== ====================== ========================
202
203mmio_regions
204------------
205This attribute is a list of MMIO region objects which the Secure Partition
206needs access to. TF-M only supports the ``named_region`` current. Please refer
207to PSA FF for more details about it. The user needs to provide a name macro to
208indicate the variable of the memory region.
209
210TF-M uses the below structure to indicate a peripheral memory.
211
212.. code-block:: c
213
Ken Liu172f1e32021-02-05 16:31:03 +0800214 struct platform_data_t {
Edison Ai1b7e8452019-12-16 10:03:52 +0800215 uint32_t periph_start;
216 uint32_t periph_limit;
217 int16_t periph_ppc_bank;
218 int16_t periph_ppc_loc;
219 };
220
221.. Note::
222 This structure is not expected by TF-M, it's only that the current
223 implementations are using. Other peripherals that need different information
224 to create isolation need to define a different structure with the same name.
225
226Here is a example for it:
227
228.. code-block:: c
229
Ken Liu172f1e32021-02-05 16:31:03 +0800230 struct platform_data_t tfm_peripheral_A;
Edison Ai1b7e8452019-12-16 10:03:52 +0800231 #define TFM_PERIPHERAL_A (&tfm_peripheral_A)
232
233linker_pattern
234--------------
235``linker_pattern`` is a legacy region which contains the minimum information
236required to link a Secure Partition’s compiled static objects. Now, it is
237required as 'IMPLEMENTATION DEFINED' in PSA FF 1.0.0.
238
239Library model support
240---------------------
241For the library model, the user needs to add a ``secure_functions`` item. The
242main difference between ``secure_function`` and ``services`` is the extra
243``signal`` key for secure function entry.
244
245The ``signal`` must be the upper case of the secure function name.
246
247.. code-block:: yaml
248
249 "secure_functions": [
250 {
251 "name": "TFM_EXAMPLE_A",
252 "signal": "EXAMPLE_A_FUNC",
253 "sid": "0x00000000",
254 "non_secure_clients": true,
255 "version": 1,
256 "version_policy": "STRICT"
257 },
258
259Add configuration
260=================
261The following configuration tasks are required for the newly added secure
262partition:
263
264Add CMake configure files
265-------------------------
Edison Ai1b7e8452019-12-16 10:03:52 +0800266- CMakeLists.txt, which is the compilation configuration for this module.
267
Boris Deleticfb231172020-08-06 22:46:53 +0100268The current CMake configuration should also be updated, by updating
Anton Komlevb8e3af02020-08-28 10:23:57 +0100269config_default.cmake to include the definition of the newly introduced partition
Boris Deleticfb231172020-08-06 22:46:53 +0100270and adding the relevant subdirectoy in ``secure_fw/CMakeLists.txt``.
Edison Ai1b7e8452019-12-16 10:03:52 +0800271Please refer to the source code of TF-M for more detail.
272
273Update manifest list
274--------------------
275The ``<TF-M base folder>/tools/tfm_manifest_list.yaml`` is used to collect
276necessary information of secure partition.
277
278- ``name``: The name string of the secure partition.
279- ``short_name``: should be the same as the ``name`` in the secure partition
280 manifest file.
281- ``manifest``: the relative path of the manifest file to TF-M root.
282- ``tfm_partition_ipc``: indicate if this partition is compatible with the IPC
283 model.
284- ``conditional``: Optional. Configure control macro for this partition.
285- ``version_major``: major version the partition manifest.
286- ``version_minor``: minor version the partition manifest.
287- ``pid``: Secure Partition ID value distributed in chapter `Secure Partition
288 ID Distribution`_.
289
290Reference configuration example:
291
292.. code-block:: yaml
293
294 {
295 "name": "Example Service",
296 "short_name": "TFM_SP_EXAMPLE",
Ken Liu738a4b02020-06-04 14:52:38 +0800297 "manifest": "secure_fw/partitions/EXAMPLE/tfm_example.yaml",
Edison Ai1b7e8452019-12-16 10:03:52 +0800298 "tfm_partition_ipc": true,
Boris Deleticfb231172020-08-06 22:46:53 +0100299 "conditional": "TFM_PARTITION_EXAMPLE",
Edison Ai1b7e8452019-12-16 10:03:52 +0800300 "version_major": 0,
301 "version_minor": 1,
302 "pid": 256
303 }
304
Anton Komlevb8e3af02020-08-28 10:23:57 +0100305.. Note::
306 The manifest configuration can be placed in a different external manifest
307 list. In this case, the cmake variable TFM_EXTRA_MANIFEST_LIST_PATH should be
308 set to the path of the external manifest list.
Edison Ai1b7e8452019-12-16 10:03:52 +0800309
310Implement the RoT services
311==========================
Boris Deleticfb231172020-08-06 22:46:53 +0100312To implement RoT services, the partition needs a source file which contains the
313implementations of the services, as well as the partition entry point. The user
314can create this source file under
315``<TF-M base folder>/secure_fw/partitions/EXAMPLE/EXAMPLE.c``. The linker
316detects source files according to the pattern matching defined by the
317"linker_pattern" attribute in the ``tfm_manifest_list.yaml`` file.
318
319As an example, the RoT service with SID **ROT_A** will be implemented.
320
321Entry point function
322--------------------
323This function acts as a main() function for the partition.
324On incoming signals for service calls, the entry point function handles
325signals by calling the relevant service function.
326An example entry point is given
327
328.. code-block:: c
329
330 void example_main(void)
331 {
332 psa_signal_t signals = 0;
333
334 while (1) {
335 signals = psa_wait(PSA_WAIT_ANY, PSA_BLOCK);
336 if (signals & ROT_A_SIGNAL) {
337 rot_A();
338 } else {
339 /* Should not come here */
340 psa_panic();
341 }
342 }
343 }
344
345Service implementation
346----------------------
347The service is implemented by the ``rot_A()`` function, which is called upon an
348incoming signal. This implementation is up to the user, however an example
349service has been included for reference. The following example sends a message
350"Hello World" when called.
351
352.. code-block:: c
353
354 static void rot_A(void)
355 {
356 const int BUFFER_LEN = 32;
357 psa_msg_t msg;
358 psa_status_t r;
359 int i;
360 uint8_t rec_buf[BUFFER_LEN];
361 uint8_t send_buf[BUFFER_LEN] = "Hello World";
362
363 psa_get(ROT_A_SIGNAL, &msg);
364 switch (msg.type) {
365 case PSA_IPC_CONNECT:
366 if (service_in_use & ROT_A_SIGNAL) {
367 r = PSA_ERROR_CONNECTION_REFUSED;
368 } else {
369 service_in_use |= ROT_A_SIGNAL;
370 r = PSA_SUCCESS;
371 }
372 psa_reply(msg.handle, r);
373 break;
374 case PSA_IPC_CALL:
375 for (i = 0; i < PSA_MAX_IOVEC; i++) {
376 if (msg.in_size[i] != 0) {
377 psa_read(msg.handle, i, rec_buf, BUFFER_LEN);
378 }
379 if (msg.out_size[i] != 0) {
380 psa_write(msg.handle, i, send_buf, BUFFER_LEN);
381 }
382 }
383 psa_reply(msg.handle, PSA_SUCCESS);
384 break;
385 case PSA_IPC_DISCONNECT:
386 assert((service_in_use & ROT_A_SIGNAL) != 0);
387 service_in_use &= ~ROT_A_SIGNAL;
388 psa_reply(msg.handle, PSA_SUCCESS);
389 break;
390 default:
391 /* cannot get here [broken SPM] */
392 psa_panic();
393 break;
394 }
395 }
396
397Test connection
398---------------
399To test that the service has been implemented correctly, the user needs to call
400it from somewhere. One option is to create a new testsuite, such as
401``<TF-M-test base folder>/test/suites/example/non_secure/example_ns_interface_testsuite.c``.
402
403.. code-block:: c
404
405 static void tfm_example_test_1001(struct test_result_t *ret)
406 {
407 char str1[] = "str1";
408 char str2[] = "str2";
409 char str3[128], str4[128];
410 struct psa_invec invecs[2] = {{str1, sizeof(str1)},
411 {str2, sizeof(str2)}};
412 struct psa_outvec outvecs[2] = {{str3, sizeof(str3)},
413 {str4, sizeof(str4)}};
414 psa_handle_t handle;
415 psa_status_t status;
416 uint32_t version;
417
418 version = psa_version(ROT_A_SID);
419 TEST_LOG("TFM service support version is %d.\r\n", version);
420 handle = psa_connect(ROT_A_SID, ROT_A_VERSION);
421 status = psa_call(handle, PSA_IPC_CALL, invecs, 2, outvecs, 2);
422 if (status >= 0) {
423 TEST_LOG("psa_call is successful!\r\n");
424 } else {
425 TEST_FAIL("psa_call is failed!\r\n");
426 return;
427 }
428
429 TEST_LOG("outvec1 is: %s\r\n", outvecs[0].base);
430 TEST_LOG("outvec2 is: %s\r\n", outvecs[1].base);
431 psa_close(handle);
432 ret->val = TEST_PASSED;
433 }
434
435Once the test and service has been implemented, the project can be built and
436executed. The user should see the "Hello World" message in the console as
437received by the testsuite.
438
439Further Notes
440-------------
Edison Ai1b7e8452019-12-16 10:03:52 +0800441
442- In the IPC model, Use PSA FF proposed memory accessing mechanism. SPM
443 provides APIs and checking between isolation boundaries, a free accessing
444 of memory can cause program panic.
445- In the IPC model, the memory checking inside partition runtime is
446 unnecessary. SPM handles the checking while memory accessing APIs are
447 called.
448- In the IPC model, the client ID had been included in the message structure
449 and secure partition can get it when calling psa_get() function. The secure
450 partition does not need to call ``tfm_core_get_caller_client_id()`` to get
451 the caller client ID anymore.
452- In the IPC model, SPM will check the security policy and partition
453 dependence between client and service. So the service does not need to
454 validate the secure caller anymore.
455
456*********
457Reference
458*********
459
460| `PSA Firmware Framework specification`_
461
462.. _PSA Firmware Framework specification: https://pages.arm.com/psa-
463 resources-ff.html?_ga=2.156169596.61580709.1542617040-1290528876.1541647333
464
465--------------
466
Sherry Zhang07b42412021-01-07 14:19:41 +0800467*Copyright (c) 2019-2021, Arm Limited. All rights reserved.*