echo -off

if %1 == "" then
   goto Err_Print
endif

if not exist %1 then 
   goto No_File
endif

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

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

# ##Read CMOS71 value
   fdtx64.efi -r 71

#if errorlevel 05 if not errorlevel 06 goto Flash_BIOS_ME
if %lasterror% == "0x05" then 
   if not %lasterror% == "0x06" then 
      goto Flash_BIOS_ME
   endif
endif

#EnterME_Disable
	echo ##########################################################################
	echo #
	echo #   ME need enter flash mode.
	echo #
	echo ##########################################################################
	fdtx64.efi -w 71 A5
	fdtx64.efi -w 70 A5
	
  mv fdtx64.efi fdt.smc 
  mv afuefix64.efi afuefi.smc
  
	goto Modify_Autoexec

:Flash_BIOS_ME
echo **************************************************************************
echo *
echo *  Program BIOS and ME regions...
echo *
echo **************************************************************************

# echo afuefix64.efi %1 /P /B /N /K /R /ME /RLC:F
	afuefix64.efi %1 /P /B /N /K /R /ME /RLC:F
	mv fdtx64.efi fdt.smc
	mv afuefix64.efi afuefi.smc
	
	cd \
	if exist fdtx64.efi then 
	  del fdtx64.efi
	endif
	
	if exist Startup.nsh then 
	   del Startup.nsh
	endif
	
  if exist a.nsh then 
    mv a.nsh Startup.nsh
	endif
goto END

:Modify_Autoexec
    cp fdt.smc \fdtx64.efi
		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 **************************************************************************

#choice /C:Y /T:Y,10
echo 
Stall 10000000
	fdtx64.efi -reset


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

: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