-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.go
More file actions
411 lines (336 loc) · 11.6 KB
/
driver.go
File metadata and controls
411 lines (336 loc) · 11.6 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package devcon
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var (
reDriverFilePath = regexp.MustCompile(`C:\\(.*)`)
reDriverInstalled = regexp.MustCompile(`Driver installed from (?P<INFFile>.*) \[(?P<INFSection>.*)].*`)
reDriverNode = regexp.MustCompile(`DriverNode #(.*):`)
reDriverNoInfo = regexp.MustCompile(`No driver information`)
reFieldAreValue = regexp.MustCompile(`(?P<Field>.*) are (?P<Value>.*)`)
reFieldIsValue = regexp.MustCompile(`(?P<Field>.*) is (?P<Value>.*)`)
reHash = regexp.MustCompile(`#`)
)
// DriverFileGroup describes the INF file and section, as well as associated
// files for a device.
type DriverFileGroup struct {
// Device is the device with which the files are associated.
Device Device `json:"device"`
// INFFile is the driver file which is used by the device.
INFFile string `json:"infFile"`
// INFSection is the corresponding section of the INF file.
// TODO: This is a bad description.
INFSection string `json:"infSection"`
// Files are the driver files associated with the device driver.
Files []string `json:"files"`
}
// DriverNode describes the components of a driver package.
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/install/components-of-a-driver-package
// for more information about driver packages.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/install/how-setup-ranks-drivers--windows-vista-and-later-
// for more information about node rank.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/device-node-status-flags
// for more information about node flags.
type DriverNode struct {
// NodeNumber represents the node order for the device driver.
NodeNumber int `json:"nodeNumber"`
// INFFile is the fully qualified path to the INF file.
INFFile string `json:"infFile"`
// INFSection is the section of the INF file to which this device
// corresponds.
INFSection string `json:"infSection"`
// Description is the description of the device.
Description string `json:"description"`
// Manufacturer is the name of the device manufacturer.
Manufacturer string `json:"manufacturer"`
// Provider is the name of the driver provider (e.g. Microsoft).
Provider string `json:"provider"`
// Date is the date associated with the current driver version.
Date string `json:"date"`
// Version is the current version of the driver.
Version string `json:"version"`
// NodeRank indicates how well the driver matches the device. A driver rank
// is represented by an integer that is equal to or greater than zero. The
// lower the rank, the better a match the driver is for the device.
NodeRank int `json:"nodeRank"`
// NodeFlags describe the status of a device.
NodeFlags int `json:"nodeFlags"`
// IsDigitallySigned indicates that the driver has been digitally signed.
IsDigitallySigned bool `json:"isDigitallySigned"`
}
// DriverNodeGroup contains device details as well as details for the corresponding
// driver nodes.
type DriverNodeGroup struct {
// Device is the device with which the nodes are associated.
Device Device `json:"device"`
// Nodes are DriverNode records that describe the nodes associated with the
// device driver.
Nodes []DriverNode `json:"nodes"`
}
// DriverFiles returns the full path and file name of installed INF files and
// device driver files for the specified devices.
//
// Cannot be run with the WithRemoteComputer() option.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-driverfiles for more information.
func (dc *DevCon) DriverFiles(idOrClass string, idsOrClasses ...string) ([]DriverFileGroup, error) {
allIdsOrClasses := concatIdsOrClasses(idOrClass, idsOrClasses...)
lines, err := dc.run(commandDriverFiles, allIdsOrClasses...)
if err != nil {
return nil, err
}
groupIndices := make([]int, 0)
for index, line := range lines {
if !strings.HasPrefix(line, " ") {
groupIndices = append(groupIndices, index)
}
}
groupIndices = append(groupIndices, len(lines))
fileGroups := make([]DriverFileGroup, 0)
for index, groupStart := range groupIndices {
nextIndex := index + 1
if len(groupIndices) == nextIndex {
break
}
groupEnd := groupIndices[nextIndex]
fileGroup := DriverFileGroup{
Device: Device{
ID: "",
Name: "",
},
INFFile: "",
INFSection: "",
Files: make([]string, 0),
}
for lineIndex := groupStart; lineIndex < groupEnd; lineIndex++ {
line := lines[lineIndex]
switch {
case lineIndex == groupStart:
fileGroup.Device.ID = line
case lineIndex == groupStart+1:
nameParams := parseParams(reName, line)
if name, ok := nameParams["Name"]; ok {
fileGroup.Device.Name = name
}
case reDriverNoInfo.MatchString(line):
continue
default:
driverParams := parseParams(reDriverInstalled, line)
if infPath, ok := driverParams["INFFile"]; ok && infPath != "" {
fileGroup.INFFile = infPath
}
if infName, ok := driverParams["INFSection"]; ok && infName != "" {
fileGroup.INFSection = infName
}
fileResult := reDriverFilePath.FindStringSubmatch(line)
if fileResult != nil {
fileGroup.Files = append(fileGroup.Files, fileResult[0])
}
}
}
if fileGroup.Device.Name != "" {
fileGroups = append(fileGroups, fileGroup)
}
}
return fileGroups, nil
}
// DriverNodes returns all driver packages that are compatible with the device,
// along with their version and ranking.
//
// The DriverNodes method is particularly useful for troubleshooting setup
// problems. For example, you can use it to determine whether a Windows INF
// file or a customized third-party INF file was used for a device.
//
// Cannot be run with the WithRemoteComputer() option.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-drivernodes for more information.
//
//nolint:funlen // Function is long, but simple.
func (dc *DevCon) DriverNodes(idOrClass string, idsOrClasses ...string) ([]DriverNodeGroup, error) {
allIdsOrClasses := concatIdsOrClasses(idOrClass, idsOrClasses...)
lines, err := dc.run(commandDriverNodes, allIdsOrClasses...)
if err != nil {
return nil, err
}
groupIndices := make([]int, 0)
for index, line := range lines {
if !strings.HasPrefix(line, " ") && !reHash.MatchString(line) {
groupIndices = append(groupIndices, index)
}
}
groupIndices = append(groupIndices, len(lines))
nodeGroups := make([]DriverNodeGroup, 0)
for index, groupStart := range groupIndices {
nextIndex := index + 1
if len(groupIndices) == nextIndex {
break
}
groupEnd := groupIndices[nextIndex]
nodeGroup := DriverNodeGroup{
Device: Device{
ID: "",
Name: "",
},
Nodes: make([]DriverNode, 0),
}
node := DriverNode{
NodeNumber: 0,
INFFile: "",
INFSection: "",
Description: "",
Manufacturer: "",
Provider: "",
Date: "",
Version: "",
NodeRank: 0,
NodeFlags: 0,
IsDigitallySigned: false,
}
for lineIndex := groupStart; lineIndex < groupEnd; lineIndex++ {
line := lines[lineIndex]
switch {
case lineIndex == groupStart:
nodeGroup.Device.ID = line
case lineIndex == groupStart+1:
nameParams := parseParams(reName, line)
if name, ok := nameParams["Name"]; ok {
nodeGroup.Device.Name = name
}
case reHash.MatchString(line):
matches := reDriverNode.FindStringSubmatch(line)
if matches != nil {
number, _ := strconv.Atoi(matches[1])
node.NodeNumber = number
}
case strings.Contains(line, "No DriverNodes"):
continue
default:
params := parseParams(reFieldIsValue, line)
if field, ok := params["Field"]; ok {
value, ok := params["Value"]
if !ok {
continue
}
switch trimSpaces(field) {
case "Inf file":
node.INFFile = value
case "Inf section":
node.INFSection = value
case "Driver description":
node.Description = value
case "Manufacturer name":
node.Manufacturer = value
case "Provider name":
node.Provider = value
case "Driver date":
node.Date = value
case "Driver version":
node.Version = value
case "Driver node rank":
number, _ := strconv.Atoi(value)
node.NodeRank = number
}
if value == "digitally signed" {
node.IsDigitallySigned = true
nodeGroup.Nodes = append(nodeGroup.Nodes, node)
node = DriverNode{
NodeNumber: 0,
INFFile: "",
INFSection: "",
Description: "",
Manufacturer: "",
Provider: "",
Date: "",
Version: "",
NodeRank: 0,
NodeFlags: 0,
IsDigitallySigned: false,
}
}
}
params = parseParams(reFieldAreValue, line)
if value, ok := params["Value"]; ok {
number, _ := strconv.Atoi(value)
node.NodeFlags = number
}
}
if lineIndex == groupEnd-1 && nodeGroup.Device.Name != "" {
nodeGroups = append(nodeGroups, nodeGroup)
}
}
}
return nodeGroups, nil
}
// Update forcibly replaces the current device drivers for a specified device
// with drivers listed in the specified INF file.
//
// Update forces an update to the most appropriate drivers in the specified INF
// file, even if those drivers are older or less appropriate than the current drivers
// or the drivers in a different INF file.
//
// You cannot use Update to update drivers for non-present devices.
//
// Before updating the driver for any device, determine which devices will
// be affected. To do so, pass the name to the HwIDs() function:
//
// dc.HwIDs("ISAPNP\CSC4324\0")
//
// Or with the DriverFiles() function:
//
// dc.DriverFiles("ISAPNP\CSC4324\0")
//
// The system might need to be rebooted to make this change effective. To reboot
// the system if required, use:
//
// dc.WithConditionalReboot().Update()
//
// Cannot be run with the WithRemoteComputer() option.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-update for more information.
func (dc *DevCon) Update(infFile string, hardwareID string) error {
lines, err := dc.run(commandUpdate, infFile, hardwareID)
if substrInLines(lines, "success") == -1 {
return fmt.Errorf("error updating driver: %s", strings.Join(lines, ". "))
}
return err
}
// UpdateNI forcibly replaces the current device drivers with drivers listed in
// the specified INF file without prompting the user for information or
// confirmation.
//
// This method suppresses all user prompts that require a response and assumes
// the default response. As a result, you cannot use this operation to install
// unsigned drivers. To display user prompts during an update, use Update().
//
// This method forces an update, even if the drivers in the specified INF file
// are older or less appropriate than the current drivers.
//
// Before updating the driver for any device, determine which devices will
// be affected. To do so, pass the name to the HwIDs() function:
//
// dc.HwIDs("ISAPNP\CSC4324\0")
//
// Or with the DriverFiles() function:
//
// dc.DriverFiles("ISAPNP\CSC4324\0")
//
// The system might need to be rebooted to make this change effective. To reboot
// the system if required, use:
//
// dc.WithConditionalReboot().UpdateNI()
//
// Cannot be run with the WithRemoteComputer() option.
//
// See https://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/devcon-updateni for more information.
func (dc *DevCon) UpdateNI(infFile string, hardwareID string) error {
lines, err := dc.run(commandUpdateNI, infFile, hardwareID)
if substrInLines(lines, "success") == -1 {
return fmt.Errorf("error updating driver: %s", strings.Join(lines, ". "))
}
return err
}