xref: /openbmc/linux/lib/vsprintf.c (revision 3a129cc2151425e5aeb69aeb25fbc994ec738137)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/lib/vsprintf.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * Wirzenius wrote this portably, Torvalds fucked it up :-)
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
141da177e4SLinus Torvalds  * - changed to provide snprintf and vsnprintf functions
151da177e4SLinus Torvalds  * So Feb  1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
161da177e4SLinus Torvalds  * - scnprintf and vscnprintf
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <stdarg.h>
200d1d7a55SStephen Boyd #include <linux/clk.h>
21900cca29SGeert Uytterhoeven #include <linux/clk-provider.h>
228bc3bcc9SPaul Gortmaker #include <linux/module.h>	/* for KSYM_SYMBOL_LEN */
231da177e4SLinus Torvalds #include <linux/types.h>
241da177e4SLinus Torvalds #include <linux/string.h>
251da177e4SLinus Torvalds #include <linux/ctype.h>
261da177e4SLinus Torvalds #include <linux/kernel.h>
270fe1ef24SLinus Torvalds #include <linux/kallsyms.h>
2853809751SJan Beulich #include <linux/math64.h>
290fe1ef24SLinus Torvalds #include <linux/uaccess.h>
30332d2e78SLinus Torvalds #include <linux/ioport.h>
314b6ccca7SAl Viro #include <linux/dcache.h>
32312b4e22SRyan Mallon #include <linux/cred.h>
332b1b0d66SAndy Shevchenko #include <linux/uuid.h>
34ce4fecf1SPantelis Antoniou #include <linux/of.h>
358a27f7c9SJoe Perches #include <net/addrconf.h>
36ad67b74dSTobin C. Harding #include <linux/siphash.h>
37ad67b74dSTobin C. Harding #include <linux/compiler.h>
381031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
391031bc58SDmitry Monakhov #include <linux/blkdev.h>
401031bc58SDmitry Monakhov #endif
411da177e4SLinus Torvalds 
42edf14cdbSVlastimil Babka #include "../mm/internal.h"	/* For the trace_print_flags arrays */
43edf14cdbSVlastimil Babka 
444e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
457c43d9a3SRasmus Villemoes #include <asm/byteorder.h>	/* cpu_to_le16 */
461da177e4SLinus Torvalds 
4771dca95dSAndy Shevchenko #include <linux/string_helpers.h>
481dff46d6SAlexey Dobriyan #include "kstrtox.h"
49aa46a63eSHarvey Harrison 
501da177e4SLinus Torvalds /**
511da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
521da177e4SLinus Torvalds  * @cp: The start of the string
531da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
541da177e4SLinus Torvalds  * @base: The number base to use
55462e4711SEldad Zack  *
56462e4711SEldad Zack  * This function is obsolete. Please use kstrtoull instead.
571da177e4SLinus Torvalds  */
581da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
591da177e4SLinus Torvalds {
601dff46d6SAlexey Dobriyan 	unsigned long long result;
611dff46d6SAlexey Dobriyan 	unsigned int rv;
621da177e4SLinus Torvalds 
631dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
641dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
651dff46d6SAlexey Dobriyan 	/* FIXME */
661dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
67aa46a63eSHarvey Harrison 
681da177e4SLinus Torvalds 	if (endp)
691da177e4SLinus Torvalds 		*endp = (char *)cp;
707b9186f5SAndré Goddard Rosa 
711da177e4SLinus Torvalds 	return result;
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /**
76922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
77922ac25cSAndré Goddard Rosa  * @cp: The start of the string
78922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
79922ac25cSAndré Goddard Rosa  * @base: The number base to use
80462e4711SEldad Zack  *
81462e4711SEldad Zack  * This function is obsolete. Please use kstrtoul instead.
82922ac25cSAndré Goddard Rosa  */
83922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
84922ac25cSAndré Goddard Rosa {
85922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
86922ac25cSAndré Goddard Rosa }
87922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
88922ac25cSAndré Goddard Rosa 
89922ac25cSAndré Goddard Rosa /**
90922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
91922ac25cSAndré Goddard Rosa  * @cp: The start of the string
92922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
93922ac25cSAndré Goddard Rosa  * @base: The number base to use
94462e4711SEldad Zack  *
95462e4711SEldad Zack  * This function is obsolete. Please use kstrtol instead.
96922ac25cSAndré Goddard Rosa  */
97922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
98922ac25cSAndré Goddard Rosa {
99922ac25cSAndré Goddard Rosa 	if (*cp == '-')
100922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
101922ac25cSAndré Goddard Rosa 
102922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
103922ac25cSAndré Goddard Rosa }
104922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
105922ac25cSAndré Goddard Rosa 
106922ac25cSAndré Goddard Rosa /**
1071da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
1081da177e4SLinus Torvalds  * @cp: The start of the string
1091da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1101da177e4SLinus Torvalds  * @base: The number base to use
111462e4711SEldad Zack  *
112462e4711SEldad Zack  * This function is obsolete. Please use kstrtoll instead.
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	if (*cp == '-')
1171da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1187b9186f5SAndré Goddard Rosa 
1191da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1201da177e4SLinus Torvalds }
12198d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1221da177e4SLinus Torvalds 
123cf3b429bSJoe Perches static noinline_for_stack
124cf3b429bSJoe Perches int skip_atoi(const char **s)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	int i = 0;
1271da177e4SLinus Torvalds 
12843e5b666SRasmus Villemoes 	do {
1291da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
13043e5b666SRasmus Villemoes 	} while (isdigit(**s));
1317b9186f5SAndré Goddard Rosa 
1321da177e4SLinus Torvalds 	return i;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
1357c43d9a3SRasmus Villemoes /*
1367c43d9a3SRasmus Villemoes  * Decimal conversion is by far the most typical, and is used for
1377c43d9a3SRasmus Villemoes  * /proc and /sys data. This directly impacts e.g. top performance
1387c43d9a3SRasmus Villemoes  * with many processes running. We optimize it for speed by emitting
1397c43d9a3SRasmus Villemoes  * two characters at a time, using a 200 byte lookup table. This
1407c43d9a3SRasmus Villemoes  * roughly halves the number of multiplications compared to computing
1417c43d9a3SRasmus Villemoes  * the digits one at a time. Implementation strongly inspired by the
1427c43d9a3SRasmus Villemoes  * previous version, which in turn used ideas described at
1437c43d9a3SRasmus Villemoes  * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
1447c43d9a3SRasmus Villemoes  * from the author, Douglas W. Jones).
1457c43d9a3SRasmus Villemoes  *
1467c43d9a3SRasmus Villemoes  * It turns out there is precisely one 26 bit fixed-point
1477c43d9a3SRasmus Villemoes  * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
1487c43d9a3SRasmus Villemoes  * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
1497c43d9a3SRasmus Villemoes  * range happens to be somewhat larger (x <= 1073741898), but that's
1507c43d9a3SRasmus Villemoes  * irrelevant for our purpose.
1517c43d9a3SRasmus Villemoes  *
1527c43d9a3SRasmus Villemoes  * For dividing a number in the range [10^4, 10^6-1] by 100, we still
1537c43d9a3SRasmus Villemoes  * need a 32x32->64 bit multiply, so we simply use the same constant.
1547c43d9a3SRasmus Villemoes  *
1557c43d9a3SRasmus Villemoes  * For dividing a number in the range [100, 10^4-1] by 100, there are
1567c43d9a3SRasmus Villemoes  * several options. The simplest is (x * 0x147b) >> 19, which is valid
1577c43d9a3SRasmus Villemoes  * for all x <= 43698.
158133fd9f5SDenys Vlasenko  */
1594277eeddSDenis Vlasenko 
1607c43d9a3SRasmus Villemoes static const u16 decpair[100] = {
1617c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
1627c43d9a3SRasmus Villemoes 	_( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
1637c43d9a3SRasmus Villemoes 	_(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
1647c43d9a3SRasmus Villemoes 	_(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
1657c43d9a3SRasmus Villemoes 	_(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
1667c43d9a3SRasmus Villemoes 	_(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
1677c43d9a3SRasmus Villemoes 	_(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
1687c43d9a3SRasmus Villemoes 	_(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
1697c43d9a3SRasmus Villemoes 	_(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
1707c43d9a3SRasmus Villemoes 	_(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
1717c43d9a3SRasmus Villemoes 	_(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
1727c43d9a3SRasmus Villemoes #undef _
1737c43d9a3SRasmus Villemoes };
1744277eeddSDenis Vlasenko 
1757b9186f5SAndré Goddard Rosa /*
1767c43d9a3SRasmus Villemoes  * This will print a single '0' even if r == 0, since we would
177675cf53cSRasmus Villemoes  * immediately jump to out_r where two 0s would be written but only
178675cf53cSRasmus Villemoes  * one of them accounted for in buf. This is needed by ip4_string
179675cf53cSRasmus Villemoes  * below. All other callers pass a non-zero value of r.
180133fd9f5SDenys Vlasenko */
181133fd9f5SDenys Vlasenko static noinline_for_stack
182133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
183133fd9f5SDenys Vlasenko {
184133fd9f5SDenys Vlasenko 	unsigned q;
1854277eeddSDenis Vlasenko 
1867c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^8 */
1877c43d9a3SRasmus Villemoes 	if (r < 100)
1887c43d9a3SRasmus Villemoes 		goto out_r;
189cb239d0aSGeorge Spelvin 
1907c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^8 */
1917c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
1927c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
1937c43d9a3SRasmus Villemoes 	buf += 2;
1947c43d9a3SRasmus Villemoes 
1957c43d9a3SRasmus Villemoes 	/* 1 <= q < 10^6 */
1967c43d9a3SRasmus Villemoes 	if (q < 100)
1977c43d9a3SRasmus Villemoes 		goto out_q;
1987c43d9a3SRasmus Villemoes 
1997c43d9a3SRasmus Villemoes 	/*  100 <= q < 10^6 */
2007c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
2017c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
2027c43d9a3SRasmus Villemoes 	buf += 2;
2037c43d9a3SRasmus Villemoes 
2047c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^4 */
2057c43d9a3SRasmus Villemoes 	if (r < 100)
2067c43d9a3SRasmus Villemoes 		goto out_r;
2077c43d9a3SRasmus Villemoes 
2087c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^4 */
2097c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2107c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2117c43d9a3SRasmus Villemoes 	buf += 2;
2127c43d9a3SRasmus Villemoes out_q:
2137c43d9a3SRasmus Villemoes 	/* 1 <= q < 100 */
2147c43d9a3SRasmus Villemoes 	r = q;
2157c43d9a3SRasmus Villemoes out_r:
2167c43d9a3SRasmus Villemoes 	/* 1 <= r < 100 */
2177c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r];
218675cf53cSRasmus Villemoes 	buf += r < 10 ? 1 : 2;
219133fd9f5SDenys Vlasenko 	return buf;
220133fd9f5SDenys Vlasenko }
221133fd9f5SDenys Vlasenko 
2227c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
2237c43d9a3SRasmus Villemoes static noinline_for_stack
2247c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r)
2257c43d9a3SRasmus Villemoes {
2267c43d9a3SRasmus Villemoes 	unsigned q;
227133fd9f5SDenys Vlasenko 
2287c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^8 */
2297c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
2307c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2317c43d9a3SRasmus Villemoes 	buf += 2;
232133fd9f5SDenys Vlasenko 
2337c43d9a3SRasmus Villemoes 	/* 0 <= q < 10^6 */
2347c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
2357c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
2367c43d9a3SRasmus Villemoes 	buf += 2;
237133fd9f5SDenys Vlasenko 
2387c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2397c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2407c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2417c43d9a3SRasmus Villemoes 	buf += 2;
2427c43d9a3SRasmus Villemoes 
2437c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2447c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2457c43d9a3SRasmus Villemoes 	buf += 2;
2467c43d9a3SRasmus Villemoes 	return buf;
2477c43d9a3SRasmus Villemoes }
2487c43d9a3SRasmus Villemoes 
2497c43d9a3SRasmus Villemoes static noinline_for_stack
250133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
251133fd9f5SDenys Vlasenko {
252133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000)
2537c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2547c43d9a3SRasmus Villemoes 	/* 1 <= n <= 1.6e11 */
2557c43d9a3SRasmus Villemoes 	if (n >= 100*1000*1000)
2567c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2577c43d9a3SRasmus Villemoes 	/* 1 <= n < 1e8 */
258133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
259133fd9f5SDenys Vlasenko }
260133fd9f5SDenys Vlasenko 
2617c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
262133fd9f5SDenys Vlasenko 
2637c43d9a3SRasmus Villemoes static void
2647c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r)
265133fd9f5SDenys Vlasenko {
2667c43d9a3SRasmus Villemoes 	unsigned q;
2677c43d9a3SRasmus Villemoes 
2687c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2697c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2707c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2717c43d9a3SRasmus Villemoes 	buf += 2;
2727c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2737c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2742359172aSGeorge Spelvin }
2752359172aSGeorge Spelvin 
2762359172aSGeorge Spelvin /*
2772359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2782359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2792359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2802359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
2817c43d9a3SRasmus Villemoes  * (second call in the put_dec code, assuming n is all-ones).
2822359172aSGeorge Spelvin  */
2837c43d9a3SRasmus Villemoes static noinline_for_stack
2842359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2852359172aSGeorge Spelvin {
2862359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2872359172aSGeorge Spelvin 
2882359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2892359172aSGeorge Spelvin         return q;
290133fd9f5SDenys Vlasenko }
291133fd9f5SDenys Vlasenko 
292133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
293133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
294133fd9f5SDenys Vlasenko  * (with permission from the author).
295133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
296133fd9f5SDenys Vlasenko  */
297133fd9f5SDenys Vlasenko static
298133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
299133fd9f5SDenys Vlasenko {
300133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
301133fd9f5SDenys Vlasenko 
302133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
303133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
304133fd9f5SDenys Vlasenko 
305133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
306133fd9f5SDenys Vlasenko 	h   = (n >> 32);
307133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
308133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
309133fd9f5SDenys Vlasenko 
3107c43d9a3SRasmus Villemoes 	/* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
3117c43d9a3SRasmus Villemoes 	     = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
312133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
3132359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
314133fd9f5SDenys Vlasenko 
3152359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
3162359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
317133fd9f5SDenys Vlasenko 
3182359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
3192359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
320133fd9f5SDenys Vlasenko 
3212359172aSGeorge Spelvin 	q += 281 * d3;
3222359172aSGeorge Spelvin 	buf += 12;
3232359172aSGeorge Spelvin 	if (q)
3242359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3252359172aSGeorge Spelvin 	else while (buf[-1] == '0')
326133fd9f5SDenys Vlasenko 		--buf;
3277b9186f5SAndré Goddard Rosa 
3284277eeddSDenis Vlasenko 	return buf;
3294277eeddSDenis Vlasenko }
330133fd9f5SDenys Vlasenko 
331133fd9f5SDenys Vlasenko #endif
3324277eeddSDenis Vlasenko 
3331ac101a5SKAMEZAWA Hiroyuki /*
3341ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3351ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3361ac101a5SKAMEZAWA Hiroyuki  *
3371ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3381ac101a5SKAMEZAWA Hiroyuki  */
3391ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num)
3401ac101a5SKAMEZAWA Hiroyuki {
3417c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
3427c43d9a3SRasmus Villemoes 	char tmp[sizeof(num) * 3] __aligned(2);
3431ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3441ac101a5SKAMEZAWA Hiroyuki 
345133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
346133fd9f5SDenys Vlasenko 	if (num <= 9) {
347133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
348133fd9f5SDenys Vlasenko 		len = 1;
349133fd9f5SDenys Vlasenko 	} else {
3501ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
351133fd9f5SDenys Vlasenko 	}
3521ac101a5SKAMEZAWA Hiroyuki 
3531ac101a5SKAMEZAWA Hiroyuki 	if (len > size)
3541ac101a5SKAMEZAWA Hiroyuki 		return 0;
3551ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
3561ac101a5SKAMEZAWA Hiroyuki 		buf[idx] = tmp[len - idx - 1];
3571ac101a5SKAMEZAWA Hiroyuki 	return len;
3581ac101a5SKAMEZAWA Hiroyuki }
3591ac101a5SKAMEZAWA Hiroyuki 
36051be17dfSRasmus Villemoes #define SIGN	1		/* unsigned/signed, must be 1 */
361d1c1b121SRasmus Villemoes #define LEFT	2		/* left justified */
3621da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3631da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
364d1c1b121SRasmus Villemoes #define ZEROPAD	16		/* pad with zero, must be 16 == '0' - ' ' */
365b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
366b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3671da177e4SLinus Torvalds 
368fef20d9cSFrederic Weisbecker enum format_type {
369fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
370ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
371fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
372fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
373fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
374fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
375fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
376fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
377fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
378fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
379fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
380a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
381a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
382fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
383fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
384fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
385fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
386fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
387fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
388fef20d9cSFrederic Weisbecker };
389fef20d9cSFrederic Weisbecker 
390fef20d9cSFrederic Weisbecker struct printf_spec {
391d0484193SRasmus Villemoes 	unsigned int	type:8;		/* format_type enum */
392d0484193SRasmus Villemoes 	signed int	field_width:24;	/* width of output field */
393d0484193SRasmus Villemoes 	unsigned int	flags:8;	/* flags to number() */
394d0484193SRasmus Villemoes 	unsigned int	base:8;		/* number base, 8, 10 or 16 only */
395d0484193SRasmus Villemoes 	signed int	precision:16;	/* # of digits/chars */
396d0484193SRasmus Villemoes } __packed;
3974d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1)
3984d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1)
399fef20d9cSFrederic Weisbecker 
400cf3b429bSJoe Perches static noinline_for_stack
401cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
402fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
4031da177e4SLinus Torvalds {
4047c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
4057c43d9a3SRasmus Villemoes 	char tmp[3 * sizeof(num)] __aligned(2);
4069b706aeeSDenys Vlasenko 	char sign;
4079b706aeeSDenys Vlasenko 	char locase;
408fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4091da177e4SLinus Torvalds 	int i;
4107c203422SPierre Carrier 	bool is_zero = num == 0LL;
4111c7a8e62SRasmus Villemoes 	int field_width = spec.field_width;
4121c7a8e62SRasmus Villemoes 	int precision = spec.precision;
4131da177e4SLinus Torvalds 
414d0484193SRasmus Villemoes 	BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
415d0484193SRasmus Villemoes 
4169b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4179b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
418fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
419fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
420fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4211da177e4SLinus Torvalds 	sign = 0;
422fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4231da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4241da177e4SLinus Torvalds 			sign = '-';
4251da177e4SLinus Torvalds 			num = -(signed long long)num;
4261c7a8e62SRasmus Villemoes 			field_width--;
427fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4281da177e4SLinus Torvalds 			sign = '+';
4291c7a8e62SRasmus Villemoes 			field_width--;
430fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4311da177e4SLinus Torvalds 			sign = ' ';
4321c7a8e62SRasmus Villemoes 			field_width--;
4331da177e4SLinus Torvalds 		}
4341da177e4SLinus Torvalds 	}
435b39a7340SDenis Vlasenko 	if (need_pfx) {
436fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4371c7a8e62SRasmus Villemoes 			field_width -= 2;
4387c203422SPierre Carrier 		else if (!is_zero)
4391c7a8e62SRasmus Villemoes 			field_width--;
4401da177e4SLinus Torvalds 	}
441b39a7340SDenis Vlasenko 
442b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4431da177e4SLinus Torvalds 	i = 0;
444133fd9f5SDenys Vlasenko 	if (num < spec.base)
4453ea8d440SRasmus Villemoes 		tmp[i++] = hex_asc_upper[num] | locase;
446fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
447fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
448b39a7340SDenis Vlasenko 		int shift = 3;
4497b9186f5SAndré Goddard Rosa 
4507b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4517b9186f5SAndré Goddard Rosa 			shift = 4;
452b39a7340SDenis Vlasenko 		do {
4533ea8d440SRasmus Villemoes 			tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
454b39a7340SDenis Vlasenko 			num >>= shift;
455b39a7340SDenis Vlasenko 		} while (num);
4564277eeddSDenis Vlasenko 	} else { /* base 10 */
4574277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4584277eeddSDenis Vlasenko 	}
459b39a7340SDenis Vlasenko 
460b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
4611c7a8e62SRasmus Villemoes 	if (i > precision)
4621c7a8e62SRasmus Villemoes 		precision = i;
463b39a7340SDenis Vlasenko 	/* leading space padding */
4641c7a8e62SRasmus Villemoes 	field_width -= precision;
46551be17dfSRasmus Villemoes 	if (!(spec.flags & (ZEROPAD | LEFT))) {
4661c7a8e62SRasmus Villemoes 		while (--field_width >= 0) {
467f796937aSJeremy Fitzhardinge 			if (buf < end)
4681da177e4SLinus Torvalds 				*buf = ' ';
4691da177e4SLinus Torvalds 			++buf;
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 	}
472b39a7340SDenis Vlasenko 	/* sign */
4731da177e4SLinus Torvalds 	if (sign) {
474f796937aSJeremy Fitzhardinge 		if (buf < end)
4751da177e4SLinus Torvalds 			*buf = sign;
4761da177e4SLinus Torvalds 		++buf;
4771da177e4SLinus Torvalds 	}
478b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
479b39a7340SDenis Vlasenko 	if (need_pfx) {
4807c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
481f796937aSJeremy Fitzhardinge 			if (buf < end)
4821da177e4SLinus Torvalds 				*buf = '0';
4831da177e4SLinus Torvalds 			++buf;
4847c203422SPierre Carrier 		}
485fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
486f796937aSJeremy Fitzhardinge 			if (buf < end)
4879b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
4881da177e4SLinus Torvalds 			++buf;
4891da177e4SLinus Torvalds 		}
4901da177e4SLinus Torvalds 	}
491b39a7340SDenis Vlasenko 	/* zero or space padding */
492fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
493d1c1b121SRasmus Villemoes 		char c = ' ' + (spec.flags & ZEROPAD);
494d1c1b121SRasmus Villemoes 		BUILD_BUG_ON(' ' + ZEROPAD != '0');
4951c7a8e62SRasmus Villemoes 		while (--field_width >= 0) {
496f796937aSJeremy Fitzhardinge 			if (buf < end)
4971da177e4SLinus Torvalds 				*buf = c;
4981da177e4SLinus Torvalds 			++buf;
4991da177e4SLinus Torvalds 		}
5001da177e4SLinus Torvalds 	}
501b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
5021c7a8e62SRasmus Villemoes 	while (i <= --precision) {
503f796937aSJeremy Fitzhardinge 		if (buf < end)
5041da177e4SLinus Torvalds 			*buf = '0';
5051da177e4SLinus Torvalds 		++buf;
5061da177e4SLinus Torvalds 	}
507b39a7340SDenis Vlasenko 	/* actual digits of result */
508b39a7340SDenis Vlasenko 	while (--i >= 0) {
509f796937aSJeremy Fitzhardinge 		if (buf < end)
5101da177e4SLinus Torvalds 			*buf = tmp[i];
5111da177e4SLinus Torvalds 		++buf;
5121da177e4SLinus Torvalds 	}
513b39a7340SDenis Vlasenko 	/* trailing space padding */
5141c7a8e62SRasmus Villemoes 	while (--field_width >= 0) {
515f796937aSJeremy Fitzhardinge 		if (buf < end)
5161da177e4SLinus Torvalds 			*buf = ' ';
5171da177e4SLinus Torvalds 		++buf;
5181da177e4SLinus Torvalds 	}
5197b9186f5SAndré Goddard Rosa 
5201da177e4SLinus Torvalds 	return buf;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5233cab1e71SAndy Shevchenko static noinline_for_stack
5243cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
5253cab1e71SAndy Shevchenko {
5263cab1e71SAndy Shevchenko 	struct printf_spec spec;
5273cab1e71SAndy Shevchenko 
5283cab1e71SAndy Shevchenko 	spec.type = FORMAT_TYPE_PTR;
5293cab1e71SAndy Shevchenko 	spec.field_width = 2 + 2 * size;	/* 0x + hex */
5303cab1e71SAndy Shevchenko 	spec.flags = SPECIAL | SMALL | ZEROPAD;
5313cab1e71SAndy Shevchenko 	spec.base = 16;
5323cab1e71SAndy Shevchenko 	spec.precision = -1;
5333cab1e71SAndy Shevchenko 
5343cab1e71SAndy Shevchenko 	return number(buf, end, num, spec);
5353cab1e71SAndy Shevchenko }
5363cab1e71SAndy Shevchenko 
537cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
5384b6ccca7SAl Viro {
5394b6ccca7SAl Viro 	size_t size;
5404b6ccca7SAl Viro 	if (buf >= end)	/* nowhere to put anything */
5414b6ccca7SAl Viro 		return;
5424b6ccca7SAl Viro 	size = end - buf;
5434b6ccca7SAl Viro 	if (size <= spaces) {
5444b6ccca7SAl Viro 		memset(buf, ' ', size);
5454b6ccca7SAl Viro 		return;
5464b6ccca7SAl Viro 	}
5474b6ccca7SAl Viro 	if (len) {
5484b6ccca7SAl Viro 		if (len > size - spaces)
5494b6ccca7SAl Viro 			len = size - spaces;
5504b6ccca7SAl Viro 		memmove(buf + spaces, buf, len);
5514b6ccca7SAl Viro 	}
5524b6ccca7SAl Viro 	memset(buf, ' ', spaces);
5534b6ccca7SAl Viro }
5544b6ccca7SAl Viro 
555cfccde04SRasmus Villemoes /*
556cfccde04SRasmus Villemoes  * Handle field width padding for a string.
557cfccde04SRasmus Villemoes  * @buf: current buffer position
558cfccde04SRasmus Villemoes  * @n: length of string
559cfccde04SRasmus Villemoes  * @end: end of output buffer
560cfccde04SRasmus Villemoes  * @spec: for field width and flags
561cfccde04SRasmus Villemoes  * Returns: new buffer position after padding.
562cfccde04SRasmus Villemoes  */
563cfccde04SRasmus Villemoes static noinline_for_stack
564cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
565cfccde04SRasmus Villemoes {
566cfccde04SRasmus Villemoes 	unsigned spaces;
567cfccde04SRasmus Villemoes 
568cfccde04SRasmus Villemoes 	if (likely(n >= spec.field_width))
569cfccde04SRasmus Villemoes 		return buf;
570cfccde04SRasmus Villemoes 	/* we want to pad the sucker */
571cfccde04SRasmus Villemoes 	spaces = spec.field_width - n;
572cfccde04SRasmus Villemoes 	if (!(spec.flags & LEFT)) {
573cfccde04SRasmus Villemoes 		move_right(buf - n, end, n, spaces);
574cfccde04SRasmus Villemoes 		return buf + spaces;
575cfccde04SRasmus Villemoes 	}
576cfccde04SRasmus Villemoes 	while (spaces--) {
577cfccde04SRasmus Villemoes 		if (buf < end)
578cfccde04SRasmus Villemoes 			*buf = ' ';
579cfccde04SRasmus Villemoes 		++buf;
580cfccde04SRasmus Villemoes 	}
581cfccde04SRasmus Villemoes 	return buf;
582cfccde04SRasmus Villemoes }
583cfccde04SRasmus Villemoes 
5844b6ccca7SAl Viro static noinline_for_stack
58595508cfaSRasmus Villemoes char *string(char *buf, char *end, const char *s, struct printf_spec spec)
58695508cfaSRasmus Villemoes {
58734fc8b90SRasmus Villemoes 	int len = 0;
58834fc8b90SRasmus Villemoes 	size_t lim = spec.precision;
58995508cfaSRasmus Villemoes 
59095508cfaSRasmus Villemoes 	if ((unsigned long)s < PAGE_SIZE)
59195508cfaSRasmus Villemoes 		s = "(null)";
59295508cfaSRasmus Villemoes 
59334fc8b90SRasmus Villemoes 	while (lim--) {
59434fc8b90SRasmus Villemoes 		char c = *s++;
59534fc8b90SRasmus Villemoes 		if (!c)
59634fc8b90SRasmus Villemoes 			break;
59795508cfaSRasmus Villemoes 		if (buf < end)
59834fc8b90SRasmus Villemoes 			*buf = c;
59995508cfaSRasmus Villemoes 		++buf;
60034fc8b90SRasmus Villemoes 		++len;
60195508cfaSRasmus Villemoes 	}
60234fc8b90SRasmus Villemoes 	return widen_string(buf, len, end, spec);
60395508cfaSRasmus Villemoes }
60495508cfaSRasmus Villemoes 
60595508cfaSRasmus Villemoes static noinline_for_stack
6064b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
6074b6ccca7SAl Viro 		  const char *fmt)
6084b6ccca7SAl Viro {
6094b6ccca7SAl Viro 	const char *array[4], *s;
6104b6ccca7SAl Viro 	const struct dentry *p;
6114b6ccca7SAl Viro 	int depth;
6124b6ccca7SAl Viro 	int i, n;
6134b6ccca7SAl Viro 
6144b6ccca7SAl Viro 	switch (fmt[1]) {
6154b6ccca7SAl Viro 		case '2': case '3': case '4':
6164b6ccca7SAl Viro 			depth = fmt[1] - '0';
6174b6ccca7SAl Viro 			break;
6184b6ccca7SAl Viro 		default:
6194b6ccca7SAl Viro 			depth = 1;
6204b6ccca7SAl Viro 	}
6214b6ccca7SAl Viro 
6224b6ccca7SAl Viro 	rcu_read_lock();
6234b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
6246aa7de05SMark Rutland 		p = READ_ONCE(d->d_parent);
6256aa7de05SMark Rutland 		array[i] = READ_ONCE(d->d_name.name);
6264b6ccca7SAl Viro 		if (p == d) {
6274b6ccca7SAl Viro 			if (i)
6284b6ccca7SAl Viro 				array[i] = "";
6294b6ccca7SAl Viro 			i++;
6304b6ccca7SAl Viro 			break;
6314b6ccca7SAl Viro 		}
6324b6ccca7SAl Viro 	}
6334b6ccca7SAl Viro 	s = array[--i];
6344b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
6354b6ccca7SAl Viro 		char c = *s++;
6364b6ccca7SAl Viro 		if (!c) {
6374b6ccca7SAl Viro 			if (!i)
6384b6ccca7SAl Viro 				break;
6394b6ccca7SAl Viro 			c = '/';
6404b6ccca7SAl Viro 			s = array[--i];
6414b6ccca7SAl Viro 		}
6424b6ccca7SAl Viro 		if (buf < end)
6434b6ccca7SAl Viro 			*buf = c;
6444b6ccca7SAl Viro 	}
6454b6ccca7SAl Viro 	rcu_read_unlock();
646cfccde04SRasmus Villemoes 	return widen_string(buf, n, end, spec);
6474b6ccca7SAl Viro }
6484b6ccca7SAl Viro 
6491031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
6501031bc58SDmitry Monakhov static noinline_for_stack
6511031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev,
6521031bc58SDmitry Monakhov 		struct printf_spec spec, const char *fmt)
6531031bc58SDmitry Monakhov {
6541031bc58SDmitry Monakhov 	struct gendisk *hd = bdev->bd_disk;
6551031bc58SDmitry Monakhov 
6561031bc58SDmitry Monakhov 	buf = string(buf, end, hd->disk_name, spec);
6571031bc58SDmitry Monakhov 	if (bdev->bd_part->partno) {
6581031bc58SDmitry Monakhov 		if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
6591031bc58SDmitry Monakhov 			if (buf < end)
6601031bc58SDmitry Monakhov 				*buf = 'p';
6611031bc58SDmitry Monakhov 			buf++;
6621031bc58SDmitry Monakhov 		}
6631031bc58SDmitry Monakhov 		buf = number(buf, end, bdev->bd_part->partno, spec);
6641031bc58SDmitry Monakhov 	}
6651031bc58SDmitry Monakhov 	return buf;
6661031bc58SDmitry Monakhov }
6671031bc58SDmitry Monakhov #endif
6681031bc58SDmitry Monakhov 
669cf3b429bSJoe Perches static noinline_for_stack
670cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
671b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
6720fe1ef24SLinus Torvalds {
673b0d33c2bSJoe Perches 	unsigned long value;
6740fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
6750fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
676b0d33c2bSJoe Perches #endif
677b0d33c2bSJoe Perches 
678b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
679b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
680b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
681b0d33c2bSJoe Perches 
682b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
683b0d33c2bSJoe Perches 	if (*fmt == 'B')
6840f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
685b0d33c2bSJoe Perches 	else if (*fmt != 'f' && *fmt != 's')
6860fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
6870c8b946eSFrederic Weisbecker 	else
6884796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
6897b9186f5SAndré Goddard Rosa 
690fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
6910fe1ef24SLinus Torvalds #else
6923cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, value, sizeof(void *));
6930fe1ef24SLinus Torvalds #endif
6940fe1ef24SLinus Torvalds }
6950fe1ef24SLinus Torvalds 
696cf3b429bSJoe Perches static noinline_for_stack
697cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
698fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
699332d2e78SLinus Torvalds {
700332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
70128405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
702332d2e78SLinus Torvalds #endif
703332d2e78SLinus Torvalds 
704332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
70528405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
706332d2e78SLinus Torvalds #endif
7074da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
708fef20d9cSFrederic Weisbecker 		.base = 16,
7094da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
710fef20d9cSFrederic Weisbecker 		.precision = -1,
711fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
712fef20d9cSFrederic Weisbecker 	};
7134da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
7144da0b66cSBjorn Helgaas 		.base = 16,
7154da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
7164da0b66cSBjorn Helgaas 		.precision = -1,
7174da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
7184da0b66cSBjorn Helgaas 	};
7190f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
7200f4050c7SBjorn Helgaas 		.base = 16,
7210f4050c7SBjorn Helgaas 		.field_width = 2,
7220f4050c7SBjorn Helgaas 		.precision = -1,
7230f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
7240f4050c7SBjorn Helgaas 	};
7254da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
726c91d3376SBjorn Helgaas 		.base = 10,
727c91d3376SBjorn Helgaas 		.precision = -1,
728c91d3376SBjorn Helgaas 		.flags = 0,
729c91d3376SBjorn Helgaas 	};
7304da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
731fd95541eSBjorn Helgaas 		.field_width = -1,
732fd95541eSBjorn Helgaas 		.precision = 10,
733fd95541eSBjorn Helgaas 		.flags = LEFT,
734fd95541eSBjorn Helgaas 	};
7354da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
736fd95541eSBjorn Helgaas 		.base = 16,
737fd95541eSBjorn Helgaas 		.precision = -1,
738fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
739fd95541eSBjorn Helgaas 	};
740c7dabef8SBjorn Helgaas 
741c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
742c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
743c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
744c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
7459d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
746c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
747c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
748c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
749c7dabef8SBjorn Helgaas 
750332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
751c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
7524da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
753332d2e78SLinus Torvalds 
754332d2e78SLinus Torvalds 	*p++ = '[';
7554da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
756fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
7574da0b66cSBjorn Helgaas 		specp = &io_spec;
7584da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
759fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
7604da0b66cSBjorn Helgaas 		specp = &mem_spec;
7614da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
762fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
7634da0b66cSBjorn Helgaas 		specp = &dec_spec;
7644da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
765fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
7664da0b66cSBjorn Helgaas 		specp = &dec_spec;
7670f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
7680f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
7690f4050c7SBjorn Helgaas 		specp = &bus_spec;
7704da0b66cSBjorn Helgaas 	} else {
771c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
7724da0b66cSBjorn Helgaas 		specp = &mem_spec;
773c7dabef8SBjorn Helgaas 		decode = 0;
774fd95541eSBjorn Helgaas 	}
775d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
776d19cb803SBjorn Helgaas 		p = string(p, pend, "size ", str_spec);
777d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
778d19cb803SBjorn Helgaas 	} else {
7794da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
780c91d3376SBjorn Helgaas 		if (res->start != res->end) {
781332d2e78SLinus Torvalds 			*p++ = '-';
7824da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
783c91d3376SBjorn Helgaas 		}
784d19cb803SBjorn Helgaas 	}
785c7dabef8SBjorn Helgaas 	if (decode) {
786fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
787fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
788fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
789fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
7909d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
7919d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
792fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
793fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
794c7dabef8SBjorn Helgaas 	} else {
795fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
796c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
797fd95541eSBjorn Helgaas 	}
798332d2e78SLinus Torvalds 	*p++ = ']';
799c7dabef8SBjorn Helgaas 	*p = '\0';
800332d2e78SLinus Torvalds 
801fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
802332d2e78SLinus Torvalds }
803332d2e78SLinus Torvalds 
804cf3b429bSJoe Perches static noinline_for_stack
80531550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
80631550a16SAndy Shevchenko 		 const char *fmt)
80731550a16SAndy Shevchenko {
808360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
80931550a16SAndy Shevchenko 				   negative value, fallback to the default */
81031550a16SAndy Shevchenko 	char separator;
81131550a16SAndy Shevchenko 
81231550a16SAndy Shevchenko 	if (spec.field_width == 0)
81331550a16SAndy Shevchenko 		/* nothing to print */
81431550a16SAndy Shevchenko 		return buf;
81531550a16SAndy Shevchenko 
81631550a16SAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
81731550a16SAndy Shevchenko 		/* NULL pointer */
81831550a16SAndy Shevchenko 		return string(buf, end, NULL, spec);
81931550a16SAndy Shevchenko 
82031550a16SAndy Shevchenko 	switch (fmt[1]) {
82131550a16SAndy Shevchenko 	case 'C':
82231550a16SAndy Shevchenko 		separator = ':';
82331550a16SAndy Shevchenko 		break;
82431550a16SAndy Shevchenko 	case 'D':
82531550a16SAndy Shevchenko 		separator = '-';
82631550a16SAndy Shevchenko 		break;
82731550a16SAndy Shevchenko 	case 'N':
82831550a16SAndy Shevchenko 		separator = 0;
82931550a16SAndy Shevchenko 		break;
83031550a16SAndy Shevchenko 	default:
83131550a16SAndy Shevchenko 		separator = ' ';
83231550a16SAndy Shevchenko 		break;
83331550a16SAndy Shevchenko 	}
83431550a16SAndy Shevchenko 
83531550a16SAndy Shevchenko 	if (spec.field_width > 0)
83631550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
83731550a16SAndy Shevchenko 
8389c98f235SRasmus Villemoes 	for (i = 0; i < len; ++i) {
8399c98f235SRasmus Villemoes 		if (buf < end)
8409c98f235SRasmus Villemoes 			*buf = hex_asc_hi(addr[i]);
8419c98f235SRasmus Villemoes 		++buf;
8429c98f235SRasmus Villemoes 		if (buf < end)
8439c98f235SRasmus Villemoes 			*buf = hex_asc_lo(addr[i]);
8449c98f235SRasmus Villemoes 		++buf;
84531550a16SAndy Shevchenko 
8469c98f235SRasmus Villemoes 		if (separator && i != len - 1) {
8479c98f235SRasmus Villemoes 			if (buf < end)
8489c98f235SRasmus Villemoes 				*buf = separator;
8499c98f235SRasmus Villemoes 			++buf;
8509c98f235SRasmus Villemoes 		}
85131550a16SAndy Shevchenko 	}
85231550a16SAndy Shevchenko 
85331550a16SAndy Shevchenko 	return buf;
85431550a16SAndy Shevchenko }
85531550a16SAndy Shevchenko 
85631550a16SAndy Shevchenko static noinline_for_stack
857dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
858dbc760bcSTejun Heo 		    struct printf_spec spec, const char *fmt)
859dbc760bcSTejun Heo {
860dbc760bcSTejun Heo 	const int CHUNKSZ = 32;
861dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
862dbc760bcSTejun Heo 	int i, chunksz;
863dbc760bcSTejun Heo 	bool first = true;
864dbc760bcSTejun Heo 
865dbc760bcSTejun Heo 	/* reused to print numbers */
866dbc760bcSTejun Heo 	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
867dbc760bcSTejun Heo 
868dbc760bcSTejun Heo 	chunksz = nr_bits & (CHUNKSZ - 1);
869dbc760bcSTejun Heo 	if (chunksz == 0)
870dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
871dbc760bcSTejun Heo 
872dbc760bcSTejun Heo 	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
873dbc760bcSTejun Heo 	for (; i >= 0; i -= CHUNKSZ) {
874dbc760bcSTejun Heo 		u32 chunkmask, val;
875dbc760bcSTejun Heo 		int word, bit;
876dbc760bcSTejun Heo 
877dbc760bcSTejun Heo 		chunkmask = ((1ULL << chunksz) - 1);
878dbc760bcSTejun Heo 		word = i / BITS_PER_LONG;
879dbc760bcSTejun Heo 		bit = i % BITS_PER_LONG;
880dbc760bcSTejun Heo 		val = (bitmap[word] >> bit) & chunkmask;
881dbc760bcSTejun Heo 
882dbc760bcSTejun Heo 		if (!first) {
883dbc760bcSTejun Heo 			if (buf < end)
884dbc760bcSTejun Heo 				*buf = ',';
885dbc760bcSTejun Heo 			buf++;
886dbc760bcSTejun Heo 		}
887dbc760bcSTejun Heo 		first = false;
888dbc760bcSTejun Heo 
889dbc760bcSTejun Heo 		spec.field_width = DIV_ROUND_UP(chunksz, 4);
890dbc760bcSTejun Heo 		buf = number(buf, end, val, spec);
891dbc760bcSTejun Heo 
892dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
893dbc760bcSTejun Heo 	}
894dbc760bcSTejun Heo 	return buf;
895dbc760bcSTejun Heo }
896dbc760bcSTejun Heo 
897dbc760bcSTejun Heo static noinline_for_stack
898dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
899dbc760bcSTejun Heo 			 struct printf_spec spec, const char *fmt)
900dbc760bcSTejun Heo {
901dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
902dbc760bcSTejun Heo 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
903dbc760bcSTejun Heo 	int cur, rbot, rtop;
904dbc760bcSTejun Heo 	bool first = true;
905dbc760bcSTejun Heo 
906dbc760bcSTejun Heo 	/* reused to print numbers */
907dbc760bcSTejun Heo 	spec = (struct printf_spec){ .base = 10 };
908dbc760bcSTejun Heo 
909dbc760bcSTejun Heo 	rbot = cur = find_first_bit(bitmap, nr_bits);
910dbc760bcSTejun Heo 	while (cur < nr_bits) {
911dbc760bcSTejun Heo 		rtop = cur;
912dbc760bcSTejun Heo 		cur = find_next_bit(bitmap, nr_bits, cur + 1);
913dbc760bcSTejun Heo 		if (cur < nr_bits && cur <= rtop + 1)
914dbc760bcSTejun Heo 			continue;
915dbc760bcSTejun Heo 
916dbc760bcSTejun Heo 		if (!first) {
917dbc760bcSTejun Heo 			if (buf < end)
918dbc760bcSTejun Heo 				*buf = ',';
919dbc760bcSTejun Heo 			buf++;
920dbc760bcSTejun Heo 		}
921dbc760bcSTejun Heo 		first = false;
922dbc760bcSTejun Heo 
923dbc760bcSTejun Heo 		buf = number(buf, end, rbot, spec);
924dbc760bcSTejun Heo 		if (rbot < rtop) {
925dbc760bcSTejun Heo 			if (buf < end)
926dbc760bcSTejun Heo 				*buf = '-';
927dbc760bcSTejun Heo 			buf++;
928dbc760bcSTejun Heo 
929dbc760bcSTejun Heo 			buf = number(buf, end, rtop, spec);
930dbc760bcSTejun Heo 		}
931dbc760bcSTejun Heo 
932dbc760bcSTejun Heo 		rbot = cur;
933dbc760bcSTejun Heo 	}
934dbc760bcSTejun Heo 	return buf;
935dbc760bcSTejun Heo }
936dbc760bcSTejun Heo 
937dbc760bcSTejun Heo static noinline_for_stack
938cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
9398a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
940dd45c9cfSHarvey Harrison {
9418a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
942dd45c9cfSHarvey Harrison 	char *p = mac_addr;
943dd45c9cfSHarvey Harrison 	int i;
944bc7259a2SJoe Perches 	char separator;
94576597ff9SAndrei Emeltchenko 	bool reversed = false;
946bc7259a2SJoe Perches 
94776597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
94876597ff9SAndrei Emeltchenko 	case 'F':
949bc7259a2SJoe Perches 		separator = '-';
95076597ff9SAndrei Emeltchenko 		break;
95176597ff9SAndrei Emeltchenko 
95276597ff9SAndrei Emeltchenko 	case 'R':
95376597ff9SAndrei Emeltchenko 		reversed = true;
95476597ff9SAndrei Emeltchenko 		/* fall through */
95576597ff9SAndrei Emeltchenko 
95676597ff9SAndrei Emeltchenko 	default:
957bc7259a2SJoe Perches 		separator = ':';
95876597ff9SAndrei Emeltchenko 		break;
959bc7259a2SJoe Perches 	}
960dd45c9cfSHarvey Harrison 
961dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
96276597ff9SAndrei Emeltchenko 		if (reversed)
96376597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
96476597ff9SAndrei Emeltchenko 		else
96555036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
96676597ff9SAndrei Emeltchenko 
9678a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
968bc7259a2SJoe Perches 			*p++ = separator;
969dd45c9cfSHarvey Harrison 	}
970dd45c9cfSHarvey Harrison 	*p = '\0';
971dd45c9cfSHarvey Harrison 
972fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
973dd45c9cfSHarvey Harrison }
974dd45c9cfSHarvey Harrison 
975cf3b429bSJoe Perches static noinline_for_stack
976cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
977689afa7dSHarvey Harrison {
978689afa7dSHarvey Harrison 	int i;
9790159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
9800159f24eSJoe Perches 	int index;
9810159f24eSJoe Perches 	int step;
982689afa7dSHarvey Harrison 
9830159f24eSJoe Perches 	switch (fmt[2]) {
9840159f24eSJoe Perches 	case 'h':
9850159f24eSJoe Perches #ifdef __BIG_ENDIAN
9860159f24eSJoe Perches 		index = 0;
9870159f24eSJoe Perches 		step = 1;
9880159f24eSJoe Perches #else
9890159f24eSJoe Perches 		index = 3;
9900159f24eSJoe Perches 		step = -1;
9910159f24eSJoe Perches #endif
9920159f24eSJoe Perches 		break;
9930159f24eSJoe Perches 	case 'l':
9940159f24eSJoe Perches 		index = 3;
9950159f24eSJoe Perches 		step = -1;
9960159f24eSJoe Perches 		break;
9970159f24eSJoe Perches 	case 'n':
9980159f24eSJoe Perches 	case 'b':
9990159f24eSJoe Perches 	default:
10000159f24eSJoe Perches 		index = 0;
10010159f24eSJoe Perches 		step = 1;
10020159f24eSJoe Perches 		break;
10030159f24eSJoe Perches 	}
10048a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
10057c43d9a3SRasmus Villemoes 		char temp[4] __aligned(2);	/* hold each IP quad in reverse order */
1006133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
10078a27f7c9SJoe Perches 		if (leading_zeros) {
10088a27f7c9SJoe Perches 			if (digits < 3)
10098a27f7c9SJoe Perches 				*p++ = '0';
10108a27f7c9SJoe Perches 			if (digits < 2)
10118a27f7c9SJoe Perches 				*p++ = '0';
10128a27f7c9SJoe Perches 		}
10138a27f7c9SJoe Perches 		/* reverse the digits in the quad */
10148a27f7c9SJoe Perches 		while (digits--)
10158a27f7c9SJoe Perches 			*p++ = temp[digits];
10168a27f7c9SJoe Perches 		if (i < 3)
10178a27f7c9SJoe Perches 			*p++ = '.';
10180159f24eSJoe Perches 		index += step;
10198a27f7c9SJoe Perches 	}
10208a27f7c9SJoe Perches 	*p = '\0';
10217b9186f5SAndré Goddard Rosa 
10228a27f7c9SJoe Perches 	return p;
10238a27f7c9SJoe Perches }
10248a27f7c9SJoe Perches 
1025cf3b429bSJoe Perches static noinline_for_stack
1026cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
10278a27f7c9SJoe Perches {
10287b9186f5SAndré Goddard Rosa 	int i, j, range;
10298a27f7c9SJoe Perches 	unsigned char zerolength[8];
10308a27f7c9SJoe Perches 	int longest = 1;
10318a27f7c9SJoe Perches 	int colonpos = -1;
10328a27f7c9SJoe Perches 	u16 word;
10337b9186f5SAndré Goddard Rosa 	u8 hi, lo;
10348a27f7c9SJoe Perches 	bool needcolon = false;
1035eb78cd26SJoe Perches 	bool useIPv4;
1036eb78cd26SJoe Perches 	struct in6_addr in6;
1037eb78cd26SJoe Perches 
1038eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
1039eb78cd26SJoe Perches 
1040eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
10418a27f7c9SJoe Perches 
10428a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
10438a27f7c9SJoe Perches 
10448a27f7c9SJoe Perches 	if (useIPv4)
10458a27f7c9SJoe Perches 		range = 6;
10468a27f7c9SJoe Perches 	else
10478a27f7c9SJoe Perches 		range = 8;
10488a27f7c9SJoe Perches 
10498a27f7c9SJoe Perches 	/* find position of longest 0 run */
10508a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10518a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
1052eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
10538a27f7c9SJoe Perches 				break;
10548a27f7c9SJoe Perches 			zerolength[i]++;
10558a27f7c9SJoe Perches 		}
10568a27f7c9SJoe Perches 	}
10578a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10588a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
10598a27f7c9SJoe Perches 			longest = zerolength[i];
10608a27f7c9SJoe Perches 			colonpos = i;
10618a27f7c9SJoe Perches 		}
10628a27f7c9SJoe Perches 	}
106329cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
106429cf519eSJoe Perches 		colonpos = -1;
10658a27f7c9SJoe Perches 
10668a27f7c9SJoe Perches 	/* emit address */
10678a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10688a27f7c9SJoe Perches 		if (i == colonpos) {
10698a27f7c9SJoe Perches 			if (needcolon || i == 0)
10708a27f7c9SJoe Perches 				*p++ = ':';
10718a27f7c9SJoe Perches 			*p++ = ':';
10728a27f7c9SJoe Perches 			needcolon = false;
10738a27f7c9SJoe Perches 			i += longest - 1;
10748a27f7c9SJoe Perches 			continue;
10758a27f7c9SJoe Perches 		}
10768a27f7c9SJoe Perches 		if (needcolon) {
10778a27f7c9SJoe Perches 			*p++ = ':';
10788a27f7c9SJoe Perches 			needcolon = false;
10798a27f7c9SJoe Perches 		}
10808a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
1081eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
10828a27f7c9SJoe Perches 		hi = word >> 8;
10838a27f7c9SJoe Perches 		lo = word & 0xff;
10848a27f7c9SJoe Perches 		if (hi) {
10858a27f7c9SJoe Perches 			if (hi > 0x0f)
108655036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
10878a27f7c9SJoe Perches 			else
10888a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
108955036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10908a27f7c9SJoe Perches 		}
1091b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
109255036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10938a27f7c9SJoe Perches 		else
10948a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
10958a27f7c9SJoe Perches 		needcolon = true;
10968a27f7c9SJoe Perches 	}
10978a27f7c9SJoe Perches 
10988a27f7c9SJoe Perches 	if (useIPv4) {
10998a27f7c9SJoe Perches 		if (needcolon)
11008a27f7c9SJoe Perches 			*p++ = ':';
11010159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
11028a27f7c9SJoe Perches 	}
11038a27f7c9SJoe Perches 	*p = '\0';
11047b9186f5SAndré Goddard Rosa 
11058a27f7c9SJoe Perches 	return p;
11068a27f7c9SJoe Perches }
11078a27f7c9SJoe Perches 
1108cf3b429bSJoe Perches static noinline_for_stack
1109cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
11108a27f7c9SJoe Perches {
11118a27f7c9SJoe Perches 	int i;
11127b9186f5SAndré Goddard Rosa 
1113689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
111455036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
111555036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
11168a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
1117689afa7dSHarvey Harrison 			*p++ = ':';
1118689afa7dSHarvey Harrison 	}
1119689afa7dSHarvey Harrison 	*p = '\0';
11207b9186f5SAndré Goddard Rosa 
11218a27f7c9SJoe Perches 	return p;
11228a27f7c9SJoe Perches }
11238a27f7c9SJoe Perches 
1124cf3b429bSJoe Perches static noinline_for_stack
1125cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
11268a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
11278a27f7c9SJoe Perches {
11288a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
11298a27f7c9SJoe Perches 
11308a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
1131eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
11328a27f7c9SJoe Perches 	else
1133eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
1134689afa7dSHarvey Harrison 
1135fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
1136689afa7dSHarvey Harrison }
1137689afa7dSHarvey Harrison 
1138cf3b429bSJoe Perches static noinline_for_stack
1139cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
11408a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
11414aa99606SHarvey Harrison {
11428a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
11434aa99606SHarvey Harrison 
11440159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
11454aa99606SHarvey Harrison 
1146fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
11474aa99606SHarvey Harrison }
11484aa99606SHarvey Harrison 
1149cf3b429bSJoe Perches static noinline_for_stack
115010679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
115110679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
115210679643SDaniel Borkmann {
115310679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
115410679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
115510679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
115610679643SDaniel Borkmann 		      sizeof("%1234567890")];
115710679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
115810679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
115910679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
116010679643SDaniel Borkmann 	u8 off = 0;
116110679643SDaniel Borkmann 
116210679643SDaniel Borkmann 	fmt++;
116310679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
116410679643SDaniel Borkmann 		switch (*fmt) {
116510679643SDaniel Borkmann 		case 'p':
116610679643SDaniel Borkmann 			have_p = true;
116710679643SDaniel Borkmann 			break;
116810679643SDaniel Borkmann 		case 'f':
116910679643SDaniel Borkmann 			have_f = true;
117010679643SDaniel Borkmann 			break;
117110679643SDaniel Borkmann 		case 's':
117210679643SDaniel Borkmann 			have_s = true;
117310679643SDaniel Borkmann 			break;
117410679643SDaniel Borkmann 		case 'c':
117510679643SDaniel Borkmann 			have_c = true;
117610679643SDaniel Borkmann 			break;
117710679643SDaniel Borkmann 		}
117810679643SDaniel Borkmann 	}
117910679643SDaniel Borkmann 
118010679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
118110679643SDaniel Borkmann 		*p = '[';
118210679643SDaniel Borkmann 		off = 1;
118310679643SDaniel Borkmann 	}
118410679643SDaniel Borkmann 
118510679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
118610679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
118710679643SDaniel Borkmann 	else
118810679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
118910679643SDaniel Borkmann 
119010679643SDaniel Borkmann 	if (have_p || have_s || have_f)
119110679643SDaniel Borkmann 		*p++ = ']';
119210679643SDaniel Borkmann 
119310679643SDaniel Borkmann 	if (have_p) {
119410679643SDaniel Borkmann 		*p++ = ':';
119510679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
119610679643SDaniel Borkmann 	}
119710679643SDaniel Borkmann 	if (have_f) {
119810679643SDaniel Borkmann 		*p++ = '/';
119910679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
120010679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
120110679643SDaniel Borkmann 	}
120210679643SDaniel Borkmann 	if (have_s) {
120310679643SDaniel Borkmann 		*p++ = '%';
120410679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
120510679643SDaniel Borkmann 	}
120610679643SDaniel Borkmann 	*p = '\0';
120710679643SDaniel Borkmann 
120810679643SDaniel Borkmann 	return string(buf, end, ip6_addr, spec);
120910679643SDaniel Borkmann }
121010679643SDaniel Borkmann 
121110679643SDaniel Borkmann static noinline_for_stack
121210679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
121310679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
121410679643SDaniel Borkmann {
121510679643SDaniel Borkmann 	bool have_p = false;
121610679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
121710679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
121810679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
121910679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
122010679643SDaniel Borkmann 
122110679643SDaniel Borkmann 	fmt++;
122210679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
122310679643SDaniel Borkmann 		switch (*fmt) {
122410679643SDaniel Borkmann 		case 'p':
122510679643SDaniel Borkmann 			have_p = true;
122610679643SDaniel Borkmann 			break;
122710679643SDaniel Borkmann 		case 'h':
122810679643SDaniel Borkmann 		case 'l':
122910679643SDaniel Borkmann 		case 'n':
123010679643SDaniel Borkmann 		case 'b':
123110679643SDaniel Borkmann 			fmt4[2] = *fmt;
123210679643SDaniel Borkmann 			break;
123310679643SDaniel Borkmann 		}
123410679643SDaniel Borkmann 	}
123510679643SDaniel Borkmann 
123610679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
123710679643SDaniel Borkmann 	if (have_p) {
123810679643SDaniel Borkmann 		*p++ = ':';
123910679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
124010679643SDaniel Borkmann 	}
124110679643SDaniel Borkmann 	*p = '\0';
124210679643SDaniel Borkmann 
124310679643SDaniel Borkmann 	return string(buf, end, ip4_addr, spec);
124410679643SDaniel Borkmann }
124510679643SDaniel Borkmann 
124610679643SDaniel Borkmann static noinline_for_stack
124771dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
124871dca95dSAndy Shevchenko 		     const char *fmt)
124971dca95dSAndy Shevchenko {
125071dca95dSAndy Shevchenko 	bool found = true;
125171dca95dSAndy Shevchenko 	int count = 1;
125271dca95dSAndy Shevchenko 	unsigned int flags = 0;
125371dca95dSAndy Shevchenko 	int len;
125471dca95dSAndy Shevchenko 
125571dca95dSAndy Shevchenko 	if (spec.field_width == 0)
125671dca95dSAndy Shevchenko 		return buf;				/* nothing to print */
125771dca95dSAndy Shevchenko 
125871dca95dSAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
125971dca95dSAndy Shevchenko 		return string(buf, end, NULL, spec);	/* NULL pointer */
126071dca95dSAndy Shevchenko 
126171dca95dSAndy Shevchenko 
126271dca95dSAndy Shevchenko 	do {
126371dca95dSAndy Shevchenko 		switch (fmt[count++]) {
126471dca95dSAndy Shevchenko 		case 'a':
126571dca95dSAndy Shevchenko 			flags |= ESCAPE_ANY;
126671dca95dSAndy Shevchenko 			break;
126771dca95dSAndy Shevchenko 		case 'c':
126871dca95dSAndy Shevchenko 			flags |= ESCAPE_SPECIAL;
126971dca95dSAndy Shevchenko 			break;
127071dca95dSAndy Shevchenko 		case 'h':
127171dca95dSAndy Shevchenko 			flags |= ESCAPE_HEX;
127271dca95dSAndy Shevchenko 			break;
127371dca95dSAndy Shevchenko 		case 'n':
127471dca95dSAndy Shevchenko 			flags |= ESCAPE_NULL;
127571dca95dSAndy Shevchenko 			break;
127671dca95dSAndy Shevchenko 		case 'o':
127771dca95dSAndy Shevchenko 			flags |= ESCAPE_OCTAL;
127871dca95dSAndy Shevchenko 			break;
127971dca95dSAndy Shevchenko 		case 'p':
128071dca95dSAndy Shevchenko 			flags |= ESCAPE_NP;
128171dca95dSAndy Shevchenko 			break;
128271dca95dSAndy Shevchenko 		case 's':
128371dca95dSAndy Shevchenko 			flags |= ESCAPE_SPACE;
128471dca95dSAndy Shevchenko 			break;
128571dca95dSAndy Shevchenko 		default:
128671dca95dSAndy Shevchenko 			found = false;
128771dca95dSAndy Shevchenko 			break;
128871dca95dSAndy Shevchenko 		}
128971dca95dSAndy Shevchenko 	} while (found);
129071dca95dSAndy Shevchenko 
129171dca95dSAndy Shevchenko 	if (!flags)
129271dca95dSAndy Shevchenko 		flags = ESCAPE_ANY_NP;
129371dca95dSAndy Shevchenko 
129471dca95dSAndy Shevchenko 	len = spec.field_width < 0 ? 1 : spec.field_width;
129571dca95dSAndy Shevchenko 
129641416f23SRasmus Villemoes 	/*
129741416f23SRasmus Villemoes 	 * string_escape_mem() writes as many characters as it can to
129841416f23SRasmus Villemoes 	 * the given buffer, and returns the total size of the output
129941416f23SRasmus Villemoes 	 * had the buffer been big enough.
130041416f23SRasmus Villemoes 	 */
130141416f23SRasmus Villemoes 	buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
130271dca95dSAndy Shevchenko 
130371dca95dSAndy Shevchenko 	return buf;
130471dca95dSAndy Shevchenko }
130571dca95dSAndy Shevchenko 
130671dca95dSAndy Shevchenko static noinline_for_stack
1307cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
13089ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
13099ac6e44eSJoe Perches {
13102b1b0d66SAndy Shevchenko 	char uuid[UUID_STRING_LEN + 1];
13119ac6e44eSJoe Perches 	char *p = uuid;
13129ac6e44eSJoe Perches 	int i;
1313f9727a17SChristoph Hellwig 	const u8 *index = uuid_index;
13149ac6e44eSJoe Perches 	bool uc = false;
13159ac6e44eSJoe Perches 
13169ac6e44eSJoe Perches 	switch (*(++fmt)) {
13179ac6e44eSJoe Perches 	case 'L':
13189ac6e44eSJoe Perches 		uc = true;		/* fall-through */
13199ac6e44eSJoe Perches 	case 'l':
1320f9727a17SChristoph Hellwig 		index = guid_index;
13219ac6e44eSJoe Perches 		break;
13229ac6e44eSJoe Perches 	case 'B':
13239ac6e44eSJoe Perches 		uc = true;
13249ac6e44eSJoe Perches 		break;
13259ac6e44eSJoe Perches 	}
13269ac6e44eSJoe Perches 
13279ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
1328aa4ea1c3SAndy Shevchenko 		if (uc)
1329aa4ea1c3SAndy Shevchenko 			p = hex_byte_pack_upper(p, addr[index[i]]);
1330aa4ea1c3SAndy Shevchenko 		else
133155036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[index[i]]);
13329ac6e44eSJoe Perches 		switch (i) {
13339ac6e44eSJoe Perches 		case 3:
13349ac6e44eSJoe Perches 		case 5:
13359ac6e44eSJoe Perches 		case 7:
13369ac6e44eSJoe Perches 		case 9:
13379ac6e44eSJoe Perches 			*p++ = '-';
13389ac6e44eSJoe Perches 			break;
13399ac6e44eSJoe Perches 		}
13409ac6e44eSJoe Perches 	}
13419ac6e44eSJoe Perches 
13429ac6e44eSJoe Perches 	*p = 0;
13439ac6e44eSJoe Perches 
13449ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
13459ac6e44eSJoe Perches }
13469ac6e44eSJoe Perches 
134757e73442STobin C. Harding int kptr_restrict __read_mostly;
134857e73442STobin C. Harding 
134957e73442STobin C. Harding static noinline_for_stack
135057e73442STobin C. Harding char *restricted_pointer(char *buf, char *end, const void *ptr,
135157e73442STobin C. Harding 			 struct printf_spec spec)
135257e73442STobin C. Harding {
135357e73442STobin C. Harding 	spec.base = 16;
135457e73442STobin C. Harding 	spec.flags |= SMALL;
135557e73442STobin C. Harding 	if (spec.field_width == -1) {
135657e73442STobin C. Harding 		spec.field_width = 2 * sizeof(ptr);
135757e73442STobin C. Harding 		spec.flags |= ZEROPAD;
135857e73442STobin C. Harding 	}
135957e73442STobin C. Harding 
136057e73442STobin C. Harding 	switch (kptr_restrict) {
136157e73442STobin C. Harding 	case 0:
136257e73442STobin C. Harding 		/* Always print %pK values */
136357e73442STobin C. Harding 		break;
136457e73442STobin C. Harding 	case 1: {
136557e73442STobin C. Harding 		const struct cred *cred;
136657e73442STobin C. Harding 
136757e73442STobin C. Harding 		/*
136857e73442STobin C. Harding 		 * kptr_restrict==1 cannot be used in IRQ context
136957e73442STobin C. Harding 		 * because its test for CAP_SYSLOG would be meaningless.
137057e73442STobin C. Harding 		 */
137157e73442STobin C. Harding 		if (in_irq() || in_serving_softirq() || in_nmi())
137257e73442STobin C. Harding 			return string(buf, end, "pK-error", spec);
137357e73442STobin C. Harding 
137457e73442STobin C. Harding 		/*
137557e73442STobin C. Harding 		 * Only print the real pointer value if the current
137657e73442STobin C. Harding 		 * process has CAP_SYSLOG and is running with the
137757e73442STobin C. Harding 		 * same credentials it started with. This is because
137857e73442STobin C. Harding 		 * access to files is checked at open() time, but %pK
137957e73442STobin C. Harding 		 * checks permission at read() time. We don't want to
138057e73442STobin C. Harding 		 * leak pointer values if a binary opens a file using
138157e73442STobin C. Harding 		 * %pK and then elevates privileges before reading it.
138257e73442STobin C. Harding 		 */
138357e73442STobin C. Harding 		cred = current_cred();
138457e73442STobin C. Harding 		if (!has_capability_noaudit(current, CAP_SYSLOG) ||
138557e73442STobin C. Harding 		    !uid_eq(cred->euid, cred->uid) ||
138657e73442STobin C. Harding 		    !gid_eq(cred->egid, cred->gid))
138757e73442STobin C. Harding 			ptr = NULL;
138857e73442STobin C. Harding 		break;
138957e73442STobin C. Harding 	}
139057e73442STobin C. Harding 	case 2:
139157e73442STobin C. Harding 	default:
139257e73442STobin C. Harding 		/* Always print 0's for %pK */
139357e73442STobin C. Harding 		ptr = NULL;
139457e73442STobin C. Harding 		break;
139557e73442STobin C. Harding 	}
139657e73442STobin C. Harding 
139757e73442STobin C. Harding 	return number(buf, end, (unsigned long)ptr, spec);
139857e73442STobin C. Harding }
139957e73442STobin C. Harding 
14005b17aecfSAndy Shevchenko static noinline_for_stack
14015b17aecfSAndy Shevchenko char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
1402c8f44affSMichał Mirosław {
14035b17aecfSAndy Shevchenko 	unsigned long long num;
14045b17aecfSAndy Shevchenko 	int size;
14055b17aecfSAndy Shevchenko 
14065b17aecfSAndy Shevchenko 	switch (fmt[1]) {
14075b17aecfSAndy Shevchenko 	case 'F':
14085b17aecfSAndy Shevchenko 		num = *(const netdev_features_t *)addr;
14095b17aecfSAndy Shevchenko 		size = sizeof(netdev_features_t);
14105b17aecfSAndy Shevchenko 		break;
14115b17aecfSAndy Shevchenko 	default:
14125b17aecfSAndy Shevchenko 		num = (unsigned long)addr;
14135b17aecfSAndy Shevchenko 		size = sizeof(unsigned long);
14145b17aecfSAndy Shevchenko 		break;
14155b17aecfSAndy Shevchenko 	}
1416c8f44affSMichał Mirosław 
14173cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, num, size);
1418c8f44affSMichał Mirosław }
1419c8f44affSMichał Mirosław 
1420aaf07621SJoe Perches static noinline_for_stack
14213cab1e71SAndy Shevchenko char *address_val(char *buf, char *end, const void *addr, const char *fmt)
1422aaf07621SJoe Perches {
1423aaf07621SJoe Perches 	unsigned long long num;
14243cab1e71SAndy Shevchenko 	int size;
1425aaf07621SJoe Perches 
1426aaf07621SJoe Perches 	switch (fmt[1]) {
1427aaf07621SJoe Perches 	case 'd':
1428aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
14293cab1e71SAndy Shevchenko 		size = sizeof(dma_addr_t);
1430aaf07621SJoe Perches 		break;
1431aaf07621SJoe Perches 	case 'p':
1432aaf07621SJoe Perches 	default:
1433aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
14343cab1e71SAndy Shevchenko 		size = sizeof(phys_addr_t);
1435aaf07621SJoe Perches 		break;
1436aaf07621SJoe Perches 	}
1437aaf07621SJoe Perches 
14383cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, num, size);
1439aaf07621SJoe Perches }
1440aaf07621SJoe Perches 
1441900cca29SGeert Uytterhoeven static noinline_for_stack
1442900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1443900cca29SGeert Uytterhoeven 	    const char *fmt)
1444900cca29SGeert Uytterhoeven {
1445900cca29SGeert Uytterhoeven 	if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1446900cca29SGeert Uytterhoeven 		return string(buf, end, NULL, spec);
1447900cca29SGeert Uytterhoeven 
1448900cca29SGeert Uytterhoeven 	switch (fmt[1]) {
1449900cca29SGeert Uytterhoeven 	case 'r':
1450900cca29SGeert Uytterhoeven 		return number(buf, end, clk_get_rate(clk), spec);
1451900cca29SGeert Uytterhoeven 
1452900cca29SGeert Uytterhoeven 	case 'n':
1453900cca29SGeert Uytterhoeven 	default:
1454900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK
1455900cca29SGeert Uytterhoeven 		return string(buf, end, __clk_get_name(clk), spec);
1456900cca29SGeert Uytterhoeven #else
14573cab1e71SAndy Shevchenko 		return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
1458900cca29SGeert Uytterhoeven #endif
1459900cca29SGeert Uytterhoeven 	}
1460900cca29SGeert Uytterhoeven }
1461900cca29SGeert Uytterhoeven 
1462edf14cdbSVlastimil Babka static
1463edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags,
1464edf14cdbSVlastimil Babka 					const struct trace_print_flags *names)
1465edf14cdbSVlastimil Babka {
1466edf14cdbSVlastimil Babka 	unsigned long mask;
1467edf14cdbSVlastimil Babka 	const struct printf_spec strspec = {
1468edf14cdbSVlastimil Babka 		.field_width = -1,
1469edf14cdbSVlastimil Babka 		.precision = -1,
1470edf14cdbSVlastimil Babka 	};
1471edf14cdbSVlastimil Babka 	const struct printf_spec numspec = {
1472edf14cdbSVlastimil Babka 		.flags = SPECIAL|SMALL,
1473edf14cdbSVlastimil Babka 		.field_width = -1,
1474edf14cdbSVlastimil Babka 		.precision = -1,
1475edf14cdbSVlastimil Babka 		.base = 16,
1476edf14cdbSVlastimil Babka 	};
1477edf14cdbSVlastimil Babka 
1478edf14cdbSVlastimil Babka 	for ( ; flags && names->name; names++) {
1479edf14cdbSVlastimil Babka 		mask = names->mask;
1480edf14cdbSVlastimil Babka 		if ((flags & mask) != mask)
1481edf14cdbSVlastimil Babka 			continue;
1482edf14cdbSVlastimil Babka 
1483edf14cdbSVlastimil Babka 		buf = string(buf, end, names->name, strspec);
1484edf14cdbSVlastimil Babka 
1485edf14cdbSVlastimil Babka 		flags &= ~mask;
1486edf14cdbSVlastimil Babka 		if (flags) {
1487edf14cdbSVlastimil Babka 			if (buf < end)
1488edf14cdbSVlastimil Babka 				*buf = '|';
1489edf14cdbSVlastimil Babka 			buf++;
1490edf14cdbSVlastimil Babka 		}
1491edf14cdbSVlastimil Babka 	}
1492edf14cdbSVlastimil Babka 
1493edf14cdbSVlastimil Babka 	if (flags)
1494edf14cdbSVlastimil Babka 		buf = number(buf, end, flags, numspec);
1495edf14cdbSVlastimil Babka 
1496edf14cdbSVlastimil Babka 	return buf;
1497edf14cdbSVlastimil Babka }
1498edf14cdbSVlastimil Babka 
1499edf14cdbSVlastimil Babka static noinline_for_stack
1500edf14cdbSVlastimil Babka char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1501edf14cdbSVlastimil Babka {
1502edf14cdbSVlastimil Babka 	unsigned long flags;
1503edf14cdbSVlastimil Babka 	const struct trace_print_flags *names;
1504edf14cdbSVlastimil Babka 
1505edf14cdbSVlastimil Babka 	switch (fmt[1]) {
1506edf14cdbSVlastimil Babka 	case 'p':
1507edf14cdbSVlastimil Babka 		flags = *(unsigned long *)flags_ptr;
1508edf14cdbSVlastimil Babka 		/* Remove zone id */
1509edf14cdbSVlastimil Babka 		flags &= (1UL << NR_PAGEFLAGS) - 1;
1510edf14cdbSVlastimil Babka 		names = pageflag_names;
1511edf14cdbSVlastimil Babka 		break;
1512edf14cdbSVlastimil Babka 	case 'v':
1513edf14cdbSVlastimil Babka 		flags = *(unsigned long *)flags_ptr;
1514edf14cdbSVlastimil Babka 		names = vmaflag_names;
1515edf14cdbSVlastimil Babka 		break;
1516edf14cdbSVlastimil Babka 	case 'g':
1517edf14cdbSVlastimil Babka 		flags = *(gfp_t *)flags_ptr;
1518edf14cdbSVlastimil Babka 		names = gfpflag_names;
1519edf14cdbSVlastimil Babka 		break;
1520edf14cdbSVlastimil Babka 	default:
1521edf14cdbSVlastimil Babka 		WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1522edf14cdbSVlastimil Babka 		return buf;
1523edf14cdbSVlastimil Babka 	}
1524edf14cdbSVlastimil Babka 
1525edf14cdbSVlastimil Babka 	return format_flags(buf, end, flags, names);
1526edf14cdbSVlastimil Babka }
1527edf14cdbSVlastimil Babka 
1528ce4fecf1SPantelis Antoniou static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1529ce4fecf1SPantelis Antoniou {
1530ce4fecf1SPantelis Antoniou 	for ( ; np && depth; depth--)
1531ce4fecf1SPantelis Antoniou 		np = np->parent;
1532ce4fecf1SPantelis Antoniou 
1533ce4fecf1SPantelis Antoniou 	return kbasename(np->full_name);
1534ce4fecf1SPantelis Antoniou }
1535ce4fecf1SPantelis Antoniou 
1536ce4fecf1SPantelis Antoniou static noinline_for_stack
1537ce4fecf1SPantelis Antoniou char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1538ce4fecf1SPantelis Antoniou {
1539ce4fecf1SPantelis Antoniou 	int depth;
1540ce4fecf1SPantelis Antoniou 	const struct device_node *parent = np->parent;
1541ce4fecf1SPantelis Antoniou 	static const struct printf_spec strspec = {
1542ce4fecf1SPantelis Antoniou 		.field_width = -1,
1543ce4fecf1SPantelis Antoniou 		.precision = -1,
1544ce4fecf1SPantelis Antoniou 	};
1545ce4fecf1SPantelis Antoniou 
1546ce4fecf1SPantelis Antoniou 	/* special case for root node */
1547ce4fecf1SPantelis Antoniou 	if (!parent)
1548ce4fecf1SPantelis Antoniou 		return string(buf, end, "/", strspec);
1549ce4fecf1SPantelis Antoniou 
1550ce4fecf1SPantelis Antoniou 	for (depth = 0; parent->parent; depth++)
1551ce4fecf1SPantelis Antoniou 		parent = parent->parent;
1552ce4fecf1SPantelis Antoniou 
1553ce4fecf1SPantelis Antoniou 	for ( ; depth >= 0; depth--) {
1554ce4fecf1SPantelis Antoniou 		buf = string(buf, end, "/", strspec);
1555ce4fecf1SPantelis Antoniou 		buf = string(buf, end, device_node_name_for_depth(np, depth),
1556ce4fecf1SPantelis Antoniou 			     strspec);
1557ce4fecf1SPantelis Antoniou 	}
1558ce4fecf1SPantelis Antoniou 	return buf;
1559ce4fecf1SPantelis Antoniou }
1560ce4fecf1SPantelis Antoniou 
1561ce4fecf1SPantelis Antoniou static noinline_for_stack
1562ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn,
1563ce4fecf1SPantelis Antoniou 			 struct printf_spec spec, const char *fmt)
1564ce4fecf1SPantelis Antoniou {
1565ce4fecf1SPantelis Antoniou 	char tbuf[sizeof("xxxx") + 1];
1566ce4fecf1SPantelis Antoniou 	const char *p;
1567ce4fecf1SPantelis Antoniou 	int ret;
1568ce4fecf1SPantelis Antoniou 	char *buf_start = buf;
1569ce4fecf1SPantelis Antoniou 	struct property *prop;
1570ce4fecf1SPantelis Antoniou 	bool has_mult, pass;
1571ce4fecf1SPantelis Antoniou 	static const struct printf_spec num_spec = {
1572ce4fecf1SPantelis Antoniou 		.flags = SMALL,
1573ce4fecf1SPantelis Antoniou 		.field_width = -1,
1574ce4fecf1SPantelis Antoniou 		.precision = -1,
1575ce4fecf1SPantelis Antoniou 		.base = 10,
1576ce4fecf1SPantelis Antoniou 	};
1577ce4fecf1SPantelis Antoniou 
1578ce4fecf1SPantelis Antoniou 	struct printf_spec str_spec = spec;
1579ce4fecf1SPantelis Antoniou 	str_spec.field_width = -1;
1580ce4fecf1SPantelis Antoniou 
1581ce4fecf1SPantelis Antoniou 	if (!IS_ENABLED(CONFIG_OF))
1582ce4fecf1SPantelis Antoniou 		return string(buf, end, "(!OF)", spec);
1583ce4fecf1SPantelis Antoniou 
1584ce4fecf1SPantelis Antoniou 	if ((unsigned long)dn < PAGE_SIZE)
1585ce4fecf1SPantelis Antoniou 		return string(buf, end, "(null)", spec);
1586ce4fecf1SPantelis Antoniou 
1587ce4fecf1SPantelis Antoniou 	/* simple case without anything any more format specifiers */
1588ce4fecf1SPantelis Antoniou 	fmt++;
1589ce4fecf1SPantelis Antoniou 	if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1590ce4fecf1SPantelis Antoniou 		fmt = "f";
1591ce4fecf1SPantelis Antoniou 
1592ce4fecf1SPantelis Antoniou 	for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
1593ce4fecf1SPantelis Antoniou 		if (pass) {
1594ce4fecf1SPantelis Antoniou 			if (buf < end)
1595ce4fecf1SPantelis Antoniou 				*buf = ':';
1596ce4fecf1SPantelis Antoniou 			buf++;
1597ce4fecf1SPantelis Antoniou 		}
1598ce4fecf1SPantelis Antoniou 
1599ce4fecf1SPantelis Antoniou 		switch (*fmt) {
1600ce4fecf1SPantelis Antoniou 		case 'f':	/* full_name */
1601ce4fecf1SPantelis Antoniou 			buf = device_node_gen_full_name(dn, buf, end);
1602ce4fecf1SPantelis Antoniou 			break;
1603ce4fecf1SPantelis Antoniou 		case 'n':	/* name */
1604ce4fecf1SPantelis Antoniou 			buf = string(buf, end, dn->name, str_spec);
1605ce4fecf1SPantelis Antoniou 			break;
1606ce4fecf1SPantelis Antoniou 		case 'p':	/* phandle */
1607ce4fecf1SPantelis Antoniou 			buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1608ce4fecf1SPantelis Antoniou 			break;
1609ce4fecf1SPantelis Antoniou 		case 'P':	/* path-spec */
1610ce4fecf1SPantelis Antoniou 			p = kbasename(of_node_full_name(dn));
1611ce4fecf1SPantelis Antoniou 			if (!p[1])
1612ce4fecf1SPantelis Antoniou 				p = "/";
1613ce4fecf1SPantelis Antoniou 			buf = string(buf, end, p, str_spec);
1614ce4fecf1SPantelis Antoniou 			break;
1615ce4fecf1SPantelis Antoniou 		case 'F':	/* flags */
1616ce4fecf1SPantelis Antoniou 			tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1617ce4fecf1SPantelis Antoniou 			tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1618ce4fecf1SPantelis Antoniou 			tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1619ce4fecf1SPantelis Antoniou 			tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1620ce4fecf1SPantelis Antoniou 			tbuf[4] = 0;
1621ce4fecf1SPantelis Antoniou 			buf = string(buf, end, tbuf, str_spec);
1622ce4fecf1SPantelis Antoniou 			break;
1623ce4fecf1SPantelis Antoniou 		case 'c':	/* major compatible string */
1624ce4fecf1SPantelis Antoniou 			ret = of_property_read_string(dn, "compatible", &p);
1625ce4fecf1SPantelis Antoniou 			if (!ret)
1626ce4fecf1SPantelis Antoniou 				buf = string(buf, end, p, str_spec);
1627ce4fecf1SPantelis Antoniou 			break;
1628ce4fecf1SPantelis Antoniou 		case 'C':	/* full compatible string */
1629ce4fecf1SPantelis Antoniou 			has_mult = false;
1630ce4fecf1SPantelis Antoniou 			of_property_for_each_string(dn, "compatible", prop, p) {
1631ce4fecf1SPantelis Antoniou 				if (has_mult)
1632ce4fecf1SPantelis Antoniou 					buf = string(buf, end, ",", str_spec);
1633ce4fecf1SPantelis Antoniou 				buf = string(buf, end, "\"", str_spec);
1634ce4fecf1SPantelis Antoniou 				buf = string(buf, end, p, str_spec);
1635ce4fecf1SPantelis Antoniou 				buf = string(buf, end, "\"", str_spec);
1636ce4fecf1SPantelis Antoniou 
1637ce4fecf1SPantelis Antoniou 				has_mult = true;
1638ce4fecf1SPantelis Antoniou 			}
1639ce4fecf1SPantelis Antoniou 			break;
1640ce4fecf1SPantelis Antoniou 		default:
1641ce4fecf1SPantelis Antoniou 			break;
1642ce4fecf1SPantelis Antoniou 		}
1643ce4fecf1SPantelis Antoniou 	}
1644ce4fecf1SPantelis Antoniou 
1645ce4fecf1SPantelis Antoniou 	return widen_string(buf, buf - buf_start, end, spec);
1646ce4fecf1SPantelis Antoniou }
1647ce4fecf1SPantelis Antoniou 
16487b1924a1STobin C. Harding static noinline_for_stack
16497b1924a1STobin C. Harding char *pointer_string(char *buf, char *end, const void *ptr,
16507b1924a1STobin C. Harding 		     struct printf_spec spec)
16517b1924a1STobin C. Harding {
16527b1924a1STobin C. Harding 	spec.base = 16;
16537b1924a1STobin C. Harding 	spec.flags |= SMALL;
16547b1924a1STobin C. Harding 	if (spec.field_width == -1) {
16557b1924a1STobin C. Harding 		spec.field_width = 2 * sizeof(ptr);
16567b1924a1STobin C. Harding 		spec.flags |= ZEROPAD;
16577b1924a1STobin C. Harding 	}
16587b1924a1STobin C. Harding 
16597b1924a1STobin C. Harding 	return number(buf, end, (unsigned long int)ptr, spec);
16607b1924a1STobin C. Harding }
16617b1924a1STobin C. Harding 
1662ad67b74dSTobin C. Harding static bool have_filled_random_ptr_key __read_mostly;
1663ad67b74dSTobin C. Harding static siphash_key_t ptr_key __read_mostly;
1664ad67b74dSTobin C. Harding 
1665ad67b74dSTobin C. Harding static void fill_random_ptr_key(struct random_ready_callback *unused)
1666ad67b74dSTobin C. Harding {
1667ad67b74dSTobin C. Harding 	get_random_bytes(&ptr_key, sizeof(ptr_key));
1668ad67b74dSTobin C. Harding 	/*
1669ad67b74dSTobin C. Harding 	 * have_filled_random_ptr_key==true is dependent on get_random_bytes().
1670ad67b74dSTobin C. Harding 	 * ptr_to_id() needs to see have_filled_random_ptr_key==true
1671ad67b74dSTobin C. Harding 	 * after get_random_bytes() returns.
1672ad67b74dSTobin C. Harding 	 */
1673ad67b74dSTobin C. Harding 	smp_mb();
1674ad67b74dSTobin C. Harding 	WRITE_ONCE(have_filled_random_ptr_key, true);
1675ad67b74dSTobin C. Harding }
1676ad67b74dSTobin C. Harding 
1677ad67b74dSTobin C. Harding static struct random_ready_callback random_ready = {
1678ad67b74dSTobin C. Harding 	.func = fill_random_ptr_key
1679ad67b74dSTobin C. Harding };
1680ad67b74dSTobin C. Harding 
1681ad67b74dSTobin C. Harding static int __init initialize_ptr_random(void)
1682ad67b74dSTobin C. Harding {
1683ad67b74dSTobin C. Harding 	int ret = add_random_ready_callback(&random_ready);
1684ad67b74dSTobin C. Harding 
1685ad67b74dSTobin C. Harding 	if (!ret) {
1686ad67b74dSTobin C. Harding 		return 0;
1687ad67b74dSTobin C. Harding 	} else if (ret == -EALREADY) {
1688ad67b74dSTobin C. Harding 		fill_random_ptr_key(&random_ready);
1689ad67b74dSTobin C. Harding 		return 0;
1690ad67b74dSTobin C. Harding 	}
1691ad67b74dSTobin C. Harding 
1692ad67b74dSTobin C. Harding 	return ret;
1693ad67b74dSTobin C. Harding }
1694ad67b74dSTobin C. Harding early_initcall(initialize_ptr_random);
1695ad67b74dSTobin C. Harding 
1696ad67b74dSTobin C. Harding /* Maps a pointer to a 32 bit unique identifier. */
1697ad67b74dSTobin C. Harding static char *ptr_to_id(char *buf, char *end, void *ptr, struct printf_spec spec)
1698ad67b74dSTobin C. Harding {
1699ad67b74dSTobin C. Harding 	unsigned long hashval;
1700ad67b74dSTobin C. Harding 	const int default_width = 2 * sizeof(ptr);
1701ad67b74dSTobin C. Harding 
1702ad67b74dSTobin C. Harding 	if (unlikely(!have_filled_random_ptr_key)) {
1703ad67b74dSTobin C. Harding 		spec.field_width = default_width;
1704ad67b74dSTobin C. Harding 		/* string length must be less than default_width */
1705ad67b74dSTobin C. Harding 		return string(buf, end, "(ptrval)", spec);
1706ad67b74dSTobin C. Harding 	}
1707ad67b74dSTobin C. Harding 
1708ad67b74dSTobin C. Harding #ifdef CONFIG_64BIT
1709ad67b74dSTobin C. Harding 	hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
1710ad67b74dSTobin C. Harding 	/*
1711ad67b74dSTobin C. Harding 	 * Mask off the first 32 bits, this makes explicit that we have
1712ad67b74dSTobin C. Harding 	 * modified the address (and 32 bits is plenty for a unique ID).
1713ad67b74dSTobin C. Harding 	 */
1714ad67b74dSTobin C. Harding 	hashval = hashval & 0xffffffff;
1715ad67b74dSTobin C. Harding #else
1716ad67b74dSTobin C. Harding 	hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
1717ad67b74dSTobin C. Harding #endif
1718ad67b74dSTobin C. Harding 
1719ad67b74dSTobin C. Harding 	spec.flags |= SMALL;
1720ad67b74dSTobin C. Harding 	if (spec.field_width == -1) {
1721ad67b74dSTobin C. Harding 		spec.field_width = default_width;
1722ad67b74dSTobin C. Harding 		spec.flags |= ZEROPAD;
1723ad67b74dSTobin C. Harding 	}
1724ad67b74dSTobin C. Harding 	spec.base = 16;
1725ad67b74dSTobin C. Harding 
1726ad67b74dSTobin C. Harding 	return number(buf, end, hashval, spec);
1727ad67b74dSTobin C. Harding }
1728ad67b74dSTobin C. Harding 
17294d8a743cSLinus Torvalds /*
17304d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
17314d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
17324d8a743cSLinus Torvalds  * specifiers.
17334d8a743cSLinus Torvalds  *
17340b523769SJoe Perches  * Please update scripts/checkpatch.pl when adding/removing conversion
17350b523769SJoe Perches  * characters.  (Search for "check for vsprintf extension").
17360b523769SJoe Perches  *
1737332d2e78SLinus Torvalds  * Right now we handle:
17380fe1ef24SLinus Torvalds  *
17390c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
17400c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
17410efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
17420efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
1743b0d33c2bSJoe Perches  * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
17440f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
1745c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1746c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1747dbc760bcSTejun Heo  * - 'b[l]' For a bitmap, the number of bits is determined by the field
1748dbc760bcSTejun Heo  *       width which must be explicitly specified either as part of the
1749dbc760bcSTejun Heo  *       format string '%32b[l]' or through '%*b[l]', [l] selects
1750dbc760bcSTejun Heo  *       range-list format instead of hex format
1751dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
1752dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
17538a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1754bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1755c8e00060SJoe Perches  *       with a dash-separated hex notation
17567c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
17578a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
17588a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
17598a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
176010679643SDaniel Borkmann  *       [S][pfs]
176110679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
176210679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
17638a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
17648a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
17658a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
176610679643SDaniel Borkmann  *       [S][pfs]
176710679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
176810679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
176910679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
177010679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
177129cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
177271dca95dSAndy Shevchenko  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
177371dca95dSAndy Shevchenko  *                of the following flags (see string_escape_mem() for the
177471dca95dSAndy Shevchenko  *                details):
177571dca95dSAndy Shevchenko  *                  a - ESCAPE_ANY
177671dca95dSAndy Shevchenko  *                  c - ESCAPE_SPECIAL
177771dca95dSAndy Shevchenko  *                  h - ESCAPE_HEX
177871dca95dSAndy Shevchenko  *                  n - ESCAPE_NULL
177971dca95dSAndy Shevchenko  *                  o - ESCAPE_OCTAL
178071dca95dSAndy Shevchenko  *                  p - ESCAPE_NP
178171dca95dSAndy Shevchenko  *                  s - ESCAPE_SPACE
178271dca95dSAndy Shevchenko  *                By default ESCAPE_ANY_NP is used.
17839ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
17849ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
17859ac6e44eSJoe Perches  *       Options for %pU are:
17869ac6e44eSJoe Perches  *         b big endian lower case hex (default)
17879ac6e44eSJoe Perches  *         B big endian UPPER case hex
17889ac6e44eSJoe Perches  *         l little endian lower case hex
17899ac6e44eSJoe Perches  *         L little endian UPPER case hex
17909ac6e44eSJoe Perches  *           big endian output byte order is:
17919ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
17929ac6e44eSJoe Perches  *           little endian output byte order is:
17939ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
17947db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
17957db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
17967db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
17977db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
17987db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
1799455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
1800c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
180131550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
180231550a16SAndy Shevchenko  *            a certain separator (' ' by default):
180331550a16SAndy Shevchenko  *              C colon
180431550a16SAndy Shevchenko  *              D dash
180531550a16SAndy Shevchenko  *              N no separator
180631550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
180731550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
1808aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1809aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
1810c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
1811c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
18121031bc58SDmitry Monakhov  * - 'g' For block_device name (gendisk + partition number)
1813900cca29SGeert Uytterhoeven  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1814900cca29SGeert Uytterhoeven  *       (legacy clock framework) of the clock
1815900cca29SGeert Uytterhoeven  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1816900cca29SGeert Uytterhoeven  *        (legacy clock framework) of the clock
1817900cca29SGeert Uytterhoeven  * - 'Cr' For a clock, it prints the current rate of the clock
1818edf14cdbSVlastimil Babka  * - 'G' For flags to be printed as a collection of symbolic strings that would
1819edf14cdbSVlastimil Babka  *       construct the specific value. Supported flags given by option:
1820edf14cdbSVlastimil Babka  *       p page flags (see struct page) given as pointer to unsigned long
1821edf14cdbSVlastimil Babka  *       g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1822edf14cdbSVlastimil Babka  *       v vma flags (VM_*) given as pointer to unsigned long
1823ce4fecf1SPantelis Antoniou  * - 'O' For a kobject based struct. Must be one of the following:
1824ce4fecf1SPantelis Antoniou  *       - 'OF[fnpPcCF]'  For a device tree object
1825ce4fecf1SPantelis Antoniou  *                        Without any optional arguments prints the full_name
1826ce4fecf1SPantelis Antoniou  *                        f device node full_name
1827ce4fecf1SPantelis Antoniou  *                        n device node name
1828ce4fecf1SPantelis Antoniou  *                        p device node phandle
1829ce4fecf1SPantelis Antoniou  *                        P device node path spec (name + @unit)
1830ce4fecf1SPantelis Antoniou  *                        F device node flags
1831ce4fecf1SPantelis Antoniou  *                        c major compatible string
1832ce4fecf1SPantelis Antoniou  *                        C full compatible string
18335e4ee7b1SMartin Kletzander  *
18347b1924a1STobin C. Harding  * - 'x' For printing the address. Equivalent to "%lx".
18357b1924a1STobin C. Harding  *
1836b3ed2321STobin C. Harding  * ** When making changes please also update:
1837b3ed2321STobin C. Harding  *	Documentation/core-api/printk-formats.rst
18389ac6e44eSJoe Perches  *
1839332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1840332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
1841332d2e78SLinus Torvalds  * pointer to the real address.
1842ad67b74dSTobin C. Harding  *
1843ad67b74dSTobin C. Harding  * Note: The default behaviour (unadorned %p) is to hash the address,
1844ad67b74dSTobin C. Harding  * rendering it useful as a unique identifier.
18454d8a743cSLinus Torvalds  */
1846cf3b429bSJoe Perches static noinline_for_stack
1847cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1848fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
184978a8bf69SLinus Torvalds {
185080c9eb46SRasmus Villemoes 	const int default_width = 2 * sizeof(void *);
1851725fe002SGrant Likely 
1852*3a129cc2SAdam Borowski 	if (!ptr && *fmt != 'K' && *fmt != 'x') {
18535e057981SJoe Perches 		/*
18545e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
18555e057981SJoe Perches 		 * tabular output look nice.
18565e057981SJoe Perches 		 */
18575e057981SJoe Perches 		if (spec.field_width == -1)
1858725fe002SGrant Likely 			spec.field_width = default_width;
1859fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
18605e057981SJoe Perches 	}
1861d97106abSLinus Torvalds 
18620fe1ef24SLinus Torvalds 	switch (*fmt) {
18630fe1ef24SLinus Torvalds 	case 'F':
18640c8b946eSFrederic Weisbecker 	case 'f':
18650fe1ef24SLinus Torvalds 	case 'S':
18669ac6e44eSJoe Perches 	case 's':
186704b8eb7aSSergey Senozhatsky 		ptr = dereference_symbol_descriptor(ptr);
186804b8eb7aSSergey Senozhatsky 		/* Fallthrough */
18690f77a8d3SNamhyung Kim 	case 'B':
1870b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
1871332d2e78SLinus Torvalds 	case 'R':
1872c7dabef8SBjorn Helgaas 	case 'r':
1873fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
187431550a16SAndy Shevchenko 	case 'h':
187531550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
1876dbc760bcSTejun Heo 	case 'b':
1877dbc760bcSTejun Heo 		switch (fmt[1]) {
1878dbc760bcSTejun Heo 		case 'l':
1879dbc760bcSTejun Heo 			return bitmap_list_string(buf, end, ptr, spec, fmt);
1880dbc760bcSTejun Heo 		default:
1881dbc760bcSTejun Heo 			return bitmap_string(buf, end, ptr, spec, fmt);
1882dbc760bcSTejun Heo 		}
18838a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
18848a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
188576597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
188676597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
18878a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
18888a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
18898a27f7c9SJoe Perches 					 * 4:	1.2.3.4
18908a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
18918a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
18928a27f7c9SJoe Perches 					 */
18938a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
18948a27f7c9SJoe Perches 					 * 4:	001.002.003.004
18958a27f7c9SJoe Perches 					 * 6:   000102...0f
18968a27f7c9SJoe Perches 					 */
18978a27f7c9SJoe Perches 		switch (fmt[1]) {
18988a27f7c9SJoe Perches 		case '6':
18998a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
19008a27f7c9SJoe Perches 		case '4':
19018a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
190210679643SDaniel Borkmann 		case 'S': {
190310679643SDaniel Borkmann 			const union {
190410679643SDaniel Borkmann 				struct sockaddr		raw;
190510679643SDaniel Borkmann 				struct sockaddr_in	v4;
190610679643SDaniel Borkmann 				struct sockaddr_in6	v6;
190710679643SDaniel Borkmann 			} *sa = ptr;
190810679643SDaniel Borkmann 
190910679643SDaniel Borkmann 			switch (sa->raw.sa_family) {
191010679643SDaniel Borkmann 			case AF_INET:
191110679643SDaniel Borkmann 				return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
191210679643SDaniel Borkmann 			case AF_INET6:
191310679643SDaniel Borkmann 				return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
191410679643SDaniel Borkmann 			default:
191510679643SDaniel Borkmann 				return string(buf, end, "(invalid address)", spec);
191610679643SDaniel Borkmann 			}}
19178a27f7c9SJoe Perches 		}
19184aa99606SHarvey Harrison 		break;
191971dca95dSAndy Shevchenko 	case 'E':
192071dca95dSAndy Shevchenko 		return escaped_string(buf, end, ptr, spec, fmt);
19219ac6e44eSJoe Perches 	case 'U':
19229ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
19237db6f5fbSJoe Perches 	case 'V':
19245756b76eSJan Beulich 		{
19255756b76eSJan Beulich 			va_list va;
19265756b76eSJan Beulich 
19275756b76eSJan Beulich 			va_copy(va, *((struct va_format *)ptr)->va);
19285756b76eSJan Beulich 			buf += vsnprintf(buf, end > buf ? end - buf : 0,
19295756b76eSJan Beulich 					 ((struct va_format *)ptr)->fmt, va);
19305756b76eSJan Beulich 			va_end(va);
19315756b76eSJan Beulich 			return buf;
19325756b76eSJan Beulich 		}
1933455cd5abSDan Rosenberg 	case 'K':
1934ef0010a3SLinus Torvalds 		if (!kptr_restrict)
1935ef0010a3SLinus Torvalds 			break;
193657e73442STobin C. Harding 		return restricted_pointer(buf, end, ptr, spec);
1937c8f44affSMichał Mirosław 	case 'N':
19385b17aecfSAndy Shevchenko 		return netdev_bits(buf, end, ptr, fmt);
19397d799210SStepan Moskovchenko 	case 'a':
19403cab1e71SAndy Shevchenko 		return address_val(buf, end, ptr, fmt);
19414b6ccca7SAl Viro 	case 'd':
19424b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
1943900cca29SGeert Uytterhoeven 	case 'C':
1944900cca29SGeert Uytterhoeven 		return clock(buf, end, ptr, spec, fmt);
19454b6ccca7SAl Viro 	case 'D':
19464b6ccca7SAl Viro 		return dentry_name(buf, end,
19474b6ccca7SAl Viro 				   ((const struct file *)ptr)->f_path.dentry,
19484b6ccca7SAl Viro 				   spec, fmt);
19491031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
19501031bc58SDmitry Monakhov 	case 'g':
19511031bc58SDmitry Monakhov 		return bdev_name(buf, end, ptr, spec, fmt);
19521031bc58SDmitry Monakhov #endif
19531031bc58SDmitry Monakhov 
1954edf14cdbSVlastimil Babka 	case 'G':
1955edf14cdbSVlastimil Babka 		return flags_string(buf, end, ptr, fmt);
1956ce4fecf1SPantelis Antoniou 	case 'O':
1957ce4fecf1SPantelis Antoniou 		switch (fmt[1]) {
1958ce4fecf1SPantelis Antoniou 		case 'F':
1959ce4fecf1SPantelis Antoniou 			return device_node_string(buf, end, ptr, spec, fmt + 1);
1960ce4fecf1SPantelis Antoniou 		}
19617b1924a1STobin C. Harding 	case 'x':
19627b1924a1STobin C. Harding 		return pointer_string(buf, end, ptr, spec);
19630fe1ef24SLinus Torvalds 	}
1964fef20d9cSFrederic Weisbecker 
1965ad67b74dSTobin C. Harding 	/* default is to _not_ leak addresses, hash before printing */
1966ad67b74dSTobin C. Harding 	return ptr_to_id(buf, end, ptr, spec);
1967fef20d9cSFrederic Weisbecker }
1968fef20d9cSFrederic Weisbecker 
1969fef20d9cSFrederic Weisbecker /*
1970fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
1971fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
1972fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
1973fef20d9cSFrederic Weisbecker  * to go on the next call).
1974fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
1975fef20d9cSFrederic Weisbecker  *
1976fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
1977fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
1978fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
19795b5e0928SAlexey Dobriyan  * 'Z' changed to 'z' --adobriyan 2017-01-25
1980fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
1981fef20d9cSFrederic Weisbecker  *
1982fef20d9cSFrederic Weisbecker  * @fmt: the format string
1983fef20d9cSFrederic Weisbecker  * @type of the token returned
1984fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
1985fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
1986fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
1987fef20d9cSFrederic Weisbecker  * @precision: precision of a number
1988fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
1989fef20d9cSFrederic Weisbecker  */
1990cf3b429bSJoe Perches static noinline_for_stack
1991cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
1992fef20d9cSFrederic Weisbecker {
1993fef20d9cSFrederic Weisbecker 	const char *start = fmt;
1994d0484193SRasmus Villemoes 	char qualifier;
1995fef20d9cSFrederic Weisbecker 
1996fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
1997ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
1998fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
1999fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
2000fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
2001fef20d9cSFrederic Weisbecker 		}
2002fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
2003fef20d9cSFrederic Weisbecker 		goto precision;
2004fef20d9cSFrederic Weisbecker 	}
2005fef20d9cSFrederic Weisbecker 
2006fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
2007fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
2008fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
2009fef20d9cSFrederic Weisbecker 			spec->precision = 0;
2010fef20d9cSFrederic Weisbecker 
2011fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
2012fef20d9cSFrederic Weisbecker 		goto qualifier;
2013fef20d9cSFrederic Weisbecker 	}
2014fef20d9cSFrederic Weisbecker 
2015fef20d9cSFrederic Weisbecker 	/* By default */
2016fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
2017fef20d9cSFrederic Weisbecker 
2018fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
2019fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
2020fef20d9cSFrederic Weisbecker 			break;
2021fef20d9cSFrederic Weisbecker 	}
2022fef20d9cSFrederic Weisbecker 
2023fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
2024fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
2025fef20d9cSFrederic Weisbecker 		return fmt - start;
2026fef20d9cSFrederic Weisbecker 
2027fef20d9cSFrederic Weisbecker 	/* Process flags */
2028fef20d9cSFrederic Weisbecker 	spec->flags = 0;
2029fef20d9cSFrederic Weisbecker 
2030fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
2031fef20d9cSFrederic Weisbecker 		bool found = true;
2032fef20d9cSFrederic Weisbecker 
2033fef20d9cSFrederic Weisbecker 		++fmt;
2034fef20d9cSFrederic Weisbecker 
2035fef20d9cSFrederic Weisbecker 		switch (*fmt) {
2036fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
2037fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
2038fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
2039fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
2040fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
2041fef20d9cSFrederic Weisbecker 		default:  found = false;
2042fef20d9cSFrederic Weisbecker 		}
2043fef20d9cSFrederic Weisbecker 
2044fef20d9cSFrederic Weisbecker 		if (!found)
2045fef20d9cSFrederic Weisbecker 			break;
2046fef20d9cSFrederic Weisbecker 	}
2047fef20d9cSFrederic Weisbecker 
2048fef20d9cSFrederic Weisbecker 	/* get field width */
2049fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
2050fef20d9cSFrederic Weisbecker 
2051fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
2052fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
2053fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
2054fef20d9cSFrederic Weisbecker 		/* it's the next argument */
2055ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
2056fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2057fef20d9cSFrederic Weisbecker 	}
2058fef20d9cSFrederic Weisbecker 
2059fef20d9cSFrederic Weisbecker precision:
2060fef20d9cSFrederic Weisbecker 	/* get the precision */
2061fef20d9cSFrederic Weisbecker 	spec->precision = -1;
2062fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
2063fef20d9cSFrederic Weisbecker 		++fmt;
2064fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
2065fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
2066fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
2067fef20d9cSFrederic Weisbecker 				spec->precision = 0;
2068fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
2069fef20d9cSFrederic Weisbecker 			/* it's the next argument */
2070adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
2071fef20d9cSFrederic Weisbecker 			return ++fmt - start;
2072fef20d9cSFrederic Weisbecker 		}
2073fef20d9cSFrederic Weisbecker 	}
2074fef20d9cSFrederic Weisbecker 
2075fef20d9cSFrederic Weisbecker qualifier:
2076fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
2077d0484193SRasmus Villemoes 	qualifier = 0;
207875fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
20795b5e0928SAlexey Dobriyan 	    *fmt == 'z' || *fmt == 't') {
2080d0484193SRasmus Villemoes 		qualifier = *fmt++;
2081d0484193SRasmus Villemoes 		if (unlikely(qualifier == *fmt)) {
2082d0484193SRasmus Villemoes 			if (qualifier == 'l') {
2083d0484193SRasmus Villemoes 				qualifier = 'L';
2084fef20d9cSFrederic Weisbecker 				++fmt;
2085d0484193SRasmus Villemoes 			} else if (qualifier == 'h') {
2086d0484193SRasmus Villemoes 				qualifier = 'H';
2087a4e94ef0SZhaolei 				++fmt;
2088a4e94ef0SZhaolei 			}
2089fef20d9cSFrederic Weisbecker 		}
2090fef20d9cSFrederic Weisbecker 	}
2091fef20d9cSFrederic Weisbecker 
2092fef20d9cSFrederic Weisbecker 	/* default base */
2093fef20d9cSFrederic Weisbecker 	spec->base = 10;
2094fef20d9cSFrederic Weisbecker 	switch (*fmt) {
2095fef20d9cSFrederic Weisbecker 	case 'c':
2096fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
2097fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2098fef20d9cSFrederic Weisbecker 
2099fef20d9cSFrederic Weisbecker 	case 's':
2100fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
2101fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2102fef20d9cSFrederic Weisbecker 
2103fef20d9cSFrederic Weisbecker 	case 'p':
2104fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
2105ffbfed03SRasmus Villemoes 		return ++fmt - start;
2106fef20d9cSFrederic Weisbecker 
2107fef20d9cSFrederic Weisbecker 	case '%':
2108fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
2109fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2110fef20d9cSFrederic Weisbecker 
2111fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
2112fef20d9cSFrederic Weisbecker 	case 'o':
2113fef20d9cSFrederic Weisbecker 		spec->base = 8;
2114fef20d9cSFrederic Weisbecker 		break;
2115fef20d9cSFrederic Weisbecker 
2116fef20d9cSFrederic Weisbecker 	case 'x':
2117fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
2118fef20d9cSFrederic Weisbecker 
2119fef20d9cSFrederic Weisbecker 	case 'X':
2120fef20d9cSFrederic Weisbecker 		spec->base = 16;
2121fef20d9cSFrederic Weisbecker 		break;
2122fef20d9cSFrederic Weisbecker 
2123fef20d9cSFrederic Weisbecker 	case 'd':
2124fef20d9cSFrederic Weisbecker 	case 'i':
212539e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
2126fef20d9cSFrederic Weisbecker 	case 'u':
2127fef20d9cSFrederic Weisbecker 		break;
2128fef20d9cSFrederic Weisbecker 
2129708d96fdSRyan Mallon 	case 'n':
2130708d96fdSRyan Mallon 		/*
2131b006f19bSRasmus Villemoes 		 * Since %n poses a greater security risk than
2132b006f19bSRasmus Villemoes 		 * utility, treat it as any other invalid or
2133b006f19bSRasmus Villemoes 		 * unsupported format specifier.
2134708d96fdSRyan Mallon 		 */
2135708d96fdSRyan Mallon 		/* Fall-through */
2136708d96fdSRyan Mallon 
2137fef20d9cSFrederic Weisbecker 	default:
2138b006f19bSRasmus Villemoes 		WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2139fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
2140fef20d9cSFrederic Weisbecker 		return fmt - start;
2141fef20d9cSFrederic Weisbecker 	}
2142fef20d9cSFrederic Weisbecker 
2143d0484193SRasmus Villemoes 	if (qualifier == 'L')
2144fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
2145d0484193SRasmus Villemoes 	else if (qualifier == 'l') {
214651be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
214751be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
21485b5e0928SAlexey Dobriyan 	} else if (qualifier == 'z') {
2149fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
2150d0484193SRasmus Villemoes 	} else if (qualifier == 't') {
2151fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
2152d0484193SRasmus Villemoes 	} else if (qualifier == 'H') {
215351be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
215451be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2155d0484193SRasmus Villemoes 	} else if (qualifier == 'h') {
215651be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
215751be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2158fef20d9cSFrederic Weisbecker 	} else {
215951be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
216051be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2161fef20d9cSFrederic Weisbecker 	}
2162fef20d9cSFrederic Weisbecker 
2163fef20d9cSFrederic Weisbecker 	return ++fmt - start;
216478a8bf69SLinus Torvalds }
216578a8bf69SLinus Torvalds 
21664d72ba01SRasmus Villemoes static void
21674d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width)
21684d72ba01SRasmus Villemoes {
21694d72ba01SRasmus Villemoes 	spec->field_width = width;
21704d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
21714d72ba01SRasmus Villemoes 		spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
21724d72ba01SRasmus Villemoes 	}
21734d72ba01SRasmus Villemoes }
21744d72ba01SRasmus Villemoes 
21754d72ba01SRasmus Villemoes static void
21764d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec)
21774d72ba01SRasmus Villemoes {
21784d72ba01SRasmus Villemoes 	spec->precision = prec;
21794d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
21804d72ba01SRasmus Villemoes 		spec->precision = clamp(prec, 0, PRECISION_MAX);
21814d72ba01SRasmus Villemoes 	}
21824d72ba01SRasmus Villemoes }
21834d72ba01SRasmus Villemoes 
21841da177e4SLinus Torvalds /**
21851da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
21861da177e4SLinus Torvalds  * @buf: The buffer to place the result into
21871da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
21881da177e4SLinus Torvalds  * @fmt: The format string to use
21891da177e4SLinus Torvalds  * @args: Arguments for the format string
21901da177e4SLinus Torvalds  *
2191d7ec9a05SRasmus Villemoes  * This function generally follows C99 vsnprintf, but has some
2192d7ec9a05SRasmus Villemoes  * extensions and a few limitations:
2193d7ec9a05SRasmus Villemoes  *
21946cc89134Smchehab@s-opensource.com  *  - ``%n`` is unsupported
21956cc89134Smchehab@s-opensource.com  *  - ``%p*`` is handled by pointer()
219620036fdcSAndi Kleen  *
219727e7c0e8SJonathan Corbet  * See pointer() or Documentation/core-api/printk-formats.rst for more
21985e4ee7b1SMartin Kletzander  * extensive description.
21995e4ee7b1SMartin Kletzander  *
22005e4ee7b1SMartin Kletzander  * **Please update the documentation in both places when making changes**
220180f548e0SAndrew Morton  *
22021da177e4SLinus Torvalds  * The return value is the number of characters which would
22031da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
22041da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
22051da177e4SLinus Torvalds  * number of characters written into @buf as return value
220672fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
22071da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
22081da177e4SLinus Torvalds  * string is truncated.
22091da177e4SLinus Torvalds  *
2210ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
22111da177e4SLinus Torvalds  */
22121da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
22131da177e4SLinus Torvalds {
22141da177e4SLinus Torvalds 	unsigned long long num;
2215d4be151bSAndré Goddard Rosa 	char *str, *end;
2216fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
22171da177e4SLinus Torvalds 
2218f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
2219f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
22202aa2f9e2SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
22211da177e4SLinus Torvalds 		return 0;
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds 	str = buf;
2224f796937aSJeremy Fitzhardinge 	end = buf + size;
22251da177e4SLinus Torvalds 
2226f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
2227f796937aSJeremy Fitzhardinge 	if (end < buf) {
22281da177e4SLinus Torvalds 		end = ((void *)-1);
2229f796937aSJeremy Fitzhardinge 		size = end - buf;
22301da177e4SLinus Torvalds 	}
22311da177e4SLinus Torvalds 
2232fef20d9cSFrederic Weisbecker 	while (*fmt) {
2233fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2234d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2235fef20d9cSFrederic Weisbecker 
2236fef20d9cSFrederic Weisbecker 		fmt += read;
2237fef20d9cSFrederic Weisbecker 
2238fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2239fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2240fef20d9cSFrederic Weisbecker 			int copy = read;
2241fef20d9cSFrederic Weisbecker 			if (str < end) {
2242fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2243fef20d9cSFrederic Weisbecker 					copy = end - str;
2244fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2245fef20d9cSFrederic Weisbecker 			}
2246fef20d9cSFrederic Weisbecker 			str += read;
2247fef20d9cSFrederic Weisbecker 			break;
22481da177e4SLinus Torvalds 		}
22491da177e4SLinus Torvalds 
2250ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
22514d72ba01SRasmus Villemoes 			set_field_width(&spec, va_arg(args, int));
2252fef20d9cSFrederic Weisbecker 			break;
22531da177e4SLinus Torvalds 
2254fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
22554d72ba01SRasmus Villemoes 			set_precision(&spec, va_arg(args, int));
2256fef20d9cSFrederic Weisbecker 			break;
22571da177e4SLinus Torvalds 
2258d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2259d4be151bSAndré Goddard Rosa 			char c;
2260d4be151bSAndré Goddard Rosa 
2261fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2262fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
2263f796937aSJeremy Fitzhardinge 					if (str < end)
22641da177e4SLinus Torvalds 						*str = ' ';
22651da177e4SLinus Torvalds 					++str;
2266fef20d9cSFrederic Weisbecker 
22671da177e4SLinus Torvalds 				}
22681da177e4SLinus Torvalds 			}
22691da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
2270f796937aSJeremy Fitzhardinge 			if (str < end)
22711da177e4SLinus Torvalds 				*str = c;
22721da177e4SLinus Torvalds 			++str;
2273fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
2274f796937aSJeremy Fitzhardinge 				if (str < end)
22751da177e4SLinus Torvalds 					*str = ' ';
22761da177e4SLinus Torvalds 				++str;
22771da177e4SLinus Torvalds 			}
2278fef20d9cSFrederic Weisbecker 			break;
2279d4be151bSAndré Goddard Rosa 		}
22801da177e4SLinus Torvalds 
2281fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
2282fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
2283fef20d9cSFrederic Weisbecker 			break;
22841da177e4SLinus Torvalds 
2285fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2286ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, va_arg(args, void *),
2287fef20d9cSFrederic Weisbecker 				      spec);
2288fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
22894d8a743cSLinus Torvalds 				fmt++;
2290fef20d9cSFrederic Weisbecker 			break;
22911da177e4SLinus Torvalds 
2292fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2293f796937aSJeremy Fitzhardinge 			if (str < end)
22941da177e4SLinus Torvalds 				*str = '%';
22951da177e4SLinus Torvalds 			++str;
22961da177e4SLinus Torvalds 			break;
22971da177e4SLinus Torvalds 
2298fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
2299b006f19bSRasmus Villemoes 			/*
2300b006f19bSRasmus Villemoes 			 * Presumably the arguments passed gcc's type
2301b006f19bSRasmus Villemoes 			 * checking, but there is no safe or sane way
2302b006f19bSRasmus Villemoes 			 * for us to continue parsing the format and
2303b006f19bSRasmus Villemoes 			 * fetching from the va_list; the remaining
2304b006f19bSRasmus Villemoes 			 * specifiers and arguments would be out of
2305b006f19bSRasmus Villemoes 			 * sync.
2306b006f19bSRasmus Villemoes 			 */
2307b006f19bSRasmus Villemoes 			goto out;
2308fef20d9cSFrederic Weisbecker 
2309fef20d9cSFrederic Weisbecker 		default:
2310fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2311fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2312fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
2313fef20d9cSFrederic Weisbecker 				break;
2314fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2315fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
2316fef20d9cSFrederic Weisbecker 				break;
2317fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2318fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
2319fef20d9cSFrederic Weisbecker 				break;
2320fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2321ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
2322ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
2323ef124960SJason Gunthorpe 				else
2324fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
2325fef20d9cSFrederic Weisbecker 				break;
2326fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2327fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
2328fef20d9cSFrederic Weisbecker 				break;
2329a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2330a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
2331a4e94ef0SZhaolei 				break;
2332a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2333a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
2334a4e94ef0SZhaolei 				break;
2335fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2336fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
2337fef20d9cSFrederic Weisbecker 				break;
2338fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2339fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
2340fef20d9cSFrederic Weisbecker 				break;
234139e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
234239e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
2343fef20d9cSFrederic Weisbecker 				break;
2344fef20d9cSFrederic Weisbecker 			default:
2345fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
23461da177e4SLinus Torvalds 			}
2347fef20d9cSFrederic Weisbecker 
2348fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
23491da177e4SLinus Torvalds 		}
2350fef20d9cSFrederic Weisbecker 	}
2351fef20d9cSFrederic Weisbecker 
2352b006f19bSRasmus Villemoes out:
2353f796937aSJeremy Fitzhardinge 	if (size > 0) {
2354f796937aSJeremy Fitzhardinge 		if (str < end)
23551da177e4SLinus Torvalds 			*str = '\0';
2356f796937aSJeremy Fitzhardinge 		else
23570a6047eeSLinus Torvalds 			end[-1] = '\0';
2358f796937aSJeremy Fitzhardinge 	}
2359fef20d9cSFrederic Weisbecker 
2360f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
23611da177e4SLinus Torvalds 	return str-buf;
2362fef20d9cSFrederic Weisbecker 
23631da177e4SLinus Torvalds }
23641da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds /**
23671da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
23681da177e4SLinus Torvalds  * @buf: The buffer to place the result into
23691da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
23701da177e4SLinus Torvalds  * @fmt: The format string to use
23711da177e4SLinus Torvalds  * @args: Arguments for the format string
23721da177e4SLinus Torvalds  *
23731da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
2374b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
23751da177e4SLinus Torvalds  * returns 0.
23761da177e4SLinus Torvalds  *
2377ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
237820036fdcSAndi Kleen  *
237920036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
23801da177e4SLinus Torvalds  */
23811da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
23821da177e4SLinus Torvalds {
23831da177e4SLinus Torvalds 	int i;
23841da177e4SLinus Torvalds 
23851da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
23867b9186f5SAndré Goddard Rosa 
2387b921c69fSAnton Arapov 	if (likely(i < size))
2388b921c69fSAnton Arapov 		return i;
2389b921c69fSAnton Arapov 	if (size != 0)
2390b921c69fSAnton Arapov 		return size - 1;
2391b921c69fSAnton Arapov 	return 0;
23921da177e4SLinus Torvalds }
23931da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
23941da177e4SLinus Torvalds 
23951da177e4SLinus Torvalds /**
23961da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
23971da177e4SLinus Torvalds  * @buf: The buffer to place the result into
23981da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
23991da177e4SLinus Torvalds  * @fmt: The format string to use
24001da177e4SLinus Torvalds  * @...: Arguments for the format string
24011da177e4SLinus Torvalds  *
24021da177e4SLinus Torvalds  * The return value is the number of characters which would be
24031da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
24041da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
24051da177e4SLinus Torvalds  * @size, the resulting string is truncated.
240620036fdcSAndi Kleen  *
240720036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
24081da177e4SLinus Torvalds  */
24091da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
24101da177e4SLinus Torvalds {
24111da177e4SLinus Torvalds 	va_list args;
24121da177e4SLinus Torvalds 	int i;
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds 	va_start(args, fmt);
24151da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
24161da177e4SLinus Torvalds 	va_end(args);
24177b9186f5SAndré Goddard Rosa 
24181da177e4SLinus Torvalds 	return i;
24191da177e4SLinus Torvalds }
24201da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds /**
24231da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
24241da177e4SLinus Torvalds  * @buf: The buffer to place the result into
24251da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
24261da177e4SLinus Torvalds  * @fmt: The format string to use
24271da177e4SLinus Torvalds  * @...: Arguments for the format string
24281da177e4SLinus Torvalds  *
24291da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
2430b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
24311da177e4SLinus Torvalds  */
24321da177e4SLinus Torvalds 
24331da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
24341da177e4SLinus Torvalds {
24351da177e4SLinus Torvalds 	va_list args;
24361da177e4SLinus Torvalds 	int i;
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds 	va_start(args, fmt);
2439b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
24401da177e4SLinus Torvalds 	va_end(args);
24417b9186f5SAndré Goddard Rosa 
2442b903c0b8SChangli Gao 	return i;
24431da177e4SLinus Torvalds }
24441da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds /**
24471da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
24481da177e4SLinus Torvalds  * @buf: The buffer to place the result into
24491da177e4SLinus Torvalds  * @fmt: The format string to use
24501da177e4SLinus Torvalds  * @args: Arguments for the format string
24511da177e4SLinus Torvalds  *
24521da177e4SLinus Torvalds  * The function returns the number of characters written
245372fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
24541da177e4SLinus Torvalds  * buffer overflows.
24551da177e4SLinus Torvalds  *
2456ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
245720036fdcSAndi Kleen  *
245820036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
24591da177e4SLinus Torvalds  */
24601da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
24611da177e4SLinus Torvalds {
24621da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
24631da177e4SLinus Torvalds }
24641da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
24651da177e4SLinus Torvalds 
24661da177e4SLinus Torvalds /**
24671da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
24681da177e4SLinus Torvalds  * @buf: The buffer to place the result into
24691da177e4SLinus Torvalds  * @fmt: The format string to use
24701da177e4SLinus Torvalds  * @...: Arguments for the format string
24711da177e4SLinus Torvalds  *
24721da177e4SLinus Torvalds  * The function returns the number of characters written
247372fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
24741da177e4SLinus Torvalds  * buffer overflows.
247520036fdcSAndi Kleen  *
247620036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
24771da177e4SLinus Torvalds  */
24781da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
24791da177e4SLinus Torvalds {
24801da177e4SLinus Torvalds 	va_list args;
24811da177e4SLinus Torvalds 	int i;
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds 	va_start(args, fmt);
24841da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
24851da177e4SLinus Torvalds 	va_end(args);
24867b9186f5SAndré Goddard Rosa 
24871da177e4SLinus Torvalds 	return i;
24881da177e4SLinus Torvalds }
24891da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
24901da177e4SLinus Torvalds 
24914370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
24924370aa4aSLai Jiangshan /*
24934370aa4aSLai Jiangshan  * bprintf service:
24944370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
24954370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
24964370aa4aSLai Jiangshan  */
24974370aa4aSLai Jiangshan 
24984370aa4aSLai Jiangshan /**
24994370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
25004370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
25014370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
25024370aa4aSLai Jiangshan  * @fmt: The format string to use
25034370aa4aSLai Jiangshan  * @args: Arguments for the format string
25044370aa4aSLai Jiangshan  *
25054370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
2506da3dae54SMasanari Iida  * is skipped.
25074370aa4aSLai Jiangshan  *
25084370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
25094370aa4aSLai Jiangshan  * the given input.
25104370aa4aSLai Jiangshan  *
25114370aa4aSLai Jiangshan  * NOTE:
25124370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
25134370aa4aSLai Jiangshan  * valid for bstr_printf().
25144370aa4aSLai Jiangshan  */
25154370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
25164370aa4aSLai Jiangshan {
2517fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
25184370aa4aSLai Jiangshan 	char *str, *end;
2519841a915dSSteven Rostedt (VMware) 	int width;
25204370aa4aSLai Jiangshan 
25214370aa4aSLai Jiangshan 	str = (char *)bin_buf;
25224370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
25234370aa4aSLai Jiangshan 
25244370aa4aSLai Jiangshan #define save_arg(type)							\
2525841a915dSSteven Rostedt (VMware) ({									\
25264370aa4aSLai Jiangshan 	unsigned long long value;					\
2527841a915dSSteven Rostedt (VMware) 	if (sizeof(type) == 8) {					\
2528841a915dSSteven Rostedt (VMware) 		unsigned long long val8;				\
25294370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
2530841a915dSSteven Rostedt (VMware) 		val8 = va_arg(args, unsigned long long);		\
25314370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
2532841a915dSSteven Rostedt (VMware) 			*(u32 *)str = *(u32 *)&val8;			\
2533841a915dSSteven Rostedt (VMware) 			*(u32 *)(str + 4) = *((u32 *)&val8 + 1);	\
25344370aa4aSLai Jiangshan 		}							\
2535841a915dSSteven Rostedt (VMware) 		value = val8;						\
25364370aa4aSLai Jiangshan 	} else {							\
2537841a915dSSteven Rostedt (VMware) 		unsigned int val4;					\
25384370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
2539841a915dSSteven Rostedt (VMware) 		val4 = va_arg(args, int);				\
25404370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
2541841a915dSSteven Rostedt (VMware) 			*(typeof(type) *)str = (type)(long)val4;	\
2542841a915dSSteven Rostedt (VMware) 		value = (unsigned long long)val4;			\
25434370aa4aSLai Jiangshan 	}								\
25444370aa4aSLai Jiangshan 	str += sizeof(type);						\
2545841a915dSSteven Rostedt (VMware) 	value;								\
2546841a915dSSteven Rostedt (VMware) })
25474370aa4aSLai Jiangshan 
2548fef20d9cSFrederic Weisbecker 	while (*fmt) {
2549d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
25504370aa4aSLai Jiangshan 
2551fef20d9cSFrederic Weisbecker 		fmt += read;
2552fef20d9cSFrederic Weisbecker 
2553fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2554fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
2555d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
2556fef20d9cSFrederic Weisbecker 			break;
2557b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
2558b006f19bSRasmus Villemoes 			goto out;
2559fef20d9cSFrederic Weisbecker 
2560ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2561fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2562841a915dSSteven Rostedt (VMware) 			width = (int)save_arg(int);
2563841a915dSSteven Rostedt (VMware) 			/* Pointers may require the width */
2564841a915dSSteven Rostedt (VMware) 			if (*fmt == 'p')
2565841a915dSSteven Rostedt (VMware) 				set_field_width(&spec, width);
2566fef20d9cSFrederic Weisbecker 			break;
25674370aa4aSLai Jiangshan 
2568fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
25694370aa4aSLai Jiangshan 			save_arg(char);
2570fef20d9cSFrederic Weisbecker 			break;
2571fef20d9cSFrederic Weisbecker 
2572fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
25734370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
25744370aa4aSLai Jiangshan 			size_t len;
25756c356634SAndré Goddard Rosa 
25764370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
25774370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
25780f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
25796c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
25806c356634SAndré Goddard Rosa 			if (str + len < end)
25816c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
25826c356634SAndré Goddard Rosa 			str += len;
2583fef20d9cSFrederic Weisbecker 			break;
25844370aa4aSLai Jiangshan 		}
2585fef20d9cSFrederic Weisbecker 
2586fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2587841a915dSSteven Rostedt (VMware) 			/* Dereferenced pointers must be done now */
2588841a915dSSteven Rostedt (VMware) 			switch (*fmt) {
2589841a915dSSteven Rostedt (VMware) 			/* Dereference of functions is still OK */
2590841a915dSSteven Rostedt (VMware) 			case 'S':
2591841a915dSSteven Rostedt (VMware) 			case 's':
2592841a915dSSteven Rostedt (VMware) 			case 'F':
2593841a915dSSteven Rostedt (VMware) 			case 'f':
25944370aa4aSLai Jiangshan 				save_arg(void *);
2595841a915dSSteven Rostedt (VMware) 				break;
2596841a915dSSteven Rostedt (VMware) 			default:
2597841a915dSSteven Rostedt (VMware) 				if (!isalnum(*fmt)) {
2598841a915dSSteven Rostedt (VMware) 					save_arg(void *);
2599841a915dSSteven Rostedt (VMware) 					break;
2600841a915dSSteven Rostedt (VMware) 				}
2601841a915dSSteven Rostedt (VMware) 				str = pointer(fmt, str, end, va_arg(args, void *),
2602841a915dSSteven Rostedt (VMware) 					      spec);
2603841a915dSSteven Rostedt (VMware) 				if (str + 1 < end)
2604841a915dSSteven Rostedt (VMware) 					*str++ = '\0';
2605841a915dSSteven Rostedt (VMware) 				else
2606841a915dSSteven Rostedt (VMware) 					end[-1] = '\0'; /* Must be nul terminated */
2607841a915dSSteven Rostedt (VMware) 			}
26084370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2609fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
26104370aa4aSLai Jiangshan 				fmt++;
2611fef20d9cSFrederic Weisbecker 			break;
2612fef20d9cSFrederic Weisbecker 
2613fef20d9cSFrederic Weisbecker 		default:
2614fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2615fef20d9cSFrederic Weisbecker 
2616fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2617fef20d9cSFrederic Weisbecker 				save_arg(long long);
2618fef20d9cSFrederic Weisbecker 				break;
2619fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2620fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2621fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
2622fef20d9cSFrederic Weisbecker 				break;
2623fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2624fef20d9cSFrederic Weisbecker 				save_arg(size_t);
2625fef20d9cSFrederic Weisbecker 				break;
2626fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2627fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
2628fef20d9cSFrederic Weisbecker 				break;
2629a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2630a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2631a4e94ef0SZhaolei 				save_arg(char);
2632a4e94ef0SZhaolei 				break;
2633fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2634fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2635fef20d9cSFrederic Weisbecker 				save_arg(short);
2636fef20d9cSFrederic Weisbecker 				break;
2637fef20d9cSFrederic Weisbecker 			default:
2638fef20d9cSFrederic Weisbecker 				save_arg(int);
2639fef20d9cSFrederic Weisbecker 			}
2640fef20d9cSFrederic Weisbecker 		}
2641fef20d9cSFrederic Weisbecker 	}
2642fef20d9cSFrederic Weisbecker 
2643b006f19bSRasmus Villemoes out:
26447b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2645fef20d9cSFrederic Weisbecker #undef save_arg
26464370aa4aSLai Jiangshan }
26474370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
26484370aa4aSLai Jiangshan 
26494370aa4aSLai Jiangshan /**
26504370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
26514370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
26524370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
26534370aa4aSLai Jiangshan  * @fmt: The format string to use
26544370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
26554370aa4aSLai Jiangshan  *
26564370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
26574370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
26584370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
26594370aa4aSLai Jiangshan  *
26604370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
26610efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
26624370aa4aSLai Jiangshan  *
26634370aa4aSLai Jiangshan  * The return value is the number of characters which would
26644370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
26654370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
26664370aa4aSLai Jiangshan  * number of characters written into @buf as return value
26674370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
26684370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
26694370aa4aSLai Jiangshan  * string is truncated.
26704370aa4aSLai Jiangshan  */
26714370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
26724370aa4aSLai Jiangshan {
2673fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
2674d4be151bSAndré Goddard Rosa 	char *str, *end;
2675d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
26764370aa4aSLai Jiangshan 
2677762abb51SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
26784370aa4aSLai Jiangshan 		return 0;
26794370aa4aSLai Jiangshan 
26804370aa4aSLai Jiangshan 	str = buf;
26814370aa4aSLai Jiangshan 	end = buf + size;
26824370aa4aSLai Jiangshan 
26834370aa4aSLai Jiangshan #define get_arg(type)							\
26844370aa4aSLai Jiangshan ({									\
26854370aa4aSLai Jiangshan 	typeof(type) value;						\
26864370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
26874370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
26884370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
26894370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
26904370aa4aSLai Jiangshan 	} else {							\
26914370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
26924370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
26934370aa4aSLai Jiangshan 	}								\
26944370aa4aSLai Jiangshan 	args += sizeof(type);						\
26954370aa4aSLai Jiangshan 	value;								\
26964370aa4aSLai Jiangshan })
26974370aa4aSLai Jiangshan 
26984370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
26994370aa4aSLai Jiangshan 	if (end < buf) {
27004370aa4aSLai Jiangshan 		end = ((void *)-1);
27014370aa4aSLai Jiangshan 		size = end - buf;
27024370aa4aSLai Jiangshan 	}
27034370aa4aSLai Jiangshan 
2704fef20d9cSFrederic Weisbecker 	while (*fmt) {
2705fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2706d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2707fef20d9cSFrederic Weisbecker 
2708fef20d9cSFrederic Weisbecker 		fmt += read;
2709fef20d9cSFrederic Weisbecker 
2710fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2711fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2712fef20d9cSFrederic Weisbecker 			int copy = read;
2713fef20d9cSFrederic Weisbecker 			if (str < end) {
2714fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2715fef20d9cSFrederic Weisbecker 					copy = end - str;
2716fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2717fef20d9cSFrederic Weisbecker 			}
2718fef20d9cSFrederic Weisbecker 			str += read;
2719fef20d9cSFrederic Weisbecker 			break;
27204370aa4aSLai Jiangshan 		}
27214370aa4aSLai Jiangshan 
2722ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
27234d72ba01SRasmus Villemoes 			set_field_width(&spec, get_arg(int));
2724fef20d9cSFrederic Weisbecker 			break;
27254370aa4aSLai Jiangshan 
2726fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
27274d72ba01SRasmus Villemoes 			set_precision(&spec, get_arg(int));
2728fef20d9cSFrederic Weisbecker 			break;
27294370aa4aSLai Jiangshan 
2730d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2731d4be151bSAndré Goddard Rosa 			char c;
2732d4be151bSAndré Goddard Rosa 
2733fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2734fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
27354370aa4aSLai Jiangshan 					if (str < end)
27364370aa4aSLai Jiangshan 						*str = ' ';
27374370aa4aSLai Jiangshan 					++str;
27384370aa4aSLai Jiangshan 				}
27394370aa4aSLai Jiangshan 			}
27404370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
27414370aa4aSLai Jiangshan 			if (str < end)
27424370aa4aSLai Jiangshan 				*str = c;
27434370aa4aSLai Jiangshan 			++str;
2744fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
27454370aa4aSLai Jiangshan 				if (str < end)
27464370aa4aSLai Jiangshan 					*str = ' ';
27474370aa4aSLai Jiangshan 				++str;
27484370aa4aSLai Jiangshan 			}
2749fef20d9cSFrederic Weisbecker 			break;
2750d4be151bSAndré Goddard Rosa 		}
27514370aa4aSLai Jiangshan 
2752fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
27534370aa4aSLai Jiangshan 			const char *str_arg = args;
2754d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
2755fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
2756fef20d9cSFrederic Weisbecker 			break;
27574370aa4aSLai Jiangshan 		}
27584370aa4aSLai Jiangshan 
2759841a915dSSteven Rostedt (VMware) 		case FORMAT_TYPE_PTR: {
2760841a915dSSteven Rostedt (VMware) 			bool process = false;
2761841a915dSSteven Rostedt (VMware) 			int copy, len;
2762841a915dSSteven Rostedt (VMware) 			/* Non function dereferences were already done */
2763841a915dSSteven Rostedt (VMware) 			switch (*fmt) {
2764841a915dSSteven Rostedt (VMware) 			case 'S':
2765841a915dSSteven Rostedt (VMware) 			case 's':
2766841a915dSSteven Rostedt (VMware) 			case 'F':
2767841a915dSSteven Rostedt (VMware) 			case 'f':
2768841a915dSSteven Rostedt (VMware) 				process = true;
2769841a915dSSteven Rostedt (VMware) 				break;
2770841a915dSSteven Rostedt (VMware) 			default:
2771841a915dSSteven Rostedt (VMware) 				if (!isalnum(*fmt)) {
2772841a915dSSteven Rostedt (VMware) 					process = true;
2773841a915dSSteven Rostedt (VMware) 					break;
2774841a915dSSteven Rostedt (VMware) 				}
2775841a915dSSteven Rostedt (VMware) 				/* Pointer dereference was already processed */
2776841a915dSSteven Rostedt (VMware) 				if (str < end) {
2777841a915dSSteven Rostedt (VMware) 					len = copy = strlen(args);
2778841a915dSSteven Rostedt (VMware) 					if (copy > end - str)
2779841a915dSSteven Rostedt (VMware) 						copy = end - str;
2780841a915dSSteven Rostedt (VMware) 					memcpy(str, args, copy);
2781841a915dSSteven Rostedt (VMware) 					str += len;
2782841a915dSSteven Rostedt (VMware) 					args += len;
2783841a915dSSteven Rostedt (VMware) 				}
2784841a915dSSteven Rostedt (VMware) 			}
2785841a915dSSteven Rostedt (VMware) 			if (process)
2786ffbfed03SRasmus Villemoes 				str = pointer(fmt, str, end, get_arg(void *), spec);
2787841a915dSSteven Rostedt (VMware) 
2788fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
27894370aa4aSLai Jiangshan 				fmt++;
2790fef20d9cSFrederic Weisbecker 			break;
2791841a915dSSteven Rostedt (VMware) 		}
27924370aa4aSLai Jiangshan 
2793fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
27944370aa4aSLai Jiangshan 			if (str < end)
27954370aa4aSLai Jiangshan 				*str = '%';
27964370aa4aSLai Jiangshan 			++str;
2797fef20d9cSFrederic Weisbecker 			break;
2798fef20d9cSFrederic Weisbecker 
2799b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
2800b006f19bSRasmus Villemoes 			goto out;
2801b006f19bSRasmus Villemoes 
2802d4be151bSAndré Goddard Rosa 		default: {
2803d4be151bSAndré Goddard Rosa 			unsigned long long num;
2804d4be151bSAndré Goddard Rosa 
2805fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2806fef20d9cSFrederic Weisbecker 
2807fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
28084370aa4aSLai Jiangshan 				num = get_arg(long long);
2809fef20d9cSFrederic Weisbecker 				break;
2810fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2811fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2812fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
2813fef20d9cSFrederic Weisbecker 				break;
2814fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
28154370aa4aSLai Jiangshan 				num = get_arg(size_t);
2816fef20d9cSFrederic Weisbecker 				break;
2817fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
28184370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
2819fef20d9cSFrederic Weisbecker 				break;
2820a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2821a4e94ef0SZhaolei 				num = get_arg(unsigned char);
2822a4e94ef0SZhaolei 				break;
2823a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2824a4e94ef0SZhaolei 				num = get_arg(signed char);
2825a4e94ef0SZhaolei 				break;
2826fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2827fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
2828fef20d9cSFrederic Weisbecker 				break;
2829fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2830fef20d9cSFrederic Weisbecker 				num = get_arg(short);
2831fef20d9cSFrederic Weisbecker 				break;
2832fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
28334370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
2834fef20d9cSFrederic Weisbecker 				break;
2835fef20d9cSFrederic Weisbecker 			default:
2836fef20d9cSFrederic Weisbecker 				num = get_arg(int);
28374370aa4aSLai Jiangshan 			}
2838fef20d9cSFrederic Weisbecker 
2839fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
2840d4be151bSAndré Goddard Rosa 		} /* default: */
2841d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
2842d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
2843fef20d9cSFrederic Weisbecker 
2844b006f19bSRasmus Villemoes out:
28454370aa4aSLai Jiangshan 	if (size > 0) {
28464370aa4aSLai Jiangshan 		if (str < end)
28474370aa4aSLai Jiangshan 			*str = '\0';
28484370aa4aSLai Jiangshan 		else
28494370aa4aSLai Jiangshan 			end[-1] = '\0';
28504370aa4aSLai Jiangshan 	}
2851fef20d9cSFrederic Weisbecker 
28524370aa4aSLai Jiangshan #undef get_arg
28534370aa4aSLai Jiangshan 
28544370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
28554370aa4aSLai Jiangshan 	return str - buf;
28564370aa4aSLai Jiangshan }
28574370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
28584370aa4aSLai Jiangshan 
28594370aa4aSLai Jiangshan /**
28604370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
28614370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
28624370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
28634370aa4aSLai Jiangshan  * @fmt: The format string to use
28644370aa4aSLai Jiangshan  * @...: Arguments for the format string
28654370aa4aSLai Jiangshan  *
28664370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
28674370aa4aSLai Jiangshan  * into @bin_buf.
28684370aa4aSLai Jiangshan  */
28694370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
28704370aa4aSLai Jiangshan {
28714370aa4aSLai Jiangshan 	va_list args;
28724370aa4aSLai Jiangshan 	int ret;
28734370aa4aSLai Jiangshan 
28744370aa4aSLai Jiangshan 	va_start(args, fmt);
28754370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
28764370aa4aSLai Jiangshan 	va_end(args);
28777b9186f5SAndré Goddard Rosa 
28784370aa4aSLai Jiangshan 	return ret;
28794370aa4aSLai Jiangshan }
28804370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
28814370aa4aSLai Jiangshan 
28824370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
28834370aa4aSLai Jiangshan 
28841da177e4SLinus Torvalds /**
28851da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
28861da177e4SLinus Torvalds  * @buf:	input buffer
28871da177e4SLinus Torvalds  * @fmt:	format of buffer
28881da177e4SLinus Torvalds  * @args:	arguments
28891da177e4SLinus Torvalds  */
28901da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
28911da177e4SLinus Torvalds {
28921da177e4SLinus Torvalds 	const char *str = buf;
28931da177e4SLinus Torvalds 	char *next;
28941da177e4SLinus Torvalds 	char digit;
28951da177e4SLinus Torvalds 	int num = 0;
2896ef0658f3SJoe Perches 	u8 qualifier;
289753809751SJan Beulich 	unsigned int base;
289853809751SJan Beulich 	union {
289953809751SJan Beulich 		long long s;
290053809751SJan Beulich 		unsigned long long u;
290153809751SJan Beulich 	} val;
2902ef0658f3SJoe Perches 	s16 field_width;
2903d4be151bSAndré Goddard Rosa 	bool is_sign;
29041da177e4SLinus Torvalds 
2905da99075cSJan Beulich 	while (*fmt) {
29061da177e4SLinus Torvalds 		/* skip any white space in format */
29071da177e4SLinus Torvalds 		/* white space in format matchs any amount of
29081da177e4SLinus Torvalds 		 * white space, including none, in the input.
29091da177e4SLinus Torvalds 		 */
29101da177e4SLinus Torvalds 		if (isspace(*fmt)) {
2911e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
2912e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
29131da177e4SLinus Torvalds 		}
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
29161da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
29171da177e4SLinus Torvalds 			if (*fmt++ != *str++)
29181da177e4SLinus Torvalds 				break;
29191da177e4SLinus Torvalds 			continue;
29201da177e4SLinus Torvalds 		}
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds 		if (!*fmt)
29231da177e4SLinus Torvalds 			break;
29241da177e4SLinus Torvalds 		++fmt;
29251da177e4SLinus Torvalds 
29261da177e4SLinus Torvalds 		/* skip this conversion.
29271da177e4SLinus Torvalds 		 * advance both strings to next white space
29281da177e4SLinus Torvalds 		 */
29291da177e4SLinus Torvalds 		if (*fmt == '*') {
2930da99075cSJan Beulich 			if (!*str)
2931da99075cSJan Beulich 				break;
2932f9310b2fSJessica Yu 			while (!isspace(*fmt) && *fmt != '%' && *fmt) {
2933f9310b2fSJessica Yu 				/* '%*[' not yet supported, invalid format */
2934f9310b2fSJessica Yu 				if (*fmt == '[')
2935f9310b2fSJessica Yu 					return num;
29361da177e4SLinus Torvalds 				fmt++;
2937f9310b2fSJessica Yu 			}
29381da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
29391da177e4SLinus Torvalds 				str++;
29401da177e4SLinus Torvalds 			continue;
29411da177e4SLinus Torvalds 		}
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds 		/* get field width */
29441da177e4SLinus Torvalds 		field_width = -1;
294553809751SJan Beulich 		if (isdigit(*fmt)) {
29461da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
294753809751SJan Beulich 			if (field_width <= 0)
294853809751SJan Beulich 				break;
294953809751SJan Beulich 		}
29501da177e4SLinus Torvalds 
29511da177e4SLinus Torvalds 		/* get conversion qualifier */
29521da177e4SLinus Torvalds 		qualifier = -1;
295375fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
29545b5e0928SAlexey Dobriyan 		    *fmt == 'z') {
29551da177e4SLinus Torvalds 			qualifier = *fmt++;
29561da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
29571da177e4SLinus Torvalds 				if (qualifier == 'h') {
29581da177e4SLinus Torvalds 					qualifier = 'H';
29591da177e4SLinus Torvalds 					fmt++;
29601da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
29611da177e4SLinus Torvalds 					qualifier = 'L';
29621da177e4SLinus Torvalds 					fmt++;
29631da177e4SLinus Torvalds 				}
29641da177e4SLinus Torvalds 			}
29651da177e4SLinus Torvalds 		}
29661da177e4SLinus Torvalds 
2967da99075cSJan Beulich 		if (!*fmt)
2968da99075cSJan Beulich 			break;
2969da99075cSJan Beulich 
2970da99075cSJan Beulich 		if (*fmt == 'n') {
2971da99075cSJan Beulich 			/* return number of characters read so far */
2972da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
2973da99075cSJan Beulich 			++fmt;
2974da99075cSJan Beulich 			continue;
2975da99075cSJan Beulich 		}
2976da99075cSJan Beulich 
2977da99075cSJan Beulich 		if (!*str)
29781da177e4SLinus Torvalds 			break;
29791da177e4SLinus Torvalds 
2980d4be151bSAndré Goddard Rosa 		base = 10;
29813f623ebaSFabian Frederick 		is_sign = false;
2982d4be151bSAndré Goddard Rosa 
29831da177e4SLinus Torvalds 		switch (*fmt++) {
29841da177e4SLinus Torvalds 		case 'c':
29851da177e4SLinus Torvalds 		{
29861da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
29871da177e4SLinus Torvalds 			if (field_width == -1)
29881da177e4SLinus Torvalds 				field_width = 1;
29891da177e4SLinus Torvalds 			do {
29901da177e4SLinus Torvalds 				*s++ = *str++;
29911da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
29921da177e4SLinus Torvalds 			num++;
29931da177e4SLinus Torvalds 		}
29941da177e4SLinus Torvalds 		continue;
29951da177e4SLinus Torvalds 		case 's':
29961da177e4SLinus Torvalds 		{
29971da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
29981da177e4SLinus Torvalds 			if (field_width == -1)
29994be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
30001da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
3001e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 			/* now copy until next white space */
30047b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
30051da177e4SLinus Torvalds 				*s++ = *str++;
30061da177e4SLinus Torvalds 			*s = '\0';
30071da177e4SLinus Torvalds 			num++;
30081da177e4SLinus Torvalds 		}
30091da177e4SLinus Torvalds 		continue;
3010f9310b2fSJessica Yu 		/*
3011f9310b2fSJessica Yu 		 * Warning: This implementation of the '[' conversion specifier
3012f9310b2fSJessica Yu 		 * deviates from its glibc counterpart in the following ways:
3013f9310b2fSJessica Yu 		 * (1) It does NOT support ranges i.e. '-' is NOT a special
3014f9310b2fSJessica Yu 		 *     character
3015f9310b2fSJessica Yu 		 * (2) It cannot match the closing bracket ']' itself
3016f9310b2fSJessica Yu 		 * (3) A field width is required
3017f9310b2fSJessica Yu 		 * (4) '%*[' (discard matching input) is currently not supported
3018f9310b2fSJessica Yu 		 *
3019f9310b2fSJessica Yu 		 * Example usage:
3020f9310b2fSJessica Yu 		 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3021f9310b2fSJessica Yu 		 *		buf1, buf2, buf3);
3022f9310b2fSJessica Yu 		 * if (ret < 3)
3023f9310b2fSJessica Yu 		 *    // etc..
3024f9310b2fSJessica Yu 		 */
3025f9310b2fSJessica Yu 		case '[':
3026f9310b2fSJessica Yu 		{
3027f9310b2fSJessica Yu 			char *s = (char *)va_arg(args, char *);
3028f9310b2fSJessica Yu 			DECLARE_BITMAP(set, 256) = {0};
3029f9310b2fSJessica Yu 			unsigned int len = 0;
3030f9310b2fSJessica Yu 			bool negate = (*fmt == '^');
3031f9310b2fSJessica Yu 
3032f9310b2fSJessica Yu 			/* field width is required */
3033f9310b2fSJessica Yu 			if (field_width == -1)
3034f9310b2fSJessica Yu 				return num;
3035f9310b2fSJessica Yu 
3036f9310b2fSJessica Yu 			if (negate)
3037f9310b2fSJessica Yu 				++fmt;
3038f9310b2fSJessica Yu 
3039f9310b2fSJessica Yu 			for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3040f9310b2fSJessica Yu 				set_bit((u8)*fmt, set);
3041f9310b2fSJessica Yu 
3042f9310b2fSJessica Yu 			/* no ']' or no character set found */
3043f9310b2fSJessica Yu 			if (!*fmt || !len)
3044f9310b2fSJessica Yu 				return num;
3045f9310b2fSJessica Yu 			++fmt;
3046f9310b2fSJessica Yu 
3047f9310b2fSJessica Yu 			if (negate) {
3048f9310b2fSJessica Yu 				bitmap_complement(set, set, 256);
3049f9310b2fSJessica Yu 				/* exclude null '\0' byte */
3050f9310b2fSJessica Yu 				clear_bit(0, set);
3051f9310b2fSJessica Yu 			}
3052f9310b2fSJessica Yu 
3053f9310b2fSJessica Yu 			/* match must be non-empty */
3054f9310b2fSJessica Yu 			if (!test_bit((u8)*str, set))
3055f9310b2fSJessica Yu 				return num;
3056f9310b2fSJessica Yu 
3057f9310b2fSJessica Yu 			while (test_bit((u8)*str, set) && field_width--)
3058f9310b2fSJessica Yu 				*s++ = *str++;
3059f9310b2fSJessica Yu 			*s = '\0';
3060f9310b2fSJessica Yu 			++num;
3061f9310b2fSJessica Yu 		}
3062f9310b2fSJessica Yu 		continue;
30631da177e4SLinus Torvalds 		case 'o':
30641da177e4SLinus Torvalds 			base = 8;
30651da177e4SLinus Torvalds 			break;
30661da177e4SLinus Torvalds 		case 'x':
30671da177e4SLinus Torvalds 		case 'X':
30681da177e4SLinus Torvalds 			base = 16;
30691da177e4SLinus Torvalds 			break;
30701da177e4SLinus Torvalds 		case 'i':
30711da177e4SLinus Torvalds 			base = 0;
30721da177e4SLinus Torvalds 		case 'd':
30733f623ebaSFabian Frederick 			is_sign = true;
30741da177e4SLinus Torvalds 		case 'u':
30751da177e4SLinus Torvalds 			break;
30761da177e4SLinus Torvalds 		case '%':
30771da177e4SLinus Torvalds 			/* looking for '%' in str */
30781da177e4SLinus Torvalds 			if (*str++ != '%')
30791da177e4SLinus Torvalds 				return num;
30801da177e4SLinus Torvalds 			continue;
30811da177e4SLinus Torvalds 		default:
30821da177e4SLinus Torvalds 			/* invalid format; stop here */
30831da177e4SLinus Torvalds 			return num;
30841da177e4SLinus Torvalds 		}
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds 		/* have some sort of integer conversion.
30871da177e4SLinus Torvalds 		 * first, skip white space in buffer.
30881da177e4SLinus Torvalds 		 */
3089e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
30901da177e4SLinus Torvalds 
30911da177e4SLinus Torvalds 		digit = *str;
30921da177e4SLinus Torvalds 		if (is_sign && digit == '-')
30931da177e4SLinus Torvalds 			digit = *(str + 1);
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds 		if (!digit
30961da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
30971da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
30981da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
30991da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
31001da177e4SLinus Torvalds 			break;
31011da177e4SLinus Torvalds 
310253809751SJan Beulich 		if (is_sign)
310353809751SJan Beulich 			val.s = qualifier != 'L' ?
310453809751SJan Beulich 				simple_strtol(str, &next, base) :
310553809751SJan Beulich 				simple_strtoll(str, &next, base);
310653809751SJan Beulich 		else
310753809751SJan Beulich 			val.u = qualifier != 'L' ?
310853809751SJan Beulich 				simple_strtoul(str, &next, base) :
310953809751SJan Beulich 				simple_strtoull(str, &next, base);
311053809751SJan Beulich 
311153809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
311253809751SJan Beulich 			if (base == 0)
311353809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
311453809751SJan Beulich 			while (next - str > field_width) {
311553809751SJan Beulich 				if (is_sign)
311653809751SJan Beulich 					val.s = div_s64(val.s, base);
311753809751SJan Beulich 				else
311853809751SJan Beulich 					val.u = div_u64(val.u, base);
311953809751SJan Beulich 				--next;
312053809751SJan Beulich 			}
312153809751SJan Beulich 		}
312253809751SJan Beulich 
31231da177e4SLinus Torvalds 		switch (qualifier) {
31241da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
312553809751SJan Beulich 			if (is_sign)
312653809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
312753809751SJan Beulich 			else
312853809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
31291da177e4SLinus Torvalds 			break;
31301da177e4SLinus Torvalds 		case 'h':
313153809751SJan Beulich 			if (is_sign)
313253809751SJan Beulich 				*va_arg(args, short *) = val.s;
313353809751SJan Beulich 			else
313453809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
31351da177e4SLinus Torvalds 			break;
31361da177e4SLinus Torvalds 		case 'l':
313753809751SJan Beulich 			if (is_sign)
313853809751SJan Beulich 				*va_arg(args, long *) = val.s;
313953809751SJan Beulich 			else
314053809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
31411da177e4SLinus Torvalds 			break;
31421da177e4SLinus Torvalds 		case 'L':
314353809751SJan Beulich 			if (is_sign)
314453809751SJan Beulich 				*va_arg(args, long long *) = val.s;
314553809751SJan Beulich 			else
314653809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
31471da177e4SLinus Torvalds 			break;
31481da177e4SLinus Torvalds 		case 'z':
314953809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
31501da177e4SLinus Torvalds 			break;
31511da177e4SLinus Torvalds 		default:
315253809751SJan Beulich 			if (is_sign)
315353809751SJan Beulich 				*va_arg(args, int *) = val.s;
315453809751SJan Beulich 			else
315553809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
31561da177e4SLinus Torvalds 			break;
31571da177e4SLinus Torvalds 		}
31581da177e4SLinus Torvalds 		num++;
31591da177e4SLinus Torvalds 
31601da177e4SLinus Torvalds 		if (!next)
31611da177e4SLinus Torvalds 			break;
31621da177e4SLinus Torvalds 		str = next;
31631da177e4SLinus Torvalds 	}
3164c6b40d16SJohannes Berg 
31651da177e4SLinus Torvalds 	return num;
31661da177e4SLinus Torvalds }
31671da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
31681da177e4SLinus Torvalds 
31691da177e4SLinus Torvalds /**
31701da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
31711da177e4SLinus Torvalds  * @buf:	input buffer
31721da177e4SLinus Torvalds  * @fmt:	formatting of buffer
31731da177e4SLinus Torvalds  * @...:	resulting arguments
31741da177e4SLinus Torvalds  */
31751da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
31761da177e4SLinus Torvalds {
31771da177e4SLinus Torvalds 	va_list args;
31781da177e4SLinus Torvalds 	int i;
31791da177e4SLinus Torvalds 
31801da177e4SLinus Torvalds 	va_start(args, fmt);
31811da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
31821da177e4SLinus Torvalds 	va_end(args);
31837b9186f5SAndré Goddard Rosa 
31841da177e4SLinus Torvalds 	return i;
31851da177e4SLinus Torvalds }
31861da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
3187