3
0

hdparm.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * hdparm implementation for busybox
  4. *
  5. *
  6. * Copyright (C) [2003] by [Matteo Croce] <3297627799@wind.it>
  7. *
  8. * Hacked by Tito <farmatito@tiscali.it> for size optimization.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. * 02111-1307 USA
  24. *
  25. *
  26. * This program is based on the source code of hdparm: see below...
  27. * hdparm.c - Command line interface to get/set hard disk parameters
  28. * - by Mark Lord (C) 1994-2002 -- freely distributable
  29. */
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <fcntl.h>
  34. #include <errno.h>
  35. #include <ctype.h>
  36. #include <endian.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/shm.h>
  39. #include <sys/sysmacros.h>
  40. #include <sys/time.h>
  41. #include <sys/times.h>
  42. #include <sys/mount.h>
  43. #include "busybox.h"
  44. #include <linux/types.h>
  45. #include <linux/hdreg.h>
  46. #include <linux/major.h>
  47. #include <asm/byteorder.h>
  48. #if (__BYTE_ORDER == __BIG_ENDIAN) && !defined(__USE_XOPEN)
  49. #define __USE_XOPEN
  50. #endif
  51. /* device types */
  52. /* ------------ */
  53. #define NO_DEV 0xffff
  54. #define ATA_DEV 0x0000
  55. #define ATAPI_DEV 0x0001
  56. /* word definitions */
  57. /* ---------------- */
  58. #define GEN_CONFIG 0 /* general configuration */
  59. #define LCYLS 1 /* number of logical cylinders */
  60. #define CONFIG 2 /* specific configuration */
  61. #define LHEADS 3 /* number of logical heads */
  62. #define TRACK_BYTES 4 /* number of bytes/track (ATA-1) */
  63. #define SECT_BYTES 5 /* number of bytes/sector (ATA-1) */
  64. #define LSECTS 6 /* number of logical sectors/track */
  65. #define START_SERIAL 10 /* ASCII serial number */
  66. #define LENGTH_SERIAL 10 /* 10 words (20 bytes or characters) */
  67. #define BUF_TYPE 20 /* buffer type (ATA-1) */
  68. #define BUFFER__SIZE 21 /* buffer size (ATA-1) */
  69. #define RW_LONG 22 /* extra bytes in R/W LONG cmd ( < ATA-4)*/
  70. #define START_FW_REV 23 /* ASCII firmware revision */
  71. #define LENGTH_FW_REV 4 /* 4 words (8 bytes or characters) */
  72. #define START_MODEL 27 /* ASCII model number */
  73. #define LENGTH_MODEL 20 /* 20 words (40 bytes or characters) */
  74. #define SECTOR_XFER_MAX 47 /* r/w multiple: max sectors xfered */
  75. #define DWORD_IO 48 /* can do double-word IO (ATA-1 only) */
  76. #define CAPAB_0 49 /* capabilities */
  77. #define CAPAB_1 50
  78. #define PIO_MODE 51 /* max PIO mode supported (obsolete)*/
  79. #define DMA_MODE 52 /* max Singleword DMA mode supported (obs)*/
  80. #define WHATS_VALID 53 /* what fields are valid */
  81. #define LCYLS_CUR 54 /* current logical cylinders */
  82. #define LHEADS_CUR 55 /* current logical heads */
  83. #define LSECTS_CUR 56 /* current logical sectors/track */
  84. #define CAPACITY_LSB 57 /* current capacity in sectors */
  85. #define CAPACITY_MSB 58
  86. #define SECTOR_XFER_CUR 59 /* r/w multiple: current sectors xfered */
  87. #define LBA_SECTS_LSB 60 /* LBA: total number of user */
  88. #define LBA_SECTS_MSB 61 /* addressable sectors */
  89. #define SINGLE_DMA 62 /* singleword DMA modes */
  90. #define MULTI_DMA 63 /* multiword DMA modes */
  91. #define ADV_PIO_MODES 64 /* advanced PIO modes supported */
  92. /* multiword DMA xfer cycle time: */
  93. #define DMA_TIME_MIN 65 /* - minimum */
  94. #define DMA_TIME_NORM 66 /* - manufacturer's recommended */
  95. /* minimum PIO xfer cycle time: */
  96. #define PIO_NO_FLOW 67 /* - without flow control */
  97. #define PIO_FLOW 68 /* - with IORDY flow control */
  98. #define PKT_REL 71 /* typical #ns from PKT cmd to bus rel */
  99. #define SVC_NBSY 72 /* typical #ns from SERVICE cmd to !BSY */
  100. #define CDR_MAJOR 73 /* CD ROM: major version number */
  101. #define CDR_MINOR 74 /* CD ROM: minor version number */
  102. #define QUEUE_DEPTH 75 /* queue depth */
  103. #define MAJOR 80 /* major version number */
  104. #define MINOR 81 /* minor version number */
  105. #define CMDS_SUPP_0 82 /* command/feature set(s) supported */
  106. #define CMDS_SUPP_1 83
  107. #define CMDS_SUPP_2 84
  108. #define CMDS_EN_0 85 /* command/feature set(s) enabled */
  109. #define CMDS_EN_1 86
  110. #define CMDS_EN_2 87
  111. #define ULTRA_DMA 88 /* ultra DMA modes */
  112. /* time to complete security erase */
  113. #define ERASE_TIME 89 /* - ordinary */
  114. #define ENH_ERASE_TIME 90 /* - enhanced */
  115. #define ADV_PWR 91 /* current advanced power management level
  116. in low byte, 0x40 in high byte. */
  117. #define PSWD_CODE 92 /* master password revision code */
  118. #define HWRST_RSLT 93 /* hardware reset result */
  119. #define ACOUSTIC 94 /* acoustic mgmt values ( >= ATA-6) */
  120. #define LBA_LSB 100 /* LBA: maximum. Currently only 48 */
  121. #define LBA_MID 101 /* bits are used, but addr 103 */
  122. #define LBA_48_MSB 102 /* has been reserved for LBA in */
  123. #define LBA_64_MSB 103 /* the future. */
  124. #define RM_STAT 127 /* removable media status notification feature set support */
  125. #define SECU_STATUS 128 /* security status */
  126. #define CFA_PWR_MODE 160 /* CFA power mode 1 */
  127. #define START_MEDIA 176 /* media serial number */
  128. #define LENGTH_MEDIA 20 /* 20 words (40 bytes or characters)*/
  129. #define START_MANUF 196 /* media manufacturer I.D. */
  130. #define LENGTH_MANUF 10 /* 10 words (20 bytes or characters) */
  131. #define INTEGRITY 255 /* integrity word */
  132. /* bit definitions within the words */
  133. /* -------------------------------- */
  134. /* many words are considered valid if bit 15 is 0 and bit 14 is 1 */
  135. #define VALID 0xc000
  136. #define VALID_VAL 0x4000
  137. /* many words are considered invalid if they are either all-0 or all-1 */
  138. #define NOVAL_0 0x0000
  139. #define NOVAL_1 0xffff
  140. /* word 0: gen_config */
  141. #define NOT_ATA 0x8000
  142. #define NOT_ATAPI 0x4000 /* (check only if bit 15 == 1) */
  143. #define MEDIA_REMOVABLE 0x0080
  144. #define DRIVE_NOT_REMOVABLE 0x0040 /* bit obsoleted in ATA 6 */
  145. #define INCOMPLETE 0x0004
  146. #define CFA_SUPPORT_VAL 0x848a /* 848a=CFA feature set support */
  147. #define DRQ_RESPONSE_TIME 0x0060
  148. #define DRQ_3MS_VAL 0x0000
  149. #define DRQ_INTR_VAL 0x0020
  150. #define DRQ_50US_VAL 0x0040
  151. #define PKT_SIZE_SUPPORTED 0x0003
  152. #define PKT_SIZE_12_VAL 0x0000
  153. #define PKT_SIZE_16_VAL 0x0001
  154. #define EQPT_TYPE 0x1f00
  155. #define SHIFT_EQPT 8
  156. #define CDROM 0x0005
  157. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  158. static const char *pkt_str[] = {
  159. "Direct-access device", /* word 0, bits 12-8 = 00 */
  160. "Sequential-access device", /* word 0, bits 12-8 = 01 */
  161. "Printer", /* word 0, bits 12-8 = 02 */
  162. "Processor", /* word 0, bits 12-8 = 03 */
  163. "Write-once device", /* word 0, bits 12-8 = 04 */
  164. "CD-ROM", /* word 0, bits 12-8 = 05 */
  165. "Scanner", /* word 0, bits 12-8 = 06 */
  166. "Optical memory", /* word 0, bits 12-8 = 07 */
  167. "Medium changer", /* word 0, bits 12-8 = 08 */
  168. "Communications device", /* word 0, bits 12-8 = 09 */
  169. "ACS-IT8 device", /* word 0, bits 12-8 = 0a */
  170. "ACS-IT8 device", /* word 0, bits 12-8 = 0b */
  171. "Array controller", /* word 0, bits 12-8 = 0c */
  172. "Enclosure services", /* word 0, bits 12-8 = 0d */
  173. "Reduced block command device", /* word 0, bits 12-8 = 0e */
  174. "Optical card reader/writer", /* word 0, bits 12-8 = 0f */
  175. "", /* word 0, bits 12-8 = 10 */
  176. "", /* word 0, bits 12-8 = 11 */
  177. "", /* word 0, bits 12-8 = 12 */
  178. "", /* word 0, bits 12-8 = 13 */
  179. "", /* word 0, bits 12-8 = 14 */
  180. "", /* word 0, bits 12-8 = 15 */
  181. "", /* word 0, bits 12-8 = 16 */
  182. "", /* word 0, bits 12-8 = 17 */
  183. "", /* word 0, bits 12-8 = 18 */
  184. "", /* word 0, bits 12-8 = 19 */
  185. "", /* word 0, bits 12-8 = 1a */
  186. "", /* word 0, bits 12-8 = 1b */
  187. "", /* word 0, bits 12-8 = 1c */
  188. "", /* word 0, bits 12-8 = 1d */
  189. "", /* word 0, bits 12-8 = 1e */
  190. "Unknown", /* word 0, bits 12-8 = 1f */
  191. };
  192. static const char *ata1_cfg_str[] = { /* word 0 in ATA-1 mode */
  193. "reserved", /* bit 0 */
  194. "hard sectored", /* bit 1 */
  195. "soft sectored", /* bit 2 */
  196. "not MFM encoded ", /* bit 3 */
  197. "head switch time > 15us", /* bit 4 */
  198. "spindle motor control option", /* bit 5 */
  199. "fixed drive", /* bit 6 */
  200. "removable drive", /* bit 7 */
  201. "disk xfer rate <= 5Mbs", /* bit 8 */
  202. "disk xfer rate > 5Mbs, <= 10Mbs", /* bit 9 */
  203. "disk xfer rate > 5Mbs", /* bit 10 */
  204. "rotational speed tol.", /* bit 11 */
  205. "data strobe offset option", /* bit 12 */
  206. "track offset option", /* bit 13 */
  207. "format speed tolerance gap reqd", /* bit 14 */
  208. "ATAPI" /* bit 14 */
  209. };
  210. #endif
  211. /* word 1: number of logical cylinders */
  212. #define LCYLS_MAX 0x3fff /* maximum allowable value */
  213. /* word 2: specific configuration
  214. * (a) require SET FEATURES to spin-up
  215. * (b) require spin-up to fully reply to IDENTIFY DEVICE
  216. */
  217. #define STBY_NID_VAL 0x37c8 /* (a) and (b) */
  218. #define STBY_ID_VAL 0x738c /* (a) and not (b) */
  219. #define PWRD_NID_VAL 0x8c73 /* not (a) and (b) */
  220. #define PWRD_ID_VAL 0xc837 /* not (a) and not (b) */
  221. /* words 47 & 59: sector_xfer_max & sector_xfer_cur */
  222. #define SECTOR_XFER 0x00ff /* sectors xfered on r/w multiple cmds*/
  223. #define MULTIPLE_SETTING_VALID 0x0100 /* 1=multiple sector setting is valid */
  224. /* word 49: capabilities 0 */
  225. #define STD_STBY 0x2000 /* 1=standard values supported (ATA);
  226. 0=vendor specific values */
  227. #define IORDY_SUP 0x0800 /* 1=support; 0=may be supported */
  228. #define IORDY_OFF 0x0400 /* 1=may be disabled */
  229. #define LBA_SUP 0x0200 /* 1=Logical Block Address support */
  230. #define DMA_SUP 0x0100 /* 1=Direct Memory Access support */
  231. #define DMA_IL_SUP 0x8000 /* 1=interleaved DMA support (ATAPI) */
  232. #define CMD_Q_SUP 0x4000 /* 1=command queuing support (ATAPI) */
  233. #define OVLP_SUP 0x2000 /* 1=overlap operation support (ATAPI) */
  234. #define SWRST_REQ 0x1000 /* 1=ATA SW reset required (ATAPI, obsolete */
  235. /* word 50: capabilities 1 */
  236. #define MIN_STANDBY_TIMER 0x0001 /* 1=device specific standby timer value minimum */
  237. /* words 51 & 52: PIO & DMA cycle times */
  238. #define MODE 0xff00 /* the mode is in the MSBs */
  239. /* word 53: whats_valid */
  240. #define OK_W88 0x0004 /* the ultra_dma info is valid */
  241. #define OK_W64_70 0x0002 /* see above for word descriptions */
  242. #define OK_W54_58 0x0001 /* current cyl, head, sector, cap. info valid */
  243. /*word 63,88: dma_mode, ultra_dma_mode*/
  244. #define MODE_MAX 7 /* bit definitions force udma <=7 (when
  245. * udma >=8 comes out it'll have to be
  246. * defined in a new dma_mode word!) */
  247. /* word 64: PIO transfer modes */
  248. #define PIO_SUP 0x00ff /* only bits 0 & 1 are used so far, */
  249. #define PIO_MODE_MAX 8 /* but all 8 bits are defined */
  250. /* word 75: queue_depth */
  251. #define DEPTH_BITS 0x001f /* bits used for queue depth */
  252. /* words 80-81: version numbers */
  253. /* NOVAL_0 or NOVAL_1 means device does not report version */
  254. /* word 81: minor version number */
  255. #define MINOR_MAX 0x1C
  256. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  257. static const char *minor_str[] = { /* word 81 value: */
  258. "device does not report version", /* 0x0000 */
  259. "ATA-1 X3T9.2 781D prior to revision 4", /* 0x0001 */
  260. "ATA-1 published, ANSI X3.221-1994", /* 0x0002 */
  261. "ATA-1 X3T9.2 781D revision 4", /* 0x0003 */
  262. "ATA-2 published, ANSI X3.279-1996", /* 0x0004 */
  263. "ATA-2 X3T10 948D prior to revision 2k", /* 0x0005 */
  264. "ATA-3 X3T10 2008D revision 1", /* 0x0006 */
  265. "ATA-2 X3T10 948D revision 2k", /* 0x0007 */
  266. "ATA-3 X3T10 2008D revision 0", /* 0x0008 */
  267. "ATA-2 X3T10 948D revision 3", /* 0x0009 */
  268. "ATA-3 published, ANSI X3.298-199x", /* 0x000a */
  269. "ATA-3 X3T10 2008D revision 6", /* 0x000b */
  270. "ATA-3 X3T13 2008D revision 7 and 7a", /* 0x000c */
  271. "ATA/ATAPI-4 X3T13 1153D revision 6", /* 0x000d */
  272. "ATA/ATAPI-4 T13 1153D revision 13", /* 0x000e */
  273. "ATA/ATAPI-4 X3T13 1153D revision 7", /* 0x000f */
  274. "ATA/ATAPI-4 T13 1153D revision 18", /* 0x0010 */
  275. "ATA/ATAPI-4 T13 1153D revision 15", /* 0x0011 */
  276. "ATA/ATAPI-4 published, ANSI NCITS 317-1998", /* 0x0012 */
  277. "ATA/ATAPI-5 T13 1321D revision 3",
  278. "ATA/ATAPI-4 T13 1153D revision 14", /* 0x0014 */
  279. "ATA/ATAPI-5 T13 1321D revision 1", /* 0x0015 */
  280. "ATA/ATAPI-5 published, ANSI NCITS 340-2000", /* 0x0016 */
  281. "ATA/ATAPI-4 T13 1153D revision 17", /* 0x0017 */
  282. "ATA/ATAPI-6 T13 1410D revision 0", /* 0x0018 */
  283. "ATA/ATAPI-6 T13 1410D revision 3a", /* 0x0019 */
  284. "Reserved", /* 0x001a */
  285. "ATA/ATAPI-6 T13 1410D revision 2", /* 0x001b */
  286. "ATA/ATAPI-6 T13 1410D revision 1", /* 0x001c */
  287. "reserved" /* 0x001d */
  288. "reserved" /* 0x001e */
  289. "reserved" /* 0x001f-0xfffe*/
  290. };
  291. #endif
  292. static const char actual_ver[] = {
  293. /* word 81 value: */
  294. 0, /* 0x0000 WARNING: */
  295. 1, /* 0x0001 WARNING: */
  296. 1, /* 0x0002 WARNING: */
  297. 1, /* 0x0003 WARNING: */
  298. 2, /* 0x0004 WARNING: This array */
  299. 2, /* 0x0005 WARNING: corresponds */
  300. 3, /* 0x0006 WARNING: *exactly* */
  301. 2, /* 0x0007 WARNING: to the ATA/ */
  302. 3, /* 0x0008 WARNING: ATAPI version */
  303. 2, /* 0x0009 WARNING: listed in */
  304. 3, /* 0x000a WARNING: the */
  305. 3, /* 0x000b WARNING: minor_str */
  306. 3, /* 0x000c WARNING: array */
  307. 4, /* 0x000d WARNING: above. */
  308. 4, /* 0x000e WARNING: */
  309. 4, /* 0x000f WARNING: if you change */
  310. 4, /* 0x0010 WARNING: that one, */
  311. 4, /* 0x0011 WARNING: change this one */
  312. 4, /* 0x0012 WARNING: too!!! */
  313. 5, /* 0x0013 WARNING: */
  314. 4, /* 0x0014 WARNING: */
  315. 5, /* 0x0015 WARNING: */
  316. 5, /* 0x0016 WARNING: */
  317. 4, /* 0x0017 WARNING: */
  318. 6, /* 0x0018 WARNING: */
  319. 6, /* 0x0019 WARNING: */
  320. 0, /* 0x001a WARNING: */
  321. 6, /* 0x001b WARNING: */
  322. 6, /* 0x001c WARNING: */
  323. 0 /* 0x001d-0xfffe */
  324. };
  325. /* words 82-84: cmds/feats supported */
  326. #define CMDS_W82 0x77ff /* word 82: defined command locations*/
  327. #define CMDS_W83 0x3fff /* word 83: defined command locations*/
  328. #define CMDS_W84 0x002f /* word 83: defined command locations*/
  329. #define SUPPORT_48_BIT 0x0400
  330. #define NUM_CMD_FEAT_STR 48
  331. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  332. static const char *cmd_feat_str[] = {
  333. "", /* word 82 bit 15: obsolete */
  334. "NOP cmd", /* word 82 bit 14 */
  335. "READ BUFFER cmd", /* word 82 bit 13 */
  336. "WRITE BUFFER cmd", /* word 82 bit 12 */
  337. "", /* word 82 bit 11: obsolete */
  338. "Host Protected Area feature set", /* word 82 bit 10 */
  339. "DEVICE RESET cmd", /* word 82 bit 9 */
  340. "SERVICE interrupt", /* word 82 bit 8 */
  341. "Release interrupt", /* word 82 bit 7 */
  342. "Look-ahead", /* word 82 bit 6 */
  343. "Write cache", /* word 82 bit 5 */
  344. "PACKET command feature set", /* word 82 bit 4 */
  345. "Power Management feature set", /* word 82 bit 3 */
  346. "Removable Media feature set", /* word 82 bit 2 */
  347. "Security Mode feature set", /* word 82 bit 1 */
  348. "SMART feature set", /* word 82 bit 0 */
  349. /* --------------*/
  350. "", /* word 83 bit 15: !valid bit */
  351. "", /* word 83 bit 14: valid bit */
  352. "FLUSH CACHE EXT command", /* word 83 bit 13 */
  353. "Mandatory FLUSH CACHE command ", /* word 83 bit 12 */
  354. "Device Configuration Overlay feature set ",
  355. "48-bit Address feature set ", /* word 83 bit 10 */
  356. "",
  357. "SET MAX security extension", /* word 83 bit 8 */
  358. "Address Offset Reserved Area Boot", /* word 83 bit 7 */
  359. "SET FEATURES subcommand required to spinup after power up",
  360. "Power-Up In Standby feature set", /* word 83 bit 5 */
  361. "Removable Media Status Notification feature set",
  362. "Adv. Power Management feature set",/* word 83 bit 3 */
  363. "CFA feature set", /* word 83 bit 2 */
  364. "READ/WRITE DMA QUEUED", /* word 83 bit 1 */
  365. "DOWNLOAD MICROCODE cmd", /* word 83 bit 0 */
  366. /* --------------*/
  367. "", /* word 84 bit 15: !valid bit */
  368. "", /* word 84 bit 14: valid bit */
  369. "", /* word 84 bit 13: reserved */
  370. "", /* word 84 bit 12: reserved */
  371. "", /* word 84 bit 11: reserved */
  372. "", /* word 84 bit 10: reserved */
  373. "", /* word 84 bit 9: reserved */
  374. "", /* word 84 bit 8: reserved */
  375. "", /* word 84 bit 7: reserved */
  376. "", /* word 84 bit 6: reserved */
  377. "General Purpose Logging feature set", /* word 84 bit 5 */
  378. "", /* word 84 bit 4: reserved */
  379. "Media Card Pass Through Command feature set ",
  380. "Media serial number ", /* word 84 bit 2 */
  381. "SMART self-test ", /* word 84 bit 1 */
  382. "SMART error logging " /* word 84 bit 0 */
  383. };
  384. #endif
  385. /* words 85-87: cmds/feats enabled */
  386. /* use cmd_feat_str[] to display what commands and features have
  387. * been enabled with words 85-87
  388. */
  389. /* words 89, 90, SECU ERASE TIME */
  390. #define ERASE_BITS 0x00ff
  391. /* word 92: master password revision */
  392. /* NOVAL_0 or NOVAL_1 means no support for master password revision */
  393. /* word 93: hw reset result */
  394. #define CBLID 0x2000 /* CBLID status */
  395. #define RST0 0x0001 /* 1=reset to device #0 */
  396. #define DEV_DET 0x0006 /* how device num determined */
  397. #define JUMPER_VAL 0x0002 /* device num determined by jumper */
  398. #define CSEL_VAL 0x0004 /* device num determined by CSEL_VAL */
  399. /* word 127: removable media status notification feature set support */
  400. #define RM_STAT_BITS 0x0003
  401. #define RM_STAT_SUP 0x0001
  402. /* word 128: security */
  403. #define SECU_ENABLED 0x0002
  404. #define SECU_LEVEL 0x0010
  405. #define NUM_SECU_STR 6
  406. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  407. static const char *secu_str[] = {
  408. "supported", /* word 128, bit 0 */
  409. "enabled", /* word 128, bit 1 */
  410. "locked", /* word 128, bit 2 */
  411. "frozen", /* word 128, bit 3 */
  412. "expired: security count", /* word 128, bit 4 */
  413. "supported: enhanced erase" /* word 128, bit 5 */
  414. };
  415. #endif
  416. /* word 160: CFA power mode */
  417. #define VALID_W160 0x8000 /* 1=word valid */
  418. #define PWR_MODE_REQ 0x2000 /* 1=CFA power mode req'd by some cmds*/
  419. #define PWR_MODE_OFF 0x1000 /* 1=CFA power moded disabled */
  420. #define MAX_AMPS 0x0fff /* value = max current in ma */
  421. /* word 255: integrity */
  422. #define SIG 0x00ff /* signature location */
  423. #define SIG_VAL 0x00A5 /* signature value */
  424. #define VERSION "v5.4"
  425. #define TIMING_MB 64
  426. #define TIMING_BUF_MB 1
  427. #define TIMING_BUF_BYTES (TIMING_BUF_MB * 1024 * 1024)
  428. #define TIMING_BUF_COUNT (timing_MB / TIMING_BUF_MB)
  429. #define BUFCACHE_FACTOR 2
  430. #undef DO_FLUSHCACHE /* under construction: force cache flush on -W0 */
  431. /* Busybox messages and functions */
  432. static const char * const bb_msg_shared_mem ="could not %s sharedmem buf";
  433. static const char * const bb_msg_op_not_supp =" operation not supported on %s disks";
  434. static void bb_ioctl(int fd, int request, void *argp, const char *string)
  435. {
  436. if (ioctl (fd, request, argp) != 0)
  437. bb_perror_msg(" %s", string);
  438. }
  439. static void if_printf(unsigned long i, char *fmt, ... )
  440. {
  441. va_list ap;
  442. va_start(ap, fmt);
  443. if (i)
  444. vprintf(fmt, ap);
  445. va_end(ap);
  446. }
  447. static void on_off (unsigned int value);
  448. static void if_printf_on_off(unsigned long get_arg,const char *fmt, unsigned long arg)
  449. {
  450. if (get_arg)
  451. {
  452. printf(fmt, arg);
  453. on_off(arg);
  454. }
  455. }
  456. static void bb_ioctl_on_off(int fd, int request, void *argp, const char *string,
  457. const char * fmt)
  458. {
  459. if (ioctl (fd, request, &argp) != 0)
  460. bb_perror_msg(" %s", string);
  461. else
  462. {
  463. printf(fmt, (unsigned long) argp);
  464. on_off((unsigned long) argp);
  465. }
  466. }
  467. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  468. static void if_else_printf(unsigned long i, char *fmt1, char *fmt2, ... )
  469. {
  470. va_list ap;
  471. va_start(ap, fmt2);
  472. if (i)
  473. vprintf(fmt1, ap);
  474. else
  475. vprintf(fmt2, ap);
  476. va_end(ap);
  477. }
  478. static void print_ascii(uint16_t *p, uint8_t length);
  479. static void xprint_ascii(uint16_t *val ,int i, char * string, int n)
  480. {
  481. if(val[i])
  482. {
  483. printf("\t%-20s",string);
  484. print_ascii(&val[i], n);
  485. }
  486. }
  487. static void if_strcat(unsigned long test, char *modes, char *string)
  488. {
  489. if (test)
  490. strcat(modes,string);
  491. }
  492. #endif
  493. static void sync_and_sleep(int i)
  494. {
  495. sync();
  496. sleep(i);
  497. }
  498. static uint16_t check_if_min_and_set_val(uint16_t a, uint16_t b)
  499. {
  500. if( a < b)
  501. a = b;
  502. return a;
  503. }
  504. static uint16_t check_if_maj_and_set_val(uint16_t a, uint16_t b)
  505. {
  506. if( a > b)
  507. a = b;
  508. return a;
  509. }
  510. static unsigned long int set_flag(char *p, char max)
  511. {
  512. if (*p >= '0' && *p <= max )
  513. return 1;
  514. return 0;
  515. }
  516. /* end of busybox specific stuff */
  517. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  518. static uint8_t mode_loop(uint16_t mode_sup, uint16_t mode_sel, int cc, uint8_t *have_mode)
  519. {
  520. uint16_t ii;
  521. uint8_t err_dma = 0;
  522. for(ii = 0; ii <= MODE_MAX; ii++)
  523. {
  524. if(mode_sel & 0x0001)
  525. {
  526. printf("*%cdma%u ",cc,ii);
  527. if(*have_mode)
  528. err_dma = 1;
  529. *have_mode = 1;
  530. }
  531. else if(mode_sup & 0x0001)
  532. printf("%cdma%u ",cc,ii);
  533. mode_sup >>=1;
  534. mode_sel >>=1;
  535. }
  536. return err_dma;
  537. }
  538. static void print_ascii(uint16_t *p, uint8_t length) {
  539. uint8_t ii;
  540. char cl;
  541. /* find first non-space & print it */
  542. for(ii = 0; ii< length; ii++)
  543. {
  544. if(((char) 0x00ff&((*p)>>8)) != ' ')
  545. break;
  546. if((cl = (char) 0x00ff&(*p)) != ' ')
  547. {
  548. if_printf((cl != '\0'),"%c",cl);
  549. p++;
  550. ii++;
  551. break;
  552. }
  553. p++;
  554. }
  555. /* print the rest */
  556. for(; ii< length; ii++)
  557. {
  558. if(!(*p))
  559. break; /* some older devices have NULLs */
  560. printf("%c%c",(char)0x00ff&((*p)>>8),(char)(*p)&0x00ff);
  561. p++;
  562. }
  563. printf("\n");
  564. }
  565. /* identify() is the only extern function used across two source files. The
  566. others, though, were declared in hdparm.c with global scope; since other
  567. functions in that file have static (file) scope, I assume the difference is
  568. intentional. */
  569. static void identify (uint16_t *id_supplied, const char *devname)
  570. {
  571. char *id_file = NULL;
  572. FILE *fl;
  573. uint16_t val[256], ii, jj, kk;
  574. uint16_t like_std = 1, std = 0, min_std = 0xffff;
  575. uint16_t dev = NO_DEV, eqpt = NO_DEV;
  576. uint8_t have_mode = 0, err_dma = 0;
  577. uint8_t chksum = 0;
  578. uint32_t ll, mm, nn, oo;
  579. __u64 bbbig; /* (:) */
  580. if (id_supplied)
  581. {
  582. #if __BYTE_ORDER == __BIG_ENDIAN
  583. swab(id_supplied, val, sizeof(val));
  584. #else
  585. memcpy(val, id_supplied, sizeof(val));
  586. #endif
  587. }
  588. else
  589. {
  590. id_file = xcalloc(1, strlen(devname)+22);
  591. sprintf(id_file, "/proc/ide/%s/identify", devname);
  592. /* open the file, read in all the info and close it */
  593. if (id_file == NULL)
  594. fl = stdin;
  595. else
  596. fl = bb_xfopen(id_file, "r");
  597. /* calculate checksum over all bytes */
  598. for(ii = GEN_CONFIG; ii<=INTEGRITY; ii++)
  599. {
  600. unsigned int scratch;
  601. if(1 != fscanf(fl,"%04x",&scratch))
  602. break;
  603. val[ii] = (uint16_t)scratch;
  604. chksum += val[ii] + (val[ii] >> 8);
  605. }
  606. fclose(fl);
  607. /*bb_fclose_nonstdin(fl);*/
  608. if(ii < (INTEGRITY+1))
  609. bb_error_msg_and_die("Input file wrong format or length");
  610. }
  611. chksum &= 0xff;
  612. /* check if we recognise the device type */
  613. printf("\n");
  614. if(!(val[GEN_CONFIG] & NOT_ATA))
  615. {
  616. dev = ATA_DEV;
  617. printf("ATA device, with ");
  618. }
  619. else if(val[GEN_CONFIG]==CFA_SUPPORT_VAL)
  620. {
  621. dev = ATA_DEV;
  622. like_std = 4;
  623. printf("CompactFlash ATA device, with ");
  624. }
  625. else if(!(val[GEN_CONFIG] & NOT_ATAPI))
  626. {
  627. dev = ATAPI_DEV;
  628. eqpt = (val[GEN_CONFIG] & EQPT_TYPE) >> SHIFT_EQPT;
  629. printf("ATAPI %s, with ", pkt_str[eqpt]);
  630. like_std = 3;
  631. }
  632. else
  633. /*"Unknown device type:\n\tbits 15&14 of general configuration word 0 both set to 1.\n"*/
  634. bb_error_msg_and_die("Unknown device type");
  635. if_printf(!(val[GEN_CONFIG] & MEDIA_REMOVABLE),"non-");
  636. printf("removable media\n");
  637. /* Info from the specific configuration word says whether or not the
  638. * ID command completed correctly. It is only defined, however in
  639. * ATA/ATAPI-5 & 6; it is reserved (value theoretically 0) in prior
  640. * standards. Since the values allowed for this word are extremely
  641. * specific, it should be safe to check it now, even though we don't
  642. * know yet what standard this device is using.
  643. */
  644. if((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL) ||
  645. (val[CONFIG]==PWRD_NID_VAL) || (val[CONFIG]==PWRD_ID_VAL) )
  646. {
  647. like_std = 5;
  648. if_printf(((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==STBY_ID_VAL)),
  649. "powers-up in standby; SET FEATURES subcmd spins-up.\n");
  650. if_printf((((val[CONFIG]==STBY_NID_VAL) || (val[CONFIG]==PWRD_NID_VAL)) && (val[GEN_CONFIG] & INCOMPLETE)),
  651. "\n\tWARNING: ID response incomplete.\n\tFollowing data may be incorrect.\n\n");
  652. }
  653. /* output the model and serial numbers and the fw revision */
  654. xprint_ascii(val, START_MODEL, "Model Number:", LENGTH_MODEL);
  655. xprint_ascii(val, START_SERIAL, "Serial Number:", LENGTH_SERIAL);
  656. xprint_ascii(val, START_FW_REV, "Firmware Revision:", LENGTH_FW_REV);
  657. xprint_ascii(val, START_MEDIA, "Media Serial Num:", LENGTH_MEDIA);
  658. xprint_ascii(val, START_MANUF, "Media Manufacturer:", LENGTH_MANUF);
  659. /* major & minor standards version number (Note: these words were not
  660. * defined until ATA-3 & the CDROM std uses different words.) */
  661. printf("Standards:");
  662. if(eqpt != CDROM)
  663. {
  664. if(val[MINOR] && (val[MINOR] <= MINOR_MAX))
  665. {
  666. like_std=check_if_min_and_set_val(like_std, 3);
  667. std = actual_ver[val[MINOR]];
  668. if_printf(std,"\n\tUsed: %s ",minor_str[val[MINOR]]);
  669. }
  670. /* looks like when they up-issue the std, they obsolete one;
  671. * thus, only the newest 4 issues need be supported. (That's
  672. * what "kk" and "min_std" are all about.) */
  673. if(val[MAJOR] && (val[MAJOR] !=NOVAL_1))
  674. {
  675. printf("\n\tSupported: ");
  676. jj = val[MAJOR] << 1;
  677. kk = like_std >4 ? like_std-4: 0;
  678. for(ii = 14; (ii >0)&&(ii>kk); ii--)
  679. {
  680. if(jj & 0x8000)
  681. {
  682. printf("%u ", ii);
  683. if(like_std < ii)
  684. {
  685. like_std = ii;
  686. kk = like_std >4 ? like_std-4: 0;
  687. }
  688. min_std=check_if_maj_and_set_val(min_std, ii);
  689. }
  690. jj <<= 1;
  691. }
  692. like_std=check_if_min_and_set_val(like_std, 3);
  693. }
  694. /* Figure out what standard the device is using if it hasn't told
  695. * us. If we know the std, check if the device is using any of
  696. * the words from the next level up. It happens.
  697. */
  698. like_std=check_if_min_and_set_val(like_std, std);
  699. if(((std == 5) || (!std && (like_std < 6))) &&
  700. ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  701. (( val[CMDS_SUPP_1] & CMDS_W83) > 0x00ff)) ||
  702. ((( val[CMDS_SUPP_2] & VALID) == VALID_VAL) &&
  703. ( val[CMDS_SUPP_2] & CMDS_W84) ) ) )
  704. {
  705. like_std = 6;
  706. }
  707. else if(((std == 4) || (!std && (like_std < 5))) &&
  708. ((((val[INTEGRITY] & SIG) == SIG_VAL) && !chksum) ||
  709. (( val[HWRST_RSLT] & VALID) == VALID_VAL) ||
  710. ((( val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  711. (( val[CMDS_SUPP_1] & CMDS_W83) > 0x001f)) ) )
  712. {
  713. like_std = 5;
  714. }
  715. else if(((std == 3) || (!std && (like_std < 4))) &&
  716. ((((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  717. ((( val[CMDS_SUPP_1] & CMDS_W83) > 0x0000) ||
  718. (( val[CMDS_SUPP_0] & CMDS_W82) > 0x000f))) ||
  719. (( val[CAPAB_1] & VALID) == VALID_VAL) ||
  720. (( val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA]) ||
  721. (( val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP) ) )
  722. {
  723. like_std = 4;
  724. }
  725. else if(((std == 2) || (!std && (like_std < 3))) &&
  726. ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) )
  727. {
  728. like_std = 3;
  729. }
  730. else if(((std == 1) || (!std && (like_std < 2))) &&
  731. ((val[CAPAB_0] & (IORDY_SUP | IORDY_OFF)) ||
  732. (val[WHATS_VALID] & OK_W64_70)) )
  733. {
  734. like_std = 2;
  735. }
  736. if(!std)
  737. printf("\n\tLikely used: %u\n",like_std);
  738. else if(like_std > std)
  739. printf("& some of %u\n",like_std);
  740. else
  741. printf("\n");
  742. }
  743. else
  744. {
  745. /* TBD: do CDROM stuff more thoroughly. For now... */
  746. kk = 0;
  747. if(val[CDR_MINOR] == 9)
  748. {
  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. {
  754. kk = 1;
  755. printf("\n\tSupported: CD-ROM ATAPI");
  756. jj = val[CDR_MAJOR] >> 1;
  757. for(ii = 1; ii <15; ii++)
  758. {
  759. if_printf((jj & 0x0001),"-%u ", ii);
  760. jj >>= 1;
  761. }
  762. }
  763. if_else_printf((!kk),"\n\tLikely used CD-ROM ATAPI-1\n","\n");
  764. /* the cdrom stuff is more like ATA-2 than anything else, so: */
  765. like_std = 2;
  766. }
  767. if(min_std == 0xffff)
  768. min_std = like_std > 4 ? like_std - 3 : 1;
  769. printf("Configuration:\n");
  770. /* more info from the general configuration word */
  771. if((eqpt != CDROM) && (like_std == 1))
  772. {
  773. jj = val[GEN_CONFIG] >> 1;
  774. for(ii = 1; ii < 15; ii++)
  775. {
  776. if_printf((jj & 0x0001),"\t%s\n",ata1_cfg_str[ii]);
  777. jj >>=1;
  778. }
  779. }
  780. if(dev == ATAPI_DEV)
  781. {
  782. printf("\tDRQ response: "); /* Data Request (DRQ) */
  783. switch(val[GEN_CONFIG] & DRQ_RESPONSE_TIME)
  784. {
  785. case DRQ_3MS_VAL :
  786. printf("3ms.\n");
  787. break;
  788. case DRQ_INTR_VAL :
  789. printf("<=10ms with INTRQ\n");
  790. break;
  791. case DRQ_50US_VAL :
  792. printf("50us.\n");
  793. break;
  794. default :
  795. printf("unknown.\n");
  796. break;
  797. }
  798. printf("\tPacket size: ");
  799. switch(val[GEN_CONFIG] & PKT_SIZE_SUPPORTED)
  800. {
  801. case PKT_SIZE_12_VAL :
  802. printf("12 bytes\n");
  803. break;
  804. case PKT_SIZE_16_VAL :
  805. printf("16 bytes\n");
  806. break;
  807. default :
  808. printf("Unknown\n");
  809. break;
  810. }
  811. }
  812. else
  813. {
  814. /* addressing...CHS? See section 6.2 of ATA specs 4 or 5 */
  815. ll = (uint32_t)val[LBA_SECTS_MSB] << 16 | val[LBA_SECTS_LSB];
  816. mm = 0; bbbig = 0;
  817. if ( (ll > 0x00FBFC10) && (!val[LCYLS]))
  818. printf("\tCHS addressing not supported\n");
  819. else
  820. {
  821. jj = val[WHATS_VALID] & OK_W54_58;
  822. printf("\tLogical\t\tmax\tcurrent\n\tcylinders\t%u\t%u\n\theads\t\t%u\t%u\n\tsectors/track\t%u\t%u\n\t--\n",
  823. val[LCYLS],jj?val[LCYLS_CUR]:0, val[LHEADS],jj?val[LHEADS_CUR]:0, val[LSECTS],jj?val[LSECTS_CUR]:0);
  824. if_printf(((min_std == 1) && (val[TRACK_BYTES] || val[SECT_BYTES])),
  825. "\tbytes/track: %u\tbytes/sector: %u\n",val[TRACK_BYTES], val[SECT_BYTES]);
  826. if(jj)
  827. {
  828. mm = (uint32_t)val[CAPACITY_MSB] << 16 | val[CAPACITY_LSB];
  829. if(like_std < 3)
  830. {
  831. /* check Endian of capacity bytes */
  832. nn = val[LCYLS_CUR] * val[LHEADS_CUR] * val[LSECTS_CUR];
  833. oo = (uint32_t)val[CAPACITY_LSB] << 16 | val[CAPACITY_MSB];
  834. if(abs(mm - nn) > abs(oo - nn))
  835. mm = oo;
  836. }
  837. printf("\tCHS current addressable sectors:%11u\n",mm);
  838. }
  839. }
  840. /* LBA addressing */
  841. printf("\tLBA user addressable sectors:%11u\n",ll);
  842. if( ((val[CMDS_SUPP_1] & VALID) == VALID_VAL) &&
  843. (val[CMDS_SUPP_1] & SUPPORT_48_BIT) )
  844. {
  845. bbbig = (__u64)val[LBA_64_MSB] << 48 |
  846. (__u64)val[LBA_48_MSB] << 32 |
  847. (__u64)val[LBA_MID] << 16 |
  848. val[LBA_LSB] ;
  849. printf("\tLBA48 user addressable sectors:%11llu\n",bbbig);
  850. }
  851. if (!bbbig)
  852. bbbig = (__u64)(ll>mm ? ll : mm); /* # 512 byte blocks */
  853. printf("\tdevice size with M = 1024*1024: %11llu MBytes\n",bbbig>>11);
  854. bbbig = (bbbig<<9)/1000000;
  855. printf("\tdevice size with M = 1000*1000: %11llu MBytes ",bbbig);
  856. if_else_printf((bbbig > 1000),"(%llu GB)\n","\n",bbbig/1000);
  857. }
  858. /* hw support of commands (capabilities) */
  859. printf("Capabilities:\n\t");
  860. if(dev == ATAPI_DEV)
  861. {
  862. if(eqpt != CDROM)
  863. if_printf((val[CAPAB_0] & CMD_Q_SUP),"Cmd queuing, ");
  864. if_printf((val[CAPAB_0] & OVLP_SUP),"Cmd overlap, ");
  865. }
  866. if_printf((val[CAPAB_0] & LBA_SUP),"LBA, ");
  867. if(like_std != 1)
  868. {
  869. printf("IORDY");
  870. if_printf((!(val[CAPAB_0] & IORDY_SUP)),"(may be)");
  871. if_else_printf((val[CAPAB_0] & IORDY_OFF),"(can","(cannot");
  872. printf(" be disabled)\n");
  873. }
  874. else
  875. printf("no IORDY\n");
  876. if((like_std == 1) && val[BUF_TYPE])
  877. {
  878. kk = val[BUF_TYPE];
  879. printf("\tBuffer type: %04x: ",kk);
  880. if_else_printf((kk < 2),"single port, single-sector","dual port, multi-sector");
  881. if_printf((kk > 2)," with read caching ability");
  882. printf("\n");
  883. }
  884. jj = 0;
  885. if((min_std == 1) && (val[BUFFER__SIZE] && (val[BUFFER__SIZE] != NOVAL_1)))
  886. {
  887. printf("\tBuffer size: %.1fkB",(float)val[BUFFER__SIZE]/2);
  888. jj = 1;
  889. }
  890. if((min_std < 4) && (val[RW_LONG]))
  891. {
  892. printf("\tbytes avail on r/w long: %u",val[RW_LONG]);
  893. jj = 1;
  894. }
  895. if((eqpt != CDROM) && (like_std > 3))
  896. {
  897. printf("\tQueue depth: %u",(val[QUEUE_DEPTH] & DEPTH_BITS)+1);
  898. jj = 1;
  899. }
  900. if_printf(jj,"\n");
  901. if(dev == ATA_DEV)
  902. {
  903. if(like_std == 1)
  904. printf("\tCan%s perform double-word IO\n",(!val[DWORD_IO]) ?"not":"");
  905. else
  906. {
  907. printf("\tStandby timer values: spec'd by ");
  908. if_else_printf((val[CAPAB_0] & STD_STBY),"Standard","Vendor");
  909. if((like_std > 3) && ((val[CAPAB_1] & VALID) == VALID_VAL))
  910. printf(", %s device specific minimum\n",(val[CAPAB_1] & MIN_STANDBY_TIMER)?"with":"no");
  911. else
  912. printf("\n");
  913. }
  914. printf("\tR/W multiple sector transfer: ");
  915. if((like_std < 3) && !(val[SECTOR_XFER_MAX] & SECTOR_XFER))
  916. printf("not supported\n");
  917. else
  918. {
  919. printf("Max = %u\tCurrent = ",val[SECTOR_XFER_MAX] & SECTOR_XFER);
  920. if_else_printf((val[SECTOR_XFER_CUR] & MULTIPLE_SETTING_VALID),
  921. "%u\n","?\n",val[SECTOR_XFER_CUR] & SECTOR_XFER);
  922. }
  923. if((like_std > 3) && (val[CMDS_SUPP_1] & 0x0008))
  924. {
  925. /* We print out elsewhere whether the APM feature is enabled or
  926. not. If it's not enabled, let's not repeat the info; just print
  927. nothing here. */
  928. printf("\tAdvancedPM level: ");
  929. if ( (val[ADV_PWR] & 0xFF00) == 0x4000 )
  930. {
  931. uint8_t apm_level = val[ADV_PWR] & 0x00FF;
  932. printf("%u (0x%x)\n", apm_level, apm_level);
  933. }
  934. else
  935. printf("unknown setting (0x%04x)\n", val[ADV_PWR]);
  936. }
  937. if(like_std > 5)
  938. {
  939. if_printf(val[ACOUSTIC],"\tRecommended acoustic management value: %u, current value: %u\n",
  940. (val[ACOUSTIC] >> 8) & 0x00ff, val[ACOUSTIC] & 0x00ff);
  941. }
  942. }
  943. else
  944. {
  945. /* ATAPI */
  946. if(eqpt != CDROM)
  947. if_printf((val[CAPAB_0] & SWRST_REQ),"\tATA sw reset required\n");
  948. if(val[PKT_REL] || val[SVC_NBSY])
  949. {
  950. printf("\tOverlap support:");
  951. if_printf(val[PKT_REL]," %uus to release bus.",val[PKT_REL]);
  952. if_printf(val[SVC_NBSY]," %uus to clear BSY after SERVICE cmd.",val[SVC_NBSY]);
  953. printf("\n");
  954. }
  955. }
  956. /* DMA stuff. Check that only one DMA mode is selected. */
  957. printf("\tDMA: ");
  958. if(!(val[CAPAB_0] & DMA_SUP))
  959. printf("not supported\n");
  960. else
  961. {
  962. if_printf((val[DMA_MODE] && !val[SINGLE_DMA] && !val[MULTI_DMA]),
  963. " sdma%u\n",(val[DMA_MODE] & MODE) >> 8);
  964. if(val[SINGLE_DMA])
  965. {
  966. jj = val[SINGLE_DMA];
  967. kk = val[SINGLE_DMA] >> 8;
  968. err_dma += mode_loop(jj,kk,'s',&have_mode);
  969. }
  970. if(val[MULTI_DMA])
  971. {
  972. jj = val[MULTI_DMA];
  973. kk = val[MULTI_DMA] >> 8;
  974. err_dma += mode_loop(jj,kk,'m',&have_mode);
  975. }
  976. if((val[WHATS_VALID] & OK_W88) && val[ULTRA_DMA])
  977. {
  978. jj = val[ULTRA_DMA];
  979. kk = val[ULTRA_DMA] >> 8;
  980. err_dma += mode_loop(jj,kk,'u',&have_mode);
  981. }
  982. if_printf((err_dma || !have_mode),"(?)");
  983. printf("\n");
  984. if_printf(((dev == ATAPI_DEV) && (eqpt != CDROM) && (val[CAPAB_0] & DMA_IL_SUP)),
  985. "\t Interleaved DMA support\n");
  986. if((val[WHATS_VALID] & OK_W64_70) &&
  987. (val[DMA_TIME_MIN] || val[DMA_TIME_NORM]))
  988. {
  989. printf("\t Cycle time:");
  990. if_printf(val[DMA_TIME_MIN]," min=%uns",val[DMA_TIME_MIN]);
  991. if_printf(val[DMA_TIME_NORM]," recommended=%uns",val[DMA_TIME_NORM]);
  992. printf("\n");
  993. }
  994. }
  995. /* Programmed IO stuff */
  996. printf("\tPIO: ");
  997. /* If a drive supports mode n (e.g. 3), it also supports all modes less
  998. * than n (e.g. 3, 2, 1 and 0). Print all the modes. */
  999. if((val[WHATS_VALID] & OK_W64_70) && (val[ADV_PIO_MODES] & PIO_SUP))
  1000. {
  1001. jj = ((val[ADV_PIO_MODES] & PIO_SUP) << 3) | 0x0007;
  1002. for(ii = 0; ii <= PIO_MODE_MAX ; ii++)
  1003. {
  1004. if_printf((jj & 0x0001),"pio%d ",ii);
  1005. jj >>=1;
  1006. }
  1007. printf("\n");
  1008. }
  1009. else if(((min_std < 5) || (eqpt == CDROM)) && (val[PIO_MODE] & MODE) )
  1010. {
  1011. for(ii = 0; ii <= val[PIO_MODE]>>8; ii++)
  1012. printf("pio%d ",ii);
  1013. printf("\n");
  1014. }
  1015. else
  1016. printf("unknown\n");
  1017. if(val[WHATS_VALID] & OK_W64_70)
  1018. {
  1019. if(val[PIO_NO_FLOW] || val[PIO_FLOW])
  1020. {
  1021. printf("\t Cycle time:");
  1022. if_printf(val[PIO_NO_FLOW]," no flow control=%uns", val[PIO_NO_FLOW]);
  1023. if_printf(val[PIO_FLOW]," IORDY flow control=%uns", val[PIO_FLOW]);
  1024. printf("\n");
  1025. }
  1026. }
  1027. if((val[CMDS_SUPP_1] & VALID) == VALID_VAL)
  1028. {
  1029. printf("Commands/features:\n\tEnabled\tSupported:\n");
  1030. jj = val[CMDS_SUPP_0];
  1031. kk = val[CMDS_EN_0];
  1032. for(ii = 0; ii < NUM_CMD_FEAT_STR; ii++)
  1033. {
  1034. if((jj & 0x8000) && (*cmd_feat_str[ii] != '\0'))
  1035. {
  1036. if_else_printf((kk & 0x8000),"\t *","\t");
  1037. printf("\t%s\n",cmd_feat_str[ii]);
  1038. }
  1039. jj <<=1; kk<<=1;
  1040. if(ii%16 == 15)
  1041. {
  1042. jj = val[CMDS_SUPP_0+1+(ii/16)];
  1043. kk = val[CMDS_EN_0+1+(ii/16)];
  1044. }
  1045. if(ii == 31)
  1046. {
  1047. if((val[CMDS_SUPP_2] & VALID) != VALID_VAL)
  1048. ii +=16;
  1049. }
  1050. }
  1051. }
  1052. if_printf(((val[RM_STAT] & RM_STAT_BITS) == RM_STAT_SUP),
  1053. "\tRemovable Media Status Notification feature set supported\n");
  1054. /* security */
  1055. if((eqpt != CDROM) && (like_std > 3) &&
  1056. (val[SECU_STATUS] || val[ERASE_TIME] || val[ENH_ERASE_TIME]))
  1057. {
  1058. printf("Security: \n");
  1059. if_printf((val[PSWD_CODE] && (val[PSWD_CODE] != NOVAL_1)),
  1060. "\tMaster password revision code = %u\n",val[PSWD_CODE]);
  1061. jj = val[SECU_STATUS];
  1062. if(jj)
  1063. {
  1064. for(ii = 0; ii < NUM_SECU_STR; ii++)
  1065. {
  1066. if_else_printf((!(jj & 0x0001)),"\tnot\t%s\n", "\t\t%s\n", secu_str[ii]);
  1067. jj >>=1;
  1068. }
  1069. if(val[SECU_STATUS] & SECU_ENABLED)
  1070. {
  1071. printf("\tSecurity level ");
  1072. if_else_printf((val[SECU_STATUS] & SECU_LEVEL),"maximum\n","high\n");
  1073. }
  1074. }
  1075. jj = val[ERASE_TIME] & ERASE_BITS;
  1076. kk = val[ENH_ERASE_TIME] & ERASE_BITS;
  1077. if(jj || kk)
  1078. {
  1079. printf("\t");
  1080. if_printf(jj,"%umin for SECURITY ERASE UNIT. ", jj==ERASE_BITS ? 508 : jj<<1);
  1081. if_printf(kk,"%umin for ENHANCED SECURITY ERASE UNIT.", kk==ERASE_BITS ? 508 : kk<<1);
  1082. printf("\n");
  1083. }
  1084. }
  1085. /* reset result */
  1086. if((val[HWRST_RSLT] & VALID) == VALID_VAL)
  1087. {
  1088. printf("HW reset results:\n");
  1089. if_else_printf((val[HWRST_RSLT] & CBLID),"\tCBLID- above Vih\n","\tCBLID- below Vih\n");
  1090. if(val[HWRST_RSLT] & RST0)
  1091. {
  1092. printf("\tDevice num = 0");
  1093. jj = val[HWRST_RSLT];
  1094. }
  1095. else
  1096. {
  1097. printf("\tDevice num = 1");
  1098. jj = val[HWRST_RSLT] >> 8;
  1099. }
  1100. if((jj & DEV_DET) == JUMPER_VAL)
  1101. printf(" determined by the jumper");
  1102. else if((jj & DEV_DET) == CSEL_VAL)
  1103. printf(" determined by CSEL");
  1104. printf("\n");
  1105. }
  1106. /* more stuff from std 5 */
  1107. if((like_std > 4) && (eqpt != CDROM))
  1108. {
  1109. if(val[CFA_PWR_MODE] & VALID_W160)
  1110. {
  1111. printf("CFA power mode 1:\n\t");
  1112. if_else_printf((val[CFA_PWR_MODE] & PWR_MODE_OFF),"disabled","enabled");
  1113. if_printf((val[CFA_PWR_MODE] & PWR_MODE_REQ)," and required by some commands");
  1114. printf("\n");
  1115. if_printf((val[CFA_PWR_MODE] & MAX_AMPS),"\tMaximum current = %uma\n",val[CFA_PWR_MODE] & MAX_AMPS);
  1116. }
  1117. if((val[INTEGRITY] & SIG) == SIG_VAL)
  1118. {
  1119. printf("Checksum: ");
  1120. if_printf(chksum,"in");
  1121. printf("correct\n");
  1122. }
  1123. }
  1124. exit(0);
  1125. }
  1126. #endif
  1127. static int verbose = 0, get_identity = 0, get_geom = 0, noisy = 1, quiet = 0;
  1128. static int flagcount = 0, do_flush = 0, is_scsi_hd = 0, is_xt_hd = 0;
  1129. static int do_ctimings, do_timings = 0;
  1130. static unsigned long set_readahead= 0, get_readahead= 0, Xreadahead= 0;
  1131. static unsigned long set_readonly = 0, get_readonly = 0, readonly = 0;
  1132. static unsigned long set_unmask = 0, get_unmask = 0, unmask = 0;
  1133. static unsigned long set_mult = 0, get_mult = 0, mult = 0;
  1134. #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
  1135. static unsigned long set_dma = 0, get_dma = 0, dma = 0;
  1136. #endif
  1137. static unsigned long set_dma_q = 0, get_dma_q = 0, dma_q = 0;
  1138. static unsigned long set_nowerr = 0, get_nowerr = 0, nowerr = 0;
  1139. static unsigned long set_keep = 0, get_keep = 0, keep = 0;
  1140. static unsigned long set_io32bit = 0, get_io32bit = 0, io32bit = 0;
  1141. static unsigned long set_piomode = 0, noisy_piomode= 0;
  1142. static int piomode = 0;
  1143. #ifdef HDIO_DRIVE_CMD
  1144. static unsigned long set_dkeep = 0, get_dkeep = 0, dkeep = 0;
  1145. static unsigned long set_standby = 0, get_standby = 0, standby_requested= 0;
  1146. static unsigned long set_xfermode = 0, get_xfermode = 0;
  1147. static int xfermode_requested= 0;
  1148. static unsigned long set_lookahead= 0, get_lookahead= 0, lookahead= 0;
  1149. static unsigned long set_prefetch = 0, get_prefetch = 0, prefetch = 0;
  1150. static unsigned long set_defects = 0, get_defects = 0, defects = 0;
  1151. static unsigned long set_wcache = 0, get_wcache = 0, wcache = 0;
  1152. static unsigned long set_doorlock = 0, get_doorlock = 0, doorlock = 0;
  1153. static unsigned long set_seagate = 0, get_seagate = 0;
  1154. static unsigned long set_standbynow = 0, get_standbynow = 0;
  1155. static unsigned long set_sleepnow = 0, get_sleepnow = 0;
  1156. static unsigned long get_powermode = 0;
  1157. static unsigned long set_apmmode = 0, get_apmmode= 0, apmmode = 0;
  1158. #endif
  1159. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  1160. static int get_IDentity = 0;
  1161. #endif
  1162. #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
  1163. static int unregister_hwif = 0;
  1164. static int hwif = 0;
  1165. #endif
  1166. #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
  1167. static int scan_hwif = 0;
  1168. static int hwif_data = 0;
  1169. static int hwif_ctrl = 0;
  1170. static int hwif_irq = 0;
  1171. #endif
  1172. #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1173. static int set_busstate = 0, get_busstate = 0, busstate = 0;
  1174. #endif
  1175. static int reread_partn = 0;
  1176. #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
  1177. static int perform_reset = 0;
  1178. #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
  1179. #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1180. static int perform_tristate = 0, tristate = 0;
  1181. #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
  1182. // Historically, if there was no HDIO_OBSOLETE_IDENTITY, then
  1183. // then the HDIO_GET_IDENTITY only returned 142 bytes.
  1184. // Otherwise, HDIO_OBSOLETE_IDENTITY returns 142 bytes,
  1185. // and HDIO_GET_IDENTITY returns 512 bytes. But the latest
  1186. // 2.5.xx kernels no longer define HDIO_OBSOLETE_IDENTITY
  1187. // (which they should, but they should just return -EINVAL).
  1188. //
  1189. // So.. we must now assume that HDIO_GET_IDENTITY returns 512 bytes.
  1190. // On a really old system, it will not, and we will be confused.
  1191. // Too bad, really.
  1192. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  1193. static const char *cfg_str[] =
  1194. { "", " HardSect", " SoftSect", " NotMFM",
  1195. " HdSw>15uSec", " SpinMotCtl", " Fixed", " Removeable",
  1196. " DTR<=5Mbs", " DTR>5Mbs", " DTR>10Mbs", " RotSpdTol>.5%",
  1197. " dStbOff", " TrkOff", " FmtGapReq", " nonMagnetic"
  1198. };
  1199. static const char *BuffType[] = {"unknown", "1Sect", "DualPort", "DualPortCache"};
  1200. static void dump_identity (const struct hd_driveid *id)
  1201. {
  1202. int i;
  1203. char pmodes[64] = {0,}, dmodes[128]={0,}, umodes[128]={0,};
  1204. const unsigned short int *id_regs= (const void*) id;
  1205. unsigned long capacity;
  1206. printf("\n Model=%.40s, FwRev=%.8s, SerialNo=%.20s\n Config={",
  1207. id->model, id->fw_rev, id->serial_no);
  1208. for (i=0; i<=15; i++)
  1209. if_printf((id->config & (1<<i)),"%s", cfg_str[i]);
  1210. printf(" }\n RawCHS=%u/%u/%u, TrkSize=%u, SectSize=%u, ECCbytes=%u\n",
  1211. id->cyls, id->heads, id->sectors, id->track_bytes,
  1212. id->sector_bytes, id->ecc_bytes);
  1213. if (id->buf_type > 3)
  1214. printf("%s%u", " BuffType=", id->buf_type);
  1215. else
  1216. printf("%s%s", " BuffType=", BuffType[id->buf_type]);
  1217. printf(", BuffSize=%ukB, MaxMultSect=%u", id->buf_size/2, id->max_multsect);
  1218. if (id->max_multsect)
  1219. {
  1220. printf(", MultSect=");
  1221. if (!(id->multsect_valid&1))
  1222. printf("?%u?", id->multsect);
  1223. else if (id->multsect)
  1224. printf("%u", id->multsect);
  1225. else
  1226. printf("off");
  1227. }
  1228. printf("\n");
  1229. if (id->tPIO <= 5)
  1230. {
  1231. strcat(pmodes, "pio0 ");
  1232. if_strcat((id->tPIO >= 1), pmodes, "pio1 ");
  1233. if_strcat((id->tPIO >= 2), pmodes, "pio2 ");
  1234. }
  1235. if_printf((!(id->field_valid&1))," (maybe):");
  1236. #if __BYTE_ORDER == __BIG_ENDIAN
  1237. capacity = (id->cur_capacity0 << 16) | id->cur_capacity1;
  1238. #else
  1239. capacity = (id->cur_capacity1 << 16) | id->cur_capacity0;
  1240. #endif
  1241. printf(" CurCHS=%u/%u/%u, CurSects=%lu, LBA=%s",id->cur_cyls, id->cur_heads,
  1242. id->cur_sectors, capacity ,
  1243. ((id->capability&2)==0)?"no":"yes");
  1244. if_printf((id->capability&2),", LBAsects=%u", id->lba_capacity);
  1245. if (id->capability&1)
  1246. {
  1247. if (id->dma_1word | id->dma_mword)
  1248. {
  1249. if_strcat((id->dma_1word & 0x100), dmodes, "*");
  1250. if_strcat((id->dma_1word & 1), dmodes, "sdma0 ");
  1251. if_strcat((id->dma_1word & 0x200), dmodes, "*");
  1252. if_strcat((id->dma_1word & 2), dmodes, "sdma1 ");
  1253. if_strcat((id->dma_1word & 0x400), dmodes, "*");
  1254. if_strcat((id->dma_1word & 4), dmodes, "sdma2 ");
  1255. if_strcat((id->dma_1word & 0xf800), dmodes, "*");
  1256. if_strcat((id->dma_1word & 0xf8), dmodes, "sdma? ");
  1257. if_strcat((id->dma_mword & 0x100), dmodes, "*");
  1258. if_strcat((id->dma_mword & 1), dmodes, "mdma0 ");
  1259. if_strcat((id->dma_mword & 0x200), dmodes, "*");
  1260. if_strcat((id->dma_mword & 2), dmodes, "mdma1 ");
  1261. if_strcat((id->dma_mword & 0x400), dmodes, "*");
  1262. if_strcat((id->dma_mword & 4), dmodes, "mdma2 ");
  1263. if_strcat((id->dma_mword & 0xf800), dmodes, "*");
  1264. if_strcat((id->dma_mword & 0xf8), dmodes, "mdma? ");
  1265. }
  1266. }
  1267. printf("\n IORDY=");
  1268. if (id->capability&8)
  1269. printf((id->capability&4) ? "on/off" : "yes");
  1270. else
  1271. printf("no");
  1272. if ((id->capability&8) || (id->field_valid&2))
  1273. {
  1274. if (id->field_valid&2)
  1275. {
  1276. printf(", tPIO={min:%u,w/IORDY:%u}", id->eide_pio, id->eide_pio_iordy);
  1277. if_strcat((id->eide_pio_modes & 1), pmodes, "pio3 ");
  1278. if_strcat((id->eide_pio_modes & 2), pmodes, "pio4 ");
  1279. if_strcat((id->eide_pio_modes &~3), pmodes, "pio? ");
  1280. }
  1281. if (id->field_valid&4)
  1282. {
  1283. if_strcat((id->dma_ultra & 0x100),umodes,"*");
  1284. if_strcat((id->dma_ultra & 0x001),umodes,"udma0 ");
  1285. if_strcat((id->dma_ultra & 0x200),umodes,"*");
  1286. if_strcat((id->dma_ultra & 0x002),umodes,"udma1 ");
  1287. if_strcat((id->dma_ultra & 0x400),umodes,"*");
  1288. if_strcat((id->dma_ultra & 0x004),umodes,"udma2 ");
  1289. #ifdef __NEW_HD_DRIVE_ID
  1290. if (id->hw_config & 0x2000)
  1291. {
  1292. #else /* !__NEW_HD_DRIVE_ID */
  1293. if (id->word93 & 0x2000)
  1294. {
  1295. #endif /* __NEW_HD_DRIVE_ID */
  1296. if_strcat((id->dma_ultra & 0x0800),umodes,"*");
  1297. if_strcat((id->dma_ultra & 0x0008),umodes,"udma3 ");
  1298. if_strcat((id->dma_ultra & 0x1000),umodes,"*");
  1299. if_strcat((id->dma_ultra & 0x0010),umodes,"udma4 ");
  1300. if_strcat((id->dma_ultra & 0x2000),umodes,"*");
  1301. if_strcat((id->dma_ultra & 0x0020),umodes,"udma5 ");
  1302. if_strcat((id->dma_ultra & 0x4000),umodes,"*");
  1303. if_strcat((id->dma_ultra & 0x0040),umodes,"udma6 ");
  1304. if_strcat((id->dma_ultra & 0x8000),umodes,"*");
  1305. if_strcat((id->dma_ultra & 0x0080),umodes,"udma7 ");
  1306. }
  1307. }
  1308. }
  1309. if_printf(((id->capability&1) && (id->field_valid&2)),
  1310. ", tDMA={min:%u,rec:%u}", id->eide_dma_min, id->eide_dma_time);
  1311. printf("\n PIO modes: %s", pmodes);
  1312. if_printf((*dmodes),"\n DMA modes: %s", dmodes);
  1313. if_printf((*umodes),"\n UDMA modes: %s", umodes);
  1314. printf("\n AdvancedPM=%s",((id_regs[83]&8)==0)?"no":"yes");
  1315. if (id_regs[83] & 8)
  1316. {
  1317. if (!(id_regs[86]&8))
  1318. printf(": disabled (255)");
  1319. else if ((id_regs[91]&0xFF00)!=0x4000)
  1320. printf(": unknown setting");
  1321. else
  1322. printf(": mode=0x%02X (%u)",id_regs[91]&0xFF,id_regs[91]&0xFF);
  1323. }
  1324. if_printf( (id_regs[82]&0x20)," WriteCache=%s",(id_regs[85]&0x20) ? "enabled" : "disabled");
  1325. #ifdef __NEW_HD_DRIVE_ID
  1326. if ((id->minor_rev_num && id->minor_rev_num <= 31) || (id->major_rev_num && id->minor_rev_num <= 31))
  1327. {
  1328. printf("\n Drive conforms to: ");
  1329. if_else_printf((id->minor_rev_num <= 31),"%s: ","unknown: ", minor_str[id->minor_rev_num]);
  1330. if (id->major_rev_num < 31)
  1331. {
  1332. for (i=0; i <= 15; i++)
  1333. if_printf((id->major_rev_num & (1<<i))," %u", i);
  1334. }
  1335. }
  1336. #endif /* __NEW_HD_DRIVE_ID */
  1337. printf("\n\n * signifies the current active mode\n\n");
  1338. }
  1339. #endif
  1340. static void flush_buffer_cache (int fd)
  1341. {
  1342. fsync (fd); /* flush buffers */
  1343. bb_ioctl(fd, BLKFLSBUF, NULL,"BLKFLSBUF" ) ;/* do it again, big time */
  1344. #ifdef HDIO_DRIVE_CMD
  1345. sleep(1);
  1346. if (ioctl(fd, HDIO_DRIVE_CMD, NULL) && errno != EINVAL) /* await completion */
  1347. bb_perror_msg("HDIO_DRIVE_CMD");
  1348. #endif
  1349. }
  1350. static int seek_to_zero (int fd)
  1351. {
  1352. if (lseek(fd, (off_t) 0, SEEK_SET))
  1353. return 1;
  1354. return 0;
  1355. }
  1356. static int read_big_block (int fd, char *buf)
  1357. {
  1358. const char *string;
  1359. int i, rc;
  1360. if ((rc = read(fd, buf, TIMING_BUF_BYTES)) != TIMING_BUF_BYTES)
  1361. {
  1362. switch(rc)
  1363. {
  1364. case -1:
  1365. string = "read()";
  1366. break;
  1367. case 0:
  1368. string = "read() hit EOF - device too small";
  1369. break;
  1370. default:
  1371. string = "read(%u) returned %u bytes";
  1372. }
  1373. bb_error_msg(string, TIMING_BUF_BYTES, rc);
  1374. return 1;
  1375. }
  1376. /* access all sectors of buf to ensure the read fully completed */
  1377. for (i = 0; i < TIMING_BUF_BYTES; i += 512)
  1378. buf[i] &= 1;
  1379. return 0;
  1380. }
  1381. static double correction = 0.0;
  1382. static void do_time (int flag, int fd)
  1383. /*
  1384. flag = 0 time_cache
  1385. flag = 1 time_device
  1386. */
  1387. {
  1388. int i;
  1389. char *buf;
  1390. double elapsed;
  1391. struct itimerval e1, e2;
  1392. int shmid;
  1393. int timing_MB = TIMING_MB;
  1394. if ((shmid = shmget(IPC_PRIVATE, TIMING_BUF_BYTES, 0600)) == -1)
  1395. {
  1396. bb_error_msg (bb_msg_shared_mem,"allocate"); /*"could not allocate sharedmem buf"*/
  1397. return;
  1398. }
  1399. if (shmctl(shmid, SHM_LOCK, NULL) == -1)
  1400. {
  1401. bb_error_msg (bb_msg_shared_mem,"lock"); /*"could not lock sharedmem buf"*/
  1402. (void) shmctl(shmid, IPC_RMID, NULL);
  1403. return;
  1404. }
  1405. if ((buf = shmat(shmid, (char *) 0, 0)) == (char *) -1)
  1406. {
  1407. bb_error_msg (bb_msg_shared_mem,"attach"); /*"could not attach sharedmem buf"*/
  1408. (void) shmctl(shmid, IPC_RMID, NULL);
  1409. return;
  1410. }
  1411. if (shmctl(shmid, IPC_RMID, NULL) == -1)
  1412. bb_error_msg ("shmctl(,IPC_RMID,)");
  1413. /* Clear out the device request queues & give them time to complete */
  1414. sync_and_sleep(3);
  1415. if(flag == 0) /* Time cache */
  1416. {
  1417. /* Calculate a correction factor for the basic
  1418. * overhead of doing a read() from the buffer cache.
  1419. * To do this, we read the data once to "cache it" and
  1420. * to force full preallocation of our timing buffer,
  1421. * and then we re-read it 10 times while timing it.
  1422. *
  1423. * getitimer() is used rather than gettimeofday() because
  1424. * it is much more consistent (on my machine, at least).
  1425. */
  1426. setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
  1427. if (seek_to_zero (fd))
  1428. return;
  1429. if (read_big_block (fd, buf))
  1430. return;
  1431. printf(" Timing buffer-cache reads: ");
  1432. fflush(stdout);
  1433. /* Clear out the device request queues & give them time to complete */
  1434. sync_and_sleep(1);
  1435. /* Time re-reading from the buffer-cache */
  1436. getitimer(ITIMER_REAL, &e1);
  1437. for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
  1438. {
  1439. if (seek_to_zero (fd))
  1440. goto quit;
  1441. if (read_big_block (fd, buf))
  1442. goto quit;
  1443. }
  1444. getitimer(ITIMER_REAL, &e2);
  1445. correction = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
  1446. /* Now remove the lseek() from the correction factor */
  1447. getitimer(ITIMER_REAL, &e1);
  1448. for (i = (BUFCACHE_FACTOR * TIMING_BUF_COUNT) ; i > 0; --i)
  1449. {
  1450. if (seek_to_zero (fd))
  1451. goto quit;
  1452. }
  1453. getitimer(ITIMER_REAL, &e2);
  1454. correction -= (e1.it_value.tv_sec - e2.it_value.tv_sec)
  1455. + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
  1456. if ((BUFCACHE_FACTOR * timing_MB) >= correction) /* more than 1MB/s */
  1457. printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
  1458. (BUFCACHE_FACTOR * timing_MB), correction,
  1459. (BUFCACHE_FACTOR * timing_MB) / correction);
  1460. else
  1461. printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
  1462. (BUFCACHE_FACTOR * timing_MB), correction,
  1463. (BUFCACHE_FACTOR * timing_MB) / correction * 1024);
  1464. correction /= BUFCACHE_FACTOR;
  1465. flush_buffer_cache(fd);
  1466. sleep(1);
  1467. }
  1468. else /* Time device */
  1469. {
  1470. printf(" Timing buffered disk reads: ");
  1471. fflush(stdout);
  1472. /*
  1473. * getitimer() is used rather than gettimeofday() because
  1474. * it is much more consistent (on my machine, at least).
  1475. */
  1476. setitimer(ITIMER_REAL, &(struct itimerval){{1000,0},{1000,0}}, NULL);
  1477. /* Now do the timings for real */
  1478. getitimer(ITIMER_REAL, &e1);
  1479. for (i = TIMING_BUF_COUNT; i > 0; --i)
  1480. {
  1481. if (read_big_block (fd, buf))
  1482. goto quit;
  1483. }
  1484. getitimer(ITIMER_REAL, &e2);
  1485. elapsed = (e1.it_value.tv_sec - e2.it_value.tv_sec) + ((e1.it_value.tv_usec - e2.it_value.tv_usec) / 1000000.0);
  1486. if (timing_MB >= elapsed) /* more than 1MB/s */
  1487. printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",timing_MB, elapsed, timing_MB / elapsed);
  1488. else
  1489. printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",timing_MB, elapsed, timing_MB / elapsed * 1024);
  1490. /*"Hmm.. suspicious results: probably not enough free memory for a proper test.");*/
  1491. if (elapsed <= (correction * 2))
  1492. bb_error_msg(bb_msg_memory_exhausted);
  1493. #if 0 /* the "estimate" is just plain wrong for many systems.. */
  1494. else if (correction != 0.0) {
  1495. printf(" Estimating raw driver speed: ");
  1496. elapsed -= correction;
  1497. if (timing_MB >= elapsed) /* more than 1MB/s */
  1498. printf("%2d MB in %5.2f seconds =%6.2f MB/sec\n",
  1499. timing_MB, elapsed, timing_MB / elapsed);
  1500. else
  1501. printf("%2d MB in %5.2f seconds =%6.2f kB/sec\n",
  1502. timing_MB, elapsed, timing_MB / elapsed * 1024);
  1503. }
  1504. #endif
  1505. }
  1506. quit:
  1507. if (-1 == shmdt(buf))
  1508. bb_error_msg (bb_msg_shared_mem,"detach"); /*"could not detach sharedmem buf"*/
  1509. }
  1510. static void no_scsi (void)
  1511. {
  1512. /*" operation not supported on SCSI disks"*/
  1513. if (is_scsi_hd)
  1514. bb_error_msg_and_die(bb_msg_op_not_supp,"SCSI");
  1515. }
  1516. static void no_xt (void)
  1517. {
  1518. if (is_xt_hd)
  1519. bb_error_msg_and_die(bb_msg_op_not_supp,"XT");
  1520. }
  1521. static void on_off (unsigned int value)
  1522. {
  1523. printf(value ? " (on)\n" : " (off)\n");
  1524. }
  1525. #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  1526. static void bus_state_value (unsigned int value)
  1527. {
  1528. const char *string;
  1529. switch (value)
  1530. {
  1531. case BUSSTATE_ON:
  1532. string = " (on)\n";
  1533. break;
  1534. case BUSSTATE_OFF:
  1535. string = " (off)\n";
  1536. break;
  1537. case BUSSTATE_TRISTATE:
  1538. string = " (tristate)\n";
  1539. break;
  1540. default:
  1541. string = " (unknown: %d)\n";
  1542. break;
  1543. }
  1544. printf(string, value);
  1545. }
  1546. #endif
  1547. #ifdef HDIO_DRIVE_CMD
  1548. static void interpret_standby (unsigned int standby)
  1549. {
  1550. printf(" (");
  1551. switch(standby)
  1552. {
  1553. case 0:
  1554. printf("off");
  1555. break;
  1556. case 252:
  1557. printf("21 minutes");
  1558. break;
  1559. case 253:
  1560. printf("vendor-specific");
  1561. break;
  1562. case 254:
  1563. printf("?reserved");
  1564. break;
  1565. case 255:
  1566. printf("21 minutes + 15 seconds");
  1567. break;
  1568. default:
  1569. if (standby <= 240)
  1570. {
  1571. unsigned int secs = standby * 5;
  1572. unsigned int mins = secs / 60;
  1573. secs %= 60;
  1574. if_printf(mins,"%u minutes", mins);
  1575. if_printf((mins && secs)," + ");
  1576. if_printf(secs,"%u seconds", secs);
  1577. }
  1578. else if (standby <= 251)
  1579. {
  1580. unsigned int mins = (standby - 240) * 30;
  1581. unsigned int hrs = mins / 60;
  1582. mins %= 60;
  1583. if_printf(hrs,"%u hours", hrs);
  1584. if_printf((hrs && mins)," + ");
  1585. if_printf(mins,"%u minutes", mins);
  1586. }
  1587. else
  1588. printf("illegal value");
  1589. break;
  1590. }
  1591. printf(")\n");
  1592. }
  1593. struct xfermode_entry {
  1594. int val;
  1595. const char *name;
  1596. };
  1597. static const struct xfermode_entry xfermode_table[] = {
  1598. { 8, "pio0" },
  1599. { 9, "pio1" },
  1600. { 10, "pio2" },
  1601. { 11, "pio3" },
  1602. { 12, "pio4" },
  1603. { 13, "pio5" },
  1604. { 14, "pio6" },
  1605. { 15, "pio7" },
  1606. { 16, "sdma0" },
  1607. { 17, "sdma1" },
  1608. { 18, "sdma2" },
  1609. { 19, "sdma3" },
  1610. { 20, "sdma4" },
  1611. { 21, "sdma5" },
  1612. { 22, "sdma6" },
  1613. { 23, "sdma7" },
  1614. { 32, "mdma0" },
  1615. { 33, "mdma1" },
  1616. { 34, "mdma2" },
  1617. { 35, "mdma3" },
  1618. { 36, "mdma4" },
  1619. { 37, "mdma5" },
  1620. { 38, "mdma6" },
  1621. { 39, "mdma7" },
  1622. { 64, "udma0" },
  1623. { 65, "udma1" },
  1624. { 66, "udma2" },
  1625. { 67, "udma3" },
  1626. { 68, "udma4" },
  1627. { 69, "udma5" },
  1628. { 70, "udma6" },
  1629. { 71, "udma7" },
  1630. { 0, NULL }
  1631. };
  1632. static int translate_xfermode(char * name)
  1633. {
  1634. const struct xfermode_entry *tmp;
  1635. char *endptr;
  1636. int val = -1;
  1637. for (tmp = xfermode_table; tmp->name != NULL; ++tmp)
  1638. {
  1639. if (!strcmp(name, tmp->name))
  1640. return tmp->val;
  1641. }
  1642. val = strtol(name, &endptr, 10);
  1643. if (*endptr == '\0')
  1644. return val;
  1645. return -1;
  1646. }
  1647. static void interpret_xfermode (unsigned int xfermode)
  1648. {
  1649. printf(" (");
  1650. switch(xfermode) {
  1651. case 0:
  1652. printf("default PIO mode");
  1653. break;
  1654. case 1:
  1655. printf("default PIO mode, disable IORDY");
  1656. break;
  1657. case 8:
  1658. case 9:
  1659. case 10:
  1660. case 11:
  1661. case 12:
  1662. case 13:
  1663. case 14:
  1664. case 15:
  1665. printf("PIO flow control mode%u", xfermode-8);
  1666. break;
  1667. case 16:
  1668. case 17:
  1669. case 18:
  1670. case 19:
  1671. case 20:
  1672. case 21:
  1673. case 22:
  1674. case 23:
  1675. printf("singleword DMA mode%u", xfermode-16);
  1676. break;
  1677. case 32:
  1678. case 33:
  1679. case 34:
  1680. case 35:
  1681. case 36:
  1682. case 37:
  1683. case 38:
  1684. case 39:
  1685. printf("multiword DMA mode%u", xfermode-32);
  1686. break;
  1687. case 64:
  1688. case 65:
  1689. case 66:
  1690. case 67:
  1691. case 68:
  1692. case 69:
  1693. case 70:
  1694. case 71:
  1695. printf("UltraDMA mode%u", xfermode-64);
  1696. break;
  1697. default:
  1698. printf("unknown, probably not valid");
  1699. break;
  1700. }
  1701. printf(")\n");
  1702. }
  1703. #endif /* HDIO_DRIVE_CMD */
  1704. #ifndef VXVM_MAJOR
  1705. #define VXVM_MAJOR 199
  1706. #endif
  1707. #ifndef CCISS_MAJOR
  1708. #define CCISS_MAJOR 104
  1709. #endif
  1710. static void process_dev (char *devname)
  1711. {
  1712. int fd;
  1713. static long parm, multcount;
  1714. struct stat stat_buf;
  1715. #ifndef HDIO_DRIVE_CMD
  1716. int force_operation = 0;
  1717. #endif
  1718. if (stat(devname,&stat_buf))
  1719. bb_perror_msg_and_die(devname);
  1720. switch(major(stat_buf.st_rdev))
  1721. {
  1722. #ifdef SCSI_DISK0_MAJOR
  1723. case (SCSI_DISK0_MAJOR):
  1724. case (SCSI_DISK1_MAJOR):
  1725. case (SCSI_DISK2_MAJOR):
  1726. case (SCSI_DISK3_MAJOR):
  1727. case (SCSI_DISK4_MAJOR):
  1728. case (SCSI_DISK5_MAJOR):
  1729. case (SCSI_DISK6_MAJOR):
  1730. case (SCSI_DISK7_MAJOR):
  1731. #else
  1732. case (SCSI_DISK_MAJOR):
  1733. #endif
  1734. #ifdef MD_MAJOR
  1735. case (MD_MAJOR):
  1736. #endif
  1737. case (VXVM_MAJOR):
  1738. #ifdef LVM_BLK_MAJOR
  1739. case (LVM_BLK_MAJOR):
  1740. #endif
  1741. case (CCISS_MAJOR):
  1742. is_scsi_hd = 1;
  1743. break;
  1744. #ifdef XT_DISK_MAJOR
  1745. case (XT_DISK_MAJOR):
  1746. is_xt_hd = 1;
  1747. break;
  1748. #endif
  1749. case IDE0_MAJOR:
  1750. case IDE1_MAJOR:
  1751. #ifdef IDE2_MAJOR
  1752. case IDE2_MAJOR:
  1753. #endif
  1754. #ifdef IDE3_MAJOR
  1755. case IDE3_MAJOR:
  1756. #endif
  1757. #ifdef IDE4_MAJOR
  1758. case IDE4_MAJOR:
  1759. #endif
  1760. #ifdef IDE5_MAJOR
  1761. case IDE5_MAJOR:
  1762. #endif
  1763. #ifdef IDE6_MAJOR
  1764. case IDE6_MAJOR:
  1765. #endif
  1766. #ifdef IDE7_MAJOR
  1767. case IDE7_MAJOR:
  1768. #endif
  1769. #ifdef IDE8_MAJOR
  1770. case IDE8_MAJOR:
  1771. #endif
  1772. #ifdef IDE9_MAJOR
  1773. case IDE9_MAJOR:
  1774. #endif
  1775. break; /* do nothing */
  1776. default:
  1777. bb_error_msg_and_die("%s not supported",devname);
  1778. }
  1779. fd = bb_xopen (devname, O_RDONLY|O_NONBLOCK);
  1780. if_printf( (!quiet),"\n%s:\n", devname);
  1781. if (set_readahead)
  1782. {
  1783. if_printf(get_readahead," setting fs readahead to %ld\n", Xreadahead);
  1784. bb_ioctl(fd, BLKRASET,(int *)Xreadahead,"BLKRASET");
  1785. }
  1786. #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
  1787. if (unregister_hwif)
  1788. {
  1789. no_scsi();
  1790. printf(" attempting to unregister hwif#%u\n", hwif);
  1791. bb_ioctl(fd, HDIO_UNREGISTER_HWIF,(int *)hwif,"HDIO_UNREGISTER_HWIF");
  1792. }
  1793. #endif
  1794. #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
  1795. if (scan_hwif)
  1796. {
  1797. int args[3];
  1798. no_scsi();
  1799. printf(" attempting to scan hwif (0x%x, 0x%x, %u)\n", hwif_data, hwif_ctrl, hwif_irq);
  1800. args[0] = hwif_data;
  1801. args[1] = hwif_ctrl;
  1802. args[2] = hwif_irq;
  1803. bb_ioctl(fd, HDIO_SCAN_HWIF, args, "HDIO_SCAN_HWIF");
  1804. }
  1805. #endif
  1806. if (set_piomode)
  1807. {
  1808. no_scsi();
  1809. no_xt();
  1810. if (noisy_piomode)
  1811. {
  1812. printf(" attempting to ");
  1813. if (piomode == 255)
  1814. printf("auto-tune PIO mode\n");
  1815. else if (piomode < 100)
  1816. printf("set PIO mode to %d\n", piomode);
  1817. else if (piomode < 200)
  1818. printf("set MDMA mode to %d\n", (piomode-100));
  1819. else
  1820. printf("set UDMA mode to %d\n", (piomode-200));
  1821. }
  1822. bb_ioctl(fd, HDIO_SET_PIO_MODE, (int *)piomode, "HDIO_SET_PIO_MODE");
  1823. }
  1824. if (set_io32bit)
  1825. {
  1826. no_scsi();
  1827. no_xt();
  1828. if_printf(get_io32bit," setting 32-bit IO_support flag to %ld\n", io32bit);
  1829. bb_ioctl(fd, HDIO_SET_32BIT, (int *)io32bit, "HDIO_SET_32BIT");
  1830. }
  1831. if (set_mult)
  1832. {
  1833. no_scsi();
  1834. no_xt();
  1835. if_printf(get_mult, " setting multcount to %ld\n", mult);
  1836. if(ioctl(fd, HDIO_SET_MULTCOUNT, mult))
  1837. bb_perror_msg("HDIO_SET_MULTCOUNT");
  1838. #ifndef HDIO_DRIVE_CMD
  1839. else
  1840. force_operation = 1;
  1841. #endif
  1842. }
  1843. if (set_readonly)
  1844. {
  1845. if_printf_on_off(get_readonly," setting readonly to %ld", readonly);
  1846. bb_ioctl(fd, BLKROSET, &readonly, "BLKROSET");
  1847. }
  1848. if (set_unmask)
  1849. {
  1850. no_scsi();
  1851. no_xt();
  1852. if_printf_on_off(get_unmask," setting unmaskirq to %ld", unmask);
  1853. bb_ioctl(fd, HDIO_SET_UNMASKINTR, (int *)unmask, "HDIO_SET_UNMASKINTR");
  1854. }
  1855. #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
  1856. if (set_dma)
  1857. {
  1858. no_scsi();
  1859. if_printf_on_off(get_dma," setting using_dma to %ld", dma);
  1860. bb_ioctl(fd, HDIO_SET_DMA, (int *)dma, "HDIO_SET_DMA");
  1861. }
  1862. #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
  1863. if (set_dma_q)
  1864. {
  1865. no_scsi();
  1866. if_printf_on_off(get_dma_q," setting DMA queue_depth to %ld", dma_q);
  1867. bb_ioctl(fd, HDIO_SET_QDMA, (int *)dma_q, "HDIO_SET_QDMA");
  1868. }
  1869. if (set_nowerr)
  1870. {
  1871. no_scsi();
  1872. no_xt();
  1873. if_printf_on_off(get_nowerr," setting nowerr to %ld", nowerr);
  1874. bb_ioctl(fd, HDIO_SET_NOWERR, (int *)nowerr,"HDIO_SET_NOWERR");
  1875. }
  1876. if (set_keep)
  1877. {
  1878. no_scsi();
  1879. no_xt();
  1880. if_printf_on_off(get_keep," setting keep_settings to %ld", keep);
  1881. bb_ioctl(fd, HDIO_SET_KEEPSETTINGS, (int *)keep,"HDIO_SET_KEEPSETTINGS");
  1882. }
  1883. #ifdef HDIO_DRIVE_CMD
  1884. if (set_doorlock)
  1885. {
  1886. unsigned char args[4] = {0,0,0,0};
  1887. no_scsi();
  1888. no_xt();
  1889. args[0] = doorlock ? WIN_DOORLOCK : WIN_DOORUNLOCK;
  1890. if_printf_on_off(get_doorlock," setting drive doorlock to %ld", doorlock);
  1891. bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(doorlock)");
  1892. }
  1893. if (set_dkeep)
  1894. {
  1895. /* lock/unlock the drive's "feature" settings */
  1896. unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
  1897. no_scsi();
  1898. no_xt();
  1899. if_printf_on_off(get_dkeep," setting drive keep features to %ld", dkeep);
  1900. args[2] = dkeep ? 0x66 : 0xcc;
  1901. bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(keepsettings)");
  1902. }
  1903. if (set_defects)
  1904. {
  1905. unsigned char args[4] = {WIN_SETFEATURES,0,0x04,0};
  1906. no_scsi();
  1907. args[2] = defects ? 0x04 : 0x84;
  1908. if_printf(get_defects," setting drive defect-mgmt to %ld\n", defects);
  1909. bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(defectmgmt)");
  1910. }
  1911. if (set_prefetch)
  1912. {
  1913. unsigned char args[4] = {WIN_SETFEATURES,0,0xab,0};
  1914. no_scsi();
  1915. no_xt();
  1916. args[1] = prefetch;
  1917. if_printf(get_prefetch," setting drive prefetch to %ld\n", prefetch);
  1918. bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setprefetch)");
  1919. }
  1920. if (set_xfermode)
  1921. {
  1922. unsigned char args[4] = {WIN_SETFEATURES,0,3,0};
  1923. no_scsi();
  1924. no_xt();
  1925. args[1] = xfermode_requested;
  1926. if (get_xfermode)
  1927. {
  1928. printf(" setting xfermode to %d", xfermode_requested);
  1929. interpret_xfermode(xfermode_requested);
  1930. }
  1931. bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD(setxfermode)");
  1932. }
  1933. if (set_lookahead)
  1934. {
  1935. unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
  1936. no_scsi();
  1937. no_xt();
  1938. args[2] = lookahead ? 0xaa : 0x55;
  1939. if_printf_on_off(get_lookahead," setting drive read-lookahead to %ld", lookahead);
  1940. bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setreadahead)");
  1941. }
  1942. if (set_apmmode)
  1943. {
  1944. unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
  1945. no_scsi();
  1946. apmmode=check_if_min_and_set_val(apmmode,1);
  1947. apmmode=check_if_maj_and_set_val(apmmode,255);
  1948. if_printf(get_apmmode," setting APM level to");
  1949. if (apmmode==255)
  1950. {
  1951. /* disable Advanced Power Management */
  1952. args[2] = 0x85; /* feature register */
  1953. if_printf(get_apmmode," disabled\n");
  1954. }
  1955. else
  1956. {
  1957. /* set Advanced Power Management mode */
  1958. args[2] = 0x05; /* feature register */
  1959. args[1] = apmmode; /* sector count register */
  1960. if_printf(get_apmmode," 0x%02lX (%ld)\n",apmmode,apmmode);
  1961. }
  1962. bb_ioctl(fd, HDIO_DRIVE_CMD, &args,"HDIO_DRIVE_CMD");
  1963. }
  1964. if (set_wcache)
  1965. {
  1966. #ifdef DO_FLUSHCACHE
  1967. #ifndef WIN_FLUSHCACHE
  1968. #define WIN_FLUSHCACHE 0xe7
  1969. #endif
  1970. unsigned char flushcache[4] = {WIN_FLUSHCACHE,0,0,0};
  1971. #endif /* DO_FLUSHCACHE */
  1972. unsigned char args[4] = {WIN_SETFEATURES,0,0,0};
  1973. no_scsi();
  1974. no_xt();
  1975. args[2] = wcache ? 0x02 : 0x82;
  1976. if_printf_on_off(get_wcache," setting drive write-caching to %ld", wcache);
  1977. #ifdef DO_FLUSHCACHE
  1978. if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
  1979. bb_perror_msg ("HDIO_DRIVE_CMD(flushcache)");
  1980. #endif /* DO_FLUSHCACHE */
  1981. bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setcache)");
  1982. #ifdef DO_FLUSHCACHE
  1983. if (!wcache && ioctl(fd, HDIO_DRIVE_CMD, &flushcache))
  1984. bb_perror_msg ("HDIO_DRIVE_CMD(flushcache)");
  1985. #endif /* DO_FLUSHCACHE */
  1986. }
  1987. if (set_standbynow)
  1988. {
  1989. #ifndef WIN_STANDBYNOW1
  1990. #define WIN_STANDBYNOW1 0xE0
  1991. #endif
  1992. #ifndef WIN_STANDBYNOW2
  1993. #define WIN_STANDBYNOW2 0x94
  1994. #endif
  1995. unsigned char args1[4] = {WIN_STANDBYNOW1,0,0,0};
  1996. unsigned char args2[4] = {WIN_STANDBYNOW2,0,0,0};
  1997. no_scsi();
  1998. if_printf(get_standbynow," issuing standby command\n");
  1999. if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
  2000. && ioctl(fd, HDIO_DRIVE_CMD, &args2))
  2001. bb_perror_msg("HDIO_DRIVE_CMD(standby)");
  2002. }
  2003. if (set_sleepnow)
  2004. {
  2005. #ifndef WIN_SLEEPNOW1
  2006. #define WIN_SLEEPNOW1 0xE6
  2007. #endif
  2008. #ifndef WIN_SLEEPNOW2
  2009. #define WIN_SLEEPNOW2 0x99
  2010. #endif
  2011. unsigned char args1[4] = {WIN_SLEEPNOW1,0,0,0};
  2012. unsigned char args2[4] = {WIN_SLEEPNOW2,0,0,0};
  2013. no_scsi();
  2014. if_printf(get_sleepnow," issuing sleep command\n");
  2015. if (ioctl(fd, HDIO_DRIVE_CMD, &args1)
  2016. && ioctl(fd, HDIO_DRIVE_CMD, &args2))
  2017. bb_perror_msg("HDIO_DRIVE_CMD(sleep)");
  2018. }
  2019. if (set_seagate)
  2020. {
  2021. unsigned char args[4] = {0xfb,0,0,0};
  2022. no_scsi();
  2023. no_xt();
  2024. if_printf(get_seagate," disabling Seagate auto powersaving mode\n");
  2025. bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(seagatepwrsave)");
  2026. }
  2027. if (set_standby)
  2028. {
  2029. unsigned char args[4] = {WIN_SETIDLE1,standby_requested,0,0};
  2030. no_scsi();
  2031. no_xt();
  2032. if (get_standby)
  2033. {
  2034. printf(" setting standby to %lu", standby_requested);
  2035. interpret_standby(standby_requested);
  2036. }
  2037. bb_ioctl(fd, HDIO_DRIVE_CMD, &args, "HDIO_DRIVE_CMD(setidle1)");
  2038. }
  2039. #else /* HDIO_DRIVE_CMD */
  2040. if (force_operation)
  2041. {
  2042. char buf[512];
  2043. flush_buffer_cache(fd);
  2044. if (-1 == read(fd, buf, sizeof(buf)))
  2045. bb_error_msg("access failed");
  2046. }
  2047. #endif /* HDIO_DRIVE_CMD */
  2048. if (!flagcount)
  2049. verbose = 1;
  2050. if ((verbose && !is_scsi_hd && !is_xt_hd) || get_mult || get_identity)
  2051. {
  2052. no_scsi();
  2053. multcount = -1;
  2054. if (ioctl(fd, HDIO_GET_MULTCOUNT, &multcount))
  2055. {
  2056. if ((verbose && !is_xt_hd) || get_mult)
  2057. bb_perror_msg("HDIO_GET_MULTCOUNT");
  2058. }
  2059. else if (verbose | get_mult)
  2060. {
  2061. printf(" multcount = %2ld", multcount);
  2062. on_off(multcount);
  2063. }
  2064. }
  2065. if ((verbose && !is_scsi_hd && !is_xt_hd) || get_io32bit)
  2066. {
  2067. no_scsi();
  2068. no_xt();
  2069. if(ioctl(fd, HDIO_GET_32BIT, &parm))
  2070. bb_perror_msg("HDIO_GET_32BIT");
  2071. else
  2072. {
  2073. printf(" IO_support =%3ld (", parm);
  2074. switch (parm)
  2075. {
  2076. case 0:
  2077. printf("default ");
  2078. case 2:
  2079. printf("16-bit)\n");
  2080. break;
  2081. case 1:
  2082. printf("32-bit)\n");
  2083. break;
  2084. case 3:
  2085. printf("32-bit w/sync)\n");
  2086. break;
  2087. case 8:
  2088. printf("Request-Queue-Bypass)\n");
  2089. break;
  2090. default:
  2091. printf("\?\?\?)\n");
  2092. /*esac*/
  2093. }
  2094. }
  2095. }
  2096. if ((verbose && !is_scsi_hd && !is_xt_hd) || get_unmask)
  2097. {
  2098. no_scsi();
  2099. no_xt();
  2100. bb_ioctl_on_off(fd, HDIO_GET_UNMASKINTR,(unsigned long *)parm,
  2101. "HDIO_GET_UNMASKINTR"," unmaskirq = %2ld");
  2102. }
  2103. #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
  2104. if ((verbose && !is_scsi_hd) || get_dma) {
  2105. no_scsi();
  2106. if(ioctl(fd, HDIO_GET_DMA, &parm))
  2107. bb_perror_msg("HDIO_GET_DMA");
  2108. else
  2109. {
  2110. printf(" using_dma = %2ld", parm);
  2111. if (parm == 8)
  2112. printf(" (DMA-Assisted-PIO)\n");
  2113. else
  2114. on_off(parm);
  2115. }
  2116. }
  2117. #endif
  2118. if (get_dma_q)
  2119. {
  2120. no_scsi();
  2121. bb_ioctl_on_off (fd, HDIO_GET_QDMA,(unsigned long *)parm,
  2122. "HDIO_GET_QDMA"," queue_depth = %2ld");
  2123. }
  2124. if ((verbose && !is_scsi_hd && !is_xt_hd) || get_keep)
  2125. {
  2126. no_scsi();
  2127. no_xt();
  2128. bb_ioctl_on_off (fd, HDIO_GET_KEEPSETTINGS,(unsigned long *)parm,
  2129. "HDIO_GET_KEEPSETTINGS"," keepsettings = %2ld");
  2130. }
  2131. if (get_nowerr)
  2132. {
  2133. no_scsi();
  2134. no_xt();
  2135. bb_ioctl_on_off (fd, HDIO_GET_NOWERR,(unsigned long *)&parm,
  2136. " HDIO_GET_NOWERR"," nowerr = %2ld");
  2137. }
  2138. if (verbose || get_readonly)
  2139. {
  2140. bb_ioctl_on_off(fd, BLKROGET,(unsigned long *)parm,
  2141. " BLKROGET"," readonly = %2ld");
  2142. }
  2143. if ((verbose && !is_scsi_hd) || get_readahead)
  2144. {
  2145. bb_ioctl_on_off (fd, BLKRAGET, (unsigned long *) parm,
  2146. " BLKRAGET"," readahead = %2ld");
  2147. }
  2148. if (verbose || get_geom)
  2149. {
  2150. static const char msg[] = " geometry = %u/%u/%u, sectors = %ld, start = %ld\n";
  2151. static struct hd_geometry g;
  2152. #ifdef HDIO_GETGEO_BIG
  2153. static struct hd_big_geometry bg;
  2154. #endif
  2155. if (ioctl(fd, BLKGETSIZE, &parm))
  2156. bb_perror_msg("BLKGETSIZE");
  2157. #ifdef HDIO_GETGEO_BIG
  2158. else if (!ioctl(fd, HDIO_GETGEO_BIG, &bg))
  2159. printf(msg, bg.cylinders, bg.heads, bg.sectors, parm, bg.start);
  2160. #endif
  2161. else if (ioctl(fd, HDIO_GETGEO, &g))
  2162. bb_perror_msg("HDIO_GETGEO");
  2163. else
  2164. printf(msg, g.cylinders, g.heads, g.sectors, parm, g.start);
  2165. }
  2166. #ifdef HDIO_DRIVE_CMD
  2167. if (get_powermode)
  2168. {
  2169. #ifndef WIN_CHECKPOWERMODE1
  2170. #define WIN_CHECKPOWERMODE1 0xE5
  2171. #endif
  2172. #ifndef WIN_CHECKPOWERMODE2
  2173. #define WIN_CHECKPOWERMODE2 0x98
  2174. #endif
  2175. unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
  2176. const char *state;
  2177. no_scsi();
  2178. if (ioctl(fd, HDIO_DRIVE_CMD, &args)
  2179. && (args[0] = WIN_CHECKPOWERMODE2) /* try again with 0x98 */
  2180. && ioctl(fd, HDIO_DRIVE_CMD, &args))
  2181. {
  2182. if (errno != EIO || args[0] != 0 || args[1] != 0)
  2183. state = "unknown";
  2184. else
  2185. state = "sleeping";
  2186. }
  2187. else
  2188. state = (args[2] == 255) ? "active/idle" : "standby";
  2189. printf(" drive state is: %s\n", state);
  2190. }
  2191. #endif
  2192. #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
  2193. if (perform_reset)
  2194. {
  2195. no_scsi();
  2196. no_xt();
  2197. bb_ioctl(fd, HDIO_DRIVE_RESET, NULL, "HDIO_DRIVE_RESET");
  2198. }
  2199. #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
  2200. #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  2201. if (perform_tristate)
  2202. {
  2203. unsigned char args[4] = {0,tristate,0,0};
  2204. no_scsi();
  2205. no_xt();
  2206. bb_ioctl(fd, HDIO_TRISTATE_HWIF, &args, "HDIO_TRISTATE_HWIF");
  2207. }
  2208. #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
  2209. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  2210. if (get_identity)
  2211. {
  2212. static struct hd_driveid id;
  2213. no_scsi();
  2214. no_xt();
  2215. if (!ioctl(fd, HDIO_GET_IDENTITY, &id))
  2216. {
  2217. if (multcount != -1)
  2218. {
  2219. id.multsect = multcount;
  2220. id.multsect_valid |= 1;
  2221. }
  2222. else
  2223. id.multsect_valid &= ~1;
  2224. dump_identity(&id);
  2225. }
  2226. else if (errno == -ENOMSG)
  2227. printf(" no identification info available\n");
  2228. else
  2229. bb_perror_msg("HDIO_GET_IDENTITY");
  2230. }
  2231. if (get_IDentity)
  2232. {
  2233. unsigned char args[4+512] = {WIN_IDENTIFY,0,0,1,};
  2234. unsigned i;
  2235. no_scsi();
  2236. no_xt();
  2237. if (ioctl(fd, HDIO_DRIVE_CMD, &args))
  2238. {
  2239. args[0] = WIN_PIDENTIFY;
  2240. if (ioctl(fd, HDIO_DRIVE_CMD, &args))
  2241. {
  2242. bb_perror_msg("HDIO_DRIVE_CMD(identify)");
  2243. goto identify_abort;
  2244. }
  2245. }
  2246. for(i=0; i<(sizeof args)/2; i+=2)
  2247. __le16_to_cpus((uint16_t *)(&args[i]));
  2248. identify((void *)&args[4], NULL);
  2249. identify_abort:
  2250. /* VOID */;
  2251. }
  2252. #endif
  2253. #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  2254. if (set_busstate)
  2255. {
  2256. no_scsi();
  2257. if (get_busstate)
  2258. {
  2259. printf(" setting bus state to %d", busstate);
  2260. bus_state_value(busstate);
  2261. }
  2262. bb_ioctl(fd, HDIO_SET_BUSSTATE, (int *)busstate, "HDIO_SET_BUSSTATE");
  2263. }
  2264. #endif
  2265. #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  2266. if (get_busstate)
  2267. {
  2268. no_scsi();
  2269. if (ioctl(fd, HDIO_GET_BUSSTATE, &parm))
  2270. bb_perror_msg("HDIO_GET_BUSSTATE");
  2271. else
  2272. {
  2273. printf(" busstate = %2ld", parm);
  2274. bus_state_value(parm);
  2275. }
  2276. }
  2277. #endif
  2278. if (reread_partn)
  2279. bb_ioctl(fd, BLKRRPART, NULL, "BLKRRPART");
  2280. if (do_ctimings)
  2281. do_time(0,fd); /*time cache */
  2282. if (do_timings)
  2283. do_time(1,fd); /*time device */
  2284. if (do_flush)
  2285. flush_buffer_cache (fd);
  2286. close (fd);
  2287. }
  2288. static char * GET_NUMBER(char *p, unsigned long *flag, unsigned long *num)
  2289. {
  2290. *num = 0;
  2291. while (isdigit(*p)) {
  2292. *flag = 1;
  2293. *num = (*num * 10) + (*p++ - '0');
  2294. }
  2295. return p;
  2296. }
  2297. static char * GET_STRING(char *p, unsigned long *flag, int *num)
  2298. {
  2299. char *tmpstr;
  2300. char name[32];
  2301. tmpstr = name;
  2302. tmpstr[0] = '\0';
  2303. while (isalnum(*p) && (tmpstr - name) < 31) {
  2304. tmpstr[0] = *p++;
  2305. tmpstr[1] = '\0';
  2306. ++tmpstr;
  2307. }
  2308. *num = translate_xfermode(name);
  2309. if (*num == -1)
  2310. *flag = 0;
  2311. else
  2312. *flag = 1;
  2313. return p;
  2314. }
  2315. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  2316. static int fromhex (unsigned char c)
  2317. {
  2318. if (c >= 'a' && c <= 'f')
  2319. return 10 + (c - 'a');
  2320. if (c >= '0' && c <= '9')
  2321. return (c - '0');
  2322. bb_error_msg_and_die("bad char: '%c' 0x%02x", c, c);
  2323. }
  2324. static int identify_from_stdin (void)
  2325. {
  2326. unsigned short sbuf[800];
  2327. unsigned char buf[1600], *b = (unsigned char *)buf;
  2328. int i, count = read(0, buf, 1280);
  2329. if (count != 1280)
  2330. bb_error_msg_and_die("read(1280 bytes) failed (rc=%d)", count);
  2331. for (i = 0; count >= 4; ++i)
  2332. {
  2333. sbuf[i] = (fromhex(b[0]) << 12) | (fromhex(b[1]) << 8) | (fromhex(b[2]) << 4) | fromhex(b[3]);
  2334. __le16_to_cpus((uint16_t *)(&sbuf[i]));
  2335. b += 5;
  2336. count -= 5;
  2337. }
  2338. identify(sbuf, NULL);
  2339. return 0;
  2340. }
  2341. #endif
  2342. static void missing_arg(int arg, char c, char* add)
  2343. {
  2344. if (!arg)
  2345. bb_error_msg("-%c: missing value %s", c, (add!=NULL)? add :"");
  2346. }
  2347. /* our main() routine: */
  2348. int hdparm_main(int argc, char **argv)
  2349. {
  2350. char c, *p;
  2351. ++argv;
  2352. if (!--argc)
  2353. bb_show_usage();
  2354. while (argc--)
  2355. {
  2356. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  2357. if (!strcmp("-Istdin", *argv))
  2358. {
  2359. exit(identify_from_stdin());
  2360. }
  2361. #endif
  2362. p = *argv++;
  2363. if (*p == '-')
  2364. {
  2365. if (!*++p)
  2366. bb_show_usage();
  2367. while ((c = *p++))
  2368. {
  2369. ++flagcount;
  2370. switch (c)
  2371. {
  2372. case 'V':
  2373. /*bb_error_msg_and_die("%s", VERSION);*/
  2374. /* We have to return 0 here and not 1 */
  2375. printf("%s %s\n",bb_applet_name, VERSION);
  2376. exit(EXIT_SUCCESS);
  2377. case 'v':
  2378. verbose = 1;
  2379. break;
  2380. #ifdef CONFIG_FEATURE_HDPARM_GET_IDENTITY
  2381. case 'I':
  2382. get_IDentity = 1;
  2383. break;
  2384. case 'i':
  2385. get_identity = 1;
  2386. break;
  2387. #endif
  2388. case 'g':
  2389. get_geom = 1;
  2390. break;
  2391. case 'f':
  2392. do_flush = 1;
  2393. break;
  2394. case 'q':
  2395. quiet = 1;
  2396. noisy = 0;
  2397. break;
  2398. case 'u':
  2399. get_unmask = noisy;
  2400. noisy = 1;
  2401. if (!*p && argc && isdigit(**argv))
  2402. p = *argv++, --argc;
  2403. if((set_unmask = set_flag(p,'1'))==1)
  2404. unmask = *p++ - '0';
  2405. break;
  2406. #ifdef CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA
  2407. case 'd':
  2408. get_dma = noisy;
  2409. noisy = 1;
  2410. if (!*p && argc && isdigit(**argv))
  2411. p = *argv++, --argc;
  2412. if((set_dma = set_flag(p,'9'))==1)
  2413. dma = *p++ - '0';
  2414. break;
  2415. #endif /* CONFIG_FEATURE_HDPARM_HDIO_GETSET_DMA */
  2416. case 'n':
  2417. get_nowerr = noisy;
  2418. noisy = 1;
  2419. if (!*p && argc && isdigit(**argv))
  2420. p = *argv++, --argc;
  2421. if((set_nowerr = set_flag(p,'1'))==1)
  2422. nowerr = *p++ - '0';
  2423. break;
  2424. case 'p':
  2425. noisy_piomode = noisy;
  2426. noisy = 1;
  2427. if (!*p && argc && isalnum(**argv))
  2428. p = *argv++, --argc;
  2429. p=GET_STRING(p,&set_piomode,&piomode);
  2430. break;
  2431. case 'r':
  2432. get_readonly = noisy;
  2433. noisy = 1;
  2434. if (!*p && argc && isdigit(**argv))
  2435. p = *argv++, --argc;
  2436. if((set_readonly = set_flag(p,'1'))==1)
  2437. readonly = *p++ - '0';
  2438. break;
  2439. case 'm':
  2440. get_mult = noisy;
  2441. noisy = 1;
  2442. if (!*p && argc && isalnum(**argv))
  2443. p = *argv++, --argc;
  2444. p=GET_NUMBER(p,&set_mult,&mult);
  2445. break;
  2446. case 'c':
  2447. get_io32bit = noisy;
  2448. noisy = 1;
  2449. if (!*p && argc && isalnum(**argv))
  2450. p = *argv++, --argc;
  2451. p=GET_NUMBER(p,&set_io32bit,&io32bit);
  2452. break;
  2453. #ifdef HDIO_DRIVE_CMD
  2454. case 'S':
  2455. get_standby = noisy;
  2456. noisy = 1;
  2457. if (!*p && argc && isalnum(**argv))
  2458. p = *argv++, --argc;
  2459. p=GET_NUMBER(p,&set_standby,&standby_requested);
  2460. missing_arg(set_standby, c, NULL);
  2461. break;
  2462. case 'D':
  2463. get_defects = noisy;
  2464. noisy = 1;
  2465. if (!*p && argc && isalnum(**argv))
  2466. p = *argv++, --argc;
  2467. p=GET_NUMBER(p,&set_defects,&defects);
  2468. missing_arg(set_defects, c, NULL);
  2469. break;
  2470. case 'P':
  2471. get_prefetch = noisy;
  2472. noisy = 1;
  2473. if (!*p && argc && isalnum(**argv))
  2474. p = *argv++, --argc;
  2475. p=GET_NUMBER(p,&set_prefetch,&prefetch);
  2476. missing_arg(set_prefetch, c, NULL);
  2477. break;
  2478. case 'X':
  2479. get_xfermode = noisy;
  2480. noisy = 1;
  2481. if (!*p && argc && isalnum(**argv))
  2482. p = *argv++, --argc;
  2483. p=GET_STRING(p,&set_xfermode,&xfermode_requested);
  2484. missing_arg(set_xfermode, c, NULL);
  2485. break;
  2486. case 'K':
  2487. get_dkeep = noisy;
  2488. noisy = 1;
  2489. if (!*p && argc && isdigit(**argv))
  2490. p = *argv++, --argc;
  2491. if((set_dkeep = set_flag(p,'1'))==1)
  2492. dkeep = *p++ - '0';
  2493. else
  2494. goto missing_arg_error;
  2495. break;
  2496. case 'A':
  2497. get_lookahead = noisy;
  2498. noisy = 1;
  2499. if (!*p && argc && isdigit(**argv))
  2500. p = *argv++, --argc;
  2501. if((set_lookahead = set_flag(p,'1'))==1)
  2502. lookahead = *p++ - '0';
  2503. else
  2504. goto missing_arg_error;
  2505. break;
  2506. case 'L':
  2507. get_doorlock = noisy;
  2508. noisy = 1;
  2509. if (!*p && argc && isdigit(**argv))
  2510. p = *argv++, --argc;
  2511. if((set_doorlock = set_flag(p,'1'))==1)
  2512. doorlock = *p++ - '0';
  2513. else
  2514. goto missing_arg_error;
  2515. break;
  2516. case 'W':
  2517. get_wcache = noisy;
  2518. noisy = 1;
  2519. if (!*p && argc && isdigit(**argv))
  2520. p = *argv++, --argc;
  2521. if((set_wcache = set_flag(p,'1'))==1)
  2522. wcache = *p++ - '0';
  2523. else
  2524. missing_arg_error:
  2525. missing_arg(1, c, "(0/1)");
  2526. break;
  2527. case 'C':
  2528. get_powermode = noisy;
  2529. noisy = 1;
  2530. break;
  2531. case 'y':
  2532. get_standbynow = noisy;
  2533. noisy = 1;
  2534. set_standbynow = 1;
  2535. break;
  2536. case 'Y':
  2537. get_sleepnow = noisy;
  2538. noisy = 1;
  2539. set_sleepnow = 1;
  2540. break;
  2541. case 'z':
  2542. reread_partn = 1;
  2543. break;
  2544. case 'Z':
  2545. get_seagate = noisy;
  2546. noisy = 1;
  2547. set_seagate = 1;
  2548. break;
  2549. #endif /* HDIO_DRIVE_CMD */
  2550. case 'k':
  2551. get_keep = noisy;
  2552. noisy = 1;
  2553. if (!*p && argc && isdigit(**argv))
  2554. p = *argv++, --argc;
  2555. if((set_keep = set_flag(p,'1'))==1)
  2556. keep = *p++ - '0';
  2557. break;
  2558. #ifdef CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF
  2559. case 'U':
  2560. if (!*p && argc && isdigit(**argv))
  2561. p = *argv++, --argc;
  2562. if(! p)
  2563. goto expected_hwif_error; /* "expected hwif_nr" */
  2564. sscanf(p++, "%i", &hwif);
  2565. unregister_hwif = 1;
  2566. break;
  2567. #endif /* CONFIG_FEATURE_HDPARM_HDIO_UNREGISTER_HWIF */
  2568. #ifdef CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF
  2569. case 'R':
  2570. if (!*p && argc && isdigit(**argv))
  2571. p = *argv++, --argc;
  2572. if(! p)
  2573. goto expected_hwif_error; /* "expected hwif_data" */
  2574. sscanf(p++, "%i", &hwif_data);
  2575. if (argc && isdigit(**argv))
  2576. p = *argv++, --argc;
  2577. else
  2578. goto expected_hwif_error; /* "expected hwif_ctrl" */
  2579. sscanf(p, "%i", &hwif_ctrl);
  2580. if (argc && isdigit(**argv))
  2581. p = *argv++, --argc;
  2582. else
  2583. expected_hwif_error:
  2584. bb_error_msg_and_die("expected hwif value"); /* "expected hwif_irq" */
  2585. sscanf(p, "%i", &hwif_irq);
  2586. *p = '\0';
  2587. scan_hwif = 1;
  2588. break;
  2589. #endif /* CONFIG_FEATURE_HDPARM_HDIO_SCAN_HWIF */
  2590. case 'Q':
  2591. #ifdef HDIO_GET_QDMA
  2592. get_dma_q = noisy;
  2593. noisy = 1;
  2594. #ifdef HDIO_SET_QDMA
  2595. if (!*p && argc && isalnum(**argv))
  2596. p = *argv++, --argc;
  2597. p=GET_NUMBER(p,&set_dma_q,&dma_q);
  2598. #ifdef HDIO_GET_QDMA
  2599. dma_q = -dma_q;
  2600. #endif
  2601. #endif
  2602. #endif
  2603. break;
  2604. #ifdef CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET
  2605. case 'w':
  2606. perform_reset = 1;
  2607. break;
  2608. #endif /* CONFIG_FEATURE_HDPARM_HDIO_DRIVE_RESET */
  2609. #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  2610. case 'x':
  2611. if (!*p && argc && isdigit(**argv))
  2612. p = *argv++, --argc;
  2613. if((perform_tristate = set_flag(p,'1'))==1)
  2614. tristate = *p++ - '0';
  2615. else
  2616. missing_arg(1, c, "(0/1)");
  2617. break;
  2618. #endif /* CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF */
  2619. case 'a':
  2620. get_readahead = noisy;
  2621. noisy = 1;
  2622. if (!*p && argc && isalnum(**argv))
  2623. p = *argv++, --argc;
  2624. p=GET_NUMBER(p,&set_readahead,&Xreadahead);
  2625. break;
  2626. case 'B':
  2627. get_apmmode = noisy;
  2628. noisy = 1;
  2629. if (!*p && argc && isalnum(**argv))
  2630. p = *argv++, --argc;
  2631. p=GET_NUMBER(p,&set_apmmode,&apmmode);
  2632. missing_arg(set_apmmode, c, "(1-255)");
  2633. break;
  2634. case 't':
  2635. do_timings = 1;
  2636. do_flush = 1;
  2637. break;
  2638. case 'T':
  2639. do_ctimings = 1;
  2640. do_flush = 1;
  2641. break;
  2642. #ifdef CONFIG_FEATURE_HDPARM_HDIO_TRISTATE_HWIF
  2643. case 'b':
  2644. get_busstate = noisy;
  2645. noisy = 1;
  2646. if (!*p && argc && isdigit(**argv))
  2647. p = *argv++, --argc;
  2648. if((set_busstate = set_flag(p,'2'))==1)
  2649. busstate = *p++ - '0';
  2650. break;
  2651. #endif
  2652. case 'h':
  2653. default:
  2654. bb_show_usage();
  2655. }
  2656. }
  2657. if (!argc)
  2658. bb_show_usage();
  2659. } else {
  2660. process_dev (p);
  2661. }
  2662. }
  2663. return 0 ;
  2664. }