BlueSCSI_cdrom.cpp 55 KB

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