BlueSCSI_disk.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. // This file implements the main SCSI disk emulation and data streaming.
  2. // It is derived from disk.c in SCSI2SD V6.
  3. //
  4. // Licensed under GPL v3.
  5. // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
  6. // Copyright (C) 2014 Doug Brown <doug@downtowndougbrown.com>
  7. // Copyright (C) 2022 Rabbit Hole Computing
  8. #include "BlueSCSI_disk.h"
  9. #include "BlueSCSI_log.h"
  10. #include "BlueSCSI_config.h"
  11. #include "BlueSCSI_presets.h"
  12. #include "BlueSCSI_cdrom.h"
  13. #include "ImageBackingStore.h"
  14. #include "ROMDrive.h"
  15. #include <minIni.h>
  16. #include <string.h>
  17. #include <strings.h>
  18. #include <assert.h>
  19. #include <SdFat.h>
  20. extern "C" {
  21. #include <scsi2sd_time.h>
  22. #include <sd.h>
  23. #include <mode.h>
  24. }
  25. #ifndef PLATFORM_MAX_SCSI_SPEED
  26. #define PLATFORM_MAX_SCSI_SPEED S2S_CFG_SPEED_ASYNC_50
  27. #endif
  28. // This can be overridden in platform file to set the size of the transfers
  29. // used when reading from SCSI bus and writing to SD card.
  30. // When SD card access is fast, these are usually better increased.
  31. // If SD card access is roughly same speed as SCSI bus, these can be left at 512
  32. #ifndef PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE
  33. #define PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE 512
  34. #endif
  35. #ifndef PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE
  36. #define PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE 1024
  37. #endif
  38. // Optimal size for the last write in a write request.
  39. // This is often better a bit smaller than PLATFORM_OPTIMAL_SD_WRITE_SIZE
  40. // to reduce the dead time between end of SCSI transfer and finishing of SD write.
  41. #ifndef PLATFORM_OPTIMAL_LAST_SD_WRITE_SIZE
  42. #define PLATFORM_OPTIMAL_LAST_SD_WRITE_SIZE 512
  43. #endif
  44. // Optimal size for read block from SCSI bus
  45. // For platforms with nonblocking transfer, this can be large.
  46. // For Akai MPC60 compatibility this has to be at least 5120
  47. #ifndef PLATFORM_OPTIMAL_SCSI_READ_BLOCK_SIZE
  48. #ifdef PLATFORM_SCSIPHY_HAS_NONBLOCKING_READ
  49. #define PLATFORM_OPTIMAL_SCSI_READ_BLOCK_SIZE 65536
  50. #else
  51. #define PLATFORM_OPTIMAL_SCSI_READ_BLOCK_SIZE 8192
  52. #endif
  53. #endif
  54. #ifndef PLATFORM_SCSIPHY_HAS_NONBLOCKING_READ
  55. // For platforms that do not have non-blocking read from SCSI bus
  56. void scsiStartRead(uint8_t* data, uint32_t count, int *parityError)
  57. {
  58. scsiRead(data, count, parityError);
  59. }
  60. void scsiFinishRead(uint8_t* data, uint32_t count, int *parityError)
  61. {
  62. }
  63. bool scsiIsReadFinished(const uint8_t *data)
  64. {
  65. return true;
  66. }
  67. #endif
  68. /************************************************/
  69. /* ROM drive support (in microcontroller flash) */
  70. /************************************************/
  71. // Check if rom drive exists and activate it
  72. bool scsiDiskActivateRomDrive()
  73. {
  74. #ifndef PLATFORM_HAS_ROM_DRIVE
  75. return false;
  76. #else
  77. log("");
  78. log("=== ROM Drive ===");
  79. uint32_t maxsize = platform_get_romdrive_maxsize() - PLATFORM_ROMDRIVE_PAGE_SIZE;
  80. log("Platform supports ROM drive up to ", (int)(maxsize / 1024), " kB");
  81. romdrive_hdr_t hdr = {};
  82. if (!romDriveCheckPresent(&hdr))
  83. {
  84. log("---- ROM drive image not detected");
  85. return false;
  86. }
  87. if (ini_getbool("SCSI", "DisableROMDrive", 0, CONFIGFILE))
  88. {
  89. log("---- ROM drive disabled in ini file, not enabling");
  90. return false;
  91. }
  92. else
  93. {
  94. debuglog("---- ROM drive enabled");
  95. }
  96. long rom_scsi_id = ini_getl("SCSI", "ROMDriveSCSIID", -1, CONFIGFILE);
  97. if (rom_scsi_id >= 0 && rom_scsi_id <= 7)
  98. {
  99. hdr.scsi_id = rom_scsi_id;
  100. log("---- ROM drive SCSI id overriden in ini file, changed to ", (int)hdr.scsi_id);
  101. }
  102. if (s2s_getConfigById(hdr.scsi_id))
  103. {
  104. log("---- ROM drive SCSI id ", (int)hdr.scsi_id, " is already in use, not enabling");
  105. return false;
  106. }
  107. log("---- Activating ROM drive, SCSI id ", (int)hdr.scsi_id, " size ", (int)(hdr.imagesize / 1024), " kB");
  108. bool status = scsiDiskOpenHDDImage(hdr.scsi_id, "ROM:", hdr.scsi_id, 0, hdr.blocksize, hdr.drivetype);
  109. if (!status)
  110. {
  111. log("---- ROM drive activation failed");
  112. return false;
  113. }
  114. else
  115. {
  116. log("---- Activated ROM drive, SCSI id ", (int)hdr.scsi_id, " size ", (int)(hdr.imagesize / 1024), " kB");
  117. return true;
  118. }
  119. #endif
  120. }
  121. /***********************/
  122. /* Image configuration */
  123. /***********************/
  124. extern SdFs SD;
  125. SdDevice sdDev = {2, 256 * 1024 * 1024 * 2}; /* For SCSI2SD */
  126. static image_config_t g_DiskImages[S2S_MAX_TARGETS];
  127. void scsiDiskResetImages()
  128. {
  129. for(int i = 0; i < S2S_MAX_TARGETS; i++)
  130. g_DiskImages[i] = image_config_t();
  131. }
  132. void scsiDiskCloseSDCardImages()
  133. {
  134. for (int i = 0; i < S2S_MAX_TARGETS; i++)
  135. {
  136. if (!g_DiskImages[i].file.isRom())
  137. {
  138. g_DiskImages[i].file.close();
  139. }
  140. }
  141. }
  142. // Verify format conformance to SCSI spec:
  143. // - Empty bytes filled with 0x20 (space)
  144. // - Only values 0x20 to 0x7E
  145. // - Left alignment for vendor/product/revision, right alignment for serial.
  146. static void formatDriveInfoField(char *field, int fieldsize, bool align_right)
  147. {
  148. if (align_right)
  149. {
  150. // Right align and trim spaces on either side
  151. int dst = fieldsize - 1;
  152. for (int src = fieldsize - 1; src >= 0; src--)
  153. {
  154. char c = field[src];
  155. if (c < 0x20 || c > 0x7E) c = 0x20;
  156. if (c != 0x20 || dst != fieldsize - 1)
  157. {
  158. field[dst--] = c;
  159. }
  160. }
  161. while (dst >= 0)
  162. {
  163. field[dst--] = 0x20;
  164. }
  165. }
  166. else
  167. {
  168. // Left align, preserve spaces in case config tries to manually right-align
  169. int dst = 0;
  170. for (int src = 0; src < fieldsize; src++)
  171. {
  172. char c = field[src];
  173. if (c < 0x20 || c > 0x7E) c = 0x20;
  174. field[dst++] = c;
  175. }
  176. while (dst < fieldsize)
  177. {
  178. field[dst++] = 0x20;
  179. }
  180. }
  181. }
  182. // Set default drive vendor / product info after the image file
  183. // is loaded and the device type is known.
  184. static void setDefaultDriveInfo(int target_idx)
  185. {
  186. image_config_t &img = g_DiskImages[target_idx];
  187. static const char *driveinfo_fixed[4] = DRIVEINFO_FIXED;
  188. static const char *driveinfo_removable[4] = DRIVEINFO_REMOVABLE;
  189. static const char *driveinfo_optical[4] = DRIVEINFO_OPTICAL;
  190. static const char *driveinfo_floppy[4] = DRIVEINFO_FLOPPY;
  191. static const char *driveinfo_magopt[4] = DRIVEINFO_MAGOPT;
  192. static const char *driveinfo_tape[4] = DRIVEINFO_TAPE;
  193. static const char *apl_driveinfo_fixed[4] = APPLE_DRIVEINFO_FIXED;
  194. static const char *apl_driveinfo_removable[4] = APPLE_DRIVEINFO_REMOVABLE;
  195. static const char *apl_driveinfo_optical[4] = APPLE_DRIVEINFO_OPTICAL;
  196. static const char *apl_driveinfo_floppy[4] = APPLE_DRIVEINFO_FLOPPY;
  197. static const char *apl_driveinfo_magopt[4] = APPLE_DRIVEINFO_MAGOPT;
  198. static const char *apl_driveinfo_tape[4] = APPLE_DRIVEINFO_TAPE;
  199. const char **driveinfo = NULL;
  200. if (img.quirks == S2S_CFG_QUIRKS_APPLE)
  201. {
  202. // Use default drive IDs that are recognized by Apple machines
  203. switch (img.deviceType)
  204. {
  205. case S2S_CFG_FIXED: driveinfo = apl_driveinfo_fixed; break;
  206. case S2S_CFG_REMOVEABLE: driveinfo = apl_driveinfo_removable; break;
  207. case S2S_CFG_OPTICAL: driveinfo = apl_driveinfo_optical; break;
  208. case S2S_CFG_FLOPPY_14MB: driveinfo = apl_driveinfo_floppy; break;
  209. case S2S_CFG_MO: driveinfo = apl_driveinfo_magopt; break;
  210. case S2S_CFG_SEQUENTIAL: driveinfo = apl_driveinfo_tape; break;
  211. default: driveinfo = apl_driveinfo_fixed; break;
  212. }
  213. }
  214. else
  215. {
  216. // Generic IDs
  217. switch (img.deviceType)
  218. {
  219. case S2S_CFG_FIXED: driveinfo = driveinfo_fixed; break;
  220. case S2S_CFG_REMOVEABLE: driveinfo = driveinfo_removable; break;
  221. case S2S_CFG_OPTICAL: driveinfo = driveinfo_optical; break;
  222. case S2S_CFG_FLOPPY_14MB: driveinfo = driveinfo_floppy; break;
  223. case S2S_CFG_MO: driveinfo = driveinfo_magopt; break;
  224. case S2S_CFG_SEQUENTIAL: driveinfo = driveinfo_tape; break;
  225. default: driveinfo = driveinfo_fixed; break;
  226. }
  227. }
  228. if (img.vendor[0] == '\0')
  229. {
  230. memset(img.vendor, 0, sizeof(img.vendor));
  231. strncpy(img.vendor, driveinfo[0], sizeof(img.vendor));
  232. }
  233. if (img.prodId[0] == '\0')
  234. {
  235. memset(img.prodId, 0, sizeof(img.prodId));
  236. strncpy(img.prodId, driveinfo[1], sizeof(img.prodId));
  237. }
  238. if (img.revision[0] == '\0')
  239. {
  240. memset(img.revision, 0, sizeof(img.revision));
  241. strncpy(img.revision, driveinfo[2], sizeof(img.revision));
  242. }
  243. if (img.serial[0] == '\0')
  244. {
  245. memset(img.serial, 0, sizeof(img.serial));
  246. strncpy(img.serial, driveinfo[3], sizeof(img.serial));
  247. }
  248. if (img.serial[0] == '\0')
  249. {
  250. // Use SD card serial number
  251. cid_t sd_cid;
  252. uint32_t sd_sn = 0;
  253. if (SD.card()->readCID(&sd_cid))
  254. {
  255. sd_sn = sd_cid.psn();
  256. }
  257. memset(img.serial, 0, sizeof(img.serial));
  258. const char *nibble = "0123456789ABCDEF";
  259. img.serial[0] = nibble[(sd_sn >> 28) & 0xF];
  260. img.serial[1] = nibble[(sd_sn >> 24) & 0xF];
  261. img.serial[2] = nibble[(sd_sn >> 20) & 0xF];
  262. img.serial[3] = nibble[(sd_sn >> 16) & 0xF];
  263. img.serial[4] = nibble[(sd_sn >> 12) & 0xF];
  264. img.serial[5] = nibble[(sd_sn >> 8) & 0xF];
  265. img.serial[6] = nibble[(sd_sn >> 4) & 0xF];
  266. img.serial[7] = nibble[(sd_sn >> 0) & 0xF];
  267. }
  268. int rightAlign = img.rightAlignStrings;
  269. formatDriveInfoField(img.vendor, sizeof(img.vendor), rightAlign);
  270. formatDriveInfoField(img.prodId, sizeof(img.prodId), rightAlign);
  271. formatDriveInfoField(img.revision, sizeof(img.revision), rightAlign);
  272. formatDriveInfoField(img.serial, sizeof(img.serial), true);
  273. }
  274. bool scsiDiskOpenHDDImage(int target_idx, const char *filename, int scsi_id, int scsi_lun, int blocksize, S2S_CFG_TYPE type)
  275. {
  276. image_config_t &img = g_DiskImages[target_idx];
  277. img.file = ImageBackingStore(filename, blocksize);
  278. if (img.file.isOpen())
  279. {
  280. img.bytesPerSector = blocksize;
  281. img.scsiSectors = img.file.size() / blocksize;
  282. img.scsiId = scsi_id | S2S_CFG_TARGET_ENABLED;
  283. img.sdSectorStart = 0;
  284. if (img.scsiSectors == 0)
  285. {
  286. log("---- Error: image file ", filename, " is empty");
  287. img.file.close();
  288. return false;
  289. }
  290. uint32_t sector_begin = 0, sector_end = 0;
  291. if (img.file.isRom())
  292. {
  293. // ROM is always contiguous, no need to log
  294. }
  295. else if (!img.file.contiguousRange(&sector_begin, &sector_end))
  296. {
  297. log("---- WARNING: file ", filename, " is fragmented, see https://github.com/BlueSCSI/BlueSCSI-v2/wiki/Image-File-Fragmentation");
  298. }
  299. if (type == S2S_CFG_OPTICAL)
  300. {
  301. log("---- Configuring as CD-ROM drive based on image name");
  302. img.deviceType = S2S_CFG_OPTICAL;
  303. }
  304. else if (type == S2S_CFG_FLOPPY_14MB)
  305. {
  306. log("---- Configuring as floppy drive based on image name");
  307. img.deviceType = S2S_CFG_FLOPPY_14MB;
  308. }
  309. else if (type == S2S_CFG_MO)
  310. {
  311. log("---- Configuring as magneto-optical based on image name");
  312. img.deviceType = S2S_CFG_MO;
  313. }
  314. else if (type == S2S_CFG_REMOVEABLE)
  315. {
  316. log("---- Configuring as removable drive based on image name");
  317. img.deviceType = S2S_CFG_REMOVEABLE;
  318. }
  319. else if (type == S2S_CFG_SEQUENTIAL)
  320. {
  321. log("---- Configuring as tape drive based on image name");
  322. img.deviceType = S2S_CFG_SEQUENTIAL;
  323. }
  324. #ifdef PLATFORM_CONFIG_HOOK
  325. PLATFORM_CONFIG_HOOK(&img);
  326. #endif
  327. setDefaultDriveInfo(target_idx);
  328. if (img.prefetchbytes != PREFETCH_BUFFER_SIZE)
  329. {
  330. log("---- Read prefetch enabled: ", (int)img.prefetchbytes, " bytes");
  331. }
  332. else if(img.prefetchbytes == 0)
  333. {
  334. log("---- Read prefetch disabled");
  335. }
  336. else
  337. {
  338. debuglog("---- Read prefetch enabled: ", (int)img.prefetchbytes, " bytes");
  339. }
  340. if (img.deviceType == S2S_CFG_OPTICAL &&
  341. strncasecmp(filename + strlen(filename) - 4, ".bin", 4) == 0)
  342. {
  343. char cuesheetname[MAX_FILE_PATH + 1] = {0};
  344. strncpy(cuesheetname, filename, strlen(filename) - 4);
  345. strlcat(cuesheetname, ".cue", sizeof(cuesheetname));
  346. img.cuesheetfile = SD.open(cuesheetname, O_RDONLY);
  347. if (img.cuesheetfile.isOpen())
  348. {
  349. log("---- Found CD-ROM CUE sheet at ", cuesheetname);
  350. if (!cdromValidateCueSheet(img))
  351. {
  352. log("---- Failed to parse cue sheet, using as plain binary image");
  353. img.cuesheetfile.close();
  354. }
  355. }
  356. else
  357. {
  358. log("---- No CUE sheet found at ", cuesheetname, ", using as plain binary image");
  359. }
  360. }
  361. return true;
  362. }
  363. return false;
  364. }
  365. static void checkDiskGeometryDivisible(image_config_t &img)
  366. {
  367. if (!img.geometrywarningprinted)
  368. {
  369. uint32_t sectorsPerHeadTrack = img.sectorsPerTrack * img.headsPerCylinder;
  370. if (img.scsiSectors % sectorsPerHeadTrack != 0)
  371. {
  372. log("WARNING: Host used command ", scsiDev.cdb[0],
  373. " which is affected by drive geometry. Current settings are ",
  374. (int)img.sectorsPerTrack, " sectors x ", (int)img.headsPerCylinder, " heads = ",
  375. (int)sectorsPerHeadTrack, " but image size of ", (int)img.scsiSectors,
  376. " sectors is not divisible. This can cause error messages in diagnostics tools.");
  377. img.geometrywarningprinted = true;
  378. }
  379. }
  380. }
  381. // Set target configuration to default values
  382. static void scsiDiskConfigDefaults(int target_idx)
  383. {
  384. // Get default values from system preset, if any
  385. char presetName[32];
  386. ini_gets("SCSI", "System", "", presetName, sizeof(presetName), CONFIGFILE);
  387. preset_config_t defaults = getSystemPreset(presetName);
  388. image_config_t &img = g_DiskImages[target_idx];
  389. img.scsiId = target_idx;
  390. img.deviceType = S2S_CFG_FIXED;
  391. img.deviceTypeModifier = defaults.deviceTypeModifier;
  392. img.sectorsPerTrack = defaults.sectorsPerTrack;
  393. img.headsPerCylinder = defaults.headsPerCylinder;
  394. img.quirks = defaults.quirks;
  395. img.prefetchbytes = defaults.prefetchBytes;
  396. memset(img.vendor, 0, sizeof(img.vendor));
  397. memset(img.prodId, 0, sizeof(img.prodId));
  398. memset(img.revision, 0, sizeof(img.revision));
  399. memset(img.serial, 0, sizeof(img.serial));
  400. }
  401. // Load values for target configuration from given section if they exist.
  402. // Otherwise keep current settings.
  403. static void scsiDiskLoadConfig(int target_idx, const char *section)
  404. {
  405. image_config_t &img = g_DiskImages[target_idx];
  406. img.deviceType = ini_getl(section, "Type", img.deviceType, CONFIGFILE);
  407. img.deviceTypeModifier = ini_getl(section, "TypeModifier", img.deviceTypeModifier, CONFIGFILE);
  408. img.sectorsPerTrack = ini_getl(section, "SectorsPerTrack", img.sectorsPerTrack, CONFIGFILE);
  409. img.headsPerCylinder = ini_getl(section, "HeadsPerCylinder", img.headsPerCylinder, CONFIGFILE);
  410. img.quirks = ini_getl(section, "Quirks", img.quirks, CONFIGFILE);
  411. img.rightAlignStrings = ini_getbool(section, "RightAlignStrings", 0, CONFIGFILE);
  412. img.prefetchbytes = ini_getl(section, "PrefetchBytes", img.prefetchbytes, CONFIGFILE);
  413. img.reinsert_on_inquiry = ini_getbool(section, "ReinsertCDOnInquiry", 0, CONFIGFILE);
  414. char tmp[32];
  415. memset(tmp, 0, sizeof(tmp));
  416. ini_gets(section, "Vendor", "", tmp, sizeof(tmp), CONFIGFILE);
  417. if (tmp[0]) memcpy(img.vendor, tmp, sizeof(img.vendor));
  418. memset(tmp, 0, sizeof(tmp));
  419. ini_gets(section, "Product", "", tmp, sizeof(tmp), CONFIGFILE);
  420. if (tmp[0]) memcpy(img.prodId, tmp, sizeof(img.prodId));
  421. memset(tmp, 0, sizeof(tmp));
  422. ini_gets(section, "Version", "", tmp, sizeof(tmp), CONFIGFILE);
  423. if (tmp[0]) memcpy(img.revision, tmp, sizeof(img.revision));
  424. memset(tmp, 0, sizeof(tmp));
  425. ini_gets(section, "Serial", "", tmp, sizeof(tmp), CONFIGFILE);
  426. if (tmp[0]) memcpy(img.serial, tmp, sizeof(img.serial));
  427. }
  428. // Check if image file name is overridden in config
  429. bool scsiDiskGetImageNameFromConfig(image_config_t &img, char *buf, size_t buflen)
  430. {
  431. int target_idx = img.scsiId & 7;
  432. char section[6] = "SCSI0";
  433. section[4] = '0' + target_idx;
  434. char key[5] = "IMG0";
  435. key[3] = '0' + img.image_index;
  436. ini_gets(section, key, "", buf, buflen, CONFIGFILE);
  437. return buf[0] != '\0';
  438. }
  439. void scsiDiskLoadConfig(int target_idx)
  440. {
  441. char section[6] = "SCSI0";
  442. section[4] = '0' + target_idx;
  443. // Set default settings
  444. scsiDiskConfigDefaults(target_idx);
  445. // First load global settings
  446. scsiDiskLoadConfig(target_idx, "SCSI");
  447. // Then settings specific to target ID
  448. scsiDiskLoadConfig(target_idx, section);
  449. // Check if we have image specified by name
  450. char filename[MAX_FILE_PATH];
  451. image_config_t &img = g_DiskImages[target_idx];
  452. if (scsiDiskGetImageNameFromConfig(img, filename, sizeof(filename)))
  453. {
  454. int blocksize = (img.deviceType == S2S_CFG_OPTICAL) ? 2048 : 512;
  455. log("-- Opening ", filename, " for id:", target_idx, ", specified in " CONFIGFILE);
  456. scsiDiskOpenHDDImage(target_idx, filename, target_idx, 0, blocksize);
  457. }
  458. }
  459. bool scsiDiskCheckAnyImagesConfigured()
  460. {
  461. for (int i = 0; i < S2S_MAX_TARGETS; i++)
  462. {
  463. if (g_DiskImages[i].file.isOpen() && (g_DiskImages[i].scsiId & S2S_CFG_TARGET_ENABLED))
  464. {
  465. return true;
  466. }
  467. }
  468. return false;
  469. }
  470. image_config_t &scsiDiskGetImageConfig(int target_idx)
  471. {
  472. assert(target_idx >= 0 && target_idx < S2S_MAX_TARGETS);
  473. return g_DiskImages[target_idx];
  474. }
  475. /*******************************/
  476. /* Config handling for SCSI2SD */
  477. /*******************************/
  478. extern "C"
  479. void s2s_configInit(S2S_BoardCfg* config)
  480. {
  481. if (SD.exists(CONFIGFILE))
  482. {
  483. log("Reading configuration from " CONFIGFILE);
  484. }
  485. else
  486. {
  487. log("Config file " CONFIGFILE " not found, using defaults");
  488. }
  489. // Get default values from system preset, if any
  490. char presetName[32];
  491. ini_gets("SCSI", "System", "", presetName, sizeof(presetName), CONFIGFILE);
  492. preset_config_t defaults = getSystemPreset(presetName);
  493. if (defaults.presetName)
  494. {
  495. log("Active configuration (using system preset \"", defaults.presetName, "\"):");
  496. }
  497. else
  498. {
  499. log("Active configuration:");
  500. }
  501. memset(config, 0, sizeof(S2S_BoardCfg));
  502. memcpy(config->magic, "BCFG", 4);
  503. config->flags = 0;
  504. config->startupDelay = 0;
  505. config->selectionDelay = ini_getl("SCSI", "SelectionDelay", defaults.selectionDelay, CONFIGFILE);
  506. config->flags6 = 0;
  507. config->scsiSpeed = PLATFORM_MAX_SCSI_SPEED;
  508. int maxSyncSpeed = ini_getl("SCSI", "MaxSyncSpeed", defaults.maxSyncSpeed, CONFIGFILE);
  509. if (maxSyncSpeed < 5 && config->scsiSpeed > S2S_CFG_SPEED_ASYNC_50)
  510. config->scsiSpeed = S2S_CFG_SPEED_ASYNC_50;
  511. else if (maxSyncSpeed < 10 && config->scsiSpeed > S2S_CFG_SPEED_SYNC_5)
  512. config->scsiSpeed = S2S_CFG_SPEED_SYNC_5;
  513. if ((int)config->selectionDelay == defaults.selectionDelay)
  514. {
  515. debuglog("-- SelectionDelay: ", (int)config->selectionDelay);
  516. }
  517. else
  518. {
  519. log("-- SelectionDelay: ", (int)config->selectionDelay);
  520. }
  521. if (ini_getbool("SCSI", "EnableUnitAttention", defaults.enableUnitAttention, CONFIGFILE))
  522. {
  523. log("-- EnableUnitAttention is on");
  524. config->flags |= S2S_CFG_ENABLE_UNIT_ATTENTION;
  525. }
  526. else
  527. {
  528. debuglog("-- EnableUnitAttention is off");
  529. }
  530. if (ini_getbool("SCSI", "EnableSCSI2", defaults.enableSCSI2, CONFIGFILE))
  531. {
  532. debuglog("-- EnableSCSI2 is on");
  533. config->flags |= S2S_CFG_ENABLE_SCSI2;
  534. }
  535. else
  536. {
  537. log("-- EnableSCSI2 is off");
  538. }
  539. if (ini_getbool("SCSI", "EnableSelLatch", defaults.enableSelLatch, CONFIGFILE))
  540. {
  541. log("-- EnableSelLatch is on");
  542. config->flags |= S2S_CFG_ENABLE_SEL_LATCH;
  543. }
  544. else
  545. {
  546. debuglog("-- EnableSelLatch is off");
  547. }
  548. if (ini_getbool("SCSI", "MapLunsToIDs", defaults.mapLunsToIDs, CONFIGFILE))
  549. {
  550. log("-- MapLunsToIDs is on");
  551. config->flags |= S2S_CFG_MAP_LUNS_TO_IDS;
  552. }
  553. else
  554. {
  555. debuglog("-- MapLunsToIDs is off");
  556. }
  557. if (ini_getbool("SCSI", "Debug", 0, CONFIGFILE))
  558. {
  559. log("-- Debug is enabled");
  560. }
  561. if (ini_getbool("SCSI", "EnableParity", defaults.enableParity, CONFIGFILE))
  562. {
  563. debuglog("-- Parity is enabled");
  564. config->flags |= S2S_CFG_ENABLE_PARITY;
  565. }
  566. else
  567. {
  568. log("-- Parity is disabled");
  569. }
  570. if (ini_getbool("SCSI", "ReinsertCDOnInquiry", defaults.reinsertOnInquiry, CONFIGFILE))
  571. {
  572. log("-- ReinsertCDOnInquiry is enabled");
  573. }
  574. else
  575. {
  576. debuglog("-- ReinsertCDOnInquiry is disabled");
  577. }
  578. }
  579. extern "C"
  580. void s2s_debugInit(void)
  581. {
  582. }
  583. extern "C"
  584. void s2s_configPoll(void)
  585. {
  586. }
  587. extern "C"
  588. void s2s_configSave(int scsiId, uint16_t byesPerSector)
  589. {
  590. // Modification of config over SCSI bus is not implemented.
  591. }
  592. extern "C"
  593. const S2S_TargetCfg* s2s_getConfigByIndex(int index)
  594. {
  595. if (index < 0 || index >= S2S_MAX_TARGETS)
  596. {
  597. return NULL;
  598. }
  599. else
  600. {
  601. return &g_DiskImages[index];
  602. }
  603. }
  604. extern "C"
  605. const S2S_TargetCfg* s2s_getConfigById(int scsiId)
  606. {
  607. int i;
  608. for (i = 0; i < S2S_MAX_TARGETS; ++i)
  609. {
  610. const S2S_TargetCfg* tgt = s2s_getConfigByIndex(i);
  611. if ((tgt->scsiId & S2S_CFG_TARGET_ID_BITS) == scsiId &&
  612. (tgt->scsiId & S2S_CFG_TARGET_ENABLED))
  613. {
  614. return tgt;
  615. }
  616. }
  617. return NULL;
  618. }
  619. /**********************/
  620. /* FormatUnit command */
  621. /**********************/
  622. // Callback once all data has been read in the data out phase.
  623. static void doFormatUnitComplete(void)
  624. {
  625. scsiDev.phase = STATUS;
  626. }
  627. static void doFormatUnitSkipData(int bytes)
  628. {
  629. // We may not have enough memory to store the initialisation pattern and
  630. // defect list data. Since we're not making use of it yet anyway, just
  631. // discard the bytes.
  632. scsiEnterPhase(DATA_OUT);
  633. int i;
  634. for (i = 0; i < bytes; ++i)
  635. {
  636. scsiReadByte();
  637. }
  638. }
  639. // Callback from the data out phase.
  640. static void doFormatUnitPatternHeader(void)
  641. {
  642. int defectLength =
  643. ((((uint16_t)scsiDev.data[2])) << 8) +
  644. scsiDev.data[3];
  645. int patternLength =
  646. ((((uint16_t)scsiDev.data[4 + 2])) << 8) +
  647. scsiDev.data[4 + 3];
  648. doFormatUnitSkipData(defectLength + patternLength);
  649. doFormatUnitComplete();
  650. }
  651. // Callback from the data out phase.
  652. static void doFormatUnitHeader(void)
  653. {
  654. int IP = (scsiDev.data[1] & 0x08) ? 1 : 0;
  655. int DSP = (scsiDev.data[1] & 0x04) ? 1 : 0;
  656. if (! DSP) // disable save parameters
  657. {
  658. // Save the "MODE SELECT savable parameters"
  659. s2s_configSave(
  660. scsiDev.target->targetId,
  661. scsiDev.target->liveCfg.bytesPerSector);
  662. }
  663. if (IP)
  664. {
  665. // We need to read the initialisation pattern header first.
  666. scsiDev.dataLen += 4;
  667. scsiDev.phase = DATA_OUT;
  668. scsiDev.postDataOutHook = doFormatUnitPatternHeader;
  669. }
  670. else
  671. {
  672. // Read the defect list data
  673. int defectLength =
  674. ((((uint16_t)scsiDev.data[2])) << 8) +
  675. scsiDev.data[3];
  676. doFormatUnitSkipData(defectLength);
  677. doFormatUnitComplete();
  678. }
  679. }
  680. /************************/
  681. /* ReadCapacity command */
  682. /************************/
  683. static void doReadCapacity()
  684. {
  685. uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
  686. (((uint32_t) scsiDev.cdb[3]) << 16) +
  687. (((uint32_t) scsiDev.cdb[4]) << 8) +
  688. scsiDev.cdb[5];
  689. int pmi = scsiDev.cdb[8] & 1;
  690. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  691. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  692. uint32_t capacity = img.file.size() / bytesPerSector;
  693. if (!pmi && lba)
  694. {
  695. // error.
  696. // We don't do anything with the "partial medium indicator", and
  697. // assume that delays are constant across each block. But the spec
  698. // says we must return this error if pmi is specified incorrectly.
  699. scsiDev.status = CHECK_CONDITION;
  700. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  701. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  702. scsiDev.phase = STATUS;
  703. }
  704. else if (capacity > 0)
  705. {
  706. uint32_t highestBlock = capacity - 1;
  707. scsiDev.data[0] = highestBlock >> 24;
  708. scsiDev.data[1] = highestBlock >> 16;
  709. scsiDev.data[2] = highestBlock >> 8;
  710. scsiDev.data[3] = highestBlock;
  711. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  712. scsiDev.data[4] = bytesPerSector >> 24;
  713. scsiDev.data[5] = bytesPerSector >> 16;
  714. scsiDev.data[6] = bytesPerSector >> 8;
  715. scsiDev.data[7] = bytesPerSector;
  716. scsiDev.dataLen = 8;
  717. scsiDev.phase = DATA_IN;
  718. }
  719. else
  720. {
  721. scsiDev.status = CHECK_CONDITION;
  722. scsiDev.target->sense.code = NOT_READY;
  723. scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
  724. scsiDev.phase = STATUS;
  725. }
  726. }
  727. /*************************/
  728. /* TestUnitReady command */
  729. /*************************/
  730. static int doTestUnitReady()
  731. {
  732. int ready = 1;
  733. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  734. if (unlikely(!scsiDev.target->started || !img.file.isOpen()))
  735. {
  736. ready = 0;
  737. scsiDev.status = CHECK_CONDITION;
  738. scsiDev.target->sense.code = NOT_READY;
  739. scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_READY_INITIALIZING_COMMAND_REQUIRED;
  740. scsiDev.phase = STATUS;
  741. }
  742. else if (img.ejected)
  743. {
  744. ready = 0;
  745. scsiDev.status = CHECK_CONDITION;
  746. scsiDev.target->sense.code = NOT_READY;
  747. scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
  748. scsiDev.phase = STATUS;
  749. // We are now reporting to host that the drive is open.
  750. // Simulate a "close" for next time the host polls.
  751. cdromSwitchNextImage(img);
  752. }
  753. else if (unlikely(!(blockDev.state & DISK_PRESENT)))
  754. {
  755. ready = 0;
  756. scsiDev.status = CHECK_CONDITION;
  757. scsiDev.target->sense.code = NOT_READY;
  758. scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
  759. scsiDev.phase = STATUS;
  760. }
  761. else if (unlikely(!(blockDev.state & DISK_INITIALISED)))
  762. {
  763. ready = 0;
  764. scsiDev.status = CHECK_CONDITION;
  765. scsiDev.target->sense.code = NOT_READY;
  766. scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE;
  767. scsiDev.phase = STATUS;
  768. }
  769. return ready;
  770. }
  771. /****************/
  772. /* Seek command */
  773. /****************/
  774. static void doSeek(uint32_t lba)
  775. {
  776. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  777. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  778. uint32_t capacity = img.file.size() / bytesPerSector;
  779. if (lba >= capacity)
  780. {
  781. scsiDev.status = CHECK_CONDITION;
  782. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  783. scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  784. scsiDev.phase = STATUS;
  785. }
  786. else
  787. {
  788. if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB) ||
  789. scsiDev.compatMode < COMPAT_SCSI2)
  790. {
  791. s2s_delay_ms(10);
  792. }
  793. else
  794. {
  795. s2s_delay_us(10);
  796. }
  797. }
  798. }
  799. /********************************************/
  800. /* Transfer state for read / write commands */
  801. /********************************************/
  802. BlockDevice blockDev = {DISK_PRESENT | DISK_INITIALISED};
  803. Transfer transfer;
  804. static struct {
  805. uint8_t *buffer;
  806. uint32_t bytes_sd; // Number of bytes that have been scheduled for transfer on SD card side
  807. uint32_t bytes_scsi; // Number of bytes that have been scheduled for transfer on SCSI side
  808. uint32_t bytes_scsi_started;
  809. uint32_t sd_transfer_start;
  810. int parityError;
  811. } g_disk_transfer;
  812. #ifdef PREFETCH_BUFFER_SIZE
  813. static struct {
  814. uint8_t buffer[PREFETCH_BUFFER_SIZE];
  815. uint32_t sector;
  816. uint32_t bytes;
  817. uint8_t scsiId;
  818. } g_scsi_prefetch;
  819. #endif
  820. /*****************/
  821. /* Write command */
  822. /*****************/
  823. void scsiDiskStartWrite(uint32_t lba, uint32_t blocks)
  824. {
  825. if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB)) {
  826. // Floppies are supposed to be slow. Some systems can't handle a floppy
  827. // without an access time
  828. s2s_delay_ms(10);
  829. }
  830. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  831. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  832. uint32_t capacity = img.file.size() / bytesPerSector;
  833. debuglog("------ Write ", (int)blocks, "x", (int)bytesPerSector, " starting at ", (int)lba);
  834. if (unlikely(blockDev.state & DISK_WP) ||
  835. unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_OPTICAL) ||
  836. unlikely(!img.file.isWritable()))
  837. {
  838. log("WARNING: Host attempted write to read-only drive ID ", (int)(img.scsiId & S2S_CFG_TARGET_ID_BITS));
  839. scsiDev.status = CHECK_CONDITION;
  840. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  841. scsiDev.target->sense.asc = WRITE_PROTECTED;
  842. scsiDev.phase = STATUS;
  843. }
  844. else if (unlikely(((uint64_t) lba) + blocks > capacity))
  845. {
  846. log("WARNING: Host attempted write at sector ", (int)lba, "+", (int)blocks,
  847. ", exceeding image size ", (int)capacity, " sectors (",
  848. (int)bytesPerSector, "B/sector)");
  849. scsiDev.status = CHECK_CONDITION;
  850. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  851. scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  852. scsiDev.phase = STATUS;
  853. }
  854. else
  855. {
  856. transfer.multiBlock = true;
  857. transfer.lba = lba;
  858. transfer.blocks = blocks;
  859. transfer.currentBlock = 0;
  860. scsiDev.phase = DATA_OUT;
  861. scsiDev.dataLen = 0;
  862. scsiDev.dataPtr = 0;
  863. #ifdef PREFETCH_BUFFER_SIZE
  864. // Invalidate prefetch buffer
  865. g_scsi_prefetch.bytes = 0;
  866. g_scsi_prefetch.sector = 0;
  867. #endif
  868. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  869. if (!img.file.seek((uint64_t)transfer.lba * bytesPerSector))
  870. {
  871. log("Seek to ", transfer.lba, " failed for SCSI ID", (int)scsiDev.target->targetId);
  872. scsiDev.status = CHECK_CONDITION;
  873. scsiDev.target->sense.code = MEDIUM_ERROR;
  874. scsiDev.target->sense.asc = NO_SEEK_COMPLETE;
  875. scsiDev.phase = STATUS;
  876. }
  877. }
  878. }
  879. // Called to transfer next block from SCSI bus.
  880. // Usually called from SD card driver during waiting for SD card access.
  881. void diskDataOut_callback(uint32_t bytes_complete)
  882. {
  883. // For best performance, do SCSI reads in blocks of 4 or more bytes
  884. bytes_complete &= ~3;
  885. if (g_disk_transfer.bytes_scsi_started < g_disk_transfer.bytes_scsi)
  886. {
  887. // How many bytes remaining in the transfer?
  888. uint32_t remain = g_disk_transfer.bytes_scsi - g_disk_transfer.bytes_scsi_started;
  889. uint32_t len = remain;
  890. // Split read so that it doesn't wrap around buffer edge
  891. uint32_t bufsize = sizeof(scsiDev.data);
  892. uint32_t start = (g_disk_transfer.bytes_scsi_started % bufsize);
  893. if (start + len > bufsize)
  894. len = bufsize - start;
  895. // Apply platform-specific optimized transfer sizes
  896. if (len > PLATFORM_OPTIMAL_SCSI_READ_BLOCK_SIZE)
  897. {
  898. len = PLATFORM_OPTIMAL_SCSI_READ_BLOCK_SIZE;
  899. }
  900. // Don't overwrite data that has not yet been written to SD card
  901. uint32_t sd_ready_cnt = g_disk_transfer.bytes_sd + bytes_complete;
  902. if (g_disk_transfer.bytes_scsi_started + len > sd_ready_cnt + bufsize)
  903. len = sd_ready_cnt + bufsize - g_disk_transfer.bytes_scsi_started;
  904. // Keep transfers a multiple of sector size.
  905. // Macintosh SCSI driver seems to get confused if we have a delay
  906. // in middle of a sector.
  907. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  908. if (remain >= bytesPerSector && len % bytesPerSector != 0)
  909. {
  910. len -= len % bytesPerSector;
  911. }
  912. if (len == 0)
  913. return;
  914. // debuglog("SCSI read ", (int)start, " + ", (int)len);
  915. scsiStartRead(&scsiDev.data[start], len, &g_disk_transfer.parityError);
  916. g_disk_transfer.bytes_scsi_started += len;
  917. }
  918. }
  919. void diskDataOut()
  920. {
  921. scsiEnterPhase(DATA_OUT);
  922. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  923. uint32_t blockcount = (transfer.blocks - transfer.currentBlock);
  924. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  925. g_disk_transfer.buffer = scsiDev.data;
  926. g_disk_transfer.bytes_scsi = blockcount * bytesPerSector;
  927. g_disk_transfer.bytes_sd = 0;
  928. g_disk_transfer.bytes_scsi_started = 0;
  929. g_disk_transfer.sd_transfer_start = 0;
  930. g_disk_transfer.parityError = 0;
  931. while (g_disk_transfer.bytes_sd < g_disk_transfer.bytes_scsi
  932. && scsiDev.phase == DATA_OUT
  933. && !scsiDev.resetFlag)
  934. {
  935. platform_poll();
  936. // Figure out how many contiguous bytes are available for writing to SD card.
  937. uint32_t bufsize = sizeof(scsiDev.data);
  938. uint32_t start = g_disk_transfer.bytes_sd % bufsize;
  939. uint32_t len = 0;
  940. // How much data until buffer edge wrap?
  941. uint32_t available = g_disk_transfer.bytes_scsi_started - g_disk_transfer.bytes_sd;
  942. if (start + available > bufsize)
  943. available = bufsize - start;
  944. // Count number of finished sectors
  945. if (scsiIsReadFinished(&scsiDev.data[start + available - 1]))
  946. {
  947. len = available;
  948. }
  949. else
  950. {
  951. while (len < available && scsiIsReadFinished(&scsiDev.data[start + len + SD_SECTOR_SIZE - 1]))
  952. {
  953. len += SD_SECTOR_SIZE;
  954. }
  955. }
  956. // In case the last sector is partial (256 byte SCSI sectors)
  957. if (len > available)
  958. {
  959. len = available;
  960. }
  961. // Apply platform-specific write size blocks for optimization
  962. if (len > PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE)
  963. {
  964. len = PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE;
  965. }
  966. uint32_t remain_in_transfer = g_disk_transfer.bytes_scsi - g_disk_transfer.bytes_sd;
  967. if (len < bufsize - start && len < remain_in_transfer)
  968. {
  969. // Use large write blocks in middle of transfer and smaller at the end of transfer.
  970. // This improves performance for large writes and reduces latency at end of request.
  971. uint32_t min_write_size = PLATFORM_OPTIMAL_MIN_SD_WRITE_SIZE;
  972. if (remain_in_transfer <= PLATFORM_OPTIMAL_MAX_SD_WRITE_SIZE)
  973. {
  974. min_write_size = PLATFORM_OPTIMAL_LAST_SD_WRITE_SIZE;
  975. }
  976. if (len < min_write_size)
  977. {
  978. len = 0;
  979. }
  980. }
  981. if (len == 0)
  982. {
  983. // Nothing ready to transfer, check if we can read more from SCSI bus
  984. diskDataOut_callback(0);
  985. }
  986. else
  987. {
  988. // Finalize transfer on SCSI side
  989. scsiFinishRead(&scsiDev.data[start], len, &g_disk_transfer.parityError);
  990. // Check parity error status before writing to SD card
  991. if (g_disk_transfer.parityError && (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY))
  992. {
  993. scsiDev.status = CHECK_CONDITION;
  994. scsiDev.target->sense.code = ABORTED_COMMAND;
  995. scsiDev.target->sense.asc = SCSI_PARITY_ERROR;
  996. scsiDev.phase = STATUS;
  997. break;
  998. }
  999. // Start writing to SD card and simultaneously start new SCSI transfers
  1000. // when buffer space is freed.
  1001. uint8_t *buf = &scsiDev.data[start];
  1002. g_disk_transfer.sd_transfer_start = start;
  1003. // debuglog("SD write ", (int)start, " + ", (int)len, " ", bytearray(buf, len));
  1004. platform_set_sd_callback(&diskDataOut_callback, buf);
  1005. if (img.file.write(buf, len) != len)
  1006. {
  1007. log("SD card write failed: ", SD.sdErrorCode());
  1008. scsiDev.status = CHECK_CONDITION;
  1009. scsiDev.target->sense.code = MEDIUM_ERROR;
  1010. scsiDev.target->sense.asc = WRITE_ERROR_AUTO_REALLOCATION_FAILED;
  1011. scsiDev.phase = STATUS;
  1012. }
  1013. platform_set_sd_callback(NULL, NULL);
  1014. g_disk_transfer.bytes_sd += len;
  1015. }
  1016. }
  1017. // Release SCSI bus
  1018. scsiFinishRead(NULL, 0, &g_disk_transfer.parityError);
  1019. transfer.currentBlock += blockcount;
  1020. scsiDev.dataPtr = scsiDev.dataLen = 0;
  1021. if (transfer.currentBlock == transfer.blocks)
  1022. {
  1023. // Verify that all data has been flushed to disk from SdFat cache.
  1024. // Normally does nothing as we do not change image file size and
  1025. // data writes are not cached.
  1026. img.file.flush();
  1027. }
  1028. }
  1029. /*****************/
  1030. /* Read command */
  1031. /*****************/
  1032. void scsiDiskStartRead(uint32_t lba, uint32_t blocks)
  1033. {
  1034. if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB)) {
  1035. // Floppies are supposed to be slow. Some systems can't handle a floppy
  1036. // without an access time
  1037. s2s_delay_ms(10);
  1038. }
  1039. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1040. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  1041. uint32_t capacity = img.file.size() / bytesPerSector;
  1042. debuglog("------ Read ", (int)blocks, "x", (int)bytesPerSector, " starting at ", (int)lba);
  1043. if (unlikely(((uint64_t) lba) + blocks > capacity))
  1044. {
  1045. log("WARNING: Host attempted read at sector ", (int)lba, "+", (int)blocks,
  1046. ", exceeding image size ", (int)capacity, " sectors (",
  1047. (int)bytesPerSector, "B/sector)");
  1048. scsiDev.status = CHECK_CONDITION;
  1049. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1050. scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1051. scsiDev.phase = STATUS;
  1052. }
  1053. else
  1054. {
  1055. transfer.multiBlock = 1;
  1056. transfer.lba = lba;
  1057. transfer.blocks = blocks;
  1058. transfer.currentBlock = 0;
  1059. scsiDev.phase = DATA_IN;
  1060. scsiDev.dataLen = 0;
  1061. scsiDev.dataPtr = 0;
  1062. #ifdef PREFETCH_BUFFER_SIZE
  1063. uint32_t sectors_in_prefetch = g_scsi_prefetch.bytes / bytesPerSector;
  1064. if (img.scsiId == g_scsi_prefetch.scsiId &&
  1065. transfer.lba >= g_scsi_prefetch.sector &&
  1066. transfer.lba < g_scsi_prefetch.sector + sectors_in_prefetch)
  1067. {
  1068. // We have the some sectors already in prefetch cache
  1069. scsiEnterPhase(DATA_IN);
  1070. uint32_t start_offset = transfer.lba - g_scsi_prefetch.sector;
  1071. uint32_t count = sectors_in_prefetch - start_offset;
  1072. if (count > transfer.blocks) count = transfer.blocks;
  1073. scsiStartWrite(g_scsi_prefetch.buffer + start_offset * bytesPerSector, count * bytesPerSector);
  1074. debuglog("------ Found ", (int)count, " sectors in prefetch cache");
  1075. transfer.currentBlock += count;
  1076. }
  1077. if (transfer.currentBlock == transfer.blocks)
  1078. {
  1079. while (!scsiIsWriteFinished(NULL))
  1080. {
  1081. platform_poll();
  1082. }
  1083. scsiFinishWrite();
  1084. }
  1085. #endif
  1086. if (!img.file.seek((uint64_t)(transfer.lba + transfer.currentBlock) * bytesPerSector))
  1087. {
  1088. log("Seek to ", transfer.lba, " failed for SCSI ID", (int)scsiDev.target->targetId);
  1089. scsiDev.status = CHECK_CONDITION;
  1090. scsiDev.target->sense.code = MEDIUM_ERROR;
  1091. scsiDev.target->sense.asc = NO_SEEK_COMPLETE;
  1092. scsiDev.phase = STATUS;
  1093. }
  1094. }
  1095. }
  1096. void diskDataIn_callback(uint32_t bytes_complete)
  1097. {
  1098. // On SCSI-1 devices the phase change has some extra delays.
  1099. // Doing it here lets the SD card transfer proceed in background.
  1100. scsiEnterPhase(DATA_IN);
  1101. // For best performance, do writes in blocks of 4 or more bytes
  1102. if (bytes_complete < g_disk_transfer.bytes_sd)
  1103. {
  1104. bytes_complete &= ~3;
  1105. }
  1106. // Machintosh SCSI driver can get confused if pauses occur in middle of
  1107. // a sector, so schedule the transfers in sector sized blocks.
  1108. if (bytes_complete < g_disk_transfer.bytes_sd)
  1109. {
  1110. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  1111. if (bytes_complete % bytesPerSector != 0)
  1112. {
  1113. bytes_complete -= bytes_complete % bytesPerSector;
  1114. }
  1115. }
  1116. if (bytes_complete > g_disk_transfer.bytes_scsi)
  1117. {
  1118. // DMA is reading from SD card, bytes_complete bytes have already been read.
  1119. // Send them to SCSI bus now.
  1120. uint32_t len = bytes_complete - g_disk_transfer.bytes_scsi;
  1121. scsiStartWrite(g_disk_transfer.buffer + g_disk_transfer.bytes_scsi, len);
  1122. g_disk_transfer.bytes_scsi += len;
  1123. }
  1124. // Provide a chance for polling request processing
  1125. scsiIsWriteFinished(NULL);
  1126. }
  1127. // Start a data in transfer using given temporary buffer.
  1128. // diskDataIn() below divides the scsiDev.data buffer to two halves for double buffering.
  1129. static void start_dataInTransfer(uint8_t *buffer, uint32_t count)
  1130. {
  1131. g_disk_transfer.buffer = buffer;
  1132. g_disk_transfer.bytes_scsi = 0;
  1133. g_disk_transfer.bytes_sd = count;
  1134. // Verify that previous write using this buffer has finished
  1135. uint32_t start = millis();
  1136. while (!scsiIsWriteFinished(buffer + count - 1) && !scsiDev.resetFlag)
  1137. {
  1138. if ((uint32_t)(millis() - start) > 5000)
  1139. {
  1140. log("start_dataInTransfer() timeout waiting for previous to finish");
  1141. scsiDev.resetFlag = 1;
  1142. }
  1143. platform_poll();
  1144. }
  1145. if (scsiDev.resetFlag) return;
  1146. // Start transferring from SD card
  1147. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1148. platform_set_sd_callback(&diskDataIn_callback, buffer);
  1149. if (img.file.read(buffer, count) != count)
  1150. {
  1151. log("SD card read failed: ", SD.sdErrorCode());
  1152. scsiDev.status = CHECK_CONDITION;
  1153. scsiDev.target->sense.code = MEDIUM_ERROR;
  1154. scsiDev.target->sense.asc = UNRECOVERED_READ_ERROR;
  1155. scsiDev.phase = STATUS;
  1156. }
  1157. diskDataIn_callback(count);
  1158. platform_set_sd_callback(NULL, NULL);
  1159. platform_poll();
  1160. }
  1161. static void diskDataIn()
  1162. {
  1163. // Figure out how many blocks we can fit in buffer
  1164. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  1165. uint32_t maxblocks = sizeof(scsiDev.data) / bytesPerSector;
  1166. uint32_t maxblocks_half = maxblocks / 2;
  1167. // Start transfer in first half of buffer
  1168. // Waits for the previous first half transfer to finish first.
  1169. uint32_t remain = (transfer.blocks - transfer.currentBlock);
  1170. if (remain > 0)
  1171. {
  1172. uint32_t transfer_blocks = std::min(remain, maxblocks_half);
  1173. uint32_t transfer_bytes = transfer_blocks * bytesPerSector;
  1174. start_dataInTransfer(&scsiDev.data[0], transfer_bytes);
  1175. transfer.currentBlock += transfer_blocks;
  1176. }
  1177. // Start transfer in second half of buffer
  1178. // Waits for the previous second half transfer to finish first
  1179. remain = (transfer.blocks - transfer.currentBlock);
  1180. if (remain > 0)
  1181. {
  1182. uint32_t transfer_blocks = std::min(remain, maxblocks_half);
  1183. uint32_t transfer_bytes = transfer_blocks * bytesPerSector;
  1184. start_dataInTransfer(&scsiDev.data[maxblocks_half * bytesPerSector], transfer_bytes);
  1185. transfer.currentBlock += transfer_blocks;
  1186. }
  1187. if (transfer.currentBlock == transfer.blocks)
  1188. {
  1189. // This was the last block, verify that everything finishes
  1190. #ifdef PREFETCH_BUFFER_SIZE
  1191. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1192. int prefetchbytes = img.prefetchbytes;
  1193. if (prefetchbytes > PREFETCH_BUFFER_SIZE) prefetchbytes = PREFETCH_BUFFER_SIZE;
  1194. uint32_t prefetch_sectors = prefetchbytes / bytesPerSector;
  1195. uint32_t img_sector_count = img.file.size() / bytesPerSector;
  1196. g_scsi_prefetch.sector = transfer.lba + transfer.blocks;
  1197. g_scsi_prefetch.bytes = 0;
  1198. g_scsi_prefetch.scsiId = scsiDev.target->cfg->scsiId;
  1199. if (g_scsi_prefetch.sector + prefetch_sectors > img_sector_count)
  1200. {
  1201. // Don't try to read past image end.
  1202. prefetch_sectors = img_sector_count - g_scsi_prefetch.sector;
  1203. }
  1204. while (!scsiIsWriteFinished(NULL) && prefetch_sectors > 0 && !scsiDev.resetFlag)
  1205. {
  1206. platform_poll();
  1207. // Check if prefetch buffer is free
  1208. g_disk_transfer.buffer = g_scsi_prefetch.buffer + g_scsi_prefetch.bytes;
  1209. if (!scsiIsWriteFinished(g_disk_transfer.buffer) ||
  1210. !scsiIsWriteFinished(g_disk_transfer.buffer + bytesPerSector - 1))
  1211. {
  1212. continue;
  1213. }
  1214. // We still have time, prefetch next sectors in case this SCSI request
  1215. // is part of a longer linear read.
  1216. g_disk_transfer.bytes_sd = bytesPerSector;
  1217. g_disk_transfer.bytes_scsi = bytesPerSector; // Tell callback not to send to SCSI
  1218. platform_set_sd_callback(&diskDataIn_callback, g_disk_transfer.buffer);
  1219. int status = img.file.read(g_disk_transfer.buffer, bytesPerSector);
  1220. if (status <= 0)
  1221. {
  1222. log("Prefetch read failed");
  1223. prefetch_sectors = 0;
  1224. break;
  1225. }
  1226. g_scsi_prefetch.bytes += status;
  1227. platform_set_sd_callback(NULL, NULL);
  1228. prefetch_sectors--;
  1229. }
  1230. #endif
  1231. while (!scsiIsWriteFinished(NULL))
  1232. {
  1233. platform_poll();
  1234. }
  1235. scsiFinishWrite();
  1236. }
  1237. }
  1238. /********************/
  1239. /* Command dispatch */
  1240. /********************/
  1241. // Handle direct-access scsi device commands
  1242. extern "C"
  1243. int scsiDiskCommand()
  1244. {
  1245. int commandHandled = 1;
  1246. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1247. uint8_t command = scsiDev.cdb[0];
  1248. if (unlikely(command == 0x1B))
  1249. {
  1250. // START STOP UNIT
  1251. // Enable or disable media access operations.
  1252. //int immed = scsiDev.cdb[1] & 1;
  1253. int start = scsiDev.cdb[4] & 1;
  1254. if (start)
  1255. {
  1256. scsiDev.target->started = 1;
  1257. }
  1258. else
  1259. {
  1260. scsiDev.target->started = 0;
  1261. }
  1262. }
  1263. else if (unlikely(command == 0x00))
  1264. {
  1265. // TEST UNIT READY
  1266. doTestUnitReady();
  1267. }
  1268. else if (unlikely(!doTestUnitReady()))
  1269. {
  1270. // Status and sense codes already set by doTestUnitReady
  1271. }
  1272. else if (likely(command == 0x08))
  1273. {
  1274. // READ(6)
  1275. uint32_t lba =
  1276. (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
  1277. (((uint32_t) scsiDev.cdb[2]) << 8) +
  1278. scsiDev.cdb[3];
  1279. uint32_t blocks = scsiDev.cdb[4];
  1280. if (unlikely(blocks == 0)) blocks = 256;
  1281. scsiDiskStartRead(lba, blocks);
  1282. }
  1283. else if (likely(command == 0x28))
  1284. {
  1285. // READ(10)
  1286. // Ignore all cache control bits - we don't support a memory cache.
  1287. uint32_t lba =
  1288. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1289. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1290. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1291. scsiDev.cdb[5];
  1292. uint32_t blocks =
  1293. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1294. scsiDev.cdb[8];
  1295. scsiDiskStartRead(lba, blocks);
  1296. }
  1297. else if (likely(command == 0x0A))
  1298. {
  1299. // WRITE(6)
  1300. uint32_t lba =
  1301. (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
  1302. (((uint32_t) scsiDev.cdb[2]) << 8) +
  1303. scsiDev.cdb[3];
  1304. uint32_t blocks = scsiDev.cdb[4];
  1305. if (unlikely(blocks == 0)) blocks = 256;
  1306. scsiDiskStartWrite(lba, blocks);
  1307. }
  1308. else if (likely(command == 0x2A) || // WRITE(10)
  1309. unlikely(command == 0x2E)) // WRITE AND VERIFY
  1310. {
  1311. // Ignore all cache control bits - we don't support a memory cache.
  1312. // Don't bother verifying either. The SD card likely stores ECC
  1313. // along with each flash row.
  1314. uint32_t lba =
  1315. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1316. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1317. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1318. scsiDev.cdb[5];
  1319. uint32_t blocks =
  1320. (((uint32_t) scsiDev.cdb[7]) << 8) +
  1321. scsiDev.cdb[8];
  1322. scsiDiskStartWrite(lba, blocks);
  1323. }
  1324. else if (unlikely(command == 0x04))
  1325. {
  1326. // FORMAT UNIT
  1327. // We don't really do any formatting, but we need to read the correct
  1328. // number of bytes in the DATA_OUT phase to make the SCSI host happy.
  1329. int fmtData = (scsiDev.cdb[1] & 0x10) ? 1 : 0;
  1330. if (fmtData)
  1331. {
  1332. // We need to read the parameter list, but we don't know how
  1333. // big it is yet. Start with the header.
  1334. scsiDev.dataLen = 4;
  1335. scsiDev.phase = DATA_OUT;
  1336. scsiDev.postDataOutHook = doFormatUnitHeader;
  1337. }
  1338. else
  1339. {
  1340. // No data to read, we're already finished!
  1341. }
  1342. }
  1343. else if (unlikely(command == 0x25))
  1344. {
  1345. // READ CAPACITY
  1346. doReadCapacity();
  1347. }
  1348. else if (unlikely(command == 0x0B))
  1349. {
  1350. // SEEK(6)
  1351. uint32_t lba =
  1352. (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
  1353. (((uint32_t) scsiDev.cdb[2]) << 8) +
  1354. scsiDev.cdb[3];
  1355. doSeek(lba);
  1356. }
  1357. else if (unlikely(command == 0x2B))
  1358. {
  1359. // SEEK(10)
  1360. uint32_t lba =
  1361. (((uint32_t) scsiDev.cdb[2]) << 24) +
  1362. (((uint32_t) scsiDev.cdb[3]) << 16) +
  1363. (((uint32_t) scsiDev.cdb[4]) << 8) +
  1364. scsiDev.cdb[5];
  1365. doSeek(lba);
  1366. }
  1367. else if (unlikely(command == 0x36))
  1368. {
  1369. // LOCK UNLOCK CACHE
  1370. // We don't have a cache to lock data into. do nothing.
  1371. }
  1372. else if (unlikely(command == 0x34))
  1373. {
  1374. // PRE-FETCH.
  1375. // We don't have a cache to pre-fetch into. do nothing.
  1376. }
  1377. else if (unlikely(command == 0x1E))
  1378. {
  1379. // PREVENT ALLOW MEDIUM REMOVAL
  1380. // Not much we can do to prevent the user removing the SD card.
  1381. // do nothing.
  1382. }
  1383. else if (unlikely(command == 0x01))
  1384. {
  1385. // REZERO UNIT
  1386. // Set the lun to a vendor-specific state. Ignore.
  1387. }
  1388. else if (unlikely(command == 0x35))
  1389. {
  1390. // SYNCHRONIZE CACHE
  1391. // We don't have a cache. do nothing.
  1392. }
  1393. else if (unlikely(command == 0x2F))
  1394. {
  1395. // VERIFY
  1396. // TODO: When they supply data to verify, we should read the data and
  1397. // verify it. If they don't supply any data, just say success.
  1398. if ((scsiDev.cdb[1] & 0x02) == 0)
  1399. {
  1400. // They are asking us to do a medium verification with no data
  1401. // comparison. Assume success, do nothing.
  1402. }
  1403. else
  1404. {
  1405. // TODO. This means they are supplying data to verify against.
  1406. // Technically we should probably grab the data and compare it.
  1407. scsiDev.status = CHECK_CONDITION;
  1408. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  1409. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  1410. scsiDev.phase = STATUS;
  1411. }
  1412. }
  1413. else if (unlikely(command == 0x37))
  1414. {
  1415. // READ DEFECT DATA
  1416. uint32_t allocLength = (((uint16_t)scsiDev.cdb[7]) << 8) |
  1417. scsiDev.cdb[8];
  1418. scsiDev.data[0] = 0;
  1419. scsiDev.data[1] = scsiDev.cdb[1];
  1420. scsiDev.data[2] = 0;
  1421. scsiDev.data[3] = 0;
  1422. scsiDev.dataLen = 4;
  1423. if (scsiDev.dataLen > allocLength)
  1424. {
  1425. scsiDev.dataLen = allocLength;
  1426. }
  1427. scsiDev.phase = DATA_IN;
  1428. }
  1429. else if (img.file.isRom())
  1430. {
  1431. // Special handling for ROM drive to make SCSI2SD code report it as read-only
  1432. blockDev.state |= DISK_WP;
  1433. commandHandled = scsiModeCommand();
  1434. blockDev.state &= ~DISK_WP;
  1435. }
  1436. else
  1437. {
  1438. commandHandled = 0;
  1439. }
  1440. return commandHandled;
  1441. }
  1442. extern "C"
  1443. void scsiDiskPoll()
  1444. {
  1445. if (scsiDev.phase == DATA_IN &&
  1446. transfer.currentBlock != transfer.blocks)
  1447. {
  1448. diskDataIn();
  1449. }
  1450. else if (scsiDev.phase == DATA_OUT &&
  1451. transfer.currentBlock != transfer.blocks)
  1452. {
  1453. diskDataOut();
  1454. }
  1455. if (scsiDev.phase == STATUS && scsiDev.target)
  1456. {
  1457. // Check if the command is affected by drive geometry.
  1458. // Affected commands are:
  1459. // 0x1A MODE SENSE command of pages 0x03 (device format), 0x04 (disk geometry) or 0x3F (all pages)
  1460. // 0x1C RECEIVE DIAGNOSTICS RESULTS
  1461. uint8_t command = scsiDev.cdb[0];
  1462. uint8_t pageCode = scsiDev.cdb[2] & 0x3F;
  1463. if ((command == 0x1A && (pageCode == 0x03 || pageCode == 0x04 || pageCode == 0x3F)) ||
  1464. command == 0x1C)
  1465. {
  1466. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1467. checkDiskGeometryDivisible(img);
  1468. }
  1469. // Check for Inquiry command to reinsert CD-ROMs on boot
  1470. if (command == 0x12)
  1471. {
  1472. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  1473. if (img.deviceType == S2S_CFG_OPTICAL && img.reinsert_on_inquiry)
  1474. {
  1475. cdromReinsertFirstImage(img);
  1476. }
  1477. }
  1478. }
  1479. }
  1480. extern "C"
  1481. void scsiDiskReset()
  1482. {
  1483. scsiDev.dataPtr = 0;
  1484. scsiDev.savedDataPtr = 0;
  1485. scsiDev.dataLen = 0;
  1486. // transfer.lba = 0; // Needed in Request Sense to determine failure
  1487. transfer.blocks = 0;
  1488. transfer.currentBlock = 0;
  1489. transfer.multiBlock = 0;
  1490. #ifdef PREFETCH_BUFFER_SIZE
  1491. g_scsi_prefetch.bytes = 0;
  1492. g_scsi_prefetch.sector = 0;
  1493. #endif
  1494. // Reinsert any ejected CD-ROMs
  1495. for (int i = 0; i < S2S_MAX_TARGETS; ++i)
  1496. {
  1497. image_config_t &img = g_DiskImages[i];
  1498. if (img.deviceType == S2S_CFG_OPTICAL)
  1499. {
  1500. cdromReinsertFirstImage(img);
  1501. }
  1502. }
  1503. }
  1504. extern "C"
  1505. void scsiDiskInit()
  1506. {
  1507. scsiDiskReset();
  1508. }