BlueSCSI_cdrom.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  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. /* CDROM positioning information */
  43. /******************************************/
  44. typedef struct {
  45. uint32_t last_lba = 0;
  46. } mechanism_status_t;
  47. static mechanism_status_t mechanism_status[8];
  48. /******************************************/
  49. /* Basic TOC generation without cue sheet */
  50. /******************************************/
  51. static const uint8_t SimpleTOC[] =
  52. {
  53. 0x00, // toc length, MSB
  54. 0x12, // toc length, LSB
  55. 0x01, // First track number
  56. 0x01, // Last track number,
  57. // TRACK 1 Descriptor
  58. 0x00, // reserved
  59. 0x14, // Q sub-channel encodes current position, Digital track
  60. 0x01, // Track 1,
  61. 0x00, // Reserved
  62. 0x00,0x00,0x00,0x00, // Track start sector (LBA)
  63. 0x00, // reserved
  64. 0x14, // Q sub-channel encodes current position, Digital track
  65. 0xAA, // Leadout Track
  66. 0x00, // Reserved
  67. 0x00,0x00,0x00,0x00, // Track start sector (LBA)
  68. };
  69. static const uint8_t LeadoutTOC[] =
  70. {
  71. 0x00, // toc length, MSB
  72. 0x0A, // toc length, LSB
  73. 0x01, // First track number
  74. 0x01, // Last track number,
  75. 0x00, // reserved
  76. 0x14, // Q sub-channel encodes current position, Digital track
  77. 0xAA, // Leadout Track
  78. 0x00, // Reserved
  79. 0x00,0x00,0x00,0x00, // Track start sector (LBA)
  80. };
  81. static const uint8_t SessionTOC[] =
  82. {
  83. 0x00, // toc length, MSB
  84. 0x0A, // toc length, LSB
  85. 0x01, // First session number
  86. 0x01, // Last session number,
  87. // TRACK 1 Descriptor
  88. 0x00, // reserved
  89. 0x14, // Q sub-channel encodes current position, Digital track
  90. 0x01, // First track number in last complete session
  91. 0x00, // Reserved
  92. 0x00,0x00,0x00,0x00 // LBA of first track in last session
  93. };
  94. static const uint8_t FullTOC[] =
  95. {
  96. 0x00, // 0: toc length, MSB
  97. 0x44, // 1: toc length, LSB
  98. 0x01, // 2: First session number
  99. 0x01, // 3: Last session number,
  100. // A0 Descriptor
  101. 0x01, // 4: session number
  102. 0x14, // 5: ADR/Control
  103. 0x00, // 6: TNO
  104. 0xA0, // 7: POINT
  105. 0x00, // 8: Min
  106. 0x00, // 9: Sec
  107. 0x00, // 10: Frame
  108. 0x00, // 11: Zero
  109. 0x01, // 12: First Track number.
  110. 0x00, // 13: Disc type 00 = Mode 1
  111. 0x00, // 14: PFRAME
  112. // A1
  113. 0x01, // 15: session number
  114. 0x14, // 16: ADR/Control
  115. 0x00, // 17: TNO
  116. 0xA1, // 18: POINT
  117. 0x00, // 19: Min
  118. 0x00, // 20: Sec
  119. 0x00, // 21: Frame
  120. 0x00, // 22: Zero
  121. 0x01, // 23: Last Track number
  122. 0x00, // 24: PSEC
  123. 0x00, // 25: PFRAME
  124. // A2
  125. 0x01, // 26: session number
  126. 0x14, // 27: ADR/Control
  127. 0x00, // 28: TNO
  128. 0xA2, // 29: POINT
  129. 0x00, // 30: Min
  130. 0x00, // 31: Sec
  131. 0x00, // 32: Frame
  132. 0x00, // 33: Zero
  133. 0x79, // 34: LEADOUT position BCD
  134. 0x59, // 35: leadout PSEC BCD
  135. 0x74, // 36: leadout PFRAME BCD
  136. // TRACK 1 Descriptor
  137. 0x01, // 37: session number
  138. 0x14, // 38: ADR/Control
  139. 0x00, // 39: TNO
  140. 0x01, // 40: Point
  141. 0x00, // 41: Min
  142. 0x00, // 42: Sec
  143. 0x00, // 43: Frame
  144. 0x00, // 44: Zero
  145. 0x00, // 45: PMIN
  146. 0x00, // 46: PSEC
  147. 0x00, // 47: PFRAME
  148. // b0
  149. 0x01, // 48: session number
  150. 0x54, // 49: ADR/Control
  151. 0x00, // 50: TNO
  152. 0xB1, // 51: POINT
  153. 0x79, // 52: Min BCD
  154. 0x59, // 53: Sec BCD
  155. 0x74, // 54: Frame BCD
  156. 0x00, // 55: Zero
  157. 0x79, // 56: PMIN BCD
  158. 0x59, // 57: PSEC BCD
  159. 0x74, // 58: PFRAME BCD
  160. // c0
  161. 0x01, // 59: session number
  162. 0x54, // 60: ADR/Control
  163. 0x00, // 61: TNO
  164. 0xC0, // 62: POINT
  165. 0x00, // 63: Min
  166. 0x00, // 64: Sec
  167. 0x00, // 65: Frame
  168. 0x00, // 66: Zero
  169. 0x00, // 67: PMIN
  170. 0x00, // 68: PSEC
  171. 0x00 // 69: PFRAME
  172. };
  173. static uint8_t SimpleHeader[] =
  174. {
  175. 0x01, // 2048byte user data, L-EC in 288 byte aux field.
  176. 0x00, // reserved
  177. 0x00, // reserved
  178. 0x00, // reserved
  179. 0x00,0x00,0x00,0x00 // Track start sector (LBA or MSF)
  180. };
  181. static const uint8_t DiscInformation[] =
  182. {
  183. 0x00, // 0: disc info length, MSB
  184. 0x20, // 1: disc info length, LSB
  185. 0x0E, // 2: disc status (finalized, single session non-rewritable)
  186. 0x01, // 3: first track number
  187. 0x01, // 4: number of sessions (LSB)
  188. 0x01, // 5: first track in last session (LSB)
  189. 0x01, // 6: last track in last session (LSB)
  190. 0x00, // 7: format status (0x00 = non-rewritable, no barcode, no disc id)
  191. 0x00, // 8: disc type (0x00 = CD-ROM)
  192. 0x00, // 9: number of sessions (MSB)
  193. 0x00, // 10: first track in last session (MSB)
  194. 0x00, // 11: last track in last session (MSB)
  195. 0x00, // 12: disc ID (MSB)
  196. 0x00, // 13: .
  197. 0x00, // 14: .
  198. 0x00, // 15: disc ID (LSB)
  199. 0x00, // 16: last session lead-in start (MSB)
  200. 0x00, // 17: .
  201. 0x00, // 18: .
  202. 0x00, // 19: last session lead-in start (LSB)
  203. 0x00, // 20: last possible lead-out start (MSB)
  204. 0x00, // 21: .
  205. 0x00, // 22: .
  206. 0x00, // 23: last possible lead-out start (LSB)
  207. 0x00, // 24: disc bar code (MSB)
  208. 0x00, // 25: .
  209. 0x00, // 26: .
  210. 0x00, // 27: .
  211. 0x00, // 28: .
  212. 0x00, // 29: .
  213. 0x00, // 30: .
  214. 0x00, // 31: disc bar code (LSB)
  215. 0x00, // 32: disc application code
  216. 0x00, // 33: number of opc tables
  217. };
  218. // Convert logical block address to CD-ROM time in formatted TOC format
  219. static void LBA2MSF(uint32_t LBA, uint8_t* MSF)
  220. {
  221. MSF[0] = 0; // reserved.
  222. MSF[3] = LBA % 75; // Frames
  223. uint32_t rem = LBA / 75;
  224. MSF[2] = rem % 60; // Seconds
  225. MSF[1] = rem / 60; // Minutes
  226. }
  227. static void doReadTOCSimple(bool MSF, uint8_t track, uint16_t allocationLength)
  228. {
  229. if (track == 0xAA)
  230. {
  231. // 0xAA requests only lead-out track information (reports capacity)
  232. uint32_t len = sizeof(LeadoutTOC);
  233. memcpy(scsiDev.data, LeadoutTOC, len);
  234. uint32_t capacity = getScsiCapacity(
  235. scsiDev.target->cfg->sdSectorStart,
  236. scsiDev.target->liveCfg.bytesPerSector,
  237. scsiDev.target->cfg->scsiSectors);
  238. // Replace start of leadout track
  239. if (MSF)
  240. {
  241. LBA2MSF(capacity, scsiDev.data + 8);
  242. }
  243. else
  244. {
  245. scsiDev.data[8] = capacity >> 24;
  246. scsiDev.data[9] = capacity >> 16;
  247. scsiDev.data[10] = capacity >> 8;
  248. scsiDev.data[11] = capacity;
  249. }
  250. if (len > allocationLength)
  251. {
  252. len = allocationLength;
  253. }
  254. scsiDev.dataLen = len;
  255. scsiDev.phase = DATA_IN;
  256. }
  257. else if (track <= 1)
  258. {
  259. // We only support track 1.
  260. // track 0 means "return all tracks"
  261. uint32_t len = sizeof(SimpleTOC);
  262. memcpy(scsiDev.data, SimpleTOC, len);
  263. uint32_t capacity = getScsiCapacity(
  264. scsiDev.target->cfg->sdSectorStart,
  265. scsiDev.target->liveCfg.bytesPerSector,
  266. scsiDev.target->cfg->scsiSectors);
  267. // Replace start of leadout track
  268. if (MSF)
  269. {
  270. LBA2MSF(capacity, scsiDev.data + 0x10);
  271. }
  272. else
  273. {
  274. scsiDev.data[0x10] = capacity >> 24;
  275. scsiDev.data[0x11] = capacity >> 16;
  276. scsiDev.data[0x12] = capacity >> 8;
  277. scsiDev.data[0x13] = capacity;
  278. }
  279. if (len > allocationLength)
  280. {
  281. len = allocationLength;
  282. }
  283. scsiDev.dataLen = len;
  284. scsiDev.phase = DATA_IN;
  285. }
  286. else
  287. {
  288. scsiDev.status = CHECK_CONDITION;
  289. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  290. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  291. scsiDev.phase = STATUS;
  292. }
  293. }
  294. static void doReadSessionInfoSimple(uint8_t session, uint16_t allocationLength)
  295. {
  296. uint32_t len = sizeof(SessionTOC);
  297. memcpy(scsiDev.data, SessionTOC, len);
  298. if (len > allocationLength)
  299. {
  300. len = allocationLength;
  301. }
  302. scsiDev.dataLen = len;
  303. scsiDev.phase = DATA_IN;
  304. }
  305. static inline uint8_t
  306. fromBCD(uint8_t val)
  307. {
  308. return ((val >> 4) * 10) + (val & 0xF);
  309. }
  310. static void doReadFullTOCSimple(int convertBCD, uint8_t session, uint16_t allocationLength)
  311. {
  312. // We only support session 1.
  313. if (session > 1)
  314. {
  315. scsiDev.status = CHECK_CONDITION;
  316. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  317. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  318. scsiDev.phase = STATUS;
  319. }
  320. else
  321. {
  322. uint32_t len = sizeof(FullTOC);
  323. memcpy(scsiDev.data, FullTOC, len);
  324. if (convertBCD)
  325. {
  326. int descriptor = 4;
  327. while (descriptor < len)
  328. {
  329. int i;
  330. for (i = 0; i < 7; ++i)
  331. {
  332. scsiDev.data[descriptor + i] =
  333. fromBCD(scsiDev.data[descriptor + 4 + i]);
  334. }
  335. descriptor += 11;
  336. }
  337. }
  338. if (len > allocationLength)
  339. {
  340. len = allocationLength;
  341. }
  342. scsiDev.dataLen = len;
  343. scsiDev.phase = DATA_IN;
  344. }
  345. }
  346. void doReadHeaderSimple(bool MSF, uint32_t lba, uint16_t allocationLength)
  347. {
  348. uint32_t len = sizeof(SimpleHeader);
  349. memcpy(scsiDev.data, SimpleHeader, len);
  350. if (len > allocationLength)
  351. {
  352. len = allocationLength;
  353. }
  354. scsiDev.dataLen = len;
  355. scsiDev.phase = DATA_IN;
  356. }
  357. void doReadDiscInformationSimple(uint16_t allocationLength)
  358. {
  359. uint32_t len = sizeof(DiscInformation);
  360. memcpy(scsiDev.data, DiscInformation, len);
  361. if (len > allocationLength)
  362. {
  363. len = allocationLength;
  364. }
  365. scsiDev.dataLen = len;
  366. scsiDev.phase = DATA_IN;
  367. }
  368. /*********************************/
  369. /* TOC generation from cue sheet */
  370. /*********************************/
  371. // Fetch track info based on LBA
  372. static void getTrackFromLBA(CUEParser &parser, uint32_t lba, CUETrackInfo *result)
  373. {
  374. // Track info in case we have no .cue file
  375. result->file_mode = CUEFile_BINARY;
  376. result->track_mode = CUETrack_MODE1_2048;
  377. result->sector_length = 2048;
  378. result->track_number = 1;
  379. const CUETrackInfo *tmptrack;
  380. while ((tmptrack = parser.next_track()) != NULL)
  381. {
  382. if (tmptrack->track_start <= lba)
  383. {
  384. *result = *tmptrack;
  385. }
  386. else
  387. {
  388. break;
  389. }
  390. }
  391. }
  392. // Format track info read from cue sheet into the format used by ReadTOC command.
  393. // Refer to T10/1545-D MMC-4 Revision 5a, "Response Format 0000b: Formatted TOC"
  394. static void formatTrackInfo(const CUETrackInfo *track, uint8_t *dest, bool use_MSF_time)
  395. {
  396. uint8_t control_adr = 0x14; // Digital track
  397. if (track->track_mode == CUETrack_AUDIO)
  398. {
  399. control_adr = 0x10; // Audio track
  400. }
  401. dest[0] = 0; // Reserved
  402. dest[1] = control_adr;
  403. dest[2] = track->track_number;
  404. dest[3] = 0; // Reserved
  405. if (use_MSF_time)
  406. {
  407. // Time in minute-second-frame format
  408. LBA2MSF(track->data_start, &dest[4]);
  409. }
  410. else
  411. {
  412. // Time as logical block address
  413. dest[4] = (track->data_start >> 24) & 0xFF;
  414. dest[5] = (track->data_start >> 16) & 0xFF;
  415. dest[6] = (track->data_start >> 8) & 0xFF;
  416. dest[7] = (track->data_start >> 0) & 0xFF;
  417. }
  418. }
  419. // Load data from CUE sheet for the given device,
  420. // using the second half of scsiDev.data buffer for temporary storage.
  421. // Returns false if no cue sheet or it could not be opened.
  422. static bool loadCueSheet(image_config_t &img, CUEParser &parser)
  423. {
  424. if (!img.cuesheetfile.isOpen())
  425. {
  426. return false;
  427. }
  428. // Use second half of scsiDev.data as the buffer for cue sheet text
  429. size_t halfbufsize = sizeof(scsiDev.data) / 2;
  430. char *cuebuf = (char*)&scsiDev.data[halfbufsize];
  431. img.cuesheetfile.seek(0);
  432. int len = img.cuesheetfile.read(cuebuf, halfbufsize);
  433. if (len <= 0)
  434. {
  435. return false;
  436. }
  437. cuebuf[len] = '\0';
  438. parser = CUEParser(cuebuf);
  439. return true;
  440. }
  441. static void doReadTOC(bool MSF, uint8_t track, uint16_t allocationLength)
  442. {
  443. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  444. CUEParser parser;
  445. if (!loadCueSheet(img, parser))
  446. {
  447. // No CUE sheet, use hardcoded data
  448. return doReadTOCSimple(MSF, track, allocationLength);
  449. }
  450. // Format track info
  451. uint8_t *trackdata = &scsiDev.data[4];
  452. int trackcount = 0;
  453. int firsttrack = -1;
  454. int lasttrack = -1;
  455. const CUETrackInfo *trackinfo;
  456. while ((trackinfo = parser.next_track()) != NULL)
  457. {
  458. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  459. lasttrack = trackinfo->track_number;
  460. if (track == 0 || track == trackinfo->track_number)
  461. {
  462. formatTrackInfo(trackinfo, &trackdata[8 * trackcount], MSF);
  463. trackcount += 1;
  464. }
  465. }
  466. // Format lead-out track info
  467. if (track == 0 || track == 0xAA)
  468. {
  469. CUETrackInfo leadout = {};
  470. leadout.track_number = 0xAA;
  471. leadout.track_mode = CUETrack_MODE1_2048;
  472. leadout.data_start = img.scsiSectors;
  473. formatTrackInfo(&leadout, &trackdata[8 * trackcount], MSF);
  474. trackcount += 1;
  475. }
  476. // Format response header
  477. uint16_t toc_length = 2 + trackcount * 8;
  478. scsiDev.data[0] = toc_length >> 8;
  479. scsiDev.data[1] = toc_length & 0xFF;
  480. scsiDev.data[2] = firsttrack;
  481. scsiDev.data[3] = lasttrack;
  482. if (trackcount == 0)
  483. {
  484. // Unknown track requested
  485. scsiDev.status = CHECK_CONDITION;
  486. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  487. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  488. scsiDev.phase = STATUS;
  489. }
  490. else
  491. {
  492. uint32_t len = 2 + toc_length;
  493. if (len > allocationLength)
  494. {
  495. len = allocationLength;
  496. }
  497. scsiDev.dataLen = len;
  498. scsiDev.phase = DATA_IN;
  499. }
  500. }
  501. static void doReadSessionInfo(uint8_t session, uint16_t allocationLength)
  502. {
  503. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  504. CUEParser parser;
  505. if (!loadCueSheet(img, parser))
  506. {
  507. // No CUE sheet, use hardcoded data
  508. return doReadSessionInfoSimple(session, allocationLength);
  509. }
  510. uint32_t len = sizeof(SessionTOC);
  511. memcpy(scsiDev.data, SessionTOC, len);
  512. // Replace first track info in the session table
  513. // based on data from CUE sheet.
  514. const CUETrackInfo *trackinfo = parser.next_track();
  515. if (trackinfo)
  516. {
  517. formatTrackInfo(trackinfo, &scsiDev.data[4], false);
  518. }
  519. if (len > allocationLength)
  520. {
  521. len = allocationLength;
  522. }
  523. scsiDev.dataLen = len;
  524. scsiDev.phase = DATA_IN;
  525. }
  526. // Convert logical block address to CD-ROM time in the raw TOC format
  527. static void LBA2MSFRaw(uint32_t LBA, uint8_t* MSF)
  528. {
  529. MSF[2] = LBA % 75; // Frames
  530. uint32_t rem = LBA / 75;
  531. MSF[1] = rem % 60; // Seconds
  532. MSF[0] = rem / 60; // Minutes
  533. }
  534. // Convert logical block address to CD-ROM time in binary coded decimal format
  535. static void LBA2MSFBCD(uint32_t LBA, uint8_t* MSF)
  536. {
  537. uint8_t fra = LBA % 75;
  538. uint32_t rem = LBA / 75;
  539. uint8_t sec = rem % 60;
  540. uint8_t min = rem / 60;
  541. MSF[0] = ((min / 10) << 4) | (min % 10);
  542. MSF[1] = ((sec / 10) << 4) | (sec % 10);
  543. MSF[2] = ((fra / 10) << 4) | (fra % 10);
  544. }
  545. // Format track info read from cue sheet into the format used by ReadFullTOC command.
  546. // Refer to T10/1545-D MMC-4 Revision 5a, "Response Format 0010b: Raw TOC"
  547. static void formatRawTrackInfo(const CUETrackInfo *track, uint8_t *dest)
  548. {
  549. uint8_t control_adr = 0x14; // Digital track
  550. if (track->track_mode == CUETrack_AUDIO)
  551. {
  552. control_adr = 0x10; // Audio track
  553. }
  554. dest[0] = 0x01; // Session always 1
  555. dest[1] = control_adr;
  556. dest[2] = 0x00; // "TNO", always 0?
  557. dest[3] = track->track_number; // "POINT", contains track number
  558. if (track->pregap_start > 0)
  559. {
  560. LBA2MSFRaw(track->pregap_start, &dest[4]);
  561. }
  562. else
  563. {
  564. LBA2MSFRaw(track->data_start, &dest[4]);
  565. }
  566. dest[7] = 0; // HOUR
  567. LBA2MSFBCD(track->data_start, &dest[8]);
  568. }
  569. static void doReadFullTOC(int convertBCD, uint8_t session, uint16_t allocationLength)
  570. {
  571. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  572. CUEParser parser;
  573. if (!loadCueSheet(img, parser))
  574. {
  575. // No CUE sheet, use hardcoded data
  576. return doReadFullTOCSimple(convertBCD, session, allocationLength);
  577. }
  578. // We only support session 1.
  579. if (session > 1)
  580. {
  581. scsiDev.status = CHECK_CONDITION;
  582. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  583. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  584. scsiDev.phase = STATUS;
  585. return;
  586. }
  587. // Take the beginning of the hardcoded TOC as base
  588. uint32_t len = 4 + 11 * 3; // Header, A0, A1, A2
  589. memcpy(scsiDev.data, FullTOC, len);
  590. // Add track descriptors
  591. int trackcount = 0;
  592. int firsttrack = -1;
  593. int lasttrack = -1;
  594. const CUETrackInfo *trackinfo;
  595. while ((trackinfo = parser.next_track()) != NULL)
  596. {
  597. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  598. lasttrack = trackinfo->track_number;
  599. formatRawTrackInfo(trackinfo, &scsiDev.data[len]);
  600. trackcount += 1;
  601. len += 11;
  602. }
  603. // First and last track numbers
  604. scsiDev.data[12] = firsttrack;
  605. scsiDev.data[23] = lasttrack;
  606. // Leadout track position
  607. LBA2MSFBCD(img.scsiSectors, &scsiDev.data[34]);
  608. // Append recordable disc records b0 and c0 indicating non-recordable disc
  609. memcpy(scsiDev.data + len, &FullTOC[48], 22);
  610. len += 22;
  611. // Correct the record length in header
  612. uint16_t toclen = len - 2;
  613. scsiDev.data[0] = toclen >> 8;
  614. scsiDev.data[1] = toclen & 0xFF;
  615. if (len > allocationLength)
  616. {
  617. len = allocationLength;
  618. }
  619. scsiDev.dataLen = len;
  620. scsiDev.phase = DATA_IN;
  621. }
  622. // SCSI-3 MMC Read Header command, seems to be deprecated in later standards.
  623. // Refer to ANSI X3.304-1997
  624. void doReadHeader(bool MSF, uint32_t lba, uint16_t allocationLength)
  625. {
  626. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  627. #if ENABLE_AUDIO_OUTPUT
  628. // terminate audio playback if active on this target (Annex C)
  629. audio_stop(img.scsiId & 7);
  630. #endif
  631. CUEParser parser;
  632. if (!loadCueSheet(img, parser))
  633. {
  634. // No CUE sheet, use hardcoded data
  635. return doReadHeaderSimple(MSF, lba, allocationLength);
  636. }
  637. // Take the hardcoded header as base
  638. uint32_t len = sizeof(SimpleHeader);
  639. memcpy(scsiDev.data, SimpleHeader, len);
  640. // Search the track with the requested LBA
  641. CUETrackInfo trackinfo = {};
  642. getTrackFromLBA(parser, lba, &trackinfo);
  643. // Track mode (audio / data)
  644. if (trackinfo.track_mode == CUETrack_AUDIO)
  645. {
  646. scsiDev.data[0] = 0;
  647. }
  648. // Track start
  649. if (MSF)
  650. {
  651. LBA2MSF(trackinfo.data_start, &scsiDev.data[4]);
  652. }
  653. else
  654. {
  655. scsiDev.data[4] = (trackinfo.data_start >> 24) & 0xFF;
  656. scsiDev.data[5] = (trackinfo.data_start >> 16) & 0xFF;
  657. scsiDev.data[6] = (trackinfo.data_start >> 8) & 0xFF;
  658. scsiDev.data[7] = (trackinfo.data_start >> 0) & 0xFF;
  659. }
  660. if (len > allocationLength)
  661. {
  662. len = allocationLength;
  663. }
  664. scsiDev.dataLen = len;
  665. scsiDev.phase = DATA_IN;
  666. }
  667. void doReadDiscInformation(uint16_t allocationLength)
  668. {
  669. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  670. CUEParser parser;
  671. if (!loadCueSheet(img, parser))
  672. {
  673. // No CUE sheet, use hardcoded data
  674. return doReadDiscInformationSimple(allocationLength);
  675. }
  676. // Take the hardcoded header as base
  677. uint32_t len = sizeof(DiscInformation);
  678. memcpy(scsiDev.data, DiscInformation, len);
  679. // Find first and last track number
  680. int firsttrack = -1;
  681. int lasttrack = -1;
  682. const CUETrackInfo *trackinfo;
  683. while ((trackinfo = parser.next_track()) != NULL)
  684. {
  685. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  686. lasttrack = trackinfo->track_number;
  687. }
  688. scsiDev.data[3] = firsttrack;
  689. scsiDev.data[5] = firsttrack;
  690. scsiDev.data[6] = lasttrack;
  691. if (len > allocationLength)
  692. {
  693. len = allocationLength;
  694. }
  695. scsiDev.dataLen = len;
  696. scsiDev.phase = DATA_IN;
  697. }
  698. /****************************************/
  699. /* CUE sheet check at image load time */
  700. /****************************************/
  701. bool cdromValidateCueSheet(image_config_t &img)
  702. {
  703. CUEParser parser;
  704. if (!loadCueSheet(img, parser))
  705. {
  706. return false;
  707. }
  708. const CUETrackInfo *trackinfo;
  709. int trackcount = 0;
  710. while ((trackinfo = parser.next_track()) != NULL)
  711. {
  712. trackcount++;
  713. if (trackinfo->track_mode != CUETrack_AUDIO &&
  714. trackinfo->track_mode != CUETrack_MODE1_2048 &&
  715. trackinfo->track_mode != CUETrack_MODE1_2352)
  716. {
  717. log("---- Warning: track ", trackinfo->track_number, " has unsupported mode ", (int)trackinfo->track_mode);
  718. }
  719. if (trackinfo->file_mode != CUEFile_BINARY)
  720. {
  721. log("---- Unsupported CUE data file mode ", (int)trackinfo->file_mode);
  722. }
  723. }
  724. if (trackcount == 0)
  725. {
  726. log("---- Opened cue sheet but no valid tracks found");
  727. return false;
  728. }
  729. log("---- Cue sheet loaded with ", (int)trackcount, " tracks");
  730. return true;
  731. }
  732. /**************************************/
  733. /* Ejection and image switching logic */
  734. /**************************************/
  735. // Reinsert any ejected CDROMs on reboot
  736. void cdromReinsertFirstImage(image_config_t &img)
  737. {
  738. if (img.image_index > 0)
  739. {
  740. // Multiple images for this drive, force restart from first one
  741. debuglog("---- Restarting from first CD-ROM image");
  742. img.image_index = 9;
  743. cdromSwitchNextImage(img);
  744. }
  745. else if (img.ejected)
  746. {
  747. // Reinsert the single image
  748. debuglog("---- Closing CD-ROM tray");
  749. img.ejected = false;
  750. img.cdrom_events = 2; // New media
  751. }
  752. }
  753. // Check if we have multiple CD-ROM images to cycle when drive is ejected.
  754. bool cdromSwitchNextImage(image_config_t &img)
  755. {
  756. // Check if we have a next image to load, so that drive is closed next time the host asks.
  757. img.image_index++;
  758. char filename[MAX_FILE_PATH];
  759. int target_idx = img.scsiId & 7;
  760. if (!scsiDiskGetImageNameFromConfig(img, filename, sizeof(filename)))
  761. {
  762. img.image_index = 0;
  763. scsiDiskGetImageNameFromConfig(img, filename, sizeof(filename));
  764. }
  765. #ifdef ENABLE_AUDIO_OUTPUT
  766. // if in progress for this device, terminate audio playback immediately (Annex C)
  767. audio_stop(target_idx);
  768. // Reset position tracking for the new image
  769. audio_get_status_code(target_idx); // trash audio status code
  770. audio_clear_bytes_read(target_idx);
  771. #endif
  772. mechanism_status[target_idx].last_lba = 0;
  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. uint8_t target = img.scsiId & 7;
  835. #ifdef ENABLE_AUDIO_OUTPUT
  836. if (status) {
  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. if (current_lba) *current_lba = mechanism_status[target].last_lba
  844. + audio_get_bytes_read(target) / 2352;
  845. #else
  846. if (status) *status = 0; // audio status code for 'unsupported/invalid' and not-playing indicator
  847. if (current_lba) *current_lba = mechanism_status[target].last_lba;
  848. #endif
  849. }
  850. static void doPlayAudio(uint32_t lba, uint32_t length)
  851. {
  852. #ifdef ENABLE_AUDIO_OUTPUT
  853. debuglog("------ CD-ROM Play Audio request at ", lba, " for ", length, " sectors");
  854. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  855. uint8_t target_id = img.scsiId & 7;
  856. // Per Annex C terminate playback immediately if already in progress on
  857. // the current target. Non-current targets may also get their audio
  858. // interrupted later due to hardware limitations
  859. audio_stop(img.scsiId & 7);
  860. // if transfer length is zero no audio playback happens.
  861. // don't treat as an error per SCSI-2; handle via short-circuit
  862. if (length == 0)
  863. {
  864. scsiDev.status = 0;
  865. scsiDev.phase = STATUS;
  866. return;
  867. }
  868. // if actual playback is requested perform steps to verify prior to playback
  869. CUEParser parser;
  870. if (loadCueSheet(img, parser))
  871. {
  872. CUETrackInfo trackinfo = {};
  873. getTrackFromLBA(parser, lba, &trackinfo);
  874. if (lba == 0xFFFFFFFF)
  875. {
  876. // request to start playback from 'current position'
  877. lba = mechanism_status[target_id].last_lba + audio_get_bytes_read(target_id) / 2352;
  878. }
  879. // --- TODO --- determine proper track offset, software I tested with had a tendency
  880. // to ask for offsets that seem to hint at 2048 here, not the 2352 you'd assume.
  881. // Might be due to a mode page reporting something unexpected? Needs investigation.
  882. uint64_t offset = trackinfo.file_offset + 2048 * (lba - trackinfo.data_start);
  883. debuglog("------ Play audio CD: ", (int)length, " sectors starting at ", (int)lba,
  884. ", track number ", trackinfo.track_number, ", data offset in file ", (int)offset);
  885. if (trackinfo.track_mode != CUETrack_AUDIO)
  886. {
  887. debuglog("---- Host tried audio playback on track type ", (int)trackinfo.track_mode);
  888. scsiDev.status = CHECK_CONDITION;
  889. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  890. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  891. scsiDev.phase = STATUS;
  892. return;
  893. }
  894. // playback request appears to be sane, so perform it
  895. char filename[MAX_FILE_PATH];
  896. if (!img.file.name(filename, sizeof(filename)))
  897. {
  898. // No underlying file available?
  899. log("---- No filename for SCSI ID ", target_id);
  900. scsiDev.status = CHECK_CONDITION;
  901. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  902. scsiDev.target->sense.asc = 0x0000;
  903. scsiDev.phase = STATUS;
  904. return;
  905. }
  906. if (!audio_play(target_id, filename, offset, offset + length * 2352, false))
  907. {
  908. // Underlying data/media error? Fake a disk scratch, which should
  909. // be a condition most CD-DA players are expecting
  910. scsiDev.status = CHECK_CONDITION;
  911. scsiDev.target->sense.code = MEDIUM_ERROR;
  912. scsiDev.target->sense.asc = 0x1106; // CIRC UNRECOVERED ERROR
  913. scsiDev.phase = STATUS;
  914. return;
  915. }
  916. mechanism_status[target_id].last_lba = lba;
  917. scsiDev.status = 0;
  918. scsiDev.phase = STATUS;
  919. }
  920. else
  921. {
  922. // virtual drive supports audio, just not with this disk image
  923. debuglog("---- Request to play audio on non-audio image");
  924. scsiDev.status = CHECK_CONDITION;
  925. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  926. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  927. scsiDev.phase = STATUS;
  928. }
  929. #else
  930. debuglog("---- Target does not support audio playback");
  931. // per SCSI-2, targets not supporting audio respond to zero-length
  932. // PLAY AUDIO commands with ILLEGAL REQUEST; this seems to be a check
  933. // performed by at least some audio playback software
  934. scsiDev.status = CHECK_CONDITION;
  935. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  936. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  937. scsiDev.phase = STATUS;
  938. #endif
  939. }
  940. static void doPauseResumeAudio(bool resume)
  941. {
  942. #ifdef ENABLE_AUDIO_OUTPUT
  943. log("------ CD-ROM ", resume ? "resume" : "pause", " audio playback");
  944. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  945. uint8_t target_id = img.scsiId & 7;
  946. if (audio_is_playing(target_id))
  947. {
  948. audio_set_paused(target_id, !resume);
  949. scsiDev.status = 0;
  950. scsiDev.phase = STATUS;
  951. }
  952. else
  953. {
  954. scsiDev.status = CHECK_CONDITION;
  955. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  956. scsiDev.target->sense.asc = 0x2C00; // COMMAND SEQUENCE ERROR
  957. scsiDev.phase = STATUS;
  958. }
  959. #else
  960. debuglog("---- Target does not support audio pausing");
  961. scsiDev.status = CHECK_CONDITION;
  962. scsiDev.target->sense.code = ILLEGAL_REQUEST; // assumed from PLAY AUDIO(10)
  963. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  964. scsiDev.phase = STATUS;
  965. #endif
  966. }
  967. static void doMechanismStatus(uint16_t allocation_length)
  968. {
  969. uint8_t *buf = scsiDev.data;
  970. uint8_t status;
  971. uint32_t lba;
  972. cdromGetAudioPlaybackStatus(&status, &lba, true);
  973. *buf++ = 0x00; // No fault state
  974. *buf++ = (status) ? 0x20 : 0x00; // Currently playing?
  975. *buf++ = (lba >> 16) & 0xFF;
  976. *buf++ = (lba >> 8) & 0xFF;
  977. *buf++ = (lba >> 0) & 0xFF;
  978. *buf++ = 0; // No CD changer
  979. *buf++ = 0;
  980. *buf++ = 0;
  981. int len = 8;
  982. if (len > allocation_length) len = allocation_length;
  983. scsiDev.dataLen = len;
  984. scsiDev.phase = DATA_IN;
  985. }
  986. /*******************************************/
  987. /* CD-ROM data reading in low level format */
  988. /*******************************************/
  989. static void doReadCD(uint32_t lba, uint32_t length, uint8_t sector_type,
  990. uint8_t main_channel, uint8_t sub_channel)
  991. {
  992. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  993. #if ENABLE_AUDIO_OUTPUT
  994. // terminate audio playback if active on this target (Annex C)
  995. audio_stop(img.scsiId & 7);
  996. #endif
  997. CUEParser parser;
  998. if (!loadCueSheet(img, parser)
  999. && (sector_type == 0 || sector_type == 2)
  1000. && main_channel == 0x10 && sub_channel == 0)
  1001. {
  1002. // Simple case, return sector data directly
  1003. scsiDiskStartRead(lba, length);
  1004. return;
  1005. }
  1006. // Search the track with the requested LBA
  1007. // Supplies dummy data if no cue sheet is active.
  1008. CUETrackInfo trackinfo = {};
  1009. getTrackFromLBA(parser, lba, &trackinfo);
  1010. // Figure out the data offset in the file
  1011. uint64_t offset = trackinfo.file_offset + trackinfo.sector_length * (lba - trackinfo.data_start);
  1012. debuglog("------ Read CD: ", (int)length, " sectors starting at ", (int)lba,
  1013. ", track number ", trackinfo.track_number, ", sector size ", (int)trackinfo.sector_length,
  1014. ", main channel ", main_channel, ", sub channel ", sub_channel,
  1015. ", data offset in file ", (int)offset);
  1016. // Verify sector type
  1017. if (sector_type != 0)
  1018. {
  1019. bool sector_type_ok = false;
  1020. if (sector_type == 1 && trackinfo.track_mode == CUETrack_AUDIO)
  1021. {
  1022. sector_type_ok = true;
  1023. }
  1024. else if (sector_type == 2 && trackinfo.track_mode == CUETrack_MODE1_2048)
  1025. {
  1026. sector_type_ok = true;
  1027. }
  1028. if (!sector_type_ok)
  1029. {
  1030. debuglog("---- Failed sector type check, host requested ", (int)sector_type, " CUE file has ", (int)trackinfo.track_mode);
  1031. scsiDev.status = CHECK_CONDITION;
  1032. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1033. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1034. scsiDev.phase = STATUS;
  1035. return;
  1036. }
  1037. }
  1038. // Select fields to transfer
  1039. // Refer to table 351 in T10/1545-D MMC-4 Revision 5a
  1040. // Only the mandatory cases are supported.
  1041. int sector_length = 0;
  1042. int skip_begin = 0;
  1043. bool add_fake_headers = false;
  1044. if (main_channel == 0)
  1045. {
  1046. // No actual data requested, just sector type check or subchannel
  1047. sector_length = 0;
  1048. }
  1049. else if (trackinfo.track_mode == CUETrack_AUDIO)
  1050. {
  1051. // Transfer whole 2352 byte audio sectors from file to host
  1052. sector_length = 2352;
  1053. }
  1054. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && main_channel == 0x10)
  1055. {
  1056. // Transfer whole 2048 byte data sectors from file to host
  1057. sector_length = 2048;
  1058. }
  1059. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && (main_channel & 0xB8) == 0xB8)
  1060. {
  1061. // Transfer 2048 bytes of data from file and fake the headers
  1062. sector_length = 2048;
  1063. add_fake_headers = true;
  1064. debuglog("------ Host requested ECC data but image file lacks it, replacing with zeros");
  1065. }
  1066. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && main_channel == 0x10)
  1067. {
  1068. // Transfer the 2048 byte payload of data sector to host.
  1069. sector_length = 2048;
  1070. skip_begin = 16;
  1071. }
  1072. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && (main_channel & 0xB8) == 0xB8)
  1073. {
  1074. // Transfer whole 2352 byte data sector with ECC to host
  1075. sector_length = 2352;
  1076. }
  1077. else
  1078. {
  1079. debuglog("---- Unsupported channel request for track type ", (int)trackinfo.track_mode);
  1080. scsiDev.status = CHECK_CONDITION;
  1081. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1082. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1083. scsiDev.phase = STATUS;
  1084. return;
  1085. }
  1086. bool field_q_subchannel = false;
  1087. if (sub_channel == 2)
  1088. {
  1089. // Include position information in Q subchannel
  1090. field_q_subchannel = true;
  1091. }
  1092. else if (sub_channel != 0)
  1093. {
  1094. debuglog("---- Unsupported subchannel request");
  1095. scsiDev.status = CHECK_CONDITION;
  1096. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1097. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1098. scsiDev.phase = STATUS;
  1099. return;
  1100. }
  1101. scsiDev.phase = DATA_IN;
  1102. scsiDev.dataLen = 0;
  1103. scsiDev.dataPtr = 0;
  1104. scsiEnterPhase(DATA_IN);
  1105. // Use two buffers alternately for formatting sector data
  1106. uint32_t result_length = sector_length + (field_q_subchannel ? 16 : 0) + (add_fake_headers ? 304 : 0);
  1107. uint8_t *buf0 = scsiDev.data;
  1108. uint8_t *buf1 = scsiDev.data + result_length;
  1109. // Format the sectors for transfer
  1110. for (uint32_t idx = 0; idx < length; idx++)
  1111. {
  1112. platform_poll();
  1113. img.file.seek(offset + idx * trackinfo.sector_length + skip_begin);
  1114. // Verify that previous write using this buffer has finished
  1115. uint8_t *buf = ((idx & 1) ? buf1 : buf0);
  1116. uint8_t *bufstart = buf;
  1117. uint32_t start = millis();
  1118. while (!scsiIsWriteFinished(buf + result_length - 1) && !scsiDev.resetFlag)
  1119. {
  1120. if ((uint32_t)(millis() - start) > 5000)
  1121. {
  1122. log("doReadCD() timeout waiting for previous to finish");
  1123. scsiDev.resetFlag = 1;
  1124. }
  1125. platform_poll();
  1126. }
  1127. if (scsiDev.resetFlag) break;
  1128. if (add_fake_headers)
  1129. {
  1130. // 12-byte data sector sync pattern
  1131. *buf++ = 0x00;
  1132. for (int i = 0; i < 10; i++)
  1133. {
  1134. *buf++ = 0xFF;
  1135. }
  1136. *buf++ = 0x00;
  1137. // 4-byte data sector header
  1138. LBA2MSFBCD(lba + idx, buf);
  1139. buf += 3;
  1140. *buf++ = 0x01; // Mode 1
  1141. }
  1142. if (sector_length > 0)
  1143. {
  1144. // User data
  1145. img.file.read(buf, sector_length);
  1146. buf += sector_length;
  1147. }
  1148. if (add_fake_headers)
  1149. {
  1150. // 288 bytes of ECC
  1151. memset(buf, 0, 288);
  1152. buf += 288;
  1153. }
  1154. if (field_q_subchannel)
  1155. {
  1156. // Formatted Q subchannel data
  1157. // Refer to table 354 in T10/1545-D MMC-4 Revision 5a
  1158. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14); // Control & ADR
  1159. *buf++ = trackinfo.track_number;
  1160. *buf++ = (lba + idx >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1161. LBA2MSFRaw(lba + idx, buf); buf += 3;
  1162. *buf++ = 0;
  1163. LBA2MSFRaw(lba + idx, buf); buf += 3;
  1164. *buf++ = 0; *buf++ = 0; // CRC (optional)
  1165. *buf++ = 0; *buf++ = 0; *buf++ = 0; // (pad)
  1166. *buf++ = 0; // No P subchannel
  1167. }
  1168. assert(buf == bufstart + result_length);
  1169. scsiStartWrite(bufstart, result_length);
  1170. }
  1171. scsiFinishWrite();
  1172. scsiDev.status = 0;
  1173. scsiDev.phase = STATUS;
  1174. }
  1175. static void doReadSubchannel(bool time, bool subq, uint8_t parameter, uint8_t track_number, uint16_t allocation_length)
  1176. {
  1177. uint8_t *buf = scsiDev.data;
  1178. if (parameter == 0x01)
  1179. {
  1180. uint8_t audiostatus;
  1181. uint32_t lba;
  1182. cdromGetAudioPlaybackStatus(&audiostatus, &lba, false);
  1183. debuglog("------ Get audio playback position: status ", (int)audiostatus, " lba ", (int)lba);
  1184. // Fetch current track info
  1185. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1186. CUEParser parser;
  1187. CUETrackInfo trackinfo = {};
  1188. loadCueSheet(img, parser);
  1189. getTrackFromLBA(parser, lba, &trackinfo);
  1190. // Request sub channel data at current playback position
  1191. *buf++ = 0; // Reserved
  1192. *buf++ = audiostatus;
  1193. int len = 12;
  1194. *buf++ = 0; // Subchannel data length (MSB)
  1195. *buf++ = len; // Subchannel data length (LSB)
  1196. *buf++ = 0x01; // Subchannel data format
  1197. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14);
  1198. *buf++ = trackinfo.track_number;
  1199. *buf++ = (lba >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1200. *buf++ = (lba >> 24) & 0xFF; // Absolute block address
  1201. *buf++ = (lba >> 16) & 0xFF;
  1202. *buf++ = (lba >> 8) & 0xFF;
  1203. *buf++ = (lba >> 0) & 0xFF;
  1204. uint32_t relpos = (uint32_t)((int32_t)lba - (int32_t)trackinfo.data_start);
  1205. *buf++ = (relpos >> 24) & 0xFF; // Track relative position (may be negative)
  1206. *buf++ = (relpos >> 16) & 0xFF;
  1207. *buf++ = (relpos >> 8) & 0xFF;
  1208. *buf++ = (relpos >> 0) & 0xFF;
  1209. if (len > allocation_length) len = allocation_length;
  1210. scsiDev.dataLen = len;
  1211. scsiDev.phase = DATA_IN;
  1212. }
  1213. else
  1214. {
  1215. debuglog("---- Unsupported subchannel request");
  1216. scsiDev.status = CHECK_CONDITION;
  1217. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1218. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1219. scsiDev.phase = STATUS;
  1220. return;
  1221. }
  1222. }
  1223. /**************************************/
  1224. /* CD-ROM command dispatching */
  1225. /**************************************/
  1226. // Handle direct-access scsi device commands
  1227. extern "C" int scsiCDRomCommand()
  1228. {
  1229. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1230. int commandHandled = 1;
  1231. uint8_t command = scsiDev.cdb[0];
  1232. if (command == 0x1B)
  1233. {
  1234. #if ENABLE_AUDIO_OUTPUT
  1235. // terminate audio playback if active on this target (Annex C)
  1236. audio_stop(img.scsiId & 7);
  1237. #endif
  1238. if ((scsiDev.cdb[4] & 2))
  1239. {
  1240. // CD-ROM load & eject
  1241. int start = scsiDev.cdb[4] & 1;
  1242. if (start)
  1243. {
  1244. debuglog("------ CDROM close tray");
  1245. img.ejected = false;
  1246. img.cdrom_events = 2; // New media
  1247. }
  1248. else
  1249. {
  1250. debuglog("------ CDROM open tray");
  1251. img.ejected = true;
  1252. img.cdrom_events = 3; // Media removal
  1253. }
  1254. }
  1255. else
  1256. {
  1257. // flow through to disk handler
  1258. commandHandled = 0;
  1259. }
  1260. }
  1261. else if (command == 0x43)
  1262. {
  1263. // CD-ROM Read TOC
  1264. bool MSF = (scsiDev.cdb[1] & 0x02);
  1265. uint8_t track = scsiDev.cdb[6];
  1266. uint16_t allocationLength =
  1267. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1268. scsiDev.cdb[8];
  1269. // Reject MMC commands for now, otherwise the TOC data format
  1270. // won't be understood.
  1271. // The "format" field is reserved for SCSI-2
  1272. uint8_t format = scsiDev.cdb[2] & 0x0F;
  1273. switch (format)
  1274. {
  1275. case 0: doReadTOC(MSF, track, allocationLength); break; // SCSI-2
  1276. case 1: doReadSessionInfo(MSF, allocationLength); break; // MMC2
  1277. case 2: doReadFullTOC(0, track, allocationLength); break; // MMC2
  1278. case 3: doReadFullTOC(1, track, allocationLength); break; // MMC2
  1279. default:
  1280. {
  1281. scsiDev.status = CHECK_CONDITION;
  1282. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1283. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1284. scsiDev.phase = STATUS;
  1285. }
  1286. }
  1287. }
  1288. else if (command == 0x44)
  1289. {
  1290. // CD-ROM Read Header
  1291. bool MSF = (scsiDev.cdb[1] & 0x02);
  1292. uint32_t lba = 0; // IGNORED for now
  1293. uint16_t allocationLength =
  1294. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1295. scsiDev.cdb[8];
  1296. doReadHeader(MSF, lba, allocationLength);
  1297. }
  1298. else if (command == 0x51)
  1299. {
  1300. uint16_t allocationLength =
  1301. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1302. scsiDev.cdb[8];
  1303. doReadDiscInformation(allocationLength);
  1304. }
  1305. else if (command == 0x4A)
  1306. {
  1307. // Get event status notifications (media change notifications)
  1308. bool immed = scsiDev.cdb[1] & 1;
  1309. doGetEventStatusNotification(immed);
  1310. }
  1311. else if (command == 0x45)
  1312. {
  1313. // PLAY AUDIO (10)
  1314. uint32_t lba =
  1315. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1316. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1317. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1318. scsiDev.cdb[5];
  1319. uint32_t blocks =
  1320. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1321. scsiDev.cdb[8];
  1322. doPlayAudio(lba, blocks);
  1323. }
  1324. else if (command == 0xA5)
  1325. {
  1326. // PLAY AUDIO (12)
  1327. uint32_t lba =
  1328. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1329. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1330. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1331. scsiDev.cdb[5];
  1332. uint32_t blocks =
  1333. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1334. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1335. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1336. scsiDev.cdb[9];
  1337. doPlayAudio(lba, blocks);
  1338. }
  1339. else if (command == 0x47)
  1340. {
  1341. // PLAY AUDIO (MSF)
  1342. uint32_t start = (scsiDev.cdb[3] * 60 + scsiDev.cdb[4]) * 75 + scsiDev.cdb[5];
  1343. uint32_t end = (scsiDev.cdb[6] * 60 + scsiDev.cdb[7]) * 75 + scsiDev.cdb[8];
  1344. doPlayAudio(start, end - start);
  1345. }
  1346. else if (command == 0x4B)
  1347. {
  1348. // PAUSE/RESUME AUDIO
  1349. doPauseResumeAudio(scsiDev.cdb[8] & 1);
  1350. }
  1351. else if (command == 0xBD)
  1352. {
  1353. // Mechanism status
  1354. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[8]) << 8) + scsiDev.cdb[9];
  1355. doMechanismStatus(allocationLength);
  1356. }
  1357. else if (command == 0xBB)
  1358. {
  1359. // Set CD speed (just ignored)
  1360. scsiDev.status = 0;
  1361. scsiDev.phase = STATUS;
  1362. }
  1363. else if (command == 0xBE)
  1364. {
  1365. // ReadCD (in low level format)
  1366. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1367. uint32_t lba =
  1368. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1369. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1370. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1371. scsiDev.cdb[5];
  1372. uint32_t blocks =
  1373. (((uint32_t) scsiDev.cdb[6]) << 16) +
  1374. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1375. (((uint32_t) scsiDev.cdb[8]));
  1376. uint8_t main_channel = scsiDev.cdb[9];
  1377. uint8_t sub_channel = scsiDev.cdb[10];
  1378. doReadCD(lba, blocks, sector_type, main_channel, sub_channel);
  1379. }
  1380. else if (command == 0xB9)
  1381. {
  1382. // ReadCD MSF
  1383. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1384. uint32_t start = (scsiDev.cdb[3] * 60 + scsiDev.cdb[4]) * 75 + scsiDev.cdb[5];
  1385. uint32_t end = (scsiDev.cdb[6] * 60 + scsiDev.cdb[7]) * 75 + scsiDev.cdb[8];
  1386. uint8_t main_channel = scsiDev.cdb[9];
  1387. uint8_t sub_channel = scsiDev.cdb[10];
  1388. doReadCD(start, end - start, sector_type, main_channel, sub_channel);
  1389. }
  1390. else if (command == 0x42)
  1391. {
  1392. // Read subchannel data
  1393. bool time = (scsiDev.cdb[1] & 0x02);
  1394. bool subq = (scsiDev.cdb[2] & 0x40);
  1395. uint8_t parameter = scsiDev.cdb[3];
  1396. uint8_t track_number = scsiDev.cdb[6];
  1397. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
  1398. doReadSubchannel(time, subq, parameter, track_number, allocationLength);
  1399. }
  1400. else if (command == 0x28)
  1401. {
  1402. // READ(10) for CDs (may need sector translation for cue file handling)
  1403. uint32_t lba =
  1404. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1405. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1406. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1407. scsiDev.cdb[5];
  1408. uint32_t blocks =
  1409. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1410. scsiDev.cdb[8];
  1411. doReadCD(lba, blocks, 0, 0x10, 0);
  1412. }
  1413. else if (command == 0xA8)
  1414. {
  1415. // READ(12) for CDs (may need sector translation for cue file handling)
  1416. uint32_t lba =
  1417. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1418. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1419. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1420. scsiDev.cdb[5];
  1421. uint32_t blocks =
  1422. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1423. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1424. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1425. scsiDev.cdb[9];
  1426. doReadCD(lba, blocks, 0, 0x10, 0);
  1427. }
  1428. else
  1429. {
  1430. commandHandled = 0;
  1431. }
  1432. return commandHandled;
  1433. }