1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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()
|