BlueSCSI_cdrom.cpp 55 KB

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