# Environment to be imported by the firmware update process from SD card. 
# This is put on the SD card in uEnv.txt. 
# For the general environment, see u_boot_env.txt. 
uenvcmd=run uenv_fw_update

# Check if all required files for FW update are present. 
sd_fwu_present_test=test -e mmc 0 BOOT.bin && test -e mmc 0 u_boot_env.bin && test -e mmc 0 uImage && test -e mmc 0 devicetree.dtb && test -e mmc 0 uramdisk.image.gz && test -e mmc 0 system.bin

# Check if firmware update done. 
sd_fwu_complete_file=FW_UPDATE_COMPLETE
sd_fwu_done_test=mmc info && test -e mmc 0 ${sd_fwu_complete_file}

sd_fw_update_load_all=mw.l 0x08000000 0xffffffff 0x400000 && run sd_fw_update_load_boot && run sd_fw_update_load_env && run sd_fw_update_load_linux && run sd_fw_update_load_dtb && run sd_fw_update_load_rootfs && run sd_fw_update_load_fpga
sd_fw_update_load_boot=load   mmc 0 0x08000000 BOOT.bin          
sd_fw_update_load_env=load    mmc 0 0x080E0000 u_boot_env.bin    
sd_fw_update_load_linux=load  mmc 0 0x08100000 uImage            
sd_fw_update_load_dtb=load    mmc 0 0x08600000 devicetree.dtb    
sd_fw_update_load_rootfs=load mmc 0 0x08620000 uramdisk.image.gz 
# Note: Old racks have $bitstream_size=1fcb9c without the "0x" in front!
sd_fw_update_load_fpga=load   mmc 0 0x08C00000 system.bin        && if test "0x$filesize" = "$bitstream_size" -o "0x$filesize" = "0x$bitstream_size" ; then echo "OK bitstream size is correct: 0x$filesize" ; else echo ; echo "*** Wrong bitstream size: 0x$filesize != $bitstream_size" ; echo "*** This firmware update is not suitable for this device." ; echo "*** Please contact Wieserlabs for support." ; echo ; false ; fi

sd_fw_update_flash_all=sf probe 0 100000000 0 && run sd_fw_update_flash_linux && run sd_fw_update_flash_dtb && run sd_fw_update_flash_rootfs && run sd_fw_update_flash_fpga && run sd_fw_update_flash_boot && run sd_fw_update_flash_env
sd_fw_update_flash_boot=   echo "Flashing BOOT.bin ..."          && sf erase 0x000000 0x0E0000 && sf write 0x08000000 0x000000 0x0E0000 && sf read 0x09000000 0x000000 0x0E0000 && cmp.l 0x08000000 0x09000000 0x038000
sd_fw_update_flash_env=    echo "Flashing u_boot_env.bin ..."    && sf erase 0x0E0000 0x020000 && sf write 0x080E0000 0x0E0000 0x020000 && sf read 0x090E0000 0x0E0000 0x020000 && cmp.l 0x080E0000 0x090E0000 0x008000
sd_fw_update_flash_linux=  echo "Flashing uImage ..."            && sf erase 0x100000 0x500000 && sf write 0x08100000 0x100000 0x500000 && sf read 0x09100000 0x100000 0x500000 && cmp.l 0x08100000 0x09100000 0x140000
sd_fw_update_flash_dtb=    echo "Flashing devicetree.dtb ..."    && sf erase 0x600000 0x020000 && sf write 0x08600000 0x600000 0x020000 && sf read 0x09600000 0x600000 0x020000 && cmp.l 0x08600000 0x09600000 0x008000
sd_fw_update_flash_rootfs= echo "Flashing uramdisk.image.gz ..." && sf erase 0x620000 0x5E0000 && sf write 0x08620000 0x620000 0x5E0000 && sf read 0x09620000 0x620000 0x5E0000 && cmp.l 0x08620000 0x09620000 0x178000
sd_fw_update_flash_fpga=   echo "Flashing system.bin ..."        && sf erase 0xC00000 0x3E0000 && sf write 0x08C00000 0xC00000 0x3E0000 && sf read 0x09C00000 0xC00000 0x3E0000 && cmp.l 0x08C00000 0x09C00000 0x0f8000

sd_fw_update_go=run sd_fw_update_load_all && run sd_fw_update_flash_all && echo && echo "** FW update performed" && save mmc 0 0x08000000 ${sd_fwu_complete_file} 4 && echo "Restarting..." && sleep 3 && reset

uenv_fw_update=echo;echo "Checking for firmware update...";if run sd_fwu_done_test; then echo "** FW update complete"; else if run sd_fwu_present_test; then echo "** FW update present"; run sd_fw_update_go; fi; fi;
