24
24
import argparse
25
25
from intelhex import IntelHex
26
26
from datetime import datetime
27
+ import sys
27
28
28
29
SCRIPT_DIR = dirname (abspath (__file__ ))
29
30
MBED_OS_ROOT = abspath (path_join (SCRIPT_DIR , os .pardir , os .pardir , os .pardir ))
30
31
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 ):
32
33
SECURE_ROOT = abspath (tfm_import_path )
33
34
34
35
secure_bin = path_join (SECURE_ROOT , 'tfm_s.bin' )
35
36
assert os .path .isfile (secure_bin )
36
37
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 )
39
40
40
- build_dir = dirname (non_secure_bin )
41
+ build_dir = dirname (non_secure_binhex )
41
42
tempdir = path_join (build_dir , 'temp' )
42
43
if not isdir (tempdir ):
43
44
os .makedirs (tempdir )
@@ -46,13 +47,20 @@ def tfm_sign_image(tfm_import_path, signing_key, signing_key_1, non_secure_bin):
46
47
47
48
bl2_bin = path_join (SECURE_ROOT , 'bl2.bin' )
48
49
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 ]
50
51
51
52
signing_key = path_join (SECURE_ROOT , 'signing_key' , signing_key )
52
53
assert os .path .isfile (signing_key )
53
54
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
+
54
62
# 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
56
64
57
65
# Specify image version
58
66
#
@@ -162,7 +170,7 @@ def tfm_sign_image(tfm_import_path, signing_key, signing_key_1, non_secure_bin):
162
170
signed_concat_bin = abspath (path_join (tempdir , 'tfm_s_signed_' + ns_bin_basename + '_signed_concat' + '.bin' ))
163
171
s_update_bin = abspath (path_join (build_dir , s_bin_basename + '_update' + '.bin' ))
164
172
ns_update_bin = abspath (path_join (build_dir , ns_bin_basename + '_update' + '.bin' ))
165
-
173
+
166
174
#1. Run wrapper to sign the secure TF-M binary
167
175
cmd_wrapper [pos_wrapper_signing_key ] = signing_key
168
176
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):
206
214
out_ih = IntelHex ()
207
215
out_ih .loadbin (bl2_bin )
208
216
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' )
211
219
212
220
# Generate firmware update file for PSA Firmware Update
213
221
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):
250
258
out_ih = IntelHex ()
251
259
out_ih .loadbin (bl2_bin )
252
260
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' )
255
263
256
264
# Generate firmware update file for PSA Firmware Update
257
265
shutil .copy (concat_signed_bin , update_bin )
@@ -357,7 +365,7 @@ def parse_args():
357
365
)
358
366
359
367
parser_tfm_sign_image .add_argument (
360
- "--non-secure-bin " ,
368
+ "--non-secure-binhex " ,
361
369
help = "Path to the non-secure binary" ,
362
370
required = True
363
371
)
@@ -368,4 +376,4 @@ def parse_args():
368
376
369
377
if __name__ == "__main__" :
370
378
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 )
0 commit comments