blob: 13a38c7cd1b17d64fedf627dc85f4766c90ecf32 [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
34
35def get_args():
36
37 parser = argparse.ArgumentParser(description='Converts a Trusted '
38 'Application ELF file into a C source file, ready for '
39 'inclusion in the TEE binary as an "early TA".')
40
41 parser.add_argument('--out', required=True,
42 help='Name of the output C file')
43
44 parser.add_argument('--ta', required=True,
45 help='Path to the TA binary. File name has to be: <uuid>.* '
46 'such as: 8aaaf200-2450-11e4-abe2-0002a5d5c51b.stripped.elf')
47
48 return parser.parse_args()
49
50def main():
51
52 args = get_args();
53
54 ta_uuid = uuid.UUID(re.sub('\..*', '', os.path.basename(args.ta)))
55
56 f = open(args.out, 'w')
57 f.write('/* Generated from ' + args.ta + ' by ' +
58 os.path.basename(__file__) + ' */\n\n')
59 f.write('#include <compiler.h>\n');
60 f.write('#include <kernel/early_ta.h>\n\n');
61 f.write('__extension__ const struct early_ta __early_ta_' +
62 ta_uuid.hex +
63 '\n__early_ta __aligned(__alignof__(struct early_ta)) = {\n')
64 f.write('\t.uuid = {\n')
65 f.write('\t\t.timeLow = 0x{:08x},\n'.format(ta_uuid.time_low))
66 f.write('\t\t.timeMid = 0x{:04x},\n'.format(ta_uuid.time_mid))
67 f.write('\t\t.timeHiAndVersion = ' +
68 '0x{:04x},\n'.format(ta_uuid.time_hi_version))
69 f.write('\t\t.clockSeqAndNode = {\n')
70 csn = '{0:02x}{1:02x}{2:012x}'.format(ta_uuid.clock_seq_hi_variant,
71 ta_uuid.clock_seq_low, ta_uuid.node)
72 f.write('\t\t\t')
73 f.write(', '.join('0x' + csn[i:i+2] for i in range(0, len(csn), 2)))
74 f.write('\n\t\t},\n\t},\n')
75 f.write('\t.size = {:d},'.format(os.path.getsize(args.ta)))
76 f.write('\t.ta = {\n')
77 i = 0
78 with open(args.ta, 'rb') as ta:
79 byte = ta.read(1)
80 while byte:
81 if i % 8 == 0:
82 f.write('\t\t');
83 f.write('0x' + '{:02x}'.format(ord(byte)) + ',')
84 i = i + 1
85 byte = ta.read(1)
86 if i % 8 == 0 or not byte:
87 f.write('\n')
88 else:
89 f.write(' ')
90 f.write('\t},\n')
91 f.write('};\n');
92 f.close()
93
94if __name__ == "__main__":
95 main()