Skip to content

Additions to TF-M source integration #9772

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 4 commits into from
Feb 21, 2019
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
21 changes: 2 additions & 19 deletions components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/mbed_lib.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
{
"name": "tfm",
"config": {
"handle_pool_size": {
"help": "maximum number of handles that can be opened at the same time",
"macro_name": "TFM_CONN_HANDLE_MAX_NUM",
"value": 10
},
"rot_pool_size": {
"help": "maximum number of RoT services allowed",
"macro_name": "TFM_SPM_MAX_ROT_SERV_NUM",
"value": 20
},
"message_pool_size": {
"help": "maximum number of RoT services allowed",
"macro_name": "TFM_MSG_QUEUE_MAX_MSG_NUM",
"value": 10
}
}
"name": "tfm-s",
"macros": ["MBED_FAULT_HANDLER_DISABLED", "BYPASS_NVSTORE_CHECK=1"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it appropriate to disable the fault handler in a library? I looked for a justification for this change in the commit message but I didn't find one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when building TFM we are not using parts of mbed-os
Some of those parts are the fault handlers, and this is because TFM implements their own

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good enough for me!

}

Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ REGION_DECLARE_EXT(Image$$, ARM_LIB_HEAP, $$ZI$$Base);
REGION_DECLARE_EXT(Image$$, ARM_LIB_HEAP, $$ZI$$Limit);
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$Base);
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$Limit);
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$ZI$$Base);
REGION_DECLARE_EXT(Image$$, ER_TFM_DATA, $$ZI$$Limit);
REGION_DECLARE_EXT(Image$$, TFM_SECURE_STACK, $$ZI$$Base);
REGION_DECLARE_EXT(Image$$, TFM_SECURE_STACK, $$ZI$$Limit);
REGION_DECLARE_EXT(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base);
Expand Down Expand Up @@ -534,6 +536,12 @@ int32_t tfm_memory_check(void *buffer, size_t len, int32_t ns_caller)
return IPC_SUCCESS;
}

base = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$ZI$$Base);
limit = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$ZI$$Limit);
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
return IPC_SUCCESS;
}

base = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$Base);
limit = (uintptr_t)&REGION_NAME(Image$$, ER_TFM_DATA, $$Limit);
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
Expand All @@ -552,6 +560,12 @@ int32_t tfm_memory_check(void *buffer, size_t len, int32_t ns_caller)
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
return IPC_SUCCESS;
}

base = (uintptr_t)S_CODE_START;
limit = (uintptr_t)(S_CODE_START + S_CODE_SIZE);
if (memory_check_range(buffer, len, base, limit) == IPC_SUCCESS) {
return IPC_SUCCESS;
}
}

return IPC_ERROR_MEMORY_CHECK;
Expand Down
29 changes: 29 additions & 0 deletions components/TARGET_PSA/TARGET_TFM/mbed_lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "tfm",
"macros": [
"TFM_PSA_API", "MBED_TZ_DEFAULT_ACCESS=1"
],
"config": {
"level": {
"help": "TFM security level",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this suppose to mean?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one TFM properties
security level is corresponding with PSA memory separation level desired

This is an internal TFM config, and a prep for the future when we will have a greater level of memory separation.

"macro_name": "TFM_LVL",
"value": 1
},
"handle_pool_size": {
"help": "maximum number of handles that can be opened at the same time",
"macro_name": "TFM_CONN_HANDLE_MAX_NUM",
"value": 10
},
"rot_pool_size": {
"help": "maximum number of RoT services allowed",
"macro_name": "TFM_SPM_MAX_ROT_SERV_NUM",
"value": 20
},
"message_pool_size": {
"help": "maximum number of active messages allowed",
"macro_name": "TFM_MSG_QUEUE_MAX_MSG_NUM",
"value": 10
}
}
}

12 changes: 2 additions & 10 deletions components/TARGET_PSA/TARGET_TFM/tf-m-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ The target should be represented in a following way in `target.json` (`MUSCA_A1`
"core": "Cortex-M33-NS",
"device_has_add": ["INTERRUPTIN", "LPTICKER", "SERIAL", "SLEEP", "USTICKER"],
"macros": [
"MBED_TZ_DEFAULT_ACCESS=1",
"MBED_FAULT_HANDLER_DISABLED",
"TFM_PSA_API",
"MBEDTLS_PSA_CRYPTO_C"
],
"extra_labels_add": ["MUSCA_A1_NS", "PSA", "TFM"],
Expand All @@ -77,16 +75,10 @@ The target should be represented in a following way in `target.json` (`MUSCA_A1`
"core": "Cortex-M33",
"device_has_add": ["FLASH"],
"macros": [
"MBED_FAULT_HANDLER_DISABLED",
"MBED_MPU_CUSTOM",
"BYPASS_NVSTORE_CHECK",
"TFM_LVL=1",
"TFM_PSA_API",
"MBEDTLS_PSA_CRYPTO_SPM",
"MBEDTLS_PSA_CRYPTO_C",
"MBEDTLS_ENTROPY_NV_SEED",
"MBEDTLS_PLATFORM_NV_SEED_READ_MACRO=mbed_default_seed_read",
"MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO=mbed_default_seed_write"
"MBEDTLS_ENTROPY_NV_SEED"
],
"components_add": ["FLASHIAP"],
"extra_labels_add": ["MUSCA_A1_S", "PSA", "TFM"]
Expand Down Expand Up @@ -116,4 +108,4 @@ Mbed-OS contains customized TF-M version. TF-M services reference implementation

The porting layer consists of:
- All functions listed in: `components/TARGET_PSA/TARGET_TFM/COMPONENT_SPE/platform/include/tfm_spm_hal.h`
- Flash API `mbed-os/hal/flash_api.h` implementation is required for TZ image. It is used by PSA Internal trusted storage implementation.
- Flash API `mbed-os/hal/flash_api.h` implementation is required for TZ image. It is used by PSA Internal trusted storage implementation.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ fuzzywuzzy>=0.11,<=0.17
pyelftools>=0.24,<=0.25
git+https://github.com/armmbed/[email protected]
icetea>=1.2.1,<1.3
pycryptodome>=3.7.2,<=3.7.3
17 changes: 9 additions & 8 deletions tools/importer/tfm_importer.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@
}
],
"commit_sha" : [
"11e5abc451acc7e7596e01b0f5605b4ad3e1965e",
"9541a37d7c878d057a40734ab4174cb46d81a922",
"e87efab83af6273a12b471ab574ddbf4359ff0de",
"bc275ff42a5c6275efffce81c91cce37e3749a3f",
"fb6b17dcdd59faa023e7940a6bb2f052956044c0",
"38bd4a279d22ff083d37c7f6a034d4a589e3527e",
"8c33f1e25ada6e3cbc15bd982825473ba9a57540",
"1134fd4dbb4245d19c010861a9f0bec7210c9701"
"1f30b52488e88704cdb51c8c26e7225e580e5cc2",
"5f2e4b3911ca3a776fab55a6d12054904614cced",
"0c23e8698958b6e716114267fbdf6d82a16b6e0c",
"fc7864055982f3b8e7e556f9dd8d0c94a1c772bc",
"011fa28110ccfd87554f9a1f06c2eb8a8a34d1e0",
"da01e3411fcf8010f4ae581946cf4b0e7753e024",
"008bf1b2d176c04fb33985e0677e90e3800cb4ca",
"67a4e6d723dfb199fdbe9a2cef57dbe58cbc026f",
"6354b9a47351a29ea24023c5471b63d39e091959"
]
}

Empty file added tools/psa/tfm/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions tools/psa/tfm/bin_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) 2017-2018 ARM Limited
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed 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.

from .assemble import Assembly

__all__ = [
'Assembly'
]
105 changes: 105 additions & 0 deletions tools/psa/tfm/bin_utils/assemble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#! /usr/bin/env python3
#
# Copyright 2017 Linaro Limited
# Copyright (c) 2017-2018, Arm Limited.
#
# Licensed 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.

"""
Assemble multiple images into a single image that can be flashed on the device.
"""

import argparse
import errno
import io
import re
import os
import shutil

offset_re = re.compile(r"^#define ([0-9A-Z_]+)_IMAGE_OFFSET\s+((0x)?[0-9a-fA-F]+)")
size_re = re.compile(r"^#define ([0-9A-Z_]+)_IMAGE_MAX_SIZE\s+((0x)?[0-9a-fA-F]+)")

class Assembly():
def __init__(self, layout_path, output):
self.output = output
self.layout_path = layout_path
self.find_slots()
try:
os.unlink(output)
except OSError as e:
if e.errno != errno.ENOENT:
raise

def find_slots(self):
offsets = {}
sizes = {}

if os.path.isabs(self.layout_path):
configFile = self.layout_path
else:
scriptsDir = os.path.dirname(os.path.abspath(__file__))
configFile = os.path.join(scriptsDir, self.layout_path)

with open(configFile, 'r') as fd:
for line in fd:
m = offset_re.match(line)
if m is not None:
offsets[m.group(1)] = int(m.group(2), 0)
m = size_re.match(line)
if m is not None:
sizes[m.group(1)] = int(m.group(2), 0)

if 'SECURE' not in offsets:
raise Exception("Image config does not have secure partition")

if 'NON_SECURE' not in offsets:
raise Exception("Image config does not have non-secure partition")

self.offsets = offsets
self.sizes = sizes

def add_image(self, source, partition):
with open(self.output, 'ab') as ofd:
ofd.seek(0, os.SEEK_END)
pos = ofd.tell()
if pos > self.offsets[partition]:
raise Exception("Partitions not in order, unsupported")
if pos < self.offsets[partition]:
ofd.write(b'\xFF' * (self.offsets[partition] - pos))
statinfo = os.stat(source)
if statinfo.st_size > self.sizes[partition]:
raise Exception("Image {} is too large for partition".format(source))
with open(source, 'rb') as rfd:
shutil.copyfileobj(rfd, ofd, 0x10000)

def main():
parser = argparse.ArgumentParser()

parser.add_argument('-l', '--layout', required=True,
help='Location of the memory layout file')
parser.add_argument('-s', '--secure', required=True,
help='Unsigned secure image')
parser.add_argument('-n', '--non_secure',
help='Unsigned non-secure image')
parser.add_argument('-o', '--output', required=True,
help='Filename to write full image to')

args = parser.parse_args()
output = Assembly(args.layout, args.output)


output.add_image(args.secure, "SECURE")
output.add_image(args.non_secure, "NON_SECURE")

if __name__ == '__main__':
main()
Loading