Statistics
| Branch: | Revision:

root / hw / ide / atapi.c @ 96478592

History | View | Annotate | Download (34.6 kB)

1 33231e0e Kevin Wolf
/*
2 33231e0e Kevin Wolf
 * QEMU ATAPI Emulation
3 33231e0e Kevin Wolf
 *
4 33231e0e Kevin Wolf
 * Copyright (c) 2003 Fabrice Bellard
5 33231e0e Kevin Wolf
 * Copyright (c) 2006 Openedhand Ltd.
6 33231e0e Kevin Wolf
 *
7 33231e0e Kevin Wolf
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 33231e0e Kevin Wolf
 * of this software and associated documentation files (the "Software"), to deal
9 33231e0e Kevin Wolf
 * in the Software without restriction, including without limitation the rights
10 33231e0e Kevin Wolf
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 33231e0e Kevin Wolf
 * copies of the Software, and to permit persons to whom the Software is
12 33231e0e Kevin Wolf
 * furnished to do so, subject to the following conditions:
13 33231e0e Kevin Wolf
 *
14 33231e0e Kevin Wolf
 * The above copyright notice and this permission notice shall be included in
15 33231e0e Kevin Wolf
 * all copies or substantial portions of the Software.
16 33231e0e Kevin Wolf
 *
17 33231e0e Kevin Wolf
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 33231e0e Kevin Wolf
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 33231e0e Kevin Wolf
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 33231e0e Kevin Wolf
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 33231e0e Kevin Wolf
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 33231e0e Kevin Wolf
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 33231e0e Kevin Wolf
 * THE SOFTWARE.
24 33231e0e Kevin Wolf
 */
25 33231e0e Kevin Wolf
26 33231e0e Kevin Wolf
#include "hw/ide/internal.h"
27 0d09e41a Paolo Bonzini
#include "hw/scsi/scsi.h"
28 33231e0e Kevin Wolf
29 33231e0e Kevin Wolf
static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
30 33231e0e Kevin Wolf
31 33231e0e Kevin Wolf
static void padstr8(uint8_t *buf, int buf_size, const char *src)
32 33231e0e Kevin Wolf
{
33 33231e0e Kevin Wolf
    int i;
34 33231e0e Kevin Wolf
    for(i = 0; i < buf_size; i++) {
35 33231e0e Kevin Wolf
        if (*src)
36 33231e0e Kevin Wolf
            buf[i] = *src++;
37 33231e0e Kevin Wolf
        else
38 33231e0e Kevin Wolf
            buf[i] = ' ';
39 33231e0e Kevin Wolf
    }
40 33231e0e Kevin Wolf
}
41 33231e0e Kevin Wolf
42 33231e0e Kevin Wolf
static inline void cpu_to_ube16(uint8_t *buf, int val)
43 33231e0e Kevin Wolf
{
44 33231e0e Kevin Wolf
    buf[0] = val >> 8;
45 33231e0e Kevin Wolf
    buf[1] = val & 0xff;
46 33231e0e Kevin Wolf
}
47 33231e0e Kevin Wolf
48 33231e0e Kevin Wolf
static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
49 33231e0e Kevin Wolf
{
50 33231e0e Kevin Wolf
    buf[0] = val >> 24;
51 33231e0e Kevin Wolf
    buf[1] = val >> 16;
52 33231e0e Kevin Wolf
    buf[2] = val >> 8;
53 33231e0e Kevin Wolf
    buf[3] = val & 0xff;
54 33231e0e Kevin Wolf
}
55 33231e0e Kevin Wolf
56 33231e0e Kevin Wolf
static inline int ube16_to_cpu(const uint8_t *buf)
57 33231e0e Kevin Wolf
{
58 33231e0e Kevin Wolf
    return (buf[0] << 8) | buf[1];
59 33231e0e Kevin Wolf
}
60 33231e0e Kevin Wolf
61 33231e0e Kevin Wolf
static inline int ube32_to_cpu(const uint8_t *buf)
62 33231e0e Kevin Wolf
{
63 33231e0e Kevin Wolf
    return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
64 33231e0e Kevin Wolf
}
65 33231e0e Kevin Wolf
66 33231e0e Kevin Wolf
static void lba_to_msf(uint8_t *buf, int lba)
67 33231e0e Kevin Wolf
{
68 33231e0e Kevin Wolf
    lba += 150;
69 33231e0e Kevin Wolf
    buf[0] = (lba / 75) / 60;
70 33231e0e Kevin Wolf
    buf[1] = (lba / 75) % 60;
71 33231e0e Kevin Wolf
    buf[2] = lba % 75;
72 33231e0e Kevin Wolf
}
73 33231e0e Kevin Wolf
74 33231e0e Kevin Wolf
static inline int media_present(IDEState *s)
75 33231e0e Kevin Wolf
{
76 a1aff5bf Markus Armbruster
    return !s->tray_open && s->nb_sectors > 0;
77 33231e0e Kevin Wolf
}
78 33231e0e Kevin Wolf
79 a7acf552 Amit Shah
/* XXX: DVDs that could fit on a CD will be reported as a CD */
80 33231e0e Kevin Wolf
static inline int media_is_dvd(IDEState *s)
81 33231e0e Kevin Wolf
{
82 33231e0e Kevin Wolf
    return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
83 33231e0e Kevin Wolf
}
84 33231e0e Kevin Wolf
85 33231e0e Kevin Wolf
static inline int media_is_cd(IDEState *s)
86 33231e0e Kevin Wolf
{
87 33231e0e Kevin Wolf
    return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
88 33231e0e Kevin Wolf
}
89 33231e0e Kevin Wolf
90 33231e0e Kevin Wolf
static void cd_data_to_raw(uint8_t *buf, int lba)
91 33231e0e Kevin Wolf
{
92 33231e0e Kevin Wolf
    /* sync bytes */
93 33231e0e Kevin Wolf
    buf[0] = 0x00;
94 33231e0e Kevin Wolf
    memset(buf + 1, 0xff, 10);
95 33231e0e Kevin Wolf
    buf[11] = 0x00;
96 33231e0e Kevin Wolf
    buf += 12;
97 33231e0e Kevin Wolf
    /* MSF */
98 33231e0e Kevin Wolf
    lba_to_msf(buf, lba);
99 33231e0e Kevin Wolf
    buf[3] = 0x01; /* mode 1 data */
100 33231e0e Kevin Wolf
    buf += 4;
101 33231e0e Kevin Wolf
    /* data */
102 33231e0e Kevin Wolf
    buf += 2048;
103 33231e0e Kevin Wolf
    /* XXX: ECC not computed */
104 33231e0e Kevin Wolf
    memset(buf, 0, 288);
105 33231e0e Kevin Wolf
}
106 33231e0e Kevin Wolf
107 a597e79c Christoph Hellwig
static int cd_read_sector(IDEState *s, int lba, uint8_t *buf, int sector_size)
108 33231e0e Kevin Wolf
{
109 33231e0e Kevin Wolf
    int ret;
110 33231e0e Kevin Wolf
111 33231e0e Kevin Wolf
    switch(sector_size) {
112 33231e0e Kevin Wolf
    case 2048:
113 a597e79c Christoph Hellwig
        bdrv_acct_start(s->bs, &s->acct, 4 * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
114 a597e79c Christoph Hellwig
        ret = bdrv_read(s->bs, (int64_t)lba << 2, buf, 4);
115 a597e79c Christoph Hellwig
        bdrv_acct_done(s->bs, &s->acct);
116 33231e0e Kevin Wolf
        break;
117 33231e0e Kevin Wolf
    case 2352:
118 a597e79c Christoph Hellwig
        bdrv_acct_start(s->bs, &s->acct, 4 * BDRV_SECTOR_SIZE, BDRV_ACCT_READ);
119 a597e79c Christoph Hellwig
        ret = bdrv_read(s->bs, (int64_t)lba << 2, buf + 16, 4);
120 a597e79c Christoph Hellwig
        bdrv_acct_done(s->bs, &s->acct);
121 33231e0e Kevin Wolf
        if (ret < 0)
122 33231e0e Kevin Wolf
            return ret;
123 33231e0e Kevin Wolf
        cd_data_to_raw(buf, lba);
124 33231e0e Kevin Wolf
        break;
125 33231e0e Kevin Wolf
    default:
126 33231e0e Kevin Wolf
        ret = -EIO;
127 33231e0e Kevin Wolf
        break;
128 33231e0e Kevin Wolf
    }
129 33231e0e Kevin Wolf
    return ret;
130 33231e0e Kevin Wolf
}
131 33231e0e Kevin Wolf
132 33231e0e Kevin Wolf
void ide_atapi_cmd_ok(IDEState *s)
133 33231e0e Kevin Wolf
{
134 33231e0e Kevin Wolf
    s->error = 0;
135 33231e0e Kevin Wolf
    s->status = READY_STAT | SEEK_STAT;
136 33231e0e Kevin Wolf
    s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
137 33231e0e Kevin Wolf
    ide_set_irq(s->bus);
138 33231e0e Kevin Wolf
}
139 33231e0e Kevin Wolf
140 33231e0e Kevin Wolf
void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
141 33231e0e Kevin Wolf
{
142 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
143 33231e0e Kevin Wolf
    printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
144 33231e0e Kevin Wolf
#endif
145 33231e0e Kevin Wolf
    s->error = sense_key << 4;
146 33231e0e Kevin Wolf
    s->status = READY_STAT | ERR_STAT;
147 33231e0e Kevin Wolf
    s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
148 33231e0e Kevin Wolf
    s->sense_key = sense_key;
149 33231e0e Kevin Wolf
    s->asc = asc;
150 33231e0e Kevin Wolf
    ide_set_irq(s->bus);
151 33231e0e Kevin Wolf
}
152 33231e0e Kevin Wolf
153 33231e0e Kevin Wolf
void ide_atapi_io_error(IDEState *s, int ret)
154 33231e0e Kevin Wolf
{
155 33231e0e Kevin Wolf
    /* XXX: handle more errors */
156 33231e0e Kevin Wolf
    if (ret == -ENOMEDIUM) {
157 67cc61e4 Paolo Bonzini
        ide_atapi_cmd_error(s, NOT_READY,
158 33231e0e Kevin Wolf
                            ASC_MEDIUM_NOT_PRESENT);
159 33231e0e Kevin Wolf
    } else {
160 67cc61e4 Paolo Bonzini
        ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
161 33231e0e Kevin Wolf
                            ASC_LOGICAL_BLOCK_OOR);
162 33231e0e Kevin Wolf
    }
163 33231e0e Kevin Wolf
}
164 33231e0e Kevin Wolf
165 33231e0e Kevin Wolf
/* The whole ATAPI transfer logic is handled in this function */
166 33231e0e Kevin Wolf
void ide_atapi_cmd_reply_end(IDEState *s)
167 33231e0e Kevin Wolf
{
168 33231e0e Kevin Wolf
    int byte_count_limit, size, ret;
169 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
170 33231e0e Kevin Wolf
    printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
171 33231e0e Kevin Wolf
           s->packet_transfer_size,
172 33231e0e Kevin Wolf
           s->elementary_transfer_size,
173 33231e0e Kevin Wolf
           s->io_buffer_index);
174 33231e0e Kevin Wolf
#endif
175 33231e0e Kevin Wolf
    if (s->packet_transfer_size <= 0) {
176 33231e0e Kevin Wolf
        /* end of transfer */
177 33231e0e Kevin Wolf
        ide_transfer_stop(s);
178 33231e0e Kevin Wolf
        s->status = READY_STAT | SEEK_STAT;
179 33231e0e Kevin Wolf
        s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
180 33231e0e Kevin Wolf
        ide_set_irq(s->bus);
181 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
182 33231e0e Kevin Wolf
        printf("status=0x%x\n", s->status);
183 33231e0e Kevin Wolf
#endif
184 33231e0e Kevin Wolf
    } else {
185 33231e0e Kevin Wolf
        /* see if a new sector must be read */
186 33231e0e Kevin Wolf
        if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
187 a597e79c Christoph Hellwig
            ret = cd_read_sector(s, s->lba, s->io_buffer, s->cd_sector_size);
188 33231e0e Kevin Wolf
            if (ret < 0) {
189 33231e0e Kevin Wolf
                ide_transfer_stop(s);
190 33231e0e Kevin Wolf
                ide_atapi_io_error(s, ret);
191 33231e0e Kevin Wolf
                return;
192 33231e0e Kevin Wolf
            }
193 33231e0e Kevin Wolf
            s->lba++;
194 33231e0e Kevin Wolf
            s->io_buffer_index = 0;
195 33231e0e Kevin Wolf
        }
196 33231e0e Kevin Wolf
        if (s->elementary_transfer_size > 0) {
197 33231e0e Kevin Wolf
            /* there are some data left to transmit in this elementary
198 33231e0e Kevin Wolf
               transfer */
199 33231e0e Kevin Wolf
            size = s->cd_sector_size - s->io_buffer_index;
200 33231e0e Kevin Wolf
            if (size > s->elementary_transfer_size)
201 33231e0e Kevin Wolf
                size = s->elementary_transfer_size;
202 33231e0e Kevin Wolf
            s->packet_transfer_size -= size;
203 33231e0e Kevin Wolf
            s->elementary_transfer_size -= size;
204 33231e0e Kevin Wolf
            s->io_buffer_index += size;
205 33231e0e Kevin Wolf
            ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
206 33231e0e Kevin Wolf
                               size, ide_atapi_cmd_reply_end);
207 33231e0e Kevin Wolf
        } else {
208 33231e0e Kevin Wolf
            /* a new transfer is needed */
209 33231e0e Kevin Wolf
            s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
210 33231e0e Kevin Wolf
            byte_count_limit = s->lcyl | (s->hcyl << 8);
211 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
212 33231e0e Kevin Wolf
            printf("byte_count_limit=%d\n", byte_count_limit);
213 33231e0e Kevin Wolf
#endif
214 33231e0e Kevin Wolf
            if (byte_count_limit == 0xffff)
215 33231e0e Kevin Wolf
                byte_count_limit--;
216 33231e0e Kevin Wolf
            size = s->packet_transfer_size;
217 33231e0e Kevin Wolf
            if (size > byte_count_limit) {
218 33231e0e Kevin Wolf
                /* byte count limit must be even if this case */
219 33231e0e Kevin Wolf
                if (byte_count_limit & 1)
220 33231e0e Kevin Wolf
                    byte_count_limit--;
221 33231e0e Kevin Wolf
                size = byte_count_limit;
222 33231e0e Kevin Wolf
            }
223 33231e0e Kevin Wolf
            s->lcyl = size;
224 33231e0e Kevin Wolf
            s->hcyl = size >> 8;
225 33231e0e Kevin Wolf
            s->elementary_transfer_size = size;
226 33231e0e Kevin Wolf
            /* we cannot transmit more than one sector at a time */
227 33231e0e Kevin Wolf
            if (s->lba != -1) {
228 33231e0e Kevin Wolf
                if (size > (s->cd_sector_size - s->io_buffer_index))
229 33231e0e Kevin Wolf
                    size = (s->cd_sector_size - s->io_buffer_index);
230 33231e0e Kevin Wolf
            }
231 33231e0e Kevin Wolf
            s->packet_transfer_size -= size;
232 33231e0e Kevin Wolf
            s->elementary_transfer_size -= size;
233 33231e0e Kevin Wolf
            s->io_buffer_index += size;
234 33231e0e Kevin Wolf
            ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
235 33231e0e Kevin Wolf
                               size, ide_atapi_cmd_reply_end);
236 33231e0e Kevin Wolf
            ide_set_irq(s->bus);
237 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
238 33231e0e Kevin Wolf
            printf("status=0x%x\n", s->status);
239 33231e0e Kevin Wolf
#endif
240 33231e0e Kevin Wolf
        }
241 33231e0e Kevin Wolf
    }
242 33231e0e Kevin Wolf
}
243 33231e0e Kevin Wolf
244 33231e0e Kevin Wolf
/* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
245 33231e0e Kevin Wolf
static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
246 33231e0e Kevin Wolf
{
247 33231e0e Kevin Wolf
    if (size > max_size)
248 33231e0e Kevin Wolf
        size = max_size;
249 33231e0e Kevin Wolf
    s->lba = -1; /* no sector read */
250 33231e0e Kevin Wolf
    s->packet_transfer_size = size;
251 33231e0e Kevin Wolf
    s->io_buffer_size = size;    /* dma: send the reply data as one chunk */
252 33231e0e Kevin Wolf
    s->elementary_transfer_size = 0;
253 33231e0e Kevin Wolf
    s->io_buffer_index = 0;
254 33231e0e Kevin Wolf
255 33231e0e Kevin Wolf
    if (s->atapi_dma) {
256 a597e79c Christoph Hellwig
        bdrv_acct_start(s->bs, &s->acct, size, BDRV_ACCT_READ);
257 33231e0e Kevin Wolf
        s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
258 33231e0e Kevin Wolf
        s->bus->dma->ops->start_dma(s->bus->dma, s,
259 33231e0e Kevin Wolf
                                   ide_atapi_cmd_read_dma_cb);
260 33231e0e Kevin Wolf
    } else {
261 33231e0e Kevin Wolf
        s->status = READY_STAT | SEEK_STAT;
262 33231e0e Kevin Wolf
        ide_atapi_cmd_reply_end(s);
263 33231e0e Kevin Wolf
    }
264 33231e0e Kevin Wolf
}
265 33231e0e Kevin Wolf
266 33231e0e Kevin Wolf
/* start a CD-CDROM read command */
267 33231e0e Kevin Wolf
static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
268 33231e0e Kevin Wolf
                                   int sector_size)
269 33231e0e Kevin Wolf
{
270 33231e0e Kevin Wolf
    s->lba = lba;
271 33231e0e Kevin Wolf
    s->packet_transfer_size = nb_sectors * sector_size;
272 33231e0e Kevin Wolf
    s->elementary_transfer_size = 0;
273 33231e0e Kevin Wolf
    s->io_buffer_index = sector_size;
274 33231e0e Kevin Wolf
    s->cd_sector_size = sector_size;
275 33231e0e Kevin Wolf
276 33231e0e Kevin Wolf
    s->status = READY_STAT | SEEK_STAT;
277 33231e0e Kevin Wolf
    ide_atapi_cmd_reply_end(s);
278 33231e0e Kevin Wolf
}
279 33231e0e Kevin Wolf
280 33231e0e Kevin Wolf
static void ide_atapi_cmd_check_status(IDEState *s)
281 33231e0e Kevin Wolf
{
282 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
283 33231e0e Kevin Wolf
    printf("atapi_cmd_check_status\n");
284 33231e0e Kevin Wolf
#endif
285 67cc61e4 Paolo Bonzini
    s->error = MC_ERR | (UNIT_ATTENTION << 4);
286 33231e0e Kevin Wolf
    s->status = ERR_STAT;
287 33231e0e Kevin Wolf
    s->nsector = 0;
288 33231e0e Kevin Wolf
    ide_set_irq(s->bus);
289 33231e0e Kevin Wolf
}
290 33231e0e Kevin Wolf
/* ATAPI DMA support */
291 33231e0e Kevin Wolf
292 33231e0e Kevin Wolf
/* XXX: handle read errors */
293 33231e0e Kevin Wolf
static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
294 33231e0e Kevin Wolf
{
295 33231e0e Kevin Wolf
    IDEState *s = opaque;
296 33231e0e Kevin Wolf
    int data_offset, n;
297 33231e0e Kevin Wolf
298 33231e0e Kevin Wolf
    if (ret < 0) {
299 33231e0e Kevin Wolf
        ide_atapi_io_error(s, ret);
300 33231e0e Kevin Wolf
        goto eot;
301 33231e0e Kevin Wolf
    }
302 33231e0e Kevin Wolf
303 33231e0e Kevin Wolf
    if (s->io_buffer_size > 0) {
304 33231e0e Kevin Wolf
        /*
305 33231e0e Kevin Wolf
         * For a cdrom read sector command (s->lba != -1),
306 33231e0e Kevin Wolf
         * adjust the lba for the next s->io_buffer_size chunk
307 33231e0e Kevin Wolf
         * and dma the current chunk.
308 33231e0e Kevin Wolf
         * For a command != read (s->lba == -1), just transfer
309 33231e0e Kevin Wolf
         * the reply data.
310 33231e0e Kevin Wolf
         */
311 33231e0e Kevin Wolf
        if (s->lba != -1) {
312 33231e0e Kevin Wolf
            if (s->cd_sector_size == 2352) {
313 33231e0e Kevin Wolf
                n = 1;
314 33231e0e Kevin Wolf
                cd_data_to_raw(s->io_buffer, s->lba);
315 33231e0e Kevin Wolf
            } else {
316 33231e0e Kevin Wolf
                n = s->io_buffer_size >> 11;
317 33231e0e Kevin Wolf
            }
318 33231e0e Kevin Wolf
            s->lba += n;
319 33231e0e Kevin Wolf
        }
320 33231e0e Kevin Wolf
        s->packet_transfer_size -= s->io_buffer_size;
321 33231e0e Kevin Wolf
        if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
322 33231e0e Kevin Wolf
            goto eot;
323 33231e0e Kevin Wolf
    }
324 33231e0e Kevin Wolf
325 33231e0e Kevin Wolf
    if (s->packet_transfer_size <= 0) {
326 33231e0e Kevin Wolf
        s->status = READY_STAT | SEEK_STAT;
327 33231e0e Kevin Wolf
        s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
328 33231e0e Kevin Wolf
        ide_set_irq(s->bus);
329 a597e79c Christoph Hellwig
        goto eot;
330 33231e0e Kevin Wolf
    }
331 33231e0e Kevin Wolf
332 33231e0e Kevin Wolf
    s->io_buffer_index = 0;
333 33231e0e Kevin Wolf
    if (s->cd_sector_size == 2352) {
334 33231e0e Kevin Wolf
        n = 1;
335 33231e0e Kevin Wolf
        s->io_buffer_size = s->cd_sector_size;
336 33231e0e Kevin Wolf
        data_offset = 16;
337 33231e0e Kevin Wolf
    } else {
338 33231e0e Kevin Wolf
        n = s->packet_transfer_size >> 11;
339 33231e0e Kevin Wolf
        if (n > (IDE_DMA_BUF_SECTORS / 4))
340 33231e0e Kevin Wolf
            n = (IDE_DMA_BUF_SECTORS / 4);
341 33231e0e Kevin Wolf
        s->io_buffer_size = n * 2048;
342 33231e0e Kevin Wolf
        data_offset = 0;
343 33231e0e Kevin Wolf
    }
344 33231e0e Kevin Wolf
#ifdef DEBUG_AIO
345 33231e0e Kevin Wolf
    printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
346 33231e0e Kevin Wolf
#endif
347 a597e79c Christoph Hellwig
348 33231e0e Kevin Wolf
    s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
349 33231e0e Kevin Wolf
    s->bus->dma->iov.iov_len = n * 4 * 512;
350 33231e0e Kevin Wolf
    qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
351 a597e79c Christoph Hellwig
352 33231e0e Kevin Wolf
    s->bus->dma->aiocb = bdrv_aio_readv(s->bs, (int64_t)s->lba << 2,
353 33231e0e Kevin Wolf
                                       &s->bus->dma->qiov, n * 4,
354 33231e0e Kevin Wolf
                                       ide_atapi_cmd_read_dma_cb, s);
355 a597e79c Christoph Hellwig
    return;
356 ad54ae80 Paolo Bonzini
357 a597e79c Christoph Hellwig
eot:
358 a597e79c Christoph Hellwig
    bdrv_acct_done(s->bs, &s->acct);
359 a597e79c Christoph Hellwig
    s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
360 a597e79c Christoph Hellwig
    ide_set_inactive(s);
361 33231e0e Kevin Wolf
}
362 33231e0e Kevin Wolf
363 33231e0e Kevin Wolf
/* start a CD-CDROM read command with DMA */
364 33231e0e Kevin Wolf
/* XXX: test if DMA is available */
365 33231e0e Kevin Wolf
static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
366 33231e0e Kevin Wolf
                                   int sector_size)
367 33231e0e Kevin Wolf
{
368 33231e0e Kevin Wolf
    s->lba = lba;
369 33231e0e Kevin Wolf
    s->packet_transfer_size = nb_sectors * sector_size;
370 33231e0e Kevin Wolf
    s->io_buffer_index = 0;
371 33231e0e Kevin Wolf
    s->io_buffer_size = 0;
372 33231e0e Kevin Wolf
    s->cd_sector_size = sector_size;
373 33231e0e Kevin Wolf
374 a597e79c Christoph Hellwig
    bdrv_acct_start(s->bs, &s->acct, s->packet_transfer_size, BDRV_ACCT_READ);
375 a597e79c Christoph Hellwig
376 33231e0e Kevin Wolf
    /* XXX: check if BUSY_STAT should be set */
377 33231e0e Kevin Wolf
    s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
378 33231e0e Kevin Wolf
    s->bus->dma->ops->start_dma(s->bus->dma, s,
379 33231e0e Kevin Wolf
                               ide_atapi_cmd_read_dma_cb);
380 33231e0e Kevin Wolf
}
381 33231e0e Kevin Wolf
382 33231e0e Kevin Wolf
static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
383 33231e0e Kevin Wolf
                               int sector_size)
384 33231e0e Kevin Wolf
{
385 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
386 33231e0e Kevin Wolf
    printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
387 33231e0e Kevin Wolf
        lba, nb_sectors);
388 33231e0e Kevin Wolf
#endif
389 33231e0e Kevin Wolf
    if (s->atapi_dma) {
390 33231e0e Kevin Wolf
        ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
391 33231e0e Kevin Wolf
    } else {
392 33231e0e Kevin Wolf
        ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
393 33231e0e Kevin Wolf
    }
394 33231e0e Kevin Wolf
}
395 33231e0e Kevin Wolf
396 33231e0e Kevin Wolf
static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
397 33231e0e Kevin Wolf
                                            uint16_t profile)
398 33231e0e Kevin Wolf
{
399 33231e0e Kevin Wolf
    uint8_t *buf_profile = buf + 12; /* start of profiles */
400 33231e0e Kevin Wolf
401 33231e0e Kevin Wolf
    buf_profile += ((*index) * 4); /* start of indexed profile */
402 33231e0e Kevin Wolf
    cpu_to_ube16 (buf_profile, profile);
403 33231e0e Kevin Wolf
    buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
404 33231e0e Kevin Wolf
405 33231e0e Kevin Wolf
    /* each profile adds 4 bytes to the response */
406 33231e0e Kevin Wolf
    (*index)++;
407 33231e0e Kevin Wolf
    buf[11] += 4; /* Additional Length */
408 33231e0e Kevin Wolf
409 33231e0e Kevin Wolf
    return 4;
410 33231e0e Kevin Wolf
}
411 33231e0e Kevin Wolf
412 33231e0e Kevin Wolf
static int ide_dvd_read_structure(IDEState *s, int format,
413 33231e0e Kevin Wolf
                                  const uint8_t *packet, uint8_t *buf)
414 33231e0e Kevin Wolf
{
415 33231e0e Kevin Wolf
    switch (format) {
416 33231e0e Kevin Wolf
        case 0x0: /* Physical format information */
417 33231e0e Kevin Wolf
            {
418 33231e0e Kevin Wolf
                int layer = packet[6];
419 33231e0e Kevin Wolf
                uint64_t total_sectors;
420 33231e0e Kevin Wolf
421 33231e0e Kevin Wolf
                if (layer != 0)
422 33231e0e Kevin Wolf
                    return -ASC_INV_FIELD_IN_CMD_PACKET;
423 33231e0e Kevin Wolf
424 e119bcac Kevin Wolf
                total_sectors = s->nb_sectors >> 2;
425 e119bcac Kevin Wolf
                if (total_sectors == 0) {
426 33231e0e Kevin Wolf
                    return -ASC_MEDIUM_NOT_PRESENT;
427 e119bcac Kevin Wolf
                }
428 33231e0e Kevin Wolf
429 33231e0e Kevin Wolf
                buf[4] = 1;   /* DVD-ROM, part version 1 */
430 33231e0e Kevin Wolf
                buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
431 33231e0e Kevin Wolf
                buf[6] = 1;   /* one layer, read-only (per MMC-2 spec) */
432 33231e0e Kevin Wolf
                buf[7] = 0;   /* default densities */
433 33231e0e Kevin Wolf
434 33231e0e Kevin Wolf
                /* FIXME: 0x30000 per spec? */
435 33231e0e Kevin Wolf
                cpu_to_ube32(buf + 8, 0); /* start sector */
436 33231e0e Kevin Wolf
                cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
437 33231e0e Kevin Wolf
                cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
438 33231e0e Kevin Wolf
439 33231e0e Kevin Wolf
                /* Size of buffer, not including 2 byte size field */
440 33231e0e Kevin Wolf
                cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
441 33231e0e Kevin Wolf
442 33231e0e Kevin Wolf
                /* 2k data + 4 byte header */
443 33231e0e Kevin Wolf
                return (2048 + 4);
444 33231e0e Kevin Wolf
            }
445 33231e0e Kevin Wolf
446 33231e0e Kevin Wolf
        case 0x01: /* DVD copyright information */
447 33231e0e Kevin Wolf
            buf[4] = 0; /* no copyright data */
448 33231e0e Kevin Wolf
            buf[5] = 0; /* no region restrictions */
449 33231e0e Kevin Wolf
450 33231e0e Kevin Wolf
            /* Size of buffer, not including 2 byte size field */
451 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)buf, 4 + 2);
452 33231e0e Kevin Wolf
453 33231e0e Kevin Wolf
            /* 4 byte header + 4 byte data */
454 33231e0e Kevin Wolf
            return (4 + 4);
455 33231e0e Kevin Wolf
456 33231e0e Kevin Wolf
        case 0x03: /* BCA information - invalid field for no BCA info */
457 33231e0e Kevin Wolf
            return -ASC_INV_FIELD_IN_CMD_PACKET;
458 33231e0e Kevin Wolf
459 33231e0e Kevin Wolf
        case 0x04: /* DVD disc manufacturing information */
460 33231e0e Kevin Wolf
            /* Size of buffer, not including 2 byte size field */
461 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
462 33231e0e Kevin Wolf
463 33231e0e Kevin Wolf
            /* 2k data + 4 byte header */
464 33231e0e Kevin Wolf
            return (2048 + 4);
465 33231e0e Kevin Wolf
466 33231e0e Kevin Wolf
        case 0xff:
467 33231e0e Kevin Wolf
            /*
468 33231e0e Kevin Wolf
             * This lists all the command capabilities above.  Add new ones
469 33231e0e Kevin Wolf
             * in order and update the length and buffer return values.
470 33231e0e Kevin Wolf
             */
471 33231e0e Kevin Wolf
472 33231e0e Kevin Wolf
            buf[4] = 0x00; /* Physical format */
473 33231e0e Kevin Wolf
            buf[5] = 0x40; /* Not writable, is readable */
474 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)(buf + 6), 2048 + 4);
475 33231e0e Kevin Wolf
476 33231e0e Kevin Wolf
            buf[8] = 0x01; /* Copyright info */
477 33231e0e Kevin Wolf
            buf[9] = 0x40; /* Not writable, is readable */
478 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)(buf + 10), 4 + 4);
479 33231e0e Kevin Wolf
480 33231e0e Kevin Wolf
            buf[12] = 0x03; /* BCA info */
481 33231e0e Kevin Wolf
            buf[13] = 0x40; /* Not writable, is readable */
482 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)(buf + 14), 188 + 4);
483 33231e0e Kevin Wolf
484 33231e0e Kevin Wolf
            buf[16] = 0x04; /* Manufacturing info */
485 33231e0e Kevin Wolf
            buf[17] = 0x40; /* Not writable, is readable */
486 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)(buf + 18), 2048 + 4);
487 33231e0e Kevin Wolf
488 33231e0e Kevin Wolf
            /* Size of buffer, not including 2 byte size field */
489 33231e0e Kevin Wolf
            cpu_to_be16wu((uint16_t *)buf, 16 + 2);
490 33231e0e Kevin Wolf
491 33231e0e Kevin Wolf
            /* data written + 4 byte header */
492 33231e0e Kevin Wolf
            return (16 + 4);
493 33231e0e Kevin Wolf
494 33231e0e Kevin Wolf
        default: /* TODO: formats beyond DVD-ROM requires */
495 33231e0e Kevin Wolf
            return -ASC_INV_FIELD_IN_CMD_PACKET;
496 33231e0e Kevin Wolf
    }
497 33231e0e Kevin Wolf
}
498 33231e0e Kevin Wolf
499 33231e0e Kevin Wolf
static unsigned int event_status_media(IDEState *s,
500 33231e0e Kevin Wolf
                                       uint8_t *buf)
501 33231e0e Kevin Wolf
{
502 33231e0e Kevin Wolf
    uint8_t event_code, media_status;
503 33231e0e Kevin Wolf
504 33231e0e Kevin Wolf
    media_status = 0;
505 dd063333 Markus Armbruster
    if (s->tray_open) {
506 33231e0e Kevin Wolf
        media_status = MS_TRAY_OPEN;
507 33231e0e Kevin Wolf
    } else if (bdrv_is_inserted(s->bs)) {
508 33231e0e Kevin Wolf
        media_status = MS_MEDIA_PRESENT;
509 33231e0e Kevin Wolf
    }
510 33231e0e Kevin Wolf
511 33231e0e Kevin Wolf
    /* Event notification descriptor */
512 33231e0e Kevin Wolf
    event_code = MEC_NO_CHANGE;
513 2df0a3a3 Paolo Bonzini
    if (media_status != MS_TRAY_OPEN) {
514 2df0a3a3 Paolo Bonzini
        if (s->events.new_media) {
515 2df0a3a3 Paolo Bonzini
            event_code = MEC_NEW_MEDIA;
516 2df0a3a3 Paolo Bonzini
            s->events.new_media = false;
517 2df0a3a3 Paolo Bonzini
        } else if (s->events.eject_request) {
518 2df0a3a3 Paolo Bonzini
            event_code = MEC_EJECT_REQUESTED;
519 2df0a3a3 Paolo Bonzini
            s->events.eject_request = false;
520 2df0a3a3 Paolo Bonzini
        }
521 33231e0e Kevin Wolf
    }
522 33231e0e Kevin Wolf
523 33231e0e Kevin Wolf
    buf[4] = event_code;
524 33231e0e Kevin Wolf
    buf[5] = media_status;
525 33231e0e Kevin Wolf
526 33231e0e Kevin Wolf
    /* These fields are reserved, just clear them. */
527 33231e0e Kevin Wolf
    buf[6] = 0;
528 33231e0e Kevin Wolf
    buf[7] = 0;
529 33231e0e Kevin Wolf
530 33231e0e Kevin Wolf
    return 8; /* We wrote to 4 extra bytes from the header */
531 33231e0e Kevin Wolf
}
532 33231e0e Kevin Wolf
533 e1a064f9 Kevin Wolf
static void cmd_get_event_status_notification(IDEState *s,
534 e1a064f9 Kevin Wolf
                                              uint8_t *buf)
535 33231e0e Kevin Wolf
{
536 e1a064f9 Kevin Wolf
    const uint8_t *packet = buf;
537 e1a064f9 Kevin Wolf
538 33231e0e Kevin Wolf
    struct {
539 33231e0e Kevin Wolf
        uint8_t opcode;
540 33231e0e Kevin Wolf
        uint8_t polled;        /* lsb bit is polled; others are reserved */
541 33231e0e Kevin Wolf
        uint8_t reserved2[2];
542 33231e0e Kevin Wolf
        uint8_t class;
543 33231e0e Kevin Wolf
        uint8_t reserved3[2];
544 33231e0e Kevin Wolf
        uint16_t len;
545 33231e0e Kevin Wolf
        uint8_t control;
546 541dc0d4 Stefan Weil
    } QEMU_PACKED *gesn_cdb;
547 33231e0e Kevin Wolf
548 33231e0e Kevin Wolf
    struct {
549 33231e0e Kevin Wolf
        uint16_t len;
550 33231e0e Kevin Wolf
        uint8_t notification_class;
551 33231e0e Kevin Wolf
        uint8_t supported_events;
552 541dc0d4 Stefan Weil
    } QEMU_PACKED *gesn_event_header;
553 33231e0e Kevin Wolf
    unsigned int max_len, used_len;
554 33231e0e Kevin Wolf
555 33231e0e Kevin Wolf
    gesn_cdb = (void *)packet;
556 33231e0e Kevin Wolf
    gesn_event_header = (void *)buf;
557 33231e0e Kevin Wolf
558 33231e0e Kevin Wolf
    max_len = be16_to_cpu(gesn_cdb->len);
559 33231e0e Kevin Wolf
560 33231e0e Kevin Wolf
    /* It is fine by the MMC spec to not support async mode operations */
561 33231e0e Kevin Wolf
    if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
562 33231e0e Kevin Wolf
        /* Only polling is supported, asynchronous mode is not. */
563 67cc61e4 Paolo Bonzini
        ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
564 33231e0e Kevin Wolf
                            ASC_INV_FIELD_IN_CMD_PACKET);
565 33231e0e Kevin Wolf
        return;
566 33231e0e Kevin Wolf
    }
567 33231e0e Kevin Wolf
568 33231e0e Kevin Wolf
    /* polling mode operation */
569 33231e0e Kevin Wolf
570 33231e0e Kevin Wolf
    /*
571 33231e0e Kevin Wolf
     * These are the supported events.
572 33231e0e Kevin Wolf
     *
573 33231e0e Kevin Wolf
     * We currently only support requests of the 'media' type.
574 f0f992e6 Paolo Bonzini
     * Notification class requests and supported event classes are bitmasks,
575 f0f992e6 Paolo Bonzini
     * but they are build from the same values as the "notification class"
576 f0f992e6 Paolo Bonzini
     * field.
577 33231e0e Kevin Wolf
     */
578 f0f992e6 Paolo Bonzini
    gesn_event_header->supported_events = 1 << GESN_MEDIA;
579 33231e0e Kevin Wolf
580 33231e0e Kevin Wolf
    /*
581 33231e0e Kevin Wolf
     * We use |= below to set the class field; other bits in this byte
582 33231e0e Kevin Wolf
     * are reserved now but this is useful to do if we have to use the
583 33231e0e Kevin Wolf
     * reserved fields later.
584 33231e0e Kevin Wolf
     */
585 33231e0e Kevin Wolf
    gesn_event_header->notification_class = 0;
586 33231e0e Kevin Wolf
587 33231e0e Kevin Wolf
    /*
588 33231e0e Kevin Wolf
     * Responses to requests are to be based on request priority.  The
589 33231e0e Kevin Wolf
     * notification_class_request_type enum above specifies the
590 33231e0e Kevin Wolf
     * priority: upper elements are higher prio than lower ones.
591 33231e0e Kevin Wolf
     */
592 f0f992e6 Paolo Bonzini
    if (gesn_cdb->class & (1 << GESN_MEDIA)) {
593 f0f992e6 Paolo Bonzini
        gesn_event_header->notification_class |= GESN_MEDIA;
594 33231e0e Kevin Wolf
        used_len = event_status_media(s, buf);
595 33231e0e Kevin Wolf
    } else {
596 33231e0e Kevin Wolf
        gesn_event_header->notification_class = 0x80; /* No event available */
597 33231e0e Kevin Wolf
        used_len = sizeof(*gesn_event_header);
598 33231e0e Kevin Wolf
    }
599 33231e0e Kevin Wolf
    gesn_event_header->len = cpu_to_be16(used_len
600 33231e0e Kevin Wolf
                                         - sizeof(*gesn_event_header));
601 33231e0e Kevin Wolf
    ide_atapi_cmd_reply(s, used_len, max_len);
602 33231e0e Kevin Wolf
}
603 33231e0e Kevin Wolf
604 a60cf7e7 Kevin Wolf
static void cmd_request_sense(IDEState *s, uint8_t *buf)
605 a60cf7e7 Kevin Wolf
{
606 a60cf7e7 Kevin Wolf
    int max_len = buf[4];
607 a60cf7e7 Kevin Wolf
608 a60cf7e7 Kevin Wolf
    memset(buf, 0, 18);
609 a60cf7e7 Kevin Wolf
    buf[0] = 0x70 | (1 << 7);
610 a60cf7e7 Kevin Wolf
    buf[2] = s->sense_key;
611 a60cf7e7 Kevin Wolf
    buf[7] = 10;
612 a60cf7e7 Kevin Wolf
    buf[12] = s->asc;
613 a60cf7e7 Kevin Wolf
614 67cc61e4 Paolo Bonzini
    if (s->sense_key == UNIT_ATTENTION) {
615 67cc61e4 Paolo Bonzini
        s->sense_key = NO_SENSE;
616 a60cf7e7 Kevin Wolf
    }
617 a60cf7e7 Kevin Wolf
618 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_reply(s, 18, max_len);
619 a60cf7e7 Kevin Wolf
}
620 a60cf7e7 Kevin Wolf
621 a60cf7e7 Kevin Wolf
static void cmd_inquiry(IDEState *s, uint8_t *buf)
622 a60cf7e7 Kevin Wolf
{
623 a60cf7e7 Kevin Wolf
    int max_len = buf[4];
624 a60cf7e7 Kevin Wolf
625 a60cf7e7 Kevin Wolf
    buf[0] = 0x05; /* CD-ROM */
626 a60cf7e7 Kevin Wolf
    buf[1] = 0x80; /* removable */
627 a60cf7e7 Kevin Wolf
    buf[2] = 0x00; /* ISO */
628 a60cf7e7 Kevin Wolf
    buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
629 a60cf7e7 Kevin Wolf
    buf[4] = 31; /* additional length */
630 a60cf7e7 Kevin Wolf
    buf[5] = 0; /* reserved */
631 a60cf7e7 Kevin Wolf
    buf[6] = 0; /* reserved */
632 a60cf7e7 Kevin Wolf
    buf[7] = 0; /* reserved */
633 a60cf7e7 Kevin Wolf
    padstr8(buf + 8, 8, "QEMU");
634 a60cf7e7 Kevin Wolf
    padstr8(buf + 16, 16, "QEMU DVD-ROM");
635 a60cf7e7 Kevin Wolf
    padstr8(buf + 32, 4, s->version);
636 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_reply(s, 36, max_len);
637 a60cf7e7 Kevin Wolf
}
638 a60cf7e7 Kevin Wolf
639 a60cf7e7 Kevin Wolf
static void cmd_get_configuration(IDEState *s, uint8_t *buf)
640 a60cf7e7 Kevin Wolf
{
641 a60cf7e7 Kevin Wolf
    uint32_t len;
642 a60cf7e7 Kevin Wolf
    uint8_t index = 0;
643 a60cf7e7 Kevin Wolf
    int max_len;
644 a60cf7e7 Kevin Wolf
645 a60cf7e7 Kevin Wolf
    /* only feature 0 is supported */
646 a60cf7e7 Kevin Wolf
    if (buf[2] != 0 || buf[3] != 0) {
647 67cc61e4 Paolo Bonzini
        ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
648 a60cf7e7 Kevin Wolf
                            ASC_INV_FIELD_IN_CMD_PACKET);
649 a60cf7e7 Kevin Wolf
        return;
650 a60cf7e7 Kevin Wolf
    }
651 a60cf7e7 Kevin Wolf
652 a60cf7e7 Kevin Wolf
    /* XXX: could result in alignment problems in some architectures */
653 a60cf7e7 Kevin Wolf
    max_len = ube16_to_cpu(buf + 7);
654 a60cf7e7 Kevin Wolf
655 a60cf7e7 Kevin Wolf
    /*
656 a60cf7e7 Kevin Wolf
     * XXX: avoid overflow for io_buffer if max_len is bigger than
657 a60cf7e7 Kevin Wolf
     *      the size of that buffer (dimensioned to max number of
658 a60cf7e7 Kevin Wolf
     *      sectors to transfer at once)
659 a60cf7e7 Kevin Wolf
     *
660 a60cf7e7 Kevin Wolf
     *      Only a problem if the feature/profiles grow.
661 a60cf7e7 Kevin Wolf
     */
662 a60cf7e7 Kevin Wolf
    if (max_len > 512) {
663 a60cf7e7 Kevin Wolf
        /* XXX: assume 1 sector */
664 a60cf7e7 Kevin Wolf
        max_len = 512;
665 a60cf7e7 Kevin Wolf
    }
666 a60cf7e7 Kevin Wolf
667 a60cf7e7 Kevin Wolf
    memset(buf, 0, max_len);
668 a60cf7e7 Kevin Wolf
    /*
669 a60cf7e7 Kevin Wolf
     * the number of sectors from the media tells us which profile
670 a60cf7e7 Kevin Wolf
     * to use as current.  0 means there is no media
671 a60cf7e7 Kevin Wolf
     */
672 a60cf7e7 Kevin Wolf
    if (media_is_dvd(s)) {
673 a60cf7e7 Kevin Wolf
        cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
674 a60cf7e7 Kevin Wolf
    } else if (media_is_cd(s)) {
675 a60cf7e7 Kevin Wolf
        cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
676 a60cf7e7 Kevin Wolf
    }
677 a60cf7e7 Kevin Wolf
678 a60cf7e7 Kevin Wolf
    buf[10] = 0x02 | 0x01; /* persistent and current */
679 a60cf7e7 Kevin Wolf
    len = 12; /* headers: 8 + 4 */
680 a60cf7e7 Kevin Wolf
    len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
681 a60cf7e7 Kevin Wolf
    len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
682 a60cf7e7 Kevin Wolf
    cpu_to_ube32(buf, len - 4); /* data length */
683 a60cf7e7 Kevin Wolf
684 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_reply(s, len, max_len);
685 a60cf7e7 Kevin Wolf
}
686 a60cf7e7 Kevin Wolf
687 a60cf7e7 Kevin Wolf
static void cmd_mode_sense(IDEState *s, uint8_t *buf)
688 a60cf7e7 Kevin Wolf
{
689 a60cf7e7 Kevin Wolf
    int action, code;
690 a60cf7e7 Kevin Wolf
    int max_len;
691 a60cf7e7 Kevin Wolf
692 2c20ae11 Paolo Bonzini
    max_len = ube16_to_cpu(buf + 7);
693 a60cf7e7 Kevin Wolf
    action = buf[2] >> 6;
694 a60cf7e7 Kevin Wolf
    code = buf[2] & 0x3f;
695 a60cf7e7 Kevin Wolf
696 a60cf7e7 Kevin Wolf
    switch(action) {
697 a60cf7e7 Kevin Wolf
    case 0: /* current values */
698 a60cf7e7 Kevin Wolf
        switch(code) {
699 67cc61e4 Paolo Bonzini
        case MODE_PAGE_R_W_ERROR: /* error recovery */
700 2c20ae11 Paolo Bonzini
            cpu_to_ube16(&buf[0], 16 - 2);
701 a60cf7e7 Kevin Wolf
            buf[2] = 0x70;
702 a60cf7e7 Kevin Wolf
            buf[3] = 0;
703 a60cf7e7 Kevin Wolf
            buf[4] = 0;
704 a60cf7e7 Kevin Wolf
            buf[5] = 0;
705 a60cf7e7 Kevin Wolf
            buf[6] = 0;
706 a60cf7e7 Kevin Wolf
            buf[7] = 0;
707 a60cf7e7 Kevin Wolf
708 af0e1ea2 Paolo Bonzini
            buf[8] = MODE_PAGE_R_W_ERROR;
709 af0e1ea2 Paolo Bonzini
            buf[9] = 16 - 10;
710 a60cf7e7 Kevin Wolf
            buf[10] = 0x00;
711 a60cf7e7 Kevin Wolf
            buf[11] = 0x05;
712 a60cf7e7 Kevin Wolf
            buf[12] = 0x00;
713 a60cf7e7 Kevin Wolf
            buf[13] = 0x00;
714 a60cf7e7 Kevin Wolf
            buf[14] = 0x00;
715 a60cf7e7 Kevin Wolf
            buf[15] = 0x00;
716 a60cf7e7 Kevin Wolf
            ide_atapi_cmd_reply(s, 16, max_len);
717 a60cf7e7 Kevin Wolf
            break;
718 67cc61e4 Paolo Bonzini
        case MODE_PAGE_AUDIO_CTL:
719 2c20ae11 Paolo Bonzini
            cpu_to_ube16(&buf[0], 24 - 2);
720 a60cf7e7 Kevin Wolf
            buf[2] = 0x70;
721 a60cf7e7 Kevin Wolf
            buf[3] = 0;
722 a60cf7e7 Kevin Wolf
            buf[4] = 0;
723 a60cf7e7 Kevin Wolf
            buf[5] = 0;
724 a60cf7e7 Kevin Wolf
            buf[6] = 0;
725 a60cf7e7 Kevin Wolf
            buf[7] = 0;
726 a60cf7e7 Kevin Wolf
727 af0e1ea2 Paolo Bonzini
            buf[8] = MODE_PAGE_AUDIO_CTL;
728 af0e1ea2 Paolo Bonzini
            buf[9] = 24 - 10;
729 a60cf7e7 Kevin Wolf
            /* Fill with CDROM audio volume */
730 a60cf7e7 Kevin Wolf
            buf[17] = 0;
731 a60cf7e7 Kevin Wolf
            buf[19] = 0;
732 a60cf7e7 Kevin Wolf
            buf[21] = 0;
733 a60cf7e7 Kevin Wolf
            buf[23] = 0;
734 a60cf7e7 Kevin Wolf
735 a60cf7e7 Kevin Wolf
            ide_atapi_cmd_reply(s, 24, max_len);
736 a60cf7e7 Kevin Wolf
            break;
737 67cc61e4 Paolo Bonzini
        case MODE_PAGE_CAPABILITIES:
738 2c20ae11 Paolo Bonzini
            cpu_to_ube16(&buf[0], 30 - 2);
739 a60cf7e7 Kevin Wolf
            buf[2] = 0x70;
740 a60cf7e7 Kevin Wolf
            buf[3] = 0;
741 a60cf7e7 Kevin Wolf
            buf[4] = 0;
742 a60cf7e7 Kevin Wolf
            buf[5] = 0;
743 a60cf7e7 Kevin Wolf
            buf[6] = 0;
744 a60cf7e7 Kevin Wolf
            buf[7] = 0;
745 a60cf7e7 Kevin Wolf
746 af0e1ea2 Paolo Bonzini
            buf[8] = MODE_PAGE_CAPABILITIES;
747 2c20ae11 Paolo Bonzini
            buf[9] = 30 - 10;
748 a07c7dcd Paolo Bonzini
            buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
749 a60cf7e7 Kevin Wolf
            buf[11] = 0x00;
750 a60cf7e7 Kevin Wolf
751 a60cf7e7 Kevin Wolf
            /* Claim PLAY_AUDIO capability (0x01) since some Linux
752 a60cf7e7 Kevin Wolf
               code checks for this to automount media. */
753 a60cf7e7 Kevin Wolf
            buf[12] = 0x71;
754 a60cf7e7 Kevin Wolf
            buf[13] = 3 << 5;
755 a60cf7e7 Kevin Wolf
            buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
756 a0a7573b Markus Armbruster
            if (s->tray_locked) {
757 a07c7dcd Paolo Bonzini
                buf[14] |= 1 << 1;
758 a0a7573b Markus Armbruster
            }
759 a07c7dcd Paolo Bonzini
            buf[15] = 0x00; /* No volume & mute control, no changer */
760 a07c7dcd Paolo Bonzini
            cpu_to_ube16(&buf[16], 704); /* 4x read speed */
761 a07c7dcd Paolo Bonzini
            buf[18] = 0; /* Two volume levels */
762 a60cf7e7 Kevin Wolf
            buf[19] = 2;
763 a07c7dcd Paolo Bonzini
            cpu_to_ube16(&buf[20], 512); /* 512k buffer */
764 a07c7dcd Paolo Bonzini
            cpu_to_ube16(&buf[22], 704); /* 4x read speed current */
765 a60cf7e7 Kevin Wolf
            buf[24] = 0;
766 a60cf7e7 Kevin Wolf
            buf[25] = 0;
767 a60cf7e7 Kevin Wolf
            buf[26] = 0;
768 a60cf7e7 Kevin Wolf
            buf[27] = 0;
769 2c20ae11 Paolo Bonzini
            buf[28] = 0;
770 2c20ae11 Paolo Bonzini
            buf[29] = 0;
771 2c20ae11 Paolo Bonzini
            ide_atapi_cmd_reply(s, 30, max_len);
772 a60cf7e7 Kevin Wolf
            break;
773 a60cf7e7 Kevin Wolf
        default:
774 a60cf7e7 Kevin Wolf
            goto error_cmd;
775 a60cf7e7 Kevin Wolf
        }
776 a60cf7e7 Kevin Wolf
        break;
777 a60cf7e7 Kevin Wolf
    case 1: /* changeable values */
778 a60cf7e7 Kevin Wolf
        goto error_cmd;
779 a60cf7e7 Kevin Wolf
    case 2: /* default values */
780 a60cf7e7 Kevin Wolf
        goto error_cmd;
781 a60cf7e7 Kevin Wolf
    default:
782 a60cf7e7 Kevin Wolf
    case 3: /* saved values */
783 67cc61e4 Paolo Bonzini
        ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
784 a60cf7e7 Kevin Wolf
                            ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
785 a60cf7e7 Kevin Wolf
        break;
786 a60cf7e7 Kevin Wolf
    }
787 a60cf7e7 Kevin Wolf
    return;
788 a60cf7e7 Kevin Wolf
789 a60cf7e7 Kevin Wolf
error_cmd:
790 67cc61e4 Paolo Bonzini
    ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
791 a60cf7e7 Kevin Wolf
}
792 a60cf7e7 Kevin Wolf
793 a60cf7e7 Kevin Wolf
static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
794 a60cf7e7 Kevin Wolf
{
795 7a2c4b82 Kevin Wolf
    /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
796 7a2c4b82 Kevin Wolf
     * come here, we know that it's ready. */
797 7a2c4b82 Kevin Wolf
    ide_atapi_cmd_ok(s);
798 a60cf7e7 Kevin Wolf
}
799 a60cf7e7 Kevin Wolf
800 a60cf7e7 Kevin Wolf
static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
801 a60cf7e7 Kevin Wolf
{
802 a0a7573b Markus Armbruster
    s->tray_locked = buf[4] & 1;
803 025e849a Markus Armbruster
    bdrv_lock_medium(s->bs, buf[4] & 1);
804 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_ok(s);
805 a60cf7e7 Kevin Wolf
}
806 a60cf7e7 Kevin Wolf
807 a60cf7e7 Kevin Wolf
static void cmd_read(IDEState *s, uint8_t* buf)
808 a60cf7e7 Kevin Wolf
{
809 a60cf7e7 Kevin Wolf
    int nb_sectors, lba;
810 a60cf7e7 Kevin Wolf
811 a60cf7e7 Kevin Wolf
    if (buf[0] == GPCMD_READ_10) {
812 a60cf7e7 Kevin Wolf
        nb_sectors = ube16_to_cpu(buf + 7);
813 a60cf7e7 Kevin Wolf
    } else {
814 a60cf7e7 Kevin Wolf
        nb_sectors = ube32_to_cpu(buf + 6);
815 a60cf7e7 Kevin Wolf
    }
816 a60cf7e7 Kevin Wolf
817 a60cf7e7 Kevin Wolf
    lba = ube32_to_cpu(buf + 2);
818 a60cf7e7 Kevin Wolf
    if (nb_sectors == 0) {
819 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_ok(s);
820 a60cf7e7 Kevin Wolf
        return;
821 a60cf7e7 Kevin Wolf
    }
822 a60cf7e7 Kevin Wolf
823 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
824 a60cf7e7 Kevin Wolf
}
825 a60cf7e7 Kevin Wolf
826 a60cf7e7 Kevin Wolf
static void cmd_read_cd(IDEState *s, uint8_t* buf)
827 a60cf7e7 Kevin Wolf
{
828 a60cf7e7 Kevin Wolf
    int nb_sectors, lba, transfer_request;
829 a60cf7e7 Kevin Wolf
830 a60cf7e7 Kevin Wolf
    nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
831 a60cf7e7 Kevin Wolf
    lba = ube32_to_cpu(buf + 2);
832 a60cf7e7 Kevin Wolf
833 a60cf7e7 Kevin Wolf
    if (nb_sectors == 0) {
834 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_ok(s);
835 a60cf7e7 Kevin Wolf
        return;
836 a60cf7e7 Kevin Wolf
    }
837 a60cf7e7 Kevin Wolf
838 a60cf7e7 Kevin Wolf
    transfer_request = buf[9];
839 a60cf7e7 Kevin Wolf
    switch(transfer_request & 0xf8) {
840 a60cf7e7 Kevin Wolf
    case 0x00:
841 a60cf7e7 Kevin Wolf
        /* nothing */
842 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_ok(s);
843 a60cf7e7 Kevin Wolf
        break;
844 a60cf7e7 Kevin Wolf
    case 0x10:
845 a60cf7e7 Kevin Wolf
        /* normal read */
846 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
847 a60cf7e7 Kevin Wolf
        break;
848 a60cf7e7 Kevin Wolf
    case 0xf8:
849 a60cf7e7 Kevin Wolf
        /* read all data */
850 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
851 a60cf7e7 Kevin Wolf
        break;
852 a60cf7e7 Kevin Wolf
    default:
853 67cc61e4 Paolo Bonzini
        ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
854 a60cf7e7 Kevin Wolf
                            ASC_INV_FIELD_IN_CMD_PACKET);
855 a60cf7e7 Kevin Wolf
        break;
856 a60cf7e7 Kevin Wolf
    }
857 a60cf7e7 Kevin Wolf
}
858 a60cf7e7 Kevin Wolf
859 a60cf7e7 Kevin Wolf
static void cmd_seek(IDEState *s, uint8_t* buf)
860 a60cf7e7 Kevin Wolf
{
861 a60cf7e7 Kevin Wolf
    unsigned int lba;
862 e119bcac Kevin Wolf
    uint64_t total_sectors = s->nb_sectors >> 2;
863 a60cf7e7 Kevin Wolf
864 a60cf7e7 Kevin Wolf
    lba = ube32_to_cpu(buf + 2);
865 a60cf7e7 Kevin Wolf
    if (lba >= total_sectors) {
866 67cc61e4 Paolo Bonzini
        ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
867 a60cf7e7 Kevin Wolf
        return;
868 a60cf7e7 Kevin Wolf
    }
869 a60cf7e7 Kevin Wolf
870 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_ok(s);
871 a60cf7e7 Kevin Wolf
}
872 a60cf7e7 Kevin Wolf
873 a60cf7e7 Kevin Wolf
static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
874 a60cf7e7 Kevin Wolf
{
875 fdec4404 Markus Armbruster
    int sense;
876 f0776564 Markus Armbruster
    bool start = buf[4] & 1;
877 f0776564 Markus Armbruster
    bool loej = buf[4] & 2;     /* load on start, eject on !start */
878 ce560dcf Ronnie Sahlberg
    int pwrcnd = buf[4] & 0xf0;
879 ce560dcf Ronnie Sahlberg
880 ce560dcf Ronnie Sahlberg
    if (pwrcnd) {
881 ce560dcf Ronnie Sahlberg
        /* eject/load only happens for power condition == 0 */
882 ce560dcf Ronnie Sahlberg
        return;
883 ce560dcf Ronnie Sahlberg
    }
884 a60cf7e7 Kevin Wolf
885 f0776564 Markus Armbruster
    if (loej) {
886 48f65b3f Markus Armbruster
        if (!start && !s->tray_open && s->tray_locked) {
887 fdec4404 Markus Armbruster
            sense = bdrv_is_inserted(s->bs)
888 67cc61e4 Paolo Bonzini
                ? NOT_READY : ILLEGAL_REQUEST;
889 fdec4404 Markus Armbruster
            ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
890 fdec4404 Markus Armbruster
            return;
891 a60cf7e7 Kevin Wolf
        }
892 d88b1819 Luiz Capitulino
893 d88b1819 Luiz Capitulino
        if (s->tray_open != !start) {
894 d88b1819 Luiz Capitulino
            bdrv_eject(s->bs, !start);
895 d88b1819 Luiz Capitulino
            s->tray_open = !start;
896 d88b1819 Luiz Capitulino
        }
897 dd063333 Markus Armbruster
    }
898 fdec4404 Markus Armbruster
899 fdec4404 Markus Armbruster
    ide_atapi_cmd_ok(s);
900 a60cf7e7 Kevin Wolf
}
901 a60cf7e7 Kevin Wolf
902 a60cf7e7 Kevin Wolf
static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
903 a60cf7e7 Kevin Wolf
{
904 a60cf7e7 Kevin Wolf
    int max_len = ube16_to_cpu(buf + 8);
905 a60cf7e7 Kevin Wolf
906 a60cf7e7 Kevin Wolf
    cpu_to_ube16(buf, 0);
907 a60cf7e7 Kevin Wolf
    /* no current LBA */
908 a60cf7e7 Kevin Wolf
    buf[2] = 0;
909 a60cf7e7 Kevin Wolf
    buf[3] = 0;
910 a60cf7e7 Kevin Wolf
    buf[4] = 0;
911 a60cf7e7 Kevin Wolf
    buf[5] = 1;
912 a60cf7e7 Kevin Wolf
    cpu_to_ube16(buf + 6, 0);
913 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_reply(s, 8, max_len);
914 a60cf7e7 Kevin Wolf
}
915 a60cf7e7 Kevin Wolf
916 a60cf7e7 Kevin Wolf
static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
917 a60cf7e7 Kevin Wolf
{
918 a60cf7e7 Kevin Wolf
    int format, msf, start_track, len;
919 a60cf7e7 Kevin Wolf
    int max_len;
920 7a2c4b82 Kevin Wolf
    uint64_t total_sectors = s->nb_sectors >> 2;
921 a60cf7e7 Kevin Wolf
922 a60cf7e7 Kevin Wolf
    max_len = ube16_to_cpu(buf + 7);
923 a60cf7e7 Kevin Wolf
    format = buf[9] >> 6;
924 a60cf7e7 Kevin Wolf
    msf = (buf[1] >> 1) & 1;
925 a60cf7e7 Kevin Wolf
    start_track = buf[6];
926 a60cf7e7 Kevin Wolf
927 a60cf7e7 Kevin Wolf
    switch(format) {
928 a60cf7e7 Kevin Wolf
    case 0:
929 a60cf7e7 Kevin Wolf
        len = cdrom_read_toc(total_sectors, buf, msf, start_track);
930 a60cf7e7 Kevin Wolf
        if (len < 0)
931 a60cf7e7 Kevin Wolf
            goto error_cmd;
932 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_reply(s, len, max_len);
933 a60cf7e7 Kevin Wolf
        break;
934 a60cf7e7 Kevin Wolf
    case 1:
935 a60cf7e7 Kevin Wolf
        /* multi session : only a single session defined */
936 a60cf7e7 Kevin Wolf
        memset(buf, 0, 12);
937 a60cf7e7 Kevin Wolf
        buf[1] = 0x0a;
938 a60cf7e7 Kevin Wolf
        buf[2] = 0x01;
939 a60cf7e7 Kevin Wolf
        buf[3] = 0x01;
940 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_reply(s, 12, max_len);
941 a60cf7e7 Kevin Wolf
        break;
942 a60cf7e7 Kevin Wolf
    case 2:
943 a60cf7e7 Kevin Wolf
        len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
944 a60cf7e7 Kevin Wolf
        if (len < 0)
945 a60cf7e7 Kevin Wolf
            goto error_cmd;
946 a60cf7e7 Kevin Wolf
        ide_atapi_cmd_reply(s, len, max_len);
947 a60cf7e7 Kevin Wolf
        break;
948 a60cf7e7 Kevin Wolf
    default:
949 a60cf7e7 Kevin Wolf
    error_cmd:
950 67cc61e4 Paolo Bonzini
        ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
951 a60cf7e7 Kevin Wolf
                            ASC_INV_FIELD_IN_CMD_PACKET);
952 a60cf7e7 Kevin Wolf
    }
953 a60cf7e7 Kevin Wolf
}
954 a60cf7e7 Kevin Wolf
955 a60cf7e7 Kevin Wolf
static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
956 a60cf7e7 Kevin Wolf
{
957 e119bcac Kevin Wolf
    uint64_t total_sectors = s->nb_sectors >> 2;
958 a60cf7e7 Kevin Wolf
959 a60cf7e7 Kevin Wolf
    /* NOTE: it is really the number of sectors minus 1 */
960 a60cf7e7 Kevin Wolf
    cpu_to_ube32(buf, total_sectors - 1);
961 a60cf7e7 Kevin Wolf
    cpu_to_ube32(buf + 4, 2048);
962 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_reply(s, 8, 8);
963 a60cf7e7 Kevin Wolf
}
964 a60cf7e7 Kevin Wolf
965 55042b95 Paolo Bonzini
static void cmd_read_disc_information(IDEState *s, uint8_t* buf)
966 55042b95 Paolo Bonzini
{
967 55042b95 Paolo Bonzini
    uint8_t type = buf[1] & 7;
968 55042b95 Paolo Bonzini
    uint32_t max_len = ube16_to_cpu(buf + 7);
969 55042b95 Paolo Bonzini
970 55042b95 Paolo Bonzini
    /* Types 1/2 are only defined for Blu-Ray.  */
971 55042b95 Paolo Bonzini
    if (type != 0) {
972 55042b95 Paolo Bonzini
        ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
973 55042b95 Paolo Bonzini
                            ASC_INV_FIELD_IN_CMD_PACKET);
974 55042b95 Paolo Bonzini
        return;
975 55042b95 Paolo Bonzini
    }
976 55042b95 Paolo Bonzini
977 55042b95 Paolo Bonzini
    memset(buf, 0, 34);
978 55042b95 Paolo Bonzini
    buf[1] = 32;
979 55042b95 Paolo Bonzini
    buf[2] = 0xe; /* last session complete, disc finalized */
980 55042b95 Paolo Bonzini
    buf[3] = 1;   /* first track on disc */
981 55042b95 Paolo Bonzini
    buf[4] = 1;   /* # of sessions */
982 55042b95 Paolo Bonzini
    buf[5] = 1;   /* first track of last session */
983 55042b95 Paolo Bonzini
    buf[6] = 1;   /* last track of last session */
984 55042b95 Paolo Bonzini
    buf[7] = 0x20; /* unrestricted use */
985 55042b95 Paolo Bonzini
    buf[8] = 0x00; /* CD-ROM or DVD-ROM */
986 55042b95 Paolo Bonzini
    /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
987 55042b95 Paolo Bonzini
    /* 12-23: not meaningful for CD-ROM or DVD-ROM */
988 55042b95 Paolo Bonzini
    /* 24-31: disc bar code */
989 55042b95 Paolo Bonzini
    /* 32: disc application code */
990 55042b95 Paolo Bonzini
    /* 33: number of OPC tables */
991 55042b95 Paolo Bonzini
992 55042b95 Paolo Bonzini
    ide_atapi_cmd_reply(s, 34, max_len);
993 55042b95 Paolo Bonzini
}
994 55042b95 Paolo Bonzini
995 a60cf7e7 Kevin Wolf
static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
996 a60cf7e7 Kevin Wolf
{
997 a60cf7e7 Kevin Wolf
    int max_len;
998 a60cf7e7 Kevin Wolf
    int media = buf[1];
999 a60cf7e7 Kevin Wolf
    int format = buf[7];
1000 a60cf7e7 Kevin Wolf
    int ret;
1001 a60cf7e7 Kevin Wolf
1002 a60cf7e7 Kevin Wolf
    max_len = ube16_to_cpu(buf + 8);
1003 a60cf7e7 Kevin Wolf
1004 a60cf7e7 Kevin Wolf
    if (format < 0xff) {
1005 a60cf7e7 Kevin Wolf
        if (media_is_cd(s)) {
1006 67cc61e4 Paolo Bonzini
            ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1007 a60cf7e7 Kevin Wolf
                                ASC_INCOMPATIBLE_FORMAT);
1008 a60cf7e7 Kevin Wolf
            return;
1009 a60cf7e7 Kevin Wolf
        } else if (!media_present(s)) {
1010 67cc61e4 Paolo Bonzini
            ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1011 a60cf7e7 Kevin Wolf
                                ASC_INV_FIELD_IN_CMD_PACKET);
1012 a60cf7e7 Kevin Wolf
            return;
1013 a60cf7e7 Kevin Wolf
        }
1014 a60cf7e7 Kevin Wolf
    }
1015 a60cf7e7 Kevin Wolf
1016 a60cf7e7 Kevin Wolf
    memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1017 a60cf7e7 Kevin Wolf
           IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1018 a60cf7e7 Kevin Wolf
1019 a60cf7e7 Kevin Wolf
    switch (format) {
1020 a60cf7e7 Kevin Wolf
        case 0x00 ... 0x7f:
1021 a60cf7e7 Kevin Wolf
        case 0xff:
1022 a60cf7e7 Kevin Wolf
            if (media == 0) {
1023 a60cf7e7 Kevin Wolf
                ret = ide_dvd_read_structure(s, format, buf, buf);
1024 a60cf7e7 Kevin Wolf
1025 a60cf7e7 Kevin Wolf
                if (ret < 0) {
1026 67cc61e4 Paolo Bonzini
                    ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
1027 a60cf7e7 Kevin Wolf
                } else {
1028 a60cf7e7 Kevin Wolf
                    ide_atapi_cmd_reply(s, ret, max_len);
1029 a60cf7e7 Kevin Wolf
                }
1030 a60cf7e7 Kevin Wolf
1031 a60cf7e7 Kevin Wolf
                break;
1032 a60cf7e7 Kevin Wolf
            }
1033 a60cf7e7 Kevin Wolf
            /* TODO: BD support, fall through for now */
1034 a60cf7e7 Kevin Wolf
1035 a60cf7e7 Kevin Wolf
        /* Generic disk structures */
1036 a60cf7e7 Kevin Wolf
        case 0x80: /* TODO: AACS volume identifier */
1037 a60cf7e7 Kevin Wolf
        case 0x81: /* TODO: AACS media serial number */
1038 a60cf7e7 Kevin Wolf
        case 0x82: /* TODO: AACS media identifier */
1039 a60cf7e7 Kevin Wolf
        case 0x83: /* TODO: AACS media key block */
1040 a60cf7e7 Kevin Wolf
        case 0x90: /* TODO: List of recognized format layers */
1041 a60cf7e7 Kevin Wolf
        case 0xc0: /* TODO: Write protection status */
1042 a60cf7e7 Kevin Wolf
        default:
1043 67cc61e4 Paolo Bonzini
            ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1044 a60cf7e7 Kevin Wolf
                                ASC_INV_FIELD_IN_CMD_PACKET);
1045 a60cf7e7 Kevin Wolf
            break;
1046 a60cf7e7 Kevin Wolf
    }
1047 a60cf7e7 Kevin Wolf
}
1048 a60cf7e7 Kevin Wolf
1049 a60cf7e7 Kevin Wolf
static void cmd_set_speed(IDEState *s, uint8_t* buf)
1050 a60cf7e7 Kevin Wolf
{
1051 a60cf7e7 Kevin Wolf
    ide_atapi_cmd_ok(s);
1052 a60cf7e7 Kevin Wolf
}
1053 a60cf7e7 Kevin Wolf
1054 e1a064f9 Kevin Wolf
enum {
1055 e1a064f9 Kevin Wolf
    /*
1056 e1a064f9 Kevin Wolf
     * Only commands flagged as ALLOW_UA are allowed to run under a
1057 e1a064f9 Kevin Wolf
     * unit attention condition. (See MMC-5, section 4.1.6.1)
1058 e1a064f9 Kevin Wolf
     */
1059 e1a064f9 Kevin Wolf
    ALLOW_UA = 0x01,
1060 7a2c4b82 Kevin Wolf
1061 7a2c4b82 Kevin Wolf
    /*
1062 7a2c4b82 Kevin Wolf
     * Commands flagged with CHECK_READY can only execute if a medium is present.
1063 7a2c4b82 Kevin Wolf
     * Otherwise they report the Not Ready Condition. (See MMC-5, section
1064 7a2c4b82 Kevin Wolf
     * 4.1.8)
1065 7a2c4b82 Kevin Wolf
     */
1066 7a2c4b82 Kevin Wolf
    CHECK_READY = 0x02,
1067 e1a064f9 Kevin Wolf
};
1068 e1a064f9 Kevin Wolf
1069 e1a064f9 Kevin Wolf
static const struct {
1070 e1a064f9 Kevin Wolf
    void (*handler)(IDEState *s, uint8_t *buf);
1071 e1a064f9 Kevin Wolf
    int flags;
1072 e1a064f9 Kevin Wolf
} atapi_cmd_table[0x100] = {
1073 7a2c4b82 Kevin Wolf
    [ 0x00 ] = { cmd_test_unit_ready,               CHECK_READY },
1074 e1a064f9 Kevin Wolf
    [ 0x03 ] = { cmd_request_sense,                 ALLOW_UA },
1075 e1a064f9 Kevin Wolf
    [ 0x12 ] = { cmd_inquiry,                       ALLOW_UA },
1076 a1aff5bf Markus Armbruster
    [ 0x1b ] = { cmd_start_stop_unit,               0 }, /* [1] */
1077 e1a064f9 Kevin Wolf
    [ 0x1e ] = { cmd_prevent_allow_medium_removal,  0 },
1078 7a2c4b82 Kevin Wolf
    [ 0x25 ] = { cmd_read_cdvd_capacity,            CHECK_READY },
1079 a1aff5bf Markus Armbruster
    [ 0x28 ] = { cmd_read, /* (10) */               CHECK_READY },
1080 7a2c4b82 Kevin Wolf
    [ 0x2b ] = { cmd_seek,                          CHECK_READY },
1081 7a2c4b82 Kevin Wolf
    [ 0x43 ] = { cmd_read_toc_pma_atip,             CHECK_READY },
1082 e1a064f9 Kevin Wolf
    [ 0x46 ] = { cmd_get_configuration,             ALLOW_UA },
1083 e1a064f9 Kevin Wolf
    [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
1084 55042b95 Paolo Bonzini
    [ 0x51 ] = { cmd_read_disc_information,         CHECK_READY },
1085 e1a064f9 Kevin Wolf
    [ 0x5a ] = { cmd_mode_sense, /* (10) */         0 },
1086 a1aff5bf Markus Armbruster
    [ 0xa8 ] = { cmd_read, /* (12) */               CHECK_READY },
1087 a1aff5bf Markus Armbruster
    [ 0xad ] = { cmd_read_dvd_structure,            CHECK_READY },
1088 e1a064f9 Kevin Wolf
    [ 0xbb ] = { cmd_set_speed,                     0 },
1089 e1a064f9 Kevin Wolf
    [ 0xbd ] = { cmd_mechanism_status,              0 },
1090 a1aff5bf Markus Armbruster
    [ 0xbe ] = { cmd_read_cd,                       CHECK_READY },
1091 a1aff5bf Markus Armbruster
    /* [1] handler detects and reports not ready condition itself */
1092 e1a064f9 Kevin Wolf
};
1093 e1a064f9 Kevin Wolf
1094 33231e0e Kevin Wolf
void ide_atapi_cmd(IDEState *s)
1095 33231e0e Kevin Wolf
{
1096 33231e0e Kevin Wolf
    uint8_t *buf;
1097 33231e0e Kevin Wolf
1098 33231e0e Kevin Wolf
    buf = s->io_buffer;
1099 33231e0e Kevin Wolf
#ifdef DEBUG_IDE_ATAPI
1100 33231e0e Kevin Wolf
    {
1101 33231e0e Kevin Wolf
        int i;
1102 33231e0e Kevin Wolf
        printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1103 33231e0e Kevin Wolf
        for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
1104 ab719827 Alon Levy
            printf(" %02x", buf[i]);
1105 33231e0e Kevin Wolf
        }
1106 33231e0e Kevin Wolf
        printf("\n");
1107 33231e0e Kevin Wolf
    }
1108 33231e0e Kevin Wolf
#endif
1109 33231e0e Kevin Wolf
    /*
1110 e1a064f9 Kevin Wolf
     * If there's a UNIT_ATTENTION condition pending, only command flagged with
1111 e1a064f9 Kevin Wolf
     * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1112 e1a064f9 Kevin Wolf
     * condition response unless a higher priority status, defined by the drive
1113 33231e0e Kevin Wolf
     * here, is pending.
1114 33231e0e Kevin Wolf
     */
1115 67cc61e4 Paolo Bonzini
    if (s->sense_key == UNIT_ATTENTION &&
1116 e1a064f9 Kevin Wolf
        !(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA)) {
1117 33231e0e Kevin Wolf
        ide_atapi_cmd_check_status(s);
1118 33231e0e Kevin Wolf
        return;
1119 33231e0e Kevin Wolf
    }
1120 4a737d14 Amit Shah
    /*
1121 4a737d14 Amit Shah
     * When a CD gets changed, we have to report an ejected state and
1122 4a737d14 Amit Shah
     * then a loaded state to guests so that they detect tray
1123 4a737d14 Amit Shah
     * open/close and media change events.  Guests that do not use
1124 4a737d14 Amit Shah
     * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1125 4a737d14 Amit Shah
     * states rely on this behavior.
1126 4a737d14 Amit Shah
     */
1127 0c6f08b0 Pavel Hrdina
    if (!(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA) &&
1128 0c6f08b0 Pavel Hrdina
        !s->tray_open && bdrv_is_inserted(s->bs) && s->cdrom_changed) {
1129 0c6f08b0 Pavel Hrdina
1130 0c6f08b0 Pavel Hrdina
        if (s->cdrom_changed == 1) {
1131 0c6f08b0 Pavel Hrdina
            ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1132 0c6f08b0 Pavel Hrdina
            s->cdrom_changed = 2;
1133 0c6f08b0 Pavel Hrdina
        } else {
1134 0c6f08b0 Pavel Hrdina
            ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED);
1135 0c6f08b0 Pavel Hrdina
            s->cdrom_changed = 0;
1136 0c6f08b0 Pavel Hrdina
        }
1137 33231e0e Kevin Wolf
1138 33231e0e Kevin Wolf
        return;
1139 33231e0e Kevin Wolf
    }
1140 e1a064f9 Kevin Wolf
1141 7a2c4b82 Kevin Wolf
    /* Report a Not Ready condition if appropriate for the command */
1142 7a2c4b82 Kevin Wolf
    if ((atapi_cmd_table[s->io_buffer[0]].flags & CHECK_READY) &&
1143 7a2c4b82 Kevin Wolf
        (!media_present(s) || !bdrv_is_inserted(s->bs)))
1144 7a2c4b82 Kevin Wolf
    {
1145 67cc61e4 Paolo Bonzini
        ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1146 7a2c4b82 Kevin Wolf
        return;
1147 7a2c4b82 Kevin Wolf
    }
1148 7a2c4b82 Kevin Wolf
1149 e1a064f9 Kevin Wolf
    /* Execute the command */
1150 e1a064f9 Kevin Wolf
    if (atapi_cmd_table[s->io_buffer[0]].handler) {
1151 e1a064f9 Kevin Wolf
        atapi_cmd_table[s->io_buffer[0]].handler(s, buf);
1152 e1a064f9 Kevin Wolf
        return;
1153 33231e0e Kevin Wolf
    }
1154 e1a064f9 Kevin Wolf
1155 67cc61e4 Paolo Bonzini
    ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);
1156 33231e0e Kevin Wolf
}