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
;
84 if (flags
& BDRV_O_RDWR
) {
85 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
87 access_flags
= GENERIC_READ
;
90 overlapped
= FILE_ATTRIBUTE_NORMAL
;
91 if (flags
& BDRV_O_NOCACHE
)
92 overlapped
|= FILE_FLAG_NO_BUFFERING
;
93 if (!(flags
& BDRV_O_CACHE_WB
))
94 overlapped
|= FILE_FLAG_WRITE_THROUGH
;
95 s
->hfile
= CreateFile(filename
, access_flags
,
96 FILE_SHARE_READ
, NULL
,
97 OPEN_EXISTING
, overlapped
, NULL
);
98 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
99 int err
= GetLastError();
101 if (err
== ERROR_ACCESS_DENIED
)
108 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
109 uint8_t *buf
, int nb_sectors
)
111 BDRVRawState
*s
= bs
->opaque
;
115 int64_t offset
= sector_num
* 512;
116 int count
= nb_sectors
* 512;
118 memset(&ov
, 0, sizeof(ov
));
120 ov
.OffsetHigh
= offset
>> 32;
121 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
124 if (ret_count
== count
)
129 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
130 const uint8_t *buf
, int nb_sectors
)
132 BDRVRawState
*s
= bs
->opaque
;
136 int64_t offset
= sector_num
* 512;
137 int count
= nb_sectors
* 512;
139 memset(&ov
, 0, sizeof(ov
));
141 ov
.OffsetHigh
= offset
>> 32;
142 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
145 if (ret_count
== count
)
150 static int raw_flush(BlockDriverState
*bs
)
152 BDRVRawState
*s
= bs
->opaque
;
155 ret
= FlushFileBuffers(s
->hfile
);
163 static void raw_close(BlockDriverState
*bs
)
165 BDRVRawState
*s
= bs
->opaque
;
166 CloseHandle(s
->hfile
);
169 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
171 BDRVRawState
*s
= bs
->opaque
;
176 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
178 if (!SetEndOfFile(s
->hfile
))
183 static int64_t raw_getlength(BlockDriverState
*bs
)
185 BDRVRawState
*s
= bs
->opaque
;
187 ULARGE_INTEGER available
, total
, total_free
;
194 l
.LowPart
= GetFileSize(s
->hfile
, (PDWORD
)&l
.HighPart
);
195 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
199 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
201 l
.QuadPart
= total
.QuadPart
;
204 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
205 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
216 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
219 int64_t total_size
= 0;
221 /* Read out options */
222 while (options
&& options
->name
) {
223 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
224 total_size
= options
->value
.n
/ 512;
229 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
234 ftruncate(fd
, total_size
* 512);
239 static QEMUOptionParameter raw_create_options
[] = {
241 .name
= BLOCK_OPT_SIZE
,
243 .help
= "Virtual disk size"
248 static BlockDriver bdrv_file
= {
249 .format_name
= "file",
250 .protocol_name
= "file",
251 .instance_size
= sizeof(BDRVRawState
),
252 .bdrv_file_open
= raw_open
,
253 .bdrv_close
= raw_close
,
254 .bdrv_create
= raw_create
,
255 .bdrv_flush
= raw_flush
,
256 .bdrv_read
= raw_read
,
257 .bdrv_write
= raw_write
,
258 .bdrv_truncate
= raw_truncate
,
259 .bdrv_getlength
= raw_getlength
,
261 .create_options
= raw_create_options
,
264 /***********************************************/
267 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
269 char drives
[256], *pdrv
= drives
;
272 memset(drives
, 0, sizeof(drives
));
273 GetLogicalDriveStrings(sizeof(drives
), drives
);
274 while(pdrv
[0] != '\0') {
275 type
= GetDriveType(pdrv
);
278 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
282 pdrv
+= lstrlen(pdrv
) + 1;
287 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
289 BDRVRawState
*s
= bs
->opaque
;
293 if (strstart(filename
, "\\\\.\\", &p
) ||
294 strstart(filename
, "//./", &p
)) {
295 if (stristart(p
, "PhysicalDrive", NULL
))
296 return FTYPE_HARDDISK
;
297 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
298 type
= GetDriveType(s
->drive_path
);
300 case DRIVE_REMOVABLE
:
302 return FTYPE_HARDDISK
;
313 static int hdev_probe_device(const char *filename
)
315 if (strstart(filename
, "/dev/cdrom", NULL
))
317 if (is_windows_drive(filename
))
322 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
324 BDRVRawState
*s
= bs
->opaque
;
325 int access_flags
, create_flags
;
327 char device_name
[64];
329 if (strstart(filename
, "/dev/cdrom", NULL
)) {
330 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
332 filename
= device_name
;
334 /* transform drive letters into device name */
335 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
336 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
337 filename
[1] == ':' && filename
[2] == '\0') {
338 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
339 filename
= device_name
;
342 s
->type
= find_device_type(bs
, filename
);
344 if (flags
& BDRV_O_RDWR
) {
345 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
347 access_flags
= GENERIC_READ
;
349 create_flags
= OPEN_EXISTING
;
351 overlapped
= FILE_ATTRIBUTE_NORMAL
;
352 if (flags
& BDRV_O_NOCACHE
)
353 overlapped
|= FILE_FLAG_NO_BUFFERING
;
354 if (!(flags
& BDRV_O_CACHE_WB
))
355 overlapped
|= FILE_FLAG_WRITE_THROUGH
;
356 s
->hfile
= CreateFile(filename
, access_flags
,
357 FILE_SHARE_READ
, NULL
,
358 create_flags
, overlapped
, NULL
);
359 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
360 int err
= GetLastError();
362 if (err
== ERROR_ACCESS_DENIED
)
370 /***********************************************/
371 /* removable device additional commands */
373 static int raw_is_inserted(BlockDriverState
*bs
)
378 static int raw_media_changed(BlockDriverState
*bs
)
383 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
387 if (s
->type
== FTYPE_FILE
)
390 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_EJECT_MEDIA
,
391 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
393 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_LOAD_MEDIA
,
394 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
398 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
404 static int hdev_has_zero_init(BlockDriverState
*bs
)
409 static BlockDriver bdrv_host_device
= {
410 .format_name
= "host_device",
411 .protocol_name
= "host_device",
412 .instance_size
= sizeof(BDRVRawState
),
413 .bdrv_probe_device
= hdev_probe_device
,
414 .bdrv_file_open
= hdev_open
,
415 .bdrv_close
= raw_close
,
416 .bdrv_flush
= raw_flush
,
417 .bdrv_has_zero_init
= hdev_has_zero_init
,
419 .bdrv_read
= raw_read
,
420 .bdrv_write
= raw_write
,
421 .bdrv_getlength
= raw_getlength
,
424 static void bdrv_file_init(void)
426 bdrv_register(&bdrv_file
);
427 bdrv_register(&bdrv_host_device
);
430 block_init(bdrv_file_init
);