3
0

hdparm.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * hdparm implementation for busybox
  4. *
  5. * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
  6. * Hacked by Tito <farmatito@tiscali.it> for size optimization.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. *
  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. */
  14. //usage:#define hdparm_trivial_usage
  15. //usage: "[OPTIONS] [DEVICE]"
  16. //usage:#define hdparm_full_usage "\n\n"
  17. //usage: " -a Get/set fs readahead"
  18. //usage: "\n -A Set drive read-lookahead flag (0/1)"
  19. //usage: "\n -b Get/set bus state (0 == off, 1 == on, 2 == tristate)"
  20. //usage: "\n -B Set Advanced Power Management setting (1-255)"
  21. //usage: "\n -c Get/set IDE 32-bit IO setting"
  22. //usage: "\n -C Check IDE power mode status"
  23. //usage: IF_FEATURE_HDPARM_HDIO_GETSET_DMA(
  24. //usage: "\n -d Get/set using_dma flag")
  25. //usage: "\n -D Enable/disable drive defect-mgmt"
  26. //usage: "\n -f Flush buffer cache for device on exit"
  27. //usage: "\n -g Display drive geometry"
  28. //usage: "\n -h Display terse usage information"
  29. //usage: IF_FEATURE_HDPARM_GET_IDENTITY(
  30. //usage: "\n -i Display drive identification")
  31. //usage: IF_FEATURE_HDPARM_GET_IDENTITY(
  32. //usage: "\n -I Detailed/current information directly from drive")
  33. //usage: "\n -k Get/set keep_settings_over_reset flag (0/1)"
  34. //usage: "\n -K Set drive keep_features_over_reset flag (0/1)"
  35. //usage: "\n -L Set drive doorlock (0/1) (removable harddisks only)"
  36. //usage: "\n -m Get/set multiple sector count"
  37. //usage: "\n -n Get/set ignore-write-errors flag (0/1)"
  38. //usage: "\n -p Set PIO mode on IDE interface chipset (0,1,2,3,4,...)"
  39. //usage: "\n -P Set drive prefetch count"
  40. /* //usage: "\n -q Change next setting quietly" - not supported ib bbox */
  41. //usage: "\n -Q Get/set DMA tagged-queuing depth (if supported)"
  42. //usage: "\n -r Get/set readonly flag (DANGEROUS to set)"
  43. //usage: IF_FEATURE_HDPARM_HDIO_SCAN_HWIF(
  44. //usage: "\n -R Register an IDE interface (DANGEROUS)")
  45. //usage: "\n -S Set standby (spindown) timeout"
  46. //usage: "\n -t Perform device read timings"
  47. //usage: "\n -T Perform cache read timings"
  48. //usage: "\n -u Get/set unmaskirq flag (0/1)"
  49. //usage: IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(
  50. //usage: "\n -U Unregister an IDE interface (DANGEROUS)")
  51. //usage: "\n -v Defaults; same as -mcudkrag for IDE drives"
  52. //usage: "\n -V Display program version and exit immediately"
  53. //usage: IF_FEATURE_HDPARM_HDIO_DRIVE_RESET(
  54. //usage: "\n -w Perform device reset (DANGEROUS)")
  55. //usage: "\n -W Set drive write-caching flag (0/1) (DANGEROUS)"
  56. //usage: IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(
  57. //usage: "\n -x Tristate device for hotswap (0/1) (DANGEROUS)")
  58. //usage: "\n -X Set IDE xfer mode (DANGEROUS)"
  59. //usage: "\n -y Put IDE drive in standby mode"
  60. //usage: "\n -Y Put IDE drive to sleep"
  61. //usage: "\n -Z Disable Seagate auto-powersaving mode"
  62. //usage: "\n -z Reread partition table"
  63. #include "libbb.h"
  64. #include "common_bufsiz.h"
  65. /* must be _after_ libbb.h: */
  66. #include <linux/hdreg.h>
  67. #include <sys/mount.h>
  68. #if !defined(BLKGETSIZE64)
  69. # define BLKGETSIZE64 _IOR(0x12,114,size_t)
  70. #endif
  71. /* device types */
  72. /* ------------ */
  73. #define NO_DEV 0xffff
  74. #define ATA_DEV 0x0000
  75. #define ATAPI_DEV 0x0001
  76. /* word definitions */
  77. /* ---------------- */
  78. #define GEN_CONFIG 0 /* general configuration */
  79. #define LCYLS 1 /* number of logical cylinders */
  80. #define CONFIG 2 /* specific configuration */
  81. #define LHEADS 3 /* number of logical heads */
  82. #define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
  83. #define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
  84. #define LSECTS 6 /* number of logical sectors/track */
  85. #define START_SERIAL 10 /* ASCII serial number */
  86. #define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
  87. #define BUF_TYPE 20 /* buffer type (ATA-1) */
  88. #define BUFFER__SIZE 21 /* buffer size (ATA-1) */
  89. #define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
  90. #define START_FW_REV 23 /* ASCII firmware revision */
  91. #define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
  92. #define START_MODEL 27 /* ASCII model number */
  93. #define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
  94. #define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
  95. #define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
  96. #define CAPAB_0 49 /* capabilities */
  97. #define CAPAB_1 50
  98. #define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
  99. #define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
  100. #define WHATS_VALID 53 /* what fields are valid */
  101. #define LCYLS_CUR 54 /* current logical cylinders */
  102. #define LHEADS_CUR 55 /* current logical heads */
  103. #define LSECTS_CUR 56 /* current logical sectors/track */
  104. #define CAPACITY_LSB 57 /* current capacity in sectors */
  105. #define CAPACITY_MSB 58
  106. #define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
  107. #define LBA_SECTS_LSB 60 /* LBA: total number of user */
  108. #define LBA_SECTS_MSB 61 /* addressable sectors */
  109. #define SINGLE_DMA 62 /* singleword DMA modes */
  110. #define MULTI_DMA 63 /* multiword DMA modes */
  111. #define ADV_PIO_MODES 64 /* advanced PIO modes supported */
  112. /* multiword DMA xfer cycle time: */
  113. #define DMA_TIME_MIN 65 /* - minimum */
  114. #define DMA_TIME_NORM 66 /* - manufacturer's recommended */
  115. /* minimum PIO xfer cycle time: */
  116. #define PIO_NO_FLOW 67 /* - without flow control */
  117. #define PIO_FLOW 68 /* - with IORDY flow control */
  118. #define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
  119. #define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
  120. #define CDR_MAJOR 73 /* CD ROM: major version number */
  121. #define CDR_MINOR 74 /* CD ROM: minor version number */
  122. #define QUEUE_DEPTH 75 /* queue depth */
  123. #define MAJOR 80 /* major version number */
  124. #define MINOR 81 /* minor version number */
  125. #define CMDS_SUPP_0 82 /* command/feature set(s) supported */
  126. #define CMDS_SUPP_1 83
  127. #define CMDS_SUPP_2 84
  128. #define CMDS_EN_0 85 /* command/feature set(s) enabled */
  129. #define CMDS_EN_1 86
  130. #define CMDS_EN_2 87
  131. #define ULTRA_DMA 88 /* ultra DMA modes */
  132. /* time to complete security erase */
  133. #define ERASE_TIME 89 /* - ordinary */
  134. #define ENH_ERASE_TIME 90 /* - enhanced */
  135. #define ADV_PWR 91 /* current advanced power management level
  136. in low byte, 0x40 in high byte. */
  137. #define PSWD_CODE 92 /* master password revision code */
  138. #define HWRST_RSLT 93 /* hardware reset result */
  139. #define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
  140. #define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
  141. #define LBA_MID 101 /* bits are used, but addr 103 */
  142. #define LBA_48_MSB 102 /* has been reserved for LBA in */
  143. #define LBA_64_MSB 103 /* the future. */
  144. #define RM_STAT 127 /* removable media status notification feature set support */
  145. #define SECU_STATUS 128 /* security status */
  146. #define CFA_PWR_MODE 160 /* CFA power mode 1 */
  147. #define START_MEDIA 176 /* media serial number */
  148. #define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
  149. #define START_MANUF 196 /* media manufacturer I.D. */
  150. #define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
  151. #define INTEGRITY 255 /* integrity word */
  152. /* bit definitions within the words */
  153. /* -------------------------------- */
  154. /* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
  155. #define VALID 0xc000
  156. #define VALID_VAL 0x4000
  157. /* many words are considered invalid if they are either all-0 or all-1 */
  158. #define NOVAL_0 0x0000
  159. #define NOVAL_1 0xffff
  160. /* word 0: gen_config */
  161. #define NOT_ATA 0x8000
  162. #define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
  163. #define MEDIA_REMOVABLE 0x0080
  164. #define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
  165. #define INCOMPLETE 0x0004
  166. #define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
  167. #define DRQ_RESPONSE_TIME 0x0060
  168. #define DRQ_3MS_VAL 0x0000
  169. #define DRQ_INTR_VAL 0x0020
  170. #define DRQ_50US_VAL 0x0040
  171. #define PKT_SIZE_SUPPORTED 0x0003
  172. #define PKT_SIZE_12_VAL 0x0000
  173. #define PKT_SIZE_16_VAL 0x0001
  174. #define EQPT_TYPE 0x1f00
  175. #define SHIFT_EQPT 8
  176. #define CDROM 0x0005
  177. /* word 1: number of logical cylinders */
  178. #define LCYLS_MAX 0x3fff /* maximum allowable value */
  179. /* word 2: specific configuration
  180. * (a) require SET FEATURES to spin-up
  181. * (b) require spin-up to fully reply to IDENTIFY DEVICE
  182. */
  183. #define STBY_NID_VAL 0x37c8 /* (a) and (b) */
  184. #define STBY_ID_VAL 0x738c /* (a) and not (b) */
  185. #define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
  186. #define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
  187. /* words 47 & 59: sector_xfer_max & sector_xfer_cur */
  188. #define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
  189. #define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
  190. /* word 49: capabilities 0 */
  191. #define STD_STBY 0x2000 /* 1=standard values supported (ATA); 0=vendor specific values */
  192. #define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
  193. #define IORDY_OFF 0x0400 /* 1=may be disabled */
  194. #define LBA_SUP 0x0200 /* 1=Logical Block Address support */
  195. #define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
  196. #define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
  197. #define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
  198. #define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
  199. #define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
  200. /* word 50: capabilities 1 */
  201. #define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
  202. /* words 51 & 52: PIO & DMA cycle times */
  203. #define MODE 0xff00 /* the mode is in the MSBs */
  204. /* word 53: whats_valid */
  205. #define OK_W88 0x0004 /* the ultra_dma info is valid */
  206. #define OK_W64_70 0x0002 /* see above for word descriptions */
  207. #define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
  208. /*word 63,88: dma_mode, ultra_dma_mode*/
  209. #define MODE_MAX 7 /* bit definitions force udma <=7 (when
  210. * udma >=8 comes out it'll have to be
  211. * defined in a new dma_mode word!) */
  212. /* word 64: PIO transfer modes */
  213. #define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
  214. #define PIO_MODE_MAX 8 /* but all 8 bits are defined */
  215. /* word 75: queue_depth */
  216. #define DEPTH_BITS 0x001f /* bits used for queue depth */
  217. /* words 80-81: version numbers */
  218. /* NOVAL_0 or NOVAL_1 means device does not report version */
  219. /* word 81: minor version number */
  220. #define MINOR_MAX 0x22
  221. /* words 82-84: cmds/feats supported */
  222. #define CMDS_W82 0x77ff /* word 82: defined command locations*/
  223. #define CMDS_W83 0x3fff /* word 83: defined command locations*/
  224. #define CMDS_W84 0x002f /* word 83: defined command locations*/
  225. #define SUPPORT_48_BIT 0x0400
  226. #define NUM_CMD_FEAT_STR 48
  227. /* words 85-87: cmds/feats enabled */
  228. /* use cmd_feat_str[] to display what commands and features have
  229. * been enabled with words 85-87
  230. */
  231. /* words 89, 90, SECU ERASE TIME */
  232. #define ERASE_BITS 0x00ff
  233. /* word 92: master password revision */
  234. /* NOVAL_0 or NOVAL_1 means no support for master password revision */
  235. /* word 93: hw reset result */
  236. #define CBLID 0x2000 /* CBLID status */
  237. #define RST0 0x0001 /* 1=reset to device #0 */
  238. #define DEV_DET 0x0006 /* how device num determined */
  239. #define JUMPER_VAL 0x0002 /* device num determined by jumper */
  240. #define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
  241. /* word 127: removable media status notification feature set support */
  242. #define RM_STAT_BITS 0x0003
  243. #define RM_STAT_SUP 0x0001
  244. /* word 128: security */
  245. #define SECU_ENABLED 0x0002
  246. #define SECU_LEVEL 0x0010
  247. #define NUM_SECU_STR 6
  248. /* word 160: CFA power mode */
  249. #define VALID_W160 0x8000 /* 1=word valid */
  250. #define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
  251. #define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
  252. #define MAX_AMPS 0x0fff /* value = max current in ma */
  253. /* word 255: integrity */
  254. #define SIG 0x00ff /* signature location */
  255. #define SIG_VAL 0x00a5 /* signature value */
  256. #define TIMING_BUF_MB 1
  257. #define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
  258. #undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
  259. #define IS_GET 1
  260. #define IS_SET 2
  261. enum { fd = 3 };
  262. struct globals {
  263. smallint get_identity, get_geom;
  264. smallint do_flush;
  265. smallint do_ctimings, do_timings;
  266. smallint reread_partn;
  267. smallint set_piomode, noisy_piomode;
  268. smallint getset_readahead;
  269. smallint getset_readonly;
  270. smallint getset_unmask;
  271. smallint getset_mult;
  272. #ifdef HDIO_GET_QDMA
  273. smallint getset_dma_q;
  274. #endif
  275. smallint getset_nowerr;
  276. smallint getset_keep;
  277. smallint getset_io32bit;
  278. int piomode;
  279. unsigned long Xreadahead;
  280. unsigned long readonly;
  281. unsigned long unmask;
  282. unsigned long mult;
  283. #ifdef HDIO_SET_QDMA
  284. unsigned long dma_q;
  285. #endif
  286. unsigned long nowerr;
  287. unsigned long keep;
  288. unsigned long io32bit;
  289. #if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
  290. unsigned long dma;
  291. smallint getset_dma;
  292. #endif
  293. #ifdef HDIO_DRIVE_CMD
  294. smallint set_xfermode, get_xfermode;
  295. smallint getset_dkeep;
  296. smallint getset_standby;
  297. smallint getset_lookahead;
  298. smallint getset_prefetch;
  299. smallint getset_defects;
  300. smallint getset_wcache;
  301. smallint getset_doorlock;
  302. smallint set_seagate;
  303. smallint set_standbynow;
  304. smallint set_sleepnow;
  305. smallint get_powermode;
  306. smallint getset_apmmode;
  307. int xfermode_requested;
  308. unsigned long dkeep;
  309. unsigned long standby_requested; /* 0..255 */
  310. unsigned long lookahead;
  311. unsigned long prefetch;
  312. unsigned long defects;
  313. unsigned long wcache;
  314. unsigned long doorlock;
  315. unsigned long apmmode;
  316. #endif
  317. IF_FEATURE_HDPARM_GET_IDENTITY( smallint get_IDentity;)
  318. IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( smallint getset_busstate;)
  319. IF_FEATURE_HDPARM_HDIO_DRIVE_RESET( smallint perform_reset;)
  320. IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( smallint perform_tristate;)
  321. IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(smallint unregister_hwif;)
  322. IF_FEATURE_HDPARM_HDIO_SCAN_HWIF( smallint scan_hwif;)
  323. IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( unsigned long busstate;)
  324. IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( unsigned long tristate;)
  325. IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(unsigned long hwif;)
  326. #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
  327. unsigned long hwif_data;
  328. unsigned long hwif_ctrl;
  329. unsigned long hwif_irq;
  330. #endif
  331. #ifdef DO_FLUSHCACHE
  332. unsigned char flushcache[4] = { WIN_FLUSHCACHE, 0, 0, 0 };
  333. #endif
  334. } FIX_ALIASING;
  335. #define G (*(struct globals*)bb_common_bufsiz1)
  336. #define get_identity (G.get_identity )
  337. #define get_geom (G.get_geom )
  338. #define do_flush (G.do_flush )
  339. #define do_ctimings (G.do_ctimings )
  340. #define do_timings (G.do_timings )
  341. #define reread_partn (G.reread_partn )
  342. #define set_piomode (G.set_piomode )
  343. #define noisy_piomode (G.noisy_piomode )
  344. #define getset_readahead (G.getset_readahead )
  345. #define getset_readonly (G.getset_readonly )
  346. #define getset_unmask (G.getset_unmask )
  347. #define getset_mult (G.getset_mult )
  348. #define getset_dma_q (G.getset_dma_q )
  349. #define getset_nowerr (G.getset_nowerr )
  350. #define getset_keep (G.getset_keep )
  351. #define getset_io32bit (G.getset_io32bit )
  352. #define piomode (G.piomode )
  353. #define Xreadahead (G.Xreadahead )
  354. #define readonly (G.readonly )
  355. #define unmask (G.unmask )
  356. #define mult (G.mult )
  357. #define dma_q (G.dma_q )
  358. #define nowerr (G.nowerr )
  359. #define keep (G.keep )
  360. #define io32bit (G.io32bit )
  361. #define dma (G.dma )
  362. #define getset_dma (G.getset_dma )
  363. #define set_xfermode (G.set_xfermode )
  364. #define get_xfermode (G.get_xfermode )
  365. #define getset_dkeep (G.getset_dkeep )
  366. #define getset_standby (G.getset_standby )
  367. #define getset_lookahead (G.getset_lookahead )
  368. #define getset_prefetch (G.getset_prefetch )
  369. #define getset_defects (G.getset_defects )
  370. #define getset_wcache (G.getset_wcache )
  371. #define getset_doorlock (G.getset_doorlock )
  372. #define set_seagate (G.set_seagate )
  373. #define set_standbynow (G.set_standbynow )
  374. #define set_sleepnow (G.set_sleepnow )
  375. #define get_powermode (G.get_powermode )
  376. #define getset_apmmode (G.getset_apmmode )
  377. #define xfermode_requested (G.xfermode_requested )
  378. #define dkeep (G.dkeep )
  379. #define standby_requested (G.standby_requested )
  380. #define lookahead (G.lookahead )
  381. #define prefetch (G.prefetch )
  382. #define defects (G.defects )
  383. #define wcache (G.wcache )
  384. #define doorlock (G.doorlock )
  385. #define apmmode (G.apmmode )
  386. #define get_IDentity (G.get_IDentity )
  387. #define getset_busstate (G.getset_busstate )
  388. #define perform_reset (G.perform_reset )
  389. #define perform_tristate (G.perform_tristate )
  390. #define unregister_hwif (G.unregister_hwif )
  391. #define scan_hwif (G.scan_hwif )
  392. #define busstate (G.busstate )
  393. #define tristate (G.tristate )
  394. #define hwif (G.hwif )
  395. #define hwif_data (G.hwif_data )
  396. #define hwif_ctrl (G.hwif_ctrl )
  397. #define hwif_irq (G.hwif_irq )
  398. #define INIT_G() do { \
  399. setup_common_bufsiz(); \
  400. BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
  401. } while (0)
  402. /* Busybox messages and functions */
  403. #if ENABLE_IOCTL_HEX2STR_ERROR
  404. static int ioctl_alt_func(/*int fd,*/ int cmd, unsigned char *args, int alt, const char *string)
  405. {
  406. if (!ioctl(fd, cmd, args))
  407. return 0;
  408. args[0] = alt;
  409. return bb_ioctl_or_warn(fd, cmd, args, string);
  410. }
  411. #define ioctl_alt_or_warn(cmd,args,alt) ioctl_alt_func(cmd,args,alt,#cmd)
  412. #else
  413. static int ioctl_alt_func(/*int fd,*/ int cmd, unsigned char *args, int alt)
  414. {
  415. if (!ioctl(fd, cmd, args))
  416. return 0;
  417. args[0] = alt;
  418. return bb_ioctl_or_warn(fd, cmd, args);
  419. }
  420. #define ioctl_alt_or_warn(cmd,args,alt) ioctl_alt_func(cmd,args,alt)
  421. #endif
  422. static void on_off(int value)
  423. {
  424. puts(value ? " (on)" : " (off)");
  425. }
  426. static void print_flag_on_off(int get_arg, const char *s, unsigned long arg)
  427. {
  428. if (get_arg) {
  429. printf(" setting %s to %lu", s, arg);
  430. on_off(arg);
  431. }
  432. }
  433. static void print_value_on_off(const char *str, unsigned long argp)
  434. {
  435. printf(" %s\t= %2lu", str, argp);
  436. on_off(argp != 0);
  437. }
  438. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  439. static void print_ascii(const char *p, int length)
  440. {
  441. #if BB_BIG_ENDIAN
  442. #define LE_ONLY(x)
  443. enum { ofs = 0 };
  444. #else
  445. #define LE_ONLY(x) x
  446. /* every 16bit word is big-endian (i.e. inverted) */
  447. /* accessing bytes in 1,0, 3,2, 5,4... sequence */
  448. int ofs = 1;
  449. #endif
  450. length *= 2;
  451. /* find first non-space & print it */
  452. while (length && p[ofs] != ' ') {
  453. p++;
  454. LE_ONLY(ofs = -ofs;)
  455. length--;
  456. }
  457. while (length && p[ofs]) {
  458. bb_putchar(p[ofs]);
  459. p++;
  460. LE_ONLY(ofs = -ofs;)
  461. length--;
  462. }
  463. bb_putchar('\n');
  464. #undef LE_ONLY
  465. }
  466. static void xprint_ascii(uint16_t *val, int i, const char *string, int n)
  467. {
  468. if (val[i]) {
  469. printf("\t%-20s", string);
  470. print_ascii((void*)&val[i], n);
  471. }
  472. }
  473. static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
  474. {
  475. uint16_t ii;
  476. uint8_t err_dma = 0;
  477. for (ii = 0; ii <= MODE_MAX; ii++) {
  478. if (mode_sel & 0x0001) {
  479. printf("*%cdma%u ", cc, ii);
  480. if (*have_mode)
  481. err_dma = 1;
  482. *have_mode = 1;
  483. } else if (mode_sup & 0x0001)
  484. printf("%cdma%u ", cc, ii);
  485. mode_sup >>= 1;
  486. mode_sel >>= 1;
  487. }
  488. return err_dma;
  489. }
  490. static const char pkt_str[] ALIGN1 =
  491. "Direct-access device" "\0" /* word 0, bits 12-8 = 00 */
  492. "Sequential-access device" "\0" /* word 0, bits 12-8 = 01 */
  493. "Printer" "\0" /* word 0, bits 12-8 = 02 */
  494. "Processor" "\0" /* word 0, bits 12-8 = 03 */
  495. "Write-once device" "\0" /* word 0, bits 12-8 = 04 */
  496. "CD-ROM" "\0" /* word 0, bits 12-8 = 05 */
  497. "Scanner" "\0" /* word 0, bits 12-8 = 06 */
  498. "Optical memory" "\0" /* word 0, bits 12-8 = 07 */
  499. "Medium changer" "\0" /* word 0, bits 12-8 = 08 */
  500. "Communications device" "\0" /* word 0, bits 12-8 = 09 */
  501. "ACS-IT8 device" "\0" /* word 0, bits 12-8 = 0a */
  502. "ACS-IT8 device" "\0" /* word 0, bits 12-8 = 0b */
  503. "Array controller" "\0" /* word 0, bits 12-8 = 0c */
  504. "Enclosure services" "\0" /* word 0, bits 12-8 = 0d */
  505. "Reduced block command device" "\0" /* word 0, bits 12-8 = 0e */
  506. "Optical card reader/writer" "\0" /* word 0, bits 12-8 = 0f */
  507. ;
  508. static const char ata1_cfg_str[] ALIGN1 = /* word 0 in ATA-1 mode */
  509. "reserved" "\0" /* bit 0 */
  510. "hard sectored" "\0" /* bit 1 */
  511. "soft sectored" "\0" /* bit 2 */
  512. "not MFM encoded " "\0" /* bit 3 */
  513. "head switch time > 15us" "\0" /* bit 4 */
  514. "spindle motor control option" "\0" /* bit 5 */
  515. "fixed drive" "\0" /* bit 6 */
  516. "removable drive" "\0" /* bit 7 */
  517. "disk xfer rate <= 5Mbs" "\0" /* bit 8 */
  518. "disk xfer rate > 5Mbs, <= 10Mbs" "\0" /* bit 9 */
  519. "disk xfer rate > 5Mbs" "\0" /* bit 10 */
  520. "rotational speed tol." "\0" /* bit 11 */
  521. "data strobe offset option" "\0" /* bit 12 */
  522. "track offset option" "\0" /* bit 13 */
  523. "format speed tolerance gap reqd" "\0" /* bit 14 */
  524. "ATAPI" /* bit 14 */
  525. ;
  526. static const char minor_str[] ALIGN1 =
  527. /* word 81 value: */
  528. "Unspecified" "\0" /* 0x0000 */
  529. "ATA-1 X3T9.2 781D prior to rev.4" "\0" /* 0x0001 */
  530. "ATA-1 published, ANSI X3.221-1994" "\0" /* 0x0002 */
  531. "ATA-1 X3T9.2 781D rev.4" "\0" /* 0x0003 */
  532. "ATA-2 published, ANSI X3.279-1996" "\0" /* 0x0004 */
  533. "ATA-2 X3T10 948D prior to rev.2k" "\0" /* 0x0005 */
  534. "ATA-3 X3T10 2008D rev.1" "\0" /* 0x0006 */
  535. "ATA-2 X3T10 948D rev.2k" "\0" /* 0x0007 */
  536. "ATA-3 X3T10 2008D rev.0" "\0" /* 0x0008 */
  537. "ATA-2 X3T10 948D rev.3" "\0" /* 0x0009 */
  538. "ATA-3 published, ANSI X3.298-199x" "\0" /* 0x000a */
  539. "ATA-3 X3T10 2008D rev.6" "\0" /* 0x000b */
  540. "ATA-3 X3T13 2008D rev.7 and 7a" "\0" /* 0x000c */
  541. "ATA/ATAPI-4 X3T13 1153D rev.6" "\0" /* 0x000d */
  542. "ATA/ATAPI-4 T13 1153D rev.13" "\0" /* 0x000e */
  543. "ATA/ATAPI-4 X3T13 1153D rev.7" "\0" /* 0x000f */
  544. "ATA/ATAPI-4 T13 1153D rev.18" "\0" /* 0x0010 */
  545. "ATA/ATAPI-4 T13 1153D rev.15" "\0" /* 0x0011 */
  546. "ATA/ATAPI-4 published, ANSI INCITS 317-1998" "\0" /* 0x0012 */
  547. "ATA/ATAPI-5 T13 1321D rev.3" "\0" /* 0x0013 */
  548. "ATA/ATAPI-4 T13 1153D rev.14" "\0" /* 0x0014 */
  549. "ATA/ATAPI-5 T13 1321D rev.1" "\0" /* 0x0015 */
  550. "ATA/ATAPI-5 published, ANSI INCITS 340-2000" "\0" /* 0x0016 */
  551. "ATA/ATAPI-4 T13 1153D rev.17" "\0" /* 0x0017 */
  552. "ATA/ATAPI-6 T13 1410D rev.0" "\0" /* 0x0018 */
  553. "ATA/ATAPI-6 T13 1410D rev.3a" "\0" /* 0x0019 */
  554. "ATA/ATAPI-7 T13 1532D rev.1" "\0" /* 0x001a */
  555. "ATA/ATAPI-6 T13 1410D rev.2" "\0" /* 0x001b */
  556. "ATA/ATAPI-6 T13 1410D rev.1" "\0" /* 0x001c */
  557. "ATA/ATAPI-7 published, ANSI INCITS 397-2005" "\0" /* 0x001d */
  558. "ATA/ATAPI-7 T13 1532D rev.0" "\0" /* 0x001e */
  559. "reserved" "\0" /* 0x001f */
  560. "reserved" "\0" /* 0x0020 */
  561. "ATA/ATAPI-7 T13 1532D rev.4a" "\0" /* 0x0021 */
  562. "ATA/ATAPI-6 published, ANSI INCITS 361-2002" "\0" /* 0x0022 */
  563. "reserved" /* 0x0023-0xfffe */
  564. ;
  565. static const char actual_ver[MINOR_MAX + 2] ALIGN1 = {
  566. /* word 81 value: */
  567. 0, /* 0x0000 WARNING: actual_ver[] array */
  568. 1, /* 0x0001 WARNING: corresponds */
  569. 1, /* 0x0002 WARNING: *exactly* */
  570. 1, /* 0x0003 WARNING: to the ATA/ */
  571. 2, /* 0x0004 WARNING: ATAPI version */
  572. 2, /* 0x0005 WARNING: listed in */
  573. 3, /* 0x0006 WARNING: the */
  574. 2, /* 0x0007 WARNING: minor_str */
  575. 3, /* 0x0008 WARNING: array */
  576. 2, /* 0x0009 WARNING: above. */
  577. 3, /* 0x000a WARNING: */
  578. 3, /* 0x000b WARNING: If you change */
  579. 3, /* 0x000c WARNING: that one, */
  580. 4, /* 0x000d WARNING: change this one */
  581. 4, /* 0x000e WARNING: too!!! */
  582. 4, /* 0x000f */
  583. 4, /* 0x0010 */
  584. 4, /* 0x0011 */
  585. 4, /* 0x0012 */
  586. 5, /* 0x0013 */
  587. 4, /* 0x0014 */
  588. 5, /* 0x0015 */
  589. 5, /* 0x0016 */
  590. 4, /* 0x0017 */
  591. 6, /* 0x0018 */
  592. 6, /* 0x0019 */
  593. 7, /* 0x001a */
  594. 6, /* 0x001b */
  595. 6, /* 0x001c */
  596. 7, /* 0x001d */
  597. 7, /* 0x001e */
  598. 0, /* 0x001f */
  599. 0, /* 0x0020 */
  600. 7, /* 0x0021 */
  601. 6, /* 0x0022 */
  602. 0 /* 0x0023-0xfffe */
  603. };
  604. static const char cmd_feat_str[] ALIGN1 =
  605. "" "\0" /* word 82 bit 15: obsolete */
  606. "NOP cmd" "\0" /* word 82 bit 14 */
  607. "READ BUFFER cmd" "\0" /* word 82 bit 13 */
  608. "WRITE BUFFER cmd" "\0" /* word 82 bit 12 */
  609. "" "\0" /* word 82 bit 11: obsolete */
  610. "Host Protected Area feature set" "\0" /* word 82 bit 10 */
  611. "DEVICE RESET cmd" "\0" /* word 82 bit 9 */
  612. "SERVICE interrupt" "\0" /* word 82 bit 8 */
  613. "Release interrupt" "\0" /* word 82 bit 7 */
  614. "Look-ahead" "\0" /* word 82 bit 6 */
  615. "Write cache" "\0" /* word 82 bit 5 */
  616. "PACKET command feature set" "\0" /* word 82 bit 4 */
  617. "Power Management feature set" "\0" /* word 82 bit 3 */
  618. "Removable Media feature set" "\0" /* word 82 bit 2 */
  619. "Security Mode feature set" "\0" /* word 82 bit 1 */
  620. "SMART feature set" "\0" /* word 82 bit 0 */
  621. /* -------------- */
  622. "" "\0" /* word 83 bit 15: !valid bit */
  623. "" "\0" /* word 83 bit 14: valid bit */
  624. "FLUSH CACHE EXT cmd" "\0" /* word 83 bit 13 */
  625. "Mandatory FLUSH CACHE cmd " "\0" /* word 83 bit 12 */
  626. "Device Configuration Overlay feature set " "\0"
  627. "48-bit Address feature set " "\0" /* word 83 bit 10 */
  628. "" "\0"
  629. "SET MAX security extension" "\0" /* word 83 bit 8 */
  630. "Address Offset Reserved Area Boot" "\0" /* word 83 bit 7 */
  631. "SET FEATURES subcommand required to spinup after power up" "\0"
  632. "Power-Up In Standby feature set" "\0" /* word 83 bit 5 */
  633. "Removable Media Status Notification feature set" "\0"
  634. "Adv. Power Management feature set" "\0" /* word 83 bit 3 */
  635. "CFA feature set" "\0" /* word 83 bit 2 */
  636. "READ/WRITE DMA QUEUED" "\0" /* word 83 bit 1 */
  637. "DOWNLOAD MICROCODE cmd" "\0" /* word 83 bit 0 */
  638. /* -------------- */
  639. "" "\0" /* word 84 bit 15: !valid bit */
  640. "" "\0" /* word 84 bit 14: valid bit */
  641. "" "\0" /* word 84 bit 13: reserved */
  642. "" "\0" /* word 84 bit 12: reserved */
  643. "" "\0" /* word 84 bit 11: reserved */
  644. "" "\0" /* word 84 bit 10: reserved */
  645. "" "\0" /* word 84 bit 9: reserved */
  646. "" "\0" /* word 84 bit 8: reserved */
  647. "" "\0" /* word 84 bit 7: reserved */
  648. "" "\0" /* word 84 bit 6: reserved */
  649. "General Purpose Logging feature set" "\0" /* word 84 bit 5 */
  650. "" "\0" /* word 84 bit 4: reserved */
  651. "Media Card Pass Through Command feature set " "\0"
  652. "Media serial number " "\0" /* word 84 bit 2 */
  653. "SMART self-test " "\0" /* word 84 bit 1 */
  654. "SMART error logging " /* word 84 bit 0 */
  655. ;
  656. static const char secu_str[] ALIGN1 =
  657. "supported" "\0" /* word 128, bit 0 */
  658. "enabled" "\0" /* word 128, bit 1 */
  659. "locked" "\0" /* word 128, bit 2 */
  660. "frozen" "\0" /* word 128, bit 3 */
  661. "expired: security count" "\0" /* word 128, bit 4 */
  662. "supported: enhanced erase" /* word 128, bit 5 */
  663. ;
  664. // Parse 512 byte disk identification block and print much crap.
  665. static void identify(uint16_t *val) NORETURN;
  666. static void identify(uint16_t *val)
  667. {
  668. uint16_t ii, jj, kk;
  669. uint16_t like_std = 1, std = 0, min_std = 0xffff;
  670. uint16_t dev = NO_DEV, eqpt = NO_DEV;
  671. uint8_t have_mode = 0, err_dma = 0;
  672. uint8_t chksum = 0;
  673. uint32_t ll, mm, nn, oo;
  674. uint64_t bbbig; /* (:) */
  675. const char *strng;
  676. #if BB_BIG_ENDIAN
  677. uint16_t buf[256];
  678. // Adjust for endianness
  679. swab(val, buf, sizeof(buf));
  680. val = buf;
  681. #endif
  682. /* check if we recognize the device type */
  683. bb_putchar('\n');
  684. if (!(val[GEN_CONFIG] & NOT_ATA)) {
  685. dev = ATA_DEV;
  686. printf("ATA device, with ");
  687. } else if (val[GEN_CONFIG]==CFA_SUPPORT_VAL) {
  688. dev = ATA_DEV;
  689. like_std = 4;
  690. printf("CompactFlash ATA device, with ");
  691. } else if (!(val[GEN_CONFIG] & NOT_ATAPI)) {
  692. dev = ATAPI_DEV;
  693. eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
  694. printf("ATAPI %s, with ", eqpt <= 0xf ? nth_string(pkt_str, eqpt) : "unknown");
  695. like_std = 3;
  696. } else
  697. /* "Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n" */
  698. bb_error_msg_and_die("unknown device type");
  699. printf("%sremovable media\n", !(val[GEN_CONFIG] & MEDIA_REMOVABLE) ? "non-" : "");
  700. /* Info from the specific configuration word says whether or not the
  701. * ID command completed correctly. It is only defined, however in
  702. * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
  703. * standards. Since the values allowed for this word are extremely
  704. * specific, it should be safe to check it now, even though we don't
  705. * know yet what standard this device is using.
  706. */
  707. if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)
  708. || (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL)
  709. ) {
  710. like_std = 5;
  711. if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL))
  712. puts("powers-up in standby; SET FEATURES subcmd spins-up.");
  713. if (((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE))
  714. puts("\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n");
  715. }
  716. /* output the model and serial numbers and the fw revision */
  717. xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
  718. xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
  719. xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
  720. xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
  721. xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
  722. /* major & minor standards version number (Note: these words were not
  723. * defined until ATA-3 & the CDROM std uses different words.) */
  724. printf("Standards:");
  725. if (eqpt != CDROM) {
  726. if (val[MINOR] && (val[MINOR] <= MINOR_MAX)) {
  727. if (like_std < 3) like_std = 3;
  728. std = actual_ver[val[MINOR]];
  729. if (std)
  730. printf("\n\tUsed: %s ", nth_string(minor_str, val[MINOR]));
  731. }
  732. /* looks like when they up-issue the std, they obsolete one;
  733. * thus, only the newest 4 issues need be supported. (That's
  734. * what "kk" and "min_std" are all about.) */
  735. if (val[MAJOR] && (val[MAJOR] != NOVAL_1)) {
  736. printf("\n\tSupported: ");
  737. jj = val[MAJOR] << 1;
  738. kk = like_std >4 ? like_std-4: 0;
  739. for (ii = 14; (ii >0)&&(ii>kk); ii--) {
  740. if (jj & 0x8000) {
  741. printf("%u ", ii);
  742. if (like_std < ii) {
  743. like_std = ii;
  744. kk = like_std >4 ? like_std-4: 0;
  745. }
  746. if (min_std > ii) min_std = ii;
  747. }
  748. jj <<= 1;
  749. }
  750. if (like_std < 3) like_std = 3;
  751. }
  752. /* Figure out what standard the device is using if it hasn't told
  753. * us. If we know the std, check if the device is using any of
  754. * the words from the next level up. It happens.
  755. */
  756. if (like_std < std) like_std = std;
  757. if (((std == 5) || (!std && (like_std < 6))) &&
  758. ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  759. (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
  760. ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
  761. ( val[CMDS_SUPP_2] & CMDS_W84) ) )
  762. ) {
  763. like_std = 6;
  764. } else if (((std == 4) || (!std && (like_std < 5))) &&
  765. ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
  766. (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
  767. ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  768. (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
  769. {
  770. like_std = 5;
  771. } else if (((std == 3) || (!std && (like_std < 4))) &&
  772. ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  773. ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
  774. (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
  775. (( val[CAPAB_1] & VALID) == VALID_VAL) ||
  776. (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
  777. (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) )
  778. ) {
  779. like_std = 4;
  780. } else if (((std == 2) || (!std && (like_std < 3)))
  781. && ((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
  782. ) {
  783. like_std = 3;
  784. } else if (((std == 1) || (!std && (like_std < 2))) &&
  785. ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
  786. (val[WHATS_VALID] & OK_W64_70)) )
  787. {
  788. like_std = 2;
  789. }
  790. if (!std)
  791. printf("\n\tLikely used: %u\n", like_std);
  792. else if (like_std > std)
  793. printf("& some of %u\n", like_std);
  794. else
  795. bb_putchar('\n');
  796. } else {
  797. /* TBD: do CDROM stuff more thoroughly. For now... */
  798. kk = 0;
  799. if (val[CDR_MINOR] == 9) {
  800. kk = 1;
  801. printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
  802. }
  803. if (val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1)) {
  804. kk = 1;
  805. printf("\n\tSupported: CD-ROM ATAPI");
  806. jj = val[CDR_MAJOR] >> 1;
  807. for (ii = 1; ii < 15; ii++) {
  808. if (jj & 0x0001) printf("-%u ", ii);
  809. jj >>= 1;
  810. }
  811. }
  812. puts(kk ? "" : "\n\tLikely used CD-ROM ATAPI-1");
  813. /* the cdrom stuff is more like ATA-2 than anything else, so: */
  814. like_std = 2;
  815. }
  816. if (min_std == 0xffff)
  817. min_std = like_std > 4 ? like_std - 3 : 1;
  818. puts("Configuration:");
  819. /* more info from the general configuration word */
  820. if ((eqpt != CDROM) && (like_std == 1)) {
  821. jj = val[GEN_CONFIG] >> 1;
  822. for (ii = 1; ii < 15; ii++) {
  823. if (jj & 0x0001)
  824. printf("\t%s\n", nth_string(ata1_cfg_str, ii));
  825. jj >>=1;
  826. }
  827. }
  828. if (dev == ATAPI_DEV) {
  829. if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_3MS_VAL)
  830. strng = "3ms";
  831. else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_INTR_VAL)
  832. strng = "<=10ms with INTRQ";
  833. else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_50US_VAL)
  834. strng ="50us";
  835. else
  836. strng = "unknown";
  837. printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */
  838. if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_12_VAL)
  839. strng = "12 bytes";
  840. else if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_16_VAL)
  841. strng = "16 bytes";
  842. else
  843. strng = "unknown";
  844. puts(strng);
  845. } else {
  846. /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
  847. ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
  848. mm = 0;
  849. bbbig = 0;
  850. if ((ll > 0x00FBFC10) && (!val[LCYLS]))
  851. puts("\tCHS addressing not supported");
  852. else {
  853. jj = val[WHATS_VALID] & OK_W54_58;
  854. printf("\tLogical\t\tmax\tcurrent\n"
  855. "\tcylinders\t%u\t%u\n"
  856. "\theads\t\t%u\t%u\n"
  857. "\tsectors/track\t%u\t%u\n"
  858. "\t--\n",
  859. val[LCYLS],
  860. jj ? val[LCYLS_CUR] : 0,
  861. val[LHEADS],
  862. jj ? val[LHEADS_CUR] : 0,
  863. val[LSECTS],
  864. jj ? val[LSECTS_CUR] : 0);
  865. if ((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES]))
  866. printf("\tbytes/track: %u\tbytes/sector: %u\n",
  867. val[TRACK_BYTES], val[SECT_BYTES]);
  868. if (jj) {
  869. mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
  870. if (like_std < 3) {
  871. /* check Endian of capacity bytes */
  872. nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
  873. oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
  874. if (abs(mm - nn) > abs(oo - nn))
  875. mm = oo;
  876. }
  877. printf("\tCHS current addressable sectors:%11u\n", mm);
  878. }
  879. }
  880. /* LBA addressing */
  881. printf("\tLBA user addressable sectors:%11u\n", ll);
  882. if (((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
  883. && (val[CMDS_SUPP_1] & SUPPORT_48_BIT)
  884. ) {
  885. bbbig = (uint64_t)val[LBA_64_MSB] << 48 |
  886. (uint64_t)val[LBA_48_MSB] << 32 |
  887. (uint64_t)val[LBA_MID] << 16 |
  888. val[LBA_LSB];
  889. printf("\tLBA48 user addressable sectors:%11"PRIu64"\n", bbbig);
  890. }
  891. if (!bbbig)
  892. bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
  893. printf("\tdevice size with M = 1024*1024: %11"PRIu64" MBytes\n", bbbig>>11);
  894. bbbig = (bbbig << 9) / 1000000;
  895. printf("\tdevice size with M = 1000*1000: %11"PRIu64" MBytes ", bbbig);
  896. if (bbbig > 1000)
  897. printf("(%"PRIu64" GB)\n", bbbig/1000);
  898. else
  899. bb_putchar('\n');
  900. }
  901. /* hw support of commands (capabilities) */
  902. printf("Capabilities:\n\t");
  903. if (dev == ATAPI_DEV) {
  904. if (eqpt != CDROM && (val[CAPAB_0] & CMD_Q_SUP))
  905. printf("Cmd queuing, ");
  906. if (val[CAPAB_0] & OVLP_SUP)
  907. printf("Cmd overlap, ");
  908. }
  909. if (val[CAPAB_0] & LBA_SUP) printf("LBA, ");
  910. if (like_std != 1) {
  911. printf("IORDY%s(can%s be disabled)\n",
  912. !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "",
  913. (val[CAPAB_0] & IORDY_OFF) ? "" :"not");
  914. } else
  915. puts("no IORDY");
  916. if ((like_std == 1) && val[BUF_TYPE]) {
  917. printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE],
  918. (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector",
  919. (val[BUF_TYPE] > 2) ? " with read caching ability" : "");
  920. }
  921. if ((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1))) {
  922. printf("\tBuffer size: %.1fkB\n", (float)val[BUFFER__SIZE]/2);
  923. }
  924. if ((min_std < 4) && (val[RW_LONG])) {
  925. printf("\tbytes avail on r/w long: %u\n", val[RW_LONG]);
  926. }
  927. if ((eqpt != CDROM) && (like_std > 3)) {
  928. printf("\tQueue depth: %u\n", (val[QUEUE_DEPTH] & DEPTH_BITS) + 1);
  929. }
  930. if (dev == ATA_DEV) {
  931. if (like_std == 1)
  932. printf("\tCan%s perform double-word IO\n", (!val[DWORD_IO]) ? "not" : "");
  933. else {
  934. printf("\tStandby timer values: spec'd by %s",
  935. (val[CAPAB_0] & STD_STBY) ? "standard" : "vendor");
  936. if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
  937. printf(", %s device specific minimum\n",
  938. (val[CAPAB_1] & MIN_STANDBY_TIMER) ? "with" : "no");
  939. else
  940. bb_putchar('\n');
  941. }
  942. printf("\tR/W multiple sector transfer: ");
  943. if ((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
  944. puts("not supported");
  945. else {
  946. printf("Max = %u\tCurrent = ", val[SECTOR_XFER_MAX] & SECTOR_XFER);
  947. if (val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
  948. printf("%u\n", val[SECTOR_XFER_CUR] & SECTOR_XFER);
  949. else
  950. puts("?");
  951. }
  952. if ((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008)) {
  953. /* We print out elsewhere whether the APM feature is enabled or
  954. * not. If it's not enabled, let's not repeat the info; just print
  955. * nothing here. */
  956. printf("\tAdvancedPM level: ");
  957. if ((val[ADV_PWR] & 0xFF00) == 0x4000) {
  958. uint8_t apm_level = val[ADV_PWR] & 0x00FF;
  959. printf("%u (0x%x)\n", apm_level, apm_level);
  960. }
  961. else
  962. printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
  963. }
  964. if (like_std > 5 && val[ACOUSTIC]) {
  965. printf("\tRecommended acoustic management value: %u, current value: %u\n",
  966. (val[ACOUSTIC] >> 8) & 0x00ff,
  967. val[ACOUSTIC] & 0x00ff);
  968. }
  969. } else {
  970. /* ATAPI */
  971. if (eqpt != CDROM && (val[CAPAB_0] & SWRST_REQ))
  972. puts("\tATA sw reset required");
  973. if (val[PKT_REL] || val[SVC_NBSY]) {
  974. printf("\tOverlap support:");
  975. if (val[PKT_REL])
  976. printf(" %uus to release bus.", val[PKT_REL]);
  977. if (val[SVC_NBSY])
  978. printf(" %uus to clear BSY after SERVICE cmd.",
  979. val[SVC_NBSY]);
  980. bb_putchar('\n');
  981. }
  982. }
  983. /* DMA stuff. Check that only one DMA mode is selected. */
  984. printf("\tDMA: ");
  985. if (!(val[CAPAB_0] & DMA_SUP))
  986. puts("not supported");
  987. else {
  988. if (val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
  989. printf(" sdma%u\n", (val[DMA_MODE] & MODE) >> 8);
  990. if (val[SINGLE_DMA]) {
  991. jj = val[SINGLE_DMA];
  992. kk = val[SINGLE_DMA] >> 8;
  993. err_dma += mode_loop(jj, kk, 's', &have_mode);
  994. }
  995. if (val[MULTI_DMA]) {
  996. jj = val[MULTI_DMA];
  997. kk = val[MULTI_DMA] >> 8;
  998. err_dma += mode_loop(jj, kk, 'm', &have_mode);
  999. }
  1000. if ((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) {
  1001. jj = val[ULTRA_DMA];
  1002. kk = val[ULTRA_DMA] >> 8;
  1003. err_dma += mode_loop(jj, kk, 'u', &have_mode);
  1004. }
  1005. if (err_dma || !have_mode) printf("(?)");
  1006. bb_putchar('\n');
  1007. if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
  1008. puts("\t\tInterleaved DMA support");
  1009. if ((val[WHATS_VALID] & OK_W64_70)
  1010. && (val[DMA_TIME_MIN] || val[DMA_TIME_NORM])
  1011. ) {
  1012. printf("\t\tCycle time:");
  1013. if (val[DMA_TIME_MIN]) printf(" min=%uns", val[DMA_TIME_MIN]);
  1014. if (val[DMA_TIME_NORM]) printf(" recommended=%uns", val[DMA_TIME_NORM]);
  1015. bb_putchar('\n');
  1016. }
  1017. }
  1018. /* Programmed IO stuff */
  1019. printf("\tPIO: ");
  1020. /* If a drive supports mode n (e.g. 3), it also supports all modes less
  1021. * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
  1022. if ((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP)) {
  1023. jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
  1024. for (ii = 0; ii <= PIO_MODE_MAX; ii++) {
  1025. if (jj & 0x0001) printf("pio%d ", ii);
  1026. jj >>=1;
  1027. }
  1028. bb_putchar('\n');
  1029. } else if (((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE)) {
  1030. for (ii = 0; ii <= val[PIO_MODE]>>8; ii++)
  1031. printf("pio%d ", ii);
  1032. bb_putchar('\n');
  1033. } else
  1034. puts("unknown");
  1035. if (val[WHATS_VALID] & OK_W64_70) {
  1036. if (val[PIO_NO_FLOW] || val[PIO_FLOW]) {
  1037. printf("\t\tCycle time:");
  1038. if (val[PIO_NO_FLOW])
  1039. printf(" no flow control=%uns", val[PIO_NO_FLOW]);
  1040. if (val[PIO_FLOW])
  1041. printf(" IORDY flow control=%uns", val[PIO_FLOW]);
  1042. bb_putchar('\n');
  1043. }
  1044. }
  1045. if ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) {
  1046. puts("Commands/features:\n"
  1047. "\tEnabled\tSupported:");
  1048. jj = val[CMDS_SUPP_0];
  1049. kk = val[CMDS_EN_0];
  1050. for (ii = 0; ii < NUM_CMD_FEAT_STR; ii++) {
  1051. const char *feat_str = nth_string(cmd_feat_str, ii);
  1052. if ((jj & 0x8000) && (*feat_str != '\0')) {
  1053. printf("\t%s\t%s\n", (kk & 0x8000) ? " *" : "", feat_str);
  1054. }
  1055. jj <<= 1;
  1056. kk <<= 1;
  1057. if (ii % 16 == 15) {
  1058. jj = val[CMDS_SUPP_0+1+(ii/16)];
  1059. kk = val[CMDS_EN_0+1+(ii/16)];
  1060. }
  1061. if (ii == 31) {
  1062. if ((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
  1063. ii +=16;
  1064. }
  1065. }
  1066. }
  1067. /* Removable Media Status Notification feature set */
  1068. if ((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
  1069. printf("\t%s supported\n", nth_string(cmd_feat_str, 27));
  1070. /* security */
  1071. if ((eqpt != CDROM) && (like_std > 3)
  1072. && (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME])
  1073. ) {
  1074. puts("Security:");
  1075. if (val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
  1076. printf("\tMaster password revision code = %u\n", val[PSWD_CODE]);
  1077. jj = val[SECU_STATUS];
  1078. if (jj) {
  1079. for (ii = 0; ii < NUM_SECU_STR; ii++) {
  1080. printf("\t%s\t%s\n",
  1081. (!(jj & 0x0001)) ? "not" : "",
  1082. nth_string(secu_str, ii));
  1083. jj >>=1;
  1084. }
  1085. if (val[SECU_STATUS] & SECU_ENABLED) {
  1086. printf("\tSecurity level %s\n",
  1087. (val[SECU_STATUS] & SECU_LEVEL) ? "maximum" : "high");
  1088. }
  1089. }
  1090. jj = val[ERASE_TIME] & ERASE_BITS;
  1091. kk = val[ENH_ERASE_TIME] & ERASE_BITS;
  1092. if (jj || kk) {
  1093. bb_putchar('\t');
  1094. if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, "");
  1095. if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED ");
  1096. bb_putchar('\n');
  1097. }
  1098. }
  1099. /* reset result */
  1100. jj = val[HWRST_RSLT];
  1101. if ((jj & VALID) == VALID_VAL) {
  1102. oo = (jj & RST0);
  1103. if (!oo)
  1104. jj >>= 8;
  1105. if ((jj & DEV_DET) == JUMPER_VAL)
  1106. strng = " determined by the jumper";
  1107. else if ((jj & DEV_DET) == CSEL_VAL)
  1108. strng = " determined by CSEL";
  1109. else
  1110. strng = "";
  1111. printf("HW reset results:\n"
  1112. "\tCBLID- %s Vih\n"
  1113. "\tDevice num = %i%s\n",
  1114. (val[HWRST_RSLT] & CBLID) ? "above" : "below",
  1115. !(oo), strng);
  1116. }
  1117. /* more stuff from std 5 */
  1118. if ((like_std > 4) && (eqpt != CDROM)) {
  1119. if (val[CFA_PWR_MODE] & VALID_W160) {
  1120. printf("CFA power mode 1:\n"
  1121. "\t%s%s\n",
  1122. (val[CFA_PWR_MODE] & PWR_MODE_OFF) ? "disabled" : "enabled",
  1123. (val[CFA_PWR_MODE] & PWR_MODE_REQ) ? " and required by some commands" : "");
  1124. if (val[CFA_PWR_MODE] & MAX_AMPS)
  1125. printf("\tMaximum current = %uma\n", val[CFA_PWR_MODE] & MAX_AMPS);
  1126. }
  1127. if ((val[INTEGRITY] & SIG) == SIG_VAL) {
  1128. printf("Checksum: %scorrect\n", chksum ? "in" : "");
  1129. }
  1130. }
  1131. exit(EXIT_SUCCESS);
  1132. }
  1133. #endif
  1134. // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
  1135. // then the HDIO_GET_IDENTITY only returned 142 bytes.
  1136. // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
  1137. // and HDIO_GET_IDENTITY returns 512 bytes. But the latest
  1138. // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
  1139. // (which they should, but they should just return -EINVAL).
  1140. //
  1141. // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
  1142. // On a really old system, it will not, and we will be confused.
  1143. // Too bad, really.
  1144. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  1145. static const char cfg_str[] ALIGN1 =
  1146. """\0" "HardSect""\0" "SoftSect""\0" "NotMFM""\0"
  1147. "HdSw>15uSec""\0" "SpinMotCtl""\0" "Fixed""\0" "Removeable""\0"
  1148. "DTR<=5Mbs""\0" "DTR>5Mbs""\0" "DTR>10Mbs""\0" "RotSpdTol>.5%""\0"
  1149. "dStbOff""\0" "TrkOff""\0" "FmtGapReq""\0" "nonMagnetic"
  1150. ;
  1151. static const char BuffType[] ALIGN1 =
  1152. "unknown""\0" "1Sect""\0" "DualPort""\0" "DualPortCache"
  1153. ;
  1154. static NOINLINE void dump_identity(const struct hd_driveid *id)
  1155. {
  1156. int i;
  1157. const unsigned short *id_regs = (const void*) id;
  1158. printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
  1159. id->model, id->fw_rev, id->serial_no);
  1160. for (i = 0; i <= 15; i++) {
  1161. if (id->config & (1<<i))
  1162. printf(" %s", nth_string(cfg_str, i));
  1163. }
  1164. printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n"
  1165. " BuffType=(%u) %s, BuffSize=%ukB, MaxMultSect=%u",
  1166. id->cyls, id->heads, id->sectors, id->track_bytes,
  1167. id->sector_bytes, id->ecc_bytes,
  1168. id->buf_type,
  1169. nth_string(BuffType, (id->buf_type > 3) ? 0 : id->buf_type),
  1170. id->buf_size/2, id->max_multsect);
  1171. if (id->max_multsect) {
  1172. printf(", MultSect=");
  1173. if (!(id->multsect_valid & 1))
  1174. printf("?%u?", id->multsect);
  1175. else if (id->multsect)
  1176. printf("%u", id->multsect);
  1177. else
  1178. printf("off");
  1179. }
  1180. bb_putchar('\n');
  1181. if (!(id->field_valid & 1))
  1182. printf(" (maybe):");
  1183. printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s", id->cur_cyls, id->cur_heads,
  1184. id->cur_sectors,
  1185. (BB_BIG_ENDIAN) ?
  1186. (unsigned long)(id->cur_capacity0 << 16) | id->cur_capacity1 :
  1187. (unsigned long)(id->cur_capacity1 << 16) | id->cur_capacity0,
  1188. ((id->capability&2) == 0) ? "no" : "yes");
  1189. if (id->capability & 2)
  1190. printf(", LBAsects=%u", id->lba_capacity);
  1191. printf("\n IORDY=%s",
  1192. (id->capability & 8)
  1193. ? ((id->capability & 4) ? "on/off" : "yes")
  1194. : "no");
  1195. if (((id->capability & 8) || (id->field_valid & 2)) && (id->field_valid & 2))
  1196. printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
  1197. if ((id->capability & 1) && (id->field_valid & 2))
  1198. printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
  1199. printf("\n PIO modes: ");
  1200. if (id->tPIO <= 5) {
  1201. printf("pio0 ");
  1202. if (id->tPIO >= 1) printf("pio1 ");
  1203. if (id->tPIO >= 2) printf("pio2 ");
  1204. }
  1205. if (id->field_valid & 2) {
  1206. static const masks_labels_t pio_modes = {
  1207. .masks = { 1, 2, ~3 },
  1208. .labels = "pio3 \0""pio4 \0""pio? \0",
  1209. };
  1210. print_flags(&pio_modes, id->eide_pio_modes);
  1211. }
  1212. if (id->capability & 1) {
  1213. if (id->dma_1word | id->dma_mword) {
  1214. static const int dma_wmode_masks[] = { 0x100, 1, 0x200, 2, 0x400, 4, 0xf800, 0xf8 };
  1215. printf("\n DMA modes: ");
  1216. print_flags_separated(dma_wmode_masks,
  1217. "*\0""sdma0 \0""*\0""sdma1 \0""*\0""sdma2 \0""*\0""sdma? \0",
  1218. id->dma_1word, NULL);
  1219. print_flags_separated(dma_wmode_masks,
  1220. "*\0""mdma0 \0""*\0""mdma1 \0""*\0""mdma2 \0""*\0""mdma? \0",
  1221. id->dma_mword, NULL);
  1222. }
  1223. }
  1224. if (((id->capability & 8) || (id->field_valid & 2)) && id->field_valid & 4) {
  1225. static const masks_labels_t ultra_modes1 = {
  1226. .masks = { 0x100, 0x001, 0x200, 0x002, 0x400, 0x004 },
  1227. .labels = "*\0""udma0 \0""*\0""udma1 \0""*\0""udma2 \0",
  1228. };
  1229. printf("\n UDMA modes: ");
  1230. print_flags(&ultra_modes1, id->dma_ultra);
  1231. #ifdef __NEW_HD_DRIVE_ID
  1232. if (id->hw_config & 0x2000) {
  1233. #else /* !__NEW_HD_DRIVE_ID */
  1234. if (id->word93 & 0x2000) {
  1235. #endif /* __NEW_HD_DRIVE_ID */
  1236. static const masks_labels_t ultra_modes2 = {
  1237. .masks = { 0x0800, 0x0008, 0x1000, 0x0010,
  1238. 0x2000, 0x0020, 0x4000, 0x0040,
  1239. 0x8000, 0x0080 },
  1240. .labels = "*\0""udma3 \0""*\0""udma4 \0"
  1241. "*\0""udma5 \0""*\0""udma6 \0"
  1242. "*\0""udma7 \0"
  1243. };
  1244. print_flags(&ultra_modes2, id->dma_ultra);
  1245. }
  1246. }
  1247. printf("\n AdvancedPM=%s", (!(id_regs[83] & 8)) ? "no" : "yes");
  1248. if (id_regs[83] & 8) {
  1249. if (!(id_regs[86] & 8))
  1250. printf(": disabled (255)");
  1251. else if ((id_regs[91] & 0xFF00) != 0x4000)
  1252. printf(": unknown setting");
  1253. else
  1254. printf(": mode=0x%02X (%u)", id_regs[91] & 0xFF, id_regs[91] & 0xFF);
  1255. }
  1256. if (id_regs[82] & 0x20)
  1257. printf(" WriteCache=%s", (id_regs[85] & 0x20) ? "enabled" : "disabled");
  1258. #ifdef __NEW_HD_DRIVE_ID
  1259. if ((id->minor_rev_num && id->minor_rev_num <= 31)
  1260. || (id->major_rev_num && id->minor_rev_num <= 31)
  1261. ) {
  1262. printf("\n Drive conforms to: %s: ",
  1263. (id->minor_rev_num <= 31) ? nth_string(minor_str, id->minor_rev_num) : "unknown");
  1264. if (id->major_rev_num != 0x0000 /* NOVAL_0 */
  1265. && id->major_rev_num != 0xFFFF /* NOVAL_1 */
  1266. ) {
  1267. for (i = 0; i <= 15; i++) {
  1268. if (id->major_rev_num & (1<<i))
  1269. printf(" ATA/ATAPI-%u", i);
  1270. }
  1271. }
  1272. }
  1273. #endif /* __NEW_HD_DRIVE_ID */
  1274. puts("\n\n * current active mode\n");
  1275. }
  1276. #endif
  1277. static void flush_buffer_cache(/*int fd*/ void)
  1278. {
  1279. fsync(fd); /* flush buffers */
  1280. ioctl_or_warn(fd, BLKFLSBUF, NULL); /* do it again, big time */
  1281. #ifdef HDIO_DRIVE_CMD
  1282. sleep(1);
  1283. if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) { /* await completion */
  1284. if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
  1285. bb_perror_msg("HDIO_DRIVE_CMD");
  1286. else
  1287. bb_perror_msg("ioctl %#x failed", HDIO_DRIVE_CMD);
  1288. }
  1289. #endif
  1290. }
  1291. static void seek_to_zero(/*int fd*/ void)
  1292. {
  1293. xlseek(fd, (off_t) 0, SEEK_SET);
  1294. }
  1295. static void read_big_block(/*int fd,*/ char *buf)
  1296. {
  1297. int i;
  1298. xread(fd, buf, TIMING_BUF_BYTES);
  1299. /* access all sectors of buf to ensure the read fully completed */
  1300. for (i = 0; i < TIMING_BUF_BYTES; i += 512)
  1301. buf[i] &= 1;
  1302. }
  1303. static unsigned dev_size_mb(/*int fd*/ void)
  1304. {
  1305. union {
  1306. unsigned long long blksize64;
  1307. unsigned blksize32;
  1308. } u;
  1309. if (0 == ioctl(fd, BLKGETSIZE64, &u.blksize64)) { // bytes
  1310. u.blksize64 /= (1024 * 1024);
  1311. } else {
  1312. xioctl(fd, BLKGETSIZE, &u.blksize32); // sectors
  1313. u.blksize64 = u.blksize32 / (2 * 1024);
  1314. }
  1315. if (u.blksize64 > UINT_MAX)
  1316. return UINT_MAX;
  1317. return u.blksize64;
  1318. }
  1319. static void print_timing(unsigned m, unsigned elapsed_us)
  1320. {
  1321. unsigned sec = elapsed_us / 1000000;
  1322. unsigned hs = (elapsed_us % 1000000) / 10000;
  1323. printf("%5u MB in %u.%02u seconds = %u kB/s\n",
  1324. m, sec, hs,
  1325. /* "| 1" prevents div-by-0 */
  1326. (unsigned) ((unsigned long long)m * (1024 * 1000000) / (elapsed_us | 1))
  1327. // ~= (m * 1024) / (elapsed_us / 1000000)
  1328. // = kb / elapsed_sec
  1329. );
  1330. }
  1331. static void do_time(int cache /*,int fd*/)
  1332. /* cache=1: time cache: repeatedly read N MB at offset 0
  1333. * cache=0: time device: linear read, starting at offset 0
  1334. */
  1335. {
  1336. unsigned max_iterations, iterations;
  1337. unsigned start; /* doesn't need to be long long */
  1338. unsigned elapsed, elapsed2;
  1339. unsigned total_MB;
  1340. char *buf = xmalloc(TIMING_BUF_BYTES);
  1341. if (mlock(buf, TIMING_BUF_BYTES))
  1342. bb_perror_msg_and_die("mlock");
  1343. /* Clear out the device request queues & give them time to complete.
  1344. * NB: *small* delay. User is expected to have a clue and to not run
  1345. * heavy io in parallel with measurements. */
  1346. sync();
  1347. sleep(1);
  1348. if (cache) { /* Time cache */
  1349. seek_to_zero();
  1350. read_big_block(buf);
  1351. printf("Timing buffer-cache reads: ");
  1352. } else { /* Time device */
  1353. printf("Timing buffered disk reads:");
  1354. }
  1355. fflush_all();
  1356. /* Now do the timing */
  1357. iterations = 0;
  1358. /* Max time to run (small for cache, avoids getting
  1359. * huge total_MB which can overlow unsigned type) */
  1360. elapsed2 = 510000; /* cache */
  1361. max_iterations = UINT_MAX;
  1362. if (!cache) {
  1363. elapsed2 = 3000000; /* not cache */
  1364. /* Don't want to read past the end! */
  1365. max_iterations = dev_size_mb() / TIMING_BUF_MB;
  1366. }
  1367. start = monotonic_us();
  1368. do {
  1369. if (cache)
  1370. seek_to_zero();
  1371. read_big_block(buf);
  1372. elapsed = (unsigned)monotonic_us() - start;
  1373. ++iterations;
  1374. } while (elapsed < elapsed2 && iterations < max_iterations);
  1375. total_MB = iterations * TIMING_BUF_MB;
  1376. //printf(" elapsed:%u iterations:%u ", elapsed, iterations);
  1377. if (cache) {
  1378. /* Cache: remove lseek() and monotonic_us() overheads
  1379. * from elapsed */
  1380. start = monotonic_us();
  1381. do {
  1382. seek_to_zero();
  1383. elapsed2 = (unsigned)monotonic_us() - start;
  1384. } while (--iterations);
  1385. //printf(" elapsed2:%u ", elapsed2);
  1386. elapsed -= elapsed2;
  1387. total_MB *= 2; // BUFCACHE_FACTOR (why?)
  1388. flush_buffer_cache();
  1389. }
  1390. print_timing(total_MB, elapsed);
  1391. munlock(buf, TIMING_BUF_BYTES);
  1392. free(buf);
  1393. }
  1394. #if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1395. static void bus_state_value(unsigned value)
  1396. {
  1397. if (value == BUSSTATE_ON)
  1398. on_off(1);
  1399. else if (value == BUSSTATE_OFF)
  1400. on_off(0);
  1401. else if (value == BUSSTATE_TRISTATE)
  1402. puts(" (tristate)");
  1403. else
  1404. printf(" (unknown: %u)\n", value);
  1405. }
  1406. #endif
  1407. #ifdef HDIO_DRIVE_CMD
  1408. static void interpret_standby(uint8_t standby)
  1409. {
  1410. printf(" (");
  1411. if (standby == 0) {
  1412. printf("off");
  1413. } else if (standby <= 240 || standby == 252 || standby == 255) {
  1414. /* standby is in 5 sec units */
  1415. unsigned t = standby * 5;
  1416. printf("%u minutes %u seconds", t / 60, t % 60);
  1417. } else if (standby <= 251) {
  1418. unsigned t = (standby - 240); /* t is in 30 min units */;
  1419. printf("%u.%c hours", t / 2, (t & 1) ? '5' : '0');
  1420. }
  1421. if (standby == 253)
  1422. printf("vendor-specific");
  1423. if (standby == 254)
  1424. printf("reserved");
  1425. puts(")");
  1426. }
  1427. static const uint8_t xfermode_val[] ALIGN1 = {
  1428. 8, 9, 10, 11, 12, 13, 14, 15,
  1429. 16, 17, 18, 19, 20, 21, 22, 23,
  1430. 32, 33, 34, 35, 36, 37, 38, 39,
  1431. 64, 65, 66, 67, 68, 69, 70, 71
  1432. };
  1433. /* NB: we save size by _not_ storing terninating NUL! */
  1434. static const char xfermode_name[][5] ALIGN1 = {
  1435. "pio0", "pio1", "pio2", "pio3", "pio4", "pio5", "pio6", "pio7",
  1436. "sdma0","sdma1","sdma2","sdma3","sdma4","sdma5","sdma6","sdma7",
  1437. "mdma0","mdma1","mdma2","mdma3","mdma4","mdma5","mdma6","mdma7",
  1438. "udma0","udma1","udma2","udma3","udma4","udma5","udma6","udma7"
  1439. };
  1440. static int translate_xfermode(const char *name)
  1441. {
  1442. int val;
  1443. unsigned i;
  1444. for (i = 0; i < ARRAY_SIZE(xfermode_val); i++) {
  1445. if (!strncmp(name, xfermode_name[i], 5))
  1446. if (strlen(name) <= 5)
  1447. return xfermode_val[i];
  1448. }
  1449. /* Negative numbers are invalid and are caught later */
  1450. val = bb_strtoi(name, NULL, 10);
  1451. if (!errno)
  1452. return val;
  1453. return -1;
  1454. }
  1455. static void interpret_xfermode(unsigned xfermode)
  1456. {
  1457. printf(" (");
  1458. if (xfermode == 0)
  1459. printf("default PIO mode");
  1460. else if (xfermode == 1)
  1461. printf("default PIO mode, disable IORDY");
  1462. else if (xfermode >= 8 && xfermode <= 15)
  1463. printf("PIO flow control mode%u", xfermode - 8);
  1464. else if (xfermode >= 16 && xfermode <= 23)
  1465. printf("singleword DMA mode%u", xfermode - 16);
  1466. else if (xfermode >= 32 && xfermode <= 39)
  1467. printf("multiword DMA mode%u", xfermode - 32);
  1468. else if (xfermode >= 64 && xfermode <= 71)
  1469. printf("UltraDMA mode%u", xfermode - 64);
  1470. else
  1471. printf("unknown");
  1472. puts(")");
  1473. }
  1474. #endif /* HDIO_DRIVE_CMD */
  1475. static void print_flag(int flag, const char *s, unsigned long value)
  1476. {
  1477. if (flag)
  1478. printf(" setting %s to %lu\n", s, value);
  1479. }
  1480. static void process_dev(char *devname)
  1481. {
  1482. /*int fd;*/
  1483. long parm, multcount;
  1484. #ifndef HDIO_DRIVE_CMD
  1485. int force_operation = 0;
  1486. #endif
  1487. /* Please restore args[n] to these values after each ioctl
  1488. except for args[2] */
  1489. unsigned char args[4] = { WIN_SETFEATURES, 0, 0, 0 };
  1490. const char *fmt = " %s\t= %2ld";
  1491. /*fd = xopen_nonblocking(devname);*/
  1492. xmove_fd(xopen_nonblocking(devname), fd);
  1493. printf("\n%s:\n", devname);
  1494. if (getset_readahead == IS_SET) {
  1495. print_flag(getset_readahead, "fs readahead", Xreadahead);
  1496. ioctl_or_warn(fd, BLKRASET, (int *)Xreadahead);
  1497. }
  1498. #if ENABLE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
  1499. if (unregister_hwif) {
  1500. printf(" attempting to unregister hwif#%lu\n", hwif);
  1501. ioctl_or_warn(fd, HDIO_UNREGISTER_HWIF, (int *)(unsigned long)hwif);
  1502. }
  1503. #endif
  1504. #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
  1505. if (scan_hwif == IS_SET) {
  1506. printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq);
  1507. args[0] = hwif_data;
  1508. args[1] = hwif_ctrl;
  1509. args[2] = hwif_irq;
  1510. ioctl_or_warn(fd, HDIO_SCAN_HWIF, args);
  1511. args[0] = WIN_SETFEATURES;
  1512. args[1] = 0;
  1513. }
  1514. #endif
  1515. if (set_piomode) {
  1516. if (noisy_piomode) {
  1517. printf(" attempting to ");
  1518. if (piomode == 255)
  1519. puts("auto-tune PIO mode");
  1520. else if (piomode < 100)
  1521. printf("set PIO mode to %d\n", piomode);
  1522. else if (piomode < 200)
  1523. printf("set MDMA mode to %d\n", (piomode-100));
  1524. else
  1525. printf("set UDMA mode to %d\n", (piomode-200));
  1526. }
  1527. ioctl_or_warn(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode);
  1528. }
  1529. if (getset_io32bit == IS_SET) {
  1530. print_flag(getset_io32bit, "32-bit IO_support flag", io32bit);
  1531. ioctl_or_warn(fd, HDIO_SET_32BIT, (int *)io32bit);
  1532. }
  1533. if (getset_mult == IS_SET) {
  1534. print_flag(getset_mult, "multcount", mult);
  1535. #ifdef HDIO_DRIVE_CMD
  1536. ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult);
  1537. #else
  1538. force_operation |= (!ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult));
  1539. #endif
  1540. }
  1541. if (getset_readonly == IS_SET) {
  1542. print_flag_on_off(getset_readonly, "readonly", readonly);
  1543. ioctl_or_warn(fd, BLKROSET, &readonly);
  1544. }
  1545. if (getset_unmask == IS_SET) {
  1546. print_flag_on_off(getset_unmask, "unmaskirq", unmask);
  1547. ioctl_or_warn(fd, HDIO_SET_UNMASKINTR, (int *)unmask);
  1548. }
  1549. #if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
  1550. if (getset_dma == IS_SET) {
  1551. print_flag_on_off(getset_dma, "using_dma", dma);
  1552. ioctl_or_warn(fd, HDIO_SET_DMA, (int *)dma);
  1553. }
  1554. #endif /* FEATURE_HDPARM_HDIO_GETSET_DMA */
  1555. #ifdef HDIO_SET_QDMA
  1556. if (getset_dma_q == IS_SET) {
  1557. print_flag_on_off(getset_dma_q, "DMA queue_depth", dma_q);
  1558. ioctl_or_warn(fd, HDIO_SET_QDMA, (int *)dma_q);
  1559. }
  1560. #endif
  1561. if (getset_nowerr == IS_SET) {
  1562. print_flag_on_off(getset_nowerr, "nowerr", nowerr);
  1563. ioctl_or_warn(fd, HDIO_SET_NOWERR, (int *)nowerr);
  1564. }
  1565. if (getset_keep == IS_SET) {
  1566. print_flag_on_off(getset_keep, "keep_settings", keep);
  1567. ioctl_or_warn(fd, HDIO_SET_KEEPSETTINGS, (int *)keep);
  1568. }
  1569. #ifdef HDIO_DRIVE_CMD
  1570. if (getset_doorlock == IS_SET) {
  1571. args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
  1572. args[2] = 0;
  1573. print_flag_on_off(getset_doorlock, "drive doorlock", doorlock);
  1574. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1575. args[0] = WIN_SETFEATURES;
  1576. }
  1577. if (getset_dkeep == IS_SET) {
  1578. /* lock/unlock the drive's "feature" settings */
  1579. print_flag_on_off(getset_dkeep, "drive keep features", dkeep);
  1580. args[2] = dkeep ? 0x66 : 0xcc;
  1581. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1582. }
  1583. if (getset_defects == IS_SET) {
  1584. args[2] = defects ? 0x04 : 0x84;
  1585. print_flag(getset_defects, "drive defect-mgmt", defects);
  1586. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1587. }
  1588. if (getset_prefetch == IS_SET) {
  1589. args[1] = prefetch;
  1590. args[2] = 0xab;
  1591. print_flag(getset_prefetch, "drive prefetch", prefetch);
  1592. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1593. args[1] = 0;
  1594. }
  1595. if (set_xfermode) {
  1596. args[1] = xfermode_requested;
  1597. args[2] = 3;
  1598. print_flag(1, "xfermode", xfermode_requested);
  1599. interpret_xfermode(xfermode_requested);
  1600. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1601. args[1] = 0;
  1602. }
  1603. if (getset_lookahead == IS_SET) {
  1604. args[2] = lookahead ? 0xaa : 0x55;
  1605. print_flag_on_off(getset_lookahead, "drive read-lookahead", lookahead);
  1606. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1607. }
  1608. if (getset_apmmode == IS_SET) {
  1609. /* feature register */
  1610. args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */;
  1611. args[1] = apmmode; /* sector count register 1-255 */
  1612. printf(" setting APM level to %s 0x%02lX (%ld)\n",
  1613. (apmmode == 255) ? "disabled" : "",
  1614. apmmode, apmmode);
  1615. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1616. args[1] = 0;
  1617. }
  1618. if (getset_wcache == IS_SET) {
  1619. #ifdef DO_FLUSHCACHE
  1620. #ifndef WIN_FLUSHCACHE
  1621. #define WIN_FLUSHCACHE 0xe7
  1622. #endif
  1623. #endif /* DO_FLUSHCACHE */
  1624. args[2] = wcache ? 0x02 : 0x82;
  1625. print_flag_on_off(getset_wcache, "drive write-caching", wcache);
  1626. #ifdef DO_FLUSHCACHE
  1627. if (!wcache)
  1628. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
  1629. #endif /* DO_FLUSHCACHE */
  1630. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1631. #ifdef DO_FLUSHCACHE
  1632. if (!wcache)
  1633. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
  1634. #endif /* DO_FLUSHCACHE */
  1635. }
  1636. /* In code below, we do not preserve args[0], but the rest
  1637. is preserved, including args[2] */
  1638. args[2] = 0;
  1639. if (set_standbynow) {
  1640. #ifndef WIN_STANDBYNOW1
  1641. #define WIN_STANDBYNOW1 0xE0
  1642. #endif
  1643. #ifndef WIN_STANDBYNOW2
  1644. #define WIN_STANDBYNOW2 0x94
  1645. #endif
  1646. puts(" issuing standby command");
  1647. args[0] = WIN_STANDBYNOW1;
  1648. ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2);
  1649. }
  1650. if (set_sleepnow) {
  1651. #ifndef WIN_SLEEPNOW1
  1652. #define WIN_SLEEPNOW1 0xE6
  1653. #endif
  1654. #ifndef WIN_SLEEPNOW2
  1655. #define WIN_SLEEPNOW2 0x99
  1656. #endif
  1657. puts(" issuing sleep command");
  1658. args[0] = WIN_SLEEPNOW1;
  1659. ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2);
  1660. }
  1661. if (set_seagate) {
  1662. args[0] = 0xfb;
  1663. puts(" disabling Seagate auto powersaving mode");
  1664. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1665. }
  1666. if (getset_standby == IS_SET) {
  1667. args[0] = WIN_SETIDLE1;
  1668. args[1] = standby_requested;
  1669. print_flag(1, "standby", standby_requested);
  1670. interpret_standby(standby_requested);
  1671. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1672. args[1] = 0;
  1673. }
  1674. #else /* HDIO_DRIVE_CMD */
  1675. if (force_operation) {
  1676. char buf[512];
  1677. flush_buffer_cache();
  1678. if (-1 == read(fd, buf, sizeof(buf)))
  1679. bb_perror_msg("read of 512 bytes failed");
  1680. }
  1681. #endif /* HDIO_DRIVE_CMD */
  1682. if (getset_mult || get_identity) {
  1683. multcount = -1;
  1684. if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount)) {
  1685. /* To be coherent with ioctl_or_warn. */
  1686. if (getset_mult && ENABLE_IOCTL_HEX2STR_ERROR)
  1687. bb_perror_msg("HDIO_GET_MULTCOUNT");
  1688. else
  1689. bb_perror_msg("ioctl %#x failed", HDIO_GET_MULTCOUNT);
  1690. } else if (getset_mult) {
  1691. printf(fmt, "multcount", multcount);
  1692. on_off(multcount != 0);
  1693. }
  1694. }
  1695. if (getset_io32bit) {
  1696. if (!ioctl_or_warn(fd, HDIO_GET_32BIT, &parm)) {
  1697. printf(" IO_support\t=%3ld (", parm);
  1698. if (parm == 0)
  1699. puts("default 16-bit)");
  1700. else if (parm == 2)
  1701. puts("16-bit)");
  1702. else if (parm == 1)
  1703. puts("32-bit)");
  1704. else if (parm == 3)
  1705. puts("32-bit w/sync)");
  1706. else if (parm == 8)
  1707. puts("Request-Queue-Bypass)");
  1708. else
  1709. puts("\?\?\?)");
  1710. }
  1711. }
  1712. if (getset_unmask) {
  1713. if (!ioctl_or_warn(fd, HDIO_GET_UNMASKINTR, &parm))
  1714. print_value_on_off("unmaskirq", parm);
  1715. }
  1716. #if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
  1717. if (getset_dma) {
  1718. if (!ioctl_or_warn(fd, HDIO_GET_DMA, &parm)) {
  1719. printf(fmt, "using_dma", parm);
  1720. if (parm == 8)
  1721. puts(" (DMA-Assisted-PIO)");
  1722. else
  1723. on_off(parm != 0);
  1724. }
  1725. }
  1726. #endif
  1727. #ifdef HDIO_GET_QDMA
  1728. if (getset_dma_q) {
  1729. if (!ioctl_or_warn(fd, HDIO_GET_QDMA, &parm))
  1730. print_value_on_off("queue_depth", parm);
  1731. }
  1732. #endif
  1733. if (getset_keep) {
  1734. if (!ioctl_or_warn(fd, HDIO_GET_KEEPSETTINGS, &parm))
  1735. print_value_on_off("keepsettings", parm);
  1736. }
  1737. if (getset_nowerr) {
  1738. if (!ioctl_or_warn(fd, HDIO_GET_NOWERR, &parm))
  1739. print_value_on_off("nowerr", parm);
  1740. }
  1741. if (getset_readonly) {
  1742. if (!ioctl_or_warn(fd, BLKROGET, &parm))
  1743. print_value_on_off("readonly", parm);
  1744. }
  1745. if (getset_readahead) {
  1746. if (!ioctl_or_warn(fd, BLKRAGET, &parm))
  1747. print_value_on_off("readahead", parm);
  1748. }
  1749. if (get_geom) {
  1750. if (!ioctl_or_warn(fd, BLKGETSIZE, &parm)) {
  1751. struct hd_geometry g;
  1752. if (!ioctl_or_warn(fd, HDIO_GETGEO, &g))
  1753. printf(" geometry\t= %u/%u/%u, sectors = %ld, start = %ld\n",
  1754. g.cylinders, g.heads, g.sectors, parm, g.start);
  1755. }
  1756. }
  1757. #ifdef HDIO_DRIVE_CMD
  1758. if (get_powermode) {
  1759. #ifndef WIN_CHECKPOWERMODE1
  1760. #define WIN_CHECKPOWERMODE1 0xE5
  1761. #endif
  1762. #ifndef WIN_CHECKPOWERMODE2
  1763. #define WIN_CHECKPOWERMODE2 0x98
  1764. #endif
  1765. const char *state;
  1766. args[0] = WIN_CHECKPOWERMODE1;
  1767. if (ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2)) {
  1768. if (errno != EIO || args[0] != 0 || args[1] != 0)
  1769. state = "unknown";
  1770. else
  1771. state = "sleeping";
  1772. } else
  1773. state = (args[2] == 255) ? "active/idle" : "standby";
  1774. args[1] = args[2] = 0;
  1775. printf(" drive state is: %s\n", state);
  1776. }
  1777. #endif
  1778. #if ENABLE_FEATURE_HDPARM_HDIO_DRIVE_RESET
  1779. if (perform_reset) {
  1780. ioctl_or_warn(fd, HDIO_DRIVE_RESET, NULL);
  1781. }
  1782. #endif /* FEATURE_HDPARM_HDIO_DRIVE_RESET */
  1783. #if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1784. if (perform_tristate) {
  1785. args[0] = 0;
  1786. args[1] = tristate;
  1787. ioctl_or_warn(fd, HDIO_TRISTATE_HWIF, &args);
  1788. }
  1789. #endif /* FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
  1790. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  1791. if (get_identity) {
  1792. struct hd_driveid id;
  1793. if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
  1794. if (multcount != -1) {
  1795. id.multsect = multcount;
  1796. id.multsect_valid |= 1;
  1797. } else
  1798. id.multsect_valid &= ~1;
  1799. dump_identity(&id);
  1800. } else if (errno == -ENOMSG)
  1801. puts(" no identification info available");
  1802. else if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
  1803. bb_perror_msg("HDIO_GET_IDENTITY");
  1804. else
  1805. bb_perror_msg("ioctl %#x failed", HDIO_GET_IDENTITY);
  1806. }
  1807. if (get_IDentity) {
  1808. unsigned char args1[4+512]; /* = { ... } will eat 0.5k of rodata! */
  1809. memset(args1, 0, sizeof(args1));
  1810. args1[0] = WIN_IDENTIFY;
  1811. args1[3] = 1;
  1812. if (!ioctl_alt_or_warn(HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY))
  1813. identify((void *)(args1 + 4));
  1814. }
  1815. #endif
  1816. #if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1817. if (getset_busstate == IS_SET) {
  1818. print_flag(1, "bus state", busstate);
  1819. bus_state_value(busstate);
  1820. ioctl_or_warn(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate);
  1821. }
  1822. if (getset_busstate) {
  1823. if (!ioctl_or_warn(fd, HDIO_GET_BUSSTATE, &parm)) {
  1824. printf(fmt, "bus state", parm);
  1825. bus_state_value(parm);
  1826. }
  1827. }
  1828. #endif
  1829. if (reread_partn)
  1830. ioctl_or_warn(fd, BLKRRPART, NULL);
  1831. if (do_ctimings)
  1832. do_time(1 /*,fd*/); /* time cache */
  1833. if (do_timings)
  1834. do_time(0 /*,fd*/); /* time device */
  1835. if (do_flush)
  1836. flush_buffer_cache();
  1837. close(fd);
  1838. }
  1839. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  1840. static int fromhex(unsigned char c)
  1841. {
  1842. if (isdigit(c))
  1843. return (c - '0');
  1844. if (c >= 'a' && c <= 'f')
  1845. return (c - ('a' - 10));
  1846. bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
  1847. }
  1848. static void identify_from_stdin(void) NORETURN;
  1849. static void identify_from_stdin(void)
  1850. {
  1851. uint16_t sbuf[256];
  1852. unsigned char buf[1280];
  1853. unsigned char *b = (unsigned char *)buf;
  1854. int i;
  1855. xread(STDIN_FILENO, buf, 1280);
  1856. // Convert the newline-separated hex data into an identify block.
  1857. for (i = 0; i < 256; i++) {
  1858. int j;
  1859. for (j = 0; j < 4; j++)
  1860. sbuf[i] = (sbuf[i] << 4) + fromhex(*(b++));
  1861. }
  1862. // Parse the data.
  1863. identify(sbuf);
  1864. }
  1865. #else
  1866. void identify_from_stdin(void);
  1867. #endif
  1868. /* busybox specific stuff */
  1869. static int parse_opts(unsigned long *value, int min, int max)
  1870. {
  1871. if (optarg) {
  1872. *value = xatol_range(optarg, min, max);
  1873. return IS_SET;
  1874. }
  1875. return IS_GET;
  1876. }
  1877. static int parse_opts_0_max(unsigned long *value, int max)
  1878. {
  1879. return parse_opts(value, 0, max);
  1880. }
  1881. static int parse_opts_0_1(unsigned long *value)
  1882. {
  1883. return parse_opts(value, 0, 1);
  1884. }
  1885. static int parse_opts_0_INTMAX(unsigned long *value)
  1886. {
  1887. return parse_opts(value, 0, INT_MAX);
  1888. }
  1889. static void parse_xfermode(int flag, smallint *get, smallint *set, int *value)
  1890. {
  1891. if (flag) {
  1892. *get = IS_GET;
  1893. if (optarg) {
  1894. *value = translate_xfermode(optarg);
  1895. *set = (*value > -1);
  1896. }
  1897. }
  1898. }
  1899. /*------- getopt short options --------*/
  1900. static const char hdparm_options[] ALIGN1 =
  1901. "gfu::n::p:r::m::c::k::a::B:tT"
  1902. IF_FEATURE_HDPARM_GET_IDENTITY("iI")
  1903. IF_FEATURE_HDPARM_HDIO_GETSET_DMA("d::")
  1904. #ifdef HDIO_DRIVE_CMD
  1905. "S:D:P:X:K:A:L:W:CyYzZ"
  1906. #endif
  1907. IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF("U:")
  1908. #ifdef HDIO_GET_QDMA
  1909. #ifdef HDIO_SET_QDMA
  1910. "Q:"
  1911. #else
  1912. "Q"
  1913. #endif
  1914. #endif
  1915. IF_FEATURE_HDPARM_HDIO_DRIVE_RESET("w")
  1916. IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF("x::b:")
  1917. IF_FEATURE_HDPARM_HDIO_SCAN_HWIF("R:");
  1918. /*-------------------------------------*/
  1919. /* our main() routine: */
  1920. int hdparm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1921. int hdparm_main(int argc, char **argv)
  1922. {
  1923. int c;
  1924. int flagcount = 0;
  1925. INIT_G();
  1926. while ((c = getopt(argc, argv, hdparm_options)) >= 0) {
  1927. flagcount++;
  1928. IF_FEATURE_HDPARM_GET_IDENTITY(get_IDentity |= (c == 'I'));
  1929. IF_FEATURE_HDPARM_GET_IDENTITY(get_identity |= (c == 'i'));
  1930. get_geom |= (c == 'g');
  1931. do_flush |= (c == 'f');
  1932. if (c == 'u') getset_unmask = parse_opts_0_1(&unmask);
  1933. IF_FEATURE_HDPARM_HDIO_GETSET_DMA(
  1934. if (c == 'd') getset_dma = parse_opts_0_max(&dma, 9);
  1935. )
  1936. if (c == 'n') getset_nowerr = parse_opts_0_1(&nowerr);
  1937. parse_xfermode((c == 'p'), &noisy_piomode, &set_piomode, &piomode);
  1938. if (c == 'r') getset_readonly = parse_opts_0_1(&readonly);
  1939. if (c == 'm') getset_mult = parse_opts_0_INTMAX(&mult /*32*/);
  1940. if (c == 'c') getset_io32bit = parse_opts_0_INTMAX(&io32bit /*8*/);
  1941. if (c == 'k') getset_keep = parse_opts_0_1(&keep);
  1942. if (c == 'a') getset_readahead = parse_opts_0_INTMAX(&Xreadahead);
  1943. if (c == 'B') getset_apmmode = parse_opts(&apmmode, 1, 255);
  1944. do_flush |= do_timings |= (c == 't');
  1945. do_flush |= do_ctimings |= (c == 'T');
  1946. #ifdef HDIO_DRIVE_CMD
  1947. if (c == 'S') getset_standby = parse_opts_0_max(&standby_requested, 255);
  1948. if (c == 'D') getset_defects = parse_opts_0_INTMAX(&defects);
  1949. if (c == 'P') getset_prefetch = parse_opts_0_INTMAX(&prefetch);
  1950. parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
  1951. if (c == 'K') getset_dkeep = parse_opts_0_1(&prefetch);
  1952. if (c == 'A') getset_lookahead = parse_opts_0_1(&lookahead);
  1953. if (c == 'L') getset_doorlock = parse_opts_0_1(&doorlock);
  1954. if (c == 'W') getset_wcache = parse_opts_0_1(&wcache);
  1955. get_powermode |= (c == 'C');
  1956. set_standbynow |= (c == 'y');
  1957. set_sleepnow |= (c == 'Y');
  1958. reread_partn |= (c == 'z');
  1959. set_seagate |= (c == 'Z');
  1960. #endif
  1961. IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') unregister_hwif = parse_opts_0_INTMAX(&hwif));
  1962. #ifdef HDIO_GET_QDMA
  1963. if (c == 'Q') {
  1964. getset_dma_q = parse_opts_0_INTMAX(&dma_q);
  1965. }
  1966. #endif
  1967. IF_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
  1968. IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') perform_tristate = parse_opts_0_1(&tristate));
  1969. IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') getset_busstate = parse_opts_0_max(&busstate, 2));
  1970. #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
  1971. if (c == 'R') {
  1972. scan_hwif = parse_opts_0_INTMAX(&hwif_data);
  1973. hwif_ctrl = xatoi_positive((argv[optind]) ? argv[optind] : "");
  1974. hwif_irq = xatoi_positive((argv[optind+1]) ? argv[optind+1] : "");
  1975. /* Move past the 2 additional arguments */
  1976. argv += 2;
  1977. argc -= 2;
  1978. }
  1979. #endif
  1980. }
  1981. /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
  1982. if (!flagcount) {
  1983. getset_mult = getset_io32bit = getset_unmask = getset_keep = getset_readonly = getset_readahead = get_geom = IS_GET;
  1984. IF_FEATURE_HDPARM_HDIO_GETSET_DMA(getset_dma = IS_GET);
  1985. }
  1986. argv += optind;
  1987. if (!*argv) {
  1988. if (ENABLE_FEATURE_HDPARM_GET_IDENTITY && !isatty(STDIN_FILENO))
  1989. identify_from_stdin(); /* EXIT */
  1990. bb_show_usage();
  1991. }
  1992. do {
  1993. process_dev(*argv++);
  1994. } while (*argv);
  1995. return EXIT_SUCCESS;
  1996. }