1 /* vi: set sw=4 ts=4: */
3 * hdparm implementation for busybox
6 * Copyright (C) [2003] by [Matteo Croce] <pinnolo@tin.it>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 * This program is based on the source code of hdparm: see below...
25 * hdparm.c - Command line interface to get/set hard disk parameters
26 * - by Mark Lord (C) 1994-2002 -- freely distributable
36 #include <sys/ioctl.h>
38 #include <sys/sysmacros.h>
40 #include <sys/times.h>
41 #include <sys/mount.h>
43 #include <linux/types.h>
44 #include <linux/hdreg.h>
45 #include <linux/major.h>
46 #include <asm/byteorder.h>
49 #if __BYTE_ORDER == __BIG_ENDIAN
56 #define ATA_DEV 0x0000
57 #define ATAPI_DEV 0x0001
59 /* word definitions */
60 /* ---------------- */
61 #define GEN_CONFIG 0 /* general configuration */
62 #define LCYLS 1 /* number of logical cylinders */
63 #define CONFIG 2 /* specific configuration */
64 #define LHEADS 3 /* number of logical heads */
65 #define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
66 #define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
67 #define LSECTS 6 /* number of logical sectors/track */
68 #define START_SERIAL 10 /* ASCII serial number */
69 #define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
70 #define BUF_TYPE 20 /* buffer type (ATA-1) */
71 #define BUFFER__SIZE 21 /* buffer size (ATA-1) */
72 #define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
73 #define START_FW_REV 23 /* ASCII firmware revision */
74 #define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
75 #define START_MODEL 27 /* ASCII model number */
76 #define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
77 #define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
78 #define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
79 #define CAPAB_0 49 /* capabilities */
81 #define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
82 #define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
83 #define WHATS_VALID 53 /* what fields are valid */
84 #define LCYLS_CUR 54 /* current logical cylinders */
85 #define LHEADS_CUR 55 /* current logical heads */
86 #define LSECTS_CUR 56 /* current logical sectors/track */
87 #define CAPACITY_LSB 57 /* current capacity in sectors */
88 #define CAPACITY_MSB 58
89 #define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
90 #define LBA_SECTS_LSB 60 /* LBA: total number of user */
91 #define LBA_SECTS_MSB 61 /* addressable sectors */
92 #define SINGLE_DMA 62 /* singleword DMA modes */
93 #define MULTI_DMA 63 /* multiword DMA modes */
94 #define ADV_PIO_MODES 64 /* advanced PIO modes supported */
95 /* multiword DMA xfer cycle time: */
96 #define DMA_TIME_MIN 65 /* - minimum */
97 #define DMA_TIME_NORM 66 /* - manufacturer's recommended */
98 /* minimum PIO xfer cycle time: */
99 #define PIO_NO_FLOW 67 /* - without flow control */
100 #define PIO_FLOW 68 /* - with IORDY flow control */
101 #define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
102 #define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
103 #define CDR_MAJOR 73 /* CD ROM: major version number */
104 #define CDR_MINOR 74 /* CD ROM: minor version number */
105 #define QUEUE_DEPTH 75 /* queue depth */
106 #define MAJOR 80 /* major version number */
107 #define MINOR 81 /* minor version number */
108 #define CMDS_SUPP_0 82 /* command/feature set(s) supported */
109 #define CMDS_SUPP_1 83
110 #define CMDS_SUPP_2 84
111 #define CMDS_EN_0 85 /* command/feature set(s) enabled */
114 #define ULTRA_DMA 88 /* ultra DMA modes */
115 /* time to complete security erase */
116 #define ERASE_TIME 89 /* - ordinary */
117 #define ENH_ERASE_TIME 90 /* - enhanced */
118 #define ADV_PWR 91 /* current advanced power management level
119 in low byte, 0x40 in high byte. */
120 #define PSWD_CODE 92 /* master password revision code */
121 #define HWRST_RSLT 93 /* hardware reset result */
122 #define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
123 #define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
124 #define LBA_MID 101 /* bits are used, but addr 103 */
125 #define LBA_48_MSB 102 /* has been reserved for LBA in */
126 #define LBA_64_MSB 103 /* the future. */
127 #define RM_STAT 127 /* removable media status notification feature set support */
128 #define SECU_STATUS 128 /* security status */
129 #define CFA_PWR_MODE 160 /* CFA power mode 1 */
130 #define START_MEDIA 176 /* media serial number */
131 #define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
132 #define START_MANUF 196 /* media manufacturer I.D. */
133 #define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
134 #define INTEGRITY 255 /* integrity word */
136 /* bit definitions within the words */
137 /* -------------------------------- */
139 /* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
141 #define VALID_VAL 0x4000
142 /* many words are considered invalid if they are either all-0 or all-1 */
143 #define NOVAL_0 0x0000
144 #define NOVAL_1 0xffff
146 /* word 0: gen_config */
147 #define NOT_ATA 0x8000
148 #define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
149 #define MEDIA_REMOVABLE 0x0080
150 #define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
151 #define INCOMPLETE 0x0004
152 #define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
153 #define DRQ_RESPONSE_TIME 0x0060
154 #define DRQ_3MS_VAL 0x0000
155 #define DRQ_INTR_VAL 0x0020
156 #define DRQ_50US_VAL 0x0040
157 #define PKT_SIZE_SUPPORTED 0x0003
158 #define PKT_SIZE_12_VAL 0x0000
159 #define PKT_SIZE_16_VAL 0x0001
160 #define EQPT_TYPE 0x1f00
165 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
166 static const char *pkt_str[] = {
167 "Direct-access device", /* word 0, bits 12-8 = 00 */
168 "Sequential-access device", /* word 0, bits 12-8 = 01 */
169 "Printer", /* word 0, bits 12-8 = 02 */
170 "Processor", /* word 0, bits 12-8 = 03 */
171 "Write-once device", /* word 0, bits 12-8 = 04 */
172 "CD-ROM", /* word 0, bits 12-8 = 05 */
173 "Scanner", /* word 0, bits 12-8 = 06 */
174 "Optical memory", /* word 0, bits 12-8 = 07 */
175 "Medium changer", /* word 0, bits 12-8 = 08 */
176 "Communications device", /* word 0, bits 12-8 = 09 */
177 "ACS-IT8 device", /* word 0, bits 12-8 = 0a */
178 "ACS-IT8 device", /* word 0, bits 12-8 = 0b */
179 "Array controller", /* word 0, bits 12-8 = 0c */
180 "Enclosure services", /* word 0, bits 12-8 = 0d */
181 "Reduced block command device", /* word 0, bits 12-8 = 0e */
182 "Optical card reader/writer", /* word 0, bits 12-8 = 0f */
183 "", /* word 0, bits 12-8 = 10 */
184 "", /* word 0, bits 12-8 = 11 */
185 "", /* word 0, bits 12-8 = 12 */
186 "", /* word 0, bits 12-8 = 13 */
187 "", /* word 0, bits 12-8 = 14 */
188 "", /* word 0, bits 12-8 = 15 */
189 "", /* word 0, bits 12-8 = 16 */
190 "", /* word 0, bits 12-8 = 17 */
191 "", /* word 0, bits 12-8 = 18 */
192 "", /* word 0, bits 12-8 = 19 */
193 "", /* word 0, bits 12-8 = 1a */
194 "", /* word 0, bits 12-8 = 1b */
195 "", /* word 0, bits 12-8 = 1c */
196 "", /* word 0, bits 12-8 = 1d */
197 "", /* word 0, bits 12-8 = 1e */
198 "Unknown", /* word 0, bits 12-8 = 1f */
200 static const char *ata1_cfg_str[] = { /* word 0 in ATA-1 mode */
201 "reserved", /* bit 0 */
202 "hard sectored", /* bit 1 */
203 "soft sectored", /* bit 2 */
204 "not MFM encoded ", /* bit 3 */
205 "head switch time > 15us", /* bit 4 */
206 "spindle motor control option", /* bit 5 */
207 "fixed drive", /* bit 6 */
208 "removable drive", /* bit 7 */
209 "disk xfer rate <= 5Mbs", /* bit 8 */
210 "disk xfer rate > 5Mbs, <= 10Mbs", /* bit 9 */
211 "disk xfer rate > 5Mbs", /* bit 10 */
212 "rotational speed tol.", /* bit 11 */
213 "data strobe offset option", /* bit 12 */
214 "track offset option", /* bit 13 */
215 "format speed tolerance gap reqd", /* bit 14 */
220 /* word 1: number of logical cylinders */
221 #define LCYLS_MAX 0x3fff /* maximum allowable value */
223 /* word 2: specific configureation
224 * (a) require SET FEATURES to spin-up
225 * (b) require spin-up to fully reply to IDENTIFY DEVICE
227 #define STBY_NID_VAL 0x37c8 /* (a) and (b) */
228 #define STBY_ID_VAL 0x738c /* (a) and not (b) */
229 #define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
230 #define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
232 /* words 47 & 59: sector_xfer_max & sector_xfer_cur */
233 #define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
234 #define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
236 /* word 49: capabilities 0 */
237 #define STD_STBY 0x2000 /* 1=standard values supported (ATA);
238 0=vendor specific values */
239 #define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
240 #define IORDY_OFF 0x0400 /* 1=may be disabled */
241 #define LBA_SUP 0x0200 /* 1=Logical Block Address support */
242 #define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
243 #define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
244 #define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
245 #define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
246 #define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
248 /* word 50: capabilities 1 */
249 #define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
251 /* words 51 & 52: PIO & DMA cycle times */
252 #define MODE 0xff00 /* the mode is in the MSBs */
254 /* word 53: whats_valid */
255 #define OK_W88 0x0004 /* the ultra_dma info is valid */
256 #define OK_W64_70 0x0002 /* see above for word descriptions */
257 #define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
259 /*word 63,88: dma_mode, ultra_dma_mode*/
260 #define MODE_MAX 7 /* bit definitions force udma <=7 (when
261 * udma >=8 comes out it'll have to be
262 * defined in a new dma_mode word!) */
264 /* word 64: PIO transfer modes */
265 #define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
266 #define PIO_MODE_MAX 8 /* but all 8 bits are defined */
268 /* word 75: queue_depth */
269 #define DEPTH_BITS 0x001f /* bits used for queue depth */
271 /* words 80-81: version numbers */
272 /* NOVAL_0 or NOVAL_1 means device does not report version */
274 /* word 81: minor version number */
275 #define MINOR_MAX 0x1C
276 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
277 static const char *minor_str[] = { /* word 81 value: */
278 "device does not report version", /* 0x0000 */
279 "ATA-1 X3T9.2 781D prior to revision 4", /* 0x0001 */
280 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
281 "ATA-1 X3T9.2 781D revision 4", /* 0x0003 */
282 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
283 "ATA-2 X3T10 948D prior to revision 2k", /* 0x0005 */
284 "ATA-3 X3T10 2008D revision 1", /* 0x0006 */
285 "ATA-2 X3T10 948D revision 2k", /* 0x0007 */
286 "ATA-3 X3T10 2008D revision 0", /* 0x0008 */
287 "ATA-2 X3T10 948D revision 3", /* 0x0009 */
288 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
289 "ATA-3 X3T10 2008D revision 6", /* 0x000b */
290 "ATA-3 X3T13 2008D revision 7 and 7a", /* 0x000c */
291 "ATA/ATAPI-4 X3T13 1153D revision 6", /* 0x000d */
292 "ATA/ATAPI-4 T13 1153D revision 13", /* 0x000e */
293 "ATA/ATAPI-4 X3T13 1153D revision 7", /* 0x000f */
294 "ATA/ATAPI-4 T13 1153D revision 18", /* 0x0010 */
295 "ATA/ATAPI-4 T13 1153D revision 15", /* 0x0011 */
296 "ATA/ATAPI-4 published, ANSI NCITS 317-1998", /* 0x0012 */
297 "ATA/ATAPI-5 T13 1321D revision 3",
298 "ATA/ATAPI-4 T13 1153D revision 14", /* 0x0014 */
299 "ATA/ATAPI-5 T13 1321D revision 1", /* 0x0015 */
300 "ATA/ATAPI-5 published, ANSI NCITS 340-2000", /* 0x0016 */
301 "ATA/ATAPI-4 T13 1153D revision 17", /* 0x0017 */
302 "ATA/ATAPI-6 T13 1410D revision 0", /* 0x0018 */
303 "ATA/ATAPI-6 T13 1410D revision 3a", /* 0x0019 */
304 "Reserved", /* 0x001a */
305 "ATA/ATAPI-6 T13 1410D revision 2", /* 0x001b */
306 "ATA/ATAPI-6 T13 1410D revision 1", /* 0x001c */
307 "reserved" /* 0x001d */
308 "reserved" /* 0x001e */
309 "reserved" /* 0x001f-0xfffe*/
312 static const char actual_ver[] = {
314 0, /* 0x0000 WARNING: */
315 1, /* 0x0001 WARNING: */
316 1, /* 0x0002 WARNING: */
317 1, /* 0x0003 WARNING: */
318 2, /* 0x0004 WARNING: This array */
319 2, /* 0x0005 WARNING: corresponds */
320 3, /* 0x0006 WARNING: *exactly* */
321 2, /* 0x0007 WARNING: to the ATA/ */
322 3, /* 0x0008 WARNING: ATAPI version */
323 2, /* 0x0009 WARNING: listed in */
324 3, /* 0x000a WARNING: the */
325 3, /* 0x000b WARNING: minor_str */
326 3, /* 0x000c WARNING: array */
327 4, /* 0x000d WARNING: above. */
328 4, /* 0x000e WARNING: */
329 4, /* 0x000f WARNING: if you change */
330 4, /* 0x0010 WARNING: that one, */
331 4, /* 0x0011 WARNING: change this one */
332 4, /* 0x0012 WARNING: too!!! */
333 5, /* 0x0013 WARNING: */
334 4, /* 0x0014 WARNING: */
335 5, /* 0x0015 WARNING: */
336 5, /* 0x0016 WARNING: */
337 4, /* 0x0017 WARNING: */
338 6, /* 0x0018 WARNING: */
339 6, /* 0x0019 WARNING: */
340 0, /* 0x001a WARNING: */
341 6, /* 0x001b WARNING: */
342 6, /* 0x001c WARNING: */
343 0 /* 0x001d-0xfffe */
346 /* words 82-84: cmds/feats supported */
347 #define CMDS_W82 0x77ff /* word 82: defined command locations*/
348 #define CMDS_W83 0x3fff /* word 83: defined command locations*/
349 #define CMDS_W84 0x002f /* word 83: defined command locations*/
350 #define SUPPORT_48_BIT 0x0400
351 #define NUM_CMD_FEAT_STR 48
353 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
354 static const char *cmd_feat_str[] = {
355 "", /* word 82 bit 15: obsolete */
356 "NOP cmd", /* word 82 bit 14 */
357 "READ BUFFER cmd", /* word 82 bit 13 */
358 "WRITE BUFFER cmd", /* word 82 bit 12 */
359 "", /* word 82 bit 11: obsolete */
360 "Host Protected Area feature set", /* word 82 bit 10 */
361 "DEVICE RESET cmd", /* word 82 bit 9 */
362 "SERVICE interrupt", /* word 82 bit 8 */
363 "Release interrupt", /* word 82 bit 7 */
364 "Look-ahead", /* word 82 bit 6 */
365 "Write cache", /* word 82 bit 5 */
366 "PACKET command feature set", /* word 82 bit 4 */
367 "Power Management feature set", /* word 82 bit 3 */
368 "Removable Media feature set", /* word 82 bit 2 */
369 "Security Mode feature set", /* word 82 bit 1 */
370 "SMART feature set", /* word 82 bit 0 */
372 "", /* word 83 bit 15: !valid bit */
373 "", /* word 83 bit 14: valid bit */
374 "FLUSH CACHE EXT command", /* word 83 bit 13 */
375 "Mandatory FLUSH CACHE command ", /* word 83 bit 12 */
376 "Device Configuration Overlay feature set ",
377 "48-bit Address feature set ", /* word 83 bit 10 */
379 "SET MAX security extension", /* word 83 bit 8 */
380 "Address Offset Reserved Area Boot", /* word 83 bit 7 */
381 "SET FEATURES subcommand required to spinup after power up",
382 "Power-Up In Standby feature set", /* word 83 bit 5 */
383 "Removable Media Status Notification feature set",
384 "Advanced Power Management feature set",/* word 83 bit 3 */
385 "CFA feature set", /* word 83 bit 2 */
386 "READ/WRITE DMA QUEUED", /* word 83 bit 1 */
387 "DOWNLOAD MICROCODE cmd", /* word 83 bit 0 */
389 "", /* word 84 bit 15: !valid bit */
390 "", /* word 84 bit 14: valid bit */
391 "", /* word 84 bit 13: reserved */
392 "", /* word 84 bit 12: reserved */
393 "", /* word 84 bit 11: reserved */
394 "", /* word 84 bit 10: reserved */
395 "", /* word 84 bit 9: reserved */
396 "", /* word 84 bit 8: reserved */
397 "", /* word 84 bit 7: reserved */
398 "", /* word 84 bit 6: reserved */
399 "General Purpose Logging feature set", /* word 84 bit 5 */
400 "", /* word 84 bit 4: reserved */
401 "Media Card Pass Through Command feature set ",
402 "Media serial number ", /* word 84 bit 2 */
403 "SMART self-test ", /* word 84 bit 1 */
404 "SMART error logging " /* word 84 bit 0 */
409 /* words 85-87: cmds/feats enabled */
410 /* use cmd_feat_str[] to display what commands and features have
411 * been enabled with words 85-87
414 /* words 89, 90, SECU ERASE TIME */
415 #define ERASE_BITS 0x00ff
417 /* word 92: master password revision */
418 /* NOVAL_0 or NOVAL_1 means no support for master password revision */
420 /* word 93: hw reset result */
421 #define CBLID 0x2000 /* CBLID status */
422 #define RST0 0x0001 /* 1=reset to device #0 */
423 #define DEV_DET 0x0006 /* how device num determined */
424 #define JUMPER_VAL 0x0002 /* device num determined by jumper */
425 #define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
427 /* word 127: removable media status notification feature set support */
428 #define RM_STAT_BITS 0x0003
429 #define RM_STAT_SUP 0x0001
431 /* word 128: security */
432 #define SECU_ENABLED 0x0002
433 #define SECU_LEVEL 0x0010
434 #define NUM_SECU_STR 6
435 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
436 static const char *secu_str[] = {
437 "supported", /* word 128, bit 0 */
438 "enabled", /* word 128, bit 1 */
439 "locked", /* word 128, bit 2 */
440 "frozen", /* word 128, bit 3 */
441 "expired: security count", /* word 128, bit 4 */
442 "supported: enhanced erase" /* word 128, bit 5 */
446 /* word 160: CFA power mode */
447 #define VALID_W160 0x8000 /* 1=word valid */
448 #define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
449 #define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
450 #define MAX_AMPS 0x0fff /* value = max current in ma */
452 /* word 255: integrity */
453 #define SIG 0x00ff /* signature location */
454 #define SIG_VAL 0x00A5 /* signature value */
456 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
457 static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode) {
460 for(ii = 0; ii <= MODE_MAX; ii++) {
461 if(mode_sel & 0x0001) {
462 printf("*%cdma%u ",cc,ii);
463 if(*have_mode) err_dma = 1;
465 } else if(mode_sup & 0x0001) {
466 printf("%cdma%u ",cc,ii);
468 mode_sup >>=1; mode_sel >>=1;
472 static void print_ascii(uint16_t *p, uint8_t length) {
476 /* find first non-space & print it */
477 for(ii = 0; ii< length; ii++) {
478 if(((char) 0x00ff&((*p)>>8)) != ' ') break;
479 if((cl = (char) 0x00ff&(*p)) != ' ') {
480 if(cl != '\0') printf("%c",cl);
487 for(; ii< length; ii++) {
488 if(!(*p)) break; /* some older devices have NULLs */
489 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
495 /* identify() is the only extern function used across two source files. The
496 others, though, were declared in hdparm.c with global scope; since other
497 functions in that file have static (file) scope, I assume the difference is
499 static void identify (uint16_t *id_supplied, const char *devname)
502 char *id_file = NULL, fmt[]="/proc/ide/%s/identify";
504 uint16_t val[256], ii, jj, kk;
505 uint16_t like_std = 1, std = 0, min_std = 0xffff;
506 uint16_t dev = NO_DEV, eqpt = NO_DEV;
507 uint8_t have_mode = 0, err_dma = 0;
509 uint32_t ll, mm, nn, oo;
510 __u64 bbbig; /* (:) */
513 #if __BYTE_ORDER == __BIG_ENDIAN
514 swab(id_supplied, val, sizeof(val));
516 memcpy(val, id_supplied, sizeof(val));
519 id_file = xcalloc(1, strlen(devname)+1+strlen(fmt));
520 sprintf(id_file, fmt, devname);
522 /* open the file, read in all the info and close it */
523 if (id_file == NULL) {
525 } else if(NULL == (fl = fopen(id_file, "r")))
526 bb_perror_msg_and_die(id_file);
527 /* calculate checksum over all bytes */
528 for(ii = GEN_CONFIG; ii<=INTEGRITY; ii++) {
529 unsigned int scratch;
530 if(1 != fscanf(fl,"%04x",&scratch)) break;
531 val[ii] = (uint16_t)scratch;
532 chksum += val[ii] + (val[ii] >> 8);
535 if(ii < (INTEGRITY+1))
536 bb_error_msg_and_die("Input file wrong format or length");
540 /* check if we recognise the device type */
542 if(!(val[GEN_CONFIG] & NOT_ATA)) {
544 printf("ATA device, with ");
545 } else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL) {
548 printf("CompactFlash ATA device, with ");
549 } else if(!(val[GEN_CONFIG] & NOT_ATAPI)) {
551 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
552 printf("ATAPI %s, with ", pkt_str[eqpt]);
555 bb_error_msg_and_die("Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n");
557 if(!(val[GEN_CONFIG] & MEDIA_REMOVABLE))
559 printf("removable media\n");
562 /* Info from the specific configuration word says whether or not the
563 * ID command completed correctly. It is only defined, however in
564 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
565 * standards. Since the values allowed for this word are extremely
566 * specific, it should be safe to check it now, even though we don't
567 * know yet what standard this device is using.
569 if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
570 (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) ) {
572 if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL))
573 printf("powers-up in standby; SET FEATURES subcmd spins-up.\n");
574 if(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) &&
575 (val[GEN_CONFIG] & INCOMPLETE))
576 printf("\n\tWARNING: ID response incomplete.\n\tWARNING: Following data may be incorrect.\n\n");
579 /* output the model and serial numbers and the fw revision */
580 if(val[START_MODEL]) {
581 printf("\t%-20s","Model Number:");
582 print_ascii(&val[START_MODEL], LENGTH_MODEL);
584 if(val[START_SERIAL]) {
585 printf("\t%-20s","Serial Number:");
586 print_ascii( &val[START_SERIAL], LENGTH_SERIAL);
588 if(val[START_FW_REV]) {
589 printf("\t%-20s","Firmware Revision:");
590 print_ascii(&val[START_FW_REV], LENGTH_FW_REV);
592 if(val[START_MEDIA]) {
593 printf("\t%-20s","Media Serial Num:");
594 print_ascii(&val[START_MEDIA], LENGTH_MEDIA);
596 if(val[START_MANUF]) {
597 printf("\t%-20s","Media Manufacturer:");
598 print_ascii(&val[START_MANUF], LENGTH_MANUF);
601 /* major & minor standards version number (Note: these words were not
602 * defined until ATA-3 & the CDROM std uses different words.) */
603 printf("Standards:");
605 if(val[MINOR] && (val[MINOR] <= MINOR_MAX)){
606 if(like_std < 3) like_std = 3;
607 std = actual_ver[val[MINOR]];
609 printf("\n\tUsed: ");
610 printf("%s ",minor_str[val[MINOR]]);
613 /* looks like when they up-issue the std, they obsolete one;
614 * thus, only the newest 4 issues need be supported. (That's
615 * what "kk" and "min_std" are all about.) */
616 if(val[MAJOR] && (val[MAJOR] !=NOVAL_1)) {
617 printf("\n\tSupported: ");
618 jj = val[MAJOR] << 1;
619 kk = like_std >4 ? like_std-4: 0;
620 for(ii = 14; (ii >0)&&(ii>kk); ii--) {
625 kk = like_std >4 ? like_std-4: 0;
627 if(min_std > ii) min_std = ii;
631 if(like_std < 3) like_std = 3;
633 /* Figure out what standard the device is using if it hasn't told
634 * us. If we know the std, check if the device is using any of
635 * the words from the next level up. It happens.
637 if(like_std < std) like_std = std;
638 if(((std == 5) || (!std && (like_std < 6))) &&
639 ( (((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
640 ((val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
641 (((val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
642 (val[CMDS_SUPP_2] & CMDS_W84) ) ) ) {
644 } else if(((std == 4) || (!std && (like_std < 5))) &&
645 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
646 ((val[HWRST_RSLT] & VALID) == VALID_VAL) ||
647 (((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
648 ((val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) ) {
650 } else if(((std == 3) || (!std && (like_std < 4))) &&
651 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
652 (((val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
653 ((val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
654 ((val[CAPAB_1] & VALID) == VALID_VAL) ||
655 ((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
656 ((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) ) {
658 } else if(((std == 2) || (!std && (like_std < 3))) &&
659 ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) ) {
661 } else if(((std == 1) || (!std && (like_std < 2))) &&
662 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
663 (val[WHATS_VALID] & OK_W64_70)) ) {
667 printf("\n\tLikely used: %u\n",like_std);
668 } else if(like_std > std) {
669 printf("& some of %u\n",like_std);
672 /* TBD: do CDROM stuff more thoroughly. For now... */
674 if(val[CDR_MINOR] == 9) {
676 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
678 if(val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1)) {
680 printf("\n\tSupported: CD-ROM ATAPI");
681 jj = val[CDR_MAJOR] >> 1;
682 for(ii = 1; ii <15; ii++) {
689 if(!kk) printf("\n\tLikely used CD-ROM ATAPI-1\n");
691 /* the cdrom stuff is more like ATA-2 than anything else, so: */
695 if(min_std == 0xffff) min_std = like_std > 4 ? like_std - 3 : 1;
697 printf("Configuration:\n");
698 /* more info from the general configuration word */
699 if((eqpt != CDROM) && (like_std == 1)) {
700 jj = val[GEN_CONFIG] >> 1;
701 for(ii = 1; ii < 15; ii++) {
702 if(jj & 0x0001) printf("\t%s\n",ata1_cfg_str[ii]);
706 if(dev == ATAPI_DEV) {
707 printf("\tDRQ response: "); /* Data Request (DRQ) */
708 switch(val[GEN_CONFIG] & DRQ_RESPONSE_TIME) {
709 case DRQ_3MS_VAL : printf("3ms.\n"); break;
710 case DRQ_INTR_VAL : printf("<=10ms with INTRQ\n"); break;
711 case DRQ_50US_VAL : printf("50us.\n"); break;
712 default : printf("unknown.\n"); break;
714 printf("\tPacket size: ");
715 switch(val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) {
716 case PKT_SIZE_12_VAL : printf("12 bytes\n"); break;
717 case PKT_SIZE_16_VAL : printf("16 bytes\n"); break;
718 default : printf("Unknown\n"); break;
721 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
722 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
724 if ( (ll > 0x00FBFC10) && (!val[LCYLS])) {
725 printf("\tCHS addressing not supported\n");
727 jj = val[WHATS_VALID] & OK_W54_58;
728 printf("\tLogical\t\tmax\tcurrent\n");
729 printf("\tcylinders\t%u\t%u\n",val[LCYLS],jj?val[LCYLS_CUR]:0);
730 printf("\theads\t\t%u\t%u\n",val[LHEADS],jj?val[LHEADS_CUR]:0);
731 printf("\tsectors/track\t%u\t%u\n",val[LSECTS],jj?val[LSECTS_CUR]:0);
733 if((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES])) {
734 printf("\tbytes/track: %u",val[TRACK_BYTES]);
735 printf("\tbytes/sector: %u\n",val[SECT_BYTES]);
738 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
739 if(like_std < 3) { /* check Endian of capacity bytes */
740 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
741 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
742 if(abs(mm - nn) > abs(oo - nn))
745 printf("\tCHS current addressable sectors:%11u\n",mm);
749 printf("\tLBA user addressable sectors:%11u\n",ll);
750 if( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
751 (val[CMDS_SUPP_1] & SUPPORT_48_BIT) ) {
752 bbbig = (__u64)val[LBA_64_MSB] << 48 |
753 (__u64)val[LBA_48_MSB] << 32 |
754 (__u64)val[LBA_MID] << 16 |
756 printf("\tLBA48 user addressable sectors:%11llu\n",bbbig);
758 if (!bbbig) bbbig = (__u64)(ll>mm ? ll : mm); /* # 512 byte blocks */
759 printf("\tdevice size with M = 1024*1024: %11llu MBytes\n",bbbig>>11);
760 bbbig = (bbbig<<9)/1000000;
761 printf("\tdevice size with M = 1000*1000: %11llu MBytes ",bbbig);
762 if(bbbig > 1000) printf("(%llu GB)\n",bbbig/1000);
767 /* hw support of commands (capabilities) */
768 printf("Capabilities:\n");
770 if(dev == ATAPI_DEV) {
772 if(val[CAPAB_0] & CMD_Q_SUP) printf("Cmd queuing, ");
774 if(val[CAPAB_0] & OVLP_SUP) printf("Cmd overlap, ");
776 if(val[CAPAB_0] & LBA_SUP) printf("LBA, ");
779 if(!(val[CAPAB_0] & IORDY_SUP)) printf("(may be)");
780 if(val[CAPAB_0] & IORDY_OFF) printf("(can");
781 else printf("(cannot");
782 printf(" be disabled)");
783 } else printf("no IORDY");
785 if((like_std == 1) && val[BUF_TYPE]) {
787 printf("\tBuffer type: %04x: ",kk);
788 if (kk < 2) printf("single port, single-sector");
789 else printf("dual port, multi-sector");
790 if (kk > 2) printf(" with read caching ability");
794 if((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1))) {
795 printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
798 if((min_std < 4) && (val[RW_LONG])) {
799 printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
802 if((eqpt != CDROM) && (like_std > 3)) {
803 printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
810 if(!val[DWORD_IO]) printf("not");
811 printf(" perform double-word IO\n");
813 printf("\tStandby timer values: spec'd by ");
814 if(val[CAPAB_0] & STD_STBY) printf("Standard");
815 else printf("Vendor");
816 if((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL)) {
817 if(val[CAPAB_1] & MIN_STANDBY_TIMER) printf(", with ");
818 else printf(", no ");
819 printf("device specific minimum\n");
822 printf("\tR/W multiple sector transfer: ");
823 if((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER)) {
824 printf("not supported\n");
826 printf("Max = %u\t",val[SECTOR_XFER_MAX] & SECTOR_XFER);
827 printf("Current = ");
828 if(val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
829 printf("%u\n",val[SECTOR_XFER_CUR] & SECTOR_XFER);
832 if((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008)) {
833 /* We print out elsewhere whether the APM feature is enabled or
834 not. If it's not enabled, let's not repeat the info; just print
836 printf("\tAdvanced power management level: ");
837 if ( (val[ADV_PWR] & 0xFF00) == 0x4000 ) {
838 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
840 printf("%u (0x%x)\n", apm_level, apm_level);
842 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
847 printf("\tRecommended acoustic management value: %u, current value: %u\n", (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
852 if(val[CAPAB_0] & SWRST_REQ) printf("\tATA sw reset required\n");
854 if(val[PKT_REL] || val[SVC_NBSY]) {
855 printf("\tOverlap support:");
856 if(val[PKT_REL]) printf(" %uus to release bus.",val[PKT_REL]);
857 if(val[SVC_NBSY]) printf(" %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
862 /* DMA stuff. Check that only one DMA mode is selected. */
864 if(!(val[CAPAB_0] & DMA_SUP)) {
865 printf("not supported\n");
867 if(val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
868 printf(" sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
869 if(val[SINGLE_DMA]) {
870 jj = val[SINGLE_DMA]; kk = val[SINGLE_DMA] >> 8;
871 err_dma += mode_loop(jj,kk,'s',&have_mode);
874 jj = val[MULTI_DMA]; kk = val[MULTI_DMA] >> 8;
875 err_dma += mode_loop(jj,kk,'m',&have_mode);
877 if((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) {
878 jj = val[ULTRA_DMA]; kk = val[ULTRA_DMA] >> 8;
879 err_dma += mode_loop(jj,kk,'u',&have_mode);
881 if(err_dma || !have_mode) printf("(?)");
884 if((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
885 printf("\t Interleaved DMA support\n");
887 if((val[WHATS_VALID] & OK_W64_70) &&
888 (val[DMA_TIME_MIN] || val[DMA_TIME_NORM])) {
889 printf("\t Cycle time:");
890 if(val[DMA_TIME_MIN])
891 printf(" min=%uns",val[DMA_TIME_MIN]);
892 if(val[DMA_TIME_NORM])
893 printf(" recommended=%uns",val[DMA_TIME_NORM]);
898 /* Programmed IO stuff */
900 /* If a drive supports mode n (e.g. 3), it also supports all modes less
901 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
902 if((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP)) {
903 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
904 for(ii = 0; ii <= PIO_MODE_MAX ; ii++) {
910 } else if(((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) ) {
911 for(ii = 0; ii <= val[PIO_MODE]>>8; ii++) {
915 } else printf("unknown\n");
916 if(val[WHATS_VALID] & OK_W64_70) {
917 if(val[PIO_NO_FLOW] || val[PIO_FLOW]) {
918 printf("\t Cycle time:");
920 printf(" no flow control=%uns", val[PIO_NO_FLOW]);
922 printf(" IORDY flow control=%uns", val[PIO_FLOW]);
927 if((val[CMDS_SUPP_1] & VALID) == VALID_VAL){
928 printf("Commands/features:\n\tEnabled\tSupported:\n");
929 jj = val[CMDS_SUPP_0];
931 for(ii = 0; ii < NUM_CMD_FEAT_STR; ii++) {
932 if((jj & 0x8000) && (*cmd_feat_str[ii] != '\0')) {
933 if(kk & 0x8000) printf("\t *");
935 printf("\t%s\n",cmd_feat_str[ii]);
939 jj = val[CMDS_SUPP_0+1+(ii/16)];
940 kk = val[CMDS_EN_0+1+(ii/16)];
943 if((val[CMDS_SUPP_2] & VALID) != VALID_VAL) ii +=16;
947 if((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
948 printf("\tRemovable Media Status Notification feature set supported\n");
952 if((eqpt != CDROM) && (like_std > 3) &&
953 (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME])) {
954 printf("Security: \n");
955 if(val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
956 printf("\tMaster password revision code = %u\n",val[PSWD_CODE]);
957 jj = val[SECU_STATUS];
959 for(ii = 0; ii < NUM_SECU_STR; ii++) {
960 if(!(jj & 0x0001)) printf("\tnot\t");
962 printf("%s\n",secu_str[ii]);
965 if(val[SECU_STATUS] & SECU_ENABLED) {
966 printf("\tSecurity level ");
967 if(val[SECU_STATUS] & SECU_LEVEL) printf("maximum\n");
968 else printf("high\n");
971 jj = val[ERASE_TIME] & ERASE_BITS;
972 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
975 if(jj) printf("%umin for SECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1);
976 if(kk) printf("%umin for ENHANCED SECURITY ERASE UNIT.", kk==ERASE_BITS ? 508 : kk<<1);
982 if((val[HWRST_RSLT] & VALID) == VALID_VAL) {
983 printf("HW reset results:\n");
984 if(val[HWRST_RSLT] & CBLID) printf("\tCBLID- above Vih\n");
985 else printf("\tCBLID- below Vih\n");
986 if(val[HWRST_RSLT] & RST0) {
987 printf("\tDevice num = 0");
988 jj = val[HWRST_RSLT];
990 printf("\tDevice num = 1");
991 jj = val[HWRST_RSLT] >> 8;
993 if((jj & DEV_DET) == JUMPER_VAL)
994 printf(" determined by the jumper");
995 else if((jj & DEV_DET) == CSEL_VAL)
996 printf(" determined by CSEL");
1000 /* more stuff from std 5 */
1001 if((like_std > 4) && (eqpt != CDROM)) {
1002 if(val[CFA_PWR_MODE] & VALID_W160) {
1003 printf("CFA power mode 1:\n\t");
1004 if(val[CFA_PWR_MODE] & PWR_MODE_OFF) printf("dis");
1007 if(val[CFA_PWR_MODE] & PWR_MODE_REQ)
1008 printf(" and required by some commands");
1010 if(val[CFA_PWR_MODE] & MAX_AMPS)
1011 printf("\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
1013 if((val[INTEGRITY] & SIG) == SIG_VAL) {
1014 printf("Checksum: ");
1015 if(chksum) printf("in");
1016 printf("correct\n");
1024 #define VERSION "v5.4"
1026 #define TIMING_MB 64
1027 #define TIMING_BUF_MB 1
1028 #define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
1029 #define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
1030 #define BUFCACHE_FACTOR 2
1032 #undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
1034 static int verbose = 0, get_identity = 0, get_geom = 0, noisy = 1, quiet = 0;
1035 static int flagcount = 0, do_flush = 0, is_scsi_hd = 0, is_xt_hd = 0;
1036 static int do_ctimings, do_timings = 0;
1038 static unsigned long set_readahead= 0, get_readahead= 0, readahead= 0;
1039 static unsigned long set_readonly = 0, get_readonly = 0, readonly = 0;
1040 static unsigned long set_unmask = 0, get_unmask = 0, unmask = 0;
1041 static unsigned long set_mult = 0, get_mult = 0, mult = 0;
1042 static unsigned long set_dma = 0, get_dma = 0, dma = 0;
1043 static unsigned long set_dma_q = 0, get_dma_q = 0, dma_q = 0;
1044 static unsigned long set_nowerr = 0, get_nowerr = 0, nowerr = 0;
1045 static unsigned long set_keep = 0, get_keep = 0, keep = 0;
1046 static unsigned long set_io32bit = 0, get_io32bit = 0, io32bit = 0;
1047 static unsigned long set_piomode = 0, noisy_piomode= 0;
1048 static int piomode = 0;
1049 #ifdef HDIO_DRIVE_CMD
1050 static unsigned long set_dkeep = 0, get_dkeep = 0, dkeep = 0;
1051 static unsigned long set_standby = 0, get_standby = 0, standby_requested= 0;
1052 static unsigned long set_xfermode = 0, get_xfermode = 0;
1053 static int xfermode_requested= 0;
1054 static unsigned long set_lookahead= 0, get_lookahead= 0, lookahead= 0;
1055 static unsigned long set_prefetch = 0, get_prefetch = 0, prefetch = 0;
1056 static unsigned long set_defects = 0, get_defects = 0, defects = 0;
1057 static unsigned long set_wcache = 0, get_wcache = 0, wcache = 0;
1058 static unsigned long set_doorlock = 0, get_doorlock = 0, doorlock = 0;
1059 static unsigned long set_seagate = 0, get_seagate = 0;
1060 static unsigned long set_standbynow = 0, get_standbynow = 0;
1061 static unsigned long set_sleepnow = 0, get_sleepnow = 0;
1062 static unsigned long get_powermode = 0;
1063 static unsigned long set_apmmode = 0, get_apmmode= 0, apmmode = 0;
1065 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1066 static int get_IDentity = 0;
1068 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1069 static int unregister_hwif = 0;
1070 static int hwif = 0;
1072 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1073 static int scan_hwif = 0;
1074 static int hwif_data = 0;
1075 static int hwif_ctrl = 0;
1076 static int hwif_irq = 0;
1078 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1079 static int set_busstate = 0, get_busstate = 0, busstate = 0;
1081 static int reread_partn = 0;
1083 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1084 static int perform_reset = 0;
1085 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1086 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1087 static int perform_tristate = 0, tristate = 0;
1088 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1090 // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1091 // then the HDIO_GET_IDENTITY only returned 142 bytes.
1092 // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1093 // and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1094 // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1095 // (which they should, but they should just return -EINVAL).
1097 // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1098 // On a really old system, it will not, and we will be confused.
1101 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1102 static const char *cfg_str[] =
1103 { "", " HardSect", " SoftSect", " NotMFM",
1104 " HdSw>15uSec", " SpinMotCtl", " Fixed", " Removeable",
1105 " DTR<=5Mbs", " DTR>5Mbs", " DTR>10Mbs", " RotSpdTol>.5%",
1106 " dStbOff", " TrkOff", " FmtGapReq", " nonMagnetic"
1109 static const char *BuffType[] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
1111 #define YN(b) (((b)==0)?"no":"yes")
1113 static void dmpstr (const char *prefix, unsigned int i, const char *s[], unsigned int maxi)
1116 printf("%s%u", prefix, i);
1118 printf("%s%s", prefix, s[i]);
1121 static void dump_identity (const struct hd_driveid *id)
1124 char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
1125 const unsigned short int *id_regs= (const void*) id;
1126 unsigned long capacity;
1128 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s",
1129 id->model, id->fw_rev, id->serial_no);
1130 printf("\n Config={");
1131 for (i=0; i<=15; i++) {
1132 if (id->config & (1<<i))
1133 printf("%s", cfg_str[i]);
1136 printf(" RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
1137 id->cyls, id->heads, id->sectors,
1138 id->track_bytes, id->sector_bytes, id->ecc_bytes);
1139 dmpstr(" BuffType=",id->buf_type,BuffType,3);
1140 printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
1141 if (id->max_multsect) {
1142 printf(", MultSect=");
1143 if (!(id->multsect_valid&1))
1144 printf("?%u?", id->multsect);
1145 else if (id->multsect)
1146 printf("%u", id->multsect);
1151 if (id->tPIO <= 5) {
1152 strcat(pmodes, "pio0 ");
1153 if (id->tPIO >= 1) strcat(pmodes, "pio1 ");
1154 if (id->tPIO >= 2) strcat(pmodes, "pio2 ");
1156 if (!(id->field_valid&1))
1157 printf(" (maybe):");
1158 #if __BYTE_ORDER == __BIG_ENDIAN
1159 capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
1161 capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
1163 printf(" CurCHS=%u/%u/%u, CurSects=%lu", id->cur_cyls, id->cur_heads, id->cur_sectors, capacity);
1164 printf(", LBA=%s", YN(id->capability&2));
1165 if (id->capability&2)
1166 printf(", LBAsects=%u", id->lba_capacity);
1168 if (id->capability&1) {
1169 if (id->dma_1word | id->dma_mword) {
1170 if (id->dma_1word & 0x100) strcat(dmodes,"*");
1171 if (id->dma_1word & 1) strcat(dmodes,"sdma0 ");
1172 if (id->dma_1word & 0x200) strcat(dmodes,"*");
1173 if (id->dma_1word & 2) strcat(dmodes,"sdma1 ");
1174 if (id->dma_1word & 0x400) strcat(dmodes,"*");
1175 if (id->dma_1word & 4) strcat(dmodes,"sdma2 ");
1176 if (id->dma_1word & 0xf800) strcat(dmodes,"*");
1177 if (id->dma_1word & 0xf8) strcat(dmodes,"sdma? ");
1178 if (id->dma_mword & 0x100) strcat(dmodes,"*");
1179 if (id->dma_mword & 1) strcat(dmodes,"mdma0 ");
1180 if (id->dma_mword & 0x200) strcat(dmodes,"*");
1181 if (id->dma_mword & 2) strcat(dmodes,"mdma1 ");
1182 if (id->dma_mword & 0x400) strcat(dmodes,"*");
1183 if (id->dma_mword & 4) strcat(dmodes,"mdma2 ");
1184 if (id->dma_mword & 0xf800) strcat(dmodes,"*");
1185 if (id->dma_mword & 0xf8) strcat(dmodes,"mdma? ");
1188 printf("\n IORDY=");
1189 if (id->capability&8)
1190 printf((id->capability&4) ? "on/off" : "yes");
1193 if ((id->capability&8) || (id->field_valid&2)) {
1194 if (id->field_valid&2) {
1195 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1196 if (id->eide_pio_modes & 1) strcat(pmodes, "pio3 ");
1197 if (id->eide_pio_modes & 2) strcat(pmodes, "pio4 ");
1198 if (id->eide_pio_modes &~3) strcat(pmodes, "pio? ");
1200 if (id->field_valid&4) {
1201 if (id->dma_ultra & 0x100) strcat(umodes,"*");
1202 if (id->dma_ultra & 0x001) strcat(umodes,"udma0 ");
1203 if (id->dma_ultra & 0x200) strcat(umodes,"*");
1204 if (id->dma_ultra & 0x002) strcat(umodes,"udma1 ");
1205 if (id->dma_ultra & 0x400) strcat(umodes,"*");
1206 if (id->dma_ultra & 0x004) strcat(umodes,"udma2 ");
1207 #ifdef __NEW_HD_DRIVE_ID
1208 if (id->hw_config & 0x2000) {
1209 #else /* !__NEW_HD_DRIVE_ID */
1210 if (id->word93 & 0x2000) {
1211 #endif /* __NEW_HD_DRIVE_ID */
1212 if (id->dma_ultra & 0x0800) strcat(umodes,"*");
1213 if (id->dma_ultra & 0x0008) strcat(umodes,"udma3 ");
1214 if (id->dma_ultra & 0x1000) strcat(umodes,"*");
1215 if (id->dma_ultra & 0x0010) strcat(umodes,"udma4 ");
1216 if (id->dma_ultra & 0x2000) strcat(umodes,"*");
1217 if (id->dma_ultra & 0x0020) strcat(umodes,"udma5 ");
1218 if (id->dma_ultra & 0x4000) strcat(umodes,"*");
1219 if (id->dma_ultra & 0x0040) strcat(umodes,"udma6 ");
1220 if (id->dma_ultra & 0x8000) strcat(umodes,"*");
1221 if (id->dma_ultra & 0x0080) strcat(umodes,"udma7 ");
1225 if ((id->capability&1) && (id->field_valid&2))
1226 printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1227 printf("\n PIO modes: %s", pmodes);
1229 printf("\n DMA modes: %s", dmodes);
1231 printf("\n UDMA modes: %s", umodes);
1233 printf("\n AdvancedPM=%s",YN(id_regs[83]&8));
1234 if (id_regs[83] & 8) {
1235 if (!(id_regs[86]&8))
1236 printf(": disabled (255)");
1237 else if ((id_regs[91]&0xFF00)!=0x4000)
1238 printf(": unknown setting");
1240 printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1242 if (id_regs[82]&0x20)
1243 printf(" WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
1244 #ifdef __NEW_HD_DRIVE_ID
1245 if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31)) {
1246 printf("\n Drive conforms to: ");
1247 if (id->minor_rev_num <= 31)
1248 printf("%s: ", minor_str[id->minor_rev_num]);
1250 printf("unknown: ");
1251 if (id->major_rev_num < 31) {
1252 for (i=0; i <= 15; i++) {
1253 if (id->major_rev_num & (1<<i))
1258 #endif /* __NEW_HD_DRIVE_ID */
1260 printf("\n * signifies the current active mode\n");
1265 static void flush_buffer_cache (int fd)
1267 fsync (fd); /* flush buffers */
1268 if (ioctl(fd, BLKFLSBUF, NULL)) /* do it again, big time */
1269 bb_perror_msg("BLKFLSBUF failed");
1270 #ifdef HDIO_DRIVE_CMD
1271 if (is_scsi_hd || is_xt_hd) {
1274 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
1275 bb_perror_msg("HDIO_DRIVE_CMD(null) (wait for flush complete) failed");
1280 static int seek_to_zero (int fd)
1282 if (lseek(fd, (off_t) 0, SEEK_SET)) {
1283 bb_perror_msg("lseek() failed");
1289 static int read_big_block (int fd, char *buf)
1292 if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES) {
1295 bb_perror_msg("read() failed");
1297 bb_error_msg("read(%u) returned %u bytes", TIMING_BUF_BYTES, rc);
1299 fputs ("read() hit EOF - device too small\n", stderr);
1303 /* access all sectors of buf to ensure the read fully completed */
1304 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1309 static double correction = 0.0;
1311 void time_cache (int fd)
1315 struct itimerval e1, e2;
1317 int timing_MB = TIMING_MB;
1319 if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1) {
1320 bb_perror_msg ("could not allocate sharedmem buf");
1323 if (shmctl(shmid, SHM_LOCK, NULL) == -1) {
1324 bb_perror_msg ("could not lock sharedmem buf");
1325 (void) shmctl(shmid, IPC_RMID, NULL);
1328 if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1) {
1329 bb_perror_msg ("could not attach sharedmem buf");
1330 (void) shmctl(shmid, IPC_RMID, NULL);
1333 if (shmctl(shmid, IPC_RMID, NULL) == -1)
1334 bb_perror_msg ("shmctl(,IPC_RMID,) failed");
1336 /* Clear out the device request queues & give them time to complete */
1340 /* Calculate a correction factor for the basic
1341 * overhead of doing a read() from the buffer cache.
1342 * To do this, we read the data once to "cache it" and
1343 * to force full preallocation of our timing buffer,
1344 * and then we re-read it 10 times while timing it.
1346 * getitimer() is used rather than gettimeofday() because
1347 * it is much more consistent (on my machine, at least).
1349 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1350 if (seek_to_zero (fd)) return;
1351 if (read_big_block (fd, buf)) return;
1352 printf(" Timing buffer-cache reads: ");
1355 /* Clear out the device request queues & give them time to complete */
1359 /* Time re-reading from the buffer-cache */
1360 getitimer(ITIMER_REAL, &e1);
1361 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i) {
1362 if (seek_to_zero (fd)) goto quit;
1363 if (read_big_block (fd, buf)) goto quit;
1365 getitimer(ITIMER_REAL, &e2);
1366 correction = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1367 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1369 /* Now remove the lseek() from the correction factor */
1370 getitimer(ITIMER_REAL, &e1);
1371 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i) {
1372 if (seek_to_zero (fd)) goto quit;
1374 getitimer(ITIMER_REAL, &e2);
1375 correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
1376 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1378 if ((BUFCACHE_FACTOR * timing_MB) >= correction) /* more than 1MB/s */
1379 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1380 (BUFCACHE_FACTOR * timing_MB), correction,
1381 (BUFCACHE_FACTOR * timing_MB) / correction);
1383 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1384 (BUFCACHE_FACTOR * timing_MB), correction,
1385 (BUFCACHE_FACTOR * timing_MB) / correction * 1024);
1386 correction /= BUFCACHE_FACTOR;
1388 flush_buffer_cache(fd);
1391 if (-1 == shmdt(buf))
1392 bb_perror_msg ("could not detach sharedmem buf");
1395 void time_device (int fd)
1400 struct itimerval e1, e2;
1402 int timing_MB = TIMING_MB;
1404 if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1) {
1405 bb_perror_msg ("could not allocate sharedmem buf");
1408 if (shmctl(shmid, SHM_LOCK, NULL) == -1) {
1409 bb_perror_msg ("could not lock sharedmem buf");
1410 (void) shmctl(shmid, IPC_RMID, NULL);
1413 if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1) {
1414 bb_perror_msg ("could not attach sharedmem buf");
1415 (void) shmctl(shmid, IPC_RMID, NULL);
1418 if (shmctl(shmid, IPC_RMID, NULL) == -1)
1419 bb_perror_msg ("shmctl(,IPC_RMID,) failed");
1421 /* Clear out the device request queues & give them time to complete */
1425 printf(" Timing buffered disk reads: ");
1429 * getitimer() is used rather than gettimeofday() because
1430 * it is much more consistent (on my machine, at least).
1432 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1434 /* Now do the timings for real */
1435 getitimer(ITIMER_REAL, &e1);
1436 for (i = TIMING_BUF_COUNT; i > 0; --i) {
1437 if (read_big_block (fd, buf)) goto quit;
1439 getitimer(ITIMER_REAL, &e2);
1441 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1442 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1443 if (timing_MB >= elapsed) /* more than 1MB/s */
1444 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1445 timing_MB, elapsed, timing_MB / elapsed);
1447 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1448 timing_MB, elapsed, timing_MB / elapsed * 1024);
1450 if (elapsed <= (correction * 2))
1451 printf("Hmm.. suspicious results: probably not enough free memory for a proper test.\n");
1452 #if 0 /* the "estimate" is just plain wrong for many systems.. */
1453 else if (correction != 0.0) {
1454 printf(" Estimating raw driver speed: ");
1455 elapsed -= correction;
1456 if (timing_MB >= elapsed) /* more than 1MB/s */
1457 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1458 timing_MB, elapsed, timing_MB / elapsed);
1460 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1461 timing_MB, elapsed, timing_MB / elapsed * 1024);
1465 if (-1 == shmdt(buf))
1466 bb_perror_msg ("could not detach sharedmem buf");
1469 static void no_scsi (void)
1472 bb_error_msg_and_die(" operation not supported on SCSI disks\n");
1476 static void no_xt (void)
1479 bb_error_msg_and_die (" operation not supported on XT disks\n");
1483 static void on_off (unsigned int value)
1485 printf(value ? " (on)\n" : " (off)\n");
1488 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1489 static void bus_state_value (unsigned int value)
1498 string = " (off)\n";
1500 case BUSSTATE_TRISTATE:
1501 string = " (tristate)\n";
1504 string = " (unknown: %d)\n";
1507 printf(string, value);
1511 #ifdef HDIO_DRIVE_CMD
1512 static void interpret_standby (unsigned int standby)
1516 case 0: printf("off");
1518 case 252: printf("21 minutes");
1520 case 253: printf("vendor-specific");
1522 case 254: printf("?reserved");
1524 case 255: printf("21 minutes + 15 seconds");
1527 if (standby <= 240) {
1528 unsigned int secs = standby * 5;
1529 unsigned int mins = secs / 60;
1531 if (mins) printf("%u minutes", mins);
1532 if (mins && secs) printf(" + ");
1533 if (secs) printf("%u seconds", secs);
1534 } else if (standby <= 251) {
1535 unsigned int mins = (standby - 240) * 30;
1536 unsigned int hrs = mins / 60;
1538 if (hrs) printf("%u hours", hrs);
1539 if (hrs && mins) printf(" + ");
1540 if (mins) printf("%u minutes", mins);
1542 printf("illegal value");
1548 struct xfermode_entry {
1553 static const struct xfermode_entry xfermode_table[] = {
1589 static int translate_xfermode(char * name)
1591 const struct xfermode_entry *tmp;
1596 for (tmp = xfermode_table; tmp->name != NULL; ++tmp) {
1597 if (!strcmp(name, tmp->name))
1601 val = strtol(name, &endptr, 10);
1602 if (*endptr == '\0')
1608 static void interpret_xfermode (unsigned int xfermode)
1612 case 0: printf("default PIO mode");
1614 case 1: printf("default PIO mode, disable IORDY");
1623 case 15: printf("PIO flow control mode%u", xfermode-8);
1632 case 23: printf("singleword DMA mode%u", xfermode-16);
1641 case 39: printf("multiword DMA mode%u", xfermode-32);
1650 case 71: printf("UltraDMA mode%u", xfermode-64);
1653 printf("unknown, probably not valid");
1658 #endif /* HDIO_DRIVE_CMD */
1661 #define VXVM_MAJOR 199
1665 #define CCISS_MAJOR 104
1668 static void process_dev (char *devname)
1671 static long parm, multcount;
1672 struct stat stat_buf;
1673 #ifndef HDIO_DRIVE_CMD
1674 int force_operation = 0;
1676 if (stat(devname,&stat_buf))
1677 bb_perror_msg_and_die(devname);
1679 switch(major(stat_buf.st_rdev)) {
1680 #ifdef SCSI_DISK0_MAJOR
1681 case (SCSI_DISK0_MAJOR):
1682 case (SCSI_DISK1_MAJOR):
1683 case (SCSI_DISK2_MAJOR):
1684 case (SCSI_DISK3_MAJOR):
1685 case (SCSI_DISK4_MAJOR):
1686 case (SCSI_DISK5_MAJOR):
1687 case (SCSI_DISK6_MAJOR):
1688 case (SCSI_DISK7_MAJOR):
1690 case (SCSI_DISK_MAJOR):
1696 #ifdef LVM_BLK_MAJOR
1697 case (LVM_BLK_MAJOR):
1702 #ifdef XT_DISK_MAJOR
1703 case (XT_DISK_MAJOR):
1733 break; /* do nothing */
1735 bb_error_msg_and_die("%s not supported by hdparm",devname);
1738 fd = open (devname, O_RDONLY|O_NONBLOCK);
1740 bb_perror_msg_and_die(devname);
1742 printf("\n%s:\n", devname);
1744 if (set_readahead) {
1746 printf(" setting fs readahead to %ld\n", readahead);
1747 if (ioctl(fd, BLKRASET, readahead))
1748 bb_perror_msg(" BLKRASET failed");
1750 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1751 if (unregister_hwif) {
1753 printf(" attempting to unregister hwif#%u\n", hwif);
1754 if (ioctl(fd, HDIO_UNREGISTER_HWIF, hwif))
1755 bb_perror_msg(" HDIO_UNREGISTER_HWIF failed");
1758 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1762 printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
1763 args[0] = hwif_data;
1764 args[1] = hwif_ctrl;
1766 if (ioctl(fd, HDIO_SCAN_HWIF, args))
1767 bb_perror_msg(" HDIO_SCAN_HWIF failed");
1773 if (noisy_piomode) {
1775 printf(" attempting to auto-tune PIO mode\n");
1776 else if (piomode < 100)
1777 printf(" attempting to set PIO mode to %d\n", piomode);
1778 else if (piomode < 200)
1779 printf(" attempting to set MDMA mode to %d\n", (piomode-100));
1781 printf(" attempting to set UDMA mode to %d\n", (piomode-200));
1783 if (ioctl(fd, HDIO_SET_PIO_MODE, piomode))
1784 bb_perror_msg(" HDIO_SET_PIO_MODE failed");
1790 printf(" setting 32-bit IO_support flag to %ld\n", io32bit);
1791 if (ioctl(fd, HDIO_SET_32BIT, io32bit))
1792 bb_perror_msg(" HDIO_SET_32BIT failed");
1798 printf(" setting multcount to %ld\n", mult);
1799 if (ioctl(fd, HDIO_SET_MULTCOUNT, mult))
1800 bb_perror_msg(" HDIO_SET_MULTCOUNT failed");
1801 #ifndef HDIO_DRIVE_CMD
1802 else force_operation = 1;
1807 printf(" setting readonly to %ld", readonly);
1810 if (ioctl(fd, BLKROSET, &readonly))
1811 bb_perror_msg(" BLKROSET failed");
1817 printf(" setting unmaskirq to %ld", unmask);
1820 if (ioctl(fd, HDIO_SET_UNMASKINTR, unmask))
1821 bb_perror_msg(" HDIO_SET_UNMASKINTR failed");
1823 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1827 printf(" setting using_dma to %ld", dma);
1830 if (ioctl(fd, HDIO_SET_DMA, dma))
1831 bb_perror_msg(" HDIO_SET_DMA failed");
1833 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
1837 printf(" setting DMA queue_depth to %ld", dma_q);
1840 if (ioctl(fd, HDIO_SET_QDMA, dma_q))
1841 bb_perror_msg(" HDIO_SET_QDMA failed");
1847 printf(" setting nowerr to %ld", nowerr);
1850 if (ioctl(fd, HDIO_SET_NOWERR, nowerr))
1851 bb_perror_msg(" HDIO_SET_NOWERR failed");
1857 printf(" setting keep_settings to %ld", keep);
1860 if (ioctl(fd, HDIO_SET_KEEPSETTINGS, keep))
1861 bb_perror_msg(" HDIO_SET_KEEPSETTINGS failed");
1863 #ifdef HDIO_DRIVE_CMD
1865 unsigned char args[4] = {0,0,0,0};
1868 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
1870 printf(" setting drive doorlock to %ld", doorlock);
1873 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1874 bb_perror_msg(" HDIO_DRIVE_CMD(doorlock) failed");
1877 /* lock/unlock the drive's "feature" settings */
1878 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1882 printf(" setting drive keep features to %ld", dkeep);
1885 args[2] = dkeep ? 0x66 : 0xcc;
1886 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1887 bb_perror_msg(" HDIO_DRIVE_CMD(keepsettings) failed");
1890 unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
1892 args[2] = defects ? 0x04 : 0x84;
1894 printf(" setting drive defect-mgmt to %ld\n", defects);
1895 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1896 bb_perror_msg(" HDIO_DRIVE_CMD(defectmgmt) failed");
1899 unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
1904 printf(" setting drive prefetch to %ld\n", prefetch);
1905 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1906 bb_perror_msg(" HDIO_DRIVE_CMD(setprefetch) failed");
1909 unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
1912 args[1] = xfermode_requested;
1914 printf(" setting xfermode to %d", xfermode_requested);
1915 interpret_xfermode(xfermode_requested);
1917 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1918 bb_perror_msg(" HDIO_DRIVE_CMD(setxfermode) failed");
1920 if (set_lookahead) {
1921 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1924 args[2] = lookahead ? 0xaa : 0x55;
1925 if (get_lookahead) {
1926 printf(" setting drive read-lookahead to %ld", lookahead);
1929 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1930 bb_perror_msg(" HDIO_DRIVE_CMD(setreadahead) failed");
1933 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1935 if (apmmode<1) apmmode=1;
1936 if (apmmode>255) apmmode=255;
1938 printf(" setting Advanced Power Management level to");
1940 /* disable Advanced Power Management */
1941 args[2] = 0x85; /* feature register */
1942 if (get_apmmode) printf(" disabled\n");
1944 /* set Advanced Power Management mode */
1945 args[2] = 0x05; /* feature register */
1946 args[1] = apmmode; /* sector count register */
1947 if (get_apmmode) printf(" 0x%02lX (%ld)\n",apmmode,apmmode);
1949 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1950 bb_perror_msg(" HDIO_DRIVE_CMD failed");
1953 #ifdef DO_FLUSHCACHE
1954 #ifndef WIN_FLUSHCACHE
1955 #define WIN_FLUSHCACHE 0xe7
1957 unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
1958 #endif /* DO_FLUSHCACHE */
1959 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1962 args[2] = wcache ? 0x02 : 0x82;
1964 printf(" setting drive write-caching to %ld", wcache);
1967 #ifdef DO_FLUSHCACHE
1968 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
1969 bb_perror_msg (" HDIO_DRIVE_CMD(flushcache) failed");
1970 #endif /* DO_FLUSHCACHE */
1971 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1972 bb_perror_msg(" HDIO_DRIVE_CMD(setcache) failed");
1973 #ifdef DO_FLUSHCACHE
1974 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
1975 bb_perror_msg (" HDIO_DRIVE_CMD(flushcache) failed");
1976 #endif /* DO_FLUSHCACHE */
1978 if (set_standbynow) {
1979 #ifndef WIN_STANDBYNOW1
1980 #define WIN_STANDBYNOW1 0xE0
1982 #ifndef WIN_STANDBYNOW2
1983 #define WIN_STANDBYNOW2 0x94
1985 unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
1986 unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
1989 printf(" issuing standby command\n");
1990 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
1991 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
1992 bb_perror_msg(" HDIO_DRIVE_CMD(standby) failed");
1995 #ifndef WIN_SLEEPNOW1
1996 #define WIN_SLEEPNOW1 0xE6
1998 #ifndef WIN_SLEEPNOW2
1999 #define WIN_SLEEPNOW2 0x99
2001 unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
2002 unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
2005 printf(" issuing sleep command\n");
2006 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2007 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
2008 bb_perror_msg(" HDIO_DRIVE_CMD(sleep) failed");
2011 unsigned char args[4] = {0xfb,0,0,0};
2015 printf(" disabling Seagate auto powersaving mode\n");
2016 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2017 bb_perror_msg(" HDIO_DRIVE_CMD(seagatepwrsave) failed");
2020 unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
2024 printf(" setting standby to %lu", standby_requested);
2025 interpret_standby(standby_requested);
2027 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2028 bb_perror_msg(" HDIO_DRIVE_CMD(setidle1) failed");
2030 #else /* HDIO_DRIVE_CMD */
2031 if (force_operation) {
2033 flush_buffer_cache(fd);
2034 if (-1 == read(fd, buf, sizeof(buf)))
2035 bb_perror_msg(" access failed");
2037 #endif /* HDIO_DRIVE_CMD */
2042 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity) {
2045 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount)) {
2046 if ((verbose && !is_xt_hd) || get_mult)
2047 bb_perror_msg(" HDIO_GET_MULTCOUNT failed");
2048 } else if (verbose | get_mult) {
2049 printf(" multcount = %2ld", multcount);
2053 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit) {
2056 if (ioctl(fd, HDIO_GET_32BIT, &parm))
2057 bb_perror_msg(" HDIO_GET_32BIT failed");
2059 printf(" IO_support =%3ld (", parm);
2061 case 0: printf("default ");
2062 case 2: printf("16-bit)\n");
2064 case 1: printf("32-bit)\n");
2066 case 3: printf("32-bit w/sync)\n");
2068 case 8: printf("Request-Queue-Bypass)\n");
2070 default:printf("\?\?\?)\n");
2074 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask) {
2077 if (ioctl(fd, HDIO_GET_UNMASKINTR, &parm))
2078 bb_perror_msg(" HDIO_GET_UNMASKINTR failed");
2080 printf(" unmaskirq = %2ld", parm);
2085 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2086 if ((verbose && !is_scsi_hd) || get_dma) {
2088 if (ioctl(fd, HDIO_GET_DMA, &parm))
2089 bb_perror_msg(" HDIO_GET_DMA failed");
2091 printf(" using_dma = %2ld", parm);
2093 printf(" (DMA-Assisted-PIO)\n");
2101 if(ioctl(fd, HDIO_GET_QDMA, &parm)) {
2102 bb_perror_msg(" HDIO_GET_QDMA failed");
2104 printf(" queue_depth = %2ld", parm);
2108 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep) {
2111 if (ioctl(fd, HDIO_GET_KEEPSETTINGS, &parm))
2112 bb_perror_msg(" HDIO_GET_KEEPSETTINGS failed");
2114 printf(" keepsettings = %2ld", parm);
2122 if (ioctl(fd, HDIO_GET_NOWERR, &parm))
2123 bb_perror_msg(" HDIO_GET_NOWERR failed");
2125 printf(" nowerr = %2ld", parm);
2129 if (verbose || get_readonly) {
2130 if (ioctl(fd, BLKROGET, &parm))
2131 bb_perror_msg(" BLKROGET failed");
2133 printf(" readonly = %2ld", parm);
2137 if ((verbose && !is_scsi_hd) || get_readahead) {
2138 if (ioctl(fd, BLKRAGET, &parm))
2139 bb_perror_msg(" BLKRAGET failed");
2141 printf(" readahead = %2ld", parm);
2145 if (verbose || get_geom) {
2146 static const char msg[] = " geometry = %u/%u/%u, sectors = %ld, start = %ld\n";
2147 static struct hd_geometry g;
2148 #ifdef HDIO_GETGEO_BIG
2149 static struct hd_big_geometry bg;
2152 if (ioctl(fd, BLKGETSIZE, &parm))
2153 bb_perror_msg(" BLKGETSIZE failed");
2154 #ifdef HDIO_GETGEO_BIG
2155 else if (!ioctl(fd, HDIO_GETGEO_BIG, &bg))
2156 printf(msg, bg.cylinders, bg.heads, bg.sectors, parm, bg.start);
2158 else if (ioctl(fd, HDIO_GETGEO, &g))
2159 bb_perror_msg(" HDIO_GETGEO failed");
2160 else printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
2162 #ifdef HDIO_DRIVE_CMD
2163 if (get_powermode) {
2164 #ifndef WIN_CHECKPOWERMODE1
2165 #define WIN_CHECKPOWERMODE1 0xE5
2167 #ifndef WIN_CHECKPOWERMODE2
2168 #define WIN_CHECKPOWERMODE2 0x98
2170 unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
2173 if (ioctl(fd, HDIO_DRIVE_CMD, &args)
2174 && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
2175 && ioctl(fd, HDIO_DRIVE_CMD, &args)) {
2176 if (errno != EIO || args[0] != 0 || args[1] != 0)
2181 state = (args[2] == 255) ? "active/idle" : "standby";
2183 printf(" drive state is: %s\n", state);
2186 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2187 if (perform_reset) {
2190 if (ioctl(fd, HDIO_DRIVE_RESET, NULL))
2191 bb_perror_msg(" HDIO_DRIVE_RESET failed");
2193 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2194 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2195 if (perform_tristate) {
2196 unsigned char args[4] = {0,tristate,0,0};
2199 if (ioctl(fd, HDIO_TRISTATE_HWIF, &args))
2200 bb_perror_msg(" HDIO_TRISTATE_HWIF failed");
2202 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2203 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2205 static struct hd_driveid id;
2210 if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
2211 if (multcount != -1) {
2212 id.multsect = multcount;
2213 id.multsect_valid |= 1;
2215 id.multsect_valid &= ~1;
2217 } else if (errno == -ENOMSG)
2218 printf(" no identification info available\n");
2220 bb_perror_msg(" HDIO_GET_IDENTITY failed");
2223 unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
2227 if (ioctl(fd, HDIO_DRIVE_CMD, &args)) {
2228 args[0] = WIN_PIDENTIFY;
2229 if (ioctl(fd, HDIO_DRIVE_CMD, &args)) {
2230 bb_perror_msg(" HDIO_DRIVE_CMD(identify) failed");
2231 goto identify_abort;
2234 for(i=0; i<(sizeof args)/2; i+=2) {
2235 __le16_to_cpus((uint16_t *)(&args[i]));
2237 identify((void *)&args[4], NULL);
2241 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2245 printf(" setting bus state to %d", busstate);
2246 bus_state_value(busstate);
2248 if (ioctl(fd, HDIO_SET_BUSSTATE, busstate))
2249 bb_perror_msg(" HDIO_SET_BUSSTATE failed");
2252 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2255 if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
2256 bb_perror_msg(" HDIO_GET_BUSSTATE failed");
2258 printf(" busstate = %2ld", parm);
2259 bus_state_value(parm);
2264 if (ioctl(fd, BLKRRPART, NULL)) {
2265 bb_perror_msg(" BLKRRPART failed");
2274 flush_buffer_cache (fd);
2278 #define GET_NUMBER(flag,num) num = 0; \
2279 if (!*p && argc && isdigit(**argv)) \
2280 p = *argv++, --argc; \
2281 while (isdigit(*p)) { \
2283 num = (num * 10) + (*p++ - '0'); \
2286 #define GET_STRING(flag, num) tmpstr = name; \
2288 if (!*p && argc && isalnum(**argv)) \
2289 p = *argv++, --argc; \
2290 while (isalnum(*p) && (tmpstr - name) < 31) { \
2295 num = translate_xfermode(name); \
2301 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2302 static int fromhex (unsigned char c)
2304 if (c >= 'a' && c <= 'f')
2305 return 10 + (c - 'a');
2306 if (c >= '0' && c <= '9')
2308 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2311 static int identify_from_stdin (void)
2313 unsigned short sbuf[800];
2314 unsigned char buf[1600], *b = (unsigned char *)buf;
2315 int i, count = read(0, buf, 1280);
2318 bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
2319 for (i = 0; count >= 4; ++i) {
2320 sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
2321 __le16_to_cpus((uint16_t *)(&sbuf[i]));
2325 identify(sbuf, NULL);
2330 /* our main() routine: */
2331 int hdparm_main(int argc, char **argv)
2342 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2343 if (!strcmp("-Istdin", *argv)) {
2344 exit(identify_from_stdin());
2351 while ((c = *p++)) {
2355 bb_error_msg_and_die("%s", VERSION);
2359 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2380 if (!*p && argc && isdigit(**argv))
2381 p = *argv++, --argc;
2382 if (*p == '0' || *p == '1') {
2384 unmask = *p++ - '0';
2387 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2391 if (!*p && argc && isdigit(**argv))
2392 p = *argv++, --argc;
2393 if (*p >= '0' && *p <= '9') {
2398 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
2402 if (!*p && argc && isdigit(**argv))
2403 p = *argv++, --argc;
2404 if (*p == '0' || *p == '1') {
2406 nowerr = *p++ - '0';
2410 noisy_piomode = noisy;
2412 GET_STRING(set_piomode,piomode);
2415 get_readonly = noisy;
2417 if (!*p && argc && isdigit(**argv))
2418 p = *argv++, --argc;
2419 if (*p == '0' || *p == '1') {
2421 readonly = *p++ - '0';
2427 GET_NUMBER(set_mult,mult);
2430 get_io32bit = noisy;
2432 GET_NUMBER(set_io32bit,io32bit);
2434 #ifdef HDIO_DRIVE_CMD
2436 get_standby = noisy;
2438 GET_NUMBER(set_standby,standby_requested);
2440 bb_error_msg("-S: missing value");
2444 get_defects = noisy;
2446 GET_NUMBER(set_defects,defects);
2448 bb_error_msg("-D: missing value");
2451 get_prefetch = noisy;
2453 GET_NUMBER(set_prefetch,prefetch);
2455 bb_error_msg("-P: missing value");
2459 get_xfermode = noisy;
2461 GET_STRING(set_xfermode,xfermode_requested);
2463 bb_error_msg("-X: missing value");
2469 if (!*p && argc && isdigit(**argv))
2470 p = *argv++, --argc;
2471 if (*p == '0' || *p == '1') {
2475 bb_error_msg("-K: missing value (0/1)");
2479 get_lookahead = noisy;
2481 if (!*p && argc && isdigit(**argv))
2482 p = *argv++, --argc;
2483 if (*p == '0' || *p == '1') {
2485 lookahead = *p++ - '0';
2487 bb_error_msg("-A: missing value (0/1)");
2491 get_doorlock = noisy;
2493 if (!*p && argc && isdigit(**argv))
2494 p = *argv++, --argc;
2495 if (*p == '0' || *p == '1') {
2497 doorlock = *p++ - '0';
2499 bb_error_msg("-L: missing value (0/1)");
2505 if (!*p && argc && isdigit(**argv))
2506 p = *argv++, --argc;
2507 if (*p == '0' || *p == '1') {
2509 wcache = *p++ - '0';
2511 bb_error_msg("-W: missing value (0/1)");
2515 get_powermode = noisy;
2520 get_standbynow = noisy;
2526 get_sleepnow = noisy;
2536 get_seagate = noisy;
2540 #endif /* HDIO_DRIVE_CMD */
2544 if (!*p && argc && isdigit(**argv))
2545 p = *argv++, --argc;
2546 if (*p == '0' || *p == '1') {
2551 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
2553 if (!*p && argc && isdigit(**argv))
2554 p = *argv++, --argc;
2557 bb_error_msg_and_die("expected hwif_nr");
2560 sscanf(p++, "%i", &hwif);
2562 unregister_hwif = 1;
2564 #endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
2565 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2567 if (!*p && argc && isdigit(**argv))
2568 p = *argv++, --argc;
2571 bb_error_msg_and_die("expected hwif_data");
2574 sscanf(p++, "%i", &hwif_data);
2576 if (argc && isdigit(**argv))
2577 p = *argv++, --argc;
2579 bb_error_msg_and_die("expected hwif_ctrl");
2582 sscanf(p, "%i", &hwif_ctrl);
2584 if (argc && isdigit(**argv))
2585 p = *argv++, --argc;
2587 bb_error_msg_and_die("expected hwif_irq");
2590 sscanf(p, "%i", &hwif_irq);
2596 #endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2601 GET_NUMBER(set_dma_q, dma_q);
2605 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2609 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2610 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2612 if (!*p && argc && isdigit(**argv))
2613 p = *argv++, --argc;
2614 if (*p == '0' || *p == '1') {
2615 perform_tristate = 1;
2616 tristate = *p++ - '0';
2618 bb_error_msg("-x: missing value (0/1)");
2621 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2623 get_readahead = noisy;
2625 GET_NUMBER(set_readahead,readahead);
2628 get_apmmode = noisy;
2630 GET_NUMBER(set_apmmode,apmmode);
2632 printf("-B: missing value (1-255)");
2642 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2644 get_busstate = noisy;
2646 if (!*p && argc && isdigit(**argv))
2647 p = *argv++, --argc;
2653 busstate = *p++ - '0';