-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_update_image.py
executable file
·96 lines (77 loc) · 2.47 KB
/
create_update_image.py
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
96
#!/usr/bin/env python3
""" Program to create update image """
import argparse
import functools
import struct
import zlib
from intelhex import IntelHex
def parse_arguments():
"""Parse the command line arguments"""
parser = argparse.ArgumentParser("Create upgrade file")
parser.add_argument(
"--input",
"-I",
help="Path to app_moved_test_update.hex file",
default="build/zephyr/app_moved_test_update.hex",
)
parser.add_argument(
"--output",
"-O",
help="Path to app_moved_test_update.hex file",
default="update_app.hex",
)
parser.add_argument(
"--page-size",
type=functools.partial(int, base=0),
help="Size of flash page",
default=4096,
)
parser.add_argument(
"--mcuboot-pad-size",
type=functools.partial(int, base=0),
help="Size of mcuboot_pad",
default=0x200,
)
parser.add_argument(
"--backup-header-start",
type=functools.partial(int, base=0),
help="Address of the backup header",
default=0xFD000,
)
parser.add_argument(
"--backup-trailer-start",
type=functools.partial(int, base=0),
help="Address of the backup trailer",
default=0xFE000,
)
return parser.parse_args()
def create_fota_header(blob, fota_type, flags, version):
"""Create a FOTA SWAP area header"""
size = len(blob)
crc = zlib.crc32(blob)
return struct.pack("<IIHBB", size, crc, fota_type, flags, version)
def main():
"""Translate a program to update format with backup pages and FOTA header"""
args = parse_arguments()
app_hex = args.input
output_hex = args.output
page_size = args.page_size
backup_header_start = args.backup_header_start
backup_trailer_start = args.backup_trailer_start
mcuboot_pad_size = args.mcuboot_pad_size
hex_data = IntelHex(app_hex)
binary_data = hex_data.tobinarray()
total_pages = (len(binary_data) + page_size - 1) // page_size
# Add header backup page:
hex_data.frombytes(binary_data[0:page_size], backup_header_start)
# Add fota header:
fota_header = create_fota_header(binary_data, 0, 0, 0)
hex_data.frombytes(
fota_header, backup_header_start + mcuboot_pad_size - len(fota_header)
)
# Add trailer backup page:
hex_data.frombytes(
binary_data[((total_pages - 1) * page_size) :], backup_trailer_start
)
hex_data.tofile(output_hex, format="hex")
main()