xref: /openbmc/linux/lib/vsprintf.c (revision 8eda94bde4ff004a942ed95e6348de711b0e6da9)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/lib/vsprintf.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds  * Wirzenius wrote this portably, Torvalds fucked it up :-)
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds /*
141da177e4SLinus Torvalds  * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
151da177e4SLinus Torvalds  * - changed to provide snprintf and vsnprintf functions
161da177e4SLinus Torvalds  * So Feb  1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
171da177e4SLinus Torvalds  * - scnprintf and vscnprintf
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <stdarg.h>
21ef27ac18SRasmus Villemoes #include <linux/build_bug.h>
220d1d7a55SStephen Boyd #include <linux/clk.h>
23900cca29SGeert Uytterhoeven #include <linux/clk-provider.h>
2457f5677eSRasmus Villemoes #include <linux/errname.h>
258bc3bcc9SPaul Gortmaker #include <linux/module.h>	/* for KSYM_SYMBOL_LEN */
261da177e4SLinus Torvalds #include <linux/types.h>
271da177e4SLinus Torvalds #include <linux/string.h>
281da177e4SLinus Torvalds #include <linux/ctype.h>
291da177e4SLinus Torvalds #include <linux/kernel.h>
300fe1ef24SLinus Torvalds #include <linux/kallsyms.h>
3153809751SJan Beulich #include <linux/math64.h>
320fe1ef24SLinus Torvalds #include <linux/uaccess.h>
33332d2e78SLinus Torvalds #include <linux/ioport.h>
344b6ccca7SAl Viro #include <linux/dcache.h>
35312b4e22SRyan Mallon #include <linux/cred.h>
364d42c447SAndy Shevchenko #include <linux/rtc.h>
377daac5b2SAndy Shevchenko #include <linux/time.h>
382b1b0d66SAndy Shevchenko #include <linux/uuid.h>
39ce4fecf1SPantelis Antoniou #include <linux/of.h>
408a27f7c9SJoe Perches #include <net/addrconf.h>
41ad67b74dSTobin C. Harding #include <linux/siphash.h>
42ad67b74dSTobin C. Harding #include <linux/compiler.h>
43a92eb762SSakari Ailus #include <linux/property.h>
441031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
451031bc58SDmitry Monakhov #include <linux/blkdev.h>
461031bc58SDmitry Monakhov #endif
471da177e4SLinus Torvalds 
48edf14cdbSVlastimil Babka #include "../mm/internal.h"	/* For the trace_print_flags arrays */
49edf14cdbSVlastimil Babka 
504e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
517c43d9a3SRasmus Villemoes #include <asm/byteorder.h>	/* cpu_to_le16 */
521da177e4SLinus Torvalds 
5371dca95dSAndy Shevchenko #include <linux/string_helpers.h>
541dff46d6SAlexey Dobriyan #include "kstrtox.h"
55aa46a63eSHarvey Harrison 
561da177e4SLinus Torvalds /**
571da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
581da177e4SLinus Torvalds  * @cp: The start of the string
591da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
601da177e4SLinus Torvalds  * @base: The number base to use
61462e4711SEldad Zack  *
62e8cc2b97SAndy Shevchenko  * This function has caveats. Please use kstrtoull instead.
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
651da177e4SLinus Torvalds {
661dff46d6SAlexey Dobriyan 	unsigned long long result;
671dff46d6SAlexey Dobriyan 	unsigned int rv;
681da177e4SLinus Torvalds 
691dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
701dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
711dff46d6SAlexey Dobriyan 	/* FIXME */
721dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
73aa46a63eSHarvey Harrison 
741da177e4SLinus Torvalds 	if (endp)
751da177e4SLinus Torvalds 		*endp = (char *)cp;
767b9186f5SAndré Goddard Rosa 
771da177e4SLinus Torvalds 	return result;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /**
82922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
83922ac25cSAndré Goddard Rosa  * @cp: The start of the string
84922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
85922ac25cSAndré Goddard Rosa  * @base: The number base to use
86462e4711SEldad Zack  *
87e8cc2b97SAndy Shevchenko  * This function has caveats. Please use kstrtoul instead.
88922ac25cSAndré Goddard Rosa  */
89922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
90922ac25cSAndré Goddard Rosa {
91922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
92922ac25cSAndré Goddard Rosa }
93922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
94922ac25cSAndré Goddard Rosa 
95922ac25cSAndré Goddard Rosa /**
96922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
97922ac25cSAndré Goddard Rosa  * @cp: The start of the string
98922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
99922ac25cSAndré Goddard Rosa  * @base: The number base to use
100462e4711SEldad Zack  *
101e8cc2b97SAndy Shevchenko  * This function has caveats. Please use kstrtol instead.
102922ac25cSAndré Goddard Rosa  */
103922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
104922ac25cSAndré Goddard Rosa {
105922ac25cSAndré Goddard Rosa 	if (*cp == '-')
106922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
107922ac25cSAndré Goddard Rosa 
108922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
109922ac25cSAndré Goddard Rosa }
110922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
111922ac25cSAndré Goddard Rosa 
112922ac25cSAndré Goddard Rosa /**
1131da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
1141da177e4SLinus Torvalds  * @cp: The start of the string
1151da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1161da177e4SLinus Torvalds  * @base: The number base to use
117462e4711SEldad Zack  *
118e8cc2b97SAndy Shevchenko  * This function has caveats. Please use kstrtoll instead.
1191da177e4SLinus Torvalds  */
1201da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	if (*cp == '-')
1231da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1247b9186f5SAndré Goddard Rosa 
1251da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1261da177e4SLinus Torvalds }
12798d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1281da177e4SLinus Torvalds 
129cf3b429bSJoe Perches static noinline_for_stack
130cf3b429bSJoe Perches int skip_atoi(const char **s)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	int i = 0;
1331da177e4SLinus Torvalds 
13443e5b666SRasmus Villemoes 	do {
1351da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
13643e5b666SRasmus Villemoes 	} while (isdigit(**s));
1377b9186f5SAndré Goddard Rosa 
1381da177e4SLinus Torvalds 	return i;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1417c43d9a3SRasmus Villemoes /*
1427c43d9a3SRasmus Villemoes  * Decimal conversion is by far the most typical, and is used for
1437c43d9a3SRasmus Villemoes  * /proc and /sys data. This directly impacts e.g. top performance
1447c43d9a3SRasmus Villemoes  * with many processes running. We optimize it for speed by emitting
1457c43d9a3SRasmus Villemoes  * two characters at a time, using a 200 byte lookup table. This
1467c43d9a3SRasmus Villemoes  * roughly halves the number of multiplications compared to computing
1477c43d9a3SRasmus Villemoes  * the digits one at a time. Implementation strongly inspired by the
1487c43d9a3SRasmus Villemoes  * previous version, which in turn used ideas described at
1497c43d9a3SRasmus Villemoes  * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
1507c43d9a3SRasmus Villemoes  * from the author, Douglas W. Jones).
1517c43d9a3SRasmus Villemoes  *
1527c43d9a3SRasmus Villemoes  * It turns out there is precisely one 26 bit fixed-point
1537c43d9a3SRasmus Villemoes  * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
1547c43d9a3SRasmus Villemoes  * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
1557c43d9a3SRasmus Villemoes  * range happens to be somewhat larger (x <= 1073741898), but that's
1567c43d9a3SRasmus Villemoes  * irrelevant for our purpose.
1577c43d9a3SRasmus Villemoes  *
1587c43d9a3SRasmus Villemoes  * For dividing a number in the range [10^4, 10^6-1] by 100, we still
1597c43d9a3SRasmus Villemoes  * need a 32x32->64 bit multiply, so we simply use the same constant.
1607c43d9a3SRasmus Villemoes  *
1617c43d9a3SRasmus Villemoes  * For dividing a number in the range [100, 10^4-1] by 100, there are
1627c43d9a3SRasmus Villemoes  * several options. The simplest is (x * 0x147b) >> 19, which is valid
1637c43d9a3SRasmus Villemoes  * for all x <= 43698.
164133fd9f5SDenys Vlasenko  */
1654277eeddSDenis Vlasenko 
1667c43d9a3SRasmus Villemoes static const u16 decpair[100] = {
1677c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
1687c43d9a3SRasmus Villemoes 	_( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
1697c43d9a3SRasmus Villemoes 	_(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
1707c43d9a3SRasmus Villemoes 	_(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
1717c43d9a3SRasmus Villemoes 	_(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
1727c43d9a3SRasmus Villemoes 	_(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
1737c43d9a3SRasmus Villemoes 	_(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
1747c43d9a3SRasmus Villemoes 	_(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
1757c43d9a3SRasmus Villemoes 	_(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
1767c43d9a3SRasmus Villemoes 	_(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
1777c43d9a3SRasmus Villemoes 	_(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
1787c43d9a3SRasmus Villemoes #undef _
1797c43d9a3SRasmus Villemoes };
1804277eeddSDenis Vlasenko 
1817b9186f5SAndré Goddard Rosa /*
1827c43d9a3SRasmus Villemoes  * This will print a single '0' even if r == 0, since we would
183675cf53cSRasmus Villemoes  * immediately jump to out_r where two 0s would be written but only
184675cf53cSRasmus Villemoes  * one of them accounted for in buf. This is needed by ip4_string
185675cf53cSRasmus Villemoes  * below. All other callers pass a non-zero value of r.
186133fd9f5SDenys Vlasenko */
187133fd9f5SDenys Vlasenko static noinline_for_stack
188133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
189133fd9f5SDenys Vlasenko {
190133fd9f5SDenys Vlasenko 	unsigned q;
1914277eeddSDenis Vlasenko 
1927c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^8 */
1937c43d9a3SRasmus Villemoes 	if (r < 100)
1947c43d9a3SRasmus Villemoes 		goto out_r;
195cb239d0aSGeorge Spelvin 
1967c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^8 */
1977c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
1987c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
1997c43d9a3SRasmus Villemoes 	buf += 2;
2007c43d9a3SRasmus Villemoes 
2017c43d9a3SRasmus Villemoes 	/* 1 <= q < 10^6 */
2027c43d9a3SRasmus Villemoes 	if (q < 100)
2037c43d9a3SRasmus Villemoes 		goto out_q;
2047c43d9a3SRasmus Villemoes 
2057c43d9a3SRasmus Villemoes 	/*  100 <= q < 10^6 */
2067c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
2077c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
2087c43d9a3SRasmus Villemoes 	buf += 2;
2097c43d9a3SRasmus Villemoes 
2107c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^4 */
2117c43d9a3SRasmus Villemoes 	if (r < 100)
2127c43d9a3SRasmus Villemoes 		goto out_r;
2137c43d9a3SRasmus Villemoes 
2147c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^4 */
2157c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2167c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2177c43d9a3SRasmus Villemoes 	buf += 2;
2187c43d9a3SRasmus Villemoes out_q:
2197c43d9a3SRasmus Villemoes 	/* 1 <= q < 100 */
2207c43d9a3SRasmus Villemoes 	r = q;
2217c43d9a3SRasmus Villemoes out_r:
2227c43d9a3SRasmus Villemoes 	/* 1 <= r < 100 */
2237c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r];
224675cf53cSRasmus Villemoes 	buf += r < 10 ? 1 : 2;
225133fd9f5SDenys Vlasenko 	return buf;
226133fd9f5SDenys Vlasenko }
227133fd9f5SDenys Vlasenko 
2287c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
2297c43d9a3SRasmus Villemoes static noinline_for_stack
2307c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r)
2317c43d9a3SRasmus Villemoes {
2327c43d9a3SRasmus Villemoes 	unsigned q;
233133fd9f5SDenys Vlasenko 
2347c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^8 */
2357c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
2367c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2377c43d9a3SRasmus Villemoes 	buf += 2;
238133fd9f5SDenys Vlasenko 
2397c43d9a3SRasmus Villemoes 	/* 0 <= q < 10^6 */
2407c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
2417c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
2427c43d9a3SRasmus Villemoes 	buf += 2;
243133fd9f5SDenys Vlasenko 
2447c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2457c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2467c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2477c43d9a3SRasmus Villemoes 	buf += 2;
2487c43d9a3SRasmus Villemoes 
2497c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2507c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2517c43d9a3SRasmus Villemoes 	buf += 2;
2527c43d9a3SRasmus Villemoes 	return buf;
2537c43d9a3SRasmus Villemoes }
2547c43d9a3SRasmus Villemoes 
2557c43d9a3SRasmus Villemoes static noinline_for_stack
256133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
257133fd9f5SDenys Vlasenko {
258133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000)
2597c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2607c43d9a3SRasmus Villemoes 	/* 1 <= n <= 1.6e11 */
2617c43d9a3SRasmus Villemoes 	if (n >= 100*1000*1000)
2627c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2637c43d9a3SRasmus Villemoes 	/* 1 <= n < 1e8 */
264133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
265133fd9f5SDenys Vlasenko }
266133fd9f5SDenys Vlasenko 
2677c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
268133fd9f5SDenys Vlasenko 
2697c43d9a3SRasmus Villemoes static void
2707c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r)
271133fd9f5SDenys Vlasenko {
2727c43d9a3SRasmus Villemoes 	unsigned q;
2737c43d9a3SRasmus Villemoes 
2747c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2757c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2767c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2777c43d9a3SRasmus Villemoes 	buf += 2;
2787c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2797c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2802359172aSGeorge Spelvin }
2812359172aSGeorge Spelvin 
2822359172aSGeorge Spelvin /*
2832359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2842359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2852359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2862359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
2877c43d9a3SRasmus Villemoes  * (second call in the put_dec code, assuming n is all-ones).
2882359172aSGeorge Spelvin  */
2897c43d9a3SRasmus Villemoes static noinline_for_stack
2902359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2912359172aSGeorge Spelvin {
2922359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2932359172aSGeorge Spelvin 
2942359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2952359172aSGeorge Spelvin         return q;
296133fd9f5SDenys Vlasenko }
297133fd9f5SDenys Vlasenko 
298133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
299133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
300133fd9f5SDenys Vlasenko  * (with permission from the author).
301133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
302133fd9f5SDenys Vlasenko  */
303133fd9f5SDenys Vlasenko static
304133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
305133fd9f5SDenys Vlasenko {
306133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
307133fd9f5SDenys Vlasenko 
308133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
309133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
310133fd9f5SDenys Vlasenko 
311133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
312133fd9f5SDenys Vlasenko 	h   = (n >> 32);
313133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
314133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
315133fd9f5SDenys Vlasenko 
3167c43d9a3SRasmus Villemoes 	/* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
3177c43d9a3SRasmus Villemoes 	     = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
318133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
3192359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
320133fd9f5SDenys Vlasenko 
3212359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
3222359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
323133fd9f5SDenys Vlasenko 
3242359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
3252359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
326133fd9f5SDenys Vlasenko 
3272359172aSGeorge Spelvin 	q += 281 * d3;
3282359172aSGeorge Spelvin 	buf += 12;
3292359172aSGeorge Spelvin 	if (q)
3302359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3312359172aSGeorge Spelvin 	else while (buf[-1] == '0')
332133fd9f5SDenys Vlasenko 		--buf;
3337b9186f5SAndré Goddard Rosa 
3344277eeddSDenis Vlasenko 	return buf;
3354277eeddSDenis Vlasenko }
336133fd9f5SDenys Vlasenko 
337133fd9f5SDenys Vlasenko #endif
3384277eeddSDenis Vlasenko 
3391ac101a5SKAMEZAWA Hiroyuki /*
3401ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3411ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3421ac101a5SKAMEZAWA Hiroyuki  *
3431ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3441ac101a5SKAMEZAWA Hiroyuki  */
345d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
3461ac101a5SKAMEZAWA Hiroyuki {
3477c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
3487c43d9a3SRasmus Villemoes 	char tmp[sizeof(num) * 3] __aligned(2);
3491ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3501ac101a5SKAMEZAWA Hiroyuki 
351133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
352133fd9f5SDenys Vlasenko 	if (num <= 9) {
353133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
354133fd9f5SDenys Vlasenko 		len = 1;
355133fd9f5SDenys Vlasenko 	} else {
3561ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
357133fd9f5SDenys Vlasenko 	}
3581ac101a5SKAMEZAWA Hiroyuki 
359d1be35cbSAndrei Vagin 	if (len > size || width > size)
3601ac101a5SKAMEZAWA Hiroyuki 		return 0;
361d1be35cbSAndrei Vagin 
362d1be35cbSAndrei Vagin 	if (width > len) {
363d1be35cbSAndrei Vagin 		width = width - len;
364d1be35cbSAndrei Vagin 		for (idx = 0; idx < width; idx++)
365d1be35cbSAndrei Vagin 			buf[idx] = ' ';
366d1be35cbSAndrei Vagin 	} else {
367d1be35cbSAndrei Vagin 		width = 0;
368d1be35cbSAndrei Vagin 	}
369d1be35cbSAndrei Vagin 
3701ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
371d1be35cbSAndrei Vagin 		buf[idx + width] = tmp[len - idx - 1];
372d1be35cbSAndrei Vagin 
373d1be35cbSAndrei Vagin 	return len + width;
3741ac101a5SKAMEZAWA Hiroyuki }
3751ac101a5SKAMEZAWA Hiroyuki 
37651be17dfSRasmus Villemoes #define SIGN	1		/* unsigned/signed, must be 1 */
377d1c1b121SRasmus Villemoes #define LEFT	2		/* left justified */
3781da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3791da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
380d1c1b121SRasmus Villemoes #define ZEROPAD	16		/* pad with zero, must be 16 == '0' - ' ' */
381b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
382b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3831da177e4SLinus Torvalds 
384fef20d9cSFrederic Weisbecker enum format_type {
385fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
386ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
387fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
388fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
389fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
390fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
391fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
392fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
393fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
394fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
395fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
396a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
397a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
398fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
399fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
400fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
401fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
402fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
403fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
404fef20d9cSFrederic Weisbecker };
405fef20d9cSFrederic Weisbecker 
406fef20d9cSFrederic Weisbecker struct printf_spec {
407d0484193SRasmus Villemoes 	unsigned int	type:8;		/* format_type enum */
408d0484193SRasmus Villemoes 	signed int	field_width:24;	/* width of output field */
409d0484193SRasmus Villemoes 	unsigned int	flags:8;	/* flags to number() */
410d0484193SRasmus Villemoes 	unsigned int	base:8;		/* number base, 8, 10 or 16 only */
411d0484193SRasmus Villemoes 	signed int	precision:16;	/* # of digits/chars */
412d0484193SRasmus Villemoes } __packed;
413ef27ac18SRasmus Villemoes static_assert(sizeof(struct printf_spec) == 8);
414ef27ac18SRasmus Villemoes 
4154d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1)
4164d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1)
417fef20d9cSFrederic Weisbecker 
418cf3b429bSJoe Perches static noinline_for_stack
419cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
420fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
4211da177e4SLinus Torvalds {
4227c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
4237c43d9a3SRasmus Villemoes 	char tmp[3 * sizeof(num)] __aligned(2);
4249b706aeeSDenys Vlasenko 	char sign;
4259b706aeeSDenys Vlasenko 	char locase;
426fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4271da177e4SLinus Torvalds 	int i;
4287c203422SPierre Carrier 	bool is_zero = num == 0LL;
4291c7a8e62SRasmus Villemoes 	int field_width = spec.field_width;
4301c7a8e62SRasmus Villemoes 	int precision = spec.precision;
4311da177e4SLinus Torvalds 
4329b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4339b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
434fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
435fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
436fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4371da177e4SLinus Torvalds 	sign = 0;
438fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4391da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4401da177e4SLinus Torvalds 			sign = '-';
4411da177e4SLinus Torvalds 			num = -(signed long long)num;
4421c7a8e62SRasmus Villemoes 			field_width--;
443fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4441da177e4SLinus Torvalds 			sign = '+';
4451c7a8e62SRasmus Villemoes 			field_width--;
446fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4471da177e4SLinus Torvalds 			sign = ' ';
4481c7a8e62SRasmus Villemoes 			field_width--;
4491da177e4SLinus Torvalds 		}
4501da177e4SLinus Torvalds 	}
451b39a7340SDenis Vlasenko 	if (need_pfx) {
452fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4531c7a8e62SRasmus Villemoes 			field_width -= 2;
4547c203422SPierre Carrier 		else if (!is_zero)
4551c7a8e62SRasmus Villemoes 			field_width--;
4561da177e4SLinus Torvalds 	}
457b39a7340SDenis Vlasenko 
458b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4591da177e4SLinus Torvalds 	i = 0;
460133fd9f5SDenys Vlasenko 	if (num < spec.base)
4613ea8d440SRasmus Villemoes 		tmp[i++] = hex_asc_upper[num] | locase;
462fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
463fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
464b39a7340SDenis Vlasenko 		int shift = 3;
4657b9186f5SAndré Goddard Rosa 
4667b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4677b9186f5SAndré Goddard Rosa 			shift = 4;
468b39a7340SDenis Vlasenko 		do {
4693ea8d440SRasmus Villemoes 			tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
470b39a7340SDenis Vlasenko 			num >>= shift;
471b39a7340SDenis Vlasenko 		} while (num);
4724277eeddSDenis Vlasenko 	} else { /* base 10 */
4734277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4744277eeddSDenis Vlasenko 	}
475b39a7340SDenis Vlasenko 
476b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
4771c7a8e62SRasmus Villemoes 	if (i > precision)
4781c7a8e62SRasmus Villemoes 		precision = i;
479b39a7340SDenis Vlasenko 	/* leading space padding */
4801c7a8e62SRasmus Villemoes 	field_width -= precision;
48151be17dfSRasmus Villemoes 	if (!(spec.flags & (ZEROPAD | LEFT))) {
4821c7a8e62SRasmus Villemoes 		while (--field_width >= 0) {
483f796937aSJeremy Fitzhardinge 			if (buf < end)
4841da177e4SLinus Torvalds 				*buf = ' ';
4851da177e4SLinus Torvalds 			++buf;
4861da177e4SLinus Torvalds 		}
4871da177e4SLinus Torvalds 	}
488b39a7340SDenis Vlasenko 	/* sign */
4891da177e4SLinus Torvalds 	if (sign) {
490f796937aSJeremy Fitzhardinge 		if (buf < end)
4911da177e4SLinus Torvalds 			*buf = sign;
4921da177e4SLinus Torvalds 		++buf;
4931da177e4SLinus Torvalds 	}
494b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
495b39a7340SDenis Vlasenko 	if (need_pfx) {
4967c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
497f796937aSJeremy Fitzhardinge 			if (buf < end)
4981da177e4SLinus Torvalds 				*buf = '0';
4991da177e4SLinus Torvalds 			++buf;
5007c203422SPierre Carrier 		}
501fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
502f796937aSJeremy Fitzhardinge 			if (buf < end)
5039b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
5041da177e4SLinus Torvalds 			++buf;
5051da177e4SLinus Torvalds 		}
5061da177e4SLinus Torvalds 	}
507b39a7340SDenis Vlasenko 	/* zero or space padding */
508fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
509d1c1b121SRasmus Villemoes 		char c = ' ' + (spec.flags & ZEROPAD);
510d1c1b121SRasmus Villemoes 		BUILD_BUG_ON(' ' + ZEROPAD != '0');
5111c7a8e62SRasmus Villemoes 		while (--field_width >= 0) {
512f796937aSJeremy Fitzhardinge 			if (buf < end)
5131da177e4SLinus Torvalds 				*buf = c;
5141da177e4SLinus Torvalds 			++buf;
5151da177e4SLinus Torvalds 		}
5161da177e4SLinus Torvalds 	}
517b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
5181c7a8e62SRasmus Villemoes 	while (i <= --precision) {
519f796937aSJeremy Fitzhardinge 		if (buf < end)
5201da177e4SLinus Torvalds 			*buf = '0';
5211da177e4SLinus Torvalds 		++buf;
5221da177e4SLinus Torvalds 	}
523b39a7340SDenis Vlasenko 	/* actual digits of result */
524b39a7340SDenis Vlasenko 	while (--i >= 0) {
525f796937aSJeremy Fitzhardinge 		if (buf < end)
5261da177e4SLinus Torvalds 			*buf = tmp[i];
5271da177e4SLinus Torvalds 		++buf;
5281da177e4SLinus Torvalds 	}
529b39a7340SDenis Vlasenko 	/* trailing space padding */
5301c7a8e62SRasmus Villemoes 	while (--field_width >= 0) {
531f796937aSJeremy Fitzhardinge 		if (buf < end)
5321da177e4SLinus Torvalds 			*buf = ' ';
5331da177e4SLinus Torvalds 		++buf;
5341da177e4SLinus Torvalds 	}
5357b9186f5SAndré Goddard Rosa 
5361da177e4SLinus Torvalds 	return buf;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5393cab1e71SAndy Shevchenko static noinline_for_stack
5403cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
5413cab1e71SAndy Shevchenko {
5423cab1e71SAndy Shevchenko 	struct printf_spec spec;
5433cab1e71SAndy Shevchenko 
5443cab1e71SAndy Shevchenko 	spec.type = FORMAT_TYPE_PTR;
5453cab1e71SAndy Shevchenko 	spec.field_width = 2 + 2 * size;	/* 0x + hex */
5463cab1e71SAndy Shevchenko 	spec.flags = SPECIAL | SMALL | ZEROPAD;
5473cab1e71SAndy Shevchenko 	spec.base = 16;
5483cab1e71SAndy Shevchenko 	spec.precision = -1;
5493cab1e71SAndy Shevchenko 
5503cab1e71SAndy Shevchenko 	return number(buf, end, num, spec);
5513cab1e71SAndy Shevchenko }
5523cab1e71SAndy Shevchenko 
553cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
5544b6ccca7SAl Viro {
5554b6ccca7SAl Viro 	size_t size;
5564b6ccca7SAl Viro 	if (buf >= end)	/* nowhere to put anything */
5574b6ccca7SAl Viro 		return;
5584b6ccca7SAl Viro 	size = end - buf;
5594b6ccca7SAl Viro 	if (size <= spaces) {
5604b6ccca7SAl Viro 		memset(buf, ' ', size);
5614b6ccca7SAl Viro 		return;
5624b6ccca7SAl Viro 	}
5634b6ccca7SAl Viro 	if (len) {
5644b6ccca7SAl Viro 		if (len > size - spaces)
5654b6ccca7SAl Viro 			len = size - spaces;
5664b6ccca7SAl Viro 		memmove(buf + spaces, buf, len);
5674b6ccca7SAl Viro 	}
5684b6ccca7SAl Viro 	memset(buf, ' ', spaces);
5694b6ccca7SAl Viro }
5704b6ccca7SAl Viro 
571cfccde04SRasmus Villemoes /*
572cfccde04SRasmus Villemoes  * Handle field width padding for a string.
573cfccde04SRasmus Villemoes  * @buf: current buffer position
574cfccde04SRasmus Villemoes  * @n: length of string
575cfccde04SRasmus Villemoes  * @end: end of output buffer
576cfccde04SRasmus Villemoes  * @spec: for field width and flags
577cfccde04SRasmus Villemoes  * Returns: new buffer position after padding.
578cfccde04SRasmus Villemoes  */
579cfccde04SRasmus Villemoes static noinline_for_stack
580cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
581cfccde04SRasmus Villemoes {
582cfccde04SRasmus Villemoes 	unsigned spaces;
583cfccde04SRasmus Villemoes 
584cfccde04SRasmus Villemoes 	if (likely(n >= spec.field_width))
585cfccde04SRasmus Villemoes 		return buf;
586cfccde04SRasmus Villemoes 	/* we want to pad the sucker */
587cfccde04SRasmus Villemoes 	spaces = spec.field_width - n;
588cfccde04SRasmus Villemoes 	if (!(spec.flags & LEFT)) {
589cfccde04SRasmus Villemoes 		move_right(buf - n, end, n, spaces);
590cfccde04SRasmus Villemoes 		return buf + spaces;
591cfccde04SRasmus Villemoes 	}
592cfccde04SRasmus Villemoes 	while (spaces--) {
593cfccde04SRasmus Villemoes 		if (buf < end)
594cfccde04SRasmus Villemoes 			*buf = ' ';
595cfccde04SRasmus Villemoes 		++buf;
596cfccde04SRasmus Villemoes 	}
597cfccde04SRasmus Villemoes 	return buf;
598cfccde04SRasmus Villemoes }
599cfccde04SRasmus Villemoes 
600d529ac41SPetr Mladek /* Handle string from a well known address. */
601d529ac41SPetr Mladek static char *string_nocheck(char *buf, char *end, const char *s,
602d529ac41SPetr Mladek 			    struct printf_spec spec)
60395508cfaSRasmus Villemoes {
60434fc8b90SRasmus Villemoes 	int len = 0;
605b314dd49SYoungmin Nam 	int lim = spec.precision;
60695508cfaSRasmus Villemoes 
60734fc8b90SRasmus Villemoes 	while (lim--) {
60834fc8b90SRasmus Villemoes 		char c = *s++;
60934fc8b90SRasmus Villemoes 		if (!c)
61034fc8b90SRasmus Villemoes 			break;
61195508cfaSRasmus Villemoes 		if (buf < end)
61234fc8b90SRasmus Villemoes 			*buf = c;
61395508cfaSRasmus Villemoes 		++buf;
61434fc8b90SRasmus Villemoes 		++len;
61595508cfaSRasmus Villemoes 	}
61634fc8b90SRasmus Villemoes 	return widen_string(buf, len, end, spec);
61795508cfaSRasmus Villemoes }
61895508cfaSRasmus Villemoes 
61957f5677eSRasmus Villemoes static char *err_ptr(char *buf, char *end, void *ptr,
62057f5677eSRasmus Villemoes 		     struct printf_spec spec)
62157f5677eSRasmus Villemoes {
62257f5677eSRasmus Villemoes 	int err = PTR_ERR(ptr);
62357f5677eSRasmus Villemoes 	const char *sym = errname(err);
62457f5677eSRasmus Villemoes 
62557f5677eSRasmus Villemoes 	if (sym)
62657f5677eSRasmus Villemoes 		return string_nocheck(buf, end, sym, spec);
62757f5677eSRasmus Villemoes 
62857f5677eSRasmus Villemoes 	/*
62957f5677eSRasmus Villemoes 	 * Somebody passed ERR_PTR(-1234) or some other non-existing
63057f5677eSRasmus Villemoes 	 * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
63157f5677eSRasmus Villemoes 	 * printing it as its decimal representation.
63257f5677eSRasmus Villemoes 	 */
63357f5677eSRasmus Villemoes 	spec.flags |= SIGN;
63457f5677eSRasmus Villemoes 	spec.base = 10;
63557f5677eSRasmus Villemoes 	return number(buf, end, err, spec);
63657f5677eSRasmus Villemoes }
63757f5677eSRasmus Villemoes 
638c8c3b584SPetr Mladek /* Be careful: error messages must fit into the given buffer. */
639c8c3b584SPetr Mladek static char *error_string(char *buf, char *end, const char *s,
640c8c3b584SPetr Mladek 			  struct printf_spec spec)
641c8c3b584SPetr Mladek {
642c8c3b584SPetr Mladek 	/*
643c8c3b584SPetr Mladek 	 * Hard limit to avoid a completely insane messages. It actually
644c8c3b584SPetr Mladek 	 * works pretty well because most error messages are in
645c8c3b584SPetr Mladek 	 * the many pointer format modifiers.
646c8c3b584SPetr Mladek 	 */
647c8c3b584SPetr Mladek 	if (spec.precision == -1)
648c8c3b584SPetr Mladek 		spec.precision = 2 * sizeof(void *);
649c8c3b584SPetr Mladek 
650c8c3b584SPetr Mladek 	return string_nocheck(buf, end, s, spec);
651c8c3b584SPetr Mladek }
652c8c3b584SPetr Mladek 
6533e5903ebSPetr Mladek /*
6542ac5a3bfSPetr Mladek  * Do not call any complex external code here. Nested printk()/vsprintf()
6552ac5a3bfSPetr Mladek  * might cause infinite loops. Failures might break printk() and would
6562ac5a3bfSPetr Mladek  * be hard to debug.
6573e5903ebSPetr Mladek  */
6583e5903ebSPetr Mladek static const char *check_pointer_msg(const void *ptr)
6593e5903ebSPetr Mladek {
6603e5903ebSPetr Mladek 	if (!ptr)
6613e5903ebSPetr Mladek 		return "(null)";
6623e5903ebSPetr Mladek 
6632ac5a3bfSPetr Mladek 	if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
6643e5903ebSPetr Mladek 		return "(efault)";
6653e5903ebSPetr Mladek 
6663e5903ebSPetr Mladek 	return NULL;
6673e5903ebSPetr Mladek }
6683e5903ebSPetr Mladek 
6693e5903ebSPetr Mladek static int check_pointer(char **buf, char *end, const void *ptr,
6703e5903ebSPetr Mladek 			 struct printf_spec spec)
6713e5903ebSPetr Mladek {
6723e5903ebSPetr Mladek 	const char *err_msg;
6733e5903ebSPetr Mladek 
6743e5903ebSPetr Mladek 	err_msg = check_pointer_msg(ptr);
6753e5903ebSPetr Mladek 	if (err_msg) {
676c8c3b584SPetr Mladek 		*buf = error_string(*buf, end, err_msg, spec);
6773e5903ebSPetr Mladek 		return -EFAULT;
6783e5903ebSPetr Mladek 	}
6793e5903ebSPetr Mladek 
6803e5903ebSPetr Mladek 	return 0;
6813e5903ebSPetr Mladek }
6823e5903ebSPetr Mladek 
68395508cfaSRasmus Villemoes static noinline_for_stack
684d529ac41SPetr Mladek char *string(char *buf, char *end, const char *s,
685d529ac41SPetr Mladek 	     struct printf_spec spec)
686d529ac41SPetr Mladek {
6873e5903ebSPetr Mladek 	if (check_pointer(&buf, end, s, spec))
6883e5903ebSPetr Mladek 		return buf;
689d529ac41SPetr Mladek 
690d529ac41SPetr Mladek 	return string_nocheck(buf, end, s, spec);
691d529ac41SPetr Mladek }
692d529ac41SPetr Mladek 
693ce9d3eceSYueHaibing static char *pointer_string(char *buf, char *end,
694ce9d3eceSYueHaibing 			    const void *ptr,
6959073dac1SGeert Uytterhoeven 			    struct printf_spec spec)
6969073dac1SGeert Uytterhoeven {
6979073dac1SGeert Uytterhoeven 	spec.base = 16;
6989073dac1SGeert Uytterhoeven 	spec.flags |= SMALL;
6999073dac1SGeert Uytterhoeven 	if (spec.field_width == -1) {
7009073dac1SGeert Uytterhoeven 		spec.field_width = 2 * sizeof(ptr);
7019073dac1SGeert Uytterhoeven 		spec.flags |= ZEROPAD;
7029073dac1SGeert Uytterhoeven 	}
7039073dac1SGeert Uytterhoeven 
7049073dac1SGeert Uytterhoeven 	return number(buf, end, (unsigned long int)ptr, spec);
7059073dac1SGeert Uytterhoeven }
7069073dac1SGeert Uytterhoeven 
7079073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */
7089073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init;
7099073dac1SGeert Uytterhoeven 
7109073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str)
7119073dac1SGeert Uytterhoeven {
7129073dac1SGeert Uytterhoeven 	debug_boot_weak_hash = 1;
7139073dac1SGeert Uytterhoeven 	pr_info("debug_boot_weak_hash enabled\n");
7149073dac1SGeert Uytterhoeven 	return 0;
7159073dac1SGeert Uytterhoeven }
7169073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
7179073dac1SGeert Uytterhoeven 
7189073dac1SGeert Uytterhoeven static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
7199073dac1SGeert Uytterhoeven static siphash_key_t ptr_key __read_mostly;
7209073dac1SGeert Uytterhoeven 
7219073dac1SGeert Uytterhoeven static void enable_ptr_key_workfn(struct work_struct *work)
7229073dac1SGeert Uytterhoeven {
7239073dac1SGeert Uytterhoeven 	get_random_bytes(&ptr_key, sizeof(ptr_key));
7249073dac1SGeert Uytterhoeven 	/* Needs to run from preemptible context */
7259073dac1SGeert Uytterhoeven 	static_branch_disable(&not_filled_random_ptr_key);
7269073dac1SGeert Uytterhoeven }
7279073dac1SGeert Uytterhoeven 
7289073dac1SGeert Uytterhoeven static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
7299073dac1SGeert Uytterhoeven 
7309073dac1SGeert Uytterhoeven static void fill_random_ptr_key(struct random_ready_callback *unused)
7319073dac1SGeert Uytterhoeven {
7329073dac1SGeert Uytterhoeven 	/* This may be in an interrupt handler. */
7339073dac1SGeert Uytterhoeven 	queue_work(system_unbound_wq, &enable_ptr_key_work);
7349073dac1SGeert Uytterhoeven }
7359073dac1SGeert Uytterhoeven 
7369073dac1SGeert Uytterhoeven static struct random_ready_callback random_ready = {
7379073dac1SGeert Uytterhoeven 	.func = fill_random_ptr_key
7389073dac1SGeert Uytterhoeven };
7399073dac1SGeert Uytterhoeven 
7409073dac1SGeert Uytterhoeven static int __init initialize_ptr_random(void)
7419073dac1SGeert Uytterhoeven {
7429073dac1SGeert Uytterhoeven 	int key_size = sizeof(ptr_key);
7439073dac1SGeert Uytterhoeven 	int ret;
7449073dac1SGeert Uytterhoeven 
7459073dac1SGeert Uytterhoeven 	/* Use hw RNG if available. */
7469073dac1SGeert Uytterhoeven 	if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
7479073dac1SGeert Uytterhoeven 		static_branch_disable(&not_filled_random_ptr_key);
7489073dac1SGeert Uytterhoeven 		return 0;
7499073dac1SGeert Uytterhoeven 	}
7509073dac1SGeert Uytterhoeven 
7519073dac1SGeert Uytterhoeven 	ret = add_random_ready_callback(&random_ready);
7529073dac1SGeert Uytterhoeven 	if (!ret) {
7539073dac1SGeert Uytterhoeven 		return 0;
7549073dac1SGeert Uytterhoeven 	} else if (ret == -EALREADY) {
7559073dac1SGeert Uytterhoeven 		/* This is in preemptible context */
7569073dac1SGeert Uytterhoeven 		enable_ptr_key_workfn(&enable_ptr_key_work);
7579073dac1SGeert Uytterhoeven 		return 0;
7589073dac1SGeert Uytterhoeven 	}
7599073dac1SGeert Uytterhoeven 
7609073dac1SGeert Uytterhoeven 	return ret;
7619073dac1SGeert Uytterhoeven }
7629073dac1SGeert Uytterhoeven early_initcall(initialize_ptr_random);
7639073dac1SGeert Uytterhoeven 
7649073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */
765e4dcad20SJoel Fernandes (Google) static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
7669073dac1SGeert Uytterhoeven {
7679073dac1SGeert Uytterhoeven 	unsigned long hashval;
7689073dac1SGeert Uytterhoeven 
769e4dcad20SJoel Fernandes (Google) 	if (static_branch_unlikely(&not_filled_random_ptr_key))
770e4dcad20SJoel Fernandes (Google) 		return -EAGAIN;
7719073dac1SGeert Uytterhoeven 
7729073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT
7739073dac1SGeert Uytterhoeven 	hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
7749073dac1SGeert Uytterhoeven 	/*
7759073dac1SGeert Uytterhoeven 	 * Mask off the first 32 bits, this makes explicit that we have
7769073dac1SGeert Uytterhoeven 	 * modified the address (and 32 bits is plenty for a unique ID).
7779073dac1SGeert Uytterhoeven 	 */
7789073dac1SGeert Uytterhoeven 	hashval = hashval & 0xffffffff;
7799073dac1SGeert Uytterhoeven #else
7809073dac1SGeert Uytterhoeven 	hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
7819073dac1SGeert Uytterhoeven #endif
782e4dcad20SJoel Fernandes (Google) 	*hashval_out = hashval;
783e4dcad20SJoel Fernandes (Google) 	return 0;
784e4dcad20SJoel Fernandes (Google) }
785e4dcad20SJoel Fernandes (Google) 
786e4dcad20SJoel Fernandes (Google) int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
787e4dcad20SJoel Fernandes (Google) {
788e4dcad20SJoel Fernandes (Google) 	return __ptr_to_hashval(ptr, hashval_out);
789e4dcad20SJoel Fernandes (Google) }
790e4dcad20SJoel Fernandes (Google) 
791e4dcad20SJoel Fernandes (Google) static char *ptr_to_id(char *buf, char *end, const void *ptr,
792e4dcad20SJoel Fernandes (Google) 		       struct printf_spec spec)
793e4dcad20SJoel Fernandes (Google) {
794e4dcad20SJoel Fernandes (Google) 	const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
795e4dcad20SJoel Fernandes (Google) 	unsigned long hashval;
796e4dcad20SJoel Fernandes (Google) 	int ret;
797e4dcad20SJoel Fernandes (Google) 
7987bd57fbcSIlya Dryomov 	/*
7997bd57fbcSIlya Dryomov 	 * Print the real pointer value for NULL and error pointers,
8007bd57fbcSIlya Dryomov 	 * as they are not actual addresses.
8017bd57fbcSIlya Dryomov 	 */
8027bd57fbcSIlya Dryomov 	if (IS_ERR_OR_NULL(ptr))
8037bd57fbcSIlya Dryomov 		return pointer_string(buf, end, ptr, spec);
8047bd57fbcSIlya Dryomov 
805e4dcad20SJoel Fernandes (Google) 	/* When debugging early boot use non-cryptographically secure hash. */
806e4dcad20SJoel Fernandes (Google) 	if (unlikely(debug_boot_weak_hash)) {
807e4dcad20SJoel Fernandes (Google) 		hashval = hash_long((unsigned long)ptr, 32);
808e4dcad20SJoel Fernandes (Google) 		return pointer_string(buf, end, (const void *)hashval, spec);
809e4dcad20SJoel Fernandes (Google) 	}
810e4dcad20SJoel Fernandes (Google) 
811e4dcad20SJoel Fernandes (Google) 	ret = __ptr_to_hashval(ptr, &hashval);
812e4dcad20SJoel Fernandes (Google) 	if (ret) {
813e4dcad20SJoel Fernandes (Google) 		spec.field_width = 2 * sizeof(ptr);
814e4dcad20SJoel Fernandes (Google) 		/* string length must be less than default_width */
815e4dcad20SJoel Fernandes (Google) 		return error_string(buf, end, str, spec);
816e4dcad20SJoel Fernandes (Google) 	}
817e4dcad20SJoel Fernandes (Google) 
8189073dac1SGeert Uytterhoeven 	return pointer_string(buf, end, (const void *)hashval, spec);
8199073dac1SGeert Uytterhoeven }
8209073dac1SGeert Uytterhoeven 
8216eea242fSPetr Mladek int kptr_restrict __read_mostly;
8226eea242fSPetr Mladek 
8236eea242fSPetr Mladek static noinline_for_stack
8246eea242fSPetr Mladek char *restricted_pointer(char *buf, char *end, const void *ptr,
8256eea242fSPetr Mladek 			 struct printf_spec spec)
8266eea242fSPetr Mladek {
8276eea242fSPetr Mladek 	switch (kptr_restrict) {
8286eea242fSPetr Mladek 	case 0:
8291ac2f978SPetr Mladek 		/* Handle as %p, hash and do _not_ leak addresses. */
8301ac2f978SPetr Mladek 		return ptr_to_id(buf, end, ptr, spec);
8316eea242fSPetr Mladek 	case 1: {
8326eea242fSPetr Mladek 		const struct cred *cred;
8336eea242fSPetr Mladek 
8346eea242fSPetr Mladek 		/*
8356eea242fSPetr Mladek 		 * kptr_restrict==1 cannot be used in IRQ context
8366eea242fSPetr Mladek 		 * because its test for CAP_SYSLOG would be meaningless.
8376eea242fSPetr Mladek 		 */
8386eea242fSPetr Mladek 		if (in_irq() || in_serving_softirq() || in_nmi()) {
8396eea242fSPetr Mladek 			if (spec.field_width == -1)
8406eea242fSPetr Mladek 				spec.field_width = 2 * sizeof(ptr);
841c8c3b584SPetr Mladek 			return error_string(buf, end, "pK-error", spec);
8426eea242fSPetr Mladek 		}
8436eea242fSPetr Mladek 
8446eea242fSPetr Mladek 		/*
8456eea242fSPetr Mladek 		 * Only print the real pointer value if the current
8466eea242fSPetr Mladek 		 * process has CAP_SYSLOG and is running with the
8476eea242fSPetr Mladek 		 * same credentials it started with. This is because
8486eea242fSPetr Mladek 		 * access to files is checked at open() time, but %pK
8496eea242fSPetr Mladek 		 * checks permission at read() time. We don't want to
8506eea242fSPetr Mladek 		 * leak pointer values if a binary opens a file using
8516eea242fSPetr Mladek 		 * %pK and then elevates privileges before reading it.
8526eea242fSPetr Mladek 		 */
8536eea242fSPetr Mladek 		cred = current_cred();
8546eea242fSPetr Mladek 		if (!has_capability_noaudit(current, CAP_SYSLOG) ||
8556eea242fSPetr Mladek 		    !uid_eq(cred->euid, cred->uid) ||
8566eea242fSPetr Mladek 		    !gid_eq(cred->egid, cred->gid))
8576eea242fSPetr Mladek 			ptr = NULL;
8586eea242fSPetr Mladek 		break;
8596eea242fSPetr Mladek 	}
8606eea242fSPetr Mladek 	case 2:
8616eea242fSPetr Mladek 	default:
8626eea242fSPetr Mladek 		/* Always print 0's for %pK */
8636eea242fSPetr Mladek 		ptr = NULL;
8646eea242fSPetr Mladek 		break;
8656eea242fSPetr Mladek 	}
8666eea242fSPetr Mladek 
8676eea242fSPetr Mladek 	return pointer_string(buf, end, ptr, spec);
8686eea242fSPetr Mladek }
8696eea242fSPetr Mladek 
8709073dac1SGeert Uytterhoeven static noinline_for_stack
8714b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
8724b6ccca7SAl Viro 		  const char *fmt)
8734b6ccca7SAl Viro {
8744b6ccca7SAl Viro 	const char *array[4], *s;
8754b6ccca7SAl Viro 	const struct dentry *p;
8764b6ccca7SAl Viro 	int depth;
8774b6ccca7SAl Viro 	int i, n;
8784b6ccca7SAl Viro 
8794b6ccca7SAl Viro 	switch (fmt[1]) {
8804b6ccca7SAl Viro 		case '2': case '3': case '4':
8814b6ccca7SAl Viro 			depth = fmt[1] - '0';
8824b6ccca7SAl Viro 			break;
8834b6ccca7SAl Viro 		default:
8844b6ccca7SAl Viro 			depth = 1;
8854b6ccca7SAl Viro 	}
8864b6ccca7SAl Viro 
8874b6ccca7SAl Viro 	rcu_read_lock();
8884b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
8893e5903ebSPetr Mladek 		if (check_pointer(&buf, end, d, spec)) {
8903e5903ebSPetr Mladek 			rcu_read_unlock();
8913e5903ebSPetr Mladek 			return buf;
8923e5903ebSPetr Mladek 		}
8933e5903ebSPetr Mladek 
8946aa7de05SMark Rutland 		p = READ_ONCE(d->d_parent);
8956aa7de05SMark Rutland 		array[i] = READ_ONCE(d->d_name.name);
8964b6ccca7SAl Viro 		if (p == d) {
8974b6ccca7SAl Viro 			if (i)
8984b6ccca7SAl Viro 				array[i] = "";
8994b6ccca7SAl Viro 			i++;
9004b6ccca7SAl Viro 			break;
9014b6ccca7SAl Viro 		}
9024b6ccca7SAl Viro 	}
9034b6ccca7SAl Viro 	s = array[--i];
9044b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
9054b6ccca7SAl Viro 		char c = *s++;
9064b6ccca7SAl Viro 		if (!c) {
9074b6ccca7SAl Viro 			if (!i)
9084b6ccca7SAl Viro 				break;
9094b6ccca7SAl Viro 			c = '/';
9104b6ccca7SAl Viro 			s = array[--i];
9114b6ccca7SAl Viro 		}
9124b6ccca7SAl Viro 		if (buf < end)
9134b6ccca7SAl Viro 			*buf = c;
9144b6ccca7SAl Viro 	}
9154b6ccca7SAl Viro 	rcu_read_unlock();
916cfccde04SRasmus Villemoes 	return widen_string(buf, n, end, spec);
9174b6ccca7SAl Viro }
9184b6ccca7SAl Viro 
91936594b31SJia He static noinline_for_stack
92036594b31SJia He char *file_dentry_name(char *buf, char *end, const struct file *f,
92136594b31SJia He 			struct printf_spec spec, const char *fmt)
92236594b31SJia He {
92336594b31SJia He 	if (check_pointer(&buf, end, f, spec))
92436594b31SJia He 		return buf;
92536594b31SJia He 
92636594b31SJia He 	return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
92736594b31SJia He }
9281031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
9291031bc58SDmitry Monakhov static noinline_for_stack
9301031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev,
9311031bc58SDmitry Monakhov 		struct printf_spec spec, const char *fmt)
9321031bc58SDmitry Monakhov {
9333e5903ebSPetr Mladek 	struct gendisk *hd;
9341031bc58SDmitry Monakhov 
9353e5903ebSPetr Mladek 	if (check_pointer(&buf, end, bdev, spec))
9363e5903ebSPetr Mladek 		return buf;
9373e5903ebSPetr Mladek 
9383e5903ebSPetr Mladek 	hd = bdev->bd_disk;
9391031bc58SDmitry Monakhov 	buf = string(buf, end, hd->disk_name, spec);
9401031bc58SDmitry Monakhov 	if (bdev->bd_part->partno) {
9411031bc58SDmitry Monakhov 		if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
9421031bc58SDmitry Monakhov 			if (buf < end)
9431031bc58SDmitry Monakhov 				*buf = 'p';
9441031bc58SDmitry Monakhov 			buf++;
9451031bc58SDmitry Monakhov 		}
9461031bc58SDmitry Monakhov 		buf = number(buf, end, bdev->bd_part->partno, spec);
9471031bc58SDmitry Monakhov 	}
9481031bc58SDmitry Monakhov 	return buf;
9491031bc58SDmitry Monakhov }
9501031bc58SDmitry Monakhov #endif
9511031bc58SDmitry Monakhov 
952cf3b429bSJoe Perches static noinline_for_stack
953cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
954b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
9550fe1ef24SLinus Torvalds {
956b0d33c2bSJoe Perches 	unsigned long value;
9570fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
9580fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
959b0d33c2bSJoe Perches #endif
960b0d33c2bSJoe Perches 
961b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
962b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
963b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
964b0d33c2bSJoe Perches 
965b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
966b0d33c2bSJoe Perches 	if (*fmt == 'B')
9670f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
9689af77064SSakari Ailus 	else if (*fmt != 's')
9690fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
9700c8b946eSFrederic Weisbecker 	else
9714796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
9727b9186f5SAndré Goddard Rosa 
973d529ac41SPetr Mladek 	return string_nocheck(buf, end, sym, spec);
9740fe1ef24SLinus Torvalds #else
9753cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, value, sizeof(void *));
9760fe1ef24SLinus Torvalds #endif
9770fe1ef24SLinus Torvalds }
9780fe1ef24SLinus Torvalds 
979abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = {
980abd4fe62SAndy Shevchenko 	.field_width = -1,
981abd4fe62SAndy Shevchenko 	.precision = -1,
982abd4fe62SAndy Shevchenko };
983abd4fe62SAndy Shevchenko 
98454433973SAndy Shevchenko static const struct printf_spec default_flag_spec = {
98554433973SAndy Shevchenko 	.base = 16,
98654433973SAndy Shevchenko 	.precision = -1,
98754433973SAndy Shevchenko 	.flags = SPECIAL | SMALL,
98854433973SAndy Shevchenko };
98954433973SAndy Shevchenko 
990ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = {
991ce0b4910SAndy Shevchenko 	.base = 10,
992ce0b4910SAndy Shevchenko 	.precision = -1,
993ce0b4910SAndy Shevchenko };
994ce0b4910SAndy Shevchenko 
9954d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = {
9964d42c447SAndy Shevchenko 	.base = 10,
9974d42c447SAndy Shevchenko 	.field_width = 2,
9984d42c447SAndy Shevchenko 	.precision = -1,
9994d42c447SAndy Shevchenko 	.flags = ZEROPAD,
10004d42c447SAndy Shevchenko };
10014d42c447SAndy Shevchenko 
10024d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = {
10034d42c447SAndy Shevchenko 	.base = 10,
10044d42c447SAndy Shevchenko 	.field_width = 4,
10054d42c447SAndy Shevchenko 	.precision = -1,
10064d42c447SAndy Shevchenko 	.flags = ZEROPAD,
10074d42c447SAndy Shevchenko };
10084d42c447SAndy Shevchenko 
1009cf3b429bSJoe Perches static noinline_for_stack
1010cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
1011fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
1012332d2e78SLinus Torvalds {
1013332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
101428405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
1015332d2e78SLinus Torvalds #endif
1016332d2e78SLinus Torvalds 
1017332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
101828405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
1019332d2e78SLinus Torvalds #endif
10204da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
1021fef20d9cSFrederic Weisbecker 		.base = 16,
10224da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
1023fef20d9cSFrederic Weisbecker 		.precision = -1,
1024fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
1025fef20d9cSFrederic Weisbecker 	};
10264da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
10274da0b66cSBjorn Helgaas 		.base = 16,
10284da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
10294da0b66cSBjorn Helgaas 		.precision = -1,
10304da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
10314da0b66cSBjorn Helgaas 	};
10320f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
10330f4050c7SBjorn Helgaas 		.base = 16,
10340f4050c7SBjorn Helgaas 		.field_width = 2,
10350f4050c7SBjorn Helgaas 		.precision = -1,
10360f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
10370f4050c7SBjorn Helgaas 	};
10384da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
1039fd95541eSBjorn Helgaas 		.field_width = -1,
1040fd95541eSBjorn Helgaas 		.precision = 10,
1041fd95541eSBjorn Helgaas 		.flags = LEFT,
1042fd95541eSBjorn Helgaas 	};
1043c7dabef8SBjorn Helgaas 
1044c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1045c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1046c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
1047c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
10489d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
1049c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
1050c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1051c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1052c7dabef8SBjorn Helgaas 
1053332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
1054c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
10554da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
1056332d2e78SLinus Torvalds 
10573e5903ebSPetr Mladek 	if (check_pointer(&buf, end, res, spec))
10583e5903ebSPetr Mladek 		return buf;
10593e5903ebSPetr Mladek 
1060332d2e78SLinus Torvalds 	*p++ = '[';
10614da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
1062d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "io  ", str_spec);
10634da0b66cSBjorn Helgaas 		specp = &io_spec;
10644da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
1065d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "mem ", str_spec);
10664da0b66cSBjorn Helgaas 		specp = &mem_spec;
10674da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
1068d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "irq ", str_spec);
1069ce0b4910SAndy Shevchenko 		specp = &default_dec_spec;
10704da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
1071d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "dma ", str_spec);
1072ce0b4910SAndy Shevchenko 		specp = &default_dec_spec;
10730f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
1074d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "bus ", str_spec);
10750f4050c7SBjorn Helgaas 		specp = &bus_spec;
10764da0b66cSBjorn Helgaas 	} else {
1077d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "??? ", str_spec);
10784da0b66cSBjorn Helgaas 		specp = &mem_spec;
1079c7dabef8SBjorn Helgaas 		decode = 0;
1080fd95541eSBjorn Helgaas 	}
1081d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
1082d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "size ", str_spec);
1083d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
1084d19cb803SBjorn Helgaas 	} else {
10854da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
1086c91d3376SBjorn Helgaas 		if (res->start != res->end) {
1087332d2e78SLinus Torvalds 			*p++ = '-';
10884da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
1089c91d3376SBjorn Helgaas 		}
1090d19cb803SBjorn Helgaas 	}
1091c7dabef8SBjorn Helgaas 	if (decode) {
1092fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
1093d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " 64bit", str_spec);
1094fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
1095d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " pref", str_spec);
10969d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
1097d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " window", str_spec);
1098fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
1099d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " disabled", str_spec);
1100c7dabef8SBjorn Helgaas 	} else {
1101d529ac41SPetr Mladek 		p = string_nocheck(p, pend, " flags ", str_spec);
110254433973SAndy Shevchenko 		p = number(p, pend, res->flags, default_flag_spec);
1103fd95541eSBjorn Helgaas 	}
1104332d2e78SLinus Torvalds 	*p++ = ']';
1105c7dabef8SBjorn Helgaas 	*p = '\0';
1106332d2e78SLinus Torvalds 
1107d529ac41SPetr Mladek 	return string_nocheck(buf, end, sym, spec);
1108332d2e78SLinus Torvalds }
1109332d2e78SLinus Torvalds 
1110cf3b429bSJoe Perches static noinline_for_stack
111131550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
111231550a16SAndy Shevchenko 		 const char *fmt)
111331550a16SAndy Shevchenko {
1114360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
111531550a16SAndy Shevchenko 				   negative value, fallback to the default */
111631550a16SAndy Shevchenko 	char separator;
111731550a16SAndy Shevchenko 
111831550a16SAndy Shevchenko 	if (spec.field_width == 0)
111931550a16SAndy Shevchenko 		/* nothing to print */
112031550a16SAndy Shevchenko 		return buf;
112131550a16SAndy Shevchenko 
11223e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
11233e5903ebSPetr Mladek 		return buf;
112431550a16SAndy Shevchenko 
112531550a16SAndy Shevchenko 	switch (fmt[1]) {
112631550a16SAndy Shevchenko 	case 'C':
112731550a16SAndy Shevchenko 		separator = ':';
112831550a16SAndy Shevchenko 		break;
112931550a16SAndy Shevchenko 	case 'D':
113031550a16SAndy Shevchenko 		separator = '-';
113131550a16SAndy Shevchenko 		break;
113231550a16SAndy Shevchenko 	case 'N':
113331550a16SAndy Shevchenko 		separator = 0;
113431550a16SAndy Shevchenko 		break;
113531550a16SAndy Shevchenko 	default:
113631550a16SAndy Shevchenko 		separator = ' ';
113731550a16SAndy Shevchenko 		break;
113831550a16SAndy Shevchenko 	}
113931550a16SAndy Shevchenko 
114031550a16SAndy Shevchenko 	if (spec.field_width > 0)
114131550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
114231550a16SAndy Shevchenko 
11439c98f235SRasmus Villemoes 	for (i = 0; i < len; ++i) {
11449c98f235SRasmus Villemoes 		if (buf < end)
11459c98f235SRasmus Villemoes 			*buf = hex_asc_hi(addr[i]);
11469c98f235SRasmus Villemoes 		++buf;
11479c98f235SRasmus Villemoes 		if (buf < end)
11489c98f235SRasmus Villemoes 			*buf = hex_asc_lo(addr[i]);
11499c98f235SRasmus Villemoes 		++buf;
115031550a16SAndy Shevchenko 
11519c98f235SRasmus Villemoes 		if (separator && i != len - 1) {
11529c98f235SRasmus Villemoes 			if (buf < end)
11539c98f235SRasmus Villemoes 				*buf = separator;
11549c98f235SRasmus Villemoes 			++buf;
11559c98f235SRasmus Villemoes 		}
115631550a16SAndy Shevchenko 	}
115731550a16SAndy Shevchenko 
115831550a16SAndy Shevchenko 	return buf;
115931550a16SAndy Shevchenko }
116031550a16SAndy Shevchenko 
116131550a16SAndy Shevchenko static noinline_for_stack
1162dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1163dbc760bcSTejun Heo 		    struct printf_spec spec, const char *fmt)
1164dbc760bcSTejun Heo {
1165dbc760bcSTejun Heo 	const int CHUNKSZ = 32;
1166dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
1167dbc760bcSTejun Heo 	int i, chunksz;
1168dbc760bcSTejun Heo 	bool first = true;
1169dbc760bcSTejun Heo 
11703e5903ebSPetr Mladek 	if (check_pointer(&buf, end, bitmap, spec))
11713e5903ebSPetr Mladek 		return buf;
11723e5903ebSPetr Mladek 
1173dbc760bcSTejun Heo 	/* reused to print numbers */
1174dbc760bcSTejun Heo 	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1175dbc760bcSTejun Heo 
1176dbc760bcSTejun Heo 	chunksz = nr_bits & (CHUNKSZ - 1);
1177dbc760bcSTejun Heo 	if (chunksz == 0)
1178dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
1179dbc760bcSTejun Heo 
1180dbc760bcSTejun Heo 	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1181dbc760bcSTejun Heo 	for (; i >= 0; i -= CHUNKSZ) {
1182dbc760bcSTejun Heo 		u32 chunkmask, val;
1183dbc760bcSTejun Heo 		int word, bit;
1184dbc760bcSTejun Heo 
1185dbc760bcSTejun Heo 		chunkmask = ((1ULL << chunksz) - 1);
1186dbc760bcSTejun Heo 		word = i / BITS_PER_LONG;
1187dbc760bcSTejun Heo 		bit = i % BITS_PER_LONG;
1188dbc760bcSTejun Heo 		val = (bitmap[word] >> bit) & chunkmask;
1189dbc760bcSTejun Heo 
1190dbc760bcSTejun Heo 		if (!first) {
1191dbc760bcSTejun Heo 			if (buf < end)
1192dbc760bcSTejun Heo 				*buf = ',';
1193dbc760bcSTejun Heo 			buf++;
1194dbc760bcSTejun Heo 		}
1195dbc760bcSTejun Heo 		first = false;
1196dbc760bcSTejun Heo 
1197dbc760bcSTejun Heo 		spec.field_width = DIV_ROUND_UP(chunksz, 4);
1198dbc760bcSTejun Heo 		buf = number(buf, end, val, spec);
1199dbc760bcSTejun Heo 
1200dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
1201dbc760bcSTejun Heo 	}
1202dbc760bcSTejun Heo 	return buf;
1203dbc760bcSTejun Heo }
1204dbc760bcSTejun Heo 
1205dbc760bcSTejun Heo static noinline_for_stack
1206dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1207dbc760bcSTejun Heo 			 struct printf_spec spec, const char *fmt)
1208dbc760bcSTejun Heo {
1209dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
1210dbc760bcSTejun Heo 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
1211dbc760bcSTejun Heo 	int cur, rbot, rtop;
1212dbc760bcSTejun Heo 	bool first = true;
1213dbc760bcSTejun Heo 
12143e5903ebSPetr Mladek 	if (check_pointer(&buf, end, bitmap, spec))
12153e5903ebSPetr Mladek 		return buf;
12163e5903ebSPetr Mladek 
1217dbc760bcSTejun Heo 	rbot = cur = find_first_bit(bitmap, nr_bits);
1218dbc760bcSTejun Heo 	while (cur < nr_bits) {
1219dbc760bcSTejun Heo 		rtop = cur;
1220dbc760bcSTejun Heo 		cur = find_next_bit(bitmap, nr_bits, cur + 1);
1221dbc760bcSTejun Heo 		if (cur < nr_bits && cur <= rtop + 1)
1222dbc760bcSTejun Heo 			continue;
1223dbc760bcSTejun Heo 
1224dbc760bcSTejun Heo 		if (!first) {
1225dbc760bcSTejun Heo 			if (buf < end)
1226dbc760bcSTejun Heo 				*buf = ',';
1227dbc760bcSTejun Heo 			buf++;
1228dbc760bcSTejun Heo 		}
1229dbc760bcSTejun Heo 		first = false;
1230dbc760bcSTejun Heo 
1231ce0b4910SAndy Shevchenko 		buf = number(buf, end, rbot, default_dec_spec);
1232dbc760bcSTejun Heo 		if (rbot < rtop) {
1233dbc760bcSTejun Heo 			if (buf < end)
1234dbc760bcSTejun Heo 				*buf = '-';
1235dbc760bcSTejun Heo 			buf++;
1236dbc760bcSTejun Heo 
1237ce0b4910SAndy Shevchenko 			buf = number(buf, end, rtop, default_dec_spec);
1238dbc760bcSTejun Heo 		}
1239dbc760bcSTejun Heo 
1240dbc760bcSTejun Heo 		rbot = cur;
1241dbc760bcSTejun Heo 	}
1242dbc760bcSTejun Heo 	return buf;
1243dbc760bcSTejun Heo }
1244dbc760bcSTejun Heo 
1245dbc760bcSTejun Heo static noinline_for_stack
1246cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
12478a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
1248dd45c9cfSHarvey Harrison {
12498a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
1250dd45c9cfSHarvey Harrison 	char *p = mac_addr;
1251dd45c9cfSHarvey Harrison 	int i;
1252bc7259a2SJoe Perches 	char separator;
125376597ff9SAndrei Emeltchenko 	bool reversed = false;
1254bc7259a2SJoe Perches 
12553e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
12563e5903ebSPetr Mladek 		return buf;
12573e5903ebSPetr Mladek 
125876597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
125976597ff9SAndrei Emeltchenko 	case 'F':
1260bc7259a2SJoe Perches 		separator = '-';
126176597ff9SAndrei Emeltchenko 		break;
126276597ff9SAndrei Emeltchenko 
126376597ff9SAndrei Emeltchenko 	case 'R':
126476597ff9SAndrei Emeltchenko 		reversed = true;
126576597ff9SAndrei Emeltchenko 		/* fall through */
126676597ff9SAndrei Emeltchenko 
126776597ff9SAndrei Emeltchenko 	default:
1268bc7259a2SJoe Perches 		separator = ':';
126976597ff9SAndrei Emeltchenko 		break;
1270bc7259a2SJoe Perches 	}
1271dd45c9cfSHarvey Harrison 
1272dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
127376597ff9SAndrei Emeltchenko 		if (reversed)
127476597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
127576597ff9SAndrei Emeltchenko 		else
127655036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
127776597ff9SAndrei Emeltchenko 
12788a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
1279bc7259a2SJoe Perches 			*p++ = separator;
1280dd45c9cfSHarvey Harrison 	}
1281dd45c9cfSHarvey Harrison 	*p = '\0';
1282dd45c9cfSHarvey Harrison 
1283d529ac41SPetr Mladek 	return string_nocheck(buf, end, mac_addr, spec);
1284dd45c9cfSHarvey Harrison }
1285dd45c9cfSHarvey Harrison 
1286cf3b429bSJoe Perches static noinline_for_stack
1287cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
1288689afa7dSHarvey Harrison {
1289689afa7dSHarvey Harrison 	int i;
12900159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
12910159f24eSJoe Perches 	int index;
12920159f24eSJoe Perches 	int step;
1293689afa7dSHarvey Harrison 
12940159f24eSJoe Perches 	switch (fmt[2]) {
12950159f24eSJoe Perches 	case 'h':
12960159f24eSJoe Perches #ifdef __BIG_ENDIAN
12970159f24eSJoe Perches 		index = 0;
12980159f24eSJoe Perches 		step = 1;
12990159f24eSJoe Perches #else
13000159f24eSJoe Perches 		index = 3;
13010159f24eSJoe Perches 		step = -1;
13020159f24eSJoe Perches #endif
13030159f24eSJoe Perches 		break;
13040159f24eSJoe Perches 	case 'l':
13050159f24eSJoe Perches 		index = 3;
13060159f24eSJoe Perches 		step = -1;
13070159f24eSJoe Perches 		break;
13080159f24eSJoe Perches 	case 'n':
13090159f24eSJoe Perches 	case 'b':
13100159f24eSJoe Perches 	default:
13110159f24eSJoe Perches 		index = 0;
13120159f24eSJoe Perches 		step = 1;
13130159f24eSJoe Perches 		break;
13140159f24eSJoe Perches 	}
13158a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
13167c43d9a3SRasmus Villemoes 		char temp[4] __aligned(2);	/* hold each IP quad in reverse order */
1317133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
13188a27f7c9SJoe Perches 		if (leading_zeros) {
13198a27f7c9SJoe Perches 			if (digits < 3)
13208a27f7c9SJoe Perches 				*p++ = '0';
13218a27f7c9SJoe Perches 			if (digits < 2)
13228a27f7c9SJoe Perches 				*p++ = '0';
13238a27f7c9SJoe Perches 		}
13248a27f7c9SJoe Perches 		/* reverse the digits in the quad */
13258a27f7c9SJoe Perches 		while (digits--)
13268a27f7c9SJoe Perches 			*p++ = temp[digits];
13278a27f7c9SJoe Perches 		if (i < 3)
13288a27f7c9SJoe Perches 			*p++ = '.';
13290159f24eSJoe Perches 		index += step;
13308a27f7c9SJoe Perches 	}
13318a27f7c9SJoe Perches 	*p = '\0';
13327b9186f5SAndré Goddard Rosa 
13338a27f7c9SJoe Perches 	return p;
13348a27f7c9SJoe Perches }
13358a27f7c9SJoe Perches 
1336cf3b429bSJoe Perches static noinline_for_stack
1337cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
13388a27f7c9SJoe Perches {
13397b9186f5SAndré Goddard Rosa 	int i, j, range;
13408a27f7c9SJoe Perches 	unsigned char zerolength[8];
13418a27f7c9SJoe Perches 	int longest = 1;
13428a27f7c9SJoe Perches 	int colonpos = -1;
13438a27f7c9SJoe Perches 	u16 word;
13447b9186f5SAndré Goddard Rosa 	u8 hi, lo;
13458a27f7c9SJoe Perches 	bool needcolon = false;
1346eb78cd26SJoe Perches 	bool useIPv4;
1347eb78cd26SJoe Perches 	struct in6_addr in6;
1348eb78cd26SJoe Perches 
1349eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
1350eb78cd26SJoe Perches 
1351eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
13528a27f7c9SJoe Perches 
13538a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
13548a27f7c9SJoe Perches 
13558a27f7c9SJoe Perches 	if (useIPv4)
13568a27f7c9SJoe Perches 		range = 6;
13578a27f7c9SJoe Perches 	else
13588a27f7c9SJoe Perches 		range = 8;
13598a27f7c9SJoe Perches 
13608a27f7c9SJoe Perches 	/* find position of longest 0 run */
13618a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
13628a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
1363eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
13648a27f7c9SJoe Perches 				break;
13658a27f7c9SJoe Perches 			zerolength[i]++;
13668a27f7c9SJoe Perches 		}
13678a27f7c9SJoe Perches 	}
13688a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
13698a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
13708a27f7c9SJoe Perches 			longest = zerolength[i];
13718a27f7c9SJoe Perches 			colonpos = i;
13728a27f7c9SJoe Perches 		}
13738a27f7c9SJoe Perches 	}
137429cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
137529cf519eSJoe Perches 		colonpos = -1;
13768a27f7c9SJoe Perches 
13778a27f7c9SJoe Perches 	/* emit address */
13788a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
13798a27f7c9SJoe Perches 		if (i == colonpos) {
13808a27f7c9SJoe Perches 			if (needcolon || i == 0)
13818a27f7c9SJoe Perches 				*p++ = ':';
13828a27f7c9SJoe Perches 			*p++ = ':';
13838a27f7c9SJoe Perches 			needcolon = false;
13848a27f7c9SJoe Perches 			i += longest - 1;
13858a27f7c9SJoe Perches 			continue;
13868a27f7c9SJoe Perches 		}
13878a27f7c9SJoe Perches 		if (needcolon) {
13888a27f7c9SJoe Perches 			*p++ = ':';
13898a27f7c9SJoe Perches 			needcolon = false;
13908a27f7c9SJoe Perches 		}
13918a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
1392eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
13938a27f7c9SJoe Perches 		hi = word >> 8;
13948a27f7c9SJoe Perches 		lo = word & 0xff;
13958a27f7c9SJoe Perches 		if (hi) {
13968a27f7c9SJoe Perches 			if (hi > 0x0f)
139755036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
13988a27f7c9SJoe Perches 			else
13998a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
140055036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
14018a27f7c9SJoe Perches 		}
1402b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
140355036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
14048a27f7c9SJoe Perches 		else
14058a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
14068a27f7c9SJoe Perches 		needcolon = true;
14078a27f7c9SJoe Perches 	}
14088a27f7c9SJoe Perches 
14098a27f7c9SJoe Perches 	if (useIPv4) {
14108a27f7c9SJoe Perches 		if (needcolon)
14118a27f7c9SJoe Perches 			*p++ = ':';
14120159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
14138a27f7c9SJoe Perches 	}
14148a27f7c9SJoe Perches 	*p = '\0';
14157b9186f5SAndré Goddard Rosa 
14168a27f7c9SJoe Perches 	return p;
14178a27f7c9SJoe Perches }
14188a27f7c9SJoe Perches 
1419cf3b429bSJoe Perches static noinline_for_stack
1420cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
14218a27f7c9SJoe Perches {
14228a27f7c9SJoe Perches 	int i;
14237b9186f5SAndré Goddard Rosa 
1424689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
142555036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
142655036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
14278a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
1428689afa7dSHarvey Harrison 			*p++ = ':';
1429689afa7dSHarvey Harrison 	}
1430689afa7dSHarvey Harrison 	*p = '\0';
14317b9186f5SAndré Goddard Rosa 
14328a27f7c9SJoe Perches 	return p;
14338a27f7c9SJoe Perches }
14348a27f7c9SJoe Perches 
1435cf3b429bSJoe Perches static noinline_for_stack
1436cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
14378a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
14388a27f7c9SJoe Perches {
14398a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
14408a27f7c9SJoe Perches 
14418a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
1442eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
14438a27f7c9SJoe Perches 	else
1444eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
1445689afa7dSHarvey Harrison 
1446d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip6_addr, spec);
1447689afa7dSHarvey Harrison }
1448689afa7dSHarvey Harrison 
1449cf3b429bSJoe Perches static noinline_for_stack
1450cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
14518a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
14524aa99606SHarvey Harrison {
14538a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
14544aa99606SHarvey Harrison 
14550159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
14564aa99606SHarvey Harrison 
1457d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip4_addr, spec);
14584aa99606SHarvey Harrison }
14594aa99606SHarvey Harrison 
1460cf3b429bSJoe Perches static noinline_for_stack
146110679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
146210679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
146310679643SDaniel Borkmann {
146410679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
146510679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
146610679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
146710679643SDaniel Borkmann 		      sizeof("%1234567890")];
146810679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
146910679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
147010679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
147110679643SDaniel Borkmann 	u8 off = 0;
147210679643SDaniel Borkmann 
147310679643SDaniel Borkmann 	fmt++;
147410679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
147510679643SDaniel Borkmann 		switch (*fmt) {
147610679643SDaniel Borkmann 		case 'p':
147710679643SDaniel Borkmann 			have_p = true;
147810679643SDaniel Borkmann 			break;
147910679643SDaniel Borkmann 		case 'f':
148010679643SDaniel Borkmann 			have_f = true;
148110679643SDaniel Borkmann 			break;
148210679643SDaniel Borkmann 		case 's':
148310679643SDaniel Borkmann 			have_s = true;
148410679643SDaniel Borkmann 			break;
148510679643SDaniel Borkmann 		case 'c':
148610679643SDaniel Borkmann 			have_c = true;
148710679643SDaniel Borkmann 			break;
148810679643SDaniel Borkmann 		}
148910679643SDaniel Borkmann 	}
149010679643SDaniel Borkmann 
149110679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
149210679643SDaniel Borkmann 		*p = '[';
149310679643SDaniel Borkmann 		off = 1;
149410679643SDaniel Borkmann 	}
149510679643SDaniel Borkmann 
149610679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
149710679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
149810679643SDaniel Borkmann 	else
149910679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
150010679643SDaniel Borkmann 
150110679643SDaniel Borkmann 	if (have_p || have_s || have_f)
150210679643SDaniel Borkmann 		*p++ = ']';
150310679643SDaniel Borkmann 
150410679643SDaniel Borkmann 	if (have_p) {
150510679643SDaniel Borkmann 		*p++ = ':';
150610679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
150710679643SDaniel Borkmann 	}
150810679643SDaniel Borkmann 	if (have_f) {
150910679643SDaniel Borkmann 		*p++ = '/';
151010679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
151110679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
151210679643SDaniel Borkmann 	}
151310679643SDaniel Borkmann 	if (have_s) {
151410679643SDaniel Borkmann 		*p++ = '%';
151510679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
151610679643SDaniel Borkmann 	}
151710679643SDaniel Borkmann 	*p = '\0';
151810679643SDaniel Borkmann 
1519d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip6_addr, spec);
152010679643SDaniel Borkmann }
152110679643SDaniel Borkmann 
152210679643SDaniel Borkmann static noinline_for_stack
152310679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
152410679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
152510679643SDaniel Borkmann {
152610679643SDaniel Borkmann 	bool have_p = false;
152710679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
152810679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
152910679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
153010679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
153110679643SDaniel Borkmann 
153210679643SDaniel Borkmann 	fmt++;
153310679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
153410679643SDaniel Borkmann 		switch (*fmt) {
153510679643SDaniel Borkmann 		case 'p':
153610679643SDaniel Borkmann 			have_p = true;
153710679643SDaniel Borkmann 			break;
153810679643SDaniel Borkmann 		case 'h':
153910679643SDaniel Borkmann 		case 'l':
154010679643SDaniel Borkmann 		case 'n':
154110679643SDaniel Borkmann 		case 'b':
154210679643SDaniel Borkmann 			fmt4[2] = *fmt;
154310679643SDaniel Borkmann 			break;
154410679643SDaniel Borkmann 		}
154510679643SDaniel Borkmann 	}
154610679643SDaniel Borkmann 
154710679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
154810679643SDaniel Borkmann 	if (have_p) {
154910679643SDaniel Borkmann 		*p++ = ':';
155010679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
155110679643SDaniel Borkmann 	}
155210679643SDaniel Borkmann 	*p = '\0';
155310679643SDaniel Borkmann 
1554d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip4_addr, spec);
155510679643SDaniel Borkmann }
155610679643SDaniel Borkmann 
155710679643SDaniel Borkmann static noinline_for_stack
1558f00cc102SPetr Mladek char *ip_addr_string(char *buf, char *end, const void *ptr,
1559f00cc102SPetr Mladek 		     struct printf_spec spec, const char *fmt)
1560f00cc102SPetr Mladek {
15610b74d4d7SPetr Mladek 	char *err_fmt_msg;
15620b74d4d7SPetr Mladek 
15633e5903ebSPetr Mladek 	if (check_pointer(&buf, end, ptr, spec))
15643e5903ebSPetr Mladek 		return buf;
15653e5903ebSPetr Mladek 
1566f00cc102SPetr Mladek 	switch (fmt[1]) {
1567f00cc102SPetr Mladek 	case '6':
1568f00cc102SPetr Mladek 		return ip6_addr_string(buf, end, ptr, spec, fmt);
1569f00cc102SPetr Mladek 	case '4':
1570f00cc102SPetr Mladek 		return ip4_addr_string(buf, end, ptr, spec, fmt);
1571f00cc102SPetr Mladek 	case 'S': {
1572f00cc102SPetr Mladek 		const union {
1573f00cc102SPetr Mladek 			struct sockaddr		raw;
1574f00cc102SPetr Mladek 			struct sockaddr_in	v4;
1575f00cc102SPetr Mladek 			struct sockaddr_in6	v6;
1576f00cc102SPetr Mladek 		} *sa = ptr;
1577f00cc102SPetr Mladek 
1578f00cc102SPetr Mladek 		switch (sa->raw.sa_family) {
1579f00cc102SPetr Mladek 		case AF_INET:
1580f00cc102SPetr Mladek 			return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1581f00cc102SPetr Mladek 		case AF_INET6:
1582f00cc102SPetr Mladek 			return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1583f00cc102SPetr Mladek 		default:
1584c8c3b584SPetr Mladek 			return error_string(buf, end, "(einval)", spec);
1585f00cc102SPetr Mladek 		}}
1586f00cc102SPetr Mladek 	}
1587f00cc102SPetr Mladek 
15880b74d4d7SPetr Mladek 	err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
1589c8c3b584SPetr Mladek 	return error_string(buf, end, err_fmt_msg, spec);
1590f00cc102SPetr Mladek }
1591f00cc102SPetr Mladek 
1592f00cc102SPetr Mladek static noinline_for_stack
159371dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
159471dca95dSAndy Shevchenko 		     const char *fmt)
159571dca95dSAndy Shevchenko {
159671dca95dSAndy Shevchenko 	bool found = true;
159771dca95dSAndy Shevchenko 	int count = 1;
159871dca95dSAndy Shevchenko 	unsigned int flags = 0;
159971dca95dSAndy Shevchenko 	int len;
160071dca95dSAndy Shevchenko 
160171dca95dSAndy Shevchenko 	if (spec.field_width == 0)
160271dca95dSAndy Shevchenko 		return buf;				/* nothing to print */
160371dca95dSAndy Shevchenko 
16043e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
16053e5903ebSPetr Mladek 		return buf;
160671dca95dSAndy Shevchenko 
160771dca95dSAndy Shevchenko 	do {
160871dca95dSAndy Shevchenko 		switch (fmt[count++]) {
160971dca95dSAndy Shevchenko 		case 'a':
161071dca95dSAndy Shevchenko 			flags |= ESCAPE_ANY;
161171dca95dSAndy Shevchenko 			break;
161271dca95dSAndy Shevchenko 		case 'c':
161371dca95dSAndy Shevchenko 			flags |= ESCAPE_SPECIAL;
161471dca95dSAndy Shevchenko 			break;
161571dca95dSAndy Shevchenko 		case 'h':
161671dca95dSAndy Shevchenko 			flags |= ESCAPE_HEX;
161771dca95dSAndy Shevchenko 			break;
161871dca95dSAndy Shevchenko 		case 'n':
161971dca95dSAndy Shevchenko 			flags |= ESCAPE_NULL;
162071dca95dSAndy Shevchenko 			break;
162171dca95dSAndy Shevchenko 		case 'o':
162271dca95dSAndy Shevchenko 			flags |= ESCAPE_OCTAL;
162371dca95dSAndy Shevchenko 			break;
162471dca95dSAndy Shevchenko 		case 'p':
162571dca95dSAndy Shevchenko 			flags |= ESCAPE_NP;
162671dca95dSAndy Shevchenko 			break;
162771dca95dSAndy Shevchenko 		case 's':
162871dca95dSAndy Shevchenko 			flags |= ESCAPE_SPACE;
162971dca95dSAndy Shevchenko 			break;
163071dca95dSAndy Shevchenko 		default:
163171dca95dSAndy Shevchenko 			found = false;
163271dca95dSAndy Shevchenko 			break;
163371dca95dSAndy Shevchenko 		}
163471dca95dSAndy Shevchenko 	} while (found);
163571dca95dSAndy Shevchenko 
163671dca95dSAndy Shevchenko 	if (!flags)
163771dca95dSAndy Shevchenko 		flags = ESCAPE_ANY_NP;
163871dca95dSAndy Shevchenko 
163971dca95dSAndy Shevchenko 	len = spec.field_width < 0 ? 1 : spec.field_width;
164071dca95dSAndy Shevchenko 
164141416f23SRasmus Villemoes 	/*
164241416f23SRasmus Villemoes 	 * string_escape_mem() writes as many characters as it can to
164341416f23SRasmus Villemoes 	 * the given buffer, and returns the total size of the output
164441416f23SRasmus Villemoes 	 * had the buffer been big enough.
164541416f23SRasmus Villemoes 	 */
164641416f23SRasmus Villemoes 	buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
164771dca95dSAndy Shevchenko 
164871dca95dSAndy Shevchenko 	return buf;
164971dca95dSAndy Shevchenko }
165071dca95dSAndy Shevchenko 
16513e5903ebSPetr Mladek static char *va_format(char *buf, char *end, struct va_format *va_fmt,
16523e5903ebSPetr Mladek 		       struct printf_spec spec, const char *fmt)
165345c3e93dSPetr Mladek {
165445c3e93dSPetr Mladek 	va_list va;
165545c3e93dSPetr Mladek 
16563e5903ebSPetr Mladek 	if (check_pointer(&buf, end, va_fmt, spec))
16573e5903ebSPetr Mladek 		return buf;
16583e5903ebSPetr Mladek 
165945c3e93dSPetr Mladek 	va_copy(va, *va_fmt->va);
166045c3e93dSPetr Mladek 	buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
166145c3e93dSPetr Mladek 	va_end(va);
166245c3e93dSPetr Mladek 
166345c3e93dSPetr Mladek 	return buf;
166445c3e93dSPetr Mladek }
166545c3e93dSPetr Mladek 
166671dca95dSAndy Shevchenko static noinline_for_stack
1667cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
16689ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
16699ac6e44eSJoe Perches {
16702b1b0d66SAndy Shevchenko 	char uuid[UUID_STRING_LEN + 1];
16719ac6e44eSJoe Perches 	char *p = uuid;
16729ac6e44eSJoe Perches 	int i;
1673f9727a17SChristoph Hellwig 	const u8 *index = uuid_index;
16749ac6e44eSJoe Perches 	bool uc = false;
16759ac6e44eSJoe Perches 
16763e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
16773e5903ebSPetr Mladek 		return buf;
16783e5903ebSPetr Mladek 
16799ac6e44eSJoe Perches 	switch (*(++fmt)) {
16809ac6e44eSJoe Perches 	case 'L':
16819ac6e44eSJoe Perches 		uc = true;		/* fall-through */
16829ac6e44eSJoe Perches 	case 'l':
1683f9727a17SChristoph Hellwig 		index = guid_index;
16849ac6e44eSJoe Perches 		break;
16859ac6e44eSJoe Perches 	case 'B':
16869ac6e44eSJoe Perches 		uc = true;
16879ac6e44eSJoe Perches 		break;
16889ac6e44eSJoe Perches 	}
16899ac6e44eSJoe Perches 
16909ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
1691aa4ea1c3SAndy Shevchenko 		if (uc)
1692aa4ea1c3SAndy Shevchenko 			p = hex_byte_pack_upper(p, addr[index[i]]);
1693aa4ea1c3SAndy Shevchenko 		else
169455036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[index[i]]);
16959ac6e44eSJoe Perches 		switch (i) {
16969ac6e44eSJoe Perches 		case 3:
16979ac6e44eSJoe Perches 		case 5:
16989ac6e44eSJoe Perches 		case 7:
16999ac6e44eSJoe Perches 		case 9:
17009ac6e44eSJoe Perches 			*p++ = '-';
17019ac6e44eSJoe Perches 			break;
17029ac6e44eSJoe Perches 		}
17039ac6e44eSJoe Perches 	}
17049ac6e44eSJoe Perches 
17059ac6e44eSJoe Perches 	*p = 0;
17069ac6e44eSJoe Perches 
1707d529ac41SPetr Mladek 	return string_nocheck(buf, end, uuid, spec);
17089ac6e44eSJoe Perches }
17099ac6e44eSJoe Perches 
17105b17aecfSAndy Shevchenko static noinline_for_stack
1711431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr,
1712431bca24SGeert Uytterhoeven 		  struct printf_spec spec,  const char *fmt)
1713c8f44affSMichał Mirosław {
17145b17aecfSAndy Shevchenko 	unsigned long long num;
17155b17aecfSAndy Shevchenko 	int size;
17165b17aecfSAndy Shevchenko 
17173e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
17183e5903ebSPetr Mladek 		return buf;
17193e5903ebSPetr Mladek 
17205b17aecfSAndy Shevchenko 	switch (fmt[1]) {
17215b17aecfSAndy Shevchenko 	case 'F':
17225b17aecfSAndy Shevchenko 		num = *(const netdev_features_t *)addr;
17235b17aecfSAndy Shevchenko 		size = sizeof(netdev_features_t);
17245b17aecfSAndy Shevchenko 		break;
17255b17aecfSAndy Shevchenko 	default:
1726c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pN?)", spec);
17275b17aecfSAndy Shevchenko 	}
1728c8f44affSMichał Mirosław 
17293cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, num, size);
1730c8f44affSMichał Mirosław }
1731c8f44affSMichał Mirosław 
1732aaf07621SJoe Perches static noinline_for_stack
17333e5903ebSPetr Mladek char *address_val(char *buf, char *end, const void *addr,
17343e5903ebSPetr Mladek 		  struct printf_spec spec, const char *fmt)
1735aaf07621SJoe Perches {
1736aaf07621SJoe Perches 	unsigned long long num;
17373cab1e71SAndy Shevchenko 	int size;
1738aaf07621SJoe Perches 
17393e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
17403e5903ebSPetr Mladek 		return buf;
17413e5903ebSPetr Mladek 
1742aaf07621SJoe Perches 	switch (fmt[1]) {
1743aaf07621SJoe Perches 	case 'd':
1744aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
17453cab1e71SAndy Shevchenko 		size = sizeof(dma_addr_t);
1746aaf07621SJoe Perches 		break;
1747aaf07621SJoe Perches 	case 'p':
1748aaf07621SJoe Perches 	default:
1749aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
17503cab1e71SAndy Shevchenko 		size = sizeof(phys_addr_t);
1751aaf07621SJoe Perches 		break;
1752aaf07621SJoe Perches 	}
1753aaf07621SJoe Perches 
17543cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, num, size);
1755aaf07621SJoe Perches }
1756aaf07621SJoe Perches 
1757900cca29SGeert Uytterhoeven static noinline_for_stack
17584d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
17594d42c447SAndy Shevchenko {
17604d42c447SAndy Shevchenko 	int year = tm->tm_year + (r ? 0 : 1900);
17614d42c447SAndy Shevchenko 	int mon = tm->tm_mon + (r ? 0 : 1);
17624d42c447SAndy Shevchenko 
17634d42c447SAndy Shevchenko 	buf = number(buf, end, year, default_dec04_spec);
17644d42c447SAndy Shevchenko 	if (buf < end)
17654d42c447SAndy Shevchenko 		*buf = '-';
17664d42c447SAndy Shevchenko 	buf++;
17674d42c447SAndy Shevchenko 
17684d42c447SAndy Shevchenko 	buf = number(buf, end, mon, default_dec02_spec);
17694d42c447SAndy Shevchenko 	if (buf < end)
17704d42c447SAndy Shevchenko 		*buf = '-';
17714d42c447SAndy Shevchenko 	buf++;
17724d42c447SAndy Shevchenko 
17734d42c447SAndy Shevchenko 	return number(buf, end, tm->tm_mday, default_dec02_spec);
17744d42c447SAndy Shevchenko }
17754d42c447SAndy Shevchenko 
17764d42c447SAndy Shevchenko static noinline_for_stack
17774d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
17784d42c447SAndy Shevchenko {
17794d42c447SAndy Shevchenko 	buf = number(buf, end, tm->tm_hour, default_dec02_spec);
17804d42c447SAndy Shevchenko 	if (buf < end)
17814d42c447SAndy Shevchenko 		*buf = ':';
17824d42c447SAndy Shevchenko 	buf++;
17834d42c447SAndy Shevchenko 
17844d42c447SAndy Shevchenko 	buf = number(buf, end, tm->tm_min, default_dec02_spec);
17854d42c447SAndy Shevchenko 	if (buf < end)
17864d42c447SAndy Shevchenko 		*buf = ':';
17874d42c447SAndy Shevchenko 	buf++;
17884d42c447SAndy Shevchenko 
17894d42c447SAndy Shevchenko 	return number(buf, end, tm->tm_sec, default_dec02_spec);
17904d42c447SAndy Shevchenko }
17914d42c447SAndy Shevchenko 
17924d42c447SAndy Shevchenko static noinline_for_stack
17933e5903ebSPetr Mladek char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
17943e5903ebSPetr Mladek 	      struct printf_spec spec, const char *fmt)
17954d42c447SAndy Shevchenko {
17964d42c447SAndy Shevchenko 	bool have_t = true, have_d = true;
17974d42c447SAndy Shevchenko 	bool raw = false;
17984d42c447SAndy Shevchenko 	int count = 2;
17994d42c447SAndy Shevchenko 
18003e5903ebSPetr Mladek 	if (check_pointer(&buf, end, tm, spec))
18013e5903ebSPetr Mladek 		return buf;
18023e5903ebSPetr Mladek 
18034d42c447SAndy Shevchenko 	switch (fmt[count]) {
18044d42c447SAndy Shevchenko 	case 'd':
18054d42c447SAndy Shevchenko 		have_t = false;
18064d42c447SAndy Shevchenko 		count++;
18074d42c447SAndy Shevchenko 		break;
18084d42c447SAndy Shevchenko 	case 't':
18094d42c447SAndy Shevchenko 		have_d = false;
18104d42c447SAndy Shevchenko 		count++;
18114d42c447SAndy Shevchenko 		break;
18124d42c447SAndy Shevchenko 	}
18134d42c447SAndy Shevchenko 
18144d42c447SAndy Shevchenko 	raw = fmt[count] == 'r';
18154d42c447SAndy Shevchenko 
18164d42c447SAndy Shevchenko 	if (have_d)
18174d42c447SAndy Shevchenko 		buf = date_str(buf, end, tm, raw);
18184d42c447SAndy Shevchenko 	if (have_d && have_t) {
18194d42c447SAndy Shevchenko 		/* Respect ISO 8601 */
18204d42c447SAndy Shevchenko 		if (buf < end)
18214d42c447SAndy Shevchenko 			*buf = 'T';
18224d42c447SAndy Shevchenko 		buf++;
18234d42c447SAndy Shevchenko 	}
18244d42c447SAndy Shevchenko 	if (have_t)
18254d42c447SAndy Shevchenko 		buf = time_str(buf, end, tm, raw);
18264d42c447SAndy Shevchenko 
18274d42c447SAndy Shevchenko 	return buf;
18284d42c447SAndy Shevchenko }
18294d42c447SAndy Shevchenko 
18304d42c447SAndy Shevchenko static noinline_for_stack
18317daac5b2SAndy Shevchenko char *time64_str(char *buf, char *end, const time64_t time,
18327daac5b2SAndy Shevchenko 		 struct printf_spec spec, const char *fmt)
18337daac5b2SAndy Shevchenko {
18347daac5b2SAndy Shevchenko 	struct rtc_time rtc_time;
18357daac5b2SAndy Shevchenko 	struct tm tm;
18367daac5b2SAndy Shevchenko 
18377daac5b2SAndy Shevchenko 	time64_to_tm(time, 0, &tm);
18387daac5b2SAndy Shevchenko 
18397daac5b2SAndy Shevchenko 	rtc_time.tm_sec = tm.tm_sec;
18407daac5b2SAndy Shevchenko 	rtc_time.tm_min = tm.tm_min;
18417daac5b2SAndy Shevchenko 	rtc_time.tm_hour = tm.tm_hour;
18427daac5b2SAndy Shevchenko 	rtc_time.tm_mday = tm.tm_mday;
18437daac5b2SAndy Shevchenko 	rtc_time.tm_mon = tm.tm_mon;
18447daac5b2SAndy Shevchenko 	rtc_time.tm_year = tm.tm_year;
18457daac5b2SAndy Shevchenko 	rtc_time.tm_wday = tm.tm_wday;
18467daac5b2SAndy Shevchenko 	rtc_time.tm_yday = tm.tm_yday;
18477daac5b2SAndy Shevchenko 
18487daac5b2SAndy Shevchenko 	rtc_time.tm_isdst = 0;
18497daac5b2SAndy Shevchenko 
18507daac5b2SAndy Shevchenko 	return rtc_str(buf, end, &rtc_time, spec, fmt);
18517daac5b2SAndy Shevchenko }
18527daac5b2SAndy Shevchenko 
18537daac5b2SAndy Shevchenko static noinline_for_stack
18544d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
18554d42c447SAndy Shevchenko 		    const char *fmt)
18564d42c447SAndy Shevchenko {
18574d42c447SAndy Shevchenko 	switch (fmt[1]) {
18584d42c447SAndy Shevchenko 	case 'R':
18593e5903ebSPetr Mladek 		return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
18607daac5b2SAndy Shevchenko 	case 'T':
18617daac5b2SAndy Shevchenko 		return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt);
18624d42c447SAndy Shevchenko 	default:
18637daac5b2SAndy Shevchenko 		return error_string(buf, end, "(%pt?)", spec);
18644d42c447SAndy Shevchenko 	}
18654d42c447SAndy Shevchenko }
18664d42c447SAndy Shevchenko 
18674d42c447SAndy Shevchenko static noinline_for_stack
1868900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1869900cca29SGeert Uytterhoeven 	    const char *fmt)
1870900cca29SGeert Uytterhoeven {
18710b74d4d7SPetr Mladek 	if (!IS_ENABLED(CONFIG_HAVE_CLK))
1872c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pC?)", spec);
18730b74d4d7SPetr Mladek 
18743e5903ebSPetr Mladek 	if (check_pointer(&buf, end, clk, spec))
18753e5903ebSPetr Mladek 		return buf;
1876900cca29SGeert Uytterhoeven 
1877900cca29SGeert Uytterhoeven 	switch (fmt[1]) {
1878900cca29SGeert Uytterhoeven 	case 'n':
1879900cca29SGeert Uytterhoeven 	default:
1880900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK
1881900cca29SGeert Uytterhoeven 		return string(buf, end, __clk_get_name(clk), spec);
1882900cca29SGeert Uytterhoeven #else
18834ca96aa9SGeert Uytterhoeven 		return ptr_to_id(buf, end, clk, spec);
1884900cca29SGeert Uytterhoeven #endif
1885900cca29SGeert Uytterhoeven 	}
1886900cca29SGeert Uytterhoeven }
1887900cca29SGeert Uytterhoeven 
1888edf14cdbSVlastimil Babka static
1889edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags,
1890edf14cdbSVlastimil Babka 					const struct trace_print_flags *names)
1891edf14cdbSVlastimil Babka {
1892edf14cdbSVlastimil Babka 	unsigned long mask;
1893edf14cdbSVlastimil Babka 
1894edf14cdbSVlastimil Babka 	for ( ; flags && names->name; names++) {
1895edf14cdbSVlastimil Babka 		mask = names->mask;
1896edf14cdbSVlastimil Babka 		if ((flags & mask) != mask)
1897edf14cdbSVlastimil Babka 			continue;
1898edf14cdbSVlastimil Babka 
1899abd4fe62SAndy Shevchenko 		buf = string(buf, end, names->name, default_str_spec);
1900edf14cdbSVlastimil Babka 
1901edf14cdbSVlastimil Babka 		flags &= ~mask;
1902edf14cdbSVlastimil Babka 		if (flags) {
1903edf14cdbSVlastimil Babka 			if (buf < end)
1904edf14cdbSVlastimil Babka 				*buf = '|';
1905edf14cdbSVlastimil Babka 			buf++;
1906edf14cdbSVlastimil Babka 		}
1907edf14cdbSVlastimil Babka 	}
1908edf14cdbSVlastimil Babka 
1909edf14cdbSVlastimil Babka 	if (flags)
191054433973SAndy Shevchenko 		buf = number(buf, end, flags, default_flag_spec);
1911edf14cdbSVlastimil Babka 
1912edf14cdbSVlastimil Babka 	return buf;
1913edf14cdbSVlastimil Babka }
1914edf14cdbSVlastimil Babka 
1915edf14cdbSVlastimil Babka static noinline_for_stack
19160b74d4d7SPetr Mladek char *flags_string(char *buf, char *end, void *flags_ptr,
19170b74d4d7SPetr Mladek 		   struct printf_spec spec, const char *fmt)
1918edf14cdbSVlastimil Babka {
1919edf14cdbSVlastimil Babka 	unsigned long flags;
1920edf14cdbSVlastimil Babka 	const struct trace_print_flags *names;
1921edf14cdbSVlastimil Babka 
19223e5903ebSPetr Mladek 	if (check_pointer(&buf, end, flags_ptr, spec))
19233e5903ebSPetr Mladek 		return buf;
19243e5903ebSPetr Mladek 
1925edf14cdbSVlastimil Babka 	switch (fmt[1]) {
1926edf14cdbSVlastimil Babka 	case 'p':
1927edf14cdbSVlastimil Babka 		flags = *(unsigned long *)flags_ptr;
1928edf14cdbSVlastimil Babka 		/* Remove zone id */
1929edf14cdbSVlastimil Babka 		flags &= (1UL << NR_PAGEFLAGS) - 1;
1930edf14cdbSVlastimil Babka 		names = pageflag_names;
1931edf14cdbSVlastimil Babka 		break;
1932edf14cdbSVlastimil Babka 	case 'v':
1933edf14cdbSVlastimil Babka 		flags = *(unsigned long *)flags_ptr;
1934edf14cdbSVlastimil Babka 		names = vmaflag_names;
1935edf14cdbSVlastimil Babka 		break;
1936edf14cdbSVlastimil Babka 	case 'g':
1937edf14cdbSVlastimil Babka 		flags = *(gfp_t *)flags_ptr;
1938edf14cdbSVlastimil Babka 		names = gfpflag_names;
1939edf14cdbSVlastimil Babka 		break;
1940edf14cdbSVlastimil Babka 	default:
1941c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pG?)", spec);
1942edf14cdbSVlastimil Babka 	}
1943edf14cdbSVlastimil Babka 
1944edf14cdbSVlastimil Babka 	return format_flags(buf, end, flags, names);
1945edf14cdbSVlastimil Babka }
1946edf14cdbSVlastimil Babka 
1947ce4fecf1SPantelis Antoniou static noinline_for_stack
1948a92eb762SSakari Ailus char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
1949a92eb762SSakari Ailus 			      char *end)
1950ce4fecf1SPantelis Antoniou {
1951ce4fecf1SPantelis Antoniou 	int depth;
1952ce4fecf1SPantelis Antoniou 
1953a92eb762SSakari Ailus 	/* Loop starting from the root node to the current node. */
1954a92eb762SSakari Ailus 	for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
1955a92eb762SSakari Ailus 		struct fwnode_handle *__fwnode =
1956a92eb762SSakari Ailus 			fwnode_get_nth_parent(fwnode, depth);
1957ce4fecf1SPantelis Antoniou 
1958a92eb762SSakari Ailus 		buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
1959abd4fe62SAndy Shevchenko 			     default_str_spec);
1960a92eb762SSakari Ailus 		buf = string(buf, end, fwnode_get_name(__fwnode),
1961a92eb762SSakari Ailus 			     default_str_spec);
1962a92eb762SSakari Ailus 
1963a92eb762SSakari Ailus 		fwnode_handle_put(__fwnode);
1964ce4fecf1SPantelis Antoniou 	}
1965a92eb762SSakari Ailus 
1966ce4fecf1SPantelis Antoniou 	return buf;
1967ce4fecf1SPantelis Antoniou }
1968ce4fecf1SPantelis Antoniou 
1969ce4fecf1SPantelis Antoniou static noinline_for_stack
1970ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn,
1971ce4fecf1SPantelis Antoniou 			 struct printf_spec spec, const char *fmt)
1972ce4fecf1SPantelis Antoniou {
1973ce4fecf1SPantelis Antoniou 	char tbuf[sizeof("xxxx") + 1];
1974ce4fecf1SPantelis Antoniou 	const char *p;
1975ce4fecf1SPantelis Antoniou 	int ret;
1976ce4fecf1SPantelis Antoniou 	char *buf_start = buf;
1977ce4fecf1SPantelis Antoniou 	struct property *prop;
1978ce4fecf1SPantelis Antoniou 	bool has_mult, pass;
1979ce4fecf1SPantelis Antoniou 	static const struct printf_spec num_spec = {
1980ce4fecf1SPantelis Antoniou 		.flags = SMALL,
1981ce4fecf1SPantelis Antoniou 		.field_width = -1,
1982ce4fecf1SPantelis Antoniou 		.precision = -1,
1983ce4fecf1SPantelis Antoniou 		.base = 10,
1984ce4fecf1SPantelis Antoniou 	};
1985ce4fecf1SPantelis Antoniou 
1986ce4fecf1SPantelis Antoniou 	struct printf_spec str_spec = spec;
1987ce4fecf1SPantelis Antoniou 	str_spec.field_width = -1;
1988ce4fecf1SPantelis Antoniou 
198983abc5a7SSakari Ailus 	if (fmt[0] != 'F')
199083abc5a7SSakari Ailus 		return error_string(buf, end, "(%pO?)", spec);
199183abc5a7SSakari Ailus 
1992ce4fecf1SPantelis Antoniou 	if (!IS_ENABLED(CONFIG_OF))
1993c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pOF?)", spec);
1994ce4fecf1SPantelis Antoniou 
19953e5903ebSPetr Mladek 	if (check_pointer(&buf, end, dn, spec))
19963e5903ebSPetr Mladek 		return buf;
1997ce4fecf1SPantelis Antoniou 
1998ce4fecf1SPantelis Antoniou 	/* simple case without anything any more format specifiers */
1999ce4fecf1SPantelis Antoniou 	fmt++;
2000ce4fecf1SPantelis Antoniou 	if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
2001ce4fecf1SPantelis Antoniou 		fmt = "f";
2002ce4fecf1SPantelis Antoniou 
2003ce4fecf1SPantelis Antoniou 	for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
20046d0a70a2SRob Herring 		int precision;
2005ce4fecf1SPantelis Antoniou 		if (pass) {
2006ce4fecf1SPantelis Antoniou 			if (buf < end)
2007ce4fecf1SPantelis Antoniou 				*buf = ':';
2008ce4fecf1SPantelis Antoniou 			buf++;
2009ce4fecf1SPantelis Antoniou 		}
2010ce4fecf1SPantelis Antoniou 
2011ce4fecf1SPantelis Antoniou 		switch (*fmt) {
2012ce4fecf1SPantelis Antoniou 		case 'f':	/* full_name */
2013a92eb762SSakari Ailus 			buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
2014a92eb762SSakari Ailus 						      end);
2015ce4fecf1SPantelis Antoniou 			break;
2016ce4fecf1SPantelis Antoniou 		case 'n':	/* name */
2017a92eb762SSakari Ailus 			p = fwnode_get_name(of_fwnode_handle(dn));
20186d0a70a2SRob Herring 			precision = str_spec.precision;
20196d0a70a2SRob Herring 			str_spec.precision = strchrnul(p, '@') - p;
20206d0a70a2SRob Herring 			buf = string(buf, end, p, str_spec);
20216d0a70a2SRob Herring 			str_spec.precision = precision;
2022ce4fecf1SPantelis Antoniou 			break;
2023ce4fecf1SPantelis Antoniou 		case 'p':	/* phandle */
2024ce4fecf1SPantelis Antoniou 			buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
2025ce4fecf1SPantelis Antoniou 			break;
2026ce4fecf1SPantelis Antoniou 		case 'P':	/* path-spec */
2027a92eb762SSakari Ailus 			p = fwnode_get_name(of_fwnode_handle(dn));
2028ce4fecf1SPantelis Antoniou 			if (!p[1])
2029ce4fecf1SPantelis Antoniou 				p = "/";
2030ce4fecf1SPantelis Antoniou 			buf = string(buf, end, p, str_spec);
2031ce4fecf1SPantelis Antoniou 			break;
2032ce4fecf1SPantelis Antoniou 		case 'F':	/* flags */
2033ce4fecf1SPantelis Antoniou 			tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
2034ce4fecf1SPantelis Antoniou 			tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
2035ce4fecf1SPantelis Antoniou 			tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
2036ce4fecf1SPantelis Antoniou 			tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
2037ce4fecf1SPantelis Antoniou 			tbuf[4] = 0;
2038d529ac41SPetr Mladek 			buf = string_nocheck(buf, end, tbuf, str_spec);
2039ce4fecf1SPantelis Antoniou 			break;
2040ce4fecf1SPantelis Antoniou 		case 'c':	/* major compatible string */
2041ce4fecf1SPantelis Antoniou 			ret = of_property_read_string(dn, "compatible", &p);
2042ce4fecf1SPantelis Antoniou 			if (!ret)
2043ce4fecf1SPantelis Antoniou 				buf = string(buf, end, p, str_spec);
2044ce4fecf1SPantelis Antoniou 			break;
2045ce4fecf1SPantelis Antoniou 		case 'C':	/* full compatible string */
2046ce4fecf1SPantelis Antoniou 			has_mult = false;
2047ce4fecf1SPantelis Antoniou 			of_property_for_each_string(dn, "compatible", prop, p) {
2048ce4fecf1SPantelis Antoniou 				if (has_mult)
2049d529ac41SPetr Mladek 					buf = string_nocheck(buf, end, ",", str_spec);
2050d529ac41SPetr Mladek 				buf = string_nocheck(buf, end, "\"", str_spec);
2051ce4fecf1SPantelis Antoniou 				buf = string(buf, end, p, str_spec);
2052d529ac41SPetr Mladek 				buf = string_nocheck(buf, end, "\"", str_spec);
2053ce4fecf1SPantelis Antoniou 
2054ce4fecf1SPantelis Antoniou 				has_mult = true;
2055ce4fecf1SPantelis Antoniou 			}
2056ce4fecf1SPantelis Antoniou 			break;
2057ce4fecf1SPantelis Antoniou 		default:
2058ce4fecf1SPantelis Antoniou 			break;
2059ce4fecf1SPantelis Antoniou 		}
2060ce4fecf1SPantelis Antoniou 	}
2061ce4fecf1SPantelis Antoniou 
2062ce4fecf1SPantelis Antoniou 	return widen_string(buf, buf - buf_start, end, spec);
2063ce4fecf1SPantelis Antoniou }
2064ce4fecf1SPantelis Antoniou 
20653bd32d6aSSakari Ailus static noinline_for_stack
20663bd32d6aSSakari Ailus char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2067798cc27aSPetr Mladek 		    struct printf_spec spec, const char *fmt)
2068798cc27aSPetr Mladek {
20693bd32d6aSSakari Ailus 	struct printf_spec str_spec = spec;
20703bd32d6aSSakari Ailus 	char *buf_start = buf;
20713bd32d6aSSakari Ailus 
20723bd32d6aSSakari Ailus 	str_spec.field_width = -1;
20733bd32d6aSSakari Ailus 
20743bd32d6aSSakari Ailus 	if (*fmt != 'w')
20753bd32d6aSSakari Ailus 		return error_string(buf, end, "(%pf?)", spec);
20763bd32d6aSSakari Ailus 
20773bd32d6aSSakari Ailus 	if (check_pointer(&buf, end, fwnode, spec))
20783bd32d6aSSakari Ailus 		return buf;
20793bd32d6aSSakari Ailus 
20803bd32d6aSSakari Ailus 	fmt++;
20813bd32d6aSSakari Ailus 
20823bd32d6aSSakari Ailus 	switch (*fmt) {
20833bd32d6aSSakari Ailus 	case 'P':	/* name */
20843bd32d6aSSakari Ailus 		buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
20853bd32d6aSSakari Ailus 		break;
20863bd32d6aSSakari Ailus 	case 'f':	/* full_name */
20873bd32d6aSSakari Ailus 	default:
20883bd32d6aSSakari Ailus 		buf = fwnode_full_name_string(fwnode, buf, end);
20893bd32d6aSSakari Ailus 		break;
2090798cc27aSPetr Mladek 	}
2091798cc27aSPetr Mladek 
20923bd32d6aSSakari Ailus 	return widen_string(buf, buf - buf_start, end, spec);
2093798cc27aSPetr Mladek }
2094798cc27aSPetr Mladek 
20954d8a743cSLinus Torvalds /*
20964d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
20974d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
20984d8a743cSLinus Torvalds  * specifiers.
20994d8a743cSLinus Torvalds  *
21000b523769SJoe Perches  * Please update scripts/checkpatch.pl when adding/removing conversion
21010b523769SJoe Perches  * characters.  (Search for "check for vsprintf extension").
21020b523769SJoe Perches  *
2103332d2e78SLinus Torvalds  * Right now we handle:
21040fe1ef24SLinus Torvalds  *
2105cdb7e52dSSergey Senozhatsky  * - 'S' For symbolic direct pointers (or function descriptors) with offset
2106cdb7e52dSSergey Senozhatsky  * - 's' For symbolic direct pointers (or function descriptors) without offset
21079af77064SSakari Ailus  * - '[Ss]R' as above with __builtin_extract_return_addr() translation
21081586c5aeSSakari Ailus  * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
21091586c5aeSSakari Ailus  *	    %ps and %pS. Be careful when re-using these specifiers.
21100f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
2111c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2112c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
2113dbc760bcSTejun Heo  * - 'b[l]' For a bitmap, the number of bits is determined by the field
2114dbc760bcSTejun Heo  *       width which must be explicitly specified either as part of the
2115dbc760bcSTejun Heo  *       format string '%32b[l]' or through '%*b[l]', [l] selects
2116dbc760bcSTejun Heo  *       range-list format instead of hex format
2117dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
2118dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
21198a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
2120bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
2121c8e00060SJoe Perches  *       with a dash-separated hex notation
21227c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
21238a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
21248a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
21258a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
212610679643SDaniel Borkmann  *       [S][pfs]
212710679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
212810679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
21298a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
21308a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
21318a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
213210679643SDaniel Borkmann  *       [S][pfs]
213310679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
213410679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
213510679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
213610679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
2137*8eda94bdSAlexander A. Klimov  *       https://tools.ietf.org/html/rfc5952
213871dca95dSAndy Shevchenko  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
213971dca95dSAndy Shevchenko  *                of the following flags (see string_escape_mem() for the
214071dca95dSAndy Shevchenko  *                details):
214171dca95dSAndy Shevchenko  *                  a - ESCAPE_ANY
214271dca95dSAndy Shevchenko  *                  c - ESCAPE_SPECIAL
214371dca95dSAndy Shevchenko  *                  h - ESCAPE_HEX
214471dca95dSAndy Shevchenko  *                  n - ESCAPE_NULL
214571dca95dSAndy Shevchenko  *                  o - ESCAPE_OCTAL
214671dca95dSAndy Shevchenko  *                  p - ESCAPE_NP
214771dca95dSAndy Shevchenko  *                  s - ESCAPE_SPACE
214871dca95dSAndy Shevchenko  *                By default ESCAPE_ANY_NP is used.
21499ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
21509ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
21519ac6e44eSJoe Perches  *       Options for %pU are:
21529ac6e44eSJoe Perches  *         b big endian lower case hex (default)
21539ac6e44eSJoe Perches  *         B big endian UPPER case hex
21549ac6e44eSJoe Perches  *         l little endian lower case hex
21559ac6e44eSJoe Perches  *         L little endian UPPER case hex
21569ac6e44eSJoe Perches  *           big endian output byte order is:
21579ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
21589ac6e44eSJoe Perches  *           little endian output byte order is:
21599ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
21607db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
21617db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
21627db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
21637db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
21647db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
2165455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
2166c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
216731550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
216831550a16SAndy Shevchenko  *            a certain separator (' ' by default):
216931550a16SAndy Shevchenko  *              C colon
217031550a16SAndy Shevchenko  *              D dash
217131550a16SAndy Shevchenko  *              N no separator
217231550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
217331550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
2174aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2175aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
2176c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
2177c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
21781031bc58SDmitry Monakhov  * - 'g' For block_device name (gendisk + partition number)
21797daac5b2SAndy Shevchenko  * - 't[RT][dt][r]' For time and date as represented by:
21804d42c447SAndy Shevchenko  *      R    struct rtc_time
21817daac5b2SAndy Shevchenko  *      T    time64_t
2182900cca29SGeert Uytterhoeven  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2183900cca29SGeert Uytterhoeven  *       (legacy clock framework) of the clock
2184900cca29SGeert Uytterhoeven  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2185900cca29SGeert Uytterhoeven  *        (legacy clock framework) of the clock
2186edf14cdbSVlastimil Babka  * - 'G' For flags to be printed as a collection of symbolic strings that would
2187edf14cdbSVlastimil Babka  *       construct the specific value. Supported flags given by option:
2188edf14cdbSVlastimil Babka  *       p page flags (see struct page) given as pointer to unsigned long
2189edf14cdbSVlastimil Babka  *       g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2190edf14cdbSVlastimil Babka  *       v vma flags (VM_*) given as pointer to unsigned long
2191ce4fecf1SPantelis Antoniou  * - 'OF[fnpPcCF]'  For a device tree object
2192ce4fecf1SPantelis Antoniou  *                  Without any optional arguments prints the full_name
2193ce4fecf1SPantelis Antoniou  *                  f device node full_name
2194ce4fecf1SPantelis Antoniou  *                  n device node name
2195ce4fecf1SPantelis Antoniou  *                  p device node phandle
2196ce4fecf1SPantelis Antoniou  *                  P device node path spec (name + @unit)
2197ce4fecf1SPantelis Antoniou  *                  F device node flags
2198ce4fecf1SPantelis Antoniou  *                  c major compatible string
2199ce4fecf1SPantelis Antoniou  *                  C full compatible string
22003bd32d6aSSakari Ailus  * - 'fw[fP]'	For a firmware node (struct fwnode_handle) pointer
22013bd32d6aSSakari Ailus  *		Without an option prints the full name of the node
22023bd32d6aSSakari Ailus  *		f full name
22033bd32d6aSSakari Ailus  *		P node name, including a possible unit address
22047b1924a1STobin C. Harding  * - 'x' For printing the address. Equivalent to "%lx".
2205b2a5212fSDaniel Borkmann  * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2206b2a5212fSDaniel Borkmann  *           bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2207b2a5212fSDaniel Borkmann  *           or user (u) memory to probe, and:
2208b2a5212fSDaniel Borkmann  *              s a string, equivalent to "%s" on direct vsnprintf() use
22097b1924a1STobin C. Harding  *
2210b3ed2321STobin C. Harding  * ** When making changes please also update:
2211b3ed2321STobin C. Harding  *	Documentation/core-api/printk-formats.rst
22129ac6e44eSJoe Perches  *
2213ad67b74dSTobin C. Harding  * Note: The default behaviour (unadorned %p) is to hash the address,
2214ad67b74dSTobin C. Harding  * rendering it useful as a unique identifier.
22154d8a743cSLinus Torvalds  */
2216cf3b429bSJoe Perches static noinline_for_stack
2217cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2218fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
221978a8bf69SLinus Torvalds {
22200fe1ef24SLinus Torvalds 	switch (*fmt) {
22210fe1ef24SLinus Torvalds 	case 'S':
22229ac6e44eSJoe Perches 	case 's':
222304b8eb7aSSergey Senozhatsky 		ptr = dereference_symbol_descriptor(ptr);
222404b8eb7aSSergey Senozhatsky 		/* Fallthrough */
22250f77a8d3SNamhyung Kim 	case 'B':
2226b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
2227332d2e78SLinus Torvalds 	case 'R':
2228c7dabef8SBjorn Helgaas 	case 'r':
2229fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
223031550a16SAndy Shevchenko 	case 'h':
223131550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
2232dbc760bcSTejun Heo 	case 'b':
2233dbc760bcSTejun Heo 		switch (fmt[1]) {
2234dbc760bcSTejun Heo 		case 'l':
2235dbc760bcSTejun Heo 			return bitmap_list_string(buf, end, ptr, spec, fmt);
2236dbc760bcSTejun Heo 		default:
2237dbc760bcSTejun Heo 			return bitmap_string(buf, end, ptr, spec, fmt);
2238dbc760bcSTejun Heo 		}
22398a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
22408a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
224176597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
224276597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
22438a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
22448a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
22458a27f7c9SJoe Perches 					 * 4:	1.2.3.4
22468a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
22478a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
22488a27f7c9SJoe Perches 					 */
22498a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
22508a27f7c9SJoe Perches 					 * 4:	001.002.003.004
22518a27f7c9SJoe Perches 					 * 6:   000102...0f
22528a27f7c9SJoe Perches 					 */
2253f00cc102SPetr Mladek 		return ip_addr_string(buf, end, ptr, spec, fmt);
225471dca95dSAndy Shevchenko 	case 'E':
225571dca95dSAndy Shevchenko 		return escaped_string(buf, end, ptr, spec, fmt);
22569ac6e44eSJoe Perches 	case 'U':
22579ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
22587db6f5fbSJoe Perches 	case 'V':
22593e5903ebSPetr Mladek 		return va_format(buf, end, ptr, spec, fmt);
2260455cd5abSDan Rosenberg 	case 'K':
226157e73442STobin C. Harding 		return restricted_pointer(buf, end, ptr, spec);
2262c8f44affSMichał Mirosław 	case 'N':
2263431bca24SGeert Uytterhoeven 		return netdev_bits(buf, end, ptr, spec, fmt);
22647d799210SStepan Moskovchenko 	case 'a':
22653e5903ebSPetr Mladek 		return address_val(buf, end, ptr, spec, fmt);
22664b6ccca7SAl Viro 	case 'd':
22674b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
22684d42c447SAndy Shevchenko 	case 't':
22694d42c447SAndy Shevchenko 		return time_and_date(buf, end, ptr, spec, fmt);
2270900cca29SGeert Uytterhoeven 	case 'C':
2271900cca29SGeert Uytterhoeven 		return clock(buf, end, ptr, spec, fmt);
22724b6ccca7SAl Viro 	case 'D':
227336594b31SJia He 		return file_dentry_name(buf, end, ptr, spec, fmt);
22741031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
22751031bc58SDmitry Monakhov 	case 'g':
22761031bc58SDmitry Monakhov 		return bdev_name(buf, end, ptr, spec, fmt);
22771031bc58SDmitry Monakhov #endif
22781031bc58SDmitry Monakhov 
2279edf14cdbSVlastimil Babka 	case 'G':
22800b74d4d7SPetr Mladek 		return flags_string(buf, end, ptr, spec, fmt);
2281ce4fecf1SPantelis Antoniou 	case 'O':
228283abc5a7SSakari Ailus 		return device_node_string(buf, end, ptr, spec, fmt + 1);
22833bd32d6aSSakari Ailus 	case 'f':
22843bd32d6aSSakari Ailus 		return fwnode_string(buf, end, ptr, spec, fmt + 1);
22857b1924a1STobin C. Harding 	case 'x':
22867b1924a1STobin C. Harding 		return pointer_string(buf, end, ptr, spec);
228757f5677eSRasmus Villemoes 	case 'e':
228857f5677eSRasmus Villemoes 		/* %pe with a non-ERR_PTR gets treated as plain %p */
228957f5677eSRasmus Villemoes 		if (!IS_ERR(ptr))
229057f5677eSRasmus Villemoes 			break;
229157f5677eSRasmus Villemoes 		return err_ptr(buf, end, ptr, spec);
2292b2a5212fSDaniel Borkmann 	case 'u':
2293b2a5212fSDaniel Borkmann 	case 'k':
2294b2a5212fSDaniel Borkmann 		switch (fmt[1]) {
2295b2a5212fSDaniel Borkmann 		case 's':
2296b2a5212fSDaniel Borkmann 			return string(buf, end, ptr, spec);
2297b2a5212fSDaniel Borkmann 		default:
2298b2a5212fSDaniel Borkmann 			return error_string(buf, end, "(einval)", spec);
2299b2a5212fSDaniel Borkmann 		}
23000fe1ef24SLinus Torvalds 	}
2301fef20d9cSFrederic Weisbecker 
2302ad67b74dSTobin C. Harding 	/* default is to _not_ leak addresses, hash before printing */
2303ad67b74dSTobin C. Harding 	return ptr_to_id(buf, end, ptr, spec);
2304fef20d9cSFrederic Weisbecker }
2305fef20d9cSFrederic Weisbecker 
2306fef20d9cSFrederic Weisbecker /*
2307fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
2308fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
2309fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
2310fef20d9cSFrederic Weisbecker  * to go on the next call).
2311fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
2312fef20d9cSFrederic Weisbecker  *
2313fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
2314fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
2315fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
23165b5e0928SAlexey Dobriyan  * 'Z' changed to 'z' --adobriyan 2017-01-25
2317fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
2318fef20d9cSFrederic Weisbecker  *
2319fef20d9cSFrederic Weisbecker  * @fmt: the format string
2320fef20d9cSFrederic Weisbecker  * @type of the token returned
2321fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
2322fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
2323fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
2324fef20d9cSFrederic Weisbecker  * @precision: precision of a number
2325fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
2326fef20d9cSFrederic Weisbecker  */
2327cf3b429bSJoe Perches static noinline_for_stack
2328cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
2329fef20d9cSFrederic Weisbecker {
2330fef20d9cSFrederic Weisbecker 	const char *start = fmt;
2331d0484193SRasmus Villemoes 	char qualifier;
2332fef20d9cSFrederic Weisbecker 
2333fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
2334ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
2335fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
2336fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
2337fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
2338fef20d9cSFrederic Weisbecker 		}
2339fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
2340fef20d9cSFrederic Weisbecker 		goto precision;
2341fef20d9cSFrederic Weisbecker 	}
2342fef20d9cSFrederic Weisbecker 
2343fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
2344fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
2345fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
2346fef20d9cSFrederic Weisbecker 			spec->precision = 0;
2347fef20d9cSFrederic Weisbecker 
2348fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
2349fef20d9cSFrederic Weisbecker 		goto qualifier;
2350fef20d9cSFrederic Weisbecker 	}
2351fef20d9cSFrederic Weisbecker 
2352fef20d9cSFrederic Weisbecker 	/* By default */
2353fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
2354fef20d9cSFrederic Weisbecker 
2355fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
2356fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
2357fef20d9cSFrederic Weisbecker 			break;
2358fef20d9cSFrederic Weisbecker 	}
2359fef20d9cSFrederic Weisbecker 
2360fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
2361fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
2362fef20d9cSFrederic Weisbecker 		return fmt - start;
2363fef20d9cSFrederic Weisbecker 
2364fef20d9cSFrederic Weisbecker 	/* Process flags */
2365fef20d9cSFrederic Weisbecker 	spec->flags = 0;
2366fef20d9cSFrederic Weisbecker 
2367fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
2368fef20d9cSFrederic Weisbecker 		bool found = true;
2369fef20d9cSFrederic Weisbecker 
2370fef20d9cSFrederic Weisbecker 		++fmt;
2371fef20d9cSFrederic Weisbecker 
2372fef20d9cSFrederic Weisbecker 		switch (*fmt) {
2373fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
2374fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
2375fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
2376fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
2377fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
2378fef20d9cSFrederic Weisbecker 		default:  found = false;
2379fef20d9cSFrederic Weisbecker 		}
2380fef20d9cSFrederic Weisbecker 
2381fef20d9cSFrederic Weisbecker 		if (!found)
2382fef20d9cSFrederic Weisbecker 			break;
2383fef20d9cSFrederic Weisbecker 	}
2384fef20d9cSFrederic Weisbecker 
2385fef20d9cSFrederic Weisbecker 	/* get field width */
2386fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
2387fef20d9cSFrederic Weisbecker 
2388fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
2389fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
2390fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
2391fef20d9cSFrederic Weisbecker 		/* it's the next argument */
2392ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
2393fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2394fef20d9cSFrederic Weisbecker 	}
2395fef20d9cSFrederic Weisbecker 
2396fef20d9cSFrederic Weisbecker precision:
2397fef20d9cSFrederic Weisbecker 	/* get the precision */
2398fef20d9cSFrederic Weisbecker 	spec->precision = -1;
2399fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
2400fef20d9cSFrederic Weisbecker 		++fmt;
2401fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
2402fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
2403fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
2404fef20d9cSFrederic Weisbecker 				spec->precision = 0;
2405fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
2406fef20d9cSFrederic Weisbecker 			/* it's the next argument */
2407adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
2408fef20d9cSFrederic Weisbecker 			return ++fmt - start;
2409fef20d9cSFrederic Weisbecker 		}
2410fef20d9cSFrederic Weisbecker 	}
2411fef20d9cSFrederic Weisbecker 
2412fef20d9cSFrederic Weisbecker qualifier:
2413fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
2414d0484193SRasmus Villemoes 	qualifier = 0;
241575fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
24165b5e0928SAlexey Dobriyan 	    *fmt == 'z' || *fmt == 't') {
2417d0484193SRasmus Villemoes 		qualifier = *fmt++;
2418d0484193SRasmus Villemoes 		if (unlikely(qualifier == *fmt)) {
2419d0484193SRasmus Villemoes 			if (qualifier == 'l') {
2420d0484193SRasmus Villemoes 				qualifier = 'L';
2421fef20d9cSFrederic Weisbecker 				++fmt;
2422d0484193SRasmus Villemoes 			} else if (qualifier == 'h') {
2423d0484193SRasmus Villemoes 				qualifier = 'H';
2424a4e94ef0SZhaolei 				++fmt;
2425a4e94ef0SZhaolei 			}
2426fef20d9cSFrederic Weisbecker 		}
2427fef20d9cSFrederic Weisbecker 	}
2428fef20d9cSFrederic Weisbecker 
2429fef20d9cSFrederic Weisbecker 	/* default base */
2430fef20d9cSFrederic Weisbecker 	spec->base = 10;
2431fef20d9cSFrederic Weisbecker 	switch (*fmt) {
2432fef20d9cSFrederic Weisbecker 	case 'c':
2433fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
2434fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2435fef20d9cSFrederic Weisbecker 
2436fef20d9cSFrederic Weisbecker 	case 's':
2437fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
2438fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2439fef20d9cSFrederic Weisbecker 
2440fef20d9cSFrederic Weisbecker 	case 'p':
2441fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
2442ffbfed03SRasmus Villemoes 		return ++fmt - start;
2443fef20d9cSFrederic Weisbecker 
2444fef20d9cSFrederic Weisbecker 	case '%':
2445fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
2446fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2447fef20d9cSFrederic Weisbecker 
2448fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
2449fef20d9cSFrederic Weisbecker 	case 'o':
2450fef20d9cSFrederic Weisbecker 		spec->base = 8;
2451fef20d9cSFrederic Weisbecker 		break;
2452fef20d9cSFrederic Weisbecker 
2453fef20d9cSFrederic Weisbecker 	case 'x':
2454fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
24557e6bd6f3SAndy Shevchenko 		/* fall through */
2456fef20d9cSFrederic Weisbecker 
2457fef20d9cSFrederic Weisbecker 	case 'X':
2458fef20d9cSFrederic Weisbecker 		spec->base = 16;
2459fef20d9cSFrederic Weisbecker 		break;
2460fef20d9cSFrederic Weisbecker 
2461fef20d9cSFrederic Weisbecker 	case 'd':
2462fef20d9cSFrederic Weisbecker 	case 'i':
246339e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
2464fef20d9cSFrederic Weisbecker 	case 'u':
2465fef20d9cSFrederic Weisbecker 		break;
2466fef20d9cSFrederic Weisbecker 
2467708d96fdSRyan Mallon 	case 'n':
2468708d96fdSRyan Mallon 		/*
2469b006f19bSRasmus Villemoes 		 * Since %n poses a greater security risk than
2470b006f19bSRasmus Villemoes 		 * utility, treat it as any other invalid or
2471b006f19bSRasmus Villemoes 		 * unsupported format specifier.
2472708d96fdSRyan Mallon 		 */
2473708d96fdSRyan Mallon 		/* Fall-through */
2474708d96fdSRyan Mallon 
2475fef20d9cSFrederic Weisbecker 	default:
2476b006f19bSRasmus Villemoes 		WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2477fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
2478fef20d9cSFrederic Weisbecker 		return fmt - start;
2479fef20d9cSFrederic Weisbecker 	}
2480fef20d9cSFrederic Weisbecker 
2481d0484193SRasmus Villemoes 	if (qualifier == 'L')
2482fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
2483d0484193SRasmus Villemoes 	else if (qualifier == 'l') {
248451be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
248551be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
24865b5e0928SAlexey Dobriyan 	} else if (qualifier == 'z') {
2487fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
2488d0484193SRasmus Villemoes 	} else if (qualifier == 't') {
2489fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
2490d0484193SRasmus Villemoes 	} else if (qualifier == 'H') {
249151be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
249251be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2493d0484193SRasmus Villemoes 	} else if (qualifier == 'h') {
249451be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
249551be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2496fef20d9cSFrederic Weisbecker 	} else {
249751be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
249851be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2499fef20d9cSFrederic Weisbecker 	}
2500fef20d9cSFrederic Weisbecker 
2501fef20d9cSFrederic Weisbecker 	return ++fmt - start;
250278a8bf69SLinus Torvalds }
250378a8bf69SLinus Torvalds 
25044d72ba01SRasmus Villemoes static void
25054d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width)
25064d72ba01SRasmus Villemoes {
25074d72ba01SRasmus Villemoes 	spec->field_width = width;
25084d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
25094d72ba01SRasmus Villemoes 		spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
25104d72ba01SRasmus Villemoes 	}
25114d72ba01SRasmus Villemoes }
25124d72ba01SRasmus Villemoes 
25134d72ba01SRasmus Villemoes static void
25144d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec)
25154d72ba01SRasmus Villemoes {
25164d72ba01SRasmus Villemoes 	spec->precision = prec;
25174d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
25184d72ba01SRasmus Villemoes 		spec->precision = clamp(prec, 0, PRECISION_MAX);
25194d72ba01SRasmus Villemoes 	}
25204d72ba01SRasmus Villemoes }
25214d72ba01SRasmus Villemoes 
25221da177e4SLinus Torvalds /**
25231da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
25241da177e4SLinus Torvalds  * @buf: The buffer to place the result into
25251da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
25261da177e4SLinus Torvalds  * @fmt: The format string to use
25271da177e4SLinus Torvalds  * @args: Arguments for the format string
25281da177e4SLinus Torvalds  *
2529d7ec9a05SRasmus Villemoes  * This function generally follows C99 vsnprintf, but has some
2530d7ec9a05SRasmus Villemoes  * extensions and a few limitations:
2531d7ec9a05SRasmus Villemoes  *
25326cc89134Smchehab@s-opensource.com  *  - ``%n`` is unsupported
25336cc89134Smchehab@s-opensource.com  *  - ``%p*`` is handled by pointer()
253420036fdcSAndi Kleen  *
253527e7c0e8SJonathan Corbet  * See pointer() or Documentation/core-api/printk-formats.rst for more
25365e4ee7b1SMartin Kletzander  * extensive description.
25375e4ee7b1SMartin Kletzander  *
25385e4ee7b1SMartin Kletzander  * **Please update the documentation in both places when making changes**
253980f548e0SAndrew Morton  *
25401da177e4SLinus Torvalds  * The return value is the number of characters which would
25411da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
25421da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
25431da177e4SLinus Torvalds  * number of characters written into @buf as return value
254472fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
25451da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
25461da177e4SLinus Torvalds  * string is truncated.
25471da177e4SLinus Torvalds  *
2548ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
25491da177e4SLinus Torvalds  */
25501da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
25511da177e4SLinus Torvalds {
25521da177e4SLinus Torvalds 	unsigned long long num;
2553d4be151bSAndré Goddard Rosa 	char *str, *end;
2554fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
25551da177e4SLinus Torvalds 
2556f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
2557f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
25582aa2f9e2SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
25591da177e4SLinus Torvalds 		return 0;
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds 	str = buf;
2562f796937aSJeremy Fitzhardinge 	end = buf + size;
25631da177e4SLinus Torvalds 
2564f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
2565f796937aSJeremy Fitzhardinge 	if (end < buf) {
25661da177e4SLinus Torvalds 		end = ((void *)-1);
2567f796937aSJeremy Fitzhardinge 		size = end - buf;
25681da177e4SLinus Torvalds 	}
25691da177e4SLinus Torvalds 
2570fef20d9cSFrederic Weisbecker 	while (*fmt) {
2571fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2572d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2573fef20d9cSFrederic Weisbecker 
2574fef20d9cSFrederic Weisbecker 		fmt += read;
2575fef20d9cSFrederic Weisbecker 
2576fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2577fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2578fef20d9cSFrederic Weisbecker 			int copy = read;
2579fef20d9cSFrederic Weisbecker 			if (str < end) {
2580fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2581fef20d9cSFrederic Weisbecker 					copy = end - str;
2582fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2583fef20d9cSFrederic Weisbecker 			}
2584fef20d9cSFrederic Weisbecker 			str += read;
2585fef20d9cSFrederic Weisbecker 			break;
25861da177e4SLinus Torvalds 		}
25871da177e4SLinus Torvalds 
2588ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
25894d72ba01SRasmus Villemoes 			set_field_width(&spec, va_arg(args, int));
2590fef20d9cSFrederic Weisbecker 			break;
25911da177e4SLinus Torvalds 
2592fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
25934d72ba01SRasmus Villemoes 			set_precision(&spec, va_arg(args, int));
2594fef20d9cSFrederic Weisbecker 			break;
25951da177e4SLinus Torvalds 
2596d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2597d4be151bSAndré Goddard Rosa 			char c;
2598d4be151bSAndré Goddard Rosa 
2599fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2600fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
2601f796937aSJeremy Fitzhardinge 					if (str < end)
26021da177e4SLinus Torvalds 						*str = ' ';
26031da177e4SLinus Torvalds 					++str;
2604fef20d9cSFrederic Weisbecker 
26051da177e4SLinus Torvalds 				}
26061da177e4SLinus Torvalds 			}
26071da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
2608f796937aSJeremy Fitzhardinge 			if (str < end)
26091da177e4SLinus Torvalds 				*str = c;
26101da177e4SLinus Torvalds 			++str;
2611fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
2612f796937aSJeremy Fitzhardinge 				if (str < end)
26131da177e4SLinus Torvalds 					*str = ' ';
26141da177e4SLinus Torvalds 				++str;
26151da177e4SLinus Torvalds 			}
2616fef20d9cSFrederic Weisbecker 			break;
2617d4be151bSAndré Goddard Rosa 		}
26181da177e4SLinus Torvalds 
2619fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
2620fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
2621fef20d9cSFrederic Weisbecker 			break;
26221da177e4SLinus Torvalds 
2623fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2624ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, va_arg(args, void *),
2625fef20d9cSFrederic Weisbecker 				      spec);
2626fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
26274d8a743cSLinus Torvalds 				fmt++;
2628fef20d9cSFrederic Weisbecker 			break;
26291da177e4SLinus Torvalds 
2630fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2631f796937aSJeremy Fitzhardinge 			if (str < end)
26321da177e4SLinus Torvalds 				*str = '%';
26331da177e4SLinus Torvalds 			++str;
26341da177e4SLinus Torvalds 			break;
26351da177e4SLinus Torvalds 
2636fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
2637b006f19bSRasmus Villemoes 			/*
2638b006f19bSRasmus Villemoes 			 * Presumably the arguments passed gcc's type
2639b006f19bSRasmus Villemoes 			 * checking, but there is no safe or sane way
2640b006f19bSRasmus Villemoes 			 * for us to continue parsing the format and
2641b006f19bSRasmus Villemoes 			 * fetching from the va_list; the remaining
2642b006f19bSRasmus Villemoes 			 * specifiers and arguments would be out of
2643b006f19bSRasmus Villemoes 			 * sync.
2644b006f19bSRasmus Villemoes 			 */
2645b006f19bSRasmus Villemoes 			goto out;
2646fef20d9cSFrederic Weisbecker 
2647fef20d9cSFrederic Weisbecker 		default:
2648fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2649fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2650fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
2651fef20d9cSFrederic Weisbecker 				break;
2652fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2653fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
2654fef20d9cSFrederic Weisbecker 				break;
2655fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2656fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
2657fef20d9cSFrederic Weisbecker 				break;
2658fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2659ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
2660ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
2661ef124960SJason Gunthorpe 				else
2662fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
2663fef20d9cSFrederic Weisbecker 				break;
2664fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2665fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
2666fef20d9cSFrederic Weisbecker 				break;
2667a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2668a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
2669a4e94ef0SZhaolei 				break;
2670a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2671a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
2672a4e94ef0SZhaolei 				break;
2673fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2674fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
2675fef20d9cSFrederic Weisbecker 				break;
2676fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2677fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
2678fef20d9cSFrederic Weisbecker 				break;
267939e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
268039e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
2681fef20d9cSFrederic Weisbecker 				break;
2682fef20d9cSFrederic Weisbecker 			default:
2683fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
26841da177e4SLinus Torvalds 			}
2685fef20d9cSFrederic Weisbecker 
2686fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
26871da177e4SLinus Torvalds 		}
2688fef20d9cSFrederic Weisbecker 	}
2689fef20d9cSFrederic Weisbecker 
2690b006f19bSRasmus Villemoes out:
2691f796937aSJeremy Fitzhardinge 	if (size > 0) {
2692f796937aSJeremy Fitzhardinge 		if (str < end)
26931da177e4SLinus Torvalds 			*str = '\0';
2694f796937aSJeremy Fitzhardinge 		else
26950a6047eeSLinus Torvalds 			end[-1] = '\0';
2696f796937aSJeremy Fitzhardinge 	}
2697fef20d9cSFrederic Weisbecker 
2698f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
26991da177e4SLinus Torvalds 	return str-buf;
2700fef20d9cSFrederic Weisbecker 
27011da177e4SLinus Torvalds }
27021da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds /**
27051da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
27061da177e4SLinus Torvalds  * @buf: The buffer to place the result into
27071da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
27081da177e4SLinus Torvalds  * @fmt: The format string to use
27091da177e4SLinus Torvalds  * @args: Arguments for the format string
27101da177e4SLinus Torvalds  *
27111da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
2712b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
27131da177e4SLinus Torvalds  * returns 0.
27141da177e4SLinus Torvalds  *
2715ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
271620036fdcSAndi Kleen  *
271720036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
27181da177e4SLinus Torvalds  */
27191da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
27201da177e4SLinus Torvalds {
27211da177e4SLinus Torvalds 	int i;
27221da177e4SLinus Torvalds 
27231da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
27247b9186f5SAndré Goddard Rosa 
2725b921c69fSAnton Arapov 	if (likely(i < size))
2726b921c69fSAnton Arapov 		return i;
2727b921c69fSAnton Arapov 	if (size != 0)
2728b921c69fSAnton Arapov 		return size - 1;
2729b921c69fSAnton Arapov 	return 0;
27301da177e4SLinus Torvalds }
27311da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds /**
27341da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
27351da177e4SLinus Torvalds  * @buf: The buffer to place the result into
27361da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
27371da177e4SLinus Torvalds  * @fmt: The format string to use
27381da177e4SLinus Torvalds  * @...: Arguments for the format string
27391da177e4SLinus Torvalds  *
27401da177e4SLinus Torvalds  * The return value is the number of characters which would be
27411da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
27421da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
27431da177e4SLinus Torvalds  * @size, the resulting string is truncated.
274420036fdcSAndi Kleen  *
274520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
27461da177e4SLinus Torvalds  */
27471da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
27481da177e4SLinus Torvalds {
27491da177e4SLinus Torvalds 	va_list args;
27501da177e4SLinus Torvalds 	int i;
27511da177e4SLinus Torvalds 
27521da177e4SLinus Torvalds 	va_start(args, fmt);
27531da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
27541da177e4SLinus Torvalds 	va_end(args);
27557b9186f5SAndré Goddard Rosa 
27561da177e4SLinus Torvalds 	return i;
27571da177e4SLinus Torvalds }
27581da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
27591da177e4SLinus Torvalds 
27601da177e4SLinus Torvalds /**
27611da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
27621da177e4SLinus Torvalds  * @buf: The buffer to place the result into
27631da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
27641da177e4SLinus Torvalds  * @fmt: The format string to use
27651da177e4SLinus Torvalds  * @...: Arguments for the format string
27661da177e4SLinus Torvalds  *
27671da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
2768b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
27691da177e4SLinus Torvalds  */
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
27721da177e4SLinus Torvalds {
27731da177e4SLinus Torvalds 	va_list args;
27741da177e4SLinus Torvalds 	int i;
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds 	va_start(args, fmt);
2777b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
27781da177e4SLinus Torvalds 	va_end(args);
27797b9186f5SAndré Goddard Rosa 
2780b903c0b8SChangli Gao 	return i;
27811da177e4SLinus Torvalds }
27821da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds /**
27851da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
27861da177e4SLinus Torvalds  * @buf: The buffer to place the result into
27871da177e4SLinus Torvalds  * @fmt: The format string to use
27881da177e4SLinus Torvalds  * @args: Arguments for the format string
27891da177e4SLinus Torvalds  *
27901da177e4SLinus Torvalds  * The function returns the number of characters written
279172fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
27921da177e4SLinus Torvalds  * buffer overflows.
27931da177e4SLinus Torvalds  *
2794ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
279520036fdcSAndi Kleen  *
279620036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
27971da177e4SLinus Torvalds  */
27981da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
27991da177e4SLinus Torvalds {
28001da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
28011da177e4SLinus Torvalds }
28021da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
28031da177e4SLinus Torvalds 
28041da177e4SLinus Torvalds /**
28051da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
28061da177e4SLinus Torvalds  * @buf: The buffer to place the result into
28071da177e4SLinus Torvalds  * @fmt: The format string to use
28081da177e4SLinus Torvalds  * @...: Arguments for the format string
28091da177e4SLinus Torvalds  *
28101da177e4SLinus Torvalds  * The function returns the number of characters written
281172fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
28121da177e4SLinus Torvalds  * buffer overflows.
281320036fdcSAndi Kleen  *
281420036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
28151da177e4SLinus Torvalds  */
28161da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
28171da177e4SLinus Torvalds {
28181da177e4SLinus Torvalds 	va_list args;
28191da177e4SLinus Torvalds 	int i;
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds 	va_start(args, fmt);
28221da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
28231da177e4SLinus Torvalds 	va_end(args);
28247b9186f5SAndré Goddard Rosa 
28251da177e4SLinus Torvalds 	return i;
28261da177e4SLinus Torvalds }
28271da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
28281da177e4SLinus Torvalds 
28294370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
28304370aa4aSLai Jiangshan /*
28314370aa4aSLai Jiangshan  * bprintf service:
28324370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
28334370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
28344370aa4aSLai Jiangshan  */
28354370aa4aSLai Jiangshan 
28364370aa4aSLai Jiangshan /**
28374370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
28384370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
28394370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
28404370aa4aSLai Jiangshan  * @fmt: The format string to use
28414370aa4aSLai Jiangshan  * @args: Arguments for the format string
28424370aa4aSLai Jiangshan  *
28434370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
2844da3dae54SMasanari Iida  * is skipped.
28454370aa4aSLai Jiangshan  *
28464370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
28474370aa4aSLai Jiangshan  * the given input.
28484370aa4aSLai Jiangshan  *
28494370aa4aSLai Jiangshan  * NOTE:
28504370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
28514370aa4aSLai Jiangshan  * valid for bstr_printf().
28524370aa4aSLai Jiangshan  */
28534370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
28544370aa4aSLai Jiangshan {
2855fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
28564370aa4aSLai Jiangshan 	char *str, *end;
2857841a915dSSteven Rostedt (VMware) 	int width;
28584370aa4aSLai Jiangshan 
28594370aa4aSLai Jiangshan 	str = (char *)bin_buf;
28604370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
28614370aa4aSLai Jiangshan 
28624370aa4aSLai Jiangshan #define save_arg(type)							\
2863841a915dSSteven Rostedt (VMware) ({									\
28644370aa4aSLai Jiangshan 	unsigned long long value;					\
2865841a915dSSteven Rostedt (VMware) 	if (sizeof(type) == 8) {					\
2866841a915dSSteven Rostedt (VMware) 		unsigned long long val8;				\
28674370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
2868841a915dSSteven Rostedt (VMware) 		val8 = va_arg(args, unsigned long long);		\
28694370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
2870841a915dSSteven Rostedt (VMware) 			*(u32 *)str = *(u32 *)&val8;			\
2871841a915dSSteven Rostedt (VMware) 			*(u32 *)(str + 4) = *((u32 *)&val8 + 1);	\
28724370aa4aSLai Jiangshan 		}							\
2873841a915dSSteven Rostedt (VMware) 		value = val8;						\
28744370aa4aSLai Jiangshan 	} else {							\
2875841a915dSSteven Rostedt (VMware) 		unsigned int val4;					\
28764370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
2877841a915dSSteven Rostedt (VMware) 		val4 = va_arg(args, int);				\
28784370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
2879841a915dSSteven Rostedt (VMware) 			*(typeof(type) *)str = (type)(long)val4;	\
2880841a915dSSteven Rostedt (VMware) 		value = (unsigned long long)val4;			\
28814370aa4aSLai Jiangshan 	}								\
28824370aa4aSLai Jiangshan 	str += sizeof(type);						\
2883841a915dSSteven Rostedt (VMware) 	value;								\
2884841a915dSSteven Rostedt (VMware) })
28854370aa4aSLai Jiangshan 
2886fef20d9cSFrederic Weisbecker 	while (*fmt) {
2887d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
28884370aa4aSLai Jiangshan 
2889fef20d9cSFrederic Weisbecker 		fmt += read;
2890fef20d9cSFrederic Weisbecker 
2891fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2892fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
2893d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
2894fef20d9cSFrederic Weisbecker 			break;
2895b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
2896b006f19bSRasmus Villemoes 			goto out;
2897fef20d9cSFrederic Weisbecker 
2898ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2899fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2900841a915dSSteven Rostedt (VMware) 			width = (int)save_arg(int);
2901841a915dSSteven Rostedt (VMware) 			/* Pointers may require the width */
2902841a915dSSteven Rostedt (VMware) 			if (*fmt == 'p')
2903841a915dSSteven Rostedt (VMware) 				set_field_width(&spec, width);
2904fef20d9cSFrederic Weisbecker 			break;
29054370aa4aSLai Jiangshan 
2906fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
29074370aa4aSLai Jiangshan 			save_arg(char);
2908fef20d9cSFrederic Weisbecker 			break;
2909fef20d9cSFrederic Weisbecker 
2910fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
29114370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
29123e5903ebSPetr Mladek 			const char *err_msg;
29134370aa4aSLai Jiangshan 			size_t len;
29146c356634SAndré Goddard Rosa 
29153e5903ebSPetr Mladek 			err_msg = check_pointer_msg(save_str);
29163e5903ebSPetr Mladek 			if (err_msg)
29173e5903ebSPetr Mladek 				save_str = err_msg;
29183e5903ebSPetr Mladek 
29196c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
29206c356634SAndré Goddard Rosa 			if (str + len < end)
29216c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
29226c356634SAndré Goddard Rosa 			str += len;
2923fef20d9cSFrederic Weisbecker 			break;
29244370aa4aSLai Jiangshan 		}
2925fef20d9cSFrederic Weisbecker 
2926fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2927841a915dSSteven Rostedt (VMware) 			/* Dereferenced pointers must be done now */
2928841a915dSSteven Rostedt (VMware) 			switch (*fmt) {
2929841a915dSSteven Rostedt (VMware) 			/* Dereference of functions is still OK */
2930841a915dSSteven Rostedt (VMware) 			case 'S':
2931841a915dSSteven Rostedt (VMware) 			case 's':
29321e6338cfSSteven Rostedt (VMware) 			case 'x':
29331e6338cfSSteven Rostedt (VMware) 			case 'K':
293457f5677eSRasmus Villemoes 			case 'e':
29354370aa4aSLai Jiangshan 				save_arg(void *);
2936841a915dSSteven Rostedt (VMware) 				break;
2937841a915dSSteven Rostedt (VMware) 			default:
2938841a915dSSteven Rostedt (VMware) 				if (!isalnum(*fmt)) {
2939841a915dSSteven Rostedt (VMware) 					save_arg(void *);
2940841a915dSSteven Rostedt (VMware) 					break;
2941841a915dSSteven Rostedt (VMware) 				}
2942841a915dSSteven Rostedt (VMware) 				str = pointer(fmt, str, end, va_arg(args, void *),
2943841a915dSSteven Rostedt (VMware) 					      spec);
2944841a915dSSteven Rostedt (VMware) 				if (str + 1 < end)
2945841a915dSSteven Rostedt (VMware) 					*str++ = '\0';
2946841a915dSSteven Rostedt (VMware) 				else
2947841a915dSSteven Rostedt (VMware) 					end[-1] = '\0'; /* Must be nul terminated */
2948841a915dSSteven Rostedt (VMware) 			}
29494370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2950fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
29514370aa4aSLai Jiangshan 				fmt++;
2952fef20d9cSFrederic Weisbecker 			break;
2953fef20d9cSFrederic Weisbecker 
2954fef20d9cSFrederic Weisbecker 		default:
2955fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2956fef20d9cSFrederic Weisbecker 
2957fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2958fef20d9cSFrederic Weisbecker 				save_arg(long long);
2959fef20d9cSFrederic Weisbecker 				break;
2960fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2961fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2962fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
2963fef20d9cSFrederic Weisbecker 				break;
2964fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2965fef20d9cSFrederic Weisbecker 				save_arg(size_t);
2966fef20d9cSFrederic Weisbecker 				break;
2967fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2968fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
2969fef20d9cSFrederic Weisbecker 				break;
2970a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2971a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2972a4e94ef0SZhaolei 				save_arg(char);
2973a4e94ef0SZhaolei 				break;
2974fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2975fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2976fef20d9cSFrederic Weisbecker 				save_arg(short);
2977fef20d9cSFrederic Weisbecker 				break;
2978fef20d9cSFrederic Weisbecker 			default:
2979fef20d9cSFrederic Weisbecker 				save_arg(int);
2980fef20d9cSFrederic Weisbecker 			}
2981fef20d9cSFrederic Weisbecker 		}
2982fef20d9cSFrederic Weisbecker 	}
2983fef20d9cSFrederic Weisbecker 
2984b006f19bSRasmus Villemoes out:
29857b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2986fef20d9cSFrederic Weisbecker #undef save_arg
29874370aa4aSLai Jiangshan }
29884370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
29894370aa4aSLai Jiangshan 
29904370aa4aSLai Jiangshan /**
29914370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
29924370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
29934370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
29944370aa4aSLai Jiangshan  * @fmt: The format string to use
29954370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
29964370aa4aSLai Jiangshan  *
29974370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
29984370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
29994370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
30004370aa4aSLai Jiangshan  *
30014370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
30020efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
30034370aa4aSLai Jiangshan  *
30044370aa4aSLai Jiangshan  * The return value is the number of characters which would
30054370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
30064370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
30074370aa4aSLai Jiangshan  * number of characters written into @buf as return value
30084370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
30094370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
30104370aa4aSLai Jiangshan  * string is truncated.
30114370aa4aSLai Jiangshan  */
30124370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
30134370aa4aSLai Jiangshan {
3014fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
3015d4be151bSAndré Goddard Rosa 	char *str, *end;
3016d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
30174370aa4aSLai Jiangshan 
3018762abb51SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
30194370aa4aSLai Jiangshan 		return 0;
30204370aa4aSLai Jiangshan 
30214370aa4aSLai Jiangshan 	str = buf;
30224370aa4aSLai Jiangshan 	end = buf + size;
30234370aa4aSLai Jiangshan 
30244370aa4aSLai Jiangshan #define get_arg(type)							\
30254370aa4aSLai Jiangshan ({									\
30264370aa4aSLai Jiangshan 	typeof(type) value;						\
30274370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
30284370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
30294370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
30304370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
30314370aa4aSLai Jiangshan 	} else {							\
30324370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
30334370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
30344370aa4aSLai Jiangshan 	}								\
30354370aa4aSLai Jiangshan 	args += sizeof(type);						\
30364370aa4aSLai Jiangshan 	value;								\
30374370aa4aSLai Jiangshan })
30384370aa4aSLai Jiangshan 
30394370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
30404370aa4aSLai Jiangshan 	if (end < buf) {
30414370aa4aSLai Jiangshan 		end = ((void *)-1);
30424370aa4aSLai Jiangshan 		size = end - buf;
30434370aa4aSLai Jiangshan 	}
30444370aa4aSLai Jiangshan 
3045fef20d9cSFrederic Weisbecker 	while (*fmt) {
3046fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
3047d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
3048fef20d9cSFrederic Weisbecker 
3049fef20d9cSFrederic Weisbecker 		fmt += read;
3050fef20d9cSFrederic Weisbecker 
3051fef20d9cSFrederic Weisbecker 		switch (spec.type) {
3052fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
3053fef20d9cSFrederic Weisbecker 			int copy = read;
3054fef20d9cSFrederic Weisbecker 			if (str < end) {
3055fef20d9cSFrederic Weisbecker 				if (copy > end - str)
3056fef20d9cSFrederic Weisbecker 					copy = end - str;
3057fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
3058fef20d9cSFrederic Weisbecker 			}
3059fef20d9cSFrederic Weisbecker 			str += read;
3060fef20d9cSFrederic Weisbecker 			break;
30614370aa4aSLai Jiangshan 		}
30624370aa4aSLai Jiangshan 
3063ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
30644d72ba01SRasmus Villemoes 			set_field_width(&spec, get_arg(int));
3065fef20d9cSFrederic Weisbecker 			break;
30664370aa4aSLai Jiangshan 
3067fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
30684d72ba01SRasmus Villemoes 			set_precision(&spec, get_arg(int));
3069fef20d9cSFrederic Weisbecker 			break;
30704370aa4aSLai Jiangshan 
3071d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
3072d4be151bSAndré Goddard Rosa 			char c;
3073d4be151bSAndré Goddard Rosa 
3074fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
3075fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
30764370aa4aSLai Jiangshan 					if (str < end)
30774370aa4aSLai Jiangshan 						*str = ' ';
30784370aa4aSLai Jiangshan 					++str;
30794370aa4aSLai Jiangshan 				}
30804370aa4aSLai Jiangshan 			}
30814370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
30824370aa4aSLai Jiangshan 			if (str < end)
30834370aa4aSLai Jiangshan 				*str = c;
30844370aa4aSLai Jiangshan 			++str;
3085fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
30864370aa4aSLai Jiangshan 				if (str < end)
30874370aa4aSLai Jiangshan 					*str = ' ';
30884370aa4aSLai Jiangshan 				++str;
30894370aa4aSLai Jiangshan 			}
3090fef20d9cSFrederic Weisbecker 			break;
3091d4be151bSAndré Goddard Rosa 		}
30924370aa4aSLai Jiangshan 
3093fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
30944370aa4aSLai Jiangshan 			const char *str_arg = args;
3095d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
3096fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
3097fef20d9cSFrederic Weisbecker 			break;
30984370aa4aSLai Jiangshan 		}
30994370aa4aSLai Jiangshan 
3100841a915dSSteven Rostedt (VMware) 		case FORMAT_TYPE_PTR: {
3101841a915dSSteven Rostedt (VMware) 			bool process = false;
3102841a915dSSteven Rostedt (VMware) 			int copy, len;
3103841a915dSSteven Rostedt (VMware) 			/* Non function dereferences were already done */
3104841a915dSSteven Rostedt (VMware) 			switch (*fmt) {
3105841a915dSSteven Rostedt (VMware) 			case 'S':
3106841a915dSSteven Rostedt (VMware) 			case 's':
3107841a915dSSteven Rostedt (VMware) 			case 'F':
3108841a915dSSteven Rostedt (VMware) 			case 'f':
31091e6338cfSSteven Rostedt (VMware) 			case 'x':
31101e6338cfSSteven Rostedt (VMware) 			case 'K':
311157f5677eSRasmus Villemoes 			case 'e':
3112841a915dSSteven Rostedt (VMware) 				process = true;
3113841a915dSSteven Rostedt (VMware) 				break;
3114841a915dSSteven Rostedt (VMware) 			default:
3115841a915dSSteven Rostedt (VMware) 				if (!isalnum(*fmt)) {
3116841a915dSSteven Rostedt (VMware) 					process = true;
3117841a915dSSteven Rostedt (VMware) 					break;
3118841a915dSSteven Rostedt (VMware) 				}
3119841a915dSSteven Rostedt (VMware) 				/* Pointer dereference was already processed */
3120841a915dSSteven Rostedt (VMware) 				if (str < end) {
3121841a915dSSteven Rostedt (VMware) 					len = copy = strlen(args);
3122841a915dSSteven Rostedt (VMware) 					if (copy > end - str)
3123841a915dSSteven Rostedt (VMware) 						copy = end - str;
3124841a915dSSteven Rostedt (VMware) 					memcpy(str, args, copy);
3125841a915dSSteven Rostedt (VMware) 					str += len;
312662165600SSteven Rostedt (VMware) 					args += len + 1;
3127841a915dSSteven Rostedt (VMware) 				}
3128841a915dSSteven Rostedt (VMware) 			}
3129841a915dSSteven Rostedt (VMware) 			if (process)
3130ffbfed03SRasmus Villemoes 				str = pointer(fmt, str, end, get_arg(void *), spec);
3131841a915dSSteven Rostedt (VMware) 
3132fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
31334370aa4aSLai Jiangshan 				fmt++;
3134fef20d9cSFrederic Weisbecker 			break;
3135841a915dSSteven Rostedt (VMware) 		}
31364370aa4aSLai Jiangshan 
3137fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
31384370aa4aSLai Jiangshan 			if (str < end)
31394370aa4aSLai Jiangshan 				*str = '%';
31404370aa4aSLai Jiangshan 			++str;
3141fef20d9cSFrederic Weisbecker 			break;
3142fef20d9cSFrederic Weisbecker 
3143b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
3144b006f19bSRasmus Villemoes 			goto out;
3145b006f19bSRasmus Villemoes 
3146d4be151bSAndré Goddard Rosa 		default: {
3147d4be151bSAndré Goddard Rosa 			unsigned long long num;
3148d4be151bSAndré Goddard Rosa 
3149fef20d9cSFrederic Weisbecker 			switch (spec.type) {
3150fef20d9cSFrederic Weisbecker 
3151fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
31524370aa4aSLai Jiangshan 				num = get_arg(long long);
3153fef20d9cSFrederic Weisbecker 				break;
3154fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
3155fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
3156fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
3157fef20d9cSFrederic Weisbecker 				break;
3158fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
31594370aa4aSLai Jiangshan 				num = get_arg(size_t);
3160fef20d9cSFrederic Weisbecker 				break;
3161fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
31624370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
3163fef20d9cSFrederic Weisbecker 				break;
3164a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
3165a4e94ef0SZhaolei 				num = get_arg(unsigned char);
3166a4e94ef0SZhaolei 				break;
3167a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
3168a4e94ef0SZhaolei 				num = get_arg(signed char);
3169a4e94ef0SZhaolei 				break;
3170fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
3171fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
3172fef20d9cSFrederic Weisbecker 				break;
3173fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
3174fef20d9cSFrederic Weisbecker 				num = get_arg(short);
3175fef20d9cSFrederic Weisbecker 				break;
3176fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
31774370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
3178fef20d9cSFrederic Weisbecker 				break;
3179fef20d9cSFrederic Weisbecker 			default:
3180fef20d9cSFrederic Weisbecker 				num = get_arg(int);
31814370aa4aSLai Jiangshan 			}
3182fef20d9cSFrederic Weisbecker 
3183fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
3184d4be151bSAndré Goddard Rosa 		} /* default: */
3185d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
3186d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
3187fef20d9cSFrederic Weisbecker 
3188b006f19bSRasmus Villemoes out:
31894370aa4aSLai Jiangshan 	if (size > 0) {
31904370aa4aSLai Jiangshan 		if (str < end)
31914370aa4aSLai Jiangshan 			*str = '\0';
31924370aa4aSLai Jiangshan 		else
31934370aa4aSLai Jiangshan 			end[-1] = '\0';
31944370aa4aSLai Jiangshan 	}
3195fef20d9cSFrederic Weisbecker 
31964370aa4aSLai Jiangshan #undef get_arg
31974370aa4aSLai Jiangshan 
31984370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
31994370aa4aSLai Jiangshan 	return str - buf;
32004370aa4aSLai Jiangshan }
32014370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
32024370aa4aSLai Jiangshan 
32034370aa4aSLai Jiangshan /**
32044370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
32054370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
32064370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
32074370aa4aSLai Jiangshan  * @fmt: The format string to use
32084370aa4aSLai Jiangshan  * @...: Arguments for the format string
32094370aa4aSLai Jiangshan  *
32104370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
32114370aa4aSLai Jiangshan  * into @bin_buf.
32124370aa4aSLai Jiangshan  */
32134370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
32144370aa4aSLai Jiangshan {
32154370aa4aSLai Jiangshan 	va_list args;
32164370aa4aSLai Jiangshan 	int ret;
32174370aa4aSLai Jiangshan 
32184370aa4aSLai Jiangshan 	va_start(args, fmt);
32194370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
32204370aa4aSLai Jiangshan 	va_end(args);
32217b9186f5SAndré Goddard Rosa 
32224370aa4aSLai Jiangshan 	return ret;
32234370aa4aSLai Jiangshan }
32244370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
32254370aa4aSLai Jiangshan 
32264370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
32274370aa4aSLai Jiangshan 
32281da177e4SLinus Torvalds /**
32291da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
32301da177e4SLinus Torvalds  * @buf:	input buffer
32311da177e4SLinus Torvalds  * @fmt:	format of buffer
32321da177e4SLinus Torvalds  * @args:	arguments
32331da177e4SLinus Torvalds  */
32341da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
32351da177e4SLinus Torvalds {
32361da177e4SLinus Torvalds 	const char *str = buf;
32371da177e4SLinus Torvalds 	char *next;
32381da177e4SLinus Torvalds 	char digit;
32391da177e4SLinus Torvalds 	int num = 0;
3240ef0658f3SJoe Perches 	u8 qualifier;
324153809751SJan Beulich 	unsigned int base;
324253809751SJan Beulich 	union {
324353809751SJan Beulich 		long long s;
324453809751SJan Beulich 		unsigned long long u;
324553809751SJan Beulich 	} val;
3246ef0658f3SJoe Perches 	s16 field_width;
3247d4be151bSAndré Goddard Rosa 	bool is_sign;
32481da177e4SLinus Torvalds 
3249da99075cSJan Beulich 	while (*fmt) {
32501da177e4SLinus Torvalds 		/* skip any white space in format */
32511da177e4SLinus Torvalds 		/* white space in format matchs any amount of
32521da177e4SLinus Torvalds 		 * white space, including none, in the input.
32531da177e4SLinus Torvalds 		 */
32541da177e4SLinus Torvalds 		if (isspace(*fmt)) {
3255e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
3256e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
32571da177e4SLinus Torvalds 		}
32581da177e4SLinus Torvalds 
32591da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
32601da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
32611da177e4SLinus Torvalds 			if (*fmt++ != *str++)
32621da177e4SLinus Torvalds 				break;
32631da177e4SLinus Torvalds 			continue;
32641da177e4SLinus Torvalds 		}
32651da177e4SLinus Torvalds 
32661da177e4SLinus Torvalds 		if (!*fmt)
32671da177e4SLinus Torvalds 			break;
32681da177e4SLinus Torvalds 		++fmt;
32691da177e4SLinus Torvalds 
32701da177e4SLinus Torvalds 		/* skip this conversion.
32711da177e4SLinus Torvalds 		 * advance both strings to next white space
32721da177e4SLinus Torvalds 		 */
32731da177e4SLinus Torvalds 		if (*fmt == '*') {
3274da99075cSJan Beulich 			if (!*str)
3275da99075cSJan Beulich 				break;
3276f9310b2fSJessica Yu 			while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3277f9310b2fSJessica Yu 				/* '%*[' not yet supported, invalid format */
3278f9310b2fSJessica Yu 				if (*fmt == '[')
3279f9310b2fSJessica Yu 					return num;
32801da177e4SLinus Torvalds 				fmt++;
3281f9310b2fSJessica Yu 			}
32821da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
32831da177e4SLinus Torvalds 				str++;
32841da177e4SLinus Torvalds 			continue;
32851da177e4SLinus Torvalds 		}
32861da177e4SLinus Torvalds 
32871da177e4SLinus Torvalds 		/* get field width */
32881da177e4SLinus Torvalds 		field_width = -1;
328953809751SJan Beulich 		if (isdigit(*fmt)) {
32901da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
329153809751SJan Beulich 			if (field_width <= 0)
329253809751SJan Beulich 				break;
329353809751SJan Beulich 		}
32941da177e4SLinus Torvalds 
32951da177e4SLinus Torvalds 		/* get conversion qualifier */
32961da177e4SLinus Torvalds 		qualifier = -1;
329775fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
32985b5e0928SAlexey Dobriyan 		    *fmt == 'z') {
32991da177e4SLinus Torvalds 			qualifier = *fmt++;
33001da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
33011da177e4SLinus Torvalds 				if (qualifier == 'h') {
33021da177e4SLinus Torvalds 					qualifier = 'H';
33031da177e4SLinus Torvalds 					fmt++;
33041da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
33051da177e4SLinus Torvalds 					qualifier = 'L';
33061da177e4SLinus Torvalds 					fmt++;
33071da177e4SLinus Torvalds 				}
33081da177e4SLinus Torvalds 			}
33091da177e4SLinus Torvalds 		}
33101da177e4SLinus Torvalds 
3311da99075cSJan Beulich 		if (!*fmt)
3312da99075cSJan Beulich 			break;
3313da99075cSJan Beulich 
3314da99075cSJan Beulich 		if (*fmt == 'n') {
3315da99075cSJan Beulich 			/* return number of characters read so far */
3316da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
3317da99075cSJan Beulich 			++fmt;
3318da99075cSJan Beulich 			continue;
3319da99075cSJan Beulich 		}
3320da99075cSJan Beulich 
3321da99075cSJan Beulich 		if (!*str)
33221da177e4SLinus Torvalds 			break;
33231da177e4SLinus Torvalds 
3324d4be151bSAndré Goddard Rosa 		base = 10;
33253f623ebaSFabian Frederick 		is_sign = false;
3326d4be151bSAndré Goddard Rosa 
33271da177e4SLinus Torvalds 		switch (*fmt++) {
33281da177e4SLinus Torvalds 		case 'c':
33291da177e4SLinus Torvalds 		{
33301da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
33311da177e4SLinus Torvalds 			if (field_width == -1)
33321da177e4SLinus Torvalds 				field_width = 1;
33331da177e4SLinus Torvalds 			do {
33341da177e4SLinus Torvalds 				*s++ = *str++;
33351da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
33361da177e4SLinus Torvalds 			num++;
33371da177e4SLinus Torvalds 		}
33381da177e4SLinus Torvalds 		continue;
33391da177e4SLinus Torvalds 		case 's':
33401da177e4SLinus Torvalds 		{
33411da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
33421da177e4SLinus Torvalds 			if (field_width == -1)
33434be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
33441da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
3345e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
33461da177e4SLinus Torvalds 
33471da177e4SLinus Torvalds 			/* now copy until next white space */
33487b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
33491da177e4SLinus Torvalds 				*s++ = *str++;
33501da177e4SLinus Torvalds 			*s = '\0';
33511da177e4SLinus Torvalds 			num++;
33521da177e4SLinus Torvalds 		}
33531da177e4SLinus Torvalds 		continue;
3354f9310b2fSJessica Yu 		/*
3355f9310b2fSJessica Yu 		 * Warning: This implementation of the '[' conversion specifier
3356f9310b2fSJessica Yu 		 * deviates from its glibc counterpart in the following ways:
3357f9310b2fSJessica Yu 		 * (1) It does NOT support ranges i.e. '-' is NOT a special
3358f9310b2fSJessica Yu 		 *     character
3359f9310b2fSJessica Yu 		 * (2) It cannot match the closing bracket ']' itself
3360f9310b2fSJessica Yu 		 * (3) A field width is required
3361f9310b2fSJessica Yu 		 * (4) '%*[' (discard matching input) is currently not supported
3362f9310b2fSJessica Yu 		 *
3363f9310b2fSJessica Yu 		 * Example usage:
3364f9310b2fSJessica Yu 		 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3365f9310b2fSJessica Yu 		 *		buf1, buf2, buf3);
3366f9310b2fSJessica Yu 		 * if (ret < 3)
3367f9310b2fSJessica Yu 		 *    // etc..
3368f9310b2fSJessica Yu 		 */
3369f9310b2fSJessica Yu 		case '[':
3370f9310b2fSJessica Yu 		{
3371f9310b2fSJessica Yu 			char *s = (char *)va_arg(args, char *);
3372f9310b2fSJessica Yu 			DECLARE_BITMAP(set, 256) = {0};
3373f9310b2fSJessica Yu 			unsigned int len = 0;
3374f9310b2fSJessica Yu 			bool negate = (*fmt == '^');
3375f9310b2fSJessica Yu 
3376f9310b2fSJessica Yu 			/* field width is required */
3377f9310b2fSJessica Yu 			if (field_width == -1)
3378f9310b2fSJessica Yu 				return num;
3379f9310b2fSJessica Yu 
3380f9310b2fSJessica Yu 			if (negate)
3381f9310b2fSJessica Yu 				++fmt;
3382f9310b2fSJessica Yu 
3383f9310b2fSJessica Yu 			for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3384f9310b2fSJessica Yu 				set_bit((u8)*fmt, set);
3385f9310b2fSJessica Yu 
3386f9310b2fSJessica Yu 			/* no ']' or no character set found */
3387f9310b2fSJessica Yu 			if (!*fmt || !len)
3388f9310b2fSJessica Yu 				return num;
3389f9310b2fSJessica Yu 			++fmt;
3390f9310b2fSJessica Yu 
3391f9310b2fSJessica Yu 			if (negate) {
3392f9310b2fSJessica Yu 				bitmap_complement(set, set, 256);
3393f9310b2fSJessica Yu 				/* exclude null '\0' byte */
3394f9310b2fSJessica Yu 				clear_bit(0, set);
3395f9310b2fSJessica Yu 			}
3396f9310b2fSJessica Yu 
3397f9310b2fSJessica Yu 			/* match must be non-empty */
3398f9310b2fSJessica Yu 			if (!test_bit((u8)*str, set))
3399f9310b2fSJessica Yu 				return num;
3400f9310b2fSJessica Yu 
3401f9310b2fSJessica Yu 			while (test_bit((u8)*str, set) && field_width--)
3402f9310b2fSJessica Yu 				*s++ = *str++;
3403f9310b2fSJessica Yu 			*s = '\0';
3404f9310b2fSJessica Yu 			++num;
3405f9310b2fSJessica Yu 		}
3406f9310b2fSJessica Yu 		continue;
34071da177e4SLinus Torvalds 		case 'o':
34081da177e4SLinus Torvalds 			base = 8;
34091da177e4SLinus Torvalds 			break;
34101da177e4SLinus Torvalds 		case 'x':
34111da177e4SLinus Torvalds 		case 'X':
34121da177e4SLinus Torvalds 			base = 16;
34131da177e4SLinus Torvalds 			break;
34141da177e4SLinus Torvalds 		case 'i':
34151da177e4SLinus Torvalds 			base = 0;
34167e6bd6f3SAndy Shevchenko 			/* fall through */
34171da177e4SLinus Torvalds 		case 'd':
34183f623ebaSFabian Frederick 			is_sign = true;
34197e6bd6f3SAndy Shevchenko 			/* fall through */
34201da177e4SLinus Torvalds 		case 'u':
34211da177e4SLinus Torvalds 			break;
34221da177e4SLinus Torvalds 		case '%':
34231da177e4SLinus Torvalds 			/* looking for '%' in str */
34241da177e4SLinus Torvalds 			if (*str++ != '%')
34251da177e4SLinus Torvalds 				return num;
34261da177e4SLinus Torvalds 			continue;
34271da177e4SLinus Torvalds 		default:
34281da177e4SLinus Torvalds 			/* invalid format; stop here */
34291da177e4SLinus Torvalds 			return num;
34301da177e4SLinus Torvalds 		}
34311da177e4SLinus Torvalds 
34321da177e4SLinus Torvalds 		/* have some sort of integer conversion.
34331da177e4SLinus Torvalds 		 * first, skip white space in buffer.
34341da177e4SLinus Torvalds 		 */
3435e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
34361da177e4SLinus Torvalds 
34371da177e4SLinus Torvalds 		digit = *str;
34381da177e4SLinus Torvalds 		if (is_sign && digit == '-')
34391da177e4SLinus Torvalds 			digit = *(str + 1);
34401da177e4SLinus Torvalds 
34411da177e4SLinus Torvalds 		if (!digit
34421da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
34431da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
34441da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
34451da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
34461da177e4SLinus Torvalds 			break;
34471da177e4SLinus Torvalds 
344853809751SJan Beulich 		if (is_sign)
344953809751SJan Beulich 			val.s = qualifier != 'L' ?
345053809751SJan Beulich 				simple_strtol(str, &next, base) :
345153809751SJan Beulich 				simple_strtoll(str, &next, base);
345253809751SJan Beulich 		else
345353809751SJan Beulich 			val.u = qualifier != 'L' ?
345453809751SJan Beulich 				simple_strtoul(str, &next, base) :
345553809751SJan Beulich 				simple_strtoull(str, &next, base);
345653809751SJan Beulich 
345753809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
345853809751SJan Beulich 			if (base == 0)
345953809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
346053809751SJan Beulich 			while (next - str > field_width) {
346153809751SJan Beulich 				if (is_sign)
346253809751SJan Beulich 					val.s = div_s64(val.s, base);
346353809751SJan Beulich 				else
346453809751SJan Beulich 					val.u = div_u64(val.u, base);
346553809751SJan Beulich 				--next;
346653809751SJan Beulich 			}
346753809751SJan Beulich 		}
346853809751SJan Beulich 
34691da177e4SLinus Torvalds 		switch (qualifier) {
34701da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
347153809751SJan Beulich 			if (is_sign)
347253809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
347353809751SJan Beulich 			else
347453809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
34751da177e4SLinus Torvalds 			break;
34761da177e4SLinus Torvalds 		case 'h':
347753809751SJan Beulich 			if (is_sign)
347853809751SJan Beulich 				*va_arg(args, short *) = val.s;
347953809751SJan Beulich 			else
348053809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
34811da177e4SLinus Torvalds 			break;
34821da177e4SLinus Torvalds 		case 'l':
348353809751SJan Beulich 			if (is_sign)
348453809751SJan Beulich 				*va_arg(args, long *) = val.s;
348553809751SJan Beulich 			else
348653809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
34871da177e4SLinus Torvalds 			break;
34881da177e4SLinus Torvalds 		case 'L':
348953809751SJan Beulich 			if (is_sign)
349053809751SJan Beulich 				*va_arg(args, long long *) = val.s;
349153809751SJan Beulich 			else
349253809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
34931da177e4SLinus Torvalds 			break;
34941da177e4SLinus Torvalds 		case 'z':
349553809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
34961da177e4SLinus Torvalds 			break;
34971da177e4SLinus Torvalds 		default:
349853809751SJan Beulich 			if (is_sign)
349953809751SJan Beulich 				*va_arg(args, int *) = val.s;
350053809751SJan Beulich 			else
350153809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
35021da177e4SLinus Torvalds 			break;
35031da177e4SLinus Torvalds 		}
35041da177e4SLinus Torvalds 		num++;
35051da177e4SLinus Torvalds 
35061da177e4SLinus Torvalds 		if (!next)
35071da177e4SLinus Torvalds 			break;
35081da177e4SLinus Torvalds 		str = next;
35091da177e4SLinus Torvalds 	}
3510c6b40d16SJohannes Berg 
35111da177e4SLinus Torvalds 	return num;
35121da177e4SLinus Torvalds }
35131da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
35141da177e4SLinus Torvalds 
35151da177e4SLinus Torvalds /**
35161da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
35171da177e4SLinus Torvalds  * @buf:	input buffer
35181da177e4SLinus Torvalds  * @fmt:	formatting of buffer
35191da177e4SLinus Torvalds  * @...:	resulting arguments
35201da177e4SLinus Torvalds  */
35211da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
35221da177e4SLinus Torvalds {
35231da177e4SLinus Torvalds 	va_list args;
35241da177e4SLinus Torvalds 	int i;
35251da177e4SLinus Torvalds 
35261da177e4SLinus Torvalds 	va_start(args, fmt);
35271da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
35281da177e4SLinus Torvalds 	va_end(args);
35297b9186f5SAndré Goddard Rosa 
35301da177e4SLinus Torvalds 	return i;
35311da177e4SLinus Torvalds }
35321da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
3533