blob: c715a328580748489c258e1061131e80e0388f33 [file] [log] [blame]
Pipat Methavanitpong8ca381f2019-07-30 15:22:53 +09001#!/usr/bin/env python3
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
Jerome Forissierd0c63612017-07-25 18:17:11 +02005#
Jerome Forissierd0c63612017-07-25 18:17:11 +02006
7import argparse
8import array
Maxim Uvarov8355f922020-04-09 15:19:28 +01009from elftools.elf.elffile import ELFFile
Jerome Forissierd0c63612017-07-25 18:17:11 +020010import os
11import re
Maxim Uvarov8355f922020-04-09 15:19:28 +010012import struct
Jerome Forissierd0c63612017-07-25 18:17:11 +020013import uuid
Jerome Forissier509a9802017-08-01 10:14:57 +020014import zlib
Jerome Forissierd0c63612017-07-25 18:17:11 +020015
Jerome Forissierc51c4e12018-11-14 11:02:49 +010016
Jerome Forissierd0c63612017-07-25 18:17:11 +020017def get_args():
Jerome Forissierc51c4e12018-11-14 11:02:49 +010018 parser = argparse.ArgumentParser(
19 description='Converts a Trusted '
20 'Application ELF file into a C source file, ready for '
21 'inclusion in the TEE binary as an "early TA".')
Jerome Forissierd0c63612017-07-25 18:17:11 +020022
Jerome Forissierc51c4e12018-11-14 11:02:49 +010023 parser.add_argument('--out', required=True,
24 help='Name of the output C file')
Jerome Forissierd0c63612017-07-25 18:17:11 +020025
Jerome Forissierc51c4e12018-11-14 11:02:49 +010026 parser.add_argument(
27 '--ta',
28 required=True,
29 help='Path to the TA binary. File name has to be: <uuid>.* '
30 'such as: 8aaaf200-2450-11e4-abe2-0002a5d5c51b.stripped.elf')
Jerome Forissierd0c63612017-07-25 18:17:11 +020031
Jerome Forissierc51c4e12018-11-14 11:02:49 +010032 parser.add_argument(
33 '--compress',
34 dest="compress",
35 action="store_true",
36 help='Compress the TA using the DEFLATE '
37 'algorithm')
Jerome Forissier509a9802017-08-01 10:14:57 +020038
Jerome Forissierc51c4e12018-11-14 11:02:49 +010039 return parser.parse_args()
40
Jerome Forissierd0c63612017-07-25 18:17:11 +020041
Maxim Uvarov8355f922020-04-09 15:19:28 +010042def get_name(obj):
43 # Symbol or section .name can be a byte array or a string, we want a string
44 try:
45 name = obj.name.decode()
46 except (UnicodeDecodeError, AttributeError):
47 name = obj.name
48 return name
49
50
51def ta_get_flags(ta_f):
52 with open(ta_f, 'rb') as f:
53 elffile = ELFFile(f)
54
55 for s in elffile.iter_sections():
56 if get_name(s) == '.ta_head':
57 return struct.unpack('<16x4xI', s.data()[:24])[0]
58
59 raise Exception('.ta_head section not found')
60
61
Jerome Forissierd0c63612017-07-25 18:17:11 +020062def main():
Jerome Forissierc51c4e12018-11-14 11:02:49 +010063 args = get_args()
Jerome Forissierd0c63612017-07-25 18:17:11 +020064
Jerome Forissierc51c4e12018-11-14 11:02:49 +010065 ta_uuid = uuid.UUID(re.sub(r'\..*', '', os.path.basename(args.ta)))
Jerome Forissierd0c63612017-07-25 18:17:11 +020066
Jerome Forissierc51c4e12018-11-14 11:02:49 +010067 with open(args.ta, 'rb') as ta:
68 bytes = ta.read()
69 uncompressed_size = len(bytes)
70 if args.compress:
71 bytes = zlib.compress(bytes)
72 size = len(bytes)
Jerome Forissier509a9802017-08-01 10:14:57 +020073
Jerome Forissierc51c4e12018-11-14 11:02:49 +010074 f = open(args.out, 'w')
75 f.write('/* Generated from ' + args.ta + ' by ' +
76 os.path.basename(__file__) + ' */\n\n')
77 f.write('#include <compiler.h>\n')
78 f.write('#include <kernel/early_ta.h>\n\n')
79 f.write('__extension__ const struct early_ta __early_ta_' +
80 ta_uuid.hex +
81 '\n__early_ta __aligned(__alignof__(struct early_ta)) = {\n')
Maxim Uvarov8355f922020-04-09 15:19:28 +010082 f.write('\t.flags = 0x{:04x},\n'.format(ta_get_flags(args.ta)))
Jerome Forissierc51c4e12018-11-14 11:02:49 +010083 f.write('\t.uuid = {\n')
84 f.write('\t\t.timeLow = 0x{:08x},\n'.format(ta_uuid.time_low))
85 f.write('\t\t.timeMid = 0x{:04x},\n'.format(ta_uuid.time_mid))
86 f.write('\t\t.timeHiAndVersion = ' +
87 '0x{:04x},\n'.format(ta_uuid.time_hi_version))
88 f.write('\t\t.clockSeqAndNode = {\n')
89 csn = '{0:02x}{1:02x}{2:012x}'.format(ta_uuid.clock_seq_hi_variant,
90 ta_uuid.clock_seq_low, ta_uuid.node)
91 f.write('\t\t\t')
92 f.write(', '.join('0x' + csn[i:i + 2] for i in range(0, len(csn), 2)))
93 f.write('\n\t\t},\n\t},\n')
94 f.write('\t.size = {:d},\n'.format(size))
95 if args.compress:
96 f.write('\t.uncompressed_size = '
97 '{:d},\n'.format(uncompressed_size))
98 f.write('\t.ta = {\n')
99 i = 0
100 while i < size:
101 if i % 8 == 0:
102 f.write('\t\t')
Pipat Methavanitpong8ca381f2019-07-30 15:22:53 +0900103 f.write(hex(bytes[i]) + ',')
Jerome Forissierc51c4e12018-11-14 11:02:49 +0100104 i = i + 1
105 if i % 8 == 0 or i == size:
106 f.write('\n')
107 else:
108 f.write(' ')
109 f.write('\t},\n')
110 f.write('};\n')
111 f.close()
112
Jerome Forissierd0c63612017-07-25 18:17:11 +0200113
114if __name__ == "__main__":
Jerome Forissierc51c4e12018-11-14 11:02:49 +0100115 main()