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