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