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 int raw_open(BlockDriverState
*bs
, const char *filename
, int flags
)
82 BDRVRawState
*s
= bs
->opaque
;
88 if (flags
& BDRV_O_RDWR
) {
89 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
91 access_flags
= GENERIC_READ
;
94 overlapped
= FILE_ATTRIBUTE_NORMAL
;
95 if (flags
& BDRV_O_NOCACHE
)
96 overlapped
|= FILE_FLAG_NO_BUFFERING
;
97 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 OPEN_EXISTING
, 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 int raw_flush(BlockDriverState
*bs
)
156 BDRVRawState
*s
= bs
->opaque
;
159 ret
= FlushFileBuffers(s
->hfile
);
167 static void raw_close(BlockDriverState
*bs
)
169 BDRVRawState
*s
= bs
->opaque
;
170 CloseHandle(s
->hfile
);
173 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
175 BDRVRawState
*s
= bs
->opaque
;
180 if (!SetFilePointer(s
->hfile
, low
, &high
, FILE_BEGIN
))
182 if (!SetEndOfFile(s
->hfile
))
187 static int64_t raw_getlength(BlockDriverState
*bs
)
189 BDRVRawState
*s
= bs
->opaque
;
191 ULARGE_INTEGER available
, total
, total_free
;
198 l
.LowPart
= GetFileSize(s
->hfile
, (PDWORD
)&l
.HighPart
);
199 if (l
.LowPart
== 0xffffffffUL
&& GetLastError() != NO_ERROR
)
203 if (!GetDiskFreeSpaceEx(s
->drive_path
, &available
, &total
, &total_free
))
205 l
.QuadPart
= total
.QuadPart
;
208 status
= DeviceIoControl(s
->hfile
, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
209 NULL
, 0, &dg
, sizeof(dg
), &count
, NULL
);
220 static int64_t raw_get_allocated_file_size(BlockDriverState
*bs
)
222 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
,
224 get_compressed_t get_compressed
;
226 const char *filename
= bs
->filename
;
227 /* WinNT support GetCompressedFileSize to determine allocate size */
229 (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"),
230 "GetCompressedFileSizeA");
231 if (get_compressed
) {
233 low
= get_compressed(filename
, &high
);
234 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
) {
235 return (((int64_t) high
) << 32) + low
;
239 if (_stati64(filename
, &st
) < 0) {
245 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
248 int64_t total_size
= 0;
250 /* Read out options */
251 while (options
&& options
->name
) {
252 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
253 total_size
= options
->value
.n
/ 512;
258 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
263 ftruncate(fd
, total_size
* 512);
268 static QEMUOptionParameter raw_create_options
[] = {
270 .name
= BLOCK_OPT_SIZE
,
272 .help
= "Virtual disk size"
277 static BlockDriver bdrv_file
= {
278 .format_name
= "file",
279 .protocol_name
= "file",
280 .instance_size
= sizeof(BDRVRawState
),
281 .bdrv_file_open
= raw_open
,
282 .bdrv_close
= raw_close
,
283 .bdrv_create
= raw_create
,
285 .bdrv_read
= raw_read
,
286 .bdrv_write
= raw_write
,
287 .bdrv_co_flush_to_disk
= raw_flush
,
289 .bdrv_truncate
= raw_truncate
,
290 .bdrv_getlength
= raw_getlength
,
291 .bdrv_get_allocated_file_size
292 = raw_get_allocated_file_size
,
294 .create_options
= raw_create_options
,
297 /***********************************************/
300 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
302 char drives
[256], *pdrv
= drives
;
305 memset(drives
, 0, sizeof(drives
));
306 GetLogicalDriveStrings(sizeof(drives
), drives
);
307 while(pdrv
[0] != '\0') {
308 type
= GetDriveType(pdrv
);
311 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
315 pdrv
+= lstrlen(pdrv
) + 1;
320 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
322 BDRVRawState
*s
= bs
->opaque
;
326 if (strstart(filename
, "\\\\.\\", &p
) ||
327 strstart(filename
, "//./", &p
)) {
328 if (stristart(p
, "PhysicalDrive", NULL
))
329 return FTYPE_HARDDISK
;
330 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
331 type
= GetDriveType(s
->drive_path
);
333 case DRIVE_REMOVABLE
:
335 return FTYPE_HARDDISK
;
346 static int hdev_probe_device(const char *filename
)
348 if (strstart(filename
, "/dev/cdrom", NULL
))
350 if (is_windows_drive(filename
))
355 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
357 BDRVRawState
*s
= bs
->opaque
;
358 int access_flags
, create_flags
;
360 char device_name
[64];
362 if (strstart(filename
, "/dev/cdrom", NULL
)) {
363 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
365 filename
= device_name
;
367 /* transform drive letters into device name */
368 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
369 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
370 filename
[1] == ':' && filename
[2] == '\0') {
371 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
372 filename
= device_name
;
375 s
->type
= find_device_type(bs
, filename
);
377 if (flags
& BDRV_O_RDWR
) {
378 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
380 access_flags
= GENERIC_READ
;
382 create_flags
= OPEN_EXISTING
;
384 overlapped
= FILE_ATTRIBUTE_NORMAL
;
385 if (flags
& BDRV_O_NOCACHE
)
386 overlapped
|= FILE_FLAG_NO_BUFFERING
;
387 if (!(flags
& BDRV_O_CACHE_WB
))
388 overlapped
|= FILE_FLAG_WRITE_THROUGH
;
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
);