blob: 484c97061542ef1043d2f18c2cd97d785ed695e5 [file] [log] [blame]
Eric Andersen3443bd72003-07-22 07:30:36 +00001/* vi: set sw=4 ts=4: */
2/*
3 * hdparm implementation for busybox
4 *
Glenn L McGrath5be6a202003-11-28 22:55:03 +00005 * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
Glenn L McGrath07085852003-10-09 07:28:22 +00006 * Hacked by Tito <farmatito@tiscali.it> for size optimization.
Eric Andersen3443bd72003-07-22 07:30:36 +00007 *
Mike Frysingerf284c762006-04-16 20:38:26 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen3443bd72003-07-22 07:30:36 +00009 *
10 * This program is based on the source code of hdparm: see below...
11 * hdparm.c - Command line interface to get/set hard disk parameters
12 * - by Mark Lord (C) 1994-2002 -- freely distributable
13 */
Glenn L McGrath07085852003-10-09 07:28:22 +000014
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Eric Andersen3443bd72003-07-22 07:30:36 +000016#include <linux/hdreg.h>
Eric Andersen3443bd72003-07-22 07:30:36 +000017
Eric Andersen3443bd72003-07-22 07:30:36 +000018/* device types */
19/* ------------ */
20#define NO_DEV 0xffff
21#define ATA_DEV 0x0000
22#define ATAPI_DEV 0x0001
23
24/* word definitions */
25/* ---------------- */
26#define GEN_CONFIG 0 /* general configuration */
27#define LCYLS 1 /* number of logical cylinders */
28#define CONFIG 2 /* specific configuration */
29#define LHEADS 3 /* number of logical heads */
30#define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
31#define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
32#define LSECTS 6 /* number of logical sectors/track */
33#define START_SERIAL 10 /* ASCII serial number */
34#define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
35#define BUF_TYPE 20 /* buffer type (ATA-1) */
36#define BUFFER__SIZE 21 /* buffer size (ATA-1) */
37#define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
38#define START_FW_REV 23 /* ASCII firmware revision */
39#define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000040#define START_MODEL 27 /* ASCII model number */
41#define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
Denis Vlasenko4b924f32007-05-30 00:29:55 +000042#define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
Eric Andersen3443bd72003-07-22 07:30:36 +000043#define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
44#define CAPAB_0 49 /* capabilities */
45#define CAPAB_1 50
46#define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
47#define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
48#define WHATS_VALID 53 /* what fields are valid */
49#define LCYLS_CUR 54 /* current logical cylinders */
50#define LHEADS_CUR 55 /* current logical heads */
Denis Vlasenko4b924f32007-05-30 00:29:55 +000051#define LSECTS_CUR 56 /* current logical sectors/track */
Eric Andersen3443bd72003-07-22 07:30:36 +000052#define CAPACITY_LSB 57 /* current capacity in sectors */
53#define CAPACITY_MSB 58
54#define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
55#define LBA_SECTS_LSB 60 /* LBA: total number of user */
56#define LBA_SECTS_MSB 61 /* addressable sectors */
57#define SINGLE_DMA 62 /* singleword DMA modes */
58#define MULTI_DMA 63 /* multiword DMA modes */
59#define ADV_PIO_MODES 64 /* advanced PIO modes supported */
60 /* multiword DMA xfer cycle time: */
61#define DMA_TIME_MIN 65 /* - minimum */
62#define DMA_TIME_NORM 66 /* - manufacturer's recommended */
63 /* minimum PIO xfer cycle time: */
64#define PIO_NO_FLOW 67 /* - without flow control */
65#define PIO_FLOW 68 /* - with IORDY flow control */
66#define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
67#define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
68#define CDR_MAJOR 73 /* CD ROM: major version number */
69#define CDR_MINOR 74 /* CD ROM: minor version number */
70#define QUEUE_DEPTH 75 /* queue depth */
71#define MAJOR 80 /* major version number */
72#define MINOR 81 /* minor version number */
73#define CMDS_SUPP_0 82 /* command/feature set(s) supported */
74#define CMDS_SUPP_1 83
75#define CMDS_SUPP_2 84
76#define CMDS_EN_0 85 /* command/feature set(s) enabled */
77#define CMDS_EN_1 86
78#define CMDS_EN_2 87
79#define ULTRA_DMA 88 /* ultra DMA modes */
80 /* time to complete security erase */
81#define ERASE_TIME 89 /* - ordinary */
82#define ENH_ERASE_TIME 90 /* - enhanced */
83#define ADV_PWR 91 /* current advanced power management level
Glenn L McGrath07085852003-10-09 07:28:22 +000084 in low byte, 0x40 in high byte. */
Eric Andersen3443bd72003-07-22 07:30:36 +000085#define PSWD_CODE 92 /* master password revision code */
86#define HWRST_RSLT 93 /* hardware reset result */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000087#define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
Eric Andersen3443bd72003-07-22 07:30:36 +000088#define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
89#define LBA_MID 101 /* bits are used, but addr 103 */
90#define LBA_48_MSB 102 /* has been reserved for LBA in */
91#define LBA_64_MSB 103 /* the future. */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000092#define RM_STAT 127 /* removable media status notification feature set support */
Eric Andersen3443bd72003-07-22 07:30:36 +000093#define SECU_STATUS 128 /* security status */
94#define CFA_PWR_MODE 160 /* CFA power mode 1 */
95#define START_MEDIA 176 /* media serial number */
96#define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
97#define START_MANUF 196 /* media manufacturer I.D. */
98#define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
99#define INTEGRITY 255 /* integrity word */
100
101/* bit definitions within the words */
102/* -------------------------------- */
103
104/* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
105#define VALID 0xc000
106#define VALID_VAL 0x4000
107/* many words are considered invalid if they are either all-0 or all-1 */
108#define NOVAL_0 0x0000
109#define NOVAL_1 0xffff
110
111/* word 0: gen_config */
Glenn L McGrath07085852003-10-09 07:28:22 +0000112#define NOT_ATA 0x8000
Eric Andersen3443bd72003-07-22 07:30:36 +0000113#define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
114#define MEDIA_REMOVABLE 0x0080
115#define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
116#define INCOMPLETE 0x0004
117#define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
118#define DRQ_RESPONSE_TIME 0x0060
119#define DRQ_3MS_VAL 0x0000
120#define DRQ_INTR_VAL 0x0020
121#define DRQ_50US_VAL 0x0040
122#define PKT_SIZE_SUPPORTED 0x0003
123#define PKT_SIZE_12_VAL 0x0000
124#define PKT_SIZE_16_VAL 0x0001
125#define EQPT_TYPE 0x1f00
126#define SHIFT_EQPT 8
127
128#define CDROM 0x0005
129
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000130#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000131static const char *const pkt_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000132 "Direct-access device", /* word 0, bits 12-8 = 00 */
133 "Sequential-access device", /* word 0, bits 12-8 = 01 */
134 "Printer", /* word 0, bits 12-8 = 02 */
135 "Processor", /* word 0, bits 12-8 = 03 */
136 "Write-once device", /* word 0, bits 12-8 = 04 */
137 "CD-ROM", /* word 0, bits 12-8 = 05 */
138 "Scanner", /* word 0, bits 12-8 = 06 */
139 "Optical memory", /* word 0, bits 12-8 = 07 */
140 "Medium changer", /* word 0, bits 12-8 = 08 */
141 "Communications device", /* word 0, bits 12-8 = 09 */
142 "ACS-IT8 device", /* word 0, bits 12-8 = 0a */
143 "ACS-IT8 device", /* word 0, bits 12-8 = 0b */
144 "Array controller", /* word 0, bits 12-8 = 0c */
145 "Enclosure services", /* word 0, bits 12-8 = 0d */
146 "Reduced block command device", /* word 0, bits 12-8 = 0e */
147 "Optical card reader/writer", /* word 0, bits 12-8 = 0f */
148 "", /* word 0, bits 12-8 = 10 */
149 "", /* word 0, bits 12-8 = 11 */
150 "", /* word 0, bits 12-8 = 12 */
151 "", /* word 0, bits 12-8 = 13 */
152 "", /* word 0, bits 12-8 = 14 */
153 "", /* word 0, bits 12-8 = 15 */
154 "", /* word 0, bits 12-8 = 16 */
155 "", /* word 0, bits 12-8 = 17 */
156 "", /* word 0, bits 12-8 = 18 */
157 "", /* word 0, bits 12-8 = 19 */
158 "", /* word 0, bits 12-8 = 1a */
159 "", /* word 0, bits 12-8 = 1b */
160 "", /* word 0, bits 12-8 = 1c */
161 "", /* word 0, bits 12-8 = 1d */
162 "", /* word 0, bits 12-8 = 1e */
Rob Landleyadde7982006-05-16 15:32:30 +0000163 "Unknown", /* word 0, bits 12-8 = 1f */
Eric Andersen3443bd72003-07-22 07:30:36 +0000164};
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +0000165
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000166static const char *const ata1_cfg_str[] = { /* word 0 in ATA-1 mode */
Rob Landleyadde7982006-05-16 15:32:30 +0000167 "Reserved", /* bit 0 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000168 "hard sectored", /* bit 1 */
169 "soft sectored", /* bit 2 */
170 "not MFM encoded ", /* bit 3 */
171 "head switch time > 15us", /* bit 4 */
172 "spindle motor control option", /* bit 5 */
173 "fixed drive", /* bit 6 */
174 "removable drive", /* bit 7 */
175 "disk xfer rate <= 5Mbs", /* bit 8 */
176 "disk xfer rate > 5Mbs, <= 10Mbs", /* bit 9 */
177 "disk xfer rate > 5Mbs", /* bit 10 */
178 "rotational speed tol.", /* bit 11 */
179 "data strobe offset option", /* bit 12 */
180 "track offset option", /* bit 13 */
181 "format speed tolerance gap reqd", /* bit 14 */
182 "ATAPI" /* bit 14 */
183};
184#endif
185
186/* word 1: number of logical cylinders */
187#define LCYLS_MAX 0x3fff /* maximum allowable value */
188
Eric Andersenaff114c2004-04-14 17:51:38 +0000189/* word 2: specific configuration
Eric Andersen3443bd72003-07-22 07:30:36 +0000190 * (a) require SET FEATURES to spin-up
191 * (b) require spin-up to fully reply to IDENTIFY DEVICE
192 */
193#define STBY_NID_VAL 0x37c8 /* (a) and (b) */
194#define STBY_ID_VAL 0x738c /* (a) and not (b) */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000195#define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
Eric Andersen3443bd72003-07-22 07:30:36 +0000196#define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
197
198/* words 47 & 59: sector_xfer_max & sector_xfer_cur */
199#define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
200#define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
201
202/* word 49: capabilities 0 */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000203#define STD_STBY 0x2000 /* 1=standard values supported (ATA); 0=vendor specific values */
Eric Andersen3443bd72003-07-22 07:30:36 +0000204#define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
205#define IORDY_OFF 0x0400 /* 1=may be disabled */
206#define LBA_SUP 0x0200 /* 1=Logical Block Address support */
207#define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
208#define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
209#define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
210#define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
211#define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
212
213/* word 50: capabilities 1 */
214#define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
215
216/* words 51 & 52: PIO & DMA cycle times */
217#define MODE 0xff00 /* the mode is in the MSBs */
218
219/* word 53: whats_valid */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000220#define OK_W88 0x0004 /* the ultra_dma info is valid */
Eric Andersen3443bd72003-07-22 07:30:36 +0000221#define OK_W64_70 0x0002 /* see above for word descriptions */
222#define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
223
224/*word 63,88: dma_mode, ultra_dma_mode*/
225#define MODE_MAX 7 /* bit definitions force udma <=7 (when
226 * udma >=8 comes out it'll have to be
227 * defined in a new dma_mode word!) */
228
229/* word 64: PIO transfer modes */
230#define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
231#define PIO_MODE_MAX 8 /* but all 8 bits are defined */
232
233/* word 75: queue_depth */
234#define DEPTH_BITS 0x001f /* bits used for queue depth */
235
236/* words 80-81: version numbers */
237/* NOVAL_0 or NOVAL_1 means device does not report version */
238
239/* word 81: minor version number */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000240#define MINOR_MAX 0x22
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000241#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Rob Landley0e6a3e12006-04-28 01:33:30 +0000242static const char *minor_str[MINOR_MAX+2] = { /* word 81 value: */
243 "Unspecified", /* 0x0000 */
Rob Landleyadde7982006-05-16 15:32:30 +0000244 "ATA-1 X3T9.2 781D prior to rev.4", /* 0x0001 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000245 "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
Rob Landleyadde7982006-05-16 15:32:30 +0000246 "ATA-1 X3T9.2 781D rev.4", /* 0x0003 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000247 "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
Rob Landleyadde7982006-05-16 15:32:30 +0000248 "ATA-2 X3T10 948D prior to rev.2k", /* 0x0005 */
249 "ATA-3 X3T10 2008D rev.1", /* 0x0006 */
250 "ATA-2 X3T10 948D rev.2k", /* 0x0007 */
251 "ATA-3 X3T10 2008D rev.0", /* 0x0008 */
252 "ATA-2 X3T10 948D rev.3", /* 0x0009 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000253 "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
Rob Landleyadde7982006-05-16 15:32:30 +0000254 "ATA-3 X3T10 2008D rev.6", /* 0x000b */
255 "ATA-3 X3T13 2008D rev.7 and 7a", /* 0x000c */
256 "ATA/ATAPI-4 X3T13 1153D rev.6", /* 0x000d */
257 "ATA/ATAPI-4 T13 1153D rev.13", /* 0x000e */
258 "ATA/ATAPI-4 X3T13 1153D rev.7", /* 0x000f */
259 "ATA/ATAPI-4 T13 1153D rev.18", /* 0x0010 */
260 "ATA/ATAPI-4 T13 1153D rev.15", /* 0x0011 */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000261 "ATA/ATAPI-4 published, ANSI INCITS 317-1998", /* 0x0012 */
Rob Landleyadde7982006-05-16 15:32:30 +0000262 "ATA/ATAPI-5 T13 1321D rev.3",
263 "ATA/ATAPI-4 T13 1153D rev.14", /* 0x0014 */
264 "ATA/ATAPI-5 T13 1321D rev.1", /* 0x0015 */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000265 "ATA/ATAPI-5 published, ANSI INCITS 340-2000", /* 0x0016 */
Rob Landleyadde7982006-05-16 15:32:30 +0000266 "ATA/ATAPI-4 T13 1153D rev.17", /* 0x0017 */
267 "ATA/ATAPI-6 T13 1410D rev.0", /* 0x0018 */
268 "ATA/ATAPI-6 T13 1410D rev.3a", /* 0x0019 */
269 "ATA/ATAPI-7 T13 1532D rev.1", /* 0x001a */
270 "ATA/ATAPI-6 T13 1410D rev.2", /* 0x001b */
271 "ATA/ATAPI-6 T13 1410D rev.1", /* 0x001c */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000272 "ATA/ATAPI-7 published, ANSI INCITS 397-2005", /* 0x001d */
Rob Landleyadde7982006-05-16 15:32:30 +0000273 "ATA/ATAPI-7 T13 1532D rev.0", /* 0x001e */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000274 "Reserved" /* 0x001f */
275 "Reserved" /* 0x0020 */
Rob Landleyadde7982006-05-16 15:32:30 +0000276 "ATA/ATAPI-7 T13 1532D rev.4a", /* 0x0021 */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000277 "ATA/ATAPI-6 published, ANSI INCITS 361-2002", /* 0x0022 */
278 "Reserved" /* 0x0023-0xfffe*/
Eric Andersen3443bd72003-07-22 07:30:36 +0000279};
280#endif
Rob Landley0e6a3e12006-04-28 01:33:30 +0000281static const char actual_ver[MINOR_MAX+2] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000282 /* word 81 value: */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000283 0, /* 0x0000 WARNING: */
284 1, /* 0x0001 WARNING: */
285 1, /* 0x0002 WARNING: */
286 1, /* 0x0003 WARNING: */
287 2, /* 0x0004 WARNING: This array */
288 2, /* 0x0005 WARNING: corresponds */
Eric Andersen3443bd72003-07-22 07:30:36 +0000289 3, /* 0x0006 WARNING: *exactly* */
290 2, /* 0x0007 WARNING: to the ATA/ */
291 3, /* 0x0008 WARNING: ATAPI version */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000292 2, /* 0x0009 WARNING: listed in */
293 3, /* 0x000a WARNING: the */
294 3, /* 0x000b WARNING: minor_str */
Eric Andersen3443bd72003-07-22 07:30:36 +0000295 3, /* 0x000c WARNING: array */
296 4, /* 0x000d WARNING: above. */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000297 4, /* 0x000e WARNING: */
298 4, /* 0x000f WARNING: if you change */
299 4, /* 0x0010 WARNING: that one, */
Eric Andersen3443bd72003-07-22 07:30:36 +0000300 4, /* 0x0011 WARNING: change this one */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000301 4, /* 0x0012 WARNING: too!!! */
Eric Andersen3443bd72003-07-22 07:30:36 +0000302 5, /* 0x0013 WARNING: */
303 4, /* 0x0014 WARNING: */
304 5, /* 0x0015 WARNING: */
305 5, /* 0x0016 WARNING: */
306 4, /* 0x0017 WARNING: */
307 6, /* 0x0018 WARNING: */
308 6, /* 0x0019 WARNING: */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000309 7, /* 0x001a WARNING: */
Eric Andersen3443bd72003-07-22 07:30:36 +0000310 6, /* 0x001b WARNING: */
311 6, /* 0x001c WARNING: */
Rob Landley0e6a3e12006-04-28 01:33:30 +0000312 7, /* 0x001d WARNING: */
313 7, /* 0x001e WARNING: */
314 0, /* 0x001f WARNING: */
315 0, /* 0x0020 WARNING: */
316 7, /* 0x0021 WARNING: */
317 6, /* 0x0022 WARNING: */
318 0 /* 0x0023-0xfffe */
Eric Andersen3443bd72003-07-22 07:30:36 +0000319};
320
321/* words 82-84: cmds/feats supported */
322#define CMDS_W82 0x77ff /* word 82: defined command locations*/
323#define CMDS_W83 0x3fff /* word 83: defined command locations*/
324#define CMDS_W84 0x002f /* word 83: defined command locations*/
Glenn L McGrath07085852003-10-09 07:28:22 +0000325#define SUPPORT_48_BIT 0x0400
Eric Andersen3443bd72003-07-22 07:30:36 +0000326#define NUM_CMD_FEAT_STR 48
327
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000328#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000329static const char *const cmd_feat_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000330 "", /* word 82 bit 15: obsolete */
331 "NOP cmd", /* word 82 bit 14 */
332 "READ BUFFER cmd", /* word 82 bit 13 */
333 "WRITE BUFFER cmd", /* word 82 bit 12 */
334 "", /* word 82 bit 11: obsolete */
335 "Host Protected Area feature set", /* word 82 bit 10 */
336 "DEVICE RESET cmd", /* word 82 bit 9 */
337 "SERVICE interrupt", /* word 82 bit 8 */
338 "Release interrupt", /* word 82 bit 7 */
339 "Look-ahead", /* word 82 bit 6 */
340 "Write cache", /* word 82 bit 5 */
341 "PACKET command feature set", /* word 82 bit 4 */
342 "Power Management feature set", /* word 82 bit 3 */
343 "Removable Media feature set", /* word 82 bit 2 */
344 "Security Mode feature set", /* word 82 bit 1 */
345 "SMART feature set", /* word 82 bit 0 */
346 /* --------------*/
347 "", /* word 83 bit 15: !valid bit */
348 "", /* word 83 bit 14: valid bit */
Rob Landleyadde7982006-05-16 15:32:30 +0000349 "FLUSH CACHE EXT cmd", /* word 83 bit 13 */
350 "Mandatory FLUSH CACHE cmd ", /* word 83 bit 12 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000351 "Device Configuration Overlay feature set ",
352 "48-bit Address feature set ", /* word 83 bit 10 */
353 "",
354 "SET MAX security extension", /* word 83 bit 8 */
355 "Address Offset Reserved Area Boot", /* word 83 bit 7 */
356 "SET FEATURES subcommand required to spinup after power up",
357 "Power-Up In Standby feature set", /* word 83 bit 5 */
358 "Removable Media Status Notification feature set",
Eric Andersen06d4ec22004-03-19 10:53:52 +0000359 "Adv. Power Management feature set",/* word 83 bit 3 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000360 "CFA feature set", /* word 83 bit 2 */
361 "READ/WRITE DMA QUEUED", /* word 83 bit 1 */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000362 "DOWNLOAD MICROCODE cmd", /* word 83 bit 0 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000363 /* --------------*/
364 "", /* word 84 bit 15: !valid bit */
365 "", /* word 84 bit 14: valid bit */
366 "", /* word 84 bit 13: reserved */
367 "", /* word 84 bit 12: reserved */
368 "", /* word 84 bit 11: reserved */
369 "", /* word 84 bit 10: reserved */
370 "", /* word 84 bit 9: reserved */
371 "", /* word 84 bit 8: reserved */
372 "", /* word 84 bit 7: reserved */
373 "", /* word 84 bit 6: reserved */
374 "General Purpose Logging feature set", /* word 84 bit 5 */
375 "", /* word 84 bit 4: reserved */
376 "Media Card Pass Through Command feature set ",
377 "Media serial number ", /* word 84 bit 2 */
378 "SMART self-test ", /* word 84 bit 1 */
379 "SMART error logging " /* word 84 bit 0 */
380};
Rob Landley0f0b6452006-05-03 18:28:06 +0000381
382static void identify(uint16_t *id_supplied) ATTRIBUTE_NORETURN;
383static void identify_from_stdin(void) ATTRIBUTE_NORETURN;
Rob Landley9c6f9552006-06-08 14:11:36 +0000384#else
385void identify_from_stdin(void);
Eric Andersen3443bd72003-07-22 07:30:36 +0000386#endif
387
388
389/* words 85-87: cmds/feats enabled */
390/* use cmd_feat_str[] to display what commands and features have
Glenn L McGrath07085852003-10-09 07:28:22 +0000391 * been enabled with words 85-87
Eric Andersen3443bd72003-07-22 07:30:36 +0000392 */
393
394/* words 89, 90, SECU ERASE TIME */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000395#define ERASE_BITS 0x00ff
Eric Andersen3443bd72003-07-22 07:30:36 +0000396
397/* word 92: master password revision */
398/* NOVAL_0 or NOVAL_1 means no support for master password revision */
399
400/* word 93: hw reset result */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000401#define CBLID 0x2000 /* CBLID status */
402#define RST0 0x0001 /* 1=reset to device #0 */
403#define DEV_DET 0x0006 /* how device num determined */
404#define JUMPER_VAL 0x0002 /* device num determined by jumper */
405#define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
Eric Andersen3443bd72003-07-22 07:30:36 +0000406
407/* word 127: removable media status notification feature set support */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000408#define RM_STAT_BITS 0x0003
409#define RM_STAT_SUP 0x0001
Glenn L McGrath07085852003-10-09 07:28:22 +0000410
Eric Andersen3443bd72003-07-22 07:30:36 +0000411/* word 128: security */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000412#define SECU_ENABLED 0x0002
413#define SECU_LEVEL 0x0010
414#define NUM_SECU_STR 6
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000415#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000416static const char *const secu_str[] = {
Eric Andersen3443bd72003-07-22 07:30:36 +0000417 "supported", /* word 128, bit 0 */
418 "enabled", /* word 128, bit 1 */
419 "locked", /* word 128, bit 2 */
420 "frozen", /* word 128, bit 3 */
421 "expired: security count", /* word 128, bit 4 */
422 "supported: enhanced erase" /* word 128, bit 5 */
423};
424#endif
425
426/* word 160: CFA power mode */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000427#define VALID_W160 0x8000 /* 1=word valid */
428#define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
429#define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
430#define MAX_AMPS 0x0fff /* value = max current in ma */
Eric Andersen3443bd72003-07-22 07:30:36 +0000431
432/* word 255: integrity */
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000433#define SIG 0x00ff /* signature location */
434#define SIG_VAL 0x00a5 /* signature value */
Eric Andersen3443bd72003-07-22 07:30:36 +0000435
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000436#define TIMING_MB 64
437#define TIMING_BUF_MB 1
438#define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
439#define BUFCACHE_FACTOR 2
Glenn L McGrath07085852003-10-09 07:28:22 +0000440
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000441#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
Glenn L McGrath07085852003-10-09 07:28:22 +0000442
443/* Busybox messages and functions */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000444#if ENABLE_IOCTL_HEX2STR_ERROR
445static int ioctl_alt_func(int fd, int cmd, unsigned char *args, int alt, const char *string)
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000446{
447 if (!ioctl(fd, cmd, args))
448 return 0;
449 args[0] = alt;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000450 return bb_ioctl_or_warn(fd, cmd, args, string);
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000451}
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000452#define ioctl_alt_or_warn(fd,cmd,args,alt) ioctl_alt_func(fd,cmd,args,alt,#cmd)
453#else
454static int ioctl_alt_func(int fd, int cmd, unsigned char *args, int alt)
455{
456 if (!ioctl(fd, cmd, args))
457 return 0;
458 args[0] = alt;
459 return bb_ioctl_or_warn(fd, cmd, args);
460}
461#define ioctl_alt_or_warn(fd,cmd,args,alt) ioctl_alt_func(fd,cmd,args,alt)
462#endif
Rob Landley5f8b5ec2006-04-29 16:03:40 +0000463
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000464static void on_off(int value)
465{
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000466 puts(value ? " (on)" : " (off)");
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000467}
Glenn L McGrath07085852003-10-09 07:28:22 +0000468
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000469static void print_flag_on_off(int get_arg, const char *s, unsigned long arg)
Glenn L McGrath07085852003-10-09 07:28:22 +0000470{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000471 if (get_arg) {
Rob Landleyadde7982006-05-16 15:32:30 +0000472 printf(" setting %s to %ld", s, arg);
Glenn L McGrath07085852003-10-09 07:28:22 +0000473 on_off(arg);
474 }
475}
476
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000477static void print_value_on_off(const char *str, unsigned long argp)
Glenn L McGrath07085852003-10-09 07:28:22 +0000478{
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +0000479 printf(" %s\t= %2ld", str, argp);
480 on_off(argp != 0);
Glenn L McGrath07085852003-10-09 07:28:22 +0000481}
Eric Andersen416c2422003-12-12 00:08:57 +0000482
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000483#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000484static void print_ascii(uint16_t *p, uint8_t length);
485
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000486static void xprint_ascii(uint16_t *val, int i, const char *string, int n)
Glenn L McGrath07085852003-10-09 07:28:22 +0000487{
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000488 if (val[i]) {
489 printf("\t%-20s", string);
Glenn L McGrath07085852003-10-09 07:28:22 +0000490 print_ascii(&val[i], n);
491 }
492}
Eric Andersen416c2422003-12-12 00:08:57 +0000493#endif
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000494/* end of busybox specific stuff */
Glenn L McGrath07085852003-10-09 07:28:22 +0000495
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000496#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Glenn L McGrath07085852003-10-09 07:28:22 +0000497static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
498{
Eric Andersen3443bd72003-07-22 07:30:36 +0000499 uint16_t ii;
500 uint8_t err_dma = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +0000501
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000502 for (ii = 0; ii <= MODE_MAX; ii++) {
503 if (mode_sel & 0x0001) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000504 printf("*%cdma%u ", cc, ii);
Rob Landleya3e4f382006-04-29 16:06:31 +0000505 if (*have_mode)
Glenn L McGrath07085852003-10-09 07:28:22 +0000506 err_dma = 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000507 *have_mode = 1;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000508 } else if (mode_sup & 0x0001)
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000509 printf("%cdma%u ", cc, ii);
Glenn L McGrath07085852003-10-09 07:28:22 +0000510
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000511 mode_sup >>= 1;
512 mode_sel >>= 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000513 }
514 return err_dma;
515}
Glenn L McGrath07085852003-10-09 07:28:22 +0000516
Denis Vlasenkoac678ec2007-04-16 22:32:04 +0000517static void print_ascii(uint16_t *p, uint8_t length)
518{
Eric Andersen3443bd72003-07-22 07:30:36 +0000519 uint8_t ii;
520 char cl;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000521
Eric Andersen3443bd72003-07-22 07:30:36 +0000522 /* find first non-space & print it */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000523 for (ii = 0; ii < length; ii++) {
524 if ((char)((*p)>>8) != ' ')
Glenn L McGrath07085852003-10-09 07:28:22 +0000525 break;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000526 cl = (char)(*p);
527 if (cl != ' ') {
528 if (cl != '\0')
529 printf("%c", cl);
Glenn L McGrath07085852003-10-09 07:28:22 +0000530 p++;
531 ii++;
Eric Andersen3443bd72003-07-22 07:30:36 +0000532 break;
533 }
534 p++;
535 }
536 /* print the rest */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000537 for (; ii< length; ii++) {
Rob Landleya3e4f382006-04-29 16:06:31 +0000538 if (!(*p))
Glenn L McGrath07085852003-10-09 07:28:22 +0000539 break; /* some older devices have NULLs */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000540 printf("%c%c", (char)((*p)>>8), (char)(*p));
Eric Andersen3443bd72003-07-22 07:30:36 +0000541 p++;
542 }
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000543 puts("");
Eric Andersen3443bd72003-07-22 07:30:36 +0000544}
545
Rob Landley0753f4a2006-06-07 00:27:25 +0000546// Parse 512 byte disk identification block and print much crap.
547
Rob Landley6389ff12006-05-01 19:28:53 +0000548static void identify(uint16_t *id_supplied)
Eric Andersen3443bd72003-07-22 07:30:36 +0000549{
Rob Landley5bc3f052006-04-29 19:11:24 +0000550 uint16_t buf[256];
551 uint16_t *val, ii, jj, kk;
Eric Andersen3443bd72003-07-22 07:30:36 +0000552 uint16_t like_std = 1, std = 0, min_std = 0xffff;
553 uint16_t dev = NO_DEV, eqpt = NO_DEV;
554 uint8_t have_mode = 0, err_dma = 0;
555 uint8_t chksum = 0;
556 uint32_t ll, mm, nn, oo;
Rob Landley2e2d7522006-04-29 15:23:33 +0000557 uint64_t bbbig; /* (:) */
Rob Landleyadde7982006-05-16 15:32:30 +0000558 const char *strng;
Eric Andersen3443bd72003-07-22 07:30:36 +0000559
Rob Landley0753f4a2006-06-07 00:27:25 +0000560 // Adjust for endianness if necessary.
561
Rob Landley5bc3f052006-04-29 19:11:24 +0000562 if (BB_BIG_ENDIAN) {
563 swab(id_supplied, buf, sizeof(buf));
564 val = buf;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000565 } else
566 val = id_supplied;
Rob Landley5bc3f052006-04-29 19:11:24 +0000567
Eric Andersen3443bd72003-07-22 07:30:36 +0000568 chksum &= 0xff;
569
570 /* check if we recognise the device type */
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000571 puts("");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000572 if (!(val[GEN_CONFIG] & NOT_ATA)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000573 dev = ATA_DEV;
574 printf("ATA device, with ");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000575 } else if (val[GEN_CONFIG]==CFA_SUPPORT_VAL) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000576 dev = ATA_DEV;
577 like_std = 4;
578 printf("CompactFlash ATA device, with ");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000579 } else if (!(val[GEN_CONFIG] & NOT_ATAPI)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000580 dev = ATAPI_DEV;
581 eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
582 printf("ATAPI %s, with ", pkt_str[eqpt]);
583 like_std = 3;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000584 } else
Glenn L McGrath07085852003-10-09 07:28:22 +0000585 /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000586 bb_error_msg_and_die("unknown device type");
Eric Andersen3443bd72003-07-22 07:30:36 +0000587
Rob Landleyadde7982006-05-16 15:32:30 +0000588 printf("%sremovable media\n", !(val[GEN_CONFIG] & MEDIA_REMOVABLE) ? "non-" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +0000589 /* Info from the specific configuration word says whether or not the
590 * ID command completed correctly. It is only defined, however in
Glenn L McGrath07085852003-10-09 07:28:22 +0000591 * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
Eric Andersen3443bd72003-07-22 07:30:36 +0000592 * standards. Since the values allowed for this word are extremely
593 * specific, it should be safe to check it now, even though we don't
594 * know yet what standard this device is using.
595 */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000596 if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)
597 || (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL)
598 ) {
Glenn L McGrath07085852003-10-09 07:28:22 +0000599 like_std = 5;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000600 if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL))
Rob Landleyadde7982006-05-16 15:32:30 +0000601 printf("powers-up in standby; SET FEATURES subcmd spins-up.\n");
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000602 if (((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE))
Rob Landleyadde7982006-05-16 15:32:30 +0000603 printf("\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000604 }
605
606 /* output the model and serial numbers and the fw revision */
Glenn L McGrath07085852003-10-09 07:28:22 +0000607 xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
608 xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
609 xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
610 xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
611 xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
Eric Andersen3443bd72003-07-22 07:30:36 +0000612
613 /* major & minor standards version number (Note: these words were not
614 * defined until ATA-3 & the CDROM std uses different words.) */
615 printf("Standards:");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000616 if (eqpt != CDROM) {
617 if (val[MINOR] && (val[MINOR] <= MINOR_MAX)) {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000618 if (like_std < 3) like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000619 std = actual_ver[val[MINOR]];
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000620 if (std) printf("\n\tUsed: %s ", minor_str[val[MINOR]]);
Glenn L McGrath07085852003-10-09 07:28:22 +0000621
Eric Andersen3443bd72003-07-22 07:30:36 +0000622 }
623 /* looks like when they up-issue the std, they obsolete one;
Glenn L McGrath07085852003-10-09 07:28:22 +0000624 * thus, only the newest 4 issues need be supported. (That's
Eric Andersen3443bd72003-07-22 07:30:36 +0000625 * what "kk" and "min_std" are all about.) */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000626 if (val[MAJOR] && (val[MAJOR] != NOVAL_1)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000627 printf("\n\tSupported: ");
628 jj = val[MAJOR] << 1;
629 kk = like_std >4 ? like_std-4: 0;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000630 for (ii = 14; (ii >0)&&(ii>kk); ii--) {
631 if (jj & 0x8000) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000632 printf("%u ", ii);
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000633 if (like_std < ii) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000634 like_std = ii;
635 kk = like_std >4 ? like_std-4: 0;
636 }
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000637 if (min_std > ii) min_std = ii;
Eric Andersen3443bd72003-07-22 07:30:36 +0000638 }
639 jj <<= 1;
640 }
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000641 if (like_std < 3) like_std = 3;
Eric Andersen3443bd72003-07-22 07:30:36 +0000642 }
643 /* Figure out what standard the device is using if it hasn't told
644 * us. If we know the std, check if the device is using any of
645 * the words from the next level up. It happens.
646 */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000647 if (like_std < std) like_std = std;
Glenn L McGrath07085852003-10-09 07:28:22 +0000648
Rob Landleya3e4f382006-04-29 16:06:31 +0000649 if (((std == 5) || (!std && (like_std < 6))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000650 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
651 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
652 ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000653 ( val[CMDS_SUPP_2] & CMDS_W84) ) )
654 ) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000655 like_std = 6;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000656 } else if (((std == 4) || (!std && (like_std < 5))) &&
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000657 ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
Glenn L McGrath07085852003-10-09 07:28:22 +0000658 (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
659 ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
660 (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
661 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000662 like_std = 5;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000663 } else if (((std == 3) || (!std && (like_std < 4))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000664 ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
665 ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
666 (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
667 (( val[CAPAB_1] & VALID) == VALID_VAL) ||
668 (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000669 (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) )
670 ) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000671 like_std = 4;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000672 } else if (((std == 2) || (!std && (like_std < 3)))
673 && ((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
674 ) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000675 like_std = 3;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000676 } else if (((std == 1) || (!std && (like_std < 2))) &&
Glenn L McGrath07085852003-10-09 07:28:22 +0000677 ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
678 (val[WHATS_VALID] & OK_W64_70)) )
679 {
Eric Andersen3443bd72003-07-22 07:30:36 +0000680 like_std = 2;
681 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000682
Rob Landleya3e4f382006-04-29 16:06:31 +0000683 if (!std)
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000684 printf("\n\tLikely used: %u\n", like_std);
Rob Landleya3e4f382006-04-29 16:06:31 +0000685 else if (like_std > std)
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000686 printf("& some of %u\n", like_std);
Glenn L McGrath07085852003-10-09 07:28:22 +0000687 else
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000688 puts("");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000689 } else {
Eric Andersen3443bd72003-07-22 07:30:36 +0000690 /* TBD: do CDROM stuff more thoroughly. For now... */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000691 kk = 0;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000692 if (val[CDR_MINOR] == 9) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000693 kk = 1;
694 printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
695 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000696 if (val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000697 kk = 1;
698 printf("\n\tSupported: CD-ROM ATAPI");
699 jj = val[CDR_MAJOR] >> 1;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000700 for (ii = 1; ii < 15; ii++) {
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000701 if (jj & 0x0001) printf("-%u ", ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000702 jj >>= 1;
703 }
704 }
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000705 printf("%s\n", kk ? "" : "\n\tLikely used CD-ROM ATAPI-1");
Eric Andersen3443bd72003-07-22 07:30:36 +0000706 /* the cdrom stuff is more like ATA-2 than anything else, so: */
707 like_std = 2;
708 }
709
Rob Landleya3e4f382006-04-29 16:06:31 +0000710 if (min_std == 0xffff)
Glenn L McGrath07085852003-10-09 07:28:22 +0000711 min_std = like_std > 4 ? like_std - 3 : 1;
Eric Andersen3443bd72003-07-22 07:30:36 +0000712
713 printf("Configuration:\n");
714 /* more info from the general configuration word */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000715 if ((eqpt != CDROM) && (like_std == 1)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000716 jj = val[GEN_CONFIG] >> 1;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000717 for (ii = 1; ii < 15; ii++) {
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000718 if (jj & 0x0001)
719 printf("\t%s\n", ata1_cfg_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000720 jj >>=1;
721 }
722 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000723 if (dev == ATAPI_DEV) {
Rob Landleyadde7982006-05-16 15:32:30 +0000724 if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_3MS_VAL)
725 strng = "3ms";
726 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_INTR_VAL)
727 strng = "<=10ms with INTRQ";
728 else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_50US_VAL)
729 strng ="50us";
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000730 else
Rob Landleyadde7982006-05-16 15:32:30 +0000731 strng = "Unknown";
732 printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */
733
734 if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_12_VAL)
735 strng = "12 bytes";
736 else if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_16_VAL)
737 strng = "16 bytes";
738 else
739 strng = "Unknown";
740 puts(strng);
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000741 } else {
Glenn L McGrath07085852003-10-09 07:28:22 +0000742 /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
Eric Andersen3443bd72003-07-22 07:30:36 +0000743 ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
744 mm = 0; bbbig = 0;
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000745 if ((ll > 0x00FBFC10) && (!val[LCYLS]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000746 printf("\tCHS addressing not supported\n");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000747 else {
Eric Andersen3443bd72003-07-22 07:30:36 +0000748 jj = val[WHATS_VALID] & OK_W54_58;
Glenn L McGrath07085852003-10-09 07:28:22 +0000749 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",
750 val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
751
Rob Landleyadde7982006-05-16 15:32:30 +0000752 if ((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES]))
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000753 printf("\tbytes/track: %u\tbytes/sector: %u\n", val[TRACK_BYTES], val[SECT_BYTES]);
Glenn L McGrath07085852003-10-09 07:28:22 +0000754
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000755 if (jj) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000756 mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000757 if (like_std < 3) {
758 /* check Endian of capacity bytes */
Eric Andersen3443bd72003-07-22 07:30:36 +0000759 nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
760 oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
Rob Landleya3e4f382006-04-29 16:06:31 +0000761 if (abs(mm - nn) > abs(oo - nn))
Eric Andersen3443bd72003-07-22 07:30:36 +0000762 mm = oo;
763 }
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000764 printf("\tCHS current addressable sectors:%11u\n", mm);
Glenn L McGrath07085852003-10-09 07:28:22 +0000765 }
Eric Andersen3443bd72003-07-22 07:30:36 +0000766 }
767 /* LBA addressing */
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000768 printf("\tLBA user addressable sectors:%11u\n", ll);
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000769 if (((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
770 && (val[CMDS_SUPP_1] & SUPPORT_48_BIT)
771 ) {
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +0000772 bbbig = (uint64_t)val[LBA_64_MSB] << 48 |
773 (uint64_t)val[LBA_48_MSB] << 32 |
774 (uint64_t)val[LBA_MID] << 16 |
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000775 val[LBA_LSB];
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000776 printf("\tLBA48 user addressable sectors:%11"PRIu64"\n", bbbig);
Eric Andersen3443bd72003-07-22 07:30:36 +0000777 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000778
779 if (!bbbig)
Rob Landley2e2d7522006-04-29 15:23:33 +0000780 bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000781 printf("\tdevice size with M = 1024*1024: %11"PRIu64" MBytes\n", bbbig>>11);
782 bbbig = (bbbig << 9) / 1000000;
783 printf("\tdevice size with M = 1000*1000: %11"PRIu64" MBytes ", bbbig);
Glenn L McGrath07085852003-10-09 07:28:22 +0000784
Rob Landleyadde7982006-05-16 15:32:30 +0000785 if (bbbig > 1000)
Rob Landley81dab2c2006-05-28 01:56:08 +0000786 printf("(%"PRIu64" GB)\n", bbbig/1000);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000787 else
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000788 puts("");
Eric Andersen3443bd72003-07-22 07:30:36 +0000789 }
790
791 /* hw support of commands (capabilities) */
Glenn L McGrath07085852003-10-09 07:28:22 +0000792 printf("Capabilities:\n\t");
793
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000794 if (dev == ATAPI_DEV) {
Rob Landleyadde7982006-05-16 15:32:30 +0000795 if (eqpt != CDROM && (val[CAPAB_0] & CMD_Q_SUP)) printf("Cmd queuing, ");
796 if (val[CAPAB_0] & OVLP_SUP) printf("Cmd overlap, ");
Eric Andersen3443bd72003-07-22 07:30:36 +0000797 }
Rob Landleyadde7982006-05-16 15:32:30 +0000798 if (val[CAPAB_0] & LBA_SUP) printf("LBA, ");
Glenn L McGrath07085852003-10-09 07:28:22 +0000799
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000800 if (like_std != 1) {
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000801 printf("IORDY%s(can%s be disabled)\n",
802 !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "",
Rob Landleyadde7982006-05-16 15:32:30 +0000803 (val[CAPAB_0] & IORDY_OFF) ? "" :"not");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000804 } else
Glenn L McGrath07085852003-10-09 07:28:22 +0000805 printf("no IORDY\n");
806
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000807 if ((like_std == 1) && val[BUF_TYPE]) {
Rob Landleyadde7982006-05-16 15:32:30 +0000808 printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE],
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000809 (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector",
Rob Landleyadde7982006-05-16 15:32:30 +0000810 (val[BUF_TYPE] > 2) ? " with read caching ability" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +0000811 }
Rob Landleyadde7982006-05-16 15:32:30 +0000812
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000813 if ((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1))) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000814 printf("\tBuffer size: %.1fkB\n", (float)val[BUFFER__SIZE]/2);
Eric Andersen3443bd72003-07-22 07:30:36 +0000815 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000816 if ((min_std < 4) && (val[RW_LONG])) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000817 printf("\tbytes avail on r/w long: %u\n", val[RW_LONG]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000818 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000819 if ((eqpt != CDROM) && (like_std > 3)) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000820 printf("\tQueue depth: %u\n", (val[QUEUE_DEPTH] & DEPTH_BITS) + 1);
Eric Andersen3443bd72003-07-22 07:30:36 +0000821 }
Glenn L McGrath07085852003-10-09 07:28:22 +0000822
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000823 if (dev == ATA_DEV) {
Rob Landleya3e4f382006-04-29 16:06:31 +0000824 if (like_std == 1)
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000825 printf("\tCan%s perform double-word IO\n", (!val[DWORD_IO]) ? "not" : "");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000826 else {
Rob Landleyadde7982006-05-16 15:32:30 +0000827 printf("\tStandby timer values: spec'd by %s", (val[CAPAB_0] & STD_STBY) ? "Standard" : "Vendor");
Rob Landleya3e4f382006-04-29 16:06:31 +0000828 if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000829 printf(", %s device specific minimum\n", (val[CAPAB_1] & MIN_STANDBY_TIMER) ? "with" : "no");
Glenn L McGrath07085852003-10-09 07:28:22 +0000830 else
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000831 puts("");
Eric Andersen3443bd72003-07-22 07:30:36 +0000832 }
833 printf("\tR/W multiple sector transfer: ");
Rob Landleya3e4f382006-04-29 16:06:31 +0000834 if ((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
Eric Andersen3443bd72003-07-22 07:30:36 +0000835 printf("not supported\n");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000836 else {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000837 printf("Max = %u\tCurrent = ", val[SECTOR_XFER_MAX] & SECTOR_XFER);
Rob Landleyadde7982006-05-16 15:32:30 +0000838 if (val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
839 printf("%u\n", val[SECTOR_XFER_CUR] & SECTOR_XFER);
840 else
841 printf("?\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000842 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000843 if ((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000844 /* We print out elsewhere whether the APM feature is enabled or
845 not. If it's not enabled, let's not repeat the info; just print
846 nothing here. */
Glenn L McGrath07085852003-10-09 07:28:22 +0000847 printf("\tAdvancedPM level: ");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000848 if ((val[ADV_PWR] & 0xFF00) == 0x4000) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000849 uint8_t apm_level = val[ADV_PWR] & 0x00FF;
Eric Andersen3443bd72003-07-22 07:30:36 +0000850 printf("%u (0x%x)\n", apm_level, apm_level);
Glenn L McGrath07085852003-10-09 07:28:22 +0000851 }
852 else
Eric Andersen3443bd72003-07-22 07:30:36 +0000853 printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000854 }
Rob Landleyadde7982006-05-16 15:32:30 +0000855 if (like_std > 5 && val[ACOUSTIC]) {
856 printf("\tRecommended acoustic management value: %u, current value: %u\n",
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000857 (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
Eric Andersen3443bd72003-07-22 07:30:36 +0000858 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000859 } else {
Glenn L McGrath07085852003-10-09 07:28:22 +0000860 /* ATAPI */
Rob Landleyadde7982006-05-16 15:32:30 +0000861 if (eqpt != CDROM && (val[CAPAB_0] & SWRST_REQ))
862 printf("\tATA sw reset required\n");
Glenn L McGrath07085852003-10-09 07:28:22 +0000863
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000864 if (val[PKT_REL] || val[SVC_NBSY]) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000865 printf("\tOverlap support:");
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000866 if (val[PKT_REL]) printf(" %uus to release bus.", val[PKT_REL]);
867 if (val[SVC_NBSY]) printf(" %uus to clear BSY after SERVICE cmd.", val[SVC_NBSY]);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000868 puts("");
Eric Andersen3443bd72003-07-22 07:30:36 +0000869 }
870 }
871
872 /* DMA stuff. Check that only one DMA mode is selected. */
873 printf("\tDMA: ");
Rob Landleya3e4f382006-04-29 16:06:31 +0000874 if (!(val[CAPAB_0] & DMA_SUP))
Eric Andersen3443bd72003-07-22 07:30:36 +0000875 printf("not supported\n");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000876 else {
Rob Landleyadde7982006-05-16 15:32:30 +0000877 if (val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000878 printf(" sdma%u\n", (val[DMA_MODE] & MODE) >> 8);
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000879 if (val[SINGLE_DMA]) {
Glenn L McGrath07085852003-10-09 07:28:22 +0000880 jj = val[SINGLE_DMA];
881 kk = val[SINGLE_DMA] >> 8;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000882 err_dma += mode_loop(jj, kk, 's', &have_mode);
Eric Andersen3443bd72003-07-22 07:30:36 +0000883 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000884 if (val[MULTI_DMA]) {
Glenn L McGrath07085852003-10-09 07:28:22 +0000885 jj = val[MULTI_DMA];
886 kk = val[MULTI_DMA] >> 8;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000887 err_dma += mode_loop(jj, kk, 'm', &have_mode);
Eric Andersen3443bd72003-07-22 07:30:36 +0000888 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000889 if ((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) {
Glenn L McGrath07085852003-10-09 07:28:22 +0000890 jj = val[ULTRA_DMA];
891 kk = val[ULTRA_DMA] >> 8;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000892 err_dma += mode_loop(jj, kk, 'u', &have_mode);
Eric Andersen3443bd72003-07-22 07:30:36 +0000893 }
Rob Landleyadde7982006-05-16 15:32:30 +0000894 if (err_dma || !have_mode) printf("(?)");
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000895 puts("");
Eric Andersen3443bd72003-07-22 07:30:36 +0000896
Rob Landleyadde7982006-05-16 15:32:30 +0000897 if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
898 printf("\t\tInterleaved DMA support\n");
Eric Andersen3443bd72003-07-22 07:30:36 +0000899
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000900 if ((val[WHATS_VALID] & OK_W64_70)
901 && (val[DMA_TIME_MIN] || val[DMA_TIME_NORM])
902 ) {
Rob Landleyadde7982006-05-16 15:32:30 +0000903 printf("\t\tCycle time:");
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000904 if (val[DMA_TIME_MIN]) printf(" min=%uns", val[DMA_TIME_MIN]);
905 if (val[DMA_TIME_NORM]) printf(" recommended=%uns", val[DMA_TIME_NORM]);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000906 puts("");
Eric Andersen3443bd72003-07-22 07:30:36 +0000907 }
908 }
909
910 /* Programmed IO stuff */
911 printf("\tPIO: ");
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000912 /* If a drive supports mode n (e.g. 3), it also supports all modes less
Eric Andersen3443bd72003-07-22 07:30:36 +0000913 * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000914 if ((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP)) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000915 jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
Denis Vlasenko6963eb52007-05-22 21:46:11 +0000916 for (ii = 0; ii <= PIO_MODE_MAX; ii++) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000917 if (jj & 0x0001) printf("pio%d ", ii);
Eric Andersen3443bd72003-07-22 07:30:36 +0000918 jj >>=1;
919 }
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000920 puts("");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000921 } else if (((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE)) {
Rob Landleya3e4f382006-04-29 16:06:31 +0000922 for (ii = 0; ii <= val[PIO_MODE]>>8; ii++)
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000923 printf("pio%d ", ii);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000924 puts("");
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000925 } else
Glenn L McGrath07085852003-10-09 07:28:22 +0000926 printf("unknown\n");
927
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000928 if (val[WHATS_VALID] & OK_W64_70) {
929 if (val[PIO_NO_FLOW] || val[PIO_FLOW]) {
Rob Landleyadde7982006-05-16 15:32:30 +0000930 printf("\t\tCycle time:");
931 if (val[PIO_NO_FLOW]) printf(" no flow control=%uns", val[PIO_NO_FLOW]);
932 if (val[PIO_FLOW]) printf(" IORDY flow control=%uns", val[PIO_FLOW]);
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000933 puts("");
Eric Andersen3443bd72003-07-22 07:30:36 +0000934 }
935 }
936
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000937 if ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000938 printf("Commands/features:\n\tEnabled\tSupported:\n");
939 jj = val[CMDS_SUPP_0];
940 kk = val[CMDS_EN_0];
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000941 for (ii = 0; ii < NUM_CMD_FEAT_STR; ii++) {
942 if ((jj & 0x8000) && (*cmd_feat_str[ii] != '\0')) {
Rob Landleyadde7982006-05-16 15:32:30 +0000943 printf("\t%s\t%s\n", (kk & 0x8000) ? " *" : "", cmd_feat_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000944 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000945 jj <<= 1;
946 kk <<= 1;
947 if (ii % 16 == 15) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000948 jj = val[CMDS_SUPP_0+1+(ii/16)];
949 kk = val[CMDS_EN_0+1+(ii/16)];
950 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000951 if (ii == 31) {
Rob Landleya3e4f382006-04-29 16:06:31 +0000952 if ((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
Glenn L McGrath07085852003-10-09 07:28:22 +0000953 ii +=16;
Eric Andersen3443bd72003-07-22 07:30:36 +0000954 }
955 }
956 }
Rob Landleyadde7982006-05-16 15:32:30 +0000957 /* Removable Media Status Notification feature set */
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000958 if ((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
Rob Landleyadde7982006-05-16 15:32:30 +0000959 printf("\t%s supported\n", cmd_feat_str[27]);
Glenn L McGrath07085852003-10-09 07:28:22 +0000960
Eric Andersen3443bd72003-07-22 07:30:36 +0000961 /* security */
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000962 if ((eqpt != CDROM) && (like_std > 3)
963 && (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME])
964 ) {
Rob Landleyade7f952006-05-25 18:53:06 +0000965 printf("Security:\n");
Rob Landleyadde7982006-05-16 15:32:30 +0000966 if (val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
Denis Vlasenkof1ba7492007-03-28 00:14:01 +0000967 printf("\tMaster password revision code = %u\n", val[PSWD_CODE]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000968 jj = val[SECU_STATUS];
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000969 if (jj) {
970 for (ii = 0; ii < NUM_SECU_STR; ii++) {
Rob Landleyadde7982006-05-16 15:32:30 +0000971 printf("\t%s\t%s\n", (!(jj & 0x0001)) ? "not" : "", secu_str[ii]);
Eric Andersen3443bd72003-07-22 07:30:36 +0000972 jj >>=1;
973 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000974 if (val[SECU_STATUS] & SECU_ENABLED) {
Rob Landleyadde7982006-05-16 15:32:30 +0000975 printf("\tSecurity level %s\n", (val[SECU_STATUS] & SECU_LEVEL) ? "maximum" : "high");
Eric Andersen3443bd72003-07-22 07:30:36 +0000976 }
977 }
978 jj = val[ERASE_TIME] & ERASE_BITS;
979 kk = val[ENH_ERASE_TIME] & ERASE_BITS;
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000980 if (jj || kk) {
Eric Andersen3443bd72003-07-22 07:30:36 +0000981 printf("\t");
Rob Landleyade7f952006-05-25 18:53:06 +0000982 if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, "");
983 if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED ");
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000984 puts("");
Eric Andersen3443bd72003-07-22 07:30:36 +0000985 }
986 }
987
988 /* reset result */
Rob Landleyadde7982006-05-16 15:32:30 +0000989 jj = val[HWRST_RSLT];
Denis Vlasenko3bf00202007-02-18 13:36:04 +0000990 if ((jj & VALID) == VALID_VAL) {
Rob Landleyadde7982006-05-16 15:32:30 +0000991 if (!(oo = (jj & RST0)))
992 jj >>= 8;
Rob Landleya3e4f382006-04-29 16:06:31 +0000993 if ((jj & DEV_DET) == JUMPER_VAL)
Rob Landleyadde7982006-05-16 15:32:30 +0000994 strng = " determined by the jumper";
Rob Landleya3e4f382006-04-29 16:06:31 +0000995 else if ((jj & DEV_DET) == CSEL_VAL)
Rob Landleyadde7982006-05-16 15:32:30 +0000996 strng = " determined by CSEL";
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000997 else
Rob Landleyadde7982006-05-16 15:32:30 +0000998 strng = "";
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000999 printf("HW reset results:\n\tCBLID- %s Vih\n\tDevice num = %i%s\n",
Rob Landleyadde7982006-05-16 15:32:30 +00001000 (val[HWRST_RSLT] & CBLID) ? "above" : "below", !(oo), strng);
Eric Andersen3443bd72003-07-22 07:30:36 +00001001 }
1002
1003 /* more stuff from std 5 */
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001004 if ((like_std > 4) && (eqpt != CDROM)) {
1005 if (val[CFA_PWR_MODE] & VALID_W160) {
Rob Landleyadde7982006-05-16 15:32:30 +00001006 printf("CFA power mode 1:\n\t%s%s\n", (val[CFA_PWR_MODE] & PWR_MODE_OFF) ? "disabled" : "enabled",
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001007 (val[CFA_PWR_MODE] & PWR_MODE_REQ) ? " and required by some commands" : "");
Glenn L McGrath07085852003-10-09 07:28:22 +00001008
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001009 if (val[CFA_PWR_MODE] & MAX_AMPS)
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001010 printf("\tMaximum current = %uma\n", val[CFA_PWR_MODE] & MAX_AMPS);
Eric Andersen3443bd72003-07-22 07:30:36 +00001011 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001012 if ((val[INTEGRITY] & SIG) == SIG_VAL) {
Rob Landleyadde7982006-05-16 15:32:30 +00001013 printf("Checksum: %scorrect\n", chksum ? "in" : "");
Eric Andersen3443bd72003-07-22 07:30:36 +00001014 }
1015 }
1016
Rob Landleyadde7982006-05-16 15:32:30 +00001017 exit(EXIT_SUCCESS);
Eric Andersen3443bd72003-07-22 07:30:36 +00001018}
1019#endif
1020
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001021static smallint get_identity, get_geom;
1022static smallint do_flush;
1023static smallint do_ctimings, do_timings;
1024static smallint reread_partn;
1025
1026static smallint set_piomode, noisy_piomode;
1027static smallint set_readahead, get_readahead;
1028static smallint set_readonly, get_readonly;
1029static smallint set_unmask, get_unmask;
1030static smallint set_mult, get_mult;
1031static smallint set_dma_q, get_dma_q;
1032static smallint set_nowerr, get_nowerr;
1033static smallint set_keep, get_keep;
1034static smallint set_io32bit, get_io32bit;
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001035static int piomode;
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001036static unsigned long Xreadahead;
1037static unsigned long readonly;
1038static unsigned long unmask;
1039static unsigned long mult;
1040static unsigned long dma_q;
1041static unsigned long nowerr;
1042static unsigned long keep;
1043static unsigned long io32bit;
1044#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
1045static unsigned long dma;
1046static smallint set_dma, get_dma;
1047#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00001048#ifdef HDIO_DRIVE_CMD
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001049static smallint set_xfermode, get_xfermode;
1050static smallint set_dkeep, get_dkeep;
1051static smallint set_standby, get_standby;
1052static smallint set_lookahead, get_lookahead;
1053static smallint set_prefetch, get_prefetch;
1054static smallint set_defects, get_defects;
1055static smallint set_wcache, get_wcache;
1056static smallint set_doorlock, get_doorlock;
1057static smallint set_seagate, get_seagate;
1058static smallint set_standbynow, get_standbynow;
1059static smallint set_sleepnow, get_sleepnow;
1060static smallint get_powermode;
1061static smallint set_apmmode, get_apmmode;
"Vladimir N. Oleynik"b4b6d262005-10-15 14:10:36 +00001062static int xfermode_requested;
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001063static unsigned long dkeep;
1064static unsigned long standby_requested;
1065static unsigned long lookahead;
1066static unsigned long prefetch;
1067static unsigned long defects;
1068static unsigned long wcache;
1069static unsigned long doorlock;
1070static unsigned long apmmode;
Eric Andersen3443bd72003-07-22 07:30:36 +00001071#endif
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001072USE_FEATURE_HDPARM_GET_IDENTITY( static smallint get_IDentity;)
1073USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( static smallint set_busstate, get_busstate;)
1074USE_FEATURE_HDPARM_HDIO_DRIVE_RESET( static smallint perform_reset;)
1075USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( static smallint perform_tristate;)
1076USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(static smallint unregister_hwif;)
1077USE_FEATURE_HDPARM_HDIO_SCAN_HWIF( static smallint scan_hwif;)
1078USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( static unsigned long busstate;)
1079USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( static unsigned long tristate;)
1080USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(static unsigned long hwif;)
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001081#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
Rob Landley20deab02006-05-07 23:34:15 +00001082static unsigned long hwif_data;
1083static unsigned long hwif_ctrl;
1084static unsigned long hwif_irq;
Eric Andersen3443bd72003-07-22 07:30:36 +00001085#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00001086
1087// Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
1088// then the HDIO_GET_IDENTITY only returned 142 bytes.
1089// Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
1090// and HDIO_GET_IDENTITY returns 512 bytes. But the latest
1091// 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
1092// (which they should, but they should just return -EINVAL).
1093//
1094// So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
1095// On a really old system, it will not, and we will be confused.
1096// Too bad, really.
1097
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001098#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001099static const char *const cfg_str[] = {
1100 "", "HardSect", "SoftSect", "NotMFM",
Rob Landleyadde7982006-05-16 15:32:30 +00001101 "HdSw>15uSec", "SpinMotCtl", "Fixed", "Removeable",
1102 "DTR<=5Mbs", "DTR>5Mbs", "DTR>10Mbs", "RotSpdTol>.5%",
1103 "dStbOff", "TrkOff", "FmtGapReq", "nonMagnetic"
Eric Andersen3443bd72003-07-22 07:30:36 +00001104};
1105
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001106static const char *const BuffType[] = {
1107 "Unknown", "1Sect", "DualPort", "DualPortCache"
1108};
Eric Andersen3443bd72003-07-22 07:30:36 +00001109
Rob Landleya3e4f382006-04-29 16:06:31 +00001110static void dump_identity(const struct hd_driveid *id)
Eric Andersen3443bd72003-07-22 07:30:36 +00001111{
1112 int i;
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001113 const unsigned short int *id_regs = (const void*) id;
Rob Landley0a7c8ef2006-02-22 17:01:00 +00001114
Glenn L McGrath07085852003-10-09 07:28:22 +00001115 printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
1116 id->model, id->fw_rev, id->serial_no);
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001117 for (i = 0; i <= 15; i++) {
Rob Landleyade7f952006-05-25 18:53:06 +00001118 if (id->config & (1<<i))
1119 printf(" %s", cfg_str[i]);
Rob Landleyadde7982006-05-16 15:32:30 +00001120 }
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001121 printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n"
Rob Landleyadde7982006-05-16 15:32:30 +00001122 " BuffType=(%u) %s, BuffSize=%ukB, MaxMultSect=%u",
Glenn L McGrath07085852003-10-09 07:28:22 +00001123 id->cyls, id->heads, id->sectors, id->track_bytes,
Rob Landleyadde7982006-05-16 15:32:30 +00001124 id->sector_bytes, id->ecc_bytes,
1125 id->buf_type, BuffType[(id->buf_type > 3) ? 0 : id->buf_type],
1126 id->buf_size/2, id->max_multsect);
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001127 if (id->max_multsect) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001128 printf(", MultSect=");
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001129 if (!(id->multsect_valid & 1))
Eric Andersen3443bd72003-07-22 07:30:36 +00001130 printf("?%u?", id->multsect);
1131 else if (id->multsect)
1132 printf("%u", id->multsect);
1133 else
1134 printf("off");
1135 }
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +00001136 puts("");
Rob Landleyade7f952006-05-25 18:53:06 +00001137
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001138 if (!(id->field_valid & 1))
Rob Landleyade7f952006-05-25 18:53:06 +00001139 printf(" (maybe):");
1140
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001141 printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s", id->cur_cyls, id->cur_heads,
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001142 id->cur_sectors,
1143 (BB_BIG_ENDIAN) ?
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001144 (unsigned long)(id->cur_capacity0 << 16) | id->cur_capacity1 :
1145 (unsigned long)(id->cur_capacity1 << 16) | id->cur_capacity0,
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001146 ((id->capability&2) == 0) ? "no" : "yes");
Rob Landleyade7f952006-05-25 18:53:06 +00001147
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001148 if (id->capability & 2)
Rob Landleyade7f952006-05-25 18:53:06 +00001149 printf(", LBAsects=%u", id->lba_capacity);
1150
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001151 printf("\n IORDY=%s", (id->capability & 8) ? (id->capability & 4) ? "on/off" : "yes" : "no");
Rob Landleyade7f952006-05-25 18:53:06 +00001152
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001153 if (((id->capability & 8) || (id->field_valid & 2)) && (id->field_valid & 2))
Rob Landleyade7f952006-05-25 18:53:06 +00001154 printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001155
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001156 if ((id->capability & 1) && (id->field_valid & 2))
Rob Landleyade7f952006-05-25 18:53:06 +00001157 printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
1158
1159 printf("\n PIO modes: ");
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001160 if (id->tPIO <= 5) {
Rob Landleyade7f952006-05-25 18:53:06 +00001161 printf("pio0 ");
1162 if (id->tPIO >= 1) printf("pio1 ");
1163 if (id->tPIO >= 2) printf("pio2 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001164 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001165 if (id->field_valid & 2) {
Rob Landleyade7f952006-05-25 18:53:06 +00001166 if (id->eide_pio_modes & 1) printf("pio3 ");
1167 if (id->eide_pio_modes & 2) printf("pio4 ");
1168 if (id->eide_pio_modes &~3) printf("pio? ");
1169 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001170 if (id->capability & 1) {
1171 if (id->dma_1word | id->dma_mword) {
Rob Landleyade7f952006-05-25 18:53:06 +00001172 printf("\n DMA modes: ");
1173 if (id->dma_1word & 0x100) printf("*");
1174 if (id->dma_1word & 1) printf("sdma0 ");
1175 if (id->dma_1word & 0x200) printf("*");
1176 if (id->dma_1word & 2) printf("sdma1 ");
1177 if (id->dma_1word & 0x400) printf("*");
1178 if (id->dma_1word & 4) printf("sdma2 ");
1179 if (id->dma_1word & 0xf800) printf("*");
1180 if (id->dma_1word & 0xf8) printf("sdma? ");
1181 if (id->dma_mword & 0x100) printf("*");
1182 if (id->dma_mword & 1) printf("mdma0 ");
1183 if (id->dma_mword & 0x200) printf("*");
1184 if (id->dma_mword & 2) printf("mdma1 ");
1185 if (id->dma_mword & 0x400) printf("*");
1186 if (id->dma_mword & 4) printf("mdma2 ");
1187 if (id->dma_mword & 0xf800) printf("*");
1188 if (id->dma_mword & 0xf8) printf("mdma? ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001189 }
1190 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001191 if (((id->capability & 8) || (id->field_valid & 2)) && id->field_valid & 4) {
Rob Landleyade7f952006-05-25 18:53:06 +00001192 printf("\n UDMA modes: ");
1193 if (id->dma_ultra & 0x100) printf("*");
1194 if (id->dma_ultra & 0x001) printf("udma0 ");
1195 if (id->dma_ultra & 0x200) printf("*");
1196 if (id->dma_ultra & 0x002) printf("udma1 ");
1197 if (id->dma_ultra & 0x400) printf("*");
1198 if (id->dma_ultra & 0x004) printf("udma2 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001199#ifdef __NEW_HD_DRIVE_ID
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001200 if (id->hw_config & 0x2000) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001201#else /* !__NEW_HD_DRIVE_ID */
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001202 if (id->word93 & 0x2000) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001203#endif /* __NEW_HD_DRIVE_ID */
Rob Landleyade7f952006-05-25 18:53:06 +00001204 if (id->dma_ultra & 0x0800) printf("*");
1205 if (id->dma_ultra & 0x0008) printf("udma3 ");
1206 if (id->dma_ultra & 0x1000) printf("*");
1207 if (id->dma_ultra & 0x0010) printf("udma4 ");
1208 if (id->dma_ultra & 0x2000) printf("*");
1209 if (id->dma_ultra & 0x0020) printf("udma5 ");
1210 if (id->dma_ultra & 0x4000) printf("*");
1211 if (id->dma_ultra & 0x0040) printf("udma6 ");
1212 if (id->dma_ultra & 0x8000) printf("*");
1213 if (id->dma_ultra & 0x0080) printf("udma7 ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001214 }
1215 }
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001216 printf("\n AdvancedPM=%s", (!(id_regs[83] & 8)) ? "no" : "yes");
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001217 if (id_regs[83] & 8) {
1218 if (!(id_regs[86] & 8))
Glenn L McGrath07085852003-10-09 07:28:22 +00001219 printf(": disabled (255)");
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001220 else if ((id_regs[91] & 0xFF00) != 0x4000)
Glenn L McGrath07085852003-10-09 07:28:22 +00001221 printf(": unknown setting");
1222 else
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001223 printf(": mode=0x%02X (%u)", id_regs[91] & 0xFF, id_regs[91] & 0xFF);
Glenn L McGrath07085852003-10-09 07:28:22 +00001224 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001225 if (id_regs[82] & 0x20)
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001226 printf(" WriteCache=%s", (id_regs[85] & 0x20) ? "enabled" : "disabled");
Glenn L McGrath07085852003-10-09 07:28:22 +00001227#ifdef __NEW_HD_DRIVE_ID
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001228 if ((id->minor_rev_num && id->minor_rev_num <= 31)
1229 || (id->major_rev_num && id->minor_rev_num <= 31)
1230 ) {
Rob Landleyadde7982006-05-16 15:32:30 +00001231 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 +00001232 if (id->major_rev_num != 0x0000 && /* NOVAL_0 */
1233 id->major_rev_num != 0xFFFF) { /* NOVAL_1 */
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001234 for (i = 0; i <= 15; i++) {
Rob Landleyadde7982006-05-16 15:32:30 +00001235 if (id->major_rev_num & (1<<i))
1236 printf(" ATA/ATAPI-%u", i);
1237 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001238 }
1239 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001240#endif /* __NEW_HD_DRIVE_ID */
Rob Landleyade7f952006-05-25 18:53:06 +00001241 printf("\n\n * current active mode\n\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001242}
1243#endif
1244
Rob Landleya3e4f382006-04-29 16:06:31 +00001245static void flush_buffer_cache(int fd)
Eric Andersen3443bd72003-07-22 07:30:36 +00001246{
Rob Landleya3e4f382006-04-29 16:06:31 +00001247 fsync(fd); /* flush buffers */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001248 ioctl_or_warn(fd, BLKFLSBUF, NULL); /* do it again, big time */
Eric Andersen3443bd72003-07-22 07:30:36 +00001249#ifdef HDIO_DRIVE_CMD
Glenn L McGrath07085852003-10-09 07:28:22 +00001250 sleep(1);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001251 if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) { /* await completion */
1252 if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
1253 bb_perror_msg("HDIO_DRIVE_CMD");
1254 else
1255 bb_perror_msg("ioctl %#x failed", HDIO_DRIVE_CMD);
1256 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001257#endif
1258}
1259
Rob Landleya3e4f382006-04-29 16:06:31 +00001260static int seek_to_zero(int fd)
Eric Andersen3443bd72003-07-22 07:30:36 +00001261{
Glenn L McGrath07085852003-10-09 07:28:22 +00001262 if (lseek(fd, (off_t) 0, SEEK_SET))
Eric Andersen3443bd72003-07-22 07:30:36 +00001263 return 1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001264 return 0;
1265}
1266
Rob Landleya3e4f382006-04-29 16:06:31 +00001267static int read_big_block(int fd, char *buf)
Eric Andersen3443bd72003-07-22 07:30:36 +00001268{
Rob Landleyadde7982006-05-16 15:32:30 +00001269 int i;
1270
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001271 i = read(fd, buf, TIMING_BUF_BYTES);
1272 if (i != TIMING_BUF_BYTES) {
Rob Landleyadde7982006-05-16 15:32:30 +00001273 bb_error_msg("read(%d bytes) failed (rc=%d)", TIMING_BUF_BYTES, i);
Eric Andersen3443bd72003-07-22 07:30:36 +00001274 return 1;
1275 }
1276 /* access all sectors of buf to ensure the read fully completed */
1277 for (i = 0; i < TIMING_BUF_BYTES; i += 512)
1278 buf[i] &= 1;
1279 return 0;
1280}
1281
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001282static int do_blkgetsize(int fd, unsigned long long *blksize64)
Rob Landleyadde7982006-05-16 15:32:30 +00001283{
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001284 int rc;
1285 unsigned blksize32 = 0;
Rob Landleyadde7982006-05-16 15:32:30 +00001286
1287 if (0 == ioctl(fd, BLKGETSIZE64, blksize64)) { // returns bytes
1288 *blksize64 /= 512;
1289 return 0;
1290 }
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001291 rc = ioctl_or_warn(fd, BLKGETSIZE, &blksize32); // returns sectors
Rob Landleyadde7982006-05-16 15:32:30 +00001292 *blksize64 = blksize32;
1293 return rc;
1294}
Eric Andersen50af12d2003-08-06 08:47:59 +00001295
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001296static void print_timing(unsigned t, double e)
1297{
1298 if (t >= e) /* more than 1MB/s */
1299 printf("%4d MB in %.2f seconds = %.2f %cB/sec\n", t, e, t / e, 'M');
1300 else
1301 printf("%4d MB in %.2f seconds = %.2f %cB/sec\n", t, e, t / e * 1024, 'k');
1302}
1303
Rob Landleya3e4f382006-04-29 16:06:31 +00001304static void do_time(int flag, int fd)
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001305/* flag = 0 time_cache, 1 time_device */
Eric Andersen3443bd72003-07-22 07:30:36 +00001306{
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001307 static const struct itimerval thousand = {{1000, 0}, {1000, 0}};
1308
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001309 struct itimerval itv;
1310 unsigned elapsed, elapsed2;
1311 unsigned max_iterations, total_MB, iterations;
Rob Landleyadde7982006-05-16 15:32:30 +00001312 unsigned long long blksize;
Rob Landley4ae2f512006-05-19 17:24:26 +00001313 RESERVE_CONFIG_BUFFER(buf, TIMING_BUF_BYTES);
1314
1315 if (mlock(buf, TIMING_BUF_BYTES)) {
1316 bb_perror_msg("mlock");
1317 goto quit2;
1318 }
Rob Landleyadde7982006-05-16 15:32:30 +00001319
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001320 max_iterations = 1024;
Rob Landleyadde7982006-05-16 15:32:30 +00001321 if (0 == do_blkgetsize(fd, &blksize)) {
1322 max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB;
1323 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001324
Eric Andersen3443bd72003-07-22 07:30:36 +00001325 /* Clear out the device request queues & give them time to complete */
Rob Landleyadde7982006-05-16 15:32:30 +00001326 sync();
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001327 sleep(2);
1328 if (flag == 0) { /* Time cache */
1329 if (seek_to_zero(fd))
1330 goto quit;
1331 if (read_big_block(fd, buf))
1332 goto quit;
1333 printf(" Timing buffer-cache reads: ");
1334 } else { /* Time device */
1335 printf(" Timing buffered disk reads: ");
1336 }
1337 fflush(stdout);
1338 iterations = 0;
1339 /*
1340 * getitimer() is used rather than gettimeofday() because
1341 * it is much more consistent (on my machine, at least).
1342 */
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001343 setitimer(ITIMER_REAL, &thousand, NULL);
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001344 /* Now do the timing */
1345 do {
1346 ++iterations;
1347 if ((flag == 0) && seek_to_zero(fd))
1348 goto quit;
1349 if (read_big_block(fd, buf))
1350 goto quit;
1351 getitimer(ITIMER_REAL, &itv);
1352 elapsed = (1000 - itv.it_value.tv_sec) * 1000000
1353 - itv.it_value.tv_usec;
1354 } while (elapsed < 3000000 && iterations < max_iterations);
1355 total_MB = iterations * TIMING_BUF_MB;
1356 if (flag == 0) {
Rob Landleyadde7982006-05-16 15:32:30 +00001357 /* Now remove the lseek() and getitimer() overheads from the elapsed time */
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001358 setitimer(ITIMER_REAL, &thousand, NULL);
Rob Landleyadde7982006-05-16 15:32:30 +00001359 do {
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001360 if (seek_to_zero(fd))
Rob Landleyadde7982006-05-16 15:32:30 +00001361 goto quit;
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001362 getitimer(ITIMER_REAL, &itv);
1363 elapsed2 = (1000 - itv.it_value.tv_sec) * 1000000
1364 - itv.it_value.tv_usec;
Rob Landleyadde7982006-05-16 15:32:30 +00001365 } while (--iterations);
Rob Landleyadde7982006-05-16 15:32:30 +00001366 elapsed -= elapsed2;
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001367 total_MB *= BUFCACHE_FACTOR;
Glenn L McGrath07085852003-10-09 07:28:22 +00001368 flush_buffer_cache(fd);
Glenn L McGrath07085852003-10-09 07:28:22 +00001369 }
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001370 print_timing(total_MB, elapsed / 1000000.0);
1371 quit:
Rob Landley4ae2f512006-05-19 17:24:26 +00001372 munlock(buf, TIMING_BUF_BYTES);
Denis Vlasenko6963eb52007-05-22 21:46:11 +00001373 quit2:
Rob Landley4ae2f512006-05-19 17:24:26 +00001374 RELEASE_CONFIG_BUFFER(buf);
Eric Andersen3443bd72003-07-22 07:30:36 +00001375}
1376
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001377#if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001378static void bus_state_value(unsigned value)
Eric Andersen3443bd72003-07-22 07:30:36 +00001379{
Rob Landleyadde7982006-05-16 15:32:30 +00001380 if (value == BUSSTATE_ON)
1381 on_off(1);
1382 else if (value == BUSSTATE_OFF)
1383 on_off(0);
1384 else if (value == BUSSTATE_TRISTATE)
1385 printf(" (tristate)\n");
1386 else
1387 printf(" (unknown: %d)\n", value);
Eric Andersen3443bd72003-07-22 07:30:36 +00001388}
1389#endif
1390
1391#ifdef HDIO_DRIVE_CMD
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001392static void interpret_standby(unsigned standby)
Eric Andersen3443bd72003-07-22 07:30:36 +00001393{
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001394 unsigned t;
Rob Landleyadde7982006-05-16 15:32:30 +00001395
Rob Landley403777f2006-08-03 20:22:37 +00001396 printf(" (");
Rob Landleyadde7982006-05-16 15:32:30 +00001397 if (standby == 0)
1398 printf("off");
1399 else if (standby == 252)
1400 printf("21 minutes");
1401 else if (standby == 253)
1402 printf("vendor-specific");
1403 else if (standby == 254)
1404 printf("Reserved");
1405 else if (standby == 255)
1406 printf("21 minutes + 15 seconds");
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001407 else if (standby <= 240) {
1408 t = standby * 5;
1409 printf("%u minutes + %u seconds", t / 60, t % 60);
1410 } else if (standby <= 251) {
1411 t = (standby - 240) * 30;
1412 printf("%u hours + %u minutes", t / 60, t % 60);
1413 } else
1414 printf("illegal value");
Eric Andersen3443bd72003-07-22 07:30:36 +00001415 printf(")\n");
1416}
1417
1418struct xfermode_entry {
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001419 int val;
1420 const char *name;
Eric Andersen3443bd72003-07-22 07:30:36 +00001421};
1422
1423static const struct xfermode_entry xfermode_table[] = {
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001424 { 8, "pio0" },
1425 { 9, "pio1" },
1426 { 10, "pio2" },
1427 { 11, "pio3" },
1428 { 12, "pio4" },
1429 { 13, "pio5" },
1430 { 14, "pio6" },
1431 { 15, "pio7" },
1432 { 16, "sdma0" },
1433 { 17, "sdma1" },
1434 { 18, "sdma2" },
1435 { 19, "sdma3" },
1436 { 20, "sdma4" },
1437 { 21, "sdma5" },
1438 { 22, "sdma6" },
1439 { 23, "sdma7" },
1440 { 32, "mdma0" },
1441 { 33, "mdma1" },
1442 { 34, "mdma2" },
1443 { 35, "mdma3" },
1444 { 36, "mdma4" },
1445 { 37, "mdma5" },
1446 { 38, "mdma6" },
1447 { 39, "mdma7" },
1448 { 64, "udma0" },
1449 { 65, "udma1" },
1450 { 66, "udma2" },
1451 { 67, "udma3" },
1452 { 68, "udma4" },
1453 { 69, "udma5" },
1454 { 70, "udma6" },
1455 { 71, "udma7" },
1456 { 0, NULL }
Eric Andersen3443bd72003-07-22 07:30:36 +00001457};
1458
1459static int translate_xfermode(char * name)
1460{
Glenn L McGrath07085852003-10-09 07:28:22 +00001461 const struct xfermode_entry *tmp;
1462 char *endptr;
1463 int val = -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001464
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001465 for (tmp = xfermode_table; tmp->name != NULL; ++tmp) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001466 if (!strcmp(name, tmp->name))
1467 return tmp->val;
Glenn L McGrath07085852003-10-09 07:28:22 +00001468 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001469
Glenn L McGrath07085852003-10-09 07:28:22 +00001470 val = strtol(name, &endptr, 10);
1471 if (*endptr == '\0')
1472 return val;
1473
1474 return -1;
Eric Andersen3443bd72003-07-22 07:30:36 +00001475}
1476
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001477static void interpret_xfermode(unsigned xfermode)
Eric Andersen3443bd72003-07-22 07:30:36 +00001478{
1479 printf(" (");
Rob Landleyadde7982006-05-16 15:32:30 +00001480 if (xfermode == 0)
1481 printf("default PIO mode");
1482 else if (xfermode == 1)
1483 printf("default PIO mode, disable IORDY");
1484 else if (xfermode >= 8 && xfermode <= 15)
1485 printf("PIO flow control mode%u", xfermode-8);
1486 else if (xfermode >= 16 && xfermode <= 23)
1487 printf("singleword DMA mode%u", xfermode-16);
1488 else if (xfermode >= 32 && xfermode <= 39)
1489 printf("multiword DMA mode%u", xfermode-32);
1490 else if (xfermode >= 64 && xfermode <= 71)
1491 printf("UltraDMA mode%u", xfermode-64);
1492 else
1493 printf("Unknown");
Eric Andersen3443bd72003-07-22 07:30:36 +00001494 printf(")\n");
1495}
1496#endif /* HDIO_DRIVE_CMD */
1497
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001498static void print_flag(int flag, const char *s, unsigned long value)
Rob Landleyadde7982006-05-16 15:32:30 +00001499{
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001500 if (flag)
Rob Landleyadde7982006-05-16 15:32:30 +00001501 printf(" setting %s to %ld\n", s, value);
1502}
1503
Rob Landleya3e4f382006-04-29 16:06:31 +00001504static void process_dev(char *devname)
Eric Andersen3443bd72003-07-22 07:30:36 +00001505{
1506 int fd;
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001507 long parm, multcount;
Eric Andersen3443bd72003-07-22 07:30:36 +00001508#ifndef HDIO_DRIVE_CMD
1509 int force_operation = 0;
1510#endif
Rob Landley39cf6452006-05-05 16:52:28 +00001511 /* Please restore args[n] to these values after each ioctl
1512 except for args[2] */
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001513 unsigned char args[4] = { WIN_SETFEATURES, 0, 0, 0 };
Rob Landleyadde7982006-05-16 15:32:30 +00001514 const char *fmt = " %s\t= %2ld";
Rob Landleye5b281f2006-04-29 15:49:18 +00001515
Rob Landleyd921b2e2006-08-03 15:41:12 +00001516 fd = xopen(devname, O_RDONLY|O_NONBLOCK);
Rob Landleyade7f952006-05-25 18:53:06 +00001517 printf("\n%s:\n", devname);
Eric Andersen3443bd72003-07-22 07:30:36 +00001518
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001519 if (set_readahead) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001520 print_flag(get_readahead, "fs readahead", Xreadahead);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001521 ioctl_or_warn(fd, BLKRASET, (int *)Xreadahead);
Eric Andersen3443bd72003-07-22 07:30:36 +00001522 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001523#if ENABLE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
1524 if (unregister_hwif) {
Rob Landley20deab02006-05-07 23:34:15 +00001525 printf(" attempting to unregister hwif#%lu\n", hwif);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001526 ioctl_or_warn(fd, HDIO_UNREGISTER_HWIF, (int *)(unsigned long)hwif);
Eric Andersen3443bd72003-07-22 07:30:36 +00001527 }
1528#endif
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001529#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
1530 if (scan_hwif) {
Rob Landley20deab02006-05-07 23:34:15 +00001531 printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq);
Eric Andersen3443bd72003-07-22 07:30:36 +00001532 args[0] = hwif_data;
1533 args[1] = hwif_ctrl;
1534 args[2] = hwif_irq;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001535 ioctl_or_warn(fd, HDIO_SCAN_HWIF, args);
Rob Landley39cf6452006-05-05 16:52:28 +00001536 args[0] = WIN_SETFEATURES;
1537 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001538 }
1539#endif
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001540 if (set_piomode) {
1541 if (noisy_piomode) {
Glenn L McGrath07085852003-10-09 07:28:22 +00001542 printf(" attempting to ");
Eric Andersen3443bd72003-07-22 07:30:36 +00001543 if (piomode == 255)
Glenn L McGrath07085852003-10-09 07:28:22 +00001544 printf("auto-tune PIO mode\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001545 else if (piomode < 100)
Glenn L McGrath07085852003-10-09 07:28:22 +00001546 printf("set PIO mode to %d\n", piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001547 else if (piomode < 200)
Glenn L McGrath07085852003-10-09 07:28:22 +00001548 printf("set MDMA mode to %d\n", (piomode-100));
Eric Andersen3443bd72003-07-22 07:30:36 +00001549 else
Glenn L McGrath07085852003-10-09 07:28:22 +00001550 printf("set UDMA mode to %d\n", (piomode-200));
Eric Andersen3443bd72003-07-22 07:30:36 +00001551 }
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001552 ioctl_or_warn(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode);
Eric Andersen3443bd72003-07-22 07:30:36 +00001553 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001554 if (set_io32bit) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001555 print_flag(get_io32bit, "32-bit IO_support flag", io32bit);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001556 ioctl_or_warn(fd, HDIO_SET_32BIT, (int *)io32bit);
Eric Andersen3443bd72003-07-22 07:30:36 +00001557 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001558 if (set_mult) {
Rob Landleyadde7982006-05-16 15:32:30 +00001559 print_flag(get_mult, "multcount", mult);
Rob Landleyade7f952006-05-25 18:53:06 +00001560#ifdef HDIO_DRIVE_CMD
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001561 ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult);
Rob Landleyade7f952006-05-25 18:53:06 +00001562#else
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001563 force_operation |= (!ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult));
Eric Andersen3443bd72003-07-22 07:30:36 +00001564#endif
1565 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001566 if (set_readonly) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001567 print_flag_on_off(get_readonly, "readonly", readonly);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001568 ioctl_or_warn(fd, BLKROSET, &readonly);
Eric Andersen3443bd72003-07-22 07:30:36 +00001569 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001570 if (set_unmask) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001571 print_flag_on_off(get_unmask, "unmaskirq", unmask);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001572 ioctl_or_warn(fd, HDIO_SET_UNMASKINTR, (int *)unmask);
Eric Andersen3443bd72003-07-22 07:30:36 +00001573 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001574#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
1575 if (set_dma) {
Rob Landleyadde7982006-05-16 15:32:30 +00001576 print_flag_on_off(get_dma, "using_dma", dma);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001577 ioctl_or_warn(fd, HDIO_SET_DMA, (int *)dma);
Eric Andersen3443bd72003-07-22 07:30:36 +00001578 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001579#endif /* FEATURE_HDPARM_HDIO_GETSET_DMA */
1580 if (set_dma_q) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001581 print_flag_on_off(get_dma_q, "DMA queue_depth", dma_q);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001582 ioctl_or_warn(fd, HDIO_SET_QDMA, (int *)dma_q);
Eric Andersen3443bd72003-07-22 07:30:36 +00001583 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001584 if (set_nowerr) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001585 print_flag_on_off(get_nowerr, "nowerr", nowerr);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001586 ioctl_or_warn(fd, HDIO_SET_NOWERR, (int *)nowerr);
Eric Andersen3443bd72003-07-22 07:30:36 +00001587 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001588 if (set_keep) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001589 print_flag_on_off(get_keep, "keep_settings", keep);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001590 ioctl_or_warn(fd, HDIO_SET_KEEPSETTINGS, (int *)keep);
Eric Andersen3443bd72003-07-22 07:30:36 +00001591 }
1592#ifdef HDIO_DRIVE_CMD
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001593 if (set_doorlock) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001594 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
Rob Landley39cf6452006-05-05 16:52:28 +00001595 args[2] = 0;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001596 print_flag_on_off(get_doorlock, "drive doorlock", doorlock);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001597 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Rob Landley39cf6452006-05-05 16:52:28 +00001598 args[0] = WIN_SETFEATURES;
Eric Andersen3443bd72003-07-22 07:30:36 +00001599 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001600 if (set_dkeep) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001601 /* lock/unlock the drive's "feature" settings */
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001602 print_flag_on_off(get_dkeep, "drive keep features", dkeep);
Eric Andersen3443bd72003-07-22 07:30:36 +00001603 args[2] = dkeep ? 0x66 : 0xcc;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001604 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001605 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001606 if (set_defects) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001607 args[2] = defects ? 0x04 : 0x84;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001608 print_flag(get_defects, "drive defect-mgmt", defects);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001609 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001610 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001611 if (set_prefetch) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001612 args[1] = prefetch;
Rob Landleye5b281f2006-04-29 15:49:18 +00001613 args[2] = 0xab;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001614 print_flag(get_prefetch, "drive prefetch", prefetch);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001615 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Rob Landley39cf6452006-05-05 16:52:28 +00001616 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001617 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001618 if (set_xfermode) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001619 args[1] = xfermode_requested;
Rob Landleye5b281f2006-04-29 15:49:18 +00001620 args[2] = 3;
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001621 if (get_xfermode) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001622 print_flag(1, "xfermode", xfermode_requested);
Eric Andersen3443bd72003-07-22 07:30:36 +00001623 interpret_xfermode(xfermode_requested);
1624 }
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001625 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Rob Landley39cf6452006-05-05 16:52:28 +00001626 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001627 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001628 if (set_lookahead) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001629 args[2] = lookahead ? 0xaa : 0x55;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001630 print_flag_on_off(get_lookahead, "drive read-lookahead", lookahead);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001631 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001632 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001633 if (set_apmmode) {
Rob Landleyadde7982006-05-16 15:32:30 +00001634 args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */; /* feature register */
1635 args[1] = apmmode; /* sector count register 1-255 */
1636 if (get_apmmode)
1637 printf(" setting APM level to %s 0x%02lX (%ld)\n", (apmmode == 255) ? "disabled" : "", apmmode, apmmode);
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001638 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Rob Landley39cf6452006-05-05 16:52:28 +00001639 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001640 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001641 if (set_wcache) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001642#ifdef DO_FLUSHCACHE
1643#ifndef WIN_FLUSHCACHE
1644#define WIN_FLUSHCACHE 0xe7
1645#endif
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001646 static unsigned char flushcache[4] = { WIN_FLUSHCACHE, 0, 0, 0 };
Eric Andersen3443bd72003-07-22 07:30:36 +00001647#endif /* DO_FLUSHCACHE */
Eric Andersen3443bd72003-07-22 07:30:36 +00001648 args[2] = wcache ? 0x02 : 0x82;
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001649 print_flag_on_off(get_wcache, "drive write-caching", wcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00001650#ifdef DO_FLUSHCACHE
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001651 if (!wcache)
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001652 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00001653#endif /* DO_FLUSHCACHE */
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001654 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001655#ifdef DO_FLUSHCACHE
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001656 if (!wcache)
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001657 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
Eric Andersen3443bd72003-07-22 07:30:36 +00001658#endif /* DO_FLUSHCACHE */
1659 }
Rob Landley39cf6452006-05-05 16:52:28 +00001660
1661 /* In code below, we do not preserve args[0], but the rest
1662 is preserved, including args[2] */
1663 args[2] = 0;
1664
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001665 if (set_standbynow) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001666#ifndef WIN_STANDBYNOW1
1667#define WIN_STANDBYNOW1 0xE0
1668#endif
1669#ifndef WIN_STANDBYNOW2
1670#define WIN_STANDBYNOW2 0x94
1671#endif
Rob Landleyadde7982006-05-16 15:32:30 +00001672 if (get_standbynow) printf(" issuing standby command\n");
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001673 args[0] = WIN_STANDBYNOW1;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001674 ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2);
Eric Andersen3443bd72003-07-22 07:30:36 +00001675 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001676 if (set_sleepnow) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001677#ifndef WIN_SLEEPNOW1
1678#define WIN_SLEEPNOW1 0xE6
1679#endif
1680#ifndef WIN_SLEEPNOW2
1681#define WIN_SLEEPNOW2 0x99
1682#endif
Rob Landleyadde7982006-05-16 15:32:30 +00001683 if (get_sleepnow) printf(" issuing sleep command\n");
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001684 args[0] = WIN_SLEEPNOW1;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001685 ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2);
Eric Andersen3443bd72003-07-22 07:30:36 +00001686 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001687 if (set_seagate) {
Rob Landleye5b281f2006-04-29 15:49:18 +00001688 args[0] = 0xfb;
Rob Landleyadde7982006-05-16 15:32:30 +00001689 if (get_seagate) printf(" disabling Seagate auto powersaving mode\n");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001690 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001691 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001692 if (set_standby) {
Rob Landleye5b281f2006-04-29 15:49:18 +00001693 args[0] = WIN_SETIDLE1;
1694 args[1] = standby_requested;
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001695 if (get_standby) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001696 print_flag(1, "standby", standby_requested);
Eric Andersen3443bd72003-07-22 07:30:36 +00001697 interpret_standby(standby_requested);
1698 }
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001699 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
Rob Landley39cf6452006-05-05 16:52:28 +00001700 args[1] = 0;
Eric Andersen3443bd72003-07-22 07:30:36 +00001701 }
1702#else /* HDIO_DRIVE_CMD */
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001703 if (force_operation) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001704 char buf[512];
1705 flush_buffer_cache(fd);
1706 if (-1 == read(fd, buf, sizeof(buf)))
Rob Landleyadde7982006-05-16 15:32:30 +00001707 bb_perror_msg("read(%d bytes) failed (rc=%d)", sizeof(buf), -1);
Eric Andersen3443bd72003-07-22 07:30:36 +00001708 }
1709#endif /* HDIO_DRIVE_CMD */
1710
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001711 if (get_mult || get_identity) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001712 multcount = -1;
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001713 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount)) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001714 if (get_mult && ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn. */
Eric Andersen06d4ec22004-03-19 10:53:52 +00001715 bb_perror_msg("HDIO_GET_MULTCOUNT");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001716 else
1717 bb_perror_msg("ioctl %#x failed", HDIO_GET_MULTCOUNT);
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001718 } else if (get_mult) {
Rob Landleyadde7982006-05-16 15:32:30 +00001719 printf(fmt, "multcount", multcount);
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001720 on_off(multcount != 0);
Eric Andersen3443bd72003-07-22 07:30:36 +00001721 }
1722 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001723 if (get_io32bit) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001724 if (!ioctl_or_warn(fd, HDIO_GET_32BIT, &parm)) {
Rob Landleyadde7982006-05-16 15:32:30 +00001725 printf(" IO_support\t=%3ld (", parm);
1726 if (parm == 0)
1727 printf("default 16-bit)\n");
1728 else if (parm == 2)
1729 printf("16-bit)\n");
1730 else if (parm == 1)
1731 printf("32-bit)\n");
1732 else if (parm == 3)
1733 printf("32-bit w/sync)\n");
1734 else if (parm == 8)
1735 printf("Request-Queue-Bypass)\n");
1736 else
1737 printf("\?\?\?)\n");
Eric Andersen3443bd72003-07-22 07:30:36 +00001738 }
1739 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001740 if (get_unmask) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001741 if(!ioctl_or_warn(fd, HDIO_GET_UNMASKINTR, (unsigned long *)parm))
1742 print_value_on_off("unmaskirq", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001743 }
1744
Glenn L McGrath07085852003-10-09 07:28:22 +00001745
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001746#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
Rob Landleyade7f952006-05-25 18:53:06 +00001747 if (get_dma) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001748 if (!ioctl_or_warn(fd, HDIO_GET_DMA, &parm)) {
Rob Landleyadde7982006-05-16 15:32:30 +00001749 printf(fmt, "using_dma", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001750 if (parm == 8)
1751 printf(" (DMA-Assisted-PIO)\n");
1752 else
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001753 on_off(parm != 0);
Eric Andersen3443bd72003-07-22 07:30:36 +00001754 }
1755 }
1756#endif
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001757 if (get_dma_q) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001758 if(!ioctl_or_warn(fd, HDIO_GET_QDMA, (unsigned long *)parm))
1759 print_value_on_off("queue_depth", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001760 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001761 if (get_keep) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001762 if(!ioctl_or_warn(fd, HDIO_GET_KEEPSETTINGS, (unsigned long *)parm))
1763 print_value_on_off("keepsettings", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001764 }
1765
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001766 if (get_nowerr) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001767 if(!ioctl_or_warn(fd, HDIO_GET_NOWERR, (unsigned long *)parm))
1768 print_value_on_off("nowerr", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001769 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001770 if (get_readonly) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001771 if(!ioctl_or_warn(fd, BLKROGET, (unsigned long *)parm))
1772 print_value_on_off("readonly", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001773 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001774 if (get_readahead) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001775 if(!ioctl_or_warn(fd, BLKRAGET, (unsigned long *)parm))
1776 print_value_on_off("readahead", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001777 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001778 if (get_geom) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001779 if (!ioctl_or_warn(fd, BLKGETSIZE, &parm)) {
Rob Landley2584e9b2006-05-03 20:00:00 +00001780 struct hd_geometry g;
1781
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001782 if (!ioctl_or_warn(fd, HDIO_GETGEO, &g))
Rob Landleyadde7982006-05-16 15:32:30 +00001783 printf(" geometry\t= %u/%u/%u, sectors = %ld, start = %ld\n",
Rob Landley2584e9b2006-05-03 20:00:00 +00001784 g.cylinders, g.heads, g.sectors, parm, g.start);
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001785 }
Eric Andersen3443bd72003-07-22 07:30:36 +00001786 }
1787#ifdef HDIO_DRIVE_CMD
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001788 if (get_powermode) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001789#ifndef WIN_CHECKPOWERMODE1
1790#define WIN_CHECKPOWERMODE1 0xE5
1791#endif
1792#ifndef WIN_CHECKPOWERMODE2
1793#define WIN_CHECKPOWERMODE2 0x98
1794#endif
Eric Andersen3443bd72003-07-22 07:30:36 +00001795 const char *state;
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001796
Rob Landleye5b281f2006-04-29 15:49:18 +00001797 args[0] = WIN_CHECKPOWERMODE1;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001798 if (ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2)) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001799 if (errno != EIO || args[0] != 0 || args[1] != 0)
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001800 state = "unknown";
Eric Andersen3443bd72003-07-22 07:30:36 +00001801 else
1802 state = "sleeping";
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001803 } else
Glenn L McGrath07085852003-10-09 07:28:22 +00001804 state = (args[2] == 255) ? "active/idle" : "standby";
Rob Landley39cf6452006-05-05 16:52:28 +00001805 args[1] = args[2] = 0;
Glenn L McGrath07085852003-10-09 07:28:22 +00001806
Eric Andersen3443bd72003-07-22 07:30:36 +00001807 printf(" drive state is: %s\n", state);
1808 }
1809#endif
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001810#if ENABLE_FEATURE_HDPARM_HDIO_DRIVE_RESET
1811 if (perform_reset) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001812 ioctl_or_warn(fd, HDIO_DRIVE_RESET, NULL);
Eric Andersen3443bd72003-07-22 07:30:36 +00001813 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001814#endif /* FEATURE_HDPARM_HDIO_DRIVE_RESET */
1815#if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1816 if (perform_tristate) {
Rob Landleye5b281f2006-04-29 15:49:18 +00001817 args[0] = 0;
1818 args[1] = tristate;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001819 ioctl_or_warn(fd, HDIO_TRISTATE_HWIF, &args);
Eric Andersen3443bd72003-07-22 07:30:36 +00001820 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001821#endif /* FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
1822#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
1823 if (get_identity) {
Denis Vlasenko7c282a22007-03-28 00:14:54 +00001824 struct hd_driveid id;
Eric Andersen3443bd72003-07-22 07:30:36 +00001825
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001826 if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
1827 if (multcount != -1) {
Eric Andersen3443bd72003-07-22 07:30:36 +00001828 id.multsect = multcount;
1829 id.multsect_valid |= 1;
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001830 } else
Eric Andersen3443bd72003-07-22 07:30:36 +00001831 id.multsect_valid &= ~1;
1832 dump_identity(&id);
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001833 } else if (errno == -ENOMSG)
Eric Andersen3443bd72003-07-22 07:30:36 +00001834 printf(" no identification info available\n");
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001835 else if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
Denis Vlasenko49a128a2007-07-17 21:42:59 +00001836 bb_perror_msg("HDIO_GET_IDENTITY");
Eric Andersen3443bd72003-07-22 07:30:36 +00001837 else
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001838 bb_perror_msg("ioctl %#x failed", HDIO_GET_IDENTITY);
Eric Andersen3443bd72003-07-22 07:30:36 +00001839 }
Glenn L McGrath07085852003-10-09 07:28:22 +00001840
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001841 if (get_IDentity) {
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001842 unsigned char args1[4+512]; /* = { ... } will eat 0.5k of rodata! */
Glenn L McGrath07085852003-10-09 07:28:22 +00001843
Rob Landley5f8b5ec2006-04-29 16:03:40 +00001844 memset(args1, 0, sizeof(args1));
1845 args1[0] = WIN_IDENTIFY;
1846 args1[3] = 1;
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001847 if (!ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY))
Rob Landley0753f4a2006-06-07 00:27:25 +00001848 identify((void *)(args1 + 4));
Eric Andersen3443bd72003-07-22 07:30:36 +00001849 }
1850#endif
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001851#if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1852 if (set_busstate) {
1853 if (get_busstate) {
Rob Landleyadde7982006-05-16 15:32:30 +00001854 print_flag(1, "bus state", busstate);
Eric Andersen3443bd72003-07-22 07:30:36 +00001855 bus_state_value(busstate);
1856 }
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001857 ioctl_or_warn(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate);
Eric Andersen3443bd72003-07-22 07:30:36 +00001858 }
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001859 if (get_busstate) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001860 if (!ioctl_or_warn(fd, HDIO_GET_BUSSTATE, &parm)) {
Rob Landleyadde7982006-05-16 15:32:30 +00001861 printf(fmt, "bus state", parm);
Eric Andersen3443bd72003-07-22 07:30:36 +00001862 bus_state_value(parm);
1863 }
1864 }
1865#endif
Glenn L McGrath07085852003-10-09 07:28:22 +00001866 if (reread_partn)
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +00001867 ioctl_or_warn(fd, BLKRRPART, NULL);
Glenn L McGrath07085852003-10-09 07:28:22 +00001868
Eric Andersen3443bd72003-07-22 07:30:36 +00001869 if (do_ctimings)
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001870 do_time(0, fd); /* time cache */
Eric Andersen3443bd72003-07-22 07:30:36 +00001871 if (do_timings)
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001872 do_time(1, fd); /* time device */
Eric Andersen3443bd72003-07-22 07:30:36 +00001873 if (do_flush)
Rob Landleya3e4f382006-04-29 16:06:31 +00001874 flush_buffer_cache(fd);
1875 close(fd);
Eric Andersen3443bd72003-07-22 07:30:36 +00001876}
1877
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001878#if ENABLE_FEATURE_HDPARM_GET_IDENTITY
Rob Landleya3e4f382006-04-29 16:06:31 +00001879static int fromhex(unsigned char c)
Eric Andersen3443bd72003-07-22 07:30:36 +00001880{
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +00001881 if (isdigit(c))
Eric Andersen3443bd72003-07-22 07:30:36 +00001882 return (c - '0');
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +00001883 if (c >= 'a' && c <= 'f')
1884 return (c - ('a' - 10));
Eric Andersen3443bd72003-07-22 07:30:36 +00001885 bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
1886}
1887
Rob Landley0f0b6452006-05-03 18:28:06 +00001888static void identify_from_stdin(void)
Eric Andersen3443bd72003-07-22 07:30:36 +00001889{
Rob Landley0753f4a2006-06-07 00:27:25 +00001890 uint16_t sbuf[256];
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +00001891 unsigned char buf[1280];
1892 unsigned char *b = (unsigned char *)buf;
1893 int i;
Eric Andersen3443bd72003-07-22 07:30:36 +00001894
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +00001895 xread(0, buf, 1280);
Rob Landleyade7f952006-05-25 18:53:06 +00001896
Rob Landley0753f4a2006-06-07 00:27:25 +00001897 // Convert the newline-separated hex data into an identify block.
1898
Denis Vlasenko3bf00202007-02-18 13:36:04 +00001899 for (i = 0; i < 256; i++) {
Rob Landley0753f4a2006-06-07 00:27:25 +00001900 int j;
Denis Vlasenkobf0a2012006-12-26 10:42:51 +00001901 for (j = 0; j < 4; j++)
Denis Vlasenko3a34d0c2007-01-12 22:10:34 +00001902 sbuf[i] = (sbuf[i] << 4) + fromhex(*(b++));
Eric Andersen3443bd72003-07-22 07:30:36 +00001903 }
Rob Landley0753f4a2006-06-07 00:27:25 +00001904
1905 // Parse the data.
1906
Rob Landley6389ff12006-05-01 19:28:53 +00001907 identify(sbuf);
Eric Andersen3443bd72003-07-22 07:30:36 +00001908}
1909#endif
1910
Rob Landley20deab02006-05-07 23:34:15 +00001911/* busybox specific stuff */
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001912static void parse_opts(smallint *get, smallint *set, unsigned long *value, int min, int max)
Eric Andersenb2aa7762004-04-05 13:08:08 +00001913{
Denis Vlasenko6429aab2006-09-23 12:22:11 +00001914 if (get) {
1915 *get = 1;
1916 }
1917 if (optarg) {
1918 *set = 1;
Denis Vlasenko13858992006-10-08 12:49:22 +00001919 *value = xatol_range(optarg, min, max);
Denis Vlasenko6429aab2006-09-23 12:22:11 +00001920 }
Rob Landley20deab02006-05-07 23:34:15 +00001921}
1922
Denis Vlasenko0eec4ab2007-03-28 01:00:45 +00001923static void parse_xfermode(int flag, smallint *get, smallint *set, int *value)
Rob Landley20deab02006-05-07 23:34:15 +00001924{
1925 if (flag) {
Rob Landleyade7f952006-05-25 18:53:06 +00001926 *get = 1;
Rob Landley20deab02006-05-07 23:34:15 +00001927 if (optarg) {
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001928 *value = translate_xfermode(optarg);
1929 *set = (*value > -1);
Rob Landley20deab02006-05-07 23:34:15 +00001930 }
1931 }
1932}
1933
Rob Landley06208412006-05-31 22:52:57 +00001934/*------- getopt short options --------*/
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001935static const char hdparm_options[] ALIGN1 =
1936 "gfu::n::p:r::m::c::k::a::B:tTh"
Rob Landley06208412006-05-31 22:52:57 +00001937 USE_FEATURE_HDPARM_GET_IDENTITY("iI")
Rob Landley20deab02006-05-07 23:34:15 +00001938 USE_FEATURE_HDPARM_HDIO_GETSET_DMA("d::")
1939#ifdef HDIO_DRIVE_CMD
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001940 "S:D:P:X:K:A:L:W:CyYzZ"
Rob Landley20deab02006-05-07 23:34:15 +00001941#endif
1942 USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF("U:")
1943#ifdef HDIO_GET_QDMA
1944#ifdef HDIO_SET_QDMA
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001945 "Q:"
Rob Landley20deab02006-05-07 23:34:15 +00001946#else
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001947 "Q"
Rob Landley20deab02006-05-07 23:34:15 +00001948#endif
1949#endif
1950 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET("w")
1951 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF("x::b:")
1952 USE_FEATURE_HDPARM_HDIO_SCAN_HWIF("R:");
1953/*-------------------------------------*/
1954
1955/* our main() routine: */
Denis Vlasenko06af2162007-02-03 17:28:39 +00001956int hdparm_main(int argc, char **argv);
Rob Landley20deab02006-05-07 23:34:15 +00001957int hdparm_main(int argc, char **argv)
1958{
1959 int c;
Rob Landleyade7f952006-05-25 18:53:06 +00001960 int flagcount = 0;
Rob Landley20deab02006-05-07 23:34:15 +00001961
Rob Landley06208412006-05-31 22:52:57 +00001962 while ((c = getopt(argc, argv, hdparm_options)) >= 0) {
Rob Landley20deab02006-05-07 23:34:15 +00001963 flagcount++;
Rob Landley06208412006-05-31 22:52:57 +00001964 if (c == 'h') bb_show_usage(); /* EXIT */
Mike Frysingerea93f8a2006-06-07 14:25:22 +00001965 USE_FEATURE_HDPARM_GET_IDENTITY(get_IDentity |= (c == 'I'));
1966 USE_FEATURE_HDPARM_GET_IDENTITY(get_identity |= (c == 'i'));
Rob Landleyadde7982006-05-16 15:32:30 +00001967 get_geom |= (c == 'g');
1968 do_flush |= (c == 'f');
Rob Landley19802562006-05-08 15:35:46 +00001969 if (c == 'u') parse_opts(&get_unmask, &set_unmask, &unmask, 0, 1);
1970 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(if (c == 'd') parse_opts(&get_dma, &set_dma, &dma, 0, 9));
1971 if (c == 'n') parse_opts(&get_nowerr, &set_nowerr, &nowerr, 0, 1);
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00001972 parse_xfermode((c == 'p'), &noisy_piomode, &set_piomode, &piomode);
Rob Landley19802562006-05-08 15:35:46 +00001973 if (c == 'r') parse_opts(&get_readonly, &set_readonly, &readonly, 0, 1);
1974 if (c == 'm') parse_opts(&get_mult, &set_mult, &mult, 0, INT_MAX /*32*/);
1975 if (c == 'c') parse_opts(&get_io32bit, &set_io32bit, &io32bit, 0, INT_MAX /*8*/);
1976 if (c == 'k') parse_opts(&get_keep, &set_keep, &keep, 0, 1);
1977 if (c == 'a') parse_opts(&get_readahead, &set_readahead, &Xreadahead, 0, INT_MAX);
1978 if (c == 'B') parse_opts(&get_apmmode, &set_apmmode, &apmmode, 1, 255);
Rob Landleyadde7982006-05-16 15:32:30 +00001979 do_flush |= do_timings |= (c == 't');
1980 do_flush |= do_ctimings |= (c == 'T');
Rob Landley20deab02006-05-07 23:34:15 +00001981#ifdef HDIO_DRIVE_CMD
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001982 if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, INT_MAX);
Rob Landley19802562006-05-08 15:35:46 +00001983 if (c == 'D') parse_opts(&get_defects, &set_defects, &defects, 0, INT_MAX);
1984 if (c == 'P') parse_opts(&get_prefetch, &set_prefetch, &prefetch, 0, INT_MAX);
Rob Landleyade7f952006-05-25 18:53:06 +00001985 parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
Rob Landley19802562006-05-08 15:35:46 +00001986 if (c == 'K') parse_opts(&get_dkeep, &set_dkeep, &prefetch, 0, 1);
1987 if (c == 'A') parse_opts(&get_lookahead, &set_lookahead, &lookahead, 0, 1);
1988 if (c == 'L') parse_opts(&get_doorlock, &set_doorlock, &doorlock, 0, 1);
1989 if (c == 'W') parse_opts(&get_wcache, &set_wcache, &wcache, 0, 1);
Rob Landleyade7f952006-05-25 18:53:06 +00001990 get_powermode |= (c == 'C');
1991 get_standbynow = set_standbynow |= (c == 'y');
1992 get_sleepnow = set_sleepnow |= (c == 'Y');
Rob Landleyadde7982006-05-16 15:32:30 +00001993 reread_partn |= (c == 'z');
Rob Landleyade7f952006-05-25 18:53:06 +00001994 get_seagate = set_seagate |= (c == 'Z');
Rob Landley20deab02006-05-07 23:34:15 +00001995#endif
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00001996 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 +00001997#ifdef HDIO_GET_QDMA
Rob Landley19802562006-05-08 15:35:46 +00001998 if (c == 'Q') {
Rob Landley20deab02006-05-07 23:34:15 +00001999#ifdef HDIO_SET_QDMA
Rob Landley19802562006-05-08 15:35:46 +00002000 parse_opts(&get_dma_q, &set_dma_q, &dma_q, 0, INT_MAX);
Rob Landley20deab02006-05-07 23:34:15 +00002001#else
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00002002 parse_opts(&get_dma_q, NULL, NULL, 0, 0);
Rob Landley20deab02006-05-07 23:34:15 +00002003#endif
Rob Landley19802562006-05-08 15:35:46 +00002004 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00002005#endif
Rob Landleyade7f952006-05-25 18:53:06 +00002006 USE_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00002007 USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1));
2008 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 +00002009#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
2010 if (c == 'R') {
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00002011 parse_opts(NULL, &scan_hwif, &hwif_data, 0, INT_MAX);
Denis Vlasenko13858992006-10-08 12:49:22 +00002012 hwif_ctrl = xatoi_u((argv[optind]) ? argv[optind] : "");
2013 hwif_irq = xatoi_u((argv[optind+1]) ? argv[optind+1] : "");
Rob Landley20deab02006-05-07 23:34:15 +00002014 /* Move past the 2 additional arguments */
2015 argv += 2;
2016 argc -= 2;
2017 }
2018#endif
2019 }
Rob Landleyade7f952006-05-25 18:53:06 +00002020 /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
Denis Vlasenko67b23e62006-10-03 21:00:06 +00002021 if (!flagcount) {
Rob Landleyade7f952006-05-25 18:53:06 +00002022 get_mult = get_io32bit = get_unmask = get_keep = get_readonly = get_readahead = get_geom = 1;
2023 USE_FEATURE_HDPARM_HDIO_GETSET_DMA(get_dma = 1);
2024 }
Rob Landley20deab02006-05-07 23:34:15 +00002025 argv += optind;
2026
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00002027 if (!*argv) {
Rob Landley6d8ce172006-06-07 21:22:42 +00002028 if (ENABLE_FEATURE_HDPARM_GET_IDENTITY && !isatty(STDIN_FILENO))
2029 identify_from_stdin(); /* EXIT */
2030 else bb_show_usage();
Rob Landley20deab02006-05-07 23:34:15 +00002031 }
2032
Denis Vlasenkof1ba7492007-03-28 00:14:01 +00002033 do {
2034 process_dev(*argv++);
2035 } while (*argv);
2036
2037 return EXIT_SUCCESS;
Eric Andersen3443bd72003-07-22 07:30:36 +00002038}