1#!/usr/bin/env python3
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2019, Linaro Limited
5#
6
7import argparse
8import sys
9import zlib
10
11
12def get_args():
13    parser = argparse.ArgumentParser()
14
15    parser.add_argument('--input',
16                        required=True, type=argparse.FileType('rb'),
17                        help='The input StMM binary (BL32_AP_MM.fd)')
18
19    parser.add_argument('--output',
20                        required=True, type=argparse.FileType('w'),
21                        help='The output stmm_hex.c')
22
23    return parser.parse_args()
24
25
26def main():
27    args = get_args()
28    inf = args.input
29    outf = args.output
30
31    bytes = inf.read()
32    uncompressed_size = len(bytes)
33    bytes = zlib.compress(bytes)
34    size = len(bytes)
35
36    outf.write('/* Automatically generated, do no edit */\n')
37    outf.write('const unsigned char stmm_image[] = {\n')
38    i = 0
39    while i < size:
40        if i % 8 == 0:
41            outf.write('\t')
42        outf.write('0x{:02x},'.format(bytes[i]))
43        i = i + 1
44        if i % 8 == 0 or i == size:
45            outf.write('\n')
46        else:
47            outf.write(' ')
48    outf.write('};\n')
49
50    outf.write('const unsigned int stmm_image_size = sizeof(stmm_image);\n')
51    outf.write('const unsigned int stmm_image_uncompressed_size = '
52               '{:d};\n'.format(uncompressed_size))
53
54    inf.close()
55    outf.close()
56
57
58if __name__ == "__main__":
59    main()
60