1 /* vi: set sw=4 ts=4: */
3 * hdparm implementation for busybox
5 * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
6 * Hacked by Tito <farmatito@tiscali.it> for size optimization.
8 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
10 * This program is based on the source code of hdparm: see below...
11 * hdparm.c - Command line interface to get/set hard disk parameters
12 * - by Mark Lord (C) 1994-2002 -- freely distributable
21 #include <sys/ioctl.h>
22 #include <sys/sysmacros.h>
23 #include <sys/times.h>
24 #include <sys/mount.h>
27 #include <linux/types.h>
28 #include <linux/hdreg.h>
29 #include <asm/byteorder.h>
31 #if BB_BIG_ENDIAN && !defined(__USE_XOPEN)
41 #define ATA_DEV 0x0000
42 #define ATAPI_DEV 0x0001
44 /* word definitions */
45 /* ---------------- */
46 #define GEN_CONFIG 0 /* general configuration */
47 #define LCYLS 1 /* number of logical cylinders */
48 #define CONFIG 2 /* specific configuration */
49 #define LHEADS 3 /* number of logical heads */
50 #define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
51 #define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
52 #define LSECTS 6 /* number of logical sectors/track */
53 #define START_SERIAL 10 /* ASCII serial number */
54 #define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
55 #define BUF_TYPE 20 /* buffer type (ATA-1) */
56 #define BUFFER__SIZE 21 /* buffer size (ATA-1) */
57 #define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
58 #define START_FW_REV 23 /* ASCII firmware revision */
59 #define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
60 #define START_MODEL 27 /* ASCII model number */
61 #define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
62 #define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
63 #define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
64 #define CAPAB_0 49 /* capabilities */
66 #define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
67 #define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
68 #define WHATS_VALID 53 /* what fields are valid */
69 #define LCYLS_CUR 54 /* current logical cylinders */
70 #define LHEADS_CUR 55 /* current logical heads */
71 #define LSECTS_CUR 56 /* current logical sectors/track */
72 #define CAPACITY_LSB 57 /* current capacity in sectors */
73 #define CAPACITY_MSB 58
74 #define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
75 #define LBA_SECTS_LSB 60 /* LBA: total number of user */
76 #define LBA_SECTS_MSB 61 /* addressable sectors */
77 #define SINGLE_DMA 62 /* singleword DMA modes */
78 #define MULTI_DMA 63 /* multiword DMA modes */
79 #define ADV_PIO_MODES 64 /* advanced PIO modes supported */
80 /* multiword DMA xfer cycle time: */
81 #define DMA_TIME_MIN 65 /* - minimum */
82 #define DMA_TIME_NORM 66 /* - manufacturer's recommended */
83 /* minimum PIO xfer cycle time: */
84 #define PIO_NO_FLOW 67 /* - without flow control */
85 #define PIO_FLOW 68 /* - with IORDY flow control */
86 #define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
87 #define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
88 #define CDR_MAJOR 73 /* CD ROM: major version number */
89 #define CDR_MINOR 74 /* CD ROM: minor version number */
90 #define QUEUE_DEPTH 75 /* queue depth */
91 #define MAJOR 80 /* major version number */
92 #define MINOR 81 /* minor version number */
93 #define CMDS_SUPP_0 82 /* command/feature set(s) supported */
94 #define CMDS_SUPP_1 83
95 #define CMDS_SUPP_2 84
96 #define CMDS_EN_0 85 /* command/feature set(s) enabled */
99 #define ULTRA_DMA 88 /* ultra DMA modes */
100 /* time to complete security erase */
101 #define ERASE_TIME 89 /* - ordinary */
102 #define ENH_ERASE_TIME 90 /* - enhanced */
103 #define ADV_PWR 91 /* current advanced power management level
104 in low byte, 0x40 in high byte. */
105 #define PSWD_CODE 92 /* master password revision code */
106 #define HWRST_RSLT 93 /* hardware reset result */
107 #define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
108 #define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
109 #define LBA_MID 101 /* bits are used, but addr 103 */
110 #define LBA_48_MSB 102 /* has been reserved for LBA in */
111 #define LBA_64_MSB 103 /* the future. */
112 #define RM_STAT 127 /* removable media status notification feature set support */
113 #define SECU_STATUS 128 /* security status */
114 #define CFA_PWR_MODE 160 /* CFA power mode 1 */
115 #define START_MEDIA 176 /* media serial number */
116 #define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
117 #define START_MANUF 196 /* media manufacturer I.D. */
118 #define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
119 #define INTEGRITY 255 /* integrity word */
121 /* bit definitions within the words */
122 /* -------------------------------- */
124 /* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
126 #define VALID_VAL 0x4000
127 /* many words are considered invalid if they are either all-0 or all-1 */
128 #define NOVAL_0 0x0000
129 #define NOVAL_1 0xffff
131 /* word 0: gen_config */
132 #define NOT_ATA 0x8000
133 #define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
134 #define MEDIA_REMOVABLE 0x0080
135 #define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
136 #define INCOMPLETE 0x0004
137 #define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
138 #define DRQ_RESPONSE_TIME 0x0060
139 #define DRQ_3MS_VAL 0x0000
140 #define DRQ_INTR_VAL 0x0020
141 #define DRQ_50US_VAL 0x0040
142 #define PKT_SIZE_SUPPORTED 0x0003
143 #define PKT_SIZE_12_VAL 0x0000
144 #define PKT_SIZE_16_VAL 0x0001
145 #define EQPT_TYPE 0x1f00
150 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
151 static const char * const pkt_str[] = {
152 "Direct-access device", /* word 0, bits 12-8 = 00 */
153 "Sequential-access device", /* word 0, bits 12-8 = 01 */
154 "Printer", /* word 0, bits 12-8 = 02 */
155 "Processor", /* word 0, bits 12-8 = 03 */
156 "Write-once device", /* word 0, bits 12-8 = 04 */
157 "CD-ROM", /* word 0, bits 12-8 = 05 */
158 "Scanner", /* word 0, bits 12-8 = 06 */
159 "Optical memory", /* word 0, bits 12-8 = 07 */
160 "Medium changer", /* word 0, bits 12-8 = 08 */
161 "Communications device", /* word 0, bits 12-8 = 09 */
162 "ACS-IT8 device", /* word 0, bits 12-8 = 0a */
163 "ACS-IT8 device", /* word 0, bits 12-8 = 0b */
164 "Array controller", /* word 0, bits 12-8 = 0c */
165 "Enclosure services", /* word 0, bits 12-8 = 0d */
166 "Reduced block command device", /* word 0, bits 12-8 = 0e */
167 "Optical card reader/writer", /* word 0, bits 12-8 = 0f */
168 "", /* word 0, bits 12-8 = 10 */
169 "", /* word 0, bits 12-8 = 11 */
170 "", /* word 0, bits 12-8 = 12 */
171 "", /* word 0, bits 12-8 = 13 */
172 "", /* word 0, bits 12-8 = 14 */
173 "", /* word 0, bits 12-8 = 15 */
174 "", /* word 0, bits 12-8 = 16 */
175 "", /* word 0, bits 12-8 = 17 */
176 "", /* word 0, bits 12-8 = 18 */
177 "", /* word 0, bits 12-8 = 19 */
178 "", /* word 0, bits 12-8 = 1a */
179 "", /* word 0, bits 12-8 = 1b */
180 "", /* word 0, bits 12-8 = 1c */
181 "", /* word 0, bits 12-8 = 1d */
182 "", /* word 0, bits 12-8 = 1e */
183 "Unknown", /* word 0, bits 12-8 = 1f */
186 static const char * const ata1_cfg_str[] = { /* word 0 in ATA-1 mode */
187 "Reserved", /* bit 0 */
188 "hard sectored", /* bit 1 */
189 "soft sectored", /* bit 2 */
190 "not MFM encoded ", /* bit 3 */
191 "head switch time > 15us", /* bit 4 */
192 "spindle motor control option", /* bit 5 */
193 "fixed drive", /* bit 6 */
194 "removable drive", /* bit 7 */
195 "disk xfer rate <= 5Mbs", /* bit 8 */
196 "disk xfer rate > 5Mbs, <= 10Mbs", /* bit 9 */
197 "disk xfer rate > 5Mbs", /* bit 10 */
198 "rotational speed tol.", /* bit 11 */
199 "data strobe offset option", /* bit 12 */
200 "track offset option", /* bit 13 */
201 "format speed tolerance gap reqd", /* bit 14 */
206 /* word 1: number of logical cylinders */
207 #define LCYLS_MAX 0x3fff /* maximum allowable value */
209 /* word 2: specific configuration
210 * (a) require SET FEATURES to spin-up
211 * (b) require spin-up to fully reply to IDENTIFY DEVICE
213 #define STBY_NID_VAL 0x37c8 /* (a) and (b) */
214 #define STBY_ID_VAL 0x738c /* (a) and not (b) */
215 #define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
216 #define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
218 /* words 47 & 59: sector_xfer_max & sector_xfer_cur */
219 #define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
220 #define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
222 /* word 49: capabilities 0 */
223 #define STD_STBY 0x2000 /* 1=standard values supported (ATA);
224 0=vendor specific values */
225 #define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
226 #define IORDY_OFF 0x0400 /* 1=may be disabled */
227 #define LBA_SUP 0x0200 /* 1=Logical Block Address support */
228 #define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
229 #define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
230 #define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
231 #define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
232 #define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
234 /* word 50: capabilities 1 */
235 #define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
237 /* words 51 & 52: PIO & DMA cycle times */
238 #define MODE 0xff00 /* the mode is in the MSBs */
240 /* word 53: whats_valid */
241 #define OK_W88 0x0004 /* the ultra_dma info is valid */
242 #define OK_W64_70 0x0002 /* see above for word descriptions */
243 #define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
245 /*word 63,88: dma_mode, ultra_dma_mode*/
246 #define MODE_MAX 7 /* bit definitions force udma <=7 (when
247 * udma >=8 comes out it'll have to be
248 * defined in a new dma_mode word!) */
250 /* word 64: PIO transfer modes */
251 #define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
252 #define PIO_MODE_MAX 8 /* but all 8 bits are defined */
254 /* word 75: queue_depth */
255 #define DEPTH_BITS 0x001f /* bits used for queue depth */
257 /* words 80-81: version numbers */
258 /* NOVAL_0 or NOVAL_1 means device does not report version */
260 /* word 81: minor version number */
261 #define MINOR_MAX 0x22
262 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
263 static const char *minor_str[MINOR_MAX+2] = { /* word 81 value: */
264 "Unspecified", /* 0x0000 */
265 "ATA-1 X3T9.2 781D prior to rev.4", /* 0x0001 */
266 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
267 "ATA-1 X3T9.2 781D rev.4", /* 0x0003 */
268 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
269 "ATA-2 X3T10 948D prior to rev.2k", /* 0x0005 */
270 "ATA-3 X3T10 2008D rev.1", /* 0x0006 */
271 "ATA-2 X3T10 948D rev.2k", /* 0x0007 */
272 "ATA-3 X3T10 2008D rev.0", /* 0x0008 */
273 "ATA-2 X3T10 948D rev.3", /* 0x0009 */
274 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
275 "ATA-3 X3T10 2008D rev.6", /* 0x000b */
276 "ATA-3 X3T13 2008D rev.7 and 7a", /* 0x000c */
277 "ATA/ATAPI-4 X3T13 1153D rev.6", /* 0x000d */
278 "ATA/ATAPI-4 T13 1153D rev.13", /* 0x000e */
279 "ATA/ATAPI-4 X3T13 1153D rev.7", /* 0x000f */
280 "ATA/ATAPI-4 T13 1153D rev.18", /* 0x0010 */
281 "ATA/ATAPI-4 T13 1153D rev.15", /* 0x0011 */
282 "ATA/ATAPI-4 published, ANSI INCITS 317-1998", /* 0x0012 */
283 "ATA/ATAPI-5 T13 1321D rev.3",
284 "ATA/ATAPI-4 T13 1153D rev.14", /* 0x0014 */
285 "ATA/ATAPI-5 T13 1321D rev.1", /* 0x0015 */
286 "ATA/ATAPI-5 published, ANSI INCITS 340-2000", /* 0x0016 */
287 "ATA/ATAPI-4 T13 1153D rev.17", /* 0x0017 */
288 "ATA/ATAPI-6 T13 1410D rev.0", /* 0x0018 */
289 "ATA/ATAPI-6 T13 1410D rev.3a", /* 0x0019 */
290 "ATA/ATAPI-7 T13 1532D rev.1", /* 0x001a */
291 "ATA/ATAPI-6 T13 1410D rev.2", /* 0x001b */
292 "ATA/ATAPI-6 T13 1410D rev.1", /* 0x001c */
293 "ATA/ATAPI-7 published, ANSI INCITS 397-2005", /* 0x001d */
294 "ATA/ATAPI-7 T13 1532D rev.0", /* 0x001e */
295 "Reserved" /* 0x001f */
296 "Reserved" /* 0x0020 */
297 "ATA/ATAPI-7 T13 1532D rev.4a", /* 0x0021 */
298 "ATA/ATAPI-6 published, ANSI INCITS 361-2002", /* 0x0022 */
299 "Reserved" /* 0x0023-0xfffe*/
302 static const char actual_ver[MINOR_MAX+2] = {
304 0, /* 0x0000 WARNING: */
305 1, /* 0x0001 WARNING: */
306 1, /* 0x0002 WARNING: */
307 1, /* 0x0003 WARNING: */
308 2, /* 0x0004 WARNING: This array */
309 2, /* 0x0005 WARNING: corresponds */
310 3, /* 0x0006 WARNING: *exactly* */
311 2, /* 0x0007 WARNING: to the ATA/ */
312 3, /* 0x0008 WARNING: ATAPI version */
313 2, /* 0x0009 WARNING: listed in */
314 3, /* 0x000a WARNING: the */
315 3, /* 0x000b WARNING: minor_str */
316 3, /* 0x000c WARNING: array */
317 4, /* 0x000d WARNING: above. */
318 4, /* 0x000e WARNING: */
319 4, /* 0x000f WARNING: if you change */
320 4, /* 0x0010 WARNING: that one, */
321 4, /* 0x0011 WARNING: change this one */
322 4, /* 0x0012 WARNING: too!!! */
323 5, /* 0x0013 WARNING: */
324 4, /* 0x0014 WARNING: */
325 5, /* 0x0015 WARNING: */
326 5, /* 0x0016 WARNING: */
327 4, /* 0x0017 WARNING: */
328 6, /* 0x0018 WARNING: */
329 6, /* 0x0019 WARNING: */
330 7, /* 0x001a WARNING: */
331 6, /* 0x001b WARNING: */
332 6, /* 0x001c WARNING: */
333 7, /* 0x001d WARNING: */
334 7, /* 0x001e WARNING: */
335 0, /* 0x001f WARNING: */
336 0, /* 0x0020 WARNING: */
337 7, /* 0x0021 WARNING: */
338 6, /* 0x0022 WARNING: */
339 0 /* 0x0023-0xfffe */
342 /* words 82-84: cmds/feats supported */
343 #define CMDS_W82 0x77ff /* word 82: defined command locations*/
344 #define CMDS_W83 0x3fff /* word 83: defined command locations*/
345 #define CMDS_W84 0x002f /* word 83: defined command locations*/
346 #define SUPPORT_48_BIT 0x0400
347 #define NUM_CMD_FEAT_STR 48
349 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
350 static const char * const cmd_feat_str[] = {
351 "", /* word 82 bit 15: obsolete */
352 "NOP cmd", /* word 82 bit 14 */
353 "READ BUFFER cmd", /* word 82 bit 13 */
354 "WRITE BUFFER cmd", /* word 82 bit 12 */
355 "", /* word 82 bit 11: obsolete */
356 "Host Protected Area feature set", /* word 82 bit 10 */
357 "DEVICE RESET cmd", /* word 82 bit 9 */
358 "SERVICE interrupt", /* word 82 bit 8 */
359 "Release interrupt", /* word 82 bit 7 */
360 "Look-ahead", /* word 82 bit 6 */
361 "Write cache", /* word 82 bit 5 */
362 "PACKET command feature set", /* word 82 bit 4 */
363 "Power Management feature set", /* word 82 bit 3 */
364 "Removable Media feature set", /* word 82 bit 2 */
365 "Security Mode feature set", /* word 82 bit 1 */
366 "SMART feature set", /* word 82 bit 0 */
368 "", /* word 83 bit 15: !valid bit */
369 "", /* word 83 bit 14: valid bit */
370 "FLUSH CACHE EXT cmd", /* word 83 bit 13 */
371 "Mandatory FLUSH CACHE cmd ", /* word 83 bit 12 */
372 "Device Configuration Overlay feature set ",
373 "48-bit Address feature set ", /* word 83 bit 10 */
375 "SET MAX security extension", /* word 83 bit 8 */
376 "Address Offset Reserved Area Boot", /* word 83 bit 7 */
377 "SET FEATURES subcommand required to spinup after power up",
378 "Power-Up In Standby feature set", /* word 83 bit 5 */
379 "Removable Media Status Notification feature set",
380 "Adv. Power Management feature set",/* word 83 bit 3 */
381 "CFA feature set", /* word 83 bit 2 */
382 "READ/WRITE DMA QUEUED", /* word 83 bit 1 */
383 "DOWNLOAD MICROCODE cmd", /* word 83 bit 0 */
385 "", /* word 84 bit 15: !valid bit */
386 "", /* word 84 bit 14: valid bit */
387 "", /* word 84 bit 13: reserved */
388 "", /* word 84 bit 12: reserved */
389 "", /* word 84 bit 11: reserved */
390 "", /* word 84 bit 10: reserved */
391 "", /* word 84 bit 9: reserved */
392 "", /* word 84 bit 8: reserved */
393 "", /* word 84 bit 7: reserved */
394 "", /* word 84 bit 6: reserved */
395 "General Purpose Logging feature set", /* word 84 bit 5 */
396 "", /* word 84 bit 4: reserved */
397 "Media Card Pass Through Command feature set ",
398 "Media serial number ", /* word 84 bit 2 */
399 "SMART self-test ", /* word 84 bit 1 */
400 "SMART error logging " /* word 84 bit 0 */
403 static void identify(uint16_t *id_supplied) ATTRIBUTE_NORETURN;
404 static void identify_from_stdin(void) ATTRIBUTE_NORETURN;
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 * const 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 */
457 #define TIMING_BUF_MB 1
458 #define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
459 #define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
460 #define BUFCACHE_FACTOR 2
462 #undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
464 /* Busybox messages and functions */
465 static int bb_ioctl(int fd, int request, void *argp, const char *string)
467 int e = ioctl(fd, request, argp);
469 bb_perror_msg(" %s", string);
473 static int bb_ioctl_alt(int fd, int cmd, unsigned char *args, int alt, const char *string)
475 if (!ioctl(fd, cmd, args))
478 return bb_ioctl(fd, cmd, args, string);
481 static void on_off(unsigned int value);
483 static void print_flag_on_off(unsigned long get_arg, const char *s, unsigned long arg)
488 printf(" setting %s to %ld", s, arg);
493 static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
496 if (ioctl(fd, request, &argp) != 0)
497 bb_perror_msg(" %s", string);
500 printf(" %s\t= %2ld", str, (unsigned long) argp);
501 on_off((unsigned long) argp);
505 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
506 static void print_ascii(uint16_t *p, uint8_t length);
508 static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
512 printf("\t%-20s",string);
513 print_ascii(&val[i], n);
517 /* end of busybox specific stuff */
519 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
520 static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
525 for (ii = 0; ii <= MODE_MAX; ii++)
527 if (mode_sel & 0x0001)
529 printf("*%cdma%u ",cc,ii);
534 else if (mode_sup & 0x0001)
535 printf("%cdma%u ",cc,ii);
543 static void print_ascii(uint16_t *p, uint8_t length) {
547 /* find first non-space & print it */
548 for (ii = 0; ii< length; ii++)
550 if (((char) 0x00ff&((*p)>>8)) != ' ')
552 if ((cl = (char) 0x00ff&(*p)) != ' ')
554 if (cl != '\0') printf("%c",cl);
562 for (; ii< length; ii++)
565 break; /* some older devices have NULLs */
566 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
572 /* identify() is the only extern function used across two source files. The
573 others, though, were declared in hdparm.c with global scope; since other
574 functions in that file have static (file) scope, I assume the difference is
576 static void identify(uint16_t *id_supplied)
579 uint16_t *val, ii, jj, kk;
580 uint16_t like_std = 1, std = 0, min_std = 0xffff;
581 uint16_t dev = NO_DEV, eqpt = NO_DEV;
582 uint8_t have_mode = 0, err_dma = 0;
584 uint32_t ll, mm, nn, oo;
585 uint64_t bbbig; /* (:) */
589 swab(id_supplied, buf, sizeof(buf));
591 } else val = id_supplied;
595 /* check if we recognise the device type */
597 if(!(val[GEN_CONFIG] & NOT_ATA))
600 printf("ATA device, with ");
602 else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
606 printf("CompactFlash ATA device, with ");
608 else if(!(val[GEN_CONFIG] & NOT_ATAPI))
611 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
612 printf("ATAPI %s, with ", pkt_str[eqpt]);
616 /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
617 bb_error_msg_and_die("Unknown device type");
619 printf("%sremovable media\n", !(val[GEN_CONFIG] & MEDIA_REMOVABLE) ? "non-" : "");
620 /* Info from the specific configuration word says whether or not the
621 * ID command completed correctly. It is only defined, however in
622 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
623 * standards. Since the values allowed for this word are extremely
624 * specific, it should be safe to check it now, even though we don't
625 * know yet what standard this device is using.
627 if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
628 (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
631 if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL))
632 printf("powers-up in standby; SET FEATURES subcmd spins-up.\n");
633 if(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE))
634 printf("\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
637 /* output the model and serial numbers and the fw revision */
638 xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
639 xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
640 xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
641 xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
642 xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
644 /* major & minor standards version number (Note: these words were not
645 * defined until ATA-3 & the CDROM std uses different words.) */
646 printf("Standards:");
649 if (val[MINOR] && (val[MINOR] <= MINOR_MAX))
651 if(like_std < 3) like_std = 3;
652 std = actual_ver[val[MINOR]];
653 if(std) printf("\n\tUsed: %s ",minor_str[val[MINOR]]);
656 /* looks like when they up-issue the std, they obsolete one;
657 * thus, only the newest 4 issues need be supported. (That's
658 * what "kk" and "min_std" are all about.) */
659 if (val[MAJOR] && (val[MAJOR] !=NOVAL_1))
661 printf("\n\tSupported: ");
662 jj = val[MAJOR] << 1;
663 kk = like_std >4 ? like_std-4: 0;
664 for (ii = 14; (ii >0)&&(ii>kk); ii--)
672 kk = like_std >4 ? like_std-4: 0;
674 if(min_std > ii) min_std = ii;
678 if(like_std < 3) like_std = 3;
680 /* Figure out what standard the device is using if it hasn't told
681 * us. If we know the std, check if the device is using any of
682 * the words from the next level up. It happens.
684 if(like_std < std) like_std = std;
686 if (((std == 5) || (!std && (like_std < 6))) &&
687 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
688 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
689 ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
690 ( val[CMDS_SUPP_2] & CMDS_W84) ) ) )
694 else if (((std == 4) || (!std && (like_std < 5))) &&
695 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
696 (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
697 ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
698 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
702 else if (((std == 3) || (!std && (like_std < 4))) &&
703 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
704 ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
705 (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
706 (( val[CAPAB_1] & VALID) == VALID_VAL) ||
707 (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
708 (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
712 else if (((std == 2) || (!std && (like_std < 3))) &&
713 ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
717 else if (((std == 1) || (!std && (like_std < 2))) &&
718 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
719 (val[WHATS_VALID] & OK_W64_70)) )
724 printf("\n\tLikely used: %u\n",like_std);
725 else if (like_std > std)
726 printf("& some of %u\n",like_std);
732 /* TBD: do CDROM stuff more thoroughly. For now... */
734 if (val[CDR_MINOR] == 9)
737 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
739 if (val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
742 printf("\n\tSupported: CD-ROM ATAPI");
743 jj = val[CDR_MAJOR] >> 1;
744 for (ii = 1; ii <15; ii++)
746 if(jj & 0x0001) printf("-%u ", ii);
750 printf("%s\n", (!kk) ? "\n\tLikely used CD-ROM ATAPI-1" : "" );
751 /* the cdrom stuff is more like ATA-2 than anything else, so: */
755 if (min_std == 0xffff)
756 min_std = like_std > 4 ? like_std - 3 : 1;
758 printf("Configuration:\n");
759 /* more info from the general configuration word */
760 if ((eqpt != CDROM) && (like_std == 1))
762 jj = val[GEN_CONFIG] >> 1;
763 for (ii = 1; ii < 15; ii++)
765 if(jj & 0x0001) printf("\t%s\n",ata1_cfg_str[ii]);
769 if (dev == ATAPI_DEV)
771 if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_3MS_VAL)
773 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_INTR_VAL)
774 strng = "<=10ms with INTRQ";
775 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_50US_VAL)
779 printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */
781 if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_12_VAL)
783 else if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_16_VAL)
791 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
792 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
794 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
795 printf("\tCHS addressing not supported\n");
798 jj = val[WHATS_VALID] & OK_W54_58;
799 printf("\tLogical\t\tmax\tcurrent\n\tcylinders\t%u\t%u\n\theads\t\t%u\t%u\n\tsectors/track\t%u\t%u\n\t--\n",
800 val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
802 if ((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES]))
803 printf("\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
807 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
810 /* check Endian of capacity bytes */
811 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
812 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
813 if (abs(mm - nn) > abs(oo - nn))
816 printf("\tCHS current addressable sectors:%11u\n",mm);
820 printf("\tLBA user addressable sectors:%11u\n",ll);
821 if ( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
822 (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
824 bbbig = (uint64_t)val[LBA_64_MSB] << 48 |
825 (uint64_t)val[LBA_48_MSB] << 32 |
826 (uint64_t)val[LBA_MID] << 16 |
828 printf("\tLBA48 user addressable sectors:%11llu\n",(long long unsigned int)bbbig);
832 bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
833 printf("\tdevice size with M = 1024*1024: %11llu MBytes\n",(long long unsigned int)(bbbig>>11));
834 bbbig = (bbbig<<9)/1000000;
835 printf("\tdevice size with M = 1000*1000: %11llu MBytes ",(long long unsigned int)bbbig);
838 printf("(%llu GB)\n", (long long unsigned int)bbbig/1000);
843 /* hw support of commands (capabilities) */
844 printf("Capabilities:\n\t");
846 if (dev == ATAPI_DEV)
848 if (eqpt != CDROM && (val[CAPAB_0] & CMD_Q_SUP)) printf("Cmd queuing, ");
849 if (val[CAPAB_0] & OVLP_SUP) printf("Cmd overlap, ");
851 if (val[CAPAB_0] & LBA_SUP) printf("LBA, ");
855 printf("IORDY%s(can%s be disabled)\n",
856 !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "",
857 (val[CAPAB_0] & IORDY_OFF) ? "" :"not");
860 printf("no IORDY\n");
862 if ((like_std == 1) && val[BUF_TYPE])
864 printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE],
865 (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector",
866 (val[BUF_TYPE] > 2) ? " with read caching ability" : "");
869 if ((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
871 printf("\tBuffer size: %.1fkB\n",(float)val[BUFFER__SIZE]/2);
873 if ((min_std < 4) && (val[RW_LONG]))
875 printf("\tbytes avail on r/w long: %u\n",val[RW_LONG]);
877 if ((eqpt != CDROM) && (like_std > 3))
879 printf("\tQueue depth: %u\n",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
885 printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
888 printf("\tStandby timer values: spec'd by %s", (val[CAPAB_0] & STD_STBY) ? "Standard" : "Vendor");
889 if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
890 printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
894 printf("\tR/W multiple sector transfer: ");
895 if ((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
896 printf("not supported\n");
899 printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
900 if (val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
901 printf("%u\n", val[SECTOR_XFER_CUR] & SECTOR_XFER);
905 if ((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
907 /* We print out elsewhere whether the APM feature is enabled or
908 not. If it's not enabled, let's not repeat the info; just print
910 printf("\tAdvancedPM level: ");
911 if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
913 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
914 printf("%u (0x%x)\n", apm_level, apm_level);
917 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
919 if (like_std > 5 && val[ACOUSTIC]) {
920 printf("\tRecommended acoustic management value: %u, current value: %u\n",
921 (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
927 if (eqpt != CDROM && (val[CAPAB_0] & SWRST_REQ))
928 printf("\tATA sw reset required\n");
930 if (val[PKT_REL] || val[SVC_NBSY])
932 printf("\tOverlap support:");
933 if (val[PKT_REL]) printf(" %uus to release bus.",val[PKT_REL]);
934 if (val[SVC_NBSY]) printf(" %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
939 /* DMA stuff. Check that only one DMA mode is selected. */
941 if (!(val[CAPAB_0] & DMA_SUP))
942 printf("not supported\n");
945 if (val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
946 printf(" sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
949 jj = val[SINGLE_DMA];
950 kk = val[SINGLE_DMA] >> 8;
951 err_dma += mode_loop(jj,kk,'s',&have_mode);
956 kk = val[MULTI_DMA] >> 8;
957 err_dma += mode_loop(jj,kk,'m',&have_mode);
959 if ((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
962 kk = val[ULTRA_DMA] >> 8;
963 err_dma += mode_loop(jj,kk,'u',&have_mode);
965 if (err_dma || !have_mode) printf("(?)");
968 if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
969 printf("\t\tInterleaved DMA support\n");
971 if ((val[WHATS_VALID] & OK_W64_70) &&
972 (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
974 printf("\t\tCycle time:");
975 if (val[DMA_TIME_MIN]) printf(" min=%uns",val[DMA_TIME_MIN]);
976 if (val[DMA_TIME_NORM]) printf(" recommended=%uns",val[DMA_TIME_NORM]);
981 /* Programmed IO stuff */
983 /* If a drive supports mode n (e.g. 3), it also supports all modes less
984 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
985 if ((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
987 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
988 for (ii = 0; ii <= PIO_MODE_MAX ; ii++)
990 if (jj & 0x0001) printf("pio%d ",ii);
995 else if (((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
997 for (ii = 0; ii <= val[PIO_MODE]>>8; ii++)
1002 printf("unknown\n");
1004 if (val[WHATS_VALID] & OK_W64_70)
1006 if (val[PIO_NO_FLOW] || val[PIO_FLOW])
1008 printf("\t\tCycle time:");
1009 if (val[PIO_NO_FLOW]) printf(" no flow control=%uns", val[PIO_NO_FLOW]);
1010 if (val[PIO_FLOW]) printf(" IORDY flow control=%uns", val[PIO_FLOW]);
1015 if ((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
1017 printf("Commands/features:\n\tEnabled\tSupported:\n");
1018 jj = val[CMDS_SUPP_0];
1019 kk = val[CMDS_EN_0];
1020 for (ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
1022 if ((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
1024 printf("\t%s\t%s\n", (kk & 0x8000) ? " *" : "", cmd_feat_str[ii]);
1029 jj = val[CMDS_SUPP_0+1+(ii/16)];
1030 kk = val[CMDS_EN_0+1+(ii/16)];
1034 if ((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
1039 /* Removable Media Status Notification feature set */
1040 if((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
1041 printf("\t%s supported\n", cmd_feat_str[27]);
1045 if ((eqpt != CDROM) && (like_std > 3) &&
1046 (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1048 printf("Security:\n");
1049 if (val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
1050 printf("\tMaster password revision code = %u\n",val[PSWD_CODE]);
1051 jj = val[SECU_STATUS];
1054 for (ii = 0; ii < NUM_SECU_STR; ii++)
1056 printf("\t%s\t%s\n", (!(jj & 0x0001)) ? "not" : "", secu_str[ii]);
1059 if (val[SECU_STATUS] & SECU_ENABLED)
1061 printf("\tSecurity level %s\n", (val[SECU_STATUS] & SECU_LEVEL) ? "maximum" : "high");
1064 jj = val[ERASE_TIME] & ERASE_BITS;
1065 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
1069 if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, "");
1070 if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED ");
1076 jj = val[HWRST_RSLT];
1077 if ((jj & VALID) == VALID_VAL)
1079 if (!(oo = (jj & RST0)))
1081 if ((jj & DEV_DET) == JUMPER_VAL)
1082 strng = " determined by the jumper";
1083 else if ((jj & DEV_DET) == CSEL_VAL)
1084 strng = " determined by CSEL";
1087 printf("HW reset results:\n\tCBLID- %s Vih\n\tDevice num = %i%s\n",
1088 (val[HWRST_RSLT] & CBLID) ? "above" : "below", !(oo), strng);
1091 /* more stuff from std 5 */
1092 if ((like_std > 4) && (eqpt != CDROM))
1094 if (val[CFA_PWR_MODE] & VALID_W160)
1096 printf("CFA power mode 1:\n\t%s%s\n", (val[CFA_PWR_MODE] & PWR_MODE_OFF) ? "disabled" : "enabled",
1097 (val[CFA_PWR_MODE] & PWR_MODE_REQ) ? " and required by some commands" : "");
1099 if (val[CFA_PWR_MODE] & MAX_AMPS) printf("\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
1101 if ((val[INTEGRITY] & SIG) == SIG_VAL)
1103 printf("Checksum: %scorrect\n", chksum ? "in" : "");
1111 static int get_identity, get_geom;
1112 static int do_flush;
1113 static int do_ctimings, do_timings;
1114 static unsigned long set_readahead, get_readahead, Xreadahead;
1115 static unsigned long set_readonly, get_readonly, readonly;
1116 static unsigned long set_unmask, get_unmask, unmask;
1117 static unsigned long set_mult, get_mult, mult;
1118 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1119 static unsigned long set_dma, get_dma, dma;
1121 static unsigned long set_dma_q, get_dma_q, dma_q;
1122 static unsigned long set_nowerr, get_nowerr, nowerr;
1123 static unsigned long set_keep, get_keep, keep;
1124 static unsigned long set_io32bit, get_io32bit, io32bit;
1125 static unsigned long set_piomode, noisy_piomode;
1127 #ifdef HDIO_DRIVE_CMD
1128 static unsigned long set_dkeep, get_dkeep, dkeep;
1129 static unsigned long set_standby, get_standby, standby_requested;
1130 static unsigned long set_xfermode, get_xfermode;
1131 static int xfermode_requested;
1132 static unsigned long set_lookahead, get_lookahead, lookahead;
1133 static unsigned long set_prefetch, get_prefetch, prefetch;
1134 static unsigned long set_defects, get_defects, defects;
1135 static unsigned long set_wcache, get_wcache, wcache;
1136 static unsigned long set_doorlock, get_doorlock, doorlock;
1137 static unsigned long set_seagate, get_seagate;
1138 static unsigned long set_standbynow, get_standbynow;
1139 static unsigned long set_sleepnow, get_sleepnow;
1140 static unsigned long get_powermode;
1141 static unsigned long set_apmmode, get_apmmode, apmmode;
1143 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1144 static int get_IDentity;
1146 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1147 static unsigned long unregister_hwif;
1148 static unsigned long hwif;
1150 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1151 static unsigned long scan_hwif;
1152 static unsigned long hwif_data;
1153 static unsigned long hwif_ctrl;
1154 static unsigned long hwif_irq;
1156 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1157 static unsigned long set_busstate, get_busstate, busstate;
1159 static int reread_partn;
1161 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1162 static int perform_reset;
1163 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1164 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1165 static unsigned long perform_tristate, tristate;
1166 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1168 // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1169 // then the HDIO_GET_IDENTITY only returned 142 bytes.
1170 // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1171 // and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1172 // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1173 // (which they should, but they should just return -EINVAL).
1175 // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1176 // On a really old system, it will not, and we will be confused.
1179 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1180 static const char * const cfg_str[] =
1181 { "", "HardSect", "SoftSect", "NotMFM",
1182 "HdSw>15uSec", "SpinMotCtl", "Fixed", "Removeable",
1183 "DTR<=5Mbs", "DTR>5Mbs", "DTR>10Mbs", "RotSpdTol>.5%",
1184 "dStbOff", "TrkOff", "FmtGapReq", "nonMagnetic"
1187 static const char * const BuffType[] = {"Unknown", "1Sect", "DualPort", "DualPortCache"};
1189 static void dump_identity(const struct hd_driveid *id)
1192 const unsigned short int *id_regs= (const void*) id;
1194 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1195 id->model, id->fw_rev, id->serial_no);
1196 for (i=0; i<=15; i++) {
1197 if (id->config & (1<<i))
1198 printf(" %s", cfg_str[i]);
1200 printf( " }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n"
1201 " BuffType=(%u) %s, BuffSize=%ukB, MaxMultSect=%u",
1202 id->cyls, id->heads, id->sectors, id->track_bytes,
1203 id->sector_bytes, id->ecc_bytes,
1204 id->buf_type, BuffType[(id->buf_type > 3) ? 0 : id->buf_type],
1205 id->buf_size/2, id->max_multsect);
1206 if (id->max_multsect)
1208 printf(", MultSect=");
1209 if (!(id->multsect_valid&1))
1210 printf("?%u?", id->multsect);
1211 else if (id->multsect)
1212 printf("%u", id->multsect);
1218 if(!(id->field_valid&1))
1219 printf(" (maybe):");
1221 printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
1224 (long unsigned int)(id->cur_capacity0 << 16) | id->cur_capacity1 :
1225 (long unsigned int)(id->cur_capacity1 << 16) | id->cur_capacity0,
1226 ((id->capability&2) == 0) ? "no" : "yes");
1228 if (id->capability&2)
1229 printf(", LBAsects=%u", id->lba_capacity);
1231 printf("\n IORDY=%s", (id->capability&8) ? (id->capability&4) ? "on/off" : "yes" : "no");
1233 if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&2)
1234 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1236 if ((id->capability&1) && (id->field_valid&2))
1237 printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1239 printf("\n PIO modes: ");
1243 if (id->tPIO >= 1) printf("pio1 ");
1244 if (id->tPIO >= 2) printf("pio2 ");
1246 if (id->field_valid&2)
1248 if (id->eide_pio_modes & 1) printf("pio3 ");
1249 if (id->eide_pio_modes & 2) printf("pio4 ");
1250 if (id->eide_pio_modes &~3) printf("pio? ");
1252 if (id->capability&1)
1254 if (id->dma_1word | id->dma_mword)
1256 printf("\n DMA modes: ");
1257 if (id->dma_1word & 0x100) printf("*");
1258 if (id->dma_1word & 1) printf("sdma0 ");
1259 if (id->dma_1word & 0x200) printf("*");
1260 if (id->dma_1word & 2) printf("sdma1 ");
1261 if (id->dma_1word & 0x400) printf("*");
1262 if (id->dma_1word & 4) printf("sdma2 ");
1263 if (id->dma_1word & 0xf800) printf("*");
1264 if (id->dma_1word & 0xf8) printf("sdma? ");
1265 if (id->dma_mword & 0x100) printf("*");
1266 if (id->dma_mword & 1) printf("mdma0 ");
1267 if (id->dma_mword & 0x200) printf("*");
1268 if (id->dma_mword & 2) printf("mdma1 ");
1269 if (id->dma_mword & 0x400) printf("*");
1270 if (id->dma_mword & 4) printf("mdma2 ");
1271 if (id->dma_mword & 0xf800) printf("*");
1272 if (id->dma_mword & 0xf8) printf("mdma? ");
1275 if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&4)
1277 printf("\n UDMA modes: ");
1278 if (id->dma_ultra & 0x100) printf("*");
1279 if (id->dma_ultra & 0x001) printf("udma0 ");
1280 if (id->dma_ultra & 0x200) printf("*");
1281 if (id->dma_ultra & 0x002) printf("udma1 ");
1282 if (id->dma_ultra & 0x400) printf("*");
1283 if (id->dma_ultra & 0x004) printf("udma2 ");
1284 #ifdef __NEW_HD_DRIVE_ID
1285 if (id->hw_config & 0x2000)
1287 #else /* !__NEW_HD_DRIVE_ID */
1288 if (id->word93 & 0x2000)
1290 #endif /* __NEW_HD_DRIVE_ID */
1291 if (id->dma_ultra & 0x0800) printf("*");
1292 if (id->dma_ultra & 0x0008) printf("udma3 ");
1293 if (id->dma_ultra & 0x1000) printf("*");
1294 if (id->dma_ultra & 0x0010) printf("udma4 ");
1295 if (id->dma_ultra & 0x2000) printf("*");
1296 if (id->dma_ultra & 0x0020) printf("udma5 ");
1297 if (id->dma_ultra & 0x4000) printf("*");
1298 if (id->dma_ultra & 0x0040) printf("udma6 ");
1299 if (id->dma_ultra & 0x8000) printf("*");
1300 if (id->dma_ultra & 0x0080) printf("udma7 ");
1303 printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
1304 if (id_regs[83] & 8)
1306 if (!(id_regs[86]&8))
1307 printf(": disabled (255)");
1308 else if ((id_regs[91]&0xFF00)!=0x4000)
1309 printf(": unknown setting");
1311 printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1313 if (id_regs[82]&0x20)
1314 printf(" WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
1315 #ifdef __NEW_HD_DRIVE_ID
1316 if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
1318 printf("\n Drive conforms to: %s: ", (id->minor_rev_num <= 31) ? minor_str[id->minor_rev_num] : "Unknown");
1319 if (id->major_rev_num != 0x0000 && /* NOVAL_0 */
1320 id->major_rev_num != 0xFFFF) { /* NOVAL_1 */
1321 for (i=0; i <= 15; i++) {
1322 if (id->major_rev_num & (1<<i))
1323 printf(" ATA/ATAPI-%u", i);
1327 #endif /* __NEW_HD_DRIVE_ID */
1328 printf("\n\n * current active mode\n\n");
1332 static void flush_buffer_cache(int fd)
1334 fsync(fd); /* flush buffers */
1335 bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
1336 #ifdef HDIO_DRIVE_CMD
1338 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
1339 bb_perror_msg("HDIO_DRIVE_CMD");
1343 static int seek_to_zero(int fd)
1345 if (lseek(fd, (off_t) 0, SEEK_SET))
1350 static int read_big_block(int fd, char *buf)
1354 if ((i = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES) {
1355 bb_error_msg("read(%d bytes) failed (rc=%d)", TIMING_BUF_BYTES, i);
1358 /* access all sectors of buf to ensure the read fully completed */
1359 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1364 static void print_timing(int t, double e)
1366 if (t >= e) /* more than 1MB/s */
1367 printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e, 'M');
1369 printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e * 1024, 'k');
1372 static int do_blkgetsize (int fd, unsigned long long *blksize64)
1375 unsigned int blksize32 = 0;
1377 if (0 == ioctl(fd, BLKGETSIZE64, blksize64)) { // returns bytes
1381 rc = ioctl(fd, BLKGETSIZE, &blksize32); // returns sectors
1383 bb_perror_msg("BLKGETSIZE");
1384 *blksize64 = blksize32;
1388 static void do_time(int flag, int fd)
1391 flag = 1 time_device
1394 struct itimerval e1, e2;
1395 double elapsed, elapsed2;
1396 unsigned int max_iterations = 1024, total_MB, iterations;
1397 unsigned long long blksize;
1398 RESERVE_CONFIG_BUFFER(buf, TIMING_BUF_BYTES);
1400 if (mlock(buf, TIMING_BUF_BYTES)) {
1401 bb_perror_msg("mlock");
1405 if (0 == do_blkgetsize(fd, &blksize)) {
1406 max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB;
1409 /* Clear out the device request queues & give them time to complete */
1413 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1415 if (flag == 0) /* Time cache */
1417 if (seek_to_zero (fd)) return;
1418 if (read_big_block (fd, buf)) return;
1419 printf(" Timing cached reads: ");
1422 /* Now do the timing */
1424 getitimer(ITIMER_REAL, &e1);
1427 if (seek_to_zero (fd) || read_big_block (fd, buf))
1429 getitimer(ITIMER_REAL, &e2);
1430 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1431 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1432 } while (elapsed < 2.0);
1433 total_MB = iterations * TIMING_BUF_MB;
1435 /* Now remove the lseek() and getitimer() overheads from the elapsed time */
1436 getitimer(ITIMER_REAL, &e1);
1438 if (seek_to_zero (fd))
1440 getitimer(ITIMER_REAL, &e2);
1441 elapsed2 = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1442 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1443 } while (--iterations);
1445 elapsed -= elapsed2;
1446 print_timing(BUFCACHE_FACTOR * total_MB, elapsed);
1447 flush_buffer_cache(fd);
1450 else /* Time device */
1452 printf(" Timing buffered disk reads: ");
1455 * getitimer() is used rather than gettimeofday() because
1456 * it is much more consistent (on my machine, at least).
1458 /* Now do the timings for real */
1460 getitimer(ITIMER_REAL, &e1);
1463 if (read_big_block (fd, buf))
1465 getitimer(ITIMER_REAL, &e2);
1466 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1467 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1468 } while (elapsed < 3.0 && iterations < max_iterations);
1470 total_MB = iterations * TIMING_BUF_MB;
1471 print_timing(total_MB, elapsed);
1474 munlock(buf, TIMING_BUF_BYTES);
1476 RELEASE_CONFIG_BUFFER(buf);
1479 static void on_off (unsigned int value)
1481 printf(value ? " (on)\n" : " (off)\n");
1484 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1485 static void bus_state_value(unsigned int value)
1487 if (value == BUSSTATE_ON)
1489 else if (value == BUSSTATE_OFF)
1491 else if (value == BUSSTATE_TRISTATE)
1492 printf(" (tristate)\n");
1494 printf(" (unknown: %d)\n", value);
1498 #ifdef HDIO_DRIVE_CMD
1499 static void interpret_standby(unsigned int standby)
1506 else if (standby == 252)
1507 printf("21 minutes");
1508 else if (standby == 253)
1509 printf("vendor-specific");
1510 else if (standby == 254)
1512 else if (standby == 255)
1513 printf("21 minutes + 15 seconds");
1515 if (standby <= 240) {
1517 printf("%u minutes + %u seconds", t / 60, t % 60);
1518 } else if (standby <= 251) {
1519 t = (standby - 240) * 30;
1520 printf("%u hours + %u minutes", t / 60, t % 60);
1522 printf("illegal value");
1527 struct xfermode_entry {
1532 static const struct xfermode_entry xfermode_table[] = {
1568 static int translate_xfermode(char * name)
1570 const struct xfermode_entry *tmp;
1575 for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1577 if (!strcmp(name, tmp->name))
1581 val = strtol(name, &endptr, 10);
1582 if (*endptr == '\0')
1588 static void interpret_xfermode(unsigned int xfermode)
1592 printf("default PIO mode");
1593 else if (xfermode == 1)
1594 printf("default PIO mode, disable IORDY");
1595 else if (xfermode >= 8 && xfermode <= 15)
1596 printf("PIO flow control mode%u", xfermode-8);
1597 else if (xfermode >= 16 && xfermode <= 23)
1598 printf("singleword DMA mode%u", xfermode-16);
1599 else if (xfermode >= 32 && xfermode <= 39)
1600 printf("multiword DMA mode%u", xfermode-32);
1601 else if (xfermode >= 64 && xfermode <= 71)
1602 printf("UltraDMA mode%u", xfermode-64);
1607 #endif /* HDIO_DRIVE_CMD */
1609 static void print_flag(unsigned long flag, char *s, unsigned long value)
1612 printf(" setting %s to %ld\n", s, value);
1615 static void process_dev(char *devname)
1618 static long parm, multcount;
1619 #ifndef HDIO_DRIVE_CMD
1620 int force_operation = 0;
1622 /* Please restore args[n] to these values after each ioctl
1623 except for args[2] */
1624 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1625 const char *fmt = " %s\t= %2ld";
1627 fd = bb_xopen(devname, O_RDONLY|O_NONBLOCK);
1628 printf("\n%s:\n", devname);
1632 print_flag(get_readahead,"fs readahead", Xreadahead);
1633 bb_ioctl(fd, BLKRASET,(int *)Xreadahead,"BLKRASET");
1635 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1636 if (unregister_hwif)
1638 printf(" attempting to unregister hwif#%lu\n", hwif);
1639 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)(unsigned long)hwif,"HDIO_UNREGISTER_HWIF");
1642 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1645 printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq);
1646 args[0] = hwif_data;
1647 args[1] = hwif_ctrl;
1649 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
1650 args[0] = WIN_SETFEATURES;
1658 printf(" attempting to ");
1660 printf("auto-tune PIO mode\n");
1661 else if (piomode < 100)
1662 printf("set PIO mode to %d\n", piomode);
1663 else if (piomode < 200)
1664 printf("set MDMA mode to %d\n", (piomode-100));
1666 printf("set UDMA mode to %d\n", (piomode-200));
1668 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode, "HDIO_SET_PIO_MODE");
1672 print_flag(get_io32bit,"32-bit IO_support flag", io32bit);
1673 bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
1677 print_flag(get_mult, "multcount", mult);
1678 #ifdef HDIO_DRIVE_CMD
1679 bb_ioctl(fd, HDIO_SET_MULTCOUNT, &mult, "HDIO_SET_MULTCOUNT");
1681 force_operation |= (!bb_ioctl(fd, HDIO_SET_MULTCOUNT, &mult, "HDIO_SET_MULTCOUNT"));
1686 print_flag_on_off(get_readonly,"readonly", readonly);
1687 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
1691 print_flag_on_off(get_unmask,"unmaskirq", unmask);
1692 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
1694 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1697 print_flag_on_off(get_dma, "using_dma", dma);
1698 bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
1700 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
1703 print_flag_on_off(get_dma_q,"DMA queue_depth", dma_q);
1704 bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
1708 print_flag_on_off(get_nowerr,"nowerr", nowerr);
1709 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
1713 print_flag_on_off(get_keep,"keep_settings", keep);
1714 bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
1716 #ifdef HDIO_DRIVE_CMD
1719 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
1721 print_flag_on_off(get_doorlock,"drive doorlock", doorlock);
1722 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
1723 args[0] = WIN_SETFEATURES;
1727 /* lock/unlock the drive's "feature" settings */
1728 print_flag_on_off(get_dkeep,"drive keep features", dkeep);
1729 args[2] = dkeep ? 0x66 : 0xcc;
1730 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
1734 args[2] = defects ? 0x04 : 0x84;
1735 print_flag(get_defects,"drive defect-mgmt", defects);
1736 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
1742 print_flag(get_prefetch,"drive prefetch", prefetch);
1743 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
1748 args[1] = xfermode_requested;
1752 print_flag(1,"xfermode", xfermode_requested);
1753 interpret_xfermode(xfermode_requested);
1755 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
1760 args[2] = lookahead ? 0xaa : 0x55;
1761 print_flag_on_off(get_lookahead,"drive read-lookahead", lookahead);
1762 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
1766 args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */; /* feature register */
1767 args[1] = apmmode; /* sector count register 1-255 */
1769 printf(" setting APM level to %s 0x%02lX (%ld)\n", (apmmode == 255) ? "disabled" : "", apmmode, apmmode);
1770 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
1775 #ifdef DO_FLUSHCACHE
1776 #ifndef WIN_FLUSHCACHE
1777 #define WIN_FLUSHCACHE 0xe7
1779 static unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
1780 #endif /* DO_FLUSHCACHE */
1781 args[2] = wcache ? 0x02 : 0x82;
1782 print_flag_on_off(get_wcache,"drive write-caching", wcache);
1783 #ifdef DO_FLUSHCACHE
1785 bb_ioctl(fd, HDIO_DRIVE_CMD, &flushcache, "HDIO_DRIVE_CMD(flushcache)");
1786 #endif /* DO_FLUSHCACHE */
1787 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
1788 #ifdef DO_FLUSHCACHE
1790 bb_ioctl(fd, HDIO_DRIVE_CMD, &flushcache, "HDIO_DRIVE_CMD(flushcache)");
1791 #endif /* DO_FLUSHCACHE */
1794 /* In code below, we do not preserve args[0], but the rest
1795 is preserved, including args[2] */
1800 #ifndef WIN_STANDBYNOW1
1801 #define WIN_STANDBYNOW1 0xE0
1803 #ifndef WIN_STANDBYNOW2
1804 #define WIN_STANDBYNOW2 0x94
1806 if (get_standbynow) printf(" issuing standby command\n");
1807 args[0] = WIN_STANDBYNOW1;
1808 bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2, "HDIO_DRIVE_CMD(standby)");
1812 #ifndef WIN_SLEEPNOW1
1813 #define WIN_SLEEPNOW1 0xE6
1815 #ifndef WIN_SLEEPNOW2
1816 #define WIN_SLEEPNOW2 0x99
1818 if (get_sleepnow) printf(" issuing sleep command\n");
1819 args[0] = WIN_SLEEPNOW1;
1820 bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2, "HDIO_DRIVE_CMD(sleep)");
1825 if (get_seagate) printf(" disabling Seagate auto powersaving mode\n");
1826 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
1830 args[0] = WIN_SETIDLE1;
1831 args[1] = standby_requested;
1834 print_flag(1,"standby", standby_requested);
1835 interpret_standby(standby_requested);
1837 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
1840 #else /* HDIO_DRIVE_CMD */
1841 if (force_operation)
1844 flush_buffer_cache(fd);
1845 if (-1 == read(fd, buf, sizeof(buf)))
1846 bb_perror_msg("read(%d bytes) failed (rc=%d)", sizeof(buf), -1);
1848 #endif /* HDIO_DRIVE_CMD */
1850 if (get_mult || get_identity)
1853 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
1856 bb_perror_msg("HDIO_GET_MULTCOUNT");
1860 printf(fmt, "multcount", multcount);
1866 if (!bb_ioctl(fd, HDIO_GET_32BIT, &parm, "HDIO_GET_32BIT"))
1868 printf(" IO_support\t=%3ld (", parm);
1870 printf("default 16-bit)\n");
1872 printf("16-bit)\n");
1874 printf("32-bit)\n");
1876 printf("32-bit w/sync)\n");
1878 printf("Request-Queue-Bypass)\n");
1880 printf("\?\?\?)\n");
1885 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
1886 "HDIO_GET_UNMASKINTR","unmaskirq");
1890 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1892 if (!bb_ioctl(fd, HDIO_GET_DMA, &parm, "HDIO_GET_DMA"))
1894 printf(fmt, "using_dma", parm);
1896 printf(" (DMA-Assisted-PIO)\n");
1904 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
1905 "HDIO_GET_QDMA","queue_depth");
1909 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
1910 "HDIO_GET_KEEPSETTINGS","keepsettings");
1915 bb_ioctl_on_off (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
1916 "HDIO_GET_NOWERR","nowerr");
1920 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
1921 "BLKROGET","readonly");
1925 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
1926 "BLKRAGET","readahead");
1930 if (!bb_ioctl(fd, BLKGETSIZE, &parm, "BLKGETSIZE"))
1932 struct hd_geometry g;
1934 if (!bb_ioctl(fd, HDIO_GETGEO, &g, "HDIO_GETGEO"))
1935 printf(" geometry\t= %u/%u/%u, sectors = %ld, start = %ld\n",
1936 g.cylinders, g.heads, g.sectors, parm, g.start);
1939 #ifdef HDIO_DRIVE_CMD
1942 #ifndef WIN_CHECKPOWERMODE1
1943 #define WIN_CHECKPOWERMODE1 0xE5
1945 #ifndef WIN_CHECKPOWERMODE2
1946 #define WIN_CHECKPOWERMODE2 0x98
1950 args[0] = WIN_CHECKPOWERMODE1;
1951 if (bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2, 0))
1953 if (errno != EIO || args[0] != 0 || args[1] != 0)
1959 state = (args[2] == 255) ? "active/idle" : "standby";
1960 args[1] = args[2] = 0;
1962 printf(" drive state is: %s\n", state);
1965 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1968 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
1970 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1971 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1972 if (perform_tristate)
1976 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
1978 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1979 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1982 static struct hd_driveid id;
1984 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
1986 if (multcount != -1)
1988 id.multsect = multcount;
1989 id.multsect_valid |= 1;
1992 id.multsect_valid &= ~1;
1995 else if (errno == -ENOMSG)
1996 printf(" no identification info available\n");
1998 bb_perror_msg("HDIO_GET_IDENTITY");
2003 unsigned char args1[4+512]; /* = { ... } will eat 0.5k of rodata! */
2006 memset(args1, 0, sizeof(args1));
2007 args1[0] = WIN_IDENTIFY;
2009 if (!bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY, "HDIO_DRIVE_CMD(identify)")) {
2010 for (i=0; i<(sizeof args1)/2; i+=2)
2011 __le16_to_cpus((uint16_t *)(&args1[i]));
2012 identify((void *)&args1[4]);
2016 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2021 print_flag(1, "bus state", busstate);
2022 bus_state_value(busstate);
2024 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate, "HDIO_SET_BUSSTATE");
2028 if (!bb_ioctl(fd, HDIO_GET_BUSSTATE, &parm, "HDIO_GET_BUSSTATE"))
2030 printf(fmt, "bus state", parm);
2031 bus_state_value(parm);
2036 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2040 do_time(0,fd); /*time cache */
2042 do_time(1,fd); /*time device */
2044 flush_buffer_cache(fd);
2048 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2049 static int fromhex(unsigned char c)
2051 if (c >= 'a' && c <= 'f')
2052 return 10 + (c - 'a');
2053 if (c >= '0' && c <= '9')
2055 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2058 static void identify_from_stdin(void)
2060 unsigned short sbuf[800];
2061 unsigned char buf[1600], *b = (unsigned char *)buf;
2062 int i, count = read(0, buf, 1280);
2065 bb_error_msg_and_die("read(%d bytes) failed (rc=%d)", 1280, count);
2067 for (i = 0; count >= 4; ++i)
2069 sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
2070 __le16_to_cpus((uint16_t *)(&sbuf[i]));
2078 /* busybox specific stuff */
2079 static void parse_opts(unsigned long *get, unsigned long *set, unsigned long *value, int min, int max)
2086 *value = bb_xgetlarg(optarg, 10, min, max);
2090 static void parse_xfermode(int flag, unsigned long *get, unsigned long *set, int *value)
2095 *set = ((*value = translate_xfermode(optarg)) > -1);
2100 /*-------------------------------------*/
2101 /* getopt long options */
2102 #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
2103 static const struct option HDPARM_LONG_OPT[] =
2105 {"Istdin", 0, NULL, '\256'},
2109 #define HDPARM_LONG_OPT NULL
2111 /*-------------------------------------*/
2112 /* getopt short options */
2113 static const char hdparm_options[]= "gfu::n::p:r::m::c::k::a::B:tTh"\
2114 USE_FEATURE_HDPARM_GET_IDENTITY("\256iI")
2115 USE_FEATURE_HDPARM_HDIO_GETSET_DMA("d::")
2116 #ifdef HDIO_DRIVE_CMD
2117 "S::D::P::X::K::A::L::W::CyYzZ"
2119 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF("U:")
2120 #ifdef HDIO_GET_QDMA
2121 #ifdef HDIO_SET_QDMA
2127 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET("w")
2128 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF("x::b:")
2129 USE_FEATURE_HDPARM_HDIO_SCAN_HWIF("R:");
2130 /*-------------------------------------*/
2132 /* our main() routine: */
2133 int hdparm_main(int argc, char **argv) ATTRIBUTE_NORETURN;
2134 int hdparm_main(int argc, char **argv)
2139 while ((c = getopt_long (argc, argv, hdparm_options, HDPARM_LONG_OPT , NULL)) >= 0) {
2141 #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
2143 identify_from_stdin(); /* EXIT */
2146 USE_FEATURE_HDPARM_GET_IDENTITY(get_IDentity = (c == 'I'));
2147 USE_FEATURE_HDPARM_GET_IDENTITY(get_identity = (c == 'i'));
2148 get_geom |= (c == 'g');
2149 do_flush |= (c == 'f');
2150 if (c == 'u') parse_opts(&get_unmask, &set_unmask, &unmask, 0, 1);
2151 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(if (c == 'd') parse_opts(&get_dma, &set_dma, &dma, 0, 9));
2152 if (c == 'n') parse_opts(&get_nowerr, &set_nowerr, &nowerr, 0, 1);
2153 parse_xfermode((c == 'p'),&noisy_piomode, &set_piomode, &piomode);
2154 if (c == 'r') parse_opts(&get_readonly, &set_readonly, &readonly, 0, 1);
2155 if (c == 'm') parse_opts(&get_mult, &set_mult, &mult, 0, INT_MAX /*32*/);
2156 if (c == 'c') parse_opts(&get_io32bit, &set_io32bit, &io32bit, 0, INT_MAX /*8*/);
2157 if (c == 'k') parse_opts(&get_keep, &set_keep, &keep, 0, 1);
2158 if (c == 'a') parse_opts(&get_readahead, &set_readahead, &Xreadahead, 0, INT_MAX);
2159 if (c == 'B') parse_opts(&get_apmmode, &set_apmmode, &apmmode, 1, 255);
2160 do_flush |= do_timings |= (c == 't');
2161 do_flush |= do_ctimings |= (c == 'T');
2162 #ifdef HDIO_DRIVE_CMD
2163 if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, INT_MAX);
2164 if (c == 'D') parse_opts(&get_defects, &set_defects, &defects, 0, INT_MAX);
2165 if (c == 'P') parse_opts(&get_prefetch, &set_prefetch, &prefetch, 0, INT_MAX);
2166 parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
2167 if (c == 'K') parse_opts(&get_dkeep, &set_dkeep, &prefetch, 0, 1);
2168 if (c == 'A') parse_opts(&get_lookahead, &set_lookahead, &lookahead, 0, 1);
2169 if (c == 'L') parse_opts(&get_doorlock, &set_doorlock, &doorlock, 0, 1);
2170 if (c == 'W') parse_opts(&get_wcache, &set_wcache, &wcache, 0, 1);
2171 get_powermode |= (c == 'C');
2172 get_standbynow = set_standbynow |= (c == 'y');
2173 get_sleepnow = set_sleepnow |= (c == 'Y');
2174 reread_partn |= (c == 'z');
2175 get_seagate = set_seagate |= (c == 'Z');
2177 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') parse_opts(NULL, &unregister_hwif, &hwif, 0, INT_MAX));
2178 #ifdef HDIO_GET_QDMA
2180 #ifdef HDIO_SET_QDMA
2181 parse_opts(&get_dma_q, &set_dma_q, &dma_q, 0, INT_MAX);
2183 parse_opts(&get_dma_q, NULL, NULL, 0, 0);
2187 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
2188 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1));
2189 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') parse_opts(&get_busstate, &set_busstate, &busstate, 0, 2));
2190 #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
2192 parse_opts(NULL, &scan_hwif, &hwif_data, 0, INT_MAX);
2193 hwif_ctrl = bb_xgetlarg((argv[optind]) ? argv[optind] : "", 10, 0, INT_MAX);
2194 hwif_irq = bb_xgetlarg((argv[optind+1]) ? argv[optind+1] : "", 10, 0, INT_MAX);
2195 /* Move past the 2 additional arguments */
2201 /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
2203 get_mult = get_io32bit = get_unmask = get_keep = get_readonly = get_readahead = get_geom = 1;
2204 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(get_dma = 1);