ReactOS 0.4.15-dev-8109-gd7be748
boot.c File Reference
#include "btrfs_drv.h"
Include dependency graph for boot.c:

Go to the source code of this file.

Classes

struct  DEVOBJ_EXTENSION2
 

Macros

#define DOE_START_PENDING   0x10
 

Functions

static bool get_system_root ()
 
static void mountmgr_notification (BTRFS_UUID *uuid)
 
static void check_boot_options ()
 
void boot_add_device (DEVICE_OBJECT *pdo)
 
void check_system_root ()
 

Variables

ERESOURCE pdo_list_lock
 
LIST_ENTRY pdo_list
 
ERESOURCE boot_lock
 
PDRIVER_OBJECT drvobj
 
BTRFS_UUID boot_uuid
 
uint64_t boot_subvol = 0
 

Macro Definition Documentation

◆ DOE_START_PENDING

#define DOE_START_PENDING   0x10

Definition at line 37 of file boot.c.

Function Documentation

◆ boot_add_device()

void boot_add_device ( DEVICE_OBJECT pdo)

Definition at line 318 of file boot.c.

318 {
320
321 AddDevice(drvobj, pdo);
322
323 // To stop Windows sneakily setting DOE_START_PENDING
324 pdode->dont_report = true;
325
326 if (pdo->DeviceObjectExtension) {
327 ((DEVOBJ_EXTENSION2*)pdo->DeviceObjectExtension)->ExtensionFlags &= ~DOE_START_PENDING;
328
329 if (pdode && pdode->vde && pdode->vde->device)
330 ((DEVOBJ_EXTENSION2*)pdode->vde->device->DeviceObjectExtension)->ExtensionFlags &= ~DOE_START_PENDING;
331 }
332
334}
PDRIVER_OBJECT drvobj
Definition: btrfs.c:65
static void mountmgr_notification(BTRFS_UUID *uuid)
Definition: boot.c:180
DRIVER_ADD_DEVICE AddDevice
Definition: parport.h:72
PVOID DeviceExtension
Definition: env_spec_w32.h:418
PDEVICE_OBJECT device
Definition: btrfs_drv.h:875
volume_device_extension * vde
Definition: btrfs_drv.h:889

Referenced by add_volume_device(), and check_system_root().

◆ check_boot_options()

static void check_boot_options ( )
static

Definition at line 232 of file boot.c.

232 {
234 WCHAR* s;
235
236 static const WCHAR pathw[] = L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control";
237 static const WCHAR namew[] = L"SystemStartOptions";
238 static const WCHAR subvol[] = L"SUBVOL=";
239
240 _SEH2_TRY {
241 HANDLE control;
244 ULONG kvfilen = sizeof(KEY_VALUE_FULL_INFORMATION) - sizeof(WCHAR) + (255 * sizeof(WCHAR));
247 WCHAR* options;
248
249 path.Buffer = (WCHAR*)pathw;
250 path.Length = path.MaximumLength = sizeof(pathw) - sizeof(WCHAR);
251
253
254 Status = ZwOpenKey(&control, KEY_QUERY_VALUE, &oa);
255 if (!NT_SUCCESS(Status)) {
256 ERR("ZwOpenKey returned %08lx\n", Status);
257 return;
258 }
259
260 // FIXME - don't fail if value too long (can we query for the length?)
261
262 kvfi = ExAllocatePoolWithTag(PagedPool, kvfilen, ALLOC_TAG);
263 if (!kvfi) {
264 ERR("out of memory\n");
265 NtClose(control);
266 return;
267 }
268
269 name.Buffer = (WCHAR*)namew;
270 name.Length = name.MaximumLength = sizeof(namew) - sizeof(WCHAR);
271
272 Status = ZwQueryValueKey(control, &name, KeyValueFullInformation, kvfi,
273 kvfilen, &kvfilen);
274 if (!NT_SUCCESS(Status)) {
275 ERR("ZwQueryValueKey returned %08lx\n", Status);
276 NtClose(control);
277 return;
278 }
279
280 NtClose(control);
281
282 options = (WCHAR*)((uint8_t*)kvfi + kvfi->DataOffset);
283 options[kvfi->DataLength / sizeof(WCHAR)] = 0; // FIXME - make sure buffer long enough to allow this
284
285 s = wcsstr(options, subvol);
286
287 if (!s)
288 return;
289
290 s += (sizeof(subvol) / sizeof(WCHAR)) - 1;
291
292 boot_subvol = 0;
293
294 while (true) {
295 if (*s >= '0' && *s <= '9') {
296 boot_subvol <<= 4;
297 boot_subvol |= *s - '0';
298 } else if (*s >= 'a' && *s <= 'f') {
299 boot_subvol <<= 4;
300 boot_subvol |= *s - 'a' + 0xa;
301 } else if (*s >= 'A' && *s <= 'F') {
302 boot_subvol <<= 4;
303 boot_subvol |= *s - 'A' + 0xa;
304 } else
305 break;
306
307 s++;
308 }
310 return;
311 } _SEH2_END;
312
313 if (boot_subvol != 0) {
314 TRACE("passed subvol %I64x in boot options\n", boot_subvol);
315 }
316}
LONG NTSTATUS
Definition: precomp.h:26
#define ERR(fmt,...)
Definition: debug.h:113
#define ALLOC_TAG
Definition: btrfs_drv.h:87
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
uint64_t boot_subvol
Definition: boot.c:34
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define PagedPool
Definition: env_spec_w32.h:308
#define _SEH2_END
Definition: filesup.c:22
#define _SEH2_TRY
Definition: filesup.c:19
Status
Definition: gdiplustypes.h:25
GLdouble s
Definition: gl.h:2039
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
_CONST_RETURN wchar_t *__cdecl wcsstr(_In_z_ const wchar_t *_Str, _In_z_ const wchar_t *_SubStr)
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
BYTE uint8_t
Definition: msvideo1.c:66
struct _KEY_VALUE_FULL_INFORMATION KEY_VALUE_FULL_INFORMATION
@ KeyValueFullInformation
Definition: nt_native.h:1181
#define KEY_QUERY_VALUE
Definition: nt_native.h:1016
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
#define L(x)
Definition: ntvdm.h:50
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:66
#define TRACE(s)
Definition: solgame.cpp:4
Definition: name.c:39
uint32_t ULONG
Definition: typedefs.h:59
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by check_system_root().

◆ check_system_root()

void check_system_root ( )

Definition at line 336 of file boot.c.

336 {
337 LIST_ENTRY* le;
338 PDEVICE_OBJECT pdo_to_add = NULL;
339
340 TRACE("()\n");
341
342 // wait for any PNP notifications in progress to finish
345
346 if (!get_system_root())
347 return;
348
350
351 le = pdo_list.Flink;
352 while (le != &pdo_list) {
354
355 if (RtlCompareMemory(&pdode->uuid, &boot_uuid, sizeof(BTRFS_UUID)) == sizeof(BTRFS_UUID)) {
356 if (!pdode->vde)
357 pdo_to_add = pdode->pdo;
358 else if (pdode->vde->device && !(pdode->vde->device->Flags & DO_SYSTEM_BOOT_PARTITION)) { // AddDevice has beaten us to it
360
363
364 Status = IoSetDeviceInterfaceState(&pdode->vde->bus_name, false);
365 if (!NT_SUCCESS(Status))
366 ERR("IoSetDeviceInterfaceState returned %08lx\n", Status);
367
369 if (!NT_SUCCESS(Status))
370 ERR("IoSetDeviceInterfaceState returned %08lx\n", Status);
371 }
372
373 break;
374 }
375
376 le = le->Flink;
377 }
378
380
382
383 // If our FS depends on volumes that aren't there when we do our IoRegisterPlugPlayNotification calls
384 // in DriverEntry, bus_query_device_relations won't get called until it's too late. We need to do our
385 // own call to AddDevice here as a result. We need to clear the DOE_START_PENDING bits, or NtOpenFile
386 // will return STATUS_NO_SUCH_DEVICE.
387 if (pdo_to_add)
388 boot_add_device(pdo_to_add);
389}
#define TRUE
Definition: types.h:120
BTRFS_UUID boot_uuid
Definition: boot.c:33
static bool get_system_root()
Definition: boot.c:49
LIST_ENTRY pdo_list
Definition: btrfs.c:104
void boot_add_device(DEVICE_OBJECT *pdo)
Definition: boot.c:318
ERESOURCE pdo_list_lock
Definition: btrfs.c:103
static void check_boot_options()
Definition: boot.c:232
ERESOURCE boot_lock
Definition: btrfs.c:110
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
#define ExAcquireResourceExclusiveLite(res, wait)
Definition: env_spec_w32.h:615
#define DO_SYSTEM_BOOT_PARTITION
Definition: env_spec_w32.h:400
#define ExAcquireResourceSharedLite(res, wait)
Definition: env_spec_w32.h:621
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1822
NTSTATUS NTAPI IoSetDeviceInterfaceState(IN PUNICODE_STRING SymbolicLinkName, IN BOOLEAN Enable)
Definition: deviface.c:1311
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
UNICODE_STRING bus_name
Definition: btrfs_drv.h:879
Definition: list.h:27
PDEVICE_OBJECT pdo
Definition: btrfs_drv.h:890
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260

Referenced by _Function_class_().

◆ get_system_root()

static bool get_system_root ( )
static

Definition at line 49 of file boot.c.

49 {
51 HANDLE h;
53 OBJECT_ATTRIBUTES objatt;
54 ULONG retlen = 0;
55 bool second_time = false;
56
57 static const WCHAR system_root[] = L"\\SystemRoot";
58 static const WCHAR boot_device[] = L"\\Device\\BootDevice";
59 static const WCHAR arc_btrfs_prefix[] = L"\\ArcName\\btrfs(";
60
61 us.Buffer = (WCHAR*)system_root;
62 us.Length = us.MaximumLength = sizeof(system_root) - sizeof(WCHAR);
63
65
66 while (true) {
68 if (!NT_SUCCESS(Status)) {
69 ERR("ZwOpenSymbolicLinkObject returned %08lx\n", Status);
70 return false;
71 }
72
73 target.Length = target.MaximumLength = 0;
74
75 Status = ZwQuerySymbolicLinkObject(h, &target, &retlen);
77 ERR("ZwQuerySymbolicLinkObject returned %08lx\n", Status);
78 NtClose(h);
79 return false;
80 }
81
82 if (retlen == 0) {
83 NtClose(h);
84 return false;
85 }
86
88 if (!target.Buffer) {
89 ERR("out of memory\n");
90 NtClose(h);
91 return false;
92 }
93
94 target.Length = target.MaximumLength = (USHORT)retlen;
95
96 Status = ZwQuerySymbolicLinkObject(h, &target, NULL);
97 if (!NT_SUCCESS(Status)) {
98 ERR("ZwQuerySymbolicLinkObject returned %08lx\n", Status);
99 NtClose(h);
100 ExFreePool(target.Buffer);
101 return false;
102 }
103
104 NtClose(h);
105
106 if (second_time) {
107 TRACE("boot device is %.*S\n", (int)(target.Length / sizeof(WCHAR)), target.Buffer);
108 } else {
109 TRACE("system root is %.*S\n", (int)(target.Length / sizeof(WCHAR)), target.Buffer);
110 }
111
112 if (!second_time && target.Length >= sizeof(boot_device) - sizeof(WCHAR) &&
113 RtlCompareMemory(target.Buffer, boot_device, sizeof(boot_device) - sizeof(WCHAR)) == sizeof(boot_device) - sizeof(WCHAR)) {
114 ExFreePool(target.Buffer);
115
116 us.Buffer = (WCHAR*)boot_device;
117 us.Length = us.MaximumLength = sizeof(boot_device) - sizeof(WCHAR);
118
119 second_time = true;
120 } else
121 break;
122 }
123
124 if (target.Length >= sizeof(arc_btrfs_prefix) - sizeof(WCHAR) &&
125 RtlCompareMemory(target.Buffer, arc_btrfs_prefix, sizeof(arc_btrfs_prefix) - sizeof(WCHAR)) == sizeof(arc_btrfs_prefix) - sizeof(WCHAR)) {
126 WCHAR* s = &target.Buffer[(sizeof(arc_btrfs_prefix) / sizeof(WCHAR)) - 1];
127
128 for (unsigned int i = 0; i < 16; i++) {
129 if (*s >= '0' && *s <= '9')
130 boot_uuid.uuid[i] = (*s - '0') << 4;
131 else if (*s >= 'a' && *s <= 'f')
132 boot_uuid.uuid[i] = (*s - 'a' + 0xa) << 4;
133 else if (*s >= 'A' && *s <= 'F')
134 boot_uuid.uuid[i] = (*s - 'A' + 0xa) << 4;
135 else {
136 ExFreePool(target.Buffer);
137 return false;
138 }
139
140 s++;
141
142 if (*s >= '0' && *s <= '9')
143 boot_uuid.uuid[i] |= *s - '0';
144 else if (*s >= 'a' && *s <= 'f')
145 boot_uuid.uuid[i] |= *s - 'a' + 0xa;
146 else if (*s >= 'A' && *s <= 'F')
147 boot_uuid.uuid[i] |= *s - 'A' + 0xa;
148 else {
149 ExFreePool(target.Buffer);
150 return false;
151 }
152
153 s++;
154
155 if (i == 3 || i == 5 || i == 7 || i == 9) {
156 if (*s != '-') {
157 ExFreePool(target.Buffer);
158 return false;
159 }
160
161 s++;
162 }
163 }
164
165 if (*s != ')') {
166 ExFreePool(target.Buffer);
167 return false;
168 }
169
170 ExFreePool(target.Buffer);
171
172 return true;
173 }
174
175 ExFreePool(target.Buffer);
176
177 return false;
178}
#define GENERIC_READ
Definition: compat.h:135
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define NonPagedPool
Definition: env_spec_w32.h:307
GLenum target
Definition: glext.h:7315
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
static const BYTE us[]
Definition: encode.c:689
NTSYSAPI NTSTATUS NTAPI ZwOpenSymbolicLinkObject(_Out_ PHANDLE SymbolicLinkHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes)
unsigned short USHORT
Definition: pedump.c:61
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
uint8_t uuid[16]
Definition: btrfs.h:140

Referenced by check_system_root().

◆ mountmgr_notification()

static void mountmgr_notification ( BTRFS_UUID uuid)
static

Definition at line 180 of file boot.c.

180 {
181 UNICODE_STRING mmdevpath;
185 ULONG mmtnlen;
187 WCHAR* w;
188
191 if (!NT_SUCCESS(Status)) {
192 ERR("IoGetDeviceObjectPointer returned %08lx\n", Status);
193 return;
194 }
195
196 mmtnlen = offsetof(MOUNTMGR_TARGET_NAME, DeviceName[0]) + sizeof(BTRFS_VOLUME_PREFIX) + (36 * sizeof(WCHAR));
197
199 if (!mmtn) {
200 ERR("out of memory\n");
201 return;
202 }
203
204 mmtn->DeviceNameLength = sizeof(BTRFS_VOLUME_PREFIX) + (36 * sizeof(WCHAR));
205
207
208 w = &mmtn->DeviceName[(sizeof(BTRFS_VOLUME_PREFIX) / sizeof(WCHAR)) - 1];
209
210 for (unsigned int i = 0; i < 16; i++) {
211 *w = hex_digit(uuid->uuid[i] >> 4); w++;
212 *w = hex_digit(uuid->uuid[i] & 0xf); w++;
213
214 if (i == 3 || i == 5 || i == 7 || i == 9) {
215 *w = L'-';
216 w++;
217 }
218 }
219
220 *w = L'}';
221
223 if (!NT_SUCCESS(Status)) {
224 ERR("IOCTL_MOUNTMGR_VOLUME_ARRIVAL_NOTIFICATION returned %08lx\n", Status);
225 ExFreePool(mmtn);
226 return;
227 }
228
229 ExFreePool(mmtn);
230}
#define BTRFS_VOLUME_PREFIX
Definition: btrfs_drv.h:127
#define hex_digit(c)
Definition: btrfs_drv.h:1748
NTSTATUS dev_ioctl(_In_ PDEVICE_OBJECT DeviceObject, _In_ ULONG ControlCode, _In_reads_bytes_opt_(InputBufferSize) PVOID InputBuffer, _In_ ULONG InputBufferSize, _Out_writes_bytes_opt_(OutputBufferSize) PVOID OutputBuffer, _In_ ULONG OutputBufferSize, _In_ bool Override, _Out_opt_ IO_STATUS_BLOCK *iosb)
Definition: btrfs.c:2954
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:6102
Definition: msctf.idl:550
#define MOUNTMGR_DEVICE_NAME
Definition: imports.h:76
#define IOCTL_MOUNTMGR_VOLUME_ARRIVAL_NOTIFICATION
Definition: imports.h:130
#define FILE_READ_ATTRIBUTES
Definition: nt_native.h:647
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSTATUS NTAPI IoGetDeviceObjectPointer(IN PUNICODE_STRING ObjectName, IN ACCESS_MASK DesiredAccess, OUT PFILE_OBJECT *FileObject, OUT PDEVICE_OBJECT *DeviceObject)
Definition: device.c:1435
#define offsetof(TYPE, MEMBER)
USHORT DeviceNameLength
Definition: imports.h:154
WCHAR DeviceName[1]
Definition: imports.h:155
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
_In_ WDFREQUEST _In_ WDFFILEOBJECT FileObject
Definition: wdfdevice.h:550
_Must_inspect_result_ _In_ PWDFDEVICE_INIT _In_opt_ PCUNICODE_STRING DeviceName
Definition: wdfdevice.h:3275
* PFILE_OBJECT
Definition: iotypes.h:1998

Referenced by boot_add_device().

Variable Documentation

◆ boot_lock

ERESOURCE boot_lock
extern

Definition at line 110 of file btrfs.c.

Referenced by check_system_root().

◆ boot_subvol

uint64_t boot_subvol = 0

Definition at line 34 of file boot.c.

Referenced by check_boot_options(), and mount_vol().

◆ boot_uuid

BTRFS_UUID boot_uuid

◆ drvobj

PDRIVER_OBJECT drvobj
extern

Definition at line 65 of file btrfs.c.

Referenced by boot_add_device().

◆ pdo_list

LIST_ENTRY pdo_list
extern

Definition at line 104 of file btrfs.c.

Referenced by check_system_root().

◆ pdo_list_lock

ERESOURCE pdo_list_lock
extern

Definition at line 103 of file btrfs.c.

Referenced by check_system_root().