::
:: Support Save Batch file for windows
::

@ECHO off

IF (%1)==(/?) GOTO _USAGE
IF /I (%1)==(--help) GOTO _USAGE
IF /I (%1)==(-h) GOTO _USAGE
IF /I (%1)==(--version) GOTO _VERSION
IF /I (%1)==(-v) GOTO _VERSION
if NOT (%2)==() GOTO _USAGE

call driverquery /v | find "Running" > TMP1.txt 2> nul

type TMP1.txt | find "bfad" > TMP2.txt 2> nul
type TMP1.txt | find "bnad" > TMP3.txt 2> nul

call driverquery /v | find "BNIP" > TMP4.txt 2> nul

set SS_IS_BFA=""
set SS_IS_BNA=""
set SS_IS_IM=""

for /F "tokens=*" %%i IN (TMP2.txt) DO set SS_IS_BFA=true
for /F "tokens=*" %%j IN (TMP3.txt) DO set SS_IS_BNA=true
for /F "tokens=*" %%k IN (TMP4.txt) DO set SS_IS_IM=true

del /f TMP1.txt TMP2.txt TMP3.txt TMP4.txt > nul 2>&1

VER | TIME /t > TEMP.BAT
VER | DATE /t > TEMP1.BAT
hostname > TEMP2.BAT
set /p HOST=< TEMP2.BAT
set HOST=%HOST:~-32%
ECHO SET TIME=%%3>CURRENT.BAT
ECHO SET DATE=%%3>CURRENT1.BAT
CALL TEMP.BAT 1  > nul 2>&1
CALL TEMP1.BAT 1  > nul 2>&1
set str=%DATE% %TIME%
set str=%str:~3%
set str= %HOST%%str%

del /f TEMP.BAT TEMP1.BAT TEMP2.BAT CURRENT.BAT CURRENT1.BAT > nul 2>&1

set str=%str:/=-%
set str=%str: =_%
set str=%str::=_%
set str=%str:.=_%

set BFA_INSTALL_LOG=bfa_install.log
set SS_SUBDIR_NAME=bfa_ss_out
set SS_SHOW_OFILE=bfa_ss.txt
set SS_SHOW=bfa_supportshow.bat
set SS_SYSCFG=bfa_syscfg.bat

set SS_SUBDIR=%SS_SUBDIR_NAME%%str%
set SS_SYSCFG_SUBDIR=syscfg

if (%1)==() (set SS_OUTDIR=%SS_SUBDIR%)
if not (%1)==() (set SS_OUTDIR="%~f1\%SS_SUBDIR%")

if (%1)==(bfa_ss_out) (
rd /q /s bfa_ss_out 1 > nul 2>&1
set SS_OUTDIR=bfa_ss_out
)

if (%1)==(hbfail_bfa_ss_out) (
rd /q /s hbfail_bfa_ss_out 1 > nul 2>&1 
set SS_OUTDIR=hbfail_bfa_ss_out
)

mkdir %SS_OUTDIR%
if ERRORLEVEL 1 GOTO _END

mkdir %SS_OUTDIR%\%SS_SYSCFG_SUBDIR%
if ERRORLEVEL 1 GOTO _END

FOR /F "usebackq" %%i IN (`bcu.exe adapter --count`) DO set SS_ADAPTER=%%i

echo Fetching the driver traces for all the ports...

for /L %%i in (1,1,%SS_ADAPTER%) do (
for /F "usebackq" %%j in (`bcu.exe port --bfs_list %%i`) do ( 
for /F "usebackq" %%k in (`bcu.exe port --pcifn_list %%i %%j`) do ( 
CALL bcu.exe debug --dtrc %%i/%%j/%%k %SS_OUTDIR%\dtrc_ad%%i-p%%j-f%%k.bin 2> NUL
CALL bfatrc.exe %SS_OUTDIR%\dtrc_ad%%i-p%%j-f%%k.bin > %SS_OUTDIR%\dtrc_ad%%i-p%%j-f%%k.txt 2> NUL
)
)
for /F "usebackq" %%j in (`bcu.exe port --bfs_list %%i`) do ( 
for /F "usebackq" %%k in (`bcu.exe port --pcifn_ethlist %%i %%j`) do ( 
if "%SS_IS_BNA%"=="true" (
if "%SS_IS_IM%"=="true" (
CALL bcu.exe ethport --trc %%i/%%j/%%k %SS_OUTDIR%\bnitrc_ad%%i-p%%j-f%%k.bin 2> NUL
CALL bfatrc.exe %SS_OUTDIR%\bnitrc_ad%%i-p%%j-f%%k.bin > %SS_OUTDIR%\bnitrc_ad%%i-p%%j-f%%k.txt 2> NUL
)
)
)
)
)
)
)

echo Dumping flash data for adapters... 
for /L %%i in (1,1,%SS_ADAPTER%) do (
CALL bcu.exe flash --read  %%i -s %SS_OUTDIR%\asic_ad%%i.bin >NUL 2>NUL
CALL bcu.exe flash --read  %%i -b %SS_OUTDIR%\boot_ad%%i.bin >NUL 2>NUL
CALL bcu.exe flash --read  %%i -pt %SS_OUTDIR%\portcfg_ad%%i.bin >NUL 2>NUL
)


echo Fetching the firmware traces/logs for all the ports...
for /L %%i in (1,1,%SS_ADAPTER%) do (
for /F "usebackq" %%j in (`bcu.exe port --bfs_list %%i`) do ( 
CALL bcu.exe debug --ftrc %%i/%%j %SS_OUTDIR%\ftrc_ad%%i-p%%j.bin > nul 2>&1
CALL bfatrc.exe %SS_OUTDIR%\ftrc_ad%%i-p%%j.bin > %SS_OUTDIR%\ftrc_ad%%i-p%%j.txt 2> NUL

if "%%j"=="0" (
CALL bcu.exe flash --read %%i -r %SS_OUTDIR%\flog_ad%%i.bin 1>nul 2>nul
CALL bfatrc.exe %SS_OUTDIR%\flog_ad%%i.bin >  %SS_OUTDIR%\flog_ad%%i.txt 2>nul
)

for /F "usebackq" %%k in (`bcu.exe port --pcifn_list %%i %%j`) do ( 
CALL bcu.exe debug --fsave %%i/%%j/%%k %SS_OUTDIR%\fsave_ad%%i-p%%j-f%%k.bin > nul 2>&1
CALL bfatrc.exe %SS_OUTDIR%\fsave_ad%%i-p%%j-f%%k.bin > %SS_OUTDIR%\fsave_ad%%i-p%%j-f%%k.txt 2> NUL
)

::read the PBC file
ECHO.Reading PBC file for port %%i/%%j
CALL bcu flash  --read %%i -p  %SS_OUTDIR%\pbc_flash_ad%%i-p%%j.bin -i %%j >NUL 2>NUL
)
)

if "%SS_IS_BFA%"=="true" (
echo Dumping the registry contents of Storage Driver...
REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\bfad\Parameters\Device" > %SS_OUTDIR%\reg_fcdump.txt 2>&1
REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\bfadfcoe\Parameters\Device" > %SS_OUTDIR%\reg_fcoedump.txt 2>&1
REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\bfadi\Parameters\Device" > %SS_OUTDIR%\reg_fcdump_inbox.txt 2>&1
REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\bfadfcoei\Parameters\Device" > %SS_OUTDIR%\reg_fcoedump_inbox.txt 2>&1
)

if "%SS_IS_BNA%"=="true" (
echo Dumping the registry contents of Ethernet Adapter...
REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}" /f "QLogic BR-series 10G Ethernet Adapter" /d /s | find "HKEY_LOCAL_MACHINE" > TMP.txt
for /F "tokens=*" %%i IN (TMP.txt) DO REG QUERY %%i /s > %SS_OUTDIR%\reg_ethdump.txt
del /f TMP.TXT > nul 2>&1

if "%SS_IS_IM%"=="true" (
echo Dumping the registry contents of Intermediate Driver...
REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}" /f "QLogic BR-series 10G Virtual Miniport Driver" /d /s | find "HKEY_LOCAL_MACHINE" > TMP.txt
for /F "tokens=*" %%i IN (TMP.txt) DO REG QUERY %%i /s > %SS_OUTDIR%\reg_imdriverdump.txt
del /f TMP.TXT > nul 2>&1

echo Dumping the registry contents of Intermediate Driver with different driver decsription...
REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}" /f "QLogic BR-series 10G Advanced Virtual Miniport Driver" /d /s | find "HKEY_LOCAL_MACHINE" > TMP.txt
for /F "tokens=*" %%i IN (TMP.txt) DO REG QUERY %%i /s > %SS_OUTDIR%\reg_imdriverdump.txt
del /f TMP.TXT > nul 2>&1

REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\BNIM" /s > %SS_OUTDIR%\reg_immdump.txt 2> NUL
REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\BNIP" /s > %SS_OUTDIR%\reg_impdump.txt 2> NUL

echo Fetching the IM driver traces...
CALL bcu.exe debug --itrc %SS_OUTDIR%\ethitrc.bin 2>nul
CALL bfatrc.exe %SS_OUTDIR%\ethitrc.bin > %SS_OUTDIR%\ethitrc.txt 2> NUL
for /F "usebackq" %%k in (`bcu.exe team --teams`) do ( 
CALL bcu.exe team --trc %%k %SS_OUTDIR%\teamtrc_%%k.bin 2> NUL
CALL bfatrc.exe %SS_OUTDIR%\teamtrc_%%k.bin > %SS_OUTDIR%\teamtrc_%%k.txt 2> NUL
)
)
)

echo Dumping the ASIC block content for ct2 cards...
for /L %%i in (1,1,%SS_ADAPTER%) do (
CALL bcu.exe flash --dumpasicblk %%i %SS_OUTDIR%\ct2_ablk_ad%%i.bin > nul 2>&1
)

echo Dumping the FRU VPD contents for 1867/1869 adapters...
for /L %%i in (1,1,%SS_ADAPTER%) do (
call bcu.exe adapter --query %%i | find "QLogic-1867" > NUL 2>&1
if not ERRORLEVEL 1 (
CALL bcu.exe fruvpd --read %%i 0 8192 > %SS_OUTDIR%\fruvpd_ad%%i.txt 2> NUL
) else (
call bcu.exe adapter --query %%i | find "QLogic-1869" > NUL 2>&1
if not ERRORLEVEL 1 (
CALL bcu.exe fruvpd --read %%i 0 8192 > %SS_OUTDIR%\fruvpd_ad%%i.txt 2> NUL
)
)
)
echo.

echo -----------------------Copying installation logs -------------------------- 
copy %BFA_INSTALL_LOG% %SS_OUTDIR% > nul 2>&1

echo -----------------------Fetching system configuration details--------------- 
CALL %SS_SYSCFG% %SS_OUTDIR%\%SS_SYSCFG_SUBDIR% 2>NUL

echo.
echo -----------------------Fetching powercfg details---------------------------

mkdir %SS_OUTDIR%\powercfglog
pushd %SS_OUTDIR%\powercfglog
CALL  powercfg -ENERGY -XML > nul 2>&1
CALL powercfg -QUERY > query.txt
popd

echo -----------------------Fetching supportshow details------------------------ 
CALL %SS_SHOW% 1> %SS_OUTDIR%/%SS_SHOW_OFILE% 2>NUL

:: ----------------------------Setupapi logs ---------------------------------
:: Warning: setupapi log may not be complete in case windows setupapi file is
::          deleted after adapter driver installtion.
call bcu winlog --setupapi
move setupapilog.txt %SS_OUTDIR%\%SS_SYSCFG_SUBDIR% > nul 2>&1

echo.
echo -----------------------Copying hcmagent logs------------------------------
set HCMAGT_LOCATION="%programfiles%\BROCADE\Adapter\driver\util\hbaagent\"
if not exist %HCMAGT_LOCATION% (
set HCMAGT_LOCATION="%programfiles% (x86)\BROCADE\Adapter\driver\util\hbaagent\"
)
if not exist %HCMAGT_LOCATION% (
set HCMAGT_LOCATION="%programfiles%\BROCADE\DriverPackage\bfa\util\hbaagent\"
)
if not exist %HCMAGT_LOCATION% (
set HCMAGT_LOCATION="%programfiles% (x86)\BROCADE\DriverPackage\bfa\util\hbaagent\"
)

if exist %HCMAGT_LOCATION% (
    mkdir %SS_OUTDIR%\hbaagentlog
    mkdir %SS_OUTDIR%\hbaagentconf
    copy %HCMAGT_LOCATION%\log %SS_OUTDIR%\hbaagentlog > nul 2>&1
    copy %HCMAGT_LOCATION%\conf %SS_OUTDIR%\hbaagentconf > nul 2>&1
    copy %HCMAGT_LOCATION%\*.dmp %SS_OUTDIR%\hbaagentlog > nul 2>&1
    %HCMAGT_LOCATION%\bin\hcmagent.exe -l > %SS_OUTDIR%\hbaagentlog\AgentSysinfo.txt 2>&1
    ipconfig >> %SS_OUTDIR%\hbaagentlog\AgentSysinfo.txt 2>&1
    ver >> %SS_OUTDIR%\hbaagentlog\AgentSysinfo.txt 2>&1
    date /T >> %SS_OUTDIR%\hbaagentlog\AgentSysinfo.txt 2>&1
    time /T >> %SS_OUTDIR%\hbaagentlog\AgentSysinfo.txt 2>&1
    set >> %SS_OUTDIR%\hbaagentlog\AgentSysinfo.txt 2>&1
)

::copy the snmpdbg log file
echo.
echo -----------------------Copying SnmpAgent logs------------------------------
set SNMPAGT_LOCATION="%SystemRoot%\System32\"

if exist %SNMPAGT_LOCATION% (
mkdir %SS_OUTDIR%\snmpagentlog
copy %SNMPAGT_LOCATION%\snmpdbg.log %SS_OUTDIR%\snmpagentlog > nul 2>&1
)

::copy the bni_config ini file
set BNICFG_FILE="%ALLUSERSPROFILE%\Brocade\bniconfig\bni_config.ini"
if exist %BNICFG_FILE% (
copy %BNICFG_FILE% %SS_OUTDIR%\ > nul 2>&1
)

::copy the bni_config log file
set BNICFG_LOG_FILE="%ALLUSERSPROFILE%\Brocade\bniconfig\bni_config.log"
if exist %BNICFG_LOG_FILE% (
copy %BNICFG_LOG_FILE% %SS_OUTDIR%\ > nul 2>&1
)

echo.
echo -----------------------Output folder %SS_OUTDIR%-----

GOTO _END

:_USAGE
echo.
echo.Usage
echo.
echo.	bfa_supportsave
echo.		to create and save the supportsave in the current directory.
echo.
echo.	bfa_supportsave "<dir>"
echo.		to create and save the supportsave under "<dir>".
echo.
echo.	bfa_supportsave /?
echo.		to view bfa_supportsave usage.
echo.
echo.	bfa_supportsave --help
echo.		to view bfa_supportsave usage.
echo.
echo.	bfa_supportsave -h
echo.		to view bfa_supportsave usage.
echo.
echo.	bfa_supportsave --version
echo.		to view bfa_supportsave version information.
echo.
echo.	bfa_supportsave -v
echo.		to view bfa_supportsave version information.
echo.
GOTO _END

:_VERSION
echo.
echo.QLogic BR-series Support-Save utility
CALL bcu -v | find /v /i "qlogic"

:_END
EXIT /B %CB_EXIT_CODE%

