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 { 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 { 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 {' 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 {' 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\tarch = "arm64";' 95 print >> file, '\t\t\tcompression = "none";' 96 print >> file, '\t\t\thash {' 97 print >> file, '\t\t\t\talgo = "sha256";' 98 print >> file, '\t\t\t};' 99 print >> file, '\t\t};' 100 print >> file, '' 101 cnt = cnt + 1 102 103def append_conf_section(file, cnt, dtname, atf_cnt): 104 print >> file, '\t\tconfig {' 105 print >> file, '\t\t\tdescription = "Rockchip armv8 with ATF";' 106 print >> file, '\t\t\trollback-index = <0x0>;' 107 print >> file, '\t\t\tfirmware = "atf@1";' 108 print >> file, '\t\t\tloadables = "uboot",', 109 for i in range(1, atf_cnt): 110 print >> file, '"atf@%d"' % (i+1), 111 if i != (atf_cnt - 1): 112 print >> file, ',', 113 else: 114 print >> file, ';' 115 print >> file, '\t\t\tfdt = "fdt";' 116 print >> file, '\t\t\tsignature {' 117 print >> file, '\t\t\t\talgo = "sha256,rsa2048";' 118 print >> file, '\t\t\t\tpadding = "pss";' 119 print >> file, '\t\t\t\tkey-name-hint = "dev";' 120 print >> file, '\t\t\t\tsign-images = "fdt", "firmware", "loadables";' 121 print >> file, '\t\t\t};' 122 print >> file, '\t\t};' 123 print >> file, '' 124 125def append_conf_node(file, dtbs, atf_cnt): 126 """ 127 Append configeration nodes. 128 """ 129 cnt = 1 130 print >> file, '\tconfigurations {' 131 print >> file, '\t\tdefault = "config";' 132 for dtb in dtbs: 133 dtname = os.path.basename(dtb) 134 append_conf_section(file, cnt, dtname, atf_cnt) 135 cnt = cnt + 1 136 print >> file, '\t};' 137 print >> file, '' 138 139def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name): 140 """ 141 Generate FIT script for ATF image. 142 """ 143 if fit_file_name != sys.stdout: 144 fit_file = open(fit_file_name, "wb") 145 else: 146 fit_file = sys.stdout 147 148 num_load_seg = 0 149 p_paddr = 0xFFFFFFFF 150 with open(uboot_file_name) as uboot_file: 151 uboot = ELFFile(uboot_file) 152 for i in range(uboot.num_segments()): 153 seg = uboot.get_segment(i) 154 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)): 155 p_paddr = seg.__getitem__(ELF_SEG_P_PADDR) 156 num_load_seg = num_load_seg + 1 157 158 assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1) 159 160 print >> fit_file, DT_HEADER % p_paddr 161 162 with open(bl31_file_name) as bl31_file: 163 bl31 = ELFFile(bl31_file) 164 for i in range(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 p= seg.__getitem__(ELF_SEG_P_PADDR) 169 append_atf_node(fit_file, i+1, paddr) 170 atf_cnt = i+1 171 append_fdt_node(fit_file, dtbs_file_name) 172 print >> fit_file, '%s' % DT_IMAGES_NODE_END 173 append_conf_node(fit_file, dtbs_file_name, atf_cnt) 174 print >> fit_file, '%s' % DT_END 175 176 if fit_file_name != sys.stdout: 177 fit_file.close() 178 179def generate_atf_binary(bl31_file_name): 180 with open(bl31_file_name) as bl31_file: 181 bl31 = ELFFile(bl31_file) 182 183 num = bl31.num_segments() 184 for i in range(num): 185 seg = bl31.get_segment(i) 186 if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)): 187 paddr = seg.__getitem__(ELF_SEG_P_PADDR) 188 file_name = 'bl31_0x%08x.bin' % paddr 189 with open(file_name, "wb") as atf: 190 atf.write(seg.data()); 191 192def get_bl31_segments_info(bl31_file_name): 193 """ 194 Get load offset, physical offset, file size 195 from bl31 elf file program headers. 196 """ 197 with open(bl31_file_name) as bl31_file: 198 bl31 = ELFFile(bl31_file) 199 200 num = bl31.num_segments() 201 print 'Number of Segments : %d' % bl31.num_segments() 202 for i in range(num): 203 print 'Segment %d' % i 204 seg = bl31.get_segment(i) 205 ptype = seg[ELF_SEG_P_TYPE] 206 poffset = seg[ELF_SEG_P_OFFSET] 207 pmemsz = seg[ELF_SEG_P_MEMSZ] 208 pfilesz = seg[ELF_SEG_P_FILESZ] 209 print 'type: %s\nfilesz: %08x\nmemsz: %08x\noffset: %08x' % (ptype, pfilesz, pmemsz, poffset) 210 paddr = seg[ELF_SEG_P_PADDR] 211 print 'paddr: %08x' % paddr 212 213def main(): 214 uboot_elf="./u-boot" 215 bl31_elf="./bl31.elf" 216 FIT_ITS=sys.stdout 217 218 opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h") 219 for opt, val in opts: 220 if opt == "-o": 221 FIT_ITS=val 222 elif opt == "-u": 223 uboot_elf=val 224 elif opt == "-b": 225 bl31_elf=val 226 elif opt == "-h": 227 print __doc__ 228 sys.exit(2) 229 230 dtbs = args 231 #get_bl31_segments_info("u-boot") 232 #get_bl31_segments_info("bl31.elf") 233 234 generate_atf_fit_dts(FIT_ITS, bl31_elf, uboot_elf, dtbs) 235 generate_atf_binary(bl31_elf); 236 237if __name__ == "__main__": 238 main() 239