21
21
import re
22
22
import subprocess
23
23
import shutil
24
+ from intelhex import IntelHex
24
25
25
26
SCRIPT_DIR = dirname (abspath (__file__ ))
26
27
MBED_OS_ROOT = abspath (path_join (SCRIPT_DIR , os .pardir , os .pardir ))
27
28
28
- def m2354_tfm_bin (t_self , non_secure_bin , secure_bin ):
29
+ def m2354_tfm_bin (t_self , non_secure_image , secure_bin ):
29
30
30
31
assert os .path .isfile (secure_bin )
31
- assert os .path .isfile (non_secure_bin )
32
+ assert os .path .isfile (non_secure_image )
32
33
33
34
secure_bin = abspath (secure_bin )
34
- non_secure_bin = abspath (non_secure_bin )
35
+ non_secure_image = abspath (non_secure_image )
35
36
36
37
SECURE_ROOT = abspath (dirname (secure_bin ))
37
38
38
- build_dir = dirname (non_secure_bin )
39
+ build_dir = dirname (non_secure_image )
39
40
tempdir = path_join (build_dir , 'temp' )
40
41
if not isdir (tempdir ):
41
42
os .makedirs (tempdir )
@@ -44,9 +45,18 @@ def m2354_tfm_bin(t_self, non_secure_bin, secure_bin):
44
45
45
46
bl2_bin = path_join (SECURE_ROOT , 'bl2.bin' )
46
47
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
50
60
51
61
assert os .path .isfile (image_macros_s_ns )
52
62
@@ -76,10 +86,10 @@ def m2354_tfm_bin(t_self, non_secure_bin, secure_bin):
76
86
" binaries, Error code: " + str (retcode ))
77
87
return
78
88
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
80
90
cmd = [
81
91
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" ),
83
93
"-v" ,
84
94
'1.2.0' ,
85
95
"-k" ,
@@ -109,13 +119,23 @@ def m2354_tfm_bin(t_self, non_secure_bin, secure_bin):
109
119
" binary, Error code: " + str (retcode ))
110
120
return
111
121
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
113
134
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" )
119
139
120
140
def find_flash_area_0_offset (configFile ):
121
141
# Compiled regular expressions
0 commit comments