-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbstoolbox.c
820 lines (732 loc) · 20.4 KB
/
bstoolbox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/*
* BlueSCSI v2 IRIX tools
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/dsreq.h>
#include <invent.h>
#define SCSI_INQUIRY 0x12
#define BLUESCSI_TOOLBOX_COUNT_FILES 0xD2
#define BLUESCSI_TOOLBOX_MODE_FILES 0xD0
#define BLUESCSI_TOOLBOX_GET_FILE 0xD1
#define BLUESCSI_TOOLBOX_SEND_FILE_PREP 0xD3
#define BLUESCSI_TOOLBOX_SEND_FILE_10 0xD4
#define BLUESCSI_TOOLBOX_SEND_FILE_END 0xD5
#define BLUESCSI_TOOLBOX_TOGGLE_DEBUG 0xD6
#define BLUESCSI_TOOLBOX_MODE_CDS 0xD7
#define BLUESCSI_TOOLBOX_SET_NEXT_CD 0xD8
#define BLUESCSI_TOOLBOX_MODE_DEVICES 0xD9
#define BLUESCSI_TOOLBOX_COUNT_CDS 0xDA
#define OPEN_RETRO_SCSI_TOO_MANY_FILES 0x0001
#define MAX_FILES 100 //TODO Hmmmm
#define MAX_DATA_LEN 4096
enum {
TYPE_NONE = 255,
TYPE_HDD = 0x00,
TYPE_CD = 0x02
} dev_type;
enum {
MODE_NONE,
MODE_CD,
MODE_SHARED,
MODE_PUT,
MODE_INQUIRY,
MODE_DEBUG
};
enum {
PRINT_OFF,
PRINT_ON
};
enum {
DEBUG_SET,
DEBUG_GET
};
int verbose;
typedef struct {
unsigned char dev_type; // Peripheral device type (bits 4-7), Peripheral qualifier (bits 0-3)
unsigned char dev_type_mod; //RMB (bit 7), Device-type modifier (bits 0-6)
unsigned char version; //SCSI version ID
unsigned char add_length; //Additional length in bytes
char reserved[3];
char vendor_id[9];
char product_id[17];
char product_rev[33];
} scsi_inquiry;
typedef struct {
unsigned char index; /* byte 00: file index in directory */
unsigned char type; /* byte 01: type 0 = file, 1 = directory */
char name[33]; /* byte 02-34: filename (32 byte max) + space for NUL terminator */
unsigned char size[5]; /* byte 35-39: file size (40 bit big endian unsigned) */
} ToolboxFileEntry;
static int scsi_open(char *path)
{
return open(path, O_RDWR | O_SYNC);
}
static int scsi_close(int dev)
{
return close(dev);
}
static int scsi_send_command(int dev, char *cmd, int cmd_len, char *buf, int buf_len)
{
int i;
int try;
dsreq_t r;
memset(&r, 0, sizeof(dsreq_t));
/* Assemble the request structure */
r.ds_cmdbuf = (caddr_t) cmd;
r.ds_cmdlen = cmd_len;
r.ds_databuf = (caddr_t) buf;
r.ds_datalen = buf_len;
//r.ids_sensebuf = (caddr_t) buf;
//r.ds_senselen = buf_len;
r.ds_sensebuf = NULL;
r.ds_senselen = 0;
r.ds_time = 5 * 1000; /* 5 seconds should be enough */
r.ds_flags = DSRQ_READ;
if (verbose){
fprintf(stdout, "Sending SCSI command: ");
for (i = 0; i < cmd_len; ++i) {
fprintf(stdout, "%02x ", (unsigned char)r.ds_cmdbuf[i]);
}
fprintf(stdout, "\n");
}
/* Issue the request */
//if (ioctl(dev, DS_ENTER, &r))
// return -errno;
for (try = 0; try < 10; try ++){
if (ioctl(dev, DS_ENTER, &r) < 0 || r.ds_status != 0){
fprintf(stderr, "WARNING: SCSI command timed out (%d); retrying...\n", r.ds_status);
sleep(try + 1);
}
else
break;
if (try >= 10){
fprintf(stderr, "ERROR: Unable to send print data (%d)\n",r.ds_status);
return (1);
}
}
return 0;
}
//TODO Document this or just do it better
static int scsi_send_commandw(int dev, char *cmd, int cmd_len, char *buf, int buf_len)
{
int i;
dsreq_t r;
memset(&r, 0, sizeof(dsreq_t));
/* Assemble the request structure */
r.ds_cmdbuf = (caddr_t) cmd;
r.ds_cmdlen = cmd_len;
r.ds_databuf = (caddr_t) buf;
r.ds_datalen = buf_len;
//r.ids_sensebuf = (caddr_t) buf;
//r.ds_senselen = buf_len;
r.ds_sensebuf = NULL;
r.ds_senselen = 0;
r.ds_time = 5 * 1000; /* 5 seconds should be enough */
r.ds_flags = DSRQ_WRITE;
if (verbose){
fprintf(stdout, "Sending SCSI command: ");
for (i = 0; i < cmd_len; ++i) {
fprintf(stdout, "%02x ", (unsigned char)r.ds_cmdbuf[i]);
}
fprintf(stdout, "\n");
}
/* Issue the request */
if (ioctl(dev, DS_ENTER, &r))
return -errno;
return 0;
}
#define SEND_BUF_SIZE 512
#define NAME_BUF_SIZE 33
static int bluescsi_sendfile (int dev, char *path)
{
char cmd[10] = {BLUESCSI_TOOLBOX_SEND_FILE_PREP, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char filename[NAME_BUF_SIZE];
char *base_name;
char send_buf[SEND_BUF_SIZE]; //Send buffer is 512 bytes
long int bytes_read = 0;
long int bytes_left = 0;
int buf_idx= 0; //offset in 512 byte chunks
int ret;
FILE *fd;
long int filesize;
struct stat st; //Struct to get filesize
if (verbose)
fprintf (stdout, "sendfile: %s\n", path);
memset(send_buf, 0, sizeof(send_buf));
// Extract filename from path
base_name = strrchr(path, '/');
if (base_name == NULL) {
base_name = path; // No '/' found, the path is the filename
} else {
base_name++; // Move past '/'
}
// Ensure filename fits in buffer
if (strlen(base_name) >= NAME_BUF_SIZE) {
fprintf(stderr, "Error: sendfile Filename too long: %s\n", base_name);
return -1;
}
strncpy(filename, base_name, NAME_BUF_SIZE);
filename[NAME_BUF_SIZE - 1] = '\0'; // Ensure null-termination
fd = fopen(path, "rb");
if ( fd == NULL)
{
fprintf (stderr, "Error: sendfile couldn't open %s\n", path);
return 1;
}
// Use stat to get file size
if (stat(path, &st) == 0) {
if (verbose)
printf("File size of %s is %lld bytes\n", filename, (long long)st.st_size);
} else {
fprintf (stderr, "Error: sendfile couldn't stat %s\n", path);
fclose(fd);
return 1;
}
filesize = st.st_size;
//Send the name as data
if (scsi_send_commandw(dev, cmd, sizeof(cmd), filename, MAX_DATA_LEN) != 0)
{
fprintf (stderr, "Error: sendfileprep failed - %s\n", strerror(errno));
fclose(fd);
return 1;
}
//Construct the command and start sending the file
cmd[0] = BLUESCSI_TOOLBOX_SEND_FILE_10;
while (bytes_read < filesize){
if (bytes_left < SEND_BUF_SIZE)
{
bytes_read += fread(send_buf, 1, bytes_left, fd);
cmd[1] = (bytes_left & 0xFF00) >> 8;
cmd[2] = (bytes_left & 0xFF);
}
else
{
cmd[1] = (sizeof(send_buf) & 0xFF00) >> 8;
cmd[2] = (sizeof(send_buf) & 0xFF);
bytes_read += fread(send_buf, 1, sizeof(send_buf), fd);
}
cmd[3] = (buf_idx & 0xFF0000) >> 16;
cmd[4] = (buf_idx & 0xFF00) >> 8;
cmd[5] = (buf_idx & 0xFF);
if (bytes_left < SEND_BUF_SIZE)
ret = scsi_send_commandw(dev, cmd, sizeof(cmd), send_buf, bytes_left );
else
ret = scsi_send_commandw(dev, cmd, sizeof(cmd), send_buf, sizeof(send_buf));
if (ret != 0)
{
fprintf (stderr, "Error: sendfile10 failed - %s\n", strerror(errno));
fclose(fd);
return 1;
}
buf_idx++;
bytes_left = filesize - bytes_read;
}
memset (cmd, 0, sizeof(cmd));
cmd[0] = BLUESCSI_TOOLBOX_SEND_FILE_END;
if (scsi_send_command(dev, cmd, sizeof(cmd), NULL, 0) != 0)
{
fprintf (stderr, "Error: sendfileend failed - %s\n", strerror(errno));
fclose(fd);
return 1;
}
fclose(fd);
return 0;
}
//Check and set debug status
static int bluescsi_getdebug (int dev)
{
int ret;
char cmd[10] = {BLUESCSI_TOOLBOX_TOGGLE_DEBUG, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char buf[1];
cmd[1] = DEBUG_GET;//Get debug flag
memset(buf, 0, sizeof(buf));
if (scsi_send_command(dev, cmd, sizeof(cmd), buf, sizeof(buf)) != 0)
{
fprintf (stderr, "Error: getdebug failed - %s\n", strerror(errno));
return -1;
}
ret = buf[0];
return ret;
}
static int bluescsi_setdebug (int dev, int value)
{
char cmd[10] = {BLUESCSI_TOOLBOX_TOGGLE_DEBUG, 0, 0, 0, 0, 0, 0, 0, 0, 0};
if (value > 1)
value = 1;
else if (value < 0)
value = 0;
cmd[1] = DEBUG_SET;
cmd[2] = value;
if (scsi_send_command(dev, cmd, sizeof(cmd), NULL, 0) != 0)
{
fprintf (stderr, "Error: BlueSCSI setdebug failed - %s\n", strerror(errno));
return -1;
}
if (verbose)
fprintf (stdout, "Debug mode set to: %i\n", bluescsi_getdebug (dev));
return 0;
}
//Returns the number of files in the /shared directory
static int bluescsi_countfiles(int dev)
{
char cmd[10] = {BLUESCSI_TOOLBOX_COUNT_FILES, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char buf[1];
int ret;
memset(buf, 0, sizeof(buf));
if (scsi_send_command(dev, cmd, sizeof(cmd), buf, sizeof(buf)) != 0)
{
fprintf (stderr, "Error: countfiles failed - %s\n", strerror(errno));
return -1;
}
ret = buf[0]; //Maximum of 100 files
return ret;
}
/** TOOLBOX_COUNT_CDS (read, length 10)
* Input:
* CDB 00 = command byte
* Output:
* Single byte indicating number of CD images available. (Max 100.)
*/
static int bluescsi_countcds(int dev)
{
char cmd[10] = {BLUESCSI_TOOLBOX_COUNT_CDS, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char buf[1];
int ret;
memset(buf, 0, sizeof(buf));
if (scsi_send_command(dev, cmd, sizeof(cmd), buf, sizeof(buf)) != 0)
{
fprintf (stderr, "Error: countcds failed - %s\n", strerror(errno));
return -1;
}
ret = buf[0]; //Maximum of 100 files
if (ret < 0 || ret > MAX_FILES)
{
fprintf (stderr,"Error: countcds invalid count %i\n", ret);
return -1;
}
return ret;
}
/** TOOLBOX_SET_NEXT_CD (read, length 10)
* Input:
* CDB 00 = command byte
* CDB 01 = image file index byte to change to
* Output:
* None.
*/
static int bluescsi_setnextcd(int dev, int num)
{
int max_cds;
char cmd[10];
memset(cmd, 0, sizeof(cmd));
max_cds = bluescsi_countcds(dev);
cmd[0] = BLUESCSI_TOOLBOX_SET_NEXT_CD;
if (num < 0 | num > max_cds)
{
fprintf (stderr, "setnextcd: %i is out of range of max %i\n", num, max_cds);
return 1;
}
cmd[1] = num;
if (verbose)
fprintf (stdout, "%i set as next CD\n", cmd[1]);
if (scsi_send_command(dev, cmd, 10, NULL, 0) != 0)
{
fprintf (stderr, "Error: setnextcd failed - %s\n", strerror(errno));
return -1;
}
return 0;
}
static int bluescsi_listcds(int dev)
{
char cmd[10] = {BLUESCSI_TOOLBOX_MODE_CDS, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char *buf;
int i, j;
int buf_size;
int num_cds;
num_cds = bluescsi_countcds (dev);
if (num_cds == 0 || num_cds > MAX_FILES)
{
fprintf (stderr, "Error: CD number requested invalid, is device a CD?: %i\n", num_cds);
return -1;
}
fprintf (stdout, "Found %i CDs\n", num_cds);
buf_size = sizeof(ToolboxFileEntry);
buf_size = buf_size * num_cds;
buf = (char *)malloc(buf_size);
memset(buf, 0, sizeof(buf));
if (scsi_send_command(dev, cmd, sizeof(cmd), buf, buf_size) != 0)
{
fprintf (stderr, "Error: listcds failed - %s\n", strerror(errno));
return -1;
}
j = 0;
for (i=0;i<buf_size;i++)
{
if (j == 0 )
fprintf (stdout, "#%i ", (buf[i]));
if (j >= 2 && j <= 34)
fprintf (stdout, "%c", buf[i]);
j++;
if (j >= sizeof(ToolboxFileEntry))
{
j = 0;
fprintf (stdout, "\n");
}
// fprintf (stdout, "%02x",buf[i]);
}
fprintf (stdout, "\n");
return 1;
}
ToolboxFileEntry files[MAX_FILES];
int files_count;
static long int size_to_long(const unsigned char size[5])
{
int i;
long int result = 0;
for (i = 0; i < 5; i++)
{
result = (result << 8) | size[i];
}
return result;
}
static int bluescsi_listfiles(int dev, int print)
{
char cmd[10] = {BLUESCSI_TOOLBOX_MODE_FILES, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char *buf;
int i;
int buf_size;
int num_files;
if (verbose)
fprintf (stdout, "Listing files on %i\n", dev);
num_files = bluescsi_countfiles (dev);
if (num_files == 0 || num_files > MAX_FILES)
{
fprintf (stderr, "Error: listfiles num_files invalid: %i", num_files);
return -1;
}
files_count = num_files;
if (verbose)
fprintf (stdout, "Found %i files\n", num_files);
buf_size = sizeof(ToolboxFileEntry);
buf_size = buf_size * num_files;
buf = (char *)malloc(buf_size);
memset(buf, 0, sizeof(buf));
if (scsi_send_command(dev, cmd, sizeof(cmd), buf, buf_size) != 0)
{
fprintf (stderr, "Error: listfiles failed - %s\n", strerror(errno));
return -1;
}
//Copy SCSI data to global files var
for (i = 0; i < num_files; i++) {
memcpy(&files[i], buf + i * sizeof(ToolboxFileEntry), sizeof(ToolboxFileEntry));
}
if (verbose || print)
{
for (i = 0;i < num_files;i++)
fprintf (stdout, "#%i %s %li bytes\n", files[i].index, files[i].name, size_to_long(files[i].size));
}
return 0;
}
//Get file from /shared directory, grabbed in chunks of 4096
static int bluescsi_getfile(int dev, int idx, char *outdir)
{
char cmd[10];
char buf[MAX_DATA_LEN];
FILE *fd;
char *filename;
size_t bytes_written;
size_t bytes_left;
int blk_idx = 0;
memset(cmd, 0, sizeof(cmd));
cmd[0] = BLUESCSI_TOOLBOX_GET_FILE;
cmd[1] = idx;
cmd[2] = 0; //Index offset in MAX_DATA_LEN blocks
if (strlen (outdir) < 2)//Default to current directory
strcpy (outdir, "./");
//We need to populate the files struct first before doing anything else
if (bluescsi_listfiles (dev, 0) != 0)
{
fprintf (stderr, "Error: getfile couldn't listfiles\n");
return -1;
}
if (verbose)
fprintf (stdout, "getfile :#%i %s %li bytes\n", files[idx].index, files[idx].name, size_to_long(files[idx].size));
filename = malloc (strlen(outdir) + strlen(files[idx].name));
strcpy (filename, outdir);
strcat (filename, files[idx].name);
if (verbose)
fprintf (stdout, "Writing to file %s\n", filename);
fd = fopen(filename, "wb");
if (fd == NULL)
{
fprintf (stderr, "Error: getfile couldn't open %s\n", filename);
return -1;
}
memset(buf, 0, sizeof(buf));
bytes_left = 0;
bytes_written = 0;
//Read the data from the SCSI bus and store to disk
//TODO timeouts and sanity checks
while (1)
{
if (scsi_send_command(dev, cmd, sizeof(cmd), buf, sizeof(buf)) != 0)
{
fprintf (stderr, "Error: getfile failed during transfer - %s\n", strerror(errno));
fclose (fd);
return -1;
}
bytes_left = size_to_long (files[idx].size) - bytes_written;
if (verbose)
fprintf (stdout, "Bytes left: %li\n", bytes_left);
if (bytes_left <= 0)
{
if (verbose)
fprintf (stdout, "Transfer of %s complete\n", filename);
break;
}
//Check to see if we are on the last chunk
if (bytes_left < MAX_DATA_LEN)
{
bytes_written += fwrite (buf, sizeof(unsigned char), bytes_left, fd);
break;
}
//Otherwise write the chunk and move onto the next one
bytes_written += fwrite (buf, sizeof(unsigned char), MAX_DATA_LEN, fd);
//increment the offset
blk_idx++;
cmd[2] = (unsigned char)(blk_idx >> 24) & 0xFF;
cmd[3] = (unsigned char)(blk_idx >> 16) & 0xFF;
cmd[4] = (unsigned char)(blk_idx >> 8) & 0xFF;
cmd[5] = (unsigned char)(blk_idx ) & 0xFF;
}
fclose (fd);
return 0;
}
/** TOOLBOX_MODE_DEVICES (read, length 10)
* Input:
* CDB 00 = command byte
* Output:
* 8 bytes, each indicating the device type of the emulated SCSI devices
* or 0xFF for not-enabled targets
*/
static int bluescsi_listdevices(int dev, char **outbuf)
{
char cmd[10] = {BLUESCSI_TOOLBOX_MODE_DEVICES, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char buf[8];
*outbuf = NULL;
memset(buf, 0, sizeof(buf));
if (scsi_send_command(dev, cmd, sizeof(cmd), buf, sizeof(buf)) != 0)
{
fprintf (stderr, "Error: BlueSCSI listdevices failed - %s\n", strerror(errno));
return -1;
}
*outbuf = (char *)calloc(sizeof(buf), sizeof(char));
if (*outbuf) {
memcpy(*outbuf, buf, sizeof(buf));
}
return 0;
}
//Returns zero on success
static int bluescsi_inquiry(int dev, int print)
{
char cmd[] ={SCSI_INQUIRY, 0, 0, 0, sizeof(scsi_inquiry), 0};
char buf[sizeof(scsi_inquiry)];
const char *BlueSCSI_ID = "BlueSCSI";
scsi_inquiry inq;
memset(buf, 0, sizeof(buf));
if (scsi_send_command(dev, cmd, sizeof(cmd), buf, sizeof(buf)) != 0)
{
fprintf (stderr, "Error: inquiry command failed - %s\n", strerror(errno));
return 1;
}
memset (&inq, 0, sizeof(scsi_inquiry));
memcpy (&inq.version, &buf[2], 1);
memcpy (&inq.vendor_id, &buf[8], sizeof(inq.vendor_id) - 1);
inq.vendor_id[9] = '\0';
memcpy (&inq.product_id, &buf[16], sizeof(inq.product_id) - 1);
inq.product_id[17] = '\0';
memcpy (&inq.product_rev, &buf[32], sizeof(inq.product_rev) - 1);
inq.product_rev[33] = '\0';
if (verbose || print)
{
fprintf (stdout, "SCSI version: %i\n", inq.version);
fprintf (stdout, "vendor_id: %s \nproduct_id: %s\n", inq.vendor_id, inq.product_id);
fprintf (stdout, "product_rev: %s\n", inq.product_rev);
fprintf (stdout, "debug mode: %i\n", bluescsi_getdebug(dev));
}
//TODO Once a BlueSCSI drive is found, send a MODE SENSE 0x1A command for page 0x31. Validate it against the BlueSCSIVendorPage (see: mode.c)
if (strstr (inq.product_rev, BlueSCSI_ID) != NULL)
return 0;
else
{
fprintf (stderr, "Error: didn't find ID %s in product_rev\n", BlueSCSI_ID);
return 2;
}
}
static void do_drive(char *path, int list, int verbose, int cd_img, int file, char *outdir)
{
int dev;
int dev_path_num; //SCSI ID pulled from path
int type[8];
int i;
char *inq = NULL;
dev = scsi_open(path);
if (dev < 0) {
fprintf(stderr, "ERROR: Cannot open device: %s\n", strerror(errno));
exit(1);
}
if (verbose)
printf("Opened dev %i %s:\n", dev, path);
//Do inquiry to check we are working with a BlueSCSI
if (bluescsi_inquiry (dev, PRINT_OFF) != 0)
{
fprintf (stderr, "Didn't find a BlueSCSI device at %s\n", path);
scsi_close (dev);
exit(1);
}
//Next double check what kind of device we are emulating
if (bluescsi_listdevices(dev, &inq) == 0) {
if (verbose)
fprintf (stdout, "List device flags: ");
for (i = 0; i < 8;i++)
{
type[i] = inq[i];
if (verbose)
fprintf (stdout, "%02x ", inq[i]);
}
if (verbose)
fprintf (stdout, "\n");
free(inq);
}
// Extract the device number from the path
// TODO This will probably break very easily
if (sscanf(path, "/dev/scsi/sc%*dd%dl%*d", &dev_path_num) != 1) {
fprintf(stderr, "ERROR: Invalid path format: %s\n", path);
scsi_close(dev);
return;
}
if (verbose)
fprintf(stdout, "dev_path_num %i\n", dev_path_num);
if (list == MODE_CD)
bluescsi_listcds(dev);
else if (list == MODE_INQUIRY)
bluescsi_inquiry(dev, PRINT_ON);
else if (list == MODE_DEBUG)
bluescsi_setdebug(dev, file);
else if (list == MODE_SHARED)
bluescsi_listfiles(dev, PRINT_ON);
else if (list == MODE_PUT)
bluescsi_sendfile (dev, outdir);
else if (file != -1)
bluescsi_getfile (dev, file, outdir);
else if (cd_img != -1)
{
if (type[dev_path_num] != TYPE_CD)
fprintf (stderr, "Device doesn't seem to be a CD drive?\n");
else
bluescsi_setnextcd(dev, cd_img);
}
scsi_close(dev);
}
static void usage(void)
{
fprintf(stderr, "\nUsage: bstoolbox [options] [device]\n\n");
fprintf(stderr, "Example: bstoolbox -s /dev/scsi/sc0d1l0\n\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, "\t-h : display this help message and exit\n");
fprintf(stderr, "\t-v : be verbose\n");
fprintf(stderr, "\t-i : interrogate BlueSCSI and return version\n");
fprintf(stderr, "\t-l : list available CDs\n");
fprintf(stderr, "\t-s : List /shared directory\n");
fprintf(stderr, "\t-c num : change to CD number (1, 2, etc)\n");
fprintf(stderr, "\t-g num : get file from shared directory (1, 2, etc)\n");
fprintf(stderr, "\t-p file : put file to shared directory\n");
fprintf(stderr, "\t-o dir : set output directory, defaults to current\n");
fprintf(stderr, "\t-d num : set debug mode (0 = off, 1 - on)\n");
fprintf(stderr, "\n\nPlease make sure you run the program as root.\n");
}
static int mediad_start() {
int status;
// Starting mediad service
if (verbose)
fprintf (stdout, "Starting mediad...\n");
status = system("/etc/init.d/mediad start");
if (status != 0) {
fprintf(stderr, "Failed to start mediad service: %s\n", strerror(errno));
return 1;
}
return 0;
}
static int mediad_stop() {
int status;
// Stop mediad service
if (verbose)
fprintf (stdout, "Stopping mediad...\n");
status = system("/etc/init.d/mediad stop");
if (status != 0) {
fprintf(stderr, "Failed to stop mediad service: %s\n", strerror(errno));
return 1;
}
return 0;
}
int main(int argc, char *argv[])
{
int c, cdimg = -1, list = 0, file = -1;
char outdir[1024];
while ((c = getopt(argc, argv, "hvlsicd:g:o:p:")) != -1) switch (c) {
case 'c':
cdimg = atoi(optarg);
break;
case 'g':
file = atoi(optarg);
break;
case 'o':
strncpy(outdir, optarg, sizeof(outdir) - 1);
break;
case 'p':
strncpy(outdir, optarg, sizeof(outdir) - 1);
list = MODE_PUT;
break;
case 'l':
list = MODE_CD;
break;
case 's':
list = MODE_SHARED;
break;
case 'i':
list = MODE_INQUIRY;
break;
case 'd':
list = MODE_DEBUG;
file = atoi(optarg);
break;
case 'v':
verbose = 1;
break;
case 'h':
default:
usage();
return 1;
}
argc -= optind;
argv += optind;
if (cdimg != -1)
mediad_stop (); //Stop the mediad before changing image
if (argc < 1) {
fprintf (stderr, "Please specify device (\"/dev/scsi/...\"\n");
usage();
return 0;
} else if (argc > 1) {
fprintf(stderr, "WARNING: Options after '%s' ignored.\n", argv[0]);
}
do_drive(argv[0], list, verbose, cdimg, file, outdir);
if (cdimg != -1)
mediad_start ();
return 0;
}