atapi.c 42 KB

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