scsi2sd-config.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. printf("SCSI ID:\t\t\t%d\n", packet->scsiId);
  102. printf("Vendor:\t\t\t\t\"%.*s\"\n", 8, packet->vendor);
  103. printf("Product ID:\t\t\t\"%.*s\"\n", 16, packet->prodId);
  104. printf("Revision:\t\t\t\"%.*s\"\n", 4, packet->revision);
  105. printf("\n");
  106. printf("Parity Checking:\t\t%s\n", packet->enableParity ? "enabled" : "disabled");
  107. printf("Unit Attention Condition:\t%s\n", packet->enableUnitAttention ? "enabled" : "disabled");
  108. printf("Bytes per sector:\t\t%d\n", packet->bytesPerSector);
  109. if (packet->maxSectors)
  110. {
  111. char sizeBuf[64];
  112. uint64_t maxBytes = packet->maxSectors * (uint64_t) packet->bytesPerSector;
  113. if (maxBytes > (1024*1024*1024))
  114. {
  115. sprintf(sizeBuf, "%.02fGB", maxBytes / (1024.0*1024.0*1024.0));
  116. }
  117. else if (maxBytes > (1024*1024))
  118. {
  119. sprintf(sizeBuf, "%.02fMB", maxBytes / (1024.0*1024.0));
  120. }
  121. else if (maxBytes > (1024))
  122. {
  123. sprintf(sizeBuf, "%.02fKB", maxBytes / (1024.0));
  124. }
  125. else
  126. {
  127. sprintf(sizeBuf, "%" PRIu64 " bytes", maxBytes);
  128. }
  129. printf("Maximum Size:\t\t\t%s (%d sectors)\n", sizeBuf, packet->maxSectors);
  130. }
  131. else
  132. {
  133. printf("Maximum Size:\t\t\tUnlimited\n");
  134. }
  135. }
  136. static void usage()
  137. {
  138. printf("Usage: scsi2sd-config [options...]\n");
  139. printf("\n");
  140. printf("--id={0-7}\tSCSI device ID.\n\n");
  141. printf("--parity\tCheck the SCSI parity signal, and reject data where\n");
  142. printf("\t\tthe parity is bad.\n\n");
  143. printf("--no-parity\tDon't check the SCSI parity signal.\n");
  144. printf("\t\tThis is required for SCSI host controllers that do not provide\n");
  145. printf("\t\tparity.\n\n");
  146. printf("--attention\tRespond with a Unit Attention status on device reset.\n");
  147. printf("\t\tSome systems will fail on this response, even though it is\n");
  148. printf("\t\trequired by the SCSI-2 standard.\n\n");
  149. printf("--no-attention\tDisable Unit Attention responses.\n\n");
  150. printf("--blocks={0-4294967295}\n\t\tSet a limit to the reported device size.\n");
  151. printf("\t\tThe size of each block/sector is set by the --sector parameter.\n");
  152. printf("\t\tThe reported size will be the lower of this value and the SD\n");
  153. printf("\t\tcard size. 0 disables the limit.\n");
  154. printf("\t\tThe maximum possible size is 2TB.\n\n");
  155. printf("--sector={64-8192}\n\t\tSet the bytes-per-sector. Normally 512 bytes.\n");
  156. printf("\t\tCan also be set with a SCSI MODE SELECT command.\n\n");
  157. printf("--apple\t\tSet the vendor, product ID and revision fields to simulate an \n");
  158. printf("\t\tapple-suppled disk. Provides support for the Apple Drive Setup\n");
  159. printf("\t\tutility.\n\n");
  160. printf("--vendor={vendor}\tSets the reported device vendor. Up to 8 characters.\n\n");
  161. printf("--prod-id={prod-id}\tSets the reported product ID. Up to 16 characters.\n\n");
  162. printf("--rev={revision}\tSets the reported device revision. Up to 4 characters.\n\n");
  163. printf("--reset\tRevert all settings to factory defaults.\n\n");
  164. printf("\n");
  165. printf("\nThe current configuration settings are displayed if no options are supplied");
  166. printf("\n\n");
  167. exit(1);
  168. }
  169. int main(int argc, char* argv[])
  170. {
  171. printf("SCSI2SD Configuration Utility.\n");
  172. printf("Copyright (C) 2013 Michael McMaster <michael@codesrc.com>\n\n");
  173. printf(
  174. "USB device parameters\n\tVendor ID:\t0x%04X\n\tProduct ID:\t0x%04X\n",
  175. HID::VENDOR_ID,
  176. HID::PRODUCT_ID);
  177. // Enumerate and print the HID devices on the system
  178. shared_ptr<HID> scsi2sdHID(HID::Open());
  179. if (!scsi2sdHID)
  180. {
  181. fprintf(stderr, "ERROR: SCSI2SD USB device not found.\n");
  182. exit(1);
  183. }
  184. std::stringstream foundMsg;
  185. foundMsg <<
  186. "Device Found\n" <<
  187. " Firmware Version:\t" << scsi2sdHID->getFirmwareVersionStr();
  188. std::cout << foundMsg.str() << std::endl;
  189. ConfigPacket packet;
  190. try
  191. {
  192. scsi2sdHID->readConfig(
  193. reinterpret_cast<uint8_t*>(&packet),
  194. sizeof(packet)
  195. );
  196. packet.fromNet();
  197. }
  198. catch (std::exception& e)
  199. {
  200. std::cerr << "ERROR: Invalid data received from device.\n" <<
  201. e.what() << std::endl;
  202. exit(1);
  203. }
  204. struct option options[] =
  205. {
  206. {
  207. "id", required_argument, NULL, PARAM_ID
  208. },
  209. {
  210. "parity", no_argument, NULL, PARAM_PARITY
  211. },
  212. {
  213. "no-parity", no_argument, NULL, PARAM_NOPARITY
  214. },
  215. {
  216. "attention", no_argument, NULL, PARAM_UNITATT
  217. },
  218. {
  219. "no-attention", no_argument, NULL, PARAM_NOUNITATT
  220. },
  221. {
  222. "blocks", required_argument, NULL, PARAM_MAXBLOCKS
  223. },
  224. {
  225. "apple", no_argument, NULL, PARAM_APPLE
  226. },
  227. {
  228. "vendor", required_argument, NULL, PARAM_VENDOR
  229. },
  230. {
  231. "prod-id", required_argument, NULL, PARAM_PRODID
  232. },
  233. {
  234. "rev", required_argument, NULL, PARAM_REV
  235. },
  236. {
  237. "sector", required_argument, NULL, PARAM_BYTESPERSECTOR
  238. },
  239. {
  240. "reset", no_argument, NULL, PARAM_RESET
  241. },
  242. {
  243. NULL, 0, NULL, 0
  244. }
  245. };
  246. int doWrite = 0;
  247. int optIdx = 0;
  248. int c;
  249. while ((c = getopt_long(argc, argv, "", options, &optIdx)) != -1)
  250. {
  251. doWrite = 1;
  252. switch (c)
  253. {
  254. case PARAM_ID:
  255. {
  256. int id = -1;
  257. if (sscanf(optarg, "%d", &id) == 1 && id >= 0 && id <= 7)
  258. {
  259. packet.scsiId = id;
  260. }
  261. else
  262. {
  263. usage();
  264. }
  265. break;
  266. }
  267. case PARAM_PARITY:
  268. packet.enableParity = 1;
  269. break;
  270. case PARAM_NOPARITY:
  271. packet.enableParity = 0;
  272. break;
  273. case PARAM_UNITATT:
  274. packet.enableUnitAttention = 1;
  275. break;
  276. case PARAM_NOUNITATT:
  277. packet.enableUnitAttention = 0;
  278. break;
  279. case PARAM_MAXBLOCKS:
  280. {
  281. int64_t maxSectors = -1;
  282. if (sscanf(optarg, "%" PRId64, &maxSectors) == 1 &&
  283. maxSectors >= 0 && maxSectors <= UINT32_MAX)
  284. {
  285. packet.maxSectors = maxSectors;
  286. }
  287. else
  288. {
  289. usage();
  290. }
  291. break;
  292. }
  293. case PARAM_APPLE:
  294. memcpy(packet.vendor, " SEAGATE", 8);
  295. memcpy(packet.prodId, " ST225N", 16);
  296. memcpy(packet.revision, "1.0 ", 4);
  297. break;
  298. case PARAM_VENDOR:
  299. memset(packet.vendor, ' ', 8);
  300. memcpy(packet.vendor, optarg, MIN(strlen(optarg), 8));
  301. break;
  302. case PARAM_PRODID:
  303. memset(packet.prodId, ' ', 16);
  304. memcpy(packet.prodId, optarg, MIN(strlen(optarg), 16));
  305. break;
  306. case PARAM_REV:
  307. memset(packet.revision, ' ', 4);
  308. memcpy(packet.revision, optarg, MIN(strlen(optarg), 4));
  309. break;
  310. case PARAM_BYTESPERSECTOR:
  311. {
  312. int64_t bytesPerSector = -1;
  313. if (sscanf(optarg, "%" PRId64, &bytesPerSector) == 1 &&
  314. bytesPerSector >= 64 && bytesPerSector <= 8192)
  315. {
  316. packet.bytesPerSector = bytesPerSector;
  317. }
  318. else
  319. {
  320. usage();
  321. }
  322. break;
  323. }
  324. case PARAM_RESET:
  325. packet.reset();
  326. break;
  327. case '?':
  328. usage();
  329. }
  330. }
  331. if (doWrite)
  332. {
  333. printf("\nSaving configuration...");
  334. try
  335. {
  336. packet.toNet();
  337. scsi2sdHID->saveConfig(
  338. reinterpret_cast<uint8_t*>(&packet),
  339. sizeof(packet));
  340. }
  341. catch (std::exception& e)
  342. {
  343. printf(" Fail.\n");
  344. std::cerr << "ERROR: Failed to save config.\n" << e.what() << std::endl;
  345. exit(1);
  346. }
  347. printf(" Done.\n");
  348. sleep(1); // Wait for the data to be saved to eeprom
  349. // Clear outstanding stale data
  350. scsi2sdHID->readConfig(
  351. reinterpret_cast<uint8_t*>(&packet),
  352. sizeof(packet));
  353. // Proper update
  354. scsi2sdHID->readConfig(
  355. reinterpret_cast<uint8_t*>(&packet),
  356. sizeof(packet));
  357. packet.fromNet();
  358. }
  359. printf("\nCurrent Device Settings:\n");
  360. printConfig(&packet);
  361. return 0;
  362. }