vsprintf.c 73 KB

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