2007-12-15 20:28:36 +03:00
|
|
|
/*
|
|
|
|
* Block driver for RAW files (win32)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "qemu-timer.h"
|
|
|
|
#include "block_int.h"
|
2009-05-10 02:03:42 +04:00
|
|
|
#include "module.h"
|
2009-03-08 19:26:59 +03:00
|
|
|
#include <windows.h>
|
2007-12-15 20:28:36 +03:00
|
|
|
#include <winioctl.h>
|
|
|
|
|
|
|
|
#define FTYPE_FILE 0
|
|
|
|
#define FTYPE_CD 1
|
|
|
|
#define FTYPE_HARDDISK 2
|
|
|
|
|
|
|
|
typedef struct BDRVRawState {
|
|
|
|
HANDLE hfile;
|
|
|
|
int type;
|
|
|
|
char drive_path[16]; /* format: "d:\" */
|
|
|
|
} BDRVRawState;
|
|
|
|
|
|
|
|
int qemu_ftruncate64(int fd, int64_t length)
|
|
|
|
{
|
|
|
|
LARGE_INTEGER li;
|
|
|
|
LONG high;
|
|
|
|
HANDLE h;
|
|
|
|
BOOL res;
|
|
|
|
|
|
|
|
if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
h = (HANDLE)_get_osfhandle(fd);
|
|
|
|
|
|
|
|
/* get current position, ftruncate do not change position */
|
|
|
|
li.HighPart = 0;
|
|
|
|
li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
|
|
|
|
if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
high = length >> 32;
|
|
|
|
if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
|
|
|
|
return -1;
|
|
|
|
res = SetEndOfFile(h);
|
|
|
|
|
|
|
|
/* back to old position */
|
|
|
|
SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
|
|
|
|
return res ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_sparse(int fd)
|
|
|
|
{
|
|
|
|
DWORD returned;
|
|
|
|
return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
|
|
|
|
NULL, 0, NULL, 0, &returned, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
int access_flags, create_flags;
|
|
|
|
DWORD overlapped;
|
|
|
|
|
|
|
|
s->type = FTYPE_FILE;
|
|
|
|
|
|
|
|
if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
|
|
|
access_flags = GENERIC_READ | GENERIC_WRITE;
|
|
|
|
} else {
|
|
|
|
access_flags = GENERIC_READ;
|
|
|
|
}
|
|
|
|
if (flags & BDRV_O_CREAT) {
|
|
|
|
create_flags = CREATE_ALWAYS;
|
|
|
|
} else {
|
|
|
|
create_flags = OPEN_EXISTING;
|
|
|
|
}
|
2008-09-15 19:51:35 +04:00
|
|
|
overlapped = FILE_ATTRIBUTE_NORMAL;
|
2008-10-14 18:42:54 +04:00
|
|
|
if ((flags & BDRV_O_NOCACHE))
|
2007-12-24 17:33:24 +03:00
|
|
|
overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
|
2008-10-14 18:42:54 +04:00
|
|
|
else if (!(flags & BDRV_O_CACHE_WB))
|
|
|
|
overlapped |= FILE_FLAG_WRITE_THROUGH;
|
2007-12-15 20:28:36 +03:00
|
|
|
s->hfile = CreateFile(filename, access_flags,
|
|
|
|
FILE_SHARE_READ, NULL,
|
|
|
|
create_flags, overlapped, NULL);
|
|
|
|
if (s->hfile == INVALID_HANDLE_VALUE) {
|
|
|
|
int err = GetLastError();
|
|
|
|
|
|
|
|
if (err == ERROR_ACCESS_DENIED)
|
|
|
|
return -EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-12 22:57:16 +03:00
|
|
|
static int raw_read(BlockDriverState *bs, int64_t sector_num,
|
|
|
|
uint8_t *buf, int nb_sectors)
|
2007-12-15 20:28:36 +03:00
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
OVERLAPPED ov;
|
|
|
|
DWORD ret_count;
|
|
|
|
int ret;
|
2009-03-12 22:57:16 +03:00
|
|
|
int64_t offset = sector_num * 512;
|
|
|
|
int count = nb_sectors * 512;
|
2007-12-15 20:28:36 +03:00
|
|
|
|
|
|
|
memset(&ov, 0, sizeof(ov));
|
|
|
|
ov.Offset = offset;
|
|
|
|
ov.OffsetHigh = offset >> 32;
|
|
|
|
ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
|
2009-03-30 00:54:17 +04:00
|
|
|
if (!ret)
|
|
|
|
return ret_count;
|
2009-03-13 06:12:03 +03:00
|
|
|
if (ret_count == count)
|
|
|
|
ret_count = 0;
|
2007-12-15 20:28:36 +03:00
|
|
|
return ret_count;
|
|
|
|
}
|
|
|
|
|
2009-03-12 22:57:16 +03:00
|
|
|
static int raw_write(BlockDriverState *bs, int64_t sector_num,
|
|
|
|
const uint8_t *buf, int nb_sectors)
|
2007-12-15 20:28:36 +03:00
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
OVERLAPPED ov;
|
|
|
|
DWORD ret_count;
|
|
|
|
int ret;
|
2009-03-12 22:57:16 +03:00
|
|
|
int64_t offset = sector_num * 512;
|
|
|
|
int count = nb_sectors * 512;
|
2007-12-15 20:28:36 +03:00
|
|
|
|
|
|
|
memset(&ov, 0, sizeof(ov));
|
|
|
|
ov.Offset = offset;
|
|
|
|
ov.OffsetHigh = offset >> 32;
|
|
|
|
ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
|
2009-03-30 00:54:17 +04:00
|
|
|
if (!ret)
|
|
|
|
return ret_count;
|
2009-03-13 06:12:03 +03:00
|
|
|
if (ret_count == count)
|
|
|
|
ret_count = 0;
|
2007-12-15 20:28:36 +03:00
|
|
|
return ret_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void raw_flush(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
FlushFileBuffers(s->hfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void raw_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
CloseHandle(s->hfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int raw_truncate(BlockDriverState *bs, int64_t offset)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
2009-04-05 22:03:31 +04:00
|
|
|
LONG low, high;
|
2007-12-15 20:28:36 +03:00
|
|
|
|
|
|
|
low = offset;
|
|
|
|
high = offset >> 32;
|
|
|
|
if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
|
|
|
|
return -EIO;
|
|
|
|
if (!SetEndOfFile(s->hfile))
|
|
|
|
return -EIO;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t raw_getlength(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
LARGE_INTEGER l;
|
|
|
|
ULARGE_INTEGER available, total, total_free;
|
|
|
|
DISK_GEOMETRY_EX dg;
|
|
|
|
DWORD count;
|
|
|
|
BOOL status;
|
|
|
|
|
|
|
|
switch(s->type) {
|
|
|
|
case FTYPE_FILE:
|
2009-04-05 22:03:31 +04:00
|
|
|
l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
|
2007-12-15 20:28:36 +03:00
|
|
|
if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
|
|
|
|
return -EIO;
|
|
|
|
break;
|
|
|
|
case FTYPE_CD:
|
|
|
|
if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
|
|
|
|
return -EIO;
|
|
|
|
l.QuadPart = total.QuadPart;
|
|
|
|
break;
|
|
|
|
case FTYPE_HARDDISK:
|
|
|
|
status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
|
|
|
|
NULL, 0, &dg, sizeof(dg), &count, NULL);
|
|
|
|
if (status != 0) {
|
|
|
|
l = dg.DiskSize;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
return l.QuadPart;
|
|
|
|
}
|
|
|
|
|
2009-05-18 18:42:10 +04:00
|
|
|
static int raw_create(const char *filename, QEMUOptionParameter *options)
|
2007-12-15 20:28:36 +03:00
|
|
|
{
|
|
|
|
int fd;
|
2009-05-18 18:42:10 +04:00
|
|
|
int64_t total_size = 0;
|
2007-12-15 20:28:36 +03:00
|
|
|
|
2009-05-18 18:42:10 +04:00
|
|
|
/* Read out options */
|
|
|
|
while (options && options->name) {
|
|
|
|
if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
|
|
|
total_size = options->value.n / 512;
|
|
|
|
}
|
|
|
|
options++;
|
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
|
|
|
|
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
|
|
|
|
0644);
|
|
|
|
if (fd < 0)
|
|
|
|
return -EIO;
|
|
|
|
set_sparse(fd);
|
|
|
|
ftruncate(fd, total_size * 512);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-18 18:42:10 +04:00
|
|
|
static QEMUOptionParameter raw_create_options[] = {
|
2009-06-04 17:39:38 +04:00
|
|
|
{
|
|
|
|
.name = BLOCK_OPT_SIZE,
|
|
|
|
.type = OPT_SIZE,
|
|
|
|
.help = "Virtual disk size"
|
|
|
|
},
|
2009-05-18 18:42:10 +04:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2009-05-10 02:03:42 +04:00
|
|
|
static BlockDriver bdrv_raw = {
|
2009-04-07 22:23:51 +04:00
|
|
|
.format_name = "raw",
|
|
|
|
.instance_size = sizeof(BDRVRawState),
|
|
|
|
.bdrv_open = raw_open,
|
|
|
|
.bdrv_close = raw_close,
|
|
|
|
.bdrv_create = raw_create,
|
|
|
|
.bdrv_flush = raw_flush,
|
|
|
|
.bdrv_read = raw_read,
|
|
|
|
.bdrv_write = raw_write,
|
|
|
|
.bdrv_truncate = raw_truncate,
|
|
|
|
.bdrv_getlength = raw_getlength,
|
2009-05-18 18:42:10 +04:00
|
|
|
|
|
|
|
.create_options = raw_create_options,
|
2007-12-15 20:28:36 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/***********************************************/
|
|
|
|
/* host device */
|
|
|
|
|
|
|
|
static int find_cdrom(char *cdrom_name, int cdrom_name_size)
|
|
|
|
{
|
|
|
|
char drives[256], *pdrv = drives;
|
|
|
|
UINT type;
|
|
|
|
|
|
|
|
memset(drives, 0, sizeof(drives));
|
|
|
|
GetLogicalDriveStrings(sizeof(drives), drives);
|
|
|
|
while(pdrv[0] != '\0') {
|
|
|
|
type = GetDriveType(pdrv);
|
|
|
|
switch(type) {
|
|
|
|
case DRIVE_CDROM:
|
|
|
|
snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pdrv += lstrlen(pdrv) + 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_device_type(BlockDriverState *bs, const char *filename)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
UINT type;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if (strstart(filename, "\\\\.\\", &p) ||
|
|
|
|
strstart(filename, "//./", &p)) {
|
|
|
|
if (stristart(p, "PhysicalDrive", NULL))
|
|
|
|
return FTYPE_HARDDISK;
|
|
|
|
snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
|
|
|
|
type = GetDriveType(s->drive_path);
|
2009-04-07 05:24:53 +04:00
|
|
|
switch (type) {
|
|
|
|
case DRIVE_REMOVABLE:
|
|
|
|
case DRIVE_FIXED:
|
|
|
|
return FTYPE_HARDDISK;
|
|
|
|
case DRIVE_CDROM:
|
2007-12-15 20:28:36 +03:00
|
|
|
return FTYPE_CD;
|
2009-04-07 05:24:53 +04:00
|
|
|
default:
|
2007-12-15 20:28:36 +03:00
|
|
|
return FTYPE_FILE;
|
2009-04-07 05:24:53 +04:00
|
|
|
}
|
2007-12-15 20:28:36 +03:00
|
|
|
} else {
|
|
|
|
return FTYPE_FILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-15 16:04:22 +04:00
|
|
|
static int hdev_probe_device(const char *filename)
|
|
|
|
{
|
|
|
|
if (strstart(filename, "/dev/cdrom", NULL))
|
|
|
|
return 100;
|
|
|
|
if (is_windows_drive(filename))
|
|
|
|
return 100;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-15 20:28:36 +03:00
|
|
|
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
|
|
|
|
{
|
|
|
|
BDRVRawState *s = bs->opaque;
|
|
|
|
int access_flags, create_flags;
|
|
|
|
DWORD overlapped;
|
|
|
|
char device_name[64];
|
|
|
|
|
|
|
|
if (strstart(filename, "/dev/cdrom", NULL)) {
|
|
|
|
if (find_cdrom(device_name, sizeof(device_name)) < 0)
|
|
|
|
return -ENOENT;
|
|
|
|
filename = device_name;
|
|
|
|
} else {
|
|
|
|
/* transform drive letters into device name */
|
|
|
|
if (((filename[0] >= 'a' && filename[0] <= 'z') ||
|
|
|
|
(filename[0] >= 'A' && filename[0] <= 'Z')) &&
|
|
|
|
filename[1] == ':' && filename[2] == '\0') {
|
|
|
|
snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
|
|
|
|
filename = device_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->type = find_device_type(bs, filename);
|
|
|
|
|
|
|
|
if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
|
|
|
access_flags = GENERIC_READ | GENERIC_WRITE;
|
|
|
|
} else {
|
|
|
|
access_flags = GENERIC_READ;
|
|
|
|
}
|
|
|
|
create_flags = OPEN_EXISTING;
|
|
|
|
|
2008-09-15 19:51:35 +04:00
|
|
|
overlapped = FILE_ATTRIBUTE_NORMAL;
|
2008-10-14 18:42:54 +04:00
|
|
|
if ((flags & BDRV_O_NOCACHE))
|
2007-12-24 17:33:24 +03:00
|
|
|
overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
|
2008-10-14 18:42:54 +04:00
|
|
|
else if (!(flags & BDRV_O_CACHE_WB))
|
|
|
|
overlapped |= FILE_FLAG_WRITE_THROUGH;
|
2007-12-15 20:28:36 +03:00
|
|
|
s->hfile = CreateFile(filename, access_flags,
|
|
|
|
FILE_SHARE_READ, NULL,
|
|
|
|
create_flags, overlapped, NULL);
|
|
|
|
if (s->hfile == INVALID_HANDLE_VALUE) {
|
|
|
|
int err = GetLastError();
|
|
|
|
|
|
|
|
if (err == ERROR_ACCESS_DENIED)
|
|
|
|
return -EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/***********************************************/
|
|
|
|
/* removable device additional commands */
|
|
|
|
|
|
|
|
static int raw_is_inserted(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int raw_media_changed(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int raw_eject(BlockDriverState *bs, int eject_flag)
|
|
|
|
{
|
|
|
|
DWORD ret_count;
|
|
|
|
|
|
|
|
if (s->type == FTYPE_FILE)
|
|
|
|
return -ENOTSUP;
|
|
|
|
if (eject_flag) {
|
|
|
|
DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
|
|
|
|
NULL, 0, NULL, 0, &lpBytesReturned, NULL);
|
|
|
|
} else {
|
|
|
|
DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
|
|
|
|
NULL, 0, NULL, 0, &lpBytesReturned, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int raw_set_locked(BlockDriverState *bs, int locked)
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-10 02:03:42 +04:00
|
|
|
static BlockDriver bdrv_host_device = {
|
2009-03-08 01:00:29 +03:00
|
|
|
.format_name = "host_device",
|
|
|
|
.instance_size = sizeof(BDRVRawState),
|
2009-06-15 16:04:22 +04:00
|
|
|
.bdrv_probe_device = hdev_probe_device,
|
2009-03-08 01:00:29 +03:00
|
|
|
.bdrv_open = hdev_open,
|
|
|
|
.bdrv_close = raw_close,
|
|
|
|
.bdrv_flush = raw_flush,
|
2007-12-15 20:28:36 +03:00
|
|
|
|
2009-03-12 22:57:16 +03:00
|
|
|
.bdrv_read = raw_read,
|
|
|
|
.bdrv_write = raw_write,
|
2009-03-08 01:00:29 +03:00
|
|
|
.bdrv_getlength = raw_getlength,
|
2007-12-15 20:28:36 +03:00
|
|
|
};
|
2009-05-10 02:03:42 +04:00
|
|
|
|
|
|
|
static void bdrv_raw_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_raw);
|
|
|
|
bdrv_register(&bdrv_host_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_raw_init);
|