xref: /openwifi/kernel_boot/build_zynqmp_boot_bin.sh (revision a6085186d94dfe08b0e09c18c8d4b1b4fe38ea35)
1#!/bin/bash
2
3# SPDX-FileCopyrightText: 2019 Jiao Xianjun <[email protected]>
4# SPDX-License-Identifier: AGPL-3.0-or-later
5# https://wiki.analog.com/resources/eval/user-guides/ad-fmcomms2-ebz/software/linux/zynqmp
6
7set -ex
8
9HDF_FILE=$1
10UBOOT_FILE=$2
11ATF_FILE=${3:-download}
12BUILD_DIR=build_boot_bin
13OUTPUT_DIR=output_boot_bin
14
15usage () {
16	echo "usage: $0 system_top.hdf u-boot.elf  (download | bl31.elf | <path-to-arm-trusted-firmware-source>) [output-archive]"
17	exit 1
18}
19
20depends () {
21	echo "Xilinx $1 must be installed and in your PATH"
22	echo "try: source /opt/Xilinx/Vivado/201x.x/settings64.sh"
23	exit 1
24}
25
26### Check command line parameters
27echo $HDF_FILE | grep -q ".hdf" || usage
28echo $UBOOT_FILE | grep -q -e ".elf" -e "uboot" || usage
29
30if [ ! -f $HDF_FILE ]; then
31    echo $HDF_FILE: File not found!
32    usage
33fi
34
35if [ ! -f $UBOOT_FILE ]; then
36    echo $UBOOT_FILE: File not found!
37    usage
38fi
39
40### Check for required Xilinx tools
41command -v xsdk >/dev/null 2>&1 || depends xsdk
42command -v bootgen >/dev/null 2>&1 || depends bootgen
43command -v hsi >/dev/null 2>&1 || depends hsi
44
45rm -Rf $BUILD_DIR $OUTPUT_DIR
46mkdir -p $OUTPUT_DIR
47mkdir -p $BUILD_DIR
48
49# 2017.4 use 47af34b94a52b8cdc8abbac44b6f3ffab33a2206
50# 2018.1 use df4a7e97d57494c7d79de51b1e0e450d982cea98
51# 2018.2 use 93a69a5a3bc318027da4af5911124537f4907642
52# 2018.3 use 08560c36ea5b6f48b962cb4bd9a79b35bb3d95ce
53
54hsi_ver=$(hsi -version | head -1 | cut -d' ' -f2)
55if [ -z "$hsi_ver" ] ; then
56	echo "Could not determine Vivado version"
57	exit 1
58fi
59atf_version=xilinx-$hsi_ver
60
61### Check if ATF_FILE is .elf or path to arm-trusted-firmware
62if [ "$ATF_FILE" != "" ] && [ -d $ATF_FILE ]; then
63### Build arm-trusted-firmware bl31.elf
64(
65	cd $ATF_FILE
66	make distclean
67	git checkout $atf_version
68	make CROSS_COMPILE=aarch64-linux-gnu- PLAT=zynqmp RESET_TO_BL31=1
69)
70	cp $ATF_FILE/build/zynqmp/release/bl31/bl31.elf $OUTPUT_DIR/bl31.elf
71elif [ "$ATF_FILE" == "download" ]; then
72(
73	command -v git >/dev/null 2>&1 || depends git
74	cd $BUILD_DIR
75	git clone https://github.com/Xilinx/arm-trusted-firmware.git
76	cd arm-trusted-firmware
77	git checkout $atf_version
78	make CROSS_COMPILE=aarch64-linux-gnu- PLAT=zynqmp RESET_TO_BL31=1
79)
80	cp $BUILD_DIR/arm-trusted-firmware/build/zynqmp/release/bl31/bl31.elf $OUTPUT_DIR/bl31.elf
81else
82	echo $ATF_FILE | grep -q -e "bl31.elf" || usage
83	if [ ! -f $ATF_FILE ]; then
84		echo $ATF_FILE: File not found!
85		usage
86	fi
87	cp $ATF_FILE $OUTPUT_DIR/bl31.elf
88fi
89
90cp $HDF_FILE $BUILD_DIR/
91cp $UBOOT_FILE $OUTPUT_DIR/u-boot.elf
92cp $HDF_FILE $OUTPUT_DIR/
93
94# get the tools version (e.g., v2018.3)
95tool_version=$(hsi -version)
96tool_version=${tool_version#hsi\ }
97tool_version=${tool_version%\ (64-bit)*}
98
99# Work-arownd for MPSoC ZCU102 and ZCU106 Evaluation Kits - DDR4 SODIMM change
100# (https://www.xilinx.com/support/answers/71961.html)
101if [ $tool_version == "v2018.3" ];then
102(
103	wget https://www.xilinx.com/Attachment/72113-files.zip -P $BUILD_DIR
104	unzip $BUILD_DIR/72113-files.zip -d  $BUILD_DIR
105)
106fi
107
108### Create create_fsbl_project.tcl file used by xsdk to create the fsbl
109echo "hsi open_hw_design `basename $HDF_FILE`" > $BUILD_DIR/create_fsbl_project.tcl
110echo 'set cpu_name [lindex [hsi get_cells -filter {IP_TYPE==PROCESSOR}] 0]' >> $BUILD_DIR/create_fsbl_project.tcl
111echo 'sdk setws ./build/sdk' >> $BUILD_DIR/create_fsbl_project.tcl
112echo "sdk createhw -name hw_0 -hwspec `basename $HDF_FILE`" >> $BUILD_DIR/create_fsbl_project.tcl
113echo 'sdk createapp -name fsbl -hwproject hw_0 -proc $cpu_name -os standalone -lang C -app {Zynq MP FSBL}' >> $BUILD_DIR/create_fsbl_project.tcl
114echo 'configapp -app fsbl build-config release' >> $BUILD_DIR/create_fsbl_project.tcl
115if [ $tool_version == "v2018.3" ];then
116(
117	echo "file copy -force xfsbl_ddr_init.c ./build/sdk/fsbl/src" >> $BUILD_DIR/create_fsbl_project.tcl
118	echo "file copy -force xfsbl_hooks.c    ./build/sdk/fsbl/src" >> $BUILD_DIR/create_fsbl_project.tcl
119	echo "file copy -force xfsbl_hooks.h    ./build/sdk/fsbl/src" >> $BUILD_DIR/create_fsbl_project.tcl
120)
121fi
122echo 'sdk projects -build -type all' >> $BUILD_DIR/create_fsbl_project.tcl
123
124### Create create_pmufw_project.tcl
125echo "set hwdsgn [open_hw_design `basename $HDF_FILE`]" > $BUILD_DIR/create_pmufw_project.tcl
126echo 'generate_app -hw $hwdsgn -os standalone -proc psu_pmu_0 -app zynqmp_pmufw -sw pmufw -dir pmufw' >> $BUILD_DIR/create_pmufw_project.tcl
127echo 'quit' >> $BUILD_DIR/create_pmufw_project.tcl
128
129### Create zynq.bif file used by bootgen
130echo "the_ROM_image:" > $OUTPUT_DIR/zynq.bif
131echo "{" >> $OUTPUT_DIR/zynq.bif
132echo "[bootloader,destination_cpu=a53-0] fsbl.elf" >> $OUTPUT_DIR/zynq.bif
133echo "[pmufw_image] pmufw.elf" >> $OUTPUT_DIR/zynq.bif
134echo "[destination_device=pl] system_top.bit" >> $OUTPUT_DIR/zynq.bif
135echo "[destination_cpu=a53-0,exception_level=el-3,trustzone] bl31.elf" >> $OUTPUT_DIR/zynq.bif
136echo "[destination_cpu=a53-0, exception_level=el-2] u-boot.elf" >> $OUTPUT_DIR/zynq.bif
137echo "}" >> $OUTPUT_DIR/zynq.bif
138
139
140### Build fsbl.elf & pmufw.elf
141(
142	cd $BUILD_DIR
143	xsdk -batch -source create_fsbl_project.tcl
144	hsi -source create_pmufw_project.tcl
145	### There was a bug in some vivado version where they build would fail -> check CC_FLAGS
146	grep "CC_FLAGS :=" pmufw/Makefile | grep -e "-Os" || sed -i '/-mxl-soft-mul/ s/$/ -Os -flto -ffat-lto-objects/' pmufw/Makefile
147	cd pmufw
148	make
149)
150
151### Copy fsbl and system_top.bit into the output folder
152cp $BUILD_DIR/build/sdk/fsbl/Release/fsbl.elf $OUTPUT_DIR/fsbl.elf
153cp $BUILD_DIR/build/sdk/hw_0/system_top.bit $OUTPUT_DIR/system_top.bit
154cp $BUILD_DIR/pmufw/executable.elf $OUTPUT_DIR/pmufw.elf
155
156### Build BOOT.BIN
157(
158	cd $OUTPUT_DIR
159	bootgen -arch zynqmp -image zynq.bif -o BOOT.BIN -w
160)
161
162### Optionally tar.gz the entire output folder with the name given in argument 3
163if [ ${#4} -ne 0 ]; then
164	tar czvf $4.tar.gz $OUTPUT_DIR
165fi
166