Skip to content

Commit 77591fb

Browse files
author
Cruz Monrreal
authored
Merge pull request #9772 from kfnta/tfm_extras
Additions to TF-M source integration
2 parents 2ecb0a5 + 7016ac7 commit 77591fb

File tree

15 files changed

+728
-38
lines changed

15 files changed

+728
-38
lines changed
Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,4 @@
11
{
2-
"name": "tfm",
3-
"config": {
4-
"handle_pool_size": {
5-
"help": "maximum number of handles that can be opened at the same time",
6-
"macro_name": "TFM_CONN_HANDLE_MAX_NUM",
7-
"value": 10
8-
},
9-
"rot_pool_size": {
10-
"help": "maximum number of RoT services allowed",
11-
"macro_name": "TFM_SPM_MAX_ROT_SERV_NUM",
12-
"value": 20
13-
},
14-
"message_pool_size": {
15-
"help": "maximum number of RoT services allowed",
16-
"macro_name": "TFM_MSG_QUEUE_MAX_MSG_NUM",
17-
"value": 10
18-
}
19-
}
2+
"name": "tfm-s",
3+
"macros": ["MBED_FAULT_HANDLER_DISABLED", "BYPASS_NVSTORE_CHECK=1"]
204
}
21-

components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/secure_fw/core/ipc/tfm_spm.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ REGION_DECLARE_EXT(Image$$, ARM_LIB_HEAP, $$ZI$$Base);
470470
REGION_DECLARE_EXT(Image$$, ARM_LIB_HEAP, $$ZI$$Limit);
471471
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$Base);
472472
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$Limit);
473+
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$ZI$$Base);
474+
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$ZI$$Limit);
473475
REGION_DECLARE_EXT(Image$$, TFM_SECURE_STACK, $$ZI$$Base);
474476
REGION_DECLARE_EXT(Image$$, TFM_SECURE_STACK, $$ZI$$Limit);
475477
REGION_DECLARE_EXT(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
@@ -534,6 +536,12 @@ int32_t tfm_memory_check(void *buffer, size_t len, int32_t ns_caller)
534536
return IPC_SUCCESS;
535537
}
536538

539+
base = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$ZI$$Base);
540+
limit = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$ZI$$Limit);
541+
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
542+
return IPC_SUCCESS;
543+
}
544+
537545
base = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$Base);
538546
limit = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$Limit);
539547
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
@@ -552,6 +560,12 @@ int32_t tfm_memory_check(void *buffer, size_t len, int32_t ns_caller)
552560
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
553561
return IPC_SUCCESS;
554562
}
563+
564+
base = (uintptr_t)S_CODE_START;
565+
limit = (uintptr_t)(S_CODE_START + S_CODE_SIZE);
566+
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
567+
return IPC_SUCCESS;
568+
}
555569
}
556570

557571
return IPC_ERROR_MEMORY_CHECK;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "tfm",
3+
"macros": [
4+
"TFM_PSA_API", "MBED_TZ_DEFAULT_ACCESS=1"
5+
],
6+
"config": {
7+
"level": {
8+
"help": "TFM security level",
9+
"macro_name": "TFM_LVL",
10+
"value": 1
11+
},
12+
"handle_pool_size": {
13+
"help": "maximum number of handles that can be opened at the same time",
14+
"macro_name": "TFM_CONN_HANDLE_MAX_NUM",
15+
"value": 10
16+
},
17+
"rot_pool_size": {
18+
"help": "maximum number of RoT services allowed",
19+
"macro_name": "TFM_SPM_MAX_ROT_SERV_NUM",
20+
"value": 20
21+
},
22+
"message_pool_size": {
23+
"help": "maximum number of active messages allowed",
24+
"macro_name": "TFM_MSG_QUEUE_MAX_MSG_NUM",
25+
"value": 10
26+
}
27+
}
28+
}
29+

components/TARGET_PSA/TARGET_TFM/tf-m-integration.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ The target should be represented in a following way in `target.json` (`MUSCA_A1`
6464
"core": "Cortex-M33-NS",
6565
"device_has_add": ["INTERRUPTIN", "LPTICKER", "SERIAL", "SLEEP", "USTICKER"],
6666
"macros": [
67-
"MBED_TZ_DEFAULT_ACCESS=1",
6867
"MBED_FAULT_HANDLER_DISABLED",
69-
"TFM_PSA_API",
7068
"MBEDTLS_PSA_CRYPTO_C"
7169
],
7270
"extra_labels_add": ["MUSCA_A1_NS", "PSA", "TFM"],
@@ -77,16 +75,10 @@ The target should be represented in a following way in `target.json` (`MUSCA_A1`
7775
"core": "Cortex-M33",
7876
"device_has_add": ["FLASH"],
7977
"macros": [
80-
"MBED_FAULT_HANDLER_DISABLED",
8178
"MBED_MPU_CUSTOM",
82-
"BYPASS_NVSTORE_CHECK",
83-
"TFM_LVL=1",
84-
"TFM_PSA_API",
8579
"MBEDTLS_PSA_CRYPTO_SPM",
8680
"MBEDTLS_PSA_CRYPTO_C",
87-
"MBEDTLS_ENTROPY_NV_SEED",
88-
"MBEDTLS_PLATFORM_NV_SEED_READ_MACRO=mbed_default_seed_read",
89-
"MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO=mbed_default_seed_write"
81+
"MBEDTLS_ENTROPY_NV_SEED"
9082
],
9183
"components_add": ["FLASHIAP"],
9284
"extra_labels_add": ["MUSCA_A1_S", "PSA", "TFM"]
@@ -116,4 +108,4 @@ Mbed-OS contains customized TF-M version. TF-M services reference implementation
116108

117109
The porting layer consists of:
118110
- All functions listed in: `components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/platform/include/tfm_spm_hal.h`
119-
- Flash API `mbed-os/hal/flash_api.h` implementation is required for TZ image. It is used by PSA Internal trusted storage implementation.
111+
- Flash API `mbed-os/hal/flash_api.h` implementation is required for TZ image. It is used by PSA Internal trusted storage implementation.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ fuzzywuzzy>=0.11,<=0.17
2121
pyelftools>=0.24,<=0.25
2222
git+https://github.com/armmbed/[email protected]
2323
icetea>=1.2.1,<1.3
24+
pycryptodome>=3.7.2,<=3.7.3

tools/importer/tfm_importer.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@
8888
}
8989
],
9090
"commit_sha" : [
91-
"11e5abc451acc7e7596e01b0f5605b4ad3e1965e",
92-
"9541a37d7c878d057a40734ab4174cb46d81a922",
93-
"e87efab83af6273a12b471ab574ddbf4359ff0de",
94-
"bc275ff42a5c6275efffce81c91cce37e3749a3f",
95-
"fb6b17dcdd59faa023e7940a6bb2f052956044c0",
96-
"38bd4a279d22ff083d37c7f6a034d4a589e3527e",
97-
"8c33f1e25ada6e3cbc15bd982825473ba9a57540",
98-
"1134fd4dbb4245d19c010861a9f0bec7210c9701"
91+
"1f30b52488e88704cdb51c8c26e7225e580e5cc2",
92+
"5f2e4b3911ca3a776fab55a6d12054904614cced",
93+
"0c23e8698958b6e716114267fbdf6d82a16b6e0c",
94+
"fc7864055982f3b8e7e556f9dd8d0c94a1c772bc",
95+
"011fa28110ccfd87554f9a1f06c2eb8a8a34d1e0",
96+
"da01e3411fcf8010f4ae581946cf4b0e7753e024",
97+
"008bf1b2d176c04fb33985e0677e90e3800cb4ca",
98+
"67a4e6d723dfb199fdbe9a2cef57dbe58cbc026f",
99+
"6354b9a47351a29ea24023c5471b63d39e091959"
99100
]
100101
}
101102

tools/psa/tfm/__init__.py

Whitespace-only changes.

tools/psa/tfm/bin_utils/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2017-2018 ARM Limited
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from .assemble import Assembly
18+
19+
__all__ = [
20+
'Assembly'
21+
]

tools/psa/tfm/bin_utils/assemble.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#! /usr/bin/env python3
2+
#
3+
# Copyright 2017 Linaro Limited
4+
# Copyright (c) 2017-2018, Arm Limited.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
"""
19+
Assemble multiple images into a single image that can be flashed on the device.
20+
"""
21+
22+
import argparse
23+
import errno
24+
import io
25+
import re
26+
import os
27+
import shutil
28+
29+
offset_re = re.compile(r"^#define ([0-9A-Z_]+)_IMAGE_OFFSET\s+((0x)?[0-9a-fA-F]+)")
30+
size_re = re.compile(r"^#define ([0-9A-Z_]+)_IMAGE_MAX_SIZE\s+((0x)?[0-9a-fA-F]+)")
31+
32+
class Assembly():
33+
def __init__(self, layout_path, output):
34+
self.output = output
35+
self.layout_path = layout_path
36+
self.find_slots()
37+
try:
38+
os.unlink(output)
39+
except OSError as e:
40+
if e.errno != errno.ENOENT:
41+
raise
42+
43+
def find_slots(self):
44+
offsets = {}
45+
sizes = {}
46+
47+
if os.path.isabs(self.layout_path):
48+
configFile = self.layout_path
49+
else:
50+
scriptsDir = os.path.dirname(os.path.abspath(__file__))
51+
configFile = os.path.join(scriptsDir, self.layout_path)
52+
53+
with open(configFile, 'r') as fd:
54+
for line in fd:
55+
m = offset_re.match(line)
56+
if m is not None:
57+
offsets[m.group(1)] = int(m.group(2), 0)
58+
m = size_re.match(line)
59+
if m is not None:
60+
sizes[m.group(1)] = int(m.group(2), 0)
61+
62+
if 'SECURE' not in offsets:
63+
raise Exception("Image config does not have secure partition")
64+
65+
if 'NON_SECURE' not in offsets:
66+
raise Exception("Image config does not have non-secure partition")
67+
68+
self.offsets = offsets
69+
self.sizes = sizes
70+
71+
def add_image(self, source, partition):
72+
with open(self.output, 'ab') as ofd:
73+
ofd.seek(0, os.SEEK_END)
74+
pos = ofd.tell()
75+
if pos > self.offsets[partition]:
76+
raise Exception("Partitions not in order, unsupported")
77+
if pos < self.offsets[partition]:
78+
ofd.write(b'\xFF' * (self.offsets[partition] - pos))
79+
statinfo = os.stat(source)
80+
if statinfo.st_size > self.sizes[partition]:
81+
raise Exception("Image {} is too large for partition".format(source))
82+
with open(source, 'rb') as rfd:
83+
shutil.copyfileobj(rfd, ofd, 0x10000)
84+
85+
def main():
86+
parser = argparse.ArgumentParser()
87+
88+
parser.add_argument('-l', '--layout', required=True,
89+
help='Location of the memory layout file')
90+
parser.add_argument('-s', '--secure', required=True,
91+
help='Unsigned secure image')
92+
parser.add_argument('-n', '--non_secure',
93+
help='Unsigned non-secure image')
94+
parser.add_argument('-o', '--output', required=True,
95+
help='Filename to write full image to')
96+
97+
args = parser.parse_args()
98+
output = Assembly(args.layout, args.output)
99+
100+
101+
output.add_image(args.secure, "SECURE")
102+
output.add_image(args.non_secure, "NON_SECURE")
103+
104+
if __name__ == '__main__':
105+
main()

0 commit comments

Comments
 (0)