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