ConfigUtil.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include "ConfigUtil.hh"
  18. #include <limits>
  19. #include <sstream>
  20. #include <stdexcept>
  21. #include <string.h>
  22. #include <wx/xml/xml.h>
  23. using namespace SCSI2SD;
  24. namespace
  25. {
  26. // Endian conversion routines.
  27. // The Cortex-M3 inside the Cypress PSoC 5LP is a
  28. // little-endian device.
  29. bool isHostLE()
  30. {
  31. union
  32. {
  33. int i;
  34. char c[sizeof(int)];
  35. } x;
  36. x.i = 1;
  37. return (x.c[0] == 1);
  38. }
  39. uint16_t toLE16(uint16_t in)
  40. {
  41. if (isHostLE())
  42. {
  43. return in;
  44. }
  45. else
  46. {
  47. return (in >> 8) | (in << 8);
  48. }
  49. }
  50. uint16_t fromLE16(uint16_t in)
  51. {
  52. return toLE16(in);
  53. }
  54. uint32_t toLE32(uint32_t in)
  55. {
  56. if (isHostLE())
  57. {
  58. return in;
  59. }
  60. else
  61. {
  62. return (in >> 24) |
  63. ((in >> 8) & 0xff00) |
  64. ((in << 8) & 0xff0000) |
  65. (in << 24);
  66. }
  67. }
  68. uint32_t fromLE32(uint32_t in)
  69. {
  70. return toLE32(in);
  71. }
  72. }
  73. TargetConfig
  74. ConfigUtil::Default(size_t targetIdx)
  75. {
  76. TargetConfig config;
  77. memset(&config, 0, sizeof(config));
  78. config.scsiId = targetIdx;
  79. if (targetIdx == 0)
  80. {
  81. config.scsiId = config.scsiId | CONFIG_TARGET_ENABLED;
  82. }
  83. config.deviceType = CONFIG_FIXED;
  84. // Default to maximum fail-safe options.
  85. config.flags = 0;
  86. config.deviceTypeModifier = 0;
  87. config.sdSectorStart = 0;
  88. // Default to 2GB. Many systems have trouble with > 2GB disks, and
  89. // a few start to complain at 1GB.
  90. config.scsiSectors = 4194303; // 2GB - 1 sector
  91. config.bytesPerSector = 512;
  92. config.sectorsPerTrack = 63;
  93. config.headsPerCylinder = 255;
  94. memcpy(config.vendor, " codesrc", 8);
  95. memcpy(config.prodId, " SCSI2SD", 16);
  96. memcpy(config.revision, " 4.2", 4);
  97. memcpy(config.serial, "1234567812345678", 16);
  98. // Reserved fields, already set to 0
  99. // config.reserved
  100. // not supported yet.
  101. // config.vpd
  102. return config;
  103. }
  104. TargetConfig
  105. ConfigUtil::fromBytes(const uint8_t* data)
  106. {
  107. TargetConfig result;
  108. memcpy(&result, data, sizeof(TargetConfig));
  109. result.sdSectorStart = toLE32(result.sdSectorStart);
  110. result.scsiSectors = toLE32(result.scsiSectors);
  111. result.bytesPerSector = toLE16(result.bytesPerSector);
  112. result.sectorsPerTrack = toLE16(result.sectorsPerTrack);
  113. result.headsPerCylinder = toLE16(result.headsPerCylinder);
  114. return result;
  115. }
  116. std::vector<uint8_t>
  117. ConfigUtil::toBytes(const TargetConfig& _config)
  118. {
  119. TargetConfig config(_config);
  120. config.sdSectorStart = fromLE32(config.sdSectorStart);
  121. config.scsiSectors = fromLE32(config.scsiSectors);
  122. config.bytesPerSector = fromLE16(config.bytesPerSector);
  123. config.sectorsPerTrack = fromLE16(config.sectorsPerTrack);
  124. config.headsPerCylinder = fromLE16(config.headsPerCylinder);
  125. const uint8_t* begin = reinterpret_cast<const uint8_t*>(&config);
  126. return std::vector<uint8_t>(begin, begin + sizeof(config));
  127. }
  128. std::string
  129. ConfigUtil::toXML(const TargetConfig& config)
  130. {
  131. std::stringstream s;
  132. s <<
  133. "<SCSITarget id=\"" <<
  134. static_cast<int>(config.scsiId & CONFIG_TARGET_ID_BITS) << "\">\n" <<
  135. " <enabled>" <<
  136. (config.scsiId & CONFIG_TARGET_ENABLED ? "true" : "false") <<
  137. "</enabled>\n" <<
  138. " <unitAttention>" <<
  139. (config.flags & CONFIG_ENABLE_UNIT_ATTENTION ? "true" : "false") <<
  140. "</unitAttention>\n" <<
  141. " <parity>" <<
  142. (config.flags & CONFIG_ENABLE_PARITY ? "true" : "false") <<
  143. "</parity>\n" <<
  144. " <!-- ********************************************************\n" <<
  145. " Only set to true when using with a fast SCSI2 host\n " <<
  146. " controller. This can cause problems with older/slower\n" <<
  147. " hardware.\n" <<
  148. " ********************************************************* -->\n" <<
  149. " <enableScsi2>" <<
  150. (config.flags & CONFIG_ENABLE_SCSI2 ? "true" : "false") <<
  151. "</enableScsi2>\n" <<
  152. " <!-- ********************************************************\n" <<
  153. " Setting to 'true' will result in increased performance at the\n" <<
  154. " cost of lower noise immunity.\n" <<
  155. " Only set to true when using short cables with only 1 or two\n" <<
  156. " devices. This should remain off when using external SCSI1 DB25\n" <<
  157. " cables.\n" <<
  158. " ********************************************************* -->\n" <<
  159. " <disableGlitchFilter>" <<
  160. (config.flags & CONFIG_DISABLE_GLITCH ? "true" : "false") <<
  161. "</disableGlitchFilter>\n" <<
  162. "\n" <<
  163. " <!-- ********************************************************\n" <<
  164. " Space separated list. Available options:\n" <<
  165. " apple\t\tReturns Apple-specific mode pages\n" <<
  166. " ********************************************************* -->\n" <<
  167. " <quirks>" <<
  168. (config.quirks & CONFIG_QUIRKS_APPLE ? "apple" : "") <<
  169. "</quirks>\n" <<
  170. "\n\n" <<
  171. " <!-- ********************************************************\n" <<
  172. " 0x0 Fixed hard drive.\n" <<
  173. " 0x1 Removable drive.\n" <<
  174. " 0x2 Optical drive (ie. CD drive).\n" <<
  175. " 0x3 1.44MB Floppy Drive.\n" <<
  176. " ********************************************************* -->\n" <<
  177. " <deviceType>0x" <<
  178. std::hex << static_cast<int>(config.deviceType) <<
  179. "</deviceType>\n" <<
  180. "\n\n" <<
  181. " <!-- ********************************************************\n" <<
  182. " Device type modifier is usually 0x00. Only change this if your\n" <<
  183. " OS requires some special value.\n" <<
  184. "\n" <<
  185. " 0x4C Data General Micropolis disk\n" <<
  186. " ********************************************************* -->\n" <<
  187. " <deviceTypeModifier>0x" <<
  188. std::hex << static_cast<int>(config.deviceTypeModifier) <<
  189. "</deviceTypeModifier>\n" <<
  190. "\n\n" <<
  191. " <!-- ********************************************************\n" <<
  192. " SD card offset, as a sector number (always 512 bytes).\n" <<
  193. " ********************************************************* -->\n" <<
  194. " <sdSectorStart>" << std::dec << config.sdSectorStart << "</sdSectorStart>\n" <<
  195. "\n\n" <<
  196. " <!-- ********************************************************\n" <<
  197. " Drive geometry settings.\n" <<
  198. " ********************************************************* -->\n" <<
  199. "\n"
  200. " <scsiSectors>" << std::dec << config.scsiSectors << "</scsiSectors>\n" <<
  201. " <bytesPerSector>" << std::dec << config.bytesPerSector << "</bytesPerSector>\n" <<
  202. " <sectorsPerTrack>" << std::dec << config.sectorsPerTrack<< "</sectorsPerTrack>\n" <<
  203. " <headsPerCylinder>" << std::dec << config.headsPerCylinder << "</headsPerCylinder>\n" <<
  204. "\n\n" <<
  205. " <!-- ********************************************************\n" <<
  206. " Drive identification information. The SCSI2SD doesn't\n" <<
  207. " care what these are set to. Use these strings to trick a OS\n" <<
  208. " thinking a specific hard drive model is attached.\n" <<
  209. " ********************************************************* -->\n" <<
  210. "\n"
  211. " <!-- 8 character vendor string -->\n" <<
  212. " <!-- For Apple HD SC Setup/Drive Setup, use ' SEAGATE' -->\n" <<
  213. " <vendor>" << std::string(config.vendor, 8) << "</vendor>\n" <<
  214. "\n" <<
  215. " <!-- 16 character produce identifier -->\n" <<
  216. " <!-- For Apple HD SC Setup/Drive Setup, use ' ST225N' -->\n" <<
  217. " <prodId>" << std::string(config.prodId, 16) << "</prodId>\n" <<
  218. "\n" <<
  219. " <!-- 4 character product revision number -->\n" <<
  220. " <!-- For Apple HD SC Setup/Drive Setup, use '1.0 ' -->\n" <<
  221. " <revision>" << std::string(config.revision, 4) << "</revision>\n" <<
  222. "\n" <<
  223. " <!-- 16 character serial number -->\n" <<
  224. " <serial>" << std::string(config.serial, 16) << "</serial>\n" <<
  225. "</SCSITarget>\n";
  226. return s.str();
  227. }
  228. static uint64_t parseInt(wxXmlNode* node, uint64_t limit)
  229. {
  230. std::string str(node->GetNodeContent().mb_str());
  231. if (str.empty())
  232. {
  233. throw std::runtime_error("Empty " + node->GetName());
  234. }
  235. std::stringstream s;
  236. if (str.find("0x") == 0)
  237. {
  238. s << std::hex << str.substr(2);
  239. }
  240. else
  241. {
  242. s << str;
  243. }
  244. uint64_t result;
  245. s >> result;
  246. if (!s)
  247. {
  248. throw std::runtime_error("Invalid value for " + node->GetName());
  249. }
  250. if (result > limit)
  251. {
  252. std::stringstream msg;
  253. msg << "Invalid value for " << node->GetName() <<
  254. " (max=" << limit << ")";
  255. throw std::runtime_error(msg.str());
  256. }
  257. return result;
  258. }
  259. static TargetConfig
  260. parseTarget(wxXmlNode* node)
  261. {
  262. int id;
  263. {
  264. std::stringstream s;
  265. s << node->GetAttribute("id", "7");
  266. s >> id;
  267. if (!s) throw std::runtime_error("Could not parse SCSITarget id attr");
  268. }
  269. TargetConfig result = ConfigUtil::Default(id & 0x7);
  270. wxXmlNode *child = node->GetChildren();
  271. while (child)
  272. {
  273. if (child->GetName() == "enabled")
  274. {
  275. std::string s(child->GetNodeContent().mb_str());
  276. if (s == "true")
  277. {
  278. result.scsiId |= CONFIG_TARGET_ENABLED;
  279. }
  280. else
  281. {
  282. result.scsiId = result.scsiId & ~CONFIG_TARGET_ENABLED;
  283. }
  284. }
  285. else if (child->GetName() == "unitAttention")
  286. {
  287. std::string s(child->GetNodeContent().mb_str());
  288. if (s == "true")
  289. {
  290. result.flags |= CONFIG_ENABLE_UNIT_ATTENTION;
  291. }
  292. else
  293. {
  294. result.flags = result.flags & ~CONFIG_ENABLE_UNIT_ATTENTION;
  295. }
  296. }
  297. else if (child->GetName() == "parity")
  298. {
  299. std::string s(child->GetNodeContent().mb_str());
  300. if (s == "true")
  301. {
  302. result.flags |= CONFIG_ENABLE_PARITY;
  303. }
  304. else
  305. {
  306. result.flags = result.flags & ~CONFIG_ENABLE_PARITY;
  307. }
  308. }
  309. else if (child->GetName() == "enableScsi2")
  310. {
  311. std::string s(child->GetNodeContent().mb_str());
  312. if (s == "true")
  313. {
  314. result.flags |= CONFIG_ENABLE_SCSI2;
  315. }
  316. else
  317. {
  318. result.flags = result.flags & ~CONFIG_ENABLE_SCSI2;
  319. }
  320. }
  321. else if (child->GetName() == "disableGlitchFilter")
  322. {
  323. std::string s(child->GetNodeContent().mb_str());
  324. if (s == "true")
  325. {
  326. result.flags |= CONFIG_DISABLE_GLITCH;
  327. }
  328. else
  329. {
  330. result.flags = result.flags & ~CONFIG_DISABLE_GLITCH;
  331. }
  332. }
  333. else if (child->GetName() == "quirks")
  334. {
  335. std::stringstream s(std::string(child->GetNodeContent().mb_str()));
  336. std::string quirk;
  337. while (s >> quirk)
  338. {
  339. if (quirk == "apple")
  340. {
  341. result.quirks |= CONFIG_QUIRKS_APPLE;
  342. }
  343. }
  344. }
  345. else if (child->GetName() == "deviceType")
  346. {
  347. result.deviceType = parseInt(child, 0xFF);
  348. }
  349. else if (child->GetName() == "deviceTypeModifier")
  350. {
  351. result.deviceTypeModifier = parseInt(child, 0xFF);
  352. }
  353. else if (child->GetName() == "sdSectorStart")
  354. {
  355. result.sdSectorStart = parseInt(child, 0xFFFFFFFF);
  356. }
  357. else if (child->GetName() == "scsiSectors")
  358. {
  359. result.scsiSectors = parseInt(child, 0xFFFFFFFF);
  360. }
  361. else if (child->GetName() == "bytesPerSector")
  362. {
  363. result.bytesPerSector = parseInt(child, 8192);
  364. }
  365. else if (child->GetName() == "sectorsPerTrack")
  366. {
  367. result.sectorsPerTrack = parseInt(child, 255);
  368. }
  369. else if (child->GetName() == "headsPerCylinder")
  370. {
  371. result.headsPerCylinder = parseInt(child, 255);
  372. }
  373. else if (child->GetName() == "vendor")
  374. {
  375. std::string s(child->GetNodeContent().mb_str());
  376. s = s.substr(0, sizeof(result.vendor));
  377. memset(result.vendor, ' ', sizeof(result.vendor));
  378. memcpy(result.vendor, s.c_str(), s.size());
  379. }
  380. else if (child->GetName() == "prodId")
  381. {
  382. std::string s(child->GetNodeContent().mb_str());
  383. s = s.substr(0, sizeof(result.prodId));
  384. memset(result.prodId, ' ', sizeof(result.prodId));
  385. memcpy(result.prodId, s.c_str(), s.size());
  386. }
  387. else if (child->GetName() == "revision")
  388. {
  389. std::string s(child->GetNodeContent().mb_str());
  390. s = s.substr(0, sizeof(result.revision));
  391. memset(result.revision, ' ', sizeof(result.revision));
  392. memcpy(result.revision, s.c_str(), s.size());
  393. }
  394. else if (child->GetName() == "serial")
  395. {
  396. std::string s(child->GetNodeContent().mb_str());
  397. s = s.substr(0, sizeof(result.serial));
  398. memset(result.serial, ' ', sizeof(result.serial));
  399. memcpy(result.serial, s.c_str(), s.size());
  400. }
  401. child = child->GetNext();
  402. }
  403. return result;
  404. }
  405. std::vector<TargetConfig>
  406. ConfigUtil::fromXML(const std::string& filename)
  407. {
  408. wxXmlDocument doc;
  409. if (!doc.Load(filename))
  410. {
  411. throw std::runtime_error("Could not load XML file");
  412. }
  413. // start processing the XML file
  414. if (doc.GetRoot()->GetName() != "SCSI2SD")
  415. {
  416. throw std::runtime_error("Invalid root node, expected <SCSI2SD>");
  417. }
  418. std::vector<TargetConfig> result;
  419. wxXmlNode *child = doc.GetRoot()->GetChildren();
  420. while (child)
  421. {
  422. if (child->GetName() == "SCSITarget")
  423. {
  424. result.push_back(parseTarget(child));
  425. }
  426. child = child->GetNext();
  427. }
  428. return result;
  429. }