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
24 #include "qemu-common.h"
25 #include "qemu-timer.h"
26 #include "block_int.h"
33 #define FTYPE_HARDDISK 2
35 typedef struct BDRVRawState {
38 char drive_path[16]; /* format: "d:\" */
41 int qemu_ftruncate64(int fd, int64_t length)
48 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
51 h = (HANDLE)_get_osfhandle(fd);
53 /* get current position, ftruncate do not change position */
55 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
56 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
60 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
62 res = SetEndOfFile(h);
64 /* back to old position */
65 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
69 static int set_sparse(int fd)
72 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
73 NULL, 0, NULL, 0, &returned, NULL);
76 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
78 BDRVRawState *s = bs->opaque;
79 int access_flags, create_flags;
84 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
85 access_flags = GENERIC_READ | GENERIC_WRITE;
87 access_flags = GENERIC_READ;
89 if (flags & BDRV_O_CREAT) {
90 create_flags = CREATE_ALWAYS;
92 create_flags = OPEN_EXISTING;
94 overlapped = FILE_ATTRIBUTE_NORMAL;
95 if ((flags & BDRV_O_NOCACHE))
96 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
97 else if (!(flags & BDRV_O_CACHE_WB))
98 overlapped |= FILE_FLAG_WRITE_THROUGH;
99 s->hfile = CreateFile(filename, access_flags,
100 FILE_SHARE_READ, NULL,
101 create_flags, overlapped, NULL);
102 if (s->hfile == INVALID_HANDLE_VALUE) {
103 int err = GetLastError();
105 if (err == ERROR_ACCESS_DENIED)
112 static int raw_read(BlockDriverState *bs, int64_t sector_num,
113 uint8_t *buf, int nb_sectors)
115 BDRVRawState *s = bs->opaque;
119 int64_t offset = sector_num * 512;
120 int count = nb_sectors * 512;
122 memset(&ov, 0, sizeof(ov));
124 ov.OffsetHigh = offset >> 32;
125 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
128 if (ret_count == count)
133 static int raw_write(BlockDriverState *bs, int64_t sector_num,
134 const uint8_t *buf, int nb_sectors)
136 BDRVRawState *s = bs->opaque;
140 int64_t offset = sector_num * 512;
141 int count = nb_sectors * 512;
143 memset(&ov, 0, sizeof(ov));
145 ov.OffsetHigh = offset >> 32;
146 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
149 if (ret_count == count)
154 static void raw_flush(BlockDriverState *bs)
156 BDRVRawState *s = bs->opaque;
157 FlushFileBuffers(s->hfile);
160 static void raw_close(BlockDriverState *bs)
162 BDRVRawState *s = bs->opaque;
163 CloseHandle(s->hfile);
166 static int raw_truncate(BlockDriverState *bs, int64_t offset)
168 BDRVRawState *s = bs->opaque;
173 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
175 if (!SetEndOfFile(s->hfile))
180 static int64_t raw_getlength(BlockDriverState *bs)
182 BDRVRawState *s = bs->opaque;
184 ULARGE_INTEGER available, total, total_free;
191 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
192 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
196 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
198 l.QuadPart = total.QuadPart;
201 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
202 NULL, 0, &dg, sizeof(dg), &count, NULL);
213 static int raw_create(const char *filename, int64_t total_size,
214 const char *backing_file, int flags)
218 if (flags || backing_file)
221 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
226 ftruncate(fd, total_size * 512);
231 static BlockDriver bdrv_raw = {
232 .format_name = "raw",
233 .instance_size = sizeof(BDRVRawState),
234 .bdrv_open = raw_open,
235 .bdrv_close = raw_close,
236 .bdrv_create = raw_create,
237 .bdrv_flush = raw_flush,
238 .bdrv_read = raw_read,
239 .bdrv_write = raw_write,
240 .bdrv_truncate = raw_truncate,
241 .bdrv_getlength = raw_getlength,
244 /***********************************************/
247 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
249 char drives[256], *pdrv = drives;
252 memset(drives, 0, sizeof(drives));
253 GetLogicalDriveStrings(sizeof(drives), drives);
254 while(pdrv[0] != '\0') {
255 type = GetDriveType(pdrv);
258 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
262 pdrv += lstrlen(pdrv) + 1;
267 static int find_device_type(BlockDriverState *bs, const char *filename)
269 BDRVRawState *s = bs->opaque;
273 if (strstart(filename, "\\\\.\\", &p) ||
274 strstart(filename, "//./", &p)) {
275 if (stristart(p, "PhysicalDrive", NULL))
276 return FTYPE_HARDDISK;
277 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
278 type = GetDriveType(s->drive_path);
280 case DRIVE_REMOVABLE:
282 return FTYPE_HARDDISK;
293 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
295 BDRVRawState *s = bs->opaque;
296 int access_flags, create_flags;
298 char device_name[64];
300 if (strstart(filename, "/dev/cdrom", NULL)) {
301 if (find_cdrom(device_name, sizeof(device_name)) < 0)
303 filename = device_name;
305 /* transform drive letters into device name */
306 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
307 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
308 filename[1] == ':' && filename[2] == '\0') {
309 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
310 filename = device_name;
313 s->type = find_device_type(bs, filename);
315 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
316 access_flags = GENERIC_READ | GENERIC_WRITE;
318 access_flags = GENERIC_READ;
320 create_flags = OPEN_EXISTING;
322 overlapped = FILE_ATTRIBUTE_NORMAL;
323 if ((flags & BDRV_O_NOCACHE))
324 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
325 else if (!(flags & BDRV_O_CACHE_WB))
326 overlapped |= FILE_FLAG_WRITE_THROUGH;
327 s->hfile = CreateFile(filename, access_flags,
328 FILE_SHARE_READ, NULL,
329 create_flags, overlapped, NULL);
330 if (s->hfile == INVALID_HANDLE_VALUE) {
331 int err = GetLastError();
333 if (err == ERROR_ACCESS_DENIED)
341 /***********************************************/
342 /* removable device additional commands */
344 static int raw_is_inserted(BlockDriverState *bs)
349 static int raw_media_changed(BlockDriverState *bs)
354 static int raw_eject(BlockDriverState *bs, int eject_flag)
358 if (s->type == FTYPE_FILE)
361 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
362 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
364 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
365 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
369 static int raw_set_locked(BlockDriverState *bs, int locked)
375 static BlockDriver bdrv_host_device = {
376 .format_name = "host_device",
377 .instance_size = sizeof(BDRVRawState),
378 .bdrv_open = hdev_open,
379 .bdrv_close = raw_close,
380 .bdrv_flush = raw_flush,
382 .bdrv_read = raw_read,
383 .bdrv_write = raw_write,
384 .bdrv_getlength = raw_getlength,
387 static void bdrv_raw_init(void)
389 bdrv_register(&bdrv_raw);
390 bdrv_register(&bdrv_host_device);
393 block_init(bdrv_raw_init);