]> Git Repo - qemu.git/blame - hw/ide/atapi.c
block/vhdx.c: Mark parent_vhdx_guid variable as unused
[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"
0d09e41a 27#include "hw/scsi/scsi.h"
33231e0e
KW
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
33231e0e
KW
74static inline int media_present(IDEState *s)
75{
a1aff5bf 76 return !s->tray_open && s->nb_sectors > 0;
33231e0e
KW
77}
78
a7acf552 79/* XXX: DVDs that could fit on a CD will be reported as a CD */
33231e0e
KW
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
a597e79c 107static int cd_read_sector(IDEState *s, int lba, uint8_t *buf, int sector_size)
33231e0e
KW
108{
109 int ret;
110
111 switch(sector_size) {
112 case 2048:
5366d0c8
BC
113 block_acct_start(bdrv_get_stats(s->bs), &s->acct,
114 4 * BDRV_SECTOR_SIZE, BLOCK_ACCT_READ);
a597e79c 115 ret = bdrv_read(s->bs, (int64_t)lba << 2, buf, 4);
5366d0c8 116 block_acct_done(bdrv_get_stats(s->bs), &s->acct);
33231e0e
KW
117 break;
118 case 2352:
5366d0c8
BC
119 block_acct_start(bdrv_get_stats(s->bs), &s->acct,
120 4 * BDRV_SECTOR_SIZE, BLOCK_ACCT_READ);
a597e79c 121 ret = bdrv_read(s->bs, (int64_t)lba << 2, buf + 16, 4);
5366d0c8 122 block_acct_done(bdrv_get_stats(s->bs), &s->acct);
33231e0e
KW
123 if (ret < 0)
124 return ret;
125 cd_data_to_raw(buf, lba);
126 break;
127 default:
128 ret = -EIO;
129 break;
130 }
131 return ret;
132}
133
134void ide_atapi_cmd_ok(IDEState *s)
135{
136 s->error = 0;
137 s->status = READY_STAT | SEEK_STAT;
138 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
139 ide_set_irq(s->bus);
140}
141
142void ide_atapi_cmd_error(IDEState *s, int sense_key, int asc)
143{
144#ifdef DEBUG_IDE_ATAPI
145 printf("atapi_cmd_error: sense=0x%x asc=0x%x\n", sense_key, asc);
146#endif
147 s->error = sense_key << 4;
148 s->status = READY_STAT | ERR_STAT;
149 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
150 s->sense_key = sense_key;
151 s->asc = asc;
152 ide_set_irq(s->bus);
153}
154
155void ide_atapi_io_error(IDEState *s, int ret)
156{
157 /* XXX: handle more errors */
158 if (ret == -ENOMEDIUM) {
67cc61e4 159 ide_atapi_cmd_error(s, NOT_READY,
33231e0e
KW
160 ASC_MEDIUM_NOT_PRESENT);
161 } else {
67cc61e4 162 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
33231e0e
KW
163 ASC_LOGICAL_BLOCK_OOR);
164 }
165}
166
167/* The whole ATAPI transfer logic is handled in this function */
168void ide_atapi_cmd_reply_end(IDEState *s)
169{
170 int byte_count_limit, size, ret;
171#ifdef DEBUG_IDE_ATAPI
172 printf("reply: tx_size=%d elem_tx_size=%d index=%d\n",
173 s->packet_transfer_size,
174 s->elementary_transfer_size,
175 s->io_buffer_index);
176#endif
177 if (s->packet_transfer_size <= 0) {
178 /* end of transfer */
33231e0e
KW
179 s->status = READY_STAT | SEEK_STAT;
180 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
c7e73adb 181 ide_transfer_stop(s);
33231e0e
KW
182 ide_set_irq(s->bus);
183#ifdef DEBUG_IDE_ATAPI
184 printf("status=0x%x\n", s->status);
185#endif
186 } else {
187 /* see if a new sector must be read */
188 if (s->lba != -1 && s->io_buffer_index >= s->cd_sector_size) {
a597e79c 189 ret = cd_read_sector(s, s->lba, s->io_buffer, s->cd_sector_size);
33231e0e
KW
190 if (ret < 0) {
191 ide_transfer_stop(s);
192 ide_atapi_io_error(s, ret);
193 return;
194 }
195 s->lba++;
196 s->io_buffer_index = 0;
197 }
198 if (s->elementary_transfer_size > 0) {
199 /* there are some data left to transmit in this elementary
200 transfer */
201 size = s->cd_sector_size - s->io_buffer_index;
202 if (size > s->elementary_transfer_size)
203 size = s->elementary_transfer_size;
204 s->packet_transfer_size -= size;
205 s->elementary_transfer_size -= size;
206 s->io_buffer_index += size;
207 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
208 size, ide_atapi_cmd_reply_end);
209 } else {
210 /* a new transfer is needed */
211 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO;
212 byte_count_limit = s->lcyl | (s->hcyl << 8);
213#ifdef DEBUG_IDE_ATAPI
214 printf("byte_count_limit=%d\n", byte_count_limit);
215#endif
216 if (byte_count_limit == 0xffff)
217 byte_count_limit--;
218 size = s->packet_transfer_size;
219 if (size > byte_count_limit) {
220 /* byte count limit must be even if this case */
221 if (byte_count_limit & 1)
222 byte_count_limit--;
223 size = byte_count_limit;
224 }
225 s->lcyl = size;
226 s->hcyl = size >> 8;
227 s->elementary_transfer_size = size;
228 /* we cannot transmit more than one sector at a time */
229 if (s->lba != -1) {
230 if (size > (s->cd_sector_size - s->io_buffer_index))
231 size = (s->cd_sector_size - s->io_buffer_index);
232 }
233 s->packet_transfer_size -= size;
234 s->elementary_transfer_size -= size;
235 s->io_buffer_index += size;
236 ide_transfer_start(s, s->io_buffer + s->io_buffer_index - size,
237 size, ide_atapi_cmd_reply_end);
238 ide_set_irq(s->bus);
239#ifdef DEBUG_IDE_ATAPI
240 printf("status=0x%x\n", s->status);
241#endif
242 }
243 }
244}
245
246/* send a reply of 'size' bytes in s->io_buffer to an ATAPI command */
247static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)
248{
249 if (size > max_size)
250 size = max_size;
251 s->lba = -1; /* no sector read */
252 s->packet_transfer_size = size;
253 s->io_buffer_size = size; /* dma: send the reply data as one chunk */
254 s->elementary_transfer_size = 0;
255 s->io_buffer_index = 0;
256
257 if (s->atapi_dma) {
5366d0c8
BC
258 block_acct_start(bdrv_get_stats(s->bs), &s->acct, size,
259 BLOCK_ACCT_READ);
33231e0e 260 s->status = READY_STAT | SEEK_STAT | DRQ_STAT;
4855b576 261 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
33231e0e
KW
262 } else {
263 s->status = READY_STAT | SEEK_STAT;
264 ide_atapi_cmd_reply_end(s);
265 }
266}
267
268/* start a CD-CDROM read command */
269static void ide_atapi_cmd_read_pio(IDEState *s, int lba, int nb_sectors,
270 int sector_size)
271{
272 s->lba = lba;
273 s->packet_transfer_size = nb_sectors * sector_size;
274 s->elementary_transfer_size = 0;
275 s->io_buffer_index = sector_size;
276 s->cd_sector_size = sector_size;
277
278 s->status = READY_STAT | SEEK_STAT;
279 ide_atapi_cmd_reply_end(s);
280}
281
282static void ide_atapi_cmd_check_status(IDEState *s)
283{
284#ifdef DEBUG_IDE_ATAPI
285 printf("atapi_cmd_check_status\n");
286#endif
67cc61e4 287 s->error = MC_ERR | (UNIT_ATTENTION << 4);
33231e0e
KW
288 s->status = ERR_STAT;
289 s->nsector = 0;
290 ide_set_irq(s->bus);
291}
292/* ATAPI DMA support */
293
294/* XXX: handle read errors */
295static void ide_atapi_cmd_read_dma_cb(void *opaque, int ret)
296{
297 IDEState *s = opaque;
298 int data_offset, n;
299
300 if (ret < 0) {
301 ide_atapi_io_error(s, ret);
302 goto eot;
303 }
304
305 if (s->io_buffer_size > 0) {
306 /*
307 * For a cdrom read sector command (s->lba != -1),
308 * adjust the lba for the next s->io_buffer_size chunk
309 * and dma the current chunk.
310 * For a command != read (s->lba == -1), just transfer
311 * the reply data.
312 */
313 if (s->lba != -1) {
314 if (s->cd_sector_size == 2352) {
315 n = 1;
316 cd_data_to_raw(s->io_buffer, s->lba);
317 } else {
318 n = s->io_buffer_size >> 11;
319 }
320 s->lba += n;
321 }
322 s->packet_transfer_size -= s->io_buffer_size;
323 if (s->bus->dma->ops->rw_buf(s->bus->dma, 1) == 0)
324 goto eot;
325 }
326
327 if (s->packet_transfer_size <= 0) {
328 s->status = READY_STAT | SEEK_STAT;
329 s->nsector = (s->nsector & ~7) | ATAPI_INT_REASON_IO | ATAPI_INT_REASON_CD;
330 ide_set_irq(s->bus);
a597e79c 331 goto eot;
33231e0e
KW
332 }
333
334 s->io_buffer_index = 0;
335 if (s->cd_sector_size == 2352) {
336 n = 1;
337 s->io_buffer_size = s->cd_sector_size;
338 data_offset = 16;
339 } else {
340 n = s->packet_transfer_size >> 11;
341 if (n > (IDE_DMA_BUF_SECTORS / 4))
342 n = (IDE_DMA_BUF_SECTORS / 4);
343 s->io_buffer_size = n * 2048;
344 data_offset = 0;
345 }
346#ifdef DEBUG_AIO
347 printf("aio_read_cd: lba=%u n=%d\n", s->lba, n);
348#endif
a597e79c 349
33231e0e
KW
350 s->bus->dma->iov.iov_base = (void *)(s->io_buffer + data_offset);
351 s->bus->dma->iov.iov_len = n * 4 * 512;
352 qemu_iovec_init_external(&s->bus->dma->qiov, &s->bus->dma->iov, 1);
a597e79c 353
33231e0e
KW
354 s->bus->dma->aiocb = bdrv_aio_readv(s->bs, (int64_t)s->lba << 2,
355 &s->bus->dma->qiov, n * 4,
356 ide_atapi_cmd_read_dma_cb, s);
a597e79c 357 return;
ad54ae80 358
a597e79c 359eot:
5366d0c8 360 block_acct_done(bdrv_get_stats(s->bs), &s->acct);
0e7ce54c 361 ide_set_inactive(s, false);
33231e0e
KW
362}
363
364/* start a CD-CDROM read command with DMA */
365/* XXX: test if DMA is available */
366static void ide_atapi_cmd_read_dma(IDEState *s, int lba, int nb_sectors,
367 int sector_size)
368{
369 s->lba = lba;
370 s->packet_transfer_size = nb_sectors * sector_size;
371 s->io_buffer_index = 0;
372 s->io_buffer_size = 0;
373 s->cd_sector_size = sector_size;
374
5366d0c8
BC
375 block_acct_start(bdrv_get_stats(s->bs), &s->acct, s->packet_transfer_size,
376 BLOCK_ACCT_READ);
a597e79c 377
33231e0e
KW
378 /* XXX: check if BUSY_STAT should be set */
379 s->status = READY_STAT | SEEK_STAT | DRQ_STAT | BUSY_STAT;
4855b576 380 ide_start_dma(s, ide_atapi_cmd_read_dma_cb);
33231e0e
KW
381}
382
383static void ide_atapi_cmd_read(IDEState *s, int lba, int nb_sectors,
384 int sector_size)
385{
386#ifdef DEBUG_IDE_ATAPI
387 printf("read %s: LBA=%d nb_sectors=%d\n", s->atapi_dma ? "dma" : "pio",
388 lba, nb_sectors);
389#endif
390 if (s->atapi_dma) {
391 ide_atapi_cmd_read_dma(s, lba, nb_sectors, sector_size);
392 } else {
393 ide_atapi_cmd_read_pio(s, lba, nb_sectors, sector_size);
394 }
395}
396
397static inline uint8_t ide_atapi_set_profile(uint8_t *buf, uint8_t *index,
398 uint16_t profile)
399{
400 uint8_t *buf_profile = buf + 12; /* start of profiles */
401
402 buf_profile += ((*index) * 4); /* start of indexed profile */
403 cpu_to_ube16 (buf_profile, profile);
404 buf_profile[2] = ((buf_profile[0] == buf[6]) && (buf_profile[1] == buf[7]));
405
406 /* each profile adds 4 bytes to the response */
407 (*index)++;
408 buf[11] += 4; /* Additional Length */
409
410 return 4;
411}
412
413static int ide_dvd_read_structure(IDEState *s, int format,
414 const uint8_t *packet, uint8_t *buf)
415{
416 switch (format) {
417 case 0x0: /* Physical format information */
418 {
419 int layer = packet[6];
420 uint64_t total_sectors;
421
422 if (layer != 0)
423 return -ASC_INV_FIELD_IN_CMD_PACKET;
424
e119bcac
KW
425 total_sectors = s->nb_sectors >> 2;
426 if (total_sectors == 0) {
33231e0e 427 return -ASC_MEDIUM_NOT_PRESENT;
e119bcac 428 }
33231e0e
KW
429
430 buf[4] = 1; /* DVD-ROM, part version 1 */
431 buf[5] = 0xf; /* 120mm disc, minimum rate unspecified */
432 buf[6] = 1; /* one layer, read-only (per MMC-2 spec) */
433 buf[7] = 0; /* default densities */
434
435 /* FIXME: 0x30000 per spec? */
436 cpu_to_ube32(buf + 8, 0); /* start sector */
437 cpu_to_ube32(buf + 12, total_sectors - 1); /* end sector */
438 cpu_to_ube32(buf + 16, total_sectors - 1); /* l0 end sector */
439
440 /* Size of buffer, not including 2 byte size field */
d8ee2591 441 stw_be_p(buf, 2048 + 2);
33231e0e
KW
442
443 /* 2k data + 4 byte header */
444 return (2048 + 4);
445 }
446
447 case 0x01: /* DVD copyright information */
448 buf[4] = 0; /* no copyright data */
449 buf[5] = 0; /* no region restrictions */
450
451 /* Size of buffer, not including 2 byte size field */
d8ee2591 452 stw_be_p(buf, 4 + 2);
33231e0e
KW
453
454 /* 4 byte header + 4 byte data */
455 return (4 + 4);
456
457 case 0x03: /* BCA information - invalid field for no BCA info */
458 return -ASC_INV_FIELD_IN_CMD_PACKET;
459
460 case 0x04: /* DVD disc manufacturing information */
461 /* Size of buffer, not including 2 byte size field */
d8ee2591 462 stw_be_p(buf, 2048 + 2);
33231e0e
KW
463
464 /* 2k data + 4 byte header */
465 return (2048 + 4);
466
467 case 0xff:
468 /*
469 * This lists all the command capabilities above. Add new ones
470 * in order and update the length and buffer return values.
471 */
472
473 buf[4] = 0x00; /* Physical format */
474 buf[5] = 0x40; /* Not writable, is readable */
d8ee2591 475 stw_be_p(buf + 6, 2048 + 4);
33231e0e
KW
476
477 buf[8] = 0x01; /* Copyright info */
478 buf[9] = 0x40; /* Not writable, is readable */
d8ee2591 479 stw_be_p(buf + 10, 4 + 4);
33231e0e
KW
480
481 buf[12] = 0x03; /* BCA info */
482 buf[13] = 0x40; /* Not writable, is readable */
d8ee2591 483 stw_be_p(buf + 14, 188 + 4);
33231e0e
KW
484
485 buf[16] = 0x04; /* Manufacturing info */
486 buf[17] = 0x40; /* Not writable, is readable */
d8ee2591 487 stw_be_p(buf + 18, 2048 + 4);
33231e0e
KW
488
489 /* Size of buffer, not including 2 byte size field */
d8ee2591 490 stw_be_p(buf, 16 + 2);
33231e0e
KW
491
492 /* data written + 4 byte header */
493 return (16 + 4);
494
495 default: /* TODO: formats beyond DVD-ROM requires */
496 return -ASC_INV_FIELD_IN_CMD_PACKET;
497 }
498}
499
500static unsigned int event_status_media(IDEState *s,
501 uint8_t *buf)
502{
33231e0e
KW
503 uint8_t event_code, media_status;
504
505 media_status = 0;
dd063333 506 if (s->tray_open) {
33231e0e
KW
507 media_status = MS_TRAY_OPEN;
508 } else if (bdrv_is_inserted(s->bs)) {
509 media_status = MS_MEDIA_PRESENT;
510 }
511
512 /* Event notification descriptor */
513 event_code = MEC_NO_CHANGE;
2df0a3a3
PB
514 if (media_status != MS_TRAY_OPEN) {
515 if (s->events.new_media) {
516 event_code = MEC_NEW_MEDIA;
517 s->events.new_media = false;
518 } else if (s->events.eject_request) {
519 event_code = MEC_EJECT_REQUESTED;
520 s->events.eject_request = false;
521 }
33231e0e
KW
522 }
523
524 buf[4] = event_code;
525 buf[5] = media_status;
526
527 /* These fields are reserved, just clear them. */
528 buf[6] = 0;
529 buf[7] = 0;
530
531 return 8; /* We wrote to 4 extra bytes from the header */
532}
533
e1a064f9
KW
534static void cmd_get_event_status_notification(IDEState *s,
535 uint8_t *buf)
33231e0e 536{
e1a064f9
KW
537 const uint8_t *packet = buf;
538
33231e0e
KW
539 struct {
540 uint8_t opcode;
541 uint8_t polled; /* lsb bit is polled; others are reserved */
542 uint8_t reserved2[2];
543 uint8_t class;
544 uint8_t reserved3[2];
545 uint16_t len;
546 uint8_t control;
541dc0d4 547 } QEMU_PACKED *gesn_cdb;
33231e0e
KW
548
549 struct {
550 uint16_t len;
551 uint8_t notification_class;
552 uint8_t supported_events;
541dc0d4 553 } QEMU_PACKED *gesn_event_header;
33231e0e
KW
554 unsigned int max_len, used_len;
555
556 gesn_cdb = (void *)packet;
557 gesn_event_header = (void *)buf;
558
559 max_len = be16_to_cpu(gesn_cdb->len);
560
561 /* It is fine by the MMC spec to not support async mode operations */
562 if (!(gesn_cdb->polled & 0x01)) { /* asynchronous mode */
563 /* Only polling is supported, asynchronous mode is not. */
67cc61e4 564 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
33231e0e
KW
565 ASC_INV_FIELD_IN_CMD_PACKET);
566 return;
567 }
568
569 /* polling mode operation */
570
571 /*
572 * These are the supported events.
573 *
574 * We currently only support requests of the 'media' type.
f0f992e6
PB
575 * Notification class requests and supported event classes are bitmasks,
576 * but they are build from the same values as the "notification class"
577 * field.
33231e0e 578 */
f0f992e6 579 gesn_event_header->supported_events = 1 << GESN_MEDIA;
33231e0e
KW
580
581 /*
582 * We use |= below to set the class field; other bits in this byte
583 * are reserved now but this is useful to do if we have to use the
584 * reserved fields later.
585 */
586 gesn_event_header->notification_class = 0;
587
588 /*
589 * Responses to requests are to be based on request priority. The
590 * notification_class_request_type enum above specifies the
591 * priority: upper elements are higher prio than lower ones.
592 */
f0f992e6
PB
593 if (gesn_cdb->class & (1 << GESN_MEDIA)) {
594 gesn_event_header->notification_class |= GESN_MEDIA;
33231e0e
KW
595 used_len = event_status_media(s, buf);
596 } else {
597 gesn_event_header->notification_class = 0x80; /* No event available */
598 used_len = sizeof(*gesn_event_header);
599 }
600 gesn_event_header->len = cpu_to_be16(used_len
601 - sizeof(*gesn_event_header));
602 ide_atapi_cmd_reply(s, used_len, max_len);
603}
604
a60cf7e7
KW
605static void cmd_request_sense(IDEState *s, uint8_t *buf)
606{
607 int max_len = buf[4];
608
609 memset(buf, 0, 18);
610 buf[0] = 0x70 | (1 << 7);
611 buf[2] = s->sense_key;
612 buf[7] = 10;
613 buf[12] = s->asc;
614
67cc61e4
PB
615 if (s->sense_key == UNIT_ATTENTION) {
616 s->sense_key = NO_SENSE;
a60cf7e7
KW
617 }
618
619 ide_atapi_cmd_reply(s, 18, max_len);
620}
621
622static void cmd_inquiry(IDEState *s, uint8_t *buf)
623{
624 int max_len = buf[4];
625
626 buf[0] = 0x05; /* CD-ROM */
627 buf[1] = 0x80; /* removable */
628 buf[2] = 0x00; /* ISO */
629 buf[3] = 0x21; /* ATAPI-2 (XXX: put ATAPI-4 ?) */
630 buf[4] = 31; /* additional length */
631 buf[5] = 0; /* reserved */
632 buf[6] = 0; /* reserved */
633 buf[7] = 0; /* reserved */
634 padstr8(buf + 8, 8, "QEMU");
635 padstr8(buf + 16, 16, "QEMU DVD-ROM");
636 padstr8(buf + 32, 4, s->version);
637 ide_atapi_cmd_reply(s, 36, max_len);
638}
639
640static void cmd_get_configuration(IDEState *s, uint8_t *buf)
641{
642 uint32_t len;
643 uint8_t index = 0;
644 int max_len;
645
646 /* only feature 0 is supported */
647 if (buf[2] != 0 || buf[3] != 0) {
67cc61e4 648 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
649 ASC_INV_FIELD_IN_CMD_PACKET);
650 return;
651 }
652
653 /* XXX: could result in alignment problems in some architectures */
654 max_len = ube16_to_cpu(buf + 7);
655
656 /*
657 * XXX: avoid overflow for io_buffer if max_len is bigger than
658 * the size of that buffer (dimensioned to max number of
659 * sectors to transfer at once)
660 *
661 * Only a problem if the feature/profiles grow.
662 */
663 if (max_len > 512) {
664 /* XXX: assume 1 sector */
665 max_len = 512;
666 }
667
668 memset(buf, 0, max_len);
669 /*
670 * the number of sectors from the media tells us which profile
671 * to use as current. 0 means there is no media
672 */
673 if (media_is_dvd(s)) {
674 cpu_to_ube16(buf + 6, MMC_PROFILE_DVD_ROM);
675 } else if (media_is_cd(s)) {
676 cpu_to_ube16(buf + 6, MMC_PROFILE_CD_ROM);
677 }
678
679 buf[10] = 0x02 | 0x01; /* persistent and current */
680 len = 12; /* headers: 8 + 4 */
681 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_DVD_ROM);
682 len += ide_atapi_set_profile(buf, &index, MMC_PROFILE_CD_ROM);
683 cpu_to_ube32(buf, len - 4); /* data length */
684
685 ide_atapi_cmd_reply(s, len, max_len);
686}
687
688static void cmd_mode_sense(IDEState *s, uint8_t *buf)
689{
690 int action, code;
691 int max_len;
692
2c20ae11 693 max_len = ube16_to_cpu(buf + 7);
a60cf7e7
KW
694 action = buf[2] >> 6;
695 code = buf[2] & 0x3f;
696
697 switch(action) {
698 case 0: /* current values */
699 switch(code) {
67cc61e4 700 case MODE_PAGE_R_W_ERROR: /* error recovery */
2c20ae11 701 cpu_to_ube16(&buf[0], 16 - 2);
a60cf7e7
KW
702 buf[2] = 0x70;
703 buf[3] = 0;
704 buf[4] = 0;
705 buf[5] = 0;
706 buf[6] = 0;
707 buf[7] = 0;
708
af0e1ea2
PB
709 buf[8] = MODE_PAGE_R_W_ERROR;
710 buf[9] = 16 - 10;
a60cf7e7
KW
711 buf[10] = 0x00;
712 buf[11] = 0x05;
713 buf[12] = 0x00;
714 buf[13] = 0x00;
715 buf[14] = 0x00;
716 buf[15] = 0x00;
717 ide_atapi_cmd_reply(s, 16, max_len);
718 break;
67cc61e4 719 case MODE_PAGE_AUDIO_CTL:
2c20ae11 720 cpu_to_ube16(&buf[0], 24 - 2);
a60cf7e7
KW
721 buf[2] = 0x70;
722 buf[3] = 0;
723 buf[4] = 0;
724 buf[5] = 0;
725 buf[6] = 0;
726 buf[7] = 0;
727
af0e1ea2
PB
728 buf[8] = MODE_PAGE_AUDIO_CTL;
729 buf[9] = 24 - 10;
a60cf7e7
KW
730 /* Fill with CDROM audio volume */
731 buf[17] = 0;
732 buf[19] = 0;
733 buf[21] = 0;
734 buf[23] = 0;
735
736 ide_atapi_cmd_reply(s, 24, max_len);
737 break;
67cc61e4 738 case MODE_PAGE_CAPABILITIES:
2c20ae11 739 cpu_to_ube16(&buf[0], 30 - 2);
a60cf7e7
KW
740 buf[2] = 0x70;
741 buf[3] = 0;
742 buf[4] = 0;
743 buf[5] = 0;
744 buf[6] = 0;
745 buf[7] = 0;
746
af0e1ea2 747 buf[8] = MODE_PAGE_CAPABILITIES;
2c20ae11 748 buf[9] = 30 - 10;
a07c7dcd 749 buf[10] = 0x3b; /* read CDR/CDRW/DVDROM/DVDR/DVDRAM */
a60cf7e7
KW
750 buf[11] = 0x00;
751
752 /* Claim PLAY_AUDIO capability (0x01) since some Linux
753 code checks for this to automount media. */
754 buf[12] = 0x71;
755 buf[13] = 3 << 5;
756 buf[14] = (1 << 0) | (1 << 3) | (1 << 5);
a0a7573b 757 if (s->tray_locked) {
a07c7dcd 758 buf[14] |= 1 << 1;
a0a7573b 759 }
a07c7dcd
PB
760 buf[15] = 0x00; /* No volume & mute control, no changer */
761 cpu_to_ube16(&buf[16], 704); /* 4x read speed */
762 buf[18] = 0; /* Two volume levels */
a60cf7e7 763 buf[19] = 2;
a07c7dcd
PB
764 cpu_to_ube16(&buf[20], 512); /* 512k buffer */
765 cpu_to_ube16(&buf[22], 704); /* 4x read speed current */
a60cf7e7
KW
766 buf[24] = 0;
767 buf[25] = 0;
768 buf[26] = 0;
769 buf[27] = 0;
2c20ae11
PB
770 buf[28] = 0;
771 buf[29] = 0;
772 ide_atapi_cmd_reply(s, 30, max_len);
a60cf7e7
KW
773 break;
774 default:
775 goto error_cmd;
776 }
777 break;
778 case 1: /* changeable values */
779 goto error_cmd;
780 case 2: /* default values */
781 goto error_cmd;
782 default:
783 case 3: /* saved values */
67cc61e4 784 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
785 ASC_SAVING_PARAMETERS_NOT_SUPPORTED);
786 break;
787 }
788 return;
789
790error_cmd:
67cc61e4 791 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_INV_FIELD_IN_CMD_PACKET);
a60cf7e7
KW
792}
793
794static void cmd_test_unit_ready(IDEState *s, uint8_t *buf)
795{
7a2c4b82
KW
796 /* Not Ready Conditions are already handled in ide_atapi_cmd(), so if we
797 * come here, we know that it's ready. */
798 ide_atapi_cmd_ok(s);
a60cf7e7
KW
799}
800
801static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)
802{
a0a7573b 803 s->tray_locked = buf[4] & 1;
025e849a 804 bdrv_lock_medium(s->bs, buf[4] & 1);
a60cf7e7
KW
805 ide_atapi_cmd_ok(s);
806}
807
808static void cmd_read(IDEState *s, uint8_t* buf)
809{
810 int nb_sectors, lba;
811
812 if (buf[0] == GPCMD_READ_10) {
813 nb_sectors = ube16_to_cpu(buf + 7);
814 } else {
815 nb_sectors = ube32_to_cpu(buf + 6);
816 }
817
818 lba = ube32_to_cpu(buf + 2);
819 if (nb_sectors == 0) {
820 ide_atapi_cmd_ok(s);
821 return;
822 }
823
824 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
825}
826
827static void cmd_read_cd(IDEState *s, uint8_t* buf)
828{
829 int nb_sectors, lba, transfer_request;
830
831 nb_sectors = (buf[6] << 16) | (buf[7] << 8) | buf[8];
832 lba = ube32_to_cpu(buf + 2);
833
834 if (nb_sectors == 0) {
835 ide_atapi_cmd_ok(s);
836 return;
837 }
838
839 transfer_request = buf[9];
840 switch(transfer_request & 0xf8) {
841 case 0x00:
842 /* nothing */
843 ide_atapi_cmd_ok(s);
844 break;
845 case 0x10:
846 /* normal read */
847 ide_atapi_cmd_read(s, lba, nb_sectors, 2048);
848 break;
849 case 0xf8:
850 /* read all data */
851 ide_atapi_cmd_read(s, lba, nb_sectors, 2352);
852 break;
853 default:
67cc61e4 854 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
855 ASC_INV_FIELD_IN_CMD_PACKET);
856 break;
857 }
858}
859
860static void cmd_seek(IDEState *s, uint8_t* buf)
861{
862 unsigned int lba;
e119bcac 863 uint64_t total_sectors = s->nb_sectors >> 2;
a60cf7e7 864
a60cf7e7
KW
865 lba = ube32_to_cpu(buf + 2);
866 if (lba >= total_sectors) {
67cc61e4 867 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_LOGICAL_BLOCK_OOR);
a60cf7e7
KW
868 return;
869 }
870
871 ide_atapi_cmd_ok(s);
872}
873
874static void cmd_start_stop_unit(IDEState *s, uint8_t* buf)
875{
fdec4404 876 int sense;
f0776564
MA
877 bool start = buf[4] & 1;
878 bool loej = buf[4] & 2; /* load on start, eject on !start */
ce560dcf
RS
879 int pwrcnd = buf[4] & 0xf0;
880
881 if (pwrcnd) {
882 /* eject/load only happens for power condition == 0 */
883 return;
884 }
a60cf7e7 885
f0776564 886 if (loej) {
48f65b3f 887 if (!start && !s->tray_open && s->tray_locked) {
fdec4404 888 sense = bdrv_is_inserted(s->bs)
67cc61e4 889 ? NOT_READY : ILLEGAL_REQUEST;
fdec4404
MA
890 ide_atapi_cmd_error(s, sense, ASC_MEDIA_REMOVAL_PREVENTED);
891 return;
a60cf7e7 892 }
d88b1819
LC
893
894 if (s->tray_open != !start) {
895 bdrv_eject(s->bs, !start);
896 s->tray_open = !start;
897 }
dd063333 898 }
fdec4404
MA
899
900 ide_atapi_cmd_ok(s);
a60cf7e7
KW
901}
902
903static void cmd_mechanism_status(IDEState *s, uint8_t* buf)
904{
905 int max_len = ube16_to_cpu(buf + 8);
906
907 cpu_to_ube16(buf, 0);
908 /* no current LBA */
909 buf[2] = 0;
910 buf[3] = 0;
911 buf[4] = 0;
912 buf[5] = 1;
913 cpu_to_ube16(buf + 6, 0);
914 ide_atapi_cmd_reply(s, 8, max_len);
915}
916
917static void cmd_read_toc_pma_atip(IDEState *s, uint8_t* buf)
918{
919 int format, msf, start_track, len;
a60cf7e7 920 int max_len;
7a2c4b82 921 uint64_t total_sectors = s->nb_sectors >> 2;
a60cf7e7
KW
922
923 max_len = ube16_to_cpu(buf + 7);
924 format = buf[9] >> 6;
925 msf = (buf[1] >> 1) & 1;
926 start_track = buf[6];
927
928 switch(format) {
929 case 0:
930 len = cdrom_read_toc(total_sectors, buf, msf, start_track);
931 if (len < 0)
932 goto error_cmd;
933 ide_atapi_cmd_reply(s, len, max_len);
934 break;
935 case 1:
936 /* multi session : only a single session defined */
937 memset(buf, 0, 12);
938 buf[1] = 0x0a;
939 buf[2] = 0x01;
940 buf[3] = 0x01;
941 ide_atapi_cmd_reply(s, 12, max_len);
942 break;
943 case 2:
944 len = cdrom_read_toc_raw(total_sectors, buf, msf, start_track);
945 if (len < 0)
946 goto error_cmd;
947 ide_atapi_cmd_reply(s, len, max_len);
948 break;
949 default:
950 error_cmd:
67cc61e4 951 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
952 ASC_INV_FIELD_IN_CMD_PACKET);
953 }
954}
955
956static void cmd_read_cdvd_capacity(IDEState *s, uint8_t* buf)
957{
e119bcac 958 uint64_t total_sectors = s->nb_sectors >> 2;
a60cf7e7 959
a60cf7e7
KW
960 /* NOTE: it is really the number of sectors minus 1 */
961 cpu_to_ube32(buf, total_sectors - 1);
962 cpu_to_ube32(buf + 4, 2048);
963 ide_atapi_cmd_reply(s, 8, 8);
964}
965
55042b95
PB
966static void cmd_read_disc_information(IDEState *s, uint8_t* buf)
967{
968 uint8_t type = buf[1] & 7;
969 uint32_t max_len = ube16_to_cpu(buf + 7);
970
971 /* Types 1/2 are only defined for Blu-Ray. */
972 if (type != 0) {
973 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
974 ASC_INV_FIELD_IN_CMD_PACKET);
975 return;
976 }
977
978 memset(buf, 0, 34);
979 buf[1] = 32;
980 buf[2] = 0xe; /* last session complete, disc finalized */
981 buf[3] = 1; /* first track on disc */
982 buf[4] = 1; /* # of sessions */
983 buf[5] = 1; /* first track of last session */
984 buf[6] = 1; /* last track of last session */
985 buf[7] = 0x20; /* unrestricted use */
986 buf[8] = 0x00; /* CD-ROM or DVD-ROM */
987 /* 9-10-11: most significant byte corresponding bytes 4-5-6 */
988 /* 12-23: not meaningful for CD-ROM or DVD-ROM */
989 /* 24-31: disc bar code */
990 /* 32: disc application code */
991 /* 33: number of OPC tables */
992
993 ide_atapi_cmd_reply(s, 34, max_len);
994}
995
a60cf7e7
KW
996static void cmd_read_dvd_structure(IDEState *s, uint8_t* buf)
997{
998 int max_len;
999 int media = buf[1];
1000 int format = buf[7];
1001 int ret;
1002
1003 max_len = ube16_to_cpu(buf + 8);
1004
1005 if (format < 0xff) {
1006 if (media_is_cd(s)) {
67cc61e4 1007 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
1008 ASC_INCOMPATIBLE_FORMAT);
1009 return;
1010 } else if (!media_present(s)) {
67cc61e4 1011 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
1012 ASC_INV_FIELD_IN_CMD_PACKET);
1013 return;
1014 }
1015 }
1016
1017 memset(buf, 0, max_len > IDE_DMA_BUF_SECTORS * 512 + 4 ?
1018 IDE_DMA_BUF_SECTORS * 512 + 4 : max_len);
1019
1020 switch (format) {
1021 case 0x00 ... 0x7f:
1022 case 0xff:
1023 if (media == 0) {
1024 ret = ide_dvd_read_structure(s, format, buf, buf);
1025
1026 if (ret < 0) {
67cc61e4 1027 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, -ret);
a60cf7e7
KW
1028 } else {
1029 ide_atapi_cmd_reply(s, ret, max_len);
1030 }
1031
1032 break;
1033 }
1034 /* TODO: BD support, fall through for now */
1035
1036 /* Generic disk structures */
1037 case 0x80: /* TODO: AACS volume identifier */
1038 case 0x81: /* TODO: AACS media serial number */
1039 case 0x82: /* TODO: AACS media identifier */
1040 case 0x83: /* TODO: AACS media key block */
1041 case 0x90: /* TODO: List of recognized format layers */
1042 case 0xc0: /* TODO: Write protection status */
1043 default:
67cc61e4 1044 ide_atapi_cmd_error(s, ILLEGAL_REQUEST,
a60cf7e7
KW
1045 ASC_INV_FIELD_IN_CMD_PACKET);
1046 break;
1047 }
1048}
1049
1050static void cmd_set_speed(IDEState *s, uint8_t* buf)
1051{
1052 ide_atapi_cmd_ok(s);
1053}
1054
e1a064f9
KW
1055enum {
1056 /*
1057 * Only commands flagged as ALLOW_UA are allowed to run under a
1058 * unit attention condition. (See MMC-5, section 4.1.6.1)
1059 */
1060 ALLOW_UA = 0x01,
7a2c4b82
KW
1061
1062 /*
1063 * Commands flagged with CHECK_READY can only execute if a medium is present.
1064 * Otherwise they report the Not Ready Condition. (See MMC-5, section
1065 * 4.1.8)
1066 */
1067 CHECK_READY = 0x02,
e1a064f9
KW
1068};
1069
1070static const struct {
1071 void (*handler)(IDEState *s, uint8_t *buf);
1072 int flags;
1073} atapi_cmd_table[0x100] = {
7a2c4b82 1074 [ 0x00 ] = { cmd_test_unit_ready, CHECK_READY },
e1a064f9
KW
1075 [ 0x03 ] = { cmd_request_sense, ALLOW_UA },
1076 [ 0x12 ] = { cmd_inquiry, ALLOW_UA },
a1aff5bf 1077 [ 0x1b ] = { cmd_start_stop_unit, 0 }, /* [1] */
e1a064f9 1078 [ 0x1e ] = { cmd_prevent_allow_medium_removal, 0 },
7a2c4b82 1079 [ 0x25 ] = { cmd_read_cdvd_capacity, CHECK_READY },
a1aff5bf 1080 [ 0x28 ] = { cmd_read, /* (10) */ CHECK_READY },
7a2c4b82
KW
1081 [ 0x2b ] = { cmd_seek, CHECK_READY },
1082 [ 0x43 ] = { cmd_read_toc_pma_atip, CHECK_READY },
e1a064f9
KW
1083 [ 0x46 ] = { cmd_get_configuration, ALLOW_UA },
1084 [ 0x4a ] = { cmd_get_event_status_notification, ALLOW_UA },
55042b95 1085 [ 0x51 ] = { cmd_read_disc_information, CHECK_READY },
e1a064f9 1086 [ 0x5a ] = { cmd_mode_sense, /* (10) */ 0 },
a1aff5bf
MA
1087 [ 0xa8 ] = { cmd_read, /* (12) */ CHECK_READY },
1088 [ 0xad ] = { cmd_read_dvd_structure, CHECK_READY },
e1a064f9
KW
1089 [ 0xbb ] = { cmd_set_speed, 0 },
1090 [ 0xbd ] = { cmd_mechanism_status, 0 },
a1aff5bf
MA
1091 [ 0xbe ] = { cmd_read_cd, CHECK_READY },
1092 /* [1] handler detects and reports not ready condition itself */
e1a064f9
KW
1093};
1094
33231e0e
KW
1095void ide_atapi_cmd(IDEState *s)
1096{
33231e0e 1097 uint8_t *buf;
33231e0e 1098
33231e0e
KW
1099 buf = s->io_buffer;
1100#ifdef DEBUG_IDE_ATAPI
1101 {
1102 int i;
1103 printf("ATAPI limit=0x%x packet:", s->lcyl | (s->hcyl << 8));
1104 for(i = 0; i < ATAPI_PACKET_SIZE; i++) {
ab719827 1105 printf(" %02x", buf[i]);
33231e0e
KW
1106 }
1107 printf("\n");
1108 }
1109#endif
1110 /*
e1a064f9
KW
1111 * If there's a UNIT_ATTENTION condition pending, only command flagged with
1112 * ALLOW_UA are allowed to complete. with other commands getting a CHECK
1113 * condition response unless a higher priority status, defined by the drive
33231e0e
KW
1114 * here, is pending.
1115 */
67cc61e4 1116 if (s->sense_key == UNIT_ATTENTION &&
e1a064f9 1117 !(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA)) {
33231e0e
KW
1118 ide_atapi_cmd_check_status(s);
1119 return;
1120 }
4a737d14
AS
1121 /*
1122 * When a CD gets changed, we have to report an ejected state and
1123 * then a loaded state to guests so that they detect tray
1124 * open/close and media change events. Guests that do not use
1125 * GET_EVENT_STATUS_NOTIFICATION to detect such tray open/close
1126 * states rely on this behavior.
1127 */
0c6f08b0
PH
1128 if (!(atapi_cmd_table[s->io_buffer[0]].flags & ALLOW_UA) &&
1129 !s->tray_open && bdrv_is_inserted(s->bs) && s->cdrom_changed) {
1130
1131 if (s->cdrom_changed == 1) {
1132 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
1133 s->cdrom_changed = 2;
1134 } else {
1135 ide_atapi_cmd_error(s, UNIT_ATTENTION, ASC_MEDIUM_MAY_HAVE_CHANGED);
1136 s->cdrom_changed = 0;
1137 }
33231e0e 1138
33231e0e
KW
1139 return;
1140 }
e1a064f9 1141
7a2c4b82
KW
1142 /* Report a Not Ready condition if appropriate for the command */
1143 if ((atapi_cmd_table[s->io_buffer[0]].flags & CHECK_READY) &&
1144 (!media_present(s) || !bdrv_is_inserted(s->bs)))
1145 {
67cc61e4 1146 ide_atapi_cmd_error(s, NOT_READY, ASC_MEDIUM_NOT_PRESENT);
7a2c4b82
KW
1147 return;
1148 }
1149
e1a064f9
KW
1150 /* Execute the command */
1151 if (atapi_cmd_table[s->io_buffer[0]].handler) {
1152 atapi_cmd_table[s->io_buffer[0]].handler(s, buf);
1153 return;
33231e0e 1154 }
e1a064f9 1155
67cc61e4 1156 ide_atapi_cmd_error(s, ILLEGAL_REQUEST, ASC_ILLEGAL_OPCODE);
33231e0e 1157}
This page took 0.453497 seconds and 4 git commands to generate.