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