Skip to content

Commit 88de6a1

Browse files
authored
M2354: Fix compile error with TF-M integration (#325)
* M2354: Fix compile error with TF-M In CMake, mbed-psa moves out of mbed-core, its library type changing to STATIC from INTERFACE. Following this modification, the platform TF-M code relying on mbed-psa needs to explicitly specify the dependency through target_link_libraries(). * M2354: Enable OUTPUT_EXT set to either bin or hex Update post-build script to enable OUTPUT_EXT can be set to "bin" or "hex" in targets.json5. * NUVOTON: Locate correct python3 command path across platforms shutil.which("python3") can locate incorrect path when there are multiple python3 installations. Instead, follow the link below, use sys.executable. https://docs.python.org/3/library/sys.html#sys.executable
1 parent c380299 commit 88de6a1

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

targets/TARGET_NUVOTON/TARGET_M2354/TARGET_TFM/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ target_sources(mbed-m2354-tfm
1818
platform_extra_secure_compat.c
1919
tfm_ns_interface.c
2020
)
21+
22+
target_link_libraries(mbed-m2354-tfm
23+
INTERFACE
24+
mbed-psa
25+
)

targets/TARGET_NUVOTON/scripts/NUVOTON.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@
2424
import argparse
2525
from intelhex import IntelHex
2626
from datetime import datetime
27+
import sys
2728

2829
SCRIPT_DIR = dirname(abspath(__file__))
2930
MBED_OS_ROOT = abspath(path_join(SCRIPT_DIR, os.pardir, os.pardir, os.pardir))
3031

31-
def tfm_sign_image(tfm_import_path, signing_key, signing_key_1, non_secure_bin):
32+
def tfm_sign_image(tfm_import_path, signing_key, signing_key_1, non_secure_binhex):
3233
SECURE_ROOT = abspath(tfm_import_path)
3334

3435
secure_bin = path_join(SECURE_ROOT, 'tfm_s.bin')
3536
assert os.path.isfile(secure_bin)
3637

37-
non_secure_bin = abspath(non_secure_bin)
38-
assert os.path.isfile(non_secure_bin)
38+
non_secure_binhex = abspath(non_secure_binhex)
39+
assert os.path.isfile(non_secure_binhex)
3940

40-
build_dir = dirname(non_secure_bin)
41+
build_dir = dirname(non_secure_binhex)
4142
tempdir = path_join(build_dir, 'temp')
4243
if not isdir(tempdir):
4344
os.makedirs(tempdir)
@@ -46,13 +47,20 @@ def tfm_sign_image(tfm_import_path, signing_key, signing_key_1, non_secure_bin):
4647

4748
bl2_bin = path_join(SECURE_ROOT, 'bl2.bin')
4849
s_bin_basename = splitext(basename(secure_bin))[0]
49-
ns_bin_basename = splitext(basename(non_secure_bin))[0]
50+
ns_bin_basename = splitext(basename(non_secure_binhex))[0]
5051

5152
signing_key = path_join(SECURE_ROOT, 'signing_key', signing_key)
5253
assert os.path.isfile(signing_key)
5354

55+
# Create non_secure_bin for signing if non_secure_binhex is hex
56+
non_secure_bin = splitext(non_secure_binhex)[0] + ".bin"
57+
if os.path.splitext(non_secure_binhex)[1].lower() == ".hex":
58+
non_secure_ih = IntelHex()
59+
non_secure_ih.loadhex(non_secure_binhex)
60+
non_secure_ih.tobinfile(non_secure_bin)
61+
5462
# Find Python 3 command name across platforms
55-
python3_cmd = "python3" if shutil.which("python3") is not None else "python"
63+
python3_cmd = sys.executable
5664

5765
# Specify image version
5866
#
@@ -162,7 +170,7 @@ def tfm_sign_image(tfm_import_path, signing_key, signing_key_1, non_secure_bin):
162170
signed_concat_bin = abspath(path_join(tempdir, 'tfm_s_signed_' + ns_bin_basename + '_signed_concat' + '.bin'))
163171
s_update_bin = abspath(path_join(build_dir, s_bin_basename + '_update' + '.bin'))
164172
ns_update_bin = abspath(path_join(build_dir, ns_bin_basename + '_update' + '.bin'))
165-
173+
166174
#1. Run wrapper to sign the secure TF-M binary
167175
cmd_wrapper[pos_wrapper_signing_key] = signing_key
168176
cmd_wrapper[pos_wrapper_layout] = image_macros_s
@@ -206,8 +214,8 @@ def tfm_sign_image(tfm_import_path, signing_key, signing_key_1, non_secure_bin):
206214
out_ih = IntelHex()
207215
out_ih.loadbin(bl2_bin)
208216
out_ih.loadbin(signed_concat_bin, flash_area_0_offset)
209-
out_ih.tofile(splitext(non_secure_bin)[0] + ".hex", 'hex')
210-
out_ih.tobinfile(non_secure_bin)
217+
out_ih.tofile(splitext(non_secure_binhex)[0] + ".bin", 'bin')
218+
out_ih.tofile(splitext(non_secure_binhex)[0] + ".hex", 'hex')
211219

212220
# Generate firmware update file for PSA Firmware Update
213221
shutil.copy(s_signed_bin, s_update_bin)
@@ -250,8 +258,8 @@ def tfm_sign_image(tfm_import_path, signing_key, signing_key_1, non_secure_bin):
250258
out_ih = IntelHex()
251259
out_ih.loadbin(bl2_bin)
252260
out_ih.loadbin(concat_signed_bin, flash_area_0_offset)
253-
out_ih.tofile(splitext(non_secure_bin)[0] + ".hex", 'hex')
254-
out_ih.tobinfile(non_secure_bin)
261+
out_ih.tofile(splitext(non_secure_binhex)[0] + ".bin", 'bin')
262+
out_ih.tofile(splitext(non_secure_binhex)[0] + ".hex", 'hex')
255263

256264
# Generate firmware update file for PSA Firmware Update
257265
shutil.copy(concat_signed_bin, update_bin)
@@ -357,7 +365,7 @@ def parse_args():
357365
)
358366

359367
parser_tfm_sign_image.add_argument(
360-
"--non-secure-bin",
368+
"--non-secure-binhex",
361369
help="Path to the non-secure binary",
362370
required=True
363371
)
@@ -368,4 +376,4 @@ def parse_args():
368376

369377
if __name__ == "__main__":
370378
args = parse_args()
371-
args.func(args.tfm_import_path, args.signing_key, args.signing_key_1, args.non_secure_bin)
379+
args.func(args.tfm_import_path, args.signing_key, args.signing_key_1, args.non_secure_binhex)

targets/TARGET_NUVOTON/scripts/mbed_set_post_build_nuvoton.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro(mbed_post_build_nuvoton_tfm_sign_image
3030
--tfm-import-path ${tfm_import_path}
3131
--signing_key ${signing_key}
3232
--signing_key_1 ${signing_key_1}
33-
--non-secure-bin $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.bin
33+
--non-secure-binhex $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.${MBED_OUTPUT_EXT}
3434
)
3535
else()
3636
add_custom_command(
@@ -43,7 +43,7 @@ macro(mbed_post_build_nuvoton_tfm_sign_image
4343
tfm_sign_image
4444
--tfm-import-path ${tfm_import_path}
4545
--signing_key ${signing_key}
46-
--non-secure-bin $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.bin
46+
--non-secure-binhex $<TARGET_FILE_DIR:${target}>/$<TARGET_FILE_BASE_NAME:${target}>.${MBED_OUTPUT_EXT}
4747
)
4848
endif()
4949
endfunction()

0 commit comments

Comments
 (0)