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 GPLv2 or later, see file LICENSE in this source tree.
  9. *
  10. * This program is based on the source code of hdparm: see below...
  11. * hdparm.c - Command line interface to get/set hard disk parameters
  12. * - by Mark Lord (C) 1994-2002 -- freely distributable
  13. */
  14. #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)
  679. printf("\n\tUsed: %s ", nth_string(minor_str, val[MINOR]));
  680. }
  681. /* looks like when they up-issue the std, they obsolete one;
  682. * thus, only the newest 4 issues need be supported. (That's
  683. * what "kk" and "min_std" are all about.) */
  684. if (val[MAJOR] && (val[MAJOR] != NOVAL_1)) {
  685. printf("\n\tSupported: ");
  686. jj = val[MAJOR] << 1;
  687. kk = like_std >4 ? like_std-4: 0;
  688. for (ii = 14; (ii >0)&&(ii>kk); ii--) {
  689. if (jj & 0x8000) {
  690. printf("%u ", ii);
  691. if (like_std < ii) {
  692. like_std = ii;
  693. kk = like_std >4 ? like_std-4: 0;
  694. }
  695. if (min_std > ii) min_std = ii;
  696. }
  697. jj <<= 1;
  698. }
  699. if (like_std < 3) like_std = 3;
  700. }
  701. /* Figure out what standard the device is using if it hasn't told
  702. * us. If we know the std, check if the device is using any of
  703. * the words from the next level up. It happens.
  704. */
  705. if (like_std < std) like_std = std;
  706. if (((std == 5) || (!std && (like_std < 6))) &&
  707. ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  708. (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
  709. ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
  710. ( val[CMDS_SUPP_2] & CMDS_W84) ) )
  711. ) {
  712. like_std = 6;
  713. } else if (((std == 4) || (!std && (like_std < 5))) &&
  714. ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
  715. (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
  716. ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  717. (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
  718. {
  719. like_std = 5;
  720. } else if (((std == 3) || (!std && (like_std < 4))) &&
  721. ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  722. ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
  723. (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
  724. (( val[CAPAB_1] & VALID) == VALID_VAL) ||
  725. (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
  726. (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) )
  727. ) {
  728. like_std = 4;
  729. } else if (((std == 2) || (!std && (like_std < 3)))
  730. && ((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
  731. ) {
  732. like_std = 3;
  733. } else if (((std == 1) || (!std && (like_std < 2))) &&
  734. ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
  735. (val[WHATS_VALID] & OK_W64_70)) )
  736. {
  737. like_std = 2;
  738. }
  739. if (!std)
  740. printf("\n\tLikely used: %u\n", like_std);
  741. else if (like_std > std)
  742. printf("& some of %u\n", like_std);
  743. else
  744. bb_putchar('\n');
  745. } else {
  746. /* TBD: do CDROM stuff more thoroughly. For now... */
  747. kk = 0;
  748. if (val[CDR_MINOR] == 9) {
  749. kk = 1;
  750. printf("\n\tUsed: ATAPI for CD-ROMs, SFF-8020i, r2.5");
  751. }
  752. if (val[CDR_MAJOR] && (val[CDR_MAJOR] !=NOVAL_1)) {
  753. kk = 1;
  754. printf("\n\tSupported: CD-ROM ATAPI");
  755. jj = val[CDR_MAJOR] >> 1;
  756. for (ii = 1; ii < 15; ii++) {
  757. if (jj & 0x0001) printf("-%u ", ii);
  758. jj >>= 1;
  759. }
  760. }
  761. puts(kk ? "" : "\n\tLikely used CD-ROM ATAPI-1");
  762. /* the cdrom stuff is more like ATA-2 than anything else, so: */
  763. like_std = 2;
  764. }
  765. if (min_std == 0xffff)
  766. min_std = like_std > 4 ? like_std - 3 : 1;
  767. printf("Configuration:\n");
  768. /* more info from the general configuration word */
  769. if ((eqpt != CDROM) && (like_std == 1)) {
  770. jj = val[GEN_CONFIG] >> 1;
  771. for (ii = 1; ii < 15; ii++) {
  772. if (jj & 0x0001)
  773. printf("\t%s\n", nth_string(ata1_cfg_str, ii));
  774. jj >>=1;
  775. }
  776. }
  777. if (dev == ATAPI_DEV) {
  778. if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_3MS_VAL)
  779. strng = "3ms";
  780. else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_INTR_VAL)
  781. strng = "<=10ms with INTRQ";
  782. else if ((val[GEN_CONFIG] & DRQ_RESPONSE_TIME) == DRQ_50US_VAL)
  783. strng ="50us";
  784. else
  785. strng = "unknown";
  786. printf("\tDRQ response: %s\n\tPacket size: ", strng); /* Data Request (DRQ) */
  787. if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_12_VAL)
  788. strng = "12 bytes";
  789. else if ((val[GEN_CONFIG] & PKT_SIZE_SUPPORTED) == PKT_SIZE_16_VAL)
  790. strng = "16 bytes";
  791. else
  792. strng = "unknown";
  793. puts(strng);
  794. } else {
  795. /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
  796. ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
  797. mm = 0;
  798. bbbig = 0;
  799. if ((ll > 0x00FBFC10) && (!val[LCYLS]))
  800. printf("\tCHS addressing not supported\n");
  801. else {
  802. jj = val[WHATS_VALID] & OK_W54_58;
  803. printf("\tLogical\t\tmax\tcurrent\n"
  804. "\tcylinders\t%u\t%u\n"
  805. "\theads\t\t%u\t%u\n"
  806. "\tsectors/track\t%u\t%u\n"
  807. "\t--\n",
  808. val[LCYLS],
  809. jj ? val[LCYLS_CUR] : 0,
  810. val[LHEADS],
  811. jj ? val[LHEADS_CUR] : 0,
  812. val[LSECTS],
  813. jj ? val[LSECTS_CUR] : 0);
  814. if ((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES]))
  815. printf("\tbytes/track: %u\tbytes/sector: %u\n",
  816. val[TRACK_BYTES], val[SECT_BYTES]);
  817. if (jj) {
  818. mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
  819. if (like_std < 3) {
  820. /* check Endian of capacity bytes */
  821. nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
  822. oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
  823. if (abs(mm - nn) > abs(oo - nn))
  824. mm = oo;
  825. }
  826. printf("\tCHS current addressable sectors:%11u\n", mm);
  827. }
  828. }
  829. /* LBA addressing */
  830. printf("\tLBA user addressable sectors:%11u\n", ll);
  831. if (((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
  832. && (val[CMDS_SUPP_1] & SUPPORT_48_BIT)
  833. ) {
  834. bbbig = (uint64_t)val[LBA_64_MSB] << 48 |
  835. (uint64_t)val[LBA_48_MSB] << 32 |
  836. (uint64_t)val[LBA_MID] << 16 |
  837. val[LBA_LSB];
  838. printf("\tLBA48 user addressable sectors:%11"PRIu64"\n", bbbig);
  839. }
  840. if (!bbbig)
  841. bbbig = (uint64_t)(ll>mm ? ll : mm); /* # 512 byte blocks */
  842. printf("\tdevice size with M = 1024*1024: %11"PRIu64" MBytes\n", bbbig>>11);
  843. bbbig = (bbbig << 9) / 1000000;
  844. printf("\tdevice size with M = 1000*1000: %11"PRIu64" MBytes ", bbbig);
  845. if (bbbig > 1000)
  846. printf("(%"PRIu64" GB)\n", bbbig/1000);
  847. else
  848. bb_putchar('\n');
  849. }
  850. /* hw support of commands (capabilities) */
  851. printf("Capabilities:\n\t");
  852. if (dev == ATAPI_DEV) {
  853. if (eqpt != CDROM && (val[CAPAB_0] & CMD_Q_SUP))
  854. printf("Cmd queuing, ");
  855. if (val[CAPAB_0] & OVLP_SUP)
  856. printf("Cmd overlap, ");
  857. }
  858. if (val[CAPAB_0] & LBA_SUP) printf("LBA, ");
  859. if (like_std != 1) {
  860. printf("IORDY%s(can%s be disabled)\n",
  861. !(val[CAPAB_0] & IORDY_SUP) ? "(may be)" : "",
  862. (val[CAPAB_0] & IORDY_OFF) ? "" :"not");
  863. } else
  864. printf("no IORDY\n");
  865. if ((like_std == 1) && val[BUF_TYPE]) {
  866. printf("\tBuffer type: %04x: %s%s\n", val[BUF_TYPE],
  867. (val[BUF_TYPE] < 2) ? "single port, single-sector" : "dual port, multi-sector",
  868. (val[BUF_TYPE] > 2) ? " with read caching ability" : "");
  869. }
  870. if ((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1))) {
  871. printf("\tBuffer size: %.1fkB\n", (float)val[BUFFER__SIZE]/2);
  872. }
  873. if ((min_std < 4) && (val[RW_LONG])) {
  874. printf("\tbytes avail on r/w long: %u\n", val[RW_LONG]);
  875. }
  876. if ((eqpt != CDROM) && (like_std > 3)) {
  877. printf("\tQueue depth: %u\n", (val[QUEUE_DEPTH] & DEPTH_BITS) + 1);
  878. }
  879. if (dev == ATA_DEV) {
  880. if (like_std == 1)
  881. printf("\tCan%s perform double-word IO\n", (!val[DWORD_IO]) ? "not" : "");
  882. else {
  883. printf("\tStandby timer values: spec'd by %s",
  884. (val[CAPAB_0] & STD_STBY) ? "standard" : "vendor");
  885. if ((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
  886. printf(", %s device specific minimum\n",
  887. (val[CAPAB_1] & MIN_STANDBY_TIMER) ? "with" : "no");
  888. else
  889. bb_putchar('\n');
  890. }
  891. printf("\tR/W multiple sector transfer: ");
  892. if ((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
  893. printf("not supported\n");
  894. else {
  895. printf("Max = %u\tCurrent = ", val[SECTOR_XFER_MAX] & SECTOR_XFER);
  896. if (val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID)
  897. printf("%u\n", val[SECTOR_XFER_CUR] & SECTOR_XFER);
  898. else
  899. printf("?\n");
  900. }
  901. if ((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008)) {
  902. /* We print out elsewhere whether the APM feature is enabled or
  903. not. If it's not enabled, let's not repeat the info; just print
  904. nothing here. */
  905. printf("\tAdvancedPM level: ");
  906. if ((val[ADV_PWR] & 0xFF00) == 0x4000) {
  907. uint8_t apm_level = val[ADV_PWR] & 0x00FF;
  908. printf("%u (0x%x)\n", apm_level, apm_level);
  909. }
  910. else
  911. printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
  912. }
  913. if (like_std > 5 && val[ACOUSTIC]) {
  914. printf("\tRecommended acoustic management value: %u, current value: %u\n",
  915. (val[ACOUSTIC] >> 8) & 0x00ff,
  916. 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])
  925. printf(" %uus to release bus.", val[PKT_REL]);
  926. if (val[SVC_NBSY])
  927. printf(" %uus to clear BSY after SERVICE cmd.",
  928. val[SVC_NBSY]);
  929. bb_putchar('\n');
  930. }
  931. }
  932. /* DMA stuff. Check that only one DMA mode is selected. */
  933. printf("\tDMA: ");
  934. if (!(val[CAPAB_0] & DMA_SUP))
  935. printf("not supported\n");
  936. else {
  937. if (val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA])
  938. printf(" sdma%u\n", (val[DMA_MODE] & MODE) >> 8);
  939. if (val[SINGLE_DMA]) {
  940. jj = val[SINGLE_DMA];
  941. kk = val[SINGLE_DMA] >> 8;
  942. err_dma += mode_loop(jj, kk, 's', &have_mode);
  943. }
  944. if (val[MULTI_DMA]) {
  945. jj = val[MULTI_DMA];
  946. kk = val[MULTI_DMA] >> 8;
  947. err_dma += mode_loop(jj, kk, 'm', &have_mode);
  948. }
  949. if ((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) {
  950. jj = val[ULTRA_DMA];
  951. kk = val[ULTRA_DMA] >> 8;
  952. err_dma += mode_loop(jj, kk, 'u', &have_mode);
  953. }
  954. if (err_dma || !have_mode) printf("(?)");
  955. bb_putchar('\n');
  956. if ((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP))
  957. printf("\t\tInterleaved DMA support\n");
  958. if ((val[WHATS_VALID] & OK_W64_70)
  959. && (val[DMA_TIME_MIN] || val[DMA_TIME_NORM])
  960. ) {
  961. printf("\t\tCycle time:");
  962. if (val[DMA_TIME_MIN]) printf(" min=%uns", val[DMA_TIME_MIN]);
  963. if (val[DMA_TIME_NORM]) printf(" recommended=%uns", val[DMA_TIME_NORM]);
  964. bb_putchar('\n');
  965. }
  966. }
  967. /* Programmed IO stuff */
  968. printf("\tPIO: ");
  969. /* If a drive supports mode n (e.g. 3), it also supports all modes less
  970. * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
  971. if ((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP)) {
  972. jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
  973. for (ii = 0; ii <= PIO_MODE_MAX; ii++) {
  974. if (jj & 0x0001) printf("pio%d ", ii);
  975. jj >>=1;
  976. }
  977. bb_putchar('\n');
  978. } else if (((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE)) {
  979. for (ii = 0; ii <= val[PIO_MODE]>>8; ii++)
  980. printf("pio%d ", ii);
  981. bb_putchar('\n');
  982. } else
  983. puts("unknown");
  984. if (val[WHATS_VALID] & OK_W64_70) {
  985. if (val[PIO_NO_FLOW] || val[PIO_FLOW]) {
  986. printf("\t\tCycle time:");
  987. if (val[PIO_NO_FLOW])
  988. printf(" no flow control=%uns", val[PIO_NO_FLOW]);
  989. if (val[PIO_FLOW])
  990. printf(" IORDY flow control=%uns", val[PIO_FLOW]);
  991. bb_putchar('\n');
  992. }
  993. }
  994. if ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) {
  995. printf("Commands/features:\n"
  996. "\tEnabled\tSupported:\n");
  997. jj = val[CMDS_SUPP_0];
  998. kk = val[CMDS_EN_0];
  999. for (ii = 0; ii < NUM_CMD_FEAT_STR; ii++) {
  1000. const char *feat_str = nth_string(cmd_feat_str, ii);
  1001. if ((jj & 0x8000) && (*feat_str != '\0')) {
  1002. printf("\t%s\t%s\n", (kk & 0x8000) ? " *" : "", feat_str);
  1003. }
  1004. jj <<= 1;
  1005. kk <<= 1;
  1006. if (ii % 16 == 15) {
  1007. jj = val[CMDS_SUPP_0+1+(ii/16)];
  1008. kk = val[CMDS_EN_0+1+(ii/16)];
  1009. }
  1010. if (ii == 31) {
  1011. if ((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
  1012. ii +=16;
  1013. }
  1014. }
  1015. }
  1016. /* Removable Media Status Notification feature set */
  1017. if ((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP)
  1018. printf("\t%s supported\n", nth_string(cmd_feat_str, 27));
  1019. /* security */
  1020. if ((eqpt != CDROM) && (like_std > 3)
  1021. && (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME])
  1022. ) {
  1023. printf("Security:\n");
  1024. if (val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1))
  1025. printf("\tMaster password revision code = %u\n", val[PSWD_CODE]);
  1026. jj = val[SECU_STATUS];
  1027. if (jj) {
  1028. for (ii = 0; ii < NUM_SECU_STR; ii++) {
  1029. printf("\t%s\t%s\n",
  1030. (!(jj & 0x0001)) ? "not" : "",
  1031. nth_string(secu_str, ii));
  1032. jj >>=1;
  1033. }
  1034. if (val[SECU_STATUS] & SECU_ENABLED) {
  1035. printf("\tSecurity level %s\n",
  1036. (val[SECU_STATUS] & SECU_LEVEL) ? "maximum" : "high");
  1037. }
  1038. }
  1039. jj = val[ERASE_TIME] & ERASE_BITS;
  1040. kk = val[ENH_ERASE_TIME] & ERASE_BITS;
  1041. if (jj || kk) {
  1042. bb_putchar('\t');
  1043. if (jj) printf("%umin for %sSECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1, "");
  1044. if (kk) printf("%umin for %sSECURITY ERASE UNIT. ", kk==ERASE_BITS ? 508 : kk<<1, "ENHANCED ");
  1045. bb_putchar('\n');
  1046. }
  1047. }
  1048. /* reset result */
  1049. jj = val[HWRST_RSLT];
  1050. if ((jj & VALID) == VALID_VAL) {
  1051. oo = (jj & RST0);
  1052. if (!oo)
  1053. jj >>= 8;
  1054. if ((jj & DEV_DET) == JUMPER_VAL)
  1055. strng = " determined by the jumper";
  1056. else if ((jj & DEV_DET) == CSEL_VAL)
  1057. strng = " determined by CSEL";
  1058. else
  1059. strng = "";
  1060. printf("HW reset results:\n"
  1061. "\tCBLID- %s Vih\n"
  1062. "\tDevice num = %i%s\n",
  1063. (val[HWRST_RSLT] & CBLID) ? "above" : "below",
  1064. !(oo), strng);
  1065. }
  1066. /* more stuff from std 5 */
  1067. if ((like_std > 4) && (eqpt != CDROM)) {
  1068. if (val[CFA_PWR_MODE] & VALID_W160) {
  1069. printf("CFA power mode 1:\n"
  1070. "\t%s%s\n",
  1071. (val[CFA_PWR_MODE] & PWR_MODE_OFF) ? "disabled" : "enabled",
  1072. (val[CFA_PWR_MODE] & PWR_MODE_REQ) ? " and required by some commands" : "");
  1073. if (val[CFA_PWR_MODE] & MAX_AMPS)
  1074. printf("\tMaximum current = %uma\n", val[CFA_PWR_MODE] & MAX_AMPS);
  1075. }
  1076. if ((val[INTEGRITY] & SIG) == SIG_VAL) {
  1077. printf("Checksum: %scorrect\n", chksum ? "in" : "");
  1078. }
  1079. }
  1080. exit(EXIT_SUCCESS);
  1081. }
  1082. #endif
  1083. // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
  1084. // then the HDIO_GET_IDENTITY only returned 142 bytes.
  1085. // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
  1086. // and HDIO_GET_IDENTITY returns 512 bytes. But the latest
  1087. // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
  1088. // (which they should, but they should just return -EINVAL).
  1089. //
  1090. // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
  1091. // On a really old system, it will not, and we will be confused.
  1092. // Too bad, really.
  1093. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  1094. static const char cfg_str[] ALIGN1 =
  1095. """\0" "HardSect""\0" "SoftSect""\0" "NotMFM""\0"
  1096. "HdSw>15uSec""\0" "SpinMotCtl""\0" "Fixed""\0" "Removeable""\0"
  1097. "DTR<=5Mbs""\0" "DTR>5Mbs""\0" "DTR>10Mbs""\0" "RotSpdTol>.5%""\0"
  1098. "dStbOff""\0" "TrkOff""\0" "FmtGapReq""\0" "nonMagnetic"
  1099. ;
  1100. static const char BuffType[] ALIGN1 =
  1101. "unknown""\0" "1Sect""\0" "DualPort""\0" "DualPortCache"
  1102. ;
  1103. static NOINLINE void dump_identity(const struct hd_driveid *id)
  1104. {
  1105. int i;
  1106. const unsigned short *id_regs = (const void*) id;
  1107. printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
  1108. id->model, id->fw_rev, id->serial_no);
  1109. for (i = 0; i <= 15; i++) {
  1110. if (id->config & (1<<i))
  1111. printf(" %s", nth_string(cfg_str, i));
  1112. }
  1113. printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n"
  1114. " BuffType=(%u) %s, BuffSize=%ukB, MaxMultSect=%u",
  1115. id->cyls, id->heads, id->sectors, id->track_bytes,
  1116. id->sector_bytes, id->ecc_bytes,
  1117. id->buf_type,
  1118. nth_string(BuffType, (id->buf_type > 3) ? 0 : id->buf_type),
  1119. id->buf_size/2, id->max_multsect);
  1120. if (id->max_multsect) {
  1121. printf(", MultSect=");
  1122. if (!(id->multsect_valid & 1))
  1123. printf("?%u?", id->multsect);
  1124. else if (id->multsect)
  1125. printf("%u", id->multsect);
  1126. else
  1127. printf("off");
  1128. }
  1129. bb_putchar('\n');
  1130. if (!(id->field_valid & 1))
  1131. printf(" (maybe):");
  1132. printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s", id->cur_cyls, id->cur_heads,
  1133. id->cur_sectors,
  1134. (BB_BIG_ENDIAN) ?
  1135. (unsigned long)(id->cur_capacity0 << 16) | id->cur_capacity1 :
  1136. (unsigned long)(id->cur_capacity1 << 16) | id->cur_capacity0,
  1137. ((id->capability&2) == 0) ? "no" : "yes");
  1138. if (id->capability & 2)
  1139. printf(", LBAsects=%u", id->lba_capacity);
  1140. printf("\n IORDY=%s",
  1141. (id->capability & 8)
  1142. ? ((id->capability & 4) ? "on/off" : "yes")
  1143. : "no");
  1144. if (((id->capability & 8) || (id->field_valid & 2)) && (id->field_valid & 2))
  1145. printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
  1146. if ((id->capability & 1) && (id->field_valid & 2))
  1147. printf(", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
  1148. printf("\n PIO modes: ");
  1149. if (id->tPIO <= 5) {
  1150. printf("pio0 ");
  1151. if (id->tPIO >= 1) printf("pio1 ");
  1152. if (id->tPIO >= 2) printf("pio2 ");
  1153. }
  1154. if (id->field_valid & 2) {
  1155. static const masks_labels_t pio_modes = {
  1156. .masks = { 1, 2, ~3 },
  1157. .labels = "pio3 \0""pio4 \0""pio? \0",
  1158. };
  1159. print_flags(&pio_modes, id->eide_pio_modes);
  1160. }
  1161. if (id->capability & 1) {
  1162. if (id->dma_1word | id->dma_mword) {
  1163. static const int dma_wmode_masks[] = { 0x100, 1, 0x200, 2, 0x400, 4, 0xf800, 0xf8 };
  1164. printf("\n DMA modes: ");
  1165. print_flags_separated(dma_wmode_masks,
  1166. "*\0""sdma0 \0""*\0""sdma1 \0""*\0""sdma2 \0""*\0""sdma? \0",
  1167. id->dma_1word, NULL);
  1168. print_flags_separated(dma_wmode_masks,
  1169. "*\0""mdma0 \0""*\0""mdma1 \0""*\0""mdma2 \0""*\0""mdma? \0",
  1170. id->dma_mword, NULL);
  1171. }
  1172. }
  1173. if (((id->capability & 8) || (id->field_valid & 2)) && id->field_valid & 4) {
  1174. static const masks_labels_t ultra_modes1 = {
  1175. .masks = { 0x100, 0x001, 0x200, 0x002, 0x400, 0x004 },
  1176. .labels = "*\0""udma0 \0""*\0""udma1 \0""*\0""udma2 \0",
  1177. };
  1178. printf("\n UDMA modes: ");
  1179. print_flags(&ultra_modes1, id->dma_ultra);
  1180. #ifdef __NEW_HD_DRIVE_ID
  1181. if (id->hw_config & 0x2000) {
  1182. #else /* !__NEW_HD_DRIVE_ID */
  1183. if (id->word93 & 0x2000) {
  1184. #endif /* __NEW_HD_DRIVE_ID */
  1185. static const masks_labels_t ultra_modes2 = {
  1186. .masks = { 0x0800, 0x0008, 0x1000, 0x0010,
  1187. 0x2000, 0x0020, 0x4000, 0x0040,
  1188. 0x8000, 0x0080 },
  1189. .labels = "*\0""udma3 \0""*\0""udma4 \0"
  1190. "*\0""udma5 \0""*\0""udma6 \0"
  1191. "*\0""udma7 \0"
  1192. };
  1193. print_flags(&ultra_modes2, id->dma_ultra);
  1194. }
  1195. }
  1196. printf("\n AdvancedPM=%s", (!(id_regs[83] & 8)) ? "no" : "yes");
  1197. if (id_regs[83] & 8) {
  1198. if (!(id_regs[86] & 8))
  1199. printf(": disabled (255)");
  1200. else if ((id_regs[91] & 0xFF00) != 0x4000)
  1201. printf(": unknown setting");
  1202. else
  1203. printf(": mode=0x%02X (%u)", id_regs[91] & 0xFF, id_regs[91] & 0xFF);
  1204. }
  1205. if (id_regs[82] & 0x20)
  1206. printf(" WriteCache=%s", (id_regs[85] & 0x20) ? "enabled" : "disabled");
  1207. #ifdef __NEW_HD_DRIVE_ID
  1208. if ((id->minor_rev_num && id->minor_rev_num <= 31)
  1209. || (id->major_rev_num && id->minor_rev_num <= 31)
  1210. ) {
  1211. printf("\n Drive conforms to: %s: ",
  1212. (id->minor_rev_num <= 31) ? nth_string(minor_str, id->minor_rev_num) : "unknown");
  1213. if (id->major_rev_num != 0x0000 /* NOVAL_0 */
  1214. && id->major_rev_num != 0xFFFF /* NOVAL_1 */
  1215. ) {
  1216. for (i = 0; i <= 15; i++) {
  1217. if (id->major_rev_num & (1<<i))
  1218. printf(" ATA/ATAPI-%u", i);
  1219. }
  1220. }
  1221. }
  1222. #endif /* __NEW_HD_DRIVE_ID */
  1223. printf("\n\n * current active mode\n\n");
  1224. }
  1225. #endif
  1226. static void flush_buffer_cache(/*int fd*/ void)
  1227. {
  1228. fsync(fd); /* flush buffers */
  1229. ioctl_or_warn(fd, BLKFLSBUF, NULL); /* do it again, big time */
  1230. #ifdef HDIO_DRIVE_CMD
  1231. sleep(1);
  1232. if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) { /* await completion */
  1233. if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
  1234. bb_perror_msg("HDIO_DRIVE_CMD");
  1235. else
  1236. bb_perror_msg("ioctl %#x failed", HDIO_DRIVE_CMD);
  1237. }
  1238. #endif
  1239. }
  1240. static void seek_to_zero(/*int fd*/ void)
  1241. {
  1242. xlseek(fd, (off_t) 0, SEEK_SET);
  1243. }
  1244. static void read_big_block(/*int fd,*/ char *buf)
  1245. {
  1246. int i;
  1247. xread(fd, buf, TIMING_BUF_BYTES);
  1248. /* access all sectors of buf to ensure the read fully completed */
  1249. for (i = 0; i < TIMING_BUF_BYTES; i += 512)
  1250. buf[i] &= 1;
  1251. }
  1252. static unsigned dev_size_mb(/*int fd*/ void)
  1253. {
  1254. union {
  1255. unsigned long long blksize64;
  1256. unsigned blksize32;
  1257. } u;
  1258. if (0 == ioctl(fd, BLKGETSIZE64, &u.blksize64)) { // bytes
  1259. u.blksize64 /= (1024 * 1024);
  1260. } else {
  1261. xioctl(fd, BLKGETSIZE, &u.blksize32); // sectors
  1262. u.blksize64 = u.blksize32 / (2 * 1024);
  1263. }
  1264. if (u.blksize64 > UINT_MAX)
  1265. return UINT_MAX;
  1266. return u.blksize64;
  1267. }
  1268. static void print_timing(unsigned m, unsigned elapsed_us)
  1269. {
  1270. unsigned sec = elapsed_us / 1000000;
  1271. unsigned hs = (elapsed_us % 1000000) / 10000;
  1272. printf("%5u MB in %u.%02u seconds = %u kB/s\n",
  1273. m, sec, hs,
  1274. /* "| 1" prevents div-by-0 */
  1275. (unsigned) ((unsigned long long)m * (1024 * 1000000) / (elapsed_us | 1))
  1276. // ~= (m * 1024) / (elapsed_us / 1000000)
  1277. // = kb / elapsed_sec
  1278. );
  1279. }
  1280. static void do_time(int cache /*,int fd*/)
  1281. /* cache=1: time cache: repeatedly read N MB at offset 0
  1282. * cache=0: time device: linear read, starting at offset 0
  1283. */
  1284. {
  1285. unsigned max_iterations, iterations;
  1286. unsigned start; /* doesn't need to be long long */
  1287. unsigned elapsed, elapsed2;
  1288. unsigned total_MB;
  1289. char *buf = xmalloc(TIMING_BUF_BYTES);
  1290. if (mlock(buf, TIMING_BUF_BYTES))
  1291. bb_perror_msg_and_die("mlock");
  1292. /* Clear out the device request queues & give them time to complete.
  1293. * NB: *small* delay. User is expected to have a clue and to not run
  1294. * heavy io in parallel with measurements. */
  1295. sync();
  1296. sleep(1);
  1297. if (cache) { /* Time cache */
  1298. seek_to_zero();
  1299. read_big_block(buf);
  1300. printf("Timing buffer-cache reads: ");
  1301. } else { /* Time device */
  1302. printf("Timing buffered disk reads:");
  1303. }
  1304. fflush_all();
  1305. /* Now do the timing */
  1306. iterations = 0;
  1307. /* Max time to run (small for cache, avoids getting
  1308. * huge total_MB which can overlow unsigned type) */
  1309. elapsed2 = 510000; /* cache */
  1310. max_iterations = UINT_MAX;
  1311. if (!cache) {
  1312. elapsed2 = 3000000; /* not cache */
  1313. /* Don't want to read past the end! */
  1314. max_iterations = dev_size_mb() / TIMING_BUF_MB;
  1315. }
  1316. start = monotonic_us();
  1317. do {
  1318. if (cache)
  1319. seek_to_zero();
  1320. read_big_block(buf);
  1321. elapsed = (unsigned)monotonic_us() - start;
  1322. ++iterations;
  1323. } while (elapsed < elapsed2 && iterations < max_iterations);
  1324. total_MB = iterations * TIMING_BUF_MB;
  1325. //printf(" elapsed:%u iterations:%u ", elapsed, iterations);
  1326. if (cache) {
  1327. /* Cache: remove lseek() and monotonic_us() overheads
  1328. * from elapsed */
  1329. start = monotonic_us();
  1330. do {
  1331. seek_to_zero();
  1332. elapsed2 = (unsigned)monotonic_us() - start;
  1333. } while (--iterations);
  1334. //printf(" elapsed2:%u ", elapsed2);
  1335. elapsed -= elapsed2;
  1336. total_MB *= 2; // BUFCACHE_FACTOR (why?)
  1337. flush_buffer_cache();
  1338. }
  1339. print_timing(total_MB, elapsed);
  1340. munlock(buf, TIMING_BUF_BYTES);
  1341. free(buf);
  1342. }
  1343. #if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1344. static void bus_state_value(unsigned value)
  1345. {
  1346. if (value == BUSSTATE_ON)
  1347. on_off(1);
  1348. else if (value == BUSSTATE_OFF)
  1349. on_off(0);
  1350. else if (value == BUSSTATE_TRISTATE)
  1351. printf(" (tristate)\n");
  1352. else
  1353. printf(" (unknown: %d)\n", value);
  1354. }
  1355. #endif
  1356. #ifdef HDIO_DRIVE_CMD
  1357. static void interpret_standby(uint8_t standby)
  1358. {
  1359. printf(" (");
  1360. if (standby == 0) {
  1361. printf("off");
  1362. } else if (standby <= 240 || standby == 252 || standby == 255) {
  1363. /* standby is in 5 sec units */
  1364. unsigned t = standby * 5;
  1365. printf("%u minutes %u seconds", t / 60, t % 60);
  1366. } else if (standby <= 251) {
  1367. unsigned t = (standby - 240); /* t is in 30 min units */;
  1368. printf("%u.%c hours", t / 2, (t & 1) ? '5' : '0');
  1369. }
  1370. if (standby == 253)
  1371. printf("vendor-specific");
  1372. if (standby == 254)
  1373. printf("reserved");
  1374. printf(")\n");
  1375. }
  1376. static const uint8_t xfermode_val[] ALIGN1 = {
  1377. 8, 9, 10, 11, 12, 13, 14, 15,
  1378. 16, 17, 18, 19, 20, 21, 22, 23,
  1379. 32, 33, 34, 35, 36, 37, 38, 39,
  1380. 64, 65, 66, 67, 68, 69, 70, 71
  1381. };
  1382. /* NB: we save size by _not_ storing terninating NUL! */
  1383. static const char xfermode_name[][5] ALIGN1 = {
  1384. "pio0", "pio1", "pio2", "pio3", "pio4", "pio5", "pio6", "pio7",
  1385. "sdma0","sdma1","sdma2","sdma3","sdma4","sdma5","sdma6","sdma7",
  1386. "mdma0","mdma1","mdma2","mdma3","mdma4","mdma5","mdma6","mdma7",
  1387. "udma0","udma1","udma2","udma3","udma4","udma5","udma6","udma7"
  1388. };
  1389. static int translate_xfermode(const char *name)
  1390. {
  1391. int val;
  1392. unsigned i;
  1393. for (i = 0; i < ARRAY_SIZE(xfermode_val); i++) {
  1394. if (!strncmp(name, xfermode_name[i], 5))
  1395. if (strlen(name) <= 5)
  1396. return xfermode_val[i];
  1397. }
  1398. /* Negative numbers are invalid and are caught later */
  1399. val = bb_strtoi(name, NULL, 10);
  1400. if (!errno)
  1401. return val;
  1402. return -1;
  1403. }
  1404. static void interpret_xfermode(unsigned xfermode)
  1405. {
  1406. printf(" (");
  1407. if (xfermode == 0)
  1408. printf("default PIO mode");
  1409. else if (xfermode == 1)
  1410. printf("default PIO mode, disable IORDY");
  1411. else if (xfermode >= 8 && xfermode <= 15)
  1412. printf("PIO flow control mode%u", xfermode - 8);
  1413. else if (xfermode >= 16 && xfermode <= 23)
  1414. printf("singleword DMA mode%u", xfermode - 16);
  1415. else if (xfermode >= 32 && xfermode <= 39)
  1416. printf("multiword DMA mode%u", xfermode - 32);
  1417. else if (xfermode >= 64 && xfermode <= 71)
  1418. printf("UltraDMA mode%u", xfermode - 64);
  1419. else
  1420. printf("unknown");
  1421. printf(")\n");
  1422. }
  1423. #endif /* HDIO_DRIVE_CMD */
  1424. static void print_flag(int flag, const char *s, unsigned long value)
  1425. {
  1426. if (flag)
  1427. printf(" setting %s to %ld\n", s, value);
  1428. }
  1429. static void process_dev(char *devname)
  1430. {
  1431. /*int fd;*/
  1432. long parm, multcount;
  1433. #ifndef HDIO_DRIVE_CMD
  1434. int force_operation = 0;
  1435. #endif
  1436. /* Please restore args[n] to these values after each ioctl
  1437. except for args[2] */
  1438. unsigned char args[4] = { WIN_SETFEATURES, 0, 0, 0 };
  1439. const char *fmt = " %s\t= %2ld";
  1440. /*fd = xopen_nonblocking(devname);*/
  1441. xmove_fd(xopen_nonblocking(devname), fd);
  1442. printf("\n%s:\n", devname);
  1443. if (getset_readahead == IS_SET) {
  1444. print_flag(getset_readahead, "fs readahead", Xreadahead);
  1445. ioctl_or_warn(fd, BLKRASET, (int *)Xreadahead);
  1446. }
  1447. #if ENABLE_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
  1448. if (unregister_hwif) {
  1449. printf(" attempting to unregister hwif#%lu\n", hwif);
  1450. ioctl_or_warn(fd, HDIO_UNREGISTER_HWIF, (int *)(unsigned long)hwif);
  1451. }
  1452. #endif
  1453. #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
  1454. if (scan_hwif == IS_SET) {
  1455. printf(" attempting to scan hwif (0x%lx, 0x%lx, %lu)\n", hwif_data, hwif_ctrl, hwif_irq);
  1456. args[0] = hwif_data;
  1457. args[1] = hwif_ctrl;
  1458. args[2] = hwif_irq;
  1459. ioctl_or_warn(fd, HDIO_SCAN_HWIF, args);
  1460. args[0] = WIN_SETFEATURES;
  1461. args[1] = 0;
  1462. }
  1463. #endif
  1464. if (set_piomode) {
  1465. if (noisy_piomode) {
  1466. printf(" attempting to ");
  1467. if (piomode == 255)
  1468. printf("auto-tune PIO mode\n");
  1469. else if (piomode < 100)
  1470. printf("set PIO mode to %d\n", piomode);
  1471. else if (piomode < 200)
  1472. printf("set MDMA mode to %d\n", (piomode-100));
  1473. else
  1474. printf("set UDMA mode to %d\n", (piomode-200));
  1475. }
  1476. ioctl_or_warn(fd, HDIO_SET_PIO_MODE, (int *)(unsigned long)piomode);
  1477. }
  1478. if (getset_io32bit == IS_SET) {
  1479. print_flag(getset_io32bit, "32-bit IO_support flag", io32bit);
  1480. ioctl_or_warn(fd, HDIO_SET_32BIT, (int *)io32bit);
  1481. }
  1482. if (getset_mult == IS_SET) {
  1483. print_flag(getset_mult, "multcount", mult);
  1484. #ifdef HDIO_DRIVE_CMD
  1485. ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult);
  1486. #else
  1487. force_operation |= (!ioctl_or_warn(fd, HDIO_SET_MULTCOUNT, (void *)mult));
  1488. #endif
  1489. }
  1490. if (getset_readonly == IS_SET) {
  1491. print_flag_on_off(getset_readonly, "readonly", readonly);
  1492. ioctl_or_warn(fd, BLKROSET, &readonly);
  1493. }
  1494. if (getset_unmask == IS_SET) {
  1495. print_flag_on_off(getset_unmask, "unmaskirq", unmask);
  1496. ioctl_or_warn(fd, HDIO_SET_UNMASKINTR, (int *)unmask);
  1497. }
  1498. #if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
  1499. if (getset_dma == IS_SET) {
  1500. print_flag_on_off(getset_dma, "using_dma", dma);
  1501. ioctl_or_warn(fd, HDIO_SET_DMA, (int *)dma);
  1502. }
  1503. #endif /* FEATURE_HDPARM_HDIO_GETSET_DMA */
  1504. #ifdef HDIO_SET_QDMA
  1505. if (getset_dma_q == IS_SET) {
  1506. print_flag_on_off(getset_dma_q, "DMA queue_depth", dma_q);
  1507. ioctl_or_warn(fd, HDIO_SET_QDMA, (int *)dma_q);
  1508. }
  1509. #endif
  1510. if (getset_nowerr == IS_SET) {
  1511. print_flag_on_off(getset_nowerr, "nowerr", nowerr);
  1512. ioctl_or_warn(fd, HDIO_SET_NOWERR, (int *)nowerr);
  1513. }
  1514. if (getset_keep == IS_SET) {
  1515. print_flag_on_off(getset_keep, "keep_settings", keep);
  1516. ioctl_or_warn(fd, HDIO_SET_KEEPSETTINGS, (int *)keep);
  1517. }
  1518. #ifdef HDIO_DRIVE_CMD
  1519. if (getset_doorlock == IS_SET) {
  1520. args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
  1521. args[2] = 0;
  1522. print_flag_on_off(getset_doorlock, "drive doorlock", doorlock);
  1523. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1524. args[0] = WIN_SETFEATURES;
  1525. }
  1526. if (getset_dkeep == IS_SET) {
  1527. /* lock/unlock the drive's "feature" settings */
  1528. print_flag_on_off(getset_dkeep, "drive keep features", dkeep);
  1529. args[2] = dkeep ? 0x66 : 0xcc;
  1530. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1531. }
  1532. if (getset_defects == IS_SET) {
  1533. args[2] = defects ? 0x04 : 0x84;
  1534. print_flag(getset_defects, "drive defect-mgmt", defects);
  1535. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1536. }
  1537. if (getset_prefetch == IS_SET) {
  1538. args[1] = prefetch;
  1539. args[2] = 0xab;
  1540. print_flag(getset_prefetch, "drive prefetch", prefetch);
  1541. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1542. args[1] = 0;
  1543. }
  1544. if (set_xfermode) {
  1545. args[1] = xfermode_requested;
  1546. args[2] = 3;
  1547. print_flag(1, "xfermode", xfermode_requested);
  1548. interpret_xfermode(xfermode_requested);
  1549. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1550. args[1] = 0;
  1551. }
  1552. if (getset_lookahead == IS_SET) {
  1553. args[2] = lookahead ? 0xaa : 0x55;
  1554. print_flag_on_off(getset_lookahead, "drive read-lookahead", lookahead);
  1555. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1556. }
  1557. if (getset_apmmode == IS_SET) {
  1558. /* feature register */
  1559. args[2] = (apmmode == 255) ? 0x85 /* disable */ : 0x05 /* set */;
  1560. args[1] = apmmode; /* sector count register 1-255 */
  1561. printf(" setting APM level to %s 0x%02lX (%ld)\n",
  1562. (apmmode == 255) ? "disabled" : "",
  1563. apmmode, apmmode);
  1564. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1565. args[1] = 0;
  1566. }
  1567. if (getset_wcache == IS_SET) {
  1568. #ifdef DO_FLUSHCACHE
  1569. #ifndef WIN_FLUSHCACHE
  1570. #define WIN_FLUSHCACHE 0xe7
  1571. #endif
  1572. #endif /* DO_FLUSHCACHE */
  1573. args[2] = wcache ? 0x02 : 0x82;
  1574. print_flag_on_off(getset_wcache, "drive write-caching", wcache);
  1575. #ifdef DO_FLUSHCACHE
  1576. if (!wcache)
  1577. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
  1578. #endif /* DO_FLUSHCACHE */
  1579. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1580. #ifdef DO_FLUSHCACHE
  1581. if (!wcache)
  1582. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &flushcache);
  1583. #endif /* DO_FLUSHCACHE */
  1584. }
  1585. /* In code below, we do not preserve args[0], but the rest
  1586. is preserved, including args[2] */
  1587. args[2] = 0;
  1588. if (set_standbynow) {
  1589. #ifndef WIN_STANDBYNOW1
  1590. #define WIN_STANDBYNOW1 0xE0
  1591. #endif
  1592. #ifndef WIN_STANDBYNOW2
  1593. #define WIN_STANDBYNOW2 0x94
  1594. #endif
  1595. printf(" issuing standby command\n");
  1596. args[0] = WIN_STANDBYNOW1;
  1597. ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_STANDBYNOW2);
  1598. }
  1599. if (set_sleepnow) {
  1600. #ifndef WIN_SLEEPNOW1
  1601. #define WIN_SLEEPNOW1 0xE6
  1602. #endif
  1603. #ifndef WIN_SLEEPNOW2
  1604. #define WIN_SLEEPNOW2 0x99
  1605. #endif
  1606. printf(" issuing sleep command\n");
  1607. args[0] = WIN_SLEEPNOW1;
  1608. ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_SLEEPNOW2);
  1609. }
  1610. if (set_seagate) {
  1611. args[0] = 0xfb;
  1612. printf(" disabling Seagate auto powersaving mode\n");
  1613. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1614. }
  1615. if (getset_standby == IS_SET) {
  1616. args[0] = WIN_SETIDLE1;
  1617. args[1] = standby_requested;
  1618. print_flag(1, "standby", standby_requested);
  1619. interpret_standby(standby_requested);
  1620. ioctl_or_warn(fd, HDIO_DRIVE_CMD, &args);
  1621. args[1] = 0;
  1622. }
  1623. #else /* HDIO_DRIVE_CMD */
  1624. if (force_operation) {
  1625. char buf[512];
  1626. flush_buffer_cache();
  1627. if (-1 == read(fd, buf, sizeof(buf)))
  1628. bb_perror_msg("read of 512 bytes failed");
  1629. }
  1630. #endif /* HDIO_DRIVE_CMD */
  1631. if (getset_mult || get_identity) {
  1632. multcount = -1;
  1633. if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount)) {
  1634. /* To be coherent with ioctl_or_warn. */
  1635. if (getset_mult && ENABLE_IOCTL_HEX2STR_ERROR)
  1636. bb_perror_msg("HDIO_GET_MULTCOUNT");
  1637. else
  1638. bb_perror_msg("ioctl %#x failed", HDIO_GET_MULTCOUNT);
  1639. } else if (getset_mult) {
  1640. printf(fmt, "multcount", multcount);
  1641. on_off(multcount != 0);
  1642. }
  1643. }
  1644. if (getset_io32bit) {
  1645. if (!ioctl_or_warn(fd, HDIO_GET_32BIT, &parm)) {
  1646. printf(" IO_support\t=%3ld (", parm);
  1647. if (parm == 0)
  1648. printf("default 16-bit)\n");
  1649. else if (parm == 2)
  1650. printf("16-bit)\n");
  1651. else if (parm == 1)
  1652. printf("32-bit)\n");
  1653. else if (parm == 3)
  1654. printf("32-bit w/sync)\n");
  1655. else if (parm == 8)
  1656. printf("Request-Queue-Bypass)\n");
  1657. else
  1658. printf("\?\?\?)\n");
  1659. }
  1660. }
  1661. if (getset_unmask) {
  1662. if (!ioctl_or_warn(fd, HDIO_GET_UNMASKINTR, &parm))
  1663. print_value_on_off("unmaskirq", parm);
  1664. }
  1665. #if ENABLE_FEATURE_HDPARM_HDIO_GETSET_DMA
  1666. if (getset_dma) {
  1667. if (!ioctl_or_warn(fd, HDIO_GET_DMA, &parm)) {
  1668. printf(fmt, "using_dma", parm);
  1669. if (parm == 8)
  1670. printf(" (DMA-Assisted-PIO)\n");
  1671. else
  1672. on_off(parm != 0);
  1673. }
  1674. }
  1675. #endif
  1676. #ifdef HDIO_GET_QDMA
  1677. if (getset_dma_q) {
  1678. if (!ioctl_or_warn(fd, HDIO_GET_QDMA, &parm))
  1679. print_value_on_off("queue_depth", parm);
  1680. }
  1681. #endif
  1682. if (getset_keep) {
  1683. if (!ioctl_or_warn(fd, HDIO_GET_KEEPSETTINGS, &parm))
  1684. print_value_on_off("keepsettings", parm);
  1685. }
  1686. if (getset_nowerr) {
  1687. if (!ioctl_or_warn(fd, HDIO_GET_NOWERR, &parm))
  1688. print_value_on_off("nowerr", parm);
  1689. }
  1690. if (getset_readonly) {
  1691. if (!ioctl_or_warn(fd, BLKROGET, &parm))
  1692. print_value_on_off("readonly", parm);
  1693. }
  1694. if (getset_readahead) {
  1695. if (!ioctl_or_warn(fd, BLKRAGET, &parm))
  1696. print_value_on_off("readahead", parm);
  1697. }
  1698. if (get_geom) {
  1699. if (!ioctl_or_warn(fd, BLKGETSIZE, &parm)) {
  1700. struct hd_geometry g;
  1701. if (!ioctl_or_warn(fd, HDIO_GETGEO, &g))
  1702. printf(" geometry\t= %u/%u/%u, sectors = %ld, start = %ld\n",
  1703. g.cylinders, g.heads, g.sectors, parm, g.start);
  1704. }
  1705. }
  1706. #ifdef HDIO_DRIVE_CMD
  1707. if (get_powermode) {
  1708. #ifndef WIN_CHECKPOWERMODE1
  1709. #define WIN_CHECKPOWERMODE1 0xE5
  1710. #endif
  1711. #ifndef WIN_CHECKPOWERMODE2
  1712. #define WIN_CHECKPOWERMODE2 0x98
  1713. #endif
  1714. const char *state;
  1715. args[0] = WIN_CHECKPOWERMODE1;
  1716. if (ioctl_alt_or_warn(HDIO_DRIVE_CMD, args, WIN_CHECKPOWERMODE2)) {
  1717. if (errno != EIO || args[0] != 0 || args[1] != 0)
  1718. state = "unknown";
  1719. else
  1720. state = "sleeping";
  1721. } else
  1722. state = (args[2] == 255) ? "active/idle" : "standby";
  1723. args[1] = args[2] = 0;
  1724. printf(" drive state is: %s\n", state);
  1725. }
  1726. #endif
  1727. #if ENABLE_FEATURE_HDPARM_HDIO_DRIVE_RESET
  1728. if (perform_reset) {
  1729. ioctl_or_warn(fd, HDIO_DRIVE_RESET, NULL);
  1730. }
  1731. #endif /* FEATURE_HDPARM_HDIO_DRIVE_RESET */
  1732. #if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1733. if (perform_tristate) {
  1734. args[0] = 0;
  1735. args[1] = tristate;
  1736. ioctl_or_warn(fd, HDIO_TRISTATE_HWIF, &args);
  1737. }
  1738. #endif /* FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
  1739. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  1740. if (get_identity) {
  1741. struct hd_driveid id;
  1742. if (!ioctl(fd, HDIO_GET_IDENTITY, &id)) {
  1743. if (multcount != -1) {
  1744. id.multsect = multcount;
  1745. id.multsect_valid |= 1;
  1746. } else
  1747. id.multsect_valid &= ~1;
  1748. dump_identity(&id);
  1749. } else if (errno == -ENOMSG)
  1750. printf(" no identification info available\n");
  1751. else if (ENABLE_IOCTL_HEX2STR_ERROR) /* To be coherent with ioctl_or_warn */
  1752. bb_perror_msg("HDIO_GET_IDENTITY");
  1753. else
  1754. bb_perror_msg("ioctl %#x failed", HDIO_GET_IDENTITY);
  1755. }
  1756. if (get_IDentity) {
  1757. unsigned char args1[4+512]; /* = { ... } will eat 0.5k of rodata! */
  1758. memset(args1, 0, sizeof(args1));
  1759. args1[0] = WIN_IDENTIFY;
  1760. args1[3] = 1;
  1761. if (!ioctl_alt_or_warn(HDIO_DRIVE_CMD, args1, WIN_PIDENTIFY))
  1762. identify((void *)(args1 + 4));
  1763. }
  1764. #endif
  1765. #if ENABLE_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1766. if (getset_busstate == IS_SET) {
  1767. print_flag(1, "bus state", busstate);
  1768. bus_state_value(busstate);
  1769. ioctl_or_warn(fd, HDIO_SET_BUSSTATE, (int *)(unsigned long)busstate);
  1770. }
  1771. if (getset_busstate) {
  1772. if (!ioctl_or_warn(fd, HDIO_GET_BUSSTATE, &parm)) {
  1773. printf(fmt, "bus state", parm);
  1774. bus_state_value(parm);
  1775. }
  1776. }
  1777. #endif
  1778. if (reread_partn)
  1779. ioctl_or_warn(fd, BLKRRPART, NULL);
  1780. if (do_ctimings)
  1781. do_time(1 /*,fd*/); /* time cache */
  1782. if (do_timings)
  1783. do_time(0 /*,fd*/); /* time device */
  1784. if (do_flush)
  1785. flush_buffer_cache();
  1786. close(fd);
  1787. }
  1788. #if ENABLE_FEATURE_HDPARM_GET_IDENTITY
  1789. static int fromhex(unsigned char c)
  1790. {
  1791. if (isdigit(c))
  1792. return (c - '0');
  1793. if (c >= 'a' && c <= 'f')
  1794. return (c - ('a' - 10));
  1795. bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
  1796. }
  1797. static void identify_from_stdin(void) NORETURN;
  1798. static void identify_from_stdin(void)
  1799. {
  1800. uint16_t sbuf[256];
  1801. unsigned char buf[1280];
  1802. unsigned char *b = (unsigned char *)buf;
  1803. int i;
  1804. xread(STDIN_FILENO, buf, 1280);
  1805. // Convert the newline-separated hex data into an identify block.
  1806. for (i = 0; i < 256; i++) {
  1807. int j;
  1808. for (j = 0; j < 4; j++)
  1809. sbuf[i] = (sbuf[i] << 4) + fromhex(*(b++));
  1810. }
  1811. // Parse the data.
  1812. identify(sbuf);
  1813. }
  1814. #else
  1815. void identify_from_stdin(void);
  1816. #endif
  1817. /* busybox specific stuff */
  1818. static int parse_opts(unsigned long *value, int min, int max)
  1819. {
  1820. if (optarg) {
  1821. *value = xatol_range(optarg, min, max);
  1822. return IS_SET;
  1823. }
  1824. return IS_GET;
  1825. }
  1826. static int parse_opts_0_max(unsigned long *value, int max)
  1827. {
  1828. return parse_opts(value, 0, max);
  1829. }
  1830. static int parse_opts_0_1(unsigned long *value)
  1831. {
  1832. return parse_opts(value, 0, 1);
  1833. }
  1834. static int parse_opts_0_INTMAX(unsigned long *value)
  1835. {
  1836. return parse_opts(value, 0, INT_MAX);
  1837. }
  1838. static void parse_xfermode(int flag, smallint *get, smallint *set, int *value)
  1839. {
  1840. if (flag) {
  1841. *get = IS_GET;
  1842. if (optarg) {
  1843. *value = translate_xfermode(optarg);
  1844. *set = (*value > -1);
  1845. }
  1846. }
  1847. }
  1848. /*------- getopt short options --------*/
  1849. static const char hdparm_options[] ALIGN1 =
  1850. "gfu::n::p:r::m::c::k::a::B:tT"
  1851. IF_FEATURE_HDPARM_GET_IDENTITY("iI")
  1852. IF_FEATURE_HDPARM_HDIO_GETSET_DMA("d::")
  1853. #ifdef HDIO_DRIVE_CMD
  1854. "S:D:P:X:K:A:L:W:CyYzZ"
  1855. #endif
  1856. IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF("U:")
  1857. #ifdef HDIO_GET_QDMA
  1858. #ifdef HDIO_SET_QDMA
  1859. "Q:"
  1860. #else
  1861. "Q"
  1862. #endif
  1863. #endif
  1864. IF_FEATURE_HDPARM_HDIO_DRIVE_RESET("w")
  1865. IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF("x::b:")
  1866. IF_FEATURE_HDPARM_HDIO_SCAN_HWIF("R:");
  1867. /*-------------------------------------*/
  1868. /* our main() routine: */
  1869. int hdparm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1870. int hdparm_main(int argc, char **argv)
  1871. {
  1872. int c;
  1873. int flagcount = 0;
  1874. while ((c = getopt(argc, argv, hdparm_options)) >= 0) {
  1875. flagcount++;
  1876. IF_FEATURE_HDPARM_GET_IDENTITY(get_IDentity |= (c == 'I'));
  1877. IF_FEATURE_HDPARM_GET_IDENTITY(get_identity |= (c == 'i'));
  1878. get_geom |= (c == 'g');
  1879. do_flush |= (c == 'f');
  1880. if (c == 'u') getset_unmask = parse_opts_0_1(&unmask);
  1881. IF_FEATURE_HDPARM_HDIO_GETSET_DMA(
  1882. if (c == 'd') getset_dma = parse_opts_0_max(&dma, 9);
  1883. )
  1884. if (c == 'n') getset_nowerr = parse_opts_0_1(&nowerr);
  1885. parse_xfermode((c == 'p'), &noisy_piomode, &set_piomode, &piomode);
  1886. if (c == 'r') getset_readonly = parse_opts_0_1(&readonly);
  1887. if (c == 'm') getset_mult = parse_opts_0_INTMAX(&mult /*32*/);
  1888. if (c == 'c') getset_io32bit = parse_opts_0_INTMAX(&io32bit /*8*/);
  1889. if (c == 'k') getset_keep = parse_opts_0_1(&keep);
  1890. if (c == 'a') getset_readahead = parse_opts_0_INTMAX(&Xreadahead);
  1891. if (c == 'B') getset_apmmode = parse_opts(&apmmode, 1, 255);
  1892. do_flush |= do_timings |= (c == 't');
  1893. do_flush |= do_ctimings |= (c == 'T');
  1894. #ifdef HDIO_DRIVE_CMD
  1895. if (c == 'S') getset_standby = parse_opts_0_max(&standby_requested, 255);
  1896. if (c == 'D') getset_defects = parse_opts_0_INTMAX(&defects);
  1897. if (c == 'P') getset_prefetch = parse_opts_0_INTMAX(&prefetch);
  1898. parse_xfermode((c == 'X'), &get_xfermode, &set_xfermode, &xfermode_requested);
  1899. if (c == 'K') getset_dkeep = parse_opts_0_1(&prefetch);
  1900. if (c == 'A') getset_lookahead = parse_opts_0_1(&lookahead);
  1901. if (c == 'L') getset_doorlock = parse_opts_0_1(&doorlock);
  1902. if (c == 'W') getset_wcache = parse_opts_0_1(&wcache);
  1903. get_powermode |= (c == 'C');
  1904. set_standbynow |= (c == 'y');
  1905. set_sleepnow |= (c == 'Y');
  1906. reread_partn |= (c == 'z');
  1907. set_seagate |= (c == 'Z');
  1908. #endif
  1909. IF_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF(if (c == 'U') unregister_hwif = parse_opts_0_INTMAX(&hwif));
  1910. #ifdef HDIO_GET_QDMA
  1911. if (c == 'Q') {
  1912. getset_dma_q = parse_opts_0_INTMAX(&dma_q);
  1913. }
  1914. #endif
  1915. IF_FEATURE_HDPARM_HDIO_DRIVE_RESET(perform_reset = (c == 'r'));
  1916. IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'x') perform_tristate = parse_opts_0_1(&tristate));
  1917. IF_FEATURE_HDPARM_HDIO_TRISTATE_HWIF(if (c == 'b') getset_busstate = parse_opts_0_max(&busstate, 2));
  1918. #if ENABLE_FEATURE_HDPARM_HDIO_SCAN_HWIF
  1919. if (c == 'R') {
  1920. scan_hwif = parse_opts_0_INTMAX(&hwif_data);
  1921. hwif_ctrl = xatoi_positive((argv[optind]) ? argv[optind] : "");
  1922. hwif_irq = xatoi_positive((argv[optind+1]) ? argv[optind+1] : "");
  1923. /* Move past the 2 additional arguments */
  1924. argv += 2;
  1925. argc -= 2;
  1926. }
  1927. #endif
  1928. }
  1929. /* When no flags are given (flagcount = 0), -acdgkmnru is assumed. */
  1930. if (!flagcount) {
  1931. getset_mult = getset_io32bit = getset_unmask = getset_keep = getset_readonly = getset_readahead = get_geom = IS_GET;
  1932. IF_FEATURE_HDPARM_HDIO_GETSET_DMA(getset_dma = IS_GET);
  1933. }
  1934. argv += optind;
  1935. if (!*argv) {
  1936. if (ENABLE_FEATURE_HDPARM_GET_IDENTITY && !isatty(STDIN_FILENO))
  1937. identify_from_stdin(); /* EXIT */
  1938. bb_show_usage();
  1939. }
  1940. do {
  1941. process_dev(*argv++);
  1942. } while (*argv);
  1943. return EXIT_SUCCESS;
  1944. }