制作android SD启动卡shell脚本

本文提供了一个详细的Shell脚本指南,用于制作Android SD启动卡,包括SD卡格式化、分区创建、文件备份及烧录过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

制作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}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值