1#!/usr/bin/env python3
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2020 Carlo Caione <ccaione@baylibre.com>
5#
6# Derived from plat-stm32mp1/scripts/stm32image.py
7#
8
9import argparse
10import struct
11import mmap
12
13header_size = 0x200
14ext_magic_number = 0x12348765
15version = 0x00002710
16
17
18def get_size(file):
19    file.seek(0, 2)        # End of the file
20    size = file.tell()
21    return size
22
23
24def aml_set_header(dest_fd, entry, res_mem_start, res_mem_size, sec_mem_start,
25                   sec_mem_size):
26    dest_fd.seek(0, 0)
27
28    dest_fd.write(struct.pack('<IIQQQQQ',
29                  ext_magic_number,
30                  version,
31                  entry,
32                  res_mem_start,
33                  res_mem_size,
34                  sec_mem_start,
35                  sec_mem_size))
36
37    # Padding
38    dest_fd.write(b'\x00' * 464)
39    dest_fd.close()
40
41
42def aml_create_header_file(source, dest, entry, res_mem_start, res_mem_size,
43                           sec_mem_start, sec_mem_size):
44    dest_fd = open(dest, 'w+b')
45    src_fd = open(source, 'rb')
46
47    dest_fd.write(b'\x00' * header_size)
48
49    sizesrc = get_size(src_fd)
50    if sizesrc > 0:
51        mmsrc = mmap.mmap(src_fd.fileno(), 0, access=mmap.ACCESS_READ)
52        dest_fd.write(mmsrc[:sizesrc])
53        mmsrc.close()
54
55    src_fd.close()
56
57    aml_set_header(dest_fd, entry, res_mem_start, res_mem_size, sec_mem_start,
58                   sec_mem_size)
59
60    dest_fd.close()
61
62
63def auto_int(x):
64    return int(x, 0)
65
66
67def get_args():
68    parser = argparse.ArgumentParser()
69
70    parser.add_argument('--source',
71                        required=True,
72                        help='Source file')
73
74    parser.add_argument('--dest',
75                        required=True,
76                        help='Destination file')
77
78    parser.add_argument('--entry',
79                        required=True,
80                        type=auto_int,
81                        help='Entry point')
82
83    parser.add_argument('--res_mem_start',
84                        required=True,
85                        type=auto_int,
86                        help='Reserved memory start')
87
88    parser.add_argument('--res_mem_size',
89                        required=True,
90                        type=auto_int,
91                        help='Reserved memory size')
92
93    parser.add_argument('--sec_mem_start',
94                        required=True,
95                        type=auto_int,
96                        help='Secure memory start')
97
98    parser.add_argument('--sec_mem_size',
99                        required=True,
100                        type=auto_int,
101                        help='Secure memory size')
102
103    return parser.parse_args()
104
105
106def main():
107    args = get_args()
108
109    source_file = args.source
110    destination_file = args.dest
111    entry_point = args.entry
112    res_mem_start = args.res_mem_start
113    res_mem_size = args.res_mem_size
114    sec_mem_start = args.sec_mem_start
115    sec_mem_size = args.sec_mem_size
116
117    aml_create_header_file(source_file,
118                           destination_file,
119                           entry_point,
120                           res_mem_start,
121                           res_mem_size,
122                           sec_mem_start,
123                           sec_mem_size)
124
125
126if __name__ == "__main__":
127    main()
128