]> Git Repo - qemu.git/blame - hw/ide/atapi.c
ide/atapi: Factor commands out
[qemu.git] / hw / ide / atapi.c
CommitLineData
33231e0e
KW
1/*
2 * QEMU ATAPI Emulation
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26#include "hw/ide/internal.h"
27#include "hw/scsi.h"
28
29static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret);
30
31static void padstr8(uint8_t *buf, int buf_size, const char *src)
32{
33 int i;
34 for(i = 0; i < buf_size; i++) {
35 if (*src)
36 buf[i] = *src++;
37 else
38 buf[i] = ' ';
39 }
40}
41
42static inline void cpu_to_ube16(uint8_t *buf, int val)
43{
44 buf[0] = val >> 8;
45 buf[1] = val & 0xff;
46}
47
48static inline void cpu_to_ube32(uint8_t *buf, unsigned int val)
49{
50 buf[0] = val >> 24;
51 buf[1] = val >> 16;
52 buf[2] = val >> 8;
53 buf[3] = val & 0xff;
54}
55
56static inline int ube16_to_cpu(const uint8_t *buf)
57{
58 return (buf[0] << 8) | buf[1];
59}
60
61static inline int ube32_to_cpu(const uint8_t *buf)
62{
63 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
64}
65
66static void lba_to_msf(uint8_t *buf, int lba)
67{
68 lba += 150;
69 buf[0] = (lba / 75) / 60;
70 buf[1] = (lba / 75) % 60;
71 buf[2] = lba % 75;
72}
73
74/* XXX: DVDs that could fit on a CD will be reported as a CD */
75static inline int media_present(IDEState *s)
76{
77 return (s->nb_sectors > 0);
78}
79
80static inline int media_is_dvd(IDEState *s)
81{
82 return (media_present(s) && s->nb_sectors > CD_MAX_SECTORS);
83}
84
85static inline int media_is_cd(IDEState *s)
86{
87 return (media_present(s) && s->nb_sectors <= CD_MAX_SECTORS);
88}
89
90static void cd_data_to_raw(uint8_t *buf, int lba)
91{
92 /* sync bytes */
93 buf[0] = 0x00;
94 memset(buf + 1, 0xff, 10);
95 buf[11] = 0x00;
96 buf += 12;
97 /* MSF */
98 lba_to_msf(buf, lba);
99 buf[3] = 0x01; /* mode 1 data */
100 buf += 4;
101 /* data */
102 buf += 2048;
103 /* XXX: ECC not computed */
104 memset(buf, 0, 288);
105}
106
107static int cd_read_sector(BlockDriverState *bs, int lba, uint8_t *buf,
108 int sector_size)
109{
110 int ret;
111
112 switch(sector_size) {
113 case 2048:
114 ret = bdrv_read(bs, (int64_t)lba << 2, buf, 4);
115 break;
116 case 2352:
117 ret = bdrv_read(bs, (int64_t)lba << 2, buf + 16, 4);
118 if (ret < 0)
119 return ret;
120 cd_data_to_raw(buf, lba);
121 break;
122 default:
123 ret = -EIO;
124 break;
125 }
126 return ret;
127}
128
129void ide_atapi_cmd_ok(IDEState *s)
130{
131 s->error = 0;
132 s->status = READY_STAT | SEEK_STAT;
133 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
134 ide_set_irq(s->bus);
135}
136
137void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
138{
139#ifdef DEBUG_IDE_ATAPI
140 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
141#endif
142 s->error = sense_key << 4;
143 s->status = READY_STAT | ERR_STAT;
144 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
145 s->sense_key = sense_key;
146 s->asc = asc;
147 ide_set_irq(s->bus);
148}
149
150void ide_atapi_io_error(IDEState *s, int ret)
151{
152 /* XXX: handle more errors */
153 if (ret == -ENOMEDIUM) {
154 ide_atapi_cmd_error(s, SENSE_NOT_READY,
155 ASC_MEDIUM_NOT_PRESENT);
156 } else {
157 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
158 ASC_LOGICAL_BLOCK_OOR);
159 }
160}
161
162/* The whole ATAPI transfer logic is handled in this function */
163void ide_atapi_cmd_reply_end(IDEState *s)
164{
165 int byte_count_limit, size, ret;
166#ifdef DEBUG_IDE_ATAPI
167 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
168 s->packet_transfer_size,
169 s->elementary_transfer_size,
170 s->io_buffer_index);
171#endif
172 if (s->packet_transfer_size <= 0) {
173 /* end of transfer */
174 ide_transfer_stop(s);
175 s->status = READY_STAT | SEEK_STAT;
176 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
177 ide_set_irq(s->bus);
178#ifdef DEBUG_IDE_ATAPI
179 printf("status=0x%x\n", s->status);
180#endif
181 } else {
182 /* see if a new sector must be read */
183 if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
184 ret = cd_read_sector(s->bs, s->lba, s->io_buffer, s->cd_sector_size);
185 if (ret < 0) {
186 ide_transfer_stop(s);
187 ide_atapi_io_error(s, ret);
188 return;
189 }
190 s->lba++;
191 s->io_buffer_index = 0;
192 }
193 if (s->elementary_transfer_size > 0) {
194 /* there are some data left to transmit in this elementary
195 transfer */
196 size = s->cd_sector_size - s->io_buffer_index;
197 if (size > s->elementary_transfer_size)
198 size = s->elementary_transfer_size;
199 s->packet_transfer_size -= size;
200 s->elementary_transfer_size -= size;
201 s->io_buffer_index += size;
202 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
203 size, ide_atapi_cmd_reply_end);
204 } else {
205 /* a new transfer is needed */
206 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
207 byte_count_limit = s->lcyl | (s->hcyl << 8);
208#ifdef DEBUG_IDE_ATAPI
209 printf("byte_count_limit=%d\n", byte_count_limit);
210#endif
211 if (byte_count_limit == 0xffff)
212 byte_count_limit--;
213 size = s->packet_transfer_size;
214 if (size > byte_count_limit) {
215 /* byte count limit must be even if this case */
216 if (byte_count_limit & 1)
217 byte_count_limit--;
218 size = byte_count_limit;
219 }
220 s->lcyl = size;
221 s->hcyl = size >> 8;
222 s->elementary_transfer_size = size;
223 /* we cannot transmit more than one sector at a time */
224 if (s->lba != -1) {
225 if (size > (s->cd_sector_size - s->io_buffer_index))
226 size = (s->cd_sector_size - s->io_buffer_index);
227 }
228 s->packet_transfer_size -= size;
229 s->elementary_transfer_size -= size;
230 s->io_buffer_index += size;
231 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
232 size, ide_atapi_cmd_reply_end);
233 ide_set_irq(s->bus);
234#ifdef DEBUG_IDE_ATAPI
235 printf("status=0x%x\n", s->status);
236#endif
237 }
238 }
239}
240
241/* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
242static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
243{
244 if (size > max_size)
245 size = max_size;
246 s->lba = -1; /* no sector read */
247 s->packet_transfer_size = size;
248 s->io_buffer_size = size; /* dma: send the reply data as one chunk */
249 s->elementary_transfer_size = 0;
250 s->io_buffer_index = 0;
251
252 if (s->atapi_dma) {
253 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
254 s->bus->dma->ops->start_dma(s->bus->dma, s,
255 ide_atapi_cmd_read_dma_cb);
256 } else {
257 s->status = READY_STAT | SEEK_STAT;
258 ide_atapi_cmd_reply_end(s);
259 }
260}
261
262/* start a CD-CDROM read command */
263static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
264 int sector_size)
265{
266 s->lba = lba;
267 s->packet_transfer_size = nb_sectors * sector_size;
268 s->elementary_transfer_size = 0;
269 s->io_buffer_index = sector_size;
270 s->cd_sector_size = sector_size;
271
272 s->status = READY_STAT | SEEK_STAT;
273 ide_atapi_cmd_reply_end(s);
274}
275
276static void ide_atapi_cmd_check_status(IDEState *s)
277{
278#ifdef DEBUG_IDE_ATAPI
279 printf("atapi_cmd_check_status\n");
280#endif
281 s->error = MC_ERR | (SENSE_UNIT_ATTENTION << 4);
282 s->status = ERR_STAT;
283 s->nsector = 0;
284 ide_set_irq(s->bus);
285}
286/* ATAPI DMA support */
287
288/* XXX: handle read errors */
289static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
290{
291 IDEState *s = opaque;
292 int data_offset, n;
293
294 if (ret < 0) {
295 ide_atapi_io_error(s, ret);
296 goto eot;
297 }
298
299 if (s->io_buffer_size > 0) {
300 /*
301 * For a cdrom read sector command (s->lba != -1),
302 * adjust the lba for the next s->io_buffer_size chunk
303 * and dma the current chunk.
304 * For a command != read (s->lba == -1), just transfer
305 * the reply data.
306 */
307 if (s->lba != -1) {
308 if (s->cd_sector_size == 2352) {
309 n = 1;
310 cd_data_to_raw(s->io_buffer, s->lba);
311 } else {
312 n = s->io_buffer_size >> 11;
313 }
314 s->lba += n;
315 }
316 s->packet_transfer_size -= s->io_buffer_size;
317 if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
318 goto eot;
319 }
320
321 if (s->packet_transfer_size <= 0) {
322 s->status = READY_STAT | SEEK_STAT;
323 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
324 ide_set_irq(s->bus);
325 eot:
326 s->bus->dma->ops->add_status(s->bus->dma, BM_STATUS_INT);
327 ide_set_inactive(s);
328 return;
329 }
330
331 s->io_buffer_index = 0;
332 if (s->cd_sector_size == 2352) {
333 n = 1;
334 s->io_buffer_size = s->cd_sector_size;
335 data_offset = 16;
336 } else {
337 n = s->packet_transfer_size >> 11;
338 if (n > (IDE_DMA_BUF_SECTORS / 4))
339 n = (IDE_DMA_BUF_SECTORS / 4);
340 s->io_buffer_size = n * 2048;
341 data_offset = 0;
342 }
343#ifdef DEBUG_AIO
344 printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
345#endif
346 s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
347 s->bus->dma->iov.iov_len = n * 4 * 512;
348 qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
349 s->bus->dma->aiocb = bdrv_aio_readv(s->bs, (int64_t)s->lba << 2,
350 &s->bus->dma->qiov, n * 4,
351 ide_atapi_cmd_read_dma_cb, s);
352 if (!s->bus->dma->aiocb) {
353 /* Note: media not present is the most likely case */
354 ide_atapi_cmd_error(s, SENSE_NOT_READY,
355 ASC_MEDIUM_NOT_PRESENT);
356 goto eot;
357 }
358}
359
360/* start a CD-CDROM read command with DMA */
361/* XXX: test if DMA is available */
362static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
363 int sector_size)
364{
365 s->lba = lba;
366 s->packet_transfer_size = nb_sectors * sector_size;
367 s->io_buffer_index = 0;
368 s->io_buffer_size = 0;
369 s->cd_sector_size = sector_size;
370
371 /* XXX: check if BUSY_STAT should be set */
372 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
373 s->bus->dma->ops->start_dma(s->bus->dma, s,
374 ide_atapi_cmd_read_dma_cb);
375}
376
377static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
378 int sector_size)
379{
380#ifdef DEBUG_IDE_ATAPI
381 printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
382 lba, nb_sectors);
383#endif
384 if (s->atapi_dma) {
385 ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
386 } else {
387 ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
388 }
389}
390
391static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
392 uint16_t profile)
393{
394 uint8_t *buf_profile = buf + 12; /* start of profiles */
395
396 buf_profile += ((*index) * 4); /* start of indexed profile */
397 cpu_to_ube16 (buf_profile, profile);
398 buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
399
400 /* each profile adds 4 bytes to the response */
401 (*index)++;
402 buf[11] += 4; /* Additional Length */
403
404 return 4;
405}
406
407static int ide_dvd_read_structure(IDEState *s, int format,
408 const uint8_t *packet, uint8_t *buf)
409{
410 switch (format) {
411 case 0x0: /* Physical format information */
412 {
413 int layer = packet[6];
414 uint64_t total_sectors;
415
416 if (layer != 0)
417 return -ASC_INV_FIELD_IN_CMD_PACKET;
418
419 bdrv_get_geometry(s->bs, &total_sectors);
420 total_sectors >>= 2;
421 if (total_sectors == 0)
422 return -ASC_MEDIUM_NOT_PRESENT;
423
424 buf[4] = 1; /* DVD-ROM, part version 1 */
425 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
426 buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
427 buf[7] = 0; /* default densities */
428
429 /* FIXME: 0x30000 per spec? */
430 cpu_to_ube32(buf + 8, 0); /* start sector */
431 cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
432 cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
433
434 /* Size of buffer, not including 2 byte size field */
435 cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
436
437 /* 2k data + 4 byte header */
438 return (2048 + 4);
439 }
440
441 case 0x01: /* DVD copyright information */
442 buf[4] = 0; /* no copyright data */
443 buf[5] = 0; /* no region restrictions */
444
445 /* Size of buffer, not including 2 byte size field */
446 cpu_to_be16wu((uint16_t *)buf, 4 + 2);
447
448 /* 4 byte header + 4 byte data */
449 return (4 + 4);
450
451 case 0x03: /* BCA information - invalid field for no BCA info */
452 return -ASC_INV_FIELD_IN_CMD_PACKET;
453
454 case 0x04: /* DVD disc manufacturing information */
455 /* Size of buffer, not including 2 byte size field */
456 cpu_to_be16wu((uint16_t *)buf, 2048 + 2);
457
458 /* 2k data + 4 byte header */
459 return (2048 + 4);
460
461 case 0xff:
462 /*
463 * This lists all the command capabilities above. Add new ones
464 * in order and update the length and buffer return values.
465 */
466
467 buf[4] = 0x00; /* Physical format */
468 buf[5] = 0x40; /* Not writable, is readable */
469 cpu_to_be16wu((uint16_t *)(buf + 6), 2048 + 4);
470
471 buf[8] = 0x01; /* Copyright info */
472 buf[9] = 0x40; /* Not writable, is readable */
473 cpu_to_be16wu((uint16_t *)(buf + 10), 4 + 4);
474
475 buf[12] = 0x03; /* BCA info */
476 buf[13] = 0x40; /* Not writable, is readable */
477 cpu_to_be16wu((uint16_t *)(buf + 14), 188 + 4);
478
479 buf[16] = 0x04; /* Manufacturing info */
480 buf[17] = 0x40; /* Not writable, is readable */
481 cpu_to_be16wu((uint16_t *)(buf + 18), 2048 + 4);
482
483 /* Size of buffer, not including 2 byte size field */
484 cpu_to_be16wu((uint16_t *)buf, 16 + 2);
485
486 /* data written + 4 byte header */
487 return (16 + 4);
488
489 default: /* TODO: formats beyond DVD-ROM requires */
490 return -ASC_INV_FIELD_IN_CMD_PACKET;
491 }
492}
493
494static unsigned int event_status_media(IDEState *s,
495 uint8_t *buf)
496{
497 enum media_event_code {
498 MEC_NO_CHANGE = 0, /* Status unchanged */
499 MEC_EJECT_REQUESTED, /* received a request from user to eject */
500 MEC_NEW_MEDIA, /* new media inserted and ready for access */
501 MEC_MEDIA_REMOVAL, /* only for media changers */
502 MEC_MEDIA_CHANGED, /* only for media changers */
503 MEC_BG_FORMAT_COMPLETED, /* MRW or DVD+RW b/g format completed */
504 MEC_BG_FORMAT_RESTARTED, /* MRW or DVD+RW b/g format restarted */
505 };
506 enum media_status {
507 MS_TRAY_OPEN = 1,
508 MS_MEDIA_PRESENT = 2,
509 };
510 uint8_t event_code, media_status;
511
512 media_status = 0;
513 if (s->bs->tray_open) {
514 media_status = MS_TRAY_OPEN;
515 } else if (bdrv_is_inserted(s->bs)) {
516 media_status = MS_MEDIA_PRESENT;
517 }
518
519 /* Event notification descriptor */
520 event_code = MEC_NO_CHANGE;
521 if (media_status != MS_TRAY_OPEN && s->events.new_media) {
522 event_code = MEC_NEW_MEDIA;
523 s->events.new_media = false;
524 }
525
526 buf[4] = event_code;
527 buf[5] = media_status;
528
529 /* These fields are reserved, just clear them. */
530 buf[6] = 0;
531 buf[7] = 0;
532
533 return 8; /* We wrote to 4 extra bytes from the header */
534}
535
536static void handle_get_event_status_notification(IDEState *s,
537 uint8_t *buf,
538 const uint8_t *packet)
539{
540 struct {
541 uint8_t opcode;
542 uint8_t polled; /* lsb bit is polled; others are reserved */
543 uint8_t reserved2[2];
544 uint8_t class;
545 uint8_t reserved3[2];
546 uint16_t len;
547 uint8_t control;
548 } __attribute__((packed)) *gesn_cdb;
549
550 struct {
551 uint16_t len;
552 uint8_t notification_class;
553 uint8_t supported_events;
554 } __attribute((packed)) *gesn_event_header;
555
556 enum notification_class_request_type {
557 NCR_RESERVED1 = 1 << 0,
558 NCR_OPERATIONAL_CHANGE = 1 << 1,
559 NCR_POWER_MANAGEMENT = 1 << 2,
560 NCR_EXTERNAL_REQUEST = 1 << 3,
561 NCR_MEDIA = 1 << 4,
562 NCR_MULTI_HOST = 1 << 5,
563 NCR_DEVICE_BUSY = 1 << 6,
564 NCR_RESERVED2 = 1 << 7,
565 };
566 enum event_notification_class_field {
567 ENC_NO_EVENTS = 0,
568 ENC_OPERATIONAL_CHANGE,
569 ENC_POWER_MANAGEMENT,
570 ENC_EXTERNAL_REQUEST,
571 ENC_MEDIA,
572 ENC_MULTIPLE_HOSTS,
573 ENC_DEVICE_BUSY,
574 ENC_RESERVED,
575 };
576 unsigned int max_len, used_len;
577
578 gesn_cdb = (void *)packet;
579 gesn_event_header = (void *)buf;
580
581 max_len = be16_to_cpu(gesn_cdb->len);
582
583 /* It is fine by the MMC spec to not support async mode operations */
584 if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
585 /* Only polling is supported, asynchronous mode is not. */
586 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
587 ASC_INV_FIELD_IN_CMD_PACKET);
588 return;
589 }
590
591 /* polling mode operation */
592
593 /*
594 * These are the supported events.
595 *
596 * We currently only support requests of the 'media' type.
597 */
598 gesn_event_header->supported_events = NCR_MEDIA;
599
600 /*
601 * We use |= below to set the class field; other bits in this byte
602 * are reserved now but this is useful to do if we have to use the
603 * reserved fields later.
604 */
605 gesn_event_header->notification_class = 0;
606
607 /*
608 * Responses to requests are to be based on request priority. The
609 * notification_class_request_type enum above specifies the
610 * priority: upper elements are higher prio than lower ones.
611 */
612 if (gesn_cdb->class & NCR_MEDIA) {
613 gesn_event_header->notification_class |= ENC_MEDIA;
614 used_len = event_status_media(s, buf);
615 } else {
616 gesn_event_header->notification_class = 0x80; /* No event available */
617 used_len = sizeof(*gesn_event_header);
618 }
619 gesn_event_header->len = cpu_to_be16(used_len
620 - sizeof(*gesn_event_header));
621 ide_atapi_cmd_reply(s, used_len, max_len);
622}
623
a60cf7e7
KW
624static void cmd_request_sense(IDEState *s, uint8_t *buf)
625{
626 int max_len = buf[4];
627
628 memset(buf, 0, 18);
629 buf[0] = 0x70 | (1 << 7);
630 buf[2] = s->sense_key;
631 buf[7] = 10;
632 buf[12] = s->asc;
633
634 if (s->sense_key == SENSE_UNIT_ATTENTION) {
635 s->sense_key = SENSE_NONE;
636 }
637
638 ide_atapi_cmd_reply(s, 18, max_len);
639}
640
641static void cmd_inquiry(IDEState *s, uint8_t *buf)
642{
643 int max_len = buf[4];
644
645 buf[0] = 0x05; /* CD-ROM */
646 buf[1] = 0x80; /* removable */
647 buf[2] = 0x00; /* ISO */
648 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
649 buf[4] = 31; /* additional length */
650 buf[5] = 0; /* reserved */
651 buf[6] = 0; /* reserved */
652 buf[7] = 0; /* reserved */
653 padstr8(buf + 8, 8, "QEMU");
654 padstr8(buf + 16, 16, "QEMU DVD-ROM");
655 padstr8(buf + 32, 4, s->version);
656 ide_atapi_cmd_reply(s, 36, max_len);
657}
658
659static void cmd_get_configuration(IDEState *s, uint8_t *buf)
660{
661 uint32_t len;
662 uint8_t index = 0;
663 int max_len;
664
665 /* only feature 0 is supported */
666 if (buf[2] != 0 || buf[3] != 0) {
667 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
668 ASC_INV_FIELD_IN_CMD_PACKET);
669 return;
670 }
671
672 /* XXX: could result in alignment problems in some architectures */
673 max_len = ube16_to_cpu(buf + 7);
674
675 /*
676 * XXX: avoid overflow for io_buffer if max_len is bigger than
677 * the size of that buffer (dimensioned to max number of
678 * sectors to transfer at once)
679 *
680 * Only a problem if the feature/profiles grow.
681 */
682 if (max_len > 512) {
683 /* XXX: assume 1 sector */
684 max_len = 512;
685 }
686
687 memset(buf, 0, max_len);
688 /*
689 * the number of sectors from the media tells us which profile
690 * to use as current. 0 means there is no media
691 */
692 if (media_is_dvd(s)) {
693 cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
694 } else if (media_is_cd(s)) {
695 cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
696 }
697
698 buf[10] = 0x02 | 0x01; /* persistent and current */
699 len = 12; /* headers: 8 + 4 */
700 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
701 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
702 cpu_to_ube32(buf, len - 4); /* data length */
703
704 ide_atapi_cmd_reply(s, len, max_len);
705}
706
707static void cmd_mode_sense(IDEState *s, uint8_t *buf)
708{
709 int action, code;
710 int max_len;
711
712 if (buf[0] == GPCMD_MODE_SENSE_10) {
713 max_len = ube16_to_cpu(buf + 7);
714 } else {
715 max_len = buf[4];
716 }
717
718 action = buf[2] >> 6;
719 code = buf[2] & 0x3f;
720
721 switch(action) {
722 case 0: /* current values */
723 switch(code) {
724 case GPMODE_R_W_ERROR_PAGE: /* error recovery */
725 cpu_to_ube16(&buf[0], 16 + 6);
726 buf[2] = 0x70;
727 buf[3] = 0;
728 buf[4] = 0;
729 buf[5] = 0;
730 buf[6] = 0;
731 buf[7] = 0;
732
733 buf[8] = 0x01;
734 buf[9] = 0x06;
735 buf[10] = 0x00;
736 buf[11] = 0x05;
737 buf[12] = 0x00;
738 buf[13] = 0x00;
739 buf[14] = 0x00;
740 buf[15] = 0x00;
741 ide_atapi_cmd_reply(s, 16, max_len);
742 break;
743 case GPMODE_AUDIO_CTL_PAGE:
744 cpu_to_ube16(&buf[0], 24 + 6);
745 buf[2] = 0x70;
746 buf[3] = 0;
747 buf[4] = 0;
748 buf[5] = 0;
749 buf[6] = 0;
750 buf[7] = 0;
751
752 /* Fill with CDROM audio volume */
753 buf[17] = 0;
754 buf[19] = 0;
755 buf[21] = 0;
756 buf[23] = 0;
757
758 ide_atapi_cmd_reply(s, 24, max_len);
759 break;
760 case GPMODE_CAPABILITIES_PAGE:
761 cpu_to_ube16(&buf[0], 28 + 6);
762 buf[2] = 0x70;
763 buf[3] = 0;
764 buf[4] = 0;
765 buf[5] = 0;
766 buf[6] = 0;
767 buf[7] = 0;
768
769 buf[8] = 0x2a;
770 buf[9] = 0x12;
771 buf[10] = 0x00;
772 buf[11] = 0x00;
773
774 /* Claim PLAY_AUDIO capability (0x01) since some Linux
775 code checks for this to automount media. */
776 buf[12] = 0x71;
777 buf[13] = 3 << 5;
778 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
779 if (bdrv_is_locked(s->bs))
780 buf[6] |= 1 << 1;
781 buf[15] = 0x00;
782 cpu_to_ube16(&buf[16], 706);
783 buf[18] = 0;
784 buf[19] = 2;
785 cpu_to_ube16(&buf[20], 512);
786 cpu_to_ube16(&buf[22], 706);
787 buf[24] = 0;
788 buf[25] = 0;
789 buf[26] = 0;
790 buf[27] = 0;
791 ide_atapi_cmd_reply(s, 28, max_len);
792 break;
793 default:
794 goto error_cmd;
795 }
796 break;
797 case 1: /* changeable values */
798 goto error_cmd;
799 case 2: /* default values */
800 goto error_cmd;
801 default:
802 case 3: /* saved values */
803 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
804 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
805 break;
806 }
807 return;
808
809error_cmd:
810 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
811}
812
813static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
814{
815 if (bdrv_is_inserted(s->bs)) {
816 ide_atapi_cmd_ok(s);
817 } else {
818 ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
819 }
820}
821
822static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
823{
824 bdrv_set_locked(s->bs, buf[4] & 1);
825 ide_atapi_cmd_ok(s);
826}
827
828static void cmd_read(IDEState *s, uint8_t* buf)
829{
830 int nb_sectors, lba;
831
832 if (buf[0] == GPCMD_READ_10) {
833 nb_sectors = ube16_to_cpu(buf + 7);
834 } else {
835 nb_sectors = ube32_to_cpu(buf + 6);
836 }
837
838 lba = ube32_to_cpu(buf + 2);
839 if (nb_sectors == 0) {
840 ide_atapi_cmd_ok(s);
841 return;
842 }
843
844 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
845}
846
847static void cmd_read_cd(IDEState *s, uint8_t* buf)
848{
849 int nb_sectors, lba, transfer_request;
850
851 nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
852 lba = ube32_to_cpu(buf + 2);
853
854 if (nb_sectors == 0) {
855 ide_atapi_cmd_ok(s);
856 return;
857 }
858
859 transfer_request = buf[9];
860 switch(transfer_request & 0xf8) {
861 case 0x00:
862 /* nothing */
863 ide_atapi_cmd_ok(s);
864 break;
865 case 0x10:
866 /* normal read */
867 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
868 break;
869 case 0xf8:
870 /* read all data */
871 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
872 break;
873 default:
874 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
875 ASC_INV_FIELD_IN_CMD_PACKET);
876 break;
877 }
878}
879
880static void cmd_seek(IDEState *s, uint8_t* buf)
881{
882 unsigned int lba;
883 uint64_t total_sectors;
884
885 bdrv_get_geometry(s->bs, &total_sectors);
886
887 total_sectors >>= 2;
888 if (total_sectors == 0) {
889 ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
890 return;
891 }
892
893 lba = ube32_to_cpu(buf + 2);
894 if (lba >= total_sectors) {
895 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
896 return;
897 }
898
899 ide_atapi_cmd_ok(s);
900}
901
902static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
903{
904 int start, eject, sense, err = 0;
905 start = buf[4] & 1;
906 eject = (buf[4] >> 1) & 1;
907
908 if (eject) {
909 err = bdrv_eject(s->bs, !start);
910 }
911
912 switch (err) {
913 case 0:
914 ide_atapi_cmd_ok(s);
915 break;
916 case -EBUSY:
917 sense = SENSE_NOT_READY;
918 if (bdrv_is_inserted(s->bs)) {
919 sense = SENSE_ILLEGAL_REQUEST;
920 }
921 ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
922 break;
923 default:
924 ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
925 break;
926 }
927}
928
929static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
930{
931 int max_len = ube16_to_cpu(buf + 8);
932
933 cpu_to_ube16(buf, 0);
934 /* no current LBA */
935 buf[2] = 0;
936 buf[3] = 0;
937 buf[4] = 0;
938 buf[5] = 1;
939 cpu_to_ube16(buf + 6, 0);
940 ide_atapi_cmd_reply(s, 8, max_len);
941}
942
943static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
944{
945 int format, msf, start_track, len;
946 uint64_t total_sectors;
947 int max_len;
948
949 bdrv_get_geometry(s->bs, &total_sectors);
950
951 total_sectors >>= 2;
952 if (total_sectors == 0) {
953 ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
954 return;
955 }
956
957 max_len = ube16_to_cpu(buf + 7);
958 format = buf[9] >> 6;
959 msf = (buf[1] >> 1) & 1;
960 start_track = buf[6];
961
962 switch(format) {
963 case 0:
964 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
965 if (len < 0)
966 goto error_cmd;
967 ide_atapi_cmd_reply(s, len, max_len);
968 break;
969 case 1:
970 /* multi session : only a single session defined */
971 memset(buf, 0, 12);
972 buf[1] = 0x0a;
973 buf[2] = 0x01;
974 buf[3] = 0x01;
975 ide_atapi_cmd_reply(s, 12, max_len);
976 break;
977 case 2:
978 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
979 if (len < 0)
980 goto error_cmd;
981 ide_atapi_cmd_reply(s, len, max_len);
982 break;
983 default:
984 error_cmd:
985 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
986 ASC_INV_FIELD_IN_CMD_PACKET);
987 }
988}
989
990static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
991{
992 uint64_t total_sectors;
993
994 bdrv_get_geometry(s->bs, &total_sectors);
995
996 total_sectors >>= 2;
997 if (total_sectors == 0) {
998 ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
999 return;
1000 }
1001
1002 /* NOTE: it is really the number of sectors minus 1 */
1003 cpu_to_ube32(buf, total_sectors - 1);
1004 cpu_to_ube32(buf + 4, 2048);
1005 ide_atapi_cmd_reply(s, 8, 8);
1006}
1007
1008static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
1009{
1010 int max_len;
1011 int media = buf[1];
1012 int format = buf[7];
1013 int ret;
1014
1015 max_len = ube16_to_cpu(buf + 8);
1016
1017 if (format < 0xff) {
1018 if (media_is_cd(s)) {
1019 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1020 ASC_INCOMPATIBLE_FORMAT);
1021 return;
1022 } else if (!media_present(s)) {
1023 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1024 ASC_INV_FIELD_IN_CMD_PACKET);
1025 return;
1026 }
1027 }
1028
1029 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1030 IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1031
1032 switch (format) {
1033 case 0x00 ... 0x7f:
1034 case 0xff:
1035 if (media == 0) {
1036 ret = ide_dvd_read_structure(s, format, buf, buf);
1037
1038 if (ret < 0) {
1039 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST, -ret);
1040 } else {
1041 ide_atapi_cmd_reply(s, ret, max_len);
1042 }
1043
1044 break;
1045 }
1046 /* TODO: BD support, fall through for now */
1047
1048 /* Generic disk structures */
1049 case 0x80: /* TODO: AACS volume identifier */
1050 case 0x81: /* TODO: AACS media serial number */
1051 case 0x82: /* TODO: AACS media identifier */
1052 case 0x83: /* TODO: AACS media key block */
1053 case 0x90: /* TODO: List of recognized format layers */
1054 case 0xc0: /* TODO: Write protection status */
1055 default:
1056 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1057 ASC_INV_FIELD_IN_CMD_PACKET);
1058 break;
1059 }
1060}
1061
1062static void cmd_set_speed(IDEState *s, uint8_t* buf)
1063{
1064 ide_atapi_cmd_ok(s);
1065}
1066
33231e0e
KW
1067void ide_atapi_cmd(IDEState *s)
1068{
1069 const uint8_t *packet;
1070 uint8_t *buf;
33231e0e
KW
1071
1072 packet = s->io_buffer;
1073 buf = s->io_buffer;
1074#ifdef DEBUG_IDE_ATAPI
1075 {
1076 int i;
1077 printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1078 for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
1079 printf(" %02x", packet[i]);
1080 }
1081 printf("\n");
1082 }
1083#endif
1084 /*
1085 * If there's a UNIT_ATTENTION condition pending, only
1086 * REQUEST_SENSE, INQUIRY, GET_CONFIGURATION and
1087 * GET_EVENT_STATUS_NOTIFICATION commands are allowed to complete.
1088 * MMC-5, section 4.1.6.1 lists only these commands being allowed
1089 * to complete, with other commands getting a CHECK condition
1090 * response unless a higher priority status, defined by the drive
1091 * here, is pending.
1092 */
1093 if (s->sense_key == SENSE_UNIT_ATTENTION &&
1094 s->io_buffer[0] != GPCMD_REQUEST_SENSE &&
1095 s->io_buffer[0] != GPCMD_INQUIRY &&
1096 s->io_buffer[0] != GPCMD_GET_EVENT_STATUS_NOTIFICATION) {
1097 ide_atapi_cmd_check_status(s);
1098 return;
1099 }
1100 if (bdrv_is_inserted(s->bs) && s->cdrom_changed) {
1101 ide_atapi_cmd_error(s, SENSE_NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1102
1103 s->cdrom_changed = 0;
1104 s->sense_key = SENSE_UNIT_ATTENTION;
1105 s->asc = ASC_MEDIUM_MAY_HAVE_CHANGED;
1106 return;
1107 }
1108 switch(s->io_buffer[0]) {
1109 case GPCMD_TEST_UNIT_READY:
a60cf7e7 1110 cmd_test_unit_ready(s, buf);
33231e0e
KW
1111 break;
1112 case GPCMD_MODE_SENSE_6:
1113 case GPCMD_MODE_SENSE_10:
a60cf7e7 1114 cmd_mode_sense(s, buf);
33231e0e
KW
1115 break;
1116 case GPCMD_REQUEST_SENSE:
a60cf7e7 1117 cmd_request_sense(s, buf);
33231e0e
KW
1118 break;
1119 case GPCMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
a60cf7e7 1120 cmd_prevent_allow_medium_removal(s, buf);
33231e0e
KW
1121 break;
1122 case GPCMD_READ_10:
1123 case GPCMD_READ_12:
a60cf7e7 1124 cmd_read(s, buf);
33231e0e
KW
1125 break;
1126 case GPCMD_READ_CD:
a60cf7e7 1127 cmd_read_cd(s, buf);
33231e0e
KW
1128 break;
1129 case GPCMD_SEEK:
a60cf7e7 1130 cmd_seek(s, buf);
33231e0e
KW
1131 break;
1132 case GPCMD_START_STOP_UNIT:
a60cf7e7 1133 cmd_start_stop_unit(s, buf);
33231e0e
KW
1134 break;
1135 case GPCMD_MECHANISM_STATUS:
a60cf7e7 1136 cmd_mechanism_status(s, buf);
33231e0e
KW
1137 break;
1138 case GPCMD_READ_TOC_PMA_ATIP:
a60cf7e7 1139 cmd_read_toc_pma_atip(s, buf);
33231e0e
KW
1140 break;
1141 case GPCMD_READ_CDVD_CAPACITY:
a60cf7e7 1142 cmd_read_cdvd_capacity(s, buf);
33231e0e
KW
1143 break;
1144 case GPCMD_READ_DVD_STRUCTURE:
a60cf7e7 1145 cmd_read_dvd_structure(s, buf);
33231e0e
KW
1146 break;
1147 case GPCMD_SET_SPEED:
a60cf7e7 1148 cmd_set_speed(s, buf);
33231e0e
KW
1149 break;
1150 case GPCMD_INQUIRY:
a60cf7e7 1151 cmd_inquiry(s, buf);
33231e0e
KW
1152 break;
1153 case GPCMD_GET_CONFIGURATION:
a60cf7e7
KW
1154 cmd_get_configuration(s, buf);
1155 break;
33231e0e
KW
1156 case GPCMD_GET_EVENT_STATUS_NOTIFICATION:
1157 handle_get_event_status_notification(s, buf, packet);
1158 break;
1159 default:
1160 ide_atapi_cmd_error(s, SENSE_ILLEGAL_REQUEST,
1161 ASC_ILLEGAL_OPCODE);
1162 break;
1163 }
1164}
This page took 0.142105 seconds and 4 git commands to generate.