blob: 585edcf9d560fc7ac9c3f791924165695ab9413a [file] [log] [blame]
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +02001#!/usr/bin/env python
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
66def append_fdt_node(file, dtbs):
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020067 # Append FDT nodes.
Kever Yang58ae2152017-12-15 11:15:03 +080068 cnt = 1
69 for dtb in dtbs:
70 dtname = os.path.basename(dtb)
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +020071 file.write('\t\tfdt_%d {\n' % cnt)
72 file.write('\t\t\tdescription = "%s";\n' % dtname)
73 file.write('\t\t\tdata = /incbin/("%s");\n' % dtb)
74 file.write('\t\t\ttype = "flat_dt";\n')
75 file.write('\t\t\tcompression = "none";\n')
76 file.write('\t\t};\n')
77 file.write('\n')
Kever Yang58ae2152017-12-15 11:15:03 +080078 cnt = cnt + 1
79
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020080def append_conf_section(file, cnt, dtname, segments):
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +020081 file.write('\t\tconfig_%d {\n' % cnt)
82 file.write('\t\t\tdescription = "%s";\n' % dtname)
83 file.write('\t\t\tfirmware = "atf_1";\n')
Jagan Teki2c2b4292019-06-20 16:29:22 +053084 file.write('\t\t\tloadables = "uboot"')
Heiko Stuebner5a989792019-09-26 21:15:15 +020085 if segments > 1:
Jagan Teki2c2b4292019-06-20 16:29:22 +053086 file.write(',')
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020087 for i in range(1, segments):
Andy Yan13833862019-07-04 17:44:40 +080088 file.write('"atf_%d"' % (i + 1))
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020089 if i != (segments - 1):
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +020090 file.write(',')
Kever Yang58ae2152017-12-15 11:15:03 +080091 else:
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +020092 file.write(';\n')
Heiko Stuebner5a989792019-09-26 21:15:15 +020093 if segments <= 1:
Jagan Teki2c2b4292019-06-20 16:29:22 +053094 file.write(';\n')
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +020095 file.write('\t\t\tfdt = "fdt_1";\n')
96 file.write('\t\t};\n')
97 file.write('\n')
Kever Yang58ae2152017-12-15 11:15:03 +080098
Christoph Muellnera0c7ee22019-05-07 11:11:01 +020099def append_conf_node(file, dtbs, segments):
100 # Append configeration nodes.
Kever Yang58ae2152017-12-15 11:15:03 +0800101 cnt = 1
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +0200102 file.write('\tconfigurations {\n')
103 file.write('\t\tdefault = "config_1";\n')
Kever Yang58ae2152017-12-15 11:15:03 +0800104 for dtb in dtbs:
105 dtname = os.path.basename(dtb)
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200106 append_conf_section(file, cnt, dtname, segments)
Kever Yang58ae2152017-12-15 11:15:03 +0800107 cnt = cnt + 1
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +0200108 file.write('\t};\n')
109 file.write('\n')
Kever Yang58ae2152017-12-15 11:15:03 +0800110
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200111def generate_atf_fit_dts_uboot(fit_file, uboot_file_name):
Chris Webbdd7c7002019-07-16 20:52:57 +0100112 segments = unpack_elf(uboot_file_name)
113 if len(segments) != 1:
114 raise ValueError("Invalid u-boot ELF image '%s'" % uboot_file_name)
115 index, entry, p_paddr, data = segments[0]
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200116 fit_file.write(DT_UBOOT % p_paddr)
Kever Yang58ae2152017-12-15 11:15:03 +0800117
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200118def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, dtbs_file_name):
Chris Webbdd7c7002019-07-16 20:52:57 +0100119 segments = unpack_elf(bl31_file_name)
120 for index, entry, paddr, data in segments:
121 append_bl31_node(fit_file, index + 1, paddr, entry)
Kever Yang58ae2152017-12-15 11:15:03 +0800122 append_fdt_node(fit_file, dtbs_file_name)
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200123 fit_file.write(DT_IMAGES_NODE_END)
Chris Webbdd7c7002019-07-16 20:52:57 +0100124 append_conf_node(fit_file, dtbs_file_name, len(segments))
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200125
126def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name):
127 # Generate FIT script for ATF image.
128 if fit_file_name != sys.stdout:
129 fit_file = open(fit_file_name, "wb")
130 else:
131 fit_file = sys.stdout
132
133 fit_file.write(DT_HEADER)
134 generate_atf_fit_dts_uboot(fit_file, uboot_file_name)
135 generate_atf_fit_dts_bl31(fit_file, bl31_file_name, dtbs_file_name)
136 fit_file.write(DT_END)
Kever Yang58ae2152017-12-15 11:15:03 +0800137
138 if fit_file_name != sys.stdout:
139 fit_file.close()
140
141def generate_atf_binary(bl31_file_name):
Chris Webbdd7c7002019-07-16 20:52:57 +0100142 for index, entry, paddr, data in unpack_elf(bl31_file_name):
143 file_name = 'bl31_0x%08x.bin' % paddr
144 with open(file_name, "wb") as atf:
145 atf.write(data)
146
147def unpack_elf(filename):
148 with open(filename, 'rb') as file:
149 elf = file.read()
150 if elf[0:7] != b'\x7fELF\x02\x01\x01' or elf[18:20] != b'\xb7\x00':
151 raise ValueError("Invalid arm64 ELF file '%s'" % filename)
152
153 e_entry, e_phoff = struct.unpack_from('<2Q', elf, 0x18)
154 e_phentsize, e_phnum = struct.unpack_from('<2H', elf, 0x36)
155 segments = []
Kever Yang58ae2152017-12-15 11:15:03 +0800156
Chris Webbdd7c7002019-07-16 20:52:57 +0100157 for index in range(e_phnum):
158 offset = e_phoff + e_phentsize * index
159 p_type, p_flags, p_offset = struct.unpack_from('<LLQ', elf, offset)
160 if p_type == 1: # PT_LOAD
161 p_paddr, p_filesz = struct.unpack_from('<2Q', elf, offset + 0x18)
162 p_data = elf[p_offset:p_offset + p_filesz]
163 segments.append((index, e_entry, p_paddr, p_data))
164 return segments
Kever Yang58ae2152017-12-15 11:15:03 +0800165
166def main():
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200167 uboot_elf = "./u-boot"
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200168 fit_its = sys.stdout
Jagan Teki2c2b4292019-06-20 16:29:22 +0530169 if "BL31" in os.environ:
170 bl31_elf=os.getenv("BL31");
171 elif os.path.isfile("./bl31.elf"):
172 bl31_elf = "./bl31.elf"
173 else:
174 os.system("echo 'int main(){}' > bl31.c")
175 os.system("${CROSS_COMPILE}gcc -c bl31.c -o bl31.elf")
176 bl31_elf = "./bl31.elf"
177 logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
178 logging.warning(' BL31 file bl31.elf NOT found, resulting binary is non-functional')
179 logging.warning(' Please read Building section in doc/README.rockchip')
Kever Yang58ae2152017-12-15 11:15:03 +0800180
181 opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h")
182 for opt, val in opts:
183 if opt == "-o":
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200184 fit_its = val
Kever Yang58ae2152017-12-15 11:15:03 +0800185 elif opt == "-u":
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200186 uboot_elf = val
Kever Yang58ae2152017-12-15 11:15:03 +0800187 elif opt == "-b":
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200188 bl31_elf = val
Kever Yang58ae2152017-12-15 11:15:03 +0800189 elif opt == "-h":
Mian Yousaf Kaukab9a9c2452018-06-08 10:47:10 +0200190 print(__doc__)
Kever Yang58ae2152017-12-15 11:15:03 +0800191 sys.exit(2)
192
193 dtbs = args
Kever Yang58ae2152017-12-15 11:15:03 +0800194
Christoph Muellnera0c7ee22019-05-07 11:11:01 +0200195 generate_atf_fit_dts(fit_its, bl31_elf, uboot_elf, dtbs)
196 generate_atf_binary(bl31_elf)
Kever Yang58ae2152017-12-15 11:15:03 +0800197
198if __name__ == "__main__":
199 main()