post-image.sh hacking

#*********************************************************************************
#*                      post-image.sh hacking
#* 说明:
#*     分析i.MX6 post-images.sh合成SD card工作原理。
#*
#*                                              2018-1-23 深圳 宝安西乡 曾剑锋
#********************************************************************************/

# 一、参考文档:
#    1. Linux mktemp命令
#        http://www.runoob.com/linux/linux-comm-mktemp.html
#    2. Genimage - The Image Creation Tool
#        https://github.com/pengutronix/genimage

#!/usr/bin/env bash

#
# dtb_list extracts the list of DTB files from BR2_LINUX_KERNEL_INTREE_DTS_NAME
# in ${BR_CONFIG}, then prints the corresponding list of file names for the
# genimage configuration file
#
dtb_list()
{
    local DTB_LIST="$(sed -n 's/^BR2_LINUX_KERNEL_INTREE_DTS_NAME="\([a-z0-9 \-]*\)"$/\1/p' ${BR2_CONFIG})"

    for dt in $DTB_LIST; do
        echo -n "\"$dt.dtb\", "
    done
}

#
# linux_image extracts the Linux image format from BR2_LINUX_KERNEL_UIMAGE in
# ${BR_CONFIG}, then prints the corresponding file name for the genimage
# configuration file
#
linux_image()
{
    if grep -Eq "^BR2_LINUX_KERNEL_UIMAGE=y$" ${BR2_CONFIG}; then
        echo "\"uImage\""
    else
        echo "\"zImage\""
    fi
}

main()
{
    # 获取dtb和linux image
    local FILES="$(dtb_list) $(linux_image)"
    # 创建配置文件文件
    local GENIMAGE_CFG="$(mktemp --suffix genimage.cfg)"
    local GENIMAGE_TMP="${BUILD_DIR}/genimage.tmp"

    # 替换掉cfg模板文件中的FILES字段
    sed -e "s/%FILES%/${FILES}/" \
        board/freescale/common/imx/genimage.cfg.template > ${GENIMAGE_CFG}

    # 可能存在上次的暂存目录,删除
    rm -rf "${GENIMAGE_TMP}"

    #
    # outputpath: default: images Mandatory path where all images are written to (must exist).
    # inputpath:  default: input This mandatory path is searched for input images, for example bootloader binaries, kernel images (must exist).
    # rootpath:   default: root Mandatory path to the root filesystem (must exist).
    # tmppath:    default: tmp Optional path to a temporary directory. There must be enough space available here to hold a copy of the root filesystem.
    # config:     default: genimage.cfg Path to the genimage config file.
    #
    # ${TARGET_DIR} = /home/zengjf/zengjf/Buildroot/buildroot/output/target
    # ${GENIMAGE_TMP} = /home/zengjf/zengjf/Buildroot/buildroot/output/build/genimage.tmp
    # ${BINARIES_DIR} = /home/zengjf/zengjf/Buildroot/buildroot/output/images
    # ${BINARIES_DIR} = /home/zengjf/zengjf/Buildroot/buildroot/output/images
    # ${GENIMAGE_CFG} = /tmp/tmp.1Kks4kDC5mgenimage.cfg
    #
    genimage \
        --rootpath "${TARGET_DIR}" \
        --tmppath "${GENIMAGE_TMP}" \
        --inputpath "${BINARIES_DIR}" \
        --outputpath "${BINARIES_DIR}" \
        --config "${GENIMAGE_CFG}"

    // 删除配置文件
    rm -f ${GENIMAGE_CFG}

    exit $?
}

main $@

 

posted on 2018-01-23 11:33  zengjf  阅读(1319)  评论(0编辑  收藏  举报

导航