scsi2sd-config.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // Copyright (C) 2013 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 "SCSI2SD_HID.hh"
  18. #if __cplusplus >= 201103L
  19. #include <memory>
  20. using std::shared_ptr;
  21. #else
  22. #include <tr1/memory>
  23. using std::tr1::shared_ptr;
  24. #endif
  25. #include <iomanip>
  26. #include <iostream>
  27. #include <sstream>
  28. // Request extended stdio format macros.
  29. #define __STDC_FORMAT_MACROS
  30. #include <inttypes.h>
  31. #include <getopt.h>
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. // htonl/ntohl includes.
  38. #ifdef _WIN32
  39. #include <winsock2.h>
  40. #else
  41. #include <arpa/inet.h>
  42. #endif
  43. #include "hidapi.h"
  44. #define MIN(a,b) (a < b ? a : b)
  45. using namespace SCSI2SD;
  46. enum
  47. {
  48. PARAM_ID,
  49. PARAM_PARITY,
  50. PARAM_NOPARITY,
  51. PARAM_UNITATT,
  52. PARAM_NOUNITATT,
  53. PARAM_MAXBLOCKS,
  54. PARAM_APPLE,
  55. PARAM_VENDOR,
  56. PARAM_PRODID,
  57. PARAM_REV,
  58. PARAM_BYTESPERSECTOR,
  59. PARAM_RESET
  60. };
  61. // Must be consistent with the structure defined in the SCSI2SD config.h header.
  62. // We always transfer data in network byte order.
  63. struct __attribute__((packed)) ConfigPacket
  64. {
  65. uint8_t scsiId;
  66. char vendor[8];
  67. char prodId[16];
  68. char revision[4];
  69. uint8_t enableParity;
  70. uint8_t enableUnitAttention;
  71. uint8_t reserved1; // Unused. Ensures maxBlocks is aligned.
  72. uint32_t maxSectors;
  73. uint16_t bytesPerSector;
  74. // Pad to 64 bytes, which is what we can fit into a USB HID packet.
  75. char reserved[26];
  76. void fromNet()
  77. {
  78. maxSectors = ntohl(maxSectors);
  79. bytesPerSector = ntohs(bytesPerSector);
  80. }
  81. void toNet()
  82. {
  83. maxSectors = htonl(maxSectors);
  84. bytesPerSector = htons(bytesPerSector);
  85. }
  86. void reset()
  87. {
  88. scsiId = 0;
  89. strcpy(vendor, " codesrc");
  90. strcpy(prodId, " SCSI2SD");
  91. strcpy(revision, " 3.5");
  92. enableParity = 1;
  93. enableUnitAttention = 1;
  94. reserved1 = 0;
  95. maxSectors = 0;
  96. bytesPerSector = 512;
  97. }
  98. };
  99. static void printConfig(ConfigPacket* packet)
  100. {
  101. std::cout <<
  102. "SCSI ID:\t\t\t" << static_cast<int>(packet->scsiId) << "\n" <<
  103. "Vendor:\t\t\t\t\"" << std::string(packet->vendor, 8) << "\"\n" <<
  104. "Product ID:\t\t\t\"" << std::string(packet->prodId, 16) << "\"\n" <<
  105. "Revision:\t\t\t\"" << std::string(packet->revision, 4) << "\"\n" <<
  106. "\n" <<
  107. "Parity Checking:\t\t" <<
  108. (packet->enableParity ? "enabled" : "disabled") << "\n" <<
  109. "Unit Attention Condition:\t" <<
  110. (packet->enableUnitAttention ? "enabled" : "disabled") << "\n" <<
  111. "Bytes per sector:\t\t" << packet->bytesPerSector << std::endl;
  112. if (packet->maxSectors)
  113. {
  114. char sizeBuf[64];
  115. uint64_t maxBytes = packet->maxSectors * (uint64_t) packet->bytesPerSector;
  116. if (maxBytes > (1024*1024*1024))
  117. {
  118. sprintf(sizeBuf, "%.02fGB", maxBytes / (1024.0*1024.0*1024.0));
  119. }
  120. else if (maxBytes > (1024*1024))
  121. {
  122. sprintf(sizeBuf, "%.02fMB", maxBytes / (1024.0*1024.0));
  123. }
  124. else if (maxBytes > (1024))
  125. {
  126. sprintf(sizeBuf, "%.02fKB", maxBytes / (1024.0));
  127. }
  128. else
  129. {
  130. sprintf(sizeBuf, "%" PRIu64 " bytes", maxBytes);
  131. }
  132. std::cout <<"Maximum Size:\t\t\t" << sizeBuf <<
  133. " (" << packet->maxSectors << " sectors)" << std::endl;
  134. }
  135. else
  136. {
  137. std::cout << "Maximum Size:\t\t\tUnlimited" << std::endl;
  138. }
  139. }
  140. static void usage()
  141. {
  142. printf("Usage: scsi2sd-config [options...]\n");
  143. printf("\n");
  144. printf("--id={0-7}\tSCSI device ID.\n\n");
  145. printf("--parity\tCheck the SCSI parity signal, and reject data where\n");
  146. printf("\t\tthe parity is bad.\n\n");
  147. printf("--no-parity\tDon't check the SCSI parity signal.\n");
  148. printf("\t\tThis is required for SCSI host controllers that do not provide\n");
  149. printf("\t\tparity.\n\n");
  150. printf("--attention\tRespond with a Unit Attention status on device reset.\n");
  151. printf("\t\tSome systems will fail on this response, even though it is\n");
  152. printf("\t\trequired by the SCSI-2 standard.\n\n");
  153. printf("--no-attention\tDisable Unit Attention responses.\n\n");
  154. printf("--blocks={0-4294967295}\n\t\tSet a limit to the reported device size.\n");
  155. printf("\t\tThe size of each block/sector is set by the --sector parameter.\n");
  156. printf("\t\tThe reported size will be the lower of this value and the SD\n");
  157. printf("\t\tcard size. 0 disables the limit.\n");
  158. printf("\t\tThe maximum possible size is 2TB.\n\n");
  159. printf("--sector={64-8192}\n\t\tSet the bytes-per-sector. Normally 512 bytes.\n");
  160. printf("\t\tCan also be set with a SCSI MODE SELECT command.\n\n");
  161. printf("--apple\t\tSet the vendor, product ID and revision fields to simulate an \n");
  162. printf("\t\tapple-suppled disk. Provides support for the Apple Drive Setup\n");
  163. printf("\t\tutility.\n\n");
  164. printf("--vendor={vendor}\tSets the reported device vendor. Up to 8 characters.\n\n");
  165. printf("--prod-id={prod-id}\tSets the reported product ID. Up to 16 characters.\n\n");
  166. printf("--rev={revision}\tSets the reported device revision. Up to 4 characters.\n\n");
  167. printf("--reset\tRevert all settings to factory defaults.\n\n");
  168. printf("\n");
  169. printf("\nThe current configuration settings are displayed if no options are supplied");
  170. printf("\n\n");
  171. exit(1);
  172. }
  173. int main(int argc, char* argv[])
  174. {
  175. printf("SCSI2SD Configuration Utility.\n");
  176. printf("Copyright (C) 2013 Michael McMaster <michael@codesrc.com>\n\n");
  177. printf(
  178. "USB device parameters\n\tVendor ID:\t0x%04X\n\tProduct ID:\t0x%04X\n",
  179. HID::VENDOR_ID,
  180. HID::PRODUCT_ID);
  181. // Enumerate and print the HID devices on the system
  182. shared_ptr<HID> scsi2sdHID(HID::Open());
  183. if (!scsi2sdHID)
  184. {
  185. fprintf(stderr, "ERROR: SCSI2SD USB device not found.\n");
  186. exit(1);
  187. }
  188. std::stringstream foundMsg;
  189. foundMsg <<
  190. "Device Found\n" <<
  191. " Firmware Version:\t" << scsi2sdHID->getFirmwareVersionStr();
  192. std::cout << foundMsg.str() << std::endl;
  193. ConfigPacket packet;
  194. try
  195. {
  196. scsi2sdHID->readConfig(
  197. reinterpret_cast<uint8_t*>(&packet),
  198. sizeof(packet)
  199. );
  200. packet.fromNet();
  201. }
  202. catch (std::exception& e)
  203. {
  204. std::cerr << "ERROR: Invalid data received from device.\n" <<
  205. e.what() << std::endl;
  206. exit(1);
  207. }
  208. struct option options[] =
  209. {
  210. {
  211. "id", required_argument, NULL, PARAM_ID
  212. },
  213. {
  214. "parity", no_argument, NULL, PARAM_PARITY
  215. },
  216. {
  217. "no-parity", no_argument, NULL, PARAM_NOPARITY
  218. },
  219. {
  220. "attention", no_argument, NULL, PARAM_UNITATT
  221. },
  222. {
  223. "no-attention", no_argument, NULL, PARAM_NOUNITATT
  224. },
  225. {
  226. "blocks", required_argument, NULL, PARAM_MAXBLOCKS
  227. },
  228. {
  229. "apple", no_argument, NULL, PARAM_APPLE
  230. },
  231. {
  232. "vendor", required_argument, NULL, PARAM_VENDOR
  233. },
  234. {
  235. "prod-id", required_argument, NULL, PARAM_PRODID
  236. },
  237. {
  238. "rev", required_argument, NULL, PARAM_REV
  239. },
  240. {
  241. "sector", required_argument, NULL, PARAM_BYTESPERSECTOR
  242. },
  243. {
  244. "reset", no_argument, NULL, PARAM_RESET
  245. },
  246. {
  247. NULL, 0, NULL, 0
  248. }
  249. };
  250. int doWrite = 0;
  251. int optIdx = 0;
  252. int c;
  253. while ((c = getopt_long(argc, argv, "", options, &optIdx)) != -1)
  254. {
  255. doWrite = 1;
  256. switch (c)
  257. {
  258. case PARAM_ID:
  259. {
  260. int id = -1;
  261. if (sscanf(optarg, "%d", &id) == 1 && id >= 0 && id <= 7)
  262. {
  263. packet.scsiId = id;
  264. }
  265. else
  266. {
  267. usage();
  268. }
  269. break;
  270. }
  271. case PARAM_PARITY:
  272. packet.enableParity = 1;
  273. break;
  274. case PARAM_NOPARITY:
  275. packet.enableParity = 0;
  276. break;
  277. case PARAM_UNITATT:
  278. packet.enableUnitAttention = 1;
  279. break;
  280. case PARAM_NOUNITATT:
  281. packet.enableUnitAttention = 0;
  282. break;
  283. case PARAM_MAXBLOCKS:
  284. {
  285. int64_t maxSectors = -1;
  286. if (sscanf(optarg, "%" PRId64, &maxSectors) == 1 &&
  287. maxSectors >= 0 && maxSectors <= 0xffffffff)
  288. {
  289. packet.maxSectors = maxSectors;
  290. }
  291. else
  292. {
  293. usage();
  294. }
  295. break;
  296. }
  297. case PARAM_APPLE:
  298. memcpy(packet.vendor, " SEAGATE", 8);
  299. memcpy(packet.prodId, " ST225N", 16);
  300. memcpy(packet.revision, "1.0 ", 4);
  301. break;
  302. case PARAM_VENDOR:
  303. memset(packet.vendor, ' ', 8);
  304. memcpy(packet.vendor, optarg, MIN(strlen(optarg), 8));
  305. break;
  306. case PARAM_PRODID:
  307. memset(packet.prodId, ' ', 16);
  308. memcpy(packet.prodId, optarg, MIN(strlen(optarg), 16));
  309. break;
  310. case PARAM_REV:
  311. memset(packet.revision, ' ', 4);
  312. memcpy(packet.revision, optarg, MIN(strlen(optarg), 4));
  313. break;
  314. case PARAM_BYTESPERSECTOR:
  315. {
  316. int64_t bytesPerSector = -1;
  317. if (sscanf(optarg, "%" PRId64, &bytesPerSector) == 1 &&
  318. bytesPerSector >= 64 && bytesPerSector <= 8192)
  319. {
  320. packet.bytesPerSector = bytesPerSector;
  321. }
  322. else
  323. {
  324. usage();
  325. }
  326. break;
  327. }
  328. case PARAM_RESET:
  329. packet.reset();
  330. break;
  331. case '?':
  332. doWrite = 0;
  333. usage();
  334. }
  335. }
  336. if (doWrite)
  337. {
  338. printf("\nSaving configuration...");
  339. try
  340. {
  341. packet.toNet();
  342. scsi2sdHID->saveConfig(
  343. reinterpret_cast<uint8_t*>(&packet),
  344. sizeof(packet));
  345. }
  346. catch (std::exception& e)
  347. {
  348. printf(" Fail.\n");
  349. std::cerr << "ERROR: Failed to save config.\n" << e.what() << std::endl;
  350. exit(1);
  351. }
  352. printf(" Done.\n");
  353. #ifdef _WIN32
  354. Sleep(1000); //ms
  355. #else
  356. sleep(1); // Wait for the data to be saved to eeprom
  357. #endif
  358. // Clear outstanding stale data
  359. scsi2sdHID->readConfig(
  360. reinterpret_cast<uint8_t*>(&packet),
  361. sizeof(packet));
  362. // Proper update
  363. scsi2sdHID->readConfig(
  364. reinterpret_cast<uint8_t*>(&packet),
  365. sizeof(packet));
  366. packet.fromNet();
  367. }
  368. printf("\nCurrent Device Settings:\n");
  369. printConfig(&packet);
  370. return 0;
  371. }