xref: /openbmc/linux/lib/vsprintf.c (revision 7c43d9a30c527d9e06e2c55f82b56f28df43caed)
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>
20900cca29SGeert Uytterhoeven #include <linux/clk-provider.h>
218bc3bcc9SPaul Gortmaker #include <linux/module.h>	/* for KSYM_SYMBOL_LEN */
221da177e4SLinus Torvalds #include <linux/types.h>
231da177e4SLinus Torvalds #include <linux/string.h>
241da177e4SLinus Torvalds #include <linux/ctype.h>
251da177e4SLinus Torvalds #include <linux/kernel.h>
260fe1ef24SLinus Torvalds #include <linux/kallsyms.h>
2753809751SJan Beulich #include <linux/math64.h>
280fe1ef24SLinus Torvalds #include <linux/uaccess.h>
29332d2e78SLinus Torvalds #include <linux/ioport.h>
304b6ccca7SAl Viro #include <linux/dcache.h>
31312b4e22SRyan Mallon #include <linux/cred.h>
328a27f7c9SJoe Perches #include <net/addrconf.h>
331da177e4SLinus Torvalds 
344e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
35deac93dfSJames Bottomley #include <asm/sections.h>	/* for dereference_function_descriptor() */
36*7c43d9a3SRasmus Villemoes #include <asm/byteorder.h>	/* cpu_to_le16 */
371da177e4SLinus Torvalds 
3871dca95dSAndy Shevchenko #include <linux/string_helpers.h>
391dff46d6SAlexey Dobriyan #include "kstrtox.h"
40aa46a63eSHarvey Harrison 
411da177e4SLinus Torvalds /**
421da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
431da177e4SLinus Torvalds  * @cp: The start of the string
441da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
451da177e4SLinus Torvalds  * @base: The number base to use
46462e4711SEldad Zack  *
47462e4711SEldad Zack  * This function is obsolete. Please use kstrtoull instead.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
501da177e4SLinus Torvalds {
511dff46d6SAlexey Dobriyan 	unsigned long long result;
521dff46d6SAlexey Dobriyan 	unsigned int rv;
531da177e4SLinus Torvalds 
541dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
551dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
561dff46d6SAlexey Dobriyan 	/* FIXME */
571dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
58aa46a63eSHarvey Harrison 
591da177e4SLinus Torvalds 	if (endp)
601da177e4SLinus Torvalds 		*endp = (char *)cp;
617b9186f5SAndré Goddard Rosa 
621da177e4SLinus Torvalds 	return result;
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /**
67922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
68922ac25cSAndré Goddard Rosa  * @cp: The start of the string
69922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
70922ac25cSAndré Goddard Rosa  * @base: The number base to use
71462e4711SEldad Zack  *
72462e4711SEldad Zack  * This function is obsolete. Please use kstrtoul instead.
73922ac25cSAndré Goddard Rosa  */
74922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
75922ac25cSAndré Goddard Rosa {
76922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
77922ac25cSAndré Goddard Rosa }
78922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
79922ac25cSAndré Goddard Rosa 
80922ac25cSAndré Goddard Rosa /**
81922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
82922ac25cSAndré Goddard Rosa  * @cp: The start of the string
83922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
84922ac25cSAndré Goddard Rosa  * @base: The number base to use
85462e4711SEldad Zack  *
86462e4711SEldad Zack  * This function is obsolete. Please use kstrtol instead.
87922ac25cSAndré Goddard Rosa  */
88922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
89922ac25cSAndré Goddard Rosa {
90922ac25cSAndré Goddard Rosa 	if (*cp == '-')
91922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
92922ac25cSAndré Goddard Rosa 
93922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
94922ac25cSAndré Goddard Rosa }
95922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
96922ac25cSAndré Goddard Rosa 
97922ac25cSAndré Goddard Rosa /**
981da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
991da177e4SLinus Torvalds  * @cp: The start of the string
1001da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1011da177e4SLinus Torvalds  * @base: The number base to use
102462e4711SEldad Zack  *
103462e4711SEldad Zack  * This function is obsolete. Please use kstrtoll instead.
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	if (*cp == '-')
1081da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1097b9186f5SAndré Goddard Rosa 
1101da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1111da177e4SLinus Torvalds }
11298d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1131da177e4SLinus Torvalds 
114cf3b429bSJoe Perches static noinline_for_stack
115cf3b429bSJoe Perches int skip_atoi(const char **s)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	int i = 0;
1181da177e4SLinus Torvalds 
11943e5b666SRasmus Villemoes 	do {
1201da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
12143e5b666SRasmus Villemoes 	} while (isdigit(**s));
1227b9186f5SAndré Goddard Rosa 
1231da177e4SLinus Torvalds 	return i;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
126*7c43d9a3SRasmus Villemoes /*
127*7c43d9a3SRasmus Villemoes  * Decimal conversion is by far the most typical, and is used for
128*7c43d9a3SRasmus Villemoes  * /proc and /sys data. This directly impacts e.g. top performance
129*7c43d9a3SRasmus Villemoes  * with many processes running. We optimize it for speed by emitting
130*7c43d9a3SRasmus Villemoes  * two characters at a time, using a 200 byte lookup table. This
131*7c43d9a3SRasmus Villemoes  * roughly halves the number of multiplications compared to computing
132*7c43d9a3SRasmus Villemoes  * the digits one at a time. Implementation strongly inspired by the
133*7c43d9a3SRasmus Villemoes  * previous version, which in turn used ideas described at
134*7c43d9a3SRasmus Villemoes  * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
135*7c43d9a3SRasmus Villemoes  * from the author, Douglas W. Jones).
136*7c43d9a3SRasmus Villemoes  *
137*7c43d9a3SRasmus Villemoes  * It turns out there is precisely one 26 bit fixed-point
138*7c43d9a3SRasmus Villemoes  * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
139*7c43d9a3SRasmus Villemoes  * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
140*7c43d9a3SRasmus Villemoes  * range happens to be somewhat larger (x <= 1073741898), but that's
141*7c43d9a3SRasmus Villemoes  * irrelevant for our purpose.
142*7c43d9a3SRasmus Villemoes  *
143*7c43d9a3SRasmus Villemoes  * For dividing a number in the range [10^4, 10^6-1] by 100, we still
144*7c43d9a3SRasmus Villemoes  * need a 32x32->64 bit multiply, so we simply use the same constant.
145*7c43d9a3SRasmus Villemoes  *
146*7c43d9a3SRasmus Villemoes  * For dividing a number in the range [100, 10^4-1] by 100, there are
147*7c43d9a3SRasmus Villemoes  * several options. The simplest is (x * 0x147b) >> 19, which is valid
148*7c43d9a3SRasmus Villemoes  * for all x <= 43698.
149133fd9f5SDenys Vlasenko  */
1504277eeddSDenis Vlasenko 
151*7c43d9a3SRasmus Villemoes static const u16 decpair[100] = {
152*7c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
153*7c43d9a3SRasmus Villemoes 	_( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
154*7c43d9a3SRasmus Villemoes 	_(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
155*7c43d9a3SRasmus Villemoes 	_(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
156*7c43d9a3SRasmus Villemoes 	_(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
157*7c43d9a3SRasmus Villemoes 	_(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
158*7c43d9a3SRasmus Villemoes 	_(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
159*7c43d9a3SRasmus Villemoes 	_(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
160*7c43d9a3SRasmus Villemoes 	_(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
161*7c43d9a3SRasmus Villemoes 	_(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
162*7c43d9a3SRasmus Villemoes 	_(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
163*7c43d9a3SRasmus Villemoes #undef _
164*7c43d9a3SRasmus Villemoes };
1654277eeddSDenis Vlasenko 
1667b9186f5SAndré Goddard Rosa /*
167*7c43d9a3SRasmus Villemoes  * This will print a single '0' even if r == 0, since we would
168*7c43d9a3SRasmus Villemoes  * immediately jump to out_r where two 0s would be written and one of
169*7c43d9a3SRasmus Villemoes  * them then discarded. This is needed by ip4_string below. All other
170*7c43d9a3SRasmus Villemoes  * callers pass a non-zero value of r.
171133fd9f5SDenys Vlasenko */
172133fd9f5SDenys Vlasenko static noinline_for_stack
173133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
174133fd9f5SDenys Vlasenko {
175133fd9f5SDenys Vlasenko 	unsigned q;
1764277eeddSDenis Vlasenko 
177*7c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^8 */
178*7c43d9a3SRasmus Villemoes 	if (r < 100)
179*7c43d9a3SRasmus Villemoes 		goto out_r;
180cb239d0aSGeorge Spelvin 
181*7c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^8 */
182*7c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
183*7c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
184*7c43d9a3SRasmus Villemoes 	buf += 2;
185*7c43d9a3SRasmus Villemoes 
186*7c43d9a3SRasmus Villemoes 	/* 1 <= q < 10^6 */
187*7c43d9a3SRasmus Villemoes 	if (q < 100)
188*7c43d9a3SRasmus Villemoes 		goto out_q;
189*7c43d9a3SRasmus Villemoes 
190*7c43d9a3SRasmus Villemoes 	/*  100 <= q < 10^6 */
191*7c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
192*7c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
193*7c43d9a3SRasmus Villemoes 	buf += 2;
194*7c43d9a3SRasmus Villemoes 
195*7c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^4 */
196*7c43d9a3SRasmus Villemoes 	if (r < 100)
197*7c43d9a3SRasmus Villemoes 		goto out_r;
198*7c43d9a3SRasmus Villemoes 
199*7c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^4 */
200*7c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
201*7c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
202*7c43d9a3SRasmus Villemoes 	buf += 2;
203*7c43d9a3SRasmus Villemoes out_q:
204*7c43d9a3SRasmus Villemoes 	/* 1 <= q < 100 */
205*7c43d9a3SRasmus Villemoes 	r = q;
206*7c43d9a3SRasmus Villemoes out_r:
207*7c43d9a3SRasmus Villemoes 	/* 1 <= r < 100 */
208*7c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r];
209*7c43d9a3SRasmus Villemoes 	buf += 2;
210*7c43d9a3SRasmus Villemoes 	if (buf[-1] == '0')
211*7c43d9a3SRasmus Villemoes 		buf--;
212133fd9f5SDenys Vlasenko 	return buf;
213133fd9f5SDenys Vlasenko }
214133fd9f5SDenys Vlasenko 
215*7c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
216*7c43d9a3SRasmus Villemoes static noinline_for_stack
217*7c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r)
218*7c43d9a3SRasmus Villemoes {
219*7c43d9a3SRasmus Villemoes 	unsigned q;
220133fd9f5SDenys Vlasenko 
221*7c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^8 */
222*7c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
223*7c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
224*7c43d9a3SRasmus Villemoes 	buf += 2;
225133fd9f5SDenys Vlasenko 
226*7c43d9a3SRasmus Villemoes 	/* 0 <= q < 10^6 */
227*7c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
228*7c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
229*7c43d9a3SRasmus Villemoes 	buf += 2;
230133fd9f5SDenys Vlasenko 
231*7c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
232*7c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
233*7c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
234*7c43d9a3SRasmus Villemoes 	buf += 2;
235*7c43d9a3SRasmus Villemoes 
236*7c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
237*7c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
238*7c43d9a3SRasmus Villemoes 	buf += 2;
239*7c43d9a3SRasmus Villemoes 	return buf;
240*7c43d9a3SRasmus Villemoes }
241*7c43d9a3SRasmus Villemoes 
242*7c43d9a3SRasmus Villemoes static noinline_for_stack
243133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
244133fd9f5SDenys Vlasenko {
245133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000)
246*7c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
247*7c43d9a3SRasmus Villemoes 	/* 1 <= n <= 1.6e11 */
248*7c43d9a3SRasmus Villemoes 	if (n >= 100*1000*1000)
249*7c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
250*7c43d9a3SRasmus Villemoes 	/* 1 <= n < 1e8 */
251133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
252133fd9f5SDenys Vlasenko }
253133fd9f5SDenys Vlasenko 
254*7c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
255133fd9f5SDenys Vlasenko 
256*7c43d9a3SRasmus Villemoes static void
257*7c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r)
258133fd9f5SDenys Vlasenko {
259*7c43d9a3SRasmus Villemoes 	unsigned q;
260*7c43d9a3SRasmus Villemoes 
261*7c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
262*7c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
263*7c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
264*7c43d9a3SRasmus Villemoes 	buf += 2;
265*7c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
266*7c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2672359172aSGeorge Spelvin }
2682359172aSGeorge Spelvin 
2692359172aSGeorge Spelvin /*
2702359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2712359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2722359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2732359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
274*7c43d9a3SRasmus Villemoes  * (second call in the put_dec code, assuming n is all-ones).
2752359172aSGeorge Spelvin  */
276*7c43d9a3SRasmus Villemoes static noinline_for_stack
2772359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2782359172aSGeorge Spelvin {
2792359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2802359172aSGeorge Spelvin 
2812359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2822359172aSGeorge Spelvin         return q;
283133fd9f5SDenys Vlasenko }
284133fd9f5SDenys Vlasenko 
285133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
286133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
287133fd9f5SDenys Vlasenko  * (with permission from the author).
288133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
289133fd9f5SDenys Vlasenko  */
290133fd9f5SDenys Vlasenko static
291133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
292133fd9f5SDenys Vlasenko {
293133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
294133fd9f5SDenys Vlasenko 
295133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
296133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
297133fd9f5SDenys Vlasenko 
298133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
299133fd9f5SDenys Vlasenko 	h   = (n >> 32);
300133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
301133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
302133fd9f5SDenys Vlasenko 
303*7c43d9a3SRasmus Villemoes 	/* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
304*7c43d9a3SRasmus Villemoes 	     = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
305133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
3062359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
307133fd9f5SDenys Vlasenko 
3082359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
3092359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
310133fd9f5SDenys Vlasenko 
3112359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
3122359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
313133fd9f5SDenys Vlasenko 
3142359172aSGeorge Spelvin 	q += 281 * d3;
3152359172aSGeorge Spelvin 	buf += 12;
3162359172aSGeorge Spelvin 	if (q)
3172359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3182359172aSGeorge Spelvin 	else while (buf[-1] == '0')
319133fd9f5SDenys Vlasenko 		--buf;
3207b9186f5SAndré Goddard Rosa 
3214277eeddSDenis Vlasenko 	return buf;
3224277eeddSDenis Vlasenko }
323133fd9f5SDenys Vlasenko 
324133fd9f5SDenys Vlasenko #endif
3254277eeddSDenis Vlasenko 
3261ac101a5SKAMEZAWA Hiroyuki /*
3271ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3281ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3291ac101a5SKAMEZAWA Hiroyuki  *
3301ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3311ac101a5SKAMEZAWA Hiroyuki  */
3321ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num)
3331ac101a5SKAMEZAWA Hiroyuki {
334*7c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
335*7c43d9a3SRasmus Villemoes 	char tmp[sizeof(num) * 3] __aligned(2);
3361ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3371ac101a5SKAMEZAWA Hiroyuki 
338133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
339133fd9f5SDenys Vlasenko 	if (num <= 9) {
340133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
341133fd9f5SDenys Vlasenko 		len = 1;
342133fd9f5SDenys Vlasenko 	} else {
3431ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
344133fd9f5SDenys Vlasenko 	}
3451ac101a5SKAMEZAWA Hiroyuki 
3461ac101a5SKAMEZAWA Hiroyuki 	if (len > size)
3471ac101a5SKAMEZAWA Hiroyuki 		return 0;
3481ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
3491ac101a5SKAMEZAWA Hiroyuki 		buf[idx] = tmp[len - idx - 1];
3501ac101a5SKAMEZAWA Hiroyuki 	return len;
3511ac101a5SKAMEZAWA Hiroyuki }
3521ac101a5SKAMEZAWA Hiroyuki 
35351be17dfSRasmus Villemoes #define SIGN	1		/* unsigned/signed, must be 1 */
354d1c1b121SRasmus Villemoes #define LEFT	2		/* left justified */
3551da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3561da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
357d1c1b121SRasmus Villemoes #define ZEROPAD	16		/* pad with zero, must be 16 == '0' - ' ' */
358b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
359b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3601da177e4SLinus Torvalds 
361fef20d9cSFrederic Weisbecker enum format_type {
362fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
363ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
364fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
365fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
366fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
367fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
368fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
369fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
370fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
371fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
372fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
373a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
374a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
375fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
376fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
377fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
378fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
379fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
380fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
381fef20d9cSFrederic Weisbecker };
382fef20d9cSFrederic Weisbecker 
383fef20d9cSFrederic Weisbecker struct printf_spec {
3844e310fdaSJoe Perches 	u8	type;		/* format_type enum */
385ef0658f3SJoe Perches 	u8	flags;		/* flags to number() */
3864e310fdaSJoe Perches 	u8	base;		/* number base, 8, 10 or 16 only */
3874e310fdaSJoe Perches 	u8	qualifier;	/* number qualifier, one of 'hHlLtzZ' */
3884e310fdaSJoe Perches 	s16	field_width;	/* width of output field */
3894e310fdaSJoe Perches 	s16	precision;	/* # of digits/chars */
390fef20d9cSFrederic Weisbecker };
391fef20d9cSFrederic Weisbecker 
392cf3b429bSJoe Perches static noinline_for_stack
393cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
394fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
3951da177e4SLinus Torvalds {
396*7c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
397*7c43d9a3SRasmus Villemoes 	char tmp[3 * sizeof(num)] __aligned(2);
3989b706aeeSDenys Vlasenko 	char sign;
3999b706aeeSDenys Vlasenko 	char locase;
400fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4011da177e4SLinus Torvalds 	int i;
4027c203422SPierre Carrier 	bool is_zero = num == 0LL;
4031da177e4SLinus Torvalds 
4049b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4059b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
406fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
407fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
408fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4091da177e4SLinus Torvalds 	sign = 0;
410fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4111da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4121da177e4SLinus Torvalds 			sign = '-';
4131da177e4SLinus Torvalds 			num = -(signed long long)num;
414fef20d9cSFrederic Weisbecker 			spec.field_width--;
415fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4161da177e4SLinus Torvalds 			sign = '+';
417fef20d9cSFrederic Weisbecker 			spec.field_width--;
418fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4191da177e4SLinus Torvalds 			sign = ' ';
420fef20d9cSFrederic Weisbecker 			spec.field_width--;
4211da177e4SLinus Torvalds 		}
4221da177e4SLinus Torvalds 	}
423b39a7340SDenis Vlasenko 	if (need_pfx) {
424fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4257c203422SPierre Carrier 			spec.field_width -= 2;
4267c203422SPierre Carrier 		else if (!is_zero)
427fef20d9cSFrederic Weisbecker 			spec.field_width--;
4281da177e4SLinus Torvalds 	}
429b39a7340SDenis Vlasenko 
430b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4311da177e4SLinus Torvalds 	i = 0;
432133fd9f5SDenys Vlasenko 	if (num < spec.base)
4333ea8d440SRasmus Villemoes 		tmp[i++] = hex_asc_upper[num] | locase;
434fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
435fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
436b39a7340SDenis Vlasenko 		int shift = 3;
4377b9186f5SAndré Goddard Rosa 
4387b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4397b9186f5SAndré Goddard Rosa 			shift = 4;
440b39a7340SDenis Vlasenko 		do {
4413ea8d440SRasmus Villemoes 			tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
442b39a7340SDenis Vlasenko 			num >>= shift;
443b39a7340SDenis Vlasenko 		} while (num);
4444277eeddSDenis Vlasenko 	} else { /* base 10 */
4454277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4464277eeddSDenis Vlasenko 	}
447b39a7340SDenis Vlasenko 
448b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
449fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
450fef20d9cSFrederic Weisbecker 		spec.precision = i;
451b39a7340SDenis Vlasenko 	/* leading space padding */
452fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
45351be17dfSRasmus Villemoes 	if (!(spec.flags & (ZEROPAD | LEFT))) {
454fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
455f796937aSJeremy Fitzhardinge 			if (buf < end)
4561da177e4SLinus Torvalds 				*buf = ' ';
4571da177e4SLinus Torvalds 			++buf;
4581da177e4SLinus Torvalds 		}
4591da177e4SLinus Torvalds 	}
460b39a7340SDenis Vlasenko 	/* sign */
4611da177e4SLinus Torvalds 	if (sign) {
462f796937aSJeremy Fitzhardinge 		if (buf < end)
4631da177e4SLinus Torvalds 			*buf = sign;
4641da177e4SLinus Torvalds 		++buf;
4651da177e4SLinus Torvalds 	}
466b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
467b39a7340SDenis Vlasenko 	if (need_pfx) {
4687c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
469f796937aSJeremy Fitzhardinge 			if (buf < end)
4701da177e4SLinus Torvalds 				*buf = '0';
4711da177e4SLinus Torvalds 			++buf;
4727c203422SPierre Carrier 		}
473fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
474f796937aSJeremy Fitzhardinge 			if (buf < end)
4759b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
4761da177e4SLinus Torvalds 			++buf;
4771da177e4SLinus Torvalds 		}
4781da177e4SLinus Torvalds 	}
479b39a7340SDenis Vlasenko 	/* zero or space padding */
480fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
481d1c1b121SRasmus Villemoes 		char c = ' ' + (spec.flags & ZEROPAD);
482d1c1b121SRasmus Villemoes 		BUILD_BUG_ON(' ' + ZEROPAD != '0');
483fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
484f796937aSJeremy Fitzhardinge 			if (buf < end)
4851da177e4SLinus Torvalds 				*buf = c;
4861da177e4SLinus Torvalds 			++buf;
4871da177e4SLinus Torvalds 		}
4881da177e4SLinus Torvalds 	}
489b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
490fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
491f796937aSJeremy Fitzhardinge 		if (buf < end)
4921da177e4SLinus Torvalds 			*buf = '0';
4931da177e4SLinus Torvalds 		++buf;
4941da177e4SLinus Torvalds 	}
495b39a7340SDenis Vlasenko 	/* actual digits of result */
496b39a7340SDenis Vlasenko 	while (--i >= 0) {
497f796937aSJeremy Fitzhardinge 		if (buf < end)
4981da177e4SLinus Torvalds 			*buf = tmp[i];
4991da177e4SLinus Torvalds 		++buf;
5001da177e4SLinus Torvalds 	}
501b39a7340SDenis Vlasenko 	/* trailing space padding */
502fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
503f796937aSJeremy Fitzhardinge 		if (buf < end)
5041da177e4SLinus Torvalds 			*buf = ' ';
5051da177e4SLinus Torvalds 		++buf;
5061da177e4SLinus Torvalds 	}
5077b9186f5SAndré Goddard Rosa 
5081da177e4SLinus Torvalds 	return buf;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
511cf3b429bSJoe Perches static noinline_for_stack
512cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec)
5130f9bfa56SLinus Torvalds {
5140f9bfa56SLinus Torvalds 	int len, i;
5150f9bfa56SLinus Torvalds 
5160f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5170f4f81dcSAndré Goddard Rosa 		s = "(null)";
5180f9bfa56SLinus Torvalds 
519fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5200f9bfa56SLinus Torvalds 
521fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
522fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5230f9bfa56SLinus Torvalds 			if (buf < end)
5240f9bfa56SLinus Torvalds 				*buf = ' ';
5250f9bfa56SLinus Torvalds 			++buf;
5260f9bfa56SLinus Torvalds 		}
5270f9bfa56SLinus Torvalds 	}
5280f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5290f9bfa56SLinus Torvalds 		if (buf < end)
5300f9bfa56SLinus Torvalds 			*buf = *s;
5310f9bfa56SLinus Torvalds 		++buf; ++s;
5320f9bfa56SLinus Torvalds 	}
533fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5340f9bfa56SLinus Torvalds 		if (buf < end)
5350f9bfa56SLinus Torvalds 			*buf = ' ';
5360f9bfa56SLinus Torvalds 		++buf;
5370f9bfa56SLinus Torvalds 	}
5387b9186f5SAndré Goddard Rosa 
5390f9bfa56SLinus Torvalds 	return buf;
5400f9bfa56SLinus Torvalds }
5410f9bfa56SLinus Torvalds 
5424b6ccca7SAl Viro static void widen(char *buf, char *end, unsigned len, unsigned spaces)
5434b6ccca7SAl Viro {
5444b6ccca7SAl Viro 	size_t size;
5454b6ccca7SAl Viro 	if (buf >= end)	/* nowhere to put anything */
5464b6ccca7SAl Viro 		return;
5474b6ccca7SAl Viro 	size = end - buf;
5484b6ccca7SAl Viro 	if (size <= spaces) {
5494b6ccca7SAl Viro 		memset(buf, ' ', size);
5504b6ccca7SAl Viro 		return;
5514b6ccca7SAl Viro 	}
5524b6ccca7SAl Viro 	if (len) {
5534b6ccca7SAl Viro 		if (len > size - spaces)
5544b6ccca7SAl Viro 			len = size - spaces;
5554b6ccca7SAl Viro 		memmove(buf + spaces, buf, len);
5564b6ccca7SAl Viro 	}
5574b6ccca7SAl Viro 	memset(buf, ' ', spaces);
5584b6ccca7SAl Viro }
5594b6ccca7SAl Viro 
5604b6ccca7SAl Viro static noinline_for_stack
5614b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
5624b6ccca7SAl Viro 		  const char *fmt)
5634b6ccca7SAl Viro {
5644b6ccca7SAl Viro 	const char *array[4], *s;
5654b6ccca7SAl Viro 	const struct dentry *p;
5664b6ccca7SAl Viro 	int depth;
5674b6ccca7SAl Viro 	int i, n;
5684b6ccca7SAl Viro 
5694b6ccca7SAl Viro 	switch (fmt[1]) {
5704b6ccca7SAl Viro 		case '2': case '3': case '4':
5714b6ccca7SAl Viro 			depth = fmt[1] - '0';
5724b6ccca7SAl Viro 			break;
5734b6ccca7SAl Viro 		default:
5744b6ccca7SAl Viro 			depth = 1;
5754b6ccca7SAl Viro 	}
5764b6ccca7SAl Viro 
5774b6ccca7SAl Viro 	rcu_read_lock();
5784b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
5794b6ccca7SAl Viro 		p = ACCESS_ONCE(d->d_parent);
5804b6ccca7SAl Viro 		array[i] = ACCESS_ONCE(d->d_name.name);
5814b6ccca7SAl Viro 		if (p == d) {
5824b6ccca7SAl Viro 			if (i)
5834b6ccca7SAl Viro 				array[i] = "";
5844b6ccca7SAl Viro 			i++;
5854b6ccca7SAl Viro 			break;
5864b6ccca7SAl Viro 		}
5874b6ccca7SAl Viro 	}
5884b6ccca7SAl Viro 	s = array[--i];
5894b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
5904b6ccca7SAl Viro 		char c = *s++;
5914b6ccca7SAl Viro 		if (!c) {
5924b6ccca7SAl Viro 			if (!i)
5934b6ccca7SAl Viro 				break;
5944b6ccca7SAl Viro 			c = '/';
5954b6ccca7SAl Viro 			s = array[--i];
5964b6ccca7SAl Viro 		}
5974b6ccca7SAl Viro 		if (buf < end)
5984b6ccca7SAl Viro 			*buf = c;
5994b6ccca7SAl Viro 	}
6004b6ccca7SAl Viro 	rcu_read_unlock();
6014b6ccca7SAl Viro 	if (n < spec.field_width) {
6024b6ccca7SAl Viro 		/* we want to pad the sucker */
6034b6ccca7SAl Viro 		unsigned spaces = spec.field_width - n;
6044b6ccca7SAl Viro 		if (!(spec.flags & LEFT)) {
6054b6ccca7SAl Viro 			widen(buf - n, end, n, spaces);
6064b6ccca7SAl Viro 			return buf + spaces;
6074b6ccca7SAl Viro 		}
6084b6ccca7SAl Viro 		while (spaces--) {
6094b6ccca7SAl Viro 			if (buf < end)
6104b6ccca7SAl Viro 				*buf = ' ';
6114b6ccca7SAl Viro 			++buf;
6124b6ccca7SAl Viro 		}
6134b6ccca7SAl Viro 	}
6144b6ccca7SAl Viro 	return buf;
6154b6ccca7SAl Viro }
6164b6ccca7SAl Viro 
617cf3b429bSJoe Perches static noinline_for_stack
618cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
619b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
6200fe1ef24SLinus Torvalds {
621b0d33c2bSJoe Perches 	unsigned long value;
6220fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
6230fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
624b0d33c2bSJoe Perches #endif
625b0d33c2bSJoe Perches 
626b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
627b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
628b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
629b0d33c2bSJoe Perches 
630b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
631b0d33c2bSJoe Perches 	if (*fmt == 'B')
6320f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
633b0d33c2bSJoe Perches 	else if (*fmt != 'f' && *fmt != 's')
6340fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
6350c8b946eSFrederic Weisbecker 	else
6364796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
6377b9186f5SAndré Goddard Rosa 
638fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
6390fe1ef24SLinus Torvalds #else
640fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
641fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
642fef20d9cSFrederic Weisbecker 	spec.base = 16;
6437b9186f5SAndré Goddard Rosa 
644fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
6450fe1ef24SLinus Torvalds #endif
6460fe1ef24SLinus Torvalds }
6470fe1ef24SLinus Torvalds 
648cf3b429bSJoe Perches static noinline_for_stack
649cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
650fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
651332d2e78SLinus Torvalds {
652332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
65328405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
654332d2e78SLinus Torvalds #endif
655332d2e78SLinus Torvalds 
656332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
65728405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
658332d2e78SLinus Torvalds #endif
6594da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
660fef20d9cSFrederic Weisbecker 		.base = 16,
6614da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
662fef20d9cSFrederic Weisbecker 		.precision = -1,
663fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
664fef20d9cSFrederic Weisbecker 	};
6654da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
6664da0b66cSBjorn Helgaas 		.base = 16,
6674da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
6684da0b66cSBjorn Helgaas 		.precision = -1,
6694da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
6704da0b66cSBjorn Helgaas 	};
6710f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
6720f4050c7SBjorn Helgaas 		.base = 16,
6730f4050c7SBjorn Helgaas 		.field_width = 2,
6740f4050c7SBjorn Helgaas 		.precision = -1,
6750f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
6760f4050c7SBjorn Helgaas 	};
6774da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
678c91d3376SBjorn Helgaas 		.base = 10,
679c91d3376SBjorn Helgaas 		.precision = -1,
680c91d3376SBjorn Helgaas 		.flags = 0,
681c91d3376SBjorn Helgaas 	};
6824da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
683fd95541eSBjorn Helgaas 		.field_width = -1,
684fd95541eSBjorn Helgaas 		.precision = 10,
685fd95541eSBjorn Helgaas 		.flags = LEFT,
686fd95541eSBjorn Helgaas 	};
6874da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
688fd95541eSBjorn Helgaas 		.base = 16,
689fd95541eSBjorn Helgaas 		.precision = -1,
690fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
691fd95541eSBjorn Helgaas 	};
692c7dabef8SBjorn Helgaas 
693c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
694c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
695c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
696c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
6979d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
698c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
699c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
700c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
701c7dabef8SBjorn Helgaas 
702332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
703c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
7044da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
705332d2e78SLinus Torvalds 
706332d2e78SLinus Torvalds 	*p++ = '[';
7074da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
708fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
7094da0b66cSBjorn Helgaas 		specp = &io_spec;
7104da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
711fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
7124da0b66cSBjorn Helgaas 		specp = &mem_spec;
7134da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
714fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
7154da0b66cSBjorn Helgaas 		specp = &dec_spec;
7164da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
717fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
7184da0b66cSBjorn Helgaas 		specp = &dec_spec;
7190f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
7200f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
7210f4050c7SBjorn Helgaas 		specp = &bus_spec;
7224da0b66cSBjorn Helgaas 	} else {
723c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
7244da0b66cSBjorn Helgaas 		specp = &mem_spec;
725c7dabef8SBjorn Helgaas 		decode = 0;
726fd95541eSBjorn Helgaas 	}
727d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
728d19cb803SBjorn Helgaas 		p = string(p, pend, "size ", str_spec);
729d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
730d19cb803SBjorn Helgaas 	} else {
7314da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
732c91d3376SBjorn Helgaas 		if (res->start != res->end) {
733332d2e78SLinus Torvalds 			*p++ = '-';
7344da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
735c91d3376SBjorn Helgaas 		}
736d19cb803SBjorn Helgaas 	}
737c7dabef8SBjorn Helgaas 	if (decode) {
738fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
739fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
740fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
741fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
7429d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
7439d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
744fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
745fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
746c7dabef8SBjorn Helgaas 	} else {
747fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
748c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
749fd95541eSBjorn Helgaas 	}
750332d2e78SLinus Torvalds 	*p++ = ']';
751c7dabef8SBjorn Helgaas 	*p = '\0';
752332d2e78SLinus Torvalds 
753fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
754332d2e78SLinus Torvalds }
755332d2e78SLinus Torvalds 
756cf3b429bSJoe Perches static noinline_for_stack
75731550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
75831550a16SAndy Shevchenko 		 const char *fmt)
75931550a16SAndy Shevchenko {
760360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
76131550a16SAndy Shevchenko 				   negative value, fallback to the default */
76231550a16SAndy Shevchenko 	char separator;
76331550a16SAndy Shevchenko 
76431550a16SAndy Shevchenko 	if (spec.field_width == 0)
76531550a16SAndy Shevchenko 		/* nothing to print */
76631550a16SAndy Shevchenko 		return buf;
76731550a16SAndy Shevchenko 
76831550a16SAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
76931550a16SAndy Shevchenko 		/* NULL pointer */
77031550a16SAndy Shevchenko 		return string(buf, end, NULL, spec);
77131550a16SAndy Shevchenko 
77231550a16SAndy Shevchenko 	switch (fmt[1]) {
77331550a16SAndy Shevchenko 	case 'C':
77431550a16SAndy Shevchenko 		separator = ':';
77531550a16SAndy Shevchenko 		break;
77631550a16SAndy Shevchenko 	case 'D':
77731550a16SAndy Shevchenko 		separator = '-';
77831550a16SAndy Shevchenko 		break;
77931550a16SAndy Shevchenko 	case 'N':
78031550a16SAndy Shevchenko 		separator = 0;
78131550a16SAndy Shevchenko 		break;
78231550a16SAndy Shevchenko 	default:
78331550a16SAndy Shevchenko 		separator = ' ';
78431550a16SAndy Shevchenko 		break;
78531550a16SAndy Shevchenko 	}
78631550a16SAndy Shevchenko 
78731550a16SAndy Shevchenko 	if (spec.field_width > 0)
78831550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
78931550a16SAndy Shevchenko 
7909c98f235SRasmus Villemoes 	for (i = 0; i < len; ++i) {
7919c98f235SRasmus Villemoes 		if (buf < end)
7929c98f235SRasmus Villemoes 			*buf = hex_asc_hi(addr[i]);
7939c98f235SRasmus Villemoes 		++buf;
7949c98f235SRasmus Villemoes 		if (buf < end)
7959c98f235SRasmus Villemoes 			*buf = hex_asc_lo(addr[i]);
7969c98f235SRasmus Villemoes 		++buf;
79731550a16SAndy Shevchenko 
7989c98f235SRasmus Villemoes 		if (separator && i != len - 1) {
7999c98f235SRasmus Villemoes 			if (buf < end)
8009c98f235SRasmus Villemoes 				*buf = separator;
8019c98f235SRasmus Villemoes 			++buf;
8029c98f235SRasmus Villemoes 		}
80331550a16SAndy Shevchenko 	}
80431550a16SAndy Shevchenko 
80531550a16SAndy Shevchenko 	return buf;
80631550a16SAndy Shevchenko }
80731550a16SAndy Shevchenko 
80831550a16SAndy Shevchenko static noinline_for_stack
809dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
810dbc760bcSTejun Heo 		    struct printf_spec spec, const char *fmt)
811dbc760bcSTejun Heo {
812dbc760bcSTejun Heo 	const int CHUNKSZ = 32;
813dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
814dbc760bcSTejun Heo 	int i, chunksz;
815dbc760bcSTejun Heo 	bool first = true;
816dbc760bcSTejun Heo 
817dbc760bcSTejun Heo 	/* reused to print numbers */
818dbc760bcSTejun Heo 	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
819dbc760bcSTejun Heo 
820dbc760bcSTejun Heo 	chunksz = nr_bits & (CHUNKSZ - 1);
821dbc760bcSTejun Heo 	if (chunksz == 0)
822dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
823dbc760bcSTejun Heo 
824dbc760bcSTejun Heo 	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
825dbc760bcSTejun Heo 	for (; i >= 0; i -= CHUNKSZ) {
826dbc760bcSTejun Heo 		u32 chunkmask, val;
827dbc760bcSTejun Heo 		int word, bit;
828dbc760bcSTejun Heo 
829dbc760bcSTejun Heo 		chunkmask = ((1ULL << chunksz) - 1);
830dbc760bcSTejun Heo 		word = i / BITS_PER_LONG;
831dbc760bcSTejun Heo 		bit = i % BITS_PER_LONG;
832dbc760bcSTejun Heo 		val = (bitmap[word] >> bit) & chunkmask;
833dbc760bcSTejun Heo 
834dbc760bcSTejun Heo 		if (!first) {
835dbc760bcSTejun Heo 			if (buf < end)
836dbc760bcSTejun Heo 				*buf = ',';
837dbc760bcSTejun Heo 			buf++;
838dbc760bcSTejun Heo 		}
839dbc760bcSTejun Heo 		first = false;
840dbc760bcSTejun Heo 
841dbc760bcSTejun Heo 		spec.field_width = DIV_ROUND_UP(chunksz, 4);
842dbc760bcSTejun Heo 		buf = number(buf, end, val, spec);
843dbc760bcSTejun Heo 
844dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
845dbc760bcSTejun Heo 	}
846dbc760bcSTejun Heo 	return buf;
847dbc760bcSTejun Heo }
848dbc760bcSTejun Heo 
849dbc760bcSTejun Heo static noinline_for_stack
850dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
851dbc760bcSTejun Heo 			 struct printf_spec spec, const char *fmt)
852dbc760bcSTejun Heo {
853dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
854dbc760bcSTejun Heo 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
855dbc760bcSTejun Heo 	int cur, rbot, rtop;
856dbc760bcSTejun Heo 	bool first = true;
857dbc760bcSTejun Heo 
858dbc760bcSTejun Heo 	/* reused to print numbers */
859dbc760bcSTejun Heo 	spec = (struct printf_spec){ .base = 10 };
860dbc760bcSTejun Heo 
861dbc760bcSTejun Heo 	rbot = cur = find_first_bit(bitmap, nr_bits);
862dbc760bcSTejun Heo 	while (cur < nr_bits) {
863dbc760bcSTejun Heo 		rtop = cur;
864dbc760bcSTejun Heo 		cur = find_next_bit(bitmap, nr_bits, cur + 1);
865dbc760bcSTejun Heo 		if (cur < nr_bits && cur <= rtop + 1)
866dbc760bcSTejun Heo 			continue;
867dbc760bcSTejun Heo 
868dbc760bcSTejun Heo 		if (!first) {
869dbc760bcSTejun Heo 			if (buf < end)
870dbc760bcSTejun Heo 				*buf = ',';
871dbc760bcSTejun Heo 			buf++;
872dbc760bcSTejun Heo 		}
873dbc760bcSTejun Heo 		first = false;
874dbc760bcSTejun Heo 
875dbc760bcSTejun Heo 		buf = number(buf, end, rbot, spec);
876dbc760bcSTejun Heo 		if (rbot < rtop) {
877dbc760bcSTejun Heo 			if (buf < end)
878dbc760bcSTejun Heo 				*buf = '-';
879dbc760bcSTejun Heo 			buf++;
880dbc760bcSTejun Heo 
881dbc760bcSTejun Heo 			buf = number(buf, end, rtop, spec);
882dbc760bcSTejun Heo 		}
883dbc760bcSTejun Heo 
884dbc760bcSTejun Heo 		rbot = cur;
885dbc760bcSTejun Heo 	}
886dbc760bcSTejun Heo 	return buf;
887dbc760bcSTejun Heo }
888dbc760bcSTejun Heo 
889dbc760bcSTejun Heo static noinline_for_stack
890cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
8918a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
892dd45c9cfSHarvey Harrison {
8938a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
894dd45c9cfSHarvey Harrison 	char *p = mac_addr;
895dd45c9cfSHarvey Harrison 	int i;
896bc7259a2SJoe Perches 	char separator;
89776597ff9SAndrei Emeltchenko 	bool reversed = false;
898bc7259a2SJoe Perches 
89976597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
90076597ff9SAndrei Emeltchenko 	case 'F':
901bc7259a2SJoe Perches 		separator = '-';
90276597ff9SAndrei Emeltchenko 		break;
90376597ff9SAndrei Emeltchenko 
90476597ff9SAndrei Emeltchenko 	case 'R':
90576597ff9SAndrei Emeltchenko 		reversed = true;
90676597ff9SAndrei Emeltchenko 		/* fall through */
90776597ff9SAndrei Emeltchenko 
90876597ff9SAndrei Emeltchenko 	default:
909bc7259a2SJoe Perches 		separator = ':';
91076597ff9SAndrei Emeltchenko 		break;
911bc7259a2SJoe Perches 	}
912dd45c9cfSHarvey Harrison 
913dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
91476597ff9SAndrei Emeltchenko 		if (reversed)
91576597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
91676597ff9SAndrei Emeltchenko 		else
91755036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
91876597ff9SAndrei Emeltchenko 
9198a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
920bc7259a2SJoe Perches 			*p++ = separator;
921dd45c9cfSHarvey Harrison 	}
922dd45c9cfSHarvey Harrison 	*p = '\0';
923dd45c9cfSHarvey Harrison 
924fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
925dd45c9cfSHarvey Harrison }
926dd45c9cfSHarvey Harrison 
927cf3b429bSJoe Perches static noinline_for_stack
928cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
929689afa7dSHarvey Harrison {
930689afa7dSHarvey Harrison 	int i;
9310159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
9320159f24eSJoe Perches 	int index;
9330159f24eSJoe Perches 	int step;
934689afa7dSHarvey Harrison 
9350159f24eSJoe Perches 	switch (fmt[2]) {
9360159f24eSJoe Perches 	case 'h':
9370159f24eSJoe Perches #ifdef __BIG_ENDIAN
9380159f24eSJoe Perches 		index = 0;
9390159f24eSJoe Perches 		step = 1;
9400159f24eSJoe Perches #else
9410159f24eSJoe Perches 		index = 3;
9420159f24eSJoe Perches 		step = -1;
9430159f24eSJoe Perches #endif
9440159f24eSJoe Perches 		break;
9450159f24eSJoe Perches 	case 'l':
9460159f24eSJoe Perches 		index = 3;
9470159f24eSJoe Perches 		step = -1;
9480159f24eSJoe Perches 		break;
9490159f24eSJoe Perches 	case 'n':
9500159f24eSJoe Perches 	case 'b':
9510159f24eSJoe Perches 	default:
9520159f24eSJoe Perches 		index = 0;
9530159f24eSJoe Perches 		step = 1;
9540159f24eSJoe Perches 		break;
9550159f24eSJoe Perches 	}
9568a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
957*7c43d9a3SRasmus Villemoes 		char temp[4] __aligned(2);	/* hold each IP quad in reverse order */
958133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
9598a27f7c9SJoe Perches 		if (leading_zeros) {
9608a27f7c9SJoe Perches 			if (digits < 3)
9618a27f7c9SJoe Perches 				*p++ = '0';
9628a27f7c9SJoe Perches 			if (digits < 2)
9638a27f7c9SJoe Perches 				*p++ = '0';
9648a27f7c9SJoe Perches 		}
9658a27f7c9SJoe Perches 		/* reverse the digits in the quad */
9668a27f7c9SJoe Perches 		while (digits--)
9678a27f7c9SJoe Perches 			*p++ = temp[digits];
9688a27f7c9SJoe Perches 		if (i < 3)
9698a27f7c9SJoe Perches 			*p++ = '.';
9700159f24eSJoe Perches 		index += step;
9718a27f7c9SJoe Perches 	}
9728a27f7c9SJoe Perches 	*p = '\0';
9737b9186f5SAndré Goddard Rosa 
9748a27f7c9SJoe Perches 	return p;
9758a27f7c9SJoe Perches }
9768a27f7c9SJoe Perches 
977cf3b429bSJoe Perches static noinline_for_stack
978cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
9798a27f7c9SJoe Perches {
9807b9186f5SAndré Goddard Rosa 	int i, j, range;
9818a27f7c9SJoe Perches 	unsigned char zerolength[8];
9828a27f7c9SJoe Perches 	int longest = 1;
9838a27f7c9SJoe Perches 	int colonpos = -1;
9848a27f7c9SJoe Perches 	u16 word;
9857b9186f5SAndré Goddard Rosa 	u8 hi, lo;
9868a27f7c9SJoe Perches 	bool needcolon = false;
987eb78cd26SJoe Perches 	bool useIPv4;
988eb78cd26SJoe Perches 	struct in6_addr in6;
989eb78cd26SJoe Perches 
990eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
991eb78cd26SJoe Perches 
992eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
9938a27f7c9SJoe Perches 
9948a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
9958a27f7c9SJoe Perches 
9968a27f7c9SJoe Perches 	if (useIPv4)
9978a27f7c9SJoe Perches 		range = 6;
9988a27f7c9SJoe Perches 	else
9998a27f7c9SJoe Perches 		range = 8;
10008a27f7c9SJoe Perches 
10018a27f7c9SJoe Perches 	/* find position of longest 0 run */
10028a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10038a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
1004eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
10058a27f7c9SJoe Perches 				break;
10068a27f7c9SJoe Perches 			zerolength[i]++;
10078a27f7c9SJoe Perches 		}
10088a27f7c9SJoe Perches 	}
10098a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10108a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
10118a27f7c9SJoe Perches 			longest = zerolength[i];
10128a27f7c9SJoe Perches 			colonpos = i;
10138a27f7c9SJoe Perches 		}
10148a27f7c9SJoe Perches 	}
101529cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
101629cf519eSJoe Perches 		colonpos = -1;
10178a27f7c9SJoe Perches 
10188a27f7c9SJoe Perches 	/* emit address */
10198a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10208a27f7c9SJoe Perches 		if (i == colonpos) {
10218a27f7c9SJoe Perches 			if (needcolon || i == 0)
10228a27f7c9SJoe Perches 				*p++ = ':';
10238a27f7c9SJoe Perches 			*p++ = ':';
10248a27f7c9SJoe Perches 			needcolon = false;
10258a27f7c9SJoe Perches 			i += longest - 1;
10268a27f7c9SJoe Perches 			continue;
10278a27f7c9SJoe Perches 		}
10288a27f7c9SJoe Perches 		if (needcolon) {
10298a27f7c9SJoe Perches 			*p++ = ':';
10308a27f7c9SJoe Perches 			needcolon = false;
10318a27f7c9SJoe Perches 		}
10328a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
1033eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
10348a27f7c9SJoe Perches 		hi = word >> 8;
10358a27f7c9SJoe Perches 		lo = word & 0xff;
10368a27f7c9SJoe Perches 		if (hi) {
10378a27f7c9SJoe Perches 			if (hi > 0x0f)
103855036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
10398a27f7c9SJoe Perches 			else
10408a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
104155036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10428a27f7c9SJoe Perches 		}
1043b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
104455036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10458a27f7c9SJoe Perches 		else
10468a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
10478a27f7c9SJoe Perches 		needcolon = true;
10488a27f7c9SJoe Perches 	}
10498a27f7c9SJoe Perches 
10508a27f7c9SJoe Perches 	if (useIPv4) {
10518a27f7c9SJoe Perches 		if (needcolon)
10528a27f7c9SJoe Perches 			*p++ = ':';
10530159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
10548a27f7c9SJoe Perches 	}
10558a27f7c9SJoe Perches 	*p = '\0';
10567b9186f5SAndré Goddard Rosa 
10578a27f7c9SJoe Perches 	return p;
10588a27f7c9SJoe Perches }
10598a27f7c9SJoe Perches 
1060cf3b429bSJoe Perches static noinline_for_stack
1061cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
10628a27f7c9SJoe Perches {
10638a27f7c9SJoe Perches 	int i;
10647b9186f5SAndré Goddard Rosa 
1065689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
106655036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
106755036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
10688a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
1069689afa7dSHarvey Harrison 			*p++ = ':';
1070689afa7dSHarvey Harrison 	}
1071689afa7dSHarvey Harrison 	*p = '\0';
10727b9186f5SAndré Goddard Rosa 
10738a27f7c9SJoe Perches 	return p;
10748a27f7c9SJoe Perches }
10758a27f7c9SJoe Perches 
1076cf3b429bSJoe Perches static noinline_for_stack
1077cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
10788a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
10798a27f7c9SJoe Perches {
10808a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
10818a27f7c9SJoe Perches 
10828a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
1083eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
10848a27f7c9SJoe Perches 	else
1085eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
1086689afa7dSHarvey Harrison 
1087fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
1088689afa7dSHarvey Harrison }
1089689afa7dSHarvey Harrison 
1090cf3b429bSJoe Perches static noinline_for_stack
1091cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
10928a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
10934aa99606SHarvey Harrison {
10948a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
10954aa99606SHarvey Harrison 
10960159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
10974aa99606SHarvey Harrison 
1098fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
10994aa99606SHarvey Harrison }
11004aa99606SHarvey Harrison 
1101cf3b429bSJoe Perches static noinline_for_stack
110210679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
110310679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
110410679643SDaniel Borkmann {
110510679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
110610679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
110710679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
110810679643SDaniel Borkmann 		      sizeof("%1234567890")];
110910679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
111010679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
111110679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
111210679643SDaniel Borkmann 	u8 off = 0;
111310679643SDaniel Borkmann 
111410679643SDaniel Borkmann 	fmt++;
111510679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
111610679643SDaniel Borkmann 		switch (*fmt) {
111710679643SDaniel Borkmann 		case 'p':
111810679643SDaniel Borkmann 			have_p = true;
111910679643SDaniel Borkmann 			break;
112010679643SDaniel Borkmann 		case 'f':
112110679643SDaniel Borkmann 			have_f = true;
112210679643SDaniel Borkmann 			break;
112310679643SDaniel Borkmann 		case 's':
112410679643SDaniel Borkmann 			have_s = true;
112510679643SDaniel Borkmann 			break;
112610679643SDaniel Borkmann 		case 'c':
112710679643SDaniel Borkmann 			have_c = true;
112810679643SDaniel Borkmann 			break;
112910679643SDaniel Borkmann 		}
113010679643SDaniel Borkmann 	}
113110679643SDaniel Borkmann 
113210679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
113310679643SDaniel Borkmann 		*p = '[';
113410679643SDaniel Borkmann 		off = 1;
113510679643SDaniel Borkmann 	}
113610679643SDaniel Borkmann 
113710679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
113810679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
113910679643SDaniel Borkmann 	else
114010679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
114110679643SDaniel Borkmann 
114210679643SDaniel Borkmann 	if (have_p || have_s || have_f)
114310679643SDaniel Borkmann 		*p++ = ']';
114410679643SDaniel Borkmann 
114510679643SDaniel Borkmann 	if (have_p) {
114610679643SDaniel Borkmann 		*p++ = ':';
114710679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
114810679643SDaniel Borkmann 	}
114910679643SDaniel Borkmann 	if (have_f) {
115010679643SDaniel Borkmann 		*p++ = '/';
115110679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
115210679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
115310679643SDaniel Borkmann 	}
115410679643SDaniel Borkmann 	if (have_s) {
115510679643SDaniel Borkmann 		*p++ = '%';
115610679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
115710679643SDaniel Borkmann 	}
115810679643SDaniel Borkmann 	*p = '\0';
115910679643SDaniel Borkmann 
116010679643SDaniel Borkmann 	return string(buf, end, ip6_addr, spec);
116110679643SDaniel Borkmann }
116210679643SDaniel Borkmann 
116310679643SDaniel Borkmann static noinline_for_stack
116410679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
116510679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
116610679643SDaniel Borkmann {
116710679643SDaniel Borkmann 	bool have_p = false;
116810679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
116910679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
117010679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
117110679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
117210679643SDaniel Borkmann 
117310679643SDaniel Borkmann 	fmt++;
117410679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
117510679643SDaniel Borkmann 		switch (*fmt) {
117610679643SDaniel Borkmann 		case 'p':
117710679643SDaniel Borkmann 			have_p = true;
117810679643SDaniel Borkmann 			break;
117910679643SDaniel Borkmann 		case 'h':
118010679643SDaniel Borkmann 		case 'l':
118110679643SDaniel Borkmann 		case 'n':
118210679643SDaniel Borkmann 		case 'b':
118310679643SDaniel Borkmann 			fmt4[2] = *fmt;
118410679643SDaniel Borkmann 			break;
118510679643SDaniel Borkmann 		}
118610679643SDaniel Borkmann 	}
118710679643SDaniel Borkmann 
118810679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
118910679643SDaniel Borkmann 	if (have_p) {
119010679643SDaniel Borkmann 		*p++ = ':';
119110679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
119210679643SDaniel Borkmann 	}
119310679643SDaniel Borkmann 	*p = '\0';
119410679643SDaniel Borkmann 
119510679643SDaniel Borkmann 	return string(buf, end, ip4_addr, spec);
119610679643SDaniel Borkmann }
119710679643SDaniel Borkmann 
119810679643SDaniel Borkmann static noinline_for_stack
119971dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
120071dca95dSAndy Shevchenko 		     const char *fmt)
120171dca95dSAndy Shevchenko {
120271dca95dSAndy Shevchenko 	bool found = true;
120371dca95dSAndy Shevchenko 	int count = 1;
120471dca95dSAndy Shevchenko 	unsigned int flags = 0;
120571dca95dSAndy Shevchenko 	int len;
120671dca95dSAndy Shevchenko 
120771dca95dSAndy Shevchenko 	if (spec.field_width == 0)
120871dca95dSAndy Shevchenko 		return buf;				/* nothing to print */
120971dca95dSAndy Shevchenko 
121071dca95dSAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
121171dca95dSAndy Shevchenko 		return string(buf, end, NULL, spec);	/* NULL pointer */
121271dca95dSAndy Shevchenko 
121371dca95dSAndy Shevchenko 
121471dca95dSAndy Shevchenko 	do {
121571dca95dSAndy Shevchenko 		switch (fmt[count++]) {
121671dca95dSAndy Shevchenko 		case 'a':
121771dca95dSAndy Shevchenko 			flags |= ESCAPE_ANY;
121871dca95dSAndy Shevchenko 			break;
121971dca95dSAndy Shevchenko 		case 'c':
122071dca95dSAndy Shevchenko 			flags |= ESCAPE_SPECIAL;
122171dca95dSAndy Shevchenko 			break;
122271dca95dSAndy Shevchenko 		case 'h':
122371dca95dSAndy Shevchenko 			flags |= ESCAPE_HEX;
122471dca95dSAndy Shevchenko 			break;
122571dca95dSAndy Shevchenko 		case 'n':
122671dca95dSAndy Shevchenko 			flags |= ESCAPE_NULL;
122771dca95dSAndy Shevchenko 			break;
122871dca95dSAndy Shevchenko 		case 'o':
122971dca95dSAndy Shevchenko 			flags |= ESCAPE_OCTAL;
123071dca95dSAndy Shevchenko 			break;
123171dca95dSAndy Shevchenko 		case 'p':
123271dca95dSAndy Shevchenko 			flags |= ESCAPE_NP;
123371dca95dSAndy Shevchenko 			break;
123471dca95dSAndy Shevchenko 		case 's':
123571dca95dSAndy Shevchenko 			flags |= ESCAPE_SPACE;
123671dca95dSAndy Shevchenko 			break;
123771dca95dSAndy Shevchenko 		default:
123871dca95dSAndy Shevchenko 			found = false;
123971dca95dSAndy Shevchenko 			break;
124071dca95dSAndy Shevchenko 		}
124171dca95dSAndy Shevchenko 	} while (found);
124271dca95dSAndy Shevchenko 
124371dca95dSAndy Shevchenko 	if (!flags)
124471dca95dSAndy Shevchenko 		flags = ESCAPE_ANY_NP;
124571dca95dSAndy Shevchenko 
124671dca95dSAndy Shevchenko 	len = spec.field_width < 0 ? 1 : spec.field_width;
124771dca95dSAndy Shevchenko 
124841416f23SRasmus Villemoes 	/*
124941416f23SRasmus Villemoes 	 * string_escape_mem() writes as many characters as it can to
125041416f23SRasmus Villemoes 	 * the given buffer, and returns the total size of the output
125141416f23SRasmus Villemoes 	 * had the buffer been big enough.
125241416f23SRasmus Villemoes 	 */
125341416f23SRasmus Villemoes 	buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
125471dca95dSAndy Shevchenko 
125571dca95dSAndy Shevchenko 	return buf;
125671dca95dSAndy Shevchenko }
125771dca95dSAndy Shevchenko 
125871dca95dSAndy Shevchenko static noinline_for_stack
1259cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
12609ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
12619ac6e44eSJoe Perches {
12629ac6e44eSJoe Perches 	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
12639ac6e44eSJoe Perches 	char *p = uuid;
12649ac6e44eSJoe Perches 	int i;
12659ac6e44eSJoe Perches 	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
12669ac6e44eSJoe Perches 	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
12679ac6e44eSJoe Perches 	const u8 *index = be;
12689ac6e44eSJoe Perches 	bool uc = false;
12699ac6e44eSJoe Perches 
12709ac6e44eSJoe Perches 	switch (*(++fmt)) {
12719ac6e44eSJoe Perches 	case 'L':
12729ac6e44eSJoe Perches 		uc = true;		/* fall-through */
12739ac6e44eSJoe Perches 	case 'l':
12749ac6e44eSJoe Perches 		index = le;
12759ac6e44eSJoe Perches 		break;
12769ac6e44eSJoe Perches 	case 'B':
12779ac6e44eSJoe Perches 		uc = true;
12789ac6e44eSJoe Perches 		break;
12799ac6e44eSJoe Perches 	}
12809ac6e44eSJoe Perches 
12819ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
128255036ba7SAndy Shevchenko 		p = hex_byte_pack(p, addr[index[i]]);
12839ac6e44eSJoe Perches 		switch (i) {
12849ac6e44eSJoe Perches 		case 3:
12859ac6e44eSJoe Perches 		case 5:
12869ac6e44eSJoe Perches 		case 7:
12879ac6e44eSJoe Perches 		case 9:
12889ac6e44eSJoe Perches 			*p++ = '-';
12899ac6e44eSJoe Perches 			break;
12909ac6e44eSJoe Perches 		}
12919ac6e44eSJoe Perches 	}
12929ac6e44eSJoe Perches 
12939ac6e44eSJoe Perches 	*p = 0;
12949ac6e44eSJoe Perches 
12959ac6e44eSJoe Perches 	if (uc) {
12969ac6e44eSJoe Perches 		p = uuid;
12979ac6e44eSJoe Perches 		do {
12989ac6e44eSJoe Perches 			*p = toupper(*p);
12999ac6e44eSJoe Perches 		} while (*(++p));
13009ac6e44eSJoe Perches 	}
13019ac6e44eSJoe Perches 
13029ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
13039ac6e44eSJoe Perches }
13049ac6e44eSJoe Perches 
1305c8f44affSMichał Mirosław static
1306c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1307c8f44affSMichał Mirosław 		      struct printf_spec spec)
1308c8f44affSMichał Mirosław {
1309c8f44affSMichał Mirosław 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1310c8f44affSMichał Mirosław 	if (spec.field_width == -1)
1311c8f44affSMichał Mirosław 		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1312c8f44affSMichał Mirosław 	spec.base = 16;
1313c8f44affSMichał Mirosław 
1314c8f44affSMichał Mirosław 	return number(buf, end, *(const netdev_features_t *)addr, spec);
1315c8f44affSMichał Mirosław }
1316c8f44affSMichał Mirosław 
1317aaf07621SJoe Perches static noinline_for_stack
1318aaf07621SJoe Perches char *address_val(char *buf, char *end, const void *addr,
1319aaf07621SJoe Perches 		  struct printf_spec spec, const char *fmt)
1320aaf07621SJoe Perches {
1321aaf07621SJoe Perches 	unsigned long long num;
1322aaf07621SJoe Perches 
1323aaf07621SJoe Perches 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1324aaf07621SJoe Perches 	spec.base = 16;
1325aaf07621SJoe Perches 
1326aaf07621SJoe Perches 	switch (fmt[1]) {
1327aaf07621SJoe Perches 	case 'd':
1328aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
1329aaf07621SJoe Perches 		spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1330aaf07621SJoe Perches 		break;
1331aaf07621SJoe Perches 	case 'p':
1332aaf07621SJoe Perches 	default:
1333aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
1334aaf07621SJoe Perches 		spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1335aaf07621SJoe Perches 		break;
1336aaf07621SJoe Perches 	}
1337aaf07621SJoe Perches 
1338aaf07621SJoe Perches 	return number(buf, end, num, spec);
1339aaf07621SJoe Perches }
1340aaf07621SJoe Perches 
1341900cca29SGeert Uytterhoeven static noinline_for_stack
1342900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1343900cca29SGeert Uytterhoeven 	    const char *fmt)
1344900cca29SGeert Uytterhoeven {
1345900cca29SGeert Uytterhoeven 	if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1346900cca29SGeert Uytterhoeven 		return string(buf, end, NULL, spec);
1347900cca29SGeert Uytterhoeven 
1348900cca29SGeert Uytterhoeven 	switch (fmt[1]) {
1349900cca29SGeert Uytterhoeven 	case 'r':
1350900cca29SGeert Uytterhoeven 		return number(buf, end, clk_get_rate(clk), spec);
1351900cca29SGeert Uytterhoeven 
1352900cca29SGeert Uytterhoeven 	case 'n':
1353900cca29SGeert Uytterhoeven 	default:
1354900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK
1355900cca29SGeert Uytterhoeven 		return string(buf, end, __clk_get_name(clk), spec);
1356900cca29SGeert Uytterhoeven #else
1357900cca29SGeert Uytterhoeven 		spec.base = 16;
1358900cca29SGeert Uytterhoeven 		spec.field_width = sizeof(unsigned long) * 2 + 2;
1359900cca29SGeert Uytterhoeven 		spec.flags |= SPECIAL | SMALL | ZEROPAD;
1360900cca29SGeert Uytterhoeven 		return number(buf, end, (unsigned long)clk, spec);
1361900cca29SGeert Uytterhoeven #endif
1362900cca29SGeert Uytterhoeven 	}
1363900cca29SGeert Uytterhoeven }
1364900cca29SGeert Uytterhoeven 
1365411f05f1SIngo Molnar int kptr_restrict __read_mostly;
1366455cd5abSDan Rosenberg 
13674d8a743cSLinus Torvalds /*
13684d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
13694d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
13704d8a743cSLinus Torvalds  * specifiers.
13714d8a743cSLinus Torvalds  *
1372332d2e78SLinus Torvalds  * Right now we handle:
13730fe1ef24SLinus Torvalds  *
13740c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
13750c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
13760efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
13770efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
1378b0d33c2bSJoe Perches  * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
13790f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
1380c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1381c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1382dbc760bcSTejun Heo  * - 'b[l]' For a bitmap, the number of bits is determined by the field
1383dbc760bcSTejun Heo  *       width which must be explicitly specified either as part of the
1384dbc760bcSTejun Heo  *       format string '%32b[l]' or through '%*b[l]', [l] selects
1385dbc760bcSTejun Heo  *       range-list format instead of hex format
1386dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
1387dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
13888a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1389bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1390c8e00060SJoe Perches  *       with a dash-separated hex notation
13917c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
13928a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
13938a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
13948a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
139510679643SDaniel Borkmann  *       [S][pfs]
139610679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
139710679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
13988a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
13998a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
14008a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
140110679643SDaniel Borkmann  *       [S][pfs]
140210679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
140310679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
140410679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
140510679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
140629cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
140771dca95dSAndy Shevchenko  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
140871dca95dSAndy Shevchenko  *                of the following flags (see string_escape_mem() for the
140971dca95dSAndy Shevchenko  *                details):
141071dca95dSAndy Shevchenko  *                  a - ESCAPE_ANY
141171dca95dSAndy Shevchenko  *                  c - ESCAPE_SPECIAL
141271dca95dSAndy Shevchenko  *                  h - ESCAPE_HEX
141371dca95dSAndy Shevchenko  *                  n - ESCAPE_NULL
141471dca95dSAndy Shevchenko  *                  o - ESCAPE_OCTAL
141571dca95dSAndy Shevchenko  *                  p - ESCAPE_NP
141671dca95dSAndy Shevchenko  *                  s - ESCAPE_SPACE
141771dca95dSAndy Shevchenko  *                By default ESCAPE_ANY_NP is used.
14189ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
14199ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
14209ac6e44eSJoe Perches  *       Options for %pU are:
14219ac6e44eSJoe Perches  *         b big endian lower case hex (default)
14229ac6e44eSJoe Perches  *         B big endian UPPER case hex
14239ac6e44eSJoe Perches  *         l little endian lower case hex
14249ac6e44eSJoe Perches  *         L little endian UPPER case hex
14259ac6e44eSJoe Perches  *           big endian output byte order is:
14269ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
14279ac6e44eSJoe Perches  *           little endian output byte order is:
14289ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
14297db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
14307db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
14317db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
14327db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
14337db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
1434455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
1435c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
143631550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
143731550a16SAndy Shevchenko  *            a certain separator (' ' by default):
143831550a16SAndy Shevchenko  *              C colon
143931550a16SAndy Shevchenko  *              D dash
144031550a16SAndy Shevchenko  *              N no separator
144131550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
144231550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
1443aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1444aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
1445c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
1446c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
1447900cca29SGeert Uytterhoeven  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1448900cca29SGeert Uytterhoeven  *       (legacy clock framework) of the clock
1449900cca29SGeert Uytterhoeven  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1450900cca29SGeert Uytterhoeven  *        (legacy clock framework) of the clock
1451900cca29SGeert Uytterhoeven  * - 'Cr' For a clock, it prints the current rate of the clock
14529ac6e44eSJoe Perches  *
1453332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1454332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
1455332d2e78SLinus Torvalds  * pointer to the real address.
14564d8a743cSLinus Torvalds  */
1457cf3b429bSJoe Perches static noinline_for_stack
1458cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1459fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
146078a8bf69SLinus Torvalds {
1461725fe002SGrant Likely 	int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1462725fe002SGrant Likely 
14639f36e2c4SKees Cook 	if (!ptr && *fmt != 'K') {
14645e057981SJoe Perches 		/*
14655e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
14665e057981SJoe Perches 		 * tabular output look nice.
14675e057981SJoe Perches 		 */
14685e057981SJoe Perches 		if (spec.field_width == -1)
1469725fe002SGrant Likely 			spec.field_width = default_width;
1470fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
14715e057981SJoe Perches 	}
1472d97106abSLinus Torvalds 
14730fe1ef24SLinus Torvalds 	switch (*fmt) {
14740fe1ef24SLinus Torvalds 	case 'F':
14750c8b946eSFrederic Weisbecker 	case 'f':
14760fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
14770fe1ef24SLinus Torvalds 		/* Fallthrough */
14780fe1ef24SLinus Torvalds 	case 'S':
14799ac6e44eSJoe Perches 	case 's':
14800f77a8d3SNamhyung Kim 	case 'B':
1481b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
1482332d2e78SLinus Torvalds 	case 'R':
1483c7dabef8SBjorn Helgaas 	case 'r':
1484fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
148531550a16SAndy Shevchenko 	case 'h':
148631550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
1487dbc760bcSTejun Heo 	case 'b':
1488dbc760bcSTejun Heo 		switch (fmt[1]) {
1489dbc760bcSTejun Heo 		case 'l':
1490dbc760bcSTejun Heo 			return bitmap_list_string(buf, end, ptr, spec, fmt);
1491dbc760bcSTejun Heo 		default:
1492dbc760bcSTejun Heo 			return bitmap_string(buf, end, ptr, spec, fmt);
1493dbc760bcSTejun Heo 		}
14948a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
14958a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
149676597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
149776597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
14988a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
14998a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
15008a27f7c9SJoe Perches 					 * 4:	1.2.3.4
15018a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
15028a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
15038a27f7c9SJoe Perches 					 */
15048a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
15058a27f7c9SJoe Perches 					 * 4:	001.002.003.004
15068a27f7c9SJoe Perches 					 * 6:   000102...0f
15078a27f7c9SJoe Perches 					 */
15088a27f7c9SJoe Perches 		switch (fmt[1]) {
15098a27f7c9SJoe Perches 		case '6':
15108a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
15118a27f7c9SJoe Perches 		case '4':
15128a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
151310679643SDaniel Borkmann 		case 'S': {
151410679643SDaniel Borkmann 			const union {
151510679643SDaniel Borkmann 				struct sockaddr		raw;
151610679643SDaniel Borkmann 				struct sockaddr_in	v4;
151710679643SDaniel Borkmann 				struct sockaddr_in6	v6;
151810679643SDaniel Borkmann 			} *sa = ptr;
151910679643SDaniel Borkmann 
152010679643SDaniel Borkmann 			switch (sa->raw.sa_family) {
152110679643SDaniel Borkmann 			case AF_INET:
152210679643SDaniel Borkmann 				return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
152310679643SDaniel Borkmann 			case AF_INET6:
152410679643SDaniel Borkmann 				return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
152510679643SDaniel Borkmann 			default:
152610679643SDaniel Borkmann 				return string(buf, end, "(invalid address)", spec);
152710679643SDaniel Borkmann 			}}
15288a27f7c9SJoe Perches 		}
15294aa99606SHarvey Harrison 		break;
153071dca95dSAndy Shevchenko 	case 'E':
153171dca95dSAndy Shevchenko 		return escaped_string(buf, end, ptr, spec, fmt);
15329ac6e44eSJoe Perches 	case 'U':
15339ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
15347db6f5fbSJoe Perches 	case 'V':
15355756b76eSJan Beulich 		{
15365756b76eSJan Beulich 			va_list va;
15375756b76eSJan Beulich 
15385756b76eSJan Beulich 			va_copy(va, *((struct va_format *)ptr)->va);
15395756b76eSJan Beulich 			buf += vsnprintf(buf, end > buf ? end - buf : 0,
15405756b76eSJan Beulich 					 ((struct va_format *)ptr)->fmt, va);
15415756b76eSJan Beulich 			va_end(va);
15425756b76eSJan Beulich 			return buf;
15435756b76eSJan Beulich 		}
1544455cd5abSDan Rosenberg 	case 'K':
1545455cd5abSDan Rosenberg 		/*
1546455cd5abSDan Rosenberg 		 * %pK cannot be used in IRQ context because its test
1547455cd5abSDan Rosenberg 		 * for CAP_SYSLOG would be meaningless.
1548455cd5abSDan Rosenberg 		 */
15493715c530SDan Rosenberg 		if (kptr_restrict && (in_irq() || in_serving_softirq() ||
15503715c530SDan Rosenberg 				      in_nmi())) {
1551455cd5abSDan Rosenberg 			if (spec.field_width == -1)
1552725fe002SGrant Likely 				spec.field_width = default_width;
1553455cd5abSDan Rosenberg 			return string(buf, end, "pK-error", spec);
1554455cd5abSDan Rosenberg 		}
1555312b4e22SRyan Mallon 
1556312b4e22SRyan Mallon 		switch (kptr_restrict) {
1557312b4e22SRyan Mallon 		case 0:
1558312b4e22SRyan Mallon 			/* Always print %pK values */
1559312b4e22SRyan Mallon 			break;
1560312b4e22SRyan Mallon 		case 1: {
1561312b4e22SRyan Mallon 			/*
1562312b4e22SRyan Mallon 			 * Only print the real pointer value if the current
1563312b4e22SRyan Mallon 			 * process has CAP_SYSLOG and is running with the
1564312b4e22SRyan Mallon 			 * same credentials it started with. This is because
1565312b4e22SRyan Mallon 			 * access to files is checked at open() time, but %pK
1566312b4e22SRyan Mallon 			 * checks permission at read() time. We don't want to
1567312b4e22SRyan Mallon 			 * leak pointer values if a binary opens a file using
1568312b4e22SRyan Mallon 			 * %pK and then elevates privileges before reading it.
1569312b4e22SRyan Mallon 			 */
1570312b4e22SRyan Mallon 			const struct cred *cred = current_cred();
1571312b4e22SRyan Mallon 
1572312b4e22SRyan Mallon 			if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1573312b4e22SRyan Mallon 			    !uid_eq(cred->euid, cred->uid) ||
1574312b4e22SRyan Mallon 			    !gid_eq(cred->egid, cred->gid))
157526297607SJoe Perches 				ptr = NULL;
157626297607SJoe Perches 			break;
1577312b4e22SRyan Mallon 		}
1578312b4e22SRyan Mallon 		case 2:
1579312b4e22SRyan Mallon 		default:
1580312b4e22SRyan Mallon 			/* Always print 0's for %pK */
1581312b4e22SRyan Mallon 			ptr = NULL;
1582312b4e22SRyan Mallon 			break;
1583312b4e22SRyan Mallon 		}
1584312b4e22SRyan Mallon 		break;
1585312b4e22SRyan Mallon 
1586c8f44affSMichał Mirosław 	case 'N':
1587c8f44affSMichał Mirosław 		switch (fmt[1]) {
1588c8f44affSMichał Mirosław 		case 'F':
1589c8f44affSMichał Mirosław 			return netdev_feature_string(buf, end, ptr, spec);
1590c8f44affSMichał Mirosław 		}
1591c8f44affSMichał Mirosław 		break;
15927d799210SStepan Moskovchenko 	case 'a':
1593aaf07621SJoe Perches 		return address_val(buf, end, ptr, spec, fmt);
15944b6ccca7SAl Viro 	case 'd':
15954b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
1596900cca29SGeert Uytterhoeven 	case 'C':
1597900cca29SGeert Uytterhoeven 		return clock(buf, end, ptr, spec, fmt);
15984b6ccca7SAl Viro 	case 'D':
15994b6ccca7SAl Viro 		return dentry_name(buf, end,
16004b6ccca7SAl Viro 				   ((const struct file *)ptr)->f_path.dentry,
16014b6ccca7SAl Viro 				   spec, fmt);
16020fe1ef24SLinus Torvalds 	}
1603fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
1604fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
1605725fe002SGrant Likely 		spec.field_width = default_width;
1606fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
160778a8bf69SLinus Torvalds 	}
1608fef20d9cSFrederic Weisbecker 	spec.base = 16;
1609fef20d9cSFrederic Weisbecker 
1610fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
1611fef20d9cSFrederic Weisbecker }
1612fef20d9cSFrederic Weisbecker 
1613fef20d9cSFrederic Weisbecker /*
1614fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
1615fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
1616fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
1617fef20d9cSFrederic Weisbecker  * to go on the next call).
1618fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
1619fef20d9cSFrederic Weisbecker  *
1620fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
1621fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
1622fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
1623fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
1624fef20d9cSFrederic Weisbecker  *
1625fef20d9cSFrederic Weisbecker  * @fmt: the format string
1626fef20d9cSFrederic Weisbecker  * @type of the token returned
1627fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
1628fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
1629fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
1630fef20d9cSFrederic Weisbecker  * @precision: precision of a number
1631fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
1632fef20d9cSFrederic Weisbecker  */
1633cf3b429bSJoe Perches static noinline_for_stack
1634cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
1635fef20d9cSFrederic Weisbecker {
1636fef20d9cSFrederic Weisbecker 	const char *start = fmt;
1637fef20d9cSFrederic Weisbecker 
1638fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
1639ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
1640fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
1641fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
1642fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
1643fef20d9cSFrederic Weisbecker 		}
1644fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1645fef20d9cSFrederic Weisbecker 		goto precision;
1646fef20d9cSFrederic Weisbecker 	}
1647fef20d9cSFrederic Weisbecker 
1648fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
1649fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
1650fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
1651fef20d9cSFrederic Weisbecker 			spec->precision = 0;
1652fef20d9cSFrederic Weisbecker 
1653fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1654fef20d9cSFrederic Weisbecker 		goto qualifier;
1655fef20d9cSFrederic Weisbecker 	}
1656fef20d9cSFrederic Weisbecker 
1657fef20d9cSFrederic Weisbecker 	/* By default */
1658fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
1659fef20d9cSFrederic Weisbecker 
1660fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
1661fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
1662fef20d9cSFrederic Weisbecker 			break;
1663fef20d9cSFrederic Weisbecker 	}
1664fef20d9cSFrederic Weisbecker 
1665fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
1666fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
1667fef20d9cSFrederic Weisbecker 		return fmt - start;
1668fef20d9cSFrederic Weisbecker 
1669fef20d9cSFrederic Weisbecker 	/* Process flags */
1670fef20d9cSFrederic Weisbecker 	spec->flags = 0;
1671fef20d9cSFrederic Weisbecker 
1672fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
1673fef20d9cSFrederic Weisbecker 		bool found = true;
1674fef20d9cSFrederic Weisbecker 
1675fef20d9cSFrederic Weisbecker 		++fmt;
1676fef20d9cSFrederic Weisbecker 
1677fef20d9cSFrederic Weisbecker 		switch (*fmt) {
1678fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
1679fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
1680fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
1681fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
1682fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
1683fef20d9cSFrederic Weisbecker 		default:  found = false;
1684fef20d9cSFrederic Weisbecker 		}
1685fef20d9cSFrederic Weisbecker 
1686fef20d9cSFrederic Weisbecker 		if (!found)
1687fef20d9cSFrederic Weisbecker 			break;
1688fef20d9cSFrederic Weisbecker 	}
1689fef20d9cSFrederic Weisbecker 
1690fef20d9cSFrederic Weisbecker 	/* get field width */
1691fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1692fef20d9cSFrederic Weisbecker 
1693fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1694fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1695fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1696fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1697ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1698fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1699fef20d9cSFrederic Weisbecker 	}
1700fef20d9cSFrederic Weisbecker 
1701fef20d9cSFrederic Weisbecker precision:
1702fef20d9cSFrederic Weisbecker 	/* get the precision */
1703fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1704fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1705fef20d9cSFrederic Weisbecker 		++fmt;
1706fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1707fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1708fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1709fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1710fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1711fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1712adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1713fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1714fef20d9cSFrederic Weisbecker 		}
1715fef20d9cSFrederic Weisbecker 	}
1716fef20d9cSFrederic Weisbecker 
1717fef20d9cSFrederic Weisbecker qualifier:
1718fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1719fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
172075fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
172175fb8f26SAndy Shevchenko 	    _tolower(*fmt) == 'z' || *fmt == 't') {
1722a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1723a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1724a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1725fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1726fef20d9cSFrederic Weisbecker 				++fmt;
1727a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1728a4e94ef0SZhaolei 				spec->qualifier = 'H';
1729a4e94ef0SZhaolei 				++fmt;
1730a4e94ef0SZhaolei 			}
1731fef20d9cSFrederic Weisbecker 		}
1732fef20d9cSFrederic Weisbecker 	}
1733fef20d9cSFrederic Weisbecker 
1734fef20d9cSFrederic Weisbecker 	/* default base */
1735fef20d9cSFrederic Weisbecker 	spec->base = 10;
1736fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1737fef20d9cSFrederic Weisbecker 	case 'c':
1738fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1739fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1740fef20d9cSFrederic Weisbecker 
1741fef20d9cSFrederic Weisbecker 	case 's':
1742fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1743fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1744fef20d9cSFrederic Weisbecker 
1745fef20d9cSFrederic Weisbecker 	case 'p':
1746fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1747ffbfed03SRasmus Villemoes 		return ++fmt - start;
1748fef20d9cSFrederic Weisbecker 
1749fef20d9cSFrederic Weisbecker 	case '%':
1750fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1751fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1752fef20d9cSFrederic Weisbecker 
1753fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1754fef20d9cSFrederic Weisbecker 	case 'o':
1755fef20d9cSFrederic Weisbecker 		spec->base = 8;
1756fef20d9cSFrederic Weisbecker 		break;
1757fef20d9cSFrederic Weisbecker 
1758fef20d9cSFrederic Weisbecker 	case 'x':
1759fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1760fef20d9cSFrederic Weisbecker 
1761fef20d9cSFrederic Weisbecker 	case 'X':
1762fef20d9cSFrederic Weisbecker 		spec->base = 16;
1763fef20d9cSFrederic Weisbecker 		break;
1764fef20d9cSFrederic Weisbecker 
1765fef20d9cSFrederic Weisbecker 	case 'd':
1766fef20d9cSFrederic Weisbecker 	case 'i':
176739e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1768fef20d9cSFrederic Weisbecker 	case 'u':
1769fef20d9cSFrederic Weisbecker 		break;
1770fef20d9cSFrederic Weisbecker 
1771708d96fdSRyan Mallon 	case 'n':
1772708d96fdSRyan Mallon 		/*
1773708d96fdSRyan Mallon 		 * Since %n poses a greater security risk than utility, treat
1774708d96fdSRyan Mallon 		 * it as an invalid format specifier. Warn about its use so
1775708d96fdSRyan Mallon 		 * that new instances don't get added.
1776708d96fdSRyan Mallon 		 */
1777708d96fdSRyan Mallon 		WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt);
1778708d96fdSRyan Mallon 		/* Fall-through */
1779708d96fdSRyan Mallon 
1780fef20d9cSFrederic Weisbecker 	default:
1781fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1782fef20d9cSFrederic Weisbecker 		return fmt - start;
1783fef20d9cSFrederic Weisbecker 	}
1784fef20d9cSFrederic Weisbecker 
1785fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1786fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1787fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
178851be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
178951be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
179075fb8f26SAndy Shevchenko 	} else if (_tolower(spec->qualifier) == 'z') {
1791fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1792fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1793fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1794a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
179551be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
179651be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
1797fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
179851be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
179951be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
1800fef20d9cSFrederic Weisbecker 	} else {
180151be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
180251be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
1803fef20d9cSFrederic Weisbecker 	}
1804fef20d9cSFrederic Weisbecker 
1805fef20d9cSFrederic Weisbecker 	return ++fmt - start;
180678a8bf69SLinus Torvalds }
180778a8bf69SLinus Torvalds 
18081da177e4SLinus Torvalds /**
18091da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
18101da177e4SLinus Torvalds  * @buf: The buffer to place the result into
18111da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
18121da177e4SLinus Torvalds  * @fmt: The format string to use
18131da177e4SLinus Torvalds  * @args: Arguments for the format string
18141da177e4SLinus Torvalds  *
181520036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
181691adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
181791adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
18180c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
18190c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
18200f77a8d3SNamhyung Kim  * %pB output the name of a backtrace symbol with its offset
18218a79503aSUwe Kleine-König  * %pR output the address range in a struct resource with decoded flags
18228a79503aSUwe Kleine-König  * %pr output the address range in a struct resource with raw flags
1823dbc760bcSTejun Heo  * %pb output the bitmap with field width as the number of bits
1824dbc760bcSTejun Heo  * %pbl output the bitmap as range list with field width as the number of bits
18258a79503aSUwe Kleine-König  * %pM output a 6-byte MAC address with colons
18267c59154eSAndy Shevchenko  * %pMR output a 6-byte MAC address with colons in reversed order
18277c59154eSAndy Shevchenko  * %pMF output a 6-byte MAC address with dashes
18288a79503aSUwe Kleine-König  * %pm output a 6-byte MAC address without colons
18297c59154eSAndy Shevchenko  * %pmR output a 6-byte MAC address without colons in reversed order
18308a79503aSUwe Kleine-König  * %pI4 print an IPv4 address without leading zeros
18318a79503aSUwe Kleine-König  * %pi4 print an IPv4 address with leading zeros
18328a79503aSUwe Kleine-König  * %pI6 print an IPv6 address with colons
18338a79503aSUwe Kleine-König  * %pi6 print an IPv6 address without colons
1834f996f208SJan Engelhardt  * %pI6c print an IPv6 address as specified by RFC 5952
183510679643SDaniel Borkmann  * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
183610679643SDaniel Borkmann  * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
18378a79503aSUwe Kleine-König  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
18388a79503aSUwe Kleine-König  *   case.
183971dca95dSAndy Shevchenko  * %*pE[achnops] print an escaped buffer
184031550a16SAndy Shevchenko  * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
184131550a16SAndy Shevchenko  *           bytes of the input)
1842900cca29SGeert Uytterhoeven  * %pC output the name (Common Clock Framework) or address (legacy clock
1843900cca29SGeert Uytterhoeven  *     framework) of a clock
1844900cca29SGeert Uytterhoeven  * %pCn output the name (Common Clock Framework) or address (legacy clock
1845900cca29SGeert Uytterhoeven  *      framework) of a clock
1846900cca29SGeert Uytterhoeven  * %pCr output the current rate of a clock
18470efb4d20SSteven Rostedt  * %n is ignored
184820036fdcSAndi Kleen  *
184980f548e0SAndrew Morton  * ** Please update Documentation/printk-formats.txt when making changes **
185080f548e0SAndrew Morton  *
18511da177e4SLinus Torvalds  * The return value is the number of characters which would
18521da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
18531da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
18541da177e4SLinus Torvalds  * number of characters written into @buf as return value
185572fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
18561da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
18571da177e4SLinus Torvalds  * string is truncated.
18581da177e4SLinus Torvalds  *
1859ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
18601da177e4SLinus Torvalds  */
18611da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
18621da177e4SLinus Torvalds {
18631da177e4SLinus Torvalds 	unsigned long long num;
1864d4be151bSAndré Goddard Rosa 	char *str, *end;
1865fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
18661da177e4SLinus Torvalds 
1867f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1868f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
18692aa2f9e2SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
18701da177e4SLinus Torvalds 		return 0;
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds 	str = buf;
1873f796937aSJeremy Fitzhardinge 	end = buf + size;
18741da177e4SLinus Torvalds 
1875f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1876f796937aSJeremy Fitzhardinge 	if (end < buf) {
18771da177e4SLinus Torvalds 		end = ((void *)-1);
1878f796937aSJeremy Fitzhardinge 		size = end - buf;
18791da177e4SLinus Torvalds 	}
18801da177e4SLinus Torvalds 
1881fef20d9cSFrederic Weisbecker 	while (*fmt) {
1882fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1883d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1884fef20d9cSFrederic Weisbecker 
1885fef20d9cSFrederic Weisbecker 		fmt += read;
1886fef20d9cSFrederic Weisbecker 
1887fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1888fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1889fef20d9cSFrederic Weisbecker 			int copy = read;
1890fef20d9cSFrederic Weisbecker 			if (str < end) {
1891fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1892fef20d9cSFrederic Weisbecker 					copy = end - str;
1893fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1894fef20d9cSFrederic Weisbecker 			}
1895fef20d9cSFrederic Weisbecker 			str += read;
1896fef20d9cSFrederic Weisbecker 			break;
18971da177e4SLinus Torvalds 		}
18981da177e4SLinus Torvalds 
1899ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1900fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1901fef20d9cSFrederic Weisbecker 			break;
19021da177e4SLinus Torvalds 
1903fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1904fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1905fef20d9cSFrederic Weisbecker 			break;
19061da177e4SLinus Torvalds 
1907d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1908d4be151bSAndré Goddard Rosa 			char c;
1909d4be151bSAndré Goddard Rosa 
1910fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1911fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1912f796937aSJeremy Fitzhardinge 					if (str < end)
19131da177e4SLinus Torvalds 						*str = ' ';
19141da177e4SLinus Torvalds 					++str;
1915fef20d9cSFrederic Weisbecker 
19161da177e4SLinus Torvalds 				}
19171da177e4SLinus Torvalds 			}
19181da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1919f796937aSJeremy Fitzhardinge 			if (str < end)
19201da177e4SLinus Torvalds 				*str = c;
19211da177e4SLinus Torvalds 			++str;
1922fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1923f796937aSJeremy Fitzhardinge 				if (str < end)
19241da177e4SLinus Torvalds 					*str = ' ';
19251da177e4SLinus Torvalds 				++str;
19261da177e4SLinus Torvalds 			}
1927fef20d9cSFrederic Weisbecker 			break;
1928d4be151bSAndré Goddard Rosa 		}
19291da177e4SLinus Torvalds 
1930fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1931fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1932fef20d9cSFrederic Weisbecker 			break;
19331da177e4SLinus Torvalds 
1934fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1935ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, va_arg(args, void *),
1936fef20d9cSFrederic Weisbecker 				      spec);
1937fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
19384d8a743cSLinus Torvalds 				fmt++;
1939fef20d9cSFrederic Weisbecker 			break;
19401da177e4SLinus Torvalds 
1941fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1942f796937aSJeremy Fitzhardinge 			if (str < end)
19431da177e4SLinus Torvalds 				*str = '%';
19441da177e4SLinus Torvalds 			++str;
19451da177e4SLinus Torvalds 			break;
19461da177e4SLinus Torvalds 
1947fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1948f796937aSJeremy Fitzhardinge 			if (str < end)
19491da177e4SLinus Torvalds 				*str = '%';
19501da177e4SLinus Torvalds 			++str;
1951fef20d9cSFrederic Weisbecker 			break;
1952fef20d9cSFrederic Weisbecker 
1953fef20d9cSFrederic Weisbecker 		default:
1954fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1955fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1956fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1957fef20d9cSFrederic Weisbecker 				break;
1958fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1959fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1960fef20d9cSFrederic Weisbecker 				break;
1961fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1962fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1963fef20d9cSFrederic Weisbecker 				break;
1964fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1965ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
1966ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
1967ef124960SJason Gunthorpe 				else
1968fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
1969fef20d9cSFrederic Weisbecker 				break;
1970fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1971fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1972fef20d9cSFrederic Weisbecker 				break;
1973a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1974a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1975a4e94ef0SZhaolei 				break;
1976a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1977a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1978a4e94ef0SZhaolei 				break;
1979fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1980fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1981fef20d9cSFrederic Weisbecker 				break;
1982fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1983fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1984fef20d9cSFrederic Weisbecker 				break;
198539e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
198639e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1987fef20d9cSFrederic Weisbecker 				break;
1988fef20d9cSFrederic Weisbecker 			default:
1989fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
19901da177e4SLinus Torvalds 			}
1991fef20d9cSFrederic Weisbecker 
1992fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
19931da177e4SLinus Torvalds 		}
1994fef20d9cSFrederic Weisbecker 	}
1995fef20d9cSFrederic Weisbecker 
1996f796937aSJeremy Fitzhardinge 	if (size > 0) {
1997f796937aSJeremy Fitzhardinge 		if (str < end)
19981da177e4SLinus Torvalds 			*str = '\0';
1999f796937aSJeremy Fitzhardinge 		else
20000a6047eeSLinus Torvalds 			end[-1] = '\0';
2001f796937aSJeremy Fitzhardinge 	}
2002fef20d9cSFrederic Weisbecker 
2003f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
20041da177e4SLinus Torvalds 	return str-buf;
2005fef20d9cSFrederic Weisbecker 
20061da177e4SLinus Torvalds }
20071da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds /**
20101da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
20111da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20121da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20131da177e4SLinus Torvalds  * @fmt: The format string to use
20141da177e4SLinus Torvalds  * @args: Arguments for the format string
20151da177e4SLinus Torvalds  *
20161da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
2017b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
20181da177e4SLinus Torvalds  * returns 0.
20191da177e4SLinus Torvalds  *
2020ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
202120036fdcSAndi Kleen  *
202220036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20231da177e4SLinus Torvalds  */
20241da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
20251da177e4SLinus Torvalds {
20261da177e4SLinus Torvalds 	int i;
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
20297b9186f5SAndré Goddard Rosa 
2030b921c69fSAnton Arapov 	if (likely(i < size))
2031b921c69fSAnton Arapov 		return i;
2032b921c69fSAnton Arapov 	if (size != 0)
2033b921c69fSAnton Arapov 		return size - 1;
2034b921c69fSAnton Arapov 	return 0;
20351da177e4SLinus Torvalds }
20361da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds /**
20391da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
20401da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20411da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20421da177e4SLinus Torvalds  * @fmt: The format string to use
20431da177e4SLinus Torvalds  * @...: Arguments for the format string
20441da177e4SLinus Torvalds  *
20451da177e4SLinus Torvalds  * The return value is the number of characters which would be
20461da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
20471da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
20481da177e4SLinus Torvalds  * @size, the resulting string is truncated.
204920036fdcSAndi Kleen  *
205020036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20511da177e4SLinus Torvalds  */
20521da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
20531da177e4SLinus Torvalds {
20541da177e4SLinus Torvalds 	va_list args;
20551da177e4SLinus Torvalds 	int i;
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	va_start(args, fmt);
20581da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
20591da177e4SLinus Torvalds 	va_end(args);
20607b9186f5SAndré Goddard Rosa 
20611da177e4SLinus Torvalds 	return i;
20621da177e4SLinus Torvalds }
20631da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds /**
20661da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
20671da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20681da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20691da177e4SLinus Torvalds  * @fmt: The format string to use
20701da177e4SLinus Torvalds  * @...: Arguments for the format string
20711da177e4SLinus Torvalds  *
20721da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
2073b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
20741da177e4SLinus Torvalds  */
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
20771da177e4SLinus Torvalds {
20781da177e4SLinus Torvalds 	va_list args;
20791da177e4SLinus Torvalds 	int i;
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds 	va_start(args, fmt);
2082b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
20831da177e4SLinus Torvalds 	va_end(args);
20847b9186f5SAndré Goddard Rosa 
2085b903c0b8SChangli Gao 	return i;
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds /**
20901da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
20911da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20921da177e4SLinus Torvalds  * @fmt: The format string to use
20931da177e4SLinus Torvalds  * @args: Arguments for the format string
20941da177e4SLinus Torvalds  *
20951da177e4SLinus Torvalds  * The function returns the number of characters written
209672fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
20971da177e4SLinus Torvalds  * buffer overflows.
20981da177e4SLinus Torvalds  *
2099ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
210020036fdcSAndi Kleen  *
210120036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
21021da177e4SLinus Torvalds  */
21031da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
21041da177e4SLinus Torvalds {
21051da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds /**
21101da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
21111da177e4SLinus Torvalds  * @buf: The buffer to place the result into
21121da177e4SLinus Torvalds  * @fmt: The format string to use
21131da177e4SLinus Torvalds  * @...: Arguments for the format string
21141da177e4SLinus Torvalds  *
21151da177e4SLinus Torvalds  * The function returns the number of characters written
211672fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
21171da177e4SLinus Torvalds  * buffer overflows.
211820036fdcSAndi Kleen  *
211920036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
21201da177e4SLinus Torvalds  */
21211da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
21221da177e4SLinus Torvalds {
21231da177e4SLinus Torvalds 	va_list args;
21241da177e4SLinus Torvalds 	int i;
21251da177e4SLinus Torvalds 
21261da177e4SLinus Torvalds 	va_start(args, fmt);
21271da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
21281da177e4SLinus Torvalds 	va_end(args);
21297b9186f5SAndré Goddard Rosa 
21301da177e4SLinus Torvalds 	return i;
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
21331da177e4SLinus Torvalds 
21344370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
21354370aa4aSLai Jiangshan /*
21364370aa4aSLai Jiangshan  * bprintf service:
21374370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
21384370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
21394370aa4aSLai Jiangshan  */
21404370aa4aSLai Jiangshan 
21414370aa4aSLai Jiangshan /**
21424370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
21434370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
21444370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
21454370aa4aSLai Jiangshan  * @fmt: The format string to use
21464370aa4aSLai Jiangshan  * @args: Arguments for the format string
21474370aa4aSLai Jiangshan  *
21484370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
2149da3dae54SMasanari Iida  * is skipped.
21504370aa4aSLai Jiangshan  *
21514370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
21524370aa4aSLai Jiangshan  * the given input.
21534370aa4aSLai Jiangshan  *
21544370aa4aSLai Jiangshan  * NOTE:
21554370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
21564370aa4aSLai Jiangshan  * valid for bstr_printf().
21574370aa4aSLai Jiangshan  */
21584370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
21594370aa4aSLai Jiangshan {
2160fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
21614370aa4aSLai Jiangshan 	char *str, *end;
21624370aa4aSLai Jiangshan 
21634370aa4aSLai Jiangshan 	str = (char *)bin_buf;
21644370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
21654370aa4aSLai Jiangshan 
21664370aa4aSLai Jiangshan #define save_arg(type)							\
21674370aa4aSLai Jiangshan do {									\
21684370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
21694370aa4aSLai Jiangshan 		unsigned long long value;				\
21704370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
21714370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
21724370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
21734370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
21744370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
21754370aa4aSLai Jiangshan 		}							\
21764370aa4aSLai Jiangshan 	} else {							\
21774370aa4aSLai Jiangshan 		unsigned long value;					\
21784370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
21794370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
21804370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
21814370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
21824370aa4aSLai Jiangshan 	}								\
21834370aa4aSLai Jiangshan 	str += sizeof(type);						\
21844370aa4aSLai Jiangshan } while (0)
21854370aa4aSLai Jiangshan 
2186fef20d9cSFrederic Weisbecker 	while (*fmt) {
2187d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
21884370aa4aSLai Jiangshan 
2189fef20d9cSFrederic Weisbecker 		fmt += read;
2190fef20d9cSFrederic Weisbecker 
2191fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2192fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
2193d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_INVALID:
2194d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
2195fef20d9cSFrederic Weisbecker 			break;
2196fef20d9cSFrederic Weisbecker 
2197ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2198fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
21994370aa4aSLai Jiangshan 			save_arg(int);
2200fef20d9cSFrederic Weisbecker 			break;
22014370aa4aSLai Jiangshan 
2202fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
22034370aa4aSLai Jiangshan 			save_arg(char);
2204fef20d9cSFrederic Weisbecker 			break;
2205fef20d9cSFrederic Weisbecker 
2206fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
22074370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
22084370aa4aSLai Jiangshan 			size_t len;
22096c356634SAndré Goddard Rosa 
22104370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
22114370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
22120f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
22136c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
22146c356634SAndré Goddard Rosa 			if (str + len < end)
22156c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
22166c356634SAndré Goddard Rosa 			str += len;
2217fef20d9cSFrederic Weisbecker 			break;
22184370aa4aSLai Jiangshan 		}
2219fef20d9cSFrederic Weisbecker 
2220fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
22214370aa4aSLai Jiangshan 			save_arg(void *);
22224370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2223fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
22244370aa4aSLai Jiangshan 				fmt++;
2225fef20d9cSFrederic Weisbecker 			break;
2226fef20d9cSFrederic Weisbecker 
2227fef20d9cSFrederic Weisbecker 		default:
2228fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2229fef20d9cSFrederic Weisbecker 
2230fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2231fef20d9cSFrederic Weisbecker 				save_arg(long long);
2232fef20d9cSFrederic Weisbecker 				break;
2233fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2234fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2235fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
2236fef20d9cSFrederic Weisbecker 				break;
2237fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2238fef20d9cSFrederic Weisbecker 				save_arg(size_t);
2239fef20d9cSFrederic Weisbecker 				break;
2240fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2241fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
2242fef20d9cSFrederic Weisbecker 				break;
2243a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2244a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2245a4e94ef0SZhaolei 				save_arg(char);
2246a4e94ef0SZhaolei 				break;
2247fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2248fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2249fef20d9cSFrederic Weisbecker 				save_arg(short);
2250fef20d9cSFrederic Weisbecker 				break;
2251fef20d9cSFrederic Weisbecker 			default:
2252fef20d9cSFrederic Weisbecker 				save_arg(int);
2253fef20d9cSFrederic Weisbecker 			}
2254fef20d9cSFrederic Weisbecker 		}
2255fef20d9cSFrederic Weisbecker 	}
2256fef20d9cSFrederic Weisbecker 
22577b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2258fef20d9cSFrederic Weisbecker #undef save_arg
22594370aa4aSLai Jiangshan }
22604370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
22614370aa4aSLai Jiangshan 
22624370aa4aSLai Jiangshan /**
22634370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
22644370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
22654370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
22664370aa4aSLai Jiangshan  * @fmt: The format string to use
22674370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
22684370aa4aSLai Jiangshan  *
22694370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
22704370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
22714370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
22724370aa4aSLai Jiangshan  *
22734370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
22740efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
22754370aa4aSLai Jiangshan  *
22764370aa4aSLai Jiangshan  * The return value is the number of characters which would
22774370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
22784370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
22794370aa4aSLai Jiangshan  * number of characters written into @buf as return value
22804370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
22814370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
22824370aa4aSLai Jiangshan  * string is truncated.
22834370aa4aSLai Jiangshan  */
22844370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
22854370aa4aSLai Jiangshan {
2286fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
2287d4be151bSAndré Goddard Rosa 	char *str, *end;
2288d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
22894370aa4aSLai Jiangshan 
22902f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
22914370aa4aSLai Jiangshan 		return 0;
22924370aa4aSLai Jiangshan 
22934370aa4aSLai Jiangshan 	str = buf;
22944370aa4aSLai Jiangshan 	end = buf + size;
22954370aa4aSLai Jiangshan 
22964370aa4aSLai Jiangshan #define get_arg(type)							\
22974370aa4aSLai Jiangshan ({									\
22984370aa4aSLai Jiangshan 	typeof(type) value;						\
22994370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
23004370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
23014370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
23024370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
23034370aa4aSLai Jiangshan 	} else {							\
23044370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
23054370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
23064370aa4aSLai Jiangshan 	}								\
23074370aa4aSLai Jiangshan 	args += sizeof(type);						\
23084370aa4aSLai Jiangshan 	value;								\
23094370aa4aSLai Jiangshan })
23104370aa4aSLai Jiangshan 
23114370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
23124370aa4aSLai Jiangshan 	if (end < buf) {
23134370aa4aSLai Jiangshan 		end = ((void *)-1);
23144370aa4aSLai Jiangshan 		size = end - buf;
23154370aa4aSLai Jiangshan 	}
23164370aa4aSLai Jiangshan 
2317fef20d9cSFrederic Weisbecker 	while (*fmt) {
2318fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2319d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2320fef20d9cSFrederic Weisbecker 
2321fef20d9cSFrederic Weisbecker 		fmt += read;
2322fef20d9cSFrederic Weisbecker 
2323fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2324fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2325fef20d9cSFrederic Weisbecker 			int copy = read;
2326fef20d9cSFrederic Weisbecker 			if (str < end) {
2327fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2328fef20d9cSFrederic Weisbecker 					copy = end - str;
2329fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2330fef20d9cSFrederic Weisbecker 			}
2331fef20d9cSFrederic Weisbecker 			str += read;
2332fef20d9cSFrederic Weisbecker 			break;
23334370aa4aSLai Jiangshan 		}
23344370aa4aSLai Jiangshan 
2335ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2336fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
2337fef20d9cSFrederic Weisbecker 			break;
23384370aa4aSLai Jiangshan 
2339fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2340fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
2341fef20d9cSFrederic Weisbecker 			break;
23424370aa4aSLai Jiangshan 
2343d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2344d4be151bSAndré Goddard Rosa 			char c;
2345d4be151bSAndré Goddard Rosa 
2346fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2347fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
23484370aa4aSLai Jiangshan 					if (str < end)
23494370aa4aSLai Jiangshan 						*str = ' ';
23504370aa4aSLai Jiangshan 					++str;
23514370aa4aSLai Jiangshan 				}
23524370aa4aSLai Jiangshan 			}
23534370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
23544370aa4aSLai Jiangshan 			if (str < end)
23554370aa4aSLai Jiangshan 				*str = c;
23564370aa4aSLai Jiangshan 			++str;
2357fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
23584370aa4aSLai Jiangshan 				if (str < end)
23594370aa4aSLai Jiangshan 					*str = ' ';
23604370aa4aSLai Jiangshan 				++str;
23614370aa4aSLai Jiangshan 			}
2362fef20d9cSFrederic Weisbecker 			break;
2363d4be151bSAndré Goddard Rosa 		}
23644370aa4aSLai Jiangshan 
2365fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
23664370aa4aSLai Jiangshan 			const char *str_arg = args;
2367d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
2368fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
2369fef20d9cSFrederic Weisbecker 			break;
23704370aa4aSLai Jiangshan 		}
23714370aa4aSLai Jiangshan 
2372fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2373ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, get_arg(void *), spec);
2374fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
23754370aa4aSLai Jiangshan 				fmt++;
2376fef20d9cSFrederic Weisbecker 			break;
23774370aa4aSLai Jiangshan 
2378fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2379fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
23804370aa4aSLai Jiangshan 			if (str < end)
23814370aa4aSLai Jiangshan 				*str = '%';
23824370aa4aSLai Jiangshan 			++str;
2383fef20d9cSFrederic Weisbecker 			break;
2384fef20d9cSFrederic Weisbecker 
2385d4be151bSAndré Goddard Rosa 		default: {
2386d4be151bSAndré Goddard Rosa 			unsigned long long num;
2387d4be151bSAndré Goddard Rosa 
2388fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2389fef20d9cSFrederic Weisbecker 
2390fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
23914370aa4aSLai Jiangshan 				num = get_arg(long long);
2392fef20d9cSFrederic Weisbecker 				break;
2393fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2394fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2395fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
2396fef20d9cSFrederic Weisbecker 				break;
2397fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
23984370aa4aSLai Jiangshan 				num = get_arg(size_t);
2399fef20d9cSFrederic Weisbecker 				break;
2400fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
24014370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
2402fef20d9cSFrederic Weisbecker 				break;
2403a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2404a4e94ef0SZhaolei 				num = get_arg(unsigned char);
2405a4e94ef0SZhaolei 				break;
2406a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2407a4e94ef0SZhaolei 				num = get_arg(signed char);
2408a4e94ef0SZhaolei 				break;
2409fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2410fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
2411fef20d9cSFrederic Weisbecker 				break;
2412fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2413fef20d9cSFrederic Weisbecker 				num = get_arg(short);
2414fef20d9cSFrederic Weisbecker 				break;
2415fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
24164370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
2417fef20d9cSFrederic Weisbecker 				break;
2418fef20d9cSFrederic Weisbecker 			default:
2419fef20d9cSFrederic Weisbecker 				num = get_arg(int);
24204370aa4aSLai Jiangshan 			}
2421fef20d9cSFrederic Weisbecker 
2422fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
2423d4be151bSAndré Goddard Rosa 		} /* default: */
2424d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
2425d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
2426fef20d9cSFrederic Weisbecker 
24274370aa4aSLai Jiangshan 	if (size > 0) {
24284370aa4aSLai Jiangshan 		if (str < end)
24294370aa4aSLai Jiangshan 			*str = '\0';
24304370aa4aSLai Jiangshan 		else
24314370aa4aSLai Jiangshan 			end[-1] = '\0';
24324370aa4aSLai Jiangshan 	}
2433fef20d9cSFrederic Weisbecker 
24344370aa4aSLai Jiangshan #undef get_arg
24354370aa4aSLai Jiangshan 
24364370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
24374370aa4aSLai Jiangshan 	return str - buf;
24384370aa4aSLai Jiangshan }
24394370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
24404370aa4aSLai Jiangshan 
24414370aa4aSLai Jiangshan /**
24424370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
24434370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
24444370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
24454370aa4aSLai Jiangshan  * @fmt: The format string to use
24464370aa4aSLai Jiangshan  * @...: Arguments for the format string
24474370aa4aSLai Jiangshan  *
24484370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
24494370aa4aSLai Jiangshan  * into @bin_buf.
24504370aa4aSLai Jiangshan  */
24514370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
24524370aa4aSLai Jiangshan {
24534370aa4aSLai Jiangshan 	va_list args;
24544370aa4aSLai Jiangshan 	int ret;
24554370aa4aSLai Jiangshan 
24564370aa4aSLai Jiangshan 	va_start(args, fmt);
24574370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
24584370aa4aSLai Jiangshan 	va_end(args);
24597b9186f5SAndré Goddard Rosa 
24604370aa4aSLai Jiangshan 	return ret;
24614370aa4aSLai Jiangshan }
24624370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
24634370aa4aSLai Jiangshan 
24644370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
24654370aa4aSLai Jiangshan 
24661da177e4SLinus Torvalds /**
24671da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
24681da177e4SLinus Torvalds  * @buf:	input buffer
24691da177e4SLinus Torvalds  * @fmt:	format of buffer
24701da177e4SLinus Torvalds  * @args:	arguments
24711da177e4SLinus Torvalds  */
24721da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
24731da177e4SLinus Torvalds {
24741da177e4SLinus Torvalds 	const char *str = buf;
24751da177e4SLinus Torvalds 	char *next;
24761da177e4SLinus Torvalds 	char digit;
24771da177e4SLinus Torvalds 	int num = 0;
2478ef0658f3SJoe Perches 	u8 qualifier;
247953809751SJan Beulich 	unsigned int base;
248053809751SJan Beulich 	union {
248153809751SJan Beulich 		long long s;
248253809751SJan Beulich 		unsigned long long u;
248353809751SJan Beulich 	} val;
2484ef0658f3SJoe Perches 	s16 field_width;
2485d4be151bSAndré Goddard Rosa 	bool is_sign;
24861da177e4SLinus Torvalds 
2487da99075cSJan Beulich 	while (*fmt) {
24881da177e4SLinus Torvalds 		/* skip any white space in format */
24891da177e4SLinus Torvalds 		/* white space in format matchs any amount of
24901da177e4SLinus Torvalds 		 * white space, including none, in the input.
24911da177e4SLinus Torvalds 		 */
24921da177e4SLinus Torvalds 		if (isspace(*fmt)) {
2493e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
2494e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
24951da177e4SLinus Torvalds 		}
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
24981da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
24991da177e4SLinus Torvalds 			if (*fmt++ != *str++)
25001da177e4SLinus Torvalds 				break;
25011da177e4SLinus Torvalds 			continue;
25021da177e4SLinus Torvalds 		}
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds 		if (!*fmt)
25051da177e4SLinus Torvalds 			break;
25061da177e4SLinus Torvalds 		++fmt;
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds 		/* skip this conversion.
25091da177e4SLinus Torvalds 		 * advance both strings to next white space
25101da177e4SLinus Torvalds 		 */
25111da177e4SLinus Torvalds 		if (*fmt == '*') {
2512da99075cSJan Beulich 			if (!*str)
2513da99075cSJan Beulich 				break;
25148fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
25151da177e4SLinus Torvalds 				fmt++;
25161da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
25171da177e4SLinus Torvalds 				str++;
25181da177e4SLinus Torvalds 			continue;
25191da177e4SLinus Torvalds 		}
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 		/* get field width */
25221da177e4SLinus Torvalds 		field_width = -1;
252353809751SJan Beulich 		if (isdigit(*fmt)) {
25241da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
252553809751SJan Beulich 			if (field_width <= 0)
252653809751SJan Beulich 				break;
252753809751SJan Beulich 		}
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds 		/* get conversion qualifier */
25301da177e4SLinus Torvalds 		qualifier = -1;
253175fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
253275fb8f26SAndy Shevchenko 		    _tolower(*fmt) == 'z') {
25331da177e4SLinus Torvalds 			qualifier = *fmt++;
25341da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
25351da177e4SLinus Torvalds 				if (qualifier == 'h') {
25361da177e4SLinus Torvalds 					qualifier = 'H';
25371da177e4SLinus Torvalds 					fmt++;
25381da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
25391da177e4SLinus Torvalds 					qualifier = 'L';
25401da177e4SLinus Torvalds 					fmt++;
25411da177e4SLinus Torvalds 				}
25421da177e4SLinus Torvalds 			}
25431da177e4SLinus Torvalds 		}
25441da177e4SLinus Torvalds 
2545da99075cSJan Beulich 		if (!*fmt)
2546da99075cSJan Beulich 			break;
2547da99075cSJan Beulich 
2548da99075cSJan Beulich 		if (*fmt == 'n') {
2549da99075cSJan Beulich 			/* return number of characters read so far */
2550da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
2551da99075cSJan Beulich 			++fmt;
2552da99075cSJan Beulich 			continue;
2553da99075cSJan Beulich 		}
2554da99075cSJan Beulich 
2555da99075cSJan Beulich 		if (!*str)
25561da177e4SLinus Torvalds 			break;
25571da177e4SLinus Torvalds 
2558d4be151bSAndré Goddard Rosa 		base = 10;
25593f623ebaSFabian Frederick 		is_sign = false;
2560d4be151bSAndré Goddard Rosa 
25611da177e4SLinus Torvalds 		switch (*fmt++) {
25621da177e4SLinus Torvalds 		case 'c':
25631da177e4SLinus Torvalds 		{
25641da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
25651da177e4SLinus Torvalds 			if (field_width == -1)
25661da177e4SLinus Torvalds 				field_width = 1;
25671da177e4SLinus Torvalds 			do {
25681da177e4SLinus Torvalds 				*s++ = *str++;
25691da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
25701da177e4SLinus Torvalds 			num++;
25711da177e4SLinus Torvalds 		}
25721da177e4SLinus Torvalds 		continue;
25731da177e4SLinus Torvalds 		case 's':
25741da177e4SLinus Torvalds 		{
25751da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
25761da177e4SLinus Torvalds 			if (field_width == -1)
25774be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
25781da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
2579e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds 			/* now copy until next white space */
25827b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
25831da177e4SLinus Torvalds 				*s++ = *str++;
25841da177e4SLinus Torvalds 			*s = '\0';
25851da177e4SLinus Torvalds 			num++;
25861da177e4SLinus Torvalds 		}
25871da177e4SLinus Torvalds 		continue;
25881da177e4SLinus Torvalds 		case 'o':
25891da177e4SLinus Torvalds 			base = 8;
25901da177e4SLinus Torvalds 			break;
25911da177e4SLinus Torvalds 		case 'x':
25921da177e4SLinus Torvalds 		case 'X':
25931da177e4SLinus Torvalds 			base = 16;
25941da177e4SLinus Torvalds 			break;
25951da177e4SLinus Torvalds 		case 'i':
25961da177e4SLinus Torvalds 			base = 0;
25971da177e4SLinus Torvalds 		case 'd':
25983f623ebaSFabian Frederick 			is_sign = true;
25991da177e4SLinus Torvalds 		case 'u':
26001da177e4SLinus Torvalds 			break;
26011da177e4SLinus Torvalds 		case '%':
26021da177e4SLinus Torvalds 			/* looking for '%' in str */
26031da177e4SLinus Torvalds 			if (*str++ != '%')
26041da177e4SLinus Torvalds 				return num;
26051da177e4SLinus Torvalds 			continue;
26061da177e4SLinus Torvalds 		default:
26071da177e4SLinus Torvalds 			/* invalid format; stop here */
26081da177e4SLinus Torvalds 			return num;
26091da177e4SLinus Torvalds 		}
26101da177e4SLinus Torvalds 
26111da177e4SLinus Torvalds 		/* have some sort of integer conversion.
26121da177e4SLinus Torvalds 		 * first, skip white space in buffer.
26131da177e4SLinus Torvalds 		 */
2614e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds 		digit = *str;
26171da177e4SLinus Torvalds 		if (is_sign && digit == '-')
26181da177e4SLinus Torvalds 			digit = *(str + 1);
26191da177e4SLinus Torvalds 
26201da177e4SLinus Torvalds 		if (!digit
26211da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
26221da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
26231da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
26241da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
26251da177e4SLinus Torvalds 			break;
26261da177e4SLinus Torvalds 
262753809751SJan Beulich 		if (is_sign)
262853809751SJan Beulich 			val.s = qualifier != 'L' ?
262953809751SJan Beulich 				simple_strtol(str, &next, base) :
263053809751SJan Beulich 				simple_strtoll(str, &next, base);
263153809751SJan Beulich 		else
263253809751SJan Beulich 			val.u = qualifier != 'L' ?
263353809751SJan Beulich 				simple_strtoul(str, &next, base) :
263453809751SJan Beulich 				simple_strtoull(str, &next, base);
263553809751SJan Beulich 
263653809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
263753809751SJan Beulich 			if (base == 0)
263853809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
263953809751SJan Beulich 			while (next - str > field_width) {
264053809751SJan Beulich 				if (is_sign)
264153809751SJan Beulich 					val.s = div_s64(val.s, base);
264253809751SJan Beulich 				else
264353809751SJan Beulich 					val.u = div_u64(val.u, base);
264453809751SJan Beulich 				--next;
264553809751SJan Beulich 			}
264653809751SJan Beulich 		}
264753809751SJan Beulich 
26481da177e4SLinus Torvalds 		switch (qualifier) {
26491da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
265053809751SJan Beulich 			if (is_sign)
265153809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
265253809751SJan Beulich 			else
265353809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
26541da177e4SLinus Torvalds 			break;
26551da177e4SLinus Torvalds 		case 'h':
265653809751SJan Beulich 			if (is_sign)
265753809751SJan Beulich 				*va_arg(args, short *) = val.s;
265853809751SJan Beulich 			else
265953809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
26601da177e4SLinus Torvalds 			break;
26611da177e4SLinus Torvalds 		case 'l':
266253809751SJan Beulich 			if (is_sign)
266353809751SJan Beulich 				*va_arg(args, long *) = val.s;
266453809751SJan Beulich 			else
266553809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
26661da177e4SLinus Torvalds 			break;
26671da177e4SLinus Torvalds 		case 'L':
266853809751SJan Beulich 			if (is_sign)
266953809751SJan Beulich 				*va_arg(args, long long *) = val.s;
267053809751SJan Beulich 			else
267153809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
26721da177e4SLinus Torvalds 			break;
26731da177e4SLinus Torvalds 		case 'Z':
26741da177e4SLinus Torvalds 		case 'z':
267553809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
26761da177e4SLinus Torvalds 			break;
26771da177e4SLinus Torvalds 		default:
267853809751SJan Beulich 			if (is_sign)
267953809751SJan Beulich 				*va_arg(args, int *) = val.s;
268053809751SJan Beulich 			else
268153809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
26821da177e4SLinus Torvalds 			break;
26831da177e4SLinus Torvalds 		}
26841da177e4SLinus Torvalds 		num++;
26851da177e4SLinus Torvalds 
26861da177e4SLinus Torvalds 		if (!next)
26871da177e4SLinus Torvalds 			break;
26881da177e4SLinus Torvalds 		str = next;
26891da177e4SLinus Torvalds 	}
2690c6b40d16SJohannes Berg 
26911da177e4SLinus Torvalds 	return num;
26921da177e4SLinus Torvalds }
26931da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
26941da177e4SLinus Torvalds 
26951da177e4SLinus Torvalds /**
26961da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
26971da177e4SLinus Torvalds  * @buf:	input buffer
26981da177e4SLinus Torvalds  * @fmt:	formatting of buffer
26991da177e4SLinus Torvalds  * @...:	resulting arguments
27001da177e4SLinus Torvalds  */
27011da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
27021da177e4SLinus Torvalds {
27031da177e4SLinus Torvalds 	va_list args;
27041da177e4SLinus Torvalds 	int i;
27051da177e4SLinus Torvalds 
27061da177e4SLinus Torvalds 	va_start(args, fmt);
27071da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
27081da177e4SLinus Torvalds 	va_end(args);
27097b9186f5SAndré Goddard Rosa 
27101da177e4SLinus Torvalds 	return i;
27111da177e4SLinus Torvalds }
27121da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2713