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

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

#if errorlevel 07 if not errorlevel 08 goto Flash_BIOS_ME
if %lasterror% == "0x07" then 
   if not %lasterror% == "0x08" 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

#Compare_FDT_Table
echo **************************************************************************
echo *
echo *  Compare FDT Table
echo *  Please wait....
echo *
echo **************************************************************************
   afuefix64.efi %1 /ME
# ##Read CMOS71 value
   fdtx64.efi -r 71
if %lasterror% == "0x5A" then 
   if not %lasterror% == "0x5B" then 
	goto Manually_Disable_ME
   endif
endif

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


:Manually_Disable_ME
echo **************************************************************************
echo *
echo *  ME FDT Changed!!
echo *  Please switch JPME2 to 2-3 to disable ME
echo *  (If there is no adjustment, Flash BIOS will skip updating FDT block)
echo *  Then press any key...
echo *
echo **************************************************************************
  PAUSE -q
  fdtx64.efi -w 71 5A
  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
	afuefix64.efi %1 /P /B /N /K /R /ME /RLC:F
        fdtx64.efi -r 71

        if %lasterror% == "0x07" then 
          if not %lasterror% == "0x08" then 
            goto Manually_Enable_ME
          endif
        endif
goto END


:Manually_Enable_ME
echo **************************************************************************
echo *
echo *  Please switch JPME2 to 1-2 to Enable ME if switch JPME2 to 2-3 before.
echo *  Please press any key and power off the system.
echo *
echo **************************************************************************
  PAUSE -q
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
	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
