ReactOS 0.4.16-dev-87-g3dfbe52
vfdlink.c
Go to the documentation of this file.
1/*
2 vfdlink.c
3
4 Virtual Floppy Drive for Windows NT platform
5 Kernel mode driver: persistent drive letter functions
6
7 Copyright (C) 2003-2005 Ken Kato
8*/
9
10#include "imports.h"
11#include "vfddrv.h"
12#include "vfddbg.h"
13
14#ifdef ALLOC_PRAGMA
15#pragma alloc_text(PAGE, VfdSetLink)
16#pragma alloc_text(PAGE, VfdLoadLink)
17#pragma alloc_text(PAGE, VfdStoreLink)
18#endif // ALLOC_PRAGMA
19
20//
21// create or remove the persistent drive letter (Windows NT)
22//
25 IN PDEVICE_EXTENSION DeviceExtension,
26 IN CHAR DriveLetter)
27{
28 UNICODE_STRING unicode_name;
29 WCHAR name_buf[15];
31
32 VFDTRACE(VFDINFO, ("[VFD] VfdSetLink - IN\n"));
33
34 // convert lower case into upper case
35
36 if (DriveLetter >= 'a' && DriveLetter <= 'z') {
37 DriveLetter -= ('a' - 'A');
38 }
39
40 // check the drive letter range
41
42 if (DriveLetter != 0 &&
43 (DriveLetter < 'A' || DriveLetter > 'Z')) {
45 }
46
47 if (DeviceExtension->DriveLetter &&
48 DeviceExtension->DriveLetter != DriveLetter) {
49 //
50 // Delete the old drive letter
51 //
52#ifndef __REACTOS__
53 name_buf[sizeof(name_buf) - 1] = UNICODE_NULL;
54#else
55 name_buf[ARRAYSIZE(name_buf) - 1] = UNICODE_NULL;
56#endif
57
58 _snwprintf(name_buf, sizeof(name_buf) - 1,
59 L"\\??\\%wc:", DeviceExtension->DriveLetter);
60
61 RtlInitUnicodeString(&unicode_name, name_buf);
62
63 status = IoDeleteSymbolicLink(&unicode_name);
64
65 if (NT_SUCCESS(status)) {
66 VFDTRACE(VFDINFO,
67 ("[VFD] Link %ws deleted\n", name_buf));
68
69 DeviceExtension->DriveLetter = 0;
70 }
72 // the driver letter did not exist in the first place
73
74 VFDTRACE(VFDINFO,
75 ("[VFD] Link %ws not found\n", name_buf));
76
77 DeviceExtension->DriveLetter = 0;
79 }
80 else {
81 VFDTRACE(VFDWARN,
82 ("[VFD] IoDeleteSymbolicLink %ws - %s\n",
83 name_buf, GetStatusName(status)));
84 }
85 }
86
87 if (NT_SUCCESS(status) && DriveLetter) {
88 //
89 // Create a new drive letter
90 //
91
92#ifndef __REACTOS__
93 name_buf[sizeof(name_buf) - 1] = UNICODE_NULL;
94
95 _snwprintf(name_buf, sizeof(name_buf) - 1,
96 (OsMajorVersion >= 5) ?
97 L"\\??\\Global\\%wc:" : L"\\??\\%wc:",
98 DriveLetter);
99#else
100 name_buf[ARRAYSIZE(name_buf) - 1] = UNICODE_NULL;
101
102 _snwprintf(name_buf, ARRAYSIZE(name_buf) - 1,
103 (OsMajorVersion >= 5) ?
104 L"\\??\\Global\\%wc:" : L"\\??\\%wc:",
105 DriveLetter);
106#endif
107
108 RtlInitUnicodeString(&unicode_name, name_buf);
109
111 &unicode_name, &(DeviceExtension->DeviceName));
112
113 if (NT_SUCCESS(status)) {
114 VFDTRACE(VFDINFO, ("[VFD] Link %ws created\n", name_buf));
115
116 DeviceExtension->DriveLetter = DriveLetter;
117 }
118 else {
119 VFDTRACE(VFDWARN,
120 ("[VFD] IoCreateSymbolicLink %ws - %s\n",
121 name_buf, GetStatusName(status)));
122 }
123 }
124
125 VFDTRACE(VFDINFO,
126 ("[VFD] VfdSetLink - %s\n", GetStatusName(status)));
127
128 return status;
129}
130
131//
132// load the persistent drive letter from the registry
133//
136 IN PDEVICE_EXTENSION DeviceExtension,
138{
140 WCHAR name_buf[20];
141 ULONG letter;
142 ULONG zero = 0;
144
145 VFDTRACE(VFDINFO, ("[VFD] VfdLoadLink - IN\n"));
146
147 RtlZeroMemory(params, sizeof(params));
148
149#ifndef __REACTOS__
150 name_buf[sizeof(name_buf) - 1] = UNICODE_NULL;
151
152 _snwprintf(name_buf, sizeof(name_buf) - 1,
154 DeviceExtension->DeviceNumber);
155#else
156 name_buf[ARRAYSIZE(name_buf) - 1] = UNICODE_NULL;
157
158 _snwprintf(name_buf, ARRAYSIZE(name_buf) - 1,
160 DeviceExtension->DeviceNumber);
161#endif
162
164 params[0].Name = name_buf;
165 params[0].EntryContext = &letter;
166 params[0].DefaultType = REG_DWORD;
167 params[0].DefaultData = &zero;
168 params[0].DefaultLength = sizeof(ULONG);
169
173
174 VFDTRACE(VFDINFO,
175 ("[VFD] Drive letter '%wc' loaded from the registry\n",
176 letter ? letter : ' '));
177
178 DeviceExtension->DriveLetter = (CHAR)letter;
179
180 VFDTRACE(VFDINFO,
181 ("[VFD] VfdLoadLink - %s\n", GetStatusName(status)));
182
183 return status;
184}
185
186//
187// store the persistent drive letter into the registry
188//
191 IN PDEVICE_EXTENSION DeviceExtension)
192{
193 PVFD_DRIVER_EXTENSION driver_extension;
194 WCHAR name_buf[20];
195 ULONG letter;
197
198 VFDTRACE(VFDINFO, ("[VFD] VfdStoreLink - IN\n"));
199
200#ifdef VFD_PNP
201 driver_extension = IoGetDriverObjectExtension(
202 DeviceExtension->device_object->DriverObject,
203 VFD_DRIVER_EXTENSION_ID);
204#else // VFD_PNP
205 driver_extension = DeviceExtension->DriverExtension;
206#endif // VFD_PNP
207
208 if (!driver_extension ||
209 !driver_extension->RegistryPath.Buffer) {
210
211 VFDTRACE(VFDWARN, ("[VFD] Registry Path not present.\n"));
212 VFDTRACE(VFDINFO, ("[VFD] VfdStoreLinks - OUT\n"));
214 }
215
216#ifndef __REACTOS__
217 name_buf[sizeof(name_buf) - 1] = UNICODE_NULL;
218
219 _snwprintf(name_buf, sizeof(name_buf) - 1,
221 DeviceExtension->DeviceNumber);
222#else
223 name_buf[ARRAYSIZE(name_buf) - 1] = UNICODE_NULL;
224
225 _snwprintf(name_buf, ARRAYSIZE(name_buf) - 1,
227 DeviceExtension->DeviceNumber);
228#endif
229
230 letter = DeviceExtension->DriveLetter;
231
234 driver_extension->RegistryPath.Buffer,
235 name_buf,
236 REG_DWORD,
237 &letter,
238 sizeof(ULONG));
239
240 if (!NT_SUCCESS(status)) {
241 VFDTRACE(VFDWARN,
242 ("[VFD] RtlWriteRegistryValue - %s\n",
243 GetStatusName(status)));
244 }
245 else {
246 VFDTRACE(VFDINFO,
247 ("[VFD] Drive letter '%wc' stored into the registry\n",
248 letter ? letter : L' '));
249 }
250
251 VFDTRACE(VFDINFO,
252 ("[VFD] VfdStoreLink - %s\n", GetStatusName(status)));
253
254 return status;
255}
LONG NTSTATUS
Definition: precomp.h:26
#define CHAR(Char)
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
GLenum const GLfloat * params
Definition: glext.h:5645
NTSYSAPI NTSTATUS WINAPI RtlWriteRegistryValue(ULONG, PCWSTR, PCWSTR, ULONG, PVOID, ULONG)
NTSYSAPI NTSTATUS WINAPI RtlQueryRegistryValues(ULONG, PCWSTR, PRTL_QUERY_REGISTRY_TABLE, PVOID, PVOID)
int _snwprintf(wchar_t *buffer, size_t count, const wchar_t *format,...)
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define RTL_REGISTRY_ABSOLUTE
Definition: nt_native.h:161
#define RTL_QUERY_REGISTRY_DIRECT
Definition: nt_native.h:144
#define RTL_REGISTRY_OPTIONAL
Definition: nt_native.h:169
#define UNICODE_NULL
PVOID NTAPI IoGetDriverObjectExtension(IN PDRIVER_OBJECT DriverObject, IN PVOID ClientIdentificationAddress)
Definition: driver.c:1904
#define L(x)
Definition: ntvdm.h:50
#define REG_DWORD
Definition: sdbapi.c:596
int zero
Definition: sehframes.cpp:29
#define STATUS_SUCCESS
Definition: shellext.h:65
UNICODE_STRING RegistryPath
Definition: vfddrv.h:64
Definition: ps.c:97
uint16_t * PWSTR
Definition: typedefs.h:56
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_DRIVER_INTERNAL_ERROR
Definition: udferr_usr.h:177
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define STATUS_OBJECT_NAME_NOT_FOUND
Definition: udferr_usr.h:149
#define VFDTRACE(LEVEL, STRING)
Definition: vfddbg.h:72
ULONG OsMajorVersion
#define VFD_REG_DRIVE_LETTER
Definition: vfdio.h:30
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
Definition: wdfdriver.h:215
__wchar_t WCHAR
Definition: xmlstorage.h:180
char CHAR
Definition: xmlstorage.h:175