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