7ad5a19b1d5a3a3d5bee23b38fef45c9679a45c4
[reactos.git] / reactos / drivers / storage / disk / disk.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2001, 2002 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: disk.c,v 1.11 2002/03/22 20:35:09 ekohl Exp $
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * FILE: services/storage/disk/disk.c
24 * PURPOSE: disk class driver
25 * PROGRAMMER: Eric Kohl (ekohl@rz-online.de)
26 */
27
28 /* INCLUDES *****************************************************************/
29
30 #include <ddk/ntddk.h>
31
32 #include "../include/scsi.h"
33 #include "../include/class2.h"
34 #include "../include/ntddscsi.h"
35
36 #define NDEBUG
37 #include <debug.h>
38
39 #define VERSION "0.0.1"
40
41
42 typedef struct _DISK_DATA
43 {
44 ULONG HiddenSectors;
45 ULONG PartitionNumber;
46 UCHAR PartitionType;
47 BOOLEAN BootIndicator;
48 BOOLEAN DriveNotReady;
49 } DISK_DATA, *PDISK_DATA;
50
51
52 BOOLEAN STDCALL
53 DiskClassFindDevices(PDRIVER_OBJECT DriverObject,
54 PUNICODE_STRING RegistryPath,
55 PCLASS_INIT_DATA InitializationData,
56 PDEVICE_OBJECT PortDeviceObject,
57 ULONG PortNumber);
58
59 BOOLEAN STDCALL
60 DiskClassCheckDevice(IN PINQUIRYDATA InquiryData);
61
62 NTSTATUS STDCALL
63 DiskClassCheckReadWrite(IN PDEVICE_OBJECT DeviceObject,
64 IN PIRP Irp);
65
66
67 static NTSTATUS
68 DiskClassCreateDeviceObject(IN PDRIVER_OBJECT DriverObject,
69 IN PUNICODE_STRING RegistryPath, /* what's this used for? */
70 IN PDEVICE_OBJECT PortDeviceObject,
71 IN ULONG PortNumber,
72 IN ULONG DiskNumber,
73 IN PIO_SCSI_CAPABILITIES Capabilities,
74 IN PSCSI_INQUIRY_DATA InquiryData,
75 IN PCLASS_INIT_DATA InitializationData);
76
77 NTSTATUS STDCALL
78 DiskClassDeviceControl(IN PDEVICE_OBJECT DeviceObject,
79 IN PIRP Irp);
80
81 NTSTATUS STDCALL
82 DiskClassShutdownFlush(IN PDEVICE_OBJECT DeviceObject,
83 IN PIRP Irp);
84
85
86
87 /* FUNCTIONS ****************************************************************/
88
89 // DriverEntry
90 //
91 // DESCRIPTION:
92 // This function initializes the driver, locates and claims
93 // hardware resources, and creates various NT objects needed
94 // to process I/O requests.
95 //
96 // RUN LEVEL:
97 // PASSIVE_LEVEL
98 //
99 // ARGUMENTS:
100 // IN PDRIVER_OBJECT DriverObject System allocated Driver Object
101 // for this driver
102 // IN PUNICODE_STRING RegistryPath Name of registry driver service
103 // key
104 //
105 // RETURNS:
106 // NTSTATUS
107
108 NTSTATUS STDCALL
109 DriverEntry(IN PDRIVER_OBJECT DriverObject,
110 IN PUNICODE_STRING RegistryPath)
111 {
112 CLASS_INIT_DATA InitData;
113
114 DbgPrint("Disk Class Driver %s\n",
115 VERSION);
116 DPRINT("RegistryPath '%wZ'\n",
117 RegistryPath);
118
119 RtlZeroMemory(&InitData,
120 sizeof(CLASS_INIT_DATA));
121
122 InitData.InitializationDataSize = sizeof(CLASS_INIT_DATA);
123 InitData.DeviceExtensionSize = sizeof(DEVICE_EXTENSION) + sizeof(DISK_DATA);
124 InitData.DeviceType = FILE_DEVICE_DISK;
125 InitData.DeviceCharacteristics = 0;
126
127 InitData.ClassError = NULL; // DiskClassProcessError;
128 InitData.ClassReadWriteVerification = DiskClassCheckReadWrite;
129 InitData.ClassFindDeviceCallBack = DiskClassCheckDevice;
130 InitData.ClassFindDevices = DiskClassFindDevices;
131 InitData.ClassDeviceControl = DiskClassDeviceControl;
132 InitData.ClassShutdownFlush = DiskClassShutdownFlush;
133 InitData.ClassCreateClose = NULL;
134 InitData.ClassStartIo = NULL;
135
136 return(ScsiClassInitialize(DriverObject,
137 RegistryPath,
138 &InitData));
139 }
140
141
142 /**********************************************************************
143 * NAME EXPORTED
144 * DiskClassFindDevices
145 *
146 * DESCRIPTION
147 * This function searches for device that are attached to the
148 * given scsi port.
149 *
150 * RUN LEVEL
151 * PASSIVE_LEVEL
152 *
153 * ARGUMENTS
154 * DriverObject
155 * System allocated Driver Object for this driver
156 *
157 * RegistryPath
158 * Name of registry driver service key
159 *
160 * InitializationData
161 * Pointer to the main initialization data
162 *
163 * PortDeviceObject
164 * Pointer to the port Device Object
165 *
166 * PortNumber
167 * Port number
168 *
169 * RETURN VALUE
170 * TRUE: At least one disk drive was found
171 * FALSE: No disk drive found
172 */
173
174 BOOLEAN STDCALL
175 DiskClassFindDevices(PDRIVER_OBJECT DriverObject,
176 PUNICODE_STRING RegistryPath,
177 PCLASS_INIT_DATA InitializationData,
178 PDEVICE_OBJECT PortDeviceObject,
179 ULONG PortNumber)
180 {
181 PCONFIGURATION_INFORMATION ConfigInfo;
182 PIO_SCSI_CAPABILITIES PortCapabilities;
183 PSCSI_ADAPTER_BUS_INFO AdapterBusInfo;
184 PSCSI_INQUIRY_DATA UnitInfo;
185 PINQUIRYDATA InquiryData;
186 PCHAR Buffer;
187 ULONG Bus;
188 ULONG DeviceCount;
189 BOOLEAN FoundDevice;
190 NTSTATUS Status;
191
192 DPRINT("DiskClassFindDevices() called.\n");
193
194 /* Get port capabilities */
195 Status = ScsiClassGetCapabilities(PortDeviceObject,
196 &PortCapabilities);
197 if (!NT_SUCCESS(Status))
198 {
199 DPRINT("ScsiClassGetCapabilities() failed! (Status 0x%lX)\n", Status);
200 return(FALSE);
201 }
202
203 DPRINT("MaximumTransferLength: %lu\n", PortCapabilities->MaximumTransferLength);
204
205 /* Get inquiry data */
206 Status = ScsiClassGetInquiryData(PortDeviceObject,
207 (PSCSI_ADAPTER_BUS_INFO *)&Buffer);
208 if (!NT_SUCCESS(Status))
209 {
210 DPRINT("ScsiClassGetInquiryData() failed! (Status %x)\n", Status);
211 return(FALSE);
212 }
213
214 /* Check whether there are unclaimed devices */
215 AdapterBusInfo = (PSCSI_ADAPTER_BUS_INFO)Buffer;
216 DeviceCount = ScsiClassFindUnclaimedDevices(InitializationData,
217 AdapterBusInfo);
218 if (DeviceCount == 0)
219 {
220 DPRINT1("No unclaimed devices!\n");
221 return(FALSE);
222 }
223
224 DPRINT("Found %lu unclaimed devices!\n", DeviceCount);
225
226 ConfigInfo = IoGetConfigurationInformation();
227
228 /* Search each bus of this adapter */
229 for (Bus = 0; Bus < (ULONG)AdapterBusInfo->NumberOfBuses; Bus++)
230 {
231 DPRINT("Searching bus %lu\n", Bus);
232
233 UnitInfo = (PSCSI_INQUIRY_DATA)(Buffer + AdapterBusInfo->BusData[Bus].InquiryDataOffset);
234
235 while (AdapterBusInfo->BusData[Bus].InquiryDataOffset)
236 {
237 InquiryData = (PINQUIRYDATA)UnitInfo->InquiryData;
238
239 if (((InquiryData->DeviceType == DIRECT_ACCESS_DEVICE) ||
240 (InquiryData->DeviceType == OPTICAL_DEVICE)) &&
241 (InquiryData->DeviceTypeQualifier == 0) &&
242 (UnitInfo->DeviceClaimed == FALSE))
243 {
244 DPRINT("Vendor: '%.24s'\n",
245 InquiryData->VendorId);
246
247 /* Create device objects for disk */
248 Status = DiskClassCreateDeviceObject(DriverObject,
249 RegistryPath,
250 PortDeviceObject,
251 PortNumber,
252 ConfigInfo->DiskCount,
253 PortCapabilities,
254 UnitInfo,
255 InitializationData);
256 if (NT_SUCCESS(Status))
257 {
258 ConfigInfo->DiskCount++;
259 FoundDevice = TRUE;
260 }
261 }
262
263 if (UnitInfo->NextInquiryDataOffset == 0)
264 break;
265
266 UnitInfo = (PSCSI_INQUIRY_DATA)(Buffer + UnitInfo->NextInquiryDataOffset);
267 }
268 }
269
270 ExFreePool(Buffer);
271 ExFreePool(PortCapabilities);
272
273 DPRINT("DiskClassFindDevices() done\n");
274
275 return(FoundDevice);
276 }
277
278
279 /**********************************************************************
280 * NAME EXPORTED
281 * DiskClassCheckDevice
282 *
283 * DESCRIPTION
284 * This function checks the InquiryData for the correct device
285 * type and qualifier.
286 *
287 * RUN LEVEL
288 * PASSIVE_LEVEL
289 *
290 * ARGUMENTS
291 * InquiryData
292 * Pointer to the inquiry data for the device in question.
293 *
294 * RETURN VALUE
295 * TRUE: A disk device was found.
296 * FALSE: Otherwise.
297 */
298
299 BOOLEAN STDCALL
300 DiskClassCheckDevice(IN PINQUIRYDATA InquiryData)
301 {
302 return((InquiryData->DeviceType == DIRECT_ACCESS_DEVICE ||
303 InquiryData->DeviceType == OPTICAL_DEVICE) &&
304 InquiryData->DeviceTypeQualifier == 0);
305 }
306
307
308 /**********************************************************************
309 * NAME EXPORTED
310 * DiskClassCheckReadWrite
311 *
312 * DESCRIPTION
313 * This function checks the given IRP for correct data.
314 *
315 * RUN LEVEL
316 * PASSIVE_LEVEL
317 *
318 * ARGUMENTS
319 * DeviceObject
320 * Pointer to the device.
321 *
322 * Irp
323 * Irp to check.
324 *
325 * RETURN VALUE
326 * STATUS_SUCCESS: The IRP matches the requirements of the given device.
327 * Others: Failure.
328 */
329
330 NTSTATUS STDCALL
331 DiskClassCheckReadWrite(IN PDEVICE_OBJECT DeviceObject,
332 IN PIRP Irp)
333 {
334 PDEVICE_EXTENSION DeviceExtension;
335 PDISK_DATA DiskData;
336
337 DPRINT1("DiskClassCheckReadWrite() called\n");
338
339 DeviceExtension = DeviceObject->DeviceExtension;
340 DiskData = (PDISK_DATA)(DeviceExtension + 1);
341
342 if (DiskData->DriveNotReady == TRUE)
343 {
344 Irp->IoStatus.Status = STATUS_DEVICE_NOT_READY;
345 IoSetHardErrorOrVerifyDevice(Irp,
346 DeviceObject);
347 return(STATUS_INVALID_PARAMETER);
348 }
349
350 return(STATUS_SUCCESS);
351 }
352
353
354 // DiskClassCreateDeviceObject
355 //
356 // DESCRIPTION:
357 // Create the raw device and any partition devices on this drive
358 //
359 // RUN LEVEL:
360 // PASSIVE_LEVEL
361 //
362 // ARGUMENTS:
363 // IN PDRIVER_OBJECT DriverObject The system created driver object
364 // IN PCONTROLLER_OBJECT ControllerObject
365 // IN PIDE_CONTROLLER_EXTENSION ControllerExtension
366 // The IDE controller extension for
367 // this device
368 // IN int DriveIdx The index of the drive on this
369 // controller
370 // IN int HarddiskIdx The NT device number for this
371 // drive
372 //
373 // RETURNS:
374 // TRUE Drive exists and devices were created
375 // FALSE no devices were created for this device
376 //
377
378 static NTSTATUS
379 DiskClassCreateDeviceObject(IN PDRIVER_OBJECT DriverObject,
380 IN PUNICODE_STRING RegistryPath, /* what's this used for? */
381 IN PDEVICE_OBJECT PortDeviceObject,
382 IN ULONG PortNumber,
383 IN ULONG DiskNumber,
384 IN PIO_SCSI_CAPABILITIES Capabilities,
385 IN PSCSI_INQUIRY_DATA InquiryData,
386 IN PCLASS_INIT_DATA InitializationData)
387 {
388 OBJECT_ATTRIBUTES ObjectAttributes;
389 UNICODE_STRING UnicodeDeviceDirName;
390 WCHAR NameBuffer[80];
391 CHAR NameBuffer2[80];
392 PDEVICE_OBJECT DiskDeviceObject;
393 PDEVICE_OBJECT PartitionDeviceObject;
394 PDEVICE_EXTENSION DiskDeviceExtension; /* defined in class2.h */
395 PDEVICE_EXTENSION PartitionDeviceExtension; /* defined in class2.h */
396 PDRIVE_LAYOUT_INFORMATION PartitionList = NULL;
397 HANDLE Handle;
398 PPARTITION_INFORMATION PartitionEntry;
399 PDISK_DATA DiskData;
400 ULONG PartitionNumber;
401 NTSTATUS Status;
402
403 DPRINT("DiskClassCreateDeviceObject() called\n");
404
405 /* Create the harddisk device directory */
406 swprintf(NameBuffer,
407 L"\\Device\\Harddisk%lu",
408 DiskNumber);
409 RtlInitUnicodeString(&UnicodeDeviceDirName,
410 NameBuffer);
411 InitializeObjectAttributes(&ObjectAttributes,
412 &UnicodeDeviceDirName,
413 0,
414 NULL,
415 NULL);
416 Status = ZwCreateDirectoryObject(&Handle,
417 0,
418 &ObjectAttributes);
419 if (!NT_SUCCESS(Status))
420 {
421 DbgPrint("Could not create device dir object\n");
422 return(Status);
423 }
424
425 /* Claim the disk device */
426 Status = ScsiClassClaimDevice(PortDeviceObject,
427 InquiryData,
428 FALSE,
429 &PortDeviceObject);
430 if (!NT_SUCCESS(Status))
431 {
432 DbgPrint("Could not claim disk device\n");
433
434 ZwMakeTemporaryObject(Handle);
435 ZwClose(Handle);
436
437 return(Status);
438 }
439
440 /* Create disk device (Partition 0) */
441 sprintf(NameBuffer2,
442 "\\Device\\Harddisk%lu\\Partition0",
443 DiskNumber);
444
445 Status = ScsiClassCreateDeviceObject(DriverObject,
446 NameBuffer2,
447 NULL,
448 &DiskDeviceObject,
449 InitializationData);
450 if (!NT_SUCCESS(Status))
451 {
452 DPRINT1("ScsiClassCreateDeviceObject() failed (Status %x)\n", Status);
453
454 /* Release (unclaim) the disk */
455 ScsiClassClaimDevice(PortDeviceObject,
456 InquiryData,
457 TRUE,
458 NULL);
459
460 /* Delete the harddisk device directory */
461 ZwMakeTemporaryObject(Handle);
462 ZwClose(Handle);
463
464 return(Status);
465 }
466
467 DiskDeviceObject->Flags |= DO_DIRECT_IO;
468 if (((PINQUIRYDATA)InquiryData->InquiryData)->RemovableMedia)
469 {
470 DiskDeviceObject->Characteristics |= FILE_REMOVABLE_MEDIA;
471 }
472 DiskDeviceObject->StackSize = (CCHAR)PortDeviceObject->StackSize + 1;
473
474 if (PortDeviceObject->AlignmentRequirement > DiskDeviceObject->AlignmentRequirement)
475 {
476 DiskDeviceObject->AlignmentRequirement = PortDeviceObject->AlignmentRequirement;
477 }
478
479 DiskDeviceExtension = DiskDeviceObject->DeviceExtension;
480 DiskDeviceExtension->LockCount = 0;
481 DiskDeviceExtension->DeviceNumber = DiskNumber;
482 DiskDeviceExtension->PortDeviceObject = PortDeviceObject;
483 DiskDeviceExtension->PhysicalDevice = DiskDeviceObject;
484
485 /* FIXME: Not yet! Will cause pointer corruption! */
486 // DiskDeviceExtension->PortCapabilities = PortCapabilities;
487
488 DiskDeviceExtension->StartingOffset.QuadPart = 0;
489 DiskDeviceExtension->PortNumber = (UCHAR)PortNumber;
490 DiskDeviceExtension->PathId = InquiryData->PathId;
491 DiskDeviceExtension->TargetId = InquiryData->TargetId;
492 DiskDeviceExtension->Lun = InquiryData->Lun;
493
494 /* zero-out disk data */
495 DiskData = (PDISK_DATA)(DiskDeviceExtension + 1);
496 RtlZeroMemory(DiskData,
497 sizeof(DISK_DATA));
498
499 /* Get disk geometry */
500 DiskDeviceExtension->DiskGeometry = ExAllocatePool(NonPagedPool,
501 sizeof(DISK_GEOMETRY));
502 if (DiskDeviceExtension->DiskGeometry == NULL)
503 {
504 DPRINT1("Failed to allocate geometry buffer!\n");
505
506 IoDeleteDevice(DiskDeviceObject);
507
508 /* Release (unclaim) the disk */
509 ScsiClassClaimDevice(PortDeviceObject,
510 InquiryData,
511 TRUE,
512 NULL);
513
514 /* Delete the harddisk device directory */
515 ZwMakeTemporaryObject(Handle);
516 ZwClose(Handle);
517
518 return(STATUS_INSUFFICIENT_RESOURCES);
519 }
520
521 /* Read the drive's capacity */
522 Status = ScsiClassReadDriveCapacity(DiskDeviceObject);
523 if (!NT_SUCCESS(Status) &&
524 (DiskDeviceObject->Characteristics & FILE_REMOVABLE_MEDIA) == 0)
525 {
526 DPRINT1("Failed to retrieve drive capacity!\n");
527 return(STATUS_SUCCESS);
528 }
529 else
530 {
531 /* Clear the verify flag for removable media drives. */
532 DiskDeviceObject->Flags &= ~DO_VERIFY_VOLUME;
533 }
534
535 DPRINT("SectorSize: %lu\n", DiskDeviceExtension->DiskGeometry->BytesPerSector);
536
537 /* Read partition table */
538 Status = IoReadPartitionTable(DiskDeviceObject,
539 DiskDeviceExtension->DiskGeometry->BytesPerSector,
540 TRUE,
541 &PartitionList);
542
543 DPRINT("IoReadPartitionTable(): Status: %lx\n", Status);
544
545 if ((!NT_SUCCESS(Status) || PartitionList->PartitionCount == 0) &&
546 DiskDeviceObject->Characteristics & FILE_REMOVABLE_MEDIA)
547 {
548 if (!NT_SUCCESS(Status))
549 {
550 /* Drive is not ready. */
551 DPRINT1("Drive not ready\n");
552 DiskData->DriveNotReady = TRUE;
553 }
554 else
555 {
556 ExFreePool(PartitionList);
557 }
558
559 /* Allocate a partition list for a single entry. */
560 PartitionList = ExAllocatePool(NonPagedPool,
561 sizeof(DRIVE_LAYOUT_INFORMATION));
562 if (PartitionList != NULL)
563 {
564 RtlZeroMemory(PartitionList,
565 sizeof(DRIVE_LAYOUT_INFORMATION));
566 PartitionList->PartitionCount = 1;
567
568 Status = STATUS_SUCCESS;
569 }
570 }
571
572 if (NT_SUCCESS(Status))
573 {
574 DPRINT("Read partition table!\n");
575
576 DPRINT(" Number of partitions: %u\n", PartitionList->PartitionCount);
577
578 for (PartitionNumber = 0; PartitionNumber < PartitionList->PartitionCount; PartitionNumber++)
579 {
580 PartitionEntry = &PartitionList->PartitionEntry[PartitionNumber];
581
582 DPRINT("Partition %02ld: nr: %d boot: %1x type: %x offset: %I64d size: %I64d\n",
583 PartitionNumber,
584 PartitionEntry->PartitionNumber,
585 PartitionEntry->BootIndicator,
586 PartitionEntry->PartitionType,
587 PartitionEntry->StartingOffset.QuadPart / 512 /*DrvParms.BytesPerSector*/,
588 PartitionEntry->PartitionLength.QuadPart / 512 /* DrvParms.BytesPerSector*/);
589
590 /* Create partition device object */
591 sprintf(NameBuffer2,
592 "\\Device\\Harddisk%lu\\Partition%lu",
593 DiskNumber,
594 PartitionNumber + 1);
595
596 Status = ScsiClassCreateDeviceObject(DriverObject,
597 NameBuffer2,
598 DiskDeviceObject,
599 &PartitionDeviceObject,
600 InitializationData);
601 DPRINT("ScsiClassCreateDeviceObject(): Status %x\n", Status);
602 if (NT_SUCCESS(Status))
603 {
604 PartitionDeviceObject->Flags = DiskDeviceObject->Flags;
605 PartitionDeviceObject->Characteristics = DiskDeviceObject->Characteristics;
606 PartitionDeviceObject->StackSize = DiskDeviceObject->StackSize;
607 PartitionDeviceObject->AlignmentRequirement = DiskDeviceObject->AlignmentRequirement;
608
609 PartitionDeviceExtension = PartitionDeviceObject->DeviceExtension;
610 PartitionDeviceExtension->LockCount = 0;
611 PartitionDeviceExtension->DeviceNumber = DiskNumber;
612 PartitionDeviceExtension->PortDeviceObject = PortDeviceObject;
613 PartitionDeviceExtension->DiskGeometry = DiskDeviceExtension->DiskGeometry;
614 PartitionDeviceExtension->PhysicalDevice = DiskDeviceExtension->PhysicalDevice;
615
616 /* FIXME: Not yet! Will cause pointer corruption! */
617 // PartitionDeviceExtension->PortCapabilities = PortCapabilities;
618
619 PartitionDeviceExtension->StartingOffset.QuadPart =
620 PartitionEntry->StartingOffset.QuadPart;
621 PartitionDeviceExtension->PartitionLength.QuadPart =
622 PartitionEntry->PartitionLength.QuadPart;
623 PartitionDeviceExtension->PortNumber = (UCHAR)PortNumber;
624 PartitionDeviceExtension->PathId = InquiryData->PathId;
625 PartitionDeviceExtension->TargetId = InquiryData->TargetId;
626 PartitionDeviceExtension->Lun = InquiryData->Lun;
627 PartitionDeviceExtension->SectorShift = DiskDeviceExtension->SectorShift;
628
629 DiskData = (PDISK_DATA)(PartitionDeviceExtension + 1);
630 DiskData->PartitionType = PartitionEntry->PartitionType;
631 DiskData->PartitionNumber = PartitionNumber + 1;
632 DiskData->HiddenSectors = PartitionEntry->HiddenSectors;
633 DiskData->BootIndicator = PartitionEntry->BootIndicator;
634 DiskData->DriveNotReady = FALSE;
635 }
636 else
637 {
638 DPRINT1("ScsiClassCreateDeviceObject() failed to create partition device object (Status %x)\n", Status);
639
640 break;
641 }
642 }
643
644
645 }
646
647 if (PartitionList != NULL)
648 ExFreePool(PartitionList);
649
650 DPRINT("DiskClassCreateDeviceObjects() done\n");
651
652 return(STATUS_SUCCESS);
653 }
654
655
656
657
658
659 // DiskClassDeviceControl
660 //
661 // DESCRIPTION:
662 // Answer requests for device control calls
663 //
664 // RUN LEVEL:
665 // PASSIVE_LEVEL
666 //
667 // ARGUMENTS:
668 // Standard dispatch arguments
669 //
670 // RETURNS:
671 // NTSTATUS
672 //
673
674 NTSTATUS STDCALL
675 DiskClassDeviceControl(IN PDEVICE_OBJECT DeviceObject,
676 IN PIRP Irp)
677 {
678 PDEVICE_EXTENSION DeviceExtension;
679 PIO_STACK_LOCATION IrpStack;
680 ULONG ControlCode, InputLength, OutputLength;
681 PDISK_DATA DiskData;
682 ULONG Information;
683 NTSTATUS Status;
684
685 DPRINT("DiskClassDeviceControl() called!\n");
686
687 Status = STATUS_INVALID_DEVICE_REQUEST;
688 Information = 0;
689 IrpStack = IoGetCurrentIrpStackLocation(Irp);
690 ControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
691 InputLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
692 OutputLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
693 DeviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
694 DiskData = (PDISK_DATA)(DeviceExtension + 1);
695
696 /* A huge switch statement in a Windows program?! who would have thought? */
697 switch (ControlCode)
698 {
699 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
700 DPRINT("IOCTL_DISK_GET_DRIVE_GEOMETRY\n");
701 if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(DISK_GEOMETRY))
702 {
703 Status = STATUS_INVALID_PARAMETER;
704 }
705 else if (DeviceExtension->DiskGeometry == NULL)
706 {
707 DPRINT1("No disk geometry available!\n");
708 Status = STATUS_NO_SUCH_DEVICE;
709 }
710 else
711 {
712 PDISK_GEOMETRY Geometry;
713
714 Geometry = (PDISK_GEOMETRY) Irp->AssociatedIrp.SystemBuffer;
715 RtlMoveMemory(Geometry,
716 DeviceExtension->DiskGeometry,
717 sizeof(DISK_GEOMETRY));
718
719 Status = STATUS_SUCCESS;
720 Information = sizeof(DISK_GEOMETRY);
721 }
722 break;
723
724 case IOCTL_DISK_GET_PARTITION_INFO:
725 DPRINT("IOCTL_DISK_GET_PARTITION_INFO\n");
726 if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength <
727 sizeof(PARTITION_INFORMATION))
728 {
729 Status = STATUS_INFO_LENGTH_MISMATCH;
730 }
731 else if (DiskData->PartitionNumber == 0)
732 {
733 Status = STATUS_INVALID_DEVICE_REQUEST;
734 }
735 else
736 {
737 PPARTITION_INFORMATION PartitionInfo;
738
739 PartitionInfo = (PPARTITION_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
740
741 PartitionInfo->PartitionType = DiskData->PartitionType;
742 PartitionInfo->StartingOffset = DeviceExtension->StartingOffset;
743 PartitionInfo->PartitionLength = DeviceExtension->PartitionLength;
744 PartitionInfo->HiddenSectors = DiskData->HiddenSectors;
745 PartitionInfo->PartitionNumber = DiskData->PartitionNumber;
746 PartitionInfo->BootIndicator = DiskData->BootIndicator;
747 PartitionInfo->RewritePartition = FALSE;
748 PartitionInfo->RecognizedPartition =
749 IsRecognizedPartition(DiskData->PartitionType);
750
751 Status = STATUS_SUCCESS;
752 Information = sizeof(PARTITION_INFORMATION);
753 }
754 break;
755
756 case IOCTL_DISK_SET_PARTITION_INFO:
757 DPRINT1("Unhandled IOCTL_DISK_SET_PARTITION_INFO\n");
758 Status = STATUS_INVALID_DEVICE_REQUEST;
759 Information = 0;
760 break;
761
762 case IOCTL_DISK_GET_DRIVE_LAYOUT:
763 if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength <
764 sizeof(DRIVE_LAYOUT_INFORMATION))
765 {
766 Status = STATUS_BUFFER_TOO_SMALL;
767 }
768 else
769 {
770 PDRIVE_LAYOUT_INFORMATION PartitionList;
771
772 Status = IoReadPartitionTable(DeviceExtension->PhysicalDevice,
773 DeviceExtension->DiskGeometry->BytesPerSector,
774 FALSE,
775 &PartitionList);
776 if (NT_SUCCESS(Status))
777 {
778 ULONG BufferSize;
779
780 BufferSize = FIELD_OFFSET(DRIVE_LAYOUT_INFORMATION,
781 PartitionEntry[0]);
782 BufferSize += PartitionList->PartitionCount * sizeof(PARTITION_INFORMATION);
783
784 if (BufferSize > IrpStack->Parameters.DeviceIoControl.OutputBufferLength)
785 {
786 Status = STATUS_BUFFER_TOO_SMALL;
787 }
788 else
789 {
790 RtlMoveMemory(Irp->AssociatedIrp.SystemBuffer,
791 PartitionList,
792 BufferSize);
793 Status = STATUS_SUCCESS;
794 Information = BufferSize;
795 }
796 ExFreePool(PartitionList);
797 }
798 }
799 break;
800
801 case IOCTL_DISK_SET_DRIVE_LAYOUT:
802 case IOCTL_DISK_VERIFY:
803 case IOCTL_DISK_FORMAT_TRACKS:
804 case IOCTL_DISK_PERFORMANCE:
805 case IOCTL_DISK_IS_WRITABLE:
806 case IOCTL_DISK_LOGGING:
807 case IOCTL_DISK_FORMAT_TRACKS_EX:
808 case IOCTL_DISK_HISTOGRAM_STRUCTURE:
809 case IOCTL_DISK_HISTOGRAM_DATA:
810 case IOCTL_DISK_HISTOGRAM_RESET:
811 case IOCTL_DISK_REQUEST_STRUCTURE:
812 case IOCTL_DISK_REQUEST_DATA:
813
814 /* If we get here, something went wrong. inform the requestor */
815 default:
816 DPRINT1("Unhandled control code: %lx\n", ControlCode);
817 Status = STATUS_INVALID_DEVICE_REQUEST;
818 Information = 0;
819 break;
820 }
821
822 Irp->IoStatus.Status = Status;
823 Irp->IoStatus.Information = Information;
824 IoCompleteRequest(Irp,
825 IO_NO_INCREMENT);
826
827 return(Status);
828 }
829
830
831 /**********************************************************************
832 * NAME EXPORTED
833 * DiskClassShutdownFlush
834 *
835 * DESCRIPTION
836 * Answer requests for shutdown and flush calls.
837 *
838 * RUN LEVEL
839 * PASSIVE_LEVEL
840 *
841 * ARGUMENTS
842 * DeviceObject
843 * Pointer to the device.
844 *
845 * Irp
846 * Pointer to the IRP
847 *
848 * RETURN VALUE
849 * Status
850 */
851
852 NTSTATUS STDCALL
853 DiskClassShutdownFlush(IN PDEVICE_OBJECT DeviceObject,
854 IN PIRP Irp)
855 {
856 DPRINT("DiskClassShutdownFlush() called!\n");
857
858 Irp->IoStatus.Status = STATUS_SUCCESS;
859 Irp->IoStatus.Information = 0;
860 IoCompleteRequest(Irp, IO_NO_INCREMENT);
861
862 return(STATUS_SUCCESS);
863 }
864
865
866 /* EOF */