echo -off

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

if not exist %1 then 
    goto No_File
endif

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

fdtx64.efi -r 71

if %lasterror% == "0x5" then
    goto Update_BIOS_ME
endif


fdtx64.efi -w 71 A5
goto Modify_Autoexec



:Update_BIOS_ME
echo **************************************************************************
echo *
echo *  Program BIOS and ME (including FDT) regions...
echo *
echo **************************************************************************

afuefix64.efi %1 /B /P /N /K /R /L0 /ME /RLC:F

if not %lasterror% == "0x0" then
    goto END
endif

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

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

echo **************************************************************************
echo *
echo *  Please press any key to reset system...
echo *
echo **************************************************************************
  PAUSE -q
  mm cf9 e -w 1 -io

goto END1

:Modify_Autoexec
echo **************************************************************************
echo *
echo *  Creating new Startup.nsh file...
echo *
echo **************************************************************************

fdtx64.efi -mauto %1

: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 **************************************************************************

echo 
Stall 10000000

fdtx64.efi -reset

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


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

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

:END1