hdparm.c 74 KB

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