blob: 30adcba4070cde7930f39cb4b2c2642cf2114cb7 [file] [log] [blame]
David Browndbc57272017-07-17 15:38:54 -06001#! /usr/bin/env python3
2
3"""
4Assemble multiple images into a single image that can be flashed on the device.
5"""
6
7import argparse
David Brown2cf522c2017-07-20 12:15:31 -06008import errno
David Browndbc57272017-07-17 15:38:54 -06009import io
10import re
11import os.path
12
13def same_keys(a, b):
14 """Determine if the dicts a and b have the same keys in them"""
15 for ak in a.keys():
16 if ak not in b:
17 return False
18 for bk in b.keys():
19 if bk not in a:
20 return False
21 return True
22
23offset_re = re.compile(r"^#define FLASH_AREA_([0-9A-Z_]+)_OFFSET_0\s+((0x)?[0-9a-fA-F]+)")
24size_re = re.compile(r"^#define FLASH_AREA_([0-9A-Z_]+)_SIZE_0\s+((0x)?[0-9a-fA-F]+)")
25
26class Assembly():
27 def __init__(self, output, bootdir):
28 self.find_slots(bootdir)
David Brown2cf522c2017-07-20 12:15:31 -060029 try:
30 os.unlink(output)
31 except OSError as e:
32 if e.errno != errno.ENOENT:
33 raise
David Browndbc57272017-07-17 15:38:54 -060034 self.output = output
35
36 def find_slots(self, bootdir):
37 offsets = {}
38 sizes = {}
39 with open(os.path.join(bootdir, 'include', 'generated', 'generated_dts_board.h'), 'r') as fd:
40 for line in fd:
41 m = offset_re.match(line)
42 if m is not None:
43 offsets[m.group(1)] = int(m.group(2), 0)
44 m = size_re.match(line)
45 if m is not None:
46 sizes[m.group(1)] = int(m.group(2), 0)
47
48 if not same_keys(offsets, sizes):
49 raise Exception("Inconsistent data in generated_dts_board.h")
50
51 # We care about the MCUBOOT, IMAGE_0, and IMAGE_1 partitions.
52 if 'MCUBOOT' not in offsets:
53 raise Exception("Board partition table does not have mcuboot partition")
54
55 if 'IMAGE_0' not in offsets:
56 raise Exception("Board partition table does not have image-0 partition")
57
58 if 'IMAGE_1' not in offsets:
59 raise Exception("Board partition table does not have image-1 partition")
60
61 self.offsets = offsets
62 self.sizes = sizes
63
64 def add_image(self, source, partition):
65 with open(self.output, 'ab') as ofd:
66 pos = ofd.tell()
67 print("partition {}, pos={}, offset={}".format(partition, pos, self.offsets[partition]))
68 if pos > self.offsets[partition]:
69 raise Exception("Partitions not in order, unsupported")
70 if pos < self.offsets[partition]:
71 buf = b'\xFF' * (self.offsets[partition] - pos)
72 ofd.write(buf)
73 with open(source, 'rb') as rfd:
74 ibuf = rfd.read()
75 if len(ibuf) > self.sizes[partition]:
76 raise Exception("Image {} is too large for partition".format(source))
77 ofd.write(ibuf)
78
79def main():
80 parser = argparse.ArgumentParser()
81
82 parser.add_argument('-b', '--bootdir', required=True,
83 help='Directory of built bootloader')
84 parser.add_argument('-p', '--primary', required=True,
85 help='Signed image file for primary image')
86 parser.add_argument('-s', '--secondary',
87 help='Signed image file for secondary image')
88 parser.add_argument('-o', '--output', required=True,
89 help='Filename to write full image to')
90
91 args = parser.parse_args()
92 output = Assembly(args.output, args.bootdir)
93
94 output.add_image(os.path.join(args.bootdir, "zephyr.bin"), 'MCUBOOT')
95 output.add_image(args.primary, "IMAGE_0")
96 if args.secondary is not None:
97 output.add_image(args.secondary, "IMAGE_1")
98
99if __name__ == '__main__':
100 main()