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
26 #include "qemu/osdep.h"
27 #include "hw/ide/internal.h"
28 #include "hw/scsi/scsi.h"
29 #include "sysemu/block-backend.h"
31 #define ATAPI_SECTOR_BITS (2 + BDRV_SECTOR_BITS)
32 #define ATAPI_SECTOR_SIZE (1 << ATAPI_SECTOR_BITS)
34 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
);
36 static void padstr8(uint8_t *buf
, int buf_size
, const char *src
)
39 for(i
= 0; i
< buf_size
; i
++) {
47 static inline void cpu_to_ube16(uint8_t *buf
, int val
)
53 static inline void cpu_to_ube32(uint8_t *buf
, unsigned int val
)
61 static inline int ube16_to_cpu(const uint8_t *buf
)
63 return (buf
[0] << 8) | buf
[1];
66 static inline int ube32_to_cpu(const uint8_t *buf
)
68 return (buf
[0] << 24) | (buf
[1] << 16) | (buf
[2] << 8) | buf
[3];
71 static void lba_to_msf(uint8_t *buf
, int lba
)
74 buf
[0] = (lba
/ 75) / 60;
75 buf
[1] = (lba
/ 75) % 60;
79 static inline int media_present(IDEState
*s
)
81 return !s
->tray_open
&& s
->nb_sectors
> 0;
84 /* XXX: DVDs that could fit on a CD will be reported as a CD */
85 static inline int media_is_dvd(IDEState
*s
)
87 return (media_present(s
) && s
->nb_sectors
> CD_MAX_SECTORS
);
90 static inline int media_is_cd(IDEState
*s
)
92 return (media_present(s
) && s
->nb_sectors
<= CD_MAX_SECTORS
);
95 static void cd_data_to_raw(uint8_t *buf
, int lba
)
99 memset(buf
+ 1, 0xff, 10);
103 lba_to_msf(buf
, lba
);
104 buf
[3] = 0x01; /* mode 1 data */
108 /* XXX: ECC not computed */
113 cd_read_sector_sync(IDEState
*s
)
116 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
,
117 ATAPI_SECTOR_SIZE
, BLOCK_ACCT_READ
);
119 #ifdef DEBUG_IDE_ATAPI
120 printf("cd_read_sector_sync: lba=%d\n", s
->lba
);
123 switch (s
->cd_sector_size
) {
125 ret
= blk_pread(s
->blk
, (int64_t)s
->lba
<< ATAPI_SECTOR_BITS
,
126 s
->io_buffer
, ATAPI_SECTOR_SIZE
);
129 ret
= blk_pread(s
->blk
, (int64_t)s
->lba
<< ATAPI_SECTOR_BITS
,
130 s
->io_buffer
+ 16, ATAPI_SECTOR_SIZE
);
132 cd_data_to_raw(s
->io_buffer
, s
->lba
);
136 block_acct_invalid(blk_get_stats(s
->blk
), BLOCK_ACCT_READ
);
141 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
143 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
145 s
->io_buffer_index
= 0;
151 static void cd_read_sector_cb(void *opaque
, int ret
)
153 IDEState
*s
= opaque
;
155 #ifdef DEBUG_IDE_ATAPI
156 printf("cd_read_sector_cb: lba=%d ret=%d\n", s
->lba
, ret
);
160 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
161 ide_atapi_io_error(s
, ret
);
165 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
167 if (s
->cd_sector_size
== 2352) {
168 cd_data_to_raw(s
->io_buffer
, s
->lba
);
172 s
->io_buffer_index
= 0;
173 s
->status
&= ~BUSY_STAT
;
175 ide_atapi_cmd_reply_end(s
);
178 static int cd_read_sector(IDEState
*s
)
180 if (s
->cd_sector_size
!= 2048 && s
->cd_sector_size
!= 2352) {
181 block_acct_invalid(blk_get_stats(s
->blk
), BLOCK_ACCT_READ
);
185 s
->iov
.iov_base
= (s
->cd_sector_size
== 2352) ?
186 s
->io_buffer
+ 16 : s
->io_buffer
;
188 s
->iov
.iov_len
= ATAPI_SECTOR_SIZE
;
189 qemu_iovec_init_external(&s
->qiov
, &s
->iov
, 1);
191 #ifdef DEBUG_IDE_ATAPI
192 printf("cd_read_sector: lba=%d\n", s
->lba
);
195 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
,
196 ATAPI_SECTOR_SIZE
, BLOCK_ACCT_READ
);
198 ide_buffered_readv(s
, (int64_t)s
->lba
<< 2, &s
->qiov
, 4,
199 cd_read_sector_cb
, s
);
201 s
->status
|= BUSY_STAT
;
205 void ide_atapi_cmd_ok(IDEState
*s
)
208 s
->status
= READY_STAT
| SEEK_STAT
;
209 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
210 ide_transfer_stop(s
);
214 void ide_atapi_cmd_error(IDEState
*s
, int sense_key
, int asc
)
216 #ifdef DEBUG_IDE_ATAPI
217 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key
, asc
);
219 s
->error
= sense_key
<< 4;
220 s
->status
= READY_STAT
| ERR_STAT
;
221 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
222 s
->sense_key
= sense_key
;
224 ide_transfer_stop(s
);
228 void ide_atapi_io_error(IDEState
*s
, int ret
)
230 /* XXX: handle more errors */
231 if (ret
== -ENOMEDIUM
) {
232 ide_atapi_cmd_error(s
, NOT_READY
,
233 ASC_MEDIUM_NOT_PRESENT
);
235 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
236 ASC_LOGICAL_BLOCK_OOR
);
240 static uint16_t atapi_byte_count_limit(IDEState
*s
)
244 bcl
= s
->lcyl
| (s
->hcyl
<< 8);
251 /* The whole ATAPI transfer logic is handled in this function */
252 void ide_atapi_cmd_reply_end(IDEState
*s
)
254 int byte_count_limit
, size
, ret
;
255 #ifdef DEBUG_IDE_ATAPI
256 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
257 s
->packet_transfer_size
,
258 s
->elementary_transfer_size
,
261 if (s
->packet_transfer_size
<= 0) {
262 /* end of transfer */
265 #ifdef DEBUG_IDE_ATAPI
266 printf("end of transfer, status=0x%x\n", s
->status
);
269 /* see if a new sector must be read */
270 if (s
->lba
!= -1 && s
->io_buffer_index
>= s
->cd_sector_size
) {
271 if (!s
->elementary_transfer_size
) {
272 ret
= cd_read_sector(s
);
274 ide_atapi_io_error(s
, ret
);
278 /* rebuffering within an elementary transfer is
279 * only possible with a sync request because we
280 * end up with a race condition otherwise */
281 ret
= cd_read_sector_sync(s
);
283 ide_atapi_io_error(s
, ret
);
288 if (s
->elementary_transfer_size
> 0) {
289 /* there are some data left to transmit in this elementary
291 size
= s
->cd_sector_size
- s
->io_buffer_index
;
292 if (size
> s
->elementary_transfer_size
)
293 size
= s
->elementary_transfer_size
;
294 s
->packet_transfer_size
-= size
;
295 s
->elementary_transfer_size
-= size
;
296 s
->io_buffer_index
+= size
;
297 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
- size
,
298 size
, ide_atapi_cmd_reply_end
);
300 /* a new transfer is needed */
301 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
;
302 byte_count_limit
= atapi_byte_count_limit(s
);
303 #ifdef DEBUG_IDE_ATAPI
304 printf("byte_count_limit=%d\n", byte_count_limit
);
306 size
= s
->packet_transfer_size
;
307 if (size
> byte_count_limit
) {
308 /* byte count limit must be even if this case */
309 if (byte_count_limit
& 1)
311 size
= byte_count_limit
;
315 s
->elementary_transfer_size
= size
;
316 /* we cannot transmit more than one sector at a time */
318 if (size
> (s
->cd_sector_size
- s
->io_buffer_index
))
319 size
= (s
->cd_sector_size
- s
->io_buffer_index
);
321 s
->packet_transfer_size
-= size
;
322 s
->elementary_transfer_size
-= size
;
323 s
->io_buffer_index
+= size
;
324 ide_transfer_start(s
, s
->io_buffer
+ s
->io_buffer_index
- size
,
325 size
, ide_atapi_cmd_reply_end
);
327 #ifdef DEBUG_IDE_ATAPI
328 printf("status=0x%x\n", s
->status
);
334 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
335 static void ide_atapi_cmd_reply(IDEState
*s
, int size
, int max_size
)
339 s
->lba
= -1; /* no sector read */
340 s
->packet_transfer_size
= size
;
341 s
->io_buffer_size
= size
; /* dma: send the reply data as one chunk */
342 s
->elementary_transfer_size
= 0;
345 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
, size
,
347 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
;
348 ide_start_dma(s
, ide_atapi_cmd_read_dma_cb
);
350 s
->status
= READY_STAT
| SEEK_STAT
;
351 s
->io_buffer_index
= 0;
352 ide_atapi_cmd_reply_end(s
);
356 /* start a CD-CDROM read command */
357 static void ide_atapi_cmd_read_pio(IDEState
*s
, int lba
, int nb_sectors
,
361 s
->packet_transfer_size
= nb_sectors
* sector_size
;
362 s
->elementary_transfer_size
= 0;
363 s
->io_buffer_index
= sector_size
;
364 s
->cd_sector_size
= sector_size
;
366 ide_atapi_cmd_reply_end(s
);
369 static void ide_atapi_cmd_check_status(IDEState
*s
)
371 #ifdef DEBUG_IDE_ATAPI
372 printf("atapi_cmd_check_status\n");
374 s
->error
= MC_ERR
| (UNIT_ATTENTION
<< 4);
375 s
->status
= ERR_STAT
;
379 /* ATAPI DMA support */
381 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
)
383 IDEState
*s
= opaque
;
387 if (ide_handle_rw_error(s
, -ret
, ide_dma_cmd_to_retry(s
->dma_cmd
))) {
388 if (s
->bus
->error_status
) {
389 s
->bus
->dma
->aiocb
= NULL
;
396 if (s
->io_buffer_size
> 0) {
398 * For a cdrom read sector command (s->lba != -1),
399 * adjust the lba for the next s->io_buffer_size chunk
400 * and dma the current chunk.
401 * For a command != read (s->lba == -1), just transfer
405 if (s
->cd_sector_size
== 2352) {
407 cd_data_to_raw(s
->io_buffer
, s
->lba
);
409 n
= s
->io_buffer_size
>> 11;
413 s
->packet_transfer_size
-= s
->io_buffer_size
;
414 if (s
->bus
->dma
->ops
->rw_buf(s
->bus
->dma
, 1) == 0)
418 if (s
->packet_transfer_size
<= 0) {
419 s
->status
= READY_STAT
| SEEK_STAT
;
420 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
425 s
->io_buffer_index
= 0;
426 if (s
->cd_sector_size
== 2352) {
428 s
->io_buffer_size
= s
->cd_sector_size
;
431 n
= s
->packet_transfer_size
>> 11;
432 if (n
> (IDE_DMA_BUF_SECTORS
/ 4))
433 n
= (IDE_DMA_BUF_SECTORS
/ 4);
434 s
->io_buffer_size
= n
* 2048;
438 printf("aio_read_cd: lba=%u n=%d\n", s
->lba
, n
);
441 s
->bus
->dma
->iov
.iov_base
= (void *)(s
->io_buffer
+ data_offset
);
442 s
->bus
->dma
->iov
.iov_len
= n
* ATAPI_SECTOR_SIZE
;
443 qemu_iovec_init_external(&s
->bus
->dma
->qiov
, &s
->bus
->dma
->iov
, 1);
445 s
->bus
->dma
->aiocb
= ide_buffered_readv(s
, (int64_t)s
->lba
<< 2,
446 &s
->bus
->dma
->qiov
, n
* 4,
447 ide_atapi_cmd_read_dma_cb
, s
);
452 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
454 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
456 ide_set_inactive(s
, false);
459 /* start a CD-CDROM read command with DMA */
460 /* XXX: test if DMA is available */
461 static void ide_atapi_cmd_read_dma(IDEState
*s
, int lba
, int nb_sectors
,
465 s
->packet_transfer_size
= nb_sectors
* sector_size
;
466 s
->io_buffer_size
= 0;
467 s
->cd_sector_size
= sector_size
;
469 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
, s
->packet_transfer_size
,
472 /* XXX: check if BUSY_STAT should be set */
473 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
474 ide_start_dma(s
, ide_atapi_cmd_read_dma_cb
);
477 static void ide_atapi_cmd_read(IDEState
*s
, int lba
, int nb_sectors
,
480 #ifdef DEBUG_IDE_ATAPI
481 printf("read %s: LBA=%d nb_sectors=%d\n", s
->atapi_dma
? "dma" : "pio",
485 ide_atapi_cmd_read_dma(s
, lba
, nb_sectors
, sector_size
);
487 ide_atapi_cmd_read_pio(s
, lba
, nb_sectors
, sector_size
);
491 void ide_atapi_dma_restart(IDEState
*s
)
494 * At this point we can just re-evaluate the packet command and start over.
495 * The presence of ->dma_cb callback in the pre_save ensures that the packet
496 * command has been completely sent and we can safely restart command.
498 s
->unit
= s
->bus
->retry_unit
;
499 s
->bus
->dma
->ops
->restart_dma(s
->bus
->dma
);
503 static inline uint8_t ide_atapi_set_profile(uint8_t *buf
, uint8_t *index
,
506 uint8_t *buf_profile
= buf
+ 12; /* start of profiles */
508 buf_profile
+= ((*index
) * 4); /* start of indexed profile */
509 cpu_to_ube16 (buf_profile
, profile
);
510 buf_profile
[2] = ((buf_profile
[0] == buf
[6]) && (buf_profile
[1] == buf
[7]));
512 /* each profile adds 4 bytes to the response */
514 buf
[11] += 4; /* Additional Length */
519 static int ide_dvd_read_structure(IDEState
*s
, int format
,
520 const uint8_t *packet
, uint8_t *buf
)
523 case 0x0: /* Physical format information */
525 int layer
= packet
[6];
526 uint64_t total_sectors
;
529 return -ASC_INV_FIELD_IN_CMD_PACKET
;
531 total_sectors
= s
->nb_sectors
>> 2;
532 if (total_sectors
== 0) {
533 return -ASC_MEDIUM_NOT_PRESENT
;
536 buf
[4] = 1; /* DVD-ROM, part version 1 */
537 buf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
538 buf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
539 buf
[7] = 0; /* default densities */
541 /* FIXME: 0x30000 per spec? */
542 cpu_to_ube32(buf
+ 8, 0); /* start sector */
543 cpu_to_ube32(buf
+ 12, total_sectors
- 1); /* end sector */
544 cpu_to_ube32(buf
+ 16, total_sectors
- 1); /* l0 end sector */
546 /* Size of buffer, not including 2 byte size field */
547 stw_be_p(buf
, 2048 + 2);
549 /* 2k data + 4 byte header */
553 case 0x01: /* DVD copyright information */
554 buf
[4] = 0; /* no copyright data */
555 buf
[5] = 0; /* no region restrictions */
557 /* Size of buffer, not including 2 byte size field */
558 stw_be_p(buf
, 4 + 2);
560 /* 4 byte header + 4 byte data */
563 case 0x03: /* BCA information - invalid field for no BCA info */
564 return -ASC_INV_FIELD_IN_CMD_PACKET
;
566 case 0x04: /* DVD disc manufacturing information */
567 /* Size of buffer, not including 2 byte size field */
568 stw_be_p(buf
, 2048 + 2);
570 /* 2k data + 4 byte header */
575 * This lists all the command capabilities above. Add new ones
576 * in order and update the length and buffer return values.
579 buf
[4] = 0x00; /* Physical format */
580 buf
[5] = 0x40; /* Not writable, is readable */
581 stw_be_p(buf
+ 6, 2048 + 4);
583 buf
[8] = 0x01; /* Copyright info */
584 buf
[9] = 0x40; /* Not writable, is readable */
585 stw_be_p(buf
+ 10, 4 + 4);
587 buf
[12] = 0x03; /* BCA info */
588 buf
[13] = 0x40; /* Not writable, is readable */
589 stw_be_p(buf
+ 14, 188 + 4);
591 buf
[16] = 0x04; /* Manufacturing info */
592 buf
[17] = 0x40; /* Not writable, is readable */
593 stw_be_p(buf
+ 18, 2048 + 4);
595 /* Size of buffer, not including 2 byte size field */
596 stw_be_p(buf
, 16 + 2);
598 /* data written + 4 byte header */
601 default: /* TODO: formats beyond DVD-ROM requires */
602 return -ASC_INV_FIELD_IN_CMD_PACKET
;
606 static unsigned int event_status_media(IDEState
*s
,
609 uint8_t event_code
, media_status
;
613 media_status
= MS_TRAY_OPEN
;
614 } else if (blk_is_inserted(s
->blk
)) {
615 media_status
= MS_MEDIA_PRESENT
;
618 /* Event notification descriptor */
619 event_code
= MEC_NO_CHANGE
;
620 if (media_status
!= MS_TRAY_OPEN
) {
621 if (s
->events
.new_media
) {
622 event_code
= MEC_NEW_MEDIA
;
623 s
->events
.new_media
= false;
624 } else if (s
->events
.eject_request
) {
625 event_code
= MEC_EJECT_REQUESTED
;
626 s
->events
.eject_request
= false;
631 buf
[5] = media_status
;
633 /* These fields are reserved, just clear them. */
637 return 8; /* We wrote to 4 extra bytes from the header */
641 * Before transferring data or otherwise signalling acceptance of a command
642 * marked CONDDATA, we must check the validity of the byte_count_limit.
644 static bool validate_bcl(IDEState
*s
)
646 /* TODO: Check IDENTIFY data word 125 for defacult BCL (currently 0) */
647 if (s
->atapi_dma
|| atapi_byte_count_limit(s
)) {
651 /* TODO: Move abort back into core.c and introduce proper error flow between
652 * ATAPI layer and IDE core layer */
653 ide_abort_command(s
);
657 static void cmd_get_event_status_notification(IDEState
*s
,
660 const uint8_t *packet
= buf
;
664 uint8_t polled
; /* lsb bit is polled; others are reserved */
665 uint8_t reserved2
[2];
667 uint8_t reserved3
[2];
670 } QEMU_PACKED
*gesn_cdb
;
674 uint8_t notification_class
;
675 uint8_t supported_events
;
676 } QEMU_PACKED
*gesn_event_header
;
677 unsigned int max_len
, used_len
;
679 gesn_cdb
= (void *)packet
;
680 gesn_event_header
= (void *)buf
;
682 max_len
= be16_to_cpu(gesn_cdb
->len
);
684 /* It is fine by the MMC spec to not support async mode operations */
685 if (!(gesn_cdb
->polled
& 0x01)) { /* asynchronous mode */
686 /* Only polling is supported, asynchronous mode is not. */
687 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
688 ASC_INV_FIELD_IN_CMD_PACKET
);
692 /* polling mode operation */
695 * These are the supported events.
697 * We currently only support requests of the 'media' type.
698 * Notification class requests and supported event classes are bitmasks,
699 * but they are build from the same values as the "notification class"
702 gesn_event_header
->supported_events
= 1 << GESN_MEDIA
;
705 * We use |= below to set the class field; other bits in this byte
706 * are reserved now but this is useful to do if we have to use the
707 * reserved fields later.
709 gesn_event_header
->notification_class
= 0;
712 * Responses to requests are to be based on request priority. The
713 * notification_class_request_type enum above specifies the
714 * priority: upper elements are higher prio than lower ones.
716 if (gesn_cdb
->class & (1 << GESN_MEDIA
)) {
717 gesn_event_header
->notification_class
|= GESN_MEDIA
;
718 used_len
= event_status_media(s
, buf
);
720 gesn_event_header
->notification_class
= 0x80; /* No event available */
721 used_len
= sizeof(*gesn_event_header
);
723 gesn_event_header
->len
= cpu_to_be16(used_len
724 - sizeof(*gesn_event_header
));
725 ide_atapi_cmd_reply(s
, used_len
, max_len
);
728 static void cmd_request_sense(IDEState
*s
, uint8_t *buf
)
730 int max_len
= buf
[4];
733 buf
[0] = 0x70 | (1 << 7);
734 buf
[2] = s
->sense_key
;
738 if (s
->sense_key
== UNIT_ATTENTION
) {
739 s
->sense_key
= NO_SENSE
;
742 ide_atapi_cmd_reply(s
, 18, max_len
);
745 static void cmd_inquiry(IDEState
*s
, uint8_t *buf
)
747 uint8_t page_code
= buf
[2];
748 int max_len
= buf
[4];
752 unsigned preamble_len
;
754 /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
755 * we are being asked for a specific page of info indicated by byte 2. */
760 buf
[idx
++] = 0x05; /* CD-ROM */
761 buf
[idx
++] = page_code
; /* Page Code */
762 buf
[idx
++] = 0x00; /* reserved */
763 idx
++; /* length (set later) */
767 /* Supported Pages: List of supported VPD responses. */
768 buf
[idx
++] = 0x00; /* 0x00: Supported Pages, and: */
769 buf
[idx
++] = 0x83; /* 0x83: Device Identification. */
773 /* Device Identification. Each entry is optional, but the entries
774 * included here are modeled after libata's VPD responses.
775 * If the response is given, at least one entry must be present. */
777 /* Entry 1: Serial */
778 if (idx
+ 24 > max_len
) {
779 /* Not enough room for even the first entry: */
780 /* 4 byte header + 20 byte string */
781 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
782 ASC_DATA_PHASE_ERROR
);
785 buf
[idx
++] = 0x02; /* Ascii */
786 buf
[idx
++] = 0x00; /* Vendor Specific */
788 buf
[idx
++] = 20; /* Remaining length */
789 padstr8(buf
+ idx
, 20, s
->drive_serial_str
);
792 /* Entry 2: Drive Model and Serial */
793 if (idx
+ 72 > max_len
) {
794 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
797 buf
[idx
++] = 0x02; /* Ascii */
798 buf
[idx
++] = 0x01; /* T10 Vendor */
801 padstr8(buf
+ idx
, 8, "ATA"); /* Generic T10 vendor */
803 padstr8(buf
+ idx
, 40, s
->drive_model_str
);
805 padstr8(buf
+ idx
, 20, s
->drive_serial_str
);
809 if (s
->wwn
&& (idx
+ 12 <= max_len
)) {
810 /* 4 byte header + 8 byte wwn */
811 buf
[idx
++] = 0x01; /* Binary */
812 buf
[idx
++] = 0x03; /* NAA */
815 stq_be_p(&buf
[idx
], s
->wwn
);
821 /* SPC-3, revision 23 sec. 6.4 */
822 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
823 ASC_INV_FIELD_IN_CMD_PACKET
);
830 buf
[0] = 0x05; /* CD-ROM */
831 buf
[1] = 0x80; /* removable */
832 buf
[2] = 0x00; /* ISO */
833 buf
[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
834 /* buf[size_idx] set below. */
835 buf
[5] = 0; /* reserved */
836 buf
[6] = 0; /* reserved */
837 buf
[7] = 0; /* reserved */
838 padstr8(buf
+ 8, 8, "QEMU");
839 padstr8(buf
+ 16, 16, "QEMU DVD-ROM");
840 padstr8(buf
+ 32, 4, s
->version
);
845 buf
[size_idx
] = idx
- preamble_len
;
846 ide_atapi_cmd_reply(s
, idx
, max_len
);
849 static void cmd_get_configuration(IDEState
*s
, uint8_t *buf
)
855 /* only feature 0 is supported */
856 if (buf
[2] != 0 || buf
[3] != 0) {
857 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
858 ASC_INV_FIELD_IN_CMD_PACKET
);
862 /* XXX: could result in alignment problems in some architectures */
863 max_len
= ube16_to_cpu(buf
+ 7);
866 * XXX: avoid overflow for io_buffer if max_len is bigger than
867 * the size of that buffer (dimensioned to max number of
868 * sectors to transfer at once)
870 * Only a problem if the feature/profiles grow.
873 /* XXX: assume 1 sector */
877 memset(buf
, 0, max_len
);
879 * the number of sectors from the media tells us which profile
880 * to use as current. 0 means there is no media
882 if (media_is_dvd(s
)) {
883 cpu_to_ube16(buf
+ 6, MMC_PROFILE_DVD_ROM
);
884 } else if (media_is_cd(s
)) {
885 cpu_to_ube16(buf
+ 6, MMC_PROFILE_CD_ROM
);
888 buf
[10] = 0x02 | 0x01; /* persistent and current */
889 len
= 12; /* headers: 8 + 4 */
890 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_DVD_ROM
);
891 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_CD_ROM
);
892 cpu_to_ube32(buf
, len
- 4); /* data length */
894 ide_atapi_cmd_reply(s
, len
, max_len
);
897 static void cmd_mode_sense(IDEState
*s
, uint8_t *buf
)
902 max_len
= ube16_to_cpu(buf
+ 7);
903 action
= buf
[2] >> 6;
904 code
= buf
[2] & 0x3f;
907 case 0: /* current values */
909 case MODE_PAGE_R_W_ERROR
: /* error recovery */
910 cpu_to_ube16(&buf
[0], 16 - 2);
918 buf
[8] = MODE_PAGE_R_W_ERROR
;
926 ide_atapi_cmd_reply(s
, 16, max_len
);
928 case MODE_PAGE_AUDIO_CTL
:
929 cpu_to_ube16(&buf
[0], 24 - 2);
937 buf
[8] = MODE_PAGE_AUDIO_CTL
;
939 /* Fill with CDROM audio volume */
945 ide_atapi_cmd_reply(s
, 24, max_len
);
947 case MODE_PAGE_CAPABILITIES
:
948 cpu_to_ube16(&buf
[0], 30 - 2);
956 buf
[8] = MODE_PAGE_CAPABILITIES
;
958 buf
[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
961 /* Claim PLAY_AUDIO capability (0x01) since some Linux
962 code checks for this to automount media. */
965 buf
[14] = (1 << 0) | (1 << 3) | (1 << 5);
966 if (s
->tray_locked
) {
969 buf
[15] = 0x00; /* No volume & mute control, no changer */
970 cpu_to_ube16(&buf
[16], 704); /* 4x read speed */
971 buf
[18] = 0; /* Two volume levels */
973 cpu_to_ube16(&buf
[20], 512); /* 512k buffer */
974 cpu_to_ube16(&buf
[22], 704); /* 4x read speed current */
981 ide_atapi_cmd_reply(s
, 30, max_len
);
987 case 1: /* changeable values */
989 case 2: /* default values */
992 case 3: /* saved values */
993 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
994 ASC_SAVING_PARAMETERS_NOT_SUPPORTED
);
1000 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_INV_FIELD_IN_CMD_PACKET
);
1003 static void cmd_test_unit_ready(IDEState
*s
, uint8_t *buf
)
1005 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
1006 * come here, we know that it's ready. */
1007 ide_atapi_cmd_ok(s
);
1010 static void cmd_prevent_allow_medium_removal(IDEState
*s
, uint8_t* buf
)
1012 s
->tray_locked
= buf
[4] & 1;
1013 blk_lock_medium(s
->blk
, buf
[4] & 1);
1014 ide_atapi_cmd_ok(s
);
1017 static void cmd_read(IDEState
*s
, uint8_t* buf
)
1019 int nb_sectors
, lba
;
1021 if (buf
[0] == GPCMD_READ_10
) {
1022 nb_sectors
= ube16_to_cpu(buf
+ 7);
1024 nb_sectors
= ube32_to_cpu(buf
+ 6);
1027 lba
= ube32_to_cpu(buf
+ 2);
1028 if (nb_sectors
== 0) {
1029 ide_atapi_cmd_ok(s
);
1033 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1036 static void cmd_read_cd(IDEState
*s
, uint8_t* buf
)
1038 int nb_sectors
, lba
, transfer_request
;
1040 nb_sectors
= (buf
[6] << 16) | (buf
[7] << 8) | buf
[8];
1041 lba
= ube32_to_cpu(buf
+ 2);
1043 if (nb_sectors
== 0) {
1044 ide_atapi_cmd_ok(s
);
1048 transfer_request
= buf
[9] & 0xf8;
1049 if (transfer_request
== 0x00) {
1051 ide_atapi_cmd_ok(s
);
1055 /* Check validity of BCL before transferring data */
1056 if (!validate_bcl(s
)) {
1060 switch (transfer_request
) {
1063 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1067 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2352);
1070 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1071 ASC_INV_FIELD_IN_CMD_PACKET
);
1076 static void cmd_seek(IDEState
*s
, uint8_t* buf
)
1079 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1081 lba
= ube32_to_cpu(buf
+ 2);
1082 if (lba
>= total_sectors
) {
1083 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_LOGICAL_BLOCK_OOR
);
1087 ide_atapi_cmd_ok(s
);
1090 static void cmd_start_stop_unit(IDEState
*s
, uint8_t* buf
)
1093 bool start
= buf
[4] & 1;
1094 bool loej
= buf
[4] & 2; /* load on start, eject on !start */
1095 int pwrcnd
= buf
[4] & 0xf0;
1098 /* eject/load only happens for power condition == 0 */
1099 ide_atapi_cmd_ok(s
);
1104 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1105 sense
= blk_is_inserted(s
->blk
)
1106 ? NOT_READY
: ILLEGAL_REQUEST
;
1107 ide_atapi_cmd_error(s
, sense
, ASC_MEDIA_REMOVAL_PREVENTED
);
1111 if (s
->tray_open
!= !start
) {
1112 blk_eject(s
->blk
, !start
);
1113 s
->tray_open
= !start
;
1117 ide_atapi_cmd_ok(s
);
1120 static void cmd_mechanism_status(IDEState
*s
, uint8_t* buf
)
1122 int max_len
= ube16_to_cpu(buf
+ 8);
1124 cpu_to_ube16(buf
, 0);
1125 /* no current LBA */
1130 cpu_to_ube16(buf
+ 6, 0);
1131 ide_atapi_cmd_reply(s
, 8, max_len
);
1134 static void cmd_read_toc_pma_atip(IDEState
*s
, uint8_t* buf
)
1136 int format
, msf
, start_track
, len
;
1138 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1140 max_len
= ube16_to_cpu(buf
+ 7);
1141 format
= buf
[9] >> 6;
1142 msf
= (buf
[1] >> 1) & 1;
1143 start_track
= buf
[6];
1147 len
= cdrom_read_toc(total_sectors
, buf
, msf
, start_track
);
1150 ide_atapi_cmd_reply(s
, len
, max_len
);
1153 /* multi session : only a single session defined */
1158 ide_atapi_cmd_reply(s
, 12, max_len
);
1161 len
= cdrom_read_toc_raw(total_sectors
, buf
, msf
, start_track
);
1164 ide_atapi_cmd_reply(s
, len
, max_len
);
1168 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1169 ASC_INV_FIELD_IN_CMD_PACKET
);
1173 static void cmd_read_cdvd_capacity(IDEState
*s
, uint8_t* buf
)
1175 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1177 /* NOTE: it is really the number of sectors minus 1 */
1178 cpu_to_ube32(buf
, total_sectors
- 1);
1179 cpu_to_ube32(buf
+ 4, 2048);
1180 ide_atapi_cmd_reply(s
, 8, 8);
1183 static void cmd_read_disc_information(IDEState
*s
, uint8_t* buf
)
1185 uint8_t type
= buf
[1] & 7;
1186 uint32_t max_len
= ube16_to_cpu(buf
+ 7);
1188 /* Types 1/2 are only defined for Blu-Ray. */
1190 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1191 ASC_INV_FIELD_IN_CMD_PACKET
);
1197 buf
[2] = 0xe; /* last session complete, disc finalized */
1198 buf
[3] = 1; /* first track on disc */
1199 buf
[4] = 1; /* # of sessions */
1200 buf
[5] = 1; /* first track of last session */
1201 buf
[6] = 1; /* last track of last session */
1202 buf
[7] = 0x20; /* unrestricted use */
1203 buf
[8] = 0x00; /* CD-ROM or DVD-ROM */
1204 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
1205 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
1206 /* 24-31: disc bar code */
1207 /* 32: disc application code */
1208 /* 33: number of OPC tables */
1210 ide_atapi_cmd_reply(s
, 34, max_len
);
1213 static void cmd_read_dvd_structure(IDEState
*s
, uint8_t* buf
)
1217 int format
= buf
[7];
1220 max_len
= ube16_to_cpu(buf
+ 8);
1222 if (format
< 0xff) {
1223 if (media_is_cd(s
)) {
1224 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1225 ASC_INCOMPATIBLE_FORMAT
);
1227 } else if (!media_present(s
)) {
1228 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1229 ASC_INV_FIELD_IN_CMD_PACKET
);
1234 memset(buf
, 0, max_len
> IDE_DMA_BUF_SECTORS
* 512 + 4 ?
1235 IDE_DMA_BUF_SECTORS
* 512 + 4 : max_len
);
1241 ret
= ide_dvd_read_structure(s
, format
, buf
, buf
);
1244 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, -ret
);
1246 ide_atapi_cmd_reply(s
, ret
, max_len
);
1251 /* TODO: BD support, fall through for now */
1253 /* Generic disk structures */
1254 case 0x80: /* TODO: AACS volume identifier */
1255 case 0x81: /* TODO: AACS media serial number */
1256 case 0x82: /* TODO: AACS media identifier */
1257 case 0x83: /* TODO: AACS media key block */
1258 case 0x90: /* TODO: List of recognized format layers */
1259 case 0xc0: /* TODO: Write protection status */
1261 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1262 ASC_INV_FIELD_IN_CMD_PACKET
);
1267 static void cmd_set_speed(IDEState
*s
, uint8_t* buf
)
1269 ide_atapi_cmd_ok(s
);
1274 * Only commands flagged as ALLOW_UA are allowed to run under a
1275 * unit attention condition. (See MMC-5, section 4.1.6.1)
1280 * Commands flagged with CHECK_READY can only execute if a medium is present.
1281 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1287 * Commands flagged with NONDATA do not in any circumstances return
1288 * any data via ide_atapi_cmd_reply. These commands are exempt from
1289 * the normal byte_count_limit constraints.
1290 * See ATA8-ACS3 "7.21.5 Byte Count Limit"
1295 * CONDDATA implies a command that transfers data only conditionally based
1296 * on the presence of suboptions. It should be exempt from the BCL check at
1297 * command validation time, but it needs to be checked at the command
1298 * handler level instead.
1303 static const struct AtapiCmd
{
1304 void (*handler
)(IDEState
*s
, uint8_t *buf
);
1306 } atapi_cmd_table
[0x100] = {
1307 [ 0x00 ] = { cmd_test_unit_ready
, CHECK_READY
| NONDATA
},
1308 [ 0x03 ] = { cmd_request_sense
, ALLOW_UA
},
1309 [ 0x12 ] = { cmd_inquiry
, ALLOW_UA
},
1310 [ 0x1b ] = { cmd_start_stop_unit
, NONDATA
}, /* [1] */
1311 [ 0x1e ] = { cmd_prevent_allow_medium_removal
, NONDATA
},
1312 [ 0x25 ] = { cmd_read_cdvd_capacity
, CHECK_READY
},
1313 [ 0x28 ] = { cmd_read
, /* (10) */ CHECK_READY
},
1314 [ 0x2b ] = { cmd_seek
, CHECK_READY
| NONDATA
},
1315 [ 0x43 ] = { cmd_read_toc_pma_atip
, CHECK_READY
},
1316 [ 0x46 ] = { cmd_get_configuration
, ALLOW_UA
},
1317 [ 0x4a ] = { cmd_get_event_status_notification
, ALLOW_UA
},
1318 [ 0x51 ] = { cmd_read_disc_information
, CHECK_READY
},
1319 [ 0x5a ] = { cmd_mode_sense
, /* (10) */ 0 },
1320 [ 0xa8 ] = { cmd_read
, /* (12) */ CHECK_READY
},
1321 [ 0xad ] = { cmd_read_dvd_structure
, CHECK_READY
},
1322 [ 0xbb ] = { cmd_set_speed
, NONDATA
},
1323 [ 0xbd ] = { cmd_mechanism_status
, 0 },
1324 [ 0xbe ] = { cmd_read_cd
, CHECK_READY
| CONDDATA
},
1325 /* [1] handler detects and reports not ready condition itself */
1328 void ide_atapi_cmd(IDEState
*s
)
1330 uint8_t *buf
= s
->io_buffer
;
1331 const struct AtapiCmd
*cmd
= &atapi_cmd_table
[s
->io_buffer
[0]];
1333 #ifdef DEBUG_IDE_ATAPI
1336 printf("ATAPI limit=0x%x packet:", s
->lcyl
| (s
->hcyl
<< 8));
1337 for(i
= 0; i
< ATAPI_PACKET_SIZE
; i
++) {
1338 printf(" %02x", buf
[i
]);
1345 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1346 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1347 * condition response unless a higher priority status, defined by the drive
1350 if (s
->sense_key
== UNIT_ATTENTION
&& !(cmd
->flags
& ALLOW_UA
)) {
1351 ide_atapi_cmd_check_status(s
);
1355 * When a CD gets changed, we have to report an ejected state and
1356 * then a loaded state to guests so that they detect tray
1357 * open/close and media change events. Guests that do not use
1358 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1359 * states rely on this behavior.
1361 if (!(cmd
->flags
& ALLOW_UA
) &&
1362 !s
->tray_open
&& blk_is_inserted(s
->blk
) && s
->cdrom_changed
) {
1364 if (s
->cdrom_changed
== 1) {
1365 ide_atapi_cmd_error(s
, NOT_READY
, ASC_MEDIUM_NOT_PRESENT
);
1366 s
->cdrom_changed
= 2;
1368 ide_atapi_cmd_error(s
, UNIT_ATTENTION
, ASC_MEDIUM_MAY_HAVE_CHANGED
);
1369 s
->cdrom_changed
= 0;
1375 /* Report a Not Ready condition if appropriate for the command */
1376 if ((cmd
->flags
& CHECK_READY
) &&
1377 (!media_present(s
) || !blk_is_inserted(s
->blk
)))
1379 ide_atapi_cmd_error(s
, NOT_READY
, ASC_MEDIUM_NOT_PRESENT
);
1383 /* Commands that don't transfer DATA permit the byte_count_limit to be 0.
1384 * If this is a data-transferring PIO command and BCL is 0,
1385 * we abort at the /ATA/ level, not the ATAPI level.
1386 * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
1387 if (cmd
->handler
&& !(cmd
->flags
& (NONDATA
| CONDDATA
))) {
1388 if (!validate_bcl(s
)) {
1393 /* Execute the command */
1395 cmd
->handler(s
, buf
);
1399 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_ILLEGAL_OPCODE
);