home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 February / Chip_2004-02_cd1.bin / zkuste / konfig / download / msic / Help / Int / MiTeC_WinIOCTL.int < prev    next >
Text File  |  2003-08-26  |  15KB  |  373 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {       MiTeC System Information Component              }
  4. {               Windows IOCTL                           }
  5. {           version 8.3 for Delphi 5,6,7                }
  6. {                                                       }
  7. {       Copyright ⌐ 1997,2003 Michal Mutl               }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. {$INCLUDE MITEC_DEF.INC}
  12.  
  13. unit MiTeC_WinIOCTL;
  14.  
  15. interface                                       
  16.  
  17. uses Windows, SysUtils;
  18.  
  19. type
  20.   TSrbIoControl = packed record
  21.     HeaderLength : ULONG;
  22.     Signature    : Array[0..7] of Char;
  23.     Timeout      : ULONG;
  24.     ControlCode  : ULONG;
  25.     ReturnCode   : ULONG;
  26.     Length       : ULONG;
  27.   end;
  28.   SRB_IO_CONTROL = TSrbIoControl;
  29.   PSrbIoControl = ^TSrbIoControl;
  30.  
  31.   TIDERegs = packed record
  32.     bFeaturesReg     : Byte; // Used for specifying SMART "commands".
  33.     bSectorCountReg  : Byte; // IDE sector count register
  34.     bSectorNumberReg : Byte; // IDE sector number register
  35.     bCylLowReg       : Byte; // IDE low order cylinder value
  36.     bCylHighReg      : Byte; // IDE high order cylinder value
  37.     bDriveHeadReg    : Byte; // IDE drive/head register
  38.     bCommandReg      : Byte; // Actual IDE command.
  39.     bReserved        : Byte; // reserved.  Must be zero.
  40.   end;
  41.   IDEREGS   = TIDERegs;
  42.   PIDERegs  = ^TIDERegs;
  43.  
  44.   TSendCmdInParams = packed record
  45.     cBufferSize  : DWORD;
  46.     irDriveRegs  : TIDERegs;
  47.     bDriveNumber : Byte;
  48.     bReserved    : Array[0..2] of Byte;
  49.     dwReserved   : Array[0..3] of DWORD;
  50.     bBuffer      : Array[0..0] of Byte;
  51.   end;
  52.   SENDCMDINPARAMS   = TSendCmdInParams;
  53.   PSendCmdInParams  = ^TSendCmdInParams;
  54.  
  55.    // Status returned from driver
  56.   TDriverStatus = record
  57.     bDriverError,  //  Error code from driver, or 0 if no error.
  58.     bIDEStatus: BYTE;    //  Contents of IDE Error register.
  59.                         //  Only valid when bDriverError is SMART_IDE_ERROR.
  60.     bReserved: array[0..1] of BYTE;  //  Reserved for future expansion.
  61.     dwReserved: array[0..1] of DWORD;  //  Reserved for future expansion.
  62.   end;
  63.   DRIVERSTATUS = TDriverStatus;
  64.   PDriverStatus = ^TDriverStatus;
  65.  
  66.      // Structure returned by PhysicalDrive IOCTL for several commands
  67.   TSendCmdOutParams = record
  68.     cBufferSize: DWORD;   //  Size of bBuffer in bytes
  69.     DriverStatus: TDriverStatus;  //  Driver status structure.
  70.     bBuffer: array[0..0] of BYTE;    //  Buffer of arbitrary length in which to store the data read from the                                                       // drive.
  71.   end;
  72.   SENDCMDOUTPARAMS = TSendCmdOutParams;
  73.   PSendCmdOutParams = ^TSendCmdOutParams;
  74.  
  75.   TIdSector = packed record
  76.     wGenConfig                 : Word;
  77.     wNumCyls                   : Word;
  78.     wReserved                  : Word;
  79.     wNumHeads                  : Word;
  80.     wUnformattedBytesPerTrack  : Word;
  81.     wUnformattedBytesPerSector : Word;
  82.     wSectorsPerTrack           : Word;
  83.     wVendorUnique              : Array[0..2] of Word;
  84.     sSerialNumber              : Array[0..19] of Char;
  85.     wBufferType                : Word;
  86.     wBufferSize                : Word;
  87.     wECCSize                   : Word;
  88.     sFirmwareRev               : Array[0..7] of Char;
  89.     sModelNumber               : Array[0..39] of Char;
  90.     wMoreVendorUnique          : Word;
  91.     wDoubleWordIO              : Word;
  92.     wCapabilities              : Word;
  93.     wReserved1                 : Word;
  94.     wPIOTiming                 : Word;
  95.     wDMATiming                 : Word;
  96.     wBS                        : Word;
  97.     wNumCurrentCyls            : Word;
  98.     wNumCurrentHeads           : Word;
  99.     wNumCurrentSectorsPerTrack : Word;
  100.     ulCurrentSectorCapacity    : ULONG;
  101.     wMultSectorStuff           : Word;
  102.     ulTotalAddressableSectors  : ULONG;
  103.     wSingleWordDMA             : Word;
  104.     wMultiWordDMA              : Word;
  105.     AdvancedPIOModes,
  106.     Reserved4,
  107.     MinimumMWXferCycleTime,
  108.     RecommendedMWXferCycleTime,
  109.     MinimumPIOCycleTime,
  110.     MinimumPIOCycleTimeIORDY: Word;
  111.     Reserved5: array[0..1] of Word;
  112.     ReleaseTimeOverlapped,
  113.     ReleaseTimeServiceCommand,
  114.     MajorRevision,
  115.     MinorRevision: Word;
  116.     Reserved6: array[0..49] of Word;
  117.     SpecialFunctionsEnabled: Word;
  118.     Reserved7: array[0..127] of Word;
  119.   end;
  120.   PIdSector = ^TIdSector;
  121.  
  122.   TGetVersionOutParams = record
  123.     bVersion,      // Binary driver version.
  124.     bRevision,     // Binary driver revision.
  125.     bReserved,     // Not used.
  126.     bIDEDeviceMap: BYTE; // Bit map of IDE devices.
  127.     fCapabilities: DWORD; // Bit mask of driver capabilities.
  128.     dwReserved: array[0..3] of DWORD; // For future use.
  129.   end;
  130.   GETVERSIONOUTPARAMS = TGetVersionOutParams;
  131.   PGetVersionOutParams = ^TGetVersionOutParams;
  132.  
  133.   TMediaType = (
  134.     Unknown,                // Format is unknown
  135.     F5_1Pt2_512,            // 5.25", 1.2MB,  512 bytes/sector
  136.     F3_1Pt44_512,           // 3.5",  1.44MB, 512 bytes/sector
  137.     F3_2Pt88_512,           // 3.5",  2.88MB, 512 bytes/sector
  138.     F3_20Pt8_512,           // 3.5",  20.8MB, 512 bytes/sector
  139.     F3_720_512,             // 3.5",  720KB,  512 bytes/sector
  140.     F5_360_512,             // 5.25", 360KB,  512 bytes/sector
  141.     F5_320_512,             // 5.25", 320KB,  512 bytes/sector
  142.     F5_320_1024,            // 5.25", 320KB,  1024 bytes/sector
  143.     F5_180_512,             // 5.25", 180KB,  512 bytes/sector
  144.     F5_160_512,             // 5.25", 160KB,  512 bytes/sector
  145.     RemovableMedia,         // Removable media other than floppy
  146.     FixedMedia,             // Fixed hard disk media
  147.     F3_120M_512,            // 3.5", 120M Floppy
  148.     F3_640_512,             // 3.5" ,  640KB,  512 bytes/sector
  149.     F5_640_512,             // 5.25",  640KB,  512 bytes/sector
  150.     F5_720_512,             // 5.25",  720KB,  512 bytes/sector
  151.     F3_1Pt2_512,            // 3.5" ,  1.2Mb,  512 bytes/sector
  152.     F3_1Pt23_1024,          // 3.5" ,  1.23Mb, 1024 bytes/sector
  153.     F5_1Pt23_1024,          // 5.25",  1.23MB, 1024 bytes/sector
  154.     F3_128Mb_512,           // 3.5" MO 128Mb   512 bytes/sector
  155.     F3_230Mb_512,           // 3.5" MO 230Mb   512 bytes/sector
  156.     F8_256_128,             // 8",     256KB,  128 bytes/sector
  157.     F3_200Mb_512,           // 3.5",   200M Floppy (HiFD)
  158.     F3_240M_512,            // 3.5",   240Mb Floppy (HiFD)
  159.     F3_32M_512);            // 3.5",   32Mb Floppy
  160.   MEDIA_TYPE = TMediaType;
  161.   PMediaType = ^TMediaType;
  162.  
  163.   TPartitionInformation = record
  164.     StartingOffset: LARGE_INTEGER;
  165.     PartitionLength: LARGE_INTEGER;
  166.     HiddenSectors: DWORD;
  167.     PartitionNumber: DWORD;
  168.     PartitionType: BYTE;
  169.     BootIndicator: ByteBool;
  170.     RecognizedPartition: ByteBool;
  171.     RewritePartition: ByteBool;
  172.   end;
  173.   PARTITION_INFORMATION = TPartitionInformation;
  174.   PPartitionInformation = ^TPartitionInformation;
  175.  
  176.   TDriveLayoutInformation = record
  177.     PartitionCount: DWORD;
  178.     Signature: DWORD;
  179.     PartitionEntry: array [0..0] of TPartitionInformation;
  180.   end;
  181.   DRIVE_LAYOUT_INFORMATION = TDriveLayoutInformation;
  182.   PDriveLayoutInformation = ^TDriveLayoutInformation;
  183.  
  184.   TDiskGeometry = record
  185.     Cylinders: LARGE_INTEGER;
  186.     MediaType: TMediaType;
  187.     TracksPerCylinder: DWORD;
  188.     SectorsPerTrack: DWORD;
  189.     BytesPerSector: DWORD;
  190.   end;
  191.   DISK_GEOMETRY = TDiskGeometry;
  192.   PDiskGeometry = ^TDiskGeometry;
  193.  
  194.   TDiskControllerNumber = record
  195.     ControllerNumber: DWORD;
  196.     DiskNumber: DWORD;
  197.   end;
  198.   DISK_CONTROLLER_NUMBER = TDiskControllerNumber;
  199.   PDiskControllerNumber = ^TDiskControllerNumber;
  200.  
  201.   TScsiPassThrough = record
  202.     Length             : Word;
  203.     ScsiStatus         : Byte;
  204.     PathId             : Byte;
  205.     TargetId           : Byte;
  206.     Lun                : Byte;
  207.     CdbLength          : Byte;
  208.     SenseInfoLength    : Byte;
  209.     DataIn             : Byte;
  210.     DataTransferLength : ULONG;
  211.     TimeOutValue       : ULONG;
  212.     DataBufferOffset   : DWORD;
  213.     SenseInfoOffset    : ULONG;
  214.     Cdb                : Array[0..15] of Byte;
  215.   end;
  216.  
  217.   TScsiPassThroughWithBuffers = record
  218.     spt : TScsiPassThrough;
  219.     bSenseBuf : Array[0..31] of Byte;
  220.     bDataBuf : Array[0..191] of Byte;
  221.   end;
  222.  
  223.   TCDB6GENERIC = record
  224.     OperationCode: Byte;
  225.     Immediate: Byte;
  226.     CommandUniqueBits: Byte;
  227.     LogicalUnitNumber: Byte;
  228.     CommandUniqueBytes: array[0..2] of Byte;
  229.     Link: Byte;
  230.     Flag: Byte;
  231.     Reserved: Byte;
  232.     VendorUnique: Byte;
  233.   end;
  234.  
  235.  
  236. const
  237.    //  Valid values for the bCommandReg member of IDEREGS.
  238.   IDE_ATAPI_IDENTIFY = $A1;  //  Returns ID sector for ATAPI.
  239.   IDE_ATA_IDENTIFY = $EC;  //  Returns ID sector for ATA.
  240.  
  241.   IDENTIFY_BUFFER_SIZE       = 512;
  242.  
  243.   DFP_GET_VERSION = $00074080;
  244.   DFP_SEND_DRIVE_COMMAND = $0007c084;
  245.   DFP_RECEIVE_DRIVE_DATA = $0007c088;
  246.  
  247.   METHOD_BUFFERED   = 0;
  248.  
  249.   FILE_DEVICE_UNKNOWN = $00000022;
  250.   FILE_DEVICE_SCSI = $0000001B;
  251.   FILE_DEVICE_DISK = $00000007;
  252.   FILE_DEVICE_FILE_SYSTEM = $00000009;
  253.   FILE_DEVICE_CONTROLLER = $00000004;
  254.  
  255.   FILE_ANY_ACCESS     = 0;
  256.   FILE_SPECIAL_ACCESS = FILE_ANY_ACCESS;
  257.   FILE_READ_ACCESS    = $0001;           // file & pipe
  258.   FILE_WRITE_ACCESS   = $0002;           // file & pipe
  259.  
  260.   SCSI_IOCTL_DATA_OUT =         0;
  261.   SCSI_IOCTL_DATA_IN  =         1;
  262.   SCSI_IOCTL_DATA_UNSPECIFIED = 2;
  263.  
  264.   CDB_INQUIRY_EVPD = 1;
  265.  
  266.   SCSIOP_INQUIRY = $12;
  267.  
  268.   CDB6GENERIC_LENGTH = 6;
  269.  
  270.   IOCTL_SCSI_BASE = FILE_DEVICE_CONTROLLER;
  271.   IOCTL_SCSI_MINIPORT_IDENTIFY = ((FILE_DEVICE_SCSI shl 16) + $0501); //$001b0501
  272.   IOCTL_SCSI_MINIPORT = ((IOCTL_SCSI_BASE shl 16) or ($0402 shl 2) or METHOD_BUFFERED or ((FILE_READ_ACCESS or FILE_WRITE_ACCESS) shl 14));
  273.   IOCTL_SCSI_PASS_THROUGH = ((IOCTL_SCSI_BASE shl 16) or ($0401 shl 2) or METHOD_BUFFERED or ((FILE_READ_ACCESS or FILE_WRITE_ACCESS) shl 14));
  274.   IOCTL_SCSI_GET_INQUIRY_DATA = ((IOCTL_SCSI_BASE shl 16) or ($0403 shl 2) or METHOD_BUFFERED or (FILE_ANY_ACCESS shl 14));
  275.   IOCTL_SCSI_GET_CAPABILITIES = ((IOCTL_SCSI_BASE shl 16) or ($0404 shl 2) or METHOD_BUFFERED or (FILE_ANY_ACCESS shl 14));
  276.   IOCTL_SCSI_PASS_THROUGH_DIRECT = ((IOCTL_SCSI_BASE shl 16) or ($0405 shl 2) or METHOD_BUFFERED or ((FILE_READ_ACCESS or FILE_WRITE_ACCESS) shl 14));
  277.   IOCTL_SCSI_GET_ADDRESS = ((IOCTL_SCSI_BASE shl 16) or ($0406 shl 2) or METHOD_BUFFERED or (FILE_ANY_ACCESS shl 14));
  278.   IOCTL_SCSI_RESCAN_BUS = ((IOCTL_SCSI_BASE shl 16) or ($0407 shl 2) or METHOD_BUFFERED or (FILE_ANY_ACCESS shl 14));
  279.   IOCTL_SCSI_GET_DUMP_POINTERS = ((IOCTL_SCSI_BASE shl 16) or ($0408 shl 2) or METHOD_BUFFERED or (FILE_ANY_ACCESS shl 14));
  280.  
  281.   IOCTL_DISK_BASE = FILE_DEVICE_DISK;
  282.   IOCTL_DISK_GET_DRIVE_GEOMETRY = (
  283.     (IOCTL_DISK_BASE shl 16) or (FILE_ANY_ACCESS shl 14) or
  284.     ($0000 shl 2) or METHOD_BUFFERED);
  285.   IOCTL_DISK_GET_PARTITION_INFO = (
  286.     (IOCTL_DISK_BASE shl 16) or (FILE_READ_ACCESS shl 14) or
  287.     ($0001 shl 2) or METHOD_BUFFERED);
  288.   IOCTL_DISK_GET_DRIVE_LAYOUT = (
  289.     (IOCTL_DISK_BASE shl 16) or (FILE_READ_ACCESS shl 14) or
  290.     ($0003 shl 2) or METHOD_BUFFERED);
  291.   IOCTL_VOLUME_BASE = DWORD('V');
  292.   IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = (
  293.     (IOCTL_VOLUME_BASE shl 16) or (FILE_ANY_ACCESS shl 14) or
  294.     (0 shl 2) or METHOD_BUFFERED);
  295.   IOCTL_DISK_CONTROLLER_NUMBER = (
  296.     (IOCTL_DISK_BASE shl 16) or (FILE_ANY_ACCESS shl 14) or
  297.     ($0011 shl 2) or METHOD_BUFFERED);
  298.  
  299.   FSCTL_LOCK_VOLUME = ((FILE_DEVICE_FILE_SYSTEM shl 16) or (FILE_ANY_ACCESS shl 14) or (6 shl 2) or METHOD_BUFFERED);
  300.  
  301.   FSCTL_UNLOCK_VOLUME = (
  302.     (FILE_DEVICE_FILE_SYSTEM shl 16) or (FILE_ANY_ACCESS shl 14) or
  303.     (7 shl 2) or METHOD_BUFFERED);
  304.  
  305.  
  306.   VWIN32_DIOC_DOS_IOCTL21 = 1; // INT 21h - 4400h, 4411h
  307.   VWIN32_DIOC_DOS_IOCTL25 = 2; // INT 25h - Disk Read
  308.   VWIN32_DIOC_DOS_IOCTL26 = 3; // INT 26h - Disk Write
  309.   VWIN32_DIOC_DOS_IOCTL13 = 4; // INT 13h
  310.   VWIN32_DIOC_DOS_DRIVEINFO = 6; // INT 21h - Function 730X commands
  311.  
  312.    //  Bits returned in the fCapabilities member of GETVERSIONOUTPARAMS
  313.   CAP_IDE_ID_FUNCTION = 1;  // ATA ID command supported
  314.   CAP_IDE_ATAPI_ID = 2;  // ATAPI ID command supported
  315.   CAP_IDE_EXECUTE_SMART_FUNCTION = 4;  // SMART commannds supported
  316.  
  317.   DataSize = sizeof(TSendCmdInParams)-1+IDENTIFY_BUFFER_SIZE;
  318.   BufferSize = SizeOf(SRB_IO_CONTROL)+DataSize;
  319.   W9xBufferSize = IDENTIFY_BUFFER_SIZE+16;
  320.  
  321.   PARTITION_ENTRY_UNUSED    = $00; // Entry unused
  322.   PARTITION_FAT_12          = $01; // 12-bit FAT entries
  323.   PARTITION_XENIX_1         = $02; // Xenix
  324.   PARTITION_XENIX_2         = $03; // Xenix
  325.   PARTITION_FAT_16          = $04; // 16-bit FAT entries
  326.   PARTITION_EXTENDED        = $05; // Extended partition entry
  327.   PARTITION_HUGE            = $06; // Huge partition MS-DOS V4
  328.   PARTITION_IFS             = $07; // IFS Partition
  329.   PARTITION_OS2BOOTMGR      = $0A; // OS/2 Boot Manager/OPUS/Coherent swap
  330.   PARTITION_FAT32           = $0B; // FAT32
  331.   PARTITION_FAT32_XINT13    = $0C; // FAT32 using extended int13 services
  332.   PARTITION_XINT13          = $0E; // Win95 partition using extended int13 services
  333.   PARTITION_XINT13_EXTENDED = $0F; // Same as type 5 but uses extended int13 services
  334.   PARTITION_PREP            = $41; // PowerPC Reference Platform (PReP) Boot Partition
  335.   PARTITION_LDM             = $42; // Logical Disk Manager partition
  336.   PARTITION_UNIX            = $63; // Unix
  337.   VALID_NTFT                = $C0; // NTFT uses high order bits
  338. // The high bit of the partition type code indicates that a partition
  339. // is part of an NTFT mirror or striped array.
  340.   PARTITION_NTFT = $80; // NTFT partition
  341.  
  342.   bBytesPerSector = 512;
  343.  
  344. type
  345.   TDiskLayout = array of TPartitionInformation;
  346.  
  347.   TDriveInfo = record
  348.     SerialNumber,                             
  349.     Model,
  350.     Revision: string;
  351.     Capacity: Int64;
  352.   end;
  353.  
  354. function IsRecognizedPartition(PartitionType: DWORD): Boolean;
  355. function IsContainerPartition(PartitionType: DWORD): Boolean;
  356. function IsFTPartition(PartitionType: DWORD): Boolean;
  357. function GetPartitionSystem(PartitionType: DWORD): string;
  358. function GetPartitionType(PartitionNumber,PartitionType: DWORD): string;
  359. function GetMediaTypeStr(Value: TMediaType): string;
  360.  
  361. function GetDeviceHandle(sDeviceName: string): THandle;
  362. function GetSCSIDeviceInfo(DeviceHandle: THandle; out DI: TDriveInfo): integer;
  363. function GetSCSIDiskInfo(DeviceHandle: THandle; DeviceNumber: Byte; out DI: TDriveInfo; out DG: TDiskGeometry): Integer;
  364. function GetPhysicalDiskInfo(DeviceHandle: THandle; out DI: TDriveInfo; out DG: TDiskGeometry): Integer;
  365. function GetSMARTDiskInfo(Drive: Byte; out DI: TDriveInfo; out DG: TDiskGeometry): Integer;
  366. function GetDiskGeometry(DeviceHandle: THandle; out DG: TDiskGeometry): Integer;
  367. function GetDiskLayout(DeviceHandle: THandle; out DL: TDiskLayout): Integer;
  368. function GetDiskController(Drive: Byte; out DC: TDiskControllerNumber): Integer;
  369. function ReadPhysicalSector(Drive: Byte; StartSector: DWORD; SectorsToRead: byte; Dump: Pointer): Integer;
  370.  
  371. implementation
  372.  
  373.