ZuluSCSI_cdrom.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  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. if (!img.ejected)
  733. {
  734. dbgmsg("------ CDROM open tray on ID ", (int)target);
  735. img.ejected = true;
  736. img.cdrom_events = 3; // Media removal
  737. }
  738. else
  739. {
  740. dbgmsg("------ CDROM close tray on ID ", (int)target);
  741. if (!cdromSwitchNextImage(img))
  742. {
  743. // Reinsert the single image
  744. img.ejected = false;
  745. img.cdrom_events = 2; // New media
  746. }
  747. }
  748. }
  749. // Reinsert any ejected CDROMs on reboot
  750. void cdromReinsertFirstImage(image_config_t &img)
  751. {
  752. if (img.image_index > 0)
  753. {
  754. // Multiple images for this drive, force restart from first one
  755. dbgmsg("---- Restarting from first CD-ROM image");
  756. img.image_index = IMAGE_INDEX_MAX;
  757. cdromSwitchNextImage(img);
  758. }
  759. else if (img.ejected)
  760. {
  761. // Reinsert the single image
  762. dbgmsg("---- Closing CD-ROM tray");
  763. img.ejected = false;
  764. img.cdrom_events = 2; // New media
  765. }
  766. }
  767. // Check if we have multiple CD-ROM images to cycle when drive is ejected.
  768. bool cdromSwitchNextImage(image_config_t &img)
  769. {
  770. // Check if we have a next image to load, so that drive is closed next time the host asks.
  771. char filename[MAX_FILE_PATH];
  772. int target_idx = img.scsiId & 7;
  773. scsiDiskGetNextImageName(img, filename, sizeof(filename));
  774. #ifdef ENABLE_AUDIO_OUTPUT
  775. // if in progress for this device, terminate audio playback immediately (Annex C)
  776. audio_stop(target_idx);
  777. // Reset position tracking for the new image
  778. audio_get_status_code(target_idx); // trash audio status code
  779. #endif
  780. if (filename[0] != '\0')
  781. {
  782. logmsg("Switching to next CD-ROM image for ", target_idx, ": ", filename);
  783. img.file.close();
  784. bool status = scsiDiskOpenHDDImage(target_idx, filename, target_idx, 0, 2048);
  785. if (status)
  786. {
  787. img.ejected = false;
  788. img.cdrom_events = 2; // New media
  789. return true;
  790. }
  791. }
  792. return false;
  793. }
  794. static void doGetEventStatusNotification(bool immed)
  795. {
  796. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  797. if (!immed)
  798. {
  799. // Asynchronous notification not supported
  800. scsiDev.status = CHECK_CONDITION;
  801. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  802. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  803. scsiDev.phase = STATUS;
  804. }
  805. else if (img.cdrom_events)
  806. {
  807. scsiDev.data[0] = 0;
  808. scsiDev.data[1] = 6; // EventDataLength
  809. scsiDev.data[2] = 0x04; // Media status events
  810. scsiDev.data[3] = 0x04; // Supported events
  811. scsiDev.data[4] = img.cdrom_events;
  812. scsiDev.data[5] = 0x01; // Power status
  813. scsiDev.data[6] = 0; // Start slot
  814. scsiDev.data[7] = 0; // End slot
  815. scsiDev.dataLen = 8;
  816. scsiDev.phase = DATA_IN;
  817. img.cdrom_events = 0;
  818. if (img.ejected && img.reinsert_after_eject)
  819. {
  820. // We are now reporting to host that the drive is open.
  821. // Simulate a "close" for next time the host polls.
  822. cdromSwitchNextImage(img);
  823. }
  824. }
  825. else
  826. {
  827. scsiDev.data[0] = 0;
  828. scsiDev.data[1] = 2; // EventDataLength
  829. scsiDev.data[2] = 0x00; // Media status events
  830. scsiDev.data[3] = 0x04; // Supported events
  831. scsiDev.dataLen = 4;
  832. scsiDev.phase = DATA_IN;
  833. }
  834. }
  835. /**************************************/
  836. /* CD-ROM audio playback */
  837. /**************************************/
  838. void cdromGetAudioPlaybackStatus(uint8_t *status, uint32_t *current_lba, bool current_only)
  839. {
  840. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  841. #ifdef ENABLE_AUDIO_OUTPUT
  842. if (status) {
  843. uint8_t target = img.scsiId & 7;
  844. if (current_only) {
  845. *status = audio_is_playing(target) ? 1 : 0;
  846. } else {
  847. *status = (uint8_t) audio_get_status_code(target);
  848. }
  849. }
  850. #else
  851. if (status) *status = 0; // audio status code for 'unsupported/invalid' and not-playing indicator
  852. #endif
  853. if (current_lba)
  854. {
  855. if (img.file.isOpen()) {
  856. *current_lba = img.file.position() / 2352;
  857. } else {
  858. *current_lba = 0;
  859. }
  860. }
  861. }
  862. static void doPlayAudio(uint32_t lba, uint32_t length)
  863. {
  864. #ifdef ENABLE_AUDIO_OUTPUT
  865. dbgmsg("------ CD-ROM Play Audio request at ", lba, " for ", length, " sectors");
  866. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  867. uint8_t target_id = img.scsiId & 7;
  868. // Per Annex C terminate playback immediately if already in progress on
  869. // the current target. Non-current targets may also get their audio
  870. // interrupted later due to hardware limitations
  871. audio_stop(img.scsiId & 7);
  872. // if transfer length is zero no audio playback happens.
  873. // don't treat as an error per SCSI-2; handle via short-circuit
  874. if (length == 0)
  875. {
  876. scsiDev.status = 0;
  877. scsiDev.phase = STATUS;
  878. return;
  879. }
  880. // if actual playback is requested perform steps to verify prior to playback
  881. CUEParser parser;
  882. if (loadCueSheet(img, parser))
  883. {
  884. CUETrackInfo trackinfo = {};
  885. getTrackFromLBA(parser, lba, &trackinfo);
  886. if (lba == 0xFFFFFFFF)
  887. {
  888. // request to start playback from 'current position'
  889. lba = img.file.position() / 2352;
  890. }
  891. uint64_t offset = trackinfo.file_offset
  892. + trackinfo.sector_length * (lba - trackinfo.track_start);
  893. dbgmsg("------ Play audio CD: ", (int)length, " sectors starting at ", (int)lba,
  894. ", track number ", trackinfo.track_number, ", data offset in file ", (int)offset);
  895. if (trackinfo.track_mode != CUETrack_AUDIO)
  896. {
  897. dbgmsg("---- Host tried audio playback on track type ", (int)trackinfo.track_mode);
  898. scsiDev.status = CHECK_CONDITION;
  899. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  900. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  901. scsiDev.phase = STATUS;
  902. return;
  903. }
  904. // playback request appears to be sane, so perform it
  905. // see earlier note for context on the block length below
  906. if (!audio_play(target_id, &(img.file), offset,
  907. offset + length * trackinfo.sector_length, false))
  908. {
  909. // Underlying data/media error? Fake a disk scratch, which should
  910. // be a condition most CD-DA players are expecting
  911. scsiDev.status = CHECK_CONDITION;
  912. scsiDev.target->sense.code = MEDIUM_ERROR;
  913. scsiDev.target->sense.asc = 0x1106; // CIRC UNRECOVERED ERROR
  914. scsiDev.phase = STATUS;
  915. return;
  916. }
  917. scsiDev.status = 0;
  918. scsiDev.phase = STATUS;
  919. }
  920. else
  921. {
  922. // virtual drive supports audio, just not with this disk image
  923. dbgmsg("---- Request to play audio on non-audio image");
  924. scsiDev.status = CHECK_CONDITION;
  925. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  926. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  927. scsiDev.phase = STATUS;
  928. }
  929. #else
  930. dbgmsg("---- Target does not support audio playback");
  931. // per SCSI-2, targets not supporting audio respond to zero-length
  932. // PLAY AUDIO commands with ILLEGAL REQUEST; this seems to be a check
  933. // performed by at least some audio playback software
  934. scsiDev.status = CHECK_CONDITION;
  935. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  936. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  937. scsiDev.phase = STATUS;
  938. #endif
  939. }
  940. static void doPauseResumeAudio(bool resume)
  941. {
  942. #ifdef ENABLE_AUDIO_OUTPUT
  943. logmsg("------ CD-ROM ", resume ? "resume" : "pause", " audio playback");
  944. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  945. uint8_t target_id = img.scsiId & 7;
  946. if (audio_is_playing(target_id))
  947. {
  948. audio_set_paused(target_id, !resume);
  949. scsiDev.status = 0;
  950. scsiDev.phase = STATUS;
  951. }
  952. else
  953. {
  954. scsiDev.status = CHECK_CONDITION;
  955. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  956. scsiDev.target->sense.asc = 0x2C00; // COMMAND SEQUENCE ERROR
  957. scsiDev.phase = STATUS;
  958. }
  959. #else
  960. dbgmsg("---- Target does not support audio pausing");
  961. scsiDev.status = CHECK_CONDITION;
  962. scsiDev.target->sense.code = ILLEGAL_REQUEST; // assumed from PLAY AUDIO(10)
  963. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  964. scsiDev.phase = STATUS;
  965. #endif
  966. }
  967. static void doStopAudio()
  968. {
  969. dbgmsg("------ CD-ROM Stop Audio request");
  970. #ifdef ENABLE_AUDIO_OUTPUT
  971. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  972. uint8_t target_id = img.scsiId & 7;
  973. audio_stop(target_id);
  974. #endif
  975. }
  976. static void doMechanismStatus(uint16_t allocation_length)
  977. {
  978. uint8_t *buf = scsiDev.data;
  979. uint8_t status;
  980. uint32_t lba;
  981. cdromGetAudioPlaybackStatus(&status, &lba, true);
  982. *buf++ = 0x00; // No fault state
  983. *buf++ = (status) ? 0x20 : 0x00; // Currently playing?
  984. *buf++ = (lba >> 16) & 0xFF;
  985. *buf++ = (lba >> 8) & 0xFF;
  986. *buf++ = (lba >> 0) & 0xFF;
  987. *buf++ = 0; // No CD changer
  988. *buf++ = 0;
  989. *buf++ = 0;
  990. int len = 8;
  991. if (len > allocation_length) len = allocation_length;
  992. scsiDev.dataLen = len;
  993. scsiDev.phase = DATA_IN;
  994. }
  995. /*******************************************/
  996. /* CD-ROM data reading in low level format */
  997. /*******************************************/
  998. static void doReadCD(uint32_t lba, uint32_t length, uint8_t sector_type,
  999. uint8_t main_channel, uint8_t sub_channel, bool data_only)
  1000. {
  1001. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1002. #if ENABLE_AUDIO_OUTPUT
  1003. // terminate audio playback if active on this target (Annex C)
  1004. audio_stop(img.scsiId & 7);
  1005. #endif
  1006. CUEParser parser;
  1007. if (!loadCueSheet(img, parser)
  1008. && (sector_type == 0 || sector_type == 2)
  1009. && main_channel == 0x10 && sub_channel == 0)
  1010. {
  1011. // Simple case, return sector data directly
  1012. scsiDiskStartRead(lba, length);
  1013. return;
  1014. }
  1015. // Search the track with the requested LBA
  1016. // Supplies dummy data if no cue sheet is active.
  1017. CUETrackInfo trackinfo = {};
  1018. getTrackFromLBA(parser, lba, &trackinfo);
  1019. // Figure out the data offset in the file
  1020. uint64_t offset = trackinfo.file_offset + trackinfo.sector_length * (lba - trackinfo.track_start);
  1021. dbgmsg("------ Read CD: ", (int)length, " sectors starting at ", (int)lba,
  1022. ", track number ", trackinfo.track_number, ", sector size ", (int)trackinfo.sector_length,
  1023. ", main channel ", main_channel, ", sub channel ", sub_channel,
  1024. ", data offset in file ", (int)offset);
  1025. // Ensure read is not out of range of the image
  1026. uint64_t readend = offset + trackinfo.sector_length * length;
  1027. if (readend > img.file.size())
  1028. {
  1029. logmsg("WARNING: Host attempted CD read at sector ", lba, "+", length,
  1030. ", exceeding image size ", img.file.size());
  1031. scsiDev.status = CHECK_CONDITION;
  1032. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1033. scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1034. scsiDev.phase = STATUS;
  1035. return;
  1036. }
  1037. // Verify sector type
  1038. if (sector_type != 0)
  1039. {
  1040. bool sector_type_ok = false;
  1041. if (sector_type == 1 && trackinfo.track_mode == CUETrack_AUDIO)
  1042. {
  1043. sector_type_ok = true;
  1044. }
  1045. else if (sector_type == 2 && trackinfo.track_mode == CUETrack_MODE1_2048)
  1046. {
  1047. sector_type_ok = true;
  1048. }
  1049. if (!sector_type_ok)
  1050. {
  1051. dbgmsg("---- Failed sector type check, host requested ", (int)sector_type, " CUE file has ", (int)trackinfo.track_mode);
  1052. scsiDev.status = CHECK_CONDITION;
  1053. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1054. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1055. scsiDev.phase = STATUS;
  1056. return;
  1057. }
  1058. }
  1059. // Select fields to transfer
  1060. // Refer to table 351 in T10/1545-D MMC-4 Revision 5a
  1061. // Only the mandatory cases are supported.
  1062. int sector_length = 0;
  1063. int skip_begin = 0;
  1064. bool add_fake_headers = false;
  1065. if (main_channel == 0)
  1066. {
  1067. // No actual data requested, just sector type check or subchannel
  1068. sector_length = 0;
  1069. }
  1070. else if (trackinfo.track_mode == CUETrack_AUDIO)
  1071. {
  1072. // Transfer whole 2352 byte audio sectors from file to host
  1073. sector_length = 2352;
  1074. }
  1075. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && main_channel == 0x10)
  1076. {
  1077. // Transfer whole 2048 byte data sectors from file to host
  1078. sector_length = 2048;
  1079. }
  1080. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && (main_channel & 0xB8) == 0xB8)
  1081. {
  1082. // Transfer 2048 bytes of data from file and fake the headers
  1083. sector_length = 2048;
  1084. add_fake_headers = true;
  1085. dbgmsg("------ Host requested ECC data but image file lacks it, replacing with zeros");
  1086. }
  1087. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && main_channel == 0x10)
  1088. {
  1089. // Transfer the 2048 byte payload of data sector to host.
  1090. sector_length = 2048;
  1091. skip_begin = 16;
  1092. }
  1093. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && (main_channel & 0xB8) == 0xB8)
  1094. {
  1095. // Transfer whole 2352 byte data sector with ECC to host
  1096. sector_length = 2352;
  1097. }
  1098. else
  1099. {
  1100. dbgmsg("---- Unsupported channel request for track type ", (int)trackinfo.track_mode);
  1101. scsiDev.status = CHECK_CONDITION;
  1102. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1103. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1104. scsiDev.phase = STATUS;
  1105. return;
  1106. }
  1107. if (data_only && sector_length != 2048)
  1108. {
  1109. dbgmsg("------ Host tried to read non-data sector with standard READ command");
  1110. scsiDev.status = CHECK_CONDITION;
  1111. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1112. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1113. scsiDev.phase = STATUS;
  1114. return;
  1115. }
  1116. bool field_q_subchannel = false;
  1117. if (sub_channel == 2)
  1118. {
  1119. // Include position information in Q subchannel
  1120. field_q_subchannel = true;
  1121. }
  1122. else if (sub_channel != 0)
  1123. {
  1124. dbgmsg("---- Unsupported subchannel request");
  1125. scsiDev.status = CHECK_CONDITION;
  1126. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1127. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1128. scsiDev.phase = STATUS;
  1129. return;
  1130. }
  1131. scsiDev.phase = DATA_IN;
  1132. scsiDev.dataLen = 0;
  1133. scsiDev.dataPtr = 0;
  1134. scsiEnterPhase(DATA_IN);
  1135. // Use two buffers alternately for formatting sector data
  1136. uint32_t result_length = sector_length + (field_q_subchannel ? 16 : 0) + (add_fake_headers ? 304 : 0);
  1137. uint8_t *buf0 = scsiDev.data;
  1138. uint8_t *buf1 = scsiDev.data + result_length;
  1139. // Format the sectors for transfer
  1140. for (uint32_t idx = 0; idx < length; idx++)
  1141. {
  1142. platform_poll();
  1143. diskEjectButtonUpdate(false);
  1144. img.file.seek(offset + idx * trackinfo.sector_length + skip_begin);
  1145. // Verify that previous write using this buffer has finished
  1146. uint8_t *buf = ((idx & 1) ? buf1 : buf0);
  1147. uint8_t *bufstart = buf;
  1148. uint32_t start = millis();
  1149. while (!scsiIsWriteFinished(buf + result_length - 1) && !scsiDev.resetFlag)
  1150. {
  1151. if ((uint32_t)(millis() - start) > 5000)
  1152. {
  1153. logmsg("doReadCD() timeout waiting for previous to finish");
  1154. scsiDev.resetFlag = 1;
  1155. }
  1156. platform_poll();
  1157. diskEjectButtonUpdate(false);
  1158. }
  1159. if (scsiDev.resetFlag) break;
  1160. if (add_fake_headers)
  1161. {
  1162. // 12-byte data sector sync pattern
  1163. *buf++ = 0x00;
  1164. for (int i = 0; i < 10; i++)
  1165. {
  1166. *buf++ = 0xFF;
  1167. }
  1168. *buf++ = 0x00;
  1169. // 4-byte data sector header
  1170. LBA2MSFBCD(lba + idx, buf, false);
  1171. buf += 3;
  1172. *buf++ = 0x01; // Mode 1
  1173. }
  1174. if (sector_length > 0)
  1175. {
  1176. // User data
  1177. img.file.read(buf, sector_length);
  1178. buf += sector_length;
  1179. }
  1180. if (add_fake_headers)
  1181. {
  1182. // 288 bytes of ECC
  1183. memset(buf, 0, 288);
  1184. buf += 288;
  1185. }
  1186. if (field_q_subchannel)
  1187. {
  1188. // Formatted Q subchannel data
  1189. // Refer to table 354 in T10/1545-D MMC-4 Revision 5a
  1190. // and ECMA-130 22.3.3
  1191. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14); // Control & ADR
  1192. *buf++ = trackinfo.track_number;
  1193. *buf++ = (lba + idx >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1194. int32_t rel = (int32_t)(lba + idx) - (int32_t)trackinfo.data_start;
  1195. LBA2MSF(rel, buf, true); buf += 3;
  1196. *buf++ = 0;
  1197. LBA2MSF(lba + idx, buf, false); buf += 3;
  1198. *buf++ = 0; *buf++ = 0; // CRC (optional)
  1199. *buf++ = 0; *buf++ = 0; *buf++ = 0; // (pad)
  1200. *buf++ = 0; // No P subchannel
  1201. }
  1202. assert(buf == bufstart + result_length);
  1203. scsiStartWrite(bufstart, result_length);
  1204. }
  1205. scsiFinishWrite();
  1206. scsiDev.status = 0;
  1207. scsiDev.phase = STATUS;
  1208. }
  1209. static void doReadSubchannel(bool time, bool subq, uint8_t parameter, uint8_t track_number, uint16_t allocation_length)
  1210. {
  1211. uint8_t *buf = scsiDev.data;
  1212. if (parameter == 0x01)
  1213. {
  1214. uint8_t audiostatus;
  1215. uint32_t lba;
  1216. cdromGetAudioPlaybackStatus(&audiostatus, &lba, false);
  1217. dbgmsg("------ Get audio playback position: status ", (int)audiostatus, " lba ", (int)lba);
  1218. // Fetch current track info
  1219. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1220. CUEParser parser;
  1221. CUETrackInfo trackinfo = {};
  1222. loadCueSheet(img, parser);
  1223. getTrackFromLBA(parser, lba, &trackinfo);
  1224. // Request sub channel data at current playback position
  1225. *buf++ = 0; // Reserved
  1226. *buf++ = audiostatus;
  1227. int len;
  1228. if (subq)
  1229. {
  1230. len = 12;
  1231. *buf++ = 0; // Subchannel data length (MSB)
  1232. *buf++ = len; // Subchannel data length (LSB)
  1233. *buf++ = 0x01; // Subchannel data format
  1234. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14);
  1235. *buf++ = trackinfo.track_number;
  1236. *buf++ = (lba >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1237. if (time)
  1238. {
  1239. *buf++ = 0;
  1240. LBA2MSF(lba, buf, false);
  1241. dbgmsg("------ ABS M ", *buf, " S ", *(buf+1), " F ", *(buf+2));
  1242. buf += 3;
  1243. }
  1244. else
  1245. {
  1246. *buf++ = (lba >> 24) & 0xFF; // Absolute block address
  1247. *buf++ = (lba >> 16) & 0xFF;
  1248. *buf++ = (lba >> 8) & 0xFF;
  1249. *buf++ = (lba >> 0) & 0xFF;
  1250. }
  1251. int32_t relpos = (int32_t)lba - (int32_t)trackinfo.data_start;
  1252. if (time)
  1253. {
  1254. *buf++ = 0;
  1255. LBA2MSF(relpos, buf, true);
  1256. dbgmsg("------ REL M ", *buf, " S ", *(buf+1), " F ", *(buf+2));
  1257. buf += 3;
  1258. }
  1259. else
  1260. {
  1261. uint32_t urelpos = relpos;
  1262. *buf++ = (urelpos >> 24) & 0xFF; // Track relative position (may be negative)
  1263. *buf++ = (urelpos >> 16) & 0xFF;
  1264. *buf++ = (urelpos >> 8) & 0xFF;
  1265. *buf++ = (urelpos >> 0) & 0xFF;
  1266. }
  1267. }
  1268. else
  1269. {
  1270. len = 0;
  1271. *buf++ = 0;
  1272. *buf++ = 0;
  1273. }
  1274. len += 4;
  1275. if (len > allocation_length) len = allocation_length;
  1276. scsiDev.dataLen = len;
  1277. scsiDev.phase = DATA_IN;
  1278. }
  1279. else
  1280. {
  1281. dbgmsg("---- Unsupported subchannel request");
  1282. scsiDev.status = CHECK_CONDITION;
  1283. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1284. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1285. scsiDev.phase = STATUS;
  1286. return;
  1287. }
  1288. }
  1289. static bool doReadCapacity(uint32_t lba, uint8_t pmi)
  1290. {
  1291. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1292. CUEParser parser;
  1293. if (!loadCueSheet(img, parser))
  1294. {
  1295. // basic image, let the disk handler resolve
  1296. return false;
  1297. }
  1298. // find the last track on the disk
  1299. CUETrackInfo lasttrack = {0};
  1300. const CUETrackInfo *trackinfo;
  1301. while ((trackinfo = parser.next_track()) != NULL)
  1302. {
  1303. lasttrack = *trackinfo;
  1304. }
  1305. uint32_t capacity = 0;
  1306. if (lasttrack.track_number != 0)
  1307. {
  1308. capacity = getLeadOutLBA(&lasttrack);
  1309. capacity--; // shift to last addressable LBA
  1310. if (pmi && lba && lba > capacity)
  1311. {
  1312. // MMC technically specifies that PMI should be zero, but SCSI-2 allows this
  1313. // potentially consider treating either out-of-bounds or PMI set as an error
  1314. // for now just ignore this
  1315. }
  1316. dbgmsg("----- Reporting capacity as ", capacity);
  1317. }
  1318. else
  1319. {
  1320. logmsg("WARNING: unable to find capacity, no cue file found for ID ", img.scsiId);
  1321. }
  1322. scsiDev.data[0] = capacity >> 24;
  1323. scsiDev.data[1] = capacity >> 16;
  1324. scsiDev.data[2] = capacity >> 8;
  1325. scsiDev.data[3] = capacity;
  1326. scsiDev.data[4] = 0;
  1327. scsiDev.data[5] = 0;
  1328. scsiDev.data[6] = 0x08; // rest of code assumes 2048 here
  1329. scsiDev.data[7] = 0x00;
  1330. scsiDev.dataLen = 8;
  1331. scsiDev.phase = DATA_IN;
  1332. return true;
  1333. }
  1334. /**************************************/
  1335. /* CD-ROM command dispatching */
  1336. /**************************************/
  1337. // Handle direct-access scsi device commands
  1338. extern "C" int scsiCDRomCommand()
  1339. {
  1340. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1341. int commandHandled = 1;
  1342. uint8_t command = scsiDev.cdb[0];
  1343. if (command == 0x1B)
  1344. {
  1345. #if ENABLE_AUDIO_OUTPUT
  1346. // terminate audio playback if active on this target (MMC-1 Annex C)
  1347. audio_stop(img.scsiId & 7);
  1348. #endif
  1349. if ((scsiDev.cdb[4] & 2))
  1350. {
  1351. // CD-ROM load & eject
  1352. int start = scsiDev.cdb[4] & 1;
  1353. if (start)
  1354. {
  1355. dbgmsg("------ CDROM close tray on ID ", (int)(img.scsiId & 7));
  1356. img.ejected = false;
  1357. img.cdrom_events = 2; // New media
  1358. }
  1359. else
  1360. {
  1361. cdromPerformEject(img);
  1362. }
  1363. }
  1364. else
  1365. {
  1366. // flow through to disk handler
  1367. commandHandled = 0;
  1368. }
  1369. }
  1370. else if (command == 0x25)
  1371. {
  1372. // READ CAPACITY
  1373. uint8_t reladdr = scsiDev.cdb[1] & 1;
  1374. uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
  1375. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1376. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1377. scsiDev.cdb[5];
  1378. uint8_t pmi = scsiDev.cdb[8] & 1;
  1379. // allow PMI as long as LBA is specified, this is permitted in SCSI-2
  1380. // we don't link commands, do not allow RELADDR
  1381. if ((!pmi && lba != 0) || reladdr)
  1382. {
  1383. scsiDev.status = CHECK_CONDITION;
  1384. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1385. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1386. scsiDev.phase = STATUS;
  1387. }
  1388. else
  1389. {
  1390. if (!doReadCapacity(lba, pmi))
  1391. {
  1392. // allow disk handler to resolve this one
  1393. commandHandled = 0;
  1394. }
  1395. }
  1396. }
  1397. else if (command == 0x43)
  1398. {
  1399. // CD-ROM Read TOC
  1400. bool MSF = (scsiDev.cdb[1] & 0x02);
  1401. uint8_t track = scsiDev.cdb[6];
  1402. uint16_t allocationLength =
  1403. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1404. scsiDev.cdb[8];
  1405. // The "format" field is reserved for SCSI-2
  1406. uint8_t format = scsiDev.cdb[2] & 0x0F;
  1407. // Matshita SCSI-2 drives appear to use the high 2 bits of the CDB
  1408. // control byte to switch on session info (0x40) and full toc (0x80)
  1409. // responses that are very similar to the standard formats described
  1410. // in MMC-1. These vendor flags must have been pretty common because
  1411. // even a modern SATA drive (ASUS DRW-24B1ST j) responds to them
  1412. // (though it always replies in hex rather than bcd)
  1413. //
  1414. // The session information page is identical to MMC. The full TOC page
  1415. // is identical _except_ it returns addresses in bcd rather than hex.
  1416. bool useBCD = false;
  1417. if (format == 0 && scsiDev.cdb[9] == 0x80)
  1418. {
  1419. format = 2;
  1420. useBCD = true;
  1421. }
  1422. else if (format == 0 && scsiDev.cdb[9] == 0x40)
  1423. {
  1424. format = 1;
  1425. }
  1426. switch (format)
  1427. {
  1428. case 0: doReadTOC(MSF, track, allocationLength); break; // SCSI-2
  1429. case 1: doReadSessionInfo(MSF, allocationLength); break; // MMC2
  1430. case 2: doReadFullTOC(track, allocationLength, useBCD); break; // MMC2
  1431. default:
  1432. {
  1433. scsiDev.status = CHECK_CONDITION;
  1434. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1435. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1436. scsiDev.phase = STATUS;
  1437. }
  1438. }
  1439. }
  1440. else if (command == 0x44)
  1441. {
  1442. // CD-ROM Read Header
  1443. bool MSF = (scsiDev.cdb[1] & 0x02);
  1444. uint32_t lba = 0; // IGNORED for now
  1445. uint16_t allocationLength =
  1446. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1447. scsiDev.cdb[8];
  1448. doReadHeader(MSF, lba, allocationLength);
  1449. }
  1450. else if (command == 0x51)
  1451. {
  1452. uint16_t allocationLength =
  1453. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1454. scsiDev.cdb[8];
  1455. doReadDiscInformation(allocationLength);
  1456. }
  1457. else if (command == 0x4A)
  1458. {
  1459. // Get event status notifications (media change notifications)
  1460. bool immed = scsiDev.cdb[1] & 1;
  1461. doGetEventStatusNotification(immed);
  1462. }
  1463. else if (command == 0x45)
  1464. {
  1465. // PLAY AUDIO (10)
  1466. uint32_t lba =
  1467. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1468. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1469. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1470. scsiDev.cdb[5];
  1471. uint32_t blocks =
  1472. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1473. scsiDev.cdb[8];
  1474. doPlayAudio(lba, blocks);
  1475. }
  1476. else if (command == 0xA5)
  1477. {
  1478. // PLAY AUDIO (12)
  1479. uint32_t lba =
  1480. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1481. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1482. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1483. scsiDev.cdb[5];
  1484. uint32_t blocks =
  1485. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1486. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1487. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1488. scsiDev.cdb[9];
  1489. doPlayAudio(lba, blocks);
  1490. }
  1491. else if (command == 0x47)
  1492. {
  1493. // PLAY AUDIO (MSF)
  1494. uint32_t start = MSF2LBA(scsiDev.cdb[3], scsiDev.cdb[4], scsiDev.cdb[5], false);
  1495. uint32_t end = MSF2LBA(scsiDev.cdb[6], scsiDev.cdb[7], scsiDev.cdb[8], false);
  1496. uint32_t lba = start;
  1497. if (scsiDev.cdb[3] == 0xFF
  1498. && scsiDev.cdb[4] == 0xFF
  1499. && scsiDev.cdb[5] == 0xFF)
  1500. {
  1501. // request to start playback from 'current position'
  1502. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1503. lba = img.file.position() / 2352;
  1504. }
  1505. uint32_t length = end - lba;
  1506. doPlayAudio(lba, length);
  1507. }
  1508. else if (command == 0x4B)
  1509. {
  1510. // PAUSE/RESUME AUDIO
  1511. doPauseResumeAudio(scsiDev.cdb[8] & 1);
  1512. }
  1513. else if (command == 0xBD)
  1514. {
  1515. // Mechanism status
  1516. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[8]) << 8) + scsiDev.cdb[9];
  1517. doMechanismStatus(allocationLength);
  1518. }
  1519. else if (command == 0xBB)
  1520. {
  1521. // Set CD speed (just ignored)
  1522. scsiDev.status = 0;
  1523. scsiDev.phase = STATUS;
  1524. }
  1525. else if (command == 0xBE)
  1526. {
  1527. // ReadCD (in low level format)
  1528. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1529. uint32_t lba =
  1530. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1531. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1532. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1533. scsiDev.cdb[5];
  1534. uint32_t blocks =
  1535. (((uint32_t) scsiDev.cdb[6]) << 16) +
  1536. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1537. (((uint32_t) scsiDev.cdb[8]));
  1538. uint8_t main_channel = scsiDev.cdb[9];
  1539. uint8_t sub_channel = scsiDev.cdb[10];
  1540. doReadCD(lba, blocks, sector_type, main_channel, sub_channel, false);
  1541. }
  1542. else if (command == 0xB9)
  1543. {
  1544. // ReadCD MSF
  1545. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1546. uint32_t start = MSF2LBA(scsiDev.cdb[3], scsiDev.cdb[4], scsiDev.cdb[5], false);
  1547. uint32_t end = MSF2LBA(scsiDev.cdb[6], scsiDev.cdb[7], scsiDev.cdb[8], false);
  1548. uint8_t main_channel = scsiDev.cdb[9];
  1549. uint8_t sub_channel = scsiDev.cdb[10];
  1550. doReadCD(start, end - start, sector_type, main_channel, sub_channel, false);
  1551. }
  1552. else if (command == 0x42)
  1553. {
  1554. // Read subchannel data
  1555. bool time = (scsiDev.cdb[1] & 0x02);
  1556. bool subq = (scsiDev.cdb[2] & 0x40);
  1557. uint8_t parameter = scsiDev.cdb[3];
  1558. uint8_t track_number = scsiDev.cdb[6];
  1559. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
  1560. doReadSubchannel(time, subq, parameter, track_number, allocationLength);
  1561. }
  1562. else if (command == 0x08)
  1563. {
  1564. // READ(6) for CDs (may need sector translation for cue file handling)
  1565. uint32_t lba =
  1566. (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
  1567. (((uint32_t) scsiDev.cdb[2]) << 8) +
  1568. scsiDev.cdb[3];
  1569. uint32_t blocks = scsiDev.cdb[4];
  1570. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1571. }
  1572. else if (command == 0x28)
  1573. {
  1574. // READ(10) for CDs (may need sector translation for cue file handling)
  1575. uint32_t lba =
  1576. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1577. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1578. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1579. scsiDev.cdb[5];
  1580. uint32_t blocks =
  1581. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1582. scsiDev.cdb[8];
  1583. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1584. }
  1585. else if (command == 0xA8)
  1586. {
  1587. // READ(12) for CDs (may need sector translation for cue file handling)
  1588. uint32_t lba =
  1589. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1590. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1591. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1592. scsiDev.cdb[5];
  1593. uint32_t blocks =
  1594. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1595. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1596. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1597. scsiDev.cdb[9];
  1598. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1599. }
  1600. else if (command == 0x4E)
  1601. {
  1602. // STOP PLAY/SCAN
  1603. doStopAudio();
  1604. scsiDev.status = 0;
  1605. scsiDev.phase = STATUS;
  1606. }
  1607. else if (command == 0x01)
  1608. {
  1609. // REZERO UNIT
  1610. // AppleCD Audio Player uses this as a nonstandard
  1611. // "stop audio playback" command
  1612. doStopAudio();
  1613. scsiDev.status = 0;
  1614. scsiDev.phase = STATUS;
  1615. }
  1616. else if (command == 0x0B || command == 0x2B)
  1617. {
  1618. // SEEK
  1619. // implement Annex C termination requirement and pass to disk handler
  1620. doStopAudio();
  1621. // this may need more specific handling, the Win9x player appears to
  1622. // expect a pickup move to the given LBA
  1623. commandHandled = 0;
  1624. }
  1625. else if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_APPLE
  1626. && command == 0xCD)
  1627. {
  1628. // vendor-specific command issued by the AppleCD Audio Player in
  1629. // response to fast-forward or rewind commands. Might be seek,
  1630. // might be reposition. Exact MSF value below is unknown.
  1631. //
  1632. // Byte 0: 0xCD
  1633. // Byte 1: 0x10 for rewind, 0x00 for fast-forward
  1634. // Byte 2: 0x00
  1635. // Byte 3: 'M' in hex
  1636. // Byte 4: 'S' in hex
  1637. // Byte 5: 'F' in hex
  1638. commandHandled = 0;
  1639. }
  1640. else
  1641. {
  1642. commandHandled = 0;
  1643. }
  1644. return commandHandled;
  1645. }