blob: eaa61c102e9804b6901a06c1acda4fb21d3db4c8 [file] [log] [blame]
Eric Andersen3443bd72003-07-22 07:30:36 +00001/* vi: set sw=4 ts=4: */
2/*
3 * hdparm implementation for busybox
4 *
5 *
Glenn L McGrath5be6a202003-11-28 22:55:03 +00006 * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
Glenn L McGrath07085852003-10-09 07:28:22 +00007 *
8 * Hacked by Tito <farmatito@tiscali.it> for size optimization.
Eric Andersen3443bd72003-07-22 07:30:36 +00009 *
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 */
Glenn L McGrath07085852003-10-09 07:28:22 +000030
Eric Andersen3443bd72003-07-22 07:30:36 +000031#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
Glenn L McGratha55d72b2003-10-09 11:38:45 +000051#if (__BYTE_ORDER == __BIG_ENDIAN) && !defined(__USE_XOPEN)
Eric Andersen3443bd72003-07-22 07:30:36 +000052#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
Glenn L McGrath07085852003-10-09 07:28:22 +0000121 in low byte, 0x40 in high byte. */
Eric Andersen3443bd72003-07-22 07:30:36 +0000122#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 */
Glenn L McGrath07085852003-10-09 07:28:22 +0000149#define NOT_ATA 0x8000
Eric Andersen3443bd72003-07-22 07:30:36 +0000150#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
168static 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};
202static 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
Glenn L McGrath07085852003-10-09 07:28:22 +0000225/* word 2: specific configureation
Eric Andersen3443bd72003-07-22 07:30:36 +0000226 * (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
279static 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
Glenn L McGrath07085852003-10-09 07:28:22 +0000314static const char actual_ver[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000315 /* 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*/
Glenn L McGrath07085852003-10-09 07:28:22 +0000352#define SUPPORT_48_BIT 0x0400
Eric Andersen3443bd72003-07-22 07:30:36 +0000353#define NUM_CMD_FEAT_STR 48
354
355#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
356static 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
Glenn L McGrath07085852003-10-09 07:28:22 +0000413 * been enabled with words 85-87
Eric Andersen3443bd72003-07-22 07:30:36 +0000414 */
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
Glenn L McGrath07085852003-10-09 07:28:22 +0000432
Eric Andersen3443bd72003-07-22 07:30:36 +0000433/* 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
438static 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
Glenn L McGrath07085852003-10-09 07:28:22 +0000458#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
470const char * const bb_msg_shared_mem ="could not %s sharedmem buf";
471const char * const bb_msg_op_not_supp =" operation not supported on %s disks";
472
473static 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
479static 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
488static void on_off (unsigned int value);
489
490static 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
499static 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}
Eric Andersen416c2422003-12-12 00:08:57 +0000510
511#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000512static 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
523static void print_ascii(uint16_t *p, uint8_t length);
524
525static 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
Eric Andersen416c2422003-12-12 00:08:57 +0000534static void if_strcat(unsigned long test, char *modes, char *string)
535{
536 if (test)
537 strcat(modes,string);
538}
539#endif
540
Glenn L McGrath07085852003-10-09 07:28:22 +0000541static void sync_and_sleep(int i)
542{
543 sync();
544 sleep(i);
545}
546
547static void check_if_min_and_set_val(uint16_t a, uint16_t b)
548{
549 if( a < b)
550 a = b;
551}
552
553static void check_if_maj_and_set_val(uint16_t a, uint16_t b)
554{
555 if( a > b)
556 a = b;
557}
558
559char * check_ptr(char *p, int argc, char **argv)
560{
561 if (!*p && argc && isdigit(**argv))
562 p = *argv++, --argc;
563 return p;
564}
565
566char * 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
573unsigned long int set_flag(char *p, char max)
574{
575 if (*p >= '0' && *p <= max )
576 return 1;
577 return 0;
578}
579
Glenn L McGrath07085852003-10-09 07:28:22 +0000580/* end of busybox specific stuff */
581
Eric Andersen3443bd72003-07-22 07:30:36 +0000582#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000583static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
584{
Eric Andersen3443bd72003-07-22 07:30:36 +0000585 uint16_t ii;
586 uint8_t err_dma = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000587
588 for(ii = 0; ii <= MODE_MAX; ii++)
589 {
590 if(mode_sel & 0x0001)
591 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000592 printf("*%cdma%u ",cc,ii);
Glenn L McGrath07085852003-10-09 07:28:22 +0000593 if(*have_mode)
594 err_dma = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000595 *have_mode = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000596 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000597 else if(mode_sup & 0x0001)
598 printf("%cdma%u ",cc,ii);
599
600 mode_sup >>=1;
601 mode_sel >>=1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000602 }
603 return err_dma;
604}
Glenn L McGrath07085852003-10-09 07:28:22 +0000605
Eric Andersen3443bd72003-07-22 07:30:36 +0000606static void print_ascii(uint16_t *p, uint8_t length) {
607 uint8_t ii;
608 char cl;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000609
Eric Andersen3443bd72003-07-22 07:30:36 +0000610 /* find first non-space & print it */
Glenn L McGrath07085852003-10-09 07:28:22 +0000611 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++;
Eric Andersen3443bd72003-07-22 07:30:36 +0000620 break;
621 }
622 p++;
623 }
624 /* print the rest */
Glenn L McGrath07085852003-10-09 07:28:22 +0000625 for(; ii< length; ii++)
626 {
627 if(!(*p))
628 break; /* some older devices have NULLs */
Eric Andersen3443bd72003-07-22 07:30:36 +0000629 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. */
639static void identify (uint16_t *id_supplied, const char *devname)
640{
641
Glenn L McGrath07085852003-10-09 07:28:22 +0000642 char *id_file = NULL;
Eric Andersen3443bd72003-07-22 07:30:36 +0000643 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
Glenn L McGrath07085852003-10-09 07:28:22 +0000652 if (id_supplied)
653 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000654#if __BYTE_ORDER == __BIG_ENDIAN
655 swab(id_supplied, val, sizeof(val));
656#else
657 memcpy(val, id_supplied, sizeof(val));
658#endif
Glenn L McGrath07085852003-10-09 07:28:22 +0000659 }
660 else
661 {
662 id_file = xcalloc(1, strlen(devname)+22);
663 sprintf(id_file, "/proc/ide/%s/identify", devname);
Eric Andersen3443bd72003-07-22 07:30:36 +0000664 /* open the file, read in all the info and close it */
Glenn L McGrath07085852003-10-09 07:28:22 +0000665 if (id_file == NULL)
Eric Andersen3443bd72003-07-22 07:30:36 +0000666 fl = stdin;
Glenn L McGrath07085852003-10-09 07:28:22 +0000667 else
668 fl = bb_xfopen(id_file, "r");
669
Eric Andersen3443bd72003-07-22 07:30:36 +0000670 /* calculate checksum over all bytes */
Glenn L McGrath07085852003-10-09 07:28:22 +0000671 for(ii = GEN_CONFIG; ii<=INTEGRITY; ii++)
672 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000673 unsigned int scratch;
Glenn L McGrath07085852003-10-09 07:28:22 +0000674 if(1 != fscanf(fl,"%04x",&scratch))
675 break;
Eric Andersen3443bd72003-07-22 07:30:36 +0000676 val[ii] = (uint16_t)scratch;
677 chksum += val[ii] + (val[ii] >> 8);
678 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000679 fclose(fl);
680 /*bb_fclose_nonstdin(fl);*/
Eric Andersen3443bd72003-07-22 07:30:36 +0000681 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");
Glenn L McGrath07085852003-10-09 07:28:22 +0000688 if(!(val[GEN_CONFIG] & NOT_ATA))
689 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000690 dev = ATA_DEV;
691 printf("ATA device, with ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000692 }
693 else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
694 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000695 dev = ATA_DEV;
696 like_std = 4;
697 printf("CompactFlash ATA device, with ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000698 }
699 else if(!(val[GEN_CONFIG] & NOT_ATAPI))
700 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000701 dev = ATAPI_DEV;
702 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
703 printf("ATAPI %s, with ", pkt_str[eqpt]);
704 like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000705 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000706 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");
Eric Andersen3443bd72003-07-22 07:30:36 +0000709
Glenn L McGrath07085852003-10-09 07:28:22 +0000710 if_printf(!(val[GEN_CONFIG] & MEDIA_REMOVABLE),"non-");
711 printf("removable media\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000712
713 /* Info from the specific configuration word says whether or not the
714 * ID command completed correctly. It is only defined, however in
Glenn L McGrath07085852003-10-09 07:28:22 +0000715 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
Eric Andersen3443bd72003-07-22 07:30:36 +0000716 * 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) ||
Glenn L McGrath07085852003-10-09 07:28:22 +0000721 (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");
Eric Andersen3443bd72003-07-22 07:30:36 +0000728 }
729
730 /* output the model and serial numbers and the fw revision */
Glenn L McGrath07085852003-10-09 07:28:22 +0000731 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);
Eric Andersen3443bd72003-07-22 07:30:36 +0000736
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:");
Glenn L McGrath07085852003-10-09 07:28:22 +0000740 if(eqpt != CDROM)
741 {
742 if(val[MINOR] && (val[MINOR] <= MINOR_MAX))
743 {
744 check_if_min_and_set_val( like_std, 3);
Eric Andersen3443bd72003-07-22 07:30:36 +0000745 std = actual_ver[val[MINOR]];
Glenn L McGrath07085852003-10-09 07:28:22 +0000746 if_printf(std,"\n\tUsed: %s ",minor_str[val[MINOR]]);
747
Eric Andersen3443bd72003-07-22 07:30:36 +0000748 }
749 /* looks like when they up-issue the std, they obsolete one;
Glenn L McGrath07085852003-10-09 07:28:22 +0000750 * thus, only the newest 4 issues need be supported. (That's
Eric Andersen3443bd72003-07-22 07:30:36 +0000751 * what "kk" and "min_std" are all about.) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000752 if(val[MAJOR] && (val[MAJOR] !=NOVAL_1))
753 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000754 printf("\n\tSupported: ");
755 jj = val[MAJOR] << 1;
756 kk = like_std >4 ? like_std-4: 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000757 for(ii = 14; (ii >0)&&(ii>kk); ii--)
758 {
759 if(jj & 0x8000)
760 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000761 printf("%u ", ii);
Glenn L McGrath07085852003-10-09 07:28:22 +0000762 if(like_std < ii)
763 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000764 like_std = ii;
765 kk = like_std >4 ? like_std-4: 0;
766 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000767 check_if_maj_and_set_val(min_std, ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000768 }
769 jj <<= 1;
770 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000771 check_if_min_and_set_val( like_std, 3);
Eric Andersen3443bd72003-07-22 07:30:36 +0000772 }
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 */
Glenn L McGrath07085852003-10-09 07:28:22 +0000777 check_if_min_and_set_val( like_std, std);
778
Eric Andersen3443bd72003-07-22 07:30:36 +0000779 if(((std == 5) || (!std && (like_std < 6))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000780 ((((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 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000785 like_std = 6;
Glenn L McGrath07085852003-10-09 07:28:22 +0000786 }
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 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000793 like_std = 5;
Glenn L McGrath07085852003-10-09 07:28:22 +0000794 }
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 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000803 like_std = 4;
Glenn L McGrath07085852003-10-09 07:28:22 +0000804 }
805 else if(((std == 2) || (!std && (like_std < 3))) &&
806 ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
807 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000808 like_std = 3;
Glenn L McGrath07085852003-10-09 07:28:22 +0000809 }
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 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000814 like_std = 2;
815 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000816 if(!std)
Eric Andersen3443bd72003-07-22 07:30:36 +0000817 printf("\n\tLikely used: %u\n",like_std);
Glenn L McGrath07085852003-10-09 07:28:22 +0000818 else if(like_std > std)
819 printf("& some of %u\n",like_std);
820 else
821 printf("\n");
822 }
823 else
824 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000825 /* TBD: do CDROM stuff more thoroughly. For now... */
826 kk = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000827 if(val[CDR_MINOR] == 9)
828 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000829 kk = 1;
830 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
831 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000832 if(val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
833 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000834 kk = 1;
835 printf("\n\tSupported: CD-ROM ATAPI");
836 jj = val[CDR_MAJOR] >> 1;
Glenn L McGrath07085852003-10-09 07:28:22 +0000837 for(ii = 1; ii <15; ii++)
838 {
839 if_printf((jj & 0x0001),"-%u ", ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000840 jj >>= 1;
841 }
842 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000843 if_else_printf((!kk),"\n\tLikely used CD-ROM ATAPI-1\n","\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000844 /* the cdrom stuff is more like ATA-2 than anything else, so: */
845 like_std = 2;
846 }
847
Glenn L McGrath07085852003-10-09 07:28:22 +0000848 if(min_std == 0xffff)
849 min_std = like_std > 4 ? like_std - 3 : 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000850
851 printf("Configuration:\n");
852 /* more info from the general configuration word */
Glenn L McGrath07085852003-10-09 07:28:22 +0000853 if((eqpt != CDROM) && (like_std == 1))
854 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000855 jj = val[GEN_CONFIG] >> 1;
Glenn L McGrath07085852003-10-09 07:28:22 +0000856 for(ii = 1; ii < 15; ii++)
857 {
858 if_printf((jj & 0x0001),"\t%s\n",ata1_cfg_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000859 jj >>=1;
860 }
861 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000862 if(dev == ATAPI_DEV)
863 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000864 printf("\tDRQ response: "); /* Data Request (DRQ) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000865 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;
Eric Andersen3443bd72003-07-22 07:30:36 +0000879 }
880 printf("\tPacket size: ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000881 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;
Eric Andersen3443bd72003-07-22 07:30:36 +0000892 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000893 }
894 else
895 {
896 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000897 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
898 mm = 0; bbbig = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000899 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
Eric Andersen3443bd72003-07-22 07:30:36 +0000900 printf("\tCHS addressing not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000901 else
902 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000903 jj = val[WHATS_VALID] & OK_W54_58;
Glenn L McGrath07085852003-10-09 07:28:22 +0000904 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 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000912 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
Glenn L McGrath07085852003-10-09 07:28:22 +0000913 if(like_std < 3)
914 {
915 /* check Endian of capacity bytes */
Eric Andersen3443bd72003-07-22 07:30:36 +0000916 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);
Glenn L McGrath07085852003-10-09 07:28:22 +0000922 }
Eric Andersen3443bd72003-07-22 07:30:36 +0000923 }
924 /* LBA addressing */
925 printf("\tLBA user addressable sectors:%11u\n",ll);
926 if( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000927 (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] ;
Eric Andersen3443bd72003-07-22 07:30:36 +0000933 printf("\tLBA48 user addressable sectors:%11llu\n",bbbig);
934 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000935
936 if (!bbbig)
937 bbbig = (__u64)(ll>mm ? ll : mm); /* # 512 byte blocks */
Eric Andersen3443bd72003-07-22 07:30:36 +0000938 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);
Glenn L McGrath07085852003-10-09 07:28:22 +0000941
942 if_else_printf((bbbig > 1000),"(%llu GB)\n","\n",bbbig/1000);
Eric Andersen3443bd72003-07-22 07:30:36 +0000943
944 }
945
946 /* hw support of commands (capabilities) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000947 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, ");
Eric Andersen3443bd72003-07-22 07:30:36 +0000955 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000956 if_printf((val[CAPAB_0] & LBA_SUP),"LBA, ");
957
958 if(like_std != 1)
959 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000960 printf("IORDY");
Glenn L McGrath07085852003-10-09 07:28:22 +0000961 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 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000970 kk = val[BUF_TYPE];
971 printf("\tBuffer type: %04x: ",kk);
Glenn L McGrath07085852003-10-09 07:28:22 +0000972 if_else_printf((kk < 2),"single port, single-sector","dual port, multi-sector");
973 if_printf((kk > 2)," with read caching ability");
Eric Andersen3443bd72003-07-22 07:30:36 +0000974 printf("\n");
975 }
976 jj = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000977 if((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
978 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000979 printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
980 jj = 1;
981 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000982 if((min_std < 4) && (val[RW_LONG]))
983 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000984 printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
985 jj = 1;
986 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000987 if((eqpt != CDROM) && (like_std > 3))
988 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000989 printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
990 jj = 1;
991 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000992 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 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001000 printf("\tStandby timer values: spec'd by ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001001 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");
Eric Andersen3443bd72003-07-22 07:30:36 +00001006 }
1007 printf("\tR/W multiple sector transfer: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001008 if((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
Eric Andersen3443bd72003-07-22 07:30:36 +00001009 printf("not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001010 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);
Eric Andersen3443bd72003-07-22 07:30:36 +00001015 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001016 if((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
1017 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001018 /* 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. */
Glenn L McGrath07085852003-10-09 07:28:22 +00001021 printf("\tAdvancedPM level: ");
1022 if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
1023 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001024 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
Eric Andersen3443bd72003-07-22 07:30:36 +00001025 printf("%u (0x%x)\n", apm_level, apm_level);
Glenn L McGrath07085852003-10-09 07:28:22 +00001026 }
1027 else
Eric Andersen3443bd72003-07-22 07:30:36 +00001028 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001029 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001030 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);
Eric Andersen3443bd72003-07-22 07:30:36 +00001034 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001035 }
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 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001044 printf("\tOverlap support:");
Glenn L McGrath07085852003-10-09 07:28:22 +00001045 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]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001047 printf("\n");
1048 }
1049 }
1050
1051 /* DMA stuff. Check that only one DMA mode is selected. */
1052 printf("\tDMA: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001053 if(!(val[CAPAB_0] & DMA_SUP))
Eric Andersen3443bd72003-07-22 07:30:36 +00001054 printf("not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001055 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;
Eric Andersen3443bd72003-07-22 07:30:36 +00001063 err_dma += mode_loop(jj,kk,'s',&have_mode);
1064 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001065 if(val[MULTI_DMA])
1066 {
1067 jj = val[MULTI_DMA];
1068 kk = val[MULTI_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +00001069 err_dma += mode_loop(jj,kk,'m',&have_mode);
1070 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001071 if((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
1072 {
1073 jj = val[ULTRA_DMA];
1074 kk = val[ULTRA_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +00001075 err_dma += mode_loop(jj,kk,'u',&have_mode);
1076 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001077 if_printf((err_dma || !have_mode),"(?)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001078 printf("\n");
1079
Glenn L McGrath07085852003-10-09 07:28:22 +00001080 if_printf(((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)),
1081 "\t Interleaved DMA support\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001082
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001083 if((val[WHATS_VALID] & OK_W64_70) &&
Glenn L McGrath07085852003-10-09 07:28:22 +00001084 (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
1085 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001086 printf("\t Cycle time:");
Glenn L McGrath07085852003-10-09 07:28:22 +00001087 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]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001089 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. */
Glenn L McGrath07085852003-10-09 07:28:22 +00001097 if((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
1098 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001099 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
Glenn L McGrath07085852003-10-09 07:28:22 +00001100 for(ii = 0; ii <= PIO_MODE_MAX ; ii++)
1101 {
1102 if_printf((jj & 0x0001),"pio%d ",ii);
Eric Andersen3443bd72003-07-22 07:30:36 +00001103 jj >>=1;
1104 }
1105 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001106 }
1107 else if(((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
1108 {
1109 for(ii = 0; ii <= val[PIO_MODE]>>8; ii++)
Eric Andersen3443bd72003-07-22 07:30:36 +00001110 printf("pio%d ",ii);
Eric Andersen3443bd72003-07-22 07:30:36 +00001111 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001112 }
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 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001120 printf("\t Cycle time:");
Glenn L McGrath07085852003-10-09 07:28:22 +00001121 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]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001123 printf("\n");
1124 }
1125 }
1126
Glenn L McGrath07085852003-10-09 07:28:22 +00001127 if((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
1128 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001129 printf("Commands/features:\n\tEnabled\tSupported:\n");
1130 jj = val[CMDS_SUPP_0];
1131 kk = val[CMDS_EN_0];
Glenn L McGrath07085852003-10-09 07:28:22 +00001132 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");
Eric Andersen3443bd72003-07-22 07:30:36 +00001137 printf("\t%s\n",cmd_feat_str[ii]);
1138 }
1139 jj <<=1; kk<<=1;
Glenn L McGrath07085852003-10-09 07:28:22 +00001140 if(ii%16 == 15)
1141 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001142 jj = val[CMDS_SUPP_0+1+(ii/16)];
1143 kk = val[CMDS_EN_0+1+(ii/16)];
1144 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001145 if(ii == 31)
1146 {
1147 if((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
1148 ii +=16;
Eric Andersen3443bd72003-07-22 07:30:36 +00001149 }
1150 }
1151 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001152 if_printf(((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP),
1153 "\tRemovable Media Status Notification feature set supported\n");
1154
1155
Eric Andersen3443bd72003-07-22 07:30:36 +00001156 /* security */
Glenn L McGrath07085852003-10-09 07:28:22 +00001157 if((eqpt != CDROM) && (like_std > 3) &&
1158 (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1159 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001160 printf("Security: \n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001161 if_printf((val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1)),
1162 "\tMaster password revision code = %u\n",val[PSWD_CODE]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001163 jj = val[SECU_STATUS];
Glenn L McGrath07085852003-10-09 07:28:22 +00001164 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]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001169 jj >>=1;
1170 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001171 if(val[SECU_STATUS] & SECU_ENABLED)
1172 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001173 printf("\tSecurity level ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001174 if_else_printf((val[SECU_STATUS] & SECU_LEVEL),"maximum\n","high\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001175 }
1176 }
1177 jj = val[ERASE_TIME] & ERASE_BITS;
1178 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
Glenn L McGrath07085852003-10-09 07:28:22 +00001179 if(jj || kk)
1180 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001181 printf("\t");
Glenn L McGrath07085852003-10-09 07:28:22 +00001182 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);
Eric Andersen3443bd72003-07-22 07:30:36 +00001184 printf("\n");
1185 }
1186 }
1187
1188 /* reset result */
Glenn L McGrath07085852003-10-09 07:28:22 +00001189 if((val[HWRST_RSLT] & VALID) == VALID_VAL)
1190 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001191 printf("HW reset results:\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001192 if_else_printf((val[HWRST_RSLT] & CBLID),"\tCBLID- above Vih\n","\tCBLID- below Vih\n");
1193
1194 if(val[HWRST_RSLT] & RST0)
1195 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001196 printf("\tDevice num = 0");
1197 jj = val[HWRST_RSLT];
Glenn L McGrath07085852003-10-09 07:28:22 +00001198 }
1199 else
1200 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001201 printf("\tDevice num = 1");
1202 jj = val[HWRST_RSLT] >> 8;
1203 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001204
1205 if((jj & DEV_DET) == JUMPER_VAL)
Eric Andersen3443bd72003-07-22 07:30:36 +00001206 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 */
Glenn L McGrath07085852003-10-09 07:28:22 +00001213 if((like_std > 4) && (eqpt != CDROM))
1214 {
1215 if(val[CFA_PWR_MODE] & VALID_W160)
1216 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001217 printf("CFA power mode 1:\n\t");
Glenn L McGrath07085852003-10-09 07:28:22 +00001218 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");
Eric Andersen3443bd72003-07-22 07:30:36 +00001221 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001222
1223 if_printf((val[CFA_PWR_MODE] & MAX_AMPS),"\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
Eric Andersen3443bd72003-07-22 07:30:36 +00001224 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001225 if((val[INTEGRITY] & SIG) == SIG_VAL)
1226 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001227 printf("Checksum: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001228 if_printf(chksum,"in");
Eric Andersen3443bd72003-07-22 07:30:36 +00001229 printf("correct\n");
1230 }
1231 }
1232
1233 exit(0);
1234}
1235#endif
1236
Eric Andersen3443bd72003-07-22 07:30:36 +00001237static int verbose = 0, get_identity = 0, get_geom = 0, noisy = 1, quiet = 0;
1238static int flagcount = 0, do_flush = 0, is_scsi_hd = 0, is_xt_hd = 0;
1239static int do_ctimings, do_timings = 0;
1240
Eric Andersen7e8f41c2004-02-14 21:33:39 +00001241static unsigned long set_readahead= 0, get_readahead= 0, bbreadahead= 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001242static unsigned long set_readonly = 0, get_readonly = 0, readonly = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +00001243static unsigned long set_unmask = 0, get_unmask = 0, unmask = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001244static unsigned long set_mult = 0, get_mult = 0, mult = 0;
Eric Andersenf1c56a92003-08-08 22:13:41 +00001245#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Eric Andersen3443bd72003-07-22 07:30:36 +00001246static unsigned long set_dma = 0, get_dma = 0, dma = 0;
Eric Andersenf1c56a92003-08-08 22:13:41 +00001247#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00001248static unsigned long set_dma_q = 0, get_dma_q = 0, dma_q = 0;
1249static unsigned long set_nowerr = 0, get_nowerr = 0, nowerr = 0;
1250static unsigned long set_keep = 0, get_keep = 0, keep = 0;
1251static unsigned long set_io32bit = 0, get_io32bit = 0, io32bit = 0;
1252static unsigned long set_piomode = 0, noisy_piomode= 0;
1253static int piomode = 0;
1254#ifdef HDIO_DRIVE_CMD
1255static unsigned long set_dkeep = 0, get_dkeep = 0, dkeep = 0;
1256static unsigned long set_standby = 0, get_standby = 0, standby_requested= 0;
1257static unsigned long set_xfermode = 0, get_xfermode = 0;
1258static int xfermode_requested= 0;
1259static unsigned long set_lookahead= 0, get_lookahead= 0, lookahead= 0;
1260static unsigned long set_prefetch = 0, get_prefetch = 0, prefetch = 0;
1261static unsigned long set_defects = 0, get_defects = 0, defects = 0;
1262static unsigned long set_wcache = 0, get_wcache = 0, wcache = 0;
1263static unsigned long set_doorlock = 0, get_doorlock = 0, doorlock = 0;
1264static unsigned long set_seagate = 0, get_seagate = 0;
1265static unsigned long set_standbynow = 0, get_standbynow = 0;
1266static unsigned long set_sleepnow = 0, get_sleepnow = 0;
1267static unsigned long get_powermode = 0;
1268static unsigned long set_apmmode = 0, get_apmmode= 0, apmmode = 0;
1269#endif
1270#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
1271static int get_IDentity = 0;
1272#endif
1273#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1274static int unregister_hwif = 0;
1275static int hwif = 0;
1276#endif
1277#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
1278static int scan_hwif = 0;
1279static int hwif_data = 0;
1280static int hwif_ctrl = 0;
1281static int hwif_irq = 0;
1282#endif
1283#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1284static int set_busstate = 0, get_busstate = 0, busstate = 0;
1285#endif
1286static int reread_partn = 0;
1287
1288#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
1289static int perform_reset = 0;
1290#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1291#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1292static 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
1307static 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
1314static const char *BuffType[] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
1315
Eric Andersen3443bd72003-07-22 07:30:36 +00001316static 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
Glenn L McGrath07085852003-10-09 07:28:22 +00001323 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
Eric Andersen3443bd72003-07-22 07:30:36 +00001337 printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
Glenn L McGrath07085852003-10-09 07:28:22 +00001338 if (id->max_multsect)
1339 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001340 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 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001348 printf("\n");
1349 if (id->tPIO <= 5)
1350 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001351 strcat(pmodes, "pio0 ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001352 if_strcat((id->tPIO >= 1), pmodes, "pio1 ");
1353 if_strcat((id->tPIO >= 2), pmodes, "pio2 ");
1354
Eric Andersen3443bd72003-07-22 07:30:36 +00001355 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001356 if_printf((!(id->field_valid&1))," (maybe):");
Eric Andersen3443bd72003-07-22 07:30:36 +00001357#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
Glenn L McGrath07085852003-10-09 07:28:22 +00001362 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");
Eric Andersen3443bd72003-07-22 07:30:36 +00001365
Glenn L McGrath07085852003-10-09 07:28:22 +00001366 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? ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001388 }
1389 }
1390 printf("\n IORDY=");
1391 if (id->capability&8)
1392 printf((id->capability&4) ? "on/off" : "yes");
1393 else
1394 printf("no");
Eric Andersen3443bd72003-07-22 07:30:36 +00001395
Glenn L McGrath07085852003-10-09 07:28:22 +00001396 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 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001413#ifdef __NEW_HD_DRIVE_ID
Glenn L McGrath07085852003-10-09 07:28:22 +00001414 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 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001430 }
1431 }
1432 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001433 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 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001461#endif /* __NEW_HD_DRIVE_ID */
Glenn L McGrath07085852003-10-09 07:28:22 +00001462 printf("\n\n * signifies the current active mode\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001463}
1464#endif
1465
1466static void flush_buffer_cache (int fd)
1467{
1468 fsync (fd); /* flush buffers */
Glenn L McGrath07085852003-10-09 07:28:22 +00001469 bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
Eric Andersen3443bd72003-07-22 07:30:36 +00001470#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001471 sleep(1);
1472 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
1473 bb_error_msg("HDIO_DRIVE_CMD");
Eric Andersen3443bd72003-07-22 07:30:36 +00001474#endif
1475}
1476
1477static int seek_to_zero (int fd)
1478{
Glenn L McGrath07085852003-10-09 07:28:22 +00001479 if (lseek(fd, (off_t) 0, SEEK_SET))
Eric Andersen3443bd72003-07-22 07:30:36 +00001480 return 1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001481 return 0;
1482}
1483
1484static int read_big_block (int fd, char *buf)
1485{
Glenn L McGrath07085852003-10-09 07:28:22 +00001486
1487 const char *string;
Eric Andersen3443bd72003-07-22 07:30:36 +00001488 int i, rc;
Glenn L McGrath07085852003-10-09 07:28:22 +00001489 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";
Eric Andersen3443bd72003-07-22 07:30:36 +00001501 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001502 bb_error_msg(string, TIMING_BUF_BYTES, rc);
Eric Andersen3443bd72003-07-22 07:30:36 +00001503 return 1;
1504 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001505
Eric Andersen3443bd72003-07-22 07:30:36 +00001506 /* 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
Eric Andersen50af12d2003-08-06 08:47:59 +00001512static double correction = 0.0;
1513
Glenn L McGrath07085852003-10-09 07:28:22 +00001514void do_time (int flag, int fd)
1515/*
1516 flag = 0 time_cache
1517 flag = 1 time_device
1518*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001519{
Eric Andersen50af12d2003-08-06 08:47:59 +00001520 int i;
Eric Andersen3443bd72003-07-22 07:30:36 +00001521 char *buf;
1522 double elapsed;
1523 struct itimerval e1, e2;
1524 int shmid;
Eric Andersen50af12d2003-08-06 08:47:59 +00001525 int timing_MB = TIMING_MB;
Eric Andersen3443bd72003-07-22 07:30:36 +00001526
Glenn L McGrath07085852003-10-09 07:28:22 +00001527 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"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001530 return;
1531 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001532 if (shmctl(shmid, SHM_LOCK, NULL) == -1)
1533 {
1534 bb_error_msg (bb_msg_shared_mem,"lock"); /*"could not lock sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001535 (void) shmctl(shmid, IPC_RMID, NULL);
1536 return;
1537 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001538 if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1)
1539 {
1540 bb_error_msg (bb_msg_shared_mem,"attach"); /*"could not attach sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001541 (void) shmctl(shmid, IPC_RMID, NULL);
1542 return;
1543 }
1544 if (shmctl(shmid, IPC_RMID, NULL) == -1)
Glenn L McGrath07085852003-10-09 07:28:22 +00001545 bb_error_msg ("shmctl(,IPC_RMID,)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001546
1547 /* Clear out the device request queues & give them time to complete */
Glenn L McGrath07085852003-10-09 07:28:22 +00001548 sync_and_sleep(3);
Eric Andersen3443bd72003-07-22 07:30:36 +00001549
Glenn L McGrath07085852003-10-09 07:28:22 +00001550 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);
Eric Andersen3443bd72003-07-22 07:30:36 +00001568
Glenn L McGrath07085852003-10-09 07:28:22 +00001569 /* Clear out the device request queues & give them time to complete */
1570 sync_and_sleep(1);
Eric Andersen3443bd72003-07-22 07:30:36 +00001571
Glenn L McGrath07085852003-10-09 07:28:22 +00001572 /* 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);
Eric Andersen3443bd72003-07-22 07:30:36 +00001583
Glenn L McGrath07085852003-10-09 07:28:22 +00001584 /* 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);
Eric Andersen50af12d2003-08-06 08:47:59 +00001594
Glenn L McGrath07085852003-10-09 07:28:22 +00001595 if ((BUFCACHE_FACTOR * timing_MB) >= correction) /* more than 1MB/s */
Eric Andersen50af12d2003-08-06 08:47:59 +00001596 printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
Glenn L McGrath07085852003-10-09 07:28:22 +00001597 (BUFCACHE_FACTOR * timing_MB), correction,
1598 (BUFCACHE_FACTOR * timing_MB) / correction);
Eric Andersen50af12d2003-08-06 08:47:59 +00001599 else
1600 printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
Glenn L McGrath07085852003-10-09 07:28:22 +00001601 (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);
Eric Andersen50af12d2003-08-06 08:47:59 +00001607 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001608 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 }
Eric Andersen50af12d2003-08-06 08:47:59 +00001650#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001651 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001652quit:
1653 if (-1 == shmdt(buf))
Glenn L McGrath07085852003-10-09 07:28:22 +00001654 bb_error_msg (bb_msg_shared_mem,"detach"); /*"could not detach sharedmem buf"*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001655}
1656
Glenn L McGrath07085852003-10-09 07:28:22 +00001657
Eric Andersen3443bd72003-07-22 07:30:36 +00001658static void no_scsi (void)
1659{
Glenn L McGrath07085852003-10-09 07:28:22 +00001660 /*" operation not supported on SCSI disks"*/
1661 if (is_scsi_hd)
1662 bb_error_msg_and_die(bb_msg_op_not_supp,"SCSI");
Eric Andersen3443bd72003-07-22 07:30:36 +00001663}
1664
1665static void no_xt (void)
1666{
Glenn L McGrath07085852003-10-09 07:28:22 +00001667 if (is_xt_hd)
1668 bb_error_msg_and_die(bb_msg_op_not_supp,"XT");
Eric Andersen3443bd72003-07-22 07:30:36 +00001669}
1670
1671static void on_off (unsigned int value)
1672{
1673 printf(value ? " (on)\n" : " (off)\n");
1674}
1675
1676#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1677static void bus_state_value (unsigned int value)
1678{
1679 const char *string;
1680
Glenn L McGrath07085852003-10-09 07:28:22 +00001681 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;
Eric Andersen3443bd72003-07-22 07:30:36 +00001695 }
1696 printf(string, value);
1697}
1698#endif
1699
1700#ifdef HDIO_DRIVE_CMD
1701static void interpret_standby (unsigned int standby)
1702{
1703 printf(" (");
Glenn L McGrath07085852003-10-09 07:28:22 +00001704 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;
Eric Andersen3443bd72003-07-22 07:30:36 +00001721 default:
Glenn L McGrath07085852003-10-09 07:28:22 +00001722 if (standby <= 240)
1723 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001724 unsigned int secs = standby * 5;
1725 unsigned int mins = secs / 60;
1726 secs %= 60;
Glenn L McGrath07085852003-10-09 07:28:22 +00001727 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 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001733 unsigned int mins = (standby - 240) * 30;
1734 unsigned int hrs = mins / 60;
1735 mins %= 60;
Glenn L McGrath07085852003-10-09 07:28:22 +00001736 if_printf(hrs,"%u hours", hrs);
1737 if_printf((hrs && mins)," + ");
1738 if_printf(mins,"%u minutes", mins);
1739 }
1740 else
Eric Andersen3443bd72003-07-22 07:30:36 +00001741 printf("illegal value");
1742 break;
1743 }
1744 printf(")\n");
1745}
1746
1747struct xfermode_entry {
1748 int val;
1749 const char *name;
1750};
1751
1752static 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
1788static int translate_xfermode(char * name)
1789{
Glenn L McGrath07085852003-10-09 07:28:22 +00001790 const struct xfermode_entry *tmp;
1791 char *endptr;
1792 int val = -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001793
1794
Glenn L McGrath07085852003-10-09 07:28:22 +00001795 for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1796 {
1797 if (!strcmp(name, tmp->name))
1798 return tmp->val;
Eric Andersen3443bd72003-07-22 07:30:36 +00001799
Glenn L McGrath07085852003-10-09 07:28:22 +00001800 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001801
Glenn L McGrath07085852003-10-09 07:28:22 +00001802 val = strtol(name, &endptr, 10);
1803 if (*endptr == '\0')
1804 return val;
1805
1806 return -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001807}
1808
1809static void interpret_xfermode (unsigned int xfermode)
1810{
1811 printf(" (");
1812 switch(xfermode) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001813 case 0:
1814 printf("default PIO mode");
1815 break;
1816 case 1:
1817 printf("default PIO mode, disable IORDY");
1818 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001819 case 8:
1820 case 9:
1821 case 10:
1822 case 11:
1823 case 12:
1824 case 13:
1825 case 14:
Glenn L McGrath07085852003-10-09 07:28:22 +00001826 case 15:
1827 printf("PIO flow control mode%u", xfermode-8);
1828 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001829 case 16:
1830 case 17:
1831 case 18:
1832 case 19:
1833 case 20:
1834 case 21:
1835 case 22:
Glenn L McGrath07085852003-10-09 07:28:22 +00001836 case 23:
1837 printf("singleword DMA mode%u", xfermode-16);
1838 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001839 case 32:
1840 case 33:
1841 case 34:
1842 case 35:
1843 case 36:
1844 case 37:
1845 case 38:
Glenn L McGrath07085852003-10-09 07:28:22 +00001846 case 39:
1847 printf("multiword DMA mode%u", xfermode-32);
1848 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001849 case 64:
1850 case 65:
1851 case 66:
1852 case 67:
1853 case 68:
1854 case 69:
1855 case 70:
Glenn L McGrath07085852003-10-09 07:28:22 +00001856 case 71:
1857 printf("UltraDMA mode%u", xfermode-64);
1858 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001859 default:
Glenn L McGrath07085852003-10-09 07:28:22 +00001860 printf("unknown, probably not valid");
1861 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001862 }
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
1875static 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
Glenn L McGrath07085852003-10-09 07:28:22 +00001886 switch(major(stat_buf.st_rdev))
1887 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001888#ifdef SCSI_DISK0_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001889 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):
Eric Andersen3443bd72003-07-22 07:30:36 +00001897#else
Glenn L McGrath07085852003-10-09 07:28:22 +00001898 case (SCSI_DISK_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001899#endif
1900#ifdef MD_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001901 case (MD_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001902#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001903 case (VXVM_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001904#ifdef LVM_BLK_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001905 case (LVM_BLK_MAJOR):
Eric Andersen3443bd72003-07-22 07:30:36 +00001906#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001907 case (CCISS_MAJOR):
1908 is_scsi_hd = 1;
1909 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001910#ifdef XT_DISK_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001911 case (XT_DISK_MAJOR):
1912 is_xt_hd = 1;
1913 break;
Eric Andersen3443bd72003-07-22 07:30:36 +00001914#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001915 case IDE0_MAJOR:
1916 case IDE1_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001917#ifdef IDE2_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001918 case IDE2_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001919#endif
1920#ifdef IDE3_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001921 case IDE3_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001922#endif
1923#ifdef IDE4_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001924 case IDE4_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001925#endif
1926#ifdef IDE5_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001927 case IDE5_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001928#endif
1929#ifdef IDE6_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001930 case IDE6_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001931#endif
1932#ifdef IDE7_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001933 case IDE7_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001934#endif
1935#ifdef IDE8_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001936 case IDE8_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001937#endif
1938#ifdef IDE9_MAJOR
Glenn L McGrath07085852003-10-09 07:28:22 +00001939 case IDE9_MAJOR:
Eric Andersen3443bd72003-07-22 07:30:36 +00001940#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001941 break; /* do nothing */
1942 default:
1943 bb_error_msg_and_die("%s not supported",devname);
Eric Andersen3443bd72003-07-22 07:30:36 +00001944 }
1945
Glenn L McGrath07085852003-10-09 07:28:22 +00001946 fd = bb_xopen (devname, O_RDONLY|O_NONBLOCK);
1947 if_printf( (!quiet),"\n%s:\n", devname);
Eric Andersen3443bd72003-07-22 07:30:36 +00001948
Glenn L McGrath07085852003-10-09 07:28:22 +00001949 if (set_readahead)
1950 {
Eric Andersen7e8f41c2004-02-14 21:33:39 +00001951 if_printf(get_readahead," setting fs readahead to %ld\n", bbreadahead);
1952 bb_ioctl(fd, BLKRASET,(int *)bbreadahead,"BLKRASET");
Eric Andersen3443bd72003-07-22 07:30:36 +00001953 }
1954#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001955 if (unregister_hwif)
1956 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001957 no_scsi();
1958 printf(" attempting to unregister hwif#%u\n", hwif);
Glenn L McGrath07085852003-10-09 07:28:22 +00001959 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)hwif,"HDIO_UNREGISTER_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00001960 }
1961#endif
1962#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001963 if (scan_hwif)
1964 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001965 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;
Glenn L McGrath07085852003-10-09 07:28:22 +00001971 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00001972 }
1973#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001974 if (set_piomode)
1975 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001976 no_scsi();
1977 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00001978
1979 if (noisy_piomode)
1980 {
1981 printf(" attempting to ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001982 if (piomode == 255)
Glenn L McGrath07085852003-10-09 07:28:22 +00001983 printf("auto-tune PIO mode\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001984 else if (piomode < 100)
Glenn L McGrath07085852003-10-09 07:28:22 +00001985 printf("set PIO mode to %d\n", piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001986 else if (piomode < 200)
Glenn L McGrath07085852003-10-09 07:28:22 +00001987 printf("set MDMA mode to %d\n", (piomode-100));
Eric Andersen3443bd72003-07-22 07:30:36 +00001988 else
Glenn L McGrath07085852003-10-09 07:28:22 +00001989 printf("set UDMA mode to %d\n", (piomode-200));
Eric Andersen3443bd72003-07-22 07:30:36 +00001990 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001991 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)piomode, "HDIO_SET_PIO_MODE");
Eric Andersen3443bd72003-07-22 07:30:36 +00001992 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001993 if (set_io32bit)
1994 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001995 no_scsi();
1996 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00001997 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");
Eric Andersen3443bd72003-07-22 07:30:36 +00001999 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002000 if (set_mult)
2001 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002002 no_scsi();
2003 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002004 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");
Eric Andersen3443bd72003-07-22 07:30:36 +00002007#ifndef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00002008 else
2009 force_operation = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +00002010#endif
2011 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002012 if (set_readonly)
2013 {
2014 if_printf_on_off(get_readonly," setting readonly to %ld", readonly);
2015 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
Eric Andersen3443bd72003-07-22 07:30:36 +00002016 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002017 if (set_unmask)
2018 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002019 no_scsi();
2020 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002021 if_printf_on_off(get_unmask," setting unmaskirq to %ld", unmask);
2022 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
Eric Andersen3443bd72003-07-22 07:30:36 +00002023 }
Eric Andersen0a57a792003-08-06 08:57:35 +00002024#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Glenn L McGrath07085852003-10-09 07:28:22 +00002025 if (set_dma)
2026 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002027 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002028 if_printf_on_off(get_dma," setting using_dma to %ld", dma);
2029 bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
Eric Andersen3443bd72003-07-22 07:30:36 +00002030 }
Eric Andersen0a57a792003-08-06 08:57:35 +00002031#endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
Glenn L McGrath07085852003-10-09 07:28:22 +00002032 if (set_dma_q)
2033 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002034 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002035 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");
Eric Andersen3443bd72003-07-22 07:30:36 +00002037 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002038 if (set_nowerr)
2039 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002040 no_scsi();
2041 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002042 if_printf_on_off(get_nowerr," setting nowerr to %ld", nowerr);
2043 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
Eric Andersen3443bd72003-07-22 07:30:36 +00002044 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002045 if (set_keep)
2046 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002047 no_scsi();
2048 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002049 if_printf_on_off(get_keep," setting keep_settings to %ld", keep);
2050 bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
Eric Andersen3443bd72003-07-22 07:30:36 +00002051 }
2052#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00002053 if (set_doorlock)
2054 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002055 unsigned char args[4] = {0,0,0,0};
2056 no_scsi();
2057 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002058
Eric Andersen3443bd72003-07-22 07:30:36 +00002059 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
Glenn L McGrath07085852003-10-09 07:28:22 +00002060 if_printf_on_off(get_doorlock," setting drive doorlock to %ld", doorlock);
2061 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002062 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002063 if (set_dkeep)
2064 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002065 /* lock/unlock the drive's "feature" settings */
2066 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2067 no_scsi();
2068 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002069
2070 if_printf_on_off(get_dkeep," setting drive keep features to %ld", dkeep);
Eric Andersen3443bd72003-07-22 07:30:36 +00002071 args[2] = dkeep ? 0x66 : 0xcc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002072 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002073 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002074 if (set_defects)
2075 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002076 unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
2077 no_scsi();
2078 args[2] = defects ? 0x04 : 0x84;
Glenn L McGrath07085852003-10-09 07:28:22 +00002079 if_printf(get_defects," setting drive defect-mgmt to %ld\n", defects);
2080 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002081 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002082 if (set_prefetch)
2083 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002084 unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
2085 no_scsi();
2086 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002087
Eric Andersen3443bd72003-07-22 07:30:36 +00002088 args[1] = prefetch;
Glenn L McGrath07085852003-10-09 07:28:22 +00002089 if_printf(get_prefetch," setting drive prefetch to %ld\n", prefetch);
2090 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002091 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002092 if (set_xfermode)
2093 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002094 unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
2095 no_scsi();
2096 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002097
Eric Andersen3443bd72003-07-22 07:30:36 +00002098 args[1] = xfermode_requested;
Glenn L McGrath07085852003-10-09 07:28:22 +00002099 if (get_xfermode)
2100 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002101 printf(" setting xfermode to %d", xfermode_requested);
2102 interpret_xfermode(xfermode_requested);
2103 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002104 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002105 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002106 if (set_lookahead)
2107 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002108 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2109 no_scsi();
2110 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002111
Eric Andersen3443bd72003-07-22 07:30:36 +00002112 args[2] = lookahead ? 0xaa : 0x55;
Glenn L McGrath07085852003-10-09 07:28:22 +00002113 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)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002115 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002116 if (set_apmmode)
2117 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002118 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
2119 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002120 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 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002125 /* disable Advanced Power Management */
Glenn L McGrath07085852003-10-09 07:28:22 +00002126 args[2] = 0x85; /* feature register */
2127 if_printf(get_apmmode," disabled\n");
2128 }
2129 else
2130 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002131 /* set Advanced Power Management mode */
Glenn L McGrath07085852003-10-09 07:28:22 +00002132 args[2] = 0x05; /* feature register */
Eric Andersen3443bd72003-07-22 07:30:36 +00002133 args[1] = apmmode; /* sector count register */
Glenn L McGrath07085852003-10-09 07:28:22 +00002134 if_printf(get_apmmode," 0x%02lX (%ld)\n",apmmode,apmmode);
2135 }
2136 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
Eric Andersen3443bd72003-07-22 07:30:36 +00002137 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002138 if (set_wcache)
2139 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002140#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;
Glenn L McGrath07085852003-10-09 07:28:22 +00002150 if_printf_on_off(get_wcache," setting drive write-caching to %ld", wcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00002151#ifdef DO_FLUSHCACHE
2152 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
Glenn L McGrath07085852003-10-09 07:28:22 +00002153 bb_error_msg ("HDIO_DRIVE_CMD(flushcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002154#endif /* DO_FLUSHCACHE */
Glenn L McGrath07085852003-10-09 07:28:22 +00002155 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002156#ifdef DO_FLUSHCACHE
2157 if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
Glenn L McGrath07085852003-10-09 07:28:22 +00002158 bb_error_msg ("HDIO_DRIVE_CMD(flushcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002159#endif /* DO_FLUSHCACHE */
2160 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002161 if (set_standbynow)
2162 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002163#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();
Glenn L McGrath07085852003-10-09 07:28:22 +00002172 if_printf(get_standbynow," issuing standby command\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002173 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2174 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
Glenn L McGrath07085852003-10-09 07:28:22 +00002175 bb_error_msg("HDIO_DRIVE_CMD(standby)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002176 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002177 if (set_sleepnow)
2178 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002179#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();
Glenn L McGrath07085852003-10-09 07:28:22 +00002188 if_printf(get_sleepnow," issuing sleep command\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002189 if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
2190 && ioctl(fd, HDIO_DRIVE_CMD, &args2))
Glenn L McGrath07085852003-10-09 07:28:22 +00002191 bb_error_msg("HDIO_DRIVE_CMD(sleep)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002192 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002193 if (set_seagate)
2194 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002195 unsigned char args[4] = {0xfb,0,0,0};
2196 no_scsi();
2197 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002198 if_printf(get_seagate," disabling Seagate auto powersaving mode\n");
2199 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002200 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002201 if (set_standby)
2202 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002203 unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
2204 no_scsi();
2205 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002206 if (get_standby)
2207 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002208 printf(" setting standby to %lu", standby_requested);
2209 interpret_standby(standby_requested);
2210 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002211 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002212 }
2213#else /* HDIO_DRIVE_CMD */
Glenn L McGrath07085852003-10-09 07:28:22 +00002214 if (force_operation)
2215 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002216 char buf[512];
2217 flush_buffer_cache(fd);
2218 if (-1 == read(fd, buf, sizeof(buf)))
Glenn L McGrath07085852003-10-09 07:28:22 +00002219 bb_error_msg("access failed");
Eric Andersen3443bd72003-07-22 07:30:36 +00002220 }
2221#endif /* HDIO_DRIVE_CMD */
2222
2223 if (!flagcount)
2224 verbose = 1;
2225
Glenn L McGrath07085852003-10-09 07:28:22 +00002226 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity)
2227 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002228 no_scsi();
2229 multcount = -1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002230 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
2231 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002232 if ((verbose && !is_xt_hd) || get_mult)
Glenn L McGrath07085852003-10-09 07:28:22 +00002233 bb_error_msg("HDIO_GET_MULTCOUNT");
2234 }
2235 else if (verbose | get_mult)
2236 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002237 printf(" multcount = %2ld", multcount);
2238 on_off(multcount);
2239 }
2240 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002241 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit)
2242 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002243 no_scsi();
2244 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002245 if(ioctl(fd, HDIO_GET_32BIT, &parm))
2246 bb_error_msg("HDIO_GET_32BIT");
2247 else
2248 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002249 printf(" IO_support =%3ld (", parm);
Glenn L McGrath07085852003-10-09 07:28:22 +00002250 switch (parm)
2251 {
2252 case 0:
2253 printf("default ");
2254 case 2:
2255 printf("16-bit)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002256 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002257 case 1:
2258 printf("32-bit)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002259 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002260 case 3:
2261 printf("32-bit w/sync)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002262 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002263 case 8:
2264 printf("Request-Queue-Bypass)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00002265 break;
Glenn L McGrath07085852003-10-09 07:28:22 +00002266 default:
2267 printf("\?\?\?)\n");
2268 /*esac*/
Eric Andersen3443bd72003-07-22 07:30:36 +00002269 }
2270 }
2271 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002272 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask)
2273 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002274 no_scsi();
2275 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002276 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
2277 "HDIO_GET_UNMASKINTR"," unmaskirq = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002278 }
2279
Glenn L McGrath07085852003-10-09 07:28:22 +00002280
Eric Andersen0a57a792003-08-06 08:57:35 +00002281#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Eric Andersen3443bd72003-07-22 07:30:36 +00002282 if ((verbose && !is_scsi_hd) || get_dma) {
2283 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002284 if(ioctl(fd, HDIO_GET_DMA, &parm))
2285 bb_error_msg("HDIO_GET_DMA");
2286 else
2287 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002288 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
Glenn L McGrath07085852003-10-09 07:28:22 +00002296 if (get_dma_q)
2297 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002298 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002299 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
2300 "HDIO_GET_QDMA"," queue_depth = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002301 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002302 if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep)
2303 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002304 no_scsi();
2305 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002306 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
2307 "HDIO_GET_KEEPSETTINGS"," keepsettings = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002308 }
2309
Glenn L McGrath07085852003-10-09 07:28:22 +00002310 if (get_nowerr)
2311 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002312 no_scsi();
2313 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002314 bb_ioctl_on_off (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
2315 " HDIO_GET_NOWERR"," nowerr = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002316 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002317 if (verbose || get_readonly)
2318 {
2319 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
2320 " BLKROGET"," readonly = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002321 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002322 if ((verbose && !is_scsi_hd) || get_readahead)
2323 {
2324 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
2325 " BLKRAGET"," readahead = %2ld");
Eric Andersen3443bd72003-07-22 07:30:36 +00002326 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002327 if (verbose || get_geom)
2328 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002329 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))
Glenn L McGrath07085852003-10-09 07:28:22 +00002336 bb_error_msg("BLKGETSIZE");
Eric Andersen3443bd72003-07-22 07:30:36 +00002337#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))
Glenn L McGrath07085852003-10-09 07:28:22 +00002342 bb_error_msg("HDIO_GETGEO");
2343 else
2344 printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
Eric Andersen3443bd72003-07-22 07:30:36 +00002345 }
2346#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00002347 if (get_powermode)
2348 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002349#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)
Glenn L McGrath07085852003-10-09 07:28:22 +00002359 && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
2360 && ioctl(fd, HDIO_DRIVE_CMD, &args))
2361 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002362 if (errno != EIO || args[0] != 0 || args[1] != 0)
2363 state = "unknown";
2364 else
2365 state = "sleeping";
Eric Andersen3443bd72003-07-22 07:30:36 +00002366 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002367 else
2368 state = (args[2] == 255) ? "active/idle" : "standby";
2369
Eric Andersen3443bd72003-07-22 07:30:36 +00002370 printf(" drive state is: %s\n", state);
2371 }
2372#endif
2373#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
Glenn L McGrath07085852003-10-09 07:28:22 +00002374 if (perform_reset)
2375 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002376 no_scsi();
2377 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002378 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
Eric Andersen3443bd72003-07-22 07:30:36 +00002379 }
2380#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
2381#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002382 if (perform_tristate)
2383 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002384 unsigned char args[4] = {0,tristate,0,0};
2385 no_scsi();
2386 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002387 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00002388 }
2389#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2390#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +00002391 if (get_identity)
2392 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002393 static struct hd_driveid id;
2394
2395 no_scsi();
2396 no_xt();
2397
Glenn L McGrath07085852003-10-09 07:28:22 +00002398 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
2399 {
2400 if (multcount != -1)
2401 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002402 id.multsect = multcount;
2403 id.multsect_valid |= 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002404 }
2405 else
Eric Andersen3443bd72003-07-22 07:30:36 +00002406 id.multsect_valid &= ~1;
2407 dump_identity(&id);
Glenn L McGrath07085852003-10-09 07:28:22 +00002408 }
2409 else if (errno == -ENOMSG)
Eric Andersen3443bd72003-07-22 07:30:36 +00002410 printf(" no identification info available\n");
2411 else
Glenn L McGrath07085852003-10-09 07:28:22 +00002412 bb_error_msg("HDIO_GET_IDENTITY");
Eric Andersen3443bd72003-07-22 07:30:36 +00002413 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002414
2415 if (get_IDentity)
2416 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002417 unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
2418 unsigned i;
Glenn L McGrath07085852003-10-09 07:28:22 +00002419
Eric Andersen3443bd72003-07-22 07:30:36 +00002420 no_scsi();
2421 no_xt();
Glenn L McGrath07085852003-10-09 07:28:22 +00002422
2423 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2424 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002425 args[0] = WIN_PIDENTIFY;
Glenn L McGrath07085852003-10-09 07:28:22 +00002426 if (ioctl(fd, HDIO_DRIVE_CMD, &args))
2427 {
2428 bb_error_msg("HDIO_DRIVE_CMD(identify)");
Eric Andersen3443bd72003-07-22 07:30:36 +00002429 goto identify_abort;
2430 }
2431 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002432 for(i=0; i<(sizeof args)/2; i+=2)
Eric Andersen3443bd72003-07-22 07:30:36 +00002433 __le16_to_cpus((uint16_t *)(&args[i]));
Glenn L McGrath07085852003-10-09 07:28:22 +00002434
Eric Andersen3443bd72003-07-22 07:30:36 +00002435 identify((void *)&args[4], NULL);
Glenn L McGrath07085852003-10-09 07:28:22 +00002436identify_abort:
2437 /* VOID */;
Eric Andersen3443bd72003-07-22 07:30:36 +00002438 }
2439#endif
2440#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002441 if (set_busstate)
2442 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002443 no_scsi();
Glenn L McGrath07085852003-10-09 07:28:22 +00002444 if (get_busstate)
2445 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002446 printf(" setting bus state to %d", busstate);
2447 bus_state_value(busstate);
2448 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002449 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)busstate, "HDIO_SET_BUSSTATE");
Eric Andersen3443bd72003-07-22 07:30:36 +00002450 }
2451#endif
2452#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002453 if (get_busstate)
2454 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002455 no_scsi();
2456 if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
Glenn L McGrath07085852003-10-09 07:28:22 +00002457 bb_error_msg("HDIO_GET_BUSSTATE");
2458 else
2459 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002460 printf(" busstate = %2ld", parm);
2461 bus_state_value(parm);
2462 }
2463 }
2464#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00002465 if (reread_partn)
2466 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2467
Eric Andersen3443bd72003-07-22 07:30:36 +00002468
2469 if (do_ctimings)
Glenn L McGrath07085852003-10-09 07:28:22 +00002470 do_time(0,fd); /*time cache */
Eric Andersen3443bd72003-07-22 07:30:36 +00002471 if (do_timings)
Glenn L McGrath07085852003-10-09 07:28:22 +00002472 do_time(1,fd); /*time device */
Eric Andersen3443bd72003-07-22 07:30:36 +00002473 if (do_flush)
2474 flush_buffer_cache (fd);
2475 close (fd);
2476}
2477
Eric Andersen3443bd72003-07-22 07:30:36 +00002478#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
2479static 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
2488static 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);
Glenn L McGrath07085852003-10-09 07:28:22 +00002496 for (i = 0; count >= 4; ++i)
2497 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002498 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: */
2509int hdparm_main(int argc, char **argv)
2510{
Glenn L McGrath07085852003-10-09 07:28:22 +00002511 const char * const bb_msg_missing_value ="missing value";
Eric Andersen3443bd72003-07-22 07:30:36 +00002512 char c, *p;
2513 char *tmpstr;
2514 char name[32];
Glenn L McGrath07085852003-10-09 07:28:22 +00002515 /*int neg;*/
Eric Andersen3443bd72003-07-22 07:30:36 +00002516
2517 ++argv;
2518 if (!--argc)
2519 bb_show_usage();
Glenn L McGrath07085852003-10-09 07:28:22 +00002520
2521 while (argc--)
2522 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002523#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +00002524 if (!strcmp("-Istdin", *argv))
2525 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002526 exit(identify_from_stdin());
2527 }
2528#endif
2529 p = *argv++;
Glenn L McGrath07085852003-10-09 07:28:22 +00002530 if (*p == '-')
2531 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002532 if (!*++p)
2533 bb_show_usage();
Glenn L McGrath07085852003-10-09 07:28:22 +00002534 while ((c = *p++))
2535 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002536 ++flagcount;
Glenn L McGrath07085852003-10-09 07:28:22 +00002537 switch (c)
2538 {
Eric Andersen3443bd72003-07-22 07:30:36 +00002539 case 'V':
Glenn L McGrath07085852003-10-09 07:28:22 +00002540 /*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);
Eric Andersen3443bd72003-07-22 07:30:36 +00002544 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;
Glenn L McGrath07085852003-10-09 07:28:22 +00002568 p = check_ptr(p,argc,argv);
2569 if((set_unmask = set_flag(p,'1'))==1)
2570 unmask = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002571 break;
Eric Andersen0a57a792003-08-06 08:57:35 +00002572#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Eric Andersen3443bd72003-07-22 07:30:36 +00002573 case 'd':
2574 get_dma = noisy;
2575 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002576 p = check_ptr(p,argc,argv);
2577 if((set_dma = set_flag(p,'9'))==1)
Eric Andersen3443bd72003-07-22 07:30:36 +00002578 dma = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002579 break;
Eric Andersen0a57a792003-08-06 08:57:35 +00002580#endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
Eric Andersen3443bd72003-07-22 07:30:36 +00002581 case 'n':
2582 get_nowerr = noisy;
2583 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002584 p = check_ptr(p,argc,argv);
2585 if((set_nowerr = set_flag(p,'1'))==1)
2586 nowerr = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002587 break;
2588 case 'p':
2589 noisy_piomode = noisy;
2590 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002591 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;
Eric Andersen3443bd72003-07-22 07:30:36 +00002605 break;
2606 case 'r':
2607 get_readonly = noisy;
2608 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002609 p = check_ptr(p,argc,argv);
2610 if((set_readonly = set_flag(p,'1'))==1)
2611 readonly = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002612 break;
2613 case 'm':
2614 get_mult = noisy;
2615 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002616 p = check_ptr(p,argc,argv);
2617 while (isdigit(*p))
2618 {
2619 set_mult = 1;
2620 mult = (mult * 10) + (*p++ - '0');
2621 }
Eric Andersen3443bd72003-07-22 07:30:36 +00002622 break;
2623 case 'c':
2624 get_io32bit = noisy;
2625 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002626 p = check_ptr(p,argc,argv);
2627 while (isdigit(*p))
2628 {
2629 set_io32bit = 1;
2630 io32bit = (io32bit * 10) + (*p++ - '0');
2631 }
Eric Andersen3443bd72003-07-22 07:30:36 +00002632 break;
2633#ifdef HDIO_DRIVE_CMD
2634 case 'S':
2635 get_standby = noisy;
2636 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002637 p = check_ptr(p,argc,argv);
2638 while (isdigit(*p))
2639 {
2640 set_standby = 1;
2641 standby_requested = (standby_requested * 10) + (*p++ - '0');
2642 }
Eric Andersen3443bd72003-07-22 07:30:36 +00002643 if (!set_standby)
Glenn L McGrath07085852003-10-09 07:28:22 +00002644 bb_error_msg("-S: %s", bb_msg_missing_value);
Eric Andersen3443bd72003-07-22 07:30:36 +00002645 break;
2646
2647 case 'D':
2648 get_defects = noisy;
2649 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002650 p = check_ptr(p,argc,argv);
2651 while (isdigit(*p))
2652 {
2653 set_defects = 1;
2654 defects = (defects * 10) + (*p++ - '0');
2655 }
Eric Andersen3443bd72003-07-22 07:30:36 +00002656 if (!set_defects)
Glenn L McGrath07085852003-10-09 07:28:22 +00002657 bb_error_msg("-D: %s", bb_msg_missing_value);
Eric Andersen3443bd72003-07-22 07:30:36 +00002658 break;
2659 case 'P':
2660 get_prefetch = noisy;
2661 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002662 p = check_ptr(p,argc,argv);
2663 while (isdigit(*p))
2664 {
2665 set_prefetch = 1;
2666 prefetch = (prefetch * 10) + (*p++ - '0');
2667 }
Eric Andersen3443bd72003-07-22 07:30:36 +00002668 if (!set_prefetch)
Glenn L McGrath07085852003-10-09 07:28:22 +00002669 bb_error_msg("-P: %s", bb_msg_missing_value);
Eric Andersen3443bd72003-07-22 07:30:36 +00002670 break;
2671
2672 case 'X':
2673 get_xfermode = noisy;
2674 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002675 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;
Eric Andersen3443bd72003-07-22 07:30:36 +00002689 if (!set_xfermode)
Glenn L McGrath07085852003-10-09 07:28:22 +00002690 bb_error_msg("-X: %s", bb_msg_missing_value);
Eric Andersen3443bd72003-07-22 07:30:36 +00002691 break;
2692
2693 case 'K':
2694 get_dkeep = noisy;
2695 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002696 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);
Eric Andersen3443bd72003-07-22 07:30:36 +00002701 break;
2702
2703 case 'A':
2704 get_lookahead = noisy;
2705 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002706 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);
Eric Andersen3443bd72003-07-22 07:30:36 +00002711 break;
2712
2713 case 'L':
2714 get_doorlock = noisy;
2715 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002716 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);
Eric Andersen3443bd72003-07-22 07:30:36 +00002721 break;
2722
2723 case 'W':
2724 get_wcache = noisy;
2725 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002726 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);
Eric Andersen3443bd72003-07-22 07:30:36 +00002731 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;
Glenn L McGrath07085852003-10-09 07:28:22 +00002763 p = check_ptr(p,argc,argv);
2764 if((set_keep = set_flag(p,'1'))==1)
2765 keep = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002766 break;
2767#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
2768 case 'U':
Glenn L McGrath07085852003-10-09 07:28:22 +00002769 p = check_ptr(p,argc,argv);
2770 if(! p)
2771 goto error; /* "expected hwif_nr" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002772
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':
Glenn L McGrath07085852003-10-09 07:28:22 +00002780 p = check_ptr(p,argc,argv);
2781 if(! p)
2782 goto error; /* "expected hwif_data" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002783
2784 sscanf(p++, "%i", &hwif_data);
2785
2786 if (argc && isdigit(**argv))
2787 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002788 else
2789 goto error; /* "expected hwif_ctrl" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002790
2791 sscanf(p, "%i", &hwif_ctrl);
2792
2793 if (argc && isdigit(**argv))
2794 p = *argv++, --argc;
Glenn L McGrath07085852003-10-09 07:28:22 +00002795 else
2796error:
2797 bb_error_msg_and_die("expected hwif value"); /* "expected hwif_irq" */
Eric Andersen3443bd72003-07-22 07:30:36 +00002798
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;
Glenn L McGrath07085852003-10-09 07:28:22 +00002809 /* 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;*/
Eric Andersen3443bd72003-07-22 07:30:36 +00002819 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':
Glenn L McGrath07085852003-10-09 07:28:22 +00002827 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);
Eric Andersen3443bd72003-07-22 07:30:36 +00002832 break;
2833
2834#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
2835 case 'a':
2836 get_readahead = noisy;
2837 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002838 p = check_ptr(p,argc,argv);
2839 while (isdigit(*p))
2840 {
2841 set_readahead = 1;
Eric Andersen7e8f41c2004-02-14 21:33:39 +00002842 bbreadahead = (bbreadahead * 10) + (*p++ - '0');
Glenn L McGrath07085852003-10-09 07:28:22 +00002843 }
Eric Andersen3443bd72003-07-22 07:30:36 +00002844 break;
2845 case 'B':
2846 get_apmmode = noisy;
2847 noisy = 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00002848 p = check_ptr(p,argc,argv);
2849 while (isdigit(*p))
2850 {
2851 set_apmmode = 1;
2852 apmmode = (io32bit * 10) + (*p++ - '0');
2853 }
Eric Andersen3443bd72003-07-22 07:30:36 +00002854 if (!set_apmmode)
Glenn L McGrath07085852003-10-09 07:28:22 +00002855 printf("-B: %s (1-255)", bb_msg_missing_value);
Eric Andersen3443bd72003-07-22 07:30:36 +00002856 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;
Glenn L McGrath07085852003-10-09 07:28:22 +00002869 p = check_ptr(p,argc,argv);
2870 if((set_busstate = set_flag(p,'2'))==1)
2871 busstate = *p++ - '0';
Eric Andersen3443bd72003-07-22 07:30:36 +00002872 break;
2873#endif
2874 case 'h':
2875 default:
2876 bb_show_usage();
2877 }
2878 }
2879 if (!argc)
2880 bb_show_usage();
Eric Andersen3443bd72003-07-22 07:30:36 +00002881 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002882 else
2883 process_dev (p);
Eric Andersen3443bd72003-07-22 07:30:36 +00002884 }
2885 return 0 ;
2886}