crypto: add support for the cast5-128 cipher algorithm
[qemu/ar7.git] / hw / ide / atapi.c
blob1fe58ab7fd32ba827639b9d9a574b19a6bade1d3
1 /*
2 * QEMU ATAPI Emulation
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "hw/ide/internal.h"
28 #include "hw/scsi/scsi.h"
29 #include "sysemu/block-backend.h"
31 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
33 static void padstr8(uint8_t *buf, int buf_size, const char *src)
35 int i;
36 for(i = 0; i < buf_size; i++) {
37 if (*src)
38 buf[i] = *src++;
39 else
40 buf[i] = ' ';
44 static inline void cpu_to_ube16(uint8_t *buf, int val)
46 buf[0] = val >> 8;
47 buf[1] = val & 0xff;
50 static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
52 buf[0] = val >> 24;
53 buf[1] = val >> 16;
54 buf[2] = val >> 8;
55 buf[3] = val & 0xff;
58 static inline int ube16_to_cpu(const uint8_t *buf)
60 return (buf[0] << 8) | buf[1];
63 static inline int ube32_to_cpu(const uint8_t *buf)
65 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
68 static void lba_to_msf(uint8_t *buf, int lba)
70 lba += 150;
71 buf[0] = (lba / 75) / 60;
72 buf[1] = (lba / 75) % 60;
73 buf[2] = lba % 75;
76 static inline int media_present(IDEState *s)
78 return !s->tray_open && s->nb_sectors > 0;
81 /* XXX: DVDs that could fit on a CD will be reported as a CD */
82 static inline int media_is_dvd(IDEState *s)
84 return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
87 static inline int media_is_cd(IDEState *s)
89 return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
92 static void cd_data_to_raw(uint8_t *buf, int lba)
94 /* sync bytes */
95 buf[0] = 0x00;
96 memset(buf + 1, 0xff, 10);
97 buf[11] = 0x00;
98 buf += 12;
99 /* MSF */
100 lba_to_msf(buf, lba);
101 buf[3] = 0x01; /* mode 1 data */
102 buf += 4;
103 /* data */
104 buf += 2048;
105 /* XXX: ECC not computed */
106 memset(buf, 0, 288);
109 static int
110 cd_read_sector_sync(IDEState *s)
112 int ret;
113 block_acct_start(blk_get_stats(s->blk), &s->acct,
114 4 * BDRV_SECTOR_SIZE, BLOCK_ACCT_READ);
116 #ifdef DEBUG_IDE_ATAPI
117 printf("cd_read_sector_sync: lba=%d\n", s->lba);
118 #endif
120 switch (s->cd_sector_size) {
121 case 2048:
122 ret = blk_read(s->blk, (int64_t)s->lba << 2,
123 s->io_buffer, 4);
124 break;
125 case 2352:
126 ret = blk_read(s->blk, (int64_t)s->lba << 2,
127 s->io_buffer + 16, 4);
128 if (ret >= 0) {
129 cd_data_to_raw(s->io_buffer, s->lba);
131 break;
132 default:
133 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
134 return -EIO;
137 if (ret < 0) {
138 block_acct_failed(blk_get_stats(s->blk), &s->acct);
139 } else {
140 block_acct_done(blk_get_stats(s->blk), &s->acct);
141 s->lba++;
142 s->io_buffer_index = 0;
145 return ret;
148 static void cd_read_sector_cb(void *opaque, int ret)
150 IDEState *s = opaque;
152 #ifdef DEBUG_IDE_ATAPI
153 printf("cd_read_sector_cb: lba=%d ret=%d\n", s->lba, ret);
154 #endif
156 if (ret < 0) {
157 block_acct_failed(blk_get_stats(s->blk), &s->acct);
158 ide_atapi_io_error(s, ret);
159 return;
162 block_acct_done(blk_get_stats(s->blk), &s->acct);
164 if (s->cd_sector_size == 2352) {
165 cd_data_to_raw(s->io_buffer, s->lba);
168 s->lba++;
169 s->io_buffer_index = 0;
170 s->status &= ~BUSY_STAT;
172 ide_atapi_cmd_reply_end(s);
175 static int cd_read_sector(IDEState *s)
177 if (s->cd_sector_size != 2048 && s->cd_sector_size != 2352) {
178 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
179 return -EINVAL;
182 s->iov.iov_base = (s->cd_sector_size == 2352) ?
183 s->io_buffer + 16 : s->io_buffer;
185 s->iov.iov_len = 4 * BDRV_SECTOR_SIZE;
186 qemu_iovec_init_external(&s->qiov, &s->iov, 1);
188 #ifdef DEBUG_IDE_ATAPI
189 printf("cd_read_sector: lba=%d\n", s->lba);
190 #endif
192 block_acct_start(blk_get_stats(s->blk), &s->acct,
193 4 * BDRV_SECTOR_SIZE, BLOCK_ACCT_READ);
195 ide_buffered_readv(s, (int64_t)s->lba << 2, &s->qiov, 4,
196 cd_read_sector_cb, s);
198 s->status |= BUSY_STAT;
199 return 0;
202 void ide_atapi_cmd_ok(IDEState *s)
204 s->error = 0;
205 s->status = READY_STAT | SEEK_STAT;
206 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
207 ide_transfer_stop(s);
208 ide_set_irq(s->bus);
211 void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
213 #ifdef DEBUG_IDE_ATAPI
214 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
215 #endif
216 s->error = sense_key << 4;
217 s->status = READY_STAT | ERR_STAT;
218 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
219 s->sense_key = sense_key;
220 s->asc = asc;
221 ide_transfer_stop(s);
222 ide_set_irq(s->bus);
225 void ide_atapi_io_error(IDEState *s, int ret)
227 /* XXX: handle more errors */
228 if (ret == -ENOMEDIUM) {
229 ide_atapi_cmd_error(s, NOT_READY,
230 ASC_MEDIUM_NOT_PRESENT);
231 } else {
232 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
233 ASC_LOGICAL_BLOCK_OOR);
237 static uint16_t atapi_byte_count_limit(IDEState *s)
239 uint16_t bcl;
241 bcl = s->lcyl | (s->hcyl << 8);
242 if (bcl == 0xffff) {
243 return 0xfffe;
245 return bcl;
248 /* The whole ATAPI transfer logic is handled in this function */
249 void ide_atapi_cmd_reply_end(IDEState *s)
251 int byte_count_limit, size, ret;
252 #ifdef DEBUG_IDE_ATAPI
253 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
254 s->packet_transfer_size,
255 s->elementary_transfer_size,
256 s->io_buffer_index);
257 #endif
258 if (s->packet_transfer_size <= 0) {
259 /* end of transfer */
260 ide_atapi_cmd_ok(s);
261 ide_set_irq(s->bus);
262 #ifdef DEBUG_IDE_ATAPI
263 printf("end of transfer, status=0x%x\n", s->status);
264 #endif
265 } else {
266 /* see if a new sector must be read */
267 if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
268 if (!s->elementary_transfer_size) {
269 ret = cd_read_sector(s);
270 if (ret < 0) {
271 ide_atapi_io_error(s, ret);
273 return;
274 } else {
275 /* rebuffering within an elementary transfer is
276 * only possible with a sync request because we
277 * end up with a race condition otherwise */
278 ret = cd_read_sector_sync(s);
279 if (ret < 0) {
280 ide_atapi_io_error(s, ret);
281 return;
285 if (s->elementary_transfer_size > 0) {
286 /* there are some data left to transmit in this elementary
287 transfer */
288 size = s->cd_sector_size - s->io_buffer_index;
289 if (size > s->elementary_transfer_size)
290 size = s->elementary_transfer_size;
291 s->packet_transfer_size -= size;
292 s->elementary_transfer_size -= size;
293 s->io_buffer_index += size;
294 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
295 size, ide_atapi_cmd_reply_end);
296 } else {
297 /* a new transfer is needed */
298 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
299 byte_count_limit = atapi_byte_count_limit(s);
300 #ifdef DEBUG_IDE_ATAPI
301 printf("byte_count_limit=%d\n", byte_count_limit);
302 #endif
303 size = s->packet_transfer_size;
304 if (size > byte_count_limit) {
305 /* byte count limit must be even if this case */
306 if (byte_count_limit & 1)
307 byte_count_limit--;
308 size = byte_count_limit;
310 s->lcyl = size;
311 s->hcyl = size >> 8;
312 s->elementary_transfer_size = size;
313 /* we cannot transmit more than one sector at a time */
314 if (s->lba != -1) {
315 if (size > (s->cd_sector_size - s->io_buffer_index))
316 size = (s->cd_sector_size - s->io_buffer_index);
318 s->packet_transfer_size -= size;
319 s->elementary_transfer_size -= size;
320 s->io_buffer_index += size;
321 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
322 size, ide_atapi_cmd_reply_end);
323 ide_set_irq(s->bus);
324 #ifdef DEBUG_IDE_ATAPI
325 printf("status=0x%x\n", s->status);
326 #endif
331 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
332 static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
334 if (size > max_size)
335 size = max_size;
336 s->lba = -1; /* no sector read */
337 s->packet_transfer_size = size;
338 s->io_buffer_size = size; /* dma: send the reply data as one chunk */
339 s->elementary_transfer_size = 0;
341 if (s->atapi_dma) {
342 block_acct_start(blk_get_stats(s->blk), &s->acct, size,
343 BLOCK_ACCT_READ);
344 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
345 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
346 } else {
347 s->status = READY_STAT | SEEK_STAT;
348 s->io_buffer_index = 0;
349 ide_atapi_cmd_reply_end(s);
353 /* start a CD-CDROM read command */
354 static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
355 int sector_size)
357 s->lba = lba;
358 s->packet_transfer_size = nb_sectors * sector_size;
359 s->elementary_transfer_size = 0;
360 s->io_buffer_index = sector_size;
361 s->cd_sector_size = sector_size;
363 ide_atapi_cmd_reply_end(s);
366 static void ide_atapi_cmd_check_status(IDEState *s)
368 #ifdef DEBUG_IDE_ATAPI
369 printf("atapi_cmd_check_status\n");
370 #endif
371 s->error = MC_ERR | (UNIT_ATTENTION << 4);
372 s->status = ERR_STAT;
373 s->nsector = 0;
374 ide_set_irq(s->bus);
376 /* ATAPI DMA support */
378 /* XXX: handle read errors */
379 static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
381 IDEState *s = opaque;
382 int data_offset, n;
384 if (ret < 0) {
385 ide_atapi_io_error(s, ret);
386 goto eot;
389 if (s->io_buffer_size > 0) {
391 * For a cdrom read sector command (s->lba != -1),
392 * adjust the lba for the next s->io_buffer_size chunk
393 * and dma the current chunk.
394 * For a command != read (s->lba == -1), just transfer
395 * the reply data.
397 if (s->lba != -1) {
398 if (s->cd_sector_size == 2352) {
399 n = 1;
400 cd_data_to_raw(s->io_buffer, s->lba);
401 } else {
402 n = s->io_buffer_size >> 11;
404 s->lba += n;
406 s->packet_transfer_size -= s->io_buffer_size;
407 if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
408 goto eot;
411 if (s->packet_transfer_size <= 0) {
412 s->status = READY_STAT | SEEK_STAT;
413 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
414 ide_set_irq(s->bus);
415 goto eot;
418 s->io_buffer_index = 0;
419 if (s->cd_sector_size == 2352) {
420 n = 1;
421 s->io_buffer_size = s->cd_sector_size;
422 data_offset = 16;
423 } else {
424 n = s->packet_transfer_size >> 11;
425 if (n > (IDE_DMA_BUF_SECTORS / 4))
426 n = (IDE_DMA_BUF_SECTORS / 4);
427 s->io_buffer_size = n * 2048;
428 data_offset = 0;
430 #ifdef DEBUG_AIO
431 printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
432 #endif
434 s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
435 s->bus->dma->iov.iov_len = n * 4 * 512;
436 qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
438 s->bus->dma->aiocb = ide_buffered_readv(s, (int64_t)s->lba << 2,
439 &s->bus->dma->qiov, n * 4,
440 ide_atapi_cmd_read_dma_cb, s);
441 return;
443 eot:
444 if (ret < 0) {
445 block_acct_failed(blk_get_stats(s->blk), &s->acct);
446 } else {
447 block_acct_done(blk_get_stats(s->blk), &s->acct);
449 ide_set_inactive(s, false);
452 /* start a CD-CDROM read command with DMA */
453 /* XXX: test if DMA is available */
454 static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
455 int sector_size)
457 s->lba = lba;
458 s->packet_transfer_size = nb_sectors * sector_size;
459 s->io_buffer_size = 0;
460 s->cd_sector_size = sector_size;
462 block_acct_start(blk_get_stats(s->blk), &s->acct, s->packet_transfer_size,
463 BLOCK_ACCT_READ);
465 /* XXX: check if BUSY_STAT should be set */
466 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
467 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
470 static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
471 int sector_size)
473 #ifdef DEBUG_IDE_ATAPI
474 printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
475 lba, nb_sectors);
476 #endif
477 if (s->atapi_dma) {
478 ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
479 } else {
480 ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
485 /* Called by *_restart_bh when the transfer function points
486 * to ide_atapi_cmd
488 void ide_atapi_dma_restart(IDEState *s)
491 * I'm not sure we have enough stored to restart the command
492 * safely, so give the guest an error it should recover from.
493 * I'm assuming most guests will try to recover from something
494 * listed as a medium error on a CD; it seems to work on Linux.
495 * This would be more of a problem if we did any other type of
496 * DMA operation.
498 ide_atapi_cmd_error(s, MEDIUM_ERROR, ASC_NO_SEEK_COMPLETE);
501 static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
502 uint16_t profile)
504 uint8_t *buf_profile = buf + 12; /* start of profiles */
506 buf_profile += ((*index) * 4); /* start of indexed profile */
507 cpu_to_ube16 (buf_profile, profile);
508 buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
510 /* each profile adds 4 bytes to the response */
511 (*index)++;
512 buf[11] += 4; /* Additional Length */
514 return 4;
517 static int ide_dvd_read_structure(IDEState *s, int format,
518 const uint8_t *packet, uint8_t *buf)
520 switch (format) {
521 case 0x0: /* Physical format information */
523 int layer = packet[6];
524 uint64_t total_sectors;
526 if (layer != 0)
527 return -ASC_INV_FIELD_IN_CMD_PACKET;
529 total_sectors = s->nb_sectors >> 2;
530 if (total_sectors == 0) {
531 return -ASC_MEDIUM_NOT_PRESENT;
534 buf[4] = 1; /* DVD-ROM, part version 1 */
535 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
536 buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
537 buf[7] = 0; /* default densities */
539 /* FIXME: 0x30000 per spec? */
540 cpu_to_ube32(buf + 8, 0); /* start sector */
541 cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
542 cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
544 /* Size of buffer, not including 2 byte size field */
545 stw_be_p(buf, 2048 + 2);
547 /* 2k data + 4 byte header */
548 return (2048 + 4);
551 case 0x01: /* DVD copyright information */
552 buf[4] = 0; /* no copyright data */
553 buf[5] = 0; /* no region restrictions */
555 /* Size of buffer, not including 2 byte size field */
556 stw_be_p(buf, 4 + 2);
558 /* 4 byte header + 4 byte data */
559 return (4 + 4);
561 case 0x03: /* BCA information - invalid field for no BCA info */
562 return -ASC_INV_FIELD_IN_CMD_PACKET;
564 case 0x04: /* DVD disc manufacturing information */
565 /* Size of buffer, not including 2 byte size field */
566 stw_be_p(buf, 2048 + 2);
568 /* 2k data + 4 byte header */
569 return (2048 + 4);
571 case 0xff:
573 * This lists all the command capabilities above. Add new ones
574 * in order and update the length and buffer return values.
577 buf[4] = 0x00; /* Physical format */
578 buf[5] = 0x40; /* Not writable, is readable */
579 stw_be_p(buf + 6, 2048 + 4);
581 buf[8] = 0x01; /* Copyright info */
582 buf[9] = 0x40; /* Not writable, is readable */
583 stw_be_p(buf + 10, 4 + 4);
585 buf[12] = 0x03; /* BCA info */
586 buf[13] = 0x40; /* Not writable, is readable */
587 stw_be_p(buf + 14, 188 + 4);
589 buf[16] = 0x04; /* Manufacturing info */
590 buf[17] = 0x40; /* Not writable, is readable */
591 stw_be_p(buf + 18, 2048 + 4);
593 /* Size of buffer, not including 2 byte size field */
594 stw_be_p(buf, 16 + 2);
596 /* data written + 4 byte header */
597 return (16 + 4);
599 default: /* TODO: formats beyond DVD-ROM requires */
600 return -ASC_INV_FIELD_IN_CMD_PACKET;
604 static unsigned int event_status_media(IDEState *s,
605 uint8_t *buf)
607 uint8_t event_code, media_status;
609 media_status = 0;
610 if (s->tray_open) {
611 media_status = MS_TRAY_OPEN;
612 } else if (blk_is_inserted(s->blk)) {
613 media_status = MS_MEDIA_PRESENT;
616 /* Event notification descriptor */
617 event_code = MEC_NO_CHANGE;
618 if (media_status != MS_TRAY_OPEN) {
619 if (s->events.new_media) {
620 event_code = MEC_NEW_MEDIA;
621 s->events.new_media = false;
622 } else if (s->events.eject_request) {
623 event_code = MEC_EJECT_REQUESTED;
624 s->events.eject_request = false;
628 buf[4] = event_code;
629 buf[5] = media_status;
631 /* These fields are reserved, just clear them. */
632 buf[6] = 0;
633 buf[7] = 0;
635 return 8; /* We wrote to 4 extra bytes from the header */
638 static void cmd_get_event_status_notification(IDEState *s,
639 uint8_t *buf)
641 const uint8_t *packet = buf;
643 struct {
644 uint8_t opcode;
645 uint8_t polled; /* lsb bit is polled; others are reserved */
646 uint8_t reserved2[2];
647 uint8_t class;
648 uint8_t reserved3[2];
649 uint16_t len;
650 uint8_t control;
651 } QEMU_PACKED *gesn_cdb;
653 struct {
654 uint16_t len;
655 uint8_t notification_class;
656 uint8_t supported_events;
657 } QEMU_PACKED *gesn_event_header;
658 unsigned int max_len, used_len;
660 gesn_cdb = (void *)packet;
661 gesn_event_header = (void *)buf;
663 max_len = be16_to_cpu(gesn_cdb->len);
665 /* It is fine by the MMC spec to not support async mode operations */
666 if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
667 /* Only polling is supported, asynchronous mode is not. */
668 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
669 ASC_INV_FIELD_IN_CMD_PACKET);
670 return;
673 /* polling mode operation */
676 * These are the supported events.
678 * We currently only support requests of the 'media' type.
679 * Notification class requests and supported event classes are bitmasks,
680 * but they are build from the same values as the "notification class"
681 * field.
683 gesn_event_header->supported_events = 1 << GESN_MEDIA;
686 * We use |= below to set the class field; other bits in this byte
687 * are reserved now but this is useful to do if we have to use the
688 * reserved fields later.
690 gesn_event_header->notification_class = 0;
693 * Responses to requests are to be based on request priority. The
694 * notification_class_request_type enum above specifies the
695 * priority: upper elements are higher prio than lower ones.
697 if (gesn_cdb->class & (1 << GESN_MEDIA)) {
698 gesn_event_header->notification_class |= GESN_MEDIA;
699 used_len = event_status_media(s, buf);
700 } else {
701 gesn_event_header->notification_class = 0x80; /* No event available */
702 used_len = sizeof(*gesn_event_header);
704 gesn_event_header->len = cpu_to_be16(used_len
705 - sizeof(*gesn_event_header));
706 ide_atapi_cmd_reply(s, used_len, max_len);
709 static void cmd_request_sense(IDEState *s, uint8_t *buf)
711 int max_len = buf[4];
713 memset(buf, 0, 18);
714 buf[0] = 0x70 | (1 << 7);
715 buf[2] = s->sense_key;
716 buf[7] = 10;
717 buf[12] = s->asc;
719 if (s->sense_key == UNIT_ATTENTION) {
720 s->sense_key = NO_SENSE;
723 ide_atapi_cmd_reply(s, 18, max_len);
726 static void cmd_inquiry(IDEState *s, uint8_t *buf)
728 uint8_t page_code = buf[2];
729 int max_len = buf[4];
731 unsigned idx = 0;
732 unsigned size_idx;
733 unsigned preamble_len;
735 /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
736 * we are being asked for a specific page of info indicated by byte 2. */
737 if (buf[1] & 0x01) {
738 preamble_len = 4;
739 size_idx = 3;
741 buf[idx++] = 0x05; /* CD-ROM */
742 buf[idx++] = page_code; /* Page Code */
743 buf[idx++] = 0x00; /* reserved */
744 idx++; /* length (set later) */
746 switch (page_code) {
747 case 0x00:
748 /* Supported Pages: List of supported VPD responses. */
749 buf[idx++] = 0x00; /* 0x00: Supported Pages, and: */
750 buf[idx++] = 0x83; /* 0x83: Device Identification. */
751 break;
753 case 0x83:
754 /* Device Identification. Each entry is optional, but the entries
755 * included here are modeled after libata's VPD responses.
756 * If the response is given, at least one entry must be present. */
758 /* Entry 1: Serial */
759 if (idx + 24 > max_len) {
760 /* Not enough room for even the first entry: */
761 /* 4 byte header + 20 byte string */
762 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
763 ASC_DATA_PHASE_ERROR);
764 return;
766 buf[idx++] = 0x02; /* Ascii */
767 buf[idx++] = 0x00; /* Vendor Specific */
768 buf[idx++] = 0x00;
769 buf[idx++] = 20; /* Remaining length */
770 padstr8(buf + idx, 20, s->drive_serial_str);
771 idx += 20;
773 /* Entry 2: Drive Model and Serial */
774 if (idx + 72 > max_len) {
775 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
776 goto out;
778 buf[idx++] = 0x02; /* Ascii */
779 buf[idx++] = 0x01; /* T10 Vendor */
780 buf[idx++] = 0x00;
781 buf[idx++] = 68;
782 padstr8(buf + idx, 8, "ATA"); /* Generic T10 vendor */
783 idx += 8;
784 padstr8(buf + idx, 40, s->drive_model_str);
785 idx += 40;
786 padstr8(buf + idx, 20, s->drive_serial_str);
787 idx += 20;
789 /* Entry 3: WWN */
790 if (s->wwn && (idx + 12 <= max_len)) {
791 /* 4 byte header + 8 byte wwn */
792 buf[idx++] = 0x01; /* Binary */
793 buf[idx++] = 0x03; /* NAA */
794 buf[idx++] = 0x00;
795 buf[idx++] = 0x08;
796 stq_be_p(&buf[idx], s->wwn);
797 idx += 8;
799 break;
801 default:
802 /* SPC-3, revision 23 sec. 6.4 */
803 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
804 ASC_INV_FIELD_IN_CMD_PACKET);
805 return;
807 } else {
808 preamble_len = 5;
809 size_idx = 4;
811 buf[0] = 0x05; /* CD-ROM */
812 buf[1] = 0x80; /* removable */
813 buf[2] = 0x00; /* ISO */
814 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
815 /* buf[size_idx] set below. */
816 buf[5] = 0; /* reserved */
817 buf[6] = 0; /* reserved */
818 buf[7] = 0; /* reserved */
819 padstr8(buf + 8, 8, "QEMU");
820 padstr8(buf + 16, 16, "QEMU DVD-ROM");
821 padstr8(buf + 32, 4, s->version);
822 idx = 36;
825 out:
826 buf[size_idx] = idx - preamble_len;
827 ide_atapi_cmd_reply(s, idx, max_len);
830 static void cmd_get_configuration(IDEState *s, uint8_t *buf)
832 uint32_t len;
833 uint8_t index = 0;
834 int max_len;
836 /* only feature 0 is supported */
837 if (buf[2] != 0 || buf[3] != 0) {
838 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
839 ASC_INV_FIELD_IN_CMD_PACKET);
840 return;
843 /* XXX: could result in alignment problems in some architectures */
844 max_len = ube16_to_cpu(buf + 7);
847 * XXX: avoid overflow for io_buffer if max_len is bigger than
848 * the size of that buffer (dimensioned to max number of
849 * sectors to transfer at once)
851 * Only a problem if the feature/profiles grow.
853 if (max_len > 512) {
854 /* XXX: assume 1 sector */
855 max_len = 512;
858 memset(buf, 0, max_len);
860 * the number of sectors from the media tells us which profile
861 * to use as current. 0 means there is no media
863 if (media_is_dvd(s)) {
864 cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
865 } else if (media_is_cd(s)) {
866 cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
869 buf[10] = 0x02 | 0x01; /* persistent and current */
870 len = 12; /* headers: 8 + 4 */
871 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
872 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
873 cpu_to_ube32(buf, len - 4); /* data length */
875 ide_atapi_cmd_reply(s, len, max_len);
878 static void cmd_mode_sense(IDEState *s, uint8_t *buf)
880 int action, code;
881 int max_len;
883 max_len = ube16_to_cpu(buf + 7);
884 action = buf[2] >> 6;
885 code = buf[2] & 0x3f;
887 switch(action) {
888 case 0: /* current values */
889 switch(code) {
890 case MODE_PAGE_R_W_ERROR: /* error recovery */
891 cpu_to_ube16(&buf[0], 16 - 2);
892 buf[2] = 0x70;
893 buf[3] = 0;
894 buf[4] = 0;
895 buf[5] = 0;
896 buf[6] = 0;
897 buf[7] = 0;
899 buf[8] = MODE_PAGE_R_W_ERROR;
900 buf[9] = 16 - 10;
901 buf[10] = 0x00;
902 buf[11] = 0x05;
903 buf[12] = 0x00;
904 buf[13] = 0x00;
905 buf[14] = 0x00;
906 buf[15] = 0x00;
907 ide_atapi_cmd_reply(s, 16, max_len);
908 break;
909 case MODE_PAGE_AUDIO_CTL:
910 cpu_to_ube16(&buf[0], 24 - 2);
911 buf[2] = 0x70;
912 buf[3] = 0;
913 buf[4] = 0;
914 buf[5] = 0;
915 buf[6] = 0;
916 buf[7] = 0;
918 buf[8] = MODE_PAGE_AUDIO_CTL;
919 buf[9] = 24 - 10;
920 /* Fill with CDROM audio volume */
921 buf[17] = 0;
922 buf[19] = 0;
923 buf[21] = 0;
924 buf[23] = 0;
926 ide_atapi_cmd_reply(s, 24, max_len);
927 break;
928 case MODE_PAGE_CAPABILITIES:
929 cpu_to_ube16(&buf[0], 30 - 2);
930 buf[2] = 0x70;
931 buf[3] = 0;
932 buf[4] = 0;
933 buf[5] = 0;
934 buf[6] = 0;
935 buf[7] = 0;
937 buf[8] = MODE_PAGE_CAPABILITIES;
938 buf[9] = 30 - 10;
939 buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
940 buf[11] = 0x00;
942 /* Claim PLAY_AUDIO capability (0x01) since some Linux
943 code checks for this to automount media. */
944 buf[12] = 0x71;
945 buf[13] = 3 << 5;
946 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
947 if (s->tray_locked) {
948 buf[14] |= 1 << 1;
950 buf[15] = 0x00; /* No volume & mute control, no changer */
951 cpu_to_ube16(&buf[16], 704); /* 4x read speed */
952 buf[18] = 0; /* Two volume levels */
953 buf[19] = 2;
954 cpu_to_ube16(&buf[20], 512); /* 512k buffer */
955 cpu_to_ube16(&buf[22], 704); /* 4x read speed current */
956 buf[24] = 0;
957 buf[25] = 0;
958 buf[26] = 0;
959 buf[27] = 0;
960 buf[28] = 0;
961 buf[29] = 0;
962 ide_atapi_cmd_reply(s, 30, max_len);
963 break;
964 default:
965 goto error_cmd;
967 break;
968 case 1: /* changeable values */
969 goto error_cmd;
970 case 2: /* default values */
971 goto error_cmd;
972 default:
973 case 3: /* saved values */
974 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
975 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
976 break;
978 return;
980 error_cmd:
981 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
984 static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
986 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
987 * come here, we know that it's ready. */
988 ide_atapi_cmd_ok(s);
991 static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
993 s->tray_locked = buf[4] & 1;
994 blk_lock_medium(s->blk, buf[4] & 1);
995 ide_atapi_cmd_ok(s);
998 static void cmd_read(IDEState *s, uint8_t* buf)
1000 int nb_sectors, lba;
1002 if (buf[0] == GPCMD_READ_10) {
1003 nb_sectors = ube16_to_cpu(buf + 7);
1004 } else {
1005 nb_sectors = ube32_to_cpu(buf + 6);
1008 lba = ube32_to_cpu(buf + 2);
1009 if (nb_sectors == 0) {
1010 ide_atapi_cmd_ok(s);
1011 return;
1014 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1017 static void cmd_read_cd(IDEState *s, uint8_t* buf)
1019 int nb_sectors, lba, transfer_request;
1021 nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
1022 lba = ube32_to_cpu(buf + 2);
1024 if (nb_sectors == 0) {
1025 ide_atapi_cmd_ok(s);
1026 return;
1029 transfer_request = buf[9];
1030 switch(transfer_request & 0xf8) {
1031 case 0x00:
1032 /* nothing */
1033 ide_atapi_cmd_ok(s);
1034 break;
1035 case 0x10:
1036 /* normal read */
1037 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1038 break;
1039 case 0xf8:
1040 /* read all data */
1041 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
1042 break;
1043 default:
1044 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1045 ASC_INV_FIELD_IN_CMD_PACKET);
1046 break;
1050 static void cmd_seek(IDEState *s, uint8_t* buf)
1052 unsigned int lba;
1053 uint64_t total_sectors = s->nb_sectors >> 2;
1055 lba = ube32_to_cpu(buf + 2);
1056 if (lba >= total_sectors) {
1057 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1058 return;
1061 ide_atapi_cmd_ok(s);
1064 static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
1066 int sense;
1067 bool start = buf[4] & 1;
1068 bool loej = buf[4] & 2; /* load on start, eject on !start */
1069 int pwrcnd = buf[4] & 0xf0;
1071 if (pwrcnd) {
1072 /* eject/load only happens for power condition == 0 */
1073 ide_atapi_cmd_ok(s);
1074 return;
1077 if (loej) {
1078 if (!start && !s->tray_open && s->tray_locked) {
1079 sense = blk_is_inserted(s->blk)
1080 ? NOT_READY : ILLEGAL_REQUEST;
1081 ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
1082 return;
1085 if (s->tray_open != !start) {
1086 blk_eject(s->blk, !start);
1087 s->tray_open = !start;
1091 ide_atapi_cmd_ok(s);
1094 static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
1096 int max_len = ube16_to_cpu(buf + 8);
1098 cpu_to_ube16(buf, 0);
1099 /* no current LBA */
1100 buf[2] = 0;
1101 buf[3] = 0;
1102 buf[4] = 0;
1103 buf[5] = 1;
1104 cpu_to_ube16(buf + 6, 0);
1105 ide_atapi_cmd_reply(s, 8, max_len);
1108 static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
1110 int format, msf, start_track, len;
1111 int max_len;
1112 uint64_t total_sectors = s->nb_sectors >> 2;
1114 max_len = ube16_to_cpu(buf + 7);
1115 format = buf[9] >> 6;
1116 msf = (buf[1] >> 1) & 1;
1117 start_track = buf[6];
1119 switch(format) {
1120 case 0:
1121 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
1122 if (len < 0)
1123 goto error_cmd;
1124 ide_atapi_cmd_reply(s, len, max_len);
1125 break;
1126 case 1:
1127 /* multi session : only a single session defined */
1128 memset(buf, 0, 12);
1129 buf[1] = 0x0a;
1130 buf[2] = 0x01;
1131 buf[3] = 0x01;
1132 ide_atapi_cmd_reply(s, 12, max_len);
1133 break;
1134 case 2:
1135 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
1136 if (len < 0)
1137 goto error_cmd;
1138 ide_atapi_cmd_reply(s, len, max_len);
1139 break;
1140 default:
1141 error_cmd:
1142 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1143 ASC_INV_FIELD_IN_CMD_PACKET);
1147 static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
1149 uint64_t total_sectors = s->nb_sectors >> 2;
1151 /* NOTE: it is really the number of sectors minus 1 */
1152 cpu_to_ube32(buf, total_sectors - 1);
1153 cpu_to_ube32(buf + 4, 2048);
1154 ide_atapi_cmd_reply(s, 8, 8);
1157 static void cmd_read_disc_information(IDEState *s, uint8_t* buf)
1159 uint8_t type = buf[1] & 7;
1160 uint32_t max_len = ube16_to_cpu(buf + 7);
1162 /* Types 1/2 are only defined for Blu-Ray. */
1163 if (type != 0) {
1164 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1165 ASC_INV_FIELD_IN_CMD_PACKET);
1166 return;
1169 memset(buf, 0, 34);
1170 buf[1] = 32;
1171 buf[2] = 0xe; /* last session complete, disc finalized */
1172 buf[3] = 1; /* first track on disc */
1173 buf[4] = 1; /* # of sessions */
1174 buf[5] = 1; /* first track of last session */
1175 buf[6] = 1; /* last track of last session */
1176 buf[7] = 0x20; /* unrestricted use */
1177 buf[8] = 0x00; /* CD-ROM or DVD-ROM */
1178 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
1179 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
1180 /* 24-31: disc bar code */
1181 /* 32: disc application code */
1182 /* 33: number of OPC tables */
1184 ide_atapi_cmd_reply(s, 34, max_len);
1187 static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
1189 int max_len;
1190 int media = buf[1];
1191 int format = buf[7];
1192 int ret;
1194 max_len = ube16_to_cpu(buf + 8);
1196 if (format < 0xff) {
1197 if (media_is_cd(s)) {
1198 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1199 ASC_INCOMPATIBLE_FORMAT);
1200 return;
1201 } else if (!media_present(s)) {
1202 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1203 ASC_INV_FIELD_IN_CMD_PACKET);
1204 return;
1208 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1209 IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1211 switch (format) {
1212 case 0x00 ... 0x7f:
1213 case 0xff:
1214 if (media == 0) {
1215 ret = ide_dvd_read_structure(s, format, buf, buf);
1217 if (ret < 0) {
1218 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
1219 } else {
1220 ide_atapi_cmd_reply(s, ret, max_len);
1223 break;
1225 /* TODO: BD support, fall through for now */
1227 /* Generic disk structures */
1228 case 0x80: /* TODO: AACS volume identifier */
1229 case 0x81: /* TODO: AACS media serial number */
1230 case 0x82: /* TODO: AACS media identifier */
1231 case 0x83: /* TODO: AACS media key block */
1232 case 0x90: /* TODO: List of recognized format layers */
1233 case 0xc0: /* TODO: Write protection status */
1234 default:
1235 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1236 ASC_INV_FIELD_IN_CMD_PACKET);
1237 break;
1241 static void cmd_set_speed(IDEState *s, uint8_t* buf)
1243 ide_atapi_cmd_ok(s);
1246 enum {
1248 * Only commands flagged as ALLOW_UA are allowed to run under a
1249 * unit attention condition. (See MMC-5, section 4.1.6.1)
1251 ALLOW_UA = 0x01,
1254 * Commands flagged with CHECK_READY can only execute if a medium is present.
1255 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1256 * 4.1.8)
1258 CHECK_READY = 0x02,
1261 * Commands flagged with NONDATA do not in any circumstances return
1262 * any data via ide_atapi_cmd_reply. These commands are exempt from
1263 * the normal byte_count_limit constraints.
1264 * See ATA8-ACS3 "7.21.5 Byte Count Limit"
1266 NONDATA = 0x04,
1269 static const struct AtapiCmd {
1270 void (*handler)(IDEState *s, uint8_t *buf);
1271 int flags;
1272 } atapi_cmd_table[0x100] = {
1273 [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY | NONDATA },
1274 [ 0x03 ] = { cmd_request_sense, ALLOW_UA },
1275 [ 0x12 ] = { cmd_inquiry, ALLOW_UA },
1276 [ 0x1b ] = { cmd_start_stop_unit, NONDATA }, /* [1] */
1277 [ 0x1e ] = { cmd_prevent_allow_medium_removal, NONDATA },
1278 [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY },
1279 [ 0x28 ] = { cmd_read, /* (10) */ CHECK_READY },
1280 [ 0x2b ] = { cmd_seek, CHECK_READY | NONDATA },
1281 [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY },
1282 [ 0x46 ] = { cmd_get_configuration, ALLOW_UA },
1283 [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
1284 [ 0x51 ] = { cmd_read_disc_information, CHECK_READY },
1285 [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 },
1286 [ 0xa8 ] = { cmd_read, /* (12) */ CHECK_READY },
1287 [ 0xad ] = { cmd_read_dvd_structure, CHECK_READY },
1288 [ 0xbb ] = { cmd_set_speed, NONDATA },
1289 [ 0xbd ] = { cmd_mechanism_status, 0 },
1290 [ 0xbe ] = { cmd_read_cd, CHECK_READY },
1291 /* [1] handler detects and reports not ready condition itself */
1294 void ide_atapi_cmd(IDEState *s)
1296 uint8_t *buf = s->io_buffer;
1297 const struct AtapiCmd *cmd = &atapi_cmd_table[s->io_buffer[0]];
1299 #ifdef DEBUG_IDE_ATAPI
1301 int i;
1302 printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1303 for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
1304 printf(" %02x", buf[i]);
1306 printf("\n");
1308 #endif
1311 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1312 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1313 * condition response unless a higher priority status, defined by the drive
1314 * here, is pending.
1316 if (s->sense_key == UNIT_ATTENTION && !(cmd->flags & ALLOW_UA)) {
1317 ide_atapi_cmd_check_status(s);
1318 return;
1321 * When a CD gets changed, we have to report an ejected state and
1322 * then a loaded state to guests so that they detect tray
1323 * open/close and media change events. Guests that do not use
1324 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1325 * states rely on this behavior.
1327 if (!(cmd->flags & ALLOW_UA) &&
1328 !s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) {
1330 if (s->cdrom_changed == 1) {
1331 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1332 s->cdrom_changed = 2;
1333 } else {
1334 ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED);
1335 s->cdrom_changed = 0;
1338 return;
1341 /* Report a Not Ready condition if appropriate for the command */
1342 if ((cmd->flags & CHECK_READY) &&
1343 (!media_present(s) || !blk_is_inserted(s->blk)))
1345 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1346 return;
1349 /* Nondata commands permit the byte_count_limit to be 0.
1350 * If this is a data-transferring PIO command and BCL is 0,
1351 * we abort at the /ATA/ level, not the ATAPI level.
1352 * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
1353 if (cmd->handler && !(cmd->flags & NONDATA)) {
1354 /* TODO: Check IDENTIFY data word 125 for default BCL (currently 0) */
1355 if (!(atapi_byte_count_limit(s) || s->atapi_dma)) {
1356 /* TODO: Move abort back into core.c and make static inline again */
1357 ide_abort_command(s);
1358 return;
1362 /* Execute the command */
1363 if (cmd->handler) {
1364 cmd->handler(s, buf);
1365 return;
1368 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);