3
0

hdparm.c 74 KB

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