Skip to content

Instantly share code, notes, and snippets.

@WebReflection
Created April 24, 2013 07:45
Show Gist options
  • Select an option

  • Save WebReflection/5450375 to your computer and use it in GitHub Desktop.

Select an option

Save WebReflection/5450375 to your computer and use it in GitHub Desktop.
Allwinner A10 sd card formatter
#!/bin/bash
# Usage ./a10.sh /dev/sdx hwpack bindfolder
hwpack_update_only=0
BOOT_SIZE=64
TEMP="${TMPDIR:-/tmp}/.a10.$$"
HWPACKDIR="$TEMP"
ROOTFSDIR="$TEMP/rootfs"
MNTBOOT="$TEMP/mnt_boot"
MNTROOT="$TEMP/mnt_root"
MB=1048576
cleanup() {
local x=
# umount card
for x in $MNTBOOT $MNTROOT; do
x=$(readlink -f "$x")
if grep -q " $x " /proc/mounts; then
umount "$x" || exit 1
fi
done
# and delete temporal files
rm -rf --one-file-system "$TEMP"
}
die() {
echo "$*" >&2
cleanup
exit 1
}
title() {
echo
echo "==="
echo "=== $* ==="
echo "==="
}
checkSyntax () {
if [ $# -lt 3 ]; then
echo "Usage: $0 [device] [hwpack] [binfolder]"
echo "automatic dd for [hwpack]/[binfolder]/sunxi-spl.bin and u-boot.bin"
exit 1
fi
[ -b "$1" ] || die "$1: Invalid device"
[ -s "$2" ] || die "$2: Hardware pack not found"
if [ "$3" = "" ]; then
die "binfolder not specified"
fi
}
umountSD () {
local partlist=$(grep "^$1" /proc/mounts | cut -d' ' -f1)
[ -z "$partlist" ] || umount $partlist
}
partitionSD () {
local dev="$1" subdevice=
local x=
case "$dev" in
*/mmcblk*|*/loop*)
subdevice="${1}p"
;;
*)
subdevice="$1"
;;
esac
title "Partitioning $dev"
dd if=/dev/zero of="$dev" bs=$MB count=1 ||
die "$dev: failed to zero the first MB"
x=$(expr $BOOT_SIZE \* 2048)
sfdisk --in-order -L -uS "$dev" <<-EOT
2048,$x,c
,,L
EOT
[ $? -eq 0 ] ||
die "$dev: failed to repartition media"
sleep 1
sfdisk -L -R "$dev" ||
die "$dev: failed to reload media"
title "Format Partition 1 to VFAT"
mkfs.vfat -I ${subdevice}1 ||
die "${subdevice}1: failed to format partition"
title "Format Partition 2 to EXT4"
mkfs.ext4 ${subdevice}2 ||
die "${subdevice}2: failed to format partition"
}
extract() {
local f=$(readlink -f "$1")
title "Extracting $3"
mkdir -p "$2"
cd "$2"
case "$f" in
*.tar.bz2|*.tbz2)
tar xjpf "$f"
;;
*.tar.gz|*.tgz)
tar xzpf "$f"
;;
*.7z|*.lzma)
7z x "$f"
;;
*.tar.xz)
tar xJpf "$f"
;;
*)
die "$f: unknown file extension"
;;
esac
cd - > /dev/null
}
copyUbootSpl ()
{
dd if=$2 bs=1024 of=$1 seek=8
}
copyUboot ()
{
dd if=$2 bs=1024 of=$1 seek=32
}
mountPartitions ()
{
local dev="$1" subdevice=
case "$dev" in
*/mmcblk*|*/loop*)
subdevice="${1}p"
;;
*)
subdevice="$1"
;;
esac
mkdir -p "$MNTROOT" "$MNTBOOT" ||
die "Failed to create SD card mount points"
mount ${subdevice}1 "$MNTBOOT" ||
die "Failed to mount VFAT partition (SD)"
mount ${subdevice}2 "$MNTROOT" ||
die "Failed to mount EXT4 partition (SD)"
}
# "main"
checkSyntax $1 $2 $3
umountSD $1
if [ ${hwpack_update_only} -eq 0 ]; then
partitionSD $1
fi
extract $2 $HWPACKDIR/ "HW Pack"
#if [ ${hwpack_update_only} -eq 0 ]; then
# extract $3 $ROOTFSDIR/ "RootFS"
#fi
title "Copy U-Boot/SPL to SD Card"
# bootloader
copyUbootSpl $1 $HWPACKDIR/$3/sunxi-spl.bin
copyUboot $1 $HWPACKDIR/$3/u-boot.bin
# mountPartitions $1
# copyData
cleanup
echo "Done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment