root / block-raw-win32.c @ eae6ce52
History | View | Annotate | Download (13.6 kB)
1 | 223d4670 | ths | /*
|
---|---|---|---|
2 | 223d4670 | ths | * Block driver for RAW files (win32)
|
3 | 223d4670 | ths | *
|
4 | 223d4670 | ths | * Copyright (c) 2006 Fabrice Bellard
|
5 | 223d4670 | ths | *
|
6 | 223d4670 | ths | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 223d4670 | ths | * of this software and associated documentation files (the "Software"), to deal
|
8 | 223d4670 | ths | * in the Software without restriction, including without limitation the rights
|
9 | 223d4670 | ths | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 223d4670 | ths | * copies of the Software, and to permit persons to whom the Software is
|
11 | 223d4670 | ths | * furnished to do so, subject to the following conditions:
|
12 | 223d4670 | ths | *
|
13 | 223d4670 | ths | * The above copyright notice and this permission notice shall be included in
|
14 | 223d4670 | ths | * all copies or substantial portions of the Software.
|
15 | 223d4670 | ths | *
|
16 | 223d4670 | ths | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 223d4670 | ths | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 223d4670 | ths | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 223d4670 | ths | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 223d4670 | ths | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 223d4670 | ths | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 223d4670 | ths | * THE SOFTWARE.
|
23 | 223d4670 | ths | */
|
24 | 223d4670 | ths | #include "qemu-common.h" |
25 | 223d4670 | ths | #ifndef QEMU_IMG
|
26 | 223d4670 | ths | #include "qemu-timer.h" |
27 | 223d4670 | ths | #include "exec-all.h" |
28 | 223d4670 | ths | #endif
|
29 | 223d4670 | ths | #include "block_int.h" |
30 | 223d4670 | ths | #include <assert.h> |
31 | 223d4670 | ths | #include <winioctl.h> |
32 | 223d4670 | ths | |
33 | 223d4670 | ths | #define FTYPE_FILE 0 |
34 | 223d4670 | ths | #define FTYPE_CD 1 |
35 | 223d4670 | ths | #define FTYPE_HARDDISK 2 |
36 | 223d4670 | ths | |
37 | 223d4670 | ths | typedef struct BDRVRawState { |
38 | 223d4670 | ths | HANDLE hfile; |
39 | 223d4670 | ths | int type;
|
40 | 223d4670 | ths | char drive_path[16]; /* format: "d:\" */ |
41 | 223d4670 | ths | } BDRVRawState; |
42 | 223d4670 | ths | |
43 | 223d4670 | ths | typedef struct RawAIOCB { |
44 | 223d4670 | ths | BlockDriverAIOCB common; |
45 | 223d4670 | ths | HANDLE hEvent; |
46 | 223d4670 | ths | OVERLAPPED ov; |
47 | 223d4670 | ths | int count;
|
48 | 223d4670 | ths | } RawAIOCB; |
49 | 223d4670 | ths | |
50 | 223d4670 | ths | int qemu_ftruncate64(int fd, int64_t length) |
51 | 223d4670 | ths | { |
52 | 223d4670 | ths | LARGE_INTEGER li; |
53 | 223d4670 | ths | LONG high; |
54 | 223d4670 | ths | HANDLE h; |
55 | 223d4670 | ths | BOOL res; |
56 | 223d4670 | ths | |
57 | 223d4670 | ths | if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0) |
58 | 223d4670 | ths | return -1; |
59 | 223d4670 | ths | |
60 | 223d4670 | ths | h = (HANDLE)_get_osfhandle(fd); |
61 | 223d4670 | ths | |
62 | 223d4670 | ths | /* get current position, ftruncate do not change position */
|
63 | 223d4670 | ths | li.HighPart = 0;
|
64 | 223d4670 | ths | li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
|
65 | 223d4670 | ths | if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) |
66 | 223d4670 | ths | return -1; |
67 | 223d4670 | ths | |
68 | 223d4670 | ths | high = length >> 32;
|
69 | 223d4670 | ths | if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
|
70 | 223d4670 | ths | return -1; |
71 | 223d4670 | ths | res = SetEndOfFile(h); |
72 | 223d4670 | ths | |
73 | 223d4670 | ths | /* back to old position */
|
74 | 223d4670 | ths | SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN); |
75 | 223d4670 | ths | return res ? 0 : -1; |
76 | 223d4670 | ths | } |
77 | 223d4670 | ths | |
78 | 223d4670 | ths | static int set_sparse(int fd) |
79 | 223d4670 | ths | { |
80 | 223d4670 | ths | DWORD returned; |
81 | 223d4670 | ths | return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE, |
82 | 223d4670 | ths | NULL, 0, NULL, 0, &returned, NULL); |
83 | 223d4670 | ths | } |
84 | 223d4670 | ths | |
85 | 223d4670 | ths | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
86 | 223d4670 | ths | { |
87 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
88 | 223d4670 | ths | int access_flags, create_flags;
|
89 | 223d4670 | ths | DWORD overlapped; |
90 | 223d4670 | ths | |
91 | 223d4670 | ths | s->type = FTYPE_FILE; |
92 | 223d4670 | ths | |
93 | 223d4670 | ths | if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
94 | 223d4670 | ths | access_flags = GENERIC_READ | GENERIC_WRITE; |
95 | 223d4670 | ths | } else {
|
96 | 223d4670 | ths | access_flags = GENERIC_READ; |
97 | 223d4670 | ths | } |
98 | 223d4670 | ths | if (flags & BDRV_O_CREAT) {
|
99 | 223d4670 | ths | create_flags = CREATE_ALWAYS; |
100 | 223d4670 | ths | } else {
|
101 | 223d4670 | ths | create_flags = OPEN_EXISTING; |
102 | 223d4670 | ths | } |
103 | 223d4670 | ths | #ifdef QEMU_IMG
|
104 | 223d4670 | ths | overlapped = FILE_ATTRIBUTE_NORMAL; |
105 | 223d4670 | ths | #else
|
106 | 223d4670 | ths | overlapped = FILE_FLAG_OVERLAPPED; |
107 | 223d4670 | ths | #endif
|
108 | 33f00271 | balrog | if (flags & BDRV_O_DIRECT)
|
109 | 33f00271 | balrog | overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH; |
110 | 223d4670 | ths | s->hfile = CreateFile(filename, access_flags, |
111 | 223d4670 | ths | FILE_SHARE_READ, NULL,
|
112 | 223d4670 | ths | create_flags, overlapped, NULL);
|
113 | 223d4670 | ths | if (s->hfile == INVALID_HANDLE_VALUE) {
|
114 | 223d4670 | ths | int err = GetLastError();
|
115 | 223d4670 | ths | |
116 | 223d4670 | ths | if (err == ERROR_ACCESS_DENIED)
|
117 | 223d4670 | ths | return -EACCES;
|
118 | 223d4670 | ths | return -1; |
119 | 223d4670 | ths | } |
120 | 223d4670 | ths | return 0; |
121 | 223d4670 | ths | } |
122 | 223d4670 | ths | |
123 | 223d4670 | ths | static int raw_pread(BlockDriverState *bs, int64_t offset, |
124 | 223d4670 | ths | uint8_t *buf, int count)
|
125 | 223d4670 | ths | { |
126 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
127 | 223d4670 | ths | OVERLAPPED ov; |
128 | 223d4670 | ths | DWORD ret_count; |
129 | 223d4670 | ths | int ret;
|
130 | 223d4670 | ths | |
131 | 223d4670 | ths | memset(&ov, 0, sizeof(ov)); |
132 | 223d4670 | ths | ov.Offset = offset; |
133 | 223d4670 | ths | ov.OffsetHigh = offset >> 32;
|
134 | 223d4670 | ths | ret = ReadFile(s->hfile, buf, count, &ret_count, &ov); |
135 | 223d4670 | ths | if (!ret) {
|
136 | 223d4670 | ths | ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE); |
137 | 223d4670 | ths | if (!ret)
|
138 | 223d4670 | ths | return -EIO;
|
139 | 223d4670 | ths | else
|
140 | 223d4670 | ths | return ret_count;
|
141 | 223d4670 | ths | } |
142 | 223d4670 | ths | return ret_count;
|
143 | 223d4670 | ths | } |
144 | 223d4670 | ths | |
145 | 223d4670 | ths | static int raw_pwrite(BlockDriverState *bs, int64_t offset, |
146 | 223d4670 | ths | const uint8_t *buf, int count) |
147 | 223d4670 | ths | { |
148 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
149 | 223d4670 | ths | OVERLAPPED ov; |
150 | 223d4670 | ths | DWORD ret_count; |
151 | 223d4670 | ths | int ret;
|
152 | 223d4670 | ths | |
153 | 223d4670 | ths | memset(&ov, 0, sizeof(ov)); |
154 | 223d4670 | ths | ov.Offset = offset; |
155 | 223d4670 | ths | ov.OffsetHigh = offset >> 32;
|
156 | 223d4670 | ths | ret = WriteFile(s->hfile, buf, count, &ret_count, &ov); |
157 | 223d4670 | ths | if (!ret) {
|
158 | 223d4670 | ths | ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE); |
159 | 223d4670 | ths | if (!ret)
|
160 | 223d4670 | ths | return -EIO;
|
161 | 223d4670 | ths | else
|
162 | 223d4670 | ths | return ret_count;
|
163 | 223d4670 | ths | } |
164 | 223d4670 | ths | return ret_count;
|
165 | 223d4670 | ths | } |
166 | 223d4670 | ths | |
167 | 223d4670 | ths | #if 0
|
168 | 223d4670 | ths | #ifndef QEMU_IMG
|
169 | 223d4670 | ths | static void raw_aio_cb(void *opaque)
|
170 | 223d4670 | ths | {
|
171 | 223d4670 | ths | RawAIOCB *acb = opaque;
|
172 | 223d4670 | ths | BlockDriverState *bs = acb->common.bs;
|
173 | 223d4670 | ths | BDRVRawState *s = bs->opaque;
|
174 | 223d4670 | ths | DWORD ret_count;
|
175 | 223d4670 | ths | int ret;
|
176 | 223d4670 | ths | |
177 | 223d4670 | ths | ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
|
178 | 223d4670 | ths | if (!ret || ret_count != acb->count) {
|
179 | 223d4670 | ths | acb->common.cb(acb->common.opaque, -EIO);
|
180 | 223d4670 | ths | } else {
|
181 | 223d4670 | ths | acb->common.cb(acb->common.opaque, 0);
|
182 | 223d4670 | ths | }
|
183 | 223d4670 | ths | }
|
184 | 223d4670 | ths | #endif
|
185 | 223d4670 | ths | |
186 | 223d4670 | ths | static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
|
187 | 223d4670 | ths | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
188 | 223d4670 | ths | BlockDriverCompletionFunc *cb, void *opaque)
|
189 | 223d4670 | ths | { |
190 | 223d4670 | ths | RawAIOCB *acb; |
191 | 223d4670 | ths | int64_t offset; |
192 | 223d4670 | ths | |
193 | 223d4670 | ths | acb = qemu_aio_get(bs, cb, opaque); |
194 | 223d4670 | ths | if (acb->hEvent) {
|
195 | 223d4670 | ths | acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
196 | 223d4670 | ths | if (!acb->hEvent) {
|
197 | 223d4670 | ths | qemu_aio_release(acb); |
198 | 223d4670 | ths | return NULL; |
199 | 223d4670 | ths | } |
200 | 223d4670 | ths | } |
201 | 223d4670 | ths | memset(&acb->ov, 0, sizeof(acb->ov)); |
202 | 223d4670 | ths | offset = sector_num * 512;
|
203 | 223d4670 | ths | acb->ov.Offset = offset; |
204 | 223d4670 | ths | acb->ov.OffsetHigh = offset >> 32;
|
205 | 223d4670 | ths | acb->ov.hEvent = acb->hEvent; |
206 | 223d4670 | ths | acb->count = nb_sectors * 512;
|
207 | 223d4670 | ths | #ifndef QEMU_IMG
|
208 | 223d4670 | ths | qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb); |
209 | 223d4670 | ths | #endif
|
210 | 223d4670 | ths | return acb;
|
211 | 223d4670 | ths | } |
212 | 223d4670 | ths | |
213 | 223d4670 | ths | static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
|
214 | 223d4670 | ths | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
215 | 223d4670 | ths | BlockDriverCompletionFunc *cb, void *opaque)
|
216 | 223d4670 | ths | { |
217 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
218 | 223d4670 | ths | RawAIOCB *acb; |
219 | 223d4670 | ths | int ret;
|
220 | 223d4670 | ths | |
221 | 223d4670 | ths | acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); |
222 | 223d4670 | ths | if (!acb)
|
223 | 223d4670 | ths | return NULL; |
224 | 223d4670 | ths | ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
|
225 | 223d4670 | ths | if (!ret) {
|
226 | 223d4670 | ths | qemu_aio_release(acb); |
227 | 223d4670 | ths | return NULL; |
228 | 223d4670 | ths | } |
229 | 223d4670 | ths | #ifdef QEMU_IMG
|
230 | 223d4670 | ths | qemu_aio_release(acb); |
231 | 223d4670 | ths | #endif
|
232 | 223d4670 | ths | return (BlockDriverAIOCB *)acb;
|
233 | 223d4670 | ths | } |
234 | 223d4670 | ths | |
235 | 223d4670 | ths | static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
|
236 | 223d4670 | ths | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
237 | 223d4670 | ths | BlockDriverCompletionFunc *cb, void *opaque)
|
238 | 223d4670 | ths | { |
239 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
240 | 223d4670 | ths | RawAIOCB *acb; |
241 | 223d4670 | ths | int ret;
|
242 | 223d4670 | ths | |
243 | 223d4670 | ths | acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); |
244 | 223d4670 | ths | if (!acb)
|
245 | 223d4670 | ths | return NULL; |
246 | 223d4670 | ths | ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
|
247 | 223d4670 | ths | if (!ret) {
|
248 | 223d4670 | ths | qemu_aio_release(acb); |
249 | 223d4670 | ths | return NULL; |
250 | 223d4670 | ths | } |
251 | 223d4670 | ths | #ifdef QEMU_IMG
|
252 | 223d4670 | ths | qemu_aio_release(acb); |
253 | 223d4670 | ths | #endif
|
254 | 223d4670 | ths | return (BlockDriverAIOCB *)acb;
|
255 | 223d4670 | ths | } |
256 | 223d4670 | ths | |
257 | 223d4670 | ths | static void raw_aio_cancel(BlockDriverAIOCB *blockacb) |
258 | 223d4670 | ths | { |
259 | 223d4670 | ths | #ifndef QEMU_IMG
|
260 | 223d4670 | ths | RawAIOCB *acb = (RawAIOCB *)blockacb; |
261 | 223d4670 | ths | BlockDriverState *bs = acb->common.bs; |
262 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
263 | 223d4670 | ths | |
264 | 223d4670 | ths | qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb); |
265 | 223d4670 | ths | /* XXX: if more than one async I/O it is not correct */
|
266 | 223d4670 | ths | CancelIo(s->hfile); |
267 | 223d4670 | ths | qemu_aio_release(acb); |
268 | 223d4670 | ths | #endif
|
269 | 223d4670 | ths | } |
270 | 223d4670 | ths | #endif /* #if 0 */ |
271 | 223d4670 | ths | |
272 | 223d4670 | ths | static void raw_flush(BlockDriverState *bs) |
273 | 223d4670 | ths | { |
274 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
275 | 223d4670 | ths | FlushFileBuffers(s->hfile); |
276 | 223d4670 | ths | } |
277 | 223d4670 | ths | |
278 | 223d4670 | ths | static void raw_close(BlockDriverState *bs) |
279 | 223d4670 | ths | { |
280 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
281 | 223d4670 | ths | CloseHandle(s->hfile); |
282 | 223d4670 | ths | } |
283 | 223d4670 | ths | |
284 | 223d4670 | ths | static int raw_truncate(BlockDriverState *bs, int64_t offset) |
285 | 223d4670 | ths | { |
286 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
287 | 223d4670 | ths | DWORD low, high; |
288 | 223d4670 | ths | |
289 | 223d4670 | ths | low = offset; |
290 | 223d4670 | ths | high = offset >> 32;
|
291 | 223d4670 | ths | if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
|
292 | 223d4670 | ths | return -EIO;
|
293 | 223d4670 | ths | if (!SetEndOfFile(s->hfile))
|
294 | 223d4670 | ths | return -EIO;
|
295 | 223d4670 | ths | return 0; |
296 | 223d4670 | ths | } |
297 | 223d4670 | ths | |
298 | 223d4670 | ths | static int64_t raw_getlength(BlockDriverState *bs)
|
299 | 223d4670 | ths | { |
300 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
301 | 223d4670 | ths | LARGE_INTEGER l; |
302 | 223d4670 | ths | ULARGE_INTEGER available, total, total_free; |
303 | 223d4670 | ths | DISK_GEOMETRY_EX dg; |
304 | 223d4670 | ths | DWORD count; |
305 | 223d4670 | ths | BOOL status; |
306 | 223d4670 | ths | |
307 | 223d4670 | ths | switch(s->type) {
|
308 | 223d4670 | ths | case FTYPE_FILE:
|
309 | 223d4670 | ths | l.LowPart = GetFileSize(s->hfile, &l.HighPart); |
310 | 223d4670 | ths | if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) |
311 | 223d4670 | ths | return -EIO;
|
312 | 223d4670 | ths | break;
|
313 | 223d4670 | ths | case FTYPE_CD:
|
314 | 223d4670 | ths | if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
|
315 | 223d4670 | ths | return -EIO;
|
316 | 223d4670 | ths | l.QuadPart = total.QuadPart; |
317 | 223d4670 | ths | break;
|
318 | 223d4670 | ths | case FTYPE_HARDDISK:
|
319 | 223d4670 | ths | status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, |
320 | 223d4670 | ths | NULL, 0, &dg, sizeof(dg), &count, NULL); |
321 | 223d4670 | ths | if (status != 0) { |
322 | 223d4670 | ths | l = dg.DiskSize; |
323 | 223d4670 | ths | } |
324 | 223d4670 | ths | break;
|
325 | 223d4670 | ths | default:
|
326 | 223d4670 | ths | return -EIO;
|
327 | 223d4670 | ths | } |
328 | 223d4670 | ths | return l.QuadPart;
|
329 | 223d4670 | ths | } |
330 | 223d4670 | ths | |
331 | 223d4670 | ths | static int raw_create(const char *filename, int64_t total_size, |
332 | 223d4670 | ths | const char *backing_file, int flags) |
333 | 223d4670 | ths | { |
334 | 223d4670 | ths | int fd;
|
335 | 223d4670 | ths | |
336 | 223d4670 | ths | if (flags || backing_file)
|
337 | 223d4670 | ths | return -ENOTSUP;
|
338 | 223d4670 | ths | |
339 | 223d4670 | ths | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
340 | 223d4670 | ths | 0644);
|
341 | 223d4670 | ths | if (fd < 0) |
342 | 223d4670 | ths | return -EIO;
|
343 | 223d4670 | ths | set_sparse(fd); |
344 | 223d4670 | ths | ftruncate(fd, total_size * 512);
|
345 | 223d4670 | ths | close(fd); |
346 | 223d4670 | ths | return 0; |
347 | 223d4670 | ths | } |
348 | 223d4670 | ths | |
349 | 223d4670 | ths | void qemu_aio_init(void) |
350 | 223d4670 | ths | { |
351 | 223d4670 | ths | } |
352 | 223d4670 | ths | |
353 | 223d4670 | ths | void qemu_aio_poll(void) |
354 | 223d4670 | ths | { |
355 | 223d4670 | ths | } |
356 | 223d4670 | ths | |
357 | 223d4670 | ths | void qemu_aio_flush(void) |
358 | 223d4670 | ths | { |
359 | 223d4670 | ths | } |
360 | 223d4670 | ths | |
361 | 223d4670 | ths | void qemu_aio_wait_start(void) |
362 | 223d4670 | ths | { |
363 | 223d4670 | ths | } |
364 | 223d4670 | ths | |
365 | 223d4670 | ths | void qemu_aio_wait(void) |
366 | 223d4670 | ths | { |
367 | 223d4670 | ths | #ifndef QEMU_IMG
|
368 | 223d4670 | ths | qemu_bh_poll(); |
369 | 223d4670 | ths | #endif
|
370 | 223d4670 | ths | } |
371 | 223d4670 | ths | |
372 | 223d4670 | ths | void qemu_aio_wait_end(void) |
373 | 223d4670 | ths | { |
374 | 223d4670 | ths | } |
375 | 223d4670 | ths | |
376 | 223d4670 | ths | BlockDriver bdrv_raw = { |
377 | 223d4670 | ths | "raw",
|
378 | 223d4670 | ths | sizeof(BDRVRawState),
|
379 | 223d4670 | ths | NULL, /* no probe for protocols */ |
380 | 223d4670 | ths | raw_open, |
381 | 223d4670 | ths | NULL,
|
382 | 223d4670 | ths | NULL,
|
383 | 223d4670 | ths | raw_close, |
384 | 223d4670 | ths | raw_create, |
385 | 223d4670 | ths | raw_flush, |
386 | 223d4670 | ths | |
387 | 223d4670 | ths | #if 0
|
388 | 223d4670 | ths | .bdrv_aio_read = raw_aio_read,
|
389 | 223d4670 | ths | .bdrv_aio_write = raw_aio_write,
|
390 | 223d4670 | ths | .bdrv_aio_cancel = raw_aio_cancel,
|
391 | 223d4670 | ths | .aiocb_size = sizeof(RawAIOCB);
|
392 | 223d4670 | ths | #endif
|
393 | 223d4670 | ths | .protocol_name = "file",
|
394 | 223d4670 | ths | .bdrv_pread = raw_pread, |
395 | 223d4670 | ths | .bdrv_pwrite = raw_pwrite, |
396 | 223d4670 | ths | .bdrv_truncate = raw_truncate, |
397 | 223d4670 | ths | .bdrv_getlength = raw_getlength, |
398 | 223d4670 | ths | }; |
399 | 223d4670 | ths | |
400 | 223d4670 | ths | /***********************************************/
|
401 | 223d4670 | ths | /* host device */
|
402 | 223d4670 | ths | |
403 | 223d4670 | ths | static int find_cdrom(char *cdrom_name, int cdrom_name_size) |
404 | 223d4670 | ths | { |
405 | 223d4670 | ths | char drives[256], *pdrv = drives; |
406 | 223d4670 | ths | UINT type; |
407 | 223d4670 | ths | |
408 | 223d4670 | ths | memset(drives, 0, sizeof(drives)); |
409 | 223d4670 | ths | GetLogicalDriveStrings(sizeof(drives), drives);
|
410 | 223d4670 | ths | while(pdrv[0] != '\0') { |
411 | 223d4670 | ths | type = GetDriveType(pdrv); |
412 | 223d4670 | ths | switch(type) {
|
413 | 223d4670 | ths | case DRIVE_CDROM:
|
414 | 223d4670 | ths | snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]); |
415 | 223d4670 | ths | return 0; |
416 | 223d4670 | ths | break;
|
417 | 223d4670 | ths | } |
418 | 223d4670 | ths | pdrv += lstrlen(pdrv) + 1;
|
419 | 223d4670 | ths | } |
420 | 223d4670 | ths | return -1; |
421 | 223d4670 | ths | } |
422 | 223d4670 | ths | |
423 | 223d4670 | ths | static int find_device_type(BlockDriverState *bs, const char *filename) |
424 | 223d4670 | ths | { |
425 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
426 | 223d4670 | ths | UINT type; |
427 | 223d4670 | ths | const char *p; |
428 | 223d4670 | ths | |
429 | 223d4670 | ths | if (strstart(filename, "\\\\.\\", &p) || |
430 | 223d4670 | ths | strstart(filename, "//./", &p)) {
|
431 | 223d4670 | ths | if (stristart(p, "PhysicalDrive", NULL)) |
432 | 223d4670 | ths | return FTYPE_HARDDISK;
|
433 | 223d4670 | ths | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]); |
434 | 223d4670 | ths | type = GetDriveType(s->drive_path); |
435 | 223d4670 | ths | if (type == DRIVE_CDROM)
|
436 | 223d4670 | ths | return FTYPE_CD;
|
437 | 223d4670 | ths | else
|
438 | 223d4670 | ths | return FTYPE_FILE;
|
439 | 223d4670 | ths | } else {
|
440 | 223d4670 | ths | return FTYPE_FILE;
|
441 | 223d4670 | ths | } |
442 | 223d4670 | ths | } |
443 | 223d4670 | ths | |
444 | 223d4670 | ths | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
445 | 223d4670 | ths | { |
446 | 223d4670 | ths | BDRVRawState *s = bs->opaque; |
447 | 223d4670 | ths | int access_flags, create_flags;
|
448 | 223d4670 | ths | DWORD overlapped; |
449 | 223d4670 | ths | char device_name[64]; |
450 | 223d4670 | ths | |
451 | 223d4670 | ths | if (strstart(filename, "/dev/cdrom", NULL)) { |
452 | 223d4670 | ths | if (find_cdrom(device_name, sizeof(device_name)) < 0) |
453 | 223d4670 | ths | return -ENOENT;
|
454 | 223d4670 | ths | filename = device_name; |
455 | 223d4670 | ths | } else {
|
456 | 223d4670 | ths | /* transform drive letters into device name */
|
457 | 223d4670 | ths | if (((filename[0] >= 'a' && filename[0] <= 'z') || |
458 | 223d4670 | ths | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
459 | 223d4670 | ths | filename[1] == ':' && filename[2] == '\0') { |
460 | 223d4670 | ths | snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]); |
461 | 223d4670 | ths | filename = device_name; |
462 | 223d4670 | ths | } |
463 | 223d4670 | ths | } |
464 | 223d4670 | ths | s->type = find_device_type(bs, filename); |
465 | 223d4670 | ths | |
466 | 223d4670 | ths | if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
467 | 223d4670 | ths | access_flags = GENERIC_READ | GENERIC_WRITE; |
468 | 223d4670 | ths | } else {
|
469 | 223d4670 | ths | access_flags = GENERIC_READ; |
470 | 223d4670 | ths | } |
471 | 223d4670 | ths | create_flags = OPEN_EXISTING; |
472 | 223d4670 | ths | |
473 | 223d4670 | ths | #ifdef QEMU_IMG
|
474 | 223d4670 | ths | overlapped = FILE_ATTRIBUTE_NORMAL; |
475 | 223d4670 | ths | #else
|
476 | 223d4670 | ths | overlapped = FILE_FLAG_OVERLAPPED; |
477 | 223d4670 | ths | #endif
|
478 | 33f00271 | balrog | if (flags & BDRV_O_DIRECT)
|
479 | 33f00271 | balrog | overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH; |
480 | 223d4670 | ths | s->hfile = CreateFile(filename, access_flags, |
481 | 223d4670 | ths | FILE_SHARE_READ, NULL,
|
482 | 223d4670 | ths | create_flags, overlapped, NULL);
|
483 | 223d4670 | ths | if (s->hfile == INVALID_HANDLE_VALUE) {
|
484 | 223d4670 | ths | int err = GetLastError();
|
485 | 223d4670 | ths | |
486 | 223d4670 | ths | if (err == ERROR_ACCESS_DENIED)
|
487 | 223d4670 | ths | return -EACCES;
|
488 | 223d4670 | ths | return -1; |
489 | 223d4670 | ths | } |
490 | 223d4670 | ths | return 0; |
491 | 223d4670 | ths | } |
492 | 223d4670 | ths | |
493 | 223d4670 | ths | #if 0
|
494 | 223d4670 | ths | /***********************************************/
|
495 | 223d4670 | ths | /* removable device additional commands */
|
496 | 223d4670 | ths | |
497 | 223d4670 | ths | static int raw_is_inserted(BlockDriverState *bs)
|
498 | 223d4670 | ths | {
|
499 | 223d4670 | ths | return 1;
|
500 | 223d4670 | ths | }
|
501 | 223d4670 | ths | |
502 | 223d4670 | ths | static int raw_media_changed(BlockDriverState *bs)
|
503 | 223d4670 | ths | {
|
504 | 223d4670 | ths | return -ENOTSUP;
|
505 | 223d4670 | ths | }
|
506 | 223d4670 | ths | |
507 | 223d4670 | ths | static int raw_eject(BlockDriverState *bs, int eject_flag)
|
508 | 223d4670 | ths | {
|
509 | 223d4670 | ths | DWORD ret_count;
|
510 | 223d4670 | ths | |
511 | 223d4670 | ths | if (s->type == FTYPE_FILE)
|
512 | 223d4670 | ths | return -ENOTSUP;
|
513 | 223d4670 | ths | if (eject_flag) {
|
514 | 223d4670 | ths | DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
|
515 | 223d4670 | ths | NULL, 0, NULL, 0, &lpBytesReturned, NULL);
|
516 | 223d4670 | ths | } else {
|
517 | 223d4670 | ths | DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
|
518 | 223d4670 | ths | NULL, 0, NULL, 0, &lpBytesReturned, NULL);
|
519 | 223d4670 | ths | }
|
520 | 223d4670 | ths | }
|
521 | 223d4670 | ths | |
522 | 223d4670 | ths | static int raw_set_locked(BlockDriverState *bs, int locked)
|
523 | 223d4670 | ths | {
|
524 | 223d4670 | ths | return -ENOTSUP;
|
525 | 223d4670 | ths | }
|
526 | 223d4670 | ths | #endif
|
527 | 223d4670 | ths | |
528 | 223d4670 | ths | BlockDriver bdrv_host_device = { |
529 | 223d4670 | ths | "host_device",
|
530 | 223d4670 | ths | sizeof(BDRVRawState),
|
531 | 223d4670 | ths | NULL, /* no probe for protocols */ |
532 | 223d4670 | ths | hdev_open, |
533 | 223d4670 | ths | NULL,
|
534 | 223d4670 | ths | NULL,
|
535 | 223d4670 | ths | raw_close, |
536 | 223d4670 | ths | NULL,
|
537 | 223d4670 | ths | raw_flush, |
538 | 223d4670 | ths | |
539 | 223d4670 | ths | #if 0
|
540 | 223d4670 | ths | .bdrv_aio_read = raw_aio_read,
|
541 | 223d4670 | ths | .bdrv_aio_write = raw_aio_write,
|
542 | 223d4670 | ths | .bdrv_aio_cancel = raw_aio_cancel,
|
543 | 223d4670 | ths | .aiocb_size = sizeof(RawAIOCB);
|
544 | 223d4670 | ths | #endif
|
545 | 223d4670 | ths | .bdrv_pread = raw_pread, |
546 | 223d4670 | ths | .bdrv_pwrite = raw_pwrite, |
547 | 223d4670 | ths | .bdrv_getlength = raw_getlength, |
548 | 223d4670 | ths | }; |