forked from microsoft/Windows-driver-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.cpp
1327 lines (1101 loc) · 35.9 KB
/
dump.cpp
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
Module Name:
dump.cpp
Abstract:
Device Console
dump information out about a particular device
--*/
#include "devcon.h"
BOOL DumpDeviceWithInfo(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo, _In_opt_ LPCTSTR Info)
/*++
Routine Description:
Write device instance & info to stdout
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
none
--*/
{
TCHAR devID[MAX_DEVICE_ID_LEN];
BOOL b = TRUE;
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
devInfoListDetail.cbSize = sizeof(devInfoListDetail);
if((!SetupDiGetDeviceInfoListDetail(Devs,&devInfoListDetail)) ||
(CM_Get_Device_ID_Ex(DevInfo->DevInst,devID,MAX_DEVICE_ID_LEN,0,devInfoListDetail.RemoteMachineHandle)!=CR_SUCCESS)) {
StringCchCopy(devID, ARRAYSIZE(devID), TEXT("?"));
b = FALSE;
}
if(Info) {
_tprintf(TEXT("%-60s: %s\n"),devID,Info);
} else {
_tprintf(TEXT("%s\n"),devID);
}
return b;
}
BOOL DumpDevice(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++
Routine Description:
Write device instance & description to stdout
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
TRUE if success
--*/
{
LPTSTR desc;
BOOL b;
desc = GetDeviceDescription(Devs,DevInfo);
b = DumpDeviceWithInfo(Devs,DevInfo,desc);
if(desc) {
delete [] desc;
}
return b;
}
BOOL DumpDeviceDescr(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++
Routine Description:
Write device description to stdout
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
TRUE if success
--*/
{
LPTSTR desc;
desc = GetDeviceDescription(Devs,DevInfo);
if(!desc) {
return FALSE;
}
Padding(1);
FormatToStream(stdout,MSG_DUMP_DESCRIPTION,desc);
delete [] desc;
return TRUE;
}
BOOL DumpDeviceClass(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++
Routine Description:
Write device class information to stdout
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
TRUE if success
--*/
{
LPTSTR cls;
LPTSTR guid;
Padding(1);
cls = GetDeviceStringProperty(Devs,DevInfo,SPDRP_CLASS);
guid = GetDeviceStringProperty(Devs,DevInfo,SPDRP_CLASSGUID);
if(!cls && !guid) {
FormatToStream(stdout,
MSG_DUMP_NOSETUPCLASS
);
} else {
FormatToStream(stdout,
MSG_DUMP_SETUPCLASS,
guid ? guid : TEXT("{}"),
cls ? cls : TEXT("(?)")
);
}
if(cls) {
delete [] cls;
}
if(guid) {
delete [] guid;
}
return TRUE;
}
BOOL DumpDeviceStatus(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++
Routine Description:
Write device status to stdout
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
none
--*/
{
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
ULONG status = 0;
ULONG problem = 0;
BOOL hasInfo = FALSE;
BOOL isPhantom = FALSE;
CONFIGRET cr = CR_SUCCESS;
devInfoListDetail.cbSize = sizeof(devInfoListDetail);
if((!SetupDiGetDeviceInfoListDetail(Devs,&devInfoListDetail)) ||
((cr = CM_Get_DevNode_Status_Ex(&status,&problem,DevInfo->DevInst,0,devInfoListDetail.RemoteMachineHandle))!=CR_SUCCESS)) {
if ((cr == CR_NO_SUCH_DEVINST) || (cr == CR_NO_SUCH_VALUE)) {
isPhantom = TRUE;
} else {
Padding(1);
FormatToStream(stdout,MSG_DUMP_STATUS_ERROR);
return FALSE;
}
}
//
// handle off the status/problem codes
//
if (isPhantom) {
Padding(1);
FormatToStream(stdout,MSG_DUMP_PHANTOM);
return TRUE;
}
if((status & DN_HAS_PROBLEM) && problem == CM_PROB_DISABLED) {
hasInfo = TRUE;
Padding(1);
FormatToStream(stdout,MSG_DUMP_DISABLED);
return TRUE;
}
if(status & DN_HAS_PROBLEM) {
hasInfo = TRUE;
Padding(1);
FormatToStream(stdout,MSG_DUMP_PROBLEM,problem);
}
if(status & DN_PRIVATE_PROBLEM) {
hasInfo = TRUE;
Padding(1);
FormatToStream(stdout,MSG_DUMP_PRIVATE_PROBLEM);
}
if(status & DN_STARTED) {
Padding(1);
FormatToStream(stdout,MSG_DUMP_STARTED);
} else if (!hasInfo) {
Padding(1);
FormatToStream(stdout,MSG_DUMP_NOTSTARTED);
}
return TRUE;
}
BOOL DumpDeviceResourcesOfType(_In_ DEVINST DevInst, _In_ HMACHINE MachineHandle, _In_ LOG_CONF Config, _In_ RESOURCEID ReqResId)
{
RES_DES prevResDes = (RES_DES)Config;
RES_DES resDes = 0;
RESOURCEID resId = ReqResId;
ULONG dataSize;
PBYTE resDesData;
BOOL retval = FALSE;
UNREFERENCED_PARAMETER(DevInst);
while(CM_Get_Next_Res_Des_Ex(&resDes,prevResDes,ReqResId,&resId,0,MachineHandle)==CR_SUCCESS) {
if(prevResDes != Config) {
CM_Free_Res_Des_Handle(prevResDes);
}
prevResDes = resDes;
if(CM_Get_Res_Des_Data_Size_Ex(&dataSize,resDes,0,MachineHandle)!=CR_SUCCESS) {
continue;
}
resDesData = new BYTE[dataSize];
if(!resDesData) {
continue;
}
if(CM_Get_Res_Des_Data_Ex(resDes,resDesData,dataSize,0,MachineHandle)!=CR_SUCCESS) {
delete [] resDesData;
continue;
}
switch(resId) {
case ResType_Mem: {
PMEM_RESOURCE pMemData = (PMEM_RESOURCE)resDesData;
if(pMemData->MEM_Header.MD_Alloc_End-pMemData->MEM_Header.MD_Alloc_Base+1) {
Padding(2);
_tprintf(TEXT("MEM : %08I64x-%08I64x\n"),pMemData->MEM_Header.MD_Alloc_Base,pMemData->MEM_Header.MD_Alloc_End);
retval = TRUE;
}
break;
}
case ResType_IO: {
PIO_RESOURCE pIoData = (PIO_RESOURCE)resDesData;
if(pIoData->IO_Header.IOD_Alloc_End-pIoData->IO_Header.IOD_Alloc_Base+1) {
Padding(2);
_tprintf(TEXT("IO : %04I64x-%04I64x\n"),pIoData->IO_Header.IOD_Alloc_Base,pIoData->IO_Header.IOD_Alloc_End);
retval = TRUE;
}
break;
}
case ResType_DMA: {
PDMA_RESOURCE pDmaData = (PDMA_RESOURCE)resDesData;
Padding(2);
_tprintf(TEXT("DMA : %u\n"),pDmaData->DMA_Header.DD_Alloc_Chan);
retval = TRUE;
break;
}
case ResType_IRQ: {
PIRQ_RESOURCE pIrqData = (PIRQ_RESOURCE)resDesData;
Padding(2);
_tprintf(TEXT("IRQ : %u\n"),pIrqData->IRQ_Header.IRQD_Alloc_Num);
retval = TRUE;
break;
}
}
delete [] resDesData;
}
if(prevResDes != Config) {
CM_Free_Res_Des_Handle(prevResDes);
}
return retval;
}
BOOL DumpDeviceResources(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++
Routine Description:
Dump Resources to stdout
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
none
--*/
{
SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
ULONG status = 0;
ULONG problem = 0;
LOG_CONF config = 0;
BOOL haveConfig = FALSE;
//
// see what state the device is in
//
devInfoListDetail.cbSize = sizeof(devInfoListDetail);
if((!SetupDiGetDeviceInfoListDetail(Devs,&devInfoListDetail)) ||
(CM_Get_DevNode_Status_Ex(&status,&problem,DevInfo->DevInst,0,devInfoListDetail.RemoteMachineHandle)!=CR_SUCCESS)) {
return FALSE;
}
//
// see if the device is running and what resources it might be using
//
if(!(status & DN_HAS_PROBLEM)) {
//
// If this device is running, does this devinst have a ALLOC log config?
//
if (CM_Get_First_Log_Conf_Ex(&config,
DevInfo->DevInst,
ALLOC_LOG_CONF,
devInfoListDetail.RemoteMachineHandle) == CR_SUCCESS) {
haveConfig = TRUE;
}
}
if(!haveConfig) {
//
// If no config so far, does it have a FORCED log config?
// (note that technically these resources might be used by another device
// but is useful info to show)
//
if (CM_Get_First_Log_Conf_Ex(&config,
DevInfo->DevInst,
FORCED_LOG_CONF,
devInfoListDetail.RemoteMachineHandle) == CR_SUCCESS) {
haveConfig = TRUE;
}
}
if(!haveConfig) {
//
// if there's a hardware-disabled problem, boot-config isn't valid
// otherwise use this if we don't have anything else
//
if(!(status & DN_HAS_PROBLEM) || (problem != CM_PROB_HARDWARE_DISABLED)) {
//
// Does it have a BOOT log config?
//
if (CM_Get_First_Log_Conf_Ex(&config,
DevInfo->DevInst,
BOOT_LOG_CONF,
devInfoListDetail.RemoteMachineHandle) == CR_SUCCESS) {
haveConfig = TRUE;
}
}
}
if(!haveConfig) {
//
// if we don't have any configuration, display an apropriate message
//
Padding(1);
FormatToStream(stdout,(status & DN_STARTED) ? MSG_DUMP_NO_RESOURCES : MSG_DUMP_NO_RESERVED_RESOURCES );
return TRUE;
}
Padding(1);
FormatToStream(stdout,(status & DN_STARTED) ? MSG_DUMP_RESOURCES : MSG_DUMP_RESERVED_RESOURCES );
//
// dump resources
//
DumpDeviceResourcesOfType(DevInfo->DevInst,devInfoListDetail.RemoteMachineHandle,config,ResType_All);
//
// release handle
//
CM_Free_Log_Conf_Handle(config);
return TRUE;
}
UINT CALLBACK DumpDeviceDriversCallback(_In_ PVOID Context, _In_ UINT Notification, _In_ UINT_PTR Param1, _In_ UINT_PTR Param2)
/*++
Routine Description:
if Context provided, Simply count
otherwise dump files indented 2
Arguments:
Context - DWORD Count
Notification - SPFILENOTIFY_QUEUESCAN
Param1 - scan
Return Value:
none
--*/
{
LPDWORD count = (LPDWORD)Context;
LPTSTR file = (LPTSTR)Param1;
UNREFERENCED_PARAMETER(Notification);
UNREFERENCED_PARAMETER(Param2);
if(count) {
count[0]++;
} else {
Padding(2);
_tprintf(TEXT("%s\n"),file);
}
return NO_ERROR;
}
BOOL FindCurrentDriver(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo, _In_ PSP_DRVINFO_DATA DriverInfoData)
/*++
Routine Description:
Find the driver that is associated with the current device
We can do this either the quick way (available in WinXP)
or the long way that works in Win2k.
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
TRUE if we managed to determine and select current driver
--*/
{
SP_DEVINSTALL_PARAMS deviceInstallParams;
WCHAR SectionName[LINE_LEN];
WCHAR DrvDescription[LINE_LEN];
WCHAR MfgName[LINE_LEN];
WCHAR ProviderName[LINE_LEN];
HKEY hKey = NULL;
DWORD RegDataLength;
DWORD RegDataType;
DWORD c;
BOOL match = FALSE;
long regerr;
ZeroMemory(&deviceInstallParams, sizeof(deviceInstallParams));
deviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
if(!SetupDiGetDeviceInstallParams(Devs, DevInfo, &deviceInstallParams)) {
return FALSE;
}
#ifdef DI_FLAGSEX_INSTALLEDDRIVER
//
// Set the flags that tell SetupDiBuildDriverInfoList to just put the
// currently installed driver node in the list, and that it should allow
// excluded drivers. This flag introduced in WinXP.
//
deviceInstallParams.FlagsEx |= (DI_FLAGSEX_INSTALLEDDRIVER | DI_FLAGSEX_ALLOWEXCLUDEDDRVS);
if(SetupDiSetDeviceInstallParams(Devs, DevInfo, &deviceInstallParams)) {
//
// we were able to specify this flag, so proceed the easy way
// we should get a list of no more than 1 driver
//
if(!SetupDiBuildDriverInfoList(Devs, DevInfo, SPDIT_CLASSDRIVER)) {
return FALSE;
}
if (!SetupDiEnumDriverInfo(Devs, DevInfo, SPDIT_CLASSDRIVER,
0, DriverInfoData)) {
return FALSE;
}
//
// we've selected the current driver
//
return TRUE;
}
deviceInstallParams.FlagsEx &= ~(DI_FLAGSEX_INSTALLEDDRIVER | DI_FLAGSEX_ALLOWEXCLUDEDDRVS);
#endif
//
// The following method works in Win2k, but it's slow and painful.
//
// First, get driver key - if it doesn't exist, no driver
//
hKey = SetupDiOpenDevRegKey(Devs,
DevInfo,
DICS_FLAG_GLOBAL,
0,
DIREG_DRV,
KEY_READ
);
if(hKey == INVALID_HANDLE_VALUE) {
//
// no such value exists, so there can't be an associated driver
//
RegCloseKey(hKey);
return FALSE;
}
//
// obtain path of INF - we'll do a search on this specific INF
//
RegDataLength = sizeof(deviceInstallParams.DriverPath); // bytes!!!
regerr = RegQueryValueEx(hKey,
REGSTR_VAL_INFPATH,
NULL,
&RegDataType,
(PBYTE)deviceInstallParams.DriverPath,
&RegDataLength
);
if((regerr != ERROR_SUCCESS) || (RegDataType != REG_SZ)) {
//
// no such value exists, so no associated driver
//
RegCloseKey(hKey);
return FALSE;
}
//
// obtain name of Provider to fill into DriverInfoData
//
RegDataLength = sizeof(ProviderName); // bytes!!!
regerr = RegQueryValueEx(hKey,
REGSTR_VAL_PROVIDER_NAME,
NULL,
&RegDataType,
(PBYTE)ProviderName,
&RegDataLength
);
if((regerr != ERROR_SUCCESS) || (RegDataType != REG_SZ)) {
//
// no such value exists, so we don't have a valid associated driver
//
RegCloseKey(hKey);
return FALSE;
}
//
// obtain name of section - for final verification
//
RegDataLength = sizeof(SectionName); // bytes!!!
regerr = RegQueryValueEx(hKey,
REGSTR_VAL_INFSECTION,
NULL,
&RegDataType,
(PBYTE)SectionName,
&RegDataLength
);
if((regerr != ERROR_SUCCESS) || (RegDataType != REG_SZ)) {
//
// no such value exists, so we don't have a valid associated driver
//
RegCloseKey(hKey);
return FALSE;
}
//
// driver description (need not be same as device description)
// - for final verification
//
RegDataLength = sizeof(DrvDescription); // bytes!!!
regerr = RegQueryValueEx(hKey,
REGSTR_VAL_DRVDESC,
NULL,
&RegDataType,
(PBYTE)DrvDescription,
&RegDataLength
);
RegCloseKey(hKey);
if((regerr != ERROR_SUCCESS) || (RegDataType != REG_SZ)) {
//
// no such value exists, so we don't have a valid associated driver
//
return FALSE;
}
//
// Manufacturer (via SPDRP_MFG, don't access registry directly!)
//
if(!SetupDiGetDeviceRegistryProperty(Devs,
DevInfo,
SPDRP_MFG,
NULL, // datatype is guaranteed to always be REG_SZ.
(PBYTE)MfgName,
sizeof(MfgName), // bytes!!!
NULL)) {
//
// no such value exists, so we don't have a valid associated driver
//
return FALSE;
}
//
// now search for drivers listed in the INF
//
//
deviceInstallParams.Flags |= DI_ENUMSINGLEINF;
deviceInstallParams.FlagsEx |= DI_FLAGSEX_ALLOWEXCLUDEDDRVS;
if(!SetupDiSetDeviceInstallParams(Devs, DevInfo, &deviceInstallParams)) {
return FALSE;
}
if(!SetupDiBuildDriverInfoList(Devs, DevInfo, SPDIT_CLASSDRIVER)) {
return FALSE;
}
//
// find the entry in the INF that was used to install the driver for
// this device
//
for(c=0;SetupDiEnumDriverInfo(Devs,DevInfo,SPDIT_CLASSDRIVER,c,DriverInfoData);c++) {
if((_tcscmp(DriverInfoData->MfgName,MfgName)==0)
&&(_tcscmp(DriverInfoData->ProviderName,ProviderName)==0)) {
//
// these two fields match, try more detailed info
// to ensure we have the exact driver entry used
//
SP_DRVINFO_DETAIL_DATA detail;
detail.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
if(!SetupDiGetDriverInfoDetail(Devs,DevInfo,DriverInfoData,&detail,sizeof(detail),NULL)
&& (GetLastError() != ERROR_INSUFFICIENT_BUFFER)) {
continue;
}
if((_tcscmp(detail.SectionName,SectionName)==0) &&
(_tcscmp(detail.DrvDescription,DrvDescription)==0)) {
match = TRUE;
break;
}
}
}
if(!match) {
SetupDiDestroyDriverInfoList(Devs,DevInfo,SPDIT_CLASSDRIVER);
}
return match;
}
BOOL DumpDeviceDriverFiles(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++
Routine Description:
Dump information about what files were installed for driver package
<tab>Installed using OEM123.INF section [abc.NT]
<tab><tab>file...
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
none
--*/
{
//
// do this by 'searching' for the current driver
// mimmicing a copy-only install to our own file queue
// and then parsing that file queue
//
SP_DEVINSTALL_PARAMS deviceInstallParams;
SP_DRVINFO_DATA driverInfoData;
SP_DRVINFO_DETAIL_DATA driverInfoDetail;
HSPFILEQ queueHandle = INVALID_HANDLE_VALUE;
DWORD count;
DWORD scanResult;
BOOL success = FALSE;
ZeroMemory(&driverInfoData,sizeof(driverInfoData));
driverInfoData.cbSize = sizeof(driverInfoData);
if(!FindCurrentDriver(Devs,DevInfo,&driverInfoData)) {
Padding(1);
FormatToStream(stdout, MSG_DUMP_NO_DRIVER);
return FALSE;
}
//
// get useful driver information
//
driverInfoDetail.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
if(!SetupDiGetDriverInfoDetail(Devs,DevInfo,&driverInfoData,&driverInfoDetail,sizeof(SP_DRVINFO_DETAIL_DATA),NULL) &&
GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
//
// no information about driver or section
//
goto final;
}
if(!driverInfoDetail.InfFileName[0] || !driverInfoDetail.SectionName[0]) {
goto final;
}
//
// pretend to do the file-copy part of a driver install
// to determine what files are used
// the specified driver must be selected as the active driver
//
if(!SetupDiSetSelectedDriver(Devs, DevInfo, &driverInfoData)) {
goto final;
}
//
// create a file queue so we can look at this queue later
//
queueHandle = SetupOpenFileQueue();
if ( queueHandle == (HSPFILEQ)INVALID_HANDLE_VALUE ) {
goto final;
}
//
// modify flags to indicate we're providing our own queue
//
ZeroMemory(&deviceInstallParams, sizeof(deviceInstallParams));
deviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
if ( !SetupDiGetDeviceInstallParams(Devs, DevInfo, &deviceInstallParams) ) {
goto final;
}
//
// we want to add the files to the file queue, not install them!
//
deviceInstallParams.FileQueue = queueHandle;
deviceInstallParams.Flags |= DI_NOVCP;
if ( !SetupDiSetDeviceInstallParams(Devs, DevInfo, &deviceInstallParams) ) {
goto final;
}
//
// now fill queue with files that are to be installed
// this involves all class/co-installers
//
if ( !SetupDiCallClassInstaller(DIF_INSTALLDEVICEFILES, Devs, DevInfo) ) {
goto final;
}
//
// we now have a list of delete/rename/copy files
// iterate the copy queue twice - 1st time to get # of files
// 2nd time to get files
// (WinXP has API to get # of files, but we want this to work
// on Win2k too)
//
count = 0;
scanResult = 0;
//
// call once to count
//
SetupScanFileQueue(queueHandle,SPQ_SCAN_USE_CALLBACK,NULL,DumpDeviceDriversCallback,&count,&scanResult);
Padding(1);
FormatToStream(stdout, count ? MSG_DUMP_DRIVER_FILES : MSG_DUMP_NO_DRIVER_FILES, count, driverInfoDetail.InfFileName, driverInfoDetail.SectionName);
//
// call again to dump the files
//
SetupScanFileQueue(queueHandle,SPQ_SCAN_USE_CALLBACK,NULL,DumpDeviceDriversCallback,NULL,&scanResult);
success = TRUE;
final:
SetupDiDestroyDriverInfoList(Devs,DevInfo,SPDIT_CLASSDRIVER);
if ( queueHandle != (HSPFILEQ)INVALID_HANDLE_VALUE ) {
SetupCloseFileQueue(queueHandle);
}
if(!success) {
Padding(1);
FormatToStream(stdout, MSG_DUMP_NO_DRIVER);
}
return success;
}
BOOL DumpArray(_In_ int pad, _In_ PZPWSTR Array)
/*++
Routine Description:
Iterate array and dump entries to screen
Arguments:
pad - padding
Array - array to dump
Return Value:
none
--*/
{
if(!Array || !Array[0]) {
return FALSE;
}
while(Array[0]) {
Padding(pad);
_tprintf(TEXT("%s\n"),Array[0]);
Array++;
}
return TRUE;
}
BOOL DumpDeviceHwIds(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++
Routine Description:
Write device instance & description to stdout
<tab>Hardware ID's
<tab><tab>ID
...
<tab>Compatible ID's
<tab><tab>ID
...
or
<tab>No Hardware ID's for device
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
none
--*/
{
LPTSTR * hwIdArray = GetDevMultiSz(Devs,DevInfo,SPDRP_HARDWAREID);
LPTSTR * compatIdArray = GetDevMultiSz(Devs,DevInfo,SPDRP_COMPATIBLEIDS);
BOOL displayed = FALSE;
if(hwIdArray && hwIdArray[0]) {
displayed = TRUE;
Padding(1);
FormatToStream(stdout, MSG_DUMP_HWIDS);
DumpArray(2,hwIdArray);
}
if(compatIdArray && compatIdArray[0]) {
displayed = TRUE;
Padding(1);
FormatToStream(stdout, MSG_DUMP_COMPATIDS);
DumpArray(2,compatIdArray);
}
if(!displayed) {
Padding(1);
FormatToStream(stdout, MSG_DUMP_NO_HWIDS);
}
DelMultiSz(hwIdArray);
DelMultiSz(compatIdArray);
return TRUE;
}
BOOL DumpDeviceDriverNodes(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++
Routine Description:
Write device instance & description to stdout
<tab>Installed using OEM123.INF section [abc.NT]
<tab><tab>file...
Arguments:
Devs )_ uniquely identify device
DevInfo )
Return Value:
none
--*/
{
BOOL success = FALSE;
SP_DEVINSTALL_PARAMS deviceInstallParams;
SP_DRVINFO_DATA driverInfoData;
SP_DRVINFO_DETAIL_DATA driverInfoDetail;
SP_DRVINSTALL_PARAMS driverInstallParams;
DWORD index;
SYSTEMTIME SystemTime;
ULARGE_INTEGER Version;
TCHAR Buffer[MAX_PATH];
ZeroMemory(&deviceInstallParams, sizeof(deviceInstallParams));
ZeroMemory(&driverInfoData, sizeof(driverInfoData));
driverInfoData.cbSize = sizeof(SP_DRVINFO_DATA);
deviceInstallParams.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
if(!SetupDiGetDeviceInstallParams(Devs, DevInfo, &deviceInstallParams)) {
return FALSE;
}
//
// Set the flags that tell SetupDiBuildDriverInfoList to allow excluded drivers.
//
deviceInstallParams.FlagsEx |= DI_FLAGSEX_ALLOWEXCLUDEDDRVS;
if(!SetupDiSetDeviceInstallParams(Devs, DevInfo, &deviceInstallParams)) {
return FALSE;
}
//
// Now build a class driver list.
//
if(!SetupDiBuildDriverInfoList(Devs, DevInfo, SPDIT_COMPATDRIVER)) {
goto final2;
}
//
// Enumerate all of the drivernodes.
//
index = 0;
while(SetupDiEnumDriverInfo(Devs, DevInfo, SPDIT_COMPATDRIVER,
index, &driverInfoData)) {
success = TRUE;
FormatToStream(stdout,MSG_DUMP_DRIVERNODE_HEADER,index);
//
// get useful driver information
//
driverInfoDetail.cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
if(SetupDiGetDriverInfoDetail(Devs,DevInfo,&driverInfoData,&driverInfoDetail,sizeof(SP_DRVINFO_DETAIL_DATA),NULL) ||
GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
Padding(1);
FormatToStream(stdout,MSG_DUMP_DRIVERNODE_INF,driverInfoDetail.InfFileName);
Padding(1);
FormatToStream(stdout,MSG_DUMP_DRIVERNODE_SECTION,driverInfoDetail.SectionName);
}
Padding(1);
FormatToStream(stdout,MSG_DUMP_DRIVERNODE_DESCRIPTION,driverInfoData.Description);
Padding(1);
FormatToStream(stdout,MSG_DUMP_DRIVERNODE_MFGNAME,driverInfoData.MfgName);
Padding(1);
FormatToStream(stdout,MSG_DUMP_DRIVERNODE_PROVIDERNAME,driverInfoData.ProviderName);
if (FileTimeToSystemTime(&driverInfoData.DriverDate, &SystemTime)) {
if (GetDateFormat(LOCALE_USER_DEFAULT,
DATE_SHORTDATE,
&SystemTime,
NULL,
Buffer,
sizeof(Buffer)/sizeof(TCHAR)
) != 0) {
Padding(1);
FormatToStream(stdout,MSG_DUMP_DRIVERNODE_DRIVERDATE,Buffer);
}
}
Version.QuadPart = driverInfoData.DriverVersion;