@ECHO OFF
rem cd|set /p TOPDIR=
set hba_id=9128
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=
if "%1"=="" goto DO_FLASH
:loop

if "%1"=="-h" goto help
if "%1"=="-help" goto help
if "%1"=="" goto DO_FLASH
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%

bin\%MVF% -setparm -did %hba_id% -aid %ad_id%

if ERRORLEVEL 1 goto end
call %parmfile%
del %parmfile%
if "%ADCOUNT%"=="" goto end
if "%ADCOUNT%"=="0" goto end
if "%ADCOUNT%"=="5" goto end
if "%ADCOUNT%"=="6" goto end
if "%ADCOUNT%"=="7" goto end
REM if "%REVID%"=="10" set IMAGFILE=ImageA0.bin
REM if "%REVID%"=="11" set IMAGFILE=ImageA1.bin
set IMAGFILE=imageRAW.bin
if "%REVID%"=="" goto end
if "%IMAGFILE%"=="" goto end
echo ----------------------------------------------------------------------
echo                           MARVELL FLASH TOOL
echo ----------------------------------------------------------------------
cd bin
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%
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
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
:IMG_END_SETP
cd ..

if "%ONCE%"=="1" goto check_error
if "%ADCOUNT%"=="1" goto check_error
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
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=
