#include "stdafx.h"
|
#include "DiskFunc.h"
|
|
//´ò¿ªÉ豸 //sFileName É豸µÄ ÎļþÃû £¨¾ø¶Ô·¾¶£©
|
HANDLE OpenDevice(LPCTSTR sFileName)
|
{
|
HANDLE hDevice; //´ò¿ªÉ豸
|
hDevice =:: CreateFile(sFileName, //ÎļþÃû
|
GENERIC_READ|GENERIC_WRITE, //¶Áд·½Ê½
|
FILE_SHARE_READ|FILE_SHARE_WRITE,//¹²Ïí·½Ê½
|
NULL, //ĬÈϵݲȫÃèÊö·û
|
OPEN_EXISTING, //´´½¨·½Ê½
|
0, //²»ÐèÉèÖÃÎļþÊôÐÔ
|
NULL); //²»Ðè²ÎÕÕÄ£°æÎļþ
|
return hDevice;
|
}
|
char szSerialNumber[2048];
|
char szModelNumber[2048];
|
BOOL __fastcall DoIdentify( HANDLE hPhysicalDriveIOCTL,
|
PSENDCMDINPARAMS pSCIP,
|
PSENDCMDOUTPARAMS pSCOP,
|
BYTE btIDCmd,
|
BYTE btDriveNum,
|
PDWORD pdwBytesReturned)
|
{
|
pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE;
|
pSCIP->irDriveRegs.bFeaturesReg = 0;
|
pSCIP->irDriveRegs.bSectorCountReg = 1;
|
pSCIP->irDriveRegs.bSectorNumberReg = 1;
|
pSCIP->irDriveRegs.bCylLowReg = 0;
|
pSCIP->irDriveRegs.bCylHighReg = 0;
|
|
pSCIP->irDriveRegs.bDriveHeadReg = (btDriveNum & 1) ? 0xB0 : 0xA0;
|
pSCIP->irDriveRegs.bCommandReg = btIDCmd;
|
pSCIP->bDriveNumber = btDriveNum;
|
pSCIP->cBufferSize = IDENTIFY_BUFFER_SIZE;
|
|
return DeviceIoControl( hPhysicalDriveIOCTL,
|
SMART_RCV_DRIVE_DATA,
|
(LPVOID)pSCIP,
|
sizeof(SENDCMDINPARAMS) - 1,
|
(LPVOID)pSCOP,
|
sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1,
|
pdwBytesReturned, NULL);
|
}
|
|
char *__fastcall ConvertToString(DWORD dwDiskData[256], int nFirstIndex, int nLastIndex)
|
{
|
static char szResBuf[1024];
|
char ss[256];
|
int nIndex = 0;
|
int nPosition = 0;
|
|
for(nIndex = nFirstIndex; nIndex <= nLastIndex; nIndex++)
|
{
|
ss[nPosition] = (char)(dwDiskData[nIndex] / 256);
|
nPosition++;
|
|
// Get low BYTE for 2nd character
|
ss[nPosition] = (char)(dwDiskData[nIndex] % 256);
|
nPosition++;
|
}
|
|
// End the string
|
ss[nPosition] = '\0';
|
|
int i, index=0;
|
for(i=0; i<nPosition; i++)
|
{
|
if(ss[i]==0 || ss[i]==32) continue;
|
szResBuf[index]=ss[i];
|
index++;
|
}
|
szResBuf[index]=0;
|
|
return szResBuf;
|
}
|
void AdjustString(char * str, int len)
|
{
|
char ch;int i;
|
//Á½Á½µßµ¹
|
for (i =0; i<len; i+=2)
|
{
|
ch=str[i];
|
str[i]=str[i+1];
|
str[i+1]=ch;
|
}
|
str[len-1]=0;
|
//ÈôÊÇÓÒ¶ÔÆ䣬µ÷ÕûΪ×ó¶ÔÆä
|
|
}
|
BOOL IDentifyDeviceAsScsi(HANDLE hDevice, int nDrive, PIDINFO pIdInfo)
|
{
|
PSENDCMDINPARAMS pSCIP;
|
PSENDCMDOUTPARAMS pSCOP;
|
PSRB_IO_CONTROL pSRBIO;
|
DWORD dwOutBytes;
|
BOOL bResult;
|
|
pSRBIO = (PSRB_IO_CONTROL)::GlobalAlloc(LMEM_ZEROINIT, sizeof(SRB_IO_CONTROL)+sizeof(SENDCMDOUTPARAMS)+sizeof(IDINFO) -1);
|
pSCIP = (PSENDCMDINPARAMS)((char *)pSRBIO+sizeof(SRB_IO_CONTROL));
|
pSCOP = (PSENDCMDOUTPARAMS)pSCIP;
|
//Ìî³äÊý¾Ý
|
pSRBIO->HeaderLength=sizeof(SRB_IO_CONTROL);
|
pSRBIO->Timeout=10000;
|
pSRBIO->Length=sizeof(SENDCMDOUTPARAMS)+sizeof(IDINFO)-1;
|
pSRBIO->ControlCode=IOCTL_SCSI_MINIPORT_IDENTIFY;
|
memcpy_s((char *)pSRBIO->Signature,8,"SCSIDISK",8);
|
//strncpy_s((char *)pSRBIO->Signature,8,"SCSIDISK",8);
|
//Ö¸¶¨ATA/ATAPI ÃüÁîµÄ¼Ä´æÆ÷Öµ
|
pSCIP->irDriveRegs.bFeaturesReg = 0;
|
pSCIP->irDriveRegs.bSectorCountReg = 0;
|
pSCIP->irDriveRegs.bSectorNumberReg = 0;
|
pSCIP->irDriveRegs.bCylLowReg = 0;
|
pSCIP->irDriveRegs.bCylHighReg = 0;
|
pSCIP->irDriveRegs.bDriveHeadReg =0;
|
pSCIP->irDriveRegs.bCommandReg = IDE_ATA_IDENTIFY;
|
pSCIP->bDriveNumber = nDrive;
|
//IDENTIFY DEVICE
|
bResult = ::DeviceIoControl(hDevice, IOCTL_SCSI_MINIPORT,
|
pSRBIO,sizeof(SRB_IO_CONTROL) + sizeof(SENDCMDINPARAMS) -1,
|
pSRBIO,sizeof(SRB_IO_CONTROL) + sizeof(SENDCMDOUTPARAMS) +sizeof(IDINFO)-1,
|
&dwOutBytes,
|
NULL);
|
::memcpy(pIdInfo,pSCOP->bBuffer,sizeof(IDINFO));
|
::GlobalFree(pSRBIO);
|
return bResult;
|
|
}
|
//ÓÃSCSIÇý¶¯¶ÁÈ¡IDEÓ²Å̵ÄÉ豸ÐÅÏ¢£¬²»ÊÜȨÏÞÖÆÔ¼
|
//nDriver Çý¶¯Æ÷ºÃ( 0 = Primary master, 1=Primary Slave, 2 = Second Master,
|
//pIdInfo É豸ÐÅÏ¢½á¹¹Ö¸Õë;
|
BOOL GetIdeDriveAsScsiInfoInNT(int nDrive, PIDINFO pIdInfo)
|
{
|
HANDLE hDevice;//
|
BOOL bResult; //
|
// char szFileName[20];
|
// ::sprintf(szFileName,"\\\\.\\Scsi%d:",nDrive/2);
|
CString sFileName;
|
sFileName.Format(_T("\\\\.\\Scsi%d:"),nDrive/2);
|
hDevice = OpenDevice(sFileName);
|
if (hDevice == INVALID_HANDLE_VALUE) {return false;}
|
//IDENTIFY DEVICE
|
bResult = IDentifyDeviceAsScsi(hDevice,nDrive%2,pIdInfo);
|
CloseHandle(hDevice);
|
//¼ì²éÊDz»ÊÇ¿Õ´®;
|
if (pIdInfo->sModelNumber[0] == '\0') {bResult = false;}
|
if (bResult)
|
{
|
//µ÷Õû×Ö·û´®
|
::AdjustString(pIdInfo->sSerialNumber,20);
|
::AdjustString(pIdInfo->sModelNumber,40);
|
::AdjustString(pIdInfo->sFirmwareRev,8);
|
}
|
return bResult;
|
|
}
|
int GetHDSerialID(int driver)
|
{
|
// Ó²ÅÌÎïÀíÐòÁкÅÊÇÓ²Å̵ijö³§ÐòÁкţ¬ËüÊÇÈ«Çò¶¼ÊÇΨһµÄ£¬²»»áËæ×ÅϵͳµÄ°²×°¡¢Ó²Å̵ĸñʽ»¯µÈ²Ù×÷¶ø¸Ä±ä£¬¸úmacµØÖ·Ò»Ñù¶¼¾ßÓÐΨһÐÔ¡£
|
CString s1;
|
// 1£¬µÚÒ»²½£º´´½¨É豸¶ÔÏ󣬵õ½É豸¾ä±ú£¬É豸ΪӲÅÌ¡£
|
{
|
// int driver=0;
|
CString sFilePath;
|
sFilePath.Format(_T("\\\\.\\PHYSICALDRIVE%d"), driver);
|
HANDLE hFile=::CreateFile(sFilePath,
|
GENERIC_READ , FILE_SHARE_READ ,
|
NULL, OPEN_EXISTING,0, NULL);
|
s1.Format(_T("´´½¨Îļþ %s ½á¹û ¾ä±ú %X \r\n"),sFilePath,hFile);
|
// mylogger1.LogTxt(s1);
|
if (hFile==INVALID_HANDLE_VALUE) {return -1;}
|
DWORD dwBytesReturned;
|
GETVERSIONINPARAMS gvopVersionParams;
|
int j = DeviceIoControl(hFile, //ÏòÉ豸¶ÔÏó·¢ËÍSMART_GET_VERSIONÉ豸ÇëÇ󣬻ñÈ¡Ó²ÅÌÊôÐÔ
|
SMART_GET_VERSION, NULL, 0,
|
&gvopVersionParams, sizeof(gvopVersionParams),
|
&dwBytesReturned, NULL);
|
s1.Format(_T("»ñÈ¡´ÅÅÌÊôÐÔ 0x%X Return %d Bytes %d bIDEDeviceMap %d \r\n"),SMART_GET_VERSION,j,dwBytesReturned,gvopVersionParams.bIDEDeviceMap);
|
// mylogger1.LogTxt(s1);
|
|
// if(gvopVersionParams.bIDEDeviceMap <= 0) return -2;
|
// 2¡£µÚ¶þ²½£¬·¢ËÍSMART_RCV_DRIVE_DATAÉ豸ÇëÇ󣬻ñÈ¡Ó²ÅÌÏêϸÐÅÏ¢¡£
|
// IDE or ATAPI IDENTIFY cmd
|
int btIDCmd = 0;
|
SENDCMDINPARAMS InParams;
|
int nDrive =0;
|
btIDCmd = (gvopVersionParams.bIDEDeviceMap >> nDrive & 0x10) ? IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
|
|
s1.Format(_T("btIDCmd %X \r\n "),btIDCmd);
|
// mylogger1.LogTxt(s1);
|
|
// Êä³ö²ÎÊý
|
BYTE btIDOutCmd[sizeof(SENDCMDOUTPARAMS) + IDENTIFY_BUFFER_SIZE - 1];
|
int bResult=DoIdentify(hFile,
|
&InParams,
|
(PSENDCMDOUTPARAMS)btIDOutCmd,
|
(BYTE)btIDCmd,
|
(BYTE)nDrive, &dwBytesReturned);
|
::CloseHandle(hFile);
|
if( bResult== FALSE) return -3;
|
|
DWORD dwDiskData[256];
|
USHORT *pIDSector; // ¶ÔÓ¦½á¹¹IDSECTOR£¬¼ûÍ·Îļþ
|
|
pIDSector = (USHORT*)((SENDCMDOUTPARAMS*)btIDOutCmd)->bBuffer;
|
for(int i=0; i < 256; i++) dwDiskData[i] = pIDSector[i];
|
|
// ȡϵÁкÅ
|
ZeroMemory(szSerialNumber, sizeof(szSerialNumber));
|
strcpy_s(szSerialNumber, 2048, ConvertToString(dwDiskData, 10, 19));
|
|
// È¡Ä£ÐͺÅ
|
ZeroMemory(szModelNumber, sizeof(szModelNumber));
|
strcpy_s(szModelNumber, 2048, ConvertToString(dwDiskData, 27, 46));
|
s1.Format(_T("\r\n Model %S \r\n Serial %S \r\n"),szModelNumber,szSerialNumber);
|
// mylogger1.LogTxt(s1);
|
int len1=strlen(szSerialNumber);
|
short int machinecode[15];
|
int len2=(len1+1)/2;
|
s1.Format(_T("MachineCode: "));
|
for (int i=0;i<len2;i++)
|
{
|
if (i!=0) s1.Append(_T("-"));
|
int j=((szSerialNumber[i*2]-48)+(szSerialNumber[i*2+1]-48)*64)%8192+1234;
|
machinecode[i]=j;
|
s1.AppendFormat(_T("%d"),j);
|
}
|
s1.Append(_T("\r\n"));
|
// mylogger1.LogTxt(s1);
|
s1.Format(_T("Back : "));
|
for (int i=0;i<len2;i++)
|
{
|
int j=machinecode[i];
|
int k,l;
|
k=((j-1234)%64)+48;l=((j-1234)/64)+48;
|
s1.AppendFormat(_T("%c%c"),k,l);
|
}
|
s1.AppendFormat(_T("\r\n"));
|
// mylogger1.LogTxt(s1);
|
return 0;
|
}
|
}
|