Skip to content

Commit a9b80a1

Browse files
committed
Added suit-generator payload_extract command
The payload allows for extracting/removing/replacing integrated payloads in the envelope. Ref: NCSDK-29708 Signed-off-by: Artur Hadasz <artur.hadasz@nordicsemi.no>
1 parent 6ae3b38 commit a9b80a1

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

suit_generator/args.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from suit_generator.cmd_convert import add_arguments as convert_args
1717
from suit_generator.cmd_mpi import add_arguments as mpi_args
1818
from suit_generator.cmd_cache_create import add_arguments as cache_create_args
19+
from suit_generator.cmd_payload_extract import add_arguments as payload_extract_args
1920

2021

2122
def _parser() -> ArgumentParser:
@@ -30,6 +31,7 @@ def _parser() -> ArgumentParser:
3031
convert_args(subparsers)
3132
mpi_args(subparsers)
3233
cache_create_args(subparsers)
34+
payload_extract_args(subparsers)
3335
return parser
3436

3537

suit_generator/cli.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111

1212
sys.path.insert(0, str(pathlib.Path(__file__).parents[1].absolute()))
1313

14-
from suit_generator import cmd_parse, cmd_keys, cmd_convert, cmd_create, cmd_image, cmd_mpi, cmd_cache_create, args
14+
from suit_generator import (
15+
cmd_parse,
16+
cmd_keys,
17+
cmd_convert,
18+
cmd_create,
19+
cmd_image,
20+
cmd_mpi,
21+
cmd_cache_create,
22+
cmd_payload_extract,
23+
args,
24+
)
1525
from suit_generator.exceptions import GeneratorError, SUITError
1626

1727
import logging
@@ -30,6 +40,7 @@
3040
cmd_image.ImageCreator.IMAGE_CMD: cmd_image.main,
3141
cmd_mpi.MPI_CMD: cmd_mpi.main,
3242
cmd_cache_create.CACHE_CREATE_CMD: cmd_cache_create.main,
43+
cmd_payload_extract.PAYLOAD_EXTRACT_CMD: cmd_payload_extract.main,
3344
}
3445

3546

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)