Skip to content

Commit 579b9a2

Browse files
committed
Permit non-TrustZone ARMv8 build
Change the heuristic for selection of CMSE in the tools python, so that a non-TrustZone ARMv8 build can happen. Ideally we would have more direct flagging in the targets, but this refines the heuristic so the necessary behaviour can be easily achieved. * DOMAIN_NS=1 is based purely on the `-NS` suffix on the core name. * Enabling CMSE in the compiler and outputting a secure import library is now enabled when the core doesn't have an `-NS` suffix by either the target label `TFM` being present or the flag `trustzone` being set. This covers the existing ARMv8-M behaviour - TF-M builds have the TFM label, as per its documentation; M2351 secure builds have no explicit flagging, so we ensure that the M2351_NS target has the trustzone flag set, and the out-of-tree secure target inherits that.
1 parent d30d772 commit 579b9a2

File tree

7 files changed

+70
-43
lines changed

7 files changed

+70
-43
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": [],
@@ -8042,6 +8043,7 @@
80428043
"MBED_TZ_DEFAULT_ACCESS=1",
80438044
"LPTICKER_DELAY_TICKS=3"
80448045
],
8046+
"trustzone": true,
80458047
"is_disk_virtual": true,
80468048
"supported_toolchains": ["ARMC6"],
80478049
"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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,28 @@ 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 (self.trustzone 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+
382404
@property
383405
def is_PSA_secure_target(self):
384406
return 'SPE_Target' in self.labels

tools/toolchains/arm.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -521,25 +521,25 @@ def __init__(self, target, *args, **kwargs):
521521
if "--library_type=microlib" not in self.flags['asm']:
522522
self.flags['asm'].append("--library_type=microlib")
523523

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
524+
if target.is_TrustZone_secure_target:
525+
if kwargs.get('build_dir', False):
526+
# Output secure import library
529527
build_dir = kwargs['build_dir']
530528
secure_file = join(build_dir, "cmse_lib.o")
531529
self.flags["ld"] += ["--import_cmse_lib_out=%s" % secure_file]
532530

531+
# Enable compiler security extensions
532+
self.flags['cxx'].append("-mcmse")
533+
self.flags['c'].append("-mcmse")
534+
535+
if target.is_TrustZone_non_secure_target:
533536
# 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")
537+
# (DOMAIN_NS is passed to compiler and assembler via CORTEX_SYMBOLS
538+
# in mbedToolchain.get_symbols)
539+
define_string = self.make_ld_define("DOMAIN_NS", "0x1")
540+
self.flags["ld"].append(define_string)
542541

542+
core = target.core_without_NS
543543
cpu = {
544544
"Cortex-M0+": "cortex-m0plus",
545545
"Cortex-M4F": "cortex-m4",

tools/toolchains/gcc.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,23 @@ def __init__(self, target, notify=None, macros=None, build_profile=None,
5959
self.flags["common"].append("-DMBED_RTOS_SINGLE_THREAD")
6060
self.flags["ld"].append("--specs=nano.specs")
6161

62-
core = target.core
6362
self.cpu = []
64-
if CORE_ARCH[target.core] == 8:
63+
if target.is_TrustZone_secure_target:
64+
# Enable compiler security extensions
65+
self.cpu.append("-mcmse")
66+
# Output secure import library
67+
self.flags["ld"].extend([
68+
"-Wl,--cmse-implib",
69+
"-Wl,--out-implib=%s" % join(build_dir, "cmse_lib.o")
70+
])
71+
72+
if target.is_TrustZone_non_secure_target:
6573
# 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-
])
74+
# (DOMAIN_NS is passed to compiler and assembler via CORTEX_SYMBOLS
75+
# in mbedToolchain.get_symbols)
76+
self.flags["ld"].append("-DDOMAIN_NS=1")
7577

78+
core = target.core_without_NS
7679
cpu = {
7780
"Cortex-M0+": "cortex-m0plus",
7881
"Cortex-M4F": "cortex-m4",

tools/toolchains/iar.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,23 @@ def __init__(self, target, notify=None, macros=None, build_profile=None,
5454
build_dir=build_dir,
5555
build_profile=build_profile
5656
)
57-
core = target.core
58-
if CORE_ARCH[target.core] == 8:
57+
58+
if target.is_TrustZone_secure_target:
59+
# Enable compiler security extensions
60+
self.flags["asm"] += ["--cmse"]
61+
self.flags["common"] += ["--cmse"]
62+
# Output secure import library
63+
secure_file = join(build_dir, "cmse_lib.o")
64+
self.flags["ld"] += ["--import_cmse_lib_out=%s" % secure_file]
65+
66+
if target.is_TrustZone_non_secure_target:
5967
# 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]
68+
# (DOMAIN_NS is passed to compiler and assembler via CORTEX_SYMBOLS
69+
# in mbedToolchain.get_symbols)
70+
define_string = self.make_ld_define("DOMAIN_NS", "0x1")
71+
self.flags["ld"].append(define_string)
7072

73+
core = target.core_without_NS
7174
cpu = {
7275
"Cortex-M7F": "Cortex-M7.fp.sp",
7376
"Cortex-M7FD": "Cortex-M7.fp.dp",

0 commit comments

Comments
 (0)