vsprintf.c 74 KB

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