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 16from elftools.elf.sections import SymbolTableSection 17from elftools.elf.segments import Segment, InterpSegment, NoteSegment 18 19ELF_SEG_P_TYPE='p_type' 20ELF_SEG_P_PADDR='p_paddr' 21ELF_SEG_P_VADDR='p_vaddr' 22ELF_SEG_P_OFFSET='p_offset' 23ELF_SEG_P_FILESZ='p_filesz' 24ELF_SEG_P_MEMSZ='p_memsz' 25 26DT_HEADER="""/* 27 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd 28 * 29 * Minimal dts for a SPL FIT image payload. 30 * 31 * SPDX-License-Identifier: GPL-2.0+ X11 32 */ 33/dts-v1/; 34 35/ { 36 description = "Configuration to load ATF before U-Boot"; 37 #address-cells = <1>; 38 39 images { 40 uboot@1 { 41 description = "U-Boot (64-bit)"; 42 data = /incbin/("u-boot-nodtb.bin"); 43 type = "standalone"; 44 os = "U-Boot"; 45 arch = "arm64"; 46 compression = "none"; 47 load = <0x%08x>; 48 hash@1 { 49 algo = "sha256"; 50 }; 51 }; 52""" 53 54DT_IMAGES_NODE_END=""" 55 }; 56""" 57 58DT_END=""" 59}; 60""" 61 62def append_atf_node(file, atf_index, phy_addr): 63 """ 64 Append ATF DT node to input FIT dts file. 65 """ 66 data = 'bl31_0x%08x.bin' % phy_addr 67 print >> file, '\t\tatf@%d {' % atf_index 68 print >> file, '\t\t\tdescription = \"ARM Trusted Firmware\";' 69 print >> file, '\t\t\tdata = /incbin/("%s");' % data 70 print >> file, '\t\t\ttype = "firmware";' 71 print >> file, '\t\t\tarch = "arm64";' 72 print >> file, '\t\t\tos = "arm-trusted-firmware";' 73 print >> file, '\t\t\tcompression = "none";' 74 print >> file, '\t\t\tload = <0x%08x>;' % phy_addr 75 if atf_index == 1: 76 print >> file, '\t\t\tentry = <0x%08x>;' % phy_addr 77 print >> file, '\t\t\thash@1 {' 78 print >> file, '\t\t\t\talgo = "sha256";' 79 print >> file, '\t\t\t};' 80 print >> file, '\t\t};' 81 print >> file, '' 82 83def append_fdt_node(file, dtbs): 84 """ 85 Append FDT nodes. 86 """ 87 cnt = 1 88 for dtb in dtbs: 89 dtname = os.path.basename(dtb) 90 print >> file, '\t\tfdt@%d {' % cnt 91 print >> file, '\t\t\tdescription = "U-Boot device tree blob";' 92 print >> file, '\t\t\tdata = /incbin/("u-boot.dtb");' 93 print >> file, '\t\t\ttype = "flat_dt";' 94 print >> file, '\t\t\tcompression = "none";' 95 print >> file, '\t\t\thash@1 {' 96 print >> file, '\t\t\t\talgo = "sha256";' 97 print >> file, '\t\t\t};' 98 print >> file, '\t\t};' 99 print >> file, '' 100 cnt = cnt + 1 101 102def append_conf_section(file, cnt, dtname, atf_cnt): 103 print >> file, '\t\tconfig@%d {' % cnt 104 print >> file, '\t\t\tdescription = "%s";' % dtname 105 print >> file, '\t\t\tfirmware = "atf@1";' 106 print >> file, '\t\t\tloadables = "uboot@1",', 107 for i in range(1, atf_cnt): 108 print >> file, '"atf@%d"' % (i+1), 109 if i != (atf_cnt - 1): 110 print >> file, ',', 111 else: 112 print >> file, ';' 113 print >> file, '\t\t\tfdt = "fdt@1";' 114 print >> file, '\t\t\tsignature@1 {' 115 print >> file, '\t\t\t\talgo = "sha256,rsa2048";' 116 print >> file, '\t\t\t\tkey-name-hint = "dev";' 117 print >> file, '\t\t\t\tsign-images = "fdt", "firmware", "loadables";' 118 print >> file, '\t\t\t};' 119 print >> file, '\t\t};' 120 print >> file, '' 121 122def append_conf_node(file, dtbs, atf_cnt): 123 """ 124 Append configeration nodes. 125 """ 126 cnt = 1 127 print >> file, '\tconfigurations {' 128 print >> file, '\t\tdefault = "config@1";' 129 for dtb in dtbs: 130 dtname = os.path.basename(dtb) 131 append_conf_section(file, cnt, dtname, atf_cnt) 132 cnt = cnt + 1 133 print >> file, '\t};' 134 print >> file, '' 135 136def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name): 137 """ 138 Generate FIT script for ATF image. 139 """ 140 if fit_file_name != sys.stdout: 141 fit_file = open(fit_file_name, "wb") 142 else: 143 fit_file = sys.stdout 144 145 num_load_seg = 0 146 p_paddr = 0xFFFFFFFF 147 with open(uboot_file_name) as uboot_file: 148 uboot = ELFFile(uboot_file) 149 for i in range(uboot.num_segments()): 150 seg = uboot.get_segment(i) 151 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)): 152 p_paddr = seg.__getitem__(ELF_SEG_P_PADDR) 153 num_load_seg = num_load_seg + 1 154 155 assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1) 156 157 print >> fit_file, DT_HEADER % p_paddr 158 159 with open(bl31_file_name) as bl31_file: 160 bl31 = ELFFile(bl31_file) 161 for i in range(bl31.num_segments()): 162 seg = bl31.get_segment(i) 163 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)): 164 paddr = seg.__getitem__(ELF_SEG_P_PADDR) 165 p= seg.__getitem__(ELF_SEG_P_PADDR) 166 append_atf_node(fit_file, i+1, paddr) 167 atf_cnt = i+1 168 append_fdt_node(fit_file, dtbs_file_name) 169 print >> fit_file, '%s' % DT_IMAGES_NODE_END 170 append_conf_node(fit_file, dtbs_file_name, atf_cnt) 171 print >> fit_file, '%s' % DT_END 172 173 if fit_file_name != sys.stdout: 174 fit_file.close() 175 176def generate_atf_binary(bl31_file_name): 177 with open(bl31_file_name) as bl31_file: 178 bl31 = ELFFile(bl31_file) 179 180 num = bl31.num_segments() 181 for i in range(num): 182 seg = bl31.get_segment(i) 183 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)): 184 paddr = seg.__getitem__(ELF_SEG_P_PADDR) 185 file_name = 'bl31_0x%08x.bin' % paddr 186 with open(file_name, "wb") as atf: 187 atf.write(seg.data()); 188 189def get_bl31_segments_info(bl31_file_name): 190 """ 191 Get load offset, physical offset, file size 192 from bl31 elf file program headers. 193 """ 194 with open(bl31_file_name) as bl31_file: 195 bl31 = ELFFile(bl31_file) 196 197 num = bl31.num_segments() 198 print 'Number of Segments : %d' % bl31.num_segments() 199 for i in range(num): 200 print 'Segment %d' % i 201 seg = bl31.get_segment(i) 202 ptype = seg[ELF_SEG_P_TYPE] 203 poffset = seg[ELF_SEG_P_OFFSET] 204 pmemsz = seg[ELF_SEG_P_MEMSZ] 205 pfilesz = seg[ELF_SEG_P_FILESZ] 206 print 'type: %s\nfilesz: %08x\nmemsz: %08x\noffset: %08x' % (ptype, pfilesz, pmemsz, poffset) 207 paddr = seg[ELF_SEG_P_PADDR] 208 print 'paddr: %08x' % paddr 209 210def main(): 211 uboot_elf="./u-boot" 212 bl31_elf="./bl31.elf" 213 FIT_ITS=sys.stdout 214 215 opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h") 216 for opt, val in opts: 217 if opt == "-o": 218 FIT_ITS=val 219 elif opt == "-u": 220 uboot_elf=val 221 elif opt == "-b": 222 bl31_elf=val 223 elif opt == "-h": 224 print __doc__ 225 sys.exit(2) 226 227 dtbs = args 228 #get_bl31_segments_info("u-boot") 229 #get_bl31_segments_info("bl31.elf") 230 231 generate_atf_fit_dts(FIT_ITS, bl31_elf, uboot_elf, dtbs) 232 generate_atf_binary(bl31_elf); 233 234if __name__ == "__main__": 235 main() 236