您的位置:首页 > 运维架构 > Shell

制作android SD启动卡shell脚本

2012-03-01 13:03 302 查看
制作android SD启动卡shell脚本

#! /bin/bash
export LC_ALL=C

if [ $# -ne 1 ]; then
echo "Usage: $0 <drive>"
exit 1;
fi

DRIVE=$1

SKIPMEDIA=0

OUT_READY=0
ROOT_DIR=$(pwd)
PRODUCT='s5pc110'

#偏移
OFFSET_AUTHKEY=1
OFFSET_BL1=9
OFFSET_BL2=57
OFFSET_KERNEL=1081
OFFSET_ROOTFS=9273

#大小
SIZE_AUTHKEY=8
SIZE_UBOOT=1072
SIZE_KERNEL=8192
SIZE_ROOTFS=6142

BACKUP_DIR="backup"
if [ -e "$ROOT_DIR/out/target/product/$PRODUCT" ] ; then
OUT_DIR="$ROOT_DIR/out/target/product/$PRODUCT"
elif [ -e "$PRODUCT" ] ; then
OUT_DIR="$PRODUCT"
else
echo "At least one out dir needed."
OUT_DIR=""
exit 1
fi

#格式化SD卡,格式化为了三个分区
#第一个分区为VFAT格式
#第二个分区和第三个分区为EXT4格式
function format_drive (){
echo "Formatting boot drive"
if [ -b ${DRIVE}2 ]; then
umount ${DRIVE}2
umount ${DRIVE}3
mkfs.ext4 -L "sys" ${DRIVE}2
mkfs.ext4 -L "data" ${DRIVE}3
else
if [ -b ${DRIVE}p2 ]; then
umount ${DRIVE}p2
umount ${DRIVE}p3
mkfs.ext4 -L "sys" ${DRIVE}p2
mkfs.ext4 -L "data" ${DRIVE}p3
else
echo "Can't find boot partition in /dev"
fi
fi
}

function format_vfat () {
echo "Formatting vfat data partition"

if [ -b ${DRIVE}1 ]; then
umount ${DRIVE}1
mkfs.vfat -F 32 -n "fat" ${DRIVE}1
else
if [ -b ${DRIVE}p1 ]; then
umount ${DRIVE}p1
mkfs.vfat -F 32 -n "fat" ${DRIVE}p1
else
echo "Can't find boot partition in /dev"
fi
fi

}

#检查是否预备了相应的镜像文件
function check_files (){
echo "Checking files in $OUT_DIR."

if [ ! -e "$OUT_DIR"/u-boot.bin ] ; then
OUT_DIR=""
echo "No u-boot.bin, checking if file existed."
exit 1
elif [ ! -e "$OUT_DIR"/zImage ] ; then
OUT_DIR=""
echo "No zImage, checking if file existed."
exit 1
elif [ ! -e "$OUT_DIR"/ramdisk-uboot.img ]; then
OUT_DIR=""
echo "No ramdisk-uboot.img, check if file existed."
exit 1
elif [ ! -e "$OUT_DIR"/system/build.prop ]; then
OUT_DIR=""
echo "No system files, check if file existed."
exit 1
fi

}

#备份SD中的数据
function backup_disk (){
echo "Backup disck"
if [ -e ${BACKUP_DIR} ]; then
mv ${BACKUP_DIR} "${BACKUP_DIR}-`date`"
mkdir ${BACKUP_DIR}
else
mkdir ${BACKUP_DIR}
fi

if [ -b ${DRIVE} ]; then
dd if=${DRIVE} of=${BACKUP_DIR}/secure.bin skip=$OFFSET_AUTHKEY count=$SIZE_AUTHKEY bs=512
dd if=${DRIVE} of=${BACKUP_DIR}/u-boot.bin skip=$OFFSET_BL1 count=$SIZE_UBOOT bs=512
dd if=${DRIVE} of=${BACKUP_DIR}/zImage skip=$OFFSET_KERNEL count=$SIZE_KERNEL bs=512
dd if=${DRIVE} of=${BACKUP_DIR}/ramdisk-uboot.img skip=$OFFSET_ROOTFS count=$SIZE_ROOTFS bs=512
else
echo "Can't find boot partition in /dev"
fi

mkdir ${BACKUP_DIR}/tmp
if [ -b ${DRIVE}2 ]; then
umount ${DRIVE}2
mount -t ext4 ${DRIVE}2 ${BACKUP_DIR}/tmp
else
if [ -b ${DRIVE}p2 ]; then
umount ${DRIVE}p2
mount -t ext4 ${DRIVE}p2 ${BACKUP_DIR}/tmp
else
echo "Can't find system partition in /dev"
fi
fi

if [ "$?" != 0 ] ; then
echo "Warning, no system partition found."
fi

cp -rp ${BACKUP_DIR}/tmp ${BACKUP_DIR}/system
umount ${BACKUP_DIR}/tmp
rmdir ${BACKUP_DIR}/tmp
echo "Backup finished."
return 0
}

#烧写SD
function write_disk (){
echo "Writing android images into disck"

check_files

if [ -b ${DRIVE} ]; then
dd of=${DRIVE} if=${OUT_DIR}/secure.bin seek=$OFFSET_AUTHKEY count=$SIZE_AUTHKEY bs=512
dd of=${DRIVE} if=${OUT_DIR}/u-boot.bin seek=$OFFSET_BL1 count=$SIZE_UBOOT bs=512
#	dd of=${DRIVE} if=${OUT_DIR}/u-boot.bin seek=$OFFSET_BL2 bs=512
dd of=${DRIVE} if=${OUT_DIR}/zImage seek=$OFFSET_KERNEL count=$SIZE_KERNEL bs=512
dd of=${DRIVE} if=${OUT_DIR}/ramdisk-uboot.img seek=$OFFSET_ROOTFS count=$SIZE_ROOTFS bs=512
else
echo "Can't write boot sectors into ${DRIVE}"
fi

mkdir ${OUT_DIR}/tmp
if [ -b ${DRIVE}2 ]; then
umount ${DRIVE}2
mount -t ext4 ${DRIVE}2 ${OUT_DIR}/tmp
else
if [ -b ${DRIVE}p2 ]; then
umount ${DRIVE}p2
mount -t ext4 ${DRIVE}p2 ${OUT_DIR}/tmp
else
echo "Can't find system partition in ${DRIVE}"
fi
fi

if [ "$?" -eq 0 ] ; then
echo "No system partition found, quit."
fi
cp -rp ${OUT_DIR}/system/* ${OUT_DIR}/tmp
umount ${OUT_DIR}/tmp
rmdir ${OUT_DIR}/tmp
echo "Writing system files finished."
return $?

}

#创建分区
function create_drives(){
if [ -b ${DRIVE}1 ]; then
umount ${DRIVE}1
umount ${DRIVE}2
umount ${DRIVE}3
else
if [ -b ${DRIVE}p1 ]; then
umount ${DRIVE}p1
umount ${DRIVE}p2
umount ${DRIVE}p3
else
echo "Can't find boot partition in /dev"
fi
fi

dd if=/dev/zero of=$DRIVE bs=1024 count=1024

SIZE=`fdisk -l $DRIVE | grep Disk | grep bytes | awk '{print $5}'`

echo DISK SIZE - $SIZE bytes

CYLINDERS=`echo $SIZE/255/63/512 | bc`
echo CYLINDERS - $CYLINDERS

{
echo 212,,0x0C,-
echo 9,68,0x83,-
echo 77,135,0x83,-
} | sfdisk -D -H 255 -S 63 -C $CYLINDERS $DRIVE

sleep 1
}

#MAIN fucntion
echo "To backup a functional disk, enter 'b'."
echo "To create a new drive, and fill it with android image enter 'c'."
echo "To write boot android image only, enter 'w'."
echo -n "Enter b , c or w:"

read answer

case "$answer" in
b) backup_disk; exit;;
c) check_files; create_drives; format_drive; format_vfat; write_disk; exit ;;
w) check_files; format_drive; backup_disk; write_disk; exit;;
*) echo "Not a valid option. Exiting"; exit ;;
esac

eject ${DRIVE}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息