|
| 1 | +# |
| 2 | +# Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | +# |
| 6 | +"""CMD_PAYLOAD_EXTRACT CLI command entry point.""" |
| 7 | + |
| 8 | +import cbor2 |
| 9 | +import logging |
| 10 | + |
| 11 | +log = logging.getLogger(__name__) |
| 12 | + |
| 13 | +PAYLOAD_EXTRACT_CMD = "payload_extract" |
| 14 | + |
| 15 | + |
| 16 | +def add_arguments(parser): |
| 17 | + """Add additional arguments to the passed parser.""" |
| 18 | + cmd_payload_extract_arg_parser = parser.add_parser(PAYLOAD_EXTRACT_CMD, help="Create raw cache structure.") |
| 19 | + |
| 20 | + cmd_payload_extract_arg_parser.add_argument("--input-envelope", required=True, help="Input envelope file path.") |
| 21 | + cmd_payload_extract_arg_parser.add_argument("--output-envelope", required=True, help="Output envelope file path.") |
| 22 | + cmd_payload_extract_arg_parser.add_argument( |
| 23 | + "--payload-name", required=True, help="Name of the integrated payload to extract." |
| 24 | + ) |
| 25 | + cmd_payload_extract_arg_parser.add_argument( |
| 26 | + "--output-payload-file", |
| 27 | + required=False, |
| 28 | + help="Output payload file path to store the extracted payload." |
| 29 | + + "If not provided, the payload will not be stored to a file.", |
| 30 | + ) |
| 31 | + |
| 32 | + cmd_payload_extract_arg_parser.add_argument( |
| 33 | + "--payload-replace-path", |
| 34 | + help="Path to the integrated payload to replace the extracted payload with." |
| 35 | + + "If not provided, the payload will be removed from the envelope.", |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def main( |
| 40 | + input_envelope: str, output_envelope: str, payload_name: str, output_payload_file: str, payload_replace_path: str |
| 41 | +) -> None: |
| 42 | + """Extract an integrated payload from a SUIT envelope. |
| 43 | +
|
| 44 | + :param input_envelope: input envelope file path |
| 45 | + :param output_envelope: output envelope file path |
| 46 | + :param payload_name: name of the integrated payload to extract |
| 47 | + :param output_payload_file: output file path to store the extracted payload |
| 48 | + None if the payload should not be stored to a file |
| 49 | + :param payload_replace_path: Path to the integrated payload to replace the extracted payload with. |
| 50 | + None if the payload should be removed from the envelope. |
| 51 | + """ |
| 52 | + with open(input_envelope, "rb") as fh: |
| 53 | + envelope = cbor2.load(fh) |
| 54 | + extracted_payload = envelope.value.pop(payload_name, None) |
| 55 | + |
| 56 | + if extracted_payload is None: |
| 57 | + log.log(logging.ERROR, 'Payload "%s" not found in envelope', payload_name) |
| 58 | + |
| 59 | + if payload_replace_path is not None: |
| 60 | + with open(payload_replace_path, "rb") as fh: |
| 61 | + envelope.value[payload_name] = fh.read() |
| 62 | + |
| 63 | + with open(output_envelope, "wb") as fh: |
| 64 | + cbor2.dump(envelope, fh) |
| 65 | + if output_payload_file is not None: |
| 66 | + with open(output_payload_file, "wb") as fh: |
| 67 | + fh.write(extracted_payload) |
0 commit comments