-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
98 lines (70 loc) · 2.11 KB
/
main.c
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
#include "defs.h"
#include "ioctls.h"
#include <mountmgr.h>
DRIVER_INITIALIZE DriverEntry;
DRIVER_UNLOAD DriverUnload;
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(INIT, DriverUnload)
PDEVICE_OBJECT pPdo = NULL;
PDEVICE_OBJECT pFdo = NULL;
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath) {
NTSTATUS status;
PDEVICE_OBJECT retVal;
UNICODE_STRING devName, registrationSymLink;
UNREFERENCED_PARAMETER(pRegistryPath);
TRACE("Driver Entry");
status = STATUS_SUCCESS;
pDriverObject->DriverUnload = DriverUnload;
// create PDO
status = IoReportDetectedDevice(pDriverObject, InterfaceTypeUndefined, -1, -1, NULL, NULL, FALSE, &pPdo);
if (!NT_SUCCESS(status)) {
TRACE("Error in IoReportDetectedDevice, code %x", status);
status = STATUS_UNSUCCESSFUL;
goto clean1;
}
// create FDO
RtlInitUnicodeString(&devName, L"\\Device\\VirtVol");
status = IoCreateDevice(pDriverObject, 0, &devName, FILE_DEVICE_DISK, 0, FALSE, &pFdo);
if (!NT_SUCCESS(status)) {
TRACE("Error in IoCreateDevice, code %x", status);
status = STATUS_UNSUCCESSFUL;
goto clean1;
}
pFdo->Flags |= DO_BUFFERED_IO;
pFdo->Flags &= (~DO_DEVICE_INITIALIZING);
// attach FDO to PDO
retVal = IoAttachDeviceToDeviceStack(pFdo, pPdo);
if (retVal == NULL) {
TRACE("Error in IoAttachDeviceToDeviceStack");
status = STATUS_UNSUCCESSFUL;
goto clean2;
}
// Ask for mount
status = IoRegisterDeviceInterface(pPdo, &MOUNTDEV_MOUNTED_DEVICE_GUID, NULL, ®istrationSymLink);
if (!NT_SUCCESS(status)) {
TRACE("Error in IoRegisterDeviceInterface, code %x", status);
status = STATUS_UNSUCCESSFUL;
goto clean3;
}
status = IoSetDeviceInterfaceState(®istrationSymLink, TRUE);
if (!NT_SUCCESS(status)) {
TRACE("Error in IoSetDeviceInterfaceState, code %x", status);
status = STATUS_UNSUCCESSFUL;
goto clean3;
}
initDispatchTable(pDriverObject);
goto exit;
clean3:
IoDetachDevice(pPdo);
clean2:
IoDeleteDevice(pFdo);
clean1:
IoDeleteDevice(pPdo);
exit:
return status;
}
VOID DriverUnload(PDRIVER_OBJECT DriverObject) {
TRACE("Driver Unload");
IoDeleteDevice(pPdo);
IoDeleteDevice(pFdo);
}