X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;ds=sidebyside;f=miscutils%2Fhdparm.c;h=eafb0789898817ed9602af38f7e830668f227163;hb=06af2165288cd6516b89001ec9e24992619230e0;hp=512995a4be74b8f8d0bdcf1fe446fba1f2187b0f;hpb=6d8ce170b0be743f761244f467cdaa5f4318638d;p=oweals%2Fbusybox.git diff --git a/miscutils/hdparm.c b/miscutils/hdparm.c index 512995a4b..eafb07898 100644 --- a/miscutils/hdparm.c +++ b/miscutils/hdparm.c @@ -13,24 +13,8 @@ */ #include "busybox.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#if BB_BIG_ENDIAN && !defined(__USE_XOPEN) -# define __USE_XOPEN -#endif -#include - /* device types */ /* ------------ */ #define NO_DEV 0xffff @@ -398,7 +382,8 @@ static const char * const cmd_feat_str[] = { static void identify(uint16_t *id_supplied) ATTRIBUTE_NORETURN; static void identify_from_stdin(void) ATTRIBUTE_NORETURN; - +#else +void identify_from_stdin(void); #endif @@ -478,9 +463,7 @@ static void on_off(unsigned int value); static void print_flag_on_off(unsigned long get_arg, const char *s, unsigned long arg) { - - if (get_arg) - { + if (get_arg) { printf(" setting %s to %ld", s, arg); on_off(arg); } @@ -491,8 +474,7 @@ static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string, { if (ioctl(fd, request, &argp) != 0) bb_perror_msg(" %s", string); - else - { + else { printf(" %s\t= %2ld", str, (unsigned long) argp); on_off((unsigned long) argp); } @@ -501,16 +483,15 @@ static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string, #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY static void print_ascii(uint16_t *p, uint8_t length); -static void xprint_ascii(uint16_t *val ,int i, char * string, int n) +static void xprint_ascii(uint16_t *val ,int i, const char *string, int n) { - if (val[i]) - { - printf("\t%-20s",string); + if (val[i]) { + printf("\t%-20s", string); print_ascii(&val[i], n); } } #endif -/* end of busybox specific stuff */ +/* end of busybox specific stuff */ #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode) @@ -562,7 +543,7 @@ static void print_ascii(uint16_t *p, uint8_t length) { printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff); p++; } - printf("\n"); + puts(""); } // Parse 512 byte disk identification block and print much crap. @@ -589,19 +570,19 @@ static void identify(uint16_t *id_supplied) chksum &= 0xff; /* check if we recognise the device type */ - printf("\n"); - if(!(val[GEN_CONFIG] & NOT_ATA)) + puts(""); + if (!(val[GEN_CONFIG] & NOT_ATA)) { dev = ATA_DEV; printf("ATA device, with "); } - else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL) + else if (val[GEN_CONFIG]==CFA_SUPPORT_VAL) { dev = ATA_DEV; like_std = 4; printf("CompactFlash ATA device, with "); } - else if(!(val[GEN_CONFIG] & NOT_ATAPI)) + else if (!(val[GEN_CONFIG] & NOT_ATAPI)) { dev = ATAPI_DEV; eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT; @@ -610,7 +591,7 @@ static void identify(uint16_t *id_supplied) } else /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/ - bb_error_msg_and_die("Unknown device type"); + bb_error_msg_and_die("unknown device type"); printf("%sremovable media\n", !(val[GEN_CONFIG] & MEDIA_REMOVABLE) ? "non-" : ""); /* Info from the specific configuration word says whether or not the @@ -624,9 +605,9 @@ static void identify(uint16_t *id_supplied) (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) ) { like_std = 5; - if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)) + if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)) printf("powers-up in standby; SET FEATURES subcmd spins-up.\n"); - if(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE)) + if (((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE)) printf("\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n"); } @@ -644,9 +625,9 @@ static void identify(uint16_t *id_supplied) { if (val[MINOR] && (val[MINOR] <= MINOR_MAX)) { - if(like_std < 3) like_std = 3; + if (like_std < 3) like_std = 3; std = actual_ver[val[MINOR]]; - if(std) printf("\n\tUsed: %s ",minor_str[val[MINOR]]); + if (std) printf("\n\tUsed: %s ",minor_str[val[MINOR]]); } /* looks like when they up-issue the std, they obsolete one; @@ -667,17 +648,17 @@ static void identify(uint16_t *id_supplied) like_std = ii; kk = like_std >4 ? like_std-4: 0; } - if(min_std > ii) min_std = ii; + if (min_std > ii) min_std = ii; } jj <<= 1; } - if(like_std < 3) like_std = 3; + if (like_std < 3) like_std = 3; } /* Figure out what standard the device is using if it hasn't told * us. If we know the std, check if the device is using any of * the words from the next level up. It happens. */ - if(like_std < std) like_std = std; + if (like_std < std) like_std = std; if (((std == 5) || (!std && (like_std < 6))) && ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) && @@ -721,7 +702,7 @@ static void identify(uint16_t *id_supplied) else if (like_std > std) printf("& some of %u\n",like_std); else - printf("\n"); + puts(""); } else { @@ -739,7 +720,7 @@ static void identify(uint16_t *id_supplied) jj = val[CDR_MAJOR] >> 1; for (ii = 1; ii <15; ii++) { - if(jj & 0x0001) printf("-%u ", ii); + if (jj & 0x0001) printf("-%u ", ii); jj >>= 1; } } @@ -758,7 +739,7 @@ static void identify(uint16_t *id_supplied) jj = val[GEN_CONFIG] >> 1; for (ii = 1; ii < 15; ii++) { - if(jj & 0x0001) printf("\t%s\n",ata1_cfg_str[ii]); + if (jj & 0x0001) printf("\t%s\n",ata1_cfg_str[ii]); jj >>=1; } } @@ -770,7 +751,7 @@ static void identify(uint16_t *id_supplied) strng = "<=10ms with INTRQ"; else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_50US_VAL) strng ="50us"; - else + else strng = "Unknown"; printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */ @@ -832,8 +813,8 @@ static void identify(uint16_t *id_supplied) if (bbbig > 1000) printf("(%"PRIu64" GB)\n", bbbig/1000); - else - printf("\n"); + else + puts(""); } /* hw support of commands (capabilities) */ @@ -848,8 +829,8 @@ static void identify(uint16_t *id_supplied) if (like_std != 1) { - printf("IORDY%s(can%s be disabled)\n", - !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "", + printf("IORDY%s(can%s be disabled)\n", + !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "", (val[CAPAB_0] & IORDY_OFF) ? "" :"not"); } else @@ -858,7 +839,7 @@ static void identify(uint16_t *id_supplied) if ((like_std == 1) && val[BUF_TYPE]) { printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE], - (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector", + (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector", (val[BUF_TYPE] > 2) ? " with read caching ability" : ""); } @@ -885,7 +866,7 @@ static void identify(uint16_t *id_supplied) if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL)) printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no"); else - printf("\n"); + puts(""); } printf("\tR/W multiple sector transfer: "); if ((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER)) @@ -928,7 +909,7 @@ static void identify(uint16_t *id_supplied) printf("\tOverlap support:"); if (val[PKT_REL]) printf(" %uus to release bus.",val[PKT_REL]); if (val[SVC_NBSY]) printf(" %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]); - printf("\n"); + puts(""); } } @@ -959,7 +940,7 @@ static void identify(uint16_t *id_supplied) err_dma += mode_loop(jj,kk,'u',&have_mode); } if (err_dma || !have_mode) printf("(?)"); - printf("\n"); + puts(""); if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)) printf("\t\tInterleaved DMA support\n"); @@ -970,7 +951,7 @@ static void identify(uint16_t *id_supplied) printf("\t\tCycle time:"); if (val[DMA_TIME_MIN]) printf(" min=%uns",val[DMA_TIME_MIN]); if (val[DMA_TIME_NORM]) printf(" recommended=%uns",val[DMA_TIME_NORM]); - printf("\n"); + puts(""); } } @@ -986,13 +967,13 @@ static void identify(uint16_t *id_supplied) if (jj & 0x0001) printf("pio%d ",ii); jj >>=1; } - printf("\n"); + puts(""); } else if (((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) ) { for (ii = 0; ii <= val[PIO_MODE]>>8; ii++) printf("pio%d ",ii); - printf("\n"); + puts(""); } else printf("unknown\n"); @@ -1004,7 +985,7 @@ static void identify(uint16_t *id_supplied) printf("\t\tCycle time:"); if (val[PIO_NO_FLOW]) printf(" no flow control=%uns", val[PIO_NO_FLOW]); if (val[PIO_FLOW]) printf(" IORDY flow control=%uns", val[PIO_FLOW]); - printf("\n"); + puts(""); } } @@ -1033,7 +1014,7 @@ static void identify(uint16_t *id_supplied) } } /* Removable Media Status Notification feature set */ - if((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) + if ((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) printf("\t%s supported\n", cmd_feat_str[27]); @@ -1064,7 +1045,7 @@ static void identify(uint16_t *id_supplied) printf("\t"); if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, ""); if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED "); - printf("\n"); + puts(""); } } @@ -1078,9 +1059,9 @@ static void identify(uint16_t *id_supplied) strng = " determined by the jumper"; else if ((jj & DEV_DET) == CSEL_VAL) strng = " determined by CSEL"; - else + else strng = ""; - printf("HW reset results:\n\tCBLID- %s Vih\n\tDevice num = %i%s\n", + printf("HW reset results:\n\tCBLID- %s Vih\n\tDevice num = %i%s\n", (val[HWRST_RSLT] & CBLID) ? "above" : "below", !(oo), strng); } @@ -1209,14 +1190,14 @@ static void dump_identity(const struct hd_driveid *id) else printf("off"); } - printf("\n"); + puts(""); - if(!(id->field_valid&1)) + if (!(id->field_valid&1)) printf(" (maybe):"); printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads, - id->cur_sectors, - (BB_BIG_ENDIAN) ? + id->cur_sectors, + (BB_BIG_ENDIAN) ? (long unsigned int)(id->cur_capacity0 << 16) | id->cur_capacity1 : (long unsigned int)(id->cur_capacity1 << 16) | id->cur_capacity0, ((id->capability&2) == 0) ? "no" : "yes"); @@ -1228,7 +1209,7 @@ static void dump_identity(const struct hd_driveid *id) if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&2) printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy); - + if ((id->capability&1) && (id->field_valid&2)) printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time); @@ -1269,7 +1250,7 @@ static void dump_identity(const struct hd_driveid *id) } } if (((id->capability&8) || (id->field_valid&2)) && id->field_valid&4) - { + { printf("\n UDMA modes: "); if (id->dma_ultra & 0x100) printf("*"); if (id->dma_ultra & 0x001) printf("udma0 "); @@ -1414,7 +1395,7 @@ static void do_time(int flag, int fd) if (read_big_block (fd, buf)) return; printf(" Timing cached reads: "); fflush(stdout); - + /* Now do the timing */ iterations = 0; getitimer(ITIMER_REAL, &e1); @@ -1437,9 +1418,9 @@ static void do_time(int flag, int fd) elapsed2 = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0); } while (--iterations); - + elapsed -= elapsed2; - print_timing(BUFCACHE_FACTOR * total_MB, elapsed); + print_timing(BUFCACHE_FACTOR * total_MB, elapsed); flush_buffer_cache(fd); sleep(1); } @@ -1462,7 +1443,7 @@ static void do_time(int flag, int fd) elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0); } while (elapsed < 3.0 && iterations < max_iterations); - + total_MB = iterations * TIMING_BUF_MB; print_timing(total_MB, elapsed); } @@ -1496,7 +1477,7 @@ static void interpret_standby(unsigned int standby) { unsigned int t; - + printf(" ("); if (standby == 0) printf("off"); else if (standby == 252) @@ -1602,9 +1583,9 @@ static void interpret_xfermode(unsigned int xfermode) } #endif /* HDIO_DRIVE_CMD */ -static void print_flag(unsigned long flag, char *s, unsigned long value) +static void print_flag(unsigned long flag, const char *s, unsigned long value) { - if(flag) + if (flag) printf(" setting %s to %ld\n", s, value); } @@ -1620,7 +1601,7 @@ static void process_dev(char *devname) unsigned char args[4] = {WIN_SETFEATURES,0,0,0}; const char *fmt = " %s\t= %2ld"; - fd = bb_xopen(devname, O_RDONLY|O_NONBLOCK); + fd = xopen(devname, O_RDONLY|O_NONBLOCK); printf("\n%s:\n", devname); if (set_readahead) @@ -2040,28 +2021,28 @@ static void process_dev(char *devname) #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY static int fromhex(unsigned char c) { - if (c >= 'a' && c <= 'f') - return 10 + (c - 'a'); - if (c >= '0' && c <= '9') + if (isdigit(c)) return (c - '0'); + if (c >= 'a' && c <= 'f') + return (c - ('a' - 10)); bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c); } static void identify_from_stdin(void) { uint16_t sbuf[256]; - unsigned char buf[1280], *b = (unsigned char *)buf; - int i, count = read(0, buf, 1280); + unsigned char buf[1280]; + unsigned char *b = (unsigned char *)buf; + int i; - if (count != 1280) - bb_error_msg_and_die("read(%d bytes) failed (rc=%d)", 1280, count); + xread(0, buf, 1280); // Convert the newline-separated hex data into an identify block. - for (i = 0; i<256; i++) - { + for (i = 0; i<256; i++) { int j; - for(j=0;j<4;j++) sbuf[i] = (sbuf[i] <<4) + fromhex(*(b++)); + for (j = 0; j < 4; j++) + sbuf[i] = (sbuf[i] << 4) + fromhex(*(b++)); } // Parse the data. @@ -2073,13 +2054,13 @@ static void identify_from_stdin(void) /* busybox specific stuff */ static void parse_opts(unsigned long *get, unsigned long *set, unsigned long *value, int min, int max) { - if (get) { - *get = 1; - } - if (optarg) { - *set = 1; - *value = bb_xgetlarg(optarg, 10, min, max); - } + if (get) { + *get = 1; + } + if (optarg) { + *set = 1; + *value = xatol_range(optarg, min, max); + } } static void parse_xfermode(int flag, unsigned long *get, unsigned long *set, int *value) @@ -2093,18 +2074,18 @@ static void parse_xfermode(int flag, unsigned long *get, unsigned long *set, int } /*------- getopt short options --------*/ -static const char hdparm_options[]= "gfu::n::p:r::m::c::k::a::B:tTh"\ +static const char hdparm_options[] = "gfu::n::p:r::m::c::k::a::B:tTh" USE_FEATURE_HDPARM_GET_IDENTITY("iI") USE_FEATURE_HDPARM_HDIO_GETSET_DMA("d::") #ifdef HDIO_DRIVE_CMD - "S::D::P::X::K::A::L::W::CyYzZ" + "S:D:P:X:K:A:L:W:CyYzZ" #endif USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF("U:") #ifdef HDIO_GET_QDMA #ifdef HDIO_SET_QDMA - "Q:" + "Q:" #else - "Q" + "Q" #endif #endif USE_FEATURE_HDPARM_HDIO_DRIVE_RESET("w") @@ -2113,7 +2094,9 @@ static const char hdparm_options[]= "gfu::n::p:r::m::c::k::a::B:tTh"\ /*-------------------------------------*/ /* our main() routine: */ +int hdparm_main(int argc, char **argv) ATTRIBUTE_NORETURN;; int hdparm_main(int argc, char **argv) ATTRIBUTE_NORETURN; +int hdparm_main(int argc, char **argv); int hdparm_main(int argc, char **argv) { int c; @@ -2139,7 +2122,7 @@ int hdparm_main(int argc, char **argv) do_flush |= do_timings |= (c == 't'); do_flush |= do_ctimings |= (c == 'T'); #ifdef HDIO_DRIVE_CMD - if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, INT_MAX); + if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, INT_MAX); if (c == 'D') parse_opts(&get_defects, &set_defects, &defects, 0, INT_MAX); if (c == 'P') parse_opts(&get_prefetch, &set_prefetch, &prefetch, 0, INT_MAX); parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested); @@ -2153,24 +2136,24 @@ int hdparm_main(int argc, char **argv) reread_partn |= (c == 'z'); get_seagate = set_seagate |= (c == 'Z'); #endif - USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') parse_opts(NULL, &unregister_hwif, &hwif, 0, INT_MAX)); + USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') parse_opts(NULL, &unregister_hwif, &hwif, 0, INT_MAX)); #ifdef HDIO_GET_QDMA if (c == 'Q') { #ifdef HDIO_SET_QDMA parse_opts(&get_dma_q, &set_dma_q, &dma_q, 0, INT_MAX); #else - parse_opts(&get_dma_q, NULL, NULL, 0, 0); + parse_opts(&get_dma_q, NULL, NULL, 0, 0); #endif } -#endif +#endif USE_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r')); - USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1)); - USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') parse_opts(&get_busstate, &set_busstate, &busstate, 0, 2)); + USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1)); + USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') parse_opts(&get_busstate, &set_busstate, &busstate, 0, 2)); #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF if (c == 'R') { - parse_opts(NULL, &scan_hwif, &hwif_data, 0, INT_MAX); - hwif_ctrl = bb_xgetlarg((argv[optind]) ? argv[optind] : "", 10, 0, INT_MAX); - hwif_irq = bb_xgetlarg((argv[optind+1]) ? argv[optind+1] : "", 10, 0, INT_MAX); + parse_opts(NULL, &scan_hwif, &hwif_data, 0, INT_MAX); + hwif_ctrl = xatoi_u((argv[optind]) ? argv[optind] : ""); + hwif_irq = xatoi_u((argv[optind+1]) ? argv[optind+1] : ""); /* Move past the 2 additional arguments */ argv += 2; argc -= 2; @@ -2178,7 +2161,7 @@ int hdparm_main(int argc, char **argv) #endif } /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */ - if (!flagcount){ + if (!flagcount) { get_mult = get_io32bit = get_unmask = get_keep = get_readonly = get_readahead = get_geom = 1; USE_FEATURE_HDPARM_HDIO_GETSET_DMA(get_dma = 1); }