3 A script to generate FIT image source for rockchip boards
4 with ARM Trusted Firmware
5 and multiple device trees (given on the command line)
7 usage: $0 <dt_name> [<dt_name> [<dt_name] ...]
14 # pip install pyelftools
15 from elftools.elf.elffile import ELFFile
17 ELF_SEG_P_TYPE='p_type'
18 ELF_SEG_P_PADDR='p_paddr'
19 ELF_SEG_P_VADDR='p_vaddr'
20 ELF_SEG_P_OFFSET='p_offset'
21 ELF_SEG_P_FILESZ='p_filesz'
22 ELF_SEG_P_MEMSZ='p_memsz'
24 DT_HEADER="""// SPDX-License-Identifier: GPL-2.0+ OR X11
26 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
28 * Minimal dts for a SPL FIT image payload.
33 description = "Configuration to load ATF before U-Boot";
38 description = "U-Boot (64-bit)";
39 data = /incbin/("u-boot-nodtb.bin");
48 DT_IMAGES_NODE_END="""
56 def append_atf_node(file, atf_index, phy_addr):
58 Append ATF DT node to input FIT dts file.
60 data = 'bl31_0x%08x.bin' % phy_addr
61 print >> file, '\t\tatf_%d {' % atf_index
62 print >> file, '\t\t\tdescription = \"ARM Trusted Firmware\";'
63 print >> file, '\t\t\tdata = /incbin/("%s");' % data
64 print >> file, '\t\t\ttype = "firmware";'
65 print >> file, '\t\t\tarch = "arm64";'
66 print >> file, '\t\t\tos = "arm-trusted-firmware";'
67 print >> file, '\t\t\tcompression = "none";'
68 print >> file, '\t\t\tload = <0x%08x>;' % phy_addr
70 print >> file, '\t\t\tentry = <0x%08x>;' % phy_addr
71 print >> file, '\t\t};'
74 def append_fdt_node(file, dtbs):
80 dtname = os.path.basename(dtb)
81 print >> file, '\t\tfdt_%d {' % cnt
82 print >> file, '\t\t\tdescription = "%s";' % dtname
83 print >> file, '\t\t\tdata = /incbin/("%s");' % dtb
84 print >> file, '\t\t\ttype = "flat_dt";'
85 print >> file, '\t\t\tcompression = "none";'
86 print >> file, '\t\t};'
90 def append_conf_section(file, cnt, dtname, atf_cnt):
91 print >> file, '\t\tconfig_%d {' % cnt
92 print >> file, '\t\t\tdescription = "%s";' % dtname
93 print >> file, '\t\t\tfirmware = "atf_1";'
94 print >> file, '\t\t\tloadables = "uboot",',
95 for i in range(1, atf_cnt):
96 print >> file, '"atf_%d"' % (i+1),
97 if i != (atf_cnt - 1):
101 print >> file, '\t\t\tfdt = "fdt_1";'
102 print >> file, '\t\t};'
105 def append_conf_node(file, dtbs, atf_cnt):
107 Append configeration nodes.
110 print >> file, '\tconfigurations {'
111 print >> file, '\t\tdefault = "config_1";'
113 dtname = os.path.basename(dtb)
114 append_conf_section(file, cnt, dtname, atf_cnt)
116 print >> file, '\t};'
119 def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name):
121 Generate FIT script for ATF image.
123 if fit_file_name != sys.stdout:
124 fit_file = open(fit_file_name, "wb")
126 fit_file = sys.stdout
130 with open(uboot_file_name) as uboot_file:
131 uboot = ELFFile(uboot_file)
132 for i in range(uboot.num_segments()):
133 seg = uboot.get_segment(i)
134 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
135 p_paddr = seg.__getitem__(ELF_SEG_P_PADDR)
136 num_load_seg = num_load_seg + 1
138 assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1)
140 print >> fit_file, DT_HEADER % p_paddr
142 with open(bl31_file_name) as bl31_file:
143 bl31 = ELFFile(bl31_file)
144 for i in range(bl31.num_segments()):
145 seg = bl31.get_segment(i)
146 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
147 paddr = seg.__getitem__(ELF_SEG_P_PADDR)
148 p= seg.__getitem__(ELF_SEG_P_PADDR)
149 append_atf_node(fit_file, i+1, paddr)
151 append_fdt_node(fit_file, dtbs_file_name)
152 print >> fit_file, '%s' % DT_IMAGES_NODE_END
153 append_conf_node(fit_file, dtbs_file_name, atf_cnt)
154 print >> fit_file, '%s' % DT_END
156 if fit_file_name != sys.stdout:
159 def generate_atf_binary(bl31_file_name):
160 with open(bl31_file_name) as bl31_file:
161 bl31 = ELFFile(bl31_file)
163 num = bl31.num_segments()
165 seg = bl31.get_segment(i)
166 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
167 paddr = seg.__getitem__(ELF_SEG_P_PADDR)
168 file_name = 'bl31_0x%08x.bin' % paddr
169 with open(file_name, "wb") as atf:
170 atf.write(seg.data());
172 def get_bl31_segments_info(bl31_file_name):
174 Get load offset, physical offset, file size
175 from bl31 elf file program headers.
177 with open(bl31_file_name) as bl31_file:
178 bl31 = ELFFile(bl31_file)
180 num = bl31.num_segments()
181 print 'Number of Segments : %d' % bl31.num_segments()
183 print 'Segment %d' % i
184 seg = bl31.get_segment(i)
185 ptype = seg[ELF_SEG_P_TYPE]
186 poffset = seg[ELF_SEG_P_OFFSET]
187 pmemsz = seg[ELF_SEG_P_MEMSZ]
188 pfilesz = seg[ELF_SEG_P_FILESZ]
189 print 'type: %s\nfilesz: %08x\nmemsz: %08x\noffset: %08x' % (ptype, pfilesz, pmemsz, poffset)
190 paddr = seg[ELF_SEG_P_PADDR]
191 print 'paddr: %08x' % paddr
195 bl31_elf="./bl31.elf"
198 opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h")
199 for opt, val in opts:
211 #get_bl31_segments_info("u-boot")
212 #get_bl31_segments_info("bl31.elf")
214 generate_atf_fit_dts(FIT_ITS, bl31_elf, uboot_elf, dtbs)
215 generate_atf_binary(bl31_elf);
217 if __name__ == "__main__":