Statistics
| Branch: | Revision:

root / block-raw.c @ 51b2772f

History | View | Annotate | Download (32.7 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 2e9671da ths
#define _POSIX_PTHREAD_SEMANTICS 1
48 2e9671da ths
#include <signal.h>
49 83f64091 bellard
#include <sys/dkio.h>
50 83f64091 bellard
#endif
51 19cb3738 bellard
#ifdef __linux__
52 19cb3738 bellard
#include <sys/ioctl.h>
53 19cb3738 bellard
#include <linux/cdrom.h>
54 19cb3738 bellard
#include <linux/fd.h>
55 19cb3738 bellard
#endif
56 1cb6c3fd ths
#ifdef __FreeBSD__
57 1cb6c3fd ths
#include <sys/disk.h>
58 1cb6c3fd ths
#endif
59 83f64091 bellard
60 19cb3738 bellard
//#define DEBUG_FLOPPY
61 83f64091 bellard
62 19cb3738 bellard
#define FTYPE_FILE   0
63 19cb3738 bellard
#define FTYPE_CD     1
64 19cb3738 bellard
#define FTYPE_FD     2
65 83f64091 bellard
66 19cb3738 bellard
/* if the FD is not accessed during that time (in ms), we try to
67 19cb3738 bellard
   reopen it to see if the disk has been changed */
68 19cb3738 bellard
#define FD_OPEN_TIMEOUT 1000
69 83f64091 bellard
70 19cb3738 bellard
typedef struct BDRVRawState {
71 19cb3738 bellard
    int fd;
72 19cb3738 bellard
    int type;
73 19cb3738 bellard
#if defined(__linux__)
74 19cb3738 bellard
    /* linux floppy specific */
75 19cb3738 bellard
    int fd_open_flags;
76 19cb3738 bellard
    int64_t fd_open_time;
77 19cb3738 bellard
    int64_t fd_error_time;
78 19cb3738 bellard
    int fd_got_error;
79 19cb3738 bellard
    int fd_media_changed;
80 83f64091 bellard
#endif
81 19cb3738 bellard
} BDRVRawState;
82 19cb3738 bellard
83 19cb3738 bellard
static int fd_open(BlockDriverState *bs);
84 83f64091 bellard
85 83f64091 bellard
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
86 83f64091 bellard
{
87 83f64091 bellard
    BDRVRawState *s = bs->opaque;
88 19cb3738 bellard
    int fd, open_flags, ret;
89 83f64091 bellard
90 83f64091 bellard
    open_flags = O_BINARY;
91 83f64091 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
92 83f64091 bellard
        open_flags |= O_RDWR;
93 83f64091 bellard
    } else {
94 83f64091 bellard
        open_flags |= O_RDONLY;
95 83f64091 bellard
        bs->read_only = 1;
96 83f64091 bellard
    }
97 83f64091 bellard
    if (flags & BDRV_O_CREAT)
98 83f64091 bellard
        open_flags |= O_CREAT | O_TRUNC;
99 83f64091 bellard
100 19cb3738 bellard
    s->type = FTYPE_FILE;
101 19cb3738 bellard
102 83f64091 bellard
    fd = open(filename, open_flags, 0644);
103 19cb3738 bellard
    if (fd < 0) {
104 19cb3738 bellard
        ret = -errno;
105 19cb3738 bellard
        if (ret == -EROFS)
106 19cb3738 bellard
            ret = -EACCES;
107 19cb3738 bellard
        return ret;
108 19cb3738 bellard
    }
109 83f64091 bellard
    s->fd = fd;
110 83f64091 bellard
    return 0;
111 83f64091 bellard
}
112 83f64091 bellard
113 83f64091 bellard
/* XXX: use host sector size if necessary with:
114 83f64091 bellard
#ifdef DIOCGSECTORSIZE
115 83f64091 bellard
        {
116 83f64091 bellard
            unsigned int sectorsize = 512;
117 83f64091 bellard
            if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
118 83f64091 bellard
                sectorsize > bufsize)
119 83f64091 bellard
                bufsize = sectorsize;
120 83f64091 bellard
        }
121 83f64091 bellard
#endif
122 83f64091 bellard
#ifdef CONFIG_COCOA
123 83f64091 bellard
        u_int32_t   blockSize = 512;
124 83f64091 bellard
        if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
125 83f64091 bellard
            bufsize = blockSize;
126 83f64091 bellard
        }
127 83f64091 bellard
#endif
128 83f64091 bellard
*/
129 83f64091 bellard
130 83f64091 bellard
static int raw_pread(BlockDriverState *bs, int64_t offset, 
131 83f64091 bellard
                     uint8_t *buf, int count)
132 83f64091 bellard
{
133 83f64091 bellard
    BDRVRawState *s = bs->opaque;
134 83f64091 bellard
    int ret;
135 83f64091 bellard
    
136 19cb3738 bellard
    ret = fd_open(bs);
137 19cb3738 bellard
    if (ret < 0)
138 19cb3738 bellard
        return ret;
139 19cb3738 bellard
140 83f64091 bellard
    lseek(s->fd, offset, SEEK_SET);
141 83f64091 bellard
    ret = read(s->fd, buf, count);
142 83f64091 bellard
    return ret;
143 83f64091 bellard
}
144 83f64091 bellard
145 83f64091 bellard
static int raw_pwrite(BlockDriverState *bs, int64_t offset, 
146 83f64091 bellard
                      const uint8_t *buf, int count)
147 83f64091 bellard
{
148 83f64091 bellard
    BDRVRawState *s = bs->opaque;
149 83f64091 bellard
    int ret;
150 83f64091 bellard
    
151 19cb3738 bellard
    ret = fd_open(bs);
152 19cb3738 bellard
    if (ret < 0)
153 19cb3738 bellard
        return ret;
154 19cb3738 bellard
155 83f64091 bellard
    lseek(s->fd, offset, SEEK_SET);
156 83f64091 bellard
    ret = write(s->fd, buf, count);
157 83f64091 bellard
    return ret;
158 83f64091 bellard
}
159 83f64091 bellard
160 83f64091 bellard
/***********************************************************/
161 19cb3738 bellard
/* Unix AIO using POSIX AIO */
162 83f64091 bellard
163 83f64091 bellard
typedef struct RawAIOCB {
164 ce1a14dc pbrook
    BlockDriverAIOCB common;
165 83f64091 bellard
    struct aiocb aiocb;
166 ce1a14dc pbrook
    struct RawAIOCB *next;
167 83f64091 bellard
} RawAIOCB;
168 83f64091 bellard
169 83f64091 bellard
static int aio_sig_num = SIGUSR2;
170 ce1a14dc pbrook
static RawAIOCB *first_aio; /* AIO issued */
171 979b67ad bellard
static int aio_initialized = 0;
172 83f64091 bellard
173 83f64091 bellard
static void aio_signal_handler(int signum)
174 83f64091 bellard
{
175 979b67ad bellard
#ifndef QEMU_TOOL
176 83f64091 bellard
    CPUState *env = cpu_single_env;
177 83f64091 bellard
    if (env) {
178 83f64091 bellard
        /* stop the currently executing cpu because a timer occured */
179 83f64091 bellard
        cpu_interrupt(env, CPU_INTERRUPT_EXIT);
180 83f64091 bellard
#ifdef USE_KQEMU
181 83f64091 bellard
        if (env->kqemu_enabled) {
182 83f64091 bellard
            kqemu_cpu_interrupt(env);
183 83f64091 bellard
        }
184 83f64091 bellard
#endif
185 83f64091 bellard
    }
186 979b67ad bellard
#endif
187 83f64091 bellard
}
188 83f64091 bellard
189 83f64091 bellard
void qemu_aio_init(void)
190 83f64091 bellard
{
191 83f64091 bellard
    struct sigaction act;
192 979b67ad bellard
193 979b67ad bellard
    aio_initialized = 1;
194 83f64091 bellard
    
195 83f64091 bellard
    sigfillset(&act.sa_mask);
196 83f64091 bellard
    act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
197 83f64091 bellard
    act.sa_handler = aio_signal_handler;
198 83f64091 bellard
    sigaction(aio_sig_num, &act, NULL);
199 83f64091 bellard
200 19cb3738 bellard
#if defined(__GLIBC__) && defined(__linux__)
201 83f64091 bellard
    {
202 19cb3738 bellard
        /* XXX: aio thread exit seems to hang on RedHat 9 and this init
203 19cb3738 bellard
           seems to fix the problem. */
204 83f64091 bellard
        struct aioinit ai;
205 83f64091 bellard
        memset(&ai, 0, sizeof(ai));
206 19cb3738 bellard
        ai.aio_threads = 1;
207 83f64091 bellard
        ai.aio_num = 1;
208 83f64091 bellard
        ai.aio_idle_time = 365 * 100000;
209 83f64091 bellard
        aio_init(&ai);
210 83f64091 bellard
    }
211 19cb3738 bellard
#endif
212 83f64091 bellard
}
213 83f64091 bellard
214 83f64091 bellard
void qemu_aio_poll(void)
215 83f64091 bellard
{
216 ce1a14dc pbrook
    RawAIOCB *acb, **pacb;
217 83f64091 bellard
    int ret;
218 83f64091 bellard
219 83f64091 bellard
    for(;;) {
220 83f64091 bellard
        pacb = &first_aio;
221 83f64091 bellard
        for(;;) {
222 83f64091 bellard
            acb = *pacb;
223 83f64091 bellard
            if (!acb)
224 83f64091 bellard
                goto the_end;
225 ce1a14dc pbrook
            ret = aio_error(&acb->aiocb);
226 83f64091 bellard
            if (ret == ECANCELED) {
227 83f64091 bellard
                /* remove the request */
228 ce1a14dc pbrook
                *pacb = acb->next;
229 ce1a14dc pbrook
                qemu_aio_release(acb);
230 83f64091 bellard
            } else if (ret != EINPROGRESS) {
231 83f64091 bellard
                /* end of aio */
232 83f64091 bellard
                if (ret == 0) {
233 ce1a14dc pbrook
                    ret = aio_return(&acb->aiocb);
234 ce1a14dc pbrook
                    if (ret == acb->aiocb.aio_nbytes)
235 83f64091 bellard
                        ret = 0;
236 83f64091 bellard
                    else
237 19cb3738 bellard
                        ret = -EINVAL;
238 83f64091 bellard
                } else {
239 83f64091 bellard
                    ret = -ret;
240 83f64091 bellard
                }
241 83f64091 bellard
                /* remove the request */
242 ce1a14dc pbrook
                *pacb = acb->next;
243 83f64091 bellard
                /* call the callback */
244 ce1a14dc pbrook
                acb->common.cb(acb->common.opaque, ret);
245 ce1a14dc pbrook
                qemu_aio_release(acb);
246 83f64091 bellard
                break;
247 83f64091 bellard
            } else {
248 ce1a14dc pbrook
                pacb = &acb->next;
249 83f64091 bellard
            }
250 83f64091 bellard
        }
251 83f64091 bellard
    }
252 83f64091 bellard
 the_end: ;
253 83f64091 bellard
}
254 83f64091 bellard
255 6192bc37 pbrook
/* Wait for all IO requests to complete.  */
256 6192bc37 pbrook
void qemu_aio_flush(void)
257 6192bc37 pbrook
{
258 6192bc37 pbrook
    qemu_aio_wait_start();
259 6192bc37 pbrook
    qemu_aio_poll();
260 6192bc37 pbrook
    while (first_aio) {
261 6192bc37 pbrook
        qemu_aio_wait();
262 6192bc37 pbrook
    }
263 6192bc37 pbrook
    qemu_aio_wait_end();
264 6192bc37 pbrook
}
265 6192bc37 pbrook
266 83f64091 bellard
/* wait until at least one AIO was handled */
267 83f64091 bellard
static sigset_t wait_oset;
268 83f64091 bellard
269 83f64091 bellard
void qemu_aio_wait_start(void)
270 83f64091 bellard
{
271 83f64091 bellard
    sigset_t set;
272 979b67ad bellard
273 979b67ad bellard
    if (!aio_initialized)
274 979b67ad bellard
        qemu_aio_init();
275 83f64091 bellard
    sigemptyset(&set);
276 83f64091 bellard
    sigaddset(&set, aio_sig_num);
277 83f64091 bellard
    sigprocmask(SIG_BLOCK, &set, &wait_oset);
278 83f64091 bellard
}
279 83f64091 bellard
280 83f64091 bellard
void qemu_aio_wait(void)
281 83f64091 bellard
{
282 83f64091 bellard
    sigset_t set;
283 83f64091 bellard
    int nb_sigs;
284 6eb5733a bellard
285 6eb5733a bellard
#ifndef QEMU_TOOL
286 6eb5733a bellard
    if (qemu_bh_poll())
287 6eb5733a bellard
        return;
288 6eb5733a bellard
#endif
289 83f64091 bellard
    sigemptyset(&set);
290 83f64091 bellard
    sigaddset(&set, aio_sig_num);
291 83f64091 bellard
    sigwait(&set, &nb_sigs);
292 83f64091 bellard
    qemu_aio_poll();
293 83f64091 bellard
}
294 83f64091 bellard
295 83f64091 bellard
void qemu_aio_wait_end(void)
296 83f64091 bellard
{
297 83f64091 bellard
    sigprocmask(SIG_SETMASK, &wait_oset, NULL);
298 83f64091 bellard
}
299 83f64091 bellard
300 ce1a14dc pbrook
static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
301 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
302 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
303 83f64091 bellard
{
304 ce1a14dc pbrook
    BDRVRawState *s = bs->opaque;
305 ce1a14dc pbrook
    RawAIOCB *acb;
306 ce1a14dc pbrook
307 19cb3738 bellard
    if (fd_open(bs) < 0)
308 19cb3738 bellard
        return NULL;
309 19cb3738 bellard
310 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
311 ce1a14dc pbrook
    if (!acb)
312 ce1a14dc pbrook
        return NULL;
313 ce1a14dc pbrook
    acb->aiocb.aio_fildes = s->fd;
314 ce1a14dc pbrook
    acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
315 ce1a14dc pbrook
    acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
316 ce1a14dc pbrook
    acb->aiocb.aio_buf = buf;
317 ce1a14dc pbrook
    acb->aiocb.aio_nbytes = nb_sectors * 512;
318 ce1a14dc pbrook
    acb->aiocb.aio_offset = sector_num * 512;
319 ce1a14dc pbrook
    acb->next = first_aio;
320 ce1a14dc pbrook
    first_aio = acb;
321 ce1a14dc pbrook
    return acb;
322 83f64091 bellard
}
323 83f64091 bellard
324 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
325 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
326 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
327 83f64091 bellard
{
328 ce1a14dc pbrook
    RawAIOCB *acb;
329 83f64091 bellard
330 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
331 ce1a14dc pbrook
    if (!acb)
332 ce1a14dc pbrook
        return NULL;
333 ce1a14dc pbrook
    if (aio_read(&acb->aiocb) < 0) {
334 ce1a14dc pbrook
        qemu_aio_release(acb);
335 ce1a14dc pbrook
        return NULL;
336 83f64091 bellard
    } 
337 ce1a14dc pbrook
    return &acb->common;
338 83f64091 bellard
}
339 83f64091 bellard
340 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
341 ce1a14dc pbrook
        int64_t sector_num, const uint8_t *buf, int nb_sectors,
342 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
343 83f64091 bellard
{
344 ce1a14dc pbrook
    RawAIOCB *acb;
345 83f64091 bellard
346 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque);
347 ce1a14dc pbrook
    if (!acb)
348 ce1a14dc pbrook
        return NULL;
349 ce1a14dc pbrook
    if (aio_write(&acb->aiocb) < 0) {
350 ce1a14dc pbrook
        qemu_aio_release(acb);
351 ce1a14dc pbrook
        return NULL;
352 83f64091 bellard
    } 
353 ce1a14dc pbrook
    return &acb->common;
354 83f64091 bellard
}
355 83f64091 bellard
356 ce1a14dc pbrook
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
357 83f64091 bellard
{
358 83f64091 bellard
    int ret;
359 ce1a14dc pbrook
    RawAIOCB *acb = (RawAIOCB *)blockacb;
360 ce1a14dc pbrook
    RawAIOCB **pacb;
361 83f64091 bellard
362 ce1a14dc pbrook
    ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb);
363 83f64091 bellard
    if (ret == AIO_NOTCANCELED) {
364 83f64091 bellard
        /* fail safe: if the aio could not be canceled, we wait for
365 83f64091 bellard
           it */
366 ce1a14dc pbrook
        while (aio_error(&acb->aiocb) == EINPROGRESS);
367 83f64091 bellard
    }
368 83f64091 bellard
369 83f64091 bellard
    /* remove the callback from the queue */
370 83f64091 bellard
    pacb = &first_aio;
371 83f64091 bellard
    for(;;) {
372 83f64091 bellard
        if (*pacb == NULL) {
373 83f64091 bellard
            break;
374 83f64091 bellard
        } else if (*pacb == acb) {
375 ce1a14dc pbrook
            *pacb = acb->next;
376 ce1a14dc pbrook
            qemu_aio_release(acb);
377 83f64091 bellard
            break;
378 83f64091 bellard
        }
379 ce1a14dc pbrook
        pacb = &acb->next;
380 83f64091 bellard
    }
381 83f64091 bellard
}
382 83f64091 bellard
383 83f64091 bellard
static void raw_close(BlockDriverState *bs)
384 83f64091 bellard
{
385 83f64091 bellard
    BDRVRawState *s = bs->opaque;
386 19cb3738 bellard
    if (s->fd >= 0) {
387 19cb3738 bellard
        close(s->fd);
388 19cb3738 bellard
        s->fd = -1;
389 19cb3738 bellard
    }
390 83f64091 bellard
}
391 83f64091 bellard
392 83f64091 bellard
static int raw_truncate(BlockDriverState *bs, int64_t offset)
393 83f64091 bellard
{
394 83f64091 bellard
    BDRVRawState *s = bs->opaque;
395 19cb3738 bellard
    if (s->type != FTYPE_FILE)
396 19cb3738 bellard
        return -ENOTSUP;
397 83f64091 bellard
    if (ftruncate(s->fd, offset) < 0)
398 83f64091 bellard
        return -errno;
399 83f64091 bellard
    return 0;
400 83f64091 bellard
}
401 83f64091 bellard
402 83f64091 bellard
static int64_t  raw_getlength(BlockDriverState *bs)
403 83f64091 bellard
{
404 83f64091 bellard
    BDRVRawState *s = bs->opaque;
405 83f64091 bellard
    int fd = s->fd;
406 83f64091 bellard
    int64_t size;
407 83f64091 bellard
#ifdef _BSD
408 83f64091 bellard
    struct stat sb;
409 83f64091 bellard
#endif
410 83f64091 bellard
#ifdef __sun__
411 83f64091 bellard
    struct dk_minfo minfo;
412 83f64091 bellard
    int rv;
413 83f64091 bellard
#endif
414 19cb3738 bellard
    int ret;
415 19cb3738 bellard
416 19cb3738 bellard
    ret = fd_open(bs);
417 19cb3738 bellard
    if (ret < 0)
418 19cb3738 bellard
        return ret;
419 83f64091 bellard
420 83f64091 bellard
#ifdef _BSD
421 83f64091 bellard
    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
422 83f64091 bellard
#ifdef DIOCGMEDIASIZE
423 83f64091 bellard
        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
424 83f64091 bellard
#endif
425 83f64091 bellard
#ifdef CONFIG_COCOA
426 83f64091 bellard
        size = LONG_LONG_MAX;
427 83f64091 bellard
#else
428 83f64091 bellard
        size = lseek(fd, 0LL, SEEK_END);
429 83f64091 bellard
#endif
430 83f64091 bellard
    } else
431 83f64091 bellard
#endif
432 83f64091 bellard
#ifdef __sun__
433 83f64091 bellard
    /*
434 83f64091 bellard
     * use the DKIOCGMEDIAINFO ioctl to read the size.
435 83f64091 bellard
     */
436 83f64091 bellard
    rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
437 83f64091 bellard
    if ( rv != -1 ) {
438 83f64091 bellard
        size = minfo.dki_lbsize * minfo.dki_capacity;
439 83f64091 bellard
    } else /* there are reports that lseek on some devices
440 83f64091 bellard
              fails, but irc discussion said that contingency
441 83f64091 bellard
              on contingency was overkill */
442 83f64091 bellard
#endif
443 83f64091 bellard
    {
444 83f64091 bellard
        size = lseek(fd, 0, SEEK_END);
445 83f64091 bellard
    }
446 83f64091 bellard
    return size;
447 83f64091 bellard
}
448 83f64091 bellard
449 83f64091 bellard
static int raw_create(const char *filename, int64_t total_size,
450 83f64091 bellard
                      const char *backing_file, int flags)
451 83f64091 bellard
{
452 83f64091 bellard
    int fd;
453 83f64091 bellard
454 83f64091 bellard
    if (flags || backing_file)
455 83f64091 bellard
        return -ENOTSUP;
456 83f64091 bellard
457 9ab8a34a bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 
458 83f64091 bellard
              0644);
459 83f64091 bellard
    if (fd < 0)
460 83f64091 bellard
        return -EIO;
461 83f64091 bellard
    ftruncate(fd, total_size * 512);
462 83f64091 bellard
    close(fd);
463 83f64091 bellard
    return 0;
464 83f64091 bellard
}
465 83f64091 bellard
466 83f64091 bellard
static void raw_flush(BlockDriverState *bs)
467 83f64091 bellard
{
468 83f64091 bellard
    BDRVRawState *s = bs->opaque;
469 83f64091 bellard
    fsync(s->fd);
470 83f64091 bellard
}
471 83f64091 bellard
472 83f64091 bellard
BlockDriver bdrv_raw = {
473 83f64091 bellard
    "raw",
474 83f64091 bellard
    sizeof(BDRVRawState),
475 83f64091 bellard
    NULL, /* no probe for protocols */
476 83f64091 bellard
    raw_open,
477 83f64091 bellard
    NULL,
478 83f64091 bellard
    NULL,
479 83f64091 bellard
    raw_close,
480 83f64091 bellard
    raw_create,
481 83f64091 bellard
    raw_flush,
482 83f64091 bellard
    
483 83f64091 bellard
    .bdrv_aio_read = raw_aio_read,
484 83f64091 bellard
    .bdrv_aio_write = raw_aio_write,
485 83f64091 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
486 ce1a14dc pbrook
    .aiocb_size = sizeof(RawAIOCB),
487 83f64091 bellard
    .protocol_name = "file",
488 83f64091 bellard
    .bdrv_pread = raw_pread,
489 83f64091 bellard
    .bdrv_pwrite = raw_pwrite,
490 83f64091 bellard
    .bdrv_truncate = raw_truncate,
491 83f64091 bellard
    .bdrv_getlength = raw_getlength,
492 83f64091 bellard
};
493 83f64091 bellard
494 19cb3738 bellard
/***********************************************/
495 19cb3738 bellard
/* host device */
496 19cb3738 bellard
497 19cb3738 bellard
#ifdef CONFIG_COCOA
498 19cb3738 bellard
static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
499 19cb3738 bellard
static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
500 19cb3738 bellard
501 19cb3738 bellard
kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
502 19cb3738 bellard
{
503 19cb3738 bellard
    kern_return_t       kernResult; 
504 19cb3738 bellard
    mach_port_t     masterPort;
505 19cb3738 bellard
    CFMutableDictionaryRef  classesToMatch;
506 19cb3738 bellard
507 19cb3738 bellard
    kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
508 19cb3738 bellard
    if ( KERN_SUCCESS != kernResult ) {
509 19cb3738 bellard
        printf( "IOMasterPort returned %d\n", kernResult );
510 19cb3738 bellard
    }
511 19cb3738 bellard
    
512 19cb3738 bellard
    classesToMatch = IOServiceMatching( kIOCDMediaClass ); 
513 19cb3738 bellard
    if ( classesToMatch == NULL ) {
514 19cb3738 bellard
        printf( "IOServiceMatching returned a NULL dictionary.\n" );
515 19cb3738 bellard
    } else {
516 19cb3738 bellard
    CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
517 19cb3738 bellard
    }
518 19cb3738 bellard
    kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
519 19cb3738 bellard
    if ( KERN_SUCCESS != kernResult )
520 19cb3738 bellard
    {
521 19cb3738 bellard
        printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
522 19cb3738 bellard
    }
523 19cb3738 bellard
    
524 19cb3738 bellard
    return kernResult;
525 19cb3738 bellard
}
526 19cb3738 bellard
527 19cb3738 bellard
kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
528 19cb3738 bellard
{
529 19cb3738 bellard
    io_object_t     nextMedia;
530 19cb3738 bellard
    kern_return_t   kernResult = KERN_FAILURE;
531 19cb3738 bellard
    *bsdPath = '\0';
532 19cb3738 bellard
    nextMedia = IOIteratorNext( mediaIterator );
533 19cb3738 bellard
    if ( nextMedia )
534 19cb3738 bellard
    {
535 19cb3738 bellard
        CFTypeRef   bsdPathAsCFString;
536 19cb3738 bellard
    bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
537 19cb3738 bellard
        if ( bsdPathAsCFString ) {
538 19cb3738 bellard
            size_t devPathLength;
539 19cb3738 bellard
            strcpy( bsdPath, _PATH_DEV );
540 19cb3738 bellard
            strcat( bsdPath, "r" );
541 19cb3738 bellard
            devPathLength = strlen( bsdPath );
542 19cb3738 bellard
            if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
543 19cb3738 bellard
                kernResult = KERN_SUCCESS;
544 19cb3738 bellard
            }
545 19cb3738 bellard
            CFRelease( bsdPathAsCFString );
546 19cb3738 bellard
        }
547 19cb3738 bellard
        IOObjectRelease( nextMedia );
548 19cb3738 bellard
    }
549 19cb3738 bellard
    
550 19cb3738 bellard
    return kernResult;
551 19cb3738 bellard
}
552 19cb3738 bellard
553 19cb3738 bellard
#endif
554 19cb3738 bellard
555 19cb3738 bellard
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
556 19cb3738 bellard
{
557 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
558 19cb3738 bellard
    int fd, open_flags, ret;
559 19cb3738 bellard
560 19cb3738 bellard
#ifdef CONFIG_COCOA
561 19cb3738 bellard
    if (strstart(filename, "/dev/cdrom", NULL)) {
562 19cb3738 bellard
        kern_return_t kernResult;
563 19cb3738 bellard
        io_iterator_t mediaIterator;
564 19cb3738 bellard
        char bsdPath[ MAXPATHLEN ];
565 19cb3738 bellard
        int fd;
566 19cb3738 bellard
 
567 19cb3738 bellard
        kernResult = FindEjectableCDMedia( &mediaIterator );
568 19cb3738 bellard
        kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
569 19cb3738 bellard
    
570 19cb3738 bellard
        if ( bsdPath[ 0 ] != '\0' ) {
571 19cb3738 bellard
            strcat(bsdPath,"s0");
572 19cb3738 bellard
            /* some CDs don't have a partition 0 */
573 19cb3738 bellard
            fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
574 19cb3738 bellard
            if (fd < 0) {
575 19cb3738 bellard
                bsdPath[strlen(bsdPath)-1] = '1';
576 19cb3738 bellard
            } else {
577 19cb3738 bellard
                close(fd);
578 19cb3738 bellard
            }
579 19cb3738 bellard
            filename = bsdPath;
580 19cb3738 bellard
        }
581 19cb3738 bellard
        
582 19cb3738 bellard
        if ( mediaIterator )
583 19cb3738 bellard
            IOObjectRelease( mediaIterator );
584 19cb3738 bellard
    }
585 19cb3738 bellard
#endif
586 19cb3738 bellard
    open_flags = O_BINARY;
587 19cb3738 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
588 19cb3738 bellard
        open_flags |= O_RDWR;
589 19cb3738 bellard
    } else {
590 19cb3738 bellard
        open_flags |= O_RDONLY;
591 19cb3738 bellard
        bs->read_only = 1;
592 19cb3738 bellard
    }
593 19cb3738 bellard
594 19cb3738 bellard
    s->type = FTYPE_FILE;
595 19cb3738 bellard
#if defined(__linux__)
596 19cb3738 bellard
    if (strstart(filename, "/dev/cd", NULL)) {
597 19cb3738 bellard
        /* open will not fail even if no CD is inserted */
598 19cb3738 bellard
        open_flags |= O_NONBLOCK;
599 19cb3738 bellard
        s->type = FTYPE_CD;
600 19cb3738 bellard
    } else if (strstart(filename, "/dev/fd", NULL)) {
601 19cb3738 bellard
        s->type = FTYPE_FD;
602 19cb3738 bellard
        s->fd_open_flags = open_flags;
603 19cb3738 bellard
        /* open will not fail even if no floppy is inserted */
604 19cb3738 bellard
        open_flags |= O_NONBLOCK;
605 19cb3738 bellard
    }
606 19cb3738 bellard
#endif
607 19cb3738 bellard
    fd = open(filename, open_flags, 0644);
608 19cb3738 bellard
    if (fd < 0) {
609 19cb3738 bellard
        ret = -errno;
610 19cb3738 bellard
        if (ret == -EROFS)
611 19cb3738 bellard
            ret = -EACCES;
612 19cb3738 bellard
        return ret;
613 19cb3738 bellard
    }
614 19cb3738 bellard
    s->fd = fd;
615 19cb3738 bellard
#if defined(__linux__)
616 19cb3738 bellard
    /* close fd so that we can reopen it as needed */
617 19cb3738 bellard
    if (s->type == FTYPE_FD) {
618 19cb3738 bellard
        close(s->fd);
619 19cb3738 bellard
        s->fd = -1;
620 19cb3738 bellard
        s->fd_media_changed = 1;
621 19cb3738 bellard
    }
622 19cb3738 bellard
#endif
623 19cb3738 bellard
    return 0;
624 19cb3738 bellard
}
625 19cb3738 bellard
626 19cb3738 bellard
#if defined(__linux__) && !defined(QEMU_TOOL)
627 19cb3738 bellard
628 19cb3738 bellard
/* Note: we do not have a reliable method to detect if the floppy is
629 19cb3738 bellard
   present. The current method is to try to open the floppy at every
630 19cb3738 bellard
   I/O and to keep it opened during a few hundreds of ms. */
631 19cb3738 bellard
static int fd_open(BlockDriverState *bs)
632 19cb3738 bellard
{
633 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
634 19cb3738 bellard
    int last_media_present;
635 19cb3738 bellard
636 19cb3738 bellard
    if (s->type != FTYPE_FD)
637 19cb3738 bellard
        return 0;
638 19cb3738 bellard
    last_media_present = (s->fd >= 0);
639 19cb3738 bellard
    if (s->fd >= 0 && 
640 19cb3738 bellard
        (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) {
641 19cb3738 bellard
        close(s->fd);
642 19cb3738 bellard
        s->fd = -1;
643 19cb3738 bellard
#ifdef DEBUG_FLOPPY
644 19cb3738 bellard
        printf("Floppy closed\n");
645 19cb3738 bellard
#endif
646 19cb3738 bellard
    }
647 19cb3738 bellard
    if (s->fd < 0) {
648 19cb3738 bellard
        if (s->fd_got_error && 
649 19cb3738 bellard
            (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) {
650 19cb3738 bellard
#ifdef DEBUG_FLOPPY
651 19cb3738 bellard
            printf("No floppy (open delayed)\n");
652 19cb3738 bellard
#endif
653 19cb3738 bellard
            return -EIO;
654 19cb3738 bellard
        }
655 19cb3738 bellard
        s->fd = open(bs->filename, s->fd_open_flags);
656 19cb3738 bellard
        if (s->fd < 0) {
657 19cb3738 bellard
            s->fd_error_time = qemu_get_clock(rt_clock);
658 19cb3738 bellard
            s->fd_got_error = 1;
659 19cb3738 bellard
            if (last_media_present)
660 19cb3738 bellard
                s->fd_media_changed = 1;
661 19cb3738 bellard
#ifdef DEBUG_FLOPPY
662 19cb3738 bellard
            printf("No floppy\n");
663 19cb3738 bellard
#endif
664 19cb3738 bellard
            return -EIO;
665 19cb3738 bellard
        }
666 19cb3738 bellard
#ifdef DEBUG_FLOPPY
667 19cb3738 bellard
        printf("Floppy opened\n");
668 19cb3738 bellard
#endif
669 19cb3738 bellard
    }
670 19cb3738 bellard
    if (!last_media_present)
671 19cb3738 bellard
        s->fd_media_changed = 1;
672 19cb3738 bellard
    s->fd_open_time = qemu_get_clock(rt_clock);
673 19cb3738 bellard
    s->fd_got_error = 0;
674 19cb3738 bellard
    return 0;
675 19cb3738 bellard
}
676 19cb3738 bellard
#else
677 19cb3738 bellard
static int fd_open(BlockDriverState *bs)
678 19cb3738 bellard
{
679 19cb3738 bellard
    return 0;
680 19cb3738 bellard
}
681 19cb3738 bellard
#endif
682 19cb3738 bellard
683 19cb3738 bellard
#if defined(__linux__)
684 19cb3738 bellard
685 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
686 19cb3738 bellard
{
687 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
688 19cb3738 bellard
    int ret;
689 19cb3738 bellard
690 19cb3738 bellard
    switch(s->type) {
691 19cb3738 bellard
    case FTYPE_CD:
692 19cb3738 bellard
        ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
693 19cb3738 bellard
        if (ret == CDS_DISC_OK)
694 19cb3738 bellard
            return 1;
695 19cb3738 bellard
        else
696 19cb3738 bellard
            return 0;
697 19cb3738 bellard
        break;
698 19cb3738 bellard
    case FTYPE_FD:
699 19cb3738 bellard
        ret = fd_open(bs);
700 19cb3738 bellard
        return (ret >= 0);
701 19cb3738 bellard
    default:
702 19cb3738 bellard
        return 1;
703 19cb3738 bellard
    }
704 19cb3738 bellard
}
705 19cb3738 bellard
706 19cb3738 bellard
/* currently only used by fdc.c, but a CD version would be good too */
707 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
708 19cb3738 bellard
{
709 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
710 19cb3738 bellard
711 19cb3738 bellard
    switch(s->type) {
712 19cb3738 bellard
    case FTYPE_FD:
713 19cb3738 bellard
        {
714 19cb3738 bellard
            int ret;
715 19cb3738 bellard
            /* XXX: we do not have a true media changed indication. It
716 19cb3738 bellard
               does not work if the floppy is changed without trying
717 19cb3738 bellard
               to read it */
718 19cb3738 bellard
            fd_open(bs);
719 19cb3738 bellard
            ret = s->fd_media_changed;
720 19cb3738 bellard
            s->fd_media_changed = 0;
721 19cb3738 bellard
#ifdef DEBUG_FLOPPY
722 19cb3738 bellard
            printf("Floppy changed=%d\n", ret);
723 19cb3738 bellard
#endif
724 19cb3738 bellard
            return ret;
725 19cb3738 bellard
        }
726 19cb3738 bellard
    default:
727 19cb3738 bellard
        return -ENOTSUP;
728 19cb3738 bellard
    }
729 19cb3738 bellard
}
730 19cb3738 bellard
731 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
732 19cb3738 bellard
{
733 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
734 19cb3738 bellard
735 19cb3738 bellard
    switch(s->type) {
736 19cb3738 bellard
    case FTYPE_CD:
737 19cb3738 bellard
        if (eject_flag) {
738 19cb3738 bellard
            if (ioctl (s->fd, CDROMEJECT, NULL) < 0)
739 19cb3738 bellard
                perror("CDROMEJECT");
740 19cb3738 bellard
        } else {
741 19cb3738 bellard
            if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0)
742 19cb3738 bellard
                perror("CDROMEJECT");
743 19cb3738 bellard
        }
744 19cb3738 bellard
        break;
745 19cb3738 bellard
    case FTYPE_FD:
746 19cb3738 bellard
        {
747 19cb3738 bellard
            int fd;
748 19cb3738 bellard
            if (s->fd >= 0) {
749 19cb3738 bellard
                close(s->fd);
750 19cb3738 bellard
                s->fd = -1;
751 19cb3738 bellard
            }
752 19cb3738 bellard
            fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK);
753 19cb3738 bellard
            if (fd >= 0) {
754 19cb3738 bellard
                if (ioctl(fd, FDEJECT, 0) < 0)
755 19cb3738 bellard
                    perror("FDEJECT");
756 19cb3738 bellard
                close(fd);
757 19cb3738 bellard
            }
758 19cb3738 bellard
        }
759 19cb3738 bellard
        break;
760 19cb3738 bellard
    default:
761 19cb3738 bellard
        return -ENOTSUP;
762 19cb3738 bellard
    }
763 19cb3738 bellard
    return 0;
764 19cb3738 bellard
}
765 19cb3738 bellard
766 19cb3738 bellard
static int raw_set_locked(BlockDriverState *bs, int locked)
767 19cb3738 bellard
{
768 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
769 19cb3738 bellard
770 19cb3738 bellard
    switch(s->type) {
771 19cb3738 bellard
    case FTYPE_CD:
772 19cb3738 bellard
        if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) {
773 19cb3738 bellard
            /* Note: an error can happen if the distribution automatically
774 19cb3738 bellard
               mounts the CD-ROM */
775 19cb3738 bellard
            //        perror("CDROM_LOCKDOOR");
776 19cb3738 bellard
        }
777 19cb3738 bellard
        break;
778 19cb3738 bellard
    default:
779 19cb3738 bellard
        return -ENOTSUP;
780 19cb3738 bellard
    }
781 19cb3738 bellard
    return 0;
782 19cb3738 bellard
}
783 19cb3738 bellard
784 19cb3738 bellard
#else
785 19cb3738 bellard
786 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
787 19cb3738 bellard
{
788 19cb3738 bellard
    return 1;
789 19cb3738 bellard
}
790 19cb3738 bellard
791 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
792 19cb3738 bellard
{
793 19cb3738 bellard
    return -ENOTSUP;
794 19cb3738 bellard
}
795 19cb3738 bellard
796 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
797 19cb3738 bellard
{
798 19cb3738 bellard
    return -ENOTSUP;
799 19cb3738 bellard
}
800 19cb3738 bellard
801 19cb3738 bellard
static int raw_set_locked(BlockDriverState *bs, int locked)
802 19cb3738 bellard
{
803 19cb3738 bellard
    return -ENOTSUP;
804 19cb3738 bellard
}
805 19cb3738 bellard
806 19cb3738 bellard
#endif /* !linux */
807 19cb3738 bellard
808 19cb3738 bellard
BlockDriver bdrv_host_device = {
809 19cb3738 bellard
    "host_device",
810 19cb3738 bellard
    sizeof(BDRVRawState),
811 19cb3738 bellard
    NULL, /* no probe for protocols */
812 19cb3738 bellard
    hdev_open,
813 19cb3738 bellard
    NULL,
814 19cb3738 bellard
    NULL,
815 19cb3738 bellard
    raw_close,
816 19cb3738 bellard
    NULL,
817 19cb3738 bellard
    raw_flush,
818 19cb3738 bellard
    
819 19cb3738 bellard
    .bdrv_aio_read = raw_aio_read,
820 19cb3738 bellard
    .bdrv_aio_write = raw_aio_write,
821 19cb3738 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
822 19cb3738 bellard
    .aiocb_size = sizeof(RawAIOCB),
823 19cb3738 bellard
    .bdrv_pread = raw_pread,
824 19cb3738 bellard
    .bdrv_pwrite = raw_pwrite,
825 19cb3738 bellard
    .bdrv_getlength = raw_getlength,
826 19cb3738 bellard
827 19cb3738 bellard
    /* removable device support */
828 19cb3738 bellard
    .bdrv_is_inserted = raw_is_inserted,
829 19cb3738 bellard
    .bdrv_media_changed = raw_media_changed,
830 19cb3738 bellard
    .bdrv_eject = raw_eject,
831 19cb3738 bellard
    .bdrv_set_locked = raw_set_locked,
832 19cb3738 bellard
};
833 19cb3738 bellard
834 83f64091 bellard
#else /* _WIN32 */
835 83f64091 bellard
836 83f64091 bellard
/* XXX: use another file ? */
837 83f64091 bellard
#include <winioctl.h>
838 83f64091 bellard
839 19cb3738 bellard
#define FTYPE_FILE 0
840 19cb3738 bellard
#define FTYPE_CD     1
841 01781963 bellard
#define FTYPE_HARDDISK 2
842 19cb3738 bellard
843 83f64091 bellard
typedef struct BDRVRawState {
844 83f64091 bellard
    HANDLE hfile;
845 19cb3738 bellard
    int type;
846 f45512fe bellard
    char drive_path[16]; /* format: "d:\" */
847 83f64091 bellard
} BDRVRawState;
848 83f64091 bellard
849 83f64091 bellard
typedef struct RawAIOCB {
850 ce1a14dc pbrook
    BlockDriverAIOCB common;
851 83f64091 bellard
    HANDLE hEvent;
852 83f64091 bellard
    OVERLAPPED ov;
853 83f64091 bellard
    int count;
854 83f64091 bellard
} RawAIOCB;
855 83f64091 bellard
856 83f64091 bellard
int qemu_ftruncate64(int fd, int64_t length)
857 83f64091 bellard
{
858 83f64091 bellard
    LARGE_INTEGER li;
859 83f64091 bellard
    LONG high;
860 83f64091 bellard
    HANDLE h;
861 83f64091 bellard
    BOOL res;
862 83f64091 bellard
863 83f64091 bellard
    if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
864 83f64091 bellard
        return -1;
865 83f64091 bellard
866 83f64091 bellard
    h = (HANDLE)_get_osfhandle(fd);
867 83f64091 bellard
868 83f64091 bellard
    /* get current position, ftruncate do not change position */
869 83f64091 bellard
    li.HighPart = 0;
870 83f64091 bellard
    li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
871 83f64091 bellard
    if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
872 83f64091 bellard
        return -1;
873 83f64091 bellard
874 83f64091 bellard
    high = length >> 32;
875 83f64091 bellard
    if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
876 83f64091 bellard
        return -1;
877 83f64091 bellard
    res = SetEndOfFile(h);
878 83f64091 bellard
879 83f64091 bellard
    /* back to old position */
880 83f64091 bellard
    SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
881 83f64091 bellard
    return res ? 0 : -1;
882 83f64091 bellard
}
883 83f64091 bellard
884 83f64091 bellard
static int set_sparse(int fd)
885 83f64091 bellard
{
886 83f64091 bellard
    DWORD returned;
887 83f64091 bellard
    return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
888 83f64091 bellard
                                 NULL, 0, NULL, 0, &returned, NULL);
889 83f64091 bellard
}
890 83f64091 bellard
891 83f64091 bellard
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
892 83f64091 bellard
{
893 83f64091 bellard
    BDRVRawState *s = bs->opaque;
894 83f64091 bellard
    int access_flags, create_flags;
895 ce1a14dc pbrook
    DWORD overlapped;
896 19cb3738 bellard
897 f45512fe bellard
    s->type = FTYPE_FILE;
898 83f64091 bellard
899 83f64091 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
900 83f64091 bellard
        access_flags = GENERIC_READ | GENERIC_WRITE;
901 83f64091 bellard
    } else {
902 83f64091 bellard
        access_flags = GENERIC_READ;
903 83f64091 bellard
    }
904 979b67ad bellard
    if (flags & BDRV_O_CREAT) {
905 83f64091 bellard
        create_flags = CREATE_ALWAYS;
906 83f64091 bellard
    } else {
907 83f64091 bellard
        create_flags = OPEN_EXISTING;
908 83f64091 bellard
    }
909 ce1a14dc pbrook
#ifdef QEMU_TOOL
910 3b9f94e1 bellard
    overlapped = FILE_ATTRIBUTE_NORMAL;
911 ce1a14dc pbrook
#else
912 ce1a14dc pbrook
    overlapped = FILE_FLAG_OVERLAPPED;
913 ce1a14dc pbrook
#endif
914 83f64091 bellard
    s->hfile = CreateFile(filename, access_flags, 
915 83f64091 bellard
                          FILE_SHARE_READ, NULL,
916 3b9f94e1 bellard
                          create_flags, overlapped, NULL);
917 54421cb1 ths
    if (s->hfile == INVALID_HANDLE_VALUE) {
918 54421cb1 ths
        int err = GetLastError();
919 54421cb1 ths
920 54421cb1 ths
        if (err == ERROR_ACCESS_DENIED)
921 54421cb1 ths
            return -EACCES;
922 83f64091 bellard
        return -1;
923 54421cb1 ths
    }
924 83f64091 bellard
    return 0;
925 83f64091 bellard
}
926 83f64091 bellard
927 83f64091 bellard
static int raw_pread(BlockDriverState *bs, int64_t offset, 
928 83f64091 bellard
                     uint8_t *buf, int count)
929 83f64091 bellard
{
930 83f64091 bellard
    BDRVRawState *s = bs->opaque;
931 83f64091 bellard
    OVERLAPPED ov;
932 83f64091 bellard
    DWORD ret_count;
933 83f64091 bellard
    int ret;
934 83f64091 bellard
    
935 83f64091 bellard
    memset(&ov, 0, sizeof(ov));
936 83f64091 bellard
    ov.Offset = offset;
937 83f64091 bellard
    ov.OffsetHigh = offset >> 32;
938 436e15b8 bellard
    ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
939 436e15b8 bellard
    if (!ret) {
940 436e15b8 bellard
        ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
941 436e15b8 bellard
        if (!ret)
942 436e15b8 bellard
            return -EIO;
943 436e15b8 bellard
        else
944 436e15b8 bellard
            return ret_count;
945 436e15b8 bellard
    }
946 83f64091 bellard
    return ret_count;
947 83f64091 bellard
}
948 83f64091 bellard
949 83f64091 bellard
static int raw_pwrite(BlockDriverState *bs, int64_t offset, 
950 83f64091 bellard
                      const uint8_t *buf, int count)
951 83f64091 bellard
{
952 83f64091 bellard
    BDRVRawState *s = bs->opaque;
953 83f64091 bellard
    OVERLAPPED ov;
954 83f64091 bellard
    DWORD ret_count;
955 83f64091 bellard
    int ret;
956 83f64091 bellard
    
957 83f64091 bellard
    memset(&ov, 0, sizeof(ov));
958 83f64091 bellard
    ov.Offset = offset;
959 83f64091 bellard
    ov.OffsetHigh = offset >> 32;
960 436e15b8 bellard
    ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
961 436e15b8 bellard
    if (!ret) {
962 436e15b8 bellard
        ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
963 436e15b8 bellard
        if (!ret)
964 436e15b8 bellard
            return -EIO;
965 436e15b8 bellard
        else
966 436e15b8 bellard
            return ret_count;
967 436e15b8 bellard
    }
968 83f64091 bellard
    return ret_count;
969 83f64091 bellard
}
970 83f64091 bellard
971 3b9f94e1 bellard
#if 0
972 436e15b8 bellard
#ifndef QEMU_TOOL
973 83f64091 bellard
static void raw_aio_cb(void *opaque)
974 83f64091 bellard
{
975 ce1a14dc pbrook
    RawAIOCB *acb = opaque;
976 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
977 979b67ad bellard
    BDRVRawState *s = bs->opaque;
978 83f64091 bellard
    DWORD ret_count;
979 83f64091 bellard
    int ret;
980 83f64091 bellard

981 ce1a14dc pbrook
    ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
982 ce1a14dc pbrook
    if (!ret || ret_count != acb->count) {
983 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, -EIO);
984 83f64091 bellard
    } else {
985 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, 0);
986 83f64091 bellard
    }
987 83f64091 bellard
}
988 436e15b8 bellard
#endif
989 ce1a14dc pbrook
990 ce1a14dc pbrook
static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
991 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
992 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
993 83f64091 bellard
{
994 ce1a14dc pbrook
    RawAIOCB *acb;
995 83f64091 bellard
    int64_t offset;
996 83f64091 bellard
997 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
998 ce1a14dc pbrook
    if (acb->hEvent) {
999 ce1a14dc pbrook
        acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1000 ce1a14dc pbrook
        if (!acb->hEvent) {
1001 ce1a14dc pbrook
            qemu_aio_release(acb);
1002 ce1a14dc pbrook
            return NULL;
1003 ce1a14dc pbrook
        }
1004 ce1a14dc pbrook
    }
1005 ce1a14dc pbrook
    memset(&acb->ov, 0, sizeof(acb->ov));
1006 83f64091 bellard
    offset = sector_num * 512;
1007 ce1a14dc pbrook
    acb->ov.Offset = offset;
1008 ce1a14dc pbrook
    acb->ov.OffsetHigh = offset >> 32;
1009 ce1a14dc pbrook
    acb->ov.hEvent = acb->hEvent;
1010 ce1a14dc pbrook
    acb->count = nb_sectors * 512;
1011 436e15b8 bellard
#ifndef QEMU_TOOL
1012 ce1a14dc pbrook
    qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1013 436e15b8 bellard
#endif
1014 ce1a14dc pbrook
    return acb;
1015 83f64091 bellard
}
1016 83f64091 bellard
1017 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
1018 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1019 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1020 83f64091 bellard
{
1021 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1022 ce1a14dc pbrook
    RawAIOCB *acb;
1023 83f64091 bellard
    int ret;
1024 83f64091 bellard
1025 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1026 ce1a14dc pbrook
    if (!acb)
1027 ce1a14dc pbrook
        return NULL;
1028 ce1a14dc pbrook
    ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1029 ce1a14dc pbrook
    if (!ret) {
1030 ce1a14dc pbrook
        qemu_aio_release(acb);
1031 ce1a14dc pbrook
        return NULL;
1032 ce1a14dc pbrook
    }
1033 ce1a14dc pbrook
#ifdef QEMU_TOOL
1034 ce1a14dc pbrook
    qemu_aio_release(acb);
1035 436e15b8 bellard
#endif
1036 ce1a14dc pbrook
    return (BlockDriverAIOCB *)acb;
1037 83f64091 bellard
}
1038 83f64091 bellard
1039 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1040 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1041 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1042 83f64091 bellard
{
1043 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1044 ce1a14dc pbrook
    RawAIOCB *acb;
1045 ce1a14dc pbrook
    int ret;
1046 83f64091 bellard
1047 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1048 ce1a14dc pbrook
    if (!acb)
1049 ce1a14dc pbrook
        return NULL;
1050 ce1a14dc pbrook
    ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1051 ce1a14dc pbrook
    if (!ret) {
1052 ce1a14dc pbrook
        qemu_aio_release(acb);
1053 ce1a14dc pbrook
        return NULL;
1054 ce1a14dc pbrook
    }
1055 ce1a14dc pbrook
#ifdef QEMU_TOOL
1056 ce1a14dc pbrook
    qemu_aio_release(acb);
1057 436e15b8 bellard
#endif
1058 ce1a14dc pbrook
    return (BlockDriverAIOCB *)acb;
1059 83f64091 bellard
}
1060 83f64091 bellard
1061 ce1a14dc pbrook
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
1062 83f64091 bellard
{
1063 ce1a14dc pbrook
#ifndef QEMU_TOOL
1064 ce1a14dc pbrook
    RawAIOCB *acb = (RawAIOCB *)blockacb;
1065 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
1066 ce1a14dc pbrook
    BDRVRawState *s = bs->opaque;
1067 ce1a14dc pbrook
1068 ce1a14dc pbrook
    qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1069 ce1a14dc pbrook
    /* XXX: if more than one async I/O it is not correct */
1070 ce1a14dc pbrook
    CancelIo(s->hfile);
1071 ce1a14dc pbrook
    qemu_aio_release(acb);
1072 ce1a14dc pbrook
#endif
1073 83f64091 bellard
}
1074 3b9f94e1 bellard
#endif /* #if 0 */
1075 83f64091 bellard
1076 83f64091 bellard
static void raw_flush(BlockDriverState *bs)
1077 83f64091 bellard
{
1078 3b9f94e1 bellard
    BDRVRawState *s = bs->opaque;
1079 3b9f94e1 bellard
    FlushFileBuffers(s->hfile);
1080 83f64091 bellard
}
1081 83f64091 bellard
1082 83f64091 bellard
static void raw_close(BlockDriverState *bs)
1083 83f64091 bellard
{
1084 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1085 83f64091 bellard
    CloseHandle(s->hfile);
1086 83f64091 bellard
}
1087 83f64091 bellard
1088 83f64091 bellard
static int raw_truncate(BlockDriverState *bs, int64_t offset)
1089 83f64091 bellard
{
1090 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1091 83f64091 bellard
    DWORD low, high;
1092 83f64091 bellard
1093 979b67ad bellard
    low = offset;
1094 979b67ad bellard
    high = offset >> 32;
1095 83f64091 bellard
    if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1096 83f64091 bellard
        return -EIO;
1097 83f64091 bellard
    if (!SetEndOfFile(s->hfile))
1098 83f64091 bellard
        return -EIO;
1099 83f64091 bellard
    return 0;
1100 83f64091 bellard
}
1101 83f64091 bellard
1102 f45512fe bellard
static int64_t raw_getlength(BlockDriverState *bs)
1103 83f64091 bellard
{
1104 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1105 83f64091 bellard
    LARGE_INTEGER l;
1106 19cb3738 bellard
    ULARGE_INTEGER available, total, total_free; 
1107 01781963 bellard
    DISK_GEOMETRY dg;
1108 01781963 bellard
    DWORD count;
1109 01781963 bellard
    BOOL status;
1110 436e15b8 bellard
1111 f45512fe bellard
    switch(s->type) {
1112 19cb3738 bellard
    case FTYPE_FILE:
1113 19cb3738 bellard
        l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1114 19cb3738 bellard
        if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1115 19cb3738 bellard
            return -EIO;
1116 19cb3738 bellard
        break;
1117 19cb3738 bellard
    case FTYPE_CD:
1118 f45512fe bellard
        if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
1119 19cb3738 bellard
            return -EIO;
1120 f45512fe bellard
        l.QuadPart = total.QuadPart;
1121 19cb3738 bellard
        break;
1122 01781963 bellard
    case FTYPE_HARDDISK:
1123 01781963 bellard
        status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY,
1124 01781963 bellard
                                 NULL, 0, &dg, sizeof(dg), &count, NULL);
1125 01781963 bellard
        if (status != FALSE) {
1126 01781963 bellard
            l.QuadPart = dg.Cylinders.QuadPart * dg.TracksPerCylinder
1127 01781963 bellard
                * dg.SectorsPerTrack * dg.BytesPerSector;
1128 01781963 bellard
        }
1129 01781963 bellard
        break;
1130 19cb3738 bellard
    default:
1131 19cb3738 bellard
        return -EIO;
1132 19cb3738 bellard
    }
1133 83f64091 bellard
    return l.QuadPart;
1134 83f64091 bellard
}
1135 83f64091 bellard
1136 83f64091 bellard
static int raw_create(const char *filename, int64_t total_size,
1137 83f64091 bellard
                      const char *backing_file, int flags)
1138 83f64091 bellard
{
1139 83f64091 bellard
    int fd;
1140 83f64091 bellard
1141 83f64091 bellard
    if (flags || backing_file)
1142 83f64091 bellard
        return -ENOTSUP;
1143 83f64091 bellard
1144 83f64091 bellard
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 
1145 83f64091 bellard
              0644);
1146 83f64091 bellard
    if (fd < 0)
1147 83f64091 bellard
        return -EIO;
1148 83f64091 bellard
    set_sparse(fd);
1149 83f64091 bellard
    ftruncate(fd, total_size * 512);
1150 83f64091 bellard
    close(fd);
1151 83f64091 bellard
    return 0;
1152 83f64091 bellard
}
1153 83f64091 bellard
1154 83f64091 bellard
void qemu_aio_init(void)
1155 83f64091 bellard
{
1156 83f64091 bellard
}
1157 83f64091 bellard
1158 83f64091 bellard
void qemu_aio_poll(void)
1159 83f64091 bellard
{
1160 83f64091 bellard
}
1161 83f64091 bellard
1162 62ee0211 ths
void qemu_aio_flush(void)
1163 62ee0211 ths
{
1164 62ee0211 ths
}
1165 62ee0211 ths
1166 83f64091 bellard
void qemu_aio_wait_start(void)
1167 83f64091 bellard
{
1168 83f64091 bellard
}
1169 83f64091 bellard
1170 83f64091 bellard
void qemu_aio_wait(void)
1171 83f64091 bellard
{
1172 fd44d818 bellard
#ifndef QEMU_TOOL
1173 fd44d818 bellard
    qemu_bh_poll();
1174 fd44d818 bellard
#endif
1175 83f64091 bellard
}
1176 83f64091 bellard
1177 83f64091 bellard
void qemu_aio_wait_end(void)
1178 83f64091 bellard
{
1179 83f64091 bellard
}
1180 83f64091 bellard
1181 83f64091 bellard
BlockDriver bdrv_raw = {
1182 83f64091 bellard
    "raw",
1183 83f64091 bellard
    sizeof(BDRVRawState),
1184 83f64091 bellard
    NULL, /* no probe for protocols */
1185 83f64091 bellard
    raw_open,
1186 83f64091 bellard
    NULL,
1187 83f64091 bellard
    NULL,
1188 83f64091 bellard
    raw_close,
1189 83f64091 bellard
    raw_create,
1190 83f64091 bellard
    raw_flush,
1191 83f64091 bellard
    
1192 83f64091 bellard
#if 0
1193 83f64091 bellard
    .bdrv_aio_read = raw_aio_read,
1194 83f64091 bellard
    .bdrv_aio_write = raw_aio_write,
1195 83f64091 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
1196 ce1a14dc pbrook
    .aiocb_size = sizeof(RawAIOCB);
1197 83f64091 bellard
#endif
1198 83f64091 bellard
    .protocol_name = "file",
1199 83f64091 bellard
    .bdrv_pread = raw_pread,
1200 83f64091 bellard
    .bdrv_pwrite = raw_pwrite,
1201 83f64091 bellard
    .bdrv_truncate = raw_truncate,
1202 83f64091 bellard
    .bdrv_getlength = raw_getlength,
1203 83f64091 bellard
};
1204 19cb3738 bellard
1205 19cb3738 bellard
/***********************************************/
1206 19cb3738 bellard
/* host device */
1207 19cb3738 bellard
1208 19cb3738 bellard
static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1209 19cb3738 bellard
{
1210 19cb3738 bellard
    char drives[256], *pdrv = drives;
1211 19cb3738 bellard
    UINT type;
1212 19cb3738 bellard
1213 f45512fe bellard
    memset(drives, 0, sizeof(drives));
1214 19cb3738 bellard
    GetLogicalDriveStrings(sizeof(drives), drives);
1215 19cb3738 bellard
    while(pdrv[0] != '\0') {
1216 19cb3738 bellard
        type = GetDriveType(pdrv);
1217 19cb3738 bellard
        switch(type) {
1218 19cb3738 bellard
        case DRIVE_CDROM:
1219 19cb3738 bellard
            snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1220 19cb3738 bellard
            return 0;
1221 19cb3738 bellard
            break;
1222 19cb3738 bellard
        }
1223 19cb3738 bellard
        pdrv += lstrlen(pdrv) + 1;
1224 19cb3738 bellard
    }
1225 19cb3738 bellard
    return -1;
1226 19cb3738 bellard
}
1227 19cb3738 bellard
1228 f45512fe bellard
static int find_device_type(BlockDriverState *bs, const char *filename)
1229 19cb3738 bellard
{
1230 f45512fe bellard
    BDRVRawState *s = bs->opaque;
1231 19cb3738 bellard
    UINT type;
1232 19cb3738 bellard
    const char *p;
1233 19cb3738 bellard
1234 19cb3738 bellard
    if (strstart(filename, "\\\\.\\", &p) ||
1235 19cb3738 bellard
        strstart(filename, "//./", &p)) {
1236 01781963 bellard
        if (stristart(p, "PhysicalDrive", NULL))
1237 01781963 bellard
            return FTYPE_HARDDISK;
1238 f45512fe bellard
        snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
1239 f45512fe bellard
        type = GetDriveType(s->drive_path);
1240 19cb3738 bellard
        if (type == DRIVE_CDROM)
1241 19cb3738 bellard
            return FTYPE_CD;
1242 19cb3738 bellard
        else
1243 19cb3738 bellard
            return FTYPE_FILE;
1244 19cb3738 bellard
    } else {
1245 19cb3738 bellard
        return FTYPE_FILE;
1246 19cb3738 bellard
    }
1247 19cb3738 bellard
}
1248 19cb3738 bellard
1249 19cb3738 bellard
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1250 19cb3738 bellard
{
1251 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
1252 19cb3738 bellard
    int access_flags, create_flags;
1253 19cb3738 bellard
    DWORD overlapped;
1254 19cb3738 bellard
    char device_name[64];
1255 19cb3738 bellard
1256 19cb3738 bellard
    if (strstart(filename, "/dev/cdrom", NULL)) {
1257 19cb3738 bellard
        if (find_cdrom(device_name, sizeof(device_name)) < 0)
1258 19cb3738 bellard
            return -ENOENT;
1259 19cb3738 bellard
        filename = device_name;
1260 19cb3738 bellard
    } else {
1261 19cb3738 bellard
        /* transform drive letters into device name */
1262 19cb3738 bellard
        if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1263 19cb3738 bellard
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1264 19cb3738 bellard
            filename[1] == ':' && filename[2] == '\0') {
1265 19cb3738 bellard
            snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1266 19cb3738 bellard
            filename = device_name;
1267 19cb3738 bellard
        }
1268 19cb3738 bellard
    }
1269 f45512fe bellard
    s->type = find_device_type(bs, filename);
1270 01781963 bellard
    
1271 19cb3738 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1272 19cb3738 bellard
        access_flags = GENERIC_READ | GENERIC_WRITE;
1273 19cb3738 bellard
    } else {
1274 19cb3738 bellard
        access_flags = GENERIC_READ;
1275 19cb3738 bellard
    }
1276 19cb3738 bellard
    create_flags = OPEN_EXISTING;
1277 19cb3738 bellard
1278 19cb3738 bellard
#ifdef QEMU_TOOL
1279 3b9f94e1 bellard
    overlapped = FILE_ATTRIBUTE_NORMAL;
1280 19cb3738 bellard
#else
1281 19cb3738 bellard
    overlapped = FILE_FLAG_OVERLAPPED;
1282 19cb3738 bellard
#endif
1283 19cb3738 bellard
    s->hfile = CreateFile(filename, access_flags, 
1284 19cb3738 bellard
                          FILE_SHARE_READ, NULL,
1285 3b9f94e1 bellard
                          create_flags, overlapped, NULL);
1286 54421cb1 ths
    if (s->hfile == INVALID_HANDLE_VALUE) {
1287 54421cb1 ths
        int err = GetLastError();
1288 54421cb1 ths
1289 54421cb1 ths
        if (err == ERROR_ACCESS_DENIED)
1290 54421cb1 ths
            return -EACCES;
1291 19cb3738 bellard
        return -1;
1292 54421cb1 ths
    }
1293 19cb3738 bellard
    return 0;
1294 19cb3738 bellard
}
1295 19cb3738 bellard
1296 19cb3738 bellard
#if 0
1297 19cb3738 bellard
/***********************************************/
1298 19cb3738 bellard
/* removable device additionnal commands */
1299 19cb3738 bellard

1300 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
1301 19cb3738 bellard
{
1302 19cb3738 bellard
    return 1;
1303 19cb3738 bellard
}
1304 19cb3738 bellard

1305 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
1306 19cb3738 bellard
{
1307 19cb3738 bellard
    return -ENOTSUP;
1308 19cb3738 bellard
}
1309 19cb3738 bellard

1310 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
1311 19cb3738 bellard
{
1312 19cb3738 bellard
    DWORD ret_count;
1313 19cb3738 bellard

1314 19cb3738 bellard
    if (s->type == FTYPE_FILE)
1315 19cb3738 bellard
        return -ENOTSUP;
1316 19cb3738 bellard
    if (eject_flag) {
1317 19cb3738 bellard
        DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA, 
1318 19cb3738 bellard
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1319 19cb3738 bellard
    } else {
1320 19cb3738 bellard
        DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA, 
1321 19cb3738 bellard
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1322 19cb3738 bellard
    }
1323 19cb3738 bellard
}
1324 19cb3738 bellard

1325 19cb3738 bellard
static int raw_set_locked(BlockDriverState *bs, int locked)
1326 19cb3738 bellard
{
1327 19cb3738 bellard
    return -ENOTSUP;
1328 19cb3738 bellard
}
1329 19cb3738 bellard
#endif
1330 19cb3738 bellard
1331 19cb3738 bellard
BlockDriver bdrv_host_device = {
1332 19cb3738 bellard
    "host_device",
1333 19cb3738 bellard
    sizeof(BDRVRawState),
1334 19cb3738 bellard
    NULL, /* no probe for protocols */
1335 19cb3738 bellard
    hdev_open,
1336 19cb3738 bellard
    NULL,
1337 19cb3738 bellard
    NULL,
1338 19cb3738 bellard
    raw_close,
1339 19cb3738 bellard
    NULL,
1340 19cb3738 bellard
    raw_flush,
1341 19cb3738 bellard
    
1342 19cb3738 bellard
#if 0
1343 19cb3738 bellard
    .bdrv_aio_read = raw_aio_read,
1344 19cb3738 bellard
    .bdrv_aio_write = raw_aio_write,
1345 19cb3738 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
1346 19cb3738 bellard
    .aiocb_size = sizeof(RawAIOCB);
1347 19cb3738 bellard
#endif
1348 19cb3738 bellard
    .bdrv_pread = raw_pread,
1349 19cb3738 bellard
    .bdrv_pwrite = raw_pwrite,
1350 19cb3738 bellard
    .bdrv_getlength = raw_getlength,
1351 19cb3738 bellard
};
1352 83f64091 bellard
#endif /* _WIN32 */