HardDriveSerialNumber.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #ifndef _HDD_SERIAL_INFO_H_
  2. #define _HDD_SERIAL_INFO_H_
  3. #include <tchar.h>
  4. #include <string.h>
  5. #include <windows.h>
  6. #include <winioctl.h>
  7. #include <stdio.h>
  8. #include <vector>
  9. #pragma pack(1)
  10. const CString DEFAULT_COMPUTER_ID = _T("OPTO-NOTS-INCA-WWWW");
  11. #define IDENTIFY_BUFFER_SIZE 512
  12. // IOCTL commands
  13. #define DFP_GET_VERSION 0x00074080
  14. #define DFP_SEND_DRIVE_COMMAND 0x0007c084
  15. #define DFP_RECEIVE_DRIVE_DATA 0x0007c088
  16. #define FILE_DEVICE_SCSI 0x0000001b
  17. #define IOCTL_SCSI_MINIPORT_IDENTIFY ((FILE_DEVICE_SCSI << 16) + 0x0501)
  18. #define IOCTL_SCSI_MINIPORT 0x0004D008 // see NTDDSCSI.H for definition
  19. #define SMART_GET_VERSION CTL_CODE(IOCTL_DISK_BASE, 0x0020, METHOD_BUFFERED, FILE_READ_ACCESS)
  20. #define SMART_SEND_DRIVE_COMMAND CTL_CODE(IOCTL_DISK_BASE, 0x0021, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
  21. #define SMART_RCV_DRIVE_DATA CTL_CODE(IOCTL_DISK_BASE, 0x0022, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
  22. // GETVERSIONOUTPARAMS contains the data returned from the
  23. // Get Driver Version function.
  24. typedef struct _GETVERSIONOUTPARAMS
  25. {
  26. BYTE bVersion; // Binary driver version.
  27. BYTE bRevision; // Binary driver revision.
  28. BYTE bReserved; // Not used.
  29. BYTE bIDEDeviceMap; // Bit map of IDE devices.
  30. DWORD fCapabilities; // Bit mask of driver capabilities.
  31. DWORD dwReserved[4]; // For future use.
  32. } GETVERSIONOUTPARAMS, *PGETVERSIONOUTPARAMS, *LPGETVERSIONOUTPARAMS;
  33. // Bits returned in the fCapabilities member of GETVERSIONOUTPARAMS
  34. #define CAP_IDE_ID_FUNCTION 1 // ATA ID command supported
  35. #define CAP_IDE_ATAPI_ID 2 // ATAPI ID command supported
  36. #define CAP_IDE_EXECUTE_SMART_FUNCTION 4 // SMART commannds supported
  37. // Valid values for the bCommandReg member of IDEREGS.
  38. #define IDE_ATAPI_IDENTIFY 0xA1 // Returns ID sector for ATAPI.
  39. #define IDE_ATA_IDENTIFY 0xEC // Returns ID sector for ATA.
  40. // The following struct defines the interesting part of the IDENTIFY
  41. // buffer:
  42. typedef struct _IDSECTOR
  43. {
  44. USHORT wGenConfig;
  45. USHORT wNumCyls;
  46. USHORT wReserved;
  47. USHORT wNumHeads;
  48. USHORT wBytesPerTrack;
  49. USHORT wBytesPerSector;
  50. USHORT wSectorsPerTrack;
  51. USHORT wVendorUnique[3];
  52. CHAR sSerialNumber[20];
  53. USHORT wBufferType;
  54. USHORT wBufferSize;
  55. USHORT wECCSize;
  56. CHAR sFirmwareRev[8];
  57. CHAR sModelNumber[40];
  58. USHORT wMoreVendorUnique;
  59. USHORT wDoubleWordIO;
  60. USHORT wCapabilities;
  61. USHORT wReserved1;
  62. USHORT wPIOTiming;
  63. USHORT wDMATiming;
  64. USHORT wBS;
  65. USHORT wNumCurrentCyls;
  66. USHORT wNumCurrentHeads;
  67. USHORT wNumCurrentSectorsPerTrack;
  68. ULONG ulCurrentSectorCapacity;
  69. USHORT wMultSectorStuff;
  70. ULONG ulTotalAddressableSectors;
  71. USHORT wSingleWordDMA;
  72. USHORT wMultiWordDMA;
  73. BYTE bReserved[128];
  74. } IDSECTOR, *PIDSECTOR;
  75. typedef struct _SRB_IO_CONTROL
  76. {
  77. ULONG HeaderLength;
  78. UCHAR Signature[8];
  79. ULONG Timeout;
  80. ULONG ControlCode;
  81. ULONG ReturnCode;
  82. ULONG Length;
  83. } SRB_IO_CONTROL, *PSRB_IO_CONTROL;
  84. // Max number of drives assuming primary/secondary, master/slave topology
  85. // Modified to read only the master serial
  86. #define MAX_IDE_DRIVES 1
  87. //
  88. // IDENTIFY data (from ATAPI driver source)
  89. //
  90. #pragma pack(1)
  91. typedef struct _IDENTIFY_DATA {
  92. USHORT GeneralConfiguration; // 00 00
  93. USHORT NumberOfCylinders; // 02 1
  94. USHORT Reserved1; // 04 2
  95. USHORT NumberOfHeads; // 06 3
  96. USHORT UnformattedBytesPerTrack; // 08 4
  97. USHORT UnformattedBytesPerSector; // 0A 5
  98. USHORT SectorsPerTrack; // 0C 6
  99. USHORT VendorUnique1[3]; // 0E 7-9
  100. USHORT SerialNumber[10]; // 14 10-19
  101. USHORT BufferType; // 28 20
  102. USHORT BufferSectorSize; // 2A 21
  103. USHORT NumberOfEccBytes; // 2C 22
  104. USHORT FirmwareRevision[4]; // 2E 23-26
  105. USHORT ModelNumber[20]; // 36 27-46
  106. UCHAR MaximumBlockTransfer; // 5E 47
  107. UCHAR VendorUnique2; // 5F
  108. USHORT DoubleWordIo; // 60 48
  109. USHORT Capabilities; // 62 49
  110. USHORT Reserved2; // 64 50
  111. UCHAR VendorUnique3; // 66 51
  112. UCHAR PioCycleTimingMode; // 67
  113. UCHAR VendorUnique4; // 68 52
  114. UCHAR DmaCycleTimingMode; // 69
  115. USHORT TranslationFieldsValid:1; // 6A 53
  116. USHORT Reserved3:15;
  117. USHORT NumberOfCurrentCylinders; // 6C 54
  118. USHORT NumberOfCurrentHeads; // 6E 55
  119. USHORT CurrentSectorsPerTrack; // 70 56
  120. ULONG CurrentSectorCapacity; // 72 57-58
  121. USHORT CurrentMultiSectorSetting; // 59
  122. ULONG UserAddressableSectors; // 60-61
  123. USHORT SingleWordDMASupport : 8; // 62
  124. USHORT SingleWordDMAActive : 8;
  125. USHORT MultiWordDMASupport : 8; // 63
  126. USHORT MultiWordDMAActive : 8;
  127. USHORT AdvancedPIOModes : 8; // 64
  128. USHORT Reserved4 : 8;
  129. USHORT MinimumMWXferCycleTime; // 65
  130. USHORT RecommendedMWXferCycleTime; // 66
  131. USHORT MinimumPIOCycleTime; // 67
  132. USHORT MinimumPIOCycleTimeIORDY; // 68
  133. USHORT Reserved5[2]; // 69-70
  134. USHORT ReleaseTimeOverlapped; // 71
  135. USHORT ReleaseTimeServiceCommand; // 72
  136. USHORT MajorRevision; // 73
  137. USHORT MinorRevision; // 74
  138. USHORT Reserved6[50]; // 75-126
  139. USHORT SpecialFunctionsEnabled; // 127
  140. USHORT Reserved7[128]; // 128-255
  141. } IDENTIFY_DATA, *PIDENTIFY_DATA;
  142. #pragma pack()
  143. // Required to ensure correct PhysicalDrive IOCTL structure setup
  144. #pragma pack(4)
  145. //
  146. // IOCTL_STORAGE_QUERY_PROPERTY
  147. //
  148. // Input Buffer:
  149. // a STORAGE_PROPERTY_QUERY structure which describes what type of query
  150. // is being done, what property is being queried for, and any additional
  151. // parameters which a particular property query requires.
  152. //
  153. // Output Buffer:
  154. // Contains a buffer to place the results of the query into. Since all
  155. // property descriptors can be cast into a STORAGE_DESCRIPTOR_HEADER,
  156. // the IOCTL can be called once with a small buffer then again using
  157. // a buffer as large as the header reports is necessary.
  158. //
  159. //
  160. // Types of queries
  161. //
  162. //
  163. // define some initial property id's
  164. //
  165. //
  166. // Query structure - additional parameters for specific queries can follow
  167. // the header
  168. //
  169. #define IOCTL_STORAGE_QUERY_PROPERTY CTL_CODE(IOCTL_STORAGE_BASE, 0x0500, METHOD_BUFFERED, FILE_ANY_ACCESS)
  170. //
  171. // Device property descriptor - this is really just a rehash of the inquiry
  172. // data retrieved from a scsi device
  173. //
  174. // This may only be retrieved from a target device. Sending this to the bus
  175. // will result in an error
  176. //
  177. #pragma pack(4)
  178. // (* Output Bbuffer for the VxD (rt_IdeDinfo record) *)
  179. typedef struct _rt_IdeDInfo_
  180. {
  181. BYTE IDEExists[4];
  182. BYTE DiskExists[8];
  183. WORD DisksRawInfo[8*256];
  184. } rt_IdeDInfo, *pt_IdeDInfo;
  185. // (* IdeDinfo "data fields" *)
  186. typedef struct _rt_DiskInfo_
  187. {
  188. BOOL DiskExists;
  189. BOOL ATAdevice;
  190. BOOL RemovableDevice;
  191. WORD TotLogCyl;
  192. WORD TotLogHeads;
  193. WORD TotLogSPT;
  194. char SerialNumber[20];
  195. char FirmwareRevision[8];
  196. char ModelNumber[40];
  197. WORD CurLogCyl;
  198. WORD CurLogHeads;
  199. WORD CurLogSPT;
  200. } rt_DiskInfo;
  201. #define SENDIDLENGTH sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE
  202. #define IOCTL_DISK_GET_DRIVE_GEOMETRY_EX CTL_CODE(IOCTL_DISK_BASE, 0x0028, METHOD_BUFFERED, FILE_ANY_ACCESS)
  203. class MasterHardDiskSerial
  204. {
  205. public:
  206. MasterHardDiskSerial();
  207. ~MasterHardDiskSerial();
  208. int GetSerialNo(std::vector<char> &serialNumber);
  209. int GetErrorMessage(TCHAR* _ptszErrorMessage= NULL);
  210. __int64 getHardDriveComputerID();
  211. ///Get the id of this computer as a string.
  212. CString GetHardDriveComputerIdAsString(void);
  213. CString GetDefaultComputerIdString(void) { return DEFAULT_COMPUTER_ID; }
  214. private:
  215. char* ConvertToString ( DWORD dwDiskdata [256], int iFirstIndex, int iLastIndex, char* pcBuf = NULL );
  216. BOOL DoIDENTIFY (HANDLE, PSENDCMDINPARAMS, PSENDCMDOUTPARAMS, BYTE, BYTE, PDWORD);
  217. int ReadPhysicalDriveInNTWithAdminRights (void);
  218. int ReadPhysicalDriveInNTUsingSmart (void);
  219. int ReadPhysicalDriveInNTWithZeroRights (void);
  220. int ReadIdeDriveAsScsiDriveInNT (void);
  221. char* flipAndCodeBytes ( int iPos, int iFlip,const char * pcStr = NULL, char * pcBuf= NULL);
  222. void PrintIdeInfo (int iDrive, DWORD dwDiskdata [256]);
  223. private:
  224. char m_cszHardDriveSerialNumber [1024];
  225. char m_cszHardDriveModelNumber [1024];
  226. char m_cszErrorMessage[256];
  227. BYTE byIdOutCmd [sizeof (SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];
  228. __int64 m_nComputerId;
  229. };
  230. #endif // _HDD_SERIAL_INFO_H_