ReactOS  0.4.14-dev-98-gb0d4763
blockdev.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS File System Recognizer
4  * FILE: drivers/filesystems/fs_rec/blockdev.c
5  * PURPOSE: Generic Helper Functions
6  * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7  * Eric Kohl
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include "fs_rec.h"
13 
14 #include <ntdddisk.h>
15 #include <ntddcdrm.h>
16 
17 #define NDEBUG
18 #include <debug.h>
19 
20 /* FUNCTIONS ****************************************************************/
21 
22 BOOLEAN
23 NTAPI
27 {
30  KEVENT Event;
31  PIRP Irp;
34  PAGED_CODE();
35 
36  /* Only needed for disks */
37  if (DeviceObject->DeviceType != FILE_DEVICE_DISK) return FALSE;
38 
39  /* Build the information IRP */
43  NULL,
44  0,
46  sizeof(PARTITION_INFORMATION),
47  FALSE,
48  &Event,
49  &IoStatusBlock);
50  if (!Irp) return FALSE;
51 
52  /* Override verification */
54 
55  /* Do the request */
57  if (Status == STATUS_PENDING)
58  {
59  /* Wait for completion */
61  Executive,
62  KernelMode,
63  FALSE,
64  NULL);
66  }
67 
68  /* Fail if we couldn't get the data */
69  if (!NT_SUCCESS(Status)) return FALSE;
70 
71  /* Otherwise, return the number of sectors */
73  SectorSize,
74  &Remainder);
75  return TRUE;
76 }
77 
78 BOOLEAN
79 NTAPI
82 {
83  DISK_GEOMETRY DiskGeometry;
85  KEVENT Event;
86  PIRP Irp;
89  PAGED_CODE();
90 
91  /* Check what device we have */
92  switch (DeviceObject->DeviceType)
93  {
94  case FILE_DEVICE_CD_ROM:
95 
96  /* Use the CD IOCTL */
98  break;
99 
100  case FILE_DEVICE_DISK:
101 
102  /* Use the Disk IOCTL */
104  break;
105 
106  default:
107 
108  /* Invalid device type */
109  return FALSE;
110  }
111 
112  /* Build the information IRP */
115  DeviceObject,
116  NULL,
117  0,
118  &DiskGeometry,
119  sizeof(DISK_GEOMETRY),
120  FALSE,
121  &Event,
122  &IoStatusBlock);
123  if (!Irp) return FALSE;
124 
125  /* Override verification */
127 
128  /* Do the request */
130  if (Status == STATUS_PENDING)
131  {
132  /* Wait for completion */
134  Executive,
135  KernelMode,
136  FALSE,
137  NULL);
139  }
140 
141  /* Fail if we couldn't get the data */
142  if (!NT_SUCCESS(Status)) return FALSE;
143 
144  /* Return the sector size if it's valid */
145  if (!DiskGeometry.BytesPerSector) return FALSE;
146  *SectorSize = DiskGeometry.BytesPerSector;
147  return TRUE;
148 }
149 
150 BOOLEAN
151 NTAPI
154  IN ULONG Length,
156  IN OUT PVOID *Buffer,
157  OUT PBOOLEAN DeviceError OPTIONAL)
158 {
160  KEVENT Event;
161  PIRP Irp;
163  PAGED_CODE();
164 
165  /* Assume failure */
166  if (DeviceError) *DeviceError = FALSE;
167 
168  /* Check if the caller requested too little */
169  if (Length < SectorSize)
170  {
171  /* Read at least the sector size */
172  Length = SectorSize;
173  }
174  else
175  {
176  /* Otherwise, just round up the request to sector size */
178  }
179 
180  /* Check if the caller gave us a buffer */
181  if (!*Buffer)
182  {
183  /* He didn't, allocate one */
186  FSREC_TAG);
187  if (!*Buffer) return FALSE;
188  }
189 
190  /* Build the IRP */
193  DeviceObject,
194  *Buffer,
195  Length,
196  Offset,
197  &Event,
198  &IoStatusBlock);
199  if (!Irp) return FALSE;
200 
201  /* Override verification */
203 
204  /* Do the request */
206  if (Status == STATUS_PENDING)
207  {
208  /* Wait for completion */
210  Executive,
211  KernelMode,
212  FALSE,
213  NULL);
215  }
216 
217  /* Check if we couldn't get the data */
218  if (!NT_SUCCESS(Status))
219  {
220  /* Check if caller wanted to know about the device and fail */
221  if (DeviceError) *DeviceError = TRUE;
222  return FALSE;
223  }
224 
225  /* All went well */
226  return TRUE;
227 }
#define IN
Definition: typedefs.h:38
#define FILE_DEVICE_DISK
Definition: winioctl.h:112
#define TRUE
Definition: types.h:120
#define ROUND_UP(n, align)
Definition: eventvwr.h:31
PIRP NTAPI IoBuildSynchronousFsdRequest(IN ULONG MajorFunction, IN PDEVICE_OBJECT DeviceObject, IN PVOID Buffer, IN ULONG Length, IN PLARGE_INTEGER StartingOffset, IN PKEVENT Event, IN PIO_STATUS_BLOCK IoStatusBlock)
Definition: irp.c:1069
_In_ PIRP Irp
Definition: csq.h:116
BOOLEAN NTAPI FsRecGetDeviceSectorSize(IN PDEVICE_OBJECT DeviceObject, OUT PULONG SectorSize)
Definition: blockdev.c:80
LONG NTSTATUS
Definition: precomp.h:26
ULONG BytesPerSector
Definition: ntdddisk.h:376
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define PAGED_CODE()
Definition: video.h:57
#define FILE_DEVICE_CD_ROM
Definition: winioctl.h:107
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
_In_ PVOID _In_ ULONG Event
Definition: iotypes.h:435
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define SL_OVERRIDE_VERIFY_VOLUME
Definition: iotypes.h:1780
NTSYSAPI LONGLONG WINAPI RtlExtendedLargeIntegerDivide(LONGLONG, INT, INT *)
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
Definition: wmitypes.h:55
Definition: bufpool.h:45
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_PENDING
Definition: ntstatus.h:82
#define IOCTL_CDROM_GET_DRIVE_GEOMETRY
Definition: ntddcdrm.h:46
BOOLEAN NTAPI FsRecGetDeviceSectors(IN PDEVICE_OBJECT DeviceObject, IN ULONG SectorSize, OUT PLARGE_INTEGER SectorCount)
Definition: blockdev.c:24
_In_ ULONG _In_ struct _SET_PARTITION_INFORMATION_EX * PartitionInfo
Definition: iofuncs.h:2101
#define FSREC_TAG
Definition: fs_rec.h:16
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
char * PBOOLEAN
Definition: retypes.h:11
#define IOCTL_DISK_GET_PARTITION_INFO
Definition: ntdddisk.h:88
ULONG SectorCount
Definition: part_xbox.c:32
__drv_aliasesMem FORCEINLINE PIO_STACK_LOCATION IoGetNextIrpStackLocation(_In_ PIRP Irp)
Definition: iofuncs.h:2647
Status
Definition: gdiplustypes.h:24
BOOLEAN NTAPI FsRecReadBlock(IN PDEVICE_OBJECT DeviceObject, IN PLARGE_INTEGER Offset, IN ULONG Length, IN ULONG SectorSize, IN OUT PVOID *Buffer, OUT PBOOLEAN DeviceError OPTIONAL)
Definition: blockdev.c:152
IN PDEVICE_OBJECT DeviceObject
Definition: fatprocs.h:1560
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define ROUND_TO_PAGES(Size)
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
unsigned int * PULONG
Definition: retypes.h:1
NTSTATUS NTAPI IoCallDriver(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
Definition: irp.c:1218
PIRP NTAPI IoBuildDeviceIoControlRequest(IN ULONG IoControlCode, IN PDEVICE_OBJECT DeviceObject, IN PVOID InputBuffer, IN ULONG InputBufferLength, IN PVOID OutputBuffer, IN ULONG OutputBufferLength, IN BOOLEAN InternalDeviceIoControl, IN PKEVENT Event, IN PIO_STATUS_BLOCK IoStatusBlock)
Definition: irp.c:881
#define IRP_MJ_READ
Definition: rdpdr.c:46
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
_In_ ULONG SectorSize
Definition: halfuncs.h:291
#define IOCTL_DISK_GET_DRIVE_GEOMETRY
Definition: cdrw_usr.h:169
_In_ LARGE_INTEGER _Out_opt_ PLARGE_INTEGER Remainder
Definition: rtlfuncs.h:3046
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68