3
0

hdparm.c 72 KB

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