blob: f446f90ccf759c6b226b61090b1e7a7141695ab3 [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001#! /usr/bin/env python3
2#
3# Copyright 2017 Linaro Limited
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +02004# Copyright (c) 2017-2019, Arm Limited.
Tamas Banf70ef8c2017-12-19 15:35:09 +00005#
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"""
19Assemble multiple images into a single image that can be flashed on the device.
20"""
21
22import argparse
23import errno
24import io
25import re
Tamas Ban581034a2017-12-19 19:54:37 +000026import os
27import shutil
Tamas Banf70ef8c2017-12-19 15:35:09 +000028
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020029offset_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*)*)")
30size_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.
35def 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 Banf70ef8c2017-12-19 15:35:09 +000048
49class Assembly():
Mate Toth-Pal48fc6a02018-01-24 09:50:14 +010050 def __init__(self, layout_path, output):
51 self.output = output
52 self.layout_path = layout_path
Tamas Ban581034a2017-12-19 19:54:37 +000053 self.find_slots()
Tamas Banf70ef8c2017-12-19 15:35:09 +000054 try:
55 os.unlink(output)
56 except OSError as e:
57 if e.errno != errno.ENOENT:
58 raise
Tamas Banf70ef8c2017-12-19 15:35:09 +000059
Tamas Ban581034a2017-12-19 19:54:37 +000060 def find_slots(self):
Tamas Banf70ef8c2017-12-19 15:35:09 +000061 offsets = {}
62 sizes = {}
Tamas Ban581034a2017-12-19 19:54:37 +000063
Alexander Zilberkant5f445542018-10-02 18:44:06 +030064 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 Ban581034a2017-12-19 19:54:37 +000069
70 with open(configFile, 'r') as fd:
Tamas Banf70ef8c2017-12-19 15:35:09 +000071 for line in fd:
72 m = offset_re.match(line)
73 if m is not None:
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020074 offsets[m.group(1)] = parse_and_sum(m.group(2))
Tamas Banf70ef8c2017-12-19 15:35:09 +000075 m = size_re.match(line)
76 if m is not None:
Sverteczky, Marcell4b78a4b2019-06-03 14:17:10 +020077 sizes[m.group(1)] = parse_and_sum(m.group(2))
Tamas Banf70ef8c2017-12-19 15:35:09 +000078
Tamas Ban581034a2017-12-19 19:54:37 +000079 if 'SECURE' not in offsets:
80 raise Exception("Image config does not have secure partition")
Tamas Banf70ef8c2017-12-19 15:35:09 +000081
Tamas Ban581034a2017-12-19 19:54:37 +000082 if 'NON_SECURE' not in offsets:
83 raise Exception("Image config does not have non-secure partition")
Tamas Banf70ef8c2017-12-19 15:35:09 +000084
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 Kertesz33e9b232018-09-12 15:38:41 +020090 ofd.seek(0, os.SEEK_END)
Tamas Banf70ef8c2017-12-19 15:35:09 +000091 pos = ofd.tell()
Tamas Banf70ef8c2017-12-19 15:35:09 +000092 if pos > self.offsets[partition]:
93 raise Exception("Partitions not in order, unsupported")
94 if pos < self.offsets[partition]:
Tamas Ban581034a2017-12-19 19:54:37 +000095 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 Banf70ef8c2017-12-19 15:35:09 +000099 with open(source, 'rb') as rfd:
Tamas Ban581034a2017-12-19 19:54:37 +0000100 shutil.copyfileobj(rfd, ofd, 0x10000)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000101
102def main():
103 parser = argparse.ArgumentParser()
104
Mate Toth-Pal48fc6a02018-01-24 09:50:14 +0100105 parser.add_argument('-l', '--layout', required=True,
106 help='Location of the memory layout file')
Tamas Ban581034a2017-12-19 19:54:37 +0000107 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 Banf70ef8c2017-12-19 15:35:09 +0000111 parser.add_argument('-o', '--output', required=True,
112 help='Filename to write full image to')
113
114 args = parser.parse_args()
Mate Toth-Pal48fc6a02018-01-24 09:50:14 +0100115 output = Assembly(args.layout, args.output)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000116
Tamas Ban581034a2017-12-19 19:54:37 +0000117 output.add_image(args.secure, "SECURE")
118 output.add_image(args.non_secure, "NON_SECURE")
Tamas Banf70ef8c2017-12-19 15:35:09 +0000119
120if __name__ == '__main__':
121 main()