@ECHO OFF
rem cd|set /p TOPDIR=
set hba_id=9230
set MVF=mvf_mag.exe
set parmfile=SETPARM.BAT
set BOARD=hba
set DEV_LIST=
set OPTS=
set ONCE=
set ADCOUNT=
set ad_id=0
set READIMG=
set READSPIIMG=
set RawImgFile0=ImageA0.bin
set RawImgFile1=ImageA1.bin

cd bin
%MVF% -chkver -rawf %RawImgFile1% -descoff 0x2000
cd..

if "%1"=="" goto DO_FLASH
:loop

if "%1"=="-h" goto help
if "%1"=="-help" goto help
if "%1"=="" goto DO_FLASH
if "%1"=="-ver" set SHOW_VERSION=1
if "%1"=="-ver" goto next
if "%1"=="-e" set OPTS=%OPTS% %1
if "%1"=="-e" goto next
if "%1"=="-a" set OPTS=%OPTS% %1
if "%1"=="-a" goto next
if "%1"=="-y" set OPTS=%OPTS% %1
if "%1"=="-y" goto next
if "%1"=="-b" set OPTS=%OPTS% %1 %2
if "%1"=="-b" goto next
if "%1"=="-x" set OPTS=%OPTS% %1
if "%1"=="-x" goto next
if "%1"=="-w" set OPTS=%OPTS% %1
if "%1"=="-w" goto next
if "%1"=="-v" set OPTS=%OPTS% %1
if "%1"=="-v" goto next
if "%1"=="-r" set READIMG=1
if "%1"=="-r" goto next
if "%1"=="-ra" set READSPIIMG=1
if "%1"=="-ra" goto next
if "%1"=="-adid" set OPTS=%OPTS% %1 %2
if "%1"=="-adid" set ONCE=1
if "%1"=="-adid" set ad_id=%2
if "%1"=="-adid" goto next
if "%1"=="-fdid" set hba_id=%2
if "%1"=="-fdid" goto next
goto fail_command

:next
if "%1"=="-adid" shift
if "%1"=="-fdid" shift
shift
goto loop

:DO_FLASH
if "%hba_id%"=="generic" goto end
if "%ONCE%"=="1" set CRTID=%ad_id%

if "%SHOW_VERSION%"=="1" echo AdapterID=%ad_id%
cd bin
%MVF% -chkver -did %hba_id% -aid %ad_id% -spif -descoff 0x2000 -unshownv
cd ..
bin\%MVF% -setparm -did %hba_id% -aid %ad_id% -unshownv

if ERRORLEVEL 1 goto end
call %parmfile%
del %parmfile%
if "%ADCOUNT%"=="" goto end
if "%ADCOUNT%"=="0" goto end
if "%ADCOUNT%"=="11" goto end
if "%ADCOUNT%"=="12" goto end
if "%ADCOUNT%"=="13" goto end
if "%REVID%"=="10" set IMAGFILE=%RawImgFile0%
if "%REVID%"=="11" set IMAGFILE=%RawImgFile1%
REM set IMAGFILE=%RawImgFile1%
if "%REVID%"=="" goto end
if "%IMAGFILE%"=="" goto end
rem echo ----------------------------------------------------------------------
rem echo                           MARVELL FLASH TOOL
rem echo ----------------------------------------------------------------------
cd bin
if "%SHOW_VERSION%"=="1" goto IMG_END_SETP
if "%READIMG%"=="1" goto READ_IMG_STEP
if "%READSPIIMG%"=="1" goto DUMP_SPI_IMG_STEP
%MVF% %IMAGFILE% -raw -forcedid %hba_id% -aid %CRTID% -fs 0 -off 0 %OPTS% -unshownv
goto IMG_END_SETP
:READ_IMG_STEP
%MVF% -did %hba_id% -raw -off 0 -len 0x7b000 -bkup -name bkimg%CRTID%.bin -aid %CRTID% -cs 3 -fs 0 -y -unshownv
goto IMG_END_SETP
:DUMP_SPI_IMG_STEP
%MVF% -did %hba_id% -raw -off 0 -len %SPIFLASHSIZE% -bkup -name bkimg%CRTID%.bin -aid %CRTID% -cs 3 -fs 0 -y -unshownv
:IMG_END_SETP
cd ..

if "%ONCE%"=="1" goto check_error
if "%ADCOUNT%"=="1" goto check_error
if "%ad_id%"=="9" set ad_id=10
if "%ad_id%"=="8" set ad_id=9
if "%ad_id%"=="7" set ad_id=8
if "%ad_id%"=="6" set ad_id=7
if "%ad_id%"=="5" set ad_id=6
if "%ad_id%"=="4" set ad_id=5
if "%ad_id%"=="3" set ad_id=4
if "%ad_id%"=="2" set ad_id=3
if "%ad_id%"=="1" set ad_id=2
if "%ad_id%"=="0" set ad_id=1
if "%ad_id%"=="%ADCOUNT%" goto check_error
goto do_flash

:check_error
if not ERRORLEVEL 1 goto end
echo Flash error, wipeout autoload!!
cd bin
%MVF% %IMAGFILE% -newImag -a -e -y
cd ..
goto end

:erase
if "%BOARD%"=="" goto help
echo erase for %BOARD%...
goto erase%BOARD%
goto end

REM Do help screen
:help
echo ----------------------------------------------------------------------
echo                        MARVELL FLASH TOOL HELP
echo ----------------------------------------------------------------------
echo type 'go'         Update Magni Firmware
echo type 'go -y'      Update Magni Firmware with Yes included
REM Have split screen
if "%OPTS%"=="-v" goto detailed
shift
if "%1"=="-v" goto detailed
echo type 'go -h'      Display simple help screen (this screen)
goto end
:detailed
echo type 'go -h -v'   Display detailed help screen (this screen)
echo type 'go -e'      Erase every sector
echo type 'go -a'      Flash only autoload
echo type 'go -b'      Flash only BIOS on MAGNI
echo type 'go -b -e'   Erase only BIOS
echo type 'go -x'      Flash only Firmware on MAGNI
echo type 'go -x -e'   Erase only Firmware on MAGNI
echo type 'go -v'      Show verbose message
echo type 'go -r'      Read back binary file, no flashing
echo type 'go -ra'     Dump the data of Spiflash to a binary file
echo type 'go -aid 0'  Update the specified the adapter 0
each type 'go -ver'    Components version
goto end

:fail_end
echo Did not find device! Check connection.
goto end

:fail_command
echo Bad Command.  Please check with help!
goto end

:end
set AD_ID=
set HBA_ID=
set ADCOUNT=
set CRTID=
set SPIFLASHSIZE=
set BOARD=
set NORAID=
set OPTS=
set ERASE=
set MVF=
set IMAGE=
set cmdline=
set IMAGFILE=
set REVID=
set parmfile=
set RawImgFile0=
set RawImgFile1=
set SHOW_VERSION=
