Fixed minor bug.
[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.10 2002/03/20 19:54:06 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 DPRINT1("DiskClassCheckReadWrite() called\n");
335
336 return(STATUS_SUCCESS);
337 }
338
339
340 // DiskClassCreateDeviceObject
341 //
342 // DESCRIPTION:
343 // Create the raw device and any partition devices on this drive
344 //
345 // RUN LEVEL:
346 // PASSIVE_LEVEL
347 //
348 // ARGUMENTS:
349 // IN PDRIVER_OBJECT DriverObject The system created driver object
350 // IN PCONTROLLER_OBJECT ControllerObject
351 // IN PIDE_CONTROLLER_EXTENSION ControllerExtension
352 // The IDE controller extension for
353 // this device
354 // IN int DriveIdx The index of the drive on this
355 // controller
356 // IN int HarddiskIdx The NT device number for this
357 // drive
358 //
359 // RETURNS:
360 // TRUE Drive exists and devices were created
361 // FALSE no devices were created for this device
362 //
363
364 static NTSTATUS
365 DiskClassCreateDeviceObject(IN PDRIVER_OBJECT DriverObject,
366 IN PUNICODE_STRING RegistryPath, /* what's this used for? */
367 IN PDEVICE_OBJECT PortDeviceObject,
368 IN ULONG PortNumber,
369 IN ULONG DiskNumber,
370 IN PIO_SCSI_CAPABILITIES Capabilities,
371 IN PSCSI_INQUIRY_DATA InquiryData,
372 IN PCLASS_INIT_DATA InitializationData)
373 {
374 OBJECT_ATTRIBUTES ObjectAttributes;
375 UNICODE_STRING UnicodeDeviceDirName;
376 WCHAR NameBuffer[80];
377 CHAR NameBuffer2[80];
378 PDEVICE_OBJECT DiskDeviceObject;
379 PDEVICE_OBJECT PartitionDeviceObject;
380 PDEVICE_EXTENSION DiskDeviceExtension; /* defined in class2.h */
381 PDEVICE_EXTENSION PartitionDeviceExtension; /* defined in class2.h */
382 PDRIVE_LAYOUT_INFORMATION PartitionList = NULL;
383 HANDLE Handle;
384 PPARTITION_INFORMATION PartitionEntry;
385 PDISK_DATA DiskData;
386 ULONG PartitionNumber;
387 NTSTATUS Status;
388
389 DPRINT("DiskClassCreateDeviceObject() called\n");
390
391 /* Create the harddisk device directory */
392 swprintf(NameBuffer,
393 L"\\Device\\Harddisk%lu",
394 DiskNumber);
395 RtlInitUnicodeString(&UnicodeDeviceDirName,
396 NameBuffer);
397 InitializeObjectAttributes(&ObjectAttributes,
398 &UnicodeDeviceDirName,
399 0,
400 NULL,
401 NULL);
402 Status = ZwCreateDirectoryObject(&Handle,
403 0,
404 &ObjectAttributes);
405 if (!NT_SUCCESS(Status))
406 {
407 DbgPrint("Could not create device dir object\n");
408 return(Status);
409 }
410
411 /* Claim the disk device */
412 Status = ScsiClassClaimDevice(PortDeviceObject,
413 InquiryData,
414 FALSE,
415 &PortDeviceObject);
416 if (!NT_SUCCESS(Status))
417 {
418 DbgPrint("Could not claim disk device\n");
419
420 ZwMakeTemporaryObject(Handle);
421 ZwClose(Handle);
422
423 return(Status);
424 }
425
426 /* Create disk device (Partition 0) */
427 sprintf(NameBuffer2,
428 "\\Device\\Harddisk%lu\\Partition0",
429 DiskNumber);
430
431 Status = ScsiClassCreateDeviceObject(DriverObject,
432 NameBuffer2,
433 NULL,
434 &DiskDeviceObject,
435 InitializationData);
436 if (!NT_SUCCESS(Status))
437 {
438 DPRINT1("ScsiClassCreateDeviceObject() failed (Status %x)\n", Status);
439
440 /* Release (unclaim) the disk */
441 ScsiClassClaimDevice(PortDeviceObject,
442 InquiryData,
443 TRUE,
444 NULL);
445
446 /* Delete the harddisk device directory */
447 ZwMakeTemporaryObject(Handle);
448 ZwClose(Handle);
449
450 return(Status);
451 }
452
453 DiskDeviceObject->Flags |= DO_DIRECT_IO;
454 if (((PINQUIRYDATA)InquiryData->InquiryData)->RemovableMedia)
455 {
456 DiskDeviceObject->Characteristics |= FILE_REMOVABLE_MEDIA;
457 }
458 DiskDeviceObject->StackSize = (CCHAR)PortDeviceObject->StackSize + 1;
459
460 if (PortDeviceObject->AlignmentRequirement > DiskDeviceObject->AlignmentRequirement)
461 {
462 DiskDeviceObject->AlignmentRequirement = PortDeviceObject->AlignmentRequirement;
463 }
464
465 DiskDeviceExtension = DiskDeviceObject->DeviceExtension;
466 DiskDeviceExtension->LockCount = 0;
467 DiskDeviceExtension->DeviceNumber = DiskNumber;
468 DiskDeviceExtension->PortDeviceObject = PortDeviceObject;
469 DiskDeviceExtension->PhysicalDevice = DiskDeviceObject;
470
471 /* FIXME: Not yet! Will cause pointer corruption! */
472 // DiskDeviceExtension->PortCapabilities = PortCapabilities;
473
474 DiskDeviceExtension->StartingOffset.QuadPart = 0;
475 DiskDeviceExtension->PortNumber = (UCHAR)PortNumber;
476 DiskDeviceExtension->PathId = InquiryData->PathId;
477 DiskDeviceExtension->TargetId = InquiryData->TargetId;
478 DiskDeviceExtension->Lun = InquiryData->Lun;
479
480 /* zero-out disk data */
481 DiskData = (PDISK_DATA)(DiskDeviceExtension + 1);
482 RtlZeroMemory(DiskData,
483 sizeof(DISK_DATA));
484
485 /* Get disk geometry */
486 DiskDeviceExtension->DiskGeometry = ExAllocatePool(NonPagedPool,
487 sizeof(DISK_GEOMETRY));
488 if (DiskDeviceExtension->DiskGeometry == NULL)
489 {
490 DPRINT1("Failed to allocate geometry buffer!\n");
491
492 IoDeleteDevice(DiskDeviceObject);
493
494 /* Release (unclaim) the disk */
495 ScsiClassClaimDevice(PortDeviceObject,
496 InquiryData,
497 TRUE,
498 NULL);
499
500 /* Delete the harddisk device directory */
501 ZwMakeTemporaryObject(Handle);
502 ZwClose(Handle);
503
504 return(STATUS_INSUFFICIENT_RESOURCES);
505 }
506
507 /* Read the drive's capacity */
508 Status = ScsiClassReadDriveCapacity(DiskDeviceObject);
509 if (!NT_SUCCESS(Status) &&
510 (DiskDeviceObject->Characteristics & FILE_REMOVABLE_MEDIA) == 0)
511 {
512 DPRINT1("Failed to retrieve drive capacity!\n");
513 return(STATUS_SUCCESS);
514 }
515 else
516 {
517 /* Clear the verify flag for removable media drives. */
518 DiskDeviceObject->Flags &= ~DO_VERIFY_VOLUME;
519 }
520
521 DPRINT("SectorSize: %lu\n", DiskDeviceExtension->DiskGeometry->BytesPerSector);
522
523 /* Read partition table */
524 Status = IoReadPartitionTable(DiskDeviceObject,
525 DiskDeviceExtension->DiskGeometry->BytesPerSector,
526 TRUE,
527 &PartitionList);
528
529 DPRINT("IoReadPartitionTable(): Status: %lx\n", Status);
530
531 if ((!NT_SUCCESS(Status) || PartitionList->PartitionCount == 0) &&
532 DiskDeviceObject->Characteristics & FILE_REMOVABLE_MEDIA)
533 {
534 if (!NT_SUCCESS(Status))
535 {
536 /* Drive is not ready. */
537 DPRINT1("Drive not ready\n");
538 DiskData->DriveNotReady = TRUE;
539 }
540 else
541 {
542 ExFreePool(PartitionList);
543 }
544
545 /* Allocate a partition list for a single entry. */
546 PartitionList = ExAllocatePool(NonPagedPool,
547 sizeof(DRIVE_LAYOUT_INFORMATION));
548 if (PartitionList != NULL)
549 {
550 RtlZeroMemory(PartitionList,
551 sizeof(DRIVE_LAYOUT_INFORMATION));
552 PartitionList->PartitionCount = 1;
553
554 Status = STATUS_SUCCESS;
555 }
556 }
557
558 if (NT_SUCCESS(Status))
559 {
560 DPRINT("Read partition table!\n");
561
562 DPRINT(" Number of partitions: %u\n", PartitionList->PartitionCount);
563
564 for (PartitionNumber = 0; PartitionNumber < PartitionList->PartitionCount; PartitionNumber++)
565 {
566 PartitionEntry = &PartitionList->PartitionEntry[PartitionNumber];
567
568 DPRINT("Partition %02ld: nr: %d boot: %1x type: %x offset: %I64d size: %I64d\n",
569 PartitionNumber,
570 PartitionEntry->PartitionNumber,
571 PartitionEntry->BootIndicator,
572 PartitionEntry->PartitionType,
573 PartitionEntry->StartingOffset.QuadPart / 512 /*DrvParms.BytesPerSector*/,
574 PartitionEntry->PartitionLength.QuadPart / 512 /* DrvParms.BytesPerSector*/);
575
576 /* Create partition device object */
577 sprintf(NameBuffer2,
578 "\\Device\\Harddisk%lu\\Partition%lu",
579 DiskNumber,
580 PartitionNumber + 1);
581
582 Status = ScsiClassCreateDeviceObject(DriverObject,
583 NameBuffer2,
584 DiskDeviceObject,
585 &PartitionDeviceObject,
586 InitializationData);
587 DPRINT("ScsiClassCreateDeviceObject(): Status %x\n", Status);
588 if (NT_SUCCESS(Status))
589 {
590 PartitionDeviceObject->Flags = DiskDeviceObject->Flags;
591 PartitionDeviceObject->Characteristics = DiskDeviceObject->Characteristics;
592 PartitionDeviceObject->StackSize = DiskDeviceObject->StackSize;
593 PartitionDeviceObject->AlignmentRequirement = DiskDeviceObject->AlignmentRequirement;
594
595 PartitionDeviceExtension = PartitionDeviceObject->DeviceExtension;
596 PartitionDeviceExtension->LockCount = 0;
597 PartitionDeviceExtension->DeviceNumber = DiskNumber;
598 PartitionDeviceExtension->PortDeviceObject = PortDeviceObject;
599 PartitionDeviceExtension->DiskGeometry = DiskDeviceExtension->DiskGeometry;
600 PartitionDeviceExtension->PhysicalDevice = DiskDeviceExtension->PhysicalDevice;
601
602 /* FIXME: Not yet! Will cause pointer corruption! */
603 // PartitionDeviceExtension->PortCapabilities = PortCapabilities;
604
605 PartitionDeviceExtension->StartingOffset.QuadPart =
606 PartitionEntry->StartingOffset.QuadPart;
607 PartitionDeviceExtension->PartitionLength.QuadPart =
608 PartitionEntry->PartitionLength.QuadPart;
609 PartitionDeviceExtension->PortNumber = (UCHAR)PortNumber;
610 PartitionDeviceExtension->PathId = InquiryData->PathId;
611 PartitionDeviceExtension->TargetId = InquiryData->TargetId;
612 PartitionDeviceExtension->Lun = InquiryData->Lun;
613 PartitionDeviceExtension->SectorShift = DiskDeviceExtension->SectorShift;
614
615 DiskData = (PDISK_DATA)(PartitionDeviceExtension + 1);
616 DiskData->PartitionType = PartitionEntry->PartitionType;
617 DiskData->PartitionNumber = PartitionNumber + 1;
618 DiskData->HiddenSectors = PartitionEntry->HiddenSectors;
619 DiskData->BootIndicator = PartitionEntry->BootIndicator;
620 DiskData->DriveNotReady = FALSE;
621 }
622 else
623 {
624 DPRINT1("ScsiClassCreateDeviceObject() failed to create partition device object (Status %x)\n", Status);
625
626 break;
627 }
628 }
629
630
631 }
632
633 if (PartitionList != NULL)
634 ExFreePool(PartitionList);
635
636 DPRINT("DiskClassCreateDeviceObjects() done\n");
637
638 return(STATUS_SUCCESS);
639 }
640
641
642
643
644
645 // DiskClassDeviceControl
646 //
647 // DESCRIPTION:
648 // Answer requests for device control calls
649 //
650 // RUN LEVEL:
651 // PASSIVE_LEVEL
652 //
653 // ARGUMENTS:
654 // Standard dispatch arguments
655 //
656 // RETURNS:
657 // NTSTATUS
658 //
659
660 NTSTATUS STDCALL
661 DiskClassDeviceControl(IN PDEVICE_OBJECT DeviceObject,
662 IN PIRP Irp)
663 {
664 PDEVICE_EXTENSION DeviceExtension;
665 PIO_STACK_LOCATION IrpStack;
666 ULONG ControlCode, InputLength, OutputLength;
667 PDISK_DATA DiskData;
668 ULONG Information;
669 NTSTATUS Status;
670
671 DPRINT("DiskClassDeviceControl() called!\n");
672
673 Status = STATUS_INVALID_DEVICE_REQUEST;
674 Information = 0;
675 IrpStack = IoGetCurrentIrpStackLocation(Irp);
676 ControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
677 InputLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
678 OutputLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
679 DeviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
680 DiskData = (PDISK_DATA)(DeviceExtension + 1);
681
682 /* A huge switch statement in a Windows program?! who would have thought? */
683 switch (ControlCode)
684 {
685 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
686 DPRINT("IOCTL_DISK_GET_DRIVE_GEOMETRY\n");
687 if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength < sizeof(DISK_GEOMETRY))
688 {
689 Status = STATUS_INVALID_PARAMETER;
690 }
691 else if (DeviceExtension->DiskGeometry == NULL)
692 {
693 DPRINT1("No disk geometry available!\n");
694 Status = STATUS_NO_SUCH_DEVICE;
695 }
696 else
697 {
698 PDISK_GEOMETRY Geometry;
699
700 Geometry = (PDISK_GEOMETRY) Irp->AssociatedIrp.SystemBuffer;
701 RtlMoveMemory(Geometry,
702 DeviceExtension->DiskGeometry,
703 sizeof(DISK_GEOMETRY));
704
705 Status = STATUS_SUCCESS;
706 Information = sizeof(DISK_GEOMETRY);
707 }
708 break;
709
710 case IOCTL_DISK_GET_PARTITION_INFO:
711 DPRINT("IOCTL_DISK_GET_PARTITION_INFO\n");
712 if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength <
713 sizeof(PARTITION_INFORMATION))
714 {
715 Status = STATUS_INFO_LENGTH_MISMATCH;
716 }
717 else if (DiskData->PartitionNumber == 0)
718 {
719 Status = STATUS_INVALID_DEVICE_REQUEST;
720 }
721 else
722 {
723 PPARTITION_INFORMATION PartitionInfo;
724
725 PartitionInfo = (PPARTITION_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
726
727 PartitionInfo->PartitionType = DiskData->PartitionType;
728 PartitionInfo->StartingOffset = DeviceExtension->StartingOffset;
729 PartitionInfo->PartitionLength = DeviceExtension->PartitionLength;
730 PartitionInfo->HiddenSectors = DiskData->HiddenSectors;
731 PartitionInfo->PartitionNumber = DiskData->PartitionNumber;
732 PartitionInfo->BootIndicator = DiskData->BootIndicator;
733 PartitionInfo->RewritePartition = FALSE;
734 PartitionInfo->RecognizedPartition =
735 IsRecognizedPartition(DiskData->PartitionType);
736
737 Status = STATUS_SUCCESS;
738 Information = sizeof(PARTITION_INFORMATION);
739 }
740 break;
741
742 case IOCTL_DISK_SET_PARTITION_INFO:
743 DPRINT1("Unhandled IOCTL_DISK_SET_PARTITION_INFO\n");
744 Status = STATUS_INVALID_DEVICE_REQUEST;
745 Information = 0;
746 break;
747
748 case IOCTL_DISK_GET_DRIVE_LAYOUT:
749 if (IrpStack->Parameters.DeviceIoControl.OutputBufferLength <
750 sizeof(DRIVE_LAYOUT_INFORMATION))
751 {
752 Status = STATUS_BUFFER_TOO_SMALL;
753 }
754 else
755 {
756 PDRIVE_LAYOUT_INFORMATION PartitionList;
757
758 Status = IoReadPartitionTable(DeviceExtension->PhysicalDevice,
759 DeviceExtension->DiskGeometry->BytesPerSector,
760 FALSE,
761 &PartitionList);
762 if (NT_SUCCESS(Status))
763 {
764 ULONG BufferSize;
765
766 BufferSize = FIELD_OFFSET(DRIVE_LAYOUT_INFORMATION,
767 PartitionEntry[0]);
768 BufferSize += PartitionList->PartitionCount * sizeof(PARTITION_INFORMATION);
769
770 if (BufferSize > IrpStack->Parameters.DeviceIoControl.OutputBufferLength)
771 {
772 Status = STATUS_BUFFER_TOO_SMALL;
773 }
774 else
775 {
776 RtlMoveMemory(Irp->AssociatedIrp.SystemBuffer,
777 PartitionList,
778 BufferSize);
779 Status = STATUS_SUCCESS;
780 Information = BufferSize;
781 }
782 ExFreePool(PartitionList);
783 }
784 }
785 break;
786
787 case IOCTL_DISK_SET_DRIVE_LAYOUT:
788 case IOCTL_DISK_VERIFY:
789 case IOCTL_DISK_FORMAT_TRACKS:
790 case IOCTL_DISK_PERFORMANCE:
791 case IOCTL_DISK_IS_WRITABLE:
792 case IOCTL_DISK_LOGGING:
793 case IOCTL_DISK_FORMAT_TRACKS_EX:
794 case IOCTL_DISK_HISTOGRAM_STRUCTURE:
795 case IOCTL_DISK_HISTOGRAM_DATA:
796 case IOCTL_DISK_HISTOGRAM_RESET:
797 case IOCTL_DISK_REQUEST_STRUCTURE:
798 case IOCTL_DISK_REQUEST_DATA:
799
800 /* If we get here, something went wrong. inform the requestor */
801 default:
802 DPRINT1("Unhandled control code: %lx\n", ControlCode);
803 Status = STATUS_INVALID_DEVICE_REQUEST;
804 Information = 0;
805 break;
806 }
807
808 Irp->IoStatus.Status = Status;
809 Irp->IoStatus.Information = Information;
810 IoCompleteRequest(Irp,
811 IO_NO_INCREMENT);
812
813 return(Status);
814 }
815
816
817 /**********************************************************************
818 * NAME EXPORTED
819 * DiskClassShutdownFlush
820 *
821 * DESCRIPTION
822 * Answer requests for shutdown and flush calls.
823 *
824 * RUN LEVEL
825 * PASSIVE_LEVEL
826 *
827 * ARGUMENTS
828 * DeviceObject
829 * Pointer to the device.
830 *
831 * Irp
832 * Pointer to the IRP
833 *
834 * RETURN VALUE
835 * Status
836 */
837
838 NTSTATUS STDCALL
839 DiskClassShutdownFlush(IN PDEVICE_OBJECT DeviceObject,
840 IN PIRP Irp)
841 {
842 DPRINT("DiskClassShutdownFlush() called!\n");
843
844 Irp->IoStatus.Status = STATUS_SUCCESS;
845 Irp->IoStatus.Information = 0;
846 IoCompleteRequest(Irp, IO_NO_INCREMENT);
847
848 return(STATUS_SUCCESS);
849 }
850
851
852 /* EOF */