-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathXHCI-IMOD-Interval.ps1
More file actions
201 lines (164 loc) · 7.32 KB
/
XHCI-IMOD-Interval.ps1
File metadata and controls
201 lines (164 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
param(
[switch]$verbose
)
# The variable names starting with "$global" store values that should be applied to all XHCI controllers
# by default. To override this default value on a per-controller basis, specify the DEV_XXXX hardware ID
# value for the XHCI controller along with its data. See the example and explanation below.
#
# $globalInterval = 0x0
# $globalHCSPARAMSOffset = 0x4
# $globalRTSOFF = 0x18
#
# $userDefinedData = @{
# "DEV_5BD2" = @{
# "ENABLED" = $false
# }
# "DEV_8A4D" = @{
# "INTERVAL" = 0xFFFF
# "HCSPARAPS_OFFSET" = 0x8
# "RTSOFF" = 0x12
# }
# "DEV_4AC6" = @{
# "INTERVAL" = 0xFA00
# }
# }
#
# Explanation:
#
# - Possible fields:
# - ENABLED
# - INTERVAL
# - HCSPARAPS_OFFSET
# - RTSOFF
#
# - For the DEV_5BD2 XHCI controller, the IMOD interval will not be set/changed at all because the ENABLED
# field is set to $false. This is generally for XHCI controllers which offsets are undocumented and unknown
#
# - The IMOD interval, HCSPARAMS and RTSOFF offsets for all controllers will be treated as 0x0, 0x4 and
# 0x18 respectively as these are the global values
#
# - For the DEV_8A4D XHCI controller, the IMOD interval, HCSPARAMS and RTSOFF offsets will override the
# global values and will be treated as 0xFFFF, 0x8, 0x12 respectively
#
# - For the DEV_4AC6 XHCI controller, only the IMOD interval will be overridden (0xFA00). The global values
# will be used for each field that is not specified because they are optional
#
#
# https://github.com/valleyofdoom/PC-Tuning/blob/main/bin/XHCI-IMOD-Interval.ps1
#
$globalInterval = 0x0
# common offsets across XHCI vendors that will work with most XHCI controllers
$globalHCSPARAMSOffset = 0x4
$globalRTSOFF = 0x18
$userDefinedData = @{}
# specify the path to Rw.exe
$rwePath = "C:\Program Files\RW-Everything\Rw.exe"
function Dec-To-Hex($decimal) {
$hexValue = $decimal.ToString("X2")
return "0x$($hexValue)"
}
function Get-Value-From-Address($address) {
# convert the hex value to string for RWE
$address = Dec-To-Hex -decimal ([uint64]$address)
$stdout = & $rwePath /Min /NoLogo /Stdout /Command="R32 $($address)" | Out-String
$splitString = $stdout -split " "
return [uint64]$splitString[-1]
}
function Get-Device-Addresses() {
$data = @{}
$resources = Get-WmiObject -Class Win32_PNPAllocatedResource -ComputerName LocalHost -Namespace root\CIMV2
foreach ($resource in $resources) {
$deviceId = $resource.Dependent.Split("=")[1].Replace('"', '').Replace("\\", "\")
$physicalAddress = $resource.Antecedent.Split("=")[1].Replace('"', '')
if (-not $data.ContainsKey($deviceId) -and $deviceId -and $physicalAddress) {
$data[$deviceId] = [uint64]$physicalAddress
}
}
return $data
}
function Is-Admin() {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
return $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
function main() {
if (-not (Is-Admin)) {
Write-Host "error: administrator privileges required"
return 1
}
if (-not (Test-Path $rwePath -PathType Leaf)) {
Write-Host "error: $($rwePath) not exists, edit the script to change the path to Rw.exe"
Write-Host "http://rweverything.com/download"
return 1
}
# CLI will not work if Rw is already open
Stop-Process -Name "Rw" -ErrorAction SilentlyContinue
# get physical address of each device present in the system
$deviceMap = Get-Device-Addresses
foreach ($xhciController in Get-WmiObject Win32_USBController) {
$isDisabled = $xhciController.ConfigManagerErrorCode -eq 22
if ($isDisabled) {
continue
}
$deviceId = $xhciController.DeviceID
# check if there is data defined for the current controller by the user
$userDefinedController = @{}
foreach ($hwid in $userDefinedData.Keys) {
if ($deviceId -match $hwid) {
$userDefinedController = $userDefinedData[$hwid]
}
}
# check if the user has specified not to apply the IMODI for the current controller
if ($userDefinedController.ContainsKey("ENABLED") -and (-not $userDefinedController["ENABLED"])) {
continue
}
Write-Host "$($xhciController.Caption) - $($deviceId)"
# skip if entry is null
if (-not $deviceMap.Contains($deviceId)) {
Write-Host "error: could not obtain base address`n"
continue
}
$desiredInterval = $globalInterval
$hcsparamsOffset = $globalHCSPARAMSOffset
$rtsoff = $globalRTSOFF
# process the user defined data
if ($userDefinedController.ContainsKey("INTERVAL")) {
$desiredInterval = $userDefinedController["INTERVAL"]
}
if ($userDefinedController.ContainsKey("HCSPARAPS_OFFSET")) {
$hcsparamsOffset = $userDefinedController["HCSPARAPS_OFFSET"]
}
if ($userDefinedController.ContainsKey("RTSOFF")) {
$rtsoff = $userDefinedController["RTSOFF"]
}
$capabilityAddress = $deviceMap[$deviceId]
$HCSPARAMSValue = Get-Value-From-Address -address ($capabilityAddress + $hcsparamsOffset)
$HCSPARAMSBitmask = [Convert]::ToString($HCSPARAMSValue, 2)
$maxIntrs = [Convert]::ToInt32($HCSPARAMSBitmask.Substring($HCSPARAMSBitmask.Length - 16, 8), 2)
$RTSOFFValue = Get-Value-From-Address -address ($capabilityAddress + $rtsoff)
$runtimeAddress = $capabilityAddress + $RTSOFFValue
if ($verbose) {
$formattedCapabilityAddress = Dec-To-Hex -decimal $capabilityAddress
$formattedRTSOFFValue = Dec-To-Hex -decimal $RTSOFFValue
Write-Host "capability_address = $($formattedCapabilityAddress)"
Write-Host "HCSPARAMS_value = capability_address + hcsparams_offset = $($formattedCapabilityAddress) + $(Dec-To-Hex -decimal $hcsparamsOffset) = $(Dec-To-Hex -decimal $HCSPARAMSValue)"
Write-Host "HCSPARAMS_bitmask = $($HCSPARAMSBitmask)"
Write-Host "max_intrs = $($maxIntrs)"
Write-Host "RTSOFF_value = capability_address + rtsoff = $($formattedCapabilityAddress) + $(Dec-To-Hex -decimal $rtsoff) = $($formattedRTSOFFValue)"
Write-Host "runtime_address = capability_address + RTSOFF_value = $($formattedCapabilityAddress) + $($formattedRTSOFFValue) = $(Dec-To-Hex -decimal $runtimeAddress)"
}
for ($i = 0; $i -lt $maxIntrs; $i++) {
# calculate address and convert address to hex string
$interrupterAddress = Dec-To-Hex -decimal ([uint64]($runtimeAddress + 0x24 + (0x20 * $i)))
if ($verbose) {
Write-Host "`ninterrupter_address = runtime_address + 0x24 + (0x20 * index) = $(Dec-To-Hex -decimal $runtimeAddress) + 0x24 + (0x20 * $($i)) = $($interrupterAddress)"
Write-Host "Write DWORD = $(Dec-To-Hex -decimal $desiredInterval)"
}
& $rwePath /Min /NoLogo /Stdout /Command="W32 $($interrupterAddress) $($desiredInterval)" | Write-Host
}
Write-Host # new line
}
return 0
}
$_exitCode = main
Write-Host # new line
exit $_exitCode