blob: 6b5f42c64aab42b28d8de7bd17351f320138f463 [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
David Browndbc57272017-07-17 15:38:54 -060028def same_keys(a, b):
29 """Determine if the dicts a and b have the same keys in them"""
30 for ak in a.keys():
31 if ak not in b:
32 return False
33 for bk in b.keys():
34 if bk not in a:
35 return False
36 return True
37
Fabio Utzig539d7662019-09-05 10:57:00 -030038offset_re = re.compile(r"^#define DT_FLASH_AREA_([0-9A-Z_]+)_OFFSET(_0)?\s+(0x[0-9a-fA-F]+|[0-9]+)$")
39size_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 -060040
41class Assembly():
Kumar Gala45b00ac2020-05-11 15:33:03 -050042 def __init__(self, output, bootdir, edt):
43 self.find_slots(edt)
David Brown2cf522c2017-07-20 12:15:31 -060044 try:
45 os.unlink(output)
46 except OSError as e:
47 if e.errno != errno.ENOENT:
48 raise
David Browndbc57272017-07-17 15:38:54 -060049 self.output = output
50
Kumar Gala45b00ac2020-05-11 15:33:03 -050051 def find_slots(self, edt):
David Browndbc57272017-07-17 15:38:54 -060052 offsets = {}
53 sizes = {}
Kumar Gala45b00ac2020-05-11 15:33:03 -050054
55 part_nodes = edt.compat2nodes["fixed-partitions"]
56 for node in part_nodes:
57 for child in node.children.values():
58 if "label" in child.props:
59 label = child.props["label"].val
60 offsets[label] = child.regs[0].addr
61 sizes[label] = child.regs[0].size
David Browndbc57272017-07-17 15:38:54 -060062
63 if not same_keys(offsets, sizes):
Ulf Magnussone96b6872020-01-13 12:06:44 +010064 raise Exception("Inconsistent data in devicetree.h")
David Browndbc57272017-07-17 15:38:54 -060065
Kumar Gala45b00ac2020-05-11 15:33:03 -050066 # We care about the mcuboot, image-0, and image-1 partitions.
67 if 'mcuboot' not in offsets:
David Browndbc57272017-07-17 15:38:54 -060068 raise Exception("Board partition table does not have mcuboot partition")
69
Kumar Gala45b00ac2020-05-11 15:33:03 -050070 if 'image-0' not in offsets:
David Browndbc57272017-07-17 15:38:54 -060071 raise Exception("Board partition table does not have image-0 partition")
72
Kumar Gala45b00ac2020-05-11 15:33:03 -050073 if 'image-1' not in offsets:
David Browndbc57272017-07-17 15:38:54 -060074 raise Exception("Board partition table does not have image-1 partition")
75
76 self.offsets = offsets
77 self.sizes = sizes
78
79 def add_image(self, source, partition):
80 with open(self.output, 'ab') as ofd:
81 pos = ofd.tell()
82 print("partition {}, pos={}, offset={}".format(partition, pos, self.offsets[partition]))
83 if pos > self.offsets[partition]:
84 raise Exception("Partitions not in order, unsupported")
85 if pos < self.offsets[partition]:
86 buf = b'\xFF' * (self.offsets[partition] - pos)
87 ofd.write(buf)
88 with open(source, 'rb') as rfd:
89 ibuf = rfd.read()
90 if len(ibuf) > self.sizes[partition]:
91 raise Exception("Image {} is too large for partition".format(source))
92 ofd.write(ibuf)
93
Viktor Sjölindf1e6e9c2020-07-08 12:22:08 +020094def find_board_name(bootdir):
95 suffix = ".dts.pre.tmp"
96
97 for _, _, files in os.walk(os.path.join(bootdir, "zephyr")):
98 for filename in files:
99 if filename.endswith(suffix):
100 return filename[:-len(suffix)]
101
102
David Browndbc57272017-07-17 15:38:54 -0600103def main():
104 parser = argparse.ArgumentParser()
105
106 parser.add_argument('-b', '--bootdir', required=True,
107 help='Directory of built bootloader')
108 parser.add_argument('-p', '--primary', required=True,
109 help='Signed image file for primary image')
110 parser.add_argument('-s', '--secondary',
111 help='Signed image file for secondary image')
112 parser.add_argument('-o', '--output', required=True,
113 help='Filename to write full image to')
Torsten Rasmussen33fbef52020-06-03 20:21:13 +0200114 parser.add_argument('-z', '--zephyr-base', required=True,
115 help='Zephyr base containg the Zephyr repository')
David Browndbc57272017-07-17 15:38:54 -0600116
117 args = parser.parse_args()
David Browndbc57272017-07-17 15:38:54 -0600118
Torsten Rasmussen33fbef52020-06-03 20:21:13 +0200119 sys.path.insert(0, os.path.join(args.zephyr_base, "scripts", "dts"))
120 import edtlib
121
Viktor Sjölindf1e6e9c2020-07-08 12:22:08 +0200122 board = find_board_name(args.bootdir)
Kumar Gala45b00ac2020-05-11 15:33:03 -0500123
124 dts_path = os.path.join(args.bootdir, "zephyr", board + ".dts.pre.tmp")
125
Torsten Rasmussen33fbef52020-06-03 20:21:13 +0200126 edt = edtlib.EDT(dts_path, [os.path.join(args.zephyr_base, "dts", "bindings")],
Kumar Gala45b00ac2020-05-11 15:33:03 -0500127 warn_reg_unit_address_mismatch=False)
128
129 output = Assembly(args.output, args.bootdir, edt)
130
131 output.add_image(os.path.join(args.bootdir, 'zephyr', 'zephyr.bin'), 'mcuboot')
132 output.add_image(args.primary, "image-0")
David Browndbc57272017-07-17 15:38:54 -0600133 if args.secondary is not None:
Kumar Gala45b00ac2020-05-11 15:33:03 -0500134 output.add_image(args.secondary, "image-1")
David Browndbc57272017-07-17 15:38:54 -0600135
136if __name__ == '__main__':
137 main()