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"
32 #define FTYPE_HARDDISK 2
34 typedef struct BDRVRawState
{
37 char drive_path
[16]; /* format: "d:\" */
40 int qemu_ftruncate64(int fd
, int64_t length
)
47 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
50 h
= (HANDLE
)_get_osfhandle(fd
);
52 /* get current position, ftruncate do not change position */
54 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
55 if (li
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
59 if (!SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
))
61 res
= SetEndOfFile(h
);
63 /* back to old position */
64 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
68 static int set_sparse(int fd
)
71 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
72 NULL
, 0, NULL
, 0, &returned
, NULL
);
75 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
77 BDRVRawState
*s
= bs
->opaque
;
78 int access_flags
, create_flags
;
83 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
84 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
86 access_flags
= GENERIC_READ
;
88 if (flags
& BDRV_O_CREAT
) {
89 create_flags
= CREATE_ALWAYS
;
91 create_flags
= OPEN_EXISTING
;
93 overlapped
= FILE_ATTRIBUTE_NORMAL
;
94 if ((flags
& BDRV_O_NOCACHE
))
95 overlapped
|= FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
;
96 else if (!(flags
& BDRV_O_CACHE_WB
))
97 overlapped
|= FILE_FLAG_WRITE_THROUGH
;
98 s
->hfile
= CreateFile(filename
, access_flags
,
99 FILE_SHARE_READ
, NULL
,
100 create_flags
, overlapped
, NULL
);
101 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
102 int err
= GetLastError();
104 if (err
== ERROR_ACCESS_DENIED
)
111 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
112 uint8_t *buf
, int nb_sectors
)
114 BDRVRawState
*s
= bs
->opaque
;
118 int64_t offset
= sector_num
* 512;
119 int count
= nb_sectors
* 512;
121 memset(&ov
, 0, sizeof(ov
));
123 ov
.OffsetHigh
= offset
>> 32;
124 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
127 if (ret_count
== count
)
132 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
133 const uint8_t *buf
, int nb_sectors
)
135 BDRVRawState
*s
= bs
->opaque
;
139 int64_t offset
= sector_num
* 512;
140 int count
= nb_sectors
* 512;
142 memset(&ov
, 0, sizeof(ov
));
144 ov
.OffsetHigh
= offset
>> 32;
145 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
148 if (ret_count
== count
)
153 static void raw_flush(BlockDriverState
*bs
)
155 BDRVRawState
*s
= bs
->opaque
;
156 FlushFileBuffers(s
->hfile
);
159 static void raw_close(BlockDriverState
*bs
)
161 BDRVRawState
*s
= bs
->opaque
;
162 CloseHandle(s
->hfile
);
165 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
167 BDRVRawState
*s
= bs
->opaque
;
172 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
174 if (!SetEndOfFile(s
->hfile
))
179 static int64_t raw_getlength(BlockDriverState
*bs
)
181 BDRVRawState
*s
= bs
->opaque
;
183 ULARGE_INTEGER available
, total
, total_free
;
190 l
.LowPart
= GetFileSize(s
->hfile
, (PDWORD
)&l
.HighPart
);
191 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
195 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
197 l
.QuadPart
= total
.QuadPart
;
200 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
201 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
212 static int raw_create(const char *filename
, int64_t total_size
,
213 const char *backing_file
, int flags
)
217 if (flags
|| backing_file
)
220 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
225 ftruncate(fd
, total_size
* 512);
230 BlockDriver bdrv_raw
= {
231 .format_name
= "raw",
232 .instance_size
= sizeof(BDRVRawState
),
233 .bdrv_open
= raw_open
,
234 .bdrv_close
= raw_close
,
235 .bdrv_create
= raw_create
,
236 .bdrv_flush
= raw_flush
,
237 .bdrv_read
= raw_read
,
238 .bdrv_write
= raw_write
,
239 .bdrv_truncate
= raw_truncate
,
240 .bdrv_getlength
= raw_getlength
,
243 /***********************************************/
246 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
248 char drives
[256], *pdrv
= drives
;
251 memset(drives
, 0, sizeof(drives
));
252 GetLogicalDriveStrings(sizeof(drives
), drives
);
253 while(pdrv
[0] != '\0') {
254 type
= GetDriveType(pdrv
);
257 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
261 pdrv
+= lstrlen(pdrv
) + 1;
266 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
268 BDRVRawState
*s
= bs
->opaque
;
272 if (strstart(filename
, "\\\\.\\", &p
) ||
273 strstart(filename
, "//./", &p
)) {
274 if (stristart(p
, "PhysicalDrive", NULL
))
275 return FTYPE_HARDDISK
;
276 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
277 type
= GetDriveType(s
->drive_path
);
279 case DRIVE_REMOVABLE
:
281 return FTYPE_HARDDISK
;
292 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
294 BDRVRawState
*s
= bs
->opaque
;
295 int access_flags
, create_flags
;
297 char device_name
[64];
299 if (strstart(filename
, "/dev/cdrom", NULL
)) {
300 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
302 filename
= device_name
;
304 /* transform drive letters into device name */
305 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
306 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
307 filename
[1] == ':' && filename
[2] == '\0') {
308 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
309 filename
= device_name
;
312 s
->type
= find_device_type(bs
, filename
);
314 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
315 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
317 access_flags
= GENERIC_READ
;
319 create_flags
= OPEN_EXISTING
;
321 overlapped
= FILE_ATTRIBUTE_NORMAL
;
322 if ((flags
& BDRV_O_NOCACHE
))
323 overlapped
|= FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
;
324 else if (!(flags
& BDRV_O_CACHE_WB
))
325 overlapped
|= FILE_FLAG_WRITE_THROUGH
;
326 s
->hfile
= CreateFile(filename
, access_flags
,
327 FILE_SHARE_READ
, NULL
,
328 create_flags
, overlapped
, NULL
);
329 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
330 int err
= GetLastError();
332 if (err
== ERROR_ACCESS_DENIED
)
340 /***********************************************/
341 /* removable device additional commands */
343 static int raw_is_inserted(BlockDriverState
*bs
)
348 static int raw_media_changed(BlockDriverState
*bs
)
353 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
357 if (s
->type
== FTYPE_FILE
)
360 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_EJECT_MEDIA
,
361 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
363 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_LOAD_MEDIA
,
364 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
368 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
374 BlockDriver bdrv_host_device
= {
375 .format_name
= "host_device",
376 .instance_size
= sizeof(BDRVRawState
),
377 .bdrv_open
= hdev_open
,
378 .bdrv_close
= raw_close
,
379 .bdrv_flush
= raw_flush
,
381 .bdrv_read
= raw_read
,
382 .bdrv_write
= raw_write
,
383 .bdrv_getlength
= raw_getlength
,