@echo off

rem ********************************************************************************
rem *
rem * FWUPDATE.BAT
rem *
rem * This file controls running updates as part of the ServerGuide Scripting 
rem * Toolkit installation process.
rem *
rem * This batch file is called by runinst.bat
rem *
rem ********************************************************************************


:RUN_UPDS
rem ********************************************************************************
rem * Run the updates
rem ********************************************************************************

rem * Change to Toolkit drive
%TKDRV%

set UPDD=N

:BIOSUPD
rem ********************************************************************************
rem * System BIOS update
rem ********************************************************************************
FOR %%i in (n N) do if %UPD_BIOS% == %%i goto RAIDUPD

set UPDD=Y

echo Running system BIOS update ...

if exist %UPD_PATH%\%UPD_BDIR%\lcreflsh.bat goto FLSHBAT
if exist %UPD_PATH%\%UPD_BDIR%\flash2.exe goto FLASH2

goto ERR_BF


:FLSHBAT
rem ********************************************************************************
rem * Special processing here, use provided batch file
rem ********************************************************************************
cd %UPD_PATH%\%UPD_BDIR%

call lcreflsh.bat

goto RAIDUPD


:FLASH2
rem ********************************************************************************
rem * Nothing special here, use standard flash2 utility
rem ********************************************************************************
cd %UPD_PATH%\%UPD_BDIR%

FLASH2.EXE /u

if not errorlevel 1 goto RAIDUPD
echo There was an error updating the system BIOS.
echo.

if not %DPLY_TYPE%==4 goto ERROR

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"FWUPDATE:  There was an error updating the system BIOS."
goto ERROR



:RAIDUPD
rem ********************************************************************************
rem * IBM ServeRAID update
rem ********************************************************************************

rem * Reset path
%TKDRV%
cd \

FOR %%i in (n N) do if %UPD_SR% == %%i goto UPDSAVE

rem * Make sure an IBM ServeRAID controller is present.

call hwdet.bat

rem ********************************************************************************
rem * the NM_xx env vars are set in hwdet.bat. Each indicates how many of
rem * each adapter type was found by hwdetect.exe.
rem ********************************************************************************
if not %NM_SR%==0 goto STARTRD

goto UPDSAVE


:STARTRD
rem ********************************************************************************
rem * ServeRAID BIOS and firmware update
rem ********************************************************************************
set UPDD=Y

echo Running IBM ServeRAID BIOS and Firmware update ...

rem * Check the path to the ServeRAID files before continuing.
if not exist %UPD_PATH%\%UPD_SDIR%\flashman.exe goto ERR_SRF

rem * Copy the update files to the RAM drive before running.
%RAMDSK%
cd \
copy %UPD_PATH%\%UPD_SDIR%\*.* .

FLASHMAN.EXE /CD:%RAMDSK%\ /AUTOXPRESS

if errorlevel 3 goto NOUPD
if not errorlevel 1 goto RAIDDONE
echo There was an error updating the IBM ServeRAID BIOS and firmware.
echo.

if not %DPLY_TYPE%==4 goto LOGCK

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"FWUPDATE:  Error updating the IBM ServeRAID BIOS and firmware."

:LOGCK
if exist %RAMDSK%\flashman.log goto ERR_LOG
goto ERROR

:NOUPD
if exist %RAMDSK%\flashman.log goto ERR_LOG

echo All IBM ServeRAID adapters were already up to date. 

if not %DPLY_TYPE%==4 goto RAIDDONE

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"FWUPDATE:  All IBM ServeRAID adapters were already up to date."

:RAIDDONE

rem * Reset path
%TKDRV%
cd \

goto UPDSAVE

rem ********************************************************************************
rem * Add other updates here as necessary
rem ********************************************************************************


:UPDSAVE
rem ********************************************************************************
rem * Write information to persistent storage that will identify what installation
rem * steps have been completed so far, and control where processing resumes after 
rem * the reboot.
rem ********************************************************************************
echo Updates Complete!  Saving status and rebooting server if necessary.

rem ********************************************************************************
rem * If not doing a standard Toolkit installation, jump
rem ********************************************************************************
if %DPLY_TYPE%==4 goto ALTSTAT0
if %DPLY_TYPE%==5 goto REBOOT

savestat.exe /set2=25 > nul

rem ********************************************************************************
rem * Check for errors: SAVESTAT.EXE
rem ********************************************************************************
if not errorlevel 255 goto UPDBOOT
echo An error occurred during writing to persistent storage.
echo.
goto ERROR


:UPDBOOT
rem ********************************************************************************
rem * Updates are done, so we need to reboot the system before continuing to 
rem * process.
rem ********************************************************************************
if %UPDD%==Y goto REBOOT 
goto END

:REBOOT
sleep 15
reboot.com

:ERR_BF
rem ********************************************************************************
rem * Can not find the file.
rem ********************************************************************************
echo Can not find a BIOS update utility to run.
echo.

if not %DPLY_TYPE%==4 goto ERROR

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"FWUPDATE:  Can not find a BIOS update utility to run."
goto ERROR


:ERR_SRF
rem ********************************************************************************
rem * Can not find the file.
rem ********************************************************************************
echo Can not find a ServeRAID update utility to run.
echo.

if not %DPLY_TYPE%==4 goto ERROR

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"FWUPDATE:  Can not find a ServeRAID update utility to run."
goto ERROR

:ERR_LOG
rem ********************************************************************************
rem * Found ServeRAID error log file
rem ********************************************************************************
echo ServeRAID update encountered a problem, view the file %RAMDSK%\FLASHMAN.LOG
echo for more information.
echo.

if not %DPLY_TYPE%==4 goto ERROR

%TKDRV%\logevent.exe -c:2 -l:2 -ss:"FWUPDATE:  ServeRAID update encountered a problem."

md %TK_PATH%\altiris\temp\%AID% > nul
copy %RAMDSK%\FLASHMAN.LOG %TK_PATH%\altiris\temp\%AID% > nul

rem ********************************************************************************
rem * Check for errors: COPY.EXE
rem ********************************************************************************
if not errorlevel 1 goto NEXTMSG
echo An error occurred during copy.
echo.
goto ERROR

:NEXTMSG
%TKDRV%\logevent.exe -c:2 -l:2 -ss:"FWUPDATE:  View the file sgdeploy\sgtk\altiris\temp\%AID%\flashman.log."
goto ERROR



:ERROR
rem ********************************************************************************
rem * Hit an error in this batch file.
rem ********************************************************************************
echo Encountered an error and stopped processing in "fwupdate.bat".

if %DPLY_TYPE%==4 goto ALTSTAT1

savestat.exe /set2=251 > nul

goto END


:ALTSTAT0
rem ********************************************************************************
rem * Update Altiris with necessary status
rem ********************************************************************************
echo Updating Altiris status: Good
%TKDRV%\logevent.exe -c:0 -l:1 -ss:"FWUPDATE: Completed successfully."

rem Reset to root of Toolkit drive
%TKDRV%
cd \

goto END


:ALTSTAT1
rem ********************************************************************************
rem * Update Altiris with necessary status
rem ********************************************************************************
echo Updating Altiris status: Error
%TKDRV%\logevent.exe -c:251 -l:3 -ss:"FWUPDATE:  Encountered an error."

rem Reset to root of Toolkit drive
%TKDRV%
cd \

rem Reset persistent storage
savestat.exe /reset > nul
goto END


:END
set UPDD=
