|
25 | 25 | from intelhex import IntelHex, hex2bin, bin2hex
|
26 | 26 |
|
27 | 27 | from ..config import ConfigException
|
| 28 | +from ..settings import ROOT |
28 | 29 |
|
29 | 30 | # The size of the program data in Cypress HEX files is limited to 0x80000000
|
30 | 31 | # Higher addresses contain additional metadata (chip protection, eFuse data, etc..)
|
@@ -486,6 +487,66 @@ def sign_image(toolchain, binf):
|
486 | 487 | toolchain.notify.info("Image UPGRADE: " + out_hex_name + "\n")
|
487 | 488 |
|
488 | 489 |
|
| 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 | + |
489 | 550 | def complete(toolchain, elf0, hexf0, hexf1=None):
|
490 | 551 | """
|
491 | 552 | Merge CM4 and CM0 images to a single binary
|
|
0 commit comments