1 /*++
2 
3 Copyright (c) Microsoft Corporation
4 
5 Module Name:
6 
7     FxDriverApiKm.cpp
8 
9 Abstract:
10 
11     This module contains the "C" interface for the FxDriver object.
12 
13 Author:
14 
15 
16 
17 Environment:
18 
19     User mode only
20 
21 Revision History:
22 
23 --*/
24 
25 #include "coreprivshared.hpp"
26 
27 // Tracing support
28 extern "C" {
29 #include <ntverp.h>
30 #include "FxDriverApiUm.tmh"
31 }
32 
33 //
34 // extern the whole file
35 //
36 extern "C" {
37 
38 _Must_inspect_result_
__drv_maxIRQL(PASSIVE_LEVEL)39 __drv_maxIRQL(PASSIVE_LEVEL)
40 NTSTATUS
41 WDFEXPORT(WdfDriverOpenParametersRegistryKey)(
42     __in
43     PWDF_DRIVER_GLOBALS DriverGlobals,
44     __in
45     WDFDRIVER Driver,
46     __in
47     ACCESS_MASK DesiredAccess,
48     __in_opt
49     PWDF_OBJECT_ATTRIBUTES KeyAttributes,
50     __out
51     WDFKEY* Key
52     )
53 {
54     NTSTATUS status;
55     LONG result;
56     PFX_DRIVER_GLOBALS pFxDriverGlobals;
57     FxDriver* pDriver;
58     FxRegKey* pKey;
59     HKEY hKey = NULL;
60     WDFKEY keyHandle;
61 
62     pFxDriverGlobals = GetFxDriverGlobals(DriverGlobals);
63 
64     FxPointerNotNull(pFxDriverGlobals, Key);
65 
66     *Key = NULL;
67 
68     status = FxVerifierCheckIrqlLevel(pFxDriverGlobals, PASSIVE_LEVEL);
69     if (!NT_SUCCESS(status)) {
70         return status;
71     }
72 
73     status = FxValidateObjectAttributes(pFxDriverGlobals, KeyAttributes);
74     if (!NT_SUCCESS(status)) {
75         return status;
76     }
77 
78     FxObjectHandleGetPtr(pFxDriverGlobals,
79                          Driver,
80                          FX_TYPE_DRIVER,
81                          (PVOID*) &pDriver);
82 
83     pKey = new(pFxDriverGlobals, KeyAttributes) FxRegKey(pFxDriverGlobals);
84     if (pKey == NULL) {
85         return STATUS_INSUFFICIENT_RESOURCES;
86     }
87 
88     status = pKey->Commit(KeyAttributes, (WDFOBJECT*)&keyHandle);
89     if (NT_SUCCESS(status)) {
90         if (DesiredAccess & (GENERIC_WRITE | KEY_CREATE_SUB_KEY | WRITE_DAC)) {
91             //
92             // These access rights are not allowed. This restriction is
93             // imposed by the host process and the reflector driver.
94             //
95             // Even though the maximum-permissions handle is already opened,
96             // we fail so that the caller knows not to assume it has the
97             // GENERIC_WRITE, KEY_CREATE_SUB_KEY, or WRITE_DAC permissions.
98             //
99             status = STATUS_ACCESS_DENIED;
100             DoTraceLevelMessage(
101                     pFxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGDRIVER,
102                     "Could not open '%s' service parameters key "
103                     "with access rights 0x%x, %!STATUS!",
104                     pFxDriverGlobals->Public.DriverName,
105                     DesiredAccess, status);
106         } else if ((DesiredAccess & ~(KEY_READ | GENERIC_READ)) == 0) {
107             //
108             // If caller requested read-only access, open a new handle
109             // to the parameters key, no reason to give more privileges
110             // than needed.
111             //
112             result = RegOpenKeyEx(pDriver->GetDriverParametersKey(),
113                                   L"",
114                                   0,
115                                   DesiredAccess,
116                                   &hKey);
117             status = WinErrorToNtStatus(result);
118             if (!NT_SUCCESS(status)) {
119                 DoTraceLevelMessage(
120                     pFxDriverGlobals, TRACE_LEVEL_ERROR, TRACINGDRIVER,
121                     "Could not open '%s' service parameters key "
122                     "with access rights 0x%x, %!STATUS!",
123                     pFxDriverGlobals->Public.DriverName,
124                     DesiredAccess, status);
125             }
126         } else {
127             //
128             // If caller requested write access, give it the pre-opened
129             // handle, since we do not have permission to open this key
130             // with write access rights from user mode.
131             //
132             hKey = pDriver->GetDriverParametersKey();
133 
134             //
135             // Mark the registry key handle such that it won't be closed
136             // when this FxRegKey is deleted. We might need the handle again
137             // for future calls to WdfDriverOpenParametersRegistryKey.
138             //
139             pKey->SetCanCloseHandle(FALSE);
140         }
141 
142         if (NT_SUCCESS(status)) {
143             pKey->SetHandle((HANDLE)hKey);
144             *Key = keyHandle;
145         }
146     }
147 
148     if (!NT_SUCCESS(status)) {
149         pKey->DeleteFromFailedCreate();
150     }
151 
152     return status;
153 }
154 
__drv_maxIRQL(DISPATCH_LEVEL)155 __drv_maxIRQL(DISPATCH_LEVEL)
156 PDRIVER_OBJECT
157 WDFEXPORT(WdfDriverWdmGetDriverObject)(
158     __in
159     PWDF_DRIVER_GLOBALS DriverGlobals,
160     __in
161     WDFDRIVER Driver
162     )
163 {
164     DDI_ENTRY();
165 
166     UNREFERENCED_PARAMETER(DriverGlobals);
167     UNREFERENCED_PARAMETER(Driver);
168 
169     ASSERTMSG("Not implemented for UMDF\n", FALSE);
170 
171     return NULL;
172 }
173 
__drv_maxIRQL(DISPATCH_LEVEL)174 __drv_maxIRQL(DISPATCH_LEVEL)
175 WDFDRIVER
176 WDFEXPORT(WdfWdmDriverGetWdfDriverHandle)(
177     __in
178     PWDF_DRIVER_GLOBALS DriverGlobals,
179     __in
180     PDRIVER_OBJECT DriverObject
181     )
182 {
183     DDI_ENTRY();
184 
185     UNREFERENCED_PARAMETER(DriverGlobals);
186     UNREFERENCED_PARAMETER(DriverObject);
187 
188     ASSERTMSG("Not implemented for UMDF\n", FALSE);
189 
190     return NULL;
191 }
192 
193 VOID
WDFEXPORT(WdfDriverMiniportUnload)194 WDFEXPORT(WdfDriverMiniportUnload)(
195     __in
196     PWDF_DRIVER_GLOBALS DriverGlobals,
197     __in
198     WDFDRIVER Driver
199     )
200 {
201     DDI_ENTRY();
202 
203     UNREFERENCED_PARAMETER(DriverGlobals);
204     UNREFERENCED_PARAMETER(Driver);
205 
206     ASSERTMSG("Not implemented for UMDF\n", FALSE);
207 }
208 
209 _Must_inspect_result_
__drv_maxIRQL(PASSIVE_LEVEL)210 __drv_maxIRQL(PASSIVE_LEVEL)
211 NTSTATUS
212 WDFEXPORT(WdfDeviceMiniportCreate)(
213     __in
214     PWDF_DRIVER_GLOBALS DriverGlobals,
215     __in
216     WDFDRIVER Driver,
217     __in_opt
218     PWDF_OBJECT_ATTRIBUTES Attributes,
219     __in
220     PDEVICE_OBJECT DeviceObject,
221     __in_opt
222     PDEVICE_OBJECT AttachedDeviceObject,
223     __in_opt
224     PDEVICE_OBJECT Pdo,
225     __out
226     WDFDEVICE* Device
227     )
228 {
229     DDI_ENTRY();
230 
231     UNREFERENCED_PARAMETER(DriverGlobals);
232     UNREFERENCED_PARAMETER(Driver);
233     UNREFERENCED_PARAMETER(Attributes);
234     UNREFERENCED_PARAMETER(DeviceObject);
235     UNREFERENCED_PARAMETER(AttachedDeviceObject);
236     UNREFERENCED_PARAMETER(Pdo);
237     UNREFERENCED_PARAMETER(Device);
238 
239     ASSERTMSG("Not implemented for UMDF\n", FALSE);
240 
241     return STATUS_NOT_IMPLEMENTED;
242 }
243 
244 } // extern "C"
245