Revert r6404
[qemu/mini2440/sniper_sniper_test.git] / block-raw-win32.c
blob6387b09593f836fb70780a2c556f6d19b5726be0
1 /*
2 * Block driver for RAW files (win32)
4 * Copyright (c) 2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "qemu-timer.h"
26 #include "block_int.h"
27 #include <assert.h>
28 #include <windows.h>
29 #include <winioctl.h>
31 //#define WIN32_AIO
33 #define FTYPE_FILE 0
34 #define FTYPE_CD 1
35 #define FTYPE_HARDDISK 2
37 typedef struct BDRVRawState {
38 HANDLE hfile;
39 int type;
40 char drive_path[16]; /* format: "d:\" */
41 } BDRVRawState;
43 typedef struct RawAIOCB {
44 BlockDriverAIOCB common;
45 HANDLE hEvent;
46 OVERLAPPED ov;
47 int count;
48 } RawAIOCB;
50 int qemu_ftruncate64(int fd, int64_t length)
52 LARGE_INTEGER li;
53 LONG high;
54 HANDLE h;
55 BOOL res;
57 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
58 return -1;
60 h = (HANDLE)_get_osfhandle(fd);
62 /* get current position, ftruncate do not change position */
63 li.HighPart = 0;
64 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
65 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
66 return -1;
68 high = length >> 32;
69 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
70 return -1;
71 res = SetEndOfFile(h);
73 /* back to old position */
74 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
75 return res ? 0 : -1;
78 static int set_sparse(int fd)
80 DWORD returned;
81 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
82 NULL, 0, NULL, 0, &returned, NULL);
85 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
87 BDRVRawState *s = bs->opaque;
88 int access_flags, create_flags;
89 DWORD overlapped;
91 s->type = FTYPE_FILE;
93 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
94 access_flags = GENERIC_READ | GENERIC_WRITE;
95 } else {
96 access_flags = GENERIC_READ;
98 if (flags & BDRV_O_CREAT) {
99 create_flags = CREATE_ALWAYS;
100 } else {
101 create_flags = OPEN_EXISTING;
103 #ifdef WIN32_AIO
104 overlapped = FILE_FLAG_OVERLAPPED;
105 #else
106 overlapped = FILE_ATTRIBUTE_NORMAL;
107 #endif
108 if ((flags & BDRV_O_NOCACHE))
109 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
110 else if (!(flags & BDRV_O_CACHE_WB))
111 overlapped |= FILE_FLAG_WRITE_THROUGH;
112 s->hfile = CreateFile(filename, access_flags,
113 FILE_SHARE_READ, NULL,
114 create_flags, overlapped, NULL);
115 if (s->hfile == INVALID_HANDLE_VALUE) {
116 int err = GetLastError();
118 if (err == ERROR_ACCESS_DENIED)
119 return -EACCES;
120 return -1;
122 return 0;
125 static int raw_pread(BlockDriverState *bs, int64_t offset,
126 uint8_t *buf, int count)
128 BDRVRawState *s = bs->opaque;
129 OVERLAPPED ov;
130 DWORD ret_count;
131 int ret;
133 memset(&ov, 0, sizeof(ov));
134 ov.Offset = offset;
135 ov.OffsetHigh = offset >> 32;
136 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
137 if (!ret) {
138 #ifdef WIN32_AIO
139 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
140 if (!ret)
141 return -EIO;
142 else
143 #endif
144 return ret_count;
146 return ret_count;
149 static int raw_pwrite(BlockDriverState *bs, int64_t offset,
150 const uint8_t *buf, int count)
152 BDRVRawState *s = bs->opaque;
153 OVERLAPPED ov;
154 DWORD ret_count;
155 int ret;
157 memset(&ov, 0, sizeof(ov));
158 ov.Offset = offset;
159 ov.OffsetHigh = offset >> 32;
160 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
161 if (!ret) {
162 #ifdef WIN32_AIO
163 ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
164 if (!ret)
165 return -EIO;
166 else
167 #endif
168 return ret_count;
170 return ret_count;
173 #ifdef WIN32_AIO
174 static void raw_aio_cb(void *opaque)
176 RawAIOCB *acb = opaque;
177 BlockDriverState *bs = acb->common.bs;
178 BDRVRawState *s = bs->opaque;
179 DWORD ret_count;
180 int ret;
182 ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
183 if (!ret || ret_count != acb->count) {
184 acb->common.cb(acb->common.opaque, -EIO);
185 } else {
186 acb->common.cb(acb->common.opaque, 0);
190 static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
191 int64_t sector_num, uint8_t *buf, int nb_sectors,
192 BlockDriverCompletionFunc *cb, void *opaque)
194 RawAIOCB *acb;
195 int64_t offset;
197 acb = qemu_aio_get(bs, cb, opaque);
198 if (acb->hEvent) {
199 acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
200 if (!acb->hEvent) {
201 qemu_aio_release(acb);
202 return NULL;
205 memset(&acb->ov, 0, sizeof(acb->ov));
206 offset = sector_num * 512;
207 acb->ov.Offset = offset;
208 acb->ov.OffsetHigh = offset >> 32;
209 acb->ov.hEvent = acb->hEvent;
210 acb->count = nb_sectors * 512;
211 qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
212 return acb;
215 static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
216 int64_t sector_num, uint8_t *buf, int nb_sectors,
217 BlockDriverCompletionFunc *cb, void *opaque)
219 BDRVRawState *s = bs->opaque;
220 RawAIOCB *acb;
221 int ret;
223 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
224 if (!acb)
225 return NULL;
226 ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
227 if (!ret) {
228 qemu_aio_release(acb);
229 return NULL;
231 qemu_aio_release(acb);
232 return (BlockDriverAIOCB *)acb;
235 static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
236 int64_t sector_num, uint8_t *buf, int nb_sectors,
237 BlockDriverCompletionFunc *cb, void *opaque)
239 BDRVRawState *s = bs->opaque;
240 RawAIOCB *acb;
241 int ret;
243 acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
244 if (!acb)
245 return NULL;
246 ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
247 if (!ret) {
248 qemu_aio_release(acb);
249 return NULL;
251 qemu_aio_release(acb);
252 return (BlockDriverAIOCB *)acb;
255 static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
257 RawAIOCB *acb = (RawAIOCB *)blockacb;
258 BlockDriverState *bs = acb->common.bs;
259 BDRVRawState *s = bs->opaque;
261 qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
262 /* XXX: if more than one async I/O it is not correct */
263 CancelIo(s->hfile);
264 qemu_aio_release(acb);
266 #endif /* #if WIN32_AIO */
268 static void raw_flush(BlockDriverState *bs)
270 BDRVRawState *s = bs->opaque;
271 FlushFileBuffers(s->hfile);
274 static void raw_close(BlockDriverState *bs)
276 BDRVRawState *s = bs->opaque;
277 CloseHandle(s->hfile);
280 static int raw_truncate(BlockDriverState *bs, int64_t offset)
282 BDRVRawState *s = bs->opaque;
283 DWORD low, high;
285 low = offset;
286 high = offset >> 32;
287 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
288 return -EIO;
289 if (!SetEndOfFile(s->hfile))
290 return -EIO;
291 return 0;
294 static int64_t raw_getlength(BlockDriverState *bs)
296 BDRVRawState *s = bs->opaque;
297 LARGE_INTEGER l;
298 ULARGE_INTEGER available, total, total_free;
299 DISK_GEOMETRY_EX dg;
300 DWORD count;
301 BOOL status;
303 switch(s->type) {
304 case FTYPE_FILE:
305 l.LowPart = GetFileSize(s->hfile, &l.HighPart);
306 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
307 return -EIO;
308 break;
309 case FTYPE_CD:
310 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
311 return -EIO;
312 l.QuadPart = total.QuadPart;
313 break;
314 case FTYPE_HARDDISK:
315 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
316 NULL, 0, &dg, sizeof(dg), &count, NULL);
317 if (status != 0) {
318 l = dg.DiskSize;
320 break;
321 default:
322 return -EIO;
324 return l.QuadPart;
327 static int raw_create(const char *filename, int64_t total_size,
328 const char *backing_file, int flags)
330 int fd;
332 if (flags || backing_file)
333 return -ENOTSUP;
335 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
336 0644);
337 if (fd < 0)
338 return -EIO;
339 set_sparse(fd);
340 ftruncate(fd, total_size * 512);
341 close(fd);
342 return 0;
345 BlockDriver bdrv_raw = {
346 "raw",
347 sizeof(BDRVRawState),
348 NULL, /* no probe for protocols */
349 raw_open,
350 NULL,
351 NULL,
352 raw_close,
353 raw_create,
354 raw_flush,
356 #ifdef WIN32_AIO
357 .bdrv_aio_read = raw_aio_read,
358 .bdrv_aio_write = raw_aio_write,
359 .bdrv_aio_cancel = raw_aio_cancel,
360 .aiocb_size = sizeof(RawAIOCB);
361 #endif
362 .bdrv_pread = raw_pread,
363 .bdrv_pwrite = raw_pwrite,
364 .bdrv_truncate = raw_truncate,
365 .bdrv_getlength = raw_getlength,
368 /***********************************************/
369 /* host device */
371 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
373 char drives[256], *pdrv = drives;
374 UINT type;
376 memset(drives, 0, sizeof(drives));
377 GetLogicalDriveStrings(sizeof(drives), drives);
378 while(pdrv[0] != '\0') {
379 type = GetDriveType(pdrv);
380 switch(type) {
381 case DRIVE_CDROM:
382 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
383 return 0;
384 break;
386 pdrv += lstrlen(pdrv) + 1;
388 return -1;
391 static int find_device_type(BlockDriverState *bs, const char *filename)
393 BDRVRawState *s = bs->opaque;
394 UINT type;
395 const char *p;
397 if (strstart(filename, "\\\\.\\", &p) ||
398 strstart(filename, "//./", &p)) {
399 if (stristart(p, "PhysicalDrive", NULL))
400 return FTYPE_HARDDISK;
401 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
402 type = GetDriveType(s->drive_path);
403 if (type == DRIVE_CDROM)
404 return FTYPE_CD;
405 else
406 return FTYPE_FILE;
407 } else {
408 return FTYPE_FILE;
412 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
414 BDRVRawState *s = bs->opaque;
415 int access_flags, create_flags;
416 DWORD overlapped;
417 char device_name[64];
419 if (strstart(filename, "/dev/cdrom", NULL)) {
420 if (find_cdrom(device_name, sizeof(device_name)) < 0)
421 return -ENOENT;
422 filename = device_name;
423 } else {
424 /* transform drive letters into device name */
425 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
426 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
427 filename[1] == ':' && filename[2] == '\0') {
428 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
429 filename = device_name;
432 s->type = find_device_type(bs, filename);
434 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
435 access_flags = GENERIC_READ | GENERIC_WRITE;
436 } else {
437 access_flags = GENERIC_READ;
439 create_flags = OPEN_EXISTING;
441 #ifdef WIN32_AIO
442 overlapped = FILE_FLAG_OVERLAPPED;
443 #else
444 overlapped = FILE_ATTRIBUTE_NORMAL;
445 #endif
446 if ((flags & BDRV_O_NOCACHE))
447 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
448 else if (!(flags & BDRV_O_CACHE_WB))
449 overlapped |= FILE_FLAG_WRITE_THROUGH;
450 s->hfile = CreateFile(filename, access_flags,
451 FILE_SHARE_READ, NULL,
452 create_flags, overlapped, NULL);
453 if (s->hfile == INVALID_HANDLE_VALUE) {
454 int err = GetLastError();
456 if (err == ERROR_ACCESS_DENIED)
457 return -EACCES;
458 return -1;
460 return 0;
463 #if 0
464 /***********************************************/
465 /* removable device additional commands */
467 static int raw_is_inserted(BlockDriverState *bs)
469 return 1;
472 static int raw_media_changed(BlockDriverState *bs)
474 return -ENOTSUP;
477 static int raw_eject(BlockDriverState *bs, int eject_flag)
479 DWORD ret_count;
481 if (s->type == FTYPE_FILE)
482 return -ENOTSUP;
483 if (eject_flag) {
484 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
485 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
486 } else {
487 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
488 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
492 static int raw_set_locked(BlockDriverState *bs, int locked)
494 return -ENOTSUP;
496 #endif
498 BlockDriver bdrv_host_device = {
499 .format_name = "host_device",
500 .instance_size = sizeof(BDRVRawState),
501 .bdrv_open = hdev_open,
502 .bdrv_close = raw_close,
503 .bdrv_flush = raw_flush,
505 #ifdef WIN32_AIO
506 .bdrv_aio_read = raw_aio_read,
507 .bdrv_aio_write = raw_aio_write,
508 .bdrv_aio_cancel = raw_aio_cancel,
509 .aiocb_size = sizeof(RawAIOCB);
510 #endif
511 .bdrv_pread = raw_pread,
512 .bdrv_pwrite = raw_pwrite,
513 .bdrv_getlength = raw_getlength,