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"
32 #define ATAPI_SECTOR_BITS (2 + BDRV_SECTOR_BITS)
33 #define ATAPI_SECTOR_SIZE (1 << ATAPI_SECTOR_BITS)
35 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
);
37 static void padstr8(uint8_t *buf
, int buf_size
, const char *src
)
40 for(i
= 0; i
< buf_size
; i
++) {
48 static void lba_to_msf(uint8_t *buf
, int lba
)
51 buf
[0] = (lba
/ 75) / 60;
52 buf
[1] = (lba
/ 75) % 60;
56 static inline int media_present(IDEState
*s
)
58 return !s
->tray_open
&& s
->nb_sectors
> 0;
61 /* XXX: DVDs that could fit on a CD will be reported as a CD */
62 static inline int media_is_dvd(IDEState
*s
)
64 return (media_present(s
) && s
->nb_sectors
> CD_MAX_SECTORS
);
67 static inline int media_is_cd(IDEState
*s
)
69 return (media_present(s
) && s
->nb_sectors
<= CD_MAX_SECTORS
);
72 static void cd_data_to_raw(uint8_t *buf
, int lba
)
76 memset(buf
+ 1, 0xff, 10);
81 buf
[3] = 0x01; /* mode 1 data */
85 /* XXX: ECC not computed */
90 cd_read_sector_sync(IDEState
*s
)
93 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
,
94 ATAPI_SECTOR_SIZE
, BLOCK_ACCT_READ
);
96 trace_cd_read_sector_sync(s
->lba
);
98 switch (s
->cd_sector_size
) {
100 ret
= blk_pread(s
->blk
, (int64_t)s
->lba
<< ATAPI_SECTOR_BITS
,
101 s
->io_buffer
, ATAPI_SECTOR_SIZE
);
104 ret
= blk_pread(s
->blk
, (int64_t)s
->lba
<< ATAPI_SECTOR_BITS
,
105 s
->io_buffer
+ 16, ATAPI_SECTOR_SIZE
);
107 cd_data_to_raw(s
->io_buffer
, s
->lba
);
111 block_acct_invalid(blk_get_stats(s
->blk
), BLOCK_ACCT_READ
);
116 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
118 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
120 s
->io_buffer_index
= 0;
126 static void cd_read_sector_cb(void *opaque
, int ret
)
128 IDEState
*s
= opaque
;
130 trace_cd_read_sector_cb(s
->lba
, ret
);
133 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
134 ide_atapi_io_error(s
, ret
);
138 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
140 if (s
->cd_sector_size
== 2352) {
141 cd_data_to_raw(s
->io_buffer
, s
->lba
);
145 s
->io_buffer_index
= 0;
146 s
->status
&= ~BUSY_STAT
;
148 ide_atapi_cmd_reply_end(s
);
151 static int cd_read_sector(IDEState
*s
)
155 if (s
->cd_sector_size
!= 2048 && s
->cd_sector_size
!= 2352) {
156 block_acct_invalid(blk_get_stats(s
->blk
), BLOCK_ACCT_READ
);
160 buf
= (s
->cd_sector_size
== 2352) ? s
->io_buffer
+ 16 : s
->io_buffer
;
161 qemu_iovec_init_buf(&s
->qiov
, buf
, ATAPI_SECTOR_SIZE
);
163 trace_cd_read_sector(s
->lba
);
165 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
,
166 ATAPI_SECTOR_SIZE
, BLOCK_ACCT_READ
);
168 ide_buffered_readv(s
, (int64_t)s
->lba
<< 2, &s
->qiov
, 4,
169 cd_read_sector_cb
, s
);
171 s
->status
|= BUSY_STAT
;
175 void ide_atapi_cmd_ok(IDEState
*s
)
178 s
->status
= READY_STAT
| SEEK_STAT
;
179 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
180 ide_transfer_stop(s
);
184 void ide_atapi_cmd_error(IDEState
*s
, int sense_key
, int asc
)
186 trace_ide_atapi_cmd_error(s
, sense_key
, asc
);
187 s
->error
= sense_key
<< 4;
188 s
->status
= READY_STAT
| ERR_STAT
;
189 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
190 s
->sense_key
= sense_key
;
192 ide_transfer_stop(s
);
196 void ide_atapi_io_error(IDEState
*s
, int ret
)
198 /* XXX: handle more errors */
199 if (ret
== -ENOMEDIUM
) {
200 ide_atapi_cmd_error(s
, NOT_READY
,
201 ASC_MEDIUM_NOT_PRESENT
);
203 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
204 ASC_LOGICAL_BLOCK_OOR
);
208 static uint16_t atapi_byte_count_limit(IDEState
*s
)
212 bcl
= s
->lcyl
| (s
->hcyl
<< 8);
219 /* The whole ATAPI transfer logic is handled in this function */
220 void ide_atapi_cmd_reply_end(IDEState
*s
)
222 int byte_count_limit
, size
, ret
;
223 while (s
->packet_transfer_size
> 0) {
224 trace_ide_atapi_cmd_reply_end(s
, s
->packet_transfer_size
,
225 s
->elementary_transfer_size
,
228 /* see if a new sector must be read */
229 if (s
->lba
!= -1 && s
->io_buffer_index
>= s
->cd_sector_size
) {
230 if (!s
->elementary_transfer_size
) {
231 ret
= cd_read_sector(s
);
233 ide_atapi_io_error(s
, ret
);
237 /* rebuffering within an elementary transfer is
238 * only possible with a sync request because we
239 * end up with a race condition otherwise */
240 ret
= cd_read_sector_sync(s
);
242 ide_atapi_io_error(s
, ret
);
247 if (s
->elementary_transfer_size
> 0) {
248 /* there are some data left to transmit in this elementary
250 size
= s
->cd_sector_size
- s
->io_buffer_index
;
251 if (size
> s
->elementary_transfer_size
)
252 size
= s
->elementary_transfer_size
;
254 /* a new transfer is needed */
255 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
;
257 byte_count_limit
= atapi_byte_count_limit(s
);
258 trace_ide_atapi_cmd_reply_end_bcl(s
, byte_count_limit
);
259 size
= s
->packet_transfer_size
;
260 if (size
> byte_count_limit
) {
261 /* byte count limit must be even if this case */
262 if (byte_count_limit
& 1)
264 size
= byte_count_limit
;
268 s
->elementary_transfer_size
= size
;
269 /* we cannot transmit more than one sector at a time */
271 if (size
> (s
->cd_sector_size
- s
->io_buffer_index
))
272 size
= (s
->cd_sector_size
- s
->io_buffer_index
);
274 trace_ide_atapi_cmd_reply_end_new(s
, s
->status
);
276 s
->packet_transfer_size
-= size
;
277 s
->elementary_transfer_size
-= size
;
278 s
->io_buffer_index
+= size
;
279 assert(size
<= s
->io_buffer_total_len
);
280 assert(s
->io_buffer_index
<= s
->io_buffer_total_len
);
282 /* Some adapters process PIO data right away. In that case, we need
283 * to avoid mutual recursion between ide_transfer_start
284 * and ide_atapi_cmd_reply_end.
286 if (!ide_transfer_start_norecurse(s
,
287 s
->io_buffer
+ s
->io_buffer_index
- size
,
288 size
, ide_atapi_cmd_reply_end
)) {
293 /* end of transfer */
294 trace_ide_atapi_cmd_reply_end_eot(s
, s
->status
);
299 /* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
300 static void ide_atapi_cmd_reply(IDEState
*s
, int size
, int max_size
)
304 s
->lba
= -1; /* no sector read */
305 s
->packet_transfer_size
= size
;
306 s
->io_buffer_size
= size
; /* dma: send the reply data as one chunk */
307 s
->elementary_transfer_size
= 0;
310 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
, size
,
312 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
;
313 ide_start_dma(s
, ide_atapi_cmd_read_dma_cb
);
315 s
->status
= READY_STAT
| SEEK_STAT
;
316 s
->io_buffer_index
= 0;
317 ide_atapi_cmd_reply_end(s
);
321 /* start a CD-CDROM read command */
322 static void ide_atapi_cmd_read_pio(IDEState
*s
, int lba
, int nb_sectors
,
326 s
->packet_transfer_size
= nb_sectors
* sector_size
;
327 s
->elementary_transfer_size
= 0;
328 s
->io_buffer_index
= sector_size
;
329 s
->cd_sector_size
= sector_size
;
331 ide_atapi_cmd_reply_end(s
);
334 static void ide_atapi_cmd_check_status(IDEState
*s
)
336 trace_ide_atapi_cmd_check_status(s
);
337 s
->error
= MC_ERR
| (UNIT_ATTENTION
<< 4);
338 s
->status
= ERR_STAT
;
342 /* ATAPI DMA support */
344 static void ide_atapi_cmd_read_dma_cb(void *opaque
, int ret
)
346 IDEState
*s
= opaque
;
350 if (ide_handle_rw_error(s
, -ret
, ide_dma_cmd_to_retry(s
->dma_cmd
))) {
351 if (s
->bus
->error_status
) {
352 s
->bus
->dma
->aiocb
= NULL
;
359 if (s
->io_buffer_size
> 0) {
361 * For a cdrom read sector command (s->lba != -1),
362 * adjust the lba for the next s->io_buffer_size chunk
363 * and dma the current chunk.
364 * For a command != read (s->lba == -1), just transfer
368 if (s
->cd_sector_size
== 2352) {
370 cd_data_to_raw(s
->io_buffer
, s
->lba
);
372 n
= s
->io_buffer_size
>> 11;
376 s
->packet_transfer_size
-= s
->io_buffer_size
;
377 if (s
->bus
->dma
->ops
->rw_buf(s
->bus
->dma
, 1) == 0)
381 if (s
->packet_transfer_size
<= 0) {
382 s
->status
= READY_STAT
| SEEK_STAT
;
383 s
->nsector
= (s
->nsector
& ~7) | ATAPI_INT_REASON_IO
| ATAPI_INT_REASON_CD
;
388 s
->io_buffer_index
= 0;
389 if (s
->cd_sector_size
== 2352) {
391 s
->io_buffer_size
= s
->cd_sector_size
;
394 n
= s
->packet_transfer_size
>> 11;
395 if (n
> (IDE_DMA_BUF_SECTORS
/ 4))
396 n
= (IDE_DMA_BUF_SECTORS
/ 4);
397 s
->io_buffer_size
= n
* 2048;
400 trace_ide_atapi_cmd_read_dma_cb_aio(s
, s
->lba
, n
);
401 qemu_iovec_init_buf(&s
->bus
->dma
->qiov
, s
->io_buffer
+ data_offset
,
402 n
* ATAPI_SECTOR_SIZE
);
404 s
->bus
->dma
->aiocb
= ide_buffered_readv(s
, (int64_t)s
->lba
<< 2,
405 &s
->bus
->dma
->qiov
, n
* 4,
406 ide_atapi_cmd_read_dma_cb
, s
);
411 block_acct_failed(blk_get_stats(s
->blk
), &s
->acct
);
413 block_acct_done(blk_get_stats(s
->blk
), &s
->acct
);
415 ide_set_inactive(s
, false);
418 /* start a CD-CDROM read command with DMA */
419 /* XXX: test if DMA is available */
420 static void ide_atapi_cmd_read_dma(IDEState
*s
, int lba
, int nb_sectors
,
424 s
->packet_transfer_size
= nb_sectors
* sector_size
;
425 s
->io_buffer_size
= 0;
426 s
->cd_sector_size
= sector_size
;
428 block_acct_start(blk_get_stats(s
->blk
), &s
->acct
, s
->packet_transfer_size
,
431 /* XXX: check if BUSY_STAT should be set */
432 s
->status
= READY_STAT
| SEEK_STAT
| DRQ_STAT
| BUSY_STAT
;
433 ide_start_dma(s
, ide_atapi_cmd_read_dma_cb
);
436 static void ide_atapi_cmd_read(IDEState
*s
, int lba
, int nb_sectors
,
439 trace_ide_atapi_cmd_read(s
, s
->atapi_dma
? "dma" : "pio",
442 ide_atapi_cmd_read_dma(s
, lba
, nb_sectors
, sector_size
);
444 ide_atapi_cmd_read_pio(s
, lba
, nb_sectors
, sector_size
);
448 void ide_atapi_dma_restart(IDEState
*s
)
451 * At this point we can just re-evaluate the packet command and start over.
452 * The presence of ->dma_cb callback in the pre_save ensures that the packet
453 * command has been completely sent and we can safely restart command.
455 s
->unit
= s
->bus
->retry_unit
;
456 s
->bus
->dma
->ops
->restart_dma(s
->bus
->dma
);
460 static inline uint8_t ide_atapi_set_profile(uint8_t *buf
, uint8_t *index
,
463 uint8_t *buf_profile
= buf
+ 12; /* start of profiles */
465 buf_profile
+= ((*index
) * 4); /* start of indexed profile */
466 stw_be_p(buf_profile
, profile
);
467 buf_profile
[2] = ((buf_profile
[0] == buf
[6]) && (buf_profile
[1] == buf
[7]));
469 /* each profile adds 4 bytes to the response */
471 buf
[11] += 4; /* Additional Length */
476 static int ide_dvd_read_structure(IDEState
*s
, int format
,
477 const uint8_t *packet
, uint8_t *buf
)
480 case 0x0: /* Physical format information */
482 int layer
= packet
[6];
483 uint64_t total_sectors
;
486 return -ASC_INV_FIELD_IN_CMD_PACKET
;
488 total_sectors
= s
->nb_sectors
>> 2;
489 if (total_sectors
== 0) {
490 return -ASC_MEDIUM_NOT_PRESENT
;
493 buf
[4] = 1; /* DVD-ROM, part version 1 */
494 buf
[5] = 0xf; /* 120mm disc, minimum rate unspecified */
495 buf
[6] = 1; /* one layer, read-only (per MMC-2 spec) */
496 buf
[7] = 0; /* default densities */
498 /* FIXME: 0x30000 per spec? */
499 stl_be_p(buf
+ 8, 0); /* start sector */
500 stl_be_p(buf
+ 12, total_sectors
- 1); /* end sector */
501 stl_be_p(buf
+ 16, total_sectors
- 1); /* l0 end sector */
503 /* Size of buffer, not including 2 byte size field */
504 stw_be_p(buf
, 2048 + 2);
506 /* 2k data + 4 byte header */
510 case 0x01: /* DVD copyright information */
511 buf
[4] = 0; /* no copyright data */
512 buf
[5] = 0; /* no region restrictions */
514 /* Size of buffer, not including 2 byte size field */
515 stw_be_p(buf
, 4 + 2);
517 /* 4 byte header + 4 byte data */
520 case 0x03: /* BCA information - invalid field for no BCA info */
521 return -ASC_INV_FIELD_IN_CMD_PACKET
;
523 case 0x04: /* DVD disc manufacturing information */
524 /* Size of buffer, not including 2 byte size field */
525 stw_be_p(buf
, 2048 + 2);
527 /* 2k data + 4 byte header */
532 * This lists all the command capabilities above. Add new ones
533 * in order and update the length and buffer return values.
536 buf
[4] = 0x00; /* Physical format */
537 buf
[5] = 0x40; /* Not writable, is readable */
538 stw_be_p(buf
+ 6, 2048 + 4);
540 buf
[8] = 0x01; /* Copyright info */
541 buf
[9] = 0x40; /* Not writable, is readable */
542 stw_be_p(buf
+ 10, 4 + 4);
544 buf
[12] = 0x03; /* BCA info */
545 buf
[13] = 0x40; /* Not writable, is readable */
546 stw_be_p(buf
+ 14, 188 + 4);
548 buf
[16] = 0x04; /* Manufacturing info */
549 buf
[17] = 0x40; /* Not writable, is readable */
550 stw_be_p(buf
+ 18, 2048 + 4);
552 /* Size of buffer, not including 2 byte size field */
553 stw_be_p(buf
, 16 + 2);
555 /* data written + 4 byte header */
558 default: /* TODO: formats beyond DVD-ROM requires */
559 return -ASC_INV_FIELD_IN_CMD_PACKET
;
563 static unsigned int event_status_media(IDEState
*s
,
566 uint8_t event_code
, media_status
;
570 media_status
= MS_TRAY_OPEN
;
571 } else if (blk_is_inserted(s
->blk
)) {
572 media_status
= MS_MEDIA_PRESENT
;
575 /* Event notification descriptor */
576 event_code
= MEC_NO_CHANGE
;
577 if (media_status
!= MS_TRAY_OPEN
) {
578 if (s
->events
.new_media
) {
579 event_code
= MEC_NEW_MEDIA
;
580 s
->events
.new_media
= false;
581 } else if (s
->events
.eject_request
) {
582 event_code
= MEC_EJECT_REQUESTED
;
583 s
->events
.eject_request
= false;
588 buf
[5] = media_status
;
590 /* These fields are reserved, just clear them. */
594 return 8; /* We wrote to 4 extra bytes from the header */
598 * Before transferring data or otherwise signalling acceptance of a command
599 * marked CONDDATA, we must check the validity of the byte_count_limit.
601 static bool validate_bcl(IDEState
*s
)
603 /* TODO: Check IDENTIFY data word 125 for defacult BCL (currently 0) */
604 if (s
->atapi_dma
|| atapi_byte_count_limit(s
)) {
608 /* TODO: Move abort back into core.c and introduce proper error flow between
609 * ATAPI layer and IDE core layer */
610 ide_abort_command(s
);
614 static void cmd_get_event_status_notification(IDEState
*s
,
617 const uint8_t *packet
= buf
;
621 uint8_t polled
; /* lsb bit is polled; others are reserved */
622 uint8_t reserved2
[2];
624 uint8_t reserved3
[2];
627 } QEMU_PACKED
*gesn_cdb
;
631 uint8_t notification_class
;
632 uint8_t supported_events
;
633 } QEMU_PACKED
*gesn_event_header
;
634 unsigned int max_len
, used_len
;
636 gesn_cdb
= (void *)packet
;
637 gesn_event_header
= (void *)buf
;
639 max_len
= be16_to_cpu(gesn_cdb
->len
);
641 /* It is fine by the MMC spec to not support async mode operations */
642 if (!(gesn_cdb
->polled
& 0x01)) { /* asynchronous mode */
643 /* Only polling is supported, asynchronous mode is not. */
644 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
645 ASC_INV_FIELD_IN_CMD_PACKET
);
649 /* polling mode operation */
652 * These are the supported events.
654 * We currently only support requests of the 'media' type.
655 * Notification class requests and supported event classes are bitmasks,
656 * but they are build from the same values as the "notification class"
659 gesn_event_header
->supported_events
= 1 << GESN_MEDIA
;
662 * We use |= below to set the class field; other bits in this byte
663 * are reserved now but this is useful to do if we have to use the
664 * reserved fields later.
666 gesn_event_header
->notification_class
= 0;
669 * Responses to requests are to be based on request priority. The
670 * notification_class_request_type enum above specifies the
671 * priority: upper elements are higher prio than lower ones.
673 if (gesn_cdb
->class & (1 << GESN_MEDIA
)) {
674 gesn_event_header
->notification_class
|= GESN_MEDIA
;
675 used_len
= event_status_media(s
, buf
);
677 gesn_event_header
->notification_class
= 0x80; /* No event available */
678 used_len
= sizeof(*gesn_event_header
);
680 gesn_event_header
->len
= cpu_to_be16(used_len
681 - sizeof(*gesn_event_header
));
682 ide_atapi_cmd_reply(s
, used_len
, max_len
);
685 static void cmd_request_sense(IDEState
*s
, uint8_t *buf
)
687 int max_len
= buf
[4];
690 buf
[0] = 0x70 | (1 << 7);
691 buf
[2] = s
->sense_key
;
695 if (s
->sense_key
== UNIT_ATTENTION
) {
696 s
->sense_key
= NO_SENSE
;
699 ide_atapi_cmd_reply(s
, 18, max_len
);
702 static void cmd_inquiry(IDEState
*s
, uint8_t *buf
)
704 uint8_t page_code
= buf
[2];
705 int max_len
= buf
[4];
709 unsigned preamble_len
;
711 /* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
712 * we are being asked for a specific page of info indicated by byte 2. */
717 buf
[idx
++] = 0x05; /* CD-ROM */
718 buf
[idx
++] = page_code
; /* Page Code */
719 buf
[idx
++] = 0x00; /* reserved */
720 idx
++; /* length (set later) */
724 /* Supported Pages: List of supported VPD responses. */
725 buf
[idx
++] = 0x00; /* 0x00: Supported Pages, and: */
726 buf
[idx
++] = 0x83; /* 0x83: Device Identification. */
730 /* Device Identification. Each entry is optional, but the entries
731 * included here are modeled after libata's VPD responses.
732 * If the response is given, at least one entry must be present. */
734 /* Entry 1: Serial */
735 if (idx
+ 24 > max_len
) {
736 /* Not enough room for even the first entry: */
737 /* 4 byte header + 20 byte string */
738 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
739 ASC_DATA_PHASE_ERROR
);
742 buf
[idx
++] = 0x02; /* Ascii */
743 buf
[idx
++] = 0x00; /* Vendor Specific */
745 buf
[idx
++] = 20; /* Remaining length */
746 padstr8(buf
+ idx
, 20, s
->drive_serial_str
);
749 /* Entry 2: Drive Model and Serial */
750 if (idx
+ 72 > max_len
) {
751 /* 4 (header) + 8 (vendor) + 60 (model & serial) */
754 buf
[idx
++] = 0x02; /* Ascii */
755 buf
[idx
++] = 0x01; /* T10 Vendor */
758 padstr8(buf
+ idx
, 8, "ATA"); /* Generic T10 vendor */
760 padstr8(buf
+ idx
, 40, s
->drive_model_str
);
762 padstr8(buf
+ idx
, 20, s
->drive_serial_str
);
766 if (s
->wwn
&& (idx
+ 12 <= max_len
)) {
767 /* 4 byte header + 8 byte wwn */
768 buf
[idx
++] = 0x01; /* Binary */
769 buf
[idx
++] = 0x03; /* NAA */
772 stq_be_p(&buf
[idx
], s
->wwn
);
778 /* SPC-3, revision 23 sec. 6.4 */
779 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
780 ASC_INV_FIELD_IN_CMD_PACKET
);
787 buf
[0] = 0x05; /* CD-ROM */
788 buf
[1] = 0x80; /* removable */
789 buf
[2] = 0x00; /* ISO */
790 buf
[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
791 /* buf[size_idx] set below. */
792 buf
[5] = 0; /* reserved */
793 buf
[6] = 0; /* reserved */
794 buf
[7] = 0; /* reserved */
795 padstr8(buf
+ 8, 8, "QEMU");
796 padstr8(buf
+ 16, 16, "QEMU DVD-ROM");
797 padstr8(buf
+ 32, 4, s
->version
);
802 buf
[size_idx
] = idx
- preamble_len
;
803 ide_atapi_cmd_reply(s
, idx
, max_len
);
806 static void cmd_get_configuration(IDEState
*s
, uint8_t *buf
)
812 /* only feature 0 is supported */
813 if (buf
[2] != 0 || buf
[3] != 0) {
814 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
815 ASC_INV_FIELD_IN_CMD_PACKET
);
819 /* XXX: could result in alignment problems in some architectures */
820 max_len
= lduw_be_p(buf
+ 7);
823 * XXX: avoid overflow for io_buffer if max_len is bigger than
824 * the size of that buffer (dimensioned to max number of
825 * sectors to transfer at once)
827 * Only a problem if the feature/profiles grow.
829 if (max_len
> BDRV_SECTOR_SIZE
) {
830 /* XXX: assume 1 sector */
831 max_len
= BDRV_SECTOR_SIZE
;
834 memset(buf
, 0, max_len
);
836 * the number of sectors from the media tells us which profile
837 * to use as current. 0 means there is no media
839 if (media_is_dvd(s
)) {
840 stw_be_p(buf
+ 6, MMC_PROFILE_DVD_ROM
);
841 } else if (media_is_cd(s
)) {
842 stw_be_p(buf
+ 6, MMC_PROFILE_CD_ROM
);
845 buf
[10] = 0x02 | 0x01; /* persistent and current */
846 len
= 12; /* headers: 8 + 4 */
847 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_DVD_ROM
);
848 len
+= ide_atapi_set_profile(buf
, &index
, MMC_PROFILE_CD_ROM
);
849 stl_be_p(buf
, len
- 4); /* data length */
851 ide_atapi_cmd_reply(s
, len
, max_len
);
854 static void cmd_mode_sense(IDEState
*s
, uint8_t *buf
)
859 max_len
= lduw_be_p(buf
+ 7);
860 action
= buf
[2] >> 6;
861 code
= buf
[2] & 0x3f;
864 case 0: /* current values */
866 case MODE_PAGE_R_W_ERROR
: /* error recovery */
867 stw_be_p(&buf
[0], 16 - 2);
875 buf
[8] = MODE_PAGE_R_W_ERROR
;
883 ide_atapi_cmd_reply(s
, 16, max_len
);
885 case MODE_PAGE_AUDIO_CTL
:
886 stw_be_p(&buf
[0], 24 - 2);
894 buf
[8] = MODE_PAGE_AUDIO_CTL
;
896 /* Fill with CDROM audio volume */
902 ide_atapi_cmd_reply(s
, 24, max_len
);
904 case MODE_PAGE_CAPABILITIES
:
905 stw_be_p(&buf
[0], 30 - 2);
913 buf
[8] = MODE_PAGE_CAPABILITIES
;
915 buf
[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
918 /* Claim PLAY_AUDIO capability (0x01) since some Linux
919 code checks for this to automount media. */
922 buf
[14] = (1 << 0) | (1 << 3) | (1 << 5);
923 if (s
->tray_locked
) {
926 buf
[15] = 0x00; /* No volume & mute control, no changer */
927 stw_be_p(&buf
[16], 704); /* 4x read speed */
928 buf
[18] = 0; /* Two volume levels */
930 stw_be_p(&buf
[20], 512); /* 512k buffer */
931 stw_be_p(&buf
[22], 704); /* 4x read speed current */
938 ide_atapi_cmd_reply(s
, 30, max_len
);
944 case 1: /* changeable values */
946 case 2: /* default values */
949 case 3: /* saved values */
950 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
951 ASC_SAVING_PARAMETERS_NOT_SUPPORTED
);
957 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_INV_FIELD_IN_CMD_PACKET
);
960 static void cmd_test_unit_ready(IDEState
*s
, uint8_t *buf
)
962 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
963 * come here, we know that it's ready. */
967 static void cmd_prevent_allow_medium_removal(IDEState
*s
, uint8_t* buf
)
969 s
->tray_locked
= buf
[4] & 1;
970 blk_lock_medium(s
->blk
, buf
[4] & 1);
974 static void cmd_read(IDEState
*s
, uint8_t* buf
)
978 if (buf
[0] == GPCMD_READ_10
) {
979 nb_sectors
= lduw_be_p(buf
+ 7);
981 nb_sectors
= ldl_be_p(buf
+ 6);
984 lba
= ldl_be_p(buf
+ 2);
985 if (nb_sectors
== 0) {
990 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
993 static void cmd_read_cd(IDEState
*s
, uint8_t* buf
)
995 int nb_sectors
, lba
, transfer_request
;
997 nb_sectors
= (buf
[6] << 16) | (buf
[7] << 8) | buf
[8];
998 lba
= ldl_be_p(buf
+ 2);
1000 if (nb_sectors
== 0) {
1001 ide_atapi_cmd_ok(s
);
1005 transfer_request
= buf
[9] & 0xf8;
1006 if (transfer_request
== 0x00) {
1008 ide_atapi_cmd_ok(s
);
1012 /* Check validity of BCL before transferring data */
1013 if (!validate_bcl(s
)) {
1017 switch (transfer_request
) {
1020 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2048);
1024 ide_atapi_cmd_read(s
, lba
, nb_sectors
, 2352);
1027 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1028 ASC_INV_FIELD_IN_CMD_PACKET
);
1033 static void cmd_seek(IDEState
*s
, uint8_t* buf
)
1036 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1038 lba
= ldl_be_p(buf
+ 2);
1039 if (lba
>= total_sectors
) {
1040 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_LOGICAL_BLOCK_OOR
);
1044 ide_atapi_cmd_ok(s
);
1047 static void cmd_start_stop_unit(IDEState
*s
, uint8_t* buf
)
1050 bool start
= buf
[4] & 1;
1051 bool loej
= buf
[4] & 2; /* load on start, eject on !start */
1052 int pwrcnd
= buf
[4] & 0xf0;
1055 /* eject/load only happens for power condition == 0 */
1056 ide_atapi_cmd_ok(s
);
1061 if (!start
&& !s
->tray_open
&& s
->tray_locked
) {
1062 sense
= blk_is_inserted(s
->blk
)
1063 ? NOT_READY
: ILLEGAL_REQUEST
;
1064 ide_atapi_cmd_error(s
, sense
, ASC_MEDIA_REMOVAL_PREVENTED
);
1068 if (s
->tray_open
!= !start
) {
1069 blk_eject(s
->blk
, !start
);
1070 s
->tray_open
= !start
;
1074 ide_atapi_cmd_ok(s
);
1077 static void cmd_mechanism_status(IDEState
*s
, uint8_t* buf
)
1079 int max_len
= lduw_be_p(buf
+ 8);
1082 /* no current LBA */
1087 stw_be_p(buf
+ 6, 0);
1088 ide_atapi_cmd_reply(s
, 8, max_len
);
1091 static void cmd_read_toc_pma_atip(IDEState
*s
, uint8_t* buf
)
1093 int format
, msf
, start_track
, len
;
1095 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1097 max_len
= lduw_be_p(buf
+ 7);
1098 format
= buf
[9] >> 6;
1099 msf
= (buf
[1] >> 1) & 1;
1100 start_track
= buf
[6];
1104 len
= cdrom_read_toc(total_sectors
, buf
, msf
, start_track
);
1107 ide_atapi_cmd_reply(s
, len
, max_len
);
1110 /* multi session : only a single session defined */
1115 ide_atapi_cmd_reply(s
, 12, max_len
);
1118 len
= cdrom_read_toc_raw(total_sectors
, buf
, msf
, start_track
);
1121 ide_atapi_cmd_reply(s
, len
, max_len
);
1125 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1126 ASC_INV_FIELD_IN_CMD_PACKET
);
1130 static void cmd_read_cdvd_capacity(IDEState
*s
, uint8_t* buf
)
1132 uint64_t total_sectors
= s
->nb_sectors
>> 2;
1134 /* NOTE: it is really the number of sectors minus 1 */
1135 stl_be_p(buf
, total_sectors
- 1);
1136 stl_be_p(buf
+ 4, 2048);
1137 ide_atapi_cmd_reply(s
, 8, 8);
1140 static void cmd_read_disc_information(IDEState
*s
, uint8_t* buf
)
1142 uint8_t type
= buf
[1] & 7;
1143 uint32_t max_len
= lduw_be_p(buf
+ 7);
1145 /* Types 1/2 are only defined for Blu-Ray. */
1147 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1148 ASC_INV_FIELD_IN_CMD_PACKET
);
1154 buf
[2] = 0xe; /* last session complete, disc finalized */
1155 buf
[3] = 1; /* first track on disc */
1156 buf
[4] = 1; /* # of sessions */
1157 buf
[5] = 1; /* first track of last session */
1158 buf
[6] = 1; /* last track of last session */
1159 buf
[7] = 0x20; /* unrestricted use */
1160 buf
[8] = 0x00; /* CD-ROM or DVD-ROM */
1161 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
1162 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
1163 /* 24-31: disc bar code */
1164 /* 32: disc application code */
1165 /* 33: number of OPC tables */
1167 ide_atapi_cmd_reply(s
, 34, max_len
);
1170 static void cmd_read_dvd_structure(IDEState
*s
, uint8_t* buf
)
1174 int format
= buf
[7];
1177 max_len
= lduw_be_p(buf
+ 8);
1179 if (format
< 0xff) {
1180 if (media_is_cd(s
)) {
1181 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1182 ASC_INCOMPATIBLE_FORMAT
);
1184 } else if (!media_present(s
)) {
1185 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1186 ASC_INV_FIELD_IN_CMD_PACKET
);
1191 memset(buf
, 0, max_len
> IDE_DMA_BUF_SECTORS
* BDRV_SECTOR_SIZE
+ 4 ?
1192 IDE_DMA_BUF_SECTORS
* BDRV_SECTOR_SIZE
+ 4 : max_len
);
1198 ret
= ide_dvd_read_structure(s
, format
, buf
, buf
);
1201 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, -ret
);
1203 ide_atapi_cmd_reply(s
, ret
, max_len
);
1208 /* TODO: BD support, fall through for now */
1210 /* Generic disk structures */
1211 case 0x80: /* TODO: AACS volume identifier */
1212 case 0x81: /* TODO: AACS media serial number */
1213 case 0x82: /* TODO: AACS media identifier */
1214 case 0x83: /* TODO: AACS media key block */
1215 case 0x90: /* TODO: List of recognized format layers */
1216 case 0xc0: /* TODO: Write protection status */
1218 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
,
1219 ASC_INV_FIELD_IN_CMD_PACKET
);
1224 static void cmd_set_speed(IDEState
*s
, uint8_t* buf
)
1226 ide_atapi_cmd_ok(s
);
1231 * Only commands flagged as ALLOW_UA are allowed to run under a
1232 * unit attention condition. (See MMC-5, section 4.1.6.1)
1237 * Commands flagged with CHECK_READY can only execute if a medium is present.
1238 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1244 * Commands flagged with NONDATA do not in any circumstances return
1245 * any data via ide_atapi_cmd_reply. These commands are exempt from
1246 * the normal byte_count_limit constraints.
1247 * See ATA8-ACS3 "7.21.5 Byte Count Limit"
1252 * CONDDATA implies a command that transfers data only conditionally based
1253 * on the presence of suboptions. It should be exempt from the BCL check at
1254 * command validation time, but it needs to be checked at the command
1255 * handler level instead.
1260 static const struct AtapiCmd
{
1261 void (*handler
)(IDEState
*s
, uint8_t *buf
);
1263 } atapi_cmd_table
[0x100] = {
1264 [ 0x00 ] = { cmd_test_unit_ready
, CHECK_READY
| NONDATA
},
1265 [ 0x03 ] = { cmd_request_sense
, ALLOW_UA
},
1266 [ 0x12 ] = { cmd_inquiry
, ALLOW_UA
},
1267 [ 0x1b ] = { cmd_start_stop_unit
, NONDATA
}, /* [1] */
1268 [ 0x1e ] = { cmd_prevent_allow_medium_removal
, NONDATA
},
1269 [ 0x25 ] = { cmd_read_cdvd_capacity
, CHECK_READY
},
1270 [ 0x28 ] = { cmd_read
, /* (10) */ CHECK_READY
},
1271 [ 0x2b ] = { cmd_seek
, CHECK_READY
| NONDATA
},
1272 [ 0x43 ] = { cmd_read_toc_pma_atip
, CHECK_READY
},
1273 [ 0x46 ] = { cmd_get_configuration
, ALLOW_UA
},
1274 [ 0x4a ] = { cmd_get_event_status_notification
, ALLOW_UA
},
1275 [ 0x51 ] = { cmd_read_disc_information
, CHECK_READY
},
1276 [ 0x5a ] = { cmd_mode_sense
, /* (10) */ 0 },
1277 [ 0xa8 ] = { cmd_read
, /* (12) */ CHECK_READY
},
1278 [ 0xad ] = { cmd_read_dvd_structure
, CHECK_READY
},
1279 [ 0xbb ] = { cmd_set_speed
, NONDATA
},
1280 [ 0xbd ] = { cmd_mechanism_status
, 0 },
1281 [ 0xbe ] = { cmd_read_cd
, CHECK_READY
| CONDDATA
},
1282 /* [1] handler detects and reports not ready condition itself */
1285 void ide_atapi_cmd(IDEState
*s
)
1287 uint8_t *buf
= s
->io_buffer
;
1288 const struct AtapiCmd
*cmd
= &atapi_cmd_table
[s
->io_buffer
[0]];
1290 trace_ide_atapi_cmd(s
, s
->io_buffer
[0]);
1292 if (trace_event_get_state_backends(TRACE_IDE_ATAPI_CMD_PACKET
)) {
1293 /* Each pretty-printed byte needs two bytes and a space; */
1294 char *ppacket
= g_malloc(ATAPI_PACKET_SIZE
* 3 + 1);
1296 for (i
= 0; i
< ATAPI_PACKET_SIZE
; i
++) {
1297 sprintf(ppacket
+ (i
* 3), "%02x ", buf
[i
]);
1299 trace_ide_atapi_cmd_packet(s
, s
->lcyl
| (s
->hcyl
<< 8), ppacket
);
1304 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1305 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1306 * condition response unless a higher priority status, defined by the drive
1309 if (s
->sense_key
== UNIT_ATTENTION
&& !(cmd
->flags
& ALLOW_UA
)) {
1310 ide_atapi_cmd_check_status(s
);
1314 * When a CD gets changed, we have to report an ejected state and
1315 * then a loaded state to guests so that they detect tray
1316 * open/close and media change events. Guests that do not use
1317 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1318 * states rely on this behavior.
1320 if (!(cmd
->flags
& ALLOW_UA
) &&
1321 !s
->tray_open
&& blk_is_inserted(s
->blk
) && s
->cdrom_changed
) {
1323 if (s
->cdrom_changed
== 1) {
1324 ide_atapi_cmd_error(s
, NOT_READY
, ASC_MEDIUM_NOT_PRESENT
);
1325 s
->cdrom_changed
= 2;
1327 ide_atapi_cmd_error(s
, UNIT_ATTENTION
, ASC_MEDIUM_MAY_HAVE_CHANGED
);
1328 s
->cdrom_changed
= 0;
1334 /* Report a Not Ready condition if appropriate for the command */
1335 if ((cmd
->flags
& CHECK_READY
) &&
1336 (!media_present(s
) || !blk_is_inserted(s
->blk
)))
1338 ide_atapi_cmd_error(s
, NOT_READY
, ASC_MEDIUM_NOT_PRESENT
);
1342 /* Commands that don't transfer DATA permit the byte_count_limit to be 0.
1343 * If this is a data-transferring PIO command and BCL is 0,
1344 * we abort at the /ATA/ level, not the ATAPI level.
1345 * See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
1346 if (cmd
->handler
&& !(cmd
->flags
& (NONDATA
| CONDDATA
))) {
1347 if (!validate_bcl(s
)) {
1352 /* Execute the command */
1354 cmd
->handler(s
, buf
);
1358 ide_atapi_cmd_error(s
, ILLEGAL_REQUEST
, ASC_ILLEGAL_OPCODE
);