Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 2 | """ |
| 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) |
| 6 | |
| 7 | usage: $0 <dt_name> [<dt_name> [<dt_name] ...] |
| 8 | """ |
| 9 | |
| 10 | import os |
| 11 | import sys |
| 12 | import getopt |
| 13 | |
| 14 | # pip install pyelftools |
| 15 | from elftools.elf.elffile import ELFFile |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 16 | |
| 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' |
| 23 | |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 24 | DT_HEADER="""// SPDX-License-Identifier: GPL-2.0+ OR X11 |
| 25 | /* |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 26 | * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd |
| 27 | * |
| 28 | * Minimal dts for a SPL FIT image payload. |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 29 | */ |
| 30 | /dts-v1/; |
| 31 | |
| 32 | / { |
| 33 | description = "Configuration to load ATF before U-Boot"; |
| 34 | #address-cells = <1>; |
| 35 | |
| 36 | images { |
Kever Yang | 29c8c74 | 2018-08-23 11:01:08 +0800 | [diff] [blame] | 37 | uboot { |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 38 | description = "U-Boot (64-bit)"; |
| 39 | data = /incbin/("u-boot-nodtb.bin"); |
| 40 | type = "standalone"; |
| 41 | os = "U-Boot"; |
| 42 | arch = "arm64"; |
| 43 | compression = "none"; |
| 44 | load = <0x%08x>; |
| 45 | }; |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 46 | |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 47 | """ |
| 48 | |
| 49 | DT_IMAGES_NODE_END=""" |
| 50 | }; |
| 51 | """ |
| 52 | |
| 53 | DT_END=""" |
| 54 | }; |
| 55 | """ |
| 56 | |
Mian Yousaf Kaukab | 080e386 | 2018-06-08 10:47:09 +0200 | [diff] [blame] | 57 | def append_atf_node(file, atf_index, phy_addr, elf_entry): |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 58 | """ |
| 59 | Append ATF DT node to input FIT dts file. |
| 60 | """ |
| 61 | data = 'bl31_0x%08x.bin' % phy_addr |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 62 | file.write('\t\tatf_%d {\n' % atf_index) |
| 63 | file.write('\t\t\tdescription = \"ARM Trusted Firmware\";\n') |
| 64 | file.write('\t\t\tdata = /incbin/("%s");\n' % data) |
| 65 | file.write('\t\t\ttype = "firmware";\n') |
| 66 | file.write('\t\t\tarch = "arm64";\n') |
| 67 | file.write('\t\t\tos = "arm-trusted-firmware";\n') |
| 68 | file.write('\t\t\tcompression = "none";\n') |
| 69 | file.write('\t\t\tload = <0x%08x>;\n' % phy_addr) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 70 | if atf_index == 1: |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 71 | file.write('\t\t\tentry = <0x%08x>;\n' % elf_entry) |
| 72 | file.write('\t\t};\n') |
| 73 | file.write('\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 74 | |
| 75 | def 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) |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 82 | file.write('\t\tfdt_%d {\n' % cnt) |
| 83 | file.write('\t\t\tdescription = "%s";\n' % dtname) |
| 84 | file.write('\t\t\tdata = /incbin/("%s");\n' % dtb) |
| 85 | file.write('\t\t\ttype = "flat_dt";\n') |
| 86 | file.write('\t\t\tcompression = "none";\n') |
| 87 | file.write('\t\t};\n') |
| 88 | file.write('\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 89 | cnt = cnt + 1 |
| 90 | |
| 91 | def append_conf_section(file, cnt, dtname, atf_cnt): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 92 | file.write('\t\tconfig_%d {\n' % cnt) |
| 93 | file.write('\t\t\tdescription = "%s";\n' % dtname) |
| 94 | file.write('\t\t\tfirmware = "atf_1";\n') |
| 95 | file.write('\t\t\tloadables = "uboot",') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 96 | for i in range(1, atf_cnt): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 97 | file.write('"atf_%d"' % (i+1)) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 98 | if i != (atf_cnt - 1): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 99 | file.write(',') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 100 | else: |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 101 | file.write(';\n') |
| 102 | file.write('\t\t\tfdt = "fdt_1";\n') |
| 103 | file.write('\t\t};\n') |
| 104 | file.write('\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 105 | |
| 106 | def append_conf_node(file, dtbs, atf_cnt): |
| 107 | """ |
| 108 | Append configeration nodes. |
| 109 | """ |
| 110 | cnt = 1 |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 111 | file.write('\tconfigurations {\n') |
| 112 | file.write('\t\tdefault = "config_1";\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 113 | for dtb in dtbs: |
| 114 | dtname = os.path.basename(dtb) |
| 115 | append_conf_section(file, cnt, dtname, atf_cnt) |
| 116 | cnt = cnt + 1 |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 117 | file.write('\t};\n') |
| 118 | file.write('\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 119 | |
| 120 | def 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 |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 131 | with open(uboot_file_name, 'rb') as uboot_file: |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 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 | |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 141 | fit_file.write(DT_HEADER % p_paddr) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 142 | |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 143 | with open(bl31_file_name, 'rb') as bl31_file: |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 144 | bl31 = ELFFile(bl31_file) |
Mian Yousaf Kaukab | 080e386 | 2018-06-08 10:47:09 +0200 | [diff] [blame] | 145 | elf_entry = bl31.header['e_entry'] |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 146 | for i in range(bl31.num_segments()): |
| 147 | seg = bl31.get_segment(i) |
| 148 | if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)): |
| 149 | paddr = seg.__getitem__(ELF_SEG_P_PADDR) |
| 150 | p= seg.__getitem__(ELF_SEG_P_PADDR) |
Mian Yousaf Kaukab | 080e386 | 2018-06-08 10:47:09 +0200 | [diff] [blame] | 151 | append_atf_node(fit_file, i+1, paddr, elf_entry) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 152 | atf_cnt = i+1 |
| 153 | append_fdt_node(fit_file, dtbs_file_name) |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 154 | fit_file.write('%s\n' % DT_IMAGES_NODE_END) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 155 | append_conf_node(fit_file, dtbs_file_name, atf_cnt) |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 156 | fit_file.write('%s\n' % DT_END) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 157 | |
| 158 | if fit_file_name != sys.stdout: |
| 159 | fit_file.close() |
| 160 | |
| 161 | def generate_atf_binary(bl31_file_name): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 162 | with open(bl31_file_name, 'rb') as bl31_file: |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 163 | bl31 = ELFFile(bl31_file) |
| 164 | |
| 165 | num = bl31.num_segments() |
| 166 | for i in range(num): |
| 167 | seg = bl31.get_segment(i) |
| 168 | if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)): |
| 169 | paddr = seg.__getitem__(ELF_SEG_P_PADDR) |
| 170 | file_name = 'bl31_0x%08x.bin' % paddr |
| 171 | with open(file_name, "wb") as atf: |
| 172 | atf.write(seg.data()); |
| 173 | |
| 174 | def get_bl31_segments_info(bl31_file_name): |
| 175 | """ |
| 176 | Get load offset, physical offset, file size |
| 177 | from bl31 elf file program headers. |
| 178 | """ |
| 179 | with open(bl31_file_name) as bl31_file: |
| 180 | bl31 = ELFFile(bl31_file) |
| 181 | |
| 182 | num = bl31.num_segments() |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 183 | print('Number of Segments : %d' % bl31.num_segments()) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 184 | for i in range(num): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 185 | print('Segment %d' % i) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 186 | seg = bl31.get_segment(i) |
| 187 | ptype = seg[ELF_SEG_P_TYPE] |
| 188 | poffset = seg[ELF_SEG_P_OFFSET] |
| 189 | pmemsz = seg[ELF_SEG_P_MEMSZ] |
| 190 | pfilesz = seg[ELF_SEG_P_FILESZ] |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 191 | print('type: %s\nfilesz: %08x\nmemsz: %08x\noffset: %08x' % (ptype, pfilesz, pmemsz, poffset)) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 192 | paddr = seg[ELF_SEG_P_PADDR] |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 193 | print('paddr: %08x' % paddr) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 194 | |
| 195 | def main(): |
| 196 | uboot_elf="./u-boot" |
| 197 | bl31_elf="./bl31.elf" |
| 198 | FIT_ITS=sys.stdout |
| 199 | |
| 200 | opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h") |
| 201 | for opt, val in opts: |
| 202 | if opt == "-o": |
| 203 | FIT_ITS=val |
| 204 | elif opt == "-u": |
| 205 | uboot_elf=val |
| 206 | elif opt == "-b": |
| 207 | bl31_elf=val |
| 208 | elif opt == "-h": |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 209 | print(__doc__) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 210 | sys.exit(2) |
| 211 | |
| 212 | dtbs = args |
| 213 | #get_bl31_segments_info("u-boot") |
| 214 | #get_bl31_segments_info("bl31.elf") |
| 215 | |
| 216 | generate_atf_fit_dts(FIT_ITS, bl31_elf, uboot_elf, dtbs) |
| 217 | generate_atf_binary(bl31_elf); |
| 218 | |
| 219 | if __name__ == "__main__": |
| 220 | main() |