hdparm.c 72 KB

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