1 /* vi: set sw=4 ts=4: */
3 * hdparm implementation for busybox
6 * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
8 * Hacked by Tito <farmatito@tiscali.it> for size optimization.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 * This program is based on the source code of hdparm: see below...
27 * hdparm.c - Command line interface to get/set hard disk parameters
28 * - by Mark Lord (C) 1994-2002 -- freely distributable
38 #include <sys/ioctl.h>
40 #include <sys/sysmacros.h>
42 #include <sys/times.h>
43 #include <sys/mount.h>
45 #include <linux/types.h>
46 #include <linux/hdreg.h>
47 #include <linux/major.h>
48 #include <asm/byteorder.h>
51 #if (__BYTE_ORDER == __BIG_ENDIAN) && !defined(__USE_XOPEN)
58 #define ATA_DEV 0x0000
59 #define ATAPI_DEV 0x0001
61 /* word definitions */
62 /* ---------------- */
63 #define GEN_CONFIG 0 /* general configuration */
64 #define LCYLS 1 /* number of logical cylinders */
65 #define CONFIG 2 /* specific configuration */
66 #define LHEADS 3 /* number of logical heads */
67 #define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
68 #define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
69 #define LSECTS 6 /* number of logical sectors/track */
70 #define START_SERIAL 10 /* ASCII serial number */
71 #define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
72 #define BUF_TYPE 20 /* buffer type (ATA-1) */
73 #define BUFFER__SIZE 21 /* buffer size (ATA-1) */
74 #define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
75 #define START_FW_REV 23 /* ASCII firmware revision */
76 #define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
77 #define START_MODEL 27 /* ASCII model number */
78 #define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
79 #define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
80 #define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
81 #define CAPAB_0 49 /* capabilities */
83 #define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
84 #define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
85 #define WHATS_VALID 53 /* what fields are valid */
86 #define LCYLS_CUR 54 /* current logical cylinders */
87 #define LHEADS_CUR 55 /* current logical heads */
88 #define LSECTS_CUR 56 /* current logical sectors/track */
89 #define CAPACITY_LSB 57 /* current capacity in sectors */
90 #define CAPACITY_MSB 58
91 #define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
92 #define LBA_SECTS_LSB 60 /* LBA: total number of user */
93 #define LBA_SECTS_MSB 61 /* addressable sectors */
94 #define SINGLE_DMA 62 /* singleword DMA modes */
95 #define MULTI_DMA 63 /* multiword DMA modes */
96 #define ADV_PIO_MODES 64 /* advanced PIO modes supported */
97 /* multiword DMA xfer cycle time: */
98 #define DMA_TIME_MIN 65 /* - minimum */
99 #define DMA_TIME_NORM 66 /* - manufacturer's recommended */
100 /* minimum PIO xfer cycle time: */
101 #define PIO_NO_FLOW 67 /* - without flow control */
102 #define PIO_FLOW 68 /* - with IORDY flow control */
103 #define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
104 #define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
105 #define CDR_MAJOR 73 /* CD ROM: major version number */
106 #define CDR_MINOR 74 /* CD ROM: minor version number */
107 #define QUEUE_DEPTH 75 /* queue depth */
108 #define MAJOR 80 /* major version number */
109 #define MINOR 81 /* minor version number */
110 #define CMDS_SUPP_0 82 /* command/feature set(s) supported */
111 #define CMDS_SUPP_1 83
112 #define CMDS_SUPP_2 84
113 #define CMDS_EN_0 85 /* command/feature set(s) enabled */
116 #define ULTRA_DMA 88 /* ultra DMA modes */
117 /* time to complete security erase */
118 #define ERASE_TIME 89 /* - ordinary */
119 #define ENH_ERASE_TIME 90 /* - enhanced */
120 #define ADV_PWR 91 /* current advanced power management level
121 in low byte, 0x40 in high byte. */
122 #define PSWD_CODE 92 /* master password revision code */
123 #define HWRST_RSLT 93 /* hardware reset result */
124 #define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
125 #define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
126 #define LBA_MID 101 /* bits are used, but addr 103 */
127 #define LBA_48_MSB 102 /* has been reserved for LBA in */
128 #define LBA_64_MSB 103 /* the future. */
129 #define RM_STAT 127 /* removable media status notification feature set support */
130 #define SECU_STATUS 128 /* security status */
131 #define CFA_PWR_MODE 160 /* CFA power mode 1 */
132 #define START_MEDIA 176 /* media serial number */
133 #define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
134 #define START_MANUF 196 /* media manufacturer I.D. */
135 #define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
136 #define INTEGRITY 255 /* integrity word */
138 /* bit definitions within the words */
139 /* -------------------------------- */
141 /* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
143 #define VALID_VAL 0x4000
144 /* many words are considered invalid if they are either all-0 or all-1 */
145 #define NOVAL_0 0x0000
146 #define NOVAL_1 0xffff
148 /* word 0: gen_config */
149 #define NOT_ATA 0x8000
150 #define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
151 #define MEDIA_REMOVABLE 0x0080
152 #define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
153 #define INCOMPLETE 0x0004
154 #define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
155 #define DRQ_RESPONSE_TIME 0x0060
156 #define DRQ_3MS_VAL 0x0000
157 #define DRQ_INTR_VAL 0x0020
158 #define DRQ_50US_VAL 0x0040
159 #define PKT_SIZE_SUPPORTED 0x0003
160 #define PKT_SIZE_12_VAL 0x0000
161 #define PKT_SIZE_16_VAL 0x0001
162 #define EQPT_TYPE 0x1f00
167 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
168 static const char *pkt_str[] = {
169 "Direct-access device", /* word 0, bits 12-8 = 00 */
170 "Sequential-access device", /* word 0, bits 12-8 = 01 */
171 "Printer", /* word 0, bits 12-8 = 02 */
172 "Processor", /* word 0, bits 12-8 = 03 */
173 "Write-once device", /* word 0, bits 12-8 = 04 */
174 "CD-ROM", /* word 0, bits 12-8 = 05 */
175 "Scanner", /* word 0, bits 12-8 = 06 */
176 "Optical memory", /* word 0, bits 12-8 = 07 */
177 "Medium changer", /* word 0, bits 12-8 = 08 */
178 "Communications device", /* word 0, bits 12-8 = 09 */
179 "ACS-IT8 device", /* word 0, bits 12-8 = 0a */
180 "ACS-IT8 device", /* word 0, bits 12-8 = 0b */
181 "Array controller", /* word 0, bits 12-8 = 0c */
182 "Enclosure services", /* word 0, bits 12-8 = 0d */
183 "Reduced block command device", /* word 0, bits 12-8 = 0e */
184 "Optical card reader/writer", /* word 0, bits 12-8 = 0f */
185 "", /* word 0, bits 12-8 = 10 */
186 "", /* word 0, bits 12-8 = 11 */
187 "", /* word 0, bits 12-8 = 12 */
188 "", /* word 0, bits 12-8 = 13 */
189 "", /* word 0, bits 12-8 = 14 */
190 "", /* word 0, bits 12-8 = 15 */
191 "", /* word 0, bits 12-8 = 16 */
192 "", /* word 0, bits 12-8 = 17 */
193 "", /* word 0, bits 12-8 = 18 */
194 "", /* word 0, bits 12-8 = 19 */
195 "", /* word 0, bits 12-8 = 1a */
196 "", /* word 0, bits 12-8 = 1b */
197 "", /* word 0, bits 12-8 = 1c */
198 "", /* word 0, bits 12-8 = 1d */
199 "", /* word 0, bits 12-8 = 1e */
200 "Unknown", /* word 0, bits 12-8 = 1f */
202 static const char *ata1_cfg_str[] = { /* word 0 in ATA-1 mode */
203 "reserved", /* bit 0 */
204 "hard sectored", /* bit 1 */
205 "soft sectored", /* bit 2 */
206 "not MFM encoded ", /* bit 3 */
207 "head switch time > 15us", /* bit 4 */
208 "spindle motor control option", /* bit 5 */
209 "fixed drive", /* bit 6 */
210 "removable drive", /* bit 7 */
211 "disk xfer rate <= 5Mbs", /* bit 8 */
212 "disk xfer rate > 5Mbs, <= 10Mbs", /* bit 9 */
213 "disk xfer rate > 5Mbs", /* bit 10 */
214 "rotational speed tol.", /* bit 11 */
215 "data strobe offset option", /* bit 12 */
216 "track offset option", /* bit 13 */
217 "format speed tolerance gap reqd", /* bit 14 */
222 /* word 1: number of logical cylinders */
223 #define LCYLS_MAX 0x3fff /* maximum allowable value */
225 /* word 2: specific configureation
226 * (a) require SET FEATURES to spin-up
227 * (b) require spin-up to fully reply to IDENTIFY DEVICE
229 #define STBY_NID_VAL 0x37c8 /* (a) and (b) */
230 #define STBY_ID_VAL 0x738c /* (a) and not (b) */
231 #define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
232 #define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
234 /* words 47 & 59: sector_xfer_max & sector_xfer_cur */
235 #define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
236 #define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
238 /* word 49: capabilities 0 */
239 #define STD_STBY 0x2000 /* 1=standard values supported (ATA);
240 0=vendor specific values */
241 #define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
242 #define IORDY_OFF 0x0400 /* 1=may be disabled */
243 #define LBA_SUP 0x0200 /* 1=Logical Block Address support */
244 #define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
245 #define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
246 #define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
247 #define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
248 #define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
250 /* word 50: capabilities 1 */
251 #define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
253 /* words 51 & 52: PIO & DMA cycle times */
254 #define MODE 0xff00 /* the mode is in the MSBs */
256 /* word 53: whats_valid */
257 #define OK_W88 0x0004 /* the ultra_dma info is valid */
258 #define OK_W64_70 0x0002 /* see above for word descriptions */
259 #define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
261 /*word 63,88: dma_mode, ultra_dma_mode*/
262 #define MODE_MAX 7 /* bit definitions force udma <=7 (when
263 * udma >=8 comes out it'll have to be
264 * defined in a new dma_mode word!) */
266 /* word 64: PIO transfer modes */
267 #define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
268 #define PIO_MODE_MAX 8 /* but all 8 bits are defined */
270 /* word 75: queue_depth */
271 #define DEPTH_BITS 0x001f /* bits used for queue depth */
273 /* words 80-81: version numbers */
274 /* NOVAL_0 or NOVAL_1 means device does not report version */
276 /* word 81: minor version number */
277 #define MINOR_MAX 0x1C
278 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
279 static const char *minor_str[] = { /* word 81 value: */
280 "device does not report version", /* 0x0000 */
281 "ATA-1 X3T9.2 781D prior to revision 4", /* 0x0001 */
282 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
283 "ATA-1 X3T9.2 781D revision 4", /* 0x0003 */
284 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
285 "ATA-2 X3T10 948D prior to revision 2k", /* 0x0005 */
286 "ATA-3 X3T10 2008D revision 1", /* 0x0006 */
287 "ATA-2 X3T10 948D revision 2k", /* 0x0007 */
288 "ATA-3 X3T10 2008D revision 0", /* 0x0008 */
289 "ATA-2 X3T10 948D revision 3", /* 0x0009 */
290 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
291 "ATA-3 X3T10 2008D revision 6", /* 0x000b */
292 "ATA-3 X3T13 2008D revision 7 and 7a", /* 0x000c */
293 "ATA/ATAPI-4 X3T13 1153D revision 6", /* 0x000d */
294 "ATA/ATAPI-4 T13 1153D revision 13", /* 0x000e */
295 "ATA/ATAPI-4 X3T13 1153D revision 7", /* 0x000f */
296 "ATA/ATAPI-4 T13 1153D revision 18", /* 0x0010 */
297 "ATA/ATAPI-4 T13 1153D revision 15", /* 0x0011 */
298 "ATA/ATAPI-4 published, ANSI NCITS 317-1998", /* 0x0012 */
299 "ATA/ATAPI-5 T13 1321D revision 3",
300 "ATA/ATAPI-4 T13 1153D revision 14", /* 0x0014 */
301 "ATA/ATAPI-5 T13 1321D revision 1", /* 0x0015 */
302 "ATA/ATAPI-5 published, ANSI NCITS 340-2000", /* 0x0016 */
303 "ATA/ATAPI-4 T13 1153D revision 17", /* 0x0017 */
304 "ATA/ATAPI-6 T13 1410D revision 0", /* 0x0018 */
305 "ATA/ATAPI-6 T13 1410D revision 3a", /* 0x0019 */
306 "Reserved", /* 0x001a */
307 "ATA/ATAPI-6 T13 1410D revision 2", /* 0x001b */
308 "ATA/ATAPI-6 T13 1410D revision 1", /* 0x001c */
309 "reserved" /* 0x001d */
310 "reserved" /* 0x001e */
311 "reserved" /* 0x001f-0xfffe*/
314 static const char actual_ver[] = {
316 0, /* 0x0000 WARNING: */
317 1, /* 0x0001 WARNING: */
318 1, /* 0x0002 WARNING: */
319 1, /* 0x0003 WARNING: */
320 2, /* 0x0004 WARNING: This array */
321 2, /* 0x0005 WARNING: corresponds */
322 3, /* 0x0006 WARNING: *exactly* */
323 2, /* 0x0007 WARNING: to the ATA/ */
324 3, /* 0x0008 WARNING: ATAPI version */
325 2, /* 0x0009 WARNING: listed in */
326 3, /* 0x000a WARNING: the */
327 3, /* 0x000b WARNING: minor_str */
328 3, /* 0x000c WARNING: array */
329 4, /* 0x000d WARNING: above. */
330 4, /* 0x000e WARNING: */
331 4, /* 0x000f WARNING: if you change */
332 4, /* 0x0010 WARNING: that one, */
333 4, /* 0x0011 WARNING: change this one */
334 4, /* 0x0012 WARNING: too!!! */
335 5, /* 0x0013 WARNING: */
336 4, /* 0x0014 WARNING: */
337 5, /* 0x0015 WARNING: */
338 5, /* 0x0016 WARNING: */
339 4, /* 0x0017 WARNING: */
340 6, /* 0x0018 WARNING: */
341 6, /* 0x0019 WARNING: */
342 0, /* 0x001a WARNING: */
343 6, /* 0x001b WARNING: */
344 6, /* 0x001c WARNING: */
345 0 /* 0x001d-0xfffe */
348 /* words 82-84: cmds/feats supported */
349 #define CMDS_W82 0x77ff /* word 82: defined command locations*/
350 #define CMDS_W83 0x3fff /* word 83: defined command locations*/
351 #define CMDS_W84 0x002f /* word 83: defined command locations*/
352 #define SUPPORT_48_BIT 0x0400
353 #define NUM_CMD_FEAT_STR 48
355 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
356 static const char *cmd_feat_str[] = {
357 "", /* word 82 bit 15: obsolete */
358 "NOP cmd", /* word 82 bit 14 */
359 "READ BUFFER cmd", /* word 82 bit 13 */
360 "WRITE BUFFER cmd", /* word 82 bit 12 */
361 "", /* word 82 bit 11: obsolete */
362 "Host Protected Area feature set", /* word 82 bit 10 */
363 "DEVICE RESET cmd", /* word 82 bit 9 */
364 "SERVICE interrupt", /* word 82 bit 8 */
365 "Release interrupt", /* word 82 bit 7 */
366 "Look-ahead", /* word 82 bit 6 */
367 "Write cache", /* word 82 bit 5 */
368 "PACKET command feature set", /* word 82 bit 4 */
369 "Power Management feature set", /* word 82 bit 3 */
370 "Removable Media feature set", /* word 82 bit 2 */
371 "Security Mode feature set", /* word 82 bit 1 */
372 "SMART feature set", /* word 82 bit 0 */
374 "", /* word 83 bit 15: !valid bit */
375 "", /* word 83 bit 14: valid bit */
376 "FLUSH CACHE EXT command", /* word 83 bit 13 */
377 "Mandatory FLUSH CACHE command ", /* word 83 bit 12 */
378 "Device Configuration Overlay feature set ",
379 "48-bit Address feature set ", /* word 83 bit 10 */
381 "SET MAX security extension", /* word 83 bit 8 */
382 "Address Offset Reserved Area Boot", /* word 83 bit 7 */
383 "SET FEATURES subcommand required to spinup after power up",
384 "Power-Up In Standby feature set", /* word 83 bit 5 */
385 "Removable Media Status Notification feature set",
386 "Advanced Power Management feature set",/* word 83 bit 3 */
387 "CFA feature set", /* word 83 bit 2 */
388 "READ/WRITE DMA QUEUED", /* word 83 bit 1 */
389 "DOWNLOAD MICROCODE cmd", /* word 83 bit 0 */
391 "", /* word 84 bit 15: !valid bit */
392 "", /* word 84 bit 14: valid bit */
393 "", /* word 84 bit 13: reserved */
394 "", /* word 84 bit 12: reserved */
395 "", /* word 84 bit 11: reserved */
396 "", /* word 84 bit 10: reserved */
397 "", /* word 84 bit 9: reserved */
398 "", /* word 84 bit 8: reserved */
399 "", /* word 84 bit 7: reserved */
400 "", /* word 84 bit 6: reserved */
401 "General Purpose Logging feature set", /* word 84 bit 5 */
402 "", /* word 84 bit 4: reserved */
403 "Media Card Pass Through Command feature set ",
404 "Media serial number ", /* word 84 bit 2 */
405 "SMART self-test ", /* word 84 bit 1 */
406 "SMART error logging " /* word 84 bit 0 */
411 /* words 85-87: cmds/feats enabled */
412 /* use cmd_feat_str[] to display what commands and features have
413 * been enabled with words 85-87
416 /* words 89, 90, SECU ERASE TIME */
417 #define ERASE_BITS 0x00ff
419 /* word 92: master password revision */
420 /* NOVAL_0 or NOVAL_1 means no support for master password revision */
422 /* word 93: hw reset result */
423 #define CBLID 0x2000 /* CBLID status */
424 #define RST0 0x0001 /* 1=reset to device #0 */
425 #define DEV_DET 0x0006 /* how device num determined */
426 #define JUMPER_VAL 0x0002 /* device num determined by jumper */
427 #define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
429 /* word 127: removable media status notification feature set support */
430 #define RM_STAT_BITS 0x0003
431 #define RM_STAT_SUP 0x0001
433 /* word 128: security */
434 #define SECU_ENABLED 0x0002
435 #define SECU_LEVEL 0x0010
436 #define NUM_SECU_STR 6
437 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
438 static const char *secu_str[] = {
439 "supported", /* word 128, bit 0 */
440 "enabled", /* word 128, bit 1 */
441 "locked", /* word 128, bit 2 */
442 "frozen", /* word 128, bit 3 */
443 "expired: security count", /* word 128, bit 4 */
444 "supported: enhanced erase" /* word 128, bit 5 */
448 /* word 160: CFA power mode */
449 #define VALID_W160 0x8000 /* 1=word valid */
450 #define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
451 #define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
452 #define MAX_AMPS 0x0fff /* value = max current in ma */
454 /* word 255: integrity */
455 #define SIG 0x00ff /* signature location */
456 #define SIG_VAL 0x00A5 /* signature value */
458 #define VERSION "v5.4"
461 #define TIMING_BUF_MB 1
462 #define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
463 #define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
464 #define BUFCACHE_FACTOR 2
466 #undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
468 /* Busybox messages and functions */
470 const char * const bb_msg_shared_mem ="could not %s sharedmem buf";
471 const char * const bb_msg_op_not_supp =" operation not supported on %s disks";
473 static void bb_ioctl(int fd, int request, void *argp, const char *string)
475 if (ioctl (fd, request, argp) != 0)
476 bb_error_msg(" %s", string);
479 static void if_printf(unsigned long i, char *fmt, ... )
488 static void on_off (unsigned int value);
490 static void if_printf_on_off(unsigned long get_arg,const char *fmt, unsigned long arg)
499 static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
502 if (ioctl (fd, request, &argp) != 0)
503 bb_error_msg(" %s", string);
506 printf(fmt, (unsigned long) argp);
507 on_off((unsigned long) argp);
511 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
512 static void if_else_printf(unsigned long i, char *fmt1, char *fmt2, ... )
523 static void print_ascii(uint16_t *p, uint8_t length);
525 static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
529 printf("\t%-20s",string);
530 print_ascii(&val[i], n);
534 static void if_strcat(unsigned long test, char *modes, char *string)
537 strcat(modes,string);
541 static void sync_and_sleep(int i)
547 static void check_if_min_and_set_val(uint16_t a, uint16_t b)
553 static void check_if_maj_and_set_val(uint16_t a, uint16_t b)
559 char * check_ptr(char *p, int argc, char **argv)
561 if (!*p && argc && isdigit(**argv))
566 char * check_ptr_v2(char *p, int argc, char **argv)
568 if (!*p && argc && isalnum(**argv))
573 unsigned long int set_flag(char *p, char max)
575 if (*p >= '0' && *p <= max )
580 /* end of busybox specific stuff */
582 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
583 static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
588 for(ii = 0; ii <= MODE_MAX; ii++)
590 if(mode_sel & 0x0001)
592 printf("*%cdma%u ",cc,ii);
597 else if(mode_sup & 0x0001)
598 printf("%cdma%u ",cc,ii);
606 static void print_ascii(uint16_t *p, uint8_t length) {
610 /* find first non-space & print it */
611 for(ii = 0; ii< length; ii++)
613 if(((char) 0x00ff&((*p)>>8)) != ' ')
615 if((cl = (char) 0x00ff&(*p)) != ' ')
617 if_printf((cl != '\0'),"%c",cl);
625 for(; ii< length; ii++)
628 break; /* some older devices have NULLs */
629 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
635 /* identify() is the only extern function used across two source files. The
636 others, though, were declared in hdparm.c with global scope; since other
637 functions in that file have static (file) scope, I assume the difference is
639 static void identify (uint16_t *id_supplied, const char *devname)
642 char *id_file = NULL;
644 uint16_t val[256], ii, jj, kk;
645 uint16_t like_std = 1, std = 0, min_std = 0xffff;
646 uint16_t dev = NO_DEV, eqpt = NO_DEV;
647 uint8_t have_mode = 0, err_dma = 0;
649 uint32_t ll, mm, nn, oo;
650 __u64 bbbig; /* (:) */
654 #if __BYTE_ORDER == __BIG_ENDIAN
655 swab(id_supplied, val, sizeof(val));
657 memcpy(val, id_supplied, sizeof(val));
662 id_file = xcalloc(1, strlen(devname)+22);
663 sprintf(id_file, "/proc/ide/%s/identify", devname);
664 /* open the file, read in all the info and close it */
668 fl = bb_xfopen(id_file, "r");
670 /* calculate checksum over all bytes */
671 for(ii = GEN_CONFIG; ii<=INTEGRITY; ii++)
673 unsigned int scratch;
674 if(1 != fscanf(fl,"%04x",&scratch))
676 val[ii] = (uint16_t)scratch;
677 chksum += val[ii] + (val[ii] >> 8);
680 /*bb_fclose_nonstdin(fl);*/
681 if(ii < (INTEGRITY+1))
682 bb_error_msg_and_die("Input file wrong format or length");
686 /* check if we recognise the device type */
688 if(!(val[GEN_CONFIG] & NOT_ATA))
691 printf("ATA device, with ");
693 else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
697 printf("CompactFlash ATA device, with ");
699 else if(!(val[GEN_CONFIG] & NOT_ATAPI))
702 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
703 printf("ATAPI %s, with ", pkt_str[eqpt]);
707 /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
708 bb_error_msg_and_die("Unknown device type");
710 if_printf(!(val[GEN_CONFIG] & MEDIA_REMOVABLE),"non-");
711 printf("removable media\n");
713 /* Info from the specific configuration word says whether or not the
714 * ID command completed correctly. It is only defined, however in
715 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
716 * standards. Since the values allowed for this word are extremely
717 * specific, it should be safe to check it now, even though we don't
718 * know yet what standard this device is using.
720 if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
721 (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
724 if_printf(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)),
725 "powers-up in standby; SET FEATURES subcmd spins-up.\n");
726 if_printf((((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE)),
727 "\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
730 /* output the model and serial numbers and the fw revision */
731 xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
732 xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
733 xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
734 xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
735 xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
737 /* major & minor standards version number (Note: these words were not
738 * defined until ATA-3 & the CDROM std uses different words.) */
739 printf("Standards:");
742 if(val[MINOR] && (val[MINOR] <= MINOR_MAX))
744 check_if_min_and_set_val( like_std, 3);
745 std = actual_ver[val[MINOR]];
746 if_printf(std,"\n\tUsed: %s ",minor_str[val[MINOR]]);
749 /* looks like when they up-issue the std, they obsolete one;
750 * thus, only the newest 4 issues need be supported. (That's
751 * what "kk" and "min_std" are all about.) */
752 if(val[MAJOR] && (val[MAJOR] !=NOVAL_1))
754 printf("\n\tSupported: ");
755 jj = val[MAJOR] << 1;
756 kk = like_std >4 ? like_std-4: 0;
757 for(ii = 14; (ii >0)&&(ii>kk); ii--)
765 kk = like_std >4 ? like_std-4: 0;
767 check_if_maj_and_set_val(min_std, ii);
771 check_if_min_and_set_val( like_std, 3);
773 /* Figure out what standard the device is using if it hasn't told
774 * us. If we know the std, check if the device is using any of
775 * the words from the next level up. It happens.
777 check_if_min_and_set_val( like_std, std);
779 if(((std == 5) || (!std && (like_std < 6))) &&
780 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
781 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
782 ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
783 ( val[CMDS_SUPP_2] & CMDS_W84) ) ) )
787 else if(((std == 4) || (!std && (like_std < 5))) &&
788 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
789 (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
790 ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
791 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
795 else if(((std == 3) || (!std && (like_std < 4))) &&
796 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
797 ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
798 (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
799 (( val[CAPAB_1] & VALID) == VALID_VAL) ||
800 (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
801 (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
805 else if(((std == 2) || (!std && (like_std < 3))) &&
806 ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
810 else if(((std == 1) || (!std && (like_std < 2))) &&
811 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
812 (val[WHATS_VALID] & OK_W64_70)) )
817 printf("\n\tLikely used: %u\n",like_std);
818 else if(like_std > std)
819 printf("& some of %u\n",like_std);
825 /* TBD: do CDROM stuff more thoroughly. For now... */
827 if(val[CDR_MINOR] == 9)
830 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
832 if(val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
835 printf("\n\tSupported: CD-ROM ATAPI");
836 jj = val[CDR_MAJOR] >> 1;
837 for(ii = 1; ii <15; ii++)
839 if_printf((jj & 0x0001),"-%u ", ii);
843 if_else_printf((!kk),"\n\tLikely used CD-ROM ATAPI-1\n","\n");
844 /* the cdrom stuff is more like ATA-2 than anything else, so: */
848 if(min_std == 0xffff)
849 min_std = like_std > 4 ? like_std - 3 : 1;
851 printf("Configuration:\n");
852 /* more info from the general configuration word */
853 if((eqpt != CDROM) && (like_std == 1))
855 jj = val[GEN_CONFIG] >> 1;
856 for(ii = 1; ii < 15; ii++)
858 if_printf((jj & 0x0001),"\t%s\n",ata1_cfg_str[ii]);
864 printf("\tDRQ response: "); /* Data Request (DRQ) */
865 switch(val[GEN_CONFIG] & DRQ_RESPONSE_TIME)
871 printf("<=10ms with INTRQ\n");
877 printf("unknown.\n");
880 printf("\tPacket size: ");
881 switch(val[GEN_CONFIG] & PKT_SIZE_SUPPORTED)
883 case PKT_SIZE_12_VAL :
884 printf("12 bytes\n");
886 case PKT_SIZE_16_VAL :
887 printf("16 bytes\n");
896 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
897 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
899 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
900 printf("\tCHS addressing not supported\n");
903 jj = val[WHATS_VALID] & OK_W54_58;
904 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",
905 val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
907 if_printf(((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES])),
908 "\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
912 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
915 /* check Endian of capacity bytes */
916 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
917 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
918 if(abs(mm - nn) > abs(oo - nn))
921 printf("\tCHS current addressable sectors:%11u\n",mm);
925 printf("\tLBA user addressable sectors:%11u\n",ll);
926 if( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
927 (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
929 bbbig = (__u64)val[LBA_64_MSB] << 48 |
930 (__u64)val[LBA_48_MSB] << 32 |
931 (__u64)val[LBA_MID] << 16 |
933 printf("\tLBA48 user addressable sectors:%11llu\n",bbbig);
937 bbbig = (__u64)(ll>mm ? ll : mm); /* # 512 byte blocks */
938 printf("\tdevice size with M = 1024*1024: %11llu MBytes\n",bbbig>>11);
939 bbbig = (bbbig<<9)/1000000;
940 printf("\tdevice size with M = 1000*1000: %11llu MBytes ",bbbig);
942 if_else_printf((bbbig > 1000),"(%llu GB)\n","\n",bbbig/1000);
946 /* hw support of commands (capabilities) */
947 printf("Capabilities:\n\t");
952 if_printf((val[CAPAB_0] & CMD_Q_SUP),"Cmd queuing, ");
954 if_printf((val[CAPAB_0] & OVLP_SUP),"Cmd overlap, ");
956 if_printf((val[CAPAB_0] & LBA_SUP),"LBA, ");
961 if_printf((!(val[CAPAB_0] & IORDY_SUP)),"(may be)");
962 if_else_printf((val[CAPAB_0] & IORDY_OFF),"(can","(cannot");
963 printf(" be disabled)\n");
966 printf("no IORDY\n");
968 if((like_std == 1) && val[BUF_TYPE])
971 printf("\tBuffer type: %04x: ",kk);
972 if_else_printf((kk < 2),"single port, single-sector","dual port, multi-sector");
973 if_printf((kk > 2)," with read caching ability");
977 if((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
979 printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
982 if((min_std < 4) && (val[RW_LONG]))
984 printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
987 if((eqpt != CDROM) && (like_std > 3))
989 printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
997 printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
1000 printf("\tStandby timer values: spec'd by ");
1001 if_else_printf((val[CAPAB_0] & STD_STBY),"Standard","Vendor");
1002 if((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
1003 printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
1007 printf("\tR/W multiple sector transfer: ");
1008 if((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
1009 printf("not supported\n");
1012 printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
1013 if_else_printf((val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID),
1014 "%u\n","?\n",val[SECTOR_XFER_CUR] & SECTOR_XFER);
1016 if((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
1018 /* We print out elsewhere whether the APM feature is enabled or
1019 not. If it's not enabled, let's not repeat the info; just print
1021 printf("\tAdvancedPM level: ");
1022 if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
1024 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
1025 printf("%u (0x%x)\n", apm_level, apm_level);
1028 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
1032 if_printf(val[ACOUSTIC],"\tRecommended acoustic management value: %u, current value: %u\n",
1033 (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
1040 if_printf((val[CAPAB_0] & SWRST_REQ),"\tATA sw reset required\n");
1042 if(val[PKT_REL] || val[SVC_NBSY])
1044 printf("\tOverlap support:");
1045 if_printf(val[PKT_REL]," %uus to release bus.",val[PKT_REL]);
1046 if_printf(val[SVC_NBSY]," %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
1051 /* DMA stuff. Check that only one DMA mode is selected. */
1053 if(!(val[CAPAB_0] & DMA_SUP))
1054 printf("not supported\n");
1057 if_printf((val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA]),
1058 " sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
1061 jj = val[SINGLE_DMA];
1062 kk = val[SINGLE_DMA] >> 8;
1063 err_dma += mode_loop(jj,kk,'s',&have_mode);
1067 jj = val[MULTI_DMA];
1068 kk = val[MULTI_DMA] >> 8;
1069 err_dma += mode_loop(jj,kk,'m',&have_mode);
1071 if((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
1073 jj = val[ULTRA_DMA];
1074 kk = val[ULTRA_DMA] >> 8;
1075 err_dma += mode_loop(jj,kk,'u',&have_mode);
1077 if_printf((err_dma || !have_mode),"(?)");
1080 if_printf(((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)),
1081 "\t Interleaved DMA support\n");
1083 if((val[WHATS_VALID] & OK_W64_70) &&
1084 (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
1086 printf("\t Cycle time:");
1087 if_printf(val[DMA_TIME_MIN]," min=%uns",val[DMA_TIME_MIN]);
1088 if_printf(val[DMA_TIME_NORM]," recommended=%uns",val[DMA_TIME_NORM]);
1093 /* Programmed IO stuff */
1095 /* If a drive supports mode n (e.g. 3), it also supports all modes less
1096 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
1097 if((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
1099 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
1100 for(ii = 0; ii <= PIO_MODE_MAX ; ii++)
1102 if_printf((jj & 0x0001),"pio%d ",ii);
1107 else if(((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
1109 for(ii = 0; ii <= val[PIO_MODE]>>8; ii++)
1110 printf("pio%d ",ii);
1114 printf("unknown\n");
1116 if(val[WHATS_VALID] & OK_W64_70)
1118 if(val[PIO_NO_FLOW] || val[PIO_FLOW])
1120 printf("\t Cycle time:");
1121 if_printf(val[PIO_NO_FLOW]," no flow control=%uns", val[PIO_NO_FLOW]);
1122 if_printf(val[PIO_FLOW]," IORDY flow control=%uns", val[PIO_FLOW]);
1127 if((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
1129 printf("Commands/features:\n\tEnabled\tSupported:\n");
1130 jj = val[CMDS_SUPP_0];
1131 kk = val[CMDS_EN_0];
1132 for(ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
1134 if((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
1136 if_else_printf((kk & 0x8000),"\t *","\t");
1137 printf("\t%s\n",cmd_feat_str[ii]);
1142 jj = val[CMDS_SUPP_0+1+(ii/16)];
1143 kk = val[CMDS_EN_0+1+(ii/16)];
1147 if((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
1152 if_printf(((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP),
1153 "\tRemovable Media Status Notification feature set supported\n");
1157 if((eqpt != CDROM) && (like_std > 3) &&
1158 (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1160 printf("Security: \n");
1161 if_printf((val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1)),
1162 "\tMaster password revision code = %u\n",val[PSWD_CODE]);
1163 jj = val[SECU_STATUS];
1166 for(ii = 0; ii < NUM_SECU_STR; ii++)
1168 if_else_printf((!(jj & 0x0001)),"\tnot\t%s\n", "\t\t%s\n", secu_str[ii]);
1171 if(val[SECU_STATUS] & SECU_ENABLED)
1173 printf("\tSecurity level ");
1174 if_else_printf((val[SECU_STATUS] & SECU_LEVEL),"maximum\n","high\n");
1177 jj = val[ERASE_TIME] & ERASE_BITS;
1178 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
1182 if_printf(jj,"%umin for SECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1);
1183 if_printf(kk,"%umin for ENHANCED SECURITY ERASE UNIT.", kk==ERASE_BITS ? 508 : kk<<1);
1189 if((val[HWRST_RSLT] & VALID) == VALID_VAL)
1191 printf("HW reset results:\n");
1192 if_else_printf((val[HWRST_RSLT] & CBLID),"\tCBLID- above Vih\n","\tCBLID- below Vih\n");
1194 if(val[HWRST_RSLT] & RST0)
1196 printf("\tDevice num = 0");
1197 jj = val[HWRST_RSLT];
1201 printf("\tDevice num = 1");
1202 jj = val[HWRST_RSLT] >> 8;
1205 if((jj & DEV_DET) == JUMPER_VAL)
1206 printf(" determined by the jumper");
1207 else if((jj & DEV_DET) == CSEL_VAL)
1208 printf(" determined by CSEL");
1212 /* more stuff from std 5 */
1213 if((like_std > 4) && (eqpt != CDROM))
1215 if(val[CFA_PWR_MODE] & VALID_W160)
1217 printf("CFA power mode 1:\n\t");
1218 if_else_printf((val[CFA_PWR_MODE] & PWR_MODE_OFF),"disabled","enabled");
1220 if_printf((val[CFA_PWR_MODE] & PWR_MODE_REQ)," and required by some commands");
1223 if_printf((val[CFA_PWR_MODE] & MAX_AMPS),"\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
1225 if((val[INTEGRITY] & SIG) == SIG_VAL)
1227 printf("Checksum: ");
1228 if_printf(chksum,"in");
1229 printf("correct\n");
1237 static int verbose = 0, get_identity = 0, get_geom = 0, noisy = 1, quiet = 0;
1238 static int flagcount = 0, do_flush = 0, is_scsi_hd = 0, is_xt_hd = 0;
1239 static int do_ctimings, do_timings = 0;
1241 static unsigned long set_readahead= 0, get_readahead= 0, readahead= 0;
1242 static unsigned long set_readonly = 0, get_readonly = 0, readonly = 0;
1243 static unsigned long set_unmask = 0, get_unmask = 0, unmask = 0;
1244 static unsigned long set_mult = 0, get_mult = 0, mult = 0;
1245 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1246 static unsigned long set_dma = 0, get_dma = 0, dma = 0;
1248 static unsigned long set_dma_q = 0, get_dma_q = 0, dma_q = 0;
1249 static unsigned long set_nowerr = 0, get_nowerr = 0, nowerr = 0;
1250 static unsigned long set_keep = 0, get_keep = 0, keep = 0;
1251 static unsigned long set_io32bit = 0, get_io32bit = 0, io32bit = 0;
1252 static unsigned long set_piomode = 0, noisy_piomode= 0;
1253 static int piomode = 0;
1254 #ifdef HDIO_DRIVE_CMD
1255 static unsigned long set_dkeep = 0, get_dkeep = 0, dkeep = 0;
1256 static unsigned long set_standby = 0, get_standby = 0, standby_requested= 0;
1257 static unsigned long set_xfermode = 0, get_xfermode = 0;
1258 static int xfermode_requested= 0;
1259 static unsigned long set_lookahead= 0, get_lookahead= 0, lookahead= 0;
1260 static unsigned long set_prefetch = 0, get_prefetch = 0, prefetch = 0;
1261 static unsigned long set_defects = 0, get_defects = 0, defects = 0;
1262 static unsigned long set_wcache = 0, get_wcache = 0, wcache = 0;
1263 static unsigned long set_doorlock = 0, get_doorlock = 0, doorlock = 0;
1264 static unsigned long set_seagate = 0, get_seagate = 0;
1265 static unsigned long set_standbynow = 0, get_standbynow = 0;
1266 static unsigned long set_sleepnow = 0, get_sleepnow = 0;
1267 static unsigned long get_powermode = 0;
1268 static unsigned long set_apmmode = 0, get_apmmode= 0, apmmode = 0;
1270 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1271 static int get_IDentity = 0;
1273 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1274 static int unregister_hwif = 0;
1275 static int hwif = 0;
1277 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1278 static int scan_hwif = 0;
1279 static int hwif_data = 0;
1280 static int hwif_ctrl = 0;
1281 static int hwif_irq = 0;
1283 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1284 static int set_busstate = 0, get_busstate = 0, busstate = 0;
1286 static int reread_partn = 0;
1288 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1289 static int perform_reset = 0;
1290 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1291 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1292 static int perform_tristate = 0, tristate = 0;
1293 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1295 // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1296 // then the HDIO_GET_IDENTITY only returned 142 bytes.
1297 // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1298 // and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1299 // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1300 // (which they should, but they should just return -EINVAL).
1302 // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1303 // On a really old system, it will not, and we will be confused.
1306 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1307 static const char *cfg_str[] =
1308 { "", " HardSect", " SoftSect", " NotMFM",
1309 " HdSw>15uSec", " SpinMotCtl", " Fixed", " Removeable",
1310 " DTR<=5Mbs", " DTR>5Mbs", " DTR>10Mbs", " RotSpdTol>.5%",
1311 " dStbOff", " TrkOff", " FmtGapReq", " nonMagnetic"
1314 static const char *BuffType[] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
1316 static void dump_identity (const struct hd_driveid *id)
1319 char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
1320 const unsigned short int *id_regs= (const void*) id;
1321 unsigned long capacity;
1323 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1324 id->model, id->fw_rev, id->serial_no);
1325 for (i=0; i<=15; i++)
1326 if_printf((id->config & (1<<i)),"%s", cfg_str[i]);
1328 printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
1329 id->cyls, id->heads, id->sectors, id->track_bytes,
1330 id->sector_bytes, id->ecc_bytes);
1332 if (id->buf_type > 3)
1333 printf("%s%u", " BuffType=", id->buf_type);
1335 printf("%s%s", " BuffType=", BuffType[id->buf_type]);
1337 printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
1338 if (id->max_multsect)
1340 printf(", MultSect=");
1341 if (!(id->multsect_valid&1))
1342 printf("?%u?", id->multsect);
1343 else if (id->multsect)
1344 printf("%u", id->multsect);
1351 strcat(pmodes, "pio0 ");
1352 if_strcat((id->tPIO >= 1), pmodes, "pio1 ");
1353 if_strcat((id->tPIO >= 2), pmodes, "pio2 ");
1356 if_printf((!(id->field_valid&1))," (maybe):");
1357 #if __BYTE_ORDER == __BIG_ENDIAN
1358 capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
1360 capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
1362 printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
1363 id->cur_sectors, capacity ,
1364 ((id->capability&2)==0)?"no":"yes");
1366 if_printf((id->capability&2),", LBAsects=%u", id->lba_capacity);
1368 if (id->capability&1)
1370 if (id->dma_1word | id->dma_mword)
1372 if_strcat((id->dma_1word & 0x100), dmodes, "*");
1373 if_strcat((id->dma_1word & 1), dmodes, "sdma0 ");
1374 if_strcat((id->dma_1word & 0x200), dmodes, "*");
1375 if_strcat((id->dma_1word & 2), dmodes, "sdma1 ");
1376 if_strcat((id->dma_1word & 0x400), dmodes, "*");
1377 if_strcat((id->dma_1word & 4), dmodes, "sdma2 ");
1378 if_strcat((id->dma_1word & 0xf800), dmodes, "*");
1379 if_strcat((id->dma_1word & 0xf8), dmodes, "sdma? ");
1380 if_strcat((id->dma_mword & 0x100), dmodes, "*");
1381 if_strcat((id->dma_mword & 1), dmodes, "mdma0 ");
1382 if_strcat((id->dma_mword & 0x200), dmodes, "*");
1383 if_strcat((id->dma_mword & 2), dmodes, "mdma1 ");
1384 if_strcat((id->dma_mword & 0x400), dmodes, "*");
1385 if_strcat((id->dma_mword & 4), dmodes, "mdma2 ");
1386 if_strcat((id->dma_mword & 0xf800), dmodes, "*");
1387 if_strcat((id->dma_mword & 0xf8), dmodes, "mdma? ");
1390 printf("\n IORDY=");
1391 if (id->capability&8)
1392 printf((id->capability&4) ? "on/off" : "yes");
1396 if ((id->capability&8) || (id->field_valid&2))
1398 if (id->field_valid&2)
1400 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1401 if_strcat((id->eide_pio_modes & 1), pmodes, "pio3 ");
1402 if_strcat((id->eide_pio_modes & 2), pmodes, "pio4 ");
1403 if_strcat((id->eide_pio_modes &~3), pmodes, "pio? ");
1405 if (id->field_valid&4)
1407 if_strcat((id->dma_ultra & 0x100),umodes,"*");
1408 if_strcat((id->dma_ultra & 0x001),umodes,"udma0 ");
1409 if_strcat((id->dma_ultra & 0x200),umodes,"*");
1410 if_strcat((id->dma_ultra & 0x002),umodes,"udma1 ");
1411 if_strcat((id->dma_ultra & 0x400),umodes,"*");
1412 if_strcat((id->dma_ultra & 0x004),umodes,"udma2 ");
1413 #ifdef __NEW_HD_DRIVE_ID
1414 if (id->hw_config & 0x2000)
1416 #else /* !__NEW_HD_DRIVE_ID */
1417 if (id->word93 & 0x2000)
1419 #endif /* __NEW_HD_DRIVE_ID */
1420 if_strcat((id->dma_ultra & 0x0800),umodes,"*");
1421 if_strcat((id->dma_ultra & 0x0008),umodes,"udma3 ");
1422 if_strcat((id->dma_ultra & 0x1000),umodes,"*");
1423 if_strcat((id->dma_ultra & 0x0010),umodes,"udma4 ");
1424 if_strcat((id->dma_ultra & 0x2000),umodes,"*");
1425 if_strcat((id->dma_ultra & 0x0020),umodes,"udma5 ");
1426 if_strcat((id->dma_ultra & 0x4000),umodes,"*");
1427 if_strcat((id->dma_ultra & 0x0040),umodes,"udma6 ");
1428 if_strcat((id->dma_ultra & 0x8000),umodes,"*");
1429 if_strcat((id->dma_ultra & 0x0080),umodes,"udma7 ");
1433 if_printf(((id->capability&1) && (id->field_valid&2)),
1434 ", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1435 printf("\n PIO modes: %s", pmodes);
1436 if_printf((*dmodes),"\n DMA modes: %s", dmodes);
1437 if_printf((*umodes),"\n UDMA modes: %s", umodes);
1439 printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
1440 if (id_regs[83] & 8)
1442 if (!(id_regs[86]&8))
1443 printf(": disabled (255)");
1444 else if ((id_regs[91]&0xFF00)!=0x4000)
1445 printf(": unknown setting");
1447 printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1449 if_printf( (id_regs[82]&0x20)," WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
1450 #ifdef __NEW_HD_DRIVE_ID
1451 if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
1453 printf("\n Drive conforms to: ");
1454 if_else_printf((id->minor_rev_num <= 31),"%s: ","unknown: ", minor_str[id->minor_rev_num]);
1455 if (id->major_rev_num < 31)
1457 for (i=0; i <= 15; i++)
1458 if_printf((id->major_rev_num & (1<<i))," %u", i);
1461 #endif /* __NEW_HD_DRIVE_ID */
1462 printf("\n\n * signifies the current active mode\n\n");
1466 static void flush_buffer_cache (int fd)
1468 fsync (fd); /* flush buffers */
1469 bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
1470 #ifdef HDIO_DRIVE_CMD
1472 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
1473 bb_error_msg("HDIO_DRIVE_CMD");
1477 static int seek_to_zero (int fd)
1479 if (lseek(fd, (off_t) 0, SEEK_SET))
1484 static int read_big_block (int fd, char *buf)
1489 if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES)
1497 string = "read() hit EOF - device too small";
1500 string = "read(%u) returned %u bytes";
1502 bb_error_msg(string, TIMING_BUF_BYTES, rc);
1506 /* access all sectors of buf to ensure the read fully completed */
1507 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1512 static double correction = 0.0;
1514 void do_time (int flag, int fd)
1517 flag = 1 time_device
1523 struct itimerval e1, e2;
1525 int timing_MB = TIMING_MB;
1527 if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1)
1529 bb_error_msg (bb_msg_shared_mem,"allocate"); /*"could not allocate sharedmem buf"*/
1532 if (shmctl(shmid, SHM_LOCK, NULL) == -1)
1534 bb_error_msg (bb_msg_shared_mem,"lock"); /*"could not lock sharedmem buf"*/
1535 (void) shmctl(shmid, IPC_RMID, NULL);
1538 if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1)
1540 bb_error_msg (bb_msg_shared_mem,"attach"); /*"could not attach sharedmem buf"*/
1541 (void) shmctl(shmid, IPC_RMID, NULL);
1544 if (shmctl(shmid, IPC_RMID, NULL) == -1)
1545 bb_error_msg ("shmctl(,IPC_RMID,)");
1547 /* Clear out the device request queues & give them time to complete */
1550 if(flag == 0) /* Time cache */
1552 /* Calculate a correction factor for the basic
1553 * overhead of doing a read() from the buffer cache.
1554 * To do this, we read the data once to "cache it" and
1555 * to force full preallocation of our timing buffer,
1556 * and then we re-read it 10 times while timing it.
1558 * getitimer() is used rather than gettimeofday() because
1559 * it is much more consistent (on my machine, at least).
1561 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1562 if (seek_to_zero (fd))
1564 if (read_big_block (fd, buf))
1566 printf(" Timing buffer-cache reads: ");
1569 /* Clear out the device request queues & give them time to complete */
1572 /* Time re-reading from the buffer-cache */
1573 getitimer(ITIMER_REAL, &e1);
1574 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1576 if (seek_to_zero (fd))
1578 if (read_big_block (fd, buf))
1581 getitimer(ITIMER_REAL, &e2);
1582 correction = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1584 /* Now remove the lseek() from the correction factor */
1585 getitimer(ITIMER_REAL, &e1);
1586 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1588 if (seek_to_zero (fd))
1591 getitimer(ITIMER_REAL, &e2);
1592 correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
1593 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1595 if ((BUFCACHE_FACTOR * timing_MB) >= correction) /* more than 1MB/s */
1596 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1597 (BUFCACHE_FACTOR * timing_MB), correction,
1598 (BUFCACHE_FACTOR * timing_MB) / correction);
1600 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1601 (BUFCACHE_FACTOR * timing_MB), correction,
1602 (BUFCACHE_FACTOR * timing_MB) / correction * 1024);
1603 correction /= BUFCACHE_FACTOR;
1605 flush_buffer_cache(fd);
1608 else /* Time device */
1610 printf(" Timing buffered disk reads: ");
1614 * getitimer() is used rather than gettimeofday() because
1615 * it is much more consistent (on my machine, at least).
1617 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1619 /* Now do the timings for real */
1620 getitimer(ITIMER_REAL, &e1);
1621 for (i = TIMING_BUF_COUNT; i > 0; --i)
1623 if (read_big_block (fd, buf))
1626 getitimer(ITIMER_REAL, &e2);
1628 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1630 if (timing_MB >= elapsed) /* more than 1MB/s */
1631 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",timing_MB, elapsed, timing_MB / elapsed);
1633 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",timing_MB, elapsed, timing_MB / elapsed * 1024);
1635 /*"Hmm.. suspicious results: probably not enough free memory for a proper test.");*/
1636 if (elapsed <= (correction * 2))
1637 bb_error_msg(bb_msg_memory_exhausted);
1639 #if 0 /* the "estimate" is just plain wrong for many systems.. */
1640 else if (correction != 0.0) {
1641 printf(" Estimating raw driver speed: ");
1642 elapsed -= correction;
1643 if (timing_MB >= elapsed) /* more than 1MB/s */
1644 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1645 timing_MB, elapsed, timing_MB / elapsed);
1647 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1648 timing_MB, elapsed, timing_MB / elapsed * 1024);
1653 if (-1 == shmdt(buf))
1654 bb_error_msg (bb_msg_shared_mem,"detach"); /*"could not detach sharedmem buf"*/
1658 static void no_scsi (void)
1660 /*" operation not supported on SCSI disks"*/
1662 bb_error_msg_and_die(bb_msg_op_not_supp,"SCSI");
1665 static void no_xt (void)
1668 bb_error_msg_and_die(bb_msg_op_not_supp,"XT");
1671 static void on_off (unsigned int value)
1673 printf(value ? " (on)\n" : " (off)\n");
1676 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1677 static void bus_state_value (unsigned int value)
1687 string = " (off)\n";
1689 case BUSSTATE_TRISTATE:
1690 string = " (tristate)\n";
1693 string = " (unknown: %d)\n";
1696 printf(string, value);
1700 #ifdef HDIO_DRIVE_CMD
1701 static void interpret_standby (unsigned int standby)
1710 printf("21 minutes");
1713 printf("vendor-specific");
1716 printf("?reserved");
1719 printf("21 minutes + 15 seconds");
1724 unsigned int secs = standby * 5;
1725 unsigned int mins = secs / 60;
1727 if_printf(mins,"%u minutes", mins);
1728 if_printf((mins && secs)," + ");
1729 if_printf(secs,"%u seconds", secs);
1731 else if (standby <= 251)
1733 unsigned int mins = (standby - 240) * 30;
1734 unsigned int hrs = mins / 60;
1736 if_printf(hrs,"%u hours", hrs);
1737 if_printf((hrs && mins)," + ");
1738 if_printf(mins,"%u minutes", mins);
1741 printf("illegal value");
1747 struct xfermode_entry {
1752 static const struct xfermode_entry xfermode_table[] = {
1788 static int translate_xfermode(char * name)
1790 const struct xfermode_entry *tmp;
1795 for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1797 if (!strcmp(name, tmp->name))
1802 val = strtol(name, &endptr, 10);
1803 if (*endptr == '\0')
1809 static void interpret_xfermode (unsigned int xfermode)
1814 printf("default PIO mode");
1817 printf("default PIO mode, disable IORDY");
1827 printf("PIO flow control mode%u", xfermode-8);
1837 printf("singleword DMA mode%u", xfermode-16);
1847 printf("multiword DMA mode%u", xfermode-32);
1857 printf("UltraDMA mode%u", xfermode-64);
1860 printf("unknown, probably not valid");
1865 #endif /* HDIO_DRIVE_CMD */
1868 #define VXVM_MAJOR 199
1872 #define CCISS_MAJOR 104
1875 static void process_dev (char *devname)
1878 static long parm, multcount;
1879 struct stat stat_buf;
1880 #ifndef HDIO_DRIVE_CMD
1881 int force_operation = 0;
1883 if (stat(devname,&stat_buf))
1884 bb_perror_msg_and_die(devname);
1886 switch(major(stat_buf.st_rdev))
1888 #ifdef SCSI_DISK0_MAJOR
1889 case (SCSI_DISK0_MAJOR):
1890 case (SCSI_DISK1_MAJOR):
1891 case (SCSI_DISK2_MAJOR):
1892 case (SCSI_DISK3_MAJOR):
1893 case (SCSI_DISK4_MAJOR):
1894 case (SCSI_DISK5_MAJOR):
1895 case (SCSI_DISK6_MAJOR):
1896 case (SCSI_DISK7_MAJOR):
1898 case (SCSI_DISK_MAJOR):
1904 #ifdef LVM_BLK_MAJOR
1905 case (LVM_BLK_MAJOR):
1910 #ifdef XT_DISK_MAJOR
1911 case (XT_DISK_MAJOR):
1941 break; /* do nothing */
1943 bb_error_msg_and_die("%s not supported",devname);
1946 fd = bb_xopen (devname, O_RDONLY|O_NONBLOCK);
1947 if_printf( (!quiet),"\n%s:\n", devname);
1951 if_printf(get_readahead," setting fs readahead to %ld\n", readahead);
1952 bb_ioctl(fd, BLKRASET,(int *)readahead,"BLKRASET");
1954 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1955 if (unregister_hwif)
1958 printf(" attempting to unregister hwif#%u\n", hwif);
1959 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)hwif,"HDIO_UNREGISTER_HWIF");
1962 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1967 printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
1968 args[0] = hwif_data;
1969 args[1] = hwif_ctrl;
1971 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
1981 printf(" attempting to ");
1983 printf("auto-tune PIO mode\n");
1984 else if (piomode < 100)
1985 printf("set PIO mode to %d\n", piomode);
1986 else if (piomode < 200)
1987 printf("set MDMA mode to %d\n", (piomode-100));
1989 printf("set UDMA mode to %d\n", (piomode-200));
1991 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)piomode, "HDIO_SET_PIO_MODE");
1997 if_printf(get_io32bit," setting 32-bit IO_support flag to %ld\n", io32bit);
1998 bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
2004 if_printf(get_mult, " setting multcount to %ld\n", mult);
2005 if(ioctl(fd, HDIO_SET_MULTCOUNT, mult))
2006 bb_error_msg("HDIO_SET_MULTCOUNT");
2007 #ifndef HDIO_DRIVE_CMD
2009 force_operation = 1;
2014 if_printf_on_off(get_readonly," setting readonly to %ld", readonly);
2015 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
2021 if_printf_on_off(get_unmask," setting unmaskirq to %ld", unmask);
2022 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
2024 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2028 if_printf_on_off(get_dma," setting using_dma to %ld", dma);
2029 bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
2031 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
2035 if_printf_on_off(get_dma_q," setting DMA queue_depth to %ld", dma_q);
2036 bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
2042 if_printf_on_off(get_nowerr," setting nowerr to %ld", nowerr);
2043 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
2049 if_printf_on_off(get_keep," setting keep_settings to %ld", keep);
2050 bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
2052 #ifdef HDIO_DRIVE_CMD
2055 unsigned char args[4] = {0,0,0,0};
2059 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
2060 if_printf_on_off(get_doorlock," setting drive doorlock to %ld", doorlock);
2061 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
2065 /* lock/unlock the drive's "feature" settings */
2066 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2070 if_printf_on_off(get_dkeep," setting drive keep features to %ld", dkeep);
2071 args[2] = dkeep ? 0x66 : 0xcc;
2072 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
2076 unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
2078 args[2] = defects ? 0x04 : 0x84;
2079 if_printf(get_defects," setting drive defect-mgmt to %ld\n", defects);
2080 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
2084 unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
2089 if_printf(get_prefetch," setting drive prefetch to %ld\n", prefetch);
2090 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
2094 unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
2098 args[1] = xfermode_requested;
2101 printf(" setting xfermode to %d", xfermode_requested);
2102 interpret_xfermode(xfermode_requested);
2104 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
2108 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2112 args[2] = lookahead ? 0xaa : 0x55;
2113 if_printf_on_off(get_lookahead," setting drive read-lookahead to %ld", lookahead);
2114 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
2118 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2120 check_if_min_and_set_val(apmmode,1);
2121 check_if_maj_and_set_val(apmmode,255);
2122 if_printf(get_apmmode," setting APM level to");
2125 /* disable Advanced Power Management */
2126 args[2] = 0x85; /* feature register */
2127 if_printf(get_apmmode," disabled\n");
2131 /* set Advanced Power Management mode */
2132 args[2] = 0x05; /* feature register */
2133 args[1] = apmmode; /* sector count register */
2134 if_printf(get_apmmode," 0x%02lX (%ld)\n",apmmode,apmmode);
2136 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
2140 #ifdef DO_FLUSHCACHE
2141 #ifndef WIN_FLUSHCACHE
2142 #define WIN_FLUSHCACHE 0xe7
2144 unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
2145 #endif /* DO_FLUSHCACHE */
2146 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2149 args[2] = wcache ? 0x02 : 0x82;
2150 if_printf_on_off(get_wcache," setting drive write-caching to %ld", wcache);
2151 #ifdef DO_FLUSHCACHE
2152 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
2153 bb_error_msg ("HDIO_DRIVE_CMD(flushcache)");
2154 #endif /* DO_FLUSHCACHE */
2155 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
2156 #ifdef DO_FLUSHCACHE
2157 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
2158 bb_error_msg ("HDIO_DRIVE_CMD(flushcache)");
2159 #endif /* DO_FLUSHCACHE */
2163 #ifndef WIN_STANDBYNOW1
2164 #define WIN_STANDBYNOW1 0xE0
2166 #ifndef WIN_STANDBYNOW2
2167 #define WIN_STANDBYNOW2 0x94
2169 unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
2170 unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
2172 if_printf(get_standbynow," issuing standby command\n");
2173 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2174 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
2175 bb_error_msg("HDIO_DRIVE_CMD(standby)");
2179 #ifndef WIN_SLEEPNOW1
2180 #define WIN_SLEEPNOW1 0xE6
2182 #ifndef WIN_SLEEPNOW2
2183 #define WIN_SLEEPNOW2 0x99
2185 unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
2186 unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
2188 if_printf(get_sleepnow," issuing sleep command\n");
2189 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2190 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
2191 bb_error_msg("HDIO_DRIVE_CMD(sleep)");
2195 unsigned char args[4] = {0xfb,0,0,0};
2198 if_printf(get_seagate," disabling Seagate auto powersaving mode\n");
2199 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
2203 unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
2208 printf(" setting standby to %lu", standby_requested);
2209 interpret_standby(standby_requested);
2211 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
2213 #else /* HDIO_DRIVE_CMD */
2214 if (force_operation)
2217 flush_buffer_cache(fd);
2218 if (-1 == read(fd, buf, sizeof(buf)))
2219 bb_error_msg("access failed");
2221 #endif /* HDIO_DRIVE_CMD */
2226 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity)
2230 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
2232 if ((verbose && !is_xt_hd) || get_mult)
2233 bb_error_msg("HDIO_GET_MULTCOUNT");
2235 else if (verbose | get_mult)
2237 printf(" multcount = %2ld", multcount);
2241 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit)
2245 if(ioctl(fd, HDIO_GET_32BIT, &parm))
2246 bb_error_msg("HDIO_GET_32BIT");
2249 printf(" IO_support =%3ld (", parm);
2255 printf("16-bit)\n");
2258 printf("32-bit)\n");
2261 printf("32-bit w/sync)\n");
2264 printf("Request-Queue-Bypass)\n");
2267 printf("\?\?\?)\n");
2272 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask)
2276 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
2277 "HDIO_GET_UNMASKINTR"," unmaskirq = %2ld");
2281 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2282 if ((verbose && !is_scsi_hd) || get_dma) {
2284 if(ioctl(fd, HDIO_GET_DMA, &parm))
2285 bb_error_msg("HDIO_GET_DMA");
2288 printf(" using_dma = %2ld", parm);
2290 printf(" (DMA-Assisted-PIO)\n");
2299 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
2300 "HDIO_GET_QDMA"," queue_depth = %2ld");
2302 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep)
2306 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
2307 "HDIO_GET_KEEPSETTINGS"," keepsettings = %2ld");
2314 bb_ioctl_on_off (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
2315 " HDIO_GET_NOWERR"," nowerr = %2ld");
2317 if (verbose || get_readonly)
2319 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
2320 " BLKROGET"," readonly = %2ld");
2322 if ((verbose && !is_scsi_hd) || get_readahead)
2324 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
2325 " BLKRAGET"," readahead = %2ld");
2327 if (verbose || get_geom)
2329 static const char msg[] = " geometry = %u/%u/%u, sectors = %ld, start = %ld\n";
2330 static struct hd_geometry g;
2331 #ifdef HDIO_GETGEO_BIG
2332 static struct hd_big_geometry bg;
2335 if (ioctl(fd, BLKGETSIZE, &parm))
2336 bb_error_msg("BLKGETSIZE");
2337 #ifdef HDIO_GETGEO_BIG
2338 else if (!ioctl(fd, HDIO_GETGEO_BIG, &bg))
2339 printf(msg, bg.cylinders, bg.heads, bg.sectors, parm, bg.start);
2341 else if (ioctl(fd, HDIO_GETGEO, &g))
2342 bb_error_msg("HDIO_GETGEO");
2344 printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
2346 #ifdef HDIO_DRIVE_CMD
2349 #ifndef WIN_CHECKPOWERMODE1
2350 #define WIN_CHECKPOWERMODE1 0xE5
2352 #ifndef WIN_CHECKPOWERMODE2
2353 #define WIN_CHECKPOWERMODE2 0x98
2355 unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
2358 if (ioctl(fd, HDIO_DRIVE_CMD, &args)
2359 && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
2360 && ioctl(fd, HDIO_DRIVE_CMD, &args))
2362 if (errno != EIO || args[0] != 0 || args[1] != 0)
2368 state = (args[2] == 255) ? "active/idle" : "standby";
2370 printf(" drive state is: %s\n", state);
2373 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2378 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
2380 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2381 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2382 if (perform_tristate)
2384 unsigned char args[4] = {0,tristate,0,0};
2387 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
2389 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2390 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2393 static struct hd_driveid id;
2398 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
2400 if (multcount != -1)
2402 id.multsect = multcount;
2403 id.multsect_valid |= 1;
2406 id.multsect_valid &= ~1;
2409 else if (errno == -ENOMSG)
2410 printf(" no identification info available\n");
2412 bb_error_msg("HDIO_GET_IDENTITY");
2417 unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
2423 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2425 args[0] = WIN_PIDENTIFY;
2426 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2428 bb_error_msg("HDIO_DRIVE_CMD(identify)");
2429 goto identify_abort;
2432 for(i=0; i<(sizeof args)/2; i+=2)
2433 __le16_to_cpus((uint16_t *)(&args[i]));
2435 identify((void *)&args[4], NULL);
2440 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2446 printf(" setting bus state to %d", busstate);
2447 bus_state_value(busstate);
2449 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)busstate, "HDIO_SET_BUSSTATE");
2452 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2456 if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
2457 bb_error_msg("HDIO_GET_BUSSTATE");
2460 printf(" busstate = %2ld", parm);
2461 bus_state_value(parm);
2466 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2470 do_time(0,fd); /*time cache */
2472 do_time(1,fd); /*time device */
2474 flush_buffer_cache (fd);
2478 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2479 static int fromhex (unsigned char c)
2481 if (c >= 'a' && c <= 'f')
2482 return 10 + (c - 'a');
2483 if (c >= '0' && c <= '9')
2485 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2488 static int identify_from_stdin (void)
2490 unsigned short sbuf[800];
2491 unsigned char buf[1600], *b = (unsigned char *)buf;
2492 int i, count = read(0, buf, 1280);
2495 bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
2496 for (i = 0; count >= 4; ++i)
2498 sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
2499 __le16_to_cpus((uint16_t *)(&sbuf[i]));
2503 identify(sbuf, NULL);
2508 /* our main() routine: */
2509 int hdparm_main(int argc, char **argv)
2511 const char * const bb_msg_missing_value ="missing value";
2523 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2524 if (!strcmp("-Istdin", *argv))
2526 exit(identify_from_stdin());
2540 /*bb_error_msg_and_die("%s", VERSION);*/
2541 /* We have to return 0 here and not 1 */
2542 printf("%s %s\n",bb_applet_name, VERSION);
2547 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2568 p = check_ptr(p,argc,argv);
2569 if((set_unmask = set_flag(p,'1'))==1)
2570 unmask = *p++ - '0';
2572 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2576 p = check_ptr(p,argc,argv);
2577 if((set_dma = set_flag(p,'9'))==1)
2580 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
2584 p = check_ptr(p,argc,argv);
2585 if((set_nowerr = set_flag(p,'1'))==1)
2586 nowerr = *p++ - '0';
2589 noisy_piomode = noisy;
2591 p = check_ptr_v2(p,argc,argv);
2594 while (isalnum(*p) && (tmpstr - name) < 31)
2600 piomode = translate_xfermode(name);
2607 get_readonly = noisy;
2609 p = check_ptr(p,argc,argv);
2610 if((set_readonly = set_flag(p,'1'))==1)
2611 readonly = *p++ - '0';
2616 p = check_ptr(p,argc,argv);
2620 mult = (mult * 10) + (*p++ - '0');
2624 get_io32bit = noisy;
2626 p = check_ptr(p,argc,argv);
2630 io32bit = (io32bit * 10) + (*p++ - '0');
2633 #ifdef HDIO_DRIVE_CMD
2635 get_standby = noisy;
2637 p = check_ptr(p,argc,argv);
2641 standby_requested = (standby_requested * 10) + (*p++ - '0');
2644 bb_error_msg("-S: %s", bb_msg_missing_value);
2648 get_defects = noisy;
2650 p = check_ptr(p,argc,argv);
2654 defects = (defects * 10) + (*p++ - '0');
2657 bb_error_msg("-D: %s", bb_msg_missing_value);
2660 get_prefetch = noisy;
2662 p = check_ptr(p,argc,argv);
2666 prefetch = (prefetch * 10) + (*p++ - '0');
2669 bb_error_msg("-P: %s", bb_msg_missing_value);
2673 get_xfermode = noisy;
2675 p = check_ptr_v2(p,argc,argv);
2678 while (isalnum(*p) && (tmpstr - name) < 31)
2684 xfermode_requested = translate_xfermode(name);
2685 if (xfermode_requested == -1)
2690 bb_error_msg("-X: %s", bb_msg_missing_value);
2696 p = check_ptr(p,argc,argv);
2697 if((set_dkeep = set_flag(p,'1'))==1)
2700 bb_error_msg("-K: %s (0/1)", bb_msg_missing_value);
2704 get_lookahead = noisy;
2706 p = check_ptr(p,argc,argv);
2707 if((set_lookahead = set_flag(p,'1'))==1)
2708 lookahead = *p++ - '0';
2710 bb_error_msg("-A: %s (0/1)", bb_msg_missing_value);
2714 get_doorlock = noisy;
2716 p = check_ptr(p,argc,argv);
2717 if((set_doorlock = set_flag(p,'1'))==1)
2718 doorlock = *p++ - '0';
2720 bb_error_msg("-L: %s (0/1)", bb_msg_missing_value);
2726 p = check_ptr(p,argc,argv);
2727 if((set_wcache = set_flag(p,'1'))==1)
2728 wcache = *p++ - '0';
2730 bb_error_msg("-W: %s (0/1)", bb_msg_missing_value);
2734 get_powermode = noisy;
2739 get_standbynow = noisy;
2745 get_sleepnow = noisy;
2755 get_seagate = noisy;
2759 #endif /* HDIO_DRIVE_CMD */
2763 p = check_ptr(p,argc,argv);
2764 if((set_keep = set_flag(p,'1'))==1)
2767 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
2769 p = check_ptr(p,argc,argv);
2771 goto error; /* "expected hwif_nr" */
2773 sscanf(p++, "%i", &hwif);
2775 unregister_hwif = 1;
2777 #endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
2778 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2780 p = check_ptr(p,argc,argv);
2782 goto error; /* "expected hwif_data" */
2784 sscanf(p++, "%i", &hwif_data);
2786 if (argc && isdigit(**argv))
2787 p = *argv++, --argc;
2789 goto error; /* "expected hwif_ctrl" */
2791 sscanf(p, "%i", &hwif_ctrl);
2793 if (argc && isdigit(**argv))
2794 p = *argv++, --argc;
2797 bb_error_msg_and_die("expected hwif value"); /* "expected hwif_irq" */
2799 sscanf(p, "%i", &hwif_irq);
2805 #endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2810 p = check_ptr(p,argc,argv);
2814 dma_q = (dma_q * 10) + (*p++ - '0');
2816 /* what is this for ? as neg = 0 (see above) it seems to do nothing */
2820 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2824 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2825 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2827 p = check_ptr(p,argc,argv);
2828 if((perform_tristate = set_flag(p,'1'))==1)
2829 tristate = *p++ - '0';
2831 bb_error_msg("-x: %s (0/1)", bb_msg_missing_value);
2834 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2836 get_readahead = noisy;
2838 p = check_ptr(p,argc,argv);
2842 readahead = (readahead * 10) + (*p++ - '0');
2846 get_apmmode = noisy;
2848 p = check_ptr(p,argc,argv);
2852 apmmode = (io32bit * 10) + (*p++ - '0');
2855 printf("-B: %s (1-255)", bb_msg_missing_value);
2865 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2867 get_busstate = noisy;
2869 p = check_ptr(p,argc,argv);
2870 if((set_busstate = set_flag(p,'2'))==1)
2871 busstate = *p++ - '0';