scsi2sd-config.cc 9.2 KB

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