Skip to content

Commit 9294eee

Browse files
committed
Add initial implementation of post build function, that uses cysecuretools
1 parent 5267ec3 commit 9294eee

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

targets/targets.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14092,8 +14092,9 @@
1409214092
"1910"
1409314093
],
1409414094
"forced_reset_timeout": 5,
14095+
"hex_filename": "psoc64_02_cm0_secure.hex",
1409514096
"post_binary_hook": {
14096-
"function": "PSOC6Code.sign_image"
14097+
"function": "PSOC6Code.sign_es_image"
1409714098
},
1409814099
"overrides": {
1409914100
"network-default-interface-type": "WIFI"

tools/targets/PSOC6.py

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

2727
from ..config import ConfigException
28+
from ..settings import ROOT
2829

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

488489

490+
def sign_es_image(toolchain, elf, binf, m0hex):
491+
"""
492+
Adds signature to a binary file being built,
493+
using cysecuretools python package.
494+
:param toolchain: Toolchain object of current build session
495+
:param binf: Binary file created for target
496+
"""
497+
498+
print("Found CM0 prebuild hex file: ")
499+
print(m0hex)
500+
501+
m0hex_build = os.path.join(toolchain.build_dir, toolchain.target.hex_filename)
502+
503+
copy2(m0hex, m0hex_build)
504+
505+
m0hex = m0hex_build
506+
507+
print(m0hex)
508+
509+
# Mapping from mbed target to cysecuretools target
510+
TARGET_MAPPING = {
511+
"CY8CKIT_064B0S2_4343W": "cy8ckit-064b0s2-4343w"
512+
}
513+
514+
try:
515+
secure_target = TARGET_MAPPING[toolchain.target.name]
516+
except KeyError:
517+
raise ConfigException("[PSOC6.sign_image] Target " + toolchain.target.name + " is not supported in cysecuretools.")
518+
519+
from pathlib import Path, PurePath
520+
521+
mbed_os_root = Path(ROOT)
522+
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():
528+
policy_file = policy_path
529+
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))
535+
536+
# Append cysecuretools path to sys.path and import cysecuretools. This will
537+
# prioritize system installations of cysecuretools over the included
538+
# cysecuretools.
539+
#sb_tools_path = mbed_os_root / Path("targets/TARGET_Cypress/TARGET_PSOC6/")
540+
#sys.path.append(str(sb_tools_path))
541+
import cysecuretools
542+
543+
tools = cysecuretools.CySecureTools(secure_target, str(policy_file))
544+
tools.sign_image(m0hex, image_id=1)
545+
tools.sign_image(binf, image_id=16)
546+
547+
complete(toolchain, elf, hexf0=binf, hexf1=m0hex)
548+
549+
489550
def complete(toolchain, elf0, hexf0, hexf1=None):
490551
"""
491552
Merge CM4 and CM0 images to a single binary

tools/targets/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,22 @@ def sign_image(t_self, resources, elf, binf):
694694
from tools.targets.PSOC6 import sign_image as psoc6_sign_image
695695
psoc6_sign_image(t_self, binf)
696696

697+
@staticmethod
698+
def sign_es_image(t_self, resources, elf, binf):
699+
"""
700+
Calls sign_es_image function to add signature to Secure Boot binary file.
701+
This function is used with Cypress kits, that support cysecuretools signing.
702+
"""
703+
from tools.targets.PSOC6 import sign_es_image as psoc6_sign_es_image
704+
if hasattr(t_self.target, "hex_filename"):
705+
hex_filename = t_self.target.hex_filename
706+
# Completing main image involves merging M0 image.
707+
from tools.targets.PSOC6 import find_cm0_image
708+
m0hexf = find_cm0_image(t_self, resources, elf, binf, hex_filename)
709+
710+
psoc6_sign_es_image(t_self, elf, binf, m0hexf)
711+
712+
697713
class ArmMuscaA1Code(object):
698714
"""Musca-A1 Hooks"""
699715
@staticmethod

0 commit comments

Comments
 (0)