blob: a5979d7fe222a092527659c2da413f3e6d553510 [file] [log] [blame]
Jens Wiklander8f7de3f2014-12-04 08:04:01 +01001#!/usr/bin/env python
2#
3# Copyright (c) 2014, Linaro Limited
4# 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
36
37def write_header(outf, init_size, args, paged_size):
38 magic = 0x4554504f # 'OPTE'
39 version = 1;
40 outf.write(struct.pack('<IBBHIIIII', \
41 magic, version, args.arch, args.flags, init_size, \
42 args.init_load_addr_hi, args.init_load_addr_lo, \
43 args.init_mem_usage, paged_size))
44
45
46def append_to(outf, start_offs, in_fname, max_bytes=0xffffffff):
47 #print "Appending %s@0x%x 0x%x bytes at position 0x%x" % \
48 #( in_fname, start_offs, max_bytes, int(outf.tell()) )
49 inf = open(in_fname, 'r');
50 inf.seek(start_offs)
51 while True :
52 nbytes = min(16 * 1024, max_bytes)
53 if nbytes == 0 :
54 break
55 #print "Reading %s %d bytes" % (in_fname, nbytes)
56 buf = inf.read(nbytes)
57 if not buf :
58 break
59 outf.write(buf)
60 max_bytes -= len(buf)
61 inf.close()
62
63def append_hashes(outf, in_fname):
64 page_size = 4 * 1024
65
66 inf = open(in_fname, 'r')
67 while True :
68 page = inf.read(page_size)
69 if len(page) == page_size :
70 #print "Writing hash at position 0x%x" % \
71 #int(outf.tell())
72 outf.write(hashlib.sha256(page).digest())
73 elif len(page) == 0 :
74 break
75 else :
76 print "Error: short read, got " + repr(len(page))
77 sys.exit(1)
78
79 inf.close()
80
81def int_parse(str):
82 return int(str, 0)
83
84def get_args():
85 parser = argparse.ArgumentParser()
86 parser.add_argument('--arch', required=True, \
87 type=int, choices=[0, 1], \
88 help='Architecture: ARM32 = 0, ARM64 = 1')
89
90 parser.add_argument('--flags', \
91 type=int, default=0, \
92 help='Flags, currently none defined')
93
94 parser.add_argument('--init_size', \
95 required=True, type=int_parse, \
96 help='Size of initialization part of binary')
97
98 parser.add_argument('--init_load_addr_hi', \
99 type=int_parse, default=0, \
100 help='Upper 32 bits of load address of binary')
101
102 parser.add_argument('--init_load_addr_lo', \
103 required=True, type=int_parse, \
104 help='Lower 32 bits of load address of binary')
105
106 parser.add_argument('--init_mem_usage', \
107 required=True, type=int_parse, \
108 help='Total amount of used memory when initializing');
109
110 parser.add_argument('--tee_pager_bin', \
111 required=True, \
112 help='The input tee_pager.bin')
113
Jerome Forissier30ca3222015-03-18 15:01:52 +0100114 parser.add_argument('--tee_pageable_bin', \
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100115 required=True, \
Jerome Forissier30ca3222015-03-18 15:01:52 +0100116 help='The input tee_pageable.bin')
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100117
118 parser.add_argument('--out', \
119 required=True, type=argparse.FileType('w'), \
120 help='The output tee.bin')
121
122 return parser.parse_args();
123
124def main():
125 args = get_args()
Jerome Forissier30ca3222015-03-18 15:01:52 +0100126 init_bin_size = args.init_size
127 tee_pager_fname = args.tee_pager_bin
128 tee_pageable_fname = args.tee_pageable_bin
129 outf = args.out
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100130
131 write_header(outf, 0, args, 0)
132 header_size = outf.tell();
133 append_to(outf, 0, tee_pager_fname)
Jerome Forissier30ca3222015-03-18 15:01:52 +0100134 append_to(outf, 0, tee_pageable_fname, init_bin_size)
135 append_hashes(outf, tee_pageable_fname)
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100136 init_size = outf.tell() - header_size;
Jerome Forissier30ca3222015-03-18 15:01:52 +0100137 append_to(outf, init_bin_size, tee_pageable_fname)
Jens Wiklander8f7de3f2014-12-04 08:04:01 +0100138 paged_size = outf.tell() - init_size - header_size;
139
140 outf.seek(0)
141 #print "init_size 0x%x" % init_size
142 write_header(outf, init_size, args, paged_size)
143
144 outf.close()
145
146if __name__ == "__main__":
147 main()