summaryrefslogtreecommitdiff
path: root/storage/class/classpnp/src/classwmi.c
blob: e21ec3562ab5c83b89b9583b17294ab0ec652b49 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
/*++

Copyright (C) Microsoft Corporation, 1991 - 1999

Module Name:

    classwmi.c

Abstract:

    SCSI class driver routines

Environment:

    kernel mode only

Notes:


Revision History:

--*/

#include "stddef.h"
#include "ntddk.h"
#include "scsi.h"

#include "classpnp.h"

#include "mountdev.h"

#include <stdarg.h>

#include "classp.h"
#include <wmistr.h>
#include <wmidata.h>
#include <classlog.h>

#ifdef DEBUG_USE_WPP
#include "classwmi.tmh"
#endif

#define TIME_STRING_LENGTH      25

BOOLEAN
ClassFindGuid(
    PGUIDREGINFO GuidList,
    ULONG GuidCount,
    LPGUID Guid,
    PULONG GuidIndex
    );

NTSTATUS
ClassQueryInternalDataBlock(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN ULONG GuidIndex,
    IN ULONG BufferAvail,
    OUT PUCHAR Buffer
    );

PWCHAR
ConvertTickToDateTime(
    IN LARGE_INTEGER Tick,
    _Out_writes_(TIME_STRING_LENGTH) PWCHAR String
    );

BOOLEAN
ClassFindInternalGuid(
    LPGUID Guid,
    PULONG GuidIndex
    );


//
// This is the name for the MOF resource that must be part of all drivers that
// register via this interface.
#define MOFRESOURCENAME L"MofResourceName"

//
// What can be paged ???
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, ClassSystemControl)
#pragma alloc_text(PAGE, ClassFindGuid)
#pragma alloc_text(PAGE, ClassFindInternalGuid)
#endif

//
// Define WMI interface to all class drivers
//
GUIDREGINFO wmiClassGuids[] =
{
    {
        MSStorageDriver_ClassErrorLogGuid, 1, 0
    }
};

#define MSStorageDriver_ClassErrorLogGuid_Index     0
#define NUM_CLASS_WMI_GUIDS     (sizeof(wmiClassGuids) / sizeof(GUIDREGINFO))


/*++////////////////////////////////////////////////////////////////////////////

ClassFindGuid()

Routine Description:

    This routine will search the list of guids registered and return
    the index for the one that was registered.

Arguments:

    GuidList is the list of guids to search

    GuidCount is the count of guids in the list

    Guid is the guid being searched for

    *GuidIndex returns the index to the guid

Return Value:

    TRUE if guid is found else FALSE

--*/
BOOLEAN
ClassFindGuid(
    PGUIDREGINFO GuidList,
    ULONG GuidCount,
    LPGUID Guid,
    PULONG GuidIndex
    )
{
    ULONG i;

    PAGED_CODE();

    for (i = 0; i < GuidCount; i++)
    {
        if (IsEqualGUID(Guid, &GuidList[i].Guid))
        {
            *GuidIndex = i;
            return(TRUE);
        }
    }
    return(FALSE);
} // end ClassFindGuid()

/*++////////////////////////////////////////////////////////////////////////////

ClassFindInternalGuid()

Routine Description:

    This routine will search the list of internal guids registered and return
    the index for the one that was registered.

Arguments:

    Guid is the guid being searched for

    *GuidIndex returns the index to the guid

Return Value:

    TRUE if guid is found else FALSE

--*/
BOOLEAN
ClassFindInternalGuid(
    LPGUID Guid,
    PULONG GuidIndex
    )
{
    ULONG i;

    PAGED_CODE();

    for (i = 0; i < NUM_CLASS_WMI_GUIDS; i++)
    {
        if (IsEqualGUID(Guid, &wmiClassGuids[i].Guid))
        {
            *GuidIndex = i;
            return(TRUE);
        }
    }

    return(FALSE);
} // end ClassFindGuid()

/*++////////////////////////////////////////////////////////////////////////////

ClassSystemControl()

Routine Description:

    Dispatch routine for IRP_MJ_SYSTEM_CONTROL. This routine will process
    all wmi requests received, forwarding them if they are not for this
    driver or determining if the guid is valid and if so passing it to
    the driver specific function for handing wmi requests.

Arguments:

    DeviceObject - Supplies a pointer to the device object for this request.

    Irp - Supplies the Irp making the request.

Return Value:

    status

--*/
NTSTATUS
ClassSystemControl(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
{
    PCOMMON_DEVICE_EXTENSION commonExtension = DeviceObject->DeviceExtension;
    PCLASS_DRIVER_EXTENSION driverExtension;
    PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
    ULONG isRemoved;
    ULONG bufferSize;
    PUCHAR buffer;
    NTSTATUS status;
    UCHAR minorFunction;
    ULONG guidIndex = (ULONG)-1;
    PCLASS_WMI_INFO classWmiInfo;
    BOOLEAN isInternalGuid = FALSE;

    PAGED_CODE();

    //
    // Make sure device has not been removed
    isRemoved = ClassAcquireRemoveLock(DeviceObject, Irp);
    if(isRemoved)
    {
        Irp->IoStatus.Status = STATUS_DEVICE_DOES_NOT_EXIST;
        ClassReleaseRemoveLock(DeviceObject, Irp);
        ClassCompleteRequest(DeviceObject, Irp, IO_NO_INCREMENT);
        return STATUS_DEVICE_DOES_NOT_EXIST;
    }

    //
    // If the irp is not a WMI irp or it is not targetted at this device
    // or this device has not regstered with WMI then just forward it on.
    minorFunction = irpStack->MinorFunction;
    if ((minorFunction > IRP_MN_EXECUTE_METHOD) ||
        (irpStack->Parameters.WMI.ProviderId != (ULONG_PTR)DeviceObject) ||
        ((minorFunction != IRP_MN_REGINFO) &&
         (commonExtension->GuidCount == 0)))
    {
        //
        // CONSIDER: Do I need to hang onto lock until IoCallDriver returns ?
        IoSkipCurrentIrpStackLocation(Irp);
        ClassReleaseRemoveLock(DeviceObject, Irp);
        return(IoCallDriver(commonExtension->LowerDeviceObject, Irp));
    }

    buffer = (PUCHAR)irpStack->Parameters.WMI.Buffer;
    bufferSize = irpStack->Parameters.WMI.BufferSize;

    if (minorFunction != IRP_MN_REGINFO)
    {
        //
        // For all requests other than query registration info we are passed
        // a guid. Determine if the guid is one that is supported by the
        // device.
        if (commonExtension->GuidRegInfo != NULL &&
            ClassFindGuid(commonExtension->GuidRegInfo,
                            commonExtension->GuidCount,
                            (LPGUID)irpStack->Parameters.WMI.DataPath,
                            &guidIndex))
        {
            isInternalGuid = FALSE;
            status = STATUS_SUCCESS;
        } else if (ClassFindInternalGuid((LPGUID)irpStack->Parameters.WMI.DataPath,
                            &guidIndex)) {
            isInternalGuid = TRUE;
            status = STATUS_SUCCESS;
        } else {
            status = STATUS_WMI_GUID_NOT_FOUND;
            TracePrint((TRACE_LEVEL_WARNING, TRACE_FLAG_WMI, "WMI GUID not found!"));
        }

        TracePrint((TRACE_LEVEL_VERBOSE, TRACE_FLAG_WMI, "WMI Find Guid = %x, isInternalGuid = %x", status, isInternalGuid));
        if (NT_SUCCESS(status) &&
            ((minorFunction == IRP_MN_QUERY_SINGLE_INSTANCE) ||
             (minorFunction == IRP_MN_CHANGE_SINGLE_INSTANCE) ||
             (minorFunction == IRP_MN_CHANGE_SINGLE_ITEM) ||
             (minorFunction == IRP_MN_EXECUTE_METHOD)))
        {
            if ( (((PWNODE_HEADER)buffer)->Flags) &
                                          WNODE_FLAG_STATIC_INSTANCE_NAMES)
            {
                if ( ((PWNODE_SINGLE_INSTANCE)buffer)->InstanceIndex != 0 )
                {
                    status = STATUS_WMI_INSTANCE_NOT_FOUND;
                }
            } else {
                status = STATUS_WMI_INSTANCE_NOT_FOUND;
                TracePrint((TRACE_LEVEL_WARNING, TRACE_FLAG_WMI, "WMI Instance not found!"));
            }
        }

        if (! NT_SUCCESS(status))
        {
            Irp->IoStatus.Status = status;
            ClassReleaseRemoveLock(DeviceObject, Irp);
            ClassCompleteRequest(DeviceObject, Irp, IO_NO_INCREMENT);
            return(status);
        }
    }

    driverExtension = commonExtension->DriverExtension;

    classWmiInfo = commonExtension->IsFdo ?
                           &driverExtension->InitData.FdoData.ClassWmiInfo :
                           &driverExtension->InitData.PdoData.ClassWmiInfo;
    switch(minorFunction)
    {
        case IRP_MN_REGINFO:
        {
            ULONG guidCount;
            PGUIDREGINFO guidList;
            PWMIREGINFOW wmiRegInfo;
            PWMIREGGUIDW wmiRegGuid;
            PUNICODE_STRING regPath;
            PWCHAR stringPtr;
            ULONG retSize;
            ULONG registryPathOffset;
            ULONG mofResourceOffset;
            ULONG bufferNeeded;
            ULONG i;
            ULONG_PTR nameInfo;
            ULONG nameSize, nameOffset, nameFlags;
            UNICODE_STRING name, mofName;
            PCLASS_QUERY_WMI_REGINFO_EX ClassQueryWmiRegInfoEx;

            name.Buffer = NULL;
            name.Length = 0;
            name.MaximumLength = 0;
            nameFlags = 0;

            ClassQueryWmiRegInfoEx = commonExtension->IsFdo ?
                               driverExtension->ClassFdoQueryWmiRegInfoEx :
                               driverExtension->ClassPdoQueryWmiRegInfoEx;

            if ((classWmiInfo->GuidRegInfo != NULL) &&
                (classWmiInfo->ClassQueryWmiRegInfo != NULL) &&
                (ClassQueryWmiRegInfoEx == NULL))
            {
                status = classWmiInfo->ClassQueryWmiRegInfo(
                                                        DeviceObject,
                                                        &nameFlags,
                                                        &name);

                RtlInitUnicodeString(&mofName, MOFRESOURCENAME);

            } else if ((classWmiInfo->GuidRegInfo != NULL) && (ClassQueryWmiRegInfoEx != NULL)) {
                RtlInitUnicodeString(&mofName, L"");

                status = (*ClassQueryWmiRegInfoEx)(
                                                    DeviceObject,
                                                    &nameFlags,
                                                    &name,
                                                    &mofName);
            } else {
                RtlInitUnicodeString(&mofName, L"");
                nameFlags = WMIREG_FLAG_INSTANCE_PDO;
                status = STATUS_SUCCESS;
            }

            if (NT_SUCCESS(status) &&
                (! (nameFlags &  WMIREG_FLAG_INSTANCE_PDO) &&
                (name.Buffer == NULL)))
            {
                //
                // if PDO flag not specified then an instance name must be
                status = STATUS_INVALID_DEVICE_REQUEST;
                TracePrint((TRACE_LEVEL_WARNING, TRACE_FLAG_WMI, "Invalid Device Request!"));
            }

            if (NT_SUCCESS(status))
            {
                guidList = classWmiInfo->GuidRegInfo;
                guidCount = (classWmiInfo->GuidRegInfo == NULL ? 0 : classWmiInfo->GuidCount) + NUM_CLASS_WMI_GUIDS;

                nameOffset = sizeof(WMIREGINFO) +
                                      guidCount * sizeof(WMIREGGUIDW);

                if (nameFlags & WMIREG_FLAG_INSTANCE_PDO)
                {
                    nameSize = 0;
                    nameInfo = commonExtension->IsFdo ?
                                   (ULONG_PTR)((PFUNCTIONAL_DEVICE_EXTENSION)commonExtension)->LowerPdo :
                                   (ULONG_PTR)DeviceObject;
                } else {
                    nameFlags |= WMIREG_FLAG_INSTANCE_LIST;
                    nameSize = name.Length + sizeof(USHORT);
                    nameInfo = nameOffset;
                }

                mofResourceOffset = nameOffset + nameSize;

                registryPathOffset = mofResourceOffset +
                                  mofName.Length + sizeof(USHORT);

                regPath = &driverExtension->RegistryPath;

                bufferNeeded = registryPathOffset + regPath->Length;
                bufferNeeded += sizeof(USHORT);

                if (bufferNeeded <= bufferSize)
                {
                    retSize = bufferNeeded;

                    commonExtension->GuidCount = guidCount;
                    commonExtension->GuidRegInfo = guidList;

                    wmiRegInfo = (PWMIREGINFO)buffer;
                    wmiRegInfo->BufferSize = bufferNeeded;
                    wmiRegInfo->NextWmiRegInfo = 0;
                    wmiRegInfo->MofResourceName = mofResourceOffset;
                    wmiRegInfo->RegistryPath = registryPathOffset;
                    wmiRegInfo->GuidCount = guidCount;

                    for (i = 0; i < classWmiInfo->GuidCount; i++)
                    {
                        wmiRegGuid = &wmiRegInfo->WmiRegGuid[i];
                        wmiRegGuid->Guid = guidList[i].Guid;
                        wmiRegGuid->Flags = guidList[i].Flags | nameFlags;
                        wmiRegGuid->InstanceInfo = nameInfo;
                        wmiRegGuid->InstanceCount = 1;
                    }
                    for (i = 0; i < NUM_CLASS_WMI_GUIDS; i++)
                    {
                        wmiRegGuid = &wmiRegInfo->WmiRegGuid[i + classWmiInfo->GuidCount];
                        wmiRegGuid->Guid = wmiClassGuids[i].Guid;
                        wmiRegGuid->Flags = wmiClassGuids[i].Flags | nameFlags;
                        wmiRegGuid->InstanceInfo = nameInfo;
                        wmiRegGuid->InstanceCount = 1;
                    }

                    if ( nameFlags &  WMIREG_FLAG_INSTANCE_LIST)
                    {
                        bufferNeeded = nameOffset + sizeof(WCHAR);
                        bufferNeeded += name.Length;

                        if (bufferSize >= bufferNeeded){
                            stringPtr = (PWCHAR)((PUCHAR)buffer + nameOffset);
                            *stringPtr++ = name.Length;
                            RtlCopyMemory(stringPtr, name.Buffer, name.Length);
                        }
                        else {
                            NT_ASSERT(bufferSize >= bufferNeeded);
                            status = STATUS_INVALID_BUFFER_SIZE;
                        }
                    }

                    bufferNeeded = mofResourceOffset + sizeof(WCHAR);
                    bufferNeeded += mofName.Length;

                    if (bufferSize >= bufferNeeded){
                        stringPtr = (PWCHAR)((PUCHAR)buffer + mofResourceOffset);
                        *stringPtr++ = mofName.Length;
                        RtlCopyMemory(stringPtr, mofName.Buffer, mofName.Length);
                    }
                    else {
                        NT_ASSERT(bufferSize >= bufferNeeded);
                        status = STATUS_INVALID_BUFFER_SIZE;
                    }

                    bufferNeeded = registryPathOffset + sizeof(WCHAR);
                    bufferNeeded += regPath->Length;

                    if (bufferSize >= bufferNeeded){
                        stringPtr = (PWCHAR)((PUCHAR)buffer + registryPathOffset);
                        *stringPtr++ = regPath->Length;
                        RtlCopyMemory(stringPtr,
                                  regPath->Buffer,
                                  regPath->Length);
                    }
                    else {

                        NT_ASSERT(bufferSize >= bufferNeeded);
                        TracePrint((TRACE_LEVEL_WARNING, TRACE_FLAG_WMI, "Invalid Buffer Size!"));
                        status = STATUS_INVALID_BUFFER_SIZE;
                    }

                } else {
                    *((PULONG)buffer) = bufferNeeded;
                    retSize = sizeof(ULONG);
                }
            } else {
                retSize = 0;
            }

            FREE_POOL(name.Buffer);

            Irp->IoStatus.Status = status;
            Irp->IoStatus.Information = retSize;
            ClassReleaseRemoveLock(DeviceObject, Irp);
            ClassCompleteRequest(DeviceObject, Irp, IO_NO_INCREMENT);
            return(status);
        }

        case IRP_MN_QUERY_ALL_DATA:
        {
            PWNODE_ALL_DATA wnode;
            ULONG bufferAvail;

            wnode = (PWNODE_ALL_DATA)buffer;

            if (bufferSize < sizeof(WNODE_ALL_DATA))
            {
                bufferAvail = 0;
            } else {
                bufferAvail = bufferSize - sizeof(WNODE_ALL_DATA);
            }

            wnode->DataBlockOffset = sizeof(WNODE_ALL_DATA);

            NT_ASSERT(guidIndex != (ULONG)-1);
            _Analysis_assume_(isInternalGuid);
            if (isInternalGuid)
            {
                status = ClassQueryInternalDataBlock(
                                                DeviceObject,
                                                Irp,
                                                guidIndex,
                                                bufferAvail,
                                                buffer + sizeof(WNODE_ALL_DATA));
            } else {
                status = classWmiInfo->ClassQueryWmiDataBlock(
                                                 DeviceObject,
                                                 Irp,
                                                 guidIndex,
                                                 bufferAvail,
                                                 buffer + sizeof(WNODE_ALL_DATA));
            }
            break;
        }

        case IRP_MN_QUERY_SINGLE_INSTANCE:
        {
            PWNODE_SINGLE_INSTANCE wnode;
            ULONG dataBlockOffset;

            wnode = (PWNODE_SINGLE_INSTANCE)buffer;

            dataBlockOffset = wnode->DataBlockOffset;

            NT_ASSERT(guidIndex != (ULONG)-1);
            _Analysis_assume_(isInternalGuid);
            if (isInternalGuid)
            {
                status = ClassQueryInternalDataBlock(
                                            DeviceObject,
                                            Irp,
                                            guidIndex,
                                            bufferSize - dataBlockOffset,
                                            (PUCHAR)wnode + dataBlockOffset);
            } else {
                status = classWmiInfo->ClassQueryWmiDataBlock(
                                              DeviceObject,
                                              Irp,
                                              guidIndex,
                                              bufferSize - dataBlockOffset,
                                              (PUCHAR)wnode + dataBlockOffset);
            }
            break;
        }

        case IRP_MN_CHANGE_SINGLE_INSTANCE:
        {
            PWNODE_SINGLE_INSTANCE wnode;

            wnode = (PWNODE_SINGLE_INSTANCE)buffer;
            _Analysis_assume_(isInternalGuid);
            if (isInternalGuid)
            {
                status = ClassWmiCompleteRequest(DeviceObject,
                                                Irp,
                                                STATUS_WMI_GUID_NOT_FOUND,
                                                0,
                                                IO_NO_INCREMENT);
            } else {
                
                NT_ASSERT(guidIndex != (ULONG)-1);

                status = classWmiInfo->ClassSetWmiDataBlock(
                                         DeviceObject,
                                         Irp,
                                         guidIndex,
                                         wnode->SizeDataBlock,
                                         (PUCHAR)wnode + wnode->DataBlockOffset);
            }

            break;
        }

        case IRP_MN_CHANGE_SINGLE_ITEM:
        {
            PWNODE_SINGLE_ITEM wnode;

            wnode = (PWNODE_SINGLE_ITEM)buffer;

            NT_ASSERT(guidIndex != (ULONG)-1);
            _Analysis_assume_(isInternalGuid);
            if (isInternalGuid)
            {
                status = ClassWmiCompleteRequest(DeviceObject,
                                                Irp,
                                                STATUS_WMI_GUID_NOT_FOUND,
                                                0,
                                                IO_NO_INCREMENT);
            } else {

                NT_ASSERT(guidIndex != (ULONG)-1);

                status = classWmiInfo->ClassSetWmiDataItem(
                                         DeviceObject,
                                         Irp,
                                         guidIndex,
                                         wnode->ItemId,
                                         wnode->SizeDataItem,
                                         (PUCHAR)wnode + wnode->DataBlockOffset);

            }

            break;
        }

        case IRP_MN_EXECUTE_METHOD:
        {
            PWNODE_METHOD_ITEM wnode;

            wnode = (PWNODE_METHOD_ITEM)buffer;
            _Analysis_assume_(isInternalGuid);
            if (isInternalGuid)
            {
                status = ClassWmiCompleteRequest(DeviceObject,
                                                Irp,
                                                STATUS_WMI_GUID_NOT_FOUND,
                                                0,
                                                IO_NO_INCREMENT);
            } else {

                NT_ASSERT(guidIndex != (ULONG)-1);

                status = classWmiInfo->ClassExecuteWmiMethod(
                                             DeviceObject,
                                             Irp,
                                             guidIndex,
                                             wnode->MethodId,
                                             wnode->SizeDataBlock,
                                             bufferSize - wnode->DataBlockOffset,
                                             buffer + wnode->DataBlockOffset);
            }

            break;
        }

        case IRP_MN_ENABLE_EVENTS:
        {
            _Analysis_assume_(isInternalGuid);
            if (isInternalGuid)
            {
                status = ClassWmiCompleteRequest(DeviceObject,
                                                Irp,
                                                STATUS_WMI_GUID_NOT_FOUND,
                                                0,
                                                IO_NO_INCREMENT);
            } else {

                NT_ASSERT(guidIndex != (ULONG)-1);

                status = classWmiInfo->ClassWmiFunctionControl(
                                                               DeviceObject,
                                                               Irp,
                                                               guidIndex,
                                                               EventGeneration,
                                                               TRUE);
            }
            break;
        }

        case IRP_MN_DISABLE_EVENTS:
        {
            _Analysis_assume_(isInternalGuid);
            if (isInternalGuid)
            {
                status = ClassWmiCompleteRequest(DeviceObject,
                                                Irp,
                                                STATUS_WMI_GUID_NOT_FOUND,
                                                0,
                                                IO_NO_INCREMENT);
            } else {

                NT_ASSERT(guidIndex != (ULONG)-1);

                status = classWmiInfo->ClassWmiFunctionControl(
                                                               DeviceObject,
                                                               Irp,
                                                               guidIndex,
                                                               EventGeneration,
                                                               FALSE);
            }
            break;
        }

        case IRP_MN_ENABLE_COLLECTION:
        {
            _Analysis_assume_(isInternalGuid);
            if (isInternalGuid)
            {
                status = ClassWmiCompleteRequest(DeviceObject,
                                                Irp,
                                                STATUS_WMI_GUID_NOT_FOUND,
                                                0,
                                                IO_NO_INCREMENT);
            } else {

                NT_ASSERT(guidIndex != (ULONG)-1);

                status = classWmiInfo->ClassWmiFunctionControl(
                                                             DeviceObject,
                                                             Irp,
                                                             guidIndex,
                                                             DataBlockCollection,
                                                             TRUE);
            }
            break;
        }

        case IRP_MN_DISABLE_COLLECTION:
        {
            _Analysis_assume_(isInternalGuid);
            if (isInternalGuid)
            {
                status = ClassWmiCompleteRequest(DeviceObject,
                                                Irp,
                                                STATUS_WMI_GUID_NOT_FOUND,
                                                0,
                                                IO_NO_INCREMENT);
            } else {

                NT_ASSERT(guidIndex != (ULONG)-1);

                status = classWmiInfo->ClassWmiFunctionControl(
                                                             DeviceObject,
                                                             Irp,
                                                             guidIndex,
                                                             DataBlockCollection,
                                                             FALSE);
            }

            break;
        }

        default:
        {
            status = STATUS_INVALID_DEVICE_REQUEST;
            break;
        }

    }

    return(status);
} // end ClassSystemControl()


NTSTATUS
ClassQueryInternalDataBlock(
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN ULONG GuidIndex,
    IN ULONG BufferAvail,
    OUT PUCHAR Buffer
    )
/*++

Routine Description:

    This routine allows querying for the contents of an internal WMI
    data block. When the driver has finished filling the data block it
    must call ClassWmiCompleteRequest to complete the irp.

Arguments:

    DeviceObject is the device whose data block is being queried

    Irp is the Irp that makes this request

    GuidIndex is the index into the list of guids provided when the
        device registered

    BufferAvail on has the maximum size available to write the data
        block.

    Buffer on return is filled with the returned data block


Return Value:

    status

--*/
{
    NTSTATUS status;
    ULONG sizeNeeded = 0, i;
    PFUNCTIONAL_DEVICE_EXTENSION fdoExt = DeviceObject->DeviceExtension;

    if (GuidIndex == MSStorageDriver_ClassErrorLogGuid_Index) {

        //
        // NOTE - ClassErrorLog is still using SCSI_REQUEST_BLOCK and will not be
        // updated to support extended SRB until classpnp is updated to send >16
        // byte CDBs. Extended SRBs will be translated to SCSI_REQUEST_BLOCK.
        //
        sizeNeeded = MSStorageDriver_ClassErrorLog_SIZE;
        if (BufferAvail >= sizeNeeded) {
            PMSStorageDriver_ClassErrorLog errorLog = (PMSStorageDriver_ClassErrorLog) Buffer;
            PMSStorageDriver_ClassErrorLogEntry logEntry;
            PMSStorageDriver_ScsiRequestBlock srbBlock;
            PMSStorageDriver_SenseData senseData;
            PCLASS_PRIVATE_FDO_DATA fdoData = fdoExt->PrivateFdoData;
            PCLASS_ERROR_LOG_DATA fdoLogEntry;
            PSCSI_REQUEST_BLOCK fdoSRBBlock;
            PSENSE_DATA fdoSenseData;
            errorLog->numEntries = NUM_ERROR_LOG_ENTRIES;
            for (i = 0; i < NUM_ERROR_LOG_ENTRIES; i++) {
                fdoLogEntry = &fdoData->ErrorLogs[i];
                fdoSRBBlock = &fdoLogEntry->Srb;
                fdoSenseData = &fdoLogEntry->SenseData;
                logEntry = &errorLog->logEntries[i];
                srbBlock = &logEntry->srb;
                senseData = &logEntry->senseData;
                logEntry->tickCount = fdoLogEntry->TickCount.QuadPart;
                logEntry->portNumber = fdoLogEntry->PortNumber;
                logEntry->errorPaging = (fdoLogEntry->ErrorPaging == 0 ? FALSE : TRUE);
                logEntry->errorRetried = (fdoLogEntry->ErrorRetried == 0 ? FALSE : TRUE);
                logEntry->errorUnhandled = (fdoLogEntry->ErrorUnhandled == 0 ? FALSE : TRUE);
                logEntry->errorReserved = fdoLogEntry->ErrorReserved;
                RtlMoveMemory(logEntry->reserved, fdoLogEntry->Reserved, sizeof(logEntry->reserved));
                ConvertTickToDateTime(fdoLogEntry->TickCount, logEntry->eventTime);

                srbBlock->length = fdoSRBBlock->Length;
                srbBlock->function = fdoSRBBlock->Function;
                srbBlock->srbStatus = fdoSRBBlock->SrbStatus;
                srbBlock->scsiStatus = fdoSRBBlock->ScsiStatus;
                srbBlock->pathID = fdoSRBBlock->PathId;
                srbBlock->targetID = fdoSRBBlock->TargetId;
                srbBlock->lun = fdoSRBBlock->Lun;
                srbBlock->queueTag = fdoSRBBlock->QueueTag;
                srbBlock->queueAction = fdoSRBBlock->QueueAction;
                srbBlock->cdbLength = fdoSRBBlock->CdbLength;
                srbBlock->senseInfoBufferLength = fdoSRBBlock->SenseInfoBufferLength;
                srbBlock->srbFlags = fdoSRBBlock->SrbFlags;
                srbBlock->dataTransferLength = fdoSRBBlock->DataTransferLength;
                srbBlock->timeOutValue = fdoSRBBlock->TimeOutValue;
                srbBlock->dataBuffer = (ULONGLONG) fdoSRBBlock->DataBuffer;
                srbBlock->senseInfoBuffer = (ULONGLONG) fdoSRBBlock->SenseInfoBuffer;
                srbBlock->nextSRB = (ULONGLONG) fdoSRBBlock->NextSrb;
                srbBlock->originalRequest = (ULONGLONG) fdoSRBBlock->OriginalRequest;
                srbBlock->srbExtension = (ULONGLONG) fdoSRBBlock->SrbExtension;
                srbBlock->internalStatus = fdoSRBBlock->InternalStatus;
#if defined(_WIN64)
                srbBlock->reserved = fdoSRBBlock->Reserved;
#else
                srbBlock->reserved = 0;
#endif
                RtlMoveMemory(srbBlock->cdb, fdoSRBBlock->Cdb, sizeof(srbBlock->cdb));

                //
                // Note: Sense data has been converted into Fixed format before it was
                //       put in the log.  Therefore, no conversion is needed here.
                //
                senseData->errorCode = fdoSenseData->ErrorCode;
                senseData->valid = (fdoSenseData->Valid == 0 ? FALSE : TRUE);
                senseData->segmentNumber = fdoSenseData->SegmentNumber;
                senseData->senseKey = fdoSenseData->SenseKey;
                senseData->reserved = (fdoSenseData->Reserved == 0 ? FALSE : TRUE);
                senseData->incorrectLength = (fdoSenseData->IncorrectLength == 0 ? FALSE : TRUE);
                senseData->endOfMedia = (fdoSenseData->EndOfMedia == 0 ? FALSE : TRUE);
                senseData->fileMark = (fdoSenseData->FileMark == 0 ? FALSE : TRUE);
                RtlMoveMemory(senseData->information, fdoSenseData->Information, sizeof(senseData->information));
                senseData->additionalSenseLength = fdoSenseData->AdditionalSenseLength;
                RtlMoveMemory(senseData->commandSpecificInformation, fdoSenseData->CommandSpecificInformation, sizeof(senseData->commandSpecificInformation));
                senseData->additionalSenseCode = fdoSenseData->AdditionalSenseCode;
                senseData->additionalSenseCodeQualifier = fdoSenseData->AdditionalSenseCodeQualifier;
                senseData->fieldReplaceableUnitCode = fdoSenseData->FieldReplaceableUnitCode;
                RtlMoveMemory(senseData->senseKeySpecific, fdoSenseData->SenseKeySpecific, sizeof(senseData->senseKeySpecific));
            }
            status = STATUS_SUCCESS;
        } else {
            status = STATUS_BUFFER_TOO_SMALL;
        }
    } else if (GuidIndex > 0 && GuidIndex < NUM_CLASS_WMI_GUIDS) {
        status = STATUS_WMI_INSTANCE_NOT_FOUND;
    } else {
        status = STATUS_WMI_GUID_NOT_FOUND;
    }
    status = ClassWmiCompleteRequest(DeviceObject,
                                    Irp,
                                    status,
                                    sizeNeeded,
                                    IO_NO_INCREMENT);
    return status;
}

PWCHAR
ConvertTickToDateTime(
    IN LARGE_INTEGER Tick,
    _Out_writes_(TIME_STRING_LENGTH) PWCHAR String
    )

/*++

Routine Description:

    This routine converts a tick count to a datetime (MOF) data type

Arguments:

    Tick - The tick count that needs to be converted
    String - The buffer to hold the time string, must be able to hold WCHAR[25]

Return Value:

    The time string

--*/

{
    LARGE_INTEGER nowTick, nowTime, time;
    ULONG maxInc = 0;
    TIME_FIELDS timeFields = {0};
    WCHAR outDateTime[TIME_STRING_LENGTH + 1];

    nowTick.QuadPart = 0;
    nowTime.QuadPart = 0;
    //
    // Translate the tick count to a system time
    //
    KeQueryTickCount(&nowTick);
    maxInc = KeQueryTimeIncrement();
    KeQuerySystemTime(&nowTime);
    time.QuadPart = nowTime.QuadPart - ((nowTick.QuadPart - Tick.QuadPart) * maxInc);

    RtlTimeToTimeFields(&time, &timeFields);

    //
    // The buffer String is of size MAX_PATH. Use that to specify the buffer size.
    //
    //yyyymmddhhmmss.mmmmmmsutc
    RtlStringCbPrintfW(outDateTime, sizeof(outDateTime), L"%04d%02d%02d%02d%02d%02d.%03d***+000", timeFields.Year, timeFields.Month, timeFields.Day, timeFields.Hour, timeFields.Minute, timeFields.Second, timeFields.Milliseconds);
    RtlMoveMemory(String, outDateTime, sizeof(WCHAR) * TIME_STRING_LENGTH);
    return  String;
}

/*++////////////////////////////////////////////////////////////////////////////

ClassWmiCompleteRequest()

Routine Description:


    This routine will do the work of completing a WMI irp. Depending upon the
    the WMI request this routine will fixup the returned WNODE appropriately.

    NOTE: This routine assumes that the ClassRemoveLock is held and it will
          release it.

Arguments:

    DeviceObject - Supplies a pointer to the device object for this request.

    Irp - Supplies the Irp making the request.

    Status - Status to complete the irp with.  STATUS_BUFFER_TOO_SMALL is used
        to indicate that more buffer is required for the data requested.

    BufferUsed - number of bytes of actual data to return (not including WMI
        specific structures)

    PriorityBoost - priority boost to pass to ClassCompleteRequest

Return Value:

    status

--*/
SCSIPORT_API
NTSTATUS
ClassWmiCompleteRequest(
    _In_ PDEVICE_OBJECT DeviceObject,
    _Inout_ PIRP Irp,
    _In_ NTSTATUS Status,
    _In_ ULONG BufferUsed,
    _In_ CCHAR PriorityBoost
    )
{
    PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
    PUCHAR buffer;
    ULONG retSize;
    UCHAR minorFunction;

    minorFunction = irpStack->MinorFunction;
    buffer = (PUCHAR)irpStack->Parameters.WMI.Buffer;

    switch(minorFunction)
    {
        case IRP_MN_QUERY_ALL_DATA:
        {
            PWNODE_ALL_DATA wnode;
            PWNODE_TOO_SMALL wnodeTooSmall;
            ULONG bufferNeeded;

            wnode = (PWNODE_ALL_DATA)buffer;

            bufferNeeded = sizeof(WNODE_ALL_DATA) + BufferUsed;

            if (NT_SUCCESS(Status))
            {
                retSize = bufferNeeded;
                wnode->WnodeHeader.BufferSize = bufferNeeded;
                KeQuerySystemTime(&wnode->WnodeHeader.TimeStamp);
                wnode->WnodeHeader.Flags |= WNODE_FLAG_FIXED_INSTANCE_SIZE;
                wnode->FixedInstanceSize = BufferUsed;
                wnode->InstanceCount = 1;

            } else if (Status == STATUS_BUFFER_TOO_SMALL) {
                wnodeTooSmall = (PWNODE_TOO_SMALL)wnode;

                wnodeTooSmall->WnodeHeader.BufferSize = sizeof(WNODE_TOO_SMALL);
                wnodeTooSmall->WnodeHeader.Flags = WNODE_FLAG_TOO_SMALL;
                wnodeTooSmall->SizeNeeded = sizeof(WNODE_ALL_DATA) + BufferUsed;
                retSize = sizeof(WNODE_TOO_SMALL);
                Status = STATUS_SUCCESS;
            } else {
                retSize = 0;
            }
            break;
        }

        case IRP_MN_QUERY_SINGLE_INSTANCE:
        {
            PWNODE_SINGLE_INSTANCE wnode;
            PWNODE_TOO_SMALL wnodeTooSmall;
            ULONG bufferNeeded;

            wnode = (PWNODE_SINGLE_INSTANCE)buffer;

            bufferNeeded = wnode->DataBlockOffset + BufferUsed;

            if (NT_SUCCESS(Status))
            {
                retSize = bufferNeeded;
                wnode->WnodeHeader.BufferSize = bufferNeeded;
                KeQuerySystemTime(&wnode->WnodeHeader.TimeStamp);
                wnode->SizeDataBlock = BufferUsed;

            } else if (Status == STATUS_BUFFER_TOO_SMALL) {
                wnodeTooSmall = (PWNODE_TOO_SMALL)wnode;

                wnodeTooSmall->WnodeHeader.BufferSize = sizeof(WNODE_TOO_SMALL);
                wnodeTooSmall->WnodeHeader.Flags = WNODE_FLAG_TOO_SMALL;
                wnodeTooSmall->SizeNeeded = bufferNeeded;
                retSize = sizeof(WNODE_TOO_SMALL);
                Status = STATUS_SUCCESS;
            } else {
                retSize = 0;
            }
            break;
        }

        case IRP_MN_EXECUTE_METHOD:
        {
            PWNODE_METHOD_ITEM wnode;
            PWNODE_TOO_SMALL wnodeTooSmall;
            ULONG bufferNeeded;

            wnode = (PWNODE_METHOD_ITEM)buffer;

            bufferNeeded = wnode->DataBlockOffset + BufferUsed;

            if (NT_SUCCESS(Status))
            {
                retSize = bufferNeeded;
                wnode->WnodeHeader.BufferSize = bufferNeeded;
                KeQuerySystemTime(&wnode->WnodeHeader.TimeStamp);
                wnode->SizeDataBlock = BufferUsed;

            } else if (Status == STATUS_BUFFER_TOO_SMALL) {
                wnodeTooSmall = (PWNODE_TOO_SMALL)wnode;

                wnodeTooSmall->WnodeHeader.BufferSize = sizeof(WNODE_TOO_SMALL);
                wnodeTooSmall->WnodeHeader.Flags = WNODE_FLAG_TOO_SMALL;
                wnodeTooSmall->SizeNeeded = bufferNeeded;
                retSize = sizeof(WNODE_TOO_SMALL);
                Status = STATUS_SUCCESS;
            } else {
                retSize = 0;
            }
            break;
        }

        default:
        {
            //
            // All other requests don't return any data
            retSize = 0;
            break;
        }

    }

    Irp->IoStatus.Status = Status;
    Irp->IoStatus.Information = retSize;
    ClassReleaseRemoveLock(DeviceObject, Irp);
    ClassCompleteRequest(DeviceObject, Irp, PriorityBoost);
    return(Status);
} // end ClassWmiCompleteRequest()

/*++////////////////////////////////////////////////////////////////////////////

ClassWmiFireEvent()

Routine Description:

    This routine will fire a WMI event using the data buffer passed. This
    routine may be called at or below DPC level

Arguments:

    DeviceObject - Supplies a pointer to the device object for this event

    Guid is pointer to the GUID that represents the event

    InstanceIndex is the index of the instance of the event

    EventDataSize is the number of bytes of data that is being fired with
       with the event

    EventData is the data that is fired with the events. This may be NULL
        if there is no data associated with the event


Return Value:

    status

--*/
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
ClassWmiFireEvent(
    _In_ PDEVICE_OBJECT DeviceObject,
    _In_ LPGUID Guid,
    _In_ ULONG InstanceIndex,
    _In_ ULONG EventDataSize,
    _In_reads_bytes_(EventDataSize) PVOID EventData
    )
{

    ULONG sizeNeeded;
    PWNODE_SINGLE_INSTANCE event;
    NTSTATUS status;

    if (EventData == NULL)
    {
        EventDataSize = 0;
    }

    sizeNeeded = sizeof(WNODE_SINGLE_INSTANCE) + EventDataSize;

    event = ExAllocatePoolZero(NonPagedPoolNx, sizeNeeded, CLASS_TAG_WMI);
    if (event != NULL)
    {
        event->WnodeHeader.Guid = *Guid;
        event->WnodeHeader.ProviderId = IoWMIDeviceObjectToProviderId(DeviceObject);
        event->WnodeHeader.BufferSize = sizeNeeded;
        event->WnodeHeader.Flags =  WNODE_FLAG_SINGLE_INSTANCE |
                                    WNODE_FLAG_EVENT_ITEM |
                                    WNODE_FLAG_STATIC_INSTANCE_NAMES;
        KeQuerySystemTime(&event->WnodeHeader.TimeStamp);

        event->InstanceIndex = InstanceIndex;
        event->SizeDataBlock = EventDataSize;
        event->DataBlockOffset = sizeof(WNODE_SINGLE_INSTANCE);
        if (EventData != NULL)
        {
            RtlCopyMemory( &event->VariableData, EventData, EventDataSize);
        }

        status = IoWMIWriteEvent(event);
        if (! NT_SUCCESS(status))
        {
            FREE_POOL(event);
        }
    } else {
        status = STATUS_INSUFFICIENT_RESOURCES;
    }

    return(status);
} // end ClassWmiFireEvent()