xref: /openbmc/linux/lib/vsprintf.c (revision 9c98f2359600386efd1c6adfabc5a04118a79798)
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() */
361da177e4SLinus Torvalds 
3771dca95dSAndy Shevchenko #include <linux/string_helpers.h>
381dff46d6SAlexey Dobriyan #include "kstrtox.h"
39aa46a63eSHarvey Harrison 
401da177e4SLinus Torvalds /**
411da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
421da177e4SLinus Torvalds  * @cp: The start of the string
431da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
441da177e4SLinus Torvalds  * @base: The number base to use
45462e4711SEldad Zack  *
46462e4711SEldad Zack  * This function is obsolete. Please use kstrtoull instead.
471da177e4SLinus Torvalds  */
481da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
491da177e4SLinus Torvalds {
501dff46d6SAlexey Dobriyan 	unsigned long long result;
511dff46d6SAlexey Dobriyan 	unsigned int rv;
521da177e4SLinus Torvalds 
531dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
541dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
551dff46d6SAlexey Dobriyan 	/* FIXME */
561dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
57aa46a63eSHarvey Harrison 
581da177e4SLinus Torvalds 	if (endp)
591da177e4SLinus Torvalds 		*endp = (char *)cp;
607b9186f5SAndré Goddard Rosa 
611da177e4SLinus Torvalds 	return result;
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /**
66922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
67922ac25cSAndré Goddard Rosa  * @cp: The start of the string
68922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
69922ac25cSAndré Goddard Rosa  * @base: The number base to use
70462e4711SEldad Zack  *
71462e4711SEldad Zack  * This function is obsolete. Please use kstrtoul instead.
72922ac25cSAndré Goddard Rosa  */
73922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
74922ac25cSAndré Goddard Rosa {
75922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
76922ac25cSAndré Goddard Rosa }
77922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
78922ac25cSAndré Goddard Rosa 
79922ac25cSAndré Goddard Rosa /**
80922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
81922ac25cSAndré Goddard Rosa  * @cp: The start of the string
82922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
83922ac25cSAndré Goddard Rosa  * @base: The number base to use
84462e4711SEldad Zack  *
85462e4711SEldad Zack  * This function is obsolete. Please use kstrtol instead.
86922ac25cSAndré Goddard Rosa  */
87922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
88922ac25cSAndré Goddard Rosa {
89922ac25cSAndré Goddard Rosa 	if (*cp == '-')
90922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
91922ac25cSAndré Goddard Rosa 
92922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
93922ac25cSAndré Goddard Rosa }
94922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
95922ac25cSAndré Goddard Rosa 
96922ac25cSAndré Goddard Rosa /**
971da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
981da177e4SLinus Torvalds  * @cp: The start of the string
991da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1001da177e4SLinus Torvalds  * @base: The number base to use
101462e4711SEldad Zack  *
102462e4711SEldad Zack  * This function is obsolete. Please use kstrtoll instead.
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	if (*cp == '-')
1071da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1087b9186f5SAndré Goddard Rosa 
1091da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1101da177e4SLinus Torvalds }
11198d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1121da177e4SLinus Torvalds 
113cf3b429bSJoe Perches static noinline_for_stack
114cf3b429bSJoe Perches int skip_atoi(const char **s)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	int i = 0;
1171da177e4SLinus Torvalds 
11843e5b666SRasmus Villemoes 	do {
1191da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
12043e5b666SRasmus Villemoes 	} while (isdigit(**s));
1217b9186f5SAndré Goddard Rosa 
1221da177e4SLinus Torvalds 	return i;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1254277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used
1264277eeddSDenis Vlasenko  * for /proc and /sys data. This directly impacts e.g. top performance
1274277eeddSDenis Vlasenko  * with many processes running. We optimize it for speed
128133fd9f5SDenys Vlasenko  * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
129133fd9f5SDenys Vlasenko  * (with permission from the author, Douglas W. Jones).
130133fd9f5SDenys Vlasenko  */
1314277eeddSDenis Vlasenko 
132133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
133133fd9f5SDenys Vlasenko /* Formats correctly any integer in [0, 999999999] */
134cf3b429bSJoe Perches static noinline_for_stack
135133fd9f5SDenys Vlasenko char *put_dec_full9(char *buf, unsigned q)
1364277eeddSDenis Vlasenko {
137133fd9f5SDenys Vlasenko 	unsigned r;
1384277eeddSDenis Vlasenko 
1397b9186f5SAndré Goddard Rosa 	/*
1407b9186f5SAndré Goddard Rosa 	 * Possible ways to approx. divide by 10
141133fd9f5SDenys Vlasenko 	 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
142133fd9f5SDenys Vlasenko 	 * (x * 0xcccd) >> 19     x <      81920 (x < 262149 when 64-bit mul)
143133fd9f5SDenys Vlasenko 	 * (x * 0x6667) >> 18     x <      43699
144133fd9f5SDenys Vlasenko 	 * (x * 0x3334) >> 17     x <      16389
145133fd9f5SDenys Vlasenko 	 * (x * 0x199a) >> 16     x <      16389
146133fd9f5SDenys Vlasenko 	 * (x * 0x0ccd) >> 15     x <      16389
147133fd9f5SDenys Vlasenko 	 * (x * 0x0667) >> 14     x <       2739
148133fd9f5SDenys Vlasenko 	 * (x * 0x0334) >> 13     x <       1029
149133fd9f5SDenys Vlasenko 	 * (x * 0x019a) >> 12     x <       1029
150133fd9f5SDenys Vlasenko 	 * (x * 0x00cd) >> 11     x <       1029 shorter code than * 0x67 (on i386)
151133fd9f5SDenys Vlasenko 	 * (x * 0x0067) >> 10     x <        179
152133fd9f5SDenys Vlasenko 	 * (x * 0x0034) >>  9     x <         69 same
153133fd9f5SDenys Vlasenko 	 * (x * 0x001a) >>  8     x <         69 same
154133fd9f5SDenys Vlasenko 	 * (x * 0x000d) >>  7     x <         69 same, shortest code (on i386)
155133fd9f5SDenys Vlasenko 	 * (x * 0x0007) >>  6     x <         19
156133fd9f5SDenys Vlasenko 	 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
1577b9186f5SAndré Goddard Rosa 	 */
158133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
159133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 1 */
160133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
161133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 2 */
162133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
163133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 3 */
164133fd9f5SDenys Vlasenko 	q      = (r * (uint64_t)0x1999999a) >> 32;
165133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 4 */
166133fd9f5SDenys Vlasenko 	r      = (q * (uint64_t)0x1999999a) >> 32;
167133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r) + '0'; /* 5 */
168133fd9f5SDenys Vlasenko 	/* Now value is under 10000, can avoid 64-bit multiply */
169133fd9f5SDenys Vlasenko 	q      = (r * 0x199a) >> 16;
170133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q)  + '0'; /* 6 */
171133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
172133fd9f5SDenys Vlasenko 	*buf++ = (q - 10 * r)  + '0'; /* 7 */
173133fd9f5SDenys Vlasenko 	q      = (r * 0xcd) >> 11;
174133fd9f5SDenys Vlasenko 	*buf++ = (r - 10 * q) + '0'; /* 8 */
175133fd9f5SDenys Vlasenko 	*buf++ = q + '0'; /* 9 */
176133fd9f5SDenys Vlasenko 	return buf;
177133fd9f5SDenys Vlasenko }
178133fd9f5SDenys Vlasenko #endif
1794277eeddSDenis Vlasenko 
180133fd9f5SDenys Vlasenko /* Similar to above but do not pad with zeros.
181133fd9f5SDenys Vlasenko  * Code can be easily arranged to print 9 digits too, but our callers
182133fd9f5SDenys Vlasenko  * always call put_dec_full9() instead when the number has 9 decimal digits.
183133fd9f5SDenys Vlasenko  */
184133fd9f5SDenys Vlasenko static noinline_for_stack
185133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
186133fd9f5SDenys Vlasenko {
187133fd9f5SDenys Vlasenko 	unsigned q;
1884277eeddSDenis Vlasenko 
189133fd9f5SDenys Vlasenko 	/* Copy of previous function's body with added early returns */
190cb239d0aSGeorge Spelvin 	while (r >= 10000) {
191cb239d0aSGeorge Spelvin 		q = r + '0';
192cb239d0aSGeorge Spelvin 		r  = (r * (uint64_t)0x1999999a) >> 32;
193cb239d0aSGeorge Spelvin 		*buf++ = q - 10*r;
194cb239d0aSGeorge Spelvin 	}
195cb239d0aSGeorge Spelvin 
196f4000516SGeorge Spelvin 	q      = (r * 0x199a) >> 16;	/* r <= 9999 */
197f4000516SGeorge Spelvin 	*buf++ = (r - 10 * q)  + '0';
198133fd9f5SDenys Vlasenko 	if (q == 0)
199133fd9f5SDenys Vlasenko 		return buf;
200f4000516SGeorge Spelvin 	r      = (q * 0xcd) >> 11;	/* q <= 999 */
201f4000516SGeorge Spelvin 	*buf++ = (q - 10 * r)  + '0';
202133fd9f5SDenys Vlasenko 	if (r == 0)
203133fd9f5SDenys Vlasenko 		return buf;
204f4000516SGeorge Spelvin 	q      = (r * 0xcd) >> 11;	/* r <= 99 */
205f4000516SGeorge Spelvin 	*buf++ = (r - 10 * q) + '0';
206133fd9f5SDenys Vlasenko 	if (q == 0)
207133fd9f5SDenys Vlasenko 		return buf;
208f4000516SGeorge Spelvin 	*buf++ = q + '0';		 /* q <= 9 */
209133fd9f5SDenys Vlasenko 	return buf;
210133fd9f5SDenys Vlasenko }
211133fd9f5SDenys Vlasenko 
212133fd9f5SDenys Vlasenko /* There are two algorithms to print larger numbers.
213133fd9f5SDenys Vlasenko  * One is generic: divide by 1000000000 and repeatedly print
214133fd9f5SDenys Vlasenko  * groups of (up to) 9 digits. It's conceptually simple,
215133fd9f5SDenys Vlasenko  * but requires a (unsigned long long) / 1000000000 division.
216133fd9f5SDenys Vlasenko  *
217133fd9f5SDenys Vlasenko  * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
218133fd9f5SDenys Vlasenko  * manipulates them cleverly and generates groups of 4 decimal digits.
219133fd9f5SDenys Vlasenko  * It so happens that it does NOT require long long division.
220133fd9f5SDenys Vlasenko  *
221133fd9f5SDenys Vlasenko  * If long is > 32 bits, division of 64-bit values is relatively easy,
222133fd9f5SDenys Vlasenko  * and we will use the first algorithm.
223133fd9f5SDenys Vlasenko  * If long long is > 64 bits (strange architecture with VERY large long long),
224133fd9f5SDenys Vlasenko  * second algorithm can't be used, and we again use the first one.
225133fd9f5SDenys Vlasenko  *
226133fd9f5SDenys Vlasenko  * Else (if long is 32 bits and long long is 64 bits) we use second one.
227133fd9f5SDenys Vlasenko  */
228133fd9f5SDenys Vlasenko 
229133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
230133fd9f5SDenys Vlasenko 
231133fd9f5SDenys Vlasenko /* First algorithm: generic */
232133fd9f5SDenys Vlasenko 
233133fd9f5SDenys Vlasenko static
234133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
235133fd9f5SDenys Vlasenko {
236133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000) {
237133fd9f5SDenys Vlasenko 		while (n >= 1000*1000*1000)
238133fd9f5SDenys Vlasenko 			buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
239133fd9f5SDenys Vlasenko 		if (n >= 100*1000*1000)
240133fd9f5SDenys Vlasenko 			return put_dec_full9(buf, n);
241133fd9f5SDenys Vlasenko 	}
242133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
243133fd9f5SDenys Vlasenko }
244133fd9f5SDenys Vlasenko 
245133fd9f5SDenys Vlasenko #else
246133fd9f5SDenys Vlasenko 
247133fd9f5SDenys Vlasenko /* Second algorithm: valid only for 64-bit long longs */
248133fd9f5SDenys Vlasenko 
249e49317d4SGeorge Spelvin /* See comment in put_dec_full9 for choice of constants */
250133fd9f5SDenys Vlasenko static noinline_for_stack
2512359172aSGeorge Spelvin void put_dec_full4(char *buf, unsigned q)
252133fd9f5SDenys Vlasenko {
253133fd9f5SDenys Vlasenko 	unsigned r;
254e49317d4SGeorge Spelvin 	r      = (q * 0xccd) >> 15;
2552359172aSGeorge Spelvin 	buf[0] = (q - 10 * r) + '0';
256e49317d4SGeorge Spelvin 	q      = (r * 0xcd) >> 11;
2572359172aSGeorge Spelvin 	buf[1] = (r - 10 * q)  + '0';
258133fd9f5SDenys Vlasenko 	r      = (q * 0xcd) >> 11;
2592359172aSGeorge Spelvin 	buf[2] = (q - 10 * r)  + '0';
2602359172aSGeorge Spelvin 	buf[3] = r + '0';
2612359172aSGeorge Spelvin }
2622359172aSGeorge Spelvin 
2632359172aSGeorge Spelvin /*
2642359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2652359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2662359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2672359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
2682359172aSGeorge Spelvin  * (d1 in the put_dec code, assuming n is all-ones).
2692359172aSGeorge Spelvin  */
2702359172aSGeorge Spelvin static
2712359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2722359172aSGeorge Spelvin {
2732359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2742359172aSGeorge Spelvin 
2752359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2762359172aSGeorge Spelvin         return q;
277133fd9f5SDenys Vlasenko }
278133fd9f5SDenys Vlasenko 
279133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
280133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
281133fd9f5SDenys Vlasenko  * (with permission from the author).
282133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
283133fd9f5SDenys Vlasenko  */
284133fd9f5SDenys Vlasenko static
285133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
286133fd9f5SDenys Vlasenko {
287133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
288133fd9f5SDenys Vlasenko 
289133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
290133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
291133fd9f5SDenys Vlasenko 
292133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
293133fd9f5SDenys Vlasenko 	h   = (n >> 32);
294133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
295133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
296133fd9f5SDenys Vlasenko 
297133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
2982359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
299133fd9f5SDenys Vlasenko 
3002359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
3012359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
302133fd9f5SDenys Vlasenko 
3032359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
3042359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
305133fd9f5SDenys Vlasenko 
3062359172aSGeorge Spelvin 	q += 281 * d3;
3072359172aSGeorge Spelvin 	buf += 12;
3082359172aSGeorge Spelvin 	if (q)
3092359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3102359172aSGeorge Spelvin 	else while (buf[-1] == '0')
311133fd9f5SDenys Vlasenko 		--buf;
3127b9186f5SAndré Goddard Rosa 
3134277eeddSDenis Vlasenko 	return buf;
3144277eeddSDenis Vlasenko }
315133fd9f5SDenys Vlasenko 
316133fd9f5SDenys Vlasenko #endif
3174277eeddSDenis Vlasenko 
3181ac101a5SKAMEZAWA Hiroyuki /*
3191ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3201ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3211ac101a5SKAMEZAWA Hiroyuki  *
3221ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3231ac101a5SKAMEZAWA Hiroyuki  */
3241ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num)
3251ac101a5SKAMEZAWA Hiroyuki {
326133fd9f5SDenys Vlasenko 	char tmp[sizeof(num) * 3];
3271ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3281ac101a5SKAMEZAWA Hiroyuki 
329133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
330133fd9f5SDenys Vlasenko 	if (num <= 9) {
331133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
332133fd9f5SDenys Vlasenko 		len = 1;
333133fd9f5SDenys Vlasenko 	} else {
3341ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
335133fd9f5SDenys Vlasenko 	}
3361ac101a5SKAMEZAWA Hiroyuki 
3371ac101a5SKAMEZAWA Hiroyuki 	if (len > size)
3381ac101a5SKAMEZAWA Hiroyuki 		return 0;
3391ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
3401ac101a5SKAMEZAWA Hiroyuki 		buf[idx] = tmp[len - idx - 1];
3411ac101a5SKAMEZAWA Hiroyuki 	return len;
3421ac101a5SKAMEZAWA Hiroyuki }
3431ac101a5SKAMEZAWA Hiroyuki 
34451be17dfSRasmus Villemoes #define SIGN	1		/* unsigned/signed, must be 1 */
345d1c1b121SRasmus Villemoes #define LEFT	2		/* left justified */
3461da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3471da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
348d1c1b121SRasmus Villemoes #define ZEROPAD	16		/* pad with zero, must be 16 == '0' - ' ' */
349b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
350b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3511da177e4SLinus Torvalds 
352fef20d9cSFrederic Weisbecker enum format_type {
353fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
354ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
355fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
356fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
357fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
358fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
359fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
360fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
361fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
362fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
363fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
364a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
365a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
366fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
367fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
368fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
369fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
370fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
371fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
372fef20d9cSFrederic Weisbecker };
373fef20d9cSFrederic Weisbecker 
374fef20d9cSFrederic Weisbecker struct printf_spec {
3754e310fdaSJoe Perches 	u8	type;		/* format_type enum */
376ef0658f3SJoe Perches 	u8	flags;		/* flags to number() */
3774e310fdaSJoe Perches 	u8	base;		/* number base, 8, 10 or 16 only */
3784e310fdaSJoe Perches 	u8	qualifier;	/* number qualifier, one of 'hHlLtzZ' */
3794e310fdaSJoe Perches 	s16	field_width;	/* width of output field */
3804e310fdaSJoe Perches 	s16	precision;	/* # of digits/chars */
381fef20d9cSFrederic Weisbecker };
382fef20d9cSFrederic Weisbecker 
383cf3b429bSJoe Perches static noinline_for_stack
384cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
385fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
3861da177e4SLinus Torvalds {
387e26c12c7SRasmus Villemoes 	char tmp[3 * sizeof(num)];
3889b706aeeSDenys Vlasenko 	char sign;
3899b706aeeSDenys Vlasenko 	char locase;
390fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
3911da177e4SLinus Torvalds 	int i;
3927c203422SPierre Carrier 	bool is_zero = num == 0LL;
3931da177e4SLinus Torvalds 
3949b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
3959b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
396fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
397fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
398fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
3991da177e4SLinus Torvalds 	sign = 0;
400fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4011da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4021da177e4SLinus Torvalds 			sign = '-';
4031da177e4SLinus Torvalds 			num = -(signed long long)num;
404fef20d9cSFrederic Weisbecker 			spec.field_width--;
405fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4061da177e4SLinus Torvalds 			sign = '+';
407fef20d9cSFrederic Weisbecker 			spec.field_width--;
408fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4091da177e4SLinus Torvalds 			sign = ' ';
410fef20d9cSFrederic Weisbecker 			spec.field_width--;
4111da177e4SLinus Torvalds 		}
4121da177e4SLinus Torvalds 	}
413b39a7340SDenis Vlasenko 	if (need_pfx) {
414fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4157c203422SPierre Carrier 			spec.field_width -= 2;
4167c203422SPierre Carrier 		else if (!is_zero)
417fef20d9cSFrederic Weisbecker 			spec.field_width--;
4181da177e4SLinus Torvalds 	}
419b39a7340SDenis Vlasenko 
420b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4211da177e4SLinus Torvalds 	i = 0;
422133fd9f5SDenys Vlasenko 	if (num < spec.base)
4233ea8d440SRasmus Villemoes 		tmp[i++] = hex_asc_upper[num] | locase;
424fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
425fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
426b39a7340SDenis Vlasenko 		int shift = 3;
4277b9186f5SAndré Goddard Rosa 
4287b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4297b9186f5SAndré Goddard Rosa 			shift = 4;
430b39a7340SDenis Vlasenko 		do {
4313ea8d440SRasmus Villemoes 			tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
432b39a7340SDenis Vlasenko 			num >>= shift;
433b39a7340SDenis Vlasenko 		} while (num);
4344277eeddSDenis Vlasenko 	} else { /* base 10 */
4354277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4364277eeddSDenis Vlasenko 	}
437b39a7340SDenis Vlasenko 
438b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
439fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
440fef20d9cSFrederic Weisbecker 		spec.precision = i;
441b39a7340SDenis Vlasenko 	/* leading space padding */
442fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
44351be17dfSRasmus Villemoes 	if (!(spec.flags & (ZEROPAD | LEFT))) {
444fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
445f796937aSJeremy Fitzhardinge 			if (buf < end)
4461da177e4SLinus Torvalds 				*buf = ' ';
4471da177e4SLinus Torvalds 			++buf;
4481da177e4SLinus Torvalds 		}
4491da177e4SLinus Torvalds 	}
450b39a7340SDenis Vlasenko 	/* sign */
4511da177e4SLinus Torvalds 	if (sign) {
452f796937aSJeremy Fitzhardinge 		if (buf < end)
4531da177e4SLinus Torvalds 			*buf = sign;
4541da177e4SLinus Torvalds 		++buf;
4551da177e4SLinus Torvalds 	}
456b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
457b39a7340SDenis Vlasenko 	if (need_pfx) {
4587c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
459f796937aSJeremy Fitzhardinge 			if (buf < end)
4601da177e4SLinus Torvalds 				*buf = '0';
4611da177e4SLinus Torvalds 			++buf;
4627c203422SPierre Carrier 		}
463fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
464f796937aSJeremy Fitzhardinge 			if (buf < end)
4659b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
4661da177e4SLinus Torvalds 			++buf;
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 	}
469b39a7340SDenis Vlasenko 	/* zero or space padding */
470fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
471d1c1b121SRasmus Villemoes 		char c = ' ' + (spec.flags & ZEROPAD);
472d1c1b121SRasmus Villemoes 		BUILD_BUG_ON(' ' + ZEROPAD != '0');
473fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
474f796937aSJeremy Fitzhardinge 			if (buf < end)
4751da177e4SLinus Torvalds 				*buf = c;
4761da177e4SLinus Torvalds 			++buf;
4771da177e4SLinus Torvalds 		}
4781da177e4SLinus Torvalds 	}
479b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
480fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
481f796937aSJeremy Fitzhardinge 		if (buf < end)
4821da177e4SLinus Torvalds 			*buf = '0';
4831da177e4SLinus Torvalds 		++buf;
4841da177e4SLinus Torvalds 	}
485b39a7340SDenis Vlasenko 	/* actual digits of result */
486b39a7340SDenis Vlasenko 	while (--i >= 0) {
487f796937aSJeremy Fitzhardinge 		if (buf < end)
4881da177e4SLinus Torvalds 			*buf = tmp[i];
4891da177e4SLinus Torvalds 		++buf;
4901da177e4SLinus Torvalds 	}
491b39a7340SDenis Vlasenko 	/* trailing space padding */
492fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
493f796937aSJeremy Fitzhardinge 		if (buf < end)
4941da177e4SLinus Torvalds 			*buf = ' ';
4951da177e4SLinus Torvalds 		++buf;
4961da177e4SLinus Torvalds 	}
4977b9186f5SAndré Goddard Rosa 
4981da177e4SLinus Torvalds 	return buf;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
501cf3b429bSJoe Perches static noinline_for_stack
502cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec)
5030f9bfa56SLinus Torvalds {
5040f9bfa56SLinus Torvalds 	int len, i;
5050f9bfa56SLinus Torvalds 
5060f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5070f4f81dcSAndré Goddard Rosa 		s = "(null)";
5080f9bfa56SLinus Torvalds 
509fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5100f9bfa56SLinus Torvalds 
511fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
512fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5130f9bfa56SLinus Torvalds 			if (buf < end)
5140f9bfa56SLinus Torvalds 				*buf = ' ';
5150f9bfa56SLinus Torvalds 			++buf;
5160f9bfa56SLinus Torvalds 		}
5170f9bfa56SLinus Torvalds 	}
5180f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5190f9bfa56SLinus Torvalds 		if (buf < end)
5200f9bfa56SLinus Torvalds 			*buf = *s;
5210f9bfa56SLinus Torvalds 		++buf; ++s;
5220f9bfa56SLinus Torvalds 	}
523fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5240f9bfa56SLinus Torvalds 		if (buf < end)
5250f9bfa56SLinus Torvalds 			*buf = ' ';
5260f9bfa56SLinus Torvalds 		++buf;
5270f9bfa56SLinus Torvalds 	}
5287b9186f5SAndré Goddard Rosa 
5290f9bfa56SLinus Torvalds 	return buf;
5300f9bfa56SLinus Torvalds }
5310f9bfa56SLinus Torvalds 
5324b6ccca7SAl Viro static void widen(char *buf, char *end, unsigned len, unsigned spaces)
5334b6ccca7SAl Viro {
5344b6ccca7SAl Viro 	size_t size;
5354b6ccca7SAl Viro 	if (buf >= end)	/* nowhere to put anything */
5364b6ccca7SAl Viro 		return;
5374b6ccca7SAl Viro 	size = end - buf;
5384b6ccca7SAl Viro 	if (size <= spaces) {
5394b6ccca7SAl Viro 		memset(buf, ' ', size);
5404b6ccca7SAl Viro 		return;
5414b6ccca7SAl Viro 	}
5424b6ccca7SAl Viro 	if (len) {
5434b6ccca7SAl Viro 		if (len > size - spaces)
5444b6ccca7SAl Viro 			len = size - spaces;
5454b6ccca7SAl Viro 		memmove(buf + spaces, buf, len);
5464b6ccca7SAl Viro 	}
5474b6ccca7SAl Viro 	memset(buf, ' ', spaces);
5484b6ccca7SAl Viro }
5494b6ccca7SAl Viro 
5504b6ccca7SAl Viro static noinline_for_stack
5514b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
5524b6ccca7SAl Viro 		  const char *fmt)
5534b6ccca7SAl Viro {
5544b6ccca7SAl Viro 	const char *array[4], *s;
5554b6ccca7SAl Viro 	const struct dentry *p;
5564b6ccca7SAl Viro 	int depth;
5574b6ccca7SAl Viro 	int i, n;
5584b6ccca7SAl Viro 
5594b6ccca7SAl Viro 	switch (fmt[1]) {
5604b6ccca7SAl Viro 		case '2': case '3': case '4':
5614b6ccca7SAl Viro 			depth = fmt[1] - '0';
5624b6ccca7SAl Viro 			break;
5634b6ccca7SAl Viro 		default:
5644b6ccca7SAl Viro 			depth = 1;
5654b6ccca7SAl Viro 	}
5664b6ccca7SAl Viro 
5674b6ccca7SAl Viro 	rcu_read_lock();
5684b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
5694b6ccca7SAl Viro 		p = ACCESS_ONCE(d->d_parent);
5704b6ccca7SAl Viro 		array[i] = ACCESS_ONCE(d->d_name.name);
5714b6ccca7SAl Viro 		if (p == d) {
5724b6ccca7SAl Viro 			if (i)
5734b6ccca7SAl Viro 				array[i] = "";
5744b6ccca7SAl Viro 			i++;
5754b6ccca7SAl Viro 			break;
5764b6ccca7SAl Viro 		}
5774b6ccca7SAl Viro 	}
5784b6ccca7SAl Viro 	s = array[--i];
5794b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
5804b6ccca7SAl Viro 		char c = *s++;
5814b6ccca7SAl Viro 		if (!c) {
5824b6ccca7SAl Viro 			if (!i)
5834b6ccca7SAl Viro 				break;
5844b6ccca7SAl Viro 			c = '/';
5854b6ccca7SAl Viro 			s = array[--i];
5864b6ccca7SAl Viro 		}
5874b6ccca7SAl Viro 		if (buf < end)
5884b6ccca7SAl Viro 			*buf = c;
5894b6ccca7SAl Viro 	}
5904b6ccca7SAl Viro 	rcu_read_unlock();
5914b6ccca7SAl Viro 	if (n < spec.field_width) {
5924b6ccca7SAl Viro 		/* we want to pad the sucker */
5934b6ccca7SAl Viro 		unsigned spaces = spec.field_width - n;
5944b6ccca7SAl Viro 		if (!(spec.flags & LEFT)) {
5954b6ccca7SAl Viro 			widen(buf - n, end, n, spaces);
5964b6ccca7SAl Viro 			return buf + spaces;
5974b6ccca7SAl Viro 		}
5984b6ccca7SAl Viro 		while (spaces--) {
5994b6ccca7SAl Viro 			if (buf < end)
6004b6ccca7SAl Viro 				*buf = ' ';
6014b6ccca7SAl Viro 			++buf;
6024b6ccca7SAl Viro 		}
6034b6ccca7SAl Viro 	}
6044b6ccca7SAl Viro 	return buf;
6054b6ccca7SAl Viro }
6064b6ccca7SAl Viro 
607cf3b429bSJoe Perches static noinline_for_stack
608cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
609b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
6100fe1ef24SLinus Torvalds {
611b0d33c2bSJoe Perches 	unsigned long value;
6120fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
6130fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
614b0d33c2bSJoe Perches #endif
615b0d33c2bSJoe Perches 
616b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
617b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
618b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
619b0d33c2bSJoe Perches 
620b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
621b0d33c2bSJoe Perches 	if (*fmt == 'B')
6220f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
623b0d33c2bSJoe Perches 	else if (*fmt != 'f' && *fmt != 's')
6240fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
6250c8b946eSFrederic Weisbecker 	else
6264796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
6277b9186f5SAndré Goddard Rosa 
628fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
6290fe1ef24SLinus Torvalds #else
630fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
631fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
632fef20d9cSFrederic Weisbecker 	spec.base = 16;
6337b9186f5SAndré Goddard Rosa 
634fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
6350fe1ef24SLinus Torvalds #endif
6360fe1ef24SLinus Torvalds }
6370fe1ef24SLinus Torvalds 
638cf3b429bSJoe Perches static noinline_for_stack
639cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
640fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
641332d2e78SLinus Torvalds {
642332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
64328405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
644332d2e78SLinus Torvalds #endif
645332d2e78SLinus Torvalds 
646332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
64728405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
648332d2e78SLinus Torvalds #endif
6494da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
650fef20d9cSFrederic Weisbecker 		.base = 16,
6514da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
652fef20d9cSFrederic Weisbecker 		.precision = -1,
653fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
654fef20d9cSFrederic Weisbecker 	};
6554da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
6564da0b66cSBjorn Helgaas 		.base = 16,
6574da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
6584da0b66cSBjorn Helgaas 		.precision = -1,
6594da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
6604da0b66cSBjorn Helgaas 	};
6610f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
6620f4050c7SBjorn Helgaas 		.base = 16,
6630f4050c7SBjorn Helgaas 		.field_width = 2,
6640f4050c7SBjorn Helgaas 		.precision = -1,
6650f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
6660f4050c7SBjorn Helgaas 	};
6674da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
668c91d3376SBjorn Helgaas 		.base = 10,
669c91d3376SBjorn Helgaas 		.precision = -1,
670c91d3376SBjorn Helgaas 		.flags = 0,
671c91d3376SBjorn Helgaas 	};
6724da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
673fd95541eSBjorn Helgaas 		.field_width = -1,
674fd95541eSBjorn Helgaas 		.precision = 10,
675fd95541eSBjorn Helgaas 		.flags = LEFT,
676fd95541eSBjorn Helgaas 	};
6774da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
678fd95541eSBjorn Helgaas 		.base = 16,
679fd95541eSBjorn Helgaas 		.precision = -1,
680fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
681fd95541eSBjorn Helgaas 	};
682c7dabef8SBjorn Helgaas 
683c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
684c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
685c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
686c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
6879d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
688c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
689c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
690c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
691c7dabef8SBjorn Helgaas 
692332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
693c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
6944da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
695332d2e78SLinus Torvalds 
696332d2e78SLinus Torvalds 	*p++ = '[';
6974da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
698fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
6994da0b66cSBjorn Helgaas 		specp = &io_spec;
7004da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
701fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
7024da0b66cSBjorn Helgaas 		specp = &mem_spec;
7034da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
704fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
7054da0b66cSBjorn Helgaas 		specp = &dec_spec;
7064da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
707fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
7084da0b66cSBjorn Helgaas 		specp = &dec_spec;
7090f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
7100f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
7110f4050c7SBjorn Helgaas 		specp = &bus_spec;
7124da0b66cSBjorn Helgaas 	} else {
713c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
7144da0b66cSBjorn Helgaas 		specp = &mem_spec;
715c7dabef8SBjorn Helgaas 		decode = 0;
716fd95541eSBjorn Helgaas 	}
717d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
718d19cb803SBjorn Helgaas 		p = string(p, pend, "size ", str_spec);
719d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
720d19cb803SBjorn Helgaas 	} else {
7214da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
722c91d3376SBjorn Helgaas 		if (res->start != res->end) {
723332d2e78SLinus Torvalds 			*p++ = '-';
7244da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
725c91d3376SBjorn Helgaas 		}
726d19cb803SBjorn Helgaas 	}
727c7dabef8SBjorn Helgaas 	if (decode) {
728fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
729fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
730fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
731fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
7329d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
7339d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
734fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
735fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
736c7dabef8SBjorn Helgaas 	} else {
737fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
738c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
739fd95541eSBjorn Helgaas 	}
740332d2e78SLinus Torvalds 	*p++ = ']';
741c7dabef8SBjorn Helgaas 	*p = '\0';
742332d2e78SLinus Torvalds 
743fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
744332d2e78SLinus Torvalds }
745332d2e78SLinus Torvalds 
746cf3b429bSJoe Perches static noinline_for_stack
74731550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
74831550a16SAndy Shevchenko 		 const char *fmt)
74931550a16SAndy Shevchenko {
750360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
75131550a16SAndy Shevchenko 				   negative value, fallback to the default */
75231550a16SAndy Shevchenko 	char separator;
75331550a16SAndy Shevchenko 
75431550a16SAndy Shevchenko 	if (spec.field_width == 0)
75531550a16SAndy Shevchenko 		/* nothing to print */
75631550a16SAndy Shevchenko 		return buf;
75731550a16SAndy Shevchenko 
75831550a16SAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
75931550a16SAndy Shevchenko 		/* NULL pointer */
76031550a16SAndy Shevchenko 		return string(buf, end, NULL, spec);
76131550a16SAndy Shevchenko 
76231550a16SAndy Shevchenko 	switch (fmt[1]) {
76331550a16SAndy Shevchenko 	case 'C':
76431550a16SAndy Shevchenko 		separator = ':';
76531550a16SAndy Shevchenko 		break;
76631550a16SAndy Shevchenko 	case 'D':
76731550a16SAndy Shevchenko 		separator = '-';
76831550a16SAndy Shevchenko 		break;
76931550a16SAndy Shevchenko 	case 'N':
77031550a16SAndy Shevchenko 		separator = 0;
77131550a16SAndy Shevchenko 		break;
77231550a16SAndy Shevchenko 	default:
77331550a16SAndy Shevchenko 		separator = ' ';
77431550a16SAndy Shevchenko 		break;
77531550a16SAndy Shevchenko 	}
77631550a16SAndy Shevchenko 
77731550a16SAndy Shevchenko 	if (spec.field_width > 0)
77831550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
77931550a16SAndy Shevchenko 
780*9c98f235SRasmus Villemoes 	for (i = 0; i < len; ++i) {
781*9c98f235SRasmus Villemoes 		if (buf < end)
782*9c98f235SRasmus Villemoes 			*buf = hex_asc_hi(addr[i]);
783*9c98f235SRasmus Villemoes 		++buf;
784*9c98f235SRasmus Villemoes 		if (buf < end)
785*9c98f235SRasmus Villemoes 			*buf = hex_asc_lo(addr[i]);
786*9c98f235SRasmus Villemoes 		++buf;
78731550a16SAndy Shevchenko 
788*9c98f235SRasmus Villemoes 		if (separator && i != len - 1) {
789*9c98f235SRasmus Villemoes 			if (buf < end)
790*9c98f235SRasmus Villemoes 				*buf = separator;
791*9c98f235SRasmus Villemoes 			++buf;
792*9c98f235SRasmus Villemoes 		}
79331550a16SAndy Shevchenko 	}
79431550a16SAndy Shevchenko 
79531550a16SAndy Shevchenko 	return buf;
79631550a16SAndy Shevchenko }
79731550a16SAndy Shevchenko 
79831550a16SAndy Shevchenko static noinline_for_stack
799dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
800dbc760bcSTejun Heo 		    struct printf_spec spec, const char *fmt)
801dbc760bcSTejun Heo {
802dbc760bcSTejun Heo 	const int CHUNKSZ = 32;
803dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
804dbc760bcSTejun Heo 	int i, chunksz;
805dbc760bcSTejun Heo 	bool first = true;
806dbc760bcSTejun Heo 
807dbc760bcSTejun Heo 	/* reused to print numbers */
808dbc760bcSTejun Heo 	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
809dbc760bcSTejun Heo 
810dbc760bcSTejun Heo 	chunksz = nr_bits & (CHUNKSZ - 1);
811dbc760bcSTejun Heo 	if (chunksz == 0)
812dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
813dbc760bcSTejun Heo 
814dbc760bcSTejun Heo 	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
815dbc760bcSTejun Heo 	for (; i >= 0; i -= CHUNKSZ) {
816dbc760bcSTejun Heo 		u32 chunkmask, val;
817dbc760bcSTejun Heo 		int word, bit;
818dbc760bcSTejun Heo 
819dbc760bcSTejun Heo 		chunkmask = ((1ULL << chunksz) - 1);
820dbc760bcSTejun Heo 		word = i / BITS_PER_LONG;
821dbc760bcSTejun Heo 		bit = i % BITS_PER_LONG;
822dbc760bcSTejun Heo 		val = (bitmap[word] >> bit) & chunkmask;
823dbc760bcSTejun Heo 
824dbc760bcSTejun Heo 		if (!first) {
825dbc760bcSTejun Heo 			if (buf < end)
826dbc760bcSTejun Heo 				*buf = ',';
827dbc760bcSTejun Heo 			buf++;
828dbc760bcSTejun Heo 		}
829dbc760bcSTejun Heo 		first = false;
830dbc760bcSTejun Heo 
831dbc760bcSTejun Heo 		spec.field_width = DIV_ROUND_UP(chunksz, 4);
832dbc760bcSTejun Heo 		buf = number(buf, end, val, spec);
833dbc760bcSTejun Heo 
834dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
835dbc760bcSTejun Heo 	}
836dbc760bcSTejun Heo 	return buf;
837dbc760bcSTejun Heo }
838dbc760bcSTejun Heo 
839dbc760bcSTejun Heo static noinline_for_stack
840dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
841dbc760bcSTejun Heo 			 struct printf_spec spec, const char *fmt)
842dbc760bcSTejun Heo {
843dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
844dbc760bcSTejun Heo 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
845dbc760bcSTejun Heo 	int cur, rbot, rtop;
846dbc760bcSTejun Heo 	bool first = true;
847dbc760bcSTejun Heo 
848dbc760bcSTejun Heo 	/* reused to print numbers */
849dbc760bcSTejun Heo 	spec = (struct printf_spec){ .base = 10 };
850dbc760bcSTejun Heo 
851dbc760bcSTejun Heo 	rbot = cur = find_first_bit(bitmap, nr_bits);
852dbc760bcSTejun Heo 	while (cur < nr_bits) {
853dbc760bcSTejun Heo 		rtop = cur;
854dbc760bcSTejun Heo 		cur = find_next_bit(bitmap, nr_bits, cur + 1);
855dbc760bcSTejun Heo 		if (cur < nr_bits && cur <= rtop + 1)
856dbc760bcSTejun Heo 			continue;
857dbc760bcSTejun Heo 
858dbc760bcSTejun Heo 		if (!first) {
859dbc760bcSTejun Heo 			if (buf < end)
860dbc760bcSTejun Heo 				*buf = ',';
861dbc760bcSTejun Heo 			buf++;
862dbc760bcSTejun Heo 		}
863dbc760bcSTejun Heo 		first = false;
864dbc760bcSTejun Heo 
865dbc760bcSTejun Heo 		buf = number(buf, end, rbot, spec);
866dbc760bcSTejun Heo 		if (rbot < rtop) {
867dbc760bcSTejun Heo 			if (buf < end)
868dbc760bcSTejun Heo 				*buf = '-';
869dbc760bcSTejun Heo 			buf++;
870dbc760bcSTejun Heo 
871dbc760bcSTejun Heo 			buf = number(buf, end, rtop, spec);
872dbc760bcSTejun Heo 		}
873dbc760bcSTejun Heo 
874dbc760bcSTejun Heo 		rbot = cur;
875dbc760bcSTejun Heo 	}
876dbc760bcSTejun Heo 	return buf;
877dbc760bcSTejun Heo }
878dbc760bcSTejun Heo 
879dbc760bcSTejun Heo static noinline_for_stack
880cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
8818a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
882dd45c9cfSHarvey Harrison {
8838a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
884dd45c9cfSHarvey Harrison 	char *p = mac_addr;
885dd45c9cfSHarvey Harrison 	int i;
886bc7259a2SJoe Perches 	char separator;
88776597ff9SAndrei Emeltchenko 	bool reversed = false;
888bc7259a2SJoe Perches 
88976597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
89076597ff9SAndrei Emeltchenko 	case 'F':
891bc7259a2SJoe Perches 		separator = '-';
89276597ff9SAndrei Emeltchenko 		break;
89376597ff9SAndrei Emeltchenko 
89476597ff9SAndrei Emeltchenko 	case 'R':
89576597ff9SAndrei Emeltchenko 		reversed = true;
89676597ff9SAndrei Emeltchenko 		/* fall through */
89776597ff9SAndrei Emeltchenko 
89876597ff9SAndrei Emeltchenko 	default:
899bc7259a2SJoe Perches 		separator = ':';
90076597ff9SAndrei Emeltchenko 		break;
901bc7259a2SJoe Perches 	}
902dd45c9cfSHarvey Harrison 
903dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
90476597ff9SAndrei Emeltchenko 		if (reversed)
90576597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
90676597ff9SAndrei Emeltchenko 		else
90755036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
90876597ff9SAndrei Emeltchenko 
9098a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
910bc7259a2SJoe Perches 			*p++ = separator;
911dd45c9cfSHarvey Harrison 	}
912dd45c9cfSHarvey Harrison 	*p = '\0';
913dd45c9cfSHarvey Harrison 
914fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
915dd45c9cfSHarvey Harrison }
916dd45c9cfSHarvey Harrison 
917cf3b429bSJoe Perches static noinline_for_stack
918cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
919689afa7dSHarvey Harrison {
920689afa7dSHarvey Harrison 	int i;
9210159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
9220159f24eSJoe Perches 	int index;
9230159f24eSJoe Perches 	int step;
924689afa7dSHarvey Harrison 
9250159f24eSJoe Perches 	switch (fmt[2]) {
9260159f24eSJoe Perches 	case 'h':
9270159f24eSJoe Perches #ifdef __BIG_ENDIAN
9280159f24eSJoe Perches 		index = 0;
9290159f24eSJoe Perches 		step = 1;
9300159f24eSJoe Perches #else
9310159f24eSJoe Perches 		index = 3;
9320159f24eSJoe Perches 		step = -1;
9330159f24eSJoe Perches #endif
9340159f24eSJoe Perches 		break;
9350159f24eSJoe Perches 	case 'l':
9360159f24eSJoe Perches 		index = 3;
9370159f24eSJoe Perches 		step = -1;
9380159f24eSJoe Perches 		break;
9390159f24eSJoe Perches 	case 'n':
9400159f24eSJoe Perches 	case 'b':
9410159f24eSJoe Perches 	default:
9420159f24eSJoe Perches 		index = 0;
9430159f24eSJoe Perches 		step = 1;
9440159f24eSJoe Perches 		break;
9450159f24eSJoe Perches 	}
9468a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
9478a27f7c9SJoe Perches 		char temp[3];	/* hold each IP quad in reverse order */
948133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
9498a27f7c9SJoe Perches 		if (leading_zeros) {
9508a27f7c9SJoe Perches 			if (digits < 3)
9518a27f7c9SJoe Perches 				*p++ = '0';
9528a27f7c9SJoe Perches 			if (digits < 2)
9538a27f7c9SJoe Perches 				*p++ = '0';
9548a27f7c9SJoe Perches 		}
9558a27f7c9SJoe Perches 		/* reverse the digits in the quad */
9568a27f7c9SJoe Perches 		while (digits--)
9578a27f7c9SJoe Perches 			*p++ = temp[digits];
9588a27f7c9SJoe Perches 		if (i < 3)
9598a27f7c9SJoe Perches 			*p++ = '.';
9600159f24eSJoe Perches 		index += step;
9618a27f7c9SJoe Perches 	}
9628a27f7c9SJoe Perches 	*p = '\0';
9637b9186f5SAndré Goddard Rosa 
9648a27f7c9SJoe Perches 	return p;
9658a27f7c9SJoe Perches }
9668a27f7c9SJoe Perches 
967cf3b429bSJoe Perches static noinline_for_stack
968cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
9698a27f7c9SJoe Perches {
9707b9186f5SAndré Goddard Rosa 	int i, j, range;
9718a27f7c9SJoe Perches 	unsigned char zerolength[8];
9728a27f7c9SJoe Perches 	int longest = 1;
9738a27f7c9SJoe Perches 	int colonpos = -1;
9748a27f7c9SJoe Perches 	u16 word;
9757b9186f5SAndré Goddard Rosa 	u8 hi, lo;
9768a27f7c9SJoe Perches 	bool needcolon = false;
977eb78cd26SJoe Perches 	bool useIPv4;
978eb78cd26SJoe Perches 	struct in6_addr in6;
979eb78cd26SJoe Perches 
980eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
981eb78cd26SJoe Perches 
982eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
9838a27f7c9SJoe Perches 
9848a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
9858a27f7c9SJoe Perches 
9868a27f7c9SJoe Perches 	if (useIPv4)
9878a27f7c9SJoe Perches 		range = 6;
9888a27f7c9SJoe Perches 	else
9898a27f7c9SJoe Perches 		range = 8;
9908a27f7c9SJoe Perches 
9918a27f7c9SJoe Perches 	/* find position of longest 0 run */
9928a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
9938a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
994eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
9958a27f7c9SJoe Perches 				break;
9968a27f7c9SJoe Perches 			zerolength[i]++;
9978a27f7c9SJoe Perches 		}
9988a27f7c9SJoe Perches 	}
9998a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10008a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
10018a27f7c9SJoe Perches 			longest = zerolength[i];
10028a27f7c9SJoe Perches 			colonpos = i;
10038a27f7c9SJoe Perches 		}
10048a27f7c9SJoe Perches 	}
100529cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
100629cf519eSJoe Perches 		colonpos = -1;
10078a27f7c9SJoe Perches 
10088a27f7c9SJoe Perches 	/* emit address */
10098a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10108a27f7c9SJoe Perches 		if (i == colonpos) {
10118a27f7c9SJoe Perches 			if (needcolon || i == 0)
10128a27f7c9SJoe Perches 				*p++ = ':';
10138a27f7c9SJoe Perches 			*p++ = ':';
10148a27f7c9SJoe Perches 			needcolon = false;
10158a27f7c9SJoe Perches 			i += longest - 1;
10168a27f7c9SJoe Perches 			continue;
10178a27f7c9SJoe Perches 		}
10188a27f7c9SJoe Perches 		if (needcolon) {
10198a27f7c9SJoe Perches 			*p++ = ':';
10208a27f7c9SJoe Perches 			needcolon = false;
10218a27f7c9SJoe Perches 		}
10228a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
1023eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
10248a27f7c9SJoe Perches 		hi = word >> 8;
10258a27f7c9SJoe Perches 		lo = word & 0xff;
10268a27f7c9SJoe Perches 		if (hi) {
10278a27f7c9SJoe Perches 			if (hi > 0x0f)
102855036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
10298a27f7c9SJoe Perches 			else
10308a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
103155036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10328a27f7c9SJoe Perches 		}
1033b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
103455036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10358a27f7c9SJoe Perches 		else
10368a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
10378a27f7c9SJoe Perches 		needcolon = true;
10388a27f7c9SJoe Perches 	}
10398a27f7c9SJoe Perches 
10408a27f7c9SJoe Perches 	if (useIPv4) {
10418a27f7c9SJoe Perches 		if (needcolon)
10428a27f7c9SJoe Perches 			*p++ = ':';
10430159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
10448a27f7c9SJoe Perches 	}
10458a27f7c9SJoe Perches 	*p = '\0';
10467b9186f5SAndré Goddard Rosa 
10478a27f7c9SJoe Perches 	return p;
10488a27f7c9SJoe Perches }
10498a27f7c9SJoe Perches 
1050cf3b429bSJoe Perches static noinline_for_stack
1051cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
10528a27f7c9SJoe Perches {
10538a27f7c9SJoe Perches 	int i;
10547b9186f5SAndré Goddard Rosa 
1055689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
105655036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
105755036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
10588a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
1059689afa7dSHarvey Harrison 			*p++ = ':';
1060689afa7dSHarvey Harrison 	}
1061689afa7dSHarvey Harrison 	*p = '\0';
10627b9186f5SAndré Goddard Rosa 
10638a27f7c9SJoe Perches 	return p;
10648a27f7c9SJoe Perches }
10658a27f7c9SJoe Perches 
1066cf3b429bSJoe Perches static noinline_for_stack
1067cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
10688a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
10698a27f7c9SJoe Perches {
10708a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
10718a27f7c9SJoe Perches 
10728a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
1073eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
10748a27f7c9SJoe Perches 	else
1075eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
1076689afa7dSHarvey Harrison 
1077fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
1078689afa7dSHarvey Harrison }
1079689afa7dSHarvey Harrison 
1080cf3b429bSJoe Perches static noinline_for_stack
1081cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
10828a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
10834aa99606SHarvey Harrison {
10848a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
10854aa99606SHarvey Harrison 
10860159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
10874aa99606SHarvey Harrison 
1088fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
10894aa99606SHarvey Harrison }
10904aa99606SHarvey Harrison 
1091cf3b429bSJoe Perches static noinline_for_stack
109210679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
109310679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
109410679643SDaniel Borkmann {
109510679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
109610679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
109710679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
109810679643SDaniel Borkmann 		      sizeof("%1234567890")];
109910679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
110010679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
110110679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
110210679643SDaniel Borkmann 	u8 off = 0;
110310679643SDaniel Borkmann 
110410679643SDaniel Borkmann 	fmt++;
110510679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
110610679643SDaniel Borkmann 		switch (*fmt) {
110710679643SDaniel Borkmann 		case 'p':
110810679643SDaniel Borkmann 			have_p = true;
110910679643SDaniel Borkmann 			break;
111010679643SDaniel Borkmann 		case 'f':
111110679643SDaniel Borkmann 			have_f = true;
111210679643SDaniel Borkmann 			break;
111310679643SDaniel Borkmann 		case 's':
111410679643SDaniel Borkmann 			have_s = true;
111510679643SDaniel Borkmann 			break;
111610679643SDaniel Borkmann 		case 'c':
111710679643SDaniel Borkmann 			have_c = true;
111810679643SDaniel Borkmann 			break;
111910679643SDaniel Borkmann 		}
112010679643SDaniel Borkmann 	}
112110679643SDaniel Borkmann 
112210679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
112310679643SDaniel Borkmann 		*p = '[';
112410679643SDaniel Borkmann 		off = 1;
112510679643SDaniel Borkmann 	}
112610679643SDaniel Borkmann 
112710679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
112810679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
112910679643SDaniel Borkmann 	else
113010679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
113110679643SDaniel Borkmann 
113210679643SDaniel Borkmann 	if (have_p || have_s || have_f)
113310679643SDaniel Borkmann 		*p++ = ']';
113410679643SDaniel Borkmann 
113510679643SDaniel Borkmann 	if (have_p) {
113610679643SDaniel Borkmann 		*p++ = ':';
113710679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
113810679643SDaniel Borkmann 	}
113910679643SDaniel Borkmann 	if (have_f) {
114010679643SDaniel Borkmann 		*p++ = '/';
114110679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
114210679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
114310679643SDaniel Borkmann 	}
114410679643SDaniel Borkmann 	if (have_s) {
114510679643SDaniel Borkmann 		*p++ = '%';
114610679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
114710679643SDaniel Borkmann 	}
114810679643SDaniel Borkmann 	*p = '\0';
114910679643SDaniel Borkmann 
115010679643SDaniel Borkmann 	return string(buf, end, ip6_addr, spec);
115110679643SDaniel Borkmann }
115210679643SDaniel Borkmann 
115310679643SDaniel Borkmann static noinline_for_stack
115410679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
115510679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
115610679643SDaniel Borkmann {
115710679643SDaniel Borkmann 	bool have_p = false;
115810679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
115910679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
116010679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
116110679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
116210679643SDaniel Borkmann 
116310679643SDaniel Borkmann 	fmt++;
116410679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
116510679643SDaniel Borkmann 		switch (*fmt) {
116610679643SDaniel Borkmann 		case 'p':
116710679643SDaniel Borkmann 			have_p = true;
116810679643SDaniel Borkmann 			break;
116910679643SDaniel Borkmann 		case 'h':
117010679643SDaniel Borkmann 		case 'l':
117110679643SDaniel Borkmann 		case 'n':
117210679643SDaniel Borkmann 		case 'b':
117310679643SDaniel Borkmann 			fmt4[2] = *fmt;
117410679643SDaniel Borkmann 			break;
117510679643SDaniel Borkmann 		}
117610679643SDaniel Borkmann 	}
117710679643SDaniel Borkmann 
117810679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
117910679643SDaniel Borkmann 	if (have_p) {
118010679643SDaniel Borkmann 		*p++ = ':';
118110679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
118210679643SDaniel Borkmann 	}
118310679643SDaniel Borkmann 	*p = '\0';
118410679643SDaniel Borkmann 
118510679643SDaniel Borkmann 	return string(buf, end, ip4_addr, spec);
118610679643SDaniel Borkmann }
118710679643SDaniel Borkmann 
118810679643SDaniel Borkmann static noinline_for_stack
118971dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
119071dca95dSAndy Shevchenko 		     const char *fmt)
119171dca95dSAndy Shevchenko {
119271dca95dSAndy Shevchenko 	bool found = true;
119371dca95dSAndy Shevchenko 	int count = 1;
119471dca95dSAndy Shevchenko 	unsigned int flags = 0;
119571dca95dSAndy Shevchenko 	int len;
119671dca95dSAndy Shevchenko 
119771dca95dSAndy Shevchenko 	if (spec.field_width == 0)
119871dca95dSAndy Shevchenko 		return buf;				/* nothing to print */
119971dca95dSAndy Shevchenko 
120071dca95dSAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
120171dca95dSAndy Shevchenko 		return string(buf, end, NULL, spec);	/* NULL pointer */
120271dca95dSAndy Shevchenko 
120371dca95dSAndy Shevchenko 
120471dca95dSAndy Shevchenko 	do {
120571dca95dSAndy Shevchenko 		switch (fmt[count++]) {
120671dca95dSAndy Shevchenko 		case 'a':
120771dca95dSAndy Shevchenko 			flags |= ESCAPE_ANY;
120871dca95dSAndy Shevchenko 			break;
120971dca95dSAndy Shevchenko 		case 'c':
121071dca95dSAndy Shevchenko 			flags |= ESCAPE_SPECIAL;
121171dca95dSAndy Shevchenko 			break;
121271dca95dSAndy Shevchenko 		case 'h':
121371dca95dSAndy Shevchenko 			flags |= ESCAPE_HEX;
121471dca95dSAndy Shevchenko 			break;
121571dca95dSAndy Shevchenko 		case 'n':
121671dca95dSAndy Shevchenko 			flags |= ESCAPE_NULL;
121771dca95dSAndy Shevchenko 			break;
121871dca95dSAndy Shevchenko 		case 'o':
121971dca95dSAndy Shevchenko 			flags |= ESCAPE_OCTAL;
122071dca95dSAndy Shevchenko 			break;
122171dca95dSAndy Shevchenko 		case 'p':
122271dca95dSAndy Shevchenko 			flags |= ESCAPE_NP;
122371dca95dSAndy Shevchenko 			break;
122471dca95dSAndy Shevchenko 		case 's':
122571dca95dSAndy Shevchenko 			flags |= ESCAPE_SPACE;
122671dca95dSAndy Shevchenko 			break;
122771dca95dSAndy Shevchenko 		default:
122871dca95dSAndy Shevchenko 			found = false;
122971dca95dSAndy Shevchenko 			break;
123071dca95dSAndy Shevchenko 		}
123171dca95dSAndy Shevchenko 	} while (found);
123271dca95dSAndy Shevchenko 
123371dca95dSAndy Shevchenko 	if (!flags)
123471dca95dSAndy Shevchenko 		flags = ESCAPE_ANY_NP;
123571dca95dSAndy Shevchenko 
123671dca95dSAndy Shevchenko 	len = spec.field_width < 0 ? 1 : spec.field_width;
123771dca95dSAndy Shevchenko 
123871dca95dSAndy Shevchenko 	/* Ignore the error. We print as many characters as we can */
123971dca95dSAndy Shevchenko 	string_escape_mem(addr, len, &buf, end - buf, flags, NULL);
124071dca95dSAndy Shevchenko 
124171dca95dSAndy Shevchenko 	return buf;
124271dca95dSAndy Shevchenko }
124371dca95dSAndy Shevchenko 
124471dca95dSAndy Shevchenko static noinline_for_stack
1245cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
12469ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
12479ac6e44eSJoe Perches {
12489ac6e44eSJoe Perches 	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
12499ac6e44eSJoe Perches 	char *p = uuid;
12509ac6e44eSJoe Perches 	int i;
12519ac6e44eSJoe Perches 	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
12529ac6e44eSJoe Perches 	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
12539ac6e44eSJoe Perches 	const u8 *index = be;
12549ac6e44eSJoe Perches 	bool uc = false;
12559ac6e44eSJoe Perches 
12569ac6e44eSJoe Perches 	switch (*(++fmt)) {
12579ac6e44eSJoe Perches 	case 'L':
12589ac6e44eSJoe Perches 		uc = true;		/* fall-through */
12599ac6e44eSJoe Perches 	case 'l':
12609ac6e44eSJoe Perches 		index = le;
12619ac6e44eSJoe Perches 		break;
12629ac6e44eSJoe Perches 	case 'B':
12639ac6e44eSJoe Perches 		uc = true;
12649ac6e44eSJoe Perches 		break;
12659ac6e44eSJoe Perches 	}
12669ac6e44eSJoe Perches 
12679ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
126855036ba7SAndy Shevchenko 		p = hex_byte_pack(p, addr[index[i]]);
12699ac6e44eSJoe Perches 		switch (i) {
12709ac6e44eSJoe Perches 		case 3:
12719ac6e44eSJoe Perches 		case 5:
12729ac6e44eSJoe Perches 		case 7:
12739ac6e44eSJoe Perches 		case 9:
12749ac6e44eSJoe Perches 			*p++ = '-';
12759ac6e44eSJoe Perches 			break;
12769ac6e44eSJoe Perches 		}
12779ac6e44eSJoe Perches 	}
12789ac6e44eSJoe Perches 
12799ac6e44eSJoe Perches 	*p = 0;
12809ac6e44eSJoe Perches 
12819ac6e44eSJoe Perches 	if (uc) {
12829ac6e44eSJoe Perches 		p = uuid;
12839ac6e44eSJoe Perches 		do {
12849ac6e44eSJoe Perches 			*p = toupper(*p);
12859ac6e44eSJoe Perches 		} while (*(++p));
12869ac6e44eSJoe Perches 	}
12879ac6e44eSJoe Perches 
12889ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
12899ac6e44eSJoe Perches }
12909ac6e44eSJoe Perches 
1291c8f44affSMichał Mirosław static
1292c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1293c8f44affSMichał Mirosław 		      struct printf_spec spec)
1294c8f44affSMichał Mirosław {
1295c8f44affSMichał Mirosław 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1296c8f44affSMichał Mirosław 	if (spec.field_width == -1)
1297c8f44affSMichał Mirosław 		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1298c8f44affSMichał Mirosław 	spec.base = 16;
1299c8f44affSMichał Mirosław 
1300c8f44affSMichał Mirosław 	return number(buf, end, *(const netdev_features_t *)addr, spec);
1301c8f44affSMichał Mirosław }
1302c8f44affSMichał Mirosław 
1303aaf07621SJoe Perches static noinline_for_stack
1304aaf07621SJoe Perches char *address_val(char *buf, char *end, const void *addr,
1305aaf07621SJoe Perches 		  struct printf_spec spec, const char *fmt)
1306aaf07621SJoe Perches {
1307aaf07621SJoe Perches 	unsigned long long num;
1308aaf07621SJoe Perches 
1309aaf07621SJoe Perches 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1310aaf07621SJoe Perches 	spec.base = 16;
1311aaf07621SJoe Perches 
1312aaf07621SJoe Perches 	switch (fmt[1]) {
1313aaf07621SJoe Perches 	case 'd':
1314aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
1315aaf07621SJoe Perches 		spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1316aaf07621SJoe Perches 		break;
1317aaf07621SJoe Perches 	case 'p':
1318aaf07621SJoe Perches 	default:
1319aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
1320aaf07621SJoe Perches 		spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1321aaf07621SJoe Perches 		break;
1322aaf07621SJoe Perches 	}
1323aaf07621SJoe Perches 
1324aaf07621SJoe Perches 	return number(buf, end, num, spec);
1325aaf07621SJoe Perches }
1326aaf07621SJoe Perches 
1327900cca29SGeert Uytterhoeven static noinline_for_stack
1328900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1329900cca29SGeert Uytterhoeven 	    const char *fmt)
1330900cca29SGeert Uytterhoeven {
1331900cca29SGeert Uytterhoeven 	if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1332900cca29SGeert Uytterhoeven 		return string(buf, end, NULL, spec);
1333900cca29SGeert Uytterhoeven 
1334900cca29SGeert Uytterhoeven 	switch (fmt[1]) {
1335900cca29SGeert Uytterhoeven 	case 'r':
1336900cca29SGeert Uytterhoeven 		return number(buf, end, clk_get_rate(clk), spec);
1337900cca29SGeert Uytterhoeven 
1338900cca29SGeert Uytterhoeven 	case 'n':
1339900cca29SGeert Uytterhoeven 	default:
1340900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK
1341900cca29SGeert Uytterhoeven 		return string(buf, end, __clk_get_name(clk), spec);
1342900cca29SGeert Uytterhoeven #else
1343900cca29SGeert Uytterhoeven 		spec.base = 16;
1344900cca29SGeert Uytterhoeven 		spec.field_width = sizeof(unsigned long) * 2 + 2;
1345900cca29SGeert Uytterhoeven 		spec.flags |= SPECIAL | SMALL | ZEROPAD;
1346900cca29SGeert Uytterhoeven 		return number(buf, end, (unsigned long)clk, spec);
1347900cca29SGeert Uytterhoeven #endif
1348900cca29SGeert Uytterhoeven 	}
1349900cca29SGeert Uytterhoeven }
1350900cca29SGeert Uytterhoeven 
1351411f05f1SIngo Molnar int kptr_restrict __read_mostly;
1352455cd5abSDan Rosenberg 
13534d8a743cSLinus Torvalds /*
13544d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
13554d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
13564d8a743cSLinus Torvalds  * specifiers.
13574d8a743cSLinus Torvalds  *
1358332d2e78SLinus Torvalds  * Right now we handle:
13590fe1ef24SLinus Torvalds  *
13600c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
13610c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
13620efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
13630efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
1364b0d33c2bSJoe Perches  * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
13650f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
1366c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1367c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1368dbc760bcSTejun Heo  * - 'b[l]' For a bitmap, the number of bits is determined by the field
1369dbc760bcSTejun Heo  *       width which must be explicitly specified either as part of the
1370dbc760bcSTejun Heo  *       format string '%32b[l]' or through '%*b[l]', [l] selects
1371dbc760bcSTejun Heo  *       range-list format instead of hex format
1372dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
1373dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
13748a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1375bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1376c8e00060SJoe Perches  *       with a dash-separated hex notation
13777c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
13788a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
13798a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
13808a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
138110679643SDaniel Borkmann  *       [S][pfs]
138210679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
138310679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
13848a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
13858a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
13868a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
138710679643SDaniel Borkmann  *       [S][pfs]
138810679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
138910679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
139010679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
139110679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
139229cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
139371dca95dSAndy Shevchenko  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
139471dca95dSAndy Shevchenko  *                of the following flags (see string_escape_mem() for the
139571dca95dSAndy Shevchenko  *                details):
139671dca95dSAndy Shevchenko  *                  a - ESCAPE_ANY
139771dca95dSAndy Shevchenko  *                  c - ESCAPE_SPECIAL
139871dca95dSAndy Shevchenko  *                  h - ESCAPE_HEX
139971dca95dSAndy Shevchenko  *                  n - ESCAPE_NULL
140071dca95dSAndy Shevchenko  *                  o - ESCAPE_OCTAL
140171dca95dSAndy Shevchenko  *                  p - ESCAPE_NP
140271dca95dSAndy Shevchenko  *                  s - ESCAPE_SPACE
140371dca95dSAndy Shevchenko  *                By default ESCAPE_ANY_NP is used.
14049ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
14059ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
14069ac6e44eSJoe Perches  *       Options for %pU are:
14079ac6e44eSJoe Perches  *         b big endian lower case hex (default)
14089ac6e44eSJoe Perches  *         B big endian UPPER case hex
14099ac6e44eSJoe Perches  *         l little endian lower case hex
14109ac6e44eSJoe Perches  *         L little endian UPPER case hex
14119ac6e44eSJoe Perches  *           big endian output byte order is:
14129ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
14139ac6e44eSJoe Perches  *           little endian output byte order is:
14149ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
14157db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
14167db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
14177db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
14187db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
14197db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
1420455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
1421c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
142231550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
142331550a16SAndy Shevchenko  *            a certain separator (' ' by default):
142431550a16SAndy Shevchenko  *              C colon
142531550a16SAndy Shevchenko  *              D dash
142631550a16SAndy Shevchenko  *              N no separator
142731550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
142831550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
1429aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1430aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
1431c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
1432c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
1433900cca29SGeert Uytterhoeven  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1434900cca29SGeert Uytterhoeven  *       (legacy clock framework) of the clock
1435900cca29SGeert Uytterhoeven  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1436900cca29SGeert Uytterhoeven  *        (legacy clock framework) of the clock
1437900cca29SGeert Uytterhoeven  * - 'Cr' For a clock, it prints the current rate of the clock
14389ac6e44eSJoe Perches  *
1439332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1440332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
1441332d2e78SLinus Torvalds  * pointer to the real address.
14424d8a743cSLinus Torvalds  */
1443cf3b429bSJoe Perches static noinline_for_stack
1444cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1445fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
144678a8bf69SLinus Torvalds {
1447725fe002SGrant Likely 	int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1448725fe002SGrant Likely 
14499f36e2c4SKees Cook 	if (!ptr && *fmt != 'K') {
14505e057981SJoe Perches 		/*
14515e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
14525e057981SJoe Perches 		 * tabular output look nice.
14535e057981SJoe Perches 		 */
14545e057981SJoe Perches 		if (spec.field_width == -1)
1455725fe002SGrant Likely 			spec.field_width = default_width;
1456fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
14575e057981SJoe Perches 	}
1458d97106abSLinus Torvalds 
14590fe1ef24SLinus Torvalds 	switch (*fmt) {
14600fe1ef24SLinus Torvalds 	case 'F':
14610c8b946eSFrederic Weisbecker 	case 'f':
14620fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
14630fe1ef24SLinus Torvalds 		/* Fallthrough */
14640fe1ef24SLinus Torvalds 	case 'S':
14659ac6e44eSJoe Perches 	case 's':
14660f77a8d3SNamhyung Kim 	case 'B':
1467b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
1468332d2e78SLinus Torvalds 	case 'R':
1469c7dabef8SBjorn Helgaas 	case 'r':
1470fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
147131550a16SAndy Shevchenko 	case 'h':
147231550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
1473dbc760bcSTejun Heo 	case 'b':
1474dbc760bcSTejun Heo 		switch (fmt[1]) {
1475dbc760bcSTejun Heo 		case 'l':
1476dbc760bcSTejun Heo 			return bitmap_list_string(buf, end, ptr, spec, fmt);
1477dbc760bcSTejun Heo 		default:
1478dbc760bcSTejun Heo 			return bitmap_string(buf, end, ptr, spec, fmt);
1479dbc760bcSTejun Heo 		}
14808a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
14818a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
148276597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
148376597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
14848a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
14858a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
14868a27f7c9SJoe Perches 					 * 4:	1.2.3.4
14878a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
14888a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
14898a27f7c9SJoe Perches 					 */
14908a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
14918a27f7c9SJoe Perches 					 * 4:	001.002.003.004
14928a27f7c9SJoe Perches 					 * 6:   000102...0f
14938a27f7c9SJoe Perches 					 */
14948a27f7c9SJoe Perches 		switch (fmt[1]) {
14958a27f7c9SJoe Perches 		case '6':
14968a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
14978a27f7c9SJoe Perches 		case '4':
14988a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
149910679643SDaniel Borkmann 		case 'S': {
150010679643SDaniel Borkmann 			const union {
150110679643SDaniel Borkmann 				struct sockaddr		raw;
150210679643SDaniel Borkmann 				struct sockaddr_in	v4;
150310679643SDaniel Borkmann 				struct sockaddr_in6	v6;
150410679643SDaniel Borkmann 			} *sa = ptr;
150510679643SDaniel Borkmann 
150610679643SDaniel Borkmann 			switch (sa->raw.sa_family) {
150710679643SDaniel Borkmann 			case AF_INET:
150810679643SDaniel Borkmann 				return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
150910679643SDaniel Borkmann 			case AF_INET6:
151010679643SDaniel Borkmann 				return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
151110679643SDaniel Borkmann 			default:
151210679643SDaniel Borkmann 				return string(buf, end, "(invalid address)", spec);
151310679643SDaniel Borkmann 			}}
15148a27f7c9SJoe Perches 		}
15154aa99606SHarvey Harrison 		break;
151671dca95dSAndy Shevchenko 	case 'E':
151771dca95dSAndy Shevchenko 		return escaped_string(buf, end, ptr, spec, fmt);
15189ac6e44eSJoe Perches 	case 'U':
15199ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
15207db6f5fbSJoe Perches 	case 'V':
15215756b76eSJan Beulich 		{
15225756b76eSJan Beulich 			va_list va;
15235756b76eSJan Beulich 
15245756b76eSJan Beulich 			va_copy(va, *((struct va_format *)ptr)->va);
15255756b76eSJan Beulich 			buf += vsnprintf(buf, end > buf ? end - buf : 0,
15265756b76eSJan Beulich 					 ((struct va_format *)ptr)->fmt, va);
15275756b76eSJan Beulich 			va_end(va);
15285756b76eSJan Beulich 			return buf;
15295756b76eSJan Beulich 		}
1530455cd5abSDan Rosenberg 	case 'K':
1531455cd5abSDan Rosenberg 		/*
1532455cd5abSDan Rosenberg 		 * %pK cannot be used in IRQ context because its test
1533455cd5abSDan Rosenberg 		 * for CAP_SYSLOG would be meaningless.
1534455cd5abSDan Rosenberg 		 */
15353715c530SDan Rosenberg 		if (kptr_restrict && (in_irq() || in_serving_softirq() ||
15363715c530SDan Rosenberg 				      in_nmi())) {
1537455cd5abSDan Rosenberg 			if (spec.field_width == -1)
1538725fe002SGrant Likely 				spec.field_width = default_width;
1539455cd5abSDan Rosenberg 			return string(buf, end, "pK-error", spec);
1540455cd5abSDan Rosenberg 		}
1541312b4e22SRyan Mallon 
1542312b4e22SRyan Mallon 		switch (kptr_restrict) {
1543312b4e22SRyan Mallon 		case 0:
1544312b4e22SRyan Mallon 			/* Always print %pK values */
1545312b4e22SRyan Mallon 			break;
1546312b4e22SRyan Mallon 		case 1: {
1547312b4e22SRyan Mallon 			/*
1548312b4e22SRyan Mallon 			 * Only print the real pointer value if the current
1549312b4e22SRyan Mallon 			 * process has CAP_SYSLOG and is running with the
1550312b4e22SRyan Mallon 			 * same credentials it started with. This is because
1551312b4e22SRyan Mallon 			 * access to files is checked at open() time, but %pK
1552312b4e22SRyan Mallon 			 * checks permission at read() time. We don't want to
1553312b4e22SRyan Mallon 			 * leak pointer values if a binary opens a file using
1554312b4e22SRyan Mallon 			 * %pK and then elevates privileges before reading it.
1555312b4e22SRyan Mallon 			 */
1556312b4e22SRyan Mallon 			const struct cred *cred = current_cred();
1557312b4e22SRyan Mallon 
1558312b4e22SRyan Mallon 			if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1559312b4e22SRyan Mallon 			    !uid_eq(cred->euid, cred->uid) ||
1560312b4e22SRyan Mallon 			    !gid_eq(cred->egid, cred->gid))
156126297607SJoe Perches 				ptr = NULL;
156226297607SJoe Perches 			break;
1563312b4e22SRyan Mallon 		}
1564312b4e22SRyan Mallon 		case 2:
1565312b4e22SRyan Mallon 		default:
1566312b4e22SRyan Mallon 			/* Always print 0's for %pK */
1567312b4e22SRyan Mallon 			ptr = NULL;
1568312b4e22SRyan Mallon 			break;
1569312b4e22SRyan Mallon 		}
1570312b4e22SRyan Mallon 		break;
1571312b4e22SRyan Mallon 
1572c8f44affSMichał Mirosław 	case 'N':
1573c8f44affSMichał Mirosław 		switch (fmt[1]) {
1574c8f44affSMichał Mirosław 		case 'F':
1575c8f44affSMichał Mirosław 			return netdev_feature_string(buf, end, ptr, spec);
1576c8f44affSMichał Mirosław 		}
1577c8f44affSMichał Mirosław 		break;
15787d799210SStepan Moskovchenko 	case 'a':
1579aaf07621SJoe Perches 		return address_val(buf, end, ptr, spec, fmt);
15804b6ccca7SAl Viro 	case 'd':
15814b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
1582900cca29SGeert Uytterhoeven 	case 'C':
1583900cca29SGeert Uytterhoeven 		return clock(buf, end, ptr, spec, fmt);
15844b6ccca7SAl Viro 	case 'D':
15854b6ccca7SAl Viro 		return dentry_name(buf, end,
15864b6ccca7SAl Viro 				   ((const struct file *)ptr)->f_path.dentry,
15874b6ccca7SAl Viro 				   spec, fmt);
15880fe1ef24SLinus Torvalds 	}
1589fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
1590fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
1591725fe002SGrant Likely 		spec.field_width = default_width;
1592fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
159378a8bf69SLinus Torvalds 	}
1594fef20d9cSFrederic Weisbecker 	spec.base = 16;
1595fef20d9cSFrederic Weisbecker 
1596fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
1597fef20d9cSFrederic Weisbecker }
1598fef20d9cSFrederic Weisbecker 
1599fef20d9cSFrederic Weisbecker /*
1600fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
1601fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
1602fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
1603fef20d9cSFrederic Weisbecker  * to go on the next call).
1604fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
1605fef20d9cSFrederic Weisbecker  *
1606fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
1607fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
1608fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
1609fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
1610fef20d9cSFrederic Weisbecker  *
1611fef20d9cSFrederic Weisbecker  * @fmt: the format string
1612fef20d9cSFrederic Weisbecker  * @type of the token returned
1613fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
1614fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
1615fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
1616fef20d9cSFrederic Weisbecker  * @precision: precision of a number
1617fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
1618fef20d9cSFrederic Weisbecker  */
1619cf3b429bSJoe Perches static noinline_for_stack
1620cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
1621fef20d9cSFrederic Weisbecker {
1622fef20d9cSFrederic Weisbecker 	const char *start = fmt;
1623fef20d9cSFrederic Weisbecker 
1624fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
1625ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
1626fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
1627fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
1628fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
1629fef20d9cSFrederic Weisbecker 		}
1630fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1631fef20d9cSFrederic Weisbecker 		goto precision;
1632fef20d9cSFrederic Weisbecker 	}
1633fef20d9cSFrederic Weisbecker 
1634fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
1635fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
1636fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
1637fef20d9cSFrederic Weisbecker 			spec->precision = 0;
1638fef20d9cSFrederic Weisbecker 
1639fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1640fef20d9cSFrederic Weisbecker 		goto qualifier;
1641fef20d9cSFrederic Weisbecker 	}
1642fef20d9cSFrederic Weisbecker 
1643fef20d9cSFrederic Weisbecker 	/* By default */
1644fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
1645fef20d9cSFrederic Weisbecker 
1646fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
1647fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
1648fef20d9cSFrederic Weisbecker 			break;
1649fef20d9cSFrederic Weisbecker 	}
1650fef20d9cSFrederic Weisbecker 
1651fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
1652fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
1653fef20d9cSFrederic Weisbecker 		return fmt - start;
1654fef20d9cSFrederic Weisbecker 
1655fef20d9cSFrederic Weisbecker 	/* Process flags */
1656fef20d9cSFrederic Weisbecker 	spec->flags = 0;
1657fef20d9cSFrederic Weisbecker 
1658fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
1659fef20d9cSFrederic Weisbecker 		bool found = true;
1660fef20d9cSFrederic Weisbecker 
1661fef20d9cSFrederic Weisbecker 		++fmt;
1662fef20d9cSFrederic Weisbecker 
1663fef20d9cSFrederic Weisbecker 		switch (*fmt) {
1664fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
1665fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
1666fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
1667fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
1668fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
1669fef20d9cSFrederic Weisbecker 		default:  found = false;
1670fef20d9cSFrederic Weisbecker 		}
1671fef20d9cSFrederic Weisbecker 
1672fef20d9cSFrederic Weisbecker 		if (!found)
1673fef20d9cSFrederic Weisbecker 			break;
1674fef20d9cSFrederic Weisbecker 	}
1675fef20d9cSFrederic Weisbecker 
1676fef20d9cSFrederic Weisbecker 	/* get field width */
1677fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1678fef20d9cSFrederic Weisbecker 
1679fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1680fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1681fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1682fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1683ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1684fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1685fef20d9cSFrederic Weisbecker 	}
1686fef20d9cSFrederic Weisbecker 
1687fef20d9cSFrederic Weisbecker precision:
1688fef20d9cSFrederic Weisbecker 	/* get the precision */
1689fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1690fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1691fef20d9cSFrederic Weisbecker 		++fmt;
1692fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1693fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1694fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1695fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1696fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1697fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1698adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1699fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1700fef20d9cSFrederic Weisbecker 		}
1701fef20d9cSFrederic Weisbecker 	}
1702fef20d9cSFrederic Weisbecker 
1703fef20d9cSFrederic Weisbecker qualifier:
1704fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1705fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
170675fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
170775fb8f26SAndy Shevchenko 	    _tolower(*fmt) == 'z' || *fmt == 't') {
1708a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1709a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1710a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1711fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1712fef20d9cSFrederic Weisbecker 				++fmt;
1713a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1714a4e94ef0SZhaolei 				spec->qualifier = 'H';
1715a4e94ef0SZhaolei 				++fmt;
1716a4e94ef0SZhaolei 			}
1717fef20d9cSFrederic Weisbecker 		}
1718fef20d9cSFrederic Weisbecker 	}
1719fef20d9cSFrederic Weisbecker 
1720fef20d9cSFrederic Weisbecker 	/* default base */
1721fef20d9cSFrederic Weisbecker 	spec->base = 10;
1722fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1723fef20d9cSFrederic Weisbecker 	case 'c':
1724fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1725fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1726fef20d9cSFrederic Weisbecker 
1727fef20d9cSFrederic Weisbecker 	case 's':
1728fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1729fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1730fef20d9cSFrederic Weisbecker 
1731fef20d9cSFrederic Weisbecker 	case 'p':
1732fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1733ffbfed03SRasmus Villemoes 		return ++fmt - start;
1734fef20d9cSFrederic Weisbecker 
1735fef20d9cSFrederic Weisbecker 	case '%':
1736fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1737fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1738fef20d9cSFrederic Weisbecker 
1739fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1740fef20d9cSFrederic Weisbecker 	case 'o':
1741fef20d9cSFrederic Weisbecker 		spec->base = 8;
1742fef20d9cSFrederic Weisbecker 		break;
1743fef20d9cSFrederic Weisbecker 
1744fef20d9cSFrederic Weisbecker 	case 'x':
1745fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1746fef20d9cSFrederic Weisbecker 
1747fef20d9cSFrederic Weisbecker 	case 'X':
1748fef20d9cSFrederic Weisbecker 		spec->base = 16;
1749fef20d9cSFrederic Weisbecker 		break;
1750fef20d9cSFrederic Weisbecker 
1751fef20d9cSFrederic Weisbecker 	case 'd':
1752fef20d9cSFrederic Weisbecker 	case 'i':
175339e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1754fef20d9cSFrederic Weisbecker 	case 'u':
1755fef20d9cSFrederic Weisbecker 		break;
1756fef20d9cSFrederic Weisbecker 
1757708d96fdSRyan Mallon 	case 'n':
1758708d96fdSRyan Mallon 		/*
1759708d96fdSRyan Mallon 		 * Since %n poses a greater security risk than utility, treat
1760708d96fdSRyan Mallon 		 * it as an invalid format specifier. Warn about its use so
1761708d96fdSRyan Mallon 		 * that new instances don't get added.
1762708d96fdSRyan Mallon 		 */
1763708d96fdSRyan Mallon 		WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt);
1764708d96fdSRyan Mallon 		/* Fall-through */
1765708d96fdSRyan Mallon 
1766fef20d9cSFrederic Weisbecker 	default:
1767fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1768fef20d9cSFrederic Weisbecker 		return fmt - start;
1769fef20d9cSFrederic Weisbecker 	}
1770fef20d9cSFrederic Weisbecker 
1771fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1772fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1773fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
177451be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
177551be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
177675fb8f26SAndy Shevchenko 	} else if (_tolower(spec->qualifier) == 'z') {
1777fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1778fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1779fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1780a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
178151be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
178251be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
1783fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
178451be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
178551be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
1786fef20d9cSFrederic Weisbecker 	} else {
178751be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
178851be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
1789fef20d9cSFrederic Weisbecker 	}
1790fef20d9cSFrederic Weisbecker 
1791fef20d9cSFrederic Weisbecker 	return ++fmt - start;
179278a8bf69SLinus Torvalds }
179378a8bf69SLinus Torvalds 
17941da177e4SLinus Torvalds /**
17951da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
17961da177e4SLinus Torvalds  * @buf: The buffer to place the result into
17971da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
17981da177e4SLinus Torvalds  * @fmt: The format string to use
17991da177e4SLinus Torvalds  * @args: Arguments for the format string
18001da177e4SLinus Torvalds  *
180120036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
180291adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
180391adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
18040c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
18050c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
18060f77a8d3SNamhyung Kim  * %pB output the name of a backtrace symbol with its offset
18078a79503aSUwe Kleine-König  * %pR output the address range in a struct resource with decoded flags
18088a79503aSUwe Kleine-König  * %pr output the address range in a struct resource with raw flags
1809dbc760bcSTejun Heo  * %pb output the bitmap with field width as the number of bits
1810dbc760bcSTejun Heo  * %pbl output the bitmap as range list with field width as the number of bits
18118a79503aSUwe Kleine-König  * %pM output a 6-byte MAC address with colons
18127c59154eSAndy Shevchenko  * %pMR output a 6-byte MAC address with colons in reversed order
18137c59154eSAndy Shevchenko  * %pMF output a 6-byte MAC address with dashes
18148a79503aSUwe Kleine-König  * %pm output a 6-byte MAC address without colons
18157c59154eSAndy Shevchenko  * %pmR output a 6-byte MAC address without colons in reversed order
18168a79503aSUwe Kleine-König  * %pI4 print an IPv4 address without leading zeros
18178a79503aSUwe Kleine-König  * %pi4 print an IPv4 address with leading zeros
18188a79503aSUwe Kleine-König  * %pI6 print an IPv6 address with colons
18198a79503aSUwe Kleine-König  * %pi6 print an IPv6 address without colons
1820f996f208SJan Engelhardt  * %pI6c print an IPv6 address as specified by RFC 5952
182110679643SDaniel Borkmann  * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
182210679643SDaniel Borkmann  * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
18238a79503aSUwe Kleine-König  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
18248a79503aSUwe Kleine-König  *   case.
182571dca95dSAndy Shevchenko  * %*pE[achnops] print an escaped buffer
182631550a16SAndy Shevchenko  * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
182731550a16SAndy Shevchenko  *           bytes of the input)
1828900cca29SGeert Uytterhoeven  * %pC output the name (Common Clock Framework) or address (legacy clock
1829900cca29SGeert Uytterhoeven  *     framework) of a clock
1830900cca29SGeert Uytterhoeven  * %pCn output the name (Common Clock Framework) or address (legacy clock
1831900cca29SGeert Uytterhoeven  *      framework) of a clock
1832900cca29SGeert Uytterhoeven  * %pCr output the current rate of a clock
18330efb4d20SSteven Rostedt  * %n is ignored
183420036fdcSAndi Kleen  *
183580f548e0SAndrew Morton  * ** Please update Documentation/printk-formats.txt when making changes **
183680f548e0SAndrew Morton  *
18371da177e4SLinus Torvalds  * The return value is the number of characters which would
18381da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
18391da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
18401da177e4SLinus Torvalds  * number of characters written into @buf as return value
184172fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
18421da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
18431da177e4SLinus Torvalds  * string is truncated.
18441da177e4SLinus Torvalds  *
1845ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
18461da177e4SLinus Torvalds  */
18471da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
18481da177e4SLinus Torvalds {
18491da177e4SLinus Torvalds 	unsigned long long num;
1850d4be151bSAndré Goddard Rosa 	char *str, *end;
1851fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
18521da177e4SLinus Torvalds 
1853f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1854f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
18552aa2f9e2SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
18561da177e4SLinus Torvalds 		return 0;
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	str = buf;
1859f796937aSJeremy Fitzhardinge 	end = buf + size;
18601da177e4SLinus Torvalds 
1861f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1862f796937aSJeremy Fitzhardinge 	if (end < buf) {
18631da177e4SLinus Torvalds 		end = ((void *)-1);
1864f796937aSJeremy Fitzhardinge 		size = end - buf;
18651da177e4SLinus Torvalds 	}
18661da177e4SLinus Torvalds 
1867fef20d9cSFrederic Weisbecker 	while (*fmt) {
1868fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1869d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1870fef20d9cSFrederic Weisbecker 
1871fef20d9cSFrederic Weisbecker 		fmt += read;
1872fef20d9cSFrederic Weisbecker 
1873fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1874fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1875fef20d9cSFrederic Weisbecker 			int copy = read;
1876fef20d9cSFrederic Weisbecker 			if (str < end) {
1877fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1878fef20d9cSFrederic Weisbecker 					copy = end - str;
1879fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1880fef20d9cSFrederic Weisbecker 			}
1881fef20d9cSFrederic Weisbecker 			str += read;
1882fef20d9cSFrederic Weisbecker 			break;
18831da177e4SLinus Torvalds 		}
18841da177e4SLinus Torvalds 
1885ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1886fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1887fef20d9cSFrederic Weisbecker 			break;
18881da177e4SLinus Torvalds 
1889fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1890fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1891fef20d9cSFrederic Weisbecker 			break;
18921da177e4SLinus Torvalds 
1893d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1894d4be151bSAndré Goddard Rosa 			char c;
1895d4be151bSAndré Goddard Rosa 
1896fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1897fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1898f796937aSJeremy Fitzhardinge 					if (str < end)
18991da177e4SLinus Torvalds 						*str = ' ';
19001da177e4SLinus Torvalds 					++str;
1901fef20d9cSFrederic Weisbecker 
19021da177e4SLinus Torvalds 				}
19031da177e4SLinus Torvalds 			}
19041da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1905f796937aSJeremy Fitzhardinge 			if (str < end)
19061da177e4SLinus Torvalds 				*str = c;
19071da177e4SLinus Torvalds 			++str;
1908fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1909f796937aSJeremy Fitzhardinge 				if (str < end)
19101da177e4SLinus Torvalds 					*str = ' ';
19111da177e4SLinus Torvalds 				++str;
19121da177e4SLinus Torvalds 			}
1913fef20d9cSFrederic Weisbecker 			break;
1914d4be151bSAndré Goddard Rosa 		}
19151da177e4SLinus Torvalds 
1916fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1917fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1918fef20d9cSFrederic Weisbecker 			break;
19191da177e4SLinus Torvalds 
1920fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1921ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, va_arg(args, void *),
1922fef20d9cSFrederic Weisbecker 				      spec);
1923fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
19244d8a743cSLinus Torvalds 				fmt++;
1925fef20d9cSFrederic Weisbecker 			break;
19261da177e4SLinus Torvalds 
1927fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1928f796937aSJeremy Fitzhardinge 			if (str < end)
19291da177e4SLinus Torvalds 				*str = '%';
19301da177e4SLinus Torvalds 			++str;
19311da177e4SLinus Torvalds 			break;
19321da177e4SLinus Torvalds 
1933fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1934f796937aSJeremy Fitzhardinge 			if (str < end)
19351da177e4SLinus Torvalds 				*str = '%';
19361da177e4SLinus Torvalds 			++str;
1937fef20d9cSFrederic Weisbecker 			break;
1938fef20d9cSFrederic Weisbecker 
1939fef20d9cSFrederic Weisbecker 		default:
1940fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1941fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1942fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1943fef20d9cSFrederic Weisbecker 				break;
1944fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1945fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1946fef20d9cSFrederic Weisbecker 				break;
1947fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1948fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1949fef20d9cSFrederic Weisbecker 				break;
1950fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1951ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
1952ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
1953ef124960SJason Gunthorpe 				else
1954fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
1955fef20d9cSFrederic Weisbecker 				break;
1956fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1957fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1958fef20d9cSFrederic Weisbecker 				break;
1959a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1960a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1961a4e94ef0SZhaolei 				break;
1962a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1963a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1964a4e94ef0SZhaolei 				break;
1965fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1966fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1967fef20d9cSFrederic Weisbecker 				break;
1968fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1969fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1970fef20d9cSFrederic Weisbecker 				break;
197139e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
197239e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1973fef20d9cSFrederic Weisbecker 				break;
1974fef20d9cSFrederic Weisbecker 			default:
1975fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
19761da177e4SLinus Torvalds 			}
1977fef20d9cSFrederic Weisbecker 
1978fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
19791da177e4SLinus Torvalds 		}
1980fef20d9cSFrederic Weisbecker 	}
1981fef20d9cSFrederic Weisbecker 
1982f796937aSJeremy Fitzhardinge 	if (size > 0) {
1983f796937aSJeremy Fitzhardinge 		if (str < end)
19841da177e4SLinus Torvalds 			*str = '\0';
1985f796937aSJeremy Fitzhardinge 		else
19860a6047eeSLinus Torvalds 			end[-1] = '\0';
1987f796937aSJeremy Fitzhardinge 	}
1988fef20d9cSFrederic Weisbecker 
1989f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
19901da177e4SLinus Torvalds 	return str-buf;
1991fef20d9cSFrederic Weisbecker 
19921da177e4SLinus Torvalds }
19931da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
19941da177e4SLinus Torvalds 
19951da177e4SLinus Torvalds /**
19961da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
19971da177e4SLinus Torvalds  * @buf: The buffer to place the result into
19981da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
19991da177e4SLinus Torvalds  * @fmt: The format string to use
20001da177e4SLinus Torvalds  * @args: Arguments for the format string
20011da177e4SLinus Torvalds  *
20021da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
2003b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
20041da177e4SLinus Torvalds  * returns 0.
20051da177e4SLinus Torvalds  *
2006ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
200720036fdcSAndi Kleen  *
200820036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20091da177e4SLinus Torvalds  */
20101da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
20111da177e4SLinus Torvalds {
20121da177e4SLinus Torvalds 	int i;
20131da177e4SLinus Torvalds 
20141da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
20157b9186f5SAndré Goddard Rosa 
2016b921c69fSAnton Arapov 	if (likely(i < size))
2017b921c69fSAnton Arapov 		return i;
2018b921c69fSAnton Arapov 	if (size != 0)
2019b921c69fSAnton Arapov 		return size - 1;
2020b921c69fSAnton Arapov 	return 0;
20211da177e4SLinus Torvalds }
20221da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds /**
20251da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
20261da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20271da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20281da177e4SLinus Torvalds  * @fmt: The format string to use
20291da177e4SLinus Torvalds  * @...: Arguments for the format string
20301da177e4SLinus Torvalds  *
20311da177e4SLinus Torvalds  * The return value is the number of characters which would be
20321da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
20331da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
20341da177e4SLinus Torvalds  * @size, the resulting string is truncated.
203520036fdcSAndi Kleen  *
203620036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20371da177e4SLinus Torvalds  */
20381da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
20391da177e4SLinus Torvalds {
20401da177e4SLinus Torvalds 	va_list args;
20411da177e4SLinus Torvalds 	int i;
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	va_start(args, fmt);
20441da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
20451da177e4SLinus Torvalds 	va_end(args);
20467b9186f5SAndré Goddard Rosa 
20471da177e4SLinus Torvalds 	return i;
20481da177e4SLinus Torvalds }
20491da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds /**
20521da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
20531da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20541da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20551da177e4SLinus Torvalds  * @fmt: The format string to use
20561da177e4SLinus Torvalds  * @...: Arguments for the format string
20571da177e4SLinus Torvalds  *
20581da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
2059b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
20601da177e4SLinus Torvalds  */
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
20631da177e4SLinus Torvalds {
20641da177e4SLinus Torvalds 	va_list args;
20651da177e4SLinus Torvalds 	int i;
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 	va_start(args, fmt);
2068b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
20691da177e4SLinus Torvalds 	va_end(args);
20707b9186f5SAndré Goddard Rosa 
2071b903c0b8SChangli Gao 	return i;
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds /**
20761da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
20771da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20781da177e4SLinus Torvalds  * @fmt: The format string to use
20791da177e4SLinus Torvalds  * @args: Arguments for the format string
20801da177e4SLinus Torvalds  *
20811da177e4SLinus Torvalds  * The function returns the number of characters written
208272fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
20831da177e4SLinus Torvalds  * buffer overflows.
20841da177e4SLinus Torvalds  *
2085ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
208620036fdcSAndi Kleen  *
208720036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20881da177e4SLinus Torvalds  */
20891da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
20901da177e4SLinus Torvalds {
20911da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
20921da177e4SLinus Torvalds }
20931da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds /**
20961da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
20971da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20981da177e4SLinus Torvalds  * @fmt: The format string to use
20991da177e4SLinus Torvalds  * @...: Arguments for the format string
21001da177e4SLinus Torvalds  *
21011da177e4SLinus Torvalds  * The function returns the number of characters written
210272fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
21031da177e4SLinus Torvalds  * buffer overflows.
210420036fdcSAndi Kleen  *
210520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
21061da177e4SLinus Torvalds  */
21071da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
21081da177e4SLinus Torvalds {
21091da177e4SLinus Torvalds 	va_list args;
21101da177e4SLinus Torvalds 	int i;
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds 	va_start(args, fmt);
21131da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
21141da177e4SLinus Torvalds 	va_end(args);
21157b9186f5SAndré Goddard Rosa 
21161da177e4SLinus Torvalds 	return i;
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
21191da177e4SLinus Torvalds 
21204370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
21214370aa4aSLai Jiangshan /*
21224370aa4aSLai Jiangshan  * bprintf service:
21234370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
21244370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
21254370aa4aSLai Jiangshan  */
21264370aa4aSLai Jiangshan 
21274370aa4aSLai Jiangshan /**
21284370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
21294370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
21304370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
21314370aa4aSLai Jiangshan  * @fmt: The format string to use
21324370aa4aSLai Jiangshan  * @args: Arguments for the format string
21334370aa4aSLai Jiangshan  *
21344370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
2135da3dae54SMasanari Iida  * is skipped.
21364370aa4aSLai Jiangshan  *
21374370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
21384370aa4aSLai Jiangshan  * the given input.
21394370aa4aSLai Jiangshan  *
21404370aa4aSLai Jiangshan  * NOTE:
21414370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
21424370aa4aSLai Jiangshan  * valid for bstr_printf().
21434370aa4aSLai Jiangshan  */
21444370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
21454370aa4aSLai Jiangshan {
2146fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
21474370aa4aSLai Jiangshan 	char *str, *end;
21484370aa4aSLai Jiangshan 
21494370aa4aSLai Jiangshan 	str = (char *)bin_buf;
21504370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
21514370aa4aSLai Jiangshan 
21524370aa4aSLai Jiangshan #define save_arg(type)							\
21534370aa4aSLai Jiangshan do {									\
21544370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
21554370aa4aSLai Jiangshan 		unsigned long long value;				\
21564370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
21574370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
21584370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
21594370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
21604370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
21614370aa4aSLai Jiangshan 		}							\
21624370aa4aSLai Jiangshan 	} else {							\
21634370aa4aSLai Jiangshan 		unsigned long value;					\
21644370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
21654370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
21664370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
21674370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
21684370aa4aSLai Jiangshan 	}								\
21694370aa4aSLai Jiangshan 	str += sizeof(type);						\
21704370aa4aSLai Jiangshan } while (0)
21714370aa4aSLai Jiangshan 
2172fef20d9cSFrederic Weisbecker 	while (*fmt) {
2173d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
21744370aa4aSLai Jiangshan 
2175fef20d9cSFrederic Weisbecker 		fmt += read;
2176fef20d9cSFrederic Weisbecker 
2177fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2178fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
2179d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_INVALID:
2180d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
2181fef20d9cSFrederic Weisbecker 			break;
2182fef20d9cSFrederic Weisbecker 
2183ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2184fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
21854370aa4aSLai Jiangshan 			save_arg(int);
2186fef20d9cSFrederic Weisbecker 			break;
21874370aa4aSLai Jiangshan 
2188fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
21894370aa4aSLai Jiangshan 			save_arg(char);
2190fef20d9cSFrederic Weisbecker 			break;
2191fef20d9cSFrederic Weisbecker 
2192fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
21934370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
21944370aa4aSLai Jiangshan 			size_t len;
21956c356634SAndré Goddard Rosa 
21964370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
21974370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
21980f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
21996c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
22006c356634SAndré Goddard Rosa 			if (str + len < end)
22016c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
22026c356634SAndré Goddard Rosa 			str += len;
2203fef20d9cSFrederic Weisbecker 			break;
22044370aa4aSLai Jiangshan 		}
2205fef20d9cSFrederic Weisbecker 
2206fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
22074370aa4aSLai Jiangshan 			save_arg(void *);
22084370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2209fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
22104370aa4aSLai Jiangshan 				fmt++;
2211fef20d9cSFrederic Weisbecker 			break;
2212fef20d9cSFrederic Weisbecker 
2213fef20d9cSFrederic Weisbecker 		default:
2214fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2215fef20d9cSFrederic Weisbecker 
2216fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2217fef20d9cSFrederic Weisbecker 				save_arg(long long);
2218fef20d9cSFrederic Weisbecker 				break;
2219fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2220fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2221fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
2222fef20d9cSFrederic Weisbecker 				break;
2223fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2224fef20d9cSFrederic Weisbecker 				save_arg(size_t);
2225fef20d9cSFrederic Weisbecker 				break;
2226fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2227fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
2228fef20d9cSFrederic Weisbecker 				break;
2229a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2230a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2231a4e94ef0SZhaolei 				save_arg(char);
2232a4e94ef0SZhaolei 				break;
2233fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2234fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2235fef20d9cSFrederic Weisbecker 				save_arg(short);
2236fef20d9cSFrederic Weisbecker 				break;
2237fef20d9cSFrederic Weisbecker 			default:
2238fef20d9cSFrederic Weisbecker 				save_arg(int);
2239fef20d9cSFrederic Weisbecker 			}
2240fef20d9cSFrederic Weisbecker 		}
2241fef20d9cSFrederic Weisbecker 	}
2242fef20d9cSFrederic Weisbecker 
22437b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2244fef20d9cSFrederic Weisbecker #undef save_arg
22454370aa4aSLai Jiangshan }
22464370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
22474370aa4aSLai Jiangshan 
22484370aa4aSLai Jiangshan /**
22494370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
22504370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
22514370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
22524370aa4aSLai Jiangshan  * @fmt: The format string to use
22534370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
22544370aa4aSLai Jiangshan  *
22554370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
22564370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
22574370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
22584370aa4aSLai Jiangshan  *
22594370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
22600efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
22614370aa4aSLai Jiangshan  *
22624370aa4aSLai Jiangshan  * The return value is the number of characters which would
22634370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
22644370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
22654370aa4aSLai Jiangshan  * number of characters written into @buf as return value
22664370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
22674370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
22684370aa4aSLai Jiangshan  * string is truncated.
22694370aa4aSLai Jiangshan  */
22704370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
22714370aa4aSLai Jiangshan {
2272fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
2273d4be151bSAndré Goddard Rosa 	char *str, *end;
2274d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
22754370aa4aSLai Jiangshan 
22762f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
22774370aa4aSLai Jiangshan 		return 0;
22784370aa4aSLai Jiangshan 
22794370aa4aSLai Jiangshan 	str = buf;
22804370aa4aSLai Jiangshan 	end = buf + size;
22814370aa4aSLai Jiangshan 
22824370aa4aSLai Jiangshan #define get_arg(type)							\
22834370aa4aSLai Jiangshan ({									\
22844370aa4aSLai Jiangshan 	typeof(type) value;						\
22854370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
22864370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
22874370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
22884370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
22894370aa4aSLai Jiangshan 	} else {							\
22904370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
22914370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
22924370aa4aSLai Jiangshan 	}								\
22934370aa4aSLai Jiangshan 	args += sizeof(type);						\
22944370aa4aSLai Jiangshan 	value;								\
22954370aa4aSLai Jiangshan })
22964370aa4aSLai Jiangshan 
22974370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
22984370aa4aSLai Jiangshan 	if (end < buf) {
22994370aa4aSLai Jiangshan 		end = ((void *)-1);
23004370aa4aSLai Jiangshan 		size = end - buf;
23014370aa4aSLai Jiangshan 	}
23024370aa4aSLai Jiangshan 
2303fef20d9cSFrederic Weisbecker 	while (*fmt) {
2304fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2305d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2306fef20d9cSFrederic Weisbecker 
2307fef20d9cSFrederic Weisbecker 		fmt += read;
2308fef20d9cSFrederic Weisbecker 
2309fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2310fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2311fef20d9cSFrederic Weisbecker 			int copy = read;
2312fef20d9cSFrederic Weisbecker 			if (str < end) {
2313fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2314fef20d9cSFrederic Weisbecker 					copy = end - str;
2315fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2316fef20d9cSFrederic Weisbecker 			}
2317fef20d9cSFrederic Weisbecker 			str += read;
2318fef20d9cSFrederic Weisbecker 			break;
23194370aa4aSLai Jiangshan 		}
23204370aa4aSLai Jiangshan 
2321ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2322fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
2323fef20d9cSFrederic Weisbecker 			break;
23244370aa4aSLai Jiangshan 
2325fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2326fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
2327fef20d9cSFrederic Weisbecker 			break;
23284370aa4aSLai Jiangshan 
2329d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2330d4be151bSAndré Goddard Rosa 			char c;
2331d4be151bSAndré Goddard Rosa 
2332fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2333fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
23344370aa4aSLai Jiangshan 					if (str < end)
23354370aa4aSLai Jiangshan 						*str = ' ';
23364370aa4aSLai Jiangshan 					++str;
23374370aa4aSLai Jiangshan 				}
23384370aa4aSLai Jiangshan 			}
23394370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
23404370aa4aSLai Jiangshan 			if (str < end)
23414370aa4aSLai Jiangshan 				*str = c;
23424370aa4aSLai Jiangshan 			++str;
2343fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
23444370aa4aSLai Jiangshan 				if (str < end)
23454370aa4aSLai Jiangshan 					*str = ' ';
23464370aa4aSLai Jiangshan 				++str;
23474370aa4aSLai Jiangshan 			}
2348fef20d9cSFrederic Weisbecker 			break;
2349d4be151bSAndré Goddard Rosa 		}
23504370aa4aSLai Jiangshan 
2351fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
23524370aa4aSLai Jiangshan 			const char *str_arg = args;
2353d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
2354fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
2355fef20d9cSFrederic Weisbecker 			break;
23564370aa4aSLai Jiangshan 		}
23574370aa4aSLai Jiangshan 
2358fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2359ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, get_arg(void *), spec);
2360fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
23614370aa4aSLai Jiangshan 				fmt++;
2362fef20d9cSFrederic Weisbecker 			break;
23634370aa4aSLai Jiangshan 
2364fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2365fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
23664370aa4aSLai Jiangshan 			if (str < end)
23674370aa4aSLai Jiangshan 				*str = '%';
23684370aa4aSLai Jiangshan 			++str;
2369fef20d9cSFrederic Weisbecker 			break;
2370fef20d9cSFrederic Weisbecker 
2371d4be151bSAndré Goddard Rosa 		default: {
2372d4be151bSAndré Goddard Rosa 			unsigned long long num;
2373d4be151bSAndré Goddard Rosa 
2374fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2375fef20d9cSFrederic Weisbecker 
2376fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
23774370aa4aSLai Jiangshan 				num = get_arg(long long);
2378fef20d9cSFrederic Weisbecker 				break;
2379fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2380fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2381fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
2382fef20d9cSFrederic Weisbecker 				break;
2383fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
23844370aa4aSLai Jiangshan 				num = get_arg(size_t);
2385fef20d9cSFrederic Weisbecker 				break;
2386fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
23874370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
2388fef20d9cSFrederic Weisbecker 				break;
2389a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2390a4e94ef0SZhaolei 				num = get_arg(unsigned char);
2391a4e94ef0SZhaolei 				break;
2392a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2393a4e94ef0SZhaolei 				num = get_arg(signed char);
2394a4e94ef0SZhaolei 				break;
2395fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2396fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
2397fef20d9cSFrederic Weisbecker 				break;
2398fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2399fef20d9cSFrederic Weisbecker 				num = get_arg(short);
2400fef20d9cSFrederic Weisbecker 				break;
2401fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
24024370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
2403fef20d9cSFrederic Weisbecker 				break;
2404fef20d9cSFrederic Weisbecker 			default:
2405fef20d9cSFrederic Weisbecker 				num = get_arg(int);
24064370aa4aSLai Jiangshan 			}
2407fef20d9cSFrederic Weisbecker 
2408fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
2409d4be151bSAndré Goddard Rosa 		} /* default: */
2410d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
2411d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
2412fef20d9cSFrederic Weisbecker 
24134370aa4aSLai Jiangshan 	if (size > 0) {
24144370aa4aSLai Jiangshan 		if (str < end)
24154370aa4aSLai Jiangshan 			*str = '\0';
24164370aa4aSLai Jiangshan 		else
24174370aa4aSLai Jiangshan 			end[-1] = '\0';
24184370aa4aSLai Jiangshan 	}
2419fef20d9cSFrederic Weisbecker 
24204370aa4aSLai Jiangshan #undef get_arg
24214370aa4aSLai Jiangshan 
24224370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
24234370aa4aSLai Jiangshan 	return str - buf;
24244370aa4aSLai Jiangshan }
24254370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
24264370aa4aSLai Jiangshan 
24274370aa4aSLai Jiangshan /**
24284370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
24294370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
24304370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
24314370aa4aSLai Jiangshan  * @fmt: The format string to use
24324370aa4aSLai Jiangshan  * @...: Arguments for the format string
24334370aa4aSLai Jiangshan  *
24344370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
24354370aa4aSLai Jiangshan  * into @bin_buf.
24364370aa4aSLai Jiangshan  */
24374370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
24384370aa4aSLai Jiangshan {
24394370aa4aSLai Jiangshan 	va_list args;
24404370aa4aSLai Jiangshan 	int ret;
24414370aa4aSLai Jiangshan 
24424370aa4aSLai Jiangshan 	va_start(args, fmt);
24434370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
24444370aa4aSLai Jiangshan 	va_end(args);
24457b9186f5SAndré Goddard Rosa 
24464370aa4aSLai Jiangshan 	return ret;
24474370aa4aSLai Jiangshan }
24484370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
24494370aa4aSLai Jiangshan 
24504370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
24514370aa4aSLai Jiangshan 
24521da177e4SLinus Torvalds /**
24531da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
24541da177e4SLinus Torvalds  * @buf:	input buffer
24551da177e4SLinus Torvalds  * @fmt:	format of buffer
24561da177e4SLinus Torvalds  * @args:	arguments
24571da177e4SLinus Torvalds  */
24581da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
24591da177e4SLinus Torvalds {
24601da177e4SLinus Torvalds 	const char *str = buf;
24611da177e4SLinus Torvalds 	char *next;
24621da177e4SLinus Torvalds 	char digit;
24631da177e4SLinus Torvalds 	int num = 0;
2464ef0658f3SJoe Perches 	u8 qualifier;
246553809751SJan Beulich 	unsigned int base;
246653809751SJan Beulich 	union {
246753809751SJan Beulich 		long long s;
246853809751SJan Beulich 		unsigned long long u;
246953809751SJan Beulich 	} val;
2470ef0658f3SJoe Perches 	s16 field_width;
2471d4be151bSAndré Goddard Rosa 	bool is_sign;
24721da177e4SLinus Torvalds 
2473da99075cSJan Beulich 	while (*fmt) {
24741da177e4SLinus Torvalds 		/* skip any white space in format */
24751da177e4SLinus Torvalds 		/* white space in format matchs any amount of
24761da177e4SLinus Torvalds 		 * white space, including none, in the input.
24771da177e4SLinus Torvalds 		 */
24781da177e4SLinus Torvalds 		if (isspace(*fmt)) {
2479e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
2480e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
24811da177e4SLinus Torvalds 		}
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
24841da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
24851da177e4SLinus Torvalds 			if (*fmt++ != *str++)
24861da177e4SLinus Torvalds 				break;
24871da177e4SLinus Torvalds 			continue;
24881da177e4SLinus Torvalds 		}
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds 		if (!*fmt)
24911da177e4SLinus Torvalds 			break;
24921da177e4SLinus Torvalds 		++fmt;
24931da177e4SLinus Torvalds 
24941da177e4SLinus Torvalds 		/* skip this conversion.
24951da177e4SLinus Torvalds 		 * advance both strings to next white space
24961da177e4SLinus Torvalds 		 */
24971da177e4SLinus Torvalds 		if (*fmt == '*') {
2498da99075cSJan Beulich 			if (!*str)
2499da99075cSJan Beulich 				break;
25008fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
25011da177e4SLinus Torvalds 				fmt++;
25021da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
25031da177e4SLinus Torvalds 				str++;
25041da177e4SLinus Torvalds 			continue;
25051da177e4SLinus Torvalds 		}
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds 		/* get field width */
25081da177e4SLinus Torvalds 		field_width = -1;
250953809751SJan Beulich 		if (isdigit(*fmt)) {
25101da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
251153809751SJan Beulich 			if (field_width <= 0)
251253809751SJan Beulich 				break;
251353809751SJan Beulich 		}
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds 		/* get conversion qualifier */
25161da177e4SLinus Torvalds 		qualifier = -1;
251775fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
251875fb8f26SAndy Shevchenko 		    _tolower(*fmt) == 'z') {
25191da177e4SLinus Torvalds 			qualifier = *fmt++;
25201da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
25211da177e4SLinus Torvalds 				if (qualifier == 'h') {
25221da177e4SLinus Torvalds 					qualifier = 'H';
25231da177e4SLinus Torvalds 					fmt++;
25241da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
25251da177e4SLinus Torvalds 					qualifier = 'L';
25261da177e4SLinus Torvalds 					fmt++;
25271da177e4SLinus Torvalds 				}
25281da177e4SLinus Torvalds 			}
25291da177e4SLinus Torvalds 		}
25301da177e4SLinus Torvalds 
2531da99075cSJan Beulich 		if (!*fmt)
2532da99075cSJan Beulich 			break;
2533da99075cSJan Beulich 
2534da99075cSJan Beulich 		if (*fmt == 'n') {
2535da99075cSJan Beulich 			/* return number of characters read so far */
2536da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
2537da99075cSJan Beulich 			++fmt;
2538da99075cSJan Beulich 			continue;
2539da99075cSJan Beulich 		}
2540da99075cSJan Beulich 
2541da99075cSJan Beulich 		if (!*str)
25421da177e4SLinus Torvalds 			break;
25431da177e4SLinus Torvalds 
2544d4be151bSAndré Goddard Rosa 		base = 10;
25453f623ebaSFabian Frederick 		is_sign = false;
2546d4be151bSAndré Goddard Rosa 
25471da177e4SLinus Torvalds 		switch (*fmt++) {
25481da177e4SLinus Torvalds 		case 'c':
25491da177e4SLinus Torvalds 		{
25501da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
25511da177e4SLinus Torvalds 			if (field_width == -1)
25521da177e4SLinus Torvalds 				field_width = 1;
25531da177e4SLinus Torvalds 			do {
25541da177e4SLinus Torvalds 				*s++ = *str++;
25551da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
25561da177e4SLinus Torvalds 			num++;
25571da177e4SLinus Torvalds 		}
25581da177e4SLinus Torvalds 		continue;
25591da177e4SLinus Torvalds 		case 's':
25601da177e4SLinus Torvalds 		{
25611da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
25621da177e4SLinus Torvalds 			if (field_width == -1)
25634be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
25641da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
2565e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
25661da177e4SLinus Torvalds 
25671da177e4SLinus Torvalds 			/* now copy until next white space */
25687b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
25691da177e4SLinus Torvalds 				*s++ = *str++;
25701da177e4SLinus Torvalds 			*s = '\0';
25711da177e4SLinus Torvalds 			num++;
25721da177e4SLinus Torvalds 		}
25731da177e4SLinus Torvalds 		continue;
25741da177e4SLinus Torvalds 		case 'o':
25751da177e4SLinus Torvalds 			base = 8;
25761da177e4SLinus Torvalds 			break;
25771da177e4SLinus Torvalds 		case 'x':
25781da177e4SLinus Torvalds 		case 'X':
25791da177e4SLinus Torvalds 			base = 16;
25801da177e4SLinus Torvalds 			break;
25811da177e4SLinus Torvalds 		case 'i':
25821da177e4SLinus Torvalds 			base = 0;
25831da177e4SLinus Torvalds 		case 'd':
25843f623ebaSFabian Frederick 			is_sign = true;
25851da177e4SLinus Torvalds 		case 'u':
25861da177e4SLinus Torvalds 			break;
25871da177e4SLinus Torvalds 		case '%':
25881da177e4SLinus Torvalds 			/* looking for '%' in str */
25891da177e4SLinus Torvalds 			if (*str++ != '%')
25901da177e4SLinus Torvalds 				return num;
25911da177e4SLinus Torvalds 			continue;
25921da177e4SLinus Torvalds 		default:
25931da177e4SLinus Torvalds 			/* invalid format; stop here */
25941da177e4SLinus Torvalds 			return num;
25951da177e4SLinus Torvalds 		}
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 		/* have some sort of integer conversion.
25981da177e4SLinus Torvalds 		 * first, skip white space in buffer.
25991da177e4SLinus Torvalds 		 */
2600e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 		digit = *str;
26031da177e4SLinus Torvalds 		if (is_sign && digit == '-')
26041da177e4SLinus Torvalds 			digit = *(str + 1);
26051da177e4SLinus Torvalds 
26061da177e4SLinus Torvalds 		if (!digit
26071da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
26081da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
26091da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
26101da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
26111da177e4SLinus Torvalds 			break;
26121da177e4SLinus Torvalds 
261353809751SJan Beulich 		if (is_sign)
261453809751SJan Beulich 			val.s = qualifier != 'L' ?
261553809751SJan Beulich 				simple_strtol(str, &next, base) :
261653809751SJan Beulich 				simple_strtoll(str, &next, base);
261753809751SJan Beulich 		else
261853809751SJan Beulich 			val.u = qualifier != 'L' ?
261953809751SJan Beulich 				simple_strtoul(str, &next, base) :
262053809751SJan Beulich 				simple_strtoull(str, &next, base);
262153809751SJan Beulich 
262253809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
262353809751SJan Beulich 			if (base == 0)
262453809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
262553809751SJan Beulich 			while (next - str > field_width) {
262653809751SJan Beulich 				if (is_sign)
262753809751SJan Beulich 					val.s = div_s64(val.s, base);
262853809751SJan Beulich 				else
262953809751SJan Beulich 					val.u = div_u64(val.u, base);
263053809751SJan Beulich 				--next;
263153809751SJan Beulich 			}
263253809751SJan Beulich 		}
263353809751SJan Beulich 
26341da177e4SLinus Torvalds 		switch (qualifier) {
26351da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
263653809751SJan Beulich 			if (is_sign)
263753809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
263853809751SJan Beulich 			else
263953809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
26401da177e4SLinus Torvalds 			break;
26411da177e4SLinus Torvalds 		case 'h':
264253809751SJan Beulich 			if (is_sign)
264353809751SJan Beulich 				*va_arg(args, short *) = val.s;
264453809751SJan Beulich 			else
264553809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
26461da177e4SLinus Torvalds 			break;
26471da177e4SLinus Torvalds 		case 'l':
264853809751SJan Beulich 			if (is_sign)
264953809751SJan Beulich 				*va_arg(args, long *) = val.s;
265053809751SJan Beulich 			else
265153809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
26521da177e4SLinus Torvalds 			break;
26531da177e4SLinus Torvalds 		case 'L':
265453809751SJan Beulich 			if (is_sign)
265553809751SJan Beulich 				*va_arg(args, long long *) = val.s;
265653809751SJan Beulich 			else
265753809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
26581da177e4SLinus Torvalds 			break;
26591da177e4SLinus Torvalds 		case 'Z':
26601da177e4SLinus Torvalds 		case 'z':
266153809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
26621da177e4SLinus Torvalds 			break;
26631da177e4SLinus Torvalds 		default:
266453809751SJan Beulich 			if (is_sign)
266553809751SJan Beulich 				*va_arg(args, int *) = val.s;
266653809751SJan Beulich 			else
266753809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
26681da177e4SLinus Torvalds 			break;
26691da177e4SLinus Torvalds 		}
26701da177e4SLinus Torvalds 		num++;
26711da177e4SLinus Torvalds 
26721da177e4SLinus Torvalds 		if (!next)
26731da177e4SLinus Torvalds 			break;
26741da177e4SLinus Torvalds 		str = next;
26751da177e4SLinus Torvalds 	}
2676c6b40d16SJohannes Berg 
26771da177e4SLinus Torvalds 	return num;
26781da177e4SLinus Torvalds }
26791da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds /**
26821da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
26831da177e4SLinus Torvalds  * @buf:	input buffer
26841da177e4SLinus Torvalds  * @fmt:	formatting of buffer
26851da177e4SLinus Torvalds  * @...:	resulting arguments
26861da177e4SLinus Torvalds  */
26871da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
26881da177e4SLinus Torvalds {
26891da177e4SLinus Torvalds 	va_list args;
26901da177e4SLinus Torvalds 	int i;
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds 	va_start(args, fmt);
26931da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
26941da177e4SLinus Torvalds 	va_end(args);
26957b9186f5SAndré Goddard Rosa 
26961da177e4SLinus Torvalds 	return i;
26971da177e4SLinus Torvalds }
26981da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2699