#!/bin/bash
|
|
# this project absolute path
|
PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
|
|
# top project absolute path
|
TOP_PATH=$(realpath $PRJ_PATH/..)
|
|
# binaries install path
|
INST_PATH=$PRJ_PATH/install
|
|
# download taballs path
|
TARBALL_PATH=$PRJ_PATH/tarballs
|
|
# config file path
|
CONF_FILE=$TOP_PATH/config.json
|
|
# shell script will exit once get command error
|
set -e
|
|
#+-------------------------+
|
#| Shell script functions |
|
#+-------------------------+
|
|
function pr_error() {
|
echo -e "\033[40;31m $1 \033[0m"
|
}
|
|
function pr_warn() {
|
echo -e "\033[40;33m $1 \033[0m"
|
}
|
|
function pr_info() {
|
echo -e "\033[40;32m $1 \033[0m"
|
}
|
|
# decompress a packet to destination path
|
function do_unpack()
|
{
|
tarball=$1
|
dstpath=`pwd`
|
|
if [[ $# == 2 ]] ; then
|
dstpath=$2
|
fi
|
|
pr_info "decompress $tarball => $dstpath"
|
|
mkdir -p $dstpath
|
case $tarball in
|
*.tar.gz)
|
tar -xzf $tarball -C $dstpath
|
;;
|
|
*.tar.bz2)
|
tar -xjf $tarball -C $dstpath
|
;;
|
|
*.tar.xz)
|
tar -xJf $tarball -C $dstpath
|
;;
|
|
*.tar.zst)
|
tar -I zstd -xf $tarball -C $dstpath
|
;;
|
|
*.tar)
|
tar -xf $tarball -C $dstpath
|
;;
|
|
*.zip)
|
unzip -qo $tarball -d $dstpath
|
;;
|
|
*)
|
pr_error "decompress Unsupport packet: $tarball"
|
return 1;
|
;;
|
esac
|
}
|
|
# parser configure file and export environment variable
|
function export_env()
|
{
|
export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
|
export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'`
|
export URL=`jq -r ".bsp.download" $CONF_FILE | tr 'A-Z' 'a-z'`
|
export CROSS_COMPILE=`jq -r ".bsp.crosstool" $CONF_FILE | tr 'A-Z' 'a-z'`
|
export JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
|
|
# i.MX6ULL only need build uboot-imx
|
if [[ $BOARD =~ 6ull ]] ; then
|
|
export ARCH=arm
|
export SRCS="uboot-imx"
|
|
# i.MX8MP need build atf and u-boot
|
elif [[ $BOARD =~ 8mp ]] ; then
|
|
export ARCH=arm64
|
export SRCS="uboot-imx imx-atf imx-mkimage firmware-imx"
|
|
# ATF and imx-mkimage variable
|
export ATF_PLATFORM=imx8mp
|
export IMX_BOOT_SOC_TARGET=iMX8MP
|
export IMXBOOT_TARGETS=flash_evk
|
export IMXBOOT_DTB=imx8mp-evk.dtb
|
export MKIMG_BIN_PATH=$PRJ_PATH/imx-mkimage/iMX8M/
|
|
else
|
|
pr_error "\nERROR: Unknow board name $BOARD"
|
exit;
|
|
fi
|
}
|
|
function do_fetch()
|
{
|
mkdir -p $TARBALL_PATH
|
cd $PRJ_PATH
|
|
for src in $SRCS
|
do
|
if [ -d $src ] ; then
|
pr_info "$src fetched already"
|
continue
|
fi
|
|
# Download source code packet
|
if [ ! -s $TARBALL_PATH/$src.tar.xz ] ; then
|
pr_info "start fetch $src source code"
|
wget $URL/bsp/$BSP_VER/$src.tar.xz -P $TARBALL_PATH
|
fi
|
|
# decompress source code packet
|
do_unpack $TARBALL_PATH/$src.tar.xz
|
|
# do patch if patch file exist
|
patch_path=$PRJ_PATH/patches/$BOARD
|
patch_file=$patch_path/${src}-$BSP_VER.patch
|
if [ -f $patch_file ] ; then
|
pr_info "found and patch for $src"
|
patch -p0 < $patch_file
|
fi
|
|
# copy uboot logo file
|
logo_file=$patch_path/logo_uboot.bmp
|
if [[ $src =~ uboot ]] ; then
|
if [ -f $logo_file ] ; then
|
pr_info "copy logo file to $src"
|
cp $logo_file $src/tools/logos/lingyun.bmp
|
fi
|
fi
|
done
|
|
rm -f *.tar.*
|
}
|
|
function build_uboot()
|
{
|
SRC=uboot-imx
|
|
pr_warn "start build $SRC"
|
cd $PRJ_PATH/${SRC}
|
|
if [ ! -f .config ] ; then
|
make ARCH=arm ${BOARD}_defconfig
|
fi
|
|
make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} ARCH=arm
|
|
if [[ $BOARD =~ 6ull ]] ; then
|
|
cp u-boot-dtb.imx u-boot-${BOARD}.imx
|
chmod a+x u-boot-${BOARD}.imx
|
set -x
|
cp u-boot-${BOARD}.imx $INST_PATH
|
set +x
|
|
elif [[ $BOARD =~ 8mp ]] ; then
|
|
# install binaries to imx-mkimage
|
set -x
|
cp u-boot.bin $MKIMG_BIN_PATH
|
cp u-boot-nodtb.bin $MKIMG_BIN_PATH
|
cp spl/u-boot-spl.bin $MKIMG_BIN_PATH
|
cp arch/arm/dts/${BOARD}.dtb $MKIMG_BIN_PATH/$IMXBOOT_DTB
|
cp tools/mkimage $MKIMG_BIN_PATH/mkimage_uboot
|
set +x
|
|
fi
|
}
|
|
# i.MX8MP need ATF(ARM Trusted Firmware)
|
function build_atf()
|
{
|
SRC=imx-atf
|
|
pr_warn "start build $SRC"
|
cd $PRJ_PATH/${SRC}
|
|
make -j${JOBS} CROSS_COMPILE=${CROSS_COMPILE} PLAT=$ATF_PLATFORM bl31
|
|
set -x
|
cp build/$ATF_PLATFORM/release/bl31.bin $MKIMG_BIN_PATH
|
set +x
|
}
|
|
# The diagram below illustrate a signed iMX8 flash.bin image layout:
|
# reference: uboot-imx/doc/imx/habv4/guides/mx8m_secure_boot.txt
|
#
|
# +-----------------------------+
|
# | |
|
# | *Signed HDMI/DP FW |
|
# | |
|
# +-----------------------------+
|
# | Padding |
|
# ------- +-----------------------------+ --------
|
# ^ | IVT - SPL | ^
|
# Signed | +-----------------------------+ |
|
# Data | | u-boot-spl.bin | |
|
# | | + | | SPL
|
# v | DDR FW | | Image
|
# ------- +-----------------------------+ |
|
# | CSF - SPL + DDR FW | v
|
# +-----------------------------+ --------
|
# | Padding |
|
# ------- +-----------------------------+ --------
|
# Signed ^ | FDT - FIT | ^
|
# Data | +-----------------------------+ |
|
# v | IVT - FIT | |
|
# ------- +-----------------------------+ |
|
# | CSF - FIT | |
|
# ------- +-----------------------------+ | FIT
|
# ^ | u-boot-nodtb.bin | | Image
|
# | +-----------------------------+ |
|
# Signed | | OP-TEE (Optional) | |
|
# Data | +-----------------------------+ |
|
# | | bl31.bin (ATF) | |
|
# | +-----------------------------+ |
|
# v | u-boot.dtb | v
|
# ------- +-----------------------------+ --------
|
#
|
#
|
# Reference: <<IMX_LINUX_USERS_GUIDE.pdf>> 4.5.13 How to build imx-boot image by using imx-mkimage
|
|
function build_imxboot()
|
{
|
SRC=imx-mkimage
|
|
pr_warn "start build $SRC"
|
cd $PRJ_PATH/${SRC}
|
|
|
cp $PRJ_PATH/firmware-imx/firmware/hdmi/cadence/signed_hdmi_imx8m.bin $MKIMG_BIN_PATH
|
cp $PRJ_PATH/firmware-imx/firmware/ddr/synopsys/lpddr4_pmu_train_[1-2]d_imem_*.bin $MKIMG_BIN_PATH
|
cp $PRJ_PATH/firmware-imx/firmware/ddr/synopsys/lpddr4_pmu_train_[1-2]d_dmem_*.bin $MKIMG_BIN_PATH
|
|
make SOC=$IMX_BOOT_SOC_TARGET REV=A0 $IMXBOOT_TARGETS
|
|
cp $MKIMG_BIN_PATH/flash.bin u-boot-${BOARD}.imx
|
chmod a+x u-boot-${BOARD}.imx
|
|
set -x
|
cp u-boot-${BOARD}.imx $INST_PATH
|
set +x
|
}
|
|
function do_build()
|
{
|
cd $PRJ_PATH
|
|
mkdir -p $INST_PATH
|
|
if [[ $BOARD =~ 6ull ]] ; then
|
|
build_uboot
|
|
elif [[ $BOARD =~ 8mp ]] ; then
|
|
build_atf
|
build_uboot
|
build_imxboot
|
|
fi
|
}
|
|
function do_install()
|
{
|
cd $PRJ_PATH
|
|
pr_info "\nbootloader installed to '$INST_PATH'"
|
ls $INST_PATH && echo ""
|
}
|
|
function do_clean()
|
{
|
for d in $SRCS
|
do
|
rm -rf $PRJ_PATH/$d
|
done
|
|
rm -rf $TARBALL_PATH
|
rm -rf $INST_PATH
|
}
|
|
#+-------------------------+
|
#| Shell script body entry |
|
#+-------------------------+
|
|
cd $PRJ_PATH
|
|
export_env
|
|
if [[ $# == 1 && $1 == -c ]] ;then
|
pr_warn "start clean bootloader"
|
do_clean
|
exit;
|
fi
|
|
pr_warn "start build bootloader for ${BOARD}"
|
|
do_fetch
|
|
do_build
|
|
do_install
|