hdparm.c 70 KB

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