2121import re
2222import subprocess
2323import shutil
24+ from intelhex import IntelHex
2425
2526SCRIPT_DIR = dirname (abspath (__file__ ))
2627MBED_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
120140def find_flash_area_0_offset (configFile ):
121141 # Compiled regular expressions
0 commit comments