#!/bin/sh
# a quick hack to burn flashroms
# returns 42 if flashed image does not match!!  check for this!

DIR=`dirname $0`
FLASHTOOL="flashtool -v"

if [ $# -ne 1 ]; then
	echo "usage: $0 <newimage.rom>"
	exit 1
fi

if [ ! -f $1 ]; then
	echo "ERROR: '$1' doesn't exist"
	exit 1
fi

touch /dev/cobalt 2>/dev/null
if [ $? -ne 0 ]; then
       	echo "ERROR: you do not have sufficient permission"
	exit 1
fi
rm /dev/cobalt

echo -ne "Backing up old ROM to 'rom-orig-`cmos -c romrev`'... "
$FLASHTOOL -r > $DIR/rom-orig-`cmos -c romrev`.img || exit 1
echo "done"

# if it fails, we should have NOT flushed the buffers to ROM
echo -ne "Writing new ROM... "
$FLASHTOOL -w $1 || exit 1
echo "done"

echo -ne "Verifying... "
$FLASHTOOL -r > $DIR/rom.new || exit 1
cmp $DIR/rom.new $1
if [ $? -ne 0 ]; then
	echo "ERROR: newly burned image does not match image on disk"
	exit 42
fi
rm $DIR/rom.new

echo "ROM update complete.  You should now reboot the system"
