blob: 406fb8aa0f90aee962acb93b61726b63f87e1141 [file] [log] [blame]
Jerome Forissierd0c63612017-07-25 18:17:11 +02001#!/usr/bin/env python
Jerome Forissier1bb92982017-12-15 14:27:02 +01002# SPDX-License-Identifier: BSD-2-Clause
Jerome Forissierd0c63612017-07-25 18:17:11 +02003#
4# Copyright (c) 2017, Linaro Limited
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# 1. Redistributions of source code must retain the above copyright notice,
11# this list of conditions and the following disclaimer.
12#
13# 2. Redistributions in binary form must reproduce the above copyright notice,
14# this list of conditions and the following disclaimer in the documentation
15# and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29
30import argparse
31import array
32import os
33import re
34import uuid
Jerome Forissier509a9802017-08-01 10:14:57 +020035import zlib
Jerome Forissierd0c63612017-07-25 18:17:11 +020036
37def get_args():
38
39 parser = argparse.ArgumentParser(description='Converts a Trusted '
40 'Application ELF file into a C source file, ready for '
41 'inclusion in the TEE binary as an "early TA".')
42
43 parser.add_argument('--out', required=True,
44 help='Name of the output C file')
45
46 parser.add_argument('--ta', required=True,
47 help='Path to the TA binary. File name has to be: <uuid>.* '
48 'such as: 8aaaf200-2450-11e4-abe2-0002a5d5c51b.stripped.elf')
49
Jerome Forissier509a9802017-08-01 10:14:57 +020050 parser.add_argument('--compress', dest="compress",
51 action="store_true", help='Compress the TA using the DEFLATE '
52 'algorithm')
53
Jerome Forissierd0c63612017-07-25 18:17:11 +020054 return parser.parse_args()
55
56def main():
57
58 args = get_args();
59
60 ta_uuid = uuid.UUID(re.sub('\..*', '', os.path.basename(args.ta)))
61
Jerome Forissier509a9802017-08-01 10:14:57 +020062 with open(args.ta, 'rb') as ta:
63 bytes = ta.read()
64 uncompressed_size = len(bytes)
65 if args.compress:
66 bytes = zlib.compress(bytes)
67 size = len(bytes)
68
Jerome Forissierd0c63612017-07-25 18:17:11 +020069 f = open(args.out, 'w')
70 f.write('/* Generated from ' + args.ta + ' by ' +
71 os.path.basename(__file__) + ' */\n\n')
72 f.write('#include <compiler.h>\n');
73 f.write('#include <kernel/early_ta.h>\n\n');
74 f.write('__extension__ const struct early_ta __early_ta_' +
75 ta_uuid.hex +
76 '\n__early_ta __aligned(__alignof__(struct early_ta)) = {\n')
77 f.write('\t.uuid = {\n')
78 f.write('\t\t.timeLow = 0x{:08x},\n'.format(ta_uuid.time_low))
79 f.write('\t\t.timeMid = 0x{:04x},\n'.format(ta_uuid.time_mid))
80 f.write('\t\t.timeHiAndVersion = ' +
81 '0x{:04x},\n'.format(ta_uuid.time_hi_version))
82 f.write('\t\t.clockSeqAndNode = {\n')
83 csn = '{0:02x}{1:02x}{2:012x}'.format(ta_uuid.clock_seq_hi_variant,
84 ta_uuid.clock_seq_low, ta_uuid.node)
85 f.write('\t\t\t')
86 f.write(', '.join('0x' + csn[i:i+2] for i in range(0, len(csn), 2)))
87 f.write('\n\t\t},\n\t},\n')
Jerome Forissier509a9802017-08-01 10:14:57 +020088 f.write('\t.size = {:d},\n'.format(size))
89 if args.compress:
90 f.write('\t.uncompressed_size = '
91 '{:d},\n'.format(uncompressed_size))
Jerome Forissierd0c63612017-07-25 18:17:11 +020092 f.write('\t.ta = {\n')
93 i = 0
Jerome Forissier509a9802017-08-01 10:14:57 +020094 while i < size:
95 if i % 8 == 0:
96 f.write('\t\t');
97 f.write('0x' + '{:02x}'.format(ord(bytes[i])) + ',')
98 i = i + 1
99 if i % 8 == 0 or i == size:
100 f.write('\n')
101 else:
102 f.write(' ')
Jerome Forissierd0c63612017-07-25 18:17:11 +0200103 f.write('\t},\n')
104 f.write('};\n');
105 f.close()
106
107if __name__ == "__main__":
108 main()