atapi.c 42 KB

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