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