xref: /openbmc/linux/lib/vsprintf.c (revision 4d42c44727a062e233e446c6c86da1c84d762d79)
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>
33*4d42c447SAndy Shevchenko #include <linux/rtc.h>
342b1b0d66SAndy Shevchenko #include <linux/uuid.h>
35ce4fecf1SPantelis Antoniou #include <linux/of.h>
368a27f7c9SJoe Perches #include <net/addrconf.h>
37ad67b74dSTobin C. Harding #include <linux/siphash.h>
38ad67b74dSTobin C. Harding #include <linux/compiler.h>
391031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
401031bc58SDmitry Monakhov #include <linux/blkdev.h>
411031bc58SDmitry Monakhov #endif
421da177e4SLinus Torvalds 
43edf14cdbSVlastimil Babka #include "../mm/internal.h"	/* For the trace_print_flags arrays */
44edf14cdbSVlastimil Babka 
454e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
467c43d9a3SRasmus Villemoes #include <asm/byteorder.h>	/* cpu_to_le16 */
471da177e4SLinus Torvalds 
4871dca95dSAndy Shevchenko #include <linux/string_helpers.h>
491dff46d6SAlexey Dobriyan #include "kstrtox.h"
50aa46a63eSHarvey Harrison 
511da177e4SLinus Torvalds /**
521da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
531da177e4SLinus Torvalds  * @cp: The start of the string
541da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
551da177e4SLinus Torvalds  * @base: The number base to use
56462e4711SEldad Zack  *
57462e4711SEldad Zack  * This function is obsolete. Please use kstrtoull instead.
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
601da177e4SLinus Torvalds {
611dff46d6SAlexey Dobriyan 	unsigned long long result;
621dff46d6SAlexey Dobriyan 	unsigned int rv;
631da177e4SLinus Torvalds 
641dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
651dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
661dff46d6SAlexey Dobriyan 	/* FIXME */
671dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
68aa46a63eSHarvey Harrison 
691da177e4SLinus Torvalds 	if (endp)
701da177e4SLinus Torvalds 		*endp = (char *)cp;
717b9186f5SAndré Goddard Rosa 
721da177e4SLinus Torvalds 	return result;
731da177e4SLinus Torvalds }
741da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /**
77922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
78922ac25cSAndré Goddard Rosa  * @cp: The start of the string
79922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
80922ac25cSAndré Goddard Rosa  * @base: The number base to use
81462e4711SEldad Zack  *
82462e4711SEldad Zack  * This function is obsolete. Please use kstrtoul instead.
83922ac25cSAndré Goddard Rosa  */
84922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
85922ac25cSAndré Goddard Rosa {
86922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
87922ac25cSAndré Goddard Rosa }
88922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
89922ac25cSAndré Goddard Rosa 
90922ac25cSAndré Goddard Rosa /**
91922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
92922ac25cSAndré Goddard Rosa  * @cp: The start of the string
93922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
94922ac25cSAndré Goddard Rosa  * @base: The number base to use
95462e4711SEldad Zack  *
96462e4711SEldad Zack  * This function is obsolete. Please use kstrtol instead.
97922ac25cSAndré Goddard Rosa  */
98922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
99922ac25cSAndré Goddard Rosa {
100922ac25cSAndré Goddard Rosa 	if (*cp == '-')
101922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
102922ac25cSAndré Goddard Rosa 
103922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
104922ac25cSAndré Goddard Rosa }
105922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
106922ac25cSAndré Goddard Rosa 
107922ac25cSAndré Goddard Rosa /**
1081da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
1091da177e4SLinus Torvalds  * @cp: The start of the string
1101da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1111da177e4SLinus Torvalds  * @base: The number base to use
112462e4711SEldad Zack  *
113462e4711SEldad Zack  * This function is obsolete. Please use kstrtoll instead.
1141da177e4SLinus Torvalds  */
1151da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	if (*cp == '-')
1181da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1197b9186f5SAndré Goddard Rosa 
1201da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1211da177e4SLinus Torvalds }
12298d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1231da177e4SLinus Torvalds 
124cf3b429bSJoe Perches static noinline_for_stack
125cf3b429bSJoe Perches int skip_atoi(const char **s)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	int i = 0;
1281da177e4SLinus Torvalds 
12943e5b666SRasmus Villemoes 	do {
1301da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
13143e5b666SRasmus Villemoes 	} while (isdigit(**s));
1327b9186f5SAndré Goddard Rosa 
1331da177e4SLinus Torvalds 	return i;
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
1367c43d9a3SRasmus Villemoes /*
1377c43d9a3SRasmus Villemoes  * Decimal conversion is by far the most typical, and is used for
1387c43d9a3SRasmus Villemoes  * /proc and /sys data. This directly impacts e.g. top performance
1397c43d9a3SRasmus Villemoes  * with many processes running. We optimize it for speed by emitting
1407c43d9a3SRasmus Villemoes  * two characters at a time, using a 200 byte lookup table. This
1417c43d9a3SRasmus Villemoes  * roughly halves the number of multiplications compared to computing
1427c43d9a3SRasmus Villemoes  * the digits one at a time. Implementation strongly inspired by the
1437c43d9a3SRasmus Villemoes  * previous version, which in turn used ideas described at
1447c43d9a3SRasmus Villemoes  * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
1457c43d9a3SRasmus Villemoes  * from the author, Douglas W. Jones).
1467c43d9a3SRasmus Villemoes  *
1477c43d9a3SRasmus Villemoes  * It turns out there is precisely one 26 bit fixed-point
1487c43d9a3SRasmus Villemoes  * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
1497c43d9a3SRasmus Villemoes  * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
1507c43d9a3SRasmus Villemoes  * range happens to be somewhat larger (x <= 1073741898), but that's
1517c43d9a3SRasmus Villemoes  * irrelevant for our purpose.
1527c43d9a3SRasmus Villemoes  *
1537c43d9a3SRasmus Villemoes  * For dividing a number in the range [10^4, 10^6-1] by 100, we still
1547c43d9a3SRasmus Villemoes  * need a 32x32->64 bit multiply, so we simply use the same constant.
1557c43d9a3SRasmus Villemoes  *
1567c43d9a3SRasmus Villemoes  * For dividing a number in the range [100, 10^4-1] by 100, there are
1577c43d9a3SRasmus Villemoes  * several options. The simplest is (x * 0x147b) >> 19, which is valid
1587c43d9a3SRasmus Villemoes  * for all x <= 43698.
159133fd9f5SDenys Vlasenko  */
1604277eeddSDenis Vlasenko 
1617c43d9a3SRasmus Villemoes static const u16 decpair[100] = {
1627c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
1637c43d9a3SRasmus Villemoes 	_( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
1647c43d9a3SRasmus Villemoes 	_(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
1657c43d9a3SRasmus Villemoes 	_(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
1667c43d9a3SRasmus Villemoes 	_(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
1677c43d9a3SRasmus Villemoes 	_(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
1687c43d9a3SRasmus Villemoes 	_(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
1697c43d9a3SRasmus Villemoes 	_(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
1707c43d9a3SRasmus Villemoes 	_(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
1717c43d9a3SRasmus Villemoes 	_(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
1727c43d9a3SRasmus Villemoes 	_(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
1737c43d9a3SRasmus Villemoes #undef _
1747c43d9a3SRasmus Villemoes };
1754277eeddSDenis Vlasenko 
1767b9186f5SAndré Goddard Rosa /*
1777c43d9a3SRasmus Villemoes  * This will print a single '0' even if r == 0, since we would
178675cf53cSRasmus Villemoes  * immediately jump to out_r where two 0s would be written but only
179675cf53cSRasmus Villemoes  * one of them accounted for in buf. This is needed by ip4_string
180675cf53cSRasmus Villemoes  * below. All other callers pass a non-zero value of r.
181133fd9f5SDenys Vlasenko */
182133fd9f5SDenys Vlasenko static noinline_for_stack
183133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
184133fd9f5SDenys Vlasenko {
185133fd9f5SDenys Vlasenko 	unsigned q;
1864277eeddSDenis Vlasenko 
1877c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^8 */
1887c43d9a3SRasmus Villemoes 	if (r < 100)
1897c43d9a3SRasmus Villemoes 		goto out_r;
190cb239d0aSGeorge Spelvin 
1917c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^8 */
1927c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
1937c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
1947c43d9a3SRasmus Villemoes 	buf += 2;
1957c43d9a3SRasmus Villemoes 
1967c43d9a3SRasmus Villemoes 	/* 1 <= q < 10^6 */
1977c43d9a3SRasmus Villemoes 	if (q < 100)
1987c43d9a3SRasmus Villemoes 		goto out_q;
1997c43d9a3SRasmus Villemoes 
2007c43d9a3SRasmus Villemoes 	/*  100 <= q < 10^6 */
2017c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
2027c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
2037c43d9a3SRasmus Villemoes 	buf += 2;
2047c43d9a3SRasmus Villemoes 
2057c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^4 */
2067c43d9a3SRasmus Villemoes 	if (r < 100)
2077c43d9a3SRasmus Villemoes 		goto out_r;
2087c43d9a3SRasmus Villemoes 
2097c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^4 */
2107c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2117c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2127c43d9a3SRasmus Villemoes 	buf += 2;
2137c43d9a3SRasmus Villemoes out_q:
2147c43d9a3SRasmus Villemoes 	/* 1 <= q < 100 */
2157c43d9a3SRasmus Villemoes 	r = q;
2167c43d9a3SRasmus Villemoes out_r:
2177c43d9a3SRasmus Villemoes 	/* 1 <= r < 100 */
2187c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r];
219675cf53cSRasmus Villemoes 	buf += r < 10 ? 1 : 2;
220133fd9f5SDenys Vlasenko 	return buf;
221133fd9f5SDenys Vlasenko }
222133fd9f5SDenys Vlasenko 
2237c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
2247c43d9a3SRasmus Villemoes static noinline_for_stack
2257c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r)
2267c43d9a3SRasmus Villemoes {
2277c43d9a3SRasmus Villemoes 	unsigned q;
228133fd9f5SDenys Vlasenko 
2297c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^8 */
2307c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
2317c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2327c43d9a3SRasmus Villemoes 	buf += 2;
233133fd9f5SDenys Vlasenko 
2347c43d9a3SRasmus Villemoes 	/* 0 <= q < 10^6 */
2357c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
2367c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
2377c43d9a3SRasmus Villemoes 	buf += 2;
238133fd9f5SDenys Vlasenko 
2397c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2407c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2417c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2427c43d9a3SRasmus Villemoes 	buf += 2;
2437c43d9a3SRasmus Villemoes 
2447c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2457c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2467c43d9a3SRasmus Villemoes 	buf += 2;
2477c43d9a3SRasmus Villemoes 	return buf;
2487c43d9a3SRasmus Villemoes }
2497c43d9a3SRasmus Villemoes 
2507c43d9a3SRasmus Villemoes static noinline_for_stack
251133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
252133fd9f5SDenys Vlasenko {
253133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000)
2547c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2557c43d9a3SRasmus Villemoes 	/* 1 <= n <= 1.6e11 */
2567c43d9a3SRasmus Villemoes 	if (n >= 100*1000*1000)
2577c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2587c43d9a3SRasmus Villemoes 	/* 1 <= n < 1e8 */
259133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
260133fd9f5SDenys Vlasenko }
261133fd9f5SDenys Vlasenko 
2627c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
263133fd9f5SDenys Vlasenko 
2647c43d9a3SRasmus Villemoes static void
2657c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r)
266133fd9f5SDenys Vlasenko {
2677c43d9a3SRasmus Villemoes 	unsigned q;
2687c43d9a3SRasmus Villemoes 
2697c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2707c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2717c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2727c43d9a3SRasmus Villemoes 	buf += 2;
2737c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2747c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2752359172aSGeorge Spelvin }
2762359172aSGeorge Spelvin 
2772359172aSGeorge Spelvin /*
2782359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2792359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2802359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2812359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
2827c43d9a3SRasmus Villemoes  * (second call in the put_dec code, assuming n is all-ones).
2832359172aSGeorge Spelvin  */
2847c43d9a3SRasmus Villemoes static noinline_for_stack
2852359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2862359172aSGeorge Spelvin {
2872359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2882359172aSGeorge Spelvin 
2892359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2902359172aSGeorge Spelvin         return q;
291133fd9f5SDenys Vlasenko }
292133fd9f5SDenys Vlasenko 
293133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
294133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
295133fd9f5SDenys Vlasenko  * (with permission from the author).
296133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
297133fd9f5SDenys Vlasenko  */
298133fd9f5SDenys Vlasenko static
299133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
300133fd9f5SDenys Vlasenko {
301133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
302133fd9f5SDenys Vlasenko 
303133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
304133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
305133fd9f5SDenys Vlasenko 
306133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
307133fd9f5SDenys Vlasenko 	h   = (n >> 32);
308133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
309133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
310133fd9f5SDenys Vlasenko 
3117c43d9a3SRasmus Villemoes 	/* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
3127c43d9a3SRasmus Villemoes 	     = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
313133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
3142359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
315133fd9f5SDenys Vlasenko 
3162359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
3172359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
318133fd9f5SDenys Vlasenko 
3192359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
3202359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
321133fd9f5SDenys Vlasenko 
3222359172aSGeorge Spelvin 	q += 281 * d3;
3232359172aSGeorge Spelvin 	buf += 12;
3242359172aSGeorge Spelvin 	if (q)
3252359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3262359172aSGeorge Spelvin 	else while (buf[-1] == '0')
327133fd9f5SDenys Vlasenko 		--buf;
3287b9186f5SAndré Goddard Rosa 
3294277eeddSDenis Vlasenko 	return buf;
3304277eeddSDenis Vlasenko }
331133fd9f5SDenys Vlasenko 
332133fd9f5SDenys Vlasenko #endif
3334277eeddSDenis Vlasenko 
3341ac101a5SKAMEZAWA Hiroyuki /*
3351ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3361ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3371ac101a5SKAMEZAWA Hiroyuki  *
3381ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3391ac101a5SKAMEZAWA Hiroyuki  */
340d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
3411ac101a5SKAMEZAWA Hiroyuki {
3427c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
3437c43d9a3SRasmus Villemoes 	char tmp[sizeof(num) * 3] __aligned(2);
3441ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3451ac101a5SKAMEZAWA Hiroyuki 
346133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
347133fd9f5SDenys Vlasenko 	if (num <= 9) {
348133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
349133fd9f5SDenys Vlasenko 		len = 1;
350133fd9f5SDenys Vlasenko 	} else {
3511ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
352133fd9f5SDenys Vlasenko 	}
3531ac101a5SKAMEZAWA Hiroyuki 
354d1be35cbSAndrei Vagin 	if (len > size || width > size)
3551ac101a5SKAMEZAWA Hiroyuki 		return 0;
356d1be35cbSAndrei Vagin 
357d1be35cbSAndrei Vagin 	if (width > len) {
358d1be35cbSAndrei Vagin 		width = width - len;
359d1be35cbSAndrei Vagin 		for (idx = 0; idx < width; idx++)
360d1be35cbSAndrei Vagin 			buf[idx] = ' ';
361d1be35cbSAndrei Vagin 	} else {
362d1be35cbSAndrei Vagin 		width = 0;
363d1be35cbSAndrei Vagin 	}
364d1be35cbSAndrei Vagin 
3651ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
366d1be35cbSAndrei Vagin 		buf[idx + width] = tmp[len - idx - 1];
367d1be35cbSAndrei Vagin 
368d1be35cbSAndrei Vagin 	return len + width;
3691ac101a5SKAMEZAWA Hiroyuki }
3701ac101a5SKAMEZAWA Hiroyuki 
37151be17dfSRasmus Villemoes #define SIGN	1		/* unsigned/signed, must be 1 */
372d1c1b121SRasmus Villemoes #define LEFT	2		/* left justified */
3731da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3741da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
375d1c1b121SRasmus Villemoes #define ZEROPAD	16		/* pad with zero, must be 16 == '0' - ' ' */
376b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
377b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3781da177e4SLinus Torvalds 
379fef20d9cSFrederic Weisbecker enum format_type {
380fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
381ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
382fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
383fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
384fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
385fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
386fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
387fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
388fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
389fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
390fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
391a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
392a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
393fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
394fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
395fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
396fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
397fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
398fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
399fef20d9cSFrederic Weisbecker };
400fef20d9cSFrederic Weisbecker 
401fef20d9cSFrederic Weisbecker struct printf_spec {
402d0484193SRasmus Villemoes 	unsigned int	type:8;		/* format_type enum */
403d0484193SRasmus Villemoes 	signed int	field_width:24;	/* width of output field */
404d0484193SRasmus Villemoes 	unsigned int	flags:8;	/* flags to number() */
405d0484193SRasmus Villemoes 	unsigned int	base:8;		/* number base, 8, 10 or 16 only */
406d0484193SRasmus Villemoes 	signed int	precision:16;	/* # of digits/chars */
407d0484193SRasmus Villemoes } __packed;
4084d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1)
4094d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1)
410fef20d9cSFrederic Weisbecker 
411cf3b429bSJoe Perches static noinline_for_stack
412cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
413fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
4141da177e4SLinus Torvalds {
4157c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
4167c43d9a3SRasmus Villemoes 	char tmp[3 * sizeof(num)] __aligned(2);
4179b706aeeSDenys Vlasenko 	char sign;
4189b706aeeSDenys Vlasenko 	char locase;
419fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4201da177e4SLinus Torvalds 	int i;
4217c203422SPierre Carrier 	bool is_zero = num == 0LL;
4221c7a8e62SRasmus Villemoes 	int field_width = spec.field_width;
4231c7a8e62SRasmus Villemoes 	int precision = spec.precision;
4241da177e4SLinus Torvalds 
425d0484193SRasmus Villemoes 	BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
426d0484193SRasmus Villemoes 
4279b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4289b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
429fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
430fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
431fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4321da177e4SLinus Torvalds 	sign = 0;
433fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4341da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4351da177e4SLinus Torvalds 			sign = '-';
4361da177e4SLinus Torvalds 			num = -(signed long long)num;
4371c7a8e62SRasmus Villemoes 			field_width--;
438fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4391da177e4SLinus Torvalds 			sign = '+';
4401c7a8e62SRasmus Villemoes 			field_width--;
441fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4421da177e4SLinus Torvalds 			sign = ' ';
4431c7a8e62SRasmus Villemoes 			field_width--;
4441da177e4SLinus Torvalds 		}
4451da177e4SLinus Torvalds 	}
446b39a7340SDenis Vlasenko 	if (need_pfx) {
447fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4481c7a8e62SRasmus Villemoes 			field_width -= 2;
4497c203422SPierre Carrier 		else if (!is_zero)
4501c7a8e62SRasmus Villemoes 			field_width--;
4511da177e4SLinus Torvalds 	}
452b39a7340SDenis Vlasenko 
453b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4541da177e4SLinus Torvalds 	i = 0;
455133fd9f5SDenys Vlasenko 	if (num < spec.base)
4563ea8d440SRasmus Villemoes 		tmp[i++] = hex_asc_upper[num] | locase;
457fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
458fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
459b39a7340SDenis Vlasenko 		int shift = 3;
4607b9186f5SAndré Goddard Rosa 
4617b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4627b9186f5SAndré Goddard Rosa 			shift = 4;
463b39a7340SDenis Vlasenko 		do {
4643ea8d440SRasmus Villemoes 			tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
465b39a7340SDenis Vlasenko 			num >>= shift;
466b39a7340SDenis Vlasenko 		} while (num);
4674277eeddSDenis Vlasenko 	} else { /* base 10 */
4684277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4694277eeddSDenis Vlasenko 	}
470b39a7340SDenis Vlasenko 
471b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
4721c7a8e62SRasmus Villemoes 	if (i > precision)
4731c7a8e62SRasmus Villemoes 		precision = i;
474b39a7340SDenis Vlasenko 	/* leading space padding */
4751c7a8e62SRasmus Villemoes 	field_width -= precision;
47651be17dfSRasmus Villemoes 	if (!(spec.flags & (ZEROPAD | LEFT))) {
4771c7a8e62SRasmus Villemoes 		while (--field_width >= 0) {
478f796937aSJeremy Fitzhardinge 			if (buf < end)
4791da177e4SLinus Torvalds 				*buf = ' ';
4801da177e4SLinus Torvalds 			++buf;
4811da177e4SLinus Torvalds 		}
4821da177e4SLinus Torvalds 	}
483b39a7340SDenis Vlasenko 	/* sign */
4841da177e4SLinus Torvalds 	if (sign) {
485f796937aSJeremy Fitzhardinge 		if (buf < end)
4861da177e4SLinus Torvalds 			*buf = sign;
4871da177e4SLinus Torvalds 		++buf;
4881da177e4SLinus Torvalds 	}
489b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
490b39a7340SDenis Vlasenko 	if (need_pfx) {
4917c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
492f796937aSJeremy Fitzhardinge 			if (buf < end)
4931da177e4SLinus Torvalds 				*buf = '0';
4941da177e4SLinus Torvalds 			++buf;
4957c203422SPierre Carrier 		}
496fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
497f796937aSJeremy Fitzhardinge 			if (buf < end)
4989b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
4991da177e4SLinus Torvalds 			++buf;
5001da177e4SLinus Torvalds 		}
5011da177e4SLinus Torvalds 	}
502b39a7340SDenis Vlasenko 	/* zero or space padding */
503fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
504d1c1b121SRasmus Villemoes 		char c = ' ' + (spec.flags & ZEROPAD);
505d1c1b121SRasmus Villemoes 		BUILD_BUG_ON(' ' + ZEROPAD != '0');
5061c7a8e62SRasmus Villemoes 		while (--field_width >= 0) {
507f796937aSJeremy Fitzhardinge 			if (buf < end)
5081da177e4SLinus Torvalds 				*buf = c;
5091da177e4SLinus Torvalds 			++buf;
5101da177e4SLinus Torvalds 		}
5111da177e4SLinus Torvalds 	}
512b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
5131c7a8e62SRasmus Villemoes 	while (i <= --precision) {
514f796937aSJeremy Fitzhardinge 		if (buf < end)
5151da177e4SLinus Torvalds 			*buf = '0';
5161da177e4SLinus Torvalds 		++buf;
5171da177e4SLinus Torvalds 	}
518b39a7340SDenis Vlasenko 	/* actual digits of result */
519b39a7340SDenis Vlasenko 	while (--i >= 0) {
520f796937aSJeremy Fitzhardinge 		if (buf < end)
5211da177e4SLinus Torvalds 			*buf = tmp[i];
5221da177e4SLinus Torvalds 		++buf;
5231da177e4SLinus Torvalds 	}
524b39a7340SDenis Vlasenko 	/* trailing space padding */
5251c7a8e62SRasmus Villemoes 	while (--field_width >= 0) {
526f796937aSJeremy Fitzhardinge 		if (buf < end)
5271da177e4SLinus Torvalds 			*buf = ' ';
5281da177e4SLinus Torvalds 		++buf;
5291da177e4SLinus Torvalds 	}
5307b9186f5SAndré Goddard Rosa 
5311da177e4SLinus Torvalds 	return buf;
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
5343cab1e71SAndy Shevchenko static noinline_for_stack
5353cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
5363cab1e71SAndy Shevchenko {
5373cab1e71SAndy Shevchenko 	struct printf_spec spec;
5383cab1e71SAndy Shevchenko 
5393cab1e71SAndy Shevchenko 	spec.type = FORMAT_TYPE_PTR;
5403cab1e71SAndy Shevchenko 	spec.field_width = 2 + 2 * size;	/* 0x + hex */
5413cab1e71SAndy Shevchenko 	spec.flags = SPECIAL | SMALL | ZEROPAD;
5423cab1e71SAndy Shevchenko 	spec.base = 16;
5433cab1e71SAndy Shevchenko 	spec.precision = -1;
5443cab1e71SAndy Shevchenko 
5453cab1e71SAndy Shevchenko 	return number(buf, end, num, spec);
5463cab1e71SAndy Shevchenko }
5473cab1e71SAndy Shevchenko 
548cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
5494b6ccca7SAl Viro {
5504b6ccca7SAl Viro 	size_t size;
5514b6ccca7SAl Viro 	if (buf >= end)	/* nowhere to put anything */
5524b6ccca7SAl Viro 		return;
5534b6ccca7SAl Viro 	size = end - buf;
5544b6ccca7SAl Viro 	if (size <= spaces) {
5554b6ccca7SAl Viro 		memset(buf, ' ', size);
5564b6ccca7SAl Viro 		return;
5574b6ccca7SAl Viro 	}
5584b6ccca7SAl Viro 	if (len) {
5594b6ccca7SAl Viro 		if (len > size - spaces)
5604b6ccca7SAl Viro 			len = size - spaces;
5614b6ccca7SAl Viro 		memmove(buf + spaces, buf, len);
5624b6ccca7SAl Viro 	}
5634b6ccca7SAl Viro 	memset(buf, ' ', spaces);
5644b6ccca7SAl Viro }
5654b6ccca7SAl Viro 
566cfccde04SRasmus Villemoes /*
567cfccde04SRasmus Villemoes  * Handle field width padding for a string.
568cfccde04SRasmus Villemoes  * @buf: current buffer position
569cfccde04SRasmus Villemoes  * @n: length of string
570cfccde04SRasmus Villemoes  * @end: end of output buffer
571cfccde04SRasmus Villemoes  * @spec: for field width and flags
572cfccde04SRasmus Villemoes  * Returns: new buffer position after padding.
573cfccde04SRasmus Villemoes  */
574cfccde04SRasmus Villemoes static noinline_for_stack
575cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
576cfccde04SRasmus Villemoes {
577cfccde04SRasmus Villemoes 	unsigned spaces;
578cfccde04SRasmus Villemoes 
579cfccde04SRasmus Villemoes 	if (likely(n >= spec.field_width))
580cfccde04SRasmus Villemoes 		return buf;
581cfccde04SRasmus Villemoes 	/* we want to pad the sucker */
582cfccde04SRasmus Villemoes 	spaces = spec.field_width - n;
583cfccde04SRasmus Villemoes 	if (!(spec.flags & LEFT)) {
584cfccde04SRasmus Villemoes 		move_right(buf - n, end, n, spaces);
585cfccde04SRasmus Villemoes 		return buf + spaces;
586cfccde04SRasmus Villemoes 	}
587cfccde04SRasmus Villemoes 	while (spaces--) {
588cfccde04SRasmus Villemoes 		if (buf < end)
589cfccde04SRasmus Villemoes 			*buf = ' ';
590cfccde04SRasmus Villemoes 		++buf;
591cfccde04SRasmus Villemoes 	}
592cfccde04SRasmus Villemoes 	return buf;
593cfccde04SRasmus Villemoes }
594cfccde04SRasmus Villemoes 
5954b6ccca7SAl Viro static noinline_for_stack
59695508cfaSRasmus Villemoes char *string(char *buf, char *end, const char *s, struct printf_spec spec)
59795508cfaSRasmus Villemoes {
59834fc8b90SRasmus Villemoes 	int len = 0;
59934fc8b90SRasmus Villemoes 	size_t lim = spec.precision;
60095508cfaSRasmus Villemoes 
60195508cfaSRasmus Villemoes 	if ((unsigned long)s < PAGE_SIZE)
60295508cfaSRasmus Villemoes 		s = "(null)";
60395508cfaSRasmus Villemoes 
60434fc8b90SRasmus Villemoes 	while (lim--) {
60534fc8b90SRasmus Villemoes 		char c = *s++;
60634fc8b90SRasmus Villemoes 		if (!c)
60734fc8b90SRasmus Villemoes 			break;
60895508cfaSRasmus Villemoes 		if (buf < end)
60934fc8b90SRasmus Villemoes 			*buf = c;
61095508cfaSRasmus Villemoes 		++buf;
61134fc8b90SRasmus Villemoes 		++len;
61295508cfaSRasmus Villemoes 	}
61334fc8b90SRasmus Villemoes 	return widen_string(buf, len, end, spec);
61495508cfaSRasmus Villemoes }
61595508cfaSRasmus Villemoes 
61695508cfaSRasmus Villemoes static noinline_for_stack
6179073dac1SGeert Uytterhoeven char *pointer_string(char *buf, char *end, const void *ptr,
6189073dac1SGeert Uytterhoeven 		     struct printf_spec spec)
6199073dac1SGeert Uytterhoeven {
6209073dac1SGeert Uytterhoeven 	spec.base = 16;
6219073dac1SGeert Uytterhoeven 	spec.flags |= SMALL;
6229073dac1SGeert Uytterhoeven 	if (spec.field_width == -1) {
6239073dac1SGeert Uytterhoeven 		spec.field_width = 2 * sizeof(ptr);
6249073dac1SGeert Uytterhoeven 		spec.flags |= ZEROPAD;
6259073dac1SGeert Uytterhoeven 	}
6269073dac1SGeert Uytterhoeven 
6279073dac1SGeert Uytterhoeven 	return number(buf, end, (unsigned long int)ptr, spec);
6289073dac1SGeert Uytterhoeven }
6299073dac1SGeert Uytterhoeven 
6309073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */
6319073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init;
6329073dac1SGeert Uytterhoeven 
6339073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str)
6349073dac1SGeert Uytterhoeven {
6359073dac1SGeert Uytterhoeven 	debug_boot_weak_hash = 1;
6369073dac1SGeert Uytterhoeven 	pr_info("debug_boot_weak_hash enabled\n");
6379073dac1SGeert Uytterhoeven 	return 0;
6389073dac1SGeert Uytterhoeven }
6399073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
6409073dac1SGeert Uytterhoeven 
6419073dac1SGeert Uytterhoeven static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
6429073dac1SGeert Uytterhoeven static siphash_key_t ptr_key __read_mostly;
6439073dac1SGeert Uytterhoeven 
6449073dac1SGeert Uytterhoeven static void enable_ptr_key_workfn(struct work_struct *work)
6459073dac1SGeert Uytterhoeven {
6469073dac1SGeert Uytterhoeven 	get_random_bytes(&ptr_key, sizeof(ptr_key));
6479073dac1SGeert Uytterhoeven 	/* Needs to run from preemptible context */
6489073dac1SGeert Uytterhoeven 	static_branch_disable(&not_filled_random_ptr_key);
6499073dac1SGeert Uytterhoeven }
6509073dac1SGeert Uytterhoeven 
6519073dac1SGeert Uytterhoeven static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
6529073dac1SGeert Uytterhoeven 
6539073dac1SGeert Uytterhoeven static void fill_random_ptr_key(struct random_ready_callback *unused)
6549073dac1SGeert Uytterhoeven {
6559073dac1SGeert Uytterhoeven 	/* This may be in an interrupt handler. */
6569073dac1SGeert Uytterhoeven 	queue_work(system_unbound_wq, &enable_ptr_key_work);
6579073dac1SGeert Uytterhoeven }
6589073dac1SGeert Uytterhoeven 
6599073dac1SGeert Uytterhoeven static struct random_ready_callback random_ready = {
6609073dac1SGeert Uytterhoeven 	.func = fill_random_ptr_key
6619073dac1SGeert Uytterhoeven };
6629073dac1SGeert Uytterhoeven 
6639073dac1SGeert Uytterhoeven static int __init initialize_ptr_random(void)
6649073dac1SGeert Uytterhoeven {
6659073dac1SGeert Uytterhoeven 	int key_size = sizeof(ptr_key);
6669073dac1SGeert Uytterhoeven 	int ret;
6679073dac1SGeert Uytterhoeven 
6689073dac1SGeert Uytterhoeven 	/* Use hw RNG if available. */
6699073dac1SGeert Uytterhoeven 	if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
6709073dac1SGeert Uytterhoeven 		static_branch_disable(&not_filled_random_ptr_key);
6719073dac1SGeert Uytterhoeven 		return 0;
6729073dac1SGeert Uytterhoeven 	}
6739073dac1SGeert Uytterhoeven 
6749073dac1SGeert Uytterhoeven 	ret = add_random_ready_callback(&random_ready);
6759073dac1SGeert Uytterhoeven 	if (!ret) {
6769073dac1SGeert Uytterhoeven 		return 0;
6779073dac1SGeert Uytterhoeven 	} else if (ret == -EALREADY) {
6789073dac1SGeert Uytterhoeven 		/* This is in preemptible context */
6799073dac1SGeert Uytterhoeven 		enable_ptr_key_workfn(&enable_ptr_key_work);
6809073dac1SGeert Uytterhoeven 		return 0;
6819073dac1SGeert Uytterhoeven 	}
6829073dac1SGeert Uytterhoeven 
6839073dac1SGeert Uytterhoeven 	return ret;
6849073dac1SGeert Uytterhoeven }
6859073dac1SGeert Uytterhoeven early_initcall(initialize_ptr_random);
6869073dac1SGeert Uytterhoeven 
6879073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */
6889073dac1SGeert Uytterhoeven static char *ptr_to_id(char *buf, char *end, const void *ptr,
6899073dac1SGeert Uytterhoeven 		       struct printf_spec spec)
6909073dac1SGeert Uytterhoeven {
6919073dac1SGeert Uytterhoeven 	const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
6929073dac1SGeert Uytterhoeven 	unsigned long hashval;
6939073dac1SGeert Uytterhoeven 
6949073dac1SGeert Uytterhoeven 	/* When debugging early boot use non-cryptographically secure hash. */
6959073dac1SGeert Uytterhoeven 	if (unlikely(debug_boot_weak_hash)) {
6969073dac1SGeert Uytterhoeven 		hashval = hash_long((unsigned long)ptr, 32);
6979073dac1SGeert Uytterhoeven 		return pointer_string(buf, end, (const void *)hashval, spec);
6989073dac1SGeert Uytterhoeven 	}
6999073dac1SGeert Uytterhoeven 
7009073dac1SGeert Uytterhoeven 	if (static_branch_unlikely(&not_filled_random_ptr_key)) {
7019073dac1SGeert Uytterhoeven 		spec.field_width = 2 * sizeof(ptr);
7029073dac1SGeert Uytterhoeven 		/* string length must be less than default_width */
7039073dac1SGeert Uytterhoeven 		return string(buf, end, str, spec);
7049073dac1SGeert Uytterhoeven 	}
7059073dac1SGeert Uytterhoeven 
7069073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT
7079073dac1SGeert Uytterhoeven 	hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
7089073dac1SGeert Uytterhoeven 	/*
7099073dac1SGeert Uytterhoeven 	 * Mask off the first 32 bits, this makes explicit that we have
7109073dac1SGeert Uytterhoeven 	 * modified the address (and 32 bits is plenty for a unique ID).
7119073dac1SGeert Uytterhoeven 	 */
7129073dac1SGeert Uytterhoeven 	hashval = hashval & 0xffffffff;
7139073dac1SGeert Uytterhoeven #else
7149073dac1SGeert Uytterhoeven 	hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
7159073dac1SGeert Uytterhoeven #endif
7169073dac1SGeert Uytterhoeven 	return pointer_string(buf, end, (const void *)hashval, spec);
7179073dac1SGeert Uytterhoeven }
7189073dac1SGeert Uytterhoeven 
7199073dac1SGeert Uytterhoeven static noinline_for_stack
7204b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
7214b6ccca7SAl Viro 		  const char *fmt)
7224b6ccca7SAl Viro {
7234b6ccca7SAl Viro 	const char *array[4], *s;
7244b6ccca7SAl Viro 	const struct dentry *p;
7254b6ccca7SAl Viro 	int depth;
7264b6ccca7SAl Viro 	int i, n;
7274b6ccca7SAl Viro 
7284b6ccca7SAl Viro 	switch (fmt[1]) {
7294b6ccca7SAl Viro 		case '2': case '3': case '4':
7304b6ccca7SAl Viro 			depth = fmt[1] - '0';
7314b6ccca7SAl Viro 			break;
7324b6ccca7SAl Viro 		default:
7334b6ccca7SAl Viro 			depth = 1;
7344b6ccca7SAl Viro 	}
7354b6ccca7SAl Viro 
7364b6ccca7SAl Viro 	rcu_read_lock();
7374b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
7386aa7de05SMark Rutland 		p = READ_ONCE(d->d_parent);
7396aa7de05SMark Rutland 		array[i] = READ_ONCE(d->d_name.name);
7404b6ccca7SAl Viro 		if (p == d) {
7414b6ccca7SAl Viro 			if (i)
7424b6ccca7SAl Viro 				array[i] = "";
7434b6ccca7SAl Viro 			i++;
7444b6ccca7SAl Viro 			break;
7454b6ccca7SAl Viro 		}
7464b6ccca7SAl Viro 	}
7474b6ccca7SAl Viro 	s = array[--i];
7484b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
7494b6ccca7SAl Viro 		char c = *s++;
7504b6ccca7SAl Viro 		if (!c) {
7514b6ccca7SAl Viro 			if (!i)
7524b6ccca7SAl Viro 				break;
7534b6ccca7SAl Viro 			c = '/';
7544b6ccca7SAl Viro 			s = array[--i];
7554b6ccca7SAl Viro 		}
7564b6ccca7SAl Viro 		if (buf < end)
7574b6ccca7SAl Viro 			*buf = c;
7584b6ccca7SAl Viro 	}
7594b6ccca7SAl Viro 	rcu_read_unlock();
760cfccde04SRasmus Villemoes 	return widen_string(buf, n, end, spec);
7614b6ccca7SAl Viro }
7624b6ccca7SAl Viro 
7631031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
7641031bc58SDmitry Monakhov static noinline_for_stack
7651031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev,
7661031bc58SDmitry Monakhov 		struct printf_spec spec, const char *fmt)
7671031bc58SDmitry Monakhov {
7681031bc58SDmitry Monakhov 	struct gendisk *hd = bdev->bd_disk;
7691031bc58SDmitry Monakhov 
7701031bc58SDmitry Monakhov 	buf = string(buf, end, hd->disk_name, spec);
7711031bc58SDmitry Monakhov 	if (bdev->bd_part->partno) {
7721031bc58SDmitry Monakhov 		if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
7731031bc58SDmitry Monakhov 			if (buf < end)
7741031bc58SDmitry Monakhov 				*buf = 'p';
7751031bc58SDmitry Monakhov 			buf++;
7761031bc58SDmitry Monakhov 		}
7771031bc58SDmitry Monakhov 		buf = number(buf, end, bdev->bd_part->partno, spec);
7781031bc58SDmitry Monakhov 	}
7791031bc58SDmitry Monakhov 	return buf;
7801031bc58SDmitry Monakhov }
7811031bc58SDmitry Monakhov #endif
7821031bc58SDmitry Monakhov 
783cf3b429bSJoe Perches static noinline_for_stack
784cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
785b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
7860fe1ef24SLinus Torvalds {
787b0d33c2bSJoe Perches 	unsigned long value;
7880fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
7890fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
790b0d33c2bSJoe Perches #endif
791b0d33c2bSJoe Perches 
792b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
793b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
794b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
795b0d33c2bSJoe Perches 
796b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
797b0d33c2bSJoe Perches 	if (*fmt == 'B')
7980f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
799b0d33c2bSJoe Perches 	else if (*fmt != 'f' && *fmt != 's')
8000fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
8010c8b946eSFrederic Weisbecker 	else
8024796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
8037b9186f5SAndré Goddard Rosa 
804fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
8050fe1ef24SLinus Torvalds #else
8063cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, value, sizeof(void *));
8070fe1ef24SLinus Torvalds #endif
8080fe1ef24SLinus Torvalds }
8090fe1ef24SLinus Torvalds 
810abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = {
811abd4fe62SAndy Shevchenko 	.field_width = -1,
812abd4fe62SAndy Shevchenko 	.precision = -1,
813abd4fe62SAndy Shevchenko };
814abd4fe62SAndy Shevchenko 
81554433973SAndy Shevchenko static const struct printf_spec default_flag_spec = {
81654433973SAndy Shevchenko 	.base = 16,
81754433973SAndy Shevchenko 	.precision = -1,
81854433973SAndy Shevchenko 	.flags = SPECIAL | SMALL,
81954433973SAndy Shevchenko };
82054433973SAndy Shevchenko 
821ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = {
822ce0b4910SAndy Shevchenko 	.base = 10,
823ce0b4910SAndy Shevchenko 	.precision = -1,
824ce0b4910SAndy Shevchenko };
825ce0b4910SAndy Shevchenko 
826*4d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = {
827*4d42c447SAndy Shevchenko 	.base = 10,
828*4d42c447SAndy Shevchenko 	.field_width = 2,
829*4d42c447SAndy Shevchenko 	.precision = -1,
830*4d42c447SAndy Shevchenko 	.flags = ZEROPAD,
831*4d42c447SAndy Shevchenko };
832*4d42c447SAndy Shevchenko 
833*4d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = {
834*4d42c447SAndy Shevchenko 	.base = 10,
835*4d42c447SAndy Shevchenko 	.field_width = 4,
836*4d42c447SAndy Shevchenko 	.precision = -1,
837*4d42c447SAndy Shevchenko 	.flags = ZEROPAD,
838*4d42c447SAndy Shevchenko };
839*4d42c447SAndy Shevchenko 
840cf3b429bSJoe Perches static noinline_for_stack
841cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
842fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
843332d2e78SLinus Torvalds {
844332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
84528405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
846332d2e78SLinus Torvalds #endif
847332d2e78SLinus Torvalds 
848332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
84928405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
850332d2e78SLinus Torvalds #endif
8514da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
852fef20d9cSFrederic Weisbecker 		.base = 16,
8534da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
854fef20d9cSFrederic Weisbecker 		.precision = -1,
855fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
856fef20d9cSFrederic Weisbecker 	};
8574da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
8584da0b66cSBjorn Helgaas 		.base = 16,
8594da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
8604da0b66cSBjorn Helgaas 		.precision = -1,
8614da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
8624da0b66cSBjorn Helgaas 	};
8630f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
8640f4050c7SBjorn Helgaas 		.base = 16,
8650f4050c7SBjorn Helgaas 		.field_width = 2,
8660f4050c7SBjorn Helgaas 		.precision = -1,
8670f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
8680f4050c7SBjorn Helgaas 	};
8694da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
870fd95541eSBjorn Helgaas 		.field_width = -1,
871fd95541eSBjorn Helgaas 		.precision = 10,
872fd95541eSBjorn Helgaas 		.flags = LEFT,
873fd95541eSBjorn Helgaas 	};
874c7dabef8SBjorn Helgaas 
875c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
876c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
877c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
878c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
8799d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
880c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
881c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
882c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
883c7dabef8SBjorn Helgaas 
884332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
885c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
8864da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
887332d2e78SLinus Torvalds 
888332d2e78SLinus Torvalds 	*p++ = '[';
8894da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
890fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
8914da0b66cSBjorn Helgaas 		specp = &io_spec;
8924da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
893fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
8944da0b66cSBjorn Helgaas 		specp = &mem_spec;
8954da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
896fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
897ce0b4910SAndy Shevchenko 		specp = &default_dec_spec;
8984da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
899fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
900ce0b4910SAndy Shevchenko 		specp = &default_dec_spec;
9010f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
9020f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
9030f4050c7SBjorn Helgaas 		specp = &bus_spec;
9044da0b66cSBjorn Helgaas 	} else {
905c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
9064da0b66cSBjorn Helgaas 		specp = &mem_spec;
907c7dabef8SBjorn Helgaas 		decode = 0;
908fd95541eSBjorn Helgaas 	}
909d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
910d19cb803SBjorn Helgaas 		p = string(p, pend, "size ", str_spec);
911d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
912d19cb803SBjorn Helgaas 	} else {
9134da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
914c91d3376SBjorn Helgaas 		if (res->start != res->end) {
915332d2e78SLinus Torvalds 			*p++ = '-';
9164da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
917c91d3376SBjorn Helgaas 		}
918d19cb803SBjorn Helgaas 	}
919c7dabef8SBjorn Helgaas 	if (decode) {
920fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
921fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
922fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
923fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
9249d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
9259d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
926fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
927fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
928c7dabef8SBjorn Helgaas 	} else {
929fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
93054433973SAndy Shevchenko 		p = number(p, pend, res->flags, default_flag_spec);
931fd95541eSBjorn Helgaas 	}
932332d2e78SLinus Torvalds 	*p++ = ']';
933c7dabef8SBjorn Helgaas 	*p = '\0';
934332d2e78SLinus Torvalds 
935fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
936332d2e78SLinus Torvalds }
937332d2e78SLinus Torvalds 
938cf3b429bSJoe Perches static noinline_for_stack
93931550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
94031550a16SAndy Shevchenko 		 const char *fmt)
94131550a16SAndy Shevchenko {
942360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
94331550a16SAndy Shevchenko 				   negative value, fallback to the default */
94431550a16SAndy Shevchenko 	char separator;
94531550a16SAndy Shevchenko 
94631550a16SAndy Shevchenko 	if (spec.field_width == 0)
94731550a16SAndy Shevchenko 		/* nothing to print */
94831550a16SAndy Shevchenko 		return buf;
94931550a16SAndy Shevchenko 
95031550a16SAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
95131550a16SAndy Shevchenko 		/* NULL pointer */
95231550a16SAndy Shevchenko 		return string(buf, end, NULL, spec);
95331550a16SAndy Shevchenko 
95431550a16SAndy Shevchenko 	switch (fmt[1]) {
95531550a16SAndy Shevchenko 	case 'C':
95631550a16SAndy Shevchenko 		separator = ':';
95731550a16SAndy Shevchenko 		break;
95831550a16SAndy Shevchenko 	case 'D':
95931550a16SAndy Shevchenko 		separator = '-';
96031550a16SAndy Shevchenko 		break;
96131550a16SAndy Shevchenko 	case 'N':
96231550a16SAndy Shevchenko 		separator = 0;
96331550a16SAndy Shevchenko 		break;
96431550a16SAndy Shevchenko 	default:
96531550a16SAndy Shevchenko 		separator = ' ';
96631550a16SAndy Shevchenko 		break;
96731550a16SAndy Shevchenko 	}
96831550a16SAndy Shevchenko 
96931550a16SAndy Shevchenko 	if (spec.field_width > 0)
97031550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
97131550a16SAndy Shevchenko 
9729c98f235SRasmus Villemoes 	for (i = 0; i < len; ++i) {
9739c98f235SRasmus Villemoes 		if (buf < end)
9749c98f235SRasmus Villemoes 			*buf = hex_asc_hi(addr[i]);
9759c98f235SRasmus Villemoes 		++buf;
9769c98f235SRasmus Villemoes 		if (buf < end)
9779c98f235SRasmus Villemoes 			*buf = hex_asc_lo(addr[i]);
9789c98f235SRasmus Villemoes 		++buf;
97931550a16SAndy Shevchenko 
9809c98f235SRasmus Villemoes 		if (separator && i != len - 1) {
9819c98f235SRasmus Villemoes 			if (buf < end)
9829c98f235SRasmus Villemoes 				*buf = separator;
9839c98f235SRasmus Villemoes 			++buf;
9849c98f235SRasmus Villemoes 		}
98531550a16SAndy Shevchenko 	}
98631550a16SAndy Shevchenko 
98731550a16SAndy Shevchenko 	return buf;
98831550a16SAndy Shevchenko }
98931550a16SAndy Shevchenko 
99031550a16SAndy Shevchenko static noinline_for_stack
991dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
992dbc760bcSTejun Heo 		    struct printf_spec spec, const char *fmt)
993dbc760bcSTejun Heo {
994dbc760bcSTejun Heo 	const int CHUNKSZ = 32;
995dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
996dbc760bcSTejun Heo 	int i, chunksz;
997dbc760bcSTejun Heo 	bool first = true;
998dbc760bcSTejun Heo 
999dbc760bcSTejun Heo 	/* reused to print numbers */
1000dbc760bcSTejun Heo 	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1001dbc760bcSTejun Heo 
1002dbc760bcSTejun Heo 	chunksz = nr_bits & (CHUNKSZ - 1);
1003dbc760bcSTejun Heo 	if (chunksz == 0)
1004dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
1005dbc760bcSTejun Heo 
1006dbc760bcSTejun Heo 	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1007dbc760bcSTejun Heo 	for (; i >= 0; i -= CHUNKSZ) {
1008dbc760bcSTejun Heo 		u32 chunkmask, val;
1009dbc760bcSTejun Heo 		int word, bit;
1010dbc760bcSTejun Heo 
1011dbc760bcSTejun Heo 		chunkmask = ((1ULL << chunksz) - 1);
1012dbc760bcSTejun Heo 		word = i / BITS_PER_LONG;
1013dbc760bcSTejun Heo 		bit = i % BITS_PER_LONG;
1014dbc760bcSTejun Heo 		val = (bitmap[word] >> bit) & chunkmask;
1015dbc760bcSTejun Heo 
1016dbc760bcSTejun Heo 		if (!first) {
1017dbc760bcSTejun Heo 			if (buf < end)
1018dbc760bcSTejun Heo 				*buf = ',';
1019dbc760bcSTejun Heo 			buf++;
1020dbc760bcSTejun Heo 		}
1021dbc760bcSTejun Heo 		first = false;
1022dbc760bcSTejun Heo 
1023dbc760bcSTejun Heo 		spec.field_width = DIV_ROUND_UP(chunksz, 4);
1024dbc760bcSTejun Heo 		buf = number(buf, end, val, spec);
1025dbc760bcSTejun Heo 
1026dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
1027dbc760bcSTejun Heo 	}
1028dbc760bcSTejun Heo 	return buf;
1029dbc760bcSTejun Heo }
1030dbc760bcSTejun Heo 
1031dbc760bcSTejun Heo static noinline_for_stack
1032dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1033dbc760bcSTejun Heo 			 struct printf_spec spec, const char *fmt)
1034dbc760bcSTejun Heo {
1035dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
1036dbc760bcSTejun Heo 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
1037dbc760bcSTejun Heo 	int cur, rbot, rtop;
1038dbc760bcSTejun Heo 	bool first = true;
1039dbc760bcSTejun Heo 
1040dbc760bcSTejun Heo 	rbot = cur = find_first_bit(bitmap, nr_bits);
1041dbc760bcSTejun Heo 	while (cur < nr_bits) {
1042dbc760bcSTejun Heo 		rtop = cur;
1043dbc760bcSTejun Heo 		cur = find_next_bit(bitmap, nr_bits, cur + 1);
1044dbc760bcSTejun Heo 		if (cur < nr_bits && cur <= rtop + 1)
1045dbc760bcSTejun Heo 			continue;
1046dbc760bcSTejun Heo 
1047dbc760bcSTejun Heo 		if (!first) {
1048dbc760bcSTejun Heo 			if (buf < end)
1049dbc760bcSTejun Heo 				*buf = ',';
1050dbc760bcSTejun Heo 			buf++;
1051dbc760bcSTejun Heo 		}
1052dbc760bcSTejun Heo 		first = false;
1053dbc760bcSTejun Heo 
1054ce0b4910SAndy Shevchenko 		buf = number(buf, end, rbot, default_dec_spec);
1055dbc760bcSTejun Heo 		if (rbot < rtop) {
1056dbc760bcSTejun Heo 			if (buf < end)
1057dbc760bcSTejun Heo 				*buf = '-';
1058dbc760bcSTejun Heo 			buf++;
1059dbc760bcSTejun Heo 
1060ce0b4910SAndy Shevchenko 			buf = number(buf, end, rtop, default_dec_spec);
1061dbc760bcSTejun Heo 		}
1062dbc760bcSTejun Heo 
1063dbc760bcSTejun Heo 		rbot = cur;
1064dbc760bcSTejun Heo 	}
1065dbc760bcSTejun Heo 	return buf;
1066dbc760bcSTejun Heo }
1067dbc760bcSTejun Heo 
1068dbc760bcSTejun Heo static noinline_for_stack
1069cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
10708a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
1071dd45c9cfSHarvey Harrison {
10728a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
1073dd45c9cfSHarvey Harrison 	char *p = mac_addr;
1074dd45c9cfSHarvey Harrison 	int i;
1075bc7259a2SJoe Perches 	char separator;
107676597ff9SAndrei Emeltchenko 	bool reversed = false;
1077bc7259a2SJoe Perches 
107876597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
107976597ff9SAndrei Emeltchenko 	case 'F':
1080bc7259a2SJoe Perches 		separator = '-';
108176597ff9SAndrei Emeltchenko 		break;
108276597ff9SAndrei Emeltchenko 
108376597ff9SAndrei Emeltchenko 	case 'R':
108476597ff9SAndrei Emeltchenko 		reversed = true;
108576597ff9SAndrei Emeltchenko 		/* fall through */
108676597ff9SAndrei Emeltchenko 
108776597ff9SAndrei Emeltchenko 	default:
1088bc7259a2SJoe Perches 		separator = ':';
108976597ff9SAndrei Emeltchenko 		break;
1090bc7259a2SJoe Perches 	}
1091dd45c9cfSHarvey Harrison 
1092dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
109376597ff9SAndrei Emeltchenko 		if (reversed)
109476597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
109576597ff9SAndrei Emeltchenko 		else
109655036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
109776597ff9SAndrei Emeltchenko 
10988a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
1099bc7259a2SJoe Perches 			*p++ = separator;
1100dd45c9cfSHarvey Harrison 	}
1101dd45c9cfSHarvey Harrison 	*p = '\0';
1102dd45c9cfSHarvey Harrison 
1103fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
1104dd45c9cfSHarvey Harrison }
1105dd45c9cfSHarvey Harrison 
1106cf3b429bSJoe Perches static noinline_for_stack
1107cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
1108689afa7dSHarvey Harrison {
1109689afa7dSHarvey Harrison 	int i;
11100159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
11110159f24eSJoe Perches 	int index;
11120159f24eSJoe Perches 	int step;
1113689afa7dSHarvey Harrison 
11140159f24eSJoe Perches 	switch (fmt[2]) {
11150159f24eSJoe Perches 	case 'h':
11160159f24eSJoe Perches #ifdef __BIG_ENDIAN
11170159f24eSJoe Perches 		index = 0;
11180159f24eSJoe Perches 		step = 1;
11190159f24eSJoe Perches #else
11200159f24eSJoe Perches 		index = 3;
11210159f24eSJoe Perches 		step = -1;
11220159f24eSJoe Perches #endif
11230159f24eSJoe Perches 		break;
11240159f24eSJoe Perches 	case 'l':
11250159f24eSJoe Perches 		index = 3;
11260159f24eSJoe Perches 		step = -1;
11270159f24eSJoe Perches 		break;
11280159f24eSJoe Perches 	case 'n':
11290159f24eSJoe Perches 	case 'b':
11300159f24eSJoe Perches 	default:
11310159f24eSJoe Perches 		index = 0;
11320159f24eSJoe Perches 		step = 1;
11330159f24eSJoe Perches 		break;
11340159f24eSJoe Perches 	}
11358a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
11367c43d9a3SRasmus Villemoes 		char temp[4] __aligned(2);	/* hold each IP quad in reverse order */
1137133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
11388a27f7c9SJoe Perches 		if (leading_zeros) {
11398a27f7c9SJoe Perches 			if (digits < 3)
11408a27f7c9SJoe Perches 				*p++ = '0';
11418a27f7c9SJoe Perches 			if (digits < 2)
11428a27f7c9SJoe Perches 				*p++ = '0';
11438a27f7c9SJoe Perches 		}
11448a27f7c9SJoe Perches 		/* reverse the digits in the quad */
11458a27f7c9SJoe Perches 		while (digits--)
11468a27f7c9SJoe Perches 			*p++ = temp[digits];
11478a27f7c9SJoe Perches 		if (i < 3)
11488a27f7c9SJoe Perches 			*p++ = '.';
11490159f24eSJoe Perches 		index += step;
11508a27f7c9SJoe Perches 	}
11518a27f7c9SJoe Perches 	*p = '\0';
11527b9186f5SAndré Goddard Rosa 
11538a27f7c9SJoe Perches 	return p;
11548a27f7c9SJoe Perches }
11558a27f7c9SJoe Perches 
1156cf3b429bSJoe Perches static noinline_for_stack
1157cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
11588a27f7c9SJoe Perches {
11597b9186f5SAndré Goddard Rosa 	int i, j, range;
11608a27f7c9SJoe Perches 	unsigned char zerolength[8];
11618a27f7c9SJoe Perches 	int longest = 1;
11628a27f7c9SJoe Perches 	int colonpos = -1;
11638a27f7c9SJoe Perches 	u16 word;
11647b9186f5SAndré Goddard Rosa 	u8 hi, lo;
11658a27f7c9SJoe Perches 	bool needcolon = false;
1166eb78cd26SJoe Perches 	bool useIPv4;
1167eb78cd26SJoe Perches 	struct in6_addr in6;
1168eb78cd26SJoe Perches 
1169eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
1170eb78cd26SJoe Perches 
1171eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
11728a27f7c9SJoe Perches 
11738a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
11748a27f7c9SJoe Perches 
11758a27f7c9SJoe Perches 	if (useIPv4)
11768a27f7c9SJoe Perches 		range = 6;
11778a27f7c9SJoe Perches 	else
11788a27f7c9SJoe Perches 		range = 8;
11798a27f7c9SJoe Perches 
11808a27f7c9SJoe Perches 	/* find position of longest 0 run */
11818a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
11828a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
1183eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
11848a27f7c9SJoe Perches 				break;
11858a27f7c9SJoe Perches 			zerolength[i]++;
11868a27f7c9SJoe Perches 		}
11878a27f7c9SJoe Perches 	}
11888a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
11898a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
11908a27f7c9SJoe Perches 			longest = zerolength[i];
11918a27f7c9SJoe Perches 			colonpos = i;
11928a27f7c9SJoe Perches 		}
11938a27f7c9SJoe Perches 	}
119429cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
119529cf519eSJoe Perches 		colonpos = -1;
11968a27f7c9SJoe Perches 
11978a27f7c9SJoe Perches 	/* emit address */
11988a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
11998a27f7c9SJoe Perches 		if (i == colonpos) {
12008a27f7c9SJoe Perches 			if (needcolon || i == 0)
12018a27f7c9SJoe Perches 				*p++ = ':';
12028a27f7c9SJoe Perches 			*p++ = ':';
12038a27f7c9SJoe Perches 			needcolon = false;
12048a27f7c9SJoe Perches 			i += longest - 1;
12058a27f7c9SJoe Perches 			continue;
12068a27f7c9SJoe Perches 		}
12078a27f7c9SJoe Perches 		if (needcolon) {
12088a27f7c9SJoe Perches 			*p++ = ':';
12098a27f7c9SJoe Perches 			needcolon = false;
12108a27f7c9SJoe Perches 		}
12118a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
1212eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
12138a27f7c9SJoe Perches 		hi = word >> 8;
12148a27f7c9SJoe Perches 		lo = word & 0xff;
12158a27f7c9SJoe Perches 		if (hi) {
12168a27f7c9SJoe Perches 			if (hi > 0x0f)
121755036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
12188a27f7c9SJoe Perches 			else
12198a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
122055036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
12218a27f7c9SJoe Perches 		}
1222b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
122355036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
12248a27f7c9SJoe Perches 		else
12258a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
12268a27f7c9SJoe Perches 		needcolon = true;
12278a27f7c9SJoe Perches 	}
12288a27f7c9SJoe Perches 
12298a27f7c9SJoe Perches 	if (useIPv4) {
12308a27f7c9SJoe Perches 		if (needcolon)
12318a27f7c9SJoe Perches 			*p++ = ':';
12320159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
12338a27f7c9SJoe Perches 	}
12348a27f7c9SJoe Perches 	*p = '\0';
12357b9186f5SAndré Goddard Rosa 
12368a27f7c9SJoe Perches 	return p;
12378a27f7c9SJoe Perches }
12388a27f7c9SJoe Perches 
1239cf3b429bSJoe Perches static noinline_for_stack
1240cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
12418a27f7c9SJoe Perches {
12428a27f7c9SJoe Perches 	int i;
12437b9186f5SAndré Goddard Rosa 
1244689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
124555036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
124655036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
12478a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
1248689afa7dSHarvey Harrison 			*p++ = ':';
1249689afa7dSHarvey Harrison 	}
1250689afa7dSHarvey Harrison 	*p = '\0';
12517b9186f5SAndré Goddard Rosa 
12528a27f7c9SJoe Perches 	return p;
12538a27f7c9SJoe Perches }
12548a27f7c9SJoe Perches 
1255cf3b429bSJoe Perches static noinline_for_stack
1256cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
12578a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
12588a27f7c9SJoe Perches {
12598a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
12608a27f7c9SJoe Perches 
12618a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
1262eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
12638a27f7c9SJoe Perches 	else
1264eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
1265689afa7dSHarvey Harrison 
1266fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
1267689afa7dSHarvey Harrison }
1268689afa7dSHarvey Harrison 
1269cf3b429bSJoe Perches static noinline_for_stack
1270cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
12718a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
12724aa99606SHarvey Harrison {
12738a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
12744aa99606SHarvey Harrison 
12750159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
12764aa99606SHarvey Harrison 
1277fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
12784aa99606SHarvey Harrison }
12794aa99606SHarvey Harrison 
1280cf3b429bSJoe Perches static noinline_for_stack
128110679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
128210679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
128310679643SDaniel Borkmann {
128410679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
128510679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
128610679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
128710679643SDaniel Borkmann 		      sizeof("%1234567890")];
128810679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
128910679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
129010679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
129110679643SDaniel Borkmann 	u8 off = 0;
129210679643SDaniel Borkmann 
129310679643SDaniel Borkmann 	fmt++;
129410679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
129510679643SDaniel Borkmann 		switch (*fmt) {
129610679643SDaniel Borkmann 		case 'p':
129710679643SDaniel Borkmann 			have_p = true;
129810679643SDaniel Borkmann 			break;
129910679643SDaniel Borkmann 		case 'f':
130010679643SDaniel Borkmann 			have_f = true;
130110679643SDaniel Borkmann 			break;
130210679643SDaniel Borkmann 		case 's':
130310679643SDaniel Borkmann 			have_s = true;
130410679643SDaniel Borkmann 			break;
130510679643SDaniel Borkmann 		case 'c':
130610679643SDaniel Borkmann 			have_c = true;
130710679643SDaniel Borkmann 			break;
130810679643SDaniel Borkmann 		}
130910679643SDaniel Borkmann 	}
131010679643SDaniel Borkmann 
131110679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
131210679643SDaniel Borkmann 		*p = '[';
131310679643SDaniel Borkmann 		off = 1;
131410679643SDaniel Borkmann 	}
131510679643SDaniel Borkmann 
131610679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
131710679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
131810679643SDaniel Borkmann 	else
131910679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
132010679643SDaniel Borkmann 
132110679643SDaniel Borkmann 	if (have_p || have_s || have_f)
132210679643SDaniel Borkmann 		*p++ = ']';
132310679643SDaniel Borkmann 
132410679643SDaniel Borkmann 	if (have_p) {
132510679643SDaniel Borkmann 		*p++ = ':';
132610679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
132710679643SDaniel Borkmann 	}
132810679643SDaniel Borkmann 	if (have_f) {
132910679643SDaniel Borkmann 		*p++ = '/';
133010679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
133110679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
133210679643SDaniel Borkmann 	}
133310679643SDaniel Borkmann 	if (have_s) {
133410679643SDaniel Borkmann 		*p++ = '%';
133510679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
133610679643SDaniel Borkmann 	}
133710679643SDaniel Borkmann 	*p = '\0';
133810679643SDaniel Borkmann 
133910679643SDaniel Borkmann 	return string(buf, end, ip6_addr, spec);
134010679643SDaniel Borkmann }
134110679643SDaniel Borkmann 
134210679643SDaniel Borkmann static noinline_for_stack
134310679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
134410679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
134510679643SDaniel Borkmann {
134610679643SDaniel Borkmann 	bool have_p = false;
134710679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
134810679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
134910679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
135010679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
135110679643SDaniel Borkmann 
135210679643SDaniel Borkmann 	fmt++;
135310679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
135410679643SDaniel Borkmann 		switch (*fmt) {
135510679643SDaniel Borkmann 		case 'p':
135610679643SDaniel Borkmann 			have_p = true;
135710679643SDaniel Borkmann 			break;
135810679643SDaniel Borkmann 		case 'h':
135910679643SDaniel Borkmann 		case 'l':
136010679643SDaniel Borkmann 		case 'n':
136110679643SDaniel Borkmann 		case 'b':
136210679643SDaniel Borkmann 			fmt4[2] = *fmt;
136310679643SDaniel Borkmann 			break;
136410679643SDaniel Borkmann 		}
136510679643SDaniel Borkmann 	}
136610679643SDaniel Borkmann 
136710679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
136810679643SDaniel Borkmann 	if (have_p) {
136910679643SDaniel Borkmann 		*p++ = ':';
137010679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
137110679643SDaniel Borkmann 	}
137210679643SDaniel Borkmann 	*p = '\0';
137310679643SDaniel Borkmann 
137410679643SDaniel Borkmann 	return string(buf, end, ip4_addr, spec);
137510679643SDaniel Borkmann }
137610679643SDaniel Borkmann 
137710679643SDaniel Borkmann static noinline_for_stack
137871dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
137971dca95dSAndy Shevchenko 		     const char *fmt)
138071dca95dSAndy Shevchenko {
138171dca95dSAndy Shevchenko 	bool found = true;
138271dca95dSAndy Shevchenko 	int count = 1;
138371dca95dSAndy Shevchenko 	unsigned int flags = 0;
138471dca95dSAndy Shevchenko 	int len;
138571dca95dSAndy Shevchenko 
138671dca95dSAndy Shevchenko 	if (spec.field_width == 0)
138771dca95dSAndy Shevchenko 		return buf;				/* nothing to print */
138871dca95dSAndy Shevchenko 
138971dca95dSAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
139071dca95dSAndy Shevchenko 		return string(buf, end, NULL, spec);	/* NULL pointer */
139171dca95dSAndy Shevchenko 
139271dca95dSAndy Shevchenko 
139371dca95dSAndy Shevchenko 	do {
139471dca95dSAndy Shevchenko 		switch (fmt[count++]) {
139571dca95dSAndy Shevchenko 		case 'a':
139671dca95dSAndy Shevchenko 			flags |= ESCAPE_ANY;
139771dca95dSAndy Shevchenko 			break;
139871dca95dSAndy Shevchenko 		case 'c':
139971dca95dSAndy Shevchenko 			flags |= ESCAPE_SPECIAL;
140071dca95dSAndy Shevchenko 			break;
140171dca95dSAndy Shevchenko 		case 'h':
140271dca95dSAndy Shevchenko 			flags |= ESCAPE_HEX;
140371dca95dSAndy Shevchenko 			break;
140471dca95dSAndy Shevchenko 		case 'n':
140571dca95dSAndy Shevchenko 			flags |= ESCAPE_NULL;
140671dca95dSAndy Shevchenko 			break;
140771dca95dSAndy Shevchenko 		case 'o':
140871dca95dSAndy Shevchenko 			flags |= ESCAPE_OCTAL;
140971dca95dSAndy Shevchenko 			break;
141071dca95dSAndy Shevchenko 		case 'p':
141171dca95dSAndy Shevchenko 			flags |= ESCAPE_NP;
141271dca95dSAndy Shevchenko 			break;
141371dca95dSAndy Shevchenko 		case 's':
141471dca95dSAndy Shevchenko 			flags |= ESCAPE_SPACE;
141571dca95dSAndy Shevchenko 			break;
141671dca95dSAndy Shevchenko 		default:
141771dca95dSAndy Shevchenko 			found = false;
141871dca95dSAndy Shevchenko 			break;
141971dca95dSAndy Shevchenko 		}
142071dca95dSAndy Shevchenko 	} while (found);
142171dca95dSAndy Shevchenko 
142271dca95dSAndy Shevchenko 	if (!flags)
142371dca95dSAndy Shevchenko 		flags = ESCAPE_ANY_NP;
142471dca95dSAndy Shevchenko 
142571dca95dSAndy Shevchenko 	len = spec.field_width < 0 ? 1 : spec.field_width;
142671dca95dSAndy Shevchenko 
142741416f23SRasmus Villemoes 	/*
142841416f23SRasmus Villemoes 	 * string_escape_mem() writes as many characters as it can to
142941416f23SRasmus Villemoes 	 * the given buffer, and returns the total size of the output
143041416f23SRasmus Villemoes 	 * had the buffer been big enough.
143141416f23SRasmus Villemoes 	 */
143241416f23SRasmus Villemoes 	buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
143371dca95dSAndy Shevchenko 
143471dca95dSAndy Shevchenko 	return buf;
143571dca95dSAndy Shevchenko }
143671dca95dSAndy Shevchenko 
143771dca95dSAndy Shevchenko static noinline_for_stack
1438cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
14399ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
14409ac6e44eSJoe Perches {
14412b1b0d66SAndy Shevchenko 	char uuid[UUID_STRING_LEN + 1];
14429ac6e44eSJoe Perches 	char *p = uuid;
14439ac6e44eSJoe Perches 	int i;
1444f9727a17SChristoph Hellwig 	const u8 *index = uuid_index;
14459ac6e44eSJoe Perches 	bool uc = false;
14469ac6e44eSJoe Perches 
14479ac6e44eSJoe Perches 	switch (*(++fmt)) {
14489ac6e44eSJoe Perches 	case 'L':
14499ac6e44eSJoe Perches 		uc = true;		/* fall-through */
14509ac6e44eSJoe Perches 	case 'l':
1451f9727a17SChristoph Hellwig 		index = guid_index;
14529ac6e44eSJoe Perches 		break;
14539ac6e44eSJoe Perches 	case 'B':
14549ac6e44eSJoe Perches 		uc = true;
14559ac6e44eSJoe Perches 		break;
14569ac6e44eSJoe Perches 	}
14579ac6e44eSJoe Perches 
14589ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
1459aa4ea1c3SAndy Shevchenko 		if (uc)
1460aa4ea1c3SAndy Shevchenko 			p = hex_byte_pack_upper(p, addr[index[i]]);
1461aa4ea1c3SAndy Shevchenko 		else
146255036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[index[i]]);
14639ac6e44eSJoe Perches 		switch (i) {
14649ac6e44eSJoe Perches 		case 3:
14659ac6e44eSJoe Perches 		case 5:
14669ac6e44eSJoe Perches 		case 7:
14679ac6e44eSJoe Perches 		case 9:
14689ac6e44eSJoe Perches 			*p++ = '-';
14699ac6e44eSJoe Perches 			break;
14709ac6e44eSJoe Perches 		}
14719ac6e44eSJoe Perches 	}
14729ac6e44eSJoe Perches 
14739ac6e44eSJoe Perches 	*p = 0;
14749ac6e44eSJoe Perches 
14759ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
14769ac6e44eSJoe Perches }
14779ac6e44eSJoe Perches 
147857e73442STobin C. Harding int kptr_restrict __read_mostly;
147957e73442STobin C. Harding 
148057e73442STobin C. Harding static noinline_for_stack
148157e73442STobin C. Harding char *restricted_pointer(char *buf, char *end, const void *ptr,
148257e73442STobin C. Harding 			 struct printf_spec spec)
148357e73442STobin C. Harding {
148457e73442STobin C. Harding 	switch (kptr_restrict) {
148557e73442STobin C. Harding 	case 0:
148657e73442STobin C. Harding 		/* Always print %pK values */
148757e73442STobin C. Harding 		break;
148857e73442STobin C. Harding 	case 1: {
148957e73442STobin C. Harding 		const struct cred *cred;
149057e73442STobin C. Harding 
149157e73442STobin C. Harding 		/*
149257e73442STobin C. Harding 		 * kptr_restrict==1 cannot be used in IRQ context
149357e73442STobin C. Harding 		 * because its test for CAP_SYSLOG would be meaningless.
149457e73442STobin C. Harding 		 */
1495496a9a5fSAndy Shevchenko 		if (in_irq() || in_serving_softirq() || in_nmi()) {
1496496a9a5fSAndy Shevchenko 			if (spec.field_width == -1)
1497496a9a5fSAndy Shevchenko 				spec.field_width = 2 * sizeof(ptr);
149857e73442STobin C. Harding 			return string(buf, end, "pK-error", spec);
1499496a9a5fSAndy Shevchenko 		}
150057e73442STobin C. Harding 
150157e73442STobin C. Harding 		/*
150257e73442STobin C. Harding 		 * Only print the real pointer value if the current
150357e73442STobin C. Harding 		 * process has CAP_SYSLOG and is running with the
150457e73442STobin C. Harding 		 * same credentials it started with. This is because
150557e73442STobin C. Harding 		 * access to files is checked at open() time, but %pK
150657e73442STobin C. Harding 		 * checks permission at read() time. We don't want to
150757e73442STobin C. Harding 		 * leak pointer values if a binary opens a file using
150857e73442STobin C. Harding 		 * %pK and then elevates privileges before reading it.
150957e73442STobin C. Harding 		 */
151057e73442STobin C. Harding 		cred = current_cred();
151157e73442STobin C. Harding 		if (!has_capability_noaudit(current, CAP_SYSLOG) ||
151257e73442STobin C. Harding 		    !uid_eq(cred->euid, cred->uid) ||
151357e73442STobin C. Harding 		    !gid_eq(cred->egid, cred->gid))
151457e73442STobin C. Harding 			ptr = NULL;
151557e73442STobin C. Harding 		break;
151657e73442STobin C. Harding 	}
151757e73442STobin C. Harding 	case 2:
151857e73442STobin C. Harding 	default:
151957e73442STobin C. Harding 		/* Always print 0's for %pK */
152057e73442STobin C. Harding 		ptr = NULL;
152157e73442STobin C. Harding 		break;
152257e73442STobin C. Harding 	}
152357e73442STobin C. Harding 
1524496a9a5fSAndy Shevchenko 	return pointer_string(buf, end, ptr, spec);
152557e73442STobin C. Harding }
152657e73442STobin C. Harding 
15275b17aecfSAndy Shevchenko static noinline_for_stack
1528431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr,
1529431bca24SGeert Uytterhoeven 		  struct printf_spec spec,  const char *fmt)
1530c8f44affSMichał Mirosław {
15315b17aecfSAndy Shevchenko 	unsigned long long num;
15325b17aecfSAndy Shevchenko 	int size;
15335b17aecfSAndy Shevchenko 
15345b17aecfSAndy Shevchenko 	switch (fmt[1]) {
15355b17aecfSAndy Shevchenko 	case 'F':
15365b17aecfSAndy Shevchenko 		num = *(const netdev_features_t *)addr;
15375b17aecfSAndy Shevchenko 		size = sizeof(netdev_features_t);
15385b17aecfSAndy Shevchenko 		break;
15395b17aecfSAndy Shevchenko 	default:
1540431bca24SGeert Uytterhoeven 		return ptr_to_id(buf, end, addr, spec);
15415b17aecfSAndy Shevchenko 	}
1542c8f44affSMichał Mirosław 
15433cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, num, size);
1544c8f44affSMichał Mirosław }
1545c8f44affSMichał Mirosław 
1546aaf07621SJoe Perches static noinline_for_stack
15473cab1e71SAndy Shevchenko char *address_val(char *buf, char *end, const void *addr, const char *fmt)
1548aaf07621SJoe Perches {
1549aaf07621SJoe Perches 	unsigned long long num;
15503cab1e71SAndy Shevchenko 	int size;
1551aaf07621SJoe Perches 
1552aaf07621SJoe Perches 	switch (fmt[1]) {
1553aaf07621SJoe Perches 	case 'd':
1554aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
15553cab1e71SAndy Shevchenko 		size = sizeof(dma_addr_t);
1556aaf07621SJoe Perches 		break;
1557aaf07621SJoe Perches 	case 'p':
1558aaf07621SJoe Perches 	default:
1559aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
15603cab1e71SAndy Shevchenko 		size = sizeof(phys_addr_t);
1561aaf07621SJoe Perches 		break;
1562aaf07621SJoe Perches 	}
1563aaf07621SJoe Perches 
15643cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, num, size);
1565aaf07621SJoe Perches }
1566aaf07621SJoe Perches 
1567900cca29SGeert Uytterhoeven static noinline_for_stack
1568*4d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1569*4d42c447SAndy Shevchenko {
1570*4d42c447SAndy Shevchenko 	int year = tm->tm_year + (r ? 0 : 1900);
1571*4d42c447SAndy Shevchenko 	int mon = tm->tm_mon + (r ? 0 : 1);
1572*4d42c447SAndy Shevchenko 
1573*4d42c447SAndy Shevchenko 	buf = number(buf, end, year, default_dec04_spec);
1574*4d42c447SAndy Shevchenko 	if (buf < end)
1575*4d42c447SAndy Shevchenko 		*buf = '-';
1576*4d42c447SAndy Shevchenko 	buf++;
1577*4d42c447SAndy Shevchenko 
1578*4d42c447SAndy Shevchenko 	buf = number(buf, end, mon, default_dec02_spec);
1579*4d42c447SAndy Shevchenko 	if (buf < end)
1580*4d42c447SAndy Shevchenko 		*buf = '-';
1581*4d42c447SAndy Shevchenko 	buf++;
1582*4d42c447SAndy Shevchenko 
1583*4d42c447SAndy Shevchenko 	return number(buf, end, tm->tm_mday, default_dec02_spec);
1584*4d42c447SAndy Shevchenko }
1585*4d42c447SAndy Shevchenko 
1586*4d42c447SAndy Shevchenko static noinline_for_stack
1587*4d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
1588*4d42c447SAndy Shevchenko {
1589*4d42c447SAndy Shevchenko 	buf = number(buf, end, tm->tm_hour, default_dec02_spec);
1590*4d42c447SAndy Shevchenko 	if (buf < end)
1591*4d42c447SAndy Shevchenko 		*buf = ':';
1592*4d42c447SAndy Shevchenko 	buf++;
1593*4d42c447SAndy Shevchenko 
1594*4d42c447SAndy Shevchenko 	buf = number(buf, end, tm->tm_min, default_dec02_spec);
1595*4d42c447SAndy Shevchenko 	if (buf < end)
1596*4d42c447SAndy Shevchenko 		*buf = ':';
1597*4d42c447SAndy Shevchenko 	buf++;
1598*4d42c447SAndy Shevchenko 
1599*4d42c447SAndy Shevchenko 	return number(buf, end, tm->tm_sec, default_dec02_spec);
1600*4d42c447SAndy Shevchenko }
1601*4d42c447SAndy Shevchenko 
1602*4d42c447SAndy Shevchenko static noinline_for_stack
1603*4d42c447SAndy Shevchenko char *rtc_str(char *buf, char *end, const struct rtc_time *tm, const char *fmt)
1604*4d42c447SAndy Shevchenko {
1605*4d42c447SAndy Shevchenko 	bool have_t = true, have_d = true;
1606*4d42c447SAndy Shevchenko 	bool raw = false;
1607*4d42c447SAndy Shevchenko 	int count = 2;
1608*4d42c447SAndy Shevchenko 
1609*4d42c447SAndy Shevchenko 	switch (fmt[count]) {
1610*4d42c447SAndy Shevchenko 	case 'd':
1611*4d42c447SAndy Shevchenko 		have_t = false;
1612*4d42c447SAndy Shevchenko 		count++;
1613*4d42c447SAndy Shevchenko 		break;
1614*4d42c447SAndy Shevchenko 	case 't':
1615*4d42c447SAndy Shevchenko 		have_d = false;
1616*4d42c447SAndy Shevchenko 		count++;
1617*4d42c447SAndy Shevchenko 		break;
1618*4d42c447SAndy Shevchenko 	}
1619*4d42c447SAndy Shevchenko 
1620*4d42c447SAndy Shevchenko 	raw = fmt[count] == 'r';
1621*4d42c447SAndy Shevchenko 
1622*4d42c447SAndy Shevchenko 	if (have_d)
1623*4d42c447SAndy Shevchenko 		buf = date_str(buf, end, tm, raw);
1624*4d42c447SAndy Shevchenko 	if (have_d && have_t) {
1625*4d42c447SAndy Shevchenko 		/* Respect ISO 8601 */
1626*4d42c447SAndy Shevchenko 		if (buf < end)
1627*4d42c447SAndy Shevchenko 			*buf = 'T';
1628*4d42c447SAndy Shevchenko 		buf++;
1629*4d42c447SAndy Shevchenko 	}
1630*4d42c447SAndy Shevchenko 	if (have_t)
1631*4d42c447SAndy Shevchenko 		buf = time_str(buf, end, tm, raw);
1632*4d42c447SAndy Shevchenko 
1633*4d42c447SAndy Shevchenko 	return buf;
1634*4d42c447SAndy Shevchenko }
1635*4d42c447SAndy Shevchenko 
1636*4d42c447SAndy Shevchenko static noinline_for_stack
1637*4d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
1638*4d42c447SAndy Shevchenko 		    const char *fmt)
1639*4d42c447SAndy Shevchenko {
1640*4d42c447SAndy Shevchenko 	switch (fmt[1]) {
1641*4d42c447SAndy Shevchenko 	case 'R':
1642*4d42c447SAndy Shevchenko 		return rtc_str(buf, end, (const struct rtc_time *)ptr, fmt);
1643*4d42c447SAndy Shevchenko 	default:
1644*4d42c447SAndy Shevchenko 		return ptr_to_id(buf, end, ptr, spec);
1645*4d42c447SAndy Shevchenko 	}
1646*4d42c447SAndy Shevchenko }
1647*4d42c447SAndy Shevchenko 
1648*4d42c447SAndy Shevchenko static noinline_for_stack
1649900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1650900cca29SGeert Uytterhoeven 	    const char *fmt)
1651900cca29SGeert Uytterhoeven {
1652900cca29SGeert Uytterhoeven 	if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1653900cca29SGeert Uytterhoeven 		return string(buf, end, NULL, spec);
1654900cca29SGeert Uytterhoeven 
1655900cca29SGeert Uytterhoeven 	switch (fmt[1]) {
1656900cca29SGeert Uytterhoeven 	case 'n':
1657900cca29SGeert Uytterhoeven 	default:
1658900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK
1659900cca29SGeert Uytterhoeven 		return string(buf, end, __clk_get_name(clk), spec);
1660900cca29SGeert Uytterhoeven #else
1661ec12bc29SGeert Uytterhoeven 		return ptr_to_id(buf, end, clk, spec);
1662900cca29SGeert Uytterhoeven #endif
1663900cca29SGeert Uytterhoeven 	}
1664900cca29SGeert Uytterhoeven }
1665900cca29SGeert Uytterhoeven 
1666edf14cdbSVlastimil Babka static
1667edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags,
1668edf14cdbSVlastimil Babka 					const struct trace_print_flags *names)
1669edf14cdbSVlastimil Babka {
1670edf14cdbSVlastimil Babka 	unsigned long mask;
1671edf14cdbSVlastimil Babka 
1672edf14cdbSVlastimil Babka 	for ( ; flags && names->name; names++) {
1673edf14cdbSVlastimil Babka 		mask = names->mask;
1674edf14cdbSVlastimil Babka 		if ((flags & mask) != mask)
1675edf14cdbSVlastimil Babka 			continue;
1676edf14cdbSVlastimil Babka 
1677abd4fe62SAndy Shevchenko 		buf = string(buf, end, names->name, default_str_spec);
1678edf14cdbSVlastimil Babka 
1679edf14cdbSVlastimil Babka 		flags &= ~mask;
1680edf14cdbSVlastimil Babka 		if (flags) {
1681edf14cdbSVlastimil Babka 			if (buf < end)
1682edf14cdbSVlastimil Babka 				*buf = '|';
1683edf14cdbSVlastimil Babka 			buf++;
1684edf14cdbSVlastimil Babka 		}
1685edf14cdbSVlastimil Babka 	}
1686edf14cdbSVlastimil Babka 
1687edf14cdbSVlastimil Babka 	if (flags)
168854433973SAndy Shevchenko 		buf = number(buf, end, flags, default_flag_spec);
1689edf14cdbSVlastimil Babka 
1690edf14cdbSVlastimil Babka 	return buf;
1691edf14cdbSVlastimil Babka }
1692edf14cdbSVlastimil Babka 
1693edf14cdbSVlastimil Babka static noinline_for_stack
1694edf14cdbSVlastimil Babka char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1695edf14cdbSVlastimil Babka {
1696edf14cdbSVlastimil Babka 	unsigned long flags;
1697edf14cdbSVlastimil Babka 	const struct trace_print_flags *names;
1698edf14cdbSVlastimil Babka 
1699edf14cdbSVlastimil Babka 	switch (fmt[1]) {
1700edf14cdbSVlastimil Babka 	case 'p':
1701edf14cdbSVlastimil Babka 		flags = *(unsigned long *)flags_ptr;
1702edf14cdbSVlastimil Babka 		/* Remove zone id */
1703edf14cdbSVlastimil Babka 		flags &= (1UL << NR_PAGEFLAGS) - 1;
1704edf14cdbSVlastimil Babka 		names = pageflag_names;
1705edf14cdbSVlastimil Babka 		break;
1706edf14cdbSVlastimil Babka 	case 'v':
1707edf14cdbSVlastimil Babka 		flags = *(unsigned long *)flags_ptr;
1708edf14cdbSVlastimil Babka 		names = vmaflag_names;
1709edf14cdbSVlastimil Babka 		break;
1710edf14cdbSVlastimil Babka 	case 'g':
1711edf14cdbSVlastimil Babka 		flags = *(gfp_t *)flags_ptr;
1712edf14cdbSVlastimil Babka 		names = gfpflag_names;
1713edf14cdbSVlastimil Babka 		break;
1714edf14cdbSVlastimil Babka 	default:
1715edf14cdbSVlastimil Babka 		WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1716edf14cdbSVlastimil Babka 		return buf;
1717edf14cdbSVlastimil Babka 	}
1718edf14cdbSVlastimil Babka 
1719edf14cdbSVlastimil Babka 	return format_flags(buf, end, flags, names);
1720edf14cdbSVlastimil Babka }
1721edf14cdbSVlastimil Babka 
1722ce4fecf1SPantelis Antoniou static const char *device_node_name_for_depth(const struct device_node *np, int depth)
1723ce4fecf1SPantelis Antoniou {
1724ce4fecf1SPantelis Antoniou 	for ( ; np && depth; depth--)
1725ce4fecf1SPantelis Antoniou 		np = np->parent;
1726ce4fecf1SPantelis Antoniou 
1727ce4fecf1SPantelis Antoniou 	return kbasename(np->full_name);
1728ce4fecf1SPantelis Antoniou }
1729ce4fecf1SPantelis Antoniou 
1730ce4fecf1SPantelis Antoniou static noinline_for_stack
1731ce4fecf1SPantelis Antoniou char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end)
1732ce4fecf1SPantelis Antoniou {
1733ce4fecf1SPantelis Antoniou 	int depth;
1734ce4fecf1SPantelis Antoniou 	const struct device_node *parent = np->parent;
1735ce4fecf1SPantelis Antoniou 
1736ce4fecf1SPantelis Antoniou 	/* special case for root node */
1737ce4fecf1SPantelis Antoniou 	if (!parent)
1738abd4fe62SAndy Shevchenko 		return string(buf, end, "/", default_str_spec);
1739ce4fecf1SPantelis Antoniou 
1740ce4fecf1SPantelis Antoniou 	for (depth = 0; parent->parent; depth++)
1741ce4fecf1SPantelis Antoniou 		parent = parent->parent;
1742ce4fecf1SPantelis Antoniou 
1743ce4fecf1SPantelis Antoniou 	for ( ; depth >= 0; depth--) {
1744abd4fe62SAndy Shevchenko 		buf = string(buf, end, "/", default_str_spec);
1745ce4fecf1SPantelis Antoniou 		buf = string(buf, end, device_node_name_for_depth(np, depth),
1746abd4fe62SAndy Shevchenko 			     default_str_spec);
1747ce4fecf1SPantelis Antoniou 	}
1748ce4fecf1SPantelis Antoniou 	return buf;
1749ce4fecf1SPantelis Antoniou }
1750ce4fecf1SPantelis Antoniou 
1751ce4fecf1SPantelis Antoniou static noinline_for_stack
1752ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn,
1753ce4fecf1SPantelis Antoniou 			 struct printf_spec spec, const char *fmt)
1754ce4fecf1SPantelis Antoniou {
1755ce4fecf1SPantelis Antoniou 	char tbuf[sizeof("xxxx") + 1];
1756ce4fecf1SPantelis Antoniou 	const char *p;
1757ce4fecf1SPantelis Antoniou 	int ret;
1758ce4fecf1SPantelis Antoniou 	char *buf_start = buf;
1759ce4fecf1SPantelis Antoniou 	struct property *prop;
1760ce4fecf1SPantelis Antoniou 	bool has_mult, pass;
1761ce4fecf1SPantelis Antoniou 	static const struct printf_spec num_spec = {
1762ce4fecf1SPantelis Antoniou 		.flags = SMALL,
1763ce4fecf1SPantelis Antoniou 		.field_width = -1,
1764ce4fecf1SPantelis Antoniou 		.precision = -1,
1765ce4fecf1SPantelis Antoniou 		.base = 10,
1766ce4fecf1SPantelis Antoniou 	};
1767ce4fecf1SPantelis Antoniou 
1768ce4fecf1SPantelis Antoniou 	struct printf_spec str_spec = spec;
1769ce4fecf1SPantelis Antoniou 	str_spec.field_width = -1;
1770ce4fecf1SPantelis Antoniou 
1771ce4fecf1SPantelis Antoniou 	if (!IS_ENABLED(CONFIG_OF))
1772ce4fecf1SPantelis Antoniou 		return string(buf, end, "(!OF)", spec);
1773ce4fecf1SPantelis Antoniou 
1774ce4fecf1SPantelis Antoniou 	if ((unsigned long)dn < PAGE_SIZE)
1775ce4fecf1SPantelis Antoniou 		return string(buf, end, "(null)", spec);
1776ce4fecf1SPantelis Antoniou 
1777ce4fecf1SPantelis Antoniou 	/* simple case without anything any more format specifiers */
1778ce4fecf1SPantelis Antoniou 	fmt++;
1779ce4fecf1SPantelis Antoniou 	if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1780ce4fecf1SPantelis Antoniou 		fmt = "f";
1781ce4fecf1SPantelis Antoniou 
1782ce4fecf1SPantelis Antoniou 	for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
17836d0a70a2SRob Herring 		int precision;
1784ce4fecf1SPantelis Antoniou 		if (pass) {
1785ce4fecf1SPantelis Antoniou 			if (buf < end)
1786ce4fecf1SPantelis Antoniou 				*buf = ':';
1787ce4fecf1SPantelis Antoniou 			buf++;
1788ce4fecf1SPantelis Antoniou 		}
1789ce4fecf1SPantelis Antoniou 
1790ce4fecf1SPantelis Antoniou 		switch (*fmt) {
1791ce4fecf1SPantelis Antoniou 		case 'f':	/* full_name */
1792ce4fecf1SPantelis Antoniou 			buf = device_node_gen_full_name(dn, buf, end);
1793ce4fecf1SPantelis Antoniou 			break;
1794ce4fecf1SPantelis Antoniou 		case 'n':	/* name */
17956d0a70a2SRob Herring 			p = kbasename(of_node_full_name(dn));
17966d0a70a2SRob Herring 			precision = str_spec.precision;
17976d0a70a2SRob Herring 			str_spec.precision = strchrnul(p, '@') - p;
17986d0a70a2SRob Herring 			buf = string(buf, end, p, str_spec);
17996d0a70a2SRob Herring 			str_spec.precision = precision;
1800ce4fecf1SPantelis Antoniou 			break;
1801ce4fecf1SPantelis Antoniou 		case 'p':	/* phandle */
1802ce4fecf1SPantelis Antoniou 			buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
1803ce4fecf1SPantelis Antoniou 			break;
1804ce4fecf1SPantelis Antoniou 		case 'P':	/* path-spec */
1805ce4fecf1SPantelis Antoniou 			p = kbasename(of_node_full_name(dn));
1806ce4fecf1SPantelis Antoniou 			if (!p[1])
1807ce4fecf1SPantelis Antoniou 				p = "/";
1808ce4fecf1SPantelis Antoniou 			buf = string(buf, end, p, str_spec);
1809ce4fecf1SPantelis Antoniou 			break;
1810ce4fecf1SPantelis Antoniou 		case 'F':	/* flags */
1811ce4fecf1SPantelis Antoniou 			tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
1812ce4fecf1SPantelis Antoniou 			tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
1813ce4fecf1SPantelis Antoniou 			tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
1814ce4fecf1SPantelis Antoniou 			tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
1815ce4fecf1SPantelis Antoniou 			tbuf[4] = 0;
1816ce4fecf1SPantelis Antoniou 			buf = string(buf, end, tbuf, str_spec);
1817ce4fecf1SPantelis Antoniou 			break;
1818ce4fecf1SPantelis Antoniou 		case 'c':	/* major compatible string */
1819ce4fecf1SPantelis Antoniou 			ret = of_property_read_string(dn, "compatible", &p);
1820ce4fecf1SPantelis Antoniou 			if (!ret)
1821ce4fecf1SPantelis Antoniou 				buf = string(buf, end, p, str_spec);
1822ce4fecf1SPantelis Antoniou 			break;
1823ce4fecf1SPantelis Antoniou 		case 'C':	/* full compatible string */
1824ce4fecf1SPantelis Antoniou 			has_mult = false;
1825ce4fecf1SPantelis Antoniou 			of_property_for_each_string(dn, "compatible", prop, p) {
1826ce4fecf1SPantelis Antoniou 				if (has_mult)
1827ce4fecf1SPantelis Antoniou 					buf = string(buf, end, ",", str_spec);
1828ce4fecf1SPantelis Antoniou 				buf = string(buf, end, "\"", str_spec);
1829ce4fecf1SPantelis Antoniou 				buf = string(buf, end, p, str_spec);
1830ce4fecf1SPantelis Antoniou 				buf = string(buf, end, "\"", str_spec);
1831ce4fecf1SPantelis Antoniou 
1832ce4fecf1SPantelis Antoniou 				has_mult = true;
1833ce4fecf1SPantelis Antoniou 			}
1834ce4fecf1SPantelis Antoniou 			break;
1835ce4fecf1SPantelis Antoniou 		default:
1836ce4fecf1SPantelis Antoniou 			break;
1837ce4fecf1SPantelis Antoniou 		}
1838ce4fecf1SPantelis Antoniou 	}
1839ce4fecf1SPantelis Antoniou 
1840ce4fecf1SPantelis Antoniou 	return widen_string(buf, buf - buf_start, end, spec);
1841ce4fecf1SPantelis Antoniou }
1842ce4fecf1SPantelis Antoniou 
18434d8a743cSLinus Torvalds /*
18444d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
18454d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
18464d8a743cSLinus Torvalds  * specifiers.
18474d8a743cSLinus Torvalds  *
18480b523769SJoe Perches  * Please update scripts/checkpatch.pl when adding/removing conversion
18490b523769SJoe Perches  * characters.  (Search for "check for vsprintf extension").
18500b523769SJoe Perches  *
1851332d2e78SLinus Torvalds  * Right now we handle:
18520fe1ef24SLinus Torvalds  *
1853cdb7e52dSSergey Senozhatsky  * - 'S' For symbolic direct pointers (or function descriptors) with offset
1854cdb7e52dSSergey Senozhatsky  * - 's' For symbolic direct pointers (or function descriptors) without offset
1855cdb7e52dSSergey Senozhatsky  * - 'F' Same as 'S'
1856cdb7e52dSSergey Senozhatsky  * - 'f' Same as 's'
1857b0d33c2bSJoe Perches  * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
18580f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
1859c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1860c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1861dbc760bcSTejun Heo  * - 'b[l]' For a bitmap, the number of bits is determined by the field
1862dbc760bcSTejun Heo  *       width which must be explicitly specified either as part of the
1863dbc760bcSTejun Heo  *       format string '%32b[l]' or through '%*b[l]', [l] selects
1864dbc760bcSTejun Heo  *       range-list format instead of hex format
1865dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
1866dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
18678a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1868bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1869c8e00060SJoe Perches  *       with a dash-separated hex notation
18707c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
18718a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
18728a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
18738a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
187410679643SDaniel Borkmann  *       [S][pfs]
187510679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
187610679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
18778a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
18788a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
18798a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
188010679643SDaniel Borkmann  *       [S][pfs]
188110679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
188210679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
188310679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
188410679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
188529cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
188671dca95dSAndy Shevchenko  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
188771dca95dSAndy Shevchenko  *                of the following flags (see string_escape_mem() for the
188871dca95dSAndy Shevchenko  *                details):
188971dca95dSAndy Shevchenko  *                  a - ESCAPE_ANY
189071dca95dSAndy Shevchenko  *                  c - ESCAPE_SPECIAL
189171dca95dSAndy Shevchenko  *                  h - ESCAPE_HEX
189271dca95dSAndy Shevchenko  *                  n - ESCAPE_NULL
189371dca95dSAndy Shevchenko  *                  o - ESCAPE_OCTAL
189471dca95dSAndy Shevchenko  *                  p - ESCAPE_NP
189571dca95dSAndy Shevchenko  *                  s - ESCAPE_SPACE
189671dca95dSAndy Shevchenko  *                By default ESCAPE_ANY_NP is used.
18979ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
18989ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
18999ac6e44eSJoe Perches  *       Options for %pU are:
19009ac6e44eSJoe Perches  *         b big endian lower case hex (default)
19019ac6e44eSJoe Perches  *         B big endian UPPER case hex
19029ac6e44eSJoe Perches  *         l little endian lower case hex
19039ac6e44eSJoe Perches  *         L little endian UPPER case hex
19049ac6e44eSJoe Perches  *           big endian output byte order is:
19059ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
19069ac6e44eSJoe Perches  *           little endian output byte order is:
19079ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
19087db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
19097db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
19107db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
19117db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
19127db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
1913455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
1914c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
191531550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
191631550a16SAndy Shevchenko  *            a certain separator (' ' by default):
191731550a16SAndy Shevchenko  *              C colon
191831550a16SAndy Shevchenko  *              D dash
191931550a16SAndy Shevchenko  *              N no separator
192031550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
192131550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
1922aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1923aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
1924c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
1925c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
19261031bc58SDmitry Monakhov  * - 'g' For block_device name (gendisk + partition number)
1927*4d42c447SAndy Shevchenko  * - 't[R][dt][r]' For time and date as represented:
1928*4d42c447SAndy Shevchenko  *      R    struct rtc_time
1929900cca29SGeert Uytterhoeven  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1930900cca29SGeert Uytterhoeven  *       (legacy clock framework) of the clock
1931900cca29SGeert Uytterhoeven  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1932900cca29SGeert Uytterhoeven  *        (legacy clock framework) of the clock
1933900cca29SGeert Uytterhoeven  * - 'Cr' For a clock, it prints the current rate of the clock
1934edf14cdbSVlastimil Babka  * - 'G' For flags to be printed as a collection of symbolic strings that would
1935edf14cdbSVlastimil Babka  *       construct the specific value. Supported flags given by option:
1936edf14cdbSVlastimil Babka  *       p page flags (see struct page) given as pointer to unsigned long
1937edf14cdbSVlastimil Babka  *       g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1938edf14cdbSVlastimil Babka  *       v vma flags (VM_*) given as pointer to unsigned long
1939ce4fecf1SPantelis Antoniou  * - 'OF[fnpPcCF]'  For a device tree object
1940ce4fecf1SPantelis Antoniou  *                  Without any optional arguments prints the full_name
1941ce4fecf1SPantelis Antoniou  *                  f device node full_name
1942ce4fecf1SPantelis Antoniou  *                  n device node name
1943ce4fecf1SPantelis Antoniou  *                  p device node phandle
1944ce4fecf1SPantelis Antoniou  *                  P device node path spec (name + @unit)
1945ce4fecf1SPantelis Antoniou  *                  F device node flags
1946ce4fecf1SPantelis Antoniou  *                  c major compatible string
1947ce4fecf1SPantelis Antoniou  *                  C full compatible string
19487b1924a1STobin C. Harding  * - 'x' For printing the address. Equivalent to "%lx".
19497b1924a1STobin C. Harding  *
1950b3ed2321STobin C. Harding  * ** When making changes please also update:
1951b3ed2321STobin C. Harding  *	Documentation/core-api/printk-formats.rst
19529ac6e44eSJoe Perches  *
1953ad67b74dSTobin C. Harding  * Note: The default behaviour (unadorned %p) is to hash the address,
1954ad67b74dSTobin C. Harding  * rendering it useful as a unique identifier.
19554d8a743cSLinus Torvalds  */
1956cf3b429bSJoe Perches static noinline_for_stack
1957cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1958fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
195978a8bf69SLinus Torvalds {
196080c9eb46SRasmus Villemoes 	const int default_width = 2 * sizeof(void *);
1961725fe002SGrant Likely 
19623a129cc2SAdam Borowski 	if (!ptr && *fmt != 'K' && *fmt != 'x') {
19635e057981SJoe Perches 		/*
19645e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
19655e057981SJoe Perches 		 * tabular output look nice.
19665e057981SJoe Perches 		 */
19675e057981SJoe Perches 		if (spec.field_width == -1)
1968725fe002SGrant Likely 			spec.field_width = default_width;
1969fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
19705e057981SJoe Perches 	}
1971d97106abSLinus Torvalds 
19720fe1ef24SLinus Torvalds 	switch (*fmt) {
19730fe1ef24SLinus Torvalds 	case 'F':
19740c8b946eSFrederic Weisbecker 	case 'f':
19750fe1ef24SLinus Torvalds 	case 'S':
19769ac6e44eSJoe Perches 	case 's':
197704b8eb7aSSergey Senozhatsky 		ptr = dereference_symbol_descriptor(ptr);
197804b8eb7aSSergey Senozhatsky 		/* Fallthrough */
19790f77a8d3SNamhyung Kim 	case 'B':
1980b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
1981332d2e78SLinus Torvalds 	case 'R':
1982c7dabef8SBjorn Helgaas 	case 'r':
1983fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
198431550a16SAndy Shevchenko 	case 'h':
198531550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
1986dbc760bcSTejun Heo 	case 'b':
1987dbc760bcSTejun Heo 		switch (fmt[1]) {
1988dbc760bcSTejun Heo 		case 'l':
1989dbc760bcSTejun Heo 			return bitmap_list_string(buf, end, ptr, spec, fmt);
1990dbc760bcSTejun Heo 		default:
1991dbc760bcSTejun Heo 			return bitmap_string(buf, end, ptr, spec, fmt);
1992dbc760bcSTejun Heo 		}
19938a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
19948a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
199576597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
199676597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
19978a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
19988a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
19998a27f7c9SJoe Perches 					 * 4:	1.2.3.4
20008a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
20018a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
20028a27f7c9SJoe Perches 					 */
20038a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
20048a27f7c9SJoe Perches 					 * 4:	001.002.003.004
20058a27f7c9SJoe Perches 					 * 6:   000102...0f
20068a27f7c9SJoe Perches 					 */
20078a27f7c9SJoe Perches 		switch (fmt[1]) {
20088a27f7c9SJoe Perches 		case '6':
20098a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
20108a27f7c9SJoe Perches 		case '4':
20118a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
201210679643SDaniel Borkmann 		case 'S': {
201310679643SDaniel Borkmann 			const union {
201410679643SDaniel Borkmann 				struct sockaddr		raw;
201510679643SDaniel Borkmann 				struct sockaddr_in	v4;
201610679643SDaniel Borkmann 				struct sockaddr_in6	v6;
201710679643SDaniel Borkmann 			} *sa = ptr;
201810679643SDaniel Borkmann 
201910679643SDaniel Borkmann 			switch (sa->raw.sa_family) {
202010679643SDaniel Borkmann 			case AF_INET:
202110679643SDaniel Borkmann 				return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
202210679643SDaniel Borkmann 			case AF_INET6:
202310679643SDaniel Borkmann 				return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
202410679643SDaniel Borkmann 			default:
202510679643SDaniel Borkmann 				return string(buf, end, "(invalid address)", spec);
202610679643SDaniel Borkmann 			}}
20278a27f7c9SJoe Perches 		}
20284aa99606SHarvey Harrison 		break;
202971dca95dSAndy Shevchenko 	case 'E':
203071dca95dSAndy Shevchenko 		return escaped_string(buf, end, ptr, spec, fmt);
20319ac6e44eSJoe Perches 	case 'U':
20329ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
20337db6f5fbSJoe Perches 	case 'V':
20345756b76eSJan Beulich 		{
20355756b76eSJan Beulich 			va_list va;
20365756b76eSJan Beulich 
20375756b76eSJan Beulich 			va_copy(va, *((struct va_format *)ptr)->va);
20385756b76eSJan Beulich 			buf += vsnprintf(buf, end > buf ? end - buf : 0,
20395756b76eSJan Beulich 					 ((struct va_format *)ptr)->fmt, va);
20405756b76eSJan Beulich 			va_end(va);
20415756b76eSJan Beulich 			return buf;
20425756b76eSJan Beulich 		}
2043455cd5abSDan Rosenberg 	case 'K':
2044ef0010a3SLinus Torvalds 		if (!kptr_restrict)
2045ef0010a3SLinus Torvalds 			break;
204657e73442STobin C. Harding 		return restricted_pointer(buf, end, ptr, spec);
2047c8f44affSMichał Mirosław 	case 'N':
2048431bca24SGeert Uytterhoeven 		return netdev_bits(buf, end, ptr, spec, fmt);
20497d799210SStepan Moskovchenko 	case 'a':
20503cab1e71SAndy Shevchenko 		return address_val(buf, end, ptr, fmt);
20514b6ccca7SAl Viro 	case 'd':
20524b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
2053*4d42c447SAndy Shevchenko 	case 't':
2054*4d42c447SAndy Shevchenko 		return time_and_date(buf, end, ptr, spec, fmt);
2055900cca29SGeert Uytterhoeven 	case 'C':
2056900cca29SGeert Uytterhoeven 		return clock(buf, end, ptr, spec, fmt);
20574b6ccca7SAl Viro 	case 'D':
20584b6ccca7SAl Viro 		return dentry_name(buf, end,
20594b6ccca7SAl Viro 				   ((const struct file *)ptr)->f_path.dentry,
20604b6ccca7SAl Viro 				   spec, fmt);
20611031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
20621031bc58SDmitry Monakhov 	case 'g':
20631031bc58SDmitry Monakhov 		return bdev_name(buf, end, ptr, spec, fmt);
20641031bc58SDmitry Monakhov #endif
20651031bc58SDmitry Monakhov 
2066edf14cdbSVlastimil Babka 	case 'G':
2067edf14cdbSVlastimil Babka 		return flags_string(buf, end, ptr, fmt);
2068ce4fecf1SPantelis Antoniou 	case 'O':
2069ce4fecf1SPantelis Antoniou 		switch (fmt[1]) {
2070ce4fecf1SPantelis Antoniou 		case 'F':
2071ce4fecf1SPantelis Antoniou 			return device_node_string(buf, end, ptr, spec, fmt + 1);
2072ce4fecf1SPantelis Antoniou 		}
2073554ec508SBart Van Assche 		break;
20747b1924a1STobin C. Harding 	case 'x':
20757b1924a1STobin C. Harding 		return pointer_string(buf, end, ptr, spec);
20760fe1ef24SLinus Torvalds 	}
2077fef20d9cSFrederic Weisbecker 
2078ad67b74dSTobin C. Harding 	/* default is to _not_ leak addresses, hash before printing */
2079ad67b74dSTobin C. Harding 	return ptr_to_id(buf, end, ptr, spec);
2080fef20d9cSFrederic Weisbecker }
2081fef20d9cSFrederic Weisbecker 
2082fef20d9cSFrederic Weisbecker /*
2083fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
2084fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
2085fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
2086fef20d9cSFrederic Weisbecker  * to go on the next call).
2087fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
2088fef20d9cSFrederic Weisbecker  *
2089fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
2090fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
2091fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
20925b5e0928SAlexey Dobriyan  * 'Z' changed to 'z' --adobriyan 2017-01-25
2093fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
2094fef20d9cSFrederic Weisbecker  *
2095fef20d9cSFrederic Weisbecker  * @fmt: the format string
2096fef20d9cSFrederic Weisbecker  * @type of the token returned
2097fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
2098fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
2099fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
2100fef20d9cSFrederic Weisbecker  * @precision: precision of a number
2101fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
2102fef20d9cSFrederic Weisbecker  */
2103cf3b429bSJoe Perches static noinline_for_stack
2104cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
2105fef20d9cSFrederic Weisbecker {
2106fef20d9cSFrederic Weisbecker 	const char *start = fmt;
2107d0484193SRasmus Villemoes 	char qualifier;
2108fef20d9cSFrederic Weisbecker 
2109fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
2110ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
2111fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
2112fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
2113fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
2114fef20d9cSFrederic Weisbecker 		}
2115fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
2116fef20d9cSFrederic Weisbecker 		goto precision;
2117fef20d9cSFrederic Weisbecker 	}
2118fef20d9cSFrederic Weisbecker 
2119fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
2120fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
2121fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
2122fef20d9cSFrederic Weisbecker 			spec->precision = 0;
2123fef20d9cSFrederic Weisbecker 
2124fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
2125fef20d9cSFrederic Weisbecker 		goto qualifier;
2126fef20d9cSFrederic Weisbecker 	}
2127fef20d9cSFrederic Weisbecker 
2128fef20d9cSFrederic Weisbecker 	/* By default */
2129fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
2130fef20d9cSFrederic Weisbecker 
2131fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
2132fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
2133fef20d9cSFrederic Weisbecker 			break;
2134fef20d9cSFrederic Weisbecker 	}
2135fef20d9cSFrederic Weisbecker 
2136fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
2137fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
2138fef20d9cSFrederic Weisbecker 		return fmt - start;
2139fef20d9cSFrederic Weisbecker 
2140fef20d9cSFrederic Weisbecker 	/* Process flags */
2141fef20d9cSFrederic Weisbecker 	spec->flags = 0;
2142fef20d9cSFrederic Weisbecker 
2143fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
2144fef20d9cSFrederic Weisbecker 		bool found = true;
2145fef20d9cSFrederic Weisbecker 
2146fef20d9cSFrederic Weisbecker 		++fmt;
2147fef20d9cSFrederic Weisbecker 
2148fef20d9cSFrederic Weisbecker 		switch (*fmt) {
2149fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
2150fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
2151fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
2152fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
2153fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
2154fef20d9cSFrederic Weisbecker 		default:  found = false;
2155fef20d9cSFrederic Weisbecker 		}
2156fef20d9cSFrederic Weisbecker 
2157fef20d9cSFrederic Weisbecker 		if (!found)
2158fef20d9cSFrederic Weisbecker 			break;
2159fef20d9cSFrederic Weisbecker 	}
2160fef20d9cSFrederic Weisbecker 
2161fef20d9cSFrederic Weisbecker 	/* get field width */
2162fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
2163fef20d9cSFrederic Weisbecker 
2164fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
2165fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
2166fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
2167fef20d9cSFrederic Weisbecker 		/* it's the next argument */
2168ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
2169fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2170fef20d9cSFrederic Weisbecker 	}
2171fef20d9cSFrederic Weisbecker 
2172fef20d9cSFrederic Weisbecker precision:
2173fef20d9cSFrederic Weisbecker 	/* get the precision */
2174fef20d9cSFrederic Weisbecker 	spec->precision = -1;
2175fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
2176fef20d9cSFrederic Weisbecker 		++fmt;
2177fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
2178fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
2179fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
2180fef20d9cSFrederic Weisbecker 				spec->precision = 0;
2181fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
2182fef20d9cSFrederic Weisbecker 			/* it's the next argument */
2183adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
2184fef20d9cSFrederic Weisbecker 			return ++fmt - start;
2185fef20d9cSFrederic Weisbecker 		}
2186fef20d9cSFrederic Weisbecker 	}
2187fef20d9cSFrederic Weisbecker 
2188fef20d9cSFrederic Weisbecker qualifier:
2189fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
2190d0484193SRasmus Villemoes 	qualifier = 0;
219175fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
21925b5e0928SAlexey Dobriyan 	    *fmt == 'z' || *fmt == 't') {
2193d0484193SRasmus Villemoes 		qualifier = *fmt++;
2194d0484193SRasmus Villemoes 		if (unlikely(qualifier == *fmt)) {
2195d0484193SRasmus Villemoes 			if (qualifier == 'l') {
2196d0484193SRasmus Villemoes 				qualifier = 'L';
2197fef20d9cSFrederic Weisbecker 				++fmt;
2198d0484193SRasmus Villemoes 			} else if (qualifier == 'h') {
2199d0484193SRasmus Villemoes 				qualifier = 'H';
2200a4e94ef0SZhaolei 				++fmt;
2201a4e94ef0SZhaolei 			}
2202fef20d9cSFrederic Weisbecker 		}
2203fef20d9cSFrederic Weisbecker 	}
2204fef20d9cSFrederic Weisbecker 
2205fef20d9cSFrederic Weisbecker 	/* default base */
2206fef20d9cSFrederic Weisbecker 	spec->base = 10;
2207fef20d9cSFrederic Weisbecker 	switch (*fmt) {
2208fef20d9cSFrederic Weisbecker 	case 'c':
2209fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
2210fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2211fef20d9cSFrederic Weisbecker 
2212fef20d9cSFrederic Weisbecker 	case 's':
2213fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
2214fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2215fef20d9cSFrederic Weisbecker 
2216fef20d9cSFrederic Weisbecker 	case 'p':
2217fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
2218ffbfed03SRasmus Villemoes 		return ++fmt - start;
2219fef20d9cSFrederic Weisbecker 
2220fef20d9cSFrederic Weisbecker 	case '%':
2221fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
2222fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2223fef20d9cSFrederic Weisbecker 
2224fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
2225fef20d9cSFrederic Weisbecker 	case 'o':
2226fef20d9cSFrederic Weisbecker 		spec->base = 8;
2227fef20d9cSFrederic Weisbecker 		break;
2228fef20d9cSFrederic Weisbecker 
2229fef20d9cSFrederic Weisbecker 	case 'x':
2230fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
22317e6bd6f3SAndy Shevchenko 		/* fall through */
2232fef20d9cSFrederic Weisbecker 
2233fef20d9cSFrederic Weisbecker 	case 'X':
2234fef20d9cSFrederic Weisbecker 		spec->base = 16;
2235fef20d9cSFrederic Weisbecker 		break;
2236fef20d9cSFrederic Weisbecker 
2237fef20d9cSFrederic Weisbecker 	case 'd':
2238fef20d9cSFrederic Weisbecker 	case 'i':
223939e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
2240fef20d9cSFrederic Weisbecker 	case 'u':
2241fef20d9cSFrederic Weisbecker 		break;
2242fef20d9cSFrederic Weisbecker 
2243708d96fdSRyan Mallon 	case 'n':
2244708d96fdSRyan Mallon 		/*
2245b006f19bSRasmus Villemoes 		 * Since %n poses a greater security risk than
2246b006f19bSRasmus Villemoes 		 * utility, treat it as any other invalid or
2247b006f19bSRasmus Villemoes 		 * unsupported format specifier.
2248708d96fdSRyan Mallon 		 */
2249708d96fdSRyan Mallon 		/* Fall-through */
2250708d96fdSRyan Mallon 
2251fef20d9cSFrederic Weisbecker 	default:
2252b006f19bSRasmus Villemoes 		WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2253fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
2254fef20d9cSFrederic Weisbecker 		return fmt - start;
2255fef20d9cSFrederic Weisbecker 	}
2256fef20d9cSFrederic Weisbecker 
2257d0484193SRasmus Villemoes 	if (qualifier == 'L')
2258fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
2259d0484193SRasmus Villemoes 	else if (qualifier == 'l') {
226051be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
226151be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
22625b5e0928SAlexey Dobriyan 	} else if (qualifier == 'z') {
2263fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
2264d0484193SRasmus Villemoes 	} else if (qualifier == 't') {
2265fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
2266d0484193SRasmus Villemoes 	} else if (qualifier == 'H') {
226751be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
226851be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2269d0484193SRasmus Villemoes 	} else if (qualifier == 'h') {
227051be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
227151be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2272fef20d9cSFrederic Weisbecker 	} else {
227351be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
227451be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2275fef20d9cSFrederic Weisbecker 	}
2276fef20d9cSFrederic Weisbecker 
2277fef20d9cSFrederic Weisbecker 	return ++fmt - start;
227878a8bf69SLinus Torvalds }
227978a8bf69SLinus Torvalds 
22804d72ba01SRasmus Villemoes static void
22814d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width)
22824d72ba01SRasmus Villemoes {
22834d72ba01SRasmus Villemoes 	spec->field_width = width;
22844d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
22854d72ba01SRasmus Villemoes 		spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
22864d72ba01SRasmus Villemoes 	}
22874d72ba01SRasmus Villemoes }
22884d72ba01SRasmus Villemoes 
22894d72ba01SRasmus Villemoes static void
22904d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec)
22914d72ba01SRasmus Villemoes {
22924d72ba01SRasmus Villemoes 	spec->precision = prec;
22934d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
22944d72ba01SRasmus Villemoes 		spec->precision = clamp(prec, 0, PRECISION_MAX);
22954d72ba01SRasmus Villemoes 	}
22964d72ba01SRasmus Villemoes }
22974d72ba01SRasmus Villemoes 
22981da177e4SLinus Torvalds /**
22991da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
23001da177e4SLinus Torvalds  * @buf: The buffer to place the result into
23011da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
23021da177e4SLinus Torvalds  * @fmt: The format string to use
23031da177e4SLinus Torvalds  * @args: Arguments for the format string
23041da177e4SLinus Torvalds  *
2305d7ec9a05SRasmus Villemoes  * This function generally follows C99 vsnprintf, but has some
2306d7ec9a05SRasmus Villemoes  * extensions and a few limitations:
2307d7ec9a05SRasmus Villemoes  *
23086cc89134Smchehab@s-opensource.com  *  - ``%n`` is unsupported
23096cc89134Smchehab@s-opensource.com  *  - ``%p*`` is handled by pointer()
231020036fdcSAndi Kleen  *
231127e7c0e8SJonathan Corbet  * See pointer() or Documentation/core-api/printk-formats.rst for more
23125e4ee7b1SMartin Kletzander  * extensive description.
23135e4ee7b1SMartin Kletzander  *
23145e4ee7b1SMartin Kletzander  * **Please update the documentation in both places when making changes**
231580f548e0SAndrew Morton  *
23161da177e4SLinus Torvalds  * The return value is the number of characters which would
23171da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
23181da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
23191da177e4SLinus Torvalds  * number of characters written into @buf as return value
232072fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
23211da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
23221da177e4SLinus Torvalds  * string is truncated.
23231da177e4SLinus Torvalds  *
2324ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
23251da177e4SLinus Torvalds  */
23261da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
23271da177e4SLinus Torvalds {
23281da177e4SLinus Torvalds 	unsigned long long num;
2329d4be151bSAndré Goddard Rosa 	char *str, *end;
2330fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
23311da177e4SLinus Torvalds 
2332f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
2333f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
23342aa2f9e2SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
23351da177e4SLinus Torvalds 		return 0;
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds 	str = buf;
2338f796937aSJeremy Fitzhardinge 	end = buf + size;
23391da177e4SLinus Torvalds 
2340f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
2341f796937aSJeremy Fitzhardinge 	if (end < buf) {
23421da177e4SLinus Torvalds 		end = ((void *)-1);
2343f796937aSJeremy Fitzhardinge 		size = end - buf;
23441da177e4SLinus Torvalds 	}
23451da177e4SLinus Torvalds 
2346fef20d9cSFrederic Weisbecker 	while (*fmt) {
2347fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2348d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2349fef20d9cSFrederic Weisbecker 
2350fef20d9cSFrederic Weisbecker 		fmt += read;
2351fef20d9cSFrederic Weisbecker 
2352fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2353fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2354fef20d9cSFrederic Weisbecker 			int copy = read;
2355fef20d9cSFrederic Weisbecker 			if (str < end) {
2356fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2357fef20d9cSFrederic Weisbecker 					copy = end - str;
2358fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2359fef20d9cSFrederic Weisbecker 			}
2360fef20d9cSFrederic Weisbecker 			str += read;
2361fef20d9cSFrederic Weisbecker 			break;
23621da177e4SLinus Torvalds 		}
23631da177e4SLinus Torvalds 
2364ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
23654d72ba01SRasmus Villemoes 			set_field_width(&spec, va_arg(args, int));
2366fef20d9cSFrederic Weisbecker 			break;
23671da177e4SLinus Torvalds 
2368fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
23694d72ba01SRasmus Villemoes 			set_precision(&spec, va_arg(args, int));
2370fef20d9cSFrederic Weisbecker 			break;
23711da177e4SLinus Torvalds 
2372d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2373d4be151bSAndré Goddard Rosa 			char c;
2374d4be151bSAndré Goddard Rosa 
2375fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2376fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
2377f796937aSJeremy Fitzhardinge 					if (str < end)
23781da177e4SLinus Torvalds 						*str = ' ';
23791da177e4SLinus Torvalds 					++str;
2380fef20d9cSFrederic Weisbecker 
23811da177e4SLinus Torvalds 				}
23821da177e4SLinus Torvalds 			}
23831da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
2384f796937aSJeremy Fitzhardinge 			if (str < end)
23851da177e4SLinus Torvalds 				*str = c;
23861da177e4SLinus Torvalds 			++str;
2387fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
2388f796937aSJeremy Fitzhardinge 				if (str < end)
23891da177e4SLinus Torvalds 					*str = ' ';
23901da177e4SLinus Torvalds 				++str;
23911da177e4SLinus Torvalds 			}
2392fef20d9cSFrederic Weisbecker 			break;
2393d4be151bSAndré Goddard Rosa 		}
23941da177e4SLinus Torvalds 
2395fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
2396fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
2397fef20d9cSFrederic Weisbecker 			break;
23981da177e4SLinus Torvalds 
2399fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2400ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, va_arg(args, void *),
2401fef20d9cSFrederic Weisbecker 				      spec);
2402fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
24034d8a743cSLinus Torvalds 				fmt++;
2404fef20d9cSFrederic Weisbecker 			break;
24051da177e4SLinus Torvalds 
2406fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2407f796937aSJeremy Fitzhardinge 			if (str < end)
24081da177e4SLinus Torvalds 				*str = '%';
24091da177e4SLinus Torvalds 			++str;
24101da177e4SLinus Torvalds 			break;
24111da177e4SLinus Torvalds 
2412fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
2413b006f19bSRasmus Villemoes 			/*
2414b006f19bSRasmus Villemoes 			 * Presumably the arguments passed gcc's type
2415b006f19bSRasmus Villemoes 			 * checking, but there is no safe or sane way
2416b006f19bSRasmus Villemoes 			 * for us to continue parsing the format and
2417b006f19bSRasmus Villemoes 			 * fetching from the va_list; the remaining
2418b006f19bSRasmus Villemoes 			 * specifiers and arguments would be out of
2419b006f19bSRasmus Villemoes 			 * sync.
2420b006f19bSRasmus Villemoes 			 */
2421b006f19bSRasmus Villemoes 			goto out;
2422fef20d9cSFrederic Weisbecker 
2423fef20d9cSFrederic Weisbecker 		default:
2424fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2425fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2426fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
2427fef20d9cSFrederic Weisbecker 				break;
2428fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2429fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
2430fef20d9cSFrederic Weisbecker 				break;
2431fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2432fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
2433fef20d9cSFrederic Weisbecker 				break;
2434fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2435ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
2436ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
2437ef124960SJason Gunthorpe 				else
2438fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
2439fef20d9cSFrederic Weisbecker 				break;
2440fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2441fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
2442fef20d9cSFrederic Weisbecker 				break;
2443a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2444a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
2445a4e94ef0SZhaolei 				break;
2446a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2447a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
2448a4e94ef0SZhaolei 				break;
2449fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2450fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
2451fef20d9cSFrederic Weisbecker 				break;
2452fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2453fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
2454fef20d9cSFrederic Weisbecker 				break;
245539e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
245639e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
2457fef20d9cSFrederic Weisbecker 				break;
2458fef20d9cSFrederic Weisbecker 			default:
2459fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
24601da177e4SLinus Torvalds 			}
2461fef20d9cSFrederic Weisbecker 
2462fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
24631da177e4SLinus Torvalds 		}
2464fef20d9cSFrederic Weisbecker 	}
2465fef20d9cSFrederic Weisbecker 
2466b006f19bSRasmus Villemoes out:
2467f796937aSJeremy Fitzhardinge 	if (size > 0) {
2468f796937aSJeremy Fitzhardinge 		if (str < end)
24691da177e4SLinus Torvalds 			*str = '\0';
2470f796937aSJeremy Fitzhardinge 		else
24710a6047eeSLinus Torvalds 			end[-1] = '\0';
2472f796937aSJeremy Fitzhardinge 	}
2473fef20d9cSFrederic Weisbecker 
2474f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
24751da177e4SLinus Torvalds 	return str-buf;
2476fef20d9cSFrederic Weisbecker 
24771da177e4SLinus Torvalds }
24781da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
24791da177e4SLinus Torvalds 
24801da177e4SLinus Torvalds /**
24811da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
24821da177e4SLinus Torvalds  * @buf: The buffer to place the result into
24831da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
24841da177e4SLinus Torvalds  * @fmt: The format string to use
24851da177e4SLinus Torvalds  * @args: Arguments for the format string
24861da177e4SLinus Torvalds  *
24871da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
2488b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
24891da177e4SLinus Torvalds  * returns 0.
24901da177e4SLinus Torvalds  *
2491ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
249220036fdcSAndi Kleen  *
249320036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
24941da177e4SLinus Torvalds  */
24951da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
24961da177e4SLinus Torvalds {
24971da177e4SLinus Torvalds 	int i;
24981da177e4SLinus Torvalds 
24991da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
25007b9186f5SAndré Goddard Rosa 
2501b921c69fSAnton Arapov 	if (likely(i < size))
2502b921c69fSAnton Arapov 		return i;
2503b921c69fSAnton Arapov 	if (size != 0)
2504b921c69fSAnton Arapov 		return size - 1;
2505b921c69fSAnton Arapov 	return 0;
25061da177e4SLinus Torvalds }
25071da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds /**
25101da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
25111da177e4SLinus Torvalds  * @buf: The buffer to place the result into
25121da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
25131da177e4SLinus Torvalds  * @fmt: The format string to use
25141da177e4SLinus Torvalds  * @...: Arguments for the format string
25151da177e4SLinus Torvalds  *
25161da177e4SLinus Torvalds  * The return value is the number of characters which would be
25171da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
25181da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
25191da177e4SLinus Torvalds  * @size, the resulting string is truncated.
252020036fdcSAndi Kleen  *
252120036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
25221da177e4SLinus Torvalds  */
25231da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
25241da177e4SLinus Torvalds {
25251da177e4SLinus Torvalds 	va_list args;
25261da177e4SLinus Torvalds 	int i;
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 	va_start(args, fmt);
25291da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
25301da177e4SLinus Torvalds 	va_end(args);
25317b9186f5SAndré Goddard Rosa 
25321da177e4SLinus Torvalds 	return i;
25331da177e4SLinus Torvalds }
25341da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
25351da177e4SLinus Torvalds 
25361da177e4SLinus Torvalds /**
25371da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
25381da177e4SLinus Torvalds  * @buf: The buffer to place the result into
25391da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
25401da177e4SLinus Torvalds  * @fmt: The format string to use
25411da177e4SLinus Torvalds  * @...: Arguments for the format string
25421da177e4SLinus Torvalds  *
25431da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
2544b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
25451da177e4SLinus Torvalds  */
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
25481da177e4SLinus Torvalds {
25491da177e4SLinus Torvalds 	va_list args;
25501da177e4SLinus Torvalds 	int i;
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	va_start(args, fmt);
2553b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
25541da177e4SLinus Torvalds 	va_end(args);
25557b9186f5SAndré Goddard Rosa 
2556b903c0b8SChangli Gao 	return i;
25571da177e4SLinus Torvalds }
25581da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds /**
25611da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
25621da177e4SLinus Torvalds  * @buf: The buffer to place the result into
25631da177e4SLinus Torvalds  * @fmt: The format string to use
25641da177e4SLinus Torvalds  * @args: Arguments for the format string
25651da177e4SLinus Torvalds  *
25661da177e4SLinus Torvalds  * The function returns the number of characters written
256772fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
25681da177e4SLinus Torvalds  * buffer overflows.
25691da177e4SLinus Torvalds  *
2570ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
257120036fdcSAndi Kleen  *
257220036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
25731da177e4SLinus Torvalds  */
25741da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
25751da177e4SLinus Torvalds {
25761da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
25771da177e4SLinus Torvalds }
25781da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
25791da177e4SLinus Torvalds 
25801da177e4SLinus Torvalds /**
25811da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
25821da177e4SLinus Torvalds  * @buf: The buffer to place the result into
25831da177e4SLinus Torvalds  * @fmt: The format string to use
25841da177e4SLinus Torvalds  * @...: Arguments for the format string
25851da177e4SLinus Torvalds  *
25861da177e4SLinus Torvalds  * The function returns the number of characters written
258772fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
25881da177e4SLinus Torvalds  * buffer overflows.
258920036fdcSAndi Kleen  *
259020036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
25911da177e4SLinus Torvalds  */
25921da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
25931da177e4SLinus Torvalds {
25941da177e4SLinus Torvalds 	va_list args;
25951da177e4SLinus Torvalds 	int i;
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 	va_start(args, fmt);
25981da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
25991da177e4SLinus Torvalds 	va_end(args);
26007b9186f5SAndré Goddard Rosa 
26011da177e4SLinus Torvalds 	return i;
26021da177e4SLinus Torvalds }
26031da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
26041da177e4SLinus Torvalds 
26054370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
26064370aa4aSLai Jiangshan /*
26074370aa4aSLai Jiangshan  * bprintf service:
26084370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
26094370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
26104370aa4aSLai Jiangshan  */
26114370aa4aSLai Jiangshan 
26124370aa4aSLai Jiangshan /**
26134370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
26144370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
26154370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
26164370aa4aSLai Jiangshan  * @fmt: The format string to use
26174370aa4aSLai Jiangshan  * @args: Arguments for the format string
26184370aa4aSLai Jiangshan  *
26194370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
2620da3dae54SMasanari Iida  * is skipped.
26214370aa4aSLai Jiangshan  *
26224370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
26234370aa4aSLai Jiangshan  * the given input.
26244370aa4aSLai Jiangshan  *
26254370aa4aSLai Jiangshan  * NOTE:
26264370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
26274370aa4aSLai Jiangshan  * valid for bstr_printf().
26284370aa4aSLai Jiangshan  */
26294370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
26304370aa4aSLai Jiangshan {
2631fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
26324370aa4aSLai Jiangshan 	char *str, *end;
2633841a915dSSteven Rostedt (VMware) 	int width;
26344370aa4aSLai Jiangshan 
26354370aa4aSLai Jiangshan 	str = (char *)bin_buf;
26364370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
26374370aa4aSLai Jiangshan 
26384370aa4aSLai Jiangshan #define save_arg(type)							\
2639841a915dSSteven Rostedt (VMware) ({									\
26404370aa4aSLai Jiangshan 	unsigned long long value;					\
2641841a915dSSteven Rostedt (VMware) 	if (sizeof(type) == 8) {					\
2642841a915dSSteven Rostedt (VMware) 		unsigned long long val8;				\
26434370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
2644841a915dSSteven Rostedt (VMware) 		val8 = va_arg(args, unsigned long long);		\
26454370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
2646841a915dSSteven Rostedt (VMware) 			*(u32 *)str = *(u32 *)&val8;			\
2647841a915dSSteven Rostedt (VMware) 			*(u32 *)(str + 4) = *((u32 *)&val8 + 1);	\
26484370aa4aSLai Jiangshan 		}							\
2649841a915dSSteven Rostedt (VMware) 		value = val8;						\
26504370aa4aSLai Jiangshan 	} else {							\
2651841a915dSSteven Rostedt (VMware) 		unsigned int val4;					\
26524370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
2653841a915dSSteven Rostedt (VMware) 		val4 = va_arg(args, int);				\
26544370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
2655841a915dSSteven Rostedt (VMware) 			*(typeof(type) *)str = (type)(long)val4;	\
2656841a915dSSteven Rostedt (VMware) 		value = (unsigned long long)val4;			\
26574370aa4aSLai Jiangshan 	}								\
26584370aa4aSLai Jiangshan 	str += sizeof(type);						\
2659841a915dSSteven Rostedt (VMware) 	value;								\
2660841a915dSSteven Rostedt (VMware) })
26614370aa4aSLai Jiangshan 
2662fef20d9cSFrederic Weisbecker 	while (*fmt) {
2663d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
26644370aa4aSLai Jiangshan 
2665fef20d9cSFrederic Weisbecker 		fmt += read;
2666fef20d9cSFrederic Weisbecker 
2667fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2668fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
2669d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
2670fef20d9cSFrederic Weisbecker 			break;
2671b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
2672b006f19bSRasmus Villemoes 			goto out;
2673fef20d9cSFrederic Weisbecker 
2674ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2675fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2676841a915dSSteven Rostedt (VMware) 			width = (int)save_arg(int);
2677841a915dSSteven Rostedt (VMware) 			/* Pointers may require the width */
2678841a915dSSteven Rostedt (VMware) 			if (*fmt == 'p')
2679841a915dSSteven Rostedt (VMware) 				set_field_width(&spec, width);
2680fef20d9cSFrederic Weisbecker 			break;
26814370aa4aSLai Jiangshan 
2682fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
26834370aa4aSLai Jiangshan 			save_arg(char);
2684fef20d9cSFrederic Weisbecker 			break;
2685fef20d9cSFrederic Weisbecker 
2686fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
26874370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
26884370aa4aSLai Jiangshan 			size_t len;
26896c356634SAndré Goddard Rosa 
26904370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
26914370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
26920f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
26936c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
26946c356634SAndré Goddard Rosa 			if (str + len < end)
26956c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
26966c356634SAndré Goddard Rosa 			str += len;
2697fef20d9cSFrederic Weisbecker 			break;
26984370aa4aSLai Jiangshan 		}
2699fef20d9cSFrederic Weisbecker 
2700fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2701841a915dSSteven Rostedt (VMware) 			/* Dereferenced pointers must be done now */
2702841a915dSSteven Rostedt (VMware) 			switch (*fmt) {
2703841a915dSSteven Rostedt (VMware) 			/* Dereference of functions is still OK */
2704841a915dSSteven Rostedt (VMware) 			case 'S':
2705841a915dSSteven Rostedt (VMware) 			case 's':
2706841a915dSSteven Rostedt (VMware) 			case 'F':
2707841a915dSSteven Rostedt (VMware) 			case 'f':
27081e6338cfSSteven Rostedt (VMware) 			case 'x':
27091e6338cfSSteven Rostedt (VMware) 			case 'K':
27104370aa4aSLai Jiangshan 				save_arg(void *);
2711841a915dSSteven Rostedt (VMware) 				break;
2712841a915dSSteven Rostedt (VMware) 			default:
2713841a915dSSteven Rostedt (VMware) 				if (!isalnum(*fmt)) {
2714841a915dSSteven Rostedt (VMware) 					save_arg(void *);
2715841a915dSSteven Rostedt (VMware) 					break;
2716841a915dSSteven Rostedt (VMware) 				}
2717841a915dSSteven Rostedt (VMware) 				str = pointer(fmt, str, end, va_arg(args, void *),
2718841a915dSSteven Rostedt (VMware) 					      spec);
2719841a915dSSteven Rostedt (VMware) 				if (str + 1 < end)
2720841a915dSSteven Rostedt (VMware) 					*str++ = '\0';
2721841a915dSSteven Rostedt (VMware) 				else
2722841a915dSSteven Rostedt (VMware) 					end[-1] = '\0'; /* Must be nul terminated */
2723841a915dSSteven Rostedt (VMware) 			}
27244370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2725fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
27264370aa4aSLai Jiangshan 				fmt++;
2727fef20d9cSFrederic Weisbecker 			break;
2728fef20d9cSFrederic Weisbecker 
2729fef20d9cSFrederic Weisbecker 		default:
2730fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2731fef20d9cSFrederic Weisbecker 
2732fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2733fef20d9cSFrederic Weisbecker 				save_arg(long long);
2734fef20d9cSFrederic Weisbecker 				break;
2735fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2736fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2737fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
2738fef20d9cSFrederic Weisbecker 				break;
2739fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2740fef20d9cSFrederic Weisbecker 				save_arg(size_t);
2741fef20d9cSFrederic Weisbecker 				break;
2742fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2743fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
2744fef20d9cSFrederic Weisbecker 				break;
2745a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2746a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2747a4e94ef0SZhaolei 				save_arg(char);
2748a4e94ef0SZhaolei 				break;
2749fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2750fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2751fef20d9cSFrederic Weisbecker 				save_arg(short);
2752fef20d9cSFrederic Weisbecker 				break;
2753fef20d9cSFrederic Weisbecker 			default:
2754fef20d9cSFrederic Weisbecker 				save_arg(int);
2755fef20d9cSFrederic Weisbecker 			}
2756fef20d9cSFrederic Weisbecker 		}
2757fef20d9cSFrederic Weisbecker 	}
2758fef20d9cSFrederic Weisbecker 
2759b006f19bSRasmus Villemoes out:
27607b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2761fef20d9cSFrederic Weisbecker #undef save_arg
27624370aa4aSLai Jiangshan }
27634370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
27644370aa4aSLai Jiangshan 
27654370aa4aSLai Jiangshan /**
27664370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
27674370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
27684370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
27694370aa4aSLai Jiangshan  * @fmt: The format string to use
27704370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
27714370aa4aSLai Jiangshan  *
27724370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
27734370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
27744370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
27754370aa4aSLai Jiangshan  *
27764370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
27770efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
27784370aa4aSLai Jiangshan  *
27794370aa4aSLai Jiangshan  * The return value is the number of characters which would
27804370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
27814370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
27824370aa4aSLai Jiangshan  * number of characters written into @buf as return value
27834370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
27844370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
27854370aa4aSLai Jiangshan  * string is truncated.
27864370aa4aSLai Jiangshan  */
27874370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
27884370aa4aSLai Jiangshan {
2789fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
2790d4be151bSAndré Goddard Rosa 	char *str, *end;
2791d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
27924370aa4aSLai Jiangshan 
2793762abb51SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
27944370aa4aSLai Jiangshan 		return 0;
27954370aa4aSLai Jiangshan 
27964370aa4aSLai Jiangshan 	str = buf;
27974370aa4aSLai Jiangshan 	end = buf + size;
27984370aa4aSLai Jiangshan 
27994370aa4aSLai Jiangshan #define get_arg(type)							\
28004370aa4aSLai Jiangshan ({									\
28014370aa4aSLai Jiangshan 	typeof(type) value;						\
28024370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
28034370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
28044370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
28054370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
28064370aa4aSLai Jiangshan 	} else {							\
28074370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
28084370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
28094370aa4aSLai Jiangshan 	}								\
28104370aa4aSLai Jiangshan 	args += sizeof(type);						\
28114370aa4aSLai Jiangshan 	value;								\
28124370aa4aSLai Jiangshan })
28134370aa4aSLai Jiangshan 
28144370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
28154370aa4aSLai Jiangshan 	if (end < buf) {
28164370aa4aSLai Jiangshan 		end = ((void *)-1);
28174370aa4aSLai Jiangshan 		size = end - buf;
28184370aa4aSLai Jiangshan 	}
28194370aa4aSLai Jiangshan 
2820fef20d9cSFrederic Weisbecker 	while (*fmt) {
2821fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2822d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2823fef20d9cSFrederic Weisbecker 
2824fef20d9cSFrederic Weisbecker 		fmt += read;
2825fef20d9cSFrederic Weisbecker 
2826fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2827fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2828fef20d9cSFrederic Weisbecker 			int copy = read;
2829fef20d9cSFrederic Weisbecker 			if (str < end) {
2830fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2831fef20d9cSFrederic Weisbecker 					copy = end - str;
2832fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2833fef20d9cSFrederic Weisbecker 			}
2834fef20d9cSFrederic Weisbecker 			str += read;
2835fef20d9cSFrederic Weisbecker 			break;
28364370aa4aSLai Jiangshan 		}
28374370aa4aSLai Jiangshan 
2838ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
28394d72ba01SRasmus Villemoes 			set_field_width(&spec, get_arg(int));
2840fef20d9cSFrederic Weisbecker 			break;
28414370aa4aSLai Jiangshan 
2842fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
28434d72ba01SRasmus Villemoes 			set_precision(&spec, get_arg(int));
2844fef20d9cSFrederic Weisbecker 			break;
28454370aa4aSLai Jiangshan 
2846d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2847d4be151bSAndré Goddard Rosa 			char c;
2848d4be151bSAndré Goddard Rosa 
2849fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2850fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
28514370aa4aSLai Jiangshan 					if (str < end)
28524370aa4aSLai Jiangshan 						*str = ' ';
28534370aa4aSLai Jiangshan 					++str;
28544370aa4aSLai Jiangshan 				}
28554370aa4aSLai Jiangshan 			}
28564370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
28574370aa4aSLai Jiangshan 			if (str < end)
28584370aa4aSLai Jiangshan 				*str = c;
28594370aa4aSLai Jiangshan 			++str;
2860fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
28614370aa4aSLai Jiangshan 				if (str < end)
28624370aa4aSLai Jiangshan 					*str = ' ';
28634370aa4aSLai Jiangshan 				++str;
28644370aa4aSLai Jiangshan 			}
2865fef20d9cSFrederic Weisbecker 			break;
2866d4be151bSAndré Goddard Rosa 		}
28674370aa4aSLai Jiangshan 
2868fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
28694370aa4aSLai Jiangshan 			const char *str_arg = args;
2870d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
2871fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
2872fef20d9cSFrederic Weisbecker 			break;
28734370aa4aSLai Jiangshan 		}
28744370aa4aSLai Jiangshan 
2875841a915dSSteven Rostedt (VMware) 		case FORMAT_TYPE_PTR: {
2876841a915dSSteven Rostedt (VMware) 			bool process = false;
2877841a915dSSteven Rostedt (VMware) 			int copy, len;
2878841a915dSSteven Rostedt (VMware) 			/* Non function dereferences were already done */
2879841a915dSSteven Rostedt (VMware) 			switch (*fmt) {
2880841a915dSSteven Rostedt (VMware) 			case 'S':
2881841a915dSSteven Rostedt (VMware) 			case 's':
2882841a915dSSteven Rostedt (VMware) 			case 'F':
2883841a915dSSteven Rostedt (VMware) 			case 'f':
28841e6338cfSSteven Rostedt (VMware) 			case 'x':
28851e6338cfSSteven Rostedt (VMware) 			case 'K':
2886841a915dSSteven Rostedt (VMware) 				process = true;
2887841a915dSSteven Rostedt (VMware) 				break;
2888841a915dSSteven Rostedt (VMware) 			default:
2889841a915dSSteven Rostedt (VMware) 				if (!isalnum(*fmt)) {
2890841a915dSSteven Rostedt (VMware) 					process = true;
2891841a915dSSteven Rostedt (VMware) 					break;
2892841a915dSSteven Rostedt (VMware) 				}
2893841a915dSSteven Rostedt (VMware) 				/* Pointer dereference was already processed */
2894841a915dSSteven Rostedt (VMware) 				if (str < end) {
2895841a915dSSteven Rostedt (VMware) 					len = copy = strlen(args);
2896841a915dSSteven Rostedt (VMware) 					if (copy > end - str)
2897841a915dSSteven Rostedt (VMware) 						copy = end - str;
2898841a915dSSteven Rostedt (VMware) 					memcpy(str, args, copy);
2899841a915dSSteven Rostedt (VMware) 					str += len;
290062165600SSteven Rostedt (VMware) 					args += len + 1;
2901841a915dSSteven Rostedt (VMware) 				}
2902841a915dSSteven Rostedt (VMware) 			}
2903841a915dSSteven Rostedt (VMware) 			if (process)
2904ffbfed03SRasmus Villemoes 				str = pointer(fmt, str, end, get_arg(void *), spec);
2905841a915dSSteven Rostedt (VMware) 
2906fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
29074370aa4aSLai Jiangshan 				fmt++;
2908fef20d9cSFrederic Weisbecker 			break;
2909841a915dSSteven Rostedt (VMware) 		}
29104370aa4aSLai Jiangshan 
2911fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
29124370aa4aSLai Jiangshan 			if (str < end)
29134370aa4aSLai Jiangshan 				*str = '%';
29144370aa4aSLai Jiangshan 			++str;
2915fef20d9cSFrederic Weisbecker 			break;
2916fef20d9cSFrederic Weisbecker 
2917b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
2918b006f19bSRasmus Villemoes 			goto out;
2919b006f19bSRasmus Villemoes 
2920d4be151bSAndré Goddard Rosa 		default: {
2921d4be151bSAndré Goddard Rosa 			unsigned long long num;
2922d4be151bSAndré Goddard Rosa 
2923fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2924fef20d9cSFrederic Weisbecker 
2925fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
29264370aa4aSLai Jiangshan 				num = get_arg(long long);
2927fef20d9cSFrederic Weisbecker 				break;
2928fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2929fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2930fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
2931fef20d9cSFrederic Weisbecker 				break;
2932fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
29334370aa4aSLai Jiangshan 				num = get_arg(size_t);
2934fef20d9cSFrederic Weisbecker 				break;
2935fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
29364370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
2937fef20d9cSFrederic Weisbecker 				break;
2938a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2939a4e94ef0SZhaolei 				num = get_arg(unsigned char);
2940a4e94ef0SZhaolei 				break;
2941a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2942a4e94ef0SZhaolei 				num = get_arg(signed char);
2943a4e94ef0SZhaolei 				break;
2944fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2945fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
2946fef20d9cSFrederic Weisbecker 				break;
2947fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2948fef20d9cSFrederic Weisbecker 				num = get_arg(short);
2949fef20d9cSFrederic Weisbecker 				break;
2950fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
29514370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
2952fef20d9cSFrederic Weisbecker 				break;
2953fef20d9cSFrederic Weisbecker 			default:
2954fef20d9cSFrederic Weisbecker 				num = get_arg(int);
29554370aa4aSLai Jiangshan 			}
2956fef20d9cSFrederic Weisbecker 
2957fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
2958d4be151bSAndré Goddard Rosa 		} /* default: */
2959d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
2960d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
2961fef20d9cSFrederic Weisbecker 
2962b006f19bSRasmus Villemoes out:
29634370aa4aSLai Jiangshan 	if (size > 0) {
29644370aa4aSLai Jiangshan 		if (str < end)
29654370aa4aSLai Jiangshan 			*str = '\0';
29664370aa4aSLai Jiangshan 		else
29674370aa4aSLai Jiangshan 			end[-1] = '\0';
29684370aa4aSLai Jiangshan 	}
2969fef20d9cSFrederic Weisbecker 
29704370aa4aSLai Jiangshan #undef get_arg
29714370aa4aSLai Jiangshan 
29724370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
29734370aa4aSLai Jiangshan 	return str - buf;
29744370aa4aSLai Jiangshan }
29754370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
29764370aa4aSLai Jiangshan 
29774370aa4aSLai Jiangshan /**
29784370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
29794370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
29804370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
29814370aa4aSLai Jiangshan  * @fmt: The format string to use
29824370aa4aSLai Jiangshan  * @...: Arguments for the format string
29834370aa4aSLai Jiangshan  *
29844370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
29854370aa4aSLai Jiangshan  * into @bin_buf.
29864370aa4aSLai Jiangshan  */
29874370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
29884370aa4aSLai Jiangshan {
29894370aa4aSLai Jiangshan 	va_list args;
29904370aa4aSLai Jiangshan 	int ret;
29914370aa4aSLai Jiangshan 
29924370aa4aSLai Jiangshan 	va_start(args, fmt);
29934370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
29944370aa4aSLai Jiangshan 	va_end(args);
29957b9186f5SAndré Goddard Rosa 
29964370aa4aSLai Jiangshan 	return ret;
29974370aa4aSLai Jiangshan }
29984370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
29994370aa4aSLai Jiangshan 
30004370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
30014370aa4aSLai Jiangshan 
30021da177e4SLinus Torvalds /**
30031da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
30041da177e4SLinus Torvalds  * @buf:	input buffer
30051da177e4SLinus Torvalds  * @fmt:	format of buffer
30061da177e4SLinus Torvalds  * @args:	arguments
30071da177e4SLinus Torvalds  */
30081da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
30091da177e4SLinus Torvalds {
30101da177e4SLinus Torvalds 	const char *str = buf;
30111da177e4SLinus Torvalds 	char *next;
30121da177e4SLinus Torvalds 	char digit;
30131da177e4SLinus Torvalds 	int num = 0;
3014ef0658f3SJoe Perches 	u8 qualifier;
301553809751SJan Beulich 	unsigned int base;
301653809751SJan Beulich 	union {
301753809751SJan Beulich 		long long s;
301853809751SJan Beulich 		unsigned long long u;
301953809751SJan Beulich 	} val;
3020ef0658f3SJoe Perches 	s16 field_width;
3021d4be151bSAndré Goddard Rosa 	bool is_sign;
30221da177e4SLinus Torvalds 
3023da99075cSJan Beulich 	while (*fmt) {
30241da177e4SLinus Torvalds 		/* skip any white space in format */
30251da177e4SLinus Torvalds 		/* white space in format matchs any amount of
30261da177e4SLinus Torvalds 		 * white space, including none, in the input.
30271da177e4SLinus Torvalds 		 */
30281da177e4SLinus Torvalds 		if (isspace(*fmt)) {
3029e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
3030e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
30311da177e4SLinus Torvalds 		}
30321da177e4SLinus Torvalds 
30331da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
30341da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
30351da177e4SLinus Torvalds 			if (*fmt++ != *str++)
30361da177e4SLinus Torvalds 				break;
30371da177e4SLinus Torvalds 			continue;
30381da177e4SLinus Torvalds 		}
30391da177e4SLinus Torvalds 
30401da177e4SLinus Torvalds 		if (!*fmt)
30411da177e4SLinus Torvalds 			break;
30421da177e4SLinus Torvalds 		++fmt;
30431da177e4SLinus Torvalds 
30441da177e4SLinus Torvalds 		/* skip this conversion.
30451da177e4SLinus Torvalds 		 * advance both strings to next white space
30461da177e4SLinus Torvalds 		 */
30471da177e4SLinus Torvalds 		if (*fmt == '*') {
3048da99075cSJan Beulich 			if (!*str)
3049da99075cSJan Beulich 				break;
3050f9310b2fSJessica Yu 			while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3051f9310b2fSJessica Yu 				/* '%*[' not yet supported, invalid format */
3052f9310b2fSJessica Yu 				if (*fmt == '[')
3053f9310b2fSJessica Yu 					return num;
30541da177e4SLinus Torvalds 				fmt++;
3055f9310b2fSJessica Yu 			}
30561da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
30571da177e4SLinus Torvalds 				str++;
30581da177e4SLinus Torvalds 			continue;
30591da177e4SLinus Torvalds 		}
30601da177e4SLinus Torvalds 
30611da177e4SLinus Torvalds 		/* get field width */
30621da177e4SLinus Torvalds 		field_width = -1;
306353809751SJan Beulich 		if (isdigit(*fmt)) {
30641da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
306553809751SJan Beulich 			if (field_width <= 0)
306653809751SJan Beulich 				break;
306753809751SJan Beulich 		}
30681da177e4SLinus Torvalds 
30691da177e4SLinus Torvalds 		/* get conversion qualifier */
30701da177e4SLinus Torvalds 		qualifier = -1;
307175fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
30725b5e0928SAlexey Dobriyan 		    *fmt == 'z') {
30731da177e4SLinus Torvalds 			qualifier = *fmt++;
30741da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
30751da177e4SLinus Torvalds 				if (qualifier == 'h') {
30761da177e4SLinus Torvalds 					qualifier = 'H';
30771da177e4SLinus Torvalds 					fmt++;
30781da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
30791da177e4SLinus Torvalds 					qualifier = 'L';
30801da177e4SLinus Torvalds 					fmt++;
30811da177e4SLinus Torvalds 				}
30821da177e4SLinus Torvalds 			}
30831da177e4SLinus Torvalds 		}
30841da177e4SLinus Torvalds 
3085da99075cSJan Beulich 		if (!*fmt)
3086da99075cSJan Beulich 			break;
3087da99075cSJan Beulich 
3088da99075cSJan Beulich 		if (*fmt == 'n') {
3089da99075cSJan Beulich 			/* return number of characters read so far */
3090da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
3091da99075cSJan Beulich 			++fmt;
3092da99075cSJan Beulich 			continue;
3093da99075cSJan Beulich 		}
3094da99075cSJan Beulich 
3095da99075cSJan Beulich 		if (!*str)
30961da177e4SLinus Torvalds 			break;
30971da177e4SLinus Torvalds 
3098d4be151bSAndré Goddard Rosa 		base = 10;
30993f623ebaSFabian Frederick 		is_sign = false;
3100d4be151bSAndré Goddard Rosa 
31011da177e4SLinus Torvalds 		switch (*fmt++) {
31021da177e4SLinus Torvalds 		case 'c':
31031da177e4SLinus Torvalds 		{
31041da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
31051da177e4SLinus Torvalds 			if (field_width == -1)
31061da177e4SLinus Torvalds 				field_width = 1;
31071da177e4SLinus Torvalds 			do {
31081da177e4SLinus Torvalds 				*s++ = *str++;
31091da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
31101da177e4SLinus Torvalds 			num++;
31111da177e4SLinus Torvalds 		}
31121da177e4SLinus Torvalds 		continue;
31131da177e4SLinus Torvalds 		case 's':
31141da177e4SLinus Torvalds 		{
31151da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
31161da177e4SLinus Torvalds 			if (field_width == -1)
31174be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
31181da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
3119e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
31201da177e4SLinus Torvalds 
31211da177e4SLinus Torvalds 			/* now copy until next white space */
31227b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
31231da177e4SLinus Torvalds 				*s++ = *str++;
31241da177e4SLinus Torvalds 			*s = '\0';
31251da177e4SLinus Torvalds 			num++;
31261da177e4SLinus Torvalds 		}
31271da177e4SLinus Torvalds 		continue;
3128f9310b2fSJessica Yu 		/*
3129f9310b2fSJessica Yu 		 * Warning: This implementation of the '[' conversion specifier
3130f9310b2fSJessica Yu 		 * deviates from its glibc counterpart in the following ways:
3131f9310b2fSJessica Yu 		 * (1) It does NOT support ranges i.e. '-' is NOT a special
3132f9310b2fSJessica Yu 		 *     character
3133f9310b2fSJessica Yu 		 * (2) It cannot match the closing bracket ']' itself
3134f9310b2fSJessica Yu 		 * (3) A field width is required
3135f9310b2fSJessica Yu 		 * (4) '%*[' (discard matching input) is currently not supported
3136f9310b2fSJessica Yu 		 *
3137f9310b2fSJessica Yu 		 * Example usage:
3138f9310b2fSJessica Yu 		 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3139f9310b2fSJessica Yu 		 *		buf1, buf2, buf3);
3140f9310b2fSJessica Yu 		 * if (ret < 3)
3141f9310b2fSJessica Yu 		 *    // etc..
3142f9310b2fSJessica Yu 		 */
3143f9310b2fSJessica Yu 		case '[':
3144f9310b2fSJessica Yu 		{
3145f9310b2fSJessica Yu 			char *s = (char *)va_arg(args, char *);
3146f9310b2fSJessica Yu 			DECLARE_BITMAP(set, 256) = {0};
3147f9310b2fSJessica Yu 			unsigned int len = 0;
3148f9310b2fSJessica Yu 			bool negate = (*fmt == '^');
3149f9310b2fSJessica Yu 
3150f9310b2fSJessica Yu 			/* field width is required */
3151f9310b2fSJessica Yu 			if (field_width == -1)
3152f9310b2fSJessica Yu 				return num;
3153f9310b2fSJessica Yu 
3154f9310b2fSJessica Yu 			if (negate)
3155f9310b2fSJessica Yu 				++fmt;
3156f9310b2fSJessica Yu 
3157f9310b2fSJessica Yu 			for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3158f9310b2fSJessica Yu 				set_bit((u8)*fmt, set);
3159f9310b2fSJessica Yu 
3160f9310b2fSJessica Yu 			/* no ']' or no character set found */
3161f9310b2fSJessica Yu 			if (!*fmt || !len)
3162f9310b2fSJessica Yu 				return num;
3163f9310b2fSJessica Yu 			++fmt;
3164f9310b2fSJessica Yu 
3165f9310b2fSJessica Yu 			if (negate) {
3166f9310b2fSJessica Yu 				bitmap_complement(set, set, 256);
3167f9310b2fSJessica Yu 				/* exclude null '\0' byte */
3168f9310b2fSJessica Yu 				clear_bit(0, set);
3169f9310b2fSJessica Yu 			}
3170f9310b2fSJessica Yu 
3171f9310b2fSJessica Yu 			/* match must be non-empty */
3172f9310b2fSJessica Yu 			if (!test_bit((u8)*str, set))
3173f9310b2fSJessica Yu 				return num;
3174f9310b2fSJessica Yu 
3175f9310b2fSJessica Yu 			while (test_bit((u8)*str, set) && field_width--)
3176f9310b2fSJessica Yu 				*s++ = *str++;
3177f9310b2fSJessica Yu 			*s = '\0';
3178f9310b2fSJessica Yu 			++num;
3179f9310b2fSJessica Yu 		}
3180f9310b2fSJessica Yu 		continue;
31811da177e4SLinus Torvalds 		case 'o':
31821da177e4SLinus Torvalds 			base = 8;
31831da177e4SLinus Torvalds 			break;
31841da177e4SLinus Torvalds 		case 'x':
31851da177e4SLinus Torvalds 		case 'X':
31861da177e4SLinus Torvalds 			base = 16;
31871da177e4SLinus Torvalds 			break;
31881da177e4SLinus Torvalds 		case 'i':
31891da177e4SLinus Torvalds 			base = 0;
31907e6bd6f3SAndy Shevchenko 			/* fall through */
31911da177e4SLinus Torvalds 		case 'd':
31923f623ebaSFabian Frederick 			is_sign = true;
31937e6bd6f3SAndy Shevchenko 			/* fall through */
31941da177e4SLinus Torvalds 		case 'u':
31951da177e4SLinus Torvalds 			break;
31961da177e4SLinus Torvalds 		case '%':
31971da177e4SLinus Torvalds 			/* looking for '%' in str */
31981da177e4SLinus Torvalds 			if (*str++ != '%')
31991da177e4SLinus Torvalds 				return num;
32001da177e4SLinus Torvalds 			continue;
32011da177e4SLinus Torvalds 		default:
32021da177e4SLinus Torvalds 			/* invalid format; stop here */
32031da177e4SLinus Torvalds 			return num;
32041da177e4SLinus Torvalds 		}
32051da177e4SLinus Torvalds 
32061da177e4SLinus Torvalds 		/* have some sort of integer conversion.
32071da177e4SLinus Torvalds 		 * first, skip white space in buffer.
32081da177e4SLinus Torvalds 		 */
3209e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
32101da177e4SLinus Torvalds 
32111da177e4SLinus Torvalds 		digit = *str;
32121da177e4SLinus Torvalds 		if (is_sign && digit == '-')
32131da177e4SLinus Torvalds 			digit = *(str + 1);
32141da177e4SLinus Torvalds 
32151da177e4SLinus Torvalds 		if (!digit
32161da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
32171da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
32181da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
32191da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
32201da177e4SLinus Torvalds 			break;
32211da177e4SLinus Torvalds 
322253809751SJan Beulich 		if (is_sign)
322353809751SJan Beulich 			val.s = qualifier != 'L' ?
322453809751SJan Beulich 				simple_strtol(str, &next, base) :
322553809751SJan Beulich 				simple_strtoll(str, &next, base);
322653809751SJan Beulich 		else
322753809751SJan Beulich 			val.u = qualifier != 'L' ?
322853809751SJan Beulich 				simple_strtoul(str, &next, base) :
322953809751SJan Beulich 				simple_strtoull(str, &next, base);
323053809751SJan Beulich 
323153809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
323253809751SJan Beulich 			if (base == 0)
323353809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
323453809751SJan Beulich 			while (next - str > field_width) {
323553809751SJan Beulich 				if (is_sign)
323653809751SJan Beulich 					val.s = div_s64(val.s, base);
323753809751SJan Beulich 				else
323853809751SJan Beulich 					val.u = div_u64(val.u, base);
323953809751SJan Beulich 				--next;
324053809751SJan Beulich 			}
324153809751SJan Beulich 		}
324253809751SJan Beulich 
32431da177e4SLinus Torvalds 		switch (qualifier) {
32441da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
324553809751SJan Beulich 			if (is_sign)
324653809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
324753809751SJan Beulich 			else
324853809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
32491da177e4SLinus Torvalds 			break;
32501da177e4SLinus Torvalds 		case 'h':
325153809751SJan Beulich 			if (is_sign)
325253809751SJan Beulich 				*va_arg(args, short *) = val.s;
325353809751SJan Beulich 			else
325453809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
32551da177e4SLinus Torvalds 			break;
32561da177e4SLinus Torvalds 		case 'l':
325753809751SJan Beulich 			if (is_sign)
325853809751SJan Beulich 				*va_arg(args, long *) = val.s;
325953809751SJan Beulich 			else
326053809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
32611da177e4SLinus Torvalds 			break;
32621da177e4SLinus Torvalds 		case 'L':
326353809751SJan Beulich 			if (is_sign)
326453809751SJan Beulich 				*va_arg(args, long long *) = val.s;
326553809751SJan Beulich 			else
326653809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
32671da177e4SLinus Torvalds 			break;
32681da177e4SLinus Torvalds 		case 'z':
326953809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
32701da177e4SLinus Torvalds 			break;
32711da177e4SLinus Torvalds 		default:
327253809751SJan Beulich 			if (is_sign)
327353809751SJan Beulich 				*va_arg(args, int *) = val.s;
327453809751SJan Beulich 			else
327553809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
32761da177e4SLinus Torvalds 			break;
32771da177e4SLinus Torvalds 		}
32781da177e4SLinus Torvalds 		num++;
32791da177e4SLinus Torvalds 
32801da177e4SLinus Torvalds 		if (!next)
32811da177e4SLinus Torvalds 			break;
32821da177e4SLinus Torvalds 		str = next;
32831da177e4SLinus Torvalds 	}
3284c6b40d16SJohannes Berg 
32851da177e4SLinus Torvalds 	return num;
32861da177e4SLinus Torvalds }
32871da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
32881da177e4SLinus Torvalds 
32891da177e4SLinus Torvalds /**
32901da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
32911da177e4SLinus Torvalds  * @buf:	input buffer
32921da177e4SLinus Torvalds  * @fmt:	formatting of buffer
32931da177e4SLinus Torvalds  * @...:	resulting arguments
32941da177e4SLinus Torvalds  */
32951da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
32961da177e4SLinus Torvalds {
32971da177e4SLinus Torvalds 	va_list args;
32981da177e4SLinus Torvalds 	int i;
32991da177e4SLinus Torvalds 
33001da177e4SLinus Torvalds 	va_start(args, fmt);
33011da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
33021da177e4SLinus Torvalds 	va_end(args);
33037b9186f5SAndré Goddard Rosa 
33041da177e4SLinus Torvalds 	return i;
33051da177e4SLinus Torvalds }
33061da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
3307