Statistics
| Branch: | Revision:

root / block-raw-win32.c @ 2ac71179

History | View | Annotate | Download (10.4 kB)

1 223d4670 ths
/*
2 223d4670 ths
 * Block driver for RAW files (win32)
3 223d4670 ths
 *
4 223d4670 ths
 * Copyright (c) 2006 Fabrice Bellard
5 223d4670 ths
 *
6 223d4670 ths
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 223d4670 ths
 * of this software and associated documentation files (the "Software"), to deal
8 223d4670 ths
 * in the Software without restriction, including without limitation the rights
9 223d4670 ths
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 223d4670 ths
 * copies of the Software, and to permit persons to whom the Software is
11 223d4670 ths
 * furnished to do so, subject to the following conditions:
12 223d4670 ths
 *
13 223d4670 ths
 * The above copyright notice and this permission notice shall be included in
14 223d4670 ths
 * all copies or substantial portions of the Software.
15 223d4670 ths
 *
16 223d4670 ths
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 223d4670 ths
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 223d4670 ths
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 223d4670 ths
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 223d4670 ths
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 223d4670 ths
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 223d4670 ths
 * THE SOFTWARE.
23 223d4670 ths
 */
24 223d4670 ths
#include "qemu-common.h"
25 223d4670 ths
#include "qemu-timer.h"
26 223d4670 ths
#include "block_int.h"
27 223d4670 ths
#include <assert.h>
28 49dc768d aliguori
#include <windows.h>
29 223d4670 ths
#include <winioctl.h>
30 223d4670 ths
31 223d4670 ths
#define FTYPE_FILE 0
32 223d4670 ths
#define FTYPE_CD     1
33 223d4670 ths
#define FTYPE_HARDDISK 2
34 223d4670 ths
35 223d4670 ths
typedef struct BDRVRawState {
36 223d4670 ths
    HANDLE hfile;
37 223d4670 ths
    int type;
38 223d4670 ths
    char drive_path[16]; /* format: "d:\" */
39 223d4670 ths
} BDRVRawState;
40 223d4670 ths
41 223d4670 ths
int qemu_ftruncate64(int fd, int64_t length)
42 223d4670 ths
{
43 223d4670 ths
    LARGE_INTEGER li;
44 223d4670 ths
    LONG high;
45 223d4670 ths
    HANDLE h;
46 223d4670 ths
    BOOL res;
47 223d4670 ths
48 223d4670 ths
    if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
49 223d4670 ths
        return -1;
50 223d4670 ths
51 223d4670 ths
    h = (HANDLE)_get_osfhandle(fd);
52 223d4670 ths
53 223d4670 ths
    /* get current position, ftruncate do not change position */
54 223d4670 ths
    li.HighPart = 0;
55 223d4670 ths
    li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
56 223d4670 ths
    if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
57 223d4670 ths
        return -1;
58 223d4670 ths
59 223d4670 ths
    high = length >> 32;
60 223d4670 ths
    if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
61 223d4670 ths
        return -1;
62 223d4670 ths
    res = SetEndOfFile(h);
63 223d4670 ths
64 223d4670 ths
    /* back to old position */
65 223d4670 ths
    SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
66 223d4670 ths
    return res ? 0 : -1;
67 223d4670 ths
}
68 223d4670 ths
69 223d4670 ths
static int set_sparse(int fd)
70 223d4670 ths
{
71 223d4670 ths
    DWORD returned;
72 223d4670 ths
    return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
73 223d4670 ths
                                 NULL, 0, NULL, 0, &returned, NULL);
74 223d4670 ths
}
75 223d4670 ths
76 223d4670 ths
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
77 223d4670 ths
{
78 223d4670 ths
    BDRVRawState *s = bs->opaque;
79 223d4670 ths
    int access_flags, create_flags;
80 223d4670 ths
    DWORD overlapped;
81 223d4670 ths
82 223d4670 ths
    s->type = FTYPE_FILE;
83 223d4670 ths
84 223d4670 ths
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
85 223d4670 ths
        access_flags = GENERIC_READ | GENERIC_WRITE;
86 223d4670 ths
    } else {
87 223d4670 ths
        access_flags = GENERIC_READ;
88 223d4670 ths
    }
89 223d4670 ths
    if (flags & BDRV_O_CREAT) {
90 223d4670 ths
        create_flags = CREATE_ALWAYS;
91 223d4670 ths
    } else {
92 223d4670 ths
        create_flags = OPEN_EXISTING;
93 223d4670 ths
    }
94 03ff3ca3 aliguori
    overlapped = FILE_ATTRIBUTE_NORMAL;
95 9f7965c7 aliguori
    if ((flags & BDRV_O_NOCACHE))
96 33f00271 balrog
        overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
97 9f7965c7 aliguori
    else if (!(flags & BDRV_O_CACHE_WB))
98 9f7965c7 aliguori
        overlapped |= FILE_FLAG_WRITE_THROUGH;
99 223d4670 ths
    s->hfile = CreateFile(filename, access_flags,
100 223d4670 ths
                          FILE_SHARE_READ, NULL,
101 223d4670 ths
                          create_flags, overlapped, NULL);
102 223d4670 ths
    if (s->hfile == INVALID_HANDLE_VALUE) {
103 223d4670 ths
        int err = GetLastError();
104 223d4670 ths
105 223d4670 ths
        if (err == ERROR_ACCESS_DENIED)
106 223d4670 ths
            return -EACCES;
107 223d4670 ths
        return -1;
108 223d4670 ths
    }
109 223d4670 ths
    return 0;
110 223d4670 ths
}
111 223d4670 ths
112 eda578e5 aliguori
static int raw_read(BlockDriverState *bs, int64_t sector_num,
113 eda578e5 aliguori
                    uint8_t *buf, int nb_sectors)
114 223d4670 ths
{
115 223d4670 ths
    BDRVRawState *s = bs->opaque;
116 223d4670 ths
    OVERLAPPED ov;
117 223d4670 ths
    DWORD ret_count;
118 223d4670 ths
    int ret;
119 eda578e5 aliguori
    int64_t offset = sector_num * 512;
120 eda578e5 aliguori
    int count = nb_sectors * 512;
121 223d4670 ths
122 223d4670 ths
    memset(&ov, 0, sizeof(ov));
123 223d4670 ths
    ov.Offset = offset;
124 223d4670 ths
    ov.OffsetHigh = offset >> 32;
125 223d4670 ths
    ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
126 9c39be47 aliguori
    if (!ret)
127 9c39be47 aliguori
        return ret_count;
128 537a1d4b aliguori
    if (ret_count == count)
129 537a1d4b aliguori
        ret_count = 0;
130 223d4670 ths
    return ret_count;
131 223d4670 ths
}
132 223d4670 ths
133 eda578e5 aliguori
static int raw_write(BlockDriverState *bs, int64_t sector_num,
134 eda578e5 aliguori
                     const uint8_t *buf, int nb_sectors)
135 223d4670 ths
{
136 223d4670 ths
    BDRVRawState *s = bs->opaque;
137 223d4670 ths
    OVERLAPPED ov;
138 223d4670 ths
    DWORD ret_count;
139 223d4670 ths
    int ret;
140 eda578e5 aliguori
    int64_t offset = sector_num * 512;
141 eda578e5 aliguori
    int count = nb_sectors * 512;
142 223d4670 ths
143 223d4670 ths
    memset(&ov, 0, sizeof(ov));
144 223d4670 ths
    ov.Offset = offset;
145 223d4670 ths
    ov.OffsetHigh = offset >> 32;
146 223d4670 ths
    ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
147 9c39be47 aliguori
    if (!ret)
148 9c39be47 aliguori
        return ret_count;
149 537a1d4b aliguori
    if (ret_count == count)
150 537a1d4b aliguori
        ret_count = 0;
151 223d4670 ths
    return ret_count;
152 223d4670 ths
}
153 223d4670 ths
154 223d4670 ths
static void raw_flush(BlockDriverState *bs)
155 223d4670 ths
{
156 223d4670 ths
    BDRVRawState *s = bs->opaque;
157 223d4670 ths
    FlushFileBuffers(s->hfile);
158 223d4670 ths
}
159 223d4670 ths
160 223d4670 ths
static void raw_close(BlockDriverState *bs)
161 223d4670 ths
{
162 223d4670 ths
    BDRVRawState *s = bs->opaque;
163 223d4670 ths
    CloseHandle(s->hfile);
164 223d4670 ths
}
165 223d4670 ths
166 223d4670 ths
static int raw_truncate(BlockDriverState *bs, int64_t offset)
167 223d4670 ths
{
168 223d4670 ths
    BDRVRawState *s = bs->opaque;
169 b9e82a59 blueswir1
    LONG low, high;
170 223d4670 ths
171 223d4670 ths
    low = offset;
172 223d4670 ths
    high = offset >> 32;
173 223d4670 ths
    if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
174 223d4670 ths
        return -EIO;
175 223d4670 ths
    if (!SetEndOfFile(s->hfile))
176 223d4670 ths
        return -EIO;
177 223d4670 ths
    return 0;
178 223d4670 ths
}
179 223d4670 ths
180 223d4670 ths
static int64_t raw_getlength(BlockDriverState *bs)
181 223d4670 ths
{
182 223d4670 ths
    BDRVRawState *s = bs->opaque;
183 223d4670 ths
    LARGE_INTEGER l;
184 223d4670 ths
    ULARGE_INTEGER available, total, total_free;
185 223d4670 ths
    DISK_GEOMETRY_EX dg;
186 223d4670 ths
    DWORD count;
187 223d4670 ths
    BOOL status;
188 223d4670 ths
189 223d4670 ths
    switch(s->type) {
190 223d4670 ths
    case FTYPE_FILE:
191 b9e82a59 blueswir1
        l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
192 223d4670 ths
        if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
193 223d4670 ths
            return -EIO;
194 223d4670 ths
        break;
195 223d4670 ths
    case FTYPE_CD:
196 223d4670 ths
        if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
197 223d4670 ths
            return -EIO;
198 223d4670 ths
        l.QuadPart = total.QuadPart;
199 223d4670 ths
        break;
200 223d4670 ths
    case FTYPE_HARDDISK:
201 223d4670 ths
        status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
202 223d4670 ths
                                 NULL, 0, &dg, sizeof(dg), &count, NULL);
203 223d4670 ths
        if (status != 0) {
204 223d4670 ths
            l = dg.DiskSize;
205 223d4670 ths
        }
206 223d4670 ths
        break;
207 223d4670 ths
    default:
208 223d4670 ths
        return -EIO;
209 223d4670 ths
    }
210 223d4670 ths
    return l.QuadPart;
211 223d4670 ths
}
212 223d4670 ths
213 223d4670 ths
static int raw_create(const char *filename, int64_t total_size,
214 223d4670 ths
                      const char *backing_file, int flags)
215 223d4670 ths
{
216 223d4670 ths
    int fd;
217 223d4670 ths
218 223d4670 ths
    if (flags || backing_file)
219 223d4670 ths
        return -ENOTSUP;
220 223d4670 ths
221 223d4670 ths
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
222 223d4670 ths
              0644);
223 223d4670 ths
    if (fd < 0)
224 223d4670 ths
        return -EIO;
225 223d4670 ths
    set_sparse(fd);
226 223d4670 ths
    ftruncate(fd, total_size * 512);
227 223d4670 ths
    close(fd);
228 223d4670 ths
    return 0;
229 223d4670 ths
}
230 223d4670 ths
231 223d4670 ths
BlockDriver bdrv_raw = {
232 f1b2f712 aliguori
    .format_name        = "raw",
233 f1b2f712 aliguori
    .instance_size        = sizeof(BDRVRawState),
234 f1b2f712 aliguori
    .bdrv_open                = raw_open,
235 f1b2f712 aliguori
    .bdrv_close                = raw_close,
236 f1b2f712 aliguori
    .bdrv_create        = raw_create,
237 f1b2f712 aliguori
    .bdrv_flush                = raw_flush,
238 f1b2f712 aliguori
    .bdrv_read                = raw_read,
239 f1b2f712 aliguori
    .bdrv_write                = raw_write,
240 f1b2f712 aliguori
    .bdrv_truncate        = raw_truncate,
241 f1b2f712 aliguori
    .bdrv_getlength        = raw_getlength,
242 223d4670 ths
};
243 223d4670 ths
244 223d4670 ths
/***********************************************/
245 223d4670 ths
/* host device */
246 223d4670 ths
247 223d4670 ths
static int find_cdrom(char *cdrom_name, int cdrom_name_size)
248 223d4670 ths
{
249 223d4670 ths
    char drives[256], *pdrv = drives;
250 223d4670 ths
    UINT type;
251 223d4670 ths
252 223d4670 ths
    memset(drives, 0, sizeof(drives));
253 223d4670 ths
    GetLogicalDriveStrings(sizeof(drives), drives);
254 223d4670 ths
    while(pdrv[0] != '\0') {
255 223d4670 ths
        type = GetDriveType(pdrv);
256 223d4670 ths
        switch(type) {
257 223d4670 ths
        case DRIVE_CDROM:
258 223d4670 ths
            snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
259 223d4670 ths
            return 0;
260 223d4670 ths
            break;
261 223d4670 ths
        }
262 223d4670 ths
        pdrv += lstrlen(pdrv) + 1;
263 223d4670 ths
    }
264 223d4670 ths
    return -1;
265 223d4670 ths
}
266 223d4670 ths
267 223d4670 ths
static int find_device_type(BlockDriverState *bs, const char *filename)
268 223d4670 ths
{
269 223d4670 ths
    BDRVRawState *s = bs->opaque;
270 223d4670 ths
    UINT type;
271 223d4670 ths
    const char *p;
272 223d4670 ths
273 223d4670 ths
    if (strstart(filename, "\\\\.\\", &p) ||
274 223d4670 ths
        strstart(filename, "//./", &p)) {
275 223d4670 ths
        if (stristart(p, "PhysicalDrive", NULL))
276 223d4670 ths
            return FTYPE_HARDDISK;
277 223d4670 ths
        snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
278 223d4670 ths
        type = GetDriveType(s->drive_path);
279 3060cd14 aliguori
        switch (type) {
280 3060cd14 aliguori
        case DRIVE_REMOVABLE:
281 3060cd14 aliguori
        case DRIVE_FIXED:
282 3060cd14 aliguori
            return FTYPE_HARDDISK;
283 3060cd14 aliguori
        case DRIVE_CDROM:
284 223d4670 ths
            return FTYPE_CD;
285 3060cd14 aliguori
        default:
286 223d4670 ths
            return FTYPE_FILE;
287 3060cd14 aliguori
        }
288 223d4670 ths
    } else {
289 223d4670 ths
        return FTYPE_FILE;
290 223d4670 ths
    }
291 223d4670 ths
}
292 223d4670 ths
293 223d4670 ths
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
294 223d4670 ths
{
295 223d4670 ths
    BDRVRawState *s = bs->opaque;
296 223d4670 ths
    int access_flags, create_flags;
297 223d4670 ths
    DWORD overlapped;
298 223d4670 ths
    char device_name[64];
299 223d4670 ths
300 223d4670 ths
    if (strstart(filename, "/dev/cdrom", NULL)) {
301 223d4670 ths
        if (find_cdrom(device_name, sizeof(device_name)) < 0)
302 223d4670 ths
            return -ENOENT;
303 223d4670 ths
        filename = device_name;
304 223d4670 ths
    } else {
305 223d4670 ths
        /* transform drive letters into device name */
306 223d4670 ths
        if (((filename[0] >= 'a' && filename[0] <= 'z') ||
307 223d4670 ths
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
308 223d4670 ths
            filename[1] == ':' && filename[2] == '\0') {
309 223d4670 ths
            snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
310 223d4670 ths
            filename = device_name;
311 223d4670 ths
        }
312 223d4670 ths
    }
313 223d4670 ths
    s->type = find_device_type(bs, filename);
314 223d4670 ths
315 223d4670 ths
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
316 223d4670 ths
        access_flags = GENERIC_READ | GENERIC_WRITE;
317 223d4670 ths
    } else {
318 223d4670 ths
        access_flags = GENERIC_READ;
319 223d4670 ths
    }
320 223d4670 ths
    create_flags = OPEN_EXISTING;
321 223d4670 ths
322 03ff3ca3 aliguori
    overlapped = FILE_ATTRIBUTE_NORMAL;
323 9f7965c7 aliguori
    if ((flags & BDRV_O_NOCACHE))
324 33f00271 balrog
        overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
325 9f7965c7 aliguori
    else if (!(flags & BDRV_O_CACHE_WB))
326 9f7965c7 aliguori
        overlapped |= FILE_FLAG_WRITE_THROUGH;
327 223d4670 ths
    s->hfile = CreateFile(filename, access_flags,
328 223d4670 ths
                          FILE_SHARE_READ, NULL,
329 223d4670 ths
                          create_flags, overlapped, NULL);
330 223d4670 ths
    if (s->hfile == INVALID_HANDLE_VALUE) {
331 223d4670 ths
        int err = GetLastError();
332 223d4670 ths
333 223d4670 ths
        if (err == ERROR_ACCESS_DENIED)
334 223d4670 ths
            return -EACCES;
335 223d4670 ths
        return -1;
336 223d4670 ths
    }
337 223d4670 ths
    return 0;
338 223d4670 ths
}
339 223d4670 ths
340 223d4670 ths
#if 0
341 223d4670 ths
/***********************************************/
342 223d4670 ths
/* removable device additional commands */
343 223d4670 ths

344 223d4670 ths
static int raw_is_inserted(BlockDriverState *bs)
345 223d4670 ths
{
346 223d4670 ths
    return 1;
347 223d4670 ths
}
348 223d4670 ths

349 223d4670 ths
static int raw_media_changed(BlockDriverState *bs)
350 223d4670 ths
{
351 223d4670 ths
    return -ENOTSUP;
352 223d4670 ths
}
353 223d4670 ths

354 223d4670 ths
static int raw_eject(BlockDriverState *bs, int eject_flag)
355 223d4670 ths
{
356 223d4670 ths
    DWORD ret_count;
357 223d4670 ths

358 223d4670 ths
    if (s->type == FTYPE_FILE)
359 223d4670 ths
        return -ENOTSUP;
360 223d4670 ths
    if (eject_flag) {
361 223d4670 ths
        DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
362 223d4670 ths
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
363 223d4670 ths
    } else {
364 223d4670 ths
        DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
365 223d4670 ths
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
366 223d4670 ths
    }
367 223d4670 ths
}
368 223d4670 ths

369 223d4670 ths
static int raw_set_locked(BlockDriverState *bs, int locked)
370 223d4670 ths
{
371 223d4670 ths
    return -ENOTSUP;
372 223d4670 ths
}
373 223d4670 ths
#endif
374 223d4670 ths
375 223d4670 ths
BlockDriver bdrv_host_device = {
376 e60f469c aurel32
    .format_name        = "host_device",
377 e60f469c aurel32
    .instance_size        = sizeof(BDRVRawState),
378 e60f469c aurel32
    .bdrv_open                = hdev_open,
379 e60f469c aurel32
    .bdrv_close                = raw_close,
380 e60f469c aurel32
    .bdrv_flush                = raw_flush,
381 223d4670 ths
382 eda578e5 aliguori
    .bdrv_read                = raw_read,
383 eda578e5 aliguori
    .bdrv_write                = raw_write,
384 e60f469c aurel32
    .bdrv_getlength        = raw_getlength,
385 223d4670 ths
};