vsprintf.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180
  1. /*
  2. * linux/lib/vsprintf.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
  7. /*
  8. * Wirzenius wrote this portably, Torvalds fucked it up :-)
  9. */
  10. /*
  11. * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
  12. * - changed to provide snprintf and vsnprintf functions
  13. * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
  14. * - scnprintf and vscnprintf
  15. */
  16. #include <stdarg.h>
  17. #include <linux/clk.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
  20. #include <linux/types.h>
  21. #include <linux/string.h>
  22. #include <linux/ctype.h>
  23. #include <linux/kernel.h>
  24. #include <linux/kallsyms.h>
  25. #include <linux/math64.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/ioport.h>
  28. #include <linux/dcache.h>
  29. #include <linux/cred.h>
  30. #include <linux/uuid.h>
  31. #include <linux/of.h>
  32. #include <net/addrconf.h>
  33. #include <linux/siphash.h>
  34. #include <linux/compiler.h>
  35. #ifdef CONFIG_BLOCK
  36. #include <linux/blkdev.h>
  37. #endif
  38. #include "../mm/internal.h" /* For the trace_print_flags arrays */
  39. #include <asm/page.h> /* for PAGE_SIZE */
  40. #include <asm/byteorder.h> /* cpu_to_le16 */
  41. #include <linux/string_helpers.h>
  42. #include "kstrtox.h"
  43. /**
  44. * simple_strtoull - convert a string to an unsigned long long
  45. * @cp: The start of the string
  46. * @endp: A pointer to the end of the parsed string will be placed here
  47. * @base: The number base to use
  48. *
  49. * This function is obsolete. Please use kstrtoull instead.
  50. */
  51. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  52. {
  53. unsigned long long result;
  54. unsigned int rv;
  55. cp = _parse_integer_fixup_radix(cp, &base);
  56. rv = _parse_integer(cp, base, &result);
  57. /* FIXME */
  58. cp += (rv & ~KSTRTOX_OVERFLOW);
  59. if (endp)
  60. *endp = (char *)cp;
  61. return result;
  62. }
  63. EXPORT_SYMBOL(simple_strtoull);
  64. /**
  65. * simple_strtoul - convert a string to an unsigned long
  66. * @cp: The start of the string
  67. * @endp: A pointer to the end of the parsed string will be placed here
  68. * @base: The number base to use
  69. *
  70. * This function is obsolete. Please use kstrtoul instead.
  71. */
  72. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  73. {
  74. return simple_strtoull(cp, endp, base);
  75. }
  76. EXPORT_SYMBOL(simple_strtoul);
  77. /**
  78. * simple_strtol - convert a string to a signed long
  79. * @cp: The start of the string
  80. * @endp: A pointer to the end of the parsed string will be placed here
  81. * @base: The number base to use
  82. *
  83. * This function is obsolete. Please use kstrtol instead.
  84. */
  85. long simple_strtol(const char *cp, char **endp, unsigned int base)
  86. {
  87. if (*cp == '-')
  88. return -simple_strtoul(cp + 1, endp, base);
  89. return simple_strtoul(cp, endp, base);
  90. }
  91. EXPORT_SYMBOL(simple_strtol);
  92. /**
  93. * simple_strtoll - convert a string to a signed long long
  94. * @cp: The start of the string
  95. * @endp: A pointer to the end of the parsed string will be placed here
  96. * @base: The number base to use
  97. *
  98. * This function is obsolete. Please use kstrtoll instead.
  99. */
  100. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  101. {
  102. if (*cp == '-')
  103. return -simple_strtoull(cp + 1, endp, base);
  104. return simple_strtoull(cp, endp, base);
  105. }
  106. EXPORT_SYMBOL(simple_strtoll);
  107. static noinline_for_stack
  108. int skip_atoi(const char **s)
  109. {
  110. int i = 0;
  111. do {
  112. i = i*10 + *((*s)++) - '0';
  113. } while (isdigit(**s));
  114. return i;
  115. }
  116. /*
  117. * Decimal conversion is by far the most typical, and is used for
  118. * /proc and /sys data. This directly impacts e.g. top performance
  119. * with many processes running. We optimize it for speed by emitting
  120. * two characters at a time, using a 200 byte lookup table. This
  121. * roughly halves the number of multiplications compared to computing
  122. * the digits one at a time. Implementation strongly inspired by the
  123. * previous version, which in turn used ideas described at
  124. * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
  125. * from the author, Douglas W. Jones).
  126. *
  127. * It turns out there is precisely one 26 bit fixed-point
  128. * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
  129. * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
  130. * range happens to be somewhat larger (x <= 1073741898), but that's
  131. * irrelevant for our purpose.
  132. *
  133. * For dividing a number in the range [10^4, 10^6-1] by 100, we still
  134. * need a 32x32->64 bit multiply, so we simply use the same constant.
  135. *
  136. * For dividing a number in the range [100, 10^4-1] by 100, there are
  137. * several options. The simplest is (x * 0x147b) >> 19, which is valid
  138. * for all x <= 43698.
  139. */
  140. static const u16 decpair[100] = {
  141. #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
  142. _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
  143. _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
  144. _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
  145. _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
  146. _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
  147. _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
  148. _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
  149. _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
  150. _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
  151. _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
  152. #undef _
  153. };
  154. /*
  155. * This will print a single '0' even if r == 0, since we would
  156. * immediately jump to out_r where two 0s would be written but only
  157. * one of them accounted for in buf. This is needed by ip4_string
  158. * below. All other callers pass a non-zero value of r.
  159. */
  160. static noinline_for_stack
  161. char *put_dec_trunc8(char *buf, unsigned r)
  162. {
  163. unsigned q;
  164. /* 1 <= r < 10^8 */
  165. if (r < 100)
  166. goto out_r;
  167. /* 100 <= r < 10^8 */
  168. q = (r * (u64)0x28f5c29) >> 32;
  169. *((u16 *)buf) = decpair[r - 100*q];
  170. buf += 2;
  171. /* 1 <= q < 10^6 */
  172. if (q < 100)
  173. goto out_q;
  174. /* 100 <= q < 10^6 */
  175. r = (q * (u64)0x28f5c29) >> 32;
  176. *((u16 *)buf) = decpair[q - 100*r];
  177. buf += 2;
  178. /* 1 <= r < 10^4 */
  179. if (r < 100)
  180. goto out_r;
  181. /* 100 <= r < 10^4 */
  182. q = (r * 0x147b) >> 19;
  183. *((u16 *)buf) = decpair[r - 100*q];
  184. buf += 2;
  185. out_q:
  186. /* 1 <= q < 100 */
  187. r = q;
  188. out_r:
  189. /* 1 <= r < 100 */
  190. *((u16 *)buf) = decpair[r];
  191. buf += r < 10 ? 1 : 2;
  192. return buf;
  193. }
  194. #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
  195. static noinline_for_stack
  196. char *put_dec_full8(char *buf, unsigned r)
  197. {
  198. unsigned q;
  199. /* 0 <= r < 10^8 */
  200. q = (r * (u64)0x28f5c29) >> 32;
  201. *((u16 *)buf) = decpair[r - 100*q];
  202. buf += 2;
  203. /* 0 <= q < 10^6 */
  204. r = (q * (u64)0x28f5c29) >> 32;
  205. *((u16 *)buf) = decpair[q - 100*r];
  206. buf += 2;
  207. /* 0 <= r < 10^4 */
  208. q = (r * 0x147b) >> 19;
  209. *((u16 *)buf) = decpair[r - 100*q];
  210. buf += 2;
  211. /* 0 <= q < 100 */
  212. *((u16 *)buf) = decpair[q];
  213. buf += 2;
  214. return buf;
  215. }
  216. static noinline_for_stack
  217. char *put_dec(char *buf, unsigned long long n)
  218. {
  219. if (n >= 100*1000*1000)
  220. buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
  221. /* 1 <= n <= 1.6e11 */
  222. if (n >= 100*1000*1000)
  223. buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
  224. /* 1 <= n < 1e8 */
  225. return put_dec_trunc8(buf, n);
  226. }
  227. #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
  228. static void
  229. put_dec_full4(char *buf, unsigned r)
  230. {
  231. unsigned q;
  232. /* 0 <= r < 10^4 */
  233. q = (r * 0x147b) >> 19;
  234. *((u16 *)buf) = decpair[r - 100*q];
  235. buf += 2;
  236. /* 0 <= q < 100 */
  237. *((u16 *)buf) = decpair[q];
  238. }
  239. /*
  240. * Call put_dec_full4 on x % 10000, return x / 10000.
  241. * The approximation x/10000 == (x * 0x346DC5D7) >> 43
  242. * holds for all x < 1,128,869,999. The largest value this
  243. * helper will ever be asked to convert is 1,125,520,955.
  244. * (second call in the put_dec code, assuming n is all-ones).
  245. */
  246. static noinline_for_stack
  247. unsigned put_dec_helper4(char *buf, unsigned x)
  248. {
  249. uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
  250. put_dec_full4(buf, x - q * 10000);
  251. return q;
  252. }
  253. /* Based on code by Douglas W. Jones found at
  254. * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
  255. * (with permission from the author).
  256. * Performs no 64-bit division and hence should be fast on 32-bit machines.
  257. */
  258. static
  259. char *put_dec(char *buf, unsigned long long n)
  260. {
  261. uint32_t d3, d2, d1, q, h;
  262. if (n < 100*1000*1000)
  263. return put_dec_trunc8(buf, n);
  264. d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
  265. h = (n >> 32);
  266. d2 = (h ) & 0xffff;
  267. d3 = (h >> 16); /* implicit "& 0xffff" */
  268. /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
  269. = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
  270. q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
  271. q = put_dec_helper4(buf, q);
  272. q += 7671 * d3 + 9496 * d2 + 6 * d1;
  273. q = put_dec_helper4(buf+4, q);
  274. q += 4749 * d3 + 42 * d2;
  275. q = put_dec_helper4(buf+8, q);
  276. q += 281 * d3;
  277. buf += 12;
  278. if (q)
  279. buf = put_dec_trunc8(buf, q);
  280. else while (buf[-1] == '0')
  281. --buf;
  282. return buf;
  283. }
  284. #endif
  285. /*
  286. * Convert passed number to decimal string.
  287. * Returns the length of string. On buffer overflow, returns 0.
  288. *
  289. * If speed is not important, use snprintf(). It's easy to read the code.
  290. */
  291. int num_to_str(char *buf, int size, unsigned long long num)
  292. {
  293. /* put_dec requires 2-byte alignment of the buffer. */
  294. char tmp[sizeof(num) * 3] __aligned(2);
  295. int idx, len;
  296. /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
  297. if (num <= 9) {
  298. tmp[0] = '0' + num;
  299. len = 1;
  300. } else {
  301. len = put_dec(tmp, num) - tmp;
  302. }
  303. if (len > size)
  304. return 0;
  305. for (idx = 0; idx < len; ++idx)
  306. buf[idx] = tmp[len - idx - 1];
  307. return len;
  308. }
  309. #define SIGN 1 /* unsigned/signed, must be 1 */
  310. #define LEFT 2 /* left justified */
  311. #define PLUS 4 /* show plus */
  312. #define SPACE 8 /* space if plus */
  313. #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
  314. #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
  315. #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
  316. enum format_type {
  317. FORMAT_TYPE_NONE, /* Just a string part */
  318. FORMAT_TYPE_WIDTH,
  319. FORMAT_TYPE_PRECISION,
  320. FORMAT_TYPE_CHAR,
  321. FORMAT_TYPE_STR,
  322. FORMAT_TYPE_PTR,
  323. FORMAT_TYPE_PERCENT_CHAR,
  324. FORMAT_TYPE_INVALID,
  325. FORMAT_TYPE_LONG_LONG,
  326. FORMAT_TYPE_ULONG,
  327. FORMAT_TYPE_LONG,
  328. FORMAT_TYPE_UBYTE,
  329. FORMAT_TYPE_BYTE,
  330. FORMAT_TYPE_USHORT,
  331. FORMAT_TYPE_SHORT,
  332. FORMAT_TYPE_UINT,
  333. FORMAT_TYPE_INT,
  334. FORMAT_TYPE_SIZE_T,
  335. FORMAT_TYPE_PTRDIFF
  336. };
  337. struct printf_spec {
  338. unsigned int type:8; /* format_type enum */
  339. signed int field_width:24; /* width of output field */
  340. unsigned int flags:8; /* flags to number() */
  341. unsigned int base:8; /* number base, 8, 10 or 16 only */
  342. signed int precision:16; /* # of digits/chars */
  343. } __packed;
  344. #define FIELD_WIDTH_MAX ((1 << 23) - 1)
  345. #define PRECISION_MAX ((1 << 15) - 1)
  346. static noinline_for_stack
  347. char *number(char *buf, char *end, unsigned long long num,
  348. struct printf_spec spec)
  349. {
  350. /* put_dec requires 2-byte alignment of the buffer. */
  351. char tmp[3 * sizeof(num)] __aligned(2);
  352. char sign;
  353. char locase;
  354. int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
  355. int i;
  356. bool is_zero = num == 0LL;
  357. int field_width = spec.field_width;
  358. int precision = spec.precision;
  359. BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
  360. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  361. * produces same digits or (maybe lowercased) letters */
  362. locase = (spec.flags & SMALL);
  363. if (spec.flags & LEFT)
  364. spec.flags &= ~ZEROPAD;
  365. sign = 0;
  366. if (spec.flags & SIGN) {
  367. if ((signed long long)num < 0) {
  368. sign = '-';
  369. num = -(signed long long)num;
  370. field_width--;
  371. } else if (spec.flags & PLUS) {
  372. sign = '+';
  373. field_width--;
  374. } else if (spec.flags & SPACE) {
  375. sign = ' ';
  376. field_width--;
  377. }
  378. }
  379. if (need_pfx) {
  380. if (spec.base == 16)
  381. field_width -= 2;
  382. else if (!is_zero)
  383. field_width--;
  384. }
  385. /* generate full string in tmp[], in reverse order */
  386. i = 0;
  387. if (num < spec.base)
  388. tmp[i++] = hex_asc_upper[num] | locase;
  389. else if (spec.base != 10) { /* 8 or 16 */
  390. int mask = spec.base - 1;
  391. int shift = 3;
  392. if (spec.base == 16)
  393. shift = 4;
  394. do {
  395. tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
  396. num >>= shift;
  397. } while (num);
  398. } else { /* base 10 */
  399. i = put_dec(tmp, num) - tmp;
  400. }
  401. /* printing 100 using %2d gives "100", not "00" */
  402. if (i > precision)
  403. precision = i;
  404. /* leading space padding */
  405. field_width -= precision;
  406. if (!(spec.flags & (ZEROPAD | LEFT))) {
  407. while (--field_width >= 0) {
  408. if (buf < end)
  409. *buf = ' ';
  410. ++buf;
  411. }
  412. }
  413. /* sign */
  414. if (sign) {
  415. if (buf < end)
  416. *buf = sign;
  417. ++buf;
  418. }
  419. /* "0x" / "0" prefix */
  420. if (need_pfx) {
  421. if (spec.base == 16 || !is_zero) {
  422. if (buf < end)
  423. *buf = '0';
  424. ++buf;
  425. }
  426. if (spec.base == 16) {
  427. if (buf < end)
  428. *buf = ('X' | locase);
  429. ++buf;
  430. }
  431. }
  432. /* zero or space padding */
  433. if (!(spec.flags & LEFT)) {
  434. char c = ' ' + (spec.flags & ZEROPAD);
  435. BUILD_BUG_ON(' ' + ZEROPAD != '0');
  436. while (--field_width >= 0) {
  437. if (buf < end)
  438. *buf = c;
  439. ++buf;
  440. }
  441. }
  442. /* hmm even more zero padding? */
  443. while (i <= --precision) {
  444. if (buf < end)
  445. *buf = '0';
  446. ++buf;
  447. }
  448. /* actual digits of result */
  449. while (--i >= 0) {
  450. if (buf < end)
  451. *buf = tmp[i];
  452. ++buf;
  453. }
  454. /* trailing space padding */
  455. while (--field_width >= 0) {
  456. if (buf < end)
  457. *buf = ' ';
  458. ++buf;
  459. }
  460. return buf;
  461. }
  462. static noinline_for_stack
  463. char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
  464. {
  465. struct printf_spec spec;
  466. spec.type = FORMAT_TYPE_PTR;
  467. spec.field_width = 2 + 2 * size; /* 0x + hex */
  468. spec.flags = SPECIAL | SMALL | ZEROPAD;
  469. spec.base = 16;
  470. spec.precision = -1;
  471. return number(buf, end, num, spec);
  472. }
  473. static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
  474. {
  475. size_t size;
  476. if (buf >= end) /* nowhere to put anything */
  477. return;
  478. size = end - buf;
  479. if (size <= spaces) {
  480. memset(buf, ' ', size);
  481. return;
  482. }
  483. if (len) {
  484. if (len > size - spaces)
  485. len = size - spaces;
  486. memmove(buf + spaces, buf, len);
  487. }
  488. memset(buf, ' ', spaces);
  489. }
  490. /*
  491. * Handle field width padding for a string.
  492. * @buf: current buffer position
  493. * @n: length of string
  494. * @end: end of output buffer
  495. * @spec: for field width and flags
  496. * Returns: new buffer position after padding.
  497. */
  498. static noinline_for_stack
  499. char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
  500. {
  501. unsigned spaces;
  502. if (likely(n >= spec.field_width))
  503. return buf;
  504. /* we want to pad the sucker */
  505. spaces = spec.field_width - n;
  506. if (!(spec.flags & LEFT)) {
  507. move_right(buf - n, end, n, spaces);
  508. return buf + spaces;
  509. }
  510. while (spaces--) {
  511. if (buf < end)
  512. *buf = ' ';
  513. ++buf;
  514. }
  515. return buf;
  516. }
  517. static noinline_for_stack
  518. char *string(char *buf, char *end, const char *s, struct printf_spec spec)
  519. {
  520. int len = 0;
  521. size_t lim = spec.precision;
  522. if ((unsigned long)s < PAGE_SIZE)
  523. s = "(null)";
  524. while (lim--) {
  525. char c = *s++;
  526. if (!c)
  527. break;
  528. if (buf < end)
  529. *buf = c;
  530. ++buf;
  531. ++len;
  532. }
  533. return widen_string(buf, len, end, spec);
  534. }
  535. static noinline_for_stack
  536. char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
  537. const char *fmt)
  538. {
  539. const char *array[4], *s;
  540. const struct dentry *p;
  541. int depth;
  542. int i, n;
  543. switch (fmt[1]) {
  544. case '2': case '3': case '4':
  545. depth = fmt[1] - '0';
  546. break;
  547. default:
  548. depth = 1;
  549. }
  550. rcu_read_lock();
  551. for (i = 0; i < depth; i++, d = p) {
  552. p = READ_ONCE(d->d_parent);
  553. array[i] = READ_ONCE(d->d_name.name);
  554. if (p == d) {
  555. if (i)
  556. array[i] = "";
  557. i++;
  558. break;
  559. }
  560. }
  561. s = array[--i];
  562. for (n = 0; n != spec.precision; n++, buf++) {
  563. char c = *s++;
  564. if (!c) {
  565. if (!i)
  566. break;
  567. c = '/';
  568. s = array[--i];
  569. }
  570. if (buf < end)
  571. *buf = c;
  572. }
  573. rcu_read_unlock();
  574. return widen_string(buf, n, end, spec);
  575. }
  576. #ifdef CONFIG_BLOCK
  577. static noinline_for_stack
  578. char *bdev_name(char *buf, char *end, struct block_device *bdev,
  579. struct printf_spec spec, const char *fmt)
  580. {
  581. struct gendisk *hd = bdev->bd_disk;
  582. buf = string(buf, end, hd->disk_name, spec);
  583. if (bdev->bd_part->partno) {
  584. if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
  585. if (buf < end)
  586. *buf = 'p';
  587. buf++;
  588. }
  589. buf = number(buf, end, bdev->bd_part->partno, spec);
  590. }
  591. return buf;
  592. }
  593. #endif
  594. static noinline_for_stack
  595. char *symbol_string(char *buf, char *end, void *ptr,
  596. struct printf_spec spec, const char *fmt)
  597. {
  598. unsigned long value;
  599. #ifdef CONFIG_KALLSYMS
  600. char sym[KSYM_SYMBOL_LEN];
  601. #endif
  602. if (fmt[1] == 'R')
  603. ptr = __builtin_extract_return_addr(ptr);
  604. value = (unsigned long)ptr;
  605. #ifdef CONFIG_KALLSYMS
  606. if (*fmt == 'B')
  607. sprint_backtrace(sym, value);
  608. else if (*fmt != 'f' && *fmt != 's')
  609. sprint_symbol(sym, value);
  610. else
  611. sprint_symbol_no_offset(sym, value);
  612. return string(buf, end, sym, spec);
  613. #else
  614. return special_hex_number(buf, end, value, sizeof(void *));
  615. #endif
  616. }
  617. static const struct printf_spec default_str_spec = {
  618. .field_width = -1,
  619. .precision = -1,
  620. };
  621. static const struct printf_spec default_dec_spec = {
  622. .base = 10,
  623. .precision = -1,
  624. };
  625. static noinline_for_stack
  626. char *resource_string(char *buf, char *end, struct resource *res,
  627. struct printf_spec spec, const char *fmt)
  628. {
  629. #ifndef IO_RSRC_PRINTK_SIZE
  630. #define IO_RSRC_PRINTK_SIZE 6
  631. #endif
  632. #ifndef MEM_RSRC_PRINTK_SIZE
  633. #define MEM_RSRC_PRINTK_SIZE 10
  634. #endif
  635. static const struct printf_spec io_spec = {
  636. .base = 16,
  637. .field_width = IO_RSRC_PRINTK_SIZE,
  638. .precision = -1,
  639. .flags = SPECIAL | SMALL | ZEROPAD,
  640. };
  641. static const struct printf_spec mem_spec = {
  642. .base = 16,
  643. .field_width = MEM_RSRC_PRINTK_SIZE,
  644. .precision = -1,
  645. .flags = SPECIAL | SMALL | ZEROPAD,
  646. };
  647. static const struct printf_spec bus_spec = {
  648. .base = 16,
  649. .field_width = 2,
  650. .precision = -1,
  651. .flags = SMALL | ZEROPAD,
  652. };
  653. static const struct printf_spec str_spec = {
  654. .field_width = -1,
  655. .precision = 10,
  656. .flags = LEFT,
  657. };
  658. static const struct printf_spec flag_spec = {
  659. .base = 16,
  660. .precision = -1,
  661. .flags = SPECIAL | SMALL,
  662. };
  663. /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
  664. * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
  665. #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
  666. #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
  667. #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
  668. #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
  669. char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
  670. 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
  671. char *p = sym, *pend = sym + sizeof(sym);
  672. int decode = (fmt[0] == 'R') ? 1 : 0;
  673. const struct printf_spec *specp;
  674. *p++ = '[';
  675. if (res->flags & IORESOURCE_IO) {
  676. p = string(p, pend, "io ", str_spec);
  677. specp = &io_spec;
  678. } else if (res->flags & IORESOURCE_MEM) {
  679. p = string(p, pend, "mem ", str_spec);
  680. specp = &mem_spec;
  681. } else if (res->flags & IORESOURCE_IRQ) {
  682. p = string(p, pend, "irq ", str_spec);
  683. specp = &default_dec_spec;
  684. } else if (res->flags & IORESOURCE_DMA) {
  685. p = string(p, pend, "dma ", str_spec);
  686. specp = &default_dec_spec;
  687. } else if (res->flags & IORESOURCE_BUS) {
  688. p = string(p, pend, "bus ", str_spec);
  689. specp = &bus_spec;
  690. } else {
  691. p = string(p, pend, "??? ", str_spec);
  692. specp = &mem_spec;
  693. decode = 0;
  694. }
  695. if (decode && res->flags & IORESOURCE_UNSET) {
  696. p = string(p, pend, "size ", str_spec);
  697. p = number(p, pend, resource_size(res), *specp);
  698. } else {
  699. p = number(p, pend, res->start, *specp);
  700. if (res->start != res->end) {
  701. *p++ = '-';
  702. p = number(p, pend, res->end, *specp);
  703. }
  704. }
  705. if (decode) {
  706. if (res->flags & IORESOURCE_MEM_64)
  707. p = string(p, pend, " 64bit", str_spec);
  708. if (res->flags & IORESOURCE_PREFETCH)
  709. p = string(p, pend, " pref", str_spec);
  710. if (res->flags & IORESOURCE_WINDOW)
  711. p = string(p, pend, " window", str_spec);
  712. if (res->flags & IORESOURCE_DISABLED)
  713. p = string(p, pend, " disabled", str_spec);
  714. } else {
  715. p = string(p, pend, " flags ", str_spec);
  716. p = number(p, pend, res->flags, flag_spec);
  717. }
  718. *p++ = ']';
  719. *p = '\0';
  720. return string(buf, end, sym, spec);
  721. }
  722. static noinline_for_stack
  723. char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
  724. const char *fmt)
  725. {
  726. int i, len = 1; /* if we pass '%ph[CDN]', field width remains
  727. negative value, fallback to the default */
  728. char separator;
  729. if (spec.field_width == 0)
  730. /* nothing to print */
  731. return buf;
  732. if (ZERO_OR_NULL_PTR(addr))
  733. /* NULL pointer */
  734. return string(buf, end, NULL, spec);
  735. switch (fmt[1]) {
  736. case 'C':
  737. separator = ':';
  738. break;
  739. case 'D':
  740. separator = '-';
  741. break;
  742. case 'N':
  743. separator = 0;
  744. break;
  745. default:
  746. separator = ' ';
  747. break;
  748. }
  749. if (spec.field_width > 0)
  750. len = min_t(int, spec.field_width, 64);
  751. for (i = 0; i < len; ++i) {
  752. if (buf < end)
  753. *buf = hex_asc_hi(addr[i]);
  754. ++buf;
  755. if (buf < end)
  756. *buf = hex_asc_lo(addr[i]);
  757. ++buf;
  758. if (separator && i != len - 1) {
  759. if (buf < end)
  760. *buf = separator;
  761. ++buf;
  762. }
  763. }
  764. return buf;
  765. }
  766. static noinline_for_stack
  767. char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
  768. struct printf_spec spec, const char *fmt)
  769. {
  770. const int CHUNKSZ = 32;
  771. int nr_bits = max_t(int, spec.field_width, 0);
  772. int i, chunksz;
  773. bool first = true;
  774. /* reused to print numbers */
  775. spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
  776. chunksz = nr_bits & (CHUNKSZ - 1);
  777. if (chunksz == 0)
  778. chunksz = CHUNKSZ;
  779. i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
  780. for (; i >= 0; i -= CHUNKSZ) {
  781. u32 chunkmask, val;
  782. int word, bit;
  783. chunkmask = ((1ULL << chunksz) - 1);
  784. word = i / BITS_PER_LONG;
  785. bit = i % BITS_PER_LONG;
  786. val = (bitmap[word] >> bit) & chunkmask;
  787. if (!first) {
  788. if (buf < end)
  789. *buf = ',';
  790. buf++;
  791. }
  792. first = false;
  793. spec.field_width = DIV_ROUND_UP(chunksz, 4);
  794. buf = number(buf, end, val, spec);
  795. chunksz = CHUNKSZ;
  796. }
  797. return buf;
  798. }
  799. static noinline_for_stack
  800. char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
  801. struct printf_spec spec, const char *fmt)
  802. {
  803. int nr_bits = max_t(int, spec.field_width, 0);
  804. /* current bit is 'cur', most recently seen range is [rbot, rtop] */
  805. int cur, rbot, rtop;
  806. bool first = true;
  807. rbot = cur = find_first_bit(bitmap, nr_bits);
  808. while (cur < nr_bits) {
  809. rtop = cur;
  810. cur = find_next_bit(bitmap, nr_bits, cur + 1);
  811. if (cur < nr_bits && cur <= rtop + 1)
  812. continue;
  813. if (!first) {
  814. if (buf < end)
  815. *buf = ',';
  816. buf++;
  817. }
  818. first = false;
  819. buf = number(buf, end, rbot, default_dec_spec);
  820. if (rbot < rtop) {
  821. if (buf < end)
  822. *buf = '-';
  823. buf++;
  824. buf = number(buf, end, rtop, default_dec_spec);
  825. }
  826. rbot = cur;
  827. }
  828. return buf;
  829. }
  830. static noinline_for_stack
  831. char *mac_address_string(char *buf, char *end, u8 *addr,
  832. struct printf_spec spec, const char *fmt)
  833. {
  834. char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
  835. char *p = mac_addr;
  836. int i;
  837. char separator;
  838. bool reversed = false;
  839. switch (fmt[1]) {
  840. case 'F':
  841. separator = '-';
  842. break;
  843. case 'R':
  844. reversed = true;
  845. /* fall through */
  846. default:
  847. separator = ':';
  848. break;
  849. }
  850. for (i = 0; i < 6; i++) {
  851. if (reversed)
  852. p = hex_byte_pack(p, addr[5 - i]);
  853. else
  854. p = hex_byte_pack(p, addr[i]);
  855. if (fmt[0] == 'M' && i != 5)
  856. *p++ = separator;
  857. }
  858. *p = '\0';
  859. return string(buf, end, mac_addr, spec);
  860. }
  861. static noinline_for_stack
  862. char *ip4_string(char *p, const u8 *addr, const char *fmt)
  863. {
  864. int i;
  865. bool leading_zeros = (fmt[0] == 'i');
  866. int index;
  867. int step;
  868. switch (fmt[2]) {
  869. case 'h':
  870. #ifdef __BIG_ENDIAN
  871. index = 0;
  872. step = 1;
  873. #else
  874. index = 3;
  875. step = -1;
  876. #endif
  877. break;
  878. case 'l':
  879. index = 3;
  880. step = -1;
  881. break;
  882. case 'n':
  883. case 'b':
  884. default:
  885. index = 0;
  886. step = 1;
  887. break;
  888. }
  889. for (i = 0; i < 4; i++) {
  890. char temp[4] __aligned(2); /* hold each IP quad in reverse order */
  891. int digits = put_dec_trunc8(temp, addr[index]) - temp;
  892. if (leading_zeros) {
  893. if (digits < 3)
  894. *p++ = '0';
  895. if (digits < 2)
  896. *p++ = '0';
  897. }
  898. /* reverse the digits in the quad */
  899. while (digits--)
  900. *p++ = temp[digits];
  901. if (i < 3)
  902. *p++ = '.';
  903. index += step;
  904. }
  905. *p = '\0';
  906. return p;
  907. }
  908. static noinline_for_stack
  909. char *ip6_compressed_string(char *p, const char *addr)
  910. {
  911. int i, j, range;
  912. unsigned char zerolength[8];
  913. int longest = 1;
  914. int colonpos = -1;
  915. u16 word;
  916. u8 hi, lo;
  917. bool needcolon = false;
  918. bool useIPv4;
  919. struct in6_addr in6;
  920. memcpy(&in6, addr, sizeof(struct in6_addr));
  921. useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
  922. memset(zerolength, 0, sizeof(zerolength));
  923. if (useIPv4)
  924. range = 6;
  925. else
  926. range = 8;
  927. /* find position of longest 0 run */
  928. for (i = 0; i < range; i++) {
  929. for (j = i; j < range; j++) {
  930. if (in6.s6_addr16[j] != 0)
  931. break;
  932. zerolength[i]++;
  933. }
  934. }
  935. for (i = 0; i < range; i++) {
  936. if (zerolength[i] > longest) {
  937. longest = zerolength[i];
  938. colonpos = i;
  939. }
  940. }
  941. if (longest == 1) /* don't compress a single 0 */
  942. colonpos = -1;
  943. /* emit address */
  944. for (i = 0; i < range; i++) {
  945. if (i == colonpos) {
  946. if (needcolon || i == 0)
  947. *p++ = ':';
  948. *p++ = ':';
  949. needcolon = false;
  950. i += longest - 1;
  951. continue;
  952. }
  953. if (needcolon) {
  954. *p++ = ':';
  955. needcolon = false;
  956. }
  957. /* hex u16 without leading 0s */
  958. word = ntohs(in6.s6_addr16[i]);
  959. hi = word >> 8;
  960. lo = word & 0xff;
  961. if (hi) {
  962. if (hi > 0x0f)
  963. p = hex_byte_pack(p, hi);
  964. else
  965. *p++ = hex_asc_lo(hi);
  966. p = hex_byte_pack(p, lo);
  967. }
  968. else if (lo > 0x0f)
  969. p = hex_byte_pack(p, lo);
  970. else
  971. *p++ = hex_asc_lo(lo);
  972. needcolon = true;
  973. }
  974. if (useIPv4) {
  975. if (needcolon)
  976. *p++ = ':';
  977. p = ip4_string(p, &in6.s6_addr[12], "I4");
  978. }
  979. *p = '\0';
  980. return p;
  981. }
  982. static noinline_for_stack
  983. char *ip6_string(char *p, const char *addr, const char *fmt)
  984. {
  985. int i;
  986. for (i = 0; i < 8; i++) {
  987. p = hex_byte_pack(p, *addr++);
  988. p = hex_byte_pack(p, *addr++);
  989. if (fmt[0] == 'I' && i != 7)
  990. *p++ = ':';
  991. }
  992. *p = '\0';
  993. return p;
  994. }
  995. static noinline_for_stack
  996. char *ip6_addr_string(char *buf, char *end, const u8 *addr,
  997. struct printf_spec spec, const char *fmt)
  998. {
  999. char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
  1000. if (fmt[0] == 'I' && fmt[2] == 'c')
  1001. ip6_compressed_string(ip6_addr, addr);
  1002. else
  1003. ip6_string(ip6_addr, addr, fmt);
  1004. return string(buf, end, ip6_addr, spec);
  1005. }
  1006. static noinline_for_stack
  1007. char *ip4_addr_string(char *buf, char *end, const u8 *addr,
  1008. struct printf_spec spec, const char *fmt)
  1009. {
  1010. char ip4_addr[sizeof("255.255.255.255")];
  1011. ip4_string(ip4_addr, addr, fmt);
  1012. return string(buf, end, ip4_addr, spec);
  1013. }
  1014. static noinline_for_stack
  1015. char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
  1016. struct printf_spec spec, const char *fmt)
  1017. {
  1018. bool have_p = false, have_s = false, have_f = false, have_c = false;
  1019. char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
  1020. sizeof(":12345") + sizeof("/123456789") +
  1021. sizeof("%1234567890")];
  1022. char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
  1023. const u8 *addr = (const u8 *) &sa->sin6_addr;
  1024. char fmt6[2] = { fmt[0], '6' };
  1025. u8 off = 0;
  1026. fmt++;
  1027. while (isalpha(*++fmt)) {
  1028. switch (*fmt) {
  1029. case 'p':
  1030. have_p = true;
  1031. break;
  1032. case 'f':
  1033. have_f = true;
  1034. break;
  1035. case 's':
  1036. have_s = true;
  1037. break;
  1038. case 'c':
  1039. have_c = true;
  1040. break;
  1041. }
  1042. }
  1043. if (have_p || have_s || have_f) {
  1044. *p = '[';
  1045. off = 1;
  1046. }
  1047. if (fmt6[0] == 'I' && have_c)
  1048. p = ip6_compressed_string(ip6_addr + off, addr);
  1049. else
  1050. p = ip6_string(ip6_addr + off, addr, fmt6);
  1051. if (have_p || have_s || have_f)
  1052. *p++ = ']';
  1053. if (have_p) {
  1054. *p++ = ':';
  1055. p = number(p, pend, ntohs(sa->sin6_port), spec);
  1056. }
  1057. if (have_f) {
  1058. *p++ = '/';
  1059. p = number(p, pend, ntohl(sa->sin6_flowinfo &
  1060. IPV6_FLOWINFO_MASK), spec);
  1061. }
  1062. if (have_s) {
  1063. *p++ = '%';
  1064. p = number(p, pend, sa->sin6_scope_id, spec);
  1065. }
  1066. *p = '\0';
  1067. return string(buf, end, ip6_addr, spec);
  1068. }
  1069. static noinline_for_stack
  1070. char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
  1071. struct printf_spec spec, const char *fmt)
  1072. {
  1073. bool have_p = false;
  1074. char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
  1075. char *pend = ip4_addr + sizeof(ip4_addr);
  1076. const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
  1077. char fmt4[3] = { fmt[0], '4', 0 };
  1078. fmt++;
  1079. while (isalpha(*++fmt)) {
  1080. switch (*fmt) {
  1081. case 'p':
  1082. have_p = true;
  1083. break;
  1084. case 'h':
  1085. case 'l':
  1086. case 'n':
  1087. case 'b':
  1088. fmt4[2] = *fmt;
  1089. break;
  1090. }
  1091. }
  1092. p = ip4_string(ip4_addr, addr, fmt4);
  1093. if (have_p) {
  1094. *p++ = ':';
  1095. p = number(p, pend, ntohs(sa->sin_port), spec);
  1096. }
  1097. *p = '\0';
  1098. return string(buf, end, ip4_addr, spec);
  1099. }
  1100. static noinline_for_stack
  1101. char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
  1102. const char *fmt)
  1103. {
  1104. bool found = true;
  1105. int count = 1;
  1106. unsigned int flags = 0;
  1107. int len;
  1108. if (spec.field_width == 0)
  1109. return buf; /* nothing to print */
  1110. if (ZERO_OR_NULL_PTR(addr))
  1111. return string(buf, end, NULL, spec); /* NULL pointer */
  1112. do {
  1113. switch (fmt[count++]) {
  1114. case 'a':
  1115. flags |= ESCAPE_ANY;
  1116. break;
  1117. case 'c':
  1118. flags |= ESCAPE_SPECIAL;
  1119. break;
  1120. case 'h':
  1121. flags |= ESCAPE_HEX;
  1122. break;
  1123. case 'n':
  1124. flags |= ESCAPE_NULL;
  1125. break;
  1126. case 'o':
  1127. flags |= ESCAPE_OCTAL;
  1128. break;
  1129. case 'p':
  1130. flags |= ESCAPE_NP;
  1131. break;
  1132. case 's':
  1133. flags |= ESCAPE_SPACE;
  1134. break;
  1135. default:
  1136. found = false;
  1137. break;
  1138. }
  1139. } while (found);
  1140. if (!flags)
  1141. flags = ESCAPE_ANY_NP;
  1142. len = spec.field_width < 0 ? 1 : spec.field_width;
  1143. /*
  1144. * string_escape_mem() writes as many characters as it can to
  1145. * the given buffer, and returns the total size of the output
  1146. * had the buffer been big enough.
  1147. */
  1148. buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
  1149. return buf;
  1150. }
  1151. static noinline_for_stack
  1152. char *uuid_string(char *buf, char *end, const u8 *addr,
  1153. struct printf_spec spec, const char *fmt)
  1154. {
  1155. char uuid[UUID_STRING_LEN + 1];
  1156. char *p = uuid;
  1157. int i;
  1158. const u8 *index = uuid_index;
  1159. bool uc = false;
  1160. switch (*(++fmt)) {
  1161. case 'L':
  1162. uc = true; /* fall-through */
  1163. case 'l':
  1164. index = guid_index;
  1165. break;
  1166. case 'B':
  1167. uc = true;
  1168. break;
  1169. }
  1170. for (i = 0; i < 16; i++) {
  1171. if (uc)
  1172. p = hex_byte_pack_upper(p, addr[index[i]]);
  1173. else
  1174. p = hex_byte_pack(p, addr[index[i]]);
  1175. switch (i) {
  1176. case 3:
  1177. case 5:
  1178. case 7:
  1179. case 9:
  1180. *p++ = '-';
  1181. break;
  1182. }
  1183. }
  1184. *p = 0;
  1185. return string(buf, end, uuid, spec);
  1186. }
  1187. int kptr_restrict __read_mostly;
  1188. static noinline_for_stack
  1189. char *restricted_pointer(char *buf, char *end, const void *ptr,
  1190. struct printf_spec spec)
  1191. {
  1192. spec.base = 16;
  1193. spec.flags |= SMALL;
  1194. if (spec.field_width == -1) {
  1195. spec.field_width = 2 * sizeof(ptr);
  1196. spec.flags |= ZEROPAD;
  1197. }
  1198. switch (kptr_restrict) {
  1199. case 0:
  1200. /* Always print %pK values */
  1201. break;
  1202. case 1: {
  1203. const struct cred *cred;
  1204. /*
  1205. * kptr_restrict==1 cannot be used in IRQ context
  1206. * because its test for CAP_SYSLOG would be meaningless.
  1207. */
  1208. if (in_irq() || in_serving_softirq() || in_nmi())
  1209. return string(buf, end, "pK-error", spec);
  1210. /*
  1211. * Only print the real pointer value if the current
  1212. * process has CAP_SYSLOG and is running with the
  1213. * same credentials it started with. This is because
  1214. * access to files is checked at open() time, but %pK
  1215. * checks permission at read() time. We don't want to
  1216. * leak pointer values if a binary opens a file using
  1217. * %pK and then elevates privileges before reading it.
  1218. */
  1219. cred = current_cred();
  1220. if (!has_capability_noaudit(current, CAP_SYSLOG) ||
  1221. !uid_eq(cred->euid, cred->uid) ||
  1222. !gid_eq(cred->egid, cred->gid))
  1223. ptr = NULL;
  1224. break;
  1225. }
  1226. case 2:
  1227. default:
  1228. /* Always print 0's for %pK */
  1229. ptr = NULL;
  1230. break;
  1231. }
  1232. return number(buf, end, (unsigned long)ptr, spec);
  1233. }
  1234. static noinline_for_stack
  1235. char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
  1236. {
  1237. unsigned long long num;
  1238. int size;
  1239. switch (fmt[1]) {
  1240. case 'F':
  1241. num = *(const netdev_features_t *)addr;
  1242. size = sizeof(netdev_features_t);
  1243. break;
  1244. default:
  1245. num = (unsigned long)addr;
  1246. size = sizeof(unsigned long);
  1247. break;
  1248. }
  1249. return special_hex_number(buf, end, num, size);
  1250. }
  1251. static noinline_for_stack
  1252. char *address_val(char *buf, char *end, const void *addr, const char *fmt)
  1253. {
  1254. unsigned long long num;
  1255. int size;
  1256. switch (fmt[1]) {
  1257. case 'd':
  1258. num = *(const dma_addr_t *)addr;
  1259. size = sizeof(dma_addr_t);
  1260. break;
  1261. case 'p':
  1262. default:
  1263. num = *(const phys_addr_t *)addr;
  1264. size = sizeof(phys_addr_t);
  1265. break;
  1266. }
  1267. return special_hex_number(buf, end, num, size);
  1268. }
  1269. static noinline_for_stack
  1270. char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
  1271. const char *fmt)
  1272. {
  1273. if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
  1274. return string(buf, end, NULL, spec);
  1275. switch (fmt[1]) {
  1276. case 'r':
  1277. return number(buf, end, clk_get_rate(clk), spec);
  1278. case 'n':
  1279. default:
  1280. #ifdef CONFIG_COMMON_CLK
  1281. return string(buf, end, __clk_get_name(clk), spec);
  1282. #else
  1283. return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
  1284. #endif
  1285. }
  1286. }
  1287. static
  1288. char *format_flags(char *buf, char *end, unsigned long flags,
  1289. const struct trace_print_flags *names)
  1290. {
  1291. unsigned long mask;
  1292. const struct printf_spec numspec = {
  1293. .flags = SPECIAL|SMALL,
  1294. .field_width = -1,
  1295. .precision = -1,
  1296. .base = 16,
  1297. };
  1298. for ( ; flags && names->name; names++) {
  1299. mask = names->mask;
  1300. if ((flags & mask) != mask)
  1301. continue;
  1302. buf = string(buf, end, names->name, default_str_spec);
  1303. flags &= ~mask;
  1304. if (flags) {
  1305. if (buf < end)
  1306. *buf = '|';
  1307. buf++;
  1308. }
  1309. }
  1310. if (flags)
  1311. buf = number(buf, end, flags, numspec);
  1312. return buf;
  1313. }
  1314. static noinline_for_stack
  1315. char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
  1316. {
  1317. unsigned long flags;
  1318. const struct trace_print_flags *names;
  1319. switch (fmt[1]) {
  1320. case 'p':
  1321. flags = *(unsigned long *)flags_ptr;
  1322. /* Remove zone id */
  1323. flags &= (1UL << NR_PAGEFLAGS) - 1;
  1324. names = pageflag_names;
  1325. break;
  1326. case 'v':
  1327. flags = *(unsigned long *)flags_ptr;
  1328. names = vmaflag_names;
  1329. break;
  1330. case 'g':
  1331. flags = *(gfp_t *)flags_ptr;
  1332. names = gfpflag_names;
  1333. break;
  1334. default:
  1335. WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
  1336. return buf;
  1337. }
  1338. return format_flags(buf, end, flags, names);
  1339. }
  1340. static const char *device_node_name_for_depth(const struct device_node *np, int depth)
  1341. {
  1342. for ( ; np && depth; depth--)
  1343. np = np->parent;
  1344. return kbasename(np->full_name);
  1345. }
  1346. static noinline_for_stack
  1347. char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
  1348. {
  1349. int depth;
  1350. const struct device_node *parent = np->parent;
  1351. /* special case for root node */
  1352. if (!parent)
  1353. return string(buf, end, "/", default_str_spec);
  1354. for (depth = 0; parent->parent; depth++)
  1355. parent = parent->parent;
  1356. for ( ; depth >= 0; depth--) {
  1357. buf = string(buf, end, "/", default_str_spec);
  1358. buf = string(buf, end, device_node_name_for_depth(np, depth),
  1359. default_str_spec);
  1360. }
  1361. return buf;
  1362. }
  1363. static noinline_for_stack
  1364. char *device_node_string(char *buf, char *end, struct device_node *dn,
  1365. struct printf_spec spec, const char *fmt)
  1366. {
  1367. char tbuf[sizeof("xxxx") + 1];
  1368. const char *p;
  1369. int ret;
  1370. char *buf_start = buf;
  1371. struct property *prop;
  1372. bool has_mult, pass;
  1373. static const struct printf_spec num_spec = {
  1374. .flags = SMALL,
  1375. .field_width = -1,
  1376. .precision = -1,
  1377. .base = 10,
  1378. };
  1379. struct printf_spec str_spec = spec;
  1380. str_spec.field_width = -1;
  1381. if (!IS_ENABLED(CONFIG_OF))
  1382. return string(buf, end, "(!OF)", spec);
  1383. if ((unsigned long)dn < PAGE_SIZE)
  1384. return string(buf, end, "(null)", spec);
  1385. /* simple case without anything any more format specifiers */
  1386. fmt++;
  1387. if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
  1388. fmt = "f";
  1389. for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
  1390. if (pass) {
  1391. if (buf < end)
  1392. *buf = ':';
  1393. buf++;
  1394. }
  1395. switch (*fmt) {
  1396. case 'f': /* full_name */
  1397. buf = device_node_gen_full_name(dn, buf, end);
  1398. break;
  1399. case 'n': /* name */
  1400. buf = string(buf, end, dn->name, str_spec);
  1401. break;
  1402. case 'p': /* phandle */
  1403. buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
  1404. break;
  1405. case 'P': /* path-spec */
  1406. p = kbasename(of_node_full_name(dn));
  1407. if (!p[1])
  1408. p = "/";
  1409. buf = string(buf, end, p, str_spec);
  1410. break;
  1411. case 'F': /* flags */
  1412. tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
  1413. tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
  1414. tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
  1415. tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
  1416. tbuf[4] = 0;
  1417. buf = string(buf, end, tbuf, str_spec);
  1418. break;
  1419. case 'c': /* major compatible string */
  1420. ret = of_property_read_string(dn, "compatible", &p);
  1421. if (!ret)
  1422. buf = string(buf, end, p, str_spec);
  1423. break;
  1424. case 'C': /* full compatible string */
  1425. has_mult = false;
  1426. of_property_for_each_string(dn, "compatible", prop, p) {
  1427. if (has_mult)
  1428. buf = string(buf, end, ",", str_spec);
  1429. buf = string(buf, end, "\"", str_spec);
  1430. buf = string(buf, end, p, str_spec);
  1431. buf = string(buf, end, "\"", str_spec);
  1432. has_mult = true;
  1433. }
  1434. break;
  1435. default:
  1436. break;
  1437. }
  1438. }
  1439. return widen_string(buf, buf - buf_start, end, spec);
  1440. }
  1441. static noinline_for_stack
  1442. char *pointer_string(char *buf, char *end, const void *ptr,
  1443. struct printf_spec spec)
  1444. {
  1445. spec.base = 16;
  1446. spec.flags |= SMALL;
  1447. if (spec.field_width == -1) {
  1448. spec.field_width = 2 * sizeof(ptr);
  1449. spec.flags |= ZEROPAD;
  1450. }
  1451. return number(buf, end, (unsigned long int)ptr, spec);
  1452. }
  1453. static bool have_filled_random_ptr_key __read_mostly;
  1454. static siphash_key_t ptr_key __read_mostly;
  1455. static void fill_random_ptr_key(struct random_ready_callback *unused)
  1456. {
  1457. get_random_bytes(&ptr_key, sizeof(ptr_key));
  1458. /*
  1459. * have_filled_random_ptr_key==true is dependent on get_random_bytes().
  1460. * ptr_to_id() needs to see have_filled_random_ptr_key==true
  1461. * after get_random_bytes() returns.
  1462. */
  1463. smp_mb();
  1464. WRITE_ONCE(have_filled_random_ptr_key, true);
  1465. }
  1466. static struct random_ready_callback random_ready = {
  1467. .func = fill_random_ptr_key
  1468. };
  1469. static int __init initialize_ptr_random(void)
  1470. {
  1471. int ret = add_random_ready_callback(&random_ready);
  1472. if (!ret) {
  1473. return 0;
  1474. } else if (ret == -EALREADY) {
  1475. fill_random_ptr_key(&random_ready);
  1476. return 0;
  1477. }
  1478. return ret;
  1479. }
  1480. early_initcall(initialize_ptr_random);
  1481. /* Maps a pointer to a 32 bit unique identifier. */
  1482. static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
  1483. {
  1484. unsigned long hashval;
  1485. const int default_width = 2 * sizeof(ptr);
  1486. if (unlikely(!have_filled_random_ptr_key)) {
  1487. spec.field_width = default_width;
  1488. /* string length must be less than default_width */
  1489. return string(buf, end, "(ptrval)", spec);
  1490. }
  1491. #ifdef CONFIG_64BIT
  1492. hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
  1493. /*
  1494. * Mask off the first 32 bits, this makes explicit that we have
  1495. * modified the address (and 32 bits is plenty for a unique ID).
  1496. */
  1497. hashval = hashval & 0xffffffff;
  1498. #else
  1499. hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
  1500. #endif
  1501. spec.flags |= SMALL;
  1502. if (spec.field_width == -1) {
  1503. spec.field_width = default_width;
  1504. spec.flags |= ZEROPAD;
  1505. }
  1506. spec.base = 16;
  1507. return number(buf, end, hashval, spec);
  1508. }
  1509. /*
  1510. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  1511. * by an extra set of alphanumeric characters that are extended format
  1512. * specifiers.
  1513. *
  1514. * Please update scripts/checkpatch.pl when adding/removing conversion
  1515. * characters. (Search for "check for vsprintf extension").
  1516. *
  1517. * Right now we handle:
  1518. *
  1519. * - 'F' For symbolic function descriptor pointers with offset
  1520. * - 'f' For simple symbolic function names without offset
  1521. * - 'S' For symbolic direct pointers with offset
  1522. * - 's' For symbolic direct pointers without offset
  1523. * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
  1524. * - 'B' For backtraced symbolic direct pointers with offset
  1525. * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
  1526. * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
  1527. * - 'b[l]' For a bitmap, the number of bits is determined by the field
  1528. * width which must be explicitly specified either as part of the
  1529. * format string '%32b[l]' or through '%*b[l]', [l] selects
  1530. * range-list format instead of hex format
  1531. * - 'M' For a 6-byte MAC address, it prints the address in the
  1532. * usual colon-separated hex notation
  1533. * - 'm' For a 6-byte MAC address, it prints the hex address without colons
  1534. * - 'MF' For a 6-byte MAC FDDI address, it prints the address
  1535. * with a dash-separated hex notation
  1536. * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
  1537. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
  1538. * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
  1539. * IPv6 uses colon separated network-order 16 bit hex with leading 0's
  1540. * [S][pfs]
  1541. * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  1542. * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  1543. * - 'i' [46] for 'raw' IPv4/IPv6 addresses
  1544. * IPv6 omits the colons (01020304...0f)
  1545. * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
  1546. * [S][pfs]
  1547. * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  1548. * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  1549. * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
  1550. * - 'I[6S]c' for IPv6 addresses printed as specified by
  1551. * http://tools.ietf.org/html/rfc5952
  1552. * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
  1553. * of the following flags (see string_escape_mem() for the
  1554. * details):
  1555. * a - ESCAPE_ANY
  1556. * c - ESCAPE_SPECIAL
  1557. * h - ESCAPE_HEX
  1558. * n - ESCAPE_NULL
  1559. * o - ESCAPE_OCTAL
  1560. * p - ESCAPE_NP
  1561. * s - ESCAPE_SPACE
  1562. * By default ESCAPE_ANY_NP is used.
  1563. * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
  1564. * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  1565. * Options for %pU are:
  1566. * b big endian lower case hex (default)
  1567. * B big endian UPPER case hex
  1568. * l little endian lower case hex
  1569. * L little endian UPPER case hex
  1570. * big endian output byte order is:
  1571. * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
  1572. * little endian output byte order is:
  1573. * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
  1574. * - 'V' For a struct va_format which contains a format string * and va_list *,
  1575. * call vsnprintf(->format, *->va_list).
  1576. * Implements a "recursive vsnprintf".
  1577. * Do not use this feature without some mechanism to verify the
  1578. * correctness of the format string and va_list arguments.
  1579. * - 'K' For a kernel pointer that should be hidden from unprivileged users
  1580. * - 'NF' For a netdev_features_t
  1581. * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
  1582. * a certain separator (' ' by default):
  1583. * C colon
  1584. * D dash
  1585. * N no separator
  1586. * The maximum supported length is 64 bytes of the input. Consider
  1587. * to use print_hex_dump() for the larger input.
  1588. * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
  1589. * (default assumed to be phys_addr_t, passed by reference)
  1590. * - 'd[234]' For a dentry name (optionally 2-4 last components)
  1591. * - 'D[234]' Same as 'd' but for a struct file
  1592. * - 'g' For block_device name (gendisk + partition number)
  1593. * - 'C' For a clock, it prints the name (Common Clock Framework) or address
  1594. * (legacy clock framework) of the clock
  1595. * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
  1596. * (legacy clock framework) of the clock
  1597. * - 'Cr' For a clock, it prints the current rate of the clock
  1598. * - 'G' For flags to be printed as a collection of symbolic strings that would
  1599. * construct the specific value. Supported flags given by option:
  1600. * p page flags (see struct page) given as pointer to unsigned long
  1601. * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
  1602. * v vma flags (VM_*) given as pointer to unsigned long
  1603. * - 'O' For a kobject based struct. Must be one of the following:
  1604. * - 'OF[fnpPcCF]' For a device tree object
  1605. * Without any optional arguments prints the full_name
  1606. * f device node full_name
  1607. * n device node name
  1608. * p device node phandle
  1609. * P device node path spec (name + @unit)
  1610. * F device node flags
  1611. * c major compatible string
  1612. * C full compatible string
  1613. *
  1614. * - 'x' For printing the address. Equivalent to "%lx".
  1615. *
  1616. * ** When making changes please also update:
  1617. * Documentation/core-api/printk-formats.rst
  1618. *
  1619. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  1620. * function pointers are really function descriptors, which contain a
  1621. * pointer to the real address.
  1622. *
  1623. * Note: The default behaviour (unadorned %p) is to hash the address,
  1624. * rendering it useful as a unique identifier.
  1625. */
  1626. static noinline_for_stack
  1627. char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  1628. struct printf_spec spec)
  1629. {
  1630. const int default_width = 2 * sizeof(void *);
  1631. if (!ptr && *fmt != 'K' && *fmt != 'x') {
  1632. /*
  1633. * Print (null) with the same width as a pointer so it makes
  1634. * tabular output look nice.
  1635. */
  1636. if (spec.field_width == -1)
  1637. spec.field_width = default_width;
  1638. return string(buf, end, "(null)", spec);
  1639. }
  1640. switch (*fmt) {
  1641. case 'F':
  1642. case 'f':
  1643. case 'S':
  1644. case 's':
  1645. ptr = dereference_symbol_descriptor(ptr);
  1646. /* Fallthrough */
  1647. case 'B':
  1648. return symbol_string(buf, end, ptr, spec, fmt);
  1649. case 'R':
  1650. case 'r':
  1651. return resource_string(buf, end, ptr, spec, fmt);
  1652. case 'h':
  1653. return hex_string(buf, end, ptr, spec, fmt);
  1654. case 'b':
  1655. switch (fmt[1]) {
  1656. case 'l':
  1657. return bitmap_list_string(buf, end, ptr, spec, fmt);
  1658. default:
  1659. return bitmap_string(buf, end, ptr, spec, fmt);
  1660. }
  1661. case 'M': /* Colon separated: 00:01:02:03:04:05 */
  1662. case 'm': /* Contiguous: 000102030405 */
  1663. /* [mM]F (FDDI) */
  1664. /* [mM]R (Reverse order; Bluetooth) */
  1665. return mac_address_string(buf, end, ptr, spec, fmt);
  1666. case 'I': /* Formatted IP supported
  1667. * 4: 1.2.3.4
  1668. * 6: 0001:0203:...:0708
  1669. * 6c: 1::708 or 1::1.2.3.4
  1670. */
  1671. case 'i': /* Contiguous:
  1672. * 4: 001.002.003.004
  1673. * 6: 000102...0f
  1674. */
  1675. switch (fmt[1]) {
  1676. case '6':
  1677. return ip6_addr_string(buf, end, ptr, spec, fmt);
  1678. case '4':
  1679. return ip4_addr_string(buf, end, ptr, spec, fmt);
  1680. case 'S': {
  1681. const union {
  1682. struct sockaddr raw;
  1683. struct sockaddr_in v4;
  1684. struct sockaddr_in6 v6;
  1685. } *sa = ptr;
  1686. switch (sa->raw.sa_family) {
  1687. case AF_INET:
  1688. return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
  1689. case AF_INET6:
  1690. return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
  1691. default:
  1692. return string(buf, end, "(invalid address)", spec);
  1693. }}
  1694. }
  1695. break;
  1696. case 'E':
  1697. return escaped_string(buf, end, ptr, spec, fmt);
  1698. case 'U':
  1699. return uuid_string(buf, end, ptr, spec, fmt);
  1700. case 'V':
  1701. {
  1702. va_list va;
  1703. va_copy(va, *((struct va_format *)ptr)->va);
  1704. buf += vsnprintf(buf, end > buf ? end - buf : 0,
  1705. ((struct va_format *)ptr)->fmt, va);
  1706. va_end(va);
  1707. return buf;
  1708. }
  1709. case 'K':
  1710. if (!kptr_restrict)
  1711. break;
  1712. return restricted_pointer(buf, end, ptr, spec);
  1713. case 'N':
  1714. return netdev_bits(buf, end, ptr, fmt);
  1715. case 'a':
  1716. return address_val(buf, end, ptr, fmt);
  1717. case 'd':
  1718. return dentry_name(buf, end, ptr, spec, fmt);
  1719. case 'C':
  1720. return clock(buf, end, ptr, spec, fmt);
  1721. case 'D':
  1722. return dentry_name(buf, end,
  1723. ((const struct file *)ptr)->f_path.dentry,
  1724. spec, fmt);
  1725. #ifdef CONFIG_BLOCK
  1726. case 'g':
  1727. return bdev_name(buf, end, ptr, spec, fmt);
  1728. #endif
  1729. case 'G':
  1730. return flags_string(buf, end, ptr, fmt);
  1731. case 'O':
  1732. switch (fmt[1]) {
  1733. case 'F':
  1734. return device_node_string(buf, end, ptr, spec, fmt + 1);
  1735. }
  1736. case 'x':
  1737. return pointer_string(buf, end, ptr, spec);
  1738. }
  1739. /* default is to _not_ leak addresses, hash before printing */
  1740. return ptr_to_id(buf, end, ptr, spec);
  1741. }
  1742. /*
  1743. * Helper function to decode printf style format.
  1744. * Each call decode a token from the format and return the
  1745. * number of characters read (or likely the delta where it wants
  1746. * to go on the next call).
  1747. * The decoded token is returned through the parameters
  1748. *
  1749. * 'h', 'l', or 'L' for integer fields
  1750. * 'z' support added 23/7/1999 S.H.
  1751. * 'z' changed to 'Z' --davidm 1/25/99
  1752. * 'Z' changed to 'z' --adobriyan 2017-01-25
  1753. * 't' added for ptrdiff_t
  1754. *
  1755. * @fmt: the format string
  1756. * @type of the token returned
  1757. * @flags: various flags such as +, -, # tokens..
  1758. * @field_width: overwritten width
  1759. * @base: base of the number (octal, hex, ...)
  1760. * @precision: precision of a number
  1761. * @qualifier: qualifier of a number (long, size_t, ...)
  1762. */
  1763. static noinline_for_stack
  1764. int format_decode(const char *fmt, struct printf_spec *spec)
  1765. {
  1766. const char *start = fmt;
  1767. char qualifier;
  1768. /* we finished early by reading the field width */
  1769. if (spec->type == FORMAT_TYPE_WIDTH) {
  1770. if (spec->field_width < 0) {
  1771. spec->field_width = -spec->field_width;
  1772. spec->flags |= LEFT;
  1773. }
  1774. spec->type = FORMAT_TYPE_NONE;
  1775. goto precision;
  1776. }
  1777. /* we finished early by reading the precision */
  1778. if (spec->type == FORMAT_TYPE_PRECISION) {
  1779. if (spec->precision < 0)
  1780. spec->precision = 0;
  1781. spec->type = FORMAT_TYPE_NONE;
  1782. goto qualifier;
  1783. }
  1784. /* By default */
  1785. spec->type = FORMAT_TYPE_NONE;
  1786. for (; *fmt ; ++fmt) {
  1787. if (*fmt == '%')
  1788. break;
  1789. }
  1790. /* Return the current non-format string */
  1791. if (fmt != start || !*fmt)
  1792. return fmt - start;
  1793. /* Process flags */
  1794. spec->flags = 0;
  1795. while (1) { /* this also skips first '%' */
  1796. bool found = true;
  1797. ++fmt;
  1798. switch (*fmt) {
  1799. case '-': spec->flags |= LEFT; break;
  1800. case '+': spec->flags |= PLUS; break;
  1801. case ' ': spec->flags |= SPACE; break;
  1802. case '#': spec->flags |= SPECIAL; break;
  1803. case '0': spec->flags |= ZEROPAD; break;
  1804. default: found = false;
  1805. }
  1806. if (!found)
  1807. break;
  1808. }
  1809. /* get field width */
  1810. spec->field_width = -1;
  1811. if (isdigit(*fmt))
  1812. spec->field_width = skip_atoi(&fmt);
  1813. else if (*fmt == '*') {
  1814. /* it's the next argument */
  1815. spec->type = FORMAT_TYPE_WIDTH;
  1816. return ++fmt - start;
  1817. }
  1818. precision:
  1819. /* get the precision */
  1820. spec->precision = -1;
  1821. if (*fmt == '.') {
  1822. ++fmt;
  1823. if (isdigit(*fmt)) {
  1824. spec->precision = skip_atoi(&fmt);
  1825. if (spec->precision < 0)
  1826. spec->precision = 0;
  1827. } else if (*fmt == '*') {
  1828. /* it's the next argument */
  1829. spec->type = FORMAT_TYPE_PRECISION;
  1830. return ++fmt - start;
  1831. }
  1832. }
  1833. qualifier:
  1834. /* get the conversion qualifier */
  1835. qualifier = 0;
  1836. if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  1837. *fmt == 'z' || *fmt == 't') {
  1838. qualifier = *fmt++;
  1839. if (unlikely(qualifier == *fmt)) {
  1840. if (qualifier == 'l') {
  1841. qualifier = 'L';
  1842. ++fmt;
  1843. } else if (qualifier == 'h') {
  1844. qualifier = 'H';
  1845. ++fmt;
  1846. }
  1847. }
  1848. }
  1849. /* default base */
  1850. spec->base = 10;
  1851. switch (*fmt) {
  1852. case 'c':
  1853. spec->type = FORMAT_TYPE_CHAR;
  1854. return ++fmt - start;
  1855. case 's':
  1856. spec->type = FORMAT_TYPE_STR;
  1857. return ++fmt - start;
  1858. case 'p':
  1859. spec->type = FORMAT_TYPE_PTR;
  1860. return ++fmt - start;
  1861. case '%':
  1862. spec->type = FORMAT_TYPE_PERCENT_CHAR;
  1863. return ++fmt - start;
  1864. /* integer number formats - set up the flags and "break" */
  1865. case 'o':
  1866. spec->base = 8;
  1867. break;
  1868. case 'x':
  1869. spec->flags |= SMALL;
  1870. case 'X':
  1871. spec->base = 16;
  1872. break;
  1873. case 'd':
  1874. case 'i':
  1875. spec->flags |= SIGN;
  1876. case 'u':
  1877. break;
  1878. case 'n':
  1879. /*
  1880. * Since %n poses a greater security risk than
  1881. * utility, treat it as any other invalid or
  1882. * unsupported format specifier.
  1883. */
  1884. /* Fall-through */
  1885. default:
  1886. WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
  1887. spec->type = FORMAT_TYPE_INVALID;
  1888. return fmt - start;
  1889. }
  1890. if (qualifier == 'L')
  1891. spec->type = FORMAT_TYPE_LONG_LONG;
  1892. else if (qualifier == 'l') {
  1893. BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
  1894. spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
  1895. } else if (qualifier == 'z') {
  1896. spec->type = FORMAT_TYPE_SIZE_T;
  1897. } else if (qualifier == 't') {
  1898. spec->type = FORMAT_TYPE_PTRDIFF;
  1899. } else if (qualifier == 'H') {
  1900. BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
  1901. spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
  1902. } else if (qualifier == 'h') {
  1903. BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
  1904. spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
  1905. } else {
  1906. BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
  1907. spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
  1908. }
  1909. return ++fmt - start;
  1910. }
  1911. static void
  1912. set_field_width(struct printf_spec *spec, int width)
  1913. {
  1914. spec->field_width = width;
  1915. if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
  1916. spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
  1917. }
  1918. }
  1919. static void
  1920. set_precision(struct printf_spec *spec, int prec)
  1921. {
  1922. spec->precision = prec;
  1923. if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
  1924. spec->precision = clamp(prec, 0, PRECISION_MAX);
  1925. }
  1926. }
  1927. /**
  1928. * vsnprintf - Format a string and place it in a buffer
  1929. * @buf: The buffer to place the result into
  1930. * @size: The size of the buffer, including the trailing null space
  1931. * @fmt: The format string to use
  1932. * @args: Arguments for the format string
  1933. *
  1934. * This function generally follows C99 vsnprintf, but has some
  1935. * extensions and a few limitations:
  1936. *
  1937. * - ``%n`` is unsupported
  1938. * - ``%p*`` is handled by pointer()
  1939. *
  1940. * See pointer() or Documentation/core-api/printk-formats.rst for more
  1941. * extensive description.
  1942. *
  1943. * **Please update the documentation in both places when making changes**
  1944. *
  1945. * The return value is the number of characters which would
  1946. * be generated for the given input, excluding the trailing
  1947. * '\0', as per ISO C99. If you want to have the exact
  1948. * number of characters written into @buf as return value
  1949. * (not including the trailing '\0'), use vscnprintf(). If the
  1950. * return is greater than or equal to @size, the resulting
  1951. * string is truncated.
  1952. *
  1953. * If you're not already dealing with a va_list consider using snprintf().
  1954. */
  1955. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1956. {
  1957. unsigned long long num;
  1958. char *str, *end;
  1959. struct printf_spec spec = {0};
  1960. /* Reject out-of-range values early. Large positive sizes are
  1961. used for unknown buffer sizes. */
  1962. if (WARN_ON_ONCE(size > INT_MAX))
  1963. return 0;
  1964. str = buf;
  1965. end = buf + size;
  1966. /* Make sure end is always >= buf */
  1967. if (end < buf) {
  1968. end = ((void *)-1);
  1969. size = end - buf;
  1970. }
  1971. while (*fmt) {
  1972. const char *old_fmt = fmt;
  1973. int read = format_decode(fmt, &spec);
  1974. fmt += read;
  1975. switch (spec.type) {
  1976. case FORMAT_TYPE_NONE: {
  1977. int copy = read;
  1978. if (str < end) {
  1979. if (copy > end - str)
  1980. copy = end - str;
  1981. memcpy(str, old_fmt, copy);
  1982. }
  1983. str += read;
  1984. break;
  1985. }
  1986. case FORMAT_TYPE_WIDTH:
  1987. set_field_width(&spec, va_arg(args, int));
  1988. break;
  1989. case FORMAT_TYPE_PRECISION:
  1990. set_precision(&spec, va_arg(args, int));
  1991. break;
  1992. case FORMAT_TYPE_CHAR: {
  1993. char c;
  1994. if (!(spec.flags & LEFT)) {
  1995. while (--spec.field_width > 0) {
  1996. if (str < end)
  1997. *str = ' ';
  1998. ++str;
  1999. }
  2000. }
  2001. c = (unsigned char) va_arg(args, int);
  2002. if (str < end)
  2003. *str = c;
  2004. ++str;
  2005. while (--spec.field_width > 0) {
  2006. if (str < end)
  2007. *str = ' ';
  2008. ++str;
  2009. }
  2010. break;
  2011. }
  2012. case FORMAT_TYPE_STR:
  2013. str = string(str, end, va_arg(args, char *), spec);
  2014. break;
  2015. case FORMAT_TYPE_PTR:
  2016. str = pointer(fmt, str, end, va_arg(args, void *),
  2017. spec);
  2018. while (isalnum(*fmt))
  2019. fmt++;
  2020. break;
  2021. case FORMAT_TYPE_PERCENT_CHAR:
  2022. if (str < end)
  2023. *str = '%';
  2024. ++str;
  2025. break;
  2026. case FORMAT_TYPE_INVALID:
  2027. /*
  2028. * Presumably the arguments passed gcc's type
  2029. * checking, but there is no safe or sane way
  2030. * for us to continue parsing the format and
  2031. * fetching from the va_list; the remaining
  2032. * specifiers and arguments would be out of
  2033. * sync.
  2034. */
  2035. goto out;
  2036. default:
  2037. switch (spec.type) {
  2038. case FORMAT_TYPE_LONG_LONG:
  2039. num = va_arg(args, long long);
  2040. break;
  2041. case FORMAT_TYPE_ULONG:
  2042. num = va_arg(args, unsigned long);
  2043. break;
  2044. case FORMAT_TYPE_LONG:
  2045. num = va_arg(args, long);
  2046. break;
  2047. case FORMAT_TYPE_SIZE_T:
  2048. if (spec.flags & SIGN)
  2049. num = va_arg(args, ssize_t);
  2050. else
  2051. num = va_arg(args, size_t);
  2052. break;
  2053. case FORMAT_TYPE_PTRDIFF:
  2054. num = va_arg(args, ptrdiff_t);
  2055. break;
  2056. case FORMAT_TYPE_UBYTE:
  2057. num = (unsigned char) va_arg(args, int);
  2058. break;
  2059. case FORMAT_TYPE_BYTE:
  2060. num = (signed char) va_arg(args, int);
  2061. break;
  2062. case FORMAT_TYPE_USHORT:
  2063. num = (unsigned short) va_arg(args, int);
  2064. break;
  2065. case FORMAT_TYPE_SHORT:
  2066. num = (short) va_arg(args, int);
  2067. break;
  2068. case FORMAT_TYPE_INT:
  2069. num = (int) va_arg(args, int);
  2070. break;
  2071. default:
  2072. num = va_arg(args, unsigned int);
  2073. }
  2074. str = number(str, end, num, spec);
  2075. }
  2076. }
  2077. out:
  2078. if (size > 0) {
  2079. if (str < end)
  2080. *str = '\0';
  2081. else
  2082. end[-1] = '\0';
  2083. }
  2084. /* the trailing null byte doesn't count towards the total */
  2085. return str-buf;
  2086. }
  2087. EXPORT_SYMBOL(vsnprintf);
  2088. /**
  2089. * vscnprintf - Format a string and place it in a buffer
  2090. * @buf: The buffer to place the result into
  2091. * @size: The size of the buffer, including the trailing null space
  2092. * @fmt: The format string to use
  2093. * @args: Arguments for the format string
  2094. *
  2095. * The return value is the number of characters which have been written into
  2096. * the @buf not including the trailing '\0'. If @size is == 0 the function
  2097. * returns 0.
  2098. *
  2099. * If you're not already dealing with a va_list consider using scnprintf().
  2100. *
  2101. * See the vsnprintf() documentation for format string extensions over C99.
  2102. */
  2103. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  2104. {
  2105. int i;
  2106. i = vsnprintf(buf, size, fmt, args);
  2107. if (likely(i < size))
  2108. return i;
  2109. if (size != 0)
  2110. return size - 1;
  2111. return 0;
  2112. }
  2113. EXPORT_SYMBOL(vscnprintf);
  2114. /**
  2115. * snprintf - Format a string and place it in a buffer
  2116. * @buf: The buffer to place the result into
  2117. * @size: The size of the buffer, including the trailing null space
  2118. * @fmt: The format string to use
  2119. * @...: Arguments for the format string
  2120. *
  2121. * The return value is the number of characters which would be
  2122. * generated for the given input, excluding the trailing null,
  2123. * as per ISO C99. If the return is greater than or equal to
  2124. * @size, the resulting string is truncated.
  2125. *
  2126. * See the vsnprintf() documentation for format string extensions over C99.
  2127. */
  2128. int snprintf(char *buf, size_t size, const char *fmt, ...)
  2129. {
  2130. va_list args;
  2131. int i;
  2132. va_start(args, fmt);
  2133. i = vsnprintf(buf, size, fmt, args);
  2134. va_end(args);
  2135. return i;
  2136. }
  2137. EXPORT_SYMBOL(snprintf);
  2138. /**
  2139. * scnprintf - Format a string and place it in a buffer
  2140. * @buf: The buffer to place the result into
  2141. * @size: The size of the buffer, including the trailing null space
  2142. * @fmt: The format string to use
  2143. * @...: Arguments for the format string
  2144. *
  2145. * The return value is the number of characters written into @buf not including
  2146. * the trailing '\0'. If @size is == 0 the function returns 0.
  2147. */
  2148. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  2149. {
  2150. va_list args;
  2151. int i;
  2152. va_start(args, fmt);
  2153. i = vscnprintf(buf, size, fmt, args);
  2154. va_end(args);
  2155. return i;
  2156. }
  2157. EXPORT_SYMBOL(scnprintf);
  2158. /**
  2159. * vsprintf - Format a string and place it in a buffer
  2160. * @buf: The buffer to place the result into
  2161. * @fmt: The format string to use
  2162. * @args: Arguments for the format string
  2163. *
  2164. * The function returns the number of characters written
  2165. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  2166. * buffer overflows.
  2167. *
  2168. * If you're not already dealing with a va_list consider using sprintf().
  2169. *
  2170. * See the vsnprintf() documentation for format string extensions over C99.
  2171. */
  2172. int vsprintf(char *buf, const char *fmt, va_list args)
  2173. {
  2174. return vsnprintf(buf, INT_MAX, fmt, args);
  2175. }
  2176. EXPORT_SYMBOL(vsprintf);
  2177. /**
  2178. * sprintf - Format a string and place it in a buffer
  2179. * @buf: The buffer to place the result into
  2180. * @fmt: The format string to use
  2181. * @...: Arguments for the format string
  2182. *
  2183. * The function returns the number of characters written
  2184. * into @buf. Use snprintf() or scnprintf() in order to avoid
  2185. * buffer overflows.
  2186. *
  2187. * See the vsnprintf() documentation for format string extensions over C99.
  2188. */
  2189. int sprintf(char *buf, const char *fmt, ...)
  2190. {
  2191. va_list args;
  2192. int i;
  2193. va_start(args, fmt);
  2194. i = vsnprintf(buf, INT_MAX, fmt, args);
  2195. va_end(args);
  2196. return i;
  2197. }
  2198. EXPORT_SYMBOL(sprintf);
  2199. #ifdef CONFIG_BINARY_PRINTF
  2200. /*
  2201. * bprintf service:
  2202. * vbin_printf() - VA arguments to binary data
  2203. * bstr_printf() - Binary data to text string
  2204. */
  2205. /**
  2206. * vbin_printf - Parse a format string and place args' binary value in a buffer
  2207. * @bin_buf: The buffer to place args' binary value
  2208. * @size: The size of the buffer(by words(32bits), not characters)
  2209. * @fmt: The format string to use
  2210. * @args: Arguments for the format string
  2211. *
  2212. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  2213. * is skipped.
  2214. *
  2215. * The return value is the number of words(32bits) which would be generated for
  2216. * the given input.
  2217. *
  2218. * NOTE:
  2219. * If the return value is greater than @size, the resulting bin_buf is NOT
  2220. * valid for bstr_printf().
  2221. */
  2222. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  2223. {
  2224. struct printf_spec spec = {0};
  2225. char *str, *end;
  2226. int width;
  2227. str = (char *)bin_buf;
  2228. end = (char *)(bin_buf + size);
  2229. #define save_arg(type) \
  2230. ({ \
  2231. unsigned long long value; \
  2232. if (sizeof(type) == 8) { \
  2233. unsigned long long val8; \
  2234. str = PTR_ALIGN(str, sizeof(u32)); \
  2235. val8 = va_arg(args, unsigned long long); \
  2236. if (str + sizeof(type) <= end) { \
  2237. *(u32 *)str = *(u32 *)&val8; \
  2238. *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
  2239. } \
  2240. value = val8; \
  2241. } else { \
  2242. unsigned int val4; \
  2243. str = PTR_ALIGN(str, sizeof(type)); \
  2244. val4 = va_arg(args, int); \
  2245. if (str + sizeof(type) <= end) \
  2246. *(typeof(type) *)str = (type)(long)val4; \
  2247. value = (unsigned long long)val4; \
  2248. } \
  2249. str += sizeof(type); \
  2250. value; \
  2251. })
  2252. while (*fmt) {
  2253. int read = format_decode(fmt, &spec);
  2254. fmt += read;
  2255. switch (spec.type) {
  2256. case FORMAT_TYPE_NONE:
  2257. case FORMAT_TYPE_PERCENT_CHAR:
  2258. break;
  2259. case FORMAT_TYPE_INVALID:
  2260. goto out;
  2261. case FORMAT_TYPE_WIDTH:
  2262. case FORMAT_TYPE_PRECISION:
  2263. width = (int)save_arg(int);
  2264. /* Pointers may require the width */
  2265. if (*fmt == 'p')
  2266. set_field_width(&spec, width);
  2267. break;
  2268. case FORMAT_TYPE_CHAR:
  2269. save_arg(char);
  2270. break;
  2271. case FORMAT_TYPE_STR: {
  2272. const char *save_str = va_arg(args, char *);
  2273. size_t len;
  2274. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  2275. || (unsigned long)save_str < PAGE_SIZE)
  2276. save_str = "(null)";
  2277. len = strlen(save_str) + 1;
  2278. if (str + len < end)
  2279. memcpy(str, save_str, len);
  2280. str += len;
  2281. break;
  2282. }
  2283. case FORMAT_TYPE_PTR:
  2284. /* Dereferenced pointers must be done now */
  2285. switch (*fmt) {
  2286. /* Dereference of functions is still OK */
  2287. case 'S':
  2288. case 's':
  2289. case 'F':
  2290. case 'f':
  2291. save_arg(void *);
  2292. break;
  2293. default:
  2294. if (!isalnum(*fmt)) {
  2295. save_arg(void *);
  2296. break;
  2297. }
  2298. str = pointer(fmt, str, end, va_arg(args, void *),
  2299. spec);
  2300. if (str + 1 < end)
  2301. *str++ = '\0';
  2302. else
  2303. end[-1] = '\0'; /* Must be nul terminated */
  2304. }
  2305. /* skip all alphanumeric pointer suffixes */
  2306. while (isalnum(*fmt))
  2307. fmt++;
  2308. break;
  2309. default:
  2310. switch (spec.type) {
  2311. case FORMAT_TYPE_LONG_LONG:
  2312. save_arg(long long);
  2313. break;
  2314. case FORMAT_TYPE_ULONG:
  2315. case FORMAT_TYPE_LONG:
  2316. save_arg(unsigned long);
  2317. break;
  2318. case FORMAT_TYPE_SIZE_T:
  2319. save_arg(size_t);
  2320. break;
  2321. case FORMAT_TYPE_PTRDIFF:
  2322. save_arg(ptrdiff_t);
  2323. break;
  2324. case FORMAT_TYPE_UBYTE:
  2325. case FORMAT_TYPE_BYTE:
  2326. save_arg(char);
  2327. break;
  2328. case FORMAT_TYPE_USHORT:
  2329. case FORMAT_TYPE_SHORT:
  2330. save_arg(short);
  2331. break;
  2332. default:
  2333. save_arg(int);
  2334. }
  2335. }
  2336. }
  2337. out:
  2338. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  2339. #undef save_arg
  2340. }
  2341. EXPORT_SYMBOL_GPL(vbin_printf);
  2342. /**
  2343. * bstr_printf - Format a string from binary arguments and place it in a buffer
  2344. * @buf: The buffer to place the result into
  2345. * @size: The size of the buffer, including the trailing null space
  2346. * @fmt: The format string to use
  2347. * @bin_buf: Binary arguments for the format string
  2348. *
  2349. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  2350. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  2351. * a binary buffer that generated by vbin_printf.
  2352. *
  2353. * The format follows C99 vsnprintf, but has some extensions:
  2354. * see vsnprintf comment for details.
  2355. *
  2356. * The return value is the number of characters which would
  2357. * be generated for the given input, excluding the trailing
  2358. * '\0', as per ISO C99. If you want to have the exact
  2359. * number of characters written into @buf as return value
  2360. * (not including the trailing '\0'), use vscnprintf(). If the
  2361. * return is greater than or equal to @size, the resulting
  2362. * string is truncated.
  2363. */
  2364. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  2365. {
  2366. struct printf_spec spec = {0};
  2367. char *str, *end;
  2368. const char *args = (const char *)bin_buf;
  2369. if (WARN_ON_ONCE(size > INT_MAX))
  2370. return 0;
  2371. str = buf;
  2372. end = buf + size;
  2373. #define get_arg(type) \
  2374. ({ \
  2375. typeof(type) value; \
  2376. if (sizeof(type) == 8) { \
  2377. args = PTR_ALIGN(args, sizeof(u32)); \
  2378. *(u32 *)&value = *(u32 *)args; \
  2379. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  2380. } else { \
  2381. args = PTR_ALIGN(args, sizeof(type)); \
  2382. value = *(typeof(type) *)args; \
  2383. } \
  2384. args += sizeof(type); \
  2385. value; \
  2386. })
  2387. /* Make sure end is always >= buf */
  2388. if (end < buf) {
  2389. end = ((void *)-1);
  2390. size = end - buf;
  2391. }
  2392. while (*fmt) {
  2393. const char *old_fmt = fmt;
  2394. int read = format_decode(fmt, &spec);
  2395. fmt += read;
  2396. switch (spec.type) {
  2397. case FORMAT_TYPE_NONE: {
  2398. int copy = read;
  2399. if (str < end) {
  2400. if (copy > end - str)
  2401. copy = end - str;
  2402. memcpy(str, old_fmt, copy);
  2403. }
  2404. str += read;
  2405. break;
  2406. }
  2407. case FORMAT_TYPE_WIDTH:
  2408. set_field_width(&spec, get_arg(int));
  2409. break;
  2410. case FORMAT_TYPE_PRECISION:
  2411. set_precision(&spec, get_arg(int));
  2412. break;
  2413. case FORMAT_TYPE_CHAR: {
  2414. char c;
  2415. if (!(spec.flags & LEFT)) {
  2416. while (--spec.field_width > 0) {
  2417. if (str < end)
  2418. *str = ' ';
  2419. ++str;
  2420. }
  2421. }
  2422. c = (unsigned char) get_arg(char);
  2423. if (str < end)
  2424. *str = c;
  2425. ++str;
  2426. while (--spec.field_width > 0) {
  2427. if (str < end)
  2428. *str = ' ';
  2429. ++str;
  2430. }
  2431. break;
  2432. }
  2433. case FORMAT_TYPE_STR: {
  2434. const char *str_arg = args;
  2435. args += strlen(str_arg) + 1;
  2436. str = string(str, end, (char *)str_arg, spec);
  2437. break;
  2438. }
  2439. case FORMAT_TYPE_PTR: {
  2440. bool process = false;
  2441. int copy, len;
  2442. /* Non function dereferences were already done */
  2443. switch (*fmt) {
  2444. case 'S':
  2445. case 's':
  2446. case 'F':
  2447. case 'f':
  2448. process = true;
  2449. break;
  2450. default:
  2451. if (!isalnum(*fmt)) {
  2452. process = true;
  2453. break;
  2454. }
  2455. /* Pointer dereference was already processed */
  2456. if (str < end) {
  2457. len = copy = strlen(args);
  2458. if (copy > end - str)
  2459. copy = end - str;
  2460. memcpy(str, args, copy);
  2461. str += len;
  2462. args += len;
  2463. }
  2464. }
  2465. if (process)
  2466. str = pointer(fmt, str, end, get_arg(void *), spec);
  2467. while (isalnum(*fmt))
  2468. fmt++;
  2469. break;
  2470. }
  2471. case FORMAT_TYPE_PERCENT_CHAR:
  2472. if (str < end)
  2473. *str = '%';
  2474. ++str;
  2475. break;
  2476. case FORMAT_TYPE_INVALID:
  2477. goto out;
  2478. default: {
  2479. unsigned long long num;
  2480. switch (spec.type) {
  2481. case FORMAT_TYPE_LONG_LONG:
  2482. num = get_arg(long long);
  2483. break;
  2484. case FORMAT_TYPE_ULONG:
  2485. case FORMAT_TYPE_LONG:
  2486. num = get_arg(unsigned long);
  2487. break;
  2488. case FORMAT_TYPE_SIZE_T:
  2489. num = get_arg(size_t);
  2490. break;
  2491. case FORMAT_TYPE_PTRDIFF:
  2492. num = get_arg(ptrdiff_t);
  2493. break;
  2494. case FORMAT_TYPE_UBYTE:
  2495. num = get_arg(unsigned char);
  2496. break;
  2497. case FORMAT_TYPE_BYTE:
  2498. num = get_arg(signed char);
  2499. break;
  2500. case FORMAT_TYPE_USHORT:
  2501. num = get_arg(unsigned short);
  2502. break;
  2503. case FORMAT_TYPE_SHORT:
  2504. num = get_arg(short);
  2505. break;
  2506. case FORMAT_TYPE_UINT:
  2507. num = get_arg(unsigned int);
  2508. break;
  2509. default:
  2510. num = get_arg(int);
  2511. }
  2512. str = number(str, end, num, spec);
  2513. } /* default: */
  2514. } /* switch(spec.type) */
  2515. } /* while(*fmt) */
  2516. out:
  2517. if (size > 0) {
  2518. if (str < end)
  2519. *str = '\0';
  2520. else
  2521. end[-1] = '\0';
  2522. }
  2523. #undef get_arg
  2524. /* the trailing null byte doesn't count towards the total */
  2525. return str - buf;
  2526. }
  2527. EXPORT_SYMBOL_GPL(bstr_printf);
  2528. /**
  2529. * bprintf - Parse a format string and place args' binary value in a buffer
  2530. * @bin_buf: The buffer to place args' binary value
  2531. * @size: The size of the buffer(by words(32bits), not characters)
  2532. * @fmt: The format string to use
  2533. * @...: Arguments for the format string
  2534. *
  2535. * The function returns the number of words(u32) written
  2536. * into @bin_buf.
  2537. */
  2538. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
  2539. {
  2540. va_list args;
  2541. int ret;
  2542. va_start(args, fmt);
  2543. ret = vbin_printf(bin_buf, size, fmt, args);
  2544. va_end(args);
  2545. return ret;
  2546. }
  2547. EXPORT_SYMBOL_GPL(bprintf);
  2548. #endif /* CONFIG_BINARY_PRINTF */
  2549. /**
  2550. * vsscanf - Unformat a buffer into a list of arguments
  2551. * @buf: input buffer
  2552. * @fmt: format of buffer
  2553. * @args: arguments
  2554. */
  2555. int vsscanf(const char *buf, const char *fmt, va_list args)
  2556. {
  2557. const char *str = buf;
  2558. char *next;
  2559. char digit;
  2560. int num = 0;
  2561. u8 qualifier;
  2562. unsigned int base;
  2563. union {
  2564. long long s;
  2565. unsigned long long u;
  2566. } val;
  2567. s16 field_width;
  2568. bool is_sign;
  2569. while (*fmt) {
  2570. /* skip any white space in format */
  2571. /* white space in format matchs any amount of
  2572. * white space, including none, in the input.
  2573. */
  2574. if (isspace(*fmt)) {
  2575. fmt = skip_spaces(++fmt);
  2576. str = skip_spaces(str);
  2577. }
  2578. /* anything that is not a conversion must match exactly */
  2579. if (*fmt != '%' && *fmt) {
  2580. if (*fmt++ != *str++)
  2581. break;
  2582. continue;
  2583. }
  2584. if (!*fmt)
  2585. break;
  2586. ++fmt;
  2587. /* skip this conversion.
  2588. * advance both strings to next white space
  2589. */
  2590. if (*fmt == '*') {
  2591. if (!*str)
  2592. break;
  2593. while (!isspace(*fmt) && *fmt != '%' && *fmt) {
  2594. /* '%*[' not yet supported, invalid format */
  2595. if (*fmt == '[')
  2596. return num;
  2597. fmt++;
  2598. }
  2599. while (!isspace(*str) && *str)
  2600. str++;
  2601. continue;
  2602. }
  2603. /* get field width */
  2604. field_width = -1;
  2605. if (isdigit(*fmt)) {
  2606. field_width = skip_atoi(&fmt);
  2607. if (field_width <= 0)
  2608. break;
  2609. }
  2610. /* get conversion qualifier */
  2611. qualifier = -1;
  2612. if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  2613. *fmt == 'z') {
  2614. qualifier = *fmt++;
  2615. if (unlikely(qualifier == *fmt)) {
  2616. if (qualifier == 'h') {
  2617. qualifier = 'H';
  2618. fmt++;
  2619. } else if (qualifier == 'l') {
  2620. qualifier = 'L';
  2621. fmt++;
  2622. }
  2623. }
  2624. }
  2625. if (!*fmt)
  2626. break;
  2627. if (*fmt == 'n') {
  2628. /* return number of characters read so far */
  2629. *va_arg(args, int *) = str - buf;
  2630. ++fmt;
  2631. continue;
  2632. }
  2633. if (!*str)
  2634. break;
  2635. base = 10;
  2636. is_sign = false;
  2637. switch (*fmt++) {
  2638. case 'c':
  2639. {
  2640. char *s = (char *)va_arg(args, char*);
  2641. if (field_width == -1)
  2642. field_width = 1;
  2643. do {
  2644. *s++ = *str++;
  2645. } while (--field_width > 0 && *str);
  2646. num++;
  2647. }
  2648. continue;
  2649. case 's':
  2650. {
  2651. char *s = (char *)va_arg(args, char *);
  2652. if (field_width == -1)
  2653. field_width = SHRT_MAX;
  2654. /* first, skip leading white space in buffer */
  2655. str = skip_spaces(str);
  2656. /* now copy until next white space */
  2657. while (*str && !isspace(*str) && field_width--)
  2658. *s++ = *str++;
  2659. *s = '\0';
  2660. num++;
  2661. }
  2662. continue;
  2663. /*
  2664. * Warning: This implementation of the '[' conversion specifier
  2665. * deviates from its glibc counterpart in the following ways:
  2666. * (1) It does NOT support ranges i.e. '-' is NOT a special
  2667. * character
  2668. * (2) It cannot match the closing bracket ']' itself
  2669. * (3) A field width is required
  2670. * (4) '%*[' (discard matching input) is currently not supported
  2671. *
  2672. * Example usage:
  2673. * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
  2674. * buf1, buf2, buf3);
  2675. * if (ret < 3)
  2676. * // etc..
  2677. */
  2678. case '[':
  2679. {
  2680. char *s = (char *)va_arg(args, char *);
  2681. DECLARE_BITMAP(set, 256) = {0};
  2682. unsigned int len = 0;
  2683. bool negate = (*fmt == '^');
  2684. /* field width is required */
  2685. if (field_width == -1)
  2686. return num;
  2687. if (negate)
  2688. ++fmt;
  2689. for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
  2690. set_bit((u8)*fmt, set);
  2691. /* no ']' or no character set found */
  2692. if (!*fmt || !len)
  2693. return num;
  2694. ++fmt;
  2695. if (negate) {
  2696. bitmap_complement(set, set, 256);
  2697. /* exclude null '\0' byte */
  2698. clear_bit(0, set);
  2699. }
  2700. /* match must be non-empty */
  2701. if (!test_bit((u8)*str, set))
  2702. return num;
  2703. while (test_bit((u8)*str, set) && field_width--)
  2704. *s++ = *str++;
  2705. *s = '\0';
  2706. ++num;
  2707. }
  2708. continue;
  2709. case 'o':
  2710. base = 8;
  2711. break;
  2712. case 'x':
  2713. case 'X':
  2714. base = 16;
  2715. break;
  2716. case 'i':
  2717. base = 0;
  2718. case 'd':
  2719. is_sign = true;
  2720. case 'u':
  2721. break;
  2722. case '%':
  2723. /* looking for '%' in str */
  2724. if (*str++ != '%')
  2725. return num;
  2726. continue;
  2727. default:
  2728. /* invalid format; stop here */
  2729. return num;
  2730. }
  2731. /* have some sort of integer conversion.
  2732. * first, skip white space in buffer.
  2733. */
  2734. str = skip_spaces(str);
  2735. digit = *str;
  2736. if (is_sign && digit == '-')
  2737. digit = *(str + 1);
  2738. if (!digit
  2739. || (base == 16 && !isxdigit(digit))
  2740. || (base == 10 && !isdigit(digit))
  2741. || (base == 8 && (!isdigit(digit) || digit > '7'))
  2742. || (base == 0 && !isdigit(digit)))
  2743. break;
  2744. if (is_sign)
  2745. val.s = qualifier != 'L' ?
  2746. simple_strtol(str, &next, base) :
  2747. simple_strtoll(str, &next, base);
  2748. else
  2749. val.u = qualifier != 'L' ?
  2750. simple_strtoul(str, &next, base) :
  2751. simple_strtoull(str, &next, base);
  2752. if (field_width > 0 && next - str > field_width) {
  2753. if (base == 0)
  2754. _parse_integer_fixup_radix(str, &base);
  2755. while (next - str > field_width) {
  2756. if (is_sign)
  2757. val.s = div_s64(val.s, base);
  2758. else
  2759. val.u = div_u64(val.u, base);
  2760. --next;
  2761. }
  2762. }
  2763. switch (qualifier) {
  2764. case 'H': /* that's 'hh' in format */
  2765. if (is_sign)
  2766. *va_arg(args, signed char *) = val.s;
  2767. else
  2768. *va_arg(args, unsigned char *) = val.u;
  2769. break;
  2770. case 'h':
  2771. if (is_sign)
  2772. *va_arg(args, short *) = val.s;
  2773. else
  2774. *va_arg(args, unsigned short *) = val.u;
  2775. break;
  2776. case 'l':
  2777. if (is_sign)
  2778. *va_arg(args, long *) = val.s;
  2779. else
  2780. *va_arg(args, unsigned long *) = val.u;
  2781. break;
  2782. case 'L':
  2783. if (is_sign)
  2784. *va_arg(args, long long *) = val.s;
  2785. else
  2786. *va_arg(args, unsigned long long *) = val.u;
  2787. break;
  2788. case 'z':
  2789. *va_arg(args, size_t *) = val.u;
  2790. break;
  2791. default:
  2792. if (is_sign)
  2793. *va_arg(args, int *) = val.s;
  2794. else
  2795. *va_arg(args, unsigned int *) = val.u;
  2796. break;
  2797. }
  2798. num++;
  2799. if (!next)
  2800. break;
  2801. str = next;
  2802. }
  2803. return num;
  2804. }
  2805. EXPORT_SYMBOL(vsscanf);
  2806. /**
  2807. * sscanf - Unformat a buffer into a list of arguments
  2808. * @buf: input buffer
  2809. * @fmt: formatting of buffer
  2810. * @...: resulting arguments
  2811. */
  2812. int sscanf(const char *buf, const char *fmt, ...)
  2813. {
  2814. va_list args;
  2815. int i;
  2816. va_start(args, fmt);
  2817. i = vsscanf(buf, fmt, args);
  2818. va_end(args);
  2819. return i;
  2820. }
  2821. EXPORT_SYMBOL(sscanf);