main.c 9.8 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 <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[26];
  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\tThe size of each block/sector is set by the --sector parameter.\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");
  149. printf("\t\tThe maximum possible size is 2TB.\n\n");
  150. printf("--sector={64-8192}\n\t\tSet the bytes-per-sector. Normally 512 bytes.\n");
  151. printf("\t\tCan also be set with a SCSI MODE SELECT command.\n\n");
  152. printf("--apple\t\tSet the vendor, product ID and revision fields to simulate an \n");
  153. printf("\t\tapple-suppled disk. Provides support for the Apple Drive Setup\n");
  154. printf("\t\tutility.\n\n");
  155. printf("--vendor={vendor}\tSets the reported device vendor. Up to 8 characters.\n\n");
  156. printf("--prod-id={prod-id}\tSets the reported product ID. Up to 16 characters.\n\n");
  157. printf("--rev={revision}\tSets the reported device revision. Up to 4 characters.\n\n");
  158. printf("\n");
  159. printf("\nThe current configuration settings are displayed if no options are supplied");
  160. printf("\n\n");
  161. exit(1);
  162. }
  163. int main(int argc, char* argv[])
  164. {
  165. printf("SCSI2SD Configuration Utility.\n");
  166. printf("Copyright (C) 2013 Michael McMaster <michael@codesrc.com>\n\n");
  167. uint16_t vendorId = 0x04B4; // Cypress
  168. uint16_t productId = 0x1337; // SCSI2SD
  169. printf(
  170. "USB device parameters\n\tVendor ID:\t0x%04X\n\tProduct ID:\t0x%04X\n",
  171. vendorId,
  172. productId);
  173. // Enumerate and print the HID devices on the system
  174. struct hid_device_info *dev = hid_enumerate(vendorId, productId);
  175. while (dev && dev->interface_number != 0)
  176. {
  177. dev = dev->next;
  178. }
  179. if (!dev)
  180. {
  181. fprintf(stderr, "ERROR: SCSI2SD USB device not found.\n");
  182. exit(1);
  183. }
  184. printf("USB Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",
  185. dev->vendor_id, dev->product_id, dev->path, dev->serial_number);
  186. printf("\n");
  187. printf(" Manufacturer: %ls\n", dev->manufacturer_string);
  188. printf(" Product: %ls\n", dev->product_string);
  189. printf("\n");
  190. // Open the device using the VID, PID,
  191. // and optionally the Serial number.
  192. hid_device* handle = hid_open(vendorId, productId, NULL);
  193. if (!handle)
  194. {
  195. fprintf(
  196. stderr,
  197. "ERROR: Could not open device %s. Check permissions.\n", dev->path
  198. );
  199. exit(1);
  200. }
  201. ConfigPacket packet;
  202. if (readConfig(handle, &packet) <= 0)
  203. {
  204. fprintf(stderr, "ERROR: Invalid data received from device.\n");
  205. exit(1);
  206. }
  207. struct option options[] =
  208. {
  209. {
  210. "id", required_argument, NULL, PARAM_ID
  211. },
  212. {
  213. "parity", no_argument, NULL, PARAM_PARITY
  214. },
  215. {
  216. "no-parity", no_argument, NULL, PARAM_NOPARITY
  217. },
  218. {
  219. "attention", no_argument, NULL, PARAM_UNITATT
  220. },
  221. {
  222. "no-attention", no_argument, NULL, PARAM_NOUNITATT
  223. },
  224. {
  225. "blocks", required_argument, NULL, PARAM_MAXBLOCKS
  226. },
  227. {
  228. "apple", no_argument, NULL, PARAM_APPLE
  229. },
  230. {
  231. "vendor", required_argument, NULL, PARAM_VENDOR
  232. },
  233. {
  234. "prod-id", required_argument, NULL, PARAM_PRODID
  235. },
  236. {
  237. "rev", required_argument, NULL, PARAM_REV
  238. },
  239. {
  240. "sector", required_argument, NULL, PARAM_BYTESPERSECTOR
  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 '?':
  325. usage();
  326. }
  327. }
  328. if (doWrite)
  329. {
  330. printf("Saving configuration...");
  331. if (writeConfig(handle, &packet) <= 0)
  332. {
  333. printf(" Fail.\n");
  334. fprintf(stderr, "ERROR: Failed to save config.\n");
  335. exit(1);
  336. }
  337. printf(" Done.\n");
  338. // Clear outstanding stale data
  339. readConfig(handle, &packet);
  340. // Proper update
  341. if (readConfig(handle, &packet) <= 0)
  342. {
  343. fprintf(stderr, "ERROR: Invalid data received from device.\n");
  344. exit(1);
  345. }
  346. }
  347. printf("\nCurrent Device Settings:\n");
  348. printConfig(&packet);
  349. return 0;
  350. }