ZuluSCSI_cdrom.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. /* Advanced CD-ROM drive emulation.
  2. * Adds a few capabilities on top of the SCSI2SD CD-ROM emulation:
  3. *
  4. * - bin/cue support for support of multiple tracks
  5. * - on the fly image switching
  6. *
  7. * SCSI2SD V6 - Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  8. * ZuluSCSI™ - Copyright (c) 2023 Rabbit Hole Computing™
  9. *
  10. * This file is licensed under the GPL version 3 or any later version. 
  11. * It is derived from cdrom.c in SCSI2SD V6
  12. *
  13. * https://www.gnu.org/licenses/gpl-3.0.html
  14. * ----
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version. 
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. * GNU General Public License for more details. 
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  27. */
  28. #include <string.h>
  29. #include "ZuluSCSI_cdrom.h"
  30. #include "ZuluSCSI_log.h"
  31. #include "ZuluSCSI_config.h"
  32. #include <CUEParser.h>
  33. #include <assert.h>
  34. #ifdef ENABLE_AUDIO_OUTPUT
  35. #include "ZuluSCSI_audio.h"
  36. #endif
  37. extern "C" {
  38. #include <scsi.h>
  39. }
  40. /******************************************/
  41. /* Basic TOC generation without cue sheet */
  42. /******************************************/
  43. static const uint8_t SimpleTOC[] =
  44. {
  45. 0x00, // toc length, MSB
  46. 0x12, // toc length, LSB
  47. 0x01, // First track number
  48. 0x01, // Last track number,
  49. // TRACK 1 Descriptor
  50. 0x00, // reserved
  51. 0x14, // Q sub-channel encodes current position, Digital track
  52. 0x01, // Track 1,
  53. 0x00, // Reserved
  54. 0x00,0x00,0x00,0x00, // Track start sector (LBA)
  55. 0x00, // reserved
  56. 0x14, // Q sub-channel encodes current position, Digital track
  57. 0xAA, // Leadout Track
  58. 0x00, // Reserved
  59. 0x00,0x00,0x00,0x00, // Track start sector (LBA)
  60. };
  61. static const uint8_t LeadoutTOC[] =
  62. {
  63. 0x00, // toc length, MSB
  64. 0x0A, // toc length, LSB
  65. 0x01, // First track number
  66. 0x01, // Last track number,
  67. 0x00, // reserved
  68. 0x14, // Q sub-channel encodes current position, Digital track
  69. 0xAA, // Leadout Track
  70. 0x00, // Reserved
  71. 0x00,0x00,0x00,0x00, // Track start sector (LBA)
  72. };
  73. static const uint8_t SessionTOC[] =
  74. {
  75. 0x00, // toc length, MSB
  76. 0x0A, // toc length, LSB
  77. 0x01, // First session number
  78. 0x01, // Last session number,
  79. // TRACK 1 Descriptor
  80. 0x00, // reserved
  81. 0x14, // Q sub-channel encodes current position, Digital track
  82. 0x01, // First track number in last complete session
  83. 0x00, // Reserved
  84. 0x00,0x00,0x00,0x00 // LBA of first track in last session
  85. };
  86. static const uint8_t FullTOC[] =
  87. {
  88. 0x00, // 0: toc length, MSB
  89. 0x2E, // 1: toc length, LSB
  90. 0x01, // 2: First session number
  91. 0x01, // 3: Last session number,
  92. // A0 Descriptor
  93. 0x01, // 4: session number
  94. 0x14, // 5: ADR/Control
  95. 0x00, // 6: TNO
  96. 0xA0, // 7: POINT
  97. 0x00, // 8: Min
  98. 0x00, // 9: Sec
  99. 0x00, // 10: Frame
  100. 0x00, // 11: Zero
  101. 0x01, // 12: First Track number.
  102. 0x00, // 13: Disc type 00 = Mode 1
  103. 0x00, // 14: PFRAME
  104. // A1
  105. 0x01, // 15: session number
  106. 0x14, // 16: ADR/Control
  107. 0x00, // 17: TNO
  108. 0xA1, // 18: POINT
  109. 0x00, // 19: Min
  110. 0x00, // 20: Sec
  111. 0x00, // 21: Frame
  112. 0x00, // 22: Zero
  113. 0x01, // 23: Last Track number
  114. 0x00, // 24: PSEC
  115. 0x00, // 25: PFRAME
  116. // A2
  117. 0x01, // 26: session number
  118. 0x14, // 27: ADR/Control
  119. 0x00, // 28: TNO
  120. 0xA2, // 29: POINT
  121. 0x00, // 30: Min
  122. 0x00, // 31: Sec
  123. 0x00, // 32: Frame
  124. 0x00, // 33: Zero
  125. 0x00, // 34: LEADOUT position
  126. 0x00, // 35: leadout PSEC
  127. 0x00, // 36: leadout PFRAME
  128. // TRACK 1 Descriptor
  129. 0x01, // 37: session number
  130. 0x14, // 38: ADR/Control
  131. 0x00, // 39: TNO
  132. 0x01, // 40: Point
  133. 0x00, // 41: Min
  134. 0x00, // 42: Sec
  135. 0x00, // 43: Frame
  136. 0x00, // 44: Zero
  137. 0x00, // 45: PMIN
  138. 0x02, // 46: PSEC
  139. 0x00, // 47: PFRAME
  140. };
  141. static const uint8_t DiscInformation[] =
  142. {
  143. 0x00, // 0: disc info length, MSB
  144. 0x20, // 1: disc info length, LSB
  145. 0x0E, // 2: disc status (finalized, single session non-rewritable)
  146. 0x01, // 3: first track number
  147. 0x01, // 4: number of sessions (LSB)
  148. 0x01, // 5: first track in last session (LSB)
  149. 0x01, // 6: last track in last session (LSB)
  150. 0x00, // 7: format status (0x00 = non-rewritable, no barcode, no disc id)
  151. 0x00, // 8: disc type (0x00 = CD-ROM)
  152. 0x00, // 9: number of sessions (MSB)
  153. 0x00, // 10: first track in last session (MSB)
  154. 0x00, // 11: last track in last session (MSB)
  155. 0x00, // 12: disc ID (MSB)
  156. 0x00, // 13: .
  157. 0x00, // 14: .
  158. 0x00, // 15: disc ID (LSB)
  159. 0x00, // 16: last session lead-in start (MSB)
  160. 0x00, // 17: .
  161. 0x00, // 18: .
  162. 0x00, // 19: last session lead-in start (LSB)
  163. 0x00, // 20: last possible lead-out start (MSB)
  164. 0x00, // 21: .
  165. 0x00, // 22: .
  166. 0x00, // 23: last possible lead-out start (LSB)
  167. 0x00, // 24: disc bar code (MSB)
  168. 0x00, // 25: .
  169. 0x00, // 26: .
  170. 0x00, // 27: .
  171. 0x00, // 28: .
  172. 0x00, // 29: .
  173. 0x00, // 30: .
  174. 0x00, // 31: disc bar code (LSB)
  175. 0x00, // 32: disc application code
  176. 0x00, // 33: number of opc tables
  177. };
  178. // Convert logical block address to CD-ROM time
  179. static void LBA2MSF(int32_t LBA, uint8_t* MSF, bool relative)
  180. {
  181. if (!relative) {
  182. LBA += 150;
  183. }
  184. uint32_t ulba = LBA;
  185. if (LBA < 0) {
  186. ulba = LBA * -1;
  187. }
  188. MSF[2] = ulba % 75; // Frames
  189. uint32_t rem = ulba / 75;
  190. MSF[1] = rem % 60; // Seconds
  191. MSF[0] = rem / 60; // Minutes
  192. }
  193. // Convert logical block address to CD-ROM time in binary coded decimal format
  194. static void LBA2MSFBCD(int32_t LBA, uint8_t* MSF, bool relative)
  195. {
  196. LBA2MSF(LBA, MSF, relative);
  197. MSF[0] = ((MSF[0] / 10) << 4) | (MSF[0] % 10);
  198. MSF[1] = ((MSF[1] / 10) << 4) | (MSF[1] % 10);
  199. MSF[2] = ((MSF[2] / 10) << 4) | (MSF[2] % 10);
  200. }
  201. // Convert CD-ROM time to logical block address
  202. static int32_t MSF2LBA(uint8_t m, uint8_t s, uint8_t f, bool relative)
  203. {
  204. int32_t lba = (m * 60 + s) * 75 + f;
  205. if (!relative) lba -= 150;
  206. return lba;
  207. }
  208. // Gets the LBA position of the lead-out for the current image
  209. static uint32_t getLeadOutLBA(const CUETrackInfo* lasttrack)
  210. {
  211. if (lasttrack != nullptr && lasttrack->track_number != 0)
  212. {
  213. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  214. uint32_t lastTrackBlocks = (img.file.size() - lasttrack->file_offset)
  215. / lasttrack->sector_length;
  216. return lasttrack->track_start + lastTrackBlocks + 1;
  217. }
  218. else
  219. {
  220. return 1;
  221. }
  222. }
  223. static void doReadTOCSimple(bool MSF, uint8_t track, uint16_t allocationLength)
  224. {
  225. if (track == 0xAA)
  226. {
  227. // 0xAA requests only lead-out track information (reports capacity)
  228. uint32_t len = sizeof(LeadoutTOC);
  229. memcpy(scsiDev.data, LeadoutTOC, len);
  230. uint32_t capacity = getScsiCapacity(
  231. scsiDev.target->cfg->sdSectorStart,
  232. scsiDev.target->liveCfg.bytesPerSector,
  233. scsiDev.target->cfg->scsiSectors);
  234. // Replace start of leadout track
  235. if (MSF)
  236. {
  237. scsiDev.data[8] = 0;
  238. LBA2MSF(capacity, scsiDev.data + 9, false);
  239. }
  240. else
  241. {
  242. scsiDev.data[8] = capacity >> 24;
  243. scsiDev.data[9] = capacity >> 16;
  244. scsiDev.data[10] = capacity >> 8;
  245. scsiDev.data[11] = capacity;
  246. }
  247. if (len > allocationLength)
  248. {
  249. len = allocationLength;
  250. }
  251. scsiDev.dataLen = len;
  252. scsiDev.phase = DATA_IN;
  253. }
  254. else if (track <= 1)
  255. {
  256. // We only support track 1.
  257. // track 0 means "return all tracks"
  258. uint32_t len = sizeof(SimpleTOC);
  259. memcpy(scsiDev.data, SimpleTOC, len);
  260. if (MSF)
  261. {
  262. scsiDev.data[0x0A] = 0x02;
  263. }
  264. uint32_t capacity = getScsiCapacity(
  265. scsiDev.target->cfg->sdSectorStart,
  266. scsiDev.target->liveCfg.bytesPerSector,
  267. scsiDev.target->cfg->scsiSectors);
  268. // Replace start of leadout track
  269. if (MSF)
  270. {
  271. scsiDev.data[0x10] = 0;
  272. LBA2MSF(capacity, scsiDev.data + 0x11, false);
  273. }
  274. else
  275. {
  276. scsiDev.data[0x10] = capacity >> 24;
  277. scsiDev.data[0x11] = capacity >> 16;
  278. scsiDev.data[0x12] = capacity >> 8;
  279. scsiDev.data[0x13] = capacity;
  280. }
  281. if (len > allocationLength)
  282. {
  283. len = allocationLength;
  284. }
  285. scsiDev.dataLen = len;
  286. scsiDev.phase = DATA_IN;
  287. }
  288. else
  289. {
  290. scsiDev.status = CHECK_CONDITION;
  291. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  292. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  293. scsiDev.phase = STATUS;
  294. }
  295. }
  296. static void doReadSessionInfoSimple(bool msf, uint16_t allocationLength)
  297. {
  298. uint32_t len = sizeof(SessionTOC);
  299. memcpy(scsiDev.data, SessionTOC, len);
  300. if (msf)
  301. {
  302. scsiDev.data[0x0A] = 0x02;
  303. }
  304. if (len > allocationLength)
  305. {
  306. len = allocationLength;
  307. }
  308. scsiDev.dataLen = len;
  309. scsiDev.phase = DATA_IN;
  310. }
  311. static void doReadFullTOCSimple(uint8_t session, uint16_t allocationLength, bool useBCD)
  312. {
  313. // We only support session 1.
  314. if (session > 1)
  315. {
  316. scsiDev.status = CHECK_CONDITION;
  317. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  318. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  319. scsiDev.phase = STATUS;
  320. }
  321. else
  322. {
  323. uint32_t len = sizeof(FullTOC);
  324. memcpy(scsiDev.data, FullTOC, len);
  325. // update leadout position
  326. // consistent 2048-byte blocks makes this easier than bin/cue version
  327. uint32_t capacity = getScsiCapacity(
  328. scsiDev.target->cfg->sdSectorStart,
  329. scsiDev.target->liveCfg.bytesPerSector,
  330. scsiDev.target->cfg->scsiSectors);
  331. if (useBCD) {
  332. LBA2MSFBCD(capacity, &scsiDev.data[34], false);
  333. } else {
  334. LBA2MSF(capacity, &scsiDev.data[34], false);
  335. }
  336. if (len > allocationLength)
  337. {
  338. len = allocationLength;
  339. }
  340. scsiDev.dataLen = len;
  341. scsiDev.phase = DATA_IN;
  342. }
  343. }
  344. void doReadDiscInformationSimple(uint16_t allocationLength)
  345. {
  346. uint32_t len = sizeof(DiscInformation);
  347. memcpy(scsiDev.data, DiscInformation, len);
  348. if (len > allocationLength)
  349. {
  350. len = allocationLength;
  351. }
  352. scsiDev.dataLen = len;
  353. scsiDev.phase = DATA_IN;
  354. }
  355. /*********************************/
  356. /* TOC generation from cue sheet */
  357. /*********************************/
  358. // Fetch track info based on LBA
  359. static void getTrackFromLBA(CUEParser &parser, uint32_t lba, CUETrackInfo *result)
  360. {
  361. // Track info in case we have no .cue file
  362. result->file_mode = CUEFile_BINARY;
  363. result->track_mode = CUETrack_MODE1_2048;
  364. result->sector_length = 2048;
  365. result->track_number = 1;
  366. const CUETrackInfo *tmptrack;
  367. while ((tmptrack = parser.next_track()) != NULL)
  368. {
  369. if (tmptrack->track_start <= lba)
  370. {
  371. *result = *tmptrack;
  372. }
  373. else
  374. {
  375. break;
  376. }
  377. }
  378. }
  379. // Format track info read from cue sheet into the format used by ReadTOC command.
  380. // Refer to T10/1545-D MMC-4 Revision 5a, "Response Format 0000b: Formatted TOC"
  381. static void formatTrackInfo(const CUETrackInfo *track, uint8_t *dest, bool use_MSF_time)
  382. {
  383. uint8_t control_adr = 0x14; // Digital track
  384. if (track->track_mode == CUETrack_AUDIO)
  385. {
  386. control_adr = 0x10; // Audio track
  387. }
  388. dest[0] = 0; // Reserved
  389. dest[1] = control_adr;
  390. dest[2] = track->track_number;
  391. dest[3] = 0; // Reserved
  392. if (use_MSF_time)
  393. {
  394. // Time in minute-second-frame format
  395. dest[4] = 0;
  396. LBA2MSF(track->data_start, &dest[5], false);
  397. }
  398. else
  399. {
  400. // Time as logical block address
  401. dest[4] = (track->data_start >> 24) & 0xFF;
  402. dest[5] = (track->data_start >> 16) & 0xFF;
  403. dest[6] = (track->data_start >> 8) & 0xFF;
  404. dest[7] = (track->data_start >> 0) & 0xFF;
  405. }
  406. }
  407. // Load data from CUE sheet for the given device,
  408. // using the second half of scsiDev.data buffer for temporary storage.
  409. // Returns false if no cue sheet or it could not be opened.
  410. static bool loadCueSheet(image_config_t &img, CUEParser &parser)
  411. {
  412. if (!img.cuesheetfile.isOpen())
  413. {
  414. return false;
  415. }
  416. // Use second half of scsiDev.data as the buffer for cue sheet text
  417. size_t halfbufsize = sizeof(scsiDev.data) / 2;
  418. char *cuebuf = (char*)&scsiDev.data[halfbufsize];
  419. img.cuesheetfile.seek(0);
  420. int len = img.cuesheetfile.read(cuebuf, halfbufsize);
  421. if (len <= 0)
  422. {
  423. return false;
  424. }
  425. cuebuf[len] = '\0';
  426. parser = CUEParser(cuebuf);
  427. return true;
  428. }
  429. static void doReadTOC(bool MSF, uint8_t track, uint16_t allocationLength)
  430. {
  431. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  432. CUEParser parser;
  433. if (!loadCueSheet(img, parser))
  434. {
  435. // No CUE sheet, use hardcoded data
  436. return doReadTOCSimple(MSF, track, allocationLength);
  437. }
  438. // Format track info
  439. uint8_t *trackdata = &scsiDev.data[4];
  440. int trackcount = 0;
  441. int firsttrack = -1;
  442. CUETrackInfo lasttrack = {0};
  443. const CUETrackInfo *trackinfo;
  444. while ((trackinfo = parser.next_track()) != NULL)
  445. {
  446. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  447. lasttrack = *trackinfo;
  448. if (track <= trackinfo->track_number)
  449. {
  450. formatTrackInfo(trackinfo, &trackdata[8 * trackcount], MSF);
  451. trackcount += 1;
  452. }
  453. }
  454. // Format lead-out track info
  455. CUETrackInfo leadout = {};
  456. leadout.track_number = 0xAA;
  457. leadout.track_mode = (lasttrack.track_number != 0) ? lasttrack.track_mode : CUETrack_MODE1_2048;
  458. leadout.data_start = getLeadOutLBA(&lasttrack);
  459. formatTrackInfo(&leadout, &trackdata[8 * trackcount], MSF);
  460. trackcount += 1;
  461. // Format response header
  462. uint16_t toc_length = 2 + trackcount * 8;
  463. scsiDev.data[0] = toc_length >> 8;
  464. scsiDev.data[1] = toc_length & 0xFF;
  465. scsiDev.data[2] = firsttrack;
  466. scsiDev.data[3] = lasttrack.track_number;
  467. if (track != 0xAA && trackcount < 2)
  468. {
  469. // Unknown track requested
  470. scsiDev.status = CHECK_CONDITION;
  471. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  472. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  473. scsiDev.phase = STATUS;
  474. }
  475. else
  476. {
  477. uint32_t len = 2 + toc_length;
  478. if (len > allocationLength)
  479. {
  480. len = allocationLength;
  481. }
  482. scsiDev.dataLen = len;
  483. scsiDev.phase = DATA_IN;
  484. }
  485. }
  486. static void doReadSessionInfo(bool msf, uint16_t allocationLength)
  487. {
  488. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  489. CUEParser parser;
  490. if (!loadCueSheet(img, parser))
  491. {
  492. // No CUE sheet, use hardcoded data
  493. return doReadSessionInfoSimple(msf, allocationLength);
  494. }
  495. uint32_t len = sizeof(SessionTOC);
  496. memcpy(scsiDev.data, SessionTOC, len);
  497. // Replace first track info in the session table
  498. // based on data from CUE sheet.
  499. const CUETrackInfo *trackinfo = parser.next_track();
  500. if (trackinfo)
  501. {
  502. formatTrackInfo(trackinfo, &scsiDev.data[4], false);
  503. }
  504. if (len > allocationLength)
  505. {
  506. len = allocationLength;
  507. }
  508. scsiDev.dataLen = len;
  509. scsiDev.phase = DATA_IN;
  510. }
  511. // Format track info read from cue sheet into the format used by ReadFullTOC command.
  512. // Refer to T10/1545-D MMC-4 Revision 5a, "Response Format 0010b: Raw TOC"
  513. static void formatRawTrackInfo(const CUETrackInfo *track, uint8_t *dest, bool useBCD)
  514. {
  515. uint8_t control_adr = 0x14; // Digital track
  516. if (track->track_mode == CUETrack_AUDIO)
  517. {
  518. control_adr = 0x10; // Audio track
  519. }
  520. dest[0] = 0x01; // Session always 1
  521. dest[1] = control_adr;
  522. dest[2] = 0x00; // "TNO", always 0?
  523. dest[3] = track->track_number; // "POINT", contains track number
  524. // Next three are ATIME. The spec doesn't directly address how these
  525. // should be reported in the TOC, just giving a description of Q-channel
  526. // data from Red Book/ECMA-130. On all disks tested so far these are
  527. // given as 00/00/00.
  528. dest[4] = 0x00;
  529. dest[5] = 0x00;
  530. dest[6] = 0x00;
  531. dest[7] = 0; // HOUR
  532. if (useBCD) {
  533. LBA2MSFBCD(track->data_start, &dest[8], false);
  534. } else {
  535. LBA2MSF(track->data_start, &dest[8], false);
  536. }
  537. }
  538. static void doReadFullTOC(uint8_t session, uint16_t allocationLength, bool useBCD)
  539. {
  540. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  541. CUEParser parser;
  542. if (!loadCueSheet(img, parser))
  543. {
  544. // No CUE sheet, use hardcoded data
  545. return doReadFullTOCSimple(session, allocationLength, useBCD);
  546. }
  547. // We only support session 1.
  548. if (session > 1)
  549. {
  550. scsiDev.status = CHECK_CONDITION;
  551. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  552. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  553. scsiDev.phase = STATUS;
  554. return;
  555. }
  556. // Take the beginning of the hardcoded TOC as base
  557. uint32_t len = 4 + 11 * 3; // Header, A0, A1, A2
  558. memcpy(scsiDev.data, FullTOC, len);
  559. // Add track descriptors
  560. int trackcount = 0;
  561. int firsttrack = -1;
  562. CUETrackInfo lasttrack = {0};
  563. const CUETrackInfo *trackinfo;
  564. while ((trackinfo = parser.next_track()) != NULL)
  565. {
  566. if (firsttrack < 0)
  567. {
  568. firsttrack = trackinfo->track_number;
  569. if (trackinfo->track_mode == CUETrack_AUDIO)
  570. {
  571. scsiDev.data[5] = 0x10;
  572. }
  573. }
  574. lasttrack = *trackinfo;
  575. formatRawTrackInfo(trackinfo, &scsiDev.data[len], useBCD);
  576. trackcount += 1;
  577. len += 11;
  578. }
  579. // First and last track numbers
  580. scsiDev.data[12] = firsttrack;
  581. if (lasttrack.track_number != 0)
  582. {
  583. scsiDev.data[23] = lasttrack.track_number;
  584. if (lasttrack.track_mode == CUETrack_AUDIO)
  585. {
  586. scsiDev.data[16] = 0x10;
  587. scsiDev.data[27] = 0x10;
  588. }
  589. }
  590. // Leadout track position
  591. if (useBCD) {
  592. LBA2MSFBCD(getLeadOutLBA(&lasttrack), &scsiDev.data[34], false);
  593. } else {
  594. LBA2MSF(getLeadOutLBA(&lasttrack), &scsiDev.data[34], false);
  595. }
  596. // Correct the record length in header
  597. uint16_t toclen = len - 2;
  598. scsiDev.data[0] = toclen >> 8;
  599. scsiDev.data[1] = toclen & 0xFF;
  600. if (len > allocationLength)
  601. {
  602. len = allocationLength;
  603. }
  604. scsiDev.dataLen = len;
  605. scsiDev.phase = DATA_IN;
  606. }
  607. // SCSI-3 MMC Read Header command, seems to be deprecated in later standards.
  608. // Refer to ANSI X3.304-1997
  609. // The spec is vague, but based on experimentation with a Matshita drive this
  610. // command should return the sector header absolute time (see ECMA-130 21).
  611. // Given 2048-byte block sizes this effectively is 1:1 with the provided LBA.
  612. void doReadHeader(bool MSF, uint32_t lba, uint16_t allocationLength)
  613. {
  614. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  615. #if ENABLE_AUDIO_OUTPUT
  616. // terminate audio playback if active on this target (Annex C)
  617. audio_stop(img.scsiId & 7);
  618. #endif
  619. uint8_t mode = 1;
  620. CUEParser parser;
  621. if (loadCueSheet(img, parser))
  622. {
  623. // Search the track with the requested LBA
  624. CUETrackInfo trackinfo = {};
  625. getTrackFromLBA(parser, lba, &trackinfo);
  626. // Track mode (audio / data)
  627. if (trackinfo.track_mode == CUETrack_AUDIO)
  628. {
  629. scsiDev.data[0] = 0;
  630. }
  631. }
  632. scsiDev.data[0] = mode;
  633. scsiDev.data[1] = 0; // reserved
  634. scsiDev.data[2] = 0; // reserved
  635. scsiDev.data[3] = 0; // reserved
  636. // Track start
  637. if (MSF)
  638. {
  639. scsiDev.data[4] = 0;
  640. LBA2MSF(lba, &scsiDev.data[5], false);
  641. }
  642. else
  643. {
  644. scsiDev.data[4] = (lba >> 24) & 0xFF;
  645. scsiDev.data[5] = (lba >> 16) & 0xFF;
  646. scsiDev.data[6] = (lba >> 8) & 0xFF;
  647. scsiDev.data[7] = (lba >> 0) & 0xFF;
  648. }
  649. uint8_t len = 8;
  650. if (len > allocationLength)
  651. {
  652. len = allocationLength;
  653. }
  654. scsiDev.dataLen = len;
  655. scsiDev.phase = DATA_IN;
  656. }
  657. void doReadDiscInformation(uint16_t allocationLength)
  658. {
  659. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  660. CUEParser parser;
  661. if (!loadCueSheet(img, parser))
  662. {
  663. // No CUE sheet, use hardcoded data
  664. return doReadDiscInformationSimple(allocationLength);
  665. }
  666. // Take the hardcoded header as base
  667. uint32_t len = sizeof(DiscInformation);
  668. memcpy(scsiDev.data, DiscInformation, len);
  669. // Find first and last track number
  670. int firsttrack = -1;
  671. int lasttrack = -1;
  672. const CUETrackInfo *trackinfo;
  673. while ((trackinfo = parser.next_track()) != NULL)
  674. {
  675. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  676. lasttrack = trackinfo->track_number;
  677. }
  678. scsiDev.data[3] = firsttrack;
  679. scsiDev.data[5] = firsttrack;
  680. scsiDev.data[6] = lasttrack;
  681. if (len > allocationLength)
  682. {
  683. len = allocationLength;
  684. }
  685. scsiDev.dataLen = len;
  686. scsiDev.phase = DATA_IN;
  687. }
  688. /****************************************/
  689. /* CUE sheet check at image load time */
  690. /****************************************/
  691. bool cdromValidateCueSheet(image_config_t &img)
  692. {
  693. CUEParser parser;
  694. if (!loadCueSheet(img, parser))
  695. {
  696. return false;
  697. }
  698. const CUETrackInfo *trackinfo;
  699. int trackcount = 0;
  700. while ((trackinfo = parser.next_track()) != NULL)
  701. {
  702. trackcount++;
  703. if (trackinfo->track_mode != CUETrack_AUDIO &&
  704. trackinfo->track_mode != CUETrack_MODE1_2048 &&
  705. trackinfo->track_mode != CUETrack_MODE1_2352)
  706. {
  707. logmsg("---- Warning: track ", trackinfo->track_number, " has unsupported mode ", (int)trackinfo->track_mode);
  708. }
  709. if (trackinfo->file_mode != CUEFile_BINARY)
  710. {
  711. logmsg("---- Unsupported CUE data file mode ", (int)trackinfo->file_mode);
  712. }
  713. }
  714. if (trackcount == 0)
  715. {
  716. logmsg("---- Opened cue sheet but no valid tracks found");
  717. return false;
  718. }
  719. logmsg("---- Cue sheet loaded with ", (int)trackcount, " tracks");
  720. return true;
  721. }
  722. /**************************************/
  723. /* Ejection and image switching logic */
  724. /**************************************/
  725. void cdromPerformEject(image_config_t &img)
  726. {
  727. uint8_t target = img.scsiId & 7;
  728. #if ENABLE_AUDIO_OUTPUT
  729. // terminate audio playback if active on this target (MMC-1 Annex C)
  730. audio_stop(target);
  731. #endif
  732. dbgmsg("------ CDROM open tray on ID ", (int)target);
  733. img.ejected = true;
  734. img.cdrom_events = 3; // Media removal
  735. }
  736. // Reinsert any ejected CDROMs on reboot
  737. void cdromReinsertFirstImage(image_config_t &img)
  738. {
  739. if (img.image_index > 0)
  740. {
  741. // Multiple images for this drive, force restart from first one
  742. dbgmsg("---- Restarting from first CD-ROM image");
  743. img.image_index = 9;
  744. cdromSwitchNextImage(img);
  745. }
  746. else if (img.ejected)
  747. {
  748. // Reinsert the single image
  749. dbgmsg("---- Closing CD-ROM tray");
  750. img.ejected = false;
  751. img.cdrom_events = 2; // New media
  752. }
  753. }
  754. // Check if we have multiple CD-ROM images to cycle when drive is ejected.
  755. bool cdromSwitchNextImage(image_config_t &img)
  756. {
  757. // Check if we have a next image to load, so that drive is closed next time the host asks.
  758. img.image_index++;
  759. char filename[MAX_FILE_PATH];
  760. int target_idx = img.scsiId & 7;
  761. if (!scsiDiskGetImageNameFromConfig(img, filename, sizeof(filename)))
  762. {
  763. img.image_index = 0;
  764. scsiDiskGetImageNameFromConfig(img, filename, sizeof(filename));
  765. }
  766. #ifdef ENABLE_AUDIO_OUTPUT
  767. // if in progress for this device, terminate audio playback immediately (Annex C)
  768. audio_stop(target_idx);
  769. // Reset position tracking for the new image
  770. audio_get_status_code(target_idx); // trash audio status code
  771. #endif
  772. if (filename[0] != '\0')
  773. {
  774. logmsg("Switching to next CD-ROM image for ", target_idx, ": ", filename);
  775. img.file.close();
  776. bool status = scsiDiskOpenHDDImage(target_idx, filename, target_idx, 0, 2048);
  777. if (status)
  778. {
  779. img.ejected = false;
  780. img.cdrom_events = 2; // New media
  781. return true;
  782. }
  783. }
  784. return false;
  785. }
  786. static void doGetEventStatusNotification(bool immed)
  787. {
  788. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  789. if (!immed)
  790. {
  791. // Asynchronous notification not supported
  792. scsiDev.status = CHECK_CONDITION;
  793. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  794. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  795. scsiDev.phase = STATUS;
  796. }
  797. else if (img.cdrom_events)
  798. {
  799. scsiDev.data[0] = 0;
  800. scsiDev.data[1] = 6; // EventDataLength
  801. scsiDev.data[2] = 0x04; // Media status events
  802. scsiDev.data[3] = 0x04; // Supported events
  803. scsiDev.data[4] = img.cdrom_events;
  804. scsiDev.data[5] = 0x01; // Power status
  805. scsiDev.data[6] = 0; // Start slot
  806. scsiDev.data[7] = 0; // End slot
  807. scsiDev.dataLen = 8;
  808. scsiDev.phase = DATA_IN;
  809. img.cdrom_events = 0;
  810. if (img.ejected)
  811. {
  812. // We are now reporting to host that the drive is open.
  813. // Simulate a "close" for next time the host polls.
  814. cdromSwitchNextImage(img);
  815. }
  816. }
  817. else
  818. {
  819. scsiDev.data[0] = 0;
  820. scsiDev.data[1] = 2; // EventDataLength
  821. scsiDev.data[2] = 0x00; // Media status events
  822. scsiDev.data[3] = 0x04; // Supported events
  823. scsiDev.dataLen = 4;
  824. scsiDev.phase = DATA_IN;
  825. }
  826. }
  827. /**************************************/
  828. /* CD-ROM audio playback */
  829. /**************************************/
  830. void cdromGetAudioPlaybackStatus(uint8_t *status, uint32_t *current_lba, bool current_only)
  831. {
  832. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  833. #ifdef ENABLE_AUDIO_OUTPUT
  834. if (status) {
  835. uint8_t target = img.scsiId & 7;
  836. if (current_only) {
  837. *status = audio_is_playing(target) ? 1 : 0;
  838. } else {
  839. *status = (uint8_t) audio_get_status_code(target);
  840. }
  841. }
  842. #else
  843. if (status) *status = 0; // audio status code for 'unsupported/invalid' and not-playing indicator
  844. #endif
  845. if (current_lba)
  846. {
  847. if (img.file.isOpen()) {
  848. *current_lba = img.file.position() / 2352;
  849. } else {
  850. *current_lba = 0;
  851. }
  852. }
  853. }
  854. static void doPlayAudio(uint32_t lba, uint32_t length)
  855. {
  856. #ifdef ENABLE_AUDIO_OUTPUT
  857. dbgmsg("------ CD-ROM Play Audio request at ", lba, " for ", length, " sectors");
  858. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  859. uint8_t target_id = img.scsiId & 7;
  860. // Per Annex C terminate playback immediately if already in progress on
  861. // the current target. Non-current targets may also get their audio
  862. // interrupted later due to hardware limitations
  863. audio_stop(img.scsiId & 7);
  864. // if transfer length is zero no audio playback happens.
  865. // don't treat as an error per SCSI-2; handle via short-circuit
  866. if (length == 0)
  867. {
  868. scsiDev.status = 0;
  869. scsiDev.phase = STATUS;
  870. return;
  871. }
  872. // if actual playback is requested perform steps to verify prior to playback
  873. CUEParser parser;
  874. if (loadCueSheet(img, parser))
  875. {
  876. CUETrackInfo trackinfo = {};
  877. getTrackFromLBA(parser, lba, &trackinfo);
  878. if (lba == 0xFFFFFFFF)
  879. {
  880. // request to start playback from 'current position'
  881. lba = img.file.position() / 2352;
  882. }
  883. uint64_t offset = trackinfo.file_offset
  884. + trackinfo.sector_length * (lba - trackinfo.track_start);
  885. dbgmsg("------ Play audio CD: ", (int)length, " sectors starting at ", (int)lba,
  886. ", track number ", trackinfo.track_number, ", data offset in file ", (int)offset);
  887. if (trackinfo.track_mode != CUETrack_AUDIO)
  888. {
  889. dbgmsg("---- Host tried audio playback on track type ", (int)trackinfo.track_mode);
  890. scsiDev.status = CHECK_CONDITION;
  891. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  892. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  893. scsiDev.phase = STATUS;
  894. return;
  895. }
  896. // playback request appears to be sane, so perform it
  897. // see earlier note for context on the block length below
  898. if (!audio_play(target_id, &(img.file), offset,
  899. offset + length * trackinfo.sector_length, false))
  900. {
  901. // Underlying data/media error? Fake a disk scratch, which should
  902. // be a condition most CD-DA players are expecting
  903. scsiDev.status = CHECK_CONDITION;
  904. scsiDev.target->sense.code = MEDIUM_ERROR;
  905. scsiDev.target->sense.asc = 0x1106; // CIRC UNRECOVERED ERROR
  906. scsiDev.phase = STATUS;
  907. return;
  908. }
  909. scsiDev.status = 0;
  910. scsiDev.phase = STATUS;
  911. }
  912. else
  913. {
  914. // virtual drive supports audio, just not with this disk image
  915. dbgmsg("---- Request to play audio on non-audio image");
  916. scsiDev.status = CHECK_CONDITION;
  917. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  918. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  919. scsiDev.phase = STATUS;
  920. }
  921. #else
  922. dbgmsg("---- Target does not support audio playback");
  923. // per SCSI-2, targets not supporting audio respond to zero-length
  924. // PLAY AUDIO commands with ILLEGAL REQUEST; this seems to be a check
  925. // performed by at least some audio playback software
  926. scsiDev.status = CHECK_CONDITION;
  927. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  928. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  929. scsiDev.phase = STATUS;
  930. #endif
  931. }
  932. static void doPauseResumeAudio(bool resume)
  933. {
  934. #ifdef ENABLE_AUDIO_OUTPUT
  935. logmsg("------ CD-ROM ", resume ? "resume" : "pause", " audio playback");
  936. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  937. uint8_t target_id = img.scsiId & 7;
  938. if (audio_is_playing(target_id))
  939. {
  940. audio_set_paused(target_id, !resume);
  941. scsiDev.status = 0;
  942. scsiDev.phase = STATUS;
  943. }
  944. else
  945. {
  946. scsiDev.status = CHECK_CONDITION;
  947. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  948. scsiDev.target->sense.asc = 0x2C00; // COMMAND SEQUENCE ERROR
  949. scsiDev.phase = STATUS;
  950. }
  951. #else
  952. dbgmsg("---- Target does not support audio pausing");
  953. scsiDev.status = CHECK_CONDITION;
  954. scsiDev.target->sense.code = ILLEGAL_REQUEST; // assumed from PLAY AUDIO(10)
  955. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  956. scsiDev.phase = STATUS;
  957. #endif
  958. }
  959. static void doStopAudio()
  960. {
  961. dbgmsg("------ CD-ROM Stop Audio request");
  962. #ifdef ENABLE_AUDIO_OUTPUT
  963. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  964. uint8_t target_id = img.scsiId & 7;
  965. audio_stop(target_id);
  966. #endif
  967. }
  968. static void doMechanismStatus(uint16_t allocation_length)
  969. {
  970. uint8_t *buf = scsiDev.data;
  971. uint8_t status;
  972. uint32_t lba;
  973. cdromGetAudioPlaybackStatus(&status, &lba, true);
  974. *buf++ = 0x00; // No fault state
  975. *buf++ = (status) ? 0x20 : 0x00; // Currently playing?
  976. *buf++ = (lba >> 16) & 0xFF;
  977. *buf++ = (lba >> 8) & 0xFF;
  978. *buf++ = (lba >> 0) & 0xFF;
  979. *buf++ = 0; // No CD changer
  980. *buf++ = 0;
  981. *buf++ = 0;
  982. int len = 8;
  983. if (len > allocation_length) len = allocation_length;
  984. scsiDev.dataLen = len;
  985. scsiDev.phase = DATA_IN;
  986. }
  987. /*******************************************/
  988. /* CD-ROM data reading in low level format */
  989. /*******************************************/
  990. static void doReadCD(uint32_t lba, uint32_t length, uint8_t sector_type,
  991. uint8_t main_channel, uint8_t sub_channel, bool data_only)
  992. {
  993. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  994. #if ENABLE_AUDIO_OUTPUT
  995. // terminate audio playback if active on this target (Annex C)
  996. audio_stop(img.scsiId & 7);
  997. #endif
  998. CUEParser parser;
  999. if (!loadCueSheet(img, parser)
  1000. && (sector_type == 0 || sector_type == 2)
  1001. && main_channel == 0x10 && sub_channel == 0)
  1002. {
  1003. // Simple case, return sector data directly
  1004. scsiDiskStartRead(lba, length);
  1005. return;
  1006. }
  1007. // Search the track with the requested LBA
  1008. // Supplies dummy data if no cue sheet is active.
  1009. CUETrackInfo trackinfo = {};
  1010. getTrackFromLBA(parser, lba, &trackinfo);
  1011. // Figure out the data offset in the file
  1012. uint64_t offset = trackinfo.file_offset + trackinfo.sector_length * (lba - trackinfo.track_start);
  1013. dbgmsg("------ Read CD: ", (int)length, " sectors starting at ", (int)lba,
  1014. ", track number ", trackinfo.track_number, ", sector size ", (int)trackinfo.sector_length,
  1015. ", main channel ", main_channel, ", sub channel ", sub_channel,
  1016. ", data offset in file ", (int)offset);
  1017. // Ensure read is not out of range of the image
  1018. uint64_t readend = offset + trackinfo.sector_length * length;
  1019. if (readend > img.file.size())
  1020. {
  1021. logmsg("WARNING: Host attempted CD read at sector ", lba, "+", length,
  1022. ", exceeding image size ", img.file.size());
  1023. scsiDev.status = CHECK_CONDITION;
  1024. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1025. scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1026. scsiDev.phase = STATUS;
  1027. return;
  1028. }
  1029. // Verify sector type
  1030. if (sector_type != 0)
  1031. {
  1032. bool sector_type_ok = false;
  1033. if (sector_type == 1 && trackinfo.track_mode == CUETrack_AUDIO)
  1034. {
  1035. sector_type_ok = true;
  1036. }
  1037. else if (sector_type == 2 && trackinfo.track_mode == CUETrack_MODE1_2048)
  1038. {
  1039. sector_type_ok = true;
  1040. }
  1041. if (!sector_type_ok)
  1042. {
  1043. dbgmsg("---- Failed sector type check, host requested ", (int)sector_type, " CUE file has ", (int)trackinfo.track_mode);
  1044. scsiDev.status = CHECK_CONDITION;
  1045. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1046. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1047. scsiDev.phase = STATUS;
  1048. return;
  1049. }
  1050. }
  1051. // Select fields to transfer
  1052. // Refer to table 351 in T10/1545-D MMC-4 Revision 5a
  1053. // Only the mandatory cases are supported.
  1054. int sector_length = 0;
  1055. int skip_begin = 0;
  1056. bool add_fake_headers = false;
  1057. if (main_channel == 0)
  1058. {
  1059. // No actual data requested, just sector type check or subchannel
  1060. sector_length = 0;
  1061. }
  1062. else if (trackinfo.track_mode == CUETrack_AUDIO)
  1063. {
  1064. // Transfer whole 2352 byte audio sectors from file to host
  1065. sector_length = 2352;
  1066. }
  1067. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && main_channel == 0x10)
  1068. {
  1069. // Transfer whole 2048 byte data sectors from file to host
  1070. sector_length = 2048;
  1071. }
  1072. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && (main_channel & 0xB8) == 0xB8)
  1073. {
  1074. // Transfer 2048 bytes of data from file and fake the headers
  1075. sector_length = 2048;
  1076. add_fake_headers = true;
  1077. dbgmsg("------ Host requested ECC data but image file lacks it, replacing with zeros");
  1078. }
  1079. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && main_channel == 0x10)
  1080. {
  1081. // Transfer the 2048 byte payload of data sector to host.
  1082. sector_length = 2048;
  1083. skip_begin = 16;
  1084. }
  1085. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && (main_channel & 0xB8) == 0xB8)
  1086. {
  1087. // Transfer whole 2352 byte data sector with ECC to host
  1088. sector_length = 2352;
  1089. }
  1090. else
  1091. {
  1092. dbgmsg("---- Unsupported channel request for track type ", (int)trackinfo.track_mode);
  1093. scsiDev.status = CHECK_CONDITION;
  1094. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1095. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1096. scsiDev.phase = STATUS;
  1097. return;
  1098. }
  1099. if (data_only && sector_length != 2048)
  1100. {
  1101. dbgmsg("------ Host tried to read non-data sector with standard READ command");
  1102. scsiDev.status = CHECK_CONDITION;
  1103. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1104. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1105. scsiDev.phase = STATUS;
  1106. return;
  1107. }
  1108. bool field_q_subchannel = false;
  1109. if (sub_channel == 2)
  1110. {
  1111. // Include position information in Q subchannel
  1112. field_q_subchannel = true;
  1113. }
  1114. else if (sub_channel != 0)
  1115. {
  1116. dbgmsg("---- Unsupported subchannel request");
  1117. scsiDev.status = CHECK_CONDITION;
  1118. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1119. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1120. scsiDev.phase = STATUS;
  1121. return;
  1122. }
  1123. scsiDev.phase = DATA_IN;
  1124. scsiDev.dataLen = 0;
  1125. scsiDev.dataPtr = 0;
  1126. scsiEnterPhase(DATA_IN);
  1127. // Use two buffers alternately for formatting sector data
  1128. uint32_t result_length = sector_length + (field_q_subchannel ? 16 : 0) + (add_fake_headers ? 304 : 0);
  1129. uint8_t *buf0 = scsiDev.data;
  1130. uint8_t *buf1 = scsiDev.data + result_length;
  1131. // Format the sectors for transfer
  1132. for (uint32_t idx = 0; idx < length; idx++)
  1133. {
  1134. platform_poll();
  1135. diskEjectButtonUpdate(false);
  1136. img.file.seek(offset + idx * trackinfo.sector_length + skip_begin);
  1137. // Verify that previous write using this buffer has finished
  1138. uint8_t *buf = ((idx & 1) ? buf1 : buf0);
  1139. uint8_t *bufstart = buf;
  1140. uint32_t start = millis();
  1141. while (!scsiIsWriteFinished(buf + result_length - 1) && !scsiDev.resetFlag)
  1142. {
  1143. if ((uint32_t)(millis() - start) > 5000)
  1144. {
  1145. logmsg("doReadCD() timeout waiting for previous to finish");
  1146. scsiDev.resetFlag = 1;
  1147. }
  1148. platform_poll();
  1149. diskEjectButtonUpdate(false);
  1150. }
  1151. if (scsiDev.resetFlag) break;
  1152. if (add_fake_headers)
  1153. {
  1154. // 12-byte data sector sync pattern
  1155. *buf++ = 0x00;
  1156. for (int i = 0; i < 10; i++)
  1157. {
  1158. *buf++ = 0xFF;
  1159. }
  1160. *buf++ = 0x00;
  1161. // 4-byte data sector header
  1162. LBA2MSFBCD(lba + idx, buf, false);
  1163. buf += 3;
  1164. *buf++ = 0x01; // Mode 1
  1165. }
  1166. if (sector_length > 0)
  1167. {
  1168. // User data
  1169. img.file.read(buf, sector_length);
  1170. buf += sector_length;
  1171. }
  1172. if (add_fake_headers)
  1173. {
  1174. // 288 bytes of ECC
  1175. memset(buf, 0, 288);
  1176. buf += 288;
  1177. }
  1178. if (field_q_subchannel)
  1179. {
  1180. // Formatted Q subchannel data
  1181. // Refer to table 354 in T10/1545-D MMC-4 Revision 5a
  1182. // and ECMA-130 22.3.3
  1183. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14); // Control & ADR
  1184. *buf++ = trackinfo.track_number;
  1185. *buf++ = (lba + idx >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1186. int32_t rel = (int32_t)(lba + idx) - (int32_t)trackinfo.data_start;
  1187. LBA2MSF(rel, buf, true); buf += 3;
  1188. *buf++ = 0;
  1189. LBA2MSF(lba + idx, buf, false); buf += 3;
  1190. *buf++ = 0; *buf++ = 0; // CRC (optional)
  1191. *buf++ = 0; *buf++ = 0; *buf++ = 0; // (pad)
  1192. *buf++ = 0; // No P subchannel
  1193. }
  1194. assert(buf == bufstart + result_length);
  1195. scsiStartWrite(bufstart, result_length);
  1196. }
  1197. scsiFinishWrite();
  1198. scsiDev.status = 0;
  1199. scsiDev.phase = STATUS;
  1200. }
  1201. static void doReadSubchannel(bool time, bool subq, uint8_t parameter, uint8_t track_number, uint16_t allocation_length)
  1202. {
  1203. uint8_t *buf = scsiDev.data;
  1204. if (parameter == 0x01)
  1205. {
  1206. uint8_t audiostatus;
  1207. uint32_t lba;
  1208. cdromGetAudioPlaybackStatus(&audiostatus, &lba, false);
  1209. dbgmsg("------ Get audio playback position: status ", (int)audiostatus, " lba ", (int)lba);
  1210. // Fetch current track info
  1211. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1212. CUEParser parser;
  1213. CUETrackInfo trackinfo = {};
  1214. loadCueSheet(img, parser);
  1215. getTrackFromLBA(parser, lba, &trackinfo);
  1216. // Request sub channel data at current playback position
  1217. *buf++ = 0; // Reserved
  1218. *buf++ = audiostatus;
  1219. int len;
  1220. if (subq)
  1221. {
  1222. len = 12;
  1223. *buf++ = 0; // Subchannel data length (MSB)
  1224. *buf++ = len; // Subchannel data length (LSB)
  1225. *buf++ = 0x01; // Subchannel data format
  1226. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14);
  1227. *buf++ = trackinfo.track_number;
  1228. *buf++ = (lba >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1229. if (time)
  1230. {
  1231. *buf++ = 0;
  1232. LBA2MSF(lba, buf, false);
  1233. dbgmsg("------ ABS M ", *buf, " S ", *(buf+1), " F ", *(buf+2));
  1234. buf += 3;
  1235. }
  1236. else
  1237. {
  1238. *buf++ = (lba >> 24) & 0xFF; // Absolute block address
  1239. *buf++ = (lba >> 16) & 0xFF;
  1240. *buf++ = (lba >> 8) & 0xFF;
  1241. *buf++ = (lba >> 0) & 0xFF;
  1242. }
  1243. int32_t relpos = (int32_t)lba - (int32_t)trackinfo.data_start;
  1244. if (time)
  1245. {
  1246. *buf++ = 0;
  1247. LBA2MSF(relpos, buf, true);
  1248. dbgmsg("------ REL M ", *buf, " S ", *(buf+1), " F ", *(buf+2));
  1249. buf += 3;
  1250. }
  1251. else
  1252. {
  1253. uint32_t urelpos = relpos;
  1254. *buf++ = (urelpos >> 24) & 0xFF; // Track relative position (may be negative)
  1255. *buf++ = (urelpos >> 16) & 0xFF;
  1256. *buf++ = (urelpos >> 8) & 0xFF;
  1257. *buf++ = (urelpos >> 0) & 0xFF;
  1258. }
  1259. }
  1260. else
  1261. {
  1262. len = 0;
  1263. *buf++ = 0;
  1264. *buf++ = 0;
  1265. }
  1266. len += 4;
  1267. if (len > allocation_length) len = allocation_length;
  1268. scsiDev.dataLen = len;
  1269. scsiDev.phase = DATA_IN;
  1270. }
  1271. else
  1272. {
  1273. dbgmsg("---- Unsupported subchannel request");
  1274. scsiDev.status = CHECK_CONDITION;
  1275. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1276. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1277. scsiDev.phase = STATUS;
  1278. return;
  1279. }
  1280. }
  1281. static bool doReadCapacity(uint32_t lba, uint8_t pmi)
  1282. {
  1283. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1284. CUEParser parser;
  1285. if (!loadCueSheet(img, parser))
  1286. {
  1287. // basic image, let the disk handler resolve
  1288. return false;
  1289. }
  1290. // find the last track on the disk
  1291. CUETrackInfo lasttrack = {0};
  1292. const CUETrackInfo *trackinfo;
  1293. while ((trackinfo = parser.next_track()) != NULL)
  1294. {
  1295. lasttrack = *trackinfo;
  1296. }
  1297. uint32_t capacity = 0;
  1298. if (lasttrack.track_number != 0)
  1299. {
  1300. capacity = getLeadOutLBA(&lasttrack);
  1301. capacity--; // shift to last addressable LBA
  1302. if (pmi && lba && lba > capacity)
  1303. {
  1304. // MMC technically specifies that PMI should be zero, but SCSI-2 allows this
  1305. // potentially consider treating either out-of-bounds or PMI set as an error
  1306. // for now just ignore this
  1307. }
  1308. dbgmsg("----- Reporting capacity as ", capacity);
  1309. }
  1310. else
  1311. {
  1312. logmsg("WARNING: unable to find capacity, no cue file found for ID ", img.scsiId);
  1313. }
  1314. scsiDev.data[0] = capacity >> 24;
  1315. scsiDev.data[1] = capacity >> 16;
  1316. scsiDev.data[2] = capacity >> 8;
  1317. scsiDev.data[3] = capacity;
  1318. scsiDev.data[4] = 0;
  1319. scsiDev.data[5] = 0;
  1320. scsiDev.data[6] = 0x08; // rest of code assumes 2048 here
  1321. scsiDev.data[7] = 0x00;
  1322. scsiDev.dataLen = 8;
  1323. scsiDev.phase = DATA_IN;
  1324. return true;
  1325. }
  1326. /**************************************/
  1327. /* CD-ROM command dispatching */
  1328. /**************************************/
  1329. // Handle direct-access scsi device commands
  1330. extern "C" int scsiCDRomCommand()
  1331. {
  1332. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1333. int commandHandled = 1;
  1334. uint8_t command = scsiDev.cdb[0];
  1335. if (command == 0x1B)
  1336. {
  1337. if ((scsiDev.cdb[4] & 2))
  1338. {
  1339. // CD-ROM load & eject
  1340. int start = scsiDev.cdb[4] & 1;
  1341. if (start)
  1342. {
  1343. dbgmsg("------ CDROM close tray on ID ", (int)(img.scsiId & 7));
  1344. img.ejected = false;
  1345. img.cdrom_events = 2; // New media
  1346. }
  1347. else
  1348. {
  1349. cdromPerformEject(img);
  1350. }
  1351. }
  1352. else
  1353. {
  1354. // flow through to disk handler
  1355. commandHandled = 0;
  1356. }
  1357. }
  1358. else if (command == 0x25)
  1359. {
  1360. // READ CAPACITY
  1361. uint8_t reladdr = scsiDev.cdb[1] & 1;
  1362. uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
  1363. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1364. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1365. scsiDev.cdb[5];
  1366. uint8_t pmi = scsiDev.cdb[8] & 1;
  1367. // allow PMI as long as LBA is specified, this is permitted in SCSI-2
  1368. // we don't link commands, do not allow RELADDR
  1369. if ((!pmi && lba != 0) || reladdr)
  1370. {
  1371. scsiDev.status = CHECK_CONDITION;
  1372. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1373. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1374. scsiDev.phase = STATUS;
  1375. }
  1376. else
  1377. {
  1378. if (!doReadCapacity(lba, pmi))
  1379. {
  1380. // allow disk handler to resolve this one
  1381. commandHandled = 0;
  1382. }
  1383. }
  1384. }
  1385. else if (command == 0x43)
  1386. {
  1387. // CD-ROM Read TOC
  1388. bool MSF = (scsiDev.cdb[1] & 0x02);
  1389. uint8_t track = scsiDev.cdb[6];
  1390. uint16_t allocationLength =
  1391. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1392. scsiDev.cdb[8];
  1393. // The "format" field is reserved for SCSI-2
  1394. uint8_t format = scsiDev.cdb[2] & 0x0F;
  1395. // Matshita SCSI-2 drives appear to use the high 2 bits of the CDB
  1396. // control byte to switch on session info (0x40) and full toc (0x80)
  1397. // responses that are very similar to the standard formats described
  1398. // in MMC-1. These vendor flags must have been pretty common because
  1399. // even a modern SATA drive (ASUS DRW-24B1ST j) responds to them
  1400. // (though it always replies in hex rather than bcd)
  1401. //
  1402. // The session information page is identical to MMC. The full TOC page
  1403. // is identical _except_ it returns addresses in bcd rather than hex.
  1404. bool useBCD = false;
  1405. if (format == 0 && scsiDev.cdb[9] == 0x80)
  1406. {
  1407. format = 2;
  1408. useBCD = true;
  1409. }
  1410. else if (format == 0 && scsiDev.cdb[9] == 0x40)
  1411. {
  1412. format = 1;
  1413. }
  1414. switch (format)
  1415. {
  1416. case 0: doReadTOC(MSF, track, allocationLength); break; // SCSI-2
  1417. case 1: doReadSessionInfo(MSF, allocationLength); break; // MMC2
  1418. case 2: doReadFullTOC(track, allocationLength, useBCD); break; // MMC2
  1419. default:
  1420. {
  1421. scsiDev.status = CHECK_CONDITION;
  1422. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1423. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1424. scsiDev.phase = STATUS;
  1425. }
  1426. }
  1427. }
  1428. else if (command == 0x44)
  1429. {
  1430. // CD-ROM Read Header
  1431. bool MSF = (scsiDev.cdb[1] & 0x02);
  1432. uint32_t lba = 0; // IGNORED for now
  1433. uint16_t allocationLength =
  1434. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1435. scsiDev.cdb[8];
  1436. doReadHeader(MSF, lba, allocationLength);
  1437. }
  1438. else if (command == 0x51)
  1439. {
  1440. uint16_t allocationLength =
  1441. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1442. scsiDev.cdb[8];
  1443. doReadDiscInformation(allocationLength);
  1444. }
  1445. else if (command == 0x4A)
  1446. {
  1447. // Get event status notifications (media change notifications)
  1448. bool immed = scsiDev.cdb[1] & 1;
  1449. doGetEventStatusNotification(immed);
  1450. }
  1451. else if (command == 0x45)
  1452. {
  1453. // PLAY AUDIO (10)
  1454. uint32_t lba =
  1455. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1456. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1457. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1458. scsiDev.cdb[5];
  1459. uint32_t blocks =
  1460. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1461. scsiDev.cdb[8];
  1462. doPlayAudio(lba, blocks);
  1463. }
  1464. else if (command == 0xA5)
  1465. {
  1466. // PLAY AUDIO (12)
  1467. uint32_t lba =
  1468. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1469. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1470. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1471. scsiDev.cdb[5];
  1472. uint32_t blocks =
  1473. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1474. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1475. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1476. scsiDev.cdb[9];
  1477. doPlayAudio(lba, blocks);
  1478. }
  1479. else if (command == 0x47)
  1480. {
  1481. // PLAY AUDIO (MSF)
  1482. uint32_t start = MSF2LBA(scsiDev.cdb[3], scsiDev.cdb[4], scsiDev.cdb[5], false);
  1483. uint32_t end = MSF2LBA(scsiDev.cdb[6], scsiDev.cdb[7], scsiDev.cdb[8], false);
  1484. uint32_t lba = start;
  1485. if (scsiDev.cdb[3] == 0xFF
  1486. && scsiDev.cdb[4] == 0xFF
  1487. && scsiDev.cdb[5] == 0xFF)
  1488. {
  1489. // request to start playback from 'current position'
  1490. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1491. lba = img.file.position() / 2352;
  1492. }
  1493. uint32_t length = end - lba;
  1494. doPlayAudio(lba, length);
  1495. }
  1496. else if (command == 0x4B)
  1497. {
  1498. // PAUSE/RESUME AUDIO
  1499. doPauseResumeAudio(scsiDev.cdb[8] & 1);
  1500. }
  1501. else if (command == 0xBD)
  1502. {
  1503. // Mechanism status
  1504. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[8]) << 8) + scsiDev.cdb[9];
  1505. doMechanismStatus(allocationLength);
  1506. }
  1507. else if (command == 0xBB)
  1508. {
  1509. // Set CD speed (just ignored)
  1510. scsiDev.status = 0;
  1511. scsiDev.phase = STATUS;
  1512. }
  1513. else if (command == 0xBE)
  1514. {
  1515. // ReadCD (in low level format)
  1516. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1517. uint32_t lba =
  1518. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1519. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1520. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1521. scsiDev.cdb[5];
  1522. uint32_t blocks =
  1523. (((uint32_t) scsiDev.cdb[6]) << 16) +
  1524. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1525. (((uint32_t) scsiDev.cdb[8]));
  1526. uint8_t main_channel = scsiDev.cdb[9];
  1527. uint8_t sub_channel = scsiDev.cdb[10];
  1528. doReadCD(lba, blocks, sector_type, main_channel, sub_channel, false);
  1529. }
  1530. else if (command == 0xB9)
  1531. {
  1532. // ReadCD MSF
  1533. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1534. uint32_t start = MSF2LBA(scsiDev.cdb[3], scsiDev.cdb[4], scsiDev.cdb[5], false);
  1535. uint32_t end = MSF2LBA(scsiDev.cdb[6], scsiDev.cdb[7], scsiDev.cdb[8], false);
  1536. uint8_t main_channel = scsiDev.cdb[9];
  1537. uint8_t sub_channel = scsiDev.cdb[10];
  1538. doReadCD(start, end - start, sector_type, main_channel, sub_channel, false);
  1539. }
  1540. else if (command == 0x42)
  1541. {
  1542. // Read subchannel data
  1543. bool time = (scsiDev.cdb[1] & 0x02);
  1544. bool subq = (scsiDev.cdb[2] & 0x40);
  1545. uint8_t parameter = scsiDev.cdb[3];
  1546. uint8_t track_number = scsiDev.cdb[6];
  1547. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
  1548. doReadSubchannel(time, subq, parameter, track_number, allocationLength);
  1549. }
  1550. else if (command == 0x08)
  1551. {
  1552. // READ(6) for CDs (may need sector translation for cue file handling)
  1553. uint32_t lba =
  1554. (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
  1555. (((uint32_t) scsiDev.cdb[2]) << 8) +
  1556. scsiDev.cdb[3];
  1557. uint32_t blocks = scsiDev.cdb[4];
  1558. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1559. }
  1560. else if (command == 0x28)
  1561. {
  1562. // READ(10) for CDs (may need sector translation for cue file handling)
  1563. uint32_t lba =
  1564. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1565. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1566. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1567. scsiDev.cdb[5];
  1568. uint32_t blocks =
  1569. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1570. scsiDev.cdb[8];
  1571. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1572. }
  1573. else if (command == 0xA8)
  1574. {
  1575. // READ(12) for CDs (may need sector translation for cue file handling)
  1576. uint32_t lba =
  1577. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1578. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1579. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1580. scsiDev.cdb[5];
  1581. uint32_t blocks =
  1582. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1583. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1584. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1585. scsiDev.cdb[9];
  1586. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1587. }
  1588. else if (command == 0x4E)
  1589. {
  1590. // STOP PLAY/SCAN
  1591. doStopAudio();
  1592. scsiDev.status = 0;
  1593. scsiDev.phase = STATUS;
  1594. }
  1595. else if (command == 0x01)
  1596. {
  1597. // REZERO UNIT
  1598. // AppleCD Audio Player uses this as a nonstandard
  1599. // "stop audio playback" command
  1600. doStopAudio();
  1601. scsiDev.status = 0;
  1602. scsiDev.phase = STATUS;
  1603. }
  1604. else if (command == 0x0B || command == 0x2B)
  1605. {
  1606. // SEEK
  1607. // implement Annex C termination requirement and pass to disk handler
  1608. doStopAudio();
  1609. // this may need more specific handling, the Win9x player appears to
  1610. // expect a pickup move to the given LBA
  1611. commandHandled = 0;
  1612. }
  1613. else if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_APPLE
  1614. && command == 0xCD)
  1615. {
  1616. // vendor-specific command issued by the AppleCD Audio Player in
  1617. // response to fast-forward or rewind commands. Might be seek,
  1618. // might be reposition. Exact MSF value below is unknown.
  1619. //
  1620. // Byte 0: 0xCD
  1621. // Byte 1: 0x10 for rewind, 0x00 for fast-forward
  1622. // Byte 2: 0x00
  1623. // Byte 3: 'M' in hex
  1624. // Byte 4: 'S' in hex
  1625. // Byte 5: 'F' in hex
  1626. commandHandled = 0;
  1627. }
  1628. else
  1629. {
  1630. commandHandled = 0;
  1631. }
  1632. return commandHandled;
  1633. }