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 | """ |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0+ |
| 4 | # |
| 5 | # A script to generate FIT image source for rockchip boards |
| 6 | # with ARM Trusted Firmware |
| 7 | # and multiple device trees (given on the command line) |
| 8 | # |
| 9 | # usage: $0 <dt_name> [<dt_name> [<dt_name] ...] |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 10 | """ |
| 11 | |
| 12 | import os |
| 13 | import sys |
| 14 | import getopt |
| 15 | |
| 16 | # pip install pyelftools |
| 17 | from elftools.elf.elffile import ELFFile |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 18 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 19 | ELF_SEG_P_TYPE = 'p_type' |
| 20 | ELF_SEG_P_PADDR = 'p_paddr' |
| 21 | ELF_SEG_P_VADDR = 'p_vaddr' |
| 22 | ELF_SEG_P_OFFSET = 'p_offset' |
| 23 | ELF_SEG_P_FILESZ = 'p_filesz' |
| 24 | ELF_SEG_P_MEMSZ = 'p_memsz' |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 25 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 26 | DT_HEADER = """ |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 27 | /* |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 28 | * This is a generated file. |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 29 | */ |
| 30 | /dts-v1/; |
| 31 | |
| 32 | / { |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 33 | description = "FIT image for U-Boot with bl31 (TF-A)"; |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 34 | #address-cells = <1>; |
| 35 | |
| 36 | images { |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 37 | """ |
| 38 | |
| 39 | DT_UBOOT = """ |
Kever Yang | 29c8c74 | 2018-08-23 11:01:08 +0800 | [diff] [blame] | 40 | uboot { |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 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 | }; |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 49 | |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 50 | """ |
| 51 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 52 | DT_IMAGES_NODE_END = """ }; |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 53 | |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 54 | """ |
| 55 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 56 | DT_END = "};" |
| 57 | |
| 58 | def append_bl31_node(file, atf_index, phy_addr, elf_entry): |
| 59 | # Append BL31 DT node to input FIT dts file. |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 60 | data = 'bl31_0x%08x.bin' % phy_addr |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 61 | file.write('\t\tatf_%d {\n' % atf_index) |
| 62 | file.write('\t\t\tdescription = \"ARM Trusted Firmware\";\n') |
| 63 | file.write('\t\t\tdata = /incbin/("%s");\n' % data) |
| 64 | file.write('\t\t\ttype = "firmware";\n') |
| 65 | file.write('\t\t\tarch = "arm64";\n') |
| 66 | file.write('\t\t\tos = "arm-trusted-firmware";\n') |
| 67 | file.write('\t\t\tcompression = "none";\n') |
| 68 | file.write('\t\t\tload = <0x%08x>;\n' % phy_addr) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 69 | if atf_index == 1: |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 70 | file.write('\t\t\tentry = <0x%08x>;\n' % elf_entry) |
| 71 | file.write('\t\t};\n') |
| 72 | file.write('\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 73 | |
| 74 | def append_fdt_node(file, dtbs): |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 75 | # Append FDT nodes. |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 76 | cnt = 1 |
| 77 | for dtb in dtbs: |
| 78 | dtname = os.path.basename(dtb) |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 79 | file.write('\t\tfdt_%d {\n' % cnt) |
| 80 | file.write('\t\t\tdescription = "%s";\n' % dtname) |
| 81 | file.write('\t\t\tdata = /incbin/("%s");\n' % dtb) |
| 82 | file.write('\t\t\ttype = "flat_dt";\n') |
| 83 | file.write('\t\t\tcompression = "none";\n') |
| 84 | file.write('\t\t};\n') |
| 85 | file.write('\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 86 | cnt = cnt + 1 |
| 87 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 88 | def append_conf_section(file, cnt, dtname, segments): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 89 | file.write('\t\tconfig_%d {\n' % cnt) |
| 90 | file.write('\t\t\tdescription = "%s";\n' % dtname) |
| 91 | file.write('\t\t\tfirmware = "atf_1";\n') |
| 92 | file.write('\t\t\tloadables = "uboot",') |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 93 | for i in range(1, segments): |
| 94 | file.write('"atf_%d"' % (i)) |
| 95 | if i != (segments - 1): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 96 | file.write(',') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 97 | else: |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 98 | file.write(';\n') |
| 99 | file.write('\t\t\tfdt = "fdt_1";\n') |
| 100 | file.write('\t\t};\n') |
| 101 | file.write('\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 102 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 103 | def append_conf_node(file, dtbs, segments): |
| 104 | # Append configeration nodes. |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 105 | cnt = 1 |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 106 | file.write('\tconfigurations {\n') |
| 107 | file.write('\t\tdefault = "config_1";\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 108 | for dtb in dtbs: |
| 109 | dtname = os.path.basename(dtb) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 110 | append_conf_section(file, cnt, dtname, segments) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 111 | cnt = cnt + 1 |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 112 | file.write('\t};\n') |
| 113 | file.write('\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 114 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 115 | def generate_atf_fit_dts_uboot(fit_file, uboot_file_name): |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 116 | num_load_seg = 0 |
| 117 | p_paddr = 0xFFFFFFFF |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 118 | with open(uboot_file_name, 'rb') as uboot_file: |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 119 | uboot = ELFFile(uboot_file) |
| 120 | for i in range(uboot.num_segments()): |
| 121 | seg = uboot.get_segment(i) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 122 | if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD': |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 123 | p_paddr = seg.__getitem__(ELF_SEG_P_PADDR) |
| 124 | num_load_seg = num_load_seg + 1 |
| 125 | |
| 126 | assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1) |
| 127 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 128 | fit_file.write(DT_UBOOT % p_paddr) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 129 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 130 | def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, dtbs_file_name): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 131 | with open(bl31_file_name, 'rb') as bl31_file: |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 132 | bl31 = ELFFile(bl31_file) |
Mian Yousaf Kaukab | 080e386 | 2018-06-08 10:47:09 +0200 | [diff] [blame] | 133 | elf_entry = bl31.header['e_entry'] |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 134 | segments = bl31.num_segments() |
| 135 | for i in range(segments): |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 136 | seg = bl31.get_segment(i) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 137 | if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD': |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 138 | paddr = seg.__getitem__(ELF_SEG_P_PADDR) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 139 | append_bl31_node(fit_file, i + 1, paddr, elf_entry) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 140 | append_fdt_node(fit_file, dtbs_file_name) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 141 | fit_file.write(DT_IMAGES_NODE_END) |
| 142 | append_conf_node(fit_file, dtbs_file_name, segments) |
| 143 | |
| 144 | def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name): |
| 145 | # Generate FIT script for ATF image. |
| 146 | if fit_file_name != sys.stdout: |
| 147 | fit_file = open(fit_file_name, "wb") |
| 148 | else: |
| 149 | fit_file = sys.stdout |
| 150 | |
| 151 | fit_file.write(DT_HEADER) |
| 152 | generate_atf_fit_dts_uboot(fit_file, uboot_file_name) |
| 153 | generate_atf_fit_dts_bl31(fit_file, bl31_file_name, dtbs_file_name) |
| 154 | fit_file.write(DT_END) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 155 | |
| 156 | if fit_file_name != sys.stdout: |
| 157 | fit_file.close() |
| 158 | |
| 159 | def generate_atf_binary(bl31_file_name): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 160 | with open(bl31_file_name, 'rb') as bl31_file: |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 161 | bl31 = ELFFile(bl31_file) |
| 162 | |
| 163 | num = bl31.num_segments() |
| 164 | for i in range(num): |
| 165 | seg = bl31.get_segment(i) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 166 | if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD': |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 167 | paddr = seg.__getitem__(ELF_SEG_P_PADDR) |
| 168 | file_name = 'bl31_0x%08x.bin' % paddr |
| 169 | with open(file_name, "wb") as atf: |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 170 | atf.write(seg.data()) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 171 | |
| 172 | def main(): |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 173 | uboot_elf = "./u-boot" |
| 174 | bl31_elf = "./bl31.elf" |
| 175 | fit_its = sys.stdout |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 176 | |
| 177 | opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h") |
| 178 | for opt, val in opts: |
| 179 | if opt == "-o": |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 180 | fit_its = val |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 181 | elif opt == "-u": |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 182 | uboot_elf = val |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 183 | elif opt == "-b": |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 184 | bl31_elf = val |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 185 | elif opt == "-h": |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 186 | print(__doc__) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 187 | sys.exit(2) |
| 188 | |
| 189 | dtbs = args |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 190 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 191 | generate_atf_fit_dts(fit_its, bl31_elf, uboot_elf, dtbs) |
| 192 | generate_atf_binary(bl31_elf) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 193 | |
| 194 | if __name__ == "__main__": |
| 195 | main() |