-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddru_diskutility.c
1685 lines (1471 loc) · 40.7 KB
/
ddru_diskutility.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) 2016 Scott Dwyer
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef __linux__
#include <stdio.h>
#include <stdlib.h>
int main()
{
fprintf (stderr, "This program only works on Linux\n");
exit (0);
}
#else
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <getopt.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <time.h>
#include <sys/time.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/ioctl.h>
#include <scsi/sg.h>
#include "ddru_diskutility_help.h"
char *title = "ddru_diskutility";
char *version_number = "1.3 20141005";
int copyright_year = 2014;
int cleanup(void);
// Function to handle ctrl-c
void signal_callback_handler(int signum)
{
printf("Terminated by user\n");
cleanup();
exit(signum);
}
// time functions
/* Return 1 if the difference is negative, otherwise 0. */
int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
{
long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
result->tv_sec = diff / 1000000;
result->tv_usec = diff % 1000000;
return (diff<0);
}
void timeval_print(struct timeval *tv)
{
char buffer[30];
time_t curtime;
// printf("%ld.%06ld", tv->tv_sec, tv->tv_usec);
curtime = tv->tv_sec;
strftime(buffer, 30, "%m-%d-%Y %T", localtime(&curtime));
printf("%s.%06ld\n", buffer, tv->tv_usec);
}
time_t start_time, end_time;
void help(void);
void version(void);
int i;
int n;
long long return_value;
bool direct = false;
bool sgpt = false;
bool ata = false;
bool read_error = false;
bool doata = false;
bool doinquiry = false;
bool doatainquiry = false;
bool doreadsector = false;
bool doreadlong28 = false;
bool doreadlong48 = false;
bool doreadlongbest = false;
bool dobinary = false;
bool error_recovery_control = false;
bool long_sector_access = false;
int verbosity = 0;
long long total_size = 0;
unsigned long long input_offset = 0;
long long input_position = 0;
long long sector_size = 512;
long long read_size = 0;
long long data_read = 0;
long long cluster_size = 0;
unsigned long long read_sector = 0;
unsigned long long error_timer = 0;
long long sector_start = 0;
long long sector_count = 0;
unsigned long long readlong_count = 0;
// stuff for sgpt
unsigned char sense_buf[64];
unsigned char scsi_cmd[16];
unsigned char sense_key = 0;
unsigned char asc = 0;
unsigned char ascq = 0;
int ioctl_ret;
struct sg_io_hdr io_hdr;
unsigned int buffer_align;
void* buffer;
int buffer_size = 0;
int ecc_size = 0;
int command_length = 0;
unsigned char scsi_inquiry_buffer[256];
unsigned char ata_inquiry_buffer[512];
long last_second = 0;
long last_usecond = 0;
int input_fd;
int output_fd;
char *source_disk;
char *destination_disk;
//FILE *read_rates;
int inquiry(void);
int scsi_inquiry(void);
int ata_inquiry(void);
int readsector(void);
int readlong28(void);
int readlong48(void);
int readlongbest(void);
int prepare_cdb(void);
int post_ioctl(void);
int main (int argc, char **argv)
{
// Register ctrl-c signal and signal handler
signal(SIGINT, signal_callback_handler);
// begin processing command line arguments
int command_line_argument;
int arguments_required; // required number of non-option arguments
bool command_line_error = false; // initialize error to false
while (1)
{
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"verbose", no_argument, 0, 'V'},
{"inquiry", no_argument, 0, 'I'},
{"binary", no_argument, 0, 'B'},
{"direct", no_argument, 0, 'd'},
{"sgpt", no_argument, 0, 'p'},
{"sgptata", no_argument, 0, 'P'},
{"readsector", required_argument, 0, 'r'},
{"readlongbest", required_argument, 0, 'g'},
{"readlong28", required_argument, 0, 'l'},
{"readlong48", required_argument, 0, 'L'},
{"sectorsize", required_argument, 0, 'b'},
{"inputoffset", required_argument, 0, 'i'},
{"readsize", required_argument, 0, 's'},
{"clustersize", required_argument, 0, 'c'},
{0, 0, 0, 0}
};
// getopt_long stores the option index here.
int option_index = 0;
command_line_argument = getopt_long (argc, argv, "hvVIBdpPr:g:l:L:b:i:s:c:",
long_options, &option_index);
// Detect the end of the options.
if (command_line_argument == -1)
break;
switch (command_line_argument)
{
case 'h':
help();
exit(0);
case 'v':
version();
exit(0);
case 'r':
read_sector = strtoull(optarg, NULL, 0);
doreadsector = true;
break;
case 'g':
readlong_count = strtoul(optarg, NULL, 0);
doreadlongbest = true;
sgpt = true;
break;
case 'l':
read_sector = strtoull(optarg, NULL, 0);
doreadlong28 = true;
sgpt = true;
break;
case 'L':
read_sector = strtoull(optarg, NULL, 0);
doreadlong48 = true;
sgpt = true;
break;
case 'b':
sector_size = strtoul(optarg, NULL, 0);
break;
case 'i':
input_offset = strtoull(optarg, NULL, 0);
break;
case 's':
read_size = strtoul(optarg, NULL, 0);
break;
case 'I':
sgpt = true;
doinquiry = true;
break;
case 'B':
dobinary = true;
break;
case 'd':
direct = true;
break;
case 'p':
sgpt = true;
break;
case 'P':
sgpt = true;
doata = true;
break;
case 'V':
verbosity++;
break;
case 'c':
cluster_size = strtoul(optarg, NULL, 0);
break;
case '?':
// getopt_long already printed an error message.
command_line_error = true;
break;
default:
fprintf (stderr, "catastrophic command line processing error\n");
exit (1);
}
}
arguments_required = 1;
if ((argc - optind) != arguments_required)
{
command_line_error = true;
if ((argc - optind) < arguments_required)
fprintf (stderr, "Error: too few arguments\n");
if ((argc - optind) > arguments_required)
fprintf (stderr, "Error: too many arguments\n");
}
// get remaining arguments that are not options
source_disk = argv[optind++];
// exit on command line error
if (command_line_error)
{
fprintf (stderr, "ERROR: command line input error\n");
fprintf (stderr, "Usage: ddru_diskutility sourcedrive (options)\n");
exit (1);
}
// end of command line processing
fprintf (stderr, "%s %s\n", title, version_number);
// open source input
if (direct)
{
input_fd = open (source_disk, O_RDONLY | O_DIRECT);
if (input_fd == -1)
{
perror ("open1");
exit (2);
}
}
else
{
input_fd = open (source_disk, O_RDONLY);
if (input_fd == -1)
{
perror ("open2");
exit (2);
}
}
// get total size of input
total_size = lseek( input_fd, 0, SEEK_END );
if (total_size == -1)
{
perror ("lseek1");
exit (2);
}
// set start position back to begining
return_value = lseek( input_fd, 0, SEEK_SET );
if (return_value == -1)
{
perror ("lseek2");
exit (2);
}
if (input_offset%sector_size)
{
fprintf (stderr, "Error, input offset not a multiple of sector size\n");
exit (1);
}
if (read_size%sector_size)
{
fprintf (stderr, "Error, read size not a multiple of sector size\n");
exit (1);
}
if (read_size !=0 && cluster_size !=0)
{
fprintf (stderr, "Error, you can not supply both clustersize and readsize\n");
exit (1);
}
if (read_size == 0 && cluster_size == 0)
{
cluster_size = 1;
}
sector_start = read_sector + (input_offset/sector_size);
sector_count = cluster_size + (read_size/sector_size);
// if passthrough then get size of buffer limit
// also works to tell if whole drive or partition
if (sgpt)
{
// extract last part of device name
int i = 0;
while (source_disk[i] != '\0')
i++;
while (source_disk[i] != '/')
{
i--;
if (i < 0)
break;
}
char device_name[20];
i++;
int n = 0;
while (source_disk[i] != '\0')
{
device_name[n] = source_disk[i];
n++;
i++;
}
device_name[n] = '\0';
// whether scsi or ata we need to get the buffer limit that the kernel uses
// and we can also use this to find out if whole disk or partition
char file_name[80] = "/sys/block/\0";
strcat (file_name, device_name);
strcat (file_name, "/queue/max_sectors_kb");
FILE *file_pointer;
file_pointer = fopen(file_name, "r");
if (file_pointer == NULL)
{
fprintf(stderr, "Cannot open %s for reading (%s)\n", file_name, strerror(errno));
fprintf(stderr, "Error! Input must be whole disk with passthrough option. Aborting...\n" );
exit (1);
}
else
{
char line[20];
while (fgets(line, sizeof line, file_pointer))
break;
long long max_sectors_kb = strtoull(line, NULL, 0);
if (sector_size * sector_count > max_sectors_kb*1024)
{
fprintf(stderr, "Error! The reported cluster limit is %lld and you chose %lld. Aborting...\n", (max_sectors_kb*1024)/sector_size, sector_count);
exit (1);
}
fclose(file_pointer);
}
}
// limit sector count to 256 to try to limit screen output to a somewhat resonable level
if (sector_count > 256)
{
fprintf (stderr, "Error, clustersize cannot be greater than 256\n");
exit (1);
}
if (verbosity > 0)
{
printf ("total size of input device in bytes= %lld\n", total_size);
printf ("total size of input device in sectors= %lld\n", total_size/sector_size);
printf ("current position in bytes= %lld\n", sector_start*sector_size);
printf ("current position in sectors= %lld\n", sector_start);
}
buffer_size = sector_size * sector_count;
if (buffer_size < sysconf(_SC_PAGESIZE))
{
buffer_size = sysconf(_SC_PAGESIZE);
}
// create a buffer that is memory aligned with the pagesize
buffer_align = sysconf(_SC_PAGESIZE);
if (posix_memalign(&buffer, buffer_align, buffer_size))
{ perror("posix_memalign failed"); exit (EXIT_FAILURE); }
buffer_size = sector_size * sector_count;
// perform initial inquiry
if (sgpt)
{
int inquiry_return;
inquiry_return = scsi_inquiry();
if (inquiry_return == 1)
{
ata_inquiry();
ata = true;
}
}
if (doinquiry)
{
inquiry();
cleanup();
exit (0);
}
if (doreadsector)
{
readsector();
cleanup();
exit (0);
}
if (doreadlong28)
{
if (doreadlongbest)
readlongbest();
else
readlong28();
cleanup();
exit (0);
}
if (doreadlong48)
{
if (doreadlongbest)
readlongbest();
else
readlong48();
cleanup();
exit (0);
}
cleanup();
exit (0);
}
// end of main
//***********************************************************************
// function cleanup
int cleanup(void)
{
free(buffer);
close (input_fd);
close (output_fd);
return (0);
}
//function to process inquiry into output
int inquiry(void)
{
int i;
// scsi inquiry data
fprintf (stdout, "SCSI inquiry results:\n");
fprintf (stdout, " Vendor ID= ");
for (i = 8; i < 16; i++)
{
fprintf (stdout, "%c", isprint(scsi_inquiry_buffer[i]) ? scsi_inquiry_buffer[i] : '.');
}
fprintf (stdout, "\n");
fprintf (stdout, " Product ID= ");
for (i = 16; i < 32; i++)
{
fprintf (stdout, "%c", isprint(scsi_inquiry_buffer[i]) ? scsi_inquiry_buffer[i] : '.');
}
fprintf (stdout, "\n");
fprintf (stdout, " Product Revision= ");
for (i = 32; i < 36; i++)
{
fprintf (stdout, "%c", isprint(scsi_inquiry_buffer[i]) ? scsi_inquiry_buffer[i] : '.');
}
fprintf (stdout, "\n");
fprintf (stdout, " Serial Number= ");
for (i = 36; i < 44; i++)
{
fprintf (stdout, "%c", isprint(scsi_inquiry_buffer[i]) ? scsi_inquiry_buffer[i] : '.');
}
fprintf (stdout, "\n");
// ata inquiry data
if (ata)
{
fprintf (stdout, "ATA indentify device results:\n");
fprintf (stdout, " Serial Number= ");
for (i = 20; i < 40; i += 2)
{
fprintf (stdout, "%c", isprint(ata_inquiry_buffer[i+1]) ? ata_inquiry_buffer[i+1] : '.');
fprintf (stdout, "%c", isprint(ata_inquiry_buffer[i]) ? ata_inquiry_buffer[i] : '.');
}
fprintf (stdout, "\n");
fprintf (stdout, " Firmware Revision= ");
for (i = 46; i < 52; i += 2)
{
fprintf (stdout, "%c", isprint(ata_inquiry_buffer[i+1]) ? ata_inquiry_buffer[i+1] : '.');
fprintf (stdout, "%c", isprint(ata_inquiry_buffer[i]) ? ata_inquiry_buffer[i] : '.');
}
fprintf (stdout, "\n");
fprintf (stdout, " Model Number= ");
for (i = 54; i < 92; i += 2)
{
fprintf (stdout, "%c", isprint(ata_inquiry_buffer[i+1]) ? ata_inquiry_buffer[i+1] : '.');
fprintf (stdout, "%c", isprint(ata_inquiry_buffer[i]) ? ata_inquiry_buffer[i] : '.');
}
fprintf (stdout, "\n");
fprintf (stdout, " Total Sectors= 0x");
for (i = 123; i > 119; i--)
{
fprintf (stdout, "%02X", ata_inquiry_buffer[i]);
}
fprintf (stdout, "\n");
fprintf (stdout, " Total Sectors 48bit= 0x");
for (i = 207; i > 199; i--)
{
fprintf (stdout, "%02X", ata_inquiry_buffer[i]);
}
fprintf (stdout, "\n");
/*
fprintf (stdout, " 2x Physical Sectors per Logical (EXPERIMENTAL)= %d\n", (ata_inquiry_buffer[213] & 0x20));
fprintf (stdout, " Logical Sector Size in Words (EXPERIMENTAL)= 0x");
for (i = 237; i > 233; i--)
{
fprintf (stdout, "%02X", ata_inquiry_buffer[i]);
}
fprintf (stdout, "\n");
*/
fprintf (stdout, " SCT Command Transport= 0x");
for (i = 413; i > 411; i--)
{
fprintf (stdout, "%02X", ata_inquiry_buffer[i]);
}
fprintf (stdout, "\n");
fprintf (stdout, " SCT Error Recovery Control= %s\n", error_recovery_control ? "yes" : "no");
fprintf (stdout, " SCT Long Sector Access= %s\n", long_sector_access ? "yes" : "no");
}
return (0);
}
// funtion for scsi inquiry
int scsi_inquiry(void)
{
scsi_cmd[0] = 0x12; // inquiry command
scsi_cmd[1] = 0;
scsi_cmd[2] = 0;
scsi_cmd[3] = 0;
scsi_cmd[4] = 0x2C; // size of data requested back
scsi_cmd[5] = 0;
buffer_size = 256;
command_length = 6;
prepare_cdb();
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
if (verbosity > 0)
fprintf (stdout, "Performing SCSI Inquiry command\n");
errno = 0;
ioctl_ret = ioctl(input_fd, SG_IO, &io_hdr);
if (ioctl_ret < 0)
{
perror("SCSI inquiry error");
exit (1);
}
post_ioctl();
// copy to external buffer for later use
unsigned char *c;
for (i = 0; i < scsi_cmd[4]; i++)
{
c = (unsigned char *)buffer+i;
scsi_inquiry_buffer[i] = *c;
}
if (strncmp((char *)buffer+8, "ATA ", 8) == 0)
{
if (verbosity > 0)
fprintf (stdout, "ATA device detected\n");
return_value = 1;
}
else
{
if (verbosity > 0)
fprintf (stdout, "Device is not ATA\n");
return_value = 0;
}
return (return_value);
}
// function to do ata inquiry
int ata_inquiry(void)
{
buffer_size = 512;
command_length = 12;
scsi_cmd[0] = 0xA1; // ata passthough 12 command
scsi_cmd[1] = 0x08; // set protocol to 4, PIO Data-In
scsi_cmd[2] = (1<<5)+(1<<3); // set check condition, direction (1 from device, 0 to device)
scsi_cmd[3] = 0;
scsi_cmd[4] = 0;
scsi_cmd[5] = 0;
scsi_cmd[6] = 0;
scsi_cmd[7] = 0;
scsi_cmd[8] = 0;
scsi_cmd[9] = 0xEC; // identify device command
scsi_cmd[10] = 0;
scsi_cmd[11] = 0;
prepare_cdb();
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
if (verbosity > 0)
fprintf (stdout, "Performing ATA Identify Device command\n");
errno = 0;
ioctl_ret = ioctl(input_fd, SG_IO, &io_hdr);
if (ioctl_ret < 0)
{
perror("ATA inquiry error");
exit (1);
}
doatainquiry = true;
post_ioctl();
// copy to external buffer for later use
unsigned char *c;
for (i = 0; i < buffer_size; i++)
{
c = (unsigned char *)buffer+i;
ata_inquiry_buffer[i] = *c;
}
// set some flags for later use
error_recovery_control = ata_inquiry_buffer[412] & 8;
long_sector_access = ata_inquiry_buffer[412] & 2;
return (0);
}
// function to read sector(s)
int readsector(void)
{
// if ata passthough was chosen check if device is ata
if (doata)
{
if (ata != true)
{
fprintf (stderr, "Unable to perform funtion, drive not ATA\n");
exit (1);
}
}
buffer_size = sector_size * sector_count;
ssize_t ret_in;
input_position = sector_start * sector_size;
read_size = sector_size * sector_count;
read_error = false;
// if passthough
if (sgpt)
{
command_length = 16;
// if disk is ata and ata option also chosen then do ata commands
if (ata && doata)
{
scsi_cmd[0] = 0x85; // ata 16 passthough
scsi_cmd[1] = (0x04<<1)+1; // set protocol to 4 PIO Data-In, and set extended bit
scsi_cmd[2] = (1<<5)+(1<<3)+2; // set check condition, direction (1 from device, 0 to device), and length to sector field
scsi_cmd[3] = 0;
scsi_cmd[4] = 0;
scsi_cmd[5] = (unsigned char)((sector_count >> 8) & 0xff);
scsi_cmd[6] = (unsigned char)(sector_count & 0xff);
scsi_cmd[7] = (unsigned char)((sector_start >> 24) & 0xff);
scsi_cmd[8] = (unsigned char)(sector_start & 0xff);
scsi_cmd[9] = (unsigned char)((sector_start >> 32) & 0xff);
scsi_cmd[10] = (unsigned char)((sector_start >> 8) & 0xff);
scsi_cmd[11] = (unsigned char)((sector_start >> 40) & 0xff);
scsi_cmd[12] = (unsigned char)((sector_start >> 16) & 0xff);
scsi_cmd[13] = (1<<7) + (1<<6) + (1<<5) + (unsigned char)((sector_start >> 24) & 0xf);; // back-compat, set LBA bit, back-compat, high 4 bits of LBA
if ((sector_start + (sector_count-1) > 0xFFFFFFF) || (sector_count-1 > 0xFF))
scsi_cmd[14] = 0x24; // read sectors PIO ext
else
scsi_cmd[14] = 0x20; // read sectors PIO
scsi_cmd[15] = 0;
}
// otherwise do scsi commands
else
{
// if the highest LBA to be read is bigger than 32 bits then use read 16
if (sector_start * sector_count > 0xFFFFFFFF)
command_length = 16;
else
command_length = 10;
switch (command_length)
{
case 10:
scsi_cmd[0] = 0x28; // read 10 command
scsi_cmd[1] = 0;
scsi_cmd[2] = (unsigned char)((sector_start >> 24) & 0xff);
scsi_cmd[3] = (unsigned char)((sector_start >> 16) & 0xff);
scsi_cmd[4] = (unsigned char)((sector_start>> 8) & 0xff);
scsi_cmd[5] = (unsigned char)(sector_start & 0xff);
scsi_cmd[6] = 0;
scsi_cmd[7] = (unsigned char)((sector_count >> 8) & 0xff);
scsi_cmd[8] = (unsigned char)(sector_count & 0xff);
scsi_cmd[9] = 0;
break;
case 16:
scsi_cmd[0] = 0x88; // read 16 command
scsi_cmd[1] = 0;
scsi_cmd[2] = (unsigned char)((sector_start >> 56) & 0xff);
scsi_cmd[3] = (unsigned char)((sector_start >> 48) & 0xff);
scsi_cmd[4] = (unsigned char)((sector_start >> 40) & 0xff);
scsi_cmd[5] = (unsigned char)((sector_start >> 32) & 0xff);
scsi_cmd[6] = (unsigned char)((sector_start >> 24) & 0xff);
scsi_cmd[7] = (unsigned char)((sector_start >> 16) & 0xff);
scsi_cmd[8] = (unsigned char)((sector_start >> 8) & 0xff);
scsi_cmd[9] = (unsigned char)(sector_start & 0xff);
scsi_cmd[10] = (unsigned char)((sector_count >> 24) & 0xff);
scsi_cmd[11] = (unsigned char)((sector_count >> 16) & 0xff);
scsi_cmd[12] = (unsigned char)((sector_count >> 8) & 0xff);
scsi_cmd[13] = (unsigned char)(sector_count & 0xff);
scsi_cmd[14] = 0;
scsi_cmd[15] = 0;
break;
default:
fprintf(stderr, "expected cdb size of 10 or 16 but got %d\n", command_length);
exit (1);
}
}
// make sure buffer is filled with zeros before we start
memset (buffer, 0, buffer_size);
prepare_cdb();
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
errno = 0;
ioctl_ret = ioctl(input_fd, SG_IO, &io_hdr);
if (ioctl_ret < 0)
{
perror("reading (SG_IO) on sg device, error");
exit (1);
}
post_ioctl();
if (errno != 0)
read_error = true;
}
else // normal read
{
return_value = lseek( input_fd, input_position, SEEK_SET );
if (return_value == -1)
{
perror ("lseek3");
exit (2);
}
errno = 0;
ret_in = read (input_fd, buffer, buffer_size);
if (ret_in < buffer_size || errno != 0)
{
read_error = true;
if (verbosity > 0)
{
fprintf (stderr, "return read size=%d ", (int)ret_in);
fprintf (stderr, "errno=%d\n", errno);
perror ("read error");
}
}
}
if (errno == 0)
{
fprintf (stdout, "sector=%lld\n", sector_start);
fprintf (stdout, "count=%lld\n", sector_count);
fprintf (stdout, "buffersize=%d\n", buffer_size);
unsigned char *c;
int i;
for (i = 0; i < buffer_size; i+=16)
{
fprintf (stdout, "%llX: ", i+(sector_start*sector_size));
int n;
for (n=0; n < 16; n++)
{
c = (unsigned char *)buffer+i+n;
if (dobinary)
{
if (n == 8)
fprintf (stdout, "\n%llX: ", i+n+(sector_start*sector_size));
int i;
for (i = 0; i < 8; i++)
{
if ((*c<<i) & 0x80)
fprintf (stdout, "1");
else
fprintf (stdout, "0");
}
fprintf (stdout, " ");
}
else
fprintf (stdout, "%02X ", *c);
}
for (n=0; n < 16; n++)
{
c = (unsigned char *)buffer+i+n;
if (!dobinary)
fprintf (stdout, "%c", isprint(*c) ? *c : '.');
}
fprintf (stdout, "\n");
}
fprintf (stdout, "\n");
}
else
{
fprintf (stderr, "Read error, unable to read sector(s) from disk\n");
}
return (0);
}
// function to perform old 28 bit read long
int readlong28(void)
{
if (ata != true)
{
fprintf (stderr, "Unable to perform funtion, drive not ATA\n");
exit (1);
}
if ((sector_start*sector_size) > total_size)
fprintf (stderr, "Warning, sector %lld is higher than reported maximum %lld\n", sector_start, total_size/sector_size);
sector_count = 1;
ecc_size = 52;
read_size = 512;
buffer_size = read_size + ecc_size;
command_length = 16;
scsi_cmd[0] = 0x85; // ata 16 passthough
scsi_cmd[1] = (0x04<<1)+1; // set protocol to 4 PIO Data-In, and set extended bit
scsi_cmd[2] = (1<<5)+(0<<3)+2; // set check condition, direction (1 from device, 0 to device), and length to sector field
scsi_cmd[3] = 0;
scsi_cmd[4] = 0;
scsi_cmd[5] = (unsigned char)((sector_count >> 8) & 0xff);
scsi_cmd[6] = (unsigned char)(sector_count & 0xff);
scsi_cmd[7] = (unsigned char)((sector_start >> 24) & 0xff);
scsi_cmd[8] = (unsigned char)(sector_start & 0xff);
scsi_cmd[9] = (unsigned char)((sector_start >> 32) & 0xff);
scsi_cmd[10] = (unsigned char)((sector_start >> 8) & 0xff);
scsi_cmd[11] = (unsigned char)((sector_start >> 40) & 0xff);
scsi_cmd[12] = (unsigned char)((sector_start >> 16) & 0xff);
scsi_cmd[13] = (1<<7) + (1<<6) + (1<<5) + (unsigned char)((sector_start >> 24) & 0xf);; // back-compat, set LBA bit, back-compat, high 4 bits of LBA
scsi_cmd[14] = 0x22; // read long PIO
scsi_cmd[15] = 0;
// make sure buffer is filled with zeros before we start
memset (buffer, 0, buffer_size);
prepare_cdb();
io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
errno = 0;
ioctl_ret = ioctl(input_fd, SG_IO, &io_hdr);
if (ioctl_ret < 0)
{
perror("reading (SG_IO) on sg device, error");
exit (1);
}
post_ioctl();
if (errno == 0)
{
fprintf (stdout, "sector=%lld\n", sector_start);
fprintf (stdout, "count=%lld\n", sector_count);
fprintf (stdout, "Data size=%lld\n", read_size);
fprintf (stdout, "ECC size=%d\n", ecc_size);
unsigned char *c;
int i;
for (i = 0; i < read_size; i+=16)
{
fprintf (stdout, "%d: ", i);
int n;