blob: 0aaff45f2ea908a1ad8952ecfcb6aecc99b40c6d [file] [log] [blame]
Eric Andersen3443bd72003-07-22 07:30:36 +00001/* vi: set sw=4 ts=4: */
2/*
3 * hdparm implementation for busybox
4 *
Glenn L McGrath5be6a202003-11-28 22:55:03 +00005 * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
Glenn L McGrath07085852003-10-09 07:28:22 +00006 * Hacked by Tito <farmatito@tiscali.it> for size optimization.
Eric Andersen3443bd72003-07-22 07:30:36 +00007 *
Mike Frysingerf284c762006-04-16 20:38:26 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen3443bd72003-07-22 07:30:36 +00009 *
10 * This program is based on the source code of hdparm: see below...
11 * hdparm.c - Command line interface to get/set hard disk parameters
12 * - by Mark Lord (C) 1994-2002 -- freely distributable
13 */
Glenn L McGrath07085852003-10-09 07:28:22 +000014
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Eric Andersen3443bd72003-07-22 07:30:36 +000016#include <linux/hdreg.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000017
Eric Andersen3443bd72003-07-22 07:30:36 +000018/* device types */
19/* ------------ */
20#define NO_DEV 0xffff
21#define ATA_DEV 0x0000
22#define ATAPI_DEV 0x0001
23
24/* word definitions */
25/* ---------------- */
26#define GEN_CONFIG 0 /* general configuration */
27#define LCYLS 1 /* number of logical cylinders */
28#define CONFIG 2 /* specific configuration */
29#define LHEADS 3 /* number of logical heads */
30#define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
31#define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
32#define LSECTS 6 /* number of logical sectors/track */
33#define START_SERIAL 10 /* ASCII serial number */
34#define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
35#define BUF_TYPE 20 /* buffer type (ATA-1) */
36#define BUFFER__SIZE 21 /* buffer size (ATA-1) */
37#define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
38#define START_FW_REV 23 /* ASCII firmware revision */
39#define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000040#define START_MODEL 27 /* ASCII model number */
41#define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
Denis Vlasenko4b924f32007-05-30 00:29:55 +000042#define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
Eric Andersen3443bd72003-07-22 07:30:36 +000043#define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
44#define CAPAB_0 49 /* capabilities */
45#define CAPAB_1 50
46#define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
47#define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
48#define WHATS_VALID 53 /* what fields are valid */
49#define LCYLS_CUR 54 /* current logical cylinders */
50#define LHEADS_CUR 55 /* current logical heads */
Denis Vlasenko4b924f32007-05-30 00:29:55 +000051#define LSECTS_CUR 56 /* current logical sectors/track */
Eric Andersen3443bd72003-07-22 07:30:36 +000052#define CAPACITY_LSB 57 /* current capacity in sectors */
53#define CAPACITY_MSB 58
54#define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
55#define LBA_SECTS_LSB 60 /* LBA: total number of user */
56#define LBA_SECTS_MSB 61 /* addressable sectors */
57#define SINGLE_DMA 62 /* singleword DMA modes */
58#define MULTI_DMA 63 /* multiword DMA modes */
59#define ADV_PIO_MODES 64 /* advanced PIO modes supported */
60 /* multiword DMA xfer cycle time: */
61#define DMA_TIME_MIN 65 /* - minimum */
62#define DMA_TIME_NORM 66 /* - manufacturer's recommended */
63 /* minimum PIO xfer cycle time: */
64#define PIO_NO_FLOW 67 /* - without flow control */
65#define PIO_FLOW 68 /* - with IORDY flow control */
66#define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
67#define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
68#define CDR_MAJOR 73 /* CD ROM: major version number */
69#define CDR_MINOR 74 /* CD ROM: minor version number */
70#define QUEUE_DEPTH 75 /* queue depth */
71#define MAJOR 80 /* major version number */
72#define MINOR 81 /* minor version number */
73#define CMDS_SUPP_0 82 /* command/feature set(s) supported */
74#define CMDS_SUPP_1 83
75#define CMDS_SUPP_2 84
76#define CMDS_EN_0 85 /* command/feature set(s) enabled */
77#define CMDS_EN_1 86
78#define CMDS_EN_2 87
79#define ULTRA_DMA 88 /* ultra DMA modes */
80 /* time to complete security erase */
81#define ERASE_TIME 89 /* - ordinary */
82#define ENH_ERASE_TIME 90 /* - enhanced */
83#define ADV_PWR 91 /* current advanced power management level
Glenn L McGrath07085852003-10-09 07:28:22 +000084 in low byte, 0x40 in high byte. */
Eric Andersen3443bd72003-07-22 07:30:36 +000085#define PSWD_CODE 92 /* master password revision code */
86#define HWRST_RSLT 93 /* hardware reset result */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000087#define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
Eric Andersen3443bd72003-07-22 07:30:36 +000088#define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
89#define LBA_MID 101 /* bits are used, but addr 103 */
90#define LBA_48_MSB 102 /* has been reserved for LBA in */
91#define LBA_64_MSB 103 /* the future. */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000092#define RM_STAT 127 /* removable media status notification feature set support */
Eric Andersen3443bd72003-07-22 07:30:36 +000093#define SECU_STATUS 128 /* security status */
94#define CFA_PWR_MODE 160 /* CFA power mode 1 */
95#define START_MEDIA 176 /* media serial number */
96#define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
97#define START_MANUF 196 /* media manufacturer I.D. */
98#define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
99#define INTEGRITY 255 /* integrity word */
100
101/* bit definitions within the words */
102/* -------------------------------- */
103
104/* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
105#define VALID 0xc000
106#define VALID_VAL 0x4000
107/* many words are considered invalid if they are either all-0 or all-1 */
108#define NOVAL_0 0x0000
109#define NOVAL_1 0xffff
110
111/* word 0: gen_config */
Glenn L McGrath07085852003-10-09 07:28:22 +0000112#define NOT_ATA 0x8000
Eric Andersen3443bd72003-07-22 07:30:36 +0000113#define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
114#define MEDIA_REMOVABLE 0x0080
115#define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
116#define INCOMPLETE 0x0004
117#define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
118#define DRQ_RESPONSE_TIME 0x0060
119#define DRQ_3MS_VAL 0x0000
120#define DRQ_INTR_VAL 0x0020
121#define DRQ_50US_VAL 0x0040
122#define PKT_SIZE_SUPPORTED 0x0003
123#define PKT_SIZE_12_VAL 0x0000
124#define PKT_SIZE_16_VAL 0x0001
125#define EQPT_TYPE 0x1f00
126#define SHIFT_EQPT 8
127
128#define CDROM 0x0005
129
Eric Andersen3443bd72003-07-22 07:30:36 +0000130/* word 1: number of logical cylinders */
131#define LCYLS_MAX 0x3fff /* maximum allowable value */
132
Eric Andersenaff114c2004-04-14 17:51:38 +0000133/* word 2: specific configuration
Eric Andersen3443bd72003-07-22 07:30:36 +0000134 * (a) require SET FEATURES to spin-up
135 * (b) require spin-up to fully reply to IDENTIFY DEVICE
136 */
137#define STBY_NID_VAL 0x37c8 /* (a) and (b) */
138#define STBY_ID_VAL 0x738c /* (a) and not (b) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000139#define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
Eric Andersen3443bd72003-07-22 07:30:36 +0000140#define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
141
142/* words 47 & 59: sector_xfer_max & sector_xfer_cur */
143#define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
144#define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
145
146/* word 49: capabilities 0 */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000147#define STD_STBY 0x2000 /* 1=standard values supported (ATA); 0=vendor specific values */
Eric Andersen3443bd72003-07-22 07:30:36 +0000148#define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
149#define IORDY_OFF 0x0400 /* 1=may be disabled */
150#define LBA_SUP 0x0200 /* 1=Logical Block Address support */
151#define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
152#define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
153#define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
154#define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
155#define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
156
157/* word 50: capabilities 1 */
158#define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
159
160/* words 51 & 52: PIO & DMA cycle times */
161#define MODE 0xff00 /* the mode is in the MSBs */
162
163/* word 53: whats_valid */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000164#define OK_W88 0x0004 /* the ultra_dma info is valid */
Eric Andersen3443bd72003-07-22 07:30:36 +0000165#define OK_W64_70 0x0002 /* see above for word descriptions */
166#define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
167
168/*word 63,88: dma_mode, ultra_dma_mode*/
169#define MODE_MAX 7 /* bit definitions force udma <=7 (when
170 * udma >=8 comes out it'll have to be
171 * defined in a new dma_mode word!) */
172
173/* word 64: PIO transfer modes */
174#define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
175#define PIO_MODE_MAX 8 /* but all 8 bits are defined */
176
177/* word 75: queue_depth */
178#define DEPTH_BITS 0x001f /* bits used for queue depth */
179
180/* words 80-81: version numbers */
181/* NOVAL_0 or NOVAL_1 means device does not report version */
182
183/* word 81: minor version number */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000184#define MINOR_MAX 0x22
Eric Andersen3443bd72003-07-22 07:30:36 +0000185/* words 82-84: cmds/feats supported */
186#define CMDS_W82 0x77ff /* word 82: defined command locations*/
187#define CMDS_W83 0x3fff /* word 83: defined command locations*/
188#define CMDS_W84 0x002f /* word 83: defined command locations*/
Glenn L McGrath07085852003-10-09 07:28:22 +0000189#define SUPPORT_48_BIT 0x0400
Eric Andersen3443bd72003-07-22 07:30:36 +0000190#define NUM_CMD_FEAT_STR 48
191
Eric Andersen3443bd72003-07-22 07:30:36 +0000192/* words 85-87: cmds/feats enabled */
193/* use cmd_feat_str[] to display what commands and features have
Glenn L McGrath07085852003-10-09 07:28:22 +0000194 * been enabled with words 85-87
Eric Andersen3443bd72003-07-22 07:30:36 +0000195 */
196
197/* words 89, 90, SECU ERASE TIME */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000198#define ERASE_BITS 0x00ff
Eric Andersen3443bd72003-07-22 07:30:36 +0000199
200/* word 92: master password revision */
201/* NOVAL_0 or NOVAL_1 means no support for master password revision */
202
203/* word 93: hw reset result */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000204#define CBLID 0x2000 /* CBLID status */
205#define RST0 0x0001 /* 1=reset to device #0 */
206#define DEV_DET 0x0006 /* how device num determined */
207#define JUMPER_VAL 0x0002 /* device num determined by jumper */
208#define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
Eric Andersen3443bd72003-07-22 07:30:36 +0000209
210/* word 127: removable media status notification feature set support */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000211#define RM_STAT_BITS 0x0003
212#define RM_STAT_SUP 0x0001
Glenn L McGrath07085852003-10-09 07:28:22 +0000213
Eric Andersen3443bd72003-07-22 07:30:36 +0000214/* word 128: security */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000215#define SECU_ENABLED 0x0002
216#define SECU_LEVEL 0x0010
217#define NUM_SECU_STR 6
Eric Andersen3443bd72003-07-22 07:30:36 +0000218
219/* word 160: CFA power mode */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000220#define VALID_W160 0x8000 /* 1=word valid */
221#define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
222#define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
223#define MAX_AMPS 0x0fff /* value = max current in ma */
Eric Andersen3443bd72003-07-22 07:30:36 +0000224
225/* word 255: integrity */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000226#define SIG 0x00ff /* signature location */
227#define SIG_VAL 0x00a5 /* signature value */
Eric Andersen3443bd72003-07-22 07:30:36 +0000228
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000229#define TIMING_BUF_MB 1
230#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
Glenn L McGrath07085852003-10-09 07:28:22 +0000231
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000232#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
Glenn L McGrath07085852003-10-09 07:28:22 +0000233
Denis Vlasenko892536f2007-09-27 10:23:34 +0000234
235enum { fd = 3 };
236
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +0000237
238struct globals {
239 smallint get_identity, get_geom;
240 smallint do_flush;
241 smallint do_ctimings, do_timings;
242 smallint reread_partn;
243 smallint set_piomode, noisy_piomode;
244 smallint set_readahead, get_readahead;
245 smallint set_readonly, get_readonly;
246 smallint set_unmask, get_unmask;
247 smallint set_mult, get_mult;
248 smallint set_dma_q, get_dma_q;
249 smallint set_nowerr, get_nowerr;
250 smallint set_keep, get_keep;
251 smallint set_io32bit, get_io32bit;
252 int piomode;
253 unsigned long Xreadahead;
254 unsigned long readonly;
255 unsigned long unmask;
256 unsigned long mult;
257 unsigned long dma_q;
258 unsigned long nowerr;
259 unsigned long keep;
260 unsigned long io32bit;
261#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
262 unsigned long dma;
263 smallint set_dma, get_dma;
264#endif
265#ifdef HDIO_DRIVE_CMD
266 smallint set_xfermode, get_xfermode;
267 smallint set_dkeep, get_dkeep;
268 smallint set_standby, get_standby;
269 smallint set_lookahead, get_lookahead;
270 smallint set_prefetch, get_prefetch;
271 smallint set_defects, get_defects;
272 smallint set_wcache, get_wcache;
273 smallint set_doorlock, get_doorlock;
274 smallint set_seagate, get_seagate;
275 smallint set_standbynow, get_standbynow;
276 smallint set_sleepnow, get_sleepnow;
277 smallint get_powermode;
278 smallint set_apmmode, get_apmmode;
279 int xfermode_requested;
280 unsigned long dkeep;
281 unsigned long standby_requested;
282 unsigned long lookahead;
283 unsigned long prefetch;
284 unsigned long defects;
285 unsigned long wcache;
286 unsigned long doorlock;
287 unsigned long apmmode;
288#endif
289 USE_FEATURE_HDPARM_GET_IDENTITY( smallint get_IDentity;)
290 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( smallint set_busstate, get_busstate;)
291 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET( smallint perform_reset;)
292 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( smallint perform_tristate;)
293 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(smallint unregister_hwif;)
294 USE_FEATURE_HDPARM_HDIO_SCAN_HWIF( smallint scan_hwif;)
295 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( unsigned long busstate;)
296 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( unsigned long tristate;)
297 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(unsigned long hwif;)
298#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
299 unsigned long hwif_data;
300 unsigned long hwif_ctrl;
301 unsigned long hwif_irq;
302#endif
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +0000303#ifdef DO_FLUSHCACHE
304 unsigned char flushcache[4] = { WIN_FLUSHCACHE, 0, 0, 0 };
305#endif
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +0000306};
307#define G (*(struct globals*)&bb_common_bufsiz1)
308struct BUG_G_too_big {
309 char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
310};
311#define get_identity (G.get_identity )
312#define get_geom (G.get_geom )
313#define do_flush (G.do_flush )
314#define do_ctimings (G.do_ctimings )
315#define do_timings (G.do_timings )
316#define reread_partn (G.reread_partn )
317#define set_piomode (G.set_piomode )
318#define noisy_piomode (G.noisy_piomode )
319#define set_readahead (G.set_readahead )
320#define get_readahead (G.get_readahead )
321#define set_readonly (G.set_readonly )
322#define get_readonly (G.get_readonly )
323#define set_unmask (G.set_unmask )
324#define get_unmask (G.get_unmask )
325#define set_mult (G.set_mult )
326#define get_mult (G.get_mult )
327#define set_dma_q (G.set_dma_q )
328#define get_dma_q (G.get_dma_q )
329#define set_nowerr (G.set_nowerr )
330#define get_nowerr (G.get_nowerr )
331#define set_keep (G.set_keep )
332#define get_keep (G.get_keep )
333#define set_io32bit (G.set_io32bit )
334#define get_io32bit (G.get_io32bit )
335#define piomode (G.piomode )
336#define Xreadahead (G.Xreadahead )
337#define readonly (G.readonly )
338#define unmask (G.unmask )
339#define mult (G.mult )
340#define dma_q (G.dma_q )
341#define nowerr (G.nowerr )
342#define keep (G.keep )
343#define io32bit (G.io32bit )
344#define dma (G.dma )
345#define set_dma (G.set_dma )
346#define get_dma (G.get_dma )
347#define set_xfermode (G.set_xfermode )
348#define get_xfermode (G.get_xfermode )
349#define set_dkeep (G.set_dkeep )
350#define get_dkeep (G.get_dkeep )
351#define set_standby (G.set_standby )
352#define get_standby (G.get_standby )
353#define set_lookahead (G.set_lookahead )
354#define get_lookahead (G.get_lookahead )
355#define set_prefetch (G.set_prefetch )
356#define get_prefetch (G.get_prefetch )
357#define set_defects (G.set_defects )
358#define get_defects (G.get_defects )
359#define set_wcache (G.set_wcache )
360#define get_wcache (G.get_wcache )
361#define set_doorlock (G.set_doorlock )
362#define get_doorlock (G.get_doorlock )
363#define set_seagate (G.set_seagate )
364#define get_seagate (G.get_seagate )
365#define set_standbynow (G.set_standbynow )
366#define get_standbynow (G.get_standbynow )
367#define set_sleepnow (G.set_sleepnow )
368#define get_sleepnow (G.get_sleepnow )
369#define get_powermode (G.get_powermode )
370#define set_apmmode (G.set_apmmode )
371#define get_apmmode (G.get_apmmode )
372#define xfermode_requested (G.xfermode_requested )
373#define dkeep (G.dkeep )
374#define standby_requested (G.standby_requested )
375#define lookahead (G.lookahead )
376#define prefetch (G.prefetch )
377#define defects (G.defects )
378#define wcache (G.wcache )
379#define doorlock (G.doorlock )
380#define apmmode (G.apmmode )
381#define get_IDentity (G.get_IDentity )
382#define set_busstate (G.set_busstate )
383#define get_busstate (G.get_busstate )
384#define perform_reset (G.perform_reset )
385#define perform_tristate (G.perform_tristate )
386#define unregister_hwif (G.unregister_hwif )
387#define scan_hwif (G.scan_hwif )
388#define busstate (G.busstate )
389#define tristate (G.tristate )
390#define hwif (G.hwif )
391#define hwif_data (G.hwif_data )
392#define hwif_ctrl (G.hwif_ctrl )
393#define hwif_irq (G.hwif_irq )
394
395
Glenn L McGrath07085852003-10-09 07:28:22 +0000396/* Busybox messages and functions */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000397#if ENABLE_IOCTL_HEX2STR_ERROR
Denis Vlasenko892536f2007-09-27 10:23:34 +0000398static int ioctl_alt_func(/*int fd,*/ int cmd, unsigned char *args, int alt, const char *string)
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000399{
400 if (!ioctl(fd, cmd, args))
401 return 0;
402 args[0] = alt;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000403 return bb_ioctl_or_warn(fd, cmd, args, string);
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000404}
Denis Vlasenko892536f2007-09-27 10:23:34 +0000405#define ioctl_alt_or_warn(cmd,args,alt) ioctl_alt_func(cmd,args,alt,#cmd)
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000406#else
Denis Vlasenko892536f2007-09-27 10:23:34 +0000407static int ioctl_alt_func(/*int fd,*/ int cmd, unsigned char *args, int alt)
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000408{
409 if (!ioctl(fd, cmd, args))
410 return 0;
411 args[0] = alt;
412 return bb_ioctl_or_warn(fd, cmd, args);
413}
Denis Vlasenko892536f2007-09-27 10:23:34 +0000414#define ioctl_alt_or_warn(cmd,args,alt) ioctl_alt_func(cmd,args,alt)
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000415#endif
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000416
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000417static void on_off(int value)
418{
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000419 puts(value ? " (on)" : " (off)");
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000420}
Glenn L McGrath07085852003-10-09 07:28:22 +0000421
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000422static void print_flag_on_off(int get_arg, const char *s, unsigned long arg)
Glenn L McGrath07085852003-10-09 07:28:22 +0000423{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000424 if (get_arg) {
Rob Landleyadde7982006-05-16 15:32:30 +0000425 printf(" setting %s to %ld", s, arg);
Glenn L McGrath07085852003-10-09 07:28:22 +0000426 on_off(arg);
427 }
428}
429
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000430static void print_value_on_off(const char *str, unsigned long argp)
Glenn L McGrath07085852003-10-09 07:28:22 +0000431{
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000432 printf(" %s\t= %2ld", str, argp);
433 on_off(argp != 0);
Glenn L McGrath07085852003-10-09 07:28:22 +0000434}
Eric Andersen416c2422003-12-12 00:08:57 +0000435
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000436#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Denis Vlasenko4daad902007-09-27 10:20:47 +0000437static void print_ascii(const char *p, int length)
438{
439#if BB_BIG_ENDIAN
440#define LE_ONLY(x)
441 enum { ofs = 0 };
442#else
443#define LE_ONLY(x) x
444 /* every 16bit word is big-endian (i.e. inverted) */
445 /* accessing bytes in 1,0, 3,2, 5,4... sequence */
446 int ofs = 1;
447#endif
448
449 length *= 2;
450 /* find first non-space & print it */
451 while (length && p[ofs] != ' ') {
452 p++;
453 LE_ONLY(ofs = -ofs;)
454 length--;
455 }
456 while (length && p[ofs]) {
457 bb_putchar(p[ofs]);
458 p++;
459 LE_ONLY(ofs = -ofs;)
460 length--;
461 }
462 bb_putchar('\n');
463#undef LE_ONLY
464}
Glenn L McGrath07085852003-10-09 07:28:22 +0000465
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000466static void xprint_ascii(uint16_t *val, int i, const char *string, int n)
Glenn L McGrath07085852003-10-09 07:28:22 +0000467{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000468 if (val[i]) {
469 printf("\t%-20s", string);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000470 print_ascii((void*)&val[i], n);
Glenn L McGrath07085852003-10-09 07:28:22 +0000471 }
472}
Glenn L McGrath07085852003-10-09 07:28:22 +0000473
Glenn L McGrath07085852003-10-09 07:28:22 +0000474static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
475{
Eric Andersen3443bd72003-07-22 07:30:36 +0000476 uint16_t ii;
477 uint8_t err_dma = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000478
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000479 for (ii = 0; ii <= MODE_MAX; ii++) {
480 if (mode_sel & 0x0001) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000481 printf("*%cdma%u ", cc, ii);
Rob Landleya3e4f382006-04-29 16:06:31 +0000482 if (*have_mode)
Glenn L McGrath07085852003-10-09 07:28:22 +0000483 err_dma = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000484 *have_mode = 1;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000485 } else if (mode_sup & 0x0001)
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000486 printf("%cdma%u ", cc, ii);
Glenn L McGrath07085852003-10-09 07:28:22 +0000487
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000488 mode_sup >>= 1;
489 mode_sel >>= 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000490 }
491 return err_dma;
492}
Glenn L McGrath07085852003-10-09 07:28:22 +0000493
Denis Vlasenko91303402007-10-30 19:36:54 +0000494static const char pkt_str[] ALIGN1 =
495 "Direct-access device" "\0" /* word 0, bits 12-8 = 00 */
496 "Sequential-access device" "\0" /* word 0, bits 12-8 = 01 */
497 "Printer" "\0" /* word 0, bits 12-8 = 02 */
498 "Processor" "\0" /* word 0, bits 12-8 = 03 */
499 "Write-once device" "\0" /* word 0, bits 12-8 = 04 */
500 "CD-ROM" "\0" /* word 0, bits 12-8 = 05 */
501 "Scanner" "\0" /* word 0, bits 12-8 = 06 */
502 "Optical memory" "\0" /* word 0, bits 12-8 = 07 */
503 "Medium changer" "\0" /* word 0, bits 12-8 = 08 */
504 "Communications device" "\0" /* word 0, bits 12-8 = 09 */
505 "ACS-IT8 device" "\0" /* word 0, bits 12-8 = 0a */
506 "ACS-IT8 device" "\0" /* word 0, bits 12-8 = 0b */
507 "Array controller" "\0" /* word 0, bits 12-8 = 0c */
508 "Enclosure services" "\0" /* word 0, bits 12-8 = 0d */
509 "Reduced block command device" "\0" /* word 0, bits 12-8 = 0e */
510 "Optical card reader/writer" "\0" /* word 0, bits 12-8 = 0f */
511;
512
513static const char ata1_cfg_str[] ALIGN1 = /* word 0 in ATA-1 mode */
514 "reserved" "\0" /* bit 0 */
515 "hard sectored" "\0" /* bit 1 */
516 "soft sectored" "\0" /* bit 2 */
517 "not MFM encoded " "\0" /* bit 3 */
518 "head switch time > 15us" "\0" /* bit 4 */
519 "spindle motor control option" "\0" /* bit 5 */
520 "fixed drive" "\0" /* bit 6 */
521 "removable drive" "\0" /* bit 7 */
522 "disk xfer rate <= 5Mbs" "\0" /* bit 8 */
523 "disk xfer rate > 5Mbs, <= 10Mbs" "\0" /* bit 9 */
524 "disk xfer rate > 5Mbs" "\0" /* bit 10 */
525 "rotational speed tol." "\0" /* bit 11 */
526 "data strobe offset option" "\0" /* bit 12 */
527 "track offset option" "\0" /* bit 13 */
528 "format speed tolerance gap reqd" "\0" /* bit 14 */
529 "ATAPI" /* bit 14 */
530;
531
532static const char minor_str[] ALIGN1 =
533 /* word 81 value: */
534 "Unspecified" "\0" /* 0x0000 */
535 "ATA-1 X3T9.2 781D prior to rev.4" "\0" /* 0x0001 */
536 "ATA-1 published, ANSI X3.221-1994" "\0" /* 0x0002 */
537 "ATA-1 X3T9.2 781D rev.4" "\0" /* 0x0003 */
538 "ATA-2 published, ANSI X3.279-1996" "\0" /* 0x0004 */
539 "ATA-2 X3T10 948D prior to rev.2k" "\0" /* 0x0005 */
540 "ATA-3 X3T10 2008D rev.1" "\0" /* 0x0006 */
541 "ATA-2 X3T10 948D rev.2k" "\0" /* 0x0007 */
542 "ATA-3 X3T10 2008D rev.0" "\0" /* 0x0008 */
543 "ATA-2 X3T10 948D rev.3" "\0" /* 0x0009 */
544 "ATA-3 published, ANSI X3.298-199x" "\0" /* 0x000a */
545 "ATA-3 X3T10 2008D rev.6" "\0" /* 0x000b */
546 "ATA-3 X3T13 2008D rev.7 and 7a" "\0" /* 0x000c */
547 "ATA/ATAPI-4 X3T13 1153D rev.6" "\0" /* 0x000d */
548 "ATA/ATAPI-4 T13 1153D rev.13" "\0" /* 0x000e */
549 "ATA/ATAPI-4 X3T13 1153D rev.7" "\0" /* 0x000f */
550 "ATA/ATAPI-4 T13 1153D rev.18" "\0" /* 0x0010 */
551 "ATA/ATAPI-4 T13 1153D rev.15" "\0" /* 0x0011 */
552 "ATA/ATAPI-4 published, ANSI INCITS 317-1998" "\0" /* 0x0012 */
553 "ATA/ATAPI-5 T13 1321D rev.3" "\0" /* 0x0013 */
554 "ATA/ATAPI-4 T13 1153D rev.14" "\0" /* 0x0014 */
555 "ATA/ATAPI-5 T13 1321D rev.1" "\0" /* 0x0015 */
556 "ATA/ATAPI-5 published, ANSI INCITS 340-2000" "\0" /* 0x0016 */
557 "ATA/ATAPI-4 T13 1153D rev.17" "\0" /* 0x0017 */
558 "ATA/ATAPI-6 T13 1410D rev.0" "\0" /* 0x0018 */
559 "ATA/ATAPI-6 T13 1410D rev.3a" "\0" /* 0x0019 */
560 "ATA/ATAPI-7 T13 1532D rev.1" "\0" /* 0x001a */
561 "ATA/ATAPI-6 T13 1410D rev.2" "\0" /* 0x001b */
562 "ATA/ATAPI-6 T13 1410D rev.1" "\0" /* 0x001c */
563 "ATA/ATAPI-7 published, ANSI INCITS 397-2005" "\0" /* 0x001d */
564 "ATA/ATAPI-7 T13 1532D rev.0" "\0" /* 0x001e */
565 "reserved" "\0" /* 0x001f */
566 "reserved" "\0" /* 0x0020 */
567 "ATA/ATAPI-7 T13 1532D rev.4a" "\0" /* 0x0021 */
568 "ATA/ATAPI-6 published, ANSI INCITS 361-2002" "\0" /* 0x0022 */
569 "reserved" /* 0x0023-0xfffe */
570;
571static const char actual_ver[MINOR_MAX + 2] ALIGN1 = {
572 /* word 81 value: */
573 0, /* 0x0000 WARNING: actual_ver[] array */
574 1, /* 0x0001 WARNING: corresponds */
575 1, /* 0x0002 WARNING: *exactly* */
576 1, /* 0x0003 WARNING: to the ATA/ */
577 2, /* 0x0004 WARNING: ATAPI version */
578 2, /* 0x0005 WARNING: listed in */
579 3, /* 0x0006 WARNING: the */
580 2, /* 0x0007 WARNING: minor_str */
581 3, /* 0x0008 WARNING: array */
582 2, /* 0x0009 WARNING: above. */
583 3, /* 0x000a WARNING: */
584 3, /* 0x000b WARNING: If you change */
585 3, /* 0x000c WARNING: that one, */
586 4, /* 0x000d WARNING: change this one */
587 4, /* 0x000e WARNING: too!!! */
588 4, /* 0x000f */
589 4, /* 0x0010 */
590 4, /* 0x0011 */
591 4, /* 0x0012 */
592 5, /* 0x0013 */
593 4, /* 0x0014 */
594 5, /* 0x0015 */
595 5, /* 0x0016 */
596 4, /* 0x0017 */
597 6, /* 0x0018 */
598 6, /* 0x0019 */
599 7, /* 0x001a */
600 6, /* 0x001b */
601 6, /* 0x001c */
602 7, /* 0x001d */
603 7, /* 0x001e */
604 0, /* 0x001f */
605 0, /* 0x0020 */
606 7, /* 0x0021 */
607 6, /* 0x0022 */
608 0 /* 0x0023-0xfffe */
609};
610
611static const char cmd_feat_str[] ALIGN1 =
612 "" "\0" /* word 82 bit 15: obsolete */
613 "NOP cmd" "\0" /* word 82 bit 14 */
614 "READ BUFFER cmd" "\0" /* word 82 bit 13 */
615 "WRITE BUFFER cmd" "\0" /* word 82 bit 12 */
616 "" "\0" /* word 82 bit 11: obsolete */
617 "Host Protected Area feature set" "\0" /* word 82 bit 10 */
618 "DEVICE RESET cmd" "\0" /* word 82 bit 9 */
619 "SERVICE interrupt" "\0" /* word 82 bit 8 */
620 "Release interrupt" "\0" /* word 82 bit 7 */
621 "Look-ahead" "\0" /* word 82 bit 6 */
622 "Write cache" "\0" /* word 82 bit 5 */
623 "PACKET command feature set" "\0" /* word 82 bit 4 */
624 "Power Management feature set" "\0" /* word 82 bit 3 */
625 "Removable Media feature set" "\0" /* word 82 bit 2 */
626 "Security Mode feature set" "\0" /* word 82 bit 1 */
627 "SMART feature set" "\0" /* word 82 bit 0 */
628 /* -------------- */
629 "" "\0" /* word 83 bit 15: !valid bit */
630 "" "\0" /* word 83 bit 14: valid bit */
631 "FLUSH CACHE EXT cmd" "\0" /* word 83 bit 13 */
632 "Mandatory FLUSH CACHE cmd " "\0" /* word 83 bit 12 */
633 "Device Configuration Overlay feature set " "\0"
634 "48-bit Address feature set " "\0" /* word 83 bit 10 */
635 "" "\0"
636 "SET MAX security extension" "\0" /* word 83 bit 8 */
637 "Address Offset Reserved Area Boot" "\0" /* word 83 bit 7 */
638 "SET FEATURES subcommand required to spinup after power up" "\0"
639 "Power-Up In Standby feature set" "\0" /* word 83 bit 5 */
640 "Removable Media Status Notification feature set" "\0"
641 "Adv. Power Management feature set" "\0" /* word 83 bit 3 */
642 "CFA feature set" "\0" /* word 83 bit 2 */
643 "READ/WRITE DMA QUEUED" "\0" /* word 83 bit 1 */
644 "DOWNLOAD MICROCODE cmd" "\0" /* word 83 bit 0 */
645 /* -------------- */
646 "" "\0" /* word 84 bit 15: !valid bit */
647 "" "\0" /* word 84 bit 14: valid bit */
648 "" "\0" /* word 84 bit 13: reserved */
649 "" "\0" /* word 84 bit 12: reserved */
650 "" "\0" /* word 84 bit 11: reserved */
651 "" "\0" /* word 84 bit 10: reserved */
652 "" "\0" /* word 84 bit 9: reserved */
653 "" "\0" /* word 84 bit 8: reserved */
654 "" "\0" /* word 84 bit 7: reserved */
655 "" "\0" /* word 84 bit 6: reserved */
656 "General Purpose Logging feature set" "\0" /* word 84 bit 5 */
657 "" "\0" /* word 84 bit 4: reserved */
658 "Media Card Pass Through Command feature set " "\0"
659 "Media serial number " "\0" /* word 84 bit 2 */
660 "SMART self-test " "\0" /* word 84 bit 1 */
661 "SMART error logging " /* word 84 bit 0 */
662;
663
664static const char secu_str[] ALIGN1 =
665 "supported" "\0" /* word 128, bit 0 */
666 "enabled" "\0" /* word 128, bit 1 */
667 "locked" "\0" /* word 128, bit 2 */
668 "frozen" "\0" /* word 128, bit 3 */
669 "expired: security count" "\0" /* word 128, bit 4 */
670 "supported: enhanced erase" /* word 128, bit 5 */
671;
672
673// Parse 512 byte disk identification block and print much crap.
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +0000674static void identify(uint16_t *val) ATTRIBUTE_NORETURN;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000675static void identify(uint16_t *val)
Eric Andersen3443bd72003-07-22 07:30:36 +0000676{
Denis Vlasenko4daad902007-09-27 10:20:47 +0000677 uint16_t ii, jj, kk;
Eric Andersen3443bd72003-07-22 07:30:36 +0000678 uint16_t like_std = 1, std = 0, min_std = 0xffff;
679 uint16_t dev = NO_DEV, eqpt = NO_DEV;
680 uint8_t have_mode = 0, err_dma = 0;
681 uint8_t chksum = 0;
682 uint32_t ll, mm, nn, oo;
Rob Landley2e2d7522006-04-29 15:23:33 +0000683 uint64_t bbbig; /* (:) */
Rob Landleyadde7982006-05-16 15:32:30 +0000684 const char *strng;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000685#if BB_BIG_ENDIAN
686 uint16_t buf[256];
Eric Andersen3443bd72003-07-22 07:30:36 +0000687
Denis Vlasenko4daad902007-09-27 10:20:47 +0000688 // Adjust for endianness
689 swab(val, buf, sizeof(buf));
690 val = buf;
691#endif
Eric Andersen3443bd72003-07-22 07:30:36 +0000692 /* check if we recognise the device type */
Denis Vlasenko4daad902007-09-27 10:20:47 +0000693 bb_putchar('\n');
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000694 if (!(val[GEN_CONFIG] & NOT_ATA)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000695 dev = ATA_DEV;
696 printf("ATA device, with ");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000697 } else if (val[GEN_CONFIG]==CFA_SUPPORT_VAL) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000698 dev = ATA_DEV;
699 like_std = 4;
700 printf("CompactFlash ATA device, with ");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000701 } else if (!(val[GEN_CONFIG] & NOT_ATAPI)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000702 dev = ATAPI_DEV;
703 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000704 printf("ATAPI %s, with ", eqpt <= 0xf ? nth_string(pkt_str, eqpt) : "unknown");
Eric Andersen3443bd72003-07-22 07:30:36 +0000705 like_std = 3;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000706 } else
Denis Vlasenko91303402007-10-30 19:36:54 +0000707 /* "Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n" */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000708 bb_error_msg_and_die("unknown device type");
Eric Andersen3443bd72003-07-22 07:30:36 +0000709
Rob Landleyadde7982006-05-16 15:32:30 +0000710 printf("%sremovable media\n", !(val[GEN_CONFIG] & MEDIA_REMOVABLE) ? "non-" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +0000711 /* Info from the specific configuration word says whether or not the
712 * ID command completed correctly. It is only defined, however in
Glenn L McGrath07085852003-10-09 07:28:22 +0000713 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
Eric Andersen3443bd72003-07-22 07:30:36 +0000714 * standards. Since the values allowed for this word are extremely
715 * specific, it should be safe to check it now, even though we don't
716 * know yet what standard this device is using.
717 */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000718 if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)
719 || (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL)
720 ) {
Glenn L McGrath07085852003-10-09 07:28:22 +0000721 like_std = 5;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000722 if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL))
Rob Landleyadde7982006-05-16 15:32:30 +0000723 printf("powers-up in standby; SET FEATURES subcmd spins-up.\n");
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000724 if (((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE))
Rob Landleyadde7982006-05-16 15:32:30 +0000725 printf("\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000726 }
727
728 /* output the model and serial numbers and the fw revision */
Glenn L McGrath07085852003-10-09 07:28:22 +0000729 xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
730 xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
731 xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
732 xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
733 xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
Eric Andersen3443bd72003-07-22 07:30:36 +0000734
735 /* major & minor standards version number (Note: these words were not
736 * defined until ATA-3 & the CDROM std uses different words.) */
737 printf("Standards:");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000738 if (eqpt != CDROM) {
739 if (val[MINOR] && (val[MINOR] <= MINOR_MAX)) {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000740 if (like_std < 3) like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000741 std = actual_ver[val[MINOR]];
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000742 if (std) printf("\n\tUsed: %s ", nth_string(minor_str, val[MINOR]));
Glenn L McGrath07085852003-10-09 07:28:22 +0000743
Eric Andersen3443bd72003-07-22 07:30:36 +0000744 }
745 /* looks like when they up-issue the std, they obsolete one;
Glenn L McGrath07085852003-10-09 07:28:22 +0000746 * thus, only the newest 4 issues need be supported. (That's
Eric Andersen3443bd72003-07-22 07:30:36 +0000747 * what "kk" and "min_std" are all about.) */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000748 if (val[MAJOR] && (val[MAJOR] != NOVAL_1)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000749 printf("\n\tSupported: ");
750 jj = val[MAJOR] << 1;
751 kk = like_std >4 ? like_std-4: 0;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000752 for (ii = 14; (ii >0)&&(ii>kk); ii--) {
753 if (jj & 0x8000) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000754 printf("%u ", ii);
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000755 if (like_std < ii) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000756 like_std = ii;
757 kk = like_std >4 ? like_std-4: 0;
758 }
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000759 if (min_std > ii) min_std = ii;
Eric Andersen3443bd72003-07-22 07:30:36 +0000760 }
761 jj <<= 1;
762 }
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000763 if (like_std < 3) like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000764 }
765 /* Figure out what standard the device is using if it hasn't told
766 * us. If we know the std, check if the device is using any of
767 * the words from the next level up. It happens.
768 */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000769 if (like_std < std) like_std = std;
Glenn L McGrath07085852003-10-09 07:28:22 +0000770
Rob Landleya3e4f382006-04-29 16:06:31 +0000771 if (((std == 5) || (!std && (like_std < 6))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000772 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
773 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
774 ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000775 ( val[CMDS_SUPP_2] & CMDS_W84) ) )
776 ) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000777 like_std = 6;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000778 } else if (((std == 4) || (!std && (like_std < 5))) &&
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000779 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
Glenn L McGrath07085852003-10-09 07:28:22 +0000780 (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
781 ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
782 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
783 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000784 like_std = 5;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000785 } else if (((std == 3) || (!std && (like_std < 4))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000786 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
787 ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
788 (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
789 (( val[CAPAB_1] & VALID) == VALID_VAL) ||
790 (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000791 (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) )
792 ) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000793 like_std = 4;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000794 } else if (((std == 2) || (!std && (like_std < 3)))
795 && ((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
796 ) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000797 like_std = 3;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000798 } else if (((std == 1) || (!std && (like_std < 2))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000799 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
800 (val[WHATS_VALID] & OK_W64_70)) )
801 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000802 like_std = 2;
803 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000804
Rob Landleya3e4f382006-04-29 16:06:31 +0000805 if (!std)
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000806 printf("\n\tLikely used: %u\n", like_std);
Rob Landleya3e4f382006-04-29 16:06:31 +0000807 else if (like_std > std)
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000808 printf("& some of %u\n", like_std);
Glenn L McGrath07085852003-10-09 07:28:22 +0000809 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000810 bb_putchar('\n');
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000811 } else {
Eric Andersen3443bd72003-07-22 07:30:36 +0000812 /* TBD: do CDROM stuff more thoroughly. For now... */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000813 kk = 0;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000814 if (val[CDR_MINOR] == 9) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000815 kk = 1;
816 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
817 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000818 if (val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000819 kk = 1;
820 printf("\n\tSupported: CD-ROM ATAPI");
821 jj = val[CDR_MAJOR] >> 1;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000822 for (ii = 1; ii < 15; ii++) {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000823 if (jj & 0x0001) printf("-%u ", ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000824 jj >>= 1;
825 }
826 }
Denis Vlasenkofeb7ae72007-10-01 12:05:12 +0000827 puts(kk ? "" : "\n\tLikely used CD-ROM ATAPI-1");
Eric Andersen3443bd72003-07-22 07:30:36 +0000828 /* the cdrom stuff is more like ATA-2 than anything else, so: */
829 like_std = 2;
830 }
831
Rob Landleya3e4f382006-04-29 16:06:31 +0000832 if (min_std == 0xffff)
Glenn L McGrath07085852003-10-09 07:28:22 +0000833 min_std = like_std > 4 ? like_std - 3 : 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000834
835 printf("Configuration:\n");
836 /* more info from the general configuration word */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000837 if ((eqpt != CDROM) && (like_std == 1)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000838 jj = val[GEN_CONFIG] >> 1;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000839 for (ii = 1; ii < 15; ii++) {
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000840 if (jj & 0x0001)
Denis Vlasenkobfc3d822007-11-04 04:10:17 +0000841 printf("\t%s\n", nth_string(ata1_cfg_str, ii));
Eric Andersen3443bd72003-07-22 07:30:36 +0000842 jj >>=1;
843 }
844 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000845 if (dev == ATAPI_DEV) {
Rob Landleyadde7982006-05-16 15:32:30 +0000846 if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_3MS_VAL)
847 strng = "3ms";
848 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_INTR_VAL)
849 strng = "<=10ms with INTRQ";
850 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_50US_VAL)
851 strng ="50us";
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000852 else
Denis Vlasenko91303402007-10-30 19:36:54 +0000853 strng = "unknown";
Rob Landleyadde7982006-05-16 15:32:30 +0000854 printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */
855
856 if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_12_VAL)
857 strng = "12 bytes";
858 else if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_16_VAL)
859 strng = "16 bytes";
860 else
Denis Vlasenko91303402007-10-30 19:36:54 +0000861 strng = "unknown";
Rob Landleyadde7982006-05-16 15:32:30 +0000862 puts(strng);
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000863 } else {
Glenn L McGrath07085852003-10-09 07:28:22 +0000864 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000865 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
866 mm = 0; bbbig = 0;
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000867 if ((ll > 0x00FBFC10) && (!val[LCYLS]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000868 printf("\tCHS addressing not supported\n");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000869 else {
Eric Andersen3443bd72003-07-22 07:30:36 +0000870 jj = val[WHATS_VALID] & OK_W54_58;
Glenn L McGrath07085852003-10-09 07:28:22 +0000871 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",
872 val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
873
Rob Landleyadde7982006-05-16 15:32:30 +0000874 if ((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES]))
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000875 printf("\tbytes/track: %u\tbytes/sector: %u\n", val[TRACK_BYTES], val[SECT_BYTES]);
Glenn L McGrath07085852003-10-09 07:28:22 +0000876
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000877 if (jj) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000878 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000879 if (like_std < 3) {
880 /* check Endian of capacity bytes */
Eric Andersen3443bd72003-07-22 07:30:36 +0000881 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
882 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
Rob Landleya3e4f382006-04-29 16:06:31 +0000883 if (abs(mm - nn) > abs(oo - nn))
Eric Andersen3443bd72003-07-22 07:30:36 +0000884 mm = oo;
885 }
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000886 printf("\tCHS current addressable sectors:%11u\n", mm);
Glenn L McGrath07085852003-10-09 07:28:22 +0000887 }
Eric Andersen3443bd72003-07-22 07:30:36 +0000888 }
889 /* LBA addressing */
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000890 printf("\tLBA user addressable sectors:%11u\n", ll);
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000891 if (((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
892 && (val[CMDS_SUPP_1] & SUPPORT_48_BIT)
893 ) {
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000894 bbbig = (uint64_t)val[LBA_64_MSB] << 48 |
895 (uint64_t)val[LBA_48_MSB] << 32 |
896 (uint64_t)val[LBA_MID] << 16 |
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000897 val[LBA_LSB];
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000898 printf("\tLBA48 user addressable sectors:%11"PRIu64"\n", bbbig);
Eric Andersen3443bd72003-07-22 07:30:36 +0000899 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000900
901 if (!bbbig)
Rob Landley2e2d7522006-04-29 15:23:33 +0000902 bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000903 printf("\tdevice size with M = 1024*1024: %11"PRIu64" MBytes\n", bbbig>>11);
904 bbbig = (bbbig << 9) / 1000000;
905 printf("\tdevice size with M = 1000*1000: %11"PRIu64" MBytes ", bbbig);
Glenn L McGrath07085852003-10-09 07:28:22 +0000906
Rob Landleyadde7982006-05-16 15:32:30 +0000907 if (bbbig > 1000)
Rob Landley81dab2c2006-05-28 01:56:08 +0000908 printf("(%"PRIu64" GB)\n", bbbig/1000);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000909 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000910 bb_putchar('\n');
Eric Andersen3443bd72003-07-22 07:30:36 +0000911 }
912
913 /* hw support of commands (capabilities) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000914 printf("Capabilities:\n\t");
915
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000916 if (dev == ATAPI_DEV) {
Rob Landleyadde7982006-05-16 15:32:30 +0000917 if (eqpt != CDROM && (val[CAPAB_0] & CMD_Q_SUP)) printf("Cmd queuing, ");
918 if (val[CAPAB_0] & OVLP_SUP) printf("Cmd overlap, ");
Eric Andersen3443bd72003-07-22 07:30:36 +0000919 }
Rob Landleyadde7982006-05-16 15:32:30 +0000920 if (val[CAPAB_0] & LBA_SUP) printf("LBA, ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000921
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000922 if (like_std != 1) {
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000923 printf("IORDY%s(can%s be disabled)\n",
924 !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "",
Rob Landleyadde7982006-05-16 15:32:30 +0000925 (val[CAPAB_0] & IORDY_OFF) ? "" :"not");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000926 } else
Glenn L McGrath07085852003-10-09 07:28:22 +0000927 printf("no IORDY\n");
928
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000929 if ((like_std == 1) && val[BUF_TYPE]) {
Rob Landleyadde7982006-05-16 15:32:30 +0000930 printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE],
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000931 (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector",
Rob Landleyadde7982006-05-16 15:32:30 +0000932 (val[BUF_TYPE] > 2) ? " with read caching ability" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +0000933 }
Rob Landleyadde7982006-05-16 15:32:30 +0000934
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000935 if ((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1))) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000936 printf("\tBuffer size: %.1fkB\n", (float)val[BUFFER__SIZE]/2);
Eric Andersen3443bd72003-07-22 07:30:36 +0000937 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000938 if ((min_std < 4) && (val[RW_LONG])) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000939 printf("\tbytes avail on r/w long: %u\n", val[RW_LONG]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000940 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000941 if ((eqpt != CDROM) && (like_std > 3)) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000942 printf("\tQueue depth: %u\n", (val[QUEUE_DEPTH] & DEPTH_BITS) + 1);
Eric Andersen3443bd72003-07-22 07:30:36 +0000943 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000944
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000945 if (dev == ATA_DEV) {
Rob Landleya3e4f382006-04-29 16:06:31 +0000946 if (like_std == 1)
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000947 printf("\tCan%s perform double-word IO\n", (!val[DWORD_IO]) ? "not" : "");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000948 else {
Rob Landleyadde7982006-05-16 15:32:30 +0000949 printf("\tStandby timer values: spec'd by %s", (val[CAPAB_0] & STD_STBY) ? "Standard" : "Vendor");
Rob Landleya3e4f382006-04-29 16:06:31 +0000950 if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000951 printf(", %s device specific minimum\n", (val[CAPAB_1] & MIN_STANDBY_TIMER) ? "with" : "no");
Glenn L McGrath07085852003-10-09 07:28:22 +0000952 else
Denis Vlasenko4daad902007-09-27 10:20:47 +0000953 bb_putchar('\n');
Eric Andersen3443bd72003-07-22 07:30:36 +0000954 }
955 printf("\tR/W multiple sector transfer: ");
Rob Landleya3e4f382006-04-29 16:06:31 +0000956 if ((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
Eric Andersen3443bd72003-07-22 07:30:36 +0000957 printf("not supported\n");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000958 else {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000959 printf("Max = %u\tCurrent = ", val[SECTOR_XFER_MAX] & SECTOR_XFER);
Rob Landleyadde7982006-05-16 15:32:30 +0000960 if (val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
961 printf("%u\n", val[SECTOR_XFER_CUR] & SECTOR_XFER);
962 else
963 printf("?\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000964 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000965 if ((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000966 /* We print out elsewhere whether the APM feature is enabled or
967 not. If it's not enabled, let's not repeat the info; just print
968 nothing here. */
Glenn L McGrath07085852003-10-09 07:28:22 +0000969 printf("\tAdvancedPM level: ");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000970 if ((val[ADV_PWR] & 0xFF00) == 0x4000) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000971 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
Eric Andersen3443bd72003-07-22 07:30:36 +0000972 printf("%u (0x%x)\n", apm_level, apm_level);
Glenn L McGrath07085852003-10-09 07:28:22 +0000973 }
974 else
Eric Andersen3443bd72003-07-22 07:30:36 +0000975 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000976 }
Rob Landleyadde7982006-05-16 15:32:30 +0000977 if (like_std > 5 && val[ACOUSTIC]) {
978 printf("\tRecommended acoustic management value: %u, current value: %u\n",
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000979 (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
Eric Andersen3443bd72003-07-22 07:30:36 +0000980 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000981 } else {
Glenn L McGrath07085852003-10-09 07:28:22 +0000982 /* ATAPI */
Rob Landleyadde7982006-05-16 15:32:30 +0000983 if (eqpt != CDROM && (val[CAPAB_0] & SWRST_REQ))
984 printf("\tATA sw reset required\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000985
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000986 if (val[PKT_REL] || val[SVC_NBSY]) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000987 printf("\tOverlap support:");
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000988 if (val[PKT_REL]) printf(" %uus to release bus.", val[PKT_REL]);
989 if (val[SVC_NBSY]) printf(" %uus to clear BSY after SERVICE cmd.", val[SVC_NBSY]);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000990 bb_putchar('\n');
Eric Andersen3443bd72003-07-22 07:30:36 +0000991 }
992 }
993
994 /* DMA stuff. Check that only one DMA mode is selected. */
995 printf("\tDMA: ");
Rob Landleya3e4f382006-04-29 16:06:31 +0000996 if (!(val[CAPAB_0] & DMA_SUP))
Eric Andersen3443bd72003-07-22 07:30:36 +0000997 printf("not supported\n");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000998 else {
Rob Landleyadde7982006-05-16 15:32:30 +0000999 if (val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001000 printf(" sdma%u\n", (val[DMA_MODE] & MODE) >> 8);
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001001 if (val[SINGLE_DMA]) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001002 jj = val[SINGLE_DMA];
1003 kk = val[SINGLE_DMA] >> 8;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001004 err_dma += mode_loop(jj, kk, 's', &have_mode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001005 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001006 if (val[MULTI_DMA]) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001007 jj = val[MULTI_DMA];
1008 kk = val[MULTI_DMA] >> 8;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001009 err_dma += mode_loop(jj, kk, 'm', &have_mode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001010 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001011 if ((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001012 jj = val[ULTRA_DMA];
1013 kk = val[ULTRA_DMA] >> 8;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001014 err_dma += mode_loop(jj, kk, 'u', &have_mode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001015 }
Rob Landleyadde7982006-05-16 15:32:30 +00001016 if (err_dma || !have_mode) printf("(?)");
Denis Vlasenko4daad902007-09-27 10:20:47 +00001017 bb_putchar('\n');
Eric Andersen3443bd72003-07-22 07:30:36 +00001018
Rob Landleyadde7982006-05-16 15:32:30 +00001019 if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
1020 printf("\t\tInterleaved DMA support\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001021
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001022 if ((val[WHATS_VALID] & OK_W64_70)
1023 && (val[DMA_TIME_MIN] || val[DMA_TIME_NORM])
1024 ) {
Rob Landleyadde7982006-05-16 15:32:30 +00001025 printf("\t\tCycle time:");
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001026 if (val[DMA_TIME_MIN]) printf(" min=%uns", val[DMA_TIME_MIN]);
1027 if (val[DMA_TIME_NORM]) printf(" recommended=%uns", val[DMA_TIME_NORM]);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001028 bb_putchar('\n');
Eric Andersen3443bd72003-07-22 07:30:36 +00001029 }
1030 }
1031
1032 /* Programmed IO stuff */
1033 printf("\tPIO: ");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001034 /* If a drive supports mode n (e.g. 3), it also supports all modes less
Eric Andersen3443bd72003-07-22 07:30:36 +00001035 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001036 if ((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP)) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001037 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001038 for (ii = 0; ii <= PIO_MODE_MAX; ii++) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001039 if (jj & 0x0001) printf("pio%d ", ii);
Eric Andersen3443bd72003-07-22 07:30:36 +00001040 jj >>=1;
1041 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001042 bb_putchar('\n');
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001043 } else if (((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE)) {
Rob Landleya3e4f382006-04-29 16:06:31 +00001044 for (ii = 0; ii <= val[PIO_MODE]>>8; ii++)
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001045 printf("pio%d ", ii);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001046 bb_putchar('\n');
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001047 } else
Denis Vlasenko91303402007-10-30 19:36:54 +00001048 puts("unknown");
Glenn L McGrath07085852003-10-09 07:28:22 +00001049
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001050 if (val[WHATS_VALID] & OK_W64_70) {
1051 if (val[PIO_NO_FLOW] || val[PIO_FLOW]) {
Rob Landleyadde7982006-05-16 15:32:30 +00001052 printf("\t\tCycle time:");
1053 if (val[PIO_NO_FLOW]) printf(" no flow control=%uns", val[PIO_NO_FLOW]);
1054 if (val[PIO_FLOW]) printf(" IORDY flow control=%uns", val[PIO_FLOW]);
Denis Vlasenko4daad902007-09-27 10:20:47 +00001055 bb_putchar('\n');
Eric Andersen3443bd72003-07-22 07:30:36 +00001056 }
1057 }
1058
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001059 if ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001060 printf("Commands/features:\n\tEnabled\tSupported:\n");
1061 jj = val[CMDS_SUPP_0];
1062 kk = val[CMDS_EN_0];
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001063 for (ii = 0; ii < NUM_CMD_FEAT_STR; ii++) {
Denis Vlasenkobfc3d822007-11-04 04:10:17 +00001064 const char *feat_str = nth_string(cmd_feat_str, ii);
Denis Vlasenko91303402007-10-30 19:36:54 +00001065 if ((jj & 0x8000) && (*feat_str != '\0')) {
1066 printf("\t%s\t%s\n", (kk & 0x8000) ? " *" : "", feat_str);
Eric Andersen3443bd72003-07-22 07:30:36 +00001067 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001068 jj <<= 1;
1069 kk <<= 1;
1070 if (ii % 16 == 15) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001071 jj = val[CMDS_SUPP_0+1+(ii/16)];
1072 kk = val[CMDS_EN_0+1+(ii/16)];
1073 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001074 if (ii == 31) {
Rob Landleya3e4f382006-04-29 16:06:31 +00001075 if ((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
Glenn L McGrath07085852003-10-09 07:28:22 +00001076 ii +=16;
Eric Andersen3443bd72003-07-22 07:30:36 +00001077 }
1078 }
1079 }
Rob Landleyadde7982006-05-16 15:32:30 +00001080 /* Removable Media Status Notification feature set */
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001081 if ((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
Denis Vlasenkobfc3d822007-11-04 04:10:17 +00001082 printf("\t%s supported\n", nth_string(cmd_feat_str, 27));
Glenn L McGrath07085852003-10-09 07:28:22 +00001083
Eric Andersen3443bd72003-07-22 07:30:36 +00001084 /* security */
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001085 if ((eqpt != CDROM) && (like_std > 3)
1086 && (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME])
1087 ) {
Rob Landleyade7f952006-05-25 18:53:06 +00001088 printf("Security:\n");
Rob Landleyadde7982006-05-16 15:32:30 +00001089 if (val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001090 printf("\tMaster password revision code = %u\n", val[PSWD_CODE]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001091 jj = val[SECU_STATUS];
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001092 if (jj) {
1093 for (ii = 0; ii < NUM_SECU_STR; ii++) {
Denis Vlasenkobfc3d822007-11-04 04:10:17 +00001094 printf("\t%s\t%s\n", (!(jj & 0x0001)) ? "not" : "", nth_string(secu_str, ii));
Eric Andersen3443bd72003-07-22 07:30:36 +00001095 jj >>=1;
1096 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001097 if (val[SECU_STATUS] & SECU_ENABLED) {
Rob Landleyadde7982006-05-16 15:32:30 +00001098 printf("\tSecurity level %s\n", (val[SECU_STATUS] & SECU_LEVEL) ? "maximum" : "high");
Eric Andersen3443bd72003-07-22 07:30:36 +00001099 }
1100 }
1101 jj = val[ERASE_TIME] & ERASE_BITS;
1102 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001103 if (jj || kk) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001104 printf("\t");
Rob Landleyade7f952006-05-25 18:53:06 +00001105 if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, "");
1106 if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED ");
Denis Vlasenko4daad902007-09-27 10:20:47 +00001107 bb_putchar('\n');
Eric Andersen3443bd72003-07-22 07:30:36 +00001108 }
1109 }
1110
1111 /* reset result */
Rob Landleyadde7982006-05-16 15:32:30 +00001112 jj = val[HWRST_RSLT];
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001113 if ((jj & VALID) == VALID_VAL) {
Denis Vlasenko6bef3d12007-11-06 03:05:54 +00001114 oo = (jj & RST0);
1115 if (!oo)
Rob Landleyadde7982006-05-16 15:32:30 +00001116 jj >>= 8;
Rob Landleya3e4f382006-04-29 16:06:31 +00001117 if ((jj & DEV_DET) == JUMPER_VAL)
Rob Landleyadde7982006-05-16 15:32:30 +00001118 strng = " determined by the jumper";
Rob Landleya3e4f382006-04-29 16:06:31 +00001119 else if ((jj & DEV_DET) == CSEL_VAL)
Rob Landleyadde7982006-05-16 15:32:30 +00001120 strng = " determined by CSEL";
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001121 else
Rob Landleyadde7982006-05-16 15:32:30 +00001122 strng = "";
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001123 printf("HW reset results:\n\tCBLID- %s Vih\n\tDevice num = %i%s\n",
Rob Landleyadde7982006-05-16 15:32:30 +00001124 (val[HWRST_RSLT] & CBLID) ? "above" : "below", !(oo), strng);
Eric Andersen3443bd72003-07-22 07:30:36 +00001125 }
1126
1127 /* more stuff from std 5 */
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001128 if ((like_std > 4) && (eqpt != CDROM)) {
1129 if (val[CFA_PWR_MODE] & VALID_W160) {
Rob Landleyadde7982006-05-16 15:32:30 +00001130 printf("CFA power mode 1:\n\t%s%s\n", (val[CFA_PWR_MODE] & PWR_MODE_OFF) ? "disabled" : "enabled",
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001131 (val[CFA_PWR_MODE] & PWR_MODE_REQ) ? " and required by some commands" : "");
Glenn L McGrath07085852003-10-09 07:28:22 +00001132
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001133 if (val[CFA_PWR_MODE] & MAX_AMPS)
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001134 printf("\tMaximum current = %uma\n", val[CFA_PWR_MODE] & MAX_AMPS);
Eric Andersen3443bd72003-07-22 07:30:36 +00001135 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001136 if ((val[INTEGRITY] & SIG) == SIG_VAL) {
Rob Landleyadde7982006-05-16 15:32:30 +00001137 printf("Checksum: %scorrect\n", chksum ? "in" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +00001138 }
1139 }
1140
Rob Landleyadde7982006-05-16 15:32:30 +00001141 exit(EXIT_SUCCESS);
Eric Andersen3443bd72003-07-22 07:30:36 +00001142}
1143#endif
1144
Eric Andersen3443bd72003-07-22 07:30:36 +00001145// Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1146// then the HDIO_GET_IDENTITY only returned 142 bytes.
1147// Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1148// and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1149// 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1150// (which they should, but they should just return -EINVAL).
1151//
1152// So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1153// On a really old system, it will not, and we will be confused.
1154// Too bad, really.
1155
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001156#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Denis Vlasenko91303402007-10-30 19:36:54 +00001157static const char cfg_str[] ALIGN1 =
Denis Vlasenko6b404432008-01-07 16:13:14 +00001158 """\0" "HardSect""\0" "SoftSect""\0" "NotMFM""\0"
1159 "HdSw>15uSec""\0" "SpinMotCtl""\0" "Fixed""\0" "Removeable""\0"
1160 "DTR<=5Mbs""\0" "DTR>5Mbs""\0" "DTR>10Mbs""\0" "RotSpdTol>.5%""\0"
1161 "dStbOff""\0" "TrkOff""\0" "FmtGapReq""\0" "nonMagnetic"
Denis Vlasenko91303402007-10-30 19:36:54 +00001162;
Eric Andersen3443bd72003-07-22 07:30:36 +00001163
Denis Vlasenko91303402007-10-30 19:36:54 +00001164static const char BuffType[] ALIGN1 =
1165 "unknown""\0" "1Sect""\0" "DualPort""\0" "DualPortCache"
1166;
Eric Andersen3443bd72003-07-22 07:30:36 +00001167
Rob Landleya3e4f382006-04-29 16:06:31 +00001168static void dump_identity(const struct hd_driveid *id)
Eric Andersen3443bd72003-07-22 07:30:36 +00001169{
1170 int i;
Denis Vlasenko284d0fa2008-02-16 13:18:17 +00001171 const unsigned short *id_regs = (const void*) id;
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001172
Glenn L McGrath07085852003-10-09 07:28:22 +00001173 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1174 id->model, id->fw_rev, id->serial_no);
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001175 for (i = 0; i <= 15; i++) {
Rob Landleyade7f952006-05-25 18:53:06 +00001176 if (id->config & (1<<i))
Denis Vlasenkobfc3d822007-11-04 04:10:17 +00001177 printf(" %s", nth_string(cfg_str, i));
Rob Landleyadde7982006-05-16 15:32:30 +00001178 }
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001179 printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n"
Rob Landleyadde7982006-05-16 15:32:30 +00001180 " BuffType=(%u) %s, BuffSize=%ukB, MaxMultSect=%u",
Glenn L McGrath07085852003-10-09 07:28:22 +00001181 id->cyls, id->heads, id->sectors, id->track_bytes,
Rob Landleyadde7982006-05-16 15:32:30 +00001182 id->sector_bytes, id->ecc_bytes,
Denis Vlasenkobfc3d822007-11-04 04:10:17 +00001183 id->buf_type, nth_string(BuffType, (id->buf_type > 3) ? 0 : id->buf_type),
Rob Landleyadde7982006-05-16 15:32:30 +00001184 id->buf_size/2, id->max_multsect);
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001185 if (id->max_multsect) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001186 printf(", MultSect=");
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001187 if (!(id->multsect_valid & 1))
Eric Andersen3443bd72003-07-22 07:30:36 +00001188 printf("?%u?", id->multsect);
1189 else if (id->multsect)
1190 printf("%u", id->multsect);
1191 else
1192 printf("off");
1193 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001194 bb_putchar('\n');
Rob Landleyade7f952006-05-25 18:53:06 +00001195
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001196 if (!(id->field_valid & 1))
Rob Landleyade7f952006-05-25 18:53:06 +00001197 printf(" (maybe):");
1198
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001199 printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s", id->cur_cyls, id->cur_heads,
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001200 id->cur_sectors,
1201 (BB_BIG_ENDIAN) ?
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001202 (unsigned long)(id->cur_capacity0 << 16) | id->cur_capacity1 :
1203 (unsigned long)(id->cur_capacity1 << 16) | id->cur_capacity0,
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001204 ((id->capability&2) == 0) ? "no" : "yes");
Rob Landleyade7f952006-05-25 18:53:06 +00001205
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001206 if (id->capability & 2)
Rob Landleyade7f952006-05-25 18:53:06 +00001207 printf(", LBAsects=%u", id->lba_capacity);
1208
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001209 printf("\n IORDY=%s", (id->capability & 8) ? (id->capability & 4) ? "on/off" : "yes" : "no");
Rob Landleyade7f952006-05-25 18:53:06 +00001210
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001211 if (((id->capability & 8) || (id->field_valid & 2)) && (id->field_valid & 2))
Rob Landleyade7f952006-05-25 18:53:06 +00001212 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001213
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001214 if ((id->capability & 1) && (id->field_valid & 2))
Rob Landleyade7f952006-05-25 18:53:06 +00001215 printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1216
1217 printf("\n PIO modes: ");
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001218 if (id->tPIO <= 5) {
Rob Landleyade7f952006-05-25 18:53:06 +00001219 printf("pio0 ");
1220 if (id->tPIO >= 1) printf("pio1 ");
1221 if (id->tPIO >= 2) printf("pio2 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001222 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001223 if (id->field_valid & 2) {
Rob Landleyade7f952006-05-25 18:53:06 +00001224 if (id->eide_pio_modes & 1) printf("pio3 ");
1225 if (id->eide_pio_modes & 2) printf("pio4 ");
1226 if (id->eide_pio_modes &~3) printf("pio? ");
1227 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001228 if (id->capability & 1) {
1229 if (id->dma_1word | id->dma_mword) {
Rob Landleyade7f952006-05-25 18:53:06 +00001230 printf("\n DMA modes: ");
1231 if (id->dma_1word & 0x100) printf("*");
1232 if (id->dma_1word & 1) printf("sdma0 ");
1233 if (id->dma_1word & 0x200) printf("*");
1234 if (id->dma_1word & 2) printf("sdma1 ");
1235 if (id->dma_1word & 0x400) printf("*");
1236 if (id->dma_1word & 4) printf("sdma2 ");
1237 if (id->dma_1word & 0xf800) printf("*");
1238 if (id->dma_1word & 0xf8) printf("sdma? ");
1239 if (id->dma_mword & 0x100) printf("*");
1240 if (id->dma_mword & 1) printf("mdma0 ");
1241 if (id->dma_mword & 0x200) printf("*");
1242 if (id->dma_mword & 2) printf("mdma1 ");
1243 if (id->dma_mword & 0x400) printf("*");
1244 if (id->dma_mword & 4) printf("mdma2 ");
1245 if (id->dma_mword & 0xf800) printf("*");
1246 if (id->dma_mword & 0xf8) printf("mdma? ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001247 }
1248 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001249 if (((id->capability & 8) || (id->field_valid & 2)) && id->field_valid & 4) {
Rob Landleyade7f952006-05-25 18:53:06 +00001250 printf("\n UDMA modes: ");
1251 if (id->dma_ultra & 0x100) printf("*");
1252 if (id->dma_ultra & 0x001) printf("udma0 ");
1253 if (id->dma_ultra & 0x200) printf("*");
1254 if (id->dma_ultra & 0x002) printf("udma1 ");
1255 if (id->dma_ultra & 0x400) printf("*");
1256 if (id->dma_ultra & 0x004) printf("udma2 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001257#ifdef __NEW_HD_DRIVE_ID
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001258 if (id->hw_config & 0x2000) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001259#else /* !__NEW_HD_DRIVE_ID */
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001260 if (id->word93 & 0x2000) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001261#endif /* __NEW_HD_DRIVE_ID */
Rob Landleyade7f952006-05-25 18:53:06 +00001262 if (id->dma_ultra & 0x0800) printf("*");
1263 if (id->dma_ultra & 0x0008) printf("udma3 ");
1264 if (id->dma_ultra & 0x1000) printf("*");
1265 if (id->dma_ultra & 0x0010) printf("udma4 ");
1266 if (id->dma_ultra & 0x2000) printf("*");
1267 if (id->dma_ultra & 0x0020) printf("udma5 ");
1268 if (id->dma_ultra & 0x4000) printf("*");
1269 if (id->dma_ultra & 0x0040) printf("udma6 ");
1270 if (id->dma_ultra & 0x8000) printf("*");
1271 if (id->dma_ultra & 0x0080) printf("udma7 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001272 }
1273 }
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001274 printf("\n AdvancedPM=%s", (!(id_regs[83] & 8)) ? "no" : "yes");
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001275 if (id_regs[83] & 8) {
1276 if (!(id_regs[86] & 8))
Glenn L McGrath07085852003-10-09 07:28:22 +00001277 printf(": disabled (255)");
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001278 else if ((id_regs[91] & 0xFF00) != 0x4000)
Glenn L McGrath07085852003-10-09 07:28:22 +00001279 printf(": unknown setting");
1280 else
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001281 printf(": mode=0x%02X (%u)", id_regs[91] & 0xFF, id_regs[91] & 0xFF);
Glenn L McGrath07085852003-10-09 07:28:22 +00001282 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001283 if (id_regs[82] & 0x20)
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001284 printf(" WriteCache=%s", (id_regs[85] & 0x20) ? "enabled" : "disabled");
Glenn L McGrath07085852003-10-09 07:28:22 +00001285#ifdef __NEW_HD_DRIVE_ID
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001286 if ((id->minor_rev_num && id->minor_rev_num <= 31)
1287 || (id->major_rev_num && id->minor_rev_num <= 31)
1288 ) {
Denis Vlasenkobfc3d822007-11-04 04:10:17 +00001289 printf("\n Drive conforms to: %s: ", (id->minor_rev_num <= 31) ? nth_string(minor_str, id->minor_rev_num) : "unknown");
Rob Landley026147a2006-04-17 22:29:13 +00001290 if (id->major_rev_num != 0x0000 && /* NOVAL_0 */
1291 id->major_rev_num != 0xFFFF) { /* NOVAL_1 */
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001292 for (i = 0; i <= 15; i++) {
Rob Landleyadde7982006-05-16 15:32:30 +00001293 if (id->major_rev_num & (1<<i))
1294 printf(" ATA/ATAPI-%u", i);
1295 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001296 }
1297 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001298#endif /* __NEW_HD_DRIVE_ID */
Rob Landleyade7f952006-05-25 18:53:06 +00001299 printf("\n\n * current active mode\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001300}
1301#endif
1302
Denis Vlasenko892536f2007-09-27 10:23:34 +00001303static void flush_buffer_cache(/*int fd*/ void)
Eric Andersen3443bd72003-07-22 07:30:36 +00001304{
Rob Landleya3e4f382006-04-29 16:06:31 +00001305 fsync(fd); /* flush buffers */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001306 ioctl_or_warn(fd, BLKFLSBUF, NULL); /* do it again, big time */
Eric Andersen3443bd72003-07-22 07:30:36 +00001307#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001308 sleep(1);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001309 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) { /* await completion */
1310 if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
1311 bb_perror_msg("HDIO_DRIVE_CMD");
1312 else
1313 bb_perror_msg("ioctl %#x failed", HDIO_DRIVE_CMD);
1314 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001315#endif
1316}
1317
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +00001318static void seek_to_zero(/*int fd*/ void)
Eric Andersen3443bd72003-07-22 07:30:36 +00001319{
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +00001320 xlseek(fd, (off_t) 0, SEEK_SET);
Eric Andersen3443bd72003-07-22 07:30:36 +00001321}
1322
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001323static void read_big_block(/*int fd,*/ char *buf)
Eric Andersen3443bd72003-07-22 07:30:36 +00001324{
Rob Landleyadde7982006-05-16 15:32:30 +00001325 int i;
1326
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001327 xread(fd, buf, TIMING_BUF_BYTES);
Eric Andersen3443bd72003-07-22 07:30:36 +00001328 /* access all sectors of buf to ensure the read fully completed */
1329 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1330 buf[i] &= 1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001331}
1332
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001333static unsigned dev_size_mb(/*int fd*/ void)
Rob Landleyadde7982006-05-16 15:32:30 +00001334{
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +00001335 union {
1336 unsigned long long blksize64;
1337 unsigned blksize32;
1338 } u;
Rob Landleyadde7982006-05-16 15:32:30 +00001339
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001340 if (0 == ioctl(fd, BLKGETSIZE64, &u.blksize64)) { // bytes
1341 u.blksize64 /= (1024 * 1024);
1342 } else {
1343 xioctl(fd, BLKGETSIZE, &u.blksize32); // sectors
1344 u.blksize64 = u.blksize32 / (2 * 1024);
Rob Landleyadde7982006-05-16 15:32:30 +00001345 }
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001346 if (u.blksize64 > UINT_MAX)
1347 return UINT_MAX;
1348 return u.blksize64;
Rob Landleyadde7982006-05-16 15:32:30 +00001349}
Eric Andersen50af12d2003-08-06 08:47:59 +00001350
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001351static void print_timing(unsigned m, unsigned elapsed_us)
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001352{
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001353 unsigned sec = elapsed_us / 1000000;
1354 unsigned hs = (elapsed_us % 1000000) / 10000;
1355
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001356 printf("%5u MB in %u.%02u seconds = %u kB/s\n",
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001357 m, sec, hs,
Denis Vlasenko0afdfdf2007-09-28 13:41:41 +00001358 /* "| 1" prevents div-by-0 */
1359 (unsigned) ((unsigned long long)m * (1024 * 1000000) / (elapsed_us | 1))
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001360 // ~= (m * 1024) / (elapsed_us / 1000000)
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001361 // = kb / elapsed_sec
1362 );
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001363}
1364
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001365static void do_time(int cache /*,int fd*/)
1366/* cache=1: time cache: repeatedly read N MB at offset 0
1367 * cache=0: time device: linear read, starting at offset 0
1368 */
Eric Andersen3443bd72003-07-22 07:30:36 +00001369{
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001370 unsigned max_iterations, iterations;
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001371 unsigned start; /* doesn't need to be long long */
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001372 unsigned elapsed, elapsed2;
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001373 unsigned total_MB;
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +00001374 char *buf = xmalloc(TIMING_BUF_BYTES);
Rob Landley4ae2f512006-05-19 17:24:26 +00001375
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001376 if (mlock(buf, TIMING_BUF_BYTES))
1377 bb_perror_msg_and_die("mlock");
Rob Landleyadde7982006-05-16 15:32:30 +00001378
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001379 /* Clear out the device request queues & give them time to complete.
1380 * NB: *small* delay. User is expected to have a clue and to not run
1381 * heavy io in parallel with measurements. */
Rob Landleyadde7982006-05-16 15:32:30 +00001382 sync();
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001383 sleep(1);
1384 if (cache) { /* Time cache */
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +00001385 seek_to_zero();
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001386 read_big_block(buf);
1387 printf("Timing buffer-cache reads: ");
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001388 } else { /* Time device */
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001389 printf("Timing buffered disk reads:");
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001390 }
1391 fflush(stdout);
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001392
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001393 /* Now do the timing */
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001394 iterations = 0;
Denis Vlasenkoa7bc9ac2007-09-28 11:21:47 +00001395 /* Max time to run (small for cache, avoids getting
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001396 * huge total_MB which can overlow unsigned type) */
Denis Vlasenkoa7bc9ac2007-09-28 11:21:47 +00001397 elapsed2 = 510000; /* cache */
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001398 max_iterations = UINT_MAX;
1399 if (!cache) {
Denis Vlasenkoa7bc9ac2007-09-28 11:21:47 +00001400 elapsed2 = 3000000; /* not cache */
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001401 /* Don't want to read past the end! */
1402 max_iterations = dev_size_mb() / TIMING_BUF_MB;
1403 }
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001404 start = monotonic_us();
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001405 do {
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001406 if (cache)
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +00001407 seek_to_zero();
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001408 read_big_block(buf);
1409 elapsed = (unsigned)monotonic_us() - start;
1410 ++iterations;
Denis Vlasenkoa7bc9ac2007-09-28 11:21:47 +00001411 } while (elapsed < elapsed2 && iterations < max_iterations);
Denis Vlasenko920c52a2007-09-28 13:38:08 +00001412 total_MB = iterations * TIMING_BUF_MB;
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001413 //printf(" elapsed:%u iterations:%u ", elapsed, iterations);
1414 if (cache) {
Denis Vlasenkoa7bc9ac2007-09-28 11:21:47 +00001415 /* Cache: remove lseek() and monotonic_us() overheads
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001416 * from elapsed */
1417 start = monotonic_us();
Rob Landleyadde7982006-05-16 15:32:30 +00001418 do {
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +00001419 seek_to_zero();
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001420 elapsed2 = (unsigned)monotonic_us() - start;
Rob Landleyadde7982006-05-16 15:32:30 +00001421 } while (--iterations);
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001422 //printf(" elapsed2:%u ", elapsed2);
Rob Landleyadde7982006-05-16 15:32:30 +00001423 elapsed -= elapsed2;
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001424 total_MB *= 2; // BUFCACHE_FACTOR (why?)
Denis Vlasenko892536f2007-09-27 10:23:34 +00001425 flush_buffer_cache();
Glenn L McGrath07085852003-10-09 07:28:22 +00001426 }
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001427 print_timing(total_MB, elapsed);
Rob Landley4ae2f512006-05-19 17:24:26 +00001428 munlock(buf, TIMING_BUF_BYTES);
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +00001429 free(buf);
Eric Andersen3443bd72003-07-22 07:30:36 +00001430}
1431
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001432#if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001433static void bus_state_value(unsigned value)
Eric Andersen3443bd72003-07-22 07:30:36 +00001434{
Rob Landleyadde7982006-05-16 15:32:30 +00001435 if (value == BUSSTATE_ON)
1436 on_off(1);
1437 else if (value == BUSSTATE_OFF)
1438 on_off(0);
1439 else if (value == BUSSTATE_TRISTATE)
1440 printf(" (tristate)\n");
1441 else
1442 printf(" (unknown: %d)\n", value);
Eric Andersen3443bd72003-07-22 07:30:36 +00001443}
1444#endif
1445
1446#ifdef HDIO_DRIVE_CMD
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001447static void interpret_standby(unsigned standby)
Eric Andersen3443bd72003-07-22 07:30:36 +00001448{
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001449 unsigned t;
Rob Landleyadde7982006-05-16 15:32:30 +00001450
Rob Landley403777f2006-08-03 20:22:37 +00001451 printf(" (");
Rob Landleyadde7982006-05-16 15:32:30 +00001452 if (standby == 0)
1453 printf("off");
1454 else if (standby == 252)
1455 printf("21 minutes");
1456 else if (standby == 253)
1457 printf("vendor-specific");
1458 else if (standby == 254)
Denis Vlasenko91303402007-10-30 19:36:54 +00001459 printf("reserved");
Rob Landleyadde7982006-05-16 15:32:30 +00001460 else if (standby == 255)
1461 printf("21 minutes + 15 seconds");
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001462 else if (standby <= 240) {
1463 t = standby * 5;
1464 printf("%u minutes + %u seconds", t / 60, t % 60);
1465 } else if (standby <= 251) {
1466 t = (standby - 240) * 30;
1467 printf("%u hours + %u minutes", t / 60, t % 60);
1468 } else
1469 printf("illegal value");
Eric Andersen3443bd72003-07-22 07:30:36 +00001470 printf(")\n");
1471}
1472
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +00001473static const uint8_t xfermode_val[] ALIGN1 = {
1474 8, 9, 10, 11, 12, 13, 14, 15,
1475 16, 17, 18, 19, 20, 21, 22, 23,
1476 32, 33, 34, 35, 36, 37, 38, 39,
1477 64, 65, 66, 67, 68, 69, 70, 71
1478};
1479/* NB: we save size by _not_ storing terninating NUL! */
1480static const char xfermode_name[][5] ALIGN1 = {
1481 "pio0", "pio1", "pio2", "pio3", "pio4", "pio5", "pio6", "pio7",
1482 "sdma0","sdma1","sdma2","sdma3","sdma4","sdma5","sdma6","sdma7",
1483 "mdma0","mdma1","mdma2","mdma3","mdma4","mdma5","mdma6","mdma7",
1484 "udma0","udma1","udma2","udma3","udma4","udma5","udma6","udma7"
Eric Andersen3443bd72003-07-22 07:30:36 +00001485};
1486
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +00001487static int translate_xfermode(const char *name)
Eric Andersen3443bd72003-07-22 07:30:36 +00001488{
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +00001489 int val, i;
Eric Andersen3443bd72003-07-22 07:30:36 +00001490
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +00001491 for (i = 0; i < ARRAY_SIZE(xfermode_val); i++) {
1492 if (!strncmp(name, xfermode_name[i], 5))
1493 if (strlen(name) <= 5)
1494 return xfermode_val[i];
Glenn L McGrath07085852003-10-09 07:28:22 +00001495 }
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +00001496 /* Negative numbers are invalid and are caught later */
1497 val = bb_strtoi(name, NULL, 10);
1498 if (!errno)
Glenn L McGrath07085852003-10-09 07:28:22 +00001499 return val;
Glenn L McGrath07085852003-10-09 07:28:22 +00001500 return -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001501}
1502
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001503static void interpret_xfermode(unsigned xfermode)
Eric Andersen3443bd72003-07-22 07:30:36 +00001504{
1505 printf(" (");
Rob Landleyadde7982006-05-16 15:32:30 +00001506 if (xfermode == 0)
1507 printf("default PIO mode");
1508 else if (xfermode == 1)
1509 printf("default PIO mode, disable IORDY");
1510 else if (xfermode >= 8 && xfermode <= 15)
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +00001511 printf("PIO flow control mode%u", xfermode - 8);
Rob Landleyadde7982006-05-16 15:32:30 +00001512 else if (xfermode >= 16 && xfermode <= 23)
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +00001513 printf("singleword DMA mode%u", xfermode - 16);
Rob Landleyadde7982006-05-16 15:32:30 +00001514 else if (xfermode >= 32 && xfermode <= 39)
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +00001515 printf("multiword DMA mode%u", xfermode - 32);
Rob Landleyadde7982006-05-16 15:32:30 +00001516 else if (xfermode >= 64 && xfermode <= 71)
Denis Vlasenkoa0319ba2007-08-16 10:37:49 +00001517 printf("UltraDMA mode%u", xfermode - 64);
Rob Landleyadde7982006-05-16 15:32:30 +00001518 else
Denis Vlasenko91303402007-10-30 19:36:54 +00001519 printf("unknown");
Eric Andersen3443bd72003-07-22 07:30:36 +00001520 printf(")\n");
1521}
1522#endif /* HDIO_DRIVE_CMD */
1523
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001524static void print_flag(int flag, const char *s, unsigned long value)
Rob Landleyadde7982006-05-16 15:32:30 +00001525{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001526 if (flag)
Rob Landleyadde7982006-05-16 15:32:30 +00001527 printf(" setting %s to %ld\n", s, value);
1528}
1529
Rob Landleya3e4f382006-04-29 16:06:31 +00001530static void process_dev(char *devname)
Eric Andersen3443bd72003-07-22 07:30:36 +00001531{
Denis Vlasenko892536f2007-09-27 10:23:34 +00001532 /*int fd;*/
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001533 long parm, multcount;
Eric Andersen3443bd72003-07-22 07:30:36 +00001534#ifndef HDIO_DRIVE_CMD
1535 int force_operation = 0;
1536#endif
Rob Landley39cf6452006-05-05 16:52:28 +00001537 /* Please restore args[n] to these values after each ioctl
1538 except for args[2] */
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001539 unsigned char args[4] = { WIN_SETFEATURES, 0, 0, 0 };
Rob Landleyadde7982006-05-16 15:32:30 +00001540 const char *fmt = " %s\t= %2ld";
Rob Landleye5b281f2006-04-29 15:49:18 +00001541
Denis Vlasenko892536f2007-09-27 10:23:34 +00001542 /*fd = xopen(devname, O_RDONLY | O_NONBLOCK);*/
1543 xmove_fd(xopen(devname, O_RDONLY | O_NONBLOCK), fd);
Rob Landleyade7f952006-05-25 18:53:06 +00001544 printf("\n%s:\n", devname);
Eric Andersen3443bd72003-07-22 07:30:36 +00001545
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001546 if (set_readahead) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001547 print_flag(get_readahead, "fs readahead", Xreadahead);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001548 ioctl_or_warn(fd, BLKRASET, (int *)Xreadahead);
Eric Andersen3443bd72003-07-22 07:30:36 +00001549 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001550#if ENABLE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1551 if (unregister_hwif) {
Rob Landley20deab02006-05-07 23:34:15 +00001552 printf(" attempting to unregister hwif#%lu\n", hwif);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001553 ioctl_or_warn(fd, HDIO_UNREGISTER_HWIF, (int *)(unsigned long)hwif);
Eric Andersen3443bd72003-07-22 07:30:36 +00001554 }
1555#endif
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001556#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
1557 if (scan_hwif) {
Rob Landley20deab02006-05-07 23:34:15 +00001558 printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq);
Eric Andersen3443bd72003-07-22 07:30:36 +00001559 args[0] = hwif_data;
1560 args[1] = hwif_ctrl;
1561 args[2] = hwif_irq;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001562 ioctl_or_warn(fd, HDIO_SCAN_HWIF, args);
Rob Landley39cf6452006-05-05 16:52:28 +00001563 args[0] = WIN_SETFEATURES;
1564 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001565 }
1566#endif
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001567 if (set_piomode) {
1568 if (noisy_piomode) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001569 printf(" attempting to ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001570 if (piomode == 255)
Glenn L McGrath07085852003-10-09 07:28:22 +00001571 printf("auto-tune PIO mode\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001572 else if (piomode < 100)
Glenn L McGrath07085852003-10-09 07:28:22 +00001573 printf("set PIO mode to %d\n", piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001574 else if (piomode < 200)
Glenn L McGrath07085852003-10-09 07:28:22 +00001575 printf("set MDMA mode to %d\n", (piomode-100));
Eric Andersen3443bd72003-07-22 07:30:36 +00001576 else
Glenn L McGrath07085852003-10-09 07:28:22 +00001577 printf("set UDMA mode to %d\n", (piomode-200));
Eric Andersen3443bd72003-07-22 07:30:36 +00001578 }
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001579 ioctl_or_warn(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001580 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001581 if (set_io32bit) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001582 print_flag(get_io32bit, "32-bit IO_support flag", io32bit);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001583 ioctl_or_warn(fd, HDIO_SET_32BIT, (int *)io32bit);
Eric Andersen3443bd72003-07-22 07:30:36 +00001584 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001585 if (set_mult) {
Rob Landleyadde7982006-05-16 15:32:30 +00001586 print_flag(get_mult, "multcount", mult);
Rob Landleyade7f952006-05-25 18:53:06 +00001587#ifdef HDIO_DRIVE_CMD
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001588 ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult);
Rob Landleyade7f952006-05-25 18:53:06 +00001589#else
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001590 force_operation |= (!ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult));
Eric Andersen3443bd72003-07-22 07:30:36 +00001591#endif
1592 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001593 if (set_readonly) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001594 print_flag_on_off(get_readonly, "readonly", readonly);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001595 ioctl_or_warn(fd, BLKROSET, &readonly);
Eric Andersen3443bd72003-07-22 07:30:36 +00001596 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001597 if (set_unmask) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001598 print_flag_on_off(get_unmask, "unmaskirq", unmask);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001599 ioctl_or_warn(fd, HDIO_SET_UNMASKINTR, (int *)unmask);
Eric Andersen3443bd72003-07-22 07:30:36 +00001600 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001601#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
1602 if (set_dma) {
Rob Landleyadde7982006-05-16 15:32:30 +00001603 print_flag_on_off(get_dma, "using_dma", dma);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001604 ioctl_or_warn(fd, HDIO_SET_DMA, (int *)dma);
Eric Andersen3443bd72003-07-22 07:30:36 +00001605 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001606#endif /* FEATURE_HDPARM_HDIO_GETSET_DMA */
1607 if (set_dma_q) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001608 print_flag_on_off(get_dma_q, "DMA queue_depth", dma_q);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001609 ioctl_or_warn(fd, HDIO_SET_QDMA, (int *)dma_q);
Eric Andersen3443bd72003-07-22 07:30:36 +00001610 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001611 if (set_nowerr) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001612 print_flag_on_off(get_nowerr, "nowerr", nowerr);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001613 ioctl_or_warn(fd, HDIO_SET_NOWERR, (int *)nowerr);
Eric Andersen3443bd72003-07-22 07:30:36 +00001614 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001615 if (set_keep) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001616 print_flag_on_off(get_keep, "keep_settings", keep);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001617 ioctl_or_warn(fd, HDIO_SET_KEEPSETTINGS, (int *)keep);
Eric Andersen3443bd72003-07-22 07:30:36 +00001618 }
1619#ifdef HDIO_DRIVE_CMD
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001620 if (set_doorlock) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001621 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
Rob Landley39cf6452006-05-05 16:52:28 +00001622 args[2] = 0;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001623 print_flag_on_off(get_doorlock, "drive doorlock", doorlock);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001624 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Rob Landley39cf6452006-05-05 16:52:28 +00001625 args[0] = WIN_SETFEATURES;
Eric Andersen3443bd72003-07-22 07:30:36 +00001626 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001627 if (set_dkeep) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001628 /* lock/unlock the drive's "feature" settings */
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001629 print_flag_on_off(get_dkeep, "drive keep features", dkeep);
Eric Andersen3443bd72003-07-22 07:30:36 +00001630 args[2] = dkeep ? 0x66 : 0xcc;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001631 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001632 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001633 if (set_defects) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001634 args[2] = defects ? 0x04 : 0x84;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001635 print_flag(get_defects, "drive defect-mgmt", defects);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001636 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001637 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001638 if (set_prefetch) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001639 args[1] = prefetch;
Rob Landleye5b281f2006-04-29 15:49:18 +00001640 args[2] = 0xab;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001641 print_flag(get_prefetch, "drive prefetch", prefetch);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001642 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Rob Landley39cf6452006-05-05 16:52:28 +00001643 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001644 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001645 if (set_xfermode) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001646 args[1] = xfermode_requested;
Rob Landleye5b281f2006-04-29 15:49:18 +00001647 args[2] = 3;
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001648 if (get_xfermode) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001649 print_flag(1, "xfermode", xfermode_requested);
Eric Andersen3443bd72003-07-22 07:30:36 +00001650 interpret_xfermode(xfermode_requested);
1651 }
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001652 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Rob Landley39cf6452006-05-05 16:52:28 +00001653 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001654 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001655 if (set_lookahead) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001656 args[2] = lookahead ? 0xaa : 0x55;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001657 print_flag_on_off(get_lookahead, "drive read-lookahead", lookahead);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001658 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001659 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001660 if (set_apmmode) {
Rob Landleyadde7982006-05-16 15:32:30 +00001661 args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */; /* feature register */
1662 args[1] = apmmode; /* sector count register 1-255 */
1663 if (get_apmmode)
1664 printf(" setting APM level to %s 0x%02lX (%ld)\n", (apmmode == 255) ? "disabled" : "", apmmode, apmmode);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001665 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Rob Landley39cf6452006-05-05 16:52:28 +00001666 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001667 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001668 if (set_wcache) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001669#ifdef DO_FLUSHCACHE
1670#ifndef WIN_FLUSHCACHE
1671#define WIN_FLUSHCACHE 0xe7
1672#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00001673#endif /* DO_FLUSHCACHE */
Eric Andersen3443bd72003-07-22 07:30:36 +00001674 args[2] = wcache ? 0x02 : 0x82;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001675 print_flag_on_off(get_wcache, "drive write-caching", wcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00001676#ifdef DO_FLUSHCACHE
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001677 if (!wcache)
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001678 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00001679#endif /* DO_FLUSHCACHE */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001680 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001681#ifdef DO_FLUSHCACHE
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001682 if (!wcache)
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001683 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00001684#endif /* DO_FLUSHCACHE */
1685 }
Rob Landley39cf6452006-05-05 16:52:28 +00001686
1687 /* In code below, we do not preserve args[0], but the rest
1688 is preserved, including args[2] */
1689 args[2] = 0;
1690
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001691 if (set_standbynow) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001692#ifndef WIN_STANDBYNOW1
1693#define WIN_STANDBYNOW1 0xE0
1694#endif
1695#ifndef WIN_STANDBYNOW2
1696#define WIN_STANDBYNOW2 0x94
1697#endif
Rob Landleyadde7982006-05-16 15:32:30 +00001698 if (get_standbynow) printf(" issuing standby command\n");
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001699 args[0] = WIN_STANDBYNOW1;
Denis Vlasenko892536f2007-09-27 10:23:34 +00001700 ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2);
Eric Andersen3443bd72003-07-22 07:30:36 +00001701 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001702 if (set_sleepnow) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001703#ifndef WIN_SLEEPNOW1
1704#define WIN_SLEEPNOW1 0xE6
1705#endif
1706#ifndef WIN_SLEEPNOW2
1707#define WIN_SLEEPNOW2 0x99
1708#endif
Rob Landleyadde7982006-05-16 15:32:30 +00001709 if (get_sleepnow) printf(" issuing sleep command\n");
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001710 args[0] = WIN_SLEEPNOW1;
Denis Vlasenko892536f2007-09-27 10:23:34 +00001711 ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2);
Eric Andersen3443bd72003-07-22 07:30:36 +00001712 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001713 if (set_seagate) {
Rob Landleye5b281f2006-04-29 15:49:18 +00001714 args[0] = 0xfb;
Rob Landleyadde7982006-05-16 15:32:30 +00001715 if (get_seagate) printf(" disabling Seagate auto powersaving mode\n");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001716 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001717 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001718 if (set_standby) {
Rob Landleye5b281f2006-04-29 15:49:18 +00001719 args[0] = WIN_SETIDLE1;
1720 args[1] = standby_requested;
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001721 if (get_standby) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001722 print_flag(1, "standby", standby_requested);
Eric Andersen3443bd72003-07-22 07:30:36 +00001723 interpret_standby(standby_requested);
1724 }
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001725 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Rob Landley39cf6452006-05-05 16:52:28 +00001726 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001727 }
1728#else /* HDIO_DRIVE_CMD */
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001729 if (force_operation) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001730 char buf[512];
Denis Vlasenko892536f2007-09-27 10:23:34 +00001731 flush_buffer_cache();
Eric Andersen3443bd72003-07-22 07:30:36 +00001732 if (-1 == read(fd, buf, sizeof(buf)))
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001733 bb_perror_msg("read(%d bytes) failed (rc=-1)", sizeof(buf));
Eric Andersen3443bd72003-07-22 07:30:36 +00001734 }
1735#endif /* HDIO_DRIVE_CMD */
1736
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001737 if (get_mult || get_identity) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001738 multcount = -1;
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001739 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount)) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001740 if (get_mult && ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn. */
Eric Andersen06d4ec22004-03-19 10:53:52 +00001741 bb_perror_msg("HDIO_GET_MULTCOUNT");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001742 else
1743 bb_perror_msg("ioctl %#x failed", HDIO_GET_MULTCOUNT);
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001744 } else if (get_mult) {
Rob Landleyadde7982006-05-16 15:32:30 +00001745 printf(fmt, "multcount", multcount);
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001746 on_off(multcount != 0);
Eric Andersen3443bd72003-07-22 07:30:36 +00001747 }
1748 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001749 if (get_io32bit) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001750 if (!ioctl_or_warn(fd, HDIO_GET_32BIT, &parm)) {
Rob Landleyadde7982006-05-16 15:32:30 +00001751 printf(" IO_support\t=%3ld (", parm);
1752 if (parm == 0)
1753 printf("default 16-bit)\n");
1754 else if (parm == 2)
1755 printf("16-bit)\n");
1756 else if (parm == 1)
1757 printf("32-bit)\n");
1758 else if (parm == 3)
1759 printf("32-bit w/sync)\n");
1760 else if (parm == 8)
1761 printf("Request-Queue-Bypass)\n");
1762 else
1763 printf("\?\?\?)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001764 }
1765 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001766 if (get_unmask) {
Bernhard Reutner-Fischer46904bf2008-01-19 11:27:11 +00001767 if(!ioctl_or_warn(fd, HDIO_GET_UNMASKINTR, &parm))
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001768 print_value_on_off("unmaskirq", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001769 }
1770
Glenn L McGrath07085852003-10-09 07:28:22 +00001771
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001772#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
Rob Landleyade7f952006-05-25 18:53:06 +00001773 if (get_dma) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001774 if (!ioctl_or_warn(fd, HDIO_GET_DMA, &parm)) {
Rob Landleyadde7982006-05-16 15:32:30 +00001775 printf(fmt, "using_dma", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001776 if (parm == 8)
1777 printf(" (DMA-Assisted-PIO)\n");
1778 else
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001779 on_off(parm != 0);
Eric Andersen3443bd72003-07-22 07:30:36 +00001780 }
1781 }
1782#endif
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001783 if (get_dma_q) {
Bernhard Reutner-Fischer46904bf2008-01-19 11:27:11 +00001784 if(!ioctl_or_warn(fd, HDIO_GET_QDMA, &parm))
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001785 print_value_on_off("queue_depth", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001786 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001787 if (get_keep) {
Bernhard Reutner-Fischer46904bf2008-01-19 11:27:11 +00001788 if(!ioctl_or_warn(fd, HDIO_GET_KEEPSETTINGS, &parm))
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001789 print_value_on_off("keepsettings", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001790 }
1791
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001792 if (get_nowerr) {
Bernhard Reutner-Fischer46904bf2008-01-19 11:27:11 +00001793 if(!ioctl_or_warn(fd, HDIO_GET_NOWERR, &parm))
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001794 print_value_on_off("nowerr", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001795 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001796 if (get_readonly) {
Bernhard Reutner-Fischer46904bf2008-01-19 11:27:11 +00001797 if(!ioctl_or_warn(fd, BLKROGET, &parm))
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001798 print_value_on_off("readonly", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001799 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001800 if (get_readahead) {
Bernhard Reutner-Fischer46904bf2008-01-19 11:27:11 +00001801 if(!ioctl_or_warn(fd, BLKRAGET, &parm))
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001802 print_value_on_off("readahead", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001803 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001804 if (get_geom) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001805 if (!ioctl_or_warn(fd, BLKGETSIZE, &parm)) {
Rob Landley2584e9b2006-05-03 20:00:00 +00001806 struct hd_geometry g;
1807
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001808 if (!ioctl_or_warn(fd, HDIO_GETGEO, &g))
Rob Landleyadde7982006-05-16 15:32:30 +00001809 printf(" geometry\t= %u/%u/%u, sectors = %ld, start = %ld\n",
Rob Landley2584e9b2006-05-03 20:00:00 +00001810 g.cylinders, g.heads, g.sectors, parm, g.start);
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001811 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001812 }
1813#ifdef HDIO_DRIVE_CMD
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001814 if (get_powermode) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001815#ifndef WIN_CHECKPOWERMODE1
1816#define WIN_CHECKPOWERMODE1 0xE5
1817#endif
1818#ifndef WIN_CHECKPOWERMODE2
1819#define WIN_CHECKPOWERMODE2 0x98
1820#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00001821 const char *state;
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001822
Rob Landleye5b281f2006-04-29 15:49:18 +00001823 args[0] = WIN_CHECKPOWERMODE1;
Denis Vlasenko892536f2007-09-27 10:23:34 +00001824 if (ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2)) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001825 if (errno != EIO || args[0] != 0 || args[1] != 0)
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001826 state = "unknown";
Eric Andersen3443bd72003-07-22 07:30:36 +00001827 else
1828 state = "sleeping";
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001829 } else
Glenn L McGrath07085852003-10-09 07:28:22 +00001830 state = (args[2] == 255) ? "active/idle" : "standby";
Rob Landley39cf6452006-05-05 16:52:28 +00001831 args[1] = args[2] = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +00001832
Eric Andersen3443bd72003-07-22 07:30:36 +00001833 printf(" drive state is: %s\n", state);
1834 }
1835#endif
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001836#if ENABLE_FEATURE_HDPARM_HDIO_DRIVE_RESET
1837 if (perform_reset) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001838 ioctl_or_warn(fd, HDIO_DRIVE_RESET, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00001839 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001840#endif /* FEATURE_HDPARM_HDIO_DRIVE_RESET */
1841#if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1842 if (perform_tristate) {
Rob Landleye5b281f2006-04-29 15:49:18 +00001843 args[0] = 0;
1844 args[1] = tristate;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001845 ioctl_or_warn(fd, HDIO_TRISTATE_HWIF, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001846 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001847#endif /* FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1848#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
1849 if (get_identity) {
Denis Vlasenko7c282a22007-03-28 00:14:54 +00001850 struct hd_driveid id;
Eric Andersen3443bd72003-07-22 07:30:36 +00001851
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001852 if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
1853 if (multcount != -1) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001854 id.multsect = multcount;
1855 id.multsect_valid |= 1;
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001856 } else
Eric Andersen3443bd72003-07-22 07:30:36 +00001857 id.multsect_valid &= ~1;
1858 dump_identity(&id);
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001859 } else if (errno == -ENOMSG)
Eric Andersen3443bd72003-07-22 07:30:36 +00001860 printf(" no identification info available\n");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001861 else if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
Denis Vlasenko49a128a2007-07-17 21:42:59 +00001862 bb_perror_msg("HDIO_GET_IDENTITY");
Eric Andersen3443bd72003-07-22 07:30:36 +00001863 else
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001864 bb_perror_msg("ioctl %#x failed", HDIO_GET_IDENTITY);
Eric Andersen3443bd72003-07-22 07:30:36 +00001865 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001866
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001867 if (get_IDentity) {
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001868 unsigned char args1[4+512]; /* = { ... } will eat 0.5k of rodata! */
Glenn L McGrath07085852003-10-09 07:28:22 +00001869
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001870 memset(args1, 0, sizeof(args1));
1871 args1[0] = WIN_IDENTIFY;
1872 args1[3] = 1;
Denis Vlasenko892536f2007-09-27 10:23:34 +00001873 if (!ioctl_alt_or_warn(HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY))
Rob Landley0753f4a2006-06-07 00:27:25 +00001874 identify((void *)(args1 + 4));
Eric Andersen3443bd72003-07-22 07:30:36 +00001875 }
1876#endif
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001877#if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1878 if (set_busstate) {
1879 if (get_busstate) {
Rob Landleyadde7982006-05-16 15:32:30 +00001880 print_flag(1, "bus state", busstate);
Eric Andersen3443bd72003-07-22 07:30:36 +00001881 bus_state_value(busstate);
1882 }
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001883 ioctl_or_warn(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate);
Eric Andersen3443bd72003-07-22 07:30:36 +00001884 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001885 if (get_busstate) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001886 if (!ioctl_or_warn(fd, HDIO_GET_BUSSTATE, &parm)) {
Rob Landleyadde7982006-05-16 15:32:30 +00001887 printf(fmt, "bus state", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001888 bus_state_value(parm);
1889 }
1890 }
1891#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001892 if (reread_partn)
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001893 ioctl_or_warn(fd, BLKRRPART, NULL);
Glenn L McGrath07085852003-10-09 07:28:22 +00001894
Eric Andersen3443bd72003-07-22 07:30:36 +00001895 if (do_ctimings)
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001896 do_time(1 /*,fd*/); /* time cache */
Eric Andersen3443bd72003-07-22 07:30:36 +00001897 if (do_timings)
Denis Vlasenkoc3122bc2007-09-28 10:28:03 +00001898 do_time(0 /*,fd*/); /* time device */
Eric Andersen3443bd72003-07-22 07:30:36 +00001899 if (do_flush)
Denis Vlasenko892536f2007-09-27 10:23:34 +00001900 flush_buffer_cache();
Rob Landleya3e4f382006-04-29 16:06:31 +00001901 close(fd);
Eric Andersen3443bd72003-07-22 07:30:36 +00001902}
1903
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001904#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Rob Landleya3e4f382006-04-29 16:06:31 +00001905static int fromhex(unsigned char c)
Eric Andersen3443bd72003-07-22 07:30:36 +00001906{
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +00001907 if (isdigit(c))
Eric Andersen3443bd72003-07-22 07:30:36 +00001908 return (c - '0');
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +00001909 if (c >= 'a' && c <= 'f')
1910 return (c - ('a' - 10));
Eric Andersen3443bd72003-07-22 07:30:36 +00001911 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
1912}
1913
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +00001914static void identify_from_stdin(void) ATTRIBUTE_NORETURN;
Rob Landley0f0b6452006-05-03 18:28:06 +00001915static void identify_from_stdin(void)
Eric Andersen3443bd72003-07-22 07:30:36 +00001916{
Rob Landley0753f4a2006-06-07 00:27:25 +00001917 uint16_t sbuf[256];
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +00001918 unsigned char buf[1280];
1919 unsigned char *b = (unsigned char *)buf;
1920 int i;
Eric Andersen3443bd72003-07-22 07:30:36 +00001921
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +00001922 xread(0, buf, 1280);
Rob Landleyade7f952006-05-25 18:53:06 +00001923
Rob Landley0753f4a2006-06-07 00:27:25 +00001924 // Convert the newline-separated hex data into an identify block.
1925
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001926 for (i = 0; i < 256; i++) {
Rob Landley0753f4a2006-06-07 00:27:25 +00001927 int j;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001928 for (j = 0; j < 4; j++)
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +00001929 sbuf[i] = (sbuf[i] << 4) + fromhex(*(b++));
Eric Andersen3443bd72003-07-22 07:30:36 +00001930 }
Rob Landley0753f4a2006-06-07 00:27:25 +00001931
1932 // Parse the data.
1933
Rob Landley6389ff12006-05-01 19:28:53 +00001934 identify(sbuf);
Eric Andersen3443bd72003-07-22 07:30:36 +00001935}
Denis Vlasenko0d3c6af2007-09-28 10:25:32 +00001936#else
1937void identify_from_stdin(void);
Eric Andersen3443bd72003-07-22 07:30:36 +00001938#endif
1939
Rob Landley20deab02006-05-07 23:34:15 +00001940/* busybox specific stuff */
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001941static void parse_opts(smallint *get, smallint *set, unsigned long *value, int min, int max)
Eric Andersenb2aa7762004-04-05 13:08:08 +00001942{
Denis Vlasenko6429aab2006-09-23 12:22:11 +00001943 if (get) {
1944 *get = 1;
1945 }
1946 if (optarg) {
1947 *set = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +00001948 *value = xatol_range(optarg, min, max);
Denis Vlasenko6429aab2006-09-23 12:22:11 +00001949 }
Rob Landley20deab02006-05-07 23:34:15 +00001950}
1951
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001952static void parse_xfermode(int flag, smallint *get, smallint *set, int *value)
Rob Landley20deab02006-05-07 23:34:15 +00001953{
1954 if (flag) {
Rob Landleyade7f952006-05-25 18:53:06 +00001955 *get = 1;
Rob Landley20deab02006-05-07 23:34:15 +00001956 if (optarg) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001957 *value = translate_xfermode(optarg);
1958 *set = (*value > -1);
Rob Landley20deab02006-05-07 23:34:15 +00001959 }
1960 }
1961}
1962
Rob Landley06208412006-05-31 22:52:57 +00001963/*------- getopt short options --------*/
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001964static const char hdparm_options[] ALIGN1 =
Denis Vlasenko4daad902007-09-27 10:20:47 +00001965 "gfu::n::p:r::m::c::k::a::B:tT"
Rob Landley06208412006-05-31 22:52:57 +00001966 USE_FEATURE_HDPARM_GET_IDENTITY("iI")
Rob Landley20deab02006-05-07 23:34:15 +00001967 USE_FEATURE_HDPARM_HDIO_GETSET_DMA("d::")
1968#ifdef HDIO_DRIVE_CMD
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001969 "S:D:P:X:K:A:L:W:CyYzZ"
Rob Landley20deab02006-05-07 23:34:15 +00001970#endif
1971 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF("U:")
1972#ifdef HDIO_GET_QDMA
1973#ifdef HDIO_SET_QDMA
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001974 "Q:"
Rob Landley20deab02006-05-07 23:34:15 +00001975#else
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001976 "Q"
Rob Landley20deab02006-05-07 23:34:15 +00001977#endif
1978#endif
1979 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET("w")
1980 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF("x::b:")
1981 USE_FEATURE_HDPARM_HDIO_SCAN_HWIF("R:");
1982/*-------------------------------------*/
1983
1984/* our main() routine: */
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001985int hdparm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landley20deab02006-05-07 23:34:15 +00001986int hdparm_main(int argc, char **argv)
1987{
1988 int c;
Rob Landleyade7f952006-05-25 18:53:06 +00001989 int flagcount = 0;
Rob Landley20deab02006-05-07 23:34:15 +00001990
Rob Landley06208412006-05-31 22:52:57 +00001991 while ((c = getopt(argc, argv, hdparm_options)) >= 0) {
Rob Landley20deab02006-05-07 23:34:15 +00001992 flagcount++;
Mike Frysingerea93f8a2006-06-07 14:25:22 +00001993 USE_FEATURE_HDPARM_GET_IDENTITY(get_IDentity |= (c == 'I'));
1994 USE_FEATURE_HDPARM_GET_IDENTITY(get_identity |= (c == 'i'));
Rob Landleyadde7982006-05-16 15:32:30 +00001995 get_geom |= (c == 'g');
1996 do_flush |= (c == 'f');
Rob Landley19802562006-05-08 15:35:46 +00001997 if (c == 'u') parse_opts(&get_unmask, &set_unmask, &unmask, 0, 1);
1998 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(if (c == 'd') parse_opts(&get_dma, &set_dma, &dma, 0, 9));
1999 if (c == 'n') parse_opts(&get_nowerr, &set_nowerr, &nowerr, 0, 1);
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00002000 parse_xfermode((c == 'p'), &noisy_piomode, &set_piomode, &piomode);
Rob Landley19802562006-05-08 15:35:46 +00002001 if (c == 'r') parse_opts(&get_readonly, &set_readonly, &readonly, 0, 1);
2002 if (c == 'm') parse_opts(&get_mult, &set_mult, &mult, 0, INT_MAX /*32*/);
2003 if (c == 'c') parse_opts(&get_io32bit, &set_io32bit, &io32bit, 0, INT_MAX /*8*/);
2004 if (c == 'k') parse_opts(&get_keep, &set_keep, &keep, 0, 1);
2005 if (c == 'a') parse_opts(&get_readahead, &set_readahead, &Xreadahead, 0, INT_MAX);
2006 if (c == 'B') parse_opts(&get_apmmode, &set_apmmode, &apmmode, 1, 255);
Rob Landleyadde7982006-05-16 15:32:30 +00002007 do_flush |= do_timings |= (c == 't');
2008 do_flush |= do_ctimings |= (c == 'T');
Rob Landley20deab02006-05-07 23:34:15 +00002009#ifdef HDIO_DRIVE_CMD
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00002010 if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, INT_MAX);
Rob Landley19802562006-05-08 15:35:46 +00002011 if (c == 'D') parse_opts(&get_defects, &set_defects, &defects, 0, INT_MAX);
2012 if (c == 'P') parse_opts(&get_prefetch, &set_prefetch, &prefetch, 0, INT_MAX);
Rob Landleyade7f952006-05-25 18:53:06 +00002013 parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
Rob Landley19802562006-05-08 15:35:46 +00002014 if (c == 'K') parse_opts(&get_dkeep, &set_dkeep, &prefetch, 0, 1);
2015 if (c == 'A') parse_opts(&get_lookahead, &set_lookahead, &lookahead, 0, 1);
2016 if (c == 'L') parse_opts(&get_doorlock, &set_doorlock, &doorlock, 0, 1);
2017 if (c == 'W') parse_opts(&get_wcache, &set_wcache, &wcache, 0, 1);
Rob Landleyade7f952006-05-25 18:53:06 +00002018 get_powermode |= (c == 'C');
2019 get_standbynow = set_standbynow |= (c == 'y');
2020 get_sleepnow = set_sleepnow |= (c == 'Y');
Rob Landleyadde7982006-05-16 15:32:30 +00002021 reread_partn |= (c == 'z');
Rob Landleyade7f952006-05-25 18:53:06 +00002022 get_seagate = set_seagate |= (c == 'Z');
Rob Landley20deab02006-05-07 23:34:15 +00002023#endif
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00002024 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') parse_opts(NULL, &unregister_hwif, &hwif, 0, INT_MAX));
Rob Landley20deab02006-05-07 23:34:15 +00002025#ifdef HDIO_GET_QDMA
Rob Landley19802562006-05-08 15:35:46 +00002026 if (c == 'Q') {
Rob Landley20deab02006-05-07 23:34:15 +00002027#ifdef HDIO_SET_QDMA
Rob Landley19802562006-05-08 15:35:46 +00002028 parse_opts(&get_dma_q, &set_dma_q, &dma_q, 0, INT_MAX);
Rob Landley20deab02006-05-07 23:34:15 +00002029#else
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00002030 parse_opts(&get_dma_q, NULL, NULL, 0, 0);
Rob Landley20deab02006-05-07 23:34:15 +00002031#endif
Rob Landley19802562006-05-08 15:35:46 +00002032 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00002033#endif
Rob Landleyade7f952006-05-25 18:53:06 +00002034 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00002035 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1));
2036 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') parse_opts(&get_busstate, &set_busstate, &busstate, 0, 2));
Rob Landley20deab02006-05-07 23:34:15 +00002037#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
2038 if (c == 'R') {
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00002039 parse_opts(NULL, &scan_hwif, &hwif_data, 0, INT_MAX);
Denis Vlasenko13858992006-10-08 12:49:22 +00002040 hwif_ctrl = xatoi_u((argv[optind]) ? argv[optind] : "");
2041 hwif_irq = xatoi_u((argv[optind+1]) ? argv[optind+1] : "");
Rob Landley20deab02006-05-07 23:34:15 +00002042 /* Move past the 2 additional arguments */
2043 argv += 2;
2044 argc -= 2;
2045 }
2046#endif
2047 }
Rob Landleyade7f952006-05-25 18:53:06 +00002048 /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
Denis Vlasenko67b23e62006-10-03 21:00:06 +00002049 if (!flagcount) {
Rob Landleyade7f952006-05-25 18:53:06 +00002050 get_mult = get_io32bit = get_unmask = get_keep = get_readonly = get_readahead = get_geom = 1;
2051 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(get_dma = 1);
2052 }
Rob Landley20deab02006-05-07 23:34:15 +00002053 argv += optind;
2054
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00002055 if (!*argv) {
Rob Landley6d8ce172006-06-07 21:22:42 +00002056 if (ENABLE_FEATURE_HDPARM_GET_IDENTITY && !isatty(STDIN_FILENO))
2057 identify_from_stdin(); /* EXIT */
Denis Vlasenko4daad902007-09-27 10:20:47 +00002058 bb_show_usage();
Rob Landley20deab02006-05-07 23:34:15 +00002059 }
2060
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00002061 do {
2062 process_dev(*argv++);
2063 } while (*argv);
2064
2065 return EXIT_SUCCESS;
Eric Andersen3443bd72003-07-22 07:30:36 +00002066}