hdparm.c 72 KB

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