Statistics
| Branch: | Revision:

root / block-raw.c @ e9c08226

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

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

1369 19cb3738 bellard
static int raw_is_inserted(BlockDriverState *bs)
1370 19cb3738 bellard
{
1371 19cb3738 bellard
    return 1;
1372 19cb3738 bellard
}
1373 19cb3738 bellard

1374 19cb3738 bellard
static int raw_media_changed(BlockDriverState *bs)
1375 19cb3738 bellard
{
1376 19cb3738 bellard
    return -ENOTSUP;
1377 19cb3738 bellard
}
1378 19cb3738 bellard

1379 19cb3738 bellard
static int raw_eject(BlockDriverState *bs, int eject_flag)
1380 19cb3738 bellard
{
1381 19cb3738 bellard
    DWORD ret_count;
1382 19cb3738 bellard

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

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