Remove trailing whitespace. Update copyright to include 2004.
[platform/upstream/busybox.git] / miscutils / hdparm.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * hdparm implementation for busybox
4  *
5  *
6  * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
7  *
8  * Hacked by Tito <farmatito@tiscali.it> for size optimization.
9  *
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.
14  *
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.
19  *
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
23  * 02111-1307 USA
24  *
25  *
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
29  */
30
31 #include <unistd.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <ctype.h>
37 #include <endian.h>
38 #include <sys/ioctl.h>
39 #include <sys/shm.h>
40 #include <sys/sysmacros.h>
41 #include <sys/time.h>
42 #include <sys/times.h>
43 #include <sys/mount.h>
44 #include "busybox.h"
45 #include <linux/types.h>
46 #include <linux/hdreg.h>
47 #include <linux/major.h>
48 #include <asm/byteorder.h>
49
50
51 #if (__BYTE_ORDER == __BIG_ENDIAN) && !defined(__USE_XOPEN)
52 #define __USE_XOPEN
53 #endif
54
55 /* device types */
56 /* ------------ */
57 #define NO_DEV                  0xffff
58 #define ATA_DEV                 0x0000
59 #define ATAPI_DEV               0x0001
60
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 */
82 #define CAPAB_1                 50
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 */
114 #define CMDS_EN_1               86
115 #define CMDS_EN_2               87
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 */
137
138 /* bit definitions within the words */
139 /* -------------------------------- */
140
141 /* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
142 #define VALID                   0xc000
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
147
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
163 #define SHIFT_EQPT              8
164
165 #define CDROM 0x0005
166
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 */
201 };
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 */
218         "ATAPI"                                 /* bit 14 */
219 };
220 #endif
221
222 /* word 1: number of logical cylinders */
223 #define LCYLS_MAX               0x3fff /* maximum allowable value */
224
225 /* word 2: specific configureation
226  * (a) require SET FEATURES to spin-up
227  * (b) require spin-up to fully reply to IDENTIFY DEVICE
228  */
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) */
233
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 */
237
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 */
249
250 /* word 50: capabilities 1 */
251 #define MIN_STANDBY_TIMER       0x0001  /* 1=device specific standby timer value minimum */
252
253 /* words 51 & 52: PIO & DMA cycle times */
254 #define MODE                    0xff00  /* the mode is in the MSBs */
255
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 */
260
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!) */
265
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        */
269
270 /* word 75: queue_depth */
271 #define DEPTH_BITS              0x001f  /* bits used for queue depth */
272
273 /* words 80-81: version numbers */
274 /* NOVAL_0 or  NOVAL_1 means device does not report version */
275
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*/
312 };
313 #endif
314 static const char actual_ver[] = {
315                         /* word 81 value: */
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                */
346 };
347
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
354
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 */
373                                                 /* --------------*/
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 */
380         "",
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 */
390                                                 /* --------------*/
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 */
407 };
408 #endif
409
410
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
414  */
415
416 /* words 89, 90, SECU ERASE TIME */
417 #define ERASE_BITS              0x00ff
418
419 /* word 92: master password revision */
420 /* NOVAL_0 or  NOVAL_1 means no support for master password revision */
421
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 */
428
429 /* word 127: removable media status notification feature set support */
430 #define RM_STAT_BITS            0x0003
431 #define RM_STAT_SUP             0x0001
432
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 */
445 };
446 #endif
447
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 */
453
454 /* word 255: integrity */
455 #define SIG                     0x00ff  /* signature location */
456 #define SIG_VAL                 0x00A5  /* signature value */
457
458 #define VERSION "v5.4"
459
460 #define TIMING_MB               64
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
465
466 #undef DO_FLUSHCACHE            /* under construction: force cache flush on -W0 */
467
468 /* Busybox messages and functions */
469
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";
472
473 static void bb_ioctl(int fd, int request, void *argp, const char *string)
474 {
475         if (ioctl (fd, request, argp) != 0)
476                 bb_error_msg(" %s", string);
477 }
478
479 static void if_printf(unsigned long i, char *fmt, ... )
480 {
481                 va_list ap;
482                 va_start(ap, fmt);
483                 if (i)
484                         vprintf(fmt, ap);
485                 va_end(ap);
486 }
487
488 static void on_off (unsigned int value);
489
490 static void if_printf_on_off(unsigned long get_arg,const char *fmt, unsigned long arg)
491 {
492         if (get_arg)
493         {
494                 printf(fmt, arg);
495                 on_off(arg);
496         }
497 }
498
499 static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
500                                                          const char * fmt)
501 {
502         if (ioctl (fd, request, &argp) != 0)
503                 bb_error_msg(" %s", string);
504         else
505         {
506                 printf(fmt, (unsigned long) argp);
507                 on_off((unsigned long) argp);
508         }
509 }
510
511 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
512 static void if_else_printf(unsigned long i, char *fmt1, char *fmt2, ... )
513 {
514                 va_list ap;
515                 va_start(ap, fmt2);
516                 if (i)
517                         vprintf(fmt1, ap);
518                 else
519                         vprintf(fmt2, ap);
520                 va_end(ap);
521 }
522
523 static void print_ascii(uint16_t *p, uint8_t length);
524
525 static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
526 {
527         if(val[i])
528         {
529                 printf("\t%-20s",string);
530                 print_ascii(&val[i], n);
531         }
532 }
533
534 static void if_strcat(unsigned long test, char *modes, char *string)
535 {
536         if (test)
537                 strcat(modes,string);
538 }
539 #endif
540
541 static void sync_and_sleep(int i)
542 {
543         sync();
544         sleep(i);
545 }
546
547 static void check_if_min_and_set_val(uint16_t a, uint16_t b)
548 {
549         if( a < b)
550                 a = b;
551 }
552
553 static void check_if_maj_and_set_val(uint16_t a, uint16_t b)
554 {
555         if( a > b)
556                 a = b;
557 }
558
559 char * check_ptr(char *p, int argc, char **argv)
560 {
561         if (!*p && argc && isdigit(**argv))
562                 p = *argv++, --argc;
563         return p;
564 }
565
566 char * check_ptr_v2(char *p, int argc, char **argv)
567 {
568         if (!*p && argc && isalnum(**argv))
569                 p = *argv++, --argc;
570         return p;
571 }
572
573 unsigned long int set_flag(char *p, char max)
574 {
575         if (*p >= '0' && *p <=  max )
576                 return 1;
577         return 0;
578 }
579
580 /* end of  busybox specific stuff */
581
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)
584 {
585         uint16_t ii;
586         uint8_t err_dma = 0;
587
588         for(ii = 0; ii <= MODE_MAX; ii++)
589         {
590                 if(mode_sel & 0x0001)
591                 {
592                         printf("*%cdma%u ",cc,ii);
593                         if(*have_mode)
594                                 err_dma = 1;
595                         *have_mode = 1;
596                 }
597                 else if(mode_sup & 0x0001)
598                         printf("%cdma%u ",cc,ii);
599
600                 mode_sup >>=1;
601                 mode_sel >>=1;
602         }
603         return err_dma;
604 }
605
606 static void print_ascii(uint16_t *p, uint8_t length) {
607         uint8_t ii;
608         char cl;
609
610         /* find first non-space & print it */
611         for(ii = 0; ii< length; ii++)
612         {
613                 if(((char) 0x00ff&((*p)>>8)) != ' ')
614                         break;
615                 if((cl = (char) 0x00ff&(*p)) != ' ')
616                 {
617                         if_printf((cl != '\0'),"%c",cl);
618                         p++;
619                         ii++;
620                         break;
621                 }
622                 p++;
623         }
624         /* print the rest */
625         for(; ii< length; ii++)
626         {
627                 if(!(*p))
628                         break; /* some older devices have NULLs */
629                 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
630                 p++;
631         }
632         printf("\n");
633 }
634
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
638    intentional. */
639 static void identify (uint16_t *id_supplied, const char *devname)
640 {
641
642         char *id_file = NULL;
643         FILE *fl;
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;
648         uint8_t  chksum = 0;
649         uint32_t ll, mm, nn, oo;
650         __u64 bbbig; /* (:) */
651
652         if (id_supplied)
653         {
654 #if __BYTE_ORDER == __BIG_ENDIAN
655                 swab(id_supplied, val, sizeof(val));
656 #else
657                 memcpy(val, id_supplied, sizeof(val));
658 #endif
659         }
660         else
661         {
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 */
665                 if (id_file == NULL)
666                         fl = stdin;
667                 else
668                         fl = bb_xfopen(id_file, "r");
669
670                 /* calculate checksum over all bytes */
671                 for(ii = GEN_CONFIG; ii<=INTEGRITY; ii++)
672                 {
673                         unsigned int scratch;
674                         if(1 != fscanf(fl,"%04x",&scratch))
675                                 break;
676                         val[ii] = (uint16_t)scratch;
677                         chksum += val[ii] + (val[ii] >> 8);
678                 }
679                 fclose(fl);
680                 /*bb_fclose_nonstdin(fl);*/
681                 if(ii < (INTEGRITY+1))
682                         bb_error_msg_and_die("Input file wrong format or length");
683         }
684         chksum &= 0xff;
685
686         /* check if we recognise the device type */
687         printf("\n");
688         if(!(val[GEN_CONFIG] & NOT_ATA))
689         {
690                 dev = ATA_DEV;
691                 printf("ATA device, with ");
692         }
693         else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
694         {
695                 dev = ATA_DEV;
696                 like_std = 4;
697                 printf("CompactFlash ATA device, with ");
698         }
699         else if(!(val[GEN_CONFIG] & NOT_ATAPI))
700         {
701                 dev = ATAPI_DEV;
702                 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
703                 printf("ATAPI %s, with ", pkt_str[eqpt]);
704                 like_std = 3;
705         }
706         else
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");
709
710         if_printf(!(val[GEN_CONFIG] & MEDIA_REMOVABLE),"non-");
711         printf("removable media\n");
712
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.
719          */
720         if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
721            (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
722         {
723                 like_std = 5;
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");
728         }
729
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);
736
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:");
740         if(eqpt != CDROM)
741         {
742                 if(val[MINOR] && (val[MINOR] <= MINOR_MAX))
743                 {
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]]);
747
748                 }
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))
753                 {
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--)
758                         {
759                                 if(jj & 0x8000)
760                                 {
761                                         printf("%u ", ii);
762                                         if(like_std < ii)
763                                         {
764                                                 like_std = ii;
765                                                 kk = like_std >4 ? like_std-4: 0;
766                                         }
767                                         check_if_maj_and_set_val(min_std, ii);
768                                 }
769                                 jj <<= 1;
770                         }
771                         check_if_min_and_set_val( like_std, 3);
772                 }
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.
776                  */
777                 check_if_min_and_set_val( like_std, std);
778
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) ) ) )
784                 {
785                         like_std = 6;
786                 }
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)) ) )
792                 {
793                         like_std = 5;
794                 }
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) ) )
802                 {
803                         like_std = 4;
804                 }
805                 else if(((std == 2) || (!std && (like_std < 3))) &&
806                            ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
807                 {
808                         like_std = 3;
809                 }
810                 else if(((std == 1) || (!std && (like_std < 2))) &&
811                                 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
812                                 (val[WHATS_VALID] & OK_W64_70)) )
813                 {
814                         like_std = 2;
815                 }
816                 if(!std)
817                         printf("\n\tLikely used: %u\n",like_std);
818                 else if(like_std > std)
819                         printf("& some of %u\n",like_std);
820                 else
821                         printf("\n");
822         }
823         else
824         {
825                 /* TBD: do CDROM stuff more thoroughly.  For now... */
826                 kk = 0;
827                 if(val[CDR_MINOR] == 9)
828                 {
829                         kk = 1;
830                         printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
831                 }
832                 if(val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
833                 {
834                         kk = 1;
835                         printf("\n\tSupported: CD-ROM ATAPI");
836                         jj = val[CDR_MAJOR] >> 1;
837                         for(ii = 1; ii <15; ii++)
838                         {
839                                 if_printf((jj & 0x0001),"-%u ", ii);
840                                 jj >>= 1;
841                         }
842                 }
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: */
845                 like_std = 2;
846         }
847
848         if(min_std == 0xffff)
849                 min_std = like_std > 4 ? like_std - 3 : 1;
850
851         printf("Configuration:\n");
852         /* more info from the general configuration word */
853         if((eqpt != CDROM) && (like_std == 1))
854         {
855                 jj = val[GEN_CONFIG] >> 1;
856                 for(ii = 1; ii < 15; ii++)
857                 {
858                         if_printf((jj & 0x0001),"\t%s\n",ata1_cfg_str[ii]);
859                         jj >>=1;
860                 }
861         }
862         if(dev == ATAPI_DEV)
863         {
864                 printf("\tDRQ response: "); /* Data Request (DRQ) */
865                 switch(val[GEN_CONFIG] & DRQ_RESPONSE_TIME)
866                 {
867                         case DRQ_3MS_VAL :
868                                 printf("3ms.\n");
869                                 break;
870                         case DRQ_INTR_VAL :
871                                 printf("<=10ms with INTRQ\n");
872                                 break;
873                         case DRQ_50US_VAL :
874                                 printf("50us.\n");
875                                 break;
876                         default :
877                                 printf("unknown.\n");
878                                 break;
879                 }
880                 printf("\tPacket size: ");
881                 switch(val[GEN_CONFIG] & PKT_SIZE_SUPPORTED)
882                 {
883                         case PKT_SIZE_12_VAL :
884                                 printf("12 bytes\n");
885                                 break;
886                         case PKT_SIZE_16_VAL :
887                                 printf("16 bytes\n");
888                                 break;
889                         default :
890                                 printf("Unknown\n");
891                                 break;
892                 }
893         }
894         else
895         {
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];
898                 mm = 0; bbbig = 0;
899                 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
900                         printf("\tCHS addressing not supported\n");
901                 else
902                 {
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);
906
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]);
909
910                         if(jj)
911                         {
912                                 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
913                                 if(like_std < 3)
914                                 {
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))
919                                                 mm = oo;
920                                 }
921                                 printf("\tCHS current addressable sectors:%11u\n",mm);
922                         }
923                 }
924                 /* LBA addressing */
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) )
928                 {
929                         bbbig = (__u64)val[LBA_64_MSB]  << 48 |
930                                 (__u64)val[LBA_48_MSB]  << 32 |
931                                 (__u64)val[LBA_MID]     << 16 |
932                                         val[LBA_LSB] ;
933                         printf("\tLBA48  user addressable sectors:%11llu\n",bbbig);
934                 }
935
936                 if (!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);
941
942                 if_else_printf((bbbig > 1000),"(%llu GB)\n","\n",bbbig/1000);
943
944         }
945
946         /* hw support of commands (capabilities) */
947         printf("Capabilities:\n\t");
948
949         if(dev == ATAPI_DEV)
950         {
951                 if(eqpt != CDROM)
952                         if_printf((val[CAPAB_0] & CMD_Q_SUP),"Cmd queuing, ");
953
954                 if_printf((val[CAPAB_0] & OVLP_SUP),"Cmd overlap, ");
955         }
956         if_printf((val[CAPAB_0] & LBA_SUP),"LBA, ");
957
958         if(like_std != 1)
959         {
960                 printf("IORDY");
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");
964         }
965         else
966                 printf("no IORDY\n");
967
968         if((like_std == 1) && val[BUF_TYPE])
969         {
970                 kk = 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");
974                 printf("\n");
975         }
976         jj = 0;
977         if((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
978         {
979                 printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
980                 jj = 1;
981         }
982         if((min_std < 4) && (val[RW_LONG]))
983         {
984                 printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
985                 jj = 1;
986         }
987         if((eqpt != CDROM) && (like_std > 3))
988         {
989                 printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
990                 jj = 1;
991         }
992         if_printf(jj,"\n");
993
994         if(dev == ATA_DEV)
995         {
996                 if(like_std == 1)
997                         printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
998                 else
999                 {
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");
1004                         else
1005                           printf("\n");
1006                 }
1007                 printf("\tR/W multiple sector transfer: ");
1008                 if((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
1009                         printf("not supported\n");
1010                 else
1011                 {
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);
1015                 }
1016                 if((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
1017                 {
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
1020                            nothing here. */
1021                         printf("\tAdvancedPM level: ");
1022                         if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
1023                         {
1024                                 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
1025                                 printf("%u (0x%x)\n", apm_level, apm_level);
1026                         }
1027                         else
1028                                 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
1029                 }
1030                 if(like_std > 5)
1031                 {
1032                         if_printf(val[ACOUSTIC],"\tRecommended acoustic management value: %u, current value: %u\n",
1033                                                                         (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
1034                 }
1035         }
1036         else
1037         {
1038                  /* ATAPI */
1039                 if(eqpt != CDROM)
1040                         if_printf((val[CAPAB_0] & SWRST_REQ),"\tATA sw reset required\n");
1041
1042                 if(val[PKT_REL] || val[SVC_NBSY])
1043                 {
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]);
1047                         printf("\n");
1048                 }
1049         }
1050
1051         /* DMA stuff. Check that only one DMA mode is selected. */
1052         printf("\tDMA: ");
1053         if(!(val[CAPAB_0] & DMA_SUP))
1054                 printf("not supported\n");
1055         else
1056         {
1057                 if_printf((val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA]),
1058                                         " sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
1059                 if(val[SINGLE_DMA])
1060                 {
1061                         jj = val[SINGLE_DMA];
1062                         kk = val[SINGLE_DMA] >> 8;
1063                         err_dma += mode_loop(jj,kk,'s',&have_mode);
1064                 }
1065                 if(val[MULTI_DMA])
1066                 {
1067                         jj = val[MULTI_DMA];
1068                         kk = val[MULTI_DMA] >> 8;
1069                         err_dma += mode_loop(jj,kk,'m',&have_mode);
1070                 }
1071                 if((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
1072                 {
1073                         jj = val[ULTRA_DMA];
1074                         kk = val[ULTRA_DMA] >> 8;
1075                         err_dma += mode_loop(jj,kk,'u',&have_mode);
1076                 }
1077                 if_printf((err_dma || !have_mode),"(?)");
1078                 printf("\n");
1079
1080                 if_printf(((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)),
1081                                         "\t     Interleaved DMA support\n");
1082
1083                 if((val[WHATS_VALID] & OK_W64_70) &&
1084                    (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
1085                 {
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]);
1089                         printf("\n");
1090                 }
1091         }
1092
1093         /* Programmed IO stuff */
1094         printf("\tPIO: ");
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))
1098         {
1099                 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
1100                 for(ii = 0; ii <= PIO_MODE_MAX ; ii++)
1101                 {
1102                         if_printf((jj & 0x0001),"pio%d ",ii);
1103                         jj >>=1;
1104                 }
1105                 printf("\n");
1106         }
1107         else if(((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
1108         {
1109                 for(ii = 0; ii <= val[PIO_MODE]>>8; ii++)
1110                         printf("pio%d ",ii);
1111                 printf("\n");
1112         }
1113         else
1114                 printf("unknown\n");
1115
1116         if(val[WHATS_VALID] & OK_W64_70)
1117         {
1118                 if(val[PIO_NO_FLOW] || val[PIO_FLOW])
1119                 {
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]);
1123                         printf("\n");
1124                 }
1125         }
1126
1127         if((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
1128         {
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++)
1133                 {
1134                         if((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
1135                         {
1136                                 if_else_printf((kk & 0x8000),"\t   *","\t");
1137                                 printf("\t%s\n",cmd_feat_str[ii]);
1138                         }
1139                         jj <<=1; kk<<=1;
1140                         if(ii%16 == 15)
1141                         {
1142                                 jj = val[CMDS_SUPP_0+1+(ii/16)];
1143                                 kk = val[CMDS_EN_0+1+(ii/16)];
1144                         }
1145                         if(ii == 31)
1146                         {
1147                                 if((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
1148                                         ii +=16;
1149                         }
1150                 }
1151         }
1152         if_printf(((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP),
1153                                 "\tRemovable Media Status Notification feature set supported\n");
1154
1155
1156         /* security */
1157         if((eqpt != CDROM) && (like_std > 3) &&
1158            (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1159         {
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];
1164                 if(jj)
1165                 {
1166                         for(ii = 0; ii < NUM_SECU_STR; ii++)
1167                         {
1168                                 if_else_printf((!(jj & 0x0001)),"\tnot\t%s\n", "\t\t%s\n", secu_str[ii]);
1169                                 jj >>=1;
1170                         }
1171                         if(val[SECU_STATUS] & SECU_ENABLED)
1172                         {
1173                                 printf("\tSecurity level ");
1174                                 if_else_printf((val[SECU_STATUS] & SECU_LEVEL),"maximum\n","high\n");
1175                         }
1176                 }
1177                 jj =  val[ERASE_TIME]     & ERASE_BITS;
1178                 kk =  val[ENH_ERASE_TIME] & ERASE_BITS;
1179                 if(jj || kk)
1180                 {
1181                         printf("\t");
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);
1184                         printf("\n");
1185                 }
1186         }
1187
1188         /* reset result */
1189         if((val[HWRST_RSLT] & VALID) == VALID_VAL)
1190         {
1191                 printf("HW reset results:\n");
1192                 if_else_printf((val[HWRST_RSLT] & CBLID),"\tCBLID- above Vih\n","\tCBLID- below Vih\n");
1193
1194                 if(val[HWRST_RSLT] & RST0)
1195                 {
1196                         printf("\tDevice num = 0");
1197                         jj = val[HWRST_RSLT];
1198                 }
1199                 else
1200                 {
1201                         printf("\tDevice num = 1");
1202                         jj = val[HWRST_RSLT] >> 8;
1203                 }
1204
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");
1209                 printf("\n");
1210         }
1211
1212         /* more stuff from std 5 */
1213         if((like_std > 4) && (eqpt != CDROM))
1214         {
1215                 if(val[CFA_PWR_MODE] & VALID_W160)
1216                 {
1217                         printf("CFA power mode 1:\n\t");
1218                         if_else_printf((val[CFA_PWR_MODE] & PWR_MODE_OFF),"disabled","enabled");
1219
1220                         if_printf((val[CFA_PWR_MODE] & PWR_MODE_REQ)," and required by some commands");
1221                         printf("\n");
1222
1223                         if_printf((val[CFA_PWR_MODE] & MAX_AMPS),"\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
1224                 }
1225                 if((val[INTEGRITY] & SIG) == SIG_VAL)
1226                 {
1227                         printf("Checksum: ");
1228                         if_printf(chksum,"in");
1229                         printf("correct\n");
1230                 }
1231         }
1232
1233         exit(0);
1234 }
1235 #endif
1236
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;
1240
1241 static unsigned long set_readahead= 0, get_readahead= 0, bbreadahead= 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;
1247 #endif
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;
1269 #endif
1270 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1271 static int get_IDentity = 0;
1272 #endif
1273 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1274 static int      unregister_hwif = 0;
1275 static int      hwif = 0;
1276 #endif
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;
1282 #endif
1283 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1284 static int      set_busstate = 0, get_busstate = 0, busstate = 0;
1285 #endif
1286 static int      reread_partn = 0;
1287
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 */
1294
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).
1301 //
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.
1304 // Too bad, really.
1305
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"
1312 };
1313
1314 static const char *BuffType[]   = {"unknown", "1Sect", "DualPort", "DualPortCache"};
1315
1316 static void dump_identity (const struct hd_driveid *id)
1317 {
1318         int i;
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;
1322
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]);
1327
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);
1331
1332         if (id->buf_type > 3)
1333                 printf("%s%u", " BuffType=", id->buf_type);
1334         else
1335                 printf("%s%s", " BuffType=", BuffType[id->buf_type]);
1336
1337         printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
1338         if (id->max_multsect)
1339         {
1340                 printf(", MultSect=");
1341                 if (!(id->multsect_valid&1))
1342                         printf("?%u?", id->multsect);
1343                 else if (id->multsect)
1344                         printf("%u", id->multsect);
1345                 else
1346                         printf("off");
1347         }
1348         printf("\n");
1349         if (id->tPIO <= 5)
1350         {
1351                 strcat(pmodes, "pio0 ");
1352                 if_strcat((id->tPIO >= 1), pmodes, "pio1 ");
1353                 if_strcat((id->tPIO >= 2), pmodes, "pio2 ");
1354
1355         }
1356         if_printf((!(id->field_valid&1))," (maybe):");
1357 #if __BYTE_ORDER == __BIG_ENDIAN
1358         capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
1359 #else
1360         capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
1361 #endif
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");
1365
1366         if_printf((id->capability&2),", LBAsects=%u", id->lba_capacity);
1367
1368         if (id->capability&1)
1369         {
1370                 if (id->dma_1word | id->dma_mword)
1371                 {
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? ");
1388                 }
1389         }
1390         printf("\n IORDY=");
1391         if (id->capability&8)
1392                 printf((id->capability&4) ? "on/off" : "yes");
1393         else
1394                 printf("no");
1395
1396         if ((id->capability&8) || (id->field_valid&2))
1397         {
1398                 if (id->field_valid&2)
1399                 {
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? ");
1404                 }
1405                 if (id->field_valid&4)
1406                 {
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)
1415                         {
1416 #else /* !__NEW_HD_DRIVE_ID */
1417                         if (id->word93 & 0x2000)
1418                         {
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 ");
1430                         }
1431                 }
1432         }
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);
1438
1439         printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
1440         if (id_regs[83] & 8)
1441         {
1442                 if (!(id_regs[86]&8))
1443                         printf(": disabled (255)");
1444                 else if ((id_regs[91]&0xFF00)!=0x4000)
1445                         printf(": unknown setting");
1446                 else
1447                         printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1448         }
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))
1452         {
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)
1456                 {
1457                         for (i=0; i <= 15; i++)
1458                                 if_printf((id->major_rev_num & (1<<i))," %u", i);
1459                 }
1460         }
1461 #endif /* __NEW_HD_DRIVE_ID */
1462         printf("\n\n * signifies the current active mode\n\n");
1463 }
1464 #endif
1465
1466 static void flush_buffer_cache (int fd)
1467 {
1468         fsync (fd);                             /* flush buffers */
1469         bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
1470 #ifdef HDIO_DRIVE_CMD
1471         sleep(1);
1472         if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
1473                         bb_error_msg("HDIO_DRIVE_CMD");
1474 #endif
1475 }
1476
1477 static int seek_to_zero (int fd)
1478 {
1479         if (lseek(fd, (off_t) 0, SEEK_SET))
1480                 return 1;
1481         return 0;
1482 }
1483
1484 static int read_big_block (int fd, char *buf)
1485 {
1486
1487         const char *string;
1488         int i, rc;
1489         if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES)
1490         {
1491                 switch(rc)
1492                 {
1493                         case -1:
1494                                 string = "read()";
1495                                 break;
1496                         case  0:
1497                                 string = "read() hit EOF - device too small";
1498                                 break;
1499                         default:
1500                                 string = "read(%u) returned %u bytes";
1501                 }
1502                 bb_error_msg(string, TIMING_BUF_BYTES, rc);
1503                 return 1;
1504         }
1505
1506         /* access all sectors of buf to ensure the read fully completed */
1507         for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1508                 buf[i] &= 1;
1509         return 0;
1510 }
1511
1512 static double correction = 0.0;
1513
1514 void do_time (int flag, int fd)
1515 /*
1516         flag = 0 time_cache
1517         flag = 1 time_device
1518 */
1519 {
1520         int i;
1521         char *buf;
1522         double elapsed;
1523         struct itimerval e1, e2;
1524         int shmid;
1525         int timing_MB = TIMING_MB;
1526
1527         if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1)
1528         {
1529                 bb_error_msg (bb_msg_shared_mem,"allocate"); /*"could not allocate sharedmem buf"*/
1530                 return;
1531         }
1532         if (shmctl(shmid, SHM_LOCK, NULL) == -1)
1533         {
1534                 bb_error_msg (bb_msg_shared_mem,"lock"); /*"could not lock sharedmem buf"*/
1535                 (void) shmctl(shmid, IPC_RMID, NULL);
1536                 return;
1537         }
1538         if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1)
1539         {
1540                 bb_error_msg (bb_msg_shared_mem,"attach"); /*"could not attach sharedmem buf"*/
1541                 (void) shmctl(shmid, IPC_RMID, NULL);
1542                 return;
1543         }
1544         if (shmctl(shmid, IPC_RMID, NULL) == -1)
1545                 bb_error_msg ("shmctl(,IPC_RMID,)");
1546
1547         /* Clear out the device request queues & give them time to complete */
1548         sync_and_sleep(3);
1549
1550         if(flag  == 0) /* Time cache */
1551         {
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.
1557                 *
1558                 * getitimer() is used rather than gettimeofday() because
1559                 * it is much more consistent (on my machine, at least).
1560                 */
1561                 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1562                 if (seek_to_zero (fd))
1563                         return;
1564                 if (read_big_block (fd, buf))
1565                         return;
1566                 printf(" Timing buffer-cache reads:   ");
1567                 fflush(stdout);
1568
1569                 /* Clear out the device request queues & give them time to complete */
1570                 sync_and_sleep(1);
1571
1572                 /* Time re-reading from the buffer-cache */
1573                 getitimer(ITIMER_REAL, &e1);
1574                 for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
1575                 {
1576                         if (seek_to_zero (fd))
1577                                 goto quit;
1578                         if (read_big_block (fd, buf))
1579                                 goto quit;
1580                 }
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);
1583
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)
1587                 {
1588                         if (seek_to_zero (fd))
1589                                 goto quit;
1590                 }
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);
1594
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);
1599                 else
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;
1604
1605                 flush_buffer_cache(fd);
1606                 sleep(1);
1607         }
1608         else /* Time device */
1609         {
1610                 printf(" Timing buffered disk reads:  ");
1611                 fflush(stdout);
1612
1613                 /*
1614                 * getitimer() is used rather than gettimeofday() because
1615                 * it is much more consistent (on my machine, at least).
1616                 */
1617                 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
1618
1619                 /* Now do the timings for real */
1620                 getitimer(ITIMER_REAL, &e1);
1621                 for (i = TIMING_BUF_COUNT; i > 0; --i)
1622                 {
1623                         if (read_big_block (fd, buf))
1624                                 goto quit;
1625                 }
1626                 getitimer(ITIMER_REAL, &e2);
1627
1628                 elapsed =       (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1629
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);
1632                 else
1633                         printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",timing_MB, elapsed, timing_MB / elapsed * 1024);
1634
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);
1638
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);
1646                         else
1647                                 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
1648                                         timing_MB, elapsed, timing_MB / elapsed * 1024);
1649                 }
1650 #endif
1651         }
1652 quit:
1653         if (-1 == shmdt(buf))
1654                 bb_error_msg (bb_msg_shared_mem,"detach"); /*"could not detach sharedmem buf"*/
1655 }
1656
1657
1658 static void no_scsi (void)
1659 {
1660         /*" operation not supported on SCSI disks"*/
1661         if (is_scsi_hd)
1662                 bb_error_msg_and_die(bb_msg_op_not_supp,"SCSI");
1663 }
1664
1665 static void no_xt (void)
1666 {
1667         if (is_xt_hd)
1668                 bb_error_msg_and_die(bb_msg_op_not_supp,"XT");
1669 }
1670
1671 static void on_off (unsigned int value)
1672 {
1673         printf(value ? " (on)\n" : " (off)\n");
1674 }
1675
1676 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1677 static void bus_state_value (unsigned int value)
1678 {
1679         const char *string;
1680
1681         switch (value)
1682         {
1683                 case BUSSTATE_ON:
1684                         string = " (on)\n";
1685                         break;
1686                 case BUSSTATE_OFF:
1687                         string = " (off)\n";
1688                         break;
1689                 case BUSSTATE_TRISTATE:
1690                         string = " (tristate)\n";
1691                         break;
1692                 default:
1693                         string = " (unknown: %d)\n";
1694                         break;
1695         }
1696         printf(string, value);
1697 }
1698 #endif
1699
1700 #ifdef HDIO_DRIVE_CMD
1701 static void interpret_standby (unsigned int standby)
1702 {
1703         printf(" (");
1704         switch(standby)
1705         {
1706                 case 0:
1707                         printf("off");
1708                         break;
1709                 case 252:
1710                         printf("21 minutes");
1711                         break;
1712                 case 253:
1713                         printf("vendor-specific");
1714                         break;
1715                 case 254:
1716                         printf("?reserved");
1717                         break;
1718                 case 255:
1719                         printf("21 minutes + 15 seconds");
1720                         break;
1721                 default:
1722                         if (standby <= 240)
1723                         {
1724                                 unsigned int secs = standby * 5;
1725                                 unsigned int mins = secs / 60;
1726                                 secs %= 60;
1727                                 if_printf(mins,"%u minutes", mins);
1728                                 if_printf((mins && secs)," + ");
1729                                 if_printf(secs,"%u seconds", secs);
1730                         }
1731                         else if (standby <= 251)
1732                         {
1733                                 unsigned int mins = (standby - 240) * 30;
1734                                 unsigned int hrs  = mins / 60;
1735                                 mins %= 60;
1736                                 if_printf(hrs,"%u hours", hrs);
1737                                 if_printf((hrs && mins)," + ");
1738                                 if_printf(mins,"%u minutes", mins);
1739                         }
1740                         else
1741                                 printf("illegal value");
1742                         break;
1743         }
1744         printf(")\n");
1745 }
1746
1747 struct xfermode_entry {
1748     int val;
1749     const char *name;
1750 };
1751
1752 static const struct xfermode_entry xfermode_table[] = {
1753     { 8,    "pio0" },
1754     { 9,    "pio1" },
1755     { 10,   "pio2" },
1756     { 11,   "pio3" },
1757     { 12,   "pio4" },
1758     { 13,   "pio5" },
1759     { 14,   "pio6" },
1760     { 15,   "pio7" },
1761     { 16,   "sdma0" },
1762     { 17,   "sdma1" },
1763     { 18,   "sdma2" },
1764     { 19,   "sdma3" },
1765     { 20,   "sdma4" },
1766     { 21,   "sdma5" },
1767     { 22,   "sdma6" },
1768     { 23,   "sdma7" },
1769     { 32,   "mdma0" },
1770     { 33,   "mdma1" },
1771     { 34,   "mdma2" },
1772     { 35,   "mdma3" },
1773     { 36,   "mdma4" },
1774     { 37,   "mdma5" },
1775     { 38,   "mdma6" },
1776     { 39,   "mdma7" },
1777     { 64,   "udma0" },
1778     { 65,   "udma1" },
1779     { 66,   "udma2" },
1780     { 67,   "udma3" },
1781     { 68,   "udma4" },
1782     { 69,   "udma5" },
1783     { 70,   "udma6" },
1784     { 71,   "udma7" },
1785     { 0, NULL }
1786 };
1787
1788 static int translate_xfermode(char * name)
1789 {
1790         const struct xfermode_entry *tmp;
1791         char *endptr;
1792         int val = -1;
1793
1794
1795         for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1796         {
1797                 if (!strcmp(name, tmp->name))
1798                         return tmp->val;
1799
1800         }
1801
1802         val = strtol(name, &endptr, 10);
1803         if (*endptr == '\0')
1804                 return val;
1805
1806         return -1;
1807 }
1808
1809 static void interpret_xfermode (unsigned int xfermode)
1810 {
1811         printf(" (");
1812         switch(xfermode) {
1813                 case 0:
1814                         printf("default PIO mode");
1815                         break;
1816                 case 1:
1817                         printf("default PIO mode, disable IORDY");
1818                         break;
1819                 case 8:
1820                 case 9:
1821                 case 10:
1822                 case 11:
1823                 case 12:
1824                 case 13:
1825                 case 14:
1826                 case 15:
1827                         printf("PIO flow control mode%u", xfermode-8);
1828                         break;
1829                 case 16:
1830                 case 17:
1831                 case 18:
1832                 case 19:
1833                 case 20:
1834                 case 21:
1835                 case 22:
1836                 case 23:
1837                         printf("singleword DMA mode%u", xfermode-16);
1838                         break;
1839                 case 32:
1840                 case 33:
1841                 case 34:
1842                 case 35:
1843                 case 36:
1844                 case 37:
1845                 case 38:
1846                 case 39:
1847                         printf("multiword DMA mode%u", xfermode-32);
1848                         break;
1849                 case 64:
1850                 case 65:
1851                 case 66:
1852                 case 67:
1853                 case 68:
1854                 case 69:
1855                 case 70:
1856                 case 71:
1857                         printf("UltraDMA mode%u", xfermode-64);
1858                         break;
1859                 default:
1860                         printf("unknown, probably not valid");
1861                         break;
1862         }
1863         printf(")\n");
1864 }
1865 #endif /* HDIO_DRIVE_CMD */
1866
1867 #ifndef VXVM_MAJOR
1868 #define VXVM_MAJOR 199
1869 #endif
1870
1871 #ifndef CCISS_MAJOR
1872 #define CCISS_MAJOR 104
1873 #endif
1874
1875 static void process_dev (char *devname)
1876 {
1877         int fd;
1878         static long parm, multcount;
1879         struct stat stat_buf;
1880 #ifndef HDIO_DRIVE_CMD
1881         int force_operation = 0;
1882 #endif
1883         if (stat(devname,&stat_buf))
1884                 bb_perror_msg_and_die(devname);
1885
1886         switch(major(stat_buf.st_rdev))
1887         {
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):
1897 #else
1898                 case (SCSI_DISK_MAJOR):
1899 #endif
1900 #ifdef MD_MAJOR
1901                 case (MD_MAJOR):
1902 #endif
1903                 case (VXVM_MAJOR):
1904 #ifdef LVM_BLK_MAJOR
1905                 case (LVM_BLK_MAJOR):
1906 #endif
1907                 case (CCISS_MAJOR):
1908                         is_scsi_hd = 1;
1909                         break;
1910 #ifdef XT_DISK_MAJOR
1911                 case (XT_DISK_MAJOR):
1912                         is_xt_hd = 1;
1913                         break;
1914 #endif
1915                 case IDE0_MAJOR:
1916                 case IDE1_MAJOR:
1917 #ifdef IDE2_MAJOR
1918                 case IDE2_MAJOR:
1919 #endif
1920 #ifdef IDE3_MAJOR
1921                 case IDE3_MAJOR:
1922 #endif
1923 #ifdef IDE4_MAJOR
1924                 case IDE4_MAJOR:
1925 #endif
1926 #ifdef IDE5_MAJOR
1927                 case IDE5_MAJOR:
1928 #endif
1929 #ifdef IDE6_MAJOR
1930                 case IDE6_MAJOR:
1931 #endif
1932 #ifdef IDE7_MAJOR
1933                 case IDE7_MAJOR:
1934 #endif
1935 #ifdef IDE8_MAJOR
1936                 case IDE8_MAJOR:
1937 #endif
1938 #ifdef IDE9_MAJOR
1939                 case IDE9_MAJOR:
1940 #endif
1941                         break;  /* do nothing */
1942                 default:
1943                         bb_error_msg_and_die("%s not supported",devname);
1944         }
1945
1946         fd = bb_xopen (devname, O_RDONLY|O_NONBLOCK);
1947         if_printf( (!quiet),"\n%s:\n", devname);
1948
1949         if (set_readahead)
1950         {
1951                 if_printf(get_readahead," setting fs readahead to %ld\n", bbreadahead);
1952                 bb_ioctl(fd, BLKRASET,(int *)bbreadahead,"BLKRASET");
1953         }
1954 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1955         if (unregister_hwif)
1956         {
1957                 no_scsi();
1958                 printf(" attempting to unregister hwif#%u\n", hwif);
1959                 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)hwif,"HDIO_UNREGISTER_HWIF");
1960         }
1961 #endif
1962 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1963         if (scan_hwif)
1964         {
1965                 int     args[3];
1966                 no_scsi();
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;
1970                 args[2] = hwif_irq;
1971                 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
1972         }
1973 #endif
1974         if (set_piomode)
1975         {
1976                 no_scsi();
1977                 no_xt();
1978
1979                 if (noisy_piomode)
1980                 {
1981                         printf(" attempting to ");
1982                         if (piomode == 255)
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));
1988                         else
1989                                 printf("set UDMA mode to %d\n", (piomode-200));
1990                 }
1991                 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)piomode, "HDIO_SET_PIO_MODE");
1992         }
1993         if (set_io32bit)
1994         {
1995                 no_scsi();
1996                 no_xt();
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");
1999         }
2000         if (set_mult)
2001         {
2002                 no_scsi();
2003                 no_xt();
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
2008                 else
2009                         force_operation = 1;
2010 #endif
2011         }
2012         if (set_readonly)
2013         {
2014                 if_printf_on_off(get_readonly," setting readonly to %ld", readonly);
2015                 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
2016         }
2017         if (set_unmask)
2018         {
2019                 no_scsi();
2020                 no_xt();
2021                 if_printf_on_off(get_unmask," setting unmaskirq to %ld", unmask);
2022                 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
2023         }
2024 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2025         if (set_dma)
2026         {
2027                 no_scsi();
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");
2030         }
2031 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
2032         if (set_dma_q)
2033         {
2034                 no_scsi();
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");
2037         }
2038         if (set_nowerr)
2039         {
2040                 no_scsi();
2041                 no_xt();
2042                 if_printf_on_off(get_nowerr," setting nowerr to %ld", nowerr);
2043                 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
2044         }
2045         if (set_keep)
2046         {
2047                 no_scsi();
2048                 no_xt();
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");
2051         }
2052 #ifdef HDIO_DRIVE_CMD
2053         if (set_doorlock)
2054         {
2055                 unsigned char args[4] = {0,0,0,0};
2056                 no_scsi();
2057                 no_xt();
2058
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)");
2062         }
2063         if (set_dkeep)
2064         {
2065                 /* lock/unlock the drive's "feature" settings */
2066                 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2067                 no_scsi();
2068                 no_xt();
2069
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)");
2073         }
2074         if (set_defects)
2075         {
2076                 unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
2077                 no_scsi();
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)");
2081         }
2082         if (set_prefetch)
2083         {
2084                 unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
2085                 no_scsi();
2086                 no_xt();
2087
2088                 args[1] = prefetch;
2089                 if_printf(get_prefetch," setting drive prefetch to %ld\n", prefetch);
2090                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
2091         }
2092         if (set_xfermode)
2093         {
2094                 unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
2095                 no_scsi();
2096                 no_xt();
2097
2098                 args[1] = xfermode_requested;
2099                 if (get_xfermode)
2100                 {
2101                         printf(" setting xfermode to %d", xfermode_requested);
2102                         interpret_xfermode(xfermode_requested);
2103                 }
2104                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
2105         }
2106         if (set_lookahead)
2107         {
2108                 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2109                 no_scsi();
2110                 no_xt();
2111
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)");
2115         }
2116         if (set_apmmode)
2117         {
2118                 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2119                 no_scsi();
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");
2123                 if (apmmode==255)
2124                 {
2125                         /* disable Advanced Power Management */
2126                         args[2] = 0x85; /* feature register */
2127                         if_printf(get_apmmode," disabled\n");
2128                 }
2129                 else
2130                 {
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);
2135                 }
2136                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
2137         }
2138         if (set_wcache)
2139         {
2140 #ifdef DO_FLUSHCACHE
2141 #ifndef WIN_FLUSHCACHE
2142 #define WIN_FLUSHCACHE 0xe7
2143 #endif
2144                 unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
2145 #endif /* DO_FLUSHCACHE */
2146                 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2147                 no_scsi();
2148                 no_xt();
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 */
2160         }
2161         if (set_standbynow)
2162         {
2163 #ifndef WIN_STANDBYNOW1
2164 #define WIN_STANDBYNOW1 0xE0
2165 #endif
2166 #ifndef WIN_STANDBYNOW2
2167 #define WIN_STANDBYNOW2 0x94
2168 #endif
2169                 unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
2170                 unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
2171                 no_scsi();
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)");
2176         }
2177         if (set_sleepnow)
2178         {
2179 #ifndef WIN_SLEEPNOW1
2180 #define WIN_SLEEPNOW1 0xE6
2181 #endif
2182 #ifndef WIN_SLEEPNOW2
2183 #define WIN_SLEEPNOW2 0x99
2184 #endif
2185                 unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
2186                 unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
2187                 no_scsi();
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)");
2192         }
2193         if (set_seagate)
2194         {
2195                 unsigned char args[4] = {0xfb,0,0,0};
2196                 no_scsi();
2197                 no_xt();
2198                 if_printf(get_seagate," disabling Seagate auto powersaving mode\n");
2199                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
2200         }
2201         if (set_standby)
2202         {
2203                 unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
2204                 no_scsi();
2205                 no_xt();
2206                 if (get_standby)
2207                 {
2208                         printf(" setting standby to %lu", standby_requested);
2209                         interpret_standby(standby_requested);
2210                 }
2211                 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
2212         }
2213 #else   /* HDIO_DRIVE_CMD */
2214         if (force_operation)
2215         {
2216                 char buf[512];
2217                 flush_buffer_cache(fd);
2218                 if (-1 == read(fd, buf, sizeof(buf)))
2219                         bb_error_msg("access failed");
2220         }
2221 #endif  /* HDIO_DRIVE_CMD */
2222
2223         if (!flagcount)
2224                 verbose = 1;
2225
2226         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity)
2227         {
2228                 no_scsi();
2229                 multcount = -1;
2230                 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
2231                 {
2232                         if ((verbose && !is_xt_hd) || get_mult)
2233                                 bb_error_msg("HDIO_GET_MULTCOUNT");
2234                 }
2235                 else if (verbose | get_mult)
2236                 {
2237                         printf(" multcount    = %2ld", multcount);
2238                         on_off(multcount);
2239                 }
2240         }
2241         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit)
2242         {
2243                 no_scsi();
2244                 no_xt();
2245                 if(ioctl(fd, HDIO_GET_32BIT, &parm))
2246                         bb_error_msg("HDIO_GET_32BIT");
2247                 else
2248                 {
2249                         printf(" IO_support   =%3ld (", parm);
2250                         switch (parm)
2251                         {
2252                                 case 0:
2253                                         printf("default ");
2254                                 case 2:
2255                                         printf("16-bit)\n");
2256                                         break;
2257                                 case 1:
2258                                         printf("32-bit)\n");
2259                                         break;
2260                                 case 3:
2261                                         printf("32-bit w/sync)\n");
2262                                         break;
2263                                 case 8:
2264                                         printf("Request-Queue-Bypass)\n");
2265                                         break;
2266                                 default:
2267                                         printf("\?\?\?)\n");
2268                                 /*esac*/
2269                         }
2270                 }
2271         }
2272         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask)
2273         {
2274                 no_scsi();
2275                 no_xt();
2276                 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
2277                                                 "HDIO_GET_UNMASKINTR"," unmaskirq    = %2ld");
2278         }
2279
2280
2281 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2282         if ((verbose && !is_scsi_hd) || get_dma) {
2283                 no_scsi();
2284                 if(ioctl(fd, HDIO_GET_DMA, &parm))
2285                         bb_error_msg("HDIO_GET_DMA");
2286                 else
2287                 {
2288                         printf(" using_dma    = %2ld", parm);
2289                         if (parm == 8)
2290                                 printf(" (DMA-Assisted-PIO)\n");
2291                         else
2292                                 on_off(parm);
2293                 }
2294         }
2295 #endif
2296         if (get_dma_q)
2297         {
2298                 no_scsi();
2299                 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
2300                                                   "HDIO_GET_QDMA"," queue_depth  = %2ld");
2301         }
2302         if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep)
2303         {
2304                 no_scsi();
2305                 no_xt();
2306                 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
2307                                                         "HDIO_GET_KEEPSETTINGS"," keepsettings = %2ld");
2308         }
2309
2310         if (get_nowerr)
2311         {
2312                 no_scsi();
2313                 no_xt();
2314                 bb_ioctl_on_off  (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
2315                                                         " HDIO_GET_NOWERR"," nowerr       = %2ld");
2316         }
2317         if (verbose || get_readonly)
2318         {
2319                 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
2320                                                   " BLKROGET"," readonly     = %2ld");
2321         }
2322         if ((verbose && !is_scsi_hd) || get_readahead)
2323         {
2324                 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
2325                                                         " BLKRAGET"," readahead    = %2ld");
2326         }
2327         if (verbose || get_geom)
2328         {
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;
2333 #endif
2334
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);
2340 #endif
2341                 else if (ioctl(fd, HDIO_GETGEO, &g))
2342                         bb_error_msg("HDIO_GETGEO");
2343                 else
2344                         printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
2345         }
2346 #ifdef HDIO_DRIVE_CMD
2347         if (get_powermode)
2348         {
2349 #ifndef WIN_CHECKPOWERMODE1
2350 #define WIN_CHECKPOWERMODE1 0xE5
2351 #endif
2352 #ifndef WIN_CHECKPOWERMODE2
2353 #define WIN_CHECKPOWERMODE2 0x98
2354 #endif
2355                 unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
2356                 const char *state;
2357                 no_scsi();
2358                 if (ioctl(fd, HDIO_DRIVE_CMD, &args)
2359                          && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
2360                         && ioctl(fd, HDIO_DRIVE_CMD, &args))
2361                 {
2362                         if (errno != EIO || args[0] != 0 || args[1] != 0)
2363                                 state = "unknown";
2364                         else
2365                                 state = "sleeping";
2366                 }
2367                 else
2368                         state = (args[2] == 255) ? "active/idle" : "standby";
2369
2370                 printf(" drive state is:  %s\n", state);
2371         }
2372 #endif
2373 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2374         if (perform_reset)
2375         {
2376                 no_scsi();
2377                 no_xt();
2378                 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
2379         }
2380 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2381 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2382         if (perform_tristate)
2383         {
2384                 unsigned char args[4] = {0,tristate,0,0};
2385                 no_scsi();
2386                 no_xt();
2387                 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
2388         }
2389 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2390 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2391         if (get_identity)
2392         {
2393                 static struct hd_driveid id;
2394
2395                 no_scsi();
2396                 no_xt();
2397
2398                 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
2399                 {
2400                         if (multcount != -1)
2401                         {
2402                                 id.multsect = multcount;
2403                                 id.multsect_valid |= 1;
2404                         }
2405                         else
2406                                 id.multsect_valid &= ~1;
2407                         dump_identity(&id);
2408                 }
2409                 else if (errno == -ENOMSG)
2410                         printf(" no identification info available\n");
2411                 else
2412                         bb_error_msg("HDIO_GET_IDENTITY");
2413         }
2414
2415         if (get_IDentity)
2416         {
2417                 unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
2418                 unsigned i;
2419
2420                 no_scsi();
2421                 no_xt();
2422
2423                 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2424                 {
2425                         args[0] = WIN_PIDENTIFY;
2426                         if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2427                         {
2428                                 bb_error_msg("HDIO_DRIVE_CMD(identify)");
2429                                 goto identify_abort;
2430                         }
2431                 }
2432                 for(i=0; i<(sizeof args)/2; i+=2)
2433                     __le16_to_cpus((uint16_t *)(&args[i]));
2434
2435                 identify((void *)&args[4], NULL);
2436 identify_abort:
2437         /* VOID */;
2438         }
2439 #endif
2440 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2441         if (set_busstate)
2442         {
2443                 no_scsi();
2444                 if (get_busstate)
2445                 {
2446                         printf(" setting bus state to %d", busstate);
2447                         bus_state_value(busstate);
2448                 }
2449                 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)busstate, "HDIO_SET_BUSSTATE");
2450         }
2451 #endif
2452 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2453         if (get_busstate)
2454         {
2455                 no_scsi();
2456                 if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
2457                         bb_error_msg("HDIO_GET_BUSSTATE");
2458                 else
2459                 {
2460                         printf(" busstate     = %2ld", parm);
2461                         bus_state_value(parm);
2462                 }
2463         }
2464 #endif
2465         if (reread_partn)
2466                 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2467
2468
2469         if (do_ctimings)
2470                 do_time(0,fd);          /*time cache  */
2471         if (do_timings)
2472                 do_time(1,fd);          /*time device */
2473         if (do_flush)
2474                 flush_buffer_cache (fd);
2475         close (fd);
2476 }
2477
2478 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2479 static int fromhex (unsigned char c)
2480 {
2481         if (c >= 'a' && c <= 'f')
2482                 return 10 + (c - 'a');
2483         if (c >= '0' && c <= '9')
2484                 return (c - '0');
2485         bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2486 }
2487
2488 static int identify_from_stdin (void)
2489 {
2490         unsigned short sbuf[800];
2491         unsigned char  buf[1600], *b = (unsigned char *)buf;
2492         int i, count = read(0, buf, 1280);
2493
2494         if (count != 1280)
2495                 bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
2496         for (i = 0; count >= 4; ++i)
2497         {
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]));
2500                 b += 5;
2501                 count -= 5;
2502         }
2503         identify(sbuf, NULL);
2504         return 0;
2505 }
2506 #endif
2507
2508 /* our main() routine: */
2509 int hdparm_main(int argc, char **argv)
2510 {
2511         const char * const bb_msg_missing_value ="missing value";
2512         char c, *p;
2513         char *tmpstr;
2514         char name[32];
2515         /*int neg;*/
2516
2517         ++argv;
2518         if (!--argc)
2519                 bb_show_usage();
2520
2521         while (argc--)
2522         {
2523 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2524                 if (!strcmp("-Istdin", *argv))
2525                 {
2526                         exit(identify_from_stdin());
2527                 }
2528 #endif
2529                 p = *argv++;
2530                 if (*p == '-')
2531                 {
2532                         if (!*++p)
2533                                 bb_show_usage();
2534                         while ((c = *p++))
2535                         {
2536                                 ++flagcount;
2537                                 switch (c)
2538                                 {
2539                                         case 'V':
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);
2543                                                 exit(EXIT_SUCCESS);
2544                                         case 'v':
2545                                                 verbose = 1;
2546                                                 break;
2547 #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2548                                         case 'I':
2549                                                 get_IDentity = 1;
2550                                                 break;
2551                                         case 'i':
2552                                                 get_identity = 1;
2553                                                 break;
2554 #endif
2555                                         case 'g':
2556                                                 get_geom = 1;
2557                                                 break;
2558                                         case 'f':
2559                                                 do_flush = 1;
2560                                                 break;
2561                                         case 'q':
2562                                                 quiet = 1;
2563                                                 noisy = 0;
2564                                                 break;
2565                                         case 'u':
2566                                                 get_unmask = noisy;
2567                                                 noisy = 1;
2568                                                 p = check_ptr(p,argc,argv);
2569                                                 if((set_unmask = set_flag(p,'1'))==1)
2570                                                         unmask  = *p++ - '0';
2571                                                 break;
2572 #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
2573                                         case 'd':
2574                                                 get_dma = noisy;
2575                                                 noisy = 1;
2576                                                 p = check_ptr(p,argc,argv);
2577                                                 if((set_dma = set_flag(p,'9'))==1)
2578                                                         dma = *p++ - '0';
2579                                                 break;
2580 #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
2581                                         case 'n':
2582                                                 get_nowerr = noisy;
2583                                                 noisy = 1;
2584                                                 p = check_ptr(p,argc,argv);
2585                                                 if((set_nowerr = set_flag(p,'1'))==1)
2586                                                         nowerr  = *p++ - '0';
2587                                                 break;
2588                                         case 'p':
2589                                                 noisy_piomode = noisy;
2590                                                 noisy = 1;
2591                                                 p = check_ptr_v2(p,argc,argv);
2592                                                 tmpstr = name;
2593                                                 tmpstr[0] = '\0';
2594                                                 while (isalnum(*p) && (tmpstr - name) < 31)
2595                                                 {
2596                                                         tmpstr[0] = *p++;
2597                                                         tmpstr[1] = '\0';
2598                                                         ++tmpstr;
2599                                                 }
2600                                                 piomode = translate_xfermode(name);
2601                                                 if (piomode == -1)
2602                                                         set_piomode = 0;
2603                                                 else
2604                                                         set_piomode = 1;
2605                                                 break;
2606                                         case 'r':
2607                                                 get_readonly = noisy;
2608                                                 noisy = 1;
2609                                                 p = check_ptr(p,argc,argv);
2610                                                 if((set_readonly = set_flag(p,'1'))==1)
2611                                                         readonly  = *p++ - '0';
2612                                                 break;
2613                                         case 'm':
2614                                                 get_mult = noisy;
2615                                                 noisy = 1;
2616                                                 p = check_ptr(p,argc,argv);
2617                                                 while (isdigit(*p))
2618                                                 {
2619                                                         set_mult = 1;
2620                                                         mult = (mult * 10) + (*p++ - '0');
2621                                                 }
2622                                                 break;
2623                                         case 'c':
2624                                                 get_io32bit = noisy;
2625                                                 noisy = 1;
2626                                                 p = check_ptr(p,argc,argv);
2627                                                 while (isdigit(*p))
2628                                                 {
2629                                                         set_io32bit = 1;
2630                                                         io32bit = (io32bit * 10) + (*p++ - '0');
2631                                                 }
2632                                                 break;
2633 #ifdef HDIO_DRIVE_CMD
2634                                         case 'S':
2635                                                 get_standby = noisy;
2636                                                 noisy = 1;
2637                                                 p = check_ptr(p,argc,argv);
2638                                                 while (isdigit(*p))
2639                                                 {
2640                                                         set_standby = 1;
2641                                                         standby_requested = (standby_requested * 10) + (*p++ - '0');
2642                                                 }
2643                                                 if (!set_standby)
2644                                                         bb_error_msg("-S: %s", bb_msg_missing_value);
2645                                                 break;
2646
2647                                         case 'D':
2648                                                 get_defects = noisy;
2649                                                 noisy = 1;
2650                                                 p = check_ptr(p,argc,argv);
2651                                                 while (isdigit(*p))
2652                                                 {
2653                                                         set_defects = 1;
2654                                                         defects = (defects * 10) + (*p++ - '0');
2655                                                 }
2656                                                 if (!set_defects)
2657                                                         bb_error_msg("-D: %s", bb_msg_missing_value);
2658                                                 break;
2659                                         case 'P':
2660                                                 get_prefetch = noisy;
2661                                                 noisy = 1;
2662                                                 p = check_ptr(p,argc,argv);
2663                                                 while (isdigit(*p))
2664                                                 {
2665                                                         set_prefetch = 1;
2666                                                         prefetch = (prefetch * 10) + (*p++ - '0');
2667                                                 }
2668                                                 if (!set_prefetch)
2669                                                         bb_error_msg("-P: %s", bb_msg_missing_value);
2670                                                 break;
2671
2672                                         case 'X':
2673                                                 get_xfermode = noisy;
2674                                                 noisy = 1;
2675                                                 p = check_ptr_v2(p,argc,argv);
2676                                                 tmpstr = name;
2677                                                 tmpstr[0] = '\0';
2678                                                 while (isalnum(*p) && (tmpstr - name) < 31)
2679                                                 {
2680                                                         tmpstr[0] = *p++;
2681                                                         tmpstr[1] = '\0';
2682                                                         ++tmpstr;
2683                                                 }
2684                                                 xfermode_requested = translate_xfermode(name);
2685                                                 if (xfermode_requested == -1)
2686                                                         set_xfermode = 0;
2687                                                 else
2688                                                         set_xfermode = 1;
2689                                                 if (!set_xfermode)
2690                                                         bb_error_msg("-X: %s", bb_msg_missing_value);
2691                                                 break;
2692
2693                                         case 'K':
2694                                                 get_dkeep = noisy;
2695                                                 noisy = 1;
2696                                                 p = check_ptr(p,argc,argv);
2697                                                 if((set_dkeep = set_flag(p,'1'))==1)
2698                                                         dkeep  = *p++ - '0';
2699                                                 else
2700                                                         bb_error_msg("-K: %s (0/1)", bb_msg_missing_value);
2701                                                 break;
2702
2703                                         case 'A':
2704                                                 get_lookahead = noisy;
2705                                                 noisy = 1;
2706                                                 p = check_ptr(p,argc,argv);
2707                                                 if((set_lookahead = set_flag(p,'1'))==1)
2708                                                         lookahead  = *p++ - '0';
2709                                                 else
2710                                                         bb_error_msg("-A: %s (0/1)", bb_msg_missing_value);
2711                                                 break;
2712
2713                                         case 'L':
2714                                                 get_doorlock = noisy;
2715                                                 noisy = 1;
2716                                                 p = check_ptr(p,argc,argv);
2717                                                 if((set_doorlock = set_flag(p,'1'))==1)
2718                                                         doorlock  = *p++ - '0';
2719                                                 else
2720                                                         bb_error_msg("-L: %s (0/1)", bb_msg_missing_value);
2721                                                 break;
2722
2723                                         case 'W':
2724                                                 get_wcache = noisy;
2725                                                 noisy = 1;
2726                                                 p = check_ptr(p,argc,argv);
2727                                                 if((set_wcache = set_flag(p,'1'))==1)
2728                                                         wcache  = *p++ - '0';
2729                                                 else
2730                                                         bb_error_msg("-W: %s (0/1)", bb_msg_missing_value);
2731                                                 break;
2732
2733                                         case 'C':
2734                                                 get_powermode = noisy;
2735                                                 noisy = 1;
2736                                                 break;
2737
2738                                         case 'y':
2739                                                 get_standbynow = noisy;
2740                                                 noisy = 1;
2741                                                 set_standbynow = 1;
2742                                                 break;
2743
2744                                         case 'Y':
2745                                                 get_sleepnow = noisy;
2746                                                 noisy = 1;
2747                                                 set_sleepnow = 1;
2748                                                 break;
2749
2750                                         case 'z':
2751                                                 reread_partn = 1;
2752                                                 break;
2753
2754                                         case 'Z':
2755                                                 get_seagate = noisy;
2756                                                 noisy = 1;
2757                                                 set_seagate = 1;
2758                                                 break;
2759 #endif /* HDIO_DRIVE_CMD */
2760                                         case 'k':
2761                                                 get_keep = noisy;
2762                                                 noisy = 1;
2763                                                 p = check_ptr(p,argc,argv);
2764                                                 if((set_keep = set_flag(p,'1'))==1)
2765                                                         keep  = *p++ - '0';
2766                                                 break;
2767 #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
2768                                         case 'U':
2769                                                 p = check_ptr(p,argc,argv);
2770                                                 if(! p)
2771                                                         goto error; /* "expected hwif_nr" */
2772
2773                                                 sscanf(p++, "%i", &hwif);
2774
2775                                                 unregister_hwif = 1;
2776                                                 break;
2777 #endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
2778 #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
2779                                         case 'R':
2780                                                 p = check_ptr(p,argc,argv);
2781                                                 if(! p)
2782                                                         goto error; /* "expected hwif_data" */
2783
2784                                                 sscanf(p++, "%i", &hwif_data);
2785
2786                                                 if (argc && isdigit(**argv))
2787                                                         p = *argv++, --argc;
2788                                                 else
2789                                                         goto error; /* "expected hwif_ctrl" */
2790
2791                                                 sscanf(p, "%i", &hwif_ctrl);
2792
2793                                                 if (argc && isdigit(**argv))
2794                                                         p = *argv++, --argc;
2795                                                 else
2796 error:
2797                                                         bb_error_msg_and_die("expected hwif value"); /* "expected hwif_irq" */
2798
2799                                                 sscanf(p, "%i", &hwif_irq);
2800
2801                                                 *p = '\0';
2802
2803                                                 scan_hwif = 1;
2804                                                 break;
2805 #endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
2806                                         case 'Q':
2807                                                 get_dma_q = noisy;
2808                                                 noisy = 1;
2809                                                 /* neg = 0; */
2810                                                 p = check_ptr(p,argc,argv);
2811                                                 while (isdigit(*p))
2812                                                 {
2813                                                         set_dma_q = 1;
2814                                                         dma_q = (dma_q * 10) + (*p++ - '0');
2815                                                 }
2816                                                 /* what is this for ? as neg = 0 (see above) it seems to do nothing */
2817                                                 /*if (neg)
2818                                                         dma_q = -dma_q;*/
2819                                                 break;
2820 #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
2821                                         case 'w':
2822                                                 perform_reset = 1;
2823                                                 break;
2824 #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2825 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2826                                         case 'x':
2827                                                 p = check_ptr(p,argc,argv);
2828                                                 if((perform_tristate = set_flag(p,'1'))==1)
2829                                                         tristate  = *p++ - '0';
2830                                                 else
2831                                                         bb_error_msg("-x: %s (0/1)", bb_msg_missing_value);
2832                                                 break;
2833
2834 #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2835                                         case 'a':
2836                                                 get_readahead = noisy;
2837                                                 noisy = 1;
2838                                                 p = check_ptr(p,argc,argv);
2839                                                 while (isdigit(*p))
2840                                                 {
2841                                                         set_readahead = 1;
2842                                                         bbreadahead = (bbreadahead * 10) + (*p++ - '0');
2843                                                 }
2844                                                 break;
2845                                         case 'B':
2846                                                 get_apmmode = noisy;
2847                                                 noisy = 1;
2848                                                 p = check_ptr(p,argc,argv);
2849                                                 while (isdigit(*p))
2850                                                 {
2851                                                         set_apmmode = 1;
2852                                                         apmmode = (io32bit * 10) + (*p++ - '0');
2853                                                 }
2854                                                 if (!set_apmmode)
2855                                                         printf("-B: %s (1-255)", bb_msg_missing_value);
2856                                                 break;
2857                                         case 't':
2858                                                 do_timings = 1;
2859                                                 do_flush = 1;
2860                                                 break;
2861                                         case 'T':
2862                                                 do_ctimings = 1;
2863                                                 do_flush = 1;
2864                                                 break;
2865 #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
2866                                         case 'b':
2867                                                 get_busstate = noisy;
2868                                                 noisy = 1;
2869                                                 p = check_ptr(p,argc,argv);
2870                                                 if((set_busstate = set_flag(p,'2'))==1)
2871                                                         busstate  = *p++ - '0';
2872                                                 break;
2873 #endif
2874                                         case 'h':
2875                                         default:
2876                                                 bb_show_usage();
2877                                 }
2878                         }
2879                         if (!argc)
2880                                 bb_show_usage();
2881                 }
2882                 else
2883                         process_dev (p);
2884         }
2885         return 0 ;
2886 }