25
25
from intelhex import IntelHex , hex2bin , bin2hex
26
26
27
27
from ..config import ConfigException
28
- from ..settings import ROOT
29
28
30
29
# The size of the program data in Cypress HEX files is limited to 0x80000000
31
30
# Higher addresses contain additional metadata (chip protection, eFuse data, etc..)
@@ -487,7 +486,7 @@ def sign_image(toolchain, binf):
487
486
toolchain .notify .info ("Image UPGRADE: " + out_hex_name + "\n " )
488
487
489
488
490
- def sign_es_image (toolchain , elf , binf , m0hex ):
489
+ def sign_es100_image (toolchain , resourses , elf , binf , m0hex ):
491
490
"""
492
491
Adds signature to a binary file being built,
493
492
using cysecuretools python package.
@@ -518,20 +517,20 @@ def sign_es_image(toolchain, elf, binf, m0hex):
518
517
519
518
from pathlib import Path , PurePath
520
519
521
- mbed_os_root = Path (ROOT )
520
+ mbed_os_root = Path (os . getcwd () )
522
521
523
- # Use custom policy file defined in users mbed_app.json or use default
524
- # policy if no custom policy exists
525
- try :
526
- policy_path = Path (str (toolchain .config .get_config_data ()[0 ]["app.policy_file" ].value ))
527
- if policy_path .is_absolute ():
522
+ policy_path = Path (toolchain .target .policy_file )
523
+ if policy_path .is_absolute ():
524
+ policy_file = policy_path
525
+ else :
526
+ policy_path = mbed_os_root / policy_path
527
+
528
+ if os .path .isfile (str (policy_path )):
528
529
policy_file = policy_path
529
530
else :
530
- policy_file = mbed_os_root / policy_path
531
- toolchain .notify .debug ("[PSOC6.sign_image] Using custom policy file at: " + str (policy_file ))
532
- except KeyError as e :
533
- policy_file = mbed_os_root / Path ("targets/TARGET_Cypress/TARGET_PSOC6/TARGET_" + toolchain .target .name + "/policy_multi_CM0_CM4.json" )
534
- toolchain .notify .debug ("[PSOC6.sign_image] Using default policy file at: " + str (policy_file ))
531
+ policy_file = Path (find_policy (toolchain , resourses ))
532
+
533
+ toolchain .notify .info ("[PSOC6.sign_image] Using policy file: " + str (policy_file ))
535
534
536
535
# Append cysecuretools path to sys.path and import cysecuretools. This will
537
536
# prioritize system installations of cysecuretools over the included
@@ -541,12 +540,54 @@ def sign_es_image(toolchain, elf, binf, m0hex):
541
540
import cysecuretools
542
541
543
542
tools = cysecuretools .CySecureTools (secure_target , str (policy_file ))
544
- tools .sign_image (m0hex , image_id = 1 )
545
- tools .sign_image (binf , image_id = 16 )
543
+
544
+ sign_application (toolchain , tools , m0hex , image_id = toolchain .target .cm0_img_id )
545
+ sign_application (toolchain , tools , binf , image_id = toolchain .target .cm4_img_id )
546
546
547
547
complete (toolchain , elf , hexf0 = binf , hexf1 = m0hex )
548
548
549
549
550
+ def sign_application (toolchain , tools , binary , image_id ):
551
+ """
552
+ Helper function for adding signature to binary
553
+ :param tools: CySecureTools object
554
+ :param binary: Path to binary file to add signature
555
+ :param image_id: ID of image slot in which binary will be flashed
556
+ """
557
+
558
+ # Get address and size of image slot from policy for passed image_id
559
+ # UPGRADE image will be generated automatically by cysecuretools
560
+ address , size = tools .flash_map (image_id = image_id , image_type = "BOOT" )
561
+
562
+ tools .sign_image (binary , image_id )
563
+ toolchain .notify .debug ("[PSOC6.sign_image] Slot start address and size for image ID " \
564
+ + str (image_id ) + " is " + hex (address ) + ", " + hex (size ))
565
+
566
+
567
+ def find_policy (toolchain , resources ):
568
+ """
569
+ Locate path to policy file, defined in targets.json
570
+ :param toolchain: toolchain object from mbed build system
571
+ :param resources: resources object from mbed build system
572
+ """
573
+ policy_filename = toolchain .target .policy_file
574
+
575
+ if policy_filename is None :
576
+ return None
577
+ # Locate user-specified image
578
+ from tools .resources import FileType
579
+ json_files = resources .get_file_paths (FileType .JSON )
580
+ policy = next ((f for f in json_files if os .path .basename (f ) == policy_filename ), None )
581
+
582
+ if policy :
583
+ toolchain .notify .info ("Policy file found: %s." % policy )
584
+ else :
585
+ toolchain .notify .info ("Policy file %s not found. Aborting." % policy_filename )
586
+ raise ConfigException ("Required policy file not found." )
587
+
588
+ return policy
589
+
590
+
550
591
def complete (toolchain , elf0 , hexf0 , hexf1 = None ):
551
592
"""
552
593
Merge CM4 and CM0 images to a single binary
0 commit comments