Leaked source code of windows server 2003
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.

3426 lines
95 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. {0x010000, 0x0200, TRUE}, // 2 1024 512 32 32 MB F3_32M_512
  198. {0x03C300, 0x0200, TRUE}, // 8 963 512 32 120 MB F3_120M_512
  199. {0x0600A4, 0x0200, TRUE}, // 13 890 512 34 200 MB F3_200Mb_512 (HiFD)
  200. {0x072A00, 0x0200, TRUE} // 32 262 512 56 240 MB F3_240M_512
  201. };
  202. DISK_GEOMETRY FloppyGeometries[] =
  203. {
  204. // Cyl MediaType Trk/Cyl Sec/Trk Bytes/Sec
  205. {{80,0}, F3_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. {{1024,0}, F3_32M_512, 2, 32, 512},
  212. {{963,0}, F3_120M_512, 8, 32, 512},
  213. {{890,0}, F3_200Mb_512, 13, 34, 512},
  214. {{262,0}, F3_240M_512, 32, 56, 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. UNICODE_STRING interfaceName;
  665. RtlInitUnicodeString(&interfaceName, NULL);
  666. status = IoRegisterDeviceInterface(fdoExtension->LowerPdo,
  667. (LPGUID) &FloppyClassGuid,
  668. NULL,
  669. &interfaceName);
  670. if(NT_SUCCESS(status)) {
  671. diskData->FloppyInterfaceString = interfaceName;
  672. } else {
  673. RtlInitUnicodeString(&(diskData->FloppyInterfaceString), NULL);
  674. DebugPrint((1, "ScsiFlopStartDevice: Unable to register device "
  675. "interface for fdo %#p [%#08lx]\n",
  676. Fdo, status));
  677. }
  678. }
  679. return (STATUS_SUCCESS);
  680. }
  681. NTSTATUS
  682. ScsiFlopStartDevice(
  683. IN PDEVICE_OBJECT Fdo
  684. )
  685. {
  686. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = Fdo->DeviceExtension;
  687. PCOMMON_DEVICE_EXTENSION commonExtension = Fdo->DeviceExtension;
  688. PIRP irp;
  689. IO_STATUS_BLOCK ioStatus;
  690. SCSI_ADDRESS scsiAddress;
  691. WCHAR ntNameBuffer[256];
  692. UNICODE_STRING ntUnicodeString;
  693. WCHAR arcNameBuffer[256];
  694. UNICODE_STRING arcUnicodeString;
  695. KEVENT event;
  696. NTSTATUS status = STATUS_SUCCESS;
  697. KeInitializeEvent(&event,SynchronizationEvent,FALSE);
  698. DetermineMediaType(Fdo); // ignore unsuccessful here
  699. //
  700. // Create a symbolic link from the disk name to the corresponding
  701. // ARC name, to be used if we're booting off the disk. This will
  702. // fail if it's not system initialization time; that's fine. The
  703. // ARC name looks something like \ArcName\scsi(0)Flop(0)fdisk(0).
  704. // In order to get the address, we need to send a IOCTL_SCSI_GET_ADDRESS...
  705. //
  706. irp = IoBuildDeviceIoControlRequest(IOCTL_SCSI_GET_ADDRESS,
  707. Fdo,
  708. NULL,
  709. 0,
  710. &scsiAddress,
  711. sizeof(scsiAddress),
  712. FALSE,
  713. &event,
  714. &ioStatus);
  715. if (irp == NULL) {
  716. return STATUS_INSUFFICIENT_RESOURCES;
  717. }
  718. status = IoCallDriver(fdoExtension->CommonExtension.LowerDeviceObject, irp);
  719. if (status == STATUS_PENDING) {
  720. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  721. status = ioStatus.Status;
  722. }
  723. swprintf(arcNameBuffer,
  724. L"\\ArcName\\scsi(%d)disk(%d)fdisk(%d)",
  725. scsiAddress.PortNumber,
  726. scsiAddress.TargetId,
  727. scsiAddress.Lun);
  728. RtlInitUnicodeString(&arcUnicodeString, arcNameBuffer);
  729. //
  730. // Create device object for this device.
  731. //
  732. swprintf(ntNameBuffer,L"\\Device\\Floppy%d",fdoExtension->DeviceNumber);
  733. //
  734. // Create local copy of unicode string
  735. //
  736. RtlInitUnicodeString(&ntUnicodeString,ntNameBuffer);
  737. IoAssignArcName(&arcUnicodeString, &ntUnicodeString);
  738. //
  739. // Create the multi() arc name -- Create the "fake"
  740. // name of multi(0)disk(0)fdisk(#) to handle the case where the
  741. // SCSI floppy is the only floppy in the system. If this fails
  742. // it doesn't matter because the previous scsi() based ArcName
  743. // will work. This name is necessary for installation.
  744. //
  745. swprintf(arcNameBuffer, L"\\ArcName\\multi(%d)disk(%d)fdisk(%d)",
  746. 0,
  747. 0,
  748. fdoExtension->DeviceNumber);
  749. RtlInitUnicodeString(&arcUnicodeString, arcNameBuffer);
  750. IoAssignArcName(&arcUnicodeString, &ntUnicodeString);
  751. //
  752. // Set our interface state.
  753. //
  754. {
  755. PDISK_DATA diskData = commonExtension->DriverData;
  756. if(diskData->FloppyInterfaceString.Buffer != NULL) {
  757. status = IoSetDeviceInterfaceState(
  758. &(diskData->FloppyInterfaceString),
  759. TRUE);
  760. #if DBG
  761. if(!NT_SUCCESS(status)) {
  762. DebugPrint((1, "ScsiFlopStartDevice: Unable to set device "
  763. "interface state to TRUE for fdo %#p "
  764. "[%#08lx]\n",
  765. Fdo, status));
  766. }
  767. #endif
  768. }
  769. }
  770. return STATUS_SUCCESS;
  771. }
  772. NTSTATUS
  773. ScsiFlopReadWriteVerification(
  774. IN PDEVICE_OBJECT DeviceObject,
  775. IN PIRP Irp
  776. )
  777. /*++
  778. Routine Description:
  779. Arguments:
  780. Return Value:
  781. NT Status
  782. --*/
  783. {
  784. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  785. PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
  786. NTSTATUS status = STATUS_SUCCESS;
  787. //
  788. // Make sure that the number of bytes to transfer is a multiple of the sector size
  789. //
  790. if ((irpSp->Parameters.Read.Length & (fdoExtension->DiskGeometry.BytesPerSector - 1)) != 0)
  791. {
  792. status = STATUS_INVALID_PARAMETER;
  793. }
  794. Irp->IoStatus.Status = status;
  795. return status;
  796. }
  797. NTSTATUS
  798. ScsiFlopDeviceControl(
  799. PDEVICE_OBJECT DeviceObject,
  800. PIRP Irp
  801. )
  802. /*++
  803. Routine Description:
  804. Arguments:
  805. Return Value:
  806. Status is returned.
  807. --*/
  808. {
  809. KIRQL currentIrql;
  810. PIO_STACK_LOCATION irpStack = IoGetCurrentIrpStackLocation(Irp);
  811. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  812. PSCSI_REQUEST_BLOCK srb;
  813. PCDB cdb;
  814. NTSTATUS status;
  815. PDISK_GEOMETRY outputBuffer;
  816. ULONG outputBufferLength;
  817. ULONG i;
  818. DRIVE_MEDIA_TYPE lowestDriveMediaType;
  819. DRIVE_MEDIA_TYPE highestDriveMediaType;
  820. PFORMAT_PARAMETERS formatParameters;
  821. PMODE_PARAMETER_HEADER modeData;
  822. ULONG length;
  823. //
  824. // Initialize the information field
  825. //
  826. Irp->IoStatus.Information = 0;
  827. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  828. if (srb == NULL) {
  829. Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
  830. if (IoIsErrorUserInduced(STATUS_INSUFFICIENT_RESOURCES)) {
  831. IoSetHardErrorOrVerifyDevice(Irp, DeviceObject);
  832. }
  833. KeRaiseIrql(DISPATCH_LEVEL, &currentIrql);
  834. ClassReleaseRemoveLock(DeviceObject, Irp);
  835. ClassCompleteRequest(DeviceObject, Irp, 0);
  836. KeLowerIrql(currentIrql);
  837. return(STATUS_INSUFFICIENT_RESOURCES);
  838. }
  839. //
  840. // Write zeros to Srb.
  841. //
  842. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  843. cdb = (PCDB)srb->Cdb;
  844. switch (irpStack->Parameters.DeviceIoControl.IoControlCode) {
  845. case IOCTL_DISK_VERIFY: {
  846. PVERIFY_INFORMATION verifyInfo = Irp->AssociatedIrp.SystemBuffer;
  847. LARGE_INTEGER byteOffset;
  848. ULONG sectorOffset;
  849. USHORT sectorCount;
  850. //
  851. // Validate buffer length.
  852. //
  853. if (irpStack->Parameters.DeviceIoControl.InputBufferLength <
  854. sizeof(VERIFY_INFORMATION)) {
  855. status = STATUS_INFO_LENGTH_MISMATCH;
  856. break;
  857. }
  858. //
  859. // Perform a bounds check on the sector range
  860. //
  861. if ((verifyInfo->StartingOffset.QuadPart > fdoExtension->CommonExtension.PartitionLength.QuadPart) ||
  862. (verifyInfo->StartingOffset.QuadPart < 0))
  863. {
  864. status = STATUS_NONEXISTENT_SECTOR;
  865. break;
  866. }
  867. else
  868. {
  869. ULONGLONG bytesRemaining = fdoExtension->CommonExtension.PartitionLength.QuadPart - verifyInfo->StartingOffset.QuadPart;
  870. if ((ULONGLONG)verifyInfo->Length > bytesRemaining)
  871. {
  872. status = STATUS_NONEXISTENT_SECTOR;
  873. break;
  874. }
  875. }
  876. //
  877. // Verify sectors
  878. //
  879. srb->CdbLength = 10;
  880. cdb->CDB10.OperationCode = SCSIOP_VERIFY;
  881. //
  882. // Add disk offset to starting sector.
  883. //
  884. byteOffset.QuadPart = fdoExtension->CommonExtension.StartingOffset.QuadPart +
  885. verifyInfo->StartingOffset.QuadPart;
  886. //
  887. // Convert byte offset to sector offset.
  888. //
  889. sectorOffset = (ULONG)(byteOffset.QuadPart >> fdoExtension->SectorShift);
  890. //
  891. // Convert ULONG byte count to USHORT sector count.
  892. //
  893. sectorCount = (USHORT)(verifyInfo->Length >> fdoExtension->SectorShift);
  894. //
  895. // Move little endian values into CDB in big endian format.
  896. //
  897. cdb->CDB10.LogicalBlockByte0 = ((PFOUR_BYTE)&sectorOffset)->Byte3;
  898. cdb->CDB10.LogicalBlockByte1 = ((PFOUR_BYTE)&sectorOffset)->Byte2;
  899. cdb->CDB10.LogicalBlockByte2 = ((PFOUR_BYTE)&sectorOffset)->Byte1;
  900. cdb->CDB10.LogicalBlockByte3 = ((PFOUR_BYTE)&sectorOffset)->Byte0;
  901. cdb->CDB10.TransferBlocksMsb = ((PFOUR_BYTE)&sectorCount)->Byte1;
  902. cdb->CDB10.TransferBlocksLsb = ((PFOUR_BYTE)&sectorCount)->Byte0;
  903. //
  904. // The verify command is used by the NT FORMAT utility and
  905. // requests are sent down for 5% of the volume size. The
  906. // request timeout value is calculated based on the number of
  907. // sectors verified.
  908. //
  909. srb->TimeOutValue = ((sectorCount + 0x7F) >> 7) *
  910. fdoExtension->TimeOutValue;
  911. status = ClassSendSrbAsynchronous(DeviceObject,
  912. srb,
  913. Irp,
  914. NULL,
  915. 0,
  916. FALSE);
  917. return(status);
  918. }
  919. case IOCTL_DISK_GET_PARTITION_INFO: {
  920. if (fdoExtension->AdapterDescriptor->BusType == BusTypeUsb) {
  921. status = USBFlopGetMediaTypes(DeviceObject, NULL);
  922. // Don't need to propagate any error if one occurs
  923. //
  924. status = STATUS_SUCCESS;
  925. } else {
  926. status = DetermineMediaType(DeviceObject);
  927. }
  928. if (!NT_SUCCESS(status)) {
  929. // so will propogate error
  930. NOTHING;
  931. } else if (fdoExtension->DiskGeometry.MediaType == F3_120M_512) {
  932. //so that the format code will not try to partition it.
  933. status = STATUS_INVALID_DEVICE_REQUEST;
  934. } else {
  935. //
  936. // Free the Srb, since it is not needed.
  937. //
  938. ExFreePool(srb);
  939. //
  940. // Pass the request to the common device control routine.
  941. //
  942. return(ClassDeviceControl(DeviceObject, Irp));
  943. }
  944. break;
  945. }
  946. case IOCTL_DISK_GET_DRIVE_GEOMETRY: {
  947. DebugPrint((3,"ScsiDeviceIoControl: Get drive geometry\n"));
  948. if (fdoExtension->AdapterDescriptor->BusType == BusTypeUsb)
  949. {
  950. status = USBFlopGetMediaTypes(DeviceObject,
  951. Irp);
  952. break;
  953. }
  954. //
  955. // If there's not enough room to write the
  956. // data, then fail the request.
  957. //
  958. if ( irpStack->Parameters.DeviceIoControl.OutputBufferLength <
  959. sizeof( DISK_GEOMETRY ) ) {
  960. status = STATUS_INVALID_PARAMETER;
  961. break;
  962. }
  963. status = DetermineMediaType(DeviceObject);
  964. if (!NT_SUCCESS(status)) {
  965. Irp->IoStatus.Information = 0;
  966. Irp->IoStatus.Status = status;
  967. } else {
  968. //
  969. // Copy drive geometry information from device extension.
  970. //
  971. RtlMoveMemory(Irp->AssociatedIrp.SystemBuffer,
  972. &(fdoExtension->DiskGeometry),
  973. sizeof(DISK_GEOMETRY));
  974. Irp->IoStatus.Information = sizeof(DISK_GEOMETRY);
  975. status = STATUS_SUCCESS;
  976. }
  977. break;
  978. }
  979. case IOCTL_DISK_GET_MEDIA_TYPES: {
  980. if (fdoExtension->AdapterDescriptor->BusType == BusTypeUsb)
  981. {
  982. status = USBFlopGetMediaTypes(DeviceObject,
  983. Irp);
  984. break;
  985. }
  986. i = DetermineDriveType(DeviceObject);
  987. if (i == DRIVE_TYPE_NONE) {
  988. status = STATUS_UNRECOGNIZED_MEDIA;
  989. break;
  990. }
  991. lowestDriveMediaType = DriveMediaLimits[i].LowestDriveMediaType;
  992. highestDriveMediaType = DriveMediaLimits[i].HighestDriveMediaType;
  993. outputBufferLength =
  994. irpStack->Parameters.DeviceIoControl.OutputBufferLength;
  995. //
  996. // Make sure that the input buffer has enough room to return
  997. // at least one descriptions of a supported media type.
  998. //
  999. if ( outputBufferLength < ( sizeof( DISK_GEOMETRY ) ) ) {
  1000. status = STATUS_BUFFER_TOO_SMALL;
  1001. break;
  1002. }
  1003. //
  1004. // Assume success, although we might modify it to a buffer
  1005. // overflow warning below (if the buffer isn't big enough
  1006. // to hold ALL of the media descriptions).
  1007. //
  1008. status = STATUS_SUCCESS;
  1009. if (outputBufferLength < ( sizeof( DISK_GEOMETRY ) *
  1010. ( highestDriveMediaType - lowestDriveMediaType + 1 ) ) ) {
  1011. //
  1012. // The buffer is too small for all of the descriptions;
  1013. // calculate what CAN fit in the buffer.
  1014. //
  1015. status = STATUS_BUFFER_OVERFLOW;
  1016. highestDriveMediaType = (DRIVE_MEDIA_TYPE)( ( lowestDriveMediaType - 1 ) +
  1017. ( outputBufferLength /
  1018. sizeof( DISK_GEOMETRY ) ) );
  1019. }
  1020. outputBuffer = (PDISK_GEOMETRY) Irp->AssociatedIrp.SystemBuffer;
  1021. for (i = (UCHAR)lowestDriveMediaType;i <= (UCHAR)highestDriveMediaType;i++ ) {
  1022. outputBuffer->MediaType = DriveMediaConstants[i].MediaType;
  1023. outputBuffer->Cylinders.LowPart =
  1024. DriveMediaConstants[i].MaximumTrack + 1;
  1025. outputBuffer->Cylinders.HighPart = 0;
  1026. outputBuffer->TracksPerCylinder =
  1027. DriveMediaConstants[i].NumberOfHeads;
  1028. outputBuffer->SectorsPerTrack =
  1029. DriveMediaConstants[i].SectorsPerTrack;
  1030. outputBuffer->BytesPerSector =
  1031. DriveMediaConstants[i].BytesPerSector;
  1032. outputBuffer++;
  1033. Irp->IoStatus.Information += sizeof( DISK_GEOMETRY );
  1034. }
  1035. break;
  1036. }
  1037. case IOCTL_DISK_FORMAT_TRACKS: {
  1038. if (fdoExtension->AdapterDescriptor->BusType == BusTypeUsb)
  1039. {
  1040. status = USBFlopFormatTracks(DeviceObject,
  1041. Irp);
  1042. break;
  1043. }
  1044. //
  1045. // Make sure that we got all the necessary format parameters.
  1046. //
  1047. if ( irpStack->Parameters.DeviceIoControl.InputBufferLength <sizeof( FORMAT_PARAMETERS ) ) {
  1048. status = STATUS_INVALID_PARAMETER;
  1049. break;
  1050. }
  1051. formatParameters = (PFORMAT_PARAMETERS) Irp->AssociatedIrp.SystemBuffer;
  1052. //
  1053. // Make sure the parameters we got are reasonable.
  1054. //
  1055. if ( !FlCheckFormatParameters(DeviceObject, formatParameters)) {
  1056. status = STATUS_INVALID_PARAMETER;
  1057. break;
  1058. }
  1059. //
  1060. // If this request is for a 20.8 MB floppy then call a special
  1061. // floppy format routine.
  1062. //
  1063. if (formatParameters->MediaType == F3_20Pt8_512) {
  1064. status = FlopticalFormatMedia(DeviceObject,
  1065. formatParameters
  1066. );
  1067. break;
  1068. }
  1069. //
  1070. // All the work is done in the pass. If this is not the first pass,
  1071. // then complete the request and return;
  1072. //
  1073. if (formatParameters->StartCylinderNumber != 0 || formatParameters->StartHeadNumber != 0) {
  1074. status = STATUS_SUCCESS;
  1075. break;
  1076. }
  1077. status = FormatMedia( DeviceObject, formatParameters->MediaType);
  1078. break;
  1079. }
  1080. case IOCTL_DISK_IS_WRITABLE: {
  1081. if ((fdoExtension->DiskGeometry.MediaType) == F3_32M_512) {
  1082. //
  1083. // 32MB media is READ ONLY. Just return
  1084. // STATUS_MEDIA_WRITE_PROTECTED
  1085. //
  1086. status = STATUS_MEDIA_WRITE_PROTECTED;
  1087. break;
  1088. }
  1089. //
  1090. // Determine if the device is writable.
  1091. //
  1092. modeData = ExAllocatePool(NonPagedPoolCacheAligned, MODE_DATA_SIZE);
  1093. if (modeData == NULL) {
  1094. status = STATUS_INSUFFICIENT_RESOURCES;
  1095. break;
  1096. }
  1097. RtlZeroMemory(modeData, MODE_DATA_SIZE);
  1098. length = ClassModeSense(DeviceObject,
  1099. (PUCHAR) modeData,
  1100. MODE_DATA_SIZE,
  1101. MODE_SENSE_RETURN_ALL);
  1102. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1103. //
  1104. // Retry the request in case of a check condition.
  1105. //
  1106. length = ClassModeSense(DeviceObject,
  1107. (PUCHAR) modeData,
  1108. MODE_DATA_SIZE,
  1109. MODE_SENSE_RETURN_ALL);
  1110. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1111. status = STATUS_IO_DEVICE_ERROR;
  1112. ExFreePool(modeData);
  1113. break;
  1114. }
  1115. }
  1116. if (modeData->DeviceSpecificParameter & MODE_DSP_WRITE_PROTECT) {
  1117. status = STATUS_MEDIA_WRITE_PROTECTED;
  1118. } else {
  1119. status = STATUS_SUCCESS;
  1120. }
  1121. DebugPrint((2,"IOCTL_DISK_IS_WRITABLE returns %08X\n", status));
  1122. ExFreePool(modeData);
  1123. break;
  1124. }
  1125. default: {
  1126. DebugPrint((3,"ScsiIoDeviceControl: Unsupported device IOCTL\n"));
  1127. //
  1128. // Free the Srb, since it is not needed.
  1129. //
  1130. ExFreePool(srb);
  1131. //
  1132. // Pass the request to the common device control routine.
  1133. //
  1134. return(ClassDeviceControl(DeviceObject, Irp));
  1135. break;
  1136. }
  1137. } // end switch( ...
  1138. //
  1139. // Check if SL_OVERRIDE_VERIFY_VOLUME flag is set in the IRP.
  1140. // If so, do not return STATUS_VERIFY_REQUIRED
  1141. //
  1142. if ((status == STATUS_VERIFY_REQUIRED) &&
  1143. (TEST_FLAG(irpStack->Flags, SL_OVERRIDE_VERIFY_VOLUME))) {
  1144. status = STATUS_IO_DEVICE_ERROR;
  1145. }
  1146. Irp->IoStatus.Status = status;
  1147. if (!NT_SUCCESS(status) && IoIsErrorUserInduced(status)) {
  1148. IoSetHardErrorOrVerifyDevice(Irp, DeviceObject);
  1149. }
  1150. KeRaiseIrql(DISPATCH_LEVEL, &currentIrql);
  1151. ClassReleaseRemoveLock(DeviceObject, Irp);
  1152. ClassCompleteRequest(DeviceObject, Irp, 0);
  1153. KeLowerIrql(currentIrql);
  1154. ExFreePool(srb);
  1155. return status;
  1156. } // end ScsiFlopDeviceControl()
  1157. #if 0
  1158. BOOLEAN
  1159. IsFloppyDevice(
  1160. PDEVICE_OBJECT DeviceObject
  1161. )
  1162. /*++
  1163. Routine Description:
  1164. The routine performs the necessary funcitons to deterime if the device is
  1165. really a floppy rather than a harddisk. This is done by a mode sense
  1166. command. First a check is made to see if the medimum type is set. Second
  1167. a check is made for the flexible parameters mode page.
  1168. Arguments:
  1169. DeviceObject - Supplies the device object to be tested.
  1170. Return Value:
  1171. Return TRUE if the indicated device is a floppy.
  1172. --*/
  1173. {
  1174. PVOID modeData;
  1175. PUCHAR pageData;
  1176. ULONG length;
  1177. modeData = ExAllocatePool(NonPagedPoolCacheAligned, MODE_DATA_SIZE);
  1178. if (modeData == NULL) {
  1179. return(FALSE);
  1180. }
  1181. RtlZeroMemory(modeData, MODE_DATA_SIZE);
  1182. length = ClassModeSense(DeviceObject, modeData, MODE_DATA_SIZE, MODE_SENSE_RETURN_ALL);
  1183. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1184. //
  1185. // Retry the request in case of a check condition.
  1186. //
  1187. length = ClassModeSense(DeviceObject,
  1188. modeData,
  1189. MODE_DATA_SIZE,
  1190. MODE_SENSE_RETURN_ALL);
  1191. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1192. ExFreePool(modeData);
  1193. return(FALSE);
  1194. }
  1195. }
  1196. #if 0
  1197. //
  1198. // Some drives incorrectly report this. In particular the SONY RMO-S350
  1199. // when in disk mode.
  1200. //
  1201. if (((PMODE_PARAMETER_HEADER) modeData)->MediumType >= MODE_FD_SINGLE_SIDE
  1202. && ((PMODE_PARAMETER_HEADER) modeData)->MediumType <= MODE_FD_MAXIMUM_TYPE) {
  1203. DebugPrint((1, "ScsiFlop: MediumType value %2x, This is a floppy.\n", ((PMODE_PARAMETER_HEADER) modeData)->MediumType));
  1204. ExFreePool(modeData);
  1205. return(TRUE);
  1206. }
  1207. #endif
  1208. //
  1209. // If the length is greater than length indiated by the mode data reset
  1210. // the data to the mode data.
  1211. //
  1212. if (length > (ULONG)((PMODE_PARAMETER_HEADER) modeData)->ModeDataLength + 1) {
  1213. length = (ULONG)((PMODE_PARAMETER_HEADER) modeData)->ModeDataLength + 1;
  1214. }
  1215. //
  1216. // Look for the flexible disk mode page.
  1217. //
  1218. pageData = ClassFindModePage( modeData, length, MODE_PAGE_FLEXIBILE, TRUE);
  1219. if (pageData != NULL) {
  1220. DebugPrint((1, "ScsiFlop: Flexible disk page found, This is a floppy.\n"));
  1221. //
  1222. // As a special case for the floptical driver do a magic mode sense to
  1223. // enable the drive.
  1224. //
  1225. ClassModeSense(DeviceObject, modeData, 0x2a, 0x2e);
  1226. ExFreePool(modeData);
  1227. return(TRUE);
  1228. }
  1229. ExFreePool(modeData);
  1230. return(FALSE);
  1231. }
  1232. #endif
  1233. NTSTATUS
  1234. DetermineMediaType(
  1235. PDEVICE_OBJECT DeviceObject
  1236. )
  1237. /*++
  1238. Routine Description:
  1239. This routine determines the floppy media type based on the size of the
  1240. device. The geometry information is set for the device object.
  1241. Arguments:
  1242. DeviceObject - Supplies the device object to be tested.
  1243. Return Value:
  1244. None
  1245. --*/
  1246. {
  1247. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1248. PDISK_GEOMETRY geometry;
  1249. LONG index;
  1250. NTSTATUS status;
  1251. geometry = &(fdoExtension->DiskGeometry);
  1252. //
  1253. // Issue ReadCapacity to update device extension
  1254. // with information for current media.
  1255. //
  1256. status = ClassReadDriveCapacity(DeviceObject);
  1257. if (!NT_SUCCESS(status)) {
  1258. //
  1259. // Set the media type to unknow and zero the geometry information.
  1260. //
  1261. geometry->MediaType = Unknown;
  1262. return status;
  1263. }
  1264. //
  1265. // Look at the capcity of disk to determine its type.
  1266. //
  1267. for (index = NUMBER_OF_DRIVE_MEDIA_COMBINATIONS - 1; index >= 0; index--) {
  1268. //
  1269. // Walk the table backward untill the drive capacity holds all of the
  1270. // data and the bytes per setor are equal
  1271. //
  1272. if ((ULONG) (DriveMediaConstants[index].NumberOfHeads *
  1273. (DriveMediaConstants[index].MaximumTrack + 1) *
  1274. DriveMediaConstants[index].SectorsPerTrack *
  1275. DriveMediaConstants[index].BytesPerSector) <=
  1276. fdoExtension->CommonExtension.PartitionLength.LowPart &&
  1277. DriveMediaConstants[index].BytesPerSector ==
  1278. geometry->BytesPerSector) {
  1279. geometry->MediaType = DriveMediaConstants[index].MediaType;
  1280. geometry->TracksPerCylinder = DriveMediaConstants[index].NumberOfHeads;
  1281. geometry->SectorsPerTrack = DriveMediaConstants[index].SectorsPerTrack;
  1282. geometry->Cylinders.LowPart = DriveMediaConstants[index].MaximumTrack+1;
  1283. break;
  1284. }
  1285. }
  1286. if (index == -1) {
  1287. //
  1288. // Set the media type to unknow and zero the geometry information.
  1289. //
  1290. geometry->MediaType = Unknown;
  1291. } else {
  1292. //
  1293. // DMF check breaks the insight SCSI floppy, so its disabled for that case
  1294. //
  1295. PDISK_DATA diskData = (PDISK_DATA) fdoExtension->CommonExtension.DriverData;
  1296. // if (diskData->EnableDMF == TRUE) {
  1297. //
  1298. //check to see if DMF
  1299. //
  1300. PSCSI_REQUEST_BLOCK srb;
  1301. PVOID readData;
  1302. //
  1303. // Allocate a Srb for the read command.
  1304. //
  1305. readData = ExAllocatePool(NonPagedPool, geometry->BytesPerSector);
  1306. if (readData == NULL) {
  1307. return STATUS_NO_MEMORY;
  1308. }
  1309. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  1310. if (srb == NULL) {
  1311. ExFreePool(readData);
  1312. return STATUS_NO_MEMORY;
  1313. }
  1314. RtlZeroMemory(readData, geometry->BytesPerSector);
  1315. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1316. srb->CdbLength = 10;
  1317. srb->Cdb[0] = SCSIOP_READ;
  1318. srb->Cdb[5] = 0;
  1319. srb->Cdb[8] = (UCHAR) 1;
  1320. //
  1321. // Set timeout value.
  1322. //
  1323. srb->TimeOutValue = fdoExtension->TimeOutValue;
  1324. //
  1325. // Send the mode select data.
  1326. //
  1327. status = ClassSendSrbSynchronous(DeviceObject,
  1328. srb,
  1329. readData,
  1330. geometry->BytesPerSector,
  1331. FALSE
  1332. );
  1333. if (NT_SUCCESS(status)) {
  1334. char *pchar = (char *)readData;
  1335. pchar += 3; //skip 3 bytes jump code
  1336. // If the MSDMF3. signature is there then mark it as DMF diskette
  1337. if (RtlCompareMemory(pchar, "MSDMF3.", 7) == 7) {
  1338. diskData->IsDMF = TRUE;
  1339. }
  1340. }
  1341. ExFreePool(readData);
  1342. ExFreePool(srb);
  1343. // }// else
  1344. }
  1345. return status;
  1346. }
  1347. ULONG
  1348. DetermineDriveType(
  1349. PDEVICE_OBJECT DeviceObject
  1350. )
  1351. /*++
  1352. Routine Description:
  1353. The routine determines the device type so that the supported medias can be
  1354. determined. It does a mode sense for the default parameters. This code
  1355. assumes that the returned values are for the maximum device size.
  1356. Arguments:
  1357. DeviceObject - Supplies the device object to be tested.
  1358. Return Value:
  1359. None
  1360. --*/
  1361. {
  1362. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1363. PVOID modeData;
  1364. PDISK_DATA diskData = (PDISK_DATA) fdoExtension->CommonExtension.DriverData;
  1365. PMODE_FLEXIBLE_DISK_PAGE pageData;
  1366. ULONG length;
  1367. LONG index;
  1368. UCHAR numberOfHeads;
  1369. UCHAR sectorsPerTrack;
  1370. USHORT maximumTrack;
  1371. BOOLEAN applyFix = FALSE;
  1372. if (diskData->DriveType != DRIVE_TYPE_NONE) {
  1373. return(diskData->DriveType);
  1374. }
  1375. modeData = ExAllocatePool(NonPagedPoolCacheAligned, MODE_DATA_SIZE);
  1376. if (modeData == NULL) {
  1377. return(DRIVE_TYPE_NONE);
  1378. }
  1379. RtlZeroMemory(modeData, MODE_DATA_SIZE);
  1380. length = ClassModeSense(DeviceObject,
  1381. modeData,
  1382. MODE_DATA_SIZE,
  1383. MODE_PAGE_FLEXIBILE);
  1384. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1385. //
  1386. // Retry the request one more time
  1387. // in case of a check condition.
  1388. //
  1389. length = ClassModeSense(DeviceObject,
  1390. modeData,
  1391. MODE_DATA_SIZE,
  1392. MODE_PAGE_FLEXIBILE);
  1393. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1394. ExFreePool(modeData);
  1395. return(DRIVE_TYPE_NONE);
  1396. }
  1397. }
  1398. //
  1399. // Look for the flexible disk mode page.
  1400. //
  1401. pageData = ClassFindModePage( modeData,
  1402. length,
  1403. MODE_PAGE_FLEXIBILE,
  1404. TRUE);
  1405. //
  1406. // Make sure the page is returned and is large enough.
  1407. //
  1408. if ((pageData != NULL) &&
  1409. (pageData->PageLength + 2 >=
  1410. offsetof(MODE_FLEXIBLE_DISK_PAGE, StartWritePrecom))) {
  1411. //
  1412. // Pull out the heads, cylinders, and sectors.
  1413. //
  1414. numberOfHeads = pageData->NumberOfHeads;
  1415. maximumTrack = pageData->NumberOfCylinders[1];
  1416. maximumTrack |= pageData->NumberOfCylinders[0] << 8;
  1417. sectorsPerTrack = pageData->SectorsPerTrack;
  1418. //
  1419. // Convert from number of cylinders to maximum track.
  1420. //
  1421. maximumTrack--;
  1422. //
  1423. // Search for the maximum supported media. Based on the number of heads,
  1424. // sectors per track and number of cylinders
  1425. //
  1426. for (index = 0; index < NUMBER_OF_DRIVE_MEDIA_COMBINATIONS; index++) {
  1427. //
  1428. // Walk the table forward until the drive capacity holds all of the
  1429. // data and the bytes per setor are equal
  1430. //
  1431. if (DriveMediaConstants[index].NumberOfHeads == numberOfHeads &&
  1432. DriveMediaConstants[index].MaximumTrack == maximumTrack &&
  1433. DriveMediaConstants[index].SectorsPerTrack ==sectorsPerTrack) {
  1434. ExFreePool(modeData);
  1435. //
  1436. // index is now a drive media combination. Compare this to
  1437. // the maximum drive media type in the drive media table.
  1438. //
  1439. for (length = 0; length < NUMBER_OF_DRIVE_TYPES; length++) {
  1440. if (DriveMediaLimits[length].HighestDriveMediaType == index) {
  1441. return(length);
  1442. }
  1443. }
  1444. return(DRIVE_TYPE_NONE);
  1445. }
  1446. }
  1447. // If the maximum track is greater than 8 bits then divide the
  1448. // number of tracks by 3 and multiply the number of heads by 3.
  1449. // This is a special case for the 20.8 MB floppy.
  1450. //
  1451. if (!applyFix && maximumTrack >= 0x0100) {
  1452. applyFix = TRUE;
  1453. maximumTrack++;
  1454. maximumTrack /= 3;
  1455. maximumTrack--;
  1456. numberOfHeads *= 3;
  1457. } else {
  1458. ExFreePool(modeData);
  1459. return(DRIVE_TYPE_NONE);
  1460. }
  1461. }
  1462. ExFreePool(modeData);
  1463. return(DRIVE_TYPE_NONE);
  1464. }
  1465. BOOLEAN
  1466. FlCheckFormatParameters(
  1467. IN PDEVICE_OBJECT DeviceObject,
  1468. IN PFORMAT_PARAMETERS FormatParameters
  1469. )
  1470. /*++
  1471. Routine Description:
  1472. This routine checks the supplied format parameters to make sure that
  1473. they'll work on the drive to be formatted.
  1474. Arguments:
  1475. DeviceObject - Pointer to the device object to be formated.
  1476. FormatParameters - a pointer to the caller's parameters for the FORMAT.
  1477. Return Value:
  1478. TRUE if parameters are OK.
  1479. FALSE if the parameters are bad.
  1480. --*/
  1481. {
  1482. PDRIVE_MEDIA_CONSTANTS driveMediaConstants;
  1483. DRIVE_MEDIA_TYPE driveMediaType;
  1484. ULONG index;
  1485. //
  1486. // Get the device type.
  1487. //
  1488. index = DetermineDriveType(DeviceObject);
  1489. if (index == DRIVE_TYPE_NONE) {
  1490. //
  1491. // If the determine device type failed then just use the media type
  1492. // and try the parameters.
  1493. //
  1494. driveMediaType = Drive360Media160;
  1495. while (( DriveMediaConstants[driveMediaType].MediaType !=
  1496. FormatParameters->MediaType ) &&
  1497. ( driveMediaType < Drive288Media288) ) {
  1498. driveMediaType++;
  1499. }
  1500. } else {
  1501. //
  1502. // Figure out which entry in the DriveMediaConstants table to use.
  1503. //
  1504. driveMediaType =
  1505. DriveMediaLimits[index].HighestDriveMediaType;
  1506. while ( ( DriveMediaConstants[driveMediaType].MediaType !=
  1507. FormatParameters->MediaType ) &&
  1508. ( driveMediaType > DriveMediaLimits[index].
  1509. LowestDriveMediaType ) ) {
  1510. driveMediaType--;
  1511. }
  1512. }
  1513. if ( DriveMediaConstants[driveMediaType].MediaType !=
  1514. FormatParameters->MediaType ) {
  1515. return FALSE;
  1516. } else {
  1517. driveMediaConstants = &DriveMediaConstants[driveMediaType];
  1518. if ( ( FormatParameters->StartHeadNumber >
  1519. (ULONG)( driveMediaConstants->NumberOfHeads - 1 ) ) ||
  1520. ( FormatParameters->EndHeadNumber >
  1521. (ULONG)( driveMediaConstants->NumberOfHeads - 1 ) ) ||
  1522. ( FormatParameters->StartCylinderNumber >
  1523. driveMediaConstants->MaximumTrack ) ||
  1524. ( FormatParameters->EndCylinderNumber >
  1525. driveMediaConstants->MaximumTrack ) ||
  1526. ( FormatParameters->EndCylinderNumber <
  1527. FormatParameters->StartCylinderNumber ) ) {
  1528. return FALSE;
  1529. } else {
  1530. return TRUE;
  1531. }
  1532. }
  1533. }
  1534. NTSTATUS
  1535. FormatMedia(
  1536. PDEVICE_OBJECT DeviceObject,
  1537. MEDIA_TYPE MediaType
  1538. )
  1539. /*++
  1540. Routine Description:
  1541. This routine formats the floppy disk. The entire floppy is formated in
  1542. one shot.
  1543. Arguments:
  1544. DeviceObject - Supplies the device object to be tested.
  1545. Irp - Supplies a pointer to the requesting Irp.
  1546. MediaType - Supplies the media type format the device for.
  1547. Return Value:
  1548. Returns a status for the operation.
  1549. --*/
  1550. {
  1551. PVOID modeData;
  1552. PSCSI_REQUEST_BLOCK srb;
  1553. PMODE_FLEXIBLE_DISK_PAGE pageData;
  1554. DRIVE_MEDIA_TYPE driveMediaType;
  1555. PDRIVE_MEDIA_CONSTANTS driveMediaConstants;
  1556. ULONG length;
  1557. NTSTATUS status;
  1558. modeData = ExAllocatePool(NonPagedPoolCacheAligned, MODE_DATA_SIZE);
  1559. if (modeData == NULL) {
  1560. return(STATUS_INSUFFICIENT_RESOURCES);
  1561. }
  1562. RtlZeroMemory(modeData, MODE_DATA_SIZE);
  1563. length = ClassModeSense(DeviceObject,
  1564. modeData,
  1565. MODE_DATA_SIZE,
  1566. MODE_PAGE_FLEXIBILE);
  1567. if (length < sizeof(MODE_PARAMETER_HEADER)) {
  1568. ExFreePool(modeData);
  1569. return(STATUS_INVALID_DEVICE_REQUEST);
  1570. }
  1571. //
  1572. // Look for the flexible disk mode page.
  1573. //
  1574. pageData = ClassFindModePage( modeData, length, MODE_PAGE_FLEXIBILE, TRUE);
  1575. //
  1576. // Make sure the page is returned and is large enough.
  1577. //
  1578. if ((pageData == NULL) ||
  1579. (pageData->PageLength + 2 <
  1580. offsetof(MODE_FLEXIBLE_DISK_PAGE, StartWritePrecom))) {
  1581. ExFreePool(modeData);
  1582. return(STATUS_INVALID_DEVICE_REQUEST);
  1583. }
  1584. //
  1585. // Look for a drive media type which matches the requested media type.
  1586. //
  1587. //
  1588. //start from Drive120MMedia120M instead of Drive2080Media2080
  1589. //
  1590. for (driveMediaType = Drive120MMedia120M;
  1591. DriveMediaConstants[driveMediaType].MediaType != MediaType;
  1592. driveMediaType--) {
  1593. if (driveMediaType == Drive360Media160) {
  1594. ExFreePool(modeData);
  1595. return(STATUS_INVALID_PARAMETER);
  1596. }
  1597. }
  1598. driveMediaConstants = &DriveMediaConstants[driveMediaType];
  1599. if ((pageData->NumberOfHeads != driveMediaConstants->NumberOfHeads) ||
  1600. (pageData->SectorsPerTrack != driveMediaConstants->SectorsPerTrack) ||
  1601. ((pageData->NumberOfCylinders[0] != (UCHAR)(driveMediaConstants->MaximumTrack+1) >> 8) &&
  1602. (pageData->NumberOfCylinders[1] != (UCHAR)driveMediaConstants->MaximumTrack+1)) ||
  1603. (pageData->BytesPerSector[0] != driveMediaConstants->BytesPerSector >> 8 )) {
  1604. //
  1605. // Update the flexible parameters page with the new parameters.
  1606. //
  1607. pageData->NumberOfHeads = driveMediaConstants->NumberOfHeads;
  1608. pageData->SectorsPerTrack = driveMediaConstants->SectorsPerTrack;
  1609. pageData->NumberOfCylinders[0] = (UCHAR)(driveMediaConstants->MaximumTrack+1) >> 8;
  1610. pageData->NumberOfCylinders[1] = (UCHAR)driveMediaConstants->MaximumTrack+1;
  1611. pageData->BytesPerSector[0] = driveMediaConstants->BytesPerSector >> 8;
  1612. //
  1613. // Clear the mode parameter header.
  1614. //
  1615. RtlZeroMemory(modeData, sizeof(MODE_PARAMETER_HEADER));
  1616. //
  1617. // Set the length equal to the length returned for the flexible page.
  1618. //
  1619. length = pageData->PageLength + 2;
  1620. //
  1621. // Copy the page after the mode parameter header.
  1622. //
  1623. RtlMoveMemory((PCHAR) modeData + sizeof(MODE_PARAMETER_HEADER),
  1624. pageData,
  1625. length
  1626. );
  1627. length += sizeof(MODE_PARAMETER_HEADER);
  1628. //
  1629. // Allocate a Srb for the format command.
  1630. //
  1631. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  1632. if (srb == NULL) {
  1633. ExFreePool(modeData);
  1634. return(STATUS_INSUFFICIENT_RESOURCES);
  1635. }
  1636. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1637. srb->CdbLength = 6;
  1638. srb->Cdb[0] = SCSIOP_MODE_SELECT;
  1639. srb->Cdb[4] = (UCHAR) length;
  1640. //
  1641. // Set the PF bit.
  1642. //
  1643. srb->Cdb[1] |= 0x10;
  1644. //
  1645. // Set timeout value.
  1646. //
  1647. srb->TimeOutValue = 2;
  1648. //
  1649. // Send the mode select data.
  1650. //
  1651. status = ClassSendSrbSynchronous(DeviceObject,
  1652. srb,
  1653. modeData,
  1654. length,
  1655. TRUE
  1656. );
  1657. //
  1658. // The mode data not needed any more so free it.
  1659. //
  1660. ExFreePool(modeData);
  1661. if (!NT_SUCCESS(status)) {
  1662. ExFreePool(srb);
  1663. return(status);
  1664. }
  1665. } else {
  1666. //
  1667. // The mode data not needed any more so free it.
  1668. //
  1669. ExFreePool(modeData);
  1670. //
  1671. // Allocate a Srb for the format command.
  1672. //
  1673. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  1674. if (srb == NULL) {
  1675. return(STATUS_INSUFFICIENT_RESOURCES);
  1676. }
  1677. }
  1678. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1679. srb->CdbLength = 6;
  1680. srb->Cdb[0] = SCSIOP_FORMAT_UNIT;
  1681. //
  1682. // Set timeout value.
  1683. //
  1684. srb->TimeOutValue = 10 * 60;
  1685. status = ClassSendSrbSynchronous(DeviceObject,
  1686. srb,
  1687. NULL,
  1688. 0,
  1689. FALSE
  1690. );
  1691. ExFreePool(srb);
  1692. return(status);
  1693. }
  1694. VOID
  1695. ScsiFlopProcessError(
  1696. PDEVICE_OBJECT DeviceObject,
  1697. PSCSI_REQUEST_BLOCK Srb,
  1698. NTSTATUS *Status,
  1699. BOOLEAN *Retry
  1700. )
  1701. /*++
  1702. Routine Description:
  1703. This routine checks the type of error. If the error indicate the floppy
  1704. controller needs to be reinitialize a command is made to do it.
  1705. Arguments:
  1706. DeviceObject - Supplies a pointer to the device object.
  1707. Srb - Supplies a pointer to the failing Srb.
  1708. Status - Status with which the IRP will be completed.
  1709. Retry - Indication of whether the request will be retried.
  1710. Return Value:
  1711. None.
  1712. --*/
  1713. {
  1714. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension = DeviceObject->DeviceExtension;
  1715. PDISK_DATA diskData = (PDISK_DATA) fdoExtension->CommonExtension.DriverData;
  1716. PSENSE_DATA senseBuffer = Srb->SenseInfoBuffer;
  1717. PIO_STACK_LOCATION irpStack;
  1718. PIRP irp;
  1719. PSCSI_REQUEST_BLOCK srb;
  1720. LARGE_INTEGER largeInt;
  1721. PCOMPLETION_CONTEXT context;
  1722. PCDB cdb;
  1723. ULONG_PTR alignment;
  1724. ULONG majorFunction;
  1725. UNREFERENCED_PARAMETER(Status);
  1726. UNREFERENCED_PARAMETER(Retry);
  1727. largeInt.QuadPart = 1;
  1728. //
  1729. // Check the status. The initialization command only needs to be sent
  1730. // if UNIT ATTENTION or LUN NOT READY is returned.
  1731. //
  1732. if (!(Srb->SrbStatus & SRB_STATUS_AUTOSENSE_VALID)) {
  1733. //
  1734. // The drive does not require reinitialization.
  1735. //
  1736. return;
  1737. }
  1738. //
  1739. // Reset the drive type.
  1740. //
  1741. diskData->DriveType = DRIVE_TYPE_NONE;
  1742. diskData->IsDMF = FALSE;
  1743. fdoExtension->DiskGeometry.MediaType = Unknown;
  1744. if (fdoExtension->AdapterDescriptor->BusType == BusTypeUsb) {
  1745. // FLPYDISK.SYS never returns a non-zero value for the ChangeCount
  1746. // on an IOCTL_DISK_CHECK_VERIFY. Some things seem to work better
  1747. // if we do the same. In particular, FatVerifyVolume() can exit between
  1748. // the IOCTL_DISK_CHECK_VERIFY and the IOCTL_DISK_GET_DRIVE_GEOMETRY
  1749. // if a non-zero ChangeCount is returned, and this appears to cause
  1750. // issues formatting unformatted media in some situations.
  1751. //
  1752. // This is something that should probably be revisited at some point.
  1753. //
  1754. fdoExtension->MediaChangeCount = 0;
  1755. if (((senseBuffer->SenseKey & 0xf) == SCSI_SENSE_UNIT_ATTENTION) &&
  1756. (senseBuffer->AdditionalSenseCode == SCSI_ADSENSE_MEDIUM_CHANGED)) {
  1757. struct _START_STOP *startStopCdb;
  1758. DebugPrint((2,"Sending SCSIOP_START_STOP_UNIT\n"));
  1759. context = ExAllocatePool(NonPagedPool,
  1760. sizeof(COMPLETION_CONTEXT));
  1761. if (context == NULL) {
  1762. return;
  1763. }
  1764. srb = &context->Srb;
  1765. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1766. srb->SrbFlags = SRB_FLAGS_DISABLE_AUTOSENSE;
  1767. srb->CdbLength = 6;
  1768. startStopCdb = (struct _START_STOP *)srb->Cdb;
  1769. startStopCdb->OperationCode = SCSIOP_START_STOP_UNIT;
  1770. startStopCdb->Start = 1;
  1771. // A Start Stop Unit request has no transfer buffer.
  1772. // Set the request to IRP_MJ_FLUSH_BUFFERS when calling
  1773. // IoBuildAsynchronousFsdRequest() so that it ignores
  1774. // the buffer pointer and buffer length parameters.
  1775. //
  1776. majorFunction = IRP_MJ_FLUSH_BUFFERS;
  1777. } else if ((senseBuffer->SenseKey & 0xf) == SCSI_SENSE_MEDIUM_ERROR) {
  1778. // Return ERROR_UNRECOGNIZED_MEDIA instead of
  1779. // STATUS_DEVICE_DATA_ERROR to make shell happy.
  1780. //
  1781. *Status = STATUS_UNRECOGNIZED_MEDIA;
  1782. return;
  1783. } else {
  1784. return;
  1785. }
  1786. } else if (((senseBuffer->SenseKey & 0xf) == SCSI_SENSE_NOT_READY) &&
  1787. senseBuffer->AdditionalSenseCodeQualifier == SCSI_SENSEQ_INIT_COMMAND_REQUIRED ||
  1788. (senseBuffer->SenseKey & 0xf) == SCSI_SENSE_UNIT_ATTENTION) {
  1789. DebugPrint((1, "ScsiFlopProcessError: Reinitializing the floppy.\n"));
  1790. //
  1791. // Send the special mode sense command to enable writes on the
  1792. // floptical drive.
  1793. //
  1794. alignment = DeviceObject->AlignmentRequirement ?
  1795. DeviceObject->AlignmentRequirement : 1;
  1796. context = ExAllocatePool(
  1797. NonPagedPool,
  1798. sizeof(COMPLETION_CONTEXT) + 0x2a + (ULONG)alignment
  1799. );
  1800. if (context == NULL) {
  1801. //
  1802. // If there is not enough memory to fulfill this request,
  1803. // simply return. A subsequent retry will fail and another
  1804. // chance to start the unit.
  1805. //
  1806. return;
  1807. }
  1808. srb = &context->Srb;
  1809. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  1810. //
  1811. // Set the transfer length.
  1812. //
  1813. srb->DataTransferLength = 0x2a;
  1814. srb->SrbFlags = SRB_FLAGS_DATA_IN | SRB_FLAGS_DISABLE_AUTOSENSE | SRB_FLAGS_DISABLE_SYNCH_TRANSFER;
  1815. //
  1816. // The data buffer must be aligned.
  1817. //
  1818. srb->DataBuffer = (PVOID) (((ULONG_PTR) (context + 1) + (alignment - 1)) &
  1819. ~(alignment - 1));
  1820. //
  1821. // Build the start unit CDB.
  1822. //
  1823. srb->CdbLength = 6;
  1824. cdb = (PCDB)srb->Cdb;
  1825. cdb->MODE_SENSE.LogicalUnitNumber = srb->Lun;
  1826. cdb->MODE_SENSE.OperationCode = SCSIOP_MODE_SENSE;
  1827. cdb->MODE_SENSE.PageCode = 0x2e;
  1828. cdb->MODE_SENSE.AllocationLength = 0x2a;
  1829. majorFunction = IRP_MJ_READ;
  1830. } else {
  1831. return;
  1832. }
  1833. context->DeviceObject = DeviceObject;
  1834. //
  1835. // Write length to SRB.
  1836. //
  1837. srb->Length = SCSI_REQUEST_BLOCK_SIZE;
  1838. srb->Function = SRB_FUNCTION_EXECUTE_SCSI;
  1839. srb->TimeOutValue = fdoExtension->TimeOutValue;
  1840. //
  1841. // Build the asynchronous request
  1842. // to be sent to the port driver.
  1843. //
  1844. irp = IoBuildAsynchronousFsdRequest(majorFunction,
  1845. DeviceObject,
  1846. srb->DataBuffer,
  1847. srb->DataTransferLength,
  1848. &largeInt,
  1849. NULL);
  1850. if(irp == NULL) {
  1851. ExFreePool(context);
  1852. return;
  1853. }
  1854. IoSetCompletionRoutine(irp,
  1855. (PIO_COMPLETION_ROUTINE)ClassAsynchronousCompletion,
  1856. context,
  1857. TRUE,
  1858. TRUE,
  1859. TRUE);
  1860. ClassAcquireRemoveLock(DeviceObject, irp);
  1861. irpStack = IoGetNextIrpStackLocation(irp);
  1862. irpStack->MajorFunction = IRP_MJ_SCSI;
  1863. srb->OriginalRequest = irp;
  1864. //
  1865. // Save SRB address in next stack for port driver.
  1866. //
  1867. irpStack->Parameters.Others.Argument1 = (PVOID)srb;
  1868. //
  1869. // Can't release the remove lock yet - let ClassAsynchronousCompletion
  1870. // take care of that for us.
  1871. //
  1872. (VOID)IoCallDriver(fdoExtension->CommonExtension.LowerDeviceObject, irp);
  1873. return;
  1874. }
  1875. NTSTATUS
  1876. FlopticalFormatMedia(
  1877. PDEVICE_OBJECT DeviceObject,
  1878. PFORMAT_PARAMETERS Format
  1879. )
  1880. /*++
  1881. Routine Description:
  1882. This routine is used to do perform a format tracks for the 20.8 MB
  1883. floppy. Because the device does not support format tracks and the full
  1884. format takes a long time a write of zeros is done instead.
  1885. Arguments:
  1886. DeviceObject - Supplies the device object to be tested.
  1887. Format - Supplies the format parameters.
  1888. Return Value:
  1889. Returns a status for the operation.
  1890. --*/
  1891. {
  1892. IO_STATUS_BLOCK ioStatus;
  1893. PIRP irp;
  1894. KEVENT event;
  1895. LARGE_INTEGER offset;
  1896. ULONG length;
  1897. PVOID buffer;
  1898. PDRIVE_MEDIA_CONSTANTS driveMediaConstants;
  1899. NTSTATUS status;
  1900. driveMediaConstants = &DriveMediaConstants[Drive2080Media2080];
  1901. //
  1902. // Calculate the length of the buffer.
  1903. //
  1904. length = ((Format->EndCylinderNumber - Format->StartCylinderNumber) *
  1905. driveMediaConstants->NumberOfHeads +
  1906. Format->EndHeadNumber - Format->StartHeadNumber + 1) *
  1907. driveMediaConstants->SectorsPerTrack *
  1908. driveMediaConstants->BytesPerSector;
  1909. buffer = ExAllocatePool(NonPagedPoolCacheAligned, length);
  1910. if (buffer == NULL) {
  1911. return(STATUS_INSUFFICIENT_RESOURCES);
  1912. }
  1913. RtlZeroMemory(buffer, length);
  1914. offset.QuadPart =
  1915. (Format->StartCylinderNumber * driveMediaConstants->NumberOfHeads +
  1916. Format->StartHeadNumber) * driveMediaConstants->SectorsPerTrack *
  1917. driveMediaConstants->BytesPerSector;
  1918. //
  1919. // Set the event object to the unsignaled state.
  1920. // It will be used to signal request completion.
  1921. //
  1922. KeInitializeEvent(&event, NotificationEvent, FALSE);
  1923. //
  1924. // Build the synchronous request with data transfer.
  1925. //
  1926. irp = IoBuildSynchronousFsdRequest(
  1927. IRP_MJ_WRITE,
  1928. DeviceObject,
  1929. buffer,
  1930. length,
  1931. &offset,
  1932. &event,
  1933. &ioStatus);
  1934. if (irp != NULL) {
  1935. status = IoCallDriver(DeviceObject, irp);
  1936. if (status == STATUS_PENDING) {
  1937. //
  1938. // Wait for the request to complete if necessary.
  1939. //
  1940. KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
  1941. }
  1942. //
  1943. // If the call driver suceeded then set the status to the status block.
  1944. //
  1945. if (NT_SUCCESS(status)) {
  1946. status = ioStatus.Status;
  1947. }
  1948. } else {
  1949. status = STATUS_INSUFFICIENT_RESOURCES;
  1950. }
  1951. ExFreePool(buffer);
  1952. return(status);
  1953. }
  1954. #if 0
  1955. LONG
  1956. SFlopStringCmp (
  1957. PCHAR FirstStr,
  1958. PCHAR SecondStr,
  1959. ULONG Count
  1960. )
  1961. {
  1962. UCHAR first ,last;
  1963. if (Count) {
  1964. do {
  1965. //
  1966. // Get next char.
  1967. //
  1968. first = *FirstStr++;
  1969. last = *SecondStr++;
  1970. if (first != last) {
  1971. //
  1972. // If no match, try lower-casing.
  1973. //
  1974. if (first>='A' && first<='Z') {
  1975. first = first - 'A' + 'a';
  1976. }
  1977. if (last>='A' && last<='Z') {
  1978. last = last - 'A' + 'a';
  1979. }
  1980. if (first != last) {
  1981. //
  1982. // No match
  1983. //
  1984. return first - last;
  1985. }
  1986. }
  1987. }while (--Count && first);
  1988. }
  1989. return 0;
  1990. }
  1991. #endif
  1992. NTSTATUS
  1993. ScsiFlopRemoveDevice(
  1994. IN PDEVICE_OBJECT DeviceObject,
  1995. IN UCHAR Type
  1996. )
  1997. /*++
  1998. Routine Description:
  1999. This routine is responsible for releasing any resources in use by the
  2000. sfloppy driver. This routine is called
  2001. when all outstanding requests have been completed and the driver has
  2002. disappeared - no requests may be issued to the lower drivers.
  2003. Arguments:
  2004. DeviceObject - the device object being removed
  2005. Type - the type of remove operation (QUERY, REMOVE or CANCEL)
  2006. Return Value:
  2007. for a query - success if the device can be removed or a failure code
  2008. indiciating why not.
  2009. for a remove or cancel - STATUS_SUCCESS
  2010. --*/
  2011. {
  2012. PFUNCTIONAL_DEVICE_EXTENSION deviceExtension =
  2013. DeviceObject->DeviceExtension;
  2014. PDISK_DATA diskData = deviceExtension->CommonExtension.DriverData;
  2015. PAGED_CODE();
  2016. if((Type == IRP_MN_QUERY_REMOVE_DEVICE) ||
  2017. (Type == IRP_MN_CANCEL_REMOVE_DEVICE)) {
  2018. return STATUS_SUCCESS;
  2019. }
  2020. if (Type == IRP_MN_REMOVE_DEVICE){
  2021. if(deviceExtension->DeviceDescriptor) {
  2022. ExFreePool(deviceExtension->DeviceDescriptor);
  2023. deviceExtension->DeviceDescriptor = NULL;
  2024. }
  2025. if(deviceExtension->AdapterDescriptor) {
  2026. ExFreePool(deviceExtension->AdapterDescriptor);
  2027. deviceExtension->AdapterDescriptor = NULL;
  2028. }
  2029. if(deviceExtension->SenseData) {
  2030. ExFreePool(deviceExtension->SenseData);
  2031. deviceExtension->SenseData = NULL;
  2032. }
  2033. ClassDeleteSrbLookasideList(&deviceExtension->CommonExtension);
  2034. }
  2035. if(diskData->FloppyInterfaceString.Buffer != NULL) {
  2036. IoSetDeviceInterfaceState(
  2037. &(diskData->FloppyInterfaceString),
  2038. FALSE);
  2039. RtlFreeUnicodeString(&(diskData->FloppyInterfaceString));
  2040. RtlInitUnicodeString(&(diskData->FloppyInterfaceString), NULL);
  2041. }
  2042. if(Type == IRP_MN_REMOVE_DEVICE) {
  2043. IoGetConfigurationInformation()->FloppyCount--;
  2044. }
  2045. return STATUS_SUCCESS;
  2046. }
  2047. NTSTATUS
  2048. ScsiFlopStopDevice(
  2049. IN PDEVICE_OBJECT DeviceObject,
  2050. IN UCHAR Type
  2051. )
  2052. {
  2053. return STATUS_SUCCESS;
  2054. }
  2055. NTSTATUS
  2056. USBFlopGetMediaTypes(
  2057. IN PDEVICE_OBJECT DeviceObject,
  2058. IN PIRP Irp
  2059. )
  2060. {
  2061. /*++
  2062. Routine Description:
  2063. This routines determines the current or default geometry of the drive
  2064. for IOCTL_DISK_GET_DRIVE_GEOMETRY, or all currently supported geometries
  2065. of the drive (which is determined by its currently inserted media) for
  2066. IOCTL_DISK_GET_MEDIA_TYPES.
  2067. The returned geometries are determined by issuing a Read Format Capacities
  2068. request and then matching the returned {Number of Blocks, Block Length}
  2069. pairs in a table of known floppy geometries.
  2070. Arguments:
  2071. DeviceObject - Supplies the device object.
  2072. Irp - A IOCTL_DISK_GET_DRIVE_GEOMETRY or a IOCTL_DISK_GET_MEDIA_TYPES Irp.
  2073. If NULL, the device geometry is updated with the current device
  2074. geometry.
  2075. Return Value:
  2076. Status is returned.
  2077. --*/
  2078. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension;
  2079. PIO_STACK_LOCATION irpStack;
  2080. ULONG ioControlCode;
  2081. PDISK_GEOMETRY outputBuffer;
  2082. PDISK_GEOMETRY outputBufferEnd;
  2083. ULONG outputBufferLength;
  2084. PSCSI_REQUEST_BLOCK srb;
  2085. PVOID dataBuffer;
  2086. ULONG dataTransferLength;
  2087. struct _READ_FORMATTED_CAPACITIES *cdb;
  2088. PFORMATTED_CAPACITY_LIST capList;
  2089. NTSTATUS status;
  2090. fdoExtension = DeviceObject->DeviceExtension;
  2091. if (Irp != NULL) {
  2092. // Get the Irp parameters
  2093. //
  2094. irpStack = IoGetCurrentIrpStackLocation(Irp);
  2095. ioControlCode = irpStack->Parameters.DeviceIoControl.IoControlCode;
  2096. Irp->IoStatus.Information = 0;
  2097. outputBuffer = (PDISK_GEOMETRY) Irp->AssociatedIrp.SystemBuffer;
  2098. outputBufferLength = irpStack->Parameters.DeviceIoControl.OutputBufferLength;
  2099. if (outputBufferLength < sizeof(DISK_GEOMETRY))
  2100. {
  2101. return STATUS_BUFFER_TOO_SMALL;
  2102. }
  2103. // Pointer arithmetic to allow multiple DISK_GEOMETRY's to be returned.
  2104. // Rounds BufferEnd down to integral multiple of DISK_GEOMETRY structs.
  2105. //
  2106. outputBufferEnd = outputBuffer +
  2107. outputBufferLength / sizeof(DISK_GEOMETRY);
  2108. } else {
  2109. // No Irp to return the result in, just update the current geometry
  2110. // in the device extension.
  2111. //
  2112. ioControlCode = IOCTL_DISK_GET_DRIVE_GEOMETRY;
  2113. outputBuffer = NULL;
  2114. outputBufferEnd = NULL;
  2115. outputBufferLength = 0;
  2116. }
  2117. if (ioControlCode == IOCTL_DISK_GET_DRIVE_GEOMETRY) {
  2118. fdoExtension->DiskGeometry.MediaType = Unknown;
  2119. status = ClassReadDriveCapacity(DeviceObject);
  2120. if (!NT_SUCCESS(status))
  2121. {
  2122. // If the media is not recongized, we want to return the default
  2123. // geometry so that the media can be formatted. Unrecognized media
  2124. // causes SCSI_SENSE_MEDIUM_ERROR, which gets reported as
  2125. // STATUS_DEVICE_DATA_ERROR. Ignore these errors, but return other
  2126. // errors, such as STATUS_NO_MEDIA_IN_DEVICE.
  2127. //
  2128. if (status != STATUS_UNRECOGNIZED_MEDIA)
  2129. {
  2130. DebugPrint((2,"IOCTL_DISK_GET_DRIVE_GEOMETRY returns %08X\n", status));
  2131. return status;
  2132. }
  2133. }
  2134. }
  2135. // Allocate an SRB for the SCSIOP_READ_FORMATTED_CAPACITY request
  2136. //
  2137. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  2138. if (srb == NULL)
  2139. {
  2140. return STATUS_INSUFFICIENT_RESOURCES;
  2141. }
  2142. // Allocate a transfer buffer for the SCSIOP_READ_FORMATTED_CAPACITY request
  2143. // The length of the returned descriptor array is limited to a byte field
  2144. // in the capacity list header.
  2145. //
  2146. dataTransferLength = sizeof(FORMATTED_CAPACITY_LIST) +
  2147. 31 * sizeof(FORMATTED_CAPACITY_DESCRIPTOR);
  2148. ASSERT(dataTransferLength < 0x100);
  2149. dataBuffer = ExAllocatePool(NonPagedPool, dataTransferLength);
  2150. if (dataBuffer == NULL)
  2151. {
  2152. ExFreePool(srb);
  2153. return STATUS_INSUFFICIENT_RESOURCES;
  2154. }
  2155. // Initialize the SRB and CDB
  2156. //
  2157. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  2158. RtlZeroMemory(dataBuffer, dataTransferLength);
  2159. srb->CdbLength = sizeof(struct _READ_FORMATTED_CAPACITIES);
  2160. srb->TimeOutValue = fdoExtension->TimeOutValue;
  2161. cdb = (struct _READ_FORMATTED_CAPACITIES *)srb->Cdb;
  2162. cdb->OperationCode = SCSIOP_READ_FORMATTED_CAPACITY;
  2163. cdb->AllocationLength[1] = (UCHAR)dataTransferLength;
  2164. //
  2165. // Send down the SCSIOP_READ_FORMATTED_CAPACITY request
  2166. //
  2167. status = ClassSendSrbSynchronous(DeviceObject,
  2168. srb,
  2169. dataBuffer,
  2170. dataTransferLength,
  2171. FALSE);
  2172. capList = (PFORMATTED_CAPACITY_LIST)dataBuffer;
  2173. // If we don't get as much data as requested, it is not an error.
  2174. //
  2175. if (SRB_STATUS(srb->SrbStatus) == SRB_STATUS_DATA_OVERRUN)
  2176. {
  2177. status = STATUS_SUCCESS;
  2178. }
  2179. if (NT_SUCCESS(status) &&
  2180. srb->DataTransferLength >= sizeof(FORMATTED_CAPACITY_LIST) &&
  2181. capList->CapacityListLength &&
  2182. capList->CapacityListLength % sizeof(FORMATTED_CAPACITY_DESCRIPTOR) == 0)
  2183. {
  2184. ULONG NumberOfBlocks;
  2185. ULONG BlockLength;
  2186. ULONG count;
  2187. ULONG i, j;
  2188. LONG currentGeometry;
  2189. BOOLEAN capacityMatches[FLOPPY_CAPACITIES];
  2190. // Subtract the size of the Capacity List Header to get
  2191. // just the size of the Capacity List Descriptor array.
  2192. //
  2193. srb->DataTransferLength -= sizeof(FORMATTED_CAPACITY_LIST);
  2194. // Only look at the Capacity List Descriptors that were actually
  2195. // returned.
  2196. //
  2197. if (srb->DataTransferLength < capList->CapacityListLength)
  2198. {
  2199. count = srb->DataTransferLength /
  2200. sizeof(FORMATTED_CAPACITY_DESCRIPTOR);
  2201. }
  2202. else
  2203. {
  2204. count = capList->CapacityListLength /
  2205. sizeof(FORMATTED_CAPACITY_DESCRIPTOR);
  2206. }
  2207. // Updated only if a match is found for the first Capacity List
  2208. // Descriptor returned by the device.
  2209. //
  2210. currentGeometry = -1;
  2211. // Initialize the array of capacities that hit a match.
  2212. //
  2213. RtlZeroMemory(capacityMatches, sizeof(capacityMatches));
  2214. // Iterate over each Capacity List Descriptor returned from the device
  2215. // and record matching capacities in the capacity match array.
  2216. //
  2217. for (i = 0; i < count; i++)
  2218. {
  2219. NumberOfBlocks = (capList->Descriptors[i].NumberOfBlocks[0] << 24) +
  2220. (capList->Descriptors[i].NumberOfBlocks[1] << 16) +
  2221. (capList->Descriptors[i].NumberOfBlocks[2] << 8) +
  2222. (capList->Descriptors[i].NumberOfBlocks[3]);
  2223. BlockLength = (capList->Descriptors[i].BlockLength[0] << 16) +
  2224. (capList->Descriptors[i].BlockLength[1] << 8) +
  2225. (capList->Descriptors[i].BlockLength[2]);
  2226. // Given the {NumberOfBlocks, BlockLength} from this Capacity List
  2227. // Descriptor, find a matching entry in FloppyCapacities[].
  2228. //
  2229. for (j = 0; j < FLOPPY_CAPACITIES; j++)
  2230. {
  2231. if (NumberOfBlocks == FloppyCapacities[j].NumberOfBlocks &&
  2232. BlockLength == FloppyCapacities[j].BlockLength)
  2233. {
  2234. // A matching capacity was found, record it.
  2235. //
  2236. capacityMatches[j] = TRUE;
  2237. // A match was found for the first Capacity List
  2238. // Descriptor returned by the device.
  2239. //
  2240. if (i == 0)
  2241. {
  2242. currentGeometry = j;
  2243. }
  2244. } else if ((capList->Descriptors[i].Valid) &&
  2245. (BlockLength == FloppyCapacities[j].BlockLength)) {
  2246. ULONG inx;
  2247. ULONG mediaInx;
  2248. //
  2249. // Check if this is 32MB media type. 32MB media
  2250. // reports variable NumberOfBlocks. So we cannot
  2251. // use that to determine the drive type
  2252. //
  2253. inx = DetermineDriveType(DeviceObject);
  2254. if (inx != DRIVE_TYPE_NONE) {
  2255. mediaInx = DriveMediaLimits[inx].HighestDriveMediaType;
  2256. if ((DriveMediaConstants[mediaInx].MediaType)
  2257. == F3_32M_512) {
  2258. capacityMatches[j] = TRUE;
  2259. if (i == 0) {
  2260. currentGeometry = j;
  2261. }
  2262. }
  2263. }
  2264. }
  2265. }
  2266. }
  2267. // Default status is STATUS_UNRECOGNIZED_MEDIA, unless we return
  2268. // either STATUS_SUCCESS or STATUS_BUFFER_OVERFLOW.
  2269. //
  2270. status = STATUS_UNRECOGNIZED_MEDIA;
  2271. if (ioControlCode == IOCTL_DISK_GET_DRIVE_GEOMETRY) {
  2272. if (currentGeometry != -1)
  2273. {
  2274. // Update the current device geometry
  2275. //
  2276. fdoExtension->DiskGeometry = FloppyGeometries[currentGeometry];
  2277. //
  2278. // Calculate sector to byte shift.
  2279. //
  2280. WHICH_BIT(fdoExtension->DiskGeometry.BytesPerSector,
  2281. fdoExtension->SectorShift);
  2282. fdoExtension->CommonExtension.PartitionLength.QuadPart =
  2283. (LONGLONG)FloppyCapacities[currentGeometry].NumberOfBlocks *
  2284. FloppyCapacities[currentGeometry].BlockLength;
  2285. DebugPrint((2,"geometry is: %3d %2d %d %2d %4d %2d %08X\n",
  2286. fdoExtension->DiskGeometry.Cylinders.LowPart,
  2287. fdoExtension->DiskGeometry.MediaType,
  2288. fdoExtension->DiskGeometry.TracksPerCylinder,
  2289. fdoExtension->DiskGeometry.SectorsPerTrack,
  2290. fdoExtension->DiskGeometry.BytesPerSector,
  2291. fdoExtension->SectorShift,
  2292. fdoExtension->CommonExtension.PartitionLength.LowPart));
  2293. // Return the current device geometry
  2294. //
  2295. if (Irp != NULL)
  2296. {
  2297. *outputBuffer = FloppyGeometries[currentGeometry];
  2298. Irp->IoStatus.Information = sizeof(DISK_GEOMETRY);
  2299. }
  2300. status = STATUS_SUCCESS;
  2301. }
  2302. } else {
  2303. // Iterate over the capacities and return the geometry
  2304. // corresponding to each matching Capacity List Descriptor
  2305. // returned from the device.
  2306. //
  2307. // The resulting list should be in sorted ascending order,
  2308. // assuming that the FloppyGeometries[] array is in sorted
  2309. // ascending order.
  2310. //
  2311. for (i = 0; i < FLOPPY_CAPACITIES; i++)
  2312. {
  2313. if (capacityMatches[i] && FloppyCapacities[i].CanFormat)
  2314. {
  2315. if (outputBuffer < outputBufferEnd)
  2316. {
  2317. *outputBuffer++ = FloppyGeometries[i];
  2318. Irp->IoStatus.Information += sizeof(DISK_GEOMETRY);
  2319. DebugPrint((2,"geometry : %3d %2d %d %2d %4d\n",
  2320. FloppyGeometries[i].Cylinders.LowPart,
  2321. FloppyGeometries[i].MediaType,
  2322. FloppyGeometries[i].TracksPerCylinder,
  2323. FloppyGeometries[i].SectorsPerTrack,
  2324. FloppyGeometries[i].BytesPerSector));
  2325. status = STATUS_SUCCESS;
  2326. }
  2327. else
  2328. {
  2329. // We ran out of output buffer room before we ran out
  2330. // geometries to return.
  2331. //
  2332. status = STATUS_BUFFER_OVERFLOW;
  2333. }
  2334. }
  2335. }
  2336. }
  2337. }
  2338. else if (NT_SUCCESS(status))
  2339. {
  2340. // The SCSIOP_READ_FORMATTED_CAPACITY request was successful, but
  2341. // returned data does not appear valid.
  2342. //
  2343. status = STATUS_UNSUCCESSFUL;
  2344. }
  2345. ExFreePool(dataBuffer);
  2346. ExFreePool(srb);
  2347. return status;
  2348. }
  2349. NTSTATUS
  2350. USBFlopFormatTracks(
  2351. IN PDEVICE_OBJECT DeviceObject,
  2352. IN PIRP Irp
  2353. )
  2354. {
  2355. /*++
  2356. Routine Description:
  2357. This routines formats the specified tracks. If multiple tracks are
  2358. specified, each is formatted with a separate Format Unit request.
  2359. Arguments:
  2360. DeviceObject - Supplies the device object.
  2361. Irp - A IOCTL_DISK_FORMAT_TRACKS Irp.
  2362. Return Value:
  2363. Status is returned.
  2364. --*/
  2365. PFUNCTIONAL_DEVICE_EXTENSION fdoExtension;
  2366. PIO_STACK_LOCATION irpStack;
  2367. PFORMAT_PARAMETERS formatParameters;
  2368. PDISK_GEOMETRY geometry;
  2369. PFORMATTED_CAPACITY capacity;
  2370. PSCSI_REQUEST_BLOCK srb;
  2371. PFORMAT_UNIT_PARAMETER_LIST parameterList;
  2372. PCDB12FORMAT cdb;
  2373. ULONG i;
  2374. ULONG cylinder, head;
  2375. NTSTATUS status;
  2376. fdoExtension = DeviceObject->DeviceExtension;
  2377. // Get the Irp parameters
  2378. //
  2379. irpStack = IoGetCurrentIrpStackLocation(Irp);
  2380. if (irpStack->Parameters.DeviceIoControl.InputBufferLength <
  2381. sizeof(FORMAT_PARAMETERS))
  2382. {
  2383. return STATUS_INVALID_PARAMETER;
  2384. }
  2385. formatParameters = (PFORMAT_PARAMETERS)Irp->AssociatedIrp.SystemBuffer;
  2386. // Find the geometry / capacity entries corresponding to the format
  2387. // parameters MediaType
  2388. //
  2389. geometry = NULL;
  2390. capacity = NULL;
  2391. for (i=0; i<FLOPPY_CAPACITIES; i++)
  2392. {
  2393. if (FloppyGeometries[i].MediaType == formatParameters->MediaType)
  2394. {
  2395. geometry = &FloppyGeometries[i];
  2396. capacity = &FloppyCapacities[i];
  2397. break;
  2398. }
  2399. }
  2400. if (geometry == NULL)
  2401. {
  2402. return STATUS_INVALID_PARAMETER;
  2403. }
  2404. // Check if the format parameters are valid
  2405. //
  2406. if ((formatParameters->StartCylinderNumber >
  2407. geometry->Cylinders.LowPart - 1) ||
  2408. (formatParameters->EndCylinderNumber >
  2409. geometry->Cylinders.LowPart - 1) ||
  2410. (formatParameters->StartHeadNumber >
  2411. geometry->TracksPerCylinder - 1) ||
  2412. (formatParameters->EndHeadNumber >
  2413. geometry->TracksPerCylinder - 1) ||
  2414. (formatParameters->StartCylinderNumber >
  2415. formatParameters->EndCylinderNumber) ||
  2416. (formatParameters->StartHeadNumber >
  2417. formatParameters->EndHeadNumber))
  2418. {
  2419. return STATUS_INVALID_PARAMETER;
  2420. }
  2421. // Don't low level format LS-120 media, Imation says it's best to not
  2422. // do this.
  2423. //
  2424. if ((formatParameters->MediaType == F3_120M_512) ||
  2425. (formatParameters->MediaType == F3_240M_512) ||
  2426. (formatParameters->MediaType == F3_32M_512))
  2427. {
  2428. return STATUS_SUCCESS;
  2429. }
  2430. // Allocate an SRB for the SCSIOP_FORMAT_UNIT request
  2431. //
  2432. srb = ExAllocatePool(NonPagedPool, SCSI_REQUEST_BLOCK_SIZE);
  2433. if (srb == NULL)
  2434. {
  2435. return STATUS_INSUFFICIENT_RESOURCES;
  2436. }
  2437. // Allocate a transfer buffer for the SCSIOP_FORMAT_UNIT parameter list
  2438. //
  2439. parameterList = ExAllocatePool(NonPagedPool,
  2440. sizeof(FORMAT_UNIT_PARAMETER_LIST));
  2441. if (parameterList == NULL)
  2442. {
  2443. ExFreePool(srb);
  2444. return STATUS_INSUFFICIENT_RESOURCES;
  2445. }
  2446. // Initialize the parameter list
  2447. //
  2448. RtlZeroMemory(parameterList, sizeof(FORMAT_UNIT_PARAMETER_LIST));
  2449. parameterList->DefectListHeader.SingleTrack = 1;
  2450. parameterList->DefectListHeader.DisableCert = 1; // TEAC requires this set
  2451. parameterList->DefectListHeader.FormatOptionsValid = 1;
  2452. parameterList->DefectListHeader.DefectListLengthLsb = 8;
  2453. parameterList->FormatDescriptor.NumberOfBlocks[0] =
  2454. (UCHAR)((capacity->NumberOfBlocks >> 24) & 0xFF);
  2455. parameterList->FormatDescriptor.NumberOfBlocks[1] =
  2456. (UCHAR)((capacity->NumberOfBlocks >> 16) & 0xFF);
  2457. parameterList->FormatDescriptor.NumberOfBlocks[2] =
  2458. (UCHAR)((capacity->NumberOfBlocks >> 8) & 0xFF);
  2459. parameterList->FormatDescriptor.NumberOfBlocks[3] =
  2460. (UCHAR)(capacity->NumberOfBlocks & 0xFF);
  2461. parameterList->FormatDescriptor.BlockLength[0] =
  2462. (UCHAR)((capacity->BlockLength >> 16) & 0xFF);
  2463. parameterList->FormatDescriptor.BlockLength[1] =
  2464. (UCHAR)((capacity->BlockLength >> 8) & 0xFF);
  2465. parameterList->FormatDescriptor.BlockLength[2] =
  2466. (UCHAR)(capacity->BlockLength & 0xFF);
  2467. for (cylinder = formatParameters->StartCylinderNumber;
  2468. cylinder <= formatParameters->EndCylinderNumber;
  2469. cylinder++)
  2470. {
  2471. for (head = formatParameters->StartHeadNumber;
  2472. head <= formatParameters->EndHeadNumber;
  2473. head++)
  2474. {
  2475. // Initialize the SRB and CDB
  2476. //
  2477. RtlZeroMemory(srb, SCSI_REQUEST_BLOCK_SIZE);
  2478. srb->CdbLength = sizeof(CDB12FORMAT);
  2479. srb->TimeOutValue = fdoExtension->TimeOutValue;
  2480. cdb = (PCDB12FORMAT)srb->Cdb;
  2481. cdb->OperationCode = SCSIOP_FORMAT_UNIT;
  2482. cdb->DefectListFormat = 7;
  2483. cdb->FmtData = 1;
  2484. cdb->TrackNumber = (UCHAR)cylinder;
  2485. cdb->ParameterListLengthLsb = sizeof(FORMAT_UNIT_PARAMETER_LIST);
  2486. parameterList->DefectListHeader.Side = (UCHAR)head;
  2487. //
  2488. // Send down the SCSIOP_FORMAT_UNIT request
  2489. //
  2490. status = ClassSendSrbSynchronous(DeviceObject,
  2491. srb,
  2492. parameterList,
  2493. sizeof(FORMAT_UNIT_PARAMETER_LIST),
  2494. TRUE);
  2495. if (!NT_SUCCESS(status))
  2496. {
  2497. break;
  2498. }
  2499. }
  2500. if (!NT_SUCCESS(status))
  2501. {
  2502. break;
  2503. }
  2504. }
  2505. if (NT_SUCCESS(status) && formatParameters->StartCylinderNumber == 0)
  2506. {
  2507. // Update the device geometry
  2508. //
  2509. DebugPrint((2,"geometry was: %3d %2d %d %2d %4d %2d %08X\n",
  2510. fdoExtension->DiskGeometry.Cylinders.LowPart,
  2511. fdoExtension->DiskGeometry.MediaType,
  2512. fdoExtension->DiskGeometry.TracksPerCylinder,
  2513. fdoExtension->DiskGeometry.SectorsPerTrack,
  2514. fdoExtension->DiskGeometry.BytesPerSector,
  2515. fdoExtension->SectorShift,
  2516. fdoExtension->CommonExtension.PartitionLength.LowPart));
  2517. fdoExtension->DiskGeometry = *geometry;
  2518. //
  2519. // Calculate sector to byte shift.
  2520. //
  2521. WHICH_BIT(fdoExtension->DiskGeometry.BytesPerSector,
  2522. fdoExtension->SectorShift);
  2523. fdoExtension->CommonExtension.PartitionLength.QuadPart =
  2524. (LONGLONG)capacity->NumberOfBlocks *
  2525. capacity->BlockLength;
  2526. DebugPrint((2,"geometry is: %3d %2d %d %2d %4d %2d %08X\n",
  2527. fdoExtension->DiskGeometry.Cylinders.LowPart,
  2528. fdoExtension->DiskGeometry.MediaType,
  2529. fdoExtension->DiskGeometry.TracksPerCylinder,
  2530. fdoExtension->DiskGeometry.SectorsPerTrack,
  2531. fdoExtension->DiskGeometry.BytesPerSector,
  2532. fdoExtension->SectorShift,
  2533. fdoExtension->CommonExtension.PartitionLength.LowPart));
  2534. }
  2535. // Free everything we allocated
  2536. //
  2537. ExFreePool(parameterList);
  2538. ExFreePool(srb);
  2539. return status;
  2540. }