main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. if (!dev)
  176. {
  177. fprintf(stderr, "ERROR: SCSI2SD USB device not found.\n");
  178. exit(1);
  179. }
  180. printf("USB Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls",
  181. dev->vendor_id, dev->product_id, dev->path, dev->serial_number);
  182. printf("\n");
  183. printf(" Manufacturer: %ls\n", dev->manufacturer_string);
  184. printf(" Product: %ls\n", dev->product_string);
  185. printf("\n");
  186. // Open the device using the VID, PID,
  187. // and optionally the Serial number.
  188. hid_device* handle = hid_open(vendorId, productId, NULL);
  189. if (!handle)
  190. {
  191. fprintf(
  192. stderr,
  193. "ERROR: Could not open device %s. Check permissions.\n", dev->path
  194. );
  195. exit(1);
  196. }
  197. ConfigPacket packet;
  198. if (readConfig(handle, &packet) <= 0)
  199. {
  200. fprintf(stderr, "ERROR: Invalid data received from device.\n");
  201. exit(1);
  202. }
  203. struct option options[] =
  204. {
  205. {
  206. "id", required_argument, NULL, PARAM_ID
  207. },
  208. {
  209. "parity", no_argument, NULL, PARAM_PARITY
  210. },
  211. {
  212. "no-parity", no_argument, NULL, PARAM_NOPARITY
  213. },
  214. {
  215. "attention", no_argument, NULL, PARAM_UNITATT
  216. },
  217. {
  218. "no-attention", no_argument, NULL, PARAM_NOUNITATT
  219. },
  220. {
  221. "blocks", required_argument, NULL, PARAM_MAXBLOCKS
  222. },
  223. {
  224. "apple", no_argument, NULL, PARAM_APPLE
  225. },
  226. {
  227. "vendor", required_argument, NULL, PARAM_VENDOR
  228. },
  229. {
  230. "prod-id", required_argument, NULL, PARAM_PRODID
  231. },
  232. {
  233. "rev", required_argument, NULL, PARAM_REV
  234. },
  235. {
  236. "sector", required_argument, NULL, PARAM_BYTESPERSECTOR
  237. },
  238. {
  239. NULL, 0, NULL, 0
  240. }
  241. };
  242. int doWrite = 0;
  243. int optIdx = 0;
  244. int c;
  245. while ((c = getopt_long(argc, argv, "", options, &optIdx)) != -1)
  246. {
  247. doWrite = 1;
  248. switch (c)
  249. {
  250. case PARAM_ID:
  251. {
  252. int id = -1;
  253. if (sscanf(optarg, "%d", &id) == 1 && id >= 0 && id <= 7)
  254. {
  255. packet.scsiId = id;
  256. }
  257. else
  258. {
  259. usage();
  260. }
  261. break;
  262. }
  263. case PARAM_PARITY:
  264. packet.enableParity = 1;
  265. break;
  266. case PARAM_NOPARITY:
  267. packet.enableParity = 0;
  268. break;
  269. case PARAM_UNITATT:
  270. packet.enableUnitAttention = 1;
  271. break;
  272. case PARAM_NOUNITATT:
  273. packet.enableUnitAttention = 0;
  274. break;
  275. case PARAM_MAXBLOCKS:
  276. {
  277. int64_t maxSectors = -1;
  278. if (sscanf(optarg, "%" PRId64, &maxSectors) == 1 &&
  279. maxSectors >= 0 && maxSectors <= UINT32_MAX)
  280. {
  281. packet.maxSectors = maxSectors;
  282. }
  283. else
  284. {
  285. usage();
  286. }
  287. break;
  288. }
  289. case PARAM_APPLE:
  290. memcpy(packet.vendor, " SEAGATE", 8);
  291. memcpy(packet.prodId, " ST225N", 16);
  292. memcpy(packet.revision, "1.0 ", 4);
  293. break;
  294. case PARAM_VENDOR:
  295. memset(packet.vendor, ' ', 8);
  296. memcpy(packet.vendor, optarg, MIN(strlen(optarg), 8));
  297. break;
  298. case PARAM_PRODID:
  299. memset(packet.prodId, ' ', 16);
  300. memcpy(packet.prodId, optarg, MIN(strlen(optarg), 16));
  301. break;
  302. case PARAM_REV:
  303. memset(packet.revision, ' ', 4);
  304. memcpy(packet.revision, optarg, MIN(strlen(optarg), 4));
  305. break;
  306. case PARAM_BYTESPERSECTOR:
  307. {
  308. int64_t bytesPerSector = -1;
  309. if (sscanf(optarg, "%" PRId64, &bytesPerSector) == 1 &&
  310. bytesPerSector >= 64 && bytesPerSector <= 8192)
  311. {
  312. packet.bytesPerSector = bytesPerSector;
  313. }
  314. else
  315. {
  316. usage();
  317. }
  318. break;
  319. }
  320. case '?':
  321. usage();
  322. }
  323. }
  324. if (doWrite)
  325. {
  326. printf("Saving configuration...");
  327. if (writeConfig(handle, &packet) <= 0)
  328. {
  329. printf(" Fail.\n");
  330. fprintf(stderr, "ERROR: Failed to save config.\n");
  331. exit(1);
  332. }
  333. printf(" Done.\n");
  334. // Clear outstanding stale data
  335. readConfig(handle, &packet);
  336. // Proper update
  337. if (readConfig(handle, &packet) <= 0)
  338. {
  339. fprintf(stderr, "ERROR: Invalid data received from device.\n");
  340. exit(1);
  341. }
  342. }
  343. printf("\nCurrent Device Settings:\n");
  344. printConfig(&packet);
  345. return 0;
  346. }