@echo off
REM #     flash-s5.cmd  - script to Flash BIOS,BMC,FRUSDR
REM #

set flashdir=%DeployPath%\win32\iFlash
set fwupdir=%DeployPath%\win32
set frudir=%DeployPath%\win32\frusdr
set fwdir=%DeployPath%\update

call :setFilename BIOSNAME
call :setFilename FWNAME
call :setFilename CFGNAME

echo BIOSNAME=%BIOSNAME%
echo FWNAME=%FWNAME%
echo CFGNAME=%CFGNAME%

set logfile=%DeploymentStore%\flashs5.log
set ADDLOG=call addLogRecord
REM set ADDLOG=echo
set RET=0

call addLogRecord FLASH INFO  "BIOSNAME=%BIOSNAME%"
call addLogRecord FLASH INFO  "FWNAME=%FWNAME%"
call addLogRecord FLASH INFO  "CFGNAME=%CFGNAME%"
call addLogRecord FLASH INFO  "logfile=%logfile%"

call queryProperty FRU  "Chassis OEM Field"  - | varSet chassis  > nul
call exportvarSet chassis

call :doflash
call :dofw
call :dofru
goto :DONE

:doflash
    pushd %flashdir%
    echo flash-s5 started >%logfile%
    REM # install the flashud.sys driver
    REM win2kinstall %flashdir%\flashud.inf *INT0800 >>%logfile%
    REM # get the BIOS, Firmware, FRUSDR versions
    iflash32 /i     >>%logfile%
    if errorlevel 1 (
        set RET=1
        %ADDLOG% FLASH ERROR "Error: iflash32 cannot read BIOS version info."
    ) else (
        REM # flash the BIOS
        echo iflash32 /ni /u %fwdir%\%BIOSNAME% >>%logfile%
        iflash32 /ni /u %fwdir%\%BIOSNAME% >>%logfile%
        if errorlevel 1 (
           RET=1
           %ADDLOG% FLASH ERROR "Error: iflash32 with %BIOSNAME% failed"
        ) else (
            %ADDLOG% FLASH INFO "Success: iflash32 with %BIOSNAME%"
        )
    )
    REM win2kuninstall *INT0800 >>%logfile%
    popd
goto :EOF


:dofw
    REM  The is an issue with build 5990 of the RMM2 firmware
    REM  that causes a conflict when flashing the HSC FW.
    call queryProperty RMM OEM -
    if %ERRORLEVEL% EQU 0 set RMM=present
    if "%RMM%" == "present" (
        if Exist "%DeployPath%\win32\kiratool\kiratool.exe" (
            pushd %DeployPath%\win32\kiratool
            REM Disable the RMM2 IPMI-passthru to avoid conflict with HSC fw flash.
            kiratool -u admin -p password  cfg set ipmi.medium._c_ none
            REM # wait a few seconds until any currently ongoing IPMB communication stops.
            sleep 5
        )
    )

    REM # flash the firmware
    pushd %fwupdir%
    REM # Reload the v10.0 IMBDRV.sys driver
    REM # Could skip this if load it by default
    REM net stop "Intel IPMI Service"  >>%logfile%
    REM frusdr\win2kuninstall *IMBDRV >>%logfile%
    REM copy /y imbdrv.sys X:\i386\System32\Drivers >>%logfile%
    REM frusdr\win2kinstall %fwupdir%\imbdrv.inf *IMBDRV >>%logfile%
    REM # Run the fw update utility
    fwpiaupd -i >>%logfile%
    echo fwpiaupd -ni -p -u -o -pia %fwdir%\%FWNAME%  >>  %logfile%
    fwpiaupd -ni -p -u -o -pia  %fwdir%\%FWNAME% >>%logfile%
    if errorlevel 1 (
        set RET=1
        %ADDLOG% FLASH ERROR "Error: fwpiaupd with %FWNAME% failed"
    ) else (
        %ADDLOG% FLASH INFO "Success: fwpiaupd with %FWNAME%"
    )
    popd

    if "%RMM%" == "present" (
        if Exist "%DeployPath%\win32\kiratool\kiratool.exe" (
            REM # After firmware update, reactivate IPMI-passthru. You must not forget this step!
            kiratool -u admin -p password  cfg set ipmi.medium._c_ i2c

            REM # It may happen that the RMM2 behaves strangely after that,
            REM # so it is recommended to reboot the module as a final step.
            echo Wait 30 seconds for RMM2 to initialize, then we can reset
            sleep 30
            echo Reseting RMM2....
            kiratool -u admin -p password reset
            echo This is not an error.  RMM2 is still in the process of resetting.
            echo wait 3 minutes....
            sleep 180
            kiratool -u admin -p password fw
            if ERRORLEVEL 0 (
                echo RMM2 reset complete.
            ) else (
                echo RMM2 reset FAILED.
            )
            popd
        )
    )
goto :EOF

:dofru
    pushd %frudir%
    echo  install the ibsmutil.sys driver
    rem win2kinstall %frudir%\ibsmutil.inf *IntelUtilDeV >>%logfile%
    echo update the FRUSDR
    move %fwdir%\%CFGNAME% %fwdir%\%CFGNAME%.SAVE
    call setMASTER.cmd  > %fwdir%\%CFGNAME%
    if "%chassis%" == "SR2500LX" (
        copy %fwdir%\MASTER.SR2500LX.AC.CFG.SAVE >> %fwdir%\%CFGNAME%
    ) else (
        type %fwdir%\%CFGNAME%.SAVE >> %fwdir%\%CFGNAME%
    )
    echo frusdr /cfg %fwdir%\%CFGNAME% >>%logfile%
    frusdr /cfg %fwdir%\%CFGNAME% >>%logfile%
    call :FRURESULT %ERRORLEVEL%
    rem win2kuninstall *IntelUtilDeV >> %logfile%
    popd
goto :EOF

:FRURESULT
    if %1 EQU 1 (
         set RET=%1
         %ADDLOG% FLASH ERROR "Error: frusdr with %CFGNAME% failed"
    ) else (
         %ADDLOG% FLASH INFO "Success: frusdr with %CFGNAME%"
    )
goto :EOF

:setFilename
    call varSet CFGLINE "findstr  %1 %fwdir%\flashupdt.cfg"
    call :getFilename  %CFGLINE%
    goto :EOF

:getFilename
    set %1=%~2
    goto :EOF

:DONE
echo flash with %BIOSNAME%, %FWNAME%, %CFGNAME% done, RET=%RET%
exit /B %RET%
