@ECHO OFF
REM Copyright 2007 Intel Corporation All Rights Reserved.
REM
REM The following variables may need to be changed.
REM They can be included as command input as shown above
REM or changed here in the script if not passed as command input.
REM administrator login required for dwipe functions.
REM rmm2 authentication information is set to default.

REM Deployment directory, e.g. %DeployPath%\win32
SET DEPLOYDIR=%DeployPath%\win32
SET LOGFILE=%DeploymentStore%\erase.log
call varset PLATFORM "queryProperty IntelBIOSID BoardFamilyID -"
REM  dwipe cleans all volumes.  If running from removable storage media,
REM  enter the volume that you wish to skip.  For example SKIP_VOLUME=d:
SET SKIP_VOLUME=
set ADDLOG=call addLogRecord
REM set ADDLOG=echo
SET EXITCODE=0
SET RUNERASE=FALSE
SET ERRLEVEL=1

IF '%1'== '' (
    CALL :Usage
    goto :END
)

:CHECKFORSWITCHES

IF '%1'=='-s' GOTO STARTPROG
IF '%1'=='-u' GOTO SETUN
IF '%1'=='-p' GOTO SETPW
IF '%1'=='-v' GOTO SKVOL
IF '%1'=='' GOTO PROCEED
IF Not '%1'=='' (
    CALL :Usage
    goto :END
)


:STARTPROG
SHIFT
SET ERRLEVEL=0
SET RUNERASE=TRUE
GOTO CHECKFORSWITCHES

:SETUN
SHIFT
SET RMMuser=%1
SHIFT
GOTO CHECKFORSWITCHES

:SETPW
SHIFT
SET RMMpassword=%1
SHIFT
GOTO CHECKFORSWITCHES

:SKVOL
SHIFT
SET SKIP_VOLUME=%1
SHIFT
GOTO CHECKFORSWITCHES

:PROCEED
IF %RUNERASE% == TRUE (
	echo "ERASE start %PLATFORM%" >%LOGFILE%
	call addLogRecord ERASE INFO "ERASE start %PLATFORM%"
	call addLogRecord ERASE INFO "LOGFILE=%LOGFILE%"
	CALL :RESETBIOS
	CALL :RESETRMM2
	CALL :RESETBMC
	CALL :CLEANSTORAGE
	CALL :ERASERAID
)
GOTO :EXIT

:RESETBIOS
pushd %DEPLOYDIR%\syscfg
ECHO Restoring BIOS settings to a default set of values ...
REM syscfg/syscfg /rfs
syscfg /bldfs
set ERRLEVEL=%ERRORLEVEL%
IF %ERRLEVEL% NEQ 0 CALL :ERROR BIOS
IF %ERRLEVEL% EQU 0 CALL :OK BIOS
popd
goto :EOF

:RESETRMM2
call queryProperty RMM "OEM" -
set ERRLEVEL=%ERRORLEVEL%
if %ERRLEVEL% EQU 0 (
    ECHO Restoring RMM2 settings to a default set of values
    pushd "%DeployPath%\win32\kiratool"
    REM  RMM2 Username and Password
    if "%RMMuser%" == "" SET RMMuser=admin
    if "%RMMpassword%" == "" SET RMMpassword=password
    call addLogRecord ERASE INFO "Restoring RMM2 settings to a default set of values"
    call addLogRecord ERASE INFO "kiratool  -l 127.0.0.1 -u %RMMuser% -p %RMMpassword% defaults"
    kiratool  -l 127.0.0.1 -u %RMMuser% -p %RMMpassword% defaults
    set ERRLEVEL=%ERRORLEVEL%
    IF %ERRLEVEL% NEQ 0 call :ERROR RMM2
    IF %ERRLEVEL% EQU 0 call  :OK RMM2
    popd
)
goto :EOF

:RESETBMC
if "%PLATFORM%" == "S5000" (
  REM S5000-Clears NVRAM and sets BMC to default settings
  iresetbmc -c -b
  set ERRLEVEL=%ERRORLEVEL%
  IF %ERRLEVEL% NEQ 0 CALL :ERROR BMC
  IF %ERRLEVEL% EQU 0 CALL :OK BMC
)
if "%PLATFORM%" == "S3000" (
  REM :RESETAMT
  REM S3000-Restores AMT settings to default
  amtcfg -d
  CALL :REPORT  AMT %ERRORLEVEL%

)
goto :EOF

:CLEANSTORAGE
ECHO Erasing hard drives by writing data to each block
echo list volume  >dp.in
diskpart /s dp.in >drives.txt
set ERRLEVEL=%ERRORLEVEL%
type drives.txt |findstr /V /C:"Volume ##" |findstr Volume >vols.txt
del dp.in drives.txt
type vols.txt >>%LOGFILE%
set COUNTER=1
FOR /F "tokens=3,9 delims= " %%a in ('type vols.txt') DO (
	echo [%COUNTER%] dwipe %%a %%b  >>%LOGFILE%
	if "%COUNTER%" NEQ 1 (
		if NOT "%%b" == "System" (
          dwipe -f -s -z %%a
          set ERRLEVEL=%ERRORLEVEL%
        )
    )
	if %ERRLEVEL% NEQ 0 CALL :ERROR DISK
	set COUNTER=%COUNTER%+1
)
CALL :OK DISK
goto :EOF

:ERASERAID
ECHO Erasing RAID volumes
REM # Clear the RAID volume, if any

call queryProperty BIOSRAID SWRAID Enabled
IF %ERRORLEVEL% EQU 1 (
    if "%PLATFORM%" == "S3000" (
        REM # vend=`queryProperty BIOSRAID Vendor LSI`
        call varSet RAIDVENDOR "queryProperty BIOSRAID Vendor -"
        IF "%RAIDVENDOR%" EQU "LSI" (
            REM # clear LSI SW RAID with Hypercfg
            hyperwin /DEL /l0
            set ERRLEVEL=%ERRORLEVEL%
        )
        IF "%RAIDVENDOR%" EQU "Intel" (
            REM # clear Matrix SW RAID with Raidcfg32
            Raidcfg32 /Y /D Demo0
            set ERRLEVEL=%ERRORLEVEL%
        )
    ) else (
        CmdTool2 -CfgLdDel -LALL -aALL
        set ERRLEVEL=%ERRORLEVEL%

    )
    call setBIOS4RAID disable
)

call queryProperty BIOSRAID HWRAID Enabled
IF %ERRORLEVEL% EQU 1 (
    CmdTool2 -CfgLdDel -LALL -aALL
    set ERRLEVEL=%ERRORLEVEL%
)
if %ERRLEVEL% NEQ 0 (
    CALL :ERROR RAID
) else (
    CALL :OK RAID
)
goto :eof

:ERROR
set CMD=%1
ECHO The erase %CMD% command failed. >>%LOGFILE%
%ADDLOG% ERASE ERROR "erase %CMD% failed. ERRLEVEL=%ERRLEVEL%"
set ERRLEVEL=5
goto :EOF

:OK
set CMD=%1
ECHO The erase %CMD% command completed successfully. >>%LOGFILE%
set ERRLEVEL=0
%ADDLOG% ERASE INFO "erase %CMD% succeeded."
goto :EOF

:REPORT
set CMD=%1
if %2 NEQ 0 (
    ECHO The erase %CMD% command failed. >>%LOGFILE%
    %ADDLOG% ERASE ERROR "erase %CMD% failed. ERRLEVEL=%2"
    set ERRLEVEL=5
) else (
    ECHO The erase %CMD% command completed successfully. >>%LOGFILE%
    %ADDLOG% ERASE INFO "erase %CMD% succeeded."
)
goto :EOF


:Usage
ECHO The Erase Utility restores the machine to a default factory state, erasing disk drives,
ECHO removing non-volatile data, and restoring default configuration values.
ECHO Please use this tool with caution.
ECHO To run the functions on this tool, erase must be run with a -s switch.
ECHO Please refer to the syntax below:
ECHO .
ECHO Usage: erase [ -supv ]
ECHO  where -s	switch needed to run erase functions
ECHO	 	-u	username for RMM2 module
ECHO		-p	password for RMM2 module
ECHO		-v	volume to skip during the storage cleaning
ECHO .
goto :EOF

:EXIT
IF %ERRLEVEL% EQU 0 ECHO The erase utility completed successfully.
IF %ERRLEVEL% NEQ 0 ECHO The erase utility completed with errors.
echo ERASE completed with status %ERRLEVEL%  >>%LOGFILE%
EXIT /B %ERRLEVEL%

:END
