ConfigUtil.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. " <!-- Only set to true when using with a fast SCSI2 host\n " <<
  145. " controller. This can cause problems with older/slower\n" <<
  146. " hardware.-->\n" <<
  147. " <enableScsi2>" <<
  148. (config.flags & CONFIG_ENABLE_SCSI2 ? "true" : "false") <<
  149. "</enableScsi2>\n" <<
  150. "\n" <<
  151. " <!-- ********************************************************\n" <<
  152. " Space separated list. Available options:\n" <<
  153. " apple\t\tReturns Apple-specific mode pages\n" <<
  154. " ********************************************************* -->\n" <<
  155. " <quirks>" <<
  156. (config.quirks & CONFIG_QUIRKS_APPLE ? "apple" : "") <<
  157. "</quirks>\n" <<
  158. "\n\n" <<
  159. " <!-- ********************************************************\n" <<
  160. " 0x0 Fixed hard drive.\n" <<
  161. " 0x1 Removable drive.\n" <<
  162. " 0x2 Optical drive (ie. CD drive).\n" <<
  163. " 0x3 1.44MB Floppy Drive.\n" <<
  164. " ********************************************************* -->\n" <<
  165. " <deviceType>0x" <<
  166. std::hex << static_cast<int>(config.deviceType) <<
  167. "</deviceType>\n" <<
  168. "\n\n" <<
  169. " <!-- ********************************************************\n" <<
  170. " Device type modifier is usually 0x00. Only change this if your\n" <<
  171. " OS requires some special value.\n" <<
  172. "\n" <<
  173. " 0x4C Data General Micropolis disk\n" <<
  174. " ********************************************************* -->\n" <<
  175. " <deviceTypeModifier>0x" <<
  176. std::hex << static_cast<int>(config.deviceTypeModifier) <<
  177. "</deviceTypeModifier>\n" <<
  178. "\n\n" <<
  179. " <!-- ********************************************************\n" <<
  180. " SD card offset, as a sector number (always 512 bytes).\n" <<
  181. " ********************************************************* -->\n" <<
  182. " <sdSectorStart>" << std::dec << config.sdSectorStart << "</sdSectorStart>\n" <<
  183. "\n\n" <<
  184. " <!-- ********************************************************\n" <<
  185. " Drive geometry settings.\n" <<
  186. " ********************************************************* -->\n" <<
  187. "\n"
  188. " <scsiSectors>" << std::dec << config.scsiSectors << "</scsiSectors>\n" <<
  189. " <bytesPerSector>" << std::dec << config.bytesPerSector << "</bytesPerSector>\n" <<
  190. " <sectorsPerTrack>" << std::dec << config.sectorsPerTrack<< "</sectorsPerTrack>\n" <<
  191. " <headsPerCylinder>" << std::dec << config.headsPerCylinder << "</headsPerCylinder>\n" <<
  192. "\n\n" <<
  193. " <!-- ********************************************************\n" <<
  194. " Drive identification information. The SCSI2SD doesn't\n" <<
  195. " care what these are set to. Use these strings to trick a OS\n" <<
  196. " thinking a specific hard drive model is attached.\n" <<
  197. " ********************************************************* -->\n" <<
  198. "\n"
  199. " <!-- 8 character vendor string -->\n" <<
  200. " <!-- For Apple HD SC Setup/Drive Setup, use ' SEAGATE' -->\n" <<
  201. " <vendor>" << std::string(config.vendor, 8) << "</vendor>\n" <<
  202. "\n" <<
  203. " <!-- 16 character produce identifier -->\n" <<
  204. " <!-- For Apple HD SC Setup/Drive Setup, use ' ST225N' -->\n" <<
  205. " <prodId>" << std::string(config.prodId, 16) << "</prodId>\n" <<
  206. "\n" <<
  207. " <!-- 4 character product revision number -->\n" <<
  208. " <!-- For Apple HD SC Setup/Drive Setup, use '1.0 ' -->\n" <<
  209. " <revision>" << std::string(config.revision, 4) << "</revision>\n" <<
  210. "\n" <<
  211. " <!-- 16 character serial number -->\n" <<
  212. " <serial>" << std::string(config.serial, 16) << "</serial>\n" <<
  213. "</SCSITarget>\n";
  214. return s.str();
  215. }
  216. static uint64_t parseInt(wxXmlNode* node, uint64_t limit)
  217. {
  218. std::string str(node->GetNodeContent().mb_str());
  219. if (str.empty())
  220. {
  221. throw std::runtime_error("Empty " + node->GetName());
  222. }
  223. std::stringstream s;
  224. if (str.find("0x") == 0)
  225. {
  226. s << std::hex << str.substr(2);
  227. }
  228. else
  229. {
  230. s << str;
  231. }
  232. uint64_t result;
  233. s >> result;
  234. if (!s)
  235. {
  236. throw std::runtime_error("Invalid value for " + node->GetName());
  237. }
  238. if (result > limit)
  239. {
  240. std::stringstream msg;
  241. msg << "Invalid value for " << node->GetName() <<
  242. " (max=" << limit << ")";
  243. throw std::runtime_error(msg.str());
  244. }
  245. return result;
  246. }
  247. static TargetConfig
  248. parseTarget(wxXmlNode* node)
  249. {
  250. int id;
  251. {
  252. std::stringstream s;
  253. s << node->GetAttribute("id", "7");
  254. s >> id;
  255. if (!s) throw std::runtime_error("Could not parse SCSITarget id attr");
  256. }
  257. TargetConfig result = ConfigUtil::Default(id & 0x7);
  258. wxXmlNode *child = node->GetChildren();
  259. while (child)
  260. {
  261. if (child->GetName() == "enabled")
  262. {
  263. std::string s(child->GetNodeContent().mb_str());
  264. if (s == "true")
  265. {
  266. result.scsiId |= CONFIG_TARGET_ENABLED;
  267. }
  268. else
  269. {
  270. result.scsiId = result.scsiId & ~CONFIG_TARGET_ENABLED;
  271. }
  272. }
  273. else if (child->GetName() == "unitAttention")
  274. {
  275. std::string s(child->GetNodeContent().mb_str());
  276. if (s == "true")
  277. {
  278. result.flags |= CONFIG_ENABLE_UNIT_ATTENTION;
  279. }
  280. else
  281. {
  282. result.flags = result.flags & ~CONFIG_ENABLE_UNIT_ATTENTION;
  283. }
  284. }
  285. else if (child->GetName() == "parity")
  286. {
  287. std::string s(child->GetNodeContent().mb_str());
  288. if (s == "true")
  289. {
  290. result.flags |= CONFIG_ENABLE_PARITY;
  291. }
  292. else
  293. {
  294. result.flags = result.flags & ~CONFIG_ENABLE_PARITY;
  295. }
  296. }
  297. else if (child->GetName() == "enableScsi2")
  298. {
  299. std::string s(child->GetNodeContent().mb_str());
  300. if (s == "true")
  301. {
  302. result.flags |= CONFIG_ENABLE_SCSI2;
  303. }
  304. else
  305. {
  306. result.flags = result.flags & ~CONFIG_ENABLE_SCSI2;
  307. }
  308. }
  309. else if (child->GetName() == "quirks")
  310. {
  311. std::stringstream s(std::string(child->GetNodeContent().mb_str()));
  312. std::string quirk;
  313. while (s >> quirk)
  314. {
  315. if (quirk == "apple")
  316. {
  317. result.quirks |= CONFIG_QUIRKS_APPLE;
  318. }
  319. }
  320. }
  321. else if (child->GetName() == "deviceType")
  322. {
  323. result.deviceType = parseInt(child, 0xFF);
  324. }
  325. else if (child->GetName() == "deviceTypeModifier")
  326. {
  327. result.deviceTypeModifier = parseInt(child, 0xFF);
  328. }
  329. else if (child->GetName() == "sdSectorStart")
  330. {
  331. result.sdSectorStart = parseInt(child, 0xFFFFFFFF);
  332. }
  333. else if (child->GetName() == "scsiSectors")
  334. {
  335. result.scsiSectors = parseInt(child, 0xFFFFFFFF);
  336. }
  337. else if (child->GetName() == "bytesPerSector")
  338. {
  339. result.bytesPerSector = parseInt(child, 8192);
  340. }
  341. else if (child->GetName() == "sectorsPerTrack")
  342. {
  343. result.sectorsPerTrack = parseInt(child, 255);
  344. }
  345. else if (child->GetName() == "headsPerCylinder")
  346. {
  347. result.headsPerCylinder = parseInt(child, 255);
  348. }
  349. else if (child->GetName() == "vendor")
  350. {
  351. std::string s(child->GetNodeContent().mb_str());
  352. s = s.substr(0, sizeof(result.vendor));
  353. memset(result.vendor, ' ', sizeof(result.vendor));
  354. memcpy(result.vendor, s.c_str(), s.size());
  355. }
  356. else if (child->GetName() == "prodId")
  357. {
  358. std::string s(child->GetNodeContent().mb_str());
  359. s = s.substr(0, sizeof(result.prodId));
  360. memset(result.prodId, ' ', sizeof(result.prodId));
  361. memcpy(result.prodId, s.c_str(), s.size());
  362. }
  363. else if (child->GetName() == "revision")
  364. {
  365. std::string s(child->GetNodeContent().mb_str());
  366. s = s.substr(0, sizeof(result.revision));
  367. memset(result.revision, ' ', sizeof(result.revision));
  368. memcpy(result.revision, s.c_str(), s.size());
  369. }
  370. else if (child->GetName() == "serial")
  371. {
  372. std::string s(child->GetNodeContent().mb_str());
  373. s = s.substr(0, sizeof(result.serial));
  374. memset(result.serial, ' ', sizeof(result.serial));
  375. memcpy(result.serial, s.c_str(), s.size());
  376. }
  377. child = child->GetNext();
  378. }
  379. return result;
  380. }
  381. std::vector<TargetConfig>
  382. ConfigUtil::fromXML(const std::string& filename)
  383. {
  384. wxXmlDocument doc;
  385. if (!doc.Load(filename))
  386. {
  387. throw std::runtime_error("Could not load XML file");
  388. }
  389. // start processing the XML file
  390. if (doc.GetRoot()->GetName() != "SCSI2SD")
  391. {
  392. throw std::runtime_error("Invalid root node, expected <SCSI2SD>");
  393. }
  394. std::vector<TargetConfig> result;
  395. wxXmlNode *child = doc.GetRoot()->GetChildren();
  396. while (child)
  397. {
  398. if (child->GetName() == "SCSITarget")
  399. {
  400. result.push_back(parseTarget(child));
  401. }
  402. child = child->GetNext();
  403. }
  404. return result;
  405. }