全志Android系统编译及镜像生成过程分析

1 source ./build/envsetup.sh
这步主要就是提供一些shell的function命令工具,具体可通过hmm命令查看envsetup.sh提供的所有命令:

Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- lunch:   lunch -
- tapas:   tapas [  ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
- croot:   Changes directory to the top of the tree.
- m:       Makes from the top of the tree.
- mm:      Builds all of the modules in the current directory, but not their dependencies.
- mmm:     Builds all of the modules in the supplied directories, but not their dependencies.
           To limit the modules being built use the syntax: mmm dir/:target1,target2.
- mma:     Builds all of the modules in the current directory, and their dependencies.
- mmma:    Builds all of the modules in the supplied directories, and their dependencies.
- cgrep:   Greps on all local C/C++ files.
- ggrep:   Greps on all local Gradle files.
- jgrep:   Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- mangrep: Greps on all local AndroidManifest.xml files.
- sepgrep: Greps on all local sepolicy files.
- sgrep:   Greps on all local source files.
- godir:   Go to the directory containing a file.

Environemnt options:
- SANITIZE_HOST: Set to 'true' to use ASAN for all host modules. Note that
                 ASAN_OPTIONS=detect_leaks=0 will be set by default until the
                 build is leak-check clean.

Look at the source to view more functions. The complete list is:
addcompletions add_lunch_combo cgrep check_product check_variant choosecombo chooseproduct choosetype choosevariant core coredump_enable coredump_setup cproj croot findmakefile get_abs_build_var getbugreports get_build_var getdriver getlastscreenshot get_make_command getprebuilt getscreenshotpath getsdcardpath gettargetarch gettop ggrep godir hmm is isviewserverstarted jgrep key_back key_home key_menu lunch _lunch m make mangrep mgrep mm mma mmm mmma pez pid printconfig print_lunch_menu qpid rcgrep resgrep runhat runtest sepgrep set_java_home setpaths set_sequence_number set_stuff_for_environment settitle sgrep smoketest stacks startviewserver stopviewserver systemstack tapas tracedmdump treegrep

2 lunch
//在build/envsetup.sh

function lunch()
{
    local answer  //声明局部变量answer

    if [ "$1" ] ; then 
        answer=$1
    else  //执行lunch时没带参数时的操作
        print_lunch_menu  //输出可选项
        echo -n "Which would you like? [aosp_arm-eng] "
        read answer //接收输入的数字,存放在answer. 输入的数值为22
    fi

    local selection=  //声明局部变量answer

    if [ -z "$answer" ] //如果输入的数字为0时的操作
    then
        selection=aosp_arm-eng
    elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
    then
        if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
        then
            selection=${LUNCH_MENU_CHOICES[$(($answer-1))]} 
        //根据输入的数值作为字符串数组LUNCH_MENU_CHOICES的下标,把相应的字符串内容存放在selection
        //selection=tulip_p1-eng
        fi
    elif (echo -n $answer | grep -q -e "^[^\-][^\-]*-[^\-][^\-]*$")
    then
        selection=$answer
    fi

    if [ -z "$selection" ]  //selection变量为空则结束执行
    then
        echo
        echo "Invalid lunch combo: $answer"
        return 1
    fi

    export TARGET_BUILD_APPS=

    local product=$(echo -n $selection | sed -e "s/-.*$//")  //product=tulip_p1
    check_product $product
    if [ $? -ne 0 ]
    then
        echo
        echo "** Don't have a product spec for: '$product'"
        echo "** Do you have the right repo manifest?"
        product=
    fi

    local variant=$(echo -n $selection | sed -e "s/^[^\-]*-//") // variant=eng
    check_variant $variant
    if [ $? -ne 0 ]
    then
        echo
        echo "** Invalid variant: '$variant'"
        echo "** Must be one of ${VARIANT_CHOICES[@]}"
        variant=
    fi

    if [ -z "$product" -o -z "$variant" ]
    then
        echo
        return 1
    fi

    export TARGET_PRODUCT=$product  //tulip_p1
    export TARGET_BUILD_VARIANT=$variant //eng
    export TARGET_BUILD_TYPE=release

    echo

    set_stuff_for_environment  //设置jdk等环境变量
    printconfig   //输出配置结果。 也可以直接执行此命令查看配置结果.
}

3 extract-bsp
//命令在”android/device/softwinner/common/vendorsetup.sh”文件

function extract-bsp()
{
    CURDIR=$PWD

    get_lichee_out_dir
        //设置环境变量:  LINUXOUT_DIR=lichee/out/sun50iw1p1/android/common
        //            LINUXOUT_MODULE_DIR=lichee/out/sun50iw1p1/android/common/lib/modules/*/*

    get_device_dir
        //设置环境变量: DEVICE=android/device/softwinner/tulip-p1

    cd $DEVICE  //进入android/device/softwinner/tulip-p1目录

    #extract kernel
    if [ -f kernel ] ; then //如android/device/softwinner/tulip-p1/kernel文件存在则删除
        rm kernel
    fi
    cp $LINUXOUT_DIR/bImage kernel  //复制lichee/out/sun50iw1p1/android/common/bImage为kernel
    echo "$DEVICE/bImage copied!"

    #extract linux modules
    if [ -d modules ] ; then //如存在android/device/softwinner/tulip-p1/modules目录则删除
        rm -rf modules
    fi
    mkdir -p modules/modules  //创建目录
    cp -rf $LINUXOUT_MODULE_DIR modules/modules //把内核编译出来的驱动模块复制到modules/modules目录里
    echo "$DEVICE/modules copied!"
    chmod 0755 modules/modules/*

# create modules.mk   
(cat << EOF) > ./modules/modules.mk 
# modules.mk generate by extract-files.sh, do not edit it.
PRODUCT_COPY_FILES += \\
    \$(call find-copy-subdir-files,*,\$(LOCAL_PATH)/modules,system/vendor/modules)
EOF

    cd $CURDIR
}

4 make //编译Android源码
//主要执行build/core/main.mk


5 pack
//命令在”android/device/softwinner/common/vendorsetup.sh”文件

function pack()
{
    T=$(gettop)   // T=android源码根目录
    get_device_dir  //设置DEVICE=android/device/softwinner/tulip-p1
    export ANDROID_IMAGE_OUT=$OUT // ANDROID_IMAGE_OUT=android/out/target/product/tulip-p1
    export PACKAGE=$T/../lichee/tools/pack  

    verity_data_init

    sh $DEVICE/package.sh $*  //最后执行android/device/softwinner/tulip-p1/package.sh脚本
}

android/device/softwinner/tulip-p1/package.sh脚本里的内容:

cd $PACKAGE  //进入lichee/tools/pack目录

chip=sun50iw1p1
platform=android
board=p1
debug=uart0
sigmode=none
securemode=none

usage()
{
    printf "Usage: pack [-cCHIP] [-pPLATFORM] [-bBOARD] [-d] [-s] [-v] [-h]
    -c CHIP (default: $chip)
    -p PLATFORM (default: $platform)
    -b BOARD (default: $board)
    -d pack firmware with debug info output to card0
    -s pack firmware with signature
    -v pack firmware with secureboot
    -h print this help message
"
}

while getopts "c:p:b:dsvh" arg
do
    case $arg in
        c)
            chip=$OPTARG
            ;;
        p)
            platform=$OPTARG
            ;;
        b)
            board=$OPTARG
            ;;
        d)
            debug=card0
            ;;
        s)
            sigmode=sig
            ;;
        v)
            securemode=secure
            ;;
        h)
            usage
            exit 0
            ;;
        ?)
            exit 1
            ;;
    esac
done

./pack -c $chip -p $platform -b $board -d $debug -s $sigmode -v $securemode
// ./pack -c sun50iw1p1 -p android -b p1 -d uart0 -s none -v none
//执行lichee/tools/pack/目录下的pack脚本

lichee/tools/pack/pack脚本里的主要操作:

do_prepare
do_ini_to_dts   //生成设备树dtb文件
do_common
do_pack_${PACK_PLATFORM}   // do_pack_android
do_finish      //生成镜成

具体分析pack的操作:

PACK_CHIP=${FLAGS_chip}  //sun50iw1p1
PACK_PLATFORM=${FLAGS_platform} //android
PACK_BOARD=${FLAGS_board} //p1
PACK_KERN=${FLAGS_kernel} //空
PACK_DEBUG=${FLAGS_debug_mode} //uart0
PACK_SIG=${FLAGS_signture}  //none
PACK_SECURE=${FLAGS_secure} //none
PACK_MODE=${FLAGS_mode}    //normal
PACK_FUNC=${FLAGS_function} //android

ROOT_DIR=`pwd`
TOOLS_DIR="${ROOT_DIR}/pctools/linux"
LICHEE_OUT="../../out/${PACK_CHIP}/${PACK_PLATFORM}/common"
  // LICHEE_OUT=lichee/out/sun50iw1p1/android/common
OTA_TEST_NAME="ota_test"

tools_file_list=(
common/tools/split_xxxx.fex
chips/${PACK_CHIP}/tools/split_xxxx.fex
common/tools/usbtool_test.fex
chips/${PACK_CHIP}/tools/usbtool_test.fex
common/tools/cardscript.fex
common/tools/cardscript_secure.fex
chips/${PACK_CHIP}/tools/cardscript.fex
chips/${PACK_CHIP}/tools/cardscript_secure.fex
common/tools/cardtool.fex
chips/${PACK_CHIP}/tools/cardtool.fex
common/tools/usbtool.fex
chips/${PACK_CHIP}/tools/usbtool.fex
common/tools/aultls32.fex
chips/${PACK_CHIP}/tools/aultls32.fex
common/tools/aultools.fex
chips/${PACK_CHIP}/tools/aultools.fex
)

configs_file_list=(
common/toc/toc1.fex
common/toc/toc0.fex
common/toc/boot_package.fex
common/dtb/sunxi.fex
common/imagecfg/*.cfg
common/partition/sys_partition_dump.fex
common/partition/sys_partition_private.fex
chips/${PACK_CHIP}/configs/default/*
chips/${PACK_CHIP}/configs/${PACK_BOARD}/*.fex
chips/${PACK_CHIP}/configs/${PACK_BOARD}/*.cfg
)

boot_resource_list=(
chips/${PACK_CHIP}/boot-resource/boot-resource:out/
chips/${PACK_CHIP}/boot-resource/boot-resource.ini:out/
chips/${PACK_CHIP}/configs/${PACK_BOARD}/*.bmp:out/boot-resource/
)

boot_file_list=(
chips/${PACK_CHIP}/bin/boot0_nand_${PACK_CHIP}.bin:out/boot0_nand.fex
chips/${PACK_CHIP}/bin/boot0_sdcard_${PACK_CHIP}.bin:out/boot0_sdcard.fex
chips/${PACK_CHIP}/bin/boot0_spinor_${PACK_CHIP}.bin:out/boot0_spinor.fex
chips/${PACK_CHIP}/bin/fes1_${PACK_CHIP}.bin:out/fes1.fex
chips/${PACK_CHIP}/bin/u-boot-${PACK_CHIP}.bin:out/u-boot.fex
chips/${PACK_CHIP}/bin/bl31.bin:out/monitor.fex
chips/${PACK_CHIP}/bin/scp.bin:out/scp.fex
chips/${PACK_CHIP}/bin/u-boot-spinor-${PACK_CHIP}.bin:out/u-boot-spinor.fex
chips/${PACK_CHIP}/bin/boot0_nand_${PACK_CHIP}-${OTA_TEST_NAME}.bin:out/boot0_nand-${OTA_TEST_NAME}.fex
chips/${PACK_CHIP}/bin/boot0_sdcard_${PACK_CHIP}-${OTA_TEST_NAME}.bin:out/boot0_sdcard-${OTA_TEST_NAME}.fex
chips/${PACK_CHIP}/bin/boot0_spinor_${PACK_CHIP}-${OTA_TEST_NAME}.bin:out/boot0_spinor-${OTA_TEST_NAME}.fex
chips/${PACK_CHIP}/bin/u-boot-${PACK_CHIP}-${OTA_TEST_NAME}.bin:out/u-boot-${OTA_TEST_NAME}.fex
chips/${PACK_CHIP}/bin/u-boot-spinor-${PACK_CHIP}-${OTA_TEST_NAME}.bin:out/u-boot-spinor-${OTA_TEST_NAME}.fex
)

boot_file_secure=(
chips/${PACK_CHIP}/bin/semelis_${PACK_CHIP}.bin:out/semelis.bin
chips/${PACK_CHIP}/bin/sboot_${PACK_CHIP}.bin:out/sboot.bin
chips/${PACK_CHIP}/bin/sboot_${PACK_CHIP}-${OTA_TEST_NAME}.bin:out/sboot-${OTA_TEST_NAME}.bin
)

a64_boot_file_secure=(
chips/${PACK_CHIP}/bin/optee_${PACK_CHIP}.bin:out/optee.fex
chips/${PACK_CHIP}/bin/sboot_${PACK_CHIP}.bin:out/sboot.bin
# chips/${PACK_CHIP}/bin/sboot_${PACK_CHIP}-${OTA_TEST_NAME}.bin:out/sboot-${OTA_TEST_NAME}.bin
)

function do_prepare()
{
    //检查是否已指定好要打包的芯片型号及平台信息等
    if [ -z "${PACK_CHIP}" -o -z "${PACK_PLATFORM}" -o -z "${PACK_BOARD}" ] ; then
        pack_error "Invalid parameters Chip: ${PACK_CHIP}, \
            Platform: ${PACK_PLATFORM}, Board: ${PACK_BOARD}"
        show_boards
        exit 1
    fi

    if [ ! -d chips/${PACK_CHIP}/configs/${PACK_BOARD} ] ; then
        pack_error "Board's directory \
            \"chips/${PACK_CHIP}/configs/${PACK_BOARD}\" is not exist."
        show_boards
        exit 1
    fi

    if [ -z "${PACK_KERN}" ] ; then
        printf "No kernel param, parse it from .buildconfig\n"
        PACK_KERN=`awk -F LICHEE_KERN_VER= '{printf $2}' ../../.buildconfig` 
            // PACK_KERN=linux-3.10
        if [ -z "${PACK_KERN}" ] ; then
            pack_error "Failed to parse kernel param from .buildconfig"
            exit 1
        fi
    fi

    # Cleanup
    rm -rf out/
    mkdir -p out/

    printf "copying tools file\n"
    //把lichee/tools/pack/目录下的common和chips子目录里的fex文件复制到lichee/tools/pack/out目录里
    for file in ${tools_file_list[@]} ; do
        cp -f $file out/ 2> /dev/null
    done

    printf "copying configs file\n"
    //把lichee/tools/pack/目录下的common和chips子目录里的fex文件复制到lichee/tools/pack/out目录里
    for file in ${configs_file_list[@]} ; do
        cp -f $file out/ 2> /dev/null
    done
    # If platform config files exist, we will cover the default files
    # For example, mv out/image_linux.cfg out/image.cfg
    find ./out/* -type f -a \( -name "*.fex" -o -name "*.cfg" \) -print | \
        sed "s#\(.*\)_${PACK_PLATFORM}\(\..*\)#mv -fv & \1\2#e"

    if [ "x${PACK_MODE}" = "xdump" ] ; then // PACK_MODE=normal
        cp -vf out/sys_partition_dump.fex out/sys_partition.fex
        cp -vf out/usbtool_test.fex out/usbtool.fex
    elif [ "x${PACK_FUNC}" = "xprvt" ] ; then  // PACK_FUNC=android
        cp -vf out/sys_partition_private.fex out/sys_partition.fex
    fi

    printf "copying boot resource\n"  //复制boot_resource_list数组里指定的文件
    for file in ${boot_resource_list[@]} ; do
        cp -rf `echo $file | awk -F: '{print $1}'` \
            `echo $file | awk -F: '{print $2}'` 2>/dev/null
    done

    printf "copying boot file\n"    //复制boot_file_list数组里指定的文件
    for file in ${boot_file_list[@]} ; do
        cp -f `echo $file | awk -F: '{print $1}'` \
            `echo $file | awk -F: '{print $2}'` 2>/dev/null
    done

    if [ "x${ARCH}" != "xarm64" ] ; then
        if [ "x${PACK_SECURE}" = "xsecure" -o "x${PACK_SIG}" = "xsecure" -o  "x${PACK_SIG}" = "xprev_refurbish" ] ; then
            printf "copying secure boot file\n"
            for file in ${boot_file_secure[@]} ; do
                cp -f `echo $file | awk -F: '{print $1}'` \
                    `echo $file | awk -F: '{print $2}'`
            done
        fi
    else
        if [ "x${PACK_SECURE}" = "xsecure" -o "x${PACK_SIG}" = "xsecure" -o  "x${PACK_SIG}" = "xprev_refurbish" ] ; then
            printf "copying arm64 secure boot file\n"
            for file in ${a64_boot_file_secure[@]} ; do
                cp -f `echo $file | awk -F: '{print $1}'` \
                    `echo $file | awk -F: '{print $2}'`
            done
        fi
    fi

    if [ "x${PACK_SECURE}" = "xsecure"  -o "x${PACK_SIG}" = "xsecure" ] ; then
        printf "add burn_secure_mode in target in sys config\n"
        sed -i -e '/^\[target\]/a\burn_secure_mode=1' out/sys_config.fex
        sed -i -e '/^\[platform\]/a\secure_without_OS=0' out/sys_config.fex
    elif [ "x${PACK_SIG}" = "xprev_refurbish" ] ; then
        printf "add burn_secure_mode in target in sys config\n"
        sed -i -e '/^\[target\]/a\burn_secure_mode=1' out/sys_config.fex
        sed -i -e '/^\[platform\]/a\secure_without_OS=1' out/sys_config.fex
    else
        sed -i '/^burn_secure_mod/d' out/sys_config.fex
        sed -i '/^secure_without_OS/d' out/sys_config.fex
    fi

    if [ "x${PACK_MODE}" = "xota_test" ] ; then
        printf "copy ota test file\n"
        copy_ota_test_file
    fi

    # Here, we can switch uart to card or normal
    if [ "x${PACK_DEBUG}" = "xcard0" -a "x${PACK_MODE}" != "xdump" \
        -a "x${PACK_FUNC}" != "xprvt" ] ; then \
        uart_switch
    fi

    sed -i 's/\\boot-resource/\/boot-resource/g' out/boot-resource.ini
    sed -i 's/\\\\/\//g' out/image.cfg
    sed -i 's/^imagename/;imagename/g' out/image.cfg

    IMG_NAME="${PACK_CHIP}_${PACK_PLATFORM}6.0_${PACK_BOARD}_${PACK_DEBUG}"

    if [ "x${PACK_SIG}" != "xnone" ]; then
        IMG_NAME="${IMG_NAME}_${PACK_SIG}"
    fi

    if [ "x${PACK_MODE}" = "xdump" -o "x${PACK_MODE}" = "xota_test" ] ; then
        IMG_NAME="${IMG_NAME}_${PACK_MODE}"
    fi

    if [ "x${PACK_FUNC}" = "xprvt" ] ; then
        IMG_NAME="${IMG_NAME}_${PACK_FUNC}"
    fi

    if [ "x${PACK_SECURE}" = "xsecure" ] ; then
        IMG_NAME="${IMG_NAME}_${PACK_SECURE}"
    fi

    if [ "x${PACK_FUNC}" = "xprev_refurbish" ] ; then
        IMG_NAME="${IMG_NAME}_${PACK_FUNC}"
    fi

    IMG_NAME="${IMG_NAME}_bv3.img"

    echo "imagename = $IMG_NAME" >> out/image.cfg
    echo "" >> out/image.cfg
}

//
function do_ini_to_dts()
{
    if [ "x${PACK_KERN}" != "xlinux-3.10" ] ; then // PACK_KERN=linux-3.10
        return
    fi

    //声明局部变量及初始值
    local DTC_COMPILER=${LICHEE_OUT}/../../../../$PACK_KERN/scripts/dtc/dtc
    local DTC_DEP_FILE=${LICHEE_OUT}/../../../../$PACK_KERN/arch/$ARCH/boot/dts/.${PACK_CHIP}-${PACK_BOARD}.dtb.d.dtc.tmp
    local DTC_SRC_PATH=${LICHEE_OUT}/../../../../$PACK_KERN/arch/$ARCH/boot/dts/
    local DTC_SRC_FILE=${LICHEE_OUT}/../../../../$PACK_KERN/arch/$ARCH/boot/dts/.${PACK_CHIP}-${PACK_BOARD}.dtb.dts
    local DTC_INI_FILE_BASE=${LICHEE_OUT}/../../../../tools/pack/out/sys_config.fex
    local DTC_INI_FILE=${LICHEE_OUT}/../../../../tools/pack/out/sys_config_fix.fex
    cp $DTC_INI_FILE_BASE $DTC_INI_FILE
    sed -i "s/\(\[dram\)_para\(\]\)/\1\2/g" $DTC_INI_FILE
    sed -i "s/\(\[nand[0-9]\)_para\(\]\)/\1\2/g" $DTC_INI_FILE

    if [ ! -f $DTC_COMPILER ]; then 
        pack_error "Script_to_dts: Can not find dtc compiler.\n"
        exit 1
    fi
    if [ ! -f $DTC_DEP_FILE ]; then //内核源码目录下不存在arch/arm64/boot/dts/.sun50iw1p1-p1.dtb.d.dtc.tmp文件
        printf "Script_to_dts: Can not find [%s-%s.dts]. Will use common dts file instead.\n" ${PACK_CHIP} ${PACK_BOARD}
        DTC_DEP_FILE=${LICHEE_OUT}/../../../../$PACK_KERN/arch/$ARCH/boot/dts/.${PACK_CHIP}-soc.dtb.d.dtc.tmp
        DTC_SRC_FILE=${LICHEE_OUT}/../../../../$PACK_KERN/arch/$ARCH/boot/dts/.${PACK_CHIP}-soc.dtb.dts
    fi
    $DTC_COMPILER -O dtb -o ${LICHEE_OUT}/sunxi.dtb    \
        -b 0            \
        -i $DTC_SRC_PATH   \
        -F $DTC_INI_FILE   \
        -d $DTC_DEP_FILE $DTC_SRC_FILE
    // dtc -O dtb -o lichee/out/sun50iw1p1/android/common/sunxi.dtb \
    //       -b 0 -i lichee/linux-3.10/arch/arm64/boot/dts          \
     //         -F  lichee/tools/pack/out/sys_config_fix.fex          \    
        //      -d  lichee/linux-3.10/arch/arm64/boot/dts/.sun50iw1p1-soc.dtb.d.dtc.tmp  \
      //          lichee/linux-3.10/arch/arm64/boot/dts/.sun50iw1p1-soc.dtb.dts   

    //生成的设备树dtb文件在lichee/out/sun50iw1p1/android/common/sunxi.dtb

    if [ $? -ne 0 ]; then
        pack_error "Conver script to dts failed"
        exit 1
    fi

    #restore the orignal dtsi
    if [ "x${ARCH}" = "xarm64" ]; then
        if [ "x${PACK_PLATFORM}" = "xdragonboard" ]; then  //不成立
            local DTS_PATH=../../linux-3.10/arch/arm64/boot/dts/
            if [ -f ${DTS_PATH}/sun50iw1p1_bak.dtsi ];then
                rm -f ${DTS_PATH}/sun50iw1p1.dtsi
                mv  ${DTS_PATH}/sun50iw1p1_bak.dtsi  ${DTS_PATH}/sun50iw1p1.dtsi
            fi
        fi
    fi

    printf "Conver script to dts ok.\n"
    return
}

//
function do_common()
{
    cd out/   //进入lichee/tools/pack/out目录

    busybox unix2dos sys_config.fex //转换文件为dos格式
    busybox unix2dos sys_partition.fex //转换文件为dos格式
    script  sys_config.fex > /dev/null
    script  sys_partition.fex > /dev/null
    cp -f   sys_config.bin config.fex

    if [ "x${PACK_PLATFORM}" = "xdragonboard" ] ; then //不成立
        busybox dos2unix test_config.fex
        cp test_config.fex boot-resource/
        busybox unix2dos test_config.fex
        script test_config.fex > /dev/null
        cp test_config.bin boot-resource/
    fi

    # Those files for SpiNor. We will try to find sys_partition_nor.fex
    if [ -f sys_partition_nor.fex ];  then //没有此文件

        if [ -f "${LICHEE_OUT}/sunxi.dtb" ]; then
            cp ${LICHEE_OUT}/sunxi.dtb sunxi.fex
            update_uboot_fdt u-boot-spinor.fex sunxi.fex u-boot-spinor.fex >/dev/null
        fi

        # Here, will create sys_partition_nor.bin
        busybox unix2dos sys_partition_nor.fex
        script  sys_partition_nor.fex > /dev/null
        update_boot0 boot0_spinor.fex   sys_config.bin SDMMC_CARD > /dev/null
        update_uboot u-boot-spinor.fex  sys_config.bin >/dev/null

        if [ -f boot_package_nor.cfg ]; then
            echo "pack boot package"
            busybox unix2dos boot_package.cfg
            dragonsecboot -pack boot_package_nor.cfg
            cp boot_package.fex boot_package_nor.fex
        fi
        # Ugly, but I don't have a better way to change it.
        # We just set env's downloadfile name to env_nor.cfg in sys_partition_nor.fex
        # And if env_nor.cfg is not exist, we should copy one.
        if [ ! -f env_nor.cfg ]; then
            cp -f env.cfg env_nor.cfg >/dev/null 2<&1
        fi

        # Fixup boot mode for SPINor, just can bootm
        sed -i '/^boot_normal/s#\#bootm#g' env_nor.cfg

        u_boot_env_gen env_nor.cfg env_nor.fex >/dev/null
    fi

        // updata_uboot*和update_boot*命令工具在lichee/tools/pack/pctools/linux/mod_update/目录里
    if [ -f "${LICHEE_OUT}/sunxi.dtb" ]; then //成立
        cp ${LICHEE_OUT}/sunxi.dtb sunxi.fex
        update_uboot_fdt u-boot.fex sunxi.fex u-boot.fex >/dev/null
    fi

    # Those files for Nand or Card
    update_boot0 boot0_nand.fex sys_config.bin NAND > /dev/null
    update_boot0 boot0_sdcard.fex   sys_config.bin SDMMC_CARD > /dev/null
    update_uboot u-boot.fex         sys_config.bin > /dev/null
    update_fes1  fes1.fex           sys_config.bin > /dev/null
    fsbuild      boot-resource.ini  split_xxxx.fex > /dev/null

    if [ -f boot_package.cfg ]; then //文件存在
            echo "pack boot package"
            busybox unix2dos boot_package.cfg
            dragonsecboot -pack boot_package.cfg
    fi

    if [ "x${PACK_FUNC}" = "xprvt" ] ; then
        u_boot_env_gen env_burn.cfg env.fex > /dev/null
    else
        u_boot_env_gen env.cfg env.fex > /dev/null
    fi

    if [ -f "$LICHEE_OUT/arisc" ]; then
        ln -s $LICHEE_OUT/arisc arisc.fex
    fi
}

/

function do_pack_android()
{
    printf "packing for android\n"

    //ANDROID_IMAGE_OUT=android/out/target/product/tulip-p1
    if [ -z "${ANDROID_IMAGE_OUT}" ] ; then
        pack_error "please specify ANDROID_IMAGE_OUT env"
        exit 1
    fi

    ln -s ${ANDROID_IMAGE_OUT}/boot.img boot.fex
    ln -s ${ANDROID_IMAGE_OUT}/system.img system.fex
    ln -s ${ANDROID_IMAGE_OUT}/recovery.img recovery.fex

    if [ -f ${ANDROID_IMAGE_OUT}/verity_block.img ] ; then
                ln -s ${ANDROID_IMAGE_OUT}/verity_block.img verity_block.fex
        fi

    if [ -f ${ANDROID_IMAGE_OUT}/userdata.img ] ; then
        ln -s ${ANDROID_IMAGE_OUT}/userdata.img userdata.fex
    fi


    if [ "x${PACK_SIG}" = "xsig" ] ; then // PACK_SIG=none 不成立
        echo "signature sunxi mbr"
        signature sunxi_mbr.fex dlinfo.fex
        echo "signature over"
    elif [ "x${PACK_SECURE}" = "xsecure" ] ; then // PACK_SECURE=none
        echo "secure"
        do_signature
    elif [ "x${PACK_SIG}" = "xprev_refurbish" ] ; then //不成立
        echo "prev_refurbish"
        do_signature
    else
        echo "normal"
    fi
}

function do_finish()
{
    # Yeah, it should contain all files into full_img.fex for spinor
    # Because, as usually, spinor image size is very small.
    # If fail to create full_img.fex, we should fake it empty.

    # WTF, it is so ugly!!! It must be sunxi_mbr.fex & sys_partition.bin,
    # not sunxi_mbr_xxx.fex & sys_partition_xxx.bin. In order to advoid this
    # loathsome thing, we need to backup & copy files. Check whether
    # sys_partition_nor.bin is exist, and create sunxi_mbr.fex for Nor.
    if [ -f sys_partition_nor.bin ]; then  //没有此文件, 不成立
        mv -f sys_partition.bin         sys_partition.bin_back
        cp -f sys_partition_nor.bin     sys_partition.bin
        update_mbr                      sys_partition.bin 1 > /dev/null
        #when use devicetree, the size of uboot+dtb is larger then 256K
        if [ "x${PACK_KERN}" = "xlinux-3.10" ] ; then
            BOOT1_FILE=boot_package_nor.fex
            LOGIC_START=496 #496+16=512K
            merge_full_img --out full_img.fex \
                      --boot0 boot0_spinor.fex \
                      --boot1 ${BOOT1_FILE} \
                  --mbr sunxi_mbr.fex \
                  --logic_start ${LOGIC_START} \
                  --partition sys_partition.bin
            if [ $? -ne 0 ]; then
                pack_error "merge_full_img failed"
                exit 1
            fi
        else
            #BOOT1_FILE=u-boot-spinor.fex
            #LOGIC_START=240 #240+16=256K
            merge_package full_img.fex boot0_spinor.fex \
                    u-boot-spinor.fex sunxi_mbr.fex sys_partition.bin
        fi

        mv -f sys_partition.bin_back    sys_partition.bin
    fi
    if [ ! -f full_img.fex ]; then
        echo "full_img.fex is empty" > full_img.fex
    fi

    // update_mbr工具在lichee/tools/pack/pctools/linux/mod_update/目录
    // dragon工具在lichee/tools/pack/pctools/linux/eDragonEx/目录
    update_mbr          sys_partition.bin 4 > /dev/null
    dragon image.cfg    sys_partition.fex  //生成镜像
        if [ $? -eq 0 ]; then
        if [ -e ${IMG_NAME} ]; then
            mv ${IMG_NAME} ../${IMG_NAME}
            echo '----------image is at----------'
            echo -e '\033[0;31;1m'
            echo ${ROOT_DIR}/${IMG_NAME}
            echo -e '\033[0m'
        fi
        fi
    cd ..
    printf "pack finish\n"
}

你可能感兴趣的:(全志Android系统编译及镜像生成过程分析)