From 2184ee617f1d6ac24a6e31733ba09b52e1eaa061 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 24 May 2025 11:19:48 +0200 Subject: tools: Format Python scripts with `black` --- tools/build_zephyr.py | 5 +---- tools/configure_zephyr.py | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) (limited to 'tools') diff --git a/tools/build_zephyr.py b/tools/build_zephyr.py index 1d9e783..5dd9e47 100755 --- a/tools/build_zephyr.py +++ b/tools/build_zephyr.py @@ -38,10 +38,7 @@ def main() -> None: check=True, ) - shutil.copy( - build_tree / "zephyr" / args.binary_name, - output_dir / args.target_name - ) + shutil.copy(build_tree / "zephyr" / args.binary_name, output_dir / args.target_name) if __name__ == "__main__": diff --git a/tools/configure_zephyr.py b/tools/configure_zephyr.py index e709063..f4707c6 100755 --- a/tools/configure_zephyr.py +++ b/tools/configure_zephyr.py @@ -43,7 +43,7 @@ def main() -> None: command.append(f"-DEXTRA_CONF_FILE={args.extra_config}") if args.signing_key is not None: - command.append(f"-DCONFIG_BOOT_SIGNATURE_KEY_FILE=\"{args.signing_key}\"") + command.append(f'-DCONFIG_BOOT_SIGNATURE_KEY_FILE="{args.signing_key}"') subprocess.run(command, shell=False, check=True) -- cgit v1.2.3-70-g09d2 From 072c1d0208b267abf6a573ad5d2482113d9c9cee Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 24 May 2025 11:19:54 +0200 Subject: tools: Add `make_factory_image.py` This script combines a bootloader firmware and a signed and confirmed MCUboot application firmware to one factory image which can be loaded to the default boot address of the microcontroller. --- tools/make_factory_image.py | 95 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100755 tools/make_factory_image.py (limited to 'tools') diff --git a/tools/make_factory_image.py b/tools/make_factory_image.py new file mode 100755 index 0000000..735f657 --- /dev/null +++ b/tools/make_factory_image.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python3 + + +import argparse +import dataclasses +import pathlib + + +PADDING_BYTE: bytes = b"\xff" + + +def main() -> None: + args = Arguments.from_cli() + print(args) + + factory_image: bytes = join( + bootloader=args.bootloader.read_bytes(), + application=args.application.read_bytes(), + offset=args.offset, + ) + + args.factory_image.write_bytes(factory_image) + + +def join(bootloader: bytes, application: bytes, offset: int) -> bytes: + padding = PADDING_BYTE * (offset - len(bootloader)) + + return bootloader + padding + application + + +@dataclasses.dataclass +class Arguments: + bootloader: pathlib.Path + offset: int + application: pathlib.Path + factory_image: pathlib.Path + + def __post_init__(self) -> None: + assert isinstance(self.bootloader, pathlib.Path) + + assert isinstance(self.offset, int) + assert self.offset >= 0 + + assert isinstance(self.application, pathlib.Path) + + assert isinstance(self.factory_image, pathlib.Path) + + def __str__(self) -> str: + return f"""{__file__} \\ + --bootloader {self.bootloader} \\ + --offset 0x{self.offset:X} \\ + --application {self.application} \\ + --factory_image {self.factory_image}""" + + @staticmethod + def from_cli() -> "Arguments": + parser = argparse.ArgumentParser( + description="Join bootloader and application firmware to a factory image" + ) + + parser.add_argument( + "-b", "--bootloader", required=True, help="path to bootloader firmware" + ) + + default_offset = 0x40000 + parser.add_argument( + "-o", + "--offset", + default=default_offset, + help=f"offset in bytes between bootloader and application (default: 0x{default_offset:X})", + ) + + parser.add_argument( + "-a", "--application", required=True, help="path to application firmware" + ) + + parser.add_argument( + "-f", + "--factory-image", + default=pathlib.Path("factory-image.bin"), + help="path to output factory image file", + ) + + args = parser.parse_args() + + return Arguments( + bootloader=pathlib.Path(args.bootloader), + offset=int(args.offset), + application=pathlib.Path(args.application), + factory_image=pathlib.Path(args.factory_image), + ) + + +if __name__ == "__main__": + main() -- cgit v1.2.3-70-g09d2 From fc3b3cbf7fecee7226f249f7b62cf36aa82a545a Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 24 May 2025 11:20:08 +0200 Subject: Provide `factory-image.bin` with Meson This automatically creates `build/artifacts/factory-image.bin` with the Meson build system. The resulting file can simply be moved to the virtual file system of the `nucleo_f767zi` board to flash bootloader and application making the board ready for operation and remote updates. --- artifacts/meson.build | 1 + fw/btl/meson.build | 1 - fw/meson.build | 14 ++++++++++++++ tools/meson.build | 1 + 4 files changed, 16 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/artifacts/meson.build b/artifacts/meson.build index ea6fc39..9e14232 100644 --- a/artifacts/meson.build +++ b/artifacts/meson.build @@ -6,6 +6,7 @@ artifacts = [ simulation, bootloader, application_signed, + factory_image, ] foreach artifact : artifacts diff --git a/fw/btl/meson.build b/fw/btl/meson.build index 2ba1cf4..204fe20 100644 --- a/fw/btl/meson.build +++ b/fw/btl/meson.build @@ -22,5 +22,4 @@ bootloader = custom_target( '--binary-name', 'zephyr.bin', '--target-name', 'bootloader.bin', ], - build_by_default: true, ) diff --git a/fw/meson.build b/fw/meson.build index 8194827..8f45d5b 100644 --- a/fw/meson.build +++ b/fw/meson.build @@ -7,3 +7,17 @@ subdir('rtos') subdir('app') subdir('btl') subdir('sim') + +factory_image = custom_target( + output: ['factory-image.bin'], + command: [ + make_factory_image, + '--bootloader', bootloader, + '--application', application_signed_confirmed, + '--factory-image', '@OUTPUT@', + ], + depends: [ + bootloader, + application_signed_confirmed, + ], +) diff --git a/tools/meson.build b/tools/meson.build index 85ddbb3..f58c54b 100644 --- a/tools/meson.build +++ b/tools/meson.build @@ -1,2 +1,3 @@ configure_zephyr = meson.current_source_dir() / 'configure_zephyr.py' build_zephyr = meson.current_source_dir() / 'build_zephyr.py' +make_factory_image = meson.current_source_dir() / 'make_factory_image.py' -- cgit v1.2.3-70-g09d2