echo -off

set Debug_Mode 0
set Flash_Milan 0

if %1 == "" then
   goto Err_Print
endif

if %2 == "-d" then
   set Debug_Mode 1
endif

if not exist %1 then
   goto No_File
endif

if exist fdt.smc then
   mv fdt.smc fdtx64.efi
endif

if exist afuefi.smc then
   mv afuefi.smc afuefix64.efi
endif

if %1 == "Milan.bin" then
   set Flash_Milan 1
   goto Dont_Split
endif

if exist Split.smc then
   mv Split.smc Splitx64.efi
endif

#echo **************************************************************************
#echo *
#echo *  Split the BIOS for individual update.
#echo *
#echo **************************************************************************

splitx64.efi %1 Rome.bin Milan.bin
mv splitx64.efi split.smc

:Dont_Split

echo **************************************************************************
echo *
echo *  Program Non Boot BIOS...
echo *
echo **************************************************************************

if %Flash_Milan% == 0 then
afuefix64.efi Rome.bin /x /SSB:{NONBOOT16M} /RLC:e
endif

if %Flash_Milan% == 1 then
afuefix64.efi Milan.bin /x /SSB:{NONBOOT16M} /RLC:e
goto Check_Startup
endif

if %Debug_Mode% == 1 then 
   echo Modify c:\Startup.nsh
endif

fdtx64.efi -mauto Milan.bin

mv afuefix64.efi afuefi.smc

del Rome.bin

:Reboot_System
echo **************************************************************************
echo *
echo *   System will reboot to change the operating mode in 10 seconds....
echo *   
echo *   Please also ensure to boot system from "'UEFI: Built-in EFI Shell'"  
echo *   boot device by pressing F11 hot key later for continuing process 
echo *   of programming BIOS.
echo *
echo **************************************************************************

Stall 10000000

if %Debug_Mode% == 1 then 
   pause
endif

fdtx64.efi -reset

goto END

:Check_Startup

mv fdtx64.efi fdt.smc
mv afuefix64.efi afuefi.smc

del Milan.bin

cd \
if exist Startup.nsh then
    echo **************************************************************************
    echo *
    echo *  Please ignore this "'Shell: Cannot read from file - Device Error'" 
    echo *  warning message due to it does not impact flashing process.
    echo *
    echo **************************************************************************
    del Startup.nsh
endif

if exist a.nsh then 
   mv a.nsh Startup.nsh
endif

:BIOS_Update_Completed
echo **************************************************************************
echo *
echo *   The process of BIOS update is completed. Please do AC cycle.
echo *
echo **************************************************************************

if %Debug_Mode% == 1 then 
   pause
endif

goto END

:No_File
echo **************************************************************************
echo *
echo *  %1 doesn't exist and please double check.
echo *
echo **************************************************************************
goto END

:Err_Print
echo **************************************************************************
echo *
echo "*  Usage:"
echo "*         flash.nsh romfile"
echo *
echo **************************************************************************

:END