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 |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 28 | |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame^] | 29 | offset_re = re.compile(r"^\s*RE_([0-9A-Z_]+)_IMAGE_OFFSET\s*=\s*(((0x)?[0-9a-fA-F]+)\s*([\+\-]\s*((0x)?[0-9a-fA-F]+)\s*)*)") |
| 30 | size_re = re.compile(r"^\s*RE_([0-9A-Z_]+)_IMAGE_MAX_SIZE\s*=\s*(((0x)?[0-9a-fA-F]+)\s*([\+\-]\s*((0x)?[0-9a-fA-F]+)\s*)*)") |
| 31 | |
| 32 | #Simple parser that takes a string and evaluates an expression from it. |
| 33 | #The expression might contain additions and subtractions amongst numbers that are |
| 34 | #written in decimal or hexadecimal form. |
| 35 | def parse_and_sum(text): |
| 36 | nums = re.findall(r'[0x\d]+|[\d]+', text) |
| 37 | for i in range(len(nums)): |
| 38 | nums[i] = int(nums[i], 0) |
| 39 | ops = re.findall(r'\+|\-', text) |
| 40 | sum = nums[0] |
| 41 | for i in range(len(ops)): |
| 42 | if ops[i] == '+': |
| 43 | sum += nums[i+1] |
| 44 | else: |
| 45 | sum -= nums[i+1] |
| 46 | return sum |
| 47 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 48 | |
| 49 | class Assembly(): |
Mate Toth-Pal | 48fc6a0 | 2018-01-24 09:50:14 +0100 | [diff] [blame] | 50 | def __init__(self, layout_path, output): |
| 51 | self.output = output |
| 52 | self.layout_path = layout_path |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 53 | self.find_slots() |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 54 | try: |
| 55 | os.unlink(output) |
| 56 | except OSError as e: |
| 57 | if e.errno != errno.ENOENT: |
| 58 | raise |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 59 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 60 | def find_slots(self): |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 61 | offsets = {} |
| 62 | sizes = {} |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 63 | |
Alexander Zilberkant | 5f44554 | 2018-10-02 18:44:06 +0300 | [diff] [blame] | 64 | if os.path.isabs(self.layout_path): |
| 65 | configFile = self.layout_path |
| 66 | else: |
| 67 | scriptsDir = os.path.dirname(os.path.abspath(__file__)) |
| 68 | configFile = os.path.join(scriptsDir, self.layout_path) |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 69 | |
| 70 | with open(configFile, 'r') as fd: |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 71 | for line in fd: |
| 72 | m = offset_re.match(line) |
| 73 | if m is not None: |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame^] | 74 | offsets[m.group(1)] = parse_and_sum(m.group(2)) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 75 | m = size_re.match(line) |
| 76 | if m is not None: |
Sverteczky, Marcell | 4b78a4b | 2019-06-03 14:17:10 +0200 | [diff] [blame^] | 77 | sizes[m.group(1)] = parse_and_sum(m.group(2)) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 78 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 79 | if 'SECURE' not in offsets: |
| 80 | raise Exception("Image config does not have secure partition") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 81 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 82 | if 'NON_SECURE' not in offsets: |
| 83 | raise Exception("Image config does not have non-secure partition") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 84 | |
| 85 | self.offsets = offsets |
| 86 | self.sizes = sizes |
| 87 | |
| 88 | def add_image(self, source, partition): |
| 89 | with open(self.output, 'ab') as ofd: |
Gabor Kertesz | 33e9b23 | 2018-09-12 15:38:41 +0200 | [diff] [blame] | 90 | ofd.seek(0, os.SEEK_END) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 91 | pos = ofd.tell() |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 92 | if pos > self.offsets[partition]: |
| 93 | raise Exception("Partitions not in order, unsupported") |
| 94 | if pos < self.offsets[partition]: |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 95 | ofd.write(b'\xFF' * (self.offsets[partition] - pos)) |
| 96 | statinfo = os.stat(source) |
| 97 | if statinfo.st_size > self.sizes[partition]: |
| 98 | raise Exception("Image {} is too large for partition".format(source)) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 99 | with open(source, 'rb') as rfd: |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 100 | shutil.copyfileobj(rfd, ofd, 0x10000) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 101 | |
| 102 | def main(): |
| 103 | parser = argparse.ArgumentParser() |
| 104 | |
Mate Toth-Pal | 48fc6a0 | 2018-01-24 09:50:14 +0100 | [diff] [blame] | 105 | parser.add_argument('-l', '--layout', required=True, |
| 106 | help='Location of the memory layout file') |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 107 | parser.add_argument('-s', '--secure', required=True, |
| 108 | help='Unsigned secure image') |
| 109 | parser.add_argument('-n', '--non_secure', |
| 110 | help='Unsigned non-secure image') |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 111 | parser.add_argument('-o', '--output', required=True, |
| 112 | help='Filename to write full image to') |
| 113 | |
| 114 | args = parser.parse_args() |
Mate Toth-Pal | 48fc6a0 | 2018-01-24 09:50:14 +0100 | [diff] [blame] | 115 | output = Assembly(args.layout, args.output) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 116 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 117 | output.add_image(args.secure, "SECURE") |
| 118 | output.add_image(args.non_secure, "NON_SECURE") |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 119 | |
| 120 | if __name__ == '__main__': |
| 121 | main() |