vsprintf.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162
  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_flag_spec = {
  622. .base = 16,
  623. .precision = -1,
  624. .flags = SPECIAL | SMALL,
  625. };
  626. static const struct printf_spec default_dec_spec = {
  627. .base = 10,
  628. .precision = -1,
  629. };
  630. static noinline_for_stack
  631. char *resource_string(char *buf, char *end, struct resource *res,
  632. struct printf_spec spec, const char *fmt)
  633. {
  634. #ifndef IO_RSRC_PRINTK_SIZE
  635. #define IO_RSRC_PRINTK_SIZE 6
  636. #endif
  637. #ifndef MEM_RSRC_PRINTK_SIZE
  638. #define MEM_RSRC_PRINTK_SIZE 10
  639. #endif
  640. static const struct printf_spec io_spec = {
  641. .base = 16,
  642. .field_width = IO_RSRC_PRINTK_SIZE,
  643. .precision = -1,
  644. .flags = SPECIAL | SMALL | ZEROPAD,
  645. };
  646. static const struct printf_spec mem_spec = {
  647. .base = 16,
  648. .field_width = MEM_RSRC_PRINTK_SIZE,
  649. .precision = -1,
  650. .flags = SPECIAL | SMALL | ZEROPAD,
  651. };
  652. static const struct printf_spec bus_spec = {
  653. .base = 16,
  654. .field_width = 2,
  655. .precision = -1,
  656. .flags = SMALL | ZEROPAD,
  657. };
  658. static const struct printf_spec str_spec = {
  659. .field_width = -1,
  660. .precision = 10,
  661. .flags = LEFT,
  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, default_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. static noinline_for_stack
  1188. char *pointer_string(char *buf, char *end, const void *ptr,
  1189. struct printf_spec spec)
  1190. {
  1191. spec.base = 16;
  1192. spec.flags |= SMALL;
  1193. if (spec.field_width == -1) {
  1194. spec.field_width = 2 * sizeof(ptr);
  1195. spec.flags |= ZEROPAD;
  1196. }
  1197. return number(buf, end, (unsigned long int)ptr, spec);
  1198. }
  1199. int kptr_restrict __read_mostly;
  1200. static noinline_for_stack
  1201. char *restricted_pointer(char *buf, char *end, const void *ptr,
  1202. struct printf_spec spec)
  1203. {
  1204. switch (kptr_restrict) {
  1205. case 0:
  1206. /* Always print %pK values */
  1207. break;
  1208. case 1: {
  1209. const struct cred *cred;
  1210. /*
  1211. * kptr_restrict==1 cannot be used in IRQ context
  1212. * because its test for CAP_SYSLOG would be meaningless.
  1213. */
  1214. if (in_irq() || in_serving_softirq() || in_nmi()) {
  1215. if (spec.field_width == -1)
  1216. spec.field_width = 2 * sizeof(ptr);
  1217. return string(buf, end, "pK-error", spec);
  1218. }
  1219. /*
  1220. * Only print the real pointer value if the current
  1221. * process has CAP_SYSLOG and is running with the
  1222. * same credentials it started with. This is because
  1223. * access to files is checked at open() time, but %pK
  1224. * checks permission at read() time. We don't want to
  1225. * leak pointer values if a binary opens a file using
  1226. * %pK and then elevates privileges before reading it.
  1227. */
  1228. cred = current_cred();
  1229. if (!has_capability_noaudit(current, CAP_SYSLOG) ||
  1230. !uid_eq(cred->euid, cred->uid) ||
  1231. !gid_eq(cred->egid, cred->gid))
  1232. ptr = NULL;
  1233. break;
  1234. }
  1235. case 2:
  1236. default:
  1237. /* Always print 0's for %pK */
  1238. ptr = NULL;
  1239. break;
  1240. }
  1241. return pointer_string(buf, end, ptr, spec);
  1242. }
  1243. static noinline_for_stack
  1244. char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
  1245. {
  1246. unsigned long long num;
  1247. int size;
  1248. switch (fmt[1]) {
  1249. case 'F':
  1250. num = *(const netdev_features_t *)addr;
  1251. size = sizeof(netdev_features_t);
  1252. break;
  1253. default:
  1254. num = (unsigned long)addr;
  1255. size = sizeof(unsigned long);
  1256. break;
  1257. }
  1258. return special_hex_number(buf, end, num, size);
  1259. }
  1260. static noinline_for_stack
  1261. char *address_val(char *buf, char *end, const void *addr, const char *fmt)
  1262. {
  1263. unsigned long long num;
  1264. int size;
  1265. switch (fmt[1]) {
  1266. case 'd':
  1267. num = *(const dma_addr_t *)addr;
  1268. size = sizeof(dma_addr_t);
  1269. break;
  1270. case 'p':
  1271. default:
  1272. num = *(const phys_addr_t *)addr;
  1273. size = sizeof(phys_addr_t);
  1274. break;
  1275. }
  1276. return special_hex_number(buf, end, num, size);
  1277. }
  1278. static noinline_for_stack
  1279. char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
  1280. const char *fmt)
  1281. {
  1282. if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
  1283. return string(buf, end, NULL, spec);
  1284. switch (fmt[1]) {
  1285. case 'r':
  1286. return number(buf, end, clk_get_rate(clk), spec);
  1287. case 'n':
  1288. default:
  1289. #ifdef CONFIG_COMMON_CLK
  1290. return string(buf, end, __clk_get_name(clk), spec);
  1291. #else
  1292. return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
  1293. #endif
  1294. }
  1295. }
  1296. static
  1297. char *format_flags(char *buf, char *end, unsigned long flags,
  1298. const struct trace_print_flags *names)
  1299. {
  1300. unsigned long mask;
  1301. for ( ; flags && names->name; names++) {
  1302. mask = names->mask;
  1303. if ((flags & mask) != mask)
  1304. continue;
  1305. buf = string(buf, end, names->name, default_str_spec);
  1306. flags &= ~mask;
  1307. if (flags) {
  1308. if (buf < end)
  1309. *buf = '|';
  1310. buf++;
  1311. }
  1312. }
  1313. if (flags)
  1314. buf = number(buf, end, flags, default_flag_spec);
  1315. return buf;
  1316. }
  1317. static noinline_for_stack
  1318. char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
  1319. {
  1320. unsigned long flags;
  1321. const struct trace_print_flags *names;
  1322. switch (fmt[1]) {
  1323. case 'p':
  1324. flags = *(unsigned long *)flags_ptr;
  1325. /* Remove zone id */
  1326. flags &= (1UL << NR_PAGEFLAGS) - 1;
  1327. names = pageflag_names;
  1328. break;
  1329. case 'v':
  1330. flags = *(unsigned long *)flags_ptr;
  1331. names = vmaflag_names;
  1332. break;
  1333. case 'g':
  1334. flags = *(gfp_t *)flags_ptr;
  1335. names = gfpflag_names;
  1336. break;
  1337. default:
  1338. WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
  1339. return buf;
  1340. }
  1341. return format_flags(buf, end, flags, names);
  1342. }
  1343. static const char *device_node_name_for_depth(const struct device_node *np, int depth)
  1344. {
  1345. for ( ; np && depth; depth--)
  1346. np = np->parent;
  1347. return kbasename(np->full_name);
  1348. }
  1349. static noinline_for_stack
  1350. char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
  1351. {
  1352. int depth;
  1353. const struct device_node *parent = np->parent;
  1354. /* special case for root node */
  1355. if (!parent)
  1356. return string(buf, end, "/", default_str_spec);
  1357. for (depth = 0; parent->parent; depth++)
  1358. parent = parent->parent;
  1359. for ( ; depth >= 0; depth--) {
  1360. buf = string(buf, end, "/", default_str_spec);
  1361. buf = string(buf, end, device_node_name_for_depth(np, depth),
  1362. default_str_spec);
  1363. }
  1364. return buf;
  1365. }
  1366. static noinline_for_stack
  1367. char *device_node_string(char *buf, char *end, struct device_node *dn,
  1368. struct printf_spec spec, const char *fmt)
  1369. {
  1370. char tbuf[sizeof("xxxx") + 1];
  1371. const char *p;
  1372. int ret;
  1373. char *buf_start = buf;
  1374. struct property *prop;
  1375. bool has_mult, pass;
  1376. static const struct printf_spec num_spec = {
  1377. .flags = SMALL,
  1378. .field_width = -1,
  1379. .precision = -1,
  1380. .base = 10,
  1381. };
  1382. struct printf_spec str_spec = spec;
  1383. str_spec.field_width = -1;
  1384. if (!IS_ENABLED(CONFIG_OF))
  1385. return string(buf, end, "(!OF)", spec);
  1386. if ((unsigned long)dn < PAGE_SIZE)
  1387. return string(buf, end, "(null)", spec);
  1388. /* simple case without anything any more format specifiers */
  1389. fmt++;
  1390. if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
  1391. fmt = "f";
  1392. for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
  1393. if (pass) {
  1394. if (buf < end)
  1395. *buf = ':';
  1396. buf++;
  1397. }
  1398. switch (*fmt) {
  1399. case 'f': /* full_name */
  1400. buf = device_node_gen_full_name(dn, buf, end);
  1401. break;
  1402. case 'n': /* name */
  1403. buf = string(buf, end, dn->name, str_spec);
  1404. break;
  1405. case 'p': /* phandle */
  1406. buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
  1407. break;
  1408. case 'P': /* path-spec */
  1409. p = kbasename(of_node_full_name(dn));
  1410. if (!p[1])
  1411. p = "/";
  1412. buf = string(buf, end, p, str_spec);
  1413. break;
  1414. case 'F': /* flags */
  1415. tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
  1416. tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
  1417. tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
  1418. tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
  1419. tbuf[4] = 0;
  1420. buf = string(buf, end, tbuf, str_spec);
  1421. break;
  1422. case 'c': /* major compatible string */
  1423. ret = of_property_read_string(dn, "compatible", &p);
  1424. if (!ret)
  1425. buf = string(buf, end, p, str_spec);
  1426. break;
  1427. case 'C': /* full compatible string */
  1428. has_mult = false;
  1429. of_property_for_each_string(dn, "compatible", prop, p) {
  1430. if (has_mult)
  1431. buf = string(buf, end, ",", str_spec);
  1432. buf = string(buf, end, "\"", str_spec);
  1433. buf = string(buf, end, p, str_spec);
  1434. buf = string(buf, end, "\"", str_spec);
  1435. has_mult = true;
  1436. }
  1437. break;
  1438. default:
  1439. break;
  1440. }
  1441. }
  1442. return widen_string(buf, buf - buf_start, end, spec);
  1443. }
  1444. static bool have_filled_random_ptr_key __read_mostly;
  1445. static siphash_key_t ptr_key __read_mostly;
  1446. static void fill_random_ptr_key(struct random_ready_callback *unused)
  1447. {
  1448. get_random_bytes(&ptr_key, sizeof(ptr_key));
  1449. /*
  1450. * have_filled_random_ptr_key==true is dependent on get_random_bytes().
  1451. * ptr_to_id() needs to see have_filled_random_ptr_key==true
  1452. * after get_random_bytes() returns.
  1453. */
  1454. smp_mb();
  1455. WRITE_ONCE(have_filled_random_ptr_key, true);
  1456. }
  1457. static struct random_ready_callback random_ready = {
  1458. .func = fill_random_ptr_key
  1459. };
  1460. static int __init initialize_ptr_random(void)
  1461. {
  1462. int ret = add_random_ready_callback(&random_ready);
  1463. if (!ret) {
  1464. return 0;
  1465. } else if (ret == -EALREADY) {
  1466. fill_random_ptr_key(&random_ready);
  1467. return 0;
  1468. }
  1469. return ret;
  1470. }
  1471. early_initcall(initialize_ptr_random);
  1472. /* Maps a pointer to a 32 bit unique identifier. */
  1473. static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
  1474. {
  1475. const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
  1476. unsigned long hashval;
  1477. if (unlikely(!have_filled_random_ptr_key)) {
  1478. spec.field_width = 2 * sizeof(ptr);
  1479. /* string length must be less than default_width */
  1480. return string(buf, end, str, spec);
  1481. }
  1482. #ifdef CONFIG_64BIT
  1483. hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
  1484. /*
  1485. * Mask off the first 32 bits, this makes explicit that we have
  1486. * modified the address (and 32 bits is plenty for a unique ID).
  1487. */
  1488. hashval = hashval & 0xffffffff;
  1489. #else
  1490. hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
  1491. #endif
  1492. return pointer_string(buf, end, (const void *)hashval, spec);
  1493. }
  1494. /*
  1495. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  1496. * by an extra set of alphanumeric characters that are extended format
  1497. * specifiers.
  1498. *
  1499. * Please update scripts/checkpatch.pl when adding/removing conversion
  1500. * characters. (Search for "check for vsprintf extension").
  1501. *
  1502. * Right now we handle:
  1503. *
  1504. * - 'S' For symbolic direct pointers (or function descriptors) with offset
  1505. * - 's' For symbolic direct pointers (or function descriptors) without offset
  1506. * - 'F' Same as 'S'
  1507. * - 'f' Same as 's'
  1508. * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
  1509. * - 'B' For backtraced symbolic direct pointers with offset
  1510. * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
  1511. * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
  1512. * - 'b[l]' For a bitmap, the number of bits is determined by the field
  1513. * width which must be explicitly specified either as part of the
  1514. * format string '%32b[l]' or through '%*b[l]', [l] selects
  1515. * range-list format instead of hex format
  1516. * - 'M' For a 6-byte MAC address, it prints the address in the
  1517. * usual colon-separated hex notation
  1518. * - 'm' For a 6-byte MAC address, it prints the hex address without colons
  1519. * - 'MF' For a 6-byte MAC FDDI address, it prints the address
  1520. * with a dash-separated hex notation
  1521. * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
  1522. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
  1523. * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
  1524. * IPv6 uses colon separated network-order 16 bit hex with leading 0's
  1525. * [S][pfs]
  1526. * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  1527. * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  1528. * - 'i' [46] for 'raw' IPv4/IPv6 addresses
  1529. * IPv6 omits the colons (01020304...0f)
  1530. * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
  1531. * [S][pfs]
  1532. * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  1533. * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  1534. * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
  1535. * - 'I[6S]c' for IPv6 addresses printed as specified by
  1536. * http://tools.ietf.org/html/rfc5952
  1537. * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
  1538. * of the following flags (see string_escape_mem() for the
  1539. * details):
  1540. * a - ESCAPE_ANY
  1541. * c - ESCAPE_SPECIAL
  1542. * h - ESCAPE_HEX
  1543. * n - ESCAPE_NULL
  1544. * o - ESCAPE_OCTAL
  1545. * p - ESCAPE_NP
  1546. * s - ESCAPE_SPACE
  1547. * By default ESCAPE_ANY_NP is used.
  1548. * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
  1549. * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  1550. * Options for %pU are:
  1551. * b big endian lower case hex (default)
  1552. * B big endian UPPER case hex
  1553. * l little endian lower case hex
  1554. * L little endian UPPER case hex
  1555. * big endian output byte order is:
  1556. * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
  1557. * little endian output byte order is:
  1558. * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
  1559. * - 'V' For a struct va_format which contains a format string * and va_list *,
  1560. * call vsnprintf(->format, *->va_list).
  1561. * Implements a "recursive vsnprintf".
  1562. * Do not use this feature without some mechanism to verify the
  1563. * correctness of the format string and va_list arguments.
  1564. * - 'K' For a kernel pointer that should be hidden from unprivileged users
  1565. * - 'NF' For a netdev_features_t
  1566. * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
  1567. * a certain separator (' ' by default):
  1568. * C colon
  1569. * D dash
  1570. * N no separator
  1571. * The maximum supported length is 64 bytes of the input. Consider
  1572. * to use print_hex_dump() for the larger input.
  1573. * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
  1574. * (default assumed to be phys_addr_t, passed by reference)
  1575. * - 'd[234]' For a dentry name (optionally 2-4 last components)
  1576. * - 'D[234]' Same as 'd' but for a struct file
  1577. * - 'g' For block_device name (gendisk + partition number)
  1578. * - 'C' For a clock, it prints the name (Common Clock Framework) or address
  1579. * (legacy clock framework) of the clock
  1580. * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
  1581. * (legacy clock framework) of the clock
  1582. * - 'Cr' For a clock, it prints the current rate of the clock
  1583. * - 'G' For flags to be printed as a collection of symbolic strings that would
  1584. * construct the specific value. Supported flags given by option:
  1585. * p page flags (see struct page) given as pointer to unsigned long
  1586. * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
  1587. * v vma flags (VM_*) given as pointer to unsigned long
  1588. * - 'O' For a kobject based struct. Must be one of the following:
  1589. * - 'OF[fnpPcCF]' For a device tree object
  1590. * Without any optional arguments prints the full_name
  1591. * f device node full_name
  1592. * n device node name
  1593. * p device node phandle
  1594. * P device node path spec (name + @unit)
  1595. * F device node flags
  1596. * c major compatible string
  1597. * C full compatible string
  1598. *
  1599. * - 'x' For printing the address. Equivalent to "%lx".
  1600. *
  1601. * ** When making changes please also update:
  1602. * Documentation/core-api/printk-formats.rst
  1603. *
  1604. * Note: The default behaviour (unadorned %p) is to hash the address,
  1605. * rendering it useful as a unique identifier.
  1606. */
  1607. static noinline_for_stack
  1608. char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  1609. struct printf_spec spec)
  1610. {
  1611. const int default_width = 2 * sizeof(void *);
  1612. if (!ptr && *fmt != 'K' && *fmt != 'x') {
  1613. /*
  1614. * Print (null) with the same width as a pointer so it makes
  1615. * tabular output look nice.
  1616. */
  1617. if (spec.field_width == -1)
  1618. spec.field_width = default_width;
  1619. return string(buf, end, "(null)", spec);
  1620. }
  1621. switch (*fmt) {
  1622. case 'F':
  1623. case 'f':
  1624. case 'S':
  1625. case 's':
  1626. ptr = dereference_symbol_descriptor(ptr);
  1627. /* Fallthrough */
  1628. case 'B':
  1629. return symbol_string(buf, end, ptr, spec, fmt);
  1630. case 'R':
  1631. case 'r':
  1632. return resource_string(buf, end, ptr, spec, fmt);
  1633. case 'h':
  1634. return hex_string(buf, end, ptr, spec, fmt);
  1635. case 'b':
  1636. switch (fmt[1]) {
  1637. case 'l':
  1638. return bitmap_list_string(buf, end, ptr, spec, fmt);
  1639. default:
  1640. return bitmap_string(buf, end, ptr, spec, fmt);
  1641. }
  1642. case 'M': /* Colon separated: 00:01:02:03:04:05 */
  1643. case 'm': /* Contiguous: 000102030405 */
  1644. /* [mM]F (FDDI) */
  1645. /* [mM]R (Reverse order; Bluetooth) */
  1646. return mac_address_string(buf, end, ptr, spec, fmt);
  1647. case 'I': /* Formatted IP supported
  1648. * 4: 1.2.3.4
  1649. * 6: 0001:0203:...:0708
  1650. * 6c: 1::708 or 1::1.2.3.4
  1651. */
  1652. case 'i': /* Contiguous:
  1653. * 4: 001.002.003.004
  1654. * 6: 000102...0f
  1655. */
  1656. switch (fmt[1]) {
  1657. case '6':
  1658. return ip6_addr_string(buf, end, ptr, spec, fmt);
  1659. case '4':
  1660. return ip4_addr_string(buf, end, ptr, spec, fmt);
  1661. case 'S': {
  1662. const union {
  1663. struct sockaddr raw;
  1664. struct sockaddr_in v4;
  1665. struct sockaddr_in6 v6;
  1666. } *sa = ptr;
  1667. switch (sa->raw.sa_family) {
  1668. case AF_INET:
  1669. return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
  1670. case AF_INET6:
  1671. return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
  1672. default:
  1673. return string(buf, end, "(invalid address)", spec);
  1674. }}
  1675. }
  1676. break;
  1677. case 'E':
  1678. return escaped_string(buf, end, ptr, spec, fmt);
  1679. case 'U':
  1680. return uuid_string(buf, end, ptr, spec, fmt);
  1681. case 'V':
  1682. {
  1683. va_list va;
  1684. va_copy(va, *((struct va_format *)ptr)->va);
  1685. buf += vsnprintf(buf, end > buf ? end - buf : 0,
  1686. ((struct va_format *)ptr)->fmt, va);
  1687. va_end(va);
  1688. return buf;
  1689. }
  1690. case 'K':
  1691. if (!kptr_restrict)
  1692. break;
  1693. return restricted_pointer(buf, end, ptr, spec);
  1694. case 'N':
  1695. return netdev_bits(buf, end, ptr, fmt);
  1696. case 'a':
  1697. return address_val(buf, end, ptr, fmt);
  1698. case 'd':
  1699. return dentry_name(buf, end, ptr, spec, fmt);
  1700. case 'C':
  1701. return clock(buf, end, ptr, spec, fmt);
  1702. case 'D':
  1703. return dentry_name(buf, end,
  1704. ((const struct file *)ptr)->f_path.dentry,
  1705. spec, fmt);
  1706. #ifdef CONFIG_BLOCK
  1707. case 'g':
  1708. return bdev_name(buf, end, ptr, spec, fmt);
  1709. #endif
  1710. case 'G':
  1711. return flags_string(buf, end, ptr, fmt);
  1712. case 'O':
  1713. switch (fmt[1]) {
  1714. case 'F':
  1715. return device_node_string(buf, end, ptr, spec, fmt + 1);
  1716. }
  1717. case 'x':
  1718. return pointer_string(buf, end, ptr, spec);
  1719. }
  1720. /* default is to _not_ leak addresses, hash before printing */
  1721. return ptr_to_id(buf, end, ptr, spec);
  1722. }
  1723. /*
  1724. * Helper function to decode printf style format.
  1725. * Each call decode a token from the format and return the
  1726. * number of characters read (or likely the delta where it wants
  1727. * to go on the next call).
  1728. * The decoded token is returned through the parameters
  1729. *
  1730. * 'h', 'l', or 'L' for integer fields
  1731. * 'z' support added 23/7/1999 S.H.
  1732. * 'z' changed to 'Z' --davidm 1/25/99
  1733. * 'Z' changed to 'z' --adobriyan 2017-01-25
  1734. * 't' added for ptrdiff_t
  1735. *
  1736. * @fmt: the format string
  1737. * @type of the token returned
  1738. * @flags: various flags such as +, -, # tokens..
  1739. * @field_width: overwritten width
  1740. * @base: base of the number (octal, hex, ...)
  1741. * @precision: precision of a number
  1742. * @qualifier: qualifier of a number (long, size_t, ...)
  1743. */
  1744. static noinline_for_stack
  1745. int format_decode(const char *fmt, struct printf_spec *spec)
  1746. {
  1747. const char *start = fmt;
  1748. char qualifier;
  1749. /* we finished early by reading the field width */
  1750. if (spec->type == FORMAT_TYPE_WIDTH) {
  1751. if (spec->field_width < 0) {
  1752. spec->field_width = -spec->field_width;
  1753. spec->flags |= LEFT;
  1754. }
  1755. spec->type = FORMAT_TYPE_NONE;
  1756. goto precision;
  1757. }
  1758. /* we finished early by reading the precision */
  1759. if (spec->type == FORMAT_TYPE_PRECISION) {
  1760. if (spec->precision < 0)
  1761. spec->precision = 0;
  1762. spec->type = FORMAT_TYPE_NONE;
  1763. goto qualifier;
  1764. }
  1765. /* By default */
  1766. spec->type = FORMAT_TYPE_NONE;
  1767. for (; *fmt ; ++fmt) {
  1768. if (*fmt == '%')
  1769. break;
  1770. }
  1771. /* Return the current non-format string */
  1772. if (fmt != start || !*fmt)
  1773. return fmt - start;
  1774. /* Process flags */
  1775. spec->flags = 0;
  1776. while (1) { /* this also skips first '%' */
  1777. bool found = true;
  1778. ++fmt;
  1779. switch (*fmt) {
  1780. case '-': spec->flags |= LEFT; break;
  1781. case '+': spec->flags |= PLUS; break;
  1782. case ' ': spec->flags |= SPACE; break;
  1783. case '#': spec->flags |= SPECIAL; break;
  1784. case '0': spec->flags |= ZEROPAD; break;
  1785. default: found = false;
  1786. }
  1787. if (!found)
  1788. break;
  1789. }
  1790. /* get field width */
  1791. spec->field_width = -1;
  1792. if (isdigit(*fmt))
  1793. spec->field_width = skip_atoi(&fmt);
  1794. else if (*fmt == '*') {
  1795. /* it's the next argument */
  1796. spec->type = FORMAT_TYPE_WIDTH;
  1797. return ++fmt - start;
  1798. }
  1799. precision:
  1800. /* get the precision */
  1801. spec->precision = -1;
  1802. if (*fmt == '.') {
  1803. ++fmt;
  1804. if (isdigit(*fmt)) {
  1805. spec->precision = skip_atoi(&fmt);
  1806. if (spec->precision < 0)
  1807. spec->precision = 0;
  1808. } else if (*fmt == '*') {
  1809. /* it's the next argument */
  1810. spec->type = FORMAT_TYPE_PRECISION;
  1811. return ++fmt - start;
  1812. }
  1813. }
  1814. qualifier:
  1815. /* get the conversion qualifier */
  1816. qualifier = 0;
  1817. if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  1818. *fmt == 'z' || *fmt == 't') {
  1819. qualifier = *fmt++;
  1820. if (unlikely(qualifier == *fmt)) {
  1821. if (qualifier == 'l') {
  1822. qualifier = 'L';
  1823. ++fmt;
  1824. } else if (qualifier == 'h') {
  1825. qualifier = 'H';
  1826. ++fmt;
  1827. }
  1828. }
  1829. }
  1830. /* default base */
  1831. spec->base = 10;
  1832. switch (*fmt) {
  1833. case 'c':
  1834. spec->type = FORMAT_TYPE_CHAR;
  1835. return ++fmt - start;
  1836. case 's':
  1837. spec->type = FORMAT_TYPE_STR;
  1838. return ++fmt - start;
  1839. case 'p':
  1840. spec->type = FORMAT_TYPE_PTR;
  1841. return ++fmt - start;
  1842. case '%':
  1843. spec->type = FORMAT_TYPE_PERCENT_CHAR;
  1844. return ++fmt - start;
  1845. /* integer number formats - set up the flags and "break" */
  1846. case 'o':
  1847. spec->base = 8;
  1848. break;
  1849. case 'x':
  1850. spec->flags |= SMALL;
  1851. /* fall through */
  1852. case 'X':
  1853. spec->base = 16;
  1854. break;
  1855. case 'd':
  1856. case 'i':
  1857. spec->flags |= SIGN;
  1858. case 'u':
  1859. break;
  1860. case 'n':
  1861. /*
  1862. * Since %n poses a greater security risk than
  1863. * utility, treat it as any other invalid or
  1864. * unsupported format specifier.
  1865. */
  1866. /* Fall-through */
  1867. default:
  1868. WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
  1869. spec->type = FORMAT_TYPE_INVALID;
  1870. return fmt - start;
  1871. }
  1872. if (qualifier == 'L')
  1873. spec->type = FORMAT_TYPE_LONG_LONG;
  1874. else if (qualifier == 'l') {
  1875. BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
  1876. spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
  1877. } else if (qualifier == 'z') {
  1878. spec->type = FORMAT_TYPE_SIZE_T;
  1879. } else if (qualifier == 't') {
  1880. spec->type = FORMAT_TYPE_PTRDIFF;
  1881. } else if (qualifier == 'H') {
  1882. BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
  1883. spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
  1884. } else if (qualifier == 'h') {
  1885. BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
  1886. spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
  1887. } else {
  1888. BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
  1889. spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
  1890. }
  1891. return ++fmt - start;
  1892. }
  1893. static void
  1894. set_field_width(struct printf_spec *spec, int width)
  1895. {
  1896. spec->field_width = width;
  1897. if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
  1898. spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
  1899. }
  1900. }
  1901. static void
  1902. set_precision(struct printf_spec *spec, int prec)
  1903. {
  1904. spec->precision = prec;
  1905. if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
  1906. spec->precision = clamp(prec, 0, PRECISION_MAX);
  1907. }
  1908. }
  1909. /**
  1910. * vsnprintf - Format a string and place it in a buffer
  1911. * @buf: The buffer to place the result into
  1912. * @size: The size of the buffer, including the trailing null space
  1913. * @fmt: The format string to use
  1914. * @args: Arguments for the format string
  1915. *
  1916. * This function generally follows C99 vsnprintf, but has some
  1917. * extensions and a few limitations:
  1918. *
  1919. * - ``%n`` is unsupported
  1920. * - ``%p*`` is handled by pointer()
  1921. *
  1922. * See pointer() or Documentation/core-api/printk-formats.rst for more
  1923. * extensive description.
  1924. *
  1925. * **Please update the documentation in both places when making changes**
  1926. *
  1927. * The return value is the number of characters which would
  1928. * be generated for the given input, excluding the trailing
  1929. * '\0', as per ISO C99. If you want to have the exact
  1930. * number of characters written into @buf as return value
  1931. * (not including the trailing '\0'), use vscnprintf(). If the
  1932. * return is greater than or equal to @size, the resulting
  1933. * string is truncated.
  1934. *
  1935. * If you're not already dealing with a va_list consider using snprintf().
  1936. */
  1937. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1938. {
  1939. unsigned long long num;
  1940. char *str, *end;
  1941. struct printf_spec spec = {0};
  1942. /* Reject out-of-range values early. Large positive sizes are
  1943. used for unknown buffer sizes. */
  1944. if (WARN_ON_ONCE(size > INT_MAX))
  1945. return 0;
  1946. str = buf;
  1947. end = buf + size;
  1948. /* Make sure end is always >= buf */
  1949. if (end < buf) {
  1950. end = ((void *)-1);
  1951. size = end - buf;
  1952. }
  1953. while (*fmt) {
  1954. const char *old_fmt = fmt;
  1955. int read = format_decode(fmt, &spec);
  1956. fmt += read;
  1957. switch (spec.type) {
  1958. case FORMAT_TYPE_NONE: {
  1959. int copy = read;
  1960. if (str < end) {
  1961. if (copy > end - str)
  1962. copy = end - str;
  1963. memcpy(str, old_fmt, copy);
  1964. }
  1965. str += read;
  1966. break;
  1967. }
  1968. case FORMAT_TYPE_WIDTH:
  1969. set_field_width(&spec, va_arg(args, int));
  1970. break;
  1971. case FORMAT_TYPE_PRECISION:
  1972. set_precision(&spec, va_arg(args, int));
  1973. break;
  1974. case FORMAT_TYPE_CHAR: {
  1975. char c;
  1976. if (!(spec.flags & LEFT)) {
  1977. while (--spec.field_width > 0) {
  1978. if (str < end)
  1979. *str = ' ';
  1980. ++str;
  1981. }
  1982. }
  1983. c = (unsigned char) va_arg(args, int);
  1984. if (str < end)
  1985. *str = c;
  1986. ++str;
  1987. while (--spec.field_width > 0) {
  1988. if (str < end)
  1989. *str = ' ';
  1990. ++str;
  1991. }
  1992. break;
  1993. }
  1994. case FORMAT_TYPE_STR:
  1995. str = string(str, end, va_arg(args, char *), spec);
  1996. break;
  1997. case FORMAT_TYPE_PTR:
  1998. str = pointer(fmt, str, end, va_arg(args, void *),
  1999. spec);
  2000. while (isalnum(*fmt))
  2001. fmt++;
  2002. break;
  2003. case FORMAT_TYPE_PERCENT_CHAR:
  2004. if (str < end)
  2005. *str = '%';
  2006. ++str;
  2007. break;
  2008. case FORMAT_TYPE_INVALID:
  2009. /*
  2010. * Presumably the arguments passed gcc's type
  2011. * checking, but there is no safe or sane way
  2012. * for us to continue parsing the format and
  2013. * fetching from the va_list; the remaining
  2014. * specifiers and arguments would be out of
  2015. * sync.
  2016. */
  2017. goto out;
  2018. default:
  2019. switch (spec.type) {
  2020. case FORMAT_TYPE_LONG_LONG:
  2021. num = va_arg(args, long long);
  2022. break;
  2023. case FORMAT_TYPE_ULONG:
  2024. num = va_arg(args, unsigned long);
  2025. break;
  2026. case FORMAT_TYPE_LONG:
  2027. num = va_arg(args, long);
  2028. break;
  2029. case FORMAT_TYPE_SIZE_T:
  2030. if (spec.flags & SIGN)
  2031. num = va_arg(args, ssize_t);
  2032. else
  2033. num = va_arg(args, size_t);
  2034. break;
  2035. case FORMAT_TYPE_PTRDIFF:
  2036. num = va_arg(args, ptrdiff_t);
  2037. break;
  2038. case FORMAT_TYPE_UBYTE:
  2039. num = (unsigned char) va_arg(args, int);
  2040. break;
  2041. case FORMAT_TYPE_BYTE:
  2042. num = (signed char) va_arg(args, int);
  2043. break;
  2044. case FORMAT_TYPE_USHORT:
  2045. num = (unsigned short) va_arg(args, int);
  2046. break;
  2047. case FORMAT_TYPE_SHORT:
  2048. num = (short) va_arg(args, int);
  2049. break;
  2050. case FORMAT_TYPE_INT:
  2051. num = (int) va_arg(args, int);
  2052. break;
  2053. default:
  2054. num = va_arg(args, unsigned int);
  2055. }
  2056. str = number(str, end, num, spec);
  2057. }
  2058. }
  2059. out:
  2060. if (size > 0) {
  2061. if (str < end)
  2062. *str = '\0';
  2063. else
  2064. end[-1] = '\0';
  2065. }
  2066. /* the trailing null byte doesn't count towards the total */
  2067. return str-buf;
  2068. }
  2069. EXPORT_SYMBOL(vsnprintf);
  2070. /**
  2071. * vscnprintf - Format a string and place it in a buffer
  2072. * @buf: The buffer to place the result into
  2073. * @size: The size of the buffer, including the trailing null space
  2074. * @fmt: The format string to use
  2075. * @args: Arguments for the format string
  2076. *
  2077. * The return value is the number of characters which have been written into
  2078. * the @buf not including the trailing '\0'. If @size is == 0 the function
  2079. * returns 0.
  2080. *
  2081. * If you're not already dealing with a va_list consider using scnprintf().
  2082. *
  2083. * See the vsnprintf() documentation for format string extensions over C99.
  2084. */
  2085. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  2086. {
  2087. int i;
  2088. i = vsnprintf(buf, size, fmt, args);
  2089. if (likely(i < size))
  2090. return i;
  2091. if (size != 0)
  2092. return size - 1;
  2093. return 0;
  2094. }
  2095. EXPORT_SYMBOL(vscnprintf);
  2096. /**
  2097. * snprintf - Format a string and place it in a buffer
  2098. * @buf: The buffer to place the result into
  2099. * @size: The size of the buffer, including the trailing null space
  2100. * @fmt: The format string to use
  2101. * @...: Arguments for the format string
  2102. *
  2103. * The return value is the number of characters which would be
  2104. * generated for the given input, excluding the trailing null,
  2105. * as per ISO C99. If the return is greater than or equal to
  2106. * @size, the resulting string is truncated.
  2107. *
  2108. * See the vsnprintf() documentation for format string extensions over C99.
  2109. */
  2110. int snprintf(char *buf, size_t size, const char *fmt, ...)
  2111. {
  2112. va_list args;
  2113. int i;
  2114. va_start(args, fmt);
  2115. i = vsnprintf(buf, size, fmt, args);
  2116. va_end(args);
  2117. return i;
  2118. }
  2119. EXPORT_SYMBOL(snprintf);
  2120. /**
  2121. * scnprintf - Format a string and place it in a buffer
  2122. * @buf: The buffer to place the result into
  2123. * @size: The size of the buffer, including the trailing null space
  2124. * @fmt: The format string to use
  2125. * @...: Arguments for the format string
  2126. *
  2127. * The return value is the number of characters written into @buf not including
  2128. * the trailing '\0'. If @size is == 0 the function returns 0.
  2129. */
  2130. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  2131. {
  2132. va_list args;
  2133. int i;
  2134. va_start(args, fmt);
  2135. i = vscnprintf(buf, size, fmt, args);
  2136. va_end(args);
  2137. return i;
  2138. }
  2139. EXPORT_SYMBOL(scnprintf);
  2140. /**
  2141. * vsprintf - Format a string and place it in a buffer
  2142. * @buf: The buffer to place the result into
  2143. * @fmt: The format string to use
  2144. * @args: Arguments for the format string
  2145. *
  2146. * The function returns the number of characters written
  2147. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  2148. * buffer overflows.
  2149. *
  2150. * If you're not already dealing with a va_list consider using sprintf().
  2151. *
  2152. * See the vsnprintf() documentation for format string extensions over C99.
  2153. */
  2154. int vsprintf(char *buf, const char *fmt, va_list args)
  2155. {
  2156. return vsnprintf(buf, INT_MAX, fmt, args);
  2157. }
  2158. EXPORT_SYMBOL(vsprintf);
  2159. /**
  2160. * sprintf - Format a string and place it in a buffer
  2161. * @buf: The buffer to place the result into
  2162. * @fmt: The format string to use
  2163. * @...: Arguments for the format string
  2164. *
  2165. * The function returns the number of characters written
  2166. * into @buf. Use snprintf() or scnprintf() in order to avoid
  2167. * buffer overflows.
  2168. *
  2169. * See the vsnprintf() documentation for format string extensions over C99.
  2170. */
  2171. int sprintf(char *buf, const char *fmt, ...)
  2172. {
  2173. va_list args;
  2174. int i;
  2175. va_start(args, fmt);
  2176. i = vsnprintf(buf, INT_MAX, fmt, args);
  2177. va_end(args);
  2178. return i;
  2179. }
  2180. EXPORT_SYMBOL(sprintf);
  2181. #ifdef CONFIG_BINARY_PRINTF
  2182. /*
  2183. * bprintf service:
  2184. * vbin_printf() - VA arguments to binary data
  2185. * bstr_printf() - Binary data to text string
  2186. */
  2187. /**
  2188. * vbin_printf - Parse a format string and place args' binary value in a buffer
  2189. * @bin_buf: The buffer to place args' binary value
  2190. * @size: The size of the buffer(by words(32bits), not characters)
  2191. * @fmt: The format string to use
  2192. * @args: Arguments for the format string
  2193. *
  2194. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  2195. * is skipped.
  2196. *
  2197. * The return value is the number of words(32bits) which would be generated for
  2198. * the given input.
  2199. *
  2200. * NOTE:
  2201. * If the return value is greater than @size, the resulting bin_buf is NOT
  2202. * valid for bstr_printf().
  2203. */
  2204. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  2205. {
  2206. struct printf_spec spec = {0};
  2207. char *str, *end;
  2208. int width;
  2209. str = (char *)bin_buf;
  2210. end = (char *)(bin_buf + size);
  2211. #define save_arg(type) \
  2212. ({ \
  2213. unsigned long long value; \
  2214. if (sizeof(type) == 8) { \
  2215. unsigned long long val8; \
  2216. str = PTR_ALIGN(str, sizeof(u32)); \
  2217. val8 = va_arg(args, unsigned long long); \
  2218. if (str + sizeof(type) <= end) { \
  2219. *(u32 *)str = *(u32 *)&val8; \
  2220. *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
  2221. } \
  2222. value = val8; \
  2223. } else { \
  2224. unsigned int val4; \
  2225. str = PTR_ALIGN(str, sizeof(type)); \
  2226. val4 = va_arg(args, int); \
  2227. if (str + sizeof(type) <= end) \
  2228. *(typeof(type) *)str = (type)(long)val4; \
  2229. value = (unsigned long long)val4; \
  2230. } \
  2231. str += sizeof(type); \
  2232. value; \
  2233. })
  2234. while (*fmt) {
  2235. int read = format_decode(fmt, &spec);
  2236. fmt += read;
  2237. switch (spec.type) {
  2238. case FORMAT_TYPE_NONE:
  2239. case FORMAT_TYPE_PERCENT_CHAR:
  2240. break;
  2241. case FORMAT_TYPE_INVALID:
  2242. goto out;
  2243. case FORMAT_TYPE_WIDTH:
  2244. case FORMAT_TYPE_PRECISION:
  2245. width = (int)save_arg(int);
  2246. /* Pointers may require the width */
  2247. if (*fmt == 'p')
  2248. set_field_width(&spec, width);
  2249. break;
  2250. case FORMAT_TYPE_CHAR:
  2251. save_arg(char);
  2252. break;
  2253. case FORMAT_TYPE_STR: {
  2254. const char *save_str = va_arg(args, char *);
  2255. size_t len;
  2256. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  2257. || (unsigned long)save_str < PAGE_SIZE)
  2258. save_str = "(null)";
  2259. len = strlen(save_str) + 1;
  2260. if (str + len < end)
  2261. memcpy(str, save_str, len);
  2262. str += len;
  2263. break;
  2264. }
  2265. case FORMAT_TYPE_PTR:
  2266. /* Dereferenced pointers must be done now */
  2267. switch (*fmt) {
  2268. /* Dereference of functions is still OK */
  2269. case 'S':
  2270. case 's':
  2271. case 'F':
  2272. case 'f':
  2273. save_arg(void *);
  2274. break;
  2275. default:
  2276. if (!isalnum(*fmt)) {
  2277. save_arg(void *);
  2278. break;
  2279. }
  2280. str = pointer(fmt, str, end, va_arg(args, void *),
  2281. spec);
  2282. if (str + 1 < end)
  2283. *str++ = '\0';
  2284. else
  2285. end[-1] = '\0'; /* Must be nul terminated */
  2286. }
  2287. /* skip all alphanumeric pointer suffixes */
  2288. while (isalnum(*fmt))
  2289. fmt++;
  2290. break;
  2291. default:
  2292. switch (spec.type) {
  2293. case FORMAT_TYPE_LONG_LONG:
  2294. save_arg(long long);
  2295. break;
  2296. case FORMAT_TYPE_ULONG:
  2297. case FORMAT_TYPE_LONG:
  2298. save_arg(unsigned long);
  2299. break;
  2300. case FORMAT_TYPE_SIZE_T:
  2301. save_arg(size_t);
  2302. break;
  2303. case FORMAT_TYPE_PTRDIFF:
  2304. save_arg(ptrdiff_t);
  2305. break;
  2306. case FORMAT_TYPE_UBYTE:
  2307. case FORMAT_TYPE_BYTE:
  2308. save_arg(char);
  2309. break;
  2310. case FORMAT_TYPE_USHORT:
  2311. case FORMAT_TYPE_SHORT:
  2312. save_arg(short);
  2313. break;
  2314. default:
  2315. save_arg(int);
  2316. }
  2317. }
  2318. }
  2319. out:
  2320. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  2321. #undef save_arg
  2322. }
  2323. EXPORT_SYMBOL_GPL(vbin_printf);
  2324. /**
  2325. * bstr_printf - Format a string from binary arguments and place it in a buffer
  2326. * @buf: The buffer to place the result into
  2327. * @size: The size of the buffer, including the trailing null space
  2328. * @fmt: The format string to use
  2329. * @bin_buf: Binary arguments for the format string
  2330. *
  2331. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  2332. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  2333. * a binary buffer that generated by vbin_printf.
  2334. *
  2335. * The format follows C99 vsnprintf, but has some extensions:
  2336. * see vsnprintf comment for details.
  2337. *
  2338. * The return value is the number of characters which would
  2339. * be generated for the given input, excluding the trailing
  2340. * '\0', as per ISO C99. If you want to have the exact
  2341. * number of characters written into @buf as return value
  2342. * (not including the trailing '\0'), use vscnprintf(). If the
  2343. * return is greater than or equal to @size, the resulting
  2344. * string is truncated.
  2345. */
  2346. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  2347. {
  2348. struct printf_spec spec = {0};
  2349. char *str, *end;
  2350. const char *args = (const char *)bin_buf;
  2351. if (WARN_ON_ONCE(size > INT_MAX))
  2352. return 0;
  2353. str = buf;
  2354. end = buf + size;
  2355. #define get_arg(type) \
  2356. ({ \
  2357. typeof(type) value; \
  2358. if (sizeof(type) == 8) { \
  2359. args = PTR_ALIGN(args, sizeof(u32)); \
  2360. *(u32 *)&value = *(u32 *)args; \
  2361. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  2362. } else { \
  2363. args = PTR_ALIGN(args, sizeof(type)); \
  2364. value = *(typeof(type) *)args; \
  2365. } \
  2366. args += sizeof(type); \
  2367. value; \
  2368. })
  2369. /* Make sure end is always >= buf */
  2370. if (end < buf) {
  2371. end = ((void *)-1);
  2372. size = end - buf;
  2373. }
  2374. while (*fmt) {
  2375. const char *old_fmt = fmt;
  2376. int read = format_decode(fmt, &spec);
  2377. fmt += read;
  2378. switch (spec.type) {
  2379. case FORMAT_TYPE_NONE: {
  2380. int copy = read;
  2381. if (str < end) {
  2382. if (copy > end - str)
  2383. copy = end - str;
  2384. memcpy(str, old_fmt, copy);
  2385. }
  2386. str += read;
  2387. break;
  2388. }
  2389. case FORMAT_TYPE_WIDTH:
  2390. set_field_width(&spec, get_arg(int));
  2391. break;
  2392. case FORMAT_TYPE_PRECISION:
  2393. set_precision(&spec, get_arg(int));
  2394. break;
  2395. case FORMAT_TYPE_CHAR: {
  2396. char c;
  2397. if (!(spec.flags & LEFT)) {
  2398. while (--spec.field_width > 0) {
  2399. if (str < end)
  2400. *str = ' ';
  2401. ++str;
  2402. }
  2403. }
  2404. c = (unsigned char) get_arg(char);
  2405. if (str < end)
  2406. *str = c;
  2407. ++str;
  2408. while (--spec.field_width > 0) {
  2409. if (str < end)
  2410. *str = ' ';
  2411. ++str;
  2412. }
  2413. break;
  2414. }
  2415. case FORMAT_TYPE_STR: {
  2416. const char *str_arg = args;
  2417. args += strlen(str_arg) + 1;
  2418. str = string(str, end, (char *)str_arg, spec);
  2419. break;
  2420. }
  2421. case FORMAT_TYPE_PTR: {
  2422. bool process = false;
  2423. int copy, len;
  2424. /* Non function dereferences were already done */
  2425. switch (*fmt) {
  2426. case 'S':
  2427. case 's':
  2428. case 'F':
  2429. case 'f':
  2430. process = true;
  2431. break;
  2432. default:
  2433. if (!isalnum(*fmt)) {
  2434. process = true;
  2435. break;
  2436. }
  2437. /* Pointer dereference was already processed */
  2438. if (str < end) {
  2439. len = copy = strlen(args);
  2440. if (copy > end - str)
  2441. copy = end - str;
  2442. memcpy(str, args, copy);
  2443. str += len;
  2444. args += len;
  2445. }
  2446. }
  2447. if (process)
  2448. str = pointer(fmt, str, end, get_arg(void *), spec);
  2449. while (isalnum(*fmt))
  2450. fmt++;
  2451. break;
  2452. }
  2453. case FORMAT_TYPE_PERCENT_CHAR:
  2454. if (str < end)
  2455. *str = '%';
  2456. ++str;
  2457. break;
  2458. case FORMAT_TYPE_INVALID:
  2459. goto out;
  2460. default: {
  2461. unsigned long long num;
  2462. switch (spec.type) {
  2463. case FORMAT_TYPE_LONG_LONG:
  2464. num = get_arg(long long);
  2465. break;
  2466. case FORMAT_TYPE_ULONG:
  2467. case FORMAT_TYPE_LONG:
  2468. num = get_arg(unsigned long);
  2469. break;
  2470. case FORMAT_TYPE_SIZE_T:
  2471. num = get_arg(size_t);
  2472. break;
  2473. case FORMAT_TYPE_PTRDIFF:
  2474. num = get_arg(ptrdiff_t);
  2475. break;
  2476. case FORMAT_TYPE_UBYTE:
  2477. num = get_arg(unsigned char);
  2478. break;
  2479. case FORMAT_TYPE_BYTE:
  2480. num = get_arg(signed char);
  2481. break;
  2482. case FORMAT_TYPE_USHORT:
  2483. num = get_arg(unsigned short);
  2484. break;
  2485. case FORMAT_TYPE_SHORT:
  2486. num = get_arg(short);
  2487. break;
  2488. case FORMAT_TYPE_UINT:
  2489. num = get_arg(unsigned int);
  2490. break;
  2491. default:
  2492. num = get_arg(int);
  2493. }
  2494. str = number(str, end, num, spec);
  2495. } /* default: */
  2496. } /* switch(spec.type) */
  2497. } /* while(*fmt) */
  2498. out:
  2499. if (size > 0) {
  2500. if (str < end)
  2501. *str = '\0';
  2502. else
  2503. end[-1] = '\0';
  2504. }
  2505. #undef get_arg
  2506. /* the trailing null byte doesn't count towards the total */
  2507. return str - buf;
  2508. }
  2509. EXPORT_SYMBOL_GPL(bstr_printf);
  2510. /**
  2511. * bprintf - Parse a format string and place args' binary value in a buffer
  2512. * @bin_buf: The buffer to place args' binary value
  2513. * @size: The size of the buffer(by words(32bits), not characters)
  2514. * @fmt: The format string to use
  2515. * @...: Arguments for the format string
  2516. *
  2517. * The function returns the number of words(u32) written
  2518. * into @bin_buf.
  2519. */
  2520. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
  2521. {
  2522. va_list args;
  2523. int ret;
  2524. va_start(args, fmt);
  2525. ret = vbin_printf(bin_buf, size, fmt, args);
  2526. va_end(args);
  2527. return ret;
  2528. }
  2529. EXPORT_SYMBOL_GPL(bprintf);
  2530. #endif /* CONFIG_BINARY_PRINTF */
  2531. /**
  2532. * vsscanf - Unformat a buffer into a list of arguments
  2533. * @buf: input buffer
  2534. * @fmt: format of buffer
  2535. * @args: arguments
  2536. */
  2537. int vsscanf(const char *buf, const char *fmt, va_list args)
  2538. {
  2539. const char *str = buf;
  2540. char *next;
  2541. char digit;
  2542. int num = 0;
  2543. u8 qualifier;
  2544. unsigned int base;
  2545. union {
  2546. long long s;
  2547. unsigned long long u;
  2548. } val;
  2549. s16 field_width;
  2550. bool is_sign;
  2551. while (*fmt) {
  2552. /* skip any white space in format */
  2553. /* white space in format matchs any amount of
  2554. * white space, including none, in the input.
  2555. */
  2556. if (isspace(*fmt)) {
  2557. fmt = skip_spaces(++fmt);
  2558. str = skip_spaces(str);
  2559. }
  2560. /* anything that is not a conversion must match exactly */
  2561. if (*fmt != '%' && *fmt) {
  2562. if (*fmt++ != *str++)
  2563. break;
  2564. continue;
  2565. }
  2566. if (!*fmt)
  2567. break;
  2568. ++fmt;
  2569. /* skip this conversion.
  2570. * advance both strings to next white space
  2571. */
  2572. if (*fmt == '*') {
  2573. if (!*str)
  2574. break;
  2575. while (!isspace(*fmt) && *fmt != '%' && *fmt) {
  2576. /* '%*[' not yet supported, invalid format */
  2577. if (*fmt == '[')
  2578. return num;
  2579. fmt++;
  2580. }
  2581. while (!isspace(*str) && *str)
  2582. str++;
  2583. continue;
  2584. }
  2585. /* get field width */
  2586. field_width = -1;
  2587. if (isdigit(*fmt)) {
  2588. field_width = skip_atoi(&fmt);
  2589. if (field_width <= 0)
  2590. break;
  2591. }
  2592. /* get conversion qualifier */
  2593. qualifier = -1;
  2594. if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  2595. *fmt == 'z') {
  2596. qualifier = *fmt++;
  2597. if (unlikely(qualifier == *fmt)) {
  2598. if (qualifier == 'h') {
  2599. qualifier = 'H';
  2600. fmt++;
  2601. } else if (qualifier == 'l') {
  2602. qualifier = 'L';
  2603. fmt++;
  2604. }
  2605. }
  2606. }
  2607. if (!*fmt)
  2608. break;
  2609. if (*fmt == 'n') {
  2610. /* return number of characters read so far */
  2611. *va_arg(args, int *) = str - buf;
  2612. ++fmt;
  2613. continue;
  2614. }
  2615. if (!*str)
  2616. break;
  2617. base = 10;
  2618. is_sign = false;
  2619. switch (*fmt++) {
  2620. case 'c':
  2621. {
  2622. char *s = (char *)va_arg(args, char*);
  2623. if (field_width == -1)
  2624. field_width = 1;
  2625. do {
  2626. *s++ = *str++;
  2627. } while (--field_width > 0 && *str);
  2628. num++;
  2629. }
  2630. continue;
  2631. case 's':
  2632. {
  2633. char *s = (char *)va_arg(args, char *);
  2634. if (field_width == -1)
  2635. field_width = SHRT_MAX;
  2636. /* first, skip leading white space in buffer */
  2637. str = skip_spaces(str);
  2638. /* now copy until next white space */
  2639. while (*str && !isspace(*str) && field_width--)
  2640. *s++ = *str++;
  2641. *s = '\0';
  2642. num++;
  2643. }
  2644. continue;
  2645. /*
  2646. * Warning: This implementation of the '[' conversion specifier
  2647. * deviates from its glibc counterpart in the following ways:
  2648. * (1) It does NOT support ranges i.e. '-' is NOT a special
  2649. * character
  2650. * (2) It cannot match the closing bracket ']' itself
  2651. * (3) A field width is required
  2652. * (4) '%*[' (discard matching input) is currently not supported
  2653. *
  2654. * Example usage:
  2655. * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
  2656. * buf1, buf2, buf3);
  2657. * if (ret < 3)
  2658. * // etc..
  2659. */
  2660. case '[':
  2661. {
  2662. char *s = (char *)va_arg(args, char *);
  2663. DECLARE_BITMAP(set, 256) = {0};
  2664. unsigned int len = 0;
  2665. bool negate = (*fmt == '^');
  2666. /* field width is required */
  2667. if (field_width == -1)
  2668. return num;
  2669. if (negate)
  2670. ++fmt;
  2671. for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
  2672. set_bit((u8)*fmt, set);
  2673. /* no ']' or no character set found */
  2674. if (!*fmt || !len)
  2675. return num;
  2676. ++fmt;
  2677. if (negate) {
  2678. bitmap_complement(set, set, 256);
  2679. /* exclude null '\0' byte */
  2680. clear_bit(0, set);
  2681. }
  2682. /* match must be non-empty */
  2683. if (!test_bit((u8)*str, set))
  2684. return num;
  2685. while (test_bit((u8)*str, set) && field_width--)
  2686. *s++ = *str++;
  2687. *s = '\0';
  2688. ++num;
  2689. }
  2690. continue;
  2691. case 'o':
  2692. base = 8;
  2693. break;
  2694. case 'x':
  2695. case 'X':
  2696. base = 16;
  2697. break;
  2698. case 'i':
  2699. base = 0;
  2700. /* fall through */
  2701. case 'd':
  2702. is_sign = true;
  2703. /* fall through */
  2704. case 'u':
  2705. break;
  2706. case '%':
  2707. /* looking for '%' in str */
  2708. if (*str++ != '%')
  2709. return num;
  2710. continue;
  2711. default:
  2712. /* invalid format; stop here */
  2713. return num;
  2714. }
  2715. /* have some sort of integer conversion.
  2716. * first, skip white space in buffer.
  2717. */
  2718. str = skip_spaces(str);
  2719. digit = *str;
  2720. if (is_sign && digit == '-')
  2721. digit = *(str + 1);
  2722. if (!digit
  2723. || (base == 16 && !isxdigit(digit))
  2724. || (base == 10 && !isdigit(digit))
  2725. || (base == 8 && (!isdigit(digit) || digit > '7'))
  2726. || (base == 0 && !isdigit(digit)))
  2727. break;
  2728. if (is_sign)
  2729. val.s = qualifier != 'L' ?
  2730. simple_strtol(str, &next, base) :
  2731. simple_strtoll(str, &next, base);
  2732. else
  2733. val.u = qualifier != 'L' ?
  2734. simple_strtoul(str, &next, base) :
  2735. simple_strtoull(str, &next, base);
  2736. if (field_width > 0 && next - str > field_width) {
  2737. if (base == 0)
  2738. _parse_integer_fixup_radix(str, &base);
  2739. while (next - str > field_width) {
  2740. if (is_sign)
  2741. val.s = div_s64(val.s, base);
  2742. else
  2743. val.u = div_u64(val.u, base);
  2744. --next;
  2745. }
  2746. }
  2747. switch (qualifier) {
  2748. case 'H': /* that's 'hh' in format */
  2749. if (is_sign)
  2750. *va_arg(args, signed char *) = val.s;
  2751. else
  2752. *va_arg(args, unsigned char *) = val.u;
  2753. break;
  2754. case 'h':
  2755. if (is_sign)
  2756. *va_arg(args, short *) = val.s;
  2757. else
  2758. *va_arg(args, unsigned short *) = val.u;
  2759. break;
  2760. case 'l':
  2761. if (is_sign)
  2762. *va_arg(args, long *) = val.s;
  2763. else
  2764. *va_arg(args, unsigned long *) = val.u;
  2765. break;
  2766. case 'L':
  2767. if (is_sign)
  2768. *va_arg(args, long long *) = val.s;
  2769. else
  2770. *va_arg(args, unsigned long long *) = val.u;
  2771. break;
  2772. case 'z':
  2773. *va_arg(args, size_t *) = val.u;
  2774. break;
  2775. default:
  2776. if (is_sign)
  2777. *va_arg(args, int *) = val.s;
  2778. else
  2779. *va_arg(args, unsigned int *) = val.u;
  2780. break;
  2781. }
  2782. num++;
  2783. if (!next)
  2784. break;
  2785. str = next;
  2786. }
  2787. return num;
  2788. }
  2789. EXPORT_SYMBOL(vsscanf);
  2790. /**
  2791. * sscanf - Unformat a buffer into a list of arguments
  2792. * @buf: input buffer
  2793. * @fmt: formatting of buffer
  2794. * @...: resulting arguments
  2795. */
  2796. int sscanf(const char *buf, const char *fmt, ...)
  2797. {
  2798. va_list args;
  2799. int i;
  2800. va_start(args, fmt);
  2801. i = vsscanf(buf, fmt, args);
  2802. va_end(args);
  2803. return i;
  2804. }
  2805. EXPORT_SYMBOL(sscanf);