summaryrefslogtreecommitdiff
path: root/scripts/gen_hashed_bin.py
blob: e39cc1330c60ffd8e1a4104c90070601de502fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
#
# Copyright (c) 2014, Linaro Limited
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

import argparse
import sys
import shutil
import os
import struct
import hashlib

arch_id = {'arm32': 0, 'arm64': 1}

def write_header(outf, init_size, args, paged_size):
	magic = 0x4554504f # 'OPTE'
	version = 1;
	outf.write(struct.pack('<IBBHIIIII', \
		magic, version, arch_id[args.arch], args.flags, init_size, \
		args.init_load_addr_hi, args.init_load_addr_lo, \
		args.init_mem_usage, paged_size))
	

def append_to(outf, start_offs, in_fname, max_bytes=0xffffffff):
	#print "Appending %s@0x%x 0x%x bytes at position 0x%x" % \
		#( in_fname, start_offs, max_bytes, int(outf.tell()) )
	inf = open(in_fname, 'rb');
	inf.seek(start_offs)
	while True :
		nbytes = min(16 * 1024, max_bytes)
		if nbytes == 0 :
			break
		#print "Reading %s %d bytes" % (in_fname, nbytes)
		buf = inf.read(nbytes)
		if not buf :
			break
		outf.write(buf)
		max_bytes -= len(buf)
	inf.close()

def append_hashes(outf, in_fname):
	page_size = 4 * 1024

	inf = open(in_fname, 'r')
	while True :
		page = inf.read(page_size)
		if len(page) == page_size :
			#print "Writing hash at position 0x%x" % \
				#int(outf.tell())
			outf.write(hashlib.sha256(page).digest())
		elif len(page) == 0 :
			break
		else :
			print("Error: short read, got " + repr(len(page)))
			sys.exit(1)

	inf.close()

def int_parse(str):
	return int(str, 0)

def get_args():
	parser = argparse.ArgumentParser()
	parser.add_argument('--arch', required=True, \
		choices=arch_id.keys(), \
		help='Architecture')

	parser.add_argument('--flags', \
		type=int, default=0, \
		help='Flags, currently none defined')

	parser.add_argument('--init_size', \
		required=True, type=int_parse, \
		help='Size of initialization part of binary')

	parser.add_argument('--init_load_addr_hi', \
		type=int_parse, default=0, \
		help='Upper 32 bits of load address of binary')

	parser.add_argument('--init_load_addr_lo', \
		required=True, type=int_parse, \
		help='Lower 32 bits of load address of binary')

	parser.add_argument('--init_mem_usage', \
		required=True, type=int_parse, \
		help='Total amount of used memory when initializing');

	parser.add_argument('--tee_pager_bin', \
		required=True, \
		help='The input tee_pager.bin')

	parser.add_argument('--tee_pageable_bin', \
		required=True, \
		help='The input tee_pageable.bin')

	parser.add_argument('--out', \
		required=True, type=argparse.FileType('wb'), \
		help='The output tee.bin')

	return parser.parse_args();

def main():
	args = get_args()
	init_bin_size	   = args.init_size
	tee_pager_fname	   = args.tee_pager_bin
	tee_pageable_fname = args.tee_pageable_bin
	outf		   = args.out

	write_header(outf, 0, args, 0)
	header_size = outf.tell();
	append_to(outf, 0, tee_pager_fname)
	append_to(outf, 0, tee_pageable_fname, init_bin_size)
	append_hashes(outf, tee_pageable_fname)
	init_size = outf.tell() - header_size;
	append_to(outf, init_bin_size, tee_pageable_fname)
	paged_size = outf.tell() - init_size - header_size;

	outf.seek(0)
	#print "init_size 0x%x" % init_size
	write_header(outf, init_size, args, paged_size)

	outf.close()

if __name__ == "__main__":
	main()