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 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1043 static unsigned long set_dma = 0, get_dma = 0, dma = 0;
1045 static unsigned long set_dma_q = 0, get_dma_q = 0, dma_q = 0;
1046 static unsigned long set_nowerr = 0, get_nowerr = 0, nowerr = 0;
1047 static unsigned long set_keep = 0, get_keep = 0, keep = 0;
1048 static unsigned long set_io32bit = 0, get_io32bit = 0, io32bit = 0;
1049 static unsigned long set_piomode = 0, noisy_piomode= 0;
1050 static int piomode = 0;
1051 #ifdef HDIO_DRIVE_CMD
1052 static unsigned long set_dkeep = 0, get_dkeep = 0, dkeep = 0;
1053 static unsigned long set_standby = 0, get_standby = 0, standby_requested= 0;
1054 static unsigned long set_xfermode = 0, get_xfermode = 0;
1055 static int xfermode_requested= 0;
1056 static unsigned long set_lookahead= 0, get_lookahead= 0, lookahead= 0;
1057 static unsigned long set_prefetch = 0, get_prefetch = 0, prefetch = 0;
1058 static unsigned long set_defects = 0, get_defects = 0, defects = 0;
1059 static unsigned long set_wcache = 0, get_wcache = 0, wcache = 0;
1060 static unsigned long set_doorlock = 0, get_doorlock = 0, doorlock = 0;
1061 static unsigned long set_seagate = 0, get_seagate = 0;
1062 static unsigned long set_standbynow = 0, get_standbynow = 0;
1063 static unsigned long set_sleepnow = 0, get_sleepnow = 0;
1064 static unsigned long get_powermode = 0;
1065 static unsigned long set_apmmode = 0, get_apmmode= 0, apmmode = 0;
1067 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1068 static int get_IDentity = 0;
1070 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1071 static int unregister_hwif = 0;
1072 static int hwif = 0;
1074 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1075 static int scan_hwif = 0;
1076 static int hwif_data = 0;
1077 static int hwif_ctrl = 0;
1078 static int hwif_irq = 0;
1080 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1081 static int set_busstate = 0, get_busstate = 0, busstate = 0;
1083 static int reread_partn = 0;
1085 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1086 static int perform_reset = 0;
1087 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1088 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1089 static int perform_tristate = 0, tristate = 0;
1090 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1092 // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1093 // then the HDIO_GET_IDENTITY only returned 142 bytes.
1094 // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1095 // and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1096 // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1097 // (which they should, but they should just return -EINVAL).
1099 // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1100 // On a really old system, it will not, and we will be confused.
1103 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1104 static const char *cfg_str[] =
1105 { "", " HardSect", " SoftSect", " NotMFM",
1106 " HdSw>15uSec", " SpinMotCtl", " Fixed", " Removeable",
1107 " DTR<=5Mbs", " DTR>5Mbs", " DTR>10Mbs", " RotSpdTol>.5%",
1108 " dStbOff", " TrkOff", " FmtGapReq", " nonMagnetic"
1111 static const char *BuffType[] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
1113 #define YN(b) (((b)==0)?"no":"yes")
1115 static void dmpstr (const char *prefix, unsigned int i, const char *s[], unsigned int maxi)
1118 printf("%s%u", prefix, i);
1120 printf("%s%s", prefix, s[i]);
1123 static void dump_identity (const struct hd_driveid *id)
1126 char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
1127 const unsigned short int *id_regs= (const void*) id;
1128 unsigned long capacity;
1130 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s",
1131 id->model, id->fw_rev, id->serial_no);
1132 printf("\n Config={");
1133 for (i=0; i<=15; i++) {
1134 if (id->config & (1<<i))
1135 printf("%s", cfg_str[i]);
1138 printf(" RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
1139 id->cyls, id->heads, id->sectors,
1140 id->track_bytes, id->sector_bytes, id->ecc_bytes);
1141 dmpstr(" BuffType=",id->buf_type,BuffType,3);
1142 printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
1143 if (id->max_multsect) {
1144 printf(", MultSect=");
1145 if (!(id->multsect_valid&1))
1146 printf("?%u?", id->multsect);
1147 else if (id->multsect)
1148 printf("%u", id->multsect);
1153 if (id->tPIO <= 5) {
1154 strcat(pmodes, "pio0 ");
1155 if (id->tPIO >= 1) strcat(pmodes, "pio1 ");
1156 if (id->tPIO >= 2) strcat(pmodes, "pio2 ");
1158 if (!(id->field_valid&1))
1159 printf(" (maybe):");
1160 #if __BYTE_ORDER == __BIG_ENDIAN
1161 capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
1163 capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
1165 printf(" CurCHS=%u/%u/%u, CurSects=%lu", id->cur_cyls, id->cur_heads, id->cur_sectors, capacity);
1166 printf(", LBA=%s", YN(id->capability&2));
1167 if (id->capability&2)
1168 printf(", LBAsects=%u", id->lba_capacity);
1170 if (id->capability&1) {
1171 if (id->dma_1word | id->dma_mword) {
1172 if (id->dma_1word & 0x100) strcat(dmodes,"*");
1173 if (id->dma_1word & 1) strcat(dmodes,"sdma0 ");
1174 if (id->dma_1word & 0x200) strcat(dmodes,"*");
1175 if (id->dma_1word & 2) strcat(dmodes,"sdma1 ");
1176 if (id->dma_1word & 0x400) strcat(dmodes,"*");
1177 if (id->dma_1word & 4) strcat(dmodes,"sdma2 ");
1178 if (id->dma_1word & 0xf800) strcat(dmodes,"*");
1179 if (id->dma_1word & 0xf8) strcat(dmodes,"sdma? ");
1180 if (id->dma_mword & 0x100) strcat(dmodes,"*");
1181 if (id->dma_mword & 1) strcat(dmodes,"mdma0 ");
1182 if (id->dma_mword & 0x200) strcat(dmodes,"*");
1183 if (id->dma_mword & 2) strcat(dmodes,"mdma1 ");
1184 if (id->dma_mword & 0x400) strcat(dmodes,"*");
1185 if (id->dma_mword & 4) strcat(dmodes,"mdma2 ");
1186 if (id->dma_mword & 0xf800) strcat(dmodes,"*");
1187 if (id->dma_mword & 0xf8) strcat(dmodes,"mdma? ");
1190 printf("\n IORDY=");
1191 if (id->capability&8)
1192 printf((id->capability&4) ? "on/off" : "yes");
1195 if ((id->capability&8) || (id->field_valid&2)) {
1196 if (id->field_valid&2) {
1197 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1198 if (id->eide_pio_modes & 1) strcat(pmodes, "pio3 ");
1199 if (id->eide_pio_modes & 2) strcat(pmodes, "pio4 ");
1200 if (id->eide_pio_modes &~3) strcat(pmodes, "pio? ");
1202 if (id->field_valid&4) {
1203 if (id->dma_ultra & 0x100) strcat(umodes,"*");
1204 if (id->dma_ultra & 0x001) strcat(umodes,"udma0 ");
1205 if (id->dma_ultra & 0x200) strcat(umodes,"*");
1206 if (id->dma_ultra & 0x002) strcat(umodes,"udma1 ");
1207 if (id->dma_ultra & 0x400) strcat(umodes,"*");
1208 if (id->dma_ultra & 0x004) strcat(umodes,"udma2 ");
1209 #ifdef __NEW_HD_DRIVE_ID
1210 if (id->hw_config & 0x2000) {
1211 #else /* !__NEW_HD_DRIVE_ID */
1212 if (id->word93 & 0x2000) {
1213 #endif /* __NEW_HD_DRIVE_ID */
1214 if (id->dma_ultra & 0x0800) strcat(umodes,"*");
1215 if (id->dma_ultra & 0x0008) strcat(umodes,"udma3 ");
1216 if (id->dma_ultra & 0x1000) strcat(umodes,"*");
1217 if (id->dma_ultra & 0x0010) strcat(umodes,"udma4 ");
1218 if (id->dma_ultra & 0x2000) strcat(umodes,"*");
1219 if (id->dma_ultra & 0x0020) strcat(umodes,"udma5 ");
1220 if (id->dma_ultra & 0x4000) strcat(umodes,"*");
1221 if (id->dma_ultra & 0x0040) strcat(umodes,"udma6 ");
1222 if (id->dma_ultra & 0x8000) strcat(umodes,"*");
1223 if (id->dma_ultra & 0x0080) strcat(umodes,"udma7 ");
1227 if ((id->capability&1) && (id->field_valid&2))
1228 printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1229 printf("\n PIO modes: %s", pmodes);
1231 printf("\n DMA modes: %s", dmodes);
1233 printf("\n UDMA modes: %s", umodes);
1235 printf("\n AdvancedPM=%s",YN(id_regs[83]&8));
1236 if (id_regs[83] & 8) {
1237 if (!(id_regs[86]&8))
1238 printf(": disabled (255)");
1239 else if ((id_regs[91]&0xFF00)!=0x4000)
1240 printf(": unknown setting");
1242 printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1244 if (id_regs[82]&0x20)
1245 printf(" WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
1246 #ifdef __NEW_HD_DRIVE_ID
1247 if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31)) {
1248 printf("\n Drive conforms to: ");
1249 if (id->minor_rev_num <= 31)
1250 printf("%s: ", minor_str[id->minor_rev_num]);
1252 printf("unknown: ");
1253 if (id->major_rev_num < 31) {
1254 for (i=0; i <= 15; i++) {
1255 if (id->major_rev_num & (1<<i))
1260 #endif /* __NEW_HD_DRIVE_ID */
1262 printf("\n * signifies the current active mode\n");
1267 static void flush_buffer_cache (int fd)
1269 fsync (fd); /* flush buffers */
1270 if (ioctl(fd, BLKFLSBUF, NULL)) /* do it again, big time */
1271 bb_perror_msg("BLKFLSBUF failed");
1272 #ifdef HDIO_DRIVE_CMD
1273 if (is_scsi_hd || is_xt_hd) {
1276 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
1277 bb_perror_msg("HDIO_DRIVE_CMD(null) (wait for flush complete) failed");
1282 static int seek_to_zero (int fd)
1284 if (lseek(fd, (off_t) 0, SEEK_SET)) {
1285 bb_perror_msg("lseek() failed");
1291 static int read_big_block (int fd, char *buf)
1294 if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES) {
1297 bb_perror_msg("read() failed");
1299 bb_error_msg("read(%u) returned %u bytes", TIMING_BUF_BYTES, rc);
1301 fputs ("read() hit EOF - device too small\n", stderr);
1305 /* access all sectors of buf to ensure the read fully completed */
1306 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1311 static double correction = 0.0;
1313 void time_cache (int fd)
1317 struct itimerval e1, e2;
1319 int timing_MB = TIMING_MB;
1321 if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1) {
1322 bb_perror_msg ("could not allocate sharedmem buf");
1325 if (shmctl(shmid, SHM_LOCK, NULL) == -1) {
1326 bb_perror_msg ("could not lock sharedmem buf");
1327 (void) shmctl(shmid, IPC_RMID, NULL);
1330 if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1) {
1331 bb_perror_msg ("could not attach sharedmem buf");
1332 (void) shmctl(shmid, IPC_RMID, NULL);
1335 if (shmctl(shmid, IPC_RMID, NULL) == -1)
1336 bb_perror_msg ("shmctl(,IPC_RMID,) failed");
1338 /* Clear out the device request queues & give them time to complete */
1342 /* Calculate a correction factor for the basic
1343 * overhead of doing a read() from the buffer cache.
1344 * To do this, we read the data once to "cache it" and
1345 * to force full preallocation of our timing buffer,
1346 * and then we re-read it 10 times while timing it.
1348 * getitimer() is used rather than gettimeofday() because
1349 * it is much more consistent (on my machine, at least).
1351 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1352 if (seek_to_zero (fd)) return;
1353 if (read_big_block (fd, buf)) return;
1354 printf(" Timing buffer-cache reads: ");
1357 /* Clear out the device request queues & give them time to complete */
1361 /* Time re-reading from the buffer-cache */
1362 getitimer(ITIMER_REAL, &e1);
1363 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i) {
1364 if (seek_to_zero (fd)) goto quit;
1365 if (read_big_block (fd, buf)) goto quit;
1367 getitimer(ITIMER_REAL, &e2);
1368 correction = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1369 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1371 /* Now remove the lseek() from the correction factor */
1372 getitimer(ITIMER_REAL, &e1);
1373 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i) {
1374 if (seek_to_zero (fd)) goto quit;
1376 getitimer(ITIMER_REAL, &e2);
1377 correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
1378 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1380 if ((BUFCACHE_FACTOR * timing_MB) >= correction) /* more than 1MB/s */
1381 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1382 (BUFCACHE_FACTOR * timing_MB), correction,
1383 (BUFCACHE_FACTOR * timing_MB) / correction);
1385 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1386 (BUFCACHE_FACTOR * timing_MB), correction,
1387 (BUFCACHE_FACTOR * timing_MB) / correction * 1024);
1388 correction /= BUFCACHE_FACTOR;
1390 flush_buffer_cache(fd);
1393 if (-1 == shmdt(buf))
1394 bb_perror_msg ("could not detach sharedmem buf");
1397 void time_device (int fd)
1402 struct itimerval e1, e2;
1404 int timing_MB = TIMING_MB;
1406 if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1) {
1407 bb_perror_msg ("could not allocate sharedmem buf");
1410 if (shmctl(shmid, SHM_LOCK, NULL) == -1) {
1411 bb_perror_msg ("could not lock sharedmem buf");
1412 (void) shmctl(shmid, IPC_RMID, NULL);
1415 if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1) {
1416 bb_perror_msg ("could not attach sharedmem buf");
1417 (void) shmctl(shmid, IPC_RMID, NULL);
1420 if (shmctl(shmid, IPC_RMID, NULL) == -1)
1421 bb_perror_msg ("shmctl(,IPC_RMID,) failed");
1423 /* Clear out the device request queues & give them time to complete */
1427 printf(" Timing buffered disk reads: ");
1431 * getitimer() is used rather than gettimeofday() because
1432 * it is much more consistent (on my machine, at least).
1434 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1436 /* Now do the timings for real */
1437 getitimer(ITIMER_REAL, &e1);
1438 for (i = TIMING_BUF_COUNT; i > 0; --i) {
1439 if (read_big_block (fd, buf)) goto quit;
1441 getitimer(ITIMER_REAL, &e2);
1443 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1444 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1445 if (timing_MB >= elapsed) /* more than 1MB/s */
1446 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1447 timing_MB, elapsed, timing_MB / elapsed);
1449 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1450 timing_MB, elapsed, timing_MB / elapsed * 1024);
1452 if (elapsed <= (correction * 2))
1453 printf("Hmm.. suspicious results: probably not enough free memory for a proper test.\n");
1454 #if 0 /* the "estimate" is just plain wrong for many systems.. */
1455 else if (correction != 0.0) {
1456 printf(" Estimating raw driver speed: ");
1457 elapsed -= correction;
1458 if (timing_MB >= elapsed) /* more than 1MB/s */
1459 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1460 timing_MB, elapsed, timing_MB / elapsed);
1462 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1463 timing_MB, elapsed, timing_MB / elapsed * 1024);
1467 if (-1 == shmdt(buf))
1468 bb_perror_msg ("could not detach sharedmem buf");
1471 static void no_scsi (void)
1474 bb_error_msg_and_die(" operation not supported on SCSI disks\n");
1478 static void no_xt (void)
1481 bb_error_msg_and_die (" operation not supported on XT disks\n");
1485 static void on_off (unsigned int value)
1487 printf(value ? " (on)\n" : " (off)\n");
1490 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1491 static void bus_state_value (unsigned int value)
1500 string = " (off)\n";
1502 case BUSSTATE_TRISTATE:
1503 string = " (tristate)\n";
1506 string = " (unknown: %d)\n";
1509 printf(string, value);
1513 #ifdef HDIO_DRIVE_CMD
1514 static void interpret_standby (unsigned int standby)
1518 case 0: printf("off");
1520 case 252: printf("21 minutes");
1522 case 253: printf("vendor-specific");
1524 case 254: printf("?reserved");
1526 case 255: printf("21 minutes + 15 seconds");
1529 if (standby <= 240) {
1530 unsigned int secs = standby * 5;
1531 unsigned int mins = secs / 60;
1533 if (mins) printf("%u minutes", mins);
1534 if (mins && secs) printf(" + ");
1535 if (secs) printf("%u seconds", secs);
1536 } else if (standby <= 251) {
1537 unsigned int mins = (standby - 240) * 30;
1538 unsigned int hrs = mins / 60;
1540 if (hrs) printf("%u hours", hrs);
1541 if (hrs && mins) printf(" + ");
1542 if (mins) printf("%u minutes", mins);
1544 printf("illegal value");
1550 struct xfermode_entry {
1555 static const struct xfermode_entry xfermode_table[] = {
1591 static int translate_xfermode(char * name)
1593 const struct xfermode_entry *tmp;
1598 for (tmp = xfermode_table; tmp->name != NULL; ++tmp) {
1599 if (!strcmp(name, tmp->name))
1603 val = strtol(name, &endptr, 10);
1604 if (*endptr == '\0')
1610 static void interpret_xfermode (unsigned int xfermode)
1614 case 0: printf("default PIO mode");
1616 case 1: printf("default PIO mode, disable IORDY");
1625 case 15: printf("PIO flow control mode%u", xfermode-8);
1634 case 23: printf("singleword DMA mode%u", xfermode-16);
1643 case 39: printf("multiword DMA mode%u", xfermode-32);
1652 case 71: printf("UltraDMA mode%u", xfermode-64);
1655 printf("unknown, probably not valid");
1660 #endif /* HDIO_DRIVE_CMD */
1663 #define VXVM_MAJOR 199
1667 #define CCISS_MAJOR 104
1670 static void process_dev (char *devname)
1673 static long parm, multcount;
1674 struct stat stat_buf;
1675 #ifndef HDIO_DRIVE_CMD
1676 int force_operation = 0;
1678 if (stat(devname,&stat_buf))
1679 bb_perror_msg_and_die(devname);
1681 switch(major(stat_buf.st_rdev)) {
1682 #ifdef SCSI_DISK0_MAJOR
1683 case (SCSI_DISK0_MAJOR):
1684 case (SCSI_DISK1_MAJOR):
1685 case (SCSI_DISK2_MAJOR):
1686 case (SCSI_DISK3_MAJOR):
1687 case (SCSI_DISK4_MAJOR):
1688 case (SCSI_DISK5_MAJOR):
1689 case (SCSI_DISK6_MAJOR):
1690 case (SCSI_DISK7_MAJOR):
1692 case (SCSI_DISK_MAJOR):
1698 #ifdef LVM_BLK_MAJOR
1699 case (LVM_BLK_MAJOR):
1704 #ifdef XT_DISK_MAJOR
1705 case (XT_DISK_MAJOR):
1735 break; /* do nothing */
1737 bb_error_msg_and_die("%s not supported by hdparm",devname);
1740 fd = open (devname, O_RDONLY|O_NONBLOCK);
1742 bb_perror_msg_and_die(devname);
1744 printf("\n%s:\n", devname);
1746 if (set_readahead) {
1748 printf(" setting fs readahead to %ld\n", readahead);
1749 if (ioctl(fd, BLKRASET, readahead))
1750 bb_perror_msg(" BLKRASET failed");
1752 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1753 if (unregister_hwif) {
1755 printf(" attempting to unregister hwif#%u\n", hwif);
1756 if (ioctl(fd, HDIO_UNREGISTER_HWIF, hwif))
1757 bb_perror_msg(" HDIO_UNREGISTER_HWIF failed");
1760 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1764 printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
1765 args[0] = hwif_data;
1766 args[1] = hwif_ctrl;
1768 if (ioctl(fd, HDIO_SCAN_HWIF, args))
1769 bb_perror_msg(" HDIO_SCAN_HWIF failed");
1775 if (noisy_piomode) {
1777 printf(" attempting to auto-tune PIO mode\n");
1778 else if (piomode < 100)
1779 printf(" attempting to set PIO mode to %d\n", piomode);
1780 else if (piomode < 200)
1781 printf(" attempting to set MDMA mode to %d\n", (piomode-100));
1783 printf(" attempting to set UDMA mode to %d\n", (piomode-200));
1785 if (ioctl(fd, HDIO_SET_PIO_MODE, piomode))
1786 bb_perror_msg(" HDIO_SET_PIO_MODE failed");
1792 printf(" setting 32-bit IO_support flag to %ld\n", io32bit);
1793 if (ioctl(fd, HDIO_SET_32BIT, io32bit))
1794 bb_perror_msg(" HDIO_SET_32BIT failed");
1800 printf(" setting multcount to %ld\n", mult);
1801 if (ioctl(fd, HDIO_SET_MULTCOUNT, mult))
1802 bb_perror_msg(" HDIO_SET_MULTCOUNT failed");
1803 #ifndef HDIO_DRIVE_CMD
1804 else force_operation = 1;
1809 printf(" setting readonly to %ld", readonly);
1812 if (ioctl(fd, BLKROSET, &readonly))
1813 bb_perror_msg(" BLKROSET failed");
1819 printf(" setting unmaskirq to %ld", unmask);
1822 if (ioctl(fd, HDIO_SET_UNMASKINTR, unmask))
1823 bb_perror_msg(" HDIO_SET_UNMASKINTR failed");
1825 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1829 printf(" setting using_dma to %ld", dma);
1832 if (ioctl(fd, HDIO_SET_DMA, dma))
1833 bb_perror_msg(" HDIO_SET_DMA failed");
1835 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
1839 printf(" setting DMA queue_depth to %ld", dma_q);
1842 if (ioctl(fd, HDIO_SET_QDMA, dma_q))
1843 bb_perror_msg(" HDIO_SET_QDMA failed");
1849 printf(" setting nowerr to %ld", nowerr);
1852 if (ioctl(fd, HDIO_SET_NOWERR, nowerr))
1853 bb_perror_msg(" HDIO_SET_NOWERR failed");
1859 printf(" setting keep_settings to %ld", keep);
1862 if (ioctl(fd, HDIO_SET_KEEPSETTINGS, keep))
1863 bb_perror_msg(" HDIO_SET_KEEPSETTINGS failed");
1865 #ifdef HDIO_DRIVE_CMD
1867 unsigned char args[4] = {0,0,0,0};
1870 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
1872 printf(" setting drive doorlock to %ld", doorlock);
1875 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1876 bb_perror_msg(" HDIO_DRIVE_CMD(doorlock) failed");
1879 /* lock/unlock the drive's "feature" settings */
1880 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1884 printf(" setting drive keep features to %ld", dkeep);
1887 args[2] = dkeep ? 0x66 : 0xcc;
1888 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1889 bb_perror_msg(" HDIO_DRIVE_CMD(keepsettings) failed");
1892 unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
1894 args[2] = defects ? 0x04 : 0x84;
1896 printf(" setting drive defect-mgmt to %ld\n", defects);
1897 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1898 bb_perror_msg(" HDIO_DRIVE_CMD(defectmgmt) failed");
1901 unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
1906 printf(" setting drive prefetch to %ld\n", prefetch);
1907 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1908 bb_perror_msg(" HDIO_DRIVE_CMD(setprefetch) failed");
1911 unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
1914 args[1] = xfermode_requested;
1916 printf(" setting xfermode to %d", xfermode_requested);
1917 interpret_xfermode(xfermode_requested);
1919 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1920 bb_perror_msg(" HDIO_DRIVE_CMD(setxfermode) failed");
1922 if (set_lookahead) {
1923 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1926 args[2] = lookahead ? 0xaa : 0x55;
1927 if (get_lookahead) {
1928 printf(" setting drive read-lookahead to %ld", lookahead);
1931 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1932 bb_perror_msg(" HDIO_DRIVE_CMD(setreadahead) failed");
1935 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1937 if (apmmode<1) apmmode=1;
1938 if (apmmode>255) apmmode=255;
1940 printf(" setting Advanced Power Management level to");
1942 /* disable Advanced Power Management */
1943 args[2] = 0x85; /* feature register */
1944 if (get_apmmode) printf(" disabled\n");
1946 /* set Advanced Power Management mode */
1947 args[2] = 0x05; /* feature register */
1948 args[1] = apmmode; /* sector count register */
1949 if (get_apmmode) printf(" 0x%02lX (%ld)\n",apmmode,apmmode);
1951 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1952 bb_perror_msg(" HDIO_DRIVE_CMD failed");
1955 #ifdef DO_FLUSHCACHE
1956 #ifndef WIN_FLUSHCACHE
1957 #define WIN_FLUSHCACHE 0xe7
1959 unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
1960 #endif /* DO_FLUSHCACHE */
1961 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1964 args[2] = wcache ? 0x02 : 0x82;
1966 printf(" setting drive write-caching to %ld", wcache);
1969 #ifdef DO_FLUSHCACHE
1970 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
1971 bb_perror_msg (" HDIO_DRIVE_CMD(flushcache) failed");
1972 #endif /* DO_FLUSHCACHE */
1973 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
1974 bb_perror_msg(" HDIO_DRIVE_CMD(setcache) failed");
1975 #ifdef DO_FLUSHCACHE
1976 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
1977 bb_perror_msg (" HDIO_DRIVE_CMD(flushcache) failed");
1978 #endif /* DO_FLUSHCACHE */
1980 if (set_standbynow) {
1981 #ifndef WIN_STANDBYNOW1
1982 #define WIN_STANDBYNOW1 0xE0
1984 #ifndef WIN_STANDBYNOW2
1985 #define WIN_STANDBYNOW2 0x94
1987 unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
1988 unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
1991 printf(" issuing standby command\n");
1992 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
1993 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
1994 bb_perror_msg(" HDIO_DRIVE_CMD(standby) failed");
1997 #ifndef WIN_SLEEPNOW1
1998 #define WIN_SLEEPNOW1 0xE6
2000 #ifndef WIN_SLEEPNOW2
2001 #define WIN_SLEEPNOW2 0x99
2003 unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
2004 unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
2007 printf(" issuing sleep command\n");
2008 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2009 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
2010 bb_perror_msg(" HDIO_DRIVE_CMD(sleep) failed");
2013 unsigned char args[4] = {0xfb,0,0,0};
2017 printf(" disabling Seagate auto powersaving mode\n");
2018 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2019 bb_perror_msg(" HDIO_DRIVE_CMD(seagatepwrsave) failed");
2022 unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
2026 printf(" setting standby to %lu", standby_requested);
2027 interpret_standby(standby_requested);
2029 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2030 bb_perror_msg(" HDIO_DRIVE_CMD(setidle1) failed");
2032 #else /* HDIO_DRIVE_CMD */
2033 if (force_operation) {
2035 flush_buffer_cache(fd);
2036 if (-1 == read(fd, buf, sizeof(buf)))
2037 bb_perror_msg(" access failed");
2039 #endif /* HDIO_DRIVE_CMD */
2044 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity) {
2047 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount)) {
2048 if ((verbose && !is_xt_hd) || get_mult)
2049 bb_perror_msg(" HDIO_GET_MULTCOUNT failed");
2050 } else if (verbose | get_mult) {
2051 printf(" multcount = %2ld", multcount);
2055 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit) {
2058 if (ioctl(fd, HDIO_GET_32BIT, &parm))
2059 bb_perror_msg(" HDIO_GET_32BIT failed");
2061 printf(" IO_support =%3ld (", parm);
2063 case 0: printf("default ");
2064 case 2: printf("16-bit)\n");
2066 case 1: printf("32-bit)\n");
2068 case 3: printf("32-bit w/sync)\n");
2070 case 8: printf("Request-Queue-Bypass)\n");
2072 default:printf("\?\?\?)\n");
2076 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask) {
2079 if (ioctl(fd, HDIO_GET_UNMASKINTR, &parm))
2080 bb_perror_msg(" HDIO_GET_UNMASKINTR failed");
2082 printf(" unmaskirq = %2ld", parm);
2087 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2088 if ((verbose && !is_scsi_hd) || get_dma) {
2090 if (ioctl(fd, HDIO_GET_DMA, &parm))
2091 bb_perror_msg(" HDIO_GET_DMA failed");
2093 printf(" using_dma = %2ld", parm);
2095 printf(" (DMA-Assisted-PIO)\n");
2103 if(ioctl(fd, HDIO_GET_QDMA, &parm)) {
2104 bb_perror_msg(" HDIO_GET_QDMA failed");
2106 printf(" queue_depth = %2ld", parm);
2110 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep) {
2113 if (ioctl(fd, HDIO_GET_KEEPSETTINGS, &parm))
2114 bb_perror_msg(" HDIO_GET_KEEPSETTINGS failed");
2116 printf(" keepsettings = %2ld", parm);
2124 if (ioctl(fd, HDIO_GET_NOWERR, &parm))
2125 bb_perror_msg(" HDIO_GET_NOWERR failed");
2127 printf(" nowerr = %2ld", parm);
2131 if (verbose || get_readonly) {
2132 if (ioctl(fd, BLKROGET, &parm))
2133 bb_perror_msg(" BLKROGET failed");
2135 printf(" readonly = %2ld", parm);
2139 if ((verbose && !is_scsi_hd) || get_readahead) {
2140 if (ioctl(fd, BLKRAGET, &parm))
2141 bb_perror_msg(" BLKRAGET failed");
2143 printf(" readahead = %2ld", parm);
2147 if (verbose || get_geom) {
2148 static const char msg[] = " geometry = %u/%u/%u, sectors = %ld, start = %ld\n";
2149 static struct hd_geometry g;
2150 #ifdef HDIO_GETGEO_BIG
2151 static struct hd_big_geometry bg;
2154 if (ioctl(fd, BLKGETSIZE, &parm))
2155 bb_perror_msg(" BLKGETSIZE failed");
2156 #ifdef HDIO_GETGEO_BIG
2157 else if (!ioctl(fd, HDIO_GETGEO_BIG, &bg))
2158 printf(msg, bg.cylinders, bg.heads, bg.sectors, parm, bg.start);
2160 else if (ioctl(fd, HDIO_GETGEO, &g))
2161 bb_perror_msg(" HDIO_GETGEO failed");
2162 else printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
2164 #ifdef HDIO_DRIVE_CMD
2165 if (get_powermode) {
2166 #ifndef WIN_CHECKPOWERMODE1
2167 #define WIN_CHECKPOWERMODE1 0xE5
2169 #ifndef WIN_CHECKPOWERMODE2
2170 #define WIN_CHECKPOWERMODE2 0x98
2172 unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
2175 if (ioctl(fd, HDIO_DRIVE_CMD, &args)
2176 && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
2177 && ioctl(fd, HDIO_DRIVE_CMD, &args)) {
2178 if (errno != EIO || args[0] != 0 || args[1] != 0)
2183 state = (args[2] == 255) ? "active/idle" : "standby";
2185 printf(" drive state is: %s\n", state);
2188 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2189 if (perform_reset) {
2192 if (ioctl(fd, HDIO_DRIVE_RESET, NULL))
2193 bb_perror_msg(" HDIO_DRIVE_RESET failed");
2195 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2196 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2197 if (perform_tristate) {
2198 unsigned char args[4] = {0,tristate,0,0};
2201 if (ioctl(fd, HDIO_TRISTATE_HWIF, &args))
2202 bb_perror_msg(" HDIO_TRISTATE_HWIF failed");
2204 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2205 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2207 static struct hd_driveid id;
2212 if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
2213 if (multcount != -1) {
2214 id.multsect = multcount;
2215 id.multsect_valid |= 1;
2217 id.multsect_valid &= ~1;
2219 } else if (errno == -ENOMSG)
2220 printf(" no identification info available\n");
2222 bb_perror_msg(" HDIO_GET_IDENTITY failed");
2225 unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
2229 if (ioctl(fd, HDIO_DRIVE_CMD, &args)) {
2230 args[0] = WIN_PIDENTIFY;
2231 if (ioctl(fd, HDIO_DRIVE_CMD, &args)) {
2232 bb_perror_msg(" HDIO_DRIVE_CMD(identify) failed");
2233 goto identify_abort;
2236 for(i=0; i<(sizeof args)/2; i+=2) {
2237 __le16_to_cpus((uint16_t *)(&args[i]));
2239 identify((void *)&args[4], NULL);
2243 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2247 printf(" setting bus state to %d", busstate);
2248 bus_state_value(busstate);
2250 if (ioctl(fd, HDIO_SET_BUSSTATE, busstate))
2251 bb_perror_msg(" HDIO_SET_BUSSTATE failed");
2254 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2257 if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
2258 bb_perror_msg(" HDIO_GET_BUSSTATE failed");
2260 printf(" busstate = %2ld", parm);
2261 bus_state_value(parm);
2266 if (ioctl(fd, BLKRRPART, NULL)) {
2267 bb_perror_msg(" BLKRRPART failed");
2276 flush_buffer_cache (fd);
2280 #define GET_NUMBER(flag,num) num = 0; \
2281 if (!*p && argc && isdigit(**argv)) \
2282 p = *argv++, --argc; \
2283 while (isdigit(*p)) { \
2285 num = (num * 10) + (*p++ - '0'); \
2288 #define GET_STRING(flag, num) tmpstr = name; \
2290 if (!*p && argc && isalnum(**argv)) \
2291 p = *argv++, --argc; \
2292 while (isalnum(*p) && (tmpstr - name) < 31) { \
2297 num = translate_xfermode(name); \
2303 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2304 static int fromhex (unsigned char c)
2306 if (c >= 'a' && c <= 'f')
2307 return 10 + (c - 'a');
2308 if (c >= '0' && c <= '9')
2310 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2313 static int identify_from_stdin (void)
2315 unsigned short sbuf[800];
2316 unsigned char buf[1600], *b = (unsigned char *)buf;
2317 int i, count = read(0, buf, 1280);
2320 bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
2321 for (i = 0; count >= 4; ++i) {
2322 sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
2323 __le16_to_cpus((uint16_t *)(&sbuf[i]));
2327 identify(sbuf, NULL);
2332 /* our main() routine: */
2333 int hdparm_main(int argc, char **argv)
2344 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2345 if (!strcmp("-Istdin", *argv)) {
2346 exit(identify_from_stdin());
2353 while ((c = *p++)) {
2357 bb_error_msg_and_die("%s", VERSION);
2361 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2382 if (!*p && argc && isdigit(**argv))
2383 p = *argv++, --argc;
2384 if (*p == '0' || *p == '1') {
2386 unmask = *p++ - '0';
2389 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2393 if (!*p && argc && isdigit(**argv))
2394 p = *argv++, --argc;
2395 if (*p >= '0' && *p <= '9') {
2400 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
2404 if (!*p && argc && isdigit(**argv))
2405 p = *argv++, --argc;
2406 if (*p == '0' || *p == '1') {
2408 nowerr = *p++ - '0';
2412 noisy_piomode = noisy;
2414 GET_STRING(set_piomode,piomode);
2417 get_readonly = noisy;
2419 if (!*p && argc && isdigit(**argv))
2420 p = *argv++, --argc;
2421 if (*p == '0' || *p == '1') {
2423 readonly = *p++ - '0';
2429 GET_NUMBER(set_mult,mult);
2432 get_io32bit = noisy;
2434 GET_NUMBER(set_io32bit,io32bit);
2436 #ifdef HDIO_DRIVE_CMD
2438 get_standby = noisy;
2440 GET_NUMBER(set_standby,standby_requested);
2442 bb_error_msg("-S: missing value");
2446 get_defects = noisy;
2448 GET_NUMBER(set_defects,defects);
2450 bb_error_msg("-D: missing value");
2453 get_prefetch = noisy;
2455 GET_NUMBER(set_prefetch,prefetch);
2457 bb_error_msg("-P: missing value");
2461 get_xfermode = noisy;
2463 GET_STRING(set_xfermode,xfermode_requested);
2465 bb_error_msg("-X: missing value");
2471 if (!*p && argc && isdigit(**argv))
2472 p = *argv++, --argc;
2473 if (*p == '0' || *p == '1') {
2477 bb_error_msg("-K: missing value (0/1)");
2481 get_lookahead = noisy;
2483 if (!*p && argc && isdigit(**argv))
2484 p = *argv++, --argc;
2485 if (*p == '0' || *p == '1') {
2487 lookahead = *p++ - '0';
2489 bb_error_msg("-A: missing value (0/1)");
2493 get_doorlock = noisy;
2495 if (!*p && argc && isdigit(**argv))
2496 p = *argv++, --argc;
2497 if (*p == '0' || *p == '1') {
2499 doorlock = *p++ - '0';
2501 bb_error_msg("-L: missing value (0/1)");
2507 if (!*p && argc && isdigit(**argv))
2508 p = *argv++, --argc;
2509 if (*p == '0' || *p == '1') {
2511 wcache = *p++ - '0';
2513 bb_error_msg("-W: missing value (0/1)");
2517 get_powermode = noisy;
2522 get_standbynow = noisy;
2528 get_sleepnow = noisy;
2538 get_seagate = noisy;
2542 #endif /* HDIO_DRIVE_CMD */
2546 if (!*p && argc && isdigit(**argv))
2547 p = *argv++, --argc;
2548 if (*p == '0' || *p == '1') {
2553 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
2555 if (!*p && argc && isdigit(**argv))
2556 p = *argv++, --argc;
2559 bb_error_msg_and_die("expected hwif_nr");
2562 sscanf(p++, "%i", &hwif);
2564 unregister_hwif = 1;
2566 #endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
2567 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2569 if (!*p && argc && isdigit(**argv))
2570 p = *argv++, --argc;
2573 bb_error_msg_and_die("expected hwif_data");
2576 sscanf(p++, "%i", &hwif_data);
2578 if (argc && isdigit(**argv))
2579 p = *argv++, --argc;
2581 bb_error_msg_and_die("expected hwif_ctrl");
2584 sscanf(p, "%i", &hwif_ctrl);
2586 if (argc && isdigit(**argv))
2587 p = *argv++, --argc;
2589 bb_error_msg_and_die("expected hwif_irq");
2592 sscanf(p, "%i", &hwif_irq);
2598 #endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2603 GET_NUMBER(set_dma_q, dma_q);
2607 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2611 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2612 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2614 if (!*p && argc && isdigit(**argv))
2615 p = *argv++, --argc;
2616 if (*p == '0' || *p == '1') {
2617 perform_tristate = 1;
2618 tristate = *p++ - '0';
2620 bb_error_msg("-x: missing value (0/1)");
2623 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2625 get_readahead = noisy;
2627 GET_NUMBER(set_readahead,readahead);
2630 get_apmmode = noisy;
2632 GET_NUMBER(set_apmmode,apmmode);
2634 printf("-B: missing value (1-255)");
2644 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2646 get_busstate = noisy;
2648 if (!*p && argc && isdigit(**argv))
2649 p = *argv++, --argc;
2655 busstate = *p++ - '0';