main.c 9.6 KB

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