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
|
/* $Id: VBoxDrvCfg.cpp $ */
/** @file
* VBoxDrvCfg.cpp - Windows Driver Manipulation API implementation
*/
/*
* Copyright (C) 2011-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <VBox/VBoxDrvCfg-win.h>
#include <iprt/win/setupapi.h>
#include <iprt/win/shlobj.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <stdio.h>
#include <Newdev.h>
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
static PFNVBOXDRVCFG_LOG g_pfnVBoxDrvCfgLog;
static void *g_pvVBoxDrvCfgLog;
static PFNVBOXDRVCFG_PANIC g_pfnVBoxDrvCfgPanic;
static void *g_pvVBoxDrvCfgPanic;
VBOXDRVCFG_DECL(void) VBoxDrvCfgLoggerSet(PFNVBOXDRVCFG_LOG pfnLog, void *pvLog)
{
g_pfnVBoxDrvCfgLog = pfnLog;
g_pvVBoxDrvCfgLog = pvLog;
}
VBOXDRVCFG_DECL(void) VBoxDrvCfgPanicSet(PFNVBOXDRVCFG_PANIC pfnPanic, void *pvPanic)
{
g_pfnVBoxDrvCfgPanic = pfnPanic;
g_pvVBoxDrvCfgPanic = pvPanic;
}
static void vboxDrvCfgLogRel(LPCSTR szString, ...)
{
PFNVBOXDRVCFG_LOG pfnLog = g_pfnVBoxDrvCfgLog;
void * pvLog = g_pvVBoxDrvCfgLog;
if (pfnLog)
{
char szBuffer[4096] = {0};
va_list pArgList;
va_start(pArgList, szString);
_vsnprintf(szBuffer, RT_ELEMENTS(szBuffer), szString, pArgList);
va_end(pArgList);
pfnLog(VBOXDRVCFG_LOG_SEVERITY_REL, szBuffer, pvLog);
}
}
static void vboxDrvCfgLogRegular(LPCSTR szString, ...)
{
PFNVBOXDRVCFG_LOG pfnLog = g_pfnVBoxDrvCfgLog;
void * pvLog = g_pvVBoxDrvCfgLog;
if (pfnLog)
{
char szBuffer[4096] = {0};
va_list pArgList;
va_start(pArgList, szString);
_vsnprintf(szBuffer, RT_ELEMENTS(szBuffer), szString, pArgList);
va_end(pArgList);
pfnLog(VBOXDRVCFG_LOG_SEVERITY_REGULAR, szBuffer, pvLog);
}
}
static void vboxDrvCfgLogFlow(LPCSTR szString, ...)
{
PFNVBOXDRVCFG_LOG pfnLog = g_pfnVBoxDrvCfgLog;
void * pvLog = g_pvVBoxDrvCfgLog;
if (pfnLog)
{
char szBuffer[4096] = {0};
va_list pArgList;
va_start(pArgList, szString);
_vsnprintf(szBuffer, RT_ELEMENTS(szBuffer), szString, pArgList);
va_end(pArgList);
pfnLog(VBOXDRVCFG_LOG_SEVERITY_FLOW, szBuffer, pvLog);
}
}
static void vboxDrvCfgPanic()
{
PFNVBOXDRVCFG_PANIC pfnPanic = g_pfnVBoxDrvCfgPanic;
void * pvPanic = g_pvVBoxDrvCfgPanic;
if (pfnPanic)
{
pfnPanic(pvPanic);
}
}
/* we do not use IPRT Logging because the lib is used in host installer and needs to
* post its msgs to MSI logger */
#define NonStandardLogCrap(_m) do { vboxDrvCfgLogRegular _m ; } while (0)
#define NonStandardLogFlowCrap(_m) do { vboxDrvCfgLogFlow _m ; } while (0)
#define NonStandardLogRelCrap(_m) do { vboxDrvCfgLogRel _m ; } while (0)
#define NonStandardAssertFailed() vboxDrvCfgPanic()
#define NonStandardAssert(_m) do { \
if (RT_UNLIKELY(!(_m))) { vboxDrvCfgPanic(); } \
} while (0)
class VBoxDrvCfgStringList
{
public:
VBoxDrvCfgStringList(int aSize);
~VBoxDrvCfgStringList();
HRESULT add(LPWSTR pStr);
int size() {return mSize;}
LPWSTR get(int i) {return maList[i];}
private:
HRESULT resize(int newSize);
LPWSTR *maList;
int mBufSize;
int mSize;
};
VBoxDrvCfgStringList::VBoxDrvCfgStringList(int aSize)
{
maList = (LPWSTR*)malloc( sizeof(maList[0]) * aSize);
mBufSize = aSize;
mSize = 0;
}
VBoxDrvCfgStringList::~VBoxDrvCfgStringList()
{
if (!mBufSize)
return;
for (int i = 0; i < mSize; ++i)
{
free(maList[i]);
}
free(maList);
}
HRESULT VBoxDrvCfgStringList::add(LPWSTR pStr)
{
if (mSize == mBufSize)
{
int hr = resize(mBufSize+10);
if (SUCCEEDED(hr))
return hr;
}
size_t cStr = wcslen(pStr) + 1;
LPWSTR str = (LPWSTR)malloc( sizeof(maList[0][0]) * cStr);
memcpy(str, pStr, sizeof(maList[0][0]) * cStr);
maList[mSize] = str;
++mSize;
return S_OK;
}
HRESULT VBoxDrvCfgStringList::resize(int newSize)
{
NonStandardAssert(newSize >= mSize);
if (newSize < mSize)
return E_FAIL;
LPWSTR* pOld = maList;
maList = (LPWSTR*)malloc( sizeof(maList[0]) * newSize);
mBufSize = newSize;
memcpy(maList, pOld, mSize*sizeof(maList[0]));
free(pOld);
return S_OK;
}
/*
* inf file manipulation API
*/
typedef bool (*PFNVBOXNETCFG_ENUMERATION_CALLBACK) (LPCWSTR lpszFileName, PVOID pContext);
typedef struct _INF_INFO
{
LPCWSTR lpszClassName;
LPCWSTR lpszPnPId;
} INF_INFO, *PINF_INFO;
typedef struct _INFENUM_CONTEXT
{
INF_INFO InfInfo;
DWORD Flags;
HRESULT hr;
} INFENUM_CONTEXT, *PINFENUM_CONTEXT;
static HRESULT vboxDrvCfgInfQueryContext(HINF hInf, LPCWSTR lpszSection, LPCWSTR lpszKey, PINFCONTEXT pCtx)
{
if (!SetupFindFirstLineW(hInf, lpszSection, lpszKey, pCtx))
{
DWORD dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__ ": SetupFindFirstLine failed WinEr (%d) for Section(%S), Key(%S)\n", dwErr, lpszSection, lpszKey));
return HRESULT_FROM_WIN32(dwErr);
}
return S_OK;
}
static HRESULT vboxDrvCfgInfQueryKeyValue(PINFCONTEXT pCtx, DWORD iValue, LPWSTR *lppszValue, PDWORD pcValue)
{
DWORD dwErr;
DWORD cValue;
if (!SetupGetStringFieldW(pCtx, iValue, NULL, 0, &cValue))
{
dwErr = GetLastError();
// NonStandardAssert(dwErr == ERROR_INSUFFICIENT_BUFFER);
if (dwErr != ERROR_INSUFFICIENT_BUFFER)
{
NonStandardLogFlowCrap((__FUNCTION__ ": SetupGetStringField failed WinEr (%d) for iValue(%d)\n", dwErr, iValue));
return HRESULT_FROM_WIN32(dwErr);
}
}
LPWSTR lpszValue = (LPWSTR)malloc(cValue * sizeof (lpszValue[0]));
NonStandardAssert(lpszValue);
if (!lpszValue)
{
NonStandardLogRelCrap((__FUNCTION__ ": SetCoTaskMemAlloc failed to alloc mem of size (%d), for iValue(%d)\n", cValue * sizeof (lpszValue[0]), iValue));
return E_FAIL;
}
if (!SetupGetStringFieldW(pCtx, iValue, lpszValue, cValue, &cValue))
{
dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__ ": SetupGetStringField failed WinEr (%d) for iValue(%d)\n", dwErr, iValue));
NonStandardAssert(0);
free(lpszValue);
return HRESULT_FROM_WIN32(dwErr);
}
*lppszValue = lpszValue;
if (pcValue)
*pcValue = cValue;
return S_OK;
}
#if defined(RT_ARCH_AMD64)
# define VBOXDRVCFG_ARCHSTR L"amd64"
#else
# define VBOXDRVCFG_ARCHSTR L"x86"
#endif
static HRESULT vboxDrvCfgInfQueryModelsSectionName(HINF hInf, LPWSTR *lppszValue, PDWORD pcValue)
{
INFCONTEXT InfCtx;
LPWSTR lpszModels, lpszPlatform = NULL, lpszPlatformCur;
LPWSTR lpszResult = NULL;
DWORD cModels, cPlatform = 0, cPlatformCur, cResult = 0;
bool bNt = false, bArch = false /*, bOs = false */;
HRESULT hr = vboxDrvCfgInfQueryContext(hInf, L"Manufacturer", NULL, &InfCtx);
if (hr != S_OK)
{
NonStandardLogCrap((__FUNCTION__ ": vboxDrvCfgInfQueryContext for Manufacturer failed, hr=0x%x\n", hr));
return hr;
}
hr = vboxDrvCfgInfQueryKeyValue(&InfCtx, 1, &lpszModels, &cModels);
if (hr != S_OK)
{
NonStandardLogRelCrap((__FUNCTION__ ": vboxDrvCfgRegQueryKeyValue 1 for Manufacturer failed, hr=0x%x\n", hr));
return hr;
}
for (DWORD i = 2; (hr = vboxDrvCfgInfQueryKeyValue(&InfCtx, i, &lpszPlatformCur, &cPlatformCur)) == S_OK; ++i)
{
if (wcsicmp(lpszPlatformCur, L"NT"VBOXDRVCFG_ARCHSTR))
{
if (bNt)
{
free(lpszPlatformCur);
lpszPlatformCur = NULL;
continue;
}
if (wcsicmp(lpszPlatformCur, L"NT"))
{
free(lpszPlatformCur);
lpszPlatformCur = NULL;
continue;
}
bNt = true;
}
else
{
bArch = true;
}
cPlatform = cPlatformCur;
if(lpszPlatform)
free(lpszPlatform);
lpszPlatform = lpszPlatformCur;
lpszPlatformCur = NULL;
}
hr = S_OK;
if (lpszPlatform)
{
lpszResult = (LPWSTR)malloc((cModels + cPlatform) * sizeof (lpszResult[0]));
if (lpszResult)
{
memcpy(lpszResult, lpszModels, (cModels - 1) * sizeof (lpszResult[0]));
*(lpszResult + cModels - 1) = L'.';
memcpy(lpszResult + cModels, lpszPlatform, cPlatform * sizeof (lpszResult[0]));
cResult = cModels + cPlatform;
}
else
{
hr = E_FAIL;
}
}
else
{
lpszResult = lpszModels;
cResult = cModels;
lpszModels = NULL;
}
if (lpszModels)
free(lpszModels);
if (lpszPlatform)
free(lpszPlatform);
if (hr == S_OK)
{
*lppszValue = lpszResult;
if (pcValue)
*pcValue = cResult;
}
return hr;
}
static HRESULT vboxDrvCfgInfQueryFirstPnPId(HINF hInf, LPWSTR *lppszPnPId)
{
*lppszPnPId = NULL;
LPWSTR lpszModels;
HRESULT hr = vboxDrvCfgInfQueryModelsSectionName(hInf, &lpszModels, NULL);
NonStandardLogRelCrap((__FUNCTION__ ": vboxDrvCfgInfQueryModelsSectionName returned lpszModels = (%S)", lpszModels));
if (hr != S_OK)
{
NonStandardLogCrap((__FUNCTION__ ": vboxDrvCfgRegQueryKeyValue for Manufacturer failed, hr=0x%x\n", hr));
return hr;
}
LPWSTR lpszPnPId = NULL;
INFCONTEXT InfCtx;
hr = vboxDrvCfgInfQueryContext(hInf, lpszModels, NULL, &InfCtx);
if (hr != S_OK)
{
NonStandardLogRelCrap((__FUNCTION__ ": vboxDrvCfgInfQueryContext for models (%S) failed, hr=0x%x\n", lpszModels, hr));
}
else
{
hr = vboxDrvCfgInfQueryKeyValue(&InfCtx, 2, &lpszPnPId, NULL);
NonStandardLogRelCrap((__FUNCTION__ ": vboxDrvCfgRegQueryKeyValue for models (%S) returned lpszPnPId (%S) \n", lpszModels, lpszPnPId));
if (hr != S_OK)
NonStandardLogRelCrap((__FUNCTION__ ": vboxDrvCfgRegQueryKeyValue for models (%S) failed, hr=0x%x\n", lpszModels, hr));
}
/* free models string right away */
free(lpszModels);
if (hr != S_OK)
return hr;
*lppszPnPId = lpszPnPId;
return S_OK;
}
static bool vboxDrvCfgInfEnumerationCallback(LPCWSTR lpszFileName, PVOID pCtxt);
#define VBOXDRVCFG_S_INFEXISTS (HRESULT_FROM_WIN32(ERROR_FILE_EXISTS))
static HRESULT vboxDrvCfgInfCopyEx(IN LPCWSTR lpszInfPath, IN DWORD fCopyStyle, OUT LPWSTR lpszDstName, IN DWORD cbDstName, OUT PDWORD pcbDstNameSize, OUT LPWSTR* lpszDstNameComponent)
{
WCHAR aMediaLocation[_MAX_DIR];
WCHAR aDir[_MAX_DIR];
_wsplitpath(lpszInfPath, aMediaLocation, aDir, NULL, NULL);
wcscat(aMediaLocation, aDir);
if (!SetupCopyOEMInfW(lpszInfPath, aMediaLocation, SPOST_PATH, fCopyStyle,
lpszDstName, cbDstName, pcbDstNameSize,
lpszDstNameComponent))
{
DWORD dwErr = GetLastError();
HRESULT hr = HRESULT_FROM_WIN32(dwErr);
if (fCopyStyle != SP_COPY_REPLACEONLY || hr != VBOXDRVCFG_S_INFEXISTS)
{
NonStandardLogRelCrap((__FUNCTION__ ": SetupCopyOEMInf fail dwErr=%ld\n", dwErr));
}
return hr;
}
return S_OK;
}
static HRESULT vboxDrvCfgInfCopy(IN LPCWSTR lpszInfPath)
{
return vboxDrvCfgInfCopyEx(lpszInfPath, 0, NULL, 0, NULL, NULL);
}
VBOXDRVCFG_DECL(HRESULT) VBoxDrvCfgInfInstall(IN LPCWSTR lpszInfPath)
{
return vboxDrvCfgInfCopy(lpszInfPath);
}
VBOXDRVCFG_DECL(HRESULT) VBoxDrvCfgInfUninstall(IN LPCWSTR lpszInfPath, DWORD fFlags)
{
WCHAR DstInfName[MAX_PATH];
DWORD cbDword = sizeof (DstInfName);
HRESULT hr = vboxDrvCfgInfCopyEx(lpszInfPath, SP_COPY_REPLACEONLY, DstInfName, cbDword, &cbDword, NULL);
if (hr == VBOXDRVCFG_S_INFEXISTS)
{
if (!SetupUninstallOEMInfW(DstInfName, fFlags, NULL /*__in PVOID Reserved == NULL */))
{
DWORD dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__ ": SetupUninstallOEMInf failed for file (%S), oem(%S), dwErr=%ld\n", lpszInfPath, DstInfName, dwErr));
NonStandardAssert(0);
return HRESULT_FROM_WIN32(dwErr);
}
}
return S_OK;
}
static HRESULT vboxDrvCfgCollectInfsSetupDi(const GUID * pGuid, LPCWSTR pPnPId, VBoxDrvCfgStringList & list)
{
DWORD dwErr = ERROR_SUCCESS;
HDEVINFO hDevInfo = SetupDiCreateDeviceInfoList(
pGuid, /* IN LPGUID ClassGuid, OPTIONAL */
NULL /*IN HWND hwndParent OPTIONAL */
);
if (hDevInfo != INVALID_HANDLE_VALUE)
{
if (SetupDiBuildDriverInfoList(hDevInfo,
NULL, /*IN OUT PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL*/
SPDIT_CLASSDRIVER /*IN DWORD DriverType*/
))
{
SP_DRVINFO_DATA DrvInfo;
DrvInfo.cbSize = sizeof(SP_DRVINFO_DATA);
char DetailBuf[16384];
PSP_DRVINFO_DETAIL_DATA pDrvDetail = (PSP_DRVINFO_DETAIL_DATA)DetailBuf;
for (DWORD i = 0; ; i++)
{
if (SetupDiEnumDriverInfo(hDevInfo,
NULL, /* IN PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL*/
SPDIT_CLASSDRIVER , /*IN DWORD DriverType,*/
i, /*IN DWORD MemberIndex,*/
&DrvInfo /*OUT PSP_DRVINFO_DATA DriverInfoData*/
))
{
DWORD dwReq;
pDrvDetail->cbSize = sizeof(SP_DRVINFO_DETAIL_DATA);
if (SetupDiGetDriverInfoDetail(
hDevInfo, /*IN HDEVINFO DeviceInfoSet,*/
NULL, /*IN PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL*/
&DrvInfo, /*IN PSP_DRVINFO_DATA DriverInfoData,*/
pDrvDetail, /*OUT PSP_DRVINFO_DETAIL_DATA DriverInfoDetailData, OPTIONAL*/
sizeof(DetailBuf), /*IN DWORD DriverInfoDetailDataSize,*/
&dwReq /*OUT PDWORD RequiredSize OPTIONAL*/
))
{
for (WCHAR * pHwId = pDrvDetail->HardwareID; pHwId && *pHwId && pHwId < (TCHAR*)(DetailBuf + sizeof(DetailBuf)/sizeof(DetailBuf[0])) ;pHwId += wcslen(pHwId) + 1)
{
if (!wcsicmp(pHwId, pPnPId))
{
NonStandardAssert(pDrvDetail->InfFileName[0]);
if (pDrvDetail->InfFileName)
{
list.add(pDrvDetail->InfFileName);
NonStandardLogRelCrap((__FUNCTION__": %S added to list", pDrvDetail->InfFileName));
}
}
}
}
else
{
DWORD dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__": SetupDiGetDriverInfoDetail fail dwErr=%ld, size(%d)", dwErr, dwReq));
// NonStandardAssert(0);
}
}
else
{
DWORD dwErr = GetLastError();
if (dwErr == ERROR_NO_MORE_ITEMS)
{
NonStandardLogRelCrap((__FUNCTION__": dwErr == ERROR_NO_MORE_ITEMS -> search was finished "));
break;
}
NonStandardAssert(0);
}
}
SetupDiDestroyDriverInfoList(hDevInfo,
NULL, /*IN PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL*/
SPDIT_CLASSDRIVER/*IN DWORD DriverType*/
);
}
else
{
dwErr = GetLastError();
NonStandardAssert(0);
}
SetupDiDestroyDeviceInfoList(hDevInfo);
}
else
{
dwErr = GetLastError();
NonStandardAssert(0);
}
return HRESULT_FROM_WIN32(dwErr);
}
#if 0
VBOXDRVCFG_DECL(HRESULT) VBoxDrvCfgInit()
{
int rc = RTR3InitDll(0);
if (rc != VINF_SUCCESS)
{
NonStandardLogRelCrap(("Could not init IPRT!, rc (%d)\n", rc));
return E_FAIL;
}
return S_OK;
}
VBOXDRVCFG_DECL(HRESULT) VBoxDrvCfgTerm()
{
return S_OK;
}
#endif
VBOXDRVCFG_DECL(HRESULT) VBoxDrvCfgInfUninstallAllSetupDi(IN const GUID * pGuidClass, IN LPCWSTR lpszClassName, IN LPCWSTR lpszPnPId, IN DWORD Flags)
{
VBoxDrvCfgStringList list(128);
HRESULT hr = vboxDrvCfgCollectInfsSetupDi(pGuidClass, lpszPnPId, list);
NonStandardLogRelCrap((__FUNCTION__": vboxDrvCfgCollectInfsSetupDi returned %d devices with PnPId %S and class name %S", list.size(), lpszPnPId, lpszClassName));
if (hr == S_OK)
{
INFENUM_CONTEXT Context;
Context.InfInfo.lpszClassName = lpszClassName;
Context.InfInfo.lpszPnPId = lpszPnPId;
Context.Flags = Flags;
Context.hr = S_OK;
int size = list.size();
for (int i = 0; i < size; ++i)
{
LPCWSTR pInf = list.get(i);
const WCHAR* pRel = wcsrchr(pInf, '\\');
if (pRel)
++pRel;
else
pRel = pInf;
vboxDrvCfgInfEnumerationCallback(pRel, &Context);
NonStandardLogRelCrap((__FUNCTION__": inf = %S\n", list.get(i)));
}
}
return hr;
}
static HRESULT vboxDrvCfgEnumFiles(LPCWSTR pPattern, PFNVBOXNETCFG_ENUMERATION_CALLBACK pfnCallback, PVOID pContext)
{
WIN32_FIND_DATA Data;
memset(&Data, 0, sizeof(Data));
HRESULT hr = S_OK;
HANDLE hEnum = FindFirstFile(pPattern,&Data);
if (hEnum != INVALID_HANDLE_VALUE)
{
do
{
if (!pfnCallback(Data.cFileName, pContext))
{
break;
}
/* next iteration */
memset(&Data, 0, sizeof(Data));
BOOL bNext = FindNextFile(hEnum,&Data);
if (!bNext)
{
DWORD dwErr = GetLastError();
if (dwErr != ERROR_NO_MORE_FILES)
{
NonStandardLogRelCrap((__FUNCTION__": FindNextFile fail dwErr=%ld\n", dwErr));
NonStandardAssert(0);
hr = HRESULT_FROM_WIN32(dwErr);
}
break;
}
}while (true);
FindClose(hEnum);
}
else
{
DWORD dwErr = GetLastError();
if (dwErr != ERROR_NO_MORE_FILES)
{
NonStandardLogRelCrap((__FUNCTION__": FindFirstFile fail dwErr=%ld\n", dwErr));
NonStandardAssert(0);
hr = HRESULT_FROM_WIN32(dwErr);
}
}
return hr;
}
static bool vboxDrvCfgInfEnumerationCallback(LPCWSTR lpszFileName, PVOID pCtxt)
{
PINFENUM_CONTEXT pContext = (PINFENUM_CONTEXT)pCtxt;
DWORD dwErr;
NonStandardLogRelCrap((__FUNCTION__": lpszFileName (%S)\n", lpszFileName));
NonStandardLogRelCrap((__FUNCTION__ ": pContext->InfInfo.lpszClassName = (%S)\n", pContext->InfInfo.lpszClassName));
HINF hInf = SetupOpenInfFileW(lpszFileName, pContext->InfInfo.lpszClassName, INF_STYLE_WIN4, NULL /*__in PUINT ErrorLine */);
if (hInf == INVALID_HANDLE_VALUE)
{
dwErr = GetLastError();
// NonStandardAssert(dwErr == ERROR_CLASS_MISMATCH);
if (dwErr != ERROR_CLASS_MISMATCH)
{
NonStandardLogCrap((__FUNCTION__ ": SetupOpenInfFileW err dwErr=%ld\n", dwErr));
}
else
{
NonStandardLogCrap((__FUNCTION__ ": dwErr == ERROR_CLASS_MISMATCH\n"));
}
return true;
}
LPWSTR lpszPnPId;
HRESULT hr = vboxDrvCfgInfQueryFirstPnPId(hInf, &lpszPnPId);
NonStandardLogRelCrap((__FUNCTION__ ": vboxDrvCfgInfQueryFirstPnPId returned lpszPnPId = (%S)\n", lpszPnPId));
NonStandardLogRelCrap((__FUNCTION__ ": pContext->InfInfo.lpszPnPId = (%S)\n", pContext->InfInfo.lpszPnPId));
if (hr == S_OK)
{
if (!wcsicmp(pContext->InfInfo.lpszPnPId, lpszPnPId))
{
if (!SetupUninstallOEMInfW(lpszFileName,
pContext->Flags, /*DWORD Flags could be SUOI_FORCEDELETE */
NULL /*__in PVOID Reserved == NULL */
))
{
dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__ ": SetupUninstallOEMInf failed for file (%S), dwErr=%ld\n", lpszFileName, dwErr));
NonStandardAssert(0);
hr = HRESULT_FROM_WIN32( dwErr );
}
}
free(lpszPnPId);
}
else
{
NonStandardLogCrap((__FUNCTION__ ": vboxDrvCfgInfQueryFirstPnPId failed, hr=0x%x\n", hr));
}
SetupCloseInfFile(hInf);
return true;
}
VBOXDRVCFG_DECL(HRESULT) VBoxDrvCfgInfUninstallAllF(LPCWSTR lpszClassName, LPCWSTR lpszPnPId, DWORD Flags)
{
static WCHAR const s_wszFilter[] = L"\\inf\\oem*.inf";
HRESULT hr;
WCHAR wszInfDirPath[MAX_PATH];
UINT cwcInput = RT_ELEMENTS(wszInfDirPath) - RT_ELEMENTS(s_wszFilter);
UINT cwcWindows = GetSystemWindowsDirectory(wszInfDirPath, cwcInput);
if (cwcWindows > 0 && cwcWindows < cwcInput)
{
wcscpy(&wszInfDirPath[cwcWindows], s_wszFilter);
INFENUM_CONTEXT Context;
Context.InfInfo.lpszClassName = lpszClassName;
Context.InfInfo.lpszPnPId = lpszPnPId;
Context.Flags = Flags;
Context.hr = S_OK;
NonStandardLogRelCrap((__FUNCTION__": Calling vboxDrvCfgEnumFiles(wszInfDirPath, vboxDrvCfgInfEnumerationCallback, &Context)"));
hr = vboxDrvCfgEnumFiles(wszInfDirPath, vboxDrvCfgInfEnumerationCallback, &Context);
NonStandardAssert(hr == S_OK);
if (hr == S_OK)
{
hr = Context.hr;
}
else
{
NonStandardLogRelCrap((__FUNCTION__": vboxDrvCfgEnumFiles failed, hr=0x%x\n", hr));
}
}
else
{
NonStandardLogRelCrap((__FUNCTION__": GetSystemWindowsDirectory failed, cwcWindows=%u lasterr=%u\n", cwcWindows, GetLastError()));
NonStandardAssertFailed();
hr = E_FAIL;
}
return hr;
}
/* time intervals in milliseconds */
/* max time to wait for the service to startup */
#define VBOXDRVCFG_SVC_WAITSTART_TIME 10000
/* sleep time before service status polls */
#define VBOXDRVCFG_SVC_WAITSTART_TIME_PERIOD 100
/* number of service start polls */
#define VBOXDRVCFG_SVC_WAITSTART_RETRIES (VBOXDRVCFG_SVC_WAITSTART_TIME/VBOXDRVCFG_SVC_WAITSTART_TIME_PERIOD)
VBOXDRVCFG_DECL(HRESULT) VBoxDrvCfgSvcStart(LPCWSTR lpszSvcName)
{
SC_HANDLE hMgr = OpenSCManager(NULL, NULL, SERVICE_QUERY_STATUS | SERVICE_START);
if (hMgr == NULL)
{
DWORD dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__": OpenSCManager failed, dwErr=%ld\n", dwErr));
return HRESULT_FROM_WIN32(dwErr);
}
HRESULT hr = S_OK;
SC_HANDLE hSvc = OpenServiceW(hMgr, lpszSvcName, SERVICE_QUERY_STATUS | SERVICE_START);
if (hSvc)
{
do
{
SERVICE_STATUS Status;
BOOL fRc = QueryServiceStatus(hSvc, &Status);
if (!fRc)
{
DWORD dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__": QueryServiceStatus failed dwErr=%ld\n", dwErr));
hr = HRESULT_FROM_WIN32(dwErr);
break;
}
if (Status.dwCurrentState != SERVICE_RUNNING && Status.dwCurrentState != SERVICE_START_PENDING)
{
NonStandardLogRelCrap(("Starting service (%S)\n", lpszSvcName));
fRc = StartService(hSvc, 0, NULL);
if (!fRc)
{
DWORD dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__": StartService failed dwErr=%ld\n", dwErr));
hr = HRESULT_FROM_WIN32(dwErr);
break;
}
}
fRc = QueryServiceStatus(hSvc, &Status);
if (!fRc)
{
DWORD dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__": QueryServiceStatus failed dwErr=%ld\n", dwErr));
hr = HRESULT_FROM_WIN32(dwErr);
break;
}
if (Status.dwCurrentState == SERVICE_START_PENDING)
{
for (int i = 0; i < VBOXDRVCFG_SVC_WAITSTART_RETRIES; ++i)
{
Sleep(VBOXDRVCFG_SVC_WAITSTART_TIME_PERIOD);
fRc = QueryServiceStatus(hSvc, &Status);
if (!fRc)
{
DWORD dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__": QueryServiceStatus failed dwErr=%ld\n", dwErr));
hr = HRESULT_FROM_WIN32(dwErr);
break;
}
else if (Status.dwCurrentState != SERVICE_START_PENDING)
break;
}
}
if (hr != S_OK || Status.dwCurrentState != SERVICE_RUNNING)
{
NonStandardLogRelCrap((__FUNCTION__": Failed to start the service\n"));
hr = E_FAIL;
break;
}
} while (0);
CloseServiceHandle(hSvc);
}
else
{
DWORD dwErr = GetLastError();
NonStandardLogRelCrap((__FUNCTION__": OpenServiceW failed, dwErr=%ld\n", dwErr));
hr = HRESULT_FROM_WIN32(dwErr);
}
CloseServiceHandle(hMgr);
return hr;
}
HRESULT VBoxDrvCfgDrvUpdate(LPCWSTR pcszwHwId, LPCWSTR pcsxwInf, BOOL *pbRebootRequired)
{
if (pbRebootRequired)
*pbRebootRequired = FALSE;
BOOL bRebootRequired = FALSE;
WCHAR InfFullPath[MAX_PATH];
DWORD dwChars = GetFullPathNameW(pcsxwInf,
sizeof (InfFullPath) / sizeof (InfFullPath[0]),
InfFullPath,
NULL /* LPTSTR *lpFilePart */
);
if (!dwChars || dwChars >= MAX_PATH)
{
NonStandardLogCrap(("GetFullPathNameW failed, dwErr=%ld, dwChars=%ld\n",
GetLastError(), dwChars));
return E_INVALIDARG;
}
if (!UpdateDriverForPlugAndPlayDevicesW(NULL, /* HWND hwndParent */
pcszwHwId,
InfFullPath,
INSTALLFLAG_FORCE,
&bRebootRequired))
{
DWORD dwErr = GetLastError();
NonStandardLogCrap(("UpdateDriverForPlugAndPlayDevicesW failed, dwErr=%ld\n",
dwErr));
return HRESULT_FROM_WIN32(dwErr);
}
if (bRebootRequired)
NonStandardLogCrap(("!!Driver Update: REBOOT REQUIRED!!\n", GetLastError(), dwChars));
if (pbRebootRequired)
*pbRebootRequired = bRebootRequired;
return S_OK;
}
|