#!/bin/bash
|
|
PRJ_PATH=`pwd`
|
BOARD=evk_8mp
|
|
ANDROID_VER=imx-android-12.0.0_1.0.0
|
IMAGE_PATH=${PRJ_PATH}/android_image
|
IMAGE_NAME=${ANDROID_VER}-${BOARD}.img
|
IMAGE_SIZE=14336M #14G
|
|
JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
|
LOOP_DEV=`losetup -f | cut -d/ -f3`
|
|
set -e
|
|
# display in green
|
function pr_info() {
|
echo -e "\033[40;32m --I-- $1 \033[0m"
|
}
|
|
function exit_handler()
|
{
|
pr_info "Shell script exit now, do some clean work"
|
|
set +e
|
|
if [ -L /dev/mapper/${LOOP_DEV}p1 ] ; then
|
echo "kpartx -dv /dev/${LOOP_DEV}"
|
kpartx -dv /dev/${LOOP_DEV} > /dev/null
|
fi
|
|
losetup -a | grep "${LOOP_DEV}" > /dev/null 2>&1
|
if [ $? == 0 ] ; then
|
echo "losetup -d /dev/${LOOP_DEV}"
|
losetup -d /dev/${LOOP_DEV} > /dev/null
|
fi
|
}
|
|
function do_systool()
|
{
|
if command -v simg2img > /dev/null 2>&1 ; then
|
pr_info "system tools installed already."
|
return ;
|
fi
|
|
pr_info "Start install system tools."
|
# ubuntu22.04 miss android-sdk-ext4-utils
|
sudo apt install -y uuid uuid-dev zlib1g-dev liblz-dev liblzo2-2 liblzo2-dev make libssl-dev \
|
lzop git curl u-boot-tools mtd-utils device-tree-compiler gdisk m4 bison flex gcc-multilib \
|
android-sdk-libsparse-utils android-sdk android-sdk-build-tools libncurses5
|
|
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
|
}
|
|
function do_fetch()
|
{
|
if [ -f ${PRJ_PATH}/${ANDROID_VER}/imx_android_setup.sh ] ; then
|
pr_info "android source code fetch already."
|
return;
|
fi
|
|
pr_info "Start fetch source code."
|
mkdir -p ${PRJ_PATH}/bin
|
curl https://storage.googleapis.com/git-repo-downloads/repo > ${PRJ_PATH}/bin/repo
|
chmod a+x ${PRJ_PATH}/bin/repo
|
|
export PATH=${PATH}:${PRJ_PATH}/bin
|
|
|
if [ ! -d $ANDROID_VER ] ; then
|
tar -xzf $ANDROID_VER.tar.gz
|
fi
|
|
source ${PRJ_PATH}/${ANDROID_VER}/imx_android_setup.sh
|
|
# By default, after preceding command finishes execution, current working directory changed to the
|
# i.MX Android source code root directory.
|
# ${MY_ANDROID} will be refered as the i.MX Android source code root directory in all i.MX Andorid
|
# release documentation
|
|
export MY_ANDROID=${PRJ_PATH}
|
}
|
|
function do_build()
|
{
|
IMX_TARGET="bootloader kernel mxmwifi dtboimage bootimage vendorbootimage vendorimage"
|
|
pr_info "Start build android source code."
|
|
export AARCH64_GCC_CROSS_COMPILE=aarch64-linux-gnu-
|
export MY_ANDROID=${PRJ_PATH}/android_build
|
|
cd $MY_ANDROID
|
source build/envsetup.sh
|
#lunch ${BOARD}-eng
|
lunch ${BOARD}-userdebug
|
|
# Build U-Boot/kernel with imx-make.sh first, but not to build Android images.
|
./imx-make.sh -j${JOBS} ${IMX_TARGET} 2>&1 | tee build-log.txt
|
|
# Start the process of build Android images with "make" function.
|
make -j${JOBS} 2>&1 | tee -a build-log.txt
|
|
# output in ${MY_ANDROID}/out/target/product/${BOARD}
|
ls out/target/product/${BOARD}/*.img
|
}
|
|
function do_install()
|
{
|
export MY_ANDROID=${PRJ_PATH}/android_build
|
|
IMAGE_FILES="boot.img dtbo-imx8mp.img partition-table.img super.img \
|
u-boot-imx8mp.imx vbmeta-imx8mp.img vendor_boot.img \
|
fastboot_imx_flashall.* imx-sdcard-partition.* uuu_imx_android_flash.* "
|
|
pr_info "Start install output images"
|
|
mkdir -p $IMAGE_PATH && rm -f $IMAGE_PATH/*
|
cd ${MY_ANDROID}/out/target/product/${BOARD}/
|
|
for f in $IMAGE_FILES
|
do
|
cp $f ${IMAGE_PATH}
|
done
|
|
cd $PRJ_PATH
|
}
|
|
function do_image()
|
{
|
cd $IMAGE_PATH
|
|
# Generate android system image file and losetup for it
|
pr_info "Start generate android system images: ${IMAGE_NAME}"
|
cp partition-table.img ${IMAGE_NAME}
|
chmod +x ${IMAGE_NAME}
|
|
truncate -s ${IMAGE_SIZE} ${IMAGE_NAME}
|
sgdisk -e ${IMAGE_NAME}
|
gdisk -l ${IMAGE_NAME}
|
|
losetup /dev/${LOOP_DEV} ${IMAGE_NAME}
|
kpartx -av /dev/${LOOP_DEV} > /dev/null
|
|
# Android system image partition table and written images
|
part1=(dtbo_a:dtbo-imx8mp.img)
|
part2=(dtbo_b:dtbo-imx8mp.img)
|
part3=(boot_a:boot.img)
|
part4=(boot_b:boot.img)
|
part5=(vendor_boot_a:vendor_boot.img)
|
part6=(vendor_boot_b:vendor_boot.img)
|
part7=(misc:zero)
|
part8=(metadata:ext4)
|
part9=(presistdata:zero)
|
part10=(super:super.img)
|
part11=(userdata:ext4)
|
part12=(fbmisc:zero)
|
part13=(vbmeta_a:vbmeta-imx8mp.img)
|
part14=(vbmeta_b:vbmeta-imx8mp.img)
|
|
pr_info "Start write built output images to android system images"
|
for i in `seq 1 14`
|
do
|
eval var=\${part${i}[@]}
|
for info in ${var}
|
do
|
partnum=$i
|
partname=`echo ${info} | cut -d: -f1`
|
imgfile=`echo ${info} | cut -d: -f2`
|
|
echo "Flash partition${partnum}($partname): $imgfile "
|
|
if [ "$imgfile" = "zero" ] ; then
|
size=`gdisk -l ${IMAGE_NAME} | grep -w ${partname} | awk '{print $4}' | cut -d. -f1`
|
unit=`gdisk -l ${IMAGE_NAME} | grep -w ${partname} | awk '{print $5}'`
|
dd if=/dev/zero of=/dev/mapper/${LOOP_DEV}p${partnum} bs=1${unit} count=${size} conv=fsync,nocreat status=none
|
|
elif [ "$imgfile" = "ext4" ] ; then
|
mkfs.ext4 -F /dev/mapper/${LOOP_DEV}p${partnum} -L${partname} > /dev/null 2>&1
|
|
elif [ "$imgfile" = "super.img" ] ; then
|
simg2img $imgfile /dev/mapper/${LOOP_DEV}p${partnum}
|
|
else
|
dd if=$imgfile of=/dev/mapper/${LOOP_DEV}p${partnum} bs=10M conv=fsync,nocreat status=none
|
|
fi
|
done
|
done
|
|
pr_info "Start write u-boot image to android system images"
|
dd if=u-boot-imx8mp.imx of=/dev/${LOOP_DEV} bs=1k seek=32 conv=fsync status=none
|
|
sync
|
pr_info "generate ${IMAGE_NAME} successful!"
|
}
|
|
function do_usage()
|
{
|
echo ""
|
echo "Usage:"
|
echo " $0 [-b] [-g] [-h]"
|
echo " -b : build android source code SDK"
|
echo " -g : generate android system image"
|
echo " -h : show this help message"
|
echo ""
|
echo " WARNNING: Generate android image need run as sudo"
|
echo ""
|
echo " Build Example: $0 -b && sudo $0 -g"
|
echo ""
|
exit;
|
}
|
|
action="usage"
|
|
while getopts "bgh" OPTNAME
|
do
|
case "${OPTNAME}" in
|
"b")
|
action="build"
|
shift
|
;;
|
|
"g")
|
action="generate"
|
shift
|
;;
|
|
"h")
|
action="usage"
|
;;
|
esac
|
done
|
|
trap 'exit_handler' EXIT
|
|
#do_systool
|
|
if [ $action == "usage" ] ; then
|
do_usage
|
|
elif [ $action == "build" ] ; then
|
do_fetch
|
do_build
|
do_install
|
|
elif [ $action == "generate" ] ; then
|
do_image
|
|
fi
|