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>
26 #include <linux/types.h>
27 #include <linux/hdreg.h>
29 #if BB_BIG_ENDIAN && !defined(__USE_XOPEN)
39 #define ATA_DEV 0x0000
40 #define ATAPI_DEV 0x0001
42 /* word definitions */
43 /* ---------------- */
44 #define GEN_CONFIG 0 /* general configuration */
45 #define LCYLS 1 /* number of logical cylinders */
46 #define CONFIG 2 /* specific configuration */
47 #define LHEADS 3 /* number of logical heads */
48 #define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
49 #define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
50 #define LSECTS 6 /* number of logical sectors/track */
51 #define START_SERIAL 10 /* ASCII serial number */
52 #define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
53 #define BUF_TYPE 20 /* buffer type (ATA-1) */
54 #define BUFFER__SIZE 21 /* buffer size (ATA-1) */
55 #define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
56 #define START_FW_REV 23 /* ASCII firmware revision */
57 #define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
58 #define START_MODEL 27 /* ASCII model number */
59 #define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
60 #define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
61 #define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
62 #define CAPAB_0 49 /* capabilities */
64 #define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
65 #define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
66 #define WHATS_VALID 53 /* what fields are valid */
67 #define LCYLS_CUR 54 /* current logical cylinders */
68 #define LHEADS_CUR 55 /* current logical heads */
69 #define LSECTS_CUR 56 /* current logical sectors/track */
70 #define CAPACITY_LSB 57 /* current capacity in sectors */
71 #define CAPACITY_MSB 58
72 #define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
73 #define LBA_SECTS_LSB 60 /* LBA: total number of user */
74 #define LBA_SECTS_MSB 61 /* addressable sectors */
75 #define SINGLE_DMA 62 /* singleword DMA modes */
76 #define MULTI_DMA 63 /* multiword DMA modes */
77 #define ADV_PIO_MODES 64 /* advanced PIO modes supported */
78 /* multiword DMA xfer cycle time: */
79 #define DMA_TIME_MIN 65 /* - minimum */
80 #define DMA_TIME_NORM 66 /* - manufacturer's recommended */
81 /* minimum PIO xfer cycle time: */
82 #define PIO_NO_FLOW 67 /* - without flow control */
83 #define PIO_FLOW 68 /* - with IORDY flow control */
84 #define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
85 #define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
86 #define CDR_MAJOR 73 /* CD ROM: major version number */
87 #define CDR_MINOR 74 /* CD ROM: minor version number */
88 #define QUEUE_DEPTH 75 /* queue depth */
89 #define MAJOR 80 /* major version number */
90 #define MINOR 81 /* minor version number */
91 #define CMDS_SUPP_0 82 /* command/feature set(s) supported */
92 #define CMDS_SUPP_1 83
93 #define CMDS_SUPP_2 84
94 #define CMDS_EN_0 85 /* command/feature set(s) enabled */
97 #define ULTRA_DMA 88 /* ultra DMA modes */
98 /* time to complete security erase */
99 #define ERASE_TIME 89 /* - ordinary */
100 #define ENH_ERASE_TIME 90 /* - enhanced */
101 #define ADV_PWR 91 /* current advanced power management level
102 in low byte, 0x40 in high byte. */
103 #define PSWD_CODE 92 /* master password revision code */
104 #define HWRST_RSLT 93 /* hardware reset result */
105 #define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
106 #define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
107 #define LBA_MID 101 /* bits are used, but addr 103 */
108 #define LBA_48_MSB 102 /* has been reserved for LBA in */
109 #define LBA_64_MSB 103 /* the future. */
110 #define RM_STAT 127 /* removable media status notification feature set support */
111 #define SECU_STATUS 128 /* security status */
112 #define CFA_PWR_MODE 160 /* CFA power mode 1 */
113 #define START_MEDIA 176 /* media serial number */
114 #define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
115 #define START_MANUF 196 /* media manufacturer I.D. */
116 #define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
117 #define INTEGRITY 255 /* integrity word */
119 /* bit definitions within the words */
120 /* -------------------------------- */
122 /* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
124 #define VALID_VAL 0x4000
125 /* many words are considered invalid if they are either all-0 or all-1 */
126 #define NOVAL_0 0x0000
127 #define NOVAL_1 0xffff
129 /* word 0: gen_config */
130 #define NOT_ATA 0x8000
131 #define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
132 #define MEDIA_REMOVABLE 0x0080
133 #define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
134 #define INCOMPLETE 0x0004
135 #define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
136 #define DRQ_RESPONSE_TIME 0x0060
137 #define DRQ_3MS_VAL 0x0000
138 #define DRQ_INTR_VAL 0x0020
139 #define DRQ_50US_VAL 0x0040
140 #define PKT_SIZE_SUPPORTED 0x0003
141 #define PKT_SIZE_12_VAL 0x0000
142 #define PKT_SIZE_16_VAL 0x0001
143 #define EQPT_TYPE 0x1f00
148 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
149 static const char * const pkt_str[] = {
150 "Direct-access device", /* word 0, bits 12-8 = 00 */
151 "Sequential-access device", /* word 0, bits 12-8 = 01 */
152 "Printer", /* word 0, bits 12-8 = 02 */
153 "Processor", /* word 0, bits 12-8 = 03 */
154 "Write-once device", /* word 0, bits 12-8 = 04 */
155 "CD-ROM", /* word 0, bits 12-8 = 05 */
156 "Scanner", /* word 0, bits 12-8 = 06 */
157 "Optical memory", /* word 0, bits 12-8 = 07 */
158 "Medium changer", /* word 0, bits 12-8 = 08 */
159 "Communications device", /* word 0, bits 12-8 = 09 */
160 "ACS-IT8 device", /* word 0, bits 12-8 = 0a */
161 "ACS-IT8 device", /* word 0, bits 12-8 = 0b */
162 "Array controller", /* word 0, bits 12-8 = 0c */
163 "Enclosure services", /* word 0, bits 12-8 = 0d */
164 "Reduced block command device", /* word 0, bits 12-8 = 0e */
165 "Optical card reader/writer", /* word 0, bits 12-8 = 0f */
166 "", /* word 0, bits 12-8 = 10 */
167 "", /* word 0, bits 12-8 = 11 */
168 "", /* word 0, bits 12-8 = 12 */
169 "", /* word 0, bits 12-8 = 13 */
170 "", /* word 0, bits 12-8 = 14 */
171 "", /* word 0, bits 12-8 = 15 */
172 "", /* word 0, bits 12-8 = 16 */
173 "", /* word 0, bits 12-8 = 17 */
174 "", /* word 0, bits 12-8 = 18 */
175 "", /* word 0, bits 12-8 = 19 */
176 "", /* word 0, bits 12-8 = 1a */
177 "", /* word 0, bits 12-8 = 1b */
178 "", /* word 0, bits 12-8 = 1c */
179 "", /* word 0, bits 12-8 = 1d */
180 "", /* word 0, bits 12-8 = 1e */
181 "Unknown", /* word 0, bits 12-8 = 1f */
184 static const char * const ata1_cfg_str[] = { /* word 0 in ATA-1 mode */
185 "Reserved", /* bit 0 */
186 "hard sectored", /* bit 1 */
187 "soft sectored", /* bit 2 */
188 "not MFM encoded ", /* bit 3 */
189 "head switch time > 15us", /* bit 4 */
190 "spindle motor control option", /* bit 5 */
191 "fixed drive", /* bit 6 */
192 "removable drive", /* bit 7 */
193 "disk xfer rate <= 5Mbs", /* bit 8 */
194 "disk xfer rate > 5Mbs, <= 10Mbs", /* bit 9 */
195 "disk xfer rate > 5Mbs", /* bit 10 */
196 "rotational speed tol.", /* bit 11 */
197 "data strobe offset option", /* bit 12 */
198 "track offset option", /* bit 13 */
199 "format speed tolerance gap reqd", /* bit 14 */
204 /* word 1: number of logical cylinders */
205 #define LCYLS_MAX 0x3fff /* maximum allowable value */
207 /* word 2: specific configuration
208 * (a) require SET FEATURES to spin-up
209 * (b) require spin-up to fully reply to IDENTIFY DEVICE
211 #define STBY_NID_VAL 0x37c8 /* (a) and (b) */
212 #define STBY_ID_VAL 0x738c /* (a) and not (b) */
213 #define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
214 #define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
216 /* words 47 & 59: sector_xfer_max & sector_xfer_cur */
217 #define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
218 #define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
220 /* word 49: capabilities 0 */
221 #define STD_STBY 0x2000 /* 1=standard values supported (ATA);
222 0=vendor specific values */
223 #define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
224 #define IORDY_OFF 0x0400 /* 1=may be disabled */
225 #define LBA_SUP 0x0200 /* 1=Logical Block Address support */
226 #define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
227 #define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
228 #define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
229 #define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
230 #define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
232 /* word 50: capabilities 1 */
233 #define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
235 /* words 51 & 52: PIO & DMA cycle times */
236 #define MODE 0xff00 /* the mode is in the MSBs */
238 /* word 53: whats_valid */
239 #define OK_W88 0x0004 /* the ultra_dma info is valid */
240 #define OK_W64_70 0x0002 /* see above for word descriptions */
241 #define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
243 /*word 63,88: dma_mode, ultra_dma_mode*/
244 #define MODE_MAX 7 /* bit definitions force udma <=7 (when
245 * udma >=8 comes out it'll have to be
246 * defined in a new dma_mode word!) */
248 /* word 64: PIO transfer modes */
249 #define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
250 #define PIO_MODE_MAX 8 /* but all 8 bits are defined */
252 /* word 75: queue_depth */
253 #define DEPTH_BITS 0x001f /* bits used for queue depth */
255 /* words 80-81: version numbers */
256 /* NOVAL_0 or NOVAL_1 means device does not report version */
258 /* word 81: minor version number */
259 #define MINOR_MAX 0x22
260 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
261 static const char *minor_str[MINOR_MAX+2] = { /* word 81 value: */
262 "Unspecified", /* 0x0000 */
263 "ATA-1 X3T9.2 781D prior to rev.4", /* 0x0001 */
264 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
265 "ATA-1 X3T9.2 781D rev.4", /* 0x0003 */
266 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
267 "ATA-2 X3T10 948D prior to rev.2k", /* 0x0005 */
268 "ATA-3 X3T10 2008D rev.1", /* 0x0006 */
269 "ATA-2 X3T10 948D rev.2k", /* 0x0007 */
270 "ATA-3 X3T10 2008D rev.0", /* 0x0008 */
271 "ATA-2 X3T10 948D rev.3", /* 0x0009 */
272 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
273 "ATA-3 X3T10 2008D rev.6", /* 0x000b */
274 "ATA-3 X3T13 2008D rev.7 and 7a", /* 0x000c */
275 "ATA/ATAPI-4 X3T13 1153D rev.6", /* 0x000d */
276 "ATA/ATAPI-4 T13 1153D rev.13", /* 0x000e */
277 "ATA/ATAPI-4 X3T13 1153D rev.7", /* 0x000f */
278 "ATA/ATAPI-4 T13 1153D rev.18", /* 0x0010 */
279 "ATA/ATAPI-4 T13 1153D rev.15", /* 0x0011 */
280 "ATA/ATAPI-4 published, ANSI INCITS 317-1998", /* 0x0012 */
281 "ATA/ATAPI-5 T13 1321D rev.3",
282 "ATA/ATAPI-4 T13 1153D rev.14", /* 0x0014 */
283 "ATA/ATAPI-5 T13 1321D rev.1", /* 0x0015 */
284 "ATA/ATAPI-5 published, ANSI INCITS 340-2000", /* 0x0016 */
285 "ATA/ATAPI-4 T13 1153D rev.17", /* 0x0017 */
286 "ATA/ATAPI-6 T13 1410D rev.0", /* 0x0018 */
287 "ATA/ATAPI-6 T13 1410D rev.3a", /* 0x0019 */
288 "ATA/ATAPI-7 T13 1532D rev.1", /* 0x001a */
289 "ATA/ATAPI-6 T13 1410D rev.2", /* 0x001b */
290 "ATA/ATAPI-6 T13 1410D rev.1", /* 0x001c */
291 "ATA/ATAPI-7 published, ANSI INCITS 397-2005", /* 0x001d */
292 "ATA/ATAPI-7 T13 1532D rev.0", /* 0x001e */
293 "Reserved" /* 0x001f */
294 "Reserved" /* 0x0020 */
295 "ATA/ATAPI-7 T13 1532D rev.4a", /* 0x0021 */
296 "ATA/ATAPI-6 published, ANSI INCITS 361-2002", /* 0x0022 */
297 "Reserved" /* 0x0023-0xfffe*/
300 static const char actual_ver[MINOR_MAX+2] = {
302 0, /* 0x0000 WARNING: */
303 1, /* 0x0001 WARNING: */
304 1, /* 0x0002 WARNING: */
305 1, /* 0x0003 WARNING: */
306 2, /* 0x0004 WARNING: This array */
307 2, /* 0x0005 WARNING: corresponds */
308 3, /* 0x0006 WARNING: *exactly* */
309 2, /* 0x0007 WARNING: to the ATA/ */
310 3, /* 0x0008 WARNING: ATAPI version */
311 2, /* 0x0009 WARNING: listed in */
312 3, /* 0x000a WARNING: the */
313 3, /* 0x000b WARNING: minor_str */
314 3, /* 0x000c WARNING: array */
315 4, /* 0x000d WARNING: above. */
316 4, /* 0x000e WARNING: */
317 4, /* 0x000f WARNING: if you change */
318 4, /* 0x0010 WARNING: that one, */
319 4, /* 0x0011 WARNING: change this one */
320 4, /* 0x0012 WARNING: too!!! */
321 5, /* 0x0013 WARNING: */
322 4, /* 0x0014 WARNING: */
323 5, /* 0x0015 WARNING: */
324 5, /* 0x0016 WARNING: */
325 4, /* 0x0017 WARNING: */
326 6, /* 0x0018 WARNING: */
327 6, /* 0x0019 WARNING: */
328 7, /* 0x001a WARNING: */
329 6, /* 0x001b WARNING: */
330 6, /* 0x001c WARNING: */
331 7, /* 0x001d WARNING: */
332 7, /* 0x001e WARNING: */
333 0, /* 0x001f WARNING: */
334 0, /* 0x0020 WARNING: */
335 7, /* 0x0021 WARNING: */
336 6, /* 0x0022 WARNING: */
337 0 /* 0x0023-0xfffe */
340 /* words 82-84: cmds/feats supported */
341 #define CMDS_W82 0x77ff /* word 82: defined command locations*/
342 #define CMDS_W83 0x3fff /* word 83: defined command locations*/
343 #define CMDS_W84 0x002f /* word 83: defined command locations*/
344 #define SUPPORT_48_BIT 0x0400
345 #define NUM_CMD_FEAT_STR 48
347 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
348 static const char * const cmd_feat_str[] = {
349 "", /* word 82 bit 15: obsolete */
350 "NOP cmd", /* word 82 bit 14 */
351 "READ BUFFER cmd", /* word 82 bit 13 */
352 "WRITE BUFFER cmd", /* word 82 bit 12 */
353 "", /* word 82 bit 11: obsolete */
354 "Host Protected Area feature set", /* word 82 bit 10 */
355 "DEVICE RESET cmd", /* word 82 bit 9 */
356 "SERVICE interrupt", /* word 82 bit 8 */
357 "Release interrupt", /* word 82 bit 7 */
358 "Look-ahead", /* word 82 bit 6 */
359 "Write cache", /* word 82 bit 5 */
360 "PACKET command feature set", /* word 82 bit 4 */
361 "Power Management feature set", /* word 82 bit 3 */
362 "Removable Media feature set", /* word 82 bit 2 */
363 "Security Mode feature set", /* word 82 bit 1 */
364 "SMART feature set", /* word 82 bit 0 */
366 "", /* word 83 bit 15: !valid bit */
367 "", /* word 83 bit 14: valid bit */
368 "FLUSH CACHE EXT cmd", /* word 83 bit 13 */
369 "Mandatory FLUSH CACHE cmd ", /* word 83 bit 12 */
370 "Device Configuration Overlay feature set ",
371 "48-bit Address feature set ", /* word 83 bit 10 */
373 "SET MAX security extension", /* word 83 bit 8 */
374 "Address Offset Reserved Area Boot", /* word 83 bit 7 */
375 "SET FEATURES subcommand required to spinup after power up",
376 "Power-Up In Standby feature set", /* word 83 bit 5 */
377 "Removable Media Status Notification feature set",
378 "Adv. Power Management feature set",/* word 83 bit 3 */
379 "CFA feature set", /* word 83 bit 2 */
380 "READ/WRITE DMA QUEUED", /* word 83 bit 1 */
381 "DOWNLOAD MICROCODE cmd", /* word 83 bit 0 */
383 "", /* word 84 bit 15: !valid bit */
384 "", /* word 84 bit 14: valid bit */
385 "", /* word 84 bit 13: reserved */
386 "", /* word 84 bit 12: reserved */
387 "", /* word 84 bit 11: reserved */
388 "", /* word 84 bit 10: reserved */
389 "", /* word 84 bit 9: reserved */
390 "", /* word 84 bit 8: reserved */
391 "", /* word 84 bit 7: reserved */
392 "", /* word 84 bit 6: reserved */
393 "General Purpose Logging feature set", /* word 84 bit 5 */
394 "", /* word 84 bit 4: reserved */
395 "Media Card Pass Through Command feature set ",
396 "Media serial number ", /* word 84 bit 2 */
397 "SMART self-test ", /* word 84 bit 1 */
398 "SMART error logging " /* word 84 bit 0 */
401 static void identify(uint16_t *id_supplied) ATTRIBUTE_NORETURN;
402 static void identify_from_stdin(void) ATTRIBUTE_NORETURN;
407 /* words 85-87: cmds/feats enabled */
408 /* use cmd_feat_str[] to display what commands and features have
409 * been enabled with words 85-87
412 /* words 89, 90, SECU ERASE TIME */
413 #define ERASE_BITS 0x00ff
415 /* word 92: master password revision */
416 /* NOVAL_0 or NOVAL_1 means no support for master password revision */
418 /* word 93: hw reset result */
419 #define CBLID 0x2000 /* CBLID status */
420 #define RST0 0x0001 /* 1=reset to device #0 */
421 #define DEV_DET 0x0006 /* how device num determined */
422 #define JUMPER_VAL 0x0002 /* device num determined by jumper */
423 #define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
425 /* word 127: removable media status notification feature set support */
426 #define RM_STAT_BITS 0x0003
427 #define RM_STAT_SUP 0x0001
429 /* word 128: security */
430 #define SECU_ENABLED 0x0002
431 #define SECU_LEVEL 0x0010
432 #define NUM_SECU_STR 6
433 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
434 static const char * const secu_str[] = {
435 "supported", /* word 128, bit 0 */
436 "enabled", /* word 128, bit 1 */
437 "locked", /* word 128, bit 2 */
438 "frozen", /* word 128, bit 3 */
439 "expired: security count", /* word 128, bit 4 */
440 "supported: enhanced erase" /* word 128, bit 5 */
444 /* word 160: CFA power mode */
445 #define VALID_W160 0x8000 /* 1=word valid */
446 #define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
447 #define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
448 #define MAX_AMPS 0x0fff /* value = max current in ma */
450 /* word 255: integrity */
451 #define SIG 0x00ff /* signature location */
452 #define SIG_VAL 0x00A5 /* signature value */
455 #define TIMING_BUF_MB 1
456 #define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
457 #define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
458 #define BUFCACHE_FACTOR 2
460 #undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
462 /* Busybox messages and functions */
463 static int bb_ioctl(int fd, int request, void *argp, const char *string)
465 int e = ioctl(fd, request, argp);
467 bb_perror_msg(" %s", string);
471 static int bb_ioctl_alt(int fd, int cmd, unsigned char *args, int alt, const char *string)
473 if (!ioctl(fd, cmd, args))
476 return bb_ioctl(fd, cmd, args, string);
479 static void on_off(unsigned int value);
481 static void print_flag_on_off(unsigned long get_arg, const char *s, unsigned long arg)
486 printf(" setting %s to %ld", s, arg);
491 static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
494 if (ioctl(fd, request, &argp) != 0)
495 bb_perror_msg(" %s", string);
498 printf(" %s\t= %2ld", str, (unsigned long) argp);
499 on_off((unsigned long) argp);
503 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
504 static void print_ascii(uint16_t *p, uint8_t length);
506 static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
510 printf("\t%-20s",string);
511 print_ascii(&val[i], n);
515 /* end of busybox specific stuff */
517 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
518 static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
523 for (ii = 0; ii <= MODE_MAX; ii++)
525 if (mode_sel & 0x0001)
527 printf("*%cdma%u ",cc,ii);
532 else if (mode_sup & 0x0001)
533 printf("%cdma%u ",cc,ii);
541 static void print_ascii(uint16_t *p, uint8_t length) {
545 /* find first non-space & print it */
546 for (ii = 0; ii< length; ii++)
548 if (((char) 0x00ff&((*p)>>8)) != ' ')
550 if ((cl = (char) 0x00ff&(*p)) != ' ')
552 if (cl != '\0') printf("%c",cl);
560 for (; ii< length; ii++)
563 break; /* some older devices have NULLs */
564 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
570 /* identify() is the only extern function used across two source files. The
571 others, though, were declared in hdparm.c with global scope; since other
572 functions in that file have static (file) scope, I assume the difference is
574 static void identify(uint16_t *id_supplied)
577 uint16_t *val, ii, jj, kk;
578 uint16_t like_std = 1, std = 0, min_std = 0xffff;
579 uint16_t dev = NO_DEV, eqpt = NO_DEV;
580 uint8_t have_mode = 0, err_dma = 0;
582 uint32_t ll, mm, nn, oo;
583 uint64_t bbbig; /* (:) */
587 swab(id_supplied, buf, sizeof(buf));
589 } else val = id_supplied;
593 /* check if we recognise the device type */
595 if(!(val[GEN_CONFIG] & NOT_ATA))
598 printf("ATA device, with ");
600 else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
604 printf("CompactFlash ATA device, with ");
606 else if(!(val[GEN_CONFIG] & NOT_ATAPI))
609 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
610 printf("ATAPI %s, with ", pkt_str[eqpt]);
614 /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
615 bb_error_msg_and_die("Unknown device type");
617 printf("%sremovable media\n", !(val[GEN_CONFIG] & MEDIA_REMOVABLE) ? "non-" : "");
618 /* Info from the specific configuration word says whether or not the
619 * ID command completed correctly. It is only defined, however in
620 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
621 * standards. Since the values allowed for this word are extremely
622 * specific, it should be safe to check it now, even though we don't
623 * know yet what standard this device is using.
625 if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
626 (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
629 if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL))
630 printf("powers-up in standby; SET FEATURES subcmd spins-up.\n");
631 if(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE))
632 printf("\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
635 /* output the model and serial numbers and the fw revision */
636 xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
637 xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
638 xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
639 xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
640 xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
642 /* major & minor standards version number (Note: these words were not
643 * defined until ATA-3 & the CDROM std uses different words.) */
644 printf("Standards:");
647 if (val[MINOR] && (val[MINOR] <= MINOR_MAX))
649 if(like_std < 3) like_std = 3;
650 std = actual_ver[val[MINOR]];
651 if(std) printf("\n\tUsed: %s ",minor_str[val[MINOR]]);
654 /* looks like when they up-issue the std, they obsolete one;
655 * thus, only the newest 4 issues need be supported. (That's
656 * what "kk" and "min_std" are all about.) */
657 if (val[MAJOR] && (val[MAJOR] !=NOVAL_1))
659 printf("\n\tSupported: ");
660 jj = val[MAJOR] << 1;
661 kk = like_std >4 ? like_std-4: 0;
662 for (ii = 14; (ii >0)&&(ii>kk); ii--)
670 kk = like_std >4 ? like_std-4: 0;
672 if(min_std > ii) min_std = ii;
676 if(like_std < 3) like_std = 3;
678 /* Figure out what standard the device is using if it hasn't told
679 * us. If we know the std, check if the device is using any of
680 * the words from the next level up. It happens.
682 if(like_std < std) like_std = std;
684 if (((std == 5) || (!std && (like_std < 6))) &&
685 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
686 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
687 ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
688 ( val[CMDS_SUPP_2] & CMDS_W84) ) ) )
692 else if (((std == 4) || (!std && (like_std < 5))) &&
693 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
694 (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
695 ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
696 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
700 else if (((std == 3) || (!std && (like_std < 4))) &&
701 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
702 ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
703 (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
704 (( val[CAPAB_1] & VALID) == VALID_VAL) ||
705 (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
706 (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
710 else if (((std == 2) || (!std && (like_std < 3))) &&
711 ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
715 else if (((std == 1) || (!std && (like_std < 2))) &&
716 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
717 (val[WHATS_VALID] & OK_W64_70)) )
722 printf("\n\tLikely used: %u\n",like_std);
723 else if (like_std > std)
724 printf("& some of %u\n",like_std);
730 /* TBD: do CDROM stuff more thoroughly. For now... */
732 if (val[CDR_MINOR] == 9)
735 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
737 if (val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
740 printf("\n\tSupported: CD-ROM ATAPI");
741 jj = val[CDR_MAJOR] >> 1;
742 for (ii = 1; ii <15; ii++)
744 if(jj & 0x0001) printf("-%u ", ii);
748 printf("%s\n", (!kk) ? "\n\tLikely used CD-ROM ATAPI-1" : "" );
749 /* the cdrom stuff is more like ATA-2 than anything else, so: */
753 if (min_std == 0xffff)
754 min_std = like_std > 4 ? like_std - 3 : 1;
756 printf("Configuration:\n");
757 /* more info from the general configuration word */
758 if ((eqpt != CDROM) && (like_std == 1))
760 jj = val[GEN_CONFIG] >> 1;
761 for (ii = 1; ii < 15; ii++)
763 if(jj & 0x0001) printf("\t%s\n",ata1_cfg_str[ii]);
767 if (dev == ATAPI_DEV)
769 if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_3MS_VAL)
771 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_INTR_VAL)
772 strng = "<=10ms with INTRQ";
773 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_50US_VAL)
777 printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */
779 if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_12_VAL)
781 else if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_16_VAL)
789 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
790 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
792 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
793 printf("\tCHS addressing not supported\n");
796 jj = val[WHATS_VALID] & OK_W54_58;
797 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",
798 val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
800 if ((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES]))
801 printf("\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
805 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
808 /* check Endian of capacity bytes */
809 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
810 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
811 if (abs(mm - nn) > abs(oo - nn))
814 printf("\tCHS current addressable sectors:%11u\n",mm);
818 printf("\tLBA user addressable sectors:%11u\n",ll);
819 if ( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
820 (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
822 bbbig = (uint64_t)val[LBA_64_MSB] << 48 |
823 (uint64_t)val[LBA_48_MSB] << 32 |
824 (uint64_t)val[LBA_MID] << 16 |
826 printf("\tLBA48 user addressable sectors:%11"PRIu64"\n",bbbig);
830 bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
831 printf("\tdevice size with M = 1024*1024: %11"PRIu64" MBytes\n",bbbig>>11);
832 bbbig = (bbbig<<9)/1000000;
833 printf("\tdevice size with M = 1000*1000: %11"PRIu64" MBytes ",bbbig);
836 printf("(%"PRIu64" GB)\n", bbbig/1000);
841 /* hw support of commands (capabilities) */
842 printf("Capabilities:\n\t");
844 if (dev == ATAPI_DEV)
846 if (eqpt != CDROM && (val[CAPAB_0] & CMD_Q_SUP)) printf("Cmd queuing, ");
847 if (val[CAPAB_0] & OVLP_SUP) printf("Cmd overlap, ");
849 if (val[CAPAB_0] & LBA_SUP) printf("LBA, ");
853 printf("IORDY%s(can%s be disabled)\n",
854 !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "",
855 (val[CAPAB_0] & IORDY_OFF) ? "" :"not");
858 printf("no IORDY\n");
860 if ((like_std == 1) && val[BUF_TYPE])
862 printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE],
863 (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector",
864 (val[BUF_TYPE] > 2) ? " with read caching ability" : "");
867 if ((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
869 printf("\tBuffer size: %.1fkB\n",(float)val[BUFFER__SIZE]/2);
871 if ((min_std < 4) && (val[RW_LONG]))
873 printf("\tbytes avail on r/w long: %u\n",val[RW_LONG]);
875 if ((eqpt != CDROM) && (like_std > 3))
877 printf("\tQueue depth: %u\n",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
883 printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
886 printf("\tStandby timer values: spec'd by %s", (val[CAPAB_0] & STD_STBY) ? "Standard" : "Vendor");
887 if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
888 printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
892 printf("\tR/W multiple sector transfer: ");
893 if ((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
894 printf("not supported\n");
897 printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
898 if (val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
899 printf("%u\n", val[SECTOR_XFER_CUR] & SECTOR_XFER);
903 if ((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
905 /* We print out elsewhere whether the APM feature is enabled or
906 not. If it's not enabled, let's not repeat the info; just print
908 printf("\tAdvancedPM level: ");
909 if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
911 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
912 printf("%u (0x%x)\n", apm_level, apm_level);
915 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
917 if (like_std > 5 && val[ACOUSTIC]) {
918 printf("\tRecommended acoustic management value: %u, current value: %u\n",
919 (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
925 if (eqpt != CDROM && (val[CAPAB_0] & SWRST_REQ))
926 printf("\tATA sw reset required\n");
928 if (val[PKT_REL] || val[SVC_NBSY])
930 printf("\tOverlap support:");
931 if (val[PKT_REL]) printf(" %uus to release bus.",val[PKT_REL]);
932 if (val[SVC_NBSY]) printf(" %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
937 /* DMA stuff. Check that only one DMA mode is selected. */
939 if (!(val[CAPAB_0] & DMA_SUP))
940 printf("not supported\n");
943 if (val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
944 printf(" sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
947 jj = val[SINGLE_DMA];
948 kk = val[SINGLE_DMA] >> 8;
949 err_dma += mode_loop(jj,kk,'s',&have_mode);
954 kk = val[MULTI_DMA] >> 8;
955 err_dma += mode_loop(jj,kk,'m',&have_mode);
957 if ((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
960 kk = val[ULTRA_DMA] >> 8;
961 err_dma += mode_loop(jj,kk,'u',&have_mode);
963 if (err_dma || !have_mode) printf("(?)");
966 if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
967 printf("\t\tInterleaved DMA support\n");
969 if ((val[WHATS_VALID] & OK_W64_70) &&
970 (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
972 printf("\t\tCycle time:");
973 if (val[DMA_TIME_MIN]) printf(" min=%uns",val[DMA_TIME_MIN]);
974 if (val[DMA_TIME_NORM]) printf(" recommended=%uns",val[DMA_TIME_NORM]);
979 /* Programmed IO stuff */
981 /* If a drive supports mode n (e.g. 3), it also supports all modes less
982 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
983 if ((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
985 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
986 for (ii = 0; ii <= PIO_MODE_MAX ; ii++)
988 if (jj & 0x0001) printf("pio%d ",ii);
993 else if (((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
995 for (ii = 0; ii <= val[PIO_MODE]>>8; ii++)
1000 printf("unknown\n");
1002 if (val[WHATS_VALID] & OK_W64_70)
1004 if (val[PIO_NO_FLOW] || val[PIO_FLOW])
1006 printf("\t\tCycle time:");
1007 if (val[PIO_NO_FLOW]) printf(" no flow control=%uns", val[PIO_NO_FLOW]);
1008 if (val[PIO_FLOW]) printf(" IORDY flow control=%uns", val[PIO_FLOW]);
1013 if ((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
1015 printf("Commands/features:\n\tEnabled\tSupported:\n");
1016 jj = val[CMDS_SUPP_0];
1017 kk = val[CMDS_EN_0];
1018 for (ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
1020 if ((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
1022 printf("\t%s\t%s\n", (kk & 0x8000) ? " *" : "", cmd_feat_str[ii]);
1027 jj = val[CMDS_SUPP_0+1+(ii/16)];
1028 kk = val[CMDS_EN_0+1+(ii/16)];
1032 if ((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
1037 /* Removable Media Status Notification feature set */
1038 if((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
1039 printf("\t%s supported\n", cmd_feat_str[27]);
1043 if ((eqpt != CDROM) && (like_std > 3) &&
1044 (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1046 printf("Security:\n");
1047 if (val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
1048 printf("\tMaster password revision code = %u\n",val[PSWD_CODE]);
1049 jj = val[SECU_STATUS];
1052 for (ii = 0; ii < NUM_SECU_STR; ii++)
1054 printf("\t%s\t%s\n", (!(jj & 0x0001)) ? "not" : "", secu_str[ii]);
1057 if (val[SECU_STATUS] & SECU_ENABLED)
1059 printf("\tSecurity level %s\n", (val[SECU_STATUS] & SECU_LEVEL) ? "maximum" : "high");
1062 jj = val[ERASE_TIME] & ERASE_BITS;
1063 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
1067 if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, "");
1068 if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED ");
1074 jj = val[HWRST_RSLT];
1075 if ((jj & VALID) == VALID_VAL)
1077 if (!(oo = (jj & RST0)))
1079 if ((jj & DEV_DET) == JUMPER_VAL)
1080 strng = " determined by the jumper";
1081 else if ((jj & DEV_DET) == CSEL_VAL)
1082 strng = " determined by CSEL";
1085 printf("HW reset results:\n\tCBLID- %s Vih\n\tDevice num = %i%s\n",
1086 (val[HWRST_RSLT] & CBLID) ? "above" : "below", !(oo), strng);
1089 /* more stuff from std 5 */
1090 if ((like_std > 4) && (eqpt != CDROM))
1092 if (val[CFA_PWR_MODE] & VALID_W160)
1094 printf("CFA power mode 1:\n\t%s%s\n", (val[CFA_PWR_MODE] & PWR_MODE_OFF) ? "disabled" : "enabled",
1095 (val[CFA_PWR_MODE] & PWR_MODE_REQ) ? " and required by some commands" : "");
1097 if (val[CFA_PWR_MODE] & MAX_AMPS) printf("\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
1099 if ((val[INTEGRITY] & SIG) == SIG_VAL)
1101 printf("Checksum: %scorrect\n", chksum ? "in" : "");
1109 static int get_identity, get_geom;
1110 static int do_flush;
1111 static int do_ctimings, do_timings;
1112 static unsigned long set_readahead, get_readahead, Xreadahead;
1113 static unsigned long set_readonly, get_readonly, readonly;
1114 static unsigned long set_unmask, get_unmask, unmask;
1115 static unsigned long set_mult, get_mult, mult;
1116 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1117 static unsigned long set_dma, get_dma, dma;
1119 static unsigned long set_dma_q, get_dma_q, dma_q;
1120 static unsigned long set_nowerr, get_nowerr, nowerr;
1121 static unsigned long set_keep, get_keep, keep;
1122 static unsigned long set_io32bit, get_io32bit, io32bit;
1123 static unsigned long set_piomode, noisy_piomode;
1125 #ifdef HDIO_DRIVE_CMD
1126 static unsigned long set_dkeep, get_dkeep, dkeep;
1127 static unsigned long set_standby, get_standby, standby_requested;
1128 static unsigned long set_xfermode, get_xfermode;
1129 static int xfermode_requested;
1130 static unsigned long set_lookahead, get_lookahead, lookahead;
1131 static unsigned long set_prefetch, get_prefetch, prefetch;
1132 static unsigned long set_defects, get_defects, defects;
1133 static unsigned long set_wcache, get_wcache, wcache;
1134 static unsigned long set_doorlock, get_doorlock, doorlock;
1135 static unsigned long set_seagate, get_seagate;
1136 static unsigned long set_standbynow, get_standbynow;
1137 static unsigned long set_sleepnow, get_sleepnow;
1138 static unsigned long get_powermode;
1139 static unsigned long set_apmmode, get_apmmode, apmmode;
1141 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1142 static int get_IDentity;
1144 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1145 static unsigned long unregister_hwif;
1146 static unsigned long hwif;
1148 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1149 static unsigned long scan_hwif;
1150 static unsigned long hwif_data;
1151 static unsigned long hwif_ctrl;
1152 static unsigned long hwif_irq;
1154 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1155 static unsigned long set_busstate, get_busstate, busstate;
1157 static int reread_partn;
1159 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1160 static int perform_reset;
1161 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1162 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1163 static unsigned long perform_tristate, tristate;
1164 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1166 // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1167 // then the HDIO_GET_IDENTITY only returned 142 bytes.
1168 // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1169 // and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1170 // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1171 // (which they should, but they should just return -EINVAL).
1173 // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1174 // On a really old system, it will not, and we will be confused.
1177 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1178 static const char * const cfg_str[] =
1179 { "", "HardSect", "SoftSect", "NotMFM",
1180 "HdSw>15uSec", "SpinMotCtl", "Fixed", "Removeable",
1181 "DTR<=5Mbs", "DTR>5Mbs", "DTR>10Mbs", "RotSpdTol>.5%",
1182 "dStbOff", "TrkOff", "FmtGapReq", "nonMagnetic"
1185 static const char * const BuffType[] = {"Unknown", "1Sect", "DualPort", "DualPortCache"};
1187 static void dump_identity(const struct hd_driveid *id)
1190 const unsigned short int *id_regs= (const void*) id;
1192 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1193 id->model, id->fw_rev, id->serial_no);
1194 for (i=0; i<=15; i++) {
1195 if (id->config & (1<<i))
1196 printf(" %s", cfg_str[i]);
1198 printf( " }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n"
1199 " BuffType=(%u) %s, BuffSize=%ukB, MaxMultSect=%u",
1200 id->cyls, id->heads, id->sectors, id->track_bytes,
1201 id->sector_bytes, id->ecc_bytes,
1202 id->buf_type, BuffType[(id->buf_type > 3) ? 0 : id->buf_type],
1203 id->buf_size/2, id->max_multsect);
1204 if (id->max_multsect)
1206 printf(", MultSect=");
1207 if (!(id->multsect_valid&1))
1208 printf("?%u?", id->multsect);
1209 else if (id->multsect)
1210 printf("%u", id->multsect);
1216 if(!(id->field_valid&1))
1217 printf(" (maybe):");
1219 printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
1222 (long unsigned int)(id->cur_capacity0 << 16) | id->cur_capacity1 :
1223 (long unsigned int)(id->cur_capacity1 << 16) | id->cur_capacity0,
1224 ((id->capability&2) == 0) ? "no" : "yes");
1226 if (id->capability&2)
1227 printf(", LBAsects=%u", id->lba_capacity);
1229 printf("\n IORDY=%s", (id->capability&8) ? (id->capability&4) ? "on/off" : "yes" : "no");
1231 if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&2)
1232 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1234 if ((id->capability&1) && (id->field_valid&2))
1235 printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1237 printf("\n PIO modes: ");
1241 if (id->tPIO >= 1) printf("pio1 ");
1242 if (id->tPIO >= 2) printf("pio2 ");
1244 if (id->field_valid&2)
1246 if (id->eide_pio_modes & 1) printf("pio3 ");
1247 if (id->eide_pio_modes & 2) printf("pio4 ");
1248 if (id->eide_pio_modes &~3) printf("pio? ");
1250 if (id->capability&1)
1252 if (id->dma_1word | id->dma_mword)
1254 printf("\n DMA modes: ");
1255 if (id->dma_1word & 0x100) printf("*");
1256 if (id->dma_1word & 1) printf("sdma0 ");
1257 if (id->dma_1word & 0x200) printf("*");
1258 if (id->dma_1word & 2) printf("sdma1 ");
1259 if (id->dma_1word & 0x400) printf("*");
1260 if (id->dma_1word & 4) printf("sdma2 ");
1261 if (id->dma_1word & 0xf800) printf("*");
1262 if (id->dma_1word & 0xf8) printf("sdma? ");
1263 if (id->dma_mword & 0x100) printf("*");
1264 if (id->dma_mword & 1) printf("mdma0 ");
1265 if (id->dma_mword & 0x200) printf("*");
1266 if (id->dma_mword & 2) printf("mdma1 ");
1267 if (id->dma_mword & 0x400) printf("*");
1268 if (id->dma_mword & 4) printf("mdma2 ");
1269 if (id->dma_mword & 0xf800) printf("*");
1270 if (id->dma_mword & 0xf8) printf("mdma? ");
1273 if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&4)
1275 printf("\n UDMA modes: ");
1276 if (id->dma_ultra & 0x100) printf("*");
1277 if (id->dma_ultra & 0x001) printf("udma0 ");
1278 if (id->dma_ultra & 0x200) printf("*");
1279 if (id->dma_ultra & 0x002) printf("udma1 ");
1280 if (id->dma_ultra & 0x400) printf("*");
1281 if (id->dma_ultra & 0x004) printf("udma2 ");
1282 #ifdef __NEW_HD_DRIVE_ID
1283 if (id->hw_config & 0x2000)
1285 #else /* !__NEW_HD_DRIVE_ID */
1286 if (id->word93 & 0x2000)
1288 #endif /* __NEW_HD_DRIVE_ID */
1289 if (id->dma_ultra & 0x0800) printf("*");
1290 if (id->dma_ultra & 0x0008) printf("udma3 ");
1291 if (id->dma_ultra & 0x1000) printf("*");
1292 if (id->dma_ultra & 0x0010) printf("udma4 ");
1293 if (id->dma_ultra & 0x2000) printf("*");
1294 if (id->dma_ultra & 0x0020) printf("udma5 ");
1295 if (id->dma_ultra & 0x4000) printf("*");
1296 if (id->dma_ultra & 0x0040) printf("udma6 ");
1297 if (id->dma_ultra & 0x8000) printf("*");
1298 if (id->dma_ultra & 0x0080) printf("udma7 ");
1301 printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
1302 if (id_regs[83] & 8)
1304 if (!(id_regs[86]&8))
1305 printf(": disabled (255)");
1306 else if ((id_regs[91]&0xFF00)!=0x4000)
1307 printf(": unknown setting");
1309 printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1311 if (id_regs[82]&0x20)
1312 printf(" WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
1313 #ifdef __NEW_HD_DRIVE_ID
1314 if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
1316 printf("\n Drive conforms to: %s: ", (id->minor_rev_num <= 31) ? minor_str[id->minor_rev_num] : "Unknown");
1317 if (id->major_rev_num != 0x0000 && /* NOVAL_0 */
1318 id->major_rev_num != 0xFFFF) { /* NOVAL_1 */
1319 for (i=0; i <= 15; i++) {
1320 if (id->major_rev_num & (1<<i))
1321 printf(" ATA/ATAPI-%u", i);
1325 #endif /* __NEW_HD_DRIVE_ID */
1326 printf("\n\n * current active mode\n\n");
1330 static void flush_buffer_cache(int fd)
1332 fsync(fd); /* flush buffers */
1333 bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
1334 #ifdef HDIO_DRIVE_CMD
1336 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
1337 bb_perror_msg("HDIO_DRIVE_CMD");
1341 static int seek_to_zero(int fd)
1343 if (lseek(fd, (off_t) 0, SEEK_SET))
1348 static int read_big_block(int fd, char *buf)
1352 if ((i = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES) {
1353 bb_error_msg("read(%d bytes) failed (rc=%d)", TIMING_BUF_BYTES, i);
1356 /* access all sectors of buf to ensure the read fully completed */
1357 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1362 static void print_timing(int t, double e)
1364 if (t >= e) /* more than 1MB/s */
1365 printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e, 'M');
1367 printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e * 1024, 'k');
1370 static int do_blkgetsize (int fd, unsigned long long *blksize64)
1373 unsigned int blksize32 = 0;
1375 if (0 == ioctl(fd, BLKGETSIZE64, blksize64)) { // returns bytes
1379 rc = ioctl(fd, BLKGETSIZE, &blksize32); // returns sectors
1381 bb_perror_msg("BLKGETSIZE");
1382 *blksize64 = blksize32;
1386 static void do_time(int flag, int fd)
1389 flag = 1 time_device
1392 struct itimerval e1, e2;
1393 double elapsed, elapsed2;
1394 unsigned int max_iterations = 1024, total_MB, iterations;
1395 unsigned long long blksize;
1396 RESERVE_CONFIG_BUFFER(buf, TIMING_BUF_BYTES);
1398 if (mlock(buf, TIMING_BUF_BYTES)) {
1399 bb_perror_msg("mlock");
1403 if (0 == do_blkgetsize(fd, &blksize)) {
1404 max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB;
1407 /* Clear out the device request queues & give them time to complete */
1411 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1413 if (flag == 0) /* Time cache */
1415 if (seek_to_zero (fd)) return;
1416 if (read_big_block (fd, buf)) return;
1417 printf(" Timing cached reads: ");
1420 /* Now do the timing */
1422 getitimer(ITIMER_REAL, &e1);
1425 if (seek_to_zero (fd) || read_big_block (fd, buf))
1427 getitimer(ITIMER_REAL, &e2);
1428 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1429 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1430 } while (elapsed < 2.0);
1431 total_MB = iterations * TIMING_BUF_MB;
1433 /* Now remove the lseek() and getitimer() overheads from the elapsed time */
1434 getitimer(ITIMER_REAL, &e1);
1436 if (seek_to_zero (fd))
1438 getitimer(ITIMER_REAL, &e2);
1439 elapsed2 = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1440 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1441 } while (--iterations);
1443 elapsed -= elapsed2;
1444 print_timing(BUFCACHE_FACTOR * total_MB, elapsed);
1445 flush_buffer_cache(fd);
1448 else /* Time device */
1450 printf(" Timing buffered disk reads: ");
1453 * getitimer() is used rather than gettimeofday() because
1454 * it is much more consistent (on my machine, at least).
1456 /* Now do the timings for real */
1458 getitimer(ITIMER_REAL, &e1);
1461 if (read_big_block (fd, buf))
1463 getitimer(ITIMER_REAL, &e2);
1464 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1465 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1466 } while (elapsed < 3.0 && iterations < max_iterations);
1468 total_MB = iterations * TIMING_BUF_MB;
1469 print_timing(total_MB, elapsed);
1472 munlock(buf, TIMING_BUF_BYTES);
1474 RELEASE_CONFIG_BUFFER(buf);
1477 static void on_off (unsigned int value)
1479 printf(value ? " (on)\n" : " (off)\n");
1482 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1483 static void bus_state_value(unsigned int value)
1485 if (value == BUSSTATE_ON)
1487 else if (value == BUSSTATE_OFF)
1489 else if (value == BUSSTATE_TRISTATE)
1490 printf(" (tristate)\n");
1492 printf(" (unknown: %d)\n", value);
1496 #ifdef HDIO_DRIVE_CMD
1497 static void interpret_standby(unsigned int standby)
1504 else if (standby == 252)
1505 printf("21 minutes");
1506 else if (standby == 253)
1507 printf("vendor-specific");
1508 else if (standby == 254)
1510 else if (standby == 255)
1511 printf("21 minutes + 15 seconds");
1513 if (standby <= 240) {
1515 printf("%u minutes + %u seconds", t / 60, t % 60);
1516 } else if (standby <= 251) {
1517 t = (standby - 240) * 30;
1518 printf("%u hours + %u minutes", t / 60, t % 60);
1520 printf("illegal value");
1525 struct xfermode_entry {
1530 static const struct xfermode_entry xfermode_table[] = {
1566 static int translate_xfermode(char * name)
1568 const struct xfermode_entry *tmp;
1573 for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1575 if (!strcmp(name, tmp->name))
1579 val = strtol(name, &endptr, 10);
1580 if (*endptr == '\0')
1586 static void interpret_xfermode(unsigned int xfermode)
1590 printf("default PIO mode");
1591 else if (xfermode == 1)
1592 printf("default PIO mode, disable IORDY");
1593 else if (xfermode >= 8 && xfermode <= 15)
1594 printf("PIO flow control mode%u", xfermode-8);
1595 else if (xfermode >= 16 && xfermode <= 23)
1596 printf("singleword DMA mode%u", xfermode-16);
1597 else if (xfermode >= 32 && xfermode <= 39)
1598 printf("multiword DMA mode%u", xfermode-32);
1599 else if (xfermode >= 64 && xfermode <= 71)
1600 printf("UltraDMA mode%u", xfermode-64);
1605 #endif /* HDIO_DRIVE_CMD */
1607 static void print_flag(unsigned long flag, char *s, unsigned long value)
1610 printf(" setting %s to %ld\n", s, value);
1613 static void process_dev(char *devname)
1616 static long parm, multcount;
1617 #ifndef HDIO_DRIVE_CMD
1618 int force_operation = 0;
1620 /* Please restore args[n] to these values after each ioctl
1621 except for args[2] */
1622 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
1623 const char *fmt = " %s\t= %2ld";
1625 fd = bb_xopen(devname, O_RDONLY|O_NONBLOCK);
1626 printf("\n%s:\n", devname);
1630 print_flag(get_readahead,"fs readahead", Xreadahead);
1631 bb_ioctl(fd, BLKRASET,(int *)Xreadahead,"BLKRASET");
1633 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1634 if (unregister_hwif)
1636 printf(" attempting to unregister hwif#%lu\n", hwif);
1637 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)(unsigned long)hwif,"HDIO_UNREGISTER_HWIF");
1640 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1643 printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq);
1644 args[0] = hwif_data;
1645 args[1] = hwif_ctrl;
1647 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
1648 args[0] = WIN_SETFEATURES;
1656 printf(" attempting to ");
1658 printf("auto-tune PIO mode\n");
1659 else if (piomode < 100)
1660 printf("set PIO mode to %d\n", piomode);
1661 else if (piomode < 200)
1662 printf("set MDMA mode to %d\n", (piomode-100));
1664 printf("set UDMA mode to %d\n", (piomode-200));
1666 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode, "HDIO_SET_PIO_MODE");
1670 print_flag(get_io32bit,"32-bit IO_support flag", io32bit);
1671 bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
1675 print_flag(get_mult, "multcount", mult);
1676 #ifdef HDIO_DRIVE_CMD
1677 bb_ioctl(fd, HDIO_SET_MULTCOUNT, &mult, "HDIO_SET_MULTCOUNT");
1679 force_operation |= (!bb_ioctl(fd, HDIO_SET_MULTCOUNT, &mult, "HDIO_SET_MULTCOUNT"));
1684 print_flag_on_off(get_readonly,"readonly", readonly);
1685 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
1689 print_flag_on_off(get_unmask,"unmaskirq", unmask);
1690 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
1692 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1695 print_flag_on_off(get_dma, "using_dma", dma);
1696 bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
1698 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
1701 print_flag_on_off(get_dma_q,"DMA queue_depth", dma_q);
1702 bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
1706 print_flag_on_off(get_nowerr,"nowerr", nowerr);
1707 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
1711 print_flag_on_off(get_keep,"keep_settings", keep);
1712 bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
1714 #ifdef HDIO_DRIVE_CMD
1717 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
1719 print_flag_on_off(get_doorlock,"drive doorlock", doorlock);
1720 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
1721 args[0] = WIN_SETFEATURES;
1725 /* lock/unlock the drive's "feature" settings */
1726 print_flag_on_off(get_dkeep,"drive keep features", dkeep);
1727 args[2] = dkeep ? 0x66 : 0xcc;
1728 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
1732 args[2] = defects ? 0x04 : 0x84;
1733 print_flag(get_defects,"drive defect-mgmt", defects);
1734 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
1740 print_flag(get_prefetch,"drive prefetch", prefetch);
1741 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
1746 args[1] = xfermode_requested;
1750 print_flag(1,"xfermode", xfermode_requested);
1751 interpret_xfermode(xfermode_requested);
1753 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
1758 args[2] = lookahead ? 0xaa : 0x55;
1759 print_flag_on_off(get_lookahead,"drive read-lookahead", lookahead);
1760 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
1764 args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */; /* feature register */
1765 args[1] = apmmode; /* sector count register 1-255 */
1767 printf(" setting APM level to %s 0x%02lX (%ld)\n", (apmmode == 255) ? "disabled" : "", apmmode, apmmode);
1768 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
1773 #ifdef DO_FLUSHCACHE
1774 #ifndef WIN_FLUSHCACHE
1775 #define WIN_FLUSHCACHE 0xe7
1777 static unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
1778 #endif /* DO_FLUSHCACHE */
1779 args[2] = wcache ? 0x02 : 0x82;
1780 print_flag_on_off(get_wcache,"drive write-caching", wcache);
1781 #ifdef DO_FLUSHCACHE
1783 bb_ioctl(fd, HDIO_DRIVE_CMD, &flushcache, "HDIO_DRIVE_CMD(flushcache)");
1784 #endif /* DO_FLUSHCACHE */
1785 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
1786 #ifdef DO_FLUSHCACHE
1788 bb_ioctl(fd, HDIO_DRIVE_CMD, &flushcache, "HDIO_DRIVE_CMD(flushcache)");
1789 #endif /* DO_FLUSHCACHE */
1792 /* In code below, we do not preserve args[0], but the rest
1793 is preserved, including args[2] */
1798 #ifndef WIN_STANDBYNOW1
1799 #define WIN_STANDBYNOW1 0xE0
1801 #ifndef WIN_STANDBYNOW2
1802 #define WIN_STANDBYNOW2 0x94
1804 if (get_standbynow) printf(" issuing standby command\n");
1805 args[0] = WIN_STANDBYNOW1;
1806 bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2, "HDIO_DRIVE_CMD(standby)");
1810 #ifndef WIN_SLEEPNOW1
1811 #define WIN_SLEEPNOW1 0xE6
1813 #ifndef WIN_SLEEPNOW2
1814 #define WIN_SLEEPNOW2 0x99
1816 if (get_sleepnow) printf(" issuing sleep command\n");
1817 args[0] = WIN_SLEEPNOW1;
1818 bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2, "HDIO_DRIVE_CMD(sleep)");
1823 if (get_seagate) printf(" disabling Seagate auto powersaving mode\n");
1824 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
1828 args[0] = WIN_SETIDLE1;
1829 args[1] = standby_requested;
1832 print_flag(1,"standby", standby_requested);
1833 interpret_standby(standby_requested);
1835 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
1838 #else /* HDIO_DRIVE_CMD */
1839 if (force_operation)
1842 flush_buffer_cache(fd);
1843 if (-1 == read(fd, buf, sizeof(buf)))
1844 bb_perror_msg("read(%d bytes) failed (rc=%d)", sizeof(buf), -1);
1846 #endif /* HDIO_DRIVE_CMD */
1848 if (get_mult || get_identity)
1851 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
1854 bb_perror_msg("HDIO_GET_MULTCOUNT");
1858 printf(fmt, "multcount", multcount);
1864 if (!bb_ioctl(fd, HDIO_GET_32BIT, &parm, "HDIO_GET_32BIT"))
1866 printf(" IO_support\t=%3ld (", parm);
1868 printf("default 16-bit)\n");
1870 printf("16-bit)\n");
1872 printf("32-bit)\n");
1874 printf("32-bit w/sync)\n");
1876 printf("Request-Queue-Bypass)\n");
1878 printf("\?\?\?)\n");
1883 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
1884 "HDIO_GET_UNMASKINTR","unmaskirq");
1888 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1890 if (!bb_ioctl(fd, HDIO_GET_DMA, &parm, "HDIO_GET_DMA"))
1892 printf(fmt, "using_dma", parm);
1894 printf(" (DMA-Assisted-PIO)\n");
1902 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
1903 "HDIO_GET_QDMA","queue_depth");
1907 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
1908 "HDIO_GET_KEEPSETTINGS","keepsettings");
1913 bb_ioctl_on_off (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
1914 "HDIO_GET_NOWERR","nowerr");
1918 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
1919 "BLKROGET","readonly");
1923 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
1924 "BLKRAGET","readahead");
1928 if (!bb_ioctl(fd, BLKGETSIZE, &parm, "BLKGETSIZE"))
1930 struct hd_geometry g;
1932 if (!bb_ioctl(fd, HDIO_GETGEO, &g, "HDIO_GETGEO"))
1933 printf(" geometry\t= %u/%u/%u, sectors = %ld, start = %ld\n",
1934 g.cylinders, g.heads, g.sectors, parm, g.start);
1937 #ifdef HDIO_DRIVE_CMD
1940 #ifndef WIN_CHECKPOWERMODE1
1941 #define WIN_CHECKPOWERMODE1 0xE5
1943 #ifndef WIN_CHECKPOWERMODE2
1944 #define WIN_CHECKPOWERMODE2 0x98
1948 args[0] = WIN_CHECKPOWERMODE1;
1949 if (bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2, 0))
1951 if (errno != EIO || args[0] != 0 || args[1] != 0)
1957 state = (args[2] == 255) ? "active/idle" : "standby";
1958 args[1] = args[2] = 0;
1960 printf(" drive state is: %s\n", state);
1963 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1966 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
1968 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1969 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1970 if (perform_tristate)
1974 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
1976 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1977 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1980 static struct hd_driveid id;
1982 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
1984 if (multcount != -1)
1986 id.multsect = multcount;
1987 id.multsect_valid |= 1;
1990 id.multsect_valid &= ~1;
1993 else if (errno == -ENOMSG)
1994 printf(" no identification info available\n");
1996 bb_perror_msg("HDIO_GET_IDENTITY");
2001 unsigned char args1[4+512]; /* = { ... } will eat 0.5k of rodata! */
2004 memset(args1, 0, sizeof(args1));
2005 args1[0] = WIN_IDENTIFY;
2007 if (!bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY, "HDIO_DRIVE_CMD(identify)")) {
2008 uint16_t *ptr = (uint16_t *)args1;
2009 for (i=0; i<sizeof(args1)/2; i++) ptr[i] = SWAP_LE16(ptr[i]);
2010 identify((void *)(ptr+2));
2014 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2019 print_flag(1, "bus state", busstate);
2020 bus_state_value(busstate);
2022 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate, "HDIO_SET_BUSSTATE");
2026 if (!bb_ioctl(fd, HDIO_GET_BUSSTATE, &parm, "HDIO_GET_BUSSTATE"))
2028 printf(fmt, "bus state", parm);
2029 bus_state_value(parm);
2034 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2038 do_time(0,fd); /*time cache */
2040 do_time(1,fd); /*time device */
2042 flush_buffer_cache(fd);
2046 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2047 static int fromhex(unsigned char c)
2049 if (c >= 'a' && c <= 'f')
2050 return 10 + (c - 'a');
2051 if (c >= '0' && c <= '9')
2053 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2056 static void identify_from_stdin(void)
2059 unsigned char buf[1600], *b = (unsigned char *)buf;
2060 int i, count = read(0, buf, 1280);
2063 bb_error_msg_and_die("read(%d bytes) failed (rc=%d)", 1280, count);
2065 for (i = 0; count >= 4; ++i)
2067 sbuf[i] = SWAP_LE16((fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]));
2075 /* busybox specific stuff */
2076 static void parse_opts(unsigned long *get, unsigned long *set, unsigned long *value, int min, int max)
2083 *value = bb_xgetlarg(optarg, 10, min, max);
2087 static void parse_xfermode(int flag, unsigned long *get, unsigned long *set, int *value)
2092 *set = ((*value = translate_xfermode(optarg)) > -1);
2097 /*------- getopt short options --------*/
2098 static const char hdparm_options[]= "gfu::n::p:r::m::c::k::a::B:tTh"\
2099 USE_FEATURE_HDPARM_GET_IDENTITY("iI")
2100 USE_FEATURE_HDPARM_HDIO_GETSET_DMA("d::")
2101 #ifdef HDIO_DRIVE_CMD
2102 "S::D::P::X::K::A::L::W::CyYzZ"
2104 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF("U:")
2105 #ifdef HDIO_GET_QDMA
2106 #ifdef HDIO_SET_QDMA
2112 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET("w")
2113 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF("x::b:")
2114 USE_FEATURE_HDPARM_HDIO_SCAN_HWIF("R:");
2115 /*-------------------------------------*/
2117 /* our main() routine: */
2118 int hdparm_main(int argc, char **argv) ATTRIBUTE_NORETURN;
2119 int hdparm_main(int argc, char **argv)
2124 while ((c = getopt(argc, argv, hdparm_options)) >= 0) {
2126 if (c == 'h') bb_show_usage(); /* EXIT */
2127 USE_FEATURE_HDPARM_GET_IDENTITY(get_IDentity = (c == 'I'));
2128 USE_FEATURE_HDPARM_GET_IDENTITY(get_identity = (c == 'i'));
2129 get_geom |= (c == 'g');
2130 do_flush |= (c == 'f');
2131 if (c == 'u') parse_opts(&get_unmask, &set_unmask, &unmask, 0, 1);
2132 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(if (c == 'd') parse_opts(&get_dma, &set_dma, &dma, 0, 9));
2133 if (c == 'n') parse_opts(&get_nowerr, &set_nowerr, &nowerr, 0, 1);
2134 parse_xfermode((c == 'p'),&noisy_piomode, &set_piomode, &piomode);
2135 if (c == 'r') parse_opts(&get_readonly, &set_readonly, &readonly, 0, 1);
2136 if (c == 'm') parse_opts(&get_mult, &set_mult, &mult, 0, INT_MAX /*32*/);
2137 if (c == 'c') parse_opts(&get_io32bit, &set_io32bit, &io32bit, 0, INT_MAX /*8*/);
2138 if (c == 'k') parse_opts(&get_keep, &set_keep, &keep, 0, 1);
2139 if (c == 'a') parse_opts(&get_readahead, &set_readahead, &Xreadahead, 0, INT_MAX);
2140 if (c == 'B') parse_opts(&get_apmmode, &set_apmmode, &apmmode, 1, 255);
2141 do_flush |= do_timings |= (c == 't');
2142 do_flush |= do_ctimings |= (c == 'T');
2143 #ifdef HDIO_DRIVE_CMD
2144 if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, INT_MAX);
2145 if (c == 'D') parse_opts(&get_defects, &set_defects, &defects, 0, INT_MAX);
2146 if (c == 'P') parse_opts(&get_prefetch, &set_prefetch, &prefetch, 0, INT_MAX);
2147 parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
2148 if (c == 'K') parse_opts(&get_dkeep, &set_dkeep, &prefetch, 0, 1);
2149 if (c == 'A') parse_opts(&get_lookahead, &set_lookahead, &lookahead, 0, 1);
2150 if (c == 'L') parse_opts(&get_doorlock, &set_doorlock, &doorlock, 0, 1);
2151 if (c == 'W') parse_opts(&get_wcache, &set_wcache, &wcache, 0, 1);
2152 get_powermode |= (c == 'C');
2153 get_standbynow = set_standbynow |= (c == 'y');
2154 get_sleepnow = set_sleepnow |= (c == 'Y');
2155 reread_partn |= (c == 'z');
2156 get_seagate = set_seagate |= (c == 'Z');
2158 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') parse_opts(NULL, &unregister_hwif, &hwif, 0, INT_MAX));
2159 #ifdef HDIO_GET_QDMA
2161 #ifdef HDIO_SET_QDMA
2162 parse_opts(&get_dma_q, &set_dma_q, &dma_q, 0, INT_MAX);
2164 parse_opts(&get_dma_q, NULL, NULL, 0, 0);
2168 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
2169 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1));
2170 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') parse_opts(&get_busstate, &set_busstate, &busstate, 0, 2));
2171 #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
2173 parse_opts(NULL, &scan_hwif, &hwif_data, 0, INT_MAX);
2174 hwif_ctrl = bb_xgetlarg((argv[optind]) ? argv[optind] : "", 10, 0, INT_MAX);
2175 hwif_irq = bb_xgetlarg((argv[optind+1]) ? argv[optind+1] : "", 10, 0, INT_MAX);
2176 /* Move past the 2 additional arguments */
2182 /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
2184 get_mult = get_io32bit = get_unmask = get_keep = get_readonly = get_readahead = get_geom = 1;
2185 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(get_dma = 1);
2190 #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
2192 identify_from_stdin(); /* EXIT */