Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright 2017 Linaro Limited |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame] | 4 | # Copyright (c) 2017-2019, 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 |
Sverteczky, Marcell | 7d069e8 | 2019-07-04 18:17:33 +0200 | [diff] [blame^] | 28 | import macro_parser |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 29 | |
Sverteczky, Marcell | 7d069e8 | 2019-07-04 18:17:33 +0200 | [diff] [blame^] | 30 | offset_re = re.compile(r"^\s*RE_([0-9A-Z_]+)_IMAGE_OFFSET\s*=\s*(.*)") |
| 31 | size_re = re.compile(r"^\s*RE_([0-9A-Z_]+)_IMAGE_MAX_SIZE\s*=\s*(.*)") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 32 | |
| 33 | class Assembly(): |
Mate Toth-Pal | 48fc6a0 | 2018-01-24 09:50:14 +0100 | [diff] [blame] | 34 | def __init__(self, layout_path, output): |
| 35 | self.output = output |
| 36 | self.layout_path = layout_path |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 37 | self.find_slots() |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 38 | try: |
| 39 | os.unlink(output) |
| 40 | except OSError as e: |
| 41 | if e.errno != errno.ENOENT: |
| 42 | raise |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 43 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 44 | def find_slots(self): |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 45 | offsets = {} |
| 46 | sizes = {} |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 47 | |
Sverteczky, Marcell | 7d069e8 | 2019-07-04 18:17:33 +0200 | [diff] [blame^] | 48 | offsets = macro_parser.evaluate_macro(self.layout_path, offset_re, 1, 2) |
| 49 | sizes = macro_parser.evaluate_macro(self.layout_path, size_re, 1, 2) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 50 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 51 | if 'SECURE' not in offsets: |
| 52 | raise Exception("Image config does not have secure partition") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 53 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 54 | if 'NON_SECURE' not in offsets: |
| 55 | raise Exception("Image config does not have non-secure partition") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 56 | |
| 57 | self.offsets = offsets |
| 58 | self.sizes = sizes |
| 59 | |
| 60 | def add_image(self, source, partition): |
| 61 | with open(self.output, 'ab') as ofd: |
Gabor Kertesz | 33e9b23 | 2018-09-12 15:38:41 +0200 | [diff] [blame] | 62 | ofd.seek(0, os.SEEK_END) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 63 | pos = ofd.tell() |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 64 | if pos > self.offsets[partition]: |
| 65 | raise Exception("Partitions not in order, unsupported") |
| 66 | if pos < self.offsets[partition]: |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 67 | ofd.write(b'\xFF' * (self.offsets[partition] - pos)) |
| 68 | statinfo = os.stat(source) |
| 69 | if statinfo.st_size > self.sizes[partition]: |
| 70 | raise Exception("Image {} is too large for partition".format(source)) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 71 | with open(source, 'rb') as rfd: |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 72 | shutil.copyfileobj(rfd, ofd, 0x10000) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 73 | |
| 74 | def main(): |
| 75 | parser = argparse.ArgumentParser() |
| 76 | |
Mate Toth-Pal | 48fc6a0 | 2018-01-24 09:50:14 +0100 | [diff] [blame] | 77 | parser.add_argument('-l', '--layout', required=True, |
Sverteczky, Marcell | 7d069e8 | 2019-07-04 18:17:33 +0200 | [diff] [blame^] | 78 | help='Location of the file that contains preprocessed macros') |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 79 | parser.add_argument('-s', '--secure', required=True, |
| 80 | help='Unsigned secure image') |
| 81 | parser.add_argument('-n', '--non_secure', |
| 82 | help='Unsigned non-secure image') |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 83 | parser.add_argument('-o', '--output', required=True, |
| 84 | help='Filename to write full image to') |
| 85 | |
| 86 | args = parser.parse_args() |
Mate Toth-Pal | 48fc6a0 | 2018-01-24 09:50:14 +0100 | [diff] [blame] | 87 | output = Assembly(args.layout, args.output) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 88 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 89 | output.add_image(args.secure, "SECURE") |
| 90 | output.add_image(args.non_secure, "NON_SECURE") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 91 | |
| 92 | if __name__ == '__main__': |
| 93 | main() |