ConfigUtil.cc 11 KB

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