BlueSCSI_cdrom.cpp 54 KB

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