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'
25 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
27 * Minimal dts for a SPL FIT image payload.
29 * SPDX-License-Identifier: GPL-2.0+ X11
34 description = "Configuration to load ATF before U-Boot";
39 description = "U-Boot (64-bit)";
40 data = /incbin/("u-boot-nodtb.bin");
49 DT_IMAGES_NODE_END="""
57 def append_atf_node(file, atf_index, phy_addr):
59 Append ATF DT node to input FIT dts file.
61 data = 'bl31_0x%08x.bin' % phy_addr
62 print >> file, '\t\tatf@%d {' % atf_index
63 print >> file, '\t\t\tdescription = \"ARM Trusted Firmware\";'
64 print >> file, '\t\t\tdata = /incbin/("%s");' % data
65 print >> file, '\t\t\ttype = "firmware";'
66 print >> file, '\t\t\tarch = "arm64";'
67 print >> file, '\t\t\tos = "arm-trusted-firmware";'
68 print >> file, '\t\t\tcompression = "none";'
69 print >> file, '\t\t\tload = <0x%08x>;' % phy_addr
71 print >> file, '\t\t\tentry = <0x%08x>;' % phy_addr
72 print >> file, '\t\t};'
75 def append_fdt_node(file, dtbs):
81 dtname = os.path.basename(dtb)
82 print >> file, '\t\tfdt@%d {' % cnt
83 print >> file, '\t\t\tdescription = "%s";' % dtname
84 print >> file, '\t\t\tdata = /incbin/("%s");' % dtb
85 print >> file, '\t\t\ttype = "flat_dt";'
86 print >> file, '\t\t\tcompression = "none";'
87 print >> file, '\t\t};'
91 def append_conf_section(file, cnt, dtname, atf_cnt):
92 print >> file, '\t\tconfig@%d {' % cnt
93 print >> file, '\t\t\tdescription = "%s";' % dtname
94 print >> file, '\t\t\tfirmware = "atf@1";'
95 print >> file, '\t\t\tloadables = "uboot@1",',
96 for i in range(1, atf_cnt):
97 print >> file, '"atf@%d"' % (i+1),
98 if i != (atf_cnt - 1):
102 print >> file, '\t\t\tfdt = "fdt@1";'
103 print >> file, '\t\t};'
106 def append_conf_node(file, dtbs, atf_cnt):
108 Append configeration nodes.
111 print >> file, '\tconfigurations {'
112 print >> file, '\t\tdefault = "config@1";'
114 dtname = os.path.basename(dtb)
115 append_conf_section(file, cnt, dtname, atf_cnt)
117 print >> file, '\t};'
120 def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name):
122 Generate FIT script for ATF image.
124 if fit_file_name != sys.stdout:
125 fit_file = open(fit_file_name, "wb")
127 fit_file = sys.stdout
131 with open(uboot_file_name) as uboot_file:
132 uboot = ELFFile(uboot_file)
133 for i in range(uboot.num_segments()):
134 seg = uboot.get_segment(i)
135 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
136 p_paddr = seg.__getitem__(ELF_SEG_P_PADDR)
137 num_load_seg = num_load_seg + 1
139 assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1)
141 print >> fit_file, DT_HEADER % p_paddr
143 with open(bl31_file_name) as bl31_file:
144 bl31 = ELFFile(bl31_file)
145 for i in range(bl31.num_segments()):
146 seg = bl31.get_segment(i)
147 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
148 paddr = seg.__getitem__(ELF_SEG_P_PADDR)
149 p= seg.__getitem__(ELF_SEG_P_PADDR)
150 append_atf_node(fit_file, i+1, paddr)
152 append_fdt_node(fit_file, dtbs_file_name)
153 print >> fit_file, '%s' % DT_IMAGES_NODE_END
154 append_conf_node(fit_file, dtbs_file_name, atf_cnt)
155 print >> fit_file, '%s' % DT_END
157 if fit_file_name != sys.stdout:
160 def generate_atf_binary(bl31_file_name):
161 with open(bl31_file_name) as bl31_file:
162 bl31 = ELFFile(bl31_file)
164 num = bl31.num_segments()
166 seg = bl31.get_segment(i)
167 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)):
168 paddr = seg.__getitem__(ELF_SEG_P_PADDR)
169 file_name = 'bl31_0x%08x.bin' % paddr
170 with open(file_name, "wb") as atf:
171 atf.write(seg.data());
173 def get_bl31_segments_info(bl31_file_name):
175 Get load offset, physical offset, file size
176 from bl31 elf file program headers.
178 with open(bl31_file_name) as bl31_file:
179 bl31 = ELFFile(bl31_file)
181 num = bl31.num_segments()
182 print 'Number of Segments : %d' % bl31.num_segments()
184 print 'Segment %d' % i
185 seg = bl31.get_segment(i)
186 ptype = seg[ELF_SEG_P_TYPE]
187 poffset = seg[ELF_SEG_P_OFFSET]
188 pmemsz = seg[ELF_SEG_P_MEMSZ]
189 pfilesz = seg[ELF_SEG_P_FILESZ]
190 print 'type: %s\nfilesz: %08x\nmemsz: %08x\noffset: %08x' % (ptype, pfilesz, pmemsz, poffset)
191 paddr = seg[ELF_SEG_P_PADDR]
192 print 'paddr: %08x' % paddr
196 bl31_elf="./bl31.elf"
199 opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h")
200 for opt, val in opts:
212 #get_bl31_segments_info("u-boot")
213 #get_bl31_segments_info("bl31.elf")
215 generate_atf_fit_dts(FIT_ITS, bl31_elf, uboot_elf, dtbs)
216 generate_atf_binary(bl31_elf);
218 if __name__ == "__main__":