blob: 31f964386dd749cb4d08fbab5415fe80487739b9 [file] [log] [blame]
Jerome Forissierd0c63612017-07-25 18:17:11 +02001#!/usr/bin/env python
2#
3# Copyright (c) 2017, 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 array
31import os
32import re
33import uuid
Jerome Forissier509a9802017-08-01 10:14:57 +020034import zlib
Jerome Forissierd0c63612017-07-25 18:17:11 +020035
36def get_args():
37
38 parser = argparse.ArgumentParser(description='Converts a Trusted '
39 'Application ELF file into a C source file, ready for '
40 'inclusion in the TEE binary as an "early TA".')
41
42 parser.add_argument('--out', required=True,
43 help='Name of the output C file')
44
45 parser.add_argument('--ta', required=True,
46 help='Path to the TA binary. File name has to be: <uuid>.* '
47 'such as: 8aaaf200-2450-11e4-abe2-0002a5d5c51b.stripped.elf')
48
Jerome Forissier509a9802017-08-01 10:14:57 +020049 parser.add_argument('--compress', dest="compress",
50 action="store_true", help='Compress the TA using the DEFLATE '
51 'algorithm')
52
Jerome Forissierd0c63612017-07-25 18:17:11 +020053 return parser.parse_args()
54
55def main():
56
57 args = get_args();
58
59 ta_uuid = uuid.UUID(re.sub('\..*', '', os.path.basename(args.ta)))
60
Jerome Forissier509a9802017-08-01 10:14:57 +020061 with open(args.ta, 'rb') as ta:
62 bytes = ta.read()
63 uncompressed_size = len(bytes)
64 if args.compress:
65 bytes = zlib.compress(bytes)
66 size = len(bytes)
67
Jerome Forissierd0c63612017-07-25 18:17:11 +020068 f = open(args.out, 'w')
69 f.write('/* Generated from ' + args.ta + ' by ' +
70 os.path.basename(__file__) + ' */\n\n')
71 f.write('#include <compiler.h>\n');
72 f.write('#include <kernel/early_ta.h>\n\n');
73 f.write('__extension__ const struct early_ta __early_ta_' +
74 ta_uuid.hex +
75 '\n__early_ta __aligned(__alignof__(struct early_ta)) = {\n')
76 f.write('\t.uuid = {\n')
77 f.write('\t\t.timeLow = 0x{:08x},\n'.format(ta_uuid.time_low))
78 f.write('\t\t.timeMid = 0x{:04x},\n'.format(ta_uuid.time_mid))
79 f.write('\t\t.timeHiAndVersion = ' +
80 '0x{:04x},\n'.format(ta_uuid.time_hi_version))
81 f.write('\t\t.clockSeqAndNode = {\n')
82 csn = '{0:02x}{1:02x}{2:012x}'.format(ta_uuid.clock_seq_hi_variant,
83 ta_uuid.clock_seq_low, ta_uuid.node)
84 f.write('\t\t\t')
85 f.write(', '.join('0x' + csn[i:i+2] for i in range(0, len(csn), 2)))
86 f.write('\n\t\t},\n\t},\n')
Jerome Forissier509a9802017-08-01 10:14:57 +020087 f.write('\t.size = {:d},\n'.format(size))
88 if args.compress:
89 f.write('\t.uncompressed_size = '
90 '{:d},\n'.format(uncompressed_size))
Jerome Forissierd0c63612017-07-25 18:17:11 +020091 f.write('\t.ta = {\n')
92 i = 0
Jerome Forissier509a9802017-08-01 10:14:57 +020093 while i < size:
94 if i % 8 == 0:
95 f.write('\t\t');
96 f.write('0x' + '{:02x}'.format(ord(bytes[i])) + ',')
97 i = i + 1
98 if i % 8 == 0 or i == size:
99 f.write('\n')
100 else:
101 f.write(' ')
Jerome Forissierd0c63612017-07-25 18:17:11 +0200102 f.write('\t},\n')
103 f.write('};\n');
104 f.close()
105
106if __name__ == "__main__":
107 main()