ZuluSCSI_cdrom.cpp 55 KB

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