sdwriter脚本分析

#!/bin/bash
# Mango T-Flash Writer tool
# created by PJ Lee,
# http://www.mangoboard.com
# http://cafe.naver.com/embeddedcrazyboys
LC_ALL="C"
TFLASH=/dev/$1
BOARD=$2
case "$BOARD" in
100)
./mkbl1 mango100_uboot.bin mango100_bl1.bin 8192
;;
210)
./mkbl1 mango210_uboot.bin mango210_bl1.bin 8192
;;
*)
echo "error"
;;
esac
BL1_210=mango210_bl1.bin
UBOOT_210=mango210_uboot.bin
KERNEL_210=mango210_zImage
ANDROID_210=mango210_froyo.tgz
GNOME_210=mango210_gnome.tgz


UBOOT_100=mango100_uboot.bin
KERNEL_100=mango100_zImage
ANDROID_100=mango100_eclair.tgz
GNOME_100=mango100_gnome.tgz
echo "SD Card Writer program V4.0"
echo "Create by Pyeongjeong Lee, Crztech"
echo "http://mangoboard.com"
echo "http://cafe.naver.com/embeddedcrazyboys"


if [ -b "$TFLASH" ] && [ "$BOARD" != "" ]; then


# Check TFlash Sectors 返回SD卡的counts of sectors
TFLASH_SECTORS=`fdisk -l -u $TFLASH | grep sectors | head -n 1 \
| cut -d',' -f4 | cut -d' ' -f3`


OFFSET_BL1_210=1
OFFSET_BL2_210=49
OFFSET_KERNEL_210=1073


OFFSET_BL1_100=$(($TFLASH_SECTORS-18))
OFFSET_BL2_100=$(($TFLASH_SECTORS-1074))
OFFSET_KERNEL_100=$(($TFLASH_SECTORS-9266))


# ANDROID, GNOME Partition Size : 512MB
SIZE_ANDROID=1048576
SIZE_GNOME=1048576
SIZE_FAT=$(($TFLASH_SECTORS-9264-9266-$SIZE_ANDROID-$SIZE_GNOME-2))


OFFSET_FAT=$(($SIZE_FAT-1))
OFFSET_ANDROID=$(($SIZE_ANDROID-1))
OFFSET_GNOME=$(($SIZE_GNOME-1))
#end reserved block 1 (512byte)


# Ask for correct calculation
echo "T-Flash-device:$TFLASH"
echo "NSectors:$TFLASH_SECTORS, Do you want to continue(yes/no): "
read ANS
if [ "$ANS" != "yes" ]; then
    exit -1
fi


print_success()
{
    if [ "$1" == 0 ]; then
echo "success"
    else
echo "failed"
exit -1
    fi
}


partition_add()
{
    echo n
    echo p
    echo $1
    echo $2
    echo +$3
}

#格式化sd卡成固定的格式
sdcard_format() 
{
    START_FAT=9265
    START_ANDROID=$(($START_FAT+$SIZE_FAT))
    START_GNOME=$(($START_ANDROID+$SIZE_ANDROID))


# Pre Umount 
    umount /media/*


    (


# Pre Partition Delete
echo d
echo 6
echo d
echo 5
echo d
echo 4
echo d
echo 3
echo d
echo 2
echo d

# Partition Create  
partition_add 1 $START_FAT $OFFSET_FAT
partition_add 2 $START_ANDROID $OFFSET_ANDROID
partition_add 3 $START_GNOME $OFFSET_GNOME

echo w
echo q
    ) | fdisk -u $TFLASH > /dev/null 2>&1


# Partition Format
    mkfs.vfat -n "mango-disk" "$TFLASH"1 > /dev/null 2>&1
    mkfs.ext3 -L "android" "$TFLASH"2 > /dev/null 2>&1
    mkfs.ext3 -L "linux-gnome" "$TFLASH"3 > /dev/null 2>&1
}


echo
echo -n "Erase Uboot and Kernel Area : "


echo
echo -n "Partition Create : "
sdcard_format
print_success "$?"
case "$BOARD" in
100)

#这部分是关键的写入的命令,主要使用的就是dd
dd bs=512 seek=$OFFSET_KERNEL_100 if=/dev/zero of=$TFLASH count=9226 > /dev/null 2>&1
echo 
echo -n "Write BL1 : "
dd bs=512 seek=$OFFSET_BL1_100 count=16 if=$UBOOT_100 of=$TFLASH > /dev/null 2>&1
print_success "$?"
echo -n "Write Uboot : "
dd bs=512 seek=$OFFSET_BL2_100 if=$UBOOT_100 of=$TFLASH > /dev/null 2>&1
print_success "$?"
echo -n "Write Kernel : "
dd bs=512 seek=$OFFSET_KERNEL_100 if=$KERNEL_100 of=$TFLASH > /dev/null 2>&1
print_success "$?"

echo "Android Filesystem Create"
mkdir temp
mount "$TFLASH"2 temp
cd temp
tar zxvf ../$ANDROID_100 >/dev/null 2>&1
print_success "$?"
cd ..
umount temp
echo "Gnome Filesystem Create"
mount "$TFLASH"3 temp
cd temp
tar zxvf ../$GNOME_100 >/dev/null 2>&1
print_success "$?"
cd ..
umount temp
rm -rf  temp
;;
210)
dd bs=512 seek=$OFFSET_BL1_210 if=/dev/zero of=$TFLASH count=9264 > /dev/null 2>&1
echo
echo -n "Write BL1 : "
dd bs=512 seek=$OFFSET_BL1_210 if=$BL1_210 of=$TFLASH > /dev/null 2>&1
print_success "$?"
echo -n "Write Uboot : "
dd bs=512 seek=$OFFSET_BL2_210 if=$UBOOT_210 of=$TFLASH > /dev/null 2>&1
print_success "$?"
echo -n "Write Kernel : "
dd bs=512 seek=$OFFSET_KERNEL_210 if=$KERNEL_210 of=$TFLASH > /dev/null 2>&1
print_success "$?"
echo "Android Filesystem Create"
mkdir temp
mount "$TFLASH"2 temp

#文件系统也是采用解压的方式,这个原理有点像OK6410android烧写方式,不过OK6410是解压到nandflash,以前linux2.6.36烧写yaffs也是通过这种方式

cd temp 

tar zxvf ../$ANDROID_210 > /dev/null 2>&1
print_success "$?"
cd ..
umount temp
echo "Gnome Filesystem Create"
mount "$TFLASH"3 temp
cd temp
tar zxvf ../$GNOME_210 > /dev/null 2>&1
print_success "$?"
cd ..
umount temp
rm -rf  temp
;;
*)
echo "Usage: $0 device board name"
echo "ex) $0 sdb 210 or $0 sdb 100"
exit 1
;;
esac


else
    echo "Usage: $0 device board_name"
    echo "ex) $0 sdb 210 or $0 sdb 100"
    exit 1

fi

下图就是2G 4G 8G卡的烧写完成后的分区表

sdwriter脚本分析_第1张图片





你可能感兴趣的:(android,linux,脚本,byte)