blob: e895ee7735dfdc388ae9b0173b3598e0418fa73e [file] [log] [blame]
David Browndbc57272017-07-17 15:38:54 -06001#! /usr/bin/env python3
David Brownb730e242017-12-20 11:10:55 -07002#
3# Copyright 2017 Linaro Limited
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
David Browndbc57272017-07-17 15:38:54 -060016
17"""
18Assemble multiple images into a single image that can be flashed on the device.
19"""
20
21import argparse
David Brown2cf522c2017-07-20 12:15:31 -060022import errno
David Browndbc57272017-07-17 15:38:54 -060023import io
24import re
25import os.path
Kumar Gala45b00ac2020-05-11 15:33:03 -050026import sys
27
28ZEPHYR_BASE = os.getenv("ZEPHYR_BASE")
29if not ZEPHYR_BASE:
30 sys.exit("$ZEPHYR_BASE environment variable undefined")
31
32sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts", "dts"))
33import edtlib
David Browndbc57272017-07-17 15:38:54 -060034
35def same_keys(a, b):
36 """Determine if the dicts a and b have the same keys in them"""
37 for ak in a.keys():
38 if ak not in b:
39 return False
40 for bk in b.keys():
41 if bk not in a:
42 return False
43 return True
44
Fabio Utzig539d7662019-09-05 10:57:00 -030045offset_re = re.compile(r"^#define DT_FLASH_AREA_([0-9A-Z_]+)_OFFSET(_0)?\s+(0x[0-9a-fA-F]+|[0-9]+)$")
46size_re = re.compile(r"^#define DT_FLASH_AREA_([0-9A-Z_]+)_SIZE(_0)?\s+(0x[0-9a-fA-F]+|[0-9]+)$")
David Browndbc57272017-07-17 15:38:54 -060047
48class Assembly():
Kumar Gala45b00ac2020-05-11 15:33:03 -050049 def __init__(self, output, bootdir, edt):
50 self.find_slots(edt)
David Brown2cf522c2017-07-20 12:15:31 -060051 try:
52 os.unlink(output)
53 except OSError as e:
54 if e.errno != errno.ENOENT:
55 raise
David Browndbc57272017-07-17 15:38:54 -060056 self.output = output
57
Kumar Gala45b00ac2020-05-11 15:33:03 -050058 def find_slots(self, edt):
David Browndbc57272017-07-17 15:38:54 -060059 offsets = {}
60 sizes = {}
Kumar Gala45b00ac2020-05-11 15:33:03 -050061
62 part_nodes = edt.compat2nodes["fixed-partitions"]
63 for node in part_nodes:
64 for child in node.children.values():
65 if "label" in child.props:
66 label = child.props["label"].val
67 offsets[label] = child.regs[0].addr
68 sizes[label] = child.regs[0].size
David Browndbc57272017-07-17 15:38:54 -060069
70 if not same_keys(offsets, sizes):
Ulf Magnussone96b6872020-01-13 12:06:44 +010071 raise Exception("Inconsistent data in devicetree.h")
David Browndbc57272017-07-17 15:38:54 -060072
Kumar Gala45b00ac2020-05-11 15:33:03 -050073 # We care about the mcuboot, image-0, and image-1 partitions.
74 if 'mcuboot' not in offsets:
David Browndbc57272017-07-17 15:38:54 -060075 raise Exception("Board partition table does not have mcuboot partition")
76
Kumar Gala45b00ac2020-05-11 15:33:03 -050077 if 'image-0' not in offsets:
David Browndbc57272017-07-17 15:38:54 -060078 raise Exception("Board partition table does not have image-0 partition")
79
Kumar Gala45b00ac2020-05-11 15:33:03 -050080 if 'image-1' not in offsets:
David Browndbc57272017-07-17 15:38:54 -060081 raise Exception("Board partition table does not have image-1 partition")
82
83 self.offsets = offsets
84 self.sizes = sizes
85
86 def add_image(self, source, partition):
87 with open(self.output, 'ab') as ofd:
88 pos = ofd.tell()
89 print("partition {}, pos={}, offset={}".format(partition, pos, self.offsets[partition]))
90 if pos > self.offsets[partition]:
91 raise Exception("Partitions not in order, unsupported")
92 if pos < self.offsets[partition]:
93 buf = b'\xFF' * (self.offsets[partition] - pos)
94 ofd.write(buf)
95 with open(source, 'rb') as rfd:
96 ibuf = rfd.read()
97 if len(ibuf) > self.sizes[partition]:
98 raise Exception("Image {} is too large for partition".format(source))
99 ofd.write(ibuf)
100
101def main():
102 parser = argparse.ArgumentParser()
103
104 parser.add_argument('-b', '--bootdir', required=True,
105 help='Directory of built bootloader')
106 parser.add_argument('-p', '--primary', required=True,
107 help='Signed image file for primary image')
108 parser.add_argument('-s', '--secondary',
109 help='Signed image file for secondary image')
110 parser.add_argument('-o', '--output', required=True,
111 help='Filename to write full image to')
112
113 args = parser.parse_args()
David Browndbc57272017-07-17 15:38:54 -0600114
Kumar Gala45b00ac2020-05-11 15:33:03 -0500115 # Extract board name from path
116 board = os.path.split(os.path.split(args.bootdir)[0])[1]
117
118 dts_path = os.path.join(args.bootdir, "zephyr", board + ".dts.pre.tmp")
119
120 edt = edtlib.EDT(dts_path, [os.path.join(ZEPHYR_BASE, "dts", "bindings")],
121 warn_reg_unit_address_mismatch=False)
122
123 output = Assembly(args.output, args.bootdir, edt)
124
125 output.add_image(os.path.join(args.bootdir, 'zephyr', 'zephyr.bin'), 'mcuboot')
126 output.add_image(args.primary, "image-0")
David Browndbc57272017-07-17 15:38:54 -0600127 if args.secondary is not None:
Kumar Gala45b00ac2020-05-11 15:33:03 -0500128 output.add_image(args.secondary, "image-1")
David Browndbc57272017-07-17 15:38:54 -0600129
130if __name__ == '__main__':
131 main()