This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.cpp
executable file
·506 lines (465 loc) · 16.7 KB
/
main.cpp
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/**********************************************************************
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see http://gnu.org/licenses/
* --- *
* Copyright (C) 2009, Justin Davis <[email protected]> *
* Copyright (C) 2009-2014 ImageWriter developers *
* https://launchpad.net/~image-writer-devs *
* Copyright (C) 2016, David Ferguson <[email protected]> *
**********************************************************************/
#ifndef WINVER
#define WINVER 0x0601
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <windows.h>
#include <winioctl.h>
#include <dbt.h>
#include <shlobj.h>
#include <iostream>
#include <sstream>
#include "main.h"
int main(int argc, char *argv[])
{
if ( argc != 3 )
{
return 1; //not enough arguments
}
const char ltr = argv[2][0];
const char *imagefile = argv[1];
status = STATUS_IDLE;
hVolume = INVALID_HANDLE_VALUE;
hFile = INVALID_HANDLE_VALUE;
hRawDisk = INVALID_HANDLE_VALUE;
sectorData = NULL;
sectorsize = 0ul;
if (strcmp( imagefile, "") != 0)
{
if(INVALID_FILE_ATTRIBUTES == GetFileAttributes(imagefile) && GetLastError()==ERROR_FILE_NOT_FOUND)
{
return 2; // .img not found
}
else
{
status = STATUS_WRITING;
unsigned long long i, availablesectors, numsectors;
int volumeID = ltr - 'A';
int deviceID = getDriveNumberFromLetter(ltr);
if( deviceID == -1 )
{
return 3; // device not found
}
hVolume = getHandleOnVolume(volumeID, GENERIC_WRITE);
if (hVolume == INVALID_HANDLE_VALUE)
{
status = STATUS_IDLE;
return 4; // invalid handle value for volume
}
if (!getLockOnVolume(hVolume))
{
CloseHandle(hVolume);
status = STATUS_IDLE;
hVolume = INVALID_HANDLE_VALUE;
return 5; // can't get lock on volume
}
if (!unmountVolume(hVolume))
{
removeLockOnVolume(hVolume);
CloseHandle(hVolume);
status = STATUS_IDLE;
hVolume = INVALID_HANDLE_VALUE;
return 6; // can't unmount volume
}
hFile = getHandleOnFile(imagefile, GENERIC_READ);
if (hFile == INVALID_HANDLE_VALUE)
{
removeLockOnVolume(hVolume);
CloseHandle(hVolume);
status = STATUS_IDLE;
hVolume = INVALID_HANDLE_VALUE;
return 7; // invalid handle value for file
}
hRawDisk = getHandleOnDevice(deviceID, GENERIC_WRITE);
if (hRawDisk == INVALID_HANDLE_VALUE)
{
removeLockOnVolume(hVolume);
CloseHandle(hFile);
CloseHandle(hVolume);
status = STATUS_IDLE;
hVolume = INVALID_HANDLE_VALUE;
hFile = INVALID_HANDLE_VALUE;
return 8; // invalid handle value for disk
}
availablesectors = getNumberOfSectors(hRawDisk, §orsize);
numsectors = getFileSizeInSectors(hFile, sectorsize);
if (numsectors > availablesectors)
{
return 9; // not enough space on volume
}
char total_string[32];
sprintf(total_string, "%d", numsectors);
for (i = 0ul; i < numsectors && status == STATUS_WRITING; i += 1024ul)
{
if( i % 100 == 0 )
{
char i_string[32];
sprintf(i_string, "%d", i);
printf(i_string);
printf("/");
printf(total_string);
setbuf(stdout, NULL);
}
sectorData = readSectorDataFromHandle(hFile, i, (numsectors - i >= 1024ul) ? 1024ul:(numsectors - i), sectorsize);
if (sectorData == NULL)
{
removeLockOnVolume(hVolume);
CloseHandle(hRawDisk);
CloseHandle(hFile);
CloseHandle(hVolume);
status = STATUS_IDLE;
hRawDisk = INVALID_HANDLE_VALUE;
hFile = INVALID_HANDLE_VALUE;
hVolume = INVALID_HANDLE_VALUE;
return 10; // sector data is null
}
if (!writeSectorDataToHandle(hRawDisk, sectorData, i, (numsectors - i >= 1024ul) ? 1024ul:(numsectors - i), sectorsize))
{
delete[] sectorData;
removeLockOnVolume(hVolume);
CloseHandle(hRawDisk);
CloseHandle(hFile);
CloseHandle(hVolume);
status = STATUS_IDLE;
sectorData = NULL;
hRawDisk = INVALID_HANDLE_VALUE;
hFile = INVALID_HANDLE_VALUE;
hVolume = INVALID_HANDLE_VALUE;
return 11; // error whilst writing
}
delete[] sectorData;
sectorData = NULL;
}
removeLockOnVolume(hVolume);
CloseHandle(hRawDisk);
CloseHandle(hFile);
CloseHandle(hVolume);
hRawDisk = INVALID_HANDLE_VALUE;
hFile = INVALID_HANDLE_VALUE;
hVolume = INVALID_HANDLE_VALUE;
}
return 0; // success
}
else
{
return 1; // not enough arguments
}
status = STATUS_IDLE;
return 11; // error whilst writing
}
int getDriveNumberFromLetter(char lookingforname)
{
unsigned long driveMask = GetLogicalDrives();
int i = 0;
ULONG pID;
while (driveMask != 0)
{
if (driveMask & 1)
{
// the "A" in drivename will get incremented by the # of bits
// we've shifted
char drivename[] = "\\\\.\\A:\\";
drivename[4] += i;
if (checkDriveType(drivename, &pID))
{
if (lookingforname == drivename[4])
{
return pID;
}
}
}
driveMask >>= 1;
++i;
}
return -1;
}
HANDLE getHandleOnFile(const char *filelocation, DWORD access)
{
HANDLE hFile;
hFile = CreateFileA(filelocation, access, (access == GENERIC_READ) ? FILE_SHARE_READ : 0, NULL, (access == GENERIC_READ) ? OPEN_EXISTING:CREATE_ALWAYS, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
//printf("error - not able to get handle on image file");
}
return hFile;
}
HANDLE getHandleOnDevice(int device, DWORD access)
{
HANDLE hDevice;
char devicename[18];
sprintf(devicename, "\\\\.\\PhysicalDrive%d", device);
hDevice = CreateFile(devicename, access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
//printf("error - not able to get handle on device");
}
return hDevice;
}
HANDLE getHandleOnVolume(int volume, DWORD access)
{
HANDLE hVolume;
char volumename[] = "\\\\.\\A:";
volumename[4] += volume;
hVolume = CreateFile(volumename, access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hVolume == INVALID_HANDLE_VALUE)
{
//printf("error - not able to get handle on volume");
}
return hVolume;
}
bool getLockOnVolume(HANDLE handle)
{
DWORD bytesreturned;
BOOL bResult;
bResult = DeviceIoControl(handle, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &bytesreturned, NULL);
if (!bResult)
{
//printf("error - not able to lock volume");
}
return (bResult);
}
bool removeLockOnVolume(HANDLE handle)
{
DWORD junk;
BOOL bResult;
bResult = DeviceIoControl(handle, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, &junk, NULL);
if (!bResult)
{
//printf("error - not able to unlock volume");
}
return (bResult);
}
bool unmountVolume(HANDLE handle)
{
DWORD junk;
BOOL bResult;
bResult = DeviceIoControl(handle, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &junk, NULL);
if (!bResult)
{
//printf("error - not able to dismount volume");
}
return (bResult);
}
char *readSectorDataFromHandle(HANDLE handle, unsigned long long startsector, unsigned long long numsectors, unsigned long long sectorsize)
{
unsigned long bytesread;
char *data = new char[sectorsize * numsectors];
LARGE_INTEGER li;
li.QuadPart = startsector * sectorsize;
SetFilePointer(handle, li.LowPart, &li.HighPart, FILE_BEGIN);
if (!ReadFile(handle, data, sectorsize * numsectors, &bytesread, NULL))
{
//printf("error - not able to read data from handle");
delete[] data;
data = NULL;
}
if (bytesread < (sectorsize * numsectors))
{
memset(data + bytesread,0,(sectorsize * numsectors) - bytesread);
}
return data;
}
bool writeSectorDataToHandle(HANDLE handle, char *data, unsigned long long startsector, unsigned long long numsectors, unsigned long long sectorsize)
{
unsigned long byteswritten;
BOOL bResult;
LARGE_INTEGER li;
li.QuadPart = startsector * sectorsize;
SetFilePointer(handle, li.LowPart, &li.HighPart, FILE_BEGIN);
bResult = WriteFile(handle, data, sectorsize * numsectors, &byteswritten, NULL);
if (!bResult)
{
//printf("error - not able to write data from handle");
}
return (bResult);
}
unsigned long long getNumberOfSectors(HANDLE handle, unsigned long long *sectorsize)
{
DWORD junk;
DISK_GEOMETRY_EX diskgeometry;
BOOL bResult;
bResult = DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &diskgeometry, sizeof(diskgeometry), &junk, NULL);
if (!bResult)
{
return 12;
}
if (sectorsize != NULL)
{
*sectorsize = (unsigned long long)diskgeometry.Geometry.BytesPerSector;
}
return (unsigned long long)diskgeometry.DiskSize.QuadPart / (unsigned long long)diskgeometry.Geometry.BytesPerSector;
}
unsigned long long getFileSizeInSectors(HANDLE handle, unsigned long long sectorsize)
{
unsigned long long retVal = 0;
LARGE_INTEGER filesize;
if(GetFileSizeEx(handle, &filesize) == 0)
{
//printf("error - not able to get image file size");
retVal = 0;
}
else
{
retVal = ((unsigned long long)filesize.QuadPart / sectorsize ) + (((unsigned long long)filesize.QuadPart % sectorsize )?1:0);
}
return(retVal);
}
BOOL GetDisksProperty(HANDLE hDevice, PSTORAGE_DEVICE_DESCRIPTOR pDevDesc,
DEVICE_NUMBER *devInfo)
{
STORAGE_PROPERTY_QUERY Query; // input param for query
DWORD dwOutBytes; // IOCTL output length
BOOL bResult; // IOCTL return val
BOOL retVal = true;
DWORD cbBytesReturned;
// specify the query type
Query.PropertyId = StorageDeviceProperty;
Query.QueryType = PropertyStandardQuery;
// Query using IOCTL_STORAGE_QUERY_PROPERTY
bResult = ::DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY,
&Query, sizeof(STORAGE_PROPERTY_QUERY), pDevDesc,
pDevDesc->Size, &dwOutBytes, (LPOVERLAPPED)NULL);
if (bResult)
{
bResult = ::DeviceIoControl(hDevice, IOCTL_STORAGE_GET_DEVICE_NUMBER,
NULL, 0, devInfo, sizeof(DEVICE_NUMBER), &dwOutBytes,
(LPOVERLAPPED)NULL);
if (!bResult)
{
retVal = false;
//printf("error - not able to get device number, is something accessing the device?");
//setbuf(stdout, NULL);
}
}
else
{
if (DeviceIoControl(hDevice, IOCTL_STORAGE_CHECK_VERIFY2, NULL, 0, NULL, 0, &cbBytesReturned,
(LPOVERLAPPED) NULL))
{
//printf("error - not able to get device properties, is something accessing the device?");
//setbuf(stdout, NULL);
}
retVal = false;
}
return(retVal);
}
bool slashify(char *str, char **slash, char **noSlash)
{
bool retVal = false;
int strLen = strlen(str);
if ( strLen > 0 )
{
if ( *(str + strLen - 1) == '\\' )
{
// trailing slash exists
if (( (*slash = (char *)calloc( (strLen + 1), sizeof(char))) != NULL) &&
( (*noSlash = (char *)calloc(strLen, sizeof(char))) != NULL))
{
strncpy(*slash, str, strLen);
strncpy(*noSlash, *slash, (strLen - 1));
retVal = true;
}
}
else
{
// no trailing slash exists
if ( ((*slash = (char *)calloc( (strLen + 2), sizeof(char))) != NULL) &&
((*noSlash = (char *)calloc( (strLen + 1), sizeof(char))) != NULL) )
{
strncpy(*noSlash, str, strLen);
sprintf(*slash, "%s\\", *noSlash);
retVal = true;
}
}
}
return(retVal);
}
bool checkDriveType(char *name, ULONG *pid)
{
HANDLE hDevice;
PSTORAGE_DEVICE_DESCRIPTOR pDevDesc;
DEVICE_NUMBER deviceInfo;
bool retVal = false;
char *nameWithSlash;
char *nameNoSlash;
int driveType;
DWORD cbBytesReturned;
// some calls require no tailing slash, some require a trailing slash...
if ( !(slashify(name, &nameWithSlash, &nameNoSlash)) )
{
return(retVal);
}
driveType = GetDriveType(nameWithSlash);
switch( driveType )
{
case DRIVE_REMOVABLE: // The media can be removed from the drive.
case DRIVE_FIXED: // The media cannot be removed from the drive.
hDevice = CreateFile(nameNoSlash, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
//printf("error - not able to get handle on device");
//setbuf(stdout, NULL);
}
else
{
int arrSz = sizeof(STORAGE_DEVICE_DESCRIPTOR) + 512 - 1;
pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)new BYTE[arrSz];
pDevDesc->Size = arrSz;
// get the device number if the drive is
// removable or (fixed AND on the usb bus, SD, or MMC (undefined in XP/mingw))
if(GetDisksProperty(hDevice, pDevDesc, &deviceInfo) &&
( ((driveType == DRIVE_REMOVABLE) && (pDevDesc->BusType != BusTypeSata))
|| ( (driveType == DRIVE_FIXED) && ((pDevDesc->BusType == BusTypeUsb)
|| (pDevDesc->BusType == /*BusTypeSd*/0xC ) || (pDevDesc->BusType == /*BusTypeMmc*/0xD )) ) ) )
{
// ensure that the drive is actually accessible
// multi-card hubs were reporting "removable" even when empty
if(DeviceIoControl(hDevice, IOCTL_STORAGE_CHECK_VERIFY2, NULL, 0, NULL, 0, &cbBytesReturned, (LPOVERLAPPED) NULL))
{
*pid = deviceInfo.DeviceNumber;
retVal = true;
}
else
// IOCTL_STORAGE_CHECK_VERIFY2 fails on some devices under XP/Vista, try the other (slower) method, just in case.
{
CloseHandle(hDevice);
hDevice = CreateFile(nameNoSlash, FILE_READ_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if(DeviceIoControl(hDevice, IOCTL_STORAGE_CHECK_VERIFY, NULL, 0, NULL, 0, &cbBytesReturned, (LPOVERLAPPED) NULL))
{
*pid = deviceInfo.DeviceNumber;
retVal = true;
}
}
}
delete[] pDevDesc;
CloseHandle(hDevice);
}
break;
default:
retVal = false;
}
// free the strings allocated by slashify
free(nameWithSlash);
free(nameNoSlash);
return(retVal);
}