-
Notifications
You must be signed in to change notification settings - Fork 10
/
reg.go
143 lines (119 loc) · 3.43 KB
/
reg.go
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
package main
import (
"log"
"strings"
"golang.org/x/sys/windows/registry"
)
func clen(n []byte) int {
for i := len(n) - 1; i >= 0; i-- {
if n[i] != 0 {
return i + 1
}
}
return len(n)
}
func GetStringValue(key registry.Key, name string) string {
value, _, err := key.GetStringValue(name)
if err != nil {
return ""
}
return value
}
func GetBinaryValue(key registry.Key, name string) []byte {
value, _, err := key.GetBinaryValue(name)
if err != nil {
return []byte{}
}
return value
}
func GetDWORDuint32Value(key registry.Key, name string) uint32 {
buf := make([]byte, 4)
key.GetValue(name, buf)
return btoi32(buf)
}
func setMSIMode(item *Device) {
var k registry.Key
var err error
if item.MsiSupported == 1 {
k, _, err = registry.CreateKey(item.reg, `Interrupt Management\MessageSignaledInterruptProperties`, registry.ALL_ACCESS)
if err != nil {
log.Println(err)
}
if err := k.SetDWordValue("MSISupported", 1); err != nil {
log.Println(err)
}
if item.MessageNumberLimit != 0 {
if err := k.SetDWordValue("MessageNumberLimit", uint32(item.MessageNumberLimit)); err != nil {
log.Println(err)
}
}
} else {
k, err = registry.OpenKey(item.reg, `Interrupt Management\MessageSignaledInterruptProperties`, registry.ALL_ACCESS)
if err != nil {
log.Println(err)
}
if err := registry.DeleteKey(item.reg, `Interrupt Management\MessageSignaledInterruptProperties`); err != nil {
log.Println(err)
}
}
if err := k.Close(); err != nil {
log.Println(err)
}
}
func setAffinityPolicy(item *Device) {
var k registry.Key
var err error
if item.DevicePolicy == 0 && item.DevicePriority == 0 {
k, err = registry.OpenKey(item.reg, `Interrupt Management\Affinity Policy`, registry.ALL_ACCESS)
if err != nil {
log.Println(err)
}
if err := registry.DeleteKey(item.reg, `Interrupt Management\Affinity Policy`); err != nil {
log.Println(err)
}
} else {
k, _, err = registry.CreateKey(item.reg, `Interrupt Management\Affinity Policy`, registry.ALL_ACCESS)
if err != nil {
log.Println(err)
}
if err := k.SetDWordValue("DevicePolicy", item.DevicePolicy); err != nil {
log.Println(err)
}
if item.DevicePolicy != 4 {
k.DeleteValue("AssignmentSetOverride")
}
if item.DevicePriority == 0 {
k.DeleteValue("DevicePriority")
} else if err := k.SetDWordValue("DevicePriority", item.DevicePriority); err != nil {
log.Println(err)
}
AssignmentSetOverrideByte := i64tob(uint64(item.AssignmentSetOverride))
if err := k.SetBinaryValue("AssignmentSetOverride", AssignmentSetOverrideByte[:clen(AssignmentSetOverrideByte)]); err != nil {
log.Println(err)
}
}
if err := k.Close(); err != nil {
log.Println(err)
}
}
// \REGISTRY\MACHINE\
func replaceRegistryMachine(regPath string) string {
indexMACHINE := strings.Index(regPath, "\\REGISTRY\\MACHINE\\")
if indexMACHINE == -1 {
log.Println("not Found")
return ""
}
return regPath[indexMACHINE+len("\\REGISTRY\\MACHINE\\"):]
}
// replaces ControlSet00X with CurrentControlSet
func generalizeControlSet(regPath string) string {
// https://learn.microsoft.com/en-us/windows-hardware/drivers/install/hklm-system-currentcontrolset-control-registry-tree
regPathArray := strings.Split(regPath, "\\")
for i := 0; i < len(regPathArray); i++ {
if strings.HasPrefix(regPathArray[i], "ControlSet00") {
regPathArray[i] = "CurrentControlSet"
return strings.Join(regPathArray, "\\")
}
}
return strings.Join(regPathArray, "\\")
}