Skip to content

Instantly share code, notes, and snippets.

@qoopooh
Last active November 29, 2016 07:07
Show Gist options
  • Save qoopooh/4fead7a0aec980dbbb6e2a9d409558a1 to your computer and use it in GitHub Desktop.
Save qoopooh/4fead7a0aec980dbbb6e2a9d409558a1 to your computer and use it in GitHub Desktop.
Force format
#!/bin/bash
BOOT_DIR=boot
ROOTFS_DIR=rootfs
EXPECTED_ARGS=0
if [ $# == $EXPECTED_ARGS ]; then
echo "mkmmc-linux Usage:"
echo " mkmmc-linux <device>"
echo " Example: mkmmc-linux /dev/sdb"
exit
fi
check_files()
{
if ! [[ -e $BOOT_DIR/MLO ]]; then
echo "MLO doesn't in $BOOT_DIR"
exit;
fi
if ! [[ -e $BOOT_DIR/u-boot.img ]]; then
echo "u-boot.img doesn't in $BOOT_DIR"
exit;
fi
if ! [[ -e $BOOT_DIR/myir_ricoboard.dtb ]]; then
echo "myir_ricoboard.dtb doesn't in $BOOT_DIR"
exit;
fi
if ! [[ -e $BOOT_DIR/zImage ]]; then
echo "zImage doesn't in $BOOT_DIR"
exit;
fi
if ! [[ -e $BOOT_DIR/uEnv.txt ]]; then
echo "uEnv.txt doesn't in $BOOT_DIR"
fi
if ! [[ -e $ROOTFS_DIR/rootfs.tar.gz ]]; then
echo "rootfs.tar.gz doesn't in $ROOTFS_DIR"
exit;
fi
}
check_files
echo "All data on "$1" now will be destroyed! Continue? [y/n]"
read ans
if ! [ $ans == 'y' ]
then
exit
fi
echo "[Unmounting all existing partitions on the device ]"
umount $1*
echo "[Partitioning $1...]"
DRIVE=$1
dd if=/dev/zero of=$DRIVE bs=1024 count=1024 &>/dev/null
SIZE=`fdisk -l $DRIVE | grep Disk | awk '{print $5}'`
echo DISK SIZE - $SIZE bytes
CYLINDERS=`echo $SIZE/255/63/512 | bc`
#echo CYLINDERS - $CYLINDERS
#{
#echo ,9,0x0C,*
#echo ,,,-
#} | sfdisk -H 255 -S 63 -C $CYLINDERS $DRIVE
parted -s $DRIVE mklabel msdos
parted -s $DRIVE unit cyl mkpart primary fat32 -- 0 9
parted -s $DRIVE set 1 boot on
parted -s $DRIVE unit cyl mkpart primary ext2 -- 9 -2
if [ $? -ne 0 ]; then
echo "error with $1" >&2
exit $?
fi
echo "[Making filesystems...]"
if [[ ${DRIVE} == /dev/*mmcblk* ]]
then
DRIVE=${DRIVE}p
fi
mkfs.vfat -F 32 -n "BOOT" ${DRIVE}1 &> /dev/null
mkfs.ext4 -F -L rootfs ${DRIVE}2 &> /dev/null
echo "[Copying files...]"
mount ${DRIVE}1 /mnt
cp $BOOT_DIR/MLO /mnt/MLO
cp $BOOT_DIR/u-boot.img /mnt/u-boot.img
cp $BOOT_DIR/myir_ricoboard.dtb /mnt/myir_ricoboard.dtb
cp $BOOT_DIR/zImage /mnt/zImage
cp $BOOT_DIR/uEnv.txt /mnt/uEnv.txt
umount ${DRIVE}1
mount ${DRIVE}2 /mnt
tar zxvf $ROOTFS_DIR/rootfs.tar.gz -C /mnt &> /dev/null
chmod 755 /mnt
umount ${DRIVE}2
echo "[Done]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment