@echo off
rem *********changes********
rem v1.O
rem Windows + Test Mode
rem This tool has 3 parameters
rem -sorting: Check the CPU and SPI status at the begining of test process
rem -set: RPMC Set in the end of Windows process, then reboot the PC
rem -check: Check RPMC status after RPMC set and reboot
rem **************************
@echo on
@echo AMDRPMC.cmd v1.0 %date%-%time% >> biosmfg.log

if "%1"=="-set" goto SetRPMC
if "%1"=="-check" goto CheckRPMC
if "%1"=="-sorting" goto Sorting
goto MiscError1

rem ==================================================

:Sorting
echo RPMC sorting begins >> biosmfg.log
rpmctool.exe --check -n
echo errorlevel: %errorlevel% >> biosmfg.log
if %errorlevel%==2 goto RetryS1
goto StartSorting
:RetryS1
rpmctool.exe --check -n
echo errorlevel: %errorlevel% >> biosmfg.log
if %errorlevel%==2 goto MiscError2
:StartSorting
rem N is the return value of rpmctool.exe --check -n (%errorlevel% decimal)
rem N=0x10087F (New CPU + New SPI), 1050751
rem N=0x100801 (New CPU + No available counters on SPI. RPMC disabled), 1050625
rem N=0x19087F (Fused CPU(Slot full) + New SPI), 1640575
rem N= 0x190801 (Fused CPU(Slot full) + fused SPI without available counter) 1640449
rem N & 0xC03(3075) match CPU +SPI,RPMC enabled,return passed
rem Others (Pass)
set a=%errorlevel%
rem keep bit19、Bit16 to check CPU Slot is full or not
set /a b=a"&"1640575
set /a spi1=a ">>" 2
set /a spibit = spi1 "&" 15
set /a cpu1=a ">>" 19
set /a cpubit = cpu1 "&" 1
set /a RPMCbit=a"&"3075
if %b%==1050751 goto SortingPass
if %RPMCbit%==3075 goto SortingPass
if %b%==1640449 goto SortingErr3
if %spibit%==0 goto SortingErr1
if %cpubit%==1 goto SortingErr2
goto SortingPass

rem ==================================================

:SetRPMC
echo RPMC set begins >> biosmfg.log
echo check RPMC status firstly >> biosmfg.log
rpmctool.exe --check -n
echo errorlevel: %errorlevel% >> biosmfg.log
if %errorlevel%==2 goto RetryC1
goto Startcheck
:RetryC1
rpmctool.exe --check -n
echo errorlevel: %errorlevel% >> biosmfg.log
if %errorlevel%==2 goto MiscError2
:Startcheck
set a=%errorlevel%
set /a b=a"&"1640575
set /a spi1=a ">>" 2
set /a spibit = spi1 "&" 15
set /a cpu1=a ">>" 19
set /a cpubit = cpu1 "&" 1
set /a RPMCbit=a"&"3075

:RPMCEnabled
if %RPMCbit%==3075 goto SetPass
:NewCPUNewSPI
if %b%==1050751 goto Set0
:FusedCPUNoAvailableSPI
if %b%==1640449 goto SetErr3
:NoAvailableSPI
if %spibit%==0 goto SetErr1
:FusedCPU
if %cpubit%==1 goto SetErr2

:AvailableCPUwithSPIAvailableCounter
goto SetX

:Set0
echo Set SPI counter 0 >> biosmfg.log
rpmctool.exe --set 0 -n
echo errorlevel: %errorlevel% >> biosmfg.log
if %errorlevel%==2 goto SetErr4
goto SetPass
:SetX
rpmctool.exe --set 0 -n
echo errorlevel: %errorlevel% >> biosmfg.log
if not %errorlevel%==2 goto SetPass
rpmctool.exe --set 1 -n
echo errorlevel: %errorlevel% >> biosmfg.log
if not %errorlevel%==2 goto SetPass
rpmctool.exe --set 2 -n
echo errorlevel: %errorlevel% >> biosmfg.log
if not %errorlevel%==2 goto SetPass
rpmctool.exe --set 3 -n
echo errorlevel: %errorlevel% >> biosmfg.log
if not %errorlevel%==2 goto SetPass
goto SetErr4

rem ==================================================

:CheckRPMC
echo RPMC check begins >> biosmfg.log
rpmctool.exe --check -n
echo errorlevel: %errorlevel% >> biosmfg.log
if %errorlevel%==2 goto RetryC2
goto Startcheck1
:RetryC2
rpmctool.exe --check -n
echo errorlevel: %errorlevel% >> biosmfg.log
if %errorlevel%==2 goto MiscError2
:Startcheck1
set a=%errorlevel%
set /a b=a"&"3075
if %b%==3075 goto CheckPass
goto CheckErr1

rem ==================================================

:MiscError1
echo Missing or wrong parameter >> biosmfg.log
set ExitCode=200
goto Err

:MiscError2
echo RPMC command faild >> biosmfg.log
set ExitCode=201
goto Err

:SortingErr1
echo RPMC sorting error1: No available counters on SPI. RPMC disabled>> biosmfg.log
set ExitCode=300
goto Err

:SortingErr2
echo RPMC sorting error2: Fused CPU(Slot full)>> biosmfg.log
set ExitCode=301
goto Err

:SortingErr3
echo RPMC sorting error3: Fused CPU(Slot full) + fused SPI without available counter >> biosmfg.log
set ExitCode=302
goto Err

:SortingPass
echo RPMC sorting pass >> biosmfg.log
exit /b 0
goto end

:SetErr1
echo RPMC set error1: No available counters on SPI, RPMC disabled >> biosmfg.log
set ExitCode=400
goto Err

:SetErr2
echo RPMC set error2: Fused CPU >> biosmfg.log
set ExitCode=401
goto Err

:SetErr3
echo RPMC set error3: Fused CPU(Slot full) + fused SPI without available counter >> biosmfg.log
set ExitCode=402
goto Err

:SetErr4
echo RPMC set error4: Set failed >> biosmfg.log
set ExitCode=403
goto Err

:SetPass
echo RPMC set pass >> biosmfg.log
exit /b 0
goto end

:CheckErr1
echo RPMC check error1: RPMC not set correctly >> biosmfg.log
set ExitCode=500
goto Err

:CheckPass
echo RPMC check pass >> biosmfg.log
exit /b 0
goto end

:Err
echo RPMC set or check or sorting failed and exit execution. >> biosmfg.log
exit /b %ExitCode%
goto end

:end