PageRenderTime 73ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/storage/class/disk_new/geometry.c

https://bitbucket.org/arty/arty-newcc-reactos
C | 1470 lines | 737 code | 299 blank | 434 comment | 119 complexity | 8ed474e23ff67223c7516f617072ee70 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-3.0, CC-BY-SA-3.0, AGPL-3.0, GPL-3.0, CPL-1.0
  1. /*++
  2. Copyright (C) Microsoft Corporation, 1991 - 1999
  3. Module Name:
  4. geometry.c
  5. Abstract:
  6. SCSI disk class driver - this module contains all the code for generating
  7. disk geometries.
  8. Environment:
  9. kernel mode only
  10. Notes:
  11. Revision History:
  12. --*/
  13. #include "disk.h"
  14. #include "ntddstor.h"
  15. #if defined (_X86_)
  16. DISK_GEOMETRY_SOURCE
  17. DiskUpdateGeometry(
  18. IN PFUNCTIONAL_DEVICE_EXTENSION DeviceExtension
  19. );
  20. NTSTATUS
  21. DiskUpdateRemovableGeometry (
  22. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension
  23. );
  24. VOID
  25. DiskScanBusDetectInfo(
  26. IN PDRIVER_OBJECT DriverObject,
  27. IN HANDLE BusKey
  28. );
  29. NTSTATUS
  30. DiskSaveBusDetectInfo(
  31. IN PDRIVER_OBJECT DriverObject,
  32. IN HANDLE TargetKey,
  33. IN ULONG DiskNumber
  34. );
  35. NTSTATUS
  36. DiskSaveGeometryDetectInfo(
  37. IN PDRIVER_OBJECT DriverObject,
  38. IN HANDLE HardwareKey
  39. );
  40. NTSTATUS
  41. DiskGetPortGeometry(
  42. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  43. OUT PDISK_GEOMETRY Geometry
  44. );
  45. typedef struct _DISK_DETECT_INFO {
  46. BOOLEAN Initialized;
  47. ULONG Style;
  48. ULONG Signature;
  49. ULONG MbrCheckSum;
  50. PDEVICE_OBJECT Device;
  51. CM_INT13_DRIVE_PARAMETER DriveParameters;
  52. } DISK_DETECT_INFO, *PDISK_DETECT_INFO;
  53. //
  54. // Information about the disk geometries collected and saved into the registry
  55. // by NTDETECT.COM or the system firmware.
  56. //
  57. PDISK_DETECT_INFO DetectInfoList = NULL;
  58. ULONG DetectInfoCount = 0;
  59. ULONG DetectInfoUsedCount = 0;
  60. #ifdef ALLOC_PRAGMA
  61. #pragma alloc_text(INIT, DiskSaveDetectInfo)
  62. #pragma alloc_text(INIT, DiskScanBusDetectInfo)
  63. #pragma alloc_text(INIT, DiskSaveBusDetectInfo)
  64. #pragma alloc_text(INIT, DiskSaveGeometryDetectInfo)
  65. #pragma alloc_text(PAGE, DiskUpdateGeometry)
  66. #pragma alloc_text(PAGE, DiskUpdateRemovableGeometry)
  67. #pragma alloc_text(PAGE, DiskGetPortGeometry)
  68. #pragma alloc_text(PAGE, DiskGetDetectInfo)
  69. #pragma alloc_text(PAGE, DiskReadSignature)
  70. #endif
  71. NTSTATUS
  72. DiskSaveDetectInfo(
  73. PDRIVER_OBJECT DriverObject
  74. )
  75. /*++
  76. Routine Description:
  77. This routine saves away the firmware information about the disks which has
  78. been saved in the registry. It generates a list (DetectInfoList) which
  79. contains the disk geometries, signatures & checksums of all drives which
  80. were examined by NtDetect. This list is later used to assign geometries
  81. to disks as they are initialized.
  82. Arguments:
  83. DriverObject - the driver being initialized. This is used to get to the
  84. hardware database.
  85. Return Value:
  86. status.
  87. --*/
  88. {
  89. OBJECT_ATTRIBUTES objectAttributes;
  90. HANDLE hardwareKey;
  91. UNICODE_STRING unicodeString;
  92. HANDLE busKey;
  93. NTSTATUS status;
  94. PAGED_CODE();
  95. InitializeObjectAttributes(
  96. &objectAttributes,
  97. DriverObject->HardwareDatabase,
  98. OBJ_CASE_INSENSITIVE,
  99. NULL,
  100. NULL);
  101. //
  102. // Create the hardware base key.
  103. //
  104. status = ZwOpenKey(&hardwareKey, KEY_READ, &objectAttributes);
  105. if(!NT_SUCCESS(status)) {
  106. DebugPrint((1, "DiskSaveDetectInfo: Cannot open hardware data. "
  107. "Name: %wZ\n",
  108. DriverObject->HardwareDatabase));
  109. return status;
  110. }
  111. status = DiskSaveGeometryDetectInfo(DriverObject, hardwareKey);
  112. if(!NT_SUCCESS(status)) {
  113. DebugPrint((1, "DiskSaveDetectInfo: Can't query configuration data "
  114. "(%#08lx)\n",
  115. status));
  116. ZwClose(hardwareKey);
  117. return status;
  118. }
  119. //
  120. // Open EISA bus key.
  121. //
  122. RtlInitUnicodeString(&unicodeString, L"EisaAdapter");
  123. InitializeObjectAttributes(&objectAttributes,
  124. &unicodeString,
  125. OBJ_CASE_INSENSITIVE,
  126. hardwareKey,
  127. NULL);
  128. status = ZwOpenKey(&busKey,
  129. KEY_READ,
  130. &objectAttributes);
  131. if(NT_SUCCESS(status)) {
  132. DebugPrint((1, "DiskSaveDetectInfo: Opened EisaAdapter key\n"));
  133. DiskScanBusDetectInfo(DriverObject, busKey);
  134. ZwClose(busKey);
  135. }
  136. //
  137. // Open MultiFunction bus key.
  138. //
  139. RtlInitUnicodeString(&unicodeString, L"MultifunctionAdapter");
  140. InitializeObjectAttributes(&objectAttributes,
  141. &unicodeString,
  142. OBJ_CASE_INSENSITIVE,
  143. hardwareKey,
  144. NULL);
  145. status = ZwOpenKey(&busKey,
  146. KEY_READ,
  147. &objectAttributes);
  148. if(NT_SUCCESS(status)) {
  149. DebugPrint((1, "DiskSaveDetectInfo: Opened MultifunctionAdapter key\n"));
  150. DiskScanBusDetectInfo(DriverObject, busKey);
  151. ZwClose(busKey);
  152. }
  153. ZwClose(hardwareKey);
  154. return STATUS_SUCCESS;
  155. }
  156. VOID
  157. DiskCleanupDetectInfo(
  158. IN PDRIVER_OBJECT DriverObject
  159. )
  160. /*++
  161. Routine Description:
  162. This routine will cleanup the data structure built by DiskSaveDetectInfo.
  163. Arguments:
  164. DriverObject - a pointer to the kernel object for this driver.
  165. Return Value:
  166. none
  167. --*/
  168. {
  169. if(DetectInfoList != NULL) {
  170. ExFreePool(DetectInfoList);
  171. DetectInfoList = NULL;
  172. }
  173. return;
  174. }
  175. NTSTATUS
  176. DiskSaveGeometryDetectInfo(
  177. IN PDRIVER_OBJECT DriverObject,
  178. IN HANDLE HardwareKey
  179. )
  180. {
  181. UNICODE_STRING unicodeString;
  182. PKEY_VALUE_FULL_INFORMATION keyData;
  183. ULONG length;
  184. PCM_FULL_RESOURCE_DESCRIPTOR fullDescriptor;
  185. PCM_PARTIAL_RESOURCE_DESCRIPTOR partialDescriptor;
  186. PCM_INT13_DRIVE_PARAMETER driveParameters;
  187. ULONG numberOfDrives;
  188. ULONG i;
  189. NTSTATUS status;
  190. PAGED_CODE();
  191. //
  192. // Get disk BIOS geometry information.
  193. //
  194. RtlInitUnicodeString(&unicodeString, L"Configuration Data");
  195. keyData = ExAllocatePoolWithTag(PagedPool,
  196. VALUE_BUFFER_SIZE,
  197. DISK_TAG_UPDATE_GEOM);
  198. if(keyData == NULL) {
  199. DebugPrint((1, "DiskSaveGeometryDetectInfo: Can't allocate config "
  200. "data buffer\n"));
  201. return STATUS_INSUFFICIENT_RESOURCES;
  202. }
  203. status = ZwQueryValueKey(HardwareKey,
  204. &unicodeString,
  205. KeyValueFullInformation,
  206. keyData,
  207. VALUE_BUFFER_SIZE,
  208. &length);
  209. if(!NT_SUCCESS(status)) {
  210. DebugPrint((1, "DiskSaveGeometryDetectInfo: Can't query configuration "
  211. "data (%#08lx)\n",
  212. status));
  213. ExFreePool(keyData);
  214. return status;
  215. }
  216. //
  217. // Extract the resource list out of the key data.
  218. //
  219. fullDescriptor = (PCM_FULL_RESOURCE_DESCRIPTOR)
  220. (((PUCHAR) keyData) + keyData->DataOffset);
  221. partialDescriptor =
  222. fullDescriptor->PartialResourceList.PartialDescriptors;
  223. length = partialDescriptor->u.DeviceSpecificData.DataSize;
  224. if((keyData->DataLength < sizeof(CM_FULL_RESOURCE_DESCRIPTOR)) ||
  225. (fullDescriptor->PartialResourceList.Count == 0) ||
  226. (partialDescriptor->Type != CmResourceTypeDeviceSpecific) ||
  227. (length < sizeof(ULONG))) {
  228. DebugPrint((1, "DiskSaveGeometryDetectInfo: BIOS header data too small "
  229. "or invalid\n"));
  230. ExFreePool(keyData);
  231. return STATUS_INVALID_PARAMETER;
  232. }
  233. //
  234. // Point to the BIOS data. THe BIOS data is located after the first
  235. // partial Resource list which should be device specific data.
  236. //
  237. {
  238. PUCHAR buffer = (PUCHAR) keyData;
  239. buffer += keyData->DataOffset;
  240. buffer += sizeof(CM_FULL_RESOURCE_DESCRIPTOR);
  241. driveParameters = (PCM_INT13_DRIVE_PARAMETER) buffer;
  242. }
  243. numberOfDrives = length / sizeof(CM_INT13_DRIVE_PARAMETER);
  244. //
  245. // Allocate our detect info list now that we know how many entries there
  246. // are going to be. No other routine allocates detect info and this is
  247. // done out of DriverEntry so we don't need to synchronize it's creation.
  248. //
  249. length = sizeof(DISK_DETECT_INFO) * numberOfDrives;
  250. DetectInfoList = ExAllocatePoolWithTag(PagedPool,
  251. length,
  252. DISK_TAG_UPDATE_GEOM);
  253. if(DetectInfoList == NULL) {
  254. DebugPrint((1, "DiskSaveGeometryDetectInfo: Couldn't allocate %x bytes "
  255. "for DetectInfoList\n",
  256. length));
  257. ExFreePool(keyData);
  258. return STATUS_INSUFFICIENT_RESOURCES;
  259. }
  260. DetectInfoCount = numberOfDrives;
  261. RtlZeroMemory(DetectInfoList, length);
  262. //
  263. // Copy the information out of the key data and into the list we've
  264. // allocated.
  265. //
  266. for(i = 0; i < numberOfDrives; i++) {
  267. DetectInfoList[i].DriveParameters = driveParameters[i];
  268. }
  269. ExFreePool(keyData);
  270. return STATUS_SUCCESS;
  271. }
  272. VOID
  273. DiskScanBusDetectInfo(
  274. IN PDRIVER_OBJECT DriverObject,
  275. IN HANDLE BusKey
  276. )
  277. /*++
  278. Routine Description:
  279. The routine queries the registry to determine which disks are visible to
  280. the BIOS. If a disk is visable to the BIOS then the geometry information
  281. is updated with the disk's signature and MBR checksum.
  282. Arguments:
  283. DriverObject - the object for this driver.
  284. BusKey - handle to the bus key to be enumerated.
  285. Return Value:
  286. status
  287. --*/
  288. {
  289. ULONG busNumber;
  290. NTSTATUS status;
  291. for(busNumber = 0; ; busNumber++) {
  292. WCHAR buffer[32];
  293. UNICODE_STRING unicodeString;
  294. OBJECT_ATTRIBUTES objectAttributes;
  295. HANDLE spareKey;
  296. HANDLE adapterKey;
  297. ULONG adapterNumber;
  298. DebugPrint((1, "DiskScanBusDetectInfo: Scanning bus %d\n", busNumber));
  299. //
  300. // Open controller name key.
  301. //
  302. swprintf(buffer, L"%d", busNumber);
  303. RtlInitUnicodeString(&unicodeString, buffer);
  304. InitializeObjectAttributes(&objectAttributes,
  305. &unicodeString,
  306. OBJ_CASE_INSENSITIVE,
  307. BusKey,
  308. NULL);
  309. status = ZwOpenKey(&spareKey, KEY_READ, &objectAttributes);
  310. if(!NT_SUCCESS(status)) {
  311. DebugPrint((1, "DiskScanBusDetectInfo: Error %#08lx opening bus "
  312. "key %#x\n",
  313. status, busNumber));
  314. break;
  315. }
  316. //
  317. // Open up a controller ordinal key.
  318. //
  319. RtlInitUnicodeString(&unicodeString, L"DiskController");
  320. InitializeObjectAttributes(&objectAttributes,
  321. &unicodeString,
  322. OBJ_CASE_INSENSITIVE,
  323. spareKey,
  324. NULL);
  325. status = ZwOpenKey(&adapterKey, KEY_READ, &objectAttributes);
  326. ZwClose(spareKey);
  327. if(!NT_SUCCESS(status)) {
  328. DebugPrint((1, "DiskScanBusDetectInfo: Error %#08lx opening "
  329. "DiskController key\n",
  330. status));
  331. continue;
  332. }
  333. for(adapterNumber = 0; ; adapterNumber++) {
  334. HANDLE diskKey;
  335. ULONG diskNumber;
  336. //
  337. // Open disk key.
  338. //
  339. DebugPrint((1, "DiskScanBusDetectInfo: Scanning disk key "
  340. "%d\\DiskController\\%d\\DiskPeripheral\n",
  341. busNumber, adapterNumber));
  342. swprintf(buffer, L"%d\\DiskPeripheral", adapterNumber);
  343. RtlInitUnicodeString(&unicodeString, buffer);
  344. InitializeObjectAttributes(&objectAttributes,
  345. &unicodeString,
  346. OBJ_CASE_INSENSITIVE,
  347. adapterKey,
  348. NULL);
  349. status = ZwOpenKey(&diskKey, KEY_READ, &objectAttributes);
  350. if(!NT_SUCCESS(status)) {
  351. DebugPrint((1, "DiskScanBusDetectInfo: Error %#08lx opening "
  352. "disk key\n",
  353. status));
  354. break;
  355. }
  356. for(diskNumber = 0; ; diskNumber++) {
  357. HANDLE targetKey;
  358. DebugPrint((1, "DiskScanBusDetectInfo: Scanning target key "
  359. "%d\\DiskController\\%d\\DiskPeripheral\\%d\n",
  360. busNumber, adapterNumber, diskNumber));
  361. swprintf(buffer, L"%d", diskNumber);
  362. RtlInitUnicodeString(&unicodeString, buffer);
  363. InitializeObjectAttributes(&objectAttributes,
  364. &unicodeString,
  365. OBJ_CASE_INSENSITIVE,
  366. diskKey,
  367. NULL);
  368. status = ZwOpenKey(&targetKey, KEY_READ, &objectAttributes);
  369. if(!NT_SUCCESS(status)) {
  370. DebugPrint((1, "DiskScanBusDetectInfo: Error %#08lx "
  371. "opening target key\n",
  372. status));
  373. break;
  374. }
  375. status = DiskSaveBusDetectInfo(DriverObject,
  376. targetKey,
  377. diskNumber);
  378. ZwClose(targetKey);
  379. }
  380. ZwClose(diskKey);
  381. }
  382. ZwClose(adapterKey);
  383. }
  384. return;
  385. }
  386. NTSTATUS
  387. DiskSaveBusDetectInfo(
  388. IN PDRIVER_OBJECT DriverObject,
  389. IN HANDLE TargetKey,
  390. IN ULONG DiskNumber
  391. )
  392. /*++
  393. Routine Description:
  394. This routine will transfer the firmware/ntdetect reported information
  395. in the specified target key into the appropriate entry in the
  396. DetectInfoList.
  397. Arguments:
  398. DriverObject - the object for this driver.
  399. TargetKey - the key for the disk being saved.
  400. DiskNumber - the ordinal of the entry in the DiskPeripheral tree for this
  401. entry
  402. Return Value:
  403. status
  404. --*/
  405. {
  406. PDISK_DETECT_INFO diskInfo;
  407. UNICODE_STRING unicodeString;
  408. PKEY_VALUE_FULL_INFORMATION keyData;
  409. ULONG length;
  410. NTSTATUS status;
  411. PAGED_CODE();
  412. diskInfo = &(DetectInfoList[DiskNumber]);
  413. if(diskInfo->Initialized) {
  414. ASSERT(FALSE);
  415. DebugPrint((1, "DiskSaveBusDetectInfo: disk entry %#x already has a "
  416. "signature of %#08lx and mbr checksum of %#08lx\n",
  417. DiskNumber,
  418. diskInfo->Signature,
  419. diskInfo->MbrCheckSum));
  420. return STATUS_UNSUCCESSFUL;
  421. }
  422. RtlInitUnicodeString(&unicodeString, L"Identifier");
  423. keyData = ExAllocatePoolWithTag(PagedPool,
  424. VALUE_BUFFER_SIZE,
  425. DISK_TAG_UPDATE_GEOM);
  426. if(keyData == NULL) {
  427. DebugPrint((1, "DiskSaveBusDetectInfo: Couldn't allocate space for "
  428. "registry data\n"));
  429. return STATUS_INSUFFICIENT_RESOURCES;
  430. }
  431. //
  432. // Get disk peripheral identifier.
  433. //
  434. status = ZwQueryValueKey(TargetKey,
  435. &unicodeString,
  436. KeyValueFullInformation,
  437. keyData,
  438. VALUE_BUFFER_SIZE,
  439. &length);
  440. if(!NT_SUCCESS(status)) {
  441. DebugPrint((1, "DiskSaveBusDetectInfo: Error %#08lx getting "
  442. "Identifier\n",
  443. status));
  444. ExFreePool(keyData);
  445. return status;
  446. } else if (keyData->DataLength < 9*sizeof(WCHAR)) {
  447. //
  448. // the data is too short to use (we subtract 9 chars in normal path)
  449. //
  450. DebugPrint((1, "DiskSaveBusDetectInfo: Saved data was invalid, "
  451. "not enough data in registry!\n"));
  452. ExFreePool(keyData);
  453. return STATUS_UNSUCCESSFUL;
  454. } else {
  455. UNICODE_STRING identifier;
  456. ULONG value;
  457. //
  458. // Complete unicode string.
  459. //
  460. identifier.Buffer = (PWSTR) ((PUCHAR)keyData + keyData->DataOffset);
  461. identifier.Length = (USHORT) keyData->DataLength;
  462. identifier.MaximumLength = (USHORT) keyData->DataLength;
  463. //
  464. // Get the first value out of the identifier - this will be the MBR
  465. // checksum.
  466. //
  467. status = RtlUnicodeStringToInteger(&identifier, 16, &value);
  468. if(!NT_SUCCESS(status)) {
  469. DebugPrint((1, "DiskSaveBusDetectInfo: Error %#08lx converting "
  470. "identifier %wZ into MBR xsum\n",
  471. status,
  472. &identifier));
  473. ExFreePool(keyData);
  474. return status;
  475. }
  476. diskInfo->MbrCheckSum = value;
  477. //
  478. // Shift the string over to get the disk signature
  479. //
  480. identifier.Buffer += 9;
  481. identifier.Length -= 9 * sizeof(WCHAR);
  482. identifier.MaximumLength -= 9 * sizeof(WCHAR);
  483. status = RtlUnicodeStringToInteger(&identifier, 16, &value);
  484. if(!NT_SUCCESS(status)) {
  485. DebugPrint((1, "DiskSaveBusDetectInfo: Error %#08lx converting "
  486. "identifier %wZ into disk signature\n",
  487. status,
  488. &identifier));
  489. ExFreePool(keyData);
  490. value = 0;
  491. }
  492. diskInfo->Signature = value;
  493. }
  494. //
  495. // Here is where we would save away the extended int13 data.
  496. //
  497. //
  498. // Mark this entry as initialized so we can make sure not to do it again.
  499. //
  500. diskInfo->Initialized = TRUE;
  501. return STATUS_SUCCESS;
  502. }
  503. DISK_GEOMETRY_SOURCE
  504. DiskUpdateGeometry(
  505. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension
  506. )
  507. /*++
  508. Routine Description:
  509. This routine checks the DetectInfoList saved away during disk driver init
  510. to see if any geometry information was reported for this drive. If the
  511. geometry data exists (determined by matching non-zero signatures or
  512. non-zero MBR checksums) then it will be saved in the RealGeometry member
  513. of the disk data block.
  514. ClassReadDriveCapacity MUST be called after calling this routine to update
  515. the cylinder count based on the size of the disk and the presence of any
  516. disk management software.
  517. Arguments:
  518. DeviceExtension - Supplies a pointer to the device information for disk.
  519. Return Value:
  520. Inidicates whether the "RealGeometry" in the data block is now valid.
  521. --*/
  522. {
  523. PDISK_DATA diskData = FdoExtension->CommonExtension.DriverData;
  524. ULONG i;
  525. PDISK_DETECT_INFO diskInfo;
  526. BOOLEAN found = FALSE;
  527. NTSTATUS status;
  528. PAGED_CODE();
  529. ASSERT(FdoExtension->CommonExtension.IsFdo);
  530. ASSERT((FdoExtension->DeviceObject->Characteristics & FILE_REMOVABLE_MEDIA) == 0);
  531. //
  532. // If we've already set a non-default geometry for this drive then there's
  533. // no need to try and update again.
  534. //
  535. if(diskData->GeometrySource != DiskGeometryUnknown) {
  536. return diskData->GeometrySource;
  537. }
  538. //
  539. // Scan through the saved detect info to see if we can find a match
  540. // for this device.
  541. //
  542. for(i = 0; i < DetectInfoCount; i++) {
  543. ASSERT(DetectInfoList != NULL);
  544. diskInfo = &(DetectInfoList[i]);
  545. if((diskData->Mbr.Signature != 0) &&
  546. (diskData->Mbr.Signature == diskInfo->Signature)) {
  547. DebugPrint((1, "DiskUpdateGeometry: found match for signature "
  548. "%#08lx\n",
  549. diskData->Mbr.Signature));
  550. found = TRUE;
  551. break;
  552. } else if((diskData->Mbr.Signature == 0) &&
  553. (diskData->Mbr.MbrCheckSum != 0) &&
  554. (diskData->Mbr.MbrCheckSum == diskInfo->MbrCheckSum)) {
  555. DebugPrint((1, "DiskUpdateGeometry: found match for xsum %#08lx\n",
  556. diskData->Mbr.MbrCheckSum));
  557. found = TRUE;
  558. break;
  559. }
  560. }
  561. if(found) {
  562. ULONG cylinders;
  563. ULONG sectorsPerTrack;
  564. ULONG tracksPerCylinder;
  565. ULONG sectors;
  566. ULONG length;
  567. //
  568. // Point to the array of drive parameters.
  569. //
  570. cylinders = diskInfo->DriveParameters.MaxCylinders + 1;
  571. sectorsPerTrack = diskInfo->DriveParameters.SectorsPerTrack;
  572. tracksPerCylinder = diskInfo->DriveParameters.MaxHeads + 1;
  573. //
  574. // Since the BIOS may not report the full drive, recalculate the drive
  575. // size based on the volume size and the BIOS values for tracks per
  576. // cylinder and sectors per track..
  577. //
  578. length = tracksPerCylinder * sectorsPerTrack;
  579. if (length == 0) {
  580. //
  581. // The BIOS information is bogus.
  582. //
  583. DebugPrint((1, "DiskUpdateGeometry: H (%d) or S(%d) is zero\n",
  584. tracksPerCylinder, sectorsPerTrack));
  585. return FALSE;
  586. }
  587. //
  588. // since we are copying the structure RealGeometry here, we should
  589. // really initialize all the fields, especially since a zero'd
  590. // BytesPerSector field would cause a trap in xHalReadPartitionTable()
  591. //
  592. diskData->RealGeometry = FdoExtension->DiskGeometry;
  593. //
  594. // Save the geometry information away in the disk data block and
  595. // set the bit indicating that we found a valid one.
  596. //
  597. diskData->RealGeometry.SectorsPerTrack = sectorsPerTrack;
  598. diskData->RealGeometry.TracksPerCylinder = tracksPerCylinder;
  599. diskData->RealGeometry.Cylinders.QuadPart = (LONGLONG)cylinders;
  600. DebugPrint((1, "DiskUpdateGeometry: BIOS spt %#x, #heads %#x, "
  601. "#cylinders %#x\n",
  602. sectorsPerTrack, tracksPerCylinder, cylinders));
  603. diskData->GeometrySource = DiskGeometryFromBios;
  604. diskInfo->Device = FdoExtension->DeviceObject;
  605. } else {
  606. DebugPrint((1, "DiskUpdateGeometry: no match found for signature %#08lx\n", diskData->Mbr.Signature));
  607. }
  608. if(diskData->GeometrySource == DiskGeometryUnknown) {
  609. //
  610. // We couldn't find a geometry from the BIOS. Check with the port
  611. // driver and see if it can provide one.
  612. //
  613. status = DiskGetPortGeometry(FdoExtension, &(diskData->RealGeometry));
  614. if(NT_SUCCESS(status)) {
  615. //
  616. // Check the geometry to make sure it's valid.
  617. //
  618. if((diskData->RealGeometry.TracksPerCylinder *
  619. diskData->RealGeometry.SectorsPerTrack) != 0) {
  620. diskData->GeometrySource = DiskGeometryFromPort;
  621. DebugPrint((1, "DiskUpdateGeometry: using Port geometry for disk %#p\n", FdoExtension));
  622. if (diskData->RealGeometry.BytesPerSector == 0) {
  623. DebugPrint((0, "DiskDriverReinit: Port driver failed to "
  624. "set BytesPerSector in the RealGeometry\n"));
  625. diskData->RealGeometry.BytesPerSector =
  626. FdoExtension->DiskGeometry.BytesPerSector;
  627. if (diskData->RealGeometry.BytesPerSector == 0) {
  628. ASSERT(!"BytesPerSector is still zero!");
  629. }
  630. }
  631. }
  632. }
  633. }
  634. //
  635. // If we came up with a "real" geometry for this drive then set it in the
  636. // device extension.
  637. //
  638. if(diskData->GeometrySource != DiskGeometryUnknown) {
  639. FdoExtension->DiskGeometry = diskData->RealGeometry;
  640. //
  641. // Increment the count of used geometry entries.
  642. //
  643. InterlockedIncrement(&DetectInfoUsedCount);
  644. }
  645. return diskData->GeometrySource;
  646. }
  647. NTSTATUS
  648. DiskUpdateRemovableGeometry (
  649. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension
  650. )
  651. /*++
  652. Routine Description:
  653. This routine updates the geometry of the disk. It will query the port
  654. driver to see if it can provide any geometry info. If not it will use
  655. the current head & sector count.
  656. Based on these values & the capacity of the drive as reported by
  657. ClassReadDriveCapacity it will determine a new cylinder count for the
  658. device.
  659. Arguments:
  660. Fdo - Supplies the functional device object whos size needs to be updated.
  661. Return Value:
  662. Returns the status of the opertion.
  663. --*/
  664. {
  665. PCOMMON_DEVICE_EXTENSION commonExtension = &(FdoExtension->CommonExtension);
  666. PDISK_DATA diskData = commonExtension->DriverData;
  667. PDISK_GEOMETRY geometry = &(diskData->RealGeometry);
  668. NTSTATUS status;
  669. PAGED_CODE();
  670. ASSERT_FDO(commonExtension->DeviceObject);
  671. if (FdoExtension->DeviceDescriptor) {
  672. ASSERT(FdoExtension->DeviceDescriptor->RemovableMedia);
  673. }
  674. ASSERT(TEST_FLAG(FdoExtension->DeviceObject->Characteristics,
  675. FILE_REMOVABLE_MEDIA));
  676. //
  677. // Attempt to determine the disk geometry. First we'll check with the
  678. // port driver to see what it suggests for a value.
  679. //
  680. status = DiskGetPortGeometry(FdoExtension, geometry);
  681. if(NT_SUCCESS(status) &&
  682. ((geometry->TracksPerCylinder * geometry->SectorsPerTrack) != 0)) {
  683. FdoExtension->DiskGeometry = (*geometry);
  684. }
  685. return status;
  686. }
  687. NTSTATUS
  688. DiskGetPortGeometry(
  689. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  690. OUT PDISK_GEOMETRY Geometry
  691. )
  692. /*++
  693. Routine Description:
  694. This routine will query the port driver for disk geometry. Some port
  695. drivers (in particular IDEPORT) may be able to provide geometry for the
  696. device.
  697. Arguments:
  698. FdoExtension - the device object for the disk.
  699. Geometry - a structure to save the geometry information into (if any is
  700. available)
  701. Return Value:
  702. STATUS_SUCCESS if geometry information can be provided or
  703. error status indicating why it can't.
  704. --*/
  705. {
  706. PCOMMON_DEVICE_EXTENSION commonExtension = &(FdoExtension->CommonExtension);
  707. PIRP irp;
  708. PIO_STACK_LOCATION irpStack;
  709. KEVENT event;
  710. NTSTATUS status;
  711. PAGED_CODE();
  712. //
  713. // Build an irp to send IOCTL_DISK_GET_DRIVE_GEOMETRY to the lower driver.
  714. //
  715. irp = IoAllocateIrp(commonExtension->LowerDeviceObject->StackSize, FALSE);
  716. if(irp == NULL) {
  717. return STATUS_INSUFFICIENT_RESOURCES;
  718. }
  719. irpStack = IoGetNextIrpStackLocation(irp);
  720. irpStack->MajorFunction = IRP_MJ_DEVICE_CONTROL;
  721. irpStack->Parameters.DeviceIoControl.IoControlCode =
  722. IOCTL_DISK_GET_DRIVE_GEOMETRY;
  723. irpStack->Parameters.DeviceIoControl.OutputBufferLength =
  724. sizeof(DISK_GEOMETRY);
  725. irp->AssociatedIrp.SystemBuffer = Geometry;
  726. KeInitializeEvent(&event, SynchronizationEvent, FALSE);
  727. IoSetCompletionRoutine(irp,
  728. (PIO_COMPLETION_ROUTINE)ClassSignalCompletion,
  729. &event,
  730. TRUE,
  731. TRUE,
  732. TRUE);
  733. status = IoCallDriver(commonExtension->LowerDeviceObject, irp);
  734. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  735. ASSERT((status == STATUS_PENDING) || (status == irp->IoStatus.Status));
  736. status = irp->IoStatus.Status;
  737. IoFreeIrp(irp);
  738. return status;
  739. }
  740. NTSTATUS
  741. DiskReadDriveCapacity(
  742. IN PDEVICE_OBJECT Fdo
  743. )
  744. /*++
  745. Routine Description:
  746. This routine is used by disk.sys as a wrapper for the classpnp API
  747. ClassReadDriveCapacity. It will perform some additional operations to
  748. attempt to determine drive geometry before it calls the classpnp version
  749. of the routine.
  750. For fixed disks this involves calling DiskUpdateGeometry which will check
  751. various sources (the BIOS, the port driver) for geometry information.
  752. Arguments:
  753. Fdo - a pointer to the device object to be checked.
  754. Return Value:
  755. status of ClassReadDriveCapacity.
  756. --*/
  757. {
  758. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = Fdo->DeviceExtension;
  759. PDISK_DATA diskData = fdoExtension->CommonExtension.DriverData;
  760. DISK_GEOMETRY_SOURCE diskGeometrySource = DiskGeometryUnknown;
  761. NTSTATUS status;
  762. ASSERT_FDO(Fdo);
  763. if (TEST_FLAG(Fdo->Characteristics, FILE_REMOVABLE_MEDIA)) {
  764. DiskUpdateRemovableGeometry(fdoExtension);
  765. } else {
  766. diskGeometrySource = DiskUpdateGeometry(fdoExtension);
  767. }
  768. status = ClassReadDriveCapacity(Fdo);
  769. return status;
  770. }
  771. VOID
  772. DiskDriverReinitialization(
  773. IN PDRIVER_OBJECT DriverObject,
  774. IN PVOID Nothing,
  775. IN ULONG Count
  776. )
  777. /*++
  778. Routine Description:
  779. This routine will scan through the current list of disks and attempt to
  780. match them to any remaining geometry information. This will only be done
  781. on the first call to the routine.
  782. Note: This routine assumes that the system will not be adding or removing
  783. devices during this phase of the init process. This is very likely
  784. a bad assumption but it greatly simplifies the code.
  785. Arguments:
  786. DriverObject - a pointer to the object for the disk driver.
  787. Nothing - unused
  788. Count - an indication of how many times this routine has been called.
  789. Return Value:
  790. none
  791. --*/
  792. {
  793. PDEVICE_OBJECT deviceObject;
  794. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension;
  795. PDISK_DATA diskData;
  796. ULONG unmatchedDiskCount;
  797. PDEVICE_OBJECT unmatchedDisk = NULL;
  798. ULONG i;
  799. PDISK_DETECT_INFO diskInfo = NULL;
  800. if(Count != 1) {
  801. DebugPrint((1, "DiskDriverReinitialization: ignoring call %d\n",
  802. Count));
  803. return;
  804. }
  805. //
  806. // Check to see how many entries in the detect info list have been matched.
  807. // If there's only one remaining we'll see if we can find a disk to go with
  808. // it.
  809. //
  810. if(DetectInfoCount == 0) {
  811. DebugPrint((1, "DiskDriverReinitialization: no detect info saved\n"));
  812. return;
  813. }
  814. if((DetectInfoCount - DetectInfoUsedCount) != 1) {
  815. DebugPrint((1, "DiskDriverReinitialization: %d of %d geometry entries "
  816. "used - will not attempt match\n"));
  817. return;
  818. }
  819. //
  820. // Scan through the list of disks and see if any of them are missing
  821. // geometry information. If there is only one such disk we'll try to
  822. // match it to the unmatched geometry.
  823. //
  824. //
  825. // ISSUE-2000/5/24-henrygab - figure out if there's a way to keep
  826. // removals from happening while doing this.
  827. //
  828. for(deviceObject = DriverObject->DeviceObject, unmatchedDiskCount = 0;
  829. deviceObject != NULL;
  830. deviceObject = deviceObject->NextDevice) {
  831. //
  832. // Make sure this is a disk and not a partition.
  833. //
  834. fdoExtension = deviceObject->DeviceExtension;
  835. if(fdoExtension->CommonExtension.IsFdo == FALSE) {
  836. DebugPrint((1, "DiskDriverReinit: DO %#p is not an FDO\n",
  837. deviceObject));
  838. continue;
  839. }
  840. //
  841. // If the geometry for this one is already known then skip it.
  842. //
  843. diskData = fdoExtension->CommonExtension.DriverData;
  844. if(diskData->GeometrySource != DiskGeometryUnknown) {
  845. DebugPrint((1, "DiskDriverReinit: FDO %#p has a geometry\n",
  846. deviceObject));
  847. continue;
  848. }
  849. DebugPrint((1, "DiskDriverReinit: FDO %#p has no geometry\n",
  850. deviceObject));
  851. //
  852. // Mark this one as using the default. It's past the time when disk
  853. // might blunder across the geometry info. If we set the geometry
  854. // from the bios we'll reset this field down below.
  855. //
  856. diskData->GeometrySource = DiskGeometryFromDefault;
  857. //
  858. // As long as we've only got one unmatched disk we're fine.
  859. //
  860. unmatchedDiskCount++;
  861. if(unmatchedDiskCount > 1) {
  862. ASSERT(unmatchedDisk != NULL);
  863. DebugPrint((1, "DiskDriverReinit: FDO %#p also has no geometry\n",
  864. unmatchedDisk));
  865. unmatchedDisk = NULL;
  866. break;
  867. }
  868. unmatchedDisk = deviceObject;
  869. }
  870. //
  871. // If there's more or less than one ungeometried disk then we can't do
  872. // anything about the geometry.
  873. //
  874. if(unmatchedDiskCount != 1) {
  875. DebugPrint((1, "DiskDriverReinit: Unable to match geometry\n"));
  876. return;
  877. }
  878. fdoExtension = unmatchedDisk->DeviceExtension;
  879. diskData = fdoExtension->CommonExtension.DriverData;
  880. DebugPrint((1, "DiskDriverReinit: Found possible match\n"));
  881. //
  882. // Find the geometry which wasn't assigned.
  883. //
  884. for(i = 0; i < DetectInfoCount; i++) {
  885. if(DetectInfoList[i].Device == NULL) {
  886. diskInfo = &(DetectInfoList[i]);
  887. break;
  888. }
  889. }
  890. ASSERT(diskInfo != NULL);
  891. {
  892. //
  893. // Save the geometry information away in the disk data block and
  894. // set the bit indicating that we found a valid one.
  895. //
  896. ULONG cylinders;
  897. ULONG sectorsPerTrack;
  898. ULONG tracksPerCylinder;
  899. ULONG sectors;
  900. ULONG length;
  901. //
  902. // Point to the array of drive parameters.
  903. //
  904. cylinders = diskInfo->DriveParameters.MaxCylinders + 1;
  905. sectorsPerTrack = diskInfo->DriveParameters.SectorsPerTrack;
  906. tracksPerCylinder = diskInfo->DriveParameters.MaxHeads + 1;
  907. //
  908. // Since the BIOS may not report the full drive, recalculate the drive
  909. // size based on the volume size and the BIOS values for tracks per
  910. // cylinder and sectors per track..
  911. //
  912. length = tracksPerCylinder * sectorsPerTrack;
  913. if (length == 0) {
  914. //
  915. // The BIOS information is bogus.
  916. //
  917. DebugPrint((1, "DiskDriverReinit: H (%d) or S(%d) is zero\n",
  918. tracksPerCylinder, sectorsPerTrack));
  919. return;
  920. }
  921. //
  922. // since we are copying the structure RealGeometry here, we should
  923. // really initialize all the fields, especially since a zero'd
  924. // BytesPerSector field would cause a trap in xHalReadPartitionTable()
  925. //
  926. diskData->RealGeometry = fdoExtension->DiskGeometry;
  927. //
  928. // Save the geometry information away in the disk data block and
  929. // set the bit indicating that we found a valid one.
  930. //
  931. diskData->RealGeometry.SectorsPerTrack = sectorsPerTrack;
  932. diskData->RealGeometry.TracksPerCylinder = tracksPerCylinder;
  933. diskData->RealGeometry.Cylinders.QuadPart = (LONGLONG)cylinders;
  934. DebugPrint((1, "DiskDriverReinit: BIOS spt %#x, #heads %#x, "
  935. "#cylinders %#x\n",
  936. sectorsPerTrack, tracksPerCylinder, cylinders));
  937. diskData->GeometrySource = DiskGeometryGuessedFromBios;
  938. diskInfo->Device = unmatchedDisk;
  939. //
  940. // Now copy the geometry over to the fdo extension and call
  941. // classpnp to redetermine the disk size and cylinder count.
  942. //
  943. fdoExtension->DiskGeometry = diskData->RealGeometry;
  944. //
  945. // BUGBUG - why not call DiskReadDriveCapacity()?
  946. //
  947. ClassReadDriveCapacity(unmatchedDisk);
  948. if (diskData->RealGeometry.BytesPerSector == 0) {
  949. //
  950. // if the BytesPerSector field is set to zero for a disk
  951. // listed in the bios, then the system will bugcheck in
  952. // xHalReadPartitionTable(). assert here since it is
  953. // easier to determine what is happening this way.
  954. //
  955. ASSERT(!"RealGeometry not set to non-zero bps\n");
  956. }
  957. }
  958. return;
  959. }
  960. NTSTATUS
  961. DiskGetDetectInfo(
  962. IN PFUNCTIONAL_DEVICE_EXTENSION FdoExtension,
  963. OUT PDISK_DETECTION_INFO DetectInfo
  964. )
  965. /*++
  966. Routine Description:
  967. Get the Int13 information from the BIOS DetectInfoList.
  968. Arguments:
  969. FdoExtension - Supplies a pointer to the FDO extension that we want to
  970. obtain the detect information for.
  971. DetectInfo - A buffer where the detect information will be copied to.
  972. Return Value:
  973. NTSTATUS code.
  974. --*/
  975. {
  976. ULONG i;
  977. BOOLEAN found;
  978. PDISK_DETECT_INFO diskInfo = NULL;
  979. PDISK_DATA diskData = FdoExtension->CommonExtension.DriverData;
  980. PAGED_CODE ();
  981. ASSERT(FdoExtension->CommonExtension.IsFdo);
  982. //
  983. // Fail for non-fixed drives.
  984. //
  985. if (TEST_FLAG (FdoExtension->DeviceObject->Characteristics, FILE_REMOVABLE_MEDIA)) {
  986. return STATUS_NOT_SUPPORTED;
  987. }
  988. //
  989. // There is no GPT detection info, so fail this.
  990. //
  991. if (diskData->PartitionStyle == PARTITION_STYLE_GPT) {
  992. return STATUS_NOT_SUPPORTED;
  993. }
  994. for(i = 0; i < DetectInfoCount; i++) {
  995. ASSERT(DetectInfoList != NULL);
  996. diskInfo = &(DetectInfoList[i]);
  997. if((diskData->Mbr.Signature != 0) &&
  998. (diskData->Mbr.Signature == diskInfo->Signature)) {
  999. DebugPrint((1, "DiskGetDetectInfo: found match for signature "
  1000. "%#08lx\n",
  1001. diskData->Mbr.Signature));
  1002. found = TRUE;
  1003. break;
  1004. } else if((diskData->Mbr.Signature == 0) &&
  1005. (diskData->Mbr.MbrCheckSum != 0) &&
  1006. (diskData->Mbr.MbrCheckSum == diskInfo->MbrCheckSum)) {
  1007. DebugPrint((1, "DiskGetDetectInfo: found match for xsum %#08lx\n",
  1008. diskData->Mbr.MbrCheckSum));
  1009. found = TRUE;
  1010. break;
  1011. }
  1012. }
  1013. if ( found ) {
  1014. DetectInfo->DetectionType = DetectInt13;
  1015. DetectInfo->Int13.DriveSelect = diskInfo->DriveParameters.DriveSelect;
  1016. DetectInfo->Int13.MaxCylinders = diskInfo->DriveParameters.MaxCylinders;
  1017. DetectInfo->Int13.SectorsPerTrack = diskInfo->DriveParameters.SectorsPerTrack;
  1018. DetectInfo->Int13.MaxHeads = diskInfo->DriveParameters.MaxHeads;
  1019. DetectInfo->Int13.NumberDrives = diskInfo->DriveParameters.NumberDrives;
  1020. RtlZeroMemory (&DetectInfo->ExInt13, sizeof (DetectInfo->ExInt13));
  1021. }
  1022. return (found ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL);
  1023. }
  1024. NTSTATUS
  1025. DiskReadSignature(
  1026. IN PDEVICE_OBJECT Fdo
  1027. )
  1028. /*++
  1029. Routine Description:
  1030. Read the disks signature from the drive. The signature can be either
  1031. a MBR signature or a GPT/EFI signature.
  1032. The low-level signature reading is done by IoReadDiskSignature().
  1033. Arguments:
  1034. Fdo - Pointer to the FDO of a disk to read the signature for.
  1035. Return Value:
  1036. NTSTATUS code.
  1037. --*/
  1038. {
  1039. NTSTATUS Status;
  1040. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = Fdo->DeviceExtension;
  1041. PDISK_DATA diskData = fdoExtension->CommonExtension.DriverData;
  1042. DISK_SIGNATURE Signature;
  1043. PAGED_CODE ();
  1044. Status = IoReadDiskSignature (Fdo,
  1045. fdoExtension->DiskGeometry.BytesPerSector,
  1046. &Signature);
  1047. if (!NT_SUCCESS (Status)) {
  1048. return Status;
  1049. }
  1050. if (Signature.PartitionStyle == PARTITION_STYLE_GPT) {
  1051. diskData->PartitionStyle = PARTITION_STYLE_GPT;
  1052. diskData->Efi.DiskId = Signature.Gpt.DiskId;
  1053. } else if (Signature.PartitionStyle == PARTITION_STYLE_MBR) {
  1054. diskData->PartitionStyle = PARTITION_STYLE_MBR;
  1055. diskData->Mbr.Signature = Signature.Mbr.Signature;
  1056. diskData->Mbr.MbrCheckSum = Signature.Mbr.CheckSum;
  1057. } else {
  1058. ASSERT (FALSE);
  1059. Status = STATUS_UNSUCCESSFUL;
  1060. }
  1061. return Status;
  1062. }
  1063. #endif // defined(_X86_)