-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
io.c
executable file
·11324 lines (10062 loc) · 349 KB
/
io.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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2015-2022 Scott Dwyer.
// You may use/distribute/modify this freely, under the terms of
// the GNU General Public License version 2 or later version.
// This software is distributed WITHOUT ANY WARRANTY.
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "common.h"
#include "io.h"
#include "clone_gui_common.h"
unsigned char io_byte_ccc[16];
uint8_t io_singlebyte_ccc;
uint16_t io_word_ccc;
uint32_t io_doubleword_ccc;
uint64_t io_quadword_ccc;
uint8_t start_bit_ccc;
int command_status_ccc;
int identify_flag_ccc;
char error_string_ccc[255];
int lsblk_ccc = 1;
void *hba_virt_addr_ccc;
int performing_reset_ccc = 0;
int did_hard_reset_ccc = 0;
int did_power_cycle_ccc = 0;
int words_per_logical_sector_ccc = 256;
int bytes_per_logical_sector_ccc = 512;
char driver_device_name_ccc[64];
bool check_message_ccc;
char *title_ccc;
char virtual_driver_name_ccc[MAX_DEVICE_NAME_LENGTH];
struct sg_io_hdr io_hdr;
bool use_fpdma_ccc;
bool wait_for_ds_bit_ccc;
int driver_running_ccc;
char curlang_ccc[LANGCOUNT][MAXLANGLENGTH];
char *version_number_ccc;
#include "strncpy_wrapper.h"
// function to prepare CDB
int prepare_cdb_ccc(void)
{
memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = command_length_ccc;
io_hdr.cmdp = passthrough_ccc.scsi_cmd;
io_hdr.dxfer_direction = passthrough_ccc.sg_xfer_direction;
io_hdr.dxfer_len = ccc_main_buffer_size_ccc;
io_hdr.dxferp = ccc_buffer_ccc;
io_hdr.mx_sb_len = sizeof(sense_buf_ccc);
io_hdr.iovec_count = 0;
io_hdr.sbp = sense_buf_ccc;
io_hdr.timeout = general_timeout_ccc / 1000; // timeout in ms
io_hdr.flags = SG_FLAG_DIRECT_IO;
io_hdr.pack_id = 0;
io_hdr.usr_ptr = 0;
sense_key_ccc = 0;
asc_ccc = 0;
ascq_ccc = 0;
if (verbose_ccc & DEBUG7)
{
fprintf (stdout, "CDB: ");
int i;
for (i = 0; i < command_length_ccc; i++)
{
fprintf (stdout, "%02x ", passthrough_ccc.scsi_cmd[i]);
}
fprintf (stdout, "\n");
}
if (debug_ccc & DEBUG7)
{
fprintf (debug_file_ccc, "CDB: ");
int i;
for (i = 0; i < command_length_ccc; i++)
{
fprintf (debug_file_ccc, "%02x ", passthrough_ccc.scsi_cmd[i]);
}
fprintf (debug_file_ccc, "\n");
}
return (0);
}
#define sensebuf ((unsigned char*)io_hdr.sbp)
// function post ioctl
int post_ioctl_ccc(void)
{
int fixed = 0;
int descriptor = 0;
// if there was sense data written then check for errors
if (io_hdr.sb_len_wr != 0)
{
switch (sensebuf[0])
{
case 0xF0:
case 0xF1:
sense_key_ccc = sensebuf[2] & 0x0f;
asc_ccc = sensebuf[12];
ascq_ccc = sensebuf[13];
fixed = 1;
break;
case 0x70:
case 0x71:
sense_key_ccc = sensebuf[2] & 0x0f;
asc_ccc = sensebuf[12];
ascq_ccc = sensebuf[13];
break;
case 0x72:
case 0x73:
sense_key_ccc = sensebuf[1] & 0x0f;
asc_ccc = sensebuf[2];
ascq_ccc = sensebuf[3];
descriptor = 1;
break;
default:
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "\nbad (SG_IO) sense buffer response code from input device\n");
if (superclone_ccc)
{
message_error_ccc(tempmessage_ccc);
}
else
{
message_now_ccc(tempmessage_ccc);
}
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "raw sense buffer: ");
if (superclone_ccc)
{
message_error_ccc(tempmessage_ccc);
}
else
{
message_now_ccc(tempmessage_ccc);
}
int i;
for (i = 0; i < io_hdr.sb_len_wr; i++)
{
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "%02x ", sensebuf[i]);
if (superclone_ccc)
{
message_error_ccc(tempmessage_ccc);
}
else
{
message_now_ccc(tempmessage_ccc);
}
}
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "\n");
if (superclone_ccc)
{
message_error_ccc(tempmessage_ccc);
print_gui_error_message_ccc(error_message_ccc, curlang_ccc[LANGERROR], 1);
clear_error_message_ccc();
}
else
{
message_now_ccc(tempmessage_ccc);
}
return INPUT_DEVICE_ERROR_RETURN_CODE;
}
}
set_number_variable_value_ccc("$io_sense_key", sense_key_ccc);
set_number_variable_value_ccc("$io_asc", asc_ccc);
set_number_variable_value_ccc("$io_ascq", ascq_ccc);
set_number_variable_value_ccc("$io_status", io_hdr.status);
set_number_variable_value_ccc("$io_masked_status", io_hdr.masked_status);
set_number_variable_value_ccc("$io_msg_status", io_hdr.msg_status);
set_number_variable_value_ccc("$io_sb_len_wr", io_hdr.sb_len_wr);
set_number_variable_value_ccc("$io_host_status", io_hdr.host_status);
set_number_variable_value_ccc("$io_driver_status", io_hdr.driver_status);
set_number_variable_value_ccc("$io_resid", io_hdr.resid);
set_number_variable_value_ccc("$io_duration", io_hdr.duration);
set_number_variable_value_ccc("$io_info", io_hdr.info);
io_status_ccc = io_hdr.status;
io_masked_status_ccc = io_hdr.masked_status;
msg_status_ccc = io_hdr.msg_status;
io_sb_len_wr_ccc = io_hdr.sb_len_wr;
io_host_status_ccc = io_hdr.host_status;
io_driver_status_ccc = io_hdr.driver_status;
io_resid_ccc = io_hdr.resid;
io_duration_ccc = io_hdr.duration;
io_info_ccc = io_hdr.info;
memset(sensebuffer_ccc, 0, sensebuffer_size_ccc);
memcpy(sensebuffer_ccc, io_hdr.sbp, io_hdr.sb_len_wr);
if (descriptor && io_hdr.sb_len_wr > 21 && sensebuf[8] == 0x9 && sensebuf[9] == 0xc)
{
//the length of the sense buffer is specified by 1 byte, just allocate the max sense buffer size
unsigned char ata_return_descriptor[256];
unsigned char additional_sense_length = sensebuf[7];
memset(ata_return_descriptor, 0, sizeof(ata_return_descriptor));
memcpy(ata_return_descriptor, &sensebuf[8], additional_sense_length);
set_number_variable_value_ccc("$ata_return_error", ata_return_descriptor[3]);
int count = (ata_return_descriptor[4] << 8) + ata_return_descriptor[5];
set_number_variable_value_ccc("$ata_return_count", count);
long long lba = ((unsigned long long)ata_return_descriptor[10] << 48) + ((unsigned long long)ata_return_descriptor[8] << 32) + ((unsigned long long)ata_return_descriptor[6] << 24) + ((unsigned long long)ata_return_descriptor[11] << 16) + ((unsigned long long)ata_return_descriptor[9] << 8) + ata_return_descriptor[7];
set_number_variable_value_ccc("$ata_return_lba", lba);
set_number_variable_value_ccc("$ata_return_device", ata_return_descriptor[12]);
set_number_variable_value_ccc("$ata_return_status", ata_return_descriptor[13]);
memcpy(&ata_error_ccc, &ata_return_descriptor[3], 1);
memcpy(&ata_status_ccc, &ata_return_descriptor[13], 1);
memcpy(&ata_device_ccc, &ata_return_descriptor[12], 1);
ata_lba_ccc = lba;
ata_count_ccc = count;
ata_return_valid_ccc = 1;
}
else if (fixed && io_hdr.sb_len_wr > 17)
{
unsigned char information[4];
int i;
for (i = 0; i < 4; i ++)
{
memcpy(&information[i], &sensebuf[i+3], 1);
}
unsigned char command_specific_information[4];
for (i = 0; i < 4; i ++)
{
memcpy(&command_specific_information[i], &sensebuf[i+8], 1);
}
set_number_variable_value_ccc("$ata_return_error", information[0]);
set_number_variable_value_ccc("$ata_return_status", information[1]);
set_number_variable_value_ccc("$ata_return_device", information[2]);
set_number_variable_value_ccc("$ata_return_count", information[3]);
long long lba = ((unsigned long long)command_specific_information[1] << 16) + ((unsigned long long)command_specific_information[2] << 8) + command_specific_information[3];
set_number_variable_value_ccc("$ata_return_lba", lba);
memcpy(&ata_error_ccc, &information[0], 1);
memcpy(&ata_status_ccc, &information[1], 1);
memcpy(&ata_device_ccc, &information[2], 1);
memcpy(&ata_count_ccc, &information[3], 1);
ata_lba_ccc = lba;
ata_return_valid_ccc = 1;
}
else
{
set_number_variable_value_ccc("$ata_return_error", 0);
set_number_variable_value_ccc("$ata_return_count", 0);
set_number_variable_value_ccc("$ata_return_lba", 0);
set_number_variable_value_ccc("$ata_return_device", 0);
set_number_variable_value_ccc("$ata_return_status", 0);
ata_error_ccc = 0;
ata_status_ccc = 0;
ata_lba_ccc = 0;
ata_count_ccc = 0;
ata_device_ccc = 0;
ata_return_valid_ccc = 0;
}
//fprintf(stdout, "error=%02x status=%02x sensekey=%02x asc=%02x ascq=%02x\n", ata_error_ccc, ata_status_ccc, sense_key_ccc, asc_ccc, ascq_ccc); //debug
if (0)
{
int i;
for (i = 0; i < io_hdr.sb_len_wr; i++)
{
fprintf (stdout, "%02x ", sensebuf[i]);
}
fprintf (stdout, "\n");
fprintf (stdout, "io_status_ccc %d\n", io_status_ccc);
fprintf (stdout, "io_masked_status_ccc %d\n", io_masked_status_ccc);
fprintf (stdout, "msg_status_ccc %d\n", msg_status_ccc);
fprintf (stdout, "io_sb_len_wr_ccc %d\n", io_sb_len_wr_ccc);
fprintf (stdout, "io_host_status_ccc %d\n", io_host_status_ccc);
fprintf (stdout, "io_driver_status_ccc %d\n", io_driver_status_ccc);
fprintf (stdout, "io_resid_ccc %d\n", io_resid_ccc);
fprintf (stdout, "io_duration_ccc %d\n", io_duration_ccc);
fprintf (stdout, "io_info_ccc %d\n", io_info_ccc);
}
return (0);
}
int do_scsi_cmd_ccc(int disk_fd)
{
if (usb_mode_ccc)
{
unsigned long long start_time = get_elapsed_usec_ccc();
unsigned long long elapsed_time;
int usb_timeout_bak = usb_timeout_ccc;
usb_timeout_ccc = soft_reset_time_ccc / 1000;
int need_reset = 0;
int inquiry_good = 0;
int fail_level = 0;
int ret = do_usb_cmd_ccc();
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
usb_read_residue_ccc = usb_csw_residue_ccc;
// good read
if (ret == (int)ccc_main_buffer_size_ccc && usb_csw_valid_ccc && !usb_csw_residue_ccc && !usb_csw_status_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return 0;
}
// bad read
else if (usb_csw_valid_ccc && usb_csw_status_ccc == 1)
{
if (usb_get_sense_ccc() != USB_SENSE_BUFFER_SIZE)
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
fail_level = 0x1;
need_reset = 1;
}
else
{
if (sense_key_ccc > 1)
{
usb_timeout_ccc = usb_timeout_bak;
return 0;
}
else
{
usb_timeout_ccc = usb_timeout_bak;
return 1;
}
}
}
// csw status phase error 0x02 or other unknown condition
if (usb_csw_valid_ccc && usb_csw_status_ccc > 1)
{
fail_level = 0x10;
if (!usb_reset_recovery_ccc(usb_timeout_ccc))
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
if (!usb_inquiry_ccc(usb_timeout_ccc) && !usb_check_capacity_ccc(usb_timeout_ccc))
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
inquiry_good = 1;
}
else
{
need_reset = 1;
}
}
else
{
need_reset = 1;
}
}
// other read conditions
if (ret < 0)
{
// check if timeout
if (ret == -110)
{
fail_level = 0x30;
need_reset = 1;
}
else
{
fail_level = 0x40;
need_reset = 1;
}
}
else if (!usb_csw_valid_ccc)
{
fail_level = 0x50;
if (usb_get_sense_ccc() != USB_SENSE_BUFFER_SIZE)
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
need_reset = 1;
}
}
else if (ret != (int)ccc_main_buffer_size_ccc || usb_csw_residue_ccc)
{
if (usb_get_sense_ccc() != USB_SENSE_BUFFER_SIZE)
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
fail_level = 0x20;
need_reset = 1;
}
else
{
inquiry_good = 1;
}
if ((ccc_main_buffer_size_ccc - ret) != usb_csw_residue_ccc)
{
fail_level = 0x21;
}
}
if (need_reset)
{
fprintf (stdout, "usb reset needed %02x\n", fail_level);
int rret;
int reset_time = hard_reset_time_ccc / 1000;
if (!usb_port_reset_enabled_ccc && !usb_hard_reset_enabled_ccc && !usb_soft_reset_enabled_ccc)
{
reset_time = reset_wait_time_ccc / 1000;
}
if (!inquiry_good && usb_bulk_reset_enabled_ccc)
{
if (!usb_reset_recovery_ccc(reset_time))
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
if (!usb_inquiry_ccc(reset_time) && !usb_check_capacity_ccc(usb_timeout_ccc))
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
inquiry_good = 1;
}
}
}
if (!usb_port_reset_enabled_ccc && !usb_hard_reset_enabled_ccc)
{
reset_time = reset_wait_time_ccc / 1000;
}
if (!inquiry_good && usb_soft_reset_enabled_ccc && usb_allow_ata_ccc)
{
if (!usb_reset_recovery_ccc(reset_time))
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
rret = usb_soft_hard_reset_ccc(USB_RESET_TYPE_SOFT, reset_time);
if (rret == 0)
{
if (!usb_inquiry_ccc(reset_time) && !usb_check_capacity_ccc(usb_timeout_ccc))
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
inquiry_good = 1;
}
}
else if (rret == 5) // invalid command
{
need_reset = 0;
}
}
}
if (!usb_port_reset_enabled_ccc)
{
reset_time = reset_wait_time_ccc / 1000;
}
if (!inquiry_good && usb_hard_reset_enabled_ccc && usb_allow_ata_ccc)
{
if (need_reset)
{
rret = usb_reset_recovery_ccc(reset_time);
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
}
else
{
rret = 0;
}
if (!rret)
{
rret = usb_soft_hard_reset_ccc(USB_RESET_TYPE_HARD, reset_time);
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
if (rret == 0)
{
if (!usb_inquiry_ccc(reset_time) && !usb_check_capacity_ccc(usb_timeout_ccc))
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
inquiry_good = 1;
}
}
}
}
reset_time = reset_wait_time_ccc / 1000;
if (!inquiry_good && usb_port_reset_enabled_ccc)
{
if (!do_usb_reset_ccc())
{
if (!usb_reset_recovery_ccc(reset_time))
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
if (!usb_inquiry_ccc(reset_time) && !usb_check_capacity_ccc(usb_timeout_ccc))
{
if (stop_signal_ccc)
{
usb_timeout_ccc = usb_timeout_bak;
return STOP_SIGNAL_RETURN_CODE;
}
elapsed_time = get_elapsed_usec_ccc();
if (elapsed_time > start_time + general_timeout_ccc)
{
// if it exceeds general timeout then quit
usb_timeout_ccc = usb_timeout_bak;
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
inquiry_good = 1;
}
}
}
}
}
if (!inquiry_good)
{
fail_level = fail_level + 0x0f;
}
usb_timeout_ccc = usb_timeout_bak;
if (0)
{
fprintf (stdout, "usb read return = %d (%02x)\n", fail_level, fail_level);
}
return fail_level;
}
else
{
prepare_cdb_ccc();
int ioctl_ret = ioctl(disk_fd, SG_IO, &io_hdr);
if (ioctl_ret < 0)
{
snprintf(tempmessage_ccc, sizeof(tempmessage_ccc), "Error performing IO on input device (%s).\n", strerror(errno));
if (superclone_ccc)
{
check_message_ccc = true;
message_error_ccc(tempmessage_ccc);
print_gui_error_message_ccc(error_message_ccc, curlang_ccc[LANGERROR], 1);
clear_error_message_ccc();
}
else
{
message_now_ccc(tempmessage_ccc);
}
return (INPUT_DEVICE_ERROR_RETURN_CODE);
}
int ret = post_ioctl_ccc();
return ret;
}
return 0;
}
int do_usb_cmd_ccc(void)
{
// clear any possible passthrough data
sense_key_ccc = 0;
asc_ccc = 0;
ascq_ccc = 0;
ata_error_ccc = 0;
ata_status_ccc = 0;
int cbw_ret = 0;
int raw_ret = 0;
int csw_ret = 0;
usb_csw_status_ccc = 0;
usb_csw_residue_ccc = 0;
usb_csw_valid_ccc = 0;
usb_tag_ccc++;
memset (usbcbwbuffer_ccc, 0, USBCBW_BUFFER_SIZE);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[71], passthrough_ccc.scsi_cmd, command_length_ccc);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[72], &command_length_ccc, 1);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[73], &usb_transfer_direction_ccc, 1);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[73] + 1, &usb_lun_ccc, 1);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[74], &usb_tag_ccc, 4);
usb_transfer_length_ccc = ccc_main_buffer_size_ccc;
memcpy (usbcbwbuffer_ccc + superbyte_ccc[75], &usb_transfer_length_ccc, 4);
uint32_t cbwsig = 0x43425355;
memcpy (usbcbwbuffer_ccc + superbyte_ccc[76], &cbwsig, 4);
cbw_timeout_ccc = usb_timeout_ccc;
cbw_ret = do_usb_send_cbw_ccc(cbw_timeout_ccc);
if (cbw_ret != USBCBW_BUFFER_SIZE)
{
fprintf (stdout, "USB command send cbw short return or error %d (%s)\n", cbw_ret, strerror(abs(cbw_ret)));
if (cbw_ret < 0)
{
return cbw_ret;
}
// reset and try again
usb_reset_recovery_ccc(usb_timeout_ccc);
cbw_ret = do_usb_send_cbw_ccc(cbw_timeout_ccc);
if (cbw_ret != USBCBW_BUFFER_SIZE)
{
fprintf (stdout, "USB command send cbw short return or error %d (%s)\n", cbw_ret, strerror(abs(cbw_ret)));
return INPUT_DEVICE_ERROR_RETURN_CODE;
}
}
if (usb_transfer_length_ccc > 0)
{
if (usb_transfer_direction_ccc == USB_DIRECTION_IN)
{
raw_ret = usb_read_data_ccc(usb_timeout_ccc);
}
else if (usb_transfer_direction_ccc == USB_DIRECTION_OUT)
{
raw_ret = usb_write_data_ccc(usb_timeout_ccc);
}
}
if (raw_ret != (int)usb_transfer_length_ccc)
{
fprintf (stdout, "USB command data transfer short return or error %d (%s)\n", raw_ret, strerror(abs(raw_ret)));
if (raw_ret < 0)
{
return raw_ret;
}
//usb_auto_endpoint_reset(csw_timeout_ccc);
}
csw_timeout_ccc = usb_timeout_ccc;
csw_ret = do_usb_receive_csw_ccc(csw_timeout_ccc);
if (csw_ret == USBCSW_BUFFER_SIZE)
{
usb_csw_valid_ccc = 1;
}
else
{
fprintf (stdout, "USB command receive csw short return or error %d (%s)\n", csw_ret, strerror(abs(csw_ret)));
if (csw_ret < 0)
{
//return csw_ret;
}
//usb_auto_endpoint_reset(csw_timeout_ccc);
csw_ret = do_usb_receive_csw_ccc(csw_timeout_ccc);
if (csw_ret == USBCSW_BUFFER_SIZE)
{
usb_csw_valid_ccc = 1;
}
else
{
fprintf (stdout, "USB command receive csw short return or error %d (%s)\n", csw_ret, strerror(abs(csw_ret)));
//return INPUT_DEVICE_ERROR_RETURN_CODE;
}
}
if (usb_csw_valid_ccc)
{
uint32_t cswsig;
memcpy (&cswsig, usbcswbuffer_ccc + 0, 4);
if (cswsig != 0x53425355)
{
fprintf (stdout, "USB command CSW wrong signature\n");
usb_csw_valid_ccc = 0;
}
uint32_t tag;
memcpy (&tag, usbcswbuffer_ccc + 4, 4);
if (tag != usb_tag_ccc)
{
fprintf (stdout, "USB command CSW tag mismatch\n");
usb_csw_valid_ccc = 0;
}
memcpy (&usb_csw_residue_ccc, usbcswbuffer_ccc + 8, 4);
if (usb_csw_residue_ccc != 0)
{
fprintf (stdout, "USB command CSW data residue %d\n", usb_csw_residue_ccc);
}
memcpy (&usb_csw_status_ccc, usbcswbuffer_ccc + 12, 1);
if (usb_csw_status_ccc != 0)
{
fprintf (stdout, "USB command CSW status failed %d\n", usb_csw_status_ccc);
}
}
return raw_ret;
}
int usb_get_sense_ccc(void)
{
// clear any possible passthrough data
sense_key_ccc = 0;
asc_ccc = 0;
ascq_ccc = 0;
ata_error_ccc = 0;
ata_status_ccc = 0;
ata_lba_ccc = 0;
ata_count_ccc = 0;
ata_device_ccc = 0;
ata_return_valid_ccc = 0;
int descriptor = 0;
int fixed = 0;
if (enable_process_chunk_ccc && 0)
{
sensebuffer_size_ccc = USB_SENSE_BUFFER_DESC_SIZE;
}
else
{
sensebuffer_size_ccc = USB_SENSE_BUFFER_SIZE;
}
usb_transfer_length_ccc = sensebuffer_size_ccc;
passthrough_ccc.scsi_cmd[0] = 0x03; // request sense
passthrough_ccc.scsi_cmd[1] = 0;
passthrough_ccc.scsi_cmd[2] = 0;
passthrough_ccc.scsi_cmd[3] = 0;
passthrough_ccc.scsi_cmd[4] = usb_transfer_length_ccc; // size of data requested back
passthrough_ccc.scsi_cmd[5] = 0;
command_length_ccc = 6;
passthrough_ccc.sg_xfer_direction = SG_DXFER_FROM_DEV;
usb_transfer_direction_ccc = USB_DIRECTION_IN;
int cbw_ret = 0;
int raw_ret = 0;
int csw_ret = 0;
unsigned char csw_status = 0;
uint32_t csw_residue = 0;
usb_tag_ccc++;
memset (usbcbwbuffer_ccc, 0, USBCBW_BUFFER_SIZE);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[71], passthrough_ccc.scsi_cmd, command_length_ccc);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[72], &command_length_ccc, 1);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[73], &usb_transfer_direction_ccc, 1);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[73] + 1, &usb_lun_ccc, 1);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[74], &usb_tag_ccc, 4);
memcpy (usbcbwbuffer_ccc + superbyte_ccc[75], &usb_transfer_length_ccc, 4);
uint32_t cbwsig = 0x43425355;
memcpy (usbcbwbuffer_ccc + superbyte_ccc[76], &cbwsig, 4);
cbw_timeout_ccc = usb_timeout_ccc;
cbw_ret = do_usb_send_cbw_ccc(cbw_timeout_ccc);
if (cbw_ret != USBCBW_BUFFER_SIZE)
{
fprintf (stdout, "USB sense send cbw short return or error %d (%s)\n", cbw_ret, strerror(abs(cbw_ret)));
if (cbw_ret < 0)
{
return cbw_ret;
}
// reset and try again
usb_reset_recovery_ccc(usb_timeout_ccc);
cbw_ret = do_usb_send_cbw_ccc(cbw_timeout_ccc);
if (cbw_ret != USBCBW_BUFFER_SIZE)
{
fprintf (stdout, "USB sense send cbw short return or error %d (%s)\n", cbw_ret, strerror(abs(cbw_ret)));
return INPUT_DEVICE_ERROR_RETURN_CODE;
}
}
set_sense_buffer_ccc();
raw_ret = do_usb_read_sense_ccc(usb_timeout_ccc);
if (raw_ret != (int)usb_transfer_length_ccc)
{
fprintf (stdout, "USB sense data transfer short return or error %d (%s)\n", raw_ret, strerror(abs(raw_ret)));
if (raw_ret < 0)
{
return raw_ret;
}
//reset_usb_endpoint_in_ccc();
}
csw_timeout_ccc = usb_timeout_ccc;
csw_ret = do_usb_receive_csw_ccc(csw_timeout_ccc);
if (csw_ret != USBCSW_BUFFER_SIZE)
{
fprintf (stdout, "USB sense receive csw short return or error %d (%s)\n", csw_ret, strerror(abs(csw_ret)));
if (csw_ret < 0)
{
return csw_ret;
}
//reset_usb_endpoint_in_ccc();
csw_ret = do_usb_receive_csw_ccc(usb_timeout_ccc);
if (csw_ret != USBCSW_BUFFER_SIZE)
{
fprintf (stdout, "USB sense receive csw short return or error %d (%s)\n", csw_ret, strerror(abs(csw_ret)));
return INPUT_DEVICE_ERROR_RETURN_CODE;
}
}
uint32_t cswsig;
memcpy (&cswsig, usbcswbuffer_ccc + 0, 4);
if (cswsig != 0x53425355)
{
fprintf (stdout, "USB sense CSW wrong signature\n");
return INPUT_DEVICE_ERROR_RETURN_CODE;
}
uint32_t tag;
memcpy (&tag, usbcswbuffer_ccc + 4, 4);
if (tag != usb_tag_ccc)
{
fprintf (stdout, "USB sense CSW tag mismatch\n");
return INPUT_DEVICE_ERROR_RETURN_CODE;
}
memcpy (&csw_residue, usbcswbuffer_ccc + 8, 4);
if (csw_residue != 0)
{
fprintf (stdout, "USB sense CSW data residue %d\n", csw_residue);
}
memcpy (&csw_status, usbcswbuffer_ccc + 12, 1);
if (csw_status != 0)
{
fprintf (stdout, "USB sense CSW status failed %d\n", csw_status);
return INPUT_DEVICE_ERROR_RETURN_CODE;
}
unsigned char byte;
unsigned char code;
memcpy (&code, sensebuffer_ccc+0,1);
switch (code)
{
case 0x70:
case 0x71:
memcpy (&byte, sensebuffer_ccc+2, 1);
sense_key_ccc = byte & 0x0f;
memcpy (&byte, sensebuffer_ccc+12, 1);
asc_ccc = byte;
memcpy (&byte, sensebuffer_ccc+13, 1);
ascq_ccc = byte;
break;
case 0xF0:
case 0xF1:
memcpy (&byte, sensebuffer_ccc+2, 1);
sense_key_ccc = byte & 0x0f;
memcpy (&byte, sensebuffer_ccc+12, 1);
asc_ccc = byte;
memcpy (&byte, sensebuffer_ccc+13, 1);
ascq_ccc = byte;
fixed = 1;
break;
case 0x72:
case 0x73:
memcpy (&byte, sensebuffer_ccc+1, 1);
sense_key_ccc = byte & 0x0f;
memcpy (&byte, sensebuffer_ccc+2, 1);
asc_ccc = byte;
memcpy (&byte, sensebuffer_ccc+3, 1);