3
0

hdparm.c 65 KB

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