blob: 81e7c6e4ca71ad05d9568608bfd47b2b9caab398 [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>
17#include <stdlib.h>
18#include <fcntl.h>
19#include <errno.h>
20#include <ctype.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000021#include <sys/ioctl.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000022#include <sys/sysmacros.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000023#include <sys/times.h>
24#include <sys/mount.h>
Rob Landley4ae2f512006-05-19 17:24:26 +000025#include <sys/mman.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000026#include <linux/types.h>
27#include <linux/hdreg.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000028
Rob Landley688ed0d2006-03-04 22:40:25 +000029#if BB_BIG_ENDIAN && !defined(__USE_XOPEN)
30# define __USE_XOPEN
Eric Andersen3443bd72003-07-22 07:30:36 +000031#endif
Rob Landleyade7f952006-05-25 18:53:06 +000032#include <unistd.h>
33
Eric Andersen3443bd72003-07-22 07:30:36 +000034/* device types */
35/* ------------ */
36#define NO_DEV 0xffff
37#define ATA_DEV 0x0000
38#define ATAPI_DEV 0x0001
39
40/* word definitions */
41/* ---------------- */
42#define GEN_CONFIG 0 /* general configuration */
43#define LCYLS 1 /* number of logical cylinders */
44#define CONFIG 2 /* specific configuration */
45#define LHEADS 3 /* number of logical heads */
46#define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
47#define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
48#define LSECTS 6 /* number of logical sectors/track */
49#define START_SERIAL 10 /* ASCII serial number */
50#define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
51#define BUF_TYPE 20 /* buffer type (ATA-1) */
52#define BUFFER__SIZE 21 /* buffer size (ATA-1) */
53#define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
54#define START_FW_REV 23 /* ASCII firmware revision */
55#define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000056#define START_MODEL 27 /* ASCII model number */
57#define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
Eric Andersen3443bd72003-07-22 07:30:36 +000058#define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
59#define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
60#define CAPAB_0 49 /* capabilities */
61#define CAPAB_1 50
62#define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
63#define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
64#define WHATS_VALID 53 /* what fields are valid */
65#define LCYLS_CUR 54 /* current logical cylinders */
66#define LHEADS_CUR 55 /* current logical heads */
67#define LSECTS_CUR 56 /* current logical sectors/track */
68#define CAPACITY_LSB 57 /* current capacity in sectors */
69#define CAPACITY_MSB 58
70#define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
71#define LBA_SECTS_LSB 60 /* LBA: total number of user */
72#define LBA_SECTS_MSB 61 /* addressable sectors */
73#define SINGLE_DMA 62 /* singleword DMA modes */
74#define MULTI_DMA 63 /* multiword DMA modes */
75#define ADV_PIO_MODES 64 /* advanced PIO modes supported */
76 /* multiword DMA xfer cycle time: */
77#define DMA_TIME_MIN 65 /* - minimum */
78#define DMA_TIME_NORM 66 /* - manufacturer's recommended */
79 /* minimum PIO xfer cycle time: */
80#define PIO_NO_FLOW 67 /* - without flow control */
81#define PIO_FLOW 68 /* - with IORDY flow control */
82#define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
83#define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
84#define CDR_MAJOR 73 /* CD ROM: major version number */
85#define CDR_MINOR 74 /* CD ROM: minor version number */
86#define QUEUE_DEPTH 75 /* queue depth */
87#define MAJOR 80 /* major version number */
88#define MINOR 81 /* minor version number */
89#define CMDS_SUPP_0 82 /* command/feature set(s) supported */
90#define CMDS_SUPP_1 83
91#define CMDS_SUPP_2 84
92#define CMDS_EN_0 85 /* command/feature set(s) enabled */
93#define CMDS_EN_1 86
94#define CMDS_EN_2 87
95#define ULTRA_DMA 88 /* ultra DMA modes */
96 /* time to complete security erase */
97#define ERASE_TIME 89 /* - ordinary */
98#define ENH_ERASE_TIME 90 /* - enhanced */
99#define ADV_PWR 91 /* current advanced power management level
Glenn L McGrath07085852003-10-09 07:28:22 +0000100 in low byte, 0x40 in high byte. */
Eric Andersen3443bd72003-07-22 07:30:36 +0000101#define PSWD_CODE 92 /* master password revision code */
102#define HWRST_RSLT 93 /* hardware reset result */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000103#define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
Eric Andersen3443bd72003-07-22 07:30:36 +0000104#define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
105#define LBA_MID 101 /* bits are used, but addr 103 */
106#define LBA_48_MSB 102 /* has been reserved for LBA in */
107#define LBA_64_MSB 103 /* the future. */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000108#define RM_STAT 127 /* removable media status notification feature set support */
Eric Andersen3443bd72003-07-22 07:30:36 +0000109#define SECU_STATUS 128 /* security status */
110#define CFA_PWR_MODE 160 /* CFA power mode 1 */
111#define START_MEDIA 176 /* media serial number */
112#define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
113#define START_MANUF 196 /* media manufacturer I.D. */
114#define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
115#define INTEGRITY 255 /* integrity word */
116
117/* bit definitions within the words */
118/* -------------------------------- */
119
120/* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
121#define VALID 0xc000
122#define VALID_VAL 0x4000
123/* many words are considered invalid if they are either all-0 or all-1 */
124#define NOVAL_0 0x0000
125#define NOVAL_1 0xffff
126
127/* word 0: gen_config */
Glenn L McGrath07085852003-10-09 07:28:22 +0000128#define NOT_ATA 0x8000
Eric Andersen3443bd72003-07-22 07:30:36 +0000129#define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
130#define MEDIA_REMOVABLE 0x0080
131#define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
132#define INCOMPLETE 0x0004
133#define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
134#define DRQ_RESPONSE_TIME 0x0060
135#define DRQ_3MS_VAL 0x0000
136#define DRQ_INTR_VAL 0x0020
137#define DRQ_50US_VAL 0x0040
138#define PKT_SIZE_SUPPORTED 0x0003
139#define PKT_SIZE_12_VAL 0x0000
140#define PKT_SIZE_16_VAL 0x0001
141#define EQPT_TYPE 0x1f00
142#define SHIFT_EQPT 8
143
144#define CDROM 0x0005
145
146#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000147static const char * const pkt_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000148 "Direct-access device", /* word 0, bits 12-8 = 00 */
149 "Sequential-access device", /* word 0, bits 12-8 = 01 */
150 "Printer", /* word 0, bits 12-8 = 02 */
151 "Processor", /* word 0, bits 12-8 = 03 */
152 "Write-once device", /* word 0, bits 12-8 = 04 */
153 "CD-ROM", /* word 0, bits 12-8 = 05 */
154 "Scanner", /* word 0, bits 12-8 = 06 */
155 "Optical memory", /* word 0, bits 12-8 = 07 */
156 "Medium changer", /* word 0, bits 12-8 = 08 */
157 "Communications device", /* word 0, bits 12-8 = 09 */
158 "ACS-IT8 device", /* word 0, bits 12-8 = 0a */
159 "ACS-IT8 device", /* word 0, bits 12-8 = 0b */
160 "Array controller", /* word 0, bits 12-8 = 0c */
161 "Enclosure services", /* word 0, bits 12-8 = 0d */
162 "Reduced block command device", /* word 0, bits 12-8 = 0e */
163 "Optical card reader/writer", /* word 0, bits 12-8 = 0f */
164 "", /* word 0, bits 12-8 = 10 */
165 "", /* word 0, bits 12-8 = 11 */
166 "", /* word 0, bits 12-8 = 12 */
167 "", /* word 0, bits 12-8 = 13 */
168 "", /* word 0, bits 12-8 = 14 */
169 "", /* word 0, bits 12-8 = 15 */
170 "", /* word 0, bits 12-8 = 16 */
171 "", /* word 0, bits 12-8 = 17 */
172 "", /* word 0, bits 12-8 = 18 */
173 "", /* word 0, bits 12-8 = 19 */
174 "", /* word 0, bits 12-8 = 1a */
175 "", /* word 0, bits 12-8 = 1b */
176 "", /* word 0, bits 12-8 = 1c */
177 "", /* word 0, bits 12-8 = 1d */
178 "", /* word 0, bits 12-8 = 1e */
Rob Landleyadde7982006-05-16 15:32:30 +0000179 "Unknown", /* word 0, bits 12-8 = 1f */
Eric Andersen3443bd72003-07-22 07:30:36 +0000180};
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000181
182static const char * const ata1_cfg_str[] = { /* word 0 in ATA-1 mode */
Rob Landleyadde7982006-05-16 15:32:30 +0000183 "Reserved", /* bit 0 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000184 "hard sectored", /* bit 1 */
185 "soft sectored", /* bit 2 */
186 "not MFM encoded ", /* bit 3 */
187 "head switch time > 15us", /* bit 4 */
188 "spindle motor control option", /* bit 5 */
189 "fixed drive", /* bit 6 */
190 "removable drive", /* bit 7 */
191 "disk xfer rate <= 5Mbs", /* bit 8 */
192 "disk xfer rate > 5Mbs, <= 10Mbs", /* bit 9 */
193 "disk xfer rate > 5Mbs", /* bit 10 */
194 "rotational speed tol.", /* bit 11 */
195 "data strobe offset option", /* bit 12 */
196 "track offset option", /* bit 13 */
197 "format speed tolerance gap reqd", /* bit 14 */
198 "ATAPI" /* bit 14 */
199};
200#endif
201
202/* word 1: number of logical cylinders */
203#define LCYLS_MAX 0x3fff /* maximum allowable value */
204
Eric Andersenaff114c2004-04-14 17:51:38 +0000205/* word 2: specific configuration
Eric Andersen3443bd72003-07-22 07:30:36 +0000206 * (a) require SET FEATURES to spin-up
207 * (b) require spin-up to fully reply to IDENTIFY DEVICE
208 */
209#define STBY_NID_VAL 0x37c8 /* (a) and (b) */
210#define STBY_ID_VAL 0x738c /* (a) and not (b) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000211#define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
Eric Andersen3443bd72003-07-22 07:30:36 +0000212#define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
213
214/* words 47 & 59: sector_xfer_max & sector_xfer_cur */
215#define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
216#define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
217
218/* word 49: capabilities 0 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000219#define STD_STBY 0x2000 /* 1=standard values supported (ATA);
Eric Andersen3443bd72003-07-22 07:30:36 +0000220 0=vendor specific values */
221#define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
222#define IORDY_OFF 0x0400 /* 1=may be disabled */
223#define LBA_SUP 0x0200 /* 1=Logical Block Address support */
224#define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
225#define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
226#define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
227#define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
228#define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
229
230/* word 50: capabilities 1 */
231#define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
232
233/* words 51 & 52: PIO & DMA cycle times */
234#define MODE 0xff00 /* the mode is in the MSBs */
235
236/* word 53: whats_valid */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000237#define OK_W88 0x0004 /* the ultra_dma info is valid */
Eric Andersen3443bd72003-07-22 07:30:36 +0000238#define OK_W64_70 0x0002 /* see above for word descriptions */
239#define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
240
241/*word 63,88: dma_mode, ultra_dma_mode*/
242#define MODE_MAX 7 /* bit definitions force udma <=7 (when
243 * udma >=8 comes out it'll have to be
244 * defined in a new dma_mode word!) */
245
246/* word 64: PIO transfer modes */
247#define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
248#define PIO_MODE_MAX 8 /* but all 8 bits are defined */
249
250/* word 75: queue_depth */
251#define DEPTH_BITS 0x001f /* bits used for queue depth */
252
253/* words 80-81: version numbers */
254/* NOVAL_0 or NOVAL_1 means device does not report version */
255
256/* word 81: minor version number */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000257#define MINOR_MAX 0x22
Eric Andersen3443bd72003-07-22 07:30:36 +0000258#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Rob Landley0e6a3e12006-04-28 01:33:30 +0000259static const char *minor_str[MINOR_MAX+2] = { /* word 81 value: */
260 "Unspecified", /* 0x0000 */
Rob Landleyadde7982006-05-16 15:32:30 +0000261 "ATA-1 X3T9.2 781D prior to rev.4", /* 0x0001 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000262 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
Rob Landleyadde7982006-05-16 15:32:30 +0000263 "ATA-1 X3T9.2 781D rev.4", /* 0x0003 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000264 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
Rob Landleyadde7982006-05-16 15:32:30 +0000265 "ATA-2 X3T10 948D prior to rev.2k", /* 0x0005 */
266 "ATA-3 X3T10 2008D rev.1", /* 0x0006 */
267 "ATA-2 X3T10 948D rev.2k", /* 0x0007 */
268 "ATA-3 X3T10 2008D rev.0", /* 0x0008 */
269 "ATA-2 X3T10 948D rev.3", /* 0x0009 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000270 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
Rob Landleyadde7982006-05-16 15:32:30 +0000271 "ATA-3 X3T10 2008D rev.6", /* 0x000b */
272 "ATA-3 X3T13 2008D rev.7 and 7a", /* 0x000c */
273 "ATA/ATAPI-4 X3T13 1153D rev.6", /* 0x000d */
274 "ATA/ATAPI-4 T13 1153D rev.13", /* 0x000e */
275 "ATA/ATAPI-4 X3T13 1153D rev.7", /* 0x000f */
276 "ATA/ATAPI-4 T13 1153D rev.18", /* 0x0010 */
277 "ATA/ATAPI-4 T13 1153D rev.15", /* 0x0011 */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000278 "ATA/ATAPI-4 published, ANSI INCITS 317-1998", /* 0x0012 */
Rob Landleyadde7982006-05-16 15:32:30 +0000279 "ATA/ATAPI-5 T13 1321D rev.3",
280 "ATA/ATAPI-4 T13 1153D rev.14", /* 0x0014 */
281 "ATA/ATAPI-5 T13 1321D rev.1", /* 0x0015 */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000282 "ATA/ATAPI-5 published, ANSI INCITS 340-2000", /* 0x0016 */
Rob Landleyadde7982006-05-16 15:32:30 +0000283 "ATA/ATAPI-4 T13 1153D rev.17", /* 0x0017 */
284 "ATA/ATAPI-6 T13 1410D rev.0", /* 0x0018 */
285 "ATA/ATAPI-6 T13 1410D rev.3a", /* 0x0019 */
286 "ATA/ATAPI-7 T13 1532D rev.1", /* 0x001a */
287 "ATA/ATAPI-6 T13 1410D rev.2", /* 0x001b */
288 "ATA/ATAPI-6 T13 1410D rev.1", /* 0x001c */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000289 "ATA/ATAPI-7 published, ANSI INCITS 397-2005", /* 0x001d */
Rob Landleyadde7982006-05-16 15:32:30 +0000290 "ATA/ATAPI-7 T13 1532D rev.0", /* 0x001e */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000291 "Reserved" /* 0x001f */
292 "Reserved" /* 0x0020 */
Rob Landleyadde7982006-05-16 15:32:30 +0000293 "ATA/ATAPI-7 T13 1532D rev.4a", /* 0x0021 */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000294 "ATA/ATAPI-6 published, ANSI INCITS 361-2002", /* 0x0022 */
295 "Reserved" /* 0x0023-0xfffe*/
Eric Andersen3443bd72003-07-22 07:30:36 +0000296};
297#endif
Rob Landley0e6a3e12006-04-28 01:33:30 +0000298static const char actual_ver[MINOR_MAX+2] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000299 /* word 81 value: */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000300 0, /* 0x0000 WARNING: */
301 1, /* 0x0001 WARNING: */
302 1, /* 0x0002 WARNING: */
303 1, /* 0x0003 WARNING: */
304 2, /* 0x0004 WARNING: This array */
305 2, /* 0x0005 WARNING: corresponds */
Eric Andersen3443bd72003-07-22 07:30:36 +0000306 3, /* 0x0006 WARNING: *exactly* */
307 2, /* 0x0007 WARNING: to the ATA/ */
308 3, /* 0x0008 WARNING: ATAPI version */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000309 2, /* 0x0009 WARNING: listed in */
310 3, /* 0x000a WARNING: the */
311 3, /* 0x000b WARNING: minor_str */
Eric Andersen3443bd72003-07-22 07:30:36 +0000312 3, /* 0x000c WARNING: array */
313 4, /* 0x000d WARNING: above. */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000314 4, /* 0x000e WARNING: */
315 4, /* 0x000f WARNING: if you change */
316 4, /* 0x0010 WARNING: that one, */
Eric Andersen3443bd72003-07-22 07:30:36 +0000317 4, /* 0x0011 WARNING: change this one */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000318 4, /* 0x0012 WARNING: too!!! */
Eric Andersen3443bd72003-07-22 07:30:36 +0000319 5, /* 0x0013 WARNING: */
320 4, /* 0x0014 WARNING: */
321 5, /* 0x0015 WARNING: */
322 5, /* 0x0016 WARNING: */
323 4, /* 0x0017 WARNING: */
324 6, /* 0x0018 WARNING: */
325 6, /* 0x0019 WARNING: */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000326 7, /* 0x001a WARNING: */
Eric Andersen3443bd72003-07-22 07:30:36 +0000327 6, /* 0x001b WARNING: */
328 6, /* 0x001c WARNING: */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000329 7, /* 0x001d WARNING: */
330 7, /* 0x001e WARNING: */
331 0, /* 0x001f WARNING: */
332 0, /* 0x0020 WARNING: */
333 7, /* 0x0021 WARNING: */
334 6, /* 0x0022 WARNING: */
335 0 /* 0x0023-0xfffe */
Eric Andersen3443bd72003-07-22 07:30:36 +0000336};
337
338/* words 82-84: cmds/feats supported */
339#define CMDS_W82 0x77ff /* word 82: defined command locations*/
340#define CMDS_W83 0x3fff /* word 83: defined command locations*/
341#define CMDS_W84 0x002f /* word 83: defined command locations*/
Glenn L McGrath07085852003-10-09 07:28:22 +0000342#define SUPPORT_48_BIT 0x0400
Eric Andersen3443bd72003-07-22 07:30:36 +0000343#define NUM_CMD_FEAT_STR 48
344
345#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000346static const char * const cmd_feat_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000347 "", /* word 82 bit 15: obsolete */
348 "NOP cmd", /* word 82 bit 14 */
349 "READ BUFFER cmd", /* word 82 bit 13 */
350 "WRITE BUFFER cmd", /* word 82 bit 12 */
351 "", /* word 82 bit 11: obsolete */
352 "Host Protected Area feature set", /* word 82 bit 10 */
353 "DEVICE RESET cmd", /* word 82 bit 9 */
354 "SERVICE interrupt", /* word 82 bit 8 */
355 "Release interrupt", /* word 82 bit 7 */
356 "Look-ahead", /* word 82 bit 6 */
357 "Write cache", /* word 82 bit 5 */
358 "PACKET command feature set", /* word 82 bit 4 */
359 "Power Management feature set", /* word 82 bit 3 */
360 "Removable Media feature set", /* word 82 bit 2 */
361 "Security Mode feature set", /* word 82 bit 1 */
362 "SMART feature set", /* word 82 bit 0 */
363 /* --------------*/
364 "", /* word 83 bit 15: !valid bit */
365 "", /* word 83 bit 14: valid bit */
Rob Landleyadde7982006-05-16 15:32:30 +0000366 "FLUSH CACHE EXT cmd", /* word 83 bit 13 */
367 "Mandatory FLUSH CACHE cmd ", /* word 83 bit 12 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000368 "Device Configuration Overlay feature set ",
369 "48-bit Address feature set ", /* word 83 bit 10 */
370 "",
371 "SET MAX security extension", /* word 83 bit 8 */
372 "Address Offset Reserved Area Boot", /* word 83 bit 7 */
373 "SET FEATURES subcommand required to spinup after power up",
374 "Power-Up In Standby feature set", /* word 83 bit 5 */
375 "Removable Media Status Notification feature set",
Eric Andersen06d4ec22004-03-19 10:53:52 +0000376 "Adv. Power Management feature set",/* word 83 bit 3 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000377 "CFA feature set", /* word 83 bit 2 */
378 "READ/WRITE DMA QUEUED", /* word 83 bit 1 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000379 "DOWNLOAD MICROCODE cmd", /* word 83 bit 0 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000380 /* --------------*/
381 "", /* word 84 bit 15: !valid bit */
382 "", /* word 84 bit 14: valid bit */
383 "", /* word 84 bit 13: reserved */
384 "", /* word 84 bit 12: reserved */
385 "", /* word 84 bit 11: reserved */
386 "", /* word 84 bit 10: reserved */
387 "", /* word 84 bit 9: reserved */
388 "", /* word 84 bit 8: reserved */
389 "", /* word 84 bit 7: reserved */
390 "", /* word 84 bit 6: reserved */
391 "General Purpose Logging feature set", /* word 84 bit 5 */
392 "", /* word 84 bit 4: reserved */
393 "Media Card Pass Through Command feature set ",
394 "Media serial number ", /* word 84 bit 2 */
395 "SMART self-test ", /* word 84 bit 1 */
396 "SMART error logging " /* word 84 bit 0 */
397};
Rob Landley0f0b6452006-05-03 18:28:06 +0000398
399static void identify(uint16_t *id_supplied) ATTRIBUTE_NORETURN;
400static void identify_from_stdin(void) ATTRIBUTE_NORETURN;
Rob Landley9c6f9552006-06-08 14:11:36 +0000401#else
402void identify_from_stdin(void);
Eric Andersen3443bd72003-07-22 07:30:36 +0000403#endif
404
405
406/* words 85-87: cmds/feats enabled */
407/* use cmd_feat_str[] to display what commands and features have
Glenn L McGrath07085852003-10-09 07:28:22 +0000408 * been enabled with words 85-87
Eric Andersen3443bd72003-07-22 07:30:36 +0000409 */
410
411/* words 89, 90, SECU ERASE TIME */
412#define ERASE_BITS 0x00ff
413
414/* word 92: master password revision */
415/* NOVAL_0 or NOVAL_1 means no support for master password revision */
416
417/* word 93: hw reset result */
418#define CBLID 0x2000 /* CBLID status */
419#define RST0 0x0001 /* 1=reset to device #0 */
420#define DEV_DET 0x0006 /* how device num determined */
421#define JUMPER_VAL 0x0002 /* device num determined by jumper */
422#define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
423
424/* word 127: removable media status notification feature set support */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000425#define RM_STAT_BITS 0x0003
Eric Andersen3443bd72003-07-22 07:30:36 +0000426#define RM_STAT_SUP 0x0001
Glenn L McGrath07085852003-10-09 07:28:22 +0000427
Eric Andersen3443bd72003-07-22 07:30:36 +0000428/* word 128: security */
429#define SECU_ENABLED 0x0002
430#define SECU_LEVEL 0x0010
431#define NUM_SECU_STR 6
432#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000433static const char * const secu_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000434 "supported", /* word 128, bit 0 */
435 "enabled", /* word 128, bit 1 */
436 "locked", /* word 128, bit 2 */
437 "frozen", /* word 128, bit 3 */
438 "expired: security count", /* word 128, bit 4 */
439 "supported: enhanced erase" /* word 128, bit 5 */
440};
441#endif
442
443/* word 160: CFA power mode */
444#define VALID_W160 0x8000 /* 1=word valid */
445#define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
446#define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
447#define MAX_AMPS 0x0fff /* value = max current in ma */
448
449/* word 255: integrity */
450#define SIG 0x00ff /* signature location */
451#define SIG_VAL 0x00A5 /* signature value */
452
Glenn L McGrath07085852003-10-09 07:28:22 +0000453#define TIMING_MB 64
454#define TIMING_BUF_MB 1
455#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
456#define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
457#define BUFCACHE_FACTOR 2
458
459#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
460
461/* Busybox messages and functions */
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000462static int bb_ioctl(int fd, int request, void *argp, const char *string)
Glenn L McGrath07085852003-10-09 07:28:22 +0000463{
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000464 int e = ioctl(fd, request, argp);
465 if (e && string)
Eric Andersen06d4ec22004-03-19 10:53:52 +0000466 bb_perror_msg(" %s", string);
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000467 return e;
Glenn L McGrath07085852003-10-09 07:28:22 +0000468}
469
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000470static int bb_ioctl_alt(int fd, int cmd, unsigned char *args, int alt, const char *string)
471{
472 if (!ioctl(fd, cmd, args))
473 return 0;
474 args[0] = alt;
475 return bb_ioctl(fd, cmd, args, string);
476}
477
Rob Landleya3e4f382006-04-29 16:06:31 +0000478static void on_off(unsigned int value);
Glenn L McGrath07085852003-10-09 07:28:22 +0000479
Rob Landleyadde7982006-05-16 15:32:30 +0000480static void print_flag_on_off(unsigned long get_arg, const char *s, unsigned long arg)
Glenn L McGrath07085852003-10-09 07:28:22 +0000481{
Rob Landleyadde7982006-05-16 15:32:30 +0000482
Glenn L McGrath07085852003-10-09 07:28:22 +0000483 if (get_arg)
484 {
Rob Landleyadde7982006-05-16 15:32:30 +0000485 printf(" setting %s to %ld", s, arg);
Glenn L McGrath07085852003-10-09 07:28:22 +0000486 on_off(arg);
487 }
488}
489
490static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
Rob Landleyadde7982006-05-16 15:32:30 +0000491 const char * str)
Glenn L McGrath07085852003-10-09 07:28:22 +0000492{
Rob Landleya3e4f382006-04-29 16:06:31 +0000493 if (ioctl(fd, request, &argp) != 0)
Eric Andersen06d4ec22004-03-19 10:53:52 +0000494 bb_perror_msg(" %s", string);
Glenn L McGrath07085852003-10-09 07:28:22 +0000495 else
496 {
Rob Landleyadde7982006-05-16 15:32:30 +0000497 printf(" %s\t= %2ld", str, (unsigned long) argp);
Glenn L McGrath07085852003-10-09 07:28:22 +0000498 on_off((unsigned long) argp);
499 }
500}
Eric Andersen416c2422003-12-12 00:08:57 +0000501
502#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000503static void print_ascii(uint16_t *p, uint8_t length);
504
505static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
506{
Rob Landleya3e4f382006-04-29 16:06:31 +0000507 if (val[i])
Glenn L McGrath07085852003-10-09 07:28:22 +0000508 {
509 printf("\t%-20s",string);
510 print_ascii(&val[i], n);
511 }
512}
Eric Andersen416c2422003-12-12 00:08:57 +0000513#endif
Glenn L McGrath07085852003-10-09 07:28:22 +0000514/* end of busybox specific stuff */
515
Eric Andersen3443bd72003-07-22 07:30:36 +0000516#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000517static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
518{
Eric Andersen3443bd72003-07-22 07:30:36 +0000519 uint16_t ii;
520 uint8_t err_dma = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000521
Rob Landleya3e4f382006-04-29 16:06:31 +0000522 for (ii = 0; ii <= MODE_MAX; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000523 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000524 if (mode_sel & 0x0001)
Glenn L McGrath07085852003-10-09 07:28:22 +0000525 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000526 printf("*%cdma%u ",cc,ii);
Rob Landleya3e4f382006-04-29 16:06:31 +0000527 if (*have_mode)
Glenn L McGrath07085852003-10-09 07:28:22 +0000528 err_dma = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000529 *have_mode = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000530 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000531 else if (mode_sup & 0x0001)
Glenn L McGrath07085852003-10-09 07:28:22 +0000532 printf("%cdma%u ",cc,ii);
533
534 mode_sup >>=1;
535 mode_sel >>=1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000536 }
537 return err_dma;
538}
Glenn L McGrath07085852003-10-09 07:28:22 +0000539
Eric Andersen3443bd72003-07-22 07:30:36 +0000540static void print_ascii(uint16_t *p, uint8_t length) {
541 uint8_t ii;
542 char cl;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000543
Eric Andersen3443bd72003-07-22 07:30:36 +0000544 /* find first non-space & print it */
Rob Landleya3e4f382006-04-29 16:06:31 +0000545 for (ii = 0; ii< length; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000546 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000547 if (((char) 0x00ff&((*p)>>8)) != ' ')
Glenn L McGrath07085852003-10-09 07:28:22 +0000548 break;
Rob Landleya3e4f382006-04-29 16:06:31 +0000549 if ((cl = (char) 0x00ff&(*p)) != ' ')
Glenn L McGrath07085852003-10-09 07:28:22 +0000550 {
Rob Landleyadde7982006-05-16 15:32:30 +0000551 if (cl != '\0') printf("%c",cl);
Glenn L McGrath07085852003-10-09 07:28:22 +0000552 p++;
553 ii++;
Eric Andersen3443bd72003-07-22 07:30:36 +0000554 break;
555 }
556 p++;
557 }
558 /* print the rest */
Rob Landleya3e4f382006-04-29 16:06:31 +0000559 for (; ii< length; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000560 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000561 if (!(*p))
Glenn L McGrath07085852003-10-09 07:28:22 +0000562 break; /* some older devices have NULLs */
Eric Andersen3443bd72003-07-22 07:30:36 +0000563 printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
564 p++;
565 }
566 printf("\n");
567}
568
Rob Landley0753f4a2006-06-07 00:27:25 +0000569// Parse 512 byte disk identification block and print much crap.
570
Rob Landley6389ff12006-05-01 19:28:53 +0000571static void identify(uint16_t *id_supplied)
Eric Andersen3443bd72003-07-22 07:30:36 +0000572{
Rob Landley5bc3f052006-04-29 19:11:24 +0000573 uint16_t buf[256];
574 uint16_t *val, ii, jj, kk;
Eric Andersen3443bd72003-07-22 07:30:36 +0000575 uint16_t like_std = 1, std = 0, min_std = 0xffff;
576 uint16_t dev = NO_DEV, eqpt = NO_DEV;
577 uint8_t have_mode = 0, err_dma = 0;
578 uint8_t chksum = 0;
579 uint32_t ll, mm, nn, oo;
Rob Landley2e2d7522006-04-29 15:23:33 +0000580 uint64_t bbbig; /* (:) */
Rob Landleyadde7982006-05-16 15:32:30 +0000581 const char *strng;
Eric Andersen3443bd72003-07-22 07:30:36 +0000582
Rob Landley0753f4a2006-06-07 00:27:25 +0000583 // Adjust for endianness if necessary.
584
Rob Landley5bc3f052006-04-29 19:11:24 +0000585 if (BB_BIG_ENDIAN) {
586 swab(id_supplied, buf, sizeof(buf));
587 val = buf;
588 } else val = id_supplied;
589
Eric Andersen3443bd72003-07-22 07:30:36 +0000590 chksum &= 0xff;
591
592 /* check if we recognise the device type */
593 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000594 if(!(val[GEN_CONFIG] & NOT_ATA))
595 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000596 dev = ATA_DEV;
597 printf("ATA device, with ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000598 }
599 else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
600 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000601 dev = ATA_DEV;
602 like_std = 4;
603 printf("CompactFlash ATA device, with ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000604 }
605 else if(!(val[GEN_CONFIG] & NOT_ATAPI))
606 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000607 dev = ATAPI_DEV;
608 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
609 printf("ATAPI %s, with ", pkt_str[eqpt]);
610 like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000611 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000612 else
613 /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
614 bb_error_msg_and_die("Unknown device type");
Eric Andersen3443bd72003-07-22 07:30:36 +0000615
Rob Landleyadde7982006-05-16 15:32:30 +0000616 printf("%sremovable media\n", !(val[GEN_CONFIG] & MEDIA_REMOVABLE) ? "non-" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +0000617 /* Info from the specific configuration word says whether or not the
618 * ID command completed correctly. It is only defined, however in
Glenn L McGrath07085852003-10-09 07:28:22 +0000619 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
Eric Andersen3443bd72003-07-22 07:30:36 +0000620 * standards. Since the values allowed for this word are extremely
621 * specific, it should be safe to check it now, even though we don't
622 * know yet what standard this device is using.
623 */
Rob Landleya3e4f382006-04-29 16:06:31 +0000624 if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
Glenn L McGrath07085852003-10-09 07:28:22 +0000625 (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
626 {
627 like_std = 5;
Rob Landleyadde7982006-05-16 15:32:30 +0000628 if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL))
629 printf("powers-up in standby; SET FEATURES subcmd spins-up.\n");
630 if(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE))
631 printf("\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000632 }
633
634 /* output the model and serial numbers and the fw revision */
Glenn L McGrath07085852003-10-09 07:28:22 +0000635 xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
636 xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
637 xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
638 xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
639 xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
Eric Andersen3443bd72003-07-22 07:30:36 +0000640
641 /* major & minor standards version number (Note: these words were not
642 * defined until ATA-3 & the CDROM std uses different words.) */
643 printf("Standards:");
Rob Landleya3e4f382006-04-29 16:06:31 +0000644 if (eqpt != CDROM)
Glenn L McGrath07085852003-10-09 07:28:22 +0000645 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000646 if (val[MINOR] && (val[MINOR] <= MINOR_MAX))
Glenn L McGrath07085852003-10-09 07:28:22 +0000647 {
Rob Landleyadde7982006-05-16 15:32:30 +0000648 if(like_std < 3) like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000649 std = actual_ver[val[MINOR]];
Rob Landleyadde7982006-05-16 15:32:30 +0000650 if(std) printf("\n\tUsed: %s ",minor_str[val[MINOR]]);
Glenn L McGrath07085852003-10-09 07:28:22 +0000651
Eric Andersen3443bd72003-07-22 07:30:36 +0000652 }
653 /* looks like when they up-issue the std, they obsolete one;
Glenn L McGrath07085852003-10-09 07:28:22 +0000654 * thus, only the newest 4 issues need be supported. (That's
Eric Andersen3443bd72003-07-22 07:30:36 +0000655 * what "kk" and "min_std" are all about.) */
Rob Landleya3e4f382006-04-29 16:06:31 +0000656 if (val[MAJOR] && (val[MAJOR] !=NOVAL_1))
Glenn L McGrath07085852003-10-09 07:28:22 +0000657 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000658 printf("\n\tSupported: ");
659 jj = val[MAJOR] << 1;
660 kk = like_std >4 ? like_std-4: 0;
Rob Landleya3e4f382006-04-29 16:06:31 +0000661 for (ii = 14; (ii >0)&&(ii>kk); ii--)
Glenn L McGrath07085852003-10-09 07:28:22 +0000662 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000663 if (jj & 0x8000)
Glenn L McGrath07085852003-10-09 07:28:22 +0000664 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000665 printf("%u ", ii);
Rob Landleya3e4f382006-04-29 16:06:31 +0000666 if (like_std < ii)
Glenn L McGrath07085852003-10-09 07:28:22 +0000667 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000668 like_std = ii;
669 kk = like_std >4 ? like_std-4: 0;
670 }
Rob Landleyadde7982006-05-16 15:32:30 +0000671 if(min_std > ii) min_std = ii;
Eric Andersen3443bd72003-07-22 07:30:36 +0000672 }
673 jj <<= 1;
674 }
Rob Landleyadde7982006-05-16 15:32:30 +0000675 if(like_std < 3) like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000676 }
677 /* Figure out what standard the device is using if it hasn't told
678 * us. If we know the std, check if the device is using any of
679 * the words from the next level up. It happens.
680 */
Rob Landleyadde7982006-05-16 15:32:30 +0000681 if(like_std < std) like_std = std;
Glenn L McGrath07085852003-10-09 07:28:22 +0000682
Rob Landleya3e4f382006-04-29 16:06:31 +0000683 if (((std == 5) || (!std && (like_std < 6))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000684 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
685 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
686 ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
687 ( val[CMDS_SUPP_2] & CMDS_W84) ) ) )
688 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000689 like_std = 6;
Glenn L McGrath07085852003-10-09 07:28:22 +0000690 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000691 else if (((std == 4) || (!std && (like_std < 5))) &&
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000692 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
Glenn L McGrath07085852003-10-09 07:28:22 +0000693 (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
694 ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
695 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
696 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000697 like_std = 5;
Glenn L McGrath07085852003-10-09 07:28:22 +0000698 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000699 else if (((std == 3) || (!std && (like_std < 4))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000700 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
701 ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
702 (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
703 (( val[CAPAB_1] & VALID) == VALID_VAL) ||
704 (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
705 (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
706 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000707 like_std = 4;
Glenn L McGrath07085852003-10-09 07:28:22 +0000708 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000709 else if (((std == 2) || (!std && (like_std < 3))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000710 ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
711 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000712 like_std = 3;
Glenn L McGrath07085852003-10-09 07:28:22 +0000713 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000714 else if (((std == 1) || (!std && (like_std < 2))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000715 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
716 (val[WHATS_VALID] & OK_W64_70)) )
717 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000718 like_std = 2;
719 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000720 if (!std)
Eric Andersen3443bd72003-07-22 07:30:36 +0000721 printf("\n\tLikely used: %u\n",like_std);
Rob Landleya3e4f382006-04-29 16:06:31 +0000722 else if (like_std > std)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000723 printf("& some of %u\n",like_std);
Glenn L McGrath07085852003-10-09 07:28:22 +0000724 else
725 printf("\n");
726 }
727 else
728 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000729 /* TBD: do CDROM stuff more thoroughly. For now... */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000730 kk = 0;
Rob Landleya3e4f382006-04-29 16:06:31 +0000731 if (val[CDR_MINOR] == 9)
Glenn L McGrath07085852003-10-09 07:28:22 +0000732 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000733 kk = 1;
734 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
735 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000736 if (val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1))
Glenn L McGrath07085852003-10-09 07:28:22 +0000737 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000738 kk = 1;
739 printf("\n\tSupported: CD-ROM ATAPI");
740 jj = val[CDR_MAJOR] >> 1;
Rob Landleya3e4f382006-04-29 16:06:31 +0000741 for (ii = 1; ii <15; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000742 {
Rob Landleyadde7982006-05-16 15:32:30 +0000743 if(jj & 0x0001) printf("-%u ", ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000744 jj >>= 1;
745 }
746 }
Rob Landleyadde7982006-05-16 15:32:30 +0000747 printf("%s\n", (!kk) ? "\n\tLikely used CD-ROM ATAPI-1" : "" );
Eric Andersen3443bd72003-07-22 07:30:36 +0000748 /* the cdrom stuff is more like ATA-2 than anything else, so: */
749 like_std = 2;
750 }
751
Rob Landleya3e4f382006-04-29 16:06:31 +0000752 if (min_std == 0xffff)
Glenn L McGrath07085852003-10-09 07:28:22 +0000753 min_std = like_std > 4 ? like_std - 3 : 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000754
755 printf("Configuration:\n");
756 /* more info from the general configuration word */
Rob Landleya3e4f382006-04-29 16:06:31 +0000757 if ((eqpt != CDROM) && (like_std == 1))
Glenn L McGrath07085852003-10-09 07:28:22 +0000758 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000759 jj = val[GEN_CONFIG] >> 1;
Rob Landleya3e4f382006-04-29 16:06:31 +0000760 for (ii = 1; ii < 15; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000761 {
Rob Landleyadde7982006-05-16 15:32:30 +0000762 if(jj & 0x0001) printf("\t%s\n",ata1_cfg_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000763 jj >>=1;
764 }
765 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000766 if (dev == ATAPI_DEV)
Glenn L McGrath07085852003-10-09 07:28:22 +0000767 {
Rob Landleyadde7982006-05-16 15:32:30 +0000768 if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_3MS_VAL)
769 strng = "3ms";
770 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_INTR_VAL)
771 strng = "<=10ms with INTRQ";
772 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_50US_VAL)
773 strng ="50us";
774 else
775 strng = "Unknown";
776 printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */
777
778 if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_12_VAL)
779 strng = "12 bytes";
780 else if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_16_VAL)
781 strng = "16 bytes";
782 else
783 strng = "Unknown";
784 puts(strng);
Glenn L McGrath07085852003-10-09 07:28:22 +0000785 }
786 else
787 {
788 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000789 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
790 mm = 0; bbbig = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000791 if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000792 printf("\tCHS addressing not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000793 else
794 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000795 jj = val[WHATS_VALID] & OK_W54_58;
Glenn L McGrath07085852003-10-09 07:28:22 +0000796 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",
797 val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
798
Rob Landleyadde7982006-05-16 15:32:30 +0000799 if ((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES]))
800 printf("\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
Glenn L McGrath07085852003-10-09 07:28:22 +0000801
Rob Landleya3e4f382006-04-29 16:06:31 +0000802 if (jj)
Glenn L McGrath07085852003-10-09 07:28:22 +0000803 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000804 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
Rob Landleya3e4f382006-04-29 16:06:31 +0000805 if (like_std < 3)
Glenn L McGrath07085852003-10-09 07:28:22 +0000806 {
807 /* check Endian of capacity bytes */
Eric Andersen3443bd72003-07-22 07:30:36 +0000808 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
809 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
Rob Landleya3e4f382006-04-29 16:06:31 +0000810 if (abs(mm - nn) > abs(oo - nn))
Eric Andersen3443bd72003-07-22 07:30:36 +0000811 mm = oo;
812 }
813 printf("\tCHS current addressable sectors:%11u\n",mm);
Glenn L McGrath07085852003-10-09 07:28:22 +0000814 }
Eric Andersen3443bd72003-07-22 07:30:36 +0000815 }
816 /* LBA addressing */
817 printf("\tLBA user addressable sectors:%11u\n",ll);
Rob Landleya3e4f382006-04-29 16:06:31 +0000818 if ( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000819 (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
820 {
Rob Landley2e2d7522006-04-29 15:23:33 +0000821 bbbig = (uint64_t)val[LBA_64_MSB] << 48 |
822 (uint64_t)val[LBA_48_MSB] << 32 |
823 (uint64_t)val[LBA_MID] << 16 |
Glenn L McGrath07085852003-10-09 07:28:22 +0000824 val[LBA_LSB] ;
Rob Landley81dab2c2006-05-28 01:56:08 +0000825 printf("\tLBA48 user addressable sectors:%11"PRIu64"\n",bbbig);
Eric Andersen3443bd72003-07-22 07:30:36 +0000826 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000827
828 if (!bbbig)
Rob Landley2e2d7522006-04-29 15:23:33 +0000829 bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
Rob Landley81dab2c2006-05-28 01:56:08 +0000830 printf("\tdevice size with M = 1024*1024: %11"PRIu64" MBytes\n",bbbig>>11);
Eric Andersen3443bd72003-07-22 07:30:36 +0000831 bbbig = (bbbig<<9)/1000000;
Rob Landley81dab2c2006-05-28 01:56:08 +0000832 printf("\tdevice size with M = 1000*1000: %11"PRIu64" MBytes ",bbbig);
Glenn L McGrath07085852003-10-09 07:28:22 +0000833
Rob Landleyadde7982006-05-16 15:32:30 +0000834 if (bbbig > 1000)
Rob Landley81dab2c2006-05-28 01:56:08 +0000835 printf("(%"PRIu64" GB)\n", bbbig/1000);
Rob Landleyadde7982006-05-16 15:32:30 +0000836 else
837 printf("\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000838 }
839
840 /* hw support of commands (capabilities) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000841 printf("Capabilities:\n\t");
842
Rob Landleya3e4f382006-04-29 16:06:31 +0000843 if (dev == ATAPI_DEV)
Glenn L McGrath07085852003-10-09 07:28:22 +0000844 {
Rob Landleyadde7982006-05-16 15:32:30 +0000845 if (eqpt != CDROM && (val[CAPAB_0] & CMD_Q_SUP)) printf("Cmd queuing, ");
846 if (val[CAPAB_0] & OVLP_SUP) printf("Cmd overlap, ");
Eric Andersen3443bd72003-07-22 07:30:36 +0000847 }
Rob Landleyadde7982006-05-16 15:32:30 +0000848 if (val[CAPAB_0] & LBA_SUP) printf("LBA, ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000849
Rob Landleya3e4f382006-04-29 16:06:31 +0000850 if (like_std != 1)
Glenn L McGrath07085852003-10-09 07:28:22 +0000851 {
Rob Landleyadde7982006-05-16 15:32:30 +0000852 printf("IORDY%s(can%s be disabled)\n",
853 !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "",
854 (val[CAPAB_0] & IORDY_OFF) ? "" :"not");
Glenn L McGrath07085852003-10-09 07:28:22 +0000855 }
856 else
857 printf("no IORDY\n");
858
Rob Landleya3e4f382006-04-29 16:06:31 +0000859 if ((like_std == 1) && val[BUF_TYPE])
Glenn L McGrath07085852003-10-09 07:28:22 +0000860 {
Rob Landleyadde7982006-05-16 15:32:30 +0000861 printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE],
862 (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector",
863 (val[BUF_TYPE] > 2) ? " with read caching ability" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +0000864 }
Rob Landleyadde7982006-05-16 15:32:30 +0000865
Rob Landleya3e4f382006-04-29 16:06:31 +0000866 if ((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
Glenn L McGrath07085852003-10-09 07:28:22 +0000867 {
Rob Landleyadde7982006-05-16 15:32:30 +0000868 printf("\tBuffer size: %.1fkB\n",(float)val[BUFFER__SIZE]/2);
Eric Andersen3443bd72003-07-22 07:30:36 +0000869 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000870 if ((min_std < 4) && (val[RW_LONG]))
Glenn L McGrath07085852003-10-09 07:28:22 +0000871 {
Rob Landleyadde7982006-05-16 15:32:30 +0000872 printf("\tbytes avail on r/w long: %u\n",val[RW_LONG]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000873 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000874 if ((eqpt != CDROM) && (like_std > 3))
Glenn L McGrath07085852003-10-09 07:28:22 +0000875 {
Rob Landleyadde7982006-05-16 15:32:30 +0000876 printf("\tQueue depth: %u\n",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
Eric Andersen3443bd72003-07-22 07:30:36 +0000877 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000878
Rob Landleya3e4f382006-04-29 16:06:31 +0000879 if (dev == ATA_DEV)
Glenn L McGrath07085852003-10-09 07:28:22 +0000880 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000881 if (like_std == 1)
Glenn L McGrath07085852003-10-09 07:28:22 +0000882 printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
883 else
884 {
Rob Landleyadde7982006-05-16 15:32:30 +0000885 printf("\tStandby timer values: spec'd by %s", (val[CAPAB_0] & STD_STBY) ? "Standard" : "Vendor");
Rob Landleya3e4f382006-04-29 16:06:31 +0000886 if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
Glenn L McGrath07085852003-10-09 07:28:22 +0000887 printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
888 else
889 printf("\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000890 }
891 printf("\tR/W multiple sector transfer: ");
Rob Landleya3e4f382006-04-29 16:06:31 +0000892 if ((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
Eric Andersen3443bd72003-07-22 07:30:36 +0000893 printf("not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000894 else
895 {
896 printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
Rob Landleyadde7982006-05-16 15:32:30 +0000897 if (val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
898 printf("%u\n", val[SECTOR_XFER_CUR] & SECTOR_XFER);
899 else
900 printf("?\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000901 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000902 if ((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
Glenn L McGrath07085852003-10-09 07:28:22 +0000903 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000904 /* We print out elsewhere whether the APM feature is enabled or
905 not. If it's not enabled, let's not repeat the info; just print
906 nothing here. */
Glenn L McGrath07085852003-10-09 07:28:22 +0000907 printf("\tAdvancedPM level: ");
908 if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
909 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000910 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
Eric Andersen3443bd72003-07-22 07:30:36 +0000911 printf("%u (0x%x)\n", apm_level, apm_level);
Glenn L McGrath07085852003-10-09 07:28:22 +0000912 }
913 else
Eric Andersen3443bd72003-07-22 07:30:36 +0000914 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000915 }
Rob Landleyadde7982006-05-16 15:32:30 +0000916 if (like_std > 5 && val[ACOUSTIC]) {
917 printf("\tRecommended acoustic management value: %u, current value: %u\n",
Glenn L McGrath07085852003-10-09 07:28:22 +0000918 (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
Eric Andersen3443bd72003-07-22 07:30:36 +0000919 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000920 }
921 else
922 {
923 /* ATAPI */
Rob Landleyadde7982006-05-16 15:32:30 +0000924 if (eqpt != CDROM && (val[CAPAB_0] & SWRST_REQ))
925 printf("\tATA sw reset required\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000926
Rob Landleya3e4f382006-04-29 16:06:31 +0000927 if (val[PKT_REL] || val[SVC_NBSY])
Glenn L McGrath07085852003-10-09 07:28:22 +0000928 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000929 printf("\tOverlap support:");
Rob Landleyadde7982006-05-16 15:32:30 +0000930 if (val[PKT_REL]) printf(" %uus to release bus.",val[PKT_REL]);
931 if (val[SVC_NBSY]) printf(" %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000932 printf("\n");
933 }
934 }
935
936 /* DMA stuff. Check that only one DMA mode is selected. */
937 printf("\tDMA: ");
Rob Landleya3e4f382006-04-29 16:06:31 +0000938 if (!(val[CAPAB_0] & DMA_SUP))
Eric Andersen3443bd72003-07-22 07:30:36 +0000939 printf("not supported\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000940 else
941 {
Rob Landleyadde7982006-05-16 15:32:30 +0000942 if (val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
943 printf(" sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
Rob Landleya3e4f382006-04-29 16:06:31 +0000944 if (val[SINGLE_DMA])
Glenn L McGrath07085852003-10-09 07:28:22 +0000945 {
946 jj = val[SINGLE_DMA];
947 kk = val[SINGLE_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +0000948 err_dma += mode_loop(jj,kk,'s',&have_mode);
949 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000950 if (val[MULTI_DMA])
Glenn L McGrath07085852003-10-09 07:28:22 +0000951 {
952 jj = val[MULTI_DMA];
953 kk = val[MULTI_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +0000954 err_dma += mode_loop(jj,kk,'m',&have_mode);
955 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000956 if ((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
Glenn L McGrath07085852003-10-09 07:28:22 +0000957 {
958 jj = val[ULTRA_DMA];
959 kk = val[ULTRA_DMA] >> 8;
Eric Andersen3443bd72003-07-22 07:30:36 +0000960 err_dma += mode_loop(jj,kk,'u',&have_mode);
961 }
Rob Landleyadde7982006-05-16 15:32:30 +0000962 if (err_dma || !have_mode) printf("(?)");
Eric Andersen3443bd72003-07-22 07:30:36 +0000963 printf("\n");
964
Rob Landleyadde7982006-05-16 15:32:30 +0000965 if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
966 printf("\t\tInterleaved DMA support\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000967
Rob Landleya3e4f382006-04-29 16:06:31 +0000968 if ((val[WHATS_VALID] & OK_W64_70) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000969 (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
970 {
Rob Landleyadde7982006-05-16 15:32:30 +0000971 printf("\t\tCycle time:");
972 if (val[DMA_TIME_MIN]) printf(" min=%uns",val[DMA_TIME_MIN]);
973 if (val[DMA_TIME_NORM]) printf(" recommended=%uns",val[DMA_TIME_NORM]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000974 printf("\n");
975 }
976 }
977
978 /* Programmed IO stuff */
979 printf("\tPIO: ");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000980 /* If a drive supports mode n (e.g. 3), it also supports all modes less
Eric Andersen3443bd72003-07-22 07:30:36 +0000981 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
Rob Landleya3e4f382006-04-29 16:06:31 +0000982 if ((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
Glenn L McGrath07085852003-10-09 07:28:22 +0000983 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000984 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
Rob Landleya3e4f382006-04-29 16:06:31 +0000985 for (ii = 0; ii <= PIO_MODE_MAX ; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +0000986 {
Rob Landleyadde7982006-05-16 15:32:30 +0000987 if (jj & 0x0001) printf("pio%d ",ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000988 jj >>=1;
989 }
990 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000991 }
Rob Landleya3e4f382006-04-29 16:06:31 +0000992 else if (((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
Glenn L McGrath07085852003-10-09 07:28:22 +0000993 {
Rob Landleya3e4f382006-04-29 16:06:31 +0000994 for (ii = 0; ii <= val[PIO_MODE]>>8; ii++)
Eric Andersen3443bd72003-07-22 07:30:36 +0000995 printf("pio%d ",ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000996 printf("\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000997 }
998 else
999 printf("unknown\n");
1000
Rob Landleya3e4f382006-04-29 16:06:31 +00001001 if (val[WHATS_VALID] & OK_W64_70)
Glenn L McGrath07085852003-10-09 07:28:22 +00001002 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001003 if (val[PIO_NO_FLOW] || val[PIO_FLOW])
Glenn L McGrath07085852003-10-09 07:28:22 +00001004 {
Rob Landleyadde7982006-05-16 15:32:30 +00001005 printf("\t\tCycle time:");
1006 if (val[PIO_NO_FLOW]) printf(" no flow control=%uns", val[PIO_NO_FLOW]);
1007 if (val[PIO_FLOW]) printf(" IORDY flow control=%uns", val[PIO_FLOW]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001008 printf("\n");
1009 }
1010 }
1011
Rob Landleya3e4f382006-04-29 16:06:31 +00001012 if ((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
Glenn L McGrath07085852003-10-09 07:28:22 +00001013 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001014 printf("Commands/features:\n\tEnabled\tSupported:\n");
1015 jj = val[CMDS_SUPP_0];
1016 kk = val[CMDS_EN_0];
Rob Landleya3e4f382006-04-29 16:06:31 +00001017 for (ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +00001018 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001019 if ((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
Glenn L McGrath07085852003-10-09 07:28:22 +00001020 {
Rob Landleyadde7982006-05-16 15:32:30 +00001021 printf("\t%s\t%s\n", (kk & 0x8000) ? " *" : "", cmd_feat_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001022 }
1023 jj <<=1; kk<<=1;
Rob Landleya3e4f382006-04-29 16:06:31 +00001024 if (ii%16 == 15)
Glenn L McGrath07085852003-10-09 07:28:22 +00001025 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001026 jj = val[CMDS_SUPP_0+1+(ii/16)];
1027 kk = val[CMDS_EN_0+1+(ii/16)];
1028 }
Rob Landleya3e4f382006-04-29 16:06:31 +00001029 if (ii == 31)
Glenn L McGrath07085852003-10-09 07:28:22 +00001030 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001031 if ((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
Glenn L McGrath07085852003-10-09 07:28:22 +00001032 ii +=16;
Eric Andersen3443bd72003-07-22 07:30:36 +00001033 }
1034 }
1035 }
Rob Landleyadde7982006-05-16 15:32:30 +00001036 /* Removable Media Status Notification feature set */
1037 if((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
1038 printf("\t%s supported\n", cmd_feat_str[27]);
Glenn L McGrath07085852003-10-09 07:28:22 +00001039
1040
Eric Andersen3443bd72003-07-22 07:30:36 +00001041 /* security */
Rob Landleya3e4f382006-04-29 16:06:31 +00001042 if ((eqpt != CDROM) && (like_std > 3) &&
Glenn L McGrath07085852003-10-09 07:28:22 +00001043 (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
1044 {
Rob Landleyade7f952006-05-25 18:53:06 +00001045 printf("Security:\n");
Rob Landleyadde7982006-05-16 15:32:30 +00001046 if (val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
1047 printf("\tMaster password revision code = %u\n",val[PSWD_CODE]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001048 jj = val[SECU_STATUS];
Rob Landleya3e4f382006-04-29 16:06:31 +00001049 if (jj)
Glenn L McGrath07085852003-10-09 07:28:22 +00001050 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001051 for (ii = 0; ii < NUM_SECU_STR; ii++)
Glenn L McGrath07085852003-10-09 07:28:22 +00001052 {
Rob Landleyadde7982006-05-16 15:32:30 +00001053 printf("\t%s\t%s\n", (!(jj & 0x0001)) ? "not" : "", secu_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +00001054 jj >>=1;
1055 }
Rob Landleya3e4f382006-04-29 16:06:31 +00001056 if (val[SECU_STATUS] & SECU_ENABLED)
Glenn L McGrath07085852003-10-09 07:28:22 +00001057 {
Rob Landleyadde7982006-05-16 15:32:30 +00001058 printf("\tSecurity level %s\n", (val[SECU_STATUS] & SECU_LEVEL) ? "maximum" : "high");
Eric Andersen3443bd72003-07-22 07:30:36 +00001059 }
1060 }
1061 jj = val[ERASE_TIME] & ERASE_BITS;
1062 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
Rob Landleya3e4f382006-04-29 16:06:31 +00001063 if (jj || kk)
Glenn L McGrath07085852003-10-09 07:28:22 +00001064 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001065 printf("\t");
Rob Landleyade7f952006-05-25 18:53:06 +00001066 if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, "");
1067 if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001068 printf("\n");
1069 }
1070 }
1071
1072 /* reset result */
Rob Landleyadde7982006-05-16 15:32:30 +00001073 jj = val[HWRST_RSLT];
1074 if ((jj & VALID) == VALID_VAL)
Glenn L McGrath07085852003-10-09 07:28:22 +00001075 {
Rob Landleyadde7982006-05-16 15:32:30 +00001076 if (!(oo = (jj & RST0)))
1077 jj >>= 8;
Rob Landleya3e4f382006-04-29 16:06:31 +00001078 if ((jj & DEV_DET) == JUMPER_VAL)
Rob Landleyadde7982006-05-16 15:32:30 +00001079 strng = " determined by the jumper";
Rob Landleya3e4f382006-04-29 16:06:31 +00001080 else if ((jj & DEV_DET) == CSEL_VAL)
Rob Landleyadde7982006-05-16 15:32:30 +00001081 strng = " determined by CSEL";
1082 else
1083 strng = "";
1084 printf("HW reset results:\n\tCBLID- %s Vih\n\tDevice num = %i%s\n",
1085 (val[HWRST_RSLT] & CBLID) ? "above" : "below", !(oo), strng);
Eric Andersen3443bd72003-07-22 07:30:36 +00001086 }
1087
1088 /* more stuff from std 5 */
Rob Landleya3e4f382006-04-29 16:06:31 +00001089 if ((like_std > 4) && (eqpt != CDROM))
Glenn L McGrath07085852003-10-09 07:28:22 +00001090 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001091 if (val[CFA_PWR_MODE] & VALID_W160)
Glenn L McGrath07085852003-10-09 07:28:22 +00001092 {
Rob Landleyadde7982006-05-16 15:32:30 +00001093 printf("CFA power mode 1:\n\t%s%s\n", (val[CFA_PWR_MODE] & PWR_MODE_OFF) ? "disabled" : "enabled",
1094 (val[CFA_PWR_MODE] & PWR_MODE_REQ) ? " and required by some commands" : "");
Glenn L McGrath07085852003-10-09 07:28:22 +00001095
Rob Landleyadde7982006-05-16 15:32:30 +00001096 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 +00001097 }
Rob Landleya3e4f382006-04-29 16:06:31 +00001098 if ((val[INTEGRITY] & SIG) == SIG_VAL)
Glenn L McGrath07085852003-10-09 07:28:22 +00001099 {
Rob Landleyadde7982006-05-16 15:32:30 +00001100 printf("Checksum: %scorrect\n", chksum ? "in" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +00001101 }
1102 }
1103
Rob Landleyadde7982006-05-16 15:32:30 +00001104 exit(EXIT_SUCCESS);
Eric Andersen3443bd72003-07-22 07:30:36 +00001105}
1106#endif
1107
Rob Landleyade7f952006-05-25 18:53:06 +00001108static int get_identity, get_geom;
1109static int do_flush;
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001110static int do_ctimings, do_timings;
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001111static unsigned long set_readahead, get_readahead, Xreadahead;
1112static unsigned long set_readonly, get_readonly, readonly;
1113static unsigned long set_unmask, get_unmask, unmask;
1114static unsigned long set_mult, get_mult, mult;
Eric Andersenf1c56a92003-08-08 22:13:41 +00001115#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001116static unsigned long set_dma, get_dma, dma;
Eric Andersenf1c56a92003-08-08 22:13:41 +00001117#endif
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001118static unsigned long set_dma_q, get_dma_q, dma_q;
1119static unsigned long set_nowerr, get_nowerr, nowerr;
1120static unsigned long set_keep, get_keep, keep;
1121static unsigned long set_io32bit, get_io32bit, io32bit;
1122static unsigned long set_piomode, noisy_piomode;
1123static int piomode;
Eric Andersen3443bd72003-07-22 07:30:36 +00001124#ifdef HDIO_DRIVE_CMD
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001125static unsigned long set_dkeep, get_dkeep, dkeep;
1126static unsigned long set_standby, get_standby, standby_requested;
1127static unsigned long set_xfermode, get_xfermode;
1128static int xfermode_requested;
1129static unsigned long set_lookahead, get_lookahead, lookahead;
1130static unsigned long set_prefetch, get_prefetch, prefetch;
1131static unsigned long set_defects, get_defects, defects;
1132static unsigned long set_wcache, get_wcache, wcache;
1133static unsigned long set_doorlock, get_doorlock, doorlock;
1134static unsigned long set_seagate, get_seagate;
1135static unsigned long set_standbynow, get_standbynow;
1136static unsigned long set_sleepnow, get_sleepnow;
1137static unsigned long get_powermode;
1138static unsigned long set_apmmode, get_apmmode, apmmode;
Eric Andersen3443bd72003-07-22 07:30:36 +00001139#endif
1140#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001141static int get_IDentity;
Eric Andersen3443bd72003-07-22 07:30:36 +00001142#endif
1143#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
Rob Landley20deab02006-05-07 23:34:15 +00001144static unsigned long unregister_hwif;
1145static unsigned long hwif;
Eric Andersen3443bd72003-07-22 07:30:36 +00001146#endif
1147#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
Rob Landley20deab02006-05-07 23:34:15 +00001148static unsigned long scan_hwif;
1149static unsigned long hwif_data;
1150static unsigned long hwif_ctrl;
1151static unsigned long hwif_irq;
Eric Andersen3443bd72003-07-22 07:30:36 +00001152#endif
1153#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Rob Landley20deab02006-05-07 23:34:15 +00001154static unsigned long set_busstate, get_busstate, busstate;
Eric Andersen3443bd72003-07-22 07:30:36 +00001155#endif
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001156static int reread_partn;
Eric Andersen3443bd72003-07-22 07:30:36 +00001157
1158#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001159static int perform_reset;
Eric Andersen3443bd72003-07-22 07:30:36 +00001160#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1161#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Rob Landley20deab02006-05-07 23:34:15 +00001162static unsigned long perform_tristate, tristate;
Eric Andersen3443bd72003-07-22 07:30:36 +00001163#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1164
1165// Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1166// then the HDIO_GET_IDENTITY only returned 142 bytes.
1167// Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1168// and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1169// 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1170// (which they should, but they should just return -EINVAL).
1171//
1172// So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1173// On a really old system, it will not, and we will be confused.
1174// Too bad, really.
1175
1176#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001177static const char * const cfg_str[] =
Rob Landleyadde7982006-05-16 15:32:30 +00001178{ "", "HardSect", "SoftSect", "NotMFM",
1179 "HdSw>15uSec", "SpinMotCtl", "Fixed", "Removeable",
1180 "DTR<=5Mbs", "DTR>5Mbs", "DTR>10Mbs", "RotSpdTol>.5%",
1181 "dStbOff", "TrkOff", "FmtGapReq", "nonMagnetic"
Eric Andersen3443bd72003-07-22 07:30:36 +00001182};
1183
Rob Landleyadde7982006-05-16 15:32:30 +00001184static const char * const BuffType[] = {"Unknown", "1Sect", "DualPort", "DualPortCache"};
Eric Andersen3443bd72003-07-22 07:30:36 +00001185
Rob Landleya3e4f382006-04-29 16:06:31 +00001186static void dump_identity(const struct hd_driveid *id)
Eric Andersen3443bd72003-07-22 07:30:36 +00001187{
1188 int i;
Eric Andersen3443bd72003-07-22 07:30:36 +00001189 const unsigned short int *id_regs= (const void*) id;
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001190
Glenn L McGrath07085852003-10-09 07:28:22 +00001191 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1192 id->model, id->fw_rev, id->serial_no);
Rob Landleyadde7982006-05-16 15:32:30 +00001193 for (i=0; i<=15; i++) {
Rob Landleyade7f952006-05-25 18:53:06 +00001194 if (id->config & (1<<i))
1195 printf(" %s", cfg_str[i]);
Rob Landleyadde7982006-05-16 15:32:30 +00001196 }
1197 printf( " }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n"
1198 " BuffType=(%u) %s, BuffSize=%ukB, MaxMultSect=%u",
Glenn L McGrath07085852003-10-09 07:28:22 +00001199 id->cyls, id->heads, id->sectors, id->track_bytes,
Rob Landleyadde7982006-05-16 15:32:30 +00001200 id->sector_bytes, id->ecc_bytes,
1201 id->buf_type, BuffType[(id->buf_type > 3) ? 0 : id->buf_type],
1202 id->buf_size/2, id->max_multsect);
Glenn L McGrath07085852003-10-09 07:28:22 +00001203 if (id->max_multsect)
1204 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001205 printf(", MultSect=");
1206 if (!(id->multsect_valid&1))
1207 printf("?%u?", id->multsect);
1208 else if (id->multsect)
1209 printf("%u", id->multsect);
1210 else
1211 printf("off");
1212 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001213 printf("\n");
Rob Landleyade7f952006-05-25 18:53:06 +00001214
1215 if(!(id->field_valid&1))
1216 printf(" (maybe):");
1217
1218 printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
1219 id->cur_sectors,
1220 (BB_BIG_ENDIAN) ?
1221 (long unsigned int)(id->cur_capacity0 << 16) | id->cur_capacity1 :
1222 (long unsigned int)(id->cur_capacity1 << 16) | id->cur_capacity0,
1223 ((id->capability&2) == 0) ? "no" : "yes");
1224
1225 if (id->capability&2)
1226 printf(", LBAsects=%u", id->lba_capacity);
1227
1228 printf("\n IORDY=%s", (id->capability&8) ? (id->capability&4) ? "on/off" : "yes" : "no");
1229
1230 if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&2)
1231 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
1232
1233 if ((id->capability&1) && (id->field_valid&2))
1234 printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1235
1236 printf("\n PIO modes: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001237 if (id->tPIO <= 5)
1238 {
Rob Landleyade7f952006-05-25 18:53:06 +00001239 printf("pio0 ");
1240 if (id->tPIO >= 1) printf("pio1 ");
1241 if (id->tPIO >= 2) printf("pio2 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001242 }
Rob Landleyade7f952006-05-25 18:53:06 +00001243 if (id->field_valid&2)
1244 {
1245 if (id->eide_pio_modes & 1) printf("pio3 ");
1246 if (id->eide_pio_modes & 2) printf("pio4 ");
1247 if (id->eide_pio_modes &~3) printf("pio? ");
1248 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001249 if (id->capability&1)
1250 {
1251 if (id->dma_1word | id->dma_mword)
1252 {
Rob Landleyade7f952006-05-25 18:53:06 +00001253 printf("\n DMA modes: ");
1254 if (id->dma_1word & 0x100) printf("*");
1255 if (id->dma_1word & 1) printf("sdma0 ");
1256 if (id->dma_1word & 0x200) printf("*");
1257 if (id->dma_1word & 2) printf("sdma1 ");
1258 if (id->dma_1word & 0x400) printf("*");
1259 if (id->dma_1word & 4) printf("sdma2 ");
1260 if (id->dma_1word & 0xf800) printf("*");
1261 if (id->dma_1word & 0xf8) printf("sdma? ");
1262 if (id->dma_mword & 0x100) printf("*");
1263 if (id->dma_mword & 1) printf("mdma0 ");
1264 if (id->dma_mword & 0x200) printf("*");
1265 if (id->dma_mword & 2) printf("mdma1 ");
1266 if (id->dma_mword & 0x400) printf("*");
1267 if (id->dma_mword & 4) printf("mdma2 ");
1268 if (id->dma_mword & 0xf800) printf("*");
1269 if (id->dma_mword & 0xf8) printf("mdma? ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001270 }
1271 }
Rob Landleyade7f952006-05-25 18:53:06 +00001272 if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&4)
1273 {
1274 printf("\n UDMA modes: ");
1275 if (id->dma_ultra & 0x100) printf("*");
1276 if (id->dma_ultra & 0x001) printf("udma0 ");
1277 if (id->dma_ultra & 0x200) printf("*");
1278 if (id->dma_ultra & 0x002) printf("udma1 ");
1279 if (id->dma_ultra & 0x400) printf("*");
1280 if (id->dma_ultra & 0x004) printf("udma2 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001281#ifdef __NEW_HD_DRIVE_ID
Rob Landleyade7f952006-05-25 18:53:06 +00001282 if (id->hw_config & 0x2000)
1283 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001284#else /* !__NEW_HD_DRIVE_ID */
Rob Landleyade7f952006-05-25 18:53:06 +00001285 if (id->word93 & 0x2000)
1286 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001287#endif /* __NEW_HD_DRIVE_ID */
Rob Landleyade7f952006-05-25 18:53:06 +00001288 if (id->dma_ultra & 0x0800) printf("*");
1289 if (id->dma_ultra & 0x0008) printf("udma3 ");
1290 if (id->dma_ultra & 0x1000) printf("*");
1291 if (id->dma_ultra & 0x0010) printf("udma4 ");
1292 if (id->dma_ultra & 0x2000) printf("*");
1293 if (id->dma_ultra & 0x0020) printf("udma5 ");
1294 if (id->dma_ultra & 0x4000) printf("*");
1295 if (id->dma_ultra & 0x0040) printf("udma6 ");
1296 if (id->dma_ultra & 0x8000) printf("*");
1297 if (id->dma_ultra & 0x0080) printf("udma7 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001298 }
1299 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001300 printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
1301 if (id_regs[83] & 8)
1302 {
1303 if (!(id_regs[86]&8))
1304 printf(": disabled (255)");
1305 else if ((id_regs[91]&0xFF00)!=0x4000)
1306 printf(": unknown setting");
1307 else
1308 printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
1309 }
Rob Landleyade7f952006-05-25 18:53:06 +00001310 if (id_regs[82]&0x20)
1311 printf(" WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
Glenn L McGrath07085852003-10-09 07:28:22 +00001312#ifdef __NEW_HD_DRIVE_ID
1313 if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
1314 {
Rob Landleyadde7982006-05-16 15:32:30 +00001315 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 +00001316 if (id->major_rev_num != 0x0000 && /* NOVAL_0 */
1317 id->major_rev_num != 0xFFFF) { /* NOVAL_1 */
Rob Landleyadde7982006-05-16 15:32:30 +00001318 for (i=0; i <= 15; i++) {
1319 if (id->major_rev_num & (1<<i))
1320 printf(" ATA/ATAPI-%u", i);
1321 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001322 }
1323 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001324#endif /* __NEW_HD_DRIVE_ID */
Rob Landleyade7f952006-05-25 18:53:06 +00001325 printf("\n\n * current active mode\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001326}
1327#endif
1328
Rob Landleya3e4f382006-04-29 16:06:31 +00001329static void flush_buffer_cache(int fd)
Eric Andersen3443bd72003-07-22 07:30:36 +00001330{
Rob Landleya3e4f382006-04-29 16:06:31 +00001331 fsync(fd); /* flush buffers */
Glenn L McGrath07085852003-10-09 07:28:22 +00001332 bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
Eric Andersen3443bd72003-07-22 07:30:36 +00001333#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001334 sleep(1);
1335 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001336 bb_perror_msg("HDIO_DRIVE_CMD");
Eric Andersen3443bd72003-07-22 07:30:36 +00001337#endif
1338}
1339
Rob Landleya3e4f382006-04-29 16:06:31 +00001340static int seek_to_zero(int fd)
Eric Andersen3443bd72003-07-22 07:30:36 +00001341{
Glenn L McGrath07085852003-10-09 07:28:22 +00001342 if (lseek(fd, (off_t) 0, SEEK_SET))
Eric Andersen3443bd72003-07-22 07:30:36 +00001343 return 1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001344 return 0;
1345}
1346
Rob Landleya3e4f382006-04-29 16:06:31 +00001347static int read_big_block(int fd, char *buf)
Eric Andersen3443bd72003-07-22 07:30:36 +00001348{
Rob Landleyadde7982006-05-16 15:32:30 +00001349 int i;
1350
1351 if ((i = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES) {
1352 bb_error_msg("read(%d bytes) failed (rc=%d)", TIMING_BUF_BYTES, i);
Eric Andersen3443bd72003-07-22 07:30:36 +00001353 return 1;
1354 }
1355 /* access all sectors of buf to ensure the read fully completed */
1356 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1357 buf[i] &= 1;
1358 return 0;
1359}
1360
Rob Landleyadde7982006-05-16 15:32:30 +00001361static void print_timing(int t, double e)
1362{
1363 if (t >= e) /* more than 1MB/s */
Rob Landleyade7f952006-05-25 18:53:06 +00001364 printf("%2d MB in %5.2f seconds =%6.2f %cB/sec\n", t, e, t / e, 'M');
Rob Landleyadde7982006-05-16 15:32:30 +00001365 else
Rob Landleyade7f952006-05-25 18:53:06 +00001366 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 +00001367}
1368
1369static int do_blkgetsize (int fd, unsigned long long *blksize64)
1370{
1371 int rc;
1372 unsigned int blksize32 = 0;
1373
1374 if (0 == ioctl(fd, BLKGETSIZE64, blksize64)) { // returns bytes
1375 *blksize64 /= 512;
1376 return 0;
1377 }
1378 rc = ioctl(fd, BLKGETSIZE, &blksize32); // returns sectors
1379 if (rc)
1380 bb_perror_msg("BLKGETSIZE");
1381 *blksize64 = blksize32;
1382 return rc;
1383}
Eric Andersen50af12d2003-08-06 08:47:59 +00001384
Rob Landleya3e4f382006-04-29 16:06:31 +00001385static void do_time(int flag, int fd)
Glenn L McGrath07085852003-10-09 07:28:22 +00001386/*
1387 flag = 0 time_cache
1388 flag = 1 time_device
1389*/
Eric Andersen3443bd72003-07-22 07:30:36 +00001390{
Eric Andersen3443bd72003-07-22 07:30:36 +00001391 struct itimerval e1, e2;
Rob Landleyadde7982006-05-16 15:32:30 +00001392 double elapsed, elapsed2;
1393 unsigned int max_iterations = 1024, total_MB, iterations;
1394 unsigned long long blksize;
Rob Landley4ae2f512006-05-19 17:24:26 +00001395 RESERVE_CONFIG_BUFFER(buf, TIMING_BUF_BYTES);
1396
1397 if (mlock(buf, TIMING_BUF_BYTES)) {
1398 bb_perror_msg("mlock");
1399 goto quit2;
1400 }
Rob Landleyadde7982006-05-16 15:32:30 +00001401
1402 if (0 == do_blkgetsize(fd, &blksize)) {
1403 max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB;
1404 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001405
Eric Andersen3443bd72003-07-22 07:30:36 +00001406 /* Clear out the device request queues & give them time to complete */
Rob Landleyadde7982006-05-16 15:32:30 +00001407 sync();
1408 sleep(3);
1409
1410 setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00001411
Rob Landleya3e4f382006-04-29 16:06:31 +00001412 if (flag == 0) /* Time cache */
Glenn L McGrath07085852003-10-09 07:28:22 +00001413 {
Rob Landleyadde7982006-05-16 15:32:30 +00001414 if (seek_to_zero (fd)) return;
1415 if (read_big_block (fd, buf)) return;
1416 printf(" Timing cached reads: ");
Glenn L McGrath07085852003-10-09 07:28:22 +00001417 fflush(stdout);
Rob Landleyadde7982006-05-16 15:32:30 +00001418
1419 /* Now do the timing */
1420 iterations = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +00001421 getitimer(ITIMER_REAL, &e1);
Rob Landleyadde7982006-05-16 15:32:30 +00001422 do {
1423 ++iterations;
1424 if (seek_to_zero (fd) || read_big_block (fd, buf))
Glenn L McGrath07085852003-10-09 07:28:22 +00001425 goto quit;
Rob Landleyadde7982006-05-16 15:32:30 +00001426 getitimer(ITIMER_REAL, &e2);
1427 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1428 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1429 } while (elapsed < 2.0);
1430 total_MB = iterations * TIMING_BUF_MB;
Rob Landleyade7f952006-05-25 18:53:06 +00001431
Rob Landleyadde7982006-05-16 15:32:30 +00001432 /* Now remove the lseek() and getitimer() overheads from the elapsed time */
1433 getitimer(ITIMER_REAL, &e1);
1434 do {
1435 if (seek_to_zero (fd))
1436 goto quit;
1437 getitimer(ITIMER_REAL, &e2);
1438 elapsed2 = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1439 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1440 } while (--iterations);
1441
1442 elapsed -= elapsed2;
1443 print_timing(BUFCACHE_FACTOR * total_MB, elapsed);
Glenn L McGrath07085852003-10-09 07:28:22 +00001444 flush_buffer_cache(fd);
1445 sleep(1);
Eric Andersen50af12d2003-08-06 08:47:59 +00001446 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001447 else /* Time device */
1448 {
1449 printf(" Timing buffered disk reads: ");
1450 fflush(stdout);
Glenn L McGrath07085852003-10-09 07:28:22 +00001451 /*
1452 * getitimer() is used rather than gettimeofday() because
1453 * it is much more consistent (on my machine, at least).
1454 */
Glenn L McGrath07085852003-10-09 07:28:22 +00001455 /* Now do the timings for real */
Rob Landleyadde7982006-05-16 15:32:30 +00001456 iterations = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +00001457 getitimer(ITIMER_REAL, &e1);
Rob Landleyadde7982006-05-16 15:32:30 +00001458 do {
1459 ++iterations;
1460 if (read_big_block (fd, buf))
Glenn L McGrath07085852003-10-09 07:28:22 +00001461 goto quit;
Rob Landleyadde7982006-05-16 15:32:30 +00001462 getitimer(ITIMER_REAL, &e2);
1463 elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec)
1464 + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
1465 } while (elapsed < 3.0 && iterations < max_iterations);
1466
1467 total_MB = iterations * TIMING_BUF_MB;
1468 print_timing(total_MB, elapsed);
Glenn L McGrath07085852003-10-09 07:28:22 +00001469 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001470quit:
Rob Landley4ae2f512006-05-19 17:24:26 +00001471 munlock(buf, TIMING_BUF_BYTES);
1472quit2:
1473 RELEASE_CONFIG_BUFFER(buf);
Eric Andersen3443bd72003-07-22 07:30:36 +00001474}
1475
Eric Andersen3443bd72003-07-22 07:30:36 +00001476static void on_off (unsigned int value)
1477{
1478 printf(value ? " (on)\n" : " (off)\n");
1479}
1480
1481#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Rob Landleya3e4f382006-04-29 16:06:31 +00001482static void bus_state_value(unsigned int value)
Eric Andersen3443bd72003-07-22 07:30:36 +00001483{
Rob Landleyadde7982006-05-16 15:32:30 +00001484 if (value == BUSSTATE_ON)
1485 on_off(1);
1486 else if (value == BUSSTATE_OFF)
1487 on_off(0);
1488 else if (value == BUSSTATE_TRISTATE)
1489 printf(" (tristate)\n");
1490 else
1491 printf(" (unknown: %d)\n", value);
Eric Andersen3443bd72003-07-22 07:30:36 +00001492}
1493#endif
1494
1495#ifdef HDIO_DRIVE_CMD
Rob Landleya3e4f382006-04-29 16:06:31 +00001496static void interpret_standby(unsigned int standby)
Eric Andersen3443bd72003-07-22 07:30:36 +00001497{
Rob Landleyadde7982006-05-16 15:32:30 +00001498 unsigned int t;
1499
1500
1501 if (standby == 0)
1502 printf("off");
1503 else if (standby == 252)
1504 printf("21 minutes");
1505 else if (standby == 253)
1506 printf("vendor-specific");
1507 else if (standby == 254)
1508 printf("Reserved");
1509 else if (standby == 255)
1510 printf("21 minutes + 15 seconds");
1511 else {
1512 if (standby <= 240) {
1513 t = standby * 5;
1514 printf("%u minutes + %u seconds", t / 60, t % 60);
1515 } else if (standby <= 251) {
1516 t = (standby - 240) * 30;
1517 printf("%u hours + %u minutes", t / 60, t % 60);
1518 } else
1519 printf("illegal value");
Eric Andersen3443bd72003-07-22 07:30:36 +00001520 }
1521 printf(")\n");
1522}
1523
1524struct xfermode_entry {
1525 int val;
1526 const char *name;
1527};
1528
1529static const struct xfermode_entry xfermode_table[] = {
1530 { 8, "pio0" },
1531 { 9, "pio1" },
1532 { 10, "pio2" },
1533 { 11, "pio3" },
1534 { 12, "pio4" },
1535 { 13, "pio5" },
1536 { 14, "pio6" },
1537 { 15, "pio7" },
1538 { 16, "sdma0" },
1539 { 17, "sdma1" },
1540 { 18, "sdma2" },
1541 { 19, "sdma3" },
1542 { 20, "sdma4" },
1543 { 21, "sdma5" },
1544 { 22, "sdma6" },
1545 { 23, "sdma7" },
1546 { 32, "mdma0" },
1547 { 33, "mdma1" },
1548 { 34, "mdma2" },
1549 { 35, "mdma3" },
1550 { 36, "mdma4" },
1551 { 37, "mdma5" },
1552 { 38, "mdma6" },
1553 { 39, "mdma7" },
1554 { 64, "udma0" },
1555 { 65, "udma1" },
1556 { 66, "udma2" },
1557 { 67, "udma3" },
1558 { 68, "udma4" },
1559 { 69, "udma5" },
1560 { 70, "udma6" },
1561 { 71, "udma7" },
1562 { 0, NULL }
1563};
1564
1565static int translate_xfermode(char * name)
1566{
Glenn L McGrath07085852003-10-09 07:28:22 +00001567 const struct xfermode_entry *tmp;
1568 char *endptr;
1569 int val = -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001570
1571
Glenn L McGrath07085852003-10-09 07:28:22 +00001572 for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
1573 {
1574 if (!strcmp(name, tmp->name))
1575 return tmp->val;
Glenn L McGrath07085852003-10-09 07:28:22 +00001576 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001577
Glenn L McGrath07085852003-10-09 07:28:22 +00001578 val = strtol(name, &endptr, 10);
1579 if (*endptr == '\0')
1580 return val;
1581
1582 return -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001583}
1584
Rob Landleya3e4f382006-04-29 16:06:31 +00001585static void interpret_xfermode(unsigned int xfermode)
Eric Andersen3443bd72003-07-22 07:30:36 +00001586{
1587 printf(" (");
Rob Landleyadde7982006-05-16 15:32:30 +00001588 if (xfermode == 0)
1589 printf("default PIO mode");
1590 else if (xfermode == 1)
1591 printf("default PIO mode, disable IORDY");
1592 else if (xfermode >= 8 && xfermode <= 15)
1593 printf("PIO flow control mode%u", xfermode-8);
1594 else if (xfermode >= 16 && xfermode <= 23)
1595 printf("singleword DMA mode%u", xfermode-16);
1596 else if (xfermode >= 32 && xfermode <= 39)
1597 printf("multiword DMA mode%u", xfermode-32);
1598 else if (xfermode >= 64 && xfermode <= 71)
1599 printf("UltraDMA mode%u", xfermode-64);
1600 else
1601 printf("Unknown");
Eric Andersen3443bd72003-07-22 07:30:36 +00001602 printf(")\n");
1603}
1604#endif /* HDIO_DRIVE_CMD */
1605
Rob Landleyadde7982006-05-16 15:32:30 +00001606static void print_flag(unsigned long flag, char *s, unsigned long value)
1607{
1608 if(flag)
1609 printf(" setting %s to %ld\n", s, value);
1610}
1611
Rob Landleya3e4f382006-04-29 16:06:31 +00001612static void process_dev(char *devname)
Eric Andersen3443bd72003-07-22 07:30:36 +00001613{
1614 int fd;
1615 static long parm, multcount;
Eric Andersen3443bd72003-07-22 07:30:36 +00001616#ifndef HDIO_DRIVE_CMD
1617 int force_operation = 0;
1618#endif
Rob Landley39cf6452006-05-05 16:52:28 +00001619 /* Please restore args[n] to these values after each ioctl
1620 except for args[2] */
Rob Landleye5b281f2006-04-29 15:49:18 +00001621 unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
Rob Landleyadde7982006-05-16 15:32:30 +00001622 const char *fmt = " %s\t= %2ld";
Rob Landleye5b281f2006-04-29 15:49:18 +00001623
Rob Landleya3e4f382006-04-29 16:06:31 +00001624 fd = bb_xopen(devname, O_RDONLY|O_NONBLOCK);
Rob Landleyade7f952006-05-25 18:53:06 +00001625 printf("\n%s:\n", devname);
Eric Andersen3443bd72003-07-22 07:30:36 +00001626
Glenn L McGrath07085852003-10-09 07:28:22 +00001627 if (set_readahead)
1628 {
Rob Landleyadde7982006-05-16 15:32:30 +00001629 print_flag(get_readahead,"fs readahead", Xreadahead);
Eric Andersenf828da02004-07-20 22:53:59 +00001630 bb_ioctl(fd, BLKRASET,(int *)Xreadahead,"BLKRASET");
Eric Andersen3443bd72003-07-22 07:30:36 +00001631 }
1632#ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001633 if (unregister_hwif)
1634 {
Rob Landley20deab02006-05-07 23:34:15 +00001635 printf(" attempting to unregister hwif#%lu\n", hwif);
Eric Andersena68ea1c2006-01-30 22:48:39 +00001636 bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)(unsigned long)hwif,"HDIO_UNREGISTER_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00001637 }
1638#endif
1639#ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001640 if (scan_hwif)
1641 {
Rob Landley20deab02006-05-07 23:34:15 +00001642 printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq);
Eric Andersen3443bd72003-07-22 07:30:36 +00001643 args[0] = hwif_data;
1644 args[1] = hwif_ctrl;
1645 args[2] = hwif_irq;
Glenn L McGrath07085852003-10-09 07:28:22 +00001646 bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
Rob Landley39cf6452006-05-05 16:52:28 +00001647 args[0] = WIN_SETFEATURES;
1648 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001649 }
1650#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001651 if (set_piomode)
1652 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001653 if (noisy_piomode)
1654 {
1655 printf(" attempting to ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001656 if (piomode == 255)
Glenn L McGrath07085852003-10-09 07:28:22 +00001657 printf("auto-tune PIO mode\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001658 else if (piomode < 100)
Glenn L McGrath07085852003-10-09 07:28:22 +00001659 printf("set PIO mode to %d\n", piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001660 else if (piomode < 200)
Glenn L McGrath07085852003-10-09 07:28:22 +00001661 printf("set MDMA mode to %d\n", (piomode-100));
Eric Andersen3443bd72003-07-22 07:30:36 +00001662 else
Glenn L McGrath07085852003-10-09 07:28:22 +00001663 printf("set UDMA mode to %d\n", (piomode-200));
Eric Andersen3443bd72003-07-22 07:30:36 +00001664 }
Eric Andersena68ea1c2006-01-30 22:48:39 +00001665 bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode, "HDIO_SET_PIO_MODE");
Eric Andersen3443bd72003-07-22 07:30:36 +00001666 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001667 if (set_io32bit)
1668 {
Rob Landleyadde7982006-05-16 15:32:30 +00001669 print_flag(get_io32bit,"32-bit IO_support flag", io32bit);
Glenn L McGrath07085852003-10-09 07:28:22 +00001670 bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
Eric Andersen3443bd72003-07-22 07:30:36 +00001671 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001672 if (set_mult)
1673 {
Rob Landleyadde7982006-05-16 15:32:30 +00001674 print_flag(get_mult, "multcount", mult);
Rob Landleyade7f952006-05-25 18:53:06 +00001675#ifdef HDIO_DRIVE_CMD
1676 bb_ioctl(fd, HDIO_SET_MULTCOUNT, &mult, "HDIO_SET_MULTCOUNT");
1677#else
1678 force_operation |= (!bb_ioctl(fd, HDIO_SET_MULTCOUNT, &mult, "HDIO_SET_MULTCOUNT"));
Eric Andersen3443bd72003-07-22 07:30:36 +00001679#endif
1680 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001681 if (set_readonly)
1682 {
Rob Landleyadde7982006-05-16 15:32:30 +00001683 print_flag_on_off(get_readonly,"readonly", readonly);
Glenn L McGrath07085852003-10-09 07:28:22 +00001684 bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
Eric Andersen3443bd72003-07-22 07:30:36 +00001685 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001686 if (set_unmask)
1687 {
Rob Landleyadde7982006-05-16 15:32:30 +00001688 print_flag_on_off(get_unmask,"unmaskirq", unmask);
Glenn L McGrath07085852003-10-09 07:28:22 +00001689 bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
Eric Andersen3443bd72003-07-22 07:30:36 +00001690 }
Eric Andersen0a57a792003-08-06 08:57:35 +00001691#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Glenn L McGrath07085852003-10-09 07:28:22 +00001692 if (set_dma)
1693 {
Rob Landleyadde7982006-05-16 15:32:30 +00001694 print_flag_on_off(get_dma, "using_dma", dma);
Glenn L McGrath07085852003-10-09 07:28:22 +00001695 bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
Eric Andersen3443bd72003-07-22 07:30:36 +00001696 }
Eric Andersen0a57a792003-08-06 08:57:35 +00001697#endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
Glenn L McGrath07085852003-10-09 07:28:22 +00001698 if (set_dma_q)
1699 {
Rob Landleyadde7982006-05-16 15:32:30 +00001700 print_flag_on_off(get_dma_q,"DMA queue_depth", dma_q);
Glenn L McGrath07085852003-10-09 07:28:22 +00001701 bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
Eric Andersen3443bd72003-07-22 07:30:36 +00001702 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001703 if (set_nowerr)
1704 {
Rob Landleyadde7982006-05-16 15:32:30 +00001705 print_flag_on_off(get_nowerr,"nowerr", nowerr);
Glenn L McGrath07085852003-10-09 07:28:22 +00001706 bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
Eric Andersen3443bd72003-07-22 07:30:36 +00001707 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001708 if (set_keep)
1709 {
Rob Landleyadde7982006-05-16 15:32:30 +00001710 print_flag_on_off(get_keep,"keep_settings", keep);
Glenn L McGrath07085852003-10-09 07:28:22 +00001711 bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
Eric Andersen3443bd72003-07-22 07:30:36 +00001712 }
1713#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001714 if (set_doorlock)
1715 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001716 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
Rob Landley39cf6452006-05-05 16:52:28 +00001717 args[2] = 0;
Rob Landleyadde7982006-05-16 15:32:30 +00001718 print_flag_on_off(get_doorlock,"drive doorlock", doorlock);
Glenn L McGrath07085852003-10-09 07:28:22 +00001719 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
Rob Landley39cf6452006-05-05 16:52:28 +00001720 args[0] = WIN_SETFEATURES;
Eric Andersen3443bd72003-07-22 07:30:36 +00001721 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001722 if (set_dkeep)
1723 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001724 /* lock/unlock the drive's "feature" settings */
Rob Landleyadde7982006-05-16 15:32:30 +00001725 print_flag_on_off(get_dkeep,"drive keep features", dkeep);
Eric Andersen3443bd72003-07-22 07:30:36 +00001726 args[2] = dkeep ? 0x66 : 0xcc;
Glenn L McGrath07085852003-10-09 07:28:22 +00001727 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001728 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001729 if (set_defects)
1730 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001731 args[2] = defects ? 0x04 : 0x84;
Rob Landleyadde7982006-05-16 15:32:30 +00001732 print_flag(get_defects,"drive defect-mgmt", defects);
Glenn L McGrath07085852003-10-09 07:28:22 +00001733 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001734 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001735 if (set_prefetch)
1736 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001737 args[1] = prefetch;
Rob Landleye5b281f2006-04-29 15:49:18 +00001738 args[2] = 0xab;
Rob Landleyadde7982006-05-16 15:32:30 +00001739 print_flag(get_prefetch,"drive prefetch", prefetch);
Glenn L McGrath07085852003-10-09 07:28:22 +00001740 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
Rob Landley39cf6452006-05-05 16:52:28 +00001741 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001742 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001743 if (set_xfermode)
1744 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001745 args[1] = xfermode_requested;
Rob Landleye5b281f2006-04-29 15:49:18 +00001746 args[2] = 3;
Glenn L McGrath07085852003-10-09 07:28:22 +00001747 if (get_xfermode)
1748 {
Rob Landleyade7f952006-05-25 18:53:06 +00001749 print_flag(1,"xfermode", xfermode_requested);
Eric Andersen3443bd72003-07-22 07:30:36 +00001750 interpret_xfermode(xfermode_requested);
1751 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001752 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
Rob Landley39cf6452006-05-05 16:52:28 +00001753 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001754 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001755 if (set_lookahead)
1756 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001757 args[2] = lookahead ? 0xaa : 0x55;
Rob Landleyadde7982006-05-16 15:32:30 +00001758 print_flag_on_off(get_lookahead,"drive read-lookahead", lookahead);
Glenn L McGrath07085852003-10-09 07:28:22 +00001759 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001760 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001761 if (set_apmmode)
1762 {
Rob Landleyadde7982006-05-16 15:32:30 +00001763 args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */; /* feature register */
1764 args[1] = apmmode; /* sector count register 1-255 */
1765 if (get_apmmode)
1766 printf(" setting APM level to %s 0x%02lX (%ld)\n", (apmmode == 255) ? "disabled" : "", apmmode, apmmode);
Glenn L McGrath07085852003-10-09 07:28:22 +00001767 bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
Rob Landley39cf6452006-05-05 16:52:28 +00001768 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001769 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001770 if (set_wcache)
1771 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001772#ifdef DO_FLUSHCACHE
1773#ifndef WIN_FLUSHCACHE
1774#define WIN_FLUSHCACHE 0xe7
1775#endif
Rob Landleye5b281f2006-04-29 15:49:18 +00001776 static unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
Eric Andersen3443bd72003-07-22 07:30:36 +00001777#endif /* DO_FLUSHCACHE */
Eric Andersen3443bd72003-07-22 07:30:36 +00001778 args[2] = wcache ? 0x02 : 0x82;
Rob Landleyadde7982006-05-16 15:32:30 +00001779 print_flag_on_off(get_wcache,"drive write-caching", wcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00001780#ifdef DO_FLUSHCACHE
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001781 if (!wcache)
1782 bb_ioctl(fd, HDIO_DRIVE_CMD, &flushcache, "HDIO_DRIVE_CMD(flushcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001783#endif /* DO_FLUSHCACHE */
Glenn L McGrath07085852003-10-09 07:28:22 +00001784 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001785#ifdef DO_FLUSHCACHE
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001786 if (!wcache)
1787 bb_ioctl(fd, HDIO_DRIVE_CMD, &flushcache, "HDIO_DRIVE_CMD(flushcache)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001788#endif /* DO_FLUSHCACHE */
1789 }
Rob Landley39cf6452006-05-05 16:52:28 +00001790
1791 /* In code below, we do not preserve args[0], but the rest
1792 is preserved, including args[2] */
1793 args[2] = 0;
1794
Glenn L McGrath07085852003-10-09 07:28:22 +00001795 if (set_standbynow)
1796 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001797#ifndef WIN_STANDBYNOW1
1798#define WIN_STANDBYNOW1 0xE0
1799#endif
1800#ifndef WIN_STANDBYNOW2
1801#define WIN_STANDBYNOW2 0x94
1802#endif
Rob Landleyadde7982006-05-16 15:32:30 +00001803 if (get_standbynow) printf(" issuing standby command\n");
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001804 args[0] = WIN_STANDBYNOW1;
1805 bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2, "HDIO_DRIVE_CMD(standby)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001806 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001807 if (set_sleepnow)
1808 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001809#ifndef WIN_SLEEPNOW1
1810#define WIN_SLEEPNOW1 0xE6
1811#endif
1812#ifndef WIN_SLEEPNOW2
1813#define WIN_SLEEPNOW2 0x99
1814#endif
Rob Landleyadde7982006-05-16 15:32:30 +00001815 if (get_sleepnow) printf(" issuing sleep command\n");
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001816 args[0] = WIN_SLEEPNOW1;
1817 bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2, "HDIO_DRIVE_CMD(sleep)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001818 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001819 if (set_seagate)
1820 {
Rob Landleye5b281f2006-04-29 15:49:18 +00001821 args[0] = 0xfb;
Rob Landleyadde7982006-05-16 15:32:30 +00001822 if (get_seagate) printf(" disabling Seagate auto powersaving mode\n");
Glenn L McGrath07085852003-10-09 07:28:22 +00001823 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
Eric Andersen3443bd72003-07-22 07:30:36 +00001824 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001825 if (set_standby)
1826 {
Rob Landleye5b281f2006-04-29 15:49:18 +00001827 args[0] = WIN_SETIDLE1;
1828 args[1] = standby_requested;
Glenn L McGrath07085852003-10-09 07:28:22 +00001829 if (get_standby)
1830 {
Rob Landleyade7f952006-05-25 18:53:06 +00001831 print_flag(1,"standby", standby_requested);
Eric Andersen3443bd72003-07-22 07:30:36 +00001832 interpret_standby(standby_requested);
1833 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001834 bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
Rob Landley39cf6452006-05-05 16:52:28 +00001835 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001836 }
1837#else /* HDIO_DRIVE_CMD */
Glenn L McGrath07085852003-10-09 07:28:22 +00001838 if (force_operation)
1839 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001840 char buf[512];
1841 flush_buffer_cache(fd);
1842 if (-1 == read(fd, buf, sizeof(buf)))
Rob Landleyadde7982006-05-16 15:32:30 +00001843 bb_perror_msg("read(%d bytes) failed (rc=%d)", sizeof(buf), -1);
Eric Andersen3443bd72003-07-22 07:30:36 +00001844 }
1845#endif /* HDIO_DRIVE_CMD */
1846
Rob Landleyade7f952006-05-25 18:53:06 +00001847 if (get_mult || get_identity)
Glenn L McGrath07085852003-10-09 07:28:22 +00001848 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001849 multcount = -1;
Glenn L McGrath07085852003-10-09 07:28:22 +00001850 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
1851 {
Rob Landleyade7f952006-05-25 18:53:06 +00001852 if (get_mult)
Eric Andersen06d4ec22004-03-19 10:53:52 +00001853 bb_perror_msg("HDIO_GET_MULTCOUNT");
Glenn L McGrath07085852003-10-09 07:28:22 +00001854 }
Rob Landleyade7f952006-05-25 18:53:06 +00001855 else if (get_mult)
Glenn L McGrath07085852003-10-09 07:28:22 +00001856 {
Rob Landleyadde7982006-05-16 15:32:30 +00001857 printf(fmt, "multcount", multcount);
Eric Andersen3443bd72003-07-22 07:30:36 +00001858 on_off(multcount);
1859 }
1860 }
Rob Landleyade7f952006-05-25 18:53:06 +00001861 if (get_io32bit)
Glenn L McGrath07085852003-10-09 07:28:22 +00001862 {
Rob Landleya3e4f382006-04-29 16:06:31 +00001863 if (!bb_ioctl(fd, HDIO_GET_32BIT, &parm, "HDIO_GET_32BIT"))
Glenn L McGrath07085852003-10-09 07:28:22 +00001864 {
Rob Landleyadde7982006-05-16 15:32:30 +00001865 printf(" IO_support\t=%3ld (", parm);
1866 if (parm == 0)
1867 printf("default 16-bit)\n");
1868 else if (parm == 2)
1869 printf("16-bit)\n");
1870 else if (parm == 1)
1871 printf("32-bit)\n");
1872 else if (parm == 3)
1873 printf("32-bit w/sync)\n");
1874 else if (parm == 8)
1875 printf("Request-Queue-Bypass)\n");
1876 else
1877 printf("\?\?\?)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001878 }
1879 }
Rob Landleyade7f952006-05-25 18:53:06 +00001880 if (get_unmask)
Glenn L McGrath07085852003-10-09 07:28:22 +00001881 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001882 bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001883 "HDIO_GET_UNMASKINTR","unmaskirq");
Eric Andersen3443bd72003-07-22 07:30:36 +00001884 }
1885
Glenn L McGrath07085852003-10-09 07:28:22 +00001886
Eric Andersen0a57a792003-08-06 08:57:35 +00001887#ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
Rob Landleyade7f952006-05-25 18:53:06 +00001888 if (get_dma) {
Rob Landleya3e4f382006-04-29 16:06:31 +00001889 if (!bb_ioctl(fd, HDIO_GET_DMA, &parm, "HDIO_GET_DMA"))
Glenn L McGrath07085852003-10-09 07:28:22 +00001890 {
Rob Landleyadde7982006-05-16 15:32:30 +00001891 printf(fmt, "using_dma", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001892 if (parm == 8)
1893 printf(" (DMA-Assisted-PIO)\n");
1894 else
1895 on_off(parm);
1896 }
1897 }
1898#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001899 if (get_dma_q)
1900 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001901 bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001902 "HDIO_GET_QDMA","queue_depth");
Eric Andersen3443bd72003-07-22 07:30:36 +00001903 }
Rob Landleyade7f952006-05-25 18:53:06 +00001904 if (get_keep)
Glenn L McGrath07085852003-10-09 07:28:22 +00001905 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001906 bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001907 "HDIO_GET_KEEPSETTINGS","keepsettings");
Eric Andersen3443bd72003-07-22 07:30:36 +00001908 }
1909
Glenn L McGrath07085852003-10-09 07:28:22 +00001910 if (get_nowerr)
1911 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001912 bb_ioctl_on_off (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001913 "HDIO_GET_NOWERR","nowerr");
Eric Andersen3443bd72003-07-22 07:30:36 +00001914 }
Rob Landleyade7f952006-05-25 18:53:06 +00001915 if (get_readonly)
Glenn L McGrath07085852003-10-09 07:28:22 +00001916 {
1917 bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001918 "BLKROGET","readonly");
Eric Andersen3443bd72003-07-22 07:30:36 +00001919 }
Rob Landleyade7f952006-05-25 18:53:06 +00001920 if (get_readahead)
Glenn L McGrath07085852003-10-09 07:28:22 +00001921 {
1922 bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
Rob Landleyadde7982006-05-16 15:32:30 +00001923 "BLKRAGET","readahead");
Eric Andersen3443bd72003-07-22 07:30:36 +00001924 }
Rob Landleyade7f952006-05-25 18:53:06 +00001925 if (get_geom)
Glenn L McGrath07085852003-10-09 07:28:22 +00001926 {
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001927 if (!bb_ioctl(fd, BLKGETSIZE, &parm, "BLKGETSIZE"))
1928 {
Rob Landley2584e9b2006-05-03 20:00:00 +00001929 struct hd_geometry g;
1930
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001931 if (!bb_ioctl(fd, HDIO_GETGEO, &g, "HDIO_GETGEO"))
Rob Landleyadde7982006-05-16 15:32:30 +00001932 printf(" geometry\t= %u/%u/%u, sectors = %ld, start = %ld\n",
Rob Landley2584e9b2006-05-03 20:00:00 +00001933 g.cylinders, g.heads, g.sectors, parm, g.start);
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001934 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001935 }
1936#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001937 if (get_powermode)
1938 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001939#ifndef WIN_CHECKPOWERMODE1
1940#define WIN_CHECKPOWERMODE1 0xE5
1941#endif
1942#ifndef WIN_CHECKPOWERMODE2
1943#define WIN_CHECKPOWERMODE2 0x98
1944#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00001945 const char *state;
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001946
Rob Landleye5b281f2006-04-29 15:49:18 +00001947 args[0] = WIN_CHECKPOWERMODE1;
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001948 if (bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2, 0))
Glenn L McGrath07085852003-10-09 07:28:22 +00001949 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001950 if (errno != EIO || args[0] != 0 || args[1] != 0)
Rob Landleyadde7982006-05-16 15:32:30 +00001951 state = "Unknown";
Eric Andersen3443bd72003-07-22 07:30:36 +00001952 else
1953 state = "sleeping";
Eric Andersen3443bd72003-07-22 07:30:36 +00001954 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001955 else
1956 state = (args[2] == 255) ? "active/idle" : "standby";
Rob Landley39cf6452006-05-05 16:52:28 +00001957 args[1] = args[2] = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +00001958
Eric Andersen3443bd72003-07-22 07:30:36 +00001959 printf(" drive state is: %s\n", state);
1960 }
1961#endif
1962#ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
Glenn L McGrath07085852003-10-09 07:28:22 +00001963 if (perform_reset)
1964 {
Glenn L McGrath07085852003-10-09 07:28:22 +00001965 bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
Eric Andersen3443bd72003-07-22 07:30:36 +00001966 }
1967#endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
1968#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00001969 if (perform_tristate)
1970 {
Rob Landleye5b281f2006-04-29 15:49:18 +00001971 args[0] = 0;
1972 args[1] = tristate;
Glenn L McGrath07085852003-10-09 07:28:22 +00001973 bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
Eric Andersen3443bd72003-07-22 07:30:36 +00001974 }
1975#endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1976#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +00001977 if (get_identity)
1978 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001979 static struct hd_driveid id;
1980
Glenn L McGrath07085852003-10-09 07:28:22 +00001981 if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
1982 {
1983 if (multcount != -1)
1984 {
Eric Andersen3443bd72003-07-22 07:30:36 +00001985 id.multsect = multcount;
1986 id.multsect_valid |= 1;
Glenn L McGrath07085852003-10-09 07:28:22 +00001987 }
1988 else
Eric Andersen3443bd72003-07-22 07:30:36 +00001989 id.multsect_valid &= ~1;
1990 dump_identity(&id);
Glenn L McGrath07085852003-10-09 07:28:22 +00001991 }
1992 else if (errno == -ENOMSG)
Eric Andersen3443bd72003-07-22 07:30:36 +00001993 printf(" no identification info available\n");
1994 else
Eric Andersen06d4ec22004-03-19 10:53:52 +00001995 bb_perror_msg("HDIO_GET_IDENTITY");
Eric Andersen3443bd72003-07-22 07:30:36 +00001996 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001997
1998 if (get_IDentity)
1999 {
Rob Landley5f8b5ec2006-04-29 16:03:40 +00002000 unsigned char args1[4+512]; /* = { ... } will eat 0.5k of rodata! */
Glenn L McGrath07085852003-10-09 07:28:22 +00002001
Rob Landley5f8b5ec2006-04-29 16:03:40 +00002002 memset(args1, 0, sizeof(args1));
2003 args1[0] = WIN_IDENTIFY;
2004 args1[3] = 1;
Rob Landley0753f4a2006-06-07 00:27:25 +00002005 if (!bb_ioctl_alt(fd, HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY, "HDIO_DRIVE_CMD(identify)"))
2006 identify((void *)(args1 + 4));
Eric Andersen3443bd72003-07-22 07:30:36 +00002007 }
2008#endif
2009#ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Glenn L McGrath07085852003-10-09 07:28:22 +00002010 if (set_busstate)
2011 {
Glenn L McGrath07085852003-10-09 07:28:22 +00002012 if (get_busstate)
2013 {
Rob Landleyadde7982006-05-16 15:32:30 +00002014 print_flag(1, "bus state", busstate);
Eric Andersen3443bd72003-07-22 07:30:36 +00002015 bus_state_value(busstate);
2016 }
Eric Andersena68ea1c2006-01-30 22:48:39 +00002017 bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate, "HDIO_SET_BUSSTATE");
Eric Andersen3443bd72003-07-22 07:30:36 +00002018 }
Glenn L McGrath07085852003-10-09 07:28:22 +00002019 if (get_busstate)
2020 {
Rob Landley5f8b5ec2006-04-29 16:03:40 +00002021 if (!bb_ioctl(fd, HDIO_GET_BUSSTATE, &parm, "HDIO_GET_BUSSTATE"))
Glenn L McGrath07085852003-10-09 07:28:22 +00002022 {
Rob Landleyadde7982006-05-16 15:32:30 +00002023 printf(fmt, "bus state", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00002024 bus_state_value(parm);
2025 }
2026 }
2027#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00002028 if (reread_partn)
2029 bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
2030
Eric Andersen3443bd72003-07-22 07:30:36 +00002031
2032 if (do_ctimings)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002033 do_time(0,fd); /*time cache */
Eric Andersen3443bd72003-07-22 07:30:36 +00002034 if (do_timings)
Glenn L McGrath07085852003-10-09 07:28:22 +00002035 do_time(1,fd); /*time device */
Eric Andersen3443bd72003-07-22 07:30:36 +00002036 if (do_flush)
Rob Landleya3e4f382006-04-29 16:06:31 +00002037 flush_buffer_cache(fd);
2038 close(fd);
Eric Andersen3443bd72003-07-22 07:30:36 +00002039}
2040
Eric Andersen3443bd72003-07-22 07:30:36 +00002041#ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
Rob Landleya3e4f382006-04-29 16:06:31 +00002042static int fromhex(unsigned char c)
Eric Andersen3443bd72003-07-22 07:30:36 +00002043{
2044 if (c >= 'a' && c <= 'f')
2045 return 10 + (c - 'a');
2046 if (c >= '0' && c <= '9')
2047 return (c - '0');
2048 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
2049}
2050
Rob Landley0f0b6452006-05-03 18:28:06 +00002051static void identify_from_stdin(void)
Eric Andersen3443bd72003-07-22 07:30:36 +00002052{
Rob Landley0753f4a2006-06-07 00:27:25 +00002053 uint16_t sbuf[256];
2054 unsigned char buf[1280], *b = (unsigned char *)buf;
Eric Andersen3443bd72003-07-22 07:30:36 +00002055 int i, count = read(0, buf, 1280);
2056
2057 if (count != 1280)
Rob Landleyadde7982006-05-16 15:32:30 +00002058 bb_error_msg_and_die("read(%d bytes) failed (rc=%d)", 1280, count);
Rob Landleyade7f952006-05-25 18:53:06 +00002059
Rob Landley0753f4a2006-06-07 00:27:25 +00002060 // Convert the newline-separated hex data into an identify block.
2061
2062 for (i = 0; i<256; i++)
Glenn L McGrath07085852003-10-09 07:28:22 +00002063 {
Rob Landley0753f4a2006-06-07 00:27:25 +00002064 int j;
2065 for(j=0;j<4;j++) sbuf[i] = (sbuf[i] <<4) + fromhex(*(b++));
Eric Andersen3443bd72003-07-22 07:30:36 +00002066 }
Rob Landley0753f4a2006-06-07 00:27:25 +00002067
2068 // Parse the data.
2069
Rob Landley6389ff12006-05-01 19:28:53 +00002070 identify(sbuf);
Eric Andersen3443bd72003-07-22 07:30:36 +00002071}
2072#endif
2073
Rob Landley20deab02006-05-07 23:34:15 +00002074/* busybox specific stuff */
Rob Landley19802562006-05-08 15:35:46 +00002075static void parse_opts(unsigned long *get, unsigned long *set, unsigned long *value, int min, int max)
Eric Andersenb2aa7762004-04-05 13:08:08 +00002076{
Rob Landleyade7f952006-05-25 18:53:06 +00002077 if (get) {
2078 *get = 1;
Rob Landley20deab02006-05-07 23:34:15 +00002079 }
2080 if (optarg) {
2081 *set = 1;
2082 *value = bb_xgetlarg(optarg, 10, min, max);
Eric Andersen06d4ec22004-03-19 10:53:52 +00002083 }
Rob Landley20deab02006-05-07 23:34:15 +00002084}
2085
Rob Landleyade7f952006-05-25 18:53:06 +00002086static void parse_xfermode(int flag, unsigned long *get, unsigned long *set, int *value)
Rob Landley20deab02006-05-07 23:34:15 +00002087{
2088 if (flag) {
Rob Landleyade7f952006-05-25 18:53:06 +00002089 *get = 1;
Rob Landley20deab02006-05-07 23:34:15 +00002090 if (optarg) {
2091 *set = ((*value = translate_xfermode(optarg)) > -1);
2092 }
2093 }
2094}
2095
Rob Landley06208412006-05-31 22:52:57 +00002096/*------- getopt short options --------*/
Rob Landleyade7f952006-05-25 18:53:06 +00002097static const char hdparm_options[]= "gfu::n::p:r::m::c::k::a::B:tTh"\
Rob Landley06208412006-05-31 22:52:57 +00002098 USE_FEATURE_HDPARM_GET_IDENTITY("iI")
Rob Landley20deab02006-05-07 23:34:15 +00002099 USE_FEATURE_HDPARM_HDIO_GETSET_DMA("d::")
2100#ifdef HDIO_DRIVE_CMD
2101 "S::D::P::X::K::A::L::W::CyYzZ"
2102#endif
2103 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF("U:")
2104#ifdef HDIO_GET_QDMA
2105#ifdef HDIO_SET_QDMA
2106 "Q:"
2107#else
2108 "Q"
2109#endif
2110#endif
2111 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET("w")
2112 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF("x::b:")
2113 USE_FEATURE_HDPARM_HDIO_SCAN_HWIF("R:");
2114/*-------------------------------------*/
2115
2116/* our main() routine: */
2117int hdparm_main(int argc, char **argv) ATTRIBUTE_NORETURN;
2118int hdparm_main(int argc, char **argv)
2119{
2120 int c;
Rob Landleyade7f952006-05-25 18:53:06 +00002121 int flagcount = 0;
Rob Landley20deab02006-05-07 23:34:15 +00002122
Rob Landley06208412006-05-31 22:52:57 +00002123 while ((c = getopt(argc, argv, hdparm_options)) >= 0) {
Rob Landley20deab02006-05-07 23:34:15 +00002124 flagcount++;
Rob Landley06208412006-05-31 22:52:57 +00002125 if (c == 'h') bb_show_usage(); /* EXIT */
Mike Frysingerea93f8a2006-06-07 14:25:22 +00002126 USE_FEATURE_HDPARM_GET_IDENTITY(get_IDentity |= (c == 'I'));
2127 USE_FEATURE_HDPARM_GET_IDENTITY(get_identity |= (c == 'i'));
Rob Landleyadde7982006-05-16 15:32:30 +00002128 get_geom |= (c == 'g');
2129 do_flush |= (c == 'f');
Rob Landley19802562006-05-08 15:35:46 +00002130 if (c == 'u') parse_opts(&get_unmask, &set_unmask, &unmask, 0, 1);
2131 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(if (c == 'd') parse_opts(&get_dma, &set_dma, &dma, 0, 9));
2132 if (c == 'n') parse_opts(&get_nowerr, &set_nowerr, &nowerr, 0, 1);
Rob Landleyade7f952006-05-25 18:53:06 +00002133 parse_xfermode((c == 'p'),&noisy_piomode, &set_piomode, &piomode);
Rob Landley19802562006-05-08 15:35:46 +00002134 if (c == 'r') parse_opts(&get_readonly, &set_readonly, &readonly, 0, 1);
2135 if (c == 'm') parse_opts(&get_mult, &set_mult, &mult, 0, INT_MAX /*32*/);
2136 if (c == 'c') parse_opts(&get_io32bit, &set_io32bit, &io32bit, 0, INT_MAX /*8*/);
2137 if (c == 'k') parse_opts(&get_keep, &set_keep, &keep, 0, 1);
2138 if (c == 'a') parse_opts(&get_readahead, &set_readahead, &Xreadahead, 0, INT_MAX);
2139 if (c == 'B') parse_opts(&get_apmmode, &set_apmmode, &apmmode, 1, 255);
Rob Landleyadde7982006-05-16 15:32:30 +00002140 do_flush |= do_timings |= (c == 't');
2141 do_flush |= do_ctimings |= (c == 'T');
Rob Landley20deab02006-05-07 23:34:15 +00002142#ifdef HDIO_DRIVE_CMD
Rob Landley19802562006-05-08 15:35:46 +00002143 if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, INT_MAX);
2144 if (c == 'D') parse_opts(&get_defects, &set_defects, &defects, 0, INT_MAX);
2145 if (c == 'P') parse_opts(&get_prefetch, &set_prefetch, &prefetch, 0, INT_MAX);
Rob Landleyade7f952006-05-25 18:53:06 +00002146 parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
Rob Landley19802562006-05-08 15:35:46 +00002147 if (c == 'K') parse_opts(&get_dkeep, &set_dkeep, &prefetch, 0, 1);
2148 if (c == 'A') parse_opts(&get_lookahead, &set_lookahead, &lookahead, 0, 1);
2149 if (c == 'L') parse_opts(&get_doorlock, &set_doorlock, &doorlock, 0, 1);
2150 if (c == 'W') parse_opts(&get_wcache, &set_wcache, &wcache, 0, 1);
Rob Landleyade7f952006-05-25 18:53:06 +00002151 get_powermode |= (c == 'C');
2152 get_standbynow = set_standbynow |= (c == 'y');
2153 get_sleepnow = set_sleepnow |= (c == 'Y');
Rob Landleyadde7982006-05-16 15:32:30 +00002154 reread_partn |= (c == 'z');
Rob Landleyade7f952006-05-25 18:53:06 +00002155 get_seagate = set_seagate |= (c == 'Z');
Rob Landley20deab02006-05-07 23:34:15 +00002156#endif
Rob Landley19802562006-05-08 15:35:46 +00002157 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 +00002158#ifdef HDIO_GET_QDMA
Rob Landley19802562006-05-08 15:35:46 +00002159 if (c == 'Q') {
Rob Landley20deab02006-05-07 23:34:15 +00002160#ifdef HDIO_SET_QDMA
Rob Landley19802562006-05-08 15:35:46 +00002161 parse_opts(&get_dma_q, &set_dma_q, &dma_q, 0, INT_MAX);
Rob Landley20deab02006-05-07 23:34:15 +00002162#else
Rob Landley19802562006-05-08 15:35:46 +00002163 parse_opts(&get_dma_q, NULL, NULL, 0, 0);
Rob Landley20deab02006-05-07 23:34:15 +00002164#endif
Rob Landley19802562006-05-08 15:35:46 +00002165 }
Rob Landley20deab02006-05-07 23:34:15 +00002166#endif
Rob Landleyade7f952006-05-25 18:53:06 +00002167 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
Rob Landley19802562006-05-08 15:35:46 +00002168 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1));
2169 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 +00002170#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
2171 if (c == 'R') {
Rob Landley19802562006-05-08 15:35:46 +00002172 parse_opts(NULL, &scan_hwif, &hwif_data, 0, INT_MAX);
Rob Landley20deab02006-05-07 23:34:15 +00002173 hwif_ctrl = bb_xgetlarg((argv[optind]) ? argv[optind] : "", 10, 0, INT_MAX);
2174 hwif_irq = bb_xgetlarg((argv[optind+1]) ? argv[optind+1] : "", 10, 0, INT_MAX);
2175 /* Move past the 2 additional arguments */
2176 argv += 2;
2177 argc -= 2;
2178 }
2179#endif
2180 }
Rob Landleyade7f952006-05-25 18:53:06 +00002181 /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
2182 if (!flagcount){
2183 get_mult = get_io32bit = get_unmask = get_keep = get_readonly = get_readahead = get_geom = 1;
2184 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(get_dma = 1);
2185 }
Rob Landley20deab02006-05-07 23:34:15 +00002186 argc -= optind;
2187 argv += optind;
2188
2189 if (argc < 1) {
Rob Landley6d8ce172006-06-07 21:22:42 +00002190 if (ENABLE_FEATURE_HDPARM_GET_IDENTITY && !isatty(STDIN_FILENO))
2191 identify_from_stdin(); /* EXIT */
2192 else bb_show_usage();
Rob Landley20deab02006-05-07 23:34:15 +00002193 }
2194
2195 while (argc--) {
2196 process_dev(*argv);
2197 argv++;
2198 }
2199 exit(EXIT_SUCCESS);
Eric Andersen3443bd72003-07-22 07:30:36 +00002200}