2 * PROJECT: ReactOS Storage Stack
3 * LICENSE: DDK - see license.txt in the root dir
4 * FILE: drivers/storage/disk/disk.c
5 * PURPOSE: Disk class driver
6 * PROGRAMMERS: Based on a source code sample from Microsoft NT4 DDK
15 #include <include/class2.h>
21 #define IO_WRITE_CACHE_ENABLED ((NTSTATUS)0x80040020L)
22 #define IO_WRITE_CACHE_DISABLED ((NTSTATUS)0x80040022L)
28 #define ExAllocatePool(a,b) ExAllocatePoolWithTag(a,b,'DscS')
35 } PARTITION_LIST_STATE
;
41 typedef struct _DISK_DATA
{
47 PDEVICE_EXTENSION NextPartition
;
50 // Disk signature (from MBR)
62 // Number of hidden sectors for BPB.
68 // Partition number of this device object
70 // This field is set during driver initialization or when the partition
71 // is created to identify a parition to the system.
74 ULONG PartitionNumber
;
77 // This field is the ordinal of a partition as it appears on a disk.
80 ULONG PartitionOrdinal
;
83 // Partition type of this device object
85 // This field is set by:
87 // 1) Initially set according to the partition list entry partition
88 // type returned by IoReadPartitionTable.
90 // 2) Subsequently set by the IOCTL_DISK_SET_PARTITION_INFORMATION
91 // I/O control function when IoSetPartitionInformation function
92 // successfully updates the partition type on the disk.
98 // Boot indicator - indicates whether this partition is a bootable (active)
99 // partition for this device
101 // This field is set according to the partition list entry boot indicator
102 // returned by IoReadPartitionTable.
105 BOOLEAN BootIndicator
;
108 // DriveNotReady - inidicates that the this device is currenly not ready
109 // because there is no media in the device.
112 BOOLEAN DriveNotReady
;
115 // State of PartitionList initialization
118 PARTITION_LIST_STATE PartitionListState
;
120 } DISK_DATA
, *PDISK_DATA
;
123 // Define a general structure of identfing disk controllers with bad
127 typedef struct _BAD_CONTROLLER_INFORMATION
{
129 BOOLEAN DisableTaggedQueuing
;
130 BOOLEAN DisableSynchronousTransfers
;
131 BOOLEAN DisableDisconnects
;
132 BOOLEAN DisableWriteCache
;
133 }BAD_CONTROLLER_INFORMATION
, *PBAD_CONTROLLER_INFORMATION
;
135 BAD_CONTROLLER_INFORMATION
const ScsiDiskBadControllers
[] = {
136 { "TOSHIBA MK538FB 60", TRUE
, FALSE
, FALSE
, FALSE
},
137 { "CONNER CP3500", FALSE
, TRUE
, FALSE
, FALSE
},
138 { "OLIVETTICP3500", FALSE
, TRUE
, FALSE
, FALSE
},
139 { "SyQuest SQ5110 CHC", TRUE
, TRUE
, FALSE
, FALSE
},
140 { "SEAGATE ST41601N 0102", FALSE
, TRUE
, FALSE
, FALSE
},
141 { "SEAGATE ST3655N", FALSE
, FALSE
, FALSE
, TRUE
},
142 { "SEAGATE ST3390N", FALSE
, FALSE
, FALSE
, TRUE
},
143 { "SEAGATE ST12550N", FALSE
, FALSE
, FALSE
, TRUE
},
144 { "SEAGATE ST32430N", FALSE
, FALSE
, FALSE
, TRUE
},
145 { "SEAGATE ST31230N", FALSE
, FALSE
, FALSE
, TRUE
},
146 { "SEAGATE ST15230N", FALSE
, FALSE
, FALSE
, TRUE
},
147 { "FUJITSU M2652S-512", TRUE
, FALSE
, FALSE
, FALSE
},
148 { "MAXTOR MXT-540SL I1.2", TRUE
, FALSE
, FALSE
, FALSE
},
149 { "COMPAQ PD-1", FALSE
, TRUE
, FALSE
, FALSE
}
153 #define NUMBER_OF_BAD_CONTROLLERS (sizeof(ScsiDiskBadControllers) / sizeof(BAD_CONTROLLER_INFORMATION))
154 #define DEVICE_EXTENSION_SIZE sizeof(DEVICE_EXTENSION) + sizeof(DISK_DATA)
156 #define MODE_DATA_SIZE 192
157 #define VALUE_BUFFER_SIZE 2048
158 #define SCSI_DISK_TIMEOUT 10
159 #define PARTITION0_LIST_SIZE 4
165 IN PDRIVER_OBJECT DriverObject
,
166 IN PUNICODE_STRING RegistryPath
171 ScsiDiskDeviceVerification(
172 IN PINQUIRYDATA InquiryData
178 IN PDRIVER_OBJECT DriveObject
,
179 IN PUNICODE_STRING RegistryPath
,
180 IN PCLASS_INIT_DATA InitializationData
,
181 IN PDEVICE_OBJECT PortDeviceObject
,
187 ScsiDiskCreateClose (
188 IN PDEVICE_OBJECT DeviceObject
,
194 ScsiDiskReadWriteVerification(
195 IN PDEVICE_OBJECT DeviceObject
,
201 ScsiDiskDeviceControl(
202 IN PDEVICE_OBJECT DeviceObject
,
208 ScsiDiskProcessError(
209 PDEVICE_OBJECT DeviceObject
,
210 PSCSI_REQUEST_BLOCK Srb
,
217 ScsiDiskShutdownFlush(
218 IN PDEVICE_OBJECT DeviceObject
,
225 IN PDEVICE_OBJECT DeviceObject
,
226 IN PSCSI_INQUIRY_DATA LunInfo
232 IN PDEVICE_OBJECT DeviceObject
,
233 IN PCHAR ModeSelectBuffer
,
241 IN PDEVICE_OBJECT DeviceObject
246 CalculateMbrCheckSum(
247 IN PDEVICE_EXTENSION DeviceExtension
,
254 IN PDEVICE_EXTENSION DeviceExtension
,
262 IN PDEVICE_EXTENSION DeviceExtension
267 UpdateRemovableGeometry (
268 IN PDEVICE_OBJECT DeviceObject
,
274 CreateDiskDeviceObject(
275 IN PDRIVER_OBJECT DriverObject
,
276 IN PUNICODE_STRING RegistryPath
,
277 IN PDEVICE_OBJECT PortDeviceObject
,
279 IN PULONG DeviceCount
,
280 IN PIO_SCSI_CAPABILITIES PortCapabilities
,
281 IN PSCSI_INQUIRY_DATA LunInfo
,
282 IN PCLASS_INIT_DATA InitData
287 CreatePartitionDeviceObjects(
288 IN PDEVICE_OBJECT PhysicalDeviceObject
,
289 IN PUNICODE_STRING RegistryPath
295 IN PDEVICE_OBJECT DeviceObject
,
302 PDEVICE_OBJECT DeviceObject
,
303 PSCSI_INQUIRY_DATA LunInfo
,
304 PIO_SCSI_CAPABILITIES PortCapabilities
310 IN PDEVICE_OBJECT DeviceObject
314 #pragma alloc_text(PAGE, DriverEntry)
315 #pragma alloc_text(PAGE, FindScsiDisks)
316 #pragma alloc_text(PAGE, CreateDiskDeviceObject)
317 #pragma alloc_text(PAGE, CalculateMbrCheckSum)
318 #pragma alloc_text(PAGE, EnumerateBusKey)
319 #pragma alloc_text(PAGE, UpdateGeometry)
320 #pragma alloc_text(PAGE, IsFloppyDevice)
321 #pragma alloc_text(PAGE, ScanForSpecial)
322 #pragma alloc_text(PAGE, ScsiDiskDeviceControl)
323 #pragma alloc_text(PAGE, ScsiDiskModeSelect)
330 IN PDRIVER_OBJECT DriverObject
,
331 IN PUNICODE_STRING RegistryPath
338 This routine initializes the SCSI hard disk class driver.
342 DriverObject - Pointer to driver object created by system.
344 RegistryPath - Pointer to the name of the services node for this driver.
348 The function value is the final status from the initialization operation.
353 CLASS_INIT_DATA InitializationData
;
359 RtlZeroMemory (&InitializationData
, sizeof(CLASS_INIT_DATA
));
365 InitializationData
.InitializationDataSize
= sizeof(CLASS_INIT_DATA
);
366 InitializationData
.DeviceExtensionSize
= DEVICE_EXTENSION_SIZE
;
368 InitializationData
.DeviceType
= FILE_DEVICE_DISK
;
369 InitializationData
.DeviceCharacteristics
= 0;
375 InitializationData
.ClassError
= ScsiDiskProcessError
;
376 InitializationData
.ClassReadWriteVerification
= ScsiDiskReadWriteVerification
;
377 InitializationData
.ClassFindDevices
= FindScsiDisks
;
378 InitializationData
.ClassFindDeviceCallBack
= ScsiDiskDeviceVerification
;
379 InitializationData
.ClassDeviceControl
= ScsiDiskDeviceControl
;
380 InitializationData
.ClassShutdownFlush
= ScsiDiskShutdownFlush
;
381 InitializationData
.ClassCreateClose
= NULL
;
384 // Call the class init routine
387 return ScsiClassInitialize( DriverObject
, RegistryPath
, &InitializationData
);
389 } // end DriverEntry()
395 ScsiDiskDeviceVerification(
396 IN PINQUIRYDATA InquiryData
403 This routine checks InquiryData for the correct device type and qualifier.
407 InquiryData - Pointer to the inquiry data for the device in question.
411 True is returned if the correct device type is found.
416 if (((InquiryData
->DeviceType
== DIRECT_ACCESS_DEVICE
) ||
417 (InquiryData
->DeviceType
== OPTICAL_DEVICE
)) &&
418 InquiryData
->DeviceTypeQualifier
== 0) {
431 IN PDRIVER_OBJECT DriverObject
,
432 IN PUNICODE_STRING RegistryPath
,
433 IN PCLASS_INIT_DATA InitializationData
,
434 IN PDEVICE_OBJECT PortDeviceObject
,
442 This routine gets a port drivers capabilities, obtains the
443 inquiry data, searches the SCSI bus for the port driver and creates
444 the device objects for the disks found.
448 DriverObject - Pointer to driver object created by system.
450 PortDeviceObject - Device object use to send requests to port driver.
452 PortNumber - Number for port driver. Used to pass on to
453 CreateDiskDeviceObjects() and create device objects.
457 True is returned if one disk was found and successfully created.
462 PIO_SCSI_CAPABILITIES portCapabilities
;
464 PCONFIGURATION_INFORMATION configurationInformation
;
466 PSCSI_INQUIRY_DATA lunInfo
;
467 PSCSI_ADAPTER_BUS_INFO adapterInfo
;
468 PINQUIRYDATA inquiryData
;
472 BOOLEAN foundOne
= FALSE
;
477 // Call port driver to get adapter capabilities.
480 status
= ScsiClassGetCapabilities(PortDeviceObject
, &portCapabilities
);
482 if (!NT_SUCCESS(status
)) {
483 DebugPrint((1,"FindScsiDevices: ScsiClassGetCapabilities failed\n"));
488 // Call port driver to get inquiry information to find disks.
491 status
= ScsiClassGetInquiryData(PortDeviceObject
, (PSCSI_ADAPTER_BUS_INFO
*) &buffer
);
493 if (!NT_SUCCESS(status
)) {
494 DebugPrint((1,"FindScsiDevices: ScsiClassGetInquiryData failed\n"));
499 // Do a quick scan of the devices on this adapter to determine how many
500 // disks are on this adapter. This is used to determine the number of
501 // SRB zone elements to allocate.
505 adapterInfo
= (PVOID
) buffer
;
507 adapterDisk
= ScsiClassFindUnclaimedDevices(InitializationData
, adapterInfo
);
510 // Allocate a zone of SRB for disks on this adapter.
513 if (adapterDisk
== 0) {
516 // No free disks were found.
523 // Get the number of disks already initialized.
526 configurationInformation
= IoGetConfigurationInformation();
527 diskCount
= &configurationInformation
->DiskCount
;
530 // For each SCSI bus this adapter supports ...
533 for (scsiBus
=0; scsiBus
< (ULONG
)adapterInfo
->NumberOfBuses
; scsiBus
++) {
536 // Get the SCSI bus scan data for this bus.
539 lunInfo
= (PVOID
) (buffer
+ adapterInfo
->BusData
[scsiBus
].InquiryDataOffset
);
542 // Search list for unclaimed disk devices.
545 while (adapterInfo
->BusData
[scsiBus
].InquiryDataOffset
) {
547 inquiryData
= (PVOID
)lunInfo
->InquiryData
;
549 if (((inquiryData
->DeviceType
== DIRECT_ACCESS_DEVICE
) ||
550 (inquiryData
->DeviceType
== OPTICAL_DEVICE
)) &&
551 inquiryData
->DeviceTypeQualifier
== 0 &&
552 (!lunInfo
->DeviceClaimed
)) {
555 "FindScsiDevices: Vendor string is %.24s\n",
556 inquiryData
->VendorId
));
559 // Create device objects for disk
562 status
= CreateDiskDeviceObject(DriverObject
,
571 if (NT_SUCCESS(status
)) {
574 // Increment system disk device count.
587 if (lunInfo
->NextInquiryDataOffset
== 0) {
591 lunInfo
= (PVOID
) (buffer
+ lunInfo
->NextInquiryDataOffset
);
597 // Buffer is allocated by ScsiClassGetInquiryData and must be free returning.
604 } // end FindScsiDisks()
609 CreateDiskDeviceObject(
610 IN PDRIVER_OBJECT DriverObject
,
611 IN PUNICODE_STRING RegistryPath
,
612 IN PDEVICE_OBJECT PortDeviceObject
,
614 IN PULONG DeviceCount
,
615 IN PIO_SCSI_CAPABILITIES PortCapabilities
,
616 IN PSCSI_INQUIRY_DATA LunInfo
,
617 IN PCLASS_INIT_DATA InitData
624 This routine creates an object for the physical device and then searches
625 the device for partitions and creates an object for each partition.
629 DriverObject - Pointer to driver object created by system.
631 PortDeviceObject - Miniport device object.
633 PortNumber - port number. Used in creating disk objects.
635 DeviceCount - Number of previously installed devices.
637 PortCapabilities - Capabilities of this SCSI port.
639 LunInfo - LUN specific information.
647 CCHAR ntNameBuffer
[MAXIMUM_FILENAME_LENGTH
];
649 UNICODE_STRING ntUnicodeString
;
650 OBJECT_ATTRIBUTES objectAttributes
;
653 PDEVICE_OBJECT deviceObject
= NULL
;
654 PDEVICE_OBJECT physicalDevice
;
655 PDISK_GEOMETRY_EX diskGeometry
= NULL
;
656 PDEVICE_EXTENSION deviceExtension
= NULL
;
657 PDEVICE_EXTENSION physicalDeviceExtension
;
658 UCHAR pathId
= LunInfo
->PathId
;
659 UCHAR targetId
= LunInfo
->TargetId
;
660 UCHAR lun
= LunInfo
->Lun
;
662 PVOID senseData
= NULL
;
665 BOOLEAN srbListInitialized
= FALSE
;
671 // Set up an object directory to contain the objects for this
672 // device and all its partitions.
675 sprintf(ntNameBuffer
,
676 "\\Device\\Harddisk%lu",
679 RtlInitString(&ntNameString
,
682 status
= RtlAnsiStringToUnicodeString(&ntUnicodeString
,
686 if (!NT_SUCCESS(status
)) {
690 InitializeObjectAttributes(&objectAttributes
,
692 OBJ_CASE_INSENSITIVE
| OBJ_PERMANENT
,
696 status
= ZwCreateDirectoryObject(&handle
,
697 DIRECTORY_ALL_ACCESS
,
700 RtlFreeUnicodeString(&ntUnicodeString
);
702 if (!NT_SUCCESS(status
)) {
705 "CreateDiskDeviceObjects: Could not create directory %s\n",
715 status
= ScsiClassClaimDevice(PortDeviceObject
,
720 if (!NT_SUCCESS(status
)) {
721 ZwMakeTemporaryObject(handle
);
727 // Create a device object for this device. Each physical disk will
728 // have at least one device object. The required device object
729 // describes the entire device. Its directory path is
730 // \Device\HarddiskN\Partition0, where N = device number.
733 sprintf(ntNameBuffer
,
734 "\\Device\\Harddisk%lu\\Partition0",
738 status
= ScsiClassCreateDeviceObject(DriverObject
,
744 if (!NT_SUCCESS(status
)) {
747 "CreateDiskDeviceObjects: Can not create device object %s\n",
750 goto CreateDiskDeviceObjectsExit
;
754 // Indicate that IRPs should include MDLs for data transfers.
757 deviceObject
->Flags
|= DO_DIRECT_IO
;
760 // Check if this is during initialization. If not indicate that
761 // system initialization already took place and this disk is ready
766 deviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
770 // Check for removable media support.
773 if (((PINQUIRYDATA
)LunInfo
->InquiryData
)->RemovableMedia
) {
774 deviceObject
->Characteristics
|= FILE_REMOVABLE_MEDIA
;
778 // Set up required stack size in device object.
781 deviceObject
->StackSize
= (CCHAR
)PortDeviceObject
->StackSize
+ 1;
783 deviceExtension
= deviceObject
->DeviceExtension
;
786 // Allocate spinlock for split request completion.
789 KeInitializeSpinLock(&deviceExtension
->SplitRequestSpinLock
);
792 // Initialize lock count to zero. The lock count is used to
793 // disable the ejection mechanism on devices that support
794 // removable media. Only the lock count in the physical
795 // device extension is used.
798 deviceExtension
->LockCount
= 0;
801 // Save system disk number.
804 deviceExtension
->DeviceNumber
= *DeviceCount
;
807 // Copy port device object pointer to the device extension.
810 deviceExtension
->PortDeviceObject
= PortDeviceObject
;
813 // Set the alignment requirements for the device based on the
814 // host adapter requirements
817 if (PortDeviceObject
->AlignmentRequirement
> deviceObject
->AlignmentRequirement
) {
818 deviceObject
->AlignmentRequirement
= PortDeviceObject
->AlignmentRequirement
;
822 // This is the physical device object.
825 physicalDevice
= deviceObject
;
826 physicalDeviceExtension
= deviceExtension
;
829 // Save address of port driver capabilities.
832 deviceExtension
->PortCapabilities
= PortCapabilities
;
835 // Build the lookaside list for srb's for the physical disk. Should only
839 ScsiClassInitializeSrbLookasideList(deviceExtension
,
840 PARTITION0_LIST_SIZE
);
842 srbListInitialized
= TRUE
;
845 // Initialize the srb flags.
848 if (((PINQUIRYDATA
)LunInfo
->InquiryData
)->CommandQueue
&&
849 PortCapabilities
->TaggedQueuing
) {
851 deviceExtension
->SrbFlags
= SRB_FLAGS_QUEUE_ACTION_ENABLE
;
855 deviceExtension
->SrbFlags
= 0;
860 // Allow queued requests if this is not removable media.
863 if (!(deviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
)) {
865 deviceExtension
->SrbFlags
|= SRB_FLAGS_NO_QUEUE_FREEZE
;
870 // Look for controller that require special flags.
873 ScanForSpecial(deviceObject
,
877 srbFlags
= deviceExtension
->SrbFlags
;
880 // Allocate buffer for drive geometry.
883 diskGeometry
= ExAllocatePool(NonPagedPool
, sizeof(DISK_GEOMETRY_EX
));
885 if (diskGeometry
== NULL
) {
888 "CreateDiskDeviceObjects: Can not allocate disk geometry buffer\n"));
889 status
= STATUS_INSUFFICIENT_RESOURCES
;
890 goto CreateDiskDeviceObjectsExit
;
893 deviceExtension
->DiskGeometry
= diskGeometry
;
896 // Allocate request sense buffer.
899 senseData
= ExAllocatePool(NonPagedPoolCacheAligned
, SENSE_BUFFER_SIZE
);
901 if (senseData
== NULL
) {
904 // The buffer can not be allocated.
908 "CreateDiskDeviceObjects: Can not allocate request sense buffer\n"));
910 status
= STATUS_INSUFFICIENT_RESOURCES
;
911 goto CreateDiskDeviceObjectsExit
;
915 // Set the sense data pointer in the device extension.
918 deviceExtension
->SenseData
= senseData
;
921 // Physical device object will describe the entire
922 // device, starting at byte offset 0.
925 deviceExtension
->StartingOffset
.QuadPart
= (LONGLONG
)(0);
928 // TargetId/LUN describes a device location on the SCSI bus.
929 // This information comes from the inquiry buffer.
932 deviceExtension
->PortNumber
= (UCHAR
)PortNumber
;
933 deviceExtension
->PathId
= pathId
;
934 deviceExtension
->TargetId
= targetId
;
935 deviceExtension
->Lun
= lun
;
938 // Set timeout value in seconds.
941 timeOut
= ScsiClassQueryTimeOutRegistryValue(RegistryPath
);
943 deviceExtension
->TimeOutValue
= timeOut
;
945 deviceExtension
->TimeOutValue
= SCSI_DISK_TIMEOUT
;
949 // Back pointer to device object.
952 deviceExtension
->DeviceObject
= deviceObject
;
955 // If this is a removable device, then make sure it is not a floppy.
956 // Perform a mode sense command to determine the media type. Note
957 // IsFloppyDevice also checks for write cache enabled.
960 if (IsFloppyDevice(deviceObject
) && deviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
&&
961 (((PINQUIRYDATA
)LunInfo
->InquiryData
)->DeviceType
== DIRECT_ACCESS_DEVICE
)) {
963 status
= STATUS_NO_SUCH_DEVICE
;
964 goto CreateDiskDeviceObjectsExit
;
967 DisableWriteCache(deviceObject
,LunInfo
);
969 writeCache
= deviceExtension
->DeviceFlags
& DEV_WRITE_CACHE
;
972 // NOTE: At this point one device object has been successfully created.
973 // from here on out return success.
977 // Do READ CAPACITY. This SCSI command
978 // returns the number of bytes on a device.
979 // Device extension is updated with device size.
982 status
= ScsiClassReadDriveCapacity(deviceObject
);
985 // If the read capcity failed then just return, unless this is a
986 // removable disk where a device object partition needs to be created.
989 if (!NT_SUCCESS(status
) &&
990 !(deviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
)) {
993 "CreateDiskDeviceObjects: Can't read capacity for device %s\n",
996 return(STATUS_SUCCESS
);
1001 // Make sure the volume verification bit is off so that
1002 // IoReadPartitionTable will work.
1005 deviceObject
->Flags
&= ~DO_VERIFY_VOLUME
;
1008 status
= CreatePartitionDeviceObjects(deviceObject
, RegistryPath
);
1010 if (NT_SUCCESS(status
))
1011 return STATUS_SUCCESS
;
1014 CreateDiskDeviceObjectsExit
:
1017 // Release the device since an error occurred.
1020 ScsiClassClaimDevice(PortDeviceObject
,
1025 if (diskGeometry
!= NULL
) {
1026 ExFreePool(diskGeometry
);
1029 if (senseData
!= NULL
) {
1030 ExFreePool(senseData
);
1033 if (deviceObject
!= NULL
) {
1035 if (srbListInitialized
) {
1036 ExDeleteNPagedLookasideList(&deviceExtension
->SrbLookasideListHead
);
1039 IoDeleteDevice(deviceObject
);
1043 // Delete directory and return.
1046 if (!NT_SUCCESS(status
)) {
1047 ZwMakeTemporaryObject(handle
);
1054 } // end CreateDiskDeviceObjects()
1059 CreatePartitionDeviceObjects(
1060 IN PDEVICE_OBJECT PhysicalDeviceObject
,
1061 IN PUNICODE_STRING RegistryPath
1064 CCHAR ntNameBuffer
[MAXIMUM_FILENAME_LENGTH
];
1065 ULONG partitionNumber
= 0;
1067 PDEVICE_OBJECT deviceObject
= NULL
;
1068 PDISK_GEOMETRY_EX diskGeometry
= NULL
;
1069 PDRIVE_LAYOUT_INFORMATION partitionList
= NULL
;
1070 PDEVICE_EXTENSION deviceExtension
;
1071 PDEVICE_EXTENSION physicalDeviceExtension
;
1072 PCLASS_INIT_DATA initData
= NULL
;
1073 PDISK_DATA diskData
;
1074 PDISK_DATA physicalDiskData
;
1075 ULONG bytesPerSector
;
1078 ULONG dmByteSkew
= 0;
1080 BOOLEAN dmActive
= FALSE
;
1081 ULONG numberListElements
= 0;
1085 // Get physical device geometry information for partition table reads.
1088 physicalDeviceExtension
= PhysicalDeviceObject
->DeviceExtension
;
1089 diskGeometry
= physicalDeviceExtension
->DiskGeometry
;
1090 bytesPerSector
= diskGeometry
->Geometry
.BytesPerSector
;
1093 // Make sure sector size is not zero.
1096 if (bytesPerSector
== 0) {
1099 // Default sector size for disk is 512.
1102 bytesPerSector
= diskGeometry
->Geometry
.BytesPerSector
= 512;
1105 sectorShift
= physicalDeviceExtension
->SectorShift
;
1108 // Set pointer to disk data area that follows device extension.
1111 diskData
= (PDISK_DATA
)(physicalDeviceExtension
+ 1);
1112 diskData
->PartitionListState
= Initializing
;
1115 // Determine is DM Driver is loaded on an IDE drive that is
1116 // under control of Atapi - this could be either a crashdump or
1117 // an Atapi device is sharing the controller with an IDE disk.
1120 HalExamineMBR(PhysicalDeviceObject
,
1121 physicalDeviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
,
1128 // Update the device extension, so that the call to IoReadPartitionTable
1129 // will get the correct information. Any I/O to this disk will have
1130 // to be skewed by *dmSkew sectors aka DMByteSkew.
1133 physicalDeviceExtension
->DMSkew
= *dmSkew
;
1134 physicalDeviceExtension
->DMActive
= TRUE
;
1135 physicalDeviceExtension
->DMByteSkew
= physicalDeviceExtension
->DMSkew
* bytesPerSector
;
1138 // Save away the infomation that we need, since this deviceExtension will soon be
1143 dmByteSkew
= physicalDeviceExtension
->DMByteSkew
;
1148 // Create objects for all the partitions on the device.
1151 status
= IoReadPartitionTable(PhysicalDeviceObject
,
1152 physicalDeviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
,
1154 (PVOID
)&partitionList
);
1157 // If the I/O read partition table failed and this is a removable device,
1158 // then fix up the partition list to make it look like there is one
1159 // zero length partition.
1161 DPRINT("IoReadPartitionTable() status: 0x%08X\n", status
);
1162 if ((!NT_SUCCESS(status
) || partitionList
->PartitionCount
== 0) &&
1163 PhysicalDeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
) {
1165 if (!NT_SUCCESS(status
)) {
1168 // Remember this disk is not ready.
1171 diskData
->DriveNotReady
= TRUE
;
1176 // Free the partition list allocated by IoReadPartitionTable.
1179 ExFreePool(partitionList
);
1183 // Allocate and zero a partition list.
1186 partitionList
= ExAllocatePool(NonPagedPool
, sizeof(*partitionList
));
1189 if (partitionList
!= NULL
) {
1191 RtlZeroMemory( partitionList
, sizeof( *partitionList
));
1194 // Set the partition count to one and the status to success
1195 // so one device object will be created. Set the partition type
1196 // to a bogus value.
1199 partitionList
->PartitionCount
= 1;
1201 status
= STATUS_SUCCESS
;
1205 if (NT_SUCCESS(status
)) {
1208 // Record disk signature.
1211 diskData
->Signature
= partitionList
->Signature
;
1214 // If disk signature is zero, then calculate the MBR checksum.
1217 if (!diskData
->Signature
) {
1219 if (!CalculateMbrCheckSum(physicalDeviceExtension
,
1220 &diskData
->MbrCheckSum
)) {
1223 "SCSIDISK: Can't calculate MBR checksum for disk %x\n",
1224 physicalDeviceExtension
->DeviceNumber
));
1228 "SCSIDISK: MBR checksum for disk %x is %x\n",
1229 physicalDeviceExtension
->DeviceNumber
,
1230 diskData
->MbrCheckSum
));
1235 // Check the registry and determine if the BIOS knew about this drive. If
1236 // it did then update the geometry with the BIOS information.
1239 UpdateGeometry(physicalDeviceExtension
);
1241 srbFlags
= physicalDeviceExtension
->SrbFlags
;
1243 initData
= ExAllocatePool(NonPagedPool
, sizeof(CLASS_INIT_DATA
));
1247 "Disk.CreatePartionDeviceObjects - Allocation of initData failed\n"));
1249 status
= STATUS_INSUFFICIENT_RESOURCES
;
1250 goto CreatePartitionDeviceObjectsExit
;
1253 RtlZeroMemory(initData
, sizeof(CLASS_INIT_DATA
));
1255 initData
->InitializationDataSize
= sizeof(CLASS_INIT_DATA
);
1256 initData
->DeviceExtensionSize
= DEVICE_EXTENSION_SIZE
;
1257 initData
->DeviceType
= FILE_DEVICE_DISK
;
1258 initData
->DeviceCharacteristics
= PhysicalDeviceObject
->Characteristics
;
1259 initData
->ClassError
= physicalDeviceExtension
->ClassError
;
1260 initData
->ClassReadWriteVerification
= physicalDeviceExtension
->ClassReadWriteVerification
;
1261 initData
->ClassFindDevices
= physicalDeviceExtension
->ClassFindDevices
;
1262 initData
->ClassDeviceControl
= physicalDeviceExtension
->ClassDeviceControl
;
1263 initData
->ClassShutdownFlush
= physicalDeviceExtension
->ClassShutdownFlush
;
1264 initData
->ClassCreateClose
= physicalDeviceExtension
->ClassCreateClose
;
1265 initData
->ClassStartIo
= physicalDeviceExtension
->ClassStartIo
;
1268 // Create device objects for the device partitions (if any).
1269 // PartitionCount includes physical device partition 0,
1270 // so only one partition means no objects to create.
1274 "CreateDiskDeviceObjects: Number of partitions is %d\n",
1275 partitionList
->PartitionCount
));
1277 for (partitionNumber
= 0; partitionNumber
<
1278 partitionList
->PartitionCount
; partitionNumber
++) {
1281 // Create partition object and set up partition parameters.
1284 sprintf(ntNameBuffer
,
1285 "\\Device\\Harddisk%lu\\Partition%lu",
1286 physicalDeviceExtension
->DeviceNumber
,
1287 partitionNumber
+ 1);
1290 "CreateDiskDeviceObjects: Create device object %s\n",
1293 status
= ScsiClassCreateDeviceObject(PhysicalDeviceObject
->DriverObject
,
1295 PhysicalDeviceObject
,
1299 if (!NT_SUCCESS(status
)) {
1301 DebugPrint((1, "CreateDiskDeviceObjects: Can't create device object for %s\n", ntNameBuffer
));
1307 // Set up device object fields.
1310 deviceObject
->Flags
|= DO_DIRECT_IO
;
1313 // Check if this is during initialization. If not indicate that
1314 // system initialization already took place and this disk is ready
1318 if (!RegistryPath
) {
1319 deviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
1322 deviceObject
->StackSize
= (CCHAR
)physicalDeviceExtension
->PortDeviceObject
->StackSize
+ 1;
1325 // Set up device extension fields.
1328 deviceExtension
= deviceObject
->DeviceExtension
;
1333 // Restore any saved DM values.
1336 deviceExtension
->DMByteSkew
= dmByteSkew
;
1337 deviceExtension
->DMSkew
= *dmSkew
;
1338 deviceExtension
->DMActive
= TRUE
;
1343 // Link new device extension to previous disk data
1344 // to support dynamic partitioning.
1347 diskData
->NextPartition
= deviceExtension
;
1350 // Get pointer to new disk data.
1353 diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
1356 // Set next partition pointer to NULL in case this is the
1360 diskData
->NextPartition
= NULL
;
1363 // Allocate spinlock for zoning for split-request completion.
1366 KeInitializeSpinLock(&deviceExtension
->SplitRequestSpinLock
);
1369 // Copy port device object pointer to device extension.
1372 deviceExtension
->PortDeviceObject
= physicalDeviceExtension
->PortDeviceObject
;
1375 // Set the alignment requirements for the device based on the
1376 // host adapter requirements
1379 if (physicalDeviceExtension
->PortDeviceObject
->AlignmentRequirement
> deviceObject
->AlignmentRequirement
) {
1380 deviceObject
->AlignmentRequirement
= physicalDeviceExtension
->PortDeviceObject
->AlignmentRequirement
;
1384 if (srbFlags
& SRB_FLAGS_QUEUE_ACTION_ENABLE
) {
1385 numberListElements
= 30;
1387 numberListElements
= 8;
1391 // Build the lookaside list for srb's for this partition based on
1392 // whether the adapter and disk can do tagged queueing.
1395 ScsiClassInitializeSrbLookasideList(deviceExtension
,
1396 numberListElements
);
1398 deviceExtension
->SrbFlags
= srbFlags
;
1401 // Set the sense-data pointer in the device extension.
1404 deviceExtension
->SenseData
= physicalDeviceExtension
->SenseData
;
1405 deviceExtension
->PortCapabilities
= physicalDeviceExtension
->PortCapabilities
;
1406 deviceExtension
->DiskGeometry
= diskGeometry
;
1407 diskData
->PartitionOrdinal
= diskData
->PartitionNumber
= partitionNumber
+ 1;
1408 diskData
->PartitionType
= partitionList
->PartitionEntry
[partitionNumber
].PartitionType
;
1409 diskData
->BootIndicator
= partitionList
->PartitionEntry
[partitionNumber
].BootIndicator
;
1411 DebugPrint((2, "CreateDiskDeviceObjects: Partition type is %x\n",
1412 diskData
->PartitionType
));
1414 deviceExtension
->StartingOffset
= partitionList
->PartitionEntry
[partitionNumber
].StartingOffset
;
1415 deviceExtension
->PartitionLength
= partitionList
->PartitionEntry
[partitionNumber
].PartitionLength
;
1416 diskData
->HiddenSectors
= partitionList
->PartitionEntry
[partitionNumber
].HiddenSectors
;
1417 deviceExtension
->PortNumber
= physicalDeviceExtension
->PortNumber
;
1418 deviceExtension
->PathId
= physicalDeviceExtension
->PathId
;
1419 deviceExtension
->TargetId
= physicalDeviceExtension
->TargetId
;
1420 deviceExtension
->Lun
= physicalDeviceExtension
->Lun
;
1423 // Check for removable media support.
1426 if (PhysicalDeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
) {
1427 deviceObject
->Characteristics
|= FILE_REMOVABLE_MEDIA
;
1431 // Set timeout value in seconds.
1434 deviceExtension
->TimeOutValue
= physicalDeviceExtension
->TimeOutValue
;
1435 deviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
= bytesPerSector
;
1436 deviceExtension
->SectorShift
= sectorShift
;
1437 deviceExtension
->DeviceObject
= deviceObject
;
1438 deviceExtension
->DeviceFlags
|= physicalDeviceExtension
->DeviceFlags
;
1440 } // end for (partitionNumber) ...
1443 // Free the buffer allocated by reading the
1447 ExFreePool(partitionList
);
1451 CreatePartitionDeviceObjectsExit
:
1453 if (partitionList
) {
1454 ExFreePool(partitionList
);
1457 ExFreePool(initData
);
1465 physicalDiskData
= (PDISK_DATA
)(physicalDeviceExtension
+ 1);
1466 physicalDiskData
->PartitionListState
= Initialized
;
1468 return(STATUS_SUCCESS
);
1471 } // end CreatePartitionDeviceObjects()
1476 ScsiDiskReadWriteVerification(
1477 IN PDEVICE_OBJECT DeviceObject
,
1483 Routine Description:
1485 I/O System entry for read and write requests to SCSI disks.
1489 DeviceObject - Pointer to driver object created by system.
1499 PDEVICE_EXTENSION deviceExtension
= DeviceObject
->DeviceExtension
;
1500 PIO_STACK_LOCATION currentIrpStack
= IoGetCurrentIrpStackLocation(Irp
);
1501 ULONG transferByteCount
= currentIrpStack
->Parameters
.Read
.Length
;
1502 LARGE_INTEGER startingOffset
;
1505 // Verify parameters of this request.
1506 // Check that ending sector is within partition and
1507 // that number of bytes to transfer is a multiple of
1511 startingOffset
.QuadPart
= (currentIrpStack
->Parameters
.Read
.ByteOffset
.QuadPart
+
1514 if ((startingOffset
.QuadPart
> deviceExtension
->PartitionLength
.QuadPart
) ||
1515 (transferByteCount
& (deviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
- 1))) {
1518 // This error maybe caused by the fact that the drive is not ready.
1521 if (((PDISK_DATA
)(deviceExtension
+ 1))->DriveNotReady
) {
1524 // Flag this as a user errror so that a popup is generated.
1527 Irp
->IoStatus
.Status
= STATUS_DEVICE_NOT_READY
;
1528 IoSetHardErrorOrVerifyDevice(Irp
, DeviceObject
);
1533 // Note fastfat depends on this parameter to determine when to
1534 // remount do to a sector size change.
1537 Irp
->IoStatus
.Status
= STATUS_INVALID_PARAMETER
;
1540 return STATUS_INVALID_PARAMETER
;
1543 return STATUS_SUCCESS
;
1545 } // end ScsiDiskReadWrite()
1550 ScsiDiskDeviceControl(
1551 PDEVICE_OBJECT DeviceObject
,
1557 Routine Description:
1559 I/O system entry for device controls to SCSI disks.
1563 DeviceObject - Pointer to driver object created by system.
1573 PIO_STACK_LOCATION irpStack
= IoGetCurrentIrpStackLocation(Irp
);
1574 PDEVICE_EXTENSION deviceExtension
= DeviceObject
->DeviceExtension
;
1575 PDISK_DATA diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
1576 PSCSI_REQUEST_BLOCK srb
;
1578 PMODE_PARAMETER_HEADER modeData
;
1583 IO_STATUS_BLOCK ioStatus
;
1587 srb
= ExAllocatePool(NonPagedPool
, SCSI_REQUEST_BLOCK_SIZE
);
1591 Irp
->IoStatus
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
1592 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1593 return(STATUS_INSUFFICIENT_RESOURCES
);
1597 // Write zeros to Srb.
1600 RtlZeroMemory(srb
, SCSI_REQUEST_BLOCK_SIZE
);
1602 cdb
= (PCDB
)srb
->Cdb
;
1604 switch (irpStack
->Parameters
.DeviceIoControl
.IoControlCode
) {
1606 case SMART_GET_VERSION
: {
1609 PSRB_IO_CONTROL srbControl
;
1610 PGETVERSIONINPARAMS versionParams
;
1612 if (irpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
1613 sizeof(GETVERSIONINPARAMS
)) {
1614 status
= STATUS_INVALID_PARAMETER
;
1619 // Create notification event object to be used to signal the
1620 // request completion.
1623 KeInitializeEvent(&event
, NotificationEvent
, FALSE
);
1625 srbControl
= ExAllocatePool(NonPagedPool
,
1626 sizeof(SRB_IO_CONTROL
) + sizeof(GETVERSIONINPARAMS
));
1629 status
= STATUS_INSUFFICIENT_RESOURCES
;
1634 // fill in srbControl fields
1637 srbControl
->HeaderLength
= sizeof(SRB_IO_CONTROL
);
1638 RtlMoveMemory (srbControl
->Signature
, "SCSIDISK", 8);
1639 srbControl
->Timeout
= deviceExtension
->TimeOutValue
;
1640 srbControl
->Length
= sizeof(GETVERSIONINPARAMS
);
1641 srbControl
->ControlCode
= IOCTL_SCSI_MINIPORT_SMART_VERSION
;
1644 // Point to the 'buffer' portion of the SRB_CONTROL
1647 buffer
= (ULONG_PTR
)srbControl
+ srbControl
->HeaderLength
;
1650 // Ensure correct target is set in the cmd parameters.
1653 versionParams
= (PGETVERSIONINPARAMS
)buffer
;
1654 versionParams
->bIDEDeviceMap
= deviceExtension
->TargetId
;
1657 // Copy the IOCTL parameters to the srb control buffer area.
1660 RtlMoveMemory((PVOID
)buffer
, Irp
->AssociatedIrp
.SystemBuffer
, sizeof(GETVERSIONINPARAMS
));
1663 irp2
= IoBuildDeviceIoControlRequest(IOCTL_SCSI_MINIPORT
,
1664 deviceExtension
->PortDeviceObject
,
1666 sizeof(SRB_IO_CONTROL
) + sizeof(GETVERSIONINPARAMS
),
1668 sizeof(SRB_IO_CONTROL
) + sizeof(GETVERSIONINPARAMS
),
1674 status
= STATUS_INSUFFICIENT_RESOURCES
;
1679 // Call the port driver with the request and wait for it to complete.
1682 status
= IoCallDriver(deviceExtension
->PortDeviceObject
, irp2
);
1684 if (status
== STATUS_PENDING
) {
1685 KeWaitForSingleObject(&event
, Suspended
, KernelMode
, FALSE
, NULL
);
1686 status
= ioStatus
.Status
;
1690 // If successful, copy the data received into the output buffer.
1691 // This should only fail in the event that the IDE driver is older than this driver.
1694 if (NT_SUCCESS(status
)) {
1696 buffer
= (ULONG_PTR
)srbControl
+ srbControl
->HeaderLength
;
1698 RtlMoveMemory ( Irp
->AssociatedIrp
.SystemBuffer
, (PVOID
)buffer
, sizeof(GETVERSIONINPARAMS
));
1699 Irp
->IoStatus
.Information
= sizeof(GETVERSIONINPARAMS
);
1702 ExFreePool(srbControl
);
1706 case SMART_RCV_DRIVE_DATA
: {
1708 PSENDCMDINPARAMS cmdInParameters
= ((PSENDCMDINPARAMS
)Irp
->AssociatedIrp
.SystemBuffer
);
1709 ULONG controlCode
= 0;
1710 PSRB_IO_CONTROL srbControl
;
1713 if (irpStack
->Parameters
.DeviceIoControl
.InputBufferLength
<
1714 (sizeof(SENDCMDINPARAMS
) - 1)) {
1715 status
= STATUS_INVALID_PARAMETER
;
1718 } else if (irpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
1719 (sizeof(SENDCMDOUTPARAMS
) + 512 - 1)) {
1720 status
= STATUS_INVALID_PARAMETER
;
1725 // Create notification event object to be used to signal the
1726 // request completion.
1729 KeInitializeEvent(&event
, NotificationEvent
, FALSE
);
1731 if (cmdInParameters
->irDriveRegs
.bCommandReg
== ID_CMD
) {
1733 length
= IDENTIFY_BUFFER_SIZE
+ sizeof(SENDCMDOUTPARAMS
);
1734 controlCode
= IOCTL_SCSI_MINIPORT_IDENTIFY
;
1736 } else if (cmdInParameters
->irDriveRegs
.bCommandReg
== SMART_CMD
) {
1737 switch (cmdInParameters
->irDriveRegs
.bFeaturesReg
) {
1738 case READ_ATTRIBUTES
:
1739 controlCode
= IOCTL_SCSI_MINIPORT_READ_SMART_ATTRIBS
;
1740 length
= READ_ATTRIBUTE_BUFFER_SIZE
+ sizeof(SENDCMDOUTPARAMS
);
1742 case READ_THRESHOLDS
:
1743 controlCode
= IOCTL_SCSI_MINIPORT_READ_SMART_THRESHOLDS
;
1744 length
= READ_THRESHOLD_BUFFER_SIZE
+ sizeof(SENDCMDOUTPARAMS
);
1747 status
= STATUS_INVALID_PARAMETER
;
1752 status
= STATUS_INVALID_PARAMETER
;
1755 if (controlCode
== 0) {
1756 status
= STATUS_INVALID_PARAMETER
;
1760 srbControl
= ExAllocatePool(NonPagedPool
,
1761 sizeof(SRB_IO_CONTROL
) + length
);
1764 status
= STATUS_INSUFFICIENT_RESOURCES
;
1769 // fill in srbControl fields
1772 srbControl
->HeaderLength
= sizeof(SRB_IO_CONTROL
);
1773 RtlMoveMemory (srbControl
->Signature
, "SCSIDISK", 8);
1774 srbControl
->Timeout
= deviceExtension
->TimeOutValue
;
1775 srbControl
->Length
= length
;
1776 srbControl
->ControlCode
= controlCode
;
1779 // Point to the 'buffer' portion of the SRB_CONTROL
1782 buffer
= (ULONG_PTR
)srbControl
+ srbControl
->HeaderLength
;
1785 // Ensure correct target is set in the cmd parameters.
1788 cmdInParameters
->bDriveNumber
= deviceExtension
->TargetId
;
1791 // Copy the IOCTL parameters to the srb control buffer area.
1794 RtlMoveMemory((PVOID
)buffer
, Irp
->AssociatedIrp
.SystemBuffer
, sizeof(SENDCMDINPARAMS
) - 1);
1796 irp2
= IoBuildDeviceIoControlRequest(IOCTL_SCSI_MINIPORT
,
1797 deviceExtension
->PortDeviceObject
,
1799 sizeof(SRB_IO_CONTROL
) + sizeof(SENDCMDINPARAMS
) - 1,
1801 sizeof(SRB_IO_CONTROL
) + length
,
1807 status
= STATUS_INSUFFICIENT_RESOURCES
;
1812 // Call the port driver with the request and wait for it to complete.
1815 status
= IoCallDriver(deviceExtension
->PortDeviceObject
, irp2
);
1817 if (status
== STATUS_PENDING
) {
1818 KeWaitForSingleObject(&event
, Suspended
, KernelMode
, FALSE
, NULL
);
1819 status
= ioStatus
.Status
;
1823 // If successful, copy the data received into the output buffer
1826 buffer
= (ULONG_PTR
)srbControl
+ srbControl
->HeaderLength
;
1828 if (NT_SUCCESS(status
)) {
1830 RtlMoveMemory ( Irp
->AssociatedIrp
.SystemBuffer
, (PVOID
)buffer
, length
- 1);
1831 Irp
->IoStatus
.Information
= length
- 1;
1835 RtlMoveMemory ( Irp
->AssociatedIrp
.SystemBuffer
, (PVOID
)buffer
, (sizeof(SENDCMDOUTPARAMS
) - 1));
1836 Irp
->IoStatus
.Information
= sizeof(SENDCMDOUTPARAMS
) - 1;
1840 ExFreePool(srbControl
);
1845 case SMART_SEND_DRIVE_COMMAND
: {
1847 PSENDCMDINPARAMS cmdInParameters
= ((PSENDCMDINPARAMS
)Irp
->AssociatedIrp
.SystemBuffer
);
1848 PSRB_IO_CONTROL srbControl
;
1849 ULONG controlCode
= 0;
1852 if (irpStack
->Parameters
.DeviceIoControl
.InputBufferLength
<
1853 (sizeof(SENDCMDINPARAMS
) - 1)) {
1854 status
= STATUS_INVALID_PARAMETER
;
1857 } else if (irpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
1858 (sizeof(SENDCMDOUTPARAMS
) - 1)) {
1859 status
= STATUS_INVALID_PARAMETER
;
1864 // Create notification event object to be used to signal the
1865 // request completion.
1868 KeInitializeEvent(&event
, NotificationEvent
, FALSE
);
1872 if (cmdInParameters
->irDriveRegs
.bCommandReg
== SMART_CMD
) {
1873 switch (cmdInParameters
->irDriveRegs
.bFeaturesReg
) {
1876 controlCode
= IOCTL_SCSI_MINIPORT_ENABLE_SMART
;
1880 controlCode
= IOCTL_SCSI_MINIPORT_DISABLE_SMART
;
1883 case RETURN_SMART_STATUS
:
1886 // Ensure bBuffer is at least 2 bytes (to hold the values of
1887 // cylinderLow and cylinderHigh).
1890 if (irpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
1891 (sizeof(SENDCMDOUTPARAMS
) - 1 + sizeof(IDEREGS
))) {
1893 status
= STATUS_INVALID_PARAMETER
;
1897 controlCode
= IOCTL_SCSI_MINIPORT_RETURN_STATUS
;
1898 length
= sizeof(IDEREGS
);
1901 case ENABLE_DISABLE_AUTOSAVE
:
1902 controlCode
= IOCTL_SCSI_MINIPORT_ENABLE_DISABLE_AUTOSAVE
;
1905 case SAVE_ATTRIBUTE_VALUES
:
1906 controlCode
= IOCTL_SCSI_MINIPORT_SAVE_ATTRIBUTE_VALUES
;
1909 case EXECUTE_OFFLINE_DIAGS
:
1910 controlCode
= IOCTL_SCSI_MINIPORT_EXECUTE_OFFLINE_DIAGS
;
1913 status
= STATUS_INVALID_PARAMETER
;
1918 status
= STATUS_INVALID_PARAMETER
;
1921 if (controlCode
== 0) {
1922 status
= STATUS_INVALID_PARAMETER
;
1926 length
+= (sizeof(SENDCMDOUTPARAMS
) > sizeof(SENDCMDINPARAMS
)) ? sizeof(SENDCMDOUTPARAMS
) : sizeof(SENDCMDINPARAMS
);
1927 srbControl
= ExAllocatePool(NonPagedPool
,
1928 sizeof(SRB_IO_CONTROL
) + length
);
1931 status
= STATUS_INSUFFICIENT_RESOURCES
;
1936 // fill in srbControl fields
1939 srbControl
->HeaderLength
= sizeof(SRB_IO_CONTROL
);
1940 RtlMoveMemory (srbControl
->Signature
, "SCSIDISK", 8);
1941 srbControl
->Timeout
= deviceExtension
->TimeOutValue
;
1942 srbControl
->Length
= length
;
1945 // Point to the 'buffer' portion of the SRB_CONTROL
1948 buffer
= (ULONG_PTR
)srbControl
+ srbControl
->HeaderLength
;
1951 // Ensure correct target is set in the cmd parameters.
1954 cmdInParameters
->bDriveNumber
= deviceExtension
->TargetId
;
1957 // Copy the IOCTL parameters to the srb control buffer area.
1960 RtlMoveMemory((PVOID
)buffer
, Irp
->AssociatedIrp
.SystemBuffer
, sizeof(SENDCMDINPARAMS
) - 1);
1962 srbControl
->ControlCode
= controlCode
;
1964 irp2
= IoBuildDeviceIoControlRequest(IOCTL_SCSI_MINIPORT
,
1965 deviceExtension
->PortDeviceObject
,
1967 sizeof(SRB_IO_CONTROL
) + sizeof(SENDCMDINPARAMS
) - 1,
1969 sizeof(SRB_IO_CONTROL
) + length
,
1975 status
= STATUS_INSUFFICIENT_RESOURCES
;
1980 // Call the port driver with the request and wait for it to complete.
1983 status
= IoCallDriver(deviceExtension
->PortDeviceObject
, irp2
);
1985 if (status
== STATUS_PENDING
) {
1986 KeWaitForSingleObject(&event
, Suspended
, KernelMode
, FALSE
, NULL
);
1987 status
= ioStatus
.Status
;
1991 // Copy the data received into the output buffer. Since the status buffer
1992 // contains error information also, always perform this copy. IO will will
1993 // either pass this back to the app, or zero it, in case of error.
1996 buffer
= (ULONG_PTR
)srbControl
+ srbControl
->HeaderLength
;
1999 // Update the return buffer size based on the sub-command.
2002 if (cmdInParameters
->irDriveRegs
.bFeaturesReg
== RETURN_SMART_STATUS
) {
2003 length
= sizeof(SENDCMDOUTPARAMS
) - 1 + sizeof(IDEREGS
);
2005 length
= sizeof(SENDCMDOUTPARAMS
) - 1;
2008 RtlMoveMemory ( Irp
->AssociatedIrp
.SystemBuffer
, (PVOID
)buffer
, length
);
2009 Irp
->IoStatus
.Information
= length
;
2011 ExFreePool(srbControl
);
2016 case IOCTL_DISK_GET_DRIVE_GEOMETRY
:
2017 case IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
:
2020 PDEVICE_EXTENSION physicalDeviceExtension
;
2021 PDISK_DATA physicalDiskData
;
2022 BOOLEAN removable
= FALSE
;
2023 BOOLEAN listInitialized
= FALSE
;
2025 if ((irpStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_DISK_GET_DRIVE_GEOMETRY
&&
2026 irpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
2027 sizeof(DISK_GEOMETRY
)) ||
2028 (irpStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
&&
2029 irpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
2030 sizeof(DISK_GEOMETRY_EX
))) {
2032 status
= STATUS_INFO_LENGTH_MISMATCH
;
2036 status
= STATUS_SUCCESS
;
2038 physicalDeviceExtension
= deviceExtension
->PhysicalDevice
->DeviceExtension
;
2039 physicalDiskData
= (PDISK_DATA
)(physicalDeviceExtension
+ 1);
2041 removable
= (BOOLEAN
)DeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
;
2042 listInitialized
= (physicalDiskData
->PartitionListState
== Initialized
);
2044 if (removable
|| (!listInitialized
))
2047 // Issue ReadCapacity to update device extension
2048 // with information for current media.
2051 status
= ScsiClassReadDriveCapacity(deviceExtension
->PhysicalDevice
);
2057 if (!NT_SUCCESS(status
)) {
2060 // Note the drive is not ready.
2063 diskData
->DriveNotReady
= TRUE
;
2069 // Note the drive is now ready.
2072 diskData
->DriveNotReady
= FALSE
;
2074 } else if (NT_SUCCESS(status
)) {
2076 // ReadDriveCapacity was allright, create Partition Objects
2078 if (physicalDiskData
->PartitionListState
== NotInitialized
) {
2079 status
= CreatePartitionDeviceObjects(deviceExtension
->PhysicalDevice
, NULL
);
2083 if (NT_SUCCESS(status
)) {
2086 // Copy drive geometry information from device extension.
2089 RtlMoveMemory(Irp
->AssociatedIrp
.SystemBuffer
,
2090 deviceExtension
->DiskGeometry
,
2091 (irpStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_DISK_GET_DRIVE_GEOMETRY
) ?
2092 sizeof(DISK_GEOMETRY
) :
2093 sizeof(DISK_GEOMETRY_EX
));
2095 status
= STATUS_SUCCESS
;
2096 Irp
->IoStatus
.Information
=
2097 (irpStack
->Parameters
.DeviceIoControl
.IoControlCode
== IOCTL_DISK_GET_DRIVE_GEOMETRY
) ?
2098 sizeof(DISK_GEOMETRY
) :
2099 sizeof(DISK_GEOMETRY_EX
);
2106 case IOCTL_DISK_VERIFY
:
2110 PVERIFY_INFORMATION verifyInfo
= Irp
->AssociatedIrp
.SystemBuffer
;
2111 LARGE_INTEGER byteOffset
;
2116 // Validate buffer length.
2119 if (irpStack
->Parameters
.DeviceIoControl
.InputBufferLength
<
2120 sizeof(VERIFY_INFORMATION
)) {
2122 status
= STATUS_INFO_LENGTH_MISMATCH
;
2130 srb
->CdbLength
= 10;
2132 cdb
->CDB10
.OperationCode
= SCSIOP_VERIFY
;
2135 // Add disk offset to starting sector.
2138 byteOffset
.QuadPart
= deviceExtension
->StartingOffset
.QuadPart
+
2139 verifyInfo
->StartingOffset
.QuadPart
;
2142 // Convert byte offset to sector offset.
2145 sectorOffset
= (ULONG
)(byteOffset
.QuadPart
>> deviceExtension
->SectorShift
);
2148 // Convert ULONG byte count to USHORT sector count.
2151 sectorCount
= (USHORT
)(verifyInfo
->Length
>> deviceExtension
->SectorShift
);
2154 // Move little endian values into CDB in big endian format.
2157 cdb
->CDB10
.LogicalBlockByte0
= ((PFOUR_BYTE
)§orOffset
)->Byte3
;
2158 cdb
->CDB10
.LogicalBlockByte1
= ((PFOUR_BYTE
)§orOffset
)->Byte2
;
2159 cdb
->CDB10
.LogicalBlockByte2
= ((PFOUR_BYTE
)§orOffset
)->Byte1
;
2160 cdb
->CDB10
.LogicalBlockByte3
= ((PFOUR_BYTE
)§orOffset
)->Byte0
;
2162 cdb
->CDB10
.TransferBlocksMsb
= ((PFOUR_BYTE
)§orCount
)->Byte1
;
2163 cdb
->CDB10
.TransferBlocksLsb
= ((PFOUR_BYTE
)§orCount
)->Byte0
;
2166 // The verify command is used by the NT FORMAT utility and
2167 // requests are sent down for 5% of the volume size. The
2168 // request timeout value is calculated based on the number of
2169 // sectors verified.
2172 srb
->TimeOutValue
= ((sectorCount
+ 0x7F) >> 7) *
2173 deviceExtension
->TimeOutValue
;
2175 status
= ScsiClassSendSrbAsynchronous(DeviceObject
,
2186 case IOCTL_DISK_GET_PARTITION_INFO
:
2189 // Return the information about the partition specified by the device
2190 // object. Note that no information is ever returned about the size
2191 // or partition type of the physical disk, as this doesn't make any
2195 if (irpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
2196 sizeof(PARTITION_INFORMATION
)) {
2198 status
= STATUS_INFO_LENGTH_MISMATCH
;
2201 #if 0 // HACK: ReactOS partition numbers must be wrong
2202 else if (diskData
->PartitionNumber
== 0) {
2205 // Paritition zero is not a partition so this is not a
2206 // reasonable request.
2209 status
= STATUS_INVALID_DEVICE_REQUEST
;
2215 PPARTITION_INFORMATION outputBuffer
;
2218 // Update the geometry in case it has changed.
2221 status
= UpdateRemovableGeometry (DeviceObject
, Irp
);
2223 if (!NT_SUCCESS(status
)) {
2226 // Note the drive is not ready.
2229 diskData
->DriveNotReady
= TRUE
;
2234 // Note the drive is now ready.
2237 diskData
->DriveNotReady
= FALSE
;
2238 // HACK: ReactOS partition numbers must be wrong (>0 part)
2239 if (diskData
->PartitionType
== 0 && (diskData
->PartitionNumber
> 0)) {
2241 status
= STATUS_INVALID_DEVICE_REQUEST
;
2246 (PPARTITION_INFORMATION
)Irp
->AssociatedIrp
.SystemBuffer
;
2248 outputBuffer
->PartitionType
= diskData
->PartitionType
;
2249 outputBuffer
->StartingOffset
= deviceExtension
->StartingOffset
;
2250 outputBuffer
->PartitionLength
.QuadPart
= (diskData
->PartitionNumber
) ?
2251 deviceExtension
->PartitionLength
.QuadPart
: 2305843009213693951LL; // HACK
2252 outputBuffer
->HiddenSectors
= diskData
->HiddenSectors
;
2253 outputBuffer
->PartitionNumber
= diskData
->PartitionNumber
;
2254 outputBuffer
->BootIndicator
= diskData
->BootIndicator
;
2255 outputBuffer
->RewritePartition
= FALSE
;
2256 outputBuffer
->RecognizedPartition
=
2257 IsRecognizedPartition(diskData
->PartitionType
);
2259 status
= STATUS_SUCCESS
;
2260 Irp
->IoStatus
.Information
= sizeof(PARTITION_INFORMATION
);
2265 case IOCTL_DISK_GET_PARTITION_INFO_EX
:
2268 // Return the information about the partition specified by the device
2269 // object. Note that no information is ever returned about the size
2270 // or partition type of the physical disk, as this doesn't make any
2274 if (irpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
2275 sizeof(PARTITION_INFORMATION_EX
)) {
2277 status
= STATUS_INFO_LENGTH_MISMATCH
;
2280 else if (diskData
->PartitionNumber
== 0) {
2283 // Paritition zero is not a partition so this is not a
2284 // reasonable request.
2287 status
= STATUS_INVALID_DEVICE_REQUEST
;
2292 PPARTITION_INFORMATION_EX outputBuffer
;
2295 // Update the geometry in case it has changed.
2298 status
= UpdateRemovableGeometry (DeviceObject
, Irp
);
2300 if (!NT_SUCCESS(status
)) {
2303 // Note the drive is not ready.
2306 diskData
->DriveNotReady
= TRUE
;
2311 // Note the drive is now ready.
2314 diskData
->DriveNotReady
= FALSE
;
2316 if (diskData
->PartitionType
== 0 && (diskData
->PartitionNumber
> 0)) {
2318 status
= STATUS_INVALID_DEVICE_REQUEST
;
2323 (PPARTITION_INFORMATION_EX
)Irp
->AssociatedIrp
.SystemBuffer
;
2326 // FIXME: hack of the year, assume that partition is MBR
2327 // Thing that can obviously be wrong...
2330 outputBuffer
->PartitionStyle
= PARTITION_STYLE_MBR
;
2331 outputBuffer
->Mbr
.PartitionType
= diskData
->PartitionType
;
2332 outputBuffer
->StartingOffset
= deviceExtension
->StartingOffset
;
2333 outputBuffer
->PartitionLength
.QuadPart
= deviceExtension
->PartitionLength
.QuadPart
;
2334 outputBuffer
->Mbr
.HiddenSectors
= diskData
->HiddenSectors
;
2335 outputBuffer
->PartitionNumber
= diskData
->PartitionNumber
;
2336 outputBuffer
->Mbr
.BootIndicator
= diskData
->BootIndicator
;
2337 outputBuffer
->RewritePartition
= FALSE
;
2338 outputBuffer
->Mbr
.RecognizedPartition
=
2339 IsRecognizedPartition(diskData
->PartitionType
);
2341 status
= STATUS_SUCCESS
;
2342 Irp
->IoStatus
.Information
= sizeof(PARTITION_INFORMATION_EX
);
2347 case IOCTL_DISK_SET_PARTITION_INFO
:
2349 if (diskData
->PartitionNumber
== 0) {
2351 status
= STATUS_UNSUCCESSFUL
;
2355 PSET_PARTITION_INFORMATION inputBuffer
=
2356 (PSET_PARTITION_INFORMATION
)Irp
->AssociatedIrp
.SystemBuffer
;
2359 // Validate buffer length.
2362 if (irpStack
->Parameters
.DeviceIoControl
.InputBufferLength
<
2363 sizeof(SET_PARTITION_INFORMATION
)) {
2365 status
= STATUS_INFO_LENGTH_MISMATCH
;
2370 // The HAL routines IoGet- and IoSetPartitionInformation were
2371 // developed before support of dynamic partitioning and therefore
2372 // don't distinguish between partition ordinal (that is the order
2373 // of a partition on a disk) and the partition number. (The
2374 // partition number is assigned to a partition to identify it to
2375 // the system.) Use partition ordinals for these legacy calls.
2378 status
= IoSetPartitionInformation(
2379 deviceExtension
->PhysicalDevice
,
2380 deviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
,
2381 diskData
->PartitionOrdinal
,
2382 inputBuffer
->PartitionType
);
2384 if (NT_SUCCESS(status
)) {
2386 diskData
->PartitionType
= inputBuffer
->PartitionType
;
2392 case IOCTL_DISK_GET_DRIVE_LAYOUT
:
2395 // Return the partition layout for the physical drive. Note that
2396 // the layout is returned for the actual physical drive, regardless
2397 // of which partition was specified for the request.
2400 if (irpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
2401 sizeof(DRIVE_LAYOUT_INFORMATION
)) {
2402 status
= STATUS_INFO_LENGTH_MISMATCH
;
2406 PDRIVE_LAYOUT_INFORMATION partitionList
;
2407 PDEVICE_EXTENSION physicalExtension
= deviceExtension
;
2408 PPARTITION_INFORMATION partitionEntry
;
2409 PDISK_DATA diskData
;
2414 // Read partition information.
2417 status
= IoReadPartitionTable(deviceExtension
->PhysicalDevice
,
2418 deviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
,
2422 if (!NT_SUCCESS(status
)) {
2427 // The disk layout has been returned in the partitionList
2428 // buffer. Determine its size and, if the data will fit
2429 // into the intermediatery buffer, return it.
2432 tempSize
= FIELD_OFFSET(DRIVE_LAYOUT_INFORMATION
,PartitionEntry
[0]);
2433 tempSize
+= partitionList
->PartitionCount
*
2434 sizeof(PARTITION_INFORMATION
);
2437 irpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
) {
2439 status
= STATUS_BUFFER_TOO_SMALL
;
2440 ExFreePool(partitionList
);
2445 // Walk partition list to associate partition numbers with
2446 // partition entries.
2449 for (i
= 0; i
< partitionList
->PartitionCount
; i
++) {
2452 // Walk partition chain anchored at physical disk extension.
2455 deviceExtension
= physicalExtension
;
2456 diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
2460 deviceExtension
= diskData
->NextPartition
;
2463 // Check if this is the last partition in the chain.
2466 if (!deviceExtension
) {
2471 // Get the partition device extension from disk data.
2474 diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
2477 // Check if this partition is not currently being used.
2480 if (!deviceExtension
->PartitionLength
.QuadPart
) {
2484 partitionEntry
= &partitionList
->PartitionEntry
[i
];
2487 // Check if empty, or describes extended partiton or hasn't changed.
2490 if (partitionEntry
->PartitionType
== PARTITION_ENTRY_UNUSED
||
2491 IsContainerPartition(partitionEntry
->PartitionType
)) {
2496 // Check if new partition starts where this partition starts.
2499 if (partitionEntry
->StartingOffset
.QuadPart
!=
2500 deviceExtension
->StartingOffset
.QuadPart
) {
2505 // Check if partition length is the same.
2508 if (partitionEntry
->PartitionLength
.QuadPart
==
2509 deviceExtension
->PartitionLength
.QuadPart
) {
2512 // Partitions match. Update partition number.
2515 partitionEntry
->PartitionNumber
=
2516 diskData
->PartitionNumber
;
2524 // Copy partition information to system buffer.
2527 RtlMoveMemory(Irp
->AssociatedIrp
.SystemBuffer
,
2530 status
= STATUS_SUCCESS
;
2531 Irp
->IoStatus
.Information
= tempSize
;
2534 // Finally, free the buffer allocated by reading the
2538 ExFreePool(partitionList
);
2543 case IOCTL_DISK_SET_DRIVE_LAYOUT
:
2548 // Update the disk with new partition information.
2551 PDRIVE_LAYOUT_INFORMATION partitionList
= Irp
->AssociatedIrp
.SystemBuffer
;
2554 // Validate buffer length.
2557 if (irpStack
->Parameters
.DeviceIoControl
.InputBufferLength
<
2558 sizeof(DRIVE_LAYOUT_INFORMATION
)) {
2560 status
= STATUS_INFO_LENGTH_MISMATCH
;
2564 length
= sizeof(DRIVE_LAYOUT_INFORMATION
) +
2565 (partitionList
->PartitionCount
- 1) * sizeof(PARTITION_INFORMATION
);
2568 if (irpStack
->Parameters
.DeviceIoControl
.InputBufferLength
<
2571 status
= STATUS_BUFFER_TOO_SMALL
;
2576 // Verify that device object is for physical disk.
2579 if (deviceExtension
->PhysicalDevice
->DeviceExtension
!= deviceExtension
) {
2580 status
= STATUS_INVALID_PARAMETER
;
2585 // Walk through partition table comparing partitions to
2586 // existing partitions to create, delete and change
2587 // device objects as necessary.
2590 UpdateDeviceObjects(DeviceObject
,
2594 // Write changes to disk.
2597 status
= IoWritePartitionTable(
2598 deviceExtension
->DeviceObject
,
2599 deviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
,
2600 deviceExtension
->DiskGeometry
->Geometry
.SectorsPerTrack
,
2601 deviceExtension
->DiskGeometry
->Geometry
.TracksPerCylinder
,
2606 // Update IRP with bytes returned.
2609 if (NT_SUCCESS(status
)) {
2610 Irp
->IoStatus
.Information
= length
;
2615 case IOCTL_DISK_REASSIGN_BLOCKS
:
2618 // Map defective blocks to new location on disk.
2623 PREASSIGN_BLOCKS badBlocks
= Irp
->AssociatedIrp
.SystemBuffer
;
2629 // Validate buffer length.
2632 if (irpStack
->Parameters
.DeviceIoControl
.InputBufferLength
<
2633 sizeof(REASSIGN_BLOCKS
)) {
2635 status
= STATUS_INFO_LENGTH_MISMATCH
;
2639 bufferSize
= sizeof(REASSIGN_BLOCKS
) +
2640 (badBlocks
->Count
- 1) * sizeof(ULONG
);
2642 if (irpStack
->Parameters
.DeviceIoControl
.InputBufferLength
<
2645 status
= STATUS_INFO_LENGTH_MISMATCH
;
2650 // Build the data buffer to be transferred in the input buffer.
2651 // The format of the data to the device is:
2655 // x * 4 btyes Block Address
2657 // All values are big endian.
2660 badBlocks
->Reserved
= 0;
2661 blockCount
= badBlocks
->Count
;
2664 // Convert # of entries to # of bytes.
2668 badBlocks
->Count
= (USHORT
) ((blockCount
>> 8) & 0XFF);
2669 badBlocks
->Count
|= (USHORT
) ((blockCount
<< 8) & 0XFF00);
2672 // Convert back to number of entries.
2677 for (; blockCount
> 0; blockCount
--) {
2679 blockNumber
= badBlocks
->BlockNumber
[blockCount
-1];
2681 REVERSE_BYTES((PFOUR_BYTE
) &badBlocks
->BlockNumber
[blockCount
-1],
2682 (PFOUR_BYTE
) &blockNumber
);
2687 cdb
->CDB6GENERIC
.OperationCode
= SCSIOP_REASSIGN_BLOCKS
;
2690 // Set timeout value.
2693 srb
->TimeOutValue
= deviceExtension
->TimeOutValue
;
2695 status
= ScsiClassSendSrbSynchronous(DeviceObject
,
2701 Irp
->IoStatus
.Status
= status
;
2702 Irp
->IoStatus
.Information
= 0;
2704 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
2709 case IOCTL_DISK_IS_WRITABLE
:
2712 // Determine if the device is writable.
2715 modeData
= ExAllocatePool(NonPagedPoolCacheAligned
, MODE_DATA_SIZE
);
2717 if (modeData
== NULL
) {
2718 status
= STATUS_INSUFFICIENT_RESOURCES
;
2722 RtlZeroMemory(modeData
, MODE_DATA_SIZE
);
2724 length
= ScsiClassModeSense(DeviceObject
,
2727 MODE_SENSE_RETURN_ALL
);
2729 if (length
< sizeof(MODE_PARAMETER_HEADER
)) {
2732 // Retry the request in case of a check condition.
2735 length
= ScsiClassModeSense(DeviceObject
,
2738 MODE_SENSE_RETURN_ALL
);
2740 if (length
< sizeof(MODE_PARAMETER_HEADER
)) {
2741 status
= STATUS_IO_DEVICE_ERROR
;
2742 ExFreePool(modeData
);
2747 if (modeData
->DeviceSpecificParameter
& MODE_DSP_WRITE_PROTECT
) {
2748 status
= STATUS_MEDIA_WRITE_PROTECTED
;
2750 status
= STATUS_SUCCESS
;
2753 ExFreePool(modeData
);
2756 case IOCTL_DISK_INTERNAL_SET_VERIFY
:
2759 // If the caller is kernel mode, set the verify bit.
2762 if (Irp
->RequestorMode
== KernelMode
) {
2763 DeviceObject
->Flags
|= DO_VERIFY_VOLUME
;
2765 status
= STATUS_SUCCESS
;
2768 case IOCTL_DISK_INTERNAL_CLEAR_VERIFY
:
2771 // If the caller is kernel mode, clear the verify bit.
2774 if (Irp
->RequestorMode
== KernelMode
) {
2775 DeviceObject
->Flags
&= ~DO_VERIFY_VOLUME
;
2777 status
= STATUS_SUCCESS
;
2780 case IOCTL_DISK_FIND_NEW_DEVICES
:
2783 // Search for devices that have been powered on since the last
2784 // device search or system initialization.
2787 DebugPrint((3,"CdRomDeviceControl: Find devices\n"));
2788 status
= DriverEntry(DeviceObject
->DriverObject
,
2791 Irp
->IoStatus
.Status
= status
;
2793 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2796 case IOCTL_DISK_MEDIA_REMOVAL
:
2799 // If the disk is not removable then don't allow this command.
2802 if (!(DeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
)) {
2803 status
= STATUS_INVALID_DEVICE_REQUEST
;
2808 // Fall through and let the class driver process the request.
2814 // Free the Srb, since it is not needed.
2820 // Pass the request to the common device control routine.
2823 return(ScsiClassDeviceControl(DeviceObject
, Irp
));
2827 } // end switch( ...
2829 Irp
->IoStatus
.Status
= status
;
2831 if (!NT_SUCCESS(status
) && IoIsErrorUserInduced(status
)) {
2833 IoSetHardErrorOrVerifyDevice(Irp
, DeviceObject
);
2836 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2840 } // end ScsiDiskDeviceControl()
2844 ScsiDiskShutdownFlush (
2845 IN PDEVICE_OBJECT DeviceObject
,
2851 Routine Description:
2853 This routine is called for a shutdown and flush IRPs. These are sent by the
2854 system before it actually shuts down or when the file system does a flush.
2855 A synchronize cache command is sent to the device if it is write caching.
2856 If the device is removable an unlock command will be sent. This routine
2857 will sent a shutdown or flush Srb to the port driver.
2861 DriverObject - Pointer to device object to being shutdown by system.
2872 PDEVICE_EXTENSION deviceExtension
= DeviceObject
->DeviceExtension
;
2873 PIO_STACK_LOCATION irpStack
;
2874 PSCSI_REQUEST_BLOCK srb
;
2879 // Allocate SCSI request block.
2882 srb
= ExAllocatePool(NonPagedPool
, sizeof(SCSI_REQUEST_BLOCK
));
2887 // Set the status and complete the request.
2890 Irp
->IoStatus
.Status
= STATUS_INSUFFICIENT_RESOURCES
;
2891 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2892 return(STATUS_INSUFFICIENT_RESOURCES
);
2895 RtlZeroMemory(srb
, SCSI_REQUEST_BLOCK_SIZE
);
2898 // Write length to SRB.
2901 srb
->Length
= SCSI_REQUEST_BLOCK_SIZE
;
2904 // Set SCSI bus address.
2907 srb
->PathId
= deviceExtension
->PathId
;
2908 srb
->TargetId
= deviceExtension
->TargetId
;
2909 srb
->Lun
= deviceExtension
->Lun
;
2912 // Set timeout value and mark the request as not being a tagged request.
2915 srb
->TimeOutValue
= deviceExtension
->TimeOutValue
* 4;
2916 srb
->QueueTag
= SP_UNTAGGED
;
2917 srb
->QueueAction
= SRB_SIMPLE_TAG_REQUEST
;
2918 srb
->SrbFlags
= deviceExtension
->SrbFlags
;
2921 // If the write cache is enabled then send a synchronize cache request.
2924 if (deviceExtension
->DeviceFlags
& DEV_WRITE_CACHE
) {
2926 srb
->Function
= SRB_FUNCTION_EXECUTE_SCSI
;
2927 srb
->CdbLength
= 10;
2929 srb
->Cdb
[0] = SCSIOP_SYNCHRONIZE_CACHE
;
2931 status
= ScsiClassSendSrbSynchronous(DeviceObject
,
2937 DebugPrint((1, "ScsiDiskShutdownFlush: Synchonize cache sent. Status = %lx\n", status
));
2941 // Unlock the device if it is removable and this is a shutdown.
2944 irpStack
= IoGetCurrentIrpStackLocation(Irp
);
2946 if (DeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
&&
2947 irpStack
->MajorFunction
== IRP_MJ_SHUTDOWN
) {
2950 cdb
= (PVOID
) srb
->Cdb
;
2951 cdb
->MEDIA_REMOVAL
.OperationCode
= SCSIOP_MEDIUM_REMOVAL
;
2952 cdb
->MEDIA_REMOVAL
.Prevent
= FALSE
;
2955 // Set timeout value.
2958 srb
->TimeOutValue
= deviceExtension
->TimeOutValue
;
2959 status
= ScsiClassSendSrbSynchronous(DeviceObject
,
2965 DebugPrint((1, "ScsiDiskShutdownFlush: Unlock device request sent. Status = %lx\n", status
));
2971 // Save a few parameters in the current stack location.
2974 srb
->Function
= irpStack
->MajorFunction
== IRP_MJ_SHUTDOWN
?
2975 SRB_FUNCTION_SHUTDOWN
: SRB_FUNCTION_FLUSH
;
2978 // Set the retry count to zero.
2981 irpStack
->Parameters
.Others
.Argument4
= (PVOID
) 0;
2984 // Set up IoCompletion routine address.
2987 IoSetCompletionRoutine(Irp
, ScsiClassIoComplete
, srb
, TRUE
, TRUE
, TRUE
);
2990 // Get next stack location and
2991 // set major function code.
2994 irpStack
= IoGetNextIrpStackLocation(Irp
);
2996 irpStack
->MajorFunction
= IRP_MJ_SCSI
;
2999 // Set up SRB for execute scsi request.
3000 // Save SRB address in next stack for port driver.
3003 irpStack
->Parameters
.Scsi
.Srb
= srb
;
3006 // Set up Irp Address.
3009 srb
->OriginalRequest
= Irp
;
3012 // Call the port driver to process the request.
3015 return(IoCallDriver(deviceExtension
->PortDeviceObject
, Irp
));
3017 } // end ScsiDiskShutdown()
3023 PDEVICE_OBJECT DeviceObject
3027 Routine Description:
3029 The routine performs the necessary functions to determine if a device is
3030 really a floppy rather than a harddisk. This is done by a mode sense
3031 command. First, a check is made to see if the medimum type is set. Second
3032 a check is made for the flexible parameters mode page. Also a check is
3033 made to see if the write cache is enabled.
3037 DeviceObject - Supplies the device object to be tested.
3041 Return TRUE if the indicated device is a floppy.
3045 PDEVICE_EXTENSION deviceExtension
= DeviceObject
->DeviceExtension
;
3052 modeData
= ExAllocatePool(NonPagedPoolCacheAligned
, MODE_DATA_SIZE
);
3054 if (modeData
== NULL
) {
3058 RtlZeroMemory(modeData
, MODE_DATA_SIZE
);
3060 length
= ScsiClassModeSense(DeviceObject
,
3063 MODE_SENSE_RETURN_ALL
);
3065 if (length
< sizeof(MODE_PARAMETER_HEADER
)) {
3068 // Retry the request in case of a check condition.
3071 length
= ScsiClassModeSense(DeviceObject
,
3074 MODE_SENSE_RETURN_ALL
);
3076 if (length
< sizeof(MODE_PARAMETER_HEADER
)) {
3078 ExFreePool(modeData
);
3085 // If the length is greater than length indicated by the mode data reset
3086 // the data to the mode data.
3089 if (length
> (ULONG
) ((PMODE_PARAMETER_HEADER
) modeData
)->ModeDataLength
+ 1) {
3090 length
= ((PMODE_PARAMETER_HEADER
) modeData
)->ModeDataLength
+ 1;
3094 // Look for the flexible disk mode page.
3097 pageData
= ScsiClassFindModePage( modeData
, length
, MODE_PAGE_FLEXIBILE
, TRUE
);
3099 if (pageData
!= NULL
) {
3101 DebugPrint((1, "Scsidisk: Flexible disk page found, This is a floppy.\n"));
3102 ExFreePool(modeData
);
3107 // Check to see if the write cache is enabled.
3110 pageData
= ScsiClassFindModePage( modeData
, length
, MODE_PAGE_CACHING
, TRUE
);
3113 // Assume that write cache is disabled or not supported.
3116 deviceExtension
->DeviceFlags
&= ~DEV_WRITE_CACHE
;
3119 // Check if valid caching page exists.
3122 if (pageData
!= NULL
) {
3125 // Check if write cache is disabled.
3128 if (((PMODE_CACHING_PAGE
)pageData
)->WriteCacheEnable
) {
3131 "SCSIDISK: Disk write cache enabled\n"));
3134 // Check if forced unit access (FUA) is supported.
3137 if (((PMODE_PARAMETER_HEADER
)modeData
)->DeviceSpecificParameter
& MODE_DSP_FUA_SUPPORTED
) {
3139 deviceExtension
->DeviceFlags
|= DEV_WRITE_CACHE
;
3144 "SCSIDISK: Disk does not support FUA or DPO\n"));
3154 ExFreePool(modeData
);
3157 } // end IsFloppyDevice()
3163 IN PDEVICE_OBJECT DeviceObject
,
3164 IN PCHAR ModeSelectBuffer
,
3171 Routine Description:
3173 This routine sends a mode select command.
3177 DeviceObject - Supplies the device object associated with this request.
3179 ModeSelectBuffer - Supplies a buffer containing the page data.
3181 Length - Supplies the length in bytes of the mode select buffer.
3183 SavePage - Indicates that parameters should be written to disk.
3187 Length of the transferred data is returned.
3191 PDEVICE_EXTENSION deviceExtension
= DeviceObject
->DeviceExtension
;
3193 SCSI_REQUEST_BLOCK srb
;
3198 PMODE_PARAMETER_BLOCK blockDescriptor
;
3202 length2
= Length
+ sizeof(MODE_PARAMETER_HEADER
) + sizeof(MODE_PARAMETER_BLOCK
);
3205 // Allocate buffer for mode select header, block descriptor, and mode page.
3208 buffer
= (ULONG_PTR
)ExAllocatePool(NonPagedPoolCacheAligned
,length2
);
3210 RtlZeroMemory((PVOID
)buffer
, length2
);
3213 // Set length in header to size of mode page.
3216 ((PMODE_PARAMETER_HEADER
)buffer
)->BlockDescriptorLength
= sizeof(MODE_PARAMETER_BLOCK
);
3218 blockDescriptor
= (PMODE_PARAMETER_BLOCK
)(buffer
+ 1);
3224 blockDescriptor
->BlockLength
[1]=0x02;
3227 // Copy mode page to buffer.
3230 RtlCopyMemory((PVOID
)(buffer
+ 3), ModeSelectBuffer
, Length
);
3236 RtlZeroMemory(&srb
, sizeof(SCSI_REQUEST_BLOCK
));
3239 // Build the MODE SELECT CDB.
3243 cdb
= (PCDB
)srb
.Cdb
;
3246 // Set timeout value from device extension.
3249 srb
.TimeOutValue
= deviceExtension
->TimeOutValue
* 2;
3251 cdb
->MODE_SELECT
.OperationCode
= SCSIOP_MODE_SELECT
;
3252 cdb
->MODE_SELECT
.SPBit
= SavePage
;
3253 cdb
->MODE_SELECT
.PFBit
= 1;
3254 cdb
->MODE_SELECT
.ParameterListLength
= (UCHAR
)(length2
);
3258 status
= ScsiClassSendSrbSynchronous(DeviceObject
,
3265 if (status
== STATUS_VERIFY_REQUIRED
) {
3268 // Routine ScsiClassSendSrbSynchronous does not retry requests returned with
3281 } else if (SRB_STATUS(srb
.SrbStatus
) == SRB_STATUS_DATA_OVERRUN
) {
3282 status
= STATUS_SUCCESS
;
3285 ExFreePool((PVOID
)buffer
);
3287 if (NT_SUCCESS(status
)) {
3293 } // end SciDiskModeSelect()
3299 IN PDEVICE_OBJECT DeviceObject
,
3300 IN PSCSI_INQUIRY_DATA LunInfo
3304 PDEVICE_EXTENSION deviceExtension
= DeviceObject
->DeviceExtension
;
3305 PINQUIRYDATA InquiryData
= (PINQUIRYDATA
)LunInfo
->InquiryData
;
3306 BAD_CONTROLLER_INFORMATION
const *controller
;
3311 for (j
= 0; j
< NUMBER_OF_BAD_CONTROLLERS
; j
++) {
3313 controller
= &ScsiDiskBadControllers
[j
];
3315 if (!controller
->DisableWriteCache
|| strncmp(controller
->InquiryString
, (PCCHAR
)InquiryData
->VendorId
, strlen(controller
->InquiryString
))) {
3319 DebugPrint((1, "ScsiDisk.DisableWriteCache, Found bad controller! %s\n", controller
->InquiryString
));
3321 modeData
= ExAllocatePool(NonPagedPoolCacheAligned
, MODE_DATA_SIZE
);
3323 if (modeData
== NULL
) {
3326 "ScsiDisk.DisableWriteCache: Check for write-cache enable failed\n"));
3330 RtlZeroMemory(modeData
, MODE_DATA_SIZE
);
3332 length
= ScsiClassModeSense(DeviceObject
,
3335 MODE_SENSE_RETURN_ALL
);
3337 if (length
< sizeof(MODE_PARAMETER_HEADER
)) {
3340 // Retry the request in case of a check condition.
3343 length
= ScsiClassModeSense(DeviceObject
,
3346 MODE_SENSE_RETURN_ALL
);
3348 if (length
< sizeof(MODE_PARAMETER_HEADER
)) {
3352 "ScsiDisk.DisableWriteCache: Mode Sense failed\n"));
3354 ExFreePool(modeData
);
3361 // If the length is greater than length indicated by the mode data reset
3362 // the data to the mode data.
3365 if (length
> (ULONG
) ((PMODE_PARAMETER_HEADER
) modeData
)->ModeDataLength
+ 1) {
3366 length
= ((PMODE_PARAMETER_HEADER
) modeData
)->ModeDataLength
+ 1;
3370 // Check to see if the write cache is enabled.
3373 pageData
= ScsiClassFindModePage( modeData
, length
, MODE_PAGE_CACHING
, TRUE
);
3376 // Assume that write cache is disabled or not supported.
3379 deviceExtension
->DeviceFlags
&= ~DEV_WRITE_CACHE
;
3382 // Check if valid caching page exists.
3385 if (pageData
!= NULL
) {
3387 BOOLEAN savePage
= FALSE
;
3389 savePage
= (BOOLEAN
)(((PMODE_CACHING_PAGE
)pageData
)->PageSavable
);
3392 // Check if write cache is disabled.
3395 if (((PMODE_CACHING_PAGE
)pageData
)->WriteCacheEnable
) {
3397 PIO_ERROR_LOG_PACKET errorLogEntry
;
3402 // Disable write cache and ensure necessary fields are zeroed.
3405 ((PMODE_CACHING_PAGE
)pageData
)->WriteCacheEnable
= FALSE
;
3406 ((PMODE_CACHING_PAGE
)pageData
)->Reserved
= 0;
3407 ((PMODE_CACHING_PAGE
)pageData
)->PageSavable
= 0;
3408 ((PMODE_CACHING_PAGE
)pageData
)->Reserved2
= 0;
3411 // Extract length from caching page.
3414 length
= ((PMODE_CACHING_PAGE
)pageData
)->PageLength
;
3417 // Compensate for page code and page length.
3423 // Issue mode select to set the parameter.
3426 if (ScsiDiskModeSelect(DeviceObject
,
3432 "SCSIDISK: Disk write cache disabled\n"));
3434 deviceExtension
->DeviceFlags
&= ~DEV_WRITE_CACHE
;
3435 errorCode
= IO_WRITE_CACHE_DISABLED
;
3438 if (ScsiDiskModeSelect(DeviceObject
,
3444 "SCSIDISK: Disk write cache disabled\n"));
3447 deviceExtension
->DeviceFlags
&= ~DEV_WRITE_CACHE
;
3448 errorCode
= IO_WRITE_CACHE_DISABLED
;
3453 "SCSIDISK: Mode select to disable write cache failed\n"));
3455 deviceExtension
->DeviceFlags
|= DEV_WRITE_CACHE
;
3456 errorCode
= IO_WRITE_CACHE_ENABLED
;
3461 // Log the appropriate informational or error entry.
3464 errorLogEntry
= (PIO_ERROR_LOG_PACKET
)IoAllocateErrorLogEntry(
3466 sizeof(IO_ERROR_LOG_PACKET
) + 3
3469 if (errorLogEntry
!= NULL
) {
3471 errorLogEntry
->FinalStatus
= STATUS_SUCCESS
;
3472 errorLogEntry
->ErrorCode
= errorCode
;
3473 errorLogEntry
->SequenceNumber
= 0;
3474 errorLogEntry
->MajorFunctionCode
= IRP_MJ_SCSI
;
3475 errorLogEntry
->IoControlCode
= 0;
3476 errorLogEntry
->RetryCount
= 0;
3477 errorLogEntry
->UniqueErrorValue
= 0x1;
3478 errorLogEntry
->DumpDataSize
= 3 * sizeof(ULONG
);
3479 errorLogEntry
->DumpData
[0] = LunInfo
->PathId
;
3480 errorLogEntry
->DumpData
[1] = LunInfo
->TargetId
;
3481 errorLogEntry
->DumpData
[2] = LunInfo
->Lun
;
3484 // Write the error log packet.
3487 IoWriteErrorLogEntry(errorLogEntry
);
3493 // Found device so exit the loop and return.
3505 CalculateMbrCheckSum(
3506 IN PDEVICE_EXTENSION DeviceExtension
,
3512 Routine Description:
3514 Read MBR and calculate checksum.
3518 DeviceExtension - Supplies a pointer to the device information for disk.
3519 Checksum - Memory location to return MBR checksum.
3523 Returns TRUE if checksum is valid.
3527 LARGE_INTEGER sectorZero
;
3529 IO_STATUS_BLOCK ioStatus
;
3537 sectorZero
.QuadPart
= (LONGLONG
) 0;
3540 // Create notification event object to be used to signal the inquiry
3541 // request completion.
3544 KeInitializeEvent(&event
, NotificationEvent
, FALSE
);
3550 sectorSize
= DeviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
;
3553 // Make sure sector size is at least 512 bytes.
3556 if (sectorSize
< 512) {
3561 // Allocate buffer for sector read.
3564 mbr
= ExAllocatePool(NonPagedPoolCacheAligned
, sectorSize
);
3571 // Build IRP to read MBR.
3574 irp
= IoBuildSynchronousFsdRequest(IRP_MJ_READ
,
3575 DeviceExtension
->DeviceObject
,
3588 // Pass request to port driver and wait for request to complete.
3591 status
= IoCallDriver(DeviceExtension
->DeviceObject
,
3594 if (status
== STATUS_PENDING
) {
3595 KeWaitForSingleObject(&event
,
3600 status
= ioStatus
.Status
;
3603 if (!NT_SUCCESS(status
)) {
3609 // Calculate MBR checksum.
3614 for (i
= 0; i
< 128; i
++) {
3615 *Checksum
+= mbr
[i
];
3618 *Checksum
= ~*Checksum
+ 1;
3628 IN PDEVICE_EXTENSION DeviceExtension
,
3635 Routine Description:
3637 The routine queries the registry to determine if this disk is visible to
3638 the BIOS. If the disk is visable to the BIOS, then the geometry information
3643 DeviceExtension - Supplies a pointer to the device information for disk.
3644 Signature - Unique identifier recorded in MBR.
3645 BusKey - Handle of bus key.
3646 DiskNumber - Returns ordinal of disk as BIOS sees it.
3650 TRUE is disk signature matched.
3654 PDISK_DATA diskData
= (PDISK_DATA
)(DeviceExtension
+ 1);
3655 BOOLEAN diskFound
= FALSE
;
3656 OBJECT_ATTRIBUTES objectAttributes
;
3657 UNICODE_STRING unicodeString
;
3658 UNICODE_STRING identifier
;
3660 ULONG adapterNumber
;
3668 STRING anotherString
;
3671 PKEY_VALUE_FULL_INFORMATION keyData
;
3675 for (busNumber
= 0; ; busNumber
++) {
3678 // Open controller name key.
3681 sprintf((PCHAR
)buffer
,
3685 RtlInitString(&string
,
3688 status
= RtlAnsiStringToUnicodeString(&unicodeString
,
3692 if (!NT_SUCCESS(status
)){
3696 InitializeObjectAttributes(&objectAttributes
,
3698 OBJ_CASE_INSENSITIVE
,
3700 (PSECURITY_DESCRIPTOR
)NULL
);
3702 status
= ZwOpenKey(&spareKey
,
3706 RtlFreeUnicodeString(&unicodeString
);
3708 if (!NT_SUCCESS(status
)) {
3713 // Open up controller ordinal key.
3716 RtlInitUnicodeString(&unicodeString
, L
"DiskController");
3717 InitializeObjectAttributes(&objectAttributes
,
3719 OBJ_CASE_INSENSITIVE
,
3721 (PSECURITY_DESCRIPTOR
)NULL
);
3723 status
= ZwOpenKey(&adapterKey
,
3728 // This could fail even with additional adapters of this type
3732 if (!NT_SUCCESS(status
)) {
3736 for (adapterNumber
= 0; ; adapterNumber
++) {
3742 sprintf((PCHAR
)buffer
,
3743 "%lu\\DiskPeripheral",
3746 RtlInitString(&string
,
3749 status
= RtlAnsiStringToUnicodeString(&unicodeString
,
3753 if (!NT_SUCCESS(status
)){
3757 InitializeObjectAttributes(&objectAttributes
,
3759 OBJ_CASE_INSENSITIVE
,
3761 (PSECURITY_DESCRIPTOR
)NULL
);
3763 status
= ZwOpenKey(&diskKey
,
3767 RtlFreeUnicodeString(&unicodeString
);
3769 if (!NT_SUCCESS(status
)) {
3773 for (diskNumber
= 0; ; diskNumber
++) {
3775 sprintf((PCHAR
)buffer
,
3779 RtlInitString(&string
,
3782 status
= RtlAnsiStringToUnicodeString(&unicodeString
,
3786 if (!NT_SUCCESS(status
)){
3790 InitializeObjectAttributes(&objectAttributes
,
3792 OBJ_CASE_INSENSITIVE
,
3794 (PSECURITY_DESCRIPTOR
)NULL
);
3796 status
= ZwOpenKey(&targetKey
,
3800 RtlFreeUnicodeString(&unicodeString
);
3802 if (!NT_SUCCESS(status
)) {
3807 // Allocate buffer for registry query.
3810 keyData
= ExAllocatePool(PagedPool
, VALUE_BUFFER_SIZE
);
3812 if (keyData
== NULL
) {
3818 // Get disk peripheral identifier.
3821 RtlInitUnicodeString(&unicodeString
, L
"Identifier");
3822 status
= ZwQueryValueKey(targetKey
,
3824 KeyValueFullInformation
,
3831 if (!NT_SUCCESS(status
)) {
3836 // Complete unicode string.
3840 (PWSTR
)((PUCHAR
)keyData
+ keyData
->DataOffset
);
3841 identifier
.Length
= (USHORT
)keyData
->DataLength
;
3842 identifier
.MaximumLength
= (USHORT
)keyData
->DataLength
;
3845 // Convert unicode identifier to ansi string.
3849 RtlUnicodeStringToAnsiString(&anotherString
,
3853 if (!NT_SUCCESS(status
)) {
3858 // If checksum is zero, then the MBR is valid and
3859 // the signature is meaningful.
3862 if (diskData
->MbrCheckSum
) {
3865 // Convert checksum to ansi string.
3868 sprintf((PCHAR
)buffer
, "%08lx", diskData
->MbrCheckSum
);
3873 // Convert signature to ansi string.
3876 sprintf((PCHAR
)buffer
, "%08lx", diskData
->Signature
);
3879 // Make string point at signature. Can't use scan
3880 // functions because they are not exported for driver use.
3883 anotherString
.Buffer
+=9;
3887 // Convert to ansi string.
3890 RtlInitString(&string
,
3895 // Make string lengths equal.
3898 anotherString
.Length
= string
.Length
;
3901 // Check if strings match.
3904 if (RtlCompareString(&string
,
3909 *DiskNumber
= diskNumber
;
3912 ExFreePool(keyData
);
3915 // Readjust indentifier string if necessary.
3918 if (!diskData
->MbrCheckSum
) {
3919 anotherString
.Buffer
-=9;
3922 RtlFreeAnsiString(&anotherString
);
3932 ZwClose(adapterKey
);
3938 } // end EnumerateBusKey()
3944 IN PDEVICE_EXTENSION DeviceExtension
3948 Routine Description:
3950 The routine queries the registry to determine if this disk is visible to
3951 the BIOS. If the disk is visable to the BIOS, then the geometry information
3956 DeviceExtension - Supplies a pointer to the device information for disk.
3965 OBJECT_ATTRIBUTES objectAttributes
;
3966 UNICODE_STRING unicodeString
;
3970 PCM_INT13_DRIVE_PARAMETER driveParameters
;
3971 PCM_FULL_RESOURCE_DESCRIPTOR resourceDescriptor
;
3972 PKEY_VALUE_FULL_INFORMATION keyData
;
3976 ULONG numberOfDrives
;
3979 ULONG sectorsPerTrack
;
3980 ULONG tracksPerCylinder
;
3981 BOOLEAN foundEZHooker
;
3987 // Initialize the object for the key.
3990 InitializeObjectAttributes(&objectAttributes
,
3991 DeviceExtension
->DeviceObject
->DriverObject
->HardwareDatabase
,
3992 OBJ_CASE_INSENSITIVE
,
3994 (PSECURITY_DESCRIPTOR
) NULL
);
3997 // Create the hardware base key.
4000 status
= ZwOpenKey(&hardwareKey
,
4005 if (!NT_SUCCESS(status
)) {
4006 DebugPrint((1, "ScsiDisk UpdateParameters: Cannot open hardware data. Name: %wZ\n", DeviceExtension
->DeviceObject
->DriverObject
->HardwareDatabase
));
4012 // Get disk BIOS geometry information.
4015 RtlInitUnicodeString(&unicodeString
, L
"Configuration Data");
4017 keyData
= ExAllocatePool(PagedPool
, VALUE_BUFFER_SIZE
);
4019 if (keyData
== NULL
) {
4020 ZwClose(hardwareKey
);
4024 status
= ZwQueryValueKey(hardwareKey
,
4026 KeyValueFullInformation
,
4031 if (!NT_SUCCESS(status
)) {
4033 "SCSIDISK: ExtractBiosGeometry: Can't query configuration data (%x)\n",
4035 ExFreePool(keyData
);
4040 // Open EISA bus key.
4043 RtlInitUnicodeString(&unicodeString
, L
"EisaAdapter");
4045 InitializeObjectAttributes(&objectAttributes
,
4047 OBJ_CASE_INSENSITIVE
,
4049 (PSECURITY_DESCRIPTOR
)NULL
);
4051 status
= ZwOpenKey(&busKey
,
4055 if (!NT_SUCCESS(status
)) {
4060 "SCSIDISK: UpdateGeometry: Opened EisaAdapter key\n"));
4061 if (EnumerateBusKey(DeviceExtension
,
4065 ZwClose(hardwareKey
);
4072 // Open Multifunction bus key.
4075 RtlInitUnicodeString(&unicodeString
, L
"MultifunctionAdapter");
4077 InitializeObjectAttributes(&objectAttributes
,
4079 OBJ_CASE_INSENSITIVE
,
4081 (PSECURITY_DESCRIPTOR
)NULL
);
4083 status
= ZwOpenKey(&busKey
,
4087 ZwClose(hardwareKey
);
4088 if (NT_SUCCESS(status
)) {
4090 "SCSIDISK: UpdateGeometry: Opened MultifunctionAdapter key\n"));
4091 if (EnumerateBusKey(DeviceExtension
,
4099 ExFreePool(keyData
);
4104 resourceDescriptor
= (PCM_FULL_RESOURCE_DESCRIPTOR
)((PUCHAR
)keyData
+
4105 keyData
->DataOffset
);
4108 // Check that the data is long enough to hold a full resource descriptor,
4109 // and that the last resouce list is device-specific and long enough.
4112 if (keyData
->DataLength
< sizeof(CM_FULL_RESOURCE_DESCRIPTOR
) ||
4113 resourceDescriptor
->PartialResourceList
.Count
== 0 ||
4114 resourceDescriptor
->PartialResourceList
.PartialDescriptors
[0].Type
!=
4115 CmResourceTypeDeviceSpecific
||
4116 resourceDescriptor
->PartialResourceList
.PartialDescriptors
[0]
4117 .u
.DeviceSpecificData
.DataSize
< sizeof(ULONG
)) {
4119 DebugPrint((1, "SCSIDISK: ExtractBiosGeometry: BIOS header data too small or invalid\n"));
4120 ExFreePool(keyData
);
4125 resourceDescriptor
->PartialResourceList
.PartialDescriptors
[0].u
.DeviceSpecificData
.DataSize
;
4128 // Point to the BIOS data. The BIOS data is located after the first
4129 // partial Resource list which should be device specific data.
4132 buffer
= (PUCHAR
) keyData
+ keyData
->DataOffset
+
4133 sizeof(CM_FULL_RESOURCE_DESCRIPTOR
);
4136 numberOfDrives
= length
/ sizeof(CM_INT13_DRIVE_PARAMETER
);
4139 // Use the defaults if the drive number is greater than the
4140 // number of drives detected by the BIOS.
4143 if (numberOfDrives
<= diskNumber
) {
4144 ExFreePool(keyData
);
4149 // Point to the array of drive parameters.
4152 driveParameters
= (PCM_INT13_DRIVE_PARAMETER
) buffer
+ diskNumber
;
4153 cylinders
= driveParameters
->MaxCylinders
+ 1;
4154 sectorsPerTrack
= driveParameters
->SectorsPerTrack
;
4155 tracksPerCylinder
= driveParameters
->MaxHeads
+1;
4158 // Calculate the actual number of sectors.
4161 sectors
= (ULONG
)(DeviceExtension
->PartitionLength
.QuadPart
>>
4162 DeviceExtension
->SectorShift
);
4165 if (sectors
>= cylinders
* tracksPerCylinder
* sectorsPerTrack
) {
4166 DebugPrint((1, "ScsiDisk: UpdateGeometry: Disk smaller than BIOS indicated\n"
4167 "SCSIDISK: Sectors: %x, Cylinders: %x, Track per Cylinder: %x Sectors per track: %x\n",
4168 sectors
, cylinders
, tracksPerCylinder
, sectorsPerTrack
));
4173 // Since the BIOS may not report the full drive, recalculate the drive
4174 // size based on the volume size and the BIOS values for tracks per
4175 // cylinder and sectors per track..
4178 length
= tracksPerCylinder
* sectorsPerTrack
;
4183 // The BIOS information is bogus.
4186 DebugPrint((1, "ScsiDisk UpdateParameters: sectorPerTrack zero\n"));
4187 ExFreePool(keyData
);
4191 cylinders
= sectors
/ length
;
4194 // Update the actual geometry information.
4197 DeviceExtension
->DiskGeometry
->Geometry
.SectorsPerTrack
= sectorsPerTrack
;
4198 DeviceExtension
->DiskGeometry
->Geometry
.TracksPerCylinder
= tracksPerCylinder
;
4199 DeviceExtension
->DiskGeometry
->Geometry
.Cylinders
.QuadPart
= (LONGLONG
)cylinders
;
4200 DeviceExtension
->DiskGeometry
->DiskSize
.QuadPart
= cylinders
* tracksPerCylinder
* sectorsPerTrack
*
4201 DeviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
;
4204 "SCSIDISK: UpdateGeometry: BIOS spt %x, #heads %x, #cylinders %x\n",
4209 ExFreePool(keyData
);
4211 foundEZHooker
= FALSE
;
4213 if (!DeviceExtension
->DMActive
) {
4215 HalExamineMBR(DeviceExtension
->DeviceObject
,
4216 DeviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
,
4224 foundEZHooker
= TRUE
;
4230 if (DeviceExtension
->DMActive
|| foundEZHooker
) {
4232 while (cylinders
> 1024) {
4234 tracksPerCylinder
= tracksPerCylinder
*2;
4235 cylinders
= cylinders
/2;
4240 // int 13 values are always 1 less.
4243 tracksPerCylinder
-= 1;
4247 // DM reserves the CE cylinder
4252 DeviceExtension
->DiskGeometry
->Geometry
.Cylinders
.QuadPart
= cylinders
+ 1;
4253 DeviceExtension
->DiskGeometry
->Geometry
.TracksPerCylinder
= tracksPerCylinder
+ 1;
4255 DeviceExtension
->PartitionLength
.QuadPart
=
4256 DeviceExtension
->DiskGeometry
->DiskSize
.QuadPart
=
4257 DeviceExtension
->DiskGeometry
->Geometry
.Cylinders
.QuadPart
*
4258 DeviceExtension
->DiskGeometry
->Geometry
.SectorsPerTrack
*
4259 DeviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
*
4260 DeviceExtension
->DiskGeometry
->Geometry
.TracksPerCylinder
;
4262 if (DeviceExtension
->DMActive
) {
4264 DeviceExtension
->DMByteSkew
= DeviceExtension
->DMSkew
* DeviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
;
4270 DeviceExtension
->DMByteSkew
= 0;
4276 } // end UpdateGeometry()
4282 UpdateRemovableGeometry (
4283 IN PDEVICE_OBJECT DeviceObject
,
4289 Routine Description:
4291 This routines updates the size and starting offset of the device. This is
4292 used when the media on the device may have changed thereby changing the
4293 size of the device. If this is the physical device then a
4294 ScsiClassReadDriveCapacity is done; otherewise, a read partition table is done.
4298 DeviceObject - Supplies the device object whos size needs to be updated.
4300 Irp - Supplies a reference where the status can be updated.
4304 Returns the status of the opertion.
4309 PDEVICE_EXTENSION deviceExtension
= DeviceObject
->DeviceExtension
;
4310 PDRIVE_LAYOUT_INFORMATION partitionList
;
4312 PDISK_DATA diskData
;
4313 ULONG partitionNumber
;
4316 // Determine if the size of the partition may have changed because
4317 // the media has changed.
4320 if (!(DeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
)) {
4322 return(STATUS_SUCCESS
);
4327 // If this request is for partition zero then do a read drive
4328 // capacity otherwise do a I/O read partition table.
4331 diskData
= (PDISK_DATA
) (deviceExtension
+ 1);
4334 // Read the drive capcity. If that fails, give up.
4337 status
= ScsiClassReadDriveCapacity(deviceExtension
->PhysicalDevice
);
4339 if (!NT_SUCCESS(status
)) {
4344 // Read the partition table agian.
4347 status
= IoReadPartitionTable(deviceExtension
->PhysicalDevice
,
4348 deviceExtension
->DiskGeometry
->Geometry
.BytesPerSector
,
4353 if (!NT_SUCCESS(status
)) {
4356 // Fail the request.
4362 if (diskData
->PartitionNumber
!= 0 &&
4363 diskData
->PartitionNumber
<= partitionList
->PartitionCount
) {
4365 partitionNumber
= diskData
->PartitionNumber
- 1;
4368 // Update the partition information for this parition.
4371 diskData
->PartitionType
=
4372 partitionList
->PartitionEntry
[partitionNumber
].PartitionType
;
4374 diskData
->BootIndicator
=
4375 partitionList
->PartitionEntry
[partitionNumber
].BootIndicator
;
4377 deviceExtension
->StartingOffset
=
4378 partitionList
->PartitionEntry
[partitionNumber
].StartingOffset
;
4380 deviceExtension
->PartitionLength
=
4381 partitionList
->PartitionEntry
[partitionNumber
].PartitionLength
;
4383 diskData
->HiddenSectors
=
4384 partitionList
->PartitionEntry
[partitionNumber
].HiddenSectors
;
4386 deviceExtension
->SectorShift
= ((PDEVICE_EXTENSION
)
4387 deviceExtension
->PhysicalDevice
->DeviceExtension
)->SectorShift
;
4389 } else if (diskData
->PartitionNumber
!= 0) {
4392 // The paritition does not exist. Zero all the data.
4395 diskData
->PartitionType
= 0;
4396 diskData
->BootIndicator
= 0;
4397 diskData
->HiddenSectors
= 0;
4398 deviceExtension
->StartingOffset
.QuadPart
= (LONGLONG
)0;
4399 deviceExtension
->PartitionLength
.QuadPart
= (LONGLONG
)0;
4403 // Free the parition list allocate by I/O read partition table.
4406 ExFreePool(partitionList
);
4409 return(STATUS_SUCCESS
);
4415 ScsiDiskProcessError(
4416 PDEVICE_OBJECT DeviceObject
,
4417 PSCSI_REQUEST_BLOCK Srb
,
4423 Routine Description:
4425 This routine checks the type of error. If the error indicates an underrun
4426 then indicate the request should be retried.
4430 DeviceObject - Supplies a pointer to the device object.
4432 Srb - Supplies a pointer to the failing Srb.
4434 Status - Status with which the IRP will be completed.
4436 Retry - Indication of whether the request will be retried.
4445 PDEVICE_EXTENSION deviceExtension
= DeviceObject
->DeviceExtension
;
4447 if (*Status
== STATUS_DATA_OVERRUN
&&
4448 ( Srb
->Cdb
[0] == SCSIOP_WRITE
|| Srb
->Cdb
[0] == SCSIOP_READ
)) {
4453 // Update the error count for the device.
4456 deviceExtension
->ErrorCount
++;
4459 if (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_ERROR
&&
4460 Srb
->ScsiStatus
== SCSISTAT_BUSY
) {
4463 // The disk drive should never be busy this long. Reset the scsi bus
4464 // maybe this will clear the condition.
4467 ResetScsiBus(DeviceObject
);
4470 // Update the error count for the device.
4473 deviceExtension
->ErrorCount
++;
4480 PDEVICE_OBJECT DeviceObject
,
4481 PSCSI_INQUIRY_DATA LunInfo
,
4482 PIO_SCSI_CAPABILITIES PortCapabilities
4487 Routine Description:
4489 This function checks to see if an SCSI logical unit requires speical
4494 DeviceObject - Supplies the device object to be tested.
4496 InquiryData - Supplies the inquiry data returned by the device of interest.
4498 PortCapabilities - Supplies the capabilities of the device object.
4507 PDEVICE_EXTENSION deviceExtension
= DeviceObject
->DeviceExtension
;
4508 PINQUIRYDATA InquiryData
= (PINQUIRYDATA
)LunInfo
->InquiryData
;
4509 BAD_CONTROLLER_INFORMATION
const *controller
;
4512 for (j
= 0; j
< NUMBER_OF_BAD_CONTROLLERS
; j
++) {
4514 controller
= &ScsiDiskBadControllers
[j
];
4516 if (strncmp(controller
->InquiryString
, (PCCHAR
)InquiryData
->VendorId
, strlen(controller
->InquiryString
))) {
4520 DebugPrint((1, "ScsiDisk ScanForSpecial, Found bad controller! %s\n", controller
->InquiryString
));
4523 // Found a listed controller. Determine what must be done.
4526 if (controller
->DisableTaggedQueuing
) {
4529 // Disable tagged queuing.
4532 deviceExtension
->SrbFlags
&= ~SRB_FLAGS_QUEUE_ACTION_ENABLE
;
4535 if (controller
->DisableSynchronousTransfers
) {
4538 // Disable synchronous data transfers.
4541 deviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
4545 if (controller
->DisableDisconnects
) {
4548 // Disable disconnects.
4551 deviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_DISCONNECT
;
4556 // Found device so exit the loop and return.
4563 // Set the StartUnit flag appropriately.
4566 if (DeviceObject
->DeviceType
== FILE_DEVICE_DISK
) {
4567 deviceExtension
->DeviceFlags
|= DEV_SAFE_START_UNIT
;
4569 if (DeviceObject
->Characteristics
& FILE_REMOVABLE_MEDIA
) {
4570 if (_strnicmp((PCCHAR
)InquiryData
->VendorId
, "iomega", strlen("iomega"))) {
4571 deviceExtension
->DeviceFlags
&= ~DEV_SAFE_START_UNIT
;
4582 IN PDEVICE_OBJECT DeviceObject
4587 Routine Description:
4589 This command sends a reset bus command to the SCSI port driver.
4593 DeviceObject - The device object for the logical unit with
4602 PIO_STACK_LOCATION irpStack
;
4604 PDEVICE_EXTENSION deviceExtension
= DeviceObject
->DeviceExtension
;
4605 PSCSI_REQUEST_BLOCK srb
;
4606 PCOMPLETION_CONTEXT context
;
4608 DebugPrint((1, "ScsiDisk ResetScsiBus: Sending reset bus request to port driver.\n"));
4611 // Allocate Srb from nonpaged pool.
4614 context
= ExAllocatePool(NonPagedPoolMustSucceed
,
4615 sizeof(COMPLETION_CONTEXT
));
4618 // Save the device object in the context for use by the completion
4622 context
->DeviceObject
= DeviceObject
;
4623 srb
= &context
->Srb
;
4629 RtlZeroMemory(srb
, SCSI_REQUEST_BLOCK_SIZE
);
4632 // Write length to SRB.
4635 srb
->Length
= SCSI_REQUEST_BLOCK_SIZE
;
4638 // Set up SCSI bus address.
4641 srb
->PathId
= deviceExtension
->PathId
;
4642 srb
->TargetId
= deviceExtension
->TargetId
;
4643 srb
->Lun
= deviceExtension
->Lun
;
4645 srb
->Function
= SRB_FUNCTION_RESET_BUS
;
4648 // Build the asynchronous request to be sent to the port driver.
4649 // Since this routine is called from a DPC the IRP should always be
4653 irp
= IoAllocateIrp(DeviceObject
->StackSize
, FALSE
);
4655 IoSetCompletionRoutine(irp
,
4656 (PIO_COMPLETION_ROUTINE
)ScsiClassAsynchronousCompletion
,
4662 irpStack
= IoGetNextIrpStackLocation(irp
);
4664 irpStack
->MajorFunction
= IRP_MJ_SCSI
;
4666 srb
->OriginalRequest
= irp
;
4669 // Store the SRB address in next stack for port driver.
4672 irpStack
->Parameters
.Scsi
.Srb
= srb
;
4675 // Call the port driver with the IRP.
4678 IoCallDriver(deviceExtension
->PortDeviceObject
, irp
);
4682 } // end ResetScsiBus()
4687 UpdateDeviceObjects(
4688 IN PDEVICE_OBJECT PhysicalDisk
,
4694 Routine Description:
4696 This routine creates, deletes and changes device objects when
4697 the IOCTL_SET_DRIVE_LAYOUT is called. This routine also updates
4698 the drive layout information for the user. It is possible to
4699 call this routine even in the GET_LAYOUT case because RewritePartition
4704 DeviceObject - Device object for physical disk.
4705 Irp - IO Request Packet (IRP).
4713 PDEVICE_EXTENSION physicalExtension
= PhysicalDisk
->DeviceExtension
;
4714 PDRIVE_LAYOUT_INFORMATION partitionList
= Irp
->AssociatedIrp
.SystemBuffer
;
4716 ULONG partitionNumber
;
4717 ULONG partitionCount
;
4718 ULONG lastPartition
;
4719 ULONG partitionOrdinal
;
4720 PPARTITION_INFORMATION partitionEntry
;
4721 CCHAR ntNameBuffer
[MAXIMUM_FILENAME_LENGTH
];
4722 STRING ntNameString
;
4723 UNICODE_STRING ntUnicodeString
;
4724 PDEVICE_OBJECT deviceObject
;
4725 PDEVICE_EXTENSION deviceExtension
;
4726 PDISK_DATA diskData
;
4728 ULONG numberListElements
;
4731 partitionCount
= ((partitionList
->PartitionCount
+ 3) / 4) * 4;
4734 // Zero all of the partition numbers.
4737 for (partition
= 0; partition
< partitionCount
; partition
++) {
4738 partitionEntry
= &partitionList
->PartitionEntry
[partition
];
4739 partitionEntry
->PartitionNumber
= 0;
4743 // Walk through chain of partitions for this disk to determine
4744 // which existing partitions have no match.
4747 deviceExtension
= physicalExtension
;
4748 diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
4753 deviceExtension
= diskData
->NextPartition
;
4756 // Check if this is the last partition in the chain.
4759 if (!deviceExtension
) {
4764 // Get the partition device extension from disk data.
4767 diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
4770 // Check for highest partition number this far.
4773 if (diskData
->PartitionNumber
> lastPartition
) {
4774 lastPartition
= diskData
->PartitionNumber
;
4778 // Check if this partition is not currently being used.
4781 if (!deviceExtension
->PartitionLength
.QuadPart
) {
4786 // Loop through partition information to look for match.
4790 partitionOrdinal
= 0;
4792 for (partition
= 0; partition
< partitionCount
; partition
++) {
4795 // Get partition descriptor.
4798 partitionEntry
= &partitionList
->PartitionEntry
[partition
];
4801 // Check if empty, or describes extended partiton or hasn't changed.
4804 if (partitionEntry
->PartitionType
== PARTITION_ENTRY_UNUSED
||
4805 IsContainerPartition(partitionEntry
->PartitionType
)) {
4810 // Advance partition ordinal.
4816 // Check if new partition starts where this partition starts.
4819 if (partitionEntry
->StartingOffset
.QuadPart
!=
4820 deviceExtension
->StartingOffset
.QuadPart
) {
4825 // Check if partition length is the same.
4828 if (partitionEntry
->PartitionLength
.QuadPart
==
4829 deviceExtension
->PartitionLength
.QuadPart
) {
4832 "UpdateDeviceObjects: Found match for \\Harddisk%d\\Partition%d\n",
4833 physicalExtension
->DeviceNumber
,
4834 diskData
->PartitionNumber
));
4837 // Indicate match is found and set partition number
4842 partitionEntry
->PartitionNumber
= diskData
->PartitionNumber
;
4850 // A match is found.
4853 diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
4856 // If this partition is marked for update then update partition type.
4859 if (partitionEntry
->RewritePartition
) {
4860 diskData
->PartitionType
= partitionEntry
->PartitionType
;
4864 // Update partitional ordinal for calls to HAL routine
4865 // IoSetPartitionInformation.
4868 diskData
->PartitionOrdinal
= partitionOrdinal
;
4871 "UpdateDeviceObjects: Disk %d ordinal %d is partition %d\n",
4872 physicalExtension
->DeviceNumber
,
4873 diskData
->PartitionOrdinal
,
4874 diskData
->PartitionNumber
));
4879 // no match was found, indicate this partition is gone.
4883 "UpdateDeviceObjects: Deleting \\Device\\Harddisk%x\\Partition%x\n",
4884 physicalExtension
->DeviceNumber
,
4885 diskData
->PartitionNumber
));
4887 deviceExtension
->PartitionLength
.QuadPart
= (LONGLONG
) 0;
4893 // Walk through partition loop to find new partitions and set up
4894 // device extensions to describe them. In some cases new device
4895 // objects will be created.
4898 partitionOrdinal
= 0;
4901 partition
< partitionCount
;
4905 // Get partition descriptor.
4908 partitionEntry
= &partitionList
->PartitionEntry
[partition
];
4911 // Check if empty, or describes an extended partiton.
4914 if (partitionEntry
->PartitionType
== PARTITION_ENTRY_UNUSED
||
4915 IsContainerPartition(partitionEntry
->PartitionType
)) {
4920 // Keep track of position on the disk for calls to IoSetPartitionInformation.
4926 // Check if this entry should be rewritten.
4929 if (!partitionEntry
->RewritePartition
) {
4933 if (partitionEntry
->PartitionNumber
) {
4936 // Partition is an exact match with an existing partition, but is
4937 // being written anyway.
4944 // Check first if existing device object is available by
4945 // walking partition extension list.
4948 partitionNumber
= 0;
4949 deviceExtension
= physicalExtension
;
4950 diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
4955 // Get next partition device extension from disk data.
4958 deviceExtension
= diskData
->NextPartition
;
4960 if (!deviceExtension
) {
4964 diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
4967 // A device object is free if the partition length is set to zero.
4970 if (!deviceExtension
->PartitionLength
.QuadPart
) {
4971 partitionNumber
= diskData
->PartitionNumber
;
4978 // If partition number is still zero then a new device object
4982 if (partitionNumber
== 0) {
4985 partitionNumber
= lastPartition
;
4988 // Get or create partition object and set up partition parameters.
4991 sprintf(ntNameBuffer
,
4992 "\\Device\\Harddisk%lu\\Partition%lu",
4993 physicalExtension
->DeviceNumber
,
4996 RtlInitString(&ntNameString
,
4999 status
= RtlAnsiStringToUnicodeString(&ntUnicodeString
,
5003 if (!NT_SUCCESS(status
)) {
5008 "UpdateDeviceObjects: Create device object %s\n",
5012 // This is a new name. Create the device object to represent it.
5015 status
= IoCreateDevice(PhysicalDisk
->DriverObject
,
5016 DEVICE_EXTENSION_SIZE
,
5023 if (!NT_SUCCESS(status
)) {
5025 "UpdateDeviceObjects: Can't create device %s\n",
5027 RtlFreeUnicodeString(&ntUnicodeString
);
5032 // Set up device object fields.
5035 deviceObject
->Flags
|= DO_DIRECT_IO
;
5036 deviceObject
->StackSize
= PhysicalDisk
->StackSize
;
5039 // Set up device extension fields.
5042 deviceExtension
= deviceObject
->DeviceExtension
;
5045 // Copy physical disk extension to partition extension.
5048 RtlMoveMemory(deviceExtension
,
5050 sizeof(DEVICE_EXTENSION
));
5053 // Initialize the new S-List.
5056 if (deviceExtension
->SrbFlags
& SRB_FLAGS_QUEUE_ACTION_ENABLE
) {
5057 numberListElements
= 30;
5059 numberListElements
= 8;
5063 // Build the lookaside list for srb's for this partition based on
5064 // whether the adapter and disk can do tagged queueing.
5067 ScsiClassInitializeSrbLookasideList(deviceExtension
,
5068 numberListElements
);
5071 // Allocate spinlock for zoning for split-request completion.
5074 KeInitializeSpinLock(&deviceExtension
->SplitRequestSpinLock
);
5077 // Write back partition number used in creating object name.
5080 partitionEntry
->PartitionNumber
= partitionNumber
;
5083 // Clear flags initializing bit.
5086 deviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
5089 // Point back at device object.
5092 deviceExtension
->DeviceObject
= deviceObject
;
5094 RtlFreeUnicodeString(&ntUnicodeString
);
5097 // Link to end of partition chain using previous disk data.
5100 diskData
->NextPartition
= deviceExtension
;
5103 // Get new disk data and zero next partition pointer.
5106 diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
5107 diskData
->NextPartition
= NULL
;
5112 // Set pointer to disk data area that follows device extension.
5115 diskData
= (PDISK_DATA
)(deviceExtension
+ 1);
5118 "UpdateDeviceObjects: Used existing device object \\Device\\Harddisk%x\\Partition%x\n",
5119 physicalExtension
->DeviceNumber
,
5124 // Update partition information in partition device extension.
5127 diskData
->PartitionNumber
= partitionNumber
;
5128 diskData
->PartitionType
= partitionEntry
->PartitionType
;
5129 diskData
->BootIndicator
= partitionEntry
->BootIndicator
;
5130 deviceExtension
->StartingOffset
= partitionEntry
->StartingOffset
;
5131 deviceExtension
->PartitionLength
= partitionEntry
->PartitionLength
;
5132 diskData
->HiddenSectors
= partitionEntry
->HiddenSectors
;
5133 diskData
->PartitionOrdinal
= partitionOrdinal
;
5136 "UpdateDeviceObjects: Ordinal %d is partition %d\n",
5137 diskData
->PartitionOrdinal
,
5138 diskData
->PartitionNumber
));
5141 // Update partition number passed in to indicate the
5142 // device name for this partition.
5145 partitionEntry
->PartitionNumber
= partitionNumber
;
5148 } // end UpdateDeviceObjects()