#!/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/buildroot/cortex-a5/bin/arm-linux- 
 | 
  
 | 
BOARD=sama5d4 
 | 
PACK_PATH=${PROJ_PATH}/tarballs 
 | 
PATCH_PATH=${PROJ_PATH}/patches 
 | 
PATCH_SUFIX=${BOARD}.patch 
 | 
  
 | 
  
 | 
  
 | 
ROOTFS_SRC=rootfs 
 | 
ROOTFS_IMG=rootfs-sama5d4.ubi 
 | 
  
 | 
TFTP_PATH=/tftp 
 | 
IMGS_PATH=images 
 | 
  
 | 
target= 
 | 
  
 | 
set -e 
 | 
  
 | 
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]} 
 | 
} 
 | 
  
 | 
  
 | 
  
 | 
# Usage: start_build [SRCDIR: linux-at91/at91bootstrap/u-boot-at91 ] 
 | 
  
 | 
function start_build() 
 | 
{ 
 | 
    SRC_DIR=$1 
 | 
  
 | 
    if [ ! -d ${SRC_DIR} ] ; then 
 | 
        show_banner "|       start decmpress $SRC_DIR packet       |" 
 | 
        tar -xJf ${PACK_PATH}/${SRC_DIR}.tar.xz  
 | 
        cd ${SRC_DIR} 
 | 
  
 | 
        if [ -f ${PATCH_PATH}/${SRC_DIR}-${PATCH_SUFIX} ] ; then  
 | 
            show_banner "|            patch for ${SRC_DIR}             |" 
 | 
  
 | 
            # update cross compiler 
 | 
            sed -i -e "s|^+CROSSTOOL=.*|+CROSSTOOL=${CROSSTOOL}|g" ${PATCH_PATH}/${SRC_DIR}-${PATCH_SUFIX} 
 | 
  
 | 
            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=200  # MiB  
 | 
  
 | 
    # MT29F4G08: 1 block=64 pages,  1 page = 4096B 
 | 
    BLOCK_PAGES=64 
 | 
    PAGE_SIZE=4096 
 | 
  
 | 
    # 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.  
 | 
  
 | 
    PEB_SIZE=`expr $PAGE_SIZE \* $BLOCK_PAGES` 
 | 
    LEB_SIZE=`expr $PEB_SIZE - 2 \* $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" 
 | 
    set -x  
 | 
    printf "\nWARNNING: generete rootfs image need root privilege, please input sudo passwd!\n\n"  
 | 
    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} $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.xz 
 | 
    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="linux-at91 at91bootstrap u-boot-at91" 
 | 
  
 | 
    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 linux-at91 at91bootstrap u-boot-at91 rootfs ${IMGS_PATH} 
 | 
} 
 | 
  
 | 
function do_build() 
 | 
{ 
 | 
    if   [ "$target" = "system" ] ; then 
 | 
        start_build at91bootstrap 
 | 
        start_build u-boot-at91 
 | 
        start_build linux-at91 
 | 
        build_rootfs 
 | 
  
 | 
    elif [ "$target" = "bootloader" ] ; then 
 | 
        start_build at91bootstrap 
 | 
        start_build u-boot-at91 
 | 
  
 | 
    elif [ "$target" = "kernel" ] ; then 
 | 
        start_build linux-at91 
 | 
  
 | 
    elif [ "$target" = "rootfs" ] ; then 
 | 
        build_rootfs 
 | 
  
 | 
    elif [ "$target" = "clean" ] ; then 
 | 
        do_clean 
 | 
  
 | 
    elif [ "$target" = "distclean" ] ; then 
 | 
        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 
 |