1#!/usr/bin/env python2 2""" 3A script to generate FIT image source for rockchip boards 4with ARM Trusted Firmware 5and multiple device trees (given on the command line) 6 7usage: $0 <dt_name> [<dt_name> [<dt_name] ...] 8""" 9 10import os 11import sys 12import getopt 13 14# pip install pyelftools 15from elftools.elf.elffile import ELFFile 16 17ELF_SEG_P_TYPE='p_type' 18ELF_SEG_P_PADDR='p_paddr' 19ELF_SEG_P_VADDR='p_vaddr' 20ELF_SEG_P_OFFSET='p_offset' 21ELF_SEG_P_FILESZ='p_filesz' 22ELF_SEG_P_MEMSZ='p_memsz' 23 24DT_HEADER="""/* 25 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd 26 * 27 * Minimal dts for a SPL FIT image payload. 28 * 29 * SPDX-License-Identifier: GPL-2.0+ X11 30 */ 31/dts-v1/; 32 33/ { 34 description = "Configuration to load ATF before U-Boot"; 35 #address-cells = <1>; 36 37 images { 38 uboot@1 { 39 description = "U-Boot (64-bit)"; 40 data = /incbin/("u-boot-nodtb.bin"); 41 type = "standalone"; 42 os = "U-Boot"; 43 arch = "arm64"; 44 compression = "none"; 45 load = <0x%08x>; 46 }; 47""" 48 49DT_IMAGES_NODE_END=""" 50 }; 51""" 52 53DT_END=""" 54}; 55""" 56 57def append_atf_node(file, atf_index, phy_addr): 58 """ 59 Append ATF DT node to input FIT dts file. 60 """ 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 70 if atf_index == 1: 71 print >> file, '\t\t\tentry = <0x%08x>;' % phy_addr 72 print >> file, '\t\t};' 73 print >> file, '' 74 75def append_fdt_node(file, dtbs): 76 """ 77 Append FDT nodes. 78 """ 79 cnt = 1 80 for dtb in 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};' 88 print >> file, '' 89 cnt = cnt + 1 90 91def 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): 99 print >> file, ',', 100 else: 101 print >> file, ';' 102 print >> file, '\t\t\tfdt = "fdt@1";' 103 print >> file, '\t\t};' 104 print >> file, '' 105 106def append_conf_node(file, dtbs, atf_cnt): 107 """ 108 Append configeration nodes. 109 """ 110 cnt = 1 111 print >> file, '\tconfigurations {' 112 print >> file, '\t\tdefault = "config@1";' 113 for dtb in dtbs: 114 dtname = os.path.basename(dtb) 115 append_conf_section(file, cnt, dtname, atf_cnt) 116 cnt = cnt + 1 117 print >> file, '\t};' 118 print >> file, '' 119 120def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name): 121 """ 122 Generate FIT script for ATF image. 123 """ 124 if fit_file_name != sys.stdout: 125 fit_file = open(fit_file_name, "wb") 126 else: 127 fit_file = sys.stdout 128 129 num_load_seg = 0 130 p_paddr = 0xFFFFFFFF 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 138 139 assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1) 140 141 print >> fit_file, DT_HEADER % p_paddr 142 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) 151 atf_cnt = i+1 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 156 157 if fit_file_name != sys.stdout: 158 fit_file.close() 159 160def generate_atf_binary(bl31_file_name): 161 with open(bl31_file_name) as bl31_file: 162 bl31 = ELFFile(bl31_file) 163 164 num = bl31.num_segments() 165 for i in range(num): 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()); 172 173def get_bl31_segments_info(bl31_file_name): 174 """ 175 Get load offset, physical offset, file size 176 from bl31 elf file program headers. 177 """ 178 with open(bl31_file_name) as bl31_file: 179 bl31 = ELFFile(bl31_file) 180 181 num = bl31.num_segments() 182 print 'Number of Segments : %d' % bl31.num_segments() 183 for i in range(num): 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 193 194def main(): 195 uboot_elf="./u-boot" 196 bl31_elf="./bl31.elf" 197 FIT_ITS=sys.stdout 198 199 opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h") 200 for opt, val in opts: 201 if opt == "-o": 202 FIT_ITS=val 203 elif opt == "-u": 204 uboot_elf=val 205 elif opt == "-b": 206 bl31_elf=val 207 elif opt == "-h": 208 print __doc__ 209 sys.exit(2) 210 211 dtbs = args 212 #get_bl31_segments_info("u-boot") 213 #get_bl31_segments_info("bl31.elf") 214 215 generate_atf_fit_dts(FIT_ITS, bl31_elf, uboot_elf, dtbs) 216 generate_atf_binary(bl31_elf); 217 218if __name__ == "__main__": 219 main() 220