#!/bin/bash
/**
*"Exit immediately if a simple command exits with a non-zero status."
* 这行代码之后的任何代码,如果返回一个非0的值,那么整个脚本立即退出
* 官方的说明是为了防止错误出现产生滚雪球的现象
*/
set -e
/* 把envsetup.sh输出重定向到/dev/null,也就是不在stdout上显示信息 */
. build/envsetup.sh >/dev/null && setpaths
export PATH=$ANDROID_BUILD_PATHS:$PATH
/**
* TARGET_PRODUCT :ProductName
* TARGET_HARDWARE :rk30board
*/
TARGET_PRODUCT=`get_build_var TARGET_PRODUCT`
TARGET_HARDWARE=`get_build_var TARGET_BOARD_HARDWARE`
echo TARGET_PRODUCT=$TARGET_PRODUCT
echo TARGET_HARDWARE=$TARGET_HARDWARE
TARGET="withoutkernel"
/**
* 如果参数不为空,如:./mk_script aaa
* 则TARGET = 指定的第一个参数值(aaa)
*/
if [ "$1"x != ""x ]; then
TARGET=$1
fi
/* IMAGE_PATH = rockdev/Image-ProductName */
IMAGE_PATH=rockdev/Image-$TARGET_PRODUCT
/**
* 每次运行都先删除原先的旧文件,再新建立文件夹
*/
rm -rf $IMAGE_PATH
mkdir -p $IMAGE_PATH
/**
* grep "text_to_find" text.txt: 列出在text.txt文件中查找"text_to_find"所在的行
* head -n 1: 列出文本中的第一行
* awk '{ print $2 }': 取数据的第2列
*
* 含'mtd@system'的一行:mount ext4 mtd@system /system wait ro noatime nodiratime noauto_da_alloc
* 最终:FSTYPE = ext4
*/
FSTYPE=`grep 'mtd@system' $OUT/root/init.$TARGET_HARDWARE.rc | head -n 1 | awk '{ print $2 }'`
if [ "$FSTYPE" = "" ]; then
FSTYPE=`grep 'mtd@system' $OUT/root/init.rc | head -n 1 | awk '{ print $2 }'`
fi
echo system filesysystem is $FSTYPE
BOARD_CONFIG=device/rockchip/rksdk/device.mk
KERNEL_SRC_PATH=`grep TARGET_PREBUILT_KERNEL ${BOARD_CONFIG} |grep "^\s*TARGET_PREBUILT_KERNEL *:= *[\w]*\s" |awk '{print $3}'`
/**
* id -u: user id
* 如果是root用户,id返回0
*/
[ $(id -u) -eq 0 ] || FAKEROOT=fakeroot
BOOT_OTA="ota"
/**
* test -a: and与操作
* -f: 判断文件是否存在
* -d: 文件存在且是目录
*/
[ $TARGET != $BOOT_OTA -a $TARGET != "withoutkernel" ] && echo "unknow target[${TARGET}],exit!" && exit 0
if [ ! -f $OUT/kernel ]
/**
* 检查$OUT/目录下是否存在kernel文件,不存在将从指定的地方拷贝
* 如果指定kernel文件的目录异常,退出
*/
then
echo "kernel image not fount![$OUT/kernel] "
read -p "copy kernel from TARGET_PREBUILT_KERNEL[$KERNEL_SRC_PATH] (y/n) n to exit?"
if [ "$REPLY" == "y" ]
then
[ -f $KERNEL_SRC_PATH ] || \
echo -n "fatal! TARGET_PREBUILT_KERNEL not eixit! " || \
echo -n "check you configuration in [${BOARD_CONFIG}] " || exit 0
cp ${KERNEL_SRC_PATH} $OUT/kernel
else
exit 0
fi
fi
if [ $TARGET == $BOOT_OTA ]
/**
* 检查执行该脚本是带了参数,且参数是$BOOT_OTA,执行then
*/
then
echo "make ota images... "
echo -n "create boot.img with kernel... "
[ -d $OUT/root ] && \
mkbootfs $OUT/root | minigzip > $OUT/ramdisk.img && \
mkbootimg --kernel $OUT/kernel --ramdisk $OUT/ramdisk.img --second kernel/resource.img --output $OUT/boot.img && \
cp -a $OUT/boot.img $IMAGE_PATH/
echo "done."
else
echo -n "create boot.img without kernel... "
[ -d $OUT/root ] && \
mkbootfs $OUT/root | minigzip > $OUT/ramdisk.img && \
rkst/mkkrnlimg $OUT/ramdisk.img $IMAGE_PATH/boot.img >/dev/null
echo "done."
fi
if [ $TARGET == $BOOT_OTA ]
then
echo -n "create recovery.img with kernel... "
[ -d $OUT/recovery/root ] && \
mkbootfs $OUT/recovery/root | minigzip > $OUT/ramdisk-recovery.img && \
mkbootimg --kernel $OUT/kernel --ramdisk $OUT/ramdisk-recovery.img --second kernel/resource.img --output $OUT/recovery.img && \
cp -a $OUT/recovery.img $IMAGE_PATH/
echo "done."
else
/**
* mkbootfs: .system/core/cpio/mkbootfs.c
* minigzip: .external/zlib/src/test/minigzip.c
* mkbootimg: .system/core/mkbootimg/mkbootimg.c
* mkyaffs2image: .external/yaffs2/yaffs2/utils/mkyaffs2image.c
*/
echo -n "create recovery.img with kernel and with out resource... "
[ -d $OUT/recovery/root ] && \
mkbootfs $OUT/recovery/root | minigzip > $OUT/ramdisk-recovery.img && \
mkbootimg --kernel $OUT/kernel --ramdisk $OUT/ramdisk-recovery.img --output $OUT/recovery.img && \
cp -a $OUT/recovery.img $IMAGE_PATH/
echo "done."
fi
echo -n "create misc.img.... "
cp -a rkst/Image/misc.img $IMAGE_PATH/misc.img
cp -a rkst/Image/pcba_small_misc.img $IMAGE_PATH/pcba_small_misc.img
cp -a rkst/Image/pcba_whole_misc.img $IMAGE_PATH/pcba_whole_misc.img
echo "done."
if [ -d $OUT/system ]
then
echo -n "create system.img... "
if [ "$FSTYPE" = "cramfs" ]
then
chmod -R 777 $OUT/system
$FAKEROOT mkfs.cramfs $OUT/system $IMAGE_PATH/system.img
elif [ "$FSTYPE" = "squashfs" ]
then
chmod -R 777 $OUT/system
mksquashfs $OUT/system $IMAGE_PATH/system.img -all-root >/dev/null
elif [ "$FSTYPE" = "ext3" ] || [ "$FSTYPE" = "ext4" ]
then
/* 获取system.img的大小 */
system_size=`ls -l $OUT/system.img | awk '{print $5;}'`
[ $system_size -gt "0" ] || { echo "Please make first!!!" && exit 1; }
MAKE_EXT4FS_ARGS=" -L system -S $OUT/root/file_contexts -a system $IMAGE_PATH/system.img $OUT/system"
ok=0
while [ "$ok" = "0" ]; do
make_ext4fs -l $system_size $MAKE_EXT4FS_ARGS >/dev/null 2>&1 &&
tune2fs -c -1 -i 0 $IMAGE_PATH/system.img >/dev/null 2>&1 &&
ok=1 || system_size=$(($system_size + 5242880))
done
e2fsck -fyD $IMAGE_PATH/system.img >/dev/null 2>&1 || true
else
mkdir -p $IMAGE_PATH/2k $IMAGE_PATH/4k
mkyaffs2image -c 2032 -s 16 -f $OUT/system $IMAGE_PATH/2k/system.img
mkyaffs2image -c 4080 -s 16 -f $OUT/system $IMAGE_PATH/4k/system.img
fi
echo "done."
fi
chmod a+r -R $IMAGE_PATH/
No argument on sucess:
// on sucess
TARGET_PRODUCT=ProductName
TARGET_HARDWARE=rk30board
system filesysystem is ext4
create boot.img without kernel... done.
create recovery.img with kernel and with out resource... done.
create misc.img.... done.
create system.img... done.
No argument on fail:
TARGET_PRODUCT=ProductName
TARGET_HARDWARE=rk30board
system filesysystem is ext4
create boot.img without kernel... ./mkimage.sh: line 66: mkbootfs: command not found
./mkimage.sh: line 66: minigzip: command not found
done.
create recovery.img with kernel and with out resource... done.
create misc.img.... done.
create system.img... ls: cannot access /home/hao/workspace-ftest/out/target/product/KoolRegister/system.img: No such file or directory
./mkimage.sh: line 106: [: -gt: unary operator expected
Please make first!!!