vsprintf.c 75 KB

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