Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright 2017 Linaro Limited |
Marc Moreno Berengue | a1f296f | 2018-01-25 15:21:22 +0000 | [diff] [blame] | 4 | # Copyright (c) 2017-2018, Arm Limited. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 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 |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 26 | import os |
| 27 | import shutil |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 28 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 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]+)") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 31 | |
| 32 | class Assembly(): |
Mate Toth-Pal | 48fc6a0 | 2018-01-24 09:50:14 +0100 | [diff] [blame] | 33 | def __init__(self, layout_path, output): |
| 34 | self.output = output |
| 35 | self.layout_path = layout_path |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 36 | self.find_slots() |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 37 | try: |
| 38 | os.unlink(output) |
| 39 | except OSError as e: |
| 40 | if e.errno != errno.ENOENT: |
| 41 | raise |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 42 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 43 | def find_slots(self): |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 44 | offsets = {} |
| 45 | sizes = {} |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 46 | |
| 47 | scriptsDir = os.path.dirname(os.path.abspath(__file__)) |
Mate Toth-Pal | 48fc6a0 | 2018-01-24 09:50:14 +0100 | [diff] [blame] | 48 | configFile = os.path.join(scriptsDir, self.layout_path) |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 49 | |
| 50 | with open(configFile, 'r') as fd: |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 51 | for line in fd: |
| 52 | m = offset_re.match(line) |
| 53 | if m is not None: |
| 54 | offsets[m.group(1)] = int(m.group(2), 0) |
| 55 | m = size_re.match(line) |
| 56 | if m is not None: |
| 57 | sizes[m.group(1)] = int(m.group(2), 0) |
| 58 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 59 | if 'SECURE' not in offsets: |
| 60 | raise Exception("Image config does not have secure partition") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 61 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 62 | if 'NON_SECURE' not in offsets: |
| 63 | raise Exception("Image config does not have non-secure partition") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 64 | |
| 65 | self.offsets = offsets |
| 66 | self.sizes = sizes |
| 67 | |
| 68 | def add_image(self, source, partition): |
| 69 | with open(self.output, 'ab') as ofd: |
Gabor Kertesz | 33e9b23 | 2018-09-12 15:38:41 +0200 | [diff] [blame^] | 70 | ofd.seek(0, os.SEEK_END) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 71 | pos = ofd.tell() |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 72 | if pos > self.offsets[partition]: |
| 73 | raise Exception("Partitions not in order, unsupported") |
| 74 | if pos < self.offsets[partition]: |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 75 | ofd.write(b'\xFF' * (self.offsets[partition] - pos)) |
| 76 | statinfo = os.stat(source) |
| 77 | if statinfo.st_size > self.sizes[partition]: |
| 78 | raise Exception("Image {} is too large for partition".format(source)) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 79 | with open(source, 'rb') as rfd: |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 80 | shutil.copyfileobj(rfd, ofd, 0x10000) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 81 | |
| 82 | def main(): |
| 83 | parser = argparse.ArgumentParser() |
| 84 | |
Mate Toth-Pal | 48fc6a0 | 2018-01-24 09:50:14 +0100 | [diff] [blame] | 85 | parser.add_argument('-l', '--layout', required=True, |
| 86 | help='Location of the memory layout file') |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 87 | parser.add_argument('-s', '--secure', required=True, |
| 88 | help='Unsigned secure image') |
| 89 | parser.add_argument('-n', '--non_secure', |
| 90 | help='Unsigned non-secure image') |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 91 | parser.add_argument('-o', '--output', required=True, |
| 92 | help='Filename to write full image to') |
| 93 | |
| 94 | args = parser.parse_args() |
Mate Toth-Pal | 48fc6a0 | 2018-01-24 09:50:14 +0100 | [diff] [blame] | 95 | output = Assembly(args.layout, args.output) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 96 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 97 | |
| 98 | output.add_image(args.secure, "SECURE") |
| 99 | output.add_image(args.non_secure, "NON_SECURE") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 100 | |
| 101 | if __name__ == '__main__': |
| 102 | main() |