xref: /openbmc/linux/lib/vsprintf.c (revision 675cf53c1deaffadc7b6a0b4afe6cdafec86bedb)
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() */
367c43d9a3SRasmus Villemoes #include <asm/byteorder.h>	/* cpu_to_le16 */
371da177e4SLinus Torvalds 
3871dca95dSAndy Shevchenko #include <linux/string_helpers.h>
391dff46d6SAlexey Dobriyan #include "kstrtox.h"
40aa46a63eSHarvey Harrison 
411da177e4SLinus Torvalds /**
421da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
431da177e4SLinus Torvalds  * @cp: The start of the string
441da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
451da177e4SLinus Torvalds  * @base: The number base to use
46462e4711SEldad Zack  *
47462e4711SEldad Zack  * This function is obsolete. Please use kstrtoull instead.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
501da177e4SLinus Torvalds {
511dff46d6SAlexey Dobriyan 	unsigned long long result;
521dff46d6SAlexey Dobriyan 	unsigned int rv;
531da177e4SLinus Torvalds 
541dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
551dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
561dff46d6SAlexey Dobriyan 	/* FIXME */
571dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
58aa46a63eSHarvey Harrison 
591da177e4SLinus Torvalds 	if (endp)
601da177e4SLinus Torvalds 		*endp = (char *)cp;
617b9186f5SAndré Goddard Rosa 
621da177e4SLinus Torvalds 	return result;
631da177e4SLinus Torvalds }
641da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /**
67922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
68922ac25cSAndré Goddard Rosa  * @cp: The start of the string
69922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
70922ac25cSAndré Goddard Rosa  * @base: The number base to use
71462e4711SEldad Zack  *
72462e4711SEldad Zack  * This function is obsolete. Please use kstrtoul instead.
73922ac25cSAndré Goddard Rosa  */
74922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
75922ac25cSAndré Goddard Rosa {
76922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
77922ac25cSAndré Goddard Rosa }
78922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
79922ac25cSAndré Goddard Rosa 
80922ac25cSAndré Goddard Rosa /**
81922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
82922ac25cSAndré Goddard Rosa  * @cp: The start of the string
83922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
84922ac25cSAndré Goddard Rosa  * @base: The number base to use
85462e4711SEldad Zack  *
86462e4711SEldad Zack  * This function is obsolete. Please use kstrtol instead.
87922ac25cSAndré Goddard Rosa  */
88922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
89922ac25cSAndré Goddard Rosa {
90922ac25cSAndré Goddard Rosa 	if (*cp == '-')
91922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
92922ac25cSAndré Goddard Rosa 
93922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
94922ac25cSAndré Goddard Rosa }
95922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
96922ac25cSAndré Goddard Rosa 
97922ac25cSAndré Goddard Rosa /**
981da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
991da177e4SLinus Torvalds  * @cp: The start of the string
1001da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1011da177e4SLinus Torvalds  * @base: The number base to use
102462e4711SEldad Zack  *
103462e4711SEldad Zack  * This function is obsolete. Please use kstrtoll instead.
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	if (*cp == '-')
1081da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1097b9186f5SAndré Goddard Rosa 
1101da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1111da177e4SLinus Torvalds }
11298d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1131da177e4SLinus Torvalds 
114cf3b429bSJoe Perches static noinline_for_stack
115cf3b429bSJoe Perches int skip_atoi(const char **s)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	int i = 0;
1181da177e4SLinus Torvalds 
11943e5b666SRasmus Villemoes 	do {
1201da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
12143e5b666SRasmus Villemoes 	} while (isdigit(**s));
1227b9186f5SAndré Goddard Rosa 
1231da177e4SLinus Torvalds 	return i;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1267c43d9a3SRasmus Villemoes /*
1277c43d9a3SRasmus Villemoes  * Decimal conversion is by far the most typical, and is used for
1287c43d9a3SRasmus Villemoes  * /proc and /sys data. This directly impacts e.g. top performance
1297c43d9a3SRasmus Villemoes  * with many processes running. We optimize it for speed by emitting
1307c43d9a3SRasmus Villemoes  * two characters at a time, using a 200 byte lookup table. This
1317c43d9a3SRasmus Villemoes  * roughly halves the number of multiplications compared to computing
1327c43d9a3SRasmus Villemoes  * the digits one at a time. Implementation strongly inspired by the
1337c43d9a3SRasmus Villemoes  * previous version, which in turn used ideas described at
1347c43d9a3SRasmus Villemoes  * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
1357c43d9a3SRasmus Villemoes  * from the author, Douglas W. Jones).
1367c43d9a3SRasmus Villemoes  *
1377c43d9a3SRasmus Villemoes  * It turns out there is precisely one 26 bit fixed-point
1387c43d9a3SRasmus Villemoes  * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
1397c43d9a3SRasmus Villemoes  * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
1407c43d9a3SRasmus Villemoes  * range happens to be somewhat larger (x <= 1073741898), but that's
1417c43d9a3SRasmus Villemoes  * irrelevant for our purpose.
1427c43d9a3SRasmus Villemoes  *
1437c43d9a3SRasmus Villemoes  * For dividing a number in the range [10^4, 10^6-1] by 100, we still
1447c43d9a3SRasmus Villemoes  * need a 32x32->64 bit multiply, so we simply use the same constant.
1457c43d9a3SRasmus Villemoes  *
1467c43d9a3SRasmus Villemoes  * For dividing a number in the range [100, 10^4-1] by 100, there are
1477c43d9a3SRasmus Villemoes  * several options. The simplest is (x * 0x147b) >> 19, which is valid
1487c43d9a3SRasmus Villemoes  * for all x <= 43698.
149133fd9f5SDenys Vlasenko  */
1504277eeddSDenis Vlasenko 
1517c43d9a3SRasmus Villemoes static const u16 decpair[100] = {
1527c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
1537c43d9a3SRasmus Villemoes 	_( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
1547c43d9a3SRasmus Villemoes 	_(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
1557c43d9a3SRasmus Villemoes 	_(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
1567c43d9a3SRasmus Villemoes 	_(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
1577c43d9a3SRasmus Villemoes 	_(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
1587c43d9a3SRasmus Villemoes 	_(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
1597c43d9a3SRasmus Villemoes 	_(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
1607c43d9a3SRasmus Villemoes 	_(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
1617c43d9a3SRasmus Villemoes 	_(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
1627c43d9a3SRasmus Villemoes 	_(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
1637c43d9a3SRasmus Villemoes #undef _
1647c43d9a3SRasmus Villemoes };
1654277eeddSDenis Vlasenko 
1667b9186f5SAndré Goddard Rosa /*
1677c43d9a3SRasmus Villemoes  * This will print a single '0' even if r == 0, since we would
168*675cf53cSRasmus Villemoes  * immediately jump to out_r where two 0s would be written but only
169*675cf53cSRasmus Villemoes  * one of them accounted for in buf. This is needed by ip4_string
170*675cf53cSRasmus Villemoes  * below. All other callers pass a non-zero value of r.
171133fd9f5SDenys Vlasenko */
172133fd9f5SDenys Vlasenko static noinline_for_stack
173133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
174133fd9f5SDenys Vlasenko {
175133fd9f5SDenys Vlasenko 	unsigned q;
1764277eeddSDenis Vlasenko 
1777c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^8 */
1787c43d9a3SRasmus Villemoes 	if (r < 100)
1797c43d9a3SRasmus Villemoes 		goto out_r;
180cb239d0aSGeorge Spelvin 
1817c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^8 */
1827c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
1837c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
1847c43d9a3SRasmus Villemoes 	buf += 2;
1857c43d9a3SRasmus Villemoes 
1867c43d9a3SRasmus Villemoes 	/* 1 <= q < 10^6 */
1877c43d9a3SRasmus Villemoes 	if (q < 100)
1887c43d9a3SRasmus Villemoes 		goto out_q;
1897c43d9a3SRasmus Villemoes 
1907c43d9a3SRasmus Villemoes 	/*  100 <= q < 10^6 */
1917c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
1927c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
1937c43d9a3SRasmus Villemoes 	buf += 2;
1947c43d9a3SRasmus Villemoes 
1957c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^4 */
1967c43d9a3SRasmus Villemoes 	if (r < 100)
1977c43d9a3SRasmus Villemoes 		goto out_r;
1987c43d9a3SRasmus Villemoes 
1997c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^4 */
2007c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2017c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2027c43d9a3SRasmus Villemoes 	buf += 2;
2037c43d9a3SRasmus Villemoes out_q:
2047c43d9a3SRasmus Villemoes 	/* 1 <= q < 100 */
2057c43d9a3SRasmus Villemoes 	r = q;
2067c43d9a3SRasmus Villemoes out_r:
2077c43d9a3SRasmus Villemoes 	/* 1 <= r < 100 */
2087c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r];
209*675cf53cSRasmus Villemoes 	buf += r < 10 ? 1 : 2;
210133fd9f5SDenys Vlasenko 	return buf;
211133fd9f5SDenys Vlasenko }
212133fd9f5SDenys Vlasenko 
2137c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
2147c43d9a3SRasmus Villemoes static noinline_for_stack
2157c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r)
2167c43d9a3SRasmus Villemoes {
2177c43d9a3SRasmus Villemoes 	unsigned q;
218133fd9f5SDenys Vlasenko 
2197c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^8 */
2207c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
2217c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2227c43d9a3SRasmus Villemoes 	buf += 2;
223133fd9f5SDenys Vlasenko 
2247c43d9a3SRasmus Villemoes 	/* 0 <= q < 10^6 */
2257c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
2267c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
2277c43d9a3SRasmus Villemoes 	buf += 2;
228133fd9f5SDenys Vlasenko 
2297c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2307c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2317c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2327c43d9a3SRasmus Villemoes 	buf += 2;
2337c43d9a3SRasmus Villemoes 
2347c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2357c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2367c43d9a3SRasmus Villemoes 	buf += 2;
2377c43d9a3SRasmus Villemoes 	return buf;
2387c43d9a3SRasmus Villemoes }
2397c43d9a3SRasmus Villemoes 
2407c43d9a3SRasmus Villemoes static noinline_for_stack
241133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
242133fd9f5SDenys Vlasenko {
243133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000)
2447c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2457c43d9a3SRasmus Villemoes 	/* 1 <= n <= 1.6e11 */
2467c43d9a3SRasmus Villemoes 	if (n >= 100*1000*1000)
2477c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2487c43d9a3SRasmus Villemoes 	/* 1 <= n < 1e8 */
249133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
250133fd9f5SDenys Vlasenko }
251133fd9f5SDenys Vlasenko 
2527c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
253133fd9f5SDenys Vlasenko 
2547c43d9a3SRasmus Villemoes static void
2557c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r)
256133fd9f5SDenys Vlasenko {
2577c43d9a3SRasmus Villemoes 	unsigned q;
2587c43d9a3SRasmus Villemoes 
2597c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2607c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2617c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2627c43d9a3SRasmus Villemoes 	buf += 2;
2637c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2647c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2652359172aSGeorge Spelvin }
2662359172aSGeorge Spelvin 
2672359172aSGeorge Spelvin /*
2682359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2692359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2702359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2712359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
2727c43d9a3SRasmus Villemoes  * (second call in the put_dec code, assuming n is all-ones).
2732359172aSGeorge Spelvin  */
2747c43d9a3SRasmus Villemoes static noinline_for_stack
2752359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2762359172aSGeorge Spelvin {
2772359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2782359172aSGeorge Spelvin 
2792359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2802359172aSGeorge Spelvin         return q;
281133fd9f5SDenys Vlasenko }
282133fd9f5SDenys Vlasenko 
283133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
284133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
285133fd9f5SDenys Vlasenko  * (with permission from the author).
286133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
287133fd9f5SDenys Vlasenko  */
288133fd9f5SDenys Vlasenko static
289133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
290133fd9f5SDenys Vlasenko {
291133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
292133fd9f5SDenys Vlasenko 
293133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
294133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
295133fd9f5SDenys Vlasenko 
296133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
297133fd9f5SDenys Vlasenko 	h   = (n >> 32);
298133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
299133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
300133fd9f5SDenys Vlasenko 
3017c43d9a3SRasmus Villemoes 	/* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
3027c43d9a3SRasmus Villemoes 	     = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
303133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
3042359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
305133fd9f5SDenys Vlasenko 
3062359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
3072359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
308133fd9f5SDenys Vlasenko 
3092359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
3102359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
311133fd9f5SDenys Vlasenko 
3122359172aSGeorge Spelvin 	q += 281 * d3;
3132359172aSGeorge Spelvin 	buf += 12;
3142359172aSGeorge Spelvin 	if (q)
3152359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3162359172aSGeorge Spelvin 	else while (buf[-1] == '0')
317133fd9f5SDenys Vlasenko 		--buf;
3187b9186f5SAndré Goddard Rosa 
3194277eeddSDenis Vlasenko 	return buf;
3204277eeddSDenis Vlasenko }
321133fd9f5SDenys Vlasenko 
322133fd9f5SDenys Vlasenko #endif
3234277eeddSDenis Vlasenko 
3241ac101a5SKAMEZAWA Hiroyuki /*
3251ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3261ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3271ac101a5SKAMEZAWA Hiroyuki  *
3281ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3291ac101a5SKAMEZAWA Hiroyuki  */
3301ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num)
3311ac101a5SKAMEZAWA Hiroyuki {
3327c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
3337c43d9a3SRasmus Villemoes 	char tmp[sizeof(num) * 3] __aligned(2);
3341ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3351ac101a5SKAMEZAWA Hiroyuki 
336133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
337133fd9f5SDenys Vlasenko 	if (num <= 9) {
338133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
339133fd9f5SDenys Vlasenko 		len = 1;
340133fd9f5SDenys Vlasenko 	} else {
3411ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
342133fd9f5SDenys Vlasenko 	}
3431ac101a5SKAMEZAWA Hiroyuki 
3441ac101a5SKAMEZAWA Hiroyuki 	if (len > size)
3451ac101a5SKAMEZAWA Hiroyuki 		return 0;
3461ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
3471ac101a5SKAMEZAWA Hiroyuki 		buf[idx] = tmp[len - idx - 1];
3481ac101a5SKAMEZAWA Hiroyuki 	return len;
3491ac101a5SKAMEZAWA Hiroyuki }
3501ac101a5SKAMEZAWA Hiroyuki 
35151be17dfSRasmus Villemoes #define SIGN	1		/* unsigned/signed, must be 1 */
352d1c1b121SRasmus Villemoes #define LEFT	2		/* left justified */
3531da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3541da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
355d1c1b121SRasmus Villemoes #define ZEROPAD	16		/* pad with zero, must be 16 == '0' - ' ' */
356b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
357b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3581da177e4SLinus Torvalds 
359fef20d9cSFrederic Weisbecker enum format_type {
360fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
361ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
362fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
363fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
364fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
365fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
366fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
367fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
368fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
369fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
370fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
371a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
372a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
373fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
374fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
375fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
376fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
377fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
378fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
379fef20d9cSFrederic Weisbecker };
380fef20d9cSFrederic Weisbecker 
381fef20d9cSFrederic Weisbecker struct printf_spec {
3824e310fdaSJoe Perches 	u8	type;		/* format_type enum */
383ef0658f3SJoe Perches 	u8	flags;		/* flags to number() */
3844e310fdaSJoe Perches 	u8	base;		/* number base, 8, 10 or 16 only */
3854e310fdaSJoe Perches 	u8	qualifier;	/* number qualifier, one of 'hHlLtzZ' */
3864e310fdaSJoe Perches 	s16	field_width;	/* width of output field */
3874e310fdaSJoe Perches 	s16	precision;	/* # of digits/chars */
388fef20d9cSFrederic Weisbecker };
389fef20d9cSFrederic Weisbecker 
390cf3b429bSJoe Perches static noinline_for_stack
391cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
392fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
3931da177e4SLinus Torvalds {
3947c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
3957c43d9a3SRasmus Villemoes 	char tmp[3 * sizeof(num)] __aligned(2);
3969b706aeeSDenys Vlasenko 	char sign;
3979b706aeeSDenys Vlasenko 	char locase;
398fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
3991da177e4SLinus Torvalds 	int i;
4007c203422SPierre Carrier 	bool is_zero = num == 0LL;
4011da177e4SLinus Torvalds 
4029b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4039b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
404fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
405fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
406fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4071da177e4SLinus Torvalds 	sign = 0;
408fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4091da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4101da177e4SLinus Torvalds 			sign = '-';
4111da177e4SLinus Torvalds 			num = -(signed long long)num;
412fef20d9cSFrederic Weisbecker 			spec.field_width--;
413fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4141da177e4SLinus Torvalds 			sign = '+';
415fef20d9cSFrederic Weisbecker 			spec.field_width--;
416fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4171da177e4SLinus Torvalds 			sign = ' ';
418fef20d9cSFrederic Weisbecker 			spec.field_width--;
4191da177e4SLinus Torvalds 		}
4201da177e4SLinus Torvalds 	}
421b39a7340SDenis Vlasenko 	if (need_pfx) {
422fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4237c203422SPierre Carrier 			spec.field_width -= 2;
4247c203422SPierre Carrier 		else if (!is_zero)
425fef20d9cSFrederic Weisbecker 			spec.field_width--;
4261da177e4SLinus Torvalds 	}
427b39a7340SDenis Vlasenko 
428b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4291da177e4SLinus Torvalds 	i = 0;
430133fd9f5SDenys Vlasenko 	if (num < spec.base)
4313ea8d440SRasmus Villemoes 		tmp[i++] = hex_asc_upper[num] | locase;
432fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
433fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
434b39a7340SDenis Vlasenko 		int shift = 3;
4357b9186f5SAndré Goddard Rosa 
4367b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4377b9186f5SAndré Goddard Rosa 			shift = 4;
438b39a7340SDenis Vlasenko 		do {
4393ea8d440SRasmus Villemoes 			tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
440b39a7340SDenis Vlasenko 			num >>= shift;
441b39a7340SDenis Vlasenko 		} while (num);
4424277eeddSDenis Vlasenko 	} else { /* base 10 */
4434277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4444277eeddSDenis Vlasenko 	}
445b39a7340SDenis Vlasenko 
446b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
447fef20d9cSFrederic Weisbecker 	if (i > spec.precision)
448fef20d9cSFrederic Weisbecker 		spec.precision = i;
449b39a7340SDenis Vlasenko 	/* leading space padding */
450fef20d9cSFrederic Weisbecker 	spec.field_width -= spec.precision;
45151be17dfSRasmus Villemoes 	if (!(spec.flags & (ZEROPAD | LEFT))) {
452fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
453f796937aSJeremy Fitzhardinge 			if (buf < end)
4541da177e4SLinus Torvalds 				*buf = ' ';
4551da177e4SLinus Torvalds 			++buf;
4561da177e4SLinus Torvalds 		}
4571da177e4SLinus Torvalds 	}
458b39a7340SDenis Vlasenko 	/* sign */
4591da177e4SLinus Torvalds 	if (sign) {
460f796937aSJeremy Fitzhardinge 		if (buf < end)
4611da177e4SLinus Torvalds 			*buf = sign;
4621da177e4SLinus Torvalds 		++buf;
4631da177e4SLinus Torvalds 	}
464b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
465b39a7340SDenis Vlasenko 	if (need_pfx) {
4667c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
467f796937aSJeremy Fitzhardinge 			if (buf < end)
4681da177e4SLinus Torvalds 				*buf = '0';
4691da177e4SLinus Torvalds 			++buf;
4707c203422SPierre Carrier 		}
471fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
472f796937aSJeremy Fitzhardinge 			if (buf < end)
4739b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
4741da177e4SLinus Torvalds 			++buf;
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 	}
477b39a7340SDenis Vlasenko 	/* zero or space padding */
478fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
479d1c1b121SRasmus Villemoes 		char c = ' ' + (spec.flags & ZEROPAD);
480d1c1b121SRasmus Villemoes 		BUILD_BUG_ON(' ' + ZEROPAD != '0');
481fef20d9cSFrederic Weisbecker 		while (--spec.field_width >= 0) {
482f796937aSJeremy Fitzhardinge 			if (buf < end)
4831da177e4SLinus Torvalds 				*buf = c;
4841da177e4SLinus Torvalds 			++buf;
4851da177e4SLinus Torvalds 		}
4861da177e4SLinus Torvalds 	}
487b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
488fef20d9cSFrederic Weisbecker 	while (i <= --spec.precision) {
489f796937aSJeremy Fitzhardinge 		if (buf < end)
4901da177e4SLinus Torvalds 			*buf = '0';
4911da177e4SLinus Torvalds 		++buf;
4921da177e4SLinus Torvalds 	}
493b39a7340SDenis Vlasenko 	/* actual digits of result */
494b39a7340SDenis Vlasenko 	while (--i >= 0) {
495f796937aSJeremy Fitzhardinge 		if (buf < end)
4961da177e4SLinus Torvalds 			*buf = tmp[i];
4971da177e4SLinus Torvalds 		++buf;
4981da177e4SLinus Torvalds 	}
499b39a7340SDenis Vlasenko 	/* trailing space padding */
500fef20d9cSFrederic Weisbecker 	while (--spec.field_width >= 0) {
501f796937aSJeremy Fitzhardinge 		if (buf < end)
5021da177e4SLinus Torvalds 			*buf = ' ';
5031da177e4SLinus Torvalds 		++buf;
5041da177e4SLinus Torvalds 	}
5057b9186f5SAndré Goddard Rosa 
5061da177e4SLinus Torvalds 	return buf;
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds 
509cf3b429bSJoe Perches static noinline_for_stack
510cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec)
5110f9bfa56SLinus Torvalds {
5120f9bfa56SLinus Torvalds 	int len, i;
5130f9bfa56SLinus Torvalds 
5140f9bfa56SLinus Torvalds 	if ((unsigned long)s < PAGE_SIZE)
5150f4f81dcSAndré Goddard Rosa 		s = "(null)";
5160f9bfa56SLinus Torvalds 
517fef20d9cSFrederic Weisbecker 	len = strnlen(s, spec.precision);
5180f9bfa56SLinus Torvalds 
519fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
520fef20d9cSFrederic Weisbecker 		while (len < spec.field_width--) {
5210f9bfa56SLinus Torvalds 			if (buf < end)
5220f9bfa56SLinus Torvalds 				*buf = ' ';
5230f9bfa56SLinus Torvalds 			++buf;
5240f9bfa56SLinus Torvalds 		}
5250f9bfa56SLinus Torvalds 	}
5260f9bfa56SLinus Torvalds 	for (i = 0; i < len; ++i) {
5270f9bfa56SLinus Torvalds 		if (buf < end)
5280f9bfa56SLinus Torvalds 			*buf = *s;
5290f9bfa56SLinus Torvalds 		++buf; ++s;
5300f9bfa56SLinus Torvalds 	}
531fef20d9cSFrederic Weisbecker 	while (len < spec.field_width--) {
5320f9bfa56SLinus Torvalds 		if (buf < end)
5330f9bfa56SLinus Torvalds 			*buf = ' ';
5340f9bfa56SLinus Torvalds 		++buf;
5350f9bfa56SLinus Torvalds 	}
5367b9186f5SAndré Goddard Rosa 
5370f9bfa56SLinus Torvalds 	return buf;
5380f9bfa56SLinus Torvalds }
5390f9bfa56SLinus Torvalds 
5404b6ccca7SAl Viro static void widen(char *buf, char *end, unsigned len, unsigned spaces)
5414b6ccca7SAl Viro {
5424b6ccca7SAl Viro 	size_t size;
5434b6ccca7SAl Viro 	if (buf >= end)	/* nowhere to put anything */
5444b6ccca7SAl Viro 		return;
5454b6ccca7SAl Viro 	size = end - buf;
5464b6ccca7SAl Viro 	if (size <= spaces) {
5474b6ccca7SAl Viro 		memset(buf, ' ', size);
5484b6ccca7SAl Viro 		return;
5494b6ccca7SAl Viro 	}
5504b6ccca7SAl Viro 	if (len) {
5514b6ccca7SAl Viro 		if (len > size - spaces)
5524b6ccca7SAl Viro 			len = size - spaces;
5534b6ccca7SAl Viro 		memmove(buf + spaces, buf, len);
5544b6ccca7SAl Viro 	}
5554b6ccca7SAl Viro 	memset(buf, ' ', spaces);
5564b6ccca7SAl Viro }
5574b6ccca7SAl Viro 
5584b6ccca7SAl Viro static noinline_for_stack
5594b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
5604b6ccca7SAl Viro 		  const char *fmt)
5614b6ccca7SAl Viro {
5624b6ccca7SAl Viro 	const char *array[4], *s;
5634b6ccca7SAl Viro 	const struct dentry *p;
5644b6ccca7SAl Viro 	int depth;
5654b6ccca7SAl Viro 	int i, n;
5664b6ccca7SAl Viro 
5674b6ccca7SAl Viro 	switch (fmt[1]) {
5684b6ccca7SAl Viro 		case '2': case '3': case '4':
5694b6ccca7SAl Viro 			depth = fmt[1] - '0';
5704b6ccca7SAl Viro 			break;
5714b6ccca7SAl Viro 		default:
5724b6ccca7SAl Viro 			depth = 1;
5734b6ccca7SAl Viro 	}
5744b6ccca7SAl Viro 
5754b6ccca7SAl Viro 	rcu_read_lock();
5764b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
5774b6ccca7SAl Viro 		p = ACCESS_ONCE(d->d_parent);
5784b6ccca7SAl Viro 		array[i] = ACCESS_ONCE(d->d_name.name);
5794b6ccca7SAl Viro 		if (p == d) {
5804b6ccca7SAl Viro 			if (i)
5814b6ccca7SAl Viro 				array[i] = "";
5824b6ccca7SAl Viro 			i++;
5834b6ccca7SAl Viro 			break;
5844b6ccca7SAl Viro 		}
5854b6ccca7SAl Viro 	}
5864b6ccca7SAl Viro 	s = array[--i];
5874b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
5884b6ccca7SAl Viro 		char c = *s++;
5894b6ccca7SAl Viro 		if (!c) {
5904b6ccca7SAl Viro 			if (!i)
5914b6ccca7SAl Viro 				break;
5924b6ccca7SAl Viro 			c = '/';
5934b6ccca7SAl Viro 			s = array[--i];
5944b6ccca7SAl Viro 		}
5954b6ccca7SAl Viro 		if (buf < end)
5964b6ccca7SAl Viro 			*buf = c;
5974b6ccca7SAl Viro 	}
5984b6ccca7SAl Viro 	rcu_read_unlock();
5994b6ccca7SAl Viro 	if (n < spec.field_width) {
6004b6ccca7SAl Viro 		/* we want to pad the sucker */
6014b6ccca7SAl Viro 		unsigned spaces = spec.field_width - n;
6024b6ccca7SAl Viro 		if (!(spec.flags & LEFT)) {
6034b6ccca7SAl Viro 			widen(buf - n, end, n, spaces);
6044b6ccca7SAl Viro 			return buf + spaces;
6054b6ccca7SAl Viro 		}
6064b6ccca7SAl Viro 		while (spaces--) {
6074b6ccca7SAl Viro 			if (buf < end)
6084b6ccca7SAl Viro 				*buf = ' ';
6094b6ccca7SAl Viro 			++buf;
6104b6ccca7SAl Viro 		}
6114b6ccca7SAl Viro 	}
6124b6ccca7SAl Viro 	return buf;
6134b6ccca7SAl Viro }
6144b6ccca7SAl Viro 
615cf3b429bSJoe Perches static noinline_for_stack
616cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
617b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
6180fe1ef24SLinus Torvalds {
619b0d33c2bSJoe Perches 	unsigned long value;
6200fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
6210fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
622b0d33c2bSJoe Perches #endif
623b0d33c2bSJoe Perches 
624b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
625b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
626b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
627b0d33c2bSJoe Perches 
628b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
629b0d33c2bSJoe Perches 	if (*fmt == 'B')
6300f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
631b0d33c2bSJoe Perches 	else if (*fmt != 'f' && *fmt != 's')
6320fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
6330c8b946eSFrederic Weisbecker 	else
6344796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
6357b9186f5SAndré Goddard Rosa 
636fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
6370fe1ef24SLinus Torvalds #else
638fef20d9cSFrederic Weisbecker 	spec.field_width = 2 * sizeof(void *);
639fef20d9cSFrederic Weisbecker 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
640fef20d9cSFrederic Weisbecker 	spec.base = 16;
6417b9186f5SAndré Goddard Rosa 
642fef20d9cSFrederic Weisbecker 	return number(buf, end, value, spec);
6430fe1ef24SLinus Torvalds #endif
6440fe1ef24SLinus Torvalds }
6450fe1ef24SLinus Torvalds 
646cf3b429bSJoe Perches static noinline_for_stack
647cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
648fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
649332d2e78SLinus Torvalds {
650332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
65128405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
652332d2e78SLinus Torvalds #endif
653332d2e78SLinus Torvalds 
654332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
65528405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
656332d2e78SLinus Torvalds #endif
6574da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
658fef20d9cSFrederic Weisbecker 		.base = 16,
6594da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
660fef20d9cSFrederic Weisbecker 		.precision = -1,
661fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
662fef20d9cSFrederic Weisbecker 	};
6634da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
6644da0b66cSBjorn Helgaas 		.base = 16,
6654da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
6664da0b66cSBjorn Helgaas 		.precision = -1,
6674da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
6684da0b66cSBjorn Helgaas 	};
6690f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
6700f4050c7SBjorn Helgaas 		.base = 16,
6710f4050c7SBjorn Helgaas 		.field_width = 2,
6720f4050c7SBjorn Helgaas 		.precision = -1,
6730f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
6740f4050c7SBjorn Helgaas 	};
6754da0b66cSBjorn Helgaas 	static const struct printf_spec dec_spec = {
676c91d3376SBjorn Helgaas 		.base = 10,
677c91d3376SBjorn Helgaas 		.precision = -1,
678c91d3376SBjorn Helgaas 		.flags = 0,
679c91d3376SBjorn Helgaas 	};
6804da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
681fd95541eSBjorn Helgaas 		.field_width = -1,
682fd95541eSBjorn Helgaas 		.precision = 10,
683fd95541eSBjorn Helgaas 		.flags = LEFT,
684fd95541eSBjorn Helgaas 	};
6854da0b66cSBjorn Helgaas 	static const struct printf_spec flag_spec = {
686fd95541eSBjorn Helgaas 		.base = 16,
687fd95541eSBjorn Helgaas 		.precision = -1,
688fd95541eSBjorn Helgaas 		.flags = SPECIAL | SMALL,
689fd95541eSBjorn Helgaas 	};
690c7dabef8SBjorn Helgaas 
691c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
692c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
693c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
694c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
6959d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
696c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
697c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
698c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
699c7dabef8SBjorn Helgaas 
700332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
701c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
7024da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
703332d2e78SLinus Torvalds 
704332d2e78SLinus Torvalds 	*p++ = '[';
7054da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
706fd95541eSBjorn Helgaas 		p = string(p, pend, "io  ", str_spec);
7074da0b66cSBjorn Helgaas 		specp = &io_spec;
7084da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
709fd95541eSBjorn Helgaas 		p = string(p, pend, "mem ", str_spec);
7104da0b66cSBjorn Helgaas 		specp = &mem_spec;
7114da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
712fd95541eSBjorn Helgaas 		p = string(p, pend, "irq ", str_spec);
7134da0b66cSBjorn Helgaas 		specp = &dec_spec;
7144da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
715fd95541eSBjorn Helgaas 		p = string(p, pend, "dma ", str_spec);
7164da0b66cSBjorn Helgaas 		specp = &dec_spec;
7170f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
7180f4050c7SBjorn Helgaas 		p = string(p, pend, "bus ", str_spec);
7190f4050c7SBjorn Helgaas 		specp = &bus_spec;
7204da0b66cSBjorn Helgaas 	} else {
721c7dabef8SBjorn Helgaas 		p = string(p, pend, "??? ", str_spec);
7224da0b66cSBjorn Helgaas 		specp = &mem_spec;
723c7dabef8SBjorn Helgaas 		decode = 0;
724fd95541eSBjorn Helgaas 	}
725d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
726d19cb803SBjorn Helgaas 		p = string(p, pend, "size ", str_spec);
727d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
728d19cb803SBjorn Helgaas 	} else {
7294da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
730c91d3376SBjorn Helgaas 		if (res->start != res->end) {
731332d2e78SLinus Torvalds 			*p++ = '-';
7324da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
733c91d3376SBjorn Helgaas 		}
734d19cb803SBjorn Helgaas 	}
735c7dabef8SBjorn Helgaas 	if (decode) {
736fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
737fd95541eSBjorn Helgaas 			p = string(p, pend, " 64bit", str_spec);
738fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
739fd95541eSBjorn Helgaas 			p = string(p, pend, " pref", str_spec);
7409d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
7419d7cca04SBjorn Helgaas 			p = string(p, pend, " window", str_spec);
742fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
743fd95541eSBjorn Helgaas 			p = string(p, pend, " disabled", str_spec);
744c7dabef8SBjorn Helgaas 	} else {
745fd95541eSBjorn Helgaas 		p = string(p, pend, " flags ", str_spec);
746c7dabef8SBjorn Helgaas 		p = number(p, pend, res->flags, flag_spec);
747fd95541eSBjorn Helgaas 	}
748332d2e78SLinus Torvalds 	*p++ = ']';
749c7dabef8SBjorn Helgaas 	*p = '\0';
750332d2e78SLinus Torvalds 
751fef20d9cSFrederic Weisbecker 	return string(buf, end, sym, spec);
752332d2e78SLinus Torvalds }
753332d2e78SLinus Torvalds 
754cf3b429bSJoe Perches static noinline_for_stack
75531550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
75631550a16SAndy Shevchenko 		 const char *fmt)
75731550a16SAndy Shevchenko {
758360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
75931550a16SAndy Shevchenko 				   negative value, fallback to the default */
76031550a16SAndy Shevchenko 	char separator;
76131550a16SAndy Shevchenko 
76231550a16SAndy Shevchenko 	if (spec.field_width == 0)
76331550a16SAndy Shevchenko 		/* nothing to print */
76431550a16SAndy Shevchenko 		return buf;
76531550a16SAndy Shevchenko 
76631550a16SAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
76731550a16SAndy Shevchenko 		/* NULL pointer */
76831550a16SAndy Shevchenko 		return string(buf, end, NULL, spec);
76931550a16SAndy Shevchenko 
77031550a16SAndy Shevchenko 	switch (fmt[1]) {
77131550a16SAndy Shevchenko 	case 'C':
77231550a16SAndy Shevchenko 		separator = ':';
77331550a16SAndy Shevchenko 		break;
77431550a16SAndy Shevchenko 	case 'D':
77531550a16SAndy Shevchenko 		separator = '-';
77631550a16SAndy Shevchenko 		break;
77731550a16SAndy Shevchenko 	case 'N':
77831550a16SAndy Shevchenko 		separator = 0;
77931550a16SAndy Shevchenko 		break;
78031550a16SAndy Shevchenko 	default:
78131550a16SAndy Shevchenko 		separator = ' ';
78231550a16SAndy Shevchenko 		break;
78331550a16SAndy Shevchenko 	}
78431550a16SAndy Shevchenko 
78531550a16SAndy Shevchenko 	if (spec.field_width > 0)
78631550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
78731550a16SAndy Shevchenko 
7889c98f235SRasmus Villemoes 	for (i = 0; i < len; ++i) {
7899c98f235SRasmus Villemoes 		if (buf < end)
7909c98f235SRasmus Villemoes 			*buf = hex_asc_hi(addr[i]);
7919c98f235SRasmus Villemoes 		++buf;
7929c98f235SRasmus Villemoes 		if (buf < end)
7939c98f235SRasmus Villemoes 			*buf = hex_asc_lo(addr[i]);
7949c98f235SRasmus Villemoes 		++buf;
79531550a16SAndy Shevchenko 
7969c98f235SRasmus Villemoes 		if (separator && i != len - 1) {
7979c98f235SRasmus Villemoes 			if (buf < end)
7989c98f235SRasmus Villemoes 				*buf = separator;
7999c98f235SRasmus Villemoes 			++buf;
8009c98f235SRasmus Villemoes 		}
80131550a16SAndy Shevchenko 	}
80231550a16SAndy Shevchenko 
80331550a16SAndy Shevchenko 	return buf;
80431550a16SAndy Shevchenko }
80531550a16SAndy Shevchenko 
80631550a16SAndy Shevchenko static noinline_for_stack
807dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
808dbc760bcSTejun Heo 		    struct printf_spec spec, const char *fmt)
809dbc760bcSTejun Heo {
810dbc760bcSTejun Heo 	const int CHUNKSZ = 32;
811dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
812dbc760bcSTejun Heo 	int i, chunksz;
813dbc760bcSTejun Heo 	bool first = true;
814dbc760bcSTejun Heo 
815dbc760bcSTejun Heo 	/* reused to print numbers */
816dbc760bcSTejun Heo 	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
817dbc760bcSTejun Heo 
818dbc760bcSTejun Heo 	chunksz = nr_bits & (CHUNKSZ - 1);
819dbc760bcSTejun Heo 	if (chunksz == 0)
820dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
821dbc760bcSTejun Heo 
822dbc760bcSTejun Heo 	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
823dbc760bcSTejun Heo 	for (; i >= 0; i -= CHUNKSZ) {
824dbc760bcSTejun Heo 		u32 chunkmask, val;
825dbc760bcSTejun Heo 		int word, bit;
826dbc760bcSTejun Heo 
827dbc760bcSTejun Heo 		chunkmask = ((1ULL << chunksz) - 1);
828dbc760bcSTejun Heo 		word = i / BITS_PER_LONG;
829dbc760bcSTejun Heo 		bit = i % BITS_PER_LONG;
830dbc760bcSTejun Heo 		val = (bitmap[word] >> bit) & chunkmask;
831dbc760bcSTejun Heo 
832dbc760bcSTejun Heo 		if (!first) {
833dbc760bcSTejun Heo 			if (buf < end)
834dbc760bcSTejun Heo 				*buf = ',';
835dbc760bcSTejun Heo 			buf++;
836dbc760bcSTejun Heo 		}
837dbc760bcSTejun Heo 		first = false;
838dbc760bcSTejun Heo 
839dbc760bcSTejun Heo 		spec.field_width = DIV_ROUND_UP(chunksz, 4);
840dbc760bcSTejun Heo 		buf = number(buf, end, val, spec);
841dbc760bcSTejun Heo 
842dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
843dbc760bcSTejun Heo 	}
844dbc760bcSTejun Heo 	return buf;
845dbc760bcSTejun Heo }
846dbc760bcSTejun Heo 
847dbc760bcSTejun Heo static noinline_for_stack
848dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
849dbc760bcSTejun Heo 			 struct printf_spec spec, const char *fmt)
850dbc760bcSTejun Heo {
851dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
852dbc760bcSTejun Heo 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
853dbc760bcSTejun Heo 	int cur, rbot, rtop;
854dbc760bcSTejun Heo 	bool first = true;
855dbc760bcSTejun Heo 
856dbc760bcSTejun Heo 	/* reused to print numbers */
857dbc760bcSTejun Heo 	spec = (struct printf_spec){ .base = 10 };
858dbc760bcSTejun Heo 
859dbc760bcSTejun Heo 	rbot = cur = find_first_bit(bitmap, nr_bits);
860dbc760bcSTejun Heo 	while (cur < nr_bits) {
861dbc760bcSTejun Heo 		rtop = cur;
862dbc760bcSTejun Heo 		cur = find_next_bit(bitmap, nr_bits, cur + 1);
863dbc760bcSTejun Heo 		if (cur < nr_bits && cur <= rtop + 1)
864dbc760bcSTejun Heo 			continue;
865dbc760bcSTejun Heo 
866dbc760bcSTejun Heo 		if (!first) {
867dbc760bcSTejun Heo 			if (buf < end)
868dbc760bcSTejun Heo 				*buf = ',';
869dbc760bcSTejun Heo 			buf++;
870dbc760bcSTejun Heo 		}
871dbc760bcSTejun Heo 		first = false;
872dbc760bcSTejun Heo 
873dbc760bcSTejun Heo 		buf = number(buf, end, rbot, spec);
874dbc760bcSTejun Heo 		if (rbot < rtop) {
875dbc760bcSTejun Heo 			if (buf < end)
876dbc760bcSTejun Heo 				*buf = '-';
877dbc760bcSTejun Heo 			buf++;
878dbc760bcSTejun Heo 
879dbc760bcSTejun Heo 			buf = number(buf, end, rtop, spec);
880dbc760bcSTejun Heo 		}
881dbc760bcSTejun Heo 
882dbc760bcSTejun Heo 		rbot = cur;
883dbc760bcSTejun Heo 	}
884dbc760bcSTejun Heo 	return buf;
885dbc760bcSTejun Heo }
886dbc760bcSTejun Heo 
887dbc760bcSTejun Heo static noinline_for_stack
888cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
8898a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
890dd45c9cfSHarvey Harrison {
8918a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
892dd45c9cfSHarvey Harrison 	char *p = mac_addr;
893dd45c9cfSHarvey Harrison 	int i;
894bc7259a2SJoe Perches 	char separator;
89576597ff9SAndrei Emeltchenko 	bool reversed = false;
896bc7259a2SJoe Perches 
89776597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
89876597ff9SAndrei Emeltchenko 	case 'F':
899bc7259a2SJoe Perches 		separator = '-';
90076597ff9SAndrei Emeltchenko 		break;
90176597ff9SAndrei Emeltchenko 
90276597ff9SAndrei Emeltchenko 	case 'R':
90376597ff9SAndrei Emeltchenko 		reversed = true;
90476597ff9SAndrei Emeltchenko 		/* fall through */
90576597ff9SAndrei Emeltchenko 
90676597ff9SAndrei Emeltchenko 	default:
907bc7259a2SJoe Perches 		separator = ':';
90876597ff9SAndrei Emeltchenko 		break;
909bc7259a2SJoe Perches 	}
910dd45c9cfSHarvey Harrison 
911dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
91276597ff9SAndrei Emeltchenko 		if (reversed)
91376597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
91476597ff9SAndrei Emeltchenko 		else
91555036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
91676597ff9SAndrei Emeltchenko 
9178a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
918bc7259a2SJoe Perches 			*p++ = separator;
919dd45c9cfSHarvey Harrison 	}
920dd45c9cfSHarvey Harrison 	*p = '\0';
921dd45c9cfSHarvey Harrison 
922fef20d9cSFrederic Weisbecker 	return string(buf, end, mac_addr, spec);
923dd45c9cfSHarvey Harrison }
924dd45c9cfSHarvey Harrison 
925cf3b429bSJoe Perches static noinline_for_stack
926cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
927689afa7dSHarvey Harrison {
928689afa7dSHarvey Harrison 	int i;
9290159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
9300159f24eSJoe Perches 	int index;
9310159f24eSJoe Perches 	int step;
932689afa7dSHarvey Harrison 
9330159f24eSJoe Perches 	switch (fmt[2]) {
9340159f24eSJoe Perches 	case 'h':
9350159f24eSJoe Perches #ifdef __BIG_ENDIAN
9360159f24eSJoe Perches 		index = 0;
9370159f24eSJoe Perches 		step = 1;
9380159f24eSJoe Perches #else
9390159f24eSJoe Perches 		index = 3;
9400159f24eSJoe Perches 		step = -1;
9410159f24eSJoe Perches #endif
9420159f24eSJoe Perches 		break;
9430159f24eSJoe Perches 	case 'l':
9440159f24eSJoe Perches 		index = 3;
9450159f24eSJoe Perches 		step = -1;
9460159f24eSJoe Perches 		break;
9470159f24eSJoe Perches 	case 'n':
9480159f24eSJoe Perches 	case 'b':
9490159f24eSJoe Perches 	default:
9500159f24eSJoe Perches 		index = 0;
9510159f24eSJoe Perches 		step = 1;
9520159f24eSJoe Perches 		break;
9530159f24eSJoe Perches 	}
9548a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
9557c43d9a3SRasmus Villemoes 		char temp[4] __aligned(2);	/* hold each IP quad in reverse order */
956133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
9578a27f7c9SJoe Perches 		if (leading_zeros) {
9588a27f7c9SJoe Perches 			if (digits < 3)
9598a27f7c9SJoe Perches 				*p++ = '0';
9608a27f7c9SJoe Perches 			if (digits < 2)
9618a27f7c9SJoe Perches 				*p++ = '0';
9628a27f7c9SJoe Perches 		}
9638a27f7c9SJoe Perches 		/* reverse the digits in the quad */
9648a27f7c9SJoe Perches 		while (digits--)
9658a27f7c9SJoe Perches 			*p++ = temp[digits];
9668a27f7c9SJoe Perches 		if (i < 3)
9678a27f7c9SJoe Perches 			*p++ = '.';
9680159f24eSJoe Perches 		index += step;
9698a27f7c9SJoe Perches 	}
9708a27f7c9SJoe Perches 	*p = '\0';
9717b9186f5SAndré Goddard Rosa 
9728a27f7c9SJoe Perches 	return p;
9738a27f7c9SJoe Perches }
9748a27f7c9SJoe Perches 
975cf3b429bSJoe Perches static noinline_for_stack
976cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
9778a27f7c9SJoe Perches {
9787b9186f5SAndré Goddard Rosa 	int i, j, range;
9798a27f7c9SJoe Perches 	unsigned char zerolength[8];
9808a27f7c9SJoe Perches 	int longest = 1;
9818a27f7c9SJoe Perches 	int colonpos = -1;
9828a27f7c9SJoe Perches 	u16 word;
9837b9186f5SAndré Goddard Rosa 	u8 hi, lo;
9848a27f7c9SJoe Perches 	bool needcolon = false;
985eb78cd26SJoe Perches 	bool useIPv4;
986eb78cd26SJoe Perches 	struct in6_addr in6;
987eb78cd26SJoe Perches 
988eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
989eb78cd26SJoe Perches 
990eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
9918a27f7c9SJoe Perches 
9928a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
9938a27f7c9SJoe Perches 
9948a27f7c9SJoe Perches 	if (useIPv4)
9958a27f7c9SJoe Perches 		range = 6;
9968a27f7c9SJoe Perches 	else
9978a27f7c9SJoe Perches 		range = 8;
9988a27f7c9SJoe Perches 
9998a27f7c9SJoe Perches 	/* find position of longest 0 run */
10008a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10018a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
1002eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
10038a27f7c9SJoe Perches 				break;
10048a27f7c9SJoe Perches 			zerolength[i]++;
10058a27f7c9SJoe Perches 		}
10068a27f7c9SJoe Perches 	}
10078a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10088a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
10098a27f7c9SJoe Perches 			longest = zerolength[i];
10108a27f7c9SJoe Perches 			colonpos = i;
10118a27f7c9SJoe Perches 		}
10128a27f7c9SJoe Perches 	}
101329cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
101429cf519eSJoe Perches 		colonpos = -1;
10158a27f7c9SJoe Perches 
10168a27f7c9SJoe Perches 	/* emit address */
10178a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
10188a27f7c9SJoe Perches 		if (i == colonpos) {
10198a27f7c9SJoe Perches 			if (needcolon || i == 0)
10208a27f7c9SJoe Perches 				*p++ = ':';
10218a27f7c9SJoe Perches 			*p++ = ':';
10228a27f7c9SJoe Perches 			needcolon = false;
10238a27f7c9SJoe Perches 			i += longest - 1;
10248a27f7c9SJoe Perches 			continue;
10258a27f7c9SJoe Perches 		}
10268a27f7c9SJoe Perches 		if (needcolon) {
10278a27f7c9SJoe Perches 			*p++ = ':';
10288a27f7c9SJoe Perches 			needcolon = false;
10298a27f7c9SJoe Perches 		}
10308a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
1031eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
10328a27f7c9SJoe Perches 		hi = word >> 8;
10338a27f7c9SJoe Perches 		lo = word & 0xff;
10348a27f7c9SJoe Perches 		if (hi) {
10358a27f7c9SJoe Perches 			if (hi > 0x0f)
103655036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
10378a27f7c9SJoe Perches 			else
10388a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
103955036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10408a27f7c9SJoe Perches 		}
1041b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
104255036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
10438a27f7c9SJoe Perches 		else
10448a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
10458a27f7c9SJoe Perches 		needcolon = true;
10468a27f7c9SJoe Perches 	}
10478a27f7c9SJoe Perches 
10488a27f7c9SJoe Perches 	if (useIPv4) {
10498a27f7c9SJoe Perches 		if (needcolon)
10508a27f7c9SJoe Perches 			*p++ = ':';
10510159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
10528a27f7c9SJoe Perches 	}
10538a27f7c9SJoe Perches 	*p = '\0';
10547b9186f5SAndré Goddard Rosa 
10558a27f7c9SJoe Perches 	return p;
10568a27f7c9SJoe Perches }
10578a27f7c9SJoe Perches 
1058cf3b429bSJoe Perches static noinline_for_stack
1059cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
10608a27f7c9SJoe Perches {
10618a27f7c9SJoe Perches 	int i;
10627b9186f5SAndré Goddard Rosa 
1063689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
106455036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
106555036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
10668a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
1067689afa7dSHarvey Harrison 			*p++ = ':';
1068689afa7dSHarvey Harrison 	}
1069689afa7dSHarvey Harrison 	*p = '\0';
10707b9186f5SAndré Goddard Rosa 
10718a27f7c9SJoe Perches 	return p;
10728a27f7c9SJoe Perches }
10738a27f7c9SJoe Perches 
1074cf3b429bSJoe Perches static noinline_for_stack
1075cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
10768a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
10778a27f7c9SJoe Perches {
10788a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
10798a27f7c9SJoe Perches 
10808a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
1081eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
10828a27f7c9SJoe Perches 	else
1083eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
1084689afa7dSHarvey Harrison 
1085fef20d9cSFrederic Weisbecker 	return string(buf, end, ip6_addr, spec);
1086689afa7dSHarvey Harrison }
1087689afa7dSHarvey Harrison 
1088cf3b429bSJoe Perches static noinline_for_stack
1089cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
10908a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
10914aa99606SHarvey Harrison {
10928a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
10934aa99606SHarvey Harrison 
10940159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
10954aa99606SHarvey Harrison 
1096fef20d9cSFrederic Weisbecker 	return string(buf, end, ip4_addr, spec);
10974aa99606SHarvey Harrison }
10984aa99606SHarvey Harrison 
1099cf3b429bSJoe Perches static noinline_for_stack
110010679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
110110679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
110210679643SDaniel Borkmann {
110310679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
110410679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
110510679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
110610679643SDaniel Borkmann 		      sizeof("%1234567890")];
110710679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
110810679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
110910679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
111010679643SDaniel Borkmann 	u8 off = 0;
111110679643SDaniel Borkmann 
111210679643SDaniel Borkmann 	fmt++;
111310679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
111410679643SDaniel Borkmann 		switch (*fmt) {
111510679643SDaniel Borkmann 		case 'p':
111610679643SDaniel Borkmann 			have_p = true;
111710679643SDaniel Borkmann 			break;
111810679643SDaniel Borkmann 		case 'f':
111910679643SDaniel Borkmann 			have_f = true;
112010679643SDaniel Borkmann 			break;
112110679643SDaniel Borkmann 		case 's':
112210679643SDaniel Borkmann 			have_s = true;
112310679643SDaniel Borkmann 			break;
112410679643SDaniel Borkmann 		case 'c':
112510679643SDaniel Borkmann 			have_c = true;
112610679643SDaniel Borkmann 			break;
112710679643SDaniel Borkmann 		}
112810679643SDaniel Borkmann 	}
112910679643SDaniel Borkmann 
113010679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
113110679643SDaniel Borkmann 		*p = '[';
113210679643SDaniel Borkmann 		off = 1;
113310679643SDaniel Borkmann 	}
113410679643SDaniel Borkmann 
113510679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
113610679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
113710679643SDaniel Borkmann 	else
113810679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
113910679643SDaniel Borkmann 
114010679643SDaniel Borkmann 	if (have_p || have_s || have_f)
114110679643SDaniel Borkmann 		*p++ = ']';
114210679643SDaniel Borkmann 
114310679643SDaniel Borkmann 	if (have_p) {
114410679643SDaniel Borkmann 		*p++ = ':';
114510679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
114610679643SDaniel Borkmann 	}
114710679643SDaniel Borkmann 	if (have_f) {
114810679643SDaniel Borkmann 		*p++ = '/';
114910679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
115010679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
115110679643SDaniel Borkmann 	}
115210679643SDaniel Borkmann 	if (have_s) {
115310679643SDaniel Borkmann 		*p++ = '%';
115410679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
115510679643SDaniel Borkmann 	}
115610679643SDaniel Borkmann 	*p = '\0';
115710679643SDaniel Borkmann 
115810679643SDaniel Borkmann 	return string(buf, end, ip6_addr, spec);
115910679643SDaniel Borkmann }
116010679643SDaniel Borkmann 
116110679643SDaniel Borkmann static noinline_for_stack
116210679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
116310679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
116410679643SDaniel Borkmann {
116510679643SDaniel Borkmann 	bool have_p = false;
116610679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
116710679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
116810679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
116910679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
117010679643SDaniel Borkmann 
117110679643SDaniel Borkmann 	fmt++;
117210679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
117310679643SDaniel Borkmann 		switch (*fmt) {
117410679643SDaniel Borkmann 		case 'p':
117510679643SDaniel Borkmann 			have_p = true;
117610679643SDaniel Borkmann 			break;
117710679643SDaniel Borkmann 		case 'h':
117810679643SDaniel Borkmann 		case 'l':
117910679643SDaniel Borkmann 		case 'n':
118010679643SDaniel Borkmann 		case 'b':
118110679643SDaniel Borkmann 			fmt4[2] = *fmt;
118210679643SDaniel Borkmann 			break;
118310679643SDaniel Borkmann 		}
118410679643SDaniel Borkmann 	}
118510679643SDaniel Borkmann 
118610679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
118710679643SDaniel Borkmann 	if (have_p) {
118810679643SDaniel Borkmann 		*p++ = ':';
118910679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
119010679643SDaniel Borkmann 	}
119110679643SDaniel Borkmann 	*p = '\0';
119210679643SDaniel Borkmann 
119310679643SDaniel Borkmann 	return string(buf, end, ip4_addr, spec);
119410679643SDaniel Borkmann }
119510679643SDaniel Borkmann 
119610679643SDaniel Borkmann static noinline_for_stack
119771dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
119871dca95dSAndy Shevchenko 		     const char *fmt)
119971dca95dSAndy Shevchenko {
120071dca95dSAndy Shevchenko 	bool found = true;
120171dca95dSAndy Shevchenko 	int count = 1;
120271dca95dSAndy Shevchenko 	unsigned int flags = 0;
120371dca95dSAndy Shevchenko 	int len;
120471dca95dSAndy Shevchenko 
120571dca95dSAndy Shevchenko 	if (spec.field_width == 0)
120671dca95dSAndy Shevchenko 		return buf;				/* nothing to print */
120771dca95dSAndy Shevchenko 
120871dca95dSAndy Shevchenko 	if (ZERO_OR_NULL_PTR(addr))
120971dca95dSAndy Shevchenko 		return string(buf, end, NULL, spec);	/* NULL pointer */
121071dca95dSAndy Shevchenko 
121171dca95dSAndy Shevchenko 
121271dca95dSAndy Shevchenko 	do {
121371dca95dSAndy Shevchenko 		switch (fmt[count++]) {
121471dca95dSAndy Shevchenko 		case 'a':
121571dca95dSAndy Shevchenko 			flags |= ESCAPE_ANY;
121671dca95dSAndy Shevchenko 			break;
121771dca95dSAndy Shevchenko 		case 'c':
121871dca95dSAndy Shevchenko 			flags |= ESCAPE_SPECIAL;
121971dca95dSAndy Shevchenko 			break;
122071dca95dSAndy Shevchenko 		case 'h':
122171dca95dSAndy Shevchenko 			flags |= ESCAPE_HEX;
122271dca95dSAndy Shevchenko 			break;
122371dca95dSAndy Shevchenko 		case 'n':
122471dca95dSAndy Shevchenko 			flags |= ESCAPE_NULL;
122571dca95dSAndy Shevchenko 			break;
122671dca95dSAndy Shevchenko 		case 'o':
122771dca95dSAndy Shevchenko 			flags |= ESCAPE_OCTAL;
122871dca95dSAndy Shevchenko 			break;
122971dca95dSAndy Shevchenko 		case 'p':
123071dca95dSAndy Shevchenko 			flags |= ESCAPE_NP;
123171dca95dSAndy Shevchenko 			break;
123271dca95dSAndy Shevchenko 		case 's':
123371dca95dSAndy Shevchenko 			flags |= ESCAPE_SPACE;
123471dca95dSAndy Shevchenko 			break;
123571dca95dSAndy Shevchenko 		default:
123671dca95dSAndy Shevchenko 			found = false;
123771dca95dSAndy Shevchenko 			break;
123871dca95dSAndy Shevchenko 		}
123971dca95dSAndy Shevchenko 	} while (found);
124071dca95dSAndy Shevchenko 
124171dca95dSAndy Shevchenko 	if (!flags)
124271dca95dSAndy Shevchenko 		flags = ESCAPE_ANY_NP;
124371dca95dSAndy Shevchenko 
124471dca95dSAndy Shevchenko 	len = spec.field_width < 0 ? 1 : spec.field_width;
124571dca95dSAndy Shevchenko 
124641416f23SRasmus Villemoes 	/*
124741416f23SRasmus Villemoes 	 * string_escape_mem() writes as many characters as it can to
124841416f23SRasmus Villemoes 	 * the given buffer, and returns the total size of the output
124941416f23SRasmus Villemoes 	 * had the buffer been big enough.
125041416f23SRasmus Villemoes 	 */
125141416f23SRasmus Villemoes 	buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
125271dca95dSAndy Shevchenko 
125371dca95dSAndy Shevchenko 	return buf;
125471dca95dSAndy Shevchenko }
125571dca95dSAndy Shevchenko 
125671dca95dSAndy Shevchenko static noinline_for_stack
1257cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
12589ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
12599ac6e44eSJoe Perches {
12609ac6e44eSJoe Perches 	char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
12619ac6e44eSJoe Perches 	char *p = uuid;
12629ac6e44eSJoe Perches 	int i;
12639ac6e44eSJoe Perches 	static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
12649ac6e44eSJoe Perches 	static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
12659ac6e44eSJoe Perches 	const u8 *index = be;
12669ac6e44eSJoe Perches 	bool uc = false;
12679ac6e44eSJoe Perches 
12689ac6e44eSJoe Perches 	switch (*(++fmt)) {
12699ac6e44eSJoe Perches 	case 'L':
12709ac6e44eSJoe Perches 		uc = true;		/* fall-through */
12719ac6e44eSJoe Perches 	case 'l':
12729ac6e44eSJoe Perches 		index = le;
12739ac6e44eSJoe Perches 		break;
12749ac6e44eSJoe Perches 	case 'B':
12759ac6e44eSJoe Perches 		uc = true;
12769ac6e44eSJoe Perches 		break;
12779ac6e44eSJoe Perches 	}
12789ac6e44eSJoe Perches 
12799ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
128055036ba7SAndy Shevchenko 		p = hex_byte_pack(p, addr[index[i]]);
12819ac6e44eSJoe Perches 		switch (i) {
12829ac6e44eSJoe Perches 		case 3:
12839ac6e44eSJoe Perches 		case 5:
12849ac6e44eSJoe Perches 		case 7:
12859ac6e44eSJoe Perches 		case 9:
12869ac6e44eSJoe Perches 			*p++ = '-';
12879ac6e44eSJoe Perches 			break;
12889ac6e44eSJoe Perches 		}
12899ac6e44eSJoe Perches 	}
12909ac6e44eSJoe Perches 
12919ac6e44eSJoe Perches 	*p = 0;
12929ac6e44eSJoe Perches 
12939ac6e44eSJoe Perches 	if (uc) {
12949ac6e44eSJoe Perches 		p = uuid;
12959ac6e44eSJoe Perches 		do {
12969ac6e44eSJoe Perches 			*p = toupper(*p);
12979ac6e44eSJoe Perches 		} while (*(++p));
12989ac6e44eSJoe Perches 	}
12999ac6e44eSJoe Perches 
13009ac6e44eSJoe Perches 	return string(buf, end, uuid, spec);
13019ac6e44eSJoe Perches }
13029ac6e44eSJoe Perches 
1303c8f44affSMichał Mirosław static
1304c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr,
1305c8f44affSMichał Mirosław 		      struct printf_spec spec)
1306c8f44affSMichał Mirosław {
1307c8f44affSMichał Mirosław 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1308c8f44affSMichał Mirosław 	if (spec.field_width == -1)
1309c8f44affSMichał Mirosław 		spec.field_width = 2 + 2 * sizeof(netdev_features_t);
1310c8f44affSMichał Mirosław 	spec.base = 16;
1311c8f44affSMichał Mirosław 
1312c8f44affSMichał Mirosław 	return number(buf, end, *(const netdev_features_t *)addr, spec);
1313c8f44affSMichał Mirosław }
1314c8f44affSMichał Mirosław 
1315aaf07621SJoe Perches static noinline_for_stack
1316aaf07621SJoe Perches char *address_val(char *buf, char *end, const void *addr,
1317aaf07621SJoe Perches 		  struct printf_spec spec, const char *fmt)
1318aaf07621SJoe Perches {
1319aaf07621SJoe Perches 	unsigned long long num;
1320aaf07621SJoe Perches 
1321aaf07621SJoe Perches 	spec.flags |= SPECIAL | SMALL | ZEROPAD;
1322aaf07621SJoe Perches 	spec.base = 16;
1323aaf07621SJoe Perches 
1324aaf07621SJoe Perches 	switch (fmt[1]) {
1325aaf07621SJoe Perches 	case 'd':
1326aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
1327aaf07621SJoe Perches 		spec.field_width = sizeof(dma_addr_t) * 2 + 2;
1328aaf07621SJoe Perches 		break;
1329aaf07621SJoe Perches 	case 'p':
1330aaf07621SJoe Perches 	default:
1331aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
1332aaf07621SJoe Perches 		spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1333aaf07621SJoe Perches 		break;
1334aaf07621SJoe Perches 	}
1335aaf07621SJoe Perches 
1336aaf07621SJoe Perches 	return number(buf, end, num, spec);
1337aaf07621SJoe Perches }
1338aaf07621SJoe Perches 
1339900cca29SGeert Uytterhoeven static noinline_for_stack
1340900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1341900cca29SGeert Uytterhoeven 	    const char *fmt)
1342900cca29SGeert Uytterhoeven {
1343900cca29SGeert Uytterhoeven 	if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
1344900cca29SGeert Uytterhoeven 		return string(buf, end, NULL, spec);
1345900cca29SGeert Uytterhoeven 
1346900cca29SGeert Uytterhoeven 	switch (fmt[1]) {
1347900cca29SGeert Uytterhoeven 	case 'r':
1348900cca29SGeert Uytterhoeven 		return number(buf, end, clk_get_rate(clk), spec);
1349900cca29SGeert Uytterhoeven 
1350900cca29SGeert Uytterhoeven 	case 'n':
1351900cca29SGeert Uytterhoeven 	default:
1352900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK
1353900cca29SGeert Uytterhoeven 		return string(buf, end, __clk_get_name(clk), spec);
1354900cca29SGeert Uytterhoeven #else
1355900cca29SGeert Uytterhoeven 		spec.base = 16;
1356900cca29SGeert Uytterhoeven 		spec.field_width = sizeof(unsigned long) * 2 + 2;
1357900cca29SGeert Uytterhoeven 		spec.flags |= SPECIAL | SMALL | ZEROPAD;
1358900cca29SGeert Uytterhoeven 		return number(buf, end, (unsigned long)clk, spec);
1359900cca29SGeert Uytterhoeven #endif
1360900cca29SGeert Uytterhoeven 	}
1361900cca29SGeert Uytterhoeven }
1362900cca29SGeert Uytterhoeven 
1363411f05f1SIngo Molnar int kptr_restrict __read_mostly;
1364455cd5abSDan Rosenberg 
13654d8a743cSLinus Torvalds /*
13664d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
13674d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
13684d8a743cSLinus Torvalds  * specifiers.
13694d8a743cSLinus Torvalds  *
1370332d2e78SLinus Torvalds  * Right now we handle:
13710fe1ef24SLinus Torvalds  *
13720c8b946eSFrederic Weisbecker  * - 'F' For symbolic function descriptor pointers with offset
13730c8b946eSFrederic Weisbecker  * - 'f' For simple symbolic function names without offset
13740efb4d20SSteven Rostedt  * - 'S' For symbolic direct pointers with offset
13750efb4d20SSteven Rostedt  * - 's' For symbolic direct pointers without offset
1376b0d33c2bSJoe Perches  * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
13770f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
1378c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1379c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1380dbc760bcSTejun Heo  * - 'b[l]' For a bitmap, the number of bits is determined by the field
1381dbc760bcSTejun Heo  *       width which must be explicitly specified either as part of the
1382dbc760bcSTejun Heo  *       format string '%32b[l]' or through '%*b[l]', [l] selects
1383dbc760bcSTejun Heo  *       range-list format instead of hex format
1384dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
1385dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
13868a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1387bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1388c8e00060SJoe Perches  *       with a dash-separated hex notation
13897c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
13908a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
13918a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
13928a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
139310679643SDaniel Borkmann  *       [S][pfs]
139410679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
139510679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
13968a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
13978a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
13988a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
139910679643SDaniel Borkmann  *       [S][pfs]
140010679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
140110679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
140210679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
140310679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
140429cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
140571dca95dSAndy Shevchenko  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
140671dca95dSAndy Shevchenko  *                of the following flags (see string_escape_mem() for the
140771dca95dSAndy Shevchenko  *                details):
140871dca95dSAndy Shevchenko  *                  a - ESCAPE_ANY
140971dca95dSAndy Shevchenko  *                  c - ESCAPE_SPECIAL
141071dca95dSAndy Shevchenko  *                  h - ESCAPE_HEX
141171dca95dSAndy Shevchenko  *                  n - ESCAPE_NULL
141271dca95dSAndy Shevchenko  *                  o - ESCAPE_OCTAL
141371dca95dSAndy Shevchenko  *                  p - ESCAPE_NP
141471dca95dSAndy Shevchenko  *                  s - ESCAPE_SPACE
141571dca95dSAndy Shevchenko  *                By default ESCAPE_ANY_NP is used.
14169ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
14179ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
14189ac6e44eSJoe Perches  *       Options for %pU are:
14199ac6e44eSJoe Perches  *         b big endian lower case hex (default)
14209ac6e44eSJoe Perches  *         B big endian UPPER case hex
14219ac6e44eSJoe Perches  *         l little endian lower case hex
14229ac6e44eSJoe Perches  *         L little endian UPPER case hex
14239ac6e44eSJoe Perches  *           big endian output byte order is:
14249ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
14259ac6e44eSJoe Perches  *           little endian output byte order is:
14269ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
14277db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
14287db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
14297db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
14307db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
14317db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
1432455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
1433c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
143431550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
143531550a16SAndy Shevchenko  *            a certain separator (' ' by default):
143631550a16SAndy Shevchenko  *              C colon
143731550a16SAndy Shevchenko  *              D dash
143831550a16SAndy Shevchenko  *              N no separator
143931550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
144031550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
1441aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1442aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
1443c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
1444c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
1445900cca29SGeert Uytterhoeven  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1446900cca29SGeert Uytterhoeven  *       (legacy clock framework) of the clock
1447900cca29SGeert Uytterhoeven  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1448900cca29SGeert Uytterhoeven  *        (legacy clock framework) of the clock
1449900cca29SGeert Uytterhoeven  * - 'Cr' For a clock, it prints the current rate of the clock
14509ac6e44eSJoe Perches  *
1451332d2e78SLinus Torvalds  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1452332d2e78SLinus Torvalds  * function pointers are really function descriptors, which contain a
1453332d2e78SLinus Torvalds  * pointer to the real address.
14544d8a743cSLinus Torvalds  */
1455cf3b429bSJoe Perches static noinline_for_stack
1456cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1457fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
145878a8bf69SLinus Torvalds {
1459725fe002SGrant Likely 	int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1460725fe002SGrant Likely 
14619f36e2c4SKees Cook 	if (!ptr && *fmt != 'K') {
14625e057981SJoe Perches 		/*
14635e057981SJoe Perches 		 * Print (null) with the same width as a pointer so it makes
14645e057981SJoe Perches 		 * tabular output look nice.
14655e057981SJoe Perches 		 */
14665e057981SJoe Perches 		if (spec.field_width == -1)
1467725fe002SGrant Likely 			spec.field_width = default_width;
1468fef20d9cSFrederic Weisbecker 		return string(buf, end, "(null)", spec);
14695e057981SJoe Perches 	}
1470d97106abSLinus Torvalds 
14710fe1ef24SLinus Torvalds 	switch (*fmt) {
14720fe1ef24SLinus Torvalds 	case 'F':
14730c8b946eSFrederic Weisbecker 	case 'f':
14740fe1ef24SLinus Torvalds 		ptr = dereference_function_descriptor(ptr);
14750fe1ef24SLinus Torvalds 		/* Fallthrough */
14760fe1ef24SLinus Torvalds 	case 'S':
14779ac6e44eSJoe Perches 	case 's':
14780f77a8d3SNamhyung Kim 	case 'B':
1479b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
1480332d2e78SLinus Torvalds 	case 'R':
1481c7dabef8SBjorn Helgaas 	case 'r':
1482fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
148331550a16SAndy Shevchenko 	case 'h':
148431550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
1485dbc760bcSTejun Heo 	case 'b':
1486dbc760bcSTejun Heo 		switch (fmt[1]) {
1487dbc760bcSTejun Heo 		case 'l':
1488dbc760bcSTejun Heo 			return bitmap_list_string(buf, end, ptr, spec, fmt);
1489dbc760bcSTejun Heo 		default:
1490dbc760bcSTejun Heo 			return bitmap_string(buf, end, ptr, spec, fmt);
1491dbc760bcSTejun Heo 		}
14928a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
14938a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
149476597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
149576597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
14968a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
14978a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
14988a27f7c9SJoe Perches 					 * 4:	1.2.3.4
14998a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
15008a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
15018a27f7c9SJoe Perches 					 */
15028a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
15038a27f7c9SJoe Perches 					 * 4:	001.002.003.004
15048a27f7c9SJoe Perches 					 * 6:   000102...0f
15058a27f7c9SJoe Perches 					 */
15068a27f7c9SJoe Perches 		switch (fmt[1]) {
15078a27f7c9SJoe Perches 		case '6':
15088a27f7c9SJoe Perches 			return ip6_addr_string(buf, end, ptr, spec, fmt);
15098a27f7c9SJoe Perches 		case '4':
15108a27f7c9SJoe Perches 			return ip4_addr_string(buf, end, ptr, spec, fmt);
151110679643SDaniel Borkmann 		case 'S': {
151210679643SDaniel Borkmann 			const union {
151310679643SDaniel Borkmann 				struct sockaddr		raw;
151410679643SDaniel Borkmann 				struct sockaddr_in	v4;
151510679643SDaniel Borkmann 				struct sockaddr_in6	v6;
151610679643SDaniel Borkmann 			} *sa = ptr;
151710679643SDaniel Borkmann 
151810679643SDaniel Borkmann 			switch (sa->raw.sa_family) {
151910679643SDaniel Borkmann 			case AF_INET:
152010679643SDaniel Borkmann 				return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
152110679643SDaniel Borkmann 			case AF_INET6:
152210679643SDaniel Borkmann 				return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
152310679643SDaniel Borkmann 			default:
152410679643SDaniel Borkmann 				return string(buf, end, "(invalid address)", spec);
152510679643SDaniel Borkmann 			}}
15268a27f7c9SJoe Perches 		}
15274aa99606SHarvey Harrison 		break;
152871dca95dSAndy Shevchenko 	case 'E':
152971dca95dSAndy Shevchenko 		return escaped_string(buf, end, ptr, spec, fmt);
15309ac6e44eSJoe Perches 	case 'U':
15319ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
15327db6f5fbSJoe Perches 	case 'V':
15335756b76eSJan Beulich 		{
15345756b76eSJan Beulich 			va_list va;
15355756b76eSJan Beulich 
15365756b76eSJan Beulich 			va_copy(va, *((struct va_format *)ptr)->va);
15375756b76eSJan Beulich 			buf += vsnprintf(buf, end > buf ? end - buf : 0,
15385756b76eSJan Beulich 					 ((struct va_format *)ptr)->fmt, va);
15395756b76eSJan Beulich 			va_end(va);
15405756b76eSJan Beulich 			return buf;
15415756b76eSJan Beulich 		}
1542455cd5abSDan Rosenberg 	case 'K':
1543455cd5abSDan Rosenberg 		/*
1544455cd5abSDan Rosenberg 		 * %pK cannot be used in IRQ context because its test
1545455cd5abSDan Rosenberg 		 * for CAP_SYSLOG would be meaningless.
1546455cd5abSDan Rosenberg 		 */
15473715c530SDan Rosenberg 		if (kptr_restrict && (in_irq() || in_serving_softirq() ||
15483715c530SDan Rosenberg 				      in_nmi())) {
1549455cd5abSDan Rosenberg 			if (spec.field_width == -1)
1550725fe002SGrant Likely 				spec.field_width = default_width;
1551455cd5abSDan Rosenberg 			return string(buf, end, "pK-error", spec);
1552455cd5abSDan Rosenberg 		}
1553312b4e22SRyan Mallon 
1554312b4e22SRyan Mallon 		switch (kptr_restrict) {
1555312b4e22SRyan Mallon 		case 0:
1556312b4e22SRyan Mallon 			/* Always print %pK values */
1557312b4e22SRyan Mallon 			break;
1558312b4e22SRyan Mallon 		case 1: {
1559312b4e22SRyan Mallon 			/*
1560312b4e22SRyan Mallon 			 * Only print the real pointer value if the current
1561312b4e22SRyan Mallon 			 * process has CAP_SYSLOG and is running with the
1562312b4e22SRyan Mallon 			 * same credentials it started with. This is because
1563312b4e22SRyan Mallon 			 * access to files is checked at open() time, but %pK
1564312b4e22SRyan Mallon 			 * checks permission at read() time. We don't want to
1565312b4e22SRyan Mallon 			 * leak pointer values if a binary opens a file using
1566312b4e22SRyan Mallon 			 * %pK and then elevates privileges before reading it.
1567312b4e22SRyan Mallon 			 */
1568312b4e22SRyan Mallon 			const struct cred *cred = current_cred();
1569312b4e22SRyan Mallon 
1570312b4e22SRyan Mallon 			if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1571312b4e22SRyan Mallon 			    !uid_eq(cred->euid, cred->uid) ||
1572312b4e22SRyan Mallon 			    !gid_eq(cred->egid, cred->gid))
157326297607SJoe Perches 				ptr = NULL;
157426297607SJoe Perches 			break;
1575312b4e22SRyan Mallon 		}
1576312b4e22SRyan Mallon 		case 2:
1577312b4e22SRyan Mallon 		default:
1578312b4e22SRyan Mallon 			/* Always print 0's for %pK */
1579312b4e22SRyan Mallon 			ptr = NULL;
1580312b4e22SRyan Mallon 			break;
1581312b4e22SRyan Mallon 		}
1582312b4e22SRyan Mallon 		break;
1583312b4e22SRyan Mallon 
1584c8f44affSMichał Mirosław 	case 'N':
1585c8f44affSMichał Mirosław 		switch (fmt[1]) {
1586c8f44affSMichał Mirosław 		case 'F':
1587c8f44affSMichał Mirosław 			return netdev_feature_string(buf, end, ptr, spec);
1588c8f44affSMichał Mirosław 		}
1589c8f44affSMichał Mirosław 		break;
15907d799210SStepan Moskovchenko 	case 'a':
1591aaf07621SJoe Perches 		return address_val(buf, end, ptr, spec, fmt);
15924b6ccca7SAl Viro 	case 'd':
15934b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
1594900cca29SGeert Uytterhoeven 	case 'C':
1595900cca29SGeert Uytterhoeven 		return clock(buf, end, ptr, spec, fmt);
15964b6ccca7SAl Viro 	case 'D':
15974b6ccca7SAl Viro 		return dentry_name(buf, end,
15984b6ccca7SAl Viro 				   ((const struct file *)ptr)->f_path.dentry,
15994b6ccca7SAl Viro 				   spec, fmt);
16000fe1ef24SLinus Torvalds 	}
1601fef20d9cSFrederic Weisbecker 	spec.flags |= SMALL;
1602fef20d9cSFrederic Weisbecker 	if (spec.field_width == -1) {
1603725fe002SGrant Likely 		spec.field_width = default_width;
1604fef20d9cSFrederic Weisbecker 		spec.flags |= ZEROPAD;
160578a8bf69SLinus Torvalds 	}
1606fef20d9cSFrederic Weisbecker 	spec.base = 16;
1607fef20d9cSFrederic Weisbecker 
1608fef20d9cSFrederic Weisbecker 	return number(buf, end, (unsigned long) ptr, spec);
1609fef20d9cSFrederic Weisbecker }
1610fef20d9cSFrederic Weisbecker 
1611fef20d9cSFrederic Weisbecker /*
1612fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
1613fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
1614fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
1615fef20d9cSFrederic Weisbecker  * to go on the next call).
1616fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
1617fef20d9cSFrederic Weisbecker  *
1618fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
1619fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
1620fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
1621fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
1622fef20d9cSFrederic Weisbecker  *
1623fef20d9cSFrederic Weisbecker  * @fmt: the format string
1624fef20d9cSFrederic Weisbecker  * @type of the token returned
1625fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
1626fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
1627fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
1628fef20d9cSFrederic Weisbecker  * @precision: precision of a number
1629fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
1630fef20d9cSFrederic Weisbecker  */
1631cf3b429bSJoe Perches static noinline_for_stack
1632cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
1633fef20d9cSFrederic Weisbecker {
1634fef20d9cSFrederic Weisbecker 	const char *start = fmt;
1635fef20d9cSFrederic Weisbecker 
1636fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
1637ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
1638fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
1639fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
1640fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
1641fef20d9cSFrederic Weisbecker 		}
1642fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1643fef20d9cSFrederic Weisbecker 		goto precision;
1644fef20d9cSFrederic Weisbecker 	}
1645fef20d9cSFrederic Weisbecker 
1646fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
1647fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
1648fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
1649fef20d9cSFrederic Weisbecker 			spec->precision = 0;
1650fef20d9cSFrederic Weisbecker 
1651fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
1652fef20d9cSFrederic Weisbecker 		goto qualifier;
1653fef20d9cSFrederic Weisbecker 	}
1654fef20d9cSFrederic Weisbecker 
1655fef20d9cSFrederic Weisbecker 	/* By default */
1656fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
1657fef20d9cSFrederic Weisbecker 
1658fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
1659fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
1660fef20d9cSFrederic Weisbecker 			break;
1661fef20d9cSFrederic Weisbecker 	}
1662fef20d9cSFrederic Weisbecker 
1663fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
1664fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
1665fef20d9cSFrederic Weisbecker 		return fmt - start;
1666fef20d9cSFrederic Weisbecker 
1667fef20d9cSFrederic Weisbecker 	/* Process flags */
1668fef20d9cSFrederic Weisbecker 	spec->flags = 0;
1669fef20d9cSFrederic Weisbecker 
1670fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
1671fef20d9cSFrederic Weisbecker 		bool found = true;
1672fef20d9cSFrederic Weisbecker 
1673fef20d9cSFrederic Weisbecker 		++fmt;
1674fef20d9cSFrederic Weisbecker 
1675fef20d9cSFrederic Weisbecker 		switch (*fmt) {
1676fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
1677fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
1678fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
1679fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
1680fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
1681fef20d9cSFrederic Weisbecker 		default:  found = false;
1682fef20d9cSFrederic Weisbecker 		}
1683fef20d9cSFrederic Weisbecker 
1684fef20d9cSFrederic Weisbecker 		if (!found)
1685fef20d9cSFrederic Weisbecker 			break;
1686fef20d9cSFrederic Weisbecker 	}
1687fef20d9cSFrederic Weisbecker 
1688fef20d9cSFrederic Weisbecker 	/* get field width */
1689fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
1690fef20d9cSFrederic Weisbecker 
1691fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
1692fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
1693fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
1694fef20d9cSFrederic Weisbecker 		/* it's the next argument */
1695ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
1696fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1697fef20d9cSFrederic Weisbecker 	}
1698fef20d9cSFrederic Weisbecker 
1699fef20d9cSFrederic Weisbecker precision:
1700fef20d9cSFrederic Weisbecker 	/* get the precision */
1701fef20d9cSFrederic Weisbecker 	spec->precision = -1;
1702fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
1703fef20d9cSFrederic Weisbecker 		++fmt;
1704fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
1705fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
1706fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
1707fef20d9cSFrederic Weisbecker 				spec->precision = 0;
1708fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
1709fef20d9cSFrederic Weisbecker 			/* it's the next argument */
1710adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
1711fef20d9cSFrederic Weisbecker 			return ++fmt - start;
1712fef20d9cSFrederic Weisbecker 		}
1713fef20d9cSFrederic Weisbecker 	}
1714fef20d9cSFrederic Weisbecker 
1715fef20d9cSFrederic Weisbecker qualifier:
1716fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
1717fef20d9cSFrederic Weisbecker 	spec->qualifier = -1;
171875fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
171975fb8f26SAndy Shevchenko 	    _tolower(*fmt) == 'z' || *fmt == 't') {
1720a4e94ef0SZhaolei 		spec->qualifier = *fmt++;
1721a4e94ef0SZhaolei 		if (unlikely(spec->qualifier == *fmt)) {
1722a4e94ef0SZhaolei 			if (spec->qualifier == 'l') {
1723fef20d9cSFrederic Weisbecker 				spec->qualifier = 'L';
1724fef20d9cSFrederic Weisbecker 				++fmt;
1725a4e94ef0SZhaolei 			} else if (spec->qualifier == 'h') {
1726a4e94ef0SZhaolei 				spec->qualifier = 'H';
1727a4e94ef0SZhaolei 				++fmt;
1728a4e94ef0SZhaolei 			}
1729fef20d9cSFrederic Weisbecker 		}
1730fef20d9cSFrederic Weisbecker 	}
1731fef20d9cSFrederic Weisbecker 
1732fef20d9cSFrederic Weisbecker 	/* default base */
1733fef20d9cSFrederic Weisbecker 	spec->base = 10;
1734fef20d9cSFrederic Weisbecker 	switch (*fmt) {
1735fef20d9cSFrederic Weisbecker 	case 'c':
1736fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
1737fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1738fef20d9cSFrederic Weisbecker 
1739fef20d9cSFrederic Weisbecker 	case 's':
1740fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
1741fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1742fef20d9cSFrederic Weisbecker 
1743fef20d9cSFrederic Weisbecker 	case 'p':
1744fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
1745ffbfed03SRasmus Villemoes 		return ++fmt - start;
1746fef20d9cSFrederic Weisbecker 
1747fef20d9cSFrederic Weisbecker 	case '%':
1748fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
1749fef20d9cSFrederic Weisbecker 		return ++fmt - start;
1750fef20d9cSFrederic Weisbecker 
1751fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
1752fef20d9cSFrederic Weisbecker 	case 'o':
1753fef20d9cSFrederic Weisbecker 		spec->base = 8;
1754fef20d9cSFrederic Weisbecker 		break;
1755fef20d9cSFrederic Weisbecker 
1756fef20d9cSFrederic Weisbecker 	case 'x':
1757fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
1758fef20d9cSFrederic Weisbecker 
1759fef20d9cSFrederic Weisbecker 	case 'X':
1760fef20d9cSFrederic Weisbecker 		spec->base = 16;
1761fef20d9cSFrederic Weisbecker 		break;
1762fef20d9cSFrederic Weisbecker 
1763fef20d9cSFrederic Weisbecker 	case 'd':
1764fef20d9cSFrederic Weisbecker 	case 'i':
176539e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
1766fef20d9cSFrederic Weisbecker 	case 'u':
1767fef20d9cSFrederic Weisbecker 		break;
1768fef20d9cSFrederic Weisbecker 
1769708d96fdSRyan Mallon 	case 'n':
1770708d96fdSRyan Mallon 		/*
1771708d96fdSRyan Mallon 		 * Since %n poses a greater security risk than utility, treat
1772708d96fdSRyan Mallon 		 * it as an invalid format specifier. Warn about its use so
1773708d96fdSRyan Mallon 		 * that new instances don't get added.
1774708d96fdSRyan Mallon 		 */
1775708d96fdSRyan Mallon 		WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt);
1776708d96fdSRyan Mallon 		/* Fall-through */
1777708d96fdSRyan Mallon 
1778fef20d9cSFrederic Weisbecker 	default:
1779fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
1780fef20d9cSFrederic Weisbecker 		return fmt - start;
1781fef20d9cSFrederic Weisbecker 	}
1782fef20d9cSFrederic Weisbecker 
1783fef20d9cSFrederic Weisbecker 	if (spec->qualifier == 'L')
1784fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
1785fef20d9cSFrederic Weisbecker 	else if (spec->qualifier == 'l') {
178651be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
178751be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
178875fb8f26SAndy Shevchenko 	} else if (_tolower(spec->qualifier) == 'z') {
1789fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
1790fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 't') {
1791fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
1792a4e94ef0SZhaolei 	} else if (spec->qualifier == 'H') {
179351be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
179451be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
1795fef20d9cSFrederic Weisbecker 	} else if (spec->qualifier == 'h') {
179651be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
179751be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
1798fef20d9cSFrederic Weisbecker 	} else {
179951be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
180051be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
1801fef20d9cSFrederic Weisbecker 	}
1802fef20d9cSFrederic Weisbecker 
1803fef20d9cSFrederic Weisbecker 	return ++fmt - start;
180478a8bf69SLinus Torvalds }
180578a8bf69SLinus Torvalds 
18061da177e4SLinus Torvalds /**
18071da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
18081da177e4SLinus Torvalds  * @buf: The buffer to place the result into
18091da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
18101da177e4SLinus Torvalds  * @fmt: The format string to use
18111da177e4SLinus Torvalds  * @args: Arguments for the format string
18121da177e4SLinus Torvalds  *
181320036fdcSAndi Kleen  * This function follows C99 vsnprintf, but has some extensions:
181491adcd2cSSteven Rostedt  * %pS output the name of a text symbol with offset
181591adcd2cSSteven Rostedt  * %ps output the name of a text symbol without offset
18160c8b946eSFrederic Weisbecker  * %pF output the name of a function pointer with its offset
18170c8b946eSFrederic Weisbecker  * %pf output the name of a function pointer without its offset
18180f77a8d3SNamhyung Kim  * %pB output the name of a backtrace symbol with its offset
18198a79503aSUwe Kleine-König  * %pR output the address range in a struct resource with decoded flags
18208a79503aSUwe Kleine-König  * %pr output the address range in a struct resource with raw flags
1821dbc760bcSTejun Heo  * %pb output the bitmap with field width as the number of bits
1822dbc760bcSTejun Heo  * %pbl output the bitmap as range list with field width as the number of bits
18238a79503aSUwe Kleine-König  * %pM output a 6-byte MAC address with colons
18247c59154eSAndy Shevchenko  * %pMR output a 6-byte MAC address with colons in reversed order
18257c59154eSAndy Shevchenko  * %pMF output a 6-byte MAC address with dashes
18268a79503aSUwe Kleine-König  * %pm output a 6-byte MAC address without colons
18277c59154eSAndy Shevchenko  * %pmR output a 6-byte MAC address without colons in reversed order
18288a79503aSUwe Kleine-König  * %pI4 print an IPv4 address without leading zeros
18298a79503aSUwe Kleine-König  * %pi4 print an IPv4 address with leading zeros
18308a79503aSUwe Kleine-König  * %pI6 print an IPv6 address with colons
18318a79503aSUwe Kleine-König  * %pi6 print an IPv6 address without colons
1832f996f208SJan Engelhardt  * %pI6c print an IPv6 address as specified by RFC 5952
183310679643SDaniel Borkmann  * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
183410679643SDaniel Borkmann  * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
18358a79503aSUwe Kleine-König  * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
18368a79503aSUwe Kleine-König  *   case.
183771dca95dSAndy Shevchenko  * %*pE[achnops] print an escaped buffer
183831550a16SAndy Shevchenko  * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
183931550a16SAndy Shevchenko  *           bytes of the input)
1840900cca29SGeert Uytterhoeven  * %pC output the name (Common Clock Framework) or address (legacy clock
1841900cca29SGeert Uytterhoeven  *     framework) of a clock
1842900cca29SGeert Uytterhoeven  * %pCn output the name (Common Clock Framework) or address (legacy clock
1843900cca29SGeert Uytterhoeven  *      framework) of a clock
1844900cca29SGeert Uytterhoeven  * %pCr output the current rate of a clock
18450efb4d20SSteven Rostedt  * %n is ignored
184620036fdcSAndi Kleen  *
184780f548e0SAndrew Morton  * ** Please update Documentation/printk-formats.txt when making changes **
184880f548e0SAndrew Morton  *
18491da177e4SLinus Torvalds  * The return value is the number of characters which would
18501da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
18511da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
18521da177e4SLinus Torvalds  * number of characters written into @buf as return value
185372fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
18541da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
18551da177e4SLinus Torvalds  * string is truncated.
18561da177e4SLinus Torvalds  *
1857ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
18581da177e4SLinus Torvalds  */
18591da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
18601da177e4SLinus Torvalds {
18611da177e4SLinus Torvalds 	unsigned long long num;
1862d4be151bSAndré Goddard Rosa 	char *str, *end;
1863fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
18641da177e4SLinus Torvalds 
1865f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
1866f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
18672aa2f9e2SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
18681da177e4SLinus Torvalds 		return 0;
18691da177e4SLinus Torvalds 
18701da177e4SLinus Torvalds 	str = buf;
1871f796937aSJeremy Fitzhardinge 	end = buf + size;
18721da177e4SLinus Torvalds 
1873f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
1874f796937aSJeremy Fitzhardinge 	if (end < buf) {
18751da177e4SLinus Torvalds 		end = ((void *)-1);
1876f796937aSJeremy Fitzhardinge 		size = end - buf;
18771da177e4SLinus Torvalds 	}
18781da177e4SLinus Torvalds 
1879fef20d9cSFrederic Weisbecker 	while (*fmt) {
1880fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
1881d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
1882fef20d9cSFrederic Weisbecker 
1883fef20d9cSFrederic Weisbecker 		fmt += read;
1884fef20d9cSFrederic Weisbecker 
1885fef20d9cSFrederic Weisbecker 		switch (spec.type) {
1886fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
1887fef20d9cSFrederic Weisbecker 			int copy = read;
1888fef20d9cSFrederic Weisbecker 			if (str < end) {
1889fef20d9cSFrederic Weisbecker 				if (copy > end - str)
1890fef20d9cSFrederic Weisbecker 					copy = end - str;
1891fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
1892fef20d9cSFrederic Weisbecker 			}
1893fef20d9cSFrederic Weisbecker 			str += read;
1894fef20d9cSFrederic Weisbecker 			break;
18951da177e4SLinus Torvalds 		}
18961da177e4SLinus Torvalds 
1897ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
1898fef20d9cSFrederic Weisbecker 			spec.field_width = va_arg(args, int);
1899fef20d9cSFrederic Weisbecker 			break;
19001da177e4SLinus Torvalds 
1901fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
1902fef20d9cSFrederic Weisbecker 			spec.precision = va_arg(args, int);
1903fef20d9cSFrederic Weisbecker 			break;
19041da177e4SLinus Torvalds 
1905d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
1906d4be151bSAndré Goddard Rosa 			char c;
1907d4be151bSAndré Goddard Rosa 
1908fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
1909fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
1910f796937aSJeremy Fitzhardinge 					if (str < end)
19111da177e4SLinus Torvalds 						*str = ' ';
19121da177e4SLinus Torvalds 					++str;
1913fef20d9cSFrederic Weisbecker 
19141da177e4SLinus Torvalds 				}
19151da177e4SLinus Torvalds 			}
19161da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
1917f796937aSJeremy Fitzhardinge 			if (str < end)
19181da177e4SLinus Torvalds 				*str = c;
19191da177e4SLinus Torvalds 			++str;
1920fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
1921f796937aSJeremy Fitzhardinge 				if (str < end)
19221da177e4SLinus Torvalds 					*str = ' ';
19231da177e4SLinus Torvalds 				++str;
19241da177e4SLinus Torvalds 			}
1925fef20d9cSFrederic Weisbecker 			break;
1926d4be151bSAndré Goddard Rosa 		}
19271da177e4SLinus Torvalds 
1928fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
1929fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
1930fef20d9cSFrederic Weisbecker 			break;
19311da177e4SLinus Torvalds 
1932fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
1933ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, va_arg(args, void *),
1934fef20d9cSFrederic Weisbecker 				      spec);
1935fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
19364d8a743cSLinus Torvalds 				fmt++;
1937fef20d9cSFrederic Weisbecker 			break;
19381da177e4SLinus Torvalds 
1939fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
1940f796937aSJeremy Fitzhardinge 			if (str < end)
19411da177e4SLinus Torvalds 				*str = '%';
19421da177e4SLinus Torvalds 			++str;
19431da177e4SLinus Torvalds 			break;
19441da177e4SLinus Torvalds 
1945fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
1946f796937aSJeremy Fitzhardinge 			if (str < end)
19471da177e4SLinus Torvalds 				*str = '%';
19481da177e4SLinus Torvalds 			++str;
1949fef20d9cSFrederic Weisbecker 			break;
1950fef20d9cSFrederic Weisbecker 
1951fef20d9cSFrederic Weisbecker 		default:
1952fef20d9cSFrederic Weisbecker 			switch (spec.type) {
1953fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
1954fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
1955fef20d9cSFrederic Weisbecker 				break;
1956fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
1957fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
1958fef20d9cSFrederic Weisbecker 				break;
1959fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
1960fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
1961fef20d9cSFrederic Weisbecker 				break;
1962fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
1963ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
1964ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
1965ef124960SJason Gunthorpe 				else
1966fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
1967fef20d9cSFrederic Weisbecker 				break;
1968fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
1969fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
1970fef20d9cSFrederic Weisbecker 				break;
1971a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
1972a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
1973a4e94ef0SZhaolei 				break;
1974a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
1975a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
1976a4e94ef0SZhaolei 				break;
1977fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
1978fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
1979fef20d9cSFrederic Weisbecker 				break;
1980fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
1981fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
1982fef20d9cSFrederic Weisbecker 				break;
198339e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
198439e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
1985fef20d9cSFrederic Weisbecker 				break;
1986fef20d9cSFrederic Weisbecker 			default:
1987fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
19881da177e4SLinus Torvalds 			}
1989fef20d9cSFrederic Weisbecker 
1990fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
19911da177e4SLinus Torvalds 		}
1992fef20d9cSFrederic Weisbecker 	}
1993fef20d9cSFrederic Weisbecker 
1994f796937aSJeremy Fitzhardinge 	if (size > 0) {
1995f796937aSJeremy Fitzhardinge 		if (str < end)
19961da177e4SLinus Torvalds 			*str = '\0';
1997f796937aSJeremy Fitzhardinge 		else
19980a6047eeSLinus Torvalds 			end[-1] = '\0';
1999f796937aSJeremy Fitzhardinge 	}
2000fef20d9cSFrederic Weisbecker 
2001f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
20021da177e4SLinus Torvalds 	return str-buf;
2003fef20d9cSFrederic Weisbecker 
20041da177e4SLinus Torvalds }
20051da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
20061da177e4SLinus Torvalds 
20071da177e4SLinus Torvalds /**
20081da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
20091da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20101da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20111da177e4SLinus Torvalds  * @fmt: The format string to use
20121da177e4SLinus Torvalds  * @args: Arguments for the format string
20131da177e4SLinus Torvalds  *
20141da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
2015b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
20161da177e4SLinus Torvalds  * returns 0.
20171da177e4SLinus Torvalds  *
2018ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
201920036fdcSAndi Kleen  *
202020036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20211da177e4SLinus Torvalds  */
20221da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
20231da177e4SLinus Torvalds {
20241da177e4SLinus Torvalds 	int i;
20251da177e4SLinus Torvalds 
20261da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
20277b9186f5SAndré Goddard Rosa 
2028b921c69fSAnton Arapov 	if (likely(i < size))
2029b921c69fSAnton Arapov 		return i;
2030b921c69fSAnton Arapov 	if (size != 0)
2031b921c69fSAnton Arapov 		return size - 1;
2032b921c69fSAnton Arapov 	return 0;
20331da177e4SLinus Torvalds }
20341da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds /**
20371da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
20381da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20391da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20401da177e4SLinus Torvalds  * @fmt: The format string to use
20411da177e4SLinus Torvalds  * @...: Arguments for the format string
20421da177e4SLinus Torvalds  *
20431da177e4SLinus Torvalds  * The return value is the number of characters which would be
20441da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
20451da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
20461da177e4SLinus Torvalds  * @size, the resulting string is truncated.
204720036fdcSAndi Kleen  *
204820036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
20491da177e4SLinus Torvalds  */
20501da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
20511da177e4SLinus Torvalds {
20521da177e4SLinus Torvalds 	va_list args;
20531da177e4SLinus Torvalds 	int i;
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	va_start(args, fmt);
20561da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
20571da177e4SLinus Torvalds 	va_end(args);
20587b9186f5SAndré Goddard Rosa 
20591da177e4SLinus Torvalds 	return i;
20601da177e4SLinus Torvalds }
20611da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds /**
20641da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
20651da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20661da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
20671da177e4SLinus Torvalds  * @fmt: The format string to use
20681da177e4SLinus Torvalds  * @...: Arguments for the format string
20691da177e4SLinus Torvalds  *
20701da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
2071b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
20721da177e4SLinus Torvalds  */
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
20751da177e4SLinus Torvalds {
20761da177e4SLinus Torvalds 	va_list args;
20771da177e4SLinus Torvalds 	int i;
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds 	va_start(args, fmt);
2080b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
20811da177e4SLinus Torvalds 	va_end(args);
20827b9186f5SAndré Goddard Rosa 
2083b903c0b8SChangli Gao 	return i;
20841da177e4SLinus Torvalds }
20851da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds /**
20881da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
20891da177e4SLinus Torvalds  * @buf: The buffer to place the result into
20901da177e4SLinus Torvalds  * @fmt: The format string to use
20911da177e4SLinus Torvalds  * @args: Arguments for the format string
20921da177e4SLinus Torvalds  *
20931da177e4SLinus Torvalds  * The function returns the number of characters written
209472fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
20951da177e4SLinus Torvalds  * buffer overflows.
20961da177e4SLinus Torvalds  *
2097ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
209820036fdcSAndi Kleen  *
209920036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
21001da177e4SLinus Torvalds  */
21011da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
21021da177e4SLinus Torvalds {
21031da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds /**
21081da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
21091da177e4SLinus Torvalds  * @buf: The buffer to place the result into
21101da177e4SLinus Torvalds  * @fmt: The format string to use
21111da177e4SLinus Torvalds  * @...: Arguments for the format string
21121da177e4SLinus Torvalds  *
21131da177e4SLinus Torvalds  * The function returns the number of characters written
211472fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
21151da177e4SLinus Torvalds  * buffer overflows.
211620036fdcSAndi Kleen  *
211720036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
21181da177e4SLinus Torvalds  */
21191da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
21201da177e4SLinus Torvalds {
21211da177e4SLinus Torvalds 	va_list args;
21221da177e4SLinus Torvalds 	int i;
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds 	va_start(args, fmt);
21251da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
21261da177e4SLinus Torvalds 	va_end(args);
21277b9186f5SAndré Goddard Rosa 
21281da177e4SLinus Torvalds 	return i;
21291da177e4SLinus Torvalds }
21301da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
21311da177e4SLinus Torvalds 
21324370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
21334370aa4aSLai Jiangshan /*
21344370aa4aSLai Jiangshan  * bprintf service:
21354370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
21364370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
21374370aa4aSLai Jiangshan  */
21384370aa4aSLai Jiangshan 
21394370aa4aSLai Jiangshan /**
21404370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
21414370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
21424370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
21434370aa4aSLai Jiangshan  * @fmt: The format string to use
21444370aa4aSLai Jiangshan  * @args: Arguments for the format string
21454370aa4aSLai Jiangshan  *
21464370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
2147da3dae54SMasanari Iida  * is skipped.
21484370aa4aSLai Jiangshan  *
21494370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
21504370aa4aSLai Jiangshan  * the given input.
21514370aa4aSLai Jiangshan  *
21524370aa4aSLai Jiangshan  * NOTE:
21534370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
21544370aa4aSLai Jiangshan  * valid for bstr_printf().
21554370aa4aSLai Jiangshan  */
21564370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
21574370aa4aSLai Jiangshan {
2158fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
21594370aa4aSLai Jiangshan 	char *str, *end;
21604370aa4aSLai Jiangshan 
21614370aa4aSLai Jiangshan 	str = (char *)bin_buf;
21624370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
21634370aa4aSLai Jiangshan 
21644370aa4aSLai Jiangshan #define save_arg(type)							\
21654370aa4aSLai Jiangshan do {									\
21664370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
21674370aa4aSLai Jiangshan 		unsigned long long value;				\
21684370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
21694370aa4aSLai Jiangshan 		value = va_arg(args, unsigned long long);		\
21704370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
21714370aa4aSLai Jiangshan 			*(u32 *)str = *(u32 *)&value;			\
21724370aa4aSLai Jiangshan 			*(u32 *)(str + 4) = *((u32 *)&value + 1);	\
21734370aa4aSLai Jiangshan 		}							\
21744370aa4aSLai Jiangshan 	} else {							\
21754370aa4aSLai Jiangshan 		unsigned long value;					\
21764370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
21774370aa4aSLai Jiangshan 		value = va_arg(args, int);				\
21784370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
21794370aa4aSLai Jiangshan 			*(typeof(type) *)str = (type)value;		\
21804370aa4aSLai Jiangshan 	}								\
21814370aa4aSLai Jiangshan 	str += sizeof(type);						\
21824370aa4aSLai Jiangshan } while (0)
21834370aa4aSLai Jiangshan 
2184fef20d9cSFrederic Weisbecker 	while (*fmt) {
2185d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
21864370aa4aSLai Jiangshan 
2187fef20d9cSFrederic Weisbecker 		fmt += read;
2188fef20d9cSFrederic Weisbecker 
2189fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2190fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
2191d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_INVALID:
2192d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
2193fef20d9cSFrederic Weisbecker 			break;
2194fef20d9cSFrederic Weisbecker 
2195ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2196fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
21974370aa4aSLai Jiangshan 			save_arg(int);
2198fef20d9cSFrederic Weisbecker 			break;
21994370aa4aSLai Jiangshan 
2200fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
22014370aa4aSLai Jiangshan 			save_arg(char);
2202fef20d9cSFrederic Weisbecker 			break;
2203fef20d9cSFrederic Weisbecker 
2204fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
22054370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
22064370aa4aSLai Jiangshan 			size_t len;
22076c356634SAndré Goddard Rosa 
22084370aa4aSLai Jiangshan 			if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
22094370aa4aSLai Jiangshan 					|| (unsigned long)save_str < PAGE_SIZE)
22100f4f81dcSAndré Goddard Rosa 				save_str = "(null)";
22116c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
22126c356634SAndré Goddard Rosa 			if (str + len < end)
22136c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
22146c356634SAndré Goddard Rosa 			str += len;
2215fef20d9cSFrederic Weisbecker 			break;
22164370aa4aSLai Jiangshan 		}
2217fef20d9cSFrederic Weisbecker 
2218fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
22194370aa4aSLai Jiangshan 			save_arg(void *);
22204370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2221fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
22224370aa4aSLai Jiangshan 				fmt++;
2223fef20d9cSFrederic Weisbecker 			break;
2224fef20d9cSFrederic Weisbecker 
2225fef20d9cSFrederic Weisbecker 		default:
2226fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2227fef20d9cSFrederic Weisbecker 
2228fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2229fef20d9cSFrederic Weisbecker 				save_arg(long long);
2230fef20d9cSFrederic Weisbecker 				break;
2231fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2232fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2233fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
2234fef20d9cSFrederic Weisbecker 				break;
2235fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2236fef20d9cSFrederic Weisbecker 				save_arg(size_t);
2237fef20d9cSFrederic Weisbecker 				break;
2238fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2239fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
2240fef20d9cSFrederic Weisbecker 				break;
2241a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2242a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2243a4e94ef0SZhaolei 				save_arg(char);
2244a4e94ef0SZhaolei 				break;
2245fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2246fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2247fef20d9cSFrederic Weisbecker 				save_arg(short);
2248fef20d9cSFrederic Weisbecker 				break;
2249fef20d9cSFrederic Weisbecker 			default:
2250fef20d9cSFrederic Weisbecker 				save_arg(int);
2251fef20d9cSFrederic Weisbecker 			}
2252fef20d9cSFrederic Weisbecker 		}
2253fef20d9cSFrederic Weisbecker 	}
2254fef20d9cSFrederic Weisbecker 
22557b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2256fef20d9cSFrederic Weisbecker #undef save_arg
22574370aa4aSLai Jiangshan }
22584370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
22594370aa4aSLai Jiangshan 
22604370aa4aSLai Jiangshan /**
22614370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
22624370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
22634370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
22644370aa4aSLai Jiangshan  * @fmt: The format string to use
22654370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
22664370aa4aSLai Jiangshan  *
22674370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
22684370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
22694370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
22704370aa4aSLai Jiangshan  *
22714370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
22720efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
22734370aa4aSLai Jiangshan  *
22744370aa4aSLai Jiangshan  * The return value is the number of characters which would
22754370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
22764370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
22774370aa4aSLai Jiangshan  * number of characters written into @buf as return value
22784370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
22794370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
22804370aa4aSLai Jiangshan  * string is truncated.
22814370aa4aSLai Jiangshan  */
22824370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
22834370aa4aSLai Jiangshan {
2284fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
2285d4be151bSAndré Goddard Rosa 	char *str, *end;
2286d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
22874370aa4aSLai Jiangshan 
22882f30b1f9SMarcin Slusarz 	if (WARN_ON_ONCE((int) size < 0))
22894370aa4aSLai Jiangshan 		return 0;
22904370aa4aSLai Jiangshan 
22914370aa4aSLai Jiangshan 	str = buf;
22924370aa4aSLai Jiangshan 	end = buf + size;
22934370aa4aSLai Jiangshan 
22944370aa4aSLai Jiangshan #define get_arg(type)							\
22954370aa4aSLai Jiangshan ({									\
22964370aa4aSLai Jiangshan 	typeof(type) value;						\
22974370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
22984370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
22994370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
23004370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
23014370aa4aSLai Jiangshan 	} else {							\
23024370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
23034370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
23044370aa4aSLai Jiangshan 	}								\
23054370aa4aSLai Jiangshan 	args += sizeof(type);						\
23064370aa4aSLai Jiangshan 	value;								\
23074370aa4aSLai Jiangshan })
23084370aa4aSLai Jiangshan 
23094370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
23104370aa4aSLai Jiangshan 	if (end < buf) {
23114370aa4aSLai Jiangshan 		end = ((void *)-1);
23124370aa4aSLai Jiangshan 		size = end - buf;
23134370aa4aSLai Jiangshan 	}
23144370aa4aSLai Jiangshan 
2315fef20d9cSFrederic Weisbecker 	while (*fmt) {
2316fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2317d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2318fef20d9cSFrederic Weisbecker 
2319fef20d9cSFrederic Weisbecker 		fmt += read;
2320fef20d9cSFrederic Weisbecker 
2321fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2322fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2323fef20d9cSFrederic Weisbecker 			int copy = read;
2324fef20d9cSFrederic Weisbecker 			if (str < end) {
2325fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2326fef20d9cSFrederic Weisbecker 					copy = end - str;
2327fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2328fef20d9cSFrederic Weisbecker 			}
2329fef20d9cSFrederic Weisbecker 			str += read;
2330fef20d9cSFrederic Weisbecker 			break;
23314370aa4aSLai Jiangshan 		}
23324370aa4aSLai Jiangshan 
2333ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2334fef20d9cSFrederic Weisbecker 			spec.field_width = get_arg(int);
2335fef20d9cSFrederic Weisbecker 			break;
23364370aa4aSLai Jiangshan 
2337fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2338fef20d9cSFrederic Weisbecker 			spec.precision = get_arg(int);
2339fef20d9cSFrederic Weisbecker 			break;
23404370aa4aSLai Jiangshan 
2341d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2342d4be151bSAndré Goddard Rosa 			char c;
2343d4be151bSAndré Goddard Rosa 
2344fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2345fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
23464370aa4aSLai Jiangshan 					if (str < end)
23474370aa4aSLai Jiangshan 						*str = ' ';
23484370aa4aSLai Jiangshan 					++str;
23494370aa4aSLai Jiangshan 				}
23504370aa4aSLai Jiangshan 			}
23514370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
23524370aa4aSLai Jiangshan 			if (str < end)
23534370aa4aSLai Jiangshan 				*str = c;
23544370aa4aSLai Jiangshan 			++str;
2355fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
23564370aa4aSLai Jiangshan 				if (str < end)
23574370aa4aSLai Jiangshan 					*str = ' ';
23584370aa4aSLai Jiangshan 				++str;
23594370aa4aSLai Jiangshan 			}
2360fef20d9cSFrederic Weisbecker 			break;
2361d4be151bSAndré Goddard Rosa 		}
23624370aa4aSLai Jiangshan 
2363fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
23644370aa4aSLai Jiangshan 			const char *str_arg = args;
2365d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
2366fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
2367fef20d9cSFrederic Weisbecker 			break;
23684370aa4aSLai Jiangshan 		}
23694370aa4aSLai Jiangshan 
2370fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2371ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, get_arg(void *), spec);
2372fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
23734370aa4aSLai Jiangshan 				fmt++;
2374fef20d9cSFrederic Weisbecker 			break;
23754370aa4aSLai Jiangshan 
2376fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2377fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
23784370aa4aSLai Jiangshan 			if (str < end)
23794370aa4aSLai Jiangshan 				*str = '%';
23804370aa4aSLai Jiangshan 			++str;
2381fef20d9cSFrederic Weisbecker 			break;
2382fef20d9cSFrederic Weisbecker 
2383d4be151bSAndré Goddard Rosa 		default: {
2384d4be151bSAndré Goddard Rosa 			unsigned long long num;
2385d4be151bSAndré Goddard Rosa 
2386fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2387fef20d9cSFrederic Weisbecker 
2388fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
23894370aa4aSLai Jiangshan 				num = get_arg(long long);
2390fef20d9cSFrederic Weisbecker 				break;
2391fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2392fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2393fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
2394fef20d9cSFrederic Weisbecker 				break;
2395fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
23964370aa4aSLai Jiangshan 				num = get_arg(size_t);
2397fef20d9cSFrederic Weisbecker 				break;
2398fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
23994370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
2400fef20d9cSFrederic Weisbecker 				break;
2401a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2402a4e94ef0SZhaolei 				num = get_arg(unsigned char);
2403a4e94ef0SZhaolei 				break;
2404a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2405a4e94ef0SZhaolei 				num = get_arg(signed char);
2406a4e94ef0SZhaolei 				break;
2407fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2408fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
2409fef20d9cSFrederic Weisbecker 				break;
2410fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2411fef20d9cSFrederic Weisbecker 				num = get_arg(short);
2412fef20d9cSFrederic Weisbecker 				break;
2413fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
24144370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
2415fef20d9cSFrederic Weisbecker 				break;
2416fef20d9cSFrederic Weisbecker 			default:
2417fef20d9cSFrederic Weisbecker 				num = get_arg(int);
24184370aa4aSLai Jiangshan 			}
2419fef20d9cSFrederic Weisbecker 
2420fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
2421d4be151bSAndré Goddard Rosa 		} /* default: */
2422d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
2423d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
2424fef20d9cSFrederic Weisbecker 
24254370aa4aSLai Jiangshan 	if (size > 0) {
24264370aa4aSLai Jiangshan 		if (str < end)
24274370aa4aSLai Jiangshan 			*str = '\0';
24284370aa4aSLai Jiangshan 		else
24294370aa4aSLai Jiangshan 			end[-1] = '\0';
24304370aa4aSLai Jiangshan 	}
2431fef20d9cSFrederic Weisbecker 
24324370aa4aSLai Jiangshan #undef get_arg
24334370aa4aSLai Jiangshan 
24344370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
24354370aa4aSLai Jiangshan 	return str - buf;
24364370aa4aSLai Jiangshan }
24374370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
24384370aa4aSLai Jiangshan 
24394370aa4aSLai Jiangshan /**
24404370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
24414370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
24424370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
24434370aa4aSLai Jiangshan  * @fmt: The format string to use
24444370aa4aSLai Jiangshan  * @...: Arguments for the format string
24454370aa4aSLai Jiangshan  *
24464370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
24474370aa4aSLai Jiangshan  * into @bin_buf.
24484370aa4aSLai Jiangshan  */
24494370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
24504370aa4aSLai Jiangshan {
24514370aa4aSLai Jiangshan 	va_list args;
24524370aa4aSLai Jiangshan 	int ret;
24534370aa4aSLai Jiangshan 
24544370aa4aSLai Jiangshan 	va_start(args, fmt);
24554370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
24564370aa4aSLai Jiangshan 	va_end(args);
24577b9186f5SAndré Goddard Rosa 
24584370aa4aSLai Jiangshan 	return ret;
24594370aa4aSLai Jiangshan }
24604370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
24614370aa4aSLai Jiangshan 
24624370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
24634370aa4aSLai Jiangshan 
24641da177e4SLinus Torvalds /**
24651da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
24661da177e4SLinus Torvalds  * @buf:	input buffer
24671da177e4SLinus Torvalds  * @fmt:	format of buffer
24681da177e4SLinus Torvalds  * @args:	arguments
24691da177e4SLinus Torvalds  */
24701da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
24711da177e4SLinus Torvalds {
24721da177e4SLinus Torvalds 	const char *str = buf;
24731da177e4SLinus Torvalds 	char *next;
24741da177e4SLinus Torvalds 	char digit;
24751da177e4SLinus Torvalds 	int num = 0;
2476ef0658f3SJoe Perches 	u8 qualifier;
247753809751SJan Beulich 	unsigned int base;
247853809751SJan Beulich 	union {
247953809751SJan Beulich 		long long s;
248053809751SJan Beulich 		unsigned long long u;
248153809751SJan Beulich 	} val;
2482ef0658f3SJoe Perches 	s16 field_width;
2483d4be151bSAndré Goddard Rosa 	bool is_sign;
24841da177e4SLinus Torvalds 
2485da99075cSJan Beulich 	while (*fmt) {
24861da177e4SLinus Torvalds 		/* skip any white space in format */
24871da177e4SLinus Torvalds 		/* white space in format matchs any amount of
24881da177e4SLinus Torvalds 		 * white space, including none, in the input.
24891da177e4SLinus Torvalds 		 */
24901da177e4SLinus Torvalds 		if (isspace(*fmt)) {
2491e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
2492e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
24931da177e4SLinus Torvalds 		}
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
24961da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
24971da177e4SLinus Torvalds 			if (*fmt++ != *str++)
24981da177e4SLinus Torvalds 				break;
24991da177e4SLinus Torvalds 			continue;
25001da177e4SLinus Torvalds 		}
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 		if (!*fmt)
25031da177e4SLinus Torvalds 			break;
25041da177e4SLinus Torvalds 		++fmt;
25051da177e4SLinus Torvalds 
25061da177e4SLinus Torvalds 		/* skip this conversion.
25071da177e4SLinus Torvalds 		 * advance both strings to next white space
25081da177e4SLinus Torvalds 		 */
25091da177e4SLinus Torvalds 		if (*fmt == '*') {
2510da99075cSJan Beulich 			if (!*str)
2511da99075cSJan Beulich 				break;
25128fccae2cSAndy Spencer 			while (!isspace(*fmt) && *fmt != '%' && *fmt)
25131da177e4SLinus Torvalds 				fmt++;
25141da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
25151da177e4SLinus Torvalds 				str++;
25161da177e4SLinus Torvalds 			continue;
25171da177e4SLinus Torvalds 		}
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds 		/* get field width */
25201da177e4SLinus Torvalds 		field_width = -1;
252153809751SJan Beulich 		if (isdigit(*fmt)) {
25221da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
252353809751SJan Beulich 			if (field_width <= 0)
252453809751SJan Beulich 				break;
252553809751SJan Beulich 		}
25261da177e4SLinus Torvalds 
25271da177e4SLinus Torvalds 		/* get conversion qualifier */
25281da177e4SLinus Torvalds 		qualifier = -1;
252975fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
253075fb8f26SAndy Shevchenko 		    _tolower(*fmt) == 'z') {
25311da177e4SLinus Torvalds 			qualifier = *fmt++;
25321da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
25331da177e4SLinus Torvalds 				if (qualifier == 'h') {
25341da177e4SLinus Torvalds 					qualifier = 'H';
25351da177e4SLinus Torvalds 					fmt++;
25361da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
25371da177e4SLinus Torvalds 					qualifier = 'L';
25381da177e4SLinus Torvalds 					fmt++;
25391da177e4SLinus Torvalds 				}
25401da177e4SLinus Torvalds 			}
25411da177e4SLinus Torvalds 		}
25421da177e4SLinus Torvalds 
2543da99075cSJan Beulich 		if (!*fmt)
2544da99075cSJan Beulich 			break;
2545da99075cSJan Beulich 
2546da99075cSJan Beulich 		if (*fmt == 'n') {
2547da99075cSJan Beulich 			/* return number of characters read so far */
2548da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
2549da99075cSJan Beulich 			++fmt;
2550da99075cSJan Beulich 			continue;
2551da99075cSJan Beulich 		}
2552da99075cSJan Beulich 
2553da99075cSJan Beulich 		if (!*str)
25541da177e4SLinus Torvalds 			break;
25551da177e4SLinus Torvalds 
2556d4be151bSAndré Goddard Rosa 		base = 10;
25573f623ebaSFabian Frederick 		is_sign = false;
2558d4be151bSAndré Goddard Rosa 
25591da177e4SLinus Torvalds 		switch (*fmt++) {
25601da177e4SLinus Torvalds 		case 'c':
25611da177e4SLinus Torvalds 		{
25621da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
25631da177e4SLinus Torvalds 			if (field_width == -1)
25641da177e4SLinus Torvalds 				field_width = 1;
25651da177e4SLinus Torvalds 			do {
25661da177e4SLinus Torvalds 				*s++ = *str++;
25671da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
25681da177e4SLinus Torvalds 			num++;
25691da177e4SLinus Torvalds 		}
25701da177e4SLinus Torvalds 		continue;
25711da177e4SLinus Torvalds 		case 's':
25721da177e4SLinus Torvalds 		{
25731da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
25741da177e4SLinus Torvalds 			if (field_width == -1)
25754be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
25761da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
2577e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds 			/* now copy until next white space */
25807b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
25811da177e4SLinus Torvalds 				*s++ = *str++;
25821da177e4SLinus Torvalds 			*s = '\0';
25831da177e4SLinus Torvalds 			num++;
25841da177e4SLinus Torvalds 		}
25851da177e4SLinus Torvalds 		continue;
25861da177e4SLinus Torvalds 		case 'o':
25871da177e4SLinus Torvalds 			base = 8;
25881da177e4SLinus Torvalds 			break;
25891da177e4SLinus Torvalds 		case 'x':
25901da177e4SLinus Torvalds 		case 'X':
25911da177e4SLinus Torvalds 			base = 16;
25921da177e4SLinus Torvalds 			break;
25931da177e4SLinus Torvalds 		case 'i':
25941da177e4SLinus Torvalds 			base = 0;
25951da177e4SLinus Torvalds 		case 'd':
25963f623ebaSFabian Frederick 			is_sign = true;
25971da177e4SLinus Torvalds 		case 'u':
25981da177e4SLinus Torvalds 			break;
25991da177e4SLinus Torvalds 		case '%':
26001da177e4SLinus Torvalds 			/* looking for '%' in str */
26011da177e4SLinus Torvalds 			if (*str++ != '%')
26021da177e4SLinus Torvalds 				return num;
26031da177e4SLinus Torvalds 			continue;
26041da177e4SLinus Torvalds 		default:
26051da177e4SLinus Torvalds 			/* invalid format; stop here */
26061da177e4SLinus Torvalds 			return num;
26071da177e4SLinus Torvalds 		}
26081da177e4SLinus Torvalds 
26091da177e4SLinus Torvalds 		/* have some sort of integer conversion.
26101da177e4SLinus Torvalds 		 * first, skip white space in buffer.
26111da177e4SLinus Torvalds 		 */
2612e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 		digit = *str;
26151da177e4SLinus Torvalds 		if (is_sign && digit == '-')
26161da177e4SLinus Torvalds 			digit = *(str + 1);
26171da177e4SLinus Torvalds 
26181da177e4SLinus Torvalds 		if (!digit
26191da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
26201da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
26211da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
26221da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
26231da177e4SLinus Torvalds 			break;
26241da177e4SLinus Torvalds 
262553809751SJan Beulich 		if (is_sign)
262653809751SJan Beulich 			val.s = qualifier != 'L' ?
262753809751SJan Beulich 				simple_strtol(str, &next, base) :
262853809751SJan Beulich 				simple_strtoll(str, &next, base);
262953809751SJan Beulich 		else
263053809751SJan Beulich 			val.u = qualifier != 'L' ?
263153809751SJan Beulich 				simple_strtoul(str, &next, base) :
263253809751SJan Beulich 				simple_strtoull(str, &next, base);
263353809751SJan Beulich 
263453809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
263553809751SJan Beulich 			if (base == 0)
263653809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
263753809751SJan Beulich 			while (next - str > field_width) {
263853809751SJan Beulich 				if (is_sign)
263953809751SJan Beulich 					val.s = div_s64(val.s, base);
264053809751SJan Beulich 				else
264153809751SJan Beulich 					val.u = div_u64(val.u, base);
264253809751SJan Beulich 				--next;
264353809751SJan Beulich 			}
264453809751SJan Beulich 		}
264553809751SJan Beulich 
26461da177e4SLinus Torvalds 		switch (qualifier) {
26471da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
264853809751SJan Beulich 			if (is_sign)
264953809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
265053809751SJan Beulich 			else
265153809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
26521da177e4SLinus Torvalds 			break;
26531da177e4SLinus Torvalds 		case 'h':
265453809751SJan Beulich 			if (is_sign)
265553809751SJan Beulich 				*va_arg(args, short *) = val.s;
265653809751SJan Beulich 			else
265753809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
26581da177e4SLinus Torvalds 			break;
26591da177e4SLinus Torvalds 		case 'l':
266053809751SJan Beulich 			if (is_sign)
266153809751SJan Beulich 				*va_arg(args, long *) = val.s;
266253809751SJan Beulich 			else
266353809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
26641da177e4SLinus Torvalds 			break;
26651da177e4SLinus Torvalds 		case 'L':
266653809751SJan Beulich 			if (is_sign)
266753809751SJan Beulich 				*va_arg(args, long long *) = val.s;
266853809751SJan Beulich 			else
266953809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
26701da177e4SLinus Torvalds 			break;
26711da177e4SLinus Torvalds 		case 'Z':
26721da177e4SLinus Torvalds 		case 'z':
267353809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
26741da177e4SLinus Torvalds 			break;
26751da177e4SLinus Torvalds 		default:
267653809751SJan Beulich 			if (is_sign)
267753809751SJan Beulich 				*va_arg(args, int *) = val.s;
267853809751SJan Beulich 			else
267953809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
26801da177e4SLinus Torvalds 			break;
26811da177e4SLinus Torvalds 		}
26821da177e4SLinus Torvalds 		num++;
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 		if (!next)
26851da177e4SLinus Torvalds 			break;
26861da177e4SLinus Torvalds 		str = next;
26871da177e4SLinus Torvalds 	}
2688c6b40d16SJohannes Berg 
26891da177e4SLinus Torvalds 	return num;
26901da177e4SLinus Torvalds }
26911da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
26921da177e4SLinus Torvalds 
26931da177e4SLinus Torvalds /**
26941da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
26951da177e4SLinus Torvalds  * @buf:	input buffer
26961da177e4SLinus Torvalds  * @fmt:	formatting of buffer
26971da177e4SLinus Torvalds  * @...:	resulting arguments
26981da177e4SLinus Torvalds  */
26991da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
27001da177e4SLinus Torvalds {
27011da177e4SLinus Torvalds 	va_list args;
27021da177e4SLinus Torvalds 	int i;
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 	va_start(args, fmt);
27051da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
27061da177e4SLinus Torvalds 	va_end(args);
27077b9186f5SAndré Goddard Rosa 
27081da177e4SLinus Torvalds 	return i;
27091da177e4SLinus Torvalds }
27101da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
2711