xref: /openbmc/linux/lib/vsprintf.c (revision 4d72ba014b4b0913f448ccaaaa2e8b39c54e3738)
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>
338a27f7c9SJoe Perches #include <net/addrconf.h>
341031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
351031bc58SDmitry Monakhov #include <linux/blkdev.h>
361031bc58SDmitry Monakhov #endif
371da177e4SLinus Torvalds 
384e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
39deac93dfSJames Bottomley #include <asm/sections.h>	/* for dereference_function_descriptor() */
407c43d9a3SRasmus Villemoes #include <asm/byteorder.h>	/* cpu_to_le16 */
411da177e4SLinus Torvalds 
4271dca95dSAndy Shevchenko #include <linux/string_helpers.h>
431dff46d6SAlexey Dobriyan #include "kstrtox.h"
44aa46a63eSHarvey Harrison 
451da177e4SLinus Torvalds /**
461da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
471da177e4SLinus Torvalds  * @cp: The start of the string
481da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
491da177e4SLinus Torvalds  * @base: The number base to use
50462e4711SEldad Zack  *
51462e4711SEldad Zack  * This function is obsolete. Please use kstrtoull instead.
521da177e4SLinus Torvalds  */
531da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
541da177e4SLinus Torvalds {
551dff46d6SAlexey Dobriyan 	unsigned long long result;
561dff46d6SAlexey Dobriyan 	unsigned int rv;
571da177e4SLinus Torvalds 
581dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
591dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
601dff46d6SAlexey Dobriyan 	/* FIXME */
611dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
62aa46a63eSHarvey Harrison 
631da177e4SLinus Torvalds 	if (endp)
641da177e4SLinus Torvalds 		*endp = (char *)cp;
657b9186f5SAndré Goddard Rosa 
661da177e4SLinus Torvalds 	return result;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /**
71922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
72922ac25cSAndré Goddard Rosa  * @cp: The start of the string
73922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
74922ac25cSAndré Goddard Rosa  * @base: The number base to use
75462e4711SEldad Zack  *
76462e4711SEldad Zack  * This function is obsolete. Please use kstrtoul instead.
77922ac25cSAndré Goddard Rosa  */
78922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
79922ac25cSAndré Goddard Rosa {
80922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
81922ac25cSAndré Goddard Rosa }
82922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
83922ac25cSAndré Goddard Rosa 
84922ac25cSAndré Goddard Rosa /**
85922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
86922ac25cSAndré Goddard Rosa  * @cp: The start of the string
87922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
88922ac25cSAndré Goddard Rosa  * @base: The number base to use
89462e4711SEldad Zack  *
90462e4711SEldad Zack  * This function is obsolete. Please use kstrtol instead.
91922ac25cSAndré Goddard Rosa  */
92922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
93922ac25cSAndré Goddard Rosa {
94922ac25cSAndré Goddard Rosa 	if (*cp == '-')
95922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
96922ac25cSAndré Goddard Rosa 
97922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
98922ac25cSAndré Goddard Rosa }
99922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
100922ac25cSAndré Goddard Rosa 
101922ac25cSAndré Goddard Rosa /**
1021da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
1031da177e4SLinus Torvalds  * @cp: The start of the string
1041da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1051da177e4SLinus Torvalds  * @base: The number base to use
106462e4711SEldad Zack  *
107462e4711SEldad Zack  * This function is obsolete. Please use kstrtoll instead.
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	if (*cp == '-')
1121da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1137b9186f5SAndré Goddard Rosa 
1141da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1151da177e4SLinus Torvalds }
11698d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1171da177e4SLinus Torvalds 
118cf3b429bSJoe Perches static noinline_for_stack
119cf3b429bSJoe Perches int skip_atoi(const char **s)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	int i = 0;
1221da177e4SLinus Torvalds 
12343e5b666SRasmus Villemoes 	do {
1241da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
12543e5b666SRasmus Villemoes 	} while (isdigit(**s));
1267b9186f5SAndré Goddard Rosa 
1271da177e4SLinus Torvalds 	return i;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
1307c43d9a3SRasmus Villemoes /*
1317c43d9a3SRasmus Villemoes  * Decimal conversion is by far the most typical, and is used for
1327c43d9a3SRasmus Villemoes  * /proc and /sys data. This directly impacts e.g. top performance
1337c43d9a3SRasmus Villemoes  * with many processes running. We optimize it for speed by emitting
1347c43d9a3SRasmus Villemoes  * two characters at a time, using a 200 byte lookup table. This
1357c43d9a3SRasmus Villemoes  * roughly halves the number of multiplications compared to computing
1367c43d9a3SRasmus Villemoes  * the digits one at a time. Implementation strongly inspired by the
1377c43d9a3SRasmus Villemoes  * previous version, which in turn used ideas described at
1387c43d9a3SRasmus Villemoes  * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
1397c43d9a3SRasmus Villemoes  * from the author, Douglas W. Jones).
1407c43d9a3SRasmus Villemoes  *
1417c43d9a3SRasmus Villemoes  * It turns out there is precisely one 26 bit fixed-point
1427c43d9a3SRasmus Villemoes  * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
1437c43d9a3SRasmus Villemoes  * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
1447c43d9a3SRasmus Villemoes  * range happens to be somewhat larger (x <= 1073741898), but that's
1457c43d9a3SRasmus Villemoes  * irrelevant for our purpose.
1467c43d9a3SRasmus Villemoes  *
1477c43d9a3SRasmus Villemoes  * For dividing a number in the range [10^4, 10^6-1] by 100, we still
1487c43d9a3SRasmus Villemoes  * need a 32x32->64 bit multiply, so we simply use the same constant.
1497c43d9a3SRasmus Villemoes  *
1507c43d9a3SRasmus Villemoes  * For dividing a number in the range [100, 10^4-1] by 100, there are
1517c43d9a3SRasmus Villemoes  * several options. The simplest is (x * 0x147b) >> 19, which is valid
1527c43d9a3SRasmus Villemoes  * for all x <= 43698.
153133fd9f5SDenys Vlasenko  */
1544277eeddSDenis Vlasenko 
1557c43d9a3SRasmus Villemoes static const u16 decpair[100] = {
1567c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
1577c43d9a3SRasmus Villemoes 	_( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
1587c43d9a3SRasmus Villemoes 	_(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
1597c43d9a3SRasmus Villemoes 	_(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
1607c43d9a3SRasmus Villemoes 	_(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
1617c43d9a3SRasmus Villemoes 	_(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
1627c43d9a3SRasmus Villemoes 	_(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
1637c43d9a3SRasmus Villemoes 	_(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
1647c43d9a3SRasmus Villemoes 	_(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
1657c43d9a3SRasmus Villemoes 	_(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
1667c43d9a3SRasmus Villemoes 	_(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
1677c43d9a3SRasmus Villemoes #undef _
1687c43d9a3SRasmus Villemoes };
1694277eeddSDenis Vlasenko 
1707b9186f5SAndré Goddard Rosa /*
1717c43d9a3SRasmus Villemoes  * This will print a single '0' even if r == 0, since we would
172675cf53cSRasmus Villemoes  * immediately jump to out_r where two 0s would be written but only
173675cf53cSRasmus Villemoes  * one of them accounted for in buf. This is needed by ip4_string
174675cf53cSRasmus Villemoes  * below. All other callers pass a non-zero value of r.
175133fd9f5SDenys Vlasenko */
176133fd9f5SDenys Vlasenko static noinline_for_stack
177133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
178133fd9f5SDenys Vlasenko {
179133fd9f5SDenys Vlasenko 	unsigned q;
1804277eeddSDenis Vlasenko 
1817c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^8 */
1827c43d9a3SRasmus Villemoes 	if (r < 100)
1837c43d9a3SRasmus Villemoes 		goto out_r;
184cb239d0aSGeorge Spelvin 
1857c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^8 */
1867c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
1877c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
1887c43d9a3SRasmus Villemoes 	buf += 2;
1897c43d9a3SRasmus Villemoes 
1907c43d9a3SRasmus Villemoes 	/* 1 <= q < 10^6 */
1917c43d9a3SRasmus Villemoes 	if (q < 100)
1927c43d9a3SRasmus Villemoes 		goto out_q;
1937c43d9a3SRasmus Villemoes 
1947c43d9a3SRasmus Villemoes 	/*  100 <= q < 10^6 */
1957c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
1967c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
1977c43d9a3SRasmus Villemoes 	buf += 2;
1987c43d9a3SRasmus Villemoes 
1997c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^4 */
2007c43d9a3SRasmus Villemoes 	if (r < 100)
2017c43d9a3SRasmus Villemoes 		goto out_r;
2027c43d9a3SRasmus Villemoes 
2037c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^4 */
2047c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2057c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2067c43d9a3SRasmus Villemoes 	buf += 2;
2077c43d9a3SRasmus Villemoes out_q:
2087c43d9a3SRasmus Villemoes 	/* 1 <= q < 100 */
2097c43d9a3SRasmus Villemoes 	r = q;
2107c43d9a3SRasmus Villemoes out_r:
2117c43d9a3SRasmus Villemoes 	/* 1 <= r < 100 */
2127c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r];
213675cf53cSRasmus Villemoes 	buf += r < 10 ? 1 : 2;
214133fd9f5SDenys Vlasenko 	return buf;
215133fd9f5SDenys Vlasenko }
216133fd9f5SDenys Vlasenko 
2177c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
2187c43d9a3SRasmus Villemoes static noinline_for_stack
2197c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r)
2207c43d9a3SRasmus Villemoes {
2217c43d9a3SRasmus Villemoes 	unsigned q;
222133fd9f5SDenys Vlasenko 
2237c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^8 */
2247c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
2257c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2267c43d9a3SRasmus Villemoes 	buf += 2;
227133fd9f5SDenys Vlasenko 
2287c43d9a3SRasmus Villemoes 	/* 0 <= q < 10^6 */
2297c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
2307c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
2317c43d9a3SRasmus Villemoes 	buf += 2;
232133fd9f5SDenys Vlasenko 
2337c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2347c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2357c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2367c43d9a3SRasmus Villemoes 	buf += 2;
2377c43d9a3SRasmus Villemoes 
2387c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2397c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2407c43d9a3SRasmus Villemoes 	buf += 2;
2417c43d9a3SRasmus Villemoes 	return buf;
2427c43d9a3SRasmus Villemoes }
2437c43d9a3SRasmus Villemoes 
2447c43d9a3SRasmus Villemoes static noinline_for_stack
245133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
246133fd9f5SDenys Vlasenko {
247133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000)
2487c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2497c43d9a3SRasmus Villemoes 	/* 1 <= n <= 1.6e11 */
2507c43d9a3SRasmus Villemoes 	if (n >= 100*1000*1000)
2517c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2527c43d9a3SRasmus Villemoes 	/* 1 <= n < 1e8 */
253133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
254133fd9f5SDenys Vlasenko }
255133fd9f5SDenys Vlasenko 
2567c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
257133fd9f5SDenys Vlasenko 
2587c43d9a3SRasmus Villemoes static void
2597c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r)
260133fd9f5SDenys Vlasenko {
2617c43d9a3SRasmus Villemoes 	unsigned q;
2627c43d9a3SRasmus Villemoes 
2637c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2647c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2657c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2667c43d9a3SRasmus Villemoes 	buf += 2;
2677c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2687c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2692359172aSGeorge Spelvin }
2702359172aSGeorge Spelvin 
2712359172aSGeorge Spelvin /*
2722359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2732359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2742359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2752359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
2767c43d9a3SRasmus Villemoes  * (second call in the put_dec code, assuming n is all-ones).
2772359172aSGeorge Spelvin  */
2787c43d9a3SRasmus Villemoes static noinline_for_stack
2792359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2802359172aSGeorge Spelvin {
2812359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2822359172aSGeorge Spelvin 
2832359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2842359172aSGeorge Spelvin         return q;
285133fd9f5SDenys Vlasenko }
286133fd9f5SDenys Vlasenko 
287133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
288133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
289133fd9f5SDenys Vlasenko  * (with permission from the author).
290133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
291133fd9f5SDenys Vlasenko  */
292133fd9f5SDenys Vlasenko static
293133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
294133fd9f5SDenys Vlasenko {
295133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
296133fd9f5SDenys Vlasenko 
297133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
298133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
299133fd9f5SDenys Vlasenko 
300133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
301133fd9f5SDenys Vlasenko 	h   = (n >> 32);
302133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
303133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
304133fd9f5SDenys Vlasenko 
3057c43d9a3SRasmus Villemoes 	/* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
3067c43d9a3SRasmus Villemoes 	     = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
307133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
3082359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
309133fd9f5SDenys Vlasenko 
3102359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
3112359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
312133fd9f5SDenys Vlasenko 
3132359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
3142359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
315133fd9f5SDenys Vlasenko 
3162359172aSGeorge Spelvin 	q += 281 * d3;
3172359172aSGeorge Spelvin 	buf += 12;
3182359172aSGeorge Spelvin 	if (q)
3192359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3202359172aSGeorge Spelvin 	else while (buf[-1] == '0')
321133fd9f5SDenys Vlasenko 		--buf;
3227b9186f5SAndré Goddard Rosa 
3234277eeddSDenis Vlasenko 	return buf;
3244277eeddSDenis Vlasenko }
325133fd9f5SDenys Vlasenko 
326133fd9f5SDenys Vlasenko #endif
3274277eeddSDenis Vlasenko 
3281ac101a5SKAMEZAWA Hiroyuki /*
3291ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3301ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3311ac101a5SKAMEZAWA Hiroyuki  *
3321ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3331ac101a5SKAMEZAWA Hiroyuki  */
3341ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num)
3351ac101a5SKAMEZAWA Hiroyuki {
3367c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
3377c43d9a3SRasmus Villemoes 	char tmp[sizeof(num) * 3] __aligned(2);
3381ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3391ac101a5SKAMEZAWA Hiroyuki 
340133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
341133fd9f5SDenys Vlasenko 	if (num <= 9) {
342133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
343133fd9f5SDenys Vlasenko 		len = 1;
344133fd9f5SDenys Vlasenko 	} else {
3451ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
346133fd9f5SDenys Vlasenko 	}
3471ac101a5SKAMEZAWA Hiroyuki 
3481ac101a5SKAMEZAWA Hiroyuki 	if (len > size)
3491ac101a5SKAMEZAWA Hiroyuki 		return 0;
3501ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
3511ac101a5SKAMEZAWA Hiroyuki 		buf[idx] = tmp[len - idx - 1];
3521ac101a5SKAMEZAWA Hiroyuki 	return len;
3531ac101a5SKAMEZAWA Hiroyuki }
3541ac101a5SKAMEZAWA Hiroyuki 
35551be17dfSRasmus Villemoes #define SIGN	1		/* unsigned/signed, must be 1 */
356d1c1b121SRasmus Villemoes #define LEFT	2		/* left justified */
3571da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3581da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
359d1c1b121SRasmus Villemoes #define ZEROPAD	16		/* pad with zero, must be 16 == '0' - ' ' */
360b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
361b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3621da177e4SLinus Torvalds 
363fef20d9cSFrederic Weisbecker enum format_type {
364fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
365ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
366fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
367fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
368fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
369fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
370fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
371fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
372fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
373fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
374fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
375a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
376a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
377fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
378fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
379fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
380fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
381fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
382fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
383fef20d9cSFrederic Weisbecker };
384fef20d9cSFrederic Weisbecker 
385fef20d9cSFrederic Weisbecker struct printf_spec {
386d0484193SRasmus Villemoes 	unsigned int	type:8;		/* format_type enum */
387d0484193SRasmus Villemoes 	signed int	field_width:24;	/* width of output field */
388d0484193SRasmus Villemoes 	unsigned int	flags:8;	/* flags to number() */
389d0484193SRasmus Villemoes 	unsigned int	base:8;		/* number base, 8, 10 or 16 only */
390d0484193SRasmus Villemoes 	signed int	precision:16;	/* # of digits/chars */
391d0484193SRasmus Villemoes } __packed;
392*4d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1)
393*4d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1)
394fef20d9cSFrederic Weisbecker 
395cf3b429bSJoe Perches static noinline_for_stack
396cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
397fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
3981da177e4SLinus Torvalds {
3997c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
4007c43d9a3SRasmus Villemoes 	char tmp[3 * sizeof(num)] __aligned(2);
4019b706aeeSDenys Vlasenko 	char sign;
4029b706aeeSDenys Vlasenko 	char locase;
403fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4041da177e4SLinus Torvalds 	int i;
4057c203422SPierre Carrier 	bool is_zero = num == 0LL;
4061c7a8e62SRasmus Villemoes 	int field_width = spec.field_width;
4071c7a8e62SRasmus Villemoes 	int precision = spec.precision;
4081da177e4SLinus Torvalds 
409d0484193SRasmus Villemoes 	BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
410d0484193SRasmus Villemoes 
4119b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4129b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
413fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
414fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
415fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4161da177e4SLinus Torvalds 	sign = 0;
417fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4181da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4191da177e4SLinus Torvalds 			sign = '-';
4201da177e4SLinus Torvalds 			num = -(signed long long)num;
4211c7a8e62SRasmus Villemoes 			field_width--;
422fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4231da177e4SLinus Torvalds 			sign = '+';
4241c7a8e62SRasmus Villemoes 			field_width--;
425fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4261da177e4SLinus Torvalds 			sign = ' ';
4271c7a8e62SRasmus Villemoes 			field_width--;
4281da177e4SLinus Torvalds 		}
4291da177e4SLinus Torvalds 	}
430b39a7340SDenis Vlasenko 	if (need_pfx) {
431fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4321c7a8e62SRasmus Villemoes 			field_width -= 2;
4337c203422SPierre Carrier 		else if (!is_zero)
4341c7a8e62SRasmus Villemoes 			field_width--;
4351da177e4SLinus Torvalds 	}
436b39a7340SDenis Vlasenko 
437b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4381da177e4SLinus Torvalds 	i = 0;
439133fd9f5SDenys Vlasenko 	if (num < spec.base)
4403ea8d440SRasmus Villemoes 		tmp[i++] = hex_asc_upper[num] | locase;
441fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
442fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
443b39a7340SDenis Vlasenko 		int shift = 3;
4447b9186f5SAndré Goddard Rosa 
4457b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4467b9186f5SAndré Goddard Rosa 			shift = 4;
447b39a7340SDenis Vlasenko 		do {
4483ea8d440SRasmus Villemoes 			tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
449b39a7340SDenis Vlasenko 			num >>= shift;
450b39a7340SDenis Vlasenko 		} while (num);
4514277eeddSDenis Vlasenko 	} else { /* base 10 */
4524277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4534277eeddSDenis Vlasenko 	}
454b39a7340SDenis Vlasenko 
455b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
4561c7a8e62SRasmus Villemoes 	if (i > precision)
4571c7a8e62SRasmus Villemoes 		precision = i;
458b39a7340SDenis Vlasenko 	/* leading space padding */
4591c7a8e62SRasmus Villemoes 	field_width -= precision;
46051be17dfSRasmus Villemoes 	if (!(spec.flags & (ZEROPAD | LEFT))) {
4611c7a8e62SRasmus Villemoes 		while (--field_width >= 0) {
462f796937aSJeremy Fitzhardinge 			if (buf < end)
4631da177e4SLinus Torvalds 				*buf = ' ';
4641da177e4SLinus Torvalds 			++buf;
4651da177e4SLinus Torvalds 		}
4661da177e4SLinus Torvalds 	}
467b39a7340SDenis Vlasenko 	/* sign */
4681da177e4SLinus Torvalds 	if (sign) {
469f796937aSJeremy Fitzhardinge 		if (buf < end)
4701da177e4SLinus Torvalds 			*buf = sign;
4711da177e4SLinus Torvalds 		++buf;
4721da177e4SLinus Torvalds 	}
473b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
474b39a7340SDenis Vlasenko 	if (need_pfx) {
4757c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
476f796937aSJeremy Fitzhardinge 			if (buf < end)
4771da177e4SLinus Torvalds 				*buf = '0';
4781da177e4SLinus Torvalds 			++buf;
4797c203422SPierre Carrier 		}
480fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
481f796937aSJeremy Fitzhardinge 			if (buf < end)
4829b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
4831da177e4SLinus Torvalds 			++buf;
4841da177e4SLinus Torvalds 		}
4851da177e4SLinus Torvalds 	}
486b39a7340SDenis Vlasenko 	/* zero or space padding */
487fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
488d1c1b121SRasmus Villemoes 		char c = ' ' + (spec.flags & ZEROPAD);
489d1c1b121SRasmus Villemoes 		BUILD_BUG_ON(' ' + ZEROPAD != '0');
4901c7a8e62SRasmus Villemoes 		while (--field_width >= 0) {
491f796937aSJeremy Fitzhardinge 			if (buf < end)
4921da177e4SLinus Torvalds 				*buf = c;
4931da177e4SLinus Torvalds 			++buf;
4941da177e4SLinus Torvalds 		}
4951da177e4SLinus Torvalds 	}
496b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
4971c7a8e62SRasmus Villemoes 	while (i <= --precision) {
498f796937aSJeremy Fitzhardinge 		if (buf < end)
4991da177e4SLinus Torvalds 			*buf = '0';
5001da177e4SLinus Torvalds 		++buf;
5011da177e4SLinus Torvalds 	}
502b39a7340SDenis Vlasenko 	/* actual digits of result */
503b39a7340SDenis Vlasenko 	while (--i >= 0) {
504f796937aSJeremy Fitzhardinge 		if (buf < end)
5051da177e4SLinus Torvalds 			*buf = tmp[i];
5061da177e4SLinus Torvalds 		++buf;
5071da177e4SLinus Torvalds 	}
508b39a7340SDenis Vlasenko 	/* trailing space padding */
5091c7a8e62SRasmus Villemoes 	while (--field_width >= 0) {
510f796937aSJeremy Fitzhardinge 		if (buf < end)
5111da177e4SLinus Torvalds 			*buf = ' ';
5121da177e4SLinus Torvalds 		++buf;
5131da177e4SLinus Torvalds 	}
5147b9186f5SAndré Goddard Rosa 
5151da177e4SLinus Torvalds 	return buf;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
518cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
5194b6ccca7SAl Viro {
5204b6ccca7SAl Viro 	size_t size;
5214b6ccca7SAl Viro 	if (buf >= end)	/* nowhere to put anything */
5224b6ccca7SAl Viro 		return;
5234b6ccca7SAl Viro 	size = end - buf;
5244b6ccca7SAl Viro 	if (size <= spaces) {
5254b6ccca7SAl Viro 		memset(buf, ' ', size);
5264b6ccca7SAl Viro 		return;
5274b6ccca7SAl Viro 	}
5284b6ccca7SAl Viro 	if (len) {
5294b6ccca7SAl Viro 		if (len > size - spaces)
5304b6ccca7SAl Viro 			len = size - spaces;
5314b6ccca7SAl Viro 		memmove(buf + spaces, buf, len);
5324b6ccca7SAl Viro 	}
5334b6ccca7SAl Viro 	memset(buf, ' ', spaces);
5344b6ccca7SAl Viro }
5354b6ccca7SAl Viro 
536cfccde04SRasmus Villemoes /*
537cfccde04SRasmus Villemoes  * Handle field width padding for a string.
538cfccde04SRasmus Villemoes  * @buf: current buffer position
539cfccde04SRasmus Villemoes  * @n: length of string
540cfccde04SRasmus Villemoes  * @end: end of output buffer
541cfccde04SRasmus Villemoes  * @spec: for field width and flags
542cfccde04SRasmus Villemoes  * Returns: new buffer position after padding.
543cfccde04SRasmus Villemoes  */
544cfccde04SRasmus Villemoes static noinline_for_stack
545cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
546cfccde04SRasmus Villemoes {
547cfccde04SRasmus Villemoes 	unsigned spaces;
548cfccde04SRasmus Villemoes 
549cfccde04SRasmus Villemoes 	if (likely(n >= spec.field_width))
550cfccde04SRasmus Villemoes 		return buf;
551cfccde04SRasmus Villemoes 	/* we want to pad the sucker */
552cfccde04SRasmus Villemoes 	spaces = spec.field_width - n;
553cfccde04SRasmus Villemoes 	if (!(spec.flags & LEFT)) {
554cfccde04SRasmus Villemoes 		move_right(buf - n, end, n, spaces);
555cfccde04SRasmus Villemoes 		return buf + spaces;
556cfccde04SRasmus Villemoes 	}
557cfccde04SRasmus Villemoes 	while (spaces--) {
558cfccde04SRasmus Villemoes 		if (buf < end)
559cfccde04SRasmus Villemoes 			*buf = ' ';
560cfccde04SRasmus Villemoes 		++buf;
561cfccde04SRasmus Villemoes 	}
562cfccde04SRasmus Villemoes 	return buf;
563cfccde04SRasmus Villemoes }
564cfccde04SRasmus Villemoes 
5654b6ccca7SAl Viro static noinline_for_stack
56695508cfaSRasmus Villemoes char *string(char *buf, char *end, const char *s, struct printf_spec spec)
56795508cfaSRasmus Villemoes {
56834fc8b90SRasmus Villemoes 	int len = 0;
56934fc8b90SRasmus Villemoes 	size_t lim = spec.precision;
57095508cfaSRasmus Villemoes 
57195508cfaSRasmus Villemoes 	if ((unsigned long)s < PAGE_SIZE)
57295508cfaSRasmus Villemoes 		s = "(null)";
57395508cfaSRasmus Villemoes 
57434fc8b90SRasmus Villemoes 	while (lim--) {
57534fc8b90SRasmus Villemoes 		char c = *s++;
57634fc8b90SRasmus Villemoes 		if (!c)
57734fc8b90SRasmus Villemoes 			break;
57895508cfaSRasmus Villemoes 		if (buf < end)
57934fc8b90SRasmus Villemoes 			*buf = c;
58095508cfaSRasmus Villemoes 		++buf;
58134fc8b90SRasmus Villemoes 		++len;
58295508cfaSRasmus Villemoes 	}
58334fc8b90SRasmus Villemoes 	return widen_string(buf, len, end, spec);
58495508cfaSRasmus Villemoes }
58595508cfaSRasmus Villemoes 
58695508cfaSRasmus Villemoes static noinline_for_stack
5874b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
5884b6ccca7SAl Viro 		  const char *fmt)
5894b6ccca7SAl Viro {
5904b6ccca7SAl Viro 	const char *array[4], *s;
5914b6ccca7SAl Viro 	const struct dentry *p;
5924b6ccca7SAl Viro 	int depth;
5934b6ccca7SAl Viro 	int i, n;
5944b6ccca7SAl Viro 
5954b6ccca7SAl Viro 	switch (fmt[1]) {
5964b6ccca7SAl Viro 		case '2': case '3': case '4':
5974b6ccca7SAl Viro 			depth = fmt[1] - '0';
5984b6ccca7SAl Viro 			break;
5994b6ccca7SAl Viro 		default:
6004b6ccca7SAl Viro 			depth = 1;
6014b6ccca7SAl Viro 	}
6024b6ccca7SAl Viro 
6034b6ccca7SAl Viro 	rcu_read_lock();
6044b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
6054b6ccca7SAl Viro 		p = ACCESS_ONCE(d->d_parent);
6064b6ccca7SAl Viro 		array[i] = ACCESS_ONCE(d->d_name.name);
6074b6ccca7SAl Viro 		if (p == d) {
6084b6ccca7SAl Viro 			if (i)
6094b6ccca7SAl Viro 				array[i] = "";
6104b6ccca7SAl Viro 			i++;
6114b6ccca7SAl Viro 			break;
6124b6ccca7SAl Viro 		}
6134b6ccca7SAl Viro 	}
6144b6ccca7SAl Viro 	s = array[--i];
6154b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
6164b6ccca7SAl Viro 		char c = *s++;
6174b6ccca7SAl Viro 		if (!c) {
6184b6ccca7SAl Viro 			if (!i)
6194b6ccca7SAl Viro 				break;
6204b6ccca7SAl Viro 			c = '/';
6214b6ccca7SAl Viro 			s = array[--i];
6224b6ccca7SAl Viro 		}
6234b6ccca7SAl Viro 		if (buf < end)
6244b6ccca7SAl Viro 			*buf = c;
6254b6ccca7SAl Viro 	}
6264b6ccca7SAl Viro 	rcu_read_unlock();
627cfccde04SRasmus Villemoes 	return widen_string(buf, n, end, spec);
6284b6ccca7SAl Viro }
6294b6ccca7SAl Viro 
6301031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
6311031bc58SDmitry Monakhov static noinline_for_stack
6321031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev,
6331031bc58SDmitry Monakhov 		struct printf_spec spec, const char *fmt)
6341031bc58SDmitry Monakhov {
6351031bc58SDmitry Monakhov 	struct gendisk *hd = bdev->bd_disk;
6361031bc58SDmitry Monakhov 
6371031bc58SDmitry Monakhov 	buf = string(buf, end, hd->disk_name, spec);
6381031bc58SDmitry Monakhov 	if (bdev->bd_part->partno) {
6391031bc58SDmitry Monakhov 		if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
6401031bc58SDmitry Monakhov 			if (buf < end)
6411031bc58SDmitry Monakhov 				*buf = 'p';
6421031bc58SDmitry Monakhov 			buf++;
6431031bc58SDmitry Monakhov 		}
6441031bc58SDmitry Monakhov 		buf = number(buf, end, bdev->bd_part->partno, spec);
6451031bc58SDmitry Monakhov 	}
6461031bc58SDmitry Monakhov 	return buf;
6471031bc58SDmitry Monakhov }
6481031bc58SDmitry Monakhov #endif
6491031bc58SDmitry Monakhov 
650cf3b429bSJoe Perches static noinline_for_stack
651cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
652b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
6530fe1ef24SLinus Torvalds {
654b0d33c2bSJoe Perches 	unsigned long value;
6550fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
6560fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
657b0d33c2bSJoe Perches #endif
658b0d33c2bSJoe Perches 
659b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
660b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
661b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
662b0d33c2bSJoe Perches 
663b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
664b0d33c2bSJoe Perches 	if (*fmt == 'B')
6650f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
666b0d33c2bSJoe Perches 	else if (*fmt != 'f' && *fmt != 's')
6670fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
6680c8b946eSFrederic Weisbecker 	else
6694796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
6707b9186f5SAndré Goddard Rosa 
671fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
6720fe1ef24SLinus Torvalds #else
673fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
674fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
675fef20d9cSFrederic Weisbecker 	spec.base = 16;
6767b9186f5SAndré Goddard Rosa 
677fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
6780fe1ef24SLinus Torvalds #endif
6790fe1ef24SLinus Torvalds }
6800fe1ef24SLinus Torvalds 
681cf3b429bSJoe Perches static noinline_for_stack
682cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
683fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
684332d2e78SLinus Torvalds {
685332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
68628405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
687332d2e78SLinus Torvalds #endif
688332d2e78SLinus Torvalds 
689332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
69028405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
691332d2e78SLinus Torvalds #endif
6924da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
693fef20d9cSFrederic Weisbecker 		.base = 16,
6944da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
695fef20d9cSFrederic Weisbecker 		.precision = -1,
696fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
697fef20d9cSFrederic Weisbecker 	};
6984da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
6994da0b66cSBjorn Helgaas 		.base = 16,
7004da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
7014da0b66cSBjorn Helgaas 		.precision = -1,
7024da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
7034da0b66cSBjorn Helgaas 	};
7040f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
7050f4050c7SBjorn Helgaas 		.base = 16,
7060f4050c7SBjorn Helgaas 		.field_width = 2,
7070f4050c7SBjorn Helgaas 		.precision = -1,
7080f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
7090f4050c7SBjorn Helgaas 	};
7104da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
711c91d3376SBjorn Helgaas 		.base = 10,
712c91d3376SBjorn Helgaas 		.precision = -1,
713c91d3376SBjorn Helgaas 		.flags = 0,
714c91d3376SBjorn Helgaas 	};
7154da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
716fd95541eSBjorn Helgaas 		.field_width = -1,
717fd95541eSBjorn Helgaas 		.precision = 10,
718fd95541eSBjorn Helgaas 		.flags = LEFT,
719fd95541eSBjorn Helgaas 	};
7204da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
721fd95541eSBjorn Helgaas 		.base = 16,
722fd95541eSBjorn Helgaas 		.precision = -1,
723fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
724fd95541eSBjorn Helgaas 	};
725c7dabef8SBjorn Helgaas 
726c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
727c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
728c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
729c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
7309d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
731c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
732c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
733c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
734c7dabef8SBjorn Helgaas 
735332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
736c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
7374da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
738332d2e78SLinus Torvalds 
739332d2e78SLinus Torvalds 	*p++ = '[';
7404da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
741fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
7424da0b66cSBjorn Helgaas 		specp = &io_spec;
7434da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
744fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
7454da0b66cSBjorn Helgaas 		specp = &mem_spec;
7464da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
747fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
7484da0b66cSBjorn Helgaas 		specp = &dec_spec;
7494da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
750fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
7514da0b66cSBjorn Helgaas 		specp = &dec_spec;
7520f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
7530f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
7540f4050c7SBjorn Helgaas 		specp = &bus_spec;
7554da0b66cSBjorn Helgaas 	} else {
756c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
7574da0b66cSBjorn Helgaas 		specp = &mem_spec;
758c7dabef8SBjorn Helgaas 		decode = 0;
759fd95541eSBjorn Helgaas 	}
760d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
761d19cb803SBjorn Helgaas 		p = string(p, pend, "size ", str_spec);
762d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
763d19cb803SBjorn Helgaas 	} else {
7644da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
765c91d3376SBjorn Helgaas 		if (res->start != res->end) {
766332d2e78SLinus Torvalds 			*p++ = '-';
7674da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
768c91d3376SBjorn Helgaas 		}
769d19cb803SBjorn Helgaas 	}
770c7dabef8SBjorn Helgaas 	if (decode) {
771fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
772fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
773fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
774fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
7759d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
7769d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
777fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
778fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
779c7dabef8SBjorn Helgaas 	} else {
780fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
781c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
782fd95541eSBjorn Helgaas 	}
783332d2e78SLinus Torvalds 	*p++ = ']';
784c7dabef8SBjorn Helgaas 	*p = '\0';
785332d2e78SLinus Torvalds 
786fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
787332d2e78SLinus Torvalds }
788332d2e78SLinus Torvalds 
789cf3b429bSJoe Perches static noinline_for_stack
79031550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
79131550a16SAndy Shevchenko 		 const char *fmt)
79231550a16SAndy Shevchenko {
793360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
79431550a16SAndy Shevchenko 				   negative value, fallback to the default */
79531550a16SAndy Shevchenko 	char separator;
79631550a16SAndy Shevchenko 
79731550a16SAndy Shevchenko 	if (spec.field_width == 0)
79831550a16SAndy Shevchenko 		/* nothing to print */
79931550a16SAndy Shevchenko 		return buf;
80031550a16SAndy Shevchenko 
80131550a16SAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
80231550a16SAndy Shevchenko 		/* NULL pointer */
80331550a16SAndy Shevchenko 		return string(buf, end, NULL, spec);
80431550a16SAndy Shevchenko 
80531550a16SAndy Shevchenko 	switch (fmt[1]) {
80631550a16SAndy Shevchenko 	case 'C':
80731550a16SAndy Shevchenko 		separator = ':';
80831550a16SAndy Shevchenko 		break;
80931550a16SAndy Shevchenko 	case 'D':
81031550a16SAndy Shevchenko 		separator = '-';
81131550a16SAndy Shevchenko 		break;
81231550a16SAndy Shevchenko 	case 'N':
81331550a16SAndy Shevchenko 		separator = 0;
81431550a16SAndy Shevchenko 		break;
81531550a16SAndy Shevchenko 	default:
81631550a16SAndy Shevchenko 		separator = ' ';
81731550a16SAndy Shevchenko 		break;
81831550a16SAndy Shevchenko 	}
81931550a16SAndy Shevchenko 
82031550a16SAndy Shevchenko 	if (spec.field_width > 0)
82131550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
82231550a16SAndy Shevchenko 
8239c98f235SRasmus Villemoes 	for (i = 0; i < len; ++i) {
8249c98f235SRasmus Villemoes 		if (buf < end)
8259c98f235SRasmus Villemoes 			*buf = hex_asc_hi(addr[i]);
8269c98f235SRasmus Villemoes 		++buf;
8279c98f235SRasmus Villemoes 		if (buf < end)
8289c98f235SRasmus Villemoes 			*buf = hex_asc_lo(addr[i]);
8299c98f235SRasmus Villemoes 		++buf;
83031550a16SAndy Shevchenko 
8319c98f235SRasmus Villemoes 		if (separator && i != len - 1) {
8329c98f235SRasmus Villemoes 			if (buf < end)
8339c98f235SRasmus Villemoes 				*buf = separator;
8349c98f235SRasmus Villemoes 			++buf;
8359c98f235SRasmus Villemoes 		}
83631550a16SAndy Shevchenko 	}
83731550a16SAndy Shevchenko 
83831550a16SAndy Shevchenko 	return buf;
83931550a16SAndy Shevchenko }
84031550a16SAndy Shevchenko 
84131550a16SAndy Shevchenko static noinline_for_stack
842dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
843dbc760bcSTejun Heo 		    struct printf_spec spec, const char *fmt)
844dbc760bcSTejun Heo {
845dbc760bcSTejun Heo 	const int CHUNKSZ = 32;
846dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
847dbc760bcSTejun Heo 	int i, chunksz;
848dbc760bcSTejun Heo 	bool first = true;
849dbc760bcSTejun Heo 
850dbc760bcSTejun Heo 	/* reused to print numbers */
851dbc760bcSTejun Heo 	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
852dbc760bcSTejun Heo 
853dbc760bcSTejun Heo 	chunksz = nr_bits & (CHUNKSZ - 1);
854dbc760bcSTejun Heo 	if (chunksz == 0)
855dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
856dbc760bcSTejun Heo 
857dbc760bcSTejun Heo 	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
858dbc760bcSTejun Heo 	for (; i >= 0; i -= CHUNKSZ) {
859dbc760bcSTejun Heo 		u32 chunkmask, val;
860dbc760bcSTejun Heo 		int word, bit;
861dbc760bcSTejun Heo 
862dbc760bcSTejun Heo 		chunkmask = ((1ULL << chunksz) - 1);
863dbc760bcSTejun Heo 		word = i / BITS_PER_LONG;
864dbc760bcSTejun Heo 		bit = i % BITS_PER_LONG;
865dbc760bcSTejun Heo 		val = (bitmap[word] >> bit) & chunkmask;
866dbc760bcSTejun Heo 
867dbc760bcSTejun Heo 		if (!first) {
868dbc760bcSTejun Heo 			if (buf < end)
869dbc760bcSTejun Heo 				*buf = ',';
870dbc760bcSTejun Heo 			buf++;
871dbc760bcSTejun Heo 		}
872dbc760bcSTejun Heo 		first = false;
873dbc760bcSTejun Heo 
874dbc760bcSTejun Heo 		spec.field_width = DIV_ROUND_UP(chunksz, 4);
875dbc760bcSTejun Heo 		buf = number(buf, end, val, spec);
876dbc760bcSTejun Heo 
877dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
878dbc760bcSTejun Heo 	}
879dbc760bcSTejun Heo 	return buf;
880dbc760bcSTejun Heo }
881dbc760bcSTejun Heo 
882dbc760bcSTejun Heo static noinline_for_stack
883dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
884dbc760bcSTejun Heo 			 struct printf_spec spec, const char *fmt)
885dbc760bcSTejun Heo {
886dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
887dbc760bcSTejun Heo 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
888dbc760bcSTejun Heo 	int cur, rbot, rtop;
889dbc760bcSTejun Heo 	bool first = true;
890dbc760bcSTejun Heo 
891dbc760bcSTejun Heo 	/* reused to print numbers */
892dbc760bcSTejun Heo 	spec = (struct printf_spec){ .base = 10 };
893dbc760bcSTejun Heo 
894dbc760bcSTejun Heo 	rbot = cur = find_first_bit(bitmap, nr_bits);
895dbc760bcSTejun Heo 	while (cur < nr_bits) {
896dbc760bcSTejun Heo 		rtop = cur;
897dbc760bcSTejun Heo 		cur = find_next_bit(bitmap, nr_bits, cur + 1);
898dbc760bcSTejun Heo 		if (cur < nr_bits && cur <= rtop + 1)
899dbc760bcSTejun Heo 			continue;
900dbc760bcSTejun Heo 
901dbc760bcSTejun Heo 		if (!first) {
902dbc760bcSTejun Heo 			if (buf < end)
903dbc760bcSTejun Heo 				*buf = ',';
904dbc760bcSTejun Heo 			buf++;
905dbc760bcSTejun Heo 		}
906dbc760bcSTejun Heo 		first = false;
907dbc760bcSTejun Heo 
908dbc760bcSTejun Heo 		buf = number(buf, end, rbot, spec);
909dbc760bcSTejun Heo 		if (rbot < rtop) {
910dbc760bcSTejun Heo 			if (buf < end)
911dbc760bcSTejun Heo 				*buf = '-';
912dbc760bcSTejun Heo 			buf++;
913dbc760bcSTejun Heo 
914dbc760bcSTejun Heo 			buf = number(buf, end, rtop, spec);
915dbc760bcSTejun Heo 		}
916dbc760bcSTejun Heo 
917dbc760bcSTejun Heo 		rbot = cur;
918dbc760bcSTejun Heo 	}
919dbc760bcSTejun Heo 	return buf;
920dbc760bcSTejun Heo }
921dbc760bcSTejun Heo 
922dbc760bcSTejun Heo static noinline_for_stack
923cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
9248a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
925dd45c9cfSHarvey Harrison {
9268a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
927dd45c9cfSHarvey Harrison 	char *p = mac_addr;
928dd45c9cfSHarvey Harrison 	int i;
929bc7259a2SJoe Perches 	char separator;
93076597ff9SAndrei Emeltchenko 	bool reversed = false;
931bc7259a2SJoe Perches 
93276597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
93376597ff9SAndrei Emeltchenko 	case 'F':
934bc7259a2SJoe Perches 		separator = '-';
93576597ff9SAndrei Emeltchenko 		break;
93676597ff9SAndrei Emeltchenko 
93776597ff9SAndrei Emeltchenko 	case 'R':
93876597ff9SAndrei Emeltchenko 		reversed = true;
93976597ff9SAndrei Emeltchenko 		/* fall through */
94076597ff9SAndrei Emeltchenko 
94176597ff9SAndrei Emeltchenko 	default:
942bc7259a2SJoe Perches 		separator = ':';
94376597ff9SAndrei Emeltchenko 		break;
944bc7259a2SJoe Perches 	}
945dd45c9cfSHarvey Harrison 
946dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
94776597ff9SAndrei Emeltchenko 		if (reversed)
94876597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
94976597ff9SAndrei Emeltchenko 		else
95055036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
95176597ff9SAndrei Emeltchenko 
9528a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
953bc7259a2SJoe Perches 			*p++ = separator;
954dd45c9cfSHarvey Harrison 	}
955dd45c9cfSHarvey Harrison 	*p = '\0';
956dd45c9cfSHarvey Harrison 
957fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
958dd45c9cfSHarvey Harrison }
959dd45c9cfSHarvey Harrison 
960cf3b429bSJoe Perches static noinline_for_stack
961cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
962689afa7dSHarvey Harrison {
963689afa7dSHarvey Harrison 	int i;
9640159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
9650159f24eSJoe Perches 	int index;
9660159f24eSJoe Perches 	int step;
967689afa7dSHarvey Harrison 
9680159f24eSJoe Perches 	switch (fmt[2]) {
9690159f24eSJoe Perches 	case 'h':
9700159f24eSJoe Perches #ifdef __BIG_ENDIAN
9710159f24eSJoe Perches 		index = 0;
9720159f24eSJoe Perches 		step = 1;
9730159f24eSJoe Perches #else
9740159f24eSJoe Perches 		index = 3;
9750159f24eSJoe Perches 		step = -1;
9760159f24eSJoe Perches #endif
9770159f24eSJoe Perches 		break;
9780159f24eSJoe Perches 	case 'l':
9790159f24eSJoe Perches 		index = 3;
9800159f24eSJoe Perches 		step = -1;
9810159f24eSJoe Perches 		break;
9820159f24eSJoe Perches 	case 'n':
9830159f24eSJoe Perches 	case 'b':
9840159f24eSJoe Perches 	default:
9850159f24eSJoe Perches 		index = 0;
9860159f24eSJoe Perches 		step = 1;
9870159f24eSJoe Perches 		break;
9880159f24eSJoe Perches 	}
9898a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
9907c43d9a3SRasmus Villemoes 		char temp[4] __aligned(2);	/* hold each IP quad in reverse order */
991133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
9928a27f7c9SJoe Perches 		if (leading_zeros) {
9938a27f7c9SJoe Perches 			if (digits < 3)
9948a27f7c9SJoe Perches 				*p++ = '0';
9958a27f7c9SJoe Perches 			if (digits < 2)
9968a27f7c9SJoe Perches 				*p++ = '0';
9978a27f7c9SJoe Perches 		}
9988a27f7c9SJoe Perches 		/* reverse the digits in the quad */
9998a27f7c9SJoe Perches 		while (digits--)
10008a27f7c9SJoe Perches 			*p++ = temp[digits];
10018a27f7c9SJoe Perches 		if (i < 3)
10028a27f7c9SJoe Perches 			*p++ = '.';
10030159f24eSJoe Perches 		index += step;
10048a27f7c9SJoe Perches 	}
10058a27f7c9SJoe Perches 	*p = '\0';
10067b9186f5SAndré Goddard Rosa 
10078a27f7c9SJoe Perches 	return p;
10088a27f7c9SJoe Perches }
10098a27f7c9SJoe Perches 
1010cf3b429bSJoe Perches static noinline_for_stack
1011cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
10128a27f7c9SJoe Perches {
10137b9186f5SAndré Goddard Rosa 	int i, j, range;
10148a27f7c9SJoe Perches 	unsigned char zerolength[8];
10158a27f7c9SJoe Perches 	int longest = 1;
10168a27f7c9SJoe Perches 	int colonpos = -1;
10178a27f7c9SJoe Perches 	u16 word;
10187b9186f5SAndré Goddard Rosa 	u8 hi, lo;
10198a27f7c9SJoe Perches 	bool needcolon = false;
1020eb78cd26SJoe Perches 	bool useIPv4;
1021eb78cd26SJoe Perches 	struct in6_addr in6;
1022eb78cd26SJoe Perches 
1023eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
1024eb78cd26SJoe Perches 
1025eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
10268a27f7c9SJoe Perches 
10278a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
10288a27f7c9SJoe Perches 
10298a27f7c9SJoe Perches 	if (useIPv4)
10308a27f7c9SJoe Perches 		range = 6;
10318a27f7c9SJoe Perches 	else
10328a27f7c9SJoe Perches 		range = 8;
10338a27f7c9SJoe Perches 
10348a27f7c9SJoe Perches 	/* find position of longest 0 run */
10358a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10368a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
1037eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
10388a27f7c9SJoe Perches 				break;
10398a27f7c9SJoe Perches 			zerolength[i]++;
10408a27f7c9SJoe Perches 		}
10418a27f7c9SJoe Perches 	}
10428a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10438a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
10448a27f7c9SJoe Perches 			longest = zerolength[i];
10458a27f7c9SJoe Perches 			colonpos = i;
10468a27f7c9SJoe Perches 		}
10478a27f7c9SJoe Perches 	}
104829cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
104929cf519eSJoe Perches 		colonpos = -1;
10508a27f7c9SJoe Perches 
10518a27f7c9SJoe Perches 	/* emit address */
10528a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10538a27f7c9SJoe Perches 		if (i == colonpos) {
10548a27f7c9SJoe Perches 			if (needcolon || i == 0)
10558a27f7c9SJoe Perches 				*p++ = ':';
10568a27f7c9SJoe Perches 			*p++ = ':';
10578a27f7c9SJoe Perches 			needcolon = false;
10588a27f7c9SJoe Perches 			i += longest - 1;
10598a27f7c9SJoe Perches 			continue;
10608a27f7c9SJoe Perches 		}
10618a27f7c9SJoe Perches 		if (needcolon) {
10628a27f7c9SJoe Perches 			*p++ = ':';
10638a27f7c9SJoe Perches 			needcolon = false;
10648a27f7c9SJoe Perches 		}
10658a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
1066eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
10678a27f7c9SJoe Perches 		hi = word >> 8;
10688a27f7c9SJoe Perches 		lo = word & 0xff;
10698a27f7c9SJoe Perches 		if (hi) {
10708a27f7c9SJoe Perches 			if (hi > 0x0f)
107155036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
10728a27f7c9SJoe Perches 			else
10738a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
107455036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10758a27f7c9SJoe Perches 		}
1076b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
107755036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10788a27f7c9SJoe Perches 		else
10798a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
10808a27f7c9SJoe Perches 		needcolon = true;
10818a27f7c9SJoe Perches 	}
10828a27f7c9SJoe Perches 
10838a27f7c9SJoe Perches 	if (useIPv4) {
10848a27f7c9SJoe Perches 		if (needcolon)
10858a27f7c9SJoe Perches 			*p++ = ':';
10860159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
10878a27f7c9SJoe Perches 	}
10888a27f7c9SJoe Perches 	*p = '\0';
10897b9186f5SAndré Goddard Rosa 
10908a27f7c9SJoe Perches 	return p;
10918a27f7c9SJoe Perches }
10928a27f7c9SJoe Perches 
1093cf3b429bSJoe Perches static noinline_for_stack
1094cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
10958a27f7c9SJoe Perches {
10968a27f7c9SJoe Perches 	int i;
10977b9186f5SAndré Goddard Rosa 
1098689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
109955036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
110055036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
11018a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
1102689afa7dSHarvey Harrison 			*p++ = ':';
1103689afa7dSHarvey Harrison 	}
1104689afa7dSHarvey Harrison 	*p = '\0';
11057b9186f5SAndré Goddard Rosa 
11068a27f7c9SJoe Perches 	return p;
11078a27f7c9SJoe Perches }
11088a27f7c9SJoe Perches 
1109cf3b429bSJoe Perches static noinline_for_stack
1110cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
11118a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
11128a27f7c9SJoe Perches {
11138a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
11148a27f7c9SJoe Perches 
11158a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
1116eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
11178a27f7c9SJoe Perches 	else
1118eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
1119689afa7dSHarvey Harrison 
1120fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
1121689afa7dSHarvey Harrison }
1122689afa7dSHarvey Harrison 
1123cf3b429bSJoe Perches static noinline_for_stack
1124cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
11258a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
11264aa99606SHarvey Harrison {
11278a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
11284aa99606SHarvey Harrison 
11290159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
11304aa99606SHarvey Harrison 
1131fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
11324aa99606SHarvey Harrison }
11334aa99606SHarvey Harrison 
1134cf3b429bSJoe Perches static noinline_for_stack
113510679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
113610679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
113710679643SDaniel Borkmann {
113810679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
113910679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
114010679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
114110679643SDaniel Borkmann 		      sizeof("%1234567890")];
114210679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
114310679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
114410679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
114510679643SDaniel Borkmann 	u8 off = 0;
114610679643SDaniel Borkmann 
114710679643SDaniel Borkmann 	fmt++;
114810679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
114910679643SDaniel Borkmann 		switch (*fmt) {
115010679643SDaniel Borkmann 		case 'p':
115110679643SDaniel Borkmann 			have_p = true;
115210679643SDaniel Borkmann 			break;
115310679643SDaniel Borkmann 		case 'f':
115410679643SDaniel Borkmann 			have_f = true;
115510679643SDaniel Borkmann 			break;
115610679643SDaniel Borkmann 		case 's':
115710679643SDaniel Borkmann 			have_s = true;
115810679643SDaniel Borkmann 			break;
115910679643SDaniel Borkmann 		case 'c':
116010679643SDaniel Borkmann 			have_c = true;
116110679643SDaniel Borkmann 			break;
116210679643SDaniel Borkmann 		}
116310679643SDaniel Borkmann 	}
116410679643SDaniel Borkmann 
116510679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
116610679643SDaniel Borkmann 		*p = '[';
116710679643SDaniel Borkmann 		off = 1;
116810679643SDaniel Borkmann 	}
116910679643SDaniel Borkmann 
117010679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
117110679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
117210679643SDaniel Borkmann 	else
117310679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
117410679643SDaniel Borkmann 
117510679643SDaniel Borkmann 	if (have_p || have_s || have_f)
117610679643SDaniel Borkmann 		*p++ = ']';
117710679643SDaniel Borkmann 
117810679643SDaniel Borkmann 	if (have_p) {
117910679643SDaniel Borkmann 		*p++ = ':';
118010679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
118110679643SDaniel Borkmann 	}
118210679643SDaniel Borkmann 	if (have_f) {
118310679643SDaniel Borkmann 		*p++ = '/';
118410679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
118510679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
118610679643SDaniel Borkmann 	}
118710679643SDaniel Borkmann 	if (have_s) {
118810679643SDaniel Borkmann 		*p++ = '%';
118910679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
119010679643SDaniel Borkmann 	}
119110679643SDaniel Borkmann 	*p = '\0';
119210679643SDaniel Borkmann 
119310679643SDaniel Borkmann 	return string(buf, end, ip6_addr, spec);
119410679643SDaniel Borkmann }
119510679643SDaniel Borkmann 
119610679643SDaniel Borkmann static noinline_for_stack
119710679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
119810679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
119910679643SDaniel Borkmann {
120010679643SDaniel Borkmann 	bool have_p = false;
120110679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
120210679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
120310679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
120410679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
120510679643SDaniel Borkmann 
120610679643SDaniel Borkmann 	fmt++;
120710679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
120810679643SDaniel Borkmann 		switch (*fmt) {
120910679643SDaniel Borkmann 		case 'p':
121010679643SDaniel Borkmann 			have_p = true;
121110679643SDaniel Borkmann 			break;
121210679643SDaniel Borkmann 		case 'h':
121310679643SDaniel Borkmann 		case 'l':
121410679643SDaniel Borkmann 		case 'n':
121510679643SDaniel Borkmann 		case 'b':
121610679643SDaniel Borkmann 			fmt4[2] = *fmt;
121710679643SDaniel Borkmann 			break;
121810679643SDaniel Borkmann 		}
121910679643SDaniel Borkmann 	}
122010679643SDaniel Borkmann 
122110679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
122210679643SDaniel Borkmann 	if (have_p) {
122310679643SDaniel Borkmann 		*p++ = ':';
122410679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
122510679643SDaniel Borkmann 	}
122610679643SDaniel Borkmann 	*p = '\0';
122710679643SDaniel Borkmann 
122810679643SDaniel Borkmann 	return string(buf, end, ip4_addr, spec);
122910679643SDaniel Borkmann }
123010679643SDaniel Borkmann 
123110679643SDaniel Borkmann static noinline_for_stack
123271dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
123371dca95dSAndy Shevchenko 		     const char *fmt)
123471dca95dSAndy Shevchenko {
123571dca95dSAndy Shevchenko 	bool found = true;
123671dca95dSAndy Shevchenko 	int count = 1;
123771dca95dSAndy Shevchenko 	unsigned int flags = 0;
123871dca95dSAndy Shevchenko 	int len;
123971dca95dSAndy Shevchenko 
124071dca95dSAndy Shevchenko 	if (spec.field_width == 0)
124171dca95dSAndy Shevchenko 		return buf;				/* nothing to print */
124271dca95dSAndy Shevchenko 
124371dca95dSAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
124471dca95dSAndy Shevchenko 		return string(buf, end, NULL, spec);	/* NULL pointer */
124571dca95dSAndy Shevchenko 
124671dca95dSAndy Shevchenko 
124771dca95dSAndy Shevchenko 	do {
124871dca95dSAndy Shevchenko 		switch (fmt[count++]) {
124971dca95dSAndy Shevchenko 		case 'a':
125071dca95dSAndy Shevchenko 			flags |= ESCAPE_ANY;
125171dca95dSAndy Shevchenko 			break;
125271dca95dSAndy Shevchenko 		case 'c':
125371dca95dSAndy Shevchenko 			flags |= ESCAPE_SPECIAL;
125471dca95dSAndy Shevchenko 			break;
125571dca95dSAndy Shevchenko 		case 'h':
125671dca95dSAndy Shevchenko 			flags |= ESCAPE_HEX;
125771dca95dSAndy Shevchenko 			break;
125871dca95dSAndy Shevchenko 		case 'n':
125971dca95dSAndy Shevchenko 			flags |= ESCAPE_NULL;
126071dca95dSAndy Shevchenko 			break;
126171dca95dSAndy Shevchenko 		case 'o':
126271dca95dSAndy Shevchenko 			flags |= ESCAPE_OCTAL;
126371dca95dSAndy Shevchenko 			break;
126471dca95dSAndy Shevchenko 		case 'p':
126571dca95dSAndy Shevchenko 			flags |= ESCAPE_NP;
126671dca95dSAndy Shevchenko 			break;
126771dca95dSAndy Shevchenko 		case 's':
126871dca95dSAndy Shevchenko 			flags |= ESCAPE_SPACE;
126971dca95dSAndy Shevchenko 			break;
127071dca95dSAndy Shevchenko 		default:
127171dca95dSAndy Shevchenko 			found = false;
127271dca95dSAndy Shevchenko 			break;
127371dca95dSAndy Shevchenko 		}
127471dca95dSAndy Shevchenko 	} while (found);
127571dca95dSAndy Shevchenko 
127671dca95dSAndy Shevchenko 	if (!flags)
127771dca95dSAndy Shevchenko 		flags = ESCAPE_ANY_NP;
127871dca95dSAndy Shevchenko 
127971dca95dSAndy Shevchenko 	len = spec.field_width < 0 ? 1 : spec.field_width;
128071dca95dSAndy Shevchenko 
128141416f23SRasmus Villemoes 	/*
128241416f23SRasmus Villemoes 	 * string_escape_mem() writes as many characters as it can to
128341416f23SRasmus Villemoes 	 * the given buffer, and returns the total size of the output
128441416f23SRasmus Villemoes 	 * had the buffer been big enough.
128541416f23SRasmus Villemoes 	 */
128641416f23SRasmus Villemoes 	buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
128771dca95dSAndy Shevchenko 
128871dca95dSAndy Shevchenko 	return buf;
128971dca95dSAndy Shevchenko }
129071dca95dSAndy Shevchenko 
129171dca95dSAndy Shevchenko static noinline_for_stack
1292cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
12939ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
12949ac6e44eSJoe Perches {
12959ac6e44eSJoe Perches 	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
12969ac6e44eSJoe Perches 	char *p = uuid;
12979ac6e44eSJoe Perches 	int i;
12989ac6e44eSJoe Perches 	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
12999ac6e44eSJoe Perches 	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
13009ac6e44eSJoe Perches 	const u8 *index = be;
13019ac6e44eSJoe Perches 	bool uc = false;
13029ac6e44eSJoe Perches 
13039ac6e44eSJoe Perches 	switch (*(++fmt)) {
13049ac6e44eSJoe Perches 	case 'L':
13059ac6e44eSJoe Perches 		uc = true;		/* fall-through */
13069ac6e44eSJoe Perches 	case 'l':
13079ac6e44eSJoe Perches 		index = le;
13089ac6e44eSJoe Perches 		break;
13099ac6e44eSJoe Perches 	case 'B':
13109ac6e44eSJoe Perches 		uc = true;
13119ac6e44eSJoe Perches 		break;
13129ac6e44eSJoe Perches 	}
13139ac6e44eSJoe Perches 
13149ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
131555036ba7SAndy Shevchenko 		p = hex_byte_pack(p, addr[index[i]]);
13169ac6e44eSJoe Perches 		switch (i) {
13179ac6e44eSJoe Perches 		case 3:
13189ac6e44eSJoe Perches 		case 5:
13199ac6e44eSJoe Perches 		case 7:
13209ac6e44eSJoe Perches 		case 9:
13219ac6e44eSJoe Perches 			*p++ = '-';
13229ac6e44eSJoe Perches 			break;
13239ac6e44eSJoe Perches 		}
13249ac6e44eSJoe Perches 	}
13259ac6e44eSJoe Perches 
13269ac6e44eSJoe Perches 	*p = 0;
13279ac6e44eSJoe Perches 
13289ac6e44eSJoe Perches 	if (uc) {
13299ac6e44eSJoe Perches 		p = uuid;
13309ac6e44eSJoe Perches 		do {
13319ac6e44eSJoe Perches 			*p = toupper(*p);
13329ac6e44eSJoe Perches 		} while (*(++p));
13339ac6e44eSJoe Perches 	}
13349ac6e44eSJoe Perches 
13359ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
13369ac6e44eSJoe Perches }
13379ac6e44eSJoe Perches 
1338c8f44affSMichał Mirosław static
1339c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1340c8f44affSMichał Mirosław 		      struct printf_spec spec)
1341c8f44affSMichał Mirosław {
1342c8f44affSMichał Mirosław 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1343c8f44affSMichał Mirosław 	if (spec.field_width == -1)
1344c8f44affSMichał Mirosław 		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1345c8f44affSMichał Mirosław 	spec.base = 16;
1346c8f44affSMichał Mirosław 
1347c8f44affSMichał Mirosław 	return number(buf, end, *(const netdev_features_t *)addr, spec);
1348c8f44affSMichał Mirosław }
1349c8f44affSMichał Mirosław 
1350aaf07621SJoe Perches static noinline_for_stack
1351aaf07621SJoe Perches char *address_val(char *buf, char *end, const void *addr,
1352aaf07621SJoe Perches 		  struct printf_spec spec, const char *fmt)
1353aaf07621SJoe Perches {
1354aaf07621SJoe Perches 	unsigned long long num;
1355aaf07621SJoe Perches 
1356aaf07621SJoe Perches 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1357aaf07621SJoe Perches 	spec.base = 16;
1358aaf07621SJoe Perches 
1359aaf07621SJoe Perches 	switch (fmt[1]) {
1360aaf07621SJoe Perches 	case 'd':
1361aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
1362aaf07621SJoe Perches 		spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1363aaf07621SJoe Perches 		break;
1364aaf07621SJoe Perches 	case 'p':
1365aaf07621SJoe Perches 	default:
1366aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
1367aaf07621SJoe Perches 		spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1368aaf07621SJoe Perches 		break;
1369aaf07621SJoe Perches 	}
1370aaf07621SJoe Perches 
1371aaf07621SJoe Perches 	return number(buf, end, num, spec);
1372aaf07621SJoe Perches }
1373aaf07621SJoe Perches 
1374900cca29SGeert Uytterhoeven static noinline_for_stack
1375900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1376900cca29SGeert Uytterhoeven 	    const char *fmt)
1377900cca29SGeert Uytterhoeven {
1378900cca29SGeert Uytterhoeven 	if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1379900cca29SGeert Uytterhoeven 		return string(buf, end, NULL, spec);
1380900cca29SGeert Uytterhoeven 
1381900cca29SGeert Uytterhoeven 	switch (fmt[1]) {
1382900cca29SGeert Uytterhoeven 	case 'r':
1383900cca29SGeert Uytterhoeven 		return number(buf, end, clk_get_rate(clk), spec);
1384900cca29SGeert Uytterhoeven 
1385900cca29SGeert Uytterhoeven 	case 'n':
1386900cca29SGeert Uytterhoeven 	default:
1387900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK
1388900cca29SGeert Uytterhoeven 		return string(buf, end, __clk_get_name(clk), spec);
1389900cca29SGeert Uytterhoeven #else
1390900cca29SGeert Uytterhoeven 		spec.base = 16;
1391900cca29SGeert Uytterhoeven 		spec.field_width = sizeof(unsigned long) * 2 + 2;
1392900cca29SGeert Uytterhoeven 		spec.flags |= SPECIAL | SMALL | ZEROPAD;
1393900cca29SGeert Uytterhoeven 		return number(buf, end, (unsigned long)clk, spec);
1394900cca29SGeert Uytterhoeven #endif
1395900cca29SGeert Uytterhoeven 	}
1396900cca29SGeert Uytterhoeven }
1397900cca29SGeert Uytterhoeven 
1398411f05f1SIngo Molnar int kptr_restrict __read_mostly;
1399455cd5abSDan Rosenberg 
14004d8a743cSLinus Torvalds /*
14014d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
14024d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
14034d8a743cSLinus Torvalds  * specifiers.
14044d8a743cSLinus Torvalds  *
1405332d2e78SLinus Torvalds  * Right now we handle:
14060fe1ef24SLinus Torvalds  *
14070c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
14080c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
14090efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
14100efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
1411b0d33c2bSJoe Perches  * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
14120f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
1413c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1414c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1415dbc760bcSTejun Heo  * - 'b[l]' For a bitmap, the number of bits is determined by the field
1416dbc760bcSTejun Heo  *       width which must be explicitly specified either as part of the
1417dbc760bcSTejun Heo  *       format string '%32b[l]' or through '%*b[l]', [l] selects
1418dbc760bcSTejun Heo  *       range-list format instead of hex format
1419dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
1420dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
14218a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1422bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1423c8e00060SJoe Perches  *       with a dash-separated hex notation
14247c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
14258a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
14268a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
14278a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
142810679643SDaniel Borkmann  *       [S][pfs]
142910679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
143010679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
14318a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
14328a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
14338a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
143410679643SDaniel Borkmann  *       [S][pfs]
143510679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
143610679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
143710679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
143810679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
143929cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
144071dca95dSAndy Shevchenko  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
144171dca95dSAndy Shevchenko  *                of the following flags (see string_escape_mem() for the
144271dca95dSAndy Shevchenko  *                details):
144371dca95dSAndy Shevchenko  *                  a - ESCAPE_ANY
144471dca95dSAndy Shevchenko  *                  c - ESCAPE_SPECIAL
144571dca95dSAndy Shevchenko  *                  h - ESCAPE_HEX
144671dca95dSAndy Shevchenko  *                  n - ESCAPE_NULL
144771dca95dSAndy Shevchenko  *                  o - ESCAPE_OCTAL
144871dca95dSAndy Shevchenko  *                  p - ESCAPE_NP
144971dca95dSAndy Shevchenko  *                  s - ESCAPE_SPACE
145071dca95dSAndy Shevchenko  *                By default ESCAPE_ANY_NP is used.
14519ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
14529ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
14539ac6e44eSJoe Perches  *       Options for %pU are:
14549ac6e44eSJoe Perches  *         b big endian lower case hex (default)
14559ac6e44eSJoe Perches  *         B big endian UPPER case hex
14569ac6e44eSJoe Perches  *         l little endian lower case hex
14579ac6e44eSJoe Perches  *         L little endian UPPER case hex
14589ac6e44eSJoe Perches  *           big endian output byte order is:
14599ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
14609ac6e44eSJoe Perches  *           little endian output byte order is:
14619ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
14627db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
14637db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
14647db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
14657db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
14667db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
1467455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
1468c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
146931550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
147031550a16SAndy Shevchenko  *            a certain separator (' ' by default):
147131550a16SAndy Shevchenko  *              C colon
147231550a16SAndy Shevchenko  *              D dash
147331550a16SAndy Shevchenko  *              N no separator
147431550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
147531550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
1476aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1477aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
1478c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
1479c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
14801031bc58SDmitry Monakhov  * - 'g' For block_device name (gendisk + partition number)
1481900cca29SGeert Uytterhoeven  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1482900cca29SGeert Uytterhoeven  *       (legacy clock framework) of the clock
1483900cca29SGeert Uytterhoeven  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1484900cca29SGeert Uytterhoeven  *        (legacy clock framework) of the clock
1485900cca29SGeert Uytterhoeven  * - 'Cr' For a clock, it prints the current rate of the clock
14865e4ee7b1SMartin Kletzander  *
14875e4ee7b1SMartin Kletzander  * ** Please update also Documentation/printk-formats.txt when making changes **
14889ac6e44eSJoe Perches  *
1489332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1490332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
1491332d2e78SLinus Torvalds  * pointer to the real address.
14924d8a743cSLinus Torvalds  */
1493cf3b429bSJoe Perches static noinline_for_stack
1494cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1495fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
149678a8bf69SLinus Torvalds {
149780c9eb46SRasmus Villemoes 	const int default_width = 2 * sizeof(void *);
1498725fe002SGrant Likely 
14999f36e2c4SKees Cook 	if (!ptr && *fmt != 'K') {
15005e057981SJoe Perches 		/*
15015e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
15025e057981SJoe Perches 		 * tabular output look nice.
15035e057981SJoe Perches 		 */
15045e057981SJoe Perches 		if (spec.field_width == -1)
1505725fe002SGrant Likely 			spec.field_width = default_width;
1506fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
15075e057981SJoe Perches 	}
1508d97106abSLinus Torvalds 
15090fe1ef24SLinus Torvalds 	switch (*fmt) {
15100fe1ef24SLinus Torvalds 	case 'F':
15110c8b946eSFrederic Weisbecker 	case 'f':
15120fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
15130fe1ef24SLinus Torvalds 		/* Fallthrough */
15140fe1ef24SLinus Torvalds 	case 'S':
15159ac6e44eSJoe Perches 	case 's':
15160f77a8d3SNamhyung Kim 	case 'B':
1517b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
1518332d2e78SLinus Torvalds 	case 'R':
1519c7dabef8SBjorn Helgaas 	case 'r':
1520fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
152131550a16SAndy Shevchenko 	case 'h':
152231550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
1523dbc760bcSTejun Heo 	case 'b':
1524dbc760bcSTejun Heo 		switch (fmt[1]) {
1525dbc760bcSTejun Heo 		case 'l':
1526dbc760bcSTejun Heo 			return bitmap_list_string(buf, end, ptr, spec, fmt);
1527dbc760bcSTejun Heo 		default:
1528dbc760bcSTejun Heo 			return bitmap_string(buf, end, ptr, spec, fmt);
1529dbc760bcSTejun Heo 		}
15308a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
15318a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
153276597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
153376597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
15348a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
15358a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
15368a27f7c9SJoe Perches 					 * 4:	1.2.3.4
15378a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
15388a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
15398a27f7c9SJoe Perches 					 */
15408a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
15418a27f7c9SJoe Perches 					 * 4:	001.002.003.004
15428a27f7c9SJoe Perches 					 * 6:   000102...0f
15438a27f7c9SJoe Perches 					 */
15448a27f7c9SJoe Perches 		switch (fmt[1]) {
15458a27f7c9SJoe Perches 		case '6':
15468a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
15478a27f7c9SJoe Perches 		case '4':
15488a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
154910679643SDaniel Borkmann 		case 'S': {
155010679643SDaniel Borkmann 			const union {
155110679643SDaniel Borkmann 				struct sockaddr		raw;
155210679643SDaniel Borkmann 				struct sockaddr_in	v4;
155310679643SDaniel Borkmann 				struct sockaddr_in6	v6;
155410679643SDaniel Borkmann 			} *sa = ptr;
155510679643SDaniel Borkmann 
155610679643SDaniel Borkmann 			switch (sa->raw.sa_family) {
155710679643SDaniel Borkmann 			case AF_INET:
155810679643SDaniel Borkmann 				return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
155910679643SDaniel Borkmann 			case AF_INET6:
156010679643SDaniel Borkmann 				return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
156110679643SDaniel Borkmann 			default:
156210679643SDaniel Borkmann 				return string(buf, end, "(invalid address)", spec);
156310679643SDaniel Borkmann 			}}
15648a27f7c9SJoe Perches 		}
15654aa99606SHarvey Harrison 		break;
156671dca95dSAndy Shevchenko 	case 'E':
156771dca95dSAndy Shevchenko 		return escaped_string(buf, end, ptr, spec, fmt);
15689ac6e44eSJoe Perches 	case 'U':
15699ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
15707db6f5fbSJoe Perches 	case 'V':
15715756b76eSJan Beulich 		{
15725756b76eSJan Beulich 			va_list va;
15735756b76eSJan Beulich 
15745756b76eSJan Beulich 			va_copy(va, *((struct va_format *)ptr)->va);
15755756b76eSJan Beulich 			buf += vsnprintf(buf, end > buf ? end - buf : 0,
15765756b76eSJan Beulich 					 ((struct va_format *)ptr)->fmt, va);
15775756b76eSJan Beulich 			va_end(va);
15785756b76eSJan Beulich 			return buf;
15795756b76eSJan Beulich 		}
1580455cd5abSDan Rosenberg 	case 'K':
1581455cd5abSDan Rosenberg 		/*
1582455cd5abSDan Rosenberg 		 * %pK cannot be used in IRQ context because its test
1583455cd5abSDan Rosenberg 		 * for CAP_SYSLOG would be meaningless.
1584455cd5abSDan Rosenberg 		 */
15853715c530SDan Rosenberg 		if (kptr_restrict && (in_irq() || in_serving_softirq() ||
15863715c530SDan Rosenberg 				      in_nmi())) {
1587455cd5abSDan Rosenberg 			if (spec.field_width == -1)
1588725fe002SGrant Likely 				spec.field_width = default_width;
1589455cd5abSDan Rosenberg 			return string(buf, end, "pK-error", spec);
1590455cd5abSDan Rosenberg 		}
1591312b4e22SRyan Mallon 
1592312b4e22SRyan Mallon 		switch (kptr_restrict) {
1593312b4e22SRyan Mallon 		case 0:
1594312b4e22SRyan Mallon 			/* Always print %pK values */
1595312b4e22SRyan Mallon 			break;
1596312b4e22SRyan Mallon 		case 1: {
1597312b4e22SRyan Mallon 			/*
1598312b4e22SRyan Mallon 			 * Only print the real pointer value if the current
1599312b4e22SRyan Mallon 			 * process has CAP_SYSLOG and is running with the
1600312b4e22SRyan Mallon 			 * same credentials it started with. This is because
1601312b4e22SRyan Mallon 			 * access to files is checked at open() time, but %pK
1602312b4e22SRyan Mallon 			 * checks permission at read() time. We don't want to
1603312b4e22SRyan Mallon 			 * leak pointer values if a binary opens a file using
1604312b4e22SRyan Mallon 			 * %pK and then elevates privileges before reading it.
1605312b4e22SRyan Mallon 			 */
1606312b4e22SRyan Mallon 			const struct cred *cred = current_cred();
1607312b4e22SRyan Mallon 
1608312b4e22SRyan Mallon 			if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1609312b4e22SRyan Mallon 			    !uid_eq(cred->euid, cred->uid) ||
1610312b4e22SRyan Mallon 			    !gid_eq(cred->egid, cred->gid))
161126297607SJoe Perches 				ptr = NULL;
161226297607SJoe Perches 			break;
1613312b4e22SRyan Mallon 		}
1614312b4e22SRyan Mallon 		case 2:
1615312b4e22SRyan Mallon 		default:
1616312b4e22SRyan Mallon 			/* Always print 0's for %pK */
1617312b4e22SRyan Mallon 			ptr = NULL;
1618312b4e22SRyan Mallon 			break;
1619312b4e22SRyan Mallon 		}
1620312b4e22SRyan Mallon 		break;
1621312b4e22SRyan Mallon 
1622c8f44affSMichał Mirosław 	case 'N':
1623c8f44affSMichał Mirosław 		switch (fmt[1]) {
1624c8f44affSMichał Mirosław 		case 'F':
1625c8f44affSMichał Mirosław 			return netdev_feature_string(buf, end, ptr, spec);
1626c8f44affSMichał Mirosław 		}
1627c8f44affSMichał Mirosław 		break;
16287d799210SStepan Moskovchenko 	case 'a':
1629aaf07621SJoe Perches 		return address_val(buf, end, ptr, spec, fmt);
16304b6ccca7SAl Viro 	case 'd':
16314b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
1632900cca29SGeert Uytterhoeven 	case 'C':
1633900cca29SGeert Uytterhoeven 		return clock(buf, end, ptr, spec, fmt);
16344b6ccca7SAl Viro 	case 'D':
16354b6ccca7SAl Viro 		return dentry_name(buf, end,
16364b6ccca7SAl Viro 				   ((const struct file *)ptr)->f_path.dentry,
16374b6ccca7SAl Viro 				   spec, fmt);
16381031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
16391031bc58SDmitry Monakhov 	case 'g':
16401031bc58SDmitry Monakhov 		return bdev_name(buf, end, ptr, spec, fmt);
16411031bc58SDmitry Monakhov #endif
16421031bc58SDmitry Monakhov 
16430fe1ef24SLinus Torvalds 	}
1644fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
1645fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
1646725fe002SGrant Likely 		spec.field_width = default_width;
1647fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
164878a8bf69SLinus Torvalds 	}
1649fef20d9cSFrederic Weisbecker 	spec.base = 16;
1650fef20d9cSFrederic Weisbecker 
1651fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
1652fef20d9cSFrederic Weisbecker }
1653fef20d9cSFrederic Weisbecker 
1654fef20d9cSFrederic Weisbecker /*
1655fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
1656fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
1657fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
1658fef20d9cSFrederic Weisbecker  * to go on the next call).
1659fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
1660fef20d9cSFrederic Weisbecker  *
1661fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
1662fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
1663fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
1664fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
1665fef20d9cSFrederic Weisbecker  *
1666fef20d9cSFrederic Weisbecker  * @fmt: the format string
1667fef20d9cSFrederic Weisbecker  * @type of the token returned
1668fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
1669fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
1670fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
1671fef20d9cSFrederic Weisbecker  * @precision: precision of a number
1672fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
1673fef20d9cSFrederic Weisbecker  */
1674cf3b429bSJoe Perches static noinline_for_stack
1675cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
1676fef20d9cSFrederic Weisbecker {
1677fef20d9cSFrederic Weisbecker 	const char *start = fmt;
1678d0484193SRasmus Villemoes 	char qualifier;
1679fef20d9cSFrederic Weisbecker 
1680fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
1681ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
1682fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
1683fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
1684fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
1685fef20d9cSFrederic Weisbecker 		}
1686fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1687fef20d9cSFrederic Weisbecker 		goto precision;
1688fef20d9cSFrederic Weisbecker 	}
1689fef20d9cSFrederic Weisbecker 
1690fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
1691fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
1692fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
1693fef20d9cSFrederic Weisbecker 			spec->precision = 0;
1694fef20d9cSFrederic Weisbecker 
1695fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1696fef20d9cSFrederic Weisbecker 		goto qualifier;
1697fef20d9cSFrederic Weisbecker 	}
1698fef20d9cSFrederic Weisbecker 
1699fef20d9cSFrederic Weisbecker 	/* By default */
1700fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
1701fef20d9cSFrederic Weisbecker 
1702fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
1703fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
1704fef20d9cSFrederic Weisbecker 			break;
1705fef20d9cSFrederic Weisbecker 	}
1706fef20d9cSFrederic Weisbecker 
1707fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
1708fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
1709fef20d9cSFrederic Weisbecker 		return fmt - start;
1710fef20d9cSFrederic Weisbecker 
1711fef20d9cSFrederic Weisbecker 	/* Process flags */
1712fef20d9cSFrederic Weisbecker 	spec->flags = 0;
1713fef20d9cSFrederic Weisbecker 
1714fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
1715fef20d9cSFrederic Weisbecker 		bool found = true;
1716fef20d9cSFrederic Weisbecker 
1717fef20d9cSFrederic Weisbecker 		++fmt;
1718fef20d9cSFrederic Weisbecker 
1719fef20d9cSFrederic Weisbecker 		switch (*fmt) {
1720fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
1721fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
1722fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
1723fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
1724fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
1725fef20d9cSFrederic Weisbecker 		default:  found = false;
1726fef20d9cSFrederic Weisbecker 		}
1727fef20d9cSFrederic Weisbecker 
1728fef20d9cSFrederic Weisbecker 		if (!found)
1729fef20d9cSFrederic Weisbecker 			break;
1730fef20d9cSFrederic Weisbecker 	}
1731fef20d9cSFrederic Weisbecker 
1732fef20d9cSFrederic Weisbecker 	/* get field width */
1733fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1734fef20d9cSFrederic Weisbecker 
1735fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1736fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1737fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1738fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1739ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1740fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1741fef20d9cSFrederic Weisbecker 	}
1742fef20d9cSFrederic Weisbecker 
1743fef20d9cSFrederic Weisbecker precision:
1744fef20d9cSFrederic Weisbecker 	/* get the precision */
1745fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1746fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1747fef20d9cSFrederic Weisbecker 		++fmt;
1748fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1749fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1750fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1751fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1752fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1753fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1754adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1755fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1756fef20d9cSFrederic Weisbecker 		}
1757fef20d9cSFrederic Weisbecker 	}
1758fef20d9cSFrederic Weisbecker 
1759fef20d9cSFrederic Weisbecker qualifier:
1760fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1761d0484193SRasmus Villemoes 	qualifier = 0;
176275fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
176375fb8f26SAndy Shevchenko 	    _tolower(*fmt) == 'z' || *fmt == 't') {
1764d0484193SRasmus Villemoes 		qualifier = *fmt++;
1765d0484193SRasmus Villemoes 		if (unlikely(qualifier == *fmt)) {
1766d0484193SRasmus Villemoes 			if (qualifier == 'l') {
1767d0484193SRasmus Villemoes 				qualifier = 'L';
1768fef20d9cSFrederic Weisbecker 				++fmt;
1769d0484193SRasmus Villemoes 			} else if (qualifier == 'h') {
1770d0484193SRasmus Villemoes 				qualifier = 'H';
1771a4e94ef0SZhaolei 				++fmt;
1772a4e94ef0SZhaolei 			}
1773fef20d9cSFrederic Weisbecker 		}
1774fef20d9cSFrederic Weisbecker 	}
1775fef20d9cSFrederic Weisbecker 
1776fef20d9cSFrederic Weisbecker 	/* default base */
1777fef20d9cSFrederic Weisbecker 	spec->base = 10;
1778fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1779fef20d9cSFrederic Weisbecker 	case 'c':
1780fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1781fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1782fef20d9cSFrederic Weisbecker 
1783fef20d9cSFrederic Weisbecker 	case 's':
1784fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1785fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1786fef20d9cSFrederic Weisbecker 
1787fef20d9cSFrederic Weisbecker 	case 'p':
1788fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1789ffbfed03SRasmus Villemoes 		return ++fmt - start;
1790fef20d9cSFrederic Weisbecker 
1791fef20d9cSFrederic Weisbecker 	case '%':
1792fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1793fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1794fef20d9cSFrederic Weisbecker 
1795fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1796fef20d9cSFrederic Weisbecker 	case 'o':
1797fef20d9cSFrederic Weisbecker 		spec->base = 8;
1798fef20d9cSFrederic Weisbecker 		break;
1799fef20d9cSFrederic Weisbecker 
1800fef20d9cSFrederic Weisbecker 	case 'x':
1801fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1802fef20d9cSFrederic Weisbecker 
1803fef20d9cSFrederic Weisbecker 	case 'X':
1804fef20d9cSFrederic Weisbecker 		spec->base = 16;
1805fef20d9cSFrederic Weisbecker 		break;
1806fef20d9cSFrederic Weisbecker 
1807fef20d9cSFrederic Weisbecker 	case 'd':
1808fef20d9cSFrederic Weisbecker 	case 'i':
180939e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1810fef20d9cSFrederic Weisbecker 	case 'u':
1811fef20d9cSFrederic Weisbecker 		break;
1812fef20d9cSFrederic Weisbecker 
1813708d96fdSRyan Mallon 	case 'n':
1814708d96fdSRyan Mallon 		/*
1815b006f19bSRasmus Villemoes 		 * Since %n poses a greater security risk than
1816b006f19bSRasmus Villemoes 		 * utility, treat it as any other invalid or
1817b006f19bSRasmus Villemoes 		 * unsupported format specifier.
1818708d96fdSRyan Mallon 		 */
1819708d96fdSRyan Mallon 		/* Fall-through */
1820708d96fdSRyan Mallon 
1821fef20d9cSFrederic Weisbecker 	default:
1822b006f19bSRasmus Villemoes 		WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
1823fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1824fef20d9cSFrederic Weisbecker 		return fmt - start;
1825fef20d9cSFrederic Weisbecker 	}
1826fef20d9cSFrederic Weisbecker 
1827d0484193SRasmus Villemoes 	if (qualifier == 'L')
1828fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1829d0484193SRasmus Villemoes 	else if (qualifier == 'l') {
183051be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
183151be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
1832d0484193SRasmus Villemoes 	} else if (_tolower(qualifier) == 'z') {
1833fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1834d0484193SRasmus Villemoes 	} else if (qualifier == 't') {
1835fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1836d0484193SRasmus Villemoes 	} else if (qualifier == 'H') {
183751be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
183851be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
1839d0484193SRasmus Villemoes 	} else if (qualifier == 'h') {
184051be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
184151be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
1842fef20d9cSFrederic Weisbecker 	} else {
184351be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
184451be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
1845fef20d9cSFrederic Weisbecker 	}
1846fef20d9cSFrederic Weisbecker 
1847fef20d9cSFrederic Weisbecker 	return ++fmt - start;
184878a8bf69SLinus Torvalds }
184978a8bf69SLinus Torvalds 
1850*4d72ba01SRasmus Villemoes static void
1851*4d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width)
1852*4d72ba01SRasmus Villemoes {
1853*4d72ba01SRasmus Villemoes 	spec->field_width = width;
1854*4d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
1855*4d72ba01SRasmus Villemoes 		spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
1856*4d72ba01SRasmus Villemoes 	}
1857*4d72ba01SRasmus Villemoes }
1858*4d72ba01SRasmus Villemoes 
1859*4d72ba01SRasmus Villemoes static void
1860*4d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec)
1861*4d72ba01SRasmus Villemoes {
1862*4d72ba01SRasmus Villemoes 	spec->precision = prec;
1863*4d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
1864*4d72ba01SRasmus Villemoes 		spec->precision = clamp(prec, 0, PRECISION_MAX);
1865*4d72ba01SRasmus Villemoes 	}
1866*4d72ba01SRasmus Villemoes }
1867*4d72ba01SRasmus Villemoes 
18681da177e4SLinus Torvalds /**
18691da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
18701da177e4SLinus Torvalds  * @buf: The buffer to place the result into
18711da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
18721da177e4SLinus Torvalds  * @fmt: The format string to use
18731da177e4SLinus Torvalds  * @args: Arguments for the format string
18741da177e4SLinus Torvalds  *
1875d7ec9a05SRasmus Villemoes  * This function generally follows C99 vsnprintf, but has some
1876d7ec9a05SRasmus Villemoes  * extensions and a few limitations:
1877d7ec9a05SRasmus Villemoes  *
1878d7ec9a05SRasmus Villemoes  * %n is unsupported
18795e4ee7b1SMartin Kletzander  * %p* is handled by pointer()
188020036fdcSAndi Kleen  *
18815e4ee7b1SMartin Kletzander  * See pointer() or Documentation/printk-formats.txt for more
18825e4ee7b1SMartin Kletzander  * extensive description.
18835e4ee7b1SMartin Kletzander  *
18845e4ee7b1SMartin Kletzander  * ** Please update the documentation in both places when making changes **
188580f548e0SAndrew Morton  *
18861da177e4SLinus Torvalds  * The return value is the number of characters which would
18871da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
18881da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
18891da177e4SLinus Torvalds  * number of characters written into @buf as return value
189072fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
18911da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
18921da177e4SLinus Torvalds  * string is truncated.
18931da177e4SLinus Torvalds  *
1894ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
18951da177e4SLinus Torvalds  */
18961da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
18971da177e4SLinus Torvalds {
18981da177e4SLinus Torvalds 	unsigned long long num;
1899d4be151bSAndré Goddard Rosa 	char *str, *end;
1900fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
19011da177e4SLinus Torvalds 
1902f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1903f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
19042aa2f9e2SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
19051da177e4SLinus Torvalds 		return 0;
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds 	str = buf;
1908f796937aSJeremy Fitzhardinge 	end = buf + size;
19091da177e4SLinus Torvalds 
1910f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1911f796937aSJeremy Fitzhardinge 	if (end < buf) {
19121da177e4SLinus Torvalds 		end = ((void *)-1);
1913f796937aSJeremy Fitzhardinge 		size = end - buf;
19141da177e4SLinus Torvalds 	}
19151da177e4SLinus Torvalds 
1916fef20d9cSFrederic Weisbecker 	while (*fmt) {
1917fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1918d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1919fef20d9cSFrederic Weisbecker 
1920fef20d9cSFrederic Weisbecker 		fmt += read;
1921fef20d9cSFrederic Weisbecker 
1922fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1923fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1924fef20d9cSFrederic Weisbecker 			int copy = read;
1925fef20d9cSFrederic Weisbecker 			if (str < end) {
1926fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1927fef20d9cSFrederic Weisbecker 					copy = end - str;
1928fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1929fef20d9cSFrederic Weisbecker 			}
1930fef20d9cSFrederic Weisbecker 			str += read;
1931fef20d9cSFrederic Weisbecker 			break;
19321da177e4SLinus Torvalds 		}
19331da177e4SLinus Torvalds 
1934ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1935*4d72ba01SRasmus Villemoes 			set_field_width(&spec, va_arg(args, int));
1936fef20d9cSFrederic Weisbecker 			break;
19371da177e4SLinus Torvalds 
1938fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1939*4d72ba01SRasmus Villemoes 			set_precision(&spec, va_arg(args, int));
1940fef20d9cSFrederic Weisbecker 			break;
19411da177e4SLinus Torvalds 
1942d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1943d4be151bSAndré Goddard Rosa 			char c;
1944d4be151bSAndré Goddard Rosa 
1945fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1946fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1947f796937aSJeremy Fitzhardinge 					if (str < end)
19481da177e4SLinus Torvalds 						*str = ' ';
19491da177e4SLinus Torvalds 					++str;
1950fef20d9cSFrederic Weisbecker 
19511da177e4SLinus Torvalds 				}
19521da177e4SLinus Torvalds 			}
19531da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1954f796937aSJeremy Fitzhardinge 			if (str < end)
19551da177e4SLinus Torvalds 				*str = c;
19561da177e4SLinus Torvalds 			++str;
1957fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1958f796937aSJeremy Fitzhardinge 				if (str < end)
19591da177e4SLinus Torvalds 					*str = ' ';
19601da177e4SLinus Torvalds 				++str;
19611da177e4SLinus Torvalds 			}
1962fef20d9cSFrederic Weisbecker 			break;
1963d4be151bSAndré Goddard Rosa 		}
19641da177e4SLinus Torvalds 
1965fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1966fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1967fef20d9cSFrederic Weisbecker 			break;
19681da177e4SLinus Torvalds 
1969fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1970ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, va_arg(args, void *),
1971fef20d9cSFrederic Weisbecker 				      spec);
1972fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
19734d8a743cSLinus Torvalds 				fmt++;
1974fef20d9cSFrederic Weisbecker 			break;
19751da177e4SLinus Torvalds 
1976fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1977f796937aSJeremy Fitzhardinge 			if (str < end)
19781da177e4SLinus Torvalds 				*str = '%';
19791da177e4SLinus Torvalds 			++str;
19801da177e4SLinus Torvalds 			break;
19811da177e4SLinus Torvalds 
1982fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1983b006f19bSRasmus Villemoes 			/*
1984b006f19bSRasmus Villemoes 			 * Presumably the arguments passed gcc's type
1985b006f19bSRasmus Villemoes 			 * checking, but there is no safe or sane way
1986b006f19bSRasmus Villemoes 			 * for us to continue parsing the format and
1987b006f19bSRasmus Villemoes 			 * fetching from the va_list; the remaining
1988b006f19bSRasmus Villemoes 			 * specifiers and arguments would be out of
1989b006f19bSRasmus Villemoes 			 * sync.
1990b006f19bSRasmus Villemoes 			 */
1991b006f19bSRasmus Villemoes 			goto out;
1992fef20d9cSFrederic Weisbecker 
1993fef20d9cSFrederic Weisbecker 		default:
1994fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1995fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1996fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1997fef20d9cSFrederic Weisbecker 				break;
1998fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1999fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
2000fef20d9cSFrederic Weisbecker 				break;
2001fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2002fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
2003fef20d9cSFrederic Weisbecker 				break;
2004fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2005ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
2006ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
2007ef124960SJason Gunthorpe 				else
2008fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
2009fef20d9cSFrederic Weisbecker 				break;
2010fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2011fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
2012fef20d9cSFrederic Weisbecker 				break;
2013a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2014a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
2015a4e94ef0SZhaolei 				break;
2016a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2017a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
2018a4e94ef0SZhaolei 				break;
2019fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2020fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
2021fef20d9cSFrederic Weisbecker 				break;
2022fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2023fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
2024fef20d9cSFrederic Weisbecker 				break;
202539e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
202639e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
2027fef20d9cSFrederic Weisbecker 				break;
2028fef20d9cSFrederic Weisbecker 			default:
2029fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
20301da177e4SLinus Torvalds 			}
2031fef20d9cSFrederic Weisbecker 
2032fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
20331da177e4SLinus Torvalds 		}
2034fef20d9cSFrederic Weisbecker 	}
2035fef20d9cSFrederic Weisbecker 
2036b006f19bSRasmus Villemoes out:
2037f796937aSJeremy Fitzhardinge 	if (size > 0) {
2038f796937aSJeremy Fitzhardinge 		if (str < end)
20391da177e4SLinus Torvalds 			*str = '\0';
2040f796937aSJeremy Fitzhardinge 		else
20410a6047eeSLinus Torvalds 			end[-1] = '\0';
2042f796937aSJeremy Fitzhardinge 	}
2043fef20d9cSFrederic Weisbecker 
2044f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
20451da177e4SLinus Torvalds 	return str-buf;
2046fef20d9cSFrederic Weisbecker 
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds /**
20511da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
20521da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20531da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20541da177e4SLinus Torvalds  * @fmt: The format string to use
20551da177e4SLinus Torvalds  * @args: Arguments for the format string
20561da177e4SLinus Torvalds  *
20571da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
2058b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
20591da177e4SLinus Torvalds  * returns 0.
20601da177e4SLinus Torvalds  *
2061ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
206220036fdcSAndi Kleen  *
206320036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20641da177e4SLinus Torvalds  */
20651da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
20661da177e4SLinus Torvalds {
20671da177e4SLinus Torvalds 	int i;
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
20707b9186f5SAndré Goddard Rosa 
2071b921c69fSAnton Arapov 	if (likely(i < size))
2072b921c69fSAnton Arapov 		return i;
2073b921c69fSAnton Arapov 	if (size != 0)
2074b921c69fSAnton Arapov 		return size - 1;
2075b921c69fSAnton Arapov 	return 0;
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds /**
20801da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
20811da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20821da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20831da177e4SLinus Torvalds  * @fmt: The format string to use
20841da177e4SLinus Torvalds  * @...: Arguments for the format string
20851da177e4SLinus Torvalds  *
20861da177e4SLinus Torvalds  * The return value is the number of characters which would be
20871da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
20881da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
20891da177e4SLinus Torvalds  * @size, the resulting string is truncated.
209020036fdcSAndi Kleen  *
209120036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20921da177e4SLinus Torvalds  */
20931da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
20941da177e4SLinus Torvalds {
20951da177e4SLinus Torvalds 	va_list args;
20961da177e4SLinus Torvalds 	int i;
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 	va_start(args, fmt);
20991da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
21001da177e4SLinus Torvalds 	va_end(args);
21017b9186f5SAndré Goddard Rosa 
21021da177e4SLinus Torvalds 	return i;
21031da177e4SLinus Torvalds }
21041da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds /**
21071da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
21081da177e4SLinus Torvalds  * @buf: The buffer to place the result into
21091da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
21101da177e4SLinus Torvalds  * @fmt: The format string to use
21111da177e4SLinus Torvalds  * @...: Arguments for the format string
21121da177e4SLinus Torvalds  *
21131da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
2114b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
21151da177e4SLinus Torvalds  */
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
21181da177e4SLinus Torvalds {
21191da177e4SLinus Torvalds 	va_list args;
21201da177e4SLinus Torvalds 	int i;
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds 	va_start(args, fmt);
2123b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
21241da177e4SLinus Torvalds 	va_end(args);
21257b9186f5SAndré Goddard Rosa 
2126b903c0b8SChangli Gao 	return i;
21271da177e4SLinus Torvalds }
21281da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
21291da177e4SLinus Torvalds 
21301da177e4SLinus Torvalds /**
21311da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
21321da177e4SLinus Torvalds  * @buf: The buffer to place the result into
21331da177e4SLinus Torvalds  * @fmt: The format string to use
21341da177e4SLinus Torvalds  * @args: Arguments for the format string
21351da177e4SLinus Torvalds  *
21361da177e4SLinus Torvalds  * The function returns the number of characters written
213772fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
21381da177e4SLinus Torvalds  * buffer overflows.
21391da177e4SLinus Torvalds  *
2140ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
214120036fdcSAndi Kleen  *
214220036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
21431da177e4SLinus Torvalds  */
21441da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
21451da177e4SLinus Torvalds {
21461da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
21471da177e4SLinus Torvalds }
21481da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds /**
21511da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
21521da177e4SLinus Torvalds  * @buf: The buffer to place the result into
21531da177e4SLinus Torvalds  * @fmt: The format string to use
21541da177e4SLinus Torvalds  * @...: Arguments for the format string
21551da177e4SLinus Torvalds  *
21561da177e4SLinus Torvalds  * The function returns the number of characters written
215772fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
21581da177e4SLinus Torvalds  * buffer overflows.
215920036fdcSAndi Kleen  *
216020036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
21611da177e4SLinus Torvalds  */
21621da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
21631da177e4SLinus Torvalds {
21641da177e4SLinus Torvalds 	va_list args;
21651da177e4SLinus Torvalds 	int i;
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 	va_start(args, fmt);
21681da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
21691da177e4SLinus Torvalds 	va_end(args);
21707b9186f5SAndré Goddard Rosa 
21711da177e4SLinus Torvalds 	return i;
21721da177e4SLinus Torvalds }
21731da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
21741da177e4SLinus Torvalds 
21754370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
21764370aa4aSLai Jiangshan /*
21774370aa4aSLai Jiangshan  * bprintf service:
21784370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
21794370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
21804370aa4aSLai Jiangshan  */
21814370aa4aSLai Jiangshan 
21824370aa4aSLai Jiangshan /**
21834370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
21844370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
21854370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
21864370aa4aSLai Jiangshan  * @fmt: The format string to use
21874370aa4aSLai Jiangshan  * @args: Arguments for the format string
21884370aa4aSLai Jiangshan  *
21894370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
2190da3dae54SMasanari Iida  * is skipped.
21914370aa4aSLai Jiangshan  *
21924370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
21934370aa4aSLai Jiangshan  * the given input.
21944370aa4aSLai Jiangshan  *
21954370aa4aSLai Jiangshan  * NOTE:
21964370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
21974370aa4aSLai Jiangshan  * valid for bstr_printf().
21984370aa4aSLai Jiangshan  */
21994370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
22004370aa4aSLai Jiangshan {
2201fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
22024370aa4aSLai Jiangshan 	char *str, *end;
22034370aa4aSLai Jiangshan 
22044370aa4aSLai Jiangshan 	str = (char *)bin_buf;
22054370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
22064370aa4aSLai Jiangshan 
22074370aa4aSLai Jiangshan #define save_arg(type)							\
22084370aa4aSLai Jiangshan do {									\
22094370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
22104370aa4aSLai Jiangshan 		unsigned long long value;				\
22114370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
22124370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
22134370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
22144370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
22154370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
22164370aa4aSLai Jiangshan 		}							\
22174370aa4aSLai Jiangshan 	} else {							\
22184370aa4aSLai Jiangshan 		unsigned long value;					\
22194370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
22204370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
22214370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
22224370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
22234370aa4aSLai Jiangshan 	}								\
22244370aa4aSLai Jiangshan 	str += sizeof(type);						\
22254370aa4aSLai Jiangshan } while (0)
22264370aa4aSLai Jiangshan 
2227fef20d9cSFrederic Weisbecker 	while (*fmt) {
2228d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
22294370aa4aSLai Jiangshan 
2230fef20d9cSFrederic Weisbecker 		fmt += read;
2231fef20d9cSFrederic Weisbecker 
2232fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2233fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
2234d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
2235fef20d9cSFrederic Weisbecker 			break;
2236b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
2237b006f19bSRasmus Villemoes 			goto out;
2238fef20d9cSFrederic Weisbecker 
2239ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2240fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
22414370aa4aSLai Jiangshan 			save_arg(int);
2242fef20d9cSFrederic Weisbecker 			break;
22434370aa4aSLai Jiangshan 
2244fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
22454370aa4aSLai Jiangshan 			save_arg(char);
2246fef20d9cSFrederic Weisbecker 			break;
2247fef20d9cSFrederic Weisbecker 
2248fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
22494370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
22504370aa4aSLai Jiangshan 			size_t len;
22516c356634SAndré Goddard Rosa 
22524370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
22534370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
22540f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
22556c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
22566c356634SAndré Goddard Rosa 			if (str + len < end)
22576c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
22586c356634SAndré Goddard Rosa 			str += len;
2259fef20d9cSFrederic Weisbecker 			break;
22604370aa4aSLai Jiangshan 		}
2261fef20d9cSFrederic Weisbecker 
2262fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
22634370aa4aSLai Jiangshan 			save_arg(void *);
22644370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2265fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
22664370aa4aSLai Jiangshan 				fmt++;
2267fef20d9cSFrederic Weisbecker 			break;
2268fef20d9cSFrederic Weisbecker 
2269fef20d9cSFrederic Weisbecker 		default:
2270fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2271fef20d9cSFrederic Weisbecker 
2272fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2273fef20d9cSFrederic Weisbecker 				save_arg(long long);
2274fef20d9cSFrederic Weisbecker 				break;
2275fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2276fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2277fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
2278fef20d9cSFrederic Weisbecker 				break;
2279fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2280fef20d9cSFrederic Weisbecker 				save_arg(size_t);
2281fef20d9cSFrederic Weisbecker 				break;
2282fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2283fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
2284fef20d9cSFrederic Weisbecker 				break;
2285a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2286a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2287a4e94ef0SZhaolei 				save_arg(char);
2288a4e94ef0SZhaolei 				break;
2289fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2290fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2291fef20d9cSFrederic Weisbecker 				save_arg(short);
2292fef20d9cSFrederic Weisbecker 				break;
2293fef20d9cSFrederic Weisbecker 			default:
2294fef20d9cSFrederic Weisbecker 				save_arg(int);
2295fef20d9cSFrederic Weisbecker 			}
2296fef20d9cSFrederic Weisbecker 		}
2297fef20d9cSFrederic Weisbecker 	}
2298fef20d9cSFrederic Weisbecker 
2299b006f19bSRasmus Villemoes out:
23007b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2301fef20d9cSFrederic Weisbecker #undef save_arg
23024370aa4aSLai Jiangshan }
23034370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
23044370aa4aSLai Jiangshan 
23054370aa4aSLai Jiangshan /**
23064370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
23074370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
23084370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
23094370aa4aSLai Jiangshan  * @fmt: The format string to use
23104370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
23114370aa4aSLai Jiangshan  *
23124370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
23134370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
23144370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
23154370aa4aSLai Jiangshan  *
23164370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
23170efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
23184370aa4aSLai Jiangshan  *
23194370aa4aSLai Jiangshan  * The return value is the number of characters which would
23204370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
23214370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
23224370aa4aSLai Jiangshan  * number of characters written into @buf as return value
23234370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
23244370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
23254370aa4aSLai Jiangshan  * string is truncated.
23264370aa4aSLai Jiangshan  */
23274370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
23284370aa4aSLai Jiangshan {
2329fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
2330d4be151bSAndré Goddard Rosa 	char *str, *end;
2331d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
23324370aa4aSLai Jiangshan 
2333762abb51SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
23344370aa4aSLai Jiangshan 		return 0;
23354370aa4aSLai Jiangshan 
23364370aa4aSLai Jiangshan 	str = buf;
23374370aa4aSLai Jiangshan 	end = buf + size;
23384370aa4aSLai Jiangshan 
23394370aa4aSLai Jiangshan #define get_arg(type)							\
23404370aa4aSLai Jiangshan ({									\
23414370aa4aSLai Jiangshan 	typeof(type) value;						\
23424370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
23434370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
23444370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
23454370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
23464370aa4aSLai Jiangshan 	} else {							\
23474370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
23484370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
23494370aa4aSLai Jiangshan 	}								\
23504370aa4aSLai Jiangshan 	args += sizeof(type);						\
23514370aa4aSLai Jiangshan 	value;								\
23524370aa4aSLai Jiangshan })
23534370aa4aSLai Jiangshan 
23544370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
23554370aa4aSLai Jiangshan 	if (end < buf) {
23564370aa4aSLai Jiangshan 		end = ((void *)-1);
23574370aa4aSLai Jiangshan 		size = end - buf;
23584370aa4aSLai Jiangshan 	}
23594370aa4aSLai Jiangshan 
2360fef20d9cSFrederic Weisbecker 	while (*fmt) {
2361fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2362d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2363fef20d9cSFrederic Weisbecker 
2364fef20d9cSFrederic Weisbecker 		fmt += read;
2365fef20d9cSFrederic Weisbecker 
2366fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2367fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2368fef20d9cSFrederic Weisbecker 			int copy = read;
2369fef20d9cSFrederic Weisbecker 			if (str < end) {
2370fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2371fef20d9cSFrederic Weisbecker 					copy = end - str;
2372fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2373fef20d9cSFrederic Weisbecker 			}
2374fef20d9cSFrederic Weisbecker 			str += read;
2375fef20d9cSFrederic Weisbecker 			break;
23764370aa4aSLai Jiangshan 		}
23774370aa4aSLai Jiangshan 
2378ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2379*4d72ba01SRasmus Villemoes 			set_field_width(&spec, get_arg(int));
2380fef20d9cSFrederic Weisbecker 			break;
23814370aa4aSLai Jiangshan 
2382fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2383*4d72ba01SRasmus Villemoes 			set_precision(&spec, get_arg(int));
2384fef20d9cSFrederic Weisbecker 			break;
23854370aa4aSLai Jiangshan 
2386d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2387d4be151bSAndré Goddard Rosa 			char c;
2388d4be151bSAndré Goddard Rosa 
2389fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2390fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
23914370aa4aSLai Jiangshan 					if (str < end)
23924370aa4aSLai Jiangshan 						*str = ' ';
23934370aa4aSLai Jiangshan 					++str;
23944370aa4aSLai Jiangshan 				}
23954370aa4aSLai Jiangshan 			}
23964370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
23974370aa4aSLai Jiangshan 			if (str < end)
23984370aa4aSLai Jiangshan 				*str = c;
23994370aa4aSLai Jiangshan 			++str;
2400fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
24014370aa4aSLai Jiangshan 				if (str < end)
24024370aa4aSLai Jiangshan 					*str = ' ';
24034370aa4aSLai Jiangshan 				++str;
24044370aa4aSLai Jiangshan 			}
2405fef20d9cSFrederic Weisbecker 			break;
2406d4be151bSAndré Goddard Rosa 		}
24074370aa4aSLai Jiangshan 
2408fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
24094370aa4aSLai Jiangshan 			const char *str_arg = args;
2410d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
2411fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
2412fef20d9cSFrederic Weisbecker 			break;
24134370aa4aSLai Jiangshan 		}
24144370aa4aSLai Jiangshan 
2415fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2416ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, get_arg(void *), spec);
2417fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
24184370aa4aSLai Jiangshan 				fmt++;
2419fef20d9cSFrederic Weisbecker 			break;
24204370aa4aSLai Jiangshan 
2421fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
24224370aa4aSLai Jiangshan 			if (str < end)
24234370aa4aSLai Jiangshan 				*str = '%';
24244370aa4aSLai Jiangshan 			++str;
2425fef20d9cSFrederic Weisbecker 			break;
2426fef20d9cSFrederic Weisbecker 
2427b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
2428b006f19bSRasmus Villemoes 			goto out;
2429b006f19bSRasmus Villemoes 
2430d4be151bSAndré Goddard Rosa 		default: {
2431d4be151bSAndré Goddard Rosa 			unsigned long long num;
2432d4be151bSAndré Goddard Rosa 
2433fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2434fef20d9cSFrederic Weisbecker 
2435fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
24364370aa4aSLai Jiangshan 				num = get_arg(long long);
2437fef20d9cSFrederic Weisbecker 				break;
2438fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2439fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2440fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
2441fef20d9cSFrederic Weisbecker 				break;
2442fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
24434370aa4aSLai Jiangshan 				num = get_arg(size_t);
2444fef20d9cSFrederic Weisbecker 				break;
2445fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
24464370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
2447fef20d9cSFrederic Weisbecker 				break;
2448a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2449a4e94ef0SZhaolei 				num = get_arg(unsigned char);
2450a4e94ef0SZhaolei 				break;
2451a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2452a4e94ef0SZhaolei 				num = get_arg(signed char);
2453a4e94ef0SZhaolei 				break;
2454fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2455fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
2456fef20d9cSFrederic Weisbecker 				break;
2457fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2458fef20d9cSFrederic Weisbecker 				num = get_arg(short);
2459fef20d9cSFrederic Weisbecker 				break;
2460fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
24614370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
2462fef20d9cSFrederic Weisbecker 				break;
2463fef20d9cSFrederic Weisbecker 			default:
2464fef20d9cSFrederic Weisbecker 				num = get_arg(int);
24654370aa4aSLai Jiangshan 			}
2466fef20d9cSFrederic Weisbecker 
2467fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
2468d4be151bSAndré Goddard Rosa 		} /* default: */
2469d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
2470d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
2471fef20d9cSFrederic Weisbecker 
2472b006f19bSRasmus Villemoes out:
24734370aa4aSLai Jiangshan 	if (size > 0) {
24744370aa4aSLai Jiangshan 		if (str < end)
24754370aa4aSLai Jiangshan 			*str = '\0';
24764370aa4aSLai Jiangshan 		else
24774370aa4aSLai Jiangshan 			end[-1] = '\0';
24784370aa4aSLai Jiangshan 	}
2479fef20d9cSFrederic Weisbecker 
24804370aa4aSLai Jiangshan #undef get_arg
24814370aa4aSLai Jiangshan 
24824370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
24834370aa4aSLai Jiangshan 	return str - buf;
24844370aa4aSLai Jiangshan }
24854370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
24864370aa4aSLai Jiangshan 
24874370aa4aSLai Jiangshan /**
24884370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
24894370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
24904370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
24914370aa4aSLai Jiangshan  * @fmt: The format string to use
24924370aa4aSLai Jiangshan  * @...: Arguments for the format string
24934370aa4aSLai Jiangshan  *
24944370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
24954370aa4aSLai Jiangshan  * into @bin_buf.
24964370aa4aSLai Jiangshan  */
24974370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
24984370aa4aSLai Jiangshan {
24994370aa4aSLai Jiangshan 	va_list args;
25004370aa4aSLai Jiangshan 	int ret;
25014370aa4aSLai Jiangshan 
25024370aa4aSLai Jiangshan 	va_start(args, fmt);
25034370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
25044370aa4aSLai Jiangshan 	va_end(args);
25057b9186f5SAndré Goddard Rosa 
25064370aa4aSLai Jiangshan 	return ret;
25074370aa4aSLai Jiangshan }
25084370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
25094370aa4aSLai Jiangshan 
25104370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
25114370aa4aSLai Jiangshan 
25121da177e4SLinus Torvalds /**
25131da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
25141da177e4SLinus Torvalds  * @buf:	input buffer
25151da177e4SLinus Torvalds  * @fmt:	format of buffer
25161da177e4SLinus Torvalds  * @args:	arguments
25171da177e4SLinus Torvalds  */
25181da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
25191da177e4SLinus Torvalds {
25201da177e4SLinus Torvalds 	const char *str = buf;
25211da177e4SLinus Torvalds 	char *next;
25221da177e4SLinus Torvalds 	char digit;
25231da177e4SLinus Torvalds 	int num = 0;
2524ef0658f3SJoe Perches 	u8 qualifier;
252553809751SJan Beulich 	unsigned int base;
252653809751SJan Beulich 	union {
252753809751SJan Beulich 		long long s;
252853809751SJan Beulich 		unsigned long long u;
252953809751SJan Beulich 	} val;
2530ef0658f3SJoe Perches 	s16 field_width;
2531d4be151bSAndré Goddard Rosa 	bool is_sign;
25321da177e4SLinus Torvalds 
2533da99075cSJan Beulich 	while (*fmt) {
25341da177e4SLinus Torvalds 		/* skip any white space in format */
25351da177e4SLinus Torvalds 		/* white space in format matchs any amount of
25361da177e4SLinus Torvalds 		 * white space, including none, in the input.
25371da177e4SLinus Torvalds 		 */
25381da177e4SLinus Torvalds 		if (isspace(*fmt)) {
2539e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
2540e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
25411da177e4SLinus Torvalds 		}
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
25441da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
25451da177e4SLinus Torvalds 			if (*fmt++ != *str++)
25461da177e4SLinus Torvalds 				break;
25471da177e4SLinus Torvalds 			continue;
25481da177e4SLinus Torvalds 		}
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds 		if (!*fmt)
25511da177e4SLinus Torvalds 			break;
25521da177e4SLinus Torvalds 		++fmt;
25531da177e4SLinus Torvalds 
25541da177e4SLinus Torvalds 		/* skip this conversion.
25551da177e4SLinus Torvalds 		 * advance both strings to next white space
25561da177e4SLinus Torvalds 		 */
25571da177e4SLinus Torvalds 		if (*fmt == '*') {
2558da99075cSJan Beulich 			if (!*str)
2559da99075cSJan Beulich 				break;
25608fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
25611da177e4SLinus Torvalds 				fmt++;
25621da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
25631da177e4SLinus Torvalds 				str++;
25641da177e4SLinus Torvalds 			continue;
25651da177e4SLinus Torvalds 		}
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds 		/* get field width */
25681da177e4SLinus Torvalds 		field_width = -1;
256953809751SJan Beulich 		if (isdigit(*fmt)) {
25701da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
257153809751SJan Beulich 			if (field_width <= 0)
257253809751SJan Beulich 				break;
257353809751SJan Beulich 		}
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 		/* get conversion qualifier */
25761da177e4SLinus Torvalds 		qualifier = -1;
257775fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
257875fb8f26SAndy Shevchenko 		    _tolower(*fmt) == 'z') {
25791da177e4SLinus Torvalds 			qualifier = *fmt++;
25801da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
25811da177e4SLinus Torvalds 				if (qualifier == 'h') {
25821da177e4SLinus Torvalds 					qualifier = 'H';
25831da177e4SLinus Torvalds 					fmt++;
25841da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
25851da177e4SLinus Torvalds 					qualifier = 'L';
25861da177e4SLinus Torvalds 					fmt++;
25871da177e4SLinus Torvalds 				}
25881da177e4SLinus Torvalds 			}
25891da177e4SLinus Torvalds 		}
25901da177e4SLinus Torvalds 
2591da99075cSJan Beulich 		if (!*fmt)
2592da99075cSJan Beulich 			break;
2593da99075cSJan Beulich 
2594da99075cSJan Beulich 		if (*fmt == 'n') {
2595da99075cSJan Beulich 			/* return number of characters read so far */
2596da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
2597da99075cSJan Beulich 			++fmt;
2598da99075cSJan Beulich 			continue;
2599da99075cSJan Beulich 		}
2600da99075cSJan Beulich 
2601da99075cSJan Beulich 		if (!*str)
26021da177e4SLinus Torvalds 			break;
26031da177e4SLinus Torvalds 
2604d4be151bSAndré Goddard Rosa 		base = 10;
26053f623ebaSFabian Frederick 		is_sign = false;
2606d4be151bSAndré Goddard Rosa 
26071da177e4SLinus Torvalds 		switch (*fmt++) {
26081da177e4SLinus Torvalds 		case 'c':
26091da177e4SLinus Torvalds 		{
26101da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
26111da177e4SLinus Torvalds 			if (field_width == -1)
26121da177e4SLinus Torvalds 				field_width = 1;
26131da177e4SLinus Torvalds 			do {
26141da177e4SLinus Torvalds 				*s++ = *str++;
26151da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
26161da177e4SLinus Torvalds 			num++;
26171da177e4SLinus Torvalds 		}
26181da177e4SLinus Torvalds 		continue;
26191da177e4SLinus Torvalds 		case 's':
26201da177e4SLinus Torvalds 		{
26211da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
26221da177e4SLinus Torvalds 			if (field_width == -1)
26234be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
26241da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
2625e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds 			/* now copy until next white space */
26287b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
26291da177e4SLinus Torvalds 				*s++ = *str++;
26301da177e4SLinus Torvalds 			*s = '\0';
26311da177e4SLinus Torvalds 			num++;
26321da177e4SLinus Torvalds 		}
26331da177e4SLinus Torvalds 		continue;
26341da177e4SLinus Torvalds 		case 'o':
26351da177e4SLinus Torvalds 			base = 8;
26361da177e4SLinus Torvalds 			break;
26371da177e4SLinus Torvalds 		case 'x':
26381da177e4SLinus Torvalds 		case 'X':
26391da177e4SLinus Torvalds 			base = 16;
26401da177e4SLinus Torvalds 			break;
26411da177e4SLinus Torvalds 		case 'i':
26421da177e4SLinus Torvalds 			base = 0;
26431da177e4SLinus Torvalds 		case 'd':
26443f623ebaSFabian Frederick 			is_sign = true;
26451da177e4SLinus Torvalds 		case 'u':
26461da177e4SLinus Torvalds 			break;
26471da177e4SLinus Torvalds 		case '%':
26481da177e4SLinus Torvalds 			/* looking for '%' in str */
26491da177e4SLinus Torvalds 			if (*str++ != '%')
26501da177e4SLinus Torvalds 				return num;
26511da177e4SLinus Torvalds 			continue;
26521da177e4SLinus Torvalds 		default:
26531da177e4SLinus Torvalds 			/* invalid format; stop here */
26541da177e4SLinus Torvalds 			return num;
26551da177e4SLinus Torvalds 		}
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds 		/* have some sort of integer conversion.
26581da177e4SLinus Torvalds 		 * first, skip white space in buffer.
26591da177e4SLinus Torvalds 		 */
2660e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds 		digit = *str;
26631da177e4SLinus Torvalds 		if (is_sign && digit == '-')
26641da177e4SLinus Torvalds 			digit = *(str + 1);
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds 		if (!digit
26671da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
26681da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
26691da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
26701da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
26711da177e4SLinus Torvalds 			break;
26721da177e4SLinus Torvalds 
267353809751SJan Beulich 		if (is_sign)
267453809751SJan Beulich 			val.s = qualifier != 'L' ?
267553809751SJan Beulich 				simple_strtol(str, &next, base) :
267653809751SJan Beulich 				simple_strtoll(str, &next, base);
267753809751SJan Beulich 		else
267853809751SJan Beulich 			val.u = qualifier != 'L' ?
267953809751SJan Beulich 				simple_strtoul(str, &next, base) :
268053809751SJan Beulich 				simple_strtoull(str, &next, base);
268153809751SJan Beulich 
268253809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
268353809751SJan Beulich 			if (base == 0)
268453809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
268553809751SJan Beulich 			while (next - str > field_width) {
268653809751SJan Beulich 				if (is_sign)
268753809751SJan Beulich 					val.s = div_s64(val.s, base);
268853809751SJan Beulich 				else
268953809751SJan Beulich 					val.u = div_u64(val.u, base);
269053809751SJan Beulich 				--next;
269153809751SJan Beulich 			}
269253809751SJan Beulich 		}
269353809751SJan Beulich 
26941da177e4SLinus Torvalds 		switch (qualifier) {
26951da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
269653809751SJan Beulich 			if (is_sign)
269753809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
269853809751SJan Beulich 			else
269953809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
27001da177e4SLinus Torvalds 			break;
27011da177e4SLinus Torvalds 		case 'h':
270253809751SJan Beulich 			if (is_sign)
270353809751SJan Beulich 				*va_arg(args, short *) = val.s;
270453809751SJan Beulich 			else
270553809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
27061da177e4SLinus Torvalds 			break;
27071da177e4SLinus Torvalds 		case 'l':
270853809751SJan Beulich 			if (is_sign)
270953809751SJan Beulich 				*va_arg(args, long *) = val.s;
271053809751SJan Beulich 			else
271153809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
27121da177e4SLinus Torvalds 			break;
27131da177e4SLinus Torvalds 		case 'L':
271453809751SJan Beulich 			if (is_sign)
271553809751SJan Beulich 				*va_arg(args, long long *) = val.s;
271653809751SJan Beulich 			else
271753809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
27181da177e4SLinus Torvalds 			break;
27191da177e4SLinus Torvalds 		case 'Z':
27201da177e4SLinus Torvalds 		case 'z':
272153809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
27221da177e4SLinus Torvalds 			break;
27231da177e4SLinus Torvalds 		default:
272453809751SJan Beulich 			if (is_sign)
272553809751SJan Beulich 				*va_arg(args, int *) = val.s;
272653809751SJan Beulich 			else
272753809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
27281da177e4SLinus Torvalds 			break;
27291da177e4SLinus Torvalds 		}
27301da177e4SLinus Torvalds 		num++;
27311da177e4SLinus Torvalds 
27321da177e4SLinus Torvalds 		if (!next)
27331da177e4SLinus Torvalds 			break;
27341da177e4SLinus Torvalds 		str = next;
27351da177e4SLinus Torvalds 	}
2736c6b40d16SJohannes Berg 
27371da177e4SLinus Torvalds 	return num;
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
27401da177e4SLinus Torvalds 
27411da177e4SLinus Torvalds /**
27421da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
27431da177e4SLinus Torvalds  * @buf:	input buffer
27441da177e4SLinus Torvalds  * @fmt:	formatting of buffer
27451da177e4SLinus Torvalds  * @...:	resulting arguments
27461da177e4SLinus Torvalds  */
27471da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
27481da177e4SLinus Torvalds {
27491da177e4SLinus Torvalds 	va_list args;
27501da177e4SLinus Torvalds 	int i;
27511da177e4SLinus Torvalds 
27521da177e4SLinus Torvalds 	va_start(args, fmt);
27531da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
27541da177e4SLinus Torvalds 	va_end(args);
27557b9186f5SAndré Goddard Rosa 
27561da177e4SLinus Torvalds 	return i;
27571da177e4SLinus Torvalds }
27581da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2759