Skip to content

Commit ffbd92c

Browse files
authored
Merge pull request #11288 from OpenNuvoton/nuvoton_m2351_comb-sec-nonsec
M2351: Enhance secure/non-secure image build flow for non-PSA target
2 parents 50b7529 + 0ed126b commit ffbd92c

File tree

8 files changed

+1907
-1812
lines changed

8 files changed

+1907
-1812
lines changed

targets/TARGET_NUVOTON/TARGET_M2351/TARGET_NUMAKER_PFM_M2351/TARGET_M23_NS/LICENSE renamed to targets/TARGET_NUVOTON/TARGET_M2351/TARGET_M23_NS/TARGET_NU_PFM_M2351_NPSA_NS/TARGET_NU_PREBUILD_SECURE/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
4646
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
4747
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
4848
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49-
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

targets/TARGET_NUVOTON/TARGET_M2351/TARGET_M23_NS/TARGET_NU_PFM_M2351_NPSA_NS/TARGET_NU_PREBUILD_SECURE/NuMaker-mbed-TZ-secure-example.hex

Lines changed: 1808 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
# Default secure code for M2351
2-
3-
The M2351 is a TrustZone target that requires two codes: secure and nonsecure. Secure code runs first to set up the secure environment and then brings up the nonsecure code. `NuMaker-mbed-TZ-secure-example.hex` is the default secure code, and `cmse_lib.o` is its accompanying
4-
secure gateway library, which exports secure functions to nonsecure code. It is built from the [secure code template](https://github.com/OpenNuvoton/NuMaker-mbed-TZ-secure-example). Because secure and nonsecure code may start at an address other than `0x0`, we have chosen `.hex` rather than `.bin` as the output format.
1+
The M2351 is a TrustZone target that requires two codes: secure and nonsecure. Secure code runs first to set up the secure environment and then brings up the nonsecure code. `NuMaker-mbed-TZ-secure-example.hex` is the pre-built secure image, and `cmse_lib.o` is its accompanying
2+
secure gateway library, which exports secure functions to nonsecure code. It is built from the [non-PSA secure code template](https://github.com/OpenNuvoton/NuMaker-mbed-TZ-secure-example). Because secure and nonsecure code may start at an address other than `0x0`, we have chosen `.hex` rather than `.bin` as the output format.
53

64
## Hardware partition
75

8-
On TrustZone targets, you need to partition the hardware first for secure code to run on secure worlds and nonsecure code to run on nonsecure worlds.
6+
On TrustZone targets, you need to partition the hardware first for secure code to run on secure world and nonsecure code to run on nonsecure world.
97

10-
The default secure code has the following hardware partition:
8+
The pre-built secure code has the following hardware partition:
119

1210
- Flash (512KiB in total): 256KiB for secure and 256KiB for nonsecure.
1311
- SRAM (96KiB in total): 32KiB for secure and 64KiB for nonsecure.
@@ -21,10 +19,13 @@ The default secure code has the following hardware partition:
2119
- **PDMA0** hardwired to secure. Implements secure asynchronous transfer.
2220
- **PDMA1** configured to nonsecure. Implements nonsecure asynchronous transfer.
2321

24-
## Flash secure and nonsecure code
22+
## Pre-built secure code files
2523

26-
To flash secure and nonsecure code on the M2351, drag-and-drop `NuMaker-mbed-TZ-secure-example.hex` first, and then build the user program, for example, [mbed-os-example-blinky.hex](https://github.com/ARMmbed/mbed-os-example-blinky).
24+
- NuMaker-mbed-TZ-secure-example.hex
25+
Pre-built secure image generated in non-PSA secure target build and to combine with non-secure image in non-PSA non-secure target post-build
26+
- cmse_lib.o
27+
Pre-built secure gateway library generated in non-PSA secure target build and to link with non-secure code in non-PSA non-secure target build
2728

2829
## Reference
2930

30-
Please refer to the [secure code template](https://github.com/OpenNuvoton/NuMaker-mbed-TZ-secure-example) for details.
31+
Please refer to the [non-PSA secure code template](https://github.com/OpenNuvoton/NuMaker-mbed-TZ-secure-example) for details.

targets/TARGET_NUVOTON/TARGET_M2351/TARGET_NUMAKER_PFM_M2351/TARGET_M23_NS/NuMaker-mbed-TZ-secure-example.hex

Lines changed: 0 additions & 1793 deletions
This file was deleted.

targets/targets.json

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8669,8 +8669,7 @@
86698669
"macros_add": ["CMSDK_CM7"],
86708670
"device_has_add": ["MPU"]
86718671
},
8672-
"NUMAKER_PFM_M2351": {
8673-
"core": "Cortex-M23-NS",
8672+
"NU_PFM_M2351_CM": {
86748673
"default_toolchain": "ARMC6",
86758674
"extra_labels": [
86768675
"NUVOTON",
@@ -8684,7 +8683,6 @@
86848683
"MBED_TZ_DEFAULT_ACCESS=1",
86858684
"LPTICKER_DELAY_TICKS=4"
86868685
],
8687-
"trustzone": true,
86888686
"is_disk_virtual": true,
86898687
"supported_toolchains": ["ARMC6"],
86908688
"config": {
@@ -8707,6 +8705,7 @@
87078705
},
87088706
"overrides": {
87098707
"deep-sleep-latency": 1,
8708+
"tickless-from-us-ticker": true,
87108709
"mpu-rom-end": "0x1fffffff"
87118710
},
87128711
"inherits": ["Target"],
@@ -8736,17 +8735,48 @@
87368735
"FLASH",
87378736
"MPU"
87388737
],
8739-
"components_add": ["FLASHIAP"],
87408738
"detect_code": ["1305"],
87418739
"release_versions": ["5"],
87428740
"device_name": "M2351KIAAEES",
87438741
"bootloader_supported": true,
87448742
"tickless-from-us-ticker": true,
87458743
"forced_reset_timeout": 3,
8746-
"mbed_rom_start" : "0x10040000",
8747-
"mbed_rom_size" : "0x40000",
8748-
"mbed_ram_start" : "0x30008000",
8749-
"mbed_ram_size" : "0x10000"
8744+
"public": false
8745+
},
8746+
"NU_PFM_M2351_NPSA_NS": {
8747+
"inherits": ["NU_PFM_M2351_CM"],
8748+
"core": "Cortex-M23-NS",
8749+
"trustzone": true,
8750+
"extra_labels_add": [
8751+
"M23_NS",
8752+
"NU_PREBUILD_SECURE"
8753+
],
8754+
"macros_add": [
8755+
"MBED_TZ_DEFAULT_ACCESS=1"
8756+
],
8757+
"components_add": ["FLASHIAP"],
8758+
"post_binary_hook": {"function": "M2351Code.merge_secure"},
8759+
"secure_image_filename": "NuMaker-mbed-TZ-secure-example.hex",
8760+
"mbed_rom_start" : "0x10040000",
8761+
"mbed_rom_size" : "0x40000",
8762+
"mbed_ram_start" : "0x30008000",
8763+
"mbed_ram_size" : "0x10000"
8764+
},
8765+
"NU_PFM_M2351_NPSA_S": {
8766+
"inherits": ["NU_PFM_M2351_CM"],
8767+
"core": "Cortex-M23",
8768+
"trustzone": true,
8769+
"extra_labels_add": [
8770+
"M23_S"
8771+
],
8772+
"device_has_remove": ["TRNG"],
8773+
"components_add": ["FLASHIAP"],
8774+
"deliver_to_target": "NU_PFM_M2351_NPSA_NS",
8775+
"delivery_dir": "TARGET_NUVOTON/TARGET_M2351/TARGET_M23_NS/TARGET_NU_PFM_M2351_NPSA_NS/TARGET_NU_PREBUILD_SECURE",
8776+
"mbed_rom_start" : "0x0",
8777+
"mbed_rom_size" : "0x40000",
8778+
"mbed_ram_start" : "0x20000000",
8779+
"mbed_ram_size" : "0x8000"
87508780
},
87518781
"TMPM3H6": {
87528782
"inherits": ["Target"],

tools/export/uvision/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ class Uvision(Exporter):
139139
"MTSCode.combine_bins_mts_dot",
140140
"MTSCode.combine_bins_mts_dragonfly",
141141
"NCS36510TargetCode.ncs36510_addfib",
142-
"LPC55S69Code.binary_hook"
142+
"LPC55S69Code.binary_hook",
143+
"M2351Code.merge_secure"
143144
])
144145

145146
# File associations within .uvprojx file

tools/targets/__init__.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,54 @@ def binary_hook(t_self, resources, elf, binf):
725725
)
726726
lpc55s69_complete(t_self, binf, secure_bin)
727727

728+
class M2351Code:
729+
"""M2351 Hooks"""
730+
@staticmethod
731+
def merge_secure(t_self, resources, ns_elf, ns_hex):
732+
t_self.notify.info("Merging non-secure image with secure image")
733+
configured_secure_image_filename = t_self.target.secure_image_filename
734+
t_self.notify.info("Non-secure elf image %s" % ns_elf)
735+
t_self.notify.info("Non-secure hex image %s" % ns_hex)
736+
t_self.notify.info("Finding secure image %s" % configured_secure_image_filename)
737+
s_hex = find_secure_image(
738+
t_self.notify,
739+
resources,
740+
ns_hex,
741+
configured_secure_image_filename,
742+
FileType.HEX
743+
)
744+
t_self.notify.info("Found secure image %s" % s_hex)
745+
746+
_, ext = os.path.splitext(s_hex)
747+
if ext != ".hex":
748+
t_self.notify.debug("Secure image %s must be in Intel HEX format" % s_hex)
749+
return
750+
if not os.path.isfile(s_hex):
751+
t_self.notify.debug("Secure image %s must be regular file" % s_hex)
752+
return
753+
754+
ns_main, ext = os.path.splitext(ns_hex)
755+
if ext != ".hex":
756+
t_self.notify.debug("Non-secure image %s must be in Intel HEX format" % s_hex)
757+
return
758+
if not os.path.isfile(ns_hex):
759+
t_self.notify.debug("Non-secure image %s must be regular file" % s_hex)
760+
return
761+
762+
# Keep original non-secure before merge with secure
763+
ns_nosecure_hex = ns_main + "_no-secure-merge" + ext
764+
t_self.notify.info("Keep no-secure-merge image %s" % ns_nosecure_hex)
765+
shutil.copy2(ns_hex, ns_nosecure_hex)
766+
767+
# Merge secure and non-secure and save to non-secure (override it)
768+
from intelhex import IntelHex
769+
s_ih = IntelHex()
770+
s_ih.loadhex(s_hex)
771+
ns_ih = IntelHex()
772+
ns_ih.loadhex(ns_hex)
773+
ns_ih.start_addr = None
774+
s_ih.merge(ns_ih)
775+
s_ih.tofile(ns_hex, 'hex')
728776

729777
# End Target specific section
730778
###############################################################################

0 commit comments

Comments
 (0)