2009年10月6日 星期二

A tool for making a bootable SD card of Samsung ARM platform

#!/bin/sh

#for 4G SD card:
#sudo mkfs /dev/sdb
#sudo mkfs.vfat -I /dev/sdb
#sudo dd if=./u-boot.bin of=/dev/sdb bs=512 count=16 seek=7897070
#sudo dd if=./u-boot.bin of=/dev/sdb bs=512 count=1024 seek=7896014
#sudo dd if=./zImage of=/dev/sdb bs=512 count=8192 seek=7887822
#sudo dd if=./rootfs.cramfs of=/dev/sdb bs=512 count=12288 seek=7871438


#ofsinfo.last = last - (eFUSE_SIZE / MOVI_BLKSIZE);
#ofsinfo.bl1 = ofsinfo.last - MOVI_BL1_BLKCNT;
#ofsinfo.env = ofsinfo.bl1 - MOVI_ENV_BLKCNT;
#ofsinfo.bl2 = ofsinfo.bl1 - (MOVI_BL2_BLKCNT + MOVI_ENV_BLKCNT);
#ofsinfo.kernel = ofsinfo.bl2 - MOVI_ZIMAGE_BLKCNT;
#ofsinfo.rootfs = ofsinfo.kernel - MOVI_ROOTFS_BLKCNT;

#./create_bootable_sd.sh sdb format
#./create_bootable_sd.sh sdb erase all
#./create_bootable_sd.sh sdb write bootloader1 ./my100/u-boot.bin
#./create_bootable_sd.sh sdb write bootloader2 ./my100/u-boot.bin
#./create_bootable_sd.sh sdb write kernel ./my100/zImage
# ./create_bootable_sd.sh sdb write rootfs ./my100/rootfs.cramfs

DEVICE_NAME=$1

if [ -z $DEVICE_NAME ];then
echo "./create_bootable_sd.sh [device] format"
echo " ./create_bootable_sd.sh [device] erase [bootloader1|bootloader2|kernel|rootfs|all]"
echo " ./create_bootable_sd.sh [device] write [bootloader1|bootloader2|kernel|rootfs|all] [file]"
exit
fi

if [ -e /dev/$DEVICE_NAME ];then

BLOCK_COUNTS_1024=`cat /proc/partitions | grep $DEVICE_NAME | cut -d' ' -f12`
SIZE=$((BLOCK_COUNTS_1024*1024))
BLOCK_COUNTS_512=$((SIZE/512))

#echo "BLOCK_COUNTS_1024= "$BLOCK_COUNTS_1024
#echo "size= "$SIZE
#echo "BLOCK_COUNTS_512= "$BLOCK_COUNTS_512


PART_SIZE_BL=$((512 * 1024))
PART_SIZE_KERNEL=$((4 * 1024 * 1024))
PART_SIZE_ROOTFS=$((8 * 1024 * 1024))
#echo "PART_SIZE_ROOTFS= "$PART_SIZE_ROOTFS
MOVI_TOTAL_BLKCNT=$BLOCK_COUNTS_512
CFG_ENV_SIZE=16384 #0x4000
SS_SIZE=$((8 * 1024))
eFUSE_SIZE=$((1 * 1024)) # 0.5k eFuse, 0.5k reserved
MOVI_BLKSIZE=$((1<<9)) #512 bytes
#echo "MOVI_TOTAL_BLKCNT= "$MOVI_TOTAL_BLKCNT
#echo "SS_SIZE= "$SS_SIZE
#echo "eFUSE_SIZE= "$eFUSE_SIZE
#echo "MOVI_BLKSIZE= "$MOVI_BLKSIZE


MOVI_LAST_BLKPOS=$((MOVI_TOTAL_BLKCNT - (eFUSE_SIZE / MOVI_BLKSIZE)))
#echo "MOVI_LAST_BLKPOS= "$MOVI_LAST_BLKPOS

MOVI_BL1_BLKCNT=$((SS_SIZE / MOVI_BLKSIZE))
MOVI_ENV_BLKCNT=$((CFG_ENV_SIZE / MOVI_BLKSIZE))
MOVI_BL2_BLKCNT=$((PART_SIZE_BL / MOVI_BLKSIZE))
MOVI_ZIMAGE_BLKCNT=$((PART_SIZE_KERNEL / MOVI_BLKSIZE))
MOVI_BL2_POS=$((MOVI_LAST_BLKPOS - MOVI_BL1_BLKCNT - MOVI_BL2_BLKCNT - MOVI_ENV_BLKCNT))
MOVI_ROOTFS_BLKCNT=$((PART_SIZE_ROOTFS / MOVI_BLKSIZE))

#echo "MOVI_BL1_BLKCNT= "$MOVI_BL1_BLKCNT
#echo "MOVI_ENV_BLKCNT= "$MOVI_ENV_BLKCNT
#echo "MOVI_BL2_BLKCNT= "$MOVI_BL2_BLKCNT
#echo "MOVI_ZIMAGE_BLKCNT= "$MOVI_ZIMAGE_BLKCNT
#echo "MOVI_ROOTFS_BLKCNT= "$MOVI_ROOTFS_BLKCNT

MOVI_BL1_OFFSET=$((MOVI_LAST_BLKPOS- MOVI_BL1_BLKCNT))
MOVI_ENV_OFFSET=$((MOVI_BL1_OFFSET-MOVI_ENV_BLKCNT))
MOVI_BL2_OFFSET=$((MOVI_ENV_OFFSET- MOVI_BL2_BLKCNT))
MOVI_ZIMAGE_OFFSET=$((MOVI_BL2_OFFSET-MOVI_ZIMAGE_BLKCNT))
MOVI_ROOTFS_OFFSET=$((MOVI_ZIMAGE_OFFSET-MOVI_ROOTFS_BLKCNT))

#echo "MOVI_BL1_OFFSET= "$MOVI_BL1_OFFSET
#echo "MOVI_ENV_OFFSET= "$MOVI_ENV_OFFSET
#echo "MOVI_BL2_OFFSET= "$MOVI_BL2_OFFSET
#echo "MOVI_ZIMAGE_OFFSET= "$MOVI_ZIMAGE_OFFSET
#echo "MOVI_ROOTFS_OFFSET= "$MOVI_ROOTFS_OFFSET

COMMAND=$2
FILE=$4
case "$3" in
*bootloader1*)
PART="BL1"
OFFSET=$MOVI_BL1_OFFSET
COUNT=$MOVI_BL1_BLKCNT
;;
*bootloader2*)
PART="BL2"
OFFSET=$MOVI_BL2_OFFSET
COUNT=$MOVI_BL2_BLKCNT
;;
*kernel*)
PART="ZIMAGE"
OFFSET=$MOVI_ZIMAGE_OFFSET
COUNT=$MOVI_ZIMAGE_BLKCNT
;;
*rootfs*)
PART="ROOTFS"
OFFSET=$MOVI_ROOTFS_OFFSET
COUNT=$MOVI_ROOTFS_BLKCNT
;;
*all*)
PART="ALL"
OFFSET=$MOVI_ROOTFS_OFFSET
COUNT=$((MOVI_ROOTFS_BLKCNT+MOVI_ZIMAGE_BLKCNT+MOVI_BL2_BLKCNT+MOVI_ENV_BLKCNT+MOVI_BL1_BLKCNT))
;;
esac

case "$COMMAND" in
*format*)
echo "Format /dev/$DEVICE_NAME...."
echo "y" > ./format.dat
sudo /sbin/mkfs /dev/$DEVICE_NAME < ./format.dat > /dev/null 2>&1
sudo /sbin/mkfs.vfat -I /dev/$DEVICE_NAME > /dev/null 2>&1
rm -f ./format.dat
;;
*erase*)
echo "PART= "$PART
if [ -z $PART ];then
echo "Not set PART..."
break
fi
echo "erase "$PART" OFFSET="$OFFSET", COUNT="$COUNT
sudo /bin/dd if=/dev/zero of=/dev/$DEVICE_NAME bs=$MOVI_BLKSIZE count=$COUNT seek=$OFFSET

;;
*write*)

if [ -f $FILE ];then
echo "werite "$FILE" size " $COUNT " blocks into "$PART" offset at "$OFFSET
FILE_SIZE=`ls -al $FILE | cut -d' ' -f5`
WRITE_SIZE=$((COUNT*MOVI_BLKSIZE))
echo "$FILE FILE_SIZE="$FILE_SIZE" WRITE_SIZE="$WRITE_SIZE
if [ $PART != "BL1" ];then

if [ $((FILE_SIZE)) -gt $((WRITE_SIZE)) ];then
echo "Because $FILE_SIZE > $WRITE_SIZE, do not do writing...."
exit
fi
fi
sudo /bin/dd if=$FILE of=/dev/$DEVICE_NAME bs=$MOVI_BLKSIZE count=$COUNT seek=$OFFSET
else
echo $FILE" not be found..."
break
fi

;;
*help*)
echo "./create_bootable_sd.sh [device_name] format"
echo " ./create_bootable_sd.sh [device_name] erase [bootloader1|bootloader2|kernel|rootfs|all]"
echo " ./create_bootable_sd.sh [device_name] write [bootloader1|bootloader2|kernel|rootfs|all] [file]"
;;
*)

echo "Do nothing..."
;;
esac

else

echo "/dev/$DEVICE_NAME does not exist... "
fi

sync;sync;sync

echo "MOVI_BL2_POS= "$MOVI_BL2_POS" MOVI_BL2_OFFSET= "$MOVI_BL2_OFFSET

echo "Exit..."

沒有留言: