#!/bin/bash
|
# Description: This shell script used to build linux system SDK. You can
|
# use command line argument to specify build target, or select
|
# it by hand if no argument given by command line. More detail
|
# information can refer to the help message.
|
#
|
# Author: guowenxue <guowenxue@gmail.com>
|
# Version: 1.0.0
|
|
|
PROJ_PATH=`pwd`
|
|
CROSSTOOL=/opt/xtools/arm920t/bin/arm-linux-
|
|
BOARD=fl2440
|
PACK_PATH=${PROJ_PATH}/tarballs
|
PATCH_PATH=${PROJ_PATH}/patches
|
PATCH_SUFIX=${BOARD}.patch
|
|
KERNEL_SRC=linux-3.0
|
UBOOT_SRC=u-boot-2010.09
|
BOOTSTRAP_SRC=bootstrap
|
|
TARBALL_FTP=ftp://master.iot-yun.club/src/
|
|
ROOTFS_SRC=rootfs
|
ROOTFS_IMG=rootfs-${BOARD}.ubi
|
|
TFTP_PATH=/tftp
|
IMGS_PATH=images
|
|
target=
|
|
function show_help()
|
{
|
printf "Usage: $1 [system/bootloader/kernel/rootfs/clean]\n\n"
|
|
echo "system : build all source code "
|
echo "bootloader: build bootstrap and u-boot "
|
echo "kernel : build linux kernel "
|
echo "rootfs : build ubifs rootfs images"
|
printf "clean : clean the source code\n\n"
|
printf "distclean : remove all the source code\n\n"
|
|
exit 0;
|
}
|
|
function show_banner()
|
{
|
echo "+---------------------------------------------+"
|
printf "$1\n"
|
echo "+---------------------------------------------+"
|
}
|
|
function select_target()
|
{
|
targets=("" "system" "bootloader" "kernel" "rootfs" "clean" "distclean")
|
echo "Please select a build target:"
|
echo "1 <system> , build all source code "
|
echo "2 <bootloader>, build bootstrap and u-boot "
|
echo "3 <kernel> , build linux kernel "
|
echo "4 <rootfs> , build ubifs rootfs image"
|
echo "5 <clean> , clean the source code"
|
echo "6 <distclean> , remove all the source code"
|
echo ""
|
|
stty erase '^H'
|
read -p "Enter your choise [1-6] : " choice
|
|
if [ $choice -gt 6 ] ; then
|
echo "ERROR: Invalid input choice, exit now..."
|
exit 1;
|
fi
|
|
target=${targets[$choice]}
|
}
|
|
function check_download_packet()
|
{
|
if [ ! -f ${PACK_PATH}/${SRC_DIR}.tar.bz2 ] ; then
|
if [[ $SRC =~ bootstrap ]] ; then
|
return ;
|
else
|
show_banner "| start download $SRC_DIR packet |"
|
cd ${PACK_PATH}/
|
wget ${TARBALL_FTP}/${SRC_DIR}.tar.bz2
|
cd -
|
fi
|
fi
|
}
|
|
|
# Usage: start_build [SRCDIR: linux-3.0/bootstrap/u-boot-2010.09 ]
|
|
function start_build()
|
{
|
SRC_DIR=$1
|
|
if [ ! -d ${SRC_DIR} ] ; then
|
|
check_download_packet ${SRC_DIR}
|
|
show_banner "| start decompress $SRC_DIR packet |"
|
tar -xjf ${PACK_PATH}/${SRC_DIR}.tar.bz2
|
cd ${SRC_DIR}
|
|
# do patch
|
if [ -f ${PATCH_PATH}/${SRC_DIR}-${PATCH_SUFIX} ] ; then
|
show_banner "| patch for ${SRC_DIR} |"
|
|
# Only u-boot not update cross compiler
|
echo ${SRC_DIR} | grep "u-boot" > /dev/null
|
if [ $? != 0 ] ; then
|
sed -i -e "s|^+CROSSTOOL=.*|+CROSSTOOL=${CROSSTOOL}|g" ${PATCH_PATH}/${SRC_DIR}-${PATCH_SUFIX}
|
fi
|
patch -p1 < ${PATCH_PATH}/${SRC_DIR}-${PATCH_SUFIX}
|
fi
|
|
else
|
cd ${SRC_DIR}
|
fi
|
|
show_banner "| start $SRC_DIR build task... |"
|
chmod a+x build.sh
|
bash build.sh
|
show_banner "| $SRC_DIR build task over! |"
|
|
cd -
|
}
|
|
function build_ubifs()
|
{
|
ubinize_cfg=ubinize.cfg
|
ubimg_tmp=ubi.img
|
|
# Rootfs partition size
|
partition_size=80 # MiB
|
|
# K9F2G08: Nandflash 1 block=64 pages, 1 page = 2048
|
BLOCK_PAGES=64
|
PAGE_SIZE=2048
|
SUBPAGE_SIZE=512
|
|
# Logic Erase Block Size: UBI requires 2 minimum I/O units out of each Physical Erase Block (PEB) for overhead:
|
# 1 for maintaining erase count information, and 1 for maintaining the Volume ID information.
|
#
|
# In FL2440 Linux-3.0 kernel only need 1 PEB, or kernel will throw error:
|
# UBIFS error (pid 1): validate_sb: LEB size mismatch: 126976 in superblock, 129024 real
|
|
PEB_SIZE=`expr $PAGE_SIZE \* $BLOCK_PAGES`
|
LEB_SIZE=`expr $PEB_SIZE - 1 \* $PAGE_SIZE `
|
|
|
#UBI reserves 4 blocks space for management and bad PEB handling operations
|
# 2 PEBs are used to store the UBI volume table
|
# 1 PEB is reserved for wear-leveling purposes;
|
# 1 PEB is reserved for the atomic LEB change operation;
|
# a% of PEBs is reserved for handling bad EBs. The default for NAND is 1%
|
|
PEB_CNT=`expr $partition_size \* 1024 \* 1024 / ${PEB_SIZE}`
|
LEB_CNT=`expr $PEB_CNT - 4 - $PEB_CNT \/ 100 `
|
|
#echo "Parition size ${partition_size}MiB and LEB=$LEB_CNT"
|
printf "\nWARNNING: generete rootfs image need root privilege, please input sudo passwd!\n\n"
|
set -x
|
sudo mkfs.ubifs -F -d ${ROOTFS_SRC} -m ${PAGE_SIZE} -e ${LEB_SIZE} -c $LEB_CNT -o ${ubimg_tmp}
|
set +x
|
|
# vol_size smaller than the actual size of the partition to leave room for Ubifs internal data.
|
# With "vol_type=dynamic", Ubifs will end up using the whole space automatically
|
vol_size=`expr ${partition_size} \- 10`
|
|
printf "[ubifs] \nmode=ubi \nimage=${ubimg_tmp} \nvol_id=0 \nvol_size=${vol_size}MiB\n" > $ubinize_cfg
|
printf "vol_type=dynamic \nvol_name=rootfs \nvol_flags=autoresize\n" >> $ubinize_cfg
|
#cat $ubinize_cfg
|
|
set -x
|
ubinize -o ${ROOTFS_IMG} -m ${PAGE_SIZE} -p ${PEB_SIZE} -s ${SUBPAGE_SIZE} $ubinize_cfg
|
set +x
|
|
rm -f $ubimg_tmp $ubinize_cfg
|
chmod a+x ${ROOTFS_IMG}
|
}
|
|
function build_rootfs()
|
{
|
if [ ! -d ${ROOTFS_SRC} ] ; then
|
printf "\nWARNNING: decompress rootfs need root privilege, please input sudo passwd!\n\n"
|
sudo tar -xjf ${PACK_PATH}/${ROOTFS_SRC}.tar.bz2
|
fi
|
|
show_banner "| start build rootfs image... |"
|
build_ubifs
|
show_banner "| rootfs image build over! |"
|
|
if [ -d ${IMGS_PATH} ] ; then
|
cp -f ${ROOTFS_IMG} ${IMGS_PATH}
|
fi
|
|
if [ -d ${TFTP_PATH} ] ; then
|
cp -f ${ROOTFS_IMG} ${TFTP_PATH}
|
fi
|
|
rm -f ${ROOTFS_IMG}
|
ls ${IMGS_PATH}
|
}
|
|
|
function do_clean()
|
{
|
SRCS="$KERNEL_SRC $UBOOT_SRC $BOOTSTRAP_SRC"
|
|
show_banner "| clean project source code |"
|
|
for dir in $SRCS
|
do
|
if [ -d $dir ] ; then
|
cd $dir
|
bash build.sh clean
|
cd -
|
fi
|
done
|
}
|
|
function do_distclean()
|
{
|
if [ `id -u` != 0 ] ; then
|
printf "\nERROR: distclean need root privilege, please use sudo!\n\n"
|
exit ;
|
fi
|
|
printf "\nWARNNING: Start remove all the source code and images\n\n"
|
rm -rf $KERNEL_SRC $UBOOT_SRC rootfs ${IMGS_PATH}
|
}
|
|
function do_build()
|
{
|
if [ "$target" = "system" ] ; then
|
start_build ${BOOTSTRAP_SRC}
|
start_build ${UBOOT_SRC}
|
start_build ${KERNEL_SRC}
|
build_rootfs
|
|
elif [ "$target" = "bootloader" ] ; then
|
start_build ${BOOTSTRAP_SRC}
|
start_build ${UBOOT_SRC}
|
|
elif [ "$target" = "kernel" ] ; then
|
start_build ${KERNEL_SRC}
|
|
elif [ "$target" = "rootfs" ] ; then
|
build_rootfs
|
|
elif [ "$target" = "clean" ] ; then
|
do_clean
|
|
elif [ "$target" = "distclean" ] ; then
|
do_clean
|
do_distclean
|
|
else
|
show_help
|
fi
|
}
|
|
mkdir -p ${IMGS_PATH}
|
|
# check input arguments for build target
|
if [ $# -ge 1 ] ; then
|
target=$1
|
else
|
select_target
|
fi
|
show_banner "| choose build target [$target] |"
|
|
do_build
|