BlueSCSI_cdrom.cpp 54 KB

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