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