-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDiskSector.cpp
More file actions
668 lines (521 loc) · 17.8 KB
/
DiskSector.cpp
File metadata and controls
668 lines (521 loc) · 17.8 KB
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#include "DiskSector.h"
#include <stdio.h>
/* -----------------------------------------------------------------------------
* Copyright (c) 2003 Elias Bachaalany <lallousz-x86@yahoo.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* -----------------------------------------------------------------------------
Reference
----------
http://homepages.borland.com/efg2lab/Library/UseNet/2001/0625c.txt
http://www.winterdom.com/dev/ptk/lock.c
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q168/1/80.ASP&NoWebContent=1 (locking)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q174569 (Read/write sectors)
History
----------
02/17/2004 - Initial version
*/
#define VWIN32_DIOC_DOS_IOCTL 1 // specified MS-DOS device I/O ctl - Interrupt 21h Function 4400h - 4411h
#define VWIN32_DIOC_DOS_INT25 2 // Absolute Disk Read command - Interrupt 25h
#define VWIN32_DIOC_DOS_INT26 3 // Absolute Disk Write command - Interrupt 25h
#define VWIN32_DIOC_DOS_INT13 4 // Interrupt 13h commands
#define VWIN32_DIOC_SIMCTRLC 5 // Simulate Ctrl-C
#define VWIN32_DIOC_DOS_DRIVEINFO 6 // Interrupt 21h Function 730X commands
#define CARRY_FLAG 1
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER (DWORD)-1
#endif
#pragma pack(1)
typedef struct _DISKIO {
DWORD dwStartSector; // starting logical sector number
WORD wSectors; // number of sectors
DWORD dwBuffer; // address of read/write buffer
} DISKIO, * PDISKIO;
typedef struct _DIOC_REGISTERS
{
DWORD reg_EBX;
DWORD reg_EDX;
DWORD reg_ECX;
DWORD reg_EAX;
DWORD reg_EDI;
DWORD reg_ESI;
DWORD reg_Flags;
} DIOC_REGISTERS, *PDIOC_REGISTERS;
#pragma pack()
// -----------------------------------------------------------------------------------
// DiskSector WinNT
//
bool DiskSectorWinNT::Open(char *vol)
{
char szDrive[10];
sprintf(szDrive, "\\\\.\\%c:", vol[0]);
m_hDisk = ::CreateFile(
szDrive,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
return m_hDisk != INVALID_HANDLE_VALUE;
}
void DiskSectorWinNT::Close()
{
if (m_hDisk != INVALID_HANDLE_VALUE)
::CloseHandle(m_hDisk);
}
bool DiskSectorWinNT::ReadSector (DWORD sector, char *Buffer, int sectorSize)
{
DWORD read = 0;
if (::SetFilePointer(m_hDisk, sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
return false;
if (!::ReadFile(m_hDisk, Buffer, sectorSize, &read, NULL))
return false;
return true;
}
bool DiskSectorWinNT::WriteSector(DWORD sector, char *Buffer, int sectorSize)
{
DWORD wrote = 0;
if (::SetFilePointer(m_hDisk, sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
return false;
if (!::WriteFile(m_hDisk, Buffer, sectorSize, &wrote, NULL))
return false;
return true;
}
// -----------------------------------------------------------------------------------
// DiskSector
//
DiskSector::DiskSector()
{
if (GetVersion() > 0x80000000)
util = new DiskSectorWin9x;
else
util = new DiskSectorWinNT;
}
void DiskSector::Close()
{
util->Close();
}
bool DiskSector::Open(char *vol)
{
return util->Open(vol);
}
bool DiskSector::WriteSector(DWORD sector, char *Buffer, int sectorSize)
{
return util->WriteSector(sector, Buffer, sectorSize);
}
bool DiskSector::ReadSector(DWORD sector, char *Buffer, int sectorSize)
{
return util->ReadSector(sector, Buffer, sectorSize);
}
DiskSector::~DiskSector()
{
delete util;
}
// -----------------------------------------------------------------------------------
// DiskSector Win9x
//
bool DiskSectorWin9x::Open(char *vol)
{
OSVERSIONINFOEX osvi = {0};
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
m_bOpened = false;
if (!::GetVersionEx((OSVERSIONINFO *)&osvi))
{
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (!::GetVersionEx ((OSVERSIONINFO *)&osvi))
return false;
}
if (osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS)
return false;
m_chDrive = toupper(vol[0]);
m_nDriveNo = m_chDrive - 'A' + 1;
char temp[10] = {0};
sprintf(temp, "%c:\\", m_chDrive);
if (::GetDriveType(temp) != DRIVE_FIXED)
return false;
m_bW9xOsr2AndAbove = (osvi.dwMajorVersion >= 4 && osvi.dwMinorVersion >= 10)
||
(
(osvi.dwBuildNumber == 4 && osvi.dwMinorVersion == 0) &&
(osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B')
);
m_hVmm32 = ::CreateFile(
"\\\\.\\VWIN32", // name
0, // access mode
0, // share mode
NULL, // security descriptor
0, // ho to create
FILE_FLAG_DELETE_ON_CLOSE, // file attributes
NULL); // handle to file with att to copy
m_bOpened = (m_hVmm32 != INVALID_HANDLE_VALUE);
return m_bOpened;
}
void DiskSectorWin9x::Close()
{
if (m_bOpened)
::CloseHandle(m_hVmm32);
m_bOpened = false;
}
/*------------------------------------------------------------------
ReadLogicalSectors (hDev, bDrive, dwStartSector, wSectors, lpSectBuff)
Purpose:
Reads sectors from a logical drive. Uses Int 25h.
Parameters:
hDev
Handle of VWIN32
bDrive
The MS-DOS logical drive number. 1 = A, 2 = B, 3 = C, etc.
dwStartSector
The first logical sector to read
wSectors
The number of sectors to read
lpSectBuff
The caller-supplied buffer that will contain the sector data
Return Value:
Returns TRUE if successful, or FALSE if failure.
Comments:
This function does not validate its parameters.
------------------------------------------------------------------*/
bool DiskSectorWin9x::ReadLogicalSectors (HANDLE hDev,
BYTE bDrive,
DWORD dwStartSector,
WORD wSectors,
LPBYTE lpSectBuff)
{
BOOL fResult;
DWORD cb;
DIOC_REGISTERS reg = {0};
DISKIO dio = {0};
dio.dwStartSector = dwStartSector;
dio.wSectors = wSectors;
dio.dwBuffer = (DWORD)lpSectBuff;
reg.reg_EAX = bDrive - 1; // Int 25h drive numbers are 0-based.
reg.reg_EBX = (DWORD)&dio; // Drive letter 0 = A, 1 = B 2 = C ect..
reg.reg_ECX = 0xFFFF; // use DISKIO struct
fResult = ::DeviceIoControl(hDev, VWIN32_DIOC_DOS_INT25,
®, sizeof(reg),
®, sizeof(reg), &cb, 0);
// Determine if the DeviceIoControl call and the read succeeded.
fResult = fResult && !(reg.reg_Flags & CARRY_FLAG);
return fResult == TRUE;
}
/*------------------------------------------------------------------
WriteLogicalSectors (hDev, bDrive, dwStartSector, wSectors, lpSectBuff)
Purpose:
Writes sectors to a logical drive. Uses Int 26h
Parameters:
hDev
Handle of VWIN32
bDrive
The MS-DOS logical drive number. 1 = A, 2 = B, 3 = C, etc.
dwStartSector
The first logical sector to write
wSectors
The number of sectors to write
lpSectBuff
The caller-supplied buffer that contains the sector data
Return Value:
Returns TRUE if successful, or FALSE if failure.
Comments:
This function does not validate its parameters.
------------------------------------------------------------------*/
bool DiskSectorWin9x::WriteLogicalSectors (HANDLE hDev,
BYTE bDrive,
DWORD dwStartSector,
WORD wSectors,
LPBYTE lpSectBuff)
{
BOOL fResult;
DWORD cb;
DIOC_REGISTERS reg = {0};
DISKIO dio = {0};
dio.dwStartSector = dwStartSector;
dio.wSectors = wSectors;
dio.dwBuffer = (DWORD)lpSectBuff;
reg.reg_EAX = bDrive - 1; // Int 26h drive numbers are 0-based.
reg.reg_EBX = (DWORD)&dio;
reg.reg_ECX = 0xFFFF; // use DISKIO struct
fResult = ::DeviceIoControl(hDev, VWIN32_DIOC_DOS_INT26,
®, sizeof(reg),
®, sizeof(reg), &cb, 0);
// Determine if the DeviceIoControl call and the write succeeded.
fResult = fResult && !(reg.reg_Flags & CARRY_FLAG);
return fResult == TRUE;
}
/*------------------------------------------------------------------
NewReadSectors(hDev, bDrive, dwStartSector, wSectors, lpSectBuff)
Purpose:
Reads the specified number of sectors into a caller-supplied
buffer. Uses Int 21h function 7305h
Parameters:
hDev
Handle of VWIN32
bDrive
The MS-DOS logical drive number. 0 = default, 1 = A, 2 = B,
3 = C, etc.
dwStartSector
The first sector to read.
wSectors
The number of sectors to read.
lpSectBuff
The caller-supplied buffer to read into.
Return Value:
Returns TRUE if successful, or FALSE if failure.
Comments:
This function does not validate its parameters. It assumes that
lpSectBuff is allocated by the caller and is large enough to
hold all of the data from all of the sectors being read.
------------------------------------------------------------------*/
bool DiskSectorWin9x::NewReadSectors(HANDLE hDev,
BYTE bDrive,
DWORD dwStartSector,
WORD wSectors,
LPBYTE lpSectBuff)
{
BOOL fResult;
DWORD cb;
DIOC_REGISTERS reg = {0};
DISKIO dio;
dio.dwStartSector = dwStartSector;
dio.wSectors = wSectors;
dio.dwBuffer = (DWORD)lpSectBuff;
reg.reg_EAX = 0x7305; // Ext_ABSDiskReadWrite
reg.reg_EBX = (DWORD)&dio;
reg.reg_ECX = -1;
reg.reg_EDX = bDrive; // Int 21h, fn 7305h drive numbers are 1-based
fResult = ::DeviceIoControl(hDev, VWIN32_DIOC_DOS_DRIVEINFO,
®, sizeof(reg),
®, sizeof(reg), &cb, 0);
// Determine if the DeviceIoControl call and the read succeeded.
fResult = fResult && !(reg.reg_Flags & CARRY_FLAG);
return fResult == TRUE;
}
/*------------------------------------------------------------------
NewWriteSectors(hDev, bDrive, dwStartSector, wSectors, lpSectBuff)
Purpose:
Writes the specified number of sectors from a caller-supplied
buffer. Uses Int 21h function 7305h
Parameters:
hDev
Handle of VWIN32
bDrive
The MS-DOS logical drive number. 0 = default, 1 = A, 2 = B,
3 = C, etc.
dwStartSector
The first sector to write.
wSectors
The number of sectors to write.
lpSectBuff
The caller-supplied buffer from which to write.
Return Value:
Returns TRUE if successful, or FALSE if failure.
Comments:
This function does not validate its parameters. It assumes that
lpSectBuff is allocated by the caller and is large enough to
hold all of the data to be written.
------------------------------------------------------------------*/
bool DiskSectorWin9x::NewWriteSectors(HANDLE hDev,
BYTE bDrive,
DWORD dwStartSector,
WORD wSectors,
LPBYTE lpSectBuff)
{
BOOL fResult;
DWORD cb;
DIOC_REGISTERS reg = {0};
DISKIO dio;
dio.dwStartSector = dwStartSector;
dio.wSectors = wSectors;
dio.dwBuffer = (DWORD)lpSectBuff;
reg.reg_EAX = 0x7305; // Ext_ABSDiskReadWrite
reg.reg_EBX = (DWORD)&dio;
reg.reg_ECX = -1;
reg.reg_EDX = bDrive; // Int 21h, fn 7305h drive numbers are 1-based
reg.reg_ESI = 0x6001; // Normal file data/write (See function
// documentation for other values)
fResult = ::DeviceIoControl(hDev, VWIN32_DIOC_DOS_DRIVEINFO,
®, sizeof(reg),
®, sizeof(reg), &cb, 0);
// Determine if the DeviceIoControl call and the write succeeded.
fResult = fResult && !(reg.reg_Flags & CARRY_FLAG);
return fResult == TRUE;
}
/*-----------------------------------------------------------------------
LockLogicalVolume (hVWin32, bDriveNum, bLockLevel, wPermissions)
Purpose:
Takes a logical volume lock on a logical volume.
Parameters:
hVWin32
An open handle to VWIN32.
bDriveNum
The logical drive number to lock. 0 = default, 1 = A:, 2 = B:,
3 = C:, etc.
bLockLevel
Can be 0, 1, 2, or 3. Level 0 is an exclusive lock that can only
be taken when there are no open files on the specified drive.
Levels 1 through 3 form a hierarchy where 1 must be taken before
2, which must be taken before 3.
wPermissions
Specifies how the lock will affect file operations when lock levels
1 through 3 are taken. Also specifies whether a formatting lock
should be taken after a level 0 lock.
Zero is a valid permission.
Return Value:
If successful, returns TRUE. If unsuccessful, returns FALSE.
-----------------------------------------------------------------------*/
bool DiskSectorWin9x::LockLogicalVolume (HANDLE hVWin32,
BYTE bDriveNum,
BYTE bLockLevel,
WORD wPermissions)
{
BOOL fResult;
DIOC_REGISTERS regs = {0};
BYTE bDeviceCat; // can be either 0x48 or 0x08
DWORD cb;
/*
Try first with device category 0x48 for FAT32 volumes. If it
doesn't work, try again with device category 0x08. If that
doesn't work, then the lock failed.
*/
bDeviceCat = 0x48;
ATTEMPT_AGAIN:
// Set up the parameters for the call.
regs.reg_EAX = 0x440D;
regs.reg_EBX = MAKEWORD(bDriveNum, bLockLevel);
regs.reg_ECX = MAKEWORD(0x4A, bDeviceCat);
regs.reg_EDX = wPermissions;
fResult = ::DeviceIoControl (hVWin32, VWIN32_DIOC_DOS_IOCTL,
®s, sizeof(regs), ®s, sizeof(regs),
&cb, 0);
// See if DeviceIoControl and the lock succeeded
fResult = fResult && !(regs.reg_Flags & CARRY_FLAG);
// If DeviceIoControl or the lock failed, and device category 0x08
// hasn't been tried, retry the operation with device category 0x08.
if (!fResult && (bDeviceCat != 0x08))
{
bDeviceCat = 0x08;
goto ATTEMPT_AGAIN;
}
return fResult == TRUE;
}
/*-----------------------------------------------------------------------
UnlockLogicalVolume (hVWin32, bDriveNum)
Purpose:
Unlocks a logical volume that was locked with LockLogicalVolume().
Parameters:
hVWin32
An open handle to VWIN32.
bDriveNum
The logical drive number to unlock. 0 = default, 1 = A:, 2 = B:,
3 = C:, etc.
Return Value:
If successful, returns TRUE. If unsuccessful, returns FALSE.
Comments:
Must be called the same number of times as LockLogicalVolume() to
completely unlock a volume.
Only the lock owner can unlock a volume.
-----------------------------------------------------------------------*/
bool DiskSectorWin9x::UnlockLogicalVolume (HANDLE hVWin32, BYTE bDriveNum)
{
BOOL fResult;
DIOC_REGISTERS regs = {0};
BYTE bDeviceCat; // can be either 0x48 or 0x08
DWORD cb;
/* Try first with device category 0x48 for FAT32 volumes. If it
doesn't work, try again with device category 0x08. If that
doesn't work, then the unlock failed.
*/
bDeviceCat = 0x48;
ATTEMPT_AGAIN:
// Set up the parameters for the call.
regs.reg_EAX = 0x440D;
regs.reg_EBX = bDriveNum;
regs.reg_ECX = MAKEWORD(0x6A, bDeviceCat);
fResult = ::DeviceIoControl (hVWin32, VWIN32_DIOC_DOS_IOCTL,
®s, sizeof(regs), ®s, sizeof(regs),
&cb, 0);
// See if DeviceIoControl and the unlock succeeded
fResult = fResult && !(regs.reg_Flags & CARRY_FLAG);
// If DeviceIoControl or the unlock failed, and device category 0x08
// hasn't been tried, retry the operation with device category 0x08.
if (!fResult && (bDeviceCat != 0x08))
{
bDeviceCat = 0x08;
goto ATTEMPT_AGAIN;
}
return fResult == TRUE;
}
bool DiskSectorWin9x::ReadSector (DWORD sector, char *Buffer, int sectorSize)
{
if (!m_bOpened)
return false;
if (m_bUseLocking)
{
if (!LockLogicalVolume(m_hVmm32, m_nDriveNo, 1, 1))
return false;
if (!LockLogicalVolume(m_hVmm32, m_nDriveNo, 2, 0))
{
UnlockLogicalVolume(m_hVmm32, m_nDriveNo);
return false;
}
}
bool bRet;
if (m_bW9xOsr2AndAbove)
bRet = NewReadSectors(m_hVmm32, m_nDriveNo, sector, 1, (LPBYTE)Buffer);
else
bRet = ReadLogicalSectors(m_hVmm32, m_nDriveNo, sector, 1, (LPBYTE)Buffer);
if (m_bUseLocking)
{
UnlockLogicalVolume(m_hVmm32, m_nDriveNo);
UnlockLogicalVolume(m_hVmm32, m_nDriveNo);
}
return bRet;
}
bool DiskSectorWin9x::WriteSector (DWORD sector, char *Buffer, int sectorSize)
{
if (!m_bOpened)
return false;
if (!LockLogicalVolume(m_hVmm32, m_nDriveNo, 1, 1))
return false;
if (!LockLogicalVolume(m_hVmm32, m_nDriveNo, 2, 0))
{
UnlockLogicalVolume(m_hVmm32, m_nDriveNo);
return false;
}
if (!LockLogicalVolume(m_hVmm32, m_nDriveNo, 3, 0))
{
UnlockLogicalVolume(m_hVmm32, m_nDriveNo);
UnlockLogicalVolume(m_hVmm32, m_nDriveNo);
return false;
}
bool bRet;
if (m_bW9xOsr2AndAbove)
bRet = NewWriteSectors(m_hVmm32, m_nDriveNo, sector, 1, (LPBYTE) Buffer);
else
bRet = WriteLogicalSectors(m_hVmm32, m_nDriveNo, sector, 1, (LPBYTE) Buffer);
UnlockLogicalVolume(m_hVmm32, m_nDriveNo);
UnlockLogicalVolume(m_hVmm32, m_nDriveNo);
UnlockLogicalVolume(m_hVmm32, m_nDriveNo);
return bRet;
}