QuakeGod
2022-01-16 326d3e312c74726814c39c9d112faab03c4a167c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#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;
    }
}