aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-05-20 16:11:41 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2009-05-20 16:11:41 +0200
commitc7068455393e03a340fe8f64126973f28bc9fd3a (patch)
treef2b383bf6a6f3aaeb9e5460ef3cb91b0d7c6077f
parent6d335be21167bbc93b7d8cd434333d78463b5b92 (diff)
downloadbusybox-w32-c7068455393e03a340fe8f64126973f28bc9fd3a.tar.gz
busybox-w32-c7068455393e03a340fe8f64126973f28bc9fd3a.tar.bz2
busybox-w32-c7068455393e03a340fe8f64126973f28bc9fd3a.zip
hdparm: code shrink
function old new delta parse_opts_0_INTMAX - 12 +12 parse_opts_0_1 - 12 +12 parse_opts_0_max - 9 +9 parse_opts 44 36 -8 process_dev 4679 4578 -101 hdparm_main 1061 854 -207 ------------------------------------------------------------------------------ (add/remove: 3/0 grow/shrink: 0/3 up/down: 33/-316) Total: -283 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--miscutils/hdparm.c317
1 files changed, 146 insertions, 171 deletions
diff --git a/miscutils/hdparm.c b/miscutils/hdparm.c
index ece01fb08..7034778e7 100644
--- a/miscutils/hdparm.c
+++ b/miscutils/hdparm.c
@@ -11,7 +11,6 @@
11 * hdparm.c - Command line interface to get/set hard disk parameters 11 * hdparm.c - Command line interface to get/set hard disk parameters
12 * - by Mark Lord (C) 1994-2002 -- freely distributable 12 * - by Mark Lord (C) 1994-2002 -- freely distributable
13 */ 13 */
14
15#include "libbb.h" 14#include "libbb.h"
16#include <linux/hdreg.h> 15#include <linux/hdreg.h>
17 16
@@ -232,6 +231,10 @@
232#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */ 231#undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
233 232
234 233
234#define IS_GET 1
235#define IS_SET 2
236
237
235enum { fd = 3 }; 238enum { fd = 3 };
236 239
237 240
@@ -241,19 +244,16 @@ struct globals {
241 smallint do_ctimings, do_timings; 244 smallint do_ctimings, do_timings;
242 smallint reread_partn; 245 smallint reread_partn;
243 smallint set_piomode, noisy_piomode; 246 smallint set_piomode, noisy_piomode;
244 smallint set_readahead, get_readahead; 247 smallint getset_readahead;
245 smallint set_readonly, get_readonly; 248 smallint getset_readonly;
246 smallint set_unmask, get_unmask; 249 smallint getset_unmask;
247 smallint set_mult, get_mult; 250 smallint getset_mult;
248#ifdef HDIO_GET_QDMA 251#ifdef HDIO_GET_QDMA
249 smallint get_dma_q; 252 smallint getset_dma_q;
250#ifdef HDIO_SET_QDMA
251 smallint set_dma_q;
252#endif
253#endif 253#endif
254 smallint set_nowerr, get_nowerr; 254 smallint getset_nowerr;
255 smallint set_keep, get_keep; 255 smallint getset_keep;
256 smallint set_io32bit, get_io32bit; 256 smallint getset_io32bit;
257 int piomode; 257 int piomode;
258 unsigned long Xreadahead; 258 unsigned long Xreadahead;
259 unsigned long readonly; 259 unsigned long readonly;
@@ -267,22 +267,22 @@ struct globals {
267 unsigned long io32bit; 267 unsigned long io32bit;
268#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA 268#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
269 unsigned long dma; 269 unsigned long dma;
270 smallint set_dma, get_dma; 270 smallint getset_dma;
271#endif 271#endif
272#ifdef HDIO_DRIVE_CMD 272#ifdef HDIO_DRIVE_CMD
273 smallint set_xfermode, get_xfermode; 273 smallint set_xfermode, get_xfermode;
274 smallint set_dkeep, get_dkeep; 274 smallint getset_dkeep;
275 smallint set_standby, get_standby; 275 smallint getset_standby;
276 smallint set_lookahead, get_lookahead; 276 smallint getset_lookahead;
277 smallint set_prefetch, get_prefetch; 277 smallint getset_prefetch;
278 smallint set_defects, get_defects; 278 smallint getset_defects;
279 smallint set_wcache, get_wcache; 279 smallint getset_wcache;
280 smallint set_doorlock, get_doorlock; 280 smallint getset_doorlock;
281 smallint set_seagate, get_seagate; 281 smallint set_seagate;
282 smallint set_standbynow, get_standbynow; 282 smallint set_standbynow;
283 smallint set_sleepnow, get_sleepnow; 283 smallint set_sleepnow;
284 smallint get_powermode; 284 smallint get_powermode;
285 smallint set_apmmode, get_apmmode; 285 smallint getset_apmmode;
286 int xfermode_requested; 286 int xfermode_requested;
287 unsigned long dkeep; 287 unsigned long dkeep;
288 unsigned long standby_requested; /* 0..255 */ 288 unsigned long standby_requested; /* 0..255 */
@@ -294,7 +294,7 @@ struct globals {
294 unsigned long apmmode; 294 unsigned long apmmode;
295#endif 295#endif
296 IF_FEATURE_HDPARM_GET_IDENTITY( smallint get_IDentity;) 296 IF_FEATURE_HDPARM_GET_IDENTITY( smallint get_IDentity;)
297 IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( smallint set_busstate, get_busstate;) 297 IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( smallint getset_busstate;)
298 IF_FEATURE_HDPARM_HDIO_DRIVE_RESET( smallint perform_reset;) 298 IF_FEATURE_HDPARM_HDIO_DRIVE_RESET( smallint perform_reset;)
299 IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( smallint perform_tristate;) 299 IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( smallint perform_tristate;)
300 IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(smallint unregister_hwif;) 300 IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(smallint unregister_hwif;)
@@ -323,22 +323,14 @@ struct BUG_G_too_big {
323#define reread_partn (G.reread_partn ) 323#define reread_partn (G.reread_partn )
324#define set_piomode (G.set_piomode ) 324#define set_piomode (G.set_piomode )
325#define noisy_piomode (G.noisy_piomode ) 325#define noisy_piomode (G.noisy_piomode )
326#define set_readahead (G.set_readahead ) 326#define getset_readahead (G.getset_readahead )
327#define get_readahead (G.get_readahead ) 327#define getset_readonly (G.getset_readonly )
328#define set_readonly (G.set_readonly ) 328#define getset_unmask (G.getset_unmask )
329#define get_readonly (G.get_readonly ) 329#define getset_mult (G.getset_mult )
330#define set_unmask (G.set_unmask ) 330#define getset_dma_q (G.getset_dma_q )
331#define get_unmask (G.get_unmask ) 331#define getset_nowerr (G.getset_nowerr )
332#define set_mult (G.set_mult ) 332#define getset_keep (G.getset_keep )
333#define get_mult (G.get_mult ) 333#define getset_io32bit (G.getset_io32bit )
334#define set_dma_q (G.set_dma_q )
335#define get_dma_q (G.get_dma_q )
336#define set_nowerr (G.set_nowerr )
337#define get_nowerr (G.get_nowerr )
338#define set_keep (G.set_keep )
339#define get_keep (G.get_keep )
340#define set_io32bit (G.set_io32bit )
341#define get_io32bit (G.get_io32bit )
342#define piomode (G.piomode ) 334#define piomode (G.piomode )
343#define Xreadahead (G.Xreadahead ) 335#define Xreadahead (G.Xreadahead )
344#define readonly (G.readonly ) 336#define readonly (G.readonly )
@@ -349,33 +341,21 @@ struct BUG_G_too_big {
349#define keep (G.keep ) 341#define keep (G.keep )
350#define io32bit (G.io32bit ) 342#define io32bit (G.io32bit )
351#define dma (G.dma ) 343#define dma (G.dma )
352#define set_dma (G.set_dma ) 344#define getset_dma (G.getset_dma )
353#define get_dma (G.get_dma )
354#define set_xfermode (G.set_xfermode ) 345#define set_xfermode (G.set_xfermode )
355#define get_xfermode (G.get_xfermode ) 346#define get_xfermode (G.get_xfermode )
356#define set_dkeep (G.set_dkeep ) 347#define getset_dkeep (G.getset_dkeep )
357#define get_dkeep (G.get_dkeep ) 348#define getset_standby (G.getset_standby )
358#define set_standby (G.set_standby ) 349#define getset_lookahead (G.getset_lookahead )
359#define get_standby (G.get_standby ) 350#define getset_prefetch (G.getset_prefetch )
360#define set_lookahead (G.set_lookahead ) 351#define getset_defects (G.getset_defects )
361#define get_lookahead (G.get_lookahead ) 352#define getset_wcache (G.getset_wcache )
362#define set_prefetch (G.set_prefetch ) 353#define getset_doorlock (G.getset_doorlock )
363#define get_prefetch (G.get_prefetch )
364#define set_defects (G.set_defects )
365#define get_defects (G.get_defects )
366#define set_wcache (G.set_wcache )
367#define get_wcache (G.get_wcache )
368#define set_doorlock (G.set_doorlock )
369#define get_doorlock (G.get_doorlock )
370#define set_seagate (G.set_seagate ) 354#define set_seagate (G.set_seagate )
371#define get_seagate (G.get_seagate )
372#define set_standbynow (G.set_standbynow ) 355#define set_standbynow (G.set_standbynow )
373#define get_standbynow (G.get_standbynow )
374#define set_sleepnow (G.set_sleepnow ) 356#define set_sleepnow (G.set_sleepnow )
375#define get_sleepnow (G.get_sleepnow )
376#define get_powermode (G.get_powermode ) 357#define get_powermode (G.get_powermode )
377#define set_apmmode (G.set_apmmode ) 358#define getset_apmmode (G.getset_apmmode )
378#define get_apmmode (G.get_apmmode )
379#define xfermode_requested (G.xfermode_requested ) 359#define xfermode_requested (G.xfermode_requested )
380#define dkeep (G.dkeep ) 360#define dkeep (G.dkeep )
381#define standby_requested (G.standby_requested ) 361#define standby_requested (G.standby_requested )
@@ -386,8 +366,7 @@ struct BUG_G_too_big {
386#define doorlock (G.doorlock ) 366#define doorlock (G.doorlock )
387#define apmmode (G.apmmode ) 367#define apmmode (G.apmmode )
388#define get_IDentity (G.get_IDentity ) 368#define get_IDentity (G.get_IDentity )
389#define set_busstate (G.set_busstate ) 369#define getset_busstate (G.getset_busstate )
390#define get_busstate (G.get_busstate )
391#define perform_reset (G.perform_reset ) 370#define perform_reset (G.perform_reset )
392#define perform_tristate (G.perform_tristate ) 371#define perform_tristate (G.perform_tristate )
393#define unregister_hwif (G.unregister_hwif ) 372#define unregister_hwif (G.unregister_hwif )
@@ -1275,7 +1254,7 @@ static void dump_identity(const struct hd_driveid *id)
1275 "*\0""sdma0 \0""*\0""sdma1 \0""*\0""sdma2 \0""*\0""sdma? \0", 1254 "*\0""sdma0 \0""*\0""sdma1 \0""*\0""sdma2 \0""*\0""sdma? \0",
1276 id->dma_1word, NULL); 1255 id->dma_1word, NULL);
1277 print_flags_separated(dma_wmode_masks, 1256 print_flags_separated(dma_wmode_masks,
1278 "*\0""mdma0\0""*\0""mdma1\0""*\0""mdma2\0""*\0""mdma?\0", 1257 "*\0""mdma0 \0""*\0""mdma1 \0""*\0""mdma2 \0""*\0""mdma? \0",
1279 id->dma_mword, NULL); 1258 id->dma_mword, NULL);
1280 } 1259 }
1281 } 1260 }
@@ -1571,8 +1550,8 @@ static void process_dev(char *devname)
1571 xmove_fd(xopen(devname, O_RDONLY | O_NONBLOCK), fd); 1550 xmove_fd(xopen(devname, O_RDONLY | O_NONBLOCK), fd);
1572 printf("\n%s:\n", devname); 1551 printf("\n%s:\n", devname);
1573 1552
1574 if (set_readahead) { 1553 if (getset_readahead == IS_SET) {
1575 print_flag(get_readahead, "fs readahead", Xreadahead); 1554 print_flag(getset_readahead, "fs readahead", Xreadahead);
1576 ioctl_or_warn(fd, BLKRASET, (int *)Xreadahead); 1555 ioctl_or_warn(fd, BLKRASET, (int *)Xreadahead);
1577 } 1556 }
1578#if ENABLE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF 1557#if ENABLE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
@@ -1582,7 +1561,7 @@ static void process_dev(char *devname)
1582 } 1561 }
1583#endif 1562#endif
1584#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF 1563#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
1585 if (scan_hwif) { 1564 if (scan_hwif == IS_SET) {
1586 printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq); 1565 printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq);
1587 args[0] = hwif_data; 1566 args[0] = hwif_data;
1588 args[1] = hwif_ctrl; 1567 args[1] = hwif_ctrl;
@@ -1606,106 +1585,103 @@ static void process_dev(char *devname)
1606 } 1585 }
1607 ioctl_or_warn(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode); 1586 ioctl_or_warn(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode);
1608 } 1587 }
1609 if (set_io32bit) { 1588 if (getset_io32bit == IS_SET) {
1610 print_flag(get_io32bit, "32-bit IO_support flag", io32bit); 1589 print_flag(getset_io32bit, "32-bit IO_support flag", io32bit);
1611 ioctl_or_warn(fd, HDIO_SET_32BIT, (int *)io32bit); 1590 ioctl_or_warn(fd, HDIO_SET_32BIT, (int *)io32bit);
1612 } 1591 }
1613 if (set_mult) { 1592 if (getset_mult == IS_SET) {
1614 print_flag(get_mult, "multcount", mult); 1593 print_flag(getset_mult, "multcount", mult);
1615#ifdef HDIO_DRIVE_CMD 1594#ifdef HDIO_DRIVE_CMD
1616 ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult); 1595 ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult);
1617#else 1596#else
1618 force_operation |= (!ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult)); 1597 force_operation |= (!ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult));
1619#endif 1598#endif
1620 } 1599 }
1621 if (set_readonly) { 1600 if (getset_readonly == IS_SET) {
1622 print_flag_on_off(get_readonly, "readonly", readonly); 1601 print_flag_on_off(getset_readonly, "readonly", readonly);
1623 ioctl_or_warn(fd, BLKROSET, &readonly); 1602 ioctl_or_warn(fd, BLKROSET, &readonly);
1624 } 1603 }
1625 if (set_unmask) { 1604 if (getset_unmask == IS_SET) {
1626 print_flag_on_off(get_unmask, "unmaskirq", unmask); 1605 print_flag_on_off(getset_unmask, "unmaskirq", unmask);
1627 ioctl_or_warn(fd, HDIO_SET_UNMASKINTR, (int *)unmask); 1606 ioctl_or_warn(fd, HDIO_SET_UNMASKINTR, (int *)unmask);
1628 } 1607 }
1629#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA 1608#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
1630 if (set_dma) { 1609 if (getset_dma == IS_SET) {
1631 print_flag_on_off(get_dma, "using_dma", dma); 1610 print_flag_on_off(getset_dma, "using_dma", dma);
1632 ioctl_or_warn(fd, HDIO_SET_DMA, (int *)dma); 1611 ioctl_or_warn(fd, HDIO_SET_DMA, (int *)dma);
1633 } 1612 }
1634#endif /* FEATURE_HDPARM_HDIO_GETSET_DMA */ 1613#endif /* FEATURE_HDPARM_HDIO_GETSET_DMA */
1635#ifdef HDIO_SET_QDMA 1614#ifdef HDIO_SET_QDMA
1636 if (set_dma_q) { 1615 if (getset_dma_q == IS_SET) {
1637 print_flag_on_off(get_dma_q, "DMA queue_depth", dma_q); 1616 print_flag_on_off(getset_dma_q, "DMA queue_depth", dma_q);
1638 ioctl_or_warn(fd, HDIO_SET_QDMA, (int *)dma_q); 1617 ioctl_or_warn(fd, HDIO_SET_QDMA, (int *)dma_q);
1639 } 1618 }
1640#endif 1619#endif
1641 if (set_nowerr) { 1620 if (getset_nowerr == IS_SET) {
1642 print_flag_on_off(get_nowerr, "nowerr", nowerr); 1621 print_flag_on_off(getset_nowerr, "nowerr", nowerr);
1643 ioctl_or_warn(fd, HDIO_SET_NOWERR, (int *)nowerr); 1622 ioctl_or_warn(fd, HDIO_SET_NOWERR, (int *)nowerr);
1644 } 1623 }
1645 if (set_keep) { 1624 if (getset_keep == IS_SET) {
1646 print_flag_on_off(get_keep, "keep_settings", keep); 1625 print_flag_on_off(getset_keep, "keep_settings", keep);
1647 ioctl_or_warn(fd, HDIO_SET_KEEPSETTINGS, (int *)keep); 1626 ioctl_or_warn(fd, HDIO_SET_KEEPSETTINGS, (int *)keep);
1648 } 1627 }
1649#ifdef HDIO_DRIVE_CMD 1628#ifdef HDIO_DRIVE_CMD
1650 if (set_doorlock) { 1629 if (getset_doorlock == IS_SET) {
1651 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK; 1630 args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
1652 args[2] = 0; 1631 args[2] = 0;
1653 print_flag_on_off(get_doorlock, "drive doorlock", doorlock); 1632 print_flag_on_off(getset_doorlock, "drive doorlock", doorlock);
1654 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args); 1633 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
1655 args[0] = WIN_SETFEATURES; 1634 args[0] = WIN_SETFEATURES;
1656 } 1635 }
1657 if (set_dkeep) { 1636 if (getset_dkeep == IS_SET) {
1658 /* lock/unlock the drive's "feature" settings */ 1637 /* lock/unlock the drive's "feature" settings */
1659 print_flag_on_off(get_dkeep, "drive keep features", dkeep); 1638 print_flag_on_off(getset_dkeep, "drive keep features", dkeep);
1660 args[2] = dkeep ? 0x66 : 0xcc; 1639 args[2] = dkeep ? 0x66 : 0xcc;
1661 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args); 1640 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
1662 } 1641 }
1663 if (set_defects) { 1642 if (getset_defects == IS_SET) {
1664 args[2] = defects ? 0x04 : 0x84; 1643 args[2] = defects ? 0x04 : 0x84;
1665 print_flag(get_defects, "drive defect-mgmt", defects); 1644 print_flag(getset_defects, "drive defect-mgmt", defects);
1666 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args); 1645 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
1667 } 1646 }
1668 if (set_prefetch) { 1647 if (getset_prefetch == IS_SET) {
1669 args[1] = prefetch; 1648 args[1] = prefetch;
1670 args[2] = 0xab; 1649 args[2] = 0xab;
1671 print_flag(get_prefetch, "drive prefetch", prefetch); 1650 print_flag(getset_prefetch, "drive prefetch", prefetch);
1672 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args); 1651 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
1673 args[1] = 0; 1652 args[1] = 0;
1674 } 1653 }
1675 if (set_xfermode) { 1654 if (set_xfermode) {
1676 args[1] = xfermode_requested; 1655 args[1] = xfermode_requested;
1677 args[2] = 3; 1656 args[2] = 3;
1678 if (get_xfermode) { 1657 print_flag(1, "xfermode", xfermode_requested);
1679 print_flag(1, "xfermode", xfermode_requested); 1658 interpret_xfermode(xfermode_requested);
1680 interpret_xfermode(xfermode_requested);
1681 }
1682 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args); 1659 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
1683 args[1] = 0; 1660 args[1] = 0;
1684 } 1661 }
1685 if (set_lookahead) { 1662 if (getset_lookahead == IS_SET) {
1686 args[2] = lookahead ? 0xaa : 0x55; 1663 args[2] = lookahead ? 0xaa : 0x55;
1687 print_flag_on_off(get_lookahead, "drive read-lookahead", lookahead); 1664 print_flag_on_off(getset_lookahead, "drive read-lookahead", lookahead);
1688 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args); 1665 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
1689 } 1666 }
1690 if (set_apmmode) { 1667 if (getset_apmmode == IS_SET) {
1691 /* feature register */ 1668 /* feature register */
1692 args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */; 1669 args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */;
1693 args[1] = apmmode; /* sector count register 1-255 */ 1670 args[1] = apmmode; /* sector count register 1-255 */
1694 if (get_apmmode) 1671 printf(" setting APM level to %s 0x%02lX (%ld)\n",
1695 printf(" setting APM level to %s 0x%02lX (%ld)\n", 1672 (apmmode == 255) ? "disabled" : "",
1696 (apmmode == 255) ? "disabled" : "", 1673 apmmode, apmmode);
1697 apmmode, apmmode);
1698 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args); 1674 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
1699 args[1] = 0; 1675 args[1] = 0;
1700 } 1676 }
1701 if (set_wcache) { 1677 if (getset_wcache == IS_SET) {
1702#ifdef DO_FLUSHCACHE 1678#ifdef DO_FLUSHCACHE
1703#ifndef WIN_FLUSHCACHE 1679#ifndef WIN_FLUSHCACHE
1704#define WIN_FLUSHCACHE 0xe7 1680#define WIN_FLUSHCACHE 0xe7
1705#endif 1681#endif
1706#endif /* DO_FLUSHCACHE */ 1682#endif /* DO_FLUSHCACHE */
1707 args[2] = wcache ? 0x02 : 0x82; 1683 args[2] = wcache ? 0x02 : 0x82;
1708 print_flag_on_off(get_wcache, "drive write-caching", wcache); 1684 print_flag_on_off(getset_wcache, "drive write-caching", wcache);
1709#ifdef DO_FLUSHCACHE 1685#ifdef DO_FLUSHCACHE
1710 if (!wcache) 1686 if (!wcache)
1711 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache); 1687 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
@@ -1728,7 +1704,7 @@ static void process_dev(char *devname)
1728#ifndef WIN_STANDBYNOW2 1704#ifndef WIN_STANDBYNOW2
1729#define WIN_STANDBYNOW2 0x94 1705#define WIN_STANDBYNOW2 0x94
1730#endif 1706#endif
1731 if (get_standbynow) printf(" issuing standby command\n"); 1707 printf(" issuing standby command\n");
1732 args[0] = WIN_STANDBYNOW1; 1708 args[0] = WIN_STANDBYNOW1;
1733 ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2); 1709 ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2);
1734 } 1710 }
@@ -1739,23 +1715,20 @@ static void process_dev(char *devname)
1739#ifndef WIN_SLEEPNOW2 1715#ifndef WIN_SLEEPNOW2
1740#define WIN_SLEEPNOW2 0x99 1716#define WIN_SLEEPNOW2 0x99
1741#endif 1717#endif
1742 if (get_sleepnow) printf(" issuing sleep command\n"); 1718 printf(" issuing sleep command\n");
1743 args[0] = WIN_SLEEPNOW1; 1719 args[0] = WIN_SLEEPNOW1;
1744 ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2); 1720 ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2);
1745 } 1721 }
1746 if (set_seagate) { 1722 if (set_seagate) {
1747 args[0] = 0xfb; 1723 args[0] = 0xfb;
1748 if (get_seagate) 1724 printf(" disabling Seagate auto powersaving mode\n");
1749 printf(" disabling Seagate auto powersaving mode\n");
1750 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args); 1725 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
1751 } 1726 }
1752 if (set_standby) { 1727 if (getset_standby == IS_SET) {
1753 args[0] = WIN_SETIDLE1; 1728 args[0] = WIN_SETIDLE1;
1754 args[1] = standby_requested; 1729 args[1] = standby_requested;
1755 if (get_standby) { 1730 print_flag(1, "standby", standby_requested);
1756 print_flag(1, "standby", standby_requested); 1731 interpret_standby(standby_requested);
1757 interpret_standby(standby_requested);
1758 }
1759 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args); 1732 ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
1760 args[1] = 0; 1733 args[1] = 0;
1761 } 1734 }
@@ -1767,21 +1740,20 @@ static void process_dev(char *devname)
1767 bb_perror_msg("read of 512 bytes failed"); 1740 bb_perror_msg("read of 512 bytes failed");
1768 } 1741 }
1769#endif /* HDIO_DRIVE_CMD */ 1742#endif /* HDIO_DRIVE_CMD */
1770 1743 if (getset_mult || get_identity) {
1771 if (get_mult || get_identity) {
1772 multcount = -1; 1744 multcount = -1;
1773 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount)) { 1745 if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount)) {
1774 /* To be coherent with ioctl_or_warn. */ 1746 /* To be coherent with ioctl_or_warn. */
1775 if (get_mult && ENABLE_IOCTL_HEX2STR_ERROR) 1747 if (getset_mult && ENABLE_IOCTL_HEX2STR_ERROR)
1776 bb_perror_msg("HDIO_GET_MULTCOUNT"); 1748 bb_perror_msg("HDIO_GET_MULTCOUNT");
1777 else 1749 else
1778 bb_perror_msg("ioctl %#x failed", HDIO_GET_MULTCOUNT); 1750 bb_perror_msg("ioctl %#x failed", HDIO_GET_MULTCOUNT);
1779 } else if (get_mult) { 1751 } else if (getset_mult) {
1780 printf(fmt, "multcount", multcount); 1752 printf(fmt, "multcount", multcount);
1781 on_off(multcount != 0); 1753 on_off(multcount != 0);
1782 } 1754 }
1783 } 1755 }
1784 if (get_io32bit) { 1756 if (getset_io32bit) {
1785 if (!ioctl_or_warn(fd, HDIO_GET_32BIT, &parm)) { 1757 if (!ioctl_or_warn(fd, HDIO_GET_32BIT, &parm)) {
1786 printf(" IO_support\t=%3ld (", parm); 1758 printf(" IO_support\t=%3ld (", parm);
1787 if (parm == 0) 1759 if (parm == 0)
@@ -1798,14 +1770,12 @@ static void process_dev(char *devname)
1798 printf("\?\?\?)\n"); 1770 printf("\?\?\?)\n");
1799 } 1771 }
1800 } 1772 }
1801 if (get_unmask) { 1773 if (getset_unmask) {
1802 if (!ioctl_or_warn(fd, HDIO_GET_UNMASKINTR, &parm)) 1774 if (!ioctl_or_warn(fd, HDIO_GET_UNMASKINTR, &parm))
1803 print_value_on_off("unmaskirq", parm); 1775 print_value_on_off("unmaskirq", parm);
1804 } 1776 }
1805
1806
1807#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA 1777#if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
1808 if (get_dma) { 1778 if (getset_dma) {
1809 if (!ioctl_or_warn(fd, HDIO_GET_DMA, &parm)) { 1779 if (!ioctl_or_warn(fd, HDIO_GET_DMA, &parm)) {
1810 printf(fmt, "using_dma", parm); 1780 printf(fmt, "using_dma", parm);
1811 if (parm == 8) 1781 if (parm == 8)
@@ -1816,25 +1786,24 @@ static void process_dev(char *devname)
1816 } 1786 }
1817#endif 1787#endif
1818#ifdef HDIO_GET_QDMA 1788#ifdef HDIO_GET_QDMA
1819 if (get_dma_q) { 1789 if (getset_dma_q) {
1820 if (!ioctl_or_warn(fd, HDIO_GET_QDMA, &parm)) 1790 if (!ioctl_or_warn(fd, HDIO_GET_QDMA, &parm))
1821 print_value_on_off("queue_depth", parm); 1791 print_value_on_off("queue_depth", parm);
1822 } 1792 }
1823#endif 1793#endif
1824 if (get_keep) { 1794 if (getset_keep) {
1825 if (!ioctl_or_warn(fd, HDIO_GET_KEEPSETTINGS, &parm)) 1795 if (!ioctl_or_warn(fd, HDIO_GET_KEEPSETTINGS, &parm))
1826 print_value_on_off("keepsettings", parm); 1796 print_value_on_off("keepsettings", parm);
1827 } 1797 }
1828 1798 if (getset_nowerr) {
1829 if (get_nowerr) {
1830 if (!ioctl_or_warn(fd, HDIO_GET_NOWERR, &parm)) 1799 if (!ioctl_or_warn(fd, HDIO_GET_NOWERR, &parm))
1831 print_value_on_off("nowerr", parm); 1800 print_value_on_off("nowerr", parm);
1832 } 1801 }
1833 if (get_readonly) { 1802 if (getset_readonly) {
1834 if (!ioctl_or_warn(fd, BLKROGET, &parm)) 1803 if (!ioctl_or_warn(fd, BLKROGET, &parm))
1835 print_value_on_off("readonly", parm); 1804 print_value_on_off("readonly", parm);
1836 } 1805 }
1837 if (get_readahead) { 1806 if (getset_readahead) {
1838 if (!ioctl_or_warn(fd, BLKRAGET, &parm)) 1807 if (!ioctl_or_warn(fd, BLKRAGET, &parm))
1839 print_value_on_off("readahead", parm); 1808 print_value_on_off("readahead", parm);
1840 } 1809 }
@@ -1912,14 +1881,12 @@ static void process_dev(char *devname)
1912 } 1881 }
1913#endif 1882#endif
1914#if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF 1883#if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
1915 if (set_busstate) { 1884 if (getset_busstate == IS_SET) {
1916 if (get_busstate) { 1885 print_flag(1, "bus state", busstate);
1917 print_flag(1, "bus state", busstate); 1886 bus_state_value(busstate);
1918 bus_state_value(busstate);
1919 }
1920 ioctl_or_warn(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate); 1887 ioctl_or_warn(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate);
1921 } 1888 }
1922 if (get_busstate) { 1889 if (getset_busstate) {
1923 if (!ioctl_or_warn(fd, HDIO_GET_BUSSTATE, &parm)) { 1890 if (!ioctl_or_warn(fd, HDIO_GET_BUSSTATE, &parm)) {
1924 printf(fmt, "bus state", parm); 1891 printf(fmt, "bus state", parm);
1925 bus_state_value(parm); 1892 bus_state_value(parm);
@@ -1975,21 +1942,31 @@ void identify_from_stdin(void);
1975#endif 1942#endif
1976 1943
1977/* busybox specific stuff */ 1944/* busybox specific stuff */
1978static void parse_opts(smallint *get, smallint *set, unsigned long *value, int min, int max) 1945static int parse_opts(unsigned long *value, int min, int max)
1979{ 1946{
1980 if (get) {
1981 *get = 1;
1982 }
1983 if (optarg) { 1947 if (optarg) {
1984 *set = 1;
1985 *value = xatol_range(optarg, min, max); 1948 *value = xatol_range(optarg, min, max);
1949 return IS_SET;
1986 } 1950 }
1951 return IS_GET;
1952}
1953static int parse_opts_0_max(unsigned long *value, int max)
1954{
1955 return parse_opts(value, 0, max);
1956}
1957static int parse_opts_0_1(unsigned long *value)
1958{
1959 return parse_opts(value, 0, 1);
1960}
1961static int parse_opts_0_INTMAX(unsigned long *value)
1962{
1963 return parse_opts(value, 0, INT_MAX);
1987} 1964}
1988 1965
1989static void parse_xfermode(int flag, smallint *get, smallint *set, int *value) 1966static void parse_xfermode(int flag, smallint *get, smallint *set, int *value)
1990{ 1967{
1991 if (flag) { 1968 if (flag) {
1992 *get = 1; 1969 *get = IS_GET;
1993 if (optarg) { 1970 if (optarg) {
1994 *value = translate_xfermode(optarg); 1971 *value = translate_xfermode(optarg);
1995 *set = (*value > -1); 1972 *set = (*value > -1);
@@ -2031,49 +2008,47 @@ int hdparm_main(int argc, char **argv)
2031 IF_FEATURE_HDPARM_GET_IDENTITY(get_identity |= (c == 'i')); 2008 IF_FEATURE_HDPARM_GET_IDENTITY(get_identity |= (c == 'i'));
2032 get_geom |= (c == 'g'); 2009 get_geom |= (c == 'g');
2033 do_flush |= (c == 'f'); 2010 do_flush |= (c == 'f');
2034 if (c == 'u') parse_opts(&get_unmask, &set_unmask, &unmask, 0, 1); 2011 if (c == 'u') getset_unmask = parse_opts_0_1(&unmask);
2035 IF_FEATURE_HDPARM_HDIO_GETSET_DMA(if (c == 'd') parse_opts(&get_dma, &set_dma, &dma, 0, 9)); 2012 IF_FEATURE_HDPARM_HDIO_GETSET_DMA(
2036 if (c == 'n') parse_opts(&get_nowerr, &set_nowerr, &nowerr, 0, 1); 2013 if (c == 'd') getset_dma = parse_opts_0_max(&dma, 9);
2014 )
2015 if (c == 'n') getset_nowerr = parse_opts_0_1(&nowerr);
2037 parse_xfermode((c == 'p'), &noisy_piomode, &set_piomode, &piomode); 2016 parse_xfermode((c == 'p'), &noisy_piomode, &set_piomode, &piomode);
2038 if (c == 'r') parse_opts(&get_readonly, &set_readonly, &readonly, 0, 1); 2017 if (c == 'r') getset_readonly = parse_opts_0_1(&readonly);
2039 if (c == 'm') parse_opts(&get_mult, &set_mult, &mult, 0, INT_MAX /*32*/); 2018 if (c == 'm') getset_mult = parse_opts_0_INTMAX(&mult /*32*/);
2040 if (c == 'c') parse_opts(&get_io32bit, &set_io32bit, &io32bit, 0, INT_MAX /*8*/); 2019 if (c == 'c') getset_io32bit = parse_opts_0_INTMAX(&io32bit /*8*/);
2041 if (c == 'k') parse_opts(&get_keep, &set_keep, &keep, 0, 1); 2020 if (c == 'k') getset_keep = parse_opts_0_1(&keep);
2042 if (c == 'a') parse_opts(&get_readahead, &set_readahead, &Xreadahead, 0, INT_MAX); 2021 if (c == 'a') getset_readahead = parse_opts_0_INTMAX(&Xreadahead);
2043 if (c == 'B') parse_opts(&get_apmmode, &set_apmmode, &apmmode, 1, 255); 2022 if (c == 'B') getset_apmmode = parse_opts(&apmmode, 1, 255);
2044 do_flush |= do_timings |= (c == 't'); 2023 do_flush |= do_timings |= (c == 't');
2045 do_flush |= do_ctimings |= (c == 'T'); 2024 do_flush |= do_ctimings |= (c == 'T');
2046#ifdef HDIO_DRIVE_CMD 2025#ifdef HDIO_DRIVE_CMD
2047 if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, 255); 2026 if (c == 'S') getset_standby = parse_opts_0_max(&standby_requested, 255);
2048 if (c == 'D') parse_opts(&get_defects, &set_defects, &defects, 0, INT_MAX); 2027 if (c == 'D') getset_defects = parse_opts_0_INTMAX(&defects);
2049 if (c == 'P') parse_opts(&get_prefetch, &set_prefetch, &prefetch, 0, INT_MAX); 2028 if (c == 'P') getset_prefetch = parse_opts_0_INTMAX(&prefetch);
2050 parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested); 2029 parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
2051 if (c == 'K') parse_opts(&get_dkeep, &set_dkeep, &prefetch, 0, 1); 2030 if (c == 'K') getset_dkeep = parse_opts_0_1(&prefetch);
2052 if (c == 'A') parse_opts(&get_lookahead, &set_lookahead, &lookahead, 0, 1); 2031 if (c == 'A') getset_lookahead = parse_opts_0_1(&lookahead);
2053 if (c == 'L') parse_opts(&get_doorlock, &set_doorlock, &doorlock, 0, 1); 2032 if (c == 'L') getset_doorlock = parse_opts_0_1(&doorlock);
2054 if (c == 'W') parse_opts(&get_wcache, &set_wcache, &wcache, 0, 1); 2033 if (c == 'W') getset_wcache = parse_opts_0_1(&wcache);
2055 get_powermode |= (c == 'C'); 2034 get_powermode |= (c == 'C');
2056 get_standbynow = set_standbynow |= (c == 'y'); 2035 set_standbynow |= (c == 'y');
2057 get_sleepnow = set_sleepnow |= (c == 'Y'); 2036 set_sleepnow |= (c == 'Y');
2058 reread_partn |= (c == 'z'); 2037 reread_partn |= (c == 'z');
2059 get_seagate = set_seagate |= (c == 'Z'); 2038 set_seagate |= (c == 'Z');
2060#endif 2039#endif
2061 IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') parse_opts(NULL, &unregister_hwif, &hwif, 0, INT_MAX)); 2040 IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') unregister_hwif = parse_opts_0_INTMAX(&hwif));
2062#ifdef HDIO_GET_QDMA 2041#ifdef HDIO_GET_QDMA
2063 if (c == 'Q') { 2042 if (c == 'Q') {
2064#ifdef HDIO_SET_QDMA 2043 getset_dma_q = parse_opts_0_INTMAX(&dma_q);
2065 parse_opts(&get_dma_q, &set_dma_q, &dma_q, 0, INT_MAX);
2066#else
2067 parse_opts(&get_dma_q, NULL, NULL, 0, 0);
2068#endif
2069 } 2044 }
2070#endif 2045#endif
2071 IF_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r')); 2046 IF_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
2072 IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1)); 2047 IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') perform_tristate = parse_opts_0_1(&tristate));
2073 IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') parse_opts(&get_busstate, &set_busstate, &busstate, 0, 2)); 2048 IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') getset_busstate = parse_opts_0_max(&busstate, 2));
2074#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF 2049#if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
2075 if (c == 'R') { 2050 if (c == 'R') {
2076 parse_opts(NULL, &scan_hwif, &hwif_data, 0, INT_MAX); 2051 scan_hwif = parse_opts_0_INTMAX(&hwif_data);
2077 hwif_ctrl = xatoi_u((argv[optind]) ? argv[optind] : ""); 2052 hwif_ctrl = xatoi_u((argv[optind]) ? argv[optind] : "");
2078 hwif_irq = xatoi_u((argv[optind+1]) ? argv[optind+1] : ""); 2053 hwif_irq = xatoi_u((argv[optind+1]) ? argv[optind+1] : "");
2079 /* Move past the 2 additional arguments */ 2054 /* Move past the 2 additional arguments */
@@ -2084,8 +2059,8 @@ int hdparm_main(int argc, char **argv)
2084 } 2059 }
2085 /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */ 2060 /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
2086 if (!flagcount) { 2061 if (!flagcount) {
2087 get_mult = get_io32bit = get_unmask = get_keep = get_readonly = get_readahead = get_geom = 1; 2062 getset_mult = getset_io32bit = getset_unmask = getset_keep = getset_readonly = getset_readahead = get_geom = IS_GET;
2088 IF_FEATURE_HDPARM_HDIO_GETSET_DMA(get_dma = 1); 2063 IF_FEATURE_HDPARM_HDIO_GETSET_DMA(getset_dma = IS_GET);
2089 } 2064 }
2090 argv += optind; 2065 argv += optind;
2091 2066