Skip to content

Commit 9fb4429

Browse files
authored
Merge pull request #10520 from kjbracey-arm/build_tz_heuristic
Permit non-TrustZone ARMv8 build
2 parents d997563 + 65e0887 commit 9fb4429

File tree

7 files changed

+74
-46
lines changed

7 files changed

+74
-46
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ TF-M is built as bare-metal in a secure target, in order to build a secure targe
2626
## Build hooks
2727

2828
Mbed-OS testing tools are designed to work with a single image (`.bin` or `.hex`).
29-
When building mbed-os for ARMv8-M targets two images are created. One for normal world(NW) and one for TrustZone(TZ).
29+
When building mbed-os for TF-M targets two images are created. One for normal world(NW) and one for TrustZone(TZ).
3030
Mbed-OS build system provides `post_binary_hook` that allows executing arbitrary Python script for merging NW and TZ images. Typically `post_binary_hook` is added to NW target and assumes TZ target images as a prerequisite.
3131

32-
## Porting ARMv8-M targets
32+
## Porting TF-M targets
3333

34-
Typically firmware for ARMv8-M targets consist of 2 or more images: normal world and TrustZone image. More images can be present in case boot loaders are used.
34+
Typically firmware for TF-M targets consist of 2 or more images: normal world and TrustZone image. More images can be present in case boot loaders are used.
3535
Two images must be built and linked separately. TrustZone image must be built first.
3636

3737
There may be code and/or header files sharing between the two targets.

targets/targets.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"Target": {
33
"core": null,
4+
"trustzone": false,
45
"default_toolchain": "ARM",
56
"supported_toolchains": null,
67
"extra_labels": [],
@@ -8048,6 +8049,7 @@
80488049
"MBED_TZ_DEFAULT_ACCESS=1",
80498050
"LPTICKER_DELAY_TICKS=3"
80508051
],
8052+
"trustzone": true,
80518053
"is_disk_virtual": true,
80528054
"supported_toolchains": ["ARMC6"],
80538055
"config": {

tools/build_api.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,7 @@ def build_project(src_paths, build_path, target, toolchain_name,
601601
if into_dir:
602602
copy_when_different(res[0], into_dir)
603603
if not extra_artifacts:
604-
if (
605-
CORE_ARCH[toolchain.target.core] == 8 and
606-
not toolchain.target.core.endswith("NS")
607-
):
604+
if toolchain.target.is_TrustZone_secure_target:
608605
cmse_lib = join(dirname(res[0]), "cmse_lib.o")
609606
copy_when_different(cmse_lib, into_dir)
610607
else:

tools/targets/__init__.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,32 @@ def labels(self):
379379
labels = (names + CORE_LABELS[self.core] + self.extra_labels)
380380
return labels
381381

382+
@property
383+
def core_without_NS(self):
384+
if self.core.endswith('-NS'):
385+
return self.core[:-3]
386+
else:
387+
return self.core
388+
389+
# Mechanism for specifying TrustZone is subject to change - see
390+
# discussion on https://github.com/ARMmbed/mbed-os/issues/9460
391+
# In the interim, we follow heuristics that support existing
392+
# documentation for ARMv8-M TF-M integration (check the "TFM" label),
393+
# plus an extra "trustzone" flag set by M2351, and looking at the "-NS"
394+
# suffix. This now permits non-TrustZone ARMv8 builds if
395+
# having trustzone = false (default), no TFM flag, and no -NS suffix.
396+
@property
397+
def is_TrustZone_secure_target(self):
398+
return (getattr(self, 'trustzone', False) or 'TFM' in self.labels) and not self.core.endswith('-NS')
399+
400+
@property
401+
def is_TrustZone_non_secure_target(self):
402+
return self.core.endswith('-NS')
403+
404+
@property
405+
def is_TrustZone_target(self):
406+
return self.is_TrustZone_secure_target or self.is_TrustZone_non_secure_target
407+
382408
@property
383409
def is_PSA_secure_target(self):
384410
return 'SPE_Target' in self.labels

tools/toolchains/arm.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from shutil import rmtree
2626
from distutils.version import LooseVersion
2727

28-
from tools.targets import CORE_ARCH
2928
from tools.toolchains.mbed_toolchain import mbedToolchain, TOOLCHAIN_PATHS
3029
from tools.utils import mkdir, NotSupportedException, run_cmd
3130
from tools.resources import FileRef
@@ -521,25 +520,25 @@ def __init__(self, target, *args, **kwargs):
521520
if "--library_type=microlib" not in self.flags['asm']:
522521
self.flags['asm'].append("--library_type=microlib")
523522

524-
core = target.core
525-
if CORE_ARCH[target.core] == 8:
526-
if ((not target.core.endswith("-NS")) and
527-
kwargs.get('build_dir', False)):
528-
# Create Secure library
523+
if target.is_TrustZone_secure_target:
524+
if kwargs.get('build_dir', False):
525+
# Output secure import library
529526
build_dir = kwargs['build_dir']
530527
secure_file = join(build_dir, "cmse_lib.o")
531528
self.flags["ld"] += ["--import_cmse_lib_out=%s" % secure_file]
532529

530+
# Enable compiler security extensions
531+
self.flags['cxx'].append("-mcmse")
532+
self.flags['c'].append("-mcmse")
533+
534+
if target.is_TrustZone_non_secure_target:
533535
# Add linking time preprocessor macro DOMAIN_NS
534-
if target.core.endswith("-NS"):
535-
define_string = self.make_ld_define("DOMAIN_NS", "0x1")
536-
self.flags["ld"].append(define_string)
537-
core = target.core[:-3]
538-
else:
539-
# Add secure build flag
540-
self.flags['cxx'].append("-mcmse")
541-
self.flags['c'].append("-mcmse")
536+
# (DOMAIN_NS is passed to compiler and assembler via CORTEX_SYMBOLS
537+
# in mbedToolchain.get_symbols)
538+
define_string = self.make_ld_define("DOMAIN_NS", "0x1")
539+
self.flags["ld"].append(define_string)
542540

541+
core = target.core_without_NS
543542
cpu = {
544543
"Cortex-M0+": "cortex-m0plus",
545544
"Cortex-M4F": "cortex-m4",

tools/toolchains/gcc.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from distutils.spawn import find_executable
2121
from distutils.version import LooseVersion
2222

23-
from tools.targets import CORE_ARCH
2423
from tools.toolchains.mbed_toolchain import mbedToolchain, TOOLCHAIN_PATHS
2524
from tools.utils import run_cmd
2625

@@ -59,20 +58,23 @@ def __init__(self, target, notify=None, macros=None, build_profile=None,
5958
self.flags["common"].append("-DMBED_RTOS_SINGLE_THREAD")
6059
self.flags["ld"].append("--specs=nano.specs")
6160

62-
core = target.core
6361
self.cpu = []
64-
if CORE_ARCH[target.core] == 8:
62+
if target.is_TrustZone_secure_target:
63+
# Enable compiler security extensions
64+
self.cpu.append("-mcmse")
65+
# Output secure import library
66+
self.flags["ld"].extend([
67+
"-Wl,--cmse-implib",
68+
"-Wl,--out-implib=%s" % join(build_dir, "cmse_lib.o")
69+
])
70+
71+
if target.is_TrustZone_non_secure_target:
6572
# Add linking time preprocessor macro DOMAIN_NS
66-
if target.core.endswith("-NS"):
67-
self.flags["ld"].append("-DDOMAIN_NS=1")
68-
core = target.core[:-3]
69-
else:
70-
self.cpu.append("-mcmse")
71-
self.flags["ld"].extend([
72-
"-Wl,--cmse-implib",
73-
"-Wl,--out-implib=%s" % join(build_dir, "cmse_lib.o")
74-
])
73+
# (DOMAIN_NS is passed to compiler and assembler via CORTEX_SYMBOLS
74+
# in mbedToolchain.get_symbols)
75+
self.flags["ld"].append("-DDOMAIN_NS=1")
7576

77+
core = target.core_without_NS
7678
cpu = {
7779
"Cortex-M0+": "cortex-m0plus",
7880
"Cortex-M4F": "cortex-m4",

tools/toolchains/iar.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from os.path import join, splitext, exists
2020
from distutils.version import LooseVersion
2121

22-
from tools.targets import CORE_ARCH
2322
from tools.toolchains.mbed_toolchain import mbedToolchain, TOOLCHAIN_PATHS
2423
from tools.utils import run_cmd
2524

@@ -54,20 +53,23 @@ def __init__(self, target, notify=None, macros=None, build_profile=None,
5453
build_dir=build_dir,
5554
build_profile=build_profile
5655
)
57-
core = target.core
58-
if CORE_ARCH[target.core] == 8:
56+
57+
if target.is_TrustZone_secure_target:
58+
# Enable compiler security extensions
59+
self.flags["asm"] += ["--cmse"]
60+
self.flags["common"] += ["--cmse"]
61+
# Output secure import library
62+
secure_file = join(build_dir, "cmse_lib.o")
63+
self.flags["ld"] += ["--import_cmse_lib_out=%s" % secure_file]
64+
65+
if target.is_TrustZone_non_secure_target:
5966
# Add linking time preprocessor macro DOMAIN_NS
60-
if target.core.endswith("-NS"):
61-
define_string = self.make_ld_define("DOMAIN_NS", "0x1")
62-
self.flags["ld"].append(define_string)
63-
core = target.core[:-3]
64-
else:
65-
# Create Secure library
66-
self.flags["asm"] += ["--cmse"]
67-
self.flags["common"] += ["--cmse"]
68-
secure_file = join(build_dir, "cmse_lib.o")
69-
self.flags["ld"] += ["--import_cmse_lib_out=%s" % secure_file]
67+
# (DOMAIN_NS is passed to compiler and assembler via CORTEX_SYMBOLS
68+
# in mbedToolchain.get_symbols)
69+
define_string = self.make_ld_define("DOMAIN_NS", "0x1")
70+
self.flags["ld"].append(define_string)
7071

72+
core = target.core_without_NS
7173
cpu = {
7274
"Cortex-M7F": "Cortex-M7.fp.sp",
7375
"Cortex-M7FD": "Cortex-M7.fp.dp",

0 commit comments

Comments
 (0)