David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
David Brown | b730e24 | 2017-12-20 11:10:55 -0700 | [diff] [blame] | 2 | # |
| 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 Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 16 | |
| 17 | """ |
| 18 | Assemble multiple images into a single image that can be flashed on the device. |
| 19 | """ |
| 20 | |
| 21 | import argparse |
David Brown | 2cf522c | 2017-07-20 12:15:31 -0600 | [diff] [blame] | 22 | import errno |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 23 | import io |
| 24 | import re |
| 25 | import os.path |
Kumar Gala | 45b00ac | 2020-05-11 15:33:03 -0500 | [diff] [blame^] | 26 | import sys |
| 27 | |
| 28 | ZEPHYR_BASE = os.getenv("ZEPHYR_BASE") |
| 29 | if not ZEPHYR_BASE: |
| 30 | sys.exit("$ZEPHYR_BASE environment variable undefined") |
| 31 | |
| 32 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts", "dts")) |
| 33 | import edtlib |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 34 | |
| 35 | def 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 Utzig | 539d766 | 2019-09-05 10:57:00 -0300 | [diff] [blame] | 45 | offset_re = re.compile(r"^#define DT_FLASH_AREA_([0-9A-Z_]+)_OFFSET(_0)?\s+(0x[0-9a-fA-F]+|[0-9]+)$") |
| 46 | size_re = re.compile(r"^#define DT_FLASH_AREA_([0-9A-Z_]+)_SIZE(_0)?\s+(0x[0-9a-fA-F]+|[0-9]+)$") |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 47 | |
| 48 | class Assembly(): |
Kumar Gala | 45b00ac | 2020-05-11 15:33:03 -0500 | [diff] [blame^] | 49 | def __init__(self, output, bootdir, edt): |
| 50 | self.find_slots(edt) |
David Brown | 2cf522c | 2017-07-20 12:15:31 -0600 | [diff] [blame] | 51 | try: |
| 52 | os.unlink(output) |
| 53 | except OSError as e: |
| 54 | if e.errno != errno.ENOENT: |
| 55 | raise |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 56 | self.output = output |
| 57 | |
Kumar Gala | 45b00ac | 2020-05-11 15:33:03 -0500 | [diff] [blame^] | 58 | def find_slots(self, edt): |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 59 | offsets = {} |
| 60 | sizes = {} |
Kumar Gala | 45b00ac | 2020-05-11 15:33:03 -0500 | [diff] [blame^] | 61 | |
| 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 Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 69 | |
| 70 | if not same_keys(offsets, sizes): |
Ulf Magnusson | e96b687 | 2020-01-13 12:06:44 +0100 | [diff] [blame] | 71 | raise Exception("Inconsistent data in devicetree.h") |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 72 | |
Kumar Gala | 45b00ac | 2020-05-11 15:33:03 -0500 | [diff] [blame^] | 73 | # We care about the mcuboot, image-0, and image-1 partitions. |
| 74 | if 'mcuboot' not in offsets: |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 75 | raise Exception("Board partition table does not have mcuboot partition") |
| 76 | |
Kumar Gala | 45b00ac | 2020-05-11 15:33:03 -0500 | [diff] [blame^] | 77 | if 'image-0' not in offsets: |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 78 | raise Exception("Board partition table does not have image-0 partition") |
| 79 | |
Kumar Gala | 45b00ac | 2020-05-11 15:33:03 -0500 | [diff] [blame^] | 80 | if 'image-1' not in offsets: |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 81 | 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 | |
| 101 | def 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 Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 114 | |
Kumar Gala | 45b00ac | 2020-05-11 15:33:03 -0500 | [diff] [blame^] | 115 | # 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 Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 127 | if args.secondary is not None: |
Kumar Gala | 45b00ac | 2020-05-11 15:33:03 -0500 | [diff] [blame^] | 128 | output.add_image(args.secondary, "image-1") |
David Brown | dbc5727 | 2017-07-17 15:38:54 -0600 | [diff] [blame] | 129 | |
| 130 | if __name__ == '__main__': |
| 131 | main() |