@echo off


echo [%date% %time%] Main script  >> C:\windows\log\AsMUpdate.txt

schtasks /delete /tn "ASUS FW Update Manager" /f


set BiosRomVersion=315

set reboot=0

for /f "tokens=2 delims==" %%f in ('wmic bios get smbiosbiosversion /value ^| find "="') do set "BIOSVersion=%%f"

for /F "tokens=1* delims=." %%G in ("%BIOSVersion%") DO set "BiosCurVersion=%%H"

echo BiosRomVersion %BiosRomVersion%
echo BiosCurVersion %BiosCurVersion%

:: Force flash BIOS if current BIOS is test version
echo %BiosCurVersion%| findstr /r "^[0-9][0-9]*$">nul

if %errorlevel% neq 0 (
    echo Invalid number
    goto :eof
)

if %BiosCurVersion% GEQ %BiosRomVersion% (
  echo BIOS updated
  echo [%date% %time%] BIOS updated  >> C:\windows\log\AsMUpdate.txt
) else (
  goto :eof
)

set /a count=0

:installFW

set /a count=count+1

pushd %~dp0
"ASUS_ASMx4242_FWUpdateV1001_u42423020920021EAcmAPDIO2U3V1_CMD.exe"

SET ELV=%ERRORLEVEL%
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\ASUS\ASMx4242" /f /v ret /t REG_SZ /d %ELV%
echo [%date% %time%] FW installer %ELV%  >> C:\windows\log\AsMUpdate.txt

IF "%ELV%"=="0" (
  reg add "HKEY_LOCAL_MACHINE\SOFTWARE\ASUS\ASMx4242" /f /v DisplayVersion /t REG_SZ /d 3.8.2
) else (
  if %count% GEQ 5 (
    goto resetSettings
  ) else (
    echo try again
    timeout /t 5
    goto installFW
  )
)


:resetSettings

echo [%date% %time%] %ELV%  >> C:\windows\log\AsMUpdate.txt

set reboot=0

if exist C:\windows\log\ALU (
  echo check VMP
) else (
  goto :eof
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\HypervisorPlatform.log
if %errorlevel% == 0 (
  echo HypervisorPlatform was enabled
  dism /online /enable-feature /featurename:HypervisorPlatform /norestart
  set reboot=1
) else (
  echo HypervisorPlatform was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\VirtualMachinePlatform.log
if %errorlevel% == 0 (
  echo VirtualMachinePlatform was enabled
  dism /online /enable-feature /featurename:VirtualMachinePlatform /norestart
  set reboot=1
) else (
  echo VirtualMachinePlatform was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\Microsoft-Hyper-V-All.log
if %errorlevel% == 0 (
  echo Microsoft-Hyper-V-All was enabled
  dism /online /enable-feature /featurename:Microsoft-Hyper-V-All /norestart
  set reboot=1
) else (
  echo Microsoft-Hyper-V-All was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\Microsoft-Hyper-V.log
if %errorlevel% == 0 (
  echo Microsoft-Hyper-V was enabled
  dism /online /enable-feature /featurename:Microsoft-Hyper-V /norestart
  set reboot=1
) else (
  echo Microsoft-Hyper-V was disabled
)


findstr /n /i /p /c:"Enable" C:\windows\log\ALU\Microsoft-Hyper-V-Tools-All.log
if %errorlevel% == 0 (

  echo Microsoft-Hyper-V-Tools-All was enabled
  dism /online /enable-feature /featurename:Microsoft-Hyper-V-Tools-All /norestart
  set reboot=1
) else (
  echo Microsoft-Hyper-V-Tools-All was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\Microsoft-Hyper-V-Management-PowerShell.log
if %errorlevel% == 0 (
  echo Microsoft-Hyper-V-Management-PowerShell was enabled
  dism /online /enable-feature /featurename:Microsoft-Hyper-V-Management-PowerShell /norestart
  set reboot=1
) else (
  echo Microsoft-Hyper-V-Management-PowerShell was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\Microsoft-Hyper-V-Hypervisor.log
if %errorlevel% == 0 (
  echo Microsoft-Hyper-V-Hypervisor was enabled
  dism /online /enable-feature /featurename:Microsoft-Hyper-V-Hypervisor /norestart
  set reboot=1
) else (
  echo Microsoft-Hyper-V-Hypervisor was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\Microsoft-Hyper-V-Services.log
if %errorlevel% == 0 (
  echo Microsoft-Hyper-V-Services was enabled
  dism /online /enable-feature /featurename:Microsoft-Hyper-V-Services /norestart
  set reboot=1
) else (
  echo Microsoft-Hyper-V-Services was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\Microsoft-Hyper-V-Management-Clients.log
if %errorlevel% == 0 (
  echo Microsoft-Hyper-V-Management-Clients was enabled
  dism /online /enable-feature /featurename:Microsoft-Hyper-V-Management-Clients /norestart
  set reboot=1
) else (
  echo Microsoft-Hyper-V-Management-Clients was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\HostGuardian.log
if %errorlevel% == 0 (
  echo HostGuardian was enabled
  dism /online /enable-feature /featurename:HostGuardian /norestart
  set reboot=1
) else (
  echo HostGuardian was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\Containers-DisposableClientVM.log
if %errorlevel% == 0 (
  echo Containers-DisposableClientVM was enabled
  dism /online /enable-feature /featurename:Containers-DisposableClientVM /norestart
  set reboot=1
) else (
  echo Containers-DisposableClientVM was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\Microsoft-Windows-Subsystem-Linux.log
if %errorlevel% == 0 (
  echo Microsoft-Windows-Subsystem-Linux was enabled
  dism /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /norestart
  set reboot=1
) else (
  echo Microsoft-Windows-Subsystem-Linux was disabled
)

findstr /n /i /p /c:"Enable" C:\windows\log\ALU\MemoryIntegrity.log
if %errorlevel% == 0 (
  echo MemoryIntegrity  was enabled
  reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\DeviceGuard\Scenarios\HypervisorEnforcedCodeIntegrity" /v Enabled /t REG_DWORD /d 1 /f
  set reboot=1
) else (
  echo MemoryIntegrity  was disabled
)


IF "%ELV%"=="0" (
  "%~dp0AsFwInstaller.exe" PASS
) ELSE (
  "%~dp0AsFwInstaller.exe" FAIL
)
