BlueSCSI_cdrom.cpp 48 KB

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