@echo off

SET PSW_DIR=PSW_INF

REM Remove sgx_psw INF
REM for /f "delims=" %%i in ( 'pnputil.exe -a %~dp0\%PSW_DIR%\sgx_psw.inf ^| findstr "oem.*.inf" ') do set psw_str=%%i

for /f "delims=" %%i in ( 'pnputil.exe -a %~dp0\%PSW_DIR%\component\sgx_psw.inf ^| findstr "oem.*.inf" ') do set psw_str=%%i

echo "Uninstall PSW driver: %psw_str:~-11%"
pnputil.exe /delete-driver %psw_str:~-11% /uninstall


timeout /t 3
REM Remove sgx_base INF
for /f "delims=" %%i in ( 'pnputil.exe -a %~dp0\%PSW_DIR%\device\sgx_base.inf ^| findstr "oem.*.inf" ') do set base_str=%%i

echo "Uninstall BASE driver: %base_str:~-11%"
pnputil.exe /delete-driver %base_str:~-11% /uninstall


REM Clean registry keys and installed binaries
sc stop aesmservice

timeout /t 5

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Intel\SGX_PSW  /f

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{50AFD900-F309-4557-8FCB-10CFAB802CDD} /f

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Interface\{50AFD900-F309-4557-8FCB-10CFAB802CDD} /f

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Interface\{C0BCB06C-A1F3-4847-91E7-71C9E76DF789} /f

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{82367CAB-F2B9-461A-B6C6-889D13EFC6CA} /f

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SGX.AESM /f

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppId\{D5D53ADA-EAFD-4AA8-BD6B-DA32F20171C4} /f

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Channels\SGX/Admin /f

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Channels\SGX/Diagnostic /f

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Publishers\{ce6e83d3-a7d9-4a91-96e0-e018ad574610} /f

reg delete HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WMI\Autologger\EventLog-Application\{ce6e83d3-a7d9-4a91-96e0-e018ad574610} /f

reg delete HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\CLSID\{50AFD900-F309-4557-8FCB-10CFAB802CDD} /f

sc delete aesmservice

takeown /f "C:\ProgramData\Intel\IntelSGXPSW" /r /d y

rmdir "C:\ProgramData\Intel\IntelSGXPSW" /s /q

takeown /f "c:\windows\system32\sgx_uae_service.dll"
icacls "C:\Windows\System32\sgx_uae_service.dll" /reset /c
del c:\windows\system32\sgx_uae_service.dll

takeown /f "c:\windows\system32\sgx_urts.dll"
icacls "C:\Windows\System32\sgx_urts.dll" /reset /c
del c:\windows\system32\sgx_urts.dll

del C:\Windows\SysWOW64\aesm_service_ps.dll
del C:\Windows\SysWOW64\sgx_uae_service.dll
del C:\Windows\SysWOW64\sgx_urts.dll




