ZuluSCSI_settings.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2023 Rabbit Hole Computing™
  3. * Copyright (c) 2023 Eric Helgeson
  4. *
  5. * This file is licensed under the GPL version 3 or any later version.  
  6. *
  7. * https://www.gnu.org/licenses/gpl-3.0.html
  8. * ----
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version. 
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. * GNU General Public License for more details. 
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  21. **/
  22. #include "ZuluSCSI_disk.h"
  23. #include "ZuluSCSI_audio.h"
  24. #include "ZuluSCSI_log.h"
  25. #include "ZuluSCSI_config.h"
  26. #include "ZuluSCSI_settings.h"
  27. #include "ZuluSCSI_platform.h"
  28. #include <strings.h>
  29. #include <minIni.h>
  30. #include <minIni_cache.h>
  31. // SCSI system and device settings
  32. ZuluSCSISettings g_scsi_settings;
  33. const char *systemPresetName[] = {"", "Mac", "MacPlus", "MPC3000", "MegaSTE", "X68000"};
  34. const char *devicePresetName[] = {"", "ST32430N"};
  35. // must be in the same order as zuluscsi_speed_grade_t in ZuluSCSI_settings.h
  36. const char * const speed_grade_strings[] =
  37. {
  38. "Default",
  39. "TurboMax",
  40. "Custom",
  41. "AudioSPDIF",
  42. "AudioI2S",
  43. "A",
  44. "B",
  45. "C",
  46. };
  47. // Helper function for case-insensitive string compare
  48. static bool strequals(const char *a, const char *b)
  49. {
  50. return strcasecmp(a, b) == 0;
  51. }
  52. // Verify format conformance to SCSI spec:
  53. // - Empty bytes filled with 0x20 (space)
  54. // - Only values 0x20 to 0x7E
  55. // - Left alignment for vendor/product/revision, right alignment for serial.
  56. static void formatDriveInfoField(char *field, int fieldsize, bool align_right)
  57. {
  58. if (align_right)
  59. {
  60. // Right align and trim spaces on either side
  61. int dst = fieldsize - 1;
  62. for (int src = fieldsize - 1; src >= 0; src--)
  63. {
  64. char c = field[src];
  65. if (c < 0x20 || c > 0x7E) c = 0x20;
  66. if (c != 0x20 || dst != fieldsize - 1)
  67. {
  68. field[dst--] = c;
  69. }
  70. }
  71. while (dst >= 0)
  72. {
  73. field[dst--] = 0x20;
  74. }
  75. }
  76. else
  77. {
  78. // Left align, preserve spaces in case config tries to manually right-align
  79. int dst = 0;
  80. for (int src = 0; src < fieldsize; src++)
  81. {
  82. char c = field[src];
  83. if (c < 0x20 || c > 0x7E) c = 0x20;
  84. field[dst++] = c;
  85. }
  86. while (dst < fieldsize)
  87. {
  88. field[dst++] = 0x20;
  89. }
  90. }
  91. }
  92. const char **ZuluSCSISettings::deviceInitST32430N(uint8_t scsiId)
  93. {
  94. static const char *st32430n[4] = {"SEAGATE", devicePresetName[DEV_PRESET_ST32430N], PLATFORM_REVISION, ""};
  95. m_dev[scsiId].deviceType = S2S_CFG_FIXED;
  96. m_dev[scsiId].sectorSDBegin = 0;
  97. m_dev[scsiId].sectorSDEnd = 4397055; // 2147MB into bytes and divide 512 - 1
  98. m_devPreset[scsiId] = DEV_PRESET_ST32430N;
  99. return st32430n;
  100. }
  101. void ZuluSCSISettings::setDefaultDriveInfo(uint8_t scsiId, const char *presetName, S2S_CFG_TYPE type)
  102. {
  103. char section[6] = "SCSI0";
  104. section[4] += scsiId;
  105. scsi_device_settings_t &cfgDev = m_dev[scsiId];
  106. scsi_device_settings_t &cfgDefault = m_dev[SCSI_SETTINGS_SYS_IDX];
  107. static const char * const driveinfo_fixed[4] = DRIVEINFO_FIXED;
  108. static const char * const driveinfo_removable[4] = DRIVEINFO_REMOVABLE;
  109. static const char * const driveinfo_optical[4] = DRIVEINFO_OPTICAL;
  110. static const char * const driveinfo_floppy[4] = DRIVEINFO_FLOPPY;
  111. static const char * const driveinfo_magopt[4] = DRIVEINFO_MAGOPT;
  112. static const char * const driveinfo_network[4] = DRIVEINFO_NETWORK;
  113. static const char * const driveinfo_tape[4] = DRIVEINFO_TAPE;
  114. static const char * const apl_driveinfo_fixed[4] = APPLE_DRIVEINFO_FIXED;
  115. static const char * const apl_driveinfo_removable[4] = APPLE_DRIVEINFO_REMOVABLE;
  116. static const char * const apl_driveinfo_optical[4] = APPLE_DRIVEINFO_OPTICAL;
  117. static const char * const apl_driveinfo_floppy[4] = APPLE_DRIVEINFO_FLOPPY;
  118. static const char * const apl_driveinfo_magopt[4] = APPLE_DRIVEINFO_MAGOPT;
  119. static const char * const apl_driveinfo_network[4] = APPLE_DRIVEINFO_NETWORK;
  120. static const char * const apl_driveinfo_tape[4] = APPLE_DRIVEINFO_TAPE;
  121. static const char * const iomega_driveinfo_removeable[4] = IOMEGA_DRIVEINFO_ZIP100;
  122. const char * const * driveinfo = NULL;
  123. bool known_preset = false;
  124. scsi_system_settings_t& cfgSys = m_sys;
  125. #ifdef ZULUSCSI_HARDWARE_CONFIG
  126. if (g_hw_config.is_active() && g_hw_config.device_preset() == DEV_PRESET_NONE)
  127. {
  128. // empty preset, use default
  129. known_preset = true;
  130. m_devPreset[scsiId] = DEV_PRESET_NONE;
  131. }
  132. else if (g_hw_config.is_active() && g_hw_config.device_preset() == DEV_PRESET_ST32430N)
  133. {
  134. driveinfo = deviceInitST32430N(scsiId);
  135. m_devPreset[scsiId] = DEV_PRESET_ST32430N;
  136. known_preset = true;
  137. }
  138. else
  139. #endif //ZULUSCSI_HARDWARE_CONFIG
  140. if (strequals(devicePresetName[DEV_PRESET_NONE], presetName))
  141. {
  142. // empty preset, use default
  143. known_preset = true;
  144. m_devPreset[scsiId] = DEV_PRESET_NONE;
  145. }
  146. else if (strequals(devicePresetName[DEV_PRESET_ST32430N], presetName))
  147. {
  148. driveinfo = deviceInitST32430N(scsiId);
  149. known_preset = true;
  150. }
  151. if (!known_preset)
  152. {
  153. m_devPreset[scsiId] = DEV_PRESET_NONE;
  154. logmsg("Unknown Device preset name ", presetName, ", using default settings");
  155. }
  156. if (m_devPreset[scsiId] == DEV_PRESET_NONE)
  157. {
  158. cfgDev.deviceType = type;
  159. cfgDev.deviceType = ini_getl(section, "Type", cfgDev.deviceType, CONFIGFILE);
  160. if (cfgSys.quirks == S2S_CFG_QUIRKS_APPLE)
  161. {
  162. // Use default drive IDs that are recognized by Apple machines
  163. switch (cfgDev.deviceType)
  164. {
  165. case S2S_CFG_FIXED: driveinfo = apl_driveinfo_fixed; break;
  166. case S2S_CFG_REMOVABLE: driveinfo = apl_driveinfo_removable; break;
  167. case S2S_CFG_OPTICAL: driveinfo = apl_driveinfo_optical; break;
  168. case S2S_CFG_FLOPPY_14MB: driveinfo = apl_driveinfo_floppy; break;
  169. case S2S_CFG_MO: driveinfo = apl_driveinfo_magopt; break;
  170. case S2S_CFG_NETWORK: driveinfo = apl_driveinfo_network; break;
  171. case S2S_CFG_SEQUENTIAL: driveinfo = apl_driveinfo_tape; break;
  172. case S2S_CFG_ZIP100: driveinfo = iomega_driveinfo_removeable; break;
  173. default: driveinfo = apl_driveinfo_fixed; break;
  174. }
  175. }
  176. else
  177. {
  178. // Generic IDs
  179. switch (cfgDev.deviceType)
  180. {
  181. case S2S_CFG_FIXED: driveinfo = driveinfo_fixed; break;
  182. case S2S_CFG_REMOVABLE: driveinfo = driveinfo_removable; break;
  183. case S2S_CFG_OPTICAL: driveinfo = driveinfo_optical; break;
  184. case S2S_CFG_FLOPPY_14MB: driveinfo = driveinfo_floppy; break;
  185. case S2S_CFG_MO: driveinfo = driveinfo_magopt; break;
  186. case S2S_CFG_NETWORK: driveinfo = driveinfo_network; break;
  187. case S2S_CFG_SEQUENTIAL: driveinfo = driveinfo_tape; break;
  188. case S2S_CFG_ZIP100: driveinfo = iomega_driveinfo_removeable; break;
  189. default: driveinfo = driveinfo_fixed; break;
  190. }
  191. }
  192. }
  193. // If the scsi string has not been set system wide use default scsi string
  194. if (!cfgDefault.vendor[0] && driveinfo[0][0])
  195. strncpy(cfgDev.vendor, driveinfo[0], sizeof(cfgDev.vendor));
  196. if (!cfgDefault.prodId[0] && driveinfo[1][0])
  197. strncpy(cfgDev.prodId, driveinfo[1], sizeof(cfgDev.prodId));
  198. if (!cfgDefault.revision[0] && driveinfo[2][0])
  199. strncpy(cfgDev.revision, driveinfo[2], sizeof(cfgDev.revision));
  200. if (!cfgDefault.serial[0] && driveinfo[3][0])
  201. strncpy(cfgDev.serial, driveinfo[3], sizeof(cfgDev.serial));
  202. }
  203. // Read device settings
  204. static void readIniSCSIDeviceSetting(scsi_device_settings_t &cfg, const char *section)
  205. {
  206. cfg.deviceTypeModifier = ini_getl(section, "TypeModifier", cfg.deviceTypeModifier, CONFIGFILE);
  207. cfg.sectorsPerTrack = ini_getl(section, "SectorsPerTrack", cfg.sectorsPerTrack, CONFIGFILE);
  208. cfg.headsPerCylinder = ini_getl(section, "HeadsPerCylinder", cfg.headsPerCylinder, CONFIGFILE);
  209. cfg.prefetchBytes = ini_getl(section, "PrefetchBytes", cfg.prefetchBytes, CONFIGFILE);
  210. cfg.ejectButton = ini_getl(section, "EjectButton", cfg.ejectButton, CONFIGFILE);
  211. cfg.vol = ini_getl(section, "CDAVolume", cfg.vol, CONFIGFILE) & 0xFF;
  212. cfg.nameFromImage = ini_getbool(section, "NameFromImage", cfg.nameFromImage, CONFIGFILE);
  213. cfg.rightAlignStrings = ini_getbool(section, "RightAlignStrings", cfg.rightAlignStrings , CONFIGFILE);
  214. cfg.reinsertOnInquiry = ini_getbool(section, "ReinsertCDOnInquiry", cfg.reinsertOnInquiry, CONFIGFILE);
  215. cfg.reinsertAfterEject = ini_getbool(section, "ReinsertAfterEject", cfg.reinsertAfterEject, CONFIGFILE);
  216. cfg.disableMacSanityCheck = ini_getbool(section, "DisableMacSanityCheck", cfg.disableMacSanityCheck, CONFIGFILE);
  217. cfg.sectorSDBegin = ini_getl(section, "SectorSDBegin", cfg.sectorSDBegin, CONFIGFILE);
  218. cfg.sectorSDEnd = ini_getl(section, "SectorSDEnd", cfg.sectorSDEnd, CONFIGFILE);
  219. cfg.vendorExtensions = ini_getl(section, "VendorExtensions", cfg.vendorExtensions, CONFIGFILE);
  220. cfg.blockSize = ini_getl(section, "BlockSize", cfg.blockSize, CONFIGFILE);
  221. char tmp[32];
  222. ini_gets(section, "Vendor", "", tmp, sizeof(tmp), CONFIGFILE);
  223. if (tmp[0])
  224. {
  225. memset(cfg.vendor, 0, sizeof(cfg.vendor));
  226. strncpy(cfg.vendor, tmp, sizeof(cfg.vendor));
  227. }
  228. memset(tmp, 0, sizeof(tmp));
  229. ini_gets(section, "Product", "", tmp, sizeof(tmp), CONFIGFILE);
  230. if (tmp[0])
  231. {
  232. memset(cfg.prodId, 0, sizeof(cfg.prodId));
  233. strncpy(cfg.prodId, tmp, sizeof(cfg.prodId));
  234. }
  235. memset(tmp, 0, sizeof(tmp));
  236. ini_gets(section, "Version", "", tmp, sizeof(tmp), CONFIGFILE);
  237. if (tmp[0])
  238. {
  239. memset(cfg.revision, 0, sizeof(cfg.revision));
  240. strncpy(cfg.revision, tmp, sizeof(cfg.revision));
  241. }
  242. memset(tmp, 0, sizeof(tmp));
  243. ini_gets(section, "Serial", "", tmp, sizeof(tmp), CONFIGFILE);
  244. if (tmp[0])
  245. {
  246. memset(cfg.serial, 0, sizeof(cfg.serial));
  247. strncpy(cfg.serial, tmp, sizeof(cfg.serial));
  248. }
  249. }
  250. scsi_system_settings_t *ZuluSCSISettings::initSystem(const char *presetName)
  251. {
  252. scsi_system_settings_t &cfgSys = m_sys;
  253. scsi_device_settings_t &cfgDev = m_dev[SCSI_SETTINGS_SYS_IDX];
  254. // This is a hack to figure out if apple quirks is on via a dip switch
  255. S2S_TargetCfg img;
  256. img.quirks = S2S_CFG_QUIRKS_NONE;
  257. #ifdef PLATFORM_CONFIG_HOOK
  258. PLATFORM_CONFIG_HOOK(&img);
  259. #endif
  260. // Default settings for host compatibility
  261. cfgSys.quirks = img.quirks;
  262. cfgSys.selectionDelay = 255;
  263. cfgSys.maxSyncSpeed = PLATFORM_DEFAULT_SCSI_SPEED_SETTING;
  264. cfgSys.initPreDelay = 0;
  265. cfgSys.initPostDelay = 0;
  266. cfgSys.phyMode = 0;
  267. cfgSys.enableUnitAttention = false;
  268. cfgSys.enableSCSI2 = true;
  269. cfgSys.enableSelLatch = false;
  270. cfgSys.mapLunsToIDs = false;
  271. cfgSys.enableParity = true;
  272. cfgSys.useFATAllocSize = false;
  273. cfgSys.enableCDAudio = false;
  274. cfgSys.maxVolume = 100;
  275. cfgSys.enableUSBMassStorage = false;
  276. cfgSys.usbMassStorageWaitPeriod = 1000;
  277. cfgSys.usbMassStoragePresentImages = false;
  278. cfgSys.invertStatusLed = false;
  279. cfgSys.speedGrade = zuluscsi_speed_grade_t::SPEED_GRADE_DEFAULT;
  280. // setting set for all or specific devices
  281. cfgDev.deviceType = S2S_CFG_NOT_SET;
  282. cfgDev.deviceTypeModifier = 0;
  283. cfgDev.sectorsPerTrack = 0;
  284. cfgDev.headsPerCylinder = 0;
  285. cfgDev.prefetchBytes = PREFETCH_BUFFER_SIZE;
  286. cfgDev.ejectButton = 0;
  287. cfgDev.vol = DEFAULT_VOLUME_LEVEL;
  288. cfgDev.nameFromImage = false;
  289. cfgDev.rightAlignStrings = false;
  290. cfgDev.reinsertOnInquiry = true;
  291. cfgDev.reinsertAfterEject = true;
  292. cfgDev.disableMacSanityCheck = false;
  293. cfgDev.sectorSDBegin = 0;
  294. cfgDev.sectorSDEnd = 0;
  295. cfgDev.vendorExtensions = 0;
  296. cfgDev.blockSize = 0;
  297. // System-specific defaults
  298. if (strequals(systemPresetName[SYS_PRESET_NONE], presetName))
  299. {
  300. // Preset name is empty, use default configuration
  301. m_sysPreset = SYS_PRESET_NONE;
  302. }
  303. else if (strequals(systemPresetName[SYS_PRESET_MAC], presetName))
  304. {
  305. m_sysPreset = SYS_PRESET_MAC;
  306. cfgSys.quirks = S2S_CFG_QUIRKS_APPLE;
  307. }
  308. else if (strequals(systemPresetName[SYS_PRESET_MACPLUS], presetName))
  309. {
  310. m_sysPreset = SYS_PRESET_MACPLUS;
  311. cfgSys.quirks = S2S_CFG_QUIRKS_APPLE;
  312. cfgSys.enableSelLatch = true;
  313. cfgSys.enableSCSI2 = false;
  314. cfgSys.selectionDelay = 0;
  315. }
  316. else if (strequals(systemPresetName[SYS_PRESET_MPC3000], presetName))
  317. {
  318. m_sysPreset = SYS_PRESET_MPC3000;
  319. cfgSys.initPreDelay = 600;
  320. }
  321. else if (strequals(systemPresetName[SYS_PRESET_MEGASTE], presetName))
  322. {
  323. m_sysPreset = SYS_PRESET_MEGASTE;
  324. cfgSys.quirks = S2S_CFG_QUIRKS_NONE;
  325. cfgSys.mapLunsToIDs = true;
  326. cfgSys.enableParity = false;
  327. }
  328. else if (strequals(systemPresetName[SYS_PRESET_X68000], presetName))
  329. {
  330. m_sysPreset = SYS_PRESET_X68000;
  331. cfgSys.selectionDelay = 0;
  332. cfgSys.quirks = S2S_CFG_QUIRKS_X68000;
  333. cfgSys.enableSCSI2 = false;
  334. cfgSys.maxSyncSpeed = 5;
  335. }
  336. else
  337. {
  338. m_sysPreset = SYS_PRESET_NONE;
  339. logmsg("Unknown System preset name ", presetName, ", using default settings");
  340. }
  341. // Clear SCSI device strings
  342. memset(cfgDev.vendor, 0, sizeof(cfgDev.vendor));
  343. memset(cfgDev.prodId, 0, sizeof(cfgDev.prodId));
  344. memset(cfgDev.revision, 0, sizeof(cfgDev.revision));
  345. memset(cfgDev.serial, 0, sizeof(cfgDev.serial));
  346. // Read default setting overrides from ini file for each SCSI device
  347. readIniSCSIDeviceSetting(cfgDev, "SCSI");
  348. // Read settings from ini file that apply to all SCSI device
  349. cfgSys.quirks = ini_getl("SCSI", "Quirks", cfgSys.quirks, CONFIGFILE);
  350. cfgSys.selectionDelay = ini_getl("SCSI", "SelectionDelay", cfgSys.selectionDelay, CONFIGFILE);
  351. cfgSys.maxSyncSpeed = ini_getl("SCSI", "MaxSyncSpeed", cfgSys.maxSyncSpeed, CONFIGFILE);
  352. cfgSys.initPreDelay = ini_getl("SCSI", "InitPreDelay", cfgSys.initPreDelay, CONFIGFILE);
  353. cfgSys.initPostDelay = ini_getl("SCSI", "InitPostDelay", cfgSys.initPostDelay, CONFIGFILE);
  354. cfgSys.phyMode = ini_getl("SCSI", "PhyMode", cfgSys.phyMode, CONFIGFILE);
  355. cfgSys.enableUnitAttention = ini_getbool("SCSI", "EnableUnitAttention", cfgSys.enableUnitAttention, CONFIGFILE);
  356. cfgSys.enableSCSI2 = ini_getbool("SCSI", "EnableSCSI2", cfgSys.enableSCSI2, CONFIGFILE);
  357. cfgSys.enableSelLatch = ini_getbool("SCSI", "EnableSelLatch", cfgSys.enableSelLatch, CONFIGFILE);
  358. cfgSys.mapLunsToIDs = ini_getbool("SCSI", "MapLunsToIDs", cfgSys.mapLunsToIDs, CONFIGFILE);
  359. cfgSys.enableParity = ini_getbool("SCSI", "EnableParity", cfgSys.enableParity, CONFIGFILE);
  360. cfgSys.useFATAllocSize = ini_getbool("SCSI", "UseFATAllocSize", cfgSys.useFATAllocSize, CONFIGFILE);
  361. cfgSys.enableCDAudio = ini_getbool("SCSI", "EnableCDAudio", cfgSys.enableCDAudio, CONFIGFILE);
  362. cfgSys.maxVolume = ini_getl("SCSI", "MaxVolume", cfgSys.maxVolume, CONFIGFILE);
  363. cfgSys.enableUSBMassStorage = ini_getbool("SCSI", "EnableUSBMassStorage", cfgSys.enableUSBMassStorage, CONFIGFILE);
  364. cfgSys.usbMassStorageWaitPeriod = ini_getl("SCSI", "USBMassStorageWaitPeriod", cfgSys.usbMassStorageWaitPeriod, CONFIGFILE);
  365. cfgSys.usbMassStoragePresentImages = ini_getbool("SCSI", "USBMassStoragePresentImages", cfgSys.usbMassStoragePresentImages, CONFIGFILE);
  366. cfgSys.invertStatusLed = ini_getbool("SCSI", "InvertStatusLED", cfgSys.invertStatusLed, CONFIGFILE);
  367. char tmp[32];
  368. ini_gets("SCSI", "SpeedGrade", "", tmp, sizeof(tmp), CONFIGFILE);
  369. if (tmp[0] != '\0')
  370. {
  371. if (platform_reclock_supported())
  372. {
  373. cfgSys.speedGrade = stringToSpeedGrade(tmp, sizeof(tmp));
  374. }
  375. else
  376. {
  377. logmsg("Speed grade setting ignored, reclocking the MCU is not supported by this device");
  378. }
  379. }
  380. return &cfgSys;
  381. }
  382. scsi_device_settings_t* ZuluSCSISettings::initDevice(uint8_t scsiId, S2S_CFG_TYPE type)
  383. {
  384. scsi_device_settings_t& cfg = m_dev[scsiId];
  385. char presetName[32] = {};
  386. char section[6] = "SCSI0";
  387. section[4] = '0' + scsiId;
  388. #ifdef ZULUSCSI_HARDWARE_CONFIG
  389. const char *hwDevicePresetName = g_scsi_settings.getDevicePresetName(scsiId);
  390. if (g_hw_config.is_active())
  391. {
  392. if (strlen(hwDevicePresetName) < sizeof(presetName))
  393. {
  394. strncpy(presetName, hwDevicePresetName, sizeof(presetName) - 1);
  395. }
  396. }
  397. else
  398. #endif
  399. {
  400. ini_gets(section, "Device", "", presetName, sizeof(presetName), CONFIGFILE);
  401. }
  402. // Write default configuration from system setting initialization
  403. memcpy(&cfg, &m_dev[SCSI_SETTINGS_SYS_IDX], sizeof(cfg));
  404. setDefaultDriveInfo(scsiId, presetName, type);
  405. readIniSCSIDeviceSetting(cfg, section);
  406. if (cfg.serial[0] == '\0')
  407. {
  408. // Use SD card serial number
  409. cid_t sd_cid;
  410. uint32_t sd_sn = 0;
  411. if (SD.card()->readCID(&sd_cid))
  412. {
  413. sd_sn = sd_cid.psn();
  414. }
  415. memset(cfg.serial, 0, sizeof(cfg.serial));
  416. const char *nibble = "0123456789ABCDEF";
  417. cfg.serial[0] = nibble[(sd_sn >> 28) & 0xF];
  418. cfg.serial[1] = nibble[(sd_sn >> 24) & 0xF];
  419. cfg.serial[2] = nibble[(sd_sn >> 20) & 0xF];
  420. cfg.serial[3] = nibble[(sd_sn >> 16) & 0xF];
  421. cfg.serial[4] = nibble[(sd_sn >> 12) & 0xF];
  422. cfg.serial[5] = nibble[(sd_sn >> 8) & 0xF];
  423. cfg.serial[6] = nibble[(sd_sn >> 4) & 0xF];
  424. cfg.serial[7] = nibble[(sd_sn >> 0) & 0xF];
  425. }
  426. formatDriveInfoField(cfg.vendor, sizeof(cfg.vendor), cfg.rightAlignStrings);
  427. formatDriveInfoField(cfg.prodId, sizeof(cfg.prodId), cfg.rightAlignStrings);
  428. formatDriveInfoField(cfg.revision, sizeof(cfg.revision), cfg.rightAlignStrings);
  429. formatDriveInfoField(cfg.serial, sizeof(cfg.serial), true);
  430. return &cfg;
  431. }
  432. scsi_system_settings_t *ZuluSCSISettings::getSystem()
  433. {
  434. return &m_sys;
  435. }
  436. scsi_device_settings_t *ZuluSCSISettings::getDevice(uint8_t scsiId)
  437. {
  438. return &m_dev[scsiId];
  439. }
  440. scsi_system_preset_t ZuluSCSISettings::getSystemPreset()
  441. {
  442. return m_sysPreset;
  443. }
  444. const char* ZuluSCSISettings::getSystemPresetName()
  445. {
  446. return systemPresetName[m_sysPreset];
  447. }
  448. scsi_device_preset_t ZuluSCSISettings::getDevicePreset(uint8_t scsiId)
  449. {
  450. return m_devPreset[scsiId];
  451. }
  452. const char* ZuluSCSISettings::getDevicePresetName(uint8_t scsiId)
  453. {
  454. return devicePresetName[m_devPreset[scsiId]];
  455. }
  456. zuluscsi_speed_grade_t ZuluSCSISettings::stringToSpeedGrade(const char *speed_grade_target, size_t length)
  457. {
  458. zuluscsi_speed_grade_t grade = zuluscsi_speed_grade_t::SPEED_GRADE_DEFAULT;
  459. bool found_speed_grade = false;
  460. // search the list of speed grade strings for a matching target
  461. for (uint8_t i = 0; i < sizeof(speed_grade_strings)/sizeof(speed_grade_strings[0]); i++)
  462. {
  463. if (strncasecmp(speed_grade_target, speed_grade_strings[i], length) == 0)
  464. {
  465. grade = (zuluscsi_speed_grade_t)i;
  466. found_speed_grade = true;
  467. break;
  468. }
  469. }
  470. if (!found_speed_grade)
  471. {
  472. logmsg("Setting \"", speed_grade_target, "\" does not match any known speed grade, using default");
  473. grade = SPEED_GRADE_DEFAULT;
  474. }
  475. return grade;
  476. }
  477. const char *ZuluSCSISettings::getSpeedGradeString()
  478. {
  479. return speed_grade_strings[m_sys.speedGrade];
  480. }