atapi.c 41 KB

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