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 |
Jagan Teki | 2c2b429 | 2019-06-20 16:29:22 +0530 | [diff] [blame] | 15 | import logging |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 16 | |
| 17 | # pip install pyelftools |
| 18 | from elftools.elf.elffile import ELFFile |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 19 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 20 | ELF_SEG_P_TYPE = 'p_type' |
| 21 | ELF_SEG_P_PADDR = 'p_paddr' |
| 22 | ELF_SEG_P_VADDR = 'p_vaddr' |
| 23 | ELF_SEG_P_OFFSET = 'p_offset' |
| 24 | ELF_SEG_P_FILESZ = 'p_filesz' |
| 25 | ELF_SEG_P_MEMSZ = 'p_memsz' |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 26 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 27 | DT_HEADER = """ |
Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 28 | /* |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 29 | * This is a generated file. |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 30 | */ |
| 31 | /dts-v1/; |
| 32 | |
| 33 | / { |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 34 | description = "FIT image for U-Boot with bl31 (TF-A)"; |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 35 | #address-cells = <1>; |
| 36 | |
| 37 | images { |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 38 | """ |
| 39 | |
| 40 | DT_UBOOT = """ |
Kever Yang | 29c8c74 | 2018-08-23 11:01:08 +0800 | [diff] [blame] | 41 | uboot { |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 42 | description = "U-Boot (64-bit)"; |
| 43 | data = /incbin/("u-boot-nodtb.bin"); |
| 44 | type = "standalone"; |
| 45 | os = "U-Boot"; |
| 46 | arch = "arm64"; |
| 47 | compression = "none"; |
| 48 | load = <0x%08x>; |
| 49 | }; |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 50 | |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 51 | """ |
| 52 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 53 | DT_IMAGES_NODE_END = """ }; |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 54 | |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 55 | """ |
| 56 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 57 | DT_END = "};" |
| 58 | |
| 59 | def append_bl31_node(file, atf_index, phy_addr, elf_entry): |
| 60 | # Append BL31 DT node to input FIT dts file. |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 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): |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 76 | # Append FDT nodes. |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 77 | cnt = 1 |
| 78 | for dtb in dtbs: |
| 79 | dtname = os.path.basename(dtb) |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 80 | file.write('\t\tfdt_%d {\n' % cnt) |
| 81 | file.write('\t\t\tdescription = "%s";\n' % dtname) |
| 82 | file.write('\t\t\tdata = /incbin/("%s");\n' % dtb) |
| 83 | file.write('\t\t\ttype = "flat_dt";\n') |
| 84 | file.write('\t\t\tcompression = "none";\n') |
| 85 | file.write('\t\t};\n') |
| 86 | file.write('\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 87 | cnt = cnt + 1 |
| 88 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 89 | def append_conf_section(file, cnt, dtname, segments): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 90 | file.write('\t\tconfig_%d {\n' % cnt) |
| 91 | file.write('\t\t\tdescription = "%s";\n' % dtname) |
| 92 | file.write('\t\t\tfirmware = "atf_1";\n') |
Jagan Teki | 2c2b429 | 2019-06-20 16:29:22 +0530 | [diff] [blame] | 93 | file.write('\t\t\tloadables = "uboot"') |
| 94 | if segments != 0: |
| 95 | file.write(',') |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 96 | for i in range(1, segments): |
Andy Yan | 1383386 | 2019-07-04 17:44:40 +0800 | [diff] [blame] | 97 | file.write('"atf_%d"' % (i + 1)) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 98 | if i != (segments - 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') |
Jagan Teki | 2c2b429 | 2019-06-20 16:29:22 +0530 | [diff] [blame] | 102 | if segments == 0: |
| 103 | file.write(';\n') |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 104 | file.write('\t\t\tfdt = "fdt_1";\n') |
| 105 | file.write('\t\t};\n') |
| 106 | file.write('\n') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 107 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 108 | def append_conf_node(file, dtbs, segments): |
| 109 | # Append configeration nodes. |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 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) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 115 | append_conf_section(file, cnt, dtname, segments) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 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 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 120 | def generate_atf_fit_dts_uboot(fit_file, uboot_file_name): |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 121 | num_load_seg = 0 |
| 122 | p_paddr = 0xFFFFFFFF |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 123 | with open(uboot_file_name, 'rb') as uboot_file: |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 124 | uboot = ELFFile(uboot_file) |
| 125 | for i in range(uboot.num_segments()): |
| 126 | seg = uboot.get_segment(i) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 127 | if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD': |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 128 | p_paddr = seg.__getitem__(ELF_SEG_P_PADDR) |
| 129 | num_load_seg = num_load_seg + 1 |
| 130 | |
| 131 | assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1) |
| 132 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 133 | fit_file.write(DT_UBOOT % p_paddr) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 134 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 135 | 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] | 136 | with open(bl31_file_name, 'rb') as bl31_file: |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 137 | bl31 = ELFFile(bl31_file) |
Mian Yousaf Kaukab | 080e386 | 2018-06-08 10:47:09 +0200 | [diff] [blame] | 138 | elf_entry = bl31.header['e_entry'] |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 139 | segments = bl31.num_segments() |
| 140 | for i in range(segments): |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 141 | seg = bl31.get_segment(i) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 142 | if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD': |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 143 | paddr = seg.__getitem__(ELF_SEG_P_PADDR) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 144 | append_bl31_node(fit_file, i + 1, paddr, elf_entry) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 145 | append_fdt_node(fit_file, dtbs_file_name) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 146 | fit_file.write(DT_IMAGES_NODE_END) |
| 147 | append_conf_node(fit_file, dtbs_file_name, segments) |
| 148 | |
| 149 | def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name): |
| 150 | # Generate FIT script for ATF image. |
| 151 | if fit_file_name != sys.stdout: |
| 152 | fit_file = open(fit_file_name, "wb") |
| 153 | else: |
| 154 | fit_file = sys.stdout |
| 155 | |
| 156 | fit_file.write(DT_HEADER) |
| 157 | generate_atf_fit_dts_uboot(fit_file, uboot_file_name) |
| 158 | generate_atf_fit_dts_bl31(fit_file, bl31_file_name, dtbs_file_name) |
| 159 | fit_file.write(DT_END) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 160 | |
| 161 | if fit_file_name != sys.stdout: |
| 162 | fit_file.close() |
| 163 | |
| 164 | def generate_atf_binary(bl31_file_name): |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 165 | with open(bl31_file_name, 'rb') as bl31_file: |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 166 | bl31 = ELFFile(bl31_file) |
| 167 | |
| 168 | num = bl31.num_segments() |
| 169 | for i in range(num): |
| 170 | seg = bl31.get_segment(i) |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 171 | if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD': |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 172 | paddr = seg.__getitem__(ELF_SEG_P_PADDR) |
| 173 | file_name = 'bl31_0x%08x.bin' % paddr |
| 174 | with open(file_name, "wb") as atf: |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 175 | atf.write(seg.data()) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 176 | |
| 177 | def main(): |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 178 | uboot_elf = "./u-boot" |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 179 | fit_its = sys.stdout |
Jagan Teki | 2c2b429 | 2019-06-20 16:29:22 +0530 | [diff] [blame] | 180 | if "BL31" in os.environ: |
| 181 | bl31_elf=os.getenv("BL31"); |
| 182 | elif os.path.isfile("./bl31.elf"): |
| 183 | bl31_elf = "./bl31.elf" |
| 184 | else: |
| 185 | os.system("echo 'int main(){}' > bl31.c") |
| 186 | os.system("${CROSS_COMPILE}gcc -c bl31.c -o bl31.elf") |
| 187 | bl31_elf = "./bl31.elf" |
| 188 | logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) |
| 189 | logging.warning(' BL31 file bl31.elf NOT found, resulting binary is non-functional') |
| 190 | logging.warning(' Please read Building section in doc/README.rockchip') |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 191 | |
| 192 | opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h") |
| 193 | for opt, val in opts: |
| 194 | if opt == "-o": |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 195 | fit_its = val |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 196 | elif opt == "-u": |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 197 | uboot_elf = val |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 198 | elif opt == "-b": |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 199 | bl31_elf = val |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 200 | elif opt == "-h": |
Mian Yousaf Kaukab | 9a9c245 | 2018-06-08 10:47:10 +0200 | [diff] [blame] | 201 | print(__doc__) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 202 | sys.exit(2) |
| 203 | |
| 204 | dtbs = args |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 205 | |
Christoph Muellner | a0c7ee2 | 2019-05-07 11:11:01 +0200 | [diff] [blame] | 206 | generate_atf_fit_dts(fit_its, bl31_elf, uboot_elf, dtbs) |
| 207 | generate_atf_binary(bl31_elf) |
Kever Yang | 58ae215 | 2017-12-15 11:15:03 +0800 | [diff] [blame] | 208 | |
| 209 | if __name__ == "__main__": |
| 210 | main() |