blob: 15239644ed705bf43c56dcbe85dfd27c9a3520d1 [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001#! /usr/bin/env python3
2#
3# Copyright 2017 Linaro Limited
Tamas Ban581034a2017-12-19 19:54:37 +00004# Copyright (c) 2017, 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
Tamas Ban581034a2017-12-19 19:54:37 +000029offset_re = re.compile(r"^#define ([0-9A-Z_]+)_IMAGE_OFFSET\s+((0x)?[0-9a-fA-F]+)")
30size_re = re.compile(r"^#define ([0-9A-Z_]+)_IMAGE_MAX_SIZE\s+((0x)?[0-9a-fA-F]+)")
Tamas Banf70ef8c2017-12-19 15:35:09 +000031
32class Assembly():
Tamas Ban581034a2017-12-19 19:54:37 +000033 def __init__(self, output):
34 self.find_slots()
Tamas Banf70ef8c2017-12-19 15:35:09 +000035 try:
36 os.unlink(output)
37 except OSError as e:
38 if e.errno != errno.ENOENT:
39 raise
40 self.output = output
41
Tamas Ban581034a2017-12-19 19:54:37 +000042 def find_slots(self):
Tamas Banf70ef8c2017-12-19 15:35:09 +000043 offsets = {}
44 sizes = {}
Tamas Ban581034a2017-12-19 19:54:37 +000045
46 scriptsDir = os.path.dirname(os.path.abspath(__file__))
47 path = '../../../../platform/ext/target/sse_200_mps2/sse_200/partition/flash_layout.h'
48 configFile = os.path.join(scriptsDir, path)
49
50 with open(configFile, 'r') as fd:
Tamas Banf70ef8c2017-12-19 15:35:09 +000051 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 Ban581034a2017-12-19 19:54:37 +000059 if 'SECURE' not in offsets:
60 raise Exception("Image config does not have secure partition")
Tamas Banf70ef8c2017-12-19 15:35:09 +000061
Tamas Ban581034a2017-12-19 19:54:37 +000062 if 'NON_SECURE' not in offsets:
63 raise Exception("Image config does not have non-secure partition")
Tamas Banf70ef8c2017-12-19 15:35:09 +000064
65 self.offsets = offsets
66 self.sizes = sizes
67
68 def add_image(self, source, partition):
69 with open(self.output, 'ab') as ofd:
70 pos = ofd.tell()
Tamas Banf70ef8c2017-12-19 15:35:09 +000071 if pos > self.offsets[partition]:
72 raise Exception("Partitions not in order, unsupported")
73 if pos < self.offsets[partition]:
Tamas Ban581034a2017-12-19 19:54:37 +000074 ofd.write(b'\xFF' * (self.offsets[partition] - pos))
75 statinfo = os.stat(source)
76 if statinfo.st_size > self.sizes[partition]:
77 raise Exception("Image {} is too large for partition".format(source))
Tamas Banf70ef8c2017-12-19 15:35:09 +000078 with open(source, 'rb') as rfd:
Tamas Ban581034a2017-12-19 19:54:37 +000079 shutil.copyfileobj(rfd, ofd, 0x10000)
Tamas Banf70ef8c2017-12-19 15:35:09 +000080
81def main():
82 parser = argparse.ArgumentParser()
83
Tamas Ban581034a2017-12-19 19:54:37 +000084 parser.add_argument('-s', '--secure', required=True,
85 help='Unsigned secure image')
86 parser.add_argument('-n', '--non_secure',
87 help='Unsigned non-secure image')
Tamas Banf70ef8c2017-12-19 15:35:09 +000088 parser.add_argument('-o', '--output', required=True,
89 help='Filename to write full image to')
90
91 args = parser.parse_args()
Tamas Ban581034a2017-12-19 19:54:37 +000092 output = Assembly(args.output)
Tamas Banf70ef8c2017-12-19 15:35:09 +000093
Tamas Ban581034a2017-12-19 19:54:37 +000094
95 output.add_image(args.secure, "SECURE")
96 output.add_image(args.non_secure, "NON_SECURE")
Tamas Banf70ef8c2017-12-19 15:35:09 +000097
98if __name__ == '__main__':
99 main()