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