if itest.s a$uboot_defconfig == a; then
        echo "Please set uboot_defconfig to the appropriate value"
        exit
fi

offset=0x400
a_uImage1=0x12000000
a_uImage2=0x12400000

if itest.s x6SX == "x${cpu}" || itest.s x7D == "x${cpu}"; then
	a_uImage1=0x82000000
	a_uImage2=0x82400000
fi

setenv stdout serial,vga
echo "check U-Boot" ;

if load ${dtype} ${disk}:1 ${a_uImage1} u-boot.$uboot_defconfig ; then
else
	echo "No U-Boot image found on SD card" ;
	exit
fi
echo "read $filesize bytes from SD card" ;
if sf probe || sf probe || sf probe 1 27000000 || sf probe 1 27000000 ; then
	echo "probed SPI ROM" ;
else
	echo "Error initializing EEPROM" ;
	exit
fi ;
if sf read ${a_uImage2} $offset $filesize ; then
else
	echo "Error reading boot loader from EEPROM" ;
	exit
fi
if cmp.b ${a_uImage1} ${a_uImage2} $filesize ; then
	echo "------- U-Boot versions match" ;
	exit
fi
echo "Need U-Boot upgrade" ;
echo "Program in 5 seconds" ;
for n in 5 4 3 2 1 ; do
	echo $n ;
	sleep 1 ;
done
echo "erasing" ;
sf erase 0 0xC0000 ;

# two steps to prevent bricking
echo "programming" ;
setexpr a1 ${a_uImage1} + 0x400
setexpr o1 ${offset} + 0x400
setexpr s1 ${filesize} - 0x400
sf write ${a1} ${o1} ${s1} ;
sf write ${a_uImage1} $offset 0x400 ;

echo "verifying" ;
if sf read ${a_uImage2} $offset $filesize ; then
else
	echo "Error re-reading EEPROM" ;
	exit
fi
if cmp.b ${a_uImage1} ${a_uImage2} $filesize ; then
else
	echo "Read verification error" ;
	exit
fi
while echo "---- U-Boot upgraded. reset" ; do
	sleep 120
done
