1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include "qemu/osdep.h"
27#include "hw/ide/internal.h"
28#include "hw/scsi/scsi.h"
29#include "sysemu/block-backend.h"
30
31#define ATAPI_SECTOR_BITS (2 + BDRV_SECTOR_BITS)
32#define ATAPI_SECTOR_SIZE (1 << ATAPI_SECTOR_BITS)
33
34static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
35
36static void padstr8(uint8_t *buf, int buf_size, const char *src)
37{
38 int i;
39 for(i = 0; i < buf_size; i++) {
40 if (*src)
41 buf[i] = *src++;
42 else
43 buf[i] = ' ';
44 }
45}
46
47static inline void cpu_to_ube16(uint8_t *buf, int val)
48{
49 buf[0] = val >> 8;
50 buf[1] = val & 0xff;
51}
52
53static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
54{
55 buf[0] = val >> 24;
56 buf[1] = val >> 16;
57 buf[2] = val >> 8;
58 buf[3] = val & 0xff;
59}
60
61static inline int ube16_to_cpu(const uint8_t *buf)
62{
63 return (buf[0] << 8) | buf[1];
64}
65
66static inline int ube32_to_cpu(const uint8_t *buf)
67{
68 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
69}
70
71static void lba_to_msf(uint8_t *buf, int lba)
72{
73 lba += 150;
74 buf[0] = (lba / 75) / 60;
75 buf[1] = (lba / 75) % 60;
76 buf[2] = lba % 75;
77}
78
79static inline int media_present(IDEState *s)
80{
81 return !s->tray_open && s->nb_sectors > 0;
82}
83
84
85static inline int media_is_dvd(IDEState *s)
86{
87 return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
88}
89
90static inline int media_is_cd(IDEState *s)
91{
92 return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
93}
94
95static void cd_data_to_raw(uint8_t *buf, int lba)
96{
97
98 buf[0] = 0x00;
99 memset(buf + 1, 0xff, 10);
100 buf[11] = 0x00;
101 buf += 12;
102
103 lba_to_msf(buf, lba);
104 buf[3] = 0x01;
105 buf += 4;
106
107 buf += 2048;
108
109 memset(buf, 0, 288);
110}
111
112static int
113cd_read_sector_sync(IDEState *s)
114{
115 int ret;
116 block_acct_start(blk_get_stats(s->blk), &s->acct,
117 ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
118
119#ifdef DEBUG_IDE_ATAPI
120 printf("cd_read_sector_sync: lba=%d\n", s->lba);
121#endif
122
123 switch (s->cd_sector_size) {
124 case 2048:
125 ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS,
126 s->io_buffer, ATAPI_SECTOR_SIZE);
127 break;
128 case 2352:
129 ret = blk_pread(s->blk, (int64_t)s->lba << ATAPI_SECTOR_BITS,
130 s->io_buffer + 16, ATAPI_SECTOR_SIZE);
131 if (ret >= 0) {
132 cd_data_to_raw(s->io_buffer, s->lba);
133 }
134 break;
135 default:
136 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
137 return -EIO;
138 }
139
140 if (ret < 0) {
141 block_acct_failed(blk_get_stats(s->blk), &s->acct);
142 } else {
143 block_acct_done(blk_get_stats(s->blk), &s->acct);
144 s->lba++;
145 s->io_buffer_index = 0;
146 }
147
148 return ret;
149}
150
151static void cd_read_sector_cb(void *opaque, int ret)
152{
153 IDEState *s = opaque;
154
155#ifdef DEBUG_IDE_ATAPI
156 printf("cd_read_sector_cb: lba=%d ret=%d\n", s->lba, ret);
157#endif
158
159 if (ret < 0) {
160 block_acct_failed(blk_get_stats(s->blk), &s->acct);
161 ide_atapi_io_error(s, ret);
162 return;
163 }
164
165 block_acct_done(blk_get_stats(s->blk), &s->acct);
166
167 if (s->cd_sector_size == 2352) {
168 cd_data_to_raw(s->io_buffer, s->lba);
169 }
170
171 s->lba++;
172 s->io_buffer_index = 0;
173 s->status &= ~BUSY_STAT;
174
175 ide_atapi_cmd_reply_end(s);
176}
177
178static int cd_read_sector(IDEState *s)
179{
180 if (s->cd_sector_size != 2048 && s->cd_sector_size != 2352) {
181 block_acct_invalid(blk_get_stats(s->blk), BLOCK_ACCT_READ);
182 return -EINVAL;
183 }
184
185 s->iov.iov_base = (s->cd_sector_size == 2352) ?
186 s->io_buffer + 16 : s->io_buffer;
187
188 s->iov.iov_len = ATAPI_SECTOR_SIZE;
189 qemu_iovec_init_external(&s->qiov, &s->iov, 1);
190
191#ifdef DEBUG_IDE_ATAPI
192 printf("cd_read_sector: lba=%d\n", s->lba);
193#endif
194
195 block_acct_start(blk_get_stats(s->blk), &s->acct,
196 ATAPI_SECTOR_SIZE, BLOCK_ACCT_READ);
197
198 ide_buffered_readv(s, (int64_t)s->lba << 2, &s->qiov, 4,
199 cd_read_sector_cb, s);
200
201 s->status |= BUSY_STAT;
202 return 0;
203}
204
205void ide_atapi_cmd_ok(IDEState *s)
206{
207 s->error = 0;
208 s->status = READY_STAT | SEEK_STAT;
209 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
210 ide_transfer_stop(s);
211 ide_set_irq(s->bus);
212}
213
214void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
215{
216#ifdef DEBUG_IDE_ATAPI
217 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
218#endif
219 s->error = sense_key << 4;
220 s->status = READY_STAT | ERR_STAT;
221 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
222 s->sense_key = sense_key;
223 s->asc = asc;
224 ide_transfer_stop(s);
225 ide_set_irq(s->bus);
226}
227
228void ide_atapi_io_error(IDEState *s, int ret)
229{
230
231 if (ret == -ENOMEDIUM) {
232 ide_atapi_cmd_error(s, NOT_READY,
233 ASC_MEDIUM_NOT_PRESENT);
234 } else {
235 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
236 ASC_LOGICAL_BLOCK_OOR);
237 }
238}
239
240static uint16_t atapi_byte_count_limit(IDEState *s)
241{
242 uint16_t bcl;
243
244 bcl = s->lcyl | (s->hcyl << 8);
245 if (bcl == 0xffff) {
246 return 0xfffe;
247 }
248 return bcl;
249}
250
251
252void ide_atapi_cmd_reply_end(IDEState *s)
253{
254 int byte_count_limit, size, ret;
255#ifdef DEBUG_IDE_ATAPI
256 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
257 s->packet_transfer_size,
258 s->elementary_transfer_size,
259 s->io_buffer_index);
260#endif
261 if (s->packet_transfer_size <= 0) {
262
263 ide_atapi_cmd_ok(s);
264 ide_set_irq(s->bus);
265#ifdef DEBUG_IDE_ATAPI
266 printf("end of transfer, status=0x%x\n", s->status);
267#endif
268 } else {
269
270 if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
271 if (!s->elementary_transfer_size) {
272 ret = cd_read_sector(s);
273 if (ret < 0) {
274 ide_atapi_io_error(s, ret);
275 }
276 return;
277 } else {
278
279
280
281 ret = cd_read_sector_sync(s);
282 if (ret < 0) {
283 ide_atapi_io_error(s, ret);
284 return;
285 }
286 }
287 }
288 if (s->elementary_transfer_size > 0) {
289
290
291 size = s->cd_sector_size - s->io_buffer_index;
292 if (size > s->elementary_transfer_size)
293 size = s->elementary_transfer_size;
294 s->packet_transfer_size -= size;
295 s->elementary_transfer_size -= size;
296 s->io_buffer_index += size;
297 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
298 size, ide_atapi_cmd_reply_end);
299 } else {
300
301 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
302 byte_count_limit = atapi_byte_count_limit(s);
303#ifdef DEBUG_IDE_ATAPI
304 printf("byte_count_limit=%d\n", byte_count_limit);
305#endif
306 size = s->packet_transfer_size;
307 if (size > byte_count_limit) {
308
309 if (byte_count_limit & 1)
310 byte_count_limit--;
311 size = byte_count_limit;
312 }
313 s->lcyl = size;
314 s->hcyl = size >> 8;
315 s->elementary_transfer_size = size;
316
317 if (s->lba != -1) {
318 if (size > (s->cd_sector_size - s->io_buffer_index))
319 size = (s->cd_sector_size - s->io_buffer_index);
320 }
321 s->packet_transfer_size -= size;
322 s->elementary_transfer_size -= size;
323 s->io_buffer_index += size;
324 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
325 size, ide_atapi_cmd_reply_end);
326 ide_set_irq(s->bus);
327#ifdef DEBUG_IDE_ATAPI
328 printf("status=0x%x\n", s->status);
329#endif
330 }
331 }
332}
333
334
335static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
336{
337 if (size > max_size)
338 size = max_size;
339 s->lba = -1;
340 s->packet_transfer_size = size;
341 s->io_buffer_size = size;
342 s->elementary_transfer_size = 0;
343
344 if (s->atapi_dma) {
345 block_acct_start(blk_get_stats(s->blk), &s->acct, size,
346 BLOCK_ACCT_READ);
347 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
348 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
349 } else {
350 s->status = READY_STAT | SEEK_STAT;
351 s->io_buffer_index = 0;
352 ide_atapi_cmd_reply_end(s);
353 }
354}
355
356
357static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
358 int sector_size)
359{
360 s->lba = lba;
361 s->packet_transfer_size = nb_sectors * sector_size;
362 s->elementary_transfer_size = 0;
363 s->io_buffer_index = sector_size;
364 s->cd_sector_size = sector_size;
365
366 ide_atapi_cmd_reply_end(s);
367}
368
369static void ide_atapi_cmd_check_status(IDEState *s)
370{
371#ifdef DEBUG_IDE_ATAPI
372 printf("atapi_cmd_check_status\n");
373#endif
374 s->error = MC_ERR | (UNIT_ATTENTION << 4);
375 s->status = ERR_STAT;
376 s->nsector = 0;
377 ide_set_irq(s->bus);
378}
379
380
381static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
382{
383 IDEState *s = opaque;
384 int data_offset, n;
385
386 if (ret < 0) {
387 if (ide_handle_rw_error(s, -ret, ide_dma_cmd_to_retry(s->dma_cmd))) {
388 if (s->bus->error_status) {
389 s->bus->dma->aiocb = NULL;
390 return;
391 }
392 goto eot;
393 }
394 }
395
396 if (s->io_buffer_size > 0) {
397
398
399
400
401
402
403
404 if (s->lba != -1) {
405 if (s->cd_sector_size == 2352) {
406 n = 1;
407 cd_data_to_raw(s->io_buffer, s->lba);
408 } else {
409 n = s->io_buffer_size >> 11;
410 }
411 s->lba += n;
412 }
413 s->packet_transfer_size -= s->io_buffer_size;
414 if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
415 goto eot;
416 }
417
418 if (s->packet_transfer_size <= 0) {
419 s->status = READY_STAT | SEEK_STAT;
420 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
421 ide_set_irq(s->bus);
422 goto eot;
423 }
424
425 s->io_buffer_index = 0;
426 if (s->cd_sector_size == 2352) {
427 n = 1;
428 s->io_buffer_size = s->cd_sector_size;
429 data_offset = 16;
430 } else {
431 n = s->packet_transfer_size >> 11;
432 if (n > (IDE_DMA_BUF_SECTORS / 4))
433 n = (IDE_DMA_BUF_SECTORS / 4);
434 s->io_buffer_size = n * 2048;
435 data_offset = 0;
436 }
437#ifdef DEBUG_AIO
438 printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
439#endif
440
441 s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
442 s->bus->dma->iov.iov_len = n * ATAPI_SECTOR_SIZE;
443 qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
444
445 s->bus->dma->aiocb = ide_buffered_readv(s, (int64_t)s->lba << 2,
446 &s->bus->dma->qiov, n * 4,
447 ide_atapi_cmd_read_dma_cb, s);
448 return;
449
450eot:
451 if (ret < 0) {
452 block_acct_failed(blk_get_stats(s->blk), &s->acct);
453 } else {
454 block_acct_done(blk_get_stats(s->blk), &s->acct);
455 }
456 ide_set_inactive(s, false);
457}
458
459
460
461static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
462 int sector_size)
463{
464 s->lba = lba;
465 s->packet_transfer_size = nb_sectors * sector_size;
466 s->io_buffer_size = 0;
467 s->cd_sector_size = sector_size;
468
469 block_acct_start(blk_get_stats(s->blk), &s->acct, s->packet_transfer_size,
470 BLOCK_ACCT_READ);
471
472
473 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
474 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
475}
476
477static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
478 int sector_size)
479{
480#ifdef DEBUG_IDE_ATAPI
481 printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
482 lba, nb_sectors);
483#endif
484 if (s->atapi_dma) {
485 ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
486 } else {
487 ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
488 }
489}
490
491void ide_atapi_dma_restart(IDEState *s)
492{
493
494
495
496
497
498 s->unit = s->bus->retry_unit;
499 s->bus->dma->ops->restart_dma(s->bus->dma);
500 ide_atapi_cmd(s);
501}
502
503static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
504 uint16_t profile)
505{
506 uint8_t *buf_profile = buf + 12;
507
508 buf_profile += ((*index) * 4);
509 cpu_to_ube16 (buf_profile, profile);
510 buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
511
512
513 (*index)++;
514 buf[11] += 4;
515
516 return 4;
517}
518
519static int ide_dvd_read_structure(IDEState *s, int format,
520 const uint8_t *packet, uint8_t *buf)
521{
522 switch (format) {
523 case 0x0:
524 {
525 int layer = packet[6];
526 uint64_t total_sectors;
527
528 if (layer != 0)
529 return -ASC_INV_FIELD_IN_CMD_PACKET;
530
531 total_sectors = s->nb_sectors >> 2;
532 if (total_sectors == 0) {
533 return -ASC_MEDIUM_NOT_PRESENT;
534 }
535
536 buf[4] = 1;
537 buf[5] = 0xf;
538 buf[6] = 1;
539 buf[7] = 0;
540
541
542 cpu_to_ube32(buf + 8, 0);
543 cpu_to_ube32(buf + 12, total_sectors - 1);
544 cpu_to_ube32(buf + 16, total_sectors - 1);
545
546
547 stw_be_p(buf, 2048 + 2);
548
549
550 return (2048 + 4);
551 }
552
553 case 0x01:
554 buf[4] = 0;
555 buf[5] = 0;
556
557
558 stw_be_p(buf, 4 + 2);
559
560
561 return (4 + 4);
562
563 case 0x03:
564 return -ASC_INV_FIELD_IN_CMD_PACKET;
565
566 case 0x04:
567
568 stw_be_p(buf, 2048 + 2);
569
570
571 return (2048 + 4);
572
573 case 0xff:
574
575
576
577
578
579 buf[4] = 0x00;
580 buf[5] = 0x40;
581 stw_be_p(buf + 6, 2048 + 4);
582
583 buf[8] = 0x01;
584 buf[9] = 0x40;
585 stw_be_p(buf + 10, 4 + 4);
586
587 buf[12] = 0x03;
588 buf[13] = 0x40;
589 stw_be_p(buf + 14, 188 + 4);
590
591 buf[16] = 0x04;
592 buf[17] = 0x40;
593 stw_be_p(buf + 18, 2048 + 4);
594
595
596 stw_be_p(buf, 16 + 2);
597
598
599 return (16 + 4);
600
601 default:
602 return -ASC_INV_FIELD_IN_CMD_PACKET;
603 }
604}
605
606static unsigned int event_status_media(IDEState *s,
607 uint8_t *buf)
608{
609 uint8_t event_code, media_status;
610
611 media_status = 0;
612 if (s->tray_open) {
613 media_status = MS_TRAY_OPEN;
614 } else if (blk_is_inserted(s->blk)) {
615 media_status = MS_MEDIA_PRESENT;
616 }
617
618
619 event_code = MEC_NO_CHANGE;
620 if (media_status != MS_TRAY_OPEN) {
621 if (s->events.new_media) {
622 event_code = MEC_NEW_MEDIA;
623 s->events.new_media = false;
624 } else if (s->events.eject_request) {
625 event_code = MEC_EJECT_REQUESTED;
626 s->events.eject_request = false;
627 }
628 }
629
630 buf[4] = event_code;
631 buf[5] = media_status;
632
633
634 buf[6] = 0;
635 buf[7] = 0;
636
637 return 8;
638}
639
640
641
642
643
644static bool validate_bcl(IDEState *s)
645{
646
647 if (s->atapi_dma || atapi_byte_count_limit(s)) {
648 return true;
649 }
650
651
652
653 ide_abort_command(s);
654 return false;
655}
656
657static void cmd_get_event_status_notification(IDEState *s,
658 uint8_t *buf)
659{
660 const uint8_t *packet = buf;
661
662 struct {
663 uint8_t opcode;
664 uint8_t polled;
665 uint8_t reserved2[2];
666 uint8_t class;
667 uint8_t reserved3[2];
668 uint16_t len;
669 uint8_t control;
670 } QEMU_PACKED *gesn_cdb;
671
672 struct {
673 uint16_t len;
674 uint8_t notification_class;
675 uint8_t supported_events;
676 } QEMU_PACKED *gesn_event_header;
677 unsigned int max_len, used_len;
678
679 gesn_cdb = (void *)packet;
680 gesn_event_header = (void *)buf;
681
682 max_len = be16_to_cpu(gesn_cdb->len);
683
684
685 if (!(gesn_cdb->polled & 0x01)) {
686
687 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
688 ASC_INV_FIELD_IN_CMD_PACKET);
689 return;
690 }
691
692
693
694
695
696
697
698
699
700
701
702 gesn_event_header->supported_events = 1 << GESN_MEDIA;
703
704
705
706
707
708
709 gesn_event_header->notification_class = 0;
710
711
712
713
714
715
716 if (gesn_cdb->class & (1 << GESN_MEDIA)) {
717 gesn_event_header->notification_class |= GESN_MEDIA;
718 used_len = event_status_media(s, buf);
719 } else {
720 gesn_event_header->notification_class = 0x80;
721 used_len = sizeof(*gesn_event_header);
722 }
723 gesn_event_header->len = cpu_to_be16(used_len
724 - sizeof(*gesn_event_header));
725 ide_atapi_cmd_reply(s, used_len, max_len);
726}
727
728static void cmd_request_sense(IDEState *s, uint8_t *buf)
729{
730 int max_len = buf[4];
731
732 memset(buf, 0, 18);
733 buf[0] = 0x70 | (1 << 7);
734 buf[2] = s->sense_key;
735 buf[7] = 10;
736 buf[12] = s->asc;
737
738 if (s->sense_key == UNIT_ATTENTION) {
739 s->sense_key = NO_SENSE;
740 }
741
742 ide_atapi_cmd_reply(s, 18, max_len);
743}
744
745static void cmd_inquiry(IDEState *s, uint8_t *buf)
746{
747 uint8_t page_code = buf[2];
748 int max_len = buf[4];
749
750 unsigned idx = 0;
751 unsigned size_idx;
752 unsigned preamble_len;
753
754
755
756 if (buf[1] & 0x01) {
757 preamble_len = 4;
758 size_idx = 3;
759
760 buf[idx++] = 0x05;
761 buf[idx++] = page_code;
762 buf[idx++] = 0x00;
763 idx++;
764
765 switch (page_code) {
766 case 0x00:
767
768 buf[idx++] = 0x00;
769 buf[idx++] = 0x83;
770 break;
771
772 case 0x83:
773
774
775
776
777
778 if (idx + 24 > max_len) {
779
780
781 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
782 ASC_DATA_PHASE_ERROR);
783 return;
784 }
785 buf[idx++] = 0x02;
786 buf[idx++] = 0x00;
787 buf[idx++] = 0x00;
788 buf[idx++] = 20;
789 padstr8(buf + idx, 20, s->drive_serial_str);
790 idx += 20;
791
792
793 if (idx + 72 > max_len) {
794
795 goto out;
796 }
797 buf[idx++] = 0x02;
798 buf[idx++] = 0x01;
799 buf[idx++] = 0x00;
800 buf[idx++] = 68;
801 padstr8(buf + idx, 8, "ATA");
802 idx += 8;
803 padstr8(buf + idx, 40, s->drive_model_str);
804 idx += 40;
805 padstr8(buf + idx, 20, s->drive_serial_str);
806 idx += 20;
807
808
809 if (s->wwn && (idx + 12 <= max_len)) {
810
811 buf[idx++] = 0x01;
812 buf[idx++] = 0x03;
813 buf[idx++] = 0x00;
814 buf[idx++] = 0x08;
815 stq_be_p(&buf[idx], s->wwn);
816 idx += 8;
817 }
818 break;
819
820 default:
821
822 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
823 ASC_INV_FIELD_IN_CMD_PACKET);
824 return;
825 }
826 } else {
827 preamble_len = 5;
828 size_idx = 4;
829
830 buf[0] = 0x05;
831 buf[1] = 0x80;
832 buf[2] = 0x00;
833 buf[3] = 0x21;
834
835 buf[5] = 0;
836 buf[6] = 0;
837 buf[7] = 0;
838 padstr8(buf + 8, 8, "QEMU");
839 padstr8(buf + 16, 16, "QEMU DVD-ROM");
840 padstr8(buf + 32, 4, s->version);
841 idx = 36;
842 }
843
844 out:
845 buf[size_idx] = idx - preamble_len;
846 ide_atapi_cmd_reply(s, idx, max_len);
847}
848
849static void cmd_get_configuration(IDEState *s, uint8_t *buf)
850{
851 uint32_t len;
852 uint8_t index = 0;
853 int max_len;
854
855
856 if (buf[2] != 0 || buf[3] != 0) {
857 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
858 ASC_INV_FIELD_IN_CMD_PACKET);
859 return;
860 }
861
862
863 max_len = ube16_to_cpu(buf + 7);
864
865
866
867
868
869
870
871
872 if (max_len > 512) {
873
874 max_len = 512;
875 }
876
877 memset(buf, 0, max_len);
878
879
880
881
882 if (media_is_dvd(s)) {
883 cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
884 } else if (media_is_cd(s)) {
885 cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
886 }
887
888 buf[10] = 0x02 | 0x01;
889 len = 12;
890 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
891 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
892 cpu_to_ube32(buf, len - 4);
893
894 ide_atapi_cmd_reply(s, len, max_len);
895}
896
897static void cmd_mode_sense(IDEState *s, uint8_t *buf)
898{
899 int action, code;
900 int max_len;
901
902 max_len = ube16_to_cpu(buf + 7);
903 action = buf[2] >> 6;
904 code = buf[2] & 0x3f;
905
906 switch(action) {
907 case 0:
908 switch(code) {
909 case MODE_PAGE_R_W_ERROR:
910 cpu_to_ube16(&buf[0], 16 - 2);
911 buf[2] = 0x70;
912 buf[3] = 0;
913 buf[4] = 0;
914 buf[5] = 0;
915 buf[6] = 0;
916 buf[7] = 0;
917
918 buf[8] = MODE_PAGE_R_W_ERROR;
919 buf[9] = 16 - 10;
920 buf[10] = 0x00;
921 buf[11] = 0x05;
922 buf[12] = 0x00;
923 buf[13] = 0x00;
924 buf[14] = 0x00;
925 buf[15] = 0x00;
926 ide_atapi_cmd_reply(s, 16, max_len);
927 break;
928 case MODE_PAGE_AUDIO_CTL:
929 cpu_to_ube16(&buf[0], 24 - 2);
930 buf[2] = 0x70;
931 buf[3] = 0;
932 buf[4] = 0;
933 buf[5] = 0;
934 buf[6] = 0;
935 buf[7] = 0;
936
937 buf[8] = MODE_PAGE_AUDIO_CTL;
938 buf[9] = 24 - 10;
939
940 buf[17] = 0;
941 buf[19] = 0;
942 buf[21] = 0;
943 buf[23] = 0;
944
945 ide_atapi_cmd_reply(s, 24, max_len);
946 break;
947 case MODE_PAGE_CAPABILITIES:
948 cpu_to_ube16(&buf[0], 30 - 2);
949 buf[2] = 0x70;
950 buf[3] = 0;
951 buf[4] = 0;
952 buf[5] = 0;
953 buf[6] = 0;
954 buf[7] = 0;
955
956 buf[8] = MODE_PAGE_CAPABILITIES;
957 buf[9] = 30 - 10;
958 buf[10] = 0x3b;
959 buf[11] = 0x00;
960
961
962
963 buf[12] = 0x71;
964 buf[13] = 3 << 5;
965 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
966 if (s->tray_locked) {
967 buf[14] |= 1 << 1;
968 }
969 buf[15] = 0x00;
970 cpu_to_ube16(&buf[16], 704);
971 buf[18] = 0;
972 buf[19] = 2;
973 cpu_to_ube16(&buf[20], 512);
974 cpu_to_ube16(&buf[22], 704);
975 buf[24] = 0;
976 buf[25] = 0;
977 buf[26] = 0;
978 buf[27] = 0;
979 buf[28] = 0;
980 buf[29] = 0;
981 ide_atapi_cmd_reply(s, 30, max_len);
982 break;
983 default:
984 goto error_cmd;
985 }
986 break;
987 case 1:
988 goto error_cmd;
989 case 2:
990 goto error_cmd;
991 default:
992 case 3:
993 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
994 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
995 break;
996 }
997 return;
998
999error_cmd:
1000 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
1001}
1002
1003static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
1004{
1005
1006
1007 ide_atapi_cmd_ok(s);
1008}
1009
1010static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
1011{
1012 s->tray_locked = buf[4] & 1;
1013 blk_lock_medium(s->blk, buf[4] & 1);
1014 ide_atapi_cmd_ok(s);
1015}
1016
1017static void cmd_read(IDEState *s, uint8_t* buf)
1018{
1019 int nb_sectors, lba;
1020
1021 if (buf[0] == GPCMD_READ_10) {
1022 nb_sectors = ube16_to_cpu(buf + 7);
1023 } else {
1024 nb_sectors = ube32_to_cpu(buf + 6);
1025 }
1026
1027 lba = ube32_to_cpu(buf + 2);
1028 if (nb_sectors == 0) {
1029 ide_atapi_cmd_ok(s);
1030 return;
1031 }
1032
1033 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1034}
1035
1036static void cmd_read_cd(IDEState *s, uint8_t* buf)
1037{
1038 int nb_sectors, lba, transfer_request;
1039
1040 nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
1041 lba = ube32_to_cpu(buf + 2);
1042
1043 if (nb_sectors == 0) {
1044 ide_atapi_cmd_ok(s);
1045 return;
1046 }
1047
1048 transfer_request = buf[9] & 0xf8;
1049 if (transfer_request == 0x00) {
1050
1051 ide_atapi_cmd_ok(s);
1052 return;
1053 }
1054
1055
1056 if (!validate_bcl(s)) {
1057 return;
1058 }
1059
1060 switch (transfer_request) {
1061 case 0x10:
1062
1063 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
1064 break;
1065 case 0xf8:
1066
1067 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
1068 break;
1069 default:
1070 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1071 ASC_INV_FIELD_IN_CMD_PACKET);
1072 break;
1073 }
1074}
1075
1076static void cmd_seek(IDEState *s, uint8_t* buf)
1077{
1078 unsigned int lba;
1079 uint64_t total_sectors = s->nb_sectors >> 2;
1080
1081 lba = ube32_to_cpu(buf + 2);
1082 if (lba >= total_sectors) {
1083 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
1084 return;
1085 }
1086
1087 ide_atapi_cmd_ok(s);
1088}
1089
1090static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
1091{
1092 int sense;
1093 bool start = buf[4] & 1;
1094 bool loej = buf[4] & 2;
1095 int pwrcnd = buf[4] & 0xf0;
1096
1097 if (pwrcnd) {
1098
1099 ide_atapi_cmd_ok(s);
1100 return;
1101 }
1102
1103 if (loej) {
1104 if (!start && !s->tray_open && s->tray_locked) {
1105 sense = blk_is_inserted(s->blk)
1106 ? NOT_READY : ILLEGAL_REQUEST;
1107 ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
1108 return;
1109 }
1110
1111 if (s->tray_open != !start) {
1112 blk_eject(s->blk, !start);
1113 s->tray_open = !start;
1114 }
1115 }
1116
1117 ide_atapi_cmd_ok(s);
1118}
1119
1120static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
1121{
1122 int max_len = ube16_to_cpu(buf + 8);
1123
1124 cpu_to_ube16(buf, 0);
1125
1126 buf[2] = 0;
1127 buf[3] = 0;
1128 buf[4] = 0;
1129 buf[5] = 1;
1130 cpu_to_ube16(buf + 6, 0);
1131 ide_atapi_cmd_reply(s, 8, max_len);
1132}
1133
1134static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
1135{
1136 int format, msf, start_track, len;
1137 int max_len;
1138 uint64_t total_sectors = s->nb_sectors >> 2;
1139
1140 max_len = ube16_to_cpu(buf + 7);
1141 format = buf[9] >> 6;
1142 msf = (buf[1] >> 1) & 1;
1143 start_track = buf[6];
1144
1145 switch(format) {
1146 case 0:
1147 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
1148 if (len < 0)
1149 goto error_cmd;
1150 ide_atapi_cmd_reply(s, len, max_len);
1151 break;
1152 case 1:
1153
1154 memset(buf, 0, 12);
1155 buf[1] = 0x0a;
1156 buf[2] = 0x01;
1157 buf[3] = 0x01;
1158 ide_atapi_cmd_reply(s, 12, max_len);
1159 break;
1160 case 2:
1161 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
1162 if (len < 0)
1163 goto error_cmd;
1164 ide_atapi_cmd_reply(s, len, max_len);
1165 break;
1166 default:
1167 error_cmd:
1168 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1169 ASC_INV_FIELD_IN_CMD_PACKET);
1170 }
1171}
1172
1173static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
1174{
1175 uint64_t total_sectors = s->nb_sectors >> 2;
1176
1177
1178 cpu_to_ube32(buf, total_sectors - 1);
1179 cpu_to_ube32(buf + 4, 2048);
1180 ide_atapi_cmd_reply(s, 8, 8);
1181}
1182
1183static void cmd_read_disc_information(IDEState *s, uint8_t* buf)
1184{
1185 uint8_t type = buf[1] & 7;
1186 uint32_t max_len = ube16_to_cpu(buf + 7);
1187
1188
1189 if (type != 0) {
1190 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1191 ASC_INV_FIELD_IN_CMD_PACKET);
1192 return;
1193 }
1194
1195 memset(buf, 0, 34);
1196 buf[1] = 32;
1197 buf[2] = 0xe;
1198 buf[3] = 1;
1199 buf[4] = 1;
1200 buf[5] = 1;
1201 buf[6] = 1;
1202 buf[7] = 0x20;
1203 buf[8] = 0x00;
1204
1205
1206
1207
1208
1209
1210 ide_atapi_cmd_reply(s, 34, max_len);
1211}
1212
1213static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
1214{
1215 int max_len;
1216 int media = buf[1];
1217 int format = buf[7];
1218 int ret;
1219
1220 max_len = ube16_to_cpu(buf + 8);
1221
1222 if (format < 0xff) {
1223 if (media_is_cd(s)) {
1224 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1225 ASC_INCOMPATIBLE_FORMAT);
1226 return;
1227 } else if (!media_present(s)) {
1228 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1229 ASC_INV_FIELD_IN_CMD_PACKET);
1230 return;
1231 }
1232 }
1233
1234 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1235 IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1236
1237 switch (format) {
1238 case 0x00 ... 0x7f:
1239 case 0xff:
1240 if (media == 0) {
1241 ret = ide_dvd_read_structure(s, format, buf, buf);
1242
1243 if (ret < 0) {
1244 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
1245 } else {
1246 ide_atapi_cmd_reply(s, ret, max_len);
1247 }
1248
1249 break;
1250 }
1251
1252
1253
1254 case 0x80:
1255 case 0x81:
1256 case 0x82:
1257 case 0x83:
1258 case 0x90:
1259 case 0xc0:
1260 default:
1261 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
1262 ASC_INV_FIELD_IN_CMD_PACKET);
1263 break;
1264 }
1265}
1266
1267static void cmd_set_speed(IDEState *s, uint8_t* buf)
1268{
1269 ide_atapi_cmd_ok(s);
1270}
1271
1272enum {
1273
1274
1275
1276
1277 ALLOW_UA = 0x01,
1278
1279
1280
1281
1282
1283
1284 CHECK_READY = 0x02,
1285
1286
1287
1288
1289
1290
1291
1292 NONDATA = 0x04,
1293
1294
1295
1296
1297
1298
1299
1300 CONDDATA = 0x08,
1301};
1302
1303static const struct AtapiCmd {
1304 void (*handler)(IDEState *s, uint8_t *buf);
1305 int flags;
1306} atapi_cmd_table[0x100] = {
1307 [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY | NONDATA },
1308 [ 0x03 ] = { cmd_request_sense, ALLOW_UA },
1309 [ 0x12 ] = { cmd_inquiry, ALLOW_UA },
1310 [ 0x1b ] = { cmd_start_stop_unit, NONDATA },
1311 [ 0x1e ] = { cmd_prevent_allow_medium_removal, NONDATA },
1312 [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY },
1313 [ 0x28 ] = { cmd_read, CHECK_READY },
1314 [ 0x2b ] = { cmd_seek, CHECK_READY | NONDATA },
1315 [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY },
1316 [ 0x46 ] = { cmd_get_configuration, ALLOW_UA },
1317 [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
1318 [ 0x51 ] = { cmd_read_disc_information, CHECK_READY },
1319 [ 0x5a ] = { cmd_mode_sense, 0 },
1320 [ 0xa8 ] = { cmd_read, CHECK_READY },
1321 [ 0xad ] = { cmd_read_dvd_structure, CHECK_READY },
1322 [ 0xbb ] = { cmd_set_speed, NONDATA },
1323 [ 0xbd ] = { cmd_mechanism_status, 0 },
1324 [ 0xbe ] = { cmd_read_cd, CHECK_READY | CONDDATA },
1325
1326};
1327
1328void ide_atapi_cmd(IDEState *s)
1329{
1330 uint8_t *buf = s->io_buffer;
1331 const struct AtapiCmd *cmd = &atapi_cmd_table[s->io_buffer[0]];
1332
1333#ifdef DEBUG_IDE_ATAPI
1334 {
1335 int i;
1336 printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1337 for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
1338 printf(" %02x", buf[i]);
1339 }
1340 printf("\n");
1341 }
1342#endif
1343
1344
1345
1346
1347
1348
1349
1350 if (s->sense_key == UNIT_ATTENTION && !(cmd->flags & ALLOW_UA)) {
1351 ide_atapi_cmd_check_status(s);
1352 return;
1353 }
1354
1355
1356
1357
1358
1359
1360
1361 if (!(cmd->flags & ALLOW_UA) &&
1362 !s->tray_open && blk_is_inserted(s->blk) && s->cdrom_changed) {
1363
1364 if (s->cdrom_changed == 1) {
1365 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1366 s->cdrom_changed = 2;
1367 } else {
1368 ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED);
1369 s->cdrom_changed = 0;
1370 }
1371
1372 return;
1373 }
1374
1375
1376 if ((cmd->flags & CHECK_READY) &&
1377 (!media_present(s) || !blk_is_inserted(s->blk)))
1378 {
1379 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1380 return;
1381 }
1382
1383
1384
1385
1386
1387 if (cmd->handler && !(cmd->flags & (NONDATA | CONDDATA))) {
1388 if (!validate_bcl(s)) {
1389 return;
1390 }
1391 }
1392
1393
1394 if (cmd->handler) {
1395 cmd->handler(s, buf);
1396 return;
1397 }
1398
1399 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);
1400}
1401