blob: 9b5f095f61f90dda379be34d9433a59532942d96 [file] [log] [blame]
Jens Wiklander8f7de3f2014-12-04 08:04:01 +01001#!/usr/bin/env python
2#
Summer Qinac3cc6c2017-04-18 13:59:03 +01003# Copyright (c) 2014-2017, Linaro Limited
Jens Wiklander8f7de3f2014-12-04 08:04:01 +01004# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8#
9# 1. Redistributions of source code must retain the above copyright notice,
10# this list of conditions and the following disclaimer.
11#
12# 2. Redistributions in binary form must reproduce the above copyright notice,
13# this list of conditions and the following disclaimer in the documentation
14# and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26# POSSIBILITY OF SUCH DAMAGE.
27#
28
29import argparse
30import sys
31import shutil
32import os
33import struct
34import hashlib
35
Jerome Forissier7a976652015-05-06 17:23:50 +020036arch_id = {'arm32': 0, 'arm64': 1}
Summer Qinac3cc6c2017-04-18 13:59:03 +010037image_id = {'pager': 0, 'paged': 1}
Jens Wiklander8f7de3f2014-12-04 08:04:01 +010038
Summer Qinac3cc6c2017-04-18 13:59:03 +010039def write_header_v1(outf, init_size, args, paged_size):
Jens Wiklander8f7de3f2014-12-04 08:04:01 +010040 magic = 0x4554504f # 'OPTE'
41 version = 1;
42 outf.write(struct.pack('<IBBHIIIII', \
Jerome Forissier7a976652015-05-06 17:23:50 +020043 magic, version, arch_id[args.arch], args.flags, init_size, \
Jens Wiklander8f7de3f2014-12-04 08:04:01 +010044 args.init_load_addr_hi, args.init_load_addr_lo, \
45 args.init_mem_usage, paged_size))
Summer Qinac3cc6c2017-04-18 13:59:03 +010046
47def write_header_v2(outf, init_size, args, paged_size):
48 magic = 0x4554504f # 'OPTE'
49 version = 2
50 nb_images = 1 if paged_size == 0 else 2
51 outf.write(struct.pack('<IBBHI', \
52 magic, version, arch_id[args.arch], args.flags, nb_images))
53 outf.write(struct.pack('<IIII', \
54 args.init_load_addr_hi, args.init_load_addr_lo, \
55 image_id['pager'], init_size))
56 if nb_images == 2:
57 outf.write(struct.pack('<IIII', \
58 0xffffffff, 0xffffffff, image_id['paged'], paged_size))
Jens Wiklander8f7de3f2014-12-04 08:04:01 +010059
60def append_to(outf, start_offs, in_fname, max_bytes=0xffffffff):
61 #print "Appending %s@0x%x 0x%x bytes at position 0x%x" % \
62 #( in_fname, start_offs, max_bytes, int(outf.tell()) )
Joakim Bech47805ee2016-07-29 17:48:39 +020063 inf = open(in_fname, 'rb');
Jens Wiklander8f7de3f2014-12-04 08:04:01 +010064 inf.seek(start_offs)
65 while True :
66 nbytes = min(16 * 1024, max_bytes)
67 if nbytes == 0 :
68 break
69 #print "Reading %s %d bytes" % (in_fname, nbytes)
70 buf = inf.read(nbytes)
71 if not buf :
72 break
73 outf.write(buf)
74 max_bytes -= len(buf)
75 inf.close()
76
77def append_hashes(outf, in_fname):
78 page_size = 4 * 1024
79
80 inf = open(in_fname, 'r')
81 while True :
82 page = inf.read(page_size)
83 if len(page) == page_size :
84 #print "Writing hash at position 0x%x" % \
85 #int(outf.tell())
86 outf.write(hashlib.sha256(page).digest())
87 elif len(page) == 0 :
88 break
89 else :
Joakim Bech47805ee2016-07-29 17:48:39 +020090 print("Error: short read, got " + repr(len(page)))
Jens Wiklander8f7de3f2014-12-04 08:04:01 +010091 sys.exit(1)
92
93 inf.close()
94
95def int_parse(str):
96 return int(str, 0)
97
98def get_args():
99 parser = argparse.ArgumentParser()
100 parser.add_argument('--arch', required=True, \
Jerome Forissier7a976652015-05-06 17:23:50 +0200101 choices=arch_id.keys(), \
102 help='Architecture')
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100103
104 parser.add_argument('--flags', \
105 type=int, default=0, \
106 help='Flags, currently none defined')
107
108 parser.add_argument('--init_size', \
109 required=True, type=int_parse, \
110 help='Size of initialization part of binary')
111
112 parser.add_argument('--init_load_addr_hi', \
113 type=int_parse, default=0, \
114 help='Upper 32 bits of load address of binary')
115
116 parser.add_argument('--init_load_addr_lo', \
117 required=True, type=int_parse, \
118 help='Lower 32 bits of load address of binary')
119
120 parser.add_argument('--init_mem_usage', \
121 required=True, type=int_parse, \
122 help='Total amount of used memory when initializing');
123
124 parser.add_argument('--tee_pager_bin', \
125 required=True, \
126 help='The input tee_pager.bin')
127
Jerome Forissier30ca3222015-03-18 15:01:52 +0100128 parser.add_argument('--tee_pageable_bin', \
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100129 required=True, \
Jerome Forissier30ca3222015-03-18 15:01:52 +0100130 help='The input tee_pageable.bin')
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100131
132 parser.add_argument('--out', \
Summer Qinac3cc6c2017-04-18 13:59:03 +0100133 required=False, type=argparse.FileType('wb'), \
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100134 help='The output tee.bin')
135
Summer Qinac3cc6c2017-04-18 13:59:03 +0100136 parser.add_argument('--out_header_v2', \
137 required=False, type=argparse.FileType('wb'), \
138 help='The output tee_header_v2.bin')
139
140 parser.add_argument('--out_pager_v2', \
141 required=False, type=argparse.FileType('wb'), \
142 help='The output tee_pager_v2.bin')
143
144 parser.add_argument('--out_pageable_v2', \
145 required=False, type=argparse.FileType('wb'), \
146 help='The output tee_pageable_v2.bin')
147
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100148 return parser.parse_args();
149
150def main():
151 args = get_args()
Jerome Forissier30ca3222015-03-18 15:01:52 +0100152 init_bin_size = args.init_size
153 tee_pager_fname = args.tee_pager_bin
154 tee_pageable_fname = args.tee_pageable_bin
Summer Qinac3cc6c2017-04-18 13:59:03 +0100155 pager_input_size = os.path.getsize(tee_pager_fname);
156 paged_input_size = os.path.getsize(tee_pageable_fname);
157 hash_size = paged_input_size / (4 * 1024) * \
158 hashlib.sha256().digest_size
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100159
Summer Qinac3cc6c2017-04-18 13:59:03 +0100160 if paged_input_size % (4 * 1024) != 0:
161 print("Error: pageable size not a multiple of 4K:" + \
162 repr(paged_input_size))
163 sys.exit(1)
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100164
Summer Qinac3cc6c2017-04-18 13:59:03 +0100165 init_size = pager_input_size + \
166 min(init_bin_size, paged_input_size) + \
167 hash_size
168 paged_size = paged_input_size - \
169 min(init_bin_size, paged_input_size)
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100170
Summer Qinac3cc6c2017-04-18 13:59:03 +0100171 if args.out is not None:
172 outf = args.out
173 write_header_v1(outf, init_size, args, paged_size)
174 append_to(outf, 0, tee_pager_fname)
175 append_to(outf, 0, tee_pageable_fname, init_bin_size)
176 append_hashes(outf, tee_pageable_fname)
177 append_to(outf, init_bin_size, tee_pageable_fname)
178 outf.close()
179
180 if args.out_header_v2 is not None:
181 outf = args.out_header_v2
182 write_header_v2(outf, init_size, args, paged_size)
183 outf.close()
184
185 if args.out_pager_v2 is not None:
186 outf = args.out_pager_v2
187 append_to(outf, 0, tee_pager_fname)
188 append_to(outf, 0, tee_pageable_fname, init_bin_size)
189 append_hashes(outf, tee_pageable_fname)
190 outf.close()
191
192 if args.out_pageable_v2 is not None:
193 outf = args.out_pageable_v2
194 append_to(outf, init_bin_size, tee_pageable_fname)
195 outf.close()
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100196
197if __name__ == "__main__":
198 main()