yang.li | 5459fc6 | 2022-10-24 17:31:15 +0800 | [diff] [blame^] | 1 | #! /usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright 2017 Linaro Limited |
| 4 | # Copyright (c) 2017-2018, Arm Limited. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | """ |
| 19 | Assemble multiple images into a single image that can be flashed on the device. |
| 20 | """ |
| 21 | |
| 22 | import argparse |
| 23 | import errno |
| 24 | import io |
| 25 | import re |
| 26 | import os |
| 27 | import shutil |
| 28 | |
| 29 | offset_re = re.compile(r"^#define ([0-9A-Z_]+)_IMAGE_OFFSET\s+((0x)?[0-9a-fA-F]+)") |
| 30 | size_re = re.compile(r"^#define ([0-9A-Z_]+)_IMAGE_MAX_SIZE\s+((0x)?[0-9a-fA-F]+)") |
| 31 | |
| 32 | class Assembly(): |
| 33 | def __init__(self, layout_path, output): |
| 34 | self.output = output |
| 35 | self.layout_path = layout_path |
| 36 | self.find_slots() |
| 37 | try: |
| 38 | os.unlink(output) |
| 39 | except OSError as e: |
| 40 | if e.errno != errno.ENOENT: |
| 41 | raise |
| 42 | |
| 43 | def find_slots(self): |
| 44 | offsets = {} |
| 45 | sizes = {} |
| 46 | |
| 47 | if os.path.isabs(self.layout_path): |
| 48 | configFile = self.layout_path |
| 49 | else: |
| 50 | scriptsDir = os.path.dirname(os.path.abspath(__file__)) |
| 51 | configFile = os.path.join(scriptsDir, self.layout_path) |
| 52 | |
| 53 | with open(configFile, 'r') as fd: |
| 54 | for line in fd: |
| 55 | m = offset_re.match(line) |
| 56 | if m is not None: |
| 57 | offsets[m.group(1)] = int(m.group(2), 0) |
| 58 | m = size_re.match(line) |
| 59 | if m is not None: |
| 60 | sizes[m.group(1)] = int(m.group(2), 0) |
| 61 | |
| 62 | if 'SECURE' not in offsets: |
| 63 | raise Exception("Image config does not have secure partition") |
| 64 | |
| 65 | if 'NON_SECURE' not in offsets: |
| 66 | raise Exception("Image config does not have non-secure partition") |
| 67 | |
| 68 | self.offsets = offsets |
| 69 | self.sizes = sizes |
| 70 | |
| 71 | def add_image(self, source, partition): |
| 72 | with open(self.output, 'ab') as ofd: |
| 73 | ofd.seek(0, os.SEEK_END) |
| 74 | pos = ofd.tell() |
| 75 | if pos > self.offsets[partition]: |
| 76 | raise Exception("Partitions not in order, unsupported") |
| 77 | if pos < self.offsets[partition]: |
| 78 | ofd.write(b'\xFF' * (self.offsets[partition] - pos)) |
| 79 | statinfo = os.stat(source) |
| 80 | if statinfo.st_size > self.sizes[partition]: |
| 81 | raise Exception("Image {} is too large for partition".format(source)) |
| 82 | with open(source, 'rb') as rfd: |
| 83 | shutil.copyfileobj(rfd, ofd, 0x10000) |
| 84 | |
| 85 | def main(): |
| 86 | parser = argparse.ArgumentParser() |
| 87 | |
| 88 | parser.add_argument('-l', '--layout', required=True, |
| 89 | help='Location of the memory layout file') |
| 90 | parser.add_argument('-s', '--secure', required=True, |
| 91 | help='Unsigned secure image') |
| 92 | parser.add_argument('-n', '--non_secure', |
| 93 | help='Unsigned non-secure image') |
| 94 | parser.add_argument('-o', '--output', required=True, |
| 95 | help='Filename to write full image to') |
| 96 | |
| 97 | args = parser.parse_args() |
| 98 | output = Assembly(args.layout, args.output) |
| 99 | |
| 100 | |
| 101 | output.add_image(args.secure, "SECURE") |
| 102 | output.add_image(args.non_secure, "NON_SECURE") |
| 103 | |
| 104 | if __name__ == '__main__': |
| 105 | main() |