Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 263 lines (222 sloc) 6.79 KB
#!/bin/sh -e
DEVICE="$1"
ROOTFS="$2"
IMGSIZE=1536
LOOPDEV="/dev/loop1"
DEFAULT_TARGET="(undefined)"
# define target machine
TARGET=$DEFAULT_TARGET
[ -n "$3" ] && TARGET="$3"
# check target machine
fs_types=""
boot_part_size="60M"
boot_part_align=8192
case $TARGET in
#don't remove the following comment
#begin_targets
cubieboard) fs_types="fat32 ext4" ;;
cubieboard2) fs_types="fat32 ext4" ;;
cubietruck) fs_types="fat32 ext4" ;;
cubox) fs_types="ext4" ;;
cuboxi) fs_types="fat32 ext4" ;;
utilite) fs_types="fat32 ext4" ;;
utilite-locked) fs_types="fat32 ext4" ;;
raspberrypi) fs_types="fat32 ext4" ;;
raspberrypi2) fs_types="fat32 ext4" ;;
igepv2) fs_types="fat32 ext2" ; boot_part_size="16M" ;;
pandaboard) fs_types="fat32 ext4" ;;
snowball-v5) fs_types="fat32 ext4" ;;
efikamx) fs_types="undefined" ;;
ac100) fs_types="undefined" ;;
harmony) fs_types="undefined" ;;
sabrelite) fs_types="undefined" ;;
tbs2910) fs_types="fat32 ext4" ;;
#end_targets
#don't remove the preceeding comment
esac
# workaound for different 'echo' implmentations
put_str() {
printf "$*\n"
}
# check for rootfs url
ROOTFS_LINK="rootfs.url"
if [ -n "$ROOTFS" ] && echo "$ROOTFS" | grep -q "^https\?://.*\.tar\.xz$"; then
if curl -Is $ROOTFS | grep -q "^HTTP.*200 OK"; then
echo "$ROOTFS" > $ROOTFS_LINK
ROOTFS=$ROOTFS_LINK
else
rm -f $ROOTFS_LINK
fi
fi
if [ ! -f "$ROOTFS" ] && [ ! -d "$ROOTFS" ] || [ -z "$fs_types" ] ; then
put_str "Usage: $0 <device|img-file> <rootfs|rootfs-archive|rootfs-url> [target-machine]\n"
put_str "Supported target-machine types are:"
curr=$(cat $0)
curr=${curr#*#begin_targets}
curr=${curr%%#end_targets*}
while [ "$prev" != "$curr" ] ; do
put_str " "${curr%%)*}
prev=$curr
curr=${curr#*;;}
done
put_str "Default target-machine type is:"
put_str " $DEFAULT_TARGET\n"
exit 1
fi
if [ "$(id -u)" -ne 0 ] ; then
put_str "Notice: Please run this program as root."
exit 1
fi
if [ ! -b "$DEVICE" ] ; then
if echo $DEVICE | grep -q "/dev/" ; then
put_str "Unknown block device : $DEVICE"
exit 1
else
IMG="$DEVICE"
DEVICE=$LOOPDEV
put_str "Using : $DEVICE for img file $IMG"
fi
fi
if [ -z "$IMG" ] ; then
put_str "I'm about to overwrite \033[1m${DEVICE}\033[0m . All existing data will be lost !"
until [ "$REPLY" = y ] || [ "$REPLY" = yes ] || [ "$REPLY" = n ] || [ "$REPLY" = no ] ; do
read -p "Do you want to continue (y/n) ? " REPLY
done
[ "$REPLY" = n ] || [ "$REPLY" = no ] && exit
else
put_str "I'm about to create \033[1m${IMG}\033[0m . All existing data will be lost !"
fi
# message output / debugging helper
put_msg() {
#read -p "$* [Enter]"
put_str "\n$*"
}
# prepare "mkfs" command
create_mkfs_command () {
local mkfs_cmd=""
local mkfs_fat16="mkfs.vfat -F 16 -n $2 $3"
local mkfs_fat32="mkfs.vfat -F 32 -n $2 $3"
local mkfs_ext2="mkfs.ext2 -L $2 $3"
local mkfs_ext3="mkfs.ext3 -L $2 $3"
local mkfs_ext4="mkfs.ext4 -L $2 $3"
local mkfs_xfs="mkfs.xfs -L $2 $3"
eval mkfs_cmd=\$mkfs_$1
echo "$mkfs_cmd"
}
# create empty image file and attach to loop device
if [ -n "$IMG" ] ; then
put_msg "#### Creating image file"
dd if=/dev/zero of=$IMG bs=1M count=$IMGSIZE
losetup -d $DEVICE 2>/dev/null || true
losetup $DEVICE $IMG
fi
# count partitions and check their types
part_count=0
boot_part_type="83"
for fs_type in $fs_types ; do
mkfs_cmd=$(create_mkfs_command $fs_type "noname" "-")
if [ -z "$mkfs_cmd" ] ; then
put_str "Fatal: unknown filesystem type: '$fs_type'"
exit 1
fi
if [ $part_count -eq 0 ] ; then
case $fs_type in
fat16) boot_part_type="6" ;;
fat32) boot_part_type="c" ;;
esac
fi
part_count=$(expr $part_count + 1)
done
# define partitions and prepare "fdisk" command
echo $DEVICE | grep -q mmc && OPT=p
echo $DEVICE | grep -q loop && OPT=p
partition_0=${DEVICE}${OPT}1 ; label_0="BOOT"
partition_1=${DEVICE}${OPT}2 ; label_1="GEEXBOX"
if [ $part_count -lt 2 ] ; then
fdisk_input="o\nn\np\n1\n${boot_part_align}\n\nt\n${boot_part_type}\na\n1\nw\n"
partition_1=$partition_0
label_0=$label_1
else
fdisk_input="o\nn\np\n1\n${boot_part_align}\n+${boot_part_size}\nt\n${boot_part_type}\nn\np\n2\n${boot_part_align}\n\n\na\n1\nw\n"
fi
fdisk_cmd="/bin/echo -e \"$fdisk_input\" | fdisk $DEVICE"
put_msg "#### Creating partion(s)..."
if [ -z "$IMG" ] ; then
grep -q $DEVICE /proc/mounts && grep $DEVICE /proc/mounts | cut -f2 -d\ | xargs umount
dd if=/dev/zero of=$DEVICE bs=512 count=$boot_part_align > /dev/null
sh -c "$fdisk_cmd" > /dev/null
else
sh -c "$fdisk_cmd" > /dev/null 2>&1 || true
partprobe $DEVICE
fi
fdisk -l $DEVICE
put_msg "#### Creating file system(s)..."
part_count=0
for fs_type in $fs_types ; do
eval label=\$label_${part_count}
eval partition=\$partition_${part_count}
mkfs_cmd=$(create_mkfs_command $fs_type $label $partition)
sh -c "$mkfs_cmd" > /dev/null
part_count=$(expr $part_count + 1)
done
put_msg "#### Mounting..."
mountdir=$(mktemp -d)
mount $partition_1 $mountdir
if [ "$partition_1" != "$partition_0" ] ; then
mkdir -p $mountdir/boot
mount $partition_0 $mountdir/boot
fi
put_msg "#### Copying files..."
if [ -d "$ROOTFS" ]; then
cp -PR $ROOTFS/* $mountdir
elif [ "$ROOTFS" = "$ROOTFS_LINK" ]; then
curl -L -k $(cat $ROOTFS) --progress | tar -xJ --no-same-owner -C $mountdir
rm -f $ROOTFS
else
tar --no-same-owner -xaf $ROOTFS -C $mountdir
fi
# trigger auto-resize feature
if [ -n "$IMG" ] ; then
touch $mountdir/etc/need-to-resize
fi
if [ $TARGET = cubieboard ] || [ $TARGET = cubieboard2 ] || [ $TARGET = cubietruck ] ; then
put_msg "#### Writing SPL and u-boot ..."
dd if=$mountdir/boot/u-boot-sunxi-with-spl.bin of=$DEVICE bs=1K seek=8
fi
if [ $TARGET = cuboxi ] ; then
if [ -e $mountdir/boot/u-boot-with-spl.bin ]; then
put_msg "#### Writing SPL and u-boot ..."
dd if=$mountdir/boot/u-boot-with-spl.bin of=$DEVICE bs=1K seek=1
else
put_msg "#### Writing SPL ..."
dd if=$mountdir/boot/SPL of=$DEVICE bs=1K seek=1
put_msg "#### Writing u-boot ..."
dd if=$mountdir/boot/u-boot.img of=$DEVICE bs=1K seek=42
fi
fi
if [ $TARGET = tbs2910 ] ; then
put_msg "#### Writing u-boot ..."
dd if=$mountdir/boot/u-boot.bin of=$DEVICE bs=1K seek=1
fi
if [ $TARGET = utilite ] ; then
put_msg "#### Writing SPL and u-boot ..."
dd if=$mountdir/boot/cm-fx6-firmware of=/$DEVICE bs=1K skip=1 seek=1
fi
put_msg "#### Flushing buffers..."
sync
put_msg "#### Unmounting..."
if [ "$partition_1" != "$partition_0" ] ; then
umount $mountdir/boot
rmdir $mountdir/boot
fi
umount $mountdir
rmdir $mountdir
sync
if [ -n "$IMG" ] ; then
losetup -d $DEVICE || true
put_msg "#### Compressing img file to $IMG.xz ...."
xz -z -9 $IMG || true
chmod 666 $IMG.xz || true
rm -f $IMG
fi
put_msg "#### Done.\n"