xref: /reactos/drivers/storage/partmgr/partmgr.h (revision 7e22dc05)
1 /*
2  * PROJECT:     Partition manager driver
3  * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE:     Main header
5  * COPYRIGHT:   2020 Victor Perevertkin (victor.perevertkin@reactos.org)
6  */
7 
8 #ifndef _PARTMGR_H_
9 #define _PARTMGR_H_
10 
11 #include <ntifs.h>
12 #include <mountdev.h>
13 #include <ntddvol.h>
14 #include <ntdddisk.h>
15 #include <ndk/psfuncs.h>
16 #include <ndk/section_attribs.h>
17 #include <ioevent.h>
18 #include <stdio.h>
19 #include <debug/driverdbg.h>
20 
21 #include "debug.h"
22 
23 #define TAG_PARTMGR 'MtrP'
24 
25 // from disk.sys
26 typedef struct _DISK_GEOMETRY_EX_INTERNAL
27 {
28     DISK_GEOMETRY Geometry;
29     INT64 DiskSize;
30     DISK_PARTITION_INFO Partition;
31     DISK_DETECTION_INFO Detection;
32 } DISK_GEOMETRY_EX_INTERNAL, *PDISK_GEOMETRY_EX_INTERNAL;
33 
34 typedef struct _FDO_EXTENSION
35 {
36     BOOLEAN IsFDO;
37     PDEVICE_OBJECT DeviceObject;
38     PDEVICE_OBJECT LowerDevice;
39     PDEVICE_OBJECT PhysicalDiskDO;
40     KEVENT SyncEvent;
41 
42     BOOLEAN LayoutValid;
43     PDRIVE_LAYOUT_INFORMATION_EX LayoutCache;
44 
45     SINGLE_LIST_ENTRY PartitionList;
46     UINT32 EnumeratedPartitionsTotal;
47     UNICODE_STRING DiskInterfaceName;
48 
49     struct {
50         UINT64 DiskSize;
51         UINT32 DeviceNumber;
52         UINT32 BytesPerSector;
53         PARTITION_STYLE PartitionStyle;
54         union {
55             struct {
56                 UINT32 Signature;
57             } Mbr;
58             struct {
59                 GUID DiskId;
60             } Gpt;
61         };
62     } DiskData;
63 } FDO_EXTENSION, *PFDO_EXTENSION;
64 
65 typedef struct _PARTITION_EXTENSION
66 {
67     BOOLEAN IsFDO;
68     PDEVICE_OBJECT DeviceObject;
69     PDEVICE_OBJECT LowerDevice;
70     PDEVICE_OBJECT Part0Device;
71 
72     UINT64 StartingOffset;
73     UINT64 PartitionLength;
74     SINGLE_LIST_ENTRY ListEntry;
75 
76     UINT32 DetectedNumber;
77     UINT32 OnDiskNumber; // partition number for issuing Io requests to the kernel
78     BOOLEAN IsEnumerated; // reported via IRP_MN_QUERY_DEVICE_RELATIONS
79     BOOLEAN SymlinkCreated;
80     BOOLEAN Attached; // attached to PartitionList of the FDO
81     union
82     {
83         struct
84         {
85             GUID PartitionType;
86             GUID PartitionId;
87             UINT64 Attributes;
88             WCHAR Name[36];
89         } Gpt;
90         struct
91         {
92             UINT8 PartitionType;
93             BOOLEAN BootIndicator;
94             BOOLEAN RecognizedPartition;
95             UINT32 HiddenSectors;
96         } Mbr;
97     };
98     UNICODE_STRING PartitionInterfaceName;
99     UNICODE_STRING VolumeInterfaceName;
100     UNICODE_STRING DeviceName;
101 } PARTITION_EXTENSION, *PPARTITION_EXTENSION;
102 
103 CODE_SEG("PAGE")
104 NTSTATUS
105 PartitionCreateDevice(
106     _In_ PDEVICE_OBJECT FDObject,
107     _In_ PPARTITION_INFORMATION_EX PartitionEntry,
108     _In_ UINT32 OnDiskNumber,
109     _In_ PARTITION_STYLE PartitionStyle,
110     _Out_ PDEVICE_OBJECT *PDO);
111 
112 CODE_SEG("PAGE")
113 NTSTATUS
114 PartitionHandleRemove(
115     _In_ PPARTITION_EXTENSION PartExt,
116     _In_ BOOLEAN FinalRemove);
117 
118 CODE_SEG("PAGE")
119 NTSTATUS
120 PartitionHandlePnp(
121     _In_ PDEVICE_OBJECT DeviceObject,
122     _In_ PIRP Irp);
123 
124 NTSTATUS
125 PartitionHandleDeviceControl(
126     _In_ PDEVICE_OBJECT DeviceObject,
127     _In_ PIRP Irp);
128 
129 NTSTATUS
130 NTAPI
131 ForwardIrpAndForget(
132     _In_ PDEVICE_OBJECT DeviceObject,
133     _In_ PIRP Irp);
134 
135 NTSTATUS
136 IssueSyncIoControlRequest(
137     _In_ UINT32 IoControlCode,
138     _In_ PDEVICE_OBJECT DeviceObject,
139     _In_ PVOID InputBuffer,
140     _In_ ULONG InputBufferLength,
141     _In_ PVOID OutputBuffer,
142     _In_ ULONG OutputBufferLength,
143     _In_ BOOLEAN InternalDeviceIoControl);
144 
145 FORCEINLINE
146 BOOLEAN
147 VerifyIrpOutBufferSize(
148     _In_ PIRP Irp,
149     _In_ SIZE_T Size)
150 {
151     PIO_STACK_LOCATION ioStack = IoGetCurrentIrpStackLocation(Irp);
152     if (ioStack->Parameters.DeviceIoControl.OutputBufferLength < Size)
153     {
154         Irp->IoStatus.Information = Size;
155         return FALSE;
156     }
157     return TRUE;
158 }
159 
160 FORCEINLINE
161 BOOLEAN
162 VerifyIrpInBufferSize(
163     _In_ PIRP Irp,
164     _In_ SIZE_T Size)
165 {
166     PIO_STACK_LOCATION ioStack = IoGetCurrentIrpStackLocation(Irp);
167     if (ioStack->Parameters.DeviceIoControl.InputBufferLength < Size)
168     {
169         Irp->IoStatus.Information = Size;
170         return FALSE;
171     }
172     return TRUE;
173 }
174 
175 FORCEINLINE
176 VOID
177 PartMgrAcquireLayoutLock(
178     _In_ PFDO_EXTENSION FDOExtension)
179 {
180     PAGED_CODE();
181 
182     KeWaitForSingleObject(&FDOExtension->SyncEvent, Executive, KernelMode, FALSE, NULL);
183 }
184 
185 FORCEINLINE
186 VOID
187 PartMgrReleaseLayoutLock(
188     _In_ PFDO_EXTENSION FDOExtension)
189 {
190     PAGED_CODE();
191 
192     KeSetEvent(&FDOExtension->SyncEvent, IO_NO_INCREMENT, FALSE);
193 }
194 
195 #endif // _PARTMGR_H_
196