petalinux 自动构建脚本

使用xilinx的petalinux构建BOOT.BIN和image.ub需要输入多条指令, 每次都这样输入的话真的会累死人, 所以想到把指令写成脚本, 一劳永逸。

# /etc/zcu102

# fpga 导出的hdf和bit文件的存放目录
hdf_file_dir=/samba/fpga_hdf_and_bit
# petalinux的安装目录
petalinux_install_dir=/opt/petalinux-2017.4
# 板级支持包
board_support_package="/opt/board_support_package/xilinx-zcu102-v2017.4-final.bsp"
# 开发板所属系列
template_type="zynqMP"

#!/bin/bash
#
# Auto build linux image for zcu102/zcu104 board
#

board_name="zcu102"
conf_file="/etc/${board_name}"

if [ ! -f $conf_file ];then
echo "$conf_file file does not exist"
exit
fi
source $conf_file

fpga_bitstream_config=""
project_workspace=~/petalinux_workspace
current_time="`date +%m%d%H%M%S`"
self_pid=$$
project_name="petalinux_project_${current_time}_${self_pid}_${board_name}"


error_log()
{
echo -e "\033[31m\033[01m[ERROR]: $1 \033[0m"
}


warning_log()
{
echo -e "\033[33m\033[01m\033[05m[INFO]: $1 \033[0m"
}


set_petalinux_env()
{
if [ ! -d $petalinux_install_dir ];then
echo "$petalinux_install_dir directory does not exist"
exit
fi

if [ ! -f $petalinux_install_dir/settings.sh -a ! -f $petalinux_install_dir/settings.csh ];then
echo "petalinux install directory config error"
exit
fi

bash_num=`echo $SHELL | grep "bash" | wc -l`
csh_num=`echo $SHELL | grep "csh" | wc -l`
if [ $bash_num -eq 0 -a $csh_num -eq 0 ];then
echo "please ensure your shell type is bash or csh"
exit
fi

echo "shell type: $SHELL"
if [ $bash_num -eq 1 ];then
source $petalinux_install_dir/settings.sh
fi

if [ $csh_num -eq 1 ];then
source $petalinux_install_dir/settings.csh
fi
}


create_project()
{
if [ -d $project_name ];then
rm -rf $project_name
fi

if [ ! -d $project_workspace ];then
mkdir -p $project_workspace
fi

if [ ! -f $board_support_package ];then
echo "$board_support_package file does not exist"
exit
fi

cd $project_workspace
petalinux-create --type project --template $template_type --name $project_name --source $board_support_package
}


change_dir_to_project()
{
cd $project_workspace/$project_name
}


config_hdf_file()
{
if [ ! -d $hdf_file_dir ];then
echo "$hdf_file_dir directory does not exist"
exit
fi

change_dir_to_project
inner_hdf_and_bit_dir="$project_workspace/$project_name/hdf_and_bit"
cp -rf $hdf_file_dir $inner_hdf_and_bit_dir
if [ $? -ne 0 ]; then
error_log "copy the .hdf and .bit failed"
exit
fi

hw_description_config="--get-hw-description=$inner_hdf_and_bit_dir"

find_bit_file_cmd="find $inner_hdf_and_bit_dir -maxdepth 1 -name "*.bit" "
find_hdf_file_cmd="find $inner_hdf_and_bit_dir -maxdepth 1 -name "*.hdf" "
bit_file_number=`$find_bit_file_cmd | wc -l`
hdf_file_number=`$find_hdf_file_cmd | wc -l`
if [ $bit_file_number -eq 0 -a $hdf_file_number -eq 0 ];then
warning_log "script will not load the hw-description"
hw_description_config=""
fpga_bitstream_config=""
fi

if [ $bit_file_number -gt 1 -o $hdf_file_number -gt 1 ];then
error_log "there are multiple .bit/.hdf files"
exit
fi

bit_file=`$find_bit_file_cmd`
if [ -f $bit_file ];then
fpga_bitstream_config="--fpga $bit_file"
fi

petalinux-config $hw_description_config
}


clean_exist_bootloader()
{
petalinux-build -c bootloader -x distclean
}


config_kernel()
{
petalinux-config -c kernel
}


config_rootfs()
{
petalinux-config -c rootfs
}


patch_bug_for_zcu104()
{
petalinuxbsp_config_file="$project_workspace/$project_name/project-spec/meta-user/conf/petalinuxbsp.conf"
echo 'IMAGE_INSTALL_remove = "gstreamer-vcu-examples"' >> $petalinuxbsp_config_file
}


build_project()
{
petalinux-build
}


package_project()
{
petalinux-package --boot --fsbl images/linux/zynqmp_fsbl.elf --pmufw images/linux/pmufw.elf --u-boot images/linux/u-boot.elf $fpga_bitstream_config
}


main()
{
set_petalinux_env

create_project

change_dir_to_project

config_hdf_file

# for zcu104 bug, before config_kernel
if [ x"$board_name" == x"zcu104" ];then
patch_bug_for_zcu104
fi

clean_exist_bootloader

config_kernel

config_rootfs

build_project

package_project
}

main