Statistics
| Branch: | Revision:

root / block-raw.c @ 8f40c388

History | View | Annotate | Download (31.8 kB)

1 83f64091 bellard
/*
2 83f64091 bellard
 * Block driver for RAW files
3 83f64091 bellard
 * 
4 83f64091 bellard
 * Copyright (c) 2006 Fabrice Bellard
5 83f64091 bellard
 * 
6 83f64091 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 83f64091 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 83f64091 bellard
 * in the Software without restriction, including without limitation the rights
9 83f64091 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 83f64091 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 83f64091 bellard
 * furnished to do so, subject to the following conditions:
12 83f64091 bellard
 *
13 83f64091 bellard
 * The above copyright notice and this permission notice shall be included in
14 83f64091 bellard
 * all copies or substantial portions of the Software.
15 83f64091 bellard
 *
16 83f64091 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 83f64091 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 83f64091 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 83f64091 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 83f64091 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 83f64091 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 83f64091 bellard
 * THE SOFTWARE.
23 83f64091 bellard
 */
24 83f64091 bellard
#include "vl.h"
25 83f64091 bellard
#include "block_int.h"
26 83f64091 bellard
#include <assert.h>
27 83f64091 bellard
#ifndef _WIN32
28 83f64091 bellard
#include <aio.h>
29 83f64091 bellard
30 83f64091 bellard
#ifndef QEMU_TOOL
31 83f64091 bellard
#include "exec-all.h"
32 83f64091 bellard
#endif
33 83f64091 bellard
34 83f64091 bellard
#ifdef CONFIG_COCOA
35 83f64091 bellard
#include <paths.h>
36 83f64091 bellard
#include <sys/param.h>
37 83f64091 bellard
#include <IOKit/IOKitLib.h>
38 83f64091 bellard
#include <IOKit/IOBSD.h>
39 83f64091 bellard
#include <IOKit/storage/IOMediaBSDClient.h>
40 83f64091 bellard
#include <IOKit/storage/IOMedia.h>
41 83f64091 bellard
#include <IOKit/storage/IOCDMedia.h>
42 83f64091 bellard
//#include <IOKit/storage/IOCDTypes.h>
43 83f64091 bellard
#include <CoreFoundation/CoreFoundation.h>
44 83f64091 bellard
#endif
45 83f64091 bellard
46 83f64091 bellard
#ifdef __sun__
47 83f64091 bellard
#include <sys/dkio.h>
48 83f64091 bellard
#endif
49 19cb3738 bellard
#ifdef __linux__
50 19cb3738 bellard
#include <sys/ioctl.h>
51 19cb3738 bellard
#include <linux/cdrom.h>
52 19cb3738 bellard
#include <linux/fd.h>
53 19cb3738 bellard
#endif
54 83f64091 bellard
55 19cb3738 bellard
//#define DEBUG_FLOPPY
56 83f64091 bellard
57 19cb3738 bellard
#define FTYPE_FILE   0
58 19cb3738 bellard
#define FTYPE_CD     1
59 19cb3738 bellard
#define FTYPE_FD     2
60 83f64091 bellard
61 19cb3738 bellard
/* if the FD is not accessed during that time (in ms), we try to
62 19cb3738 bellard
   reopen it to see if the disk has been changed */
63 19cb3738 bellard
#define FD_OPEN_TIMEOUT 1000
64 83f64091 bellard
65 19cb3738 bellard
typedef struct BDRVRawState {
66 19cb3738 bellard
    int fd;
67 19cb3738 bellard
    int type;
68 19cb3738 bellard
#if defined(__linux__)
69 19cb3738 bellard
    /* linux floppy specific */
70 19cb3738 bellard
    int fd_open_flags;
71 19cb3738 bellard
    int64_t fd_open_time;
72 19cb3738 bellard
    int64_t fd_error_time;
73 19cb3738 bellard
    int fd_got_error;
74 19cb3738 bellard
    int fd_media_changed;
75 83f64091 bellard
#endif
76 19cb3738 bellard
} BDRVRawState;
77 19cb3738 bellard
78 19cb3738 bellard
static int fd_open(BlockDriverState *bs);
79 83f64091 bellard
80 83f64091 bellard
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
81 83f64091 bellard
{
82 83f64091 bellard
    BDRVRawState *s = bs->opaque;
83 19cb3738 bellard
    int fd, open_flags, ret;
84 83f64091 bellard
85 83f64091 bellard
    open_flags = O_BINARY;
86 83f64091 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
87 83f64091 bellard
        open_flags |= O_RDWR;
88 83f64091 bellard
    } else {
89 83f64091 bellard
        open_flags |= O_RDONLY;
90 83f64091 bellard
        bs->read_only = 1;
91 83f64091 bellard
    }
92 83f64091 bellard
    if (flags & BDRV_O_CREAT)
93 83f64091 bellard
        open_flags |= O_CREAT | O_TRUNC;
94 83f64091 bellard
95 19cb3738 bellard
    s->type = FTYPE_FILE;
96 19cb3738 bellard
97 83f64091 bellard
    fd = open(filename, open_flags, 0644);
98 19cb3738 bellard
    if (fd < 0) {
99 19cb3738 bellard
        ret = -errno;
100 19cb3738 bellard
        if (ret == -EROFS)
101 19cb3738 bellard
            ret = -EACCES;
102 19cb3738 bellard
        return ret;
103 19cb3738 bellard
    }
104 83f64091 bellard
    s->fd = fd;
105 83f64091 bellard
    return 0;
106 83f64091 bellard
}
107 83f64091 bellard
108 83f64091 bellard
/* XXX: use host sector size if necessary with:
109 83f64091 bellard
#ifdef DIOCGSECTORSIZE
110 83f64091 bellard
        {
111 83f64091 bellard
            unsigned int sectorsize = 512;
112 83f64091 bellard
            if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
113 83f64091 bellard
                sectorsize > bufsize)
114 83f64091 bellard
                bufsize = sectorsize;
115 83f64091 bellard
        }
116 83f64091 bellard
#endif
117 83f64091 bellard
#ifdef CONFIG_COCOA
118 83f64091 bellard
        u_int32_t   blockSize = 512;
119 83f64091 bellard
        if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
120 83f64091 bellard
            bufsize = blockSize;
121 83f64091 bellard
        }
122 83f64091 bellard
#endif
123 83f64091 bellard
*/
124 83f64091 bellard
125 83f64091 bellard
static int raw_pread(BlockDriverState *bs, int64_t offset, 
126 83f64091 bellard
                     uint8_t *buf, int count)
127 83f64091 bellard
{
128 83f64091 bellard
    BDRVRawState *s = bs->opaque;
129 83f64091 bellard
    int ret;
130 83f64091 bellard
    
131 19cb3738 bellard
    ret = fd_open(bs);
132 19cb3738 bellard
    if (ret < 0)
133 19cb3738 bellard
        return ret;
134 19cb3738 bellard
135 83f64091 bellard
    lseek(s->fd, offset, SEEK_SET);
136 83f64091 bellard
    ret = read(s->fd, buf, count);
137 83f64091 bellard
    return ret;
138 83f64091 bellard
}
139 83f64091 bellard
140 83f64091 bellard
static int raw_pwrite(BlockDriverState *bs, int64_t offset, 
141 83f64091 bellard
                      const uint8_t *buf, int count)
142 83f64091 bellard
{
143 83f64091 bellard
    BDRVRawState *s = bs->opaque;
144 83f64091 bellard
    int ret;
145 83f64091 bellard
    
146 19cb3738 bellard
    ret = fd_open(bs);
147 19cb3738 bellard
    if (ret < 0)
148 19cb3738 bellard
        return ret;
149 19cb3738 bellard
150 83f64091 bellard
    lseek(s->fd, offset, SEEK_SET);
151 83f64091 bellard
    ret = write(s->fd, buf, count);
152 83f64091 bellard
    return ret;
153 83f64091 bellard
}
154 83f64091 bellard
155 83f64091 bellard
/***********************************************************/
156 19cb3738 bellard
/* Unix AIO using POSIX AIO */
157 83f64091 bellard
158 83f64091 bellard
typedef struct RawAIOCB {
159 ce1a14dc pbrook
    BlockDriverAIOCB common;
160 83f64091 bellard
    struct aiocb aiocb;
161 ce1a14dc pbrook
    struct RawAIOCB *next;
162 83f64091 bellard
} RawAIOCB;
163 83f64091 bellard
164 83f64091 bellard
static int aio_sig_num = SIGUSR2;
165 ce1a14dc pbrook
static RawAIOCB *first_aio; /* AIO issued */
166 979b67ad bellard
static int aio_initialized = 0;
167 83f64091 bellard
168 83f64091 bellard
static void aio_signal_handler(int signum)
169 83f64091 bellard
{
170 979b67ad bellard
#ifndef QEMU_TOOL
171 83f64091 bellard
    CPUState *env = cpu_single_env;
172 83f64091 bellard
    if (env) {
173 83f64091 bellard
        /* stop the currently executing cpu because a timer occured */
174 83f64091 bellard
        cpu_interrupt(env, CPU_INTERRUPT_EXIT);
175 83f64091 bellard
#ifdef USE_KQEMU
176 83f64091 bellard
        if (env->kqemu_enabled) {
177 83f64091 bellard
            kqemu_cpu_interrupt(env);
178 83f64091 bellard
        }
179 83f64091 bellard
#endif
180 83f64091 bellard
    }
181 979b67ad bellard
#endif
182 83f64091 bellard
}
183 83f64091 bellard
184 83f64091 bellard
void qemu_aio_init(void)
185 83f64091 bellard
{
186 83f64091 bellard
    struct sigaction act;
187 979b67ad bellard
188 979b67ad bellard
    aio_initialized = 1;
189 83f64091 bellard
    
190 83f64091 bellard
    sigfillset(&act.sa_mask);
191 83f64091 bellard
    act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
192 83f64091 bellard
    act.sa_handler = aio_signal_handler;
193 83f64091 bellard
    sigaction(aio_sig_num, &act, NULL);
194 83f64091 bellard
195 19cb3738 bellard
#if defined(__GLIBC__) && defined(__linux__)
196 83f64091 bellard
    {
197 19cb3738 bellard
        /* XXX: aio thread exit seems to hang on RedHat 9 and this init
198 19cb3738 bellard
           seems to fix the problem. */
199 83f64091 bellard
        struct aioinit ai;
200 83f64091 bellard
        memset(&ai, 0, sizeof(ai));
201 19cb3738 bellard
        ai.aio_threads = 1;
202 83f64091 bellard
        ai.aio_num = 1;
203 83f64091 bellard
        ai.aio_idle_time = 365 * 100000;
204 83f64091 bellard
        aio_init(&ai);
205 83f64091 bellard
    }
206 19cb3738 bellard
#endif
207 83f64091 bellard
}
208 83f64091 bellard
209 83f64091 bellard
void qemu_aio_poll(void)
210 83f64091 bellard
{
211 ce1a14dc pbrook
    RawAIOCB *acb, **pacb;
212 83f64091 bellard
    int ret;
213 83f64091 bellard
214 83f64091 bellard
    for(;;) {
215 83f64091 bellard
        pacb = &first_aio;
216 83f64091 bellard
        for(;;) {
217 83f64091 bellard
            acb = *pacb;
218 83f64091 bellard
            if (!acb)
219 83f64091 bellard
                goto the_end;
220 ce1a14dc pbrook
            ret = aio_error(&acb->aiocb);
221 83f64091 bellard
            if (ret == ECANCELED) {
222 83f64091 bellard
                /* remove the request */
223 ce1a14dc pbrook
                *pacb = acb->next;
224 ce1a14dc pbrook
                qemu_aio_release(acb);
225 83f64091 bellard
            } else if (ret != EINPROGRESS) {
226 83f64091 bellard
                /* end of aio */
227 83f64091 bellard
                if (ret == 0) {
228 ce1a14dc pbrook
                    ret = aio_return(&acb->aiocb);
229 ce1a14dc pbrook
                    if (ret == acb->aiocb.aio_nbytes)
230 83f64091 bellard
                        ret = 0;
231 83f64091 bellard
                    else
232 19cb3738 bellard
                        ret = -EINVAL;
233 83f64091 bellard
                } else {
234 83f64091 bellard
                    ret = -ret;
235 83f64091 bellard
                }
236 83f64091 bellard
                /* remove the request */
237 ce1a14dc pbrook
                *pacb = acb->next;
238 83f64091 bellard
                /* call the callback */
239 ce1a14dc pbrook
                acb->common.cb(acb->common.opaque, ret);
240 ce1a14dc pbrook
                qemu_aio_release(acb);
241 83f64091 bellard
                break;
242 83f64091 bellard
            } else {
243 ce1a14dc pbrook
                pacb = &acb->next;
244 83f64091 bellard
            }
245 83f64091 bellard
        }
246 83f64091 bellard
    }
247 83f64091 bellard
 the_end: ;
248 83f64091 bellard
}
249 83f64091 bellard
250 6192bc37 pbrook
/* Wait for all IO requests to complete.  */
251 6192bc37 pbrook
void qemu_aio_flush(void)
252 6192bc37 pbrook
{
253 6192bc37 pbrook
    qemu_aio_wait_start();
254 6192bc37 pbrook
    qemu_aio_poll();
255 6192bc37 pbrook
    while (first_aio) {
256 6192bc37 pbrook
        qemu_aio_wait();
257 6192bc37 pbrook
    }
258 6192bc37 pbrook
    qemu_aio_wait_end();
259 6192bc37 pbrook
}
260 6192bc37 pbrook
261 83f64091 bellard
/* wait until at least one AIO was handled */
262 83f64091 bellard
static sigset_t wait_oset;
263 83f64091 bellard
264 83f64091 bellard
void qemu_aio_wait_start(void)
265 83f64091 bellard
{
266 83f64091 bellard
    sigset_t set;
267 979b67ad bellard
268 979b67ad bellard
    if (!aio_initialized)
269 979b67ad bellard
        qemu_aio_init();
270 83f64091 bellard
    sigemptyset(&set);
271 83f64091 bellard
    sigaddset(&set, aio_sig_num);
272 83f64091 bellard
    sigprocmask(SIG_BLOCK, &set, &wait_oset);
273 83f64091 bellard
}
274 83f64091 bellard
275 83f64091 bellard
void qemu_aio_wait(void)
276 83f64091 bellard
{
277 83f64091 bellard
    sigset_t set;
278 83f64091 bellard
    int nb_sigs;
279 6eb5733a bellard
280 6eb5733a bellard
#ifndef QEMU_TOOL
281 6eb5733a bellard
    if (qemu_bh_poll())
282 6eb5733a bellard
        return;
283 6eb5733a bellard
#endif
284 83f64091 bellard
    sigemptyset(&set);
285 83f64091 bellard
    sigaddset(&set, aio_sig_num);
286 83f64091 bellard
    sigwait(&set, &nb_sigs);
287 83f64091 bellard
    qemu_aio_poll();
288 83f64091 bellard
}
289 83f64091 bellard
290 83f64091 bellard
void qemu_aio_wait_end(void)
291 83f64091 bellard
{
292 83f64091 bellard
    sigprocmask(SIG_SETMASK, &wait_oset, NULL);
293 83f64091 bellard
}
294 83f64091 bellard
295 ce1a14dc pbrook
static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
296 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
297 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
298 83f64091 bellard
{
299 ce1a14dc pbrook
    BDRVRawState *s = bs->opaque;
300 ce1a14dc pbrook
    RawAIOCB *acb;
301 ce1a14dc pbrook
302 19cb3738 bellard
    if (fd_open(bs) < 0)
303 19cb3738 bellard
        return NULL;
304 19cb3738 bellard
305 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
306 ce1a14dc pbrook
    if (!acb)
307 ce1a14dc pbrook
        return NULL;
308 ce1a14dc pbrook
    acb->aiocb.aio_fildes = s->fd;
309 ce1a14dc pbrook
    acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
310 ce1a14dc pbrook
    acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
311 ce1a14dc pbrook
    acb->aiocb.aio_buf = buf;
312 ce1a14dc pbrook
    acb->aiocb.aio_nbytes = nb_sectors * 512;
313 ce1a14dc pbrook
    acb->aiocb.aio_offset = sector_num * 512;
314 ce1a14dc pbrook
    acb->next = first_aio;
315 ce1a14dc pbrook
    first_aio = acb;
316 ce1a14dc pbrook
    return acb;
317 83f64091 bellard
}
318 83f64091 bellard
319 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
320 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
321 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
322 83f64091 bellard
{
323 ce1a14dc pbrook
    RawAIOCB *acb;
324 83f64091 bellard
325 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
326 ce1a14dc pbrook
    if (!acb)
327 ce1a14dc pbrook
        return NULL;
328 ce1a14dc pbrook
    if (aio_read(&acb->aiocb) < 0) {
329 ce1a14dc pbrook
        qemu_aio_release(acb);
330 ce1a14dc pbrook
        return NULL;
331 83f64091 bellard
    } 
332 ce1a14dc pbrook
    return &acb->common;
333 83f64091 bellard
}
334 83f64091 bellard
335 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
336 ce1a14dc pbrook
        int64_t sector_num, const uint8_t *buf, int nb_sectors,
337 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
338 83f64091 bellard
{
339 ce1a14dc pbrook
    RawAIOCB *acb;
340 83f64091 bellard
341 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
342 ce1a14dc pbrook
    if (!acb)
343 ce1a14dc pbrook
        return NULL;
344 ce1a14dc pbrook
    if (aio_write(&acb->aiocb) < 0) {
345 ce1a14dc pbrook
        qemu_aio_release(acb);
346 ce1a14dc pbrook
        return NULL;
347 83f64091 bellard
    } 
348 ce1a14dc pbrook
    return &acb->common;
349 83f64091 bellard
}
350 83f64091 bellard
351 ce1a14dc pbrook
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
352 83f64091 bellard
{
353 83f64091 bellard
    int ret;
354 ce1a14dc pbrook
    RawAIOCB *acb = (RawAIOCB *)blockacb;
355 ce1a14dc pbrook
    RawAIOCB **pacb;
356 83f64091 bellard
357 ce1a14dc pbrook
    ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
358 83f64091 bellard
    if (ret == AIO_NOTCANCELED) {
359 83f64091 bellard
        /* fail safe: if the aio could not be canceled, we wait for
360 83f64091 bellard
           it */
361 ce1a14dc pbrook
        while (aio_error(&acb->aiocb) == EINPROGRESS);
362 83f64091 bellard
    }
363 83f64091 bellard
364 83f64091 bellard
    /* remove the callback from the queue */
365 83f64091 bellard
    pacb = &first_aio;
366 83f64091 bellard
    for(;;) {
367 83f64091 bellard
        if (*pacb == NULL) {
368 83f64091 bellard
            break;
369 83f64091 bellard
        } else if (*pacb == acb) {
370 ce1a14dc pbrook
            *pacb = acb->next;
371 ce1a14dc pbrook
            qemu_aio_release(acb);
372 83f64091 bellard
            break;
373 83f64091 bellard
        }
374 ce1a14dc pbrook
        pacb = &acb->next;
375 83f64091 bellard
    }
376 83f64091 bellard
}
377 83f64091 bellard
378 83f64091 bellard
static void raw_close(BlockDriverState *bs)
379 83f64091 bellard
{
380 83f64091 bellard
    BDRVRawState *s = bs->opaque;
381 19cb3738 bellard
    if (s->fd >= 0) {
382 19cb3738 bellard
        close(s->fd);
383 19cb3738 bellard
        s->fd = -1;
384 19cb3738 bellard
    }
385 83f64091 bellard
}
386 83f64091 bellard
387 83f64091 bellard
static int raw_truncate(BlockDriverState *bs, int64_t offset)
388 83f64091 bellard
{
389 83f64091 bellard
    BDRVRawState *s = bs->opaque;
390 19cb3738 bellard
    if (s->type != FTYPE_FILE)
391 19cb3738 bellard
        return -ENOTSUP;
392 83f64091 bellard
    if (ftruncate(s->fd, offset) < 0)
393 83f64091 bellard
        return -errno;
394 83f64091 bellard
    return 0;
395 83f64091 bellard
}
396 83f64091 bellard
397 83f64091 bellard
static int64_t  raw_getlength(BlockDriverState *bs)
398 83f64091 bellard
{
399 83f64091 bellard
    BDRVRawState *s = bs->opaque;
400 83f64091 bellard
    int fd = s->fd;
401 83f64091 bellard
    int64_t size;
402 83f64091 bellard
#ifdef _BSD
403 83f64091 bellard
    struct stat sb;
404 83f64091 bellard
#endif
405 83f64091 bellard
#ifdef __sun__
406 83f64091 bellard
    struct dk_minfo minfo;
407 83f64091 bellard
    int rv;
408 83f64091 bellard
#endif
409 19cb3738 bellard
    int ret;
410 19cb3738 bellard
411 19cb3738 bellard
    ret = fd_open(bs);
412 19cb3738 bellard
    if (ret < 0)
413 19cb3738 bellard
        return ret;
414 83f64091 bellard
415 83f64091 bellard
#ifdef _BSD
416 83f64091 bellard
    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
417 83f64091 bellard
#ifdef DIOCGMEDIASIZE
418 83f64091 bellard
        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
419 83f64091 bellard
#endif
420 83f64091 bellard
#ifdef CONFIG_COCOA
421 83f64091 bellard
        size = LONG_LONG_MAX;
422 83f64091 bellard
#else
423 83f64091 bellard
        size = lseek(fd, 0LL, SEEK_END);
424 83f64091 bellard
#endif
425 83f64091 bellard
    } else
426 83f64091 bellard
#endif
427 83f64091 bellard
#ifdef __sun__
428 83f64091 bellard
    /*
429 83f64091 bellard
     * use the DKIOCGMEDIAINFO ioctl to read the size.
430 83f64091 bellard
     */
431 83f64091 bellard
    rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
432 83f64091 bellard
    if ( rv != -1 ) {
433 83f64091 bellard
        size = minfo.dki_lbsize * minfo.dki_capacity;
434 83f64091 bellard
    } else /* there are reports that lseek on some devices
435 83f64091 bellard
              fails, but irc discussion said that contingency
436 83f64091 bellard
              on contingency was overkill */
437 83f64091 bellard
#endif
438 83f64091 bellard
    {
439 83f64091 bellard
        size = lseek(fd, 0, SEEK_END);
440 83f64091 bellard
    }
441 83f64091 bellard
    return size;
442 83f64091 bellard
}
443 83f64091 bellard
444 83f64091 bellard
static int raw_create(const char *filename, int64_t total_size,
445 83f64091 bellard
                      const char *backing_file, int flags)
446 83f64091 bellard
{
447 83f64091 bellard
    int fd;
448 83f64091 bellard
449 83f64091 bellard
    if (flags || backing_file)
450 83f64091 bellard
        return -ENOTSUP;
451 83f64091 bellard
452 9ab8a34a bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 
453 83f64091 bellard
              0644);
454 83f64091 bellard
    if (fd < 0)
455 83f64091 bellard
        return -EIO;
456 83f64091 bellard
    ftruncate(fd, total_size * 512);
457 83f64091 bellard
    close(fd);
458 83f64091 bellard
    return 0;
459 83f64091 bellard
}
460 83f64091 bellard
461 83f64091 bellard
static void raw_flush(BlockDriverState *bs)
462 83f64091 bellard
{
463 83f64091 bellard
    BDRVRawState *s = bs->opaque;
464 83f64091 bellard
    fsync(s->fd);
465 83f64091 bellard
}
466 83f64091 bellard
467 83f64091 bellard
BlockDriver bdrv_raw = {
468 83f64091 bellard
    "raw",
469 83f64091 bellard
    sizeof(BDRVRawState),
470 83f64091 bellard
    NULL, /* no probe for protocols */
471 83f64091 bellard
    raw_open,
472 83f64091 bellard
    NULL,
473 83f64091 bellard
    NULL,
474 83f64091 bellard
    raw_close,
475 83f64091 bellard
    raw_create,
476 83f64091 bellard
    raw_flush,
477 83f64091 bellard
    
478 83f64091 bellard
    .bdrv_aio_read = raw_aio_read,
479 83f64091 bellard
    .bdrv_aio_write = raw_aio_write,
480 83f64091 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
481 ce1a14dc pbrook
    .aiocb_size = sizeof(RawAIOCB),
482 83f64091 bellard
    .protocol_name = "file",
483 83f64091 bellard
    .bdrv_pread = raw_pread,
484 83f64091 bellard
    .bdrv_pwrite = raw_pwrite,
485 83f64091 bellard
    .bdrv_truncate = raw_truncate,
486 83f64091 bellard
    .bdrv_getlength = raw_getlength,
487 83f64091 bellard
};
488 83f64091 bellard
489 19cb3738 bellard
/***********************************************/
490 19cb3738 bellard
/* host device */
491 19cb3738 bellard
492 19cb3738 bellard
#ifdef CONFIG_COCOA
493 19cb3738 bellard
static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
494 19cb3738 bellard
static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
495 19cb3738 bellard
496 19cb3738 bellard
kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
497 19cb3738 bellard
{
498 19cb3738 bellard
    kern_return_t       kernResult; 
499 19cb3738 bellard
    mach_port_t     masterPort;
500 19cb3738 bellard
    CFMutableDictionaryRef  classesToMatch;
501 19cb3738 bellard
502 19cb3738 bellard
    kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
503 19cb3738 bellard
    if ( KERN_SUCCESS != kernResult ) {
504 19cb3738 bellard
        printf( "IOMasterPort returned %d\n", kernResult );
505 19cb3738 bellard
    }
506 19cb3738 bellard
    
507 19cb3738 bellard
    classesToMatch = IOServiceMatching( kIOCDMediaClass ); 
508 19cb3738 bellard
    if ( classesToMatch == NULL ) {
509 19cb3738 bellard
        printf( "IOServiceMatching returned a NULL dictionary.\n" );
510 19cb3738 bellard
    } else {
511 19cb3738 bellard
    CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
512 19cb3738 bellard
    }
513 19cb3738 bellard
    kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
514 19cb3738 bellard
    if ( KERN_SUCCESS != kernResult )
515 19cb3738 bellard
    {
516 19cb3738 bellard
        printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
517 19cb3738 bellard
    }
518 19cb3738 bellard
    
519 19cb3738 bellard
    return kernResult;
520 19cb3738 bellard
}
521 19cb3738 bellard
522 19cb3738 bellard
kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
523 19cb3738 bellard
{
524 19cb3738 bellard
    io_object_t     nextMedia;
525 19cb3738 bellard
    kern_return_t   kernResult = KERN_FAILURE;
526 19cb3738 bellard
    *bsdPath = '\0';
527 19cb3738 bellard
    nextMedia = IOIteratorNext( mediaIterator );
528 19cb3738 bellard
    if ( nextMedia )
529 19cb3738 bellard
    {
530 19cb3738 bellard
        CFTypeRef   bsdPathAsCFString;
531 19cb3738 bellard
    bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
532 19cb3738 bellard
        if ( bsdPathAsCFString ) {
533 19cb3738 bellard
            size_t devPathLength;
534 19cb3738 bellard
            strcpy( bsdPath, _PATH_DEV );
535 19cb3738 bellard
            strcat( bsdPath, "r" );
536 19cb3738 bellard
            devPathLength = strlen( bsdPath );
537 19cb3738 bellard
            if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
538 19cb3738 bellard
                kernResult = KERN_SUCCESS;
539 19cb3738 bellard
            }
540 19cb3738 bellard
            CFRelease( bsdPathAsCFString );
541 19cb3738 bellard
        }
542 19cb3738 bellard
        IOObjectRelease( nextMedia );
543 19cb3738 bellard
    }
544 19cb3738 bellard
    
545 19cb3738 bellard
    return kernResult;
546 19cb3738 bellard
}
547 19cb3738 bellard
548 19cb3738 bellard
#endif
549 19cb3738 bellard
550 19cb3738 bellard
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
551 19cb3738 bellard
{
552 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
553 19cb3738 bellard
    int fd, open_flags, ret;
554 19cb3738 bellard
555 19cb3738 bellard
#ifdef CONFIG_COCOA
556 19cb3738 bellard
    if (strstart(filename, "/dev/cdrom", NULL)) {
557 19cb3738 bellard
        kern_return_t kernResult;
558 19cb3738 bellard
        io_iterator_t mediaIterator;
559 19cb3738 bellard
        char bsdPath[ MAXPATHLEN ];
560 19cb3738 bellard
        int fd;
561 19cb3738 bellard
 
562 19cb3738 bellard
        kernResult = FindEjectableCDMedia( &mediaIterator );
563 19cb3738 bellard
        kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
564 19cb3738 bellard
    
565 19cb3738 bellard
        if ( bsdPath[ 0 ] != '\0' ) {
566 19cb3738 bellard
            strcat(bsdPath,"s0");
567 19cb3738 bellard
            /* some CDs don't have a partition 0 */
568 19cb3738 bellard
            fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
569 19cb3738 bellard
            if (fd < 0) {
570 19cb3738 bellard
                bsdPath[strlen(bsdPath)-1] = '1';
571 19cb3738 bellard
            } else {
572 19cb3738 bellard
                close(fd);
573 19cb3738 bellard
            }
574 19cb3738 bellard
            filename = bsdPath;
575 19cb3738 bellard
        }
576 19cb3738 bellard
        
577 19cb3738 bellard
        if ( mediaIterator )
578 19cb3738 bellard
            IOObjectRelease( mediaIterator );
579 19cb3738 bellard
    }
580 19cb3738 bellard
#endif
581 19cb3738 bellard
    open_flags = O_BINARY;
582 19cb3738 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
583 19cb3738 bellard
        open_flags |= O_RDWR;
584 19cb3738 bellard
    } else {
585 19cb3738 bellard
        open_flags |= O_RDONLY;
586 19cb3738 bellard
        bs->read_only = 1;
587 19cb3738 bellard
    }
588 19cb3738 bellard
589 19cb3738 bellard
    s->type = FTYPE_FILE;
590 19cb3738 bellard
#if defined(__linux__)
591 19cb3738 bellard
    if (strstart(filename, "/dev/cd", NULL)) {
592 19cb3738 bellard
        /* open will not fail even if no CD is inserted */
593 19cb3738 bellard
        open_flags |= O_NONBLOCK;
594 19cb3738 bellard
        s->type = FTYPE_CD;
595 19cb3738 bellard
    } else if (strstart(filename, "/dev/fd", NULL)) {
596 19cb3738 bellard
        s->type = FTYPE_FD;
597 19cb3738 bellard
        s->fd_open_flags = open_flags;
598 19cb3738 bellard
        /* open will not fail even if no floppy is inserted */
599 19cb3738 bellard
        open_flags |= O_NONBLOCK;
600 19cb3738 bellard
    }
601 19cb3738 bellard
#endif
602 19cb3738 bellard
    fd = open(filename, open_flags, 0644);
603 19cb3738 bellard
    if (fd < 0) {
604 19cb3738 bellard
        ret = -errno;
605 19cb3738 bellard
        if (ret == -EROFS)
606 19cb3738 bellard
            ret = -EACCES;
607 19cb3738 bellard
        return ret;
608 19cb3738 bellard
    }
609 19cb3738 bellard
    s->fd = fd;
610 19cb3738 bellard
#if defined(__linux__)
611 19cb3738 bellard
    /* close fd so that we can reopen it as needed */
612 19cb3738 bellard
    if (s->type == FTYPE_FD) {
613 19cb3738 bellard
        close(s->fd);
614 19cb3738 bellard
        s->fd = -1;
615 19cb3738 bellard
        s->fd_media_changed = 1;
616 19cb3738 bellard
    }
617 19cb3738 bellard
#endif
618 19cb3738 bellard
    return 0;
619 19cb3738 bellard
}
620 19cb3738 bellard
621 19cb3738 bellard
#if defined(__linux__) && !defined(QEMU_TOOL)
622 19cb3738 bellard
623 19cb3738 bellard
/* Note: we do not have a reliable method to detect if the floppy is
624 19cb3738 bellard
   present. The current method is to try to open the floppy at every
625 19cb3738 bellard
   I/O and to keep it opened during a few hundreds of ms. */
626 19cb3738 bellard
static int fd_open(BlockDriverState *bs)
627 19cb3738 bellard
{
628 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
629 19cb3738 bellard
    int last_media_present;
630 19cb3738 bellard
631 19cb3738 bellard
    if (s->type != FTYPE_FD)
632 19cb3738 bellard
        return 0;
633 19cb3738 bellard
    last_media_present = (s->fd >= 0);
634 19cb3738 bellard
    if (s->fd >= 0 && 
635 19cb3738 bellard
        (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
636 19cb3738 bellard
        close(s->fd);
637 19cb3738 bellard
        s->fd = -1;
638 19cb3738 bellard
#ifdef DEBUG_FLOPPY
639 19cb3738 bellard
        printf("Floppy closed\n");
640 19cb3738 bellard
#endif
641 19cb3738 bellard
    }
642 19cb3738 bellard
    if (s->fd < 0) {
643 19cb3738 bellard
        if (s->fd_got_error && 
644 19cb3738 bellard
            (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
645 19cb3738 bellard
#ifdef DEBUG_FLOPPY
646 19cb3738 bellard
            printf("No floppy (open delayed)\n");
647 19cb3738 bellard
#endif
648 19cb3738 bellard
            return -EIO;
649 19cb3738 bellard
        }
650 19cb3738 bellard
        s->fd = open(bs->filename, s->fd_open_flags);
651 19cb3738 bellard
        if (s->fd < 0) {
652 19cb3738 bellard
            s->fd_error_time = qemu_get_clock(rt_clock);
653 19cb3738 bellard
            s->fd_got_error = 1;
654 19cb3738 bellard
            if (last_media_present)
655 19cb3738 bellard
                s->fd_media_changed = 1;
656 19cb3738 bellard
#ifdef DEBUG_FLOPPY
657 19cb3738 bellard
            printf("No floppy\n");
658 19cb3738 bellard
#endif
659 19cb3738 bellard
            return -EIO;
660 19cb3738 bellard
        }
661 19cb3738 bellard
#ifdef DEBUG_FLOPPY
662 19cb3738 bellard
        printf("Floppy opened\n");
663 19cb3738 bellard
#endif
664 19cb3738 bellard
    }
665 19cb3738 bellard
    if (!last_media_present)
666 19cb3738 bellard
        s->fd_media_changed = 1;
667 19cb3738 bellard
    s->fd_open_time = qemu_get_clock(rt_clock);
668 19cb3738 bellard
    s->fd_got_error = 0;
669 19cb3738 bellard
    return 0;
670 19cb3738 bellard
}
671 19cb3738 bellard
#else
672 19cb3738 bellard
static int fd_open(BlockDriverState *bs)
673 19cb3738 bellard
{
674 19cb3738 bellard
    return 0;
675 19cb3738 bellard
}
676 19cb3738 bellard
#endif
677 19cb3738 bellard
678 19cb3738 bellard
#if defined(__linux__)
679 19cb3738 bellard
680 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
681 19cb3738 bellard
{
682 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
683 19cb3738 bellard
    int ret;
684 19cb3738 bellard
685 19cb3738 bellard
    switch(s->type) {
686 19cb3738 bellard
    case FTYPE_CD:
687 19cb3738 bellard
        ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
688 19cb3738 bellard
        if (ret == CDS_DISC_OK)
689 19cb3738 bellard
            return 1;
690 19cb3738 bellard
        else
691 19cb3738 bellard
            return 0;
692 19cb3738 bellard
        break;
693 19cb3738 bellard
    case FTYPE_FD:
694 19cb3738 bellard
        ret = fd_open(bs);
695 19cb3738 bellard
        return (ret >= 0);
696 19cb3738 bellard
    default:
697 19cb3738 bellard
        return 1;
698 19cb3738 bellard
    }
699 19cb3738 bellard
}
700 19cb3738 bellard
701 19cb3738 bellard
/* currently only used by fdc.c, but a CD version would be good too */
702 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
703 19cb3738 bellard
{
704 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
705 19cb3738 bellard
706 19cb3738 bellard
    switch(s->type) {
707 19cb3738 bellard
    case FTYPE_FD:
708 19cb3738 bellard
        {
709 19cb3738 bellard
            int ret;
710 19cb3738 bellard
            /* XXX: we do not have a true media changed indication. It
711 19cb3738 bellard
               does not work if the floppy is changed without trying
712 19cb3738 bellard
               to read it */
713 19cb3738 bellard
            fd_open(bs);
714 19cb3738 bellard
            ret = s->fd_media_changed;
715 19cb3738 bellard
            s->fd_media_changed = 0;
716 19cb3738 bellard
#ifdef DEBUG_FLOPPY
717 19cb3738 bellard
            printf("Floppy changed=%d\n", ret);
718 19cb3738 bellard
#endif
719 19cb3738 bellard
            return ret;
720 19cb3738 bellard
        }
721 19cb3738 bellard
    default:
722 19cb3738 bellard
        return -ENOTSUP;
723 19cb3738 bellard
    }
724 19cb3738 bellard
}
725 19cb3738 bellard
726 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
727 19cb3738 bellard
{
728 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
729 19cb3738 bellard
730 19cb3738 bellard
    switch(s->type) {
731 19cb3738 bellard
    case FTYPE_CD:
732 19cb3738 bellard
        if (eject_flag) {
733 19cb3738 bellard
            if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
734 19cb3738 bellard
                perror("CDROMEJECT");
735 19cb3738 bellard
        } else {
736 19cb3738 bellard
            if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
737 19cb3738 bellard
                perror("CDROMEJECT");
738 19cb3738 bellard
        }
739 19cb3738 bellard
        break;
740 19cb3738 bellard
    case FTYPE_FD:
741 19cb3738 bellard
        {
742 19cb3738 bellard
            int fd;
743 19cb3738 bellard
            if (s->fd >= 0) {
744 19cb3738 bellard
                close(s->fd);
745 19cb3738 bellard
                s->fd = -1;
746 19cb3738 bellard
            }
747 19cb3738 bellard
            fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
748 19cb3738 bellard
            if (fd >= 0) {
749 19cb3738 bellard
                if (ioctl(fd, FDEJECT, 0) < 0)
750 19cb3738 bellard
                    perror("FDEJECT");
751 19cb3738 bellard
                close(fd);
752 19cb3738 bellard
            }
753 19cb3738 bellard
        }
754 19cb3738 bellard
        break;
755 19cb3738 bellard
    default:
756 19cb3738 bellard
        return -ENOTSUP;
757 19cb3738 bellard
    }
758 19cb3738 bellard
    return 0;
759 19cb3738 bellard
}
760 19cb3738 bellard
761 19cb3738 bellard
static int raw_set_locked(BlockDriverState *bs, int locked)
762 19cb3738 bellard
{
763 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
764 19cb3738 bellard
765 19cb3738 bellard
    switch(s->type) {
766 19cb3738 bellard
    case FTYPE_CD:
767 19cb3738 bellard
        if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
768 19cb3738 bellard
            /* Note: an error can happen if the distribution automatically
769 19cb3738 bellard
               mounts the CD-ROM */
770 19cb3738 bellard
            //        perror("CDROM_LOCKDOOR");
771 19cb3738 bellard
        }
772 19cb3738 bellard
        break;
773 19cb3738 bellard
    default:
774 19cb3738 bellard
        return -ENOTSUP;
775 19cb3738 bellard
    }
776 19cb3738 bellard
    return 0;
777 19cb3738 bellard
}
778 19cb3738 bellard
779 19cb3738 bellard
#else
780 19cb3738 bellard
781 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
782 19cb3738 bellard
{
783 19cb3738 bellard
    return 1;
784 19cb3738 bellard
}
785 19cb3738 bellard
786 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
787 19cb3738 bellard
{
788 19cb3738 bellard
    return -ENOTSUP;
789 19cb3738 bellard
}
790 19cb3738 bellard
791 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
792 19cb3738 bellard
{
793 19cb3738 bellard
    return -ENOTSUP;
794 19cb3738 bellard
}
795 19cb3738 bellard
796 19cb3738 bellard
static int raw_set_locked(BlockDriverState *bs, int locked)
797 19cb3738 bellard
{
798 19cb3738 bellard
    return -ENOTSUP;
799 19cb3738 bellard
}
800 19cb3738 bellard
801 19cb3738 bellard
#endif /* !linux */
802 19cb3738 bellard
803 19cb3738 bellard
BlockDriver bdrv_host_device = {
804 19cb3738 bellard
    "host_device",
805 19cb3738 bellard
    sizeof(BDRVRawState),
806 19cb3738 bellard
    NULL, /* no probe for protocols */
807 19cb3738 bellard
    hdev_open,
808 19cb3738 bellard
    NULL,
809 19cb3738 bellard
    NULL,
810 19cb3738 bellard
    raw_close,
811 19cb3738 bellard
    NULL,
812 19cb3738 bellard
    raw_flush,
813 19cb3738 bellard
    
814 19cb3738 bellard
    .bdrv_aio_read = raw_aio_read,
815 19cb3738 bellard
    .bdrv_aio_write = raw_aio_write,
816 19cb3738 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
817 19cb3738 bellard
    .aiocb_size = sizeof(RawAIOCB),
818 19cb3738 bellard
    .bdrv_pread = raw_pread,
819 19cb3738 bellard
    .bdrv_pwrite = raw_pwrite,
820 19cb3738 bellard
    .bdrv_getlength = raw_getlength,
821 19cb3738 bellard
822 19cb3738 bellard
    /* removable device support */
823 19cb3738 bellard
    .bdrv_is_inserted = raw_is_inserted,
824 19cb3738 bellard
    .bdrv_media_changed = raw_media_changed,
825 19cb3738 bellard
    .bdrv_eject = raw_eject,
826 19cb3738 bellard
    .bdrv_set_locked = raw_set_locked,
827 19cb3738 bellard
};
828 19cb3738 bellard
829 83f64091 bellard
#else /* _WIN32 */
830 83f64091 bellard
831 83f64091 bellard
/* XXX: use another file ? */
832 83f64091 bellard
#include <winioctl.h>
833 83f64091 bellard
834 19cb3738 bellard
#define FTYPE_FILE 0
835 19cb3738 bellard
#define FTYPE_CD     1
836 19cb3738 bellard
837 83f64091 bellard
typedef struct BDRVRawState {
838 83f64091 bellard
    HANDLE hfile;
839 19cb3738 bellard
    int type;
840 f45512fe bellard
    char drive_path[16]; /* format: "d:\" */
841 83f64091 bellard
} BDRVRawState;
842 83f64091 bellard
843 83f64091 bellard
typedef struct RawAIOCB {
844 ce1a14dc pbrook
    BlockDriverAIOCB common;
845 83f64091 bellard
    HANDLE hEvent;
846 83f64091 bellard
    OVERLAPPED ov;
847 83f64091 bellard
    int count;
848 83f64091 bellard
} RawAIOCB;
849 83f64091 bellard
850 83f64091 bellard
int qemu_ftruncate64(int fd, int64_t length)
851 83f64091 bellard
{
852 83f64091 bellard
    LARGE_INTEGER li;
853 83f64091 bellard
    LONG high;
854 83f64091 bellard
    HANDLE h;
855 83f64091 bellard
    BOOL res;
856 83f64091 bellard
857 83f64091 bellard
    if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
858 83f64091 bellard
        return -1;
859 83f64091 bellard
860 83f64091 bellard
    h = (HANDLE)_get_osfhandle(fd);
861 83f64091 bellard
862 83f64091 bellard
    /* get current position, ftruncate do not change position */
863 83f64091 bellard
    li.HighPart = 0;
864 83f64091 bellard
    li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
865 83f64091 bellard
    if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
866 83f64091 bellard
        return -1;
867 83f64091 bellard
868 83f64091 bellard
    high = length >> 32;
869 83f64091 bellard
    if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
870 83f64091 bellard
        return -1;
871 83f64091 bellard
    res = SetEndOfFile(h);
872 83f64091 bellard
873 83f64091 bellard
    /* back to old position */
874 83f64091 bellard
    SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
875 83f64091 bellard
    return res ? 0 : -1;
876 83f64091 bellard
}
877 83f64091 bellard
878 83f64091 bellard
static int set_sparse(int fd)
879 83f64091 bellard
{
880 83f64091 bellard
    DWORD returned;
881 83f64091 bellard
    return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
882 83f64091 bellard
                                 NULL, 0, NULL, 0, &returned, NULL);
883 83f64091 bellard
}
884 83f64091 bellard
885 83f64091 bellard
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
886 83f64091 bellard
{
887 83f64091 bellard
    BDRVRawState *s = bs->opaque;
888 83f64091 bellard
    int access_flags, create_flags;
889 ce1a14dc pbrook
    DWORD overlapped;
890 19cb3738 bellard
891 f45512fe bellard
    s->type = FTYPE_FILE;
892 83f64091 bellard
893 83f64091 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
894 83f64091 bellard
        access_flags = GENERIC_READ | GENERIC_WRITE;
895 83f64091 bellard
    } else {
896 83f64091 bellard
        access_flags = GENERIC_READ;
897 83f64091 bellard
    }
898 979b67ad bellard
    if (flags & BDRV_O_CREAT) {
899 83f64091 bellard
        create_flags = CREATE_ALWAYS;
900 83f64091 bellard
    } else {
901 83f64091 bellard
        create_flags = OPEN_EXISTING;
902 83f64091 bellard
    }
903 ce1a14dc pbrook
#ifdef QEMU_TOOL
904 ce1a14dc pbrook
    overlapped = 0;
905 ce1a14dc pbrook
#else
906 ce1a14dc pbrook
    overlapped = FILE_FLAG_OVERLAPPED;
907 ce1a14dc pbrook
#endif
908 83f64091 bellard
    s->hfile = CreateFile(filename, access_flags, 
909 83f64091 bellard
                          FILE_SHARE_READ, NULL,
910 ce1a14dc pbrook
                          create_flags, overlapped, 0);
911 83f64091 bellard
    if (s->hfile == INVALID_HANDLE_VALUE) 
912 83f64091 bellard
        return -1;
913 83f64091 bellard
    return 0;
914 83f64091 bellard
}
915 83f64091 bellard
916 83f64091 bellard
static int raw_pread(BlockDriverState *bs, int64_t offset, 
917 83f64091 bellard
                     uint8_t *buf, int count)
918 83f64091 bellard
{
919 83f64091 bellard
    BDRVRawState *s = bs->opaque;
920 83f64091 bellard
    OVERLAPPED ov;
921 83f64091 bellard
    DWORD ret_count;
922 83f64091 bellard
    int ret;
923 83f64091 bellard
    
924 83f64091 bellard
    memset(&ov, 0, sizeof(ov));
925 83f64091 bellard
    ov.Offset = offset;
926 83f64091 bellard
    ov.OffsetHigh = offset >> 32;
927 436e15b8 bellard
    ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
928 436e15b8 bellard
    if (!ret) {
929 436e15b8 bellard
        ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
930 436e15b8 bellard
        if (!ret)
931 436e15b8 bellard
            return -EIO;
932 436e15b8 bellard
        else
933 436e15b8 bellard
            return ret_count;
934 436e15b8 bellard
    }
935 83f64091 bellard
    return ret_count;
936 83f64091 bellard
}
937 83f64091 bellard
938 83f64091 bellard
static int raw_pwrite(BlockDriverState *bs, int64_t offset, 
939 83f64091 bellard
                      const uint8_t *buf, int count)
940 83f64091 bellard
{
941 83f64091 bellard
    BDRVRawState *s = bs->opaque;
942 83f64091 bellard
    OVERLAPPED ov;
943 83f64091 bellard
    DWORD ret_count;
944 83f64091 bellard
    int ret;
945 83f64091 bellard
    
946 83f64091 bellard
    memset(&ov, 0, sizeof(ov));
947 83f64091 bellard
    ov.Offset = offset;
948 83f64091 bellard
    ov.OffsetHigh = offset >> 32;
949 436e15b8 bellard
    ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
950 436e15b8 bellard
    if (!ret) {
951 436e15b8 bellard
        ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
952 436e15b8 bellard
        if (!ret)
953 436e15b8 bellard
            return -EIO;
954 436e15b8 bellard
        else
955 436e15b8 bellard
            return ret_count;
956 436e15b8 bellard
    }
957 83f64091 bellard
    return ret_count;
958 83f64091 bellard
}
959 83f64091 bellard
960 436e15b8 bellard
#ifndef QEMU_TOOL
961 83f64091 bellard
static void raw_aio_cb(void *opaque)
962 83f64091 bellard
{
963 ce1a14dc pbrook
    RawAIOCB *acb = opaque;
964 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
965 979b67ad bellard
    BDRVRawState *s = bs->opaque;
966 83f64091 bellard
    DWORD ret_count;
967 83f64091 bellard
    int ret;
968 83f64091 bellard
969 ce1a14dc pbrook
    ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
970 ce1a14dc pbrook
    if (!ret || ret_count != acb->count) {
971 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, -EIO);
972 83f64091 bellard
    } else {
973 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, 0);
974 83f64091 bellard
    }
975 83f64091 bellard
}
976 436e15b8 bellard
#endif
977 ce1a14dc pbrook
978 ce1a14dc pbrook
static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
979 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
980 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
981 83f64091 bellard
{
982 ce1a14dc pbrook
    RawAIOCB *acb;
983 83f64091 bellard
    int64_t offset;
984 83f64091 bellard
985 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
986 ce1a14dc pbrook
    if (acb->hEvent) {
987 ce1a14dc pbrook
        acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
988 ce1a14dc pbrook
        if (!acb->hEvent) {
989 ce1a14dc pbrook
            qemu_aio_release(acb);
990 ce1a14dc pbrook
            return NULL;
991 ce1a14dc pbrook
        }
992 ce1a14dc pbrook
    }
993 ce1a14dc pbrook
    memset(&acb->ov, 0, sizeof(acb->ov));
994 83f64091 bellard
    offset = sector_num * 512;
995 ce1a14dc pbrook
    acb->ov.Offset = offset;
996 ce1a14dc pbrook
    acb->ov.OffsetHigh = offset >> 32;
997 ce1a14dc pbrook
    acb->ov.hEvent = acb->hEvent;
998 ce1a14dc pbrook
    acb->count = nb_sectors * 512;
999 436e15b8 bellard
#ifndef QEMU_TOOL
1000 ce1a14dc pbrook
    qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1001 436e15b8 bellard
#endif
1002 ce1a14dc pbrook
    return acb;
1003 83f64091 bellard
}
1004 83f64091 bellard
1005 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
1006 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1007 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1008 83f64091 bellard
{
1009 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1010 ce1a14dc pbrook
    RawAIOCB *acb;
1011 83f64091 bellard
    int ret;
1012 83f64091 bellard
1013 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1014 ce1a14dc pbrook
    if (!acb)
1015 ce1a14dc pbrook
        return NULL;
1016 ce1a14dc pbrook
    ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1017 ce1a14dc pbrook
    if (!ret) {
1018 ce1a14dc pbrook
        qemu_aio_release(acb);
1019 ce1a14dc pbrook
        return NULL;
1020 ce1a14dc pbrook
    }
1021 ce1a14dc pbrook
#ifdef QEMU_TOOL
1022 ce1a14dc pbrook
    qemu_aio_release(acb);
1023 436e15b8 bellard
#endif
1024 ce1a14dc pbrook
    return (BlockDriverAIOCB *)acb;
1025 83f64091 bellard
}
1026 83f64091 bellard
1027 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1028 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1029 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1030 83f64091 bellard
{
1031 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1032 ce1a14dc pbrook
    RawAIOCB *acb;
1033 ce1a14dc pbrook
    int ret;
1034 83f64091 bellard
1035 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1036 ce1a14dc pbrook
    if (!acb)
1037 ce1a14dc pbrook
        return NULL;
1038 ce1a14dc pbrook
    ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1039 ce1a14dc pbrook
    if (!ret) {
1040 ce1a14dc pbrook
        qemu_aio_release(acb);
1041 ce1a14dc pbrook
        return NULL;
1042 ce1a14dc pbrook
    }
1043 ce1a14dc pbrook
#ifdef QEMU_TOOL
1044 ce1a14dc pbrook
    qemu_aio_release(acb);
1045 436e15b8 bellard
#endif
1046 ce1a14dc pbrook
    return (BlockDriverAIOCB *)acb;
1047 83f64091 bellard
}
1048 83f64091 bellard
1049 ce1a14dc pbrook
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
1050 83f64091 bellard
{
1051 ce1a14dc pbrook
#ifndef QEMU_TOOL
1052 ce1a14dc pbrook
    RawAIOCB *acb = (RawAIOCB *)blockacb;
1053 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
1054 ce1a14dc pbrook
    BDRVRawState *s = bs->opaque;
1055 ce1a14dc pbrook
1056 ce1a14dc pbrook
    qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1057 ce1a14dc pbrook
    /* XXX: if more than one async I/O it is not correct */
1058 ce1a14dc pbrook
    CancelIo(s->hfile);
1059 ce1a14dc pbrook
    qemu_aio_release(acb);
1060 ce1a14dc pbrook
#endif
1061 83f64091 bellard
}
1062 83f64091 bellard
1063 83f64091 bellard
static void raw_flush(BlockDriverState *bs)
1064 83f64091 bellard
{
1065 83f64091 bellard
    /* XXX: add it */
1066 83f64091 bellard
}
1067 83f64091 bellard
1068 83f64091 bellard
static void raw_close(BlockDriverState *bs)
1069 83f64091 bellard
{
1070 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1071 83f64091 bellard
    CloseHandle(s->hfile);
1072 83f64091 bellard
}
1073 83f64091 bellard
1074 83f64091 bellard
static int raw_truncate(BlockDriverState *bs, int64_t offset)
1075 83f64091 bellard
{
1076 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1077 83f64091 bellard
    DWORD low, high;
1078 83f64091 bellard
1079 979b67ad bellard
    low = offset;
1080 979b67ad bellard
    high = offset >> 32;
1081 83f64091 bellard
    if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1082 83f64091 bellard
        return -EIO;
1083 83f64091 bellard
    if (!SetEndOfFile(s->hfile))
1084 83f64091 bellard
        return -EIO;
1085 83f64091 bellard
    return 0;
1086 83f64091 bellard
}
1087 83f64091 bellard
1088 f45512fe bellard
static int64_t raw_getlength(BlockDriverState *bs)
1089 83f64091 bellard
{
1090 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1091 83f64091 bellard
    LARGE_INTEGER l;
1092 19cb3738 bellard
    ULARGE_INTEGER available, total, total_free; 
1093 436e15b8 bellard
1094 f45512fe bellard
    switch(s->type) {
1095 19cb3738 bellard
    case FTYPE_FILE:
1096 19cb3738 bellard
        l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1097 19cb3738 bellard
        if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1098 19cb3738 bellard
            return -EIO;
1099 19cb3738 bellard
        break;
1100 19cb3738 bellard
    case FTYPE_CD:
1101 f45512fe bellard
        if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
1102 19cb3738 bellard
            return -EIO;
1103 f45512fe bellard
        l.QuadPart = total.QuadPart;
1104 19cb3738 bellard
        break;
1105 19cb3738 bellard
    default:
1106 19cb3738 bellard
        return -EIO;
1107 19cb3738 bellard
    }
1108 83f64091 bellard
    return l.QuadPart;
1109 83f64091 bellard
}
1110 83f64091 bellard
1111 83f64091 bellard
static int raw_create(const char *filename, int64_t total_size,
1112 83f64091 bellard
                      const char *backing_file, int flags)
1113 83f64091 bellard
{
1114 83f64091 bellard
    int fd;
1115 83f64091 bellard
1116 83f64091 bellard
    if (flags || backing_file)
1117 83f64091 bellard
        return -ENOTSUP;
1118 83f64091 bellard
1119 83f64091 bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 
1120 83f64091 bellard
              0644);
1121 83f64091 bellard
    if (fd < 0)
1122 83f64091 bellard
        return -EIO;
1123 83f64091 bellard
    set_sparse(fd);
1124 83f64091 bellard
    ftruncate(fd, total_size * 512);
1125 83f64091 bellard
    close(fd);
1126 83f64091 bellard
    return 0;
1127 83f64091 bellard
}
1128 83f64091 bellard
1129 83f64091 bellard
void qemu_aio_init(void)
1130 83f64091 bellard
{
1131 83f64091 bellard
}
1132 83f64091 bellard
1133 83f64091 bellard
void qemu_aio_poll(void)
1134 83f64091 bellard
{
1135 83f64091 bellard
}
1136 83f64091 bellard
1137 83f64091 bellard
void qemu_aio_wait_start(void)
1138 83f64091 bellard
{
1139 83f64091 bellard
}
1140 83f64091 bellard
1141 83f64091 bellard
void qemu_aio_wait(void)
1142 83f64091 bellard
{
1143 fd44d818 bellard
#ifndef QEMU_TOOL
1144 fd44d818 bellard
    qemu_bh_poll();
1145 fd44d818 bellard
#endif
1146 83f64091 bellard
}
1147 83f64091 bellard
1148 83f64091 bellard
void qemu_aio_wait_end(void)
1149 83f64091 bellard
{
1150 83f64091 bellard
}
1151 83f64091 bellard
1152 83f64091 bellard
BlockDriver bdrv_raw = {
1153 83f64091 bellard
    "raw",
1154 83f64091 bellard
    sizeof(BDRVRawState),
1155 83f64091 bellard
    NULL, /* no probe for protocols */
1156 83f64091 bellard
    raw_open,
1157 83f64091 bellard
    NULL,
1158 83f64091 bellard
    NULL,
1159 83f64091 bellard
    raw_close,
1160 83f64091 bellard
    raw_create,
1161 83f64091 bellard
    raw_flush,
1162 83f64091 bellard
    
1163 83f64091 bellard
#if 0
1164 83f64091 bellard
    .bdrv_aio_read = raw_aio_read,
1165 83f64091 bellard
    .bdrv_aio_write = raw_aio_write,
1166 83f64091 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
1167 ce1a14dc pbrook
    .aiocb_size = sizeof(RawAIOCB);
1168 83f64091 bellard
#endif
1169 83f64091 bellard
    .protocol_name = "file",
1170 83f64091 bellard
    .bdrv_pread = raw_pread,
1171 83f64091 bellard
    .bdrv_pwrite = raw_pwrite,
1172 83f64091 bellard
    .bdrv_truncate = raw_truncate,
1173 83f64091 bellard
    .bdrv_getlength = raw_getlength,
1174 83f64091 bellard
};
1175 19cb3738 bellard
1176 19cb3738 bellard
/***********************************************/
1177 19cb3738 bellard
/* host device */
1178 19cb3738 bellard
1179 19cb3738 bellard
static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1180 19cb3738 bellard
{
1181 19cb3738 bellard
    char drives[256], *pdrv = drives;
1182 19cb3738 bellard
    UINT type;
1183 19cb3738 bellard
1184 f45512fe bellard
    memset(drives, 0, sizeof(drives));
1185 19cb3738 bellard
    GetLogicalDriveStrings(sizeof(drives), drives);
1186 19cb3738 bellard
    while(pdrv[0] != '\0') {
1187 19cb3738 bellard
        type = GetDriveType(pdrv);
1188 19cb3738 bellard
        switch(type) {
1189 19cb3738 bellard
        case DRIVE_CDROM:
1190 19cb3738 bellard
            snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1191 19cb3738 bellard
            return 0;
1192 19cb3738 bellard
            break;
1193 19cb3738 bellard
        }
1194 19cb3738 bellard
        pdrv += lstrlen(pdrv) + 1;
1195 19cb3738 bellard
    }
1196 19cb3738 bellard
    return -1;
1197 19cb3738 bellard
}
1198 19cb3738 bellard
1199 f45512fe bellard
static int find_device_type(BlockDriverState *bs, const char *filename)
1200 19cb3738 bellard
{
1201 f45512fe bellard
    BDRVRawState *s = bs->opaque;
1202 19cb3738 bellard
    UINT type;
1203 19cb3738 bellard
    const char *p;
1204 19cb3738 bellard
1205 19cb3738 bellard
    if (strstart(filename, "\\\\.\\", &p) ||
1206 19cb3738 bellard
        strstart(filename, "//./", &p)) {
1207 f45512fe bellard
        snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
1208 f45512fe bellard
        type = GetDriveType(s->drive_path);
1209 19cb3738 bellard
        if (type == DRIVE_CDROM)
1210 19cb3738 bellard
            return FTYPE_CD;
1211 19cb3738 bellard
        else
1212 19cb3738 bellard
            return FTYPE_FILE;
1213 19cb3738 bellard
    } else {
1214 19cb3738 bellard
        return FTYPE_FILE;
1215 19cb3738 bellard
    }
1216 19cb3738 bellard
}
1217 19cb3738 bellard
1218 19cb3738 bellard
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1219 19cb3738 bellard
{
1220 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
1221 19cb3738 bellard
    int access_flags, create_flags;
1222 19cb3738 bellard
    DWORD overlapped;
1223 19cb3738 bellard
    char device_name[64];
1224 19cb3738 bellard
1225 19cb3738 bellard
    if (strstart(filename, "/dev/cdrom", NULL)) {
1226 19cb3738 bellard
        if (find_cdrom(device_name, sizeof(device_name)) < 0)
1227 19cb3738 bellard
            return -ENOENT;
1228 19cb3738 bellard
        filename = device_name;
1229 19cb3738 bellard
    } else {
1230 19cb3738 bellard
        /* transform drive letters into device name */
1231 19cb3738 bellard
        if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1232 19cb3738 bellard
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1233 19cb3738 bellard
            filename[1] == ':' && filename[2] == '\0') {
1234 19cb3738 bellard
            snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1235 19cb3738 bellard
            filename = device_name;
1236 19cb3738 bellard
        }
1237 19cb3738 bellard
    }
1238 f45512fe bellard
    s->type = find_device_type(bs, filename);
1239 19cb3738 bellard
1240 19cb3738 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1241 19cb3738 bellard
        access_flags = GENERIC_READ | GENERIC_WRITE;
1242 19cb3738 bellard
    } else {
1243 19cb3738 bellard
        access_flags = GENERIC_READ;
1244 19cb3738 bellard
    }
1245 19cb3738 bellard
    create_flags = OPEN_EXISTING;
1246 19cb3738 bellard
1247 19cb3738 bellard
#ifdef QEMU_TOOL
1248 19cb3738 bellard
    overlapped = 0;
1249 19cb3738 bellard
#else
1250 19cb3738 bellard
    overlapped = FILE_FLAG_OVERLAPPED;
1251 19cb3738 bellard
#endif
1252 19cb3738 bellard
    s->hfile = CreateFile(filename, access_flags, 
1253 19cb3738 bellard
                          FILE_SHARE_READ, NULL,
1254 19cb3738 bellard
                          create_flags, overlapped, 0);
1255 19cb3738 bellard
    if (s->hfile == INVALID_HANDLE_VALUE) 
1256 19cb3738 bellard
        return -1;
1257 19cb3738 bellard
    return 0;
1258 19cb3738 bellard
}
1259 19cb3738 bellard
1260 19cb3738 bellard
#if 0
1261 19cb3738 bellard
/***********************************************/
1262 19cb3738 bellard
/* removable device additionnal commands */
1263 19cb3738 bellard

1264 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
1265 19cb3738 bellard
{
1266 19cb3738 bellard
    return 1;
1267 19cb3738 bellard
}
1268 19cb3738 bellard

1269 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
1270 19cb3738 bellard
{
1271 19cb3738 bellard
    return -ENOTSUP;
1272 19cb3738 bellard
}
1273 19cb3738 bellard

1274 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
1275 19cb3738 bellard
{
1276 19cb3738 bellard
    DWORD ret_count;
1277 19cb3738 bellard

1278 19cb3738 bellard
    if (s->type == FTYPE_FILE)
1279 19cb3738 bellard
        return -ENOTSUP;
1280 19cb3738 bellard
    if (eject_flag) {
1281 19cb3738 bellard
        DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA, 
1282 19cb3738 bellard
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1283 19cb3738 bellard
    } else {
1284 19cb3738 bellard
        DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA, 
1285 19cb3738 bellard
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1286 19cb3738 bellard
    }
1287 19cb3738 bellard
}
1288 19cb3738 bellard

1289 19cb3738 bellard
static int raw_set_locked(BlockDriverState *bs, int locked)
1290 19cb3738 bellard
{
1291 19cb3738 bellard
    return -ENOTSUP;
1292 19cb3738 bellard
}
1293 19cb3738 bellard
#endif
1294 19cb3738 bellard
1295 19cb3738 bellard
BlockDriver bdrv_host_device = {
1296 19cb3738 bellard
    "host_device",
1297 19cb3738 bellard
    sizeof(BDRVRawState),
1298 19cb3738 bellard
    NULL, /* no probe for protocols */
1299 19cb3738 bellard
    hdev_open,
1300 19cb3738 bellard
    NULL,
1301 19cb3738 bellard
    NULL,
1302 19cb3738 bellard
    raw_close,
1303 19cb3738 bellard
    NULL,
1304 19cb3738 bellard
    raw_flush,
1305 19cb3738 bellard
    
1306 19cb3738 bellard
#if 0
1307 19cb3738 bellard
    .bdrv_aio_read = raw_aio_read,
1308 19cb3738 bellard
    .bdrv_aio_write = raw_aio_write,
1309 19cb3738 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
1310 19cb3738 bellard
    .aiocb_size = sizeof(RawAIOCB);
1311 19cb3738 bellard
#endif
1312 19cb3738 bellard
    .bdrv_pread = raw_pread,
1313 19cb3738 bellard
    .bdrv_pwrite = raw_pwrite,
1314 19cb3738 bellard
    .bdrv_getlength = raw_getlength,
1315 19cb3738 bellard
};
1316 83f64091 bellard
#endif /* _WIN32 */