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
, QEMUOptionParameter
*options
)
216 int64_t total_size
= 0;
218 /* Read out options */
219 while (options
&& options
->name
) {
220 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
221 total_size
= options
->value
.n
/ 512;
226 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
231 ftruncate(fd
, total_size
* 512);
236 static QEMUOptionParameter raw_create_options
[] = {
238 .name
= BLOCK_OPT_SIZE
,
240 .help
= "Virtual disk size"
245 static BlockDriver bdrv_raw
= {
246 .format_name
= "raw",
247 .instance_size
= sizeof(BDRVRawState
),
248 .bdrv_open
= raw_open
,
249 .bdrv_close
= raw_close
,
250 .bdrv_create
= raw_create
,
251 .bdrv_flush
= raw_flush
,
252 .bdrv_read
= raw_read
,
253 .bdrv_write
= raw_write
,
254 .bdrv_truncate
= raw_truncate
,
255 .bdrv_getlength
= raw_getlength
,
257 .create_options
= raw_create_options
,
260 /***********************************************/
263 static int find_cdrom(char *cdrom_name
, int cdrom_name_size
)
265 char drives
[256], *pdrv
= drives
;
268 memset(drives
, 0, sizeof(drives
));
269 GetLogicalDriveStrings(sizeof(drives
), drives
);
270 while(pdrv
[0] != '\0') {
271 type
= GetDriveType(pdrv
);
274 snprintf(cdrom_name
, cdrom_name_size
, "\\\\.\\%c:", pdrv
[0]);
278 pdrv
+= lstrlen(pdrv
) + 1;
283 static int find_device_type(BlockDriverState
*bs
, const char *filename
)
285 BDRVRawState
*s
= bs
->opaque
;
289 if (strstart(filename
, "\\\\.\\", &p
) ||
290 strstart(filename
, "//./", &p
)) {
291 if (stristart(p
, "PhysicalDrive", NULL
))
292 return FTYPE_HARDDISK
;
293 snprintf(s
->drive_path
, sizeof(s
->drive_path
), "%c:\\", p
[0]);
294 type
= GetDriveType(s
->drive_path
);
296 case DRIVE_REMOVABLE
:
298 return FTYPE_HARDDISK
;
309 static int hdev_open(BlockDriverState
*bs
, const char *filename
, int flags
)
311 BDRVRawState
*s
= bs
->opaque
;
312 int access_flags
, create_flags
;
314 char device_name
[64];
316 if (strstart(filename
, "/dev/cdrom", NULL
)) {
317 if (find_cdrom(device_name
, sizeof(device_name
)) < 0)
319 filename
= device_name
;
321 /* transform drive letters into device name */
322 if (((filename
[0] >= 'a' && filename
[0] <= 'z') ||
323 (filename
[0] >= 'A' && filename
[0] <= 'Z')) &&
324 filename
[1] == ':' && filename
[2] == '\0') {
325 snprintf(device_name
, sizeof(device_name
), "\\\\.\\%c:", filename
[0]);
326 filename
= device_name
;
329 s
->type
= find_device_type(bs
, filename
);
331 if ((flags
& BDRV_O_ACCESS
) == O_RDWR
) {
332 access_flags
= GENERIC_READ
| GENERIC_WRITE
;
334 access_flags
= GENERIC_READ
;
336 create_flags
= OPEN_EXISTING
;
338 overlapped
= FILE_ATTRIBUTE_NORMAL
;
339 if ((flags
& BDRV_O_NOCACHE
))
340 overlapped
|= FILE_FLAG_NO_BUFFERING
| FILE_FLAG_WRITE_THROUGH
;
341 else if (!(flags
& BDRV_O_CACHE_WB
))
342 overlapped
|= FILE_FLAG_WRITE_THROUGH
;
343 s
->hfile
= CreateFile(filename
, access_flags
,
344 FILE_SHARE_READ
, NULL
,
345 create_flags
, overlapped
, NULL
);
346 if (s
->hfile
== INVALID_HANDLE_VALUE
) {
347 int err
= GetLastError();
349 if (err
== ERROR_ACCESS_DENIED
)
357 /***********************************************/
358 /* removable device additional commands */
360 static int raw_is_inserted(BlockDriverState
*bs
)
365 static int raw_media_changed(BlockDriverState
*bs
)
370 static int raw_eject(BlockDriverState
*bs
, int eject_flag
)
374 if (s
->type
== FTYPE_FILE
)
377 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_EJECT_MEDIA
,
378 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
380 DeviceIoControl(s
->hfile
, IOCTL_STORAGE_LOAD_MEDIA
,
381 NULL
, 0, NULL
, 0, &lpBytesReturned
, NULL
);
385 static int raw_set_locked(BlockDriverState
*bs
, int locked
)
391 static BlockDriver bdrv_host_device
= {
392 .format_name
= "host_device",
393 .instance_size
= sizeof(BDRVRawState
),
394 .bdrv_open
= hdev_open
,
395 .bdrv_close
= raw_close
,
396 .bdrv_flush
= raw_flush
,
398 .bdrv_read
= raw_read
,
399 .bdrv_write
= raw_write
,
400 .bdrv_getlength
= raw_getlength
,
403 static void bdrv_raw_init(void)
405 bdrv_register(&bdrv_raw
);
406 bdrv_register(&bdrv_host_device
);
409 block_init(bdrv_raw_init
);