sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
//
|
sl@0
|
18 |
// MbrUtil.cpp
|
sl@0
|
19 |
//
|
sl@0
|
20 |
|
sl@0
|
21 |
|
sl@0
|
22 |
#define _WIN32_WINNT 0x0400
|
sl@0
|
23 |
#include <windows.h>
|
sl@0
|
24 |
|
sl@0
|
25 |
#pragma warning (disable:4201) // warning C4201: nonstandard extension used : nameless struct/union
|
sl@0
|
26 |
#include <winioctl.h>
|
sl@0
|
27 |
|
sl@0
|
28 |
#include <stdio.h>
|
sl@0
|
29 |
|
sl@0
|
30 |
|
sl@0
|
31 |
const int KSectorSize = 512;
|
sl@0
|
32 |
BYTE TheBuffer[KSectorSize];
|
sl@0
|
33 |
|
sl@0
|
34 |
template <class T>
|
sl@0
|
35 |
T Min(T aLeft,T aRight)
|
sl@0
|
36 |
{return(aLeft<aRight ? aLeft : aRight);}
|
sl@0
|
37 |
|
sl@0
|
38 |
|
sl@0
|
39 |
|
sl@0
|
40 |
const unsigned char KPartitionTypeEmpty=0x00;
|
sl@0
|
41 |
const unsigned char KPartitionTypeFAT12=0x01;
|
sl@0
|
42 |
const unsigned char KPartitionTypeFAT16small=0x04;
|
sl@0
|
43 |
const unsigned char KPartitionTypeFAT16=0x06;
|
sl@0
|
44 |
const unsigned char KPartitionTypeNTFS=0x07;
|
sl@0
|
45 |
const unsigned char KPartitionTypeWin95FAT32=0x0b;
|
sl@0
|
46 |
const unsigned char KPartitionTypeWin95FAT32LBA=0x0c;
|
sl@0
|
47 |
const unsigned char KPartitionTypeWin95FAT16LBA=0x0e;
|
sl@0
|
48 |
const unsigned char KPartitionTypeWin95ExtdLBA=0x0f;
|
sl@0
|
49 |
const unsigned char KPartitionTypeHiddenFAT12=0x11;
|
sl@0
|
50 |
const unsigned char KPartitionTypeHiddenFAT16small=0x14;
|
sl@0
|
51 |
const unsigned char KPartitionTypeHiddenFAT16=0x16;
|
sl@0
|
52 |
const unsigned char KPartitionTypeHiddenNTFS=0x17;
|
sl@0
|
53 |
const unsigned char KPartitionTypeHiddenWin95FAT32=0x1b;
|
sl@0
|
54 |
const unsigned char KPartitionTypeHiddenWin95FAT32LBA=0x1c;
|
sl@0
|
55 |
const unsigned char KPartitionTypeHiddenWin95FAT16LBA=0x1e;
|
sl@0
|
56 |
|
sl@0
|
57 |
const unsigned char KBootIndicatorBootable=0x80;
|
sl@0
|
58 |
|
sl@0
|
59 |
const int KDiskSectorSize = 512;
|
sl@0
|
60 |
const int KDiskSectorSizeLog2 = 9;
|
sl@0
|
61 |
|
sl@0
|
62 |
const int KMegaByte = 0x100000;
|
sl@0
|
63 |
const int KMegaByteLog2 = 20;
|
sl@0
|
64 |
const int KMegaByteToSectorSizeShift = KMegaByteLog2 - KDiskSectorSizeLog2;
|
sl@0
|
65 |
const int KMegaByteInSectors = KMegaByte >> KMegaByteToSectorSizeShift;
|
sl@0
|
66 |
|
sl@0
|
67 |
const int KMaxPartitionEntries=0x4;
|
sl@0
|
68 |
const int KMBRFirstPartitionOffset=0x1BE;
|
sl@0
|
69 |
const int KMBRSignatureOffset=0x1FE;
|
sl@0
|
70 |
|
sl@0
|
71 |
inline BOOL PartitionIsFAT(unsigned char a)
|
sl@0
|
72 |
{
|
sl@0
|
73 |
return (
|
sl@0
|
74 |
a==KPartitionTypeFAT12 ||
|
sl@0
|
75 |
a==KPartitionTypeFAT16small ||
|
sl@0
|
76 |
a==KPartitionTypeFAT16 ||
|
sl@0
|
77 |
a==KPartitionTypeFAT16 ||
|
sl@0
|
78 |
a==KPartitionTypeWin95FAT16LBA ||
|
sl@0
|
79 |
a==KPartitionTypeHiddenFAT12 ||
|
sl@0
|
80 |
a==KPartitionTypeHiddenFAT16small ||
|
sl@0
|
81 |
a==KPartitionTypeHiddenFAT16 ||
|
sl@0
|
82 |
a==KPartitionTypeHiddenWin95FAT16LBA
|
sl@0
|
83 |
);
|
sl@0
|
84 |
}
|
sl@0
|
85 |
|
sl@0
|
86 |
inline BOOL PartitionIsFAT32(unsigned char a)
|
sl@0
|
87 |
{
|
sl@0
|
88 |
return (
|
sl@0
|
89 |
a==KPartitionTypeWin95FAT32 ||
|
sl@0
|
90 |
a==KPartitionTypeWin95FAT32LBA ||
|
sl@0
|
91 |
a==KPartitionTypeHiddenWin95FAT32 ||
|
sl@0
|
92 |
a==KPartitionTypeHiddenWin95FAT32LBA
|
sl@0
|
93 |
);
|
sl@0
|
94 |
}
|
sl@0
|
95 |
|
sl@0
|
96 |
inline BOOL PartitionIsNTFS(unsigned char a)
|
sl@0
|
97 |
{
|
sl@0
|
98 |
return (
|
sl@0
|
99 |
a==KPartitionTypeNTFS ||
|
sl@0
|
100 |
a==KPartitionTypeHiddenNTFS
|
sl@0
|
101 |
);
|
sl@0
|
102 |
}
|
sl@0
|
103 |
|
sl@0
|
104 |
class TMBRPartitionEntry
|
sl@0
|
105 |
{
|
sl@0
|
106 |
public:
|
sl@0
|
107 |
BOOL IsValidPartition()
|
sl@0
|
108 |
{ return (iNumSectors>0 && iPartitionType!=KPartitionTypeEmpty); }
|
sl@0
|
109 |
BOOL IsValidDosPartition()
|
sl@0
|
110 |
{ return (iNumSectors>0 && PartitionIsFAT(iPartitionType)); }
|
sl@0
|
111 |
BOOL IsDefaultBootPartition()
|
sl@0
|
112 |
{ return(iX86BootIndicator==KBootIndicatorBootable && (IsValidDosPartition() || IsValidFAT32Partition())); }
|
sl@0
|
113 |
BOOL IsValidFAT32Partition()
|
sl@0
|
114 |
{ return (iNumSectors>0 && PartitionIsFAT32(iPartitionType)); }
|
sl@0
|
115 |
public:
|
sl@0
|
116 |
unsigned char iX86BootIndicator;
|
sl@0
|
117 |
unsigned char iStartHead;
|
sl@0
|
118 |
unsigned char iStartSector;
|
sl@0
|
119 |
unsigned char iStartCylinder;
|
sl@0
|
120 |
unsigned char iPartitionType;
|
sl@0
|
121 |
unsigned char iEndHead;
|
sl@0
|
122 |
unsigned char iEndSector;
|
sl@0
|
123 |
unsigned char iEndCylinder;
|
sl@0
|
124 |
DWORD iFirstSector;
|
sl@0
|
125 |
DWORD iNumSectors;
|
sl@0
|
126 |
};
|
sl@0
|
127 |
|
sl@0
|
128 |
|
sl@0
|
129 |
char* GetPartitionType(unsigned char aType)
|
sl@0
|
130 |
{
|
sl@0
|
131 |
switch (aType)
|
sl@0
|
132 |
{
|
sl@0
|
133 |
case KPartitionTypeFAT12: return "FAT12";
|
sl@0
|
134 |
case KPartitionTypeFAT16: return "FAT16";
|
sl@0
|
135 |
case KPartitionTypeFAT16small: return "FAT16";
|
sl@0
|
136 |
case KPartitionTypeWin95FAT32: return "FAT32";
|
sl@0
|
137 |
default:
|
sl@0
|
138 |
return "????";
|
sl@0
|
139 |
|
sl@0
|
140 |
}
|
sl@0
|
141 |
}
|
sl@0
|
142 |
|
sl@0
|
143 |
void ReadMbr(unsigned char* aMbrBuf, int aDiskSizeInSectors, BOOL aHexDump)
|
sl@0
|
144 |
{
|
sl@0
|
145 |
// printf("Reading MBR...\n");
|
sl@0
|
146 |
if (aHexDump)
|
sl@0
|
147 |
{
|
sl@0
|
148 |
int n;
|
sl@0
|
149 |
for (n=0; n<KDiskSectorSize; n+=16)
|
sl@0
|
150 |
{
|
sl@0
|
151 |
printf("%08X : %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
|
sl@0
|
152 |
n,
|
sl@0
|
153 |
aMbrBuf[n+0x00],aMbrBuf[n+0x01],aMbrBuf[n+0x02],aMbrBuf[n+0x03],aMbrBuf[n+0x04],aMbrBuf[n+0x05],aMbrBuf[n+0x06],aMbrBuf[n+0x07],
|
sl@0
|
154 |
aMbrBuf[n+0x08],aMbrBuf[n+0x09],aMbrBuf[n+0x0A],aMbrBuf[n+0x0B],aMbrBuf[n+0x0C],aMbrBuf[n+0x0D],aMbrBuf[n+0x0E],aMbrBuf[n+0x0F]);
|
sl@0
|
155 |
}
|
sl@0
|
156 |
}
|
sl@0
|
157 |
|
sl@0
|
158 |
|
sl@0
|
159 |
|
sl@0
|
160 |
if ((aMbrBuf[KMBRSignatureOffset+0] != 0x55) ||
|
sl@0
|
161 |
(aMbrBuf[KMBRSignatureOffset+1] != 0xAA))
|
sl@0
|
162 |
{
|
sl@0
|
163 |
printf("No valid MBR\n");
|
sl@0
|
164 |
return;
|
sl@0
|
165 |
}
|
sl@0
|
166 |
|
sl@0
|
167 |
int i;
|
sl@0
|
168 |
TMBRPartitionEntry *pe;
|
sl@0
|
169 |
for (i=0, pe = (TMBRPartitionEntry*)(&aMbrBuf[KMBRFirstPartitionOffset]); i < KMaxPartitionEntries && pe->iPartitionType != 0; i++, pe++)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
if (pe->iFirstSector + pe->iNumSectors > (DWORD) aDiskSizeInSectors)
|
sl@0
|
172 |
{
|
sl@0
|
173 |
printf("No valid MBR\n");
|
sl@0
|
174 |
return;
|
sl@0
|
175 |
}
|
sl@0
|
176 |
}
|
sl@0
|
177 |
|
sl@0
|
178 |
printf("Partitions: \n");
|
sl@0
|
179 |
printf(" # Name Type Fat BtI Hed Sct Cyl Hed Sct Cyl FirstSect NumSectors\n");
|
sl@0
|
180 |
|
sl@0
|
181 |
for (i=0, pe = (TMBRPartitionEntry*)(&aMbrBuf[KMBRFirstPartitionOffset]); i < KMaxPartitionEntries && pe->iPartitionType != 0; i++, pe++)
|
sl@0
|
182 |
{
|
sl@0
|
183 |
char* partitionName = GetPartitionType(pe->iPartitionType);
|
sl@0
|
184 |
printf("%3d: %-6s %3u %3u %3u %3u %3u %3u %3u %3u %3u %10u %10u (%u MB)\n",
|
sl@0
|
185 |
i,
|
sl@0
|
186 |
partitionName,
|
sl@0
|
187 |
pe->iPartitionType,
|
sl@0
|
188 |
pe->IsValidDosPartition() || pe->IsValidFAT32Partition() ? 1 : 0,
|
sl@0
|
189 |
pe->iX86BootIndicator,
|
sl@0
|
190 |
pe->iStartHead,
|
sl@0
|
191 |
pe->iStartSector,
|
sl@0
|
192 |
pe->iStartCylinder,
|
sl@0
|
193 |
pe->iEndHead,
|
sl@0
|
194 |
pe->iEndSector,
|
sl@0
|
195 |
pe->iEndCylinder,
|
sl@0
|
196 |
pe->iFirstSector,
|
sl@0
|
197 |
pe->iNumSectors,
|
sl@0
|
198 |
pe->iNumSectors >> KMegaByteToSectorSizeShift);
|
sl@0
|
199 |
|
sl@0
|
200 |
if (pe->iPartitionType == KPartitionTypeHiddenNTFS)
|
sl@0
|
201 |
{
|
sl@0
|
202 |
printf("Drive contains an NTFS partition, aborting for safety\n");
|
sl@0
|
203 |
return;
|
sl@0
|
204 |
}
|
sl@0
|
205 |
}
|
sl@0
|
206 |
}
|
sl@0
|
207 |
|
sl@0
|
208 |
|
sl@0
|
209 |
BOOL WriteMbr(unsigned char* aMbrBuf, int aFatSectorCount)
|
sl@0
|
210 |
{
|
sl@0
|
211 |
TMBRPartitionEntry *pe=(TMBRPartitionEntry*)(&aMbrBuf[KMBRFirstPartitionOffset]);
|
sl@0
|
212 |
|
sl@0
|
213 |
// first partition starts at one MB
|
sl@0
|
214 |
int sectorStart = KMegaByteInSectors;
|
sl@0
|
215 |
|
sl@0
|
216 |
// Create FAT partition
|
sl@0
|
217 |
pe->iFirstSector = sectorStart;
|
sl@0
|
218 |
|
sl@0
|
219 |
|
sl@0
|
220 |
pe->iNumSectors = aFatSectorCount;
|
sl@0
|
221 |
pe->iX86BootIndicator = 0x00;
|
sl@0
|
222 |
if (pe->iNumSectors < 32680)
|
sl@0
|
223 |
pe->iPartitionType = KPartitionTypeFAT12;
|
sl@0
|
224 |
else if(pe->iNumSectors < 65536)
|
sl@0
|
225 |
pe->iPartitionType = KPartitionTypeFAT16small;
|
sl@0
|
226 |
else if (pe->iNumSectors < 1048576)
|
sl@0
|
227 |
pe->iPartitionType = KPartitionTypeFAT16;
|
sl@0
|
228 |
else
|
sl@0
|
229 |
pe->iPartitionType = KPartitionTypeWin95FAT32;
|
sl@0
|
230 |
sectorStart+= pe->iNumSectors;
|
sl@0
|
231 |
|
sl@0
|
232 |
aMbrBuf[KMBRSignatureOffset+0] = 0x55;
|
sl@0
|
233 |
aMbrBuf[KMBRSignatureOffset+1] = 0xAA;
|
sl@0
|
234 |
|
sl@0
|
235 |
return true;
|
sl@0
|
236 |
}
|
sl@0
|
237 |
|
sl@0
|
238 |
|
sl@0
|
239 |
int main(int argc,char *argv[])
|
sl@0
|
240 |
{
|
sl@0
|
241 |
|
sl@0
|
242 |
if (argc < 2)
|
sl@0
|
243 |
{
|
sl@0
|
244 |
printf("MbrUtil - Decodes and optionally writes or erases the Master Boot Record on a removable drive\n");
|
sl@0
|
245 |
printf("Syntax : MbrUtil <drive letter> [-e] [-w <FatSizeInMegabytes>] [FAT] [FAT32]\n");
|
sl@0
|
246 |
printf("Where :\n");
|
sl@0
|
247 |
printf("-e : erase Master Boot Record:\n");
|
sl@0
|
248 |
printf("-w : create FAT partition of size <FatSizeInMegabytes>:\n");
|
sl@0
|
249 |
printf("E.g. : MbrUtil f:\n");
|
sl@0
|
250 |
printf(" : MbrUtil f: -w 16 FAT\n");
|
sl@0
|
251 |
exit(0);
|
sl@0
|
252 |
}
|
sl@0
|
253 |
|
sl@0
|
254 |
bool writeMbr = false;
|
sl@0
|
255 |
bool eraseMbr = false;
|
sl@0
|
256 |
int fatSectorCount = 0;
|
sl@0
|
257 |
char* fatType = "FAT";
|
sl@0
|
258 |
|
sl@0
|
259 |
char driveLetter = (char) toupper(argv[1][0]);
|
sl@0
|
260 |
if ((strlen(argv[1]) > 2) ||
|
sl@0
|
261 |
(strlen(argv[1]) == 2 && argv[1][1] != ':') ||
|
sl@0
|
262 |
(driveLetter < 'A' || driveLetter > 'Z'))
|
sl@0
|
263 |
{
|
sl@0
|
264 |
printf("invalid drive letter");
|
sl@0
|
265 |
exit(4);
|
sl@0
|
266 |
}
|
sl@0
|
267 |
|
sl@0
|
268 |
for (int i=2; i<argc; i++)
|
sl@0
|
269 |
{
|
sl@0
|
270 |
if (strcmpi(argv[i], "-e") == 0)
|
sl@0
|
271 |
{
|
sl@0
|
272 |
eraseMbr = true;
|
sl@0
|
273 |
}
|
sl@0
|
274 |
else if (strcmpi(argv[i], "-w") == 0)
|
sl@0
|
275 |
{
|
sl@0
|
276 |
writeMbr = true;
|
sl@0
|
277 |
i++;
|
sl@0
|
278 |
if (i >= argc)
|
sl@0
|
279 |
{
|
sl@0
|
280 |
printf("no drive size specified");
|
sl@0
|
281 |
exit(4);
|
sl@0
|
282 |
}
|
sl@0
|
283 |
int fatSizeInMegabytes = atoi(argv[i]);
|
sl@0
|
284 |
fatSectorCount = fatSizeInMegabytes << KMegaByteToSectorSizeShift;
|
sl@0
|
285 |
// printf("fatSizeInMegabytes %d, fatSectorCount %d\n", fatSizeInMegabytes, fatSectorCount);
|
sl@0
|
286 |
}
|
sl@0
|
287 |
else if (strcmpi(argv[i], "FAT") == 0)
|
sl@0
|
288 |
{
|
sl@0
|
289 |
}
|
sl@0
|
290 |
else if (strcmpi(argv[i], "FAT32") == 0)
|
sl@0
|
291 |
{
|
sl@0
|
292 |
fatType = argv[i];
|
sl@0
|
293 |
}
|
sl@0
|
294 |
else
|
sl@0
|
295 |
{
|
sl@0
|
296 |
printf("invalid option");
|
sl@0
|
297 |
exit(4);
|
sl@0
|
298 |
}
|
sl@0
|
299 |
}
|
sl@0
|
300 |
|
sl@0
|
301 |
|
sl@0
|
302 |
char diskName[10] = "\\\\.\\?:";
|
sl@0
|
303 |
diskName[4] = driveLetter;
|
sl@0
|
304 |
char physDiskName[20] = "\\\\.\\PHYSICALDRIVE?";
|
sl@0
|
305 |
|
sl@0
|
306 |
|
sl@0
|
307 |
|
sl@0
|
308 |
DISK_GEOMETRY geometry;
|
sl@0
|
309 |
DWORD dummy;
|
sl@0
|
310 |
HANDLE logDeviceHandle;
|
sl@0
|
311 |
HANDLE physDeviceHandle;
|
sl@0
|
312 |
BOOL b;
|
sl@0
|
313 |
DWORD bytesRead;
|
sl@0
|
314 |
DWORD bytesWritten;
|
sl@0
|
315 |
DWORD dwRet;
|
sl@0
|
316 |
|
sl@0
|
317 |
//*****************************************************************************************
|
sl@0
|
318 |
// open logical drive...
|
sl@0
|
319 |
//*****************************************************************************************
|
sl@0
|
320 |
printf("Opening %s...\n", diskName);
|
sl@0
|
321 |
logDeviceHandle = CreateFileA(
|
sl@0
|
322 |
diskName,
|
sl@0
|
323 |
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE ,
|
sl@0
|
324 |
NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, NULL);
|
sl@0
|
325 |
if (logDeviceHandle == INVALID_HANDLE_VALUE)
|
sl@0
|
326 |
{
|
sl@0
|
327 |
printf("CreateFileA() returned INVALID_HANDLE_VALUE\n");
|
sl@0
|
328 |
exit(4);
|
sl@0
|
329 |
}
|
sl@0
|
330 |
|
sl@0
|
331 |
// query geometry
|
sl@0
|
332 |
b = DeviceIoControl(
|
sl@0
|
333 |
logDeviceHandle,
|
sl@0
|
334 |
IOCTL_DISK_GET_DRIVE_GEOMETRY,
|
sl@0
|
335 |
NULL,
|
sl@0
|
336 |
0,
|
sl@0
|
337 |
&geometry,
|
sl@0
|
338 |
sizeof(geometry),
|
sl@0
|
339 |
&dummy,
|
sl@0
|
340 |
(LPOVERLAPPED)NULL);
|
sl@0
|
341 |
if (!b)
|
sl@0
|
342 |
{
|
sl@0
|
343 |
printf("IOCTL_DISK_GET_DRIVE_GEOMETRY failed error %x", GetLastError());
|
sl@0
|
344 |
CloseHandle(logDeviceHandle);
|
sl@0
|
345 |
exit(4);
|
sl@0
|
346 |
}
|
sl@0
|
347 |
|
sl@0
|
348 |
LONGLONG diskSizeInBytes = geometry.Cylinders.QuadPart * geometry.TracksPerCylinder * geometry.SectorsPerTrack * KDiskSectorSize;
|
sl@0
|
349 |
int diskSizeInMegaBytes = (int) (diskSizeInBytes / KMegaByte);
|
sl@0
|
350 |
int diskSizeInSectors = (int) (diskSizeInBytes / KDiskSectorSize);
|
sl@0
|
351 |
|
sl@0
|
352 |
|
sl@0
|
353 |
printf("Drive %c MediaType: %d (%s). Size: %ld MBytes, %lu sectors\n",
|
sl@0
|
354 |
driveLetter,
|
sl@0
|
355 |
geometry.MediaType,
|
sl@0
|
356 |
geometry.MediaType==RemovableMedia ? "RemovableMedia" :
|
sl@0
|
357 |
geometry.MediaType==FixedMedia ? "FixedMedia" : "?",
|
sl@0
|
358 |
diskSizeInMegaBytes,
|
sl@0
|
359 |
diskSizeInSectors
|
sl@0
|
360 |
);
|
sl@0
|
361 |
// printf("Size: %llu sectors\n", diskSizeInSectors);
|
sl@0
|
362 |
|
sl@0
|
363 |
if (geometry.MediaType != RemovableMedia)
|
sl@0
|
364 |
{
|
sl@0
|
365 |
printf("Drive is not removable, exiting");
|
sl@0
|
366 |
CloseHandle(logDeviceHandle);
|
sl@0
|
367 |
exit(4);
|
sl@0
|
368 |
}
|
sl@0
|
369 |
|
sl@0
|
370 |
if (fatSectorCount + KMegaByteInSectors > diskSizeInSectors)
|
sl@0
|
371 |
{
|
sl@0
|
372 |
printf("Specified size is too big");
|
sl@0
|
373 |
CloseHandle(logDeviceHandle);
|
sl@0
|
374 |
exit(4);
|
sl@0
|
375 |
}
|
sl@0
|
376 |
|
sl@0
|
377 |
//*****************************************************************************************
|
sl@0
|
378 |
// Get physical device number
|
sl@0
|
379 |
//*****************************************************************************************
|
sl@0
|
380 |
STORAGE_DEVICE_NUMBER storageDeviceNumber;
|
sl@0
|
381 |
b = IOCTL_STORAGE_GET_DEVICE_NUMBER;
|
sl@0
|
382 |
b = DeviceIoControl(
|
sl@0
|
383 |
logDeviceHandle,
|
sl@0
|
384 |
IOCTL_STORAGE_GET_DEVICE_NUMBER, NULL, 0, &storageDeviceNumber, sizeof(storageDeviceNumber), &bytesRead, 0);
|
sl@0
|
385 |
// printf("IOCTL_STORAGE_GET_DEVICE_NUMBER b %d DeviceNumber %d\n", b, storageDeviceNumber.DeviceNumber);
|
sl@0
|
386 |
if (!b)
|
sl@0
|
387 |
{
|
sl@0
|
388 |
printf("IOCTL_STORAGE_GET_DEVICE_NUMBER failed error %x", GetLastError());
|
sl@0
|
389 |
CloseHandle(logDeviceHandle);
|
sl@0
|
390 |
exit(4);
|
sl@0
|
391 |
}
|
sl@0
|
392 |
|
sl@0
|
393 |
|
sl@0
|
394 |
|
sl@0
|
395 |
//*****************************************************************************************
|
sl@0
|
396 |
// open physical drive...
|
sl@0
|
397 |
//*****************************************************************************************
|
sl@0
|
398 |
physDiskName[strlen(physDiskName)-1] = (char) ('0' + storageDeviceNumber.DeviceNumber);
|
sl@0
|
399 |
|
sl@0
|
400 |
printf("Opening %s...\n", physDiskName);
|
sl@0
|
401 |
physDeviceHandle = CreateFileA(
|
sl@0
|
402 |
physDiskName,
|
sl@0
|
403 |
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE ,
|
sl@0
|
404 |
NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, NULL);
|
sl@0
|
405 |
if (physDeviceHandle == INVALID_HANDLE_VALUE)
|
sl@0
|
406 |
{
|
sl@0
|
407 |
printf("CreateFileA() returned INVALID_HANDLE_VALUE\n");
|
sl@0
|
408 |
exit(4);
|
sl@0
|
409 |
}
|
sl@0
|
410 |
|
sl@0
|
411 |
|
sl@0
|
412 |
|
sl@0
|
413 |
|
sl@0
|
414 |
//*******************************************************
|
sl@0
|
415 |
// Read first sector
|
sl@0
|
416 |
//*******************************************************
|
sl@0
|
417 |
dwRet = SetFilePointer(
|
sl@0
|
418 |
physDeviceHandle, 0, 0, FILE_BEGIN);
|
sl@0
|
419 |
if (dwRet != 0)
|
sl@0
|
420 |
{
|
sl@0
|
421 |
printf("Unable to set file pointer, exiting");
|
sl@0
|
422 |
CloseHandle(physDeviceHandle);
|
sl@0
|
423 |
CloseHandle(logDeviceHandle);
|
sl@0
|
424 |
exit(4);
|
sl@0
|
425 |
}
|
sl@0
|
426 |
if (!ReadFile (physDeviceHandle, TheBuffer, KSectorSize, &bytesRead, NULL) )
|
sl@0
|
427 |
{
|
sl@0
|
428 |
printf("ReadFile failed with %d", GetLastError());
|
sl@0
|
429 |
CloseHandle(physDeviceHandle);
|
sl@0
|
430 |
CloseHandle(logDeviceHandle);
|
sl@0
|
431 |
exit(4);
|
sl@0
|
432 |
}
|
sl@0
|
433 |
if(bytesRead != KSectorSize)
|
sl@0
|
434 |
{
|
sl@0
|
435 |
printf("ReadFile length too small: %d", bytesRead);
|
sl@0
|
436 |
CloseHandle(physDeviceHandle);
|
sl@0
|
437 |
CloseHandle(logDeviceHandle);
|
sl@0
|
438 |
exit(4);
|
sl@0
|
439 |
}
|
sl@0
|
440 |
|
sl@0
|
441 |
//*******************************************************
|
sl@0
|
442 |
// Interpret MBR
|
sl@0
|
443 |
//*******************************************************
|
sl@0
|
444 |
ReadMbr(TheBuffer, diskSizeInSectors, false);
|
sl@0
|
445 |
|
sl@0
|
446 |
//*******************************************************
|
sl@0
|
447 |
// Write new MBR
|
sl@0
|
448 |
//*******************************************************
|
sl@0
|
449 |
if (writeMbr)
|
sl@0
|
450 |
{
|
sl@0
|
451 |
printf("Writing MBR...\n");
|
sl@0
|
452 |
memset(TheBuffer, 0, sizeof(TheBuffer));
|
sl@0
|
453 |
b = WriteMbr(TheBuffer, fatSectorCount);
|
sl@0
|
454 |
if (!b)
|
sl@0
|
455 |
writeMbr = eraseMbr = false;
|
sl@0
|
456 |
}
|
sl@0
|
457 |
if (eraseMbr)
|
sl@0
|
458 |
{
|
sl@0
|
459 |
printf("Erasing MBR...\n");
|
sl@0
|
460 |
memset(TheBuffer, 0, sizeof(TheBuffer));
|
sl@0
|
461 |
}
|
sl@0
|
462 |
if (writeMbr || eraseMbr)
|
sl@0
|
463 |
{
|
sl@0
|
464 |
// Write first sector
|
sl@0
|
465 |
dwRet = SetFilePointer(
|
sl@0
|
466 |
physDeviceHandle, 0, 0, FILE_BEGIN);
|
sl@0
|
467 |
if (dwRet != 0)
|
sl@0
|
468 |
{
|
sl@0
|
469 |
printf("Unable to set file pointer, exiting");
|
sl@0
|
470 |
CloseHandle(physDeviceHandle);
|
sl@0
|
471 |
CloseHandle(logDeviceHandle);
|
sl@0
|
472 |
exit(4);
|
sl@0
|
473 |
}
|
sl@0
|
474 |
if (!WriteFile (physDeviceHandle, TheBuffer, KSectorSize, &bytesWritten, NULL) )
|
sl@0
|
475 |
{
|
sl@0
|
476 |
printf("WriteFile failed with %d", GetLastError());
|
sl@0
|
477 |
CloseHandle(physDeviceHandle);
|
sl@0
|
478 |
CloseHandle(logDeviceHandle);
|
sl@0
|
479 |
exit(4);
|
sl@0
|
480 |
}
|
sl@0
|
481 |
if(bytesWritten != KSectorSize)
|
sl@0
|
482 |
{
|
sl@0
|
483 |
printf("WriteFile length too small: %d", bytesWritten);
|
sl@0
|
484 |
CloseHandle(physDeviceHandle);
|
sl@0
|
485 |
CloseHandle(logDeviceHandle);
|
sl@0
|
486 |
exit(4);
|
sl@0
|
487 |
}
|
sl@0
|
488 |
if (writeMbr)
|
sl@0
|
489 |
ReadMbr(TheBuffer, diskSizeInSectors, false);
|
sl@0
|
490 |
}
|
sl@0
|
491 |
|
sl@0
|
492 |
|
sl@0
|
493 |
CloseHandle(physDeviceHandle);
|
sl@0
|
494 |
|
sl@0
|
495 |
|
sl@0
|
496 |
if (writeMbr || eraseMbr)
|
sl@0
|
497 |
{
|
sl@0
|
498 |
printf("Dismounting %s...\n", diskName);
|
sl@0
|
499 |
b = DeviceIoControl(
|
sl@0
|
500 |
logDeviceHandle,
|
sl@0
|
501 |
FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &bytesRead, 0);
|
sl@0
|
502 |
if (!b)
|
sl@0
|
503 |
printf("FSCTL_DISMOUNT_VOLUME b %d err %d\n", b, GetLastError());
|
sl@0
|
504 |
}
|
sl@0
|
505 |
|
sl@0
|
506 |
CloseHandle(logDeviceHandle);
|
sl@0
|
507 |
|
sl@0
|
508 |
//*******************************************************
|
sl@0
|
509 |
// Format the disk
|
sl@0
|
510 |
//*******************************************************
|
sl@0
|
511 |
if (writeMbr || eraseMbr)
|
sl@0
|
512 |
{
|
sl@0
|
513 |
printf("\n");
|
sl@0
|
514 |
char prompt[80];
|
sl@0
|
515 |
sprintf(prompt, "Eject card from drive %C: and press ENTER when ready...\n", driveLetter);
|
sl@0
|
516 |
printf(prompt);
|
sl@0
|
517 |
getchar();
|
sl@0
|
518 |
|
sl@0
|
519 |
char winCmd[1024];
|
sl@0
|
520 |
char cmdArgs[1024];
|
sl@0
|
521 |
STARTUPINFO si = {0,};
|
sl@0
|
522 |
PROCESS_INFORMATION pi;
|
sl@0
|
523 |
|
sl@0
|
524 |
GetWindowsDirectory(winCmd, sizeof(winCmd));
|
sl@0
|
525 |
strncat(winCmd, "\\system32\\cmd.exe", sizeof(winCmd));
|
sl@0
|
526 |
sprintf(cmdArgs, "/C format %c: /FS:%s /Q /X", driveLetter, fatType);
|
sl@0
|
527 |
printf("Executing : %s %s\n", winCmd, cmdArgs);
|
sl@0
|
528 |
b = CreateProcessA(
|
sl@0
|
529 |
winCmd, //__in_opt LPCTSTR lpApplicationName,
|
sl@0
|
530 |
cmdArgs, //__inout_opt LPTSTR lpCommandLine,
|
sl@0
|
531 |
NULL, //__in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
|
sl@0
|
532 |
NULL, //__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
|
sl@0
|
533 |
FALSE, //__in BOOL bInheritHandles,
|
sl@0
|
534 |
0, //__in DWORD dwCreationFlags,
|
sl@0
|
535 |
NULL, //__in_opt LPVOID lpEnvironment,
|
sl@0
|
536 |
NULL, //__in_opt LPCTSTR lpCurrentDirectory,
|
sl@0
|
537 |
&si, //__in LPSTARTUPINFO lpStartupInfo,
|
sl@0
|
538 |
&pi); //__out LPPROCESS_INFORMATION lpProcessInformation
|
sl@0
|
539 |
if (!b)
|
sl@0
|
540 |
printf("CreateProcess failed with %d", GetLastError());
|
sl@0
|
541 |
|
sl@0
|
542 |
dwRet = WaitForSingleObject(pi.hProcess, INFINITE);
|
sl@0
|
543 |
CloseHandle(pi.hThread);
|
sl@0
|
544 |
CloseHandle(pi.hProcess);
|
sl@0
|
545 |
|
sl@0
|
546 |
}
|
sl@0
|
547 |
|
sl@0
|
548 |
return 0;
|
sl@0
|
549 |
} |