-
Notifications
You must be signed in to change notification settings - Fork 827
Feature: images in image support #2465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ozersa
wants to merge
2
commits into
mcu-tools:main
Choose a base branch
from
analogdevicesinc:feat_images_in_image_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+360
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
#! /usr/bin/env python3 | ||
# | ||
# Copyright (C) 2025 Analog Devices, Inc. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import argparse | ||
import os | ||
import pathlib | ||
import subprocess | ||
import sys | ||
|
||
import yaml | ||
import shutil | ||
|
||
|
||
# Function definitions | ||
|
||
|
||
def main(): | ||
global img_tool | ||
global output_dir | ||
global config_path | ||
|
||
parser = argparse.ArgumentParser(description="Create application package", allow_abbrev=False) | ||
parser.add_argument('--config', help="The path to config yaml file", required=True) | ||
parser.add_argument('--imgtool', help="The path to ImgTool", required=True) | ||
parser.add_argument('--output', help="Output directory", required=True) | ||
|
||
args = parser.parse_args() | ||
|
||
if not os.path.isfile(args.config): | ||
print(f"Error: The config file '{args.config}' does not exist") | ||
return | ||
|
||
config_path = os.path.dirname(os.path.abspath(args.config)) | ||
print(f"config_path: {config_path}") | ||
|
||
with open(args.config) as config_file: | ||
config = yaml.safe_load(config_file) | ||
|
||
img_tool = args.imgtool | ||
if img_tool.endswith('.py'): | ||
if not os.path.exists(img_tool): # Is python file exist? | ||
print(f"Error: The '{img_tool}' not found") | ||
return | ||
else: | ||
if not shutil.which(img_tool): # Is binary file exist? | ||
print(f"Error: The '{img_tool}' not found in the path") | ||
return | ||
|
||
|
||
output_dir = args.output | ||
os.makedirs(output_dir, exist_ok=True) | ||
|
||
parse_app_pack(config, None) | ||
|
||
print(f"\nCreated {package_name}") | ||
|
||
|
||
def verify_file_exists(filename): | ||
filepath = pathlib.Path(filename) | ||
|
||
if not filepath.exists(): | ||
print("ERROR: File " + filename + " not found") | ||
sys.exit(1) | ||
|
||
|
||
def parse_app_pack(app_pack, name): | ||
header = None | ||
image = [] | ||
imgtype = 0 | ||
global package_name | ||
|
||
for key in app_pack: | ||
if key.endswith("_pack"): | ||
imagename, imagetype = parse_app_pack(app_pack[key], name) | ||
image += [imagename] | ||
if key.lower() == "header": | ||
header = app_pack[key] | ||
if key.lower() == "image": | ||
image_path = os.path.join(config_path, app_pack[key]) | ||
verify_file_exists(image_path) | ||
image += [image_path] | ||
if key.lower() == "outputfile": | ||
name = app_pack[key] | ||
|
||
# Exit if header or image are not specified | ||
if (header is None) or (image is None): | ||
return (None, 0) | ||
|
||
operation = 0 | ||
|
||
cmd = [] | ||
if img_tool.endswith('.py'): | ||
cmd += [sys.executable] | ||
|
||
cmd += [img_tool] | ||
cmd += ["sign"] | ||
|
||
for key in header: | ||
if key == "align": | ||
cmd += ["--align", str(header[key])] | ||
elif key == "aes-gcm-key": | ||
operation += 2 | ||
gcm_key_path = os.path.join(config_path, header[key]) | ||
verify_file_exists(gcm_key_path) | ||
cmd += ["--aes-gcm-key", gcm_key_path] | ||
elif key == "aes-kw-key": | ||
operation += 2 | ||
kw_key_path = os.path.join(config_path, header[key]) | ||
verify_file_exists(kw_key_path) | ||
cmd += ["--aes-kw-key", kw_key_path] | ||
elif key == "header-size": | ||
cmd += ["--header-size", hex(header[key])] | ||
elif key == "load-addr": | ||
cmd += ["--load-addr", hex(header[key])] | ||
elif key == "pad-header": | ||
if header[key] is True: | ||
cmd += ["--pad-header"] | ||
elif key == "private_signing_key": | ||
operation += 1 | ||
private_key_path = os.path.join(config_path, header[key]) | ||
verify_file_exists(private_key_path) | ||
cmd += ["--key", private_key_path] | ||
elif key == "public-key-format": | ||
cmd += ["--public-key-format", header[key]] | ||
elif key == "slot-size": | ||
cmd += ["--slot-size", hex(header[key])] | ||
elif key == "version": | ||
cmd += ["--version", header[key]] | ||
else: | ||
print("Unknown argument: " + key) | ||
|
||
# If there are multiple input files they must be combined for signing or encryption | ||
if len(image) > 1: | ||
combined_images = os.path.join(output_dir, name + ".bin") | ||
|
||
with open(combined_images, 'wb') as outfile: | ||
for fname in image: | ||
with open(fname, 'rb') as infile: | ||
outfile.write(infile.read()) | ||
infile.close() | ||
outfile.close() | ||
|
||
image_input = combined_images | ||
else: | ||
image_input = image[0] | ||
|
||
if operation > 1: | ||
image_output = os.path.join(output_dir, name + "_signed_encrypted.bin") | ||
else: | ||
image_output = os.path.join(output_dir, name + "_signed.bin") | ||
|
||
cmd += [image_input] | ||
cmd += [image_output] | ||
|
||
print(f'Calling imgtool to generate file {image_output}') | ||
package_name = image_output | ||
subprocess.run(cmd) | ||
|
||
return (image_output, imgtype) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#! /usr/bin/env python3 | ||
# | ||
# Copyright (C) 2025 Analog Devices, Inc. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
combined_app_pack: | ||
outputfile: combined | ||
|
||
header: | ||
private_signing_key: ../root-rsa-2048.pem | ||
header-size: 0x400 | ||
align: 4 | ||
load-addr: 0x20080000 | ||
pad-header: yes | ||
version: 1.0.0 | ||
slot-size: 0x40000 | ||
|
||
image1_pack: | ||
outputfile: image1 | ||
header: | ||
private_signing_key: ../root-rsa-2048.pem | ||
header-size: 0x400 | ||
align: 4 | ||
load-addr: 0x20010000 | ||
pad-header: yes | ||
version: 1.0.0 | ||
slot-size: 0x10000 | ||
image: image1.bin | ||
|
||
image2_pack: | ||
outputfile: image2 | ||
header: | ||
private_signing_key: ../root-rsa-2048.pem | ||
header-size: 0x400 | ||
align: 4 | ||
load-addr: 0x20020000 | ||
pad-header: yes | ||
version: 1.0.0 | ||
slot-size: 0x10000 | ||
image: image2.bin |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should get it's own feature defined (maybe
MCUBOOT_SUBIMAGES
) instead of just assuming that if we have a single image that is ram loaded subimages would be wanted.In addition, we should define a new flag in the header to indicate when subimages are present in the image itself. Functionality wise:
MCUBOOT_SUBIMAGES
it not defined, but the flag is present, fail.MCUBOOT_SUBIMAGES
is defined, the flag will indicate whether a single image is loaded, or multiple images.In other words, separate the implementation of the feature from the flags indicate whether it is present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for feedback, checked it, as per of my check it seems new flags may not be fit for this feature due to for this feature MCUBOOT_IMAGE_NUMBER should be 1, unless you do not want it supports "multi image X multi-subimages".
And for Zephyr port, MCUBOOT_IMAGE_NUMBER set by zephyr Kconfig parameter, if we add MCUBOOT_SUBIMAGES that need to be set by each port of MCUboot.
Additionally if we define a new flag in mcuboot header, I am not clear how it should be set, please see existing options: https://github.com/mcu-tools/mcuboot/blob/main/scripts/imgtool/image.py#L804 The header flag set indirectly depend on some parameters specified.
So I think "#if (BOOT_IMAGE_NUMBER == 1) && defined(MCUBOOT_RAM_LOAD)" might be better,
Please let me know you would like to new flag be defined?