3
0

hdparm.c 74 KB

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