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