scsi2sd-config.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #include <iomanip>
  19. #include <iostream>
  20. #include <memory>
  21. #include <sstream>
  22. // Request extended stdio format macros.
  23. #define __STDC_FORMAT_MACROS
  24. #include <inttypes.h>
  25. #include <getopt.h>
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. // htonl/ntohl includes.
  32. #ifdef _WIN32
  33. #include <winsock2.h>
  34. #else
  35. #include <arpa/inet.h>
  36. #endif
  37. #include "hidapi.h"
  38. #define MIN(a,b) (a < b ? a : b)
  39. using namespace SCSI2SD;
  40. enum
  41. {
  42. PARAM_ID,
  43. PARAM_PARITY,
  44. PARAM_NOPARITY,
  45. PARAM_UNITATT,
  46. PARAM_NOUNITATT,
  47. PARAM_MAXBLOCKS,
  48. PARAM_APPLE,
  49. PARAM_VENDOR,
  50. PARAM_PRODID,
  51. PARAM_REV,
  52. PARAM_BYTESPERSECTOR,
  53. PARAM_RESET
  54. };
  55. // Must be consistent with the structure defined in the SCSI2SD config.h header.
  56. // We always transfer data in network byte order.
  57. struct __attribute__((packed)) ConfigPacket
  58. {
  59. uint8_t scsiId;
  60. char vendor[8];
  61. char prodId[16];
  62. char revision[4];
  63. uint8_t enableParity;
  64. uint8_t enableUnitAttention;
  65. uint8_t reserved1; // Unused. Ensures maxBlocks is aligned.
  66. uint32_t maxSectors;
  67. uint16_t bytesPerSector;
  68. // Pad to 64 bytes, which is what we can fit into a USB HID packet.
  69. char reserved[26];
  70. void fromNet()
  71. {
  72. maxSectors = ntohl(maxSectors);
  73. bytesPerSector = ntohs(bytesPerSector);
  74. }
  75. void toNet()
  76. {
  77. maxSectors = htonl(maxSectors);
  78. bytesPerSector = htons(bytesPerSector);
  79. }
  80. void reset()
  81. {
  82. scsiId = 0;
  83. strcpy(vendor, " codesrc");
  84. strcpy(prodId, " SCSI2SD");
  85. strcpy(revision, " 3.5");
  86. enableParity = 1;
  87. enableUnitAttention = 1;
  88. reserved1 = 0;
  89. maxSectors = 0;
  90. bytesPerSector = 512;
  91. }
  92. };
  93. static void printConfig(ConfigPacket* packet)
  94. {
  95. printf("SCSI ID:\t\t\t%d\n", packet->scsiId);
  96. printf("Vendor:\t\t\t\t\"%.*s\"\n", 8, packet->vendor);
  97. printf("Product ID:\t\t\t\"%.*s\"\n", 16, packet->prodId);
  98. printf("Revision:\t\t\t\"%.*s\"\n", 4, packet->revision);
  99. printf("\n");
  100. printf("Parity Checking:\t\t%s\n", packet->enableParity ? "enabled" : "disabled");
  101. printf("Unit Attention Condition:\t%s\n", packet->enableUnitAttention ? "enabled" : "disabled");
  102. printf("Bytes per sector:\t\t%d\n", packet->bytesPerSector);
  103. if (packet->maxSectors)
  104. {
  105. char sizeBuf[64];
  106. uint64_t maxBytes = packet->maxSectors * (uint64_t) packet->bytesPerSector;
  107. if (maxBytes > (1024*1024*1024))
  108. {
  109. sprintf(sizeBuf, "%.02fGB", maxBytes / (1024.0*1024.0*1024.0));
  110. }
  111. else if (maxBytes > (1024*1024))
  112. {
  113. sprintf(sizeBuf, "%.02fMB", maxBytes / (1024.0*1024.0));
  114. }
  115. else if (maxBytes > (1024))
  116. {
  117. sprintf(sizeBuf, "%.02fKB", maxBytes / (1024.0));
  118. }
  119. else
  120. {
  121. sprintf(sizeBuf, "%" PRIu64 " bytes", maxBytes);
  122. }
  123. printf("Maximum Size:\t\t\t%s (%d sectors)\n", sizeBuf, packet->maxSectors);
  124. }
  125. else
  126. {
  127. printf("Maximum Size:\t\t\tUnlimited\n");
  128. }
  129. }
  130. static void usage()
  131. {
  132. printf("Usage: scsi2sd-config [options...]\n");
  133. printf("\n");
  134. printf("--id={0-7}\tSCSI device ID.\n\n");
  135. printf("--parity\tCheck the SCSI parity signal, and reject data where\n");
  136. printf("\t\tthe parity is bad.\n\n");
  137. printf("--no-parity\tDon't check the SCSI parity signal.\n");
  138. printf("\t\tThis is required for SCSI host controllers that do not provide\n");
  139. printf("\t\tparity.\n\n");
  140. printf("--attention\tRespond with a Unit Attention status on device reset.\n");
  141. printf("\t\tSome systems will fail on this response, even though it is\n");
  142. printf("\t\trequired by the SCSI-2 standard.\n\n");
  143. printf("--no-attention\tDisable Unit Attention responses.\n\n");
  144. printf("--blocks={0-4294967295}\n\t\tSet a limit to the reported device size.\n");
  145. printf("\t\tThe size of each block/sector is set by the --sector parameter.\n");
  146. printf("\t\tThe reported size will be the lower of this value and the SD\n");
  147. printf("\t\tcard size. 0 disables the limit.\n");
  148. printf("\t\tThe maximum possible size is 2TB.\n\n");
  149. printf("--sector={64-8192}\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("--reset\tRevert all settings to factory defaults.\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. printf(
  168. "USB device parameters\n\tVendor ID:\t0x%04X\n\tProduct ID:\t0x%04X\n",
  169. HID::VENDOR_ID,
  170. HID::PRODUCT_ID);
  171. // Enumerate and print the HID devices on the system
  172. std::shared_ptr<HID> scsi2sdHID(HID::Open());
  173. if (!scsi2sdHID)
  174. {
  175. fprintf(stderr, "ERROR: SCSI2SD USB device not found.\n");
  176. exit(1);
  177. }
  178. std::stringstream foundMsg;
  179. foundMsg <<
  180. "Device Found\n" <<
  181. " Firmware Version:\t" << scsi2sdHID->getFirmwareVersionStr();
  182. std::cout << foundMsg.str() << std::endl;
  183. ConfigPacket packet;
  184. try
  185. {
  186. scsi2sdHID->readConfig(
  187. reinterpret_cast<uint8_t*>(&packet),
  188. sizeof(packet)
  189. );
  190. packet.fromNet();
  191. }
  192. catch (std::exception& e)
  193. {
  194. std::cerr << "ERROR: Invalid data received from device.\n" <<
  195. e.what() << std::endl;
  196. exit(1);
  197. }
  198. struct option options[] =
  199. {
  200. {
  201. "id", required_argument, NULL, PARAM_ID
  202. },
  203. {
  204. "parity", no_argument, NULL, PARAM_PARITY
  205. },
  206. {
  207. "no-parity", no_argument, NULL, PARAM_NOPARITY
  208. },
  209. {
  210. "attention", no_argument, NULL, PARAM_UNITATT
  211. },
  212. {
  213. "no-attention", no_argument, NULL, PARAM_NOUNITATT
  214. },
  215. {
  216. "blocks", required_argument, NULL, PARAM_MAXBLOCKS
  217. },
  218. {
  219. "apple", no_argument, NULL, PARAM_APPLE
  220. },
  221. {
  222. "vendor", required_argument, NULL, PARAM_VENDOR
  223. },
  224. {
  225. "prod-id", required_argument, NULL, PARAM_PRODID
  226. },
  227. {
  228. "rev", required_argument, NULL, PARAM_REV
  229. },
  230. {
  231. "sector", required_argument, NULL, PARAM_BYTESPERSECTOR
  232. },
  233. {
  234. "reset", no_argument, NULL, PARAM_RESET
  235. },
  236. {
  237. NULL, 0, NULL, 0
  238. }
  239. };
  240. int doWrite = 0;
  241. int optIdx = 0;
  242. int c;
  243. while ((c = getopt_long(argc, argv, "", options, &optIdx)) != -1)
  244. {
  245. doWrite = 1;
  246. switch (c)
  247. {
  248. case PARAM_ID:
  249. {
  250. int id = -1;
  251. if (sscanf(optarg, "%d", &id) == 1 && id >= 0 && id <= 7)
  252. {
  253. packet.scsiId = id;
  254. }
  255. else
  256. {
  257. usage();
  258. }
  259. break;
  260. }
  261. case PARAM_PARITY:
  262. packet.enableParity = 1;
  263. break;
  264. case PARAM_NOPARITY:
  265. packet.enableParity = 0;
  266. break;
  267. case PARAM_UNITATT:
  268. packet.enableUnitAttention = 1;
  269. break;
  270. case PARAM_NOUNITATT:
  271. packet.enableUnitAttention = 0;
  272. break;
  273. case PARAM_MAXBLOCKS:
  274. {
  275. int64_t maxSectors = -1;
  276. if (sscanf(optarg, "%" PRId64, &maxSectors) == 1 &&
  277. maxSectors >= 0 && maxSectors <= UINT32_MAX)
  278. {
  279. packet.maxSectors = maxSectors;
  280. }
  281. else
  282. {
  283. usage();
  284. }
  285. break;
  286. }
  287. case PARAM_APPLE:
  288. memcpy(packet.vendor, " SEAGATE", 8);
  289. memcpy(packet.prodId, " ST225N", 16);
  290. memcpy(packet.revision, "1.0 ", 4);
  291. break;
  292. case PARAM_VENDOR:
  293. memset(packet.vendor, ' ', 8);
  294. memcpy(packet.vendor, optarg, MIN(strlen(optarg), 8));
  295. break;
  296. case PARAM_PRODID:
  297. memset(packet.prodId, ' ', 16);
  298. memcpy(packet.prodId, optarg, MIN(strlen(optarg), 16));
  299. break;
  300. case PARAM_REV:
  301. memset(packet.revision, ' ', 4);
  302. memcpy(packet.revision, optarg, MIN(strlen(optarg), 4));
  303. break;
  304. case PARAM_BYTESPERSECTOR:
  305. {
  306. int64_t bytesPerSector = -1;
  307. if (sscanf(optarg, "%" PRId64, &bytesPerSector) == 1 &&
  308. bytesPerSector >= 64 && bytesPerSector <= 8192)
  309. {
  310. packet.bytesPerSector = bytesPerSector;
  311. }
  312. else
  313. {
  314. usage();
  315. }
  316. break;
  317. }
  318. case PARAM_RESET:
  319. packet.reset();
  320. break;
  321. case '?':
  322. usage();
  323. }
  324. }
  325. if (doWrite)
  326. {
  327. printf("\nSaving configuration...");
  328. try
  329. {
  330. packet.toNet();
  331. scsi2sdHID->saveConfig(
  332. reinterpret_cast<uint8_t*>(&packet),
  333. sizeof(packet));
  334. }
  335. catch (std::exception& e)
  336. {
  337. printf(" Fail.\n");
  338. std::cerr << "ERROR: Failed to save config.\n" << e.what() << std::endl;
  339. exit(1);
  340. }
  341. printf(" Done.\n");
  342. sleep(1); // Wait for the data to be saved to eeprom
  343. // Clear outstanding stale data
  344. scsi2sdHID->readConfig(
  345. reinterpret_cast<uint8_t*>(&packet),
  346. sizeof(packet));
  347. // Proper update
  348. scsi2sdHID->readConfig(
  349. reinterpret_cast<uint8_t*>(&packet),
  350. sizeof(packet));
  351. packet.fromNet();
  352. }
  353. printf("\nCurrent Device Settings:\n");
  354. printConfig(&packet);
  355. return 0;
  356. }