vsprintf.c 84 KB

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