BlueSCSI_cdrom.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185
  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 & 7);
  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 CDROM 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 & 7;
  1044. debuglog("------ CDROM close tray on ID ", (int)target);
  1045. img.ejected = false;
  1046. img.cdrom_events = 2; // New media
  1047. }
  1048. }
  1049. // Eject CDROM tray if closed, close if open
  1050. // Switch image on ejection.
  1051. void cdromPerformEject(image_config_t &img)
  1052. {
  1053. uint8_t target = img.scsiId & 7;
  1054. #if ENABLE_AUDIO_OUTPUT
  1055. // terminate audio playback if active on this target (MMC-1 Annex C)
  1056. audio_stop(target);
  1057. #endif
  1058. if (!img.ejected)
  1059. {
  1060. debuglog("------ CDROM open tray on ID ", (int)target);
  1061. img.ejected = true;
  1062. img.cdrom_events = 3; // Media removal
  1063. cdromSwitchNextImage(img); // Switch media for next time
  1064. }
  1065. else
  1066. {
  1067. cdromCloseTray(img);
  1068. }
  1069. }
  1070. // Reinsert any ejected CDROMs on reboot
  1071. void cdromReinsertFirstImage(image_config_t &img)
  1072. {
  1073. if (img.image_index > 0)
  1074. {
  1075. // Multiple images for this drive, force restart from first one
  1076. uint8_t target = img.scsiId & 7;
  1077. debuglog("---- Restarting from first CD-ROM image for ID ", (int)target);
  1078. img.image_index = -1;
  1079. img.current_image[0] = '\0';
  1080. cdromSwitchNextImage(img);
  1081. }
  1082. else if (img.ejected)
  1083. {
  1084. // Reinsert the single image
  1085. cdromCloseTray(img);
  1086. }
  1087. }
  1088. // Check if we have multiple CD-ROM images to cycle when drive is ejected.
  1089. bool cdromSwitchNextImage(image_config_t &img)
  1090. {
  1091. // Check if we have a next image to load, so that drive is closed next time the host asks.
  1092. char filename[MAX_FILE_PATH];
  1093. int target_idx = img.scsiId & 7;
  1094. scsiDiskGetNextImageName(img, filename, sizeof(filename));
  1095. #ifdef ENABLE_AUDIO_OUTPUT
  1096. // if in progress for this device, terminate audio playback immediately (Annex C)
  1097. audio_stop(target_idx);
  1098. // Reset position tracking for the new image
  1099. audio_get_status_code(target_idx); // trash audio status code
  1100. #endif
  1101. if (filename[0] != '\0')
  1102. {
  1103. log("Switching to next CD-ROM image for SCSI ID: ", target_idx, ": ", filename);
  1104. img.file.close();
  1105. bool status = scsiDiskOpenHDDImage(target_idx, filename, target_idx, 0,
  1106. getBlockSize(filename, target_idx, 2048));
  1107. if (status)
  1108. {
  1109. return true;
  1110. }
  1111. }
  1112. return false;
  1113. }
  1114. static void doGetEventStatusNotification(bool immed)
  1115. {
  1116. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1117. if (!immed)
  1118. {
  1119. // Asynchronous notification not supported
  1120. scsiDev.status = CHECK_CONDITION;
  1121. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1122. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1123. scsiDev.phase = STATUS;
  1124. }
  1125. else if (img.cdrom_events)
  1126. {
  1127. scsiDev.data[0] = 0;
  1128. scsiDev.data[1] = 6; // EventDataLength
  1129. scsiDev.data[2] = 0x04; // Media status events
  1130. scsiDev.data[3] = 0x04; // Supported events
  1131. scsiDev.data[4] = img.cdrom_events;
  1132. scsiDev.data[5] = 0x01; // Power status
  1133. scsiDev.data[6] = 0; // Start slot
  1134. scsiDev.data[7] = 0; // End slot
  1135. scsiDev.dataLen = 8;
  1136. scsiDev.phase = DATA_IN;
  1137. img.cdrom_events = 0;
  1138. if (img.ejected && img.reinsert_after_eject)
  1139. {
  1140. // We are now reporting to host that the drive is open.
  1141. // Simulate a "close" for next time the host polls.
  1142. cdromCloseTray(img);
  1143. }
  1144. }
  1145. else
  1146. {
  1147. scsiDev.data[0] = 0;
  1148. scsiDev.data[1] = 2; // EventDataLength
  1149. scsiDev.data[2] = 0x00; // Media status events
  1150. scsiDev.data[3] = 0x04; // Supported events
  1151. scsiDev.dataLen = 4;
  1152. scsiDev.phase = DATA_IN;
  1153. }
  1154. }
  1155. /**************************************/
  1156. /* CD-ROM audio playback */
  1157. /**************************************/
  1158. void cdromGetAudioPlaybackStatus(uint8_t *status, uint32_t *current_lba, bool current_only)
  1159. {
  1160. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1161. #ifdef ENABLE_AUDIO_OUTPUT
  1162. if (status) {
  1163. uint8_t target = img.scsiId & 7;
  1164. if (current_only) {
  1165. *status = audio_is_playing(target) ? 1 : 0;
  1166. } else {
  1167. *status = (uint8_t) audio_get_status_code(target);
  1168. }
  1169. }
  1170. #else
  1171. if (status) *status = 0; // audio status code for 'unsupported/invalid' and not-playing indicator
  1172. #endif
  1173. if (current_lba)
  1174. {
  1175. if (img.file.isOpen()) {
  1176. *current_lba = img.file.position() / 2352;
  1177. } else {
  1178. *current_lba = 0;
  1179. }
  1180. }
  1181. }
  1182. static void doPlayAudio(uint32_t lba, uint32_t length)
  1183. {
  1184. #ifdef ENABLE_AUDIO_OUTPUT
  1185. debuglog("------ CD-ROM Play Audio request at ", lba, " for ", length, " sectors");
  1186. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1187. uint8_t target_id = img.scsiId & 7;
  1188. // Per Annex C terminate playback immediately if already in progress on
  1189. // the current target. Non-current targets may also get their audio
  1190. // interrupted later due to hardware limitations
  1191. audio_stop(img.scsiId & 7);
  1192. // if transfer length is zero no audio playback happens.
  1193. // don't treat as an error per SCSI-2; handle via short-circuit
  1194. if (length == 0)
  1195. {
  1196. scsiDev.status = 0;
  1197. scsiDev.phase = STATUS;
  1198. return;
  1199. }
  1200. // if actual playback is requested perform steps to verify prior to playback
  1201. CUEParser parser;
  1202. if (loadCueSheet(img, parser))
  1203. {
  1204. CUETrackInfo trackinfo = {};
  1205. getTrackFromLBA(parser, lba, &trackinfo);
  1206. if (lba == 0xFFFFFFFF)
  1207. {
  1208. // request to start playback from 'current position'
  1209. lba = img.file.position() / 2352;
  1210. }
  1211. uint64_t offset = trackinfo.file_offset
  1212. + trackinfo.sector_length * (lba - trackinfo.track_start);
  1213. debuglog("------ Play audio CD: ", (int)length, " sectors starting at ", (int)lba,
  1214. ", track number ", trackinfo.track_number, ", data offset in file ", (int)offset);
  1215. if (trackinfo.track_mode != CUETrack_AUDIO)
  1216. {
  1217. debuglog("---- Host tried audio playback on track type ", (int)trackinfo.track_mode);
  1218. scsiDev.status = CHECK_CONDITION;
  1219. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1220. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1221. scsiDev.phase = STATUS;
  1222. return;
  1223. }
  1224. // playback request appears to be sane, so perform it
  1225. // see earlier note for context on the block length below
  1226. if (!audio_play(target_id, &(img.file), offset,
  1227. offset + length * trackinfo.sector_length, false))
  1228. {
  1229. // Underlying data/media error? Fake a disk scratch, which should
  1230. // be a condition most CD-DA players are expecting
  1231. scsiDev.status = CHECK_CONDITION;
  1232. scsiDev.target->sense.code = MEDIUM_ERROR;
  1233. scsiDev.target->sense.asc = 0x1106; // CIRC UNRECOVERED ERROR
  1234. scsiDev.phase = STATUS;
  1235. return;
  1236. }
  1237. scsiDev.status = 0;
  1238. scsiDev.phase = STATUS;
  1239. }
  1240. else
  1241. {
  1242. // virtual drive supports audio, just not with this disk image
  1243. debuglog("---- Request to play audio on non-audio image");
  1244. scsiDev.status = CHECK_CONDITION;
  1245. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1246. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1247. scsiDev.phase = STATUS;
  1248. }
  1249. #else
  1250. debuglog("---- Target does not support audio playback");
  1251. // per SCSI-2, targets not supporting audio respond to zero-length
  1252. // PLAY AUDIO commands with ILLEGAL REQUEST; this seems to be a check
  1253. // performed by at least some audio playback software
  1254. scsiDev.status = CHECK_CONDITION;
  1255. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1256. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  1257. scsiDev.phase = STATUS;
  1258. #endif
  1259. }
  1260. static void doPauseResumeAudio(bool resume)
  1261. {
  1262. #ifdef ENABLE_AUDIO_OUTPUT
  1263. log("------ CD-ROM ", resume ? "resume" : "pause", " audio playback");
  1264. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1265. uint8_t target_id = img.scsiId & 7;
  1266. if (audio_is_playing(target_id))
  1267. {
  1268. audio_set_paused(target_id, !resume);
  1269. scsiDev.status = 0;
  1270. scsiDev.phase = STATUS;
  1271. }
  1272. else
  1273. {
  1274. scsiDev.status = CHECK_CONDITION;
  1275. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1276. scsiDev.target->sense.asc = 0x2C00; // COMMAND SEQUENCE ERROR
  1277. scsiDev.phase = STATUS;
  1278. }
  1279. #else
  1280. debuglog("---- Target does not support audio pausing");
  1281. scsiDev.status = CHECK_CONDITION;
  1282. scsiDev.target->sense.code = ILLEGAL_REQUEST; // assumed from PLAY AUDIO(10)
  1283. scsiDev.target->sense.asc = 0x0000; // NO ADDITIONAL SENSE INFORMATION
  1284. scsiDev.phase = STATUS;
  1285. #endif
  1286. }
  1287. static void doStopAudio()
  1288. {
  1289. debuglog("------ CD-ROM Stop Audio request");
  1290. #ifdef ENABLE_AUDIO_OUTPUT
  1291. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1292. uint8_t target_id = img.scsiId & 7;
  1293. audio_stop(target_id);
  1294. #endif
  1295. }
  1296. static void doMechanismStatus(uint16_t allocation_length)
  1297. {
  1298. uint8_t *buf = scsiDev.data;
  1299. uint8_t status;
  1300. uint32_t lba;
  1301. cdromGetAudioPlaybackStatus(&status, &lba, true);
  1302. *buf++ = 0x00; // No fault state
  1303. *buf++ = (status) ? 0x20 : 0x00; // Currently playing?
  1304. *buf++ = (lba >> 16) & 0xFF;
  1305. *buf++ = (lba >> 8) & 0xFF;
  1306. *buf++ = (lba >> 0) & 0xFF;
  1307. *buf++ = 0; // No CD changer
  1308. *buf++ = 0;
  1309. *buf++ = 0;
  1310. int len = 8;
  1311. if (len > allocation_length) len = allocation_length;
  1312. scsiDev.dataLen = len;
  1313. scsiDev.phase = DATA_IN;
  1314. }
  1315. /*******************************************/
  1316. /* CD-ROM data reading in low level format */
  1317. /*******************************************/
  1318. static void doReadCD(uint32_t lba, uint32_t length, uint8_t sector_type,
  1319. uint8_t main_channel, uint8_t sub_channel, bool data_only)
  1320. {
  1321. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1322. #if ENABLE_AUDIO_OUTPUT
  1323. // terminate audio playback if active on this target (Annex C)
  1324. audio_stop(img.scsiId & 7);
  1325. #endif
  1326. CUEParser parser;
  1327. if (!loadCueSheet(img, parser)
  1328. && (sector_type == 0 || sector_type == 2)
  1329. && main_channel == 0x10 && sub_channel == 0)
  1330. {
  1331. // Simple case, return sector data directly
  1332. scsiDiskStartRead(lba, length);
  1333. return;
  1334. }
  1335. // Search the track with the requested LBA
  1336. // Supplies dummy data if no cue sheet is active.
  1337. CUETrackInfo trackinfo = {};
  1338. getTrackFromLBA(parser, lba, &trackinfo);
  1339. // Figure out the data offset in the file
  1340. uint64_t offset = trackinfo.file_offset + trackinfo.sector_length * (lba - trackinfo.track_start);
  1341. debuglog("------ Read CD: ", (int)length, " sectors starting at ", (int)lba,
  1342. ", track number ", trackinfo.track_number, ", sector size ", (int)trackinfo.sector_length,
  1343. ", main channel ", main_channel, ", sub channel ", sub_channel,
  1344. ", data offset in file ", (int)offset);
  1345. // Ensure read is not out of range of the image
  1346. uint64_t readend = offset + trackinfo.sector_length * length;
  1347. if (readend > img.file.size())
  1348. {
  1349. log("WARNING: Host attempted CD read at sector ", lba, "+", length,
  1350. ", exceeding image size ", img.file.size());
  1351. scsiDev.status = CHECK_CONDITION;
  1352. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1353. scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1354. scsiDev.phase = STATUS;
  1355. return;
  1356. }
  1357. // Verify sector type
  1358. if (sector_type != 0)
  1359. {
  1360. bool sector_type_ok = false;
  1361. if (sector_type == 1 && trackinfo.track_mode == CUETrack_AUDIO)
  1362. {
  1363. sector_type_ok = true;
  1364. }
  1365. else if (sector_type == 2 && trackinfo.track_mode == CUETrack_MODE1_2048)
  1366. {
  1367. sector_type_ok = true;
  1368. }
  1369. if (!sector_type_ok)
  1370. {
  1371. debuglog("---- Failed sector type check, host requested ", (int)sector_type, " CUE file has ", (int)trackinfo.track_mode);
  1372. scsiDev.status = CHECK_CONDITION;
  1373. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1374. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1375. scsiDev.phase = STATUS;
  1376. return;
  1377. }
  1378. }
  1379. // Select fields to transfer
  1380. // Refer to table 351 in T10/1545-D MMC-4 Revision 5a
  1381. // Only the mandatory cases are supported.
  1382. int sector_length = 0;
  1383. int skip_begin = 0;
  1384. bool add_fake_headers = false;
  1385. if (main_channel == 0)
  1386. {
  1387. // No actual data requested, just sector type check or subchannel
  1388. sector_length = 0;
  1389. }
  1390. else if (trackinfo.track_mode == CUETrack_AUDIO)
  1391. {
  1392. // Transfer whole 2352 byte audio sectors from file to host
  1393. sector_length = 2352;
  1394. }
  1395. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && main_channel == 0x10)
  1396. {
  1397. // Transfer whole 2048 byte data sectors from file to host
  1398. sector_length = 2048;
  1399. }
  1400. else if (trackinfo.track_mode == CUETrack_MODE1_2048 && (main_channel & 0xB8) == 0xB8)
  1401. {
  1402. // Transfer 2048 bytes of data from file and fake the headers
  1403. sector_length = 2048;
  1404. add_fake_headers = true;
  1405. debuglog("------ Host requested ECC data but image file lacks it, replacing with zeros");
  1406. }
  1407. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && main_channel == 0x10)
  1408. {
  1409. // Transfer the 2048 byte payload of data sector to host.
  1410. sector_length = 2048;
  1411. skip_begin = 16;
  1412. }
  1413. else if (trackinfo.track_mode == CUETrack_MODE1_2352 && (main_channel & 0xB8) == 0xB8)
  1414. {
  1415. // Transfer whole 2352 byte data sector with ECC to host
  1416. sector_length = 2352;
  1417. }
  1418. else
  1419. {
  1420. debuglog("---- Unsupported channel request for track type ", (int)trackinfo.track_mode);
  1421. scsiDev.status = CHECK_CONDITION;
  1422. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1423. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1424. scsiDev.phase = STATUS;
  1425. return;
  1426. }
  1427. if (data_only && sector_length != 2048)
  1428. {
  1429. debuglog("------ Host tried to read non-data sector with standard READ command");
  1430. scsiDev.status = CHECK_CONDITION;
  1431. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1432. scsiDev.target->sense.asc = 0x6400; // ILLEGAL MODE FOR THIS TRACK
  1433. scsiDev.phase = STATUS;
  1434. return;
  1435. }
  1436. bool field_q_subchannel = false;
  1437. if (sub_channel == 2)
  1438. {
  1439. // Include position information in Q subchannel
  1440. field_q_subchannel = true;
  1441. }
  1442. else if (sub_channel != 0)
  1443. {
  1444. debuglog("---- Unsupported subchannel request");
  1445. scsiDev.status = CHECK_CONDITION;
  1446. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1447. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1448. scsiDev.phase = STATUS;
  1449. return;
  1450. }
  1451. scsiDev.phase = DATA_IN;
  1452. scsiDev.dataLen = 0;
  1453. scsiDev.dataPtr = 0;
  1454. scsiEnterPhase(DATA_IN);
  1455. // Use two buffers alternately for formatting sector data
  1456. uint32_t result_length = sector_length + (field_q_subchannel ? 16 : 0) + (add_fake_headers ? 304 : 0);
  1457. uint8_t *buf0 = scsiDev.data;
  1458. uint8_t *buf1 = scsiDev.data + result_length;
  1459. // Format the sectors for transfer
  1460. for (uint32_t idx = 0; idx < length; idx++)
  1461. {
  1462. platform_poll();
  1463. diskEjectButtonUpdate(false);
  1464. img.file.seek(offset + idx * trackinfo.sector_length + skip_begin);
  1465. // Verify that previous write using this buffer has finished
  1466. uint8_t *buf = ((idx & 1) ? buf1 : buf0);
  1467. uint8_t *bufstart = buf;
  1468. uint32_t start = millis();
  1469. while (!scsiIsWriteFinished(buf + result_length - 1) && !scsiDev.resetFlag)
  1470. {
  1471. if ((uint32_t)(millis() - start) > 5000)
  1472. {
  1473. log("doReadCD() timeout waiting for previous to finish");
  1474. scsiDev.resetFlag = 1;
  1475. }
  1476. platform_poll();
  1477. diskEjectButtonUpdate(false);
  1478. }
  1479. if (scsiDev.resetFlag) break;
  1480. if (add_fake_headers)
  1481. {
  1482. // 12-byte data sector sync pattern
  1483. *buf++ = 0x00;
  1484. for (int i = 0; i < 10; i++)
  1485. {
  1486. *buf++ = 0xFF;
  1487. }
  1488. *buf++ = 0x00;
  1489. // 4-byte data sector header
  1490. LBA2MSFBCD(lba + idx, buf, false);
  1491. buf += 3;
  1492. *buf++ = 0x01; // Mode 1
  1493. }
  1494. if (sector_length > 0)
  1495. {
  1496. // User data
  1497. img.file.read(buf, sector_length);
  1498. buf += sector_length;
  1499. }
  1500. if (add_fake_headers)
  1501. {
  1502. // 288 bytes of ECC
  1503. memset(buf, 0, 288);
  1504. buf += 288;
  1505. }
  1506. if (field_q_subchannel)
  1507. {
  1508. // Formatted Q subchannel data
  1509. // Refer to table 354 in T10/1545-D MMC-4 Revision 5a
  1510. // and ECMA-130 22.3.3
  1511. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14); // Control & ADR
  1512. *buf++ = trackinfo.track_number;
  1513. *buf++ = (lba + idx >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1514. int32_t rel = (int32_t)(lba + idx) - (int32_t)trackinfo.data_start;
  1515. LBA2MSF(rel, buf, true); buf += 3;
  1516. *buf++ = 0;
  1517. LBA2MSF(lba + idx, buf, false); buf += 3;
  1518. *buf++ = 0; *buf++ = 0; // CRC (optional)
  1519. *buf++ = 0; *buf++ = 0; *buf++ = 0; // (pad)
  1520. *buf++ = 0; // No P subchannel
  1521. }
  1522. assert(buf == bufstart + result_length);
  1523. scsiStartWrite(bufstart, result_length);
  1524. }
  1525. scsiFinishWrite();
  1526. scsiDev.status = 0;
  1527. scsiDev.phase = STATUS;
  1528. }
  1529. static void doReadSubchannel(bool time, bool subq, uint8_t parameter, uint8_t track_number, uint16_t allocation_length)
  1530. {
  1531. uint8_t *buf = scsiDev.data;
  1532. if (parameter == 0x01)
  1533. {
  1534. uint8_t audiostatus;
  1535. uint32_t lba;
  1536. cdromGetAudioPlaybackStatus(&audiostatus, &lba, false);
  1537. debuglog("------ Get audio playback position: status ", (int)audiostatus, " lba ", (int)lba);
  1538. // Fetch current track info
  1539. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1540. CUEParser parser;
  1541. CUETrackInfo trackinfo = {};
  1542. loadCueSheet(img, parser);
  1543. getTrackFromLBA(parser, lba, &trackinfo);
  1544. // Request sub channel data at current playback position
  1545. *buf++ = 0; // Reserved
  1546. *buf++ = audiostatus;
  1547. int len;
  1548. if (subq)
  1549. {
  1550. len = 12;
  1551. *buf++ = 0; // Subchannel data length (MSB)
  1552. *buf++ = len; // Subchannel data length (LSB)
  1553. *buf++ = 0x01; // Subchannel data format
  1554. *buf++ = (trackinfo.track_mode == CUETrack_AUDIO ? 0x10 : 0x14);
  1555. *buf++ = trackinfo.track_number;
  1556. *buf++ = (lba >= trackinfo.data_start) ? 1 : 0; // Index number (0 = pregap)
  1557. if (time)
  1558. {
  1559. *buf++ = 0;
  1560. LBA2MSF(lba, buf, false);
  1561. debuglog("------ ABS M ", *buf, " S ", *(buf+1), " F ", *(buf+2));
  1562. buf += 3;
  1563. }
  1564. else
  1565. {
  1566. *buf++ = (lba >> 24) & 0xFF; // Absolute block address
  1567. *buf++ = (lba >> 16) & 0xFF;
  1568. *buf++ = (lba >> 8) & 0xFF;
  1569. *buf++ = (lba >> 0) & 0xFF;
  1570. }
  1571. int32_t relpos = (int32_t)lba - (int32_t)trackinfo.data_start;
  1572. if (time)
  1573. {
  1574. *buf++ = 0;
  1575. LBA2MSF(relpos, buf, true);
  1576. debuglog("------ REL M ", *buf, " S ", *(buf+1), " F ", *(buf+2));
  1577. buf += 3;
  1578. }
  1579. else
  1580. {
  1581. uint32_t urelpos = relpos;
  1582. *buf++ = (urelpos >> 24) & 0xFF; // Track relative position (may be negative)
  1583. *buf++ = (urelpos >> 16) & 0xFF;
  1584. *buf++ = (urelpos >> 8) & 0xFF;
  1585. *buf++ = (urelpos >> 0) & 0xFF;
  1586. }
  1587. }
  1588. else
  1589. {
  1590. len = 0;
  1591. *buf++ = 0;
  1592. *buf++ = 0;
  1593. }
  1594. len += 4;
  1595. if (len > allocation_length) len = allocation_length;
  1596. scsiDev.dataLen = len;
  1597. scsiDev.phase = DATA_IN;
  1598. }
  1599. else
  1600. {
  1601. debuglog("---- Unsupported subchannel request");
  1602. scsiDev.status = CHECK_CONDITION;
  1603. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1604. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1605. scsiDev.phase = STATUS;
  1606. return;
  1607. }
  1608. }
  1609. static bool doReadCapacity(uint32_t lba, uint8_t pmi)
  1610. {
  1611. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1612. CUEParser parser;
  1613. if (!loadCueSheet(img, parser))
  1614. {
  1615. // basic image, let the disk handler resolve
  1616. return false;
  1617. }
  1618. // find the last track on the disk
  1619. CUETrackInfo lasttrack = {0};
  1620. const CUETrackInfo *trackinfo;
  1621. while ((trackinfo = parser.next_track()) != NULL)
  1622. {
  1623. lasttrack = *trackinfo;
  1624. }
  1625. uint32_t capacity = 0;
  1626. if (lasttrack.track_number != 0)
  1627. {
  1628. capacity = getLeadOutLBA(&lasttrack);
  1629. capacity--; // shift to last addressable LBA
  1630. if (pmi && lba && lba > capacity)
  1631. {
  1632. // MMC technically specifies that PMI should be zero, but SCSI-2 allows this
  1633. // potentially consider treating either out-of-bounds or PMI set as an error
  1634. // for now just ignore this
  1635. }
  1636. debuglog("----- Reporting capacity as ", capacity);
  1637. }
  1638. else
  1639. {
  1640. log("WARNING: unable to find capacity, no cue file found for ID ", img.scsiId);
  1641. }
  1642. scsiDev.data[0] = capacity >> 24;
  1643. scsiDev.data[1] = capacity >> 16;
  1644. scsiDev.data[2] = capacity >> 8;
  1645. scsiDev.data[3] = capacity;
  1646. scsiDev.data[4] = 0;
  1647. scsiDev.data[5] = 0;
  1648. scsiDev.data[6] = 0x08; // rest of code assumes 2048 here
  1649. scsiDev.data[7] = 0x00;
  1650. scsiDev.dataLen = 8;
  1651. scsiDev.phase = DATA_IN;
  1652. return true;
  1653. }
  1654. /**************************************/
  1655. /* CD-ROM command dispatching */
  1656. /**************************************/
  1657. // Handle direct-access scsi device commands
  1658. extern "C" int scsiCDRomCommand()
  1659. {
  1660. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1661. int commandHandled = 1;
  1662. uint8_t command = scsiDev.cdb[0];
  1663. if (command == 0x1B)
  1664. {
  1665. #if ENABLE_AUDIO_OUTPUT
  1666. // terminate audio playback if active on this target (MMC-1 Annex C)
  1667. audio_stop(img.scsiId & 7);
  1668. #endif
  1669. if ((scsiDev.cdb[4] & 2))
  1670. {
  1671. // CD-ROM load & eject
  1672. int start = scsiDev.cdb[4] & 1;
  1673. if (start)
  1674. {
  1675. cdromCloseTray(img);
  1676. }
  1677. else
  1678. {
  1679. // Eject and switch image
  1680. cdromPerformEject(img);
  1681. }
  1682. }
  1683. }
  1684. else if (command == 0x25)
  1685. {
  1686. // READ CAPACITY
  1687. uint8_t reladdr = scsiDev.cdb[1] & 1;
  1688. uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
  1689. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1690. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1691. scsiDev.cdb[5];
  1692. uint8_t pmi = scsiDev.cdb[8] & 1;
  1693. // allow PMI as long as LBA is specified, this is permitted in SCSI-2
  1694. // we don't link commands, do not allow RELADDR
  1695. if ((!pmi && lba != 0) || reladdr)
  1696. {
  1697. scsiDev.status = CHECK_CONDITION;
  1698. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1699. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1700. scsiDev.phase = STATUS;
  1701. }
  1702. else
  1703. {
  1704. if (!doReadCapacity(lba, pmi))
  1705. {
  1706. // allow disk handler to resolve this one
  1707. commandHandled = 0;
  1708. }
  1709. }
  1710. }
  1711. else if (command == 0x43)
  1712. {
  1713. // CD-ROM Read TOC
  1714. bool MSF = (scsiDev.cdb[1] & 0x02);
  1715. uint8_t track = scsiDev.cdb[6];
  1716. uint16_t allocationLength =
  1717. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1718. scsiDev.cdb[8];
  1719. // The "format" field is reserved for SCSI-2
  1720. uint8_t format = scsiDev.cdb[2] & 0x0F;
  1721. // Matshita SCSI-2 drives appear to use the high 2 bits of the CDB
  1722. // control byte to switch on session info (0x40) and full toc (0x80)
  1723. // responses that are very similar to the standard formats described
  1724. // in MMC-1. These vendor flags must have been pretty common because
  1725. // even a modern SATA drive (ASUS DRW-24B1ST j) responds to them
  1726. // (though it always replies in hex rather than bcd)
  1727. //
  1728. // The session information page is identical to MMC. The full TOC page
  1729. // is identical _except_ it returns addresses in bcd rather than hex.
  1730. bool useBCD = false;
  1731. if (format == 0 && scsiDev.cdb[9] == 0x80)
  1732. {
  1733. format = 2;
  1734. useBCD = true;
  1735. }
  1736. else if (format == 0 && scsiDev.cdb[9] == 0x40)
  1737. {
  1738. format = 1;
  1739. }
  1740. switch (format)
  1741. {
  1742. case 0: doReadTOC(MSF, track, allocationLength); break; // SCSI-2
  1743. case 1: doReadSessionInfo(MSF, allocationLength); break; // MMC2
  1744. case 2: doReadFullTOC(track, allocationLength, useBCD); break; // MMC2
  1745. default:
  1746. {
  1747. scsiDev.status = CHECK_CONDITION;
  1748. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1749. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1750. scsiDev.phase = STATUS;
  1751. }
  1752. }
  1753. }
  1754. else if (command == 0x44)
  1755. {
  1756. // CD-ROM Read Header
  1757. bool MSF = (scsiDev.cdb[1] & 0x02);
  1758. uint32_t lba = 0; // IGNORED for now
  1759. uint16_t allocationLength =
  1760. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1761. scsiDev.cdb[8];
  1762. doReadHeader(MSF, lba, allocationLength);
  1763. }
  1764. else if (command == 0x46)
  1765. {
  1766. // GET CONFIGURATION
  1767. uint8_t rt = (scsiDev.cdb[1] & 0x03);
  1768. uint16_t startFeature =
  1769. (((uint16_t) scsiDev.cdb[2]) << 8) +
  1770. scsiDev.cdb[3];
  1771. uint16_t allocationLength =
  1772. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1773. scsiDev.cdb[8];
  1774. doGetConfiguration(rt, startFeature, allocationLength);
  1775. }
  1776. else if (command == 0x51)
  1777. {
  1778. uint16_t allocationLength =
  1779. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1780. scsiDev.cdb[8];
  1781. doReadDiscInformation(allocationLength);
  1782. }
  1783. else if (command == 0x52)
  1784. {
  1785. // READ TRACK INFORMATION
  1786. bool track = (scsiDev.cdb[1] & 0x01);
  1787. uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
  1788. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1789. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1790. scsiDev.cdb[5];
  1791. uint16_t allocationLength =
  1792. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1793. scsiDev.cdb[8];
  1794. doReadTrackInformation(track, lba, allocationLength);
  1795. }
  1796. else if (command == 0x4A)
  1797. {
  1798. // Get event status notifications (media change notifications)
  1799. bool immed = scsiDev.cdb[1] & 1;
  1800. doGetEventStatusNotification(immed);
  1801. }
  1802. else if (command == 0x45)
  1803. {
  1804. // PLAY AUDIO (10)
  1805. uint32_t lba =
  1806. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1807. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1808. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1809. scsiDev.cdb[5];
  1810. uint32_t blocks =
  1811. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1812. scsiDev.cdb[8];
  1813. doPlayAudio(lba, blocks);
  1814. }
  1815. else if (command == 0xA5)
  1816. {
  1817. // PLAY AUDIO (12)
  1818. uint32_t lba =
  1819. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1820. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1821. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1822. scsiDev.cdb[5];
  1823. uint32_t blocks =
  1824. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1825. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1826. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1827. scsiDev.cdb[9];
  1828. doPlayAudio(lba, blocks);
  1829. }
  1830. else if (command == 0x47)
  1831. {
  1832. // PLAY AUDIO (MSF)
  1833. uint32_t start = MSF2LBA(scsiDev.cdb[3], scsiDev.cdb[4], scsiDev.cdb[5], false);
  1834. uint32_t end = MSF2LBA(scsiDev.cdb[6], scsiDev.cdb[7], scsiDev.cdb[8], false);
  1835. uint32_t lba = start;
  1836. if (scsiDev.cdb[3] == 0xFF
  1837. && scsiDev.cdb[4] == 0xFF
  1838. && scsiDev.cdb[5] == 0xFF)
  1839. {
  1840. // request to start playback from 'current position'
  1841. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1842. lba = img.file.position() / 2352;
  1843. }
  1844. uint32_t length = end - lba;
  1845. doPlayAudio(lba, length);
  1846. }
  1847. else if (command == 0x4B)
  1848. {
  1849. // PAUSE/RESUME AUDIO
  1850. doPauseResumeAudio(scsiDev.cdb[8] & 1);
  1851. }
  1852. else if (command == 0xBD)
  1853. {
  1854. // Mechanism status
  1855. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[8]) << 8) + scsiDev.cdb[9];
  1856. doMechanismStatus(allocationLength);
  1857. }
  1858. else if (command == 0xBB)
  1859. {
  1860. // Set CD speed (just ignored)
  1861. scsiDev.status = 0;
  1862. scsiDev.phase = STATUS;
  1863. }
  1864. else if (command == 0xBE)
  1865. {
  1866. // ReadCD (in low level format)
  1867. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1868. uint32_t lba =
  1869. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1870. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1871. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1872. scsiDev.cdb[5];
  1873. uint32_t blocks =
  1874. (((uint32_t) scsiDev.cdb[6]) << 16) +
  1875. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1876. (((uint32_t) scsiDev.cdb[8]));
  1877. uint8_t main_channel = scsiDev.cdb[9];
  1878. uint8_t sub_channel = scsiDev.cdb[10];
  1879. doReadCD(lba, blocks, sector_type, main_channel, sub_channel, false);
  1880. }
  1881. else if (command == 0xB9)
  1882. {
  1883. // ReadCD MSF
  1884. uint8_t sector_type = (scsiDev.cdb[1] >> 2) & 7;
  1885. uint32_t start = MSF2LBA(scsiDev.cdb[3], scsiDev.cdb[4], scsiDev.cdb[5], false);
  1886. uint32_t end = MSF2LBA(scsiDev.cdb[6], scsiDev.cdb[7], scsiDev.cdb[8], false);
  1887. uint8_t main_channel = scsiDev.cdb[9];
  1888. uint8_t sub_channel = scsiDev.cdb[10];
  1889. doReadCD(start, end - start, sector_type, main_channel, sub_channel, false);
  1890. }
  1891. else if (command == 0x42)
  1892. {
  1893. // Read subchannel data
  1894. bool time = (scsiDev.cdb[1] & 0x02);
  1895. bool subq = (scsiDev.cdb[2] & 0x40);
  1896. uint8_t parameter = scsiDev.cdb[3];
  1897. uint8_t track_number = scsiDev.cdb[6];
  1898. uint16_t allocationLength = (((uint32_t) scsiDev.cdb[7]) << 8) + scsiDev.cdb[8];
  1899. doReadSubchannel(time, subq, parameter, track_number, allocationLength);
  1900. }
  1901. else if (command == 0x08)
  1902. {
  1903. // READ(6) for CDs (may need sector translation for cue file handling)
  1904. uint32_t lba =
  1905. (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
  1906. (((uint32_t) scsiDev.cdb[2]) << 8) +
  1907. scsiDev.cdb[3];
  1908. uint32_t blocks = scsiDev.cdb[4];
  1909. if (blocks == 0) blocks = 256;
  1910. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1911. }
  1912. else if (command == 0x28)
  1913. {
  1914. // READ(10) for CDs (may need sector translation for cue file handling)
  1915. uint32_t lba =
  1916. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1917. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1918. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1919. scsiDev.cdb[5];
  1920. uint32_t blocks =
  1921. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1922. scsiDev.cdb[8];
  1923. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1924. }
  1925. else if (command == 0xA8)
  1926. {
  1927. // READ(12) for CDs (may need sector translation for cue file handling)
  1928. uint32_t lba =
  1929. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1930. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1931. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1932. scsiDev.cdb[5];
  1933. uint32_t blocks =
  1934. (((uint32_t) scsiDev.cdb[6]) << 24) +
  1935. (((uint32_t) scsiDev.cdb[7]) << 16) +
  1936. (((uint32_t) scsiDev.cdb[8]) << 8) +
  1937. scsiDev.cdb[9];
  1938. doReadCD(lba, blocks, 0, 0x10, 0, true);
  1939. }
  1940. else if (command == 0x4E)
  1941. {
  1942. // STOP PLAY/SCAN
  1943. doStopAudio();
  1944. scsiDev.status = 0;
  1945. scsiDev.phase = STATUS;
  1946. }
  1947. else if (command == 0x01)
  1948. {
  1949. // REZERO UNIT
  1950. // AppleCD Audio Player uses this as a nonstandard
  1951. // "stop audio playback" command
  1952. doStopAudio();
  1953. scsiDev.status = 0;
  1954. scsiDev.phase = STATUS;
  1955. }
  1956. else if (command == 0x0B || command == 0x2B)
  1957. {
  1958. // SEEK
  1959. // implement Annex C termination requirement and pass to disk handler
  1960. doStopAudio();
  1961. // this may need more specific handling, the Win9x player appears to
  1962. // expect a pickup move to the given LBA
  1963. commandHandled = 0;
  1964. }
  1965. else if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_APPLE
  1966. && command == 0xCD)
  1967. {
  1968. // vendor-specific command issued by the AppleCD Audio Player in
  1969. // response to fast-forward or rewind commands. Might be seek,
  1970. // might be reposition. Exact MSF value below is unknown.
  1971. //
  1972. // Byte 0: 0xCD
  1973. // Byte 1: 0x10 for rewind, 0x00 for fast-forward
  1974. // Byte 2: 0x00
  1975. // Byte 3: 'M' in hex
  1976. // Byte 4: 'S' in hex
  1977. // Byte 5: 'F' in hex
  1978. commandHandled = 0;
  1979. }
  1980. else
  1981. {
  1982. commandHandled = 0;
  1983. }
  1984. return commandHandled;
  1985. }