2
0

atapi.c 33 KB

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