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