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
)
49 if ((GetVersion() & 0x80000000UL
) && (length
>> 32) != 0)
52 h
= (HANDLE
)_get_osfhandle(fd
);
54 /* get current position, ftruncate do not change position */
56 li
.LowPart
= SetFilePointer (h
, 0, &li
.HighPart
, FILE_CURRENT
);
57 if (li
.LowPart
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
62 dw
= SetFilePointer(h
, (DWORD
) length
, &high
, FILE_BEGIN
);
63 if (dw
== INVALID_SET_FILE_POINTER
&& GetLastError() != NO_ERROR
) {
66 res
= SetEndOfFile(h
);
68 /* back to old position */
69 SetFilePointer(h
, li
.LowPart
, &li
.HighPart
, FILE_BEGIN
);
73 static int set_sparse(int fd
)
76 return (int) DeviceIoControl((HANDLE
)_get_osfhandle(fd
), FSCTL_SET_SPARSE
,
77 NULL
, 0, NULL
, 0, &returned
, NULL
);
80 static void raw_parse_flags(int flags
, int *access_flags
, DWORD
*overlapped
)
82 assert(access_flags
!= NULL
);
83 assert(overlapped
!= NULL
);
85 if (flags
& BDRV_O_RDWR
) {
86 *access_flags
= GENERIC_READ
| GENERIC_WRITE
;
88 *access_flags
= GENERIC_READ
;
91 *overlapped
= FILE_ATTRIBUTE_NORMAL
;
92 if (flags
& BDRV_O_NOCACHE
) {
93 *overlapped
|= FILE_FLAG_NO_BUFFERING
;
97 static int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
99 BDRVRawState
*s
= bs
->opaque
;
103 s
->type
= FTYPE_FILE
;
105 raw_parse_flags(flags
, &access_flags
, &overlapped
);
107 s
->hfile
= CreateFile(filename
, access_flags
,
108 FILE_SHARE_READ
, NULL
,
109 OPEN_EXISTING
, overlapped
, NULL
);
110 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
111 int err
= GetLastError();
113 if (err
== ERROR_ACCESS_DENIED
)
120 static int raw_read(BlockDriverState
*bs
, int64_t sector_num
,
121 uint8_t *buf
, int nb_sectors
)
123 BDRVRawState
*s
= bs
->opaque
;
127 int64_t offset
= sector_num
* 512;
128 int count
= nb_sectors
* 512;
130 memset(&ov
, 0, sizeof(ov
));
132 ov
.OffsetHigh
= offset
>> 32;
133 ret
= ReadFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
136 if (ret_count
== count
)
141 static int raw_write(BlockDriverState
*bs
, int64_t sector_num
,
142 const uint8_t *buf
, int nb_sectors
)
144 BDRVRawState
*s
= bs
->opaque
;
148 int64_t offset
= sector_num
* 512;
149 int count
= nb_sectors
* 512;
151 memset(&ov
, 0, sizeof(ov
));
153 ov
.OffsetHigh
= offset
>> 32;
154 ret
= WriteFile(s
->hfile
, buf
, count
, &ret_count
, &ov
);
157 if (ret_count
== count
)
162 static int raw_flush(BlockDriverState
*bs
)
164 BDRVRawState
*s
= bs
->opaque
;
167 ret
= FlushFileBuffers(s
->hfile
);
175 static void raw_close(BlockDriverState
*bs
)
177 BDRVRawState
*s
= bs
->opaque
;
178 CloseHandle(s
->hfile
);
181 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
183 BDRVRawState
*s
= bs
->opaque
;
188 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
190 if (!SetEndOfFile(s
->hfile
))
195 static int64_t raw_getlength(BlockDriverState
*bs
)
197 BDRVRawState
*s
= bs
->opaque
;
199 ULARGE_INTEGER available
, total
, total_free
;
206 l
.LowPart
= GetFileSize(s
->hfile
, (PDWORD
)&l
.HighPart
);
207 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
211 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
213 l
.QuadPart
= total
.QuadPart
;
216 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
217 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
228 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
230 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
,
232 get_compressed_t get_compressed
;
234 const char *filename
= bs
->filename
;
235 /* WinNT support GetCompressedFileSize to determine allocate size */
237 (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"),
238 "GetCompressedFileSizeA");
239 if (get_compressed
) {
241 low
= get_compressed(filename
, &high
);
242 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
) {
243 return (((int64_t) high
) << 32) + low
;
247 if (_stati64(filename
, &st
) < 0) {
253 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
256 int64_t total_size
= 0;
258 /* Read out options */
259 while (options
&& options
->name
) {
260 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
261 total_size
= options
->value
.n
/ 512;
266 fd
= qemu_open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
271 ftruncate(fd
, total_size
* 512);
276 static QEMUOptionParameter raw_create_options
[] = {
278 .name
= BLOCK_OPT_SIZE
,
280 .help
= "Virtual disk size"
285 static BlockDriver bdrv_file
= {
286 .format_name
= "file",
287 .protocol_name
= "file",
288 .instance_size
= sizeof(BDRVRawState
),
289 .bdrv_file_open
= raw_open
,
290 .bdrv_close
= raw_close
,
291 .bdrv_create
= raw_create
,
293 .bdrv_read
= raw_read
,
294 .bdrv_write
= raw_write
,
295 .bdrv_co_flush_to_disk
= raw_flush
,
297 .bdrv_truncate
= raw_truncate
,
298 .bdrv_getlength
= raw_getlength
,
299 .bdrv_get_allocated_file_size
300 = raw_get_allocated_file_size
,
302 .create_options
= raw_create_options
,
305 /***********************************************/
308 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
310 char drives
[256], *pdrv
= drives
;
313 memset(drives
, 0, sizeof(drives
));
314 GetLogicalDriveStrings(sizeof(drives
), drives
);
315 while(pdrv
[0] != '\0') {
316 type
= GetDriveType(pdrv
);
319 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
323 pdrv
+= lstrlen(pdrv
) + 1;
328 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
330 BDRVRawState
*s
= bs
->opaque
;
334 if (strstart(filename
, "\\\\.\\", &p
) ||
335 strstart(filename
, "//./", &p
)) {
336 if (stristart(p
, "PhysicalDrive", NULL
))
337 return FTYPE_HARDDISK
;
338 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
339 type
= GetDriveType(s
->drive_path
);
341 case DRIVE_REMOVABLE
:
343 return FTYPE_HARDDISK
;
354 static int hdev_probe_device(const char *filename
)
356 if (strstart(filename
, "/dev/cdrom", NULL
))
358 if (is_windows_drive(filename
))
363 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
365 BDRVRawState
*s
= bs
->opaque
;
366 int access_flags
, create_flags
;
368 char device_name
[64];
370 if (strstart(filename
, "/dev/cdrom", NULL
)) {
371 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
373 filename
= device_name
;
375 /* transform drive letters into device name */
376 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
377 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
378 filename
[1] == ':' && filename
[2] == '\0') {
379 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
380 filename
= device_name
;
383 s
->type
= find_device_type(bs
, filename
);
385 raw_parse_flags(flags
, &access_flags
, &overlapped
);
387 create_flags
= OPEN_EXISTING
;
389 s
->hfile
= CreateFile(filename
, access_flags
,
390 FILE_SHARE_READ
, NULL
,
391 create_flags
, overlapped
, NULL
);
392 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
393 int err
= GetLastError();
395 if (err
== ERROR_ACCESS_DENIED
)
402 static int hdev_has_zero_init(BlockDriverState
*bs
)
407 static BlockDriver bdrv_host_device
= {
408 .format_name
= "host_device",
409 .protocol_name
= "host_device",
410 .instance_size
= sizeof(BDRVRawState
),
411 .bdrv_probe_device
= hdev_probe_device
,
412 .bdrv_file_open
= hdev_open
,
413 .bdrv_close
= raw_close
,
414 .bdrv_has_zero_init
= hdev_has_zero_init
,
416 .bdrv_read
= raw_read
,
417 .bdrv_write
= raw_write
,
418 .bdrv_co_flush_to_disk
= raw_flush
,
420 .bdrv_getlength
= raw_getlength
,
421 .bdrv_get_allocated_file_size
422 = raw_get_allocated_file_size
,
425 static void bdrv_file_init(void)
427 bdrv_register(&bdrv_file
);
428 bdrv_register(&bdrv_host_device
);
431 block_init(bdrv_file_init
);