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