blob: 08cfe9f51e9fae359d03800275c47d57edc562e5 [file] [log] [blame]
Jack Mitchelld43d0c62019-12-10 13:07:21 +00001#!/usr/bin/env python3
Kever Yang58ae2152017-12-15 11:15:03 +08002"""
Christoph Muellnera0c7ee22019-05-07 11:11:01 +02003# SPDX-License-Identifier: GPL-2.0+
4#
5# A script to generate FIT image source for rockchip boards
6# with ARM Trusted Firmware
7# and multiple device trees (given on the command line)
8#
9# usage: $0 <dt_name> [<dt_name> [<dt_name] ...]
Kever Yang58ae2152017-12-15 11:15:03 +080010"""
11
12import os
13import sys
14import getopt
Jagan Teki2c2b4292019-06-20 16:29:22 +053015import logging
Chris Webbdd7c7002019-07-16 20:52:57 +010016import struct
Kever Yang58ae2152017-12-15 11:15:03 +080017
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020018DT_HEADER = """
Tom Rini10e47792018-05-06 17:58:06 -040019/*
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020020 * This is a generated file.
Kever Yang58ae2152017-12-15 11:15:03 +080021 */
22/dts-v1/;
23
24/ {
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020025 description = "FIT image for U-Boot with bl31 (TF-A)";
Kever Yang58ae2152017-12-15 11:15:03 +080026 #address-cells = <1>;
27
28 images {
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020029"""
30
31DT_UBOOT = """
Kever Yang29c8c742018-08-23 11:01:08 +080032 uboot {
Kever Yang58ae2152017-12-15 11:15:03 +080033 description = "U-Boot (64-bit)";
34 data = /incbin/("u-boot-nodtb.bin");
35 type = "standalone";
36 os = "U-Boot";
37 arch = "arm64";
38 compression = "none";
39 load = <0x%08x>;
40 };
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +020041
Kever Yang58ae2152017-12-15 11:15:03 +080042"""
43
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020044DT_IMAGES_NODE_END = """ };
Kever Yang58ae2152017-12-15 11:15:03 +080045
Kever Yang58ae2152017-12-15 11:15:03 +080046"""
47
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020048DT_END = "};"
49
50def append_bl31_node(file, atf_index, phy_addr, elf_entry):
51 # Append BL31 DT node to input FIT dts file.
Kever Yang58ae2152017-12-15 11:15:03 +080052 data = 'bl31_0x%08x.bin' % phy_addr
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +020053 file.write('\t\tatf_%d {\n' % atf_index)
54 file.write('\t\t\tdescription = \"ARM Trusted Firmware\";\n')
55 file.write('\t\t\tdata = /incbin/("%s");\n' % data)
56 file.write('\t\t\ttype = "firmware";\n')
57 file.write('\t\t\tarch = "arm64";\n')
58 file.write('\t\t\tos = "arm-trusted-firmware";\n')
59 file.write('\t\t\tcompression = "none";\n')
60 file.write('\t\t\tload = <0x%08x>;\n' % phy_addr)
Kever Yang58ae2152017-12-15 11:15:03 +080061 if atf_index == 1:
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +020062 file.write('\t\t\tentry = <0x%08x>;\n' % elf_entry)
63 file.write('\t\t};\n')
64 file.write('\n')
Kever Yang58ae2152017-12-15 11:15:03 +080065
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +020066def append_tee_node(file, atf_index, phy_addr, elf_entry):
67 # Append TEE DT node to input FIT dts file.
68 data = 'tee_0x%08x.bin' % phy_addr
69 file.write('\t\tatf_%d {\n' % atf_index)
70 file.write('\t\t\tdescription = \"TEE\";\n')
71 file.write('\t\t\tdata = /incbin/("%s");\n' % data)
72 file.write('\t\t\ttype = "tee";\n')
73 file.write('\t\t\tarch = "arm64";\n')
74 file.write('\t\t\tos = "tee";\n')
75 file.write('\t\t\tcompression = "none";\n')
76 file.write('\t\t\tload = <0x%08x>;\n' % phy_addr)
77 file.write('\t\t\tentry = <0x%08x>;\n' % elf_entry)
78 file.write('\t\t};\n')
79 file.write('\n')
80
Kever Yang58ae2152017-12-15 11:15:03 +080081def append_fdt_node(file, dtbs):
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020082 # Append FDT nodes.
Kever Yang58ae2152017-12-15 11:15:03 +080083 cnt = 1
84 for dtb in dtbs:
85 dtname = os.path.basename(dtb)
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +020086 file.write('\t\tfdt_%d {\n' % cnt)
87 file.write('\t\t\tdescription = "%s";\n' % dtname)
88 file.write('\t\t\tdata = /incbin/("%s");\n' % dtb)
89 file.write('\t\t\ttype = "flat_dt";\n')
90 file.write('\t\t\tcompression = "none";\n')
91 file.write('\t\t};\n')
92 file.write('\n')
Kever Yang58ae2152017-12-15 11:15:03 +080093 cnt = cnt + 1
94
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020095def append_conf_section(file, cnt, dtname, segments):
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +020096 file.write('\t\tconfig_%d {\n' % cnt)
97 file.write('\t\t\tdescription = "%s";\n' % dtname)
98 file.write('\t\t\tfirmware = "atf_1";\n')
Jagan Teki2c2b4292019-06-20 16:29:22 +053099 file.write('\t\t\tloadables = "uboot"')
Heiko Stuebner5a989792019-09-26 21:15:15 +0200100 if segments > 1:
Jagan Teki2c2b4292019-06-20 16:29:22 +0530101 file.write(',')
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200102 for i in range(1, segments):
Andy Yan13833862019-07-04 17:44:40 +0800103 file.write('"atf_%d"' % (i + 1))
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200104 if i != (segments - 1):
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +0200105 file.write(',')
Kever Yang58ae2152017-12-15 11:15:03 +0800106 else:
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +0200107 file.write(';\n')
Heiko Stuebner5a989792019-09-26 21:15:15 +0200108 if segments <= 1:
Jagan Teki2c2b4292019-06-20 16:29:22 +0530109 file.write(';\n')
Heiko Stuebnerbf4c6c42020-01-17 21:26:04 +0100110 file.write('\t\t\tfdt = "fdt_%d";\n' % cnt)
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +0200111 file.write('\t\t};\n')
112 file.write('\n')
Kever Yang58ae2152017-12-15 11:15:03 +0800113
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200114def append_conf_node(file, dtbs, segments):
115 # Append configeration nodes.
Kever Yang58ae2152017-12-15 11:15:03 +0800116 cnt = 1
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +0200117 file.write('\tconfigurations {\n')
118 file.write('\t\tdefault = "config_1";\n')
Kever Yang58ae2152017-12-15 11:15:03 +0800119 for dtb in dtbs:
120 dtname = os.path.basename(dtb)
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200121 append_conf_section(file, cnt, dtname, segments)
Kever Yang58ae2152017-12-15 11:15:03 +0800122 cnt = cnt + 1
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +0200123 file.write('\t};\n')
124 file.write('\n')
Kever Yang58ae2152017-12-15 11:15:03 +0800125
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200126def generate_atf_fit_dts_uboot(fit_file, uboot_file_name):
Chris Webbdd7c7002019-07-16 20:52:57 +0100127 segments = unpack_elf(uboot_file_name)
128 if len(segments) != 1:
129 raise ValueError("Invalid u-boot ELF image '%s'" % uboot_file_name)
130 index, entry, p_paddr, data = segments[0]
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200131 fit_file.write(DT_UBOOT % p_paddr)
Kever Yang58ae2152017-12-15 11:15:03 +0800132
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200133def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, tee_file_name, dtbs_file_name):
Chris Webbdd7c7002019-07-16 20:52:57 +0100134 segments = unpack_elf(bl31_file_name)
135 for index, entry, paddr, data in segments:
136 append_bl31_node(fit_file, index + 1, paddr, entry)
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200137 num_segments = len(segments)
138
139 if tee_file_name:
Jerome Forissierce796052022-05-11 17:35:14 +0200140 tee_segments = unpack_tee_file(tee_file_name)
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200141 for index, entry, paddr, data in tee_segments:
142 append_tee_node(fit_file, num_segments + index + 1, paddr, entry)
143 num_segments = num_segments + len(tee_segments)
144
Kever Yang58ae2152017-12-15 11:15:03 +0800145 append_fdt_node(fit_file, dtbs_file_name)
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200146 fit_file.write(DT_IMAGES_NODE_END)
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200147 append_conf_node(fit_file, dtbs_file_name, num_segments)
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200148
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200149def generate_atf_fit_dts(fit_file_name, bl31_file_name, tee_file_name, uboot_file_name, dtbs_file_name):
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200150 # Generate FIT script for ATF image.
151 if fit_file_name != sys.stdout:
152 fit_file = open(fit_file_name, "wb")
153 else:
154 fit_file = sys.stdout
155
156 fit_file.write(DT_HEADER)
157 generate_atf_fit_dts_uboot(fit_file, uboot_file_name)
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200158 generate_atf_fit_dts_bl31(fit_file, bl31_file_name, tee_file_name, dtbs_file_name)
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200159 fit_file.write(DT_END)
Kever Yang58ae2152017-12-15 11:15:03 +0800160
161 if fit_file_name != sys.stdout:
162 fit_file.close()
163
164def generate_atf_binary(bl31_file_name):
Chris Webbdd7c7002019-07-16 20:52:57 +0100165 for index, entry, paddr, data in unpack_elf(bl31_file_name):
166 file_name = 'bl31_0x%08x.bin' % paddr
167 with open(file_name, "wb") as atf:
168 atf.write(data)
169
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200170def generate_tee_binary(tee_file_name):
171 if tee_file_name:
Jerome Forissierce796052022-05-11 17:35:14 +0200172 for index, entry, paddr, data in unpack_tee_file(tee_file_name):
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200173 file_name = 'tee_0x%08x.bin' % paddr
174 with open(file_name, "wb") as atf:
175 atf.write(data)
176
Chris Webbdd7c7002019-07-16 20:52:57 +0100177def unpack_elf(filename):
178 with open(filename, 'rb') as file:
179 elf = file.read()
180 if elf[0:7] != b'\x7fELF\x02\x01\x01' or elf[18:20] != b'\xb7\x00':
181 raise ValueError("Invalid arm64 ELF file '%s'" % filename)
182
183 e_entry, e_phoff = struct.unpack_from('<2Q', elf, 0x18)
184 e_phentsize, e_phnum = struct.unpack_from('<2H', elf, 0x36)
185 segments = []
Kever Yang58ae2152017-12-15 11:15:03 +0800186
Chris Webbdd7c7002019-07-16 20:52:57 +0100187 for index in range(e_phnum):
188 offset = e_phoff + e_phentsize * index
189 p_type, p_flags, p_offset = struct.unpack_from('<LLQ', elf, offset)
190 if p_type == 1: # PT_LOAD
191 p_paddr, p_filesz = struct.unpack_from('<2Q', elf, offset + 0x18)
Heinrich Schuchardte30025a2020-09-15 03:43:29 +0200192 if p_filesz > 0:
193 p_data = elf[p_offset:p_offset + p_filesz]
194 segments.append((index, e_entry, p_paddr, p_data))
Chris Webbdd7c7002019-07-16 20:52:57 +0100195 return segments
Kever Yang58ae2152017-12-15 11:15:03 +0800196
Jerome Forissierce796052022-05-11 17:35:14 +0200197def unpack_tee_file(filename):
198 if filename.endswith('.elf'):
199 return unpack_elf(filename)
200 with open(filename, 'rb') as file:
201 bin = file.read()
202 segments = []
203 if bin[0:5] == b'OPTE\x01':
204 # OP-TEE v1 format (tee.bin)
205 init_sz, start_hi, start_lo, _, paged_sz = struct.unpack_from('<5I',
206 bin,
207 0x8)
208 if paged_sz != 0:
209 raise ValueError("OP-TEE paged mode not supported")
210 e_entry = (start_hi << 32) + start_lo
211 p_addr = e_entry
212 p_data = bin[0x1c:]
213 if len(p_data) != init_sz:
214 raise ValueError("Invalid file '%s': size mismatch "
215 "(expected %d, have %d)" % (filename, init_sz,
216 len(p_data)))
217 segments.append((0, e_entry, p_addr, p_data))
218 else:
219 raise ValueError("Unknown format for TEE file '%s'" % filename)
220 return segments
221
Kever Yang58ae2152017-12-15 11:15:03 +0800222def main():
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200223 uboot_elf = "./u-boot"
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200224 fit_its = sys.stdout
Jagan Teki2c2b4292019-06-20 16:29:22 +0530225 if "BL31" in os.environ:
226 bl31_elf=os.getenv("BL31");
227 elif os.path.isfile("./bl31.elf"):
228 bl31_elf = "./bl31.elf"
229 else:
230 os.system("echo 'int main(){}' > bl31.c")
231 os.system("${CROSS_COMPILE}gcc -c bl31.c -o bl31.elf")
232 bl31_elf = "./bl31.elf"
233 logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
234 logging.warning(' BL31 file bl31.elf NOT found, resulting binary is non-functional')
235 logging.warning(' Please read Building section in doc/README.rockchip')
Kever Yang58ae2152017-12-15 11:15:03 +0800236
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200237 if "TEE" in os.environ:
Jerome Forissierce796052022-05-11 17:35:14 +0200238 tee_file = os.getenv("TEE")
239 elif os.path.isfile("./tee.bin"):
240 tee_file = "./tee.bin"
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200241 elif os.path.isfile("./tee.elf"):
Jerome Forissierce796052022-05-11 17:35:14 +0200242 tee_file = "./tee.elf"
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200243 else:
Jerome Forissierce796052022-05-11 17:35:14 +0200244 tee_file = ""
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200245
246 opts, args = getopt.getopt(sys.argv[1:], "o:u:b:t:h")
Kever Yang58ae2152017-12-15 11:15:03 +0800247 for opt, val in opts:
248 if opt == "-o":
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200249 fit_its = val
Kever Yang58ae2152017-12-15 11:15:03 +0800250 elif opt == "-u":
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200251 uboot_elf = val
Kever Yang58ae2152017-12-15 11:15:03 +0800252 elif opt == "-b":
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200253 bl31_elf = val
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200254 elif opt == "-t":
Jerome Forissierce796052022-05-11 17:35:14 +0200255 tee_file = val
Kever Yang58ae2152017-12-15 11:15:03 +0800256 elif opt == "-h":
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +0200257 print(__doc__)
Kever Yang58ae2152017-12-15 11:15:03 +0800258 sys.exit(2)
259
260 dtbs = args
Kever Yang58ae2152017-12-15 11:15:03 +0800261
Jerome Forissierce796052022-05-11 17:35:14 +0200262 generate_atf_fit_dts(fit_its, bl31_elf, tee_file, uboot_elf, dtbs)
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200263 generate_atf_binary(bl31_elf)
Jerome Forissierce796052022-05-11 17:35:14 +0200264 generate_tee_binary(tee_file)
Kever Yang58ae2152017-12-15 11:15:03 +0800265
266if __name__ == "__main__":
267 main()