hdparm.c 69 KB

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