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