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 48 struct { 49 UINT64 DiskSize; 50 UINT32 DeviceNumber; 51 UINT32 BytesPerSector; 52 PARTITION_STYLE PartitionStyle; 53 union { 54 struct { 55 UINT32 Signature; 56 } Mbr; 57 struct { 58 GUID DiskId; 59 } Gpt; 60 }; 61 } DiskData; 62 } FDO_EXTENSION, *PFDO_EXTENSION; 63 64 typedef struct _PARTITION_EXTENSION 65 { 66 BOOLEAN IsFDO; 67 PDEVICE_OBJECT DeviceObject; 68 PDEVICE_OBJECT LowerDevice; 69 PDEVICE_OBJECT Part0Device; 70 71 UINT64 StartingOffset; 72 UINT64 PartitionLength; 73 SINGLE_LIST_ENTRY ListEntry; 74 75 UINT32 DetectedNumber; 76 UINT32 OnDiskNumber; // partition number for issuing Io requests to the kernel 77 BOOLEAN IsEnumerated; // reported via IRP_MN_QUERY_DEVICE_RELATIONS 78 BOOLEAN SymlinkCreated; 79 BOOLEAN DeviceRemoved; // !!! 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 NTSTATUS 104 PartitionCreateDevice( 105 _In_ PDEVICE_OBJECT FDObject, 106 _In_ PPARTITION_INFORMATION_EX PartitionEntry, 107 _In_ UINT32 OnDiskNumber, 108 _In_ PARTITION_STYLE PartitionStyle, 109 _Out_ PDEVICE_OBJECT *PDO); 110 111 NTSTATUS 112 PartitionHandleRemove( 113 _In_ PPARTITION_EXTENSION PartExt, 114 _In_ BOOLEAN FinalRemove); 115 116 NTSTATUS 117 PartitionHandlePnp( 118 _In_ PDEVICE_OBJECT DeviceObject, 119 _In_ PIRP Irp); 120 121 NTSTATUS 122 PartitionHandleDeviceControl( 123 _In_ PDEVICE_OBJECT DeviceObject, 124 _In_ PIRP Irp); 125 126 NTSTATUS 127 NTAPI 128 ForwardIrpAndForget( 129 _In_ PDEVICE_OBJECT DeviceObject, 130 _In_ PIRP Irp); 131 132 NTSTATUS 133 IssueSyncIoControlRequest( 134 _In_ UINT32 IoControlCode, 135 _In_ PDEVICE_OBJECT DeviceObject, 136 _In_ PVOID InputBuffer, 137 _In_ ULONG InputBufferLength, 138 _In_ PVOID OutputBuffer, 139 _In_ ULONG OutputBufferLength, 140 _In_ BOOLEAN InternalDeviceIoControl); 141 142 FORCEINLINE 143 BOOLEAN 144 VerifyIrpOutBufferSize( 145 _In_ PIRP Irp, 146 _In_ SIZE_T Size) 147 { 148 PIO_STACK_LOCATION ioStack = IoGetCurrentIrpStackLocation(Irp); 149 if (ioStack->Parameters.DeviceIoControl.OutputBufferLength < Size) 150 { 151 Irp->IoStatus.Information = Size; 152 return FALSE; 153 } 154 return TRUE; 155 } 156 157 FORCEINLINE 158 BOOLEAN 159 VerifyIrpInBufferSize( 160 _In_ PIRP Irp, 161 _In_ SIZE_T Size) 162 { 163 PIO_STACK_LOCATION ioStack = IoGetCurrentIrpStackLocation(Irp); 164 if (ioStack->Parameters.DeviceIoControl.InputBufferLength < Size) 165 { 166 Irp->IoStatus.Information = Size; 167 return FALSE; 168 } 169 return TRUE; 170 } 171 172 FORCEINLINE 173 VOID 174 PartMgrAcquireLayoutLock( 175 _In_ PFDO_EXTENSION FDOExtension) 176 { 177 PAGED_CODE(); 178 179 KeWaitForSingleObject(&FDOExtension->SyncEvent, Executive, KernelMode, FALSE, NULL); 180 } 181 182 FORCEINLINE 183 VOID 184 PartMgrReleaseLayoutLock( 185 _In_ PFDO_EXTENSION FDOExtension) 186 { 187 PAGED_CODE(); 188 189 KeSetEvent(&FDOExtension->SyncEvent, IO_NO_INCREMENT, FALSE); 190 } 191 192 #endif // _PARTMGR_H_ 193