atapi.c 42 KB

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