-
Notifications
You must be signed in to change notification settings - Fork 19
/
hdparm.c
2844 lines (2650 loc) · 80 KB
/
hdparm.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
/*
* hdparm.c - Command line interface to get/set hard disk parameters.
* - by Mark Lord (C) 1994-2012 -- freely distributable.
*/
#define _LARGEFILE64_SOURCE /*for lseek64*/
#define _BSD_SOURCE /* for strtoll() */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define __USE_GNU /* for O_DIRECT */
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <endian.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/mman.h>
#include <sys/user.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/major.h>
#include <endian.h>
#include <asm/byteorder.h>
#include "hdparm.h"
#include "sgio.h"
static int argc;
static char **argv;
static char *argp;
static int num_flags_processed = 0;
extern const char *minor_str[];
#define VERSION "v9.43"
#ifndef O_DIRECT
#define O_DIRECT 040000 /* direct disk access, not easily obtained from headers */
#endif
#ifndef CDROM_SELECT_SPEED /* already defined in 2.3.xx kernels and above */
#define CDROM_SELECT_SPEED 0x5322
#endif
#define TIMING_BUF_MB 2
#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
char *progname;
int verbose = 0;
int prefer_ata12 = 0;
static int do_defaults = 0, do_flush = 0, do_ctimings, do_timings = 0;
static int do_identity = 0, get_geom = 0, noisy = 1, quiet = 0;
static int do_flush_wcache = 0;
static int set_wdidle3 = 0, get_wdidle3 = 0, wdidle3 = 0;
static int set_timings_offset = 0;
static __u64 timings_offset = 0;
static int set_fsreadahead= 0, get_fsreadahead= 0, fsreadahead= 0;
static int set_readonly = 0, get_readonly = 0, readonly = 0;
static int set_unmask = 0, get_unmask = 0, unmask = 0;
static int set_mult = 0, get_mult = 0, mult = 0;
static int set_dma = 0, get_dma = 0, dma = 0;
static int set_dma_q = 0, get_dma_q = 0, dma_q = 0;
static int set_nowerr = 0, get_nowerr = 0, nowerr = 0;
static int set_keep = 0, get_keep = 0, keep = 0;
static int set_io32bit = 0, get_io32bit = 0, io32bit = 0;
static int set_piomode = 0, get_piomode= 0, piomode = 0;
static int set_dkeep = 0, get_dkeep = 0, dkeep = 0;
static int set_standby = 0, get_standby = 0, standby= 0;
static int set_xfermode = 0, get_xfermode = 0;
static int xfermode_requested= 0;
static int set_lookahead= 0, get_lookahead= 0, lookahead= 0;
static int set_prefetch = 0, get_prefetch = 0, prefetch = 0;
static int set_defects = 0, get_defects = 0, defects = 0;
static int set_wcache = 0, get_wcache = 0, wcache = 0;
static int set_doorlock = 0, get_doorlock = 0, doorlock = 0;
static int set_seagate = 0, get_seagate = 0;
static int get_idleimmediate = 0, set_idleimmediate = 0;
static int get_idleunload = 0, set_idleunload = 0;
static int set_standbynow = 0, get_standbynow = 0;
static int set_sleepnow = 0, get_sleepnow = 0;
static int set_powerup_in_standby = 0, get_powerup_in_standby = 0, powerup_in_standby = 0;
static int get_hitachi_temp = 0, set_hitachi_temp = 0;
static int security_freeze = 0;
static int security_master = 0, security_mode = 0;
static int enhanced_erase = 0;
static int set_security = 0;
static int do_dco_freeze = 0, do_dco_restore = 0, do_dco_identify = 0;
static unsigned int security_command = ATA_OP_SECURITY_UNLOCK;
static char security_password[33], *fwpath;
static int get_powermode = 0, set_powermode = 0;
static int set_apmmode = 0, get_apmmode= 0, apmmode = 0;
static int get_cdromspeed = 0, set_cdromspeed = 0, cdromspeed = 0;
static int do_IDentity = 0, drq_hsm_error = 0;
static int do_fwdownload = 0, xfer_mode = 0;
static int set_busstate = 0, get_busstate = 0, busstate = 0;
static int set_reread_partn = 0, get_reread_partn;
static int set_acoustic = 0, get_acoustic = 0, acoustic = 0;
static int write_read_verify = 0, get_write_read_verify = 0, set_write_read_verify = 0;
static int make_bad_sector = 0, make_bad_sector_flagged;
static __u64 make_bad_sector_addr = ~0ULL;
#ifdef FORMAT_AND_ERASE
static int format_track = 0;
static __u64 format_track_addr = ~0ULL;
static int erase_sectors = 0;
static __u64 erase_sectors_addr = ~0ULL;
#endif
static struct sector_range_s *trim_sector_ranges = NULL;
static int trim_sector_ranges_count = 0;
static int trim_from_stdin = 0;
static int write_sector = 0;
static __u64 write_sector_addr = ~0ULL;
static int read_sector = 0;
static __u64 read_sector_addr = ~0ULL;
static int set_max_sectors = 0, set_max_permanent, get_native_max_sectors = 0;
static __u64 set_max_addr = 0;
static int get_doreset = 0, set_doreset = 0;
static int i_know_what_i_am_doing = 0;
static int please_destroy_my_drive = 0;
const int timeout_15secs = 15;
const int timeout_60secs = 60;
const int timeout_5mins = (5 * 60);
const int timeout_2hrs = (2 * 60 * 60);
static int open_flags = O_RDONLY|O_NONBLOCK;
// Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
// then the HDIO_GET_IDENTITY only returned 142 bytes.
// Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
// and HDIO_GET_IDENTITY returns 512 bytes. But the latest
// 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
// (which they should, but they should just return -EINVAL).
//
// So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
// On a really old system, it will not, and we will be confused.
// Too bad, really.
const char *cfg_str[] =
{ "", " HardSect", " SoftSect", " NotMFM",
" HdSw>15uSec", " SpinMotCtl", " Fixed", " Removeable",
" DTR<=5Mbs", " DTR>5Mbs", " DTR>10Mbs", " RotSpdTol>.5%",
" dStbOff", " TrkOff", " FmtGapReq", " nonMagnetic"
};
const char *SlowMedFast[] = {"slow", "medium", "fast", "eide", "ata"};
const char *BuffType[4] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
#define YN(b) (((b)==0)?"no":"yes")
static void on_off (unsigned int value)
{
printf(value ? " (on)\n" : " (off)\n");
}
#ifndef ENOIOCTLCMD
#define ENOIOCTLCMD ENOTTY
#endif
static void flush_buffer_cache (int fd)
{
sync();
fsync(fd); /* flush buffers */
fdatasync(fd); /* flush buffers */
sync();
if (ioctl(fd, BLKFLSBUF, NULL)) /* do it again, big time */
perror("BLKFLSBUF failed");
else
do_drive_cmd(fd, NULL, 0); /* IDE: await completion */
sync();
}
static int seek_to_zero (int fd)
{
if (lseek(fd, (off_t) 0, SEEK_SET)) {
perror("lseek() failed");
return 1;
}
return 0;
}
static int read_big_block (int fd, char *buf)
{
int i, rc;
if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES) {
if (rc) {
if (rc == -1)
perror("read() failed");
else
fprintf(stderr, "read(%u) returned %u bytes\n", TIMING_BUF_BYTES, rc);
} else {
fputs ("read() hit EOF - device too small\n", stderr);
}
return 1;
}
/* access all sectors of buf to ensure the read fully completed */
for (i = 0; i < TIMING_BUF_BYTES; i += 512)
buf[i] &= 1;
return 0;
}
static void *prepare_timing_buf (unsigned int len)
{
unsigned int i;
__u8 *buf;
buf = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
if (buf == MAP_FAILED) {
perror("could not allocate timing buf");
return NULL;
}
for (i = 0; i < len; i += 4096)
buf[i] = 0; /* guarantee memory is present/assigned */
if (-1 == mlock(buf, len)) {
perror("mlock() failed on timing buf");
munmap(buf, len);
return NULL;
}
mlockall(MCL_CURRENT|MCL_FUTURE); // don't care if this fails on low-memory machines
sync();
/* give time for I/O to settle */
sleep(3);
return buf;
}
static void time_cache (int fd)
{
char *buf;
struct itimerval e1, e2;
double elapsed, elapsed2;
unsigned int iterations, total_MB;
buf = prepare_timing_buf(TIMING_BUF_BYTES);
if (!buf)
return;
/*
* getitimer() is used rather than gettimeofday() because
* it is much more consistent (on my machine, at least).
*/
setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
if (seek_to_zero (fd)) return;
if (read_big_block (fd, buf)) return;
printf(" Timing %scached reads: ", (open_flags & O_DIRECT) ? "O_DIRECT " : "");
fflush(stdout);
/* Clear out the device request queues & give them time to complete */
flush_buffer_cache(fd);
sleep(1);
/* Now do the timing */
iterations = 0;
getitimer(ITIMER_REAL, &e1);
do {
++iterations;
if (seek_to_zero (fd) || read_big_block (fd, buf))
goto quit;
getitimer(ITIMER_REAL, &e2);
elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
} while (elapsed < 2.0);
total_MB = iterations * TIMING_BUF_MB;
elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
/* Now remove the lseek() and getitimer() overheads from the elapsed time */
getitimer(ITIMER_REAL, &e1);
do {
if (seek_to_zero (fd))
goto quit;
getitimer(ITIMER_REAL, &e2);
elapsed2 = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
} while (--iterations);
elapsed -= elapsed2;
if (total_MB >= elapsed) /* more than 1MB/s */
printf("%3u MB in %5.2f seconds = %6.2f MB/sec\n",
total_MB, elapsed,
total_MB / elapsed);
else
printf("%3u MB in %5.2f seconds = %6.2f kB/sec\n",
total_MB, elapsed,
total_MB / elapsed * 1024);
flush_buffer_cache(fd);
sleep(1);
quit:
munlockall();
munmap(buf, TIMING_BUF_BYTES);
}
static int time_device (int fd)
{
char *buf;
double elapsed;
struct itimerval e1, e2;
int err = 0;
unsigned int max_iterations = 1024, total_MB, iterations;
/*
* get device size
*/
if (do_ctimings || do_timings) {
__u64 nsectors;
do_flush = 1;
err = get_dev_geometry(fd, NULL, NULL, NULL, NULL, &nsectors);
if (!err)
max_iterations = nsectors / (2 * 1024) / TIMING_BUF_MB;
}
buf = prepare_timing_buf(TIMING_BUF_BYTES);
if (!buf)
err = ENOMEM;
if (err)
goto quit;
printf(" Timing %s disk reads", (open_flags & O_DIRECT) ? "O_DIRECT" : "buffered");
if (set_timings_offset)
printf(" (offset %llu GB)", timings_offset / 0x40000000ULL);
printf(": ");
fflush(stdout);
if (set_timings_offset && lseek64(fd, timings_offset, SEEK_SET) == (off64_t)-1) {
err = errno;
perror("lseek() failed");
goto quit;
}
/*
* getitimer() is used rather than gettimeofday() because
* it is much more consistent (on my machine, at least).
*/
setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
/* Now do the timings for real */
iterations = 0;
getitimer(ITIMER_REAL, &e1);
do {
++iterations;
if (read_big_block (fd, buf))
goto quit;
getitimer(ITIMER_REAL, &e2);
elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
+ ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
} while (elapsed < 3.0 && iterations < max_iterations);
total_MB = iterations * TIMING_BUF_MB;
if ((total_MB / elapsed) > 1.0) /* more than 1MB/s */
printf("%3u MB in %5.2f seconds = %6.2f MB/sec\n",
total_MB, elapsed, total_MB / elapsed);
else
printf("%3u MB in %5.2f seconds = %6.2f kB/sec\n",
total_MB, elapsed, total_MB / elapsed * 1024);
quit:
munlockall();
if (buf)
munmap(buf, TIMING_BUF_BYTES);
return err;
}
static void dmpstr (const char *prefix, unsigned int i, const char *s[], unsigned int maxi)
{
if (i > maxi)
printf("%s%u", prefix, i);
else
printf("%s%s", prefix, s[i]);
}
static __u16 *id;
static void get_identify_data (int fd);
static __u64 get_lba_capacity (__u16 *idw)
{
__u64 nsects = ((__u32)idw[58] << 16) | idw[57];
if (idw[49] & 0x200) {
nsects = ((__u32)idw[61] << 16) | idw[60];
if ((idw[83] & 0xc000) == 0x4000 && (idw[86] & 0x0400)) {
nsects = (__u64)idw[103] << 48 | (__u64)idw[102] << 32 |
(__u64)idw[101] << 16 | idw[100];
}
}
return nsects;
}
static char *strip (char *s)
{
char *e;
while (*s == ' ') ++s;
if (*s)
for (e = s + strlen(s); *--e == ' '; *e = '\0');
return s;
}
static void dump_identity (__u16 *idw)
{
int i;
char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
char *model = strip(strndup((char *)&idw[27], 40));
char *fwrev = strip(strndup((char *)&idw[23], 8));
char *serno = strip(strndup((char *)&idw[10], 20));
__u8 tPIO;
printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s", model, fwrev, serno);
printf("\n Config={");
for (i = 0; i <= 15; i++) {
if (idw[0] & (1<<i))
printf("%s", cfg_str[i]);
}
printf(" }\n");
printf(" RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
idw[1], idw[3], idw[6], idw[4], idw[5], idw[22]);
dmpstr(" BuffType=", idw[20], BuffType, 3);
if (idw[21] && idw[21] != 0xffff)
printf(", BuffSize=%ukB", idw[21] / 2);
else
printf(", BuffSize=unknown");
printf(", MaxMultSect=%u", idw[47] & 0xff);
if ((idw[47] & 0xff)) {
printf(", MultSect=");
if (!(idw[59] & 0x100))
printf("?%u?", idw[59] & 0xff);
else if (idw[59] & 0xff)
printf("%u", idw[59] & 0xff);
else
printf("off");
}
putchar('\n');
tPIO = idw[51] >> 8;
if (tPIO <= 5) {
strcat(pmodes, "pio0 ");
if (tPIO >= 1) strcat(pmodes, "pio1 ");
if (tPIO >= 2) strcat(pmodes, "pio2 ");
}
if (!(idw[53] & 1))
printf(" (maybe):");
printf(" CurCHS=%u/%u/%u, CurSects=%u", idw[54], idw[55], idw[56], idw[57] | (idw[58] << 16));
printf(", LBA=%s", YN(idw[49] & 0x200));
if (idw[49] & 0x200)
printf(", LBAsects=%llu", get_lba_capacity(idw));
if (idw[49] & 0x100) {
if (idw[62] | idw[63]) {
if (idw[62] & 0x100) strcat(dmodes,"*");
if (idw[62] & 1) strcat(dmodes,"sdma0 ");
if (idw[62] & 0x200) strcat(dmodes,"*");
if (idw[62] & 2) strcat(dmodes,"sdma1 ");
if (idw[62] & 0x400) strcat(dmodes,"*");
if (idw[62] & 4) strcat(dmodes,"sdma2 ");
if (idw[62] & 0xf800) strcat(dmodes,"*");
if (idw[62] & 0xf8) strcat(dmodes,"sdma? ");
if (idw[63] & 0x100) strcat(dmodes,"*");
if (idw[63] & 1) strcat(dmodes,"mdma0 ");
if (idw[63] & 0x200) strcat(dmodes,"*");
if (idw[63] & 2) strcat(dmodes,"mdma1 ");
if (idw[63] & 0x400) strcat(dmodes,"*");
if (idw[63] & 4) strcat(dmodes,"mdma2 ");
if (idw[63] & 0xf800) strcat(dmodes,"*");
if (idw[63] & 0xf8) strcat(dmodes,"mdma? ");
}
}
printf("\n IORDY=");
if (idw[49] & 0x800)
printf((idw[49] & 0x400) ? "on/off" : "yes");
else
printf("no");
if ((idw[49] & 0x800) || (idw[53] & 2)) {
if ((idw[53] & 2)) {
printf(", tPIO={min:%u,w/IORDY:%u}", idw[67], idw[68]);
if (idw[64] & 1) strcat(pmodes, "pio3 ");
if (idw[64] & 2) strcat(pmodes, "pio4 ");
if (idw[64] &~3) strcat(pmodes, "pio? ");
}
if (idw[53] & 4) {
if (idw[88] & 0x100) strcat(umodes,"*");
if (idw[88] & 0x001) strcat(umodes,"udma0 ");
if (idw[88] & 0x200) strcat(umodes,"*");
if (idw[88] & 0x002) strcat(umodes,"udma1 ");
if (idw[88] & 0x400) strcat(umodes,"*");
if (idw[88] & 0x004) strcat(umodes,"udma2 ");
if (idw[88] & 0x800) strcat(umodes,"*");
if (idw[88] & 0x008) strcat(umodes,"udma3 ");
if (idw[88] & 0x1000) strcat(umodes,"*");
if (idw[88] & 0x010) strcat(umodes,"udma4 ");
if (idw[88] & 0x2000) strcat(umodes,"*");
if (idw[88] & 0x020) strcat(umodes,"udma5 ");
if (idw[88] & 0x4000) strcat(umodes,"*");
if (idw[88] & 0x040) strcat(umodes,"udma6 ");
}
}
if ((idw[49] & 0x100) && (idw[53] & 2))
printf(", tDMA={min:%u,rec:%u}", idw[65], idw[66]);
printf("\n PIO modes: %s", pmodes);
if (*dmodes)
printf("\n DMA modes: %s", dmodes);
if (*umodes)
printf("\n UDMA modes: %s", umodes);
printf("\n AdvancedPM=%s",YN(idw[83]&8));
if (idw[83] & 8) {
if (!(idw[86]&8))
printf(": disabled (255)");
else if ((idw[91]&0xFF00)!=0x4000)
printf(": unknown setting");
else
printf(": mode=0x%02X (%u)",idw[91]&0xFF,idw[91]&0xFF);
}
if (idw[82]&0x20)
printf(" WriteCache=%s",(idw[85]&0x20) ? "enabled" : "disabled");
if (idw[81] || idw[80]) {
printf("\n Drive conforms to: ");
if (idw[81] <= 31)
printf("%s: ", minor_str[idw[81]]);
else
printf("unknown: ");
if (idw[80] != 0x0000 && /* NOVAL_0 */
idw[80] != 0xFFFF) { /* NOVAL_1 */
int count = 0;
for (i=0; i <= 7; i++) {
if (idw[80] & (1<<i))
printf("%s%u", count++ ? "," : " ATA/ATAPI-", i);
}
}
}
printf("\n");
printf("\n * signifies the current active mode\n");
printf("\n");
}
static const char *busstate_str (unsigned int value)
{
static const char *states[4] = {"off", "on", "tristate", "unknown"};
if (value > 3)
value = 3;
return states[value];
}
static void interpret_standby (void)
{
printf(" (");
switch(standby) {
case 0: printf("off");
break;
case 252: printf("21 minutes");
break;
case 253: printf("vendor-specific");
break;
case 254: printf("?reserved");
break;
case 255: printf("21 minutes + 15 seconds");
break;
default:
if (standby <= 240) {
unsigned int secs = standby * 5;
unsigned int mins = secs / 60;
secs %= 60;
if (mins) printf("%u minutes", mins);
if (mins && secs) printf(" + ");
if (secs) printf("%u seconds", secs);
} else if (standby <= 251) {
unsigned int mins = (standby - 240) * 30;
unsigned int hrs = mins / 60;
mins %= 60;
if (hrs) printf("%u hours", hrs);
if (hrs && mins) printf(" + ");
if (mins) printf("%u minutes", mins);
} else {
printf("illegal value");
}
break;
}
printf(")\n");
}
struct xfermode_entry {
int val;
const char *name;
};
static const struct xfermode_entry xfermode_table[] = {
{ 8, "pio0" },
{ 9, "pio1" },
{ 10, "pio2" },
{ 11, "pio3" },
{ 12, "pio4" },
{ 13, "pio5" },
{ 14, "pio6" },
{ 15, "pio7" },
{ 16, "sdma0" },
{ 17, "sdma1" },
{ 18, "sdma2" },
{ 19, "sdma3" },
{ 20, "sdma4" },
{ 21, "sdma5" },
{ 22, "sdma6" },
{ 23, "sdma7" },
{ 32, "mdma0" },
{ 33, "mdma1" },
{ 34, "mdma2" },
{ 35, "mdma3" },
{ 36, "mdma4" },
{ 37, "mdma5" },
{ 38, "mdma6" },
{ 39, "mdma7" },
{ 64, "udma0" },
{ 65, "udma1" },
{ 66, "udma2" },
{ 67, "udma3" },
{ 68, "udma4" },
{ 69, "udma5" },
{ 70, "udma6" },
{ 71, "udma7" },
{ 0, NULL }
};
static int translate_xfermode(char * name)
{
const struct xfermode_entry *tmp;
char *endptr;
int val = -1;
for (tmp = xfermode_table; tmp->name != NULL; ++tmp) {
if (!strcmp(name, tmp->name))
return tmp->val;
}
val = strtol(name, &endptr, 10);
if (*endptr == '\0')
return val;
return -1;
}
static void interpret_xfermode (unsigned int xfermode)
{
printf(" (");
switch(xfermode) {
case 0: printf("default PIO mode");
break;
case 1: printf("default PIO mode, disable IORDY");
break;
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15: printf("PIO flow control mode%u", xfermode-8);
break;
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23: printf("singleword DMA mode%u", xfermode-16);
break;
case 32:
case 33:
case 34:
case 35:
case 36:
case 37:
case 38:
case 39: printf("multiword DMA mode%u", xfermode-32);
break;
case 64:
case 65:
case 66:
case 67:
case 68:
case 69:
case 70:
case 71: printf("UltraDMA mode%u", xfermode-64);
break;
default:
printf("unknown, probably not valid");
break;
}
printf(")\n");
}
static unsigned int get_erase_timeout_secs (int fd, int enhanced)
{
unsigned int timeout = 0;
unsigned int idx = 89 + enhanced;
get_identify_data(fd);
if (id) {
timeout = id[idx];
if (timeout && timeout <= 0xff) {
/*
* 0xff means "more than 254 2-minute intervals (508+ minutes),
* but we really want a better idea than that.
* Norman Diamond suggests allowing 1sec per 30MB of capacity.
*/
if (timeout == 0xff) {
__u64 lba_limit = get_lba_capacity(id);
__u64 estimate = (lba_limit / 2048ULL) / 30ULL / 60;
timeout = 508 + 60; /* spec says > 508 minutes */
if (timeout < estimate)
timeout = estimate;
} else {
timeout = (timeout * 2) + 30; /* Add on a 30min margin */
}
}
}
if (!timeout)
timeout = 2 * 60; /* default: two hours */
timeout *= 60; /* convert minutes to seconds */
return timeout;
}
static void
do_set_security (int fd)
{
int err = 0;
const char *description;
struct hdio_taskfile *r;
__u8 *data;
r = malloc(sizeof(struct hdio_taskfile) + 512);
if (!r) {
err = errno;
perror("malloc()");
exit(err);
}
memset(r, 0, sizeof(struct hdio_taskfile) + 512);
r->cmd_req = TASKFILE_CMD_REQ_OUT;
r->dphase = TASKFILE_DPHASE_PIO_OUT;
r->obytes = 512;
r->lob.command = security_command;
r->oflags.lob.nsect = 1;
r->lob.nsect = 1;
data = (__u8*)r->data;
data[0] = security_master & 0x01;
memcpy(data+2, security_password, 32);
r->oflags.lob.command = 1;
r->oflags.lob.feat = 1;
switch (security_command) {
case ATA_OP_SECURITY_ERASE_UNIT:
description = "SECURITY_ERASE";
data[0] |= enhanced_erase ? 0x02 : 0;
break;
case ATA_OP_SECURITY_DISABLE:
description = "SECURITY_DISABLE";
break;
case ATA_OP_SECURITY_UNLOCK:
description = "SECURITY_UNLOCK";
break;
case ATA_OP_SECURITY_SET_PASS:
description = "SECURITY_SET_PASS";
data[1] = (security_mode & 0x01);
if (security_master) {
/* increment master-password revision-code */
__u16 revcode;
get_identify_data(fd);
if (!id)
exit(EIO);
revcode = id[92];
if (revcode == 0xfffe)
revcode = 0;
revcode += 1;
data[34] = revcode;
data[35] = revcode >> 8;
}
break;
default:
fprintf(stderr, "BUG in do_set_security(), command1=0x%x\n", security_command);
exit(EINVAL);
}
printf(" Issuing %s command, password=\"%s\", user=%s",
description, security_password, (data[0] & 1) ? "master" : "user");
if (security_command == ATA_OP_SECURITY_SET_PASS)
printf(", mode=%s", data[1] ? "max" : "high");
printf("\n");
/*
* The Linux kernel IDE driver (until at least 2.6.12) segfaults on the first
* command when issued on a locked drive, and the actual erase is never issued.
* One could patch the code to issue separate commands for erase prepare and
* erase to erase a locked drive.
*
* We would like to issue these commands consecutively, but since the Linux
* kernel until at least 2.6.12 segfaults on each command issued the second will
* never be executed.
*
* One is at least able to issue the commands consecutively in two hdparm invocations,
* assuming the segfault isn't followed by an oops.
*/
if (security_command == ATA_OP_SECURITY_ERASE_UNIT) {
unsigned int timeout = get_erase_timeout_secs(fd, enhanced_erase);
__u8 args[4] = {ATA_OP_SECURITY_ERASE_PREPARE,0,0,0};
if (do_drive_cmd(fd, args, 0)) {
err = errno;
perror("ERASE_PREPARE");
} else {
if ((do_taskfile_cmd(fd, r, timeout))) {
err = errno;
perror("SECURITY_ERASE");
}
}
} else if (security_command == ATA_OP_SECURITY_DISABLE) {
/* First attempt an unlock */
r->lob.command = ATA_OP_SECURITY_UNLOCK;
if ((do_taskfile_cmd(fd, r, timeout_15secs))) {
err = errno;
perror("SECURITY_UNLOCK");
} else {
/* Then the security disable */
r->lob.command = security_command;
if ((do_taskfile_cmd(fd, r, timeout_15secs))) {
err = errno;
perror("SECURITY_DISABLE");
}
}
} else if (security_command == ATA_OP_SECURITY_UNLOCK) {
if ((do_taskfile_cmd(fd, r, timeout_15secs))) {
err = errno;
perror("SECURITY_UNLOCK");
}
} else if (security_command == ATA_OP_SECURITY_SET_PASS) {
if ((do_taskfile_cmd(fd, r, timeout_15secs))) {
err = errno;
perror("SECURITY_SET_PASS");
}
} else {
fprintf(stderr, "BUG in do_set_security(), command2=0x%x\n", security_command);
err = EINVAL;
}
free(r);
if (err)
exit(err);
}
static __u8 last_identify_op = 0;
static void get_identify_data (int fd)
{
static __u8 args[4+512];
int i;
if (id)
return;
memset(args, 0, sizeof(args));
last_identify_op = ATA_OP_IDENTIFY;
args[0] = last_identify_op;
args[3] = 1; /* sector count */
if (do_drive_cmd(fd, args, 0)) {
prefer_ata12 = 0;
memset(args, 0, sizeof(args));
last_identify_op = ATA_OP_PIDENTIFY;
args[0] = last_identify_op;
args[3] = 1; /* sector count */
if (do_drive_cmd(fd, args, 0)) {
perror(" HDIO_DRIVE_CMD(identify) failed");
return;
}
}
/* byte-swap the little-endian IDENTIFY data to match byte-order on host CPU */
id = (void *)(args + 4);
for (i = 0; i < 0x100; ++i)
__le16_to_cpus(&id[i]);
}
static void confirm_i_know_what_i_am_doing (const char *opt, const char *explanation)
{
if (!i_know_what_i_am_doing) {
fprintf(stderr, "Use of %s is VERY DANGEROUS.\n%s\n"
"Please supply the --yes-i-know-what-i-am-doing flag if you really want this.\n"
"Program aborted.\n", opt, explanation);
exit(EPERM);
}
}
static void confirm_please_destroy_my_drive (const char *opt, const char *explanation)
{
if (!please_destroy_my_drive) {
fprintf(stderr, "Use of %s is EXTREMELY DANGEROUS.\n%s\n"
"Please also supply the --please-destroy-my-drive flag if you really want this.\n"
"Program aborted.\n", opt, explanation);
exit(EPERM);
}
}
static int flush_wcache (int fd)
{
__u8 args[4] = {ATA_OP_FLUSHCACHE,0,0,0};
int err = 0;
get_identify_data(fd);
if (id && (id[83] & 0xe000) == 0x6000)
args[0] = ATA_OP_FLUSHCACHE_EXT;
if (do_drive_cmd(fd, args, timeout_60secs)) {
err = errno;
perror (" HDIO_DRIVE_CMD(flushcache) failed");
}
return err;
}
static void dump_sectors (__u16 *w, unsigned int count)
{
unsigned int i;
for (i = 0; i < (count*256/8); ++i) {
#if 0
printf("%04x %04x %04x %04x %04x %04x %04x %04x\n",
w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7]);
w += 8;
#else
int word;
for (word = 0; word < 8; ++word) {
printf("%04x", le16toh(w[0]));
++w;
putchar(word == 7 ? '\n' : ' ');
}
#endif
}
}
static int abort_if_not_full_device (int fd, __u64 lba, const char *devname, const char *msg)
{
__u64 start_lba;
int i, err, shortened = 0;
char *fdevname = strdup(devname);
err = get_dev_geometry(fd, NULL, NULL, NULL, &start_lba, NULL);
if (err)
exit(err);
for (i = strlen(fdevname); --i > 2 && (fdevname[i] >= '0' && fdevname[i] <= '9');) {
fdevname[i] = '\0';
shortened = 1;
}
if (!shortened)
fdevname = strdup("the full disk");
if (start_lba == 0ULL)
return 0;
if (start_lba == START_LBA_UNKNOWN || fd_is_raid(fd)) {
fprintf(stderr, "%s is a RAID device: please specify an absolute LBA of a raw member device instead (raid1 only)\n", devname);
} else if (msg) {
fprintf(stderr, "%s\n", msg);
} else {
fprintf(stderr, "Device %s has non-zero LBA starting offset of %llu.\n", devname, start_lba);
fprintf(stderr, "Please use an absolute LBA with the /dev/ entry for the raw device, rather than a partition or raid name.\n");
fprintf(stderr, "%s is probably a partition of %s (?)\n", devname, fdevname);
fprintf(stderr, "The absolute LBA of sector %llu from %s should be %llu\n", lba, devname, start_lba + lba);
}
fprintf(stderr, "Aborting.\n");
exit(EINVAL);
}
static __u16 *get_dco_identify_data (int fd, int quietly)
{
static __u8 args[4+512];
__u16 *dco = (void *)(args + 4);
int i;
memset(args, 0, sizeof(args));
args[0] = ATA_OP_DCO;
args[2] = 0xc2;
args[3] = 1;
if (do_drive_cmd(fd, args, 0)) {
if (!quietly)
perror(" HDIO_DRIVE_CMD(dco_identify) failed");
return NULL;
} else {
/* byte-swap the little-endian DCO data to match byte-order on host CPU */
for (i = 0; i < 0x100; ++i)
__le16_to_cpus(&dco[i]);
//dump_sectors(dco, 1);
return dco;
}
}