@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
rem Display UPDATE information
echo.
echo Perform BIOS updates values:
echo Path to the BIOS update files = %UPD_PATH%\%UPD_BDIR%
echo.

sleep 15

echo Running system BIOS update ...

rem * Verify we have a known utility to run
if exist %UPD_PATH%\%UPD_BDIR%\lcreflsh.bat goto FLSHBAT
if exist %UPD_PATH%\%UPD_BDIR%\flash2.exe goto FLASH2

rem * OK, have fallen through without finding something to run, cleanup RAMDSK       
       
goto ERR_BF


:FLSHBAT
rem ********************************************************************************
rem * Special processing here, use provided batch file
rem ********************************************************************************

rem * Copy the update files to the RAM drive before running.
%RAMDSK%
cd \
md BIOS
cd BIOS
copy %UPD_PATH%\%UPD_BDIR%\*.* . > nul


rem * Drop the network if using ADS
if not %DPLY_TYPE%==5 goto :LCREFLSH

if exist %RAMDSK%\tknet.bat call %RAMDSK%\tknet.bat DOWN


:LCREFLSH
call lcreflsh.bat

rem * reconnect the network

if exist %RAMDSK%\tknet.bat call %RAMDSK%\tknet.bat UP

goto RAIDUPD


:FLASH2
rem ********************************************************************************
rem * Nothing special here, use standard flash2 utility
rem ********************************************************************************

rem * Copy the update files to the RAM drive before running.
%RAMDSK%
cd \
md BIOS
cd BIOS
copy %UPD_PATH%\%UPD_BDIR%\*.* . > nul

rem * Drop the network if using ADS
if not %DPLY_TYPE%==5 goto :FLASHEXE

if exist %RAMDSK%\tknet.bat call %RAMDSK%\tknet.bat DOWN

:FLASHEXE
FLASH2.EXE /u

rem * reconnect the network

if exist %RAMDSK%\tknet.bat call %RAMDSK%\tknet.bat UP

if not errorlevel 1 goto BIOSCLN
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

                                           
:BIOSCLN
rem ********************************************************************************
rem * Clean up the RAM drive for use by other Toolkit processes.
rem ********************************************************************************
%RAMDSK%
cd \
echo y | deltree \BIOS  > nul

                                           


: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

if not %NM_SI%==0 goto STARTRD

if not %NM_SK%==0 goto STARTRD

if not %NM_SS%==0 goto STARTRD

goto UPDSAVE


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


if %NM_SR%==0 goto UPD8I
echo.
echo Perform ServeRAID updates values:
echo Path to the ServeRAID update files = %UPD_PATH%\%UPD_SDIR%
echo.

sleep 15
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 \
mkdir %UPD_SDIR%
cd %UPD_SDIR%
copy %UPD_PATH%\%UPD_SDIR%\*.* .	> nul
FLASHMAN.EXE /CD:%RAMDSK%\ /AUTOXPRESS
if not %NM_SI%==0 goto UPD8I
if not %NM_SK%==0 goto UPD8K
if not %NM_SS%==0 goto UPD8S
if errorlevel 3 goto NOUPD
if not errorlevel 1 goto RAIDDONE
goto UPDSR1

:UPD8I
if %NM_SI%==0 goto UPD8K
echo.
echo Perform ServeRAID updates values:
echo Path to the ServeRAID 8i update files = %UPD_PATH%\%UPD_SV8I%
echo.
sleep 15
echo Running IBM ServeRAID-8i BIOS and Firmware update ...
rem * Check the path to the ServeRAID files before continuing.
if not exist %UPD_PATH%\SVRAID8I\afu.exe goto ERR_SRF
rem * Copy the update files to the RAM drive before running.
%RAMDSK%
cd \
mkdir %UPD_SV8I%
cd %UPD_SV8I% 
copy %UPD_PATH%\%UPD_SV8I%\*.* .	> nul
AFU UPDATE /CALL
if errorlevel 3 goto NOUPD
if not errorlevel 1 goto RAIDDONE
goto UPDSR1

:UPD8K
if %NM_SK%==0 goto UPD8S
echo.
echo Perform ServeRAID updates values:
echo Path to the ServeRAID 8k update files = %UPD_PATH%\%UPD_SV8K%
echo.
sleep 15
echo Running IBM ServeRAID-8k BIOS and Firmware update ...
rem * Check the path to the ServeRAID files before continuing.
if not exist %UPD_PATH%\SVRAID8K\afu.exe goto ERR_SRF
rem * Copy the update files to the RAM drive before running.
%RAMDSK%
cd \
mkdir %UPD_SV8K%
cd %UPD_SV8K% 
copy %UPD_PATH%\%UPD_SV8K%\*.* .	> nul
AFU UPDATE /CALL
if errorlevel 3 goto NOUPD
if not errorlevel 1 goto RAIDDONE

:UPD8S
echo.
echo Perform ServeRAID updates values:
echo Path to the ServeRAID 8s update files = %UPD_PATH%\%UPD_SV8S%
echo.
sleep 15
echo Running IBM ServeRAID-8s BIOS and Firmware update ...
rem * Check the path to the ServeRAID files before continuing.
if not exist %UPD_PATH%\SVRAID8S\afu.exe goto ERR_SRF
rem * Copy the update files to the RAM drive before running.
%RAMDSK%
cd \
mkdir %UPD_SV8S%
cd %UPD_SV8S% 
copy %UPD_PATH%\%UPD_SV8S%\*.* .	> nul
AFU UPDATE /CALL
if errorlevel 3 goto NOUPD
if not errorlevel 1 goto RAIDDONE





:UPDSR1
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 ********************************************************************************
set MSG=Can not find a BIOS update utility to run for the %MACHTYPE%.
echo.
echo %MSG%
echo.

if not %DPLY_TYPE%==4 goto ERROR
%TKDRV%\logevent.exe -c:2 -l:2 -ss:"FWUPDATE:  %MSG%"

goto ERROR


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

if not %DPLY_TYPE%==4 goto ERROR
%TKDRV%\logevent.exe -c:2 -l:2 -ss:"FWUPDATE:  %MSG%"

goto ERROR

:ERR_LOG
rem ********************************************************************************
rem * Found ServeRAID error log file
rem ********************************************************************************
echo.
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=
