-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiskDevice.cpp
278 lines (245 loc) · 8.32 KB
/
DiskDevice.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
/* ES40 emulator.
* Copyright (C) 2007-2008 by the ES40 Emulator Project
*
* WWW : http://sourceforge.net/projects/es40
* E-mail : [email protected]
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Although this is not required, the author would appreciate being notified of,
* and receiving any modifications you may make to the source code that might serve
* the general public.
*/
/**
* \file
* Contains code to use a raw device as a disk image.
*
* $Id: DiskDevice.cpp,v 1.6 2008/02/27 12:04:22 iamcamiel Exp $
*
* X-1.6 Brian Wheeler 27-FEB-2008
* Avoid compiler warnings.
*
* X-1.5 Camiel Vanderhoeven 13-JAN-2008
* Use determine_layout in stead of calc_cylinders.
*
* X-1.4 Camiel Vanderhoeven 09-JAN-2008
* Save disk state to state file.
*
* X-1.3 Camiel Vanderhoeven 06-JAN-2008
* Set default blocksize to 2048 for cd-rom devices.
*
* X-1.2 Camiel Vanderhoeven 06-JAN-2008
* Support changing the block size (required for SCSI, ATAPI).
*
* X-1.1 Camiel Vanderhoeven 05-JAN-2008
* Initial version in CVS.
**/
#include "StdAfx.h"
#include "DiskDevice.h"
#if defined(_WIN32)
#include <WinIoCtl.h>
#endif
CDiskDevice::CDiskDevice(CConfigurator * cfg, CSystem * sys, CDiskController * c, int idebus, int idedev) : CDisk(cfg,sys,c,idebus,idedev)
{
filename = myCfg->get_text_value("device");
if (!filename)
{
printf("%s: Disk has no device attached!\n",devid_string);
throw((int)1);
}
if (read_only)
{
#if defined(_WIN32)
buffer = (char*)malloc(2048);
buffer_size = 2048;
handle = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
#else
handle = fopen(filename,"rb");
#endif
}
else
{
#if defined(_WIN32)
handle = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
#else
handle = fopen(filename,"rb+");
#endif
}
#if defined(_WIN32)
if (handle==INVALID_HANDLE_VALUE)
{
printf("%s: Could not open device %s!\n",devid_string,filename);
printf("%s: Error %ld.\n",devid_string,GetLastError());
throw((int)1);
}
#else
if (!handle)
{
printf("%s: Could not open device %s!\n",devid_string,filename);
throw((int)1);
}
#endif
// determine size...
#if defined(_WIN32)
DISK_GEOMETRY x;
DWORD bytesret;
if (!DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &x, sizeof(x), &bytesret, NULL))
{
printf("%s: Could not get drive geometry for %s!\n",devid_string,filename);
printf("%s: Error %ld.\n",devid_string,GetLastError());
throw((int)1);
}
sectors = x.SectorsPerTrack;
heads = x.TracksPerCylinder;
byte_size = x.Cylinders.QuadPart * x.TracksPerCylinder * x.SectorsPerTrack * x.BytesPerSector;
dev_block_size = x.BytesPerSector;
LARGE_INTEGER a;
a.QuadPart = 0;
SetFilePointerEx(handle, a, (PLARGE_INTEGER) &state.byte_pos, FILE_BEGIN);
#else
fseek_large(handle,0,SEEK_END);
byte_size=ftell_large(handle);
fseek_large(handle,0,SEEK_SET);
state.byte_pos = ftell_large(handle);
sectors = 32;
heads = 8;
#endif
//calc_cylinders();
determine_layout();
model_number=myCfg->get_text_value("model_number",filename);
printf("%s: Mounted device %s, %" LL "d %d-byte blocks, %" LL "d/%d/%d.\n",devid_string,filename,byte_size/state.block_size,state.block_size,cylinders,heads,sectors);
}
CDiskDevice::~CDiskDevice(void)
{
printf("%s: Closing file.\n",devid_string);
#if defined(_WIN32)
if (handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
#else
if (handle)
fclose(handle);
#endif
}
bool CDiskDevice::seek_byte(off_t_large byte)
{
if (byte >=byte_size)
{
printf("%s: Seek beyond end of file!\n",devid_string);
throw((int)1);
}
#if defined(_WIN32)
state.byte_pos = byte;
#else
fseek_large(handle,byte,SEEK_SET);
state.byte_pos = ftell_large(handle);
#endif
return true;
}
size_t CDiskDevice::read_bytes(void *dest, size_t bytes)
{
// printf("%s: read %d bytes @ %" LL "d.\n",devid_string,bytes,state.byte_pos);
#if defined(_WIN32)
off_t_large byte_from = (state.byte_pos/dev_block_size)*dev_block_size;
off_t_large byte_to = (((state.byte_pos+bytes-1)/dev_block_size)+1)*dev_block_size;
DWORD byte_len = (DWORD)(byte_to - byte_from);
DWORD byte_off = (DWORD)(state.byte_pos - byte_from);
LARGE_INTEGER a;
DWORD r;
if (byte_len > buffer_size)
{
buffer_size = byte_len;
CHECK_REALLOCATION(buffer,realloc(buffer,buffer_size),char);
// printf("%s: buffer enlarged to %d bytes.\n",devid_string,buffer_size);
}
a.QuadPart = byte_from;
SetFilePointerEx(handle, a, NULL, FILE_BEGIN);
ReadFile(handle,buffer,byte_len,&r,NULL);
if (r != (byte_len))
{
printf("%s: Tried to read %d bytes from pos %" LL "d, but could only read %d bytes!\n",devid_string,byte_len,byte_from,r);
printf("%s: Error %ld.\n",devid_string,GetLastError());
}
memcpy(dest,buffer+byte_off,bytes);
state.byte_pos += bytes;
return bytes;
#else
size_t r;
r = fread(dest,1,bytes,handle);
state.byte_pos = ftell_large(handle);
return r;
#endif
}
size_t CDiskDevice::write_bytes(void * src, size_t bytes)
{
if (read_only)
return 0;
#if defined(_WIN32)
off_t_large byte_from = (state.byte_pos/dev_block_size)*dev_block_size;
off_t_large byte_to = (((state.byte_pos+bytes-1)/dev_block_size)+1)*dev_block_size;
DWORD byte_len = (DWORD)(byte_to - byte_from);
DWORD byte_off = (DWORD)(state.byte_pos - byte_from);
LARGE_INTEGER a;
DWORD r;
if (byte_len > buffer_size)
{
buffer_size = byte_len;
CHECK_REALLOCATION(buffer,realloc(buffer,buffer_size),char);
}
if (byte_from != state.byte_pos)
{
// we don't write the entire first block, so we read it
// from disk first so we don't corrupt the disk
a.QuadPart = byte_from;
SetFilePointerEx(handle, a, NULL, FILE_BEGIN);
ReadFile(handle,buffer,(DWORD)dev_block_size,&r,NULL);
if (r != (dev_block_size))
{
printf("%s: Tried to read %d bytes from pos %" LL "d, but could only read %d bytes!\n",devid_string,dev_block_size,byte_from,r);
FAILURE("Error during device write operation. Terminating to avoid disk corruption.");
}
}
if ((byte_to != state.byte_pos+bytes) && (byte_to-byte_from>dev_block_size))
{
// we don't write the entire last block, so we read it
// from disk first so we don't corrupt the disk
a.QuadPart = byte_to-dev_block_size;
SetFilePointerEx(handle, a, NULL, FILE_BEGIN);
ReadFile(handle,buffer+byte_len-dev_block_size,(DWORD)dev_block_size,&r,NULL);
if (r != (dev_block_size))
{
printf("%s: Tried to read %d bytes from pos %" LL "d, but could only read %d bytes!\n",devid_string,dev_block_size,byte_to-dev_block_size,r);
FAILURE("Error during device write operation. Terminating to avoid disk corruption.");
}
}
// add the data we're writing to the buffer
memcpy(buffer+byte_off,src,bytes);
a.QuadPart = byte_from;
SetFilePointerEx(handle, a, NULL, FILE_BEGIN);
// and write the buffer to disk
WriteFile(handle,buffer,byte_len,&r,NULL);
if (r != byte_len)
{
printf("%s: Tried to write %d bytes to pos %" LL "d, but could only write %d bytes!\n",devid_string,byte_len,byte_from,r);
FAILURE("Error during device write operation. Terminating to avoid disk corruption.");
}
state.byte_pos += bytes;
return bytes;
#else
size_t r;
r = fwrite(src,1,bytes,handle);
state.byte_pos = ftell_large(handle);
return r;
#endif
}