post-image.sh hacking

weixin_34259232發表於2018-01-23
#*********************************************************************************
#*                      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 $@

 

相關文章