ZuluSCSI_settings.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 <strings.h>
  28. #include <minIni.h>
  29. #include <minIni_cache.h>
  30. // SCSI system and device settings
  31. ZuluSCSISettings g_scsi_settings;
  32. const char *systemPresetName[] = {"", "Mac", "MacPlus", "MPC3000", "MegaSTE", "X68000"};
  33. const char *devicePresetName[] = {"", "ST32430N"};
  34. // Helper function for case-insensitive string compare
  35. static bool strequals(const char *a, const char *b)
  36. {
  37. return strcasecmp(a, b) == 0;
  38. }
  39. // Verify format conformance to SCSI spec:
  40. // - Empty bytes filled with 0x20 (space)
  41. // - Only values 0x20 to 0x7E
  42. // - Left alignment for vendor/product/revision, right alignment for serial.
  43. static void formatDriveInfoField(char *field, int fieldsize, bool align_right)
  44. {
  45. if (align_right)
  46. {
  47. // Right align and trim spaces on either side
  48. int dst = fieldsize - 1;
  49. for (int src = fieldsize - 1; src >= 0; src--)
  50. {
  51. char c = field[src];
  52. if (c < 0x20 || c > 0x7E) c = 0x20;
  53. if (c != 0x20 || dst != fieldsize - 1)
  54. {
  55. field[dst--] = c;
  56. }
  57. }
  58. while (dst >= 0)
  59. {
  60. field[dst--] = 0x20;
  61. }
  62. }
  63. else
  64. {
  65. // Left align, preserve spaces in case config tries to manually right-align
  66. int dst = 0;
  67. for (int src = 0; src < fieldsize; src++)
  68. {
  69. char c = field[src];
  70. if (c < 0x20 || c > 0x7E) c = 0x20;
  71. field[dst++] = c;
  72. }
  73. while (dst < fieldsize)
  74. {
  75. field[dst++] = 0x20;
  76. }
  77. }
  78. }
  79. const char **ZuluSCSISettings::deviceInitST32430N(uint8_t scsiId)
  80. {
  81. static const char *st32430n[4] = {"SEAGATE", devicePresetName[DEV_PRESET_ST32430N], PLATFORM_REVISION, ""};
  82. m_dev[scsiId].deviceType = S2S_CFG_FIXED;
  83. m_dev[scsiId].sectorSDBegin = 0;
  84. m_dev[scsiId].sectorSDEnd = 4397055; // 2147MB into bytes and divide 512 - 1
  85. m_devPreset[scsiId] = DEV_PRESET_ST32430N;
  86. return st32430n;
  87. }
  88. void ZuluSCSISettings::setDefaultDriveInfo(uint8_t scsiId, const char *presetName, S2S_CFG_TYPE type)
  89. {
  90. char section[6] = "SCSI0";
  91. section[4] += scsiId;
  92. scsi_device_settings_t &cfgDev = m_dev[scsiId];
  93. scsi_device_settings_t &cfgDefault = m_dev[SCSI_SETTINGS_SYS_IDX];
  94. static const char *driveinfo_fixed[4] = DRIVEINFO_FIXED;
  95. static const char *driveinfo_removable[4] = DRIVEINFO_REMOVABLE;
  96. static const char *driveinfo_optical[4] = DRIVEINFO_OPTICAL;
  97. static const char *driveinfo_floppy[4] = DRIVEINFO_FLOPPY;
  98. static const char *driveinfo_magopt[4] = DRIVEINFO_MAGOPT;
  99. static const char *driveinfo_network[4] = DRIVEINFO_NETWORK;
  100. static const char *driveinfo_tape[4] = DRIVEINFO_TAPE;
  101. static const char *apl_driveinfo_fixed[4] = APPLE_DRIVEINFO_FIXED;
  102. static const char *apl_driveinfo_removable[4] = APPLE_DRIVEINFO_REMOVABLE;
  103. static const char *apl_driveinfo_optical[4] = APPLE_DRIVEINFO_OPTICAL;
  104. static const char *apl_driveinfo_floppy[4] = APPLE_DRIVEINFO_FLOPPY;
  105. static const char *apl_driveinfo_magopt[4] = APPLE_DRIVEINFO_MAGOPT;
  106. static const char *apl_driveinfo_network[4] = APPLE_DRIVEINFO_NETWORK;
  107. static const char *apl_driveinfo_tape[4] = APPLE_DRIVEINFO_TAPE;
  108. const char **driveinfo = NULL;
  109. bool known_preset = false;
  110. scsi_system_settings_t& cfgSys = m_sys;
  111. #ifdef ZULUSCSI_HARDWARE_CONFIG
  112. if (g_hw_config.is_active() && g_hw_config.device_preset() == DEV_PRESET_NONE)
  113. {
  114. // empty preset, use default
  115. known_preset = true;
  116. m_devPreset[scsiId] = DEV_PRESET_NONE;
  117. }
  118. else if (g_hw_config.is_active() && g_hw_config.device_preset() == DEV_PRESET_ST32430N)
  119. {
  120. driveinfo = deviceInitST32430N(scsiId);
  121. m_devPreset[scsiId] = DEV_PRESET_ST32430N;
  122. known_preset = true;
  123. }
  124. else
  125. #endif //ZULUSCSI_HARDWARE_CONFIG
  126. if (strequals(devicePresetName[DEV_PRESET_NONE], presetName))
  127. {
  128. // empty preset, use default
  129. known_preset = true;
  130. m_devPreset[scsiId] = DEV_PRESET_NONE;
  131. }
  132. else if (strequals(devicePresetName[DEV_PRESET_ST32430N], presetName))
  133. {
  134. driveinfo = deviceInitST32430N(scsiId);
  135. known_preset = true;
  136. }
  137. if (!known_preset)
  138. {
  139. m_devPreset[scsiId] = DEV_PRESET_NONE;
  140. logmsg("Unknown Device preset name ", presetName, ", using default settings");
  141. }
  142. if (m_devPreset[scsiId] == DEV_PRESET_NONE)
  143. {
  144. cfgDev.deviceType = type;
  145. if (cfgSys.quirks == S2S_CFG_QUIRKS_APPLE)
  146. {
  147. // Use default drive IDs that are recognized by Apple machines
  148. switch (cfgDev.deviceType)
  149. {
  150. case S2S_CFG_FIXED: driveinfo = apl_driveinfo_fixed; break;
  151. case S2S_CFG_REMOVABLE: driveinfo = apl_driveinfo_removable; break;
  152. case S2S_CFG_OPTICAL: driveinfo = apl_driveinfo_optical; break;
  153. case S2S_CFG_FLOPPY_14MB: driveinfo = apl_driveinfo_floppy; break;
  154. case S2S_CFG_MO: driveinfo = apl_driveinfo_magopt; break;
  155. case S2S_CFG_NETWORK: driveinfo = apl_driveinfo_network; break;
  156. case S2S_CFG_SEQUENTIAL: driveinfo = apl_driveinfo_tape; break;
  157. default: driveinfo = apl_driveinfo_fixed; break;
  158. }
  159. }
  160. else
  161. {
  162. // Generic IDs
  163. switch (cfgDev.deviceType)
  164. {
  165. case S2S_CFG_FIXED: driveinfo = driveinfo_fixed; break;
  166. case S2S_CFG_REMOVABLE: driveinfo = driveinfo_removable; break;
  167. case S2S_CFG_OPTICAL: driveinfo = driveinfo_optical; break;
  168. case S2S_CFG_FLOPPY_14MB: driveinfo = driveinfo_floppy; break;
  169. case S2S_CFG_MO: driveinfo = driveinfo_magopt; break;
  170. case S2S_CFG_NETWORK: driveinfo = driveinfo_network; break;
  171. case S2S_CFG_SEQUENTIAL: driveinfo = driveinfo_tape; break;
  172. default: driveinfo = driveinfo_fixed; break;
  173. }
  174. }
  175. }
  176. // If the scsi string has not been set system wide use default scsi string
  177. if (!cfgDefault.vendor[0] && driveinfo[0][0])
  178. strncpy(cfgDev.vendor, driveinfo[0], sizeof(cfgDev.vendor));
  179. if (!cfgDefault.prodId[0] && driveinfo[1][0])
  180. strncpy(cfgDev.prodId, driveinfo[1], sizeof(cfgDev.prodId));
  181. if (!cfgDefault.revision[0] && driveinfo[2][0])
  182. strncpy(cfgDev.revision, driveinfo[2], sizeof(cfgDev.revision));
  183. if (!cfgDefault.serial[0] && driveinfo[3][0])
  184. strncpy(cfgDev.serial, driveinfo[3], sizeof(cfgDev.serial));
  185. }
  186. // Read device settings
  187. static void readIniSCSIDeviceSetting(scsi_device_settings_t &cfg, const char *section)
  188. {
  189. cfg.deviceType = ini_getl(section, "Type", cfg.deviceType, CONFIGFILE);
  190. cfg.deviceTypeModifier = ini_getl(section, "TypeModifier", cfg.deviceTypeModifier, CONFIGFILE);
  191. cfg.sectorsPerTrack = ini_getl(section, "SectorsPerTrack", cfg.sectorsPerTrack, CONFIGFILE);
  192. cfg.headsPerCylinder = ini_getl(section, "HeadsPerCylinder", cfg.headsPerCylinder, CONFIGFILE);
  193. cfg.prefetchBytes = ini_getl(section, "PrefetchBytes", cfg.prefetchBytes, CONFIGFILE);
  194. cfg.ejectButton = ini_getl(section, "EjectButton", cfg.ejectButton, CONFIGFILE);
  195. cfg.vol = ini_getl(section, "CDAVolume", cfg.vol, CONFIGFILE) & 0xFF;
  196. cfg.nameFromImage = ini_getbool(section, "NameFromImage", cfg.nameFromImage, CONFIGFILE);
  197. cfg.rightAlignStrings = ini_getbool(section, "RightAlignStrings", cfg.rightAlignStrings , CONFIGFILE);
  198. cfg.reinsertOnInquiry = ini_getbool(section, "ReinsertCDOnInquiry", cfg.reinsertOnInquiry, CONFIGFILE);
  199. cfg.reinsertAfterEject = ini_getbool(section, "ReinsertAfterEject", cfg.reinsertAfterEject, CONFIGFILE);
  200. cfg.disableMacSanityCheck = ini_getbool(section, "DisableMacSanityCheck", cfg.disableMacSanityCheck, CONFIGFILE);
  201. cfg.sectorSDBegin = ini_getl(section, "SectorSDBegin", cfg.sectorSDBegin, CONFIGFILE);
  202. cfg.sectorSDEnd = ini_getl(section, "SectorSDEnd", cfg.sectorSDEnd, CONFIGFILE);
  203. char tmp[32];
  204. ini_gets(section, "Vendor", "", tmp, sizeof(tmp), CONFIGFILE);
  205. if (tmp[0])
  206. {
  207. memset(cfg.vendor, 0, sizeof(cfg.vendor));
  208. strncpy(cfg.vendor, tmp, sizeof(cfg.vendor));
  209. }
  210. memset(tmp, 0, sizeof(tmp));
  211. ini_gets(section, "Product", "", tmp, sizeof(tmp), CONFIGFILE);
  212. if (tmp[0])
  213. {
  214. memset(cfg.prodId, 0, sizeof(cfg.prodId));
  215. strncpy(cfg.prodId, tmp, sizeof(cfg.prodId));
  216. }
  217. memset(tmp, 0, sizeof(tmp));
  218. ini_gets(section, "Version", "", tmp, sizeof(tmp), CONFIGFILE);
  219. if (tmp[0])
  220. {
  221. memset(cfg.revision, 0, sizeof(cfg.revision));
  222. strncpy(cfg.revision, tmp, sizeof(cfg.revision));
  223. }
  224. memset(tmp, 0, sizeof(tmp));
  225. ini_gets(section, "Serial", "", tmp, sizeof(tmp), CONFIGFILE);
  226. if (tmp[0])
  227. {
  228. memset(cfg.serial, 0, sizeof(cfg.serial));
  229. strncpy(cfg.serial, tmp, sizeof(cfg.serial));
  230. }
  231. }
  232. scsi_system_settings_t *ZuluSCSISettings::initSystem(const char *presetName)
  233. {
  234. scsi_system_settings_t &cfgSys = m_sys;
  235. scsi_device_settings_t &cfgDev = m_dev[SCSI_SETTINGS_SYS_IDX];
  236. // This is a hack to figure out if apple quirks is on via a dip switch
  237. S2S_TargetCfg img;
  238. img.quirks = S2S_CFG_QUIRKS_NONE;
  239. #ifdef PLATFORM_CONFIG_HOOK
  240. PLATFORM_CONFIG_HOOK(&img);
  241. #endif
  242. // Default settings for host compatibility
  243. cfgSys.quirks = img.quirks;
  244. cfgSys.selectionDelay = 255;
  245. cfgSys.maxSyncSpeed = 10;
  246. cfgSys.initPreDelay = 0;
  247. cfgSys.initPostDelay = 0;
  248. cfgSys.phyMode = 0;
  249. cfgSys.enableUnitAttention = false;
  250. cfgSys.enableSCSI2 = true;
  251. cfgSys.enableSelLatch = false;
  252. cfgSys.mapLunsToIDs = false;
  253. cfgSys.enableParity = true;
  254. cfgSys.useFATAllocSize = false;
  255. // setting set for all or specific devices
  256. cfgDev.deviceType = S2S_CFG_NOT_SET;
  257. cfgDev.deviceTypeModifier = 0;
  258. cfgDev.sectorsPerTrack = 63;
  259. cfgDev.headsPerCylinder = 255;
  260. cfgDev.prefetchBytes = PREFETCH_BUFFER_SIZE;
  261. cfgDev.ejectButton = 0;
  262. cfgDev.vol = DEFAULT_VOLUME_LEVEL;
  263. cfgDev.nameFromImage = false;
  264. cfgDev.rightAlignStrings = false;
  265. cfgDev.reinsertOnInquiry = true;
  266. cfgDev.reinsertAfterEject = true;
  267. cfgDev.disableMacSanityCheck = false;
  268. cfgDev.sectorSDBegin = 0;
  269. cfgDev.sectorSDEnd = 0;
  270. // System-specific defaults
  271. if (strequals(systemPresetName[SYS_PRESET_NONE], presetName))
  272. {
  273. // Preset name is empty, use default configuration
  274. m_sysPreset = SYS_PRESET_NONE;
  275. }
  276. else if (strequals(systemPresetName[SYS_PRESET_MAC], presetName))
  277. {
  278. m_sysPreset = SYS_PRESET_MAC;
  279. cfgSys.quirks = S2S_CFG_QUIRKS_APPLE;
  280. }
  281. else if (strequals(systemPresetName[SYS_PRESET_MACPLUS], presetName))
  282. {
  283. m_sysPreset = SYS_PRESET_MACPLUS;
  284. cfgSys.quirks = S2S_CFG_QUIRKS_APPLE;
  285. cfgSys.enableSelLatch = true;
  286. cfgSys.enableSCSI2 = false;
  287. cfgSys.selectionDelay = 0;
  288. }
  289. else if (strequals(systemPresetName[SYS_PRESET_MPC3000], presetName))
  290. {
  291. m_sysPreset = SYS_PRESET_MPC3000;
  292. cfgSys.initPreDelay = 600;
  293. }
  294. else if (strequals(systemPresetName[SYS_PRESET_MEGASTE], presetName))
  295. {
  296. m_sysPreset = SYS_PRESET_MEGASTE;
  297. cfgSys.quirks = S2S_CFG_QUIRKS_NONE;
  298. cfgSys.mapLunsToIDs = true;
  299. cfgSys.enableParity = false;
  300. }
  301. else if (strequals(systemPresetName[SYS_PRESET_X68000], presetName))
  302. {
  303. m_sysPreset = SYS_PRESET_X68000;
  304. cfgSys.selectionDelay = 0;
  305. cfgSys.quirks = S2S_CFG_QUIRKS_NONE;
  306. cfgSys.enableSCSI2 = false;
  307. cfgSys.maxSyncSpeed = 5;
  308. }
  309. else
  310. {
  311. m_sysPreset = SYS_PRESET_NONE;
  312. logmsg("Unknown System preset name ", presetName, ", using default settings");
  313. }
  314. // Clear SCSI device strings
  315. memset(cfgDev.vendor, 0, sizeof(cfgDev.vendor));
  316. memset(cfgDev.prodId, 0, sizeof(cfgDev.prodId));
  317. memset(cfgDev.revision, 0, sizeof(cfgDev.revision));
  318. memset(cfgDev.serial, 0, sizeof(cfgDev.serial));
  319. // Read default setting overrides from ini file for each SCSI device
  320. readIniSCSIDeviceSetting(cfgDev, "SCSI");
  321. // Read settings from ini file that apply to all SCSI device
  322. cfgSys.quirks = ini_getl("SCSI", "Quirks", cfgSys.quirks, CONFIGFILE);
  323. cfgSys.selectionDelay = ini_getl("SCSI", "SelectionDelay", cfgSys.selectionDelay, CONFIGFILE);
  324. cfgSys.maxSyncSpeed = ini_getl("SCSI", "MaxSyncSpeed", cfgSys.maxSyncSpeed, CONFIGFILE);
  325. cfgSys.initPreDelay = ini_getl("SCSI", "InitPreDelay", cfgSys.initPreDelay, CONFIGFILE);
  326. cfgSys.initPostDelay = ini_getl("SCSI", "InitPostDelay", cfgSys.initPostDelay, CONFIGFILE);
  327. cfgSys.phyMode = ini_getl("SCSI", "PhyMode", cfgSys.phyMode, CONFIGFILE);
  328. cfgSys.enableUnitAttention = ini_getbool("SCSI", "EnableUnitAttention", cfgSys.enableUnitAttention, CONFIGFILE);
  329. cfgSys.enableSCSI2 = ini_getbool("SCSI", "EnableSCSI2", cfgSys.enableSCSI2, CONFIGFILE);
  330. cfgSys.enableSelLatch = ini_getbool("SCSI", "EnableSelLatch", cfgSys.enableSelLatch, CONFIGFILE);
  331. cfgSys.mapLunsToIDs = ini_getbool("SCSI", "MapLunsToIDs", cfgSys.mapLunsToIDs, CONFIGFILE);
  332. cfgSys.enableParity = ini_getbool("SCSI", "EnableParity", cfgSys.enableParity, CONFIGFILE);
  333. cfgSys.useFATAllocSize = ini_getbool("SCSI", "UseFATAllocSize", cfgSys.useFATAllocSize, CONFIGFILE);
  334. return &cfgSys;
  335. }
  336. scsi_device_settings_t* ZuluSCSISettings::initDevice(uint8_t scsiId, S2S_CFG_TYPE type)
  337. {
  338. scsi_device_settings_t& cfg = m_dev[scsiId];
  339. char presetName[32] = {};
  340. char section[6] = "SCSI0";
  341. section[4] = '0' + scsiId;
  342. #ifdef ZULUSCSI_HARDWARE_CONFIG
  343. const char *hwDevicePresetName = g_scsi_settings.getDevicePresetName(scsiId);
  344. if (g_hw_config.is_active())
  345. {
  346. if (strlen(hwDevicePresetName) < sizeof(presetName))
  347. {
  348. strncpy(presetName, hwDevicePresetName, sizeof(presetName) - 1);
  349. }
  350. }
  351. else
  352. #endif
  353. {
  354. ini_gets(section, "Device", "", presetName, sizeof(presetName), CONFIGFILE);
  355. }
  356. // Write default configuration from system setting initialization
  357. memcpy(&cfg, &m_dev[SCSI_SETTINGS_SYS_IDX], sizeof(cfg));
  358. setDefaultDriveInfo(scsiId, presetName, type);
  359. readIniSCSIDeviceSetting(cfg, section);
  360. if (cfg.serial[0] == '\0')
  361. {
  362. // Use SD card serial number
  363. cid_t sd_cid;
  364. uint32_t sd_sn = 0;
  365. if (SD.card()->readCID(&sd_cid))
  366. {
  367. sd_sn = sd_cid.psn();
  368. }
  369. memset(cfg.serial, 0, sizeof(cfg.serial));
  370. const char *nibble = "0123456789ABCDEF";
  371. cfg.serial[0] = nibble[(sd_sn >> 28) & 0xF];
  372. cfg.serial[1] = nibble[(sd_sn >> 24) & 0xF];
  373. cfg.serial[2] = nibble[(sd_sn >> 20) & 0xF];
  374. cfg.serial[3] = nibble[(sd_sn >> 16) & 0xF];
  375. cfg.serial[4] = nibble[(sd_sn >> 12) & 0xF];
  376. cfg.serial[5] = nibble[(sd_sn >> 8) & 0xF];
  377. cfg.serial[6] = nibble[(sd_sn >> 4) & 0xF];
  378. cfg.serial[7] = nibble[(sd_sn >> 0) & 0xF];
  379. }
  380. formatDriveInfoField(cfg.vendor, sizeof(cfg.vendor), cfg.rightAlignStrings);
  381. formatDriveInfoField(cfg.prodId, sizeof(cfg.prodId), cfg.rightAlignStrings);
  382. formatDriveInfoField(cfg.revision, sizeof(cfg.revision), cfg.rightAlignStrings);
  383. formatDriveInfoField(cfg.serial, sizeof(cfg.serial), true);
  384. return &cfg;
  385. }
  386. scsi_system_settings_t *ZuluSCSISettings::getSystem()
  387. {
  388. return &m_sys;
  389. }
  390. scsi_device_settings_t *ZuluSCSISettings::getDevice(uint8_t scsiId)
  391. {
  392. return &m_dev[scsiId];
  393. }
  394. scsi_system_preset_t ZuluSCSISettings::getSystemPreset()
  395. {
  396. return m_sysPreset;
  397. }
  398. const char* ZuluSCSISettings::getSystemPresetName()
  399. {
  400. return systemPresetName[m_sysPreset];
  401. }
  402. scsi_device_preset_t ZuluSCSISettings::getDevicePreset(uint8_t scsiId)
  403. {
  404. return m_devPreset[scsiId];
  405. }
  406. const char* ZuluSCSISettings::getDevicePresetName(uint8_t scsiId)
  407. {
  408. return devicePresetName[m_devPreset[scsiId]];
  409. }