2018-06-08 08:47:10 +00:00
|
|
|
#!/usr/bin/env python
|
2017-12-15 03:15:03 +00:00
|
|
|
"""
|
|
|
|
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] ...]
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import getopt
|
|
|
|
|
|
|
|
# pip install pyelftools
|
|
|
|
from elftools.elf.elffile import ELFFile
|
|
|
|
|
|
|
|
ELF_SEG_P_TYPE='p_type'
|
|
|
|
ELF_SEG_P_PADDR='p_paddr'
|
|
|
|
ELF_SEG_P_VADDR='p_vaddr'
|
|
|
|
ELF_SEG_P_OFFSET='p_offset'
|
|
|
|
ELF_SEG_P_FILESZ='p_filesz'
|
|
|
|
ELF_SEG_P_MEMSZ='p_memsz'
|
|
|
|
|
2018-05-06 21:58:06 +00:00
|
|
|
DT_HEADER="""// SPDX-License-Identifier: GPL-2.0+ OR X11
|
|
|
|
/*
|
2017-12-15 03:15:03 +00:00
|
|
|
* Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
|
|
|
|
*
|
|
|
|
* Minimal dts for a SPL FIT image payload.
|
|
|
|
*/
|
|
|
|
/dts-v1/;
|
|
|
|
|
|
|
|
/ {
|
|
|
|
description = "Configuration to load ATF before U-Boot";
|
|
|
|
#address-cells = <1>;
|
|
|
|
|
|
|
|
images {
|
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
|
|
|
"""
|
|
|
|
|
|
|
|
DT_IMAGES_NODE_END="""
|
|
|
|
};
|
|
|
|
"""
|
|
|
|
|
|
|
|
DT_END="""
|
|
|
|
};
|
|
|
|
"""
|
|
|
|
|
2018-06-08 08:47:09 +00:00
|
|
|
def append_atf_node(file, atf_index, phy_addr, elf_entry):
|
2017-12-15 03:15:03 +00:00
|
|
|
"""
|
|
|
|
Append ATF DT node to input FIT dts file.
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
def append_fdt_node(file, dtbs):
|
|
|
|
"""
|
|
|
|
Append FDT nodes.
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
def append_conf_section(file, cnt, dtname, atf_cnt):
|
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')
|
|
|
|
file.write('\t\t\tloadables = "uboot",')
|
2017-12-15 03:15:03 +00:00
|
|
|
for i in range(1, atf_cnt):
|
2018-06-08 08:47:10 +00:00
|
|
|
file.write('"atf_%d"' % (i+1))
|
2017-12-15 03:15:03 +00:00
|
|
|
if i != (atf_cnt - 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')
|
|
|
|
file.write('\t\t\tfdt = "fdt_1";\n')
|
|
|
|
file.write('\t\t};\n')
|
|
|
|
file.write('\n')
|
2017-12-15 03:15:03 +00:00
|
|
|
|
|
|
|
def append_conf_node(file, dtbs, atf_cnt):
|
|
|
|
"""
|
|
|
|
Append configeration nodes.
|
|
|
|
"""
|
|
|
|
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)
|
|
|
|
append_conf_section(file, cnt, dtname, atf_cnt)
|
|
|
|
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
|
|
|
|
|
|
|
def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name):
|
|
|
|
"""
|
|
|
|
Generate FIT script for ATF image.
|
|
|
|
"""
|
|
|
|
if fit_file_name != sys.stdout:
|
|
|
|
fit_file = open(fit_file_name, "wb")
|
|
|
|
else:
|
|
|
|
fit_file = sys.stdout
|
|
|
|
|
|
|
|
num_load_seg = 0
|
|
|
|
p_paddr = 0xFFFFFFFF
|
2018-06-08 08:47:10 +00:00
|
|
|
with open(uboot_file_name, 'rb') as uboot_file:
|
2017-12-15 03:15:03 +00:00
|
|
|
uboot = ELFFile(uboot_file)
|
|
|
|
for i in range(uboot.num_segments()):
|
|
|
|
seg = uboot.get_segment(i)
|
|
|
|
if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
|
|
|
|
p_paddr = seg.__getitem__(ELF_SEG_P_PADDR)
|
|
|
|
num_load_seg = num_load_seg + 1
|
|
|
|
|
|
|
|
assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1)
|
|
|
|
|
2018-06-08 08:47:10 +00:00
|
|
|
fit_file.write(DT_HEADER % p_paddr)
|
2017-12-15 03:15:03 +00:00
|
|
|
|
2018-06-08 08:47:10 +00:00
|
|
|
with open(bl31_file_name, 'rb') as bl31_file:
|
2017-12-15 03:15:03 +00:00
|
|
|
bl31 = ELFFile(bl31_file)
|
2018-06-08 08:47:09 +00:00
|
|
|
elf_entry = bl31.header['e_entry']
|
2017-12-15 03:15:03 +00:00
|
|
|
for i in range(bl31.num_segments()):
|
|
|
|
seg = bl31.get_segment(i)
|
|
|
|
if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
|
|
|
|
paddr = seg.__getitem__(ELF_SEG_P_PADDR)
|
|
|
|
p= seg.__getitem__(ELF_SEG_P_PADDR)
|
2018-06-08 08:47:09 +00:00
|
|
|
append_atf_node(fit_file, i+1, paddr, elf_entry)
|
2017-12-15 03:15:03 +00:00
|
|
|
atf_cnt = i+1
|
|
|
|
append_fdt_node(fit_file, dtbs_file_name)
|
2018-06-08 08:47:10 +00:00
|
|
|
fit_file.write('%s\n' % DT_IMAGES_NODE_END)
|
2017-12-15 03:15:03 +00:00
|
|
|
append_conf_node(fit_file, dtbs_file_name, atf_cnt)
|
2018-06-08 08:47:10 +00:00
|
|
|
fit_file.write('%s\n' % 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):
|
2018-06-08 08:47:10 +00:00
|
|
|
with open(bl31_file_name, 'rb') as bl31_file:
|
2017-12-15 03:15:03 +00:00
|
|
|
bl31 = ELFFile(bl31_file)
|
|
|
|
|
|
|
|
num = bl31.num_segments()
|
|
|
|
for i in range(num):
|
|
|
|
seg = bl31.get_segment(i)
|
|
|
|
if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
|
|
|
|
paddr = seg.__getitem__(ELF_SEG_P_PADDR)
|
|
|
|
file_name = 'bl31_0x%08x.bin' % paddr
|
|
|
|
with open(file_name, "wb") as atf:
|
|
|
|
atf.write(seg.data());
|
|
|
|
|
|
|
|
def get_bl31_segments_info(bl31_file_name):
|
|
|
|
"""
|
|
|
|
Get load offset, physical offset, file size
|
|
|
|
from bl31 elf file program headers.
|
|
|
|
"""
|
|
|
|
with open(bl31_file_name) as bl31_file:
|
|
|
|
bl31 = ELFFile(bl31_file)
|
|
|
|
|
|
|
|
num = bl31.num_segments()
|
2018-06-08 08:47:10 +00:00
|
|
|
print('Number of Segments : %d' % bl31.num_segments())
|
2017-12-15 03:15:03 +00:00
|
|
|
for i in range(num):
|
2018-06-08 08:47:10 +00:00
|
|
|
print('Segment %d' % i)
|
2017-12-15 03:15:03 +00:00
|
|
|
seg = bl31.get_segment(i)
|
|
|
|
ptype = seg[ELF_SEG_P_TYPE]
|
|
|
|
poffset = seg[ELF_SEG_P_OFFSET]
|
|
|
|
pmemsz = seg[ELF_SEG_P_MEMSZ]
|
|
|
|
pfilesz = seg[ELF_SEG_P_FILESZ]
|
2018-06-08 08:47:10 +00:00
|
|
|
print('type: %s\nfilesz: %08x\nmemsz: %08x\noffset: %08x' % (ptype, pfilesz, pmemsz, poffset))
|
2017-12-15 03:15:03 +00:00
|
|
|
paddr = seg[ELF_SEG_P_PADDR]
|
2018-06-08 08:47:10 +00:00
|
|
|
print('paddr: %08x' % paddr)
|
2017-12-15 03:15:03 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
uboot_elf="./u-boot"
|
|
|
|
bl31_elf="./bl31.elf"
|
|
|
|
FIT_ITS=sys.stdout
|
|
|
|
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h")
|
|
|
|
for opt, val in opts:
|
|
|
|
if opt == "-o":
|
|
|
|
FIT_ITS=val
|
|
|
|
elif opt == "-u":
|
|
|
|
uboot_elf=val
|
|
|
|
elif opt == "-b":
|
|
|
|
bl31_elf=val
|
|
|
|
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
|
|
|
|
#get_bl31_segments_info("u-boot")
|
|
|
|
#get_bl31_segments_info("bl31.elf")
|
|
|
|
|
|
|
|
generate_atf_fit_dts(FIT_ITS, bl31_elf, uboot_elf, dtbs)
|
|
|
|
generate_atf_binary(bl31_elf);
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|