Skip to content

Commit 2c3fb3d

Browse files
committed
M2354: Reduce output image size
1. Enable mcuboot no padding option 2. Support output image format Intel Hex
1 parent 356c8fd commit 2c3fb3d

File tree

2 files changed

+60
-27
lines changed

2 files changed

+60
-27
lines changed

targets/TARGET_NUVOTON/scripts/NUVOTON.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import subprocess
2323
import shutil
2424
import argparse
25+
from intelhex import IntelHex
2526

2627
SCRIPT_DIR = dirname(abspath(__file__))
2728
MBED_OS_ROOT = abspath(path_join(SCRIPT_DIR, os.pardir, os.pardir, os.pardir))
@@ -45,9 +46,10 @@ def tfm_sign_image_tgt(tfm_import_path, signing_key, non_secure_bin):
4546

4647
bl2_bin = path_join(SECURE_ROOT, 'bl2.bin')
4748
image_macros_s_ns = path_join(SECURE_ROOT, 'partition', 'signing_layout_preprocessed.h')
48-
ns_bin_name, ns_bin_ext = splitext(basename(non_secure_bin))
49-
concatenated_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_name + ns_bin_ext))
50-
signed_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_name + '_signed' + ns_bin_ext))
49+
ns_bin_basename = splitext(basename(non_secure_bin))[0]
50+
concatenated_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_basename + ".bin"))
51+
signed_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_basename + '_signed' + ".bin"))
52+
signed_nopad_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_basename + '_signed_nopad' + ".bin"))
5153

5254
assert os.path.isfile(image_macros_s_ns)
5355

@@ -60,7 +62,7 @@ def tfm_sign_image_tgt(tfm_import_path, signing_key, non_secure_bin):
6062
#1. Concatenate secure TFM and non-secure mbed binaries
6163
cmd = [
6264
python3_cmd,
63-
path_join(MBED_OS_ROOT, "tools", "psa","tfm", "bin_utils","assemble.py"),
65+
path_join(MBED_OS_ROOT, "tools", "psa", "tfm", "bin_utils", "assemble.py"),
6466
"--layout",
6567
image_macros_s_ns,
6668
"-s",
@@ -77,10 +79,10 @@ def tfm_sign_image_tgt(tfm_import_path, signing_key, non_secure_bin):
7779
" binaries, Error code: " + str(retcode))
7880
return
7981

80-
#2. Run wrapper to sign the concatenated binary
82+
#2.1 Run wrapper to sign the concatenated binary with padding ("--pad"), so upgradeable by mcuboot
8183
cmd = [
8284
python3_cmd,
83-
path_join(MBED_OS_ROOT, "tools", "psa","tfm", "bin_utils","wrapper.py"),
85+
path_join(MBED_OS_ROOT, "tools", "psa","tfm", "bin_utils", "wrapper.py"),
8486
"-v",
8587
'1.2.0',
8688
"-k",
@@ -110,13 +112,24 @@ def tfm_sign_image_tgt(tfm_import_path, signing_key, non_secure_bin):
110112
" binary, Error code: " + str(retcode))
111113
return
112114

113-
#3. Concatenate mcuboot and signed binary and overwrite mbed built binary file
115+
#2.2. Re-run above but without padding ("--pad"), so non-upgradeable by mcuboot
116+
cmd.remove("--pad")
117+
cmd.pop()
118+
cmd.append(signed_nopad_bin)
119+
120+
retcode = run_cmd(cmd, MBED_OS_ROOT)
121+
if retcode:
122+
raise Exception("Unable to sign " + "concatenated" +
123+
" binary, Error code: " + str(retcode))
124+
return
125+
126+
#3. Concatenate mcuboot and signed binary and overwrite mbed built bin/hex file
114127
flash_area_0_offset = find_flash_area_0_offset(flash_layout)
115-
with open(bl2_bin, "rb") as mcuboot_fh, open(signed_bin, "rb") as signed_fh:
116-
with open(non_secure_bin, "w+b") as out_fh:
117-
out_fh.write(mcuboot_fh.read())
118-
out_fh.seek(flash_area_0_offset)
119-
out_fh.write(signed_fh.read())
128+
out_ih = IntelHex()
129+
out_ih.loadbin(bl2_bin)
130+
out_ih.loadbin(signed_nopad_bin, flash_area_0_offset)
131+
out_ih.tofile(splitext(non_secure_bin)[0] + ".hex", 'hex')
132+
out_ih.tobinfile(non_secure_bin)
120133

121134
def find_flash_area_0_offset(configFile):
122135
# Compiled regular expressions

tools/targets/NU_M2354.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,22 @@
2121
import re
2222
import subprocess
2323
import shutil
24+
from intelhex import IntelHex
2425

2526
SCRIPT_DIR = dirname(abspath(__file__))
2627
MBED_OS_ROOT = abspath(path_join(SCRIPT_DIR, os.pardir, os.pardir))
2728

28-
def m2354_tfm_bin(t_self, non_secure_bin, secure_bin):
29+
def m2354_tfm_bin(t_self, non_secure_image, secure_bin):
2930

3031
assert os.path.isfile(secure_bin)
31-
assert os.path.isfile(non_secure_bin)
32+
assert os.path.isfile(non_secure_image)
3233

3334
secure_bin = abspath(secure_bin)
34-
non_secure_bin = abspath(non_secure_bin)
35+
non_secure_image = abspath(non_secure_image)
3536

3637
SECURE_ROOT = abspath(dirname(secure_bin))
3738

38-
build_dir = dirname(non_secure_bin)
39+
build_dir = dirname(non_secure_image)
3940
tempdir = path_join(build_dir, 'temp')
4041
if not isdir(tempdir):
4142
os.makedirs(tempdir)
@@ -44,9 +45,18 @@ def m2354_tfm_bin(t_self, non_secure_bin, secure_bin):
4445

4546
bl2_bin = path_join(SECURE_ROOT, 'bl2.bin')
4647
image_macros_s_ns = path_join(SECURE_ROOT, 'partition', 'signing_layout_preprocessed.h')
47-
ns_bin_name, ns_bin_ext = splitext(basename(non_secure_bin))
48-
concatenated_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_name + ns_bin_ext))
49-
signed_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_name + '_signed' + ns_bin_ext))
48+
ns_bin_basename, output_ext = splitext(basename(non_secure_image))
49+
concatenated_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_basename + ".bin"))
50+
signed_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_basename + '_signed' + ".bin"))
51+
signed_nopad_bin = abspath(path_join(tempdir, 'tfm_' + ns_bin_basename + '_signed_nopad' + ".bin"))
52+
53+
# Convert NS image to BIN format if it is HEX
54+
if output_ext == ".hex":
55+
non_secure_bin = abspath(path_join(tempdir, ns_bin_basename + ".bin"))
56+
ns_ih = IntelHex(non_secure_image)
57+
ns_ih.tobinfile(non_secure_bin)
58+
else:
59+
non_secure_bin = non_secure_image
5060

5161
assert os.path.isfile(image_macros_s_ns)
5262

@@ -76,10 +86,10 @@ def m2354_tfm_bin(t_self, non_secure_bin, secure_bin):
7686
" binaries, Error code: " + str(retcode))
7787
return
7888

79-
#2. Run wrapper to sign the concatenated binary
89+
#2.1 Run wrapper to sign the concatenated binary with padding ("--pad"), so upgradeable by mcuboot
8090
cmd = [
8191
python3_cmd,
82-
path_join(MBED_OS_ROOT, "tools", "psa","tfm", "bin_utils","wrapper.py"),
92+
path_join(MBED_OS_ROOT, "tools", "psa", "tfm", "bin_utils", "wrapper.py"),
8393
"-v",
8494
'1.2.0',
8595
"-k",
@@ -109,13 +119,23 @@ def m2354_tfm_bin(t_self, non_secure_bin, secure_bin):
109119
" binary, Error code: " + str(retcode))
110120
return
111121

112-
#3. Concatenate mcuboot and signed binary and overwrite mbed built binary file
122+
#2.2. Re-run above but without padding ("--pad"), so non-upgradeable by mcuboot
123+
cmd.remove("--pad")
124+
cmd.pop()
125+
cmd.append(signed_nopad_bin)
126+
127+
retcode = run_cmd(cmd, MBED_OS_ROOT)
128+
if retcode:
129+
raise Exception("Unable to sign " + "concatenated" +
130+
" binary, Error code: " + str(retcode))
131+
return
132+
133+
#3. Concatenate mcuboot and signed binary and overwrite mbed built bin/hex file
113134
flash_area_0_offset = find_flash_area_0_offset(flash_layout)
114-
with open(bl2_bin, "rb") as mcuboot_fh, open(signed_bin, "rb") as signed_fh:
115-
with open(non_secure_bin, "w+b") as out_fh:
116-
out_fh.write(mcuboot_fh.read())
117-
out_fh.seek(flash_area_0_offset)
118-
out_fh.write(signed_fh.read())
135+
out_ih = IntelHex()
136+
out_ih.loadbin(bl2_bin)
137+
out_ih.loadbin(signed_nopad_bin, flash_area_0_offset)
138+
out_ih.tofile(non_secure_image, 'hex' if output_ext == ".hex" else "bin")
119139

120140
def find_flash_area_0_offset(configFile):
121141
# Compiled regular expressions

0 commit comments

Comments
 (0)