-
Notifications
You must be signed in to change notification settings - Fork 4
/
BlockDev.pas
326 lines (275 loc) · 8.71 KB
/
BlockDev.pas
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
unit BlockDev;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DiskIO, WinIOCTL;
type
TBlockDevice = class(TObject)
public
constructor Create; virtual;
destructor Destroy; override;
procedure ReadPhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer); virtual; abstract;
procedure WritePhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer); virtual; abstract;
function Open : Boolean; virtual; abstract;
function Close : Boolean; virtual; abstract;
end;
TWin95Disk = class(TBlockDevice)
public
constructor Create; override;
destructor Destroy; override;
procedure ReadPhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer); override;
procedure WritePhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer); override;
function Open : Boolean; override;
function Close : Boolean; override;
function SetDiskNumber(Drive : DWORD) : Boolean;
procedure SetOffset(Offset : DWORD);
private
Disk : T95Disk;
SectorOffset : DWORD;
end;
TNTDisk = class(TBlockDevice)
public
constructor Create; override;
destructor Destroy; override;
procedure ReadPhysicalSector (Sector : DWORD; count : DWORD; Buffer : Pointer); override;
procedure WritePhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer); override;
function Open : Boolean; override;
function Close : Boolean; override;
procedure SetFileName(Name : String);
procedure SetMode(Writeable : Boolean);
procedure SetPartition(Start : _Large_Integer; Length : _Large_Integer);
private
FileName : String;
h : THandle;
Writeable : Boolean;
SectorSize : DWORD;
Start : _Large_Integer; //longlong; // start
Length : _Large_Integer; //longlong; // length
end;
{ TVirtualDisk = class(TNTDisk)
public
constructor Create; override;
destructor Destroy; override;
procedure ReadPhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer); override;
procedure WritePhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer); override;
end;}
implementation
uses rawwrite;
//////////////////////////////
// TBlockDevice
//////////////////////////////
constructor TBlockDevice.Create;
begin
inherited Create;
// Debug('Creating BlockDevice', DebugHigh);
end;
destructor TBlockDevice.Destroy;
begin
Debug('Destroying BlockDevice', DebugHigh);
inherited Destroy;
end;
//////////////////////////////
// TWin95Disk
//////////////////////////////
// This is a type of block device, which uses a 16bit DLL to access
// physical disk sectors
constructor TWin95Disk.Create;
begin
inherited Create;
Debug('Creating Win95Disk', DebugHigh);
Disk := T95Disk.Create;
end;
destructor TWin95Disk.Destroy;
begin
Debug('Destroying Win95Disk', DebugHigh);
Disk.Free;
inherited Destroy;
end;
function TWin95Disk.SetDiskNumber(Drive : DWORD) : Boolean;
begin
Result := Disk.SetDisk(Drive);
end;
procedure TWin95Disk.SetOffset(Offset : DWORD);
begin
SectorOffset := Offset;
end;
function TWin95Disk.Open : Boolean;
begin
Result := True;
end;
function TWin95Disk.Close : Boolean;
begin
Result := True;
end;
procedure TWin95Disk.ReadPhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer);
begin
Debug('Reading sector ' + IntToStr(Sector) + ' count = ' + IntToStr(count), DebugHigh);
if not Disk.ReadSector(Sector + SectorOffset, Buffer, Count) then
begin
raise Exception.Create('Error reading disk');
end;
end;
procedure TWin95Disk.WritePhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer);
begin
Debug('Writing sector ' + IntToStr(Sector) + ' count = ' + IntToStr(count), DebugHigh);
if not Disk.WriteSector(Sector + SectorOffset, Buffer, Count) then
begin
raise Exception.Create('Error writing to disk');
end;
end;
//////////////////////////////
// TNTDisk
//////////////////////////////
// This is a type of block device which uses the
// WIN32 API
constructor TNTDisk.Create;
begin
inherited Create;
Debug('Creating NTDisk', DebugHigh);
h := INVALID_HANDLE_VALUE;
Writeable := False;
SectorSize := 512;
end;
destructor TNTDisk.Destroy;
begin
Debug('Destroying NTDisk', DebugHigh);
Close;
inherited Destroy;
end;
function TNTDisk.Open : Boolean;
begin
if Writeable then
begin
h := Windows.CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ , nil, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
end
else
begin
h := Windows.CreateFile(PChar(FileName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, 0);
end;
if h <> INVALID_HANDLE_VALUE then
begin
Result := True;
end
else
begin
Result := False;
end;
end;
function TNTDisk.Close : Boolean;
begin
if h <> INVALID_HANDLE_VALUE then
begin
CloseHandle(h);
end;
h := INVALID_HANDLE_VALUE;
Result := True;
end;
procedure TNTDisk.SetFileName(Name : String);
begin
FileName := Name;
end;
procedure TNTDisk.SetMode(Writeable : Boolean);
begin
self.Writeable := Writeable;
end;
procedure TNTDisk.SetPartition(Start : _Large_Integer; Length : _Large_Integer);
begin
self.Start.QuadPart := Start.QuadPart;
self.Length.QuadPart := Length.QuadPart;
end;
procedure TNTDisk.ReadPhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer);
var
ReadStart : _Large_Integer;
ReadLength : DWORD;
ActualLengthRead : DWORD;
Seek : DWORD;
Error : DWORD;
ErrorMsg : String;
begin
Debug('Reading sector ' + IntToStr(Sector) + ' count = ' + IntToStr(count), DebugHigh);
// check for a valid handle
if h <> INVALID_HANDLE_VALUE then
begin
// Debug('Read sector', DebugOff);
// work out the start address (of partition/file)
ReadStart.QuadPart := SectorSize;
ReadStart.QuadPart := ReadStart.QuadPart * Sector;
ReadStart.QuadPart := ReadStart.QuadPart + Start.Quadpart;
ReadLength := Count * SectorSize;
// seek to the correct pos
Seek := SetFilePointer(h, ReadStart.LowPart, @ReadStart.HighPart, FILE_BEGIN);
if Seek <> $FFFFFFFF then
begin
// seek successful, lets read
if not ReadFile2(h, Buffer, ReadLength, ActualLengthRead, nil) then
begin
Error := GetLastError;
ErrorMsg := 'Read failed error = ' + IntToStr(Error) + '(' + SysErrorMessage(Error) + ')';
Debug(ErrorMsg, DebugOff);
raise Exception.Create(ErrorMsg);
end
else if ReadLength <> ActualLengthRead then
begin
Debug('Possible error, only ' + IntToStr(ActualLengthRead) + ' bytes read', DebugLow);
end;
end
else
begin
// error
Error := GetLastError;
ErrorMsg := 'Seek failed error = ' + IntToStr(Error) + '(' + SysErrorMessage(Error) + ')';
Debug(ErrorMsg, DebugOff);
raise Exception.Create(ErrorMsg);
end;
end;
end;
procedure TNTDisk.WritePhysicalSector(Sector : DWORD; count : DWORD; Buffer : Pointer);
var
WriteStart : _Large_Integer;
WriteLength : DWORD;
ActualLengthWritten : DWORD;
Seek : DWORD;
Error : DWORD;
begin
// check for a valid handle
if h <> INVALID_HANDLE_VALUE then
begin
Debug('Write sector ' + IntToStr(Sector) + ' count = ' + IntToStr(Count), DebugOff);
// work out the start address (of partition/file)
WriteStart.QuadPart := Start.QuadPart + (Sector * SectorSize);
WriteLength := Count * SectorSize;
// seek to the correct pos
Seek := SetFilePointer(h, WriteStart.LowPart, @WriteStart.HighPart, FILE_BEGIN);
if Seek <> $FFFFFFFF then
begin
// seek successful, lets read
if not WriteFile2(h, Buffer, WriteLength, ActualLengthWritten, nil) then
begin
Error := GetLastError;
Debug('Write failed error=' + IntToStr(GetLastError), DebugOff);
raise Exception.Create('Write Failed: (' + IntToStr(GetLastError) + ')'#10 + SysErrorMessage(Error));
end
else if WriteLength <> ActualLengthWritten then
begin
Debug('Possible error, only ' + IntToStr(ActualLengthWritten) + ' bytes written', DebugLow);
end;
end
else
begin
// error
Debug('Seek failed error=' + IntToStr(GetLastError), DebugOff);
raise Exception.Create('Seek failed');
end;
end;
end;
{constructor TVirtualDisk.Create;
begin
inherited Create;
Debug('Creating VirtualDisk', DebugHigh);
end;
destructor TVirtualDisk.Destroy;
begin
Debug('Destroying VirtualDisk', DebugHigh);
inherited Destroy;
end;}
end.