main.c 8.8 KB

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