1 /* vi: set sw=4 ts=4: */
3 * hdparm implementation for busybox
6 * Copyright (C) [2003] by [Matteo Croce] <pinnolo@tin.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);
510 static void if_else_printf(unsigned long i, char *fmt1, char *fmt2, ... )
521 static void print_ascii(uint16_t *p, uint8_t length);
523 static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
527 printf("\t%-20s",string);
528 print_ascii(&val[i], n);
532 static void sync_and_sleep(int i)
538 static void check_if_min_and_set_val(uint16_t a, uint16_t b)
544 static void check_if_maj_and_set_val(uint16_t a, uint16_t b)
550 char * check_ptr(char *p, int argc, char **argv)
552 if (!*p && argc && isdigit(**argv))
557 char * check_ptr_v2(char *p, int argc, char **argv)
559 if (!*p && argc && isalnum(**argv))
564 unsigned long int set_flag(char *p, char max)
566 if (*p >= '0' && *p <= max )
571 static void if_strcat(unsigned long test, char *modes, char *string)
574 strcat(modes,string);
577 /* end of busybox specific stuff */
579 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
580 static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
585 for(ii = 0; ii <= MODE_MAX; ii++)
587 if(mode_sel & 0x0001)
589 printf("*%cdma%u ",cc,ii);
594 else if(mode_sup & 0x0001)
595 printf("%cdma%u ",cc,ii);
603 static void print_ascii(uint16_t *p, uint8_t length) {
607 /* find first non-space & print it */
608 for(ii = 0; ii< length; ii++)
610 if(((char) 0x00ff&((*p)>>8)) != ' ')
612 if((cl = (char) 0x00ff&(*p)) != ' ')
614 if_printf((cl != '\0'),"%c",cl);
622 for(; ii< length; ii++)
625 break; /* some older devices have NULLs */
626 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
632 /* identify() is the only extern function used across two source files. The
633 others, though, were declared in hdparm.c with global scope; since other
634 functions in that file have static (file) scope, I assume the difference is
636 static void identify (uint16_t *id_supplied, const char *devname)
639 char *id_file = NULL;
641 uint16_t val[256], ii, jj, kk;
642 uint16_t like_std = 1, std = 0, min_std = 0xffff;
643 uint16_t dev = NO_DEV, eqpt = NO_DEV;
644 uint8_t have_mode = 0, err_dma = 0;
646 uint32_t ll, mm, nn, oo;
647 __u64 bbbig; /* (:) */
651 #if __BYTE_ORDER == __BIG_ENDIAN
652 swab(id_supplied, val, sizeof(val));
654 memcpy(val, id_supplied, sizeof(val));
659 id_file = xcalloc(1, strlen(devname)+22);
660 sprintf(id_file, "/proc/ide/%s/identify", devname);
661 /* open the file, read in all the info and close it */
665 fl = bb_xfopen(id_file, "r");
667 /* calculate checksum over all bytes */
668 for(ii = GEN_CONFIG; ii<=INTEGRITY; ii++)
670 unsigned int scratch;
671 if(1 != fscanf(fl,"%04x",&scratch))
673 val[ii] = (uint16_t)scratch;
674 chksum += val[ii] + (val[ii] >> 8);
677 /*bb_fclose_nonstdin(fl);*/
678 if(ii < (INTEGRITY+1))
679 bb_error_msg_and_die("Input file wrong format or length");
683 /* check if we recognise the device type */
685 if(!(val[GEN_CONFIG] & NOT_ATA))
688 printf("ATA device, with ");
690 else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
694 printf("CompactFlash ATA device, with ");
696 else if(!(val[GEN_CONFIG] & NOT_ATAPI))
699 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
700 printf("ATAPI %s, with ", pkt_str[eqpt]);
704 /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
705 bb_error_msg_and_die("Unknown device type");
707 if_printf(!(val[GEN_CONFIG] & MEDIA_REMOVABLE),"non-");
708 printf("removable media\n");
710 /* Info from the specific configuration word says whether or not the
711 * ID command completed correctly. It is only defined, however in
712 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
713 * standards. Since the values allowed for this word are extremely
714 * specific, it should be safe to check it now, even though we don't
715 * know yet what standard this device is using.
717 if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
718 (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
721 if_printf(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)),
722 "powers-up in standby; SET FEATURES subcmd spins-up.\n");
723 if_printf((((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE)),
724 "\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
727 /* output the model and serial numbers and the fw revision */
728 xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
729 xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
730 xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
731 xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
732 xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
734 /* major & minor standards version number (Note: these words were not
735 * defined until ATA-3 & the CDROM std uses different words.) */
736 printf("Standards:");
739 if(val[MINOR] && (val[MINOR] <= MINOR_MAX))
741 check_if_min_and_set_val( like_std, 3);
742 std = actual_ver[val[MINOR]];
743 if_printf(std,"\n\tUsed: %s ",minor_str[val[MINOR]]);
746 /* looks like when they up-issue the std, they obsolete one;
747 * thus, only the newest 4 issues need be supported. (That's
748 * what "kk" and "min_std" are all about.) */
749 if(val[MAJOR] && (val[MAJOR] !=NOVAL_1))
751 printf("\n\tSupported: ");
752 jj = val[MAJOR] << 1;
753 kk = like_std >4 ? like_std-4: 0;
754 for(ii = 14; (ii >0)&&(ii>kk); ii--)
762 kk = like_std >4 ? like_std-4: 0;
764 check_if_maj_and_set_val(min_std, ii);
768 check_if_min_and_set_val( like_std, 3);
770 /* Figure out what standard the device is using if it hasn't told
771 * us. If we know the std, check if the device is using any of
772 * the words from the next level up. It happens.
774 check_if_min_and_set_val( like_std, std);
776 if(((std == 5) || (!std && (like_std < 6))) &&
777 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
778 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
779 ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
780 ( val[CMDS_SUPP_2] & CMDS_W84) ) ) )
784 else if(((std == 4) || (!std && (like_std < 5))) &&
785 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
786 (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
787 ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
788 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
792 else if(((std == 3) || (!std && (like_std < 4))) &&
793 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
794 ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
795 (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
796 (( val[CAPAB_1] & VALID) == VALID_VAL) ||
797 (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
798 (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
802 else if(((std == 2) || (!std && (like_std < 3))) &&
803 ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
807 else if(((std == 1) || (!std && (like_std < 2))) &&
808 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
809 (val[WHATS_VALID] & OK_W64_70)) )
814 printf("\n\tLikely used: %u\n",like_std);
815 else if(like_std > std)
816 printf("& some of %u\n",like_std);
822 /* TBD: do CDROM stuff more thoroughly. For now... */
824 if(val[CDR_MINOR] == 9)
827 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
829 if(val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
832 printf("\n\tSupported: CD-ROM ATAPI");
833 jj = val[CDR_MAJOR] >> 1;
834 for(ii = 1; ii <15; ii++)
836 if_printf((jj & 0x0001),"-%u ", ii);
840 if_else_printf((!kk),"\n\tLikely used CD-ROM ATAPI-1\n","\n");
841 /* the cdrom stuff is more like ATA-2 than anything else, so: */
845 if(min_std == 0xffff)
846 min_std = like_std > 4 ? like_std - 3 : 1;
848 printf("Configuration:\n");
849 /* more info from the general configuration word */
850 if((eqpt != CDROM) && (like_std == 1))
852 jj = val[GEN_CONFIG] >> 1;
853 for(ii = 1; ii < 15; ii++)
855 if_printf((jj & 0x0001),"\t%s\n",ata1_cfg_str[ii]);
861 printf("\tDRQ response: "); /* Data Request (DRQ) */
862 switch(val[GEN_CONFIG] & DRQ_RESPONSE_TIME)
868 printf("<=10ms with INTRQ\n");
874 printf("unknown.\n");
877 printf("\tPacket size: ");
878 switch(val[GEN_CONFIG] & PKT_SIZE_SUPPORTED)
880 case PKT_SIZE_12_VAL :
881 printf("12 bytes\n");
883 case PKT_SIZE_16_VAL :
884 printf("16 bytes\n");
893 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
894 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
896 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
897 printf("\tCHS addressing not supported\n");
900 jj = val[WHATS_VALID] & OK_W54_58;
901 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",
902 val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
904 if_printf(((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES])),
905 "\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
909 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
912 /* check Endian of capacity bytes */
913 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
914 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
915 if(abs(mm - nn) > abs(oo - nn))
918 printf("\tCHS current addressable sectors:%11u\n",mm);
922 printf("\tLBA user addressable sectors:%11u\n",ll);
923 if( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
924 (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
926 bbbig = (__u64)val[LBA_64_MSB] << 48 |
927 (__u64)val[LBA_48_MSB] << 32 |
928 (__u64)val[LBA_MID] << 16 |
930 printf("\tLBA48 user addressable sectors:%11llu\n",bbbig);
934 bbbig = (__u64)(ll>mm ? ll : mm); /* # 512 byte blocks */
935 printf("\tdevice size with M = 1024*1024: %11llu MBytes\n",bbbig>>11);
936 bbbig = (bbbig<<9)/1000000;
937 printf("\tdevice size with M = 1000*1000: %11llu MBytes ",bbbig);
939 if_else_printf((bbbig > 1000),"(%llu GB)\n","\n",bbbig/1000);
943 /* hw support of commands (capabilities) */
944 printf("Capabilities:\n\t");
949 if_printf((val[CAPAB_0] & CMD_Q_SUP),"Cmd queuing, ");
951 if_printf((val[CAPAB_0] & OVLP_SUP),"Cmd overlap, ");
953 if_printf((val[CAPAB_0] & LBA_SUP),"LBA, ");
958 if_printf((!(val[CAPAB_0] & IORDY_SUP)),"(may be)");
959 if_else_printf((val[CAPAB_0] & IORDY_OFF),"(can","(cannot");
960 printf(" be disabled)\n");
963 printf("no IORDY\n");
965 if((like_std == 1) && val[BUF_TYPE])
968 printf("\tBuffer type: %04x: ",kk);
969 if_else_printf((kk < 2),"single port, single-sector","dual port, multi-sector");
970 if_printf((kk > 2)," with read caching ability");
974 if((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
976 printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
979 if((min_std < 4) && (val[RW_LONG]))
981 printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
984 if((eqpt != CDROM) && (like_std > 3))
986 printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
994 printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
997 printf("\tStandby timer values: spec'd by ");
998 if_else_printf((val[CAPAB_0] & STD_STBY),"Standard","Vendor");
999 if((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
1000 printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
1004 printf("\tR/W multiple sector transfer: ");
1005 if((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
1006 printf("not supported\n");
1009 printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
1010 if_else_printf((val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID),
1011 "%u\n","?\n",val[SECTOR_XFER_CUR] & SECTOR_XFER);
1013 if((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
1015 /* We print out elsewhere whether the APM feature is enabled or
1016 not. If it's not enabled, let's not repeat the info; just print
1018 printf("\tAdvancedPM level: ");
1019 if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
1021 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
1022 printf("%u (0x%x)\n", apm_level, apm_level);
1025 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
1029 if_printf(val[ACOUSTIC],"\tRecommended acoustic management value: %u, current value: %u\n",
1030 (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
1037 if_printf((val[CAPAB_0] & SWRST_REQ),"\tATA sw reset required\n");
1039 if(val[PKT_REL] || val[SVC_NBSY])
1041 printf("\tOverlap support:");
1042 if_printf(val[PKT_REL]," %uus to release bus.",val[PKT_REL]);
1043 if_printf(val[SVC_NBSY]," %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
1048 /* DMA stuff. Check that only one DMA mode is selected. */
1050 if(!(val[CAPAB_0] & DMA_SUP))
1051 printf("not supported\n");
1054 if_printf((val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA]),
1055 " sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
1058 jj = val[SINGLE_DMA];
1059 kk = val[SINGLE_DMA] >> 8;
1060 err_dma += mode_loop(jj,kk,'s',&have_mode);
1064 jj = val[MULTI_DMA];
1065 kk = val[MULTI_DMA] >> 8;
1066 err_dma += mode_loop(jj,kk,'m',&have_mode);
1068 if((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
1070 jj = val[ULTRA_DMA];
1071 kk = val[ULTRA_DMA] >> 8;
1072 err_dma += mode_loop(jj,kk,'u',&have_mode);
1074 if_printf((err_dma || !have_mode),"(?)");
1077 if_printf(((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)),
1078 "\t Interleaved DMA support\n");
1080 if((val[WHATS_VALID] & OK_W64_70) &&
1081 (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
1083 printf("\t Cycle time:");
1084 if_printf(val[DMA_TIME_MIN]," min=%uns",val[DMA_TIME_MIN]);
1085 if_printf(val[DMA_TIME_NORM]," recommended=%uns",val[DMA_TIME_NORM]);
1090 /* Programmed IO stuff */
1092 /* If a drive supports mode n (e.g. 3), it also supports all modes less
1093 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
1094 if((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
1096 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
1097 for(ii = 0; ii <= PIO_MODE_MAX ; ii++)
1099 if_printf((jj & 0x0001),"pio%d ",ii);
1104 else if(((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
1106 for(ii = 0; ii <= val[PIO_MODE]>>8; ii++)
1107 printf("pio%d ",ii);
1111 printf("unknown\n");
1113 if(val[WHATS_VALID] & OK_W64_70)
1115 if(val[PIO_NO_FLOW] || val[PIO_FLOW])
1117 printf("\t Cycle time:");
1118 if_printf(val[PIO_NO_FLOW]," no flow control=%uns", val[PIO_NO_FLOW]);
1119 if_printf(val[PIO_FLOW]," IORDY flow control=%uns", val[PIO_FLOW]);
1124 if((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
1126 printf("Commands/features:\n\tEnabled\tSupported:\n");
1127 jj = val[CMDS_SUPP_0];
1128 kk = val[CMDS_EN_0];
1129 for(ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
1131 if((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
1133 if_else_printf((kk & 0x8000),"\t *","\t");
1134 printf("\t%s\n",cmd_feat_str[ii]);
1139 jj = val[CMDS_SUPP_0+1+(ii/16)];
1140 kk = val[CMDS_EN_0+1+(ii/16)];
1144 if((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
1149 if_printf(((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP),
1150 "\tRemovable Media Status Notification feature set supported\n");
1154 if((eqpt != CDROM) && (like_std > 3) &&
1155 (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1157 printf("Security: \n");
1158 if_printf((val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1)),
1159 "\tMaster password revision code = %u\n",val[PSWD_CODE]);
1160 jj = val[SECU_STATUS];
1163 for(ii = 0; ii < NUM_SECU_STR; ii++)
1165 if_else_printf((!(jj & 0x0001)),"\tnot\t%s\n", "\t\t%s\n", secu_str[ii]);
1168 if(val[SECU_STATUS] & SECU_ENABLED)
1170 printf("\tSecurity level ");
1171 if_else_printf((val[SECU_STATUS] & SECU_LEVEL),"maximum\n","high\n");
1174 jj = val[ERASE_TIME] & ERASE_BITS;
1175 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
1179 if_printf(jj,"%umin for SECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1);
1180 if_printf(kk,"%umin for ENHANCED SECURITY ERASE UNIT.", kk==ERASE_BITS ? 508 : kk<<1);
1186 if((val[HWRST_RSLT] & VALID) == VALID_VAL)
1188 printf("HW reset results:\n");
1189 if_else_printf((val[HWRST_RSLT] & CBLID),"\tCBLID- above Vih\n","\tCBLID- below Vih\n");
1191 if(val[HWRST_RSLT] & RST0)
1193 printf("\tDevice num = 0");
1194 jj = val[HWRST_RSLT];
1198 printf("\tDevice num = 1");
1199 jj = val[HWRST_RSLT] >> 8;
1202 if((jj & DEV_DET) == JUMPER_VAL)
1203 printf(" determined by the jumper");
1204 else if((jj & DEV_DET) == CSEL_VAL)
1205 printf(" determined by CSEL");
1209 /* more stuff from std 5 */
1210 if((like_std > 4) && (eqpt != CDROM))
1212 if(val[CFA_PWR_MODE] & VALID_W160)
1214 printf("CFA power mode 1:\n\t");
1215 if_else_printf((val[CFA_PWR_MODE] & PWR_MODE_OFF),"disabled","enabled");
1217 if_printf((val[CFA_PWR_MODE] & PWR_MODE_REQ)," and required by some commands");
1220 if_printf((val[CFA_PWR_MODE] & MAX_AMPS),"\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
1222 if((val[INTEGRITY] & SIG) == SIG_VAL)
1224 printf("Checksum: ");
1225 if_printf(chksum,"in");
1226 printf("correct\n");
1234 static int verbose = 0, get_identity = 0, get_geom = 0, noisy = 1, quiet = 0;
1235 static int flagcount = 0, do_flush = 0, is_scsi_hd = 0, is_xt_hd = 0;
1236 static int do_ctimings, do_timings = 0;
1238 static unsigned long set_readahead= 0, get_readahead= 0, readahead= 0;
1239 static unsigned long set_readonly = 0, get_readonly = 0, readonly = 0;
1240 static unsigned long set_unmask = 0, get_unmask = 0, unmask = 0;
1241 static unsigned long set_mult = 0, get_mult = 0, mult = 0;
1242 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
1243 static unsigned long set_dma = 0, get_dma = 0, dma = 0;
1245 static unsigned long set_dma_q = 0, get_dma_q = 0, dma_q = 0;
1246 static unsigned long set_nowerr = 0, get_nowerr = 0, nowerr = 0;
1247 static unsigned long set_keep = 0, get_keep = 0, keep = 0;
1248 static unsigned long set_io32bit = 0, get_io32bit = 0, io32bit = 0;
1249 static unsigned long set_piomode = 0, noisy_piomode= 0;
1250 static int piomode = 0;
1251 #ifdef HDIO_DRIVE_CMD
1252 static unsigned long set_dkeep = 0, get_dkeep = 0, dkeep = 0;
1253 static unsigned long set_standby = 0, get_standby = 0, standby_requested= 0;
1254 static unsigned long set_xfermode = 0, get_xfermode = 0;
1255 static int xfermode_requested= 0;
1256 static unsigned long set_lookahead= 0, get_lookahead= 0, lookahead= 0;
1257 static unsigned long set_prefetch = 0, get_prefetch = 0, prefetch = 0;
1258 static unsigned long set_defects = 0, get_defects = 0, defects = 0;
1259 static unsigned long set_wcache = 0, get_wcache = 0, wcache = 0;
1260 static unsigned long set_doorlock = 0, get_doorlock = 0, doorlock = 0;
1261 static unsigned long set_seagate = 0, get_seagate = 0;
1262 static unsigned long set_standbynow = 0, get_standbynow = 0;
1263 static unsigned long set_sleepnow = 0, get_sleepnow = 0;
1264 static unsigned long get_powermode = 0;
1265 static unsigned long set_apmmode = 0, get_apmmode= 0, apmmode = 0;
1267 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1268 static int get_IDentity = 0;
1270 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1271 static int unregister_hwif = 0;
1272 static int hwif = 0;
1274 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1275 static int scan_hwif = 0;
1276 static int hwif_data = 0;
1277 static int hwif_ctrl = 0;
1278 static int hwif_irq = 0;
1280 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1281 static int set_busstate = 0, get_busstate = 0, busstate = 0;
1283 static int reread_partn = 0;
1285 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1286 static int perform_reset = 0;
1287 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1288 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1289 static int perform_tristate = 0, tristate = 0;
1290 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1292 // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1293 // then the HDIO_GET_IDENTITY only returned 142 bytes.
1294 // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1295 // and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1296 // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1297 // (which they should, but they should just return -EINVAL).
1299 // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1300 // On a really old system, it will not, and we will be confused.
1303 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1304 static const char *cfg_str[] =
1305 { "", " HardSect", " SoftSect", " NotMFM",
1306 " HdSw>15uSec", " SpinMotCtl", " Fixed", " Removeable",
1307 " DTR<=5Mbs", " DTR>5Mbs", " DTR>10Mbs", " RotSpdTol>.5%",
1308 " dStbOff", " TrkOff", " FmtGapReq", " nonMagnetic"
1311 static const char *BuffType[] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
1313 static void dump_identity (const struct hd_driveid *id)
1316 char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
1317 const unsigned short int *id_regs= (const void*) id;
1318 unsigned long capacity;
1320 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1321 id->model, id->fw_rev, id->serial_no);
1322 for (i=0; i<=15; i++)
1323 if_printf((id->config & (1<<i)),"%s", cfg_str[i]);
1325 printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
1326 id->cyls, id->heads, id->sectors, id->track_bytes,
1327 id->sector_bytes, id->ecc_bytes);
1329 if (id->buf_type > 3)
1330 printf("%s%u", " BuffType=", id->buf_type);
1332 printf("%s%s", " BuffType=", BuffType[id->buf_type]);
1334 printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
1335 if (id->max_multsect)
1337 printf(", MultSect=");
1338 if (!(id->multsect_valid&1))
1339 printf("?%u?", id->multsect);
1340 else if (id->multsect)
1341 printf("%u", id->multsect);
1348 strcat(pmodes, "pio0 ");
1349 if_strcat((id->tPIO >= 1), pmodes, "pio1 ");
1350 if_strcat((id->tPIO >= 2), pmodes, "pio2 ");
1353 if_printf((!(id->field_valid&1))," (maybe):");
1354 #if __BYTE_ORDER == __BIG_ENDIAN
1355 capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
1357 capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
1359 printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
1360 id->cur_sectors, capacity ,
1361 ((id->capability&2)==0)?"no":"yes");
1363 if_printf((id->capability&2),", LBAsects=%u", id->lba_capacity);
1365 if (id->capability&1)
1367 if (id->dma_1word | id->dma_mword)
1369 if_strcat((id->dma_1word & 0x100), dmodes, "*");
1370 if_strcat((id->dma_1word & 1), dmodes, "sdma0 ");
1371 if_strcat((id->dma_1word & 0x200), dmodes, "*");
1372 if_strcat((id->dma_1word & 2), dmodes, "sdma1 ");
1373 if_strcat((id->dma_1word & 0x400), dmodes, "*");
1374 if_strcat((id->dma_1word & 4), dmodes, "sdma2 ");
1375 if_strcat((id->dma_1word & 0xf800), dmodes, "*");
1376 if_strcat((id->dma_1word & 0xf8), dmodes, "sdma? ");
1377 if_strcat((id->dma_mword & 0x100), dmodes, "*");
1378 if_strcat((id->dma_mword & 1), dmodes, "mdma0 ");
1379 if_strcat((id->dma_mword & 0x200), dmodes, "*");
1380 if_strcat((id->dma_mword & 2), dmodes, "mdma1 ");
1381 if_strcat((id->dma_mword & 0x400), dmodes, "*");
1382 if_strcat((id->dma_mword & 4), dmodes, "mdma2 ");
1383 if_strcat((id->dma_mword & 0xf800), dmodes, "*");
1384 if_strcat((id->dma_mword & 0xf8), dmodes, "mdma? ");
1387 printf("\n IORDY=");
1388 if (id->capability&8)
1389 printf((id->capability&4) ? "on/off" : "yes");
1393 if ((id->capability&8) || (id->field_valid&2))
1395 if (id->field_valid&2)
1397 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1398 if_strcat((id->eide_pio_modes & 1), pmodes, "pio3 ");
1399 if_strcat((id->eide_pio_modes & 2), pmodes, "pio4 ");
1400 if_strcat((id->eide_pio_modes &~3), pmodes, "pio? ");
1402 if (id->field_valid&4)
1404 if_strcat((id->dma_ultra & 0x100),umodes,"*");
1405 if_strcat((id->dma_ultra & 0x001),umodes,"udma0 ");
1406 if_strcat((id->dma_ultra & 0x200),umodes,"*");
1407 if_strcat((id->dma_ultra & 0x002),umodes,"udma1 ");
1408 if_strcat((id->dma_ultra & 0x400),umodes,"*");
1409 if_strcat((id->dma_ultra & 0x004),umodes,"udma2 ");
1410 #ifdef __NEW_HD_DRIVE_ID
1411 if (id->hw_config & 0x2000)
1413 #else /* !__NEW_HD_DRIVE_ID */
1414 if (id->word93 & 0x2000)
1416 #endif /* __NEW_HD_DRIVE_ID */
1417 if_strcat((id->dma_ultra & 0x0800),umodes,"*");
1418 if_strcat((id->dma_ultra & 0x0008),umodes,"udma3 ");
1419 if_strcat((id->dma_ultra & 0x1000),umodes,"*");
1420 if_strcat((id->dma_ultra & 0x0010),umodes,"udma4 ");
1421 if_strcat((id->dma_ultra & 0x2000),umodes,"*");
1422 if_strcat((id->dma_ultra & 0x0020),umodes,"udma5 ");
1423 if_strcat((id->dma_ultra & 0x4000),umodes,"*");
1424 if_strcat((id->dma_ultra & 0x0040),umodes,"udma6 ");
1425 if_strcat((id->dma_ultra & 0x8000),umodes,"*");
1426 if_strcat((id->dma_ultra & 0x0080),umodes,"udma7 ");
1430 if_printf(((id->capability&1) && (id->field_valid&2)),
1431 ", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1432 printf("\n PIO modes: %s", pmodes);
1433 if_printf((*dmodes),"\n DMA modes: %s", dmodes);
1434 if_printf((*umodes),"\n UDMA modes: %s", umodes);
1436 printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
1437 if (id_regs[83] & 8)
1439 if (!(id_regs[86]&8))
1440 printf(": disabled (255)");
1441 else if ((id_regs[91]&0xFF00)!=0x4000)
1442 printf(": unknown setting");
1444 printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1446 if_printf( (id_regs[82]&0x20)," WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
1447 #ifdef __NEW_HD_DRIVE_ID
1448 if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
1450 printf("\n Drive conforms to: ");
1451 if_else_printf((id->minor_rev_num <= 31),"%s: ","unknown: ", minor_str[id->minor_rev_num]);
1452 if (id->major_rev_num < 31)
1454 for (i=0; i <= 15; i++)
1455 if_printf((id->major_rev_num & (1<<i))," %u", i);
1458 #endif /* __NEW_HD_DRIVE_ID */
1459 printf("\n\n * signifies the current active mode\n\n");
1463 static void flush_buffer_cache (int fd)
1465 fsync (fd); /* flush buffers */
1466 bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
1467 #ifdef HDIO_DRIVE_CMD
1469 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
1470 bb_error_msg("HDIO_DRIVE_CMD");
1474 static int seek_to_zero (int fd)
1476 if (lseek(fd, (off_t) 0, SEEK_SET))
1481 static int read_big_block (int fd, char *buf)
1486 if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES)
1494 string = "read() hit EOF - device too small";
1497 string = "read(%u) returned %u bytes";
1499 bb_error_msg(string, TIMING_BUF_BYTES, rc);
1503 /* access all sectors of buf to ensure the read fully completed */
1504 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1509 static double correction = 0.0;
1511 void do_time (int flag, int fd)
1514 flag = 1 time_device
1520 struct itimerval e1, e2;
1522 int timing_MB = TIMING_MB;
1524 if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1)
1526 bb_error_msg (bb_msg_shared_mem,"allocate"); /*"could not allocate sharedmem buf"*/
1529 if (shmctl(shmid, SHM_LOCK, NULL) == -1)
1531 bb_error_msg (bb_msg_shared_mem,"lock"); /*"could not lock sharedmem buf"*/
1532 (void) shmctl(shmid, IPC_RMID, NULL);
1535 if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1)
1537 bb_error_msg (bb_msg_shared_mem,"attach"); /*"could not attach sharedmem buf"*/
1538 (void) shmctl(shmid, IPC_RMID, NULL);
1541 if (shmctl(shmid, IPC_RMID, NULL) == -1)
1542 bb_error_msg ("shmctl(,IPC_RMID,)");
1544 /* Clear out the device request queues & give them time to complete */
1547 if(flag == 0) /* Time cache */
1549 /* Calculate a correction factor for the basic
1550 * overhead of doing a read() from the buffer cache.
1551 * To do this, we read the data once to "cache it" and
1552 * to force full preallocation of our timing buffer,
1553 * and then we re-read it 10 times while timing it.
1555 * getitimer() is used rather than gettimeofday() because
1556 * it is much more consistent (on my machine, at least).
1558 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1559 if (seek_to_zero (fd))
1561 if (read_big_block (fd, buf))
1563 printf(" Timing buffer-cache reads: ");
1566 /* Clear out the device request queues & give them time to complete */
1569 /* Time re-reading from the buffer-cache */
1570 getitimer(ITIMER_REAL, &e1);
1571 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1573 if (seek_to_zero (fd))
1575 if (read_big_block (fd, buf))
1578 getitimer(ITIMER_REAL, &e2);
1579 correction = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1581 /* Now remove the lseek() from the correction factor */
1582 getitimer(ITIMER_REAL, &e1);
1583 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1585 if (seek_to_zero (fd))
1588 getitimer(ITIMER_REAL, &e2);
1589 correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
1590 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1592 if ((BUFCACHE_FACTOR * timing_MB) >= correction) /* more than 1MB/s */
1593 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1594 (BUFCACHE_FACTOR * timing_MB), correction,
1595 (BUFCACHE_FACTOR * timing_MB) / correction);
1597 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1598 (BUFCACHE_FACTOR * timing_MB), correction,
1599 (BUFCACHE_FACTOR * timing_MB) / correction * 1024);
1600 correction /= BUFCACHE_FACTOR;
1602 flush_buffer_cache(fd);
1605 else /* Time device */
1607 printf(" Timing buffered disk reads: ");
1611 * getitimer() is used rather than gettimeofday() because
1612 * it is much more consistent (on my machine, at least).
1614 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1616 /* Now do the timings for real */
1617 getitimer(ITIMER_REAL, &e1);
1618 for (i = TIMING_BUF_COUNT; i > 0; --i)
1620 if (read_big_block (fd, buf))
1623 getitimer(ITIMER_REAL, &e2);
1625 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1627 if (timing_MB >= elapsed) /* more than 1MB/s */
1628 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",timing_MB, elapsed, timing_MB / elapsed);
1630 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",timing_MB, elapsed, timing_MB / elapsed * 1024);
1632 /*"Hmm.. suspicious results: probably not enough free memory for a proper test.");*/
1633 if (elapsed <= (correction * 2))
1634 bb_error_msg(bb_msg_memory_exhausted);
1636 #if 0 /* the "estimate" is just plain wrong for many systems.. */
1637 else if (correction != 0.0) {
1638 printf(" Estimating raw driver speed: ");
1639 elapsed -= correction;
1640 if (timing_MB >= elapsed) /* more than 1MB/s */
1641 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
1642 timing_MB, elapsed, timing_MB / elapsed);
1644 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1645 timing_MB, elapsed, timing_MB / elapsed * 1024);
1650 if (-1 == shmdt(buf))
1651 bb_error_msg (bb_msg_shared_mem,"detach"); /*"could not detach sharedmem buf"*/
1655 static void no_scsi (void)
1657 /*" operation not supported on SCSI disks"*/
1659 bb_error_msg_and_die(bb_msg_op_not_supp,"SCSI");
1662 static void no_xt (void)
1665 bb_error_msg_and_die(bb_msg_op_not_supp,"XT");
1668 static void on_off (unsigned int value)
1670 printf(value ? " (on)\n" : " (off)\n");
1673 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1674 static void bus_state_value (unsigned int value)
1684 string = " (off)\n";
1686 case BUSSTATE_TRISTATE:
1687 string = " (tristate)\n";
1690 string = " (unknown: %d)\n";
1693 printf(string, value);
1697 #ifdef HDIO_DRIVE_CMD
1698 static void interpret_standby (unsigned int standby)
1707 printf("21 minutes");
1710 printf("vendor-specific");
1713 printf("?reserved");
1716 printf("21 minutes + 15 seconds");
1721 unsigned int secs = standby * 5;
1722 unsigned int mins = secs / 60;
1724 if_printf(mins,"%u minutes", mins);
1725 if_printf((mins && secs)," + ");
1726 if_printf(secs,"%u seconds", secs);
1728 else if (standby <= 251)
1730 unsigned int mins = (standby - 240) * 30;
1731 unsigned int hrs = mins / 60;
1733 if_printf(hrs,"%u hours", hrs);
1734 if_printf((hrs && mins)," + ");
1735 if_printf(mins,"%u minutes", mins);
1738 printf("illegal value");
1744 struct xfermode_entry {
1749 static const struct xfermode_entry xfermode_table[] = {
1785 static int translate_xfermode(char * name)
1787 const struct xfermode_entry *tmp;
1792 for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1794 if (!strcmp(name, tmp->name))
1799 val = strtol(name, &endptr, 10);
1800 if (*endptr == '\0')
1806 static void interpret_xfermode (unsigned int xfermode)
1811 printf("default PIO mode");
1814 printf("default PIO mode, disable IORDY");
1824 printf("PIO flow control mode%u", xfermode-8);
1834 printf("singleword DMA mode%u", xfermode-16);
1844 printf("multiword DMA mode%u", xfermode-32);
1854 printf("UltraDMA mode%u", xfermode-64);
1857 printf("unknown, probably not valid");
1862 #endif /* HDIO_DRIVE_CMD */
1865 #define VXVM_MAJOR 199
1869 #define CCISS_MAJOR 104
1872 static void process_dev (char *devname)
1875 static long parm, multcount;
1876 struct stat stat_buf;
1877 #ifndef HDIO_DRIVE_CMD
1878 int force_operation = 0;
1880 if (stat(devname,&stat_buf))
1881 bb_perror_msg_and_die(devname);
1883 switch(major(stat_buf.st_rdev))
1885 #ifdef SCSI_DISK0_MAJOR
1886 case (SCSI_DISK0_MAJOR):
1887 case (SCSI_DISK1_MAJOR):
1888 case (SCSI_DISK2_MAJOR):
1889 case (SCSI_DISK3_MAJOR):
1890 case (SCSI_DISK4_MAJOR):
1891 case (SCSI_DISK5_MAJOR):
1892 case (SCSI_DISK6_MAJOR):
1893 case (SCSI_DISK7_MAJOR):
1895 case (SCSI_DISK_MAJOR):
1901 #ifdef LVM_BLK_MAJOR
1902 case (LVM_BLK_MAJOR):
1907 #ifdef XT_DISK_MAJOR
1908 case (XT_DISK_MAJOR):
1938 break; /* do nothing */
1940 bb_error_msg_and_die("%s not supported",devname);
1943 fd = bb_xopen (devname, O_RDONLY|O_NONBLOCK);
1944 if_printf( (!quiet),"\n%s:\n", devname);
1948 if_printf(get_readahead," setting fs readahead to %ld\n", readahead);
1949 bb_ioctl(fd, BLKRASET,(int *)readahead,"BLKRASET");
1951 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1952 if (unregister_hwif)
1955 printf(" attempting to unregister hwif#%u\n", hwif);
1956 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)hwif,"HDIO_UNREGISTER_HWIF");
1959 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1964 printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
1965 args[0] = hwif_data;
1966 args[1] = hwif_ctrl;
1968 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
1978 printf(" attempting to ");
1980 printf("auto-tune PIO mode\n");
1981 else if (piomode < 100)
1982 printf("set PIO mode to %d\n", piomode);
1983 else if (piomode < 200)
1984 printf("set MDMA mode to %d\n", (piomode-100));
1986 printf("set UDMA mode to %d\n", (piomode-200));
1988 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)piomode, "HDIO_SET_PIO_MODE");
1994 if_printf(get_io32bit," setting 32-bit IO_support flag to %ld\n", io32bit);
1995 bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
2001 if_printf(get_mult, " setting multcount to %ld\n", mult);
2002 if(ioctl(fd, HDIO_SET_MULTCOUNT, mult))
2003 bb_error_msg("HDIO_SET_MULTCOUNT");
2004 #ifndef HDIO_DRIVE_CMD
2006 force_operation = 1;
2011 if_printf_on_off(get_readonly," setting readonly to %ld", readonly);
2012 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
2018 if_printf_on_off(get_unmask," setting unmaskirq to %ld", unmask);
2019 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
2021 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2025 if_printf_on_off(get_dma," setting using_dma to %ld", dma);
2026 bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
2028 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
2032 if_printf_on_off(get_dma_q," setting DMA queue_depth to %ld", dma_q);
2033 bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
2039 if_printf_on_off(get_nowerr," setting nowerr to %ld", nowerr);
2040 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
2046 if_printf_on_off(get_keep," setting keep_settings to %ld", keep);
2047 bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
2049 #ifdef HDIO_DRIVE_CMD
2052 unsigned char args[4] = {0,0,0,0};
2056 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
2057 if_printf_on_off(get_doorlock," setting drive doorlock to %ld", doorlock);
2058 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
2062 /* lock/unlock the drive's "feature" settings */
2063 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2067 if_printf_on_off(get_dkeep," setting drive keep features to %ld", dkeep);
2068 args[2] = dkeep ? 0x66 : 0xcc;
2069 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
2073 unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
2075 args[2] = defects ? 0x04 : 0x84;
2076 if_printf(get_defects," setting drive defect-mgmt to %ld\n", defects);
2077 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
2081 unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
2086 if_printf(get_prefetch," setting drive prefetch to %ld\n", prefetch);
2087 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
2091 unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
2095 args[1] = xfermode_requested;
2098 printf(" setting xfermode to %d", xfermode_requested);
2099 interpret_xfermode(xfermode_requested);
2101 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
2105 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2109 args[2] = lookahead ? 0xaa : 0x55;
2110 if_printf_on_off(get_lookahead," setting drive read-lookahead to %ld", lookahead);
2111 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
2115 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2117 check_if_min_and_set_val(apmmode,1);
2118 check_if_maj_and_set_val(apmmode,255);
2119 if_printf(get_apmmode," setting APM level to");
2122 /* disable Advanced Power Management */
2123 args[2] = 0x85; /* feature register */
2124 if_printf(get_apmmode," disabled\n");
2128 /* set Advanced Power Management mode */
2129 args[2] = 0x05; /* feature register */
2130 args[1] = apmmode; /* sector count register */
2131 if_printf(get_apmmode," 0x%02lX (%ld)\n",apmmode,apmmode);
2133 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
2137 #ifdef DO_FLUSHCACHE
2138 #ifndef WIN_FLUSHCACHE
2139 #define WIN_FLUSHCACHE 0xe7
2141 unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
2142 #endif /* DO_FLUSHCACHE */
2143 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2146 args[2] = wcache ? 0x02 : 0x82;
2147 if_printf_on_off(get_wcache," setting drive write-caching to %ld", wcache);
2148 #ifdef DO_FLUSHCACHE
2149 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
2150 bb_error_msg ("HDIO_DRIVE_CMD(flushcache)");
2151 #endif /* DO_FLUSHCACHE */
2152 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
2153 #ifdef DO_FLUSHCACHE
2154 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
2155 bb_error_msg ("HDIO_DRIVE_CMD(flushcache)");
2156 #endif /* DO_FLUSHCACHE */
2160 #ifndef WIN_STANDBYNOW1
2161 #define WIN_STANDBYNOW1 0xE0
2163 #ifndef WIN_STANDBYNOW2
2164 #define WIN_STANDBYNOW2 0x94
2166 unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
2167 unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
2169 if_printf(get_standbynow," issuing standby command\n");
2170 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2171 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
2172 bb_error_msg("HDIO_DRIVE_CMD(standby)");
2176 #ifndef WIN_SLEEPNOW1
2177 #define WIN_SLEEPNOW1 0xE6
2179 #ifndef WIN_SLEEPNOW2
2180 #define WIN_SLEEPNOW2 0x99
2182 unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
2183 unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
2185 if_printf(get_sleepnow," issuing sleep command\n");
2186 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2187 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
2188 bb_error_msg("HDIO_DRIVE_CMD(sleep)");
2192 unsigned char args[4] = {0xfb,0,0,0};
2195 if_printf(get_seagate," disabling Seagate auto powersaving mode\n");
2196 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
2200 unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
2205 printf(" setting standby to %lu", standby_requested);
2206 interpret_standby(standby_requested);
2208 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
2210 #else /* HDIO_DRIVE_CMD */
2211 if (force_operation)
2214 flush_buffer_cache(fd);
2215 if (-1 == read(fd, buf, sizeof(buf)))
2216 bb_error_msg("access failed");
2218 #endif /* HDIO_DRIVE_CMD */
2223 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity)
2227 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
2229 if ((verbose && !is_xt_hd) || get_mult)
2230 bb_error_msg("HDIO_GET_MULTCOUNT");
2232 else if (verbose | get_mult)
2234 printf(" multcount = %2ld", multcount);
2238 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit)
2242 if(ioctl(fd, HDIO_GET_32BIT, &parm))
2243 bb_error_msg("HDIO_GET_32BIT");
2246 printf(" IO_support =%3ld (", parm);
2252 printf("16-bit)\n");
2255 printf("32-bit)\n");
2258 printf("32-bit w/sync)\n");
2261 printf("Request-Queue-Bypass)\n");
2264 printf("\?\?\?)\n");
2269 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask)
2273 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
2274 "HDIO_GET_UNMASKINTR"," unmaskirq = %2ld");
2278 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2279 if ((verbose && !is_scsi_hd) || get_dma) {
2281 if(ioctl(fd, HDIO_GET_DMA, &parm))
2282 bb_error_msg("HDIO_GET_DMA");
2285 printf(" using_dma = %2ld", parm);
2287 printf(" (DMA-Assisted-PIO)\n");
2296 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
2297 "HDIO_GET_QDMA"," queue_depth = %2ld");
2299 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep)
2303 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
2304 "HDIO_GET_KEEPSETTINGS"," keepsettings = %2ld");
2311 bb_ioctl_on_off (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
2312 " HDIO_GET_NOWERR"," nowerr = %2ld");
2314 if (verbose || get_readonly)
2316 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
2317 " BLKROGET"," readonly = %2ld");
2319 if ((verbose && !is_scsi_hd) || get_readahead)
2321 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
2322 " BLKRAGET"," readahead = %2ld");
2324 if (verbose || get_geom)
2326 static const char msg[] = " geometry = %u/%u/%u, sectors = %ld, start = %ld\n";
2327 static struct hd_geometry g;
2328 #ifdef HDIO_GETGEO_BIG
2329 static struct hd_big_geometry bg;
2332 if (ioctl(fd, BLKGETSIZE, &parm))
2333 bb_error_msg("BLKGETSIZE");
2334 #ifdef HDIO_GETGEO_BIG
2335 else if (!ioctl(fd, HDIO_GETGEO_BIG, &bg))
2336 printf(msg, bg.cylinders, bg.heads, bg.sectors, parm, bg.start);
2338 else if (ioctl(fd, HDIO_GETGEO, &g))
2339 bb_error_msg("HDIO_GETGEO");
2341 printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
2343 #ifdef HDIO_DRIVE_CMD
2346 #ifndef WIN_CHECKPOWERMODE1
2347 #define WIN_CHECKPOWERMODE1 0xE5
2349 #ifndef WIN_CHECKPOWERMODE2
2350 #define WIN_CHECKPOWERMODE2 0x98
2352 unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
2355 if (ioctl(fd, HDIO_DRIVE_CMD, &args)
2356 && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
2357 && ioctl(fd, HDIO_DRIVE_CMD, &args))
2359 if (errno != EIO || args[0] != 0 || args[1] != 0)
2365 state = (args[2] == 255) ? "active/idle" : "standby";
2367 printf(" drive state is: %s\n", state);
2370 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2375 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
2377 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2378 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2379 if (perform_tristate)
2381 unsigned char args[4] = {0,tristate,0,0};
2384 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
2386 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2387 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2390 static struct hd_driveid id;
2395 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
2397 if (multcount != -1)
2399 id.multsect = multcount;
2400 id.multsect_valid |= 1;
2403 id.multsect_valid &= ~1;
2406 else if (errno == -ENOMSG)
2407 printf(" no identification info available\n");
2409 bb_error_msg("HDIO_GET_IDENTITY");
2414 unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
2420 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2422 args[0] = WIN_PIDENTIFY;
2423 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2425 bb_error_msg("HDIO_DRIVE_CMD(identify)");
2426 goto identify_abort;
2429 for(i=0; i<(sizeof args)/2; i+=2)
2430 __le16_to_cpus((uint16_t *)(&args[i]));
2432 identify((void *)&args[4], NULL);
2437 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2443 printf(" setting bus state to %d", busstate);
2444 bus_state_value(busstate);
2446 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)busstate, "HDIO_SET_BUSSTATE");
2449 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2453 if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
2454 bb_error_msg("HDIO_GET_BUSSTATE");
2457 printf(" busstate = %2ld", parm);
2458 bus_state_value(parm);
2463 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2467 do_time(0,fd); /*time cache */
2469 do_time(1,fd); /*time device */
2471 flush_buffer_cache (fd);
2475 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2476 static int fromhex (unsigned char c)
2478 if (c >= 'a' && c <= 'f')
2479 return 10 + (c - 'a');
2480 if (c >= '0' && c <= '9')
2482 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2485 static int identify_from_stdin (void)
2487 unsigned short sbuf[800];
2488 unsigned char buf[1600], *b = (unsigned char *)buf;
2489 int i, count = read(0, buf, 1280);
2492 bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
2493 for (i = 0; count >= 4; ++i)
2495 sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
2496 __le16_to_cpus((uint16_t *)(&sbuf[i]));
2500 identify(sbuf, NULL);
2505 /* our main() routine: */
2506 int hdparm_main(int argc, char **argv)
2508 const char * const bb_msg_missing_value ="missing value";
2520 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2521 if (!strcmp("-Istdin", *argv))
2523 exit(identify_from_stdin());
2537 /*bb_error_msg_and_die("%s", VERSION);*/
2538 /* We have to return 0 here and not 1 */
2539 printf("%s %s\n",bb_applet_name, VERSION);
2544 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2565 p = check_ptr(p,argc,argv);
2566 if((set_unmask = set_flag(p,'1'))==1)
2567 unmask = *p++ - '0';
2569 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2573 p = check_ptr(p,argc,argv);
2574 if((set_dma = set_flag(p,'9'))==1)
2577 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
2581 p = check_ptr(p,argc,argv);
2582 if((set_nowerr = set_flag(p,'1'))==1)
2583 nowerr = *p++ - '0';
2586 noisy_piomode = noisy;
2588 p = check_ptr_v2(p,argc,argv);
2591 while (isalnum(*p) && (tmpstr - name) < 31)
2597 piomode = translate_xfermode(name);
2604 get_readonly = noisy;
2606 p = check_ptr(p,argc,argv);
2607 if((set_readonly = set_flag(p,'1'))==1)
2608 readonly = *p++ - '0';
2613 p = check_ptr(p,argc,argv);
2617 mult = (mult * 10) + (*p++ - '0');
2621 get_io32bit = noisy;
2623 p = check_ptr(p,argc,argv);
2627 io32bit = (io32bit * 10) + (*p++ - '0');
2630 #ifdef HDIO_DRIVE_CMD
2632 get_standby = noisy;
2634 p = check_ptr(p,argc,argv);
2638 standby_requested = (standby_requested * 10) + (*p++ - '0');
2641 bb_error_msg("-S: %s", bb_msg_missing_value);
2645 get_defects = noisy;
2647 p = check_ptr(p,argc,argv);
2651 defects = (defects * 10) + (*p++ - '0');
2654 bb_error_msg("-D: %s", bb_msg_missing_value);
2657 get_prefetch = noisy;
2659 p = check_ptr(p,argc,argv);
2663 prefetch = (prefetch * 10) + (*p++ - '0');
2666 bb_error_msg("-P: %s", bb_msg_missing_value);
2670 get_xfermode = noisy;
2672 p = check_ptr_v2(p,argc,argv);
2675 while (isalnum(*p) && (tmpstr - name) < 31)
2681 xfermode_requested = translate_xfermode(name);
2682 if (xfermode_requested == -1)
2687 bb_error_msg("-X: %s", bb_msg_missing_value);
2693 p = check_ptr(p,argc,argv);
2694 if((set_dkeep = set_flag(p,'1'))==1)
2697 bb_error_msg("-K: %s (0/1)", bb_msg_missing_value);
2701 get_lookahead = noisy;
2703 p = check_ptr(p,argc,argv);
2704 if((set_lookahead = set_flag(p,'1'))==1)
2705 lookahead = *p++ - '0';
2707 bb_error_msg("-A: %s (0/1)", bb_msg_missing_value);
2711 get_doorlock = noisy;
2713 p = check_ptr(p,argc,argv);
2714 if((set_doorlock = set_flag(p,'1'))==1)
2715 doorlock = *p++ - '0';
2717 bb_error_msg("-L: %s (0/1)", bb_msg_missing_value);
2723 p = check_ptr(p,argc,argv);
2724 if((set_wcache = set_flag(p,'1'))==1)
2725 wcache = *p++ - '0';
2727 bb_error_msg("-W: %s (0/1)", bb_msg_missing_value);
2731 get_powermode = noisy;
2736 get_standbynow = noisy;
2742 get_sleepnow = noisy;
2752 get_seagate = noisy;
2756 #endif /* HDIO_DRIVE_CMD */
2760 p = check_ptr(p,argc,argv);
2761 if((set_keep = set_flag(p,'1'))==1)
2764 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
2766 p = check_ptr(p,argc,argv);
2768 goto error; /* "expected hwif_nr" */
2770 sscanf(p++, "%i", &hwif);
2772 unregister_hwif = 1;
2774 #endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
2775 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2777 p = check_ptr(p,argc,argv);
2779 goto error; /* "expected hwif_data" */
2781 sscanf(p++, "%i", &hwif_data);
2783 if (argc && isdigit(**argv))
2784 p = *argv++, --argc;
2786 goto error; /* "expected hwif_ctrl" */
2788 sscanf(p, "%i", &hwif_ctrl);
2790 if (argc && isdigit(**argv))
2791 p = *argv++, --argc;
2794 bb_error_msg_and_die("expected hwif value"); /* "expected hwif_irq" */
2796 sscanf(p, "%i", &hwif_irq);
2802 #endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2807 p = check_ptr(p,argc,argv);
2811 dma_q = (dma_q * 10) + (*p++ - '0');
2813 /* what is this for ? as neg = 0 (see above) it seems to do nothing */
2817 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2821 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2822 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2824 p = check_ptr(p,argc,argv);
2825 if((perform_tristate = set_flag(p,'1'))==1)
2826 tristate = *p++ - '0';
2828 bb_error_msg("-x: %s (0/1)", bb_msg_missing_value);
2831 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2833 get_readahead = noisy;
2835 p = check_ptr(p,argc,argv);
2839 readahead = (readahead * 10) + (*p++ - '0');
2843 get_apmmode = noisy;
2845 p = check_ptr(p,argc,argv);
2849 apmmode = (io32bit * 10) + (*p++ - '0');
2852 printf("-B: %s (1-255)", bb_msg_missing_value);
2862 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2864 get_busstate = noisy;
2866 p = check_ptr(p,argc,argv);
2867 if((set_busstate = set_flag(p,'2'))==1)
2868 busstate = *p++ - '0';