blob: d15c32b303297b7a10db3044898579993c5ac689 [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:
140 tee_segments = unpack_elf(tee_file_name)
141 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:
172 for index, entry, paddr, data in unpack_elf(tee_file_name):
173 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)
192 p_data = elf[p_offset:p_offset + p_filesz]
193 segments.append((index, e_entry, p_paddr, p_data))
194 return segments
Kever Yang58ae2152017-12-15 11:15:03 +0800195
196def main():
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200197 uboot_elf = "./u-boot"
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200198 fit_its = sys.stdout
Jagan Teki2c2b4292019-06-20 16:29:22 +0530199 if "BL31" in os.environ:
200 bl31_elf=os.getenv("BL31");
201 elif os.path.isfile("./bl31.elf"):
202 bl31_elf = "./bl31.elf"
203 else:
204 os.system("echo 'int main(){}' > bl31.c")
205 os.system("${CROSS_COMPILE}gcc -c bl31.c -o bl31.elf")
206 bl31_elf = "./bl31.elf"
207 logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
208 logging.warning(' BL31 file bl31.elf NOT found, resulting binary is non-functional')
209 logging.warning(' Please read Building section in doc/README.rockchip')
Kever Yang58ae2152017-12-15 11:15:03 +0800210
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200211 if "TEE" in os.environ:
212 tee_elf = os.getenv("TEE")
213 elif os.path.isfile("./tee.elf"):
214 tee_elf = "./tee.elf"
215 else:
216 tee_elf = ""
217
218 opts, args = getopt.getopt(sys.argv[1:], "o:u:b:t:h")
Kever Yang58ae2152017-12-15 11:15:03 +0800219 for opt, val in opts:
220 if opt == "-o":
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200221 fit_its = val
Kever Yang58ae2152017-12-15 11:15:03 +0800222 elif opt == "-u":
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200223 uboot_elf = val
Kever Yang58ae2152017-12-15 11:15:03 +0800224 elif opt == "-b":
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200225 bl31_elf = val
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200226 elif opt == "-t":
227 tee_elf = val
Kever Yang58ae2152017-12-15 11:15:03 +0800228 elif opt == "-h":
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +0200229 print(__doc__)
Kever Yang58ae2152017-12-15 11:15:03 +0800230 sys.exit(2)
231
232 dtbs = args
Kever Yang58ae2152017-12-15 11:15:03 +0800233
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200234 generate_atf_fit_dts(fit_its, bl31_elf, tee_elf, uboot_elf, dtbs)
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200235 generate_atf_binary(bl31_elf)
Heiko Stuebnera2efb6c2019-10-06 20:10:21 +0200236 generate_tee_binary(tee_elf)
Kever Yang58ae2152017-12-15 11:15:03 +0800237
238if __name__ == "__main__":
239 main()