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