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