嵌入式Linux之RK3399:内核buildroot的emmc-img编译原理(一)

前提是你 提交于 2020-01-25 09:13:36
一、脚本分析

在sdk的根目录下有自动化脚本build.sh,build.sh里面定义了emmc-img的功能函数。

function build_emmcimg() {
    local IMG=
    local ROOTFS=
#如果OSNAME未定义,则定义为buildroot
    if [ -z ${OS_NAME} ]; then
        OS_NAME=buildroot
    fi
    if [ x${OS_NAME} = xbuildroot ]; then
        IMG=rk3399-eflasher-buildroot-linux-4.4-arm64-$(date +%Y%m%d).img
        ROOTFS=${TOP_DIR}/buildroot/output/rockchip_rk3399/images/rootfs.ext2
    elif [ x${OS_NAME} = xdebian ]; then
        IMG=rk3399-eflasher-debian9-4.4-armhf-$(date +%Y%m%d).img
        ROOTFS=${TOP_DIR}/rootfs/binary
    else
        echo "Unknow OS: ${OS_NAME}"
        exit 1
    fi
    prepare_image_for_friendlyelec_eflasher ${OS_NAME} ${ROOTFS} && (cd ${SDFUSE_DIR} && ./mk-emmc-image.sh ${OS_NAME}) && {
        echo "-----------------------------------------"
        echo "Run the following for sdcard install:"
        echo "    dd if=out/${IMG} of=/dev/sdX bs=1M"
        echo "-----------------------------------------"
    }
}

显然,在emmc-img函数里面真正起作用的是prepare_image_for_friendlyelec_eflasher函数,该函数同样在build.sh里面定义。

function copy_and_verify(){
	if [ ! -f $1 ]; then
        echo "not found: $1"
		echo "$3"
		exit 1
	fi
	cp $1 $2
}  
function prepare_image_for_friendlyelec_eflasher(){
    local OS_DIR=$1
    local ROOTFS=$2
    ###如果pwd/friendlyelec/rk3399/sd-fuse_rk3399/buildroot目录不存在就创建一个
    if [ ! -d ${SDFUSE_DIR}/${OS_DIR} ]; then
        mkdir ${SDFUSE_DIR}/${OS_DIR}
    fi
    ###存在就清空目录下已经存在的文件
    rm -rf ${SDFUSE_DIR}/${OS_DIR}/*

    ###############################
    ###将已经编译出来的img文件重命名并拷贝到${SDFUSE_DIR}/${OS_DIR}####
    ###############################
    copy_and_verify $TOP_DIR/u-boot/rk3399_loader_v1.22.119.bin ${SDFUSE_DIR}/${OS_DIR}/MiniLoaderAll.bin "error: please build uboot first."
    copy_and_verify $TOP_DIR/u-boot/uboot.img ${SDFUSE_DIR}/${OS_DIR} "error: please build uboot first."
    copy_and_verify $TOP_DIR/u-boot/trust.img ${SDFUSE_DIR}/${OS_DIR} "error: please build uboot first."
    copy_and_verify $TOP_DIR/kernel/kernel.img ${SDFUSE_DIR}/${OS_DIR} "error: please build kernel first."
    copy_and_verify $TOP_DIR/kernel/resource.img ${SDFUSE_DIR}/${OS_DIR} "error: please build kernel first."

    ###############################
    ######## ramdisk      ########
    ###SDFUSE_DIR=pwd/friendlyelec/rk3399/sd-fuse_rk3399
    ###############################
    copy_and_verify ${SDFUSE_DIR}/prebuilt/idbloader.img ${SDFUSE_DIR}/${OS_DIR} ""
    copy_and_verify ${SDFUSE_DIR}/prebuilt/boot.img ${SDFUSE_DIR}/${OS_DIR} ""

    ###############################
    ######## custom rootfs ########
    ###############################
    FA_TMP_DIR=$TOP_DIR/friendlyelec/tmp-rootfs
    rm -rf $FA_TMP_DIR
    mkdir -p $FA_TMP_DIR

    if [ -f ${ROOTFS} ]; then
        mkdir -p $FA_TMP_DIR/mnt
        copy_and_verify ${ROOTFS} $FA_TMP_DIR/rootfs-org.img "error: please build rootfs first."
        mount -t ext4 -o loop $FA_TMP_DIR/rootfs-org.img $FA_TMP_DIR/mnt
        mkdir $FA_TMP_DIR/rootfs
        cp -af $FA_TMP_DIR/mnt/* $FA_TMP_DIR/rootfs
        umount $FA_TMP_DIR/mnt
        rm -rf $FA_TMP_DIR/mnt
    elif [ -d ${ROOTFS} ]; then
        mkdir $FA_TMP_DIR/rootfs
        echo "Coping rootfs ..."
        cp -af ${ROOTFS}/* $FA_TMP_DIR/rootfs
        clean_device_files $FA_TMP_DIR/rootfs
    else
        echo "error: please build rootfs first."
        exit 1
    fi

    cat << 'EOL' > $FA_TMP_DIR/rootfs/etc/fstab
    # <file system>                 <mount pt>              <type>          <options>               <dump>  <pass>
    proc                            /proc                   proc            defaults                0       0
    devpts                          /dev/pts                devpts          defaults,gid=5,mode=620 0       0
    tmpfs                           /dev/shm                tmpfs           mode=0777               0       0
    tmpfs                           /tmp                    tmpfs           mode=1777               0       0
    tmpfs                           /run                    tmpfs           mode=0755,nosuid,nodev  0       0
    sysfs                           /sys                    sysfs           defaults                0       0
    debug                           /sys/kernel/debug       debugfs         defaults                0       0
    pstore                          /sys/fs/pstore          pstore          defaults                0       0
EOL

    #kernel modules
    KER_MODULES_OUTDIR=/tmp/output_rk3399_kmodules
    mkdir -p $FA_TMP_DIR/rootfs/lib/modules
    if [ -d $KER_MODULES_OUTDIR/lib/modules ]; then
        cp -af $KER_MODULES_OUTDIR/lib/modules/* $FA_TMP_DIR/rootfs/lib/modules/
    else
        echo "error: please build kernel first."
        exit 1
    fi

    #fs-overlay
    rsync -a --exclude='.git' $TOP_DIR/friendlyelec/rk3399/fs-overlay-64/* $FA_TMP_DIR/rootfs/

    # oem
    [ -d $FA_TMP_DIR/rootfs/oem ] || mkdir $FA_TMP_DIR/rootfs/oem
    cp -af $TOP_DIR/device/rockchip/oem/oem_normal/* $FA_TMP_DIR/rootfs/oem
    # userdata
    [ -d $FA_TMP_DIR/rootfs/userdata ] || mkdir $FA_TMP_DIR/rootfs/userdata
    cp -af $TOP_DIR/device/rockchip/userdata/userdata_normal/* $FA_TMP_DIR/rootfs/userdata

    # misc
    rm -f $FA_TMP_DIR/rootfs/misc
    mkdir $FA_TMP_DIR/rootfs/misc

    # remove /dev/console
    rm -f $FA_TMP_DIR/rootfs/dev/console


    MKFS=${SDFUSE_DIR}/tools/make_ext4fs
    ROOT=${FA_TMP_DIR}/rootfs

    # calc image size
    ROOTFS_SIZE=`du -s -B 1 ${ROOT} | cut -f1`
    MAX_IMG_SIZE=7100000000
    TMPFILE=`tempfile`
    ${MKFS} -s -l ${MAX_IMG_SIZE} -a root -L rootfs /dev/null ${ROOT} > ${TMPFILE}
    IMG_SIZE=`cat ${TMPFILE} | grep "Suggest size:" | cut -f2 -d ':' | awk '{gsub(/^\s+|\s+$/, "");print}'`
    cat $TMPFILE
    rm -f ${TMPFILE}

    if [ ${ROOTFS_SIZE} -gt ${IMG_SIZE} ]; then
            pt_error "IMG_SIZE less than ROOTFS_SIZE, why?"
            exit 1
    fi

    # make rootfs.img
    ${MKFS} -s -l ${IMG_SIZE} -a root -L rootfs $FA_TMP_DIR/rootfs.img ${ROOT}
    copy_and_verify $FA_TMP_DIR/rootfs.img ${SDFUSE_DIR}/${OS_DIR}/rootfs.img " "
    rm -rf $FA_TMP_DIR

    # fill size to parameter.txt and partmap.txt
    ROOTFS_PARTITION_SIZE=`printf "0x%X" $(($IMG_SIZE/512))`
    ROOTFS_PARTITION_ADDR=0x00030000
    USERDATA_PARTITION_ADDR=`printf "0x%X" $((${ROOTFS_PARTITION_ADDR}+${ROOTFS_PARTITION_SIZE}))`

    ###############################
    ######## parameter.txt ########
    ###############################
cat > ${SDFUSE_DIR}/${OS_DIR}/parameter.txt << EOL
FIRMWARE_VER: 6.0.1
MACHINE_MODEL: RK3399
MACHINE_ID: 007
MANUFACTURER: RK3399
MAGIC: 0x5041524B
ATAG: 0x00200800
MACHINE: 3399
CHECK_MASK: 0x80
PWR_HLD: 0,0,A,0,1
#KERNEL_IMG: 0x00280000
#FDT_NAME: rk-kernel.dtb
#RECOVER_KEY: 1,1,0,20,0
#in section; per section 512(0x200) bytes
CMDLINE: root=/dev/mmcblk1p7 rw rootfstype=ext4 data=/dev/mmcblk1p8 consoleblank=0 mtdparts=rk29xxnand:0x00002000@0x00002000(uboot),0x00002000@0x00004000(trust),0x00002000@0x00006000(misc),0x00006000@0x00008000(resource),0x00010000@0x0000e000(kernel),0x00010000@0x0001e000(boot),${ROOTFS_PARTITION_SIZE}@0x00030000(rootfs),-@${USERDATA_PARTITION_ADDR}(userdata)

EOL

    mkdir -p ${SDFUSE_DIR}/${OS_DIR}/sd-boot
    ##########################################################
    ######## param4sd.txt (parameter.txt for sd-boot) ########
    ##########################################################
cat > ${SDFUSE_DIR}/${OS_DIR}/sd-boot/param4sd.txt << EOL
FIRMWARE_VER: 6.0.1
MACHINE_MODEL: RK3399
MACHINE_ID: 007
MANUFACTURER: RK3399
MAGIC: 0x5041524B
ATAG: 0x00200800
MACHINE: 3399
CHECK_MASK: 0x80
PWR_HLD: 0,0,A,0,1
#KERNEL_IMG: 0x00280000
#FDT_NAME: rk-kernel.dtb
#RECOVER_KEY: 1,1,0,20,0
#in section; per section 512(0x200) bytes
CMDLINE: root=/dev/mmcblk0p1 rw rootfstype=ext4 data=/dev/mmcblk0p2 consoleblank=0 mtdparts=rk29xxnand:0x00002000@0x00002000(uboot),0x00002000@0x00004000(trust),0x00002000@0x00006000(misc),0x00006000@0x00008000(resource),0x00010000@0x0000e000(kernel),0x00010000@0x0001e000(boot),${ROOTFS_PARTITION_SIZE}@0x00030000(rootfs),-@${USERDATA_PARTITION_ADDR}(userdata)

EOL

    ROOTFS_PARTITION_SIZE=`printf "0x%X" ${IMG_SIZE}`
    ROOTFS_PARTITION_ADDR=0x6000000
    USERDATA_PARTITION_ADDR=`printf "0x%X" $((${ROOTFS_PARTITION_ADDR}+${ROOTFS_PARTITION_SIZE}))`

    #########################################
    ######## partmap.txt for sd-boot ########
    #########################################
cat > ${SDFUSE_DIR}/${OS_DIR}/sd-boot/partmap.txt << EOL
flash=mmc,1:loader:idb:0x8000,0x280000:idbloader.img;
flash=mmc,1:env:env:0x3F8000,0x8000;
flash=mmc,1:parm:parm:0x400000,0x0400000:param4sd.txt;
flash=mmc,1:uboot:raw:0x800000,0x0400000:uboot.img;
flash=mmc,1:trust:raw:0xC00000,0x0400000:trust.img;
flash=mmc,1:misc:raw:0x1000000,0x0400000;
flash=mmc,1:resc:raw:0x1400000,0x0C00000:resource.img;
flash=mmc,1:kern:raw:0x2000000,0x2000000:kernel.img;
flash=mmc,1:boot:raw:0x4000000,0x2000000:boot.img;
flash=mmc,1:rootfs:ext4:0x6000000,${ROOTFS_PARTITION_SIZE}:rootfs.img;
flash=mmc,1:userdata:ext4:${USERDATA_PARTITION_ADDR},0x0:userdata.img;

EOL

    # gen empty userdata.img
    if [ ! -f ${SDFUSE_DIR}/${OS_DIR}/userdata.img ]; then
        USERDATA_SIZE=104857600
        TMPDIR=`mktemp -d`
        ${MKFS} -s -l ${USERDATA_SIZE} -a root -L userdata ${SDFUSE_DIR}/${OS_DIR}/userdata.img ${TMPDIR}
        rm -rf ${TMPDIR}
    fi
}
1)

在prepare_image_for_friendlyelec_eflasher函数中,kernel编译出来的boot.img不能用,而是选择${SDFUSE_DIR}/prebuilt/boot.img。

2)

创建分区文件的操作是
cat > $ {SDFUSE_DIR}/$ {OS_DIR}/parameter.txt << EOL

EOL
显然可以改变的只有根文件的大小ROOTFS_PARTITION_SIZE和userdata分区起始地址USERDATA_PARTITION_ADDR。
如果是自己编译的根文件系统,需要修改ROOTFS_PARTITION_SIZE的大小,然后对应修改USERDATA_PARTITION_ADDR的值。

二、BUG分析

1、下载rootfs显示失败时,可能是rootfs.img的大小超过分区的大小,修改ROOTFS_PARTITION_SIZE和userdata和USERDATA_PARTITION_ADDR。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!