Skip to content

Commit ae733ff

Browse files
committed
Add default secure boot params to targets.json, add option to post build to redefine them in mbed_app.json
1 parent 9294eee commit ae733ff

File tree

3 files changed

+64
-20
lines changed

3 files changed

+64
-20
lines changed

targets/targets.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14093,8 +14093,11 @@
1409314093
],
1409414094
"forced_reset_timeout": 5,
1409514095
"hex_filename": "psoc64_02_cm0_secure.hex",
14096+
"cm0_img_id": 1,
14097+
"cm4_img_id": 16,
14098+
"policy_file": "policy_multi_CM0_CM4.json",
1409614099
"post_binary_hook": {
14097-
"function": "PSOC6Code.sign_es_image"
14100+
"function": "PSOC6Code.sign_es100_image"
1409814101
},
1409914102
"overrides": {
1410014103
"network-default-interface-type": "WIFI"

tools/targets/PSOC6.py

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from intelhex import IntelHex, hex2bin, bin2hex
2626

2727
from ..config import ConfigException
28-
from ..settings import ROOT
2928

3029
# The size of the program data in Cypress HEX files is limited to 0x80000000
3130
# Higher addresses contain additional metadata (chip protection, eFuse data, etc..)
@@ -487,7 +486,7 @@ def sign_image(toolchain, binf):
487486
toolchain.notify.info("Image UPGRADE: " + out_hex_name + "\n")
488487

489488

490-
def sign_es_image(toolchain, elf, binf, m0hex):
489+
def sign_es100_image(toolchain, resourses, elf, binf, m0hex):
491490
"""
492491
Adds signature to a binary file being built,
493492
using cysecuretools python package.
@@ -518,20 +517,20 @@ def sign_es_image(toolchain, elf, binf, m0hex):
518517

519518
from pathlib import Path, PurePath
520519

521-
mbed_os_root = Path(ROOT)
520+
mbed_os_root = Path(os.getcwd())
522521

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)):
528529
policy_file = policy_path
529530
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))
535534

536535
# Append cysecuretools path to sys.path and import cysecuretools. This will
537536
# prioritize system installations of cysecuretools over the included
@@ -541,12 +540,54 @@ def sign_es_image(toolchain, elf, binf, m0hex):
541540
import cysecuretools
542541

543542
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)
546546

547547
complete(toolchain, elf, hexf0=binf, hexf1=m0hex)
548548

549549

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+
550591
def complete(toolchain, elf0, hexf0, hexf1=None):
551592
"""
552593
Merge CM4 and CM0 images to a single binary

tools/targets/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,19 +695,19 @@ def sign_image(t_self, resources, elf, binf):
695695
psoc6_sign_image(t_self, binf)
696696

697697
@staticmethod
698-
def sign_es_image(t_self, resources, elf, binf):
698+
def sign_es100_image(t_self, resources, elf, binf):
699699
"""
700-
Calls sign_es_image function to add signature to Secure Boot binary file.
700+
Calls sign_es100_image function to add signature to Secure Boot binary file.
701701
This function is used with Cypress kits, that support cysecuretools signing.
702702
"""
703-
from tools.targets.PSOC6 import sign_es_image as psoc6_sign_es_image
703+
from tools.targets.PSOC6 import sign_es100_image as psoc6_sign_es100_image
704704
if hasattr(t_self.target, "hex_filename"):
705705
hex_filename = t_self.target.hex_filename
706706
# Completing main image involves merging M0 image.
707707
from tools.targets.PSOC6 import find_cm0_image
708708
m0hexf = find_cm0_image(t_self, resources, elf, binf, hex_filename)
709709

710-
psoc6_sign_es_image(t_self, elf, binf, m0hexf)
710+
psoc6_sign_es100_image(t_self, resources, elf, binf, m0hexf)
711711

712712

713713
class ArmMuscaA1Code(object):

0 commit comments

Comments
 (0)