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