BlueSCSI_cdrom.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  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. static const uint8_t TrackInformation[] =
  180. {
  181. 0x00, // 0: data length, MSB
  182. 0x1A, // 1: data length, LSB
  183. 0x01, // 2: track number
  184. 0x01, // 3: session number
  185. 0x00, // 4: reserved
  186. 0x04, // 5: track mode and flags
  187. 0x8F, // 6: data mode and flags
  188. 0x00, // 7: nwa_v
  189. 0x00, // 8: track start address (MSB)
  190. 0x00, // 9: .
  191. 0x00, // 10: .
  192. 0x00, // 11: track start address (LSB)
  193. 0xFF, // 12: next writable address (MSB)
  194. 0xFF, // 13: .
  195. 0xFF, // 14: .
  196. 0xFF, // 15: next writable address (LSB)
  197. 0x00, // 16: free blocks (MSB)
  198. 0x00, // 17: .
  199. 0x00, // 18: .
  200. 0x00, // 19: free blocks (LSB)
  201. 0x00, // 20: fixed packet size (MSB)
  202. 0x00, // 21: .
  203. 0x00, // 22: .
  204. 0x00, // 23: fixed packet size (LSB)
  205. 0x00, // 24: track size (MSB)
  206. 0x00, // 25: .
  207. 0x00, // 26: .
  208. 0x00, // 27: track size (LSB)
  209. };
  210. // Convert logical block address to CD-ROM time
  211. static void LBA2MSF(int32_t LBA, uint8_t* MSF, bool relative)
  212. {
  213. if (!relative) {
  214. LBA += 150;
  215. }
  216. uint32_t ulba = LBA;
  217. if (LBA < 0) {
  218. ulba = LBA * -1;
  219. }
  220. MSF[2] = ulba % 75; // Frames
  221. uint32_t rem = ulba / 75;
  222. MSF[1] = rem % 60; // Seconds
  223. MSF[0] = rem / 60; // Minutes
  224. }
  225. // Convert logical block address to CD-ROM time in binary coded decimal format
  226. static void LBA2MSFBCD(int32_t LBA, uint8_t* MSF, bool relative)
  227. {
  228. LBA2MSF(LBA, MSF, relative);
  229. MSF[0] = ((MSF[0] / 10) << 4) | (MSF[0] % 10);
  230. MSF[1] = ((MSF[1] / 10) << 4) | (MSF[1] % 10);
  231. MSF[2] = ((MSF[2] / 10) << 4) | (MSF[2] % 10);
  232. }
  233. // Convert CD-ROM time to logical block address
  234. static int32_t MSF2LBA(uint8_t m, uint8_t s, uint8_t f, bool relative)
  235. {
  236. int32_t lba = (m * 60 + s) * 75 + f;
  237. if (!relative) lba -= 150;
  238. return lba;
  239. }
  240. // Gets the LBA position of the lead-out for the current image
  241. static uint32_t getLeadOutLBA(const CUETrackInfo* lasttrack)
  242. {
  243. if (lasttrack != nullptr && lasttrack->track_number != 0)
  244. {
  245. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  246. uint32_t lastTrackBlocks = (img.file.size() - lasttrack->file_offset)
  247. / lasttrack->sector_length;
  248. return lasttrack->track_start + lastTrackBlocks;
  249. }
  250. else
  251. {
  252. return 1;
  253. }
  254. }
  255. static void doReadTOCSimple(bool MSF, uint8_t track, uint16_t allocationLength)
  256. {
  257. if (track == 0xAA)
  258. {
  259. // 0xAA requests only lead-out track information (reports capacity)
  260. uint32_t len = sizeof(LeadoutTOC);
  261. memcpy(scsiDev.data, LeadoutTOC, len);
  262. uint32_t capacity = getScsiCapacity(
  263. scsiDev.target->cfg->sdSectorStart,
  264. scsiDev.target->liveCfg.bytesPerSector,
  265. scsiDev.target->cfg->scsiSectors);
  266. // Replace start of leadout track
  267. if (MSF)
  268. {
  269. scsiDev.data[8] = 0;
  270. LBA2MSF(capacity, scsiDev.data + 9, false);
  271. }
  272. else
  273. {
  274. scsiDev.data[8] = capacity >> 24;
  275. scsiDev.data[9] = capacity >> 16;
  276. scsiDev.data[10] = capacity >> 8;
  277. scsiDev.data[11] = capacity;
  278. }
  279. if (len > allocationLength)
  280. {
  281. len = allocationLength;
  282. }
  283. scsiDev.dataLen = len;
  284. scsiDev.phase = DATA_IN;
  285. }
  286. else if (track <= 1)
  287. {
  288. // We only support track 1.
  289. // track 0 means "return all tracks"
  290. uint32_t len = sizeof(SimpleTOC);
  291. memcpy(scsiDev.data, SimpleTOC, len);
  292. if (MSF)
  293. {
  294. scsiDev.data[0x0A] = 0x02;
  295. }
  296. uint32_t capacity = getScsiCapacity(
  297. scsiDev.target->cfg->sdSectorStart,
  298. scsiDev.target->liveCfg.bytesPerSector,
  299. scsiDev.target->cfg->scsiSectors);
  300. // Replace start of leadout track
  301. if (MSF)
  302. {
  303. scsiDev.data[0x10] = 0;
  304. LBA2MSF(capacity, scsiDev.data + 0x11, false);
  305. }
  306. else
  307. {
  308. scsiDev.data[0x10] = capacity >> 24;
  309. scsiDev.data[0x11] = capacity >> 16;
  310. scsiDev.data[0x12] = capacity >> 8;
  311. scsiDev.data[0x13] = capacity;
  312. }
  313. if (len > allocationLength)
  314. {
  315. len = allocationLength;
  316. }
  317. scsiDev.dataLen = len;
  318. scsiDev.phase = DATA_IN;
  319. }
  320. else
  321. {
  322. scsiDev.status = CHECK_CONDITION;
  323. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  324. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  325. scsiDev.phase = STATUS;
  326. }
  327. }
  328. static void doReadSessionInfoSimple(bool msf, uint16_t allocationLength)
  329. {
  330. uint32_t len = sizeof(SessionTOC);
  331. memcpy(scsiDev.data, SessionTOC, len);
  332. if (msf)
  333. {
  334. scsiDev.data[0x0A] = 0x02;
  335. }
  336. if (len > allocationLength)
  337. {
  338. len = allocationLength;
  339. }
  340. scsiDev.dataLen = len;
  341. scsiDev.phase = DATA_IN;
  342. }
  343. static void doReadFullTOCSimple(uint8_t session, uint16_t allocationLength, bool useBCD)
  344. {
  345. // We only support session 1.
  346. if (session > 1)
  347. {
  348. scsiDev.status = CHECK_CONDITION;
  349. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  350. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  351. scsiDev.phase = STATUS;
  352. }
  353. else
  354. {
  355. uint32_t len = sizeof(FullTOC);
  356. memcpy(scsiDev.data, FullTOC, len);
  357. // update leadout position
  358. // consistent 2048-byte blocks makes this easier than bin/cue version
  359. uint32_t capacity = getScsiCapacity(
  360. scsiDev.target->cfg->sdSectorStart,
  361. scsiDev.target->liveCfg.bytesPerSector,
  362. scsiDev.target->cfg->scsiSectors);
  363. if (useBCD) {
  364. LBA2MSFBCD(capacity, &scsiDev.data[34], false);
  365. } else {
  366. LBA2MSF(capacity, &scsiDev.data[34], false);
  367. }
  368. if (len > allocationLength)
  369. {
  370. len = allocationLength;
  371. }
  372. scsiDev.dataLen = len;
  373. scsiDev.phase = DATA_IN;
  374. }
  375. }
  376. void doReadDiscInformationSimple(uint16_t allocationLength)
  377. {
  378. uint32_t len = sizeof(DiscInformation);
  379. memcpy(scsiDev.data, DiscInformation, len);
  380. if (len > allocationLength)
  381. {
  382. len = allocationLength;
  383. }
  384. scsiDev.dataLen = len;
  385. scsiDev.phase = DATA_IN;
  386. }
  387. void doReadTrackInformationSimple(bool track, uint32_t lba, uint16_t allocationLength)
  388. {
  389. uint32_t len = sizeof(TrackInformation);
  390. memcpy(scsiDev.data, TrackInformation, len);
  391. uint32_t capacity = getScsiCapacity(
  392. scsiDev.target->cfg->sdSectorStart,
  393. scsiDev.target->liveCfg.bytesPerSector,
  394. scsiDev.target->cfg->scsiSectors);
  395. if (!track && lba >= capacity)
  396. {
  397. scsiDev.status = CHECK_CONDITION;
  398. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  399. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  400. scsiDev.phase = STATUS;
  401. }
  402. else
  403. {
  404. // update track size
  405. scsiDev.data[24] = capacity >> 24;
  406. scsiDev.data[25] = capacity >> 16;
  407. scsiDev.data[26] = capacity >> 8;
  408. scsiDev.data[27] = capacity;
  409. if (len > allocationLength)
  410. {
  411. len = allocationLength;
  412. }
  413. scsiDev.dataLen = len;
  414. scsiDev.phase = DATA_IN;
  415. }
  416. }
  417. /*********************************/
  418. /* TOC generation from cue sheet */
  419. /*********************************/
  420. // Fetch track info based on LBA
  421. static void getTrackFromLBA(CUEParser &parser, uint32_t lba, CUETrackInfo *result)
  422. {
  423. // Track info in case we have no .cue file
  424. result->file_mode = CUEFile_BINARY;
  425. result->track_mode = CUETrack_MODE1_2048;
  426. result->sector_length = 2048;
  427. result->track_number = 1;
  428. const CUETrackInfo *tmptrack;
  429. while ((tmptrack = parser.next_track()) != NULL)
  430. {
  431. if (tmptrack->track_start <= lba)
  432. {
  433. *result = *tmptrack;
  434. }
  435. else
  436. {
  437. break;
  438. }
  439. }
  440. }
  441. // Format track info read from cue sheet into the format used by ReadTOC command.
  442. // Refer to T10/1545-D MMC-4 Revision 5a, "Response Format 0000b: Formatted TOC"
  443. static void formatTrackInfo(const CUETrackInfo *track, uint8_t *dest, bool use_MSF_time)
  444. {
  445. uint8_t control_adr = 0x14; // Digital track
  446. if (track->track_mode == CUETrack_AUDIO)
  447. {
  448. control_adr = 0x10; // Audio track
  449. }
  450. dest[0] = 0; // Reserved
  451. dest[1] = control_adr;
  452. dest[2] = track->track_number;
  453. dest[3] = 0; // Reserved
  454. if (use_MSF_time)
  455. {
  456. // Time in minute-second-frame format
  457. dest[4] = 0;
  458. LBA2MSF(track->data_start, &dest[5], false);
  459. }
  460. else
  461. {
  462. // Time as logical block address
  463. dest[4] = (track->data_start >> 24) & 0xFF;
  464. dest[5] = (track->data_start >> 16) & 0xFF;
  465. dest[6] = (track->data_start >> 8) & 0xFF;
  466. dest[7] = (track->data_start >> 0) & 0xFF;
  467. }
  468. }
  469. // Load data from CUE sheet for the given device,
  470. // using the second half of scsiDev.data buffer for temporary storage.
  471. // Returns false if no cue sheet or it could not be opened.
  472. static bool loadCueSheet(image_config_t &img, CUEParser &parser)
  473. {
  474. if (!img.cuesheetfile.isOpen())
  475. {
  476. return false;
  477. }
  478. // Use second half of scsiDev.data as the buffer for cue sheet text
  479. size_t halfbufsize = sizeof(scsiDev.data) / 2;
  480. char *cuebuf = (char*)&scsiDev.data[halfbufsize];
  481. img.cuesheetfile.seek(0);
  482. int len = img.cuesheetfile.read(cuebuf, halfbufsize);
  483. if (len <= 0)
  484. {
  485. return false;
  486. }
  487. cuebuf[len] = '\0';
  488. parser = CUEParser(cuebuf);
  489. return true;
  490. }
  491. static void doReadTOC(bool MSF, uint8_t track, uint16_t allocationLength)
  492. {
  493. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  494. CUEParser parser;
  495. if (!loadCueSheet(img, parser))
  496. {
  497. // No CUE sheet, use hardcoded data
  498. return doReadTOCSimple(MSF, track, allocationLength);
  499. }
  500. // Format track info
  501. uint8_t *trackdata = &scsiDev.data[4];
  502. int trackcount = 0;
  503. int firsttrack = -1;
  504. CUETrackInfo lasttrack = {0};
  505. const CUETrackInfo *trackinfo;
  506. while ((trackinfo = parser.next_track()) != NULL)
  507. {
  508. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  509. lasttrack = *trackinfo;
  510. if (track <= trackinfo->track_number)
  511. {
  512. formatTrackInfo(trackinfo, &trackdata[8 * trackcount], MSF);
  513. trackcount += 1;
  514. }
  515. }
  516. // Format lead-out track info
  517. CUETrackInfo leadout = {};
  518. leadout.track_number = 0xAA;
  519. leadout.track_mode = (lasttrack.track_number != 0) ? lasttrack.track_mode : CUETrack_MODE1_2048;
  520. leadout.data_start = getLeadOutLBA(&lasttrack);
  521. formatTrackInfo(&leadout, &trackdata[8 * trackcount], MSF);
  522. trackcount += 1;
  523. // Format response header
  524. uint16_t toc_length = 2 + trackcount * 8;
  525. scsiDev.data[0] = toc_length >> 8;
  526. scsiDev.data[1] = toc_length & 0xFF;
  527. scsiDev.data[2] = firsttrack;
  528. scsiDev.data[3] = lasttrack.track_number;
  529. if (track != 0xAA && trackcount < 2)
  530. {
  531. // Unknown track requested
  532. scsiDev.status = CHECK_CONDITION;
  533. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  534. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  535. scsiDev.phase = STATUS;
  536. }
  537. else
  538. {
  539. uint32_t len = 2 + toc_length;
  540. if (len > allocationLength)
  541. {
  542. len = allocationLength;
  543. }
  544. scsiDev.dataLen = len;
  545. scsiDev.phase = DATA_IN;
  546. }
  547. }
  548. static void doReadSessionInfo(bool msf, uint16_t allocationLength)
  549. {
  550. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  551. CUEParser parser;
  552. if (!loadCueSheet(img, parser))
  553. {
  554. // No CUE sheet, use hardcoded data
  555. return doReadSessionInfoSimple(msf, allocationLength);
  556. }
  557. uint32_t len = sizeof(SessionTOC);
  558. memcpy(scsiDev.data, SessionTOC, len);
  559. // Replace first track info in the session table
  560. // based on data from CUE sheet.
  561. const CUETrackInfo *trackinfo = parser.next_track();
  562. if (trackinfo)
  563. {
  564. formatTrackInfo(trackinfo, &scsiDev.data[4], false);
  565. }
  566. if (len > allocationLength)
  567. {
  568. len = allocationLength;
  569. }
  570. scsiDev.dataLen = len;
  571. scsiDev.phase = DATA_IN;
  572. }
  573. // Format track info read from cue sheet into the format used by ReadFullTOC command.
  574. // Refer to T10/1545-D MMC-4 Revision 5a, "Response Format 0010b: Raw TOC"
  575. static void formatRawTrackInfo(const CUETrackInfo *track, uint8_t *dest, bool useBCD)
  576. {
  577. uint8_t control_adr = 0x14; // Digital track
  578. if (track->track_mode == CUETrack_AUDIO)
  579. {
  580. control_adr = 0x10; // Audio track
  581. }
  582. dest[0] = 0x01; // Session always 1
  583. dest[1] = control_adr;
  584. dest[2] = 0x00; // "TNO", always 0?
  585. dest[3] = track->track_number; // "POINT", contains track number
  586. // Next three are ATIME. The spec doesn't directly address how these
  587. // should be reported in the TOC, just giving a description of Q-channel
  588. // data from Red Book/ECMA-130. On all disks tested so far these are
  589. // given as 00/00/00.
  590. dest[4] = 0x00;
  591. dest[5] = 0x00;
  592. dest[6] = 0x00;
  593. dest[7] = 0; // HOUR
  594. if (useBCD) {
  595. LBA2MSFBCD(track->data_start, &dest[8], false);
  596. } else {
  597. LBA2MSF(track->data_start, &dest[8], false);
  598. }
  599. }
  600. static void doReadFullTOC(uint8_t session, uint16_t allocationLength, bool useBCD)
  601. {
  602. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  603. CUEParser parser;
  604. if (!loadCueSheet(img, parser))
  605. {
  606. // No CUE sheet, use hardcoded data
  607. return doReadFullTOCSimple(session, allocationLength, useBCD);
  608. }
  609. // We only support session 1.
  610. if (session > 1)
  611. {
  612. scsiDev.status = CHECK_CONDITION;
  613. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  614. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  615. scsiDev.phase = STATUS;
  616. return;
  617. }
  618. // Take the beginning of the hardcoded TOC as base
  619. uint32_t len = 4 + 11 * 3; // Header, A0, A1, A2
  620. memcpy(scsiDev.data, FullTOC, len);
  621. // Add track descriptors
  622. int trackcount = 0;
  623. int firsttrack = -1;
  624. CUETrackInfo lasttrack = {0};
  625. const CUETrackInfo *trackinfo;
  626. while ((trackinfo = parser.next_track()) != NULL)
  627. {
  628. if (firsttrack < 0)
  629. {
  630. firsttrack = trackinfo->track_number;
  631. if (trackinfo->track_mode == CUETrack_AUDIO)
  632. {
  633. scsiDev.data[5] = 0x10;
  634. }
  635. }
  636. lasttrack = *trackinfo;
  637. formatRawTrackInfo(trackinfo, &scsiDev.data[len], useBCD);
  638. trackcount += 1;
  639. len += 11;
  640. }
  641. // First and last track numbers
  642. scsiDev.data[12] = firsttrack;
  643. if (lasttrack.track_number != 0)
  644. {
  645. scsiDev.data[23] = lasttrack.track_number;
  646. if (lasttrack.track_mode == CUETrack_AUDIO)
  647. {
  648. scsiDev.data[16] = 0x10;
  649. scsiDev.data[27] = 0x10;
  650. }
  651. }
  652. // Leadout track position
  653. if (useBCD) {
  654. LBA2MSFBCD(getLeadOutLBA(&lasttrack), &scsiDev.data[34], false);
  655. } else {
  656. LBA2MSF(getLeadOutLBA(&lasttrack), &scsiDev.data[34], false);
  657. }
  658. // Correct the record length in header
  659. uint16_t toclen = len - 2;
  660. scsiDev.data[0] = toclen >> 8;
  661. scsiDev.data[1] = toclen & 0xFF;
  662. if (len > allocationLength)
  663. {
  664. len = allocationLength;
  665. }
  666. scsiDev.dataLen = len;
  667. scsiDev.phase = DATA_IN;
  668. }
  669. // SCSI-3 MMC Read Header command, seems to be deprecated in later standards.
  670. // Refer to ANSI X3.304-1997
  671. // The spec is vague, but based on experimentation with a Matshita drive this
  672. // command should return the sector header absolute time (see ECMA-130 21).
  673. // Given 2048-byte block sizes this effectively is 1:1 with the provided LBA.
  674. void doReadHeader(bool MSF, uint32_t lba, uint16_t allocationLength)
  675. {
  676. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  677. #if ENABLE_AUDIO_OUTPUT
  678. // terminate audio playback if active on this target (Annex C)
  679. audio_stop(img.scsiId & S2S_CFG_TARGET_ID_BITS);
  680. #endif
  681. uint8_t mode = 1;
  682. CUEParser parser;
  683. if (loadCueSheet(img, parser))
  684. {
  685. // Search the track with the requested LBA
  686. CUETrackInfo trackinfo = {};
  687. getTrackFromLBA(parser, lba, &trackinfo);
  688. // Track mode (audio / data)
  689. if (trackinfo.track_mode == CUETrack_AUDIO)
  690. {
  691. scsiDev.data[0] = 0;
  692. }
  693. }
  694. scsiDev.data[0] = mode;
  695. scsiDev.data[1] = 0; // reserved
  696. scsiDev.data[2] = 0; // reserved
  697. scsiDev.data[3] = 0; // reserved
  698. // Track start
  699. if (MSF)
  700. {
  701. scsiDev.data[4] = 0;
  702. LBA2MSF(lba, &scsiDev.data[5], false);
  703. }
  704. else
  705. {
  706. scsiDev.data[4] = (lba >> 24) & 0xFF;
  707. scsiDev.data[5] = (lba >> 16) & 0xFF;
  708. scsiDev.data[6] = (lba >> 8) & 0xFF;
  709. scsiDev.data[7] = (lba >> 0) & 0xFF;
  710. }
  711. uint8_t len = 8;
  712. if (len > allocationLength)
  713. {
  714. len = allocationLength;
  715. }
  716. scsiDev.dataLen = len;
  717. scsiDev.phase = DATA_IN;
  718. }
  719. void doReadDiscInformation(uint16_t allocationLength)
  720. {
  721. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  722. CUEParser parser;
  723. if (!loadCueSheet(img, parser))
  724. {
  725. // No CUE sheet, use hardcoded data
  726. return doReadDiscInformationSimple(allocationLength);
  727. }
  728. // Take the hardcoded header as base
  729. uint32_t len = sizeof(DiscInformation);
  730. memcpy(scsiDev.data, DiscInformation, len);
  731. // Find first and last track number
  732. int firsttrack = -1;
  733. int lasttrack = -1;
  734. const CUETrackInfo *trackinfo;
  735. while ((trackinfo = parser.next_track()) != NULL)
  736. {
  737. if (firsttrack < 0) firsttrack = trackinfo->track_number;
  738. lasttrack = trackinfo->track_number;
  739. }
  740. scsiDev.data[3] = firsttrack;
  741. scsiDev.data[5] = firsttrack;
  742. scsiDev.data[6] = lasttrack;
  743. if (len > allocationLength)
  744. {
  745. len = allocationLength;
  746. }
  747. scsiDev.dataLen = len;
  748. scsiDev.phase = DATA_IN;
  749. }
  750. void doReadTrackInformation(bool track, uint32_t lba, uint16_t allocationLength)
  751. {
  752. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  753. CUEParser parser;
  754. if (!loadCueSheet(img, parser))
  755. {
  756. // No CUE sheet, use hardcoded data
  757. return doReadTrackInformationSimple(track, lba, allocationLength);
  758. }
  759. // Take the hardcoded header as base
  760. uint32_t len = sizeof(TrackInformation);
  761. memcpy(scsiDev.data, TrackInformation, len);
  762. // Step through the tracks until the one requested is found
  763. // Result will be placed in mtrack for later use if found
  764. bool trackfound = false;
  765. uint32_t tracklen = 0;
  766. CUETrackInfo mtrack = {0};
  767. const CUETrackInfo *trackinfo;
  768. while ((trackinfo = parser.next_track()) != NULL)
  769. {
  770. if (mtrack.track_number != 0) // skip 1st track, just store later
  771. {
  772. if ((track && lba == mtrack.track_number)
  773. || (!track && lba < trackinfo->data_start))
  774. {
  775. trackfound = true;
  776. tracklen = trackinfo->data_start - mtrack.data_start;
  777. break;
  778. }
  779. }
  780. mtrack = *trackinfo;
  781. }
  782. // try the last track as a final attempt if no match found beforehand
  783. if (!trackfound)
  784. {
  785. uint32_t lastLba = getLeadOutLBA(&mtrack);
  786. if ((track && lba == mtrack.track_number)
  787. || (!track && lba < lastLba))
  788. {
  789. trackfound = true;
  790. tracklen = lastLba - mtrack.data_start;
  791. }
  792. }
  793. // bail out if no match found
  794. if (!trackfound)
  795. {
  796. scsiDev.status = CHECK_CONDITION;
  797. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  798. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  799. scsiDev.phase = STATUS;
  800. return;
  801. }
  802. // rewrite relevant bytes, starting with track number
  803. scsiDev.data[3] = mtrack.track_number;
  804. // track mode
  805. if (mtrack.track_mode == CUETrack_AUDIO)
  806. {
  807. scsiDev.data[5] = 0x00;
  808. }
  809. // track start
  810. uint32_t start = mtrack.data_start;
  811. scsiDev.data[8] = start >> 24;
  812. scsiDev.data[9] = start >> 16;
  813. scsiDev.data[10] = start >> 8;
  814. scsiDev.data[11] = start;
  815. // track size
  816. scsiDev.data[24] = tracklen >> 24;
  817. scsiDev.data[25] = tracklen >> 16;
  818. scsiDev.data[26] = tracklen >> 8;
  819. scsiDev.data[27] = tracklen;
  820. debuglog("------ Reporting track ", mtrack.track_number, ", start ", start,
  821. ", length ", tracklen);
  822. if (len > allocationLength)
  823. {
  824. len = allocationLength;
  825. }
  826. scsiDev.dataLen = len;
  827. scsiDev.phase = DATA_IN;
  828. }
  829. void doGetConfiguration(uint8_t rt, uint16_t startFeature, uint16_t allocationLength)
  830. {
  831. // rt = 0 is all features, rt = 1 is current features,
  832. // rt = 2 only startFeature, others reserved
  833. if (rt > 2)
  834. {
  835. scsiDev.status = CHECK_CONDITION;
  836. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  837. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  838. scsiDev.phase = STATUS;
  839. return;
  840. }
  841. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  842. // write feature header
  843. uint32_t len = 8; // length bytes set at end of call
  844. scsiDev.data[4] = 0; // reserved
  845. scsiDev.data[5] = 0; // reserved
  846. if (!img.ejected)
  847. {
  848. // disk in drive, current profile is CD-ROM
  849. scsiDev.data[6] = 0x00;
  850. scsiDev.data[7] = 0x08;
  851. }
  852. else
  853. {
  854. // no disk, report no current profile
  855. scsiDev.data[6] = 0;
  856. scsiDev.data[7] = 0;
  857. }
  858. // profile list (0)
  859. if ((rt == 2 && 0 == startFeature)
  860. || (rt == 1 && startFeature <= 0)
  861. || (rt == 0 && startFeature <= 0))
  862. {
  863. scsiDev.data[len++] = 0x00;
  864. scsiDev.data[len++] = 0x00;
  865. scsiDev.data[len++] = 0x03; // ver 0, persist=1,current=1
  866. scsiDev.data[len++] = 8; // 2 more
  867. // CD-ROM profile
  868. scsiDev.data[len++] = 0x00;
  869. scsiDev.data[len++] = 0x08;
  870. scsiDev.data[len++] = (img.ejected) ? 0x00 : 0x01;
  871. scsiDev.data[len++] = 0;
  872. // removable disk profile
  873. scsiDev.data[len++] = 0x00;
  874. scsiDev.data[len++] = 0x02;
  875. scsiDev.data[len++] = 0x00;
  876. scsiDev.data[len++] = 0;
  877. }
  878. // core feature (1)
  879. if ((rt == 2 && startFeature == 1)
  880. || (rt == 1 && startFeature <= 1)
  881. || (rt == 0 && startFeature <= 1))
  882. {
  883. scsiDev.data[len++] = 0x00;
  884. scsiDev.data[len++] = 0x01;
  885. scsiDev.data[len++] = 0x0B; // ver 2, persist=1,current=1
  886. scsiDev.data[len++] = 8;
  887. // physical interface standard (SCSI)
  888. scsiDev.data[len++] = 0x00;
  889. scsiDev.data[len++] = 0x00;
  890. scsiDev.data[len++] = 0x00;
  891. scsiDev.data[len++] = 0x01;
  892. scsiDev.data[len++] = 0x03; // support INQ2 and DBE
  893. scsiDev.data[len++] = 0;
  894. scsiDev.data[len++] = 0;
  895. scsiDev.data[len++] = 0;
  896. }
  897. // morphing feature (2)
  898. if ((rt == 2 && startFeature == 2)
  899. || (rt == 1 && startFeature <= 2)
  900. || (rt == 0 && startFeature <= 2))
  901. {
  902. scsiDev.data[len++] = 0x00;
  903. scsiDev.data[len++] = 0x02;
  904. scsiDev.data[len++] = 0x07; // ver 1, persist=1,current=1
  905. scsiDev.data[len++] = 4;
  906. scsiDev.data[len++] = 0x02; // OCEvent=1,async=0
  907. scsiDev.data[len++] = 0;
  908. scsiDev.data[len++] = 0;
  909. scsiDev.data[len++] = 0;
  910. }
  911. // removable medium feature (3)
  912. if ((rt == 2 && startFeature == 3)
  913. || (rt == 1 && startFeature <= 3)
  914. || (rt == 0 && startFeature <= 3))
  915. {
  916. scsiDev.data[len++] = 0x00;
  917. scsiDev.data[len++] = 0x03;
  918. scsiDev.data[len++] = 0x03; // ver 0, persist=1,current=1
  919. scsiDev.data[len++] = 4;
  920. scsiDev.data[len++] = 0x28; // matches 0x2A mode page version
  921. scsiDev.data[len++] = 0;
  922. scsiDev.data[len++] = 0;
  923. scsiDev.data[len++] = 0;
  924. }
  925. // random readable feature (0x10, 16)
  926. if ((rt == 2 && startFeature == 16)
  927. || (rt == 1 && startFeature <= 16 && !img.ejected)
  928. || (rt == 0 && startFeature <= 16))
  929. {
  930. scsiDev.data[len++] = 0x00;
  931. scsiDev.data[len++] = 0x10;
  932. // ver 0, persist=0,current=drive state
  933. scsiDev.data[len++] = (img.ejected) ? 0x00 : 0x01;
  934. scsiDev.data[len++] = 8;
  935. scsiDev.data[len++] = 0x00; // 2048 (MSB)
  936. scsiDev.data[len++] = 0x00; // .
  937. scsiDev.data[len++] = 0x08; // .
  938. scsiDev.data[len++] = 0x00; // 2048 (LSB)
  939. scsiDev.data[len++] = 0x00;
  940. // one block min when disk in drive only
  941. scsiDev.data[len++] = (img.ejected) ? 0x00 : 0x01;
  942. scsiDev.data[len++] = 0x00; // no support for PP error correction (TODO?)
  943. scsiDev.data[len++] = 0;
  944. }
  945. // multi-read feature (0x1D, 29)
  946. if ((rt == 2 && startFeature == 29)
  947. || (rt == 1 && startFeature <= 29 && !img.ejected)
  948. || (rt == 0 && startFeature <= 29))
  949. {
  950. scsiDev.data[len++] = 0x00;
  951. scsiDev.data[len++] = 0x1D;
  952. // ver 0, persist=0,current=drive state
  953. scsiDev.data[len++] = (img.ejected) ? 0x00 : 0x01;
  954. scsiDev.data[len++] = 0;
  955. }
  956. // CD read feature (0x1E, 30)
  957. if ((rt == 2 && startFeature == 30)
  958. || (rt == 1 && startFeature <= 30 && !img.ejected)
  959. || (rt == 0 && startFeature <= 30))
  960. {
  961. scsiDev.data[len++] = 0x00;
  962. scsiDev.data[len++] = 0x1E;
  963. // ver 2, persist=0,current=drive state
  964. scsiDev.data[len++] = (img.ejected) ? 0x08 : 0x09;
  965. scsiDev.data[len++] = 4;
  966. scsiDev.data[len++] = 0x00; // dap=0,c2=0,cd-text=0
  967. scsiDev.data[len++] = 0;
  968. scsiDev.data[len++] = 0;
  969. scsiDev.data[len++] = 0;
  970. }
  971. #ifdef ENABLE_AUDIO_OUTPUT
  972. // CD audio feature (0x103, 259)
  973. if ((rt == 2 && startFeature == 259)
  974. || (rt == 1 && startFeature <= 259 && !img.ejected)
  975. || (rt == 0 && startFeature <= 259))
  976. {
  977. scsiDev.data[len++] = 0x01;
  978. scsiDev.data[len++] = 0x03;
  979. // ver 1, persist=0,current=drive state
  980. scsiDev.data[len++] = (img.ejected) ? 0x04 : 0x05;
  981. scsiDev.data[len++] = 4;
  982. scsiDev.data[len++] = 0x03; // scan=0,scm=1,sv=1
  983. scsiDev.data[len++] = 0;
  984. scsiDev.data[len++] = 0x01; // 256 volume levels
  985. scsiDev.data[len++] = 0x00; // .
  986. }
  987. #endif
  988. // finally, rewrite data length to match
  989. uint32_t dlen = len - 8;
  990. scsiDev.data[0] = dlen >> 24;
  991. scsiDev.data[1] = dlen >> 16;
  992. scsiDev.data[2] = dlen >> 8;
  993. scsiDev.data[3] = dlen;
  994. if (len > allocationLength)
  995. {
  996. len = allocationLength;
  997. }
  998. scsiDev.dataLen = len;
  999. scsiDev.phase = DATA_IN;
  1000. }
  1001. /****************************************/
  1002. /* CUE sheet check at image load time */
  1003. /****************************************/
  1004. bool cdromValidateCueSheet(image_config_t &img)
  1005. {
  1006. CUEParser parser;
  1007. if (!loadCueSheet(img, parser))
  1008. {
  1009. return false;
  1010. }
  1011. const CUETrackInfo *trackinfo;
  1012. int trackcount = 0;
  1013. while ((trackinfo = parser.next_track()) != NULL)
  1014. {
  1015. trackcount++;
  1016. if (trackinfo->track_mode != CUETrack_AUDIO &&
  1017. trackinfo->track_mode != CUETrack_MODE1_2048 &&
  1018. trackinfo->track_mode != CUETrack_MODE1_2352)
  1019. {
  1020. log("---- Warning: track ", trackinfo->track_number, " has unsupported mode ", (int)trackinfo->track_mode);
  1021. }
  1022. if (trackinfo->file_mode != CUEFile_BINARY)
  1023. {
  1024. log("---- Unsupported CUE data file mode ", (int)trackinfo->file_mode);
  1025. }
  1026. }
  1027. if (trackcount == 0)
  1028. {
  1029. log("---- Opened cue sheet but no valid tracks found");
  1030. return false;
  1031. }
  1032. log("---- Cue sheet loaded with ", (int)trackcount, " tracks");
  1033. return true;
  1034. }
  1035. /**************************************/
  1036. /* Ejection and image switching logic */
  1037. /**************************************/
  1038. // Close CD-ROM tray and note media change event
  1039. void cdromCloseTray(image_config_t &img)
  1040. {
  1041. if (img.ejected)
  1042. {
  1043. uint8_t target = img.scsiId & S2S_CFG_TARGET_ID_BITS;
  1044. debuglog("------ CDROM close tray on ID ", (int)target);
  1045. img.ejected = false;
  1046. img.cdrom_events = 2; // New media
  1047. if (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_UNIT_ATTENTION)
  1048. {
  1049. debuglog("------ Posting UNIT ATTENTION after medium change");
  1050. scsiDev.targets[target].unitAttention = NOT_READY_TO_READY_TRANSITION_MEDIUM_MAY_HAVE_CHANGED;
  1051. }
  1052. }
  1053. }
  1054. // Eject CD-ROM tray if closed, close if open
  1055. // Switch image on ejection.
  1056. void cdromPerformEject(image_config_t &img)
  1057. {
  1058. uint8_t target = img.scsiId & S2S_CFG_TARGET_ID_BITS;
  1059. #if ENABLE_AUDIO_OUTPUT
  1060. // terminate audio playback if active on this target (MMC-1 Annex C)
  1061. audio_stop(target);
  1062. #endif
  1063. if (!img.ejected)
  1064. {
  1065. debuglog("------ CDROM open tray on ID ", (int)target);
  1066. img.ejected = true;
  1067. img.cdrom_events = 3; // Media removal
  1068. switchNextImage(img); // Switch media for next time
  1069. }
  1070. else
  1071. {
  1072. cdromCloseTray(img);
  1073. }
  1074. }
  1075. // Reinsert any ejected CD-ROMs on reboot
  1076. void cdromReinsertFirstImage(image_config_t &img)
  1077. {
  1078. if (img.image_index > 0)
  1079. {
  1080. // Multiple images for this drive, force restart from first one
  1081. uint8_t target = img.scsiId & S2S_CFG_TARGET_ID_BITS;
  1082. debuglog("---- Restarting from first CD-ROM image for ID ", (int)target);
  1083. img.image_index = -1;
  1084. img.current_image[0] = '\0';
  1085. switchNextImage(img);
  1086. }
  1087. else if (img.ejected)
  1088. {
  1089. // Reinsert the single image
  1090. cdromCloseTray(img);
  1091. }
  1092. }
  1093. static void doGetEventStatusNotification(bool immed)
  1094. {
  1095. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1096. if (!immed)
  1097. {
  1098. // Asynchronous notification not supported
  1099. scsiDev.status = CHECK_CONDITION;
  1100. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1101. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1102. scsiDev.phase = STATUS;
  1103. }
  1104. else if (img.cdrom_events)
  1105. {
  1106. scsiDev.data[0] = 0;
  1107. scsiDev.data[1] = 6; // EventDataLength
  1108. scsiDev.data[2] = 0x04; // Media status events
  1109. scsiDev.data[3] = 0x04; // Supported events
  1110. scsiDev.data[4] = img.cdrom_events;
  1111. scsiDev.data[5] = 0x01; // Power status
  1112. scsiDev.data[6] = 0; // Start slot
  1113. scsiDev.data[7] = 0; // End slot
  1114. scsiDev.dataLen = 8;
  1115. scsiDev.phase = DATA_IN;
  1116. img.cdrom_events = 0;
  1117. if (img.ejected && img.reinsert_after_eject)
  1118. {
  1119. // We are now reporting to host that the drive is open.
  1120. // Simulate a "close" for next time the host polls.
  1121. cdromCloseTray(img);
  1122. }
  1123. }
  1124. else
  1125. {
  1126. scsiDev.data[0] = 0;
  1127. scsiDev.data[1] = 2; // EventDataLength
  1128. scsiDev.data[2] = 0x00; // Media status events
  1129. scsiDev.data[3] = 0x04; // Supported events
  1130. scsiDev.dataLen = 4;
  1131. scsiDev.phase = DATA_IN;
  1132. }
  1133. }
  1134. /**************************************/
  1135. /* CD-ROM audio playback */
  1136. /**************************************/
  1137. void cdromGetAudioPlaybackStatus(uint8_t *status, uint32_t *current_lba, bool current_only)
  1138. {
  1139. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1140. #ifdef ENABLE_AUDIO_OUTPUT
  1141. if (status) {
  1142. uint8_t target = img.scsiId & S2S_CFG_TARGET_ID_BITS;
  1143. if (current_only) {
  1144. *status = audio_is_playing(target) ? 1 : 0;
  1145. } else {
  1146. *status = (uint8_t) audio_get_status_code(target);
  1147. }
  1148. }
  1149. #else
  1150. if (status) *status = 0; // audio status code for 'unsupported/invalid' and not-playing indicator
  1151. #endif
  1152. if (current_lba)
  1153. {
  1154. if (img.file.isOpen()) {
  1155. *current_lba = img.file.position() / 2352;
  1156. } else {
  1157. *current_lba = 0;
  1158. }
  1159. }
  1160. }
  1161. static void doPlayAudio(uint32_t lba, uint32_t length)
  1162. {
  1163. #ifdef ENABLE_AUDIO_OUTPUT
  1164. debuglog("------ CD-ROM Play Audio request at ", lba, " for ", length, " sectors");
  1165. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1166. uint8_t target_id = img.scsiId & S2S_CFG_TARGET_ID_BITS;
  1167. // Per Annex C terminate playback immediately if already in progress on
  1168. // the current target. Non-current targets may also get their audio
  1169. // interrupted later due to hardware limitations
  1170. audio_stop(target_id);
  1171. // if transfer length is zero no audio playback happens.
  1172. // don't treat as an error per SCSI-2; handle via short-circuit
  1173. if (length == 0)
  1174. {
  1175. scsiDev.status = 0;
  1176. scsiDev.phase = STATUS;
  1177. return;
  1178. }
  1179. // if actual playback is requested perform steps to verify prior to playback
  1180. CUEParser parser;
  1181. if (loadCueSheet(img, parser))
  1182. {
  1183. CUETrackInfo trackinfo = {};
  1184. getTrackFromLBA(parser, lba, &trackinfo);
  1185. if (lba == 0xFFFFFFFF)
  1186. {
  1187. // request to start playback from 'current position'
  1188. lba = img.file.position() / 2352;
  1189. }
  1190. uint64_t offset = trackinfo.file_offset
  1191. + trackinfo.sector_length * (lba - trackinfo.track_start);
  1192. debuglog("------ Play audio CD: ", (int)length, " sectors starting at ", (int)lba,
  1193. ", track number ", trackinfo.track_number, ", data offset in file ", (int)offset);
  1194. if (trackinfo.track_mode != CUETrack_AUDIO)
  1195. {
  1196. debuglog("---- Host tried audio playback on track type ", (int)trackinfo.track_mode);
  1197. scsiDev.status = CHECK_CONDITION;
  1198. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1199. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1200. scsiDev.phase = STATUS;
  1201. return;
  1202. }
  1203. // playback request appears to be sane, so perform it
  1204. // see earlier note for context on the block length below
  1205. if (!audio_play(target_id, &(img.file), offset,
  1206. offset + length * trackinfo.sector_length, false))
  1207. {
  1208. // Underlying data/media error? Fake a disk scratch, which should
  1209. // be a condition most CD-DA players are expecting
  1210. scsiDev.status = CHECK_CONDITION;
  1211. scsiDev.target->sense.code = MEDIUM_ERROR;
  1212. scsiDev.target->sense.asc = 0x1106; // CIRC UNRECOVERED ERROR
  1213. scsiDev.phase = STATUS;
  1214. return;
  1215. }
  1216. scsiDev.status = 0;
  1217. scsiDev.phase = STATUS;
  1218. }
  1219. else
  1220. {
  1221. // virtual drive supports audio, just not with this disk image
  1222. debuglog("---- Request to play audio on non-audio image");
  1223. scsiDev.status = CHECK_CONDITION;
  1224. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1225. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1226. scsiDev.phase = STATUS;
  1227. }
  1228. #else
  1229. debuglog("---- Target does not support audio playback");
  1230. // per SCSI-2, targets not supporting audio respond to zero-length
  1231. // PLAY AUDIO commands with ILLEGAL REQUEST; this seems to be a check
  1232. // performed by at least some audio playback software
  1233. scsiDev.status = CHECK_CONDITION;
  1234. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1235. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  1236. scsiDev.phase = STATUS;
  1237. #endif
  1238. }
  1239. static void doPauseResumeAudio(bool resume)
  1240. {
  1241. #ifdef ENABLE_AUDIO_OUTPUT
  1242. log("------ CD-ROM ", resume ? "resume" : "pause", " audio playback");
  1243. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1244. uint8_t target_id = img.scsiId & S2S_CFG_TARGET_ID_BITS;
  1245. if (audio_is_playing(target_id))
  1246. {
  1247. audio_set_paused(target_id, !resume);
  1248. scsiDev.status = 0;
  1249. scsiDev.phase = STATUS;
  1250. }
  1251. else
  1252. {
  1253. scsiDev.status = CHECK_CONDITION;
  1254. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1255. scsiDev.target->sense.asc = 0x2C00; // COMMAND SEQUENCE ERROR
  1256. scsiDev.phase = STATUS;
  1257. }
  1258. #else
  1259. debuglog("---- Target does not support audio pausing");
  1260. scsiDev.status = CHECK_CONDITION;
  1261. scsiDev.target->sense.code = ILLEGAL_REQUEST; // assumed from PLAY AUDIO(10)
  1262. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  1263. scsiDev.phase = STATUS;
  1264. #endif
  1265. }
  1266. static void doStopAudio()
  1267. {
  1268. debuglog("------ CD-ROM Stop Audio request");
  1269. #ifdef ENABLE_AUDIO_OUTPUT
  1270. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1271. uint8_t target_id = img.scsiId & S2S_CFG_TARGET_ID_BITS;
  1272. audio_stop(target_id);
  1273. #endif
  1274. }
  1275. static void doMechanismStatus(uint16_t allocation_length)
  1276. {
  1277. uint8_t *buf = scsiDev.data;
  1278. uint8_t status;
  1279. uint32_t lba;
  1280. cdromGetAudioPlaybackStatus(&status, &lba, true);
  1281. *buf++ = 0x00; // No fault state
  1282. *buf++ = (status) ? 0x20 : 0x00; // Currently playing?
  1283. *buf++ = (lba >> 16) & 0xFF;
  1284. *buf++ = (lba >> 8) & 0xFF;
  1285. *buf++ = (lba >> 0) & 0xFF;
  1286. *buf++ = 0; // No CD changer
  1287. *buf++ = 0;
  1288. *buf++ = 0;
  1289. int len = 8;
  1290. if (len > allocation_length) len = allocation_length;
  1291. scsiDev.dataLen = len;
  1292. scsiDev.phase = DATA_IN;
  1293. }
  1294. /*******************************************/
  1295. /* CD-ROM data reading in low level format */
  1296. /*******************************************/
  1297. static void doReadCD(uint32_t lba, uint32_t length, uint8_t sector_type,
  1298. uint8_t main_channel, uint8_t sub_channel, bool data_only)
  1299. {
  1300. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1301. #if ENABLE_AUDIO_OUTPUT
  1302. // terminate audio playback if active on this target (Annex C)
  1303. audio_stop(img.scsiId & S2S_CFG_TARGET_ID_BITS);
  1304. #endif
  1305. CUEParser parser;
  1306. if (!loadCueSheet(img, parser)
  1307. && (sector_type == 0 || sector_type == 2)
  1308. && main_channel == 0x10 && sub_channel == 0)
  1309. {
  1310. // Simple case, return sector data directly
  1311. scsiDiskStartRead(lba, length);
  1312. return;
  1313. }
  1314. // Search the track with the requested LBA
  1315. // Supplies dummy data if no cue sheet is active.
  1316. CUETrackInfo trackinfo = {};
  1317. getTrackFromLBA(parser, lba, &trackinfo);
  1318. // Figure out the data offset in the file
  1319. uint64_t offset = trackinfo.file_offset + trackinfo.sector_length * (lba - trackinfo.track_start);
  1320. debuglog("------ Read CD: ", (int)length, " sectors starting at ", (int)lba,
  1321. ", track number ", trackinfo.track_number, ", sector size ", (int)trackinfo.sector_length,
  1322. ", main channel ", main_channel, ", sub channel ", sub_channel,
  1323. ", data offset in file ", (int)offset);
  1324. // Ensure read is not out of range of the image
  1325. uint64_t readend = offset + trackinfo.sector_length * length;
  1326. if (readend > img.file.size())
  1327. {
  1328. log("WARNING: Host attempted CD read at sector ", lba, "+", length,
  1329. ", exceeding image size ", img.file.size());
  1330. scsiDev.status = CHECK_CONDITION;
  1331. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1332. scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1333. scsiDev.phase = STATUS;
  1334. return;
  1335. }
  1336. // Verify sector type
  1337. if (sector_type != 0)
  1338. {
  1339. bool sector_type_ok = false;
  1340. if (sector_type == 1 && trackinfo.track_mode == CUETrack_AUDIO)
  1341. {
  1342. sector_type_ok = true;
  1343. }
  1344. else if (sector_type == 2 && trackinfo.track_mode == CUETrack_MODE1_2048)
  1345. {
  1346. sector_type_ok = true;
  1347. }
  1348. if (!sector_type_ok)
  1349. {
  1350. debuglog("---- Failed sector type check, host requested ", (int)sector_type, " CUE file has ", (int)trackinfo.track_mode);
  1351. scsiDev.status = CHECK_CONDITION;
  1352. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1353. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1354. scsiDev.phase = STATUS;
  1355. return;
  1356. }
  1357. }
  1358. // Select fields to transfer
  1359. // Refer to table 351 in T10/1545-D MMC-4 Revision 5a
  1360. // Only the mandatory cases are supported.
  1361. int sector_length = 0;
  1362. int skip_begin = 0;
  1363. bool add_fake_headers = false;
  1364. if (main_channel == 0)
  1365. {
  1366. // No actual data requested, just sector type check or subchannel
  1367. sector_length = 0;
  1368. }
  1369. else if (trackinfo.track_mode == CUETrack_AUDIO)
  1370. {
  1371. // Transfer whole 2352 byte audio sectors from file to host
  1372. sector_length = 2352;
  1373. }
  1374. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && main_channel == 0x10)
  1375. {
  1376. // Transfer whole 2048 byte data sectors from file to host
  1377. sector_length = 2048;
  1378. }
  1379. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && (main_channel & 0xB8) == 0xB8)
  1380. {
  1381. // Transfer 2048 bytes of data from file and fake the headers
  1382. sector_length = 2048;
  1383. add_fake_headers = true;
  1384. debuglog("------ Host requested ECC data but image file lacks it, replacing with zeros");
  1385. }
  1386. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && main_channel == 0x10)
  1387. {
  1388. // Transfer the 2048 byte payload of data sector to host.
  1389. sector_length = 2048;
  1390. skip_begin = 16;
  1391. }
  1392. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && (main_channel & 0xB8) == 0xB8)
  1393. {
  1394. // Transfer whole 2352 byte data sector with ECC to host
  1395. sector_length = 2352;
  1396. }
  1397. else
  1398. {
  1399. debuglog("---- Unsupported channel request for track type ", (int)trackinfo.track_mode);
  1400. scsiDev.status = CHECK_CONDITION;
  1401. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1402. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1403. scsiDev.phase = STATUS;
  1404. return;
  1405. }
  1406. if (data_only && sector_length != 2048)
  1407. {
  1408. debuglog("------ Host tried to read non-data sector with standard READ command");
  1409. scsiDev.status = CHECK_CONDITION;
  1410. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1411. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1412. scsiDev.phase = STATUS;
  1413. return;
  1414. }
  1415. bool field_q_subchannel = false;
  1416. if (sub_channel == 2)
  1417. {
  1418. // Include position information in Q subchannel
  1419. field_q_subchannel = true;
  1420. }
  1421. else if (sub_channel != 0)
  1422. {
  1423. debuglog("---- Unsupported subchannel request");
  1424. scsiDev.status = CHECK_CONDITION;
  1425. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1426. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1427. scsiDev.phase = STATUS;
  1428. return;
  1429. }
  1430. scsiDev.phase = DATA_IN;
  1431. scsiDev.dataLen = 0;
  1432. scsiDev.dataPtr = 0;
  1433. scsiEnterPhase(DATA_IN);
  1434. // Use two buffers alternately for formatting sector data
  1435. uint32_t result_length = sector_length + (field_q_subchannel ? 16 : 0) + (add_fake_headers ? 304 : 0);
  1436. uint8_t *buf0 = scsiDev.data;
  1437. uint8_t *buf1 = scsiDev.data + result_length;
  1438. // Format the sectors for transfer
  1439. for (uint32_t idx = 0; idx < length; idx++)
  1440. {
  1441. platform_poll();
  1442. diskEjectButtonUpdate(false);
  1443. img.file.seek(offset + idx * trackinfo.sector_length + skip_begin);
  1444. // Verify that previous write using this buffer has finished
  1445. uint8_t *buf = ((idx & 1) ? buf1 : buf0);
  1446. uint8_t *bufstart = buf;
  1447. uint32_t start = millis();
  1448. while (!scsiIsWriteFinished(buf + result_length - 1) && !scsiDev.resetFlag)
  1449. {
  1450. if ((uint32_t)(millis() - start) > 5000)
  1451. {
  1452. log("doReadCD() timeout waiting for previous to finish");
  1453. scsiDev.resetFlag = 1;
  1454. }
  1455. platform_poll();
  1456. diskEjectButtonUpdate(false);
  1457. }
  1458. if (scsiDev.resetFlag) break;
  1459. if (add_fake_headers)
  1460. {
  1461. // 12-byte data sector sync pattern
  1462. *buf++ = 0x00;
  1463. for (int i = 0; i < 10; i++)
  1464. {
  1465. *buf++ = 0xFF;
  1466. }
  1467. *buf++ = 0x00;
  1468. // 4-byte data sector header
  1469. LBA2MSFBCD(lba + idx, buf, false);
  1470. buf += 3;
  1471. *buf++ = 0x01; // Mode 1
  1472. }
  1473. if (sector_length > 0)
  1474. {
  1475. // User data
  1476. img.file.read(buf, sector_length);
  1477. buf += sector_length;
  1478. }
  1479. if (add_fake_headers)
  1480. {
  1481. // 288 bytes of ECC
  1482. memset(buf, 0, 288);
  1483. buf += 288;
  1484. }
  1485. if (field_q_subchannel)
  1486. {
  1487. // Formatted Q subchannel data
  1488. // Refer to table 354 in T10/1545-D MMC-4 Revision 5a
  1489. // and ECMA-130 22.3.3
  1490. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14); // Control & ADR
  1491. *buf++ = trackinfo.track_number;
  1492. *buf++ = (lba + idx >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1493. int32_t rel = (int32_t)(lba + idx) - (int32_t)trackinfo.data_start;
  1494. LBA2MSF(rel, buf, true); buf += 3;
  1495. *buf++ = 0;
  1496. LBA2MSF(lba + idx, buf, false); buf += 3;
  1497. *buf++ = 0; *buf++ = 0; // CRC (optional)
  1498. *buf++ = 0; *buf++ = 0; *buf++ = 0; // (pad)
  1499. *buf++ = 0; // No P subchannel
  1500. }
  1501. assert(buf == bufstart + result_length);
  1502. scsiStartWrite(bufstart, result_length);
  1503. }
  1504. scsiFinishWrite();
  1505. scsiDev.status = 0;
  1506. scsiDev.phase = STATUS;
  1507. }
  1508. static void doReadSubchannel(bool time, bool subq, uint8_t parameter, uint8_t track_number, uint16_t allocation_length)
  1509. {
  1510. uint8_t *buf = scsiDev.data;
  1511. if (parameter == 0x01)
  1512. {
  1513. uint8_t audiostatus;
  1514. uint32_t lba;
  1515. cdromGetAudioPlaybackStatus(&audiostatus, &lba, false);
  1516. debuglog("------ Get audio playback position: status ", (int)audiostatus, " lba ", (int)lba);
  1517. // Fetch current track info
  1518. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1519. CUEParser parser;
  1520. CUETrackInfo trackinfo = {};
  1521. loadCueSheet(img, parser);
  1522. getTrackFromLBA(parser, lba, &trackinfo);
  1523. // Request sub channel data at current playback position
  1524. *buf++ = 0; // Reserved
  1525. *buf++ = audiostatus;
  1526. int len;
  1527. if (subq)
  1528. {
  1529. len = 12;
  1530. *buf++ = 0; // Subchannel data length (MSB)
  1531. *buf++ = len; // Subchannel data length (LSB)
  1532. *buf++ = 0x01; // Subchannel data format
  1533. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14);
  1534. *buf++ = trackinfo.track_number;
  1535. *buf++ = (lba >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1536. if (time)
  1537. {
  1538. *buf++ = 0;
  1539. LBA2MSF(lba, buf, false);
  1540. debuglog("------ ABS M ", *buf, " S ", *(buf+1), " F ", *(buf+2));
  1541. buf += 3;
  1542. }
  1543. else
  1544. {
  1545. *buf++ = (lba >> 24) & 0xFF; // Absolute block address
  1546. *buf++ = (lba >> 16) & 0xFF;
  1547. *buf++ = (lba >> 8) & 0xFF;
  1548. *buf++ = (lba >> 0) & 0xFF;
  1549. }
  1550. int32_t relpos = (int32_t)lba - (int32_t)trackinfo.data_start;
  1551. if (time)
  1552. {
  1553. *buf++ = 0;
  1554. LBA2MSF(relpos, buf, true);
  1555. debuglog("------ REL M ", *buf, " S ", *(buf+1), " F ", *(buf+2));
  1556. buf += 3;
  1557. }
  1558. else
  1559. {
  1560. uint32_t urelpos = relpos;
  1561. *buf++ = (urelpos >> 24) & 0xFF; // Track relative position (may be negative)
  1562. *buf++ = (urelpos >> 16) & 0xFF;
  1563. *buf++ = (urelpos >> 8) & 0xFF;
  1564. *buf++ = (urelpos >> 0) & 0xFF;
  1565. }
  1566. }
  1567. else
  1568. {
  1569. len = 0;
  1570. *buf++ = 0;
  1571. *buf++ = 0;
  1572. }
  1573. len += 4;
  1574. if (len > allocation_length) len = allocation_length;
  1575. scsiDev.dataLen = len;
  1576. scsiDev.phase = DATA_IN;
  1577. }
  1578. else
  1579. {
  1580. debuglog("---- Unsupported subchannel request");
  1581. scsiDev.status = CHECK_CONDITION;
  1582. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1583. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1584. scsiDev.phase = STATUS;
  1585. return;
  1586. }
  1587. }
  1588. static bool doReadCapacity(uint32_t lba, uint8_t pmi)
  1589. {
  1590. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1591. CUEParser parser;
  1592. if (!loadCueSheet(img, parser))
  1593. {
  1594. // basic image, let the disk handler resolve
  1595. return false;
  1596. }
  1597. // find the last track on the disk
  1598. CUETrackInfo lasttrack = {0};
  1599. const CUETrackInfo *trackinfo;
  1600. while ((trackinfo = parser.next_track()) != NULL)
  1601. {
  1602. lasttrack = *trackinfo;
  1603. }
  1604. uint32_t capacity = 0;
  1605. if (lasttrack.track_number != 0)
  1606. {
  1607. capacity = getLeadOutLBA(&lasttrack);
  1608. capacity--; // shift to last addressable LBA
  1609. if (pmi && lba && lba > capacity)
  1610. {
  1611. // MMC technically specifies that PMI should be zero, but SCSI-2 allows this
  1612. // potentially consider treating either out-of-bounds or PMI set as an error
  1613. // for now just ignore this
  1614. }
  1615. debuglog("----- Reporting capacity as ", capacity);
  1616. }
  1617. else
  1618. {
  1619. log("WARNING: unable to find capacity, no cue file found for ID ", img.scsiId);
  1620. }
  1621. scsiDev.data[0] = capacity >> 24;
  1622. scsiDev.data[1] = capacity >> 16;
  1623. scsiDev.data[2] = capacity >> 8;
  1624. scsiDev.data[3] = capacity;
  1625. scsiDev.data[4] = 0;
  1626. scsiDev.data[5] = 0;
  1627. scsiDev.data[6] = 0x08; // rest of code assumes 2048 here
  1628. scsiDev.data[7] = 0x00;
  1629. scsiDev.dataLen = 8;
  1630. scsiDev.phase = DATA_IN;
  1631. return true;
  1632. }
  1633. /**************************************/
  1634. /* CD-ROM command dispatching */
  1635. /**************************************/
  1636. // Handle direct-access scsi device commands
  1637. extern "C" int scsiCDRomCommand()
  1638. {
  1639. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1640. int commandHandled = 1;
  1641. uint8_t command = scsiDev.cdb[0];
  1642. if (command == 0x1B)
  1643. {
  1644. #if ENABLE_AUDIO_OUTPUT
  1645. // terminate audio playback if active on this target (MMC-1 Annex C)
  1646. audio_stop(img.scsiId & S2S_CFG_TARGET_ID_BITS);
  1647. #endif
  1648. if ((scsiDev.cdb[4] & 2))
  1649. {
  1650. // CD-ROM load & eject
  1651. int start = scsiDev.cdb[4] & 1;
  1652. if (start)
  1653. {
  1654. cdromCloseTray(img);
  1655. }
  1656. else
  1657. {
  1658. // Eject and switch image
  1659. cdromPerformEject(img);
  1660. }
  1661. }
  1662. }
  1663. else if (command == 0x25)
  1664. {
  1665. // READ CAPACITY
  1666. uint8_t reladdr = scsiDev.cdb[1] & 1;
  1667. uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
  1668. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1669. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1670. scsiDev.cdb[5];
  1671. uint8_t pmi = scsiDev.cdb[8] & 1;
  1672. // allow PMI as long as LBA is specified, this is permitted in SCSI-2
  1673. // we don't link commands, do not allow RELADDR
  1674. if ((!pmi && lba != 0) || reladdr)
  1675. {
  1676. scsiDev.status = CHECK_CONDITION;
  1677. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1678. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1679. scsiDev.phase = STATUS;
  1680. }
  1681. else
  1682. {
  1683. if (!doReadCapacity(lba, pmi))
  1684. {
  1685. // allow disk handler to resolve this one
  1686. commandHandled = 0;
  1687. }
  1688. }
  1689. }
  1690. else if (command == 0x43)
  1691. {
  1692. // CD-ROM Read TOC
  1693. bool MSF = (scsiDev.cdb[1] & 0x02);
  1694. uint8_t track = scsiDev.cdb[6];
  1695. uint16_t allocationLength =
  1696. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1697. scsiDev.cdb[8];
  1698. // The "format" field is reserved for SCSI-2
  1699. uint8_t format = scsiDev.cdb[2] & 0x0F;
  1700. // Matshita SCSI-2 drives appear to use the high 2 bits of the CDB
  1701. // control byte to switch on session info (0x40) and full toc (0x80)
  1702. // responses that are very similar to the standard formats described
  1703. // in MMC-1. These vendor flags must have been pretty common because
  1704. // even a modern SATA drive (ASUS DRW-24B1ST j) responds to them
  1705. // (though it always replies in hex rather than bcd)
  1706. //
  1707. // The session information page is identical to MMC. The full TOC page
  1708. // is identical _except_ it returns addresses in bcd rather than hex.
  1709. bool useBCD = false;
  1710. if (format == 0 && scsiDev.cdb[9] == 0x80)
  1711. {
  1712. format = 2;
  1713. useBCD = true;
  1714. }
  1715. else if (format == 0 && scsiDev.cdb[9] == 0x40)
  1716. {
  1717. format = 1;
  1718. }
  1719. switch (format)
  1720. {
  1721. case 0: doReadTOC(MSF, track, allocationLength); break; // SCSI-2
  1722. case 1: doReadSessionInfo(MSF, allocationLength); break; // MMC2
  1723. case 2: doReadFullTOC(track, allocationLength, useBCD); break; // MMC2
  1724. default:
  1725. {
  1726. scsiDev.status = CHECK_CONDITION;
  1727. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1728. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1729. scsiDev.phase = STATUS;
  1730. }
  1731. }
  1732. }
  1733. else if (command == 0x44)
  1734. {
  1735. // CD-ROM Read Header
  1736. bool MSF = (scsiDev.cdb[1] & 0x02);
  1737. uint32_t lba = 0; // IGNORED for now
  1738. uint16_t allocationLength =
  1739. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1740. scsiDev.cdb[8];
  1741. doReadHeader(MSF, lba, allocationLength);
  1742. }
  1743. else if (command == 0x46)
  1744. {
  1745. // GET CONFIGURATION
  1746. uint8_t rt = (scsiDev.cdb[1] & 0x03);
  1747. uint16_t startFeature =
  1748. (((uint16_t) scsiDev.cdb[2]) << 8) +
  1749. scsiDev.cdb[3];
  1750. uint16_t allocationLength =
  1751. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1752. scsiDev.cdb[8];
  1753. doGetConfiguration(rt, startFeature, allocationLength);
  1754. }
  1755. else if (command == 0x51)
  1756. {
  1757. uint16_t allocationLength =
  1758. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1759. scsiDev.cdb[8];
  1760. doReadDiscInformation(allocationLength);
  1761. }
  1762. else if (command == 0x52)
  1763. {
  1764. // READ TRACK INFORMATION
  1765. bool track = (scsiDev.cdb[1] & 0x01);
  1766. uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
  1767. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1768. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1769. scsiDev.cdb[5];
  1770. uint16_t allocationLength =
  1771. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1772. scsiDev.cdb[8];
  1773. doReadTrackInformation(track, lba, allocationLength);
  1774. }
  1775. else if (command == 0x4A)
  1776. {
  1777. // Get event status notifications (media change notifications)
  1778. bool immed = scsiDev.cdb[1] & 1;
  1779. doGetEventStatusNotification(immed);
  1780. }
  1781. else if (command == 0x45)
  1782. {
  1783. // PLAY AUDIO (10)
  1784. uint32_t lba =
  1785. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1786. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1787. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1788. scsiDev.cdb[5];
  1789. uint32_t blocks =
  1790. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1791. scsiDev.cdb[8];
  1792. doPlayAudio(lba, blocks);
  1793. }
  1794. else if (command == 0xA5)
  1795. {
  1796. // PLAY AUDIO (12)
  1797. uint32_t lba =
  1798. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1799. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1800. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1801. scsiDev.cdb[5];
  1802. uint32_t blocks =
  1803. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1804. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1805. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1806. scsiDev.cdb[9];
  1807. doPlayAudio(lba, blocks);
  1808. }
  1809. else if (command == 0x47)
  1810. {
  1811. // PLAY AUDIO (MSF)
  1812. uint32_t start = MSF2LBA(scsiDev.cdb[3], scsiDev.cdb[4], scsiDev.cdb[5], false);
  1813. uint32_t end = MSF2LBA(scsiDev.cdb[6], scsiDev.cdb[7], scsiDev.cdb[8], false);
  1814. uint32_t lba = start;
  1815. if (scsiDev.cdb[3] == 0xFF
  1816. && scsiDev.cdb[4] == 0xFF
  1817. && scsiDev.cdb[5] == 0xFF)
  1818. {
  1819. // request to start playback from 'current position'
  1820. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1821. lba = img.file.position() / 2352;
  1822. }
  1823. uint32_t length = end - lba;
  1824. doPlayAudio(lba, length);
  1825. }
  1826. else if (command == 0x4B)
  1827. {
  1828. // PAUSE/RESUME AUDIO
  1829. doPauseResumeAudio(scsiDev.cdb[8] & 1);
  1830. }
  1831. else if (command == 0xBD)
  1832. {
  1833. // Mechanism status
  1834. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[8]) << 8) + scsiDev.cdb[9];
  1835. doMechanismStatus(allocationLength);
  1836. }
  1837. else if (command == 0xBB)
  1838. {
  1839. // Set CD speed (just ignored)
  1840. scsiDev.status = 0;
  1841. scsiDev.phase = STATUS;
  1842. }
  1843. else if (command == 0xBE)
  1844. {
  1845. // ReadCD (in low level format)
  1846. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1847. uint32_t lba =
  1848. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1849. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1850. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1851. scsiDev.cdb[5];
  1852. uint32_t blocks =
  1853. (((uint32_t) scsiDev.cdb[6]) << 16) +
  1854. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1855. (((uint32_t) scsiDev.cdb[8]));
  1856. uint8_t main_channel = scsiDev.cdb[9];
  1857. uint8_t sub_channel = scsiDev.cdb[10];
  1858. doReadCD(lba, blocks, sector_type, main_channel, sub_channel, false);
  1859. }
  1860. else if (command == 0xB9)
  1861. {
  1862. // ReadCD MSF
  1863. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1864. uint32_t start = MSF2LBA(scsiDev.cdb[3], scsiDev.cdb[4], scsiDev.cdb[5], false);
  1865. uint32_t end = MSF2LBA(scsiDev.cdb[6], scsiDev.cdb[7], scsiDev.cdb[8], false);
  1866. uint8_t main_channel = scsiDev.cdb[9];
  1867. uint8_t sub_channel = scsiDev.cdb[10];
  1868. doReadCD(start, end - start, sector_type, main_channel, sub_channel, false);
  1869. }
  1870. else if (command == 0x42)
  1871. {
  1872. // Read subchannel data
  1873. bool time = (scsiDev.cdb[1] & 0x02);
  1874. bool subq = (scsiDev.cdb[2] & 0x40);
  1875. uint8_t parameter = scsiDev.cdb[3];
  1876. uint8_t track_number = scsiDev.cdb[6];
  1877. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
  1878. doReadSubchannel(time, subq, parameter, track_number, allocationLength);
  1879. }
  1880. else if (command == 0x08)
  1881. {
  1882. // READ(6) for CDs (may need sector translation for cue file handling)
  1883. uint32_t lba =
  1884. (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
  1885. (((uint32_t) scsiDev.cdb[2]) << 8) +
  1886. scsiDev.cdb[3];
  1887. uint32_t blocks = scsiDev.cdb[4];
  1888. if (blocks == 0) blocks = 256;
  1889. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1890. }
  1891. else if (command == 0x28)
  1892. {
  1893. // READ(10) for CDs (may need sector translation for cue file handling)
  1894. uint32_t lba =
  1895. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1896. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1897. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1898. scsiDev.cdb[5];
  1899. uint32_t blocks =
  1900. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1901. scsiDev.cdb[8];
  1902. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1903. }
  1904. else if (command == 0xA8)
  1905. {
  1906. // READ(12) for CDs (may need sector translation for cue file handling)
  1907. uint32_t lba =
  1908. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1909. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1910. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1911. scsiDev.cdb[5];
  1912. uint32_t blocks =
  1913. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1914. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1915. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1916. scsiDev.cdb[9];
  1917. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1918. }
  1919. else if (command == 0x4E)
  1920. {
  1921. // STOP PLAY/SCAN
  1922. doStopAudio();
  1923. scsiDev.status = 0;
  1924. scsiDev.phase = STATUS;
  1925. }
  1926. else if (command == 0x01)
  1927. {
  1928. // REZERO UNIT
  1929. // AppleCD Audio Player uses this as a nonstandard
  1930. // "stop audio playback" command
  1931. doStopAudio();
  1932. scsiDev.status = 0;
  1933. scsiDev.phase = STATUS;
  1934. }
  1935. else if (command == 0x0B || command == 0x2B)
  1936. {
  1937. // SEEK
  1938. // implement Annex C termination requirement and pass to disk handler
  1939. doStopAudio();
  1940. // this may need more specific handling, the Win9x player appears to
  1941. // expect a pickup move to the given LBA
  1942. commandHandled = 0;
  1943. }
  1944. else if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_APPLE
  1945. && command == 0xCD)
  1946. {
  1947. // vendor-specific command issued by the AppleCD Audio Player in
  1948. // response to fast-forward or rewind commands. Might be seek,
  1949. // might be reposition. Exact MSF value below is unknown.
  1950. //
  1951. // Byte 0: 0xCD
  1952. // Byte 1: 0x10 for rewind, 0x00 for fast-forward
  1953. // Byte 2: 0x00
  1954. // Byte 3: 'M' in hex
  1955. // Byte 4: 'S' in hex
  1956. // Byte 5: 'F' in hex
  1957. commandHandled = 0;
  1958. }
  1959. else
  1960. {
  1961. commandHandled = 0;
  1962. }
  1963. return commandHandled;
  1964. }