Statistics
| Branch: | Revision:

root / block-raw.c @ a50a6282

History | View | Annotate | Download (35 kB)

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

1046 ce1a14dc pbrook
    ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
1047 ce1a14dc pbrook
    if (!ret || ret_count != acb->count) {
1048 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, -EIO);
1049 83f64091 bellard
    } else {
1050 ce1a14dc pbrook
        acb->common.cb(acb->common.opaque, 0);
1051 83f64091 bellard
    }
1052 83f64091 bellard
}
1053 436e15b8 bellard
#endif
1054 ce1a14dc pbrook
1055 ce1a14dc pbrook
static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
1056 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1057 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1058 83f64091 bellard
{
1059 ce1a14dc pbrook
    RawAIOCB *acb;
1060 83f64091 bellard
    int64_t offset;
1061 83f64091 bellard
1062 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
1063 ce1a14dc pbrook
    if (acb->hEvent) {
1064 ce1a14dc pbrook
        acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1065 ce1a14dc pbrook
        if (!acb->hEvent) {
1066 ce1a14dc pbrook
            qemu_aio_release(acb);
1067 ce1a14dc pbrook
            return NULL;
1068 ce1a14dc pbrook
        }
1069 ce1a14dc pbrook
    }
1070 ce1a14dc pbrook
    memset(&acb->ov, 0, sizeof(acb->ov));
1071 83f64091 bellard
    offset = sector_num * 512;
1072 ce1a14dc pbrook
    acb->ov.Offset = offset;
1073 ce1a14dc pbrook
    acb->ov.OffsetHigh = offset >> 32;
1074 ce1a14dc pbrook
    acb->ov.hEvent = acb->hEvent;
1075 ce1a14dc pbrook
    acb->count = nb_sectors * 512;
1076 436e15b8 bellard
#ifndef QEMU_TOOL
1077 ce1a14dc pbrook
    qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1078 436e15b8 bellard
#endif
1079 ce1a14dc pbrook
    return acb;
1080 83f64091 bellard
}
1081 83f64091 bellard
1082 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
1083 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1084 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1085 83f64091 bellard
{
1086 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1087 ce1a14dc pbrook
    RawAIOCB *acb;
1088 83f64091 bellard
    int ret;
1089 83f64091 bellard
1090 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1091 ce1a14dc pbrook
    if (!acb)
1092 ce1a14dc pbrook
        return NULL;
1093 ce1a14dc pbrook
    ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1094 ce1a14dc pbrook
    if (!ret) {
1095 ce1a14dc pbrook
        qemu_aio_release(acb);
1096 ce1a14dc pbrook
        return NULL;
1097 ce1a14dc pbrook
    }
1098 ce1a14dc pbrook
#ifdef QEMU_TOOL
1099 ce1a14dc pbrook
    qemu_aio_release(acb);
1100 436e15b8 bellard
#endif
1101 ce1a14dc pbrook
    return (BlockDriverAIOCB *)acb;
1102 83f64091 bellard
}
1103 83f64091 bellard
1104 ce1a14dc pbrook
static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
1105 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1106 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1107 83f64091 bellard
{
1108 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1109 ce1a14dc pbrook
    RawAIOCB *acb;
1110 ce1a14dc pbrook
    int ret;
1111 83f64091 bellard
1112 ce1a14dc pbrook
    acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
1113 ce1a14dc pbrook
    if (!acb)
1114 ce1a14dc pbrook
        return NULL;
1115 ce1a14dc pbrook
    ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
1116 ce1a14dc pbrook
    if (!ret) {
1117 ce1a14dc pbrook
        qemu_aio_release(acb);
1118 ce1a14dc pbrook
        return NULL;
1119 ce1a14dc pbrook
    }
1120 ce1a14dc pbrook
#ifdef QEMU_TOOL
1121 ce1a14dc pbrook
    qemu_aio_release(acb);
1122 436e15b8 bellard
#endif
1123 ce1a14dc pbrook
    return (BlockDriverAIOCB *)acb;
1124 83f64091 bellard
}
1125 83f64091 bellard
1126 ce1a14dc pbrook
static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
1127 83f64091 bellard
{
1128 ce1a14dc pbrook
#ifndef QEMU_TOOL
1129 ce1a14dc pbrook
    RawAIOCB *acb = (RawAIOCB *)blockacb;
1130 ce1a14dc pbrook
    BlockDriverState *bs = acb->common.bs;
1131 ce1a14dc pbrook
    BDRVRawState *s = bs->opaque;
1132 ce1a14dc pbrook
1133 ce1a14dc pbrook
    qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
1134 ce1a14dc pbrook
    /* XXX: if more than one async I/O it is not correct */
1135 ce1a14dc pbrook
    CancelIo(s->hfile);
1136 ce1a14dc pbrook
    qemu_aio_release(acb);
1137 ce1a14dc pbrook
#endif
1138 83f64091 bellard
}
1139 3b9f94e1 bellard
#endif /* #if 0 */
1140 83f64091 bellard
1141 83f64091 bellard
static void raw_flush(BlockDriverState *bs)
1142 83f64091 bellard
{
1143 3b9f94e1 bellard
    BDRVRawState *s = bs->opaque;
1144 3b9f94e1 bellard
    FlushFileBuffers(s->hfile);
1145 83f64091 bellard
}
1146 83f64091 bellard
1147 83f64091 bellard
static void raw_close(BlockDriverState *bs)
1148 83f64091 bellard
{
1149 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1150 83f64091 bellard
    CloseHandle(s->hfile);
1151 83f64091 bellard
}
1152 83f64091 bellard
1153 83f64091 bellard
static int raw_truncate(BlockDriverState *bs, int64_t offset)
1154 83f64091 bellard
{
1155 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1156 83f64091 bellard
    DWORD low, high;
1157 83f64091 bellard
1158 979b67ad bellard
    low = offset;
1159 979b67ad bellard
    high = offset >> 32;
1160 83f64091 bellard
    if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
1161 83f64091 bellard
        return -EIO;
1162 83f64091 bellard
    if (!SetEndOfFile(s->hfile))
1163 83f64091 bellard
        return -EIO;
1164 83f64091 bellard
    return 0;
1165 83f64091 bellard
}
1166 83f64091 bellard
1167 f45512fe bellard
static int64_t raw_getlength(BlockDriverState *bs)
1168 83f64091 bellard
{
1169 83f64091 bellard
    BDRVRawState *s = bs->opaque;
1170 83f64091 bellard
    LARGE_INTEGER l;
1171 5fafdf24 ths
    ULARGE_INTEGER available, total, total_free;
1172 01781963 bellard
    DISK_GEOMETRY dg;
1173 01781963 bellard
    DWORD count;
1174 01781963 bellard
    BOOL status;
1175 436e15b8 bellard
1176 f45512fe bellard
    switch(s->type) {
1177 19cb3738 bellard
    case FTYPE_FILE:
1178 19cb3738 bellard
        l.LowPart = GetFileSize(s->hfile, &l.HighPart);
1179 19cb3738 bellard
        if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
1180 19cb3738 bellard
            return -EIO;
1181 19cb3738 bellard
        break;
1182 19cb3738 bellard
    case FTYPE_CD:
1183 f45512fe bellard
        if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
1184 19cb3738 bellard
            return -EIO;
1185 f45512fe bellard
        l.QuadPart = total.QuadPart;
1186 19cb3738 bellard
        break;
1187 01781963 bellard
    case FTYPE_HARDDISK:
1188 01781963 bellard
        status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY,
1189 01781963 bellard
                                 NULL, 0, &dg, sizeof(dg), &count, NULL);
1190 01781963 bellard
        if (status != FALSE) {
1191 01781963 bellard
            l.QuadPart = dg.Cylinders.QuadPart * dg.TracksPerCylinder
1192 01781963 bellard
                * dg.SectorsPerTrack * dg.BytesPerSector;
1193 01781963 bellard
        }
1194 01781963 bellard
        break;
1195 19cb3738 bellard
    default:
1196 19cb3738 bellard
        return -EIO;
1197 19cb3738 bellard
    }
1198 83f64091 bellard
    return l.QuadPart;
1199 83f64091 bellard
}
1200 83f64091 bellard
1201 83f64091 bellard
static int raw_create(const char *filename, int64_t total_size,
1202 83f64091 bellard
                      const char *backing_file, int flags)
1203 83f64091 bellard
{
1204 83f64091 bellard
    int fd;
1205 83f64091 bellard
1206 83f64091 bellard
    if (flags || backing_file)
1207 83f64091 bellard
        return -ENOTSUP;
1208 83f64091 bellard
1209 5fafdf24 ths
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
1210 83f64091 bellard
              0644);
1211 83f64091 bellard
    if (fd < 0)
1212 83f64091 bellard
        return -EIO;
1213 83f64091 bellard
    set_sparse(fd);
1214 83f64091 bellard
    ftruncate(fd, total_size * 512);
1215 83f64091 bellard
    close(fd);
1216 83f64091 bellard
    return 0;
1217 83f64091 bellard
}
1218 83f64091 bellard
1219 83f64091 bellard
void qemu_aio_init(void)
1220 83f64091 bellard
{
1221 83f64091 bellard
}
1222 83f64091 bellard
1223 83f64091 bellard
void qemu_aio_poll(void)
1224 83f64091 bellard
{
1225 83f64091 bellard
}
1226 83f64091 bellard
1227 62ee0211 ths
void qemu_aio_flush(void)
1228 62ee0211 ths
{
1229 62ee0211 ths
}
1230 62ee0211 ths
1231 83f64091 bellard
void qemu_aio_wait_start(void)
1232 83f64091 bellard
{
1233 83f64091 bellard
}
1234 83f64091 bellard
1235 83f64091 bellard
void qemu_aio_wait(void)
1236 83f64091 bellard
{
1237 fd44d818 bellard
#ifndef QEMU_TOOL
1238 fd44d818 bellard
    qemu_bh_poll();
1239 fd44d818 bellard
#endif
1240 83f64091 bellard
}
1241 83f64091 bellard
1242 83f64091 bellard
void qemu_aio_wait_end(void)
1243 83f64091 bellard
{
1244 83f64091 bellard
}
1245 83f64091 bellard
1246 83f64091 bellard
BlockDriver bdrv_raw = {
1247 83f64091 bellard
    "raw",
1248 83f64091 bellard
    sizeof(BDRVRawState),
1249 83f64091 bellard
    NULL, /* no probe for protocols */
1250 83f64091 bellard
    raw_open,
1251 83f64091 bellard
    NULL,
1252 83f64091 bellard
    NULL,
1253 83f64091 bellard
    raw_close,
1254 83f64091 bellard
    raw_create,
1255 83f64091 bellard
    raw_flush,
1256 3b46e624 ths
1257 83f64091 bellard
#if 0
1258 83f64091 bellard
    .bdrv_aio_read = raw_aio_read,
1259 83f64091 bellard
    .bdrv_aio_write = raw_aio_write,
1260 83f64091 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
1261 ce1a14dc pbrook
    .aiocb_size = sizeof(RawAIOCB);
1262 83f64091 bellard
#endif
1263 83f64091 bellard
    .protocol_name = "file",
1264 83f64091 bellard
    .bdrv_pread = raw_pread,
1265 83f64091 bellard
    .bdrv_pwrite = raw_pwrite,
1266 83f64091 bellard
    .bdrv_truncate = raw_truncate,
1267 83f64091 bellard
    .bdrv_getlength = raw_getlength,
1268 83f64091 bellard
};
1269 19cb3738 bellard
1270 19cb3738 bellard
/***********************************************/
1271 19cb3738 bellard
/* host device */
1272 19cb3738 bellard
1273 19cb3738 bellard
static int find_cdrom(char *cdrom_name, int cdrom_name_size)
1274 19cb3738 bellard
{
1275 19cb3738 bellard
    char drives[256], *pdrv = drives;
1276 19cb3738 bellard
    UINT type;
1277 19cb3738 bellard
1278 f45512fe bellard
    memset(drives, 0, sizeof(drives));
1279 19cb3738 bellard
    GetLogicalDriveStrings(sizeof(drives), drives);
1280 19cb3738 bellard
    while(pdrv[0] != '\0') {
1281 19cb3738 bellard
        type = GetDriveType(pdrv);
1282 19cb3738 bellard
        switch(type) {
1283 19cb3738 bellard
        case DRIVE_CDROM:
1284 19cb3738 bellard
            snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
1285 19cb3738 bellard
            return 0;
1286 19cb3738 bellard
            break;
1287 19cb3738 bellard
        }
1288 19cb3738 bellard
        pdrv += lstrlen(pdrv) + 1;
1289 19cb3738 bellard
    }
1290 19cb3738 bellard
    return -1;
1291 19cb3738 bellard
}
1292 19cb3738 bellard
1293 f45512fe bellard
static int find_device_type(BlockDriverState *bs, const char *filename)
1294 19cb3738 bellard
{
1295 f45512fe bellard
    BDRVRawState *s = bs->opaque;
1296 19cb3738 bellard
    UINT type;
1297 19cb3738 bellard
    const char *p;
1298 19cb3738 bellard
1299 19cb3738 bellard
    if (strstart(filename, "\\\\.\\", &p) ||
1300 19cb3738 bellard
        strstart(filename, "//./", &p)) {
1301 01781963 bellard
        if (stristart(p, "PhysicalDrive", NULL))
1302 01781963 bellard
            return FTYPE_HARDDISK;
1303 f45512fe bellard
        snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
1304 f45512fe bellard
        type = GetDriveType(s->drive_path);
1305 19cb3738 bellard
        if (type == DRIVE_CDROM)
1306 19cb3738 bellard
            return FTYPE_CD;
1307 19cb3738 bellard
        else
1308 19cb3738 bellard
            return FTYPE_FILE;
1309 19cb3738 bellard
    } else {
1310 19cb3738 bellard
        return FTYPE_FILE;
1311 19cb3738 bellard
    }
1312 19cb3738 bellard
}
1313 19cb3738 bellard
1314 19cb3738 bellard
static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
1315 19cb3738 bellard
{
1316 19cb3738 bellard
    BDRVRawState *s = bs->opaque;
1317 19cb3738 bellard
    int access_flags, create_flags;
1318 19cb3738 bellard
    DWORD overlapped;
1319 19cb3738 bellard
    char device_name[64];
1320 19cb3738 bellard
1321 19cb3738 bellard
    if (strstart(filename, "/dev/cdrom", NULL)) {
1322 19cb3738 bellard
        if (find_cdrom(device_name, sizeof(device_name)) < 0)
1323 19cb3738 bellard
            return -ENOENT;
1324 19cb3738 bellard
        filename = device_name;
1325 19cb3738 bellard
    } else {
1326 19cb3738 bellard
        /* transform drive letters into device name */
1327 19cb3738 bellard
        if (((filename[0] >= 'a' && filename[0] <= 'z') ||
1328 19cb3738 bellard
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
1329 19cb3738 bellard
            filename[1] == ':' && filename[2] == '\0') {
1330 19cb3738 bellard
            snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
1331 19cb3738 bellard
            filename = device_name;
1332 19cb3738 bellard
        }
1333 19cb3738 bellard
    }
1334 f45512fe bellard
    s->type = find_device_type(bs, filename);
1335 3b46e624 ths
1336 19cb3738 bellard
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
1337 19cb3738 bellard
        access_flags = GENERIC_READ | GENERIC_WRITE;
1338 19cb3738 bellard
    } else {
1339 19cb3738 bellard
        access_flags = GENERIC_READ;
1340 19cb3738 bellard
    }
1341 19cb3738 bellard
    create_flags = OPEN_EXISTING;
1342 19cb3738 bellard
1343 19cb3738 bellard
#ifdef QEMU_TOOL
1344 3b9f94e1 bellard
    overlapped = FILE_ATTRIBUTE_NORMAL;
1345 19cb3738 bellard
#else
1346 19cb3738 bellard
    overlapped = FILE_FLAG_OVERLAPPED;
1347 19cb3738 bellard
#endif
1348 5fafdf24 ths
    s->hfile = CreateFile(filename, access_flags,
1349 19cb3738 bellard
                          FILE_SHARE_READ, NULL,
1350 3b9f94e1 bellard
                          create_flags, overlapped, NULL);
1351 54421cb1 ths
    if (s->hfile == INVALID_HANDLE_VALUE) {
1352 54421cb1 ths
        int err = GetLastError();
1353 54421cb1 ths
1354 54421cb1 ths
        if (err == ERROR_ACCESS_DENIED)
1355 54421cb1 ths
            return -EACCES;
1356 19cb3738 bellard
        return -1;
1357 54421cb1 ths
    }
1358 19cb3738 bellard
    return 0;
1359 19cb3738 bellard
}
1360 19cb3738 bellard
1361 19cb3738 bellard
#if 0
1362 19cb3738 bellard
/***********************************************/
1363 aa1f17c1 ths
/* removable device additional commands */
1364 19cb3738 bellard

1365 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
1366 19cb3738 bellard
{
1367 19cb3738 bellard
    return 1;
1368 19cb3738 bellard
}
1369 19cb3738 bellard

1370 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
1371 19cb3738 bellard
{
1372 19cb3738 bellard
    return -ENOTSUP;
1373 19cb3738 bellard
}
1374 19cb3738 bellard

1375 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
1376 19cb3738 bellard
{
1377 19cb3738 bellard
    DWORD ret_count;
1378 19cb3738 bellard

1379 19cb3738 bellard
    if (s->type == FTYPE_FILE)
1380 19cb3738 bellard
        return -ENOTSUP;
1381 19cb3738 bellard
    if (eject_flag) {
1382 5fafdf24 ths
        DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
1383 19cb3738 bellard
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1384 19cb3738 bellard
    } else {
1385 5fafdf24 ths
        DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
1386 19cb3738 bellard
                        NULL, 0, NULL, 0, &lpBytesReturned, NULL);
1387 19cb3738 bellard
    }
1388 19cb3738 bellard
}
1389 19cb3738 bellard

1390 19cb3738 bellard
static int raw_set_locked(BlockDriverState *bs, int locked)
1391 19cb3738 bellard
{
1392 19cb3738 bellard
    return -ENOTSUP;
1393 19cb3738 bellard
}
1394 19cb3738 bellard
#endif
1395 19cb3738 bellard
1396 19cb3738 bellard
BlockDriver bdrv_host_device = {
1397 19cb3738 bellard
    "host_device",
1398 19cb3738 bellard
    sizeof(BDRVRawState),
1399 19cb3738 bellard
    NULL, /* no probe for protocols */
1400 19cb3738 bellard
    hdev_open,
1401 19cb3738 bellard
    NULL,
1402 19cb3738 bellard
    NULL,
1403 19cb3738 bellard
    raw_close,
1404 19cb3738 bellard
    NULL,
1405 19cb3738 bellard
    raw_flush,
1406 3b46e624 ths
1407 19cb3738 bellard
#if 0
1408 19cb3738 bellard
    .bdrv_aio_read = raw_aio_read,
1409 19cb3738 bellard
    .bdrv_aio_write = raw_aio_write,
1410 19cb3738 bellard
    .bdrv_aio_cancel = raw_aio_cancel,
1411 19cb3738 bellard
    .aiocb_size = sizeof(RawAIOCB);
1412 19cb3738 bellard
#endif
1413 19cb3738 bellard
    .bdrv_pread = raw_pread,
1414 19cb3738 bellard
    .bdrv_pwrite = raw_pwrite,
1415 19cb3738 bellard
    .bdrv_getlength = raw_getlength,
1416 19cb3738 bellard
};
1417 83f64091 bellard
#endif /* _WIN32 */