2019-12-10 13:07:21 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-12-15 03:15:03 +00:00
|
|
|
"""
|
2019-05-07 09:11:01 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0+
|
|
|
|
#
|
|
|
|
# A script to generate FIT image source for rockchip boards
|
|
|
|
# with ARM Trusted Firmware
|
|
|
|
# and multiple device trees (given on the command line)
|
|
|
|
#
|
|
|
|
# usage: $0 <dt_name> [<dt_name> [<dt_name] ...]
|
2017-12-15 03:15:03 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import getopt
|
2019-06-20 10:59:22 +00:00
|
|
|
import logging
|
2019-07-16 19:52:57 +00:00
|
|
|
import struct
|
2017-12-15 03:15:03 +00:00
|
|
|
|
2019-05-07 09:11:01 +00:00
|
|
|
DT_HEADER = """
|
2018-05-06 21:58:06 +00:00
|
|
|
/*
|
2019-05-07 09:11:01 +00:00
|
|
|
* This is a generated file.
|
2017-12-15 03:15:03 +00:00
|
|
|
*/
|
|
|
|
/dts-v1/;
|
|
|
|
|
|
|
|
/ {
|
2019-05-07 09:11:01 +00:00
|
|
|
description = "FIT image for U-Boot with bl31 (TF-A)";
|
2017-12-15 03:15:03 +00:00
|
|
|
#address-cells = <1>;
|
|
|
|
|
|
|
|
images {
|
2019-05-07 09:11:01 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
DT_UBOOT = """
|
2018-08-23 03:01:08 +00:00
|
|
|
uboot {
|
2017-12-15 03:15:03 +00:00
|
|
|
description = "U-Boot (64-bit)";
|
|
|
|
data = /incbin/("u-boot-nodtb.bin");
|
|
|
|
type = "standalone";
|
|
|
|
os = "U-Boot";
|
|
|
|
arch = "arm64";
|
|
|
|
compression = "none";
|
|
|
|
load = <0x%08x>;
|
|
|
|
};
|
2018-06-08 08:47:10 +00:00
|
|
|
|
2017-12-15 03:15:03 +00:00
|
|
|
"""
|
|
|
|
|
2019-05-07 09:11:01 +00:00
|
|
|
DT_IMAGES_NODE_END = """ };
|
2017-12-15 03:15:03 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-05-07 09:11:01 +00:00
|
|
|
DT_END = "};"
|
|
|
|
|
|
|
|
def append_bl31_node(file, atf_index, phy_addr, elf_entry):
|
|
|
|
# Append BL31 DT node to input FIT dts file.
|
2017-12-15 03:15:03 +00:00
|
|
|
data = 'bl31_0x%08x.bin' % phy_addr
|
2018-06-08 08:47:10 +00:00
|
|
|
file.write('\t\tatf_%d {\n' % atf_index)
|
|
|
|
file.write('\t\t\tdescription = \"ARM Trusted Firmware\";\n')
|
|
|
|
file.write('\t\t\tdata = /incbin/("%s");\n' % data)
|
|
|
|
file.write('\t\t\ttype = "firmware";\n')
|
|
|
|
file.write('\t\t\tarch = "arm64";\n')
|
|
|
|
file.write('\t\t\tos = "arm-trusted-firmware";\n')
|
|
|
|
file.write('\t\t\tcompression = "none";\n')
|
|
|
|
file.write('\t\t\tload = <0x%08x>;\n' % phy_addr)
|
2017-12-15 03:15:03 +00:00
|
|
|
if atf_index == 1:
|
2018-06-08 08:47:10 +00:00
|
|
|
file.write('\t\t\tentry = <0x%08x>;\n' % elf_entry)
|
|
|
|
file.write('\t\t};\n')
|
|
|
|
file.write('\n')
|
2017-12-15 03:15:03 +00:00
|
|
|
|
2019-10-06 18:10:21 +00:00
|
|
|
def append_tee_node(file, atf_index, phy_addr, elf_entry):
|
|
|
|
# Append TEE DT node to input FIT dts file.
|
|
|
|
data = 'tee_0x%08x.bin' % phy_addr
|
|
|
|
file.write('\t\tatf_%d {\n' % atf_index)
|
|
|
|
file.write('\t\t\tdescription = \"TEE\";\n')
|
|
|
|
file.write('\t\t\tdata = /incbin/("%s");\n' % data)
|
|
|
|
file.write('\t\t\ttype = "tee";\n')
|
|
|
|
file.write('\t\t\tarch = "arm64";\n')
|
|
|
|
file.write('\t\t\tos = "tee";\n')
|
|
|
|
file.write('\t\t\tcompression = "none";\n')
|
|
|
|
file.write('\t\t\tload = <0x%08x>;\n' % phy_addr)
|
|
|
|
file.write('\t\t\tentry = <0x%08x>;\n' % elf_entry)
|
|
|
|
file.write('\t\t};\n')
|
|
|
|
file.write('\n')
|
|
|
|
|
2017-12-15 03:15:03 +00:00
|
|
|
def append_fdt_node(file, dtbs):
|
2019-05-07 09:11:01 +00:00
|
|
|
# Append FDT nodes.
|
2017-12-15 03:15:03 +00:00
|
|
|
cnt = 1
|
|
|
|
for dtb in dtbs:
|
|
|
|
dtname = os.path.basename(dtb)
|
2018-06-08 08:47:10 +00:00
|
|
|
file.write('\t\tfdt_%d {\n' % cnt)
|
|
|
|
file.write('\t\t\tdescription = "%s";\n' % dtname)
|
|
|
|
file.write('\t\t\tdata = /incbin/("%s");\n' % dtb)
|
|
|
|
file.write('\t\t\ttype = "flat_dt";\n')
|
|
|
|
file.write('\t\t\tcompression = "none";\n')
|
|
|
|
file.write('\t\t};\n')
|
|
|
|
file.write('\n')
|
2017-12-15 03:15:03 +00:00
|
|
|
cnt = cnt + 1
|
|
|
|
|
2019-05-07 09:11:01 +00:00
|
|
|
def append_conf_section(file, cnt, dtname, segments):
|
2018-06-08 08:47:10 +00:00
|
|
|
file.write('\t\tconfig_%d {\n' % cnt)
|
|
|
|
file.write('\t\t\tdescription = "%s";\n' % dtname)
|
|
|
|
file.write('\t\t\tfirmware = "atf_1";\n')
|
2019-06-20 10:59:22 +00:00
|
|
|
file.write('\t\t\tloadables = "uboot"')
|
2019-09-26 19:15:15 +00:00
|
|
|
if segments > 1:
|
2019-06-20 10:59:22 +00:00
|
|
|
file.write(',')
|
2019-05-07 09:11:01 +00:00
|
|
|
for i in range(1, segments):
|
2019-07-04 09:44:40 +00:00
|
|
|
file.write('"atf_%d"' % (i + 1))
|
2019-05-07 09:11:01 +00:00
|
|
|
if i != (segments - 1):
|
2018-06-08 08:47:10 +00:00
|
|
|
file.write(',')
|
2017-12-15 03:15:03 +00:00
|
|
|
else:
|
2018-06-08 08:47:10 +00:00
|
|
|
file.write(';\n')
|
2019-09-26 19:15:15 +00:00
|
|
|
if segments <= 1:
|
2019-06-20 10:59:22 +00:00
|
|
|
file.write(';\n')
|
2020-01-17 20:26:04 +00:00
|
|
|
file.write('\t\t\tfdt = "fdt_%d";\n' % cnt)
|
2018-06-08 08:47:10 +00:00
|
|
|
file.write('\t\t};\n')
|
|
|
|
file.write('\n')
|
2017-12-15 03:15:03 +00:00
|
|
|
|
2019-05-07 09:11:01 +00:00
|
|
|
def append_conf_node(file, dtbs, segments):
|
|
|
|
# Append configeration nodes.
|
2017-12-15 03:15:03 +00:00
|
|
|
cnt = 1
|
2018-06-08 08:47:10 +00:00
|
|
|
file.write('\tconfigurations {\n')
|
|
|
|
file.write('\t\tdefault = "config_1";\n')
|
2017-12-15 03:15:03 +00:00
|
|
|
for dtb in dtbs:
|
|
|
|
dtname = os.path.basename(dtb)
|
2019-05-07 09:11:01 +00:00
|
|
|
append_conf_section(file, cnt, dtname, segments)
|
2017-12-15 03:15:03 +00:00
|
|
|
cnt = cnt + 1
|
2018-06-08 08:47:10 +00:00
|
|
|
file.write('\t};\n')
|
|
|
|
file.write('\n')
|
2017-12-15 03:15:03 +00:00
|
|
|
|
2019-05-07 09:11:01 +00:00
|
|
|
def generate_atf_fit_dts_uboot(fit_file, uboot_file_name):
|
2019-07-16 19:52:57 +00:00
|
|
|
segments = unpack_elf(uboot_file_name)
|
|
|
|
if len(segments) != 1:
|
|
|
|
raise ValueError("Invalid u-boot ELF image '%s'" % uboot_file_name)
|
|
|
|
index, entry, p_paddr, data = segments[0]
|
2019-05-07 09:11:01 +00:00
|
|
|
fit_file.write(DT_UBOOT % p_paddr)
|
2017-12-15 03:15:03 +00:00
|
|
|
|
2019-10-06 18:10:21 +00:00
|
|
|
def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, tee_file_name, dtbs_file_name):
|
2019-07-16 19:52:57 +00:00
|
|
|
segments = unpack_elf(bl31_file_name)
|
|
|
|
for index, entry, paddr, data in segments:
|
|
|
|
append_bl31_node(fit_file, index + 1, paddr, entry)
|
2019-10-06 18:10:21 +00:00
|
|
|
num_segments = len(segments)
|
|
|
|
|
|
|
|
if tee_file_name:
|
|
|
|
tee_segments = unpack_elf(tee_file_name)
|
|
|
|
for index, entry, paddr, data in tee_segments:
|
|
|
|
append_tee_node(fit_file, num_segments + index + 1, paddr, entry)
|
|
|
|
num_segments = num_segments + len(tee_segments)
|
|
|
|
|
2017-12-15 03:15:03 +00:00
|
|
|
append_fdt_node(fit_file, dtbs_file_name)
|
2019-05-07 09:11:01 +00:00
|
|
|
fit_file.write(DT_IMAGES_NODE_END)
|
2019-10-06 18:10:21 +00:00
|
|
|
append_conf_node(fit_file, dtbs_file_name, num_segments)
|
2019-05-07 09:11:01 +00:00
|
|
|
|
2019-10-06 18:10:21 +00:00
|
|
|
def generate_atf_fit_dts(fit_file_name, bl31_file_name, tee_file_name, uboot_file_name, dtbs_file_name):
|
2019-05-07 09:11:01 +00:00
|
|
|
# Generate FIT script for ATF image.
|
|
|
|
if fit_file_name != sys.stdout:
|
|
|
|
fit_file = open(fit_file_name, "wb")
|
|
|
|
else:
|
|
|
|
fit_file = sys.stdout
|
|
|
|
|
|
|
|
fit_file.write(DT_HEADER)
|
|
|
|
generate_atf_fit_dts_uboot(fit_file, uboot_file_name)
|
2019-10-06 18:10:21 +00:00
|
|
|
generate_atf_fit_dts_bl31(fit_file, bl31_file_name, tee_file_name, dtbs_file_name)
|
2019-05-07 09:11:01 +00:00
|
|
|
fit_file.write(DT_END)
|
2017-12-15 03:15:03 +00:00
|
|
|
|
|
|
|
if fit_file_name != sys.stdout:
|
|
|
|
fit_file.close()
|
|
|
|
|
|
|
|
def generate_atf_binary(bl31_file_name):
|
2019-07-16 19:52:57 +00:00
|
|
|
for index, entry, paddr, data in unpack_elf(bl31_file_name):
|
|
|
|
file_name = 'bl31_0x%08x.bin' % paddr
|
|
|
|
with open(file_name, "wb") as atf:
|
|
|
|
atf.write(data)
|
|
|
|
|
2019-10-06 18:10:21 +00:00
|
|
|
def generate_tee_binary(tee_file_name):
|
|
|
|
if tee_file_name:
|
|
|
|
for index, entry, paddr, data in unpack_elf(tee_file_name):
|
|
|
|
file_name = 'tee_0x%08x.bin' % paddr
|
|
|
|
with open(file_name, "wb") as atf:
|
|
|
|
atf.write(data)
|
|
|
|
|
2019-07-16 19:52:57 +00:00
|
|
|
def unpack_elf(filename):
|
|
|
|
with open(filename, 'rb') as file:
|
|
|
|
elf = file.read()
|
|
|
|
if elf[0:7] != b'\x7fELF\x02\x01\x01' or elf[18:20] != b'\xb7\x00':
|
|
|
|
raise ValueError("Invalid arm64 ELF file '%s'" % filename)
|
|
|
|
|
|
|
|
e_entry, e_phoff = struct.unpack_from('<2Q', elf, 0x18)
|
|
|
|
e_phentsize, e_phnum = struct.unpack_from('<2H', elf, 0x36)
|
|
|
|
segments = []
|
|
|
|
|
|
|
|
for index in range(e_phnum):
|
|
|
|
offset = e_phoff + e_phentsize * index
|
|
|
|
p_type, p_flags, p_offset = struct.unpack_from('<LLQ', elf, offset)
|
|
|
|
if p_type == 1: # PT_LOAD
|
|
|
|
p_paddr, p_filesz = struct.unpack_from('<2Q', elf, offset + 0x18)
|
2020-09-15 01:43:29 +00:00
|
|
|
if p_filesz > 0:
|
|
|
|
p_data = elf[p_offset:p_offset + p_filesz]
|
|
|
|
segments.append((index, e_entry, p_paddr, p_data))
|
2019-07-16 19:52:57 +00:00
|
|
|
return segments
|
2017-12-15 03:15:03 +00:00
|
|
|
|
|
|
|
def main():
|
2019-05-07 09:11:01 +00:00
|
|
|
uboot_elf = "./u-boot"
|
|
|
|
fit_its = sys.stdout
|
2019-06-20 10:59:22 +00:00
|
|
|
if "BL31" in os.environ:
|
|
|
|
bl31_elf=os.getenv("BL31");
|
|
|
|
elif os.path.isfile("./bl31.elf"):
|
|
|
|
bl31_elf = "./bl31.elf"
|
|
|
|
else:
|
|
|
|
os.system("echo 'int main(){}' > bl31.c")
|
|
|
|
os.system("${CROSS_COMPILE}gcc -c bl31.c -o bl31.elf")
|
|
|
|
bl31_elf = "./bl31.elf"
|
|
|
|
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
|
|
|
|
logging.warning(' BL31 file bl31.elf NOT found, resulting binary is non-functional')
|
|
|
|
logging.warning(' Please read Building section in doc/README.rockchip')
|
2017-12-15 03:15:03 +00:00
|
|
|
|
2019-10-06 18:10:21 +00:00
|
|
|
if "TEE" in os.environ:
|
|
|
|
tee_elf = os.getenv("TEE")
|
|
|
|
elif os.path.isfile("./tee.elf"):
|
|
|
|
tee_elf = "./tee.elf"
|
|
|
|
else:
|
|
|
|
tee_elf = ""
|
|
|
|
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "o:u:b:t:h")
|
2017-12-15 03:15:03 +00:00
|
|
|
for opt, val in opts:
|
|
|
|
if opt == "-o":
|
2019-05-07 09:11:01 +00:00
|
|
|
fit_its = val
|
2017-12-15 03:15:03 +00:00
|
|
|
elif opt == "-u":
|
2019-05-07 09:11:01 +00:00
|
|
|
uboot_elf = val
|
2017-12-15 03:15:03 +00:00
|
|
|
elif opt == "-b":
|
2019-05-07 09:11:01 +00:00
|
|
|
bl31_elf = val
|
2019-10-06 18:10:21 +00:00
|
|
|
elif opt == "-t":
|
|
|
|
tee_elf = val
|
2017-12-15 03:15:03 +00:00
|
|
|
elif opt == "-h":
|
2018-06-08 08:47:10 +00:00
|
|
|
print(__doc__)
|
2017-12-15 03:15:03 +00:00
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
dtbs = args
|
|
|
|
|
2019-10-06 18:10:21 +00:00
|
|
|
generate_atf_fit_dts(fit_its, bl31_elf, tee_elf, uboot_elf, dtbs)
|
2019-05-07 09:11:01 +00:00
|
|
|
generate_atf_binary(bl31_elf)
|
2019-10-06 18:10:21 +00:00
|
|
|
generate_tee_binary(tee_elf)
|
2017-12-15 03:15:03 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|