ZuluSCSI_cdrom.cpp 55 KB

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