blob: daf516ccb90871d27534827d72ddd4e618aefec6 [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
9import os
10import re
11import uuid
Jerome Forissier509a9802017-08-01 10:14:57 +020012import zlib
Jerome Forissierd0c63612017-07-25 18:17:11 +020013
Jerome Forissierc51c4e12018-11-14 11:02:49 +010014
Jerome Forissierd0c63612017-07-25 18:17:11 +020015def get_args():
Jerome Forissierc51c4e12018-11-14 11:02:49 +010016 parser = argparse.ArgumentParser(
17 description='Converts a Trusted '
18 'Application ELF file into a C source file, ready for '
19 'inclusion in the TEE binary as an "early TA".')
Jerome Forissierd0c63612017-07-25 18:17:11 +020020
Jerome Forissierc51c4e12018-11-14 11:02:49 +010021 parser.add_argument('--out', required=True,
22 help='Name of the output C file')
Jerome Forissierd0c63612017-07-25 18:17:11 +020023
Jerome Forissierc51c4e12018-11-14 11:02:49 +010024 parser.add_argument(
25 '--ta',
26 required=True,
27 help='Path to the TA binary. File name has to be: <uuid>.* '
28 'such as: 8aaaf200-2450-11e4-abe2-0002a5d5c51b.stripped.elf')
Jerome Forissierd0c63612017-07-25 18:17:11 +020029
Jerome Forissierc51c4e12018-11-14 11:02:49 +010030 parser.add_argument(
31 '--compress',
32 dest="compress",
33 action="store_true",
34 help='Compress the TA using the DEFLATE '
35 'algorithm')
Jerome Forissier509a9802017-08-01 10:14:57 +020036
Jerome Forissierc51c4e12018-11-14 11:02:49 +010037 return parser.parse_args()
38
Jerome Forissierd0c63612017-07-25 18:17:11 +020039
40def main():
Jerome Forissierc51c4e12018-11-14 11:02:49 +010041 args = get_args()
Jerome Forissierd0c63612017-07-25 18:17:11 +020042
Jerome Forissierc51c4e12018-11-14 11:02:49 +010043 ta_uuid = uuid.UUID(re.sub(r'\..*', '', os.path.basename(args.ta)))
Jerome Forissierd0c63612017-07-25 18:17:11 +020044
Jerome Forissierc51c4e12018-11-14 11:02:49 +010045 with open(args.ta, 'rb') as ta:
46 bytes = ta.read()
47 uncompressed_size = len(bytes)
48 if args.compress:
49 bytes = zlib.compress(bytes)
50 size = len(bytes)
Jerome Forissier509a9802017-08-01 10:14:57 +020051
Jerome Forissierc51c4e12018-11-14 11:02:49 +010052 f = open(args.out, 'w')
53 f.write('/* Generated from ' + args.ta + ' by ' +
54 os.path.basename(__file__) + ' */\n\n')
55 f.write('#include <compiler.h>\n')
56 f.write('#include <kernel/early_ta.h>\n\n')
57 f.write('__extension__ const struct early_ta __early_ta_' +
58 ta_uuid.hex +
59 '\n__early_ta __aligned(__alignof__(struct early_ta)) = {\n')
60 f.write('\t.uuid = {\n')
61 f.write('\t\t.timeLow = 0x{:08x},\n'.format(ta_uuid.time_low))
62 f.write('\t\t.timeMid = 0x{:04x},\n'.format(ta_uuid.time_mid))
63 f.write('\t\t.timeHiAndVersion = ' +
64 '0x{:04x},\n'.format(ta_uuid.time_hi_version))
65 f.write('\t\t.clockSeqAndNode = {\n')
66 csn = '{0:02x}{1:02x}{2:012x}'.format(ta_uuid.clock_seq_hi_variant,
67 ta_uuid.clock_seq_low, ta_uuid.node)
68 f.write('\t\t\t')
69 f.write(', '.join('0x' + csn[i:i + 2] for i in range(0, len(csn), 2)))
70 f.write('\n\t\t},\n\t},\n')
71 f.write('\t.size = {:d},\n'.format(size))
72 if args.compress:
73 f.write('\t.uncompressed_size = '
74 '{:d},\n'.format(uncompressed_size))
75 f.write('\t.ta = {\n')
76 i = 0
77 while i < size:
78 if i % 8 == 0:
79 f.write('\t\t')
Pipat Methavanitpong8ca381f2019-07-30 15:22:53 +090080 f.write(hex(bytes[i]) + ',')
Jerome Forissierc51c4e12018-11-14 11:02:49 +010081 i = i + 1
82 if i % 8 == 0 or i == size:
83 f.write('\n')
84 else:
85 f.write(' ')
86 f.write('\t},\n')
87 f.write('};\n')
88 f.close()
89
Jerome Forissierd0c63612017-07-25 18:17:11 +020090
91if __name__ == "__main__":
Jerome Forissierc51c4e12018-11-14 11:02:49 +010092 main()