hdparm.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  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 "libbb.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. #if ENABLE_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); 0=vendor specific values */
  191. #define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
  192. #define IORDY_OFF 0x0400 /* 1=may be disabled */
  193. #define LBA_SUP 0x0200 /* 1=Logical Block Address support */
  194. #define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
  195. #define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
  196. #define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
  197. #define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
  198. #define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
  199. /* word 50: capabilities 1 */
  200. #define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
  201. /* words 51 & 52: PIO & DMA cycle times */
  202. #define MODE 0xff00 /* the mode is in the MSBs */
  203. /* word 53: whats_valid */
  204. #define OK_W88 0x0004 /* the ultra_dma info is valid */
  205. #define OK_W64_70 0x0002 /* see above for word descriptions */
  206. #define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
  207. /*word 63,88: dma_mode, ultra_dma_mode*/
  208. #define MODE_MAX 7 /* bit definitions force udma <=7 (when
  209. * udma >=8 comes out it'll have to be
  210. * defined in a new dma_mode word!) */
  211. /* word 64: PIO transfer modes */
  212. #define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
  213. #define PIO_MODE_MAX 8 /* but all 8 bits are defined */
  214. /* word 75: queue_depth */
  215. #define DEPTH_BITS 0x001f /* bits used for queue depth */
  216. /* words 80-81: version numbers */
  217. /* NOVAL_0 or NOVAL_1 means device does not report version */
  218. /* word 81: minor version number */
  219. #define MINOR_MAX 0x22
  220. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  221. static const char *const minor_str[MINOR_MAX + 2] = {
  222. /* 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", /* 0x0013 */
  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] ALIGN1 = {
  262. /* word 81 value: */
  263. 0, /* 0x0000 WARNING: actual_ver[] array */
  264. 1, /* 0x0001 WARNING: corresponds */
  265. 1, /* 0x0002 WARNING: *exactly* */
  266. 1, /* 0x0003 WARNING: to the ATA/ */
  267. 2, /* 0x0004 WARNING: ATAPI version */
  268. 2, /* 0x0005 WARNING: listed in */
  269. 3, /* 0x0006 WARNING: the */
  270. 2, /* 0x0007 WARNING: minor_str */
  271. 3, /* 0x0008 WARNING: array */
  272. 2, /* 0x0009 WARNING: above. */
  273. 3, /* 0x000a WARNING: */
  274. 3, /* 0x000b WARNING: If you change */
  275. 3, /* 0x000c WARNING: that one, */
  276. 4, /* 0x000d WARNING: change this one */
  277. 4, /* 0x000e WARNING: too!!! */
  278. 4, /* 0x000f */
  279. 4, /* 0x0010 */
  280. 4, /* 0x0011 */
  281. 4, /* 0x0012 */
  282. 5, /* 0x0013 */
  283. 4, /* 0x0014 */
  284. 5, /* 0x0015 */
  285. 5, /* 0x0016 */
  286. 4, /* 0x0017 */
  287. 6, /* 0x0018 */
  288. 6, /* 0x0019 */
  289. 7, /* 0x001a */
  290. 6, /* 0x001b */
  291. 6, /* 0x001c */
  292. 7, /* 0x001d */
  293. 7, /* 0x001e */
  294. 0, /* 0x001f */
  295. 0, /* 0x0020 */
  296. 7, /* 0x0021 */
  297. 6, /* 0x0022 */
  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. #if ENABLE_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. #if ENABLE_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 BUFCACHE_FACTOR 2
  407. #undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
  408. /* Busybox messages and functions */
  409. #if ENABLE_IOCTL_HEX2STR_ERROR
  410. static int ioctl_alt_func(int fd, int cmd, unsigned char *args, int alt, const char *string)
  411. {
  412. if (!ioctl(fd, cmd, args))
  413. return 0;
  414. args[0] = alt;
  415. return bb_ioctl_or_warn(fd, cmd, args, string);
  416. }
  417. #define ioctl_alt_or_warn(fd,cmd,args,alt) ioctl_alt_func(fd,cmd,args,alt,#cmd)
  418. #else
  419. static int ioctl_alt_func(int fd, int cmd, unsigned char *args, int alt)
  420. {
  421. if (!ioctl(fd, cmd, args))
  422. return 0;
  423. args[0] = alt;
  424. return bb_ioctl_or_warn(fd, cmd, args);
  425. }
  426. #define ioctl_alt_or_warn(fd,cmd,args,alt) ioctl_alt_func(fd,cmd,args,alt)
  427. #endif
  428. static void on_off(int value)
  429. {
  430. puts(value ? " (on)" : " (off)");
  431. }
  432. static void print_flag_on_off(int get_arg, const char *s, unsigned long arg)
  433. {
  434. if (get_arg) {
  435. printf(" setting %s to %ld", s, arg);
  436. on_off(arg);
  437. }
  438. }
  439. static void print_value_on_off(const char *str, unsigned long argp)
  440. {
  441. printf(" %s\t= %2ld", str, argp);
  442. on_off(argp != 0);
  443. }
  444. #if ENABLE_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, const char *string, int n)
  447. {
  448. if (val[i]) {
  449. printf("\t%-20s", string);
  450. print_ascii(&val[i], n);
  451. }
  452. }
  453. #endif
  454. /* end of busybox specific stuff */
  455. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  456. static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
  457. {
  458. uint16_t ii;
  459. uint8_t err_dma = 0;
  460. for (ii = 0; ii <= MODE_MAX; ii++) {
  461. if (mode_sel & 0x0001) {
  462. printf("*%cdma%u ", cc, ii);
  463. if (*have_mode)
  464. err_dma = 1;
  465. *have_mode = 1;
  466. } else if (mode_sup & 0x0001)
  467. printf("%cdma%u ", cc, ii);
  468. mode_sup >>= 1;
  469. mode_sel >>= 1;
  470. }
  471. return err_dma;
  472. }
  473. static void print_ascii(uint16_t *p, uint8_t length)
  474. {
  475. uint8_t ii;
  476. char cl;
  477. /* find first non-space & print it */
  478. for (ii = 0; ii < length; ii++) {
  479. if ((char)((*p)>>8) != ' ')
  480. break;
  481. cl = (char)(*p);
  482. if (cl != ' ') {
  483. if (cl != '\0')
  484. printf("%c", cl);
  485. p++;
  486. ii++;
  487. break;
  488. }
  489. p++;
  490. }
  491. /* print the rest */
  492. for (; ii< length; ii++) {
  493. if (!(*p))
  494. break; /* some older devices have NULLs */
  495. printf("%c%c", (char)((*p)>>8), (char)(*p));
  496. p++;
  497. }
  498. puts("");
  499. }
  500. // Parse 512 byte disk identification block and print much crap.
  501. static void identify(uint16_t *id_supplied)
  502. {
  503. uint16_t buf[256];
  504. uint16_t *val, ii, jj, kk;
  505. uint16_t like_std = 1, std = 0, min_std = 0xffff;
  506. uint16_t dev = NO_DEV, eqpt = NO_DEV;
  507. uint8_t have_mode = 0, err_dma = 0;
  508. uint8_t chksum = 0;
  509. uint32_t ll, mm, nn, oo;
  510. uint64_t bbbig; /* (:) */
  511. const char *strng;
  512. // Adjust for endianness if necessary.
  513. if (BB_BIG_ENDIAN) {
  514. swab(id_supplied, buf, sizeof(buf));
  515. val = buf;
  516. } else
  517. val = id_supplied;
  518. chksum &= 0xff;
  519. /* check if we recognise the device type */
  520. puts("");
  521. if (!(val[GEN_CONFIG] & NOT_ATA)) {
  522. dev = ATA_DEV;
  523. printf("ATA device, with ");
  524. } else if (val[GEN_CONFIG]==CFA_SUPPORT_VAL) {
  525. dev = ATA_DEV;
  526. like_std = 4;
  527. printf("CompactFlash ATA device, with ");
  528. } else if (!(val[GEN_CONFIG] & NOT_ATAPI)) {
  529. dev = ATAPI_DEV;
  530. eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
  531. printf("ATAPI %s, with ", pkt_str[eqpt]);
  532. like_std = 3;
  533. } else
  534. /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
  535. bb_error_msg_and_die("unknown device type");
  536. printf("%sremovable media\n", !(val[GEN_CONFIG] & MEDIA_REMOVABLE) ? "non-" : "");
  537. /* Info from the specific configuration word says whether or not the
  538. * ID command completed correctly. It is only defined, however in
  539. * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
  540. * standards. Since the values allowed for this word are extremely
  541. * specific, it should be safe to check it now, even though we don't
  542. * know yet what standard this device is using.
  543. */
  544. if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)
  545. || (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL)
  546. ) {
  547. like_std = 5;
  548. if ((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL))
  549. printf("powers-up in standby; SET FEATURES subcmd spins-up.\n");
  550. if (((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE))
  551. printf("\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
  552. }
  553. /* output the model and serial numbers and the fw revision */
  554. xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
  555. xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
  556. xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
  557. xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
  558. xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
  559. /* major & minor standards version number (Note: these words were not
  560. * defined until ATA-3 & the CDROM std uses different words.) */
  561. printf("Standards:");
  562. if (eqpt != CDROM) {
  563. if (val[MINOR] && (val[MINOR] <= MINOR_MAX)) {
  564. if (like_std < 3) like_std = 3;
  565. std = actual_ver[val[MINOR]];
  566. if (std) printf("\n\tUsed: %s ", minor_str[val[MINOR]]);
  567. }
  568. /* looks like when they up-issue the std, they obsolete one;
  569. * thus, only the newest 4 issues need be supported. (That's
  570. * what "kk" and "min_std" are all about.) */
  571. if (val[MAJOR] && (val[MAJOR] != NOVAL_1)) {
  572. printf("\n\tSupported: ");
  573. jj = val[MAJOR] << 1;
  574. kk = like_std >4 ? like_std-4: 0;
  575. for (ii = 14; (ii >0)&&(ii>kk); ii--) {
  576. if (jj & 0x8000) {
  577. printf("%u ", ii);
  578. if (like_std < ii) {
  579. like_std = ii;
  580. kk = like_std >4 ? like_std-4: 0;
  581. }
  582. if (min_std > ii) min_std = ii;
  583. }
  584. jj <<= 1;
  585. }
  586. if (like_std < 3) like_std = 3;
  587. }
  588. /* Figure out what standard the device is using if it hasn't told
  589. * us. If we know the std, check if the device is using any of
  590. * the words from the next level up. It happens.
  591. */
  592. if (like_std < std) like_std = std;
  593. if (((std == 5) || (!std && (like_std < 6))) &&
  594. ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  595. (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
  596. ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
  597. ( val[CMDS_SUPP_2] & CMDS_W84) ) )
  598. ) {
  599. like_std = 6;
  600. } else if (((std == 4) || (!std && (like_std < 5))) &&
  601. ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
  602. (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
  603. ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  604. (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
  605. {
  606. like_std = 5;
  607. } else if (((std == 3) || (!std && (like_std < 4))) &&
  608. ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  609. ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
  610. (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
  611. (( val[CAPAB_1] & VALID) == VALID_VAL) ||
  612. (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
  613. (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) )
  614. ) {
  615. like_std = 4;
  616. } else if (((std == 2) || (!std && (like_std < 3)))
  617. && ((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
  618. ) {
  619. like_std = 3;
  620. } else if (((std == 1) || (!std && (like_std < 2))) &&
  621. ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
  622. (val[WHATS_VALID] & OK_W64_70)) )
  623. {
  624. like_std = 2;
  625. }
  626. if (!std)
  627. printf("\n\tLikely used: %u\n", like_std);
  628. else if (like_std > std)
  629. printf("& some of %u\n", like_std);
  630. else
  631. puts("");
  632. } else {
  633. /* TBD: do CDROM stuff more thoroughly. For now... */
  634. kk = 0;
  635. if (val[CDR_MINOR] == 9) {
  636. kk = 1;
  637. printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
  638. }
  639. if (val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1)) {
  640. kk = 1;
  641. printf("\n\tSupported: CD-ROM ATAPI");
  642. jj = val[CDR_MAJOR] >> 1;
  643. for (ii = 1; ii < 15; ii++) {
  644. if (jj & 0x0001) printf("-%u ", ii);
  645. jj >>= 1;
  646. }
  647. }
  648. printf("%s\n", kk ? "" : "\n\tLikely used CD-ROM ATAPI-1");
  649. /* the cdrom stuff is more like ATA-2 than anything else, so: */
  650. like_std = 2;
  651. }
  652. if (min_std == 0xffff)
  653. min_std = like_std > 4 ? like_std - 3 : 1;
  654. printf("Configuration:\n");
  655. /* more info from the general configuration word */
  656. if ((eqpt != CDROM) && (like_std == 1)) {
  657. jj = val[GEN_CONFIG] >> 1;
  658. for (ii = 1; ii < 15; ii++) {
  659. if (jj & 0x0001)
  660. printf("\t%s\n", ata1_cfg_str[ii]);
  661. jj >>=1;
  662. }
  663. }
  664. if (dev == ATAPI_DEV) {
  665. if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_3MS_VAL)
  666. strng = "3ms";
  667. else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_INTR_VAL)
  668. strng = "<=10ms with INTRQ";
  669. else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_50US_VAL)
  670. strng ="50us";
  671. else
  672. strng = "Unknown";
  673. printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */
  674. if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_12_VAL)
  675. strng = "12 bytes";
  676. else if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_16_VAL)
  677. strng = "16 bytes";
  678. else
  679. strng = "Unknown";
  680. puts(strng);
  681. } else {
  682. /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
  683. ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
  684. mm = 0; bbbig = 0;
  685. if ((ll > 0x00FBFC10) && (!val[LCYLS]))
  686. printf("\tCHS addressing not supported\n");
  687. else {
  688. jj = val[WHATS_VALID] & OK_W54_58;
  689. 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",
  690. val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
  691. if ((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES]))
  692. printf("\tbytes/track: %u\tbytes/sector: %u\n", val[TRACK_BYTES], val[SECT_BYTES]);
  693. if (jj) {
  694. mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
  695. if (like_std < 3) {
  696. /* check Endian of capacity bytes */
  697. nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
  698. oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
  699. if (abs(mm - nn) > abs(oo - nn))
  700. mm = oo;
  701. }
  702. printf("\tCHS current addressable sectors:%11u\n", mm);
  703. }
  704. }
  705. /* LBA addressing */
  706. printf("\tLBA user addressable sectors:%11u\n", ll);
  707. if (((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
  708. && (val[CMDS_SUPP_1] & SUPPORT_48_BIT)
  709. ) {
  710. bbbig = (uint64_t)val[LBA_64_MSB] << 48 |
  711. (uint64_t)val[LBA_48_MSB] << 32 |
  712. (uint64_t)val[LBA_MID] << 16 |
  713. val[LBA_LSB];
  714. printf("\tLBA48 user addressable sectors:%11"PRIu64"\n", bbbig);
  715. }
  716. if (!bbbig)
  717. bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
  718. printf("\tdevice size with M = 1024*1024: %11"PRIu64" MBytes\n", bbbig>>11);
  719. bbbig = (bbbig << 9) / 1000000;
  720. printf("\tdevice size with M = 1000*1000: %11"PRIu64" MBytes ", bbbig);
  721. if (bbbig > 1000)
  722. printf("(%"PRIu64" GB)\n", bbbig/1000);
  723. else
  724. puts("");
  725. }
  726. /* hw support of commands (capabilities) */
  727. printf("Capabilities:\n\t");
  728. if (dev == ATAPI_DEV) {
  729. if (eqpt != CDROM && (val[CAPAB_0] & CMD_Q_SUP)) printf("Cmd queuing, ");
  730. if (val[CAPAB_0] & OVLP_SUP) printf("Cmd overlap, ");
  731. }
  732. if (val[CAPAB_0] & LBA_SUP) printf("LBA, ");
  733. if (like_std != 1) {
  734. printf("IORDY%s(can%s be disabled)\n",
  735. !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "",
  736. (val[CAPAB_0] & IORDY_OFF) ? "" :"not");
  737. } else
  738. printf("no IORDY\n");
  739. if ((like_std == 1) && val[BUF_TYPE]) {
  740. printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE],
  741. (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector",
  742. (val[BUF_TYPE] > 2) ? " with read caching ability" : "");
  743. }
  744. if ((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1))) {
  745. printf("\tBuffer size: %.1fkB\n", (float)val[BUFFER__SIZE]/2);
  746. }
  747. if ((min_std < 4) && (val[RW_LONG])) {
  748. printf("\tbytes avail on r/w long: %u\n", val[RW_LONG]);
  749. }
  750. if ((eqpt != CDROM) && (like_std > 3)) {
  751. printf("\tQueue depth: %u\n", (val[QUEUE_DEPTH] & DEPTH_BITS) + 1);
  752. }
  753. if (dev == ATA_DEV) {
  754. if (like_std == 1)
  755. printf("\tCan%s perform double-word IO\n", (!val[DWORD_IO]) ? "not" : "");
  756. else {
  757. printf("\tStandby timer values: spec'd by %s", (val[CAPAB_0] & STD_STBY) ? "Standard" : "Vendor");
  758. if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
  759. printf(", %s device specific minimum\n", (val[CAPAB_1] & MIN_STANDBY_TIMER) ? "with" : "no");
  760. else
  761. puts("");
  762. }
  763. printf("\tR/W multiple sector transfer: ");
  764. if ((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
  765. printf("not supported\n");
  766. else {
  767. printf("Max = %u\tCurrent = ", val[SECTOR_XFER_MAX] & SECTOR_XFER);
  768. if (val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
  769. printf("%u\n", val[SECTOR_XFER_CUR] & SECTOR_XFER);
  770. else
  771. printf("?\n");
  772. }
  773. if ((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008)) {
  774. /* We print out elsewhere whether the APM feature is enabled or
  775. not. If it's not enabled, let's not repeat the info; just print
  776. nothing here. */
  777. printf("\tAdvancedPM level: ");
  778. if ((val[ADV_PWR] & 0xFF00) == 0x4000) {
  779. uint8_t apm_level = val[ADV_PWR] & 0x00FF;
  780. printf("%u (0x%x)\n", apm_level, apm_level);
  781. }
  782. else
  783. printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
  784. }
  785. if (like_std > 5 && val[ACOUSTIC]) {
  786. printf("\tRecommended acoustic management value: %u, current value: %u\n",
  787. (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
  788. }
  789. } else {
  790. /* ATAPI */
  791. if (eqpt != CDROM && (val[CAPAB_0] & SWRST_REQ))
  792. printf("\tATA sw reset required\n");
  793. if (val[PKT_REL] || val[SVC_NBSY]) {
  794. printf("\tOverlap support:");
  795. if (val[PKT_REL]) printf(" %uus to release bus.", val[PKT_REL]);
  796. if (val[SVC_NBSY]) printf(" %uus to clear BSY after SERVICE cmd.", val[SVC_NBSY]);
  797. puts("");
  798. }
  799. }
  800. /* DMA stuff. Check that only one DMA mode is selected. */
  801. printf("\tDMA: ");
  802. if (!(val[CAPAB_0] & DMA_SUP))
  803. printf("not supported\n");
  804. else {
  805. if (val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
  806. printf(" sdma%u\n", (val[DMA_MODE] & MODE) >> 8);
  807. if (val[SINGLE_DMA]) {
  808. jj = val[SINGLE_DMA];
  809. kk = val[SINGLE_DMA] >> 8;
  810. err_dma += mode_loop(jj, kk, 's', &have_mode);
  811. }
  812. if (val[MULTI_DMA]) {
  813. jj = val[MULTI_DMA];
  814. kk = val[MULTI_DMA] >> 8;
  815. err_dma += mode_loop(jj, kk, 'm', &have_mode);
  816. }
  817. if ((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) {
  818. jj = val[ULTRA_DMA];
  819. kk = val[ULTRA_DMA] >> 8;
  820. err_dma += mode_loop(jj, kk, 'u', &have_mode);
  821. }
  822. if (err_dma || !have_mode) printf("(?)");
  823. puts("");
  824. if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
  825. printf("\t\tInterleaved DMA support\n");
  826. if ((val[WHATS_VALID] & OK_W64_70)
  827. && (val[DMA_TIME_MIN] || val[DMA_TIME_NORM])
  828. ) {
  829. printf("\t\tCycle time:");
  830. if (val[DMA_TIME_MIN]) printf(" min=%uns", val[DMA_TIME_MIN]);
  831. if (val[DMA_TIME_NORM]) printf(" recommended=%uns", val[DMA_TIME_NORM]);
  832. puts("");
  833. }
  834. }
  835. /* Programmed IO stuff */
  836. printf("\tPIO: ");
  837. /* If a drive supports mode n (e.g. 3), it also supports all modes less
  838. * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
  839. if ((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP)) {
  840. jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
  841. for (ii = 0; ii <= PIO_MODE_MAX; ii++) {
  842. if (jj & 0x0001) printf("pio%d ", ii);
  843. jj >>=1;
  844. }
  845. puts("");
  846. } else if (((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE)) {
  847. for (ii = 0; ii <= val[PIO_MODE]>>8; ii++)
  848. printf("pio%d ", ii);
  849. puts("");
  850. } else
  851. printf("unknown\n");
  852. if (val[WHATS_VALID] & OK_W64_70) {
  853. if (val[PIO_NO_FLOW] || val[PIO_FLOW]) {
  854. printf("\t\tCycle time:");
  855. if (val[PIO_NO_FLOW]) printf(" no flow control=%uns", val[PIO_NO_FLOW]);
  856. if (val[PIO_FLOW]) printf(" IORDY flow control=%uns", val[PIO_FLOW]);
  857. puts("");
  858. }
  859. }
  860. if ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) {
  861. printf("Commands/features:\n\tEnabled\tSupported:\n");
  862. jj = val[CMDS_SUPP_0];
  863. kk = val[CMDS_EN_0];
  864. for (ii = 0; ii < NUM_CMD_FEAT_STR; ii++) {
  865. if ((jj & 0x8000) && (*cmd_feat_str[ii] != '\0')) {
  866. printf("\t%s\t%s\n", (kk & 0x8000) ? " *" : "", cmd_feat_str[ii]);
  867. }
  868. jj <<= 1;
  869. kk <<= 1;
  870. if (ii % 16 == 15) {
  871. jj = val[CMDS_SUPP_0+1+(ii/16)];
  872. kk = val[CMDS_EN_0+1+(ii/16)];
  873. }
  874. if (ii == 31) {
  875. if ((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
  876. ii +=16;
  877. }
  878. }
  879. }
  880. /* Removable Media Status Notification feature set */
  881. if ((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
  882. printf("\t%s supported\n", cmd_feat_str[27]);
  883. /* security */
  884. if ((eqpt != CDROM) && (like_std > 3)
  885. && (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME])
  886. ) {
  887. printf("Security:\n");
  888. if (val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
  889. printf("\tMaster password revision code = %u\n", val[PSWD_CODE]);
  890. jj = val[SECU_STATUS];
  891. if (jj) {
  892. for (ii = 0; ii < NUM_SECU_STR; ii++) {
  893. printf("\t%s\t%s\n", (!(jj & 0x0001)) ? "not" : "", secu_str[ii]);
  894. jj >>=1;
  895. }
  896. if (val[SECU_STATUS] & SECU_ENABLED) {
  897. printf("\tSecurity level %s\n", (val[SECU_STATUS] & SECU_LEVEL) ? "maximum" : "high");
  898. }
  899. }
  900. jj = val[ERASE_TIME] & ERASE_BITS;
  901. kk = val[ENH_ERASE_TIME] & ERASE_BITS;
  902. if (jj || kk) {
  903. printf("\t");
  904. if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, "");
  905. if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED ");
  906. puts("");
  907. }
  908. }
  909. /* reset result */
  910. jj = val[HWRST_RSLT];
  911. if ((jj & VALID) == VALID_VAL) {
  912. if (!(oo = (jj & RST0)))
  913. jj >>= 8;
  914. if ((jj & DEV_DET) == JUMPER_VAL)
  915. strng = " determined by the jumper";
  916. else if ((jj & DEV_DET) == CSEL_VAL)
  917. strng = " determined by CSEL";
  918. else
  919. strng = "";
  920. printf("HW reset results:\n\tCBLID- %s Vih\n\tDevice num = %i%s\n",
  921. (val[HWRST_RSLT] & CBLID) ? "above" : "below", !(oo), strng);
  922. }
  923. /* more stuff from std 5 */
  924. if ((like_std > 4) && (eqpt != CDROM)) {
  925. if (val[CFA_PWR_MODE] & VALID_W160) {
  926. printf("CFA power mode 1:\n\t%s%s\n", (val[CFA_PWR_MODE] & PWR_MODE_OFF) ? "disabled" : "enabled",
  927. (val[CFA_PWR_MODE] & PWR_MODE_REQ) ? " and required by some commands" : "");
  928. if (val[CFA_PWR_MODE] & MAX_AMPS)
  929. printf("\tMaximum current = %uma\n", val[CFA_PWR_MODE] & MAX_AMPS);
  930. }
  931. if ((val[INTEGRITY] & SIG) == SIG_VAL) {
  932. printf("Checksum: %scorrect\n", chksum ? "in" : "");
  933. }
  934. }
  935. exit(EXIT_SUCCESS);
  936. }
  937. #endif
  938. static smallint get_identity, get_geom;
  939. static smallint do_flush;
  940. static smallint do_ctimings, do_timings;
  941. static smallint reread_partn;
  942. static smallint set_piomode, noisy_piomode;
  943. static smallint set_readahead, get_readahead;
  944. static smallint set_readonly, get_readonly;
  945. static smallint set_unmask, get_unmask;
  946. static smallint set_mult, get_mult;
  947. static smallint set_dma_q, get_dma_q;
  948. static smallint set_nowerr, get_nowerr;
  949. static smallint set_keep, get_keep;
  950. static smallint set_io32bit, get_io32bit;
  951. static int piomode;
  952. static unsigned long Xreadahead;
  953. static unsigned long readonly;
  954. static unsigned long unmask;
  955. static unsigned long mult;
  956. static unsigned long dma_q;
  957. static unsigned long nowerr;
  958. static unsigned long keep;
  959. static unsigned long io32bit;
  960. #if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
  961. static unsigned long dma;
  962. static smallint set_dma, get_dma;
  963. #endif
  964. #ifdef HDIO_DRIVE_CMD
  965. static smallint set_xfermode, get_xfermode;
  966. static smallint set_dkeep, get_dkeep;
  967. static smallint set_standby, get_standby;
  968. static smallint set_lookahead, get_lookahead;
  969. static smallint set_prefetch, get_prefetch;
  970. static smallint set_defects, get_defects;
  971. static smallint set_wcache, get_wcache;
  972. static smallint set_doorlock, get_doorlock;
  973. static smallint set_seagate, get_seagate;
  974. static smallint set_standbynow, get_standbynow;
  975. static smallint set_sleepnow, get_sleepnow;
  976. static smallint get_powermode;
  977. static smallint set_apmmode, get_apmmode;
  978. static int xfermode_requested;
  979. static unsigned long dkeep;
  980. static unsigned long standby_requested;
  981. static unsigned long lookahead;
  982. static unsigned long prefetch;
  983. static unsigned long defects;
  984. static unsigned long wcache;
  985. static unsigned long doorlock;
  986. static unsigned long apmmode;
  987. #endif
  988. USE_FEATURE_HDPARM_GET_IDENTITY( static smallint get_IDentity;)
  989. USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( static smallint set_busstate, get_busstate;)
  990. USE_FEATURE_HDPARM_HDIO_DRIVE_RESET( static smallint perform_reset;)
  991. USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( static smallint perform_tristate;)
  992. USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(static smallint unregister_hwif;)
  993. USE_FEATURE_HDPARM_HDIO_SCAN_HWIF( static smallint scan_hwif;)
  994. USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( static unsigned long busstate;)
  995. USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF( static unsigned long tristate;)
  996. USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(static unsigned long hwif;)
  997. #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
  998. static unsigned long hwif_data;
  999. static unsigned long hwif_ctrl;
  1000. static unsigned long hwif_irq;
  1001. #endif
  1002. // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
  1003. // then the HDIO_GET_IDENTITY only returned 142 bytes.
  1004. // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
  1005. // and HDIO_GET_IDENTITY returns 512 bytes. But the latest
  1006. // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
  1007. // (which they should, but they should just return -EINVAL).
  1008. //
  1009. // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
  1010. // On a really old system, it will not, and we will be confused.
  1011. // Too bad, really.
  1012. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  1013. static const char *const cfg_str[] = {
  1014. "", "HardSect", "SoftSect", "NotMFM",
  1015. "HdSw>15uSec", "SpinMotCtl", "Fixed", "Removeable",
  1016. "DTR<=5Mbs", "DTR>5Mbs", "DTR>10Mbs", "RotSpdTol>.5%",
  1017. "dStbOff", "TrkOff", "FmtGapReq", "nonMagnetic"
  1018. };
  1019. static const char *const BuffType[] = {
  1020. "Unknown", "1Sect", "DualPort", "DualPortCache"
  1021. };
  1022. static void dump_identity(const struct hd_driveid *id)
  1023. {
  1024. int i;
  1025. const unsigned short int *id_regs = (const void*) id;
  1026. printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
  1027. id->model, id->fw_rev, id->serial_no);
  1028. for (i = 0; i <= 15; i++) {
  1029. if (id->config & (1<<i))
  1030. printf(" %s", cfg_str[i]);
  1031. }
  1032. printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n"
  1033. " BuffType=(%u) %s, BuffSize=%ukB, MaxMultSect=%u",
  1034. id->cyls, id->heads, id->sectors, id->track_bytes,
  1035. id->sector_bytes, id->ecc_bytes,
  1036. id->buf_type, BuffType[(id->buf_type > 3) ? 0 : id->buf_type],
  1037. id->buf_size/2, id->max_multsect);
  1038. if (id->max_multsect) {
  1039. printf(", MultSect=");
  1040. if (!(id->multsect_valid & 1))
  1041. printf("?%u?", id->multsect);
  1042. else if (id->multsect)
  1043. printf("%u", id->multsect);
  1044. else
  1045. printf("off");
  1046. }
  1047. puts("");
  1048. if (!(id->field_valid & 1))
  1049. printf(" (maybe):");
  1050. printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s", id->cur_cyls, id->cur_heads,
  1051. id->cur_sectors,
  1052. (BB_BIG_ENDIAN) ?
  1053. (unsigned long)(id->cur_capacity0 << 16) | id->cur_capacity1 :
  1054. (unsigned long)(id->cur_capacity1 << 16) | id->cur_capacity0,
  1055. ((id->capability&2) == 0) ? "no" : "yes");
  1056. if (id->capability & 2)
  1057. printf(", LBAsects=%u", id->lba_capacity);
  1058. printf("\n IORDY=%s", (id->capability & 8) ? (id->capability & 4) ? "on/off" : "yes" : "no");
  1059. if (((id->capability & 8) || (id->field_valid & 2)) && (id->field_valid & 2))
  1060. printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
  1061. if ((id->capability & 1) && (id->field_valid & 2))
  1062. printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
  1063. printf("\n PIO modes: ");
  1064. if (id->tPIO <= 5) {
  1065. printf("pio0 ");
  1066. if (id->tPIO >= 1) printf("pio1 ");
  1067. if (id->tPIO >= 2) printf("pio2 ");
  1068. }
  1069. if (id->field_valid & 2) {
  1070. if (id->eide_pio_modes & 1) printf("pio3 ");
  1071. if (id->eide_pio_modes & 2) printf("pio4 ");
  1072. if (id->eide_pio_modes &~3) printf("pio? ");
  1073. }
  1074. if (id->capability & 1) {
  1075. if (id->dma_1word | id->dma_mword) {
  1076. printf("\n DMA modes: ");
  1077. if (id->dma_1word & 0x100) printf("*");
  1078. if (id->dma_1word & 1) printf("sdma0 ");
  1079. if (id->dma_1word & 0x200) printf("*");
  1080. if (id->dma_1word & 2) printf("sdma1 ");
  1081. if (id->dma_1word & 0x400) printf("*");
  1082. if (id->dma_1word & 4) printf("sdma2 ");
  1083. if (id->dma_1word & 0xf800) printf("*");
  1084. if (id->dma_1word & 0xf8) printf("sdma? ");
  1085. if (id->dma_mword & 0x100) printf("*");
  1086. if (id->dma_mword & 1) printf("mdma0 ");
  1087. if (id->dma_mword & 0x200) printf("*");
  1088. if (id->dma_mword & 2) printf("mdma1 ");
  1089. if (id->dma_mword & 0x400) printf("*");
  1090. if (id->dma_mword & 4) printf("mdma2 ");
  1091. if (id->dma_mword & 0xf800) printf("*");
  1092. if (id->dma_mword & 0xf8) printf("mdma? ");
  1093. }
  1094. }
  1095. if (((id->capability & 8) || (id->field_valid & 2)) && id->field_valid & 4) {
  1096. printf("\n UDMA modes: ");
  1097. if (id->dma_ultra & 0x100) printf("*");
  1098. if (id->dma_ultra & 0x001) printf("udma0 ");
  1099. if (id->dma_ultra & 0x200) printf("*");
  1100. if (id->dma_ultra & 0x002) printf("udma1 ");
  1101. if (id->dma_ultra & 0x400) printf("*");
  1102. if (id->dma_ultra & 0x004) printf("udma2 ");
  1103. #ifdef __NEW_HD_DRIVE_ID
  1104. if (id->hw_config & 0x2000) {
  1105. #else /* !__NEW_HD_DRIVE_ID */
  1106. if (id->word93 & 0x2000) {
  1107. #endif /* __NEW_HD_DRIVE_ID */
  1108. if (id->dma_ultra & 0x0800) printf("*");
  1109. if (id->dma_ultra & 0x0008) printf("udma3 ");
  1110. if (id->dma_ultra & 0x1000) printf("*");
  1111. if (id->dma_ultra & 0x0010) printf("udma4 ");
  1112. if (id->dma_ultra & 0x2000) printf("*");
  1113. if (id->dma_ultra & 0x0020) printf("udma5 ");
  1114. if (id->dma_ultra & 0x4000) printf("*");
  1115. if (id->dma_ultra & 0x0040) printf("udma6 ");
  1116. if (id->dma_ultra & 0x8000) printf("*");
  1117. if (id->dma_ultra & 0x0080) printf("udma7 ");
  1118. }
  1119. }
  1120. printf("\n AdvancedPM=%s", (!(id_regs[83] & 8)) ? "no" : "yes");
  1121. if (id_regs[83] & 8) {
  1122. if (!(id_regs[86] & 8))
  1123. printf(": disabled (255)");
  1124. else if ((id_regs[91] & 0xFF00) != 0x4000)
  1125. printf(": unknown setting");
  1126. else
  1127. printf(": mode=0x%02X (%u)", id_regs[91] & 0xFF, id_regs[91] & 0xFF);
  1128. }
  1129. if (id_regs[82] & 0x20)
  1130. printf(" WriteCache=%s", (id_regs[85] & 0x20) ? "enabled" : "disabled");
  1131. #ifdef __NEW_HD_DRIVE_ID
  1132. if ((id->minor_rev_num && id->minor_rev_num <= 31)
  1133. || (id->major_rev_num && id->minor_rev_num <= 31)
  1134. ) {
  1135. printf("\n Drive conforms to: %s: ", (id->minor_rev_num <= 31) ? minor_str[id->minor_rev_num] : "Unknown");
  1136. if (id->major_rev_num != 0x0000 && /* NOVAL_0 */
  1137. id->major_rev_num != 0xFFFF) { /* NOVAL_1 */
  1138. for (i = 0; i <= 15; i++) {
  1139. if (id->major_rev_num & (1<<i))
  1140. printf(" ATA/ATAPI-%u", i);
  1141. }
  1142. }
  1143. }
  1144. #endif /* __NEW_HD_DRIVE_ID */
  1145. printf("\n\n * current active mode\n\n");
  1146. }
  1147. #endif
  1148. static void flush_buffer_cache(int fd)
  1149. {
  1150. fsync(fd); /* flush buffers */
  1151. ioctl_or_warn(fd, BLKFLSBUF, NULL); /* do it again, big time */
  1152. #ifdef HDIO_DRIVE_CMD
  1153. sleep(1);
  1154. if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) { /* await completion */
  1155. if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
  1156. bb_perror_msg("HDIO_DRIVE_CMD");
  1157. else
  1158. bb_perror_msg("ioctl %#x failed", HDIO_DRIVE_CMD);
  1159. }
  1160. #endif
  1161. }
  1162. static int seek_to_zero(int fd)
  1163. {
  1164. if (lseek(fd, (off_t) 0, SEEK_SET))
  1165. return 1;
  1166. return 0;
  1167. }
  1168. static int read_big_block(int fd, char *buf)
  1169. {
  1170. int i;
  1171. i = read(fd, buf, TIMING_BUF_BYTES);
  1172. if (i != TIMING_BUF_BYTES) {
  1173. bb_error_msg("read(%d bytes) failed (rc=%d)", TIMING_BUF_BYTES, i);
  1174. return 1;
  1175. }
  1176. /* access all sectors of buf to ensure the read fully completed */
  1177. for (i = 0; i < TIMING_BUF_BYTES; i += 512)
  1178. buf[i] &= 1;
  1179. return 0;
  1180. }
  1181. static int do_blkgetsize(int fd, unsigned long long *blksize64)
  1182. {
  1183. int rc;
  1184. unsigned blksize32 = 0;
  1185. if (0 == ioctl(fd, BLKGETSIZE64, blksize64)) { // returns bytes
  1186. *blksize64 /= 512;
  1187. return 0;
  1188. }
  1189. rc = ioctl_or_warn(fd, BLKGETSIZE, &blksize32); // returns sectors
  1190. *blksize64 = blksize32;
  1191. return rc;
  1192. }
  1193. static void print_timing(unsigned t, double e)
  1194. {
  1195. if (t >= e) /* more than 1MB/s */
  1196. printf("%4d MB in %.2f seconds = %.2f %cB/sec\n", t, e, t / e, 'M');
  1197. else
  1198. printf("%4d MB in %.2f seconds = %.2f %cB/sec\n", t, e, t / e * 1024, 'k');
  1199. }
  1200. static void do_time(int flag, int fd)
  1201. /* flag = 0 time_cache, 1 time_device */
  1202. {
  1203. static const struct itimerval thousand = {{1000, 0}, {1000, 0}};
  1204. struct itimerval itv;
  1205. unsigned elapsed, elapsed2;
  1206. unsigned max_iterations, total_MB, iterations;
  1207. unsigned long long blksize;
  1208. RESERVE_CONFIG_BUFFER(buf, TIMING_BUF_BYTES);
  1209. if (mlock(buf, TIMING_BUF_BYTES)) {
  1210. bb_perror_msg("mlock");
  1211. goto quit2;
  1212. }
  1213. max_iterations = 1024;
  1214. if (0 == do_blkgetsize(fd, &blksize)) {
  1215. max_iterations = blksize / (2 * 1024) / TIMING_BUF_MB;
  1216. }
  1217. /* Clear out the device request queues & give them time to complete */
  1218. sync();
  1219. sleep(2);
  1220. if (flag == 0) { /* Time cache */
  1221. if (seek_to_zero(fd))
  1222. goto quit;
  1223. if (read_big_block(fd, buf))
  1224. goto quit;
  1225. printf(" Timing buffer-cache reads: ");
  1226. } else { /* Time device */
  1227. printf(" Timing buffered disk reads: ");
  1228. }
  1229. fflush(stdout);
  1230. iterations = 0;
  1231. /*
  1232. * getitimer() is used rather than gettimeofday() because
  1233. * it is much more consistent (on my machine, at least).
  1234. */
  1235. setitimer(ITIMER_REAL, &thousand, NULL);
  1236. /* Now do the timing */
  1237. do {
  1238. ++iterations;
  1239. if ((flag == 0) && seek_to_zero(fd))
  1240. goto quit;
  1241. if (read_big_block(fd, buf))
  1242. goto quit;
  1243. getitimer(ITIMER_REAL, &itv);
  1244. elapsed = (1000 - itv.it_value.tv_sec) * 1000000
  1245. - itv.it_value.tv_usec;
  1246. } while (elapsed < 3000000 && iterations < max_iterations);
  1247. total_MB = iterations * TIMING_BUF_MB;
  1248. if (flag == 0) {
  1249. /* Now remove the lseek() and getitimer() overheads from the elapsed time */
  1250. setitimer(ITIMER_REAL, &thousand, NULL);
  1251. do {
  1252. if (seek_to_zero(fd))
  1253. goto quit;
  1254. getitimer(ITIMER_REAL, &itv);
  1255. elapsed2 = (1000 - itv.it_value.tv_sec) * 1000000
  1256. - itv.it_value.tv_usec;
  1257. } while (--iterations);
  1258. elapsed -= elapsed2;
  1259. total_MB *= BUFCACHE_FACTOR;
  1260. flush_buffer_cache(fd);
  1261. }
  1262. print_timing(total_MB, elapsed / 1000000.0);
  1263. quit:
  1264. munlock(buf, TIMING_BUF_BYTES);
  1265. quit2:
  1266. RELEASE_CONFIG_BUFFER(buf);
  1267. }
  1268. #if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1269. static void bus_state_value(unsigned value)
  1270. {
  1271. if (value == BUSSTATE_ON)
  1272. on_off(1);
  1273. else if (value == BUSSTATE_OFF)
  1274. on_off(0);
  1275. else if (value == BUSSTATE_TRISTATE)
  1276. printf(" (tristate)\n");
  1277. else
  1278. printf(" (unknown: %d)\n", value);
  1279. }
  1280. #endif
  1281. #ifdef HDIO_DRIVE_CMD
  1282. static void interpret_standby(unsigned standby)
  1283. {
  1284. unsigned t;
  1285. printf(" (");
  1286. if (standby == 0)
  1287. printf("off");
  1288. else if (standby == 252)
  1289. printf("21 minutes");
  1290. else if (standby == 253)
  1291. printf("vendor-specific");
  1292. else if (standby == 254)
  1293. printf("Reserved");
  1294. else if (standby == 255)
  1295. printf("21 minutes + 15 seconds");
  1296. else if (standby <= 240) {
  1297. t = standby * 5;
  1298. printf("%u minutes + %u seconds", t / 60, t % 60);
  1299. } else if (standby <= 251) {
  1300. t = (standby - 240) * 30;
  1301. printf("%u hours + %u minutes", t / 60, t % 60);
  1302. } else
  1303. printf("illegal value");
  1304. printf(")\n");
  1305. }
  1306. static const uint8_t xfermode_val[] ALIGN1 = {
  1307. 8, 9, 10, 11, 12, 13, 14, 15,
  1308. 16, 17, 18, 19, 20, 21, 22, 23,
  1309. 32, 33, 34, 35, 36, 37, 38, 39,
  1310. 64, 65, 66, 67, 68, 69, 70, 71
  1311. };
  1312. /* NB: we save size by _not_ storing terninating NUL! */
  1313. static const char xfermode_name[][5] ALIGN1 = {
  1314. "pio0", "pio1", "pio2", "pio3", "pio4", "pio5", "pio6", "pio7",
  1315. "sdma0","sdma1","sdma2","sdma3","sdma4","sdma5","sdma6","sdma7",
  1316. "mdma0","mdma1","mdma2","mdma3","mdma4","mdma5","mdma6","mdma7",
  1317. "udma0","udma1","udma2","udma3","udma4","udma5","udma6","udma7"
  1318. };
  1319. static int translate_xfermode(const char *name)
  1320. {
  1321. int val, i;
  1322. for (i = 0; i < ARRAY_SIZE(xfermode_val); i++) {
  1323. if (!strncmp(name, xfermode_name[i], 5))
  1324. if (strlen(name) <= 5)
  1325. return xfermode_val[i];
  1326. }
  1327. /* Negative numbers are invalid and are caught later */
  1328. val = bb_strtoi(name, NULL, 10);
  1329. if (!errno)
  1330. return val;
  1331. return -1;
  1332. }
  1333. static void interpret_xfermode(unsigned xfermode)
  1334. {
  1335. printf(" (");
  1336. if (xfermode == 0)
  1337. printf("default PIO mode");
  1338. else if (xfermode == 1)
  1339. printf("default PIO mode, disable IORDY");
  1340. else if (xfermode >= 8 && xfermode <= 15)
  1341. printf("PIO flow control mode%u", xfermode - 8);
  1342. else if (xfermode >= 16 && xfermode <= 23)
  1343. printf("singleword DMA mode%u", xfermode - 16);
  1344. else if (xfermode >= 32 && xfermode <= 39)
  1345. printf("multiword DMA mode%u", xfermode - 32);
  1346. else if (xfermode >= 64 && xfermode <= 71)
  1347. printf("UltraDMA mode%u", xfermode - 64);
  1348. else
  1349. printf("Unknown");
  1350. printf(")\n");
  1351. }
  1352. #endif /* HDIO_DRIVE_CMD */
  1353. static void print_flag(int flag, const char *s, unsigned long value)
  1354. {
  1355. if (flag)
  1356. printf(" setting %s to %ld\n", s, value);
  1357. }
  1358. static void process_dev(char *devname)
  1359. {
  1360. int fd;
  1361. long parm, multcount;
  1362. #ifndef HDIO_DRIVE_CMD
  1363. int force_operation = 0;
  1364. #endif
  1365. /* Please restore args[n] to these values after each ioctl
  1366. except for args[2] */
  1367. unsigned char args[4] = { WIN_SETFEATURES, 0, 0, 0 };
  1368. const char *fmt = " %s\t= %2ld";
  1369. fd = xopen(devname, O_RDONLY|O_NONBLOCK);
  1370. printf("\n%s:\n", devname);
  1371. if (set_readahead) {
  1372. print_flag(get_readahead, "fs readahead", Xreadahead);
  1373. ioctl_or_warn(fd, BLKRASET, (int *)Xreadahead);
  1374. }
  1375. #if ENABLE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
  1376. if (unregister_hwif) {
  1377. printf(" attempting to unregister hwif#%lu\n", hwif);
  1378. ioctl_or_warn(fd, HDIO_UNREGISTER_HWIF, (int *)(unsigned long)hwif);
  1379. }
  1380. #endif
  1381. #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
  1382. if (scan_hwif) {
  1383. printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq);
  1384. args[0] = hwif_data;
  1385. args[1] = hwif_ctrl;
  1386. args[2] = hwif_irq;
  1387. ioctl_or_warn(fd, HDIO_SCAN_HWIF, args);
  1388. args[0] = WIN_SETFEATURES;
  1389. args[1] = 0;
  1390. }
  1391. #endif
  1392. if (set_piomode) {
  1393. if (noisy_piomode) {
  1394. printf(" attempting to ");
  1395. if (piomode == 255)
  1396. printf("auto-tune PIO mode\n");
  1397. else if (piomode < 100)
  1398. printf("set PIO mode to %d\n", piomode);
  1399. else if (piomode < 200)
  1400. printf("set MDMA mode to %d\n", (piomode-100));
  1401. else
  1402. printf("set UDMA mode to %d\n", (piomode-200));
  1403. }
  1404. ioctl_or_warn(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode);
  1405. }
  1406. if (set_io32bit) {
  1407. print_flag(get_io32bit, "32-bit IO_support flag", io32bit);
  1408. ioctl_or_warn(fd, HDIO_SET_32BIT, (int *)io32bit);
  1409. }
  1410. if (set_mult) {
  1411. print_flag(get_mult, "multcount", mult);
  1412. #ifdef HDIO_DRIVE_CMD
  1413. ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult);
  1414. #else
  1415. force_operation |= (!ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult));
  1416. #endif
  1417. }
  1418. if (set_readonly) {
  1419. print_flag_on_off(get_readonly, "readonly", readonly);
  1420. ioctl_or_warn(fd, BLKROSET, &readonly);
  1421. }
  1422. if (set_unmask) {
  1423. print_flag_on_off(get_unmask, "unmaskirq", unmask);
  1424. ioctl_or_warn(fd, HDIO_SET_UNMASKINTR, (int *)unmask);
  1425. }
  1426. #if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
  1427. if (set_dma) {
  1428. print_flag_on_off(get_dma, "using_dma", dma);
  1429. ioctl_or_warn(fd, HDIO_SET_DMA, (int *)dma);
  1430. }
  1431. #endif /* FEATURE_HDPARM_HDIO_GETSET_DMA */
  1432. if (set_dma_q) {
  1433. print_flag_on_off(get_dma_q, "DMA queue_depth", dma_q);
  1434. ioctl_or_warn(fd, HDIO_SET_QDMA, (int *)dma_q);
  1435. }
  1436. if (set_nowerr) {
  1437. print_flag_on_off(get_nowerr, "nowerr", nowerr);
  1438. ioctl_or_warn(fd, HDIO_SET_NOWERR, (int *)nowerr);
  1439. }
  1440. if (set_keep) {
  1441. print_flag_on_off(get_keep, "keep_settings", keep);
  1442. ioctl_or_warn(fd, HDIO_SET_KEEPSETTINGS, (int *)keep);
  1443. }
  1444. #ifdef HDIO_DRIVE_CMD
  1445. if (set_doorlock) {
  1446. args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
  1447. args[2] = 0;
  1448. print_flag_on_off(get_doorlock, "drive doorlock", doorlock);
  1449. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1450. args[0] = WIN_SETFEATURES;
  1451. }
  1452. if (set_dkeep) {
  1453. /* lock/unlock the drive's "feature" settings */
  1454. print_flag_on_off(get_dkeep, "drive keep features", dkeep);
  1455. args[2] = dkeep ? 0x66 : 0xcc;
  1456. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1457. }
  1458. if (set_defects) {
  1459. args[2] = defects ? 0x04 : 0x84;
  1460. print_flag(get_defects, "drive defect-mgmt", defects);
  1461. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1462. }
  1463. if (set_prefetch) {
  1464. args[1] = prefetch;
  1465. args[2] = 0xab;
  1466. print_flag(get_prefetch, "drive prefetch", prefetch);
  1467. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1468. args[1] = 0;
  1469. }
  1470. if (set_xfermode) {
  1471. args[1] = xfermode_requested;
  1472. args[2] = 3;
  1473. if (get_xfermode) {
  1474. print_flag(1, "xfermode", xfermode_requested);
  1475. interpret_xfermode(xfermode_requested);
  1476. }
  1477. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1478. args[1] = 0;
  1479. }
  1480. if (set_lookahead) {
  1481. args[2] = lookahead ? 0xaa : 0x55;
  1482. print_flag_on_off(get_lookahead, "drive read-lookahead", lookahead);
  1483. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1484. }
  1485. if (set_apmmode) {
  1486. args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */; /* feature register */
  1487. args[1] = apmmode; /* sector count register 1-255 */
  1488. if (get_apmmode)
  1489. printf(" setting APM level to %s 0x%02lX (%ld)\n", (apmmode == 255) ? "disabled" : "", apmmode, apmmode);
  1490. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1491. args[1] = 0;
  1492. }
  1493. if (set_wcache) {
  1494. #ifdef DO_FLUSHCACHE
  1495. #ifndef WIN_FLUSHCACHE
  1496. #define WIN_FLUSHCACHE 0xe7
  1497. #endif
  1498. static unsigned char flushcache[4] = { WIN_FLUSHCACHE, 0, 0, 0 };
  1499. #endif /* DO_FLUSHCACHE */
  1500. args[2] = wcache ? 0x02 : 0x82;
  1501. print_flag_on_off(get_wcache, "drive write-caching", wcache);
  1502. #ifdef DO_FLUSHCACHE
  1503. if (!wcache)
  1504. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
  1505. #endif /* DO_FLUSHCACHE */
  1506. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1507. #ifdef DO_FLUSHCACHE
  1508. if (!wcache)
  1509. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
  1510. #endif /* DO_FLUSHCACHE */
  1511. }
  1512. /* In code below, we do not preserve args[0], but the rest
  1513. is preserved, including args[2] */
  1514. args[2] = 0;
  1515. if (set_standbynow) {
  1516. #ifndef WIN_STANDBYNOW1
  1517. #define WIN_STANDBYNOW1 0xE0
  1518. #endif
  1519. #ifndef WIN_STANDBYNOW2
  1520. #define WIN_STANDBYNOW2 0x94
  1521. #endif
  1522. if (get_standbynow) printf(" issuing standby command\n");
  1523. args[0] = WIN_STANDBYNOW1;
  1524. ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2);
  1525. }
  1526. if (set_sleepnow) {
  1527. #ifndef WIN_SLEEPNOW1
  1528. #define WIN_SLEEPNOW1 0xE6
  1529. #endif
  1530. #ifndef WIN_SLEEPNOW2
  1531. #define WIN_SLEEPNOW2 0x99
  1532. #endif
  1533. if (get_sleepnow) printf(" issuing sleep command\n");
  1534. args[0] = WIN_SLEEPNOW1;
  1535. ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2);
  1536. }
  1537. if (set_seagate) {
  1538. args[0] = 0xfb;
  1539. if (get_seagate) printf(" disabling Seagate auto powersaving mode\n");
  1540. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1541. }
  1542. if (set_standby) {
  1543. args[0] = WIN_SETIDLE1;
  1544. args[1] = standby_requested;
  1545. if (get_standby) {
  1546. print_flag(1, "standby", standby_requested);
  1547. interpret_standby(standby_requested);
  1548. }
  1549. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1550. args[1] = 0;
  1551. }
  1552. #else /* HDIO_DRIVE_CMD */
  1553. if (force_operation) {
  1554. char buf[512];
  1555. flush_buffer_cache(fd);
  1556. if (-1 == read(fd, buf, sizeof(buf)))
  1557. bb_perror_msg("read(%d bytes) failed (rc=%d)", sizeof(buf), -1);
  1558. }
  1559. #endif /* HDIO_DRIVE_CMD */
  1560. if (get_mult || get_identity) {
  1561. multcount = -1;
  1562. if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount)) {
  1563. if (get_mult && ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn. */
  1564. bb_perror_msg("HDIO_GET_MULTCOUNT");
  1565. else
  1566. bb_perror_msg("ioctl %#x failed", HDIO_GET_MULTCOUNT);
  1567. } else if (get_mult) {
  1568. printf(fmt, "multcount", multcount);
  1569. on_off(multcount != 0);
  1570. }
  1571. }
  1572. if (get_io32bit) {
  1573. if (!ioctl_or_warn(fd, HDIO_GET_32BIT, &parm)) {
  1574. printf(" IO_support\t=%3ld (", parm);
  1575. if (parm == 0)
  1576. printf("default 16-bit)\n");
  1577. else if (parm == 2)
  1578. printf("16-bit)\n");
  1579. else if (parm == 1)
  1580. printf("32-bit)\n");
  1581. else if (parm == 3)
  1582. printf("32-bit w/sync)\n");
  1583. else if (parm == 8)
  1584. printf("Request-Queue-Bypass)\n");
  1585. else
  1586. printf("\?\?\?)\n");
  1587. }
  1588. }
  1589. if (get_unmask) {
  1590. if(!ioctl_or_warn(fd, HDIO_GET_UNMASKINTR, (unsigned long *)parm))
  1591. print_value_on_off("unmaskirq", parm);
  1592. }
  1593. #if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
  1594. if (get_dma) {
  1595. if (!ioctl_or_warn(fd, HDIO_GET_DMA, &parm)) {
  1596. printf(fmt, "using_dma", parm);
  1597. if (parm == 8)
  1598. printf(" (DMA-Assisted-PIO)\n");
  1599. else
  1600. on_off(parm != 0);
  1601. }
  1602. }
  1603. #endif
  1604. if (get_dma_q) {
  1605. if(!ioctl_or_warn(fd, HDIO_GET_QDMA, (unsigned long *)parm))
  1606. print_value_on_off("queue_depth", parm);
  1607. }
  1608. if (get_keep) {
  1609. if(!ioctl_or_warn(fd, HDIO_GET_KEEPSETTINGS, (unsigned long *)parm))
  1610. print_value_on_off("keepsettings", parm);
  1611. }
  1612. if (get_nowerr) {
  1613. if(!ioctl_or_warn(fd, HDIO_GET_NOWERR, (unsigned long *)parm))
  1614. print_value_on_off("nowerr", parm);
  1615. }
  1616. if (get_readonly) {
  1617. if(!ioctl_or_warn(fd, BLKROGET, (unsigned long *)parm))
  1618. print_value_on_off("readonly", parm);
  1619. }
  1620. if (get_readahead) {
  1621. if(!ioctl_or_warn(fd, BLKRAGET, (unsigned long *)parm))
  1622. print_value_on_off("readahead", parm);
  1623. }
  1624. if (get_geom) {
  1625. if (!ioctl_or_warn(fd, BLKGETSIZE, &parm)) {
  1626. struct hd_geometry g;
  1627. if (!ioctl_or_warn(fd, HDIO_GETGEO, &g))
  1628. printf(" geometry\t= %u/%u/%u, sectors = %ld, start = %ld\n",
  1629. g.cylinders, g.heads, g.sectors, parm, g.start);
  1630. }
  1631. }
  1632. #ifdef HDIO_DRIVE_CMD
  1633. if (get_powermode) {
  1634. #ifndef WIN_CHECKPOWERMODE1
  1635. #define WIN_CHECKPOWERMODE1 0xE5
  1636. #endif
  1637. #ifndef WIN_CHECKPOWERMODE2
  1638. #define WIN_CHECKPOWERMODE2 0x98
  1639. #endif
  1640. const char *state;
  1641. args[0] = WIN_CHECKPOWERMODE1;
  1642. if (ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2)) {
  1643. if (errno != EIO || args[0] != 0 || args[1] != 0)
  1644. state = "unknown";
  1645. else
  1646. state = "sleeping";
  1647. } else
  1648. state = (args[2] == 255) ? "active/idle" : "standby";
  1649. args[1] = args[2] = 0;
  1650. printf(" drive state is: %s\n", state);
  1651. }
  1652. #endif
  1653. #if ENABLE_FEATURE_HDPARM_HDIO_DRIVE_RESET
  1654. if (perform_reset) {
  1655. ioctl_or_warn(fd, HDIO_DRIVE_RESET, NULL);
  1656. }
  1657. #endif /* FEATURE_HDPARM_HDIO_DRIVE_RESET */
  1658. #if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1659. if (perform_tristate) {
  1660. args[0] = 0;
  1661. args[1] = tristate;
  1662. ioctl_or_warn(fd, HDIO_TRISTATE_HWIF, &args);
  1663. }
  1664. #endif /* FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
  1665. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  1666. if (get_identity) {
  1667. struct hd_driveid id;
  1668. if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
  1669. if (multcount != -1) {
  1670. id.multsect = multcount;
  1671. id.multsect_valid |= 1;
  1672. } else
  1673. id.multsect_valid &= ~1;
  1674. dump_identity(&id);
  1675. } else if (errno == -ENOMSG)
  1676. printf(" no identification info available\n");
  1677. else if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
  1678. bb_perror_msg("HDIO_GET_IDENTITY");
  1679. else
  1680. bb_perror_msg("ioctl %#x failed", HDIO_GET_IDENTITY);
  1681. }
  1682. if (get_IDentity) {
  1683. unsigned char args1[4+512]; /* = { ... } will eat 0.5k of rodata! */
  1684. memset(args1, 0, sizeof(args1));
  1685. args1[0] = WIN_IDENTIFY;
  1686. args1[3] = 1;
  1687. if (!ioctl_alt_or_warn(fd, HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY))
  1688. identify((void *)(args1 + 4));
  1689. }
  1690. #endif
  1691. #if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1692. if (set_busstate) {
  1693. if (get_busstate) {
  1694. print_flag(1, "bus state", busstate);
  1695. bus_state_value(busstate);
  1696. }
  1697. ioctl_or_warn(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate);
  1698. }
  1699. if (get_busstate) {
  1700. if (!ioctl_or_warn(fd, HDIO_GET_BUSSTATE, &parm)) {
  1701. printf(fmt, "bus state", parm);
  1702. bus_state_value(parm);
  1703. }
  1704. }
  1705. #endif
  1706. if (reread_partn)
  1707. ioctl_or_warn(fd, BLKRRPART, NULL);
  1708. if (do_ctimings)
  1709. do_time(0, fd); /* time cache */
  1710. if (do_timings)
  1711. do_time(1, fd); /* time device */
  1712. if (do_flush)
  1713. flush_buffer_cache(fd);
  1714. close(fd);
  1715. }
  1716. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  1717. static int fromhex(unsigned char c)
  1718. {
  1719. if (isdigit(c))
  1720. return (c - '0');
  1721. if (c >= 'a' && c <= 'f')
  1722. return (c - ('a' - 10));
  1723. bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
  1724. }
  1725. static void identify_from_stdin(void)
  1726. {
  1727. uint16_t sbuf[256];
  1728. unsigned char buf[1280];
  1729. unsigned char *b = (unsigned char *)buf;
  1730. int i;
  1731. xread(0, buf, 1280);
  1732. // Convert the newline-separated hex data into an identify block.
  1733. for (i = 0; i < 256; i++) {
  1734. int j;
  1735. for (j = 0; j < 4; j++)
  1736. sbuf[i] = (sbuf[i] << 4) + fromhex(*(b++));
  1737. }
  1738. // Parse the data.
  1739. identify(sbuf);
  1740. }
  1741. #endif
  1742. /* busybox specific stuff */
  1743. static void parse_opts(smallint *get, smallint *set, unsigned long *value, int min, int max)
  1744. {
  1745. if (get) {
  1746. *get = 1;
  1747. }
  1748. if (optarg) {
  1749. *set = 1;
  1750. *value = xatol_range(optarg, min, max);
  1751. }
  1752. }
  1753. static void parse_xfermode(int flag, smallint *get, smallint *set, int *value)
  1754. {
  1755. if (flag) {
  1756. *get = 1;
  1757. if (optarg) {
  1758. *value = translate_xfermode(optarg);
  1759. *set = (*value > -1);
  1760. }
  1761. }
  1762. }
  1763. /*------- getopt short options --------*/
  1764. static const char hdparm_options[] ALIGN1 =
  1765. "gfu::n::p:r::m::c::k::a::B:tTh"
  1766. USE_FEATURE_HDPARM_GET_IDENTITY("iI")
  1767. USE_FEATURE_HDPARM_HDIO_GETSET_DMA("d::")
  1768. #ifdef HDIO_DRIVE_CMD
  1769. "S:D:P:X:K:A:L:W:CyYzZ"
  1770. #endif
  1771. USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF("U:")
  1772. #ifdef HDIO_GET_QDMA
  1773. #ifdef HDIO_SET_QDMA
  1774. "Q:"
  1775. #else
  1776. "Q"
  1777. #endif
  1778. #endif
  1779. USE_FEATURE_HDPARM_HDIO_DRIVE_RESET("w")
  1780. USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF("x::b:")
  1781. USE_FEATURE_HDPARM_HDIO_SCAN_HWIF("R:");
  1782. /*-------------------------------------*/
  1783. /* our main() routine: */
  1784. int hdparm_main(int argc, char **argv);
  1785. int hdparm_main(int argc, char **argv)
  1786. {
  1787. int c;
  1788. int flagcount = 0;
  1789. while ((c = getopt(argc, argv, hdparm_options)) >= 0) {
  1790. flagcount++;
  1791. if (c == 'h') bb_show_usage(); /* EXIT */
  1792. USE_FEATURE_HDPARM_GET_IDENTITY(get_IDentity |= (c == 'I'));
  1793. USE_FEATURE_HDPARM_GET_IDENTITY(get_identity |= (c == 'i'));
  1794. get_geom |= (c == 'g');
  1795. do_flush |= (c == 'f');
  1796. if (c == 'u') parse_opts(&get_unmask, &set_unmask, &unmask, 0, 1);
  1797. USE_FEATURE_HDPARM_HDIO_GETSET_DMA(if (c == 'd') parse_opts(&get_dma, &set_dma, &dma, 0, 9));
  1798. if (c == 'n') parse_opts(&get_nowerr, &set_nowerr, &nowerr, 0, 1);
  1799. parse_xfermode((c == 'p'), &noisy_piomode, &set_piomode, &piomode);
  1800. if (c == 'r') parse_opts(&get_readonly, &set_readonly, &readonly, 0, 1);
  1801. if (c == 'm') parse_opts(&get_mult, &set_mult, &mult, 0, INT_MAX /*32*/);
  1802. if (c == 'c') parse_opts(&get_io32bit, &set_io32bit, &io32bit, 0, INT_MAX /*8*/);
  1803. if (c == 'k') parse_opts(&get_keep, &set_keep, &keep, 0, 1);
  1804. if (c == 'a') parse_opts(&get_readahead, &set_readahead, &Xreadahead, 0, INT_MAX);
  1805. if (c == 'B') parse_opts(&get_apmmode, &set_apmmode, &apmmode, 1, 255);
  1806. do_flush |= do_timings |= (c == 't');
  1807. do_flush |= do_ctimings |= (c == 'T');
  1808. #ifdef HDIO_DRIVE_CMD
  1809. if (c == 'S') parse_opts(&get_standby, &set_standby, &standby_requested, 0, INT_MAX);
  1810. if (c == 'D') parse_opts(&get_defects, &set_defects, &defects, 0, INT_MAX);
  1811. if (c == 'P') parse_opts(&get_prefetch, &set_prefetch, &prefetch, 0, INT_MAX);
  1812. parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
  1813. if (c == 'K') parse_opts(&get_dkeep, &set_dkeep, &prefetch, 0, 1);
  1814. if (c == 'A') parse_opts(&get_lookahead, &set_lookahead, &lookahead, 0, 1);
  1815. if (c == 'L') parse_opts(&get_doorlock, &set_doorlock, &doorlock, 0, 1);
  1816. if (c == 'W') parse_opts(&get_wcache, &set_wcache, &wcache, 0, 1);
  1817. get_powermode |= (c == 'C');
  1818. get_standbynow = set_standbynow |= (c == 'y');
  1819. get_sleepnow = set_sleepnow |= (c == 'Y');
  1820. reread_partn |= (c == 'z');
  1821. get_seagate = set_seagate |= (c == 'Z');
  1822. #endif
  1823. USE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') parse_opts(NULL, &unregister_hwif, &hwif, 0, INT_MAX));
  1824. #ifdef HDIO_GET_QDMA
  1825. if (c == 'Q') {
  1826. #ifdef HDIO_SET_QDMA
  1827. parse_opts(&get_dma_q, &set_dma_q, &dma_q, 0, INT_MAX);
  1828. #else
  1829. parse_opts(&get_dma_q, NULL, NULL, 0, 0);
  1830. #endif
  1831. }
  1832. #endif
  1833. USE_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
  1834. USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') parse_opts(NULL, &perform_tristate, &tristate, 0, 1));
  1835. USE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') parse_opts(&get_busstate, &set_busstate, &busstate, 0, 2));
  1836. #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
  1837. if (c == 'R') {
  1838. parse_opts(NULL, &scan_hwif, &hwif_data, 0, INT_MAX);
  1839. hwif_ctrl = xatoi_u((argv[optind]) ? argv[optind] : "");
  1840. hwif_irq = xatoi_u((argv[optind+1]) ? argv[optind+1] : "");
  1841. /* Move past the 2 additional arguments */
  1842. argv += 2;
  1843. argc -= 2;
  1844. }
  1845. #endif
  1846. }
  1847. /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
  1848. if (!flagcount) {
  1849. get_mult = get_io32bit = get_unmask = get_keep = get_readonly = get_readahead = get_geom = 1;
  1850. USE_FEATURE_HDPARM_HDIO_GETSET_DMA(get_dma = 1);
  1851. }
  1852. argv += optind;
  1853. if (!*argv) {
  1854. if (ENABLE_FEATURE_HDPARM_GET_IDENTITY && !isatty(STDIN_FILENO))
  1855. identify_from_stdin(); /* EXIT */
  1856. else bb_show_usage();
  1857. }
  1858. do {
  1859. process_dev(*argv++);
  1860. } while (*argv);
  1861. return EXIT_SUCCESS;
  1862. }