vsprintf.c 87 KB

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