2424import argparse
2525from intelhex import IntelHex
2626from datetime import datetime
27+ import sys
2728
2829SCRIPT_DIR = dirname (abspath (__file__ ))
2930MBED_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
369377if __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 )
0 commit comments