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