2011-04-18 16:45:49 +02:00
|
|
|
/*
|
|
|
|
* QEMU ATAPI Emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
* Copyright (c) 2006 Openedhand Ltd.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:09 +01:00
|
|
|
#include "qemu/osdep.h"
|
2011-04-18 16:45:49 +02:00
|
|
|
#include "hw/ide/internal.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/scsi/scsi.h"
|
2014-10-07 13:59:18 +02:00
|
|
|
#include "sysemu/block-backend.h"
|
2011-04-18 16:45:49 +02:00
|
|
|
|
2016-05-06 18:26:41 +02:00
|
|
|
#define ATAPI_SECTOR_BITS (2 + BDRV_SECTOR_BITS)
|
|
|
|
#define ATAPI_SECTOR_SIZE (1 << ATAPI_SECTOR_BITS)
|
|
|
|
|
2011-04-18 16:45:49 +02:00
|
|
|
static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
|
|
|
|
|
|
|
|
static void padstr8(uint8_t *buf, int buf_size, const char *src)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < buf_size; i++) {
|
|
|
|
if (*src)
|
|
|
|
buf[i] = *src++;
|
|
|
|
else
|
|
|
|
buf[i] = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void cpu_to_ube16(uint8_t *buf, int val)
|
|
|
|
{
|
|
|
|
buf[0] = val >> 8;
|
|
|
|
buf[1] = val & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
|
|
|
|
{
|
|
|
|
buf[0] = val >> 24;
|
|
|
|
buf[1] = val >> 16;
|
|
|
|
buf[2] = val >> 8;
|
|
|
|
buf[3] = val & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ube16_to_cpu(const uint8_t *buf)
|
|
|
|
{
|
|
|
|
return (buf[0] << 8) | buf[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ube32_to_cpu(const uint8_t *buf)
|
|
|
|
{
|
|
|
|
return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lba_to_msf(uint8_t *buf, int lba)
|
|
|
|
{
|
|
|
|
lba += 150;
|
|
|
|
buf[0] = (lba / 75) / 60;
|
|
|
|
buf[1] = (lba / 75) % 60;
|
|
|
|
buf[2] = lba % 75;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int media_present(IDEState *s)
|
|
|
|
{
|
2011-09-06 18:58:41 +02:00
|
|
|
return !s->tray_open && s->nb_sectors > 0;
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 16:34:40 +02:00
|
|
|
/* XXX: DVDs that could fit on a CD will be reported as a CD */
|
2011-04-18 16:45:49 +02:00
|
|
|
static inline int media_is_dvd(IDEState *s)
|
|
|
|
{
|
|
|
|
return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int media_is_cd(IDEState *s)
|
|
|
|
{
|
|
|
|
return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cd_data_to_raw(uint8_t *buf, int lba)
|
|
|
|
{
|
|
|
|
/* sync bytes */
|
|
|
|
buf[0] = 0x00;
|
|
|
|
memset(buf + 1, 0xff, 10);
|
|
|
|
buf[11] = 0x00;
|
|
|
|
buf += 12;
|
|
|
|
/* MSF */
|
|
|
|
lba_to_msf(buf, lba);
|
|
|
|
buf[3] = 0x01; /* mode 1 data */
|
|
|
|
buf += 4;
|
|
|
|
/* data */
|
|
|
|
buf += 2048;
|
|
|
|
/* XXX: ECC not computed */
|
|
|
|
memset(buf, 0, 288);
|
|
|
|
}
|
|
|
|
|
2015-11-17 20:59:52 +01:00
|
|
|
static int
|
|
|
|
cd_read_sector_sync(IDEState *s)
|
2011-04-18 16:45:49 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2015-10-28 16:33:14 +01:00
|
|
|
block_acct_start(blk_get_stats(s->blk), &s->acct,
|
2016-05-06 18:26:41 +02:00
|
|
|
ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
|
2011-04-18 16:45:49 +02:00
|
|
|
|
2015-11-17 20:59:52 +01:00
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
|
|
|
printf("cd_read_sector_sync: lba=%d\n", s->lba);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (s->cd_sector_size) {
|
2011-04-18 16:45:49 +02:00
|
|
|
case 2048:
|
2016-05-06 18:26:41 +02:00
|
|
|
ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS,
|
|
|
|
s->io_buffer, ATAPI_SECTOR_SIZE);
|
2011-04-18 16:45:49 +02:00
|
|
|
break;
|
|
|
|
case 2352:
|
2016-05-06 18:26:41 +02:00
|
|
|
ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS,
|
|
|
|
s->io_buffer + 16, ATAPI_SECTOR_SIZE);
|
2015-10-28 16:33:14 +01:00
|
|
|
if (ret >= 0) {
|
2015-11-17 20:59:52 +01:00
|
|
|
cd_data_to_raw(s->io_buffer, s->lba);
|
2015-10-28 16:33:14 +01:00
|
|
|
}
|
2011-04-18 16:45:49 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-10-28 16:33:14 +01:00
|
|
|
block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
block_acct_failed(blk_get_stats(s->blk), &s->acct);
|
|
|
|
} else {
|
|
|
|
block_acct_done(blk_get_stats(s->blk), &s->acct);
|
2015-11-17 20:59:52 +01:00
|
|
|
s->lba++;
|
|
|
|
s->io_buffer_index = 0;
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|
2015-10-28 16:33:14 +01:00
|
|
|
|
2011-04-18 16:45:49 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-11-17 20:59:52 +01:00
|
|
|
static void cd_read_sector_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
IDEState *s = opaque;
|
|
|
|
|
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
|
|
|
printf("cd_read_sector_cb: lba=%d ret=%d\n", s->lba, ret);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2015-11-24 13:17:59 +01:00
|
|
|
block_acct_failed(blk_get_stats(s->blk), &s->acct);
|
2015-11-17 20:59:52 +01:00
|
|
|
ide_atapi_io_error(s, ret);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-24 13:17:59 +01:00
|
|
|
block_acct_done(blk_get_stats(s->blk), &s->acct);
|
|
|
|
|
2015-11-17 20:59:52 +01:00
|
|
|
if (s->cd_sector_size == 2352) {
|
|
|
|
cd_data_to_raw(s->io_buffer, s->lba);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->lba++;
|
|
|
|
s->io_buffer_index = 0;
|
|
|
|
s->status &= ~BUSY_STAT;
|
|
|
|
|
|
|
|
ide_atapi_cmd_reply_end(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int cd_read_sector(IDEState *s)
|
|
|
|
{
|
|
|
|
if (s->cd_sector_size != 2048 && s->cd_sector_size != 2352) {
|
2015-11-24 13:17:59 +01:00
|
|
|
block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
|
2015-11-17 20:59:52 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->iov.iov_base = (s->cd_sector_size == 2352) ?
|
|
|
|
s->io_buffer + 16 : s->io_buffer;
|
|
|
|
|
2016-05-06 18:26:41 +02:00
|
|
|
s->iov.iov_len = ATAPI_SECTOR_SIZE;
|
2015-11-17 20:59:52 +01:00
|
|
|
qemu_iovec_init_external(&s->qiov, &s->iov, 1);
|
|
|
|
|
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
|
|
|
printf("cd_read_sector: lba=%d\n", s->lba);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
block_acct_start(blk_get_stats(s->blk), &s->acct,
|
2016-05-06 18:26:41 +02:00
|
|
|
ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
|
2015-11-17 20:59:52 +01:00
|
|
|
|
2015-11-17 21:06:33 +01:00
|
|
|
ide_buffered_readv(s, (int64_t)s->lba << 2, &s->qiov, 4,
|
|
|
|
cd_read_sector_cb, s);
|
2015-11-17 20:59:52 +01:00
|
|
|
|
|
|
|
s->status |= BUSY_STAT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-18 16:45:49 +02:00
|
|
|
void ide_atapi_cmd_ok(IDEState *s)
|
|
|
|
{
|
|
|
|
s->error = 0;
|
|
|
|
s->status = READY_STAT | SEEK_STAT;
|
|
|
|
s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
|
2014-09-13 05:51:12 +02:00
|
|
|
ide_transfer_stop(s);
|
2011-04-18 16:45:49 +02:00
|
|
|
ide_set_irq(s->bus);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
|
|
|
printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
|
|
|
|
#endif
|
|
|
|
s->error = sense_key << 4;
|
|
|
|
s->status = READY_STAT | ERR_STAT;
|
|
|
|
s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
|
|
|
|
s->sense_key = sense_key;
|
|
|
|
s->asc = asc;
|
2014-09-13 05:51:12 +02:00
|
|
|
ide_transfer_stop(s);
|
2011-04-18 16:45:49 +02:00
|
|
|
ide_set_irq(s->bus);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ide_atapi_io_error(IDEState *s, int ret)
|
|
|
|
{
|
|
|
|
/* XXX: handle more errors */
|
|
|
|
if (ret == -ENOMEDIUM) {
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, NOT_READY,
|
2011-04-18 16:45:49 +02:00
|
|
|
ASC_MEDIUM_NOT_PRESENT);
|
|
|
|
} else {
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
2011-04-18 16:45:49 +02:00
|
|
|
ASC_LOGICAL_BLOCK_OOR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 20:31:42 +01:00
|
|
|
static uint16_t atapi_byte_count_limit(IDEState *s)
|
|
|
|
{
|
|
|
|
uint16_t bcl;
|
|
|
|
|
|
|
|
bcl = s->lcyl | (s->hcyl << 8);
|
|
|
|
if (bcl == 0xffff) {
|
|
|
|
return 0xfffe;
|
|
|
|
}
|
|
|
|
return bcl;
|
|
|
|
}
|
|
|
|
|
2011-04-18 16:45:49 +02:00
|
|
|
/* The whole ATAPI transfer logic is handled in this function */
|
|
|
|
void ide_atapi_cmd_reply_end(IDEState *s)
|
|
|
|
{
|
|
|
|
int byte_count_limit, size, ret;
|
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
|
|
|
printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
|
|
|
|
s->packet_transfer_size,
|
|
|
|
s->elementary_transfer_size,
|
|
|
|
s->io_buffer_index);
|
|
|
|
#endif
|
|
|
|
if (s->packet_transfer_size <= 0) {
|
|
|
|
/* end of transfer */
|
2014-09-13 05:51:12 +02:00
|
|
|
ide_atapi_cmd_ok(s);
|
2011-04-18 16:45:49 +02:00
|
|
|
ide_set_irq(s->bus);
|
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
2015-11-17 20:59:52 +01:00
|
|
|
printf("end of transfer, status=0x%x\n", s->status);
|
2011-04-18 16:45:49 +02:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
/* see if a new sector must be read */
|
|
|
|
if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
|
2015-11-17 20:59:52 +01:00
|
|
|
if (!s->elementary_transfer_size) {
|
|
|
|
ret = cd_read_sector(s);
|
|
|
|
if (ret < 0) {
|
|
|
|
ide_atapi_io_error(s, ret);
|
|
|
|
}
|
2011-04-18 16:45:49 +02:00
|
|
|
return;
|
2015-11-17 20:59:52 +01:00
|
|
|
} else {
|
|
|
|
/* rebuffering within an elementary transfer is
|
|
|
|
* only possible with a sync request because we
|
|
|
|
* end up with a race condition otherwise */
|
|
|
|
ret = cd_read_sector_sync(s);
|
|
|
|
if (ret < 0) {
|
|
|
|
ide_atapi_io_error(s, ret);
|
|
|
|
return;
|
|
|
|
}
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (s->elementary_transfer_size > 0) {
|
|
|
|
/* there are some data left to transmit in this elementary
|
|
|
|
transfer */
|
|
|
|
size = s->cd_sector_size - s->io_buffer_index;
|
|
|
|
if (size > s->elementary_transfer_size)
|
|
|
|
size = s->elementary_transfer_size;
|
|
|
|
s->packet_transfer_size -= size;
|
|
|
|
s->elementary_transfer_size -= size;
|
|
|
|
s->io_buffer_index += size;
|
|
|
|
ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
|
|
|
|
size, ide_atapi_cmd_reply_end);
|
|
|
|
} else {
|
|
|
|
/* a new transfer is needed */
|
|
|
|
s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
|
2015-11-13 20:31:42 +01:00
|
|
|
byte_count_limit = atapi_byte_count_limit(s);
|
2011-04-18 16:45:49 +02:00
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
|
|
|
printf("byte_count_limit=%d\n", byte_count_limit);
|
|
|
|
#endif
|
|
|
|
size = s->packet_transfer_size;
|
|
|
|
if (size > byte_count_limit) {
|
|
|
|
/* byte count limit must be even if this case */
|
|
|
|
if (byte_count_limit & 1)
|
|
|
|
byte_count_limit--;
|
|
|
|
size = byte_count_limit;
|
|
|
|
}
|
|
|
|
s->lcyl = size;
|
|
|
|
s->hcyl = size >> 8;
|
|
|
|
s->elementary_transfer_size = size;
|
|
|
|
/* we cannot transmit more than one sector at a time */
|
|
|
|
if (s->lba != -1) {
|
|
|
|
if (size > (s->cd_sector_size - s->io_buffer_index))
|
|
|
|
size = (s->cd_sector_size - s->io_buffer_index);
|
|
|
|
}
|
|
|
|
s->packet_transfer_size -= size;
|
|
|
|
s->elementary_transfer_size -= size;
|
|
|
|
s->io_buffer_index += size;
|
|
|
|
ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
|
|
|
|
size, ide_atapi_cmd_reply_end);
|
|
|
|
ide_set_irq(s->bus);
|
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
|
|
|
printf("status=0x%x\n", s->status);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
|
|
|
|
static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
|
|
|
|
{
|
|
|
|
if (size > max_size)
|
|
|
|
size = max_size;
|
|
|
|
s->lba = -1; /* no sector read */
|
|
|
|
s->packet_transfer_size = size;
|
|
|
|
s->io_buffer_size = size; /* dma: send the reply data as one chunk */
|
|
|
|
s->elementary_transfer_size = 0;
|
|
|
|
|
|
|
|
if (s->atapi_dma) {
|
2014-10-07 13:59:18 +02:00
|
|
|
block_acct_start(blk_get_stats(s->blk), &s->acct, size,
|
2014-09-05 15:46:18 +02:00
|
|
|
BLOCK_ACCT_READ);
|
2011-04-18 16:45:49 +02:00
|
|
|
s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
|
2014-08-04 23:11:10 +02:00
|
|
|
ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
|
2011-04-18 16:45:49 +02:00
|
|
|
} else {
|
|
|
|
s->status = READY_STAT | SEEK_STAT;
|
2015-02-23 17:18:00 +01:00
|
|
|
s->io_buffer_index = 0;
|
2011-04-18 16:45:49 +02:00
|
|
|
ide_atapi_cmd_reply_end(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start a CD-CDROM read command */
|
|
|
|
static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
|
|
|
|
int sector_size)
|
|
|
|
{
|
|
|
|
s->lba = lba;
|
|
|
|
s->packet_transfer_size = nb_sectors * sector_size;
|
|
|
|
s->elementary_transfer_size = 0;
|
|
|
|
s->io_buffer_index = sector_size;
|
|
|
|
s->cd_sector_size = sector_size;
|
|
|
|
|
|
|
|
ide_atapi_cmd_reply_end(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ide_atapi_cmd_check_status(IDEState *s)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
|
|
|
printf("atapi_cmd_check_status\n");
|
|
|
|
#endif
|
2011-09-13 14:41:56 +02:00
|
|
|
s->error = MC_ERR | (UNIT_ATTENTION << 4);
|
2011-04-18 16:45:49 +02:00
|
|
|
s->status = ERR_STAT;
|
|
|
|
s->nsector = 0;
|
|
|
|
ide_set_irq(s->bus);
|
|
|
|
}
|
|
|
|
/* ATAPI DMA support */
|
|
|
|
|
|
|
|
static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
IDEState *s = opaque;
|
|
|
|
int data_offset, n;
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2016-04-13 00:48:15 +02:00
|
|
|
if (ide_handle_rw_error(s, -ret, ide_dma_cmd_to_retry(s->dma_cmd))) {
|
|
|
|
if (s->bus->error_status) {
|
2016-08-09 17:47:23 +02:00
|
|
|
s->bus->dma->aiocb = NULL;
|
2016-04-13 00:48:15 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
goto eot;
|
|
|
|
}
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s->io_buffer_size > 0) {
|
|
|
|
/*
|
|
|
|
* For a cdrom read sector command (s->lba != -1),
|
|
|
|
* adjust the lba for the next s->io_buffer_size chunk
|
|
|
|
* and dma the current chunk.
|
|
|
|
* For a command != read (s->lba == -1), just transfer
|
|
|
|
* the reply data.
|
|
|
|
*/
|
|
|
|
if (s->lba != -1) {
|
|
|
|
if (s->cd_sector_size == 2352) {
|
|
|
|
n = 1;
|
|
|
|
cd_data_to_raw(s->io_buffer, s->lba);
|
|
|
|
} else {
|
|
|
|
n = s->io_buffer_size >> 11;
|
|
|
|
}
|
|
|
|
s->lba += n;
|
|
|
|
}
|
|
|
|
s->packet_transfer_size -= s->io_buffer_size;
|
|
|
|
if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
|
|
|
|
goto eot;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->packet_transfer_size <= 0) {
|
|
|
|
s->status = READY_STAT | SEEK_STAT;
|
|
|
|
s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
|
|
|
|
ide_set_irq(s->bus);
|
2011-08-25 08:26:01 +02:00
|
|
|
goto eot;
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s->io_buffer_index = 0;
|
|
|
|
if (s->cd_sector_size == 2352) {
|
|
|
|
n = 1;
|
|
|
|
s->io_buffer_size = s->cd_sector_size;
|
|
|
|
data_offset = 16;
|
|
|
|
} else {
|
|
|
|
n = s->packet_transfer_size >> 11;
|
|
|
|
if (n > (IDE_DMA_BUF_SECTORS / 4))
|
|
|
|
n = (IDE_DMA_BUF_SECTORS / 4);
|
|
|
|
s->io_buffer_size = n * 2048;
|
|
|
|
data_offset = 0;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_AIO
|
|
|
|
printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
|
|
|
|
#endif
|
2011-08-25 08:26:01 +02:00
|
|
|
|
2011-04-18 16:45:49 +02:00
|
|
|
s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
|
2016-05-06 18:26:41 +02:00
|
|
|
s->bus->dma->iov.iov_len = n * ATAPI_SECTOR_SIZE;
|
2011-04-18 16:45:49 +02:00
|
|
|
qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
|
2011-08-25 08:26:01 +02:00
|
|
|
|
2015-11-17 21:06:33 +01:00
|
|
|
s->bus->dma->aiocb = ide_buffered_readv(s, (int64_t)s->lba << 2,
|
|
|
|
&s->bus->dma->qiov, n * 4,
|
|
|
|
ide_atapi_cmd_read_dma_cb, s);
|
2011-08-25 08:26:01 +02:00
|
|
|
return;
|
2011-11-30 09:12:30 +01:00
|
|
|
|
2011-08-25 08:26:01 +02:00
|
|
|
eot:
|
2015-10-28 16:33:14 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
block_acct_failed(blk_get_stats(s->blk), &s->acct);
|
|
|
|
} else {
|
2015-11-24 13:18:00 +01:00
|
|
|
block_acct_done(blk_get_stats(s->blk), &s->acct);
|
2015-10-28 16:33:14 +01:00
|
|
|
}
|
2014-08-04 23:11:12 +02:00
|
|
|
ide_set_inactive(s, false);
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* start a CD-CDROM read command with DMA */
|
|
|
|
/* XXX: test if DMA is available */
|
|
|
|
static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
|
|
|
|
int sector_size)
|
|
|
|
{
|
|
|
|
s->lba = lba;
|
|
|
|
s->packet_transfer_size = nb_sectors * sector_size;
|
|
|
|
s->io_buffer_size = 0;
|
|
|
|
s->cd_sector_size = sector_size;
|
|
|
|
|
2014-10-07 13:59:18 +02:00
|
|
|
block_acct_start(blk_get_stats(s->blk), &s->acct, s->packet_transfer_size,
|
2014-09-05 15:46:18 +02:00
|
|
|
BLOCK_ACCT_READ);
|
2011-08-25 08:26:01 +02:00
|
|
|
|
2011-04-18 16:45:49 +02:00
|
|
|
/* XXX: check if BUSY_STAT should be set */
|
|
|
|
s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
|
2014-08-04 23:11:10 +02:00
|
|
|
ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
|
|
|
|
int sector_size)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
|
|
|
printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
|
|
|
|
lba, nb_sectors);
|
|
|
|
#endif
|
|
|
|
if (s->atapi_dma) {
|
|
|
|
ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
|
|
|
|
} else {
|
|
|
|
ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 19:15:09 +01:00
|
|
|
void ide_atapi_dma_restart(IDEState *s)
|
|
|
|
{
|
|
|
|
/*
|
2016-04-12 22:47:52 +02:00
|
|
|
* At this point we can just re-evaluate the packet command and start over.
|
|
|
|
* The presence of ->dma_cb callback in the pre_save ensures that the packet
|
|
|
|
* command has been completely sent and we can safely restart command.
|
2014-12-09 19:15:09 +01:00
|
|
|
*/
|
2016-04-12 22:47:52 +02:00
|
|
|
s->unit = s->bus->retry_unit;
|
|
|
|
s->bus->dma->ops->restart_dma(s->bus->dma);
|
|
|
|
ide_atapi_cmd(s);
|
2014-12-09 19:15:09 +01:00
|
|
|
}
|
|
|
|
|
2011-04-18 16:45:49 +02:00
|
|
|
static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
|
|
|
|
uint16_t profile)
|
|
|
|
{
|
|
|
|
uint8_t *buf_profile = buf + 12; /* start of profiles */
|
|
|
|
|
|
|
|
buf_profile += ((*index) * 4); /* start of indexed profile */
|
|
|
|
cpu_to_ube16 (buf_profile, profile);
|
|
|
|
buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
|
|
|
|
|
|
|
|
/* each profile adds 4 bytes to the response */
|
|
|
|
(*index)++;
|
|
|
|
buf[11] += 4; /* Additional Length */
|
|
|
|
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ide_dvd_read_structure(IDEState *s, int format,
|
|
|
|
const uint8_t *packet, uint8_t *buf)
|
|
|
|
{
|
|
|
|
switch (format) {
|
|
|
|
case 0x0: /* Physical format information */
|
|
|
|
{
|
|
|
|
int layer = packet[6];
|
|
|
|
uint64_t total_sectors;
|
|
|
|
|
|
|
|
if (layer != 0)
|
|
|
|
return -ASC_INV_FIELD_IN_CMD_PACKET;
|
|
|
|
|
2011-04-19 13:13:44 +02:00
|
|
|
total_sectors = s->nb_sectors >> 2;
|
|
|
|
if (total_sectors == 0) {
|
2011-04-18 16:45:49 +02:00
|
|
|
return -ASC_MEDIUM_NOT_PRESENT;
|
2011-04-19 13:13:44 +02:00
|
|
|
}
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
buf[4] = 1; /* DVD-ROM, part version 1 */
|
|
|
|
buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
|
|
|
|
buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
|
|
|
|
buf[7] = 0; /* default densities */
|
|
|
|
|
|
|
|
/* FIXME: 0x30000 per spec? */
|
|
|
|
cpu_to_ube32(buf + 8, 0); /* start sector */
|
|
|
|
cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
|
|
|
|
cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
|
|
|
|
|
|
|
|
/* Size of buffer, not including 2 byte size field */
|
2013-11-05 17:38:34 +01:00
|
|
|
stw_be_p(buf, 2048 + 2);
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
/* 2k data + 4 byte header */
|
|
|
|
return (2048 + 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x01: /* DVD copyright information */
|
|
|
|
buf[4] = 0; /* no copyright data */
|
|
|
|
buf[5] = 0; /* no region restrictions */
|
|
|
|
|
|
|
|
/* Size of buffer, not including 2 byte size field */
|
2013-11-05 17:38:34 +01:00
|
|
|
stw_be_p(buf, 4 + 2);
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
/* 4 byte header + 4 byte data */
|
|
|
|
return (4 + 4);
|
|
|
|
|
|
|
|
case 0x03: /* BCA information - invalid field for no BCA info */
|
|
|
|
return -ASC_INV_FIELD_IN_CMD_PACKET;
|
|
|
|
|
|
|
|
case 0x04: /* DVD disc manufacturing information */
|
|
|
|
/* Size of buffer, not including 2 byte size field */
|
2013-11-05 17:38:34 +01:00
|
|
|
stw_be_p(buf, 2048 + 2);
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
/* 2k data + 4 byte header */
|
|
|
|
return (2048 + 4);
|
|
|
|
|
|
|
|
case 0xff:
|
|
|
|
/*
|
|
|
|
* This lists all the command capabilities above. Add new ones
|
|
|
|
* in order and update the length and buffer return values.
|
|
|
|
*/
|
|
|
|
|
|
|
|
buf[4] = 0x00; /* Physical format */
|
|
|
|
buf[5] = 0x40; /* Not writable, is readable */
|
2013-11-05 17:38:34 +01:00
|
|
|
stw_be_p(buf + 6, 2048 + 4);
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
buf[8] = 0x01; /* Copyright info */
|
|
|
|
buf[9] = 0x40; /* Not writable, is readable */
|
2013-11-05 17:38:34 +01:00
|
|
|
stw_be_p(buf + 10, 4 + 4);
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
buf[12] = 0x03; /* BCA info */
|
|
|
|
buf[13] = 0x40; /* Not writable, is readable */
|
2013-11-05 17:38:34 +01:00
|
|
|
stw_be_p(buf + 14, 188 + 4);
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
buf[16] = 0x04; /* Manufacturing info */
|
|
|
|
buf[17] = 0x40; /* Not writable, is readable */
|
2013-11-05 17:38:34 +01:00
|
|
|
stw_be_p(buf + 18, 2048 + 4);
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
/* Size of buffer, not including 2 byte size field */
|
2013-11-05 17:38:34 +01:00
|
|
|
stw_be_p(buf, 16 + 2);
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
/* data written + 4 byte header */
|
|
|
|
return (16 + 4);
|
|
|
|
|
|
|
|
default: /* TODO: formats beyond DVD-ROM requires */
|
|
|
|
return -ASC_INV_FIELD_IN_CMD_PACKET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int event_status_media(IDEState *s,
|
|
|
|
uint8_t *buf)
|
|
|
|
{
|
|
|
|
uint8_t event_code, media_status;
|
|
|
|
|
|
|
|
media_status = 0;
|
2011-09-06 18:58:38 +02:00
|
|
|
if (s->tray_open) {
|
2011-04-18 16:45:49 +02:00
|
|
|
media_status = MS_TRAY_OPEN;
|
2014-10-07 13:59:18 +02:00
|
|
|
} else if (blk_is_inserted(s->blk)) {
|
2011-04-18 16:45:49 +02:00
|
|
|
media_status = MS_MEDIA_PRESENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Event notification descriptor */
|
|
|
|
event_code = MEC_NO_CHANGE;
|
2011-10-25 12:53:39 +02:00
|
|
|
if (media_status != MS_TRAY_OPEN) {
|
|
|
|
if (s->events.new_media) {
|
|
|
|
event_code = MEC_NEW_MEDIA;
|
|
|
|
s->events.new_media = false;
|
|
|
|
} else if (s->events.eject_request) {
|
|
|
|
event_code = MEC_EJECT_REQUESTED;
|
|
|
|
s->events.eject_request = false;
|
|
|
|
}
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
buf[4] = event_code;
|
|
|
|
buf[5] = media_status;
|
|
|
|
|
|
|
|
/* These fields are reserved, just clear them. */
|
|
|
|
buf[6] = 0;
|
|
|
|
buf[7] = 0;
|
|
|
|
|
|
|
|
return 8; /* We wrote to 4 extra bytes from the header */
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:15:54 +01:00
|
|
|
/*
|
|
|
|
* Before transferring data or otherwise signalling acceptance of a command
|
|
|
|
* marked CONDDATA, we must check the validity of the byte_count_limit.
|
|
|
|
*/
|
|
|
|
static bool validate_bcl(IDEState *s)
|
|
|
|
{
|
|
|
|
/* TODO: Check IDENTIFY data word 125 for defacult BCL (currently 0) */
|
|
|
|
if (s->atapi_dma || atapi_byte_count_limit(s)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Move abort back into core.c and introduce proper error flow between
|
|
|
|
* ATAPI layer and IDE core layer */
|
|
|
|
ide_abort_command(s);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-04-18 17:55:08 +02:00
|
|
|
static void cmd_get_event_status_notification(IDEState *s,
|
|
|
|
uint8_t *buf)
|
2011-04-18 16:45:49 +02:00
|
|
|
{
|
2011-04-18 17:55:08 +02:00
|
|
|
const uint8_t *packet = buf;
|
|
|
|
|
2011-04-18 16:45:49 +02:00
|
|
|
struct {
|
|
|
|
uint8_t opcode;
|
|
|
|
uint8_t polled; /* lsb bit is polled; others are reserved */
|
|
|
|
uint8_t reserved2[2];
|
|
|
|
uint8_t class;
|
|
|
|
uint8_t reserved3[2];
|
|
|
|
uint16_t len;
|
|
|
|
uint8_t control;
|
2011-08-31 12:38:01 +02:00
|
|
|
} QEMU_PACKED *gesn_cdb;
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
struct {
|
|
|
|
uint16_t len;
|
|
|
|
uint8_t notification_class;
|
|
|
|
uint8_t supported_events;
|
2011-08-31 12:38:01 +02:00
|
|
|
} QEMU_PACKED *gesn_event_header;
|
2011-04-18 16:45:49 +02:00
|
|
|
unsigned int max_len, used_len;
|
|
|
|
|
|
|
|
gesn_cdb = (void *)packet;
|
|
|
|
gesn_event_header = (void *)buf;
|
|
|
|
|
|
|
|
max_len = be16_to_cpu(gesn_cdb->len);
|
|
|
|
|
|
|
|
/* It is fine by the MMC spec to not support async mode operations */
|
|
|
|
if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
|
|
|
|
/* Only polling is supported, asynchronous mode is not. */
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
2011-04-18 16:45:49 +02:00
|
|
|
ASC_INV_FIELD_IN_CMD_PACKET);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* polling mode operation */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are the supported events.
|
|
|
|
*
|
|
|
|
* We currently only support requests of the 'media' type.
|
2011-09-13 16:00:52 +02:00
|
|
|
* Notification class requests and supported event classes are bitmasks,
|
|
|
|
* but they are build from the same values as the "notification class"
|
|
|
|
* field.
|
2011-04-18 16:45:49 +02:00
|
|
|
*/
|
2011-09-13 16:00:52 +02:00
|
|
|
gesn_event_header->supported_events = 1 << GESN_MEDIA;
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We use |= below to set the class field; other bits in this byte
|
|
|
|
* are reserved now but this is useful to do if we have to use the
|
|
|
|
* reserved fields later.
|
|
|
|
*/
|
|
|
|
gesn_event_header->notification_class = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Responses to requests are to be based on request priority. The
|
|
|
|
* notification_class_request_type enum above specifies the
|
|
|
|
* priority: upper elements are higher prio than lower ones.
|
|
|
|
*/
|
2011-09-13 16:00:52 +02:00
|
|
|
if (gesn_cdb->class & (1 << GESN_MEDIA)) {
|
|
|
|
gesn_event_header->notification_class |= GESN_MEDIA;
|
2011-04-18 16:45:49 +02:00
|
|
|
used_len = event_status_media(s, buf);
|
|
|
|
} else {
|
|
|
|
gesn_event_header->notification_class = 0x80; /* No event available */
|
|
|
|
used_len = sizeof(*gesn_event_header);
|
|
|
|
}
|
|
|
|
gesn_event_header->len = cpu_to_be16(used_len
|
|
|
|
- sizeof(*gesn_event_header));
|
|
|
|
ide_atapi_cmd_reply(s, used_len, max_len);
|
|
|
|
}
|
|
|
|
|
2011-04-18 17:55:08 +02:00
|
|
|
static void cmd_request_sense(IDEState *s, uint8_t *buf)
|
|
|
|
{
|
|
|
|
int max_len = buf[4];
|
|
|
|
|
|
|
|
memset(buf, 0, 18);
|
|
|
|
buf[0] = 0x70 | (1 << 7);
|
|
|
|
buf[2] = s->sense_key;
|
|
|
|
buf[7] = 10;
|
|
|
|
buf[12] = s->asc;
|
|
|
|
|
2011-09-13 14:41:56 +02:00
|
|
|
if (s->sense_key == UNIT_ATTENTION) {
|
|
|
|
s->sense_key = NO_SENSE;
|
2011-04-18 17:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ide_atapi_cmd_reply(s, 18, max_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_inquiry(IDEState *s, uint8_t *buf)
|
|
|
|
{
|
ide: Implement VPD response for ATAPI
SCSI devices have multiple kinds of queries they need to respond
to, as defined in the "cmd inquiry" section in MMC-6 and SPC-3.
Relevent sections:
MMC-6 revision 2g:
Non-VPD response data and pointer to SPC-3;
Section 6.8 "Inquiry Command"
SPC-3 revision 23:
Inquiry command and error handling:
Section 6.4 "INQUIRY command"
VPD data pages format:
Section 7.6 "Vital product data parameters"
We implement these Vital Product Data queries for SCSI, but not for
ATAPI through IDE. The result is that if you are looking for the WWN
identifier via tools such as sg3_utils, you will be unable to query
our CD/DVD rom device to obtain it.
This patch adds the minimum number of mandatory responses as defined
by SPC-3, which include the "supported pages" response (page 0x00)
and the "Device Identification" response (page 0x83). It also correctly
responds when it receives a request for an illegal page to improve
error output from related tools.
The Device ID page contains an arbitrary list of identification
strings of various formats; the ID strings included in this patch
were chosen to mimic those provided by the libata driver when
emulating this SCSI query (model, serial, and wwn when present.)
Example:
# libata emulated response
[root@localhost ~]# sg_inq --id /dev/sda
VPD INQUIRY: Device Identification page
Designation descriptor number 1, descriptor length: 24
designator_type: vendor specific [0x0], code_set: ASCII
associated with the addressed logical unit
vendor specific: QM00001
Designation descriptor number 2, descriptor length: 72
designator_type: T10 vendor identification, code_set: ASCII
associated with the addressed logical unit
vendor id: ATA
vendor specific: QEMU HARDDISK QM00001
# QEMU generated ATAPI response, with WWN
[root@localhost ~]# sg_inq --id /dev/sr0
VPD INQUIRY: Device Identification page
Designation descriptor number 1, descriptor length: 24
designator_type: vendor specific [0x0], code_set: ASCII
associated with the addressed logical unit
vendor specific: QM00005
Designation descriptor number 2, descriptor length: 72
designator_type: T10 vendor identification, code_set: ASCII
associated with the addressed logical unit
vendor id: ATA
vendor specific: QEMU DVD-ROM QM00005
Designation descriptor number 3, descriptor length: 12
designator_type: NAA, code_set: Binary
associated with the addressed logical unit
NAA 5, IEEE Company_id: 0xc50
Vendor Specific Identifier: 0x15ea71bb
[0x5000c50015ea71bb]
See also: hw/scsi/scsi-disk.c, scsi_disk_emulate_inquiry()
Signed-off-by: John Snow <jsnow@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-12-10 19:17:07 +01:00
|
|
|
uint8_t page_code = buf[2];
|
2011-04-18 17:55:08 +02:00
|
|
|
int max_len = buf[4];
|
|
|
|
|
ide: Implement VPD response for ATAPI
SCSI devices have multiple kinds of queries they need to respond
to, as defined in the "cmd inquiry" section in MMC-6 and SPC-3.
Relevent sections:
MMC-6 revision 2g:
Non-VPD response data and pointer to SPC-3;
Section 6.8 "Inquiry Command"
SPC-3 revision 23:
Inquiry command and error handling:
Section 6.4 "INQUIRY command"
VPD data pages format:
Section 7.6 "Vital product data parameters"
We implement these Vital Product Data queries for SCSI, but not for
ATAPI through IDE. The result is that if you are looking for the WWN
identifier via tools such as sg3_utils, you will be unable to query
our CD/DVD rom device to obtain it.
This patch adds the minimum number of mandatory responses as defined
by SPC-3, which include the "supported pages" response (page 0x00)
and the "Device Identification" response (page 0x83). It also correctly
responds when it receives a request for an illegal page to improve
error output from related tools.
The Device ID page contains an arbitrary list of identification
strings of various formats; the ID strings included in this patch
were chosen to mimic those provided by the libata driver when
emulating this SCSI query (model, serial, and wwn when present.)
Example:
# libata emulated response
[root@localhost ~]# sg_inq --id /dev/sda
VPD INQUIRY: Device Identification page
Designation descriptor number 1, descriptor length: 24
designator_type: vendor specific [0x0], code_set: ASCII
associated with the addressed logical unit
vendor specific: QM00001
Designation descriptor number 2, descriptor length: 72
designator_type: T10 vendor identification, code_set: ASCII
associated with the addressed logical unit
vendor id: ATA
vendor specific: QEMU HARDDISK QM00001
# QEMU generated ATAPI response, with WWN
[root@localhost ~]# sg_inq --id /dev/sr0
VPD INQUIRY: Device Identification page
Designation descriptor number 1, descriptor length: 24
designator_type: vendor specific [0x0], code_set: ASCII
associated with the addressed logical unit
vendor specific: QM00005
Designation descriptor number 2, descriptor length: 72
designator_type: T10 vendor identification, code_set: ASCII
associated with the addressed logical unit
vendor id: ATA
vendor specific: QEMU DVD-ROM QM00005
Designation descriptor number 3, descriptor length: 12
designator_type: NAA, code_set: Binary
associated with the addressed logical unit
NAA 5, IEEE Company_id: 0xc50
Vendor Specific Identifier: 0x15ea71bb
[0x5000c50015ea71bb]
See also: hw/scsi/scsi-disk.c, scsi_disk_emulate_inquiry()
Signed-off-by: John Snow <jsnow@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
2014-12-10 19:17:07 +01:00
|
|
|
unsigned idx = 0;
|
|
|
|
unsigned size_idx;
|
|
|
|
unsigned preamble_len;
|
|
|
|
|
|
|
|
/* If the EVPD (Enable Vital Product Data) bit is set in byte 1,
|
|
|
|
* we are being asked for a specific page of info indicated by byte 2. */
|
|
|
|
if (buf[1] & 0x01) {
|
|
|
|
preamble_len = 4;
|
|
|
|
size_idx = 3;
|
|
|
|
|
|
|
|
buf[idx++] = 0x05; /* CD-ROM */
|
|
|
|
buf[idx++] = page_code; /* Page Code */
|
|
|
|
buf[idx++] = 0x00; /* reserved */
|
|
|
|
idx++; /* length (set later) */
|
|
|
|
|
|
|
|
switch (page_code) {
|
|
|
|
case 0x00:
|
|
|
|
/* Supported Pages: List of supported VPD responses. */
|
|
|
|
buf[idx++] = 0x00; /* 0x00: Supported Pages, and: */
|
|
|
|
buf[idx++] = 0x83; /* 0x83: Device Identification. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x83:
|
|
|
|
/* Device Identification. Each entry is optional, but the entries
|
|
|
|
* included here are modeled after libata's VPD responses.
|
|
|
|
* If the response is given, at least one entry must be present. */
|
|
|
|
|
|
|
|
/* Entry 1: Serial */
|
|
|
|
if (idx + 24 > max_len) {
|
|
|
|
/* Not enough room for even the first entry: */
|
|
|
|
/* 4 byte header + 20 byte string */
|
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
|
|
|
ASC_DATA_PHASE_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf[idx++] = 0x02; /* Ascii */
|
|
|
|
buf[idx++] = 0x00; /* Vendor Specific */
|
|
|
|
buf[idx++] = 0x00;
|
|
|
|
buf[idx++] = 20; /* Remaining length */
|
|
|
|
padstr8(buf + idx, 20, s->drive_serial_str);
|
|
|
|
idx += 20;
|
|
|
|
|
|
|
|
/* Entry 2: Drive Model and Serial */
|
|
|
|
if (idx + 72 > max_len) {
|
|
|
|
/* 4 (header) + 8 (vendor) + 60 (model & serial) */
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
buf[idx++] = 0x02; /* Ascii */
|
|
|
|
buf[idx++] = 0x01; /* T10 Vendor */
|
|
|
|
buf[idx++] = 0x00;
|
|
|
|
buf[idx++] = 68;
|
|
|
|
padstr8(buf + idx, 8, "ATA"); /* Generic T10 vendor */
|
|
|
|
idx += 8;
|
|
|
|
padstr8(buf + idx, 40, s->drive_model_str);
|
|
|
|
idx += 40;
|
|
|
|
padstr8(buf + idx, 20, s->drive_serial_str);
|
|
|
|
idx += 20;
|
|
|
|
|
|
|
|
/* Entry 3: WWN */
|
|
|
|
if (s->wwn && (idx + 12 <= max_len)) {
|
|
|
|
/* 4 byte header + 8 byte wwn */
|
|
|
|
buf[idx++] = 0x01; /* Binary */
|
|
|
|
buf[idx++] = 0x03; /* NAA */
|
|
|
|
buf[idx++] = 0x00;
|
|
|
|
buf[idx++] = 0x08;
|
|
|
|
stq_be_p(&buf[idx], s->wwn);
|
|
|
|
idx += 8;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* SPC-3, revision 23 sec. 6.4 */
|
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
|
|
|
ASC_INV_FIELD_IN_CMD_PACKET);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
preamble_len = 5;
|
|
|
|
size_idx = 4;
|
|
|
|
|
|
|
|
buf[0] = 0x05; /* CD-ROM */
|
|
|
|
buf[1] = 0x80; /* removable */
|
|
|
|
buf[2] = 0x00; /* ISO */
|
|
|
|
buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
|
|
|
|
/* buf[size_idx] set below. */
|
|
|
|
buf[5] = 0; /* reserved */
|
|
|
|
buf[6] = 0; /* reserved */
|
|
|
|
buf[7] = 0; /* reserved */
|
|
|
|
padstr8(buf + 8, 8, "QEMU");
|
|
|
|
padstr8(buf + 16, 16, "QEMU DVD-ROM");
|
|
|
|
padstr8(buf + 32, 4, s->version);
|
|
|
|
idx = 36;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
buf[size_idx] = idx - preamble_len;
|
|
|
|
ide_atapi_cmd_reply(s, idx, max_len);
|
2011-04-18 17:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_get_configuration(IDEState *s, uint8_t *buf)
|
|
|
|
{
|
|
|
|
uint32_t len;
|
|
|
|
uint8_t index = 0;
|
|
|
|
int max_len;
|
|
|
|
|
|
|
|
/* only feature 0 is supported */
|
|
|
|
if (buf[2] != 0 || buf[3] != 0) {
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
2011-04-18 17:55:08 +02:00
|
|
|
ASC_INV_FIELD_IN_CMD_PACKET);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: could result in alignment problems in some architectures */
|
|
|
|
max_len = ube16_to_cpu(buf + 7);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX: avoid overflow for io_buffer if max_len is bigger than
|
|
|
|
* the size of that buffer (dimensioned to max number of
|
|
|
|
* sectors to transfer at once)
|
|
|
|
*
|
|
|
|
* Only a problem if the feature/profiles grow.
|
|
|
|
*/
|
|
|
|
if (max_len > 512) {
|
|
|
|
/* XXX: assume 1 sector */
|
|
|
|
max_len = 512;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf, 0, max_len);
|
|
|
|
/*
|
|
|
|
* the number of sectors from the media tells us which profile
|
|
|
|
* to use as current. 0 means there is no media
|
|
|
|
*/
|
|
|
|
if (media_is_dvd(s)) {
|
|
|
|
cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
|
|
|
|
} else if (media_is_cd(s)) {
|
|
|
|
cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[10] = 0x02 | 0x01; /* persistent and current */
|
|
|
|
len = 12; /* headers: 8 + 4 */
|
|
|
|
len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
|
|
|
|
len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
|
|
|
|
cpu_to_ube32(buf, len - 4); /* data length */
|
|
|
|
|
|
|
|
ide_atapi_cmd_reply(s, len, max_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_mode_sense(IDEState *s, uint8_t *buf)
|
|
|
|
{
|
|
|
|
int action, code;
|
|
|
|
int max_len;
|
|
|
|
|
2011-11-14 14:31:47 +01:00
|
|
|
max_len = ube16_to_cpu(buf + 7);
|
2011-04-18 17:55:08 +02:00
|
|
|
action = buf[2] >> 6;
|
|
|
|
code = buf[2] & 0x3f;
|
|
|
|
|
|
|
|
switch(action) {
|
|
|
|
case 0: /* current values */
|
|
|
|
switch(code) {
|
2011-09-13 14:41:56 +02:00
|
|
|
case MODE_PAGE_R_W_ERROR: /* error recovery */
|
2011-11-14 14:31:47 +01:00
|
|
|
cpu_to_ube16(&buf[0], 16 - 2);
|
2011-04-18 17:55:08 +02:00
|
|
|
buf[2] = 0x70;
|
|
|
|
buf[3] = 0;
|
|
|
|
buf[4] = 0;
|
|
|
|
buf[5] = 0;
|
|
|
|
buf[6] = 0;
|
|
|
|
buf[7] = 0;
|
|
|
|
|
2011-10-17 16:34:59 +02:00
|
|
|
buf[8] = MODE_PAGE_R_W_ERROR;
|
|
|
|
buf[9] = 16 - 10;
|
2011-04-18 17:55:08 +02:00
|
|
|
buf[10] = 0x00;
|
|
|
|
buf[11] = 0x05;
|
|
|
|
buf[12] = 0x00;
|
|
|
|
buf[13] = 0x00;
|
|
|
|
buf[14] = 0x00;
|
|
|
|
buf[15] = 0x00;
|
|
|
|
ide_atapi_cmd_reply(s, 16, max_len);
|
|
|
|
break;
|
2011-09-13 14:41:56 +02:00
|
|
|
case MODE_PAGE_AUDIO_CTL:
|
2011-11-14 14:31:47 +01:00
|
|
|
cpu_to_ube16(&buf[0], 24 - 2);
|
2011-04-18 17:55:08 +02:00
|
|
|
buf[2] = 0x70;
|
|
|
|
buf[3] = 0;
|
|
|
|
buf[4] = 0;
|
|
|
|
buf[5] = 0;
|
|
|
|
buf[6] = 0;
|
|
|
|
buf[7] = 0;
|
|
|
|
|
2011-10-17 16:34:59 +02:00
|
|
|
buf[8] = MODE_PAGE_AUDIO_CTL;
|
|
|
|
buf[9] = 24 - 10;
|
2011-04-18 17:55:08 +02:00
|
|
|
/* Fill with CDROM audio volume */
|
|
|
|
buf[17] = 0;
|
|
|
|
buf[19] = 0;
|
|
|
|
buf[21] = 0;
|
|
|
|
buf[23] = 0;
|
|
|
|
|
|
|
|
ide_atapi_cmd_reply(s, 24, max_len);
|
|
|
|
break;
|
2011-09-13 14:41:56 +02:00
|
|
|
case MODE_PAGE_CAPABILITIES:
|
2011-11-14 14:31:47 +01:00
|
|
|
cpu_to_ube16(&buf[0], 30 - 2);
|
2011-04-18 17:55:08 +02:00
|
|
|
buf[2] = 0x70;
|
|
|
|
buf[3] = 0;
|
|
|
|
buf[4] = 0;
|
|
|
|
buf[5] = 0;
|
|
|
|
buf[6] = 0;
|
|
|
|
buf[7] = 0;
|
|
|
|
|
2011-10-17 16:34:59 +02:00
|
|
|
buf[8] = MODE_PAGE_CAPABILITIES;
|
2011-11-14 14:31:47 +01:00
|
|
|
buf[9] = 30 - 10;
|
2011-09-13 15:08:22 +02:00
|
|
|
buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
|
2011-04-18 17:55:08 +02:00
|
|
|
buf[11] = 0x00;
|
|
|
|
|
|
|
|
/* Claim PLAY_AUDIO capability (0x01) since some Linux
|
|
|
|
code checks for this to automount media. */
|
|
|
|
buf[12] = 0x71;
|
|
|
|
buf[13] = 3 << 5;
|
|
|
|
buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
|
2011-09-06 18:58:43 +02:00
|
|
|
if (s->tray_locked) {
|
2011-09-13 15:08:22 +02:00
|
|
|
buf[14] |= 1 << 1;
|
2011-09-06 18:58:43 +02:00
|
|
|
}
|
2011-09-13 15:08:22 +02:00
|
|
|
buf[15] = 0x00; /* No volume & mute control, no changer */
|
|
|
|
cpu_to_ube16(&buf[16], 704); /* 4x read speed */
|
|
|
|
buf[18] = 0; /* Two volume levels */
|
2011-04-18 17:55:08 +02:00
|
|
|
buf[19] = 2;
|
2011-09-13 15:08:22 +02:00
|
|
|
cpu_to_ube16(&buf[20], 512); /* 512k buffer */
|
|
|
|
cpu_to_ube16(&buf[22], 704); /* 4x read speed current */
|
2011-04-18 17:55:08 +02:00
|
|
|
buf[24] = 0;
|
|
|
|
buf[25] = 0;
|
|
|
|
buf[26] = 0;
|
|
|
|
buf[27] = 0;
|
2011-11-14 14:31:47 +01:00
|
|
|
buf[28] = 0;
|
|
|
|
buf[29] = 0;
|
|
|
|
ide_atapi_cmd_reply(s, 30, max_len);
|
2011-04-18 17:55:08 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto error_cmd;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: /* changeable values */
|
|
|
|
goto error_cmd;
|
|
|
|
case 2: /* default values */
|
|
|
|
goto error_cmd;
|
|
|
|
default:
|
|
|
|
case 3: /* saved values */
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
2011-04-18 17:55:08 +02:00
|
|
|
ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
error_cmd:
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
|
2011-04-18 17:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
|
|
|
|
{
|
2011-04-19 13:15:52 +02:00
|
|
|
/* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
|
|
|
|
* come here, we know that it's ready. */
|
|
|
|
ide_atapi_cmd_ok(s);
|
2011-04-18 17:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
2011-09-06 18:58:43 +02:00
|
|
|
s->tray_locked = buf[4] & 1;
|
2014-10-07 13:59:18 +02:00
|
|
|
blk_lock_medium(s->blk, buf[4] & 1);
|
2011-04-18 17:55:08 +02:00
|
|
|
ide_atapi_cmd_ok(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_read(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
|
|
|
int nb_sectors, lba;
|
|
|
|
|
|
|
|
if (buf[0] == GPCMD_READ_10) {
|
|
|
|
nb_sectors = ube16_to_cpu(buf + 7);
|
|
|
|
} else {
|
|
|
|
nb_sectors = ube32_to_cpu(buf + 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
lba = ube32_to_cpu(buf + 2);
|
|
|
|
if (nb_sectors == 0) {
|
|
|
|
ide_atapi_cmd_ok(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_read_cd(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
|
|
|
int nb_sectors, lba, transfer_request;
|
|
|
|
|
|
|
|
nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
|
|
|
|
lba = ube32_to_cpu(buf + 2);
|
|
|
|
|
|
|
|
if (nb_sectors == 0) {
|
|
|
|
ide_atapi_cmd_ok(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:15:54 +01:00
|
|
|
transfer_request = buf[9] & 0xf8;
|
|
|
|
if (transfer_request == 0x00) {
|
2011-04-18 17:55:08 +02:00
|
|
|
/* nothing */
|
|
|
|
ide_atapi_cmd_ok(s);
|
2016-11-14 17:15:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check validity of BCL before transferring data */
|
|
|
|
if (!validate_bcl(s)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (transfer_request) {
|
2011-04-18 17:55:08 +02:00
|
|
|
case 0x10:
|
|
|
|
/* normal read */
|
|
|
|
ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
|
|
|
|
break;
|
|
|
|
case 0xf8:
|
|
|
|
/* read all data */
|
|
|
|
ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
|
|
|
|
break;
|
|
|
|
default:
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
2011-04-18 17:55:08 +02:00
|
|
|
ASC_INV_FIELD_IN_CMD_PACKET);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_seek(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
|
|
|
unsigned int lba;
|
2011-04-19 13:13:44 +02:00
|
|
|
uint64_t total_sectors = s->nb_sectors >> 2;
|
2011-04-18 17:55:08 +02:00
|
|
|
|
|
|
|
lba = ube32_to_cpu(buf + 2);
|
|
|
|
if (lba >= total_sectors) {
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
|
2011-04-18 17:55:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ide_atapi_cmd_ok(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
2011-09-06 18:58:45 +02:00
|
|
|
int sense;
|
2011-09-06 18:58:37 +02:00
|
|
|
bool start = buf[4] & 1;
|
|
|
|
bool loej = buf[4] & 2; /* load on start, eject on !start */
|
2012-07-31 03:28:26 +02:00
|
|
|
int pwrcnd = buf[4] & 0xf0;
|
|
|
|
|
|
|
|
if (pwrcnd) {
|
|
|
|
/* eject/load only happens for power condition == 0 */
|
2015-07-27 05:42:53 +02:00
|
|
|
ide_atapi_cmd_ok(s);
|
2012-07-31 03:28:26 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-04-18 17:55:08 +02:00
|
|
|
|
2011-09-06 18:58:37 +02:00
|
|
|
if (loej) {
|
2011-09-06 18:58:48 +02:00
|
|
|
if (!start && !s->tray_open && s->tray_locked) {
|
2014-10-07 13:59:18 +02:00
|
|
|
sense = blk_is_inserted(s->blk)
|
2011-09-13 14:41:56 +02:00
|
|
|
? NOT_READY : ILLEGAL_REQUEST;
|
2011-09-06 18:58:45 +02:00
|
|
|
ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
|
|
|
|
return;
|
2011-04-18 17:55:08 +02:00
|
|
|
}
|
2012-02-17 14:05:21 +01:00
|
|
|
|
|
|
|
if (s->tray_open != !start) {
|
2014-10-07 13:59:18 +02:00
|
|
|
blk_eject(s->blk, !start);
|
2012-02-17 14:05:21 +01:00
|
|
|
s->tray_open = !start;
|
|
|
|
}
|
2011-09-06 18:58:38 +02:00
|
|
|
}
|
2011-09-06 18:58:45 +02:00
|
|
|
|
|
|
|
ide_atapi_cmd_ok(s);
|
2011-04-18 17:55:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
|
|
|
int max_len = ube16_to_cpu(buf + 8);
|
|
|
|
|
|
|
|
cpu_to_ube16(buf, 0);
|
|
|
|
/* no current LBA */
|
|
|
|
buf[2] = 0;
|
|
|
|
buf[3] = 0;
|
|
|
|
buf[4] = 0;
|
|
|
|
buf[5] = 1;
|
|
|
|
cpu_to_ube16(buf + 6, 0);
|
|
|
|
ide_atapi_cmd_reply(s, 8, max_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
|
|
|
int format, msf, start_track, len;
|
|
|
|
int max_len;
|
2011-04-19 13:15:52 +02:00
|
|
|
uint64_t total_sectors = s->nb_sectors >> 2;
|
2011-04-18 17:55:08 +02:00
|
|
|
|
|
|
|
max_len = ube16_to_cpu(buf + 7);
|
|
|
|
format = buf[9] >> 6;
|
|
|
|
msf = (buf[1] >> 1) & 1;
|
|
|
|
start_track = buf[6];
|
|
|
|
|
|
|
|
switch(format) {
|
|
|
|
case 0:
|
|
|
|
len = cdrom_read_toc(total_sectors, buf, msf, start_track);
|
|
|
|
if (len < 0)
|
|
|
|
goto error_cmd;
|
|
|
|
ide_atapi_cmd_reply(s, len, max_len);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* multi session : only a single session defined */
|
|
|
|
memset(buf, 0, 12);
|
|
|
|
buf[1] = 0x0a;
|
|
|
|
buf[2] = 0x01;
|
|
|
|
buf[3] = 0x01;
|
|
|
|
ide_atapi_cmd_reply(s, 12, max_len);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
|
|
|
|
if (len < 0)
|
|
|
|
goto error_cmd;
|
|
|
|
ide_atapi_cmd_reply(s, len, max_len);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error_cmd:
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
2011-04-18 17:55:08 +02:00
|
|
|
ASC_INV_FIELD_IN_CMD_PACKET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
2011-04-19 13:13:44 +02:00
|
|
|
uint64_t total_sectors = s->nb_sectors >> 2;
|
2011-04-18 17:55:08 +02:00
|
|
|
|
|
|
|
/* NOTE: it is really the number of sectors minus 1 */
|
|
|
|
cpu_to_ube32(buf, total_sectors - 1);
|
|
|
|
cpu_to_ube32(buf + 4, 2048);
|
|
|
|
ide_atapi_cmd_reply(s, 8, 8);
|
|
|
|
}
|
|
|
|
|
2012-05-25 12:59:31 +02:00
|
|
|
static void cmd_read_disc_information(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
|
|
|
uint8_t type = buf[1] & 7;
|
|
|
|
uint32_t max_len = ube16_to_cpu(buf + 7);
|
|
|
|
|
|
|
|
/* Types 1/2 are only defined for Blu-Ray. */
|
|
|
|
if (type != 0) {
|
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
|
|
|
ASC_INV_FIELD_IN_CMD_PACKET);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf, 0, 34);
|
|
|
|
buf[1] = 32;
|
|
|
|
buf[2] = 0xe; /* last session complete, disc finalized */
|
|
|
|
buf[3] = 1; /* first track on disc */
|
|
|
|
buf[4] = 1; /* # of sessions */
|
|
|
|
buf[5] = 1; /* first track of last session */
|
|
|
|
buf[6] = 1; /* last track of last session */
|
|
|
|
buf[7] = 0x20; /* unrestricted use */
|
|
|
|
buf[8] = 0x00; /* CD-ROM or DVD-ROM */
|
|
|
|
/* 9-10-11: most significant byte corresponding bytes 4-5-6 */
|
|
|
|
/* 12-23: not meaningful for CD-ROM or DVD-ROM */
|
|
|
|
/* 24-31: disc bar code */
|
|
|
|
/* 32: disc application code */
|
|
|
|
/* 33: number of OPC tables */
|
|
|
|
|
|
|
|
ide_atapi_cmd_reply(s, 34, max_len);
|
|
|
|
}
|
|
|
|
|
2011-04-18 17:55:08 +02:00
|
|
|
static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
|
|
|
int max_len;
|
|
|
|
int media = buf[1];
|
|
|
|
int format = buf[7];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
max_len = ube16_to_cpu(buf + 8);
|
|
|
|
|
|
|
|
if (format < 0xff) {
|
|
|
|
if (media_is_cd(s)) {
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
2011-04-18 17:55:08 +02:00
|
|
|
ASC_INCOMPATIBLE_FORMAT);
|
|
|
|
return;
|
|
|
|
} else if (!media_present(s)) {
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
2011-04-18 17:55:08 +02:00
|
|
|
ASC_INV_FIELD_IN_CMD_PACKET);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
|
|
|
|
IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case 0x00 ... 0x7f:
|
|
|
|
case 0xff:
|
|
|
|
if (media == 0) {
|
|
|
|
ret = ide_dvd_read_structure(s, format, buf, buf);
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
|
2011-04-18 17:55:08 +02:00
|
|
|
} else {
|
|
|
|
ide_atapi_cmd_reply(s, ret, max_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* TODO: BD support, fall through for now */
|
|
|
|
|
|
|
|
/* Generic disk structures */
|
|
|
|
case 0x80: /* TODO: AACS volume identifier */
|
|
|
|
case 0x81: /* TODO: AACS media serial number */
|
|
|
|
case 0x82: /* TODO: AACS media identifier */
|
|
|
|
case 0x83: /* TODO: AACS media key block */
|
|
|
|
case 0x90: /* TODO: List of recognized format layers */
|
|
|
|
case 0xc0: /* TODO: Write protection status */
|
|
|
|
default:
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
|
2011-04-18 17:55:08 +02:00
|
|
|
ASC_INV_FIELD_IN_CMD_PACKET);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_set_speed(IDEState *s, uint8_t* buf)
|
|
|
|
{
|
|
|
|
ide_atapi_cmd_ok(s);
|
|
|
|
}
|
|
|
|
|
2011-04-18 17:55:08 +02:00
|
|
|
enum {
|
|
|
|
/*
|
|
|
|
* Only commands flagged as ALLOW_UA are allowed to run under a
|
|
|
|
* unit attention condition. (See MMC-5, section 4.1.6.1)
|
|
|
|
*/
|
|
|
|
ALLOW_UA = 0x01,
|
2011-04-19 13:15:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Commands flagged with CHECK_READY can only execute if a medium is present.
|
|
|
|
* Otherwise they report the Not Ready Condition. (See MMC-5, section
|
|
|
|
* 4.1.8)
|
|
|
|
*/
|
|
|
|
CHECK_READY = 0x02,
|
2015-09-17 20:17:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Commands flagged with NONDATA do not in any circumstances return
|
|
|
|
* any data via ide_atapi_cmd_reply. These commands are exempt from
|
|
|
|
* the normal byte_count_limit constraints.
|
|
|
|
* See ATA8-ACS3 "7.21.5 Byte Count Limit"
|
|
|
|
*/
|
|
|
|
NONDATA = 0x04,
|
2016-11-14 17:15:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CONDDATA implies a command that transfers data only conditionally based
|
|
|
|
* on the presence of suboptions. It should be exempt from the BCL check at
|
|
|
|
* command validation time, but it needs to be checked at the command
|
|
|
|
* handler level instead.
|
|
|
|
*/
|
|
|
|
CONDDATA = 0x08,
|
2011-04-18 17:55:08 +02:00
|
|
|
};
|
|
|
|
|
2015-11-13 20:31:42 +01:00
|
|
|
static const struct AtapiCmd {
|
2011-04-18 17:55:08 +02:00
|
|
|
void (*handler)(IDEState *s, uint8_t *buf);
|
|
|
|
int flags;
|
|
|
|
} atapi_cmd_table[0x100] = {
|
2015-09-17 20:17:05 +02:00
|
|
|
[ 0x00 ] = { cmd_test_unit_ready, CHECK_READY | NONDATA },
|
2011-04-18 17:55:08 +02:00
|
|
|
[ 0x03 ] = { cmd_request_sense, ALLOW_UA },
|
|
|
|
[ 0x12 ] = { cmd_inquiry, ALLOW_UA },
|
2015-09-17 20:17:05 +02:00
|
|
|
[ 0x1b ] = { cmd_start_stop_unit, NONDATA }, /* [1] */
|
|
|
|
[ 0x1e ] = { cmd_prevent_allow_medium_removal, NONDATA },
|
2011-04-19 13:15:52 +02:00
|
|
|
[ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY },
|
2011-09-06 18:58:41 +02:00
|
|
|
[ 0x28 ] = { cmd_read, /* (10) */ CHECK_READY },
|
2015-09-17 20:17:05 +02:00
|
|
|
[ 0x2b ] = { cmd_seek, CHECK_READY | NONDATA },
|
2011-04-19 13:15:52 +02:00
|
|
|
[ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY },
|
2011-04-18 17:55:08 +02:00
|
|
|
[ 0x46 ] = { cmd_get_configuration, ALLOW_UA },
|
|
|
|
[ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
|
2012-05-25 12:59:31 +02:00
|
|
|
[ 0x51 ] = { cmd_read_disc_information, CHECK_READY },
|
2011-04-18 17:55:08 +02:00
|
|
|
[ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 },
|
2011-09-06 18:58:41 +02:00
|
|
|
[ 0xa8 ] = { cmd_read, /* (12) */ CHECK_READY },
|
|
|
|
[ 0xad ] = { cmd_read_dvd_structure, CHECK_READY },
|
2015-09-17 20:17:05 +02:00
|
|
|
[ 0xbb ] = { cmd_set_speed, NONDATA },
|
2011-04-18 17:55:08 +02:00
|
|
|
[ 0xbd ] = { cmd_mechanism_status, 0 },
|
2016-11-14 17:15:54 +01:00
|
|
|
[ 0xbe ] = { cmd_read_cd, CHECK_READY | CONDDATA },
|
2011-09-06 18:58:41 +02:00
|
|
|
/* [1] handler detects and reports not ready condition itself */
|
2011-04-18 17:55:08 +02:00
|
|
|
};
|
|
|
|
|
2011-04-18 16:45:49 +02:00
|
|
|
void ide_atapi_cmd(IDEState *s)
|
|
|
|
{
|
2015-11-13 20:31:42 +01:00
|
|
|
uint8_t *buf = s->io_buffer;
|
|
|
|
const struct AtapiCmd *cmd = &atapi_cmd_table[s->io_buffer[0]];
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG_IDE_ATAPI
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
|
|
|
|
for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
|
2011-04-28 15:34:39 +02:00
|
|
|
printf(" %02x", buf[i]);
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
2015-11-13 20:31:42 +01:00
|
|
|
|
2011-04-18 16:45:49 +02:00
|
|
|
/*
|
2011-04-18 17:55:08 +02:00
|
|
|
* If there's a UNIT_ATTENTION condition pending, only command flagged with
|
|
|
|
* ALLOW_UA are allowed to complete. with other commands getting a CHECK
|
|
|
|
* condition response unless a higher priority status, defined by the drive
|
2011-04-18 16:45:49 +02:00
|
|
|
* here, is pending.
|
|
|
|
*/
|
2015-11-13 20:31:42 +01:00
|
|
|
if (s->sense_key == UNIT_ATTENTION && !(cmd->flags & ALLOW_UA)) {
|
2011-04-18 16:45:49 +02:00
|
|
|
ide_atapi_cmd_check_status(s);
|
|
|
|
return;
|
|
|
|
}
|
2011-04-28 16:34:41 +02:00
|
|
|
/*
|
|
|
|
* When a CD gets changed, we have to report an ejected state and
|
|
|
|
* then a loaded state to guests so that they detect tray
|
|
|
|
* open/close and media change events. Guests that do not use
|
|
|
|
* GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
|
|
|
|
* states rely on this behavior.
|
|
|
|
*/
|
2015-11-13 20:31:42 +01:00
|
|
|
if (!(cmd->flags & ALLOW_UA) &&
|
2014-10-07 13:59:18 +02:00
|
|
|
!s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) {
|
2012-11-26 16:37:39 +01:00
|
|
|
|
|
|
|
if (s->cdrom_changed == 1) {
|
|
|
|
ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
|
|
|
|
s->cdrom_changed = 2;
|
|
|
|
} else {
|
|
|
|
ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED);
|
|
|
|
s->cdrom_changed = 0;
|
|
|
|
}
|
2011-04-18 16:45:49 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2011-04-18 17:55:08 +02:00
|
|
|
|
2011-04-19 13:15:52 +02:00
|
|
|
/* Report a Not Ready condition if appropriate for the command */
|
2015-11-13 20:31:42 +01:00
|
|
|
if ((cmd->flags & CHECK_READY) &&
|
2014-10-07 13:59:18 +02:00
|
|
|
(!media_present(s) || !blk_is_inserted(s->blk)))
|
2011-04-19 13:15:52 +02:00
|
|
|
{
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
|
2011-04-19 13:15:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:15:54 +01:00
|
|
|
/* Commands that don't transfer DATA permit the byte_count_limit to be 0.
|
2015-09-17 20:17:05 +02:00
|
|
|
* If this is a data-transferring PIO command and BCL is 0,
|
|
|
|
* we abort at the /ATA/ level, not the ATAPI level.
|
|
|
|
* See ATA8 ACS3 section 7.17.6.49 and 7.21.5 */
|
2016-11-14 17:15:54 +01:00
|
|
|
if (cmd->handler && !(cmd->flags & (NONDATA | CONDDATA))) {
|
|
|
|
if (!validate_bcl(s)) {
|
2015-09-17 20:17:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-18 17:55:08 +02:00
|
|
|
/* Execute the command */
|
2015-11-13 20:31:42 +01:00
|
|
|
if (cmd->handler) {
|
|
|
|
cmd->handler(s, buf);
|
2011-04-18 17:55:08 +02:00
|
|
|
return;
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|
2011-04-18 17:55:08 +02:00
|
|
|
|
2011-09-13 14:41:56 +02:00
|
|
|
ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);
|
2011-04-18 16:45:49 +02:00
|
|
|
}
|