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