blob: 925644e1fc8943ff1cf3ae45de60e584bbd714fa [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
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000015#include "busybox.h"
Eric Andersen3443bd72003-07-22 07:30:36 +000016#include <string.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000017#include <fcntl.h>
18#include <errno.h>
19#include <ctype.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000020#include <sys/ioctl.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000021#include <sys/sysmacros.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000022#include <sys/times.h>
Rob Landley4ae2f512006-05-19 17:24:26 +000023#include <sys/mman.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000024#include <linux/types.h>
25#include <linux/hdreg.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000026
Rob Landley688ed0d2006-03-04 22:40:25 +000027#if BB_BIG_ENDIAN && !defined(__USE_XOPEN)
28# define __USE_XOPEN
Eric Andersen3443bd72003-07-22 07:30:36 +000029#endif
Rob Landleyade7f952006-05-25 18:53:06 +000030#include <unistd.h>
31
Eric Andersen3443bd72003-07-22 07:30:36 +000032/* device types */
33/* ------------ */
34#define NO_DEV 0xffff
35#define ATA_DEV 0x0000
36#define ATAPI_DEV 0x0001
37
38/* word definitions */
39/* ---------------- */
40#define GEN_CONFIG 0 /* general configuration */
41#define LCYLS 1 /* number of logical cylinders */
42#define CONFIG 2 /* specific configuration */
43#define LHEADS 3 /* number of logical heads */
44#define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
45#define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
46#define LSECTS 6 /* number of logical sectors/track */
47#define START_SERIAL 10 /* ASCII serial number */
48#define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
49#define BUF_TYPE 20 /* buffer type (ATA-1) */
50#define BUFFER__SIZE 21 /* buffer size (ATA-1) */
51#define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
52#define START_FW_REV 23 /* ASCII firmware revision */
53#define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000054#define START_MODEL 27 /* ASCII model number */
55#define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
Eric Andersen3443bd72003-07-22 07:30:36 +000056#define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
57#define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
58#define CAPAB_0 49 /* capabilities */
59#define CAPAB_1 50
60#define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
61#define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
62#define WHATS_VALID 53 /* what fields are valid */
63#define LCYLS_CUR 54 /* current logical cylinders */
64#define LHEADS_CUR 55 /* current logical heads */
65#define LSECTS_CUR 56 /* current logical sectors/track */
66#define CAPACITY_LSB 57 /* current capacity in sectors */
67#define CAPACITY_MSB 58
68#define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
69#define LBA_SECTS_LSB 60 /* LBA: total number of user */
70#define LBA_SECTS_MSB 61 /* addressable sectors */
71#define SINGLE_DMA 62 /* singleword DMA modes */
72#define MULTI_DMA 63 /* multiword DMA modes */
73#define ADV_PIO_MODES 64 /* advanced PIO modes supported */
74 /* multiword DMA xfer cycle time: */
75#define DMA_TIME_MIN 65 /* - minimum */
76#define DMA_TIME_NORM 66 /* - manufacturer's recommended */
77 /* minimum PIO xfer cycle time: */
78#define PIO_NO_FLOW 67 /* - without flow control */
79#define PIO_FLOW 68 /* - with IORDY flow control */
80#define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
81#define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
82#define CDR_MAJOR 73 /* CD ROM: major version number */
83#define CDR_MINOR 74 /* CD ROM: minor version number */
84#define QUEUE_DEPTH 75 /* queue depth */
85#define MAJOR 80 /* major version number */
86#define MINOR 81 /* minor version number */
87#define CMDS_SUPP_0 82 /* command/feature set(s) supported */
88#define CMDS_SUPP_1 83
89#define CMDS_SUPP_2 84
90#define CMDS_EN_0 85 /* command/feature set(s) enabled */
91#define CMDS_EN_1 86
92#define CMDS_EN_2 87
93#define ULTRA_DMA 88 /* ultra DMA modes */
94 /* time to complete security erase */
95#define ERASE_TIME 89 /* - ordinary */
96#define ENH_ERASE_TIME 90 /* - enhanced */
97#define ADV_PWR 91 /* current advanced power management level
Glenn L McGrath07085852003-10-09 07:28:22 +000098 in low byte, 0x40 in high byte. */
Eric Andersen3443bd72003-07-22 07:30:36 +000099#define PSWD_CODE 92 /* master password revision code */
100#define HWRST_RSLT 93 /* hardware reset result */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000101#define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
Eric Andersen3443bd72003-07-22 07:30:36 +0000102#define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
103#define LBA_MID 101 /* bits are used, but addr 103 */
104#define LBA_48_MSB 102 /* has been reserved for LBA in */
105#define LBA_64_MSB 103 /* the future. */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000106#define RM_STAT 127 /* removable media status notification feature set support */
Eric Andersen3443bd72003-07-22 07:30:36 +0000107#define SECU_STATUS 128 /* security status */
108#define CFA_PWR_MODE 160 /* CFA power mode 1 */
109#define START_MEDIA 176 /* media serial number */
110#define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
111#define START_MANUF 196 /* media manufacturer I.D. */
112#define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
113#define INTEGRITY 255 /* integrity word */
114
115/* bit definitions within the words */
116/* -------------------------------- */
117
118/* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
119#define VALID 0xc000
120#define VALID_VAL 0x4000
121/* many words are considered invalid if they are either all-0 or all-1 */
122#define NOVAL_0 0x0000
123#define NOVAL_1 0xffff
124
125/* word 0: gen_config */
Glenn L McGrath07085852003-10-09 07:28:22 +0000126#define NOT_ATA 0x8000
Eric Andersen3443bd72003-07-22 07:30:36 +0000127#define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
128#define MEDIA_REMOVABLE 0x0080
129#define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
130#define INCOMPLETE 0x0004
131#define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
132#define DRQ_RESPONSE_TIME 0x0060
133#define DRQ_3MS_VAL 0x0000
134#define DRQ_INTR_VAL 0x0020
135#define DRQ_50US_VAL 0x0040
136#define PKT_SIZE_SUPPORTED 0x0003
137#define PKT_SIZE_12_VAL 0x0000
138#define PKT_SIZE_16_VAL 0x0001
139#define EQPT_TYPE 0x1f00
140#define SHIFT_EQPT 8
141
142#define CDROM 0x0005
143
144#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000145static const char * const pkt_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000146 "Direct-access device", /* word 0, bits 12-8 = 00 */
147 "Sequential-access device", /* word 0, bits 12-8 = 01 */
148 "Printer", /* word 0, bits 12-8 = 02 */
149 "Processor", /* word 0, bits 12-8 = 03 */
150 "Write-once device", /* word 0, bits 12-8 = 04 */
151 "CD-ROM", /* word 0, bits 12-8 = 05 */
152 "Scanner", /* word 0, bits 12-8 = 06 */
153 "Optical memory", /* word 0, bits 12-8 = 07 */
154 "Medium changer", /* word 0, bits 12-8 = 08 */
155 "Communications device", /* word 0, bits 12-8 = 09 */
156 "ACS-IT8 device", /* word 0, bits 12-8 = 0a */
157 "ACS-IT8 device", /* word 0, bits 12-8 = 0b */
158 "Array controller", /* word 0, bits 12-8 = 0c */
159 "Enclosure services", /* word 0, bits 12-8 = 0d */
160 "Reduced block command device", /* word 0, bits 12-8 = 0e */
161 "Optical card reader/writer", /* word 0, bits 12-8 = 0f */
162 "", /* word 0, bits 12-8 = 10 */
163 "", /* word 0, bits 12-8 = 11 */
164 "", /* word 0, bits 12-8 = 12 */
165 "", /* word 0, bits 12-8 = 13 */
166 "", /* word 0, bits 12-8 = 14 */
167 "", /* word 0, bits 12-8 = 15 */
168 "", /* word 0, bits 12-8 = 16 */
169 "", /* word 0, bits 12-8 = 17 */
170 "", /* word 0, bits 12-8 = 18 */
171 "", /* word 0, bits 12-8 = 19 */
172 "", /* word 0, bits 12-8 = 1a */
173 "", /* word 0, bits 12-8 = 1b */
174 "", /* word 0, bits 12-8 = 1c */
175 "", /* word 0, bits 12-8 = 1d */
176 "", /* word 0, bits 12-8 = 1e */
Rob Landleyadde7982006-05-16 15:32:30 +0000177 "Unknown", /* word 0, bits 12-8 = 1f */
Eric Andersen3443bd72003-07-22 07:30:36 +0000178};
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000179
180static const char * const ata1_cfg_str[] = { /* word 0 in ATA-1 mode */
Rob Landleyadde7982006-05-16 15:32:30 +0000181 "Reserved", /* bit 0 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000182 "hard sectored", /* bit 1 */
183 "soft sectored", /* bit 2 */
184 "not MFM encoded ", /* bit 3 */
185 "head switch time > 15us", /* bit 4 */
186 "spindle motor control option", /* bit 5 */
187 "fixed drive", /* bit 6 */
188 "removable drive", /* bit 7 */
189 "disk xfer rate <= 5Mbs", /* bit 8 */
190 "disk xfer rate > 5Mbs, <= 10Mbs", /* bit 9 */
191 "disk xfer rate > 5Mbs", /* bit 10 */
192 "rotational speed tol.", /* bit 11 */
193 "data strobe offset option", /* bit 12 */
194 "track offset option", /* bit 13 */
195 "format speed tolerance gap reqd", /* bit 14 */
196 "ATAPI" /* bit 14 */
197};
198#endif
199
200/* word 1: number of logical cylinders */
201#define LCYLS_MAX 0x3fff /* maximum allowable value */
202
Eric Andersenaff114c2004-04-14 17:51:38 +0000203/* word 2: specific configuration
Eric Andersen3443bd72003-07-22 07:30:36 +0000204 * (a) require SET FEATURES to spin-up
205 * (b) require spin-up to fully reply to IDENTIFY DEVICE
206 */
207#define STBY_NID_VAL 0x37c8 /* (a) and (b) */
208#define STBY_ID_VAL 0x738c /* (a) and not (b) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000209#define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
Eric Andersen3443bd72003-07-22 07:30:36 +0000210#define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
211
212/* words 47 & 59: sector_xfer_max & sector_xfer_cur */
213#define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
214#define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
215
216/* word 49: capabilities 0 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000217#define STD_STBY 0x2000 /* 1=standard values supported (ATA);
Eric Andersen3443bd72003-07-22 07:30:36 +0000218 0=vendor specific values */
219#define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
220#define IORDY_OFF 0x0400 /* 1=may be disabled */
221#define LBA_SUP 0x0200 /* 1=Logical Block Address support */
222#define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
223#define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
224#define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
225#define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
226#define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
227
228/* word 50: capabilities 1 */
229#define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
230
231/* words 51 & 52: PIO & DMA cycle times */
232#define MODE 0xff00 /* the mode is in the MSBs */
233
234/* word 53: whats_valid */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000235#define OK_W88 0x0004 /* the ultra_dma info is valid */
Eric Andersen3443bd72003-07-22 07:30:36 +0000236#define OK_W64_70 0x0002 /* see above for word descriptions */
237#define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
238
239/*word 63,88: dma_mode, ultra_dma_mode*/
240#define MODE_MAX 7 /* bit definitions force udma <=7 (when
241 * udma >=8 comes out it'll have to be
242 * defined in a new dma_mode word!) */
243
244/* word 64: PIO transfer modes */
245#define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
246#define PIO_MODE_MAX 8 /* but all 8 bits are defined */
247
248/* word 75: queue_depth */
249#define DEPTH_BITS 0x001f /* bits used for queue depth */
250
251/* words 80-81: version numbers */
252/* NOVAL_0 or NOVAL_1 means device does not report version */
253
254/* word 81: minor version number */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000255#define MINOR_MAX 0x22
Eric Andersen3443bd72003-07-22 07:30:36 +0000256#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Rob Landley0e6a3e12006-04-28 01:33:30 +0000257static const char *minor_str[MINOR_MAX+2] = { /* word 81 value: */
258 "Unspecified", /* 0x0000 */
Rob Landleyadde7982006-05-16 15:32:30 +0000259 "ATA-1 X3T9.2 781D prior to rev.4", /* 0x0001 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000260 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
Rob Landleyadde7982006-05-16 15:32:30 +0000261 "ATA-1 X3T9.2 781D rev.4", /* 0x0003 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000262 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
Rob Landleyadde7982006-05-16 15:32:30 +0000263 "ATA-2 X3T10 948D prior to rev.2k", /* 0x0005 */
264 "ATA-3 X3T10 2008D rev.1", /* 0x0006 */
265 "ATA-2 X3T10 948D rev.2k", /* 0x0007 */
266 "ATA-3 X3T10 2008D rev.0", /* 0x0008 */
267 "ATA-2 X3T10 948D rev.3", /* 0x0009 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000268 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
Rob Landleyadde7982006-05-16 15:32:30 +0000269 "ATA-3 X3T10 2008D rev.6", /* 0x000b */
270 "ATA-3 X3T13 2008D rev.7 and 7a", /* 0x000c */
271 "ATA/ATAPI-4 X3T13 1153D rev.6", /* 0x000d */
272 "ATA/ATAPI-4 T13 1153D rev.13", /* 0x000e */
273 "ATA/ATAPI-4 X3T13 1153D rev.7", /* 0x000f */
274 "ATA/ATAPI-4 T13 1153D rev.18", /* 0x0010 */
275 "ATA/ATAPI-4 T13 1153D rev.15", /* 0x0011 */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000276 "ATA/ATAPI-4 published, ANSI INCITS 317-1998", /* 0x0012 */
Rob Landleyadde7982006-05-16 15:32:30 +0000277 "ATA/ATAPI-5 T13 1321D rev.3",
278 "ATA/ATAPI-4 T13 1153D rev.14", /* 0x0014 */
279 "ATA/ATAPI-5 T13 1321D rev.1", /* 0x0015 */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000280 "ATA/ATAPI-5 published, ANSI INCITS 340-2000", /* 0x0016 */
Rob Landleyadde7982006-05-16 15:32:30 +0000281 "ATA/ATAPI-4 T13 1153D rev.17", /* 0x0017 */
282 "ATA/ATAPI-6 T13 1410D rev.0", /* 0x0018 */
283 "ATA/ATAPI-6 T13 1410D rev.3a", /* 0x0019 */
284 "ATA/ATAPI-7 T13 1532D rev.1", /* 0x001a */
285 "ATA/ATAPI-6 T13 1410D rev.2", /* 0x001b */
286 "ATA/ATAPI-6 T13 1410D rev.1", /* 0x001c */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000287 "ATA/ATAPI-7 published, ANSI INCITS 397-2005", /* 0x001d */
Rob Landleyadde7982006-05-16 15:32:30 +0000288 "ATA/ATAPI-7 T13 1532D rev.0", /* 0x001e */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000289 "Reserved" /* 0x001f */
290 "Reserved" /* 0x0020 */
Rob Landleyadde7982006-05-16 15:32:30 +0000291 "ATA/ATAPI-7 T13 1532D rev.4a", /* 0x0021 */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000292 "ATA/ATAPI-6 published, ANSI INCITS 361-2002", /* 0x0022 */
293 "Reserved" /* 0x0023-0xfffe*/
Eric Andersen3443bd72003-07-22 07:30:36 +0000294};
295#endif
Rob Landley0e6a3e12006-04-28 01:33:30 +0000296static const char actual_ver[MINOR_MAX+2] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000297 /* word 81 value: */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000298 0, /* 0x0000 WARNING: */
299 1, /* 0x0001 WARNING: */
300 1, /* 0x0002 WARNING: */
301 1, /* 0x0003 WARNING: */
302 2, /* 0x0004 WARNING: This array */
303 2, /* 0x0005 WARNING: corresponds */
Eric Andersen3443bd72003-07-22 07:30:36 +0000304 3, /* 0x0006 WARNING: *exactly* */
305 2, /* 0x0007 WARNING: to the ATA/ */
306 3, /* 0x0008 WARNING: ATAPI version */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000307 2, /* 0x0009 WARNING: listed in */
308 3, /* 0x000a WARNING: the */
309 3, /* 0x000b WARNING: minor_str */
Eric Andersen3443bd72003-07-22 07:30:36 +0000310 3, /* 0x000c WARNING: array */
311 4, /* 0x000d WARNING: above. */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000312 4, /* 0x000e WARNING: */
313 4, /* 0x000f WARNING: if you change */
314 4, /* 0x0010 WARNING: that one, */
Eric Andersen3443bd72003-07-22 07:30:36 +0000315 4, /* 0x0011 WARNING: change this one */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000316 4, /* 0x0012 WARNING: too!!! */
Eric Andersen3443bd72003-07-22 07:30:36 +0000317 5, /* 0x0013 WARNING: */
318 4, /* 0x0014 WARNING: */
319 5, /* 0x0015 WARNING: */
320 5, /* 0x0016 WARNING: */
321 4, /* 0x0017 WARNING: */
322 6, /* 0x0018 WARNING: */
323 6, /* 0x0019 WARNING: */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000324 7, /* 0x001a WARNING: */
Eric Andersen3443bd72003-07-22 07:30:36 +0000325 6, /* 0x001b WARNING: */
326 6, /* 0x001c WARNING: */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000327 7, /* 0x001d WARNING: */
328 7, /* 0x001e WARNING: */
329 0, /* 0x001f WARNING: */
330 0, /* 0x0020 WARNING: */
331 7, /* 0x0021 WARNING: */
332 6, /* 0x0022 WARNING: */
333 0 /* 0x0023-0xfffe */
Eric Andersen3443bd72003-07-22 07:30:36 +0000334};
335
336/* words 82-84: cmds/feats supported */
337#define CMDS_W82 0x77ff /* word 82: defined command locations*/
338#define CMDS_W83 0x3fff /* word 83: defined command locations*/
339#define CMDS_W84 0x002f /* word 83: defined command locations*/
Glenn L McGrath07085852003-10-09 07:28:22 +0000340#define SUPPORT_48_BIT 0x0400
Eric Andersen3443bd72003-07-22 07:30:36 +0000341#define NUM_CMD_FEAT_STR 48
342
343#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000344static const char * const cmd_feat_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000345 "", /* word 82 bit 15: obsolete */
346 "NOP cmd", /* word 82 bit 14 */
347 "READ BUFFER cmd", /* word 82 bit 13 */
348 "WRITE BUFFER cmd", /* word 82 bit 12 */
349 "", /* word 82 bit 11: obsolete */
350 "Host Protected Area feature set", /* word 82 bit 10 */
351 "DEVICE RESET cmd", /* word 82 bit 9 */
352 "SERVICE interrupt", /* word 82 bit 8 */
353 "Release interrupt", /* word 82 bit 7 */
354 "Look-ahead", /* word 82 bit 6 */
355 "Write cache", /* word 82 bit 5 */
356 "PACKET command feature set", /* word 82 bit 4 */
357 "Power Management feature set", /* word 82 bit 3 */
358 "Removable Media feature set", /* word 82 bit 2 */
359 "Security Mode feature set", /* word 82 bit 1 */
360 "SMART feature set", /* word 82 bit 0 */
361 /* --------------*/
362 "", /* word 83 bit 15: !valid bit */
363 "", /* word 83 bit 14: valid bit */
Rob Landleyadde7982006-05-16 15:32:30 +0000364 "FLUSH CACHE EXT cmd", /* word 83 bit 13 */
365 "Mandatory FLUSH CACHE cmd ", /* word 83 bit 12 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000366 "Device Configuration Overlay feature set ",
367 "48-bit Address feature set ", /* word 83 bit 10 */
368 "",
369 "SET MAX security extension", /* word 83 bit 8 */
370 "Address Offset Reserved Area Boot", /* word 83 bit 7 */
371 "SET FEATURES subcommand required to spinup after power up",
372 "Power-Up In Standby feature set", /* word 83 bit 5 */
373 "Removable Media Status Notification feature set",
Eric Andersen06d4ec22004-03-19 10:53:52 +0000374 "Adv. Power Management feature set",/* word 83 bit 3 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000375 "CFA feature set", /* word 83 bit 2 */
376 "READ/WRITE DMA QUEUED", /* word 83 bit 1 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000377 "DOWNLOAD MICROCODE cmd", /* word 83 bit 0 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000378 /* --------------*/
379 "", /* word 84 bit 15: !valid bit */
380 "", /* word 84 bit 14: valid bit */
381 "", /* word 84 bit 13: reserved */
382 "", /* word 84 bit 12: reserved */
383 "", /* word 84 bit 11: reserved */
384 "", /* word 84 bit 10: reserved */
385 "", /* word 84 bit 9: reserved */
386 "", /* word 84 bit 8: reserved */
387 "", /* word 84 bit 7: reserved */
388 "", /* word 84 bit 6: reserved */
389 "General Purpose Logging feature set", /* word 84 bit 5 */
390 "", /* word 84 bit 4: reserved */
391 "Media Card Pass Through Command feature set ",
392 "Media serial number ", /* word 84 bit 2 */
393 "SMART self-test ", /* word 84 bit 1 */
394 "SMART error logging " /* word 84 bit 0 */
395};
Rob Landley0f0b6452006-05-03 18:28:06 +0000396
397static void identify(uint16_t *id_supplied) ATTRIBUTE_NORETURN;
398static void identify_from_stdin(void) ATTRIBUTE_NORETURN;
Rob Landley9c6f9552006-06-08 14:11:36 +0000399#else
400void identify_from_stdin(void);
Eric Andersen3443bd72003-07-22 07:30:36 +0000401#endif
402
403
404/* words 85-87: cmds/feats enabled */
405/* use cmd_feat_str[] to display what commands and features have
Glenn L McGrath07085852003-10-09 07:28:22 +0000406 * been enabled with words 85-87
Eric Andersen3443bd72003-07-22 07:30:36 +0000407 */
408
409/* words 89, 90, SECU ERASE TIME */
410#define ERASE_BITS 0x00ff
411
412/* word 92: master password revision */
413/* NOVAL_0 or NOVAL_1 means no support for master password revision */
414
415/* word 93: hw reset result */
416#define CBLID 0x2000 /* CBLID status */
417#define RST0 0x0001 /* 1=reset to device #0 */
418#define DEV_DET 0x0006 /* how device num determined */
419#define JUMPER_VAL 0x0002 /* device num determined by jumper */
420#define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
421
422/* word 127: removable media status notification feature set support */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000423#define RM_STAT_BITS 0x0003
Eric Andersen3443bd72003-07-22 07:30:36 +0000424#define RM_STAT_SUP 0x0001
Glenn L McGrath07085852003-10-09 07:28:22 +0000425
Eric Andersen3443bd72003-07-22 07:30:36 +0000426/* word 128: security */
427#define SECU_ENABLED 0x0002
428#define SECU_LEVEL 0x0010
429#define NUM_SECU_STR 6
430#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000431static const char * const secu_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000432 "supported", /* word 128, bit 0 */
433 "enabled", /* word 128, bit 1 */
434 "locked", /* word 128, bit 2 */
435 "frozen", /* word 128, bit 3 */
436 "expired: security count", /* word 128, bit 4 */
437 "supported: enhanced erase" /* word 128, bit 5 */
438};
439#endif
440
441/* word 160: CFA power mode */
442#define VALID_W160 0x8000 /* 1=word valid */
443#define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
444#define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
445#define MAX_AMPS 0x0fff /* value = max current in ma */
446
447/* word 255: integrity */
448#define SIG 0x00ff /* signature location */
449#define SIG_VAL 0x00A5 /* signature value */
450
Glenn L McGrath07085852003-10-09 07:28:22 +0000451#define TIMING_MB 64
452#define TIMING_BUF_MB 1
453#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
454#define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
455#define BUFCACHE_FACTOR 2
456
457#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
458
459/* Busybox messages and functions */
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000460static int bb_ioctl(int fd, int request, void *argp, const char *string)
Glenn L McGrath07085852003-10-09 07:28:22 +0000461{
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000462 int e = ioctl(fd, request, argp);
463 if (e && string)
Eric Andersen06d4ec22004-03-19 10:53:52 +0000464 bb_perror_msg(" %s", string);
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000465 return e;
Glenn L McGrath07085852003-10-09 07:28:22 +0000466}
467
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000468static int bb_ioctl_alt(int fd, int cmd, unsigned char *args, int alt, const char *string)
469{
470 if (!ioctl(fd, cmd, args))
471 return 0;
472 args[0] = alt;
473 return bb_ioctl(fd, cmd, args, string);
474}
475
Rob Landleya3e4f382006-04-29 16:06:31 +0000476static void on_off(unsigned int value);
Glenn L McGrath07085852003-10-09 07:28:22 +0000477
Rob Landleyadde7982006-05-16 15:32:30 +0000478static void print_flag_on_off(unsigned long get_arg, const char *s, unsigned long arg)
Glenn L McGrath07085852003-10-09 07:28:22 +0000479{
Rob Landleyadde7982006-05-16 15:32:30 +0000480
Glenn L McGrath07085852003-10-09 07:28:22 +0000481 if (get_arg)
482 {
Rob Landleyadde7982006-05-16 15:32:30 +0000483 printf(" setting %s to %ld", s, arg);
Glenn L McGrath07085852003-10-09 07:28:22 +0000484 on_off(arg);
485 }
486}
487
488static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
Rob Landleyadde7982006-05-16 15:32:30 +0000489 const char * str)
Glenn L McGrath07085852003-10-09 07:28:22 +0000490{
Rob Landleya3e4f382006-04-29 16:06:31 +0000491 if (ioctl(fd, request, &argp) != 0)
Eric Andersen06d4ec22004-03-19 10:53:52 +0000492 bb_perror_msg(" %s", string);
Glenn L McGrath07085852003-10-09 07:28:22 +0000493 else
494 {
Rob Landleyadde7982006-05-16 15:32:30 +0000495 printf(" %s\t= %2ld", str, (unsigned long) argp);
Glenn L McGrath07085852003-10-09 07:28:22 +0000496 on_off((unsigned long) argp);
497 }
498}
Eric Andersen416c2422003-12-12 00:08:57 +0000499
500#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000501static void print_ascii(uint16_t *p, uint8_t length);
502
503static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
504{
Rob Landleya3e4f382006-04-29 16:06:31 +0000505 if (val[i])
Glenn L McGrath07085852003-10-09 07:28:22 +0000506 {
507 printf("\t%-20s",string);
508 print_ascii(&val[i], n);
509 }
510}
Eric Andersen416c2422003-12-12 00:08:57 +0000511#endif
Glenn L McGrath07085852003-10-09 07:28:22 +0000512/* end of busybox specific stuff */
513
Eric Andersen3443bd72003-07-22 07:30:36 +0000514#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000515static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
516{
Eric Andersen3443bd72003-07-22 07:30:36 +0000517 uint16_t ii;
518 uint8_t err_dma = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000519
Rob Landleya3e4f382006-04-29 16:06:31 +0000520 for (ii = 0; ii <= MODE_MAX; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000521 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000522 if (mode_sel & 0x0001)
Glenn L McGrath07085852003-10-09 07:28:22 +0000523 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000524 printf("*%cdma%u ",cc,ii);
Rob Landleya3e4f382006-04-29 16:06:31 +0000525 if (*have_mode)
Glenn L McGrath07085852003-10-09 07:28:22 +0000526 err_dma = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000527 *have_mode = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000528 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000529 else if (mode_sup & 0x0001)
Glenn L McGrath07085852003-10-09 07:28:22 +0000530 printf("%cdma%u ",cc,ii);
531
532 mode_sup >>=1;
533 mode_sel >>=1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000534 }
535 return err_dma;
536}
Glenn L McGrath07085852003-10-09 07:28:22 +0000537
Eric Andersen3443bd72003-07-22 07:30:36 +0000538static void print_ascii(uint16_t *p, uint8_t length) {
539 uint8_t ii;
540 char cl;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000541
Eric Andersen3443bd72003-07-22 07:30:36 +0000542 /* find first non-space & print it */
Rob Landleya3e4f382006-04-29 16:06:31 +0000543 for (ii = 0; ii< length; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000544 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000545 if (((char) 0x00ff&((*p)>>8)) != ' ')
Glenn L McGrath07085852003-10-09 07:28:22 +0000546 break;
Rob Landleya3e4f382006-04-29 16:06:31 +0000547 if ((cl = (char) 0x00ff&(*p)) != ' ')
Glenn L McGrath07085852003-10-09 07:28:22 +0000548 {
Rob Landleyadde7982006-05-16 15:32:30 +0000549 if (cl != '\0') printf("%c",cl);
Glenn L McGrath07085852003-10-09 07:28:22 +0000550 p++;
551 ii++;
Eric Andersen3443bd72003-07-22 07:30:36 +0000552 break;
553 }
554 p++;
555 }
556 /* print the rest */
Rob Landleya3e4f382006-04-29 16:06:31 +0000557 for (; ii< length; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000558 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000559 if (!(*p))
Glenn L McGrath07085852003-10-09 07:28:22 +0000560 break; /* some older devices have NULLs */
Eric Andersen3443bd72003-07-22 07:30:36 +0000561 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
562 p++;
563 }
564 printf("\n");
565}
566
Rob Landley0753f4a2006-06-07 00:27:25 +0000567// Parse 512 byte disk identification block and print much crap.
568
Rob Landley6389ff12006-05-01 19:28:53 +0000569static void identify(uint16_t *id_supplied)
Eric Andersen3443bd72003-07-22 07:30:36 +0000570{
Rob Landley5bc3f052006-04-29 19:11:24 +0000571 uint16_t buf[256];
572 uint16_t *val, ii, jj, kk;
Eric Andersen3443bd72003-07-22 07:30:36 +0000573 uint16_t like_std = 1, std = 0, min_std = 0xffff;
574 uint16_t dev = NO_DEV, eqpt = NO_DEV;
575 uint8_t have_mode = 0, err_dma = 0;
576 uint8_t chksum = 0;
577 uint32_t ll, mm, nn, oo;
Rob Landley2e2d7522006-04-29 15:23:33 +0000578 uint64_t bbbig; /* (:) */
Rob Landleyadde7982006-05-16 15:32:30 +0000579 const char *strng;
Eric Andersen3443bd72003-07-22 07:30:36 +0000580
Rob Landley0753f4a2006-06-07 00:27:25 +0000581 // Adjust for endianness if necessary.
582
Rob Landley5bc3f052006-04-29 19:11:24 +0000583 if (BB_BIG_ENDIAN) {
584 swab(id_supplied, buf, sizeof(buf));
585 val = buf;
586 } else val = id_supplied;
587
Eric Andersen3443bd72003-07-22 07:30:36 +0000588 chksum &= 0xff;
589
590 /* check if we recognise the device type */
591 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000592 if(!(val[GEN_CONFIG] & NOT_ATA))
593 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000594 dev = ATA_DEV;
595 printf("ATA device, with ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000596 }
597 else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
598 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000599 dev = ATA_DEV;
600 like_std = 4;
601 printf("CompactFlash ATA device, with ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000602 }
603 else if(!(val[GEN_CONFIG] & NOT_ATAPI))
604 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000605 dev = ATAPI_DEV;
606 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
607 printf("ATAPI %s, with ", pkt_str[eqpt]);
608 like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000609 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000610 else
611 /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
612 bb_error_msg_and_die("Unknown device type");
Eric Andersen3443bd72003-07-22 07:30:36 +0000613
Rob Landleyadde7982006-05-16 15:32:30 +0000614 printf("%sremovable media\n", !(val[GEN_CONFIG] & MEDIA_REMOVABLE) ? "non-" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +0000615 /* Info from the specific configuration word says whether or not the
616 * ID command completed correctly. It is only defined, however in
Glenn L McGrath07085852003-10-09 07:28:22 +0000617 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
Eric Andersen3443bd72003-07-22 07:30:36 +0000618 * standards. Since the values allowed for this word are extremely
619 * specific, it should be safe to check it now, even though we don't
620 * know yet what standard this device is using.
621 */
Rob Landleya3e4f382006-04-29 16:06:31 +0000622 if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
Glenn L McGrath07085852003-10-09 07:28:22 +0000623 (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
624 {
625 like_std = 5;
Rob Landleyadde7982006-05-16 15:32:30 +0000626 if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL))
627 printf("powers-up in standby; SET FEATURES subcmd spins-up.\n");
628 if(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE))
629 printf("\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000630 }
631
632 /* output the model and serial numbers and the fw revision */
Glenn L McGrath07085852003-10-09 07:28:22 +0000633 xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
634 xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
635 xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
636 xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
637 xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
Eric Andersen3443bd72003-07-22 07:30:36 +0000638
639 /* major & minor standards version number (Note: these words were not
640 * defined until ATA-3 & the CDROM std uses different words.) */
641 printf("Standards:");
Rob Landleya3e4f382006-04-29 16:06:31 +0000642 if (eqpt != CDROM)
Glenn L McGrath07085852003-10-09 07:28:22 +0000643 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000644 if (val[MINOR] && (val[MINOR] <= MINOR_MAX))
Glenn L McGrath07085852003-10-09 07:28:22 +0000645 {
Rob Landleyadde7982006-05-16 15:32:30 +0000646 if(like_std < 3) like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000647 std = actual_ver[val[MINOR]];
Rob Landleyadde7982006-05-16 15:32:30 +0000648 if(std) printf("\n\tUsed: %s ",minor_str[val[MINOR]]);
Glenn L McGrath07085852003-10-09 07:28:22 +0000649
Eric Andersen3443bd72003-07-22 07:30:36 +0000650 }
651 /* looks like when they up-issue the std, they obsolete one;
Glenn L McGrath07085852003-10-09 07:28:22 +0000652 * thus, only the newest 4 issues need be supported. (That's
Eric Andersen3443bd72003-07-22 07:30:36 +0000653 * what "kk" and "min_std" are all about.) */
Rob Landleya3e4f382006-04-29 16:06:31 +0000654 if (val[MAJOR] && (val[MAJOR] !=NOVAL_1))
Glenn L McGrath07085852003-10-09 07:28:22 +0000655 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000656 printf("\n\tSupported: ");
657 jj = val[MAJOR] << 1;
658 kk = like_std >4 ? like_std-4: 0;
Rob Landleya3e4f382006-04-29 16:06:31 +0000659 for (ii = 14; (ii >0)&&(ii>kk); ii--)
Glenn L McGrath07085852003-10-09 07:28:22 +0000660 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000661 if (jj & 0x8000)
Glenn L McGrath07085852003-10-09 07:28:22 +0000662 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000663 printf("%u ", ii);
Rob Landleya3e4f382006-04-29 16:06:31 +0000664 if (like_std < ii)
Glenn L McGrath07085852003-10-09 07:28:22 +0000665 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000666 like_std = ii;
667 kk = like_std >4 ? like_std-4: 0;
668 }
Rob Landleyadde7982006-05-16 15:32:30 +0000669 if(min_std > ii) min_std = ii;
Eric Andersen3443bd72003-07-22 07:30:36 +0000670 }
671 jj <<= 1;
672 }
Rob Landleyadde7982006-05-16 15:32:30 +0000673 if(like_std < 3) like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000674 }
675 /* Figure out what standard the device is using if it hasn't told
676 * us. If we know the std, check if the device is using any of
677 * the words from the next level up. It happens.
678 */
Rob Landleyadde7982006-05-16 15:32:30 +0000679 if(like_std < std) like_std = std;
Glenn L McGrath07085852003-10-09 07:28:22 +0000680
Rob Landleya3e4f382006-04-29 16:06:31 +0000681 if (((std == 5) || (!std && (like_std < 6))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000682 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
683 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
684 ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
685 ( val[CMDS_SUPP_2] & CMDS_W84) ) ) )
686 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000687 like_std = 6;
Glenn L McGrath07085852003-10-09 07:28:22 +0000688 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000689 else if (((std == 4) || (!std && (like_std < 5))) &&
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000690 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
Glenn L McGrath07085852003-10-09 07:28:22 +0000691 (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
692 ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
693 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
694 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000695 like_std = 5;
Glenn L McGrath07085852003-10-09 07:28:22 +0000696 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000697 else if (((std == 3) || (!std && (like_std < 4))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000698 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
699 ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
700 (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
701 (( val[CAPAB_1] & VALID) == VALID_VAL) ||
702 (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
703 (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
704 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000705 like_std = 4;
Glenn L McGrath07085852003-10-09 07:28:22 +0000706 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000707 else if (((std == 2) || (!std && (like_std < 3))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000708 ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
709 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000710 like_std = 3;
Glenn L McGrath07085852003-10-09 07:28:22 +0000711 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000712 else if (((std == 1) || (!std && (like_std < 2))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000713 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
714 (val[WHATS_VALID] & OK_W64_70)) )
715 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000716 like_std = 2;
717 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000718 if (!std)
Eric Andersen3443bd72003-07-22 07:30:36 +0000719 printf("\n\tLikely used: %u\n",like_std);
Rob Landleya3e4f382006-04-29 16:06:31 +0000720 else if (like_std > std)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000721 printf("& some of %u\n",like_std);
Glenn L McGrath07085852003-10-09 07:28:22 +0000722 else
723 printf("\n");
724 }
725 else
726 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000727 /* TBD: do CDROM stuff more thoroughly. For now... */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000728 kk = 0;
Rob Landleya3e4f382006-04-29 16:06:31 +0000729 if (val[CDR_MINOR] == 9)
Glenn L McGrath07085852003-10-09 07:28:22 +0000730 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000731 kk = 1;
732 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
733 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000734 if (val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
Glenn L McGrath07085852003-10-09 07:28:22 +0000735 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000736 kk = 1;
737 printf("\n\tSupported: CD-ROM ATAPI");
738 jj = val[CDR_MAJOR] >> 1;
Rob Landleya3e4f382006-04-29 16:06:31 +0000739 for (ii = 1; ii <15; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000740 {
Rob Landleyadde7982006-05-16 15:32:30 +0000741 if(jj & 0x0001) printf("-%u ", ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000742 jj >>= 1;
743 }
744 }
Rob Landleyadde7982006-05-16 15:32:30 +0000745 printf("%s\n", (!kk) ? "\n\tLikely used CD-ROM ATAPI-1" : "" );
Eric Andersen3443bd72003-07-22 07:30:36 +0000746 /* the cdrom stuff is more like ATA-2 than anything else, so: */
747 like_std = 2;
748 }
749
Rob Landleya3e4f382006-04-29 16:06:31 +0000750 if (min_std == 0xffff)
Glenn L McGrath07085852003-10-09 07:28:22 +0000751 min_std = like_std > 4 ? like_std - 3 : 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000752
753 printf("Configuration:\n");
754 /* more info from the general configuration word */
Rob Landleya3e4f382006-04-29 16:06:31 +0000755 if ((eqpt != CDROM) && (like_std == 1))
Glenn L McGrath07085852003-10-09 07:28:22 +0000756 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000757 jj = val[GEN_CONFIG] >> 1;
Rob Landleya3e4f382006-04-29 16:06:31 +0000758 for (ii = 1; ii < 15; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000759 {
Rob Landleyadde7982006-05-16 15:32:30 +0000760 if(jj & 0x0001) printf("\t%s\n",ata1_cfg_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000761 jj >>=1;
762 }
763 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000764 if (dev == ATAPI_DEV)
Glenn L McGrath07085852003-10-09 07:28:22 +0000765 {
Rob Landleyadde7982006-05-16 15:32:30 +0000766 if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_3MS_VAL)
767 strng = "3ms";
768 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_INTR_VAL)
769 strng = "<=10ms with INTRQ";
770 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_50US_VAL)
771 strng ="50us";
772 else
773 strng = "Unknown";
774 printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */
775
776 if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_12_VAL)
777 strng = "12 bytes";
778 else if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_16_VAL)
779 strng = "16 bytes";
780 else
781 strng = "Unknown";
782 puts(strng);
Glenn L McGrath07085852003-10-09 07:28:22 +0000783 }
784 else
785 {
786 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000787 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
788 mm = 0; bbbig = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000789 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000790 printf("\tCHS addressing not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000791 else
792 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000793 jj = val[WHATS_VALID] & OK_W54_58;
Glenn L McGrath07085852003-10-09 07:28:22 +0000794 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",
795 val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
796
Rob Landleyadde7982006-05-16 15:32:30 +0000797 if ((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES]))
798 printf("\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
Glenn L McGrath07085852003-10-09 07:28:22 +0000799
Rob Landleya3e4f382006-04-29 16:06:31 +0000800 if (jj)
Glenn L McGrath07085852003-10-09 07:28:22 +0000801 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000802 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
Rob Landleya3e4f382006-04-29 16:06:31 +0000803 if (like_std < 3)
Glenn L McGrath07085852003-10-09 07:28:22 +0000804 {
805 /* check Endian of capacity bytes */
Eric Andersen3443bd72003-07-22 07:30:36 +0000806 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
807 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
Rob Landleya3e4f382006-04-29 16:06:31 +0000808 if (abs(mm - nn) > abs(oo - nn))
Eric Andersen3443bd72003-07-22 07:30:36 +0000809 mm = oo;
810 }
811 printf("\tCHS current addressable sectors:%11u\n",mm);
Glenn L McGrath07085852003-10-09 07:28:22 +0000812 }
Eric Andersen3443bd72003-07-22 07:30:36 +0000813 }
814 /* LBA addressing */
815 printf("\tLBA user addressable sectors:%11u\n",ll);
Rob Landleya3e4f382006-04-29 16:06:31 +0000816 if ( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000817 (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
818 {
Rob Landley2e2d7522006-04-29 15:23:33 +0000819 bbbig = (uint64_t)val[LBA_64_MSB] << 48 |
820 (uint64_t)val[LBA_48_MSB] << 32 |
821 (uint64_t)val[LBA_MID] << 16 |
Glenn L McGrath07085852003-10-09 07:28:22 +0000822 val[LBA_LSB] ;
Rob Landley81dab2c2006-05-28 01:56:08 +0000823 printf("\tLBA48 user addressable sectors:%11"PRIu64"\n",bbbig);
Eric Andersen3443bd72003-07-22 07:30:36 +0000824 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000825
826 if (!bbbig)
Rob Landley2e2d7522006-04-29 15:23:33 +0000827 bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
Rob Landley81dab2c2006-05-28 01:56:08 +0000828 printf("\tdevice size with M = 1024*1024: %11"PRIu64" MBytes\n",bbbig>>11);
Eric Andersen3443bd72003-07-22 07:30:36 +0000829 bbbig = (bbbig<<9)/1000000;
Rob Landley81dab2c2006-05-28 01:56:08 +0000830 printf("\tdevice size with M = 1000*1000: %11"PRIu64" MBytes ",bbbig);
Glenn L McGrath07085852003-10-09 07:28:22 +0000831
Rob Landleyadde7982006-05-16 15:32:30 +0000832 if (bbbig > 1000)
Rob Landley81dab2c2006-05-28 01:56:08 +0000833 printf("(%"PRIu64" GB)\n", bbbig/1000);
Rob Landleyadde7982006-05-16 15:32:30 +0000834 else
835 printf("\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000836 }
837
838 /* hw support of commands (capabilities) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000839 printf("Capabilities:\n\t");
840
Rob Landleya3e4f382006-04-29 16:06:31 +0000841 if (dev == ATAPI_DEV)
Glenn L McGrath07085852003-10-09 07:28:22 +0000842 {
Rob Landleyadde7982006-05-16 15:32:30 +0000843 if (eqpt != CDROM && (val[CAPAB_0] & CMD_Q_SUP)) printf("Cmd queuing, ");
844 if (val[CAPAB_0] & OVLP_SUP) printf("Cmd overlap, ");
Eric Andersen3443bd72003-07-22 07:30:36 +0000845 }
Rob Landleyadde7982006-05-16 15:32:30 +0000846 if (val[CAPAB_0] & LBA_SUP) printf("LBA, ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000847
Rob Landleya3e4f382006-04-29 16:06:31 +0000848 if (like_std != 1)
Glenn L McGrath07085852003-10-09 07:28:22 +0000849 {
Rob Landleyadde7982006-05-16 15:32:30 +0000850 printf("IORDY%s(can%s be disabled)\n",
851 !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "",
852 (val[CAPAB_0] & IORDY_OFF) ? "" :"not");
Glenn L McGrath07085852003-10-09 07:28:22 +0000853 }
854 else
855 printf("no IORDY\n");
856
Rob Landleya3e4f382006-04-29 16:06:31 +0000857 if ((like_std == 1) && val[BUF_TYPE])
Glenn L McGrath07085852003-10-09 07:28:22 +0000858 {
Rob Landleyadde7982006-05-16 15:32:30 +0000859 printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE],
860 (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector",
861 (val[BUF_TYPE] > 2) ? " with read caching ability" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +0000862 }
Rob Landleyadde7982006-05-16 15:32:30 +0000863
Rob Landleya3e4f382006-04-29 16:06:31 +0000864 if ((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
Glenn L McGrath07085852003-10-09 07:28:22 +0000865 {
Rob Landleyadde7982006-05-16 15:32:30 +0000866 printf("\tBuffer size: %.1fkB\n",(float)val[BUFFER__SIZE]/2);
Eric Andersen3443bd72003-07-22 07:30:36 +0000867 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000868 if ((min_std < 4) && (val[RW_LONG]))
Glenn L McGrath07085852003-10-09 07:28:22 +0000869 {
Rob Landleyadde7982006-05-16 15:32:30 +0000870 printf("\tbytes avail on r/w long: %u\n",val[RW_LONG]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000871 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000872 if ((eqpt != CDROM) && (like_std > 3))
Glenn L McGrath07085852003-10-09 07:28:22 +0000873 {
Rob Landleyadde7982006-05-16 15:32:30 +0000874 printf("\tQueue depth: %u\n",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
Eric Andersen3443bd72003-07-22 07:30:36 +0000875 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000876
Rob Landleya3e4f382006-04-29 16:06:31 +0000877 if (dev == ATA_DEV)
Glenn L McGrath07085852003-10-09 07:28:22 +0000878 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000879 if (like_std == 1)
Glenn L McGrath07085852003-10-09 07:28:22 +0000880 printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
881 else
882 {
Rob Landleyadde7982006-05-16 15:32:30 +0000883 printf("\tStandby timer values: spec'd by %s", (val[CAPAB_0] & STD_STBY) ? "Standard" : "Vendor");
Rob Landleya3e4f382006-04-29 16:06:31 +0000884 if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
Glenn L McGrath07085852003-10-09 07:28:22 +0000885 printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
886 else
887 printf("\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000888 }
889 printf("\tR/W multiple sector transfer: ");
Rob Landleya3e4f382006-04-29 16:06:31 +0000890 if ((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
Eric Andersen3443bd72003-07-22 07:30:36 +0000891 printf("not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000892 else
893 {
894 printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
Rob Landleyadde7982006-05-16 15:32:30 +0000895 if (val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
896 printf("%u\n", val[SECTOR_XFER_CUR] & SECTOR_XFER);
897 else
898 printf("?\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000899 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000900 if ((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
Glenn L McGrath07085852003-10-09 07:28:22 +0000901 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000902 /* We print out elsewhere whether the APM feature is enabled or
903 not. If it's not enabled, let's not repeat the info; just print
904 nothing here. */
Glenn L McGrath07085852003-10-09 07:28:22 +0000905 printf("\tAdvancedPM level: ");
906 if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
907 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000908 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
Eric Andersen3443bd72003-07-22 07:30:36 +0000909 printf("%u (0x%x)\n", apm_level, apm_level);
Glenn L McGrath07085852003-10-09 07:28:22 +0000910 }
911 else
Eric Andersen3443bd72003-07-22 07:30:36 +0000912 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000913 }
Rob Landleyadde7982006-05-16 15:32:30 +0000914 if (like_std > 5 && val[ACOUSTIC]) {
915 printf("\tRecommended acoustic management value: %u, current value: %u\n",
Glenn L McGrath07085852003-10-09 07:28:22 +0000916 (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
Eric Andersen3443bd72003-07-22 07:30:36 +0000917 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000918 }
919 else
920 {
921 /* ATAPI */
Rob Landleyadde7982006-05-16 15:32:30 +0000922 if (eqpt != CDROM && (val[CAPAB_0] & SWRST_REQ))
923 printf("\tATA sw reset required\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000924
Rob Landleya3e4f382006-04-29 16:06:31 +0000925 if (val[PKT_REL] || val[SVC_NBSY])
Glenn L McGrath07085852003-10-09 07:28:22 +0000926 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000927 printf("\tOverlap support:");
Rob Landleyadde7982006-05-16 15:32:30 +0000928 if (val[PKT_REL]) printf(" %uus to release bus.",val[PKT_REL]);
929 if (val[SVC_NBSY]) printf(" %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000930 printf("\n");
931 }
932 }
933
934 /* DMA stuff. Check that only one DMA mode is selected. */
935 printf("\tDMA: ");
Rob Landleya3e4f382006-04-29 16:06:31 +0000936 if (!(val[CAPAB_0] & DMA_SUP))
Eric Andersen3443bd72003-07-22 07:30:36 +0000937 printf("not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000938 else
939 {
Rob Landleyadde7982006-05-16 15:32:30 +0000940 if (val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
941 printf(" sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
Rob Landleya3e4f382006-04-29 16:06:31 +0000942 if (val[SINGLE_DMA])
Glenn L McGrath07085852003-10-09 07:28:22 +0000943 {
944 jj = val[SINGLE_DMA];
945 kk = val[SINGLE_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +0000946 err_dma += mode_loop(jj,kk,'s',&have_mode);
947 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000948 if (val[MULTI_DMA])
Glenn L McGrath07085852003-10-09 07:28:22 +0000949 {
950 jj = val[MULTI_DMA];
951 kk = val[MULTI_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +0000952 err_dma += mode_loop(jj,kk,'m',&have_mode);
953 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000954 if ((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
Glenn L McGrath07085852003-10-09 07:28:22 +0000955 {
956 jj = val[ULTRA_DMA];
957 kk = val[ULTRA_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +0000958 err_dma += mode_loop(jj,kk,'u',&have_mode);
959 }
Rob Landleyadde7982006-05-16 15:32:30 +0000960 if (err_dma || !have_mode) printf("(?)");
Eric Andersen3443bd72003-07-22 07:30:36 +0000961 printf("\n");
962
Rob Landleyadde7982006-05-16 15:32:30 +0000963 if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
964 printf("\t\tInterleaved DMA support\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000965
Rob Landleya3e4f382006-04-29 16:06:31 +0000966 if ((val[WHATS_VALID] & OK_W64_70) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000967 (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
968 {
Rob Landleyadde7982006-05-16 15:32:30 +0000969 printf("\t\tCycle time:");
970 if (val[DMA_TIME_MIN]) printf(" min=%uns",val[DMA_TIME_MIN]);
971 if (val[DMA_TIME_NORM]) printf(" recommended=%uns",val[DMA_TIME_NORM]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000972 printf("\n");
973 }
974 }
975
976 /* Programmed IO stuff */
977 printf("\tPIO: ");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000978 /* If a drive supports mode n (e.g. 3), it also supports all modes less
Eric Andersen3443bd72003-07-22 07:30:36 +0000979 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
Rob Landleya3e4f382006-04-29 16:06:31 +0000980 if ((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
Glenn L McGrath07085852003-10-09 07:28:22 +0000981 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000982 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
Rob Landleya3e4f382006-04-29 16:06:31 +0000983 for (ii = 0; ii <= PIO_MODE_MAX ; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000984 {
Rob Landleyadde7982006-05-16 15:32:30 +0000985 if (jj & 0x0001) printf("pio%d ",ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000986 jj >>=1;
987 }
988 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000989 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000990 else if (((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
Glenn L McGrath07085852003-10-09 07:28:22 +0000991 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000992 for (ii = 0; ii <= val[PIO_MODE]>>8; ii++)
Eric Andersen3443bd72003-07-22 07:30:36 +0000993 printf("pio%d ",ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000994 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000995 }
996 else
997 printf("unknown\n");
998
Rob Landleya3e4f382006-04-29 16:06:31 +0000999 if (val[WHATS_VALID] & OK_W64_70)
Glenn L McGrath07085852003-10-09 07:28:22 +00001000 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001001 if (val[PIO_NO_FLOW] || val[PIO_FLOW])
Glenn L McGrath07085852003-10-09 07:28:22 +00001002 {
Rob Landleyadde7982006-05-16 15:32:30 +00001003 printf("\t\tCycle time:");
1004 if (val[PIO_NO_FLOW]) printf(" no flow control=%uns", val[PIO_NO_FLOW]);
1005 if (val[PIO_FLOW]) printf(" IORDY flow control=%uns", val[PIO_FLOW]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001006 printf("\n");
1007 }
1008 }
1009
Rob Landleya3e4f382006-04-29 16:06:31 +00001010 if ((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
Glenn L McGrath07085852003-10-09 07:28:22 +00001011 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001012 printf("Commands/features:\n\tEnabled\tSupported:\n");
1013 jj = val[CMDS_SUPP_0];
1014 kk = val[CMDS_EN_0];
Rob Landleya3e4f382006-04-29 16:06:31 +00001015 for (ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +00001016 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001017 if ((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
Glenn L McGrath07085852003-10-09 07:28:22 +00001018 {
Rob Landleyadde7982006-05-16 15:32:30 +00001019 printf("\t%s\t%s\n", (kk & 0x8000) ? " *" : "", cmd_feat_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001020 }
1021 jj <<=1; kk<<=1;
Rob Landleya3e4f382006-04-29 16:06:31 +00001022 if (ii%16 == 15)
Glenn L McGrath07085852003-10-09 07:28:22 +00001023 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001024 jj = val[CMDS_SUPP_0+1+(ii/16)];
1025 kk = val[CMDS_EN_0+1+(ii/16)];
1026 }
Rob Landleya3e4f382006-04-29 16:06:31 +00001027 if (ii == 31)
Glenn L McGrath07085852003-10-09 07:28:22 +00001028 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001029 if ((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
Glenn L McGrath07085852003-10-09 07:28:22 +00001030 ii +=16;
Eric Andersen3443bd72003-07-22 07:30:36 +00001031 }
1032 }
1033 }
Rob Landleyadde7982006-05-16 15:32:30 +00001034 /* Removable Media Status Notification feature set */
1035 if((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
1036 printf("\t%s supported\n", cmd_feat_str[27]);
Glenn L McGrath07085852003-10-09 07:28:22 +00001037
1038
Eric Andersen3443bd72003-07-22 07:30:36 +00001039 /* security */
Rob Landleya3e4f382006-04-29 16:06:31 +00001040 if ((eqpt != CDROM) && (like_std > 3) &&
Glenn L McGrath07085852003-10-09 07:28:22 +00001041 (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1042 {
Rob Landleyade7f952006-05-25 18:53:06 +00001043 printf("Security:\n");
Rob Landleyadde7982006-05-16 15:32:30 +00001044 if (val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
1045 printf("\tMaster password revision code = %u\n",val[PSWD_CODE]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001046 jj = val[SECU_STATUS];
Rob Landleya3e4f382006-04-29 16:06:31 +00001047 if (jj)
Glenn L McGrath07085852003-10-09 07:28:22 +00001048 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001049 for (ii = 0; ii < NUM_SECU_STR; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +00001050 {
Rob Landleyadde7982006-05-16 15:32:30 +00001051 printf("\t%s\t%s\n", (!(jj & 0x0001)) ? "not" : "", secu_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001052 jj >>=1;
1053 }
Rob Landleya3e4f382006-04-29 16:06:31 +00001054 if (val[SECU_STATUS] & SECU_ENABLED)
Glenn L McGrath07085852003-10-09 07:28:22 +00001055 {
Rob Landleyadde7982006-05-16 15:32:30 +00001056 printf("\tSecurity level %s\n", (val[SECU_STATUS] & SECU_LEVEL) ? "maximum" : "high");
Eric Andersen3443bd72003-07-22 07:30:36 +00001057 }
1058 }
1059 jj = val[ERASE_TIME] & ERASE_BITS;
1060 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
Rob Landleya3e4f382006-04-29 16:06:31 +00001061 if (jj || kk)
Glenn L McGrath07085852003-10-09 07:28:22 +00001062 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001063 printf("\t");
Rob Landleyade7f952006-05-25 18:53:06 +00001064 if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, "");
1065 if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001066 printf("\n");
1067 }
1068 }
1069
1070 /* reset result */
Rob Landleyadde7982006-05-16 15:32:30 +00001071 jj = val[HWRST_RSLT];
1072 if ((jj & VALID) == VALID_VAL)
Glenn L McGrath07085852003-10-09 07:28:22 +00001073 {
Rob Landleyadde7982006-05-16 15:32:30 +00001074 if (!(oo = (jj & RST0)))
1075 jj >>= 8;
Rob Landleya3e4f382006-04-29 16:06:31 +00001076 if ((jj & DEV_DET) == JUMPER_VAL)
Rob Landleyadde7982006-05-16 15:32:30 +00001077 strng = " determined by the jumper";
Rob Landleya3e4f382006-04-29 16:06:31 +00001078 else if ((jj & DEV_DET) == CSEL_VAL)
Rob Landleyadde7982006-05-16 15:32:30 +00001079 strng = " determined by CSEL";
1080 else
1081 strng = "";
1082 printf("HW reset results:\n\tCBLID- %s Vih\n\tDevice num = %i%s\n",
1083 (val[HWRST_RSLT] & CBLID) ? "above" : "below", !(oo), strng);
Eric Andersen3443bd72003-07-22 07:30:36 +00001084 }
1085
1086 /* more stuff from std 5 */
Rob Landleya3e4f382006-04-29 16:06:31 +00001087 if ((like_std > 4) && (eqpt != CDROM))
Glenn L McGrath07085852003-10-09 07:28:22 +00001088 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001089 if (val[CFA_PWR_MODE] & VALID_W160)
Glenn L McGrath07085852003-10-09 07:28:22 +00001090 {
Rob Landleyadde7982006-05-16 15:32:30 +00001091 printf("CFA power mode 1:\n\t%s%s\n", (val[CFA_PWR_MODE] & PWR_MODE_OFF) ? "disabled" : "enabled",
1092 (val[CFA_PWR_MODE] & PWR_MODE_REQ) ? " and required by some commands" : "");
Glenn L McGrath07085852003-10-09 07:28:22 +00001093
Rob Landleyadde7982006-05-16 15:32:30 +00001094 if (val[CFA_PWR_MODE] & MAX_AMPS) printf("\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
Eric Andersen3443bd72003-07-22 07:30:36 +00001095 }
Rob Landleya3e4f382006-04-29 16:06:31 +00001096 if ((val[INTEGRITY] & SIG) == SIG_VAL)
Glenn L McGrath07085852003-10-09 07:28:22 +00001097 {
Rob Landleyadde7982006-05-16 15:32:30 +00001098 printf("Checksum: %scorrect\n", chksum ? "in" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +00001099 }
1100 }
1101
Rob Landleyadde7982006-05-16 15:32:30 +00001102 exit(EXIT_SUCCESS);
Eric Andersen3443bd72003-07-22 07:30:36 +00001103}
1104#endif
1105
Rob Landleyade7f952006-05-25 18:53:06 +00001106static int get_identity, get_geom;
1107static int do_flush;
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001108static int do_ctimings, do_timings;
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001109static unsigned long set_readahead, get_readahead, Xreadahead;
1110static unsigned long set_readonly, get_readonly, readonly;
1111static unsigned long set_unmask, get_unmask, unmask;
1112static unsigned long set_mult, get_mult, mult;
Eric Andersenf1c56a92003-08-08 22:13:41 +00001113#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001114static unsigned long set_dma, get_dma, dma;
Eric Andersenf1c56a92003-08-08 22:13:41 +00001115#endif
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001116static unsigned long set_dma_q, get_dma_q, dma_q;
1117static unsigned long set_nowerr, get_nowerr, nowerr;
1118static unsigned long set_keep, get_keep, keep;
1119static unsigned long set_io32bit, get_io32bit, io32bit;
1120static unsigned long set_piomode, noisy_piomode;
1121static int piomode;
Eric Andersen3443bd72003-07-22 07:30:36 +00001122#ifdef HDIO_DRIVE_CMD
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001123static unsigned long set_dkeep, get_dkeep, dkeep;
1124static unsigned long set_standby, get_standby, standby_requested;
1125static unsigned long set_xfermode, get_xfermode;
1126static int xfermode_requested;
1127static unsigned long set_lookahead, get_lookahead, lookahead;
1128static unsigned long set_prefetch, get_prefetch, prefetch;
1129static unsigned long set_defects, get_defects, defects;
1130static unsigned long set_wcache, get_wcache, wcache;
1131static unsigned long set_doorlock, get_doorlock, doorlock;
1132static unsigned long set_seagate, get_seagate;
1133static unsigned long set_standbynow, get_standbynow;
1134static unsigned long set_sleepnow, get_sleepnow;
1135static unsigned long get_powermode;
1136static unsigned long set_apmmode, get_apmmode, apmmode;
Eric Andersen3443bd72003-07-22 07:30:36 +00001137#endif
1138#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001139static int get_IDentity;
Eric Andersen3443bd72003-07-22 07:30:36 +00001140#endif
1141#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
Rob Landley20deab02006-05-07 23:34:15 +00001142static unsigned long unregister_hwif;
1143static unsigned long hwif;
Eric Andersen3443bd72003-07-22 07:30:36 +00001144#endif
1145#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
Rob Landley20deab02006-05-07 23:34:15 +00001146static unsigned long scan_hwif;
1147static unsigned long hwif_data;
1148static unsigned long hwif_ctrl;
1149static unsigned long hwif_irq;
Eric Andersen3443bd72003-07-22 07:30:36 +00001150#endif
1151#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Rob Landley20deab02006-05-07 23:34:15 +00001152static unsigned long set_busstate, get_busstate, busstate;
Eric Andersen3443bd72003-07-22 07:30:36 +00001153#endif
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001154static int reread_partn;
Eric Andersen3443bd72003-07-22 07:30:36 +00001155
1156#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001157static int perform_reset;
Eric Andersen3443bd72003-07-22 07:30:36 +00001158#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1159#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Rob Landley20deab02006-05-07 23:34:15 +00001160static unsigned long perform_tristate, tristate;
Eric Andersen3443bd72003-07-22 07:30:36 +00001161#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1162
1163// Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1164// then the HDIO_GET_IDENTITY only returned 142 bytes.
1165// Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1166// and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1167// 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1168// (which they should, but they should just return -EINVAL).
1169//
1170// So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1171// On a really old system, it will not, and we will be confused.
1172// Too bad, really.
1173
1174#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001175static const char * const cfg_str[] =
Rob Landleyadde7982006-05-16 15:32:30 +00001176{ "", "HardSect", "SoftSect", "NotMFM",
1177 "HdSw>15uSec", "SpinMotCtl", "Fixed", "Removeable",
1178 "DTR<=5Mbs", "DTR>5Mbs", "DTR>10Mbs", "RotSpdTol>.5%",
1179 "dStbOff", "TrkOff", "FmtGapReq", "nonMagnetic"
Eric Andersen3443bd72003-07-22 07:30:36 +00001180};
1181
Rob Landleyadde7982006-05-16 15:32:30 +00001182static const char * const BuffType[] = {"Unknown", "1Sect", "DualPort", "DualPortCache"};
Eric Andersen3443bd72003-07-22 07:30:36 +00001183
Rob Landleya3e4f382006-04-29 16:06:31 +00001184static void dump_identity(const struct hd_driveid *id)
Eric Andersen3443bd72003-07-22 07:30:36 +00001185{
1186 int i;
Eric Andersen3443bd72003-07-22 07:30:36 +00001187 const unsigned short int *id_regs= (const void*) id;
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001188
Glenn L McGrath07085852003-10-09 07:28:22 +00001189 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1190 id->model, id->fw_rev, id->serial_no);
Rob Landleyadde7982006-05-16 15:32:30 +00001191 for (i=0; i<=15; i++) {
Rob Landleyade7f952006-05-25 18:53:06 +00001192 if (id->config & (1<<i))
1193 printf(" %s", cfg_str[i]);
Rob Landleyadde7982006-05-16 15:32:30 +00001194 }
1195 printf( " }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n"
1196 " BuffType=(%u) %s, BuffSize=%ukB, MaxMultSect=%u",
Glenn L McGrath07085852003-10-09 07:28:22 +00001197 id->cyls, id->heads, id->sectors, id->track_bytes,
Rob Landleyadde7982006-05-16 15:32:30 +00001198 id->sector_bytes, id->ecc_bytes,
1199 id->buf_type, BuffType[(id->buf_type > 3) ? 0 : id->buf_type],
1200 id->buf_size/2, id->max_multsect);
Glenn L McGrath07085852003-10-09 07:28:22 +00001201 if (id->max_multsect)
1202 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001203 printf(", MultSect=");
1204 if (!(id->multsect_valid&1))
1205 printf("?%u?", id->multsect);
1206 else if (id->multsect)
1207 printf("%u", id->multsect);
1208 else
1209 printf("off");
1210 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001211 printf("\n");
Rob Landleyade7f952006-05-25 18:53:06 +00001212
1213 if(!(id->field_valid&1))
1214 printf(" (maybe):");
1215
1216 printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
1217 id->cur_sectors,
1218 (BB_BIG_ENDIAN) ?
1219 (long unsigned int)(id->cur_capacity0 << 16) | id->cur_capacity1 :
1220 (long unsigned int)(id->cur_capacity1 << 16) | id->cur_capacity0,
1221 ((id->capability&2) == 0) ? "no" : "yes");
1222
1223 if (id->capability&2)
1224 printf(", LBAsects=%u", id->lba_capacity);
1225
1226 printf("\n IORDY=%s", (id->capability&8) ? (id->capability&4) ? "on/off" : "yes" : "no");
1227
1228 if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&2)
1229 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1230
1231 if ((id->capability&1) && (id->field_valid&2))
1232 printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1233
1234 printf("\n PIO modes: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001235 if (id->tPIO <= 5)
1236 {
Rob Landleyade7f952006-05-25 18:53:06 +00001237 printf("pio0 ");
1238 if (id->tPIO >= 1) printf("pio1 ");
1239 if (id->tPIO >= 2) printf("pio2 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001240 }
Rob Landleyade7f952006-05-25 18:53:06 +00001241 if (id->field_valid&2)
1242 {
1243 if (id->eide_pio_modes & 1) printf("pio3 ");
1244 if (id->eide_pio_modes & 2) printf("pio4 ");
1245 if (id->eide_pio_modes &~3) printf("pio? ");
1246 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001247 if (id->capability&1)
1248 {
1249 if (id->dma_1word | id->dma_mword)
1250 {
Rob Landleyade7f952006-05-25 18:53:06 +00001251 printf("\n DMA modes: ");
1252 if (id->dma_1word & 0x100) printf("*");
1253 if (id->dma_1word & 1) printf("sdma0 ");
1254 if (id->dma_1word & 0x200) printf("*");
1255 if (id->dma_1word & 2) printf("sdma1 ");
1256 if (id->dma_1word & 0x400) printf("*");
1257 if (id->dma_1word & 4) printf("sdma2 ");
1258 if (id->dma_1word & 0xf800) printf("*");
1259 if (id->dma_1word & 0xf8) printf("sdma? ");
1260 if (id->dma_mword & 0x100) printf("*");
1261 if (id->dma_mword & 1) printf("mdma0 ");
1262 if (id->dma_mword & 0x200) printf("*");
1263 if (id->dma_mword & 2) printf("mdma1 ");
1264 if (id->dma_mword & 0x400) printf("*");
1265 if (id->dma_mword & 4) printf("mdma2 ");
1266 if (id->dma_mword & 0xf800) printf("*");
1267 if (id->dma_mword & 0xf8) printf("mdma? ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001268 }
1269 }
Rob Landleyade7f952006-05-25 18:53:06 +00001270 if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&4)
1271 {
1272 printf("\n UDMA modes: ");
1273 if (id->dma_ultra & 0x100) printf("*");
1274 if (id->dma_ultra & 0x001) printf("udma0 ");
1275 if (id->dma_ultra & 0x200) printf("*");
1276 if (id->dma_ultra & 0x002) printf("udma1 ");
1277 if (id->dma_ultra & 0x400) printf("*");
1278 if (id->dma_ultra & 0x004) printf("udma2 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001279#ifdef __NEW_HD_DRIVE_ID
Rob Landleyade7f952006-05-25 18:53:06 +00001280 if (id->hw_config & 0x2000)
1281 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001282#else /* !__NEW_HD_DRIVE_ID */
Rob Landleyade7f952006-05-25 18:53:06 +00001283 if (id->word93 & 0x2000)
1284 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001285#endif /* __NEW_HD_DRIVE_ID */
Rob Landleyade7f952006-05-25 18:53:06 +00001286 if (id->dma_ultra & 0x0800) printf("*");
1287 if (id->dma_ultra & 0x0008) printf("udma3 ");
1288 if (id->dma_ultra & 0x1000) printf("*");
1289 if (id->dma_ultra & 0x0010) printf("udma4 ");
1290 if (id->dma_ultra & 0x2000) printf("*");
1291 if (id->dma_ultra & 0x0020) printf("udma5 ");
1292 if (id->dma_ultra & 0x4000) printf("*");
1293 if (id->dma_ultra & 0x0040) printf("udma6 ");
1294 if (id->dma_ultra & 0x8000) printf("*");
1295 if (id->dma_ultra & 0x0080) printf("udma7 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001296 }
1297 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001298 printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
1299 if (id_regs[83] & 8)
1300 {
1301 if (!(id_regs[86]&8))
1302 printf(": disabled (255)");
1303 else if ((id_regs[91]&0xFF00)!=0x4000)
1304 printf(": unknown setting");
1305 else
1306 printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1307 }
Rob Landleyade7f952006-05-25 18:53:06 +00001308 if (id_regs[82]&0x20)
1309 printf(" WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
Glenn L McGrath07085852003-10-09 07:28:22 +00001310#ifdef __NEW_HD_DRIVE_ID
1311 if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
1312 {
Rob Landleyadde7982006-05-16 15:32:30 +00001313 printf("\n Drive conforms to: %s: ", (id->minor_rev_num <= 31) ? minor_str[id->minor_rev_num] : "Unknown");
Rob Landley026147a2006-04-17 22:29:13 +00001314 if (id->major_rev_num != 0x0000 && /* NOVAL_0 */
1315 id->major_rev_num != 0xFFFF) { /* NOVAL_1 */
Rob Landleyadde7982006-05-16 15:32:30 +00001316 for (i=0; i <= 15; i++) {
1317 if (id->major_rev_num & (1<<i))
1318 printf(" ATA/ATAPI-%u", i);
1319 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001320 }
1321 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001322#endif /* __NEW_HD_DRIVE_ID */
Rob Landleyade7f952006-05-25 18:53:06 +00001323 printf("\n\n * current active mode\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001324}
1325#endif
1326
Rob Landleya3e4f382006-04-29 16:06:31 +00001327static void flush_buffer_cache(int fd)
Eric Andersen3443bd72003-07-22 07:30:36 +00001328{
Rob Landleya3e4f382006-04-29 16:06:31 +00001329 fsync(fd); /* flush buffers */
Glenn L McGrath07085852003-10-09 07:28:22 +00001330 bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
Eric Andersen3443bd72003-07-22 07:30:36 +00001331#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001332 sleep(1);
1333 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001334 bb_perror_msg("HDIO_DRIVE_CMD");
Eric Andersen3443bd72003-07-22 07:30:36 +00001335#endif
1336}
1337
Rob Landleya3e4f382006-04-29 16:06:31 +00001338static int seek_to_zero(int fd)
Eric Andersen3443bd72003-07-22 07:30:36 +00001339{
Glenn L McGrath07085852003-10-09 07:28:22 +00001340 if (lseek(fd, (off_t) 0, SEEK_SET))
Eric Andersen3443bd72003-07-22 07:30:36 +00001341 return 1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001342 return 0;
1343}
1344
Rob Landleya3e4f382006-04-29 16:06:31 +00001345static int read_big_block(int fd, char *buf)
Eric Andersen3443bd72003-07-22 07:30:36 +00001346{
Rob Landleyadde7982006-05-16 15:32:30 +00001347 int i;
1348
1349 if ((i = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES) {
1350 bb_error_msg("read(%d bytes) failed (rc=%d)", TIMING_BUF_BYTES, i);
Eric Andersen3443bd72003-07-22 07:30:36 +00001351 return 1;
1352 }
1353 /* access all sectors of buf to ensure the read fully completed */
1354 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1355 buf[i] &= 1;
1356 return 0;
1357}
1358
Rob Landleyadde7982006-05-16 15:32:30 +00001359static void print_timing(int t, double e)
1360{
1361 if (t >= e) /* more than 1MB/s */
Rob Landleyade7f952006-05-25 18:53:06 +00001362 printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e, 'M');
Rob Landleyadde7982006-05-16 15:32:30 +00001363 else
Rob Landleyade7f952006-05-25 18:53:06 +00001364 printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e * 1024, 'k');
Rob Landleyadde7982006-05-16 15:32:30 +00001365}
1366
1367static int do_blkgetsize (int fd, unsigned long long *blksize64)
1368{
1369 int rc;
1370 unsigned int blksize32 = 0;
1371
1372 if (0 == ioctl(fd, BLKGETSIZE64, blksize64)) { // returns bytes
1373 *blksize64 /= 512;
1374 return 0;
1375 }
1376 rc = ioctl(fd, BLKGETSIZE, &blksize32); // returns sectors
1377 if (rc)
1378 bb_perror_msg("BLKGETSIZE");
1379 *blksize64 = blksize32;
1380 return rc;
1381}
Eric Andersen50af12d2003-08-06 08:47:59 +00001382
Rob Landleya3e4f382006-04-29 16:06:31 +00001383static void do_time(int flag, int fd)
Glenn L McGrath07085852003-10-09 07:28:22 +00001384/*
1385 flag = 0 time_cache
1386 flag = 1 time_device
1387*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001388{
Eric Andersen3443bd72003-07-22 07:30:36 +00001389 struct itimerval e1, e2;
Rob Landleyadde7982006-05-16 15:32:30 +00001390 double elapsed, elapsed2;
1391 unsigned int max_iterations = 1024, total_MB, iterations;
1392 unsigned long long blksize;
Rob Landley4ae2f512006-05-19 17:24:26 +00001393 RESERVE_CONFIG_BUFFER(buf, TIMING_BUF_BYTES);
1394
1395 if (mlock(buf, TIMING_BUF_BYTES)) {
1396 bb_perror_msg("mlock");
1397 goto quit2;
1398 }
Rob Landleyadde7982006-05-16 15:32:30 +00001399
1400 if (0 == do_blkgetsize(fd, &blksize)) {
1401 max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB;
1402 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001403
Eric Andersen3443bd72003-07-22 07:30:36 +00001404 /* Clear out the device request queues & give them time to complete */
Rob Landleyadde7982006-05-16 15:32:30 +00001405 sync();
1406 sleep(3);
1407
1408 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00001409
Rob Landleya3e4f382006-04-29 16:06:31 +00001410 if (flag == 0) /* Time cache */
Glenn L McGrath07085852003-10-09 07:28:22 +00001411 {
Rob Landleyadde7982006-05-16 15:32:30 +00001412 if (seek_to_zero (fd)) return;
1413 if (read_big_block (fd, buf)) return;
1414 printf(" Timing cached reads: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001415 fflush(stdout);
Rob Landleyadde7982006-05-16 15:32:30 +00001416
1417 /* Now do the timing */
1418 iterations = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +00001419 getitimer(ITIMER_REAL, &e1);
Rob Landleyadde7982006-05-16 15:32:30 +00001420 do {
1421 ++iterations;
1422 if (seek_to_zero (fd) || read_big_block (fd, buf))
Glenn L McGrath07085852003-10-09 07:28:22 +00001423 goto quit;
Rob Landleyadde7982006-05-16 15:32:30 +00001424 getitimer(ITIMER_REAL, &e2);
1425 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1426 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1427 } while (elapsed < 2.0);
1428 total_MB = iterations * TIMING_BUF_MB;
Rob Landleyade7f952006-05-25 18:53:06 +00001429
Rob Landleyadde7982006-05-16 15:32:30 +00001430 /* Now remove the lseek() and getitimer() overheads from the elapsed time */
1431 getitimer(ITIMER_REAL, &e1);
1432 do {
1433 if (seek_to_zero (fd))
1434 goto quit;
1435 getitimer(ITIMER_REAL, &e2);
1436 elapsed2 = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1437 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1438 } while (--iterations);
1439
1440 elapsed -= elapsed2;
1441 print_timing(BUFCACHE_FACTOR * total_MB, elapsed);
Glenn L McGrath07085852003-10-09 07:28:22 +00001442 flush_buffer_cache(fd);
1443 sleep(1);
Eric Andersen50af12d2003-08-06 08:47:59 +00001444 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001445 else /* Time device */
1446 {
1447 printf(" Timing buffered disk reads: ");
1448 fflush(stdout);
Glenn L McGrath07085852003-10-09 07:28:22 +00001449 /*
1450 * getitimer() is used rather than gettimeofday() because
1451 * it is much more consistent (on my machine, at least).
1452 */
Glenn L McGrath07085852003-10-09 07:28:22 +00001453 /* Now do the timings for real */
Rob Landleyadde7982006-05-16 15:32:30 +00001454 iterations = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +00001455 getitimer(ITIMER_REAL, &e1);
Rob Landleyadde7982006-05-16 15:32:30 +00001456 do {
1457 ++iterations;
1458 if (read_big_block (fd, buf))
Glenn L McGrath07085852003-10-09 07:28:22 +00001459 goto quit;
Rob Landleyadde7982006-05-16 15:32:30 +00001460 getitimer(ITIMER_REAL, &e2);
1461 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1462 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1463 } while (elapsed < 3.0 && iterations < max_iterations);
1464
1465 total_MB = iterations * TIMING_BUF_MB;
1466 print_timing(total_MB, elapsed);
Glenn L McGrath07085852003-10-09 07:28:22 +00001467 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001468quit:
Rob Landley4ae2f512006-05-19 17:24:26 +00001469 munlock(buf, TIMING_BUF_BYTES);
1470quit2:
1471 RELEASE_CONFIG_BUFFER(buf);
Eric Andersen3443bd72003-07-22 07:30:36 +00001472}
1473
Eric Andersen3443bd72003-07-22 07:30:36 +00001474static void on_off (unsigned int value)
1475{
1476 printf(value ? " (on)\n" : " (off)\n");
1477}
1478
1479#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Rob Landleya3e4f382006-04-29 16:06:31 +00001480static void bus_state_value(unsigned int value)
Eric Andersen3443bd72003-07-22 07:30:36 +00001481{
Rob Landleyadde7982006-05-16 15:32:30 +00001482 if (value == BUSSTATE_ON)
1483 on_off(1);
1484 else if (value == BUSSTATE_OFF)
1485 on_off(0);
1486 else if (value == BUSSTATE_TRISTATE)
1487 printf(" (tristate)\n");
1488 else
1489 printf(" (unknown: %d)\n", value);
Eric Andersen3443bd72003-07-22 07:30:36 +00001490}
1491#endif
1492
1493#ifdef HDIO_DRIVE_CMD
Rob Landleya3e4f382006-04-29 16:06:31 +00001494static void interpret_standby(unsigned int standby)
Eric Andersen3443bd72003-07-22 07:30:36 +00001495{
Rob Landleyadde7982006-05-16 15:32:30 +00001496 unsigned int t;
1497
1498
1499 if (standby == 0)
1500 printf("off");
1501 else if (standby == 252)
1502 printf("21 minutes");
1503 else if (standby == 253)
1504 printf("vendor-specific");
1505 else if (standby == 254)
1506 printf("Reserved");
1507 else if (standby == 255)
1508 printf("21 minutes + 15 seconds");
1509 else {
1510 if (standby <= 240) {
1511 t = standby * 5;
1512 printf("%u minutes + %u seconds", t / 60, t % 60);
1513 } else if (standby <= 251) {
1514 t = (standby - 240) * 30;
1515 printf("%u hours + %u minutes", t / 60, t % 60);
1516 } else
1517 printf("illegal value");
Eric Andersen3443bd72003-07-22 07:30:36 +00001518 }
1519 printf(")\n");
1520}
1521
1522struct xfermode_entry {
1523 int val;
1524 const char *name;
1525};
1526
1527static const struct xfermode_entry xfermode_table[] = {
1528 { 8, "pio0" },
1529 { 9, "pio1" },
1530 { 10, "pio2" },
1531 { 11, "pio3" },
1532 { 12, "pio4" },
1533 { 13, "pio5" },
1534 { 14, "pio6" },
1535 { 15, "pio7" },
1536 { 16, "sdma0" },
1537 { 17, "sdma1" },
1538 { 18, "sdma2" },
1539 { 19, "sdma3" },
1540 { 20, "sdma4" },
1541 { 21, "sdma5" },
1542 { 22, "sdma6" },
1543 { 23, "sdma7" },
1544 { 32, "mdma0" },
1545 { 33, "mdma1" },
1546 { 34, "mdma2" },
1547 { 35, "mdma3" },
1548 { 36, "mdma4" },
1549 { 37, "mdma5" },
1550 { 38, "mdma6" },
1551 { 39, "mdma7" },
1552 { 64, "udma0" },
1553 { 65, "udma1" },
1554 { 66, "udma2" },
1555 { 67, "udma3" },
1556 { 68, "udma4" },
1557 { 69, "udma5" },
1558 { 70, "udma6" },
1559 { 71, "udma7" },
1560 { 0, NULL }
1561};
1562
1563static int translate_xfermode(char * name)
1564{
Glenn L McGrath07085852003-10-09 07:28:22 +00001565 const struct xfermode_entry *tmp;
1566 char *endptr;
1567 int val = -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001568
1569
Glenn L McGrath07085852003-10-09 07:28:22 +00001570 for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1571 {
1572 if (!strcmp(name, tmp->name))
1573 return tmp->val;
Glenn L McGrath07085852003-10-09 07:28:22 +00001574 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001575
Glenn L McGrath07085852003-10-09 07:28:22 +00001576 val = strtol(name, &endptr, 10);
1577 if (*endptr == '\0')
1578 return val;
1579
1580 return -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001581}
1582
Rob Landleya3e4f382006-04-29 16:06:31 +00001583static void interpret_xfermode(unsigned int xfermode)
Eric Andersen3443bd72003-07-22 07:30:36 +00001584{
1585 printf(" (");
Rob Landleyadde7982006-05-16 15:32:30 +00001586 if (xfermode == 0)
1587 printf("default PIO mode");
1588 else if (xfermode == 1)
1589 printf("default PIO mode, disable IORDY");
1590 else if (xfermode >= 8 && xfermode <= 15)
1591 printf("PIO flow control mode%u", xfermode-8);
1592 else if (xfermode >= 16 && xfermode <= 23)
1593 printf("singleword DMA mode%u", xfermode-16);
1594 else if (xfermode >= 32 && xfermode <= 39)
1595 printf("multiword DMA mode%u", xfermode-32);
1596 else if (xfermode >= 64 && xfermode <= 71)
1597 printf("UltraDMA mode%u", xfermode-64);
1598 else
1599 printf("Unknown");
Eric Andersen3443bd72003-07-22 07:30:36 +00001600 printf(")\n");
1601}
1602#endif /* HDIO_DRIVE_CMD */
1603
Rob Landleyadde7982006-05-16 15:32:30 +00001604static void print_flag(unsigned long flag, char *s, unsigned long value)
1605{
1606 if(flag)
1607 printf(" setting %s to %ld\n", s, value);
1608}
1609
Rob Landleya3e4f382006-04-29 16:06:31 +00001610static void process_dev(char *devname)
Eric Andersen3443bd72003-07-22 07:30:36 +00001611{
1612 int fd;
1613 static long parm, multcount;
Eric Andersen3443bd72003-07-22 07:30:36 +00001614#ifndef HDIO_DRIVE_CMD
1615 int force_operation = 0;
1616#endif
Rob Landley39cf6452006-05-05 16:52:28 +00001617 /* Please restore args[n] to these values after each ioctl
1618 except for args[2] */
Rob Landleye5b281f2006-04-29 15:49:18 +00001619 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
Rob Landleyadde7982006-05-16 15:32:30 +00001620 const char *fmt = " %s\t= %2ld";
Rob Landleye5b281f2006-04-29 15:49:18 +00001621
Rob Landleya3e4f382006-04-29 16:06:31 +00001622 fd = bb_xopen(devname, O_RDONLY|O_NONBLOCK);
Rob Landleyade7f952006-05-25 18:53:06 +00001623 printf("\n%s:\n", devname);
Eric Andersen3443bd72003-07-22 07:30:36 +00001624
Glenn L McGrath07085852003-10-09 07:28:22 +00001625 if (set_readahead)
1626 {
Rob Landleyadde7982006-05-16 15:32:30 +00001627 print_flag(get_readahead,"fs readahead", Xreadahead);
Eric Andersenf828da02004-07-20 22:53:59 +00001628 bb_ioctl(fd, BLKRASET,(int *)Xreadahead,"BLKRASET");
Eric Andersen3443bd72003-07-22 07:30:36 +00001629 }
1630#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001631 if (unregister_hwif)
1632 {
Rob Landley20deab02006-05-07 23:34:15 +00001633 printf(" attempting to unregister hwif#%lu\n", hwif);
Eric Andersena68ea1c2006-01-30 22:48:39 +00001634 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)(unsigned long)hwif,"HDIO_UNREGISTER_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00001635 }
1636#endif
1637#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001638 if (scan_hwif)
1639 {
Rob Landley20deab02006-05-07 23:34:15 +00001640 printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq);
Eric Andersen3443bd72003-07-22 07:30:36 +00001641 args[0] = hwif_data;
1642 args[1] = hwif_ctrl;
1643 args[2] = hwif_irq;
Glenn L McGrath07085852003-10-09 07:28:22 +00001644 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
Rob Landley39cf6452006-05-05 16:52:28 +00001645 args[0] = WIN_SETFEATURES;
1646 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001647 }
1648#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001649 if (set_piomode)
1650 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001651 if (noisy_piomode)
1652 {
1653 printf(" attempting to ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001654 if (piomode == 255)
Glenn L McGrath07085852003-10-09 07:28:22 +00001655 printf("auto-tune PIO mode\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001656 else if (piomode < 100)
Glenn L McGrath07085852003-10-09 07:28:22 +00001657 printf("set PIO mode to %d\n", piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001658 else if (piomode < 200)
Glenn L McGrath07085852003-10-09 07:28:22 +00001659 printf("set MDMA mode to %d\n", (piomode-100));
Eric Andersen3443bd72003-07-22 07:30:36 +00001660 else
Glenn L McGrath07085852003-10-09 07:28:22 +00001661 printf("set UDMA mode to %d\n", (piomode-200));
Eric Andersen3443bd72003-07-22 07:30:36 +00001662 }
Eric Andersena68ea1c2006-01-30 22:48:39 +00001663 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode, "HDIO_SET_PIO_MODE");
Eric Andersen3443bd72003-07-22 07:30:36 +00001664 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001665 if (set_io32bit)
1666 {
Rob Landleyadde7982006-05-16 15:32:30 +00001667 print_flag(get_io32bit,"32-bit IO_support flag", io32bit);
Glenn L McGrath07085852003-10-09 07:28:22 +00001668 bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
Eric Andersen3443bd72003-07-22 07:30:36 +00001669 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001670 if (set_mult)
1671 {
Rob Landleyadde7982006-05-16 15:32:30 +00001672 print_flag(get_mult, "multcount", mult);
Rob Landleyade7f952006-05-25 18:53:06 +00001673#ifdef HDIO_DRIVE_CMD
1674 bb_ioctl(fd, HDIO_SET_MULTCOUNT, &mult, "HDIO_SET_MULTCOUNT");
1675#else
1676 force_operation |= (!bb_ioctl(fd, HDIO_SET_MULTCOUNT, &mult, "HDIO_SET_MULTCOUNT"));
Eric Andersen3443bd72003-07-22 07:30:36 +00001677#endif
1678 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001679 if (set_readonly)
1680 {
Rob Landleyadde7982006-05-16 15:32:30 +00001681 print_flag_on_off(get_readonly,"readonly", readonly);
Glenn L McGrath07085852003-10-09 07:28:22 +00001682 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
Eric Andersen3443bd72003-07-22 07:30:36 +00001683 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001684 if (set_unmask)
1685 {
Rob Landleyadde7982006-05-16 15:32:30 +00001686 print_flag_on_off(get_unmask,"unmaskirq", unmask);
Glenn L McGrath07085852003-10-09 07:28:22 +00001687 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
Eric Andersen3443bd72003-07-22 07:30:36 +00001688 }
Eric Andersen0a57a792003-08-06 08:57:35 +00001689#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Glenn L McGrath07085852003-10-09 07:28:22 +00001690 if (set_dma)
1691 {
Rob Landleyadde7982006-05-16 15:32:30 +00001692 print_flag_on_off(get_dma, "using_dma", dma);
Glenn L McGrath07085852003-10-09 07:28:22 +00001693 bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
Eric Andersen3443bd72003-07-22 07:30:36 +00001694 }
Eric Andersen0a57a792003-08-06 08:57:35 +00001695#endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
Glenn L McGrath07085852003-10-09 07:28:22 +00001696 if (set_dma_q)
1697 {
Rob Landleyadde7982006-05-16 15:32:30 +00001698 print_flag_on_off(get_dma_q,"DMA queue_depth", dma_q);
Glenn L McGrath07085852003-10-09 07:28:22 +00001699 bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
Eric Andersen3443bd72003-07-22 07:30:36 +00001700 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001701 if (set_nowerr)
1702 {
Rob Landleyadde7982006-05-16 15:32:30 +00001703 print_flag_on_off(get_nowerr,"nowerr", nowerr);
Glenn L McGrath07085852003-10-09 07:28:22 +00001704 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
Eric Andersen3443bd72003-07-22 07:30:36 +00001705 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001706 if (set_keep)
1707 {
Rob Landleyadde7982006-05-16 15:32:30 +00001708 print_flag_on_off(get_keep,"keep_settings", keep);
Glenn L McGrath07085852003-10-09 07:28:22 +00001709 bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
Eric Andersen3443bd72003-07-22 07:30:36 +00001710 }
1711#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001712 if (set_doorlock)
1713 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001714 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
Rob Landley39cf6452006-05-05 16:52:28 +00001715 args[2] = 0;
Rob Landleyadde7982006-05-16 15:32:30 +00001716 print_flag_on_off(get_doorlock,"drive doorlock", doorlock);
Glenn L McGrath07085852003-10-09 07:28:22 +00001717 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
Rob Landley39cf6452006-05-05 16:52:28 +00001718 args[0] = WIN_SETFEATURES;
Eric Andersen3443bd72003-07-22 07:30:36 +00001719 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001720 if (set_dkeep)
1721 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001722 /* lock/unlock the drive's "feature" settings */
Rob Landleyadde7982006-05-16 15:32:30 +00001723 print_flag_on_off(get_dkeep,"drive keep features", dkeep);
Eric Andersen3443bd72003-07-22 07:30:36 +00001724 args[2] = dkeep ? 0x66 : 0xcc;
Glenn L McGrath07085852003-10-09 07:28:22 +00001725 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001726 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001727 if (set_defects)
1728 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001729 args[2] = defects ? 0x04 : 0x84;
Rob Landleyadde7982006-05-16 15:32:30 +00001730 print_flag(get_defects,"drive defect-mgmt", defects);
Glenn L McGrath07085852003-10-09 07:28:22 +00001731 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001732 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001733 if (set_prefetch)
1734 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001735 args[1] = prefetch;
Rob Landleye5b281f2006-04-29 15:49:18 +00001736 args[2] = 0xab;
Rob Landleyadde7982006-05-16 15:32:30 +00001737 print_flag(get_prefetch,"drive prefetch", prefetch);
Glenn L McGrath07085852003-10-09 07:28:22 +00001738 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
Rob Landley39cf6452006-05-05 16:52:28 +00001739 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001740 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001741 if (set_xfermode)
1742 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001743 args[1] = xfermode_requested;
Rob Landleye5b281f2006-04-29 15:49:18 +00001744 args[2] = 3;
Glenn L McGrath07085852003-10-09 07:28:22 +00001745 if (get_xfermode)
1746 {
Rob Landleyade7f952006-05-25 18:53:06 +00001747 print_flag(1,"xfermode", xfermode_requested);
Eric Andersen3443bd72003-07-22 07:30:36 +00001748 interpret_xfermode(xfermode_requested);
1749 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001750 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
Rob Landley39cf6452006-05-05 16:52:28 +00001751 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001752 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001753 if (set_lookahead)
1754 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001755 args[2] = lookahead ? 0xaa : 0x55;
Rob Landleyadde7982006-05-16 15:32:30 +00001756 print_flag_on_off(get_lookahead,"drive read-lookahead", lookahead);
Glenn L McGrath07085852003-10-09 07:28:22 +00001757 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001758 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001759 if (set_apmmode)
1760 {
Rob Landleyadde7982006-05-16 15:32:30 +00001761 args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */; /* feature register */
1762 args[1] = apmmode; /* sector count register 1-255 */
1763 if (get_apmmode)
1764 printf(" setting APM level to %s 0x%02lX (%ld)\n", (apmmode == 255) ? "disabled" : "", apmmode, apmmode);
Glenn L McGrath07085852003-10-09 07:28:22 +00001765 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
Rob Landley39cf6452006-05-05 16:52:28 +00001766 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001767 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001768 if (set_wcache)
1769 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001770#ifdef DO_FLUSHCACHE
1771#ifndef WIN_FLUSHCACHE
1772#define WIN_FLUSHCACHE 0xe7
1773#endif
Rob Landleye5b281f2006-04-29 15:49:18 +00001774 static unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
Eric Andersen3443bd72003-07-22 07:30:36 +00001775#endif /* DO_FLUSHCACHE */
Eric Andersen3443bd72003-07-22 07:30:36 +00001776 args[2] = wcache ? 0x02 : 0x82;
Rob Landleyadde7982006-05-16 15:32:30 +00001777 print_flag_on_off(get_wcache,"drive write-caching", wcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00001778#ifdef DO_FLUSHCACHE
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001779 if (!wcache)
1780 bb_ioctl(fd, HDIO_DRIVE_CMD, &flushcache, "HDIO_DRIVE_CMD(flushcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001781#endif /* DO_FLUSHCACHE */
Glenn L McGrath07085852003-10-09 07:28:22 +00001782 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001783#ifdef DO_FLUSHCACHE
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001784 if (!wcache)
1785 bb_ioctl(fd, HDIO_DRIVE_CMD, &flushcache, "HDIO_DRIVE_CMD(flushcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001786#endif /* DO_FLUSHCACHE */
1787 }
Rob Landley39cf6452006-05-05 16:52:28 +00001788
1789 /* In code below, we do not preserve args[0], but the rest
1790 is preserved, including args[2] */
1791 args[2] = 0;
1792
Glenn L McGrath07085852003-10-09 07:28:22 +00001793 if (set_standbynow)
1794 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001795#ifndef WIN_STANDBYNOW1
1796#define WIN_STANDBYNOW1 0xE0
1797#endif
1798#ifndef WIN_STANDBYNOW2
1799#define WIN_STANDBYNOW2 0x94
1800#endif
Rob Landleyadde7982006-05-16 15:32:30 +00001801 if (get_standbynow) printf(" issuing standby command\n");
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001802 args[0] = WIN_STANDBYNOW1;
1803 bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2, "HDIO_DRIVE_CMD(standby)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001804 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001805 if (set_sleepnow)
1806 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001807#ifndef WIN_SLEEPNOW1
1808#define WIN_SLEEPNOW1 0xE6
1809#endif
1810#ifndef WIN_SLEEPNOW2
1811#define WIN_SLEEPNOW2 0x99
1812#endif
Rob Landleyadde7982006-05-16 15:32:30 +00001813 if (get_sleepnow) printf(" issuing sleep command\n");
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001814 args[0] = WIN_SLEEPNOW1;
1815 bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2, "HDIO_DRIVE_CMD(sleep)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001816 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001817 if (set_seagate)
1818 {
Rob Landleye5b281f2006-04-29 15:49:18 +00001819 args[0] = 0xfb;
Rob Landleyadde7982006-05-16 15:32:30 +00001820 if (get_seagate) printf(" disabling Seagate auto powersaving mode\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001821 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001822 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001823 if (set_standby)
1824 {
Rob Landleye5b281f2006-04-29 15:49:18 +00001825 args[0] = WIN_SETIDLE1;
1826 args[1] = standby_requested;
Glenn L McGrath07085852003-10-09 07:28:22 +00001827 if (get_standby)
1828 {
Rob Landleyade7f952006-05-25 18:53:06 +00001829 print_flag(1,"standby", standby_requested);
Eric Andersen3443bd72003-07-22 07:30:36 +00001830 interpret_standby(standby_requested);
1831 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001832 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
Rob Landley39cf6452006-05-05 16:52:28 +00001833 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001834 }
1835#else /* HDIO_DRIVE_CMD */
Glenn L McGrath07085852003-10-09 07:28:22 +00001836 if (force_operation)
1837 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001838 char buf[512];
1839 flush_buffer_cache(fd);
1840 if (-1 == read(fd, buf, sizeof(buf)))
Rob Landleyadde7982006-05-16 15:32:30 +00001841 bb_perror_msg("read(%d bytes) failed (rc=%d)", sizeof(buf), -1);
Eric Andersen3443bd72003-07-22 07:30:36 +00001842 }
1843#endif /* HDIO_DRIVE_CMD */
1844
Rob Landleyade7f952006-05-25 18:53:06 +00001845 if (get_mult || get_identity)
Glenn L McGrath07085852003-10-09 07:28:22 +00001846 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001847 multcount = -1;
Glenn L McGrath07085852003-10-09 07:28:22 +00001848 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
1849 {
Rob Landleyade7f952006-05-25 18:53:06 +00001850 if (get_mult)
Eric Andersen06d4ec22004-03-19 10:53:52 +00001851 bb_perror_msg("HDIO_GET_MULTCOUNT");
Glenn L McGrath07085852003-10-09 07:28:22 +00001852 }
Rob Landleyade7f952006-05-25 18:53:06 +00001853 else if (get_mult)
Glenn L McGrath07085852003-10-09 07:28:22 +00001854 {
Rob Landleyadde7982006-05-16 15:32:30 +00001855 printf(fmt, "multcount", multcount);
Eric Andersen3443bd72003-07-22 07:30:36 +00001856 on_off(multcount);
1857 }
1858 }
Rob Landleyade7f952006-05-25 18:53:06 +00001859 if (get_io32bit)
Glenn L McGrath07085852003-10-09 07:28:22 +00001860 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001861 if (!bb_ioctl(fd, HDIO_GET_32BIT, &parm, "HDIO_GET_32BIT"))
Glenn L McGrath07085852003-10-09 07:28:22 +00001862 {
Rob Landleyadde7982006-05-16 15:32:30 +00001863 printf(" IO_support\t=%3ld (", parm);
1864 if (parm == 0)
1865 printf("default 16-bit)\n");
1866 else if (parm == 2)
1867 printf("16-bit)\n");
1868 else if (parm == 1)
1869 printf("32-bit)\n");
1870 else if (parm == 3)
1871 printf("32-bit w/sync)\n");
1872 else if (parm == 8)
1873 printf("Request-Queue-Bypass)\n");
1874 else
1875 printf("\?\?\?)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001876 }
1877 }
Rob Landleyade7f952006-05-25 18:53:06 +00001878 if (get_unmask)
Glenn L McGrath07085852003-10-09 07:28:22 +00001879 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001880 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001881 "HDIO_GET_UNMASKINTR","unmaskirq");
Eric Andersen3443bd72003-07-22 07:30:36 +00001882 }
1883
Glenn L McGrath07085852003-10-09 07:28:22 +00001884
Eric Andersen0a57a792003-08-06 08:57:35 +00001885#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Rob Landleyade7f952006-05-25 18:53:06 +00001886 if (get_dma) {
Rob Landleya3e4f382006-04-29 16:06:31 +00001887 if (!bb_ioctl(fd, HDIO_GET_DMA, &parm, "HDIO_GET_DMA"))
Glenn L McGrath07085852003-10-09 07:28:22 +00001888 {
Rob Landleyadde7982006-05-16 15:32:30 +00001889 printf(fmt, "using_dma", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001890 if (parm == 8)
1891 printf(" (DMA-Assisted-PIO)\n");
1892 else
1893 on_off(parm);
1894 }
1895 }
1896#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001897 if (get_dma_q)
1898 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001899 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001900 "HDIO_GET_QDMA","queue_depth");
Eric Andersen3443bd72003-07-22 07:30:36 +00001901 }
Rob Landleyade7f952006-05-25 18:53:06 +00001902 if (get_keep)
Glenn L McGrath07085852003-10-09 07:28:22 +00001903 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001904 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001905 "HDIO_GET_KEEPSETTINGS","keepsettings");
Eric Andersen3443bd72003-07-22 07:30:36 +00001906 }
1907
Glenn L McGrath07085852003-10-09 07:28:22 +00001908 if (get_nowerr)
1909 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001910 bb_ioctl_on_off (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001911 "HDIO_GET_NOWERR","nowerr");
Eric Andersen3443bd72003-07-22 07:30:36 +00001912 }
Rob Landleyade7f952006-05-25 18:53:06 +00001913 if (get_readonly)
Glenn L McGrath07085852003-10-09 07:28:22 +00001914 {
1915 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001916 "BLKROGET","readonly");
Eric Andersen3443bd72003-07-22 07:30:36 +00001917 }
Rob Landleyade7f952006-05-25 18:53:06 +00001918 if (get_readahead)
Glenn L McGrath07085852003-10-09 07:28:22 +00001919 {
1920 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001921 "BLKRAGET","readahead");
Eric Andersen3443bd72003-07-22 07:30:36 +00001922 }
Rob Landleyade7f952006-05-25 18:53:06 +00001923 if (get_geom)
Glenn L McGrath07085852003-10-09 07:28:22 +00001924 {
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001925 if (!bb_ioctl(fd, BLKGETSIZE, &parm, "BLKGETSIZE"))
1926 {
Rob Landley2584e9b2006-05-03 20:00:00 +00001927 struct hd_geometry g;
1928
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001929 if (!bb_ioctl(fd, HDIO_GETGEO, &g, "HDIO_GETGEO"))
Rob Landleyadde7982006-05-16 15:32:30 +00001930 printf(" geometry\t= %u/%u/%u, sectors = %ld, start = %ld\n",
Rob Landley2584e9b2006-05-03 20:00:00 +00001931 g.cylinders, g.heads, g.sectors, parm, g.start);
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001932 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001933 }
1934#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001935 if (get_powermode)
1936 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001937#ifndef WIN_CHECKPOWERMODE1
1938#define WIN_CHECKPOWERMODE1 0xE5
1939#endif
1940#ifndef WIN_CHECKPOWERMODE2
1941#define WIN_CHECKPOWERMODE2 0x98
1942#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00001943 const char *state;
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001944
Rob Landleye5b281f2006-04-29 15:49:18 +00001945 args[0] = WIN_CHECKPOWERMODE1;
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001946 if (bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2, 0))
Glenn L McGrath07085852003-10-09 07:28:22 +00001947 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001948 if (errno != EIO || args[0] != 0 || args[1] != 0)
Rob Landleyadde7982006-05-16 15:32:30 +00001949 state = "Unknown";
Eric Andersen3443bd72003-07-22 07:30:36 +00001950 else
1951 state = "sleeping";
Eric Andersen3443bd72003-07-22 07:30:36 +00001952 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001953 else
1954 state = (args[2] == 255) ? "active/idle" : "standby";
Rob Landley39cf6452006-05-05 16:52:28 +00001955 args[1] = args[2] = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +00001956
Eric Andersen3443bd72003-07-22 07:30:36 +00001957 printf(" drive state is: %s\n", state);
1958 }
1959#endif
1960#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
Glenn L McGrath07085852003-10-09 07:28:22 +00001961 if (perform_reset)
1962 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001963 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
Eric Andersen3443bd72003-07-22 07:30:36 +00001964 }
1965#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1966#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001967 if (perform_tristate)
1968 {
Rob Landleye5b281f2006-04-29 15:49:18 +00001969 args[0] = 0;
1970 args[1] = tristate;
Glenn L McGrath07085852003-10-09 07:28:22 +00001971 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00001972 }
1973#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1974#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +00001975 if (get_identity)
1976 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001977 static struct hd_driveid id;
1978
Glenn L McGrath07085852003-10-09 07:28:22 +00001979 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
1980 {
1981 if (multcount != -1)
1982 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001983 id.multsect = multcount;
1984 id.multsect_valid |= 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00001985 }
1986 else
Eric Andersen3443bd72003-07-22 07:30:36 +00001987 id.multsect_valid &= ~1;
1988 dump_identity(&id);
Glenn L McGrath07085852003-10-09 07:28:22 +00001989 }
1990 else if (errno == -ENOMSG)
Eric Andersen3443bd72003-07-22 07:30:36 +00001991 printf(" no identification info available\n");
1992 else
Eric Andersen06d4ec22004-03-19 10:53:52 +00001993 bb_perror_msg("HDIO_GET_IDENTITY");
Eric Andersen3443bd72003-07-22 07:30:36 +00001994 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001995
1996 if (get_IDentity)
1997 {
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001998 unsigned char args1[4+512]; /* = { ... } will eat 0.5k of rodata! */
Glenn L McGrath07085852003-10-09 07:28:22 +00001999
Rob Landley5f8b5ec2006-04-29 16:03:40 +00002000 memset(args1, 0, sizeof(args1));
2001 args1[0] = WIN_IDENTIFY;
2002 args1[3] = 1;
Rob Landley0753f4a2006-06-07 00:27:25 +00002003 if (!bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY, "HDIO_DRIVE_CMD(identify)"))
2004 identify((void *)(args1 + 4));
Eric Andersen3443bd72003-07-22 07:30:36 +00002005 }
2006#endif
2007#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002008 if (set_busstate)
2009 {
Glenn L McGrath07085852003-10-09 07:28:22 +00002010 if (get_busstate)
2011 {
Rob Landleyadde7982006-05-16 15:32:30 +00002012 print_flag(1, "bus state", busstate);
Eric Andersen3443bd72003-07-22 07:30:36 +00002013 bus_state_value(busstate);
2014 }
Eric Andersena68ea1c2006-01-30 22:48:39 +00002015 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate, "HDIO_SET_BUSSTATE");
Eric Andersen3443bd72003-07-22 07:30:36 +00002016 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002017 if (get_busstate)
2018 {
Rob Landley5f8b5ec2006-04-29 16:03:40 +00002019 if (!bb_ioctl(fd, HDIO_GET_BUSSTATE, &parm, "HDIO_GET_BUSSTATE"))
Glenn L McGrath07085852003-10-09 07:28:22 +00002020 {
Rob Landleyadde7982006-05-16 15:32:30 +00002021 printf(fmt, "bus state", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00002022 bus_state_value(parm);
2023 }
2024 }
2025#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00002026 if (reread_partn)
2027 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2028
Eric Andersen3443bd72003-07-22 07:30:36 +00002029
2030 if (do_ctimings)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002031 do_time(0,fd); /*time cache */
Eric Andersen3443bd72003-07-22 07:30:36 +00002032 if (do_timings)
Glenn L McGrath07085852003-10-09 07:28:22 +00002033 do_time(1,fd); /*time device */
Eric Andersen3443bd72003-07-22 07:30:36 +00002034 if (do_flush)
Rob Landleya3e4f382006-04-29 16:06:31 +00002035 flush_buffer_cache(fd);
2036 close(fd);
Eric Andersen3443bd72003-07-22 07:30:36 +00002037}
2038
Eric Andersen3443bd72003-07-22 07:30:36 +00002039#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Rob Landleya3e4f382006-04-29 16:06:31 +00002040static int fromhex(unsigned char c)
Eric Andersen3443bd72003-07-22 07:30:36 +00002041{
2042 if (c >= 'a' && c <= 'f')
2043 return 10 + (c - 'a');
2044 if (c >= '0' && c <= '9')
2045 return (c - '0');
2046 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2047}
2048
Rob Landley0f0b6452006-05-03 18:28:06 +00002049static void identify_from_stdin(void)
Eric Andersen3443bd72003-07-22 07:30:36 +00002050{
Rob Landley0753f4a2006-06-07 00:27:25 +00002051 uint16_t sbuf[256];
2052 unsigned char buf[1280], *b = (unsigned char *)buf;
Eric Andersen3443bd72003-07-22 07:30:36 +00002053 int i, count = read(0, buf, 1280);
2054
2055 if (count != 1280)
Rob Landleyadde7982006-05-16 15:32:30 +00002056 bb_error_msg_and_die("read(%d bytes) failed (rc=%d)", 1280, count);
Rob Landleyade7f952006-05-25 18:53:06 +00002057
Rob Landley0753f4a2006-06-07 00:27:25 +00002058 // Convert the newline-separated hex data into an identify block.
2059
2060 for (i = 0; i<256; i++)
Glenn L McGrath07085852003-10-09 07:28:22 +00002061 {
Rob Landley0753f4a2006-06-07 00:27:25 +00002062 int j;
2063 for(j=0;j<4;j++) sbuf[i] = (sbuf[i] <<4) + fromhex(*(b++));
Eric Andersen3443bd72003-07-22 07:30:36 +00002064 }
Rob Landley0753f4a2006-06-07 00:27:25 +00002065
2066 // Parse the data.
2067
Rob Landley6389ff12006-05-01 19:28:53 +00002068 identify(sbuf);
Eric Andersen3443bd72003-07-22 07:30:36 +00002069}
2070#endif
2071
Rob Landley20deab02006-05-07 23:34:15 +00002072/* busybox specific stuff */
Rob Landley19802562006-05-08 15:35:46 +00002073static void parse_opts(unsigned long *get, unsigned long *set, unsigned long *value, int min, int max)
Eric Andersenb2aa7762004-04-05 13:08:08 +00002074{
Rob Landleyade7f952006-05-25 18:53:06 +00002075 if (get) {
2076 *get = 1;
Rob Landley20deab02006-05-07 23:34:15 +00002077 }
2078 if (optarg) {
2079 *set = 1;
2080 *value = bb_xgetlarg(optarg, 10, min, max);
Eric Andersen06d4ec22004-03-19 10:53:52 +00002081 }
Rob Landley20deab02006-05-07 23:34:15 +00002082}
2083
Rob Landleyade7f952006-05-25 18:53:06 +00002084static void parse_xfermode(int flag, unsigned long *get, unsigned long *set, int *value)
Rob Landley20deab02006-05-07 23:34:15 +00002085{
2086 if (flag) {
Rob Landleyade7f952006-05-25 18:53:06 +00002087 *get = 1;
Rob Landley20deab02006-05-07 23:34:15 +00002088 if (optarg) {
2089 *set = ((*value = translate_xfermode(optarg)) > -1);
2090 }
2091 }
2092}
2093
Rob Landley06208412006-05-31 22:52:57 +00002094/*------- getopt short options --------*/
Rob Landleyade7f952006-05-25 18:53:06 +00002095static const char hdparm_options[]= "gfu::n::p:r::m::c::k::a::B:tTh"\
Rob Landley06208412006-05-31 22:52:57 +00002096 USE_FEATURE_HDPARM_GET_IDENTITY("iI")
Rob Landley20deab02006-05-07 23:34:15 +00002097 USE_FEATURE_HDPARM_HDIO_GETSET_DMA("d::")
2098#ifdef HDIO_DRIVE_CMD
2099 "S::D::P::X::K::A::L::W::CyYzZ"
2100#endif
2101 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF("U:")
2102#ifdef HDIO_GET_QDMA
2103#ifdef HDIO_SET_QDMA
2104 "Q:"
2105#else
2106 "Q"
2107#endif
2108#endif
2109 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET("w")
2110 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF("x::b:")
2111 USE_FEATURE_HDPARM_HDIO_SCAN_HWIF("R:");
2112/*-------------------------------------*/
2113
2114/* our main() routine: */
2115int hdparm_main(int argc, char **argv) ATTRIBUTE_NORETURN;
2116int hdparm_main(int argc, char **argv)
2117{
2118 int c;
Rob Landleyade7f952006-05-25 18:53:06 +00002119 int flagcount = 0;
Rob Landley20deab02006-05-07 23:34:15 +00002120
Rob Landley06208412006-05-31 22:52:57 +00002121 while ((c = getopt(argc, argv, hdparm_options)) >= 0) {
Rob Landley20deab02006-05-07 23:34:15 +00002122 flagcount++;
Rob Landley06208412006-05-31 22:52:57 +00002123 if (c == 'h') bb_show_usage(); /* EXIT */
Mike Frysingerea93f8a2006-06-07 14:25:22 +00002124 USE_FEATURE_HDPARM_GET_IDENTITY(get_IDentity |= (c == 'I'));
2125 USE_FEATURE_HDPARM_GET_IDENTITY(get_identity |= (c == 'i'));
Rob Landleyadde7982006-05-16 15:32:30 +00002126 get_geom |= (c == 'g');
2127 do_flush |= (c == 'f');
Rob Landley19802562006-05-08 15:35:46 +00002128 if (c == 'u') parse_opts(&get_unmask, &set_unmask, &unmask, 0, 1);
2129 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(if (c == 'd') parse_opts(&get_dma, &set_dma, &dma, 0, 9));
2130 if (c == 'n') parse_opts(&get_nowerr, &set_nowerr, &nowerr, 0, 1);
Rob Landleyade7f952006-05-25 18:53:06 +00002131 parse_xfermode((c == 'p'),&noisy_piomode, &set_piomode, &piomode);
Rob Landley19802562006-05-08 15:35:46 +00002132 if (c == 'r') parse_opts(&get_readonly, &set_readonly, &readonly, 0, 1);
2133 if (c == 'm') parse_opts(&get_mult, &set_mult, &mult, 0, INT_MAX /*32*/);
2134 if (c == 'c') parse_opts(&get_io32bit, &set_io32bit, &io32bit, 0, INT_MAX /*8*/);
2135 if (c == 'k') parse_opts(&get_keep, &set_keep, &keep, 0, 1);
2136 if (c == 'a') parse_opts(&get_readahead, &set_readahead, &Xreadahead, 0, INT_MAX);
2137 if (c == 'B') parse_opts(&get_apmmode, &set_apmmode, &apmmode, 1, 255);
Rob Landleyadde7982006-05-16 15:32:30 +00002138 do_flush |= do_timings |= (c == 't');
2139 do_flush |= do_ctimings |= (c == 'T');
Rob Landley20deab02006-05-07 23:34:15 +00002140#ifdef HDIO_DRIVE_CMD
Rob Landley19802562006-05-08 15:35:46 +00002141 if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, INT_MAX);
2142 if (c == 'D') parse_opts(&get_defects, &set_defects, &defects, 0, INT_MAX);
2143 if (c == 'P') parse_opts(&get_prefetch, &set_prefetch, &prefetch, 0, INT_MAX);
Rob Landleyade7f952006-05-25 18:53:06 +00002144 parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
Rob Landley19802562006-05-08 15:35:46 +00002145 if (c == 'K') parse_opts(&get_dkeep, &set_dkeep, &prefetch, 0, 1);
2146 if (c == 'A') parse_opts(&get_lookahead, &set_lookahead, &lookahead, 0, 1);
2147 if (c == 'L') parse_opts(&get_doorlock, &set_doorlock, &doorlock, 0, 1);
2148 if (c == 'W') parse_opts(&get_wcache, &set_wcache, &wcache, 0, 1);
Rob Landleyade7f952006-05-25 18:53:06 +00002149 get_powermode |= (c == 'C');
2150 get_standbynow = set_standbynow |= (c == 'y');
2151 get_sleepnow = set_sleepnow |= (c == 'Y');
Rob Landleyadde7982006-05-16 15:32:30 +00002152 reread_partn |= (c == 'z');
Rob Landleyade7f952006-05-25 18:53:06 +00002153 get_seagate = set_seagate |= (c == 'Z');
Rob Landley20deab02006-05-07 23:34:15 +00002154#endif
Rob Landley19802562006-05-08 15:35:46 +00002155 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 +00002156#ifdef HDIO_GET_QDMA
Rob Landley19802562006-05-08 15:35:46 +00002157 if (c == 'Q') {
Rob Landley20deab02006-05-07 23:34:15 +00002158#ifdef HDIO_SET_QDMA
Rob Landley19802562006-05-08 15:35:46 +00002159 parse_opts(&get_dma_q, &set_dma_q, &dma_q, 0, INT_MAX);
Rob Landley20deab02006-05-07 23:34:15 +00002160#else
Rob Landley19802562006-05-08 15:35:46 +00002161 parse_opts(&get_dma_q, NULL, NULL, 0, 0);
Rob Landley20deab02006-05-07 23:34:15 +00002162#endif
Rob Landley19802562006-05-08 15:35:46 +00002163 }
Rob Landley20deab02006-05-07 23:34:15 +00002164#endif
Rob Landleyade7f952006-05-25 18:53:06 +00002165 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
Rob Landley19802562006-05-08 15:35:46 +00002166 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1));
2167 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 +00002168#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
2169 if (c == 'R') {
Rob Landley19802562006-05-08 15:35:46 +00002170 parse_opts(NULL, &scan_hwif, &hwif_data, 0, INT_MAX);
Rob Landley20deab02006-05-07 23:34:15 +00002171 hwif_ctrl = bb_xgetlarg((argv[optind]) ? argv[optind] : "", 10, 0, INT_MAX);
2172 hwif_irq = bb_xgetlarg((argv[optind+1]) ? argv[optind+1] : "", 10, 0, INT_MAX);
2173 /* Move past the 2 additional arguments */
2174 argv += 2;
2175 argc -= 2;
2176 }
2177#endif
2178 }
Rob Landleyade7f952006-05-25 18:53:06 +00002179 /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
2180 if (!flagcount){
2181 get_mult = get_io32bit = get_unmask = get_keep = get_readonly = get_readahead = get_geom = 1;
2182 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(get_dma = 1);
2183 }
Rob Landley20deab02006-05-07 23:34:15 +00002184 argc -= optind;
2185 argv += optind;
2186
2187 if (argc < 1) {
Rob Landley6d8ce172006-06-07 21:22:42 +00002188 if (ENABLE_FEATURE_HDPARM_GET_IDENTITY && !isatty(STDIN_FILENO))
2189 identify_from_stdin(); /* EXIT */
2190 else bb_show_usage();
Rob Landley20deab02006-05-07 23:34:15 +00002191 }
2192
2193 while (argc--) {
2194 process_dev(*argv);
2195 argv++;
2196 }
2197 exit(EXIT_SUCCESS);
Eric Andersen3443bd72003-07-22 07:30:36 +00002198}