[U-Boot] [PATCH v3 1/3] rockchip: add a common script for generate fit its

Rockchip release bl31.elf file for armv8 SoCs like rk3399, rk3328, the elf have more than one section, we need to decode it first and packed them into u-boot.itb with its file. This script is to generate the its script. Need default bl31.elf in root directory of U-Boot source and dtb as parameter.
Signed-off-by: Kever Yang kever.yang@rock-chips.com ---
Changes in v3: - use python script - adapt for latest spl atf support
Changes in v2: None
arch/arm/mach-rockchip/make_fit_atf.py | 221 +++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100755 arch/arm/mach-rockchip/make_fit_atf.py
diff --git a/arch/arm/mach-rockchip/make_fit_atf.py b/arch/arm/mach-rockchip/make_fit_atf.py new file mode 100755 index 0000000..7c6dd57 --- /dev/null +++ b/arch/arm/mach-rockchip/make_fit_atf.py @@ -0,0 +1,221 @@ +#!/usr/bin/env python2 +""" +A script to generate FIT image source for rockchip boards +with ARM Trusted Firmware +and multiple device trees (given on the command line) + +usage: $0 <dt_name> [<dt_name> [<dt_name] ...] +""" + +import os +import sys +import getopt + +# pip install pyelftools +from elftools.elf.elffile import ELFFile +from elftools.elf.sections import SymbolTableSection +from elftools.elf.segments import Segment, InterpSegment, NoteSegment + +ELF_SEG_P_TYPE='p_type' +ELF_SEG_P_PADDR='p_paddr' +ELF_SEG_P_VADDR='p_vaddr' +ELF_SEG_P_OFFSET='p_offset' +ELF_SEG_P_FILESZ='p_filesz' +ELF_SEG_P_MEMSZ='p_memsz' + +DT_HEADER="""/* + * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd + * + * Minimal dts for a SPL FIT image payload. + * + * SPDX-License-Identifier: GPL-2.0+ X11 + */ +/dts-v1/; + +/ { + description = "Configuration to load ATF before U-Boot"; + #address-cells = <1>; + + images { + uboot@1 { + description = "U-Boot (64-bit)"; + data = /incbin/("u-boot-nodtb.bin"); + type = "standalone"; + os = "U-Boot"; + arch = "arm64"; + compression = "none"; + load = <0x%08x>; + }; +""" + +DT_IMAGES_NODE_END=""" + }; +""" + +DT_END=""" +}; +""" + +def append_atf_node(file, atf_index, phy_addr): + """ + Append ATF DT node to input FIT dts file. + """ + data = 'bl31_0x%08x.bin' % phy_addr + print >> file, '\t\tatf@%d {' % atf_index + print >> file, '\t\t\tdescription = "ARM Trusted Firmware";' + print >> file, '\t\t\tdata = /incbin/("%s");' % data + print >> file, '\t\t\ttype = "firmware";' + print >> file, '\t\t\tarch = "arm64";' + print >> file, '\t\t\tos = "arm-trusted-firmware";' + print >> file, '\t\t\tcompression = "none";' + print >> file, '\t\t\tload = <0x%08x>;' % phy_addr + if atf_index == 1: + print >> file, '\t\t\tentry = <0x%08x>;' % phy_addr + print >> file, '\t\t};' + print >> file, '' + +def append_fdt_node(file, dtbs): + """ + Append FDT nodes. + """ + cnt = 1 + for dtb in dtbs: + dtname = os.path.basename(dtb) + print >> file, '\t\tfdt@%d {' % cnt + print >> file, '\t\t\tdescription = "%s";' % dtname + print >> file, '\t\t\tdata = /incbin/("%s");' % dtb + print >> file, '\t\t\ttype = "flat_dt";' + print >> file, '\t\t\tcompression = "none";' + print >> file, '\t\t};' + print >> file, '' + cnt = cnt + 1 + +def append_conf_section(file, cnt, dtname, atf_cnt): + print >> file, '\t\tconfig@%d {' % cnt + print >> file, '\t\t\tdescription = "%s";' % dtname + print >> file, '\t\t\tfirmware = "atf@1";' + print >> file, '\t\t\tloadables = "uboot@1",', + for i in range(1, atf_cnt): + print >> file, '"atf@%d"' % (i+1), + if i != (atf_cnt - 1): + print >> file, ',', + else: + print >> file, ';' + print >> file, '\t\t\tfdt = "fdt@1";' + print >> file, '\t\t};' + print >> file, '' + +def append_conf_node(file, dtbs, atf_cnt): + """ + Append configeration nodes. + """ + cnt = 1 + print >> file, '\tconfigurations {' + print >> file, '\t\tdefault = "config@1";' + for dtb in dtbs: + dtname = os.path.basename(dtb) + append_conf_section(file, cnt, dtname, atf_cnt) + cnt = cnt + 1 + print >> file, '\t};' + print >> file, '' + +def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name): + """ + Generate FIT script for ATF image. + """ + if fit_file_name != sys.stdout: + fit_file = open(fit_file_name, "wb") + else: + fit_file = sys.stdout + + num_load_seg = 0 + p_paddr = 0xFFFFFFFF + with open(uboot_file_name) as uboot_file: + uboot = ELFFile(uboot_file) + for i in range(uboot.num_segments()): + seg = uboot.get_segment(i) + if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)): + p_paddr = seg.__getitem__(ELF_SEG_P_PADDR) + num_load_seg = num_load_seg + 1 + + assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1) + + print >> fit_file, DT_HEADER % p_paddr + + with open(bl31_file_name) as bl31_file: + bl31 = ELFFile(bl31_file) + for i in range(bl31.num_segments()): + seg = bl31.get_segment(i) + if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)): + paddr = seg.__getitem__(ELF_SEG_P_PADDR) + p= seg.__getitem__(ELF_SEG_P_PADDR) + append_atf_node(fit_file, i+1, paddr) + atf_cnt = i+1 + append_fdt_node(fit_file, dtbs_file_name) + print >> fit_file, '%s' % DT_IMAGES_NODE_END + append_conf_node(fit_file, dtbs_file_name, atf_cnt) + print >> fit_file, '%s' % DT_END + + if fit_file_name != sys.stdout: + fit_file.close() + +def generate_atf_binary(bl31_file_name): + with open(bl31_file_name) as bl31_file: + bl31 = ELFFile(bl31_file) + + num = bl31.num_segments() + for i in range(num): + seg = bl31.get_segment(i) + if ('PT_LOAD' == seg.__getitem__(ELF_SEG_P_TYPE)): + paddr = seg.__getitem__(ELF_SEG_P_PADDR) + file_name = 'bl31_0x%08x.bin' % paddr + with open(file_name, "wb") as atf: + atf.write(seg.data()); + +def get_bl31_segments_info(bl31_file_name): + """ + Get load offset, physical offset, file size + from bl31 elf file program headers. + """ + with open(bl31_file_name) as bl31_file: + bl31 = ELFFile(bl31_file) + + num = bl31.num_segments() + print 'Number of Segments : %d' % bl31.num_segments() + for i in range(num): + print 'Segment %d' % i + seg = bl31.get_segment(i) + ptype = seg[ELF_SEG_P_TYPE] + poffset = seg[ELF_SEG_P_OFFSET] + pmemsz = seg[ELF_SEG_P_MEMSZ] + pfilesz = seg[ELF_SEG_P_FILESZ] + print 'type: %s\nfilesz: %08x\nmemsz: %08x\noffset: %08x' % (ptype, pfilesz, pmemsz, poffset) + paddr = seg[ELF_SEG_P_PADDR] + print 'paddr: %08x' % paddr + +def main(): + uboot_elf="./u-boot" + bl31_elf="./bl31.elf" + FIT_ITS=sys.stdout + + opts, args = getopt.getopt(sys.argv[1:], "o:u:b:h") + for opt, val in opts: + if opt == "-o": + FIT_ITS=val + elif opt == "-u": + uboot_elf=val + elif opt == "-b": + bl31_elf=val + elif opt == "-h": + print __doc__ + sys.exit(2) + + dtbs = args + #get_bl31_segments_info("u-boot") + #get_bl31_segments_info("bl31.elf") + + generate_atf_fit_dts(FIT_ITS, bl31_elf, uboot_elf, dtbs) + generate_atf_binary(bl31_elf); + +if __name__ == "__main__": + main()

Enable SPL_FIT_GENERATOR with path for it. With this patch you can get u-boot.itb for rk3399-firefly with:
make u-boot.itb
Signed-off-by: Kever Yang kever.yang@rock-chips.com Reviewed-by: Mark Kettenis kettenis@openbsd.org Tested-by: Mark Kettenis kettenis@openbsd.org ---
Changes in v3: - update with new script path
Changes in v2: - typo fix, rk3399-evb->rk3399-firefly
configs/firefly-rk3399_defconfig | 1 + 1 file changed, 1 insertion(+)
diff --git a/configs/firefly-rk3399_defconfig b/configs/firefly-rk3399_defconfig index 731222c..7a3fb83 100644 --- a/configs/firefly-rk3399_defconfig +++ b/configs/firefly-rk3399_defconfig @@ -9,6 +9,7 @@ CONFIG_DEFAULT_DEVICE_TREE="rk3399-firefly" CONFIG_DEBUG_UART=y CONFIG_FIT=y CONFIG_SPL_LOAD_FIT=y +CONFIG_SPL_FIT_GENERATOR="arch/arm/mach-rockchip/make_fit_atf.py" # CONFIG_DISPLAY_CPUINFO is not set CONFIG_SPL_STACK_R=y CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN=0x4000

Enable SPL_FIT_GENERATOR with path for it. With this patch you can get u-boot.itb for rk3399-firefly with:
make u-boot.itb
Signed-off-by: Kever Yang kever.yang@rock-chips.com Reviewed-by: Mark Kettenis kettenis@openbsd.org Tested-by: Mark Kettenis kettenis@openbsd.org
Changes in v3:
- update with new script path
Changes in v2:
- typo fix, rk3399-evb->rk3399-firefly
configs/firefly-rk3399_defconfig | 1 + 1 file changed, 1 insertion(+)
Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com

Enable SPL_FIT_GENERATOR with path for it. With this patch you can get u-boot.itb for rk3399-firefly with:
make u-boot.itb
Signed-off-by: Kever Yang kever.yang@rock-chips.com Reviewed-by: Mark Kettenis kettenis@openbsd.org Tested-by: Mark Kettenis kettenis@openbsd.org Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com
Changes in v3:
- update with new script path
Changes in v2:
- typo fix, rk3399-evb->rk3399-firefly
configs/firefly-rk3399_defconfig | 1 + 1 file changed, 1 insertion(+)
Reviewed-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com

Enable SPL_FIT_GENERATOR with path for it. With this patch you can get u-boot.itb for rk3399-firefly with:
make u-boot.itb
Signed-off-by: Kever Yang kever.yang@rock-chips.com Reviewed-by: Mark Kettenis kettenis@openbsd.org Tested-by: Mark Kettenis kettenis@openbsd.org Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com Reviewed-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com
Changes in v3:
- update with new script path
Changes in v2:
- typo fix, rk3399-evb->rk3399-firefly
configs/firefly-rk3399_defconfig | 1 + 1 file changed, 1 insertion(+)
Applied to u-boot-rockchip, thanks!

Enable SPL_FIT_GENERATOR with path for it. With this patch you can get u-boot.itb for rk3399-firefly with:
make u-boot.itb
Signed-off-by: Kever Yang kever.yang@rock-chips.com Reviewed-by: Mark Kettenis kettenis@openbsd.org Tested-by: Mark Kettenis kettenis@openbsd.org Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com Reviewed-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com
Changes in v3:
- update with new script path
Changes in v2:
- typo fix, rk3399-evb->rk3399-firefly
configs/firefly-rk3399_defconfig | 1 + 1 file changed, 1 insertion(+)
Applied to u-boot-rockchip, thanks!

Since we support ATF in SPL and add script for it, let's make the document up to date.
Signed-off-by: Kever Yang kever.yang@rock-chips.com Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com ---
Changes in v3: - update addr for uboot.itb to 0x4000
Changes in v2: - typo fix, evb-firefly->firefly-rk3399
board/rockchip/evb_rk3399/README | 83 +++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 18 deletions(-)
diff --git a/board/rockchip/evb_rk3399/README b/board/rockchip/evb_rk3399/README index fb8bb19..ada8ca7 100644 --- a/board/rockchip/evb_rk3399/README +++ b/board/rockchip/evb_rk3399/README @@ -18,8 +18,8 @@ evb key features: * PMIC: rk808 * debug console: UART2
-In order to support Arm Trust Firmware(ATF), we need to use the -miniloader from rockchip which: +In order to support Arm Trust Firmware(ATF), we can use either SPL or +miniloader from rockchip to do: * do DRAM init * load and verify ATF image * load and verify U-Boot image @@ -32,8 +32,8 @@ Get the Source and prebuild binary
mkdir ~/evb_rk3399 cd ~/evb_rk3399 git clone https://github.com/ARM-software/arm-trusted-firmware.git
- > git clone https://github.com/rockchip-linux/rkbin - > git clone https://github.com/rockchip-linux/rkflashtool + > git clone https://github.com/rockchip-linux/rkbin.git + > git clone https://github.com/rockchip-linux/rkdeveloptool.git
Compile the ATF =============== @@ -42,32 +42,79 @@ Compile the ATF
make realclean make CROSS_COMPILE=aarch64-linux-gnu- PLAT=rk3399 bl31
+ Or you can get the bl31.elf directly from Rockchip: + cp rkbin/rk33/rk3399_bl31_v1.00.elf ../u-boot/bl31.elf + + Get bl31.elf in this step, copy it to U-Boot root dir: + > cp bl31.elf ../u-boot/ + Compile the U-Boot ==================
cd ../u-boot
- > make CROSS_COMPILE=aarch64-linux-gnu- evb-rk3399_defconfig all + > export ARCH=arm64 + > export CROSS_COMPILE=aarch64-linux-gnu- + > make evb-rk3399_defconfig + for firefly-rk3399, use below instead: + > make firefly-rk3399_defconfig + > make + > make u-boot.itb
-Compile the rkflashtool -======================= + Get spl/u-boot-spl.bin and u-boot.itb in this step.
+Compile the rkdeveloptool +======================= + Follow instructions in latest README
cd ../rkflashtool
+ > autoreconf -i + > ./configure
make
+ > sudo make install
-Package the image for miniloader -================================ + Get rkdeveloptool in you Host in this step. + +Both origin binaries and Tool are ready now, choose either option 1 or +option 2 to deploy U-Boot. + +Package the image +================= + +Package the image for U-Boot SPL(option 1) +--------------------------------
cd ..
- > cp arm-trusted-firmware/build/rk3399/release/bl31.bin rkbin/rk33 + > tools/mkimage -n rk3399 -T rksd -d spl/u-boot-spl.bin idbspl.img + + Get idbspl.img in this step. + +Package the image for Rockchip miniloader(option 2) +------------------------------------------ + > cd .. + > cp arm-trusted-firmware/build/rk3399/release/bl31.elf rkbin/rk33
./rkbin/tools/trust_merger rkbin/tools/RK3399TRUST.ini ./rkbin/tools/loaderimage --pack --uboot u-boot/u-boot-dtb.bin uboot.img
- > mkdir image - > mv trust.img ./image/ - > mv uboot.img ./image/rk3399evb-uboot.bin
-Flash the image -=============== -Power on(or reset with RESET KEY) with MASKROM KEY preesed, and then: + Get trust.img and uboot.img in this step.
- > ./rkflashtool/rkflashloader rk3399evb +Flash the image to eMMC +======================= + +Flash the image with U-Boot SPL(option 1) +------------------------------- +Power on(or reset with RESET KEY) with MASKROM KEY preesed, and then: + > rkdeveloptool db rkbin/rk33/rk3399_loader_v1.08.106.bin + > rkdeveloptool wl 64 u-boot/idbspl.img + > rkdeveloptool wl 0x4000 u-boot/u-boot.itb + > rkdeveloptool rd
-You should be able to get U-Boot log message in console/UART2 now. +Flash the image with Rockchip miniloader(option 2) +---------------------------------------- +Power on(or reset with RESET KEY) with MASKROM KEY preesed, and then: + > rkdeveloptool db rkbin/rk33/rk3399_loader_v1.08.106.bin + > rkdeveloptool ul rkbin/rk33/rk3399_loader_v1.08.106.bin + > rkdeveloptool wl 0x4000 u-boot/uboot.img + > rkdeveloptool wl 0x6000 u-boot/trust.img + > rkdeveloptool rd + +You should be able to get U-Boot log in console/UART2(baurdrate 1500000) +For more detail, please reference to: +http://opensource.rock-chips.com/wiki_Boot_option

Since we support ATF in SPL and add script for it, let's make the document up to date.
Signed-off-by: Kever Yang kever.yang@rock-chips.com Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com
Changes in v3:
- update addr for uboot.itb to 0x4000
Changes in v2:
- typo fix, evb-firefly->firefly-rk3399
board/rockchip/evb_rk3399/README | 83 +++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 18 deletions(-)
Reviewed-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com

Since we support ATF in SPL and add script for it, let's make the document up to date.
Signed-off-by: Kever Yang kever.yang@rock-chips.com Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com
Changes in v3:
- update addr for uboot.itb to 0x4000
Changes in v2:
- typo fix, evb-firefly->firefly-rk3399
board/rockchip/evb_rk3399/README | 83 +++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 18 deletions(-)
Applied to u-boot-rockchip, thanks!

Since we support ATF in SPL and add script for it, let's make the document up to date.
Signed-off-by: Kever Yang kever.yang@rock-chips.com Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com
Changes in v3:
- update addr for uboot.itb to 0x4000
Changes in v2:
- typo fix, evb-firefly->firefly-rk3399
board/rockchip/evb_rk3399/README | 83 +++++++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 18 deletions(-)
Applied to u-boot-rockchip, thanks!

Rockchip release bl31.elf file for armv8 SoCs like rk3399, rk3328, the elf have more than one section, we need to decode it first and packed them into u-boot.itb with its file. This script is to generate the its script. Need default bl31.elf in root directory of U-Boot source and dtb as parameter.
Signed-off-by: Kever Yang kever.yang@rock-chips.com
Changes in v3:
- use python script
- adapt for latest spl atf support
Changes in v2: None
arch/arm/mach-rockchip/make_fit_atf.py | 221 +++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100755 arch/arm/mach-rockchip/make_fit_atf.py
Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com

Rockchip release bl31.elf file for armv8 SoCs like rk3399, rk3328, the elf have more than one section, we need to decode it first and packed them into u-boot.itb with its file. This script is to generate the its script. Need default bl31.elf in root directory of U-Boot source and dtb as parameter.
Signed-off-by: Kever Yang kever.yang@rock-chips.com Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com
Changes in v3:
- use python script
- adapt for latest spl atf support
Changes in v2: None
arch/arm/mach-rockchip/make_fit_atf.py | 221 +++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100755 arch/arm/mach-rockchip/make_fit_atf.py
Reviewed-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com

Rockchip release bl31.elf file for armv8 SoCs like rk3399, rk3328, the elf have more than one section, we need to decode it first and packed them into u-boot.itb with its file. This script is to generate the its script. Need default bl31.elf in root directory of U-Boot source and dtb as parameter.
Signed-off-by: Kever Yang kever.yang@rock-chips.com Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com Reviewed-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com
Changes in v3:
- use python script
- adapt for latest spl atf support
Changes in v2: None
arch/arm/mach-rockchip/make_fit_atf.py | 221 +++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100755 arch/arm/mach-rockchip/make_fit_atf.py
Applied to u-boot-rockchip, thanks!

Rockchip release bl31.elf file for armv8 SoCs like rk3399, rk3328, the elf have more than one section, we need to decode it first and packed them into u-boot.itb with its file. This script is to generate the its script. Need default bl31.elf in root directory of U-Boot source and dtb as parameter.
Signed-off-by: Kever Yang kever.yang@rock-chips.com Acked-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com Reviewed-by: Philipp Tomsich philipp.tomsich@theobroma-systems.com
Changes in v3:
- use python script
- adapt for latest spl atf support
Changes in v2: None
arch/arm/mach-rockchip/make_fit_atf.py | 221 +++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100755 arch/arm/mach-rockchip/make_fit_atf.py
Applied to u-boot-rockchip, thanks!
participants (2)
-
Kever Yang
-
Philipp Tomsich