Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3346 lines
89 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1991 - 1999
  3. Module Name:
  4. floppy.c
  5. Abstract:
  6. SCSI floppy class driver
  7. Author:
  8. Jeff Havens (jhavens)
  9. Environment:
  10. kernel mode only
  11. Notes:
  12. Revision History:
  13. 02/28/96 georgioc Merged this code with code developed by compaq in
  14. parallel with microsoft, for 120MB floppy support.
  15. 01/17/96 georgioc Made code PNP aware (uses the new \storage\classpnp/scsiport)
  16. --*/
  17. #include "stddef.h"
  18. #include "ntddk.h"
  19. #include "scsi.h"
  20. #include "classpnp.h"
  21. #include "initguid.h"
  22. #include "ntddstor.h"
  23. #define MODE_DATA_SIZE 192
  24. #define SCSI_FLOPPY_TIMEOUT 20
  25. #define SFLOPPY_SRB_LIST_SIZE 4
  26. //
  27. // Define all possible drive/media combinations, given drives listed above
  28. // and media types in ntdddisk.h.
  29. //
  30. // These values are used to index the DriveMediaConstants table.
  31. //
  32. #define NUMBER_OF_DRIVE_TYPES 7
  33. #define DRIVE_TYPE_120M 4 //120MB Floptical
  34. #define DRIVE_TYPE_NONE NUMBER_OF_DRIVE_TYPES
  35. //
  36. // This array describes all media types we support.
  37. // It should be arranged in the increasing order of density
  38. //
  39. // For a given drive, we list all the mediatypes that will
  40. // work with that drive. For instance, a 120MB drive will
  41. // take 720KB media, 1.44MB media, and 120MB media.
  42. //
  43. // Note that, DriveMediaConstants given below is grouped
  44. // as drive and media combination
  45. //
  46. typedef enum _DRIVE_MEDIA_TYPE {
  47. Drive360Media160, // 5.25" 360k drive; 160k media
  48. Drive360Media180, // 5.25" 360k drive; 180k media
  49. Drive360Media320, // 5.25" 360k drive; 320k media
  50. Drive360Media32X, // 5.25" 360k drive; 320k 1k secs
  51. Drive360Media360, // 5.25" 360k drive; 360k media
  52. Drive720Media720, // 3.5" 720k drive; 720k media
  53. Drive120Media160, // 5.25" 1.2Mb drive; 160k media
  54. Drive120Media180, // 5.25" 1.2Mb drive; 180k media
  55. Drive120Media320, // 5.25" 1.2Mb drive; 320k media
  56. Drive120Media32X, // 5.25" 1.2Mb drive; 320k 1k secs
  57. Drive120Media360, // 5.25" 1.2Mb drive; 360k media
  58. Drive120Media120, // 5.25" 1.2Mb drive; 1.2Mb media
  59. Drive144Media720, // 3.5" 1.44Mb drive; 720k media
  60. Drive144Media144, // 3.5" 1.44Mb drive; 1.44Mb media
  61. Drive288Media720, // 3.5" 2.88Mb drive; 720k media
  62. Drive288Media144, // 3.5" 2.88Mb drive; 1.44Mb media
  63. Drive288Media288, // 3.5" 2.88Mb drive; 2.88Mb media
  64. Drive2080Media720, // 3.5" 20.8Mb drive; 720k media
  65. Drive2080Media144, // 3.5" 20.8Mb drive; 1.44Mb media
  66. Drive2080Media2080, // 3.5" 20.8Mb drive; 20.8Mb media
  67. Drive32MMedia32M, // 3.5" 32Mb drive; 32MB media
  68. Drive120MMedia720, // 3.5" 120Mb drive; 720k media
  69. Drive120MMedia144, // 3.5" 120Mb drive; 1.44Mb media
  70. Drive120MMedia120M, // 3.5" 120Mb drive; 120Mb media
  71. Drive240MMedia144M, // 3.5" 240Mb drive; 1.44Mb media
  72. Drive240MMedia120M, // 3.5" 240Mb drive; 120Mb media
  73. Drive240MMedia240M // 3.5" 240Mb drive; 240Mb media
  74. } DRIVE_MEDIA_TYPE;
  75. //
  76. // When we want to determine the media type in a drive, we will first
  77. // guess that the media with highest possible density is in the drive,
  78. // and keep trying lower densities until we can successfully read from
  79. // the drive.
  80. //
  81. // These values are used to select a DRIVE_MEDIA_TYPE value.
  82. //
  83. // The following table defines ranges that apply to the DRIVE_MEDIA_TYPE
  84. // enumerated values when trying media types for a particular drive type.
  85. // Note that for this to work, the DRIVE_MEDIA_TYPE values must be sorted
  86. // by ascending densities within drive types. Also, for maximum track
  87. // size to be determined properly, the drive types must be in ascending
  88. // order.
  89. //
  90. typedef struct _DRIVE_MEDIA_LIMITS {
  91. DRIVE_MEDIA_TYPE HighestDriveMediaType;
  92. DRIVE_MEDIA_TYPE LowestDriveMediaType;
  93. } DRIVE_MEDIA_LIMITS, *PDRIVE_MEDIA_LIMITS;
  94. #if 0
  95. DRIVE_MEDIA_LIMITS DriveMediaLimits[NUMBER_OF_DRIVE_TYPES] = {
  96. { Drive360Media360, Drive360Media160 }, // DRIVE_TYPE_0360
  97. { Drive120Media120, Drive120Media160 }, // DRIVE_TYPE_1200
  98. { Drive720Media720, Drive720Media720 }, // DRIVE_TYPE_0720
  99. { Drive144Media144, Drive144Media720 }, // DRIVE_TYPE_1440
  100. { Drive288Media288, Drive288Media720 }, // DRIVE_TYPE_2880
  101. { Drive2080Media2080, Drive2080Media720 }
  102. };
  103. #else
  104. DRIVE_MEDIA_LIMITS DriveMediaLimits[NUMBER_OF_DRIVE_TYPES] = {
  105. { Drive720Media720, Drive720Media720 }, // DRIVE_TYPE_0720
  106. { Drive144Media144, Drive144Media720}, // DRIVE_TYPE_1440
  107. { Drive288Media288, Drive288Media720}, // DRIVE_TYPE_2880
  108. { Drive2080Media2080, Drive2080Media720 },
  109. { Drive32MMedia32M, Drive32MMedia32M }, // DRIVE_TYPE_32M
  110. { Drive120MMedia120M, Drive120MMedia720 }, // DRIVE_TYPE_120M
  111. { Drive240MMedia240M, Drive240MMedia144M } // DRIVE_TYPE_240M
  112. };
  113. #endif
  114. //
  115. // For each drive/media combination, define important constants.
  116. //
  117. typedef struct _DRIVE_MEDIA_CONSTANTS {
  118. MEDIA_TYPE MediaType;
  119. USHORT BytesPerSector;
  120. UCHAR SectorsPerTrack;
  121. USHORT MaximumTrack;
  122. UCHAR NumberOfHeads;
  123. } DRIVE_MEDIA_CONSTANTS, *PDRIVE_MEDIA_CONSTANTS;
  124. //
  125. // Magic value to add to the SectorLengthCode to use it as a shift value
  126. // to determine the sector size.
  127. //
  128. #define SECTORLENGTHCODE_TO_BYTESHIFT 7
  129. //
  130. // The following values were gleaned from many different sources, which
  131. // often disagreed with each other. Where numbers were in conflict, I
  132. // chose the more conservative or most-often-selected value.
  133. //
  134. DRIVE_MEDIA_CONSTANTS DriveMediaConstants[] =
  135. {
  136. { F5_160_512, 0x200, 0x08, 0x27, 0x1 },
  137. { F5_180_512, 0x200, 0x09, 0x27, 0x1 },
  138. { F5_320_1024, 0x400, 0x04, 0x27, 0x2 },
  139. { F5_320_512, 0x200, 0x08, 0x27, 0x2 },
  140. { F5_360_512, 0x200, 0x09, 0x27, 0x2 },
  141. { F3_720_512, 0x200, 0x09, 0x4f, 0x2 },
  142. { F5_160_512, 0x200, 0x08, 0x27, 0x1 },
  143. { F5_180_512, 0x200, 0x09, 0x27, 0x1 },
  144. { F5_320_1024, 0x400, 0x04, 0x27, 0x2 },
  145. { F5_320_512, 0x200, 0x08, 0x27, 0x2 },
  146. { F5_360_512, 0x200, 0x09, 0x27, 0x2 },
  147. { F5_1Pt2_512, 0x200, 0x0f, 0x4f, 0x2 },
  148. { F3_720_512, 0x200, 0x09, 0x4f, 0x2 },
  149. { F3_1Pt44_512, 0x200, 0x12, 0x4f, 0x2 },
  150. { F3_720_512, 0x200, 0x09, 0x4f, 0x2 },
  151. { F3_1Pt44_512, 0x200, 0x12, 0x4f, 0x2 },
  152. { F3_2Pt88_512, 0x200, 0x24, 0x4f, 0x2 },
  153. { F3_720_512, 0x200, 0x09, 0x4f, 0x2 },
  154. { F3_1Pt44_512, 0x200, 0x12, 0x4f, 0x2 },
  155. { F3_20Pt8_512, 0x200, 0x1b, 0xfa, 0x6 },
  156. { F3_32M_512, 0x200, 0x20, 0x3ff,0x2},
  157. { F3_720_512, 0x200, 0x09, 0x4f, 0x2 },
  158. { F3_1Pt44_512, 0x200, 0x12, 0x4f, 0x2 },
  159. { F3_120M_512, 0x200, 0x20, 0x3c2,0x8 },
  160. { F3_1Pt44_512, 0x200, 0x12, 0x4f, 0x2 },
  161. { F3_120M_512, 0x200, 0x20, 0x3c2,0x8 },
  162. { F3_240M_512, 0x200, 0x38, 0x105,0x20}
  163. };
  164. #define NUMBER_OF_DRIVE_MEDIA_COMBINATIONS sizeof(DriveMediaConstants)/sizeof(DRIVE_MEDIA_CONSTANTS)
  165. //
  166. // floppy device data
  167. //
  168. typedef struct _DISK_DATA {
  169. ULONG DriveType;
  170. BOOLEAN IsDMF;
  171. // BOOLEAN EnableDMF;
  172. UNICODE_STRING FloppyInterfaceString;
  173. } DISK_DATA, *PDISK_DATA;
  174. //
  175. // The FloppyCapacities and FloppyGeometries arrays are used by the
  176. // USBFlopGetMediaTypes() and USBFlopFormatTracks() routines.
  177. // The FloppyCapacities and FloppyGeometries arrays must be kept in 1:1 sync,
  178. // i.e. each FloppyGeometries[i] must correspond to each FloppyCapacities[i].
  179. // Also, the arrays must be kept in sorted ascending order so that they
  180. // are returned in sorted ascending order by IOCTL_DISK_GET_MEDIA_TYPES.
  181. //
  182. typedef struct _FORMATTED_CAPACITY
  183. {
  184. ULONG NumberOfBlocks;
  185. ULONG BlockLength;
  186. BOOLEAN CanFormat; // return for IOCTL_DISK_GET_MEDIA_TYPES ?
  187. } FORMATTED_CAPACITY, *PFORMATTED_CAPACITY;
  188. FORMATTED_CAPACITY FloppyCapacities[] =
  189. {
  190. // Blocks BlockLen CanFormat H T B/S S/T
  191. {0x000500, 0x0200, TRUE}, // 2 80 512 8 640 KB F5_640_512
  192. {0x0005A0, 0x0200, TRUE}, // 2 80 512 9 720 KB F3_720_512
  193. {0x000960, 0x0200, TRUE}, // 2 80 512 15 1.20 MB F3_1Pt2_512 (Toshiba)
  194. {0x0004D0, 0x0400, TRUE}, // 2 77 1024 8 1.23 MB F3_1Pt23_1024 (NEC)
  195. {0x000B40, 0x0200, TRUE}, // 2 80 512 18 1.44 MB F3_1Pt44_512
  196. {0x000D20, 0x0200, FALSE}, // 2 80 512 21 1.70 MB DMF
  197. {0x03C300, 0x0200, TRUE}, // 8 963 512 32 120 MB F3_120M_512
  198. {0x0600A4, 0x0200, TRUE}, // 13 890 512 34 200 MB F3_200Mb_512 (HiFD)
  199. {0x072A00, 0x0200, TRUE}, // 32 262 512 56 240 MB F3_240M_512
  200. {0x010000, 0x0200, FALSE} // 2 1024 512 32 32 MB F3_32M_512
  201. };
  202. DISK_GEOMETRY FloppyGeometries[] =
  203. {
  204. // Cyl MediaType Trk/Cyl Sec/Trk Bytes/Sec
  205. {{80,0}, F5_640_512, 2, 8, 512},
  206. {{80,0}, F3_720_512, 2, 9, 512},
  207. {{80,0}, F3_1Pt2_512, 2, 15, 512},
  208. {{77,0}, F3_1Pt23_1024, 2, 8, 1024},
  209. {{80,0}, F3_1Pt44_512, 2, 18, 512},
  210. {{80,0}, F3_1Pt44_512, 2, 21, 512}, // DMF -> F3_1Pt44_512
  211. {{963,0}, F3_120M_512, 8, 32, 512},
  212. {{890,0}, F3_200Mb_512, 13, 34, 512},
  213. {{262,0}, F3_240M_512, 32, 56, 512},
  214. {{1024,0}, F3_32M_512, 2, 32, 512}
  215. };
  216. #define FLOPPY_CAPACITIES (sizeof(FloppyCapacities)/sizeof(FloppyCapacities[0]))
  217. C_ASSERT((sizeof(FloppyGeometries)/sizeof(FloppyGeometries[0])) == FLOPPY_CAPACITIES);
  218. //
  219. // The following structures are used by USBFlopFormatTracks()
  220. //
  221. #pragma pack (push, 1)
  222. typedef struct _CDB12FORMAT
  223. {
  224. UCHAR OperationCode;
  225. UCHAR DefectListFormat : 3;
  226. UCHAR CmpList : 1;
  227. UCHAR FmtData : 1;
  228. UCHAR LogicalUnitNumber : 3;
  229. UCHAR TrackNumber;
  230. UCHAR InterleaveMsb;
  231. UCHAR InterleaveLsb;
  232. UCHAR Reserved1[2];
  233. UCHAR ParameterListLengthMsb;
  234. UCHAR ParameterListLengthLsb;
  235. UCHAR Reserved2[3];
  236. } CDB12FORMAT, *PCDB12FORMAT;
  237. typedef struct _DEFECT_LIST_HEADER
  238. {
  239. UCHAR Reserved1;
  240. UCHAR Side : 1;
  241. UCHAR Immediate : 1;
  242. UCHAR Reserved2 : 2;
  243. UCHAR SingleTrack : 1;
  244. UCHAR DisableCert : 1;
  245. UCHAR Reserved3 : 1;
  246. UCHAR FormatOptionsValid : 1;
  247. UCHAR DefectListLengthMsb;
  248. UCHAR DefectListLengthLsb;
  249. } DEFECT_LIST_HEADER, *PDEFECT_LIST_HEADER;
  250. typedef struct _FORMAT_UNIT_PARAMETER_LIST
  251. {
  252. DEFECT_LIST_HEADER DefectListHeader;
  253. FORMATTED_CAPACITY_DESCRIPTOR FormatDescriptor;
  254. } FORMAT_UNIT_PARAMETER_LIST, *PFORMAT_UNIT_PARAMETER_LIST;
  255. #pragma pack (pop)
  256. NTSTATUS
  257. DriverEntry(
  258. IN PDRIVER_OBJECT DriverObject,
  259. IN PUNICODE_STRING RegistryPath
  260. );
  261. VOID
  262. ScsiFlopUnload(
  263. IN PDRIVER_OBJECT DriverObject
  264. );
  265. NTSTATUS
  266. ScsiFlopAddDevice (
  267. IN PDRIVER_OBJECT DriverObject,
  268. IN PDEVICE_OBJECT Pdo
  269. );
  270. NTSTATUS
  271. ScsiFlopInitDevice(
  272. IN PDEVICE_OBJECT Fdo
  273. );
  274. NTSTATUS
  275. ScsiFlopStartDevice(
  276. IN PDEVICE_OBJECT Fdo
  277. );
  278. NTSTATUS
  279. ScsiFlopRemoveDevice(
  280. IN PDEVICE_OBJECT Fdo,
  281. IN UCHAR Type
  282. );
  283. NTSTATUS
  284. ScsiFlopStopDevice(
  285. IN PDEVICE_OBJECT Fdo,
  286. IN UCHAR Type
  287. );
  288. BOOLEAN
  289. FindScsiFlops(
  290. IN PDRIVER_OBJECT DriverObject,
  291. IN PUNICODE_STRING RegistryPath,
  292. IN PCLASS_INIT_DATA InitializationData,
  293. IN PDEVICE_OBJECT PortDeviceObject,
  294. IN ULONG PortNumber
  295. );
  296. NTSTATUS
  297. ScsiFlopReadWriteVerification(
  298. IN PDEVICE_OBJECT DeviceObject,
  299. IN PIRP Irp
  300. );
  301. NTSTATUS
  302. ScsiFlopDeviceControl(
  303. IN PDEVICE_OBJECT DeviceObject,
  304. IN PIRP Irp
  305. );
  306. BOOLEAN
  307. IsFloppyDevice(
  308. PDEVICE_OBJECT DeviceObject
  309. );
  310. NTSTATUS
  311. CreateFlopDeviceObject(
  312. IN PDRIVER_OBJECT DriverObject,
  313. IN PDEVICE_OBJECT PortDeviceObject,
  314. IN ULONG DeviceCount
  315. );
  316. NTSTATUS
  317. DetermineMediaType(
  318. PDEVICE_OBJECT DeviceObject
  319. );
  320. ULONG
  321. DetermineDriveType(
  322. PDEVICE_OBJECT DeviceObject
  323. );
  324. BOOLEAN
  325. FlCheckFormatParameters(
  326. IN PDEVICE_OBJECT DeviceObject,
  327. IN PFORMAT_PARAMETERS FormatParameters
  328. );
  329. NTSTATUS
  330. FormatMedia(
  331. PDEVICE_OBJECT DeviceObject,
  332. MEDIA_TYPE MediaType
  333. );
  334. NTSTATUS
  335. FlopticalFormatMedia(
  336. PDEVICE_OBJECT DeviceObject,
  337. PFORMAT_PARAMETERS Format
  338. );
  339. VOID
  340. ScsiFlopProcessError(
  341. PDEVICE_OBJECT DeviceObject,
  342. PSCSI_REQUEST_BLOCK Srb,
  343. NTSTATUS *Status,
  344. BOOLEAN *Retry
  345. );
  346. LONG
  347. SFlopStringCmp (
  348. PCHAR FirstStr,
  349. PCHAR SecondStr,
  350. ULONG Count
  351. );
  352. NTSTATUS
  353. USBFlopGetMediaTypes(
  354. IN PDEVICE_OBJECT DeviceObject,
  355. IN PIRP Irp
  356. );
  357. NTSTATUS
  358. USBFlopFormatTracks(
  359. IN PDEVICE_OBJECT DeviceObject,
  360. IN PIRP Irp
  361. );
  362. #ifdef ALLOC_PRAGMA
  363. #pragma alloc_text(INIT, DriverEntry)
  364. #pragma alloc_text(PAGE, ScsiFlopUnload)
  365. #pragma alloc_text(PAGE, ScsiFlopAddDevice)
  366. #pragma alloc_text(PAGE, CreateFlopDeviceObject)
  367. #pragma alloc_text(PAGE, ScsiFlopStartDevice)
  368. #pragma alloc_text(PAGE, IsFloppyDevice)
  369. #pragma alloc_text(PAGE, SFlopStringCmp)
  370. #pragma alloc_text(PAGE, DetermineMediaType)
  371. #pragma alloc_text(PAGE, DetermineDriveType)
  372. #pragma alloc_text(PAGE, FlCheckFormatParameters)
  373. #pragma alloc_text(PAGE, FormatMedia)
  374. #pragma alloc_text(PAGE, FlopticalFormatMedia)
  375. #pragma alloc_text(PAGE, USBFlopGetMediaTypes)
  376. #pragma alloc_text(PAGE, USBFlopFormatTracks)
  377. #endif
  378. NTSTATUS
  379. DriverEntry(
  380. IN PDRIVER_OBJECT DriverObject,
  381. IN PUNICODE_STRING RegistryPath
  382. )
  383. /*++
  384. Routine Description:
  385. This is the system initialization routine for installable drivers.
  386. It calls the SCSI class driver initialization routine.
  387. Arguments:
  388. DriverObject - Pointer to driver object created by system.
  389. Return Value:
  390. NTSTATUS
  391. --*/
  392. {
  393. CLASS_INIT_DATA InitializationData;
  394. //
  395. // Zero InitData
  396. //
  397. RtlZeroMemory (&InitializationData, sizeof(CLASS_INIT_DATA));
  398. //
  399. // Set sizes
  400. //
  401. InitializationData.InitializationDataSize = sizeof(CLASS_INIT_DATA);
  402. InitializationData.FdoData.DeviceExtensionSize =
  403. sizeof(FUNCTIONAL_DEVICE_EXTENSION) + sizeof(DISK_DATA);
  404. InitializationData.FdoData.DeviceType = FILE_DEVICE_DISK;
  405. InitializationData.FdoData.DeviceCharacteristics = FILE_REMOVABLE_MEDIA | FILE_FLOPPY_DISKETTE;
  406. //
  407. // Set entry points
  408. //
  409. InitializationData.FdoData.ClassInitDevice = ScsiFlopInitDevice;
  410. InitializationData.FdoData.ClassStartDevice = ScsiFlopStartDevice;
  411. InitializationData.FdoData.ClassStopDevice = ScsiFlopStopDevice;
  412. InitializationData.FdoData.ClassRemoveDevice = ScsiFlopRemoveDevice;
  413. InitializationData.FdoData.ClassReadWriteVerification = ScsiFlopReadWriteVerification;
  414. InitializationData.FdoData.ClassDeviceControl = ScsiFlopDeviceControl;
  415. InitializationData.FdoData.ClassShutdownFlush = NULL;
  416. InitializationData.FdoData.ClassCreateClose = NULL;
  417. InitializationData.FdoData.ClassError = ScsiFlopProcessError;
  418. InitializationData.ClassStartIo = NULL;
  419. InitializationData.ClassAddDevice = ScsiFlopAddDevice;
  420. InitializationData.ClassUnload = ScsiFlopUnload;
  421. //
  422. // Call the class init routine
  423. //
  424. return ClassInitialize( DriverObject, RegistryPath, &InitializationData);
  425. } // end DriverEntry()
  426. VOID
  427. ScsiFlopUnload(
  428. IN PDRIVER_OBJECT DriverObject
  429. )
  430. {
  431. PAGED_CODE();
  432. UNREFERENCED_PARAMETER(DriverObject);
  433. return;
  434. }
  435. NTSTATUS
  436. ScsiFlopAddDevice (
  437. IN PDRIVER_OBJECT DriverObject,
  438. IN PDEVICE_OBJECT Pdo
  439. )
  440. /*++
  441. Routine Description:
  442. This routine creates and initializes a new FDO for the corresponding
  443. PDO. It may perform property queries on the FDO but cannot do any
  444. media access operations.
  445. Arguments:
  446. DriverObject - Scsiscan class driver object.
  447. Pdo - the physical device object we are being added to
  448. Return Value:
  449. status
  450. --*/
  451. {
  452. PCONFIGURATION_INFORMATION configurationInformation;
  453. NTSTATUS status;
  454. ULONG floppyCount = IoGetConfigurationInformation()->FloppyCount;
  455. //
  456. // Get the number of disks already initialized.
  457. //
  458. status = CreateFlopDeviceObject(DriverObject, Pdo, floppyCount);
  459. if (NT_SUCCESS(status)) {
  460. //
  461. // Increment system floppy device count.
  462. //
  463. IoGetConfigurationInformation()->FloppyCount++;
  464. }
  465. return status;
  466. }
  467. NTSTATUS
  468. CreateFlopDeviceObject(
  469. IN PDRIVER_OBJECT DriverObject,
  470. IN PDEVICE_OBJECT Pdo,
  471. IN ULONG DeviceCount
  472. )
  473. /*++
  474. Routine Description:
  475. This routine creates an object for the device and then calls the
  476. SCSI port driver for media capacity and sector size.
  477. Arguments:
  478. DriverObject - Pointer to driver object created by system.
  479. PortDeviceObject - to connect to SCSI port driver.
  480. DeviceCount - Number of previously installed Floppys.
  481. AdapterDescriptor - Pointer to structure returned by SCSI port
  482. driver describing adapter capabilites (and limitations).
  483. DeviceDescriptor - Pointer to configuration information for this device.
  484. Return Value:
  485. --*/
  486. {
  487. NTSTATUS status;
  488. PDEVICE_OBJECT deviceObject;
  489. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = NULL;
  490. PDISK_DATA diskData;
  491. PVOID senseData;
  492. BOOLEAN freeDevice = TRUE;
  493. DebugPrint((3,"CreateFlopDeviceObject: Enter routine\n"));
  494. //
  495. // Try to claim the device.
  496. //
  497. status = ClassClaimDevice(Pdo,FALSE);
  498. if (!NT_SUCCESS(status)) {
  499. return(status);
  500. }
  501. DeviceCount--;
  502. do {
  503. UCHAR name[256];
  504. //
  505. // Create device object for this device.
  506. //
  507. DeviceCount++;
  508. sprintf(name, "\\Device\\Floppy%d", DeviceCount);
  509. status = ClassCreateDeviceObject(DriverObject,
  510. name,
  511. Pdo,
  512. TRUE,
  513. &deviceObject);
  514. } while ((status == STATUS_OBJECT_NAME_COLLISION) ||
  515. (status == STATUS_OBJECT_NAME_EXISTS));
  516. if (!NT_SUCCESS(status)) {
  517. DebugPrint((1,"CreateFlopDeviceObjects: Can not create device\n"));
  518. goto CreateFlopDeviceObjectExit;
  519. }
  520. //
  521. // Indicate that IRPs should include MDLs.
  522. //
  523. deviceObject->Flags |= DO_DIRECT_IO;
  524. fdoExtension = deviceObject->DeviceExtension;
  525. //
  526. // Back pointer to device object.
  527. //
  528. fdoExtension->CommonExtension.DeviceObject = deviceObject;
  529. //
  530. // This is the physical device.
  531. //
  532. fdoExtension->CommonExtension.PartitionZeroExtension = fdoExtension;
  533. //
  534. // Reset the drive type.
  535. //
  536. diskData = (PDISK_DATA) fdoExtension->CommonExtension.DriverData;
  537. diskData->DriveType = DRIVE_TYPE_NONE;
  538. diskData->IsDMF = FALSE;
  539. // diskData->EnableDMF = TRUE;
  540. //
  541. // Initialize lock count to zero. The lock count is used to
  542. // disable the ejection mechanism when media is mounted.
  543. //
  544. fdoExtension->LockCount = 0;
  545. //
  546. // Save system floppy number
  547. //
  548. fdoExtension->DeviceNumber = DeviceCount;
  549. //
  550. // Set the alignment requirements for the device based on the
  551. // host adapter requirements
  552. //
  553. if (Pdo->AlignmentRequirement > deviceObject->AlignmentRequirement) {
  554. deviceObject->AlignmentRequirement = Pdo->AlignmentRequirement;
  555. }
  556. //
  557. // Clear the SrbFlags and disable synchronous transfers
  558. //
  559. fdoExtension->SrbFlags = SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  560. //
  561. // Finally, attach to the PDO
  562. //
  563. fdoExtension->LowerPdo = Pdo;
  564. fdoExtension->CommonExtension.LowerDeviceObject =
  565. IoAttachDeviceToDeviceStack(deviceObject, Pdo);
  566. if(fdoExtension->CommonExtension.LowerDeviceObject == NULL) {
  567. status = STATUS_UNSUCCESSFUL;
  568. goto CreateFlopDeviceObjectExit;
  569. }
  570. deviceObject->StackSize++;
  571. //
  572. // The device is initialized properly - mark it as such.
  573. //
  574. deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
  575. return STATUS_SUCCESS;
  576. CreateFlopDeviceObjectExit:
  577. if (deviceObject != NULL) {
  578. IoDeleteDevice(deviceObject);
  579. }
  580. return status;
  581. } // end CreateFlopDeviceObject()
  582. NTSTATUS
  583. ScsiFlopInitDevice(
  584. IN PDEVICE_OBJECT Fdo
  585. )
  586. {
  587. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = Fdo->DeviceExtension;
  588. PCOMMON_DEVICE_EXTENSION commonExtension = Fdo->DeviceExtension;
  589. PDISK_DATA diskData = commonExtension->DriverData;
  590. PVOID senseData = NULL;
  591. ULONG timeOut;
  592. BOOLEAN srbListInitialized = FALSE;
  593. NTSTATUS status = STATUS_SUCCESS;
  594. //
  595. // Allocate request sense buffer.
  596. //
  597. senseData = ExAllocatePool(NonPagedPoolCacheAligned, SENSE_BUFFER_SIZE);
  598. if (senseData == NULL) {
  599. //
  600. // The buffer cannot be allocated.
  601. //
  602. status = STATUS_INSUFFICIENT_RESOURCES;
  603. return status;
  604. }
  605. //
  606. // Set the sense data pointer in the device extension.
  607. //
  608. fdoExtension->SenseData = senseData;
  609. //
  610. // Build the lookaside list for srb's for this device.
  611. //
  612. ClassInitializeSrbLookasideList((PCOMMON_DEVICE_EXTENSION)fdoExtension,
  613. SFLOPPY_SRB_LIST_SIZE);
  614. srbListInitialized = TRUE;
  615. //
  616. // Register for media change notification
  617. //
  618. ClassInitializeMediaChangeDetection(fdoExtension,
  619. "SFloppy");
  620. //
  621. // Set timeout value in seconds.
  622. //
  623. timeOut = ClassQueryTimeOutRegistryValue(Fdo);
  624. if (timeOut) {
  625. fdoExtension->TimeOutValue = timeOut;
  626. } else {
  627. fdoExtension->TimeOutValue = SCSI_FLOPPY_TIMEOUT;
  628. }
  629. //
  630. // Floppies are not partitionable so starting offset is 0.
  631. //
  632. fdoExtension->CommonExtension.StartingOffset.QuadPart = (LONGLONG)0;
  633. #if 0
  634. if (!IsFloppyDevice(Fdo) ||
  635. !(Fdo->Characteristics & FILE_REMOVABLE_MEDIA) ||
  636. (fdoExtension->DeviceDescriptor->DeviceType != DIRECT_ACCESS_DEVICE)) {
  637. ExFreePool(senseData);
  638. status = STATUS_NO_SUCH_DEVICE;
  639. return status;
  640. }
  641. #endif
  642. RtlZeroMemory(&(fdoExtension->DiskGeometry),
  643. sizeof(DISK_GEOMETRY));
  644. //
  645. // Determine the media type if possible. Set the current media type to
  646. // Unknown so that determine media type will check the media.
  647. //
  648. fdoExtension->DiskGeometry.MediaType = Unknown;
  649. #if 0
  650. {
  651. PUCHAR vendorId;
  652. UCHAR vendorString[6] = {'I','N','S','I','T','E'};
  653. vendorId = (PUCHAR)fdoExtension->DeviceDescriptor +
  654. fdoExtension->DeviceDescriptor->VendorIdOffset;
  655. if (!SFlopStringCmp(vendorId,vendorString,6)) {
  656. diskData->EnableDMF = FALSE;
  657. }
  658. }
  659. #endif
  660. //
  661. // Register interfaces for this device.
  662. //
  663. {
  664. PDISK_DATA diskData = (PDISK_DATA) commonExtension->DriverData;
  665. UNICODE_STRING interfaceName;
  666. RtlInitUnicodeString(&interfaceName, NULL);
  667. status = IoRegisterDeviceInterface(fdoExtension->LowerPdo,
  668. (LPGUID) &FloppyClassGuid,
  669. NULL,
  670. &interfaceName);
  671. if(NT_SUCCESS(status)) {
  672. diskData->FloppyInterfaceString = interfaceName;
  673. } else {
  674. RtlInitUnicodeString(&(diskData->FloppyInterfaceString), NULL);
  675. DebugPrint((1, "ScsiFlopStartDevice: Unable to register device "
  676. "interface for fdo %#p [%#08lx]\n",
  677. Fdo, status));
  678. }
  679. }
  680. return (STATUS_SUCCESS);
  681. }
  682. NTSTATUS
  683. ScsiFlopStartDevice(
  684. IN PDEVICE_OBJECT Fdo
  685. )
  686. {
  687. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = Fdo->DeviceExtension;
  688. PCOMMON_DEVICE_EXTENSION commonExtension = Fdo->DeviceExtension;
  689. PIRP irp;
  690. IO_STATUS_BLOCK ioStatus;
  691. SCSI_ADDRESS scsiAddress;
  692. WCHAR ntNameBuffer[256];
  693. UNICODE_STRING ntUnicodeString;
  694. WCHAR arcNameBuffer[256];
  695. UNICODE_STRING arcUnicodeString;
  696. KEVENT event;
  697. NTSTATUS status = STATUS_SUCCESS;
  698. KeInitializeEvent(&event,SynchronizationEvent,FALSE);
  699. DetermineMediaType(Fdo); // ignore unsuccessful here
  700. //
  701. // Create a symbolic link from the disk name to the corresponding
  702. // ARC name, to be used if we're booting off the disk. This will
  703. // fail if it's not system initialization time; that's fine. The
  704. // ARC name looks something like \ArcName\scsi(0)Flop(0)fdisk(0).
  705. // In order to get the address, we need to send a IOCTL_SCSI_GET_ADDRESS...
  706. //
  707. irp = IoBuildDeviceIoControlRequest(IOCTL_SCSI_GET_ADDRESS,
  708. Fdo,
  709. NULL,
  710. 0,
  711. &scsiAddress,
  712. sizeof(scsiAddress),
  713. FALSE,
  714. &event,
  715. &ioStatus);
  716. if (irp == NULL) {
  717. return STATUS_INSUFFICIENT_RESOURCES;
  718. }
  719. status = IoCallDriver(fdoExtension->CommonExtension.LowerDeviceObject, irp);
  720. if (status == STATUS_PENDING) {
  721. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  722. status = ioStatus.Status;
  723. }
  724. swprintf(arcNameBuffer,
  725. L"\\ArcName\\scsi(%d)disk(%d)fdisk(%d)",
  726. scsiAddress.PortNumber,
  727. scsiAddress.TargetId,
  728. scsiAddress.Lun);
  729. RtlInitUnicodeString(&arcUnicodeString, arcNameBuffer);
  730. //
  731. // Create device object for this device.
  732. //
  733. swprintf(ntNameBuffer,L"\\Device\\Floppy%d",fdoExtension->DeviceNumber);
  734. //
  735. // Create local copy of unicode string
  736. //
  737. RtlInitUnicodeString(&ntUnicodeString,ntNameBuffer);
  738. IoAssignArcName(&arcUnicodeString, &ntUnicodeString);
  739. //
  740. // Create the multi() arc name -- Create the "fake"
  741. // name of multi(0)disk(0)fdisk(#) to handle the case where the
  742. // SCSI floppy is the only floppy in the system. If this fails
  743. // it doesn't matter because the previous scsi() based ArcName
  744. // will work. This name is necessary for installation.
  745. //
  746. swprintf(arcNameBuffer, L"\\ArcName\\multi(%d)disk(%d)fdisk(%d)",
  747. 0,
  748. 0,
  749. fdoExtension->DeviceNumber);
  750. RtlInitUnicodeString(&arcUnicodeString, arcNameBuffer);
  751. IoAssignArcName(&arcUnicodeString, &ntUnicodeString);
  752. //
  753. // Set our interface state.
  754. //
  755. {
  756. PDISK_DATA diskData = commonExtension->DriverData;
  757. if(diskData->FloppyInterfaceString.Buffer != NULL) {
  758. status = IoSetDeviceInterfaceState(
  759. &(diskData->FloppyInterfaceString),
  760. TRUE);
  761. #if DBG
  762. if(!NT_SUCCESS(status)) {
  763. DebugPrint((1, "ScsiFlopStartDevice: Unable to set device "
  764. "interface state to TRUE for fdo %#p "
  765. "[%#08lx]\n",
  766. Fdo, status));
  767. }
  768. #endif
  769. }
  770. }
  771. return STATUS_SUCCESS;
  772. }
  773. NTSTATUS
  774. ScsiFlopReadWriteVerification(
  775. IN PDEVICE_OBJECT DeviceObject,
  776. IN PIRP Irp
  777. )
  778. /*++
  779. Routine Description:
  780. Arguments:
  781. Return Value:
  782. NT Status
  783. --*/
  784. {
  785. return STATUS_SUCCESS;
  786. } // end ScsiFlopReadWriteVerification()
  787. NTSTATUS
  788. ScsiFlopDeviceControl(
  789. PDEVICE_OBJECT DeviceObject,
  790. PIRP Irp
  791. )
  792. /*++
  793. Routine Description:
  794. Arguments:
  795. Return Value:
  796. Status is returned.
  797. --*/
  798. {
  799. KIRQL currentIrql;
  800. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  801. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  802. PSCSI_REQUEST_BLOCK srb;
  803. PCDB cdb;
  804. NTSTATUS status;
  805. PDISK_GEOMETRY outputBuffer;
  806. ULONG outputBufferLength;
  807. ULONG i;
  808. DRIVE_MEDIA_TYPE lowestDriveMediaType;
  809. DRIVE_MEDIA_TYPE highestDriveMediaType;
  810. PFORMAT_PARAMETERS formatParameters;
  811. PMODE_PARAMETER_HEADER modeData;
  812. ULONG length;
  813. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  814. if (srb == NULL) {
  815. Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
  816. if (IoIsErrorUserInduced(STATUS_INSUFFICIENT_RESOURCES)) {
  817. IoSetHardErrorOrVerifyDevice(Irp, DeviceObject);
  818. }
  819. KeRaiseIrql(DISPATCH_LEVEL, &currentIrql);
  820. ClassReleaseRemoveLock(DeviceObject, Irp);
  821. ClassCompleteRequest(DeviceObject, Irp, 0);
  822. KeLowerIrql(currentIrql);
  823. return(STATUS_INSUFFICIENT_RESOURCES);
  824. }
  825. //
  826. // Write zeros to Srb.
  827. //
  828. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  829. cdb = (PCDB)srb->Cdb;
  830. switch (irpStack->Parameters.DeviceIoControl.IoControlCode) {
  831. case IOCTL_DISK_VERIFY: {
  832. PVERIFY_INFORMATION verifyInfo = Irp->AssociatedIrp.SystemBuffer;
  833. LARGE_INTEGER byteOffset;
  834. ULONG sectorOffset;
  835. USHORT sectorCount;
  836. //
  837. // Validate buffer length.
  838. //
  839. if (irpStack->Parameters.DeviceIoControl.InputBufferLength <
  840. sizeof(VERIFY_INFORMATION)) {
  841. status = STATUS_INFO_LENGTH_MISMATCH;
  842. break;
  843. }
  844. //
  845. // Verify sectors
  846. //
  847. srb->CdbLength = 10;
  848. cdb->CDB10.OperationCode = SCSIOP_VERIFY;
  849. //
  850. // Add disk offset to starting sector.
  851. //
  852. byteOffset.QuadPart = fdoExtension->CommonExtension.StartingOffset.QuadPart +
  853. verifyInfo->StartingOffset.QuadPart;
  854. //
  855. // Convert byte offset to sector offset.
  856. //
  857. sectorOffset = (ULONG)(byteOffset.QuadPart >> fdoExtension->SectorShift);
  858. //
  859. // Convert ULONG byte count to USHORT sector count.
  860. //
  861. sectorCount = (USHORT)(verifyInfo->Length >> fdoExtension->SectorShift);
  862. //
  863. // Move little endian values into CDB in big endian format.
  864. //
  865. cdb->CDB10.LogicalBlockByte0 = ((PFOUR_BYTE)&sectorOffset)->Byte3;
  866. cdb->CDB10.LogicalBlockByte1 = ((PFOUR_BYTE)&sectorOffset)->Byte2;
  867. cdb->CDB10.LogicalBlockByte2 = ((PFOUR_BYTE)&sectorOffset)->Byte1;
  868. cdb->CDB10.LogicalBlockByte3 = ((PFOUR_BYTE)&sectorOffset)->Byte0;
  869. cdb->CDB10.TransferBlocksMsb = ((PFOUR_BYTE)&sectorCount)->Byte1;
  870. cdb->CDB10.TransferBlocksLsb = ((PFOUR_BYTE)&sectorCount)->Byte0;
  871. //
  872. // The verify command is used by the NT FORMAT utility and
  873. // requests are sent down for 5% of the volume size. The
  874. // request timeout value is calculated based on the number of
  875. // sectors verified.
  876. //
  877. srb->TimeOutValue = ((sectorCount + 0x7F) >> 7) *
  878. fdoExtension->TimeOutValue;
  879. status = ClassSendSrbAsynchronous(DeviceObject,
  880. srb,
  881. Irp,
  882. NULL,
  883. 0,
  884. FALSE);
  885. return(status);
  886. }
  887. case IOCTL_DISK_GET_PARTITION_INFO: {
  888. if (fdoExtension->AdapterDescriptor->BusType == BusTypeUsb) {
  889. status = USBFlopGetMediaTypes(DeviceObject, NULL);
  890. // Don't need to propagate any error if one occurs
  891. //
  892. status = STATUS_SUCCESS;
  893. } else {
  894. status = DetermineMediaType(DeviceObject);
  895. }
  896. if (!NT_SUCCESS(status)) {
  897. // so will propogate error
  898. NOTHING;
  899. } else if (fdoExtension->DiskGeometry.MediaType == F3_120M_512) {
  900. //so that the format code will not try to partition it.
  901. status = STATUS_INVALID_DEVICE_REQUEST;
  902. } else {
  903. //
  904. // Free the Srb, since it is not needed.
  905. //
  906. ExFreePool(srb);
  907. //
  908. // Pass the request to the common device control routine.
  909. //
  910. return(ClassDeviceControl(DeviceObject, Irp));
  911. }
  912. break;
  913. }
  914. case IOCTL_DISK_GET_DRIVE_GEOMETRY: {
  915. DebugPrint((3,"ScsiDeviceIoControl: Get drive geometry\n"));
  916. if (fdoExtension->AdapterDescriptor->BusType == BusTypeUsb)
  917. {
  918. status = USBFlopGetMediaTypes(DeviceObject,
  919. Irp);
  920. break;
  921. }
  922. //
  923. // If there's not enough room to write the
  924. // data, then fail the request.
  925. //
  926. if ( irpStack->Parameters.DeviceIoControl.OutputBufferLength <
  927. sizeof( DISK_GEOMETRY ) ) {
  928. status = STATUS_INVALID_PARAMETER;
  929. break;
  930. }
  931. status = DetermineMediaType(DeviceObject);
  932. if (!NT_SUCCESS(status)) {
  933. Irp->IoStatus.Information = 0;
  934. Irp->IoStatus.Status = status;
  935. } else {
  936. //
  937. // Copy drive geometry information from device extension.
  938. //
  939. RtlMoveMemory(Irp->AssociatedIrp.SystemBuffer,
  940. &(fdoExtension->DiskGeometry),
  941. sizeof(DISK_GEOMETRY));
  942. Irp->IoStatus.Information = sizeof(DISK_GEOMETRY);
  943. status = STATUS_SUCCESS;
  944. }
  945. break;
  946. }
  947. case IOCTL_DISK_GET_MEDIA_TYPES: {
  948. if (fdoExtension->AdapterDescriptor->BusType == BusTypeUsb)
  949. {
  950. status = USBFlopGetMediaTypes(DeviceObject,
  951. Irp);
  952. break;
  953. }
  954. i = DetermineDriveType(DeviceObject);
  955. if (i == DRIVE_TYPE_NONE) {
  956. status = STATUS_UNRECOGNIZED_MEDIA;
  957. break;
  958. }
  959. lowestDriveMediaType = DriveMediaLimits[i].LowestDriveMediaType;
  960. highestDriveMediaType = DriveMediaLimits[i].HighestDriveMediaType;
  961. outputBufferLength =
  962. irpStack->Parameters.DeviceIoControl.OutputBufferLength;
  963. //
  964. // Make sure that the input buffer has enough room to return
  965. // at least one descriptions of a supported media type.
  966. //
  967. if ( outputBufferLength < ( sizeof( DISK_GEOMETRY ) ) ) {
  968. status = STATUS_BUFFER_TOO_SMALL;
  969. break;
  970. }
  971. //
  972. // Assume success, although we might modify it to a buffer
  973. // overflow warning below (if the buffer isn't big enough
  974. // to hold ALL of the media descriptions).
  975. //
  976. status = STATUS_SUCCESS;
  977. if (outputBufferLength < ( sizeof( DISK_GEOMETRY ) *
  978. ( highestDriveMediaType - lowestDriveMediaType + 1 ) ) ) {
  979. //
  980. // The buffer is too small for all of the descriptions;
  981. // calculate what CAN fit in the buffer.
  982. //
  983. status = STATUS_BUFFER_OVERFLOW;
  984. highestDriveMediaType = (DRIVE_MEDIA_TYPE)( ( lowestDriveMediaType - 1 ) +
  985. ( outputBufferLength /
  986. sizeof( DISK_GEOMETRY ) ) );
  987. }
  988. outputBuffer = (PDISK_GEOMETRY) Irp->AssociatedIrp.SystemBuffer;
  989. for (i = (UCHAR)lowestDriveMediaType;i <= (UCHAR)highestDriveMediaType;i++ ) {
  990. outputBuffer->MediaType = DriveMediaConstants[i].MediaType;
  991. outputBuffer->Cylinders.LowPart =
  992. DriveMediaConstants[i].MaximumTrack + 1;
  993. outputBuffer->Cylinders.HighPart = 0;
  994. outputBuffer->TracksPerCylinder =
  995. DriveMediaConstants[i].NumberOfHeads;
  996. outputBuffer->SectorsPerTrack =
  997. DriveMediaConstants[i].SectorsPerTrack;
  998. outputBuffer->BytesPerSector =
  999. DriveMediaConstants[i].BytesPerSector;
  1000. outputBuffer++;
  1001. Irp->IoStatus.Information += sizeof( DISK_GEOMETRY );
  1002. }
  1003. break;
  1004. }
  1005. case IOCTL_DISK_FORMAT_TRACKS: {
  1006. if (fdoExtension->AdapterDescriptor->BusType == BusTypeUsb)
  1007. {
  1008. status = USBFlopFormatTracks(DeviceObject,
  1009. Irp);
  1010. break;
  1011. }
  1012. //
  1013. // Make sure that we got all the necessary format parameters.
  1014. //
  1015. if ( irpStack->Parameters.DeviceIoControl.InputBufferLength <sizeof( FORMAT_PARAMETERS ) ) {
  1016. status = STATUS_INVALID_PARAMETER;
  1017. break;
  1018. }
  1019. formatParameters = (PFORMAT_PARAMETERS) Irp->AssociatedIrp.SystemBuffer;
  1020. //
  1021. // Make sure the parameters we got are reasonable.
  1022. //
  1023. if ( !FlCheckFormatParameters(DeviceObject, formatParameters)) {
  1024. status = STATUS_INVALID_PARAMETER;
  1025. break;
  1026. }
  1027. //
  1028. // If this request is for a 20.8 MB floppy then call a special
  1029. // floppy format routine.
  1030. //
  1031. if (formatParameters->MediaType == F3_20Pt8_512) {
  1032. status = FlopticalFormatMedia(DeviceObject,
  1033. formatParameters
  1034. );
  1035. break;
  1036. }
  1037. //
  1038. // All the work is done in the pass. If this is not the first pass,
  1039. // then complete the request and return;
  1040. //
  1041. if (formatParameters->StartCylinderNumber != 0 || formatParameters->StartHeadNumber != 0) {
  1042. status = STATUS_SUCCESS;
  1043. break;
  1044. }
  1045. status = FormatMedia( DeviceObject, formatParameters->MediaType);
  1046. break;
  1047. }
  1048. case IOCTL_DISK_IS_WRITABLE: {
  1049. //
  1050. // Determine if the device is writable.
  1051. //
  1052. modeData = ExAllocatePool(NonPagedPoolCacheAligned, MODE_DATA_SIZE);
  1053. if (modeData == NULL) {
  1054. status = STATUS_INSUFFICIENT_RESOURCES;
  1055. break;
  1056. }
  1057. RtlZeroMemory(modeData, MODE_DATA_SIZE);
  1058. length = ClassModeSense(DeviceObject,
  1059. (PUCHAR) modeData,
  1060. MODE_DATA_SIZE,
  1061. MODE_SENSE_RETURN_ALL);
  1062. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1063. //
  1064. // Retry the request in case of a check condition.
  1065. //
  1066. length = ClassModeSense(DeviceObject,
  1067. (PUCHAR) modeData,
  1068. MODE_DATA_SIZE,
  1069. MODE_SENSE_RETURN_ALL);
  1070. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1071. status = STATUS_IO_DEVICE_ERROR;
  1072. ExFreePool(modeData);
  1073. break;
  1074. }
  1075. }
  1076. if (modeData->DeviceSpecificParameter & MODE_DSP_WRITE_PROTECT) {
  1077. status = STATUS_MEDIA_WRITE_PROTECTED;
  1078. } else {
  1079. status = STATUS_SUCCESS;
  1080. }
  1081. DebugPrint((2,"IOCTL_DISK_IS_WRITABLE returns %08X\n", status));
  1082. ExFreePool(modeData);
  1083. break;
  1084. }
  1085. default: {
  1086. DebugPrint((3,"ScsiIoDeviceControl: Unsupported device IOCTL\n"));
  1087. //
  1088. // Free the Srb, since it is not needed.
  1089. //
  1090. ExFreePool(srb);
  1091. //
  1092. // Pass the request to the common device control routine.
  1093. //
  1094. return(ClassDeviceControl(DeviceObject, Irp));
  1095. break;
  1096. }
  1097. } // end switch( ...
  1098. //
  1099. // Check if SL_OVERRIDE_VERIFY_VOLUME flag is set in the IRP.
  1100. // If so, do not return STATUS_VERIFY_REQUIRED
  1101. //
  1102. if ((status == STATUS_VERIFY_REQUIRED) &&
  1103. (TEST_FLAG(irpStack->Flags, SL_OVERRIDE_VERIFY_VOLUME))) {
  1104. status = STATUS_IO_DEVICE_ERROR;
  1105. }
  1106. Irp->IoStatus.Status = status;
  1107. if (!NT_SUCCESS(status) && IoIsErrorUserInduced(status)) {
  1108. IoSetHardErrorOrVerifyDevice(Irp, DeviceObject);
  1109. }
  1110. KeRaiseIrql(DISPATCH_LEVEL, &currentIrql);
  1111. ClassReleaseRemoveLock(DeviceObject, Irp);
  1112. ClassCompleteRequest(DeviceObject, Irp, 0);
  1113. KeLowerIrql(currentIrql);
  1114. ExFreePool(srb);
  1115. return status;
  1116. } // end ScsiFlopDeviceControl()
  1117. #if 0
  1118. BOOLEAN
  1119. IsFloppyDevice(
  1120. PDEVICE_OBJECT DeviceObject
  1121. )
  1122. /*++
  1123. Routine Description:
  1124. The routine performs the necessary funcitons to deterime if the device is
  1125. really a floppy rather than a harddisk. This is done by a mode sense
  1126. command. First a check is made to see if the medimum type is set. Second
  1127. a check is made for the flexible parameters mode page.
  1128. Arguments:
  1129. DeviceObject - Supplies the device object to be tested.
  1130. Return Value:
  1131. Return TRUE if the indicated device is a floppy.
  1132. --*/
  1133. {
  1134. PVOID modeData;
  1135. PUCHAR pageData;
  1136. ULONG length;
  1137. modeData = ExAllocatePool(NonPagedPoolCacheAligned, MODE_DATA_SIZE);
  1138. if (modeData == NULL) {
  1139. return(FALSE);
  1140. }
  1141. RtlZeroMemory(modeData, MODE_DATA_SIZE);
  1142. length = ClassModeSense(DeviceObject, modeData, MODE_DATA_SIZE, MODE_SENSE_RETURN_ALL);
  1143. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1144. //
  1145. // Retry the request in case of a check condition.
  1146. //
  1147. length = ClassModeSense(DeviceObject,
  1148. modeData,
  1149. MODE_DATA_SIZE,
  1150. MODE_SENSE_RETURN_ALL);
  1151. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1152. ExFreePool(modeData);
  1153. return(FALSE);
  1154. }
  1155. }
  1156. #if 0
  1157. //
  1158. // Some drives incorrectly report this. In particular the SONY RMO-S350
  1159. // when in disk mode.
  1160. //
  1161. if (((PMODE_PARAMETER_HEADER) modeData)->MediumType >= MODE_FD_SINGLE_SIDE
  1162. && ((PMODE_PARAMETER_HEADER) modeData)->MediumType <= MODE_FD_MAXIMUM_TYPE) {
  1163. DebugPrint((1, "ScsiFlop: MediumType value %2x, This is a floppy.\n", ((PMODE_PARAMETER_HEADER) modeData)->MediumType));
  1164. ExFreePool(modeData);
  1165. return(TRUE);
  1166. }
  1167. #endif
  1168. //
  1169. // If the length is greater than length indiated by the mode data reset
  1170. // the data to the mode data.
  1171. //
  1172. if (length > (ULONG)((PMODE_PARAMETER_HEADER) modeData)->ModeDataLength + 1) {
  1173. length = (ULONG)((PMODE_PARAMETER_HEADER) modeData)->ModeDataLength + 1;
  1174. }
  1175. //
  1176. // Look for the flexible disk mode page.
  1177. //
  1178. pageData = ClassFindModePage( modeData, length, MODE_PAGE_FLEXIBILE, TRUE);
  1179. if (pageData != NULL) {
  1180. DebugPrint((1, "ScsiFlop: Flexible disk page found, This is a floppy.\n"));
  1181. //
  1182. // As a special case for the floptical driver do a magic mode sense to
  1183. // enable the drive.
  1184. //
  1185. ClassModeSense(DeviceObject, modeData, 0x2a, 0x2e);
  1186. ExFreePool(modeData);
  1187. return(TRUE);
  1188. }
  1189. ExFreePool(modeData);
  1190. return(FALSE);
  1191. }
  1192. #endif
  1193. NTSTATUS
  1194. DetermineMediaType(
  1195. PDEVICE_OBJECT DeviceObject
  1196. )
  1197. /*++
  1198. Routine Description:
  1199. This routine determines the floppy media type based on the size of the
  1200. device. The geometry information is set for the device object.
  1201. Arguments:
  1202. DeviceObject - Supplies the device object to be tested.
  1203. Return Value:
  1204. None
  1205. --*/
  1206. {
  1207. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1208. PDISK_GEOMETRY geometry;
  1209. LONG index;
  1210. NTSTATUS status;
  1211. geometry = &(fdoExtension->DiskGeometry);
  1212. //
  1213. // Issue ReadCapacity to update device extension
  1214. // with information for current media.
  1215. //
  1216. status = ClassReadDriveCapacity(DeviceObject);
  1217. if (!NT_SUCCESS(status)) {
  1218. //
  1219. // Set the media type to unknow and zero the geometry information.
  1220. //
  1221. geometry->MediaType = Unknown;
  1222. return status;
  1223. }
  1224. //
  1225. // Look at the capcity of disk to determine its type.
  1226. //
  1227. for (index = NUMBER_OF_DRIVE_MEDIA_COMBINATIONS - 1; index >= 0; index--) {
  1228. //
  1229. // Walk the table backward untill the drive capacity holds all of the
  1230. // data and the bytes per setor are equal
  1231. //
  1232. if ((ULONG) (DriveMediaConstants[index].NumberOfHeads *
  1233. (DriveMediaConstants[index].MaximumTrack + 1) *
  1234. DriveMediaConstants[index].SectorsPerTrack *
  1235. DriveMediaConstants[index].BytesPerSector) <=
  1236. fdoExtension->CommonExtension.PartitionLength.LowPart &&
  1237. DriveMediaConstants[index].BytesPerSector ==
  1238. geometry->BytesPerSector) {
  1239. geometry->MediaType = DriveMediaConstants[index].MediaType;
  1240. geometry->TracksPerCylinder = DriveMediaConstants[index].NumberOfHeads;
  1241. geometry->SectorsPerTrack = DriveMediaConstants[index].SectorsPerTrack;
  1242. geometry->Cylinders.LowPart = DriveMediaConstants[index].MaximumTrack+1;
  1243. break;
  1244. }
  1245. }
  1246. if (index == -1) {
  1247. //
  1248. // Set the media type to unknow and zero the geometry information.
  1249. //
  1250. geometry->MediaType = Unknown;
  1251. } else {
  1252. //
  1253. // DMF check breaks the insight SCSI floppy, so its disabled for that case
  1254. //
  1255. PDISK_DATA diskData = (PDISK_DATA) fdoExtension->CommonExtension.DriverData;
  1256. // if (diskData->EnableDMF == TRUE) {
  1257. //
  1258. //check to see if DMF
  1259. //
  1260. PSCSI_REQUEST_BLOCK srb;
  1261. PVOID readData;
  1262. //
  1263. // Allocate a Srb for the read command.
  1264. //
  1265. readData = ExAllocatePool(NonPagedPool, geometry->BytesPerSector);
  1266. if (readData == NULL) {
  1267. return STATUS_NO_MEMORY;
  1268. }
  1269. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  1270. if (srb == NULL) {
  1271. ExFreePool(readData);
  1272. return STATUS_NO_MEMORY;
  1273. }
  1274. RtlZeroMemory(readData, geometry->BytesPerSector);
  1275. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1276. srb->CdbLength = 10;
  1277. srb->Cdb[0] = SCSIOP_READ;
  1278. srb->Cdb[5] = 0;
  1279. srb->Cdb[8] = (UCHAR) 1;
  1280. //
  1281. // Set timeout value.
  1282. //
  1283. srb->TimeOutValue = fdoExtension->TimeOutValue;
  1284. //
  1285. // Send the mode select data.
  1286. //
  1287. status = ClassSendSrbSynchronous(DeviceObject,
  1288. srb,
  1289. readData,
  1290. geometry->BytesPerSector,
  1291. FALSE
  1292. );
  1293. if (NT_SUCCESS(status)) {
  1294. char *pchar = (char *)readData;
  1295. pchar += 3; //skip 3 bytes jump code
  1296. // If the MSDMF3. signature is there then mark it as DMF diskette
  1297. if (RtlCompareMemory(pchar, "MSDMF3.", 7) == 7) {
  1298. diskData->IsDMF = TRUE;
  1299. }
  1300. }
  1301. ExFreePool(readData);
  1302. ExFreePool(srb);
  1303. // }// else
  1304. }
  1305. return status;
  1306. }
  1307. ULONG
  1308. DetermineDriveType(
  1309. PDEVICE_OBJECT DeviceObject
  1310. )
  1311. /*++
  1312. Routine Description:
  1313. The routine determines the device type so that the supported medias can be
  1314. determined. It does a mode sense for the default parameters. This code
  1315. assumes that the returned values are for the maximum device size.
  1316. Arguments:
  1317. DeviceObject - Supplies the device object to be tested.
  1318. Return Value:
  1319. None
  1320. --*/
  1321. {
  1322. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1323. PVOID modeData;
  1324. PDISK_DATA diskData = (PDISK_DATA) fdoExtension->CommonExtension.DriverData;
  1325. PMODE_FLEXIBLE_DISK_PAGE pageData;
  1326. ULONG length;
  1327. LONG index;
  1328. UCHAR numberOfHeads;
  1329. UCHAR sectorsPerTrack;
  1330. USHORT maximumTrack;
  1331. BOOLEAN applyFix = FALSE;
  1332. if (diskData->DriveType != DRIVE_TYPE_NONE) {
  1333. return(diskData->DriveType);
  1334. }
  1335. modeData = ExAllocatePool(NonPagedPoolCacheAligned, MODE_DATA_SIZE);
  1336. if (modeData == NULL) {
  1337. return(DRIVE_TYPE_NONE);
  1338. }
  1339. RtlZeroMemory(modeData, MODE_DATA_SIZE);
  1340. length = ClassModeSense(DeviceObject,
  1341. modeData,
  1342. MODE_DATA_SIZE,
  1343. MODE_PAGE_FLEXIBILE);
  1344. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1345. RtlZeroMemory(modeData, MODE_DATA_SIZE);
  1346. //
  1347. // Retry the request one more time
  1348. // in case of a check condition.
  1349. //
  1350. length = ClassModeSense(DeviceObject,
  1351. modeData,
  1352. MODE_DATA_SIZE,
  1353. MODE_PAGE_FLEXIBILE);
  1354. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1355. ExFreePool(modeData);
  1356. return(DRIVE_TYPE_NONE);
  1357. }
  1358. }
  1359. //
  1360. // Look for the flexible disk mode page.
  1361. //
  1362. pageData = ClassFindModePage( modeData,
  1363. length,
  1364. MODE_PAGE_FLEXIBILE,
  1365. TRUE);
  1366. //
  1367. // Make sure the page is returned and is large enough.
  1368. //
  1369. if ((pageData != NULL) &&
  1370. (pageData->PageLength + 2 >=
  1371. offsetof(MODE_FLEXIBLE_DISK_PAGE, StartWritePrecom))) {
  1372. //
  1373. // Pull out the heads, cylinders, and sectors.
  1374. //
  1375. numberOfHeads = pageData->NumberOfHeads;
  1376. maximumTrack = pageData->NumberOfCylinders[1];
  1377. maximumTrack |= pageData->NumberOfCylinders[0] << 8;
  1378. sectorsPerTrack = pageData->SectorsPerTrack;
  1379. //
  1380. // Convert from number of cylinders to maximum track.
  1381. //
  1382. maximumTrack--;
  1383. //
  1384. // Search for the maximum supported media. Based on the number of heads,
  1385. // sectors per track and number of cylinders
  1386. //
  1387. for (index = 0; index < NUMBER_OF_DRIVE_MEDIA_COMBINATIONS; index++) {
  1388. //
  1389. // Walk the table forward until the drive capacity holds all of the
  1390. // data and the bytes per setor are equal
  1391. //
  1392. if (DriveMediaConstants[index].NumberOfHeads == numberOfHeads &&
  1393. DriveMediaConstants[index].MaximumTrack == maximumTrack &&
  1394. DriveMediaConstants[index].SectorsPerTrack ==sectorsPerTrack) {
  1395. ExFreePool(modeData);
  1396. //
  1397. // index is now a drive media combination. Compare this to
  1398. // the maximum drive media type in the drive media table.
  1399. //
  1400. for (length = 0; length < NUMBER_OF_DRIVE_TYPES; length++) {
  1401. if (DriveMediaLimits[length].HighestDriveMediaType == index) {
  1402. return(length);
  1403. }
  1404. }
  1405. return(DRIVE_TYPE_NONE);
  1406. }
  1407. }
  1408. // If the maximum track is greater than 8 bits then divide the
  1409. // number of tracks by 3 and multiply the number of heads by 3.
  1410. // This is a special case for the 20.8 MB floppy.
  1411. //
  1412. if (!applyFix && maximumTrack >= 0x0100) {
  1413. applyFix = TRUE;
  1414. maximumTrack++;
  1415. maximumTrack /= 3;
  1416. maximumTrack--;
  1417. numberOfHeads *= 3;
  1418. } else {
  1419. ExFreePool(modeData);
  1420. return(DRIVE_TYPE_NONE);
  1421. }
  1422. }
  1423. ExFreePool(modeData);
  1424. return(DRIVE_TYPE_NONE);
  1425. }
  1426. BOOLEAN
  1427. FlCheckFormatParameters(
  1428. IN PDEVICE_OBJECT DeviceObject,
  1429. IN PFORMAT_PARAMETERS FormatParameters
  1430. )
  1431. /*++
  1432. Routine Description:
  1433. This routine checks the supplied format parameters to make sure that
  1434. they'll work on the drive to be formatted.
  1435. Arguments:
  1436. DeviceObject - Pointer to the device object to be formated.
  1437. FormatParameters - a pointer to the caller's parameters for the FORMAT.
  1438. Return Value:
  1439. TRUE if parameters are OK.
  1440. FALSE if the parameters are bad.
  1441. --*/
  1442. {
  1443. PDRIVE_MEDIA_CONSTANTS driveMediaConstants;
  1444. DRIVE_MEDIA_TYPE driveMediaType;
  1445. ULONG index;
  1446. //
  1447. // Get the device type.
  1448. //
  1449. index = DetermineDriveType(DeviceObject);
  1450. if (index == DRIVE_TYPE_NONE) {
  1451. //
  1452. // If the determine device type failed then just use the media type
  1453. // and try the parameters.
  1454. //
  1455. driveMediaType = Drive360Media160;
  1456. while (( DriveMediaConstants[driveMediaType].MediaType !=
  1457. FormatParameters->MediaType ) &&
  1458. ( driveMediaType < Drive288Media288) ) {
  1459. driveMediaType++;
  1460. }
  1461. } else {
  1462. //
  1463. // Figure out which entry in the DriveMediaConstants table to use.
  1464. //
  1465. driveMediaType =
  1466. DriveMediaLimits[index].HighestDriveMediaType;
  1467. while ( ( DriveMediaConstants[driveMediaType].MediaType !=
  1468. FormatParameters->MediaType ) &&
  1469. ( driveMediaType > DriveMediaLimits[index].
  1470. LowestDriveMediaType ) ) {
  1471. driveMediaType--;
  1472. }
  1473. }
  1474. if ( DriveMediaConstants[driveMediaType].MediaType !=
  1475. FormatParameters->MediaType ) {
  1476. return FALSE;
  1477. } else {
  1478. driveMediaConstants = &DriveMediaConstants[driveMediaType];
  1479. if ( ( FormatParameters->StartHeadNumber >
  1480. (ULONG)( driveMediaConstants->NumberOfHeads - 1 ) ) ||
  1481. ( FormatParameters->EndHeadNumber >
  1482. (ULONG)( driveMediaConstants->NumberOfHeads - 1 ) ) ||
  1483. ( FormatParameters->StartCylinderNumber >
  1484. driveMediaConstants->MaximumTrack ) ||
  1485. ( FormatParameters->EndCylinderNumber >
  1486. driveMediaConstants->MaximumTrack ) ||
  1487. ( FormatParameters->EndCylinderNumber <
  1488. FormatParameters->StartCylinderNumber ) ) {
  1489. return FALSE;
  1490. } else {
  1491. return TRUE;
  1492. }
  1493. }
  1494. }
  1495. NTSTATUS
  1496. FormatMedia(
  1497. PDEVICE_OBJECT DeviceObject,
  1498. MEDIA_TYPE MediaType
  1499. )
  1500. /*++
  1501. Routine Description:
  1502. This routine formats the floppy disk. The entire floppy is formated in
  1503. one shot.
  1504. Arguments:
  1505. DeviceObject - Supplies the device object to be tested.
  1506. Irp - Supplies a pointer to the requesting Irp.
  1507. MediaType - Supplies the media type format the device for.
  1508. Return Value:
  1509. Returns a status for the operation.
  1510. --*/
  1511. {
  1512. PVOID modeData;
  1513. PSCSI_REQUEST_BLOCK srb;
  1514. PMODE_FLEXIBLE_DISK_PAGE pageData;
  1515. DRIVE_MEDIA_TYPE driveMediaType;
  1516. PDRIVE_MEDIA_CONSTANTS driveMediaConstants;
  1517. ULONG length;
  1518. NTSTATUS status;
  1519. modeData = ExAllocatePool(NonPagedPoolCacheAligned, MODE_DATA_SIZE);
  1520. if (modeData == NULL) {
  1521. return(STATUS_INSUFFICIENT_RESOURCES);
  1522. }
  1523. RtlZeroMemory(modeData, MODE_DATA_SIZE);
  1524. length = ClassModeSense(DeviceObject,
  1525. modeData,
  1526. MODE_DATA_SIZE,
  1527. MODE_PAGE_FLEXIBILE);
  1528. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1529. ExFreePool(modeData);
  1530. return(STATUS_INVALID_DEVICE_REQUEST);
  1531. }
  1532. //
  1533. // Look for the flexible disk mode page.
  1534. //
  1535. pageData = ClassFindModePage( modeData, length, MODE_PAGE_FLEXIBILE, TRUE);
  1536. //
  1537. // Make sure the page is returned and is large enough.
  1538. //
  1539. if ((pageData == NULL) ||
  1540. (pageData->PageLength + 2 <
  1541. offsetof(MODE_FLEXIBLE_DISK_PAGE, StartWritePrecom))) {
  1542. ExFreePool(modeData);
  1543. return(STATUS_INVALID_DEVICE_REQUEST);
  1544. }
  1545. //
  1546. // Look for a drive media type which matches the requested media type.
  1547. //
  1548. //
  1549. //start from Drive120MMedia120M instead of Drive2080Media2080
  1550. //
  1551. for (driveMediaType = Drive120MMedia120M;
  1552. DriveMediaConstants[driveMediaType].MediaType != MediaType;
  1553. driveMediaType--) {
  1554. if (driveMediaType == Drive360Media160) {
  1555. ExFreePool(modeData);
  1556. return(STATUS_INVALID_PARAMETER);
  1557. }
  1558. }
  1559. driveMediaConstants = &DriveMediaConstants[driveMediaType];
  1560. if ((pageData->NumberOfHeads != driveMediaConstants->NumberOfHeads) ||
  1561. (pageData->SectorsPerTrack != driveMediaConstants->SectorsPerTrack) ||
  1562. ((pageData->NumberOfCylinders[0] != (UCHAR)(driveMediaConstants->MaximumTrack+1) >> 8) &&
  1563. (pageData->NumberOfCylinders[1] != (UCHAR)driveMediaConstants->MaximumTrack+1)) ||
  1564. (pageData->BytesPerSector[0] != driveMediaConstants->BytesPerSector >> 8 )) {
  1565. //
  1566. // Update the flexible parameters page with the new parameters.
  1567. //
  1568. pageData->NumberOfHeads = driveMediaConstants->NumberOfHeads;
  1569. pageData->SectorsPerTrack = driveMediaConstants->SectorsPerTrack;
  1570. pageData->NumberOfCylinders[0] = (UCHAR)(driveMediaConstants->MaximumTrack+1) >> 8;
  1571. pageData->NumberOfCylinders[1] = (UCHAR)driveMediaConstants->MaximumTrack+1;
  1572. pageData->BytesPerSector[0] = driveMediaConstants->BytesPerSector >> 8;
  1573. //
  1574. // Clear the mode parameter header.
  1575. //
  1576. RtlZeroMemory(modeData, sizeof(MODE_PARAMETER_HEADER));
  1577. //
  1578. // Set the length equal to the length returned for the flexible page.
  1579. //
  1580. length = pageData->PageLength + 2;
  1581. //
  1582. // Copy the page after the mode parameter header.
  1583. //
  1584. RtlMoveMemory((PCHAR) modeData + sizeof(MODE_PARAMETER_HEADER),
  1585. pageData,
  1586. length
  1587. );
  1588. length += sizeof(MODE_PARAMETER_HEADER);
  1589. //
  1590. // Allocate a Srb for the format command.
  1591. //
  1592. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  1593. if (srb == NULL) {
  1594. ExFreePool(modeData);
  1595. return(STATUS_INSUFFICIENT_RESOURCES);
  1596. }
  1597. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1598. srb->CdbLength = 6;
  1599. srb->Cdb[0] = SCSIOP_MODE_SELECT;
  1600. srb->Cdb[4] = (UCHAR) length;
  1601. //
  1602. // Set the PF bit.
  1603. //
  1604. srb->Cdb[1] |= 0x10;
  1605. //
  1606. // Set timeout value.
  1607. //
  1608. srb->TimeOutValue = 2;
  1609. //
  1610. // Send the mode select data.
  1611. //
  1612. status = ClassSendSrbSynchronous(DeviceObject,
  1613. srb,
  1614. modeData,
  1615. length,
  1616. TRUE
  1617. );
  1618. //
  1619. // The mode data not needed any more so free it.
  1620. //
  1621. ExFreePool(modeData);
  1622. if (!NT_SUCCESS(status)) {
  1623. ExFreePool(srb);
  1624. return(status);
  1625. }
  1626. } else {
  1627. //
  1628. // The mode data not needed any more so free it.
  1629. //
  1630. ExFreePool(modeData);
  1631. //
  1632. // Allocate a Srb for the format command.
  1633. //
  1634. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  1635. if (srb == NULL) {
  1636. return(STATUS_INSUFFICIENT_RESOURCES);
  1637. }
  1638. }
  1639. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1640. srb->CdbLength = 6;
  1641. srb->Cdb[0] = SCSIOP_FORMAT_UNIT;
  1642. //
  1643. // Set timeout value.
  1644. //
  1645. srb->TimeOutValue = 10 * 60;
  1646. status = ClassSendSrbSynchronous(DeviceObject,
  1647. srb,
  1648. NULL,
  1649. 0,
  1650. FALSE
  1651. );
  1652. ExFreePool(srb);
  1653. return(status);
  1654. }
  1655. VOID
  1656. ScsiFlopProcessError(
  1657. PDEVICE_OBJECT DeviceObject,
  1658. PSCSI_REQUEST_BLOCK Srb,
  1659. NTSTATUS *Status,
  1660. BOOLEAN *Retry
  1661. )
  1662. /*++
  1663. Routine Description:
  1664. This routine checks the type of error. If the error indicate the floppy
  1665. controller needs to be reinitialize a command is made to do it.
  1666. Arguments:
  1667. DeviceObject - Supplies a pointer to the device object.
  1668. Srb - Supplies a pointer to the failing Srb.
  1669. Status - Status with which the IRP will be completed.
  1670. Retry - Indication of whether the request will be retried.
  1671. Return Value:
  1672. None.
  1673. --*/
  1674. {
  1675. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1676. PDISK_DATA diskData = (PDISK_DATA) fdoExtension->CommonExtension.DriverData;
  1677. PSENSE_DATA senseBuffer = Srb->SenseInfoBuffer;
  1678. PIO_STACK_LOCATION irpStack;
  1679. PIRP irp;
  1680. PSCSI_REQUEST_BLOCK srb;
  1681. LARGE_INTEGER largeInt;
  1682. PCOMPLETION_CONTEXT context;
  1683. PCDB cdb;
  1684. ULONG_PTR alignment;
  1685. ULONG majorFunction;
  1686. UNREFERENCED_PARAMETER(Status);
  1687. UNREFERENCED_PARAMETER(Retry);
  1688. largeInt.QuadPart = 1;
  1689. //
  1690. // Check the status. The initialization command only needs to be sent
  1691. // if UNIT ATTENTION or LUN NOT READY is returned.
  1692. //
  1693. if (!(Srb->SrbStatus & SRB_STATUS_AUTOSENSE_VALID)) {
  1694. //
  1695. // The drive does not require reinitialization.
  1696. //
  1697. return;
  1698. }
  1699. //
  1700. // Reset the drive type.
  1701. //
  1702. diskData->DriveType = DRIVE_TYPE_NONE;
  1703. diskData->IsDMF = FALSE;
  1704. fdoExtension->DiskGeometry.MediaType = Unknown;
  1705. if (fdoExtension->AdapterDescriptor->BusType == BusTypeUsb) {
  1706. // FLPYDISK.SYS never returns a non-zero value for the ChangeCount
  1707. // on an IOCTL_DISK_CHECK_VERIFY. Some things seem to work better
  1708. // if we do the same. In particular, FatVerifyVolume() can exit between
  1709. // the IOCTL_DISK_CHECK_VERIFY and the IOCTL_DISK_GET_DRIVE_GEOMETRY
  1710. // if a non-zero ChangeCount is returned, and this appears to cause
  1711. // issues formatting unformatted media in some situations.
  1712. //
  1713. // This is something that should probably be revisited at some point.
  1714. //
  1715. fdoExtension->MediaChangeCount = 0;
  1716. if (((senseBuffer->SenseKey & 0xf) == SCSI_SENSE_UNIT_ATTENTION) &&
  1717. (senseBuffer->AdditionalSenseCode == SCSI_ADSENSE_MEDIUM_CHANGED)) {
  1718. struct _START_STOP *cdb;
  1719. DebugPrint((2,"Sending SCSIOP_START_STOP_UNIT\n"));
  1720. context = ExAllocatePool(NonPagedPool,
  1721. sizeof(COMPLETION_CONTEXT));
  1722. if (context == NULL) {
  1723. return;
  1724. }
  1725. srb = &context->Srb;
  1726. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1727. srb->SrbFlags = SRB_FLAGS_DISABLE_AUTOSENSE;
  1728. srb->CdbLength = 6;
  1729. cdb = (struct _START_STOP *)srb->Cdb;
  1730. cdb->OperationCode = SCSIOP_START_STOP_UNIT;
  1731. cdb->Start = 1;
  1732. // A Start Stop Unit request has no transfer buffer.
  1733. // Set the request to IRP_MJ_FLUSH_BUFFERS when calling
  1734. // IoBuildAsynchronousFsdRequest() so that it ignores
  1735. // the buffer pointer and buffer length parameters.
  1736. //
  1737. majorFunction = IRP_MJ_FLUSH_BUFFERS;
  1738. } else if ((senseBuffer->SenseKey & 0xf) == SCSI_SENSE_MEDIUM_ERROR) {
  1739. // Return ERROR_UNRECOGNIZED_MEDIA instead of
  1740. // STATUS_DEVICE_DATA_ERROR to make shell happy.
  1741. //
  1742. *Status = STATUS_UNRECOGNIZED_MEDIA;
  1743. return;
  1744. } else {
  1745. return;
  1746. }
  1747. } else if (((senseBuffer->SenseKey & 0xf) == SCSI_SENSE_NOT_READY) &&
  1748. senseBuffer->AdditionalSenseCodeQualifier == SCSI_SENSEQ_INIT_COMMAND_REQUIRED ||
  1749. (senseBuffer->SenseKey & 0xf) == SCSI_SENSE_UNIT_ATTENTION) {
  1750. DebugPrint((1, "ScsiFlopProcessError: Reinitializing the floppy.\n"));
  1751. //
  1752. // Send the special mode sense command to enable writes on the
  1753. // floptical drive.
  1754. //
  1755. alignment = DeviceObject->AlignmentRequirement ?
  1756. DeviceObject->AlignmentRequirement : 1;
  1757. context = ExAllocatePool(
  1758. NonPagedPool,
  1759. sizeof(COMPLETION_CONTEXT) + 0x2a + (ULONG)alignment
  1760. );
  1761. if (context == NULL) {
  1762. //
  1763. // If there is not enough memory to fulfill this request,
  1764. // simply return. A subsequent retry will fail and another
  1765. // chance to start the unit.
  1766. //
  1767. return;
  1768. }
  1769. srb = &context->Srb;
  1770. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1771. //
  1772. // Set the transfer length.
  1773. //
  1774. srb->DataTransferLength = 0x2a;
  1775. srb->SrbFlags = SRB_FLAGS_DATA_IN | SRB_FLAGS_DISABLE_AUTOSENSE | SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  1776. //
  1777. // The data buffer must be aligned.
  1778. //
  1779. srb->DataBuffer = (PVOID) (((ULONG_PTR) (context + 1) + (alignment - 1)) &
  1780. ~(alignment - 1));
  1781. //
  1782. // Build the start unit CDB.
  1783. //
  1784. srb->CdbLength = 6;
  1785. cdb = (PCDB)srb->Cdb;
  1786. cdb->MODE_SENSE.LogicalUnitNumber = srb->Lun;
  1787. cdb->MODE_SENSE.OperationCode = SCSIOP_MODE_SENSE;
  1788. cdb->MODE_SENSE.PageCode = 0x2e;
  1789. cdb->MODE_SENSE.AllocationLength = 0x2a;
  1790. majorFunction = IRP_MJ_READ;
  1791. } else {
  1792. return;
  1793. }
  1794. context->DeviceObject = DeviceObject;
  1795. //
  1796. // Write length to SRB.
  1797. //
  1798. srb->Length = SCSI_REQUEST_BLOCK_SIZE;
  1799. srb->Function = SRB_FUNCTION_EXECUTE_SCSI;
  1800. srb->TimeOutValue = fdoExtension->TimeOutValue;
  1801. //
  1802. // Build the asynchronous request
  1803. // to be sent to the port driver.
  1804. //
  1805. irp = IoBuildAsynchronousFsdRequest(majorFunction,
  1806. DeviceObject,
  1807. srb->DataBuffer,
  1808. srb->DataTransferLength,
  1809. &largeInt,
  1810. NULL);
  1811. if(irp == NULL) {
  1812. ExFreePool(context);
  1813. return;
  1814. }
  1815. IoSetCompletionRoutine(irp,
  1816. (PIO_COMPLETION_ROUTINE)ClassAsynchronousCompletion,
  1817. context,
  1818. TRUE,
  1819. TRUE,
  1820. TRUE);
  1821. ClassAcquireRemoveLock(DeviceObject, irp);
  1822. irpStack = IoGetNextIrpStackLocation(irp);
  1823. irpStack->MajorFunction = IRP_MJ_SCSI;
  1824. srb->OriginalRequest = irp;
  1825. //
  1826. // Save SRB address in next stack for port driver.
  1827. //
  1828. irpStack->Parameters.Others.Argument1 = (PVOID)srb;
  1829. //
  1830. // Can't release the remove lock yet - let ClassAsynchronousCompletion
  1831. // take care of that for us.
  1832. //
  1833. (VOID)IoCallDriver(fdoExtension->CommonExtension.LowerDeviceObject, irp);
  1834. return;
  1835. }
  1836. NTSTATUS
  1837. FlopticalFormatMedia(
  1838. PDEVICE_OBJECT DeviceObject,
  1839. PFORMAT_PARAMETERS Format
  1840. )
  1841. /*++
  1842. Routine Description:
  1843. This routine is used to do perform a format tracks for the 20.8 MB
  1844. floppy. Because the device does not support format tracks and the full
  1845. format takes a long time a write of zeros is done instead.
  1846. Arguments:
  1847. DeviceObject - Supplies the device object to be tested.
  1848. Format - Supplies the format parameters.
  1849. Return Value:
  1850. Returns a status for the operation.
  1851. --*/
  1852. {
  1853. IO_STATUS_BLOCK ioStatus;
  1854. PIRP irp;
  1855. KEVENT event;
  1856. LARGE_INTEGER offset;
  1857. ULONG length;
  1858. PVOID buffer;
  1859. PDRIVE_MEDIA_CONSTANTS driveMediaConstants;
  1860. NTSTATUS status;
  1861. driveMediaConstants = &DriveMediaConstants[Drive2080Media2080];
  1862. //
  1863. // Calculate the length of the buffer.
  1864. //
  1865. length = ((Format->EndCylinderNumber - Format->StartCylinderNumber) *
  1866. driveMediaConstants->NumberOfHeads +
  1867. Format->EndHeadNumber - Format->StartHeadNumber + 1) *
  1868. driveMediaConstants->SectorsPerTrack *
  1869. driveMediaConstants->BytesPerSector;
  1870. buffer = ExAllocatePool(NonPagedPoolCacheAligned, length);
  1871. if (buffer == NULL) {
  1872. return(STATUS_INSUFFICIENT_RESOURCES);
  1873. }
  1874. RtlZeroMemory(buffer, length);
  1875. offset.QuadPart =
  1876. (Format->StartCylinderNumber * driveMediaConstants->NumberOfHeads +
  1877. Format->StartHeadNumber) * driveMediaConstants->SectorsPerTrack *
  1878. driveMediaConstants->BytesPerSector;
  1879. //
  1880. // Set the event object to the unsignaled state.
  1881. // It will be used to signal request completion.
  1882. //
  1883. KeInitializeEvent(&event, NotificationEvent, FALSE);
  1884. //
  1885. // Build the synchronous request with data transfer.
  1886. //
  1887. irp = IoBuildSynchronousFsdRequest(
  1888. IRP_MJ_WRITE,
  1889. DeviceObject,
  1890. buffer,
  1891. length,
  1892. &offset,
  1893. &event,
  1894. &ioStatus);
  1895. status = IoCallDriver(DeviceObject, irp);
  1896. if (status == STATUS_PENDING) {
  1897. //
  1898. // Wait for the request to complete if necessary.
  1899. //
  1900. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  1901. }
  1902. //
  1903. // If the call driver suceeded then set the status to the status block.
  1904. //
  1905. if (NT_SUCCESS(status)) {
  1906. status = ioStatus.Status;
  1907. }
  1908. ExFreePool(buffer);
  1909. return(status);
  1910. }
  1911. #if 0
  1912. LONG
  1913. SFlopStringCmp (
  1914. PCHAR FirstStr,
  1915. PCHAR SecondStr,
  1916. ULONG Count
  1917. )
  1918. {
  1919. UCHAR first ,last;
  1920. if (Count) {
  1921. do {
  1922. //
  1923. // Get next char.
  1924. //
  1925. first = *FirstStr++;
  1926. last = *SecondStr++;
  1927. if (first != last) {
  1928. //
  1929. // If no match, try lower-casing.
  1930. //
  1931. if (first>='A' && first<='Z') {
  1932. first = first - 'A' + 'a';
  1933. }
  1934. if (last>='A' && last<='Z') {
  1935. last = last - 'A' + 'a';
  1936. }
  1937. if (first != last) {
  1938. //
  1939. // No match
  1940. //
  1941. return first - last;
  1942. }
  1943. }
  1944. }while (--Count && first);
  1945. }
  1946. return 0;
  1947. }
  1948. #endif
  1949. NTSTATUS
  1950. ScsiFlopRemoveDevice(
  1951. IN PDEVICE_OBJECT DeviceObject,
  1952. IN UCHAR Type
  1953. )
  1954. /*++
  1955. Routine Description:
  1956. This routine is responsible for releasing any resources in use by the
  1957. sfloppy driver. This routine is called
  1958. when all outstanding requests have been completed and the driver has
  1959. disappeared - no requests may be issued to the lower drivers.
  1960. Arguments:
  1961. DeviceObject - the device object being removed
  1962. Type - the type of remove operation (QUERY, REMOVE or CANCEL)
  1963. Return Value:
  1964. for a query - success if the device can be removed or a failure code
  1965. indiciating why not.
  1966. for a remove or cancel - STATUS_SUCCESS
  1967. --*/
  1968. {
  1969. PFUNCTIONAL_DEVICE_EXTENSION deviceExtension =
  1970. DeviceObject->DeviceExtension;
  1971. PDISK_DATA diskData = deviceExtension->CommonExtension.DriverData;
  1972. PAGED_CODE();
  1973. if((Type == IRP_MN_QUERY_REMOVE_DEVICE) ||
  1974. (Type == IRP_MN_CANCEL_REMOVE_DEVICE)) {
  1975. return STATUS_SUCCESS;
  1976. }
  1977. if(deviceExtension->DeviceDescriptor) {
  1978. ExFreePool(deviceExtension->DeviceDescriptor);
  1979. deviceExtension->DeviceDescriptor = NULL;
  1980. }
  1981. if(deviceExtension->AdapterDescriptor) {
  1982. ExFreePool(deviceExtension->AdapterDescriptor);
  1983. deviceExtension->AdapterDescriptor = NULL;
  1984. }
  1985. if(deviceExtension->SenseData) {
  1986. ExFreePool(deviceExtension->SenseData);
  1987. deviceExtension->SenseData = NULL;
  1988. }
  1989. ClassDeleteSrbLookasideList(&deviceExtension->CommonExtension);
  1990. if(diskData->FloppyInterfaceString.Buffer != NULL) {
  1991. IoSetDeviceInterfaceState(
  1992. &(diskData->FloppyInterfaceString),
  1993. FALSE);
  1994. RtlFreeUnicodeString(&(diskData->FloppyInterfaceString));
  1995. RtlInitUnicodeString(&(diskData->FloppyInterfaceString), NULL);
  1996. }
  1997. if(Type == IRP_MN_REMOVE_DEVICE) {
  1998. IoGetConfigurationInformation()->FloppyCount--;
  1999. }
  2000. return STATUS_SUCCESS;
  2001. }
  2002. NTSTATUS
  2003. ScsiFlopStopDevice(
  2004. IN PDEVICE_OBJECT DeviceObject,
  2005. IN UCHAR Type
  2006. )
  2007. {
  2008. return STATUS_SUCCESS;
  2009. }
  2010. NTSTATUS
  2011. USBFlopGetMediaTypes(
  2012. IN PDEVICE_OBJECT DeviceObject,
  2013. IN PIRP Irp
  2014. )
  2015. {
  2016. /*++
  2017. Routine Description:
  2018. This routines determines the current or default geometry of the drive
  2019. for IOCTL_DISK_GET_DRIVE_GEOMETRY, or all currently supported geometries
  2020. of the drive (which is determined by its currently inserted media) for
  2021. IOCTL_DISK_GET_MEDIA_TYPES.
  2022. The returned geometries are determined by issuing a Read Format Capacities
  2023. request and then matching the returned {Number of Blocks, Block Length}
  2024. pairs in a table of known floppy geometries.
  2025. Arguments:
  2026. DeviceObject - Supplies the device object.
  2027. Irp - A IOCTL_DISK_GET_DRIVE_GEOMETRY or a IOCTL_DISK_GET_MEDIA_TYPES Irp.
  2028. If NULL, the device geometry is updated with the current device
  2029. geometry.
  2030. Return Value:
  2031. Status is returned.
  2032. --*/
  2033. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension;
  2034. PIO_STACK_LOCATION irpStack;
  2035. ULONG ioControlCode;
  2036. PDISK_GEOMETRY outputBuffer;
  2037. PDISK_GEOMETRY outputBufferEnd;
  2038. ULONG outputBufferLength;
  2039. PSCSI_REQUEST_BLOCK srb;
  2040. PVOID dataBuffer;
  2041. ULONG dataTransferLength;
  2042. struct _READ_FORMATTED_CAPACITIES *cdb;
  2043. PFORMATTED_CAPACITY_LIST capList;
  2044. NTSTATUS status;
  2045. fdoExtension = DeviceObject->DeviceExtension;
  2046. if (Irp != NULL) {
  2047. // Get the Irp parameters
  2048. //
  2049. irpStack = IoGetCurrentIrpStackLocation(Irp);
  2050. ioControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;
  2051. Irp->IoStatus.Information = 0;
  2052. outputBuffer = (PDISK_GEOMETRY) Irp->AssociatedIrp.SystemBuffer;
  2053. outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;
  2054. if (outputBufferLength < sizeof(DISK_GEOMETRY))
  2055. {
  2056. return STATUS_BUFFER_TOO_SMALL;
  2057. }
  2058. // Pointer arithmetic to allow multiple DISK_GEOMETRY's to be returned.
  2059. // Rounds BufferEnd down to integral multiple of DISK_GEOMETRY structs.
  2060. //
  2061. outputBufferEnd = outputBuffer +
  2062. outputBufferLength / sizeof(DISK_GEOMETRY);
  2063. } else {
  2064. // No Irp to return the result in, just update the current geometry
  2065. // in the device extension.
  2066. //
  2067. ioControlCode = IOCTL_DISK_GET_DRIVE_GEOMETRY;
  2068. outputBuffer = NULL;
  2069. outputBufferEnd = NULL;
  2070. outputBufferLength = 0;
  2071. }
  2072. if (ioControlCode == IOCTL_DISK_GET_DRIVE_GEOMETRY) {
  2073. fdoExtension->DiskGeometry.MediaType = Unknown;
  2074. status = ClassReadDriveCapacity(DeviceObject);
  2075. if (!NT_SUCCESS(status))
  2076. {
  2077. // If the media is not recongized, we want to return the default
  2078. // geometry so that the media can be formatted. Unrecognized media
  2079. // causes SCSI_SENSE_MEDIUM_ERROR, which gets reported as
  2080. // STATUS_DEVICE_DATA_ERROR. Ignore these errors, but return other
  2081. // errors, such as STATUS_NO_MEDIA_IN_DEVICE.
  2082. //
  2083. if (status != STATUS_UNRECOGNIZED_MEDIA)
  2084. {
  2085. DebugPrint((2,"IOCTL_DISK_GET_DRIVE_GEOMETRY returns %08X\n", status));
  2086. return status;
  2087. }
  2088. }
  2089. }
  2090. // Allocate an SRB for the SCSIOP_READ_FORMATTED_CAPACITY request
  2091. //
  2092. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  2093. if (srb == NULL)
  2094. {
  2095. return STATUS_INSUFFICIENT_RESOURCES;
  2096. }
  2097. // Allocate a transfer buffer for the SCSIOP_READ_FORMATTED_CAPACITY request
  2098. // The length of the returned descriptor array is limited to a byte field
  2099. // in the capacity list header.
  2100. //
  2101. dataTransferLength = sizeof(FORMATTED_CAPACITY_LIST) +
  2102. 31 * sizeof(FORMATTED_CAPACITY_DESCRIPTOR);
  2103. ASSERT(dataTransferLength < 0x100);
  2104. dataBuffer = ExAllocatePool(NonPagedPool, dataTransferLength);
  2105. if (dataBuffer == NULL)
  2106. {
  2107. ExFreePool(srb);
  2108. return STATUS_INSUFFICIENT_RESOURCES;
  2109. }
  2110. // Initialize the SRB and CDB
  2111. //
  2112. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  2113. RtlZeroMemory(dataBuffer, dataTransferLength);
  2114. srb->CdbLength = sizeof(struct _READ_FORMATTED_CAPACITIES);
  2115. srb->TimeOutValue = fdoExtension->TimeOutValue;
  2116. cdb = (struct _READ_FORMATTED_CAPACITIES *)srb->Cdb;
  2117. cdb->OperationCode = SCSIOP_READ_FORMATTED_CAPACITY;
  2118. cdb->AllocationLength[1] = (UCHAR)dataTransferLength;
  2119. // Send down the SCSIOP_READ_FORMATTED_CAPACITY request
  2120. //
  2121. status = ClassSendSrbSynchronous(DeviceObject,
  2122. srb,
  2123. dataBuffer,
  2124. dataTransferLength,
  2125. FALSE);
  2126. capList = (PFORMATTED_CAPACITY_LIST)dataBuffer;
  2127. // If we don't get as much data as requested, it is not an error.
  2128. //
  2129. if (SRB_STATUS(srb->SrbStatus) == SRB_STATUS_DATA_OVERRUN)
  2130. {
  2131. status = STATUS_SUCCESS;
  2132. }
  2133. if (NT_SUCCESS(status) &&
  2134. srb->DataTransferLength >= sizeof(FORMATTED_CAPACITY_LIST) &&
  2135. capList->CapacityListLength &&
  2136. capList->CapacityListLength % sizeof(FORMATTED_CAPACITY_DESCRIPTOR) == 0)
  2137. {
  2138. ULONG NumberOfBlocks;
  2139. ULONG BlockLength;
  2140. ULONG count;
  2141. ULONG i, j;
  2142. LONG currentGeometry;
  2143. BOOLEAN capacityMatches[FLOPPY_CAPACITIES];
  2144. // Subtract the size of the Capacity List Header to get
  2145. // just the size of the Capacity List Descriptor array.
  2146. //
  2147. srb->DataTransferLength -= sizeof(FORMATTED_CAPACITY_LIST);
  2148. // Only look at the Capacity List Descriptors that were actually
  2149. // returned.
  2150. //
  2151. if (srb->DataTransferLength < capList->CapacityListLength)
  2152. {
  2153. count = srb->DataTransferLength /
  2154. sizeof(FORMATTED_CAPACITY_DESCRIPTOR);
  2155. }
  2156. else
  2157. {
  2158. count = capList->CapacityListLength /
  2159. sizeof(FORMATTED_CAPACITY_DESCRIPTOR);
  2160. }
  2161. // Updated only if a match is found for the first Capacity List
  2162. // Descriptor returned by the device.
  2163. //
  2164. currentGeometry = -1;
  2165. // Initialize the array of capacities that hit a match.
  2166. //
  2167. RtlZeroMemory(capacityMatches, sizeof(capacityMatches));
  2168. // Iterate over each Capacity List Descriptor returned from the device
  2169. // and record matching capacities in the capacity match array.
  2170. //
  2171. for (i = 0; i < count; i++)
  2172. {
  2173. NumberOfBlocks = (capList->Descriptors[i].NumberOfBlocks[0] << 24) +
  2174. (capList->Descriptors[i].NumberOfBlocks[1] << 16) +
  2175. (capList->Descriptors[i].NumberOfBlocks[2] << 8) +
  2176. (capList->Descriptors[i].NumberOfBlocks[3]);
  2177. BlockLength = (capList->Descriptors[i].BlockLength[0] << 16) +
  2178. (capList->Descriptors[i].BlockLength[1] << 8) +
  2179. (capList->Descriptors[i].BlockLength[2]);
  2180. // Given the {NumberOfBlocks, BlockLength} from this Capacity List
  2181. // Descriptor, find a matching entry in FloppyCapacities[].
  2182. //
  2183. for (j = 0; j < FLOPPY_CAPACITIES; j++)
  2184. {
  2185. if (NumberOfBlocks == FloppyCapacities[j].NumberOfBlocks &&
  2186. BlockLength == FloppyCapacities[j].BlockLength)
  2187. {
  2188. // A matching capacity was found, record it.
  2189. //
  2190. capacityMatches[j] = TRUE;
  2191. // A match was found for the first Capacity List
  2192. // Descriptor returned by the device.
  2193. //
  2194. if (i == 0)
  2195. {
  2196. currentGeometry = j;
  2197. }
  2198. }
  2199. }
  2200. }
  2201. // Default status is STATUS_UNRECOGNIZED_MEDIA, unless we return
  2202. // either STATUS_SUCCESS or STATUS_BUFFER_OVERFLOW.
  2203. //
  2204. status = STATUS_UNRECOGNIZED_MEDIA;
  2205. if (ioControlCode == IOCTL_DISK_GET_DRIVE_GEOMETRY) {
  2206. if (currentGeometry != -1)
  2207. {
  2208. // Update the current device geometry
  2209. //
  2210. fdoExtension->DiskGeometry = FloppyGeometries[currentGeometry];
  2211. //
  2212. // Calculate sector to byte shift.
  2213. //
  2214. WHICH_BIT(fdoExtension->DiskGeometry.BytesPerSector,
  2215. fdoExtension->SectorShift);
  2216. fdoExtension->CommonExtension.PartitionLength.QuadPart =
  2217. (LONGLONG)FloppyCapacities[currentGeometry].NumberOfBlocks *
  2218. FloppyCapacities[currentGeometry].BlockLength;
  2219. DebugPrint((2,"geometry is: %3d %2d %d %2d %4d %2d %08X\n",
  2220. fdoExtension->DiskGeometry.Cylinders.LowPart,
  2221. fdoExtension->DiskGeometry.MediaType,
  2222. fdoExtension->DiskGeometry.TracksPerCylinder,
  2223. fdoExtension->DiskGeometry.SectorsPerTrack,
  2224. fdoExtension->DiskGeometry.BytesPerSector,
  2225. fdoExtension->SectorShift,
  2226. fdoExtension->CommonExtension.PartitionLength.LowPart));
  2227. // Return the current device geometry
  2228. //
  2229. if (Irp != NULL)
  2230. {
  2231. *outputBuffer = FloppyGeometries[currentGeometry];
  2232. Irp->IoStatus.Information = sizeof(DISK_GEOMETRY);
  2233. }
  2234. status = STATUS_SUCCESS;
  2235. }
  2236. } else {
  2237. // Iterate over the capacities and return the geometry
  2238. // corresponding to each matching Capacity List Descriptor
  2239. // returned from the device.
  2240. //
  2241. // The resulting list should be in sorted ascending order,
  2242. // assuming that the FloppyGeometries[] array is in sorted
  2243. // ascending order.
  2244. //
  2245. for (i = 0; i < FLOPPY_CAPACITIES; i++)
  2246. {
  2247. if (capacityMatches[i] && FloppyCapacities[i].CanFormat)
  2248. {
  2249. if (outputBuffer < outputBufferEnd)
  2250. {
  2251. *outputBuffer++ = FloppyGeometries[i];
  2252. Irp->IoStatus.Information += sizeof(DISK_GEOMETRY);
  2253. DebugPrint((2,"geometry : %3d %2d %d %2d %4d\n",
  2254. FloppyGeometries[i].Cylinders.LowPart,
  2255. FloppyGeometries[i].MediaType,
  2256. FloppyGeometries[i].TracksPerCylinder,
  2257. FloppyGeometries[i].SectorsPerTrack,
  2258. FloppyGeometries[i].BytesPerSector));
  2259. status = STATUS_SUCCESS;
  2260. }
  2261. else
  2262. {
  2263. // We ran out of output buffer room before we ran out
  2264. // geometries to return.
  2265. //
  2266. status = STATUS_BUFFER_OVERFLOW;
  2267. }
  2268. }
  2269. }
  2270. }
  2271. }
  2272. else if (NT_SUCCESS(status))
  2273. {
  2274. // The SCSIOP_READ_FORMATTED_CAPACITY request was successful, but
  2275. // returned data does not appear valid.
  2276. //
  2277. status = STATUS_UNSUCCESSFUL;
  2278. }
  2279. ExFreePool(dataBuffer);
  2280. ExFreePool(srb);
  2281. return status;
  2282. }
  2283. NTSTATUS
  2284. USBFlopFormatTracks(
  2285. IN PDEVICE_OBJECT DeviceObject,
  2286. IN PIRP Irp
  2287. )
  2288. {
  2289. /*++
  2290. Routine Description:
  2291. This routines formats the specified tracks. If multiple tracks are
  2292. specified, each is formatted with a separate Format Unit request.
  2293. Arguments:
  2294. DeviceObject - Supplies the device object.
  2295. Irp - A IOCTL_DISK_FORMAT_TRACKS Irp.
  2296. Return Value:
  2297. Status is returned.
  2298. --*/
  2299. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension;
  2300. PIO_STACK_LOCATION irpStack;
  2301. PFORMAT_PARAMETERS formatParameters;
  2302. PDISK_GEOMETRY geometry;
  2303. PFORMATTED_CAPACITY capacity;
  2304. PSCSI_REQUEST_BLOCK srb;
  2305. PFORMAT_UNIT_PARAMETER_LIST parameterList;
  2306. PCDB12FORMAT cdb;
  2307. ULONG i;
  2308. ULONG cylinder, head;
  2309. NTSTATUS status;
  2310. fdoExtension = DeviceObject->DeviceExtension;
  2311. // Get the Irp parameters
  2312. //
  2313. irpStack = IoGetCurrentIrpStackLocation(Irp);
  2314. if (irpStack->Parameters.DeviceIoControl.InputBufferLength <
  2315. sizeof(FORMAT_PARAMETERS))
  2316. {
  2317. return STATUS_INVALID_PARAMETER;
  2318. }
  2319. formatParameters = (PFORMAT_PARAMETERS)Irp->AssociatedIrp.SystemBuffer;
  2320. // Find the geometry / capacity entries corresponding to the format
  2321. // parameters MediaType
  2322. //
  2323. geometry = NULL;
  2324. capacity = NULL;
  2325. for (i=0; i<FLOPPY_CAPACITIES; i++)
  2326. {
  2327. if (FloppyGeometries[i].MediaType == formatParameters->MediaType)
  2328. {
  2329. geometry = &FloppyGeometries[i];
  2330. capacity = &FloppyCapacities[i];
  2331. break;
  2332. }
  2333. }
  2334. if (geometry == NULL)
  2335. {
  2336. return STATUS_INVALID_PARAMETER;
  2337. }
  2338. // Check if the format parameters are valid
  2339. //
  2340. if ((formatParameters->StartCylinderNumber >
  2341. geometry->Cylinders.LowPart - 1) ||
  2342. (formatParameters->EndCylinderNumber >
  2343. geometry->Cylinders.LowPart - 1) ||
  2344. (formatParameters->StartHeadNumber >
  2345. geometry->TracksPerCylinder - 1) ||
  2346. (formatParameters->EndHeadNumber >
  2347. geometry->TracksPerCylinder - 1) ||
  2348. (formatParameters->StartCylinderNumber >
  2349. formatParameters->EndCylinderNumber) ||
  2350. (formatParameters->StartHeadNumber >
  2351. formatParameters->EndHeadNumber))
  2352. {
  2353. return STATUS_INVALID_PARAMETER;
  2354. }
  2355. // Don't low level format LS-120 media, Imation says it's best to not
  2356. // do this.
  2357. //
  2358. if ((formatParameters->MediaType == F3_120M_512) ||
  2359. (formatParameters->MediaType == F3_240M_512))
  2360. {
  2361. return STATUS_SUCCESS;
  2362. }
  2363. // Allocate an SRB for the SCSIOP_FORMAT_UNIT request
  2364. //
  2365. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  2366. if (srb == NULL)
  2367. {
  2368. return STATUS_INSUFFICIENT_RESOURCES;
  2369. }
  2370. // Allocate a transfer buffer for the SCSIOP_FORMAT_UNIT parameter list
  2371. //
  2372. parameterList = ExAllocatePool(NonPagedPool,
  2373. sizeof(FORMAT_UNIT_PARAMETER_LIST));
  2374. if (parameterList == NULL)
  2375. {
  2376. ExFreePool(srb);
  2377. return STATUS_INSUFFICIENT_RESOURCES;
  2378. }
  2379. // Initialize the parameter list
  2380. //
  2381. RtlZeroMemory(parameterList, sizeof(FORMAT_UNIT_PARAMETER_LIST));
  2382. parameterList->DefectListHeader.SingleTrack = 1;
  2383. parameterList->DefectListHeader.DisableCert = 1; // TEAC requires this set
  2384. parameterList->DefectListHeader.FormatOptionsValid = 1;
  2385. parameterList->DefectListHeader.DefectListLengthLsb = 8;
  2386. parameterList->FormatDescriptor.NumberOfBlocks[0] =
  2387. (UCHAR)((capacity->NumberOfBlocks >> 24) & 0xFF);
  2388. parameterList->FormatDescriptor.NumberOfBlocks[1] =
  2389. (UCHAR)((capacity->NumberOfBlocks >> 16) & 0xFF);
  2390. parameterList->FormatDescriptor.NumberOfBlocks[2] =
  2391. (UCHAR)((capacity->NumberOfBlocks >> 8) & 0xFF);
  2392. parameterList->FormatDescriptor.NumberOfBlocks[3] =
  2393. (UCHAR)(capacity->NumberOfBlocks & 0xFF);
  2394. parameterList->FormatDescriptor.BlockLength[0] =
  2395. (UCHAR)((capacity->BlockLength >> 16) & 0xFF);
  2396. parameterList->FormatDescriptor.BlockLength[1] =
  2397. (UCHAR)((capacity->BlockLength >> 8) & 0xFF);
  2398. parameterList->FormatDescriptor.BlockLength[2] =
  2399. (UCHAR)(capacity->BlockLength & 0xFF);
  2400. for (cylinder = formatParameters->StartCylinderNumber;
  2401. cylinder <= formatParameters->EndCylinderNumber;
  2402. cylinder++)
  2403. {
  2404. for (head = formatParameters->StartHeadNumber;
  2405. head <= formatParameters->EndHeadNumber;
  2406. head++)
  2407. {
  2408. // Initialize the SRB and CDB
  2409. //
  2410. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  2411. srb->CdbLength = sizeof(CDB12FORMAT);
  2412. srb->TimeOutValue = fdoExtension->TimeOutValue;
  2413. cdb = (PCDB12FORMAT)srb->Cdb;
  2414. cdb->OperationCode = SCSIOP_FORMAT_UNIT;
  2415. cdb->DefectListFormat = 7;
  2416. cdb->FmtData = 1;
  2417. cdb->TrackNumber = (UCHAR)cylinder;
  2418. cdb->ParameterListLengthLsb = sizeof(FORMAT_UNIT_PARAMETER_LIST);
  2419. parameterList->DefectListHeader.Side = (UCHAR)head;
  2420. // Send down the SCSIOP_READ_FORMATTED_CAPACITY request
  2421. //
  2422. status = ClassSendSrbSynchronous(DeviceObject,
  2423. srb,
  2424. parameterList,
  2425. sizeof(FORMAT_UNIT_PARAMETER_LIST),
  2426. TRUE);
  2427. if (!NT_SUCCESS(status))
  2428. {
  2429. break;
  2430. }
  2431. }
  2432. if (!NT_SUCCESS(status))
  2433. {
  2434. break;
  2435. }
  2436. }
  2437. if (NT_SUCCESS(status) && formatParameters->StartCylinderNumber == 0)
  2438. {
  2439. // Update the device geometry
  2440. //
  2441. DebugPrint((2,"geometry was: %3d %2d %d %2d %4d %2d %08X\n",
  2442. fdoExtension->DiskGeometry.Cylinders.LowPart,
  2443. fdoExtension->DiskGeometry.MediaType,
  2444. fdoExtension->DiskGeometry.TracksPerCylinder,
  2445. fdoExtension->DiskGeometry.SectorsPerTrack,
  2446. fdoExtension->DiskGeometry.BytesPerSector,
  2447. fdoExtension->SectorShift,
  2448. fdoExtension->CommonExtension.PartitionLength.LowPart));
  2449. fdoExtension->DiskGeometry = *geometry;
  2450. //
  2451. // Calculate sector to byte shift.
  2452. //
  2453. WHICH_BIT(fdoExtension->DiskGeometry.BytesPerSector,
  2454. fdoExtension->SectorShift);
  2455. fdoExtension->CommonExtension.PartitionLength.QuadPart =
  2456. (LONGLONG)capacity->NumberOfBlocks *
  2457. capacity->BlockLength;
  2458. DebugPrint((2,"geometry is: %3d %2d %d %2d %4d %2d %08X\n",
  2459. fdoExtension->DiskGeometry.Cylinders.LowPart,
  2460. fdoExtension->DiskGeometry.MediaType,
  2461. fdoExtension->DiskGeometry.TracksPerCylinder,
  2462. fdoExtension->DiskGeometry.SectorsPerTrack,
  2463. fdoExtension->DiskGeometry.BytesPerSector,
  2464. fdoExtension->SectorShift,
  2465. fdoExtension->CommonExtension.PartitionLength.LowPart));
  2466. }
  2467. // Free everything we allocated
  2468. //
  2469. ExFreePool(parameterList);
  2470. ExFreePool(srb);
  2471. return status;
  2472. }