xref: /openbmc/linux/lib/vsprintf.c (revision 7daac5b2fdf88e3c3e84cf0d577f524beb0244ab)
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>
37*7daac5b2SAndy 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  *
62462e4711SEldad Zack  * This function is obsolete. 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  *
87462e4711SEldad Zack  * This function is obsolete. 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  *
101462e4711SEldad Zack  * This function is obsolete. 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  *
118462e4711SEldad Zack  * This function is obsolete. 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) 
798e4dcad20SJoel Fernandes (Google) 	/* When debugging early boot use non-cryptographically secure hash. */
799e4dcad20SJoel Fernandes (Google) 	if (unlikely(debug_boot_weak_hash)) {
800e4dcad20SJoel Fernandes (Google) 		hashval = hash_long((unsigned long)ptr, 32);
801e4dcad20SJoel Fernandes (Google) 		return pointer_string(buf, end, (const void *)hashval, spec);
802e4dcad20SJoel Fernandes (Google) 	}
803e4dcad20SJoel Fernandes (Google) 
804e4dcad20SJoel Fernandes (Google) 	ret = __ptr_to_hashval(ptr, &hashval);
805e4dcad20SJoel Fernandes (Google) 	if (ret) {
806e4dcad20SJoel Fernandes (Google) 		spec.field_width = 2 * sizeof(ptr);
807e4dcad20SJoel Fernandes (Google) 		/* string length must be less than default_width */
808e4dcad20SJoel Fernandes (Google) 		return error_string(buf, end, str, spec);
809e4dcad20SJoel Fernandes (Google) 	}
810e4dcad20SJoel Fernandes (Google) 
8119073dac1SGeert Uytterhoeven 	return pointer_string(buf, end, (const void *)hashval, spec);
8129073dac1SGeert Uytterhoeven }
8139073dac1SGeert Uytterhoeven 
8146eea242fSPetr Mladek int kptr_restrict __read_mostly;
8156eea242fSPetr Mladek 
8166eea242fSPetr Mladek static noinline_for_stack
8176eea242fSPetr Mladek char *restricted_pointer(char *buf, char *end, const void *ptr,
8186eea242fSPetr Mladek 			 struct printf_spec spec)
8196eea242fSPetr Mladek {
8206eea242fSPetr Mladek 	switch (kptr_restrict) {
8216eea242fSPetr Mladek 	case 0:
8221ac2f978SPetr Mladek 		/* Handle as %p, hash and do _not_ leak addresses. */
8231ac2f978SPetr Mladek 		return ptr_to_id(buf, end, ptr, spec);
8246eea242fSPetr Mladek 	case 1: {
8256eea242fSPetr Mladek 		const struct cred *cred;
8266eea242fSPetr Mladek 
8276eea242fSPetr Mladek 		/*
8286eea242fSPetr Mladek 		 * kptr_restrict==1 cannot be used in IRQ context
8296eea242fSPetr Mladek 		 * because its test for CAP_SYSLOG would be meaningless.
8306eea242fSPetr Mladek 		 */
8316eea242fSPetr Mladek 		if (in_irq() || in_serving_softirq() || in_nmi()) {
8326eea242fSPetr Mladek 			if (spec.field_width == -1)
8336eea242fSPetr Mladek 				spec.field_width = 2 * sizeof(ptr);
834c8c3b584SPetr Mladek 			return error_string(buf, end, "pK-error", spec);
8356eea242fSPetr Mladek 		}
8366eea242fSPetr Mladek 
8376eea242fSPetr Mladek 		/*
8386eea242fSPetr Mladek 		 * Only print the real pointer value if the current
8396eea242fSPetr Mladek 		 * process has CAP_SYSLOG and is running with the
8406eea242fSPetr Mladek 		 * same credentials it started with. This is because
8416eea242fSPetr Mladek 		 * access to files is checked at open() time, but %pK
8426eea242fSPetr Mladek 		 * checks permission at read() time. We don't want to
8436eea242fSPetr Mladek 		 * leak pointer values if a binary opens a file using
8446eea242fSPetr Mladek 		 * %pK and then elevates privileges before reading it.
8456eea242fSPetr Mladek 		 */
8466eea242fSPetr Mladek 		cred = current_cred();
8476eea242fSPetr Mladek 		if (!has_capability_noaudit(current, CAP_SYSLOG) ||
8486eea242fSPetr Mladek 		    !uid_eq(cred->euid, cred->uid) ||
8496eea242fSPetr Mladek 		    !gid_eq(cred->egid, cred->gid))
8506eea242fSPetr Mladek 			ptr = NULL;
8516eea242fSPetr Mladek 		break;
8526eea242fSPetr Mladek 	}
8536eea242fSPetr Mladek 	case 2:
8546eea242fSPetr Mladek 	default:
8556eea242fSPetr Mladek 		/* Always print 0's for %pK */
8566eea242fSPetr Mladek 		ptr = NULL;
8576eea242fSPetr Mladek 		break;
8586eea242fSPetr Mladek 	}
8596eea242fSPetr Mladek 
8606eea242fSPetr Mladek 	return pointer_string(buf, end, ptr, spec);
8616eea242fSPetr Mladek }
8626eea242fSPetr Mladek 
8639073dac1SGeert Uytterhoeven static noinline_for_stack
8644b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
8654b6ccca7SAl Viro 		  const char *fmt)
8664b6ccca7SAl Viro {
8674b6ccca7SAl Viro 	const char *array[4], *s;
8684b6ccca7SAl Viro 	const struct dentry *p;
8694b6ccca7SAl Viro 	int depth;
8704b6ccca7SAl Viro 	int i, n;
8714b6ccca7SAl Viro 
8724b6ccca7SAl Viro 	switch (fmt[1]) {
8734b6ccca7SAl Viro 		case '2': case '3': case '4':
8744b6ccca7SAl Viro 			depth = fmt[1] - '0';
8754b6ccca7SAl Viro 			break;
8764b6ccca7SAl Viro 		default:
8774b6ccca7SAl Viro 			depth = 1;
8784b6ccca7SAl Viro 	}
8794b6ccca7SAl Viro 
8804b6ccca7SAl Viro 	rcu_read_lock();
8814b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
8823e5903ebSPetr Mladek 		if (check_pointer(&buf, end, d, spec)) {
8833e5903ebSPetr Mladek 			rcu_read_unlock();
8843e5903ebSPetr Mladek 			return buf;
8853e5903ebSPetr Mladek 		}
8863e5903ebSPetr Mladek 
8876aa7de05SMark Rutland 		p = READ_ONCE(d->d_parent);
8886aa7de05SMark Rutland 		array[i] = READ_ONCE(d->d_name.name);
8894b6ccca7SAl Viro 		if (p == d) {
8904b6ccca7SAl Viro 			if (i)
8914b6ccca7SAl Viro 				array[i] = "";
8924b6ccca7SAl Viro 			i++;
8934b6ccca7SAl Viro 			break;
8944b6ccca7SAl Viro 		}
8954b6ccca7SAl Viro 	}
8964b6ccca7SAl Viro 	s = array[--i];
8974b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
8984b6ccca7SAl Viro 		char c = *s++;
8994b6ccca7SAl Viro 		if (!c) {
9004b6ccca7SAl Viro 			if (!i)
9014b6ccca7SAl Viro 				break;
9024b6ccca7SAl Viro 			c = '/';
9034b6ccca7SAl Viro 			s = array[--i];
9044b6ccca7SAl Viro 		}
9054b6ccca7SAl Viro 		if (buf < end)
9064b6ccca7SAl Viro 			*buf = c;
9074b6ccca7SAl Viro 	}
9084b6ccca7SAl Viro 	rcu_read_unlock();
909cfccde04SRasmus Villemoes 	return widen_string(buf, n, end, spec);
9104b6ccca7SAl Viro }
9114b6ccca7SAl Viro 
91236594b31SJia He static noinline_for_stack
91336594b31SJia He char *file_dentry_name(char *buf, char *end, const struct file *f,
91436594b31SJia He 			struct printf_spec spec, const char *fmt)
91536594b31SJia He {
91636594b31SJia He 	if (check_pointer(&buf, end, f, spec))
91736594b31SJia He 		return buf;
91836594b31SJia He 
91936594b31SJia He 	return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
92036594b31SJia He }
9211031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
9221031bc58SDmitry Monakhov static noinline_for_stack
9231031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev,
9241031bc58SDmitry Monakhov 		struct printf_spec spec, const char *fmt)
9251031bc58SDmitry Monakhov {
9263e5903ebSPetr Mladek 	struct gendisk *hd;
9271031bc58SDmitry Monakhov 
9283e5903ebSPetr Mladek 	if (check_pointer(&buf, end, bdev, spec))
9293e5903ebSPetr Mladek 		return buf;
9303e5903ebSPetr Mladek 
9313e5903ebSPetr Mladek 	hd = bdev->bd_disk;
9321031bc58SDmitry Monakhov 	buf = string(buf, end, hd->disk_name, spec);
9331031bc58SDmitry Monakhov 	if (bdev->bd_part->partno) {
9341031bc58SDmitry Monakhov 		if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
9351031bc58SDmitry Monakhov 			if (buf < end)
9361031bc58SDmitry Monakhov 				*buf = 'p';
9371031bc58SDmitry Monakhov 			buf++;
9381031bc58SDmitry Monakhov 		}
9391031bc58SDmitry Monakhov 		buf = number(buf, end, bdev->bd_part->partno, spec);
9401031bc58SDmitry Monakhov 	}
9411031bc58SDmitry Monakhov 	return buf;
9421031bc58SDmitry Monakhov }
9431031bc58SDmitry Monakhov #endif
9441031bc58SDmitry Monakhov 
945cf3b429bSJoe Perches static noinline_for_stack
946cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
947b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
9480fe1ef24SLinus Torvalds {
949b0d33c2bSJoe Perches 	unsigned long value;
9500fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
9510fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
952b0d33c2bSJoe Perches #endif
953b0d33c2bSJoe Perches 
954b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
955b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
956b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
957b0d33c2bSJoe Perches 
958b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
959b0d33c2bSJoe Perches 	if (*fmt == 'B')
9600f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
9619af77064SSakari Ailus 	else if (*fmt != 's')
9620fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
9630c8b946eSFrederic Weisbecker 	else
9644796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
9657b9186f5SAndré Goddard Rosa 
966d529ac41SPetr Mladek 	return string_nocheck(buf, end, sym, spec);
9670fe1ef24SLinus Torvalds #else
9683cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, value, sizeof(void *));
9690fe1ef24SLinus Torvalds #endif
9700fe1ef24SLinus Torvalds }
9710fe1ef24SLinus Torvalds 
972abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = {
973abd4fe62SAndy Shevchenko 	.field_width = -1,
974abd4fe62SAndy Shevchenko 	.precision = -1,
975abd4fe62SAndy Shevchenko };
976abd4fe62SAndy Shevchenko 
97754433973SAndy Shevchenko static const struct printf_spec default_flag_spec = {
97854433973SAndy Shevchenko 	.base = 16,
97954433973SAndy Shevchenko 	.precision = -1,
98054433973SAndy Shevchenko 	.flags = SPECIAL | SMALL,
98154433973SAndy Shevchenko };
98254433973SAndy Shevchenko 
983ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = {
984ce0b4910SAndy Shevchenko 	.base = 10,
985ce0b4910SAndy Shevchenko 	.precision = -1,
986ce0b4910SAndy Shevchenko };
987ce0b4910SAndy Shevchenko 
9884d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = {
9894d42c447SAndy Shevchenko 	.base = 10,
9904d42c447SAndy Shevchenko 	.field_width = 2,
9914d42c447SAndy Shevchenko 	.precision = -1,
9924d42c447SAndy Shevchenko 	.flags = ZEROPAD,
9934d42c447SAndy Shevchenko };
9944d42c447SAndy Shevchenko 
9954d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = {
9964d42c447SAndy Shevchenko 	.base = 10,
9974d42c447SAndy Shevchenko 	.field_width = 4,
9984d42c447SAndy Shevchenko 	.precision = -1,
9994d42c447SAndy Shevchenko 	.flags = ZEROPAD,
10004d42c447SAndy Shevchenko };
10014d42c447SAndy Shevchenko 
1002cf3b429bSJoe Perches static noinline_for_stack
1003cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
1004fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
1005332d2e78SLinus Torvalds {
1006332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
100728405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
1008332d2e78SLinus Torvalds #endif
1009332d2e78SLinus Torvalds 
1010332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
101128405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
1012332d2e78SLinus Torvalds #endif
10134da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
1014fef20d9cSFrederic Weisbecker 		.base = 16,
10154da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
1016fef20d9cSFrederic Weisbecker 		.precision = -1,
1017fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
1018fef20d9cSFrederic Weisbecker 	};
10194da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
10204da0b66cSBjorn Helgaas 		.base = 16,
10214da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
10224da0b66cSBjorn Helgaas 		.precision = -1,
10234da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
10244da0b66cSBjorn Helgaas 	};
10250f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
10260f4050c7SBjorn Helgaas 		.base = 16,
10270f4050c7SBjorn Helgaas 		.field_width = 2,
10280f4050c7SBjorn Helgaas 		.precision = -1,
10290f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
10300f4050c7SBjorn Helgaas 	};
10314da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
1032fd95541eSBjorn Helgaas 		.field_width = -1,
1033fd95541eSBjorn Helgaas 		.precision = 10,
1034fd95541eSBjorn Helgaas 		.flags = LEFT,
1035fd95541eSBjorn Helgaas 	};
1036c7dabef8SBjorn Helgaas 
1037c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1038c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1039c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
1040c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
10419d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
1042c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
1043c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1044c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1045c7dabef8SBjorn Helgaas 
1046332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
1047c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
10484da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
1049332d2e78SLinus Torvalds 
10503e5903ebSPetr Mladek 	if (check_pointer(&buf, end, res, spec))
10513e5903ebSPetr Mladek 		return buf;
10523e5903ebSPetr Mladek 
1053332d2e78SLinus Torvalds 	*p++ = '[';
10544da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
1055d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "io  ", str_spec);
10564da0b66cSBjorn Helgaas 		specp = &io_spec;
10574da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
1058d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "mem ", str_spec);
10594da0b66cSBjorn Helgaas 		specp = &mem_spec;
10604da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
1061d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "irq ", str_spec);
1062ce0b4910SAndy Shevchenko 		specp = &default_dec_spec;
10634da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
1064d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "dma ", str_spec);
1065ce0b4910SAndy Shevchenko 		specp = &default_dec_spec;
10660f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
1067d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "bus ", str_spec);
10680f4050c7SBjorn Helgaas 		specp = &bus_spec;
10694da0b66cSBjorn Helgaas 	} else {
1070d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "??? ", str_spec);
10714da0b66cSBjorn Helgaas 		specp = &mem_spec;
1072c7dabef8SBjorn Helgaas 		decode = 0;
1073fd95541eSBjorn Helgaas 	}
1074d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
1075d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "size ", str_spec);
1076d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
1077d19cb803SBjorn Helgaas 	} else {
10784da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
1079c91d3376SBjorn Helgaas 		if (res->start != res->end) {
1080332d2e78SLinus Torvalds 			*p++ = '-';
10814da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
1082c91d3376SBjorn Helgaas 		}
1083d19cb803SBjorn Helgaas 	}
1084c7dabef8SBjorn Helgaas 	if (decode) {
1085fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
1086d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " 64bit", str_spec);
1087fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
1088d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " pref", str_spec);
10899d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
1090d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " window", str_spec);
1091fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
1092d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " disabled", str_spec);
1093c7dabef8SBjorn Helgaas 	} else {
1094d529ac41SPetr Mladek 		p = string_nocheck(p, pend, " flags ", str_spec);
109554433973SAndy Shevchenko 		p = number(p, pend, res->flags, default_flag_spec);
1096fd95541eSBjorn Helgaas 	}
1097332d2e78SLinus Torvalds 	*p++ = ']';
1098c7dabef8SBjorn Helgaas 	*p = '\0';
1099332d2e78SLinus Torvalds 
1100d529ac41SPetr Mladek 	return string_nocheck(buf, end, sym, spec);
1101332d2e78SLinus Torvalds }
1102332d2e78SLinus Torvalds 
1103cf3b429bSJoe Perches static noinline_for_stack
110431550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
110531550a16SAndy Shevchenko 		 const char *fmt)
110631550a16SAndy Shevchenko {
1107360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
110831550a16SAndy Shevchenko 				   negative value, fallback to the default */
110931550a16SAndy Shevchenko 	char separator;
111031550a16SAndy Shevchenko 
111131550a16SAndy Shevchenko 	if (spec.field_width == 0)
111231550a16SAndy Shevchenko 		/* nothing to print */
111331550a16SAndy Shevchenko 		return buf;
111431550a16SAndy Shevchenko 
11153e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
11163e5903ebSPetr Mladek 		return buf;
111731550a16SAndy Shevchenko 
111831550a16SAndy Shevchenko 	switch (fmt[1]) {
111931550a16SAndy Shevchenko 	case 'C':
112031550a16SAndy Shevchenko 		separator = ':';
112131550a16SAndy Shevchenko 		break;
112231550a16SAndy Shevchenko 	case 'D':
112331550a16SAndy Shevchenko 		separator = '-';
112431550a16SAndy Shevchenko 		break;
112531550a16SAndy Shevchenko 	case 'N':
112631550a16SAndy Shevchenko 		separator = 0;
112731550a16SAndy Shevchenko 		break;
112831550a16SAndy Shevchenko 	default:
112931550a16SAndy Shevchenko 		separator = ' ';
113031550a16SAndy Shevchenko 		break;
113131550a16SAndy Shevchenko 	}
113231550a16SAndy Shevchenko 
113331550a16SAndy Shevchenko 	if (spec.field_width > 0)
113431550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
113531550a16SAndy Shevchenko 
11369c98f235SRasmus Villemoes 	for (i = 0; i < len; ++i) {
11379c98f235SRasmus Villemoes 		if (buf < end)
11389c98f235SRasmus Villemoes 			*buf = hex_asc_hi(addr[i]);
11399c98f235SRasmus Villemoes 		++buf;
11409c98f235SRasmus Villemoes 		if (buf < end)
11419c98f235SRasmus Villemoes 			*buf = hex_asc_lo(addr[i]);
11429c98f235SRasmus Villemoes 		++buf;
114331550a16SAndy Shevchenko 
11449c98f235SRasmus Villemoes 		if (separator && i != len - 1) {
11459c98f235SRasmus Villemoes 			if (buf < end)
11469c98f235SRasmus Villemoes 				*buf = separator;
11479c98f235SRasmus Villemoes 			++buf;
11489c98f235SRasmus Villemoes 		}
114931550a16SAndy Shevchenko 	}
115031550a16SAndy Shevchenko 
115131550a16SAndy Shevchenko 	return buf;
115231550a16SAndy Shevchenko }
115331550a16SAndy Shevchenko 
115431550a16SAndy Shevchenko static noinline_for_stack
1155dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1156dbc760bcSTejun Heo 		    struct printf_spec spec, const char *fmt)
1157dbc760bcSTejun Heo {
1158dbc760bcSTejun Heo 	const int CHUNKSZ = 32;
1159dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
1160dbc760bcSTejun Heo 	int i, chunksz;
1161dbc760bcSTejun Heo 	bool first = true;
1162dbc760bcSTejun Heo 
11633e5903ebSPetr Mladek 	if (check_pointer(&buf, end, bitmap, spec))
11643e5903ebSPetr Mladek 		return buf;
11653e5903ebSPetr Mladek 
1166dbc760bcSTejun Heo 	/* reused to print numbers */
1167dbc760bcSTejun Heo 	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1168dbc760bcSTejun Heo 
1169dbc760bcSTejun Heo 	chunksz = nr_bits & (CHUNKSZ - 1);
1170dbc760bcSTejun Heo 	if (chunksz == 0)
1171dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
1172dbc760bcSTejun Heo 
1173dbc760bcSTejun Heo 	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1174dbc760bcSTejun Heo 	for (; i >= 0; i -= CHUNKSZ) {
1175dbc760bcSTejun Heo 		u32 chunkmask, val;
1176dbc760bcSTejun Heo 		int word, bit;
1177dbc760bcSTejun Heo 
1178dbc760bcSTejun Heo 		chunkmask = ((1ULL << chunksz) - 1);
1179dbc760bcSTejun Heo 		word = i / BITS_PER_LONG;
1180dbc760bcSTejun Heo 		bit = i % BITS_PER_LONG;
1181dbc760bcSTejun Heo 		val = (bitmap[word] >> bit) & chunkmask;
1182dbc760bcSTejun Heo 
1183dbc760bcSTejun Heo 		if (!first) {
1184dbc760bcSTejun Heo 			if (buf < end)
1185dbc760bcSTejun Heo 				*buf = ',';
1186dbc760bcSTejun Heo 			buf++;
1187dbc760bcSTejun Heo 		}
1188dbc760bcSTejun Heo 		first = false;
1189dbc760bcSTejun Heo 
1190dbc760bcSTejun Heo 		spec.field_width = DIV_ROUND_UP(chunksz, 4);
1191dbc760bcSTejun Heo 		buf = number(buf, end, val, spec);
1192dbc760bcSTejun Heo 
1193dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
1194dbc760bcSTejun Heo 	}
1195dbc760bcSTejun Heo 	return buf;
1196dbc760bcSTejun Heo }
1197dbc760bcSTejun Heo 
1198dbc760bcSTejun Heo static noinline_for_stack
1199dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1200dbc760bcSTejun Heo 			 struct printf_spec spec, const char *fmt)
1201dbc760bcSTejun Heo {
1202dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
1203dbc760bcSTejun Heo 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
1204dbc760bcSTejun Heo 	int cur, rbot, rtop;
1205dbc760bcSTejun Heo 	bool first = true;
1206dbc760bcSTejun Heo 
12073e5903ebSPetr Mladek 	if (check_pointer(&buf, end, bitmap, spec))
12083e5903ebSPetr Mladek 		return buf;
12093e5903ebSPetr Mladek 
1210dbc760bcSTejun Heo 	rbot = cur = find_first_bit(bitmap, nr_bits);
1211dbc760bcSTejun Heo 	while (cur < nr_bits) {
1212dbc760bcSTejun Heo 		rtop = cur;
1213dbc760bcSTejun Heo 		cur = find_next_bit(bitmap, nr_bits, cur + 1);
1214dbc760bcSTejun Heo 		if (cur < nr_bits && cur <= rtop + 1)
1215dbc760bcSTejun Heo 			continue;
1216dbc760bcSTejun Heo 
1217dbc760bcSTejun Heo 		if (!first) {
1218dbc760bcSTejun Heo 			if (buf < end)
1219dbc760bcSTejun Heo 				*buf = ',';
1220dbc760bcSTejun Heo 			buf++;
1221dbc760bcSTejun Heo 		}
1222dbc760bcSTejun Heo 		first = false;
1223dbc760bcSTejun Heo 
1224ce0b4910SAndy Shevchenko 		buf = number(buf, end, rbot, default_dec_spec);
1225dbc760bcSTejun Heo 		if (rbot < rtop) {
1226dbc760bcSTejun Heo 			if (buf < end)
1227dbc760bcSTejun Heo 				*buf = '-';
1228dbc760bcSTejun Heo 			buf++;
1229dbc760bcSTejun Heo 
1230ce0b4910SAndy Shevchenko 			buf = number(buf, end, rtop, default_dec_spec);
1231dbc760bcSTejun Heo 		}
1232dbc760bcSTejun Heo 
1233dbc760bcSTejun Heo 		rbot = cur;
1234dbc760bcSTejun Heo 	}
1235dbc760bcSTejun Heo 	return buf;
1236dbc760bcSTejun Heo }
1237dbc760bcSTejun Heo 
1238dbc760bcSTejun Heo static noinline_for_stack
1239cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
12408a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
1241dd45c9cfSHarvey Harrison {
12428a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
1243dd45c9cfSHarvey Harrison 	char *p = mac_addr;
1244dd45c9cfSHarvey Harrison 	int i;
1245bc7259a2SJoe Perches 	char separator;
124676597ff9SAndrei Emeltchenko 	bool reversed = false;
1247bc7259a2SJoe Perches 
12483e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
12493e5903ebSPetr Mladek 		return buf;
12503e5903ebSPetr Mladek 
125176597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
125276597ff9SAndrei Emeltchenko 	case 'F':
1253bc7259a2SJoe Perches 		separator = '-';
125476597ff9SAndrei Emeltchenko 		break;
125576597ff9SAndrei Emeltchenko 
125676597ff9SAndrei Emeltchenko 	case 'R':
125776597ff9SAndrei Emeltchenko 		reversed = true;
125876597ff9SAndrei Emeltchenko 		/* fall through */
125976597ff9SAndrei Emeltchenko 
126076597ff9SAndrei Emeltchenko 	default:
1261bc7259a2SJoe Perches 		separator = ':';
126276597ff9SAndrei Emeltchenko 		break;
1263bc7259a2SJoe Perches 	}
1264dd45c9cfSHarvey Harrison 
1265dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
126676597ff9SAndrei Emeltchenko 		if (reversed)
126776597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
126876597ff9SAndrei Emeltchenko 		else
126955036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
127076597ff9SAndrei Emeltchenko 
12718a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
1272bc7259a2SJoe Perches 			*p++ = separator;
1273dd45c9cfSHarvey Harrison 	}
1274dd45c9cfSHarvey Harrison 	*p = '\0';
1275dd45c9cfSHarvey Harrison 
1276d529ac41SPetr Mladek 	return string_nocheck(buf, end, mac_addr, spec);
1277dd45c9cfSHarvey Harrison }
1278dd45c9cfSHarvey Harrison 
1279cf3b429bSJoe Perches static noinline_for_stack
1280cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
1281689afa7dSHarvey Harrison {
1282689afa7dSHarvey Harrison 	int i;
12830159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
12840159f24eSJoe Perches 	int index;
12850159f24eSJoe Perches 	int step;
1286689afa7dSHarvey Harrison 
12870159f24eSJoe Perches 	switch (fmt[2]) {
12880159f24eSJoe Perches 	case 'h':
12890159f24eSJoe Perches #ifdef __BIG_ENDIAN
12900159f24eSJoe Perches 		index = 0;
12910159f24eSJoe Perches 		step = 1;
12920159f24eSJoe Perches #else
12930159f24eSJoe Perches 		index = 3;
12940159f24eSJoe Perches 		step = -1;
12950159f24eSJoe Perches #endif
12960159f24eSJoe Perches 		break;
12970159f24eSJoe Perches 	case 'l':
12980159f24eSJoe Perches 		index = 3;
12990159f24eSJoe Perches 		step = -1;
13000159f24eSJoe Perches 		break;
13010159f24eSJoe Perches 	case 'n':
13020159f24eSJoe Perches 	case 'b':
13030159f24eSJoe Perches 	default:
13040159f24eSJoe Perches 		index = 0;
13050159f24eSJoe Perches 		step = 1;
13060159f24eSJoe Perches 		break;
13070159f24eSJoe Perches 	}
13088a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
13097c43d9a3SRasmus Villemoes 		char temp[4] __aligned(2);	/* hold each IP quad in reverse order */
1310133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
13118a27f7c9SJoe Perches 		if (leading_zeros) {
13128a27f7c9SJoe Perches 			if (digits < 3)
13138a27f7c9SJoe Perches 				*p++ = '0';
13148a27f7c9SJoe Perches 			if (digits < 2)
13158a27f7c9SJoe Perches 				*p++ = '0';
13168a27f7c9SJoe Perches 		}
13178a27f7c9SJoe Perches 		/* reverse the digits in the quad */
13188a27f7c9SJoe Perches 		while (digits--)
13198a27f7c9SJoe Perches 			*p++ = temp[digits];
13208a27f7c9SJoe Perches 		if (i < 3)
13218a27f7c9SJoe Perches 			*p++ = '.';
13220159f24eSJoe Perches 		index += step;
13238a27f7c9SJoe Perches 	}
13248a27f7c9SJoe Perches 	*p = '\0';
13257b9186f5SAndré Goddard Rosa 
13268a27f7c9SJoe Perches 	return p;
13278a27f7c9SJoe Perches }
13288a27f7c9SJoe Perches 
1329cf3b429bSJoe Perches static noinline_for_stack
1330cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
13318a27f7c9SJoe Perches {
13327b9186f5SAndré Goddard Rosa 	int i, j, range;
13338a27f7c9SJoe Perches 	unsigned char zerolength[8];
13348a27f7c9SJoe Perches 	int longest = 1;
13358a27f7c9SJoe Perches 	int colonpos = -1;
13368a27f7c9SJoe Perches 	u16 word;
13377b9186f5SAndré Goddard Rosa 	u8 hi, lo;
13388a27f7c9SJoe Perches 	bool needcolon = false;
1339eb78cd26SJoe Perches 	bool useIPv4;
1340eb78cd26SJoe Perches 	struct in6_addr in6;
1341eb78cd26SJoe Perches 
1342eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
1343eb78cd26SJoe Perches 
1344eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
13458a27f7c9SJoe Perches 
13468a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
13478a27f7c9SJoe Perches 
13488a27f7c9SJoe Perches 	if (useIPv4)
13498a27f7c9SJoe Perches 		range = 6;
13508a27f7c9SJoe Perches 	else
13518a27f7c9SJoe Perches 		range = 8;
13528a27f7c9SJoe Perches 
13538a27f7c9SJoe Perches 	/* find position of longest 0 run */
13548a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
13558a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
1356eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
13578a27f7c9SJoe Perches 				break;
13588a27f7c9SJoe Perches 			zerolength[i]++;
13598a27f7c9SJoe Perches 		}
13608a27f7c9SJoe Perches 	}
13618a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
13628a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
13638a27f7c9SJoe Perches 			longest = zerolength[i];
13648a27f7c9SJoe Perches 			colonpos = i;
13658a27f7c9SJoe Perches 		}
13668a27f7c9SJoe Perches 	}
136729cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
136829cf519eSJoe Perches 		colonpos = -1;
13698a27f7c9SJoe Perches 
13708a27f7c9SJoe Perches 	/* emit address */
13718a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
13728a27f7c9SJoe Perches 		if (i == colonpos) {
13738a27f7c9SJoe Perches 			if (needcolon || i == 0)
13748a27f7c9SJoe Perches 				*p++ = ':';
13758a27f7c9SJoe Perches 			*p++ = ':';
13768a27f7c9SJoe Perches 			needcolon = false;
13778a27f7c9SJoe Perches 			i += longest - 1;
13788a27f7c9SJoe Perches 			continue;
13798a27f7c9SJoe Perches 		}
13808a27f7c9SJoe Perches 		if (needcolon) {
13818a27f7c9SJoe Perches 			*p++ = ':';
13828a27f7c9SJoe Perches 			needcolon = false;
13838a27f7c9SJoe Perches 		}
13848a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
1385eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
13868a27f7c9SJoe Perches 		hi = word >> 8;
13878a27f7c9SJoe Perches 		lo = word & 0xff;
13888a27f7c9SJoe Perches 		if (hi) {
13898a27f7c9SJoe Perches 			if (hi > 0x0f)
139055036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
13918a27f7c9SJoe Perches 			else
13928a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
139355036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
13948a27f7c9SJoe Perches 		}
1395b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
139655036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
13978a27f7c9SJoe Perches 		else
13988a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
13998a27f7c9SJoe Perches 		needcolon = true;
14008a27f7c9SJoe Perches 	}
14018a27f7c9SJoe Perches 
14028a27f7c9SJoe Perches 	if (useIPv4) {
14038a27f7c9SJoe Perches 		if (needcolon)
14048a27f7c9SJoe Perches 			*p++ = ':';
14050159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
14068a27f7c9SJoe Perches 	}
14078a27f7c9SJoe Perches 	*p = '\0';
14087b9186f5SAndré Goddard Rosa 
14098a27f7c9SJoe Perches 	return p;
14108a27f7c9SJoe Perches }
14118a27f7c9SJoe Perches 
1412cf3b429bSJoe Perches static noinline_for_stack
1413cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
14148a27f7c9SJoe Perches {
14158a27f7c9SJoe Perches 	int i;
14167b9186f5SAndré Goddard Rosa 
1417689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
141855036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
141955036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
14208a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
1421689afa7dSHarvey Harrison 			*p++ = ':';
1422689afa7dSHarvey Harrison 	}
1423689afa7dSHarvey Harrison 	*p = '\0';
14247b9186f5SAndré Goddard Rosa 
14258a27f7c9SJoe Perches 	return p;
14268a27f7c9SJoe Perches }
14278a27f7c9SJoe Perches 
1428cf3b429bSJoe Perches static noinline_for_stack
1429cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
14308a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
14318a27f7c9SJoe Perches {
14328a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
14338a27f7c9SJoe Perches 
14348a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
1435eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
14368a27f7c9SJoe Perches 	else
1437eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
1438689afa7dSHarvey Harrison 
1439d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip6_addr, spec);
1440689afa7dSHarvey Harrison }
1441689afa7dSHarvey Harrison 
1442cf3b429bSJoe Perches static noinline_for_stack
1443cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
14448a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
14454aa99606SHarvey Harrison {
14468a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
14474aa99606SHarvey Harrison 
14480159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
14494aa99606SHarvey Harrison 
1450d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip4_addr, spec);
14514aa99606SHarvey Harrison }
14524aa99606SHarvey Harrison 
1453cf3b429bSJoe Perches static noinline_for_stack
145410679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
145510679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
145610679643SDaniel Borkmann {
145710679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
145810679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
145910679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
146010679643SDaniel Borkmann 		      sizeof("%1234567890")];
146110679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
146210679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
146310679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
146410679643SDaniel Borkmann 	u8 off = 0;
146510679643SDaniel Borkmann 
146610679643SDaniel Borkmann 	fmt++;
146710679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
146810679643SDaniel Borkmann 		switch (*fmt) {
146910679643SDaniel Borkmann 		case 'p':
147010679643SDaniel Borkmann 			have_p = true;
147110679643SDaniel Borkmann 			break;
147210679643SDaniel Borkmann 		case 'f':
147310679643SDaniel Borkmann 			have_f = true;
147410679643SDaniel Borkmann 			break;
147510679643SDaniel Borkmann 		case 's':
147610679643SDaniel Borkmann 			have_s = true;
147710679643SDaniel Borkmann 			break;
147810679643SDaniel Borkmann 		case 'c':
147910679643SDaniel Borkmann 			have_c = true;
148010679643SDaniel Borkmann 			break;
148110679643SDaniel Borkmann 		}
148210679643SDaniel Borkmann 	}
148310679643SDaniel Borkmann 
148410679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
148510679643SDaniel Borkmann 		*p = '[';
148610679643SDaniel Borkmann 		off = 1;
148710679643SDaniel Borkmann 	}
148810679643SDaniel Borkmann 
148910679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
149010679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
149110679643SDaniel Borkmann 	else
149210679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
149310679643SDaniel Borkmann 
149410679643SDaniel Borkmann 	if (have_p || have_s || have_f)
149510679643SDaniel Borkmann 		*p++ = ']';
149610679643SDaniel Borkmann 
149710679643SDaniel Borkmann 	if (have_p) {
149810679643SDaniel Borkmann 		*p++ = ':';
149910679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
150010679643SDaniel Borkmann 	}
150110679643SDaniel Borkmann 	if (have_f) {
150210679643SDaniel Borkmann 		*p++ = '/';
150310679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
150410679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
150510679643SDaniel Borkmann 	}
150610679643SDaniel Borkmann 	if (have_s) {
150710679643SDaniel Borkmann 		*p++ = '%';
150810679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
150910679643SDaniel Borkmann 	}
151010679643SDaniel Borkmann 	*p = '\0';
151110679643SDaniel Borkmann 
1512d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip6_addr, spec);
151310679643SDaniel Borkmann }
151410679643SDaniel Borkmann 
151510679643SDaniel Borkmann static noinline_for_stack
151610679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
151710679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
151810679643SDaniel Borkmann {
151910679643SDaniel Borkmann 	bool have_p = false;
152010679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
152110679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
152210679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
152310679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
152410679643SDaniel Borkmann 
152510679643SDaniel Borkmann 	fmt++;
152610679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
152710679643SDaniel Borkmann 		switch (*fmt) {
152810679643SDaniel Borkmann 		case 'p':
152910679643SDaniel Borkmann 			have_p = true;
153010679643SDaniel Borkmann 			break;
153110679643SDaniel Borkmann 		case 'h':
153210679643SDaniel Borkmann 		case 'l':
153310679643SDaniel Borkmann 		case 'n':
153410679643SDaniel Borkmann 		case 'b':
153510679643SDaniel Borkmann 			fmt4[2] = *fmt;
153610679643SDaniel Borkmann 			break;
153710679643SDaniel Borkmann 		}
153810679643SDaniel Borkmann 	}
153910679643SDaniel Borkmann 
154010679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
154110679643SDaniel Borkmann 	if (have_p) {
154210679643SDaniel Borkmann 		*p++ = ':';
154310679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
154410679643SDaniel Borkmann 	}
154510679643SDaniel Borkmann 	*p = '\0';
154610679643SDaniel Borkmann 
1547d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip4_addr, spec);
154810679643SDaniel Borkmann }
154910679643SDaniel Borkmann 
155010679643SDaniel Borkmann static noinline_for_stack
1551f00cc102SPetr Mladek char *ip_addr_string(char *buf, char *end, const void *ptr,
1552f00cc102SPetr Mladek 		     struct printf_spec spec, const char *fmt)
1553f00cc102SPetr Mladek {
15540b74d4d7SPetr Mladek 	char *err_fmt_msg;
15550b74d4d7SPetr Mladek 
15563e5903ebSPetr Mladek 	if (check_pointer(&buf, end, ptr, spec))
15573e5903ebSPetr Mladek 		return buf;
15583e5903ebSPetr Mladek 
1559f00cc102SPetr Mladek 	switch (fmt[1]) {
1560f00cc102SPetr Mladek 	case '6':
1561f00cc102SPetr Mladek 		return ip6_addr_string(buf, end, ptr, spec, fmt);
1562f00cc102SPetr Mladek 	case '4':
1563f00cc102SPetr Mladek 		return ip4_addr_string(buf, end, ptr, spec, fmt);
1564f00cc102SPetr Mladek 	case 'S': {
1565f00cc102SPetr Mladek 		const union {
1566f00cc102SPetr Mladek 			struct sockaddr		raw;
1567f00cc102SPetr Mladek 			struct sockaddr_in	v4;
1568f00cc102SPetr Mladek 			struct sockaddr_in6	v6;
1569f00cc102SPetr Mladek 		} *sa = ptr;
1570f00cc102SPetr Mladek 
1571f00cc102SPetr Mladek 		switch (sa->raw.sa_family) {
1572f00cc102SPetr Mladek 		case AF_INET:
1573f00cc102SPetr Mladek 			return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1574f00cc102SPetr Mladek 		case AF_INET6:
1575f00cc102SPetr Mladek 			return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1576f00cc102SPetr Mladek 		default:
1577c8c3b584SPetr Mladek 			return error_string(buf, end, "(einval)", spec);
1578f00cc102SPetr Mladek 		}}
1579f00cc102SPetr Mladek 	}
1580f00cc102SPetr Mladek 
15810b74d4d7SPetr Mladek 	err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
1582c8c3b584SPetr Mladek 	return error_string(buf, end, err_fmt_msg, spec);
1583f00cc102SPetr Mladek }
1584f00cc102SPetr Mladek 
1585f00cc102SPetr Mladek static noinline_for_stack
158671dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
158771dca95dSAndy Shevchenko 		     const char *fmt)
158871dca95dSAndy Shevchenko {
158971dca95dSAndy Shevchenko 	bool found = true;
159071dca95dSAndy Shevchenko 	int count = 1;
159171dca95dSAndy Shevchenko 	unsigned int flags = 0;
159271dca95dSAndy Shevchenko 	int len;
159371dca95dSAndy Shevchenko 
159471dca95dSAndy Shevchenko 	if (spec.field_width == 0)
159571dca95dSAndy Shevchenko 		return buf;				/* nothing to print */
159671dca95dSAndy Shevchenko 
15973e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
15983e5903ebSPetr Mladek 		return buf;
159971dca95dSAndy Shevchenko 
160071dca95dSAndy Shevchenko 	do {
160171dca95dSAndy Shevchenko 		switch (fmt[count++]) {
160271dca95dSAndy Shevchenko 		case 'a':
160371dca95dSAndy Shevchenko 			flags |= ESCAPE_ANY;
160471dca95dSAndy Shevchenko 			break;
160571dca95dSAndy Shevchenko 		case 'c':
160671dca95dSAndy Shevchenko 			flags |= ESCAPE_SPECIAL;
160771dca95dSAndy Shevchenko 			break;
160871dca95dSAndy Shevchenko 		case 'h':
160971dca95dSAndy Shevchenko 			flags |= ESCAPE_HEX;
161071dca95dSAndy Shevchenko 			break;
161171dca95dSAndy Shevchenko 		case 'n':
161271dca95dSAndy Shevchenko 			flags |= ESCAPE_NULL;
161371dca95dSAndy Shevchenko 			break;
161471dca95dSAndy Shevchenko 		case 'o':
161571dca95dSAndy Shevchenko 			flags |= ESCAPE_OCTAL;
161671dca95dSAndy Shevchenko 			break;
161771dca95dSAndy Shevchenko 		case 'p':
161871dca95dSAndy Shevchenko 			flags |= ESCAPE_NP;
161971dca95dSAndy Shevchenko 			break;
162071dca95dSAndy Shevchenko 		case 's':
162171dca95dSAndy Shevchenko 			flags |= ESCAPE_SPACE;
162271dca95dSAndy Shevchenko 			break;
162371dca95dSAndy Shevchenko 		default:
162471dca95dSAndy Shevchenko 			found = false;
162571dca95dSAndy Shevchenko 			break;
162671dca95dSAndy Shevchenko 		}
162771dca95dSAndy Shevchenko 	} while (found);
162871dca95dSAndy Shevchenko 
162971dca95dSAndy Shevchenko 	if (!flags)
163071dca95dSAndy Shevchenko 		flags = ESCAPE_ANY_NP;
163171dca95dSAndy Shevchenko 
163271dca95dSAndy Shevchenko 	len = spec.field_width < 0 ? 1 : spec.field_width;
163371dca95dSAndy Shevchenko 
163441416f23SRasmus Villemoes 	/*
163541416f23SRasmus Villemoes 	 * string_escape_mem() writes as many characters as it can to
163641416f23SRasmus Villemoes 	 * the given buffer, and returns the total size of the output
163741416f23SRasmus Villemoes 	 * had the buffer been big enough.
163841416f23SRasmus Villemoes 	 */
163941416f23SRasmus Villemoes 	buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
164071dca95dSAndy Shevchenko 
164171dca95dSAndy Shevchenko 	return buf;
164271dca95dSAndy Shevchenko }
164371dca95dSAndy Shevchenko 
16443e5903ebSPetr Mladek static char *va_format(char *buf, char *end, struct va_format *va_fmt,
16453e5903ebSPetr Mladek 		       struct printf_spec spec, const char *fmt)
164645c3e93dSPetr Mladek {
164745c3e93dSPetr Mladek 	va_list va;
164845c3e93dSPetr Mladek 
16493e5903ebSPetr Mladek 	if (check_pointer(&buf, end, va_fmt, spec))
16503e5903ebSPetr Mladek 		return buf;
16513e5903ebSPetr Mladek 
165245c3e93dSPetr Mladek 	va_copy(va, *va_fmt->va);
165345c3e93dSPetr Mladek 	buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
165445c3e93dSPetr Mladek 	va_end(va);
165545c3e93dSPetr Mladek 
165645c3e93dSPetr Mladek 	return buf;
165745c3e93dSPetr Mladek }
165845c3e93dSPetr Mladek 
165971dca95dSAndy Shevchenko static noinline_for_stack
1660cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
16619ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
16629ac6e44eSJoe Perches {
16632b1b0d66SAndy Shevchenko 	char uuid[UUID_STRING_LEN + 1];
16649ac6e44eSJoe Perches 	char *p = uuid;
16659ac6e44eSJoe Perches 	int i;
1666f9727a17SChristoph Hellwig 	const u8 *index = uuid_index;
16679ac6e44eSJoe Perches 	bool uc = false;
16689ac6e44eSJoe Perches 
16693e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
16703e5903ebSPetr Mladek 		return buf;
16713e5903ebSPetr Mladek 
16729ac6e44eSJoe Perches 	switch (*(++fmt)) {
16739ac6e44eSJoe Perches 	case 'L':
16749ac6e44eSJoe Perches 		uc = true;		/* fall-through */
16759ac6e44eSJoe Perches 	case 'l':
1676f9727a17SChristoph Hellwig 		index = guid_index;
16779ac6e44eSJoe Perches 		break;
16789ac6e44eSJoe Perches 	case 'B':
16799ac6e44eSJoe Perches 		uc = true;
16809ac6e44eSJoe Perches 		break;
16819ac6e44eSJoe Perches 	}
16829ac6e44eSJoe Perches 
16839ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
1684aa4ea1c3SAndy Shevchenko 		if (uc)
1685aa4ea1c3SAndy Shevchenko 			p = hex_byte_pack_upper(p, addr[index[i]]);
1686aa4ea1c3SAndy Shevchenko 		else
168755036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[index[i]]);
16889ac6e44eSJoe Perches 		switch (i) {
16899ac6e44eSJoe Perches 		case 3:
16909ac6e44eSJoe Perches 		case 5:
16919ac6e44eSJoe Perches 		case 7:
16929ac6e44eSJoe Perches 		case 9:
16939ac6e44eSJoe Perches 			*p++ = '-';
16949ac6e44eSJoe Perches 			break;
16959ac6e44eSJoe Perches 		}
16969ac6e44eSJoe Perches 	}
16979ac6e44eSJoe Perches 
16989ac6e44eSJoe Perches 	*p = 0;
16999ac6e44eSJoe Perches 
1700d529ac41SPetr Mladek 	return string_nocheck(buf, end, uuid, spec);
17019ac6e44eSJoe Perches }
17029ac6e44eSJoe Perches 
17035b17aecfSAndy Shevchenko static noinline_for_stack
1704431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr,
1705431bca24SGeert Uytterhoeven 		  struct printf_spec spec,  const char *fmt)
1706c8f44affSMichał Mirosław {
17075b17aecfSAndy Shevchenko 	unsigned long long num;
17085b17aecfSAndy Shevchenko 	int size;
17095b17aecfSAndy Shevchenko 
17103e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
17113e5903ebSPetr Mladek 		return buf;
17123e5903ebSPetr Mladek 
17135b17aecfSAndy Shevchenko 	switch (fmt[1]) {
17145b17aecfSAndy Shevchenko 	case 'F':
17155b17aecfSAndy Shevchenko 		num = *(const netdev_features_t *)addr;
17165b17aecfSAndy Shevchenko 		size = sizeof(netdev_features_t);
17175b17aecfSAndy Shevchenko 		break;
17185b17aecfSAndy Shevchenko 	default:
1719c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pN?)", spec);
17205b17aecfSAndy Shevchenko 	}
1721c8f44affSMichał Mirosław 
17223cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, num, size);
1723c8f44affSMichał Mirosław }
1724c8f44affSMichał Mirosław 
1725aaf07621SJoe Perches static noinline_for_stack
17263e5903ebSPetr Mladek char *address_val(char *buf, char *end, const void *addr,
17273e5903ebSPetr Mladek 		  struct printf_spec spec, const char *fmt)
1728aaf07621SJoe Perches {
1729aaf07621SJoe Perches 	unsigned long long num;
17303cab1e71SAndy Shevchenko 	int size;
1731aaf07621SJoe Perches 
17323e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
17333e5903ebSPetr Mladek 		return buf;
17343e5903ebSPetr Mladek 
1735aaf07621SJoe Perches 	switch (fmt[1]) {
1736aaf07621SJoe Perches 	case 'd':
1737aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
17383cab1e71SAndy Shevchenko 		size = sizeof(dma_addr_t);
1739aaf07621SJoe Perches 		break;
1740aaf07621SJoe Perches 	case 'p':
1741aaf07621SJoe Perches 	default:
1742aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
17433cab1e71SAndy Shevchenko 		size = sizeof(phys_addr_t);
1744aaf07621SJoe Perches 		break;
1745aaf07621SJoe Perches 	}
1746aaf07621SJoe Perches 
17473cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, num, size);
1748aaf07621SJoe Perches }
1749aaf07621SJoe Perches 
1750900cca29SGeert Uytterhoeven static noinline_for_stack
17514d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
17524d42c447SAndy Shevchenko {
17534d42c447SAndy Shevchenko 	int year = tm->tm_year + (r ? 0 : 1900);
17544d42c447SAndy Shevchenko 	int mon = tm->tm_mon + (r ? 0 : 1);
17554d42c447SAndy Shevchenko 
17564d42c447SAndy Shevchenko 	buf = number(buf, end, year, default_dec04_spec);
17574d42c447SAndy Shevchenko 	if (buf < end)
17584d42c447SAndy Shevchenko 		*buf = '-';
17594d42c447SAndy Shevchenko 	buf++;
17604d42c447SAndy Shevchenko 
17614d42c447SAndy Shevchenko 	buf = number(buf, end, mon, default_dec02_spec);
17624d42c447SAndy Shevchenko 	if (buf < end)
17634d42c447SAndy Shevchenko 		*buf = '-';
17644d42c447SAndy Shevchenko 	buf++;
17654d42c447SAndy Shevchenko 
17664d42c447SAndy Shevchenko 	return number(buf, end, tm->tm_mday, default_dec02_spec);
17674d42c447SAndy Shevchenko }
17684d42c447SAndy Shevchenko 
17694d42c447SAndy Shevchenko static noinline_for_stack
17704d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
17714d42c447SAndy Shevchenko {
17724d42c447SAndy Shevchenko 	buf = number(buf, end, tm->tm_hour, default_dec02_spec);
17734d42c447SAndy Shevchenko 	if (buf < end)
17744d42c447SAndy Shevchenko 		*buf = ':';
17754d42c447SAndy Shevchenko 	buf++;
17764d42c447SAndy Shevchenko 
17774d42c447SAndy Shevchenko 	buf = number(buf, end, tm->tm_min, default_dec02_spec);
17784d42c447SAndy Shevchenko 	if (buf < end)
17794d42c447SAndy Shevchenko 		*buf = ':';
17804d42c447SAndy Shevchenko 	buf++;
17814d42c447SAndy Shevchenko 
17824d42c447SAndy Shevchenko 	return number(buf, end, tm->tm_sec, default_dec02_spec);
17834d42c447SAndy Shevchenko }
17844d42c447SAndy Shevchenko 
17854d42c447SAndy Shevchenko static noinline_for_stack
17863e5903ebSPetr Mladek char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
17873e5903ebSPetr Mladek 	      struct printf_spec spec, const char *fmt)
17884d42c447SAndy Shevchenko {
17894d42c447SAndy Shevchenko 	bool have_t = true, have_d = true;
17904d42c447SAndy Shevchenko 	bool raw = false;
17914d42c447SAndy Shevchenko 	int count = 2;
17924d42c447SAndy Shevchenko 
17933e5903ebSPetr Mladek 	if (check_pointer(&buf, end, tm, spec))
17943e5903ebSPetr Mladek 		return buf;
17953e5903ebSPetr Mladek 
17964d42c447SAndy Shevchenko 	switch (fmt[count]) {
17974d42c447SAndy Shevchenko 	case 'd':
17984d42c447SAndy Shevchenko 		have_t = false;
17994d42c447SAndy Shevchenko 		count++;
18004d42c447SAndy Shevchenko 		break;
18014d42c447SAndy Shevchenko 	case 't':
18024d42c447SAndy Shevchenko 		have_d = false;
18034d42c447SAndy Shevchenko 		count++;
18044d42c447SAndy Shevchenko 		break;
18054d42c447SAndy Shevchenko 	}
18064d42c447SAndy Shevchenko 
18074d42c447SAndy Shevchenko 	raw = fmt[count] == 'r';
18084d42c447SAndy Shevchenko 
18094d42c447SAndy Shevchenko 	if (have_d)
18104d42c447SAndy Shevchenko 		buf = date_str(buf, end, tm, raw);
18114d42c447SAndy Shevchenko 	if (have_d && have_t) {
18124d42c447SAndy Shevchenko 		/* Respect ISO 8601 */
18134d42c447SAndy Shevchenko 		if (buf < end)
18144d42c447SAndy Shevchenko 			*buf = 'T';
18154d42c447SAndy Shevchenko 		buf++;
18164d42c447SAndy Shevchenko 	}
18174d42c447SAndy Shevchenko 	if (have_t)
18184d42c447SAndy Shevchenko 		buf = time_str(buf, end, tm, raw);
18194d42c447SAndy Shevchenko 
18204d42c447SAndy Shevchenko 	return buf;
18214d42c447SAndy Shevchenko }
18224d42c447SAndy Shevchenko 
18234d42c447SAndy Shevchenko static noinline_for_stack
1824*7daac5b2SAndy Shevchenko char *time64_str(char *buf, char *end, const time64_t time,
1825*7daac5b2SAndy Shevchenko 		 struct printf_spec spec, const char *fmt)
1826*7daac5b2SAndy Shevchenko {
1827*7daac5b2SAndy Shevchenko 	struct rtc_time rtc_time;
1828*7daac5b2SAndy Shevchenko 	struct tm tm;
1829*7daac5b2SAndy Shevchenko 
1830*7daac5b2SAndy Shevchenko 	time64_to_tm(time, 0, &tm);
1831*7daac5b2SAndy Shevchenko 
1832*7daac5b2SAndy Shevchenko 	rtc_time.tm_sec = tm.tm_sec;
1833*7daac5b2SAndy Shevchenko 	rtc_time.tm_min = tm.tm_min;
1834*7daac5b2SAndy Shevchenko 	rtc_time.tm_hour = tm.tm_hour;
1835*7daac5b2SAndy Shevchenko 	rtc_time.tm_mday = tm.tm_mday;
1836*7daac5b2SAndy Shevchenko 	rtc_time.tm_mon = tm.tm_mon;
1837*7daac5b2SAndy Shevchenko 	rtc_time.tm_year = tm.tm_year;
1838*7daac5b2SAndy Shevchenko 	rtc_time.tm_wday = tm.tm_wday;
1839*7daac5b2SAndy Shevchenko 	rtc_time.tm_yday = tm.tm_yday;
1840*7daac5b2SAndy Shevchenko 
1841*7daac5b2SAndy Shevchenko 	rtc_time.tm_isdst = 0;
1842*7daac5b2SAndy Shevchenko 
1843*7daac5b2SAndy Shevchenko 	return rtc_str(buf, end, &rtc_time, spec, fmt);
1844*7daac5b2SAndy Shevchenko }
1845*7daac5b2SAndy Shevchenko 
1846*7daac5b2SAndy Shevchenko static noinline_for_stack
18474d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
18484d42c447SAndy Shevchenko 		    const char *fmt)
18494d42c447SAndy Shevchenko {
18504d42c447SAndy Shevchenko 	switch (fmt[1]) {
18514d42c447SAndy Shevchenko 	case 'R':
18523e5903ebSPetr Mladek 		return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
1853*7daac5b2SAndy Shevchenko 	case 'T':
1854*7daac5b2SAndy Shevchenko 		return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt);
18554d42c447SAndy Shevchenko 	default:
1856*7daac5b2SAndy Shevchenko 		return error_string(buf, end, "(%pt?)", spec);
18574d42c447SAndy Shevchenko 	}
18584d42c447SAndy Shevchenko }
18594d42c447SAndy Shevchenko 
18604d42c447SAndy Shevchenko static noinline_for_stack
1861900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1862900cca29SGeert Uytterhoeven 	    const char *fmt)
1863900cca29SGeert Uytterhoeven {
18640b74d4d7SPetr Mladek 	if (!IS_ENABLED(CONFIG_HAVE_CLK))
1865c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pC?)", spec);
18660b74d4d7SPetr Mladek 
18673e5903ebSPetr Mladek 	if (check_pointer(&buf, end, clk, spec))
18683e5903ebSPetr Mladek 		return buf;
1869900cca29SGeert Uytterhoeven 
1870900cca29SGeert Uytterhoeven 	switch (fmt[1]) {
1871900cca29SGeert Uytterhoeven 	case 'n':
1872900cca29SGeert Uytterhoeven 	default:
1873900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK
1874900cca29SGeert Uytterhoeven 		return string(buf, end, __clk_get_name(clk), spec);
1875900cca29SGeert Uytterhoeven #else
18764ca96aa9SGeert Uytterhoeven 		return ptr_to_id(buf, end, clk, spec);
1877900cca29SGeert Uytterhoeven #endif
1878900cca29SGeert Uytterhoeven 	}
1879900cca29SGeert Uytterhoeven }
1880900cca29SGeert Uytterhoeven 
1881edf14cdbSVlastimil Babka static
1882edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags,
1883edf14cdbSVlastimil Babka 					const struct trace_print_flags *names)
1884edf14cdbSVlastimil Babka {
1885edf14cdbSVlastimil Babka 	unsigned long mask;
1886edf14cdbSVlastimil Babka 
1887edf14cdbSVlastimil Babka 	for ( ; flags && names->name; names++) {
1888edf14cdbSVlastimil Babka 		mask = names->mask;
1889edf14cdbSVlastimil Babka 		if ((flags & mask) != mask)
1890edf14cdbSVlastimil Babka 			continue;
1891edf14cdbSVlastimil Babka 
1892abd4fe62SAndy Shevchenko 		buf = string(buf, end, names->name, default_str_spec);
1893edf14cdbSVlastimil Babka 
1894edf14cdbSVlastimil Babka 		flags &= ~mask;
1895edf14cdbSVlastimil Babka 		if (flags) {
1896edf14cdbSVlastimil Babka 			if (buf < end)
1897edf14cdbSVlastimil Babka 				*buf = '|';
1898edf14cdbSVlastimil Babka 			buf++;
1899edf14cdbSVlastimil Babka 		}
1900edf14cdbSVlastimil Babka 	}
1901edf14cdbSVlastimil Babka 
1902edf14cdbSVlastimil Babka 	if (flags)
190354433973SAndy Shevchenko 		buf = number(buf, end, flags, default_flag_spec);
1904edf14cdbSVlastimil Babka 
1905edf14cdbSVlastimil Babka 	return buf;
1906edf14cdbSVlastimil Babka }
1907edf14cdbSVlastimil Babka 
1908edf14cdbSVlastimil Babka static noinline_for_stack
19090b74d4d7SPetr Mladek char *flags_string(char *buf, char *end, void *flags_ptr,
19100b74d4d7SPetr Mladek 		   struct printf_spec spec, const char *fmt)
1911edf14cdbSVlastimil Babka {
1912edf14cdbSVlastimil Babka 	unsigned long flags;
1913edf14cdbSVlastimil Babka 	const struct trace_print_flags *names;
1914edf14cdbSVlastimil Babka 
19153e5903ebSPetr Mladek 	if (check_pointer(&buf, end, flags_ptr, spec))
19163e5903ebSPetr Mladek 		return buf;
19173e5903ebSPetr Mladek 
1918edf14cdbSVlastimil Babka 	switch (fmt[1]) {
1919edf14cdbSVlastimil Babka 	case 'p':
1920edf14cdbSVlastimil Babka 		flags = *(unsigned long *)flags_ptr;
1921edf14cdbSVlastimil Babka 		/* Remove zone id */
1922edf14cdbSVlastimil Babka 		flags &= (1UL << NR_PAGEFLAGS) - 1;
1923edf14cdbSVlastimil Babka 		names = pageflag_names;
1924edf14cdbSVlastimil Babka 		break;
1925edf14cdbSVlastimil Babka 	case 'v':
1926edf14cdbSVlastimil Babka 		flags = *(unsigned long *)flags_ptr;
1927edf14cdbSVlastimil Babka 		names = vmaflag_names;
1928edf14cdbSVlastimil Babka 		break;
1929edf14cdbSVlastimil Babka 	case 'g':
1930edf14cdbSVlastimil Babka 		flags = *(gfp_t *)flags_ptr;
1931edf14cdbSVlastimil Babka 		names = gfpflag_names;
1932edf14cdbSVlastimil Babka 		break;
1933edf14cdbSVlastimil Babka 	default:
1934c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pG?)", spec);
1935edf14cdbSVlastimil Babka 	}
1936edf14cdbSVlastimil Babka 
1937edf14cdbSVlastimil Babka 	return format_flags(buf, end, flags, names);
1938edf14cdbSVlastimil Babka }
1939edf14cdbSVlastimil Babka 
1940ce4fecf1SPantelis Antoniou static noinline_for_stack
1941a92eb762SSakari Ailus char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
1942a92eb762SSakari Ailus 			      char *end)
1943ce4fecf1SPantelis Antoniou {
1944ce4fecf1SPantelis Antoniou 	int depth;
1945ce4fecf1SPantelis Antoniou 
1946a92eb762SSakari Ailus 	/* Loop starting from the root node to the current node. */
1947a92eb762SSakari Ailus 	for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
1948a92eb762SSakari Ailus 		struct fwnode_handle *__fwnode =
1949a92eb762SSakari Ailus 			fwnode_get_nth_parent(fwnode, depth);
1950ce4fecf1SPantelis Antoniou 
1951a92eb762SSakari Ailus 		buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
1952abd4fe62SAndy Shevchenko 			     default_str_spec);
1953a92eb762SSakari Ailus 		buf = string(buf, end, fwnode_get_name(__fwnode),
1954a92eb762SSakari Ailus 			     default_str_spec);
1955a92eb762SSakari Ailus 
1956a92eb762SSakari Ailus 		fwnode_handle_put(__fwnode);
1957ce4fecf1SPantelis Antoniou 	}
1958a92eb762SSakari Ailus 
1959ce4fecf1SPantelis Antoniou 	return buf;
1960ce4fecf1SPantelis Antoniou }
1961ce4fecf1SPantelis Antoniou 
1962ce4fecf1SPantelis Antoniou static noinline_for_stack
1963ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn,
1964ce4fecf1SPantelis Antoniou 			 struct printf_spec spec, const char *fmt)
1965ce4fecf1SPantelis Antoniou {
1966ce4fecf1SPantelis Antoniou 	char tbuf[sizeof("xxxx") + 1];
1967ce4fecf1SPantelis Antoniou 	const char *p;
1968ce4fecf1SPantelis Antoniou 	int ret;
1969ce4fecf1SPantelis Antoniou 	char *buf_start = buf;
1970ce4fecf1SPantelis Antoniou 	struct property *prop;
1971ce4fecf1SPantelis Antoniou 	bool has_mult, pass;
1972ce4fecf1SPantelis Antoniou 	static const struct printf_spec num_spec = {
1973ce4fecf1SPantelis Antoniou 		.flags = SMALL,
1974ce4fecf1SPantelis Antoniou 		.field_width = -1,
1975ce4fecf1SPantelis Antoniou 		.precision = -1,
1976ce4fecf1SPantelis Antoniou 		.base = 10,
1977ce4fecf1SPantelis Antoniou 	};
1978ce4fecf1SPantelis Antoniou 
1979ce4fecf1SPantelis Antoniou 	struct printf_spec str_spec = spec;
1980ce4fecf1SPantelis Antoniou 	str_spec.field_width = -1;
1981ce4fecf1SPantelis Antoniou 
198283abc5a7SSakari Ailus 	if (fmt[0] != 'F')
198383abc5a7SSakari Ailus 		return error_string(buf, end, "(%pO?)", spec);
198483abc5a7SSakari Ailus 
1985ce4fecf1SPantelis Antoniou 	if (!IS_ENABLED(CONFIG_OF))
1986c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pOF?)", spec);
1987ce4fecf1SPantelis Antoniou 
19883e5903ebSPetr Mladek 	if (check_pointer(&buf, end, dn, spec))
19893e5903ebSPetr Mladek 		return buf;
1990ce4fecf1SPantelis Antoniou 
1991ce4fecf1SPantelis Antoniou 	/* simple case without anything any more format specifiers */
1992ce4fecf1SPantelis Antoniou 	fmt++;
1993ce4fecf1SPantelis Antoniou 	if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
1994ce4fecf1SPantelis Antoniou 		fmt = "f";
1995ce4fecf1SPantelis Antoniou 
1996ce4fecf1SPantelis Antoniou 	for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
19976d0a70a2SRob Herring 		int precision;
1998ce4fecf1SPantelis Antoniou 		if (pass) {
1999ce4fecf1SPantelis Antoniou 			if (buf < end)
2000ce4fecf1SPantelis Antoniou 				*buf = ':';
2001ce4fecf1SPantelis Antoniou 			buf++;
2002ce4fecf1SPantelis Antoniou 		}
2003ce4fecf1SPantelis Antoniou 
2004ce4fecf1SPantelis Antoniou 		switch (*fmt) {
2005ce4fecf1SPantelis Antoniou 		case 'f':	/* full_name */
2006a92eb762SSakari Ailus 			buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
2007a92eb762SSakari Ailus 						      end);
2008ce4fecf1SPantelis Antoniou 			break;
2009ce4fecf1SPantelis Antoniou 		case 'n':	/* name */
2010a92eb762SSakari Ailus 			p = fwnode_get_name(of_fwnode_handle(dn));
20116d0a70a2SRob Herring 			precision = str_spec.precision;
20126d0a70a2SRob Herring 			str_spec.precision = strchrnul(p, '@') - p;
20136d0a70a2SRob Herring 			buf = string(buf, end, p, str_spec);
20146d0a70a2SRob Herring 			str_spec.precision = precision;
2015ce4fecf1SPantelis Antoniou 			break;
2016ce4fecf1SPantelis Antoniou 		case 'p':	/* phandle */
2017ce4fecf1SPantelis Antoniou 			buf = number(buf, end, (unsigned int)dn->phandle, num_spec);
2018ce4fecf1SPantelis Antoniou 			break;
2019ce4fecf1SPantelis Antoniou 		case 'P':	/* path-spec */
2020a92eb762SSakari Ailus 			p = fwnode_get_name(of_fwnode_handle(dn));
2021ce4fecf1SPantelis Antoniou 			if (!p[1])
2022ce4fecf1SPantelis Antoniou 				p = "/";
2023ce4fecf1SPantelis Antoniou 			buf = string(buf, end, p, str_spec);
2024ce4fecf1SPantelis Antoniou 			break;
2025ce4fecf1SPantelis Antoniou 		case 'F':	/* flags */
2026ce4fecf1SPantelis Antoniou 			tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
2027ce4fecf1SPantelis Antoniou 			tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
2028ce4fecf1SPantelis Antoniou 			tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
2029ce4fecf1SPantelis Antoniou 			tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
2030ce4fecf1SPantelis Antoniou 			tbuf[4] = 0;
2031d529ac41SPetr Mladek 			buf = string_nocheck(buf, end, tbuf, str_spec);
2032ce4fecf1SPantelis Antoniou 			break;
2033ce4fecf1SPantelis Antoniou 		case 'c':	/* major compatible string */
2034ce4fecf1SPantelis Antoniou 			ret = of_property_read_string(dn, "compatible", &p);
2035ce4fecf1SPantelis Antoniou 			if (!ret)
2036ce4fecf1SPantelis Antoniou 				buf = string(buf, end, p, str_spec);
2037ce4fecf1SPantelis Antoniou 			break;
2038ce4fecf1SPantelis Antoniou 		case 'C':	/* full compatible string */
2039ce4fecf1SPantelis Antoniou 			has_mult = false;
2040ce4fecf1SPantelis Antoniou 			of_property_for_each_string(dn, "compatible", prop, p) {
2041ce4fecf1SPantelis Antoniou 				if (has_mult)
2042d529ac41SPetr Mladek 					buf = string_nocheck(buf, end, ",", str_spec);
2043d529ac41SPetr Mladek 				buf = string_nocheck(buf, end, "\"", str_spec);
2044ce4fecf1SPantelis Antoniou 				buf = string(buf, end, p, str_spec);
2045d529ac41SPetr Mladek 				buf = string_nocheck(buf, end, "\"", str_spec);
2046ce4fecf1SPantelis Antoniou 
2047ce4fecf1SPantelis Antoniou 				has_mult = true;
2048ce4fecf1SPantelis Antoniou 			}
2049ce4fecf1SPantelis Antoniou 			break;
2050ce4fecf1SPantelis Antoniou 		default:
2051ce4fecf1SPantelis Antoniou 			break;
2052ce4fecf1SPantelis Antoniou 		}
2053ce4fecf1SPantelis Antoniou 	}
2054ce4fecf1SPantelis Antoniou 
2055ce4fecf1SPantelis Antoniou 	return widen_string(buf, buf - buf_start, end, spec);
2056ce4fecf1SPantelis Antoniou }
2057ce4fecf1SPantelis Antoniou 
20583bd32d6aSSakari Ailus static noinline_for_stack
20593bd32d6aSSakari Ailus char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2060798cc27aSPetr Mladek 		    struct printf_spec spec, const char *fmt)
2061798cc27aSPetr Mladek {
20623bd32d6aSSakari Ailus 	struct printf_spec str_spec = spec;
20633bd32d6aSSakari Ailus 	char *buf_start = buf;
20643bd32d6aSSakari Ailus 
20653bd32d6aSSakari Ailus 	str_spec.field_width = -1;
20663bd32d6aSSakari Ailus 
20673bd32d6aSSakari Ailus 	if (*fmt != 'w')
20683bd32d6aSSakari Ailus 		return error_string(buf, end, "(%pf?)", spec);
20693bd32d6aSSakari Ailus 
20703bd32d6aSSakari Ailus 	if (check_pointer(&buf, end, fwnode, spec))
20713bd32d6aSSakari Ailus 		return buf;
20723bd32d6aSSakari Ailus 
20733bd32d6aSSakari Ailus 	fmt++;
20743bd32d6aSSakari Ailus 
20753bd32d6aSSakari Ailus 	switch (*fmt) {
20763bd32d6aSSakari Ailus 	case 'P':	/* name */
20773bd32d6aSSakari Ailus 		buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
20783bd32d6aSSakari Ailus 		break;
20793bd32d6aSSakari Ailus 	case 'f':	/* full_name */
20803bd32d6aSSakari Ailus 	default:
20813bd32d6aSSakari Ailus 		buf = fwnode_full_name_string(fwnode, buf, end);
20823bd32d6aSSakari Ailus 		break;
2083798cc27aSPetr Mladek 	}
2084798cc27aSPetr Mladek 
20853bd32d6aSSakari Ailus 	return widen_string(buf, buf - buf_start, end, spec);
2086798cc27aSPetr Mladek }
2087798cc27aSPetr Mladek 
20884d8a743cSLinus Torvalds /*
20894d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
20904d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
20914d8a743cSLinus Torvalds  * specifiers.
20924d8a743cSLinus Torvalds  *
20930b523769SJoe Perches  * Please update scripts/checkpatch.pl when adding/removing conversion
20940b523769SJoe Perches  * characters.  (Search for "check for vsprintf extension").
20950b523769SJoe Perches  *
2096332d2e78SLinus Torvalds  * Right now we handle:
20970fe1ef24SLinus Torvalds  *
2098cdb7e52dSSergey Senozhatsky  * - 'S' For symbolic direct pointers (or function descriptors) with offset
2099cdb7e52dSSergey Senozhatsky  * - 's' For symbolic direct pointers (or function descriptors) without offset
21009af77064SSakari Ailus  * - '[Ss]R' as above with __builtin_extract_return_addr() translation
21011586c5aeSSakari Ailus  * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
21021586c5aeSSakari Ailus  *	    %ps and %pS. Be careful when re-using these specifiers.
21030f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
2104c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2105c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
2106dbc760bcSTejun Heo  * - 'b[l]' For a bitmap, the number of bits is determined by the field
2107dbc760bcSTejun Heo  *       width which must be explicitly specified either as part of the
2108dbc760bcSTejun Heo  *       format string '%32b[l]' or through '%*b[l]', [l] selects
2109dbc760bcSTejun Heo  *       range-list format instead of hex format
2110dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
2111dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
21128a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
2113bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
2114c8e00060SJoe Perches  *       with a dash-separated hex notation
21157c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
21168a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
21178a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
21188a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
211910679643SDaniel Borkmann  *       [S][pfs]
212010679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
212110679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
21228a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
21238a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
21248a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
212510679643SDaniel Borkmann  *       [S][pfs]
212610679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
212710679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
212810679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
212910679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
213029cf519eSJoe Perches  *       http://tools.ietf.org/html/rfc5952
213171dca95dSAndy Shevchenko  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
213271dca95dSAndy Shevchenko  *                of the following flags (see string_escape_mem() for the
213371dca95dSAndy Shevchenko  *                details):
213471dca95dSAndy Shevchenko  *                  a - ESCAPE_ANY
213571dca95dSAndy Shevchenko  *                  c - ESCAPE_SPECIAL
213671dca95dSAndy Shevchenko  *                  h - ESCAPE_HEX
213771dca95dSAndy Shevchenko  *                  n - ESCAPE_NULL
213871dca95dSAndy Shevchenko  *                  o - ESCAPE_OCTAL
213971dca95dSAndy Shevchenko  *                  p - ESCAPE_NP
214071dca95dSAndy Shevchenko  *                  s - ESCAPE_SPACE
214171dca95dSAndy Shevchenko  *                By default ESCAPE_ANY_NP is used.
21429ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
21439ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
21449ac6e44eSJoe Perches  *       Options for %pU are:
21459ac6e44eSJoe Perches  *         b big endian lower case hex (default)
21469ac6e44eSJoe Perches  *         B big endian UPPER case hex
21479ac6e44eSJoe Perches  *         l little endian lower case hex
21489ac6e44eSJoe Perches  *         L little endian UPPER case hex
21499ac6e44eSJoe Perches  *           big endian output byte order is:
21509ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
21519ac6e44eSJoe Perches  *           little endian output byte order is:
21529ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
21537db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
21547db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
21557db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
21567db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
21577db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
2158455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
2159c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
216031550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
216131550a16SAndy Shevchenko  *            a certain separator (' ' by default):
216231550a16SAndy Shevchenko  *              C colon
216331550a16SAndy Shevchenko  *              D dash
216431550a16SAndy Shevchenko  *              N no separator
216531550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
216631550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
2167aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2168aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
2169c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
2170c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
21711031bc58SDmitry Monakhov  * - 'g' For block_device name (gendisk + partition number)
2172*7daac5b2SAndy Shevchenko  * - 't[RT][dt][r]' For time and date as represented by:
21734d42c447SAndy Shevchenko  *      R    struct rtc_time
2174*7daac5b2SAndy Shevchenko  *      T    time64_t
2175900cca29SGeert Uytterhoeven  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2176900cca29SGeert Uytterhoeven  *       (legacy clock framework) of the clock
2177900cca29SGeert Uytterhoeven  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2178900cca29SGeert Uytterhoeven  *        (legacy clock framework) of the clock
2179edf14cdbSVlastimil Babka  * - 'G' For flags to be printed as a collection of symbolic strings that would
2180edf14cdbSVlastimil Babka  *       construct the specific value. Supported flags given by option:
2181edf14cdbSVlastimil Babka  *       p page flags (see struct page) given as pointer to unsigned long
2182edf14cdbSVlastimil Babka  *       g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2183edf14cdbSVlastimil Babka  *       v vma flags (VM_*) given as pointer to unsigned long
2184ce4fecf1SPantelis Antoniou  * - 'OF[fnpPcCF]'  For a device tree object
2185ce4fecf1SPantelis Antoniou  *                  Without any optional arguments prints the full_name
2186ce4fecf1SPantelis Antoniou  *                  f device node full_name
2187ce4fecf1SPantelis Antoniou  *                  n device node name
2188ce4fecf1SPantelis Antoniou  *                  p device node phandle
2189ce4fecf1SPantelis Antoniou  *                  P device node path spec (name + @unit)
2190ce4fecf1SPantelis Antoniou  *                  F device node flags
2191ce4fecf1SPantelis Antoniou  *                  c major compatible string
2192ce4fecf1SPantelis Antoniou  *                  C full compatible string
21933bd32d6aSSakari Ailus  * - 'fw[fP]'	For a firmware node (struct fwnode_handle) pointer
21943bd32d6aSSakari Ailus  *		Without an option prints the full name of the node
21953bd32d6aSSakari Ailus  *		f full name
21963bd32d6aSSakari Ailus  *		P node name, including a possible unit address
21977b1924a1STobin C. Harding  * - 'x' For printing the address. Equivalent to "%lx".
21987b1924a1STobin C. Harding  *
2199b3ed2321STobin C. Harding  * ** When making changes please also update:
2200b3ed2321STobin C. Harding  *	Documentation/core-api/printk-formats.rst
22019ac6e44eSJoe Perches  *
2202ad67b74dSTobin C. Harding  * Note: The default behaviour (unadorned %p) is to hash the address,
2203ad67b74dSTobin C. Harding  * rendering it useful as a unique identifier.
22044d8a743cSLinus Torvalds  */
2205cf3b429bSJoe Perches static noinline_for_stack
2206cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2207fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
220878a8bf69SLinus Torvalds {
22090fe1ef24SLinus Torvalds 	switch (*fmt) {
22100fe1ef24SLinus Torvalds 	case 'S':
22119ac6e44eSJoe Perches 	case 's':
221204b8eb7aSSergey Senozhatsky 		ptr = dereference_symbol_descriptor(ptr);
221304b8eb7aSSergey Senozhatsky 		/* Fallthrough */
22140f77a8d3SNamhyung Kim 	case 'B':
2215b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
2216332d2e78SLinus Torvalds 	case 'R':
2217c7dabef8SBjorn Helgaas 	case 'r':
2218fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
221931550a16SAndy Shevchenko 	case 'h':
222031550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
2221dbc760bcSTejun Heo 	case 'b':
2222dbc760bcSTejun Heo 		switch (fmt[1]) {
2223dbc760bcSTejun Heo 		case 'l':
2224dbc760bcSTejun Heo 			return bitmap_list_string(buf, end, ptr, spec, fmt);
2225dbc760bcSTejun Heo 		default:
2226dbc760bcSTejun Heo 			return bitmap_string(buf, end, ptr, spec, fmt);
2227dbc760bcSTejun Heo 		}
22288a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
22298a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
223076597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
223176597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
22328a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
22338a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
22348a27f7c9SJoe Perches 					 * 4:	1.2.3.4
22358a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
22368a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
22378a27f7c9SJoe Perches 					 */
22388a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
22398a27f7c9SJoe Perches 					 * 4:	001.002.003.004
22408a27f7c9SJoe Perches 					 * 6:   000102...0f
22418a27f7c9SJoe Perches 					 */
2242f00cc102SPetr Mladek 		return ip_addr_string(buf, end, ptr, spec, fmt);
224371dca95dSAndy Shevchenko 	case 'E':
224471dca95dSAndy Shevchenko 		return escaped_string(buf, end, ptr, spec, fmt);
22459ac6e44eSJoe Perches 	case 'U':
22469ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
22477db6f5fbSJoe Perches 	case 'V':
22483e5903ebSPetr Mladek 		return va_format(buf, end, ptr, spec, fmt);
2249455cd5abSDan Rosenberg 	case 'K':
225057e73442STobin C. Harding 		return restricted_pointer(buf, end, ptr, spec);
2251c8f44affSMichał Mirosław 	case 'N':
2252431bca24SGeert Uytterhoeven 		return netdev_bits(buf, end, ptr, spec, fmt);
22537d799210SStepan Moskovchenko 	case 'a':
22543e5903ebSPetr Mladek 		return address_val(buf, end, ptr, spec, fmt);
22554b6ccca7SAl Viro 	case 'd':
22564b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
22574d42c447SAndy Shevchenko 	case 't':
22584d42c447SAndy Shevchenko 		return time_and_date(buf, end, ptr, spec, fmt);
2259900cca29SGeert Uytterhoeven 	case 'C':
2260900cca29SGeert Uytterhoeven 		return clock(buf, end, ptr, spec, fmt);
22614b6ccca7SAl Viro 	case 'D':
226236594b31SJia He 		return file_dentry_name(buf, end, ptr, spec, fmt);
22631031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
22641031bc58SDmitry Monakhov 	case 'g':
22651031bc58SDmitry Monakhov 		return bdev_name(buf, end, ptr, spec, fmt);
22661031bc58SDmitry Monakhov #endif
22671031bc58SDmitry Monakhov 
2268edf14cdbSVlastimil Babka 	case 'G':
22690b74d4d7SPetr Mladek 		return flags_string(buf, end, ptr, spec, fmt);
2270ce4fecf1SPantelis Antoniou 	case 'O':
227183abc5a7SSakari Ailus 		return device_node_string(buf, end, ptr, spec, fmt + 1);
22723bd32d6aSSakari Ailus 	case 'f':
22733bd32d6aSSakari Ailus 		return fwnode_string(buf, end, ptr, spec, fmt + 1);
22747b1924a1STobin C. Harding 	case 'x':
22757b1924a1STobin C. Harding 		return pointer_string(buf, end, ptr, spec);
227657f5677eSRasmus Villemoes 	case 'e':
227757f5677eSRasmus Villemoes 		/* %pe with a non-ERR_PTR gets treated as plain %p */
227857f5677eSRasmus Villemoes 		if (!IS_ERR(ptr))
227957f5677eSRasmus Villemoes 			break;
228057f5677eSRasmus Villemoes 		return err_ptr(buf, end, ptr, spec);
22810fe1ef24SLinus Torvalds 	}
2282fef20d9cSFrederic Weisbecker 
2283ad67b74dSTobin C. Harding 	/* default is to _not_ leak addresses, hash before printing */
2284ad67b74dSTobin C. Harding 	return ptr_to_id(buf, end, ptr, spec);
2285fef20d9cSFrederic Weisbecker }
2286fef20d9cSFrederic Weisbecker 
2287fef20d9cSFrederic Weisbecker /*
2288fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
2289fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
2290fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
2291fef20d9cSFrederic Weisbecker  * to go on the next call).
2292fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
2293fef20d9cSFrederic Weisbecker  *
2294fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
2295fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
2296fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
22975b5e0928SAlexey Dobriyan  * 'Z' changed to 'z' --adobriyan 2017-01-25
2298fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
2299fef20d9cSFrederic Weisbecker  *
2300fef20d9cSFrederic Weisbecker  * @fmt: the format string
2301fef20d9cSFrederic Weisbecker  * @type of the token returned
2302fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
2303fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
2304fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
2305fef20d9cSFrederic Weisbecker  * @precision: precision of a number
2306fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
2307fef20d9cSFrederic Weisbecker  */
2308cf3b429bSJoe Perches static noinline_for_stack
2309cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
2310fef20d9cSFrederic Weisbecker {
2311fef20d9cSFrederic Weisbecker 	const char *start = fmt;
2312d0484193SRasmus Villemoes 	char qualifier;
2313fef20d9cSFrederic Weisbecker 
2314fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
2315ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
2316fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
2317fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
2318fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
2319fef20d9cSFrederic Weisbecker 		}
2320fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
2321fef20d9cSFrederic Weisbecker 		goto precision;
2322fef20d9cSFrederic Weisbecker 	}
2323fef20d9cSFrederic Weisbecker 
2324fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
2325fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
2326fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
2327fef20d9cSFrederic Weisbecker 			spec->precision = 0;
2328fef20d9cSFrederic Weisbecker 
2329fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
2330fef20d9cSFrederic Weisbecker 		goto qualifier;
2331fef20d9cSFrederic Weisbecker 	}
2332fef20d9cSFrederic Weisbecker 
2333fef20d9cSFrederic Weisbecker 	/* By default */
2334fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
2335fef20d9cSFrederic Weisbecker 
2336fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
2337fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
2338fef20d9cSFrederic Weisbecker 			break;
2339fef20d9cSFrederic Weisbecker 	}
2340fef20d9cSFrederic Weisbecker 
2341fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
2342fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
2343fef20d9cSFrederic Weisbecker 		return fmt - start;
2344fef20d9cSFrederic Weisbecker 
2345fef20d9cSFrederic Weisbecker 	/* Process flags */
2346fef20d9cSFrederic Weisbecker 	spec->flags = 0;
2347fef20d9cSFrederic Weisbecker 
2348fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
2349fef20d9cSFrederic Weisbecker 		bool found = true;
2350fef20d9cSFrederic Weisbecker 
2351fef20d9cSFrederic Weisbecker 		++fmt;
2352fef20d9cSFrederic Weisbecker 
2353fef20d9cSFrederic Weisbecker 		switch (*fmt) {
2354fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
2355fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
2356fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
2357fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
2358fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
2359fef20d9cSFrederic Weisbecker 		default:  found = false;
2360fef20d9cSFrederic Weisbecker 		}
2361fef20d9cSFrederic Weisbecker 
2362fef20d9cSFrederic Weisbecker 		if (!found)
2363fef20d9cSFrederic Weisbecker 			break;
2364fef20d9cSFrederic Weisbecker 	}
2365fef20d9cSFrederic Weisbecker 
2366fef20d9cSFrederic Weisbecker 	/* get field width */
2367fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
2368fef20d9cSFrederic Weisbecker 
2369fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
2370fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
2371fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
2372fef20d9cSFrederic Weisbecker 		/* it's the next argument */
2373ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
2374fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2375fef20d9cSFrederic Weisbecker 	}
2376fef20d9cSFrederic Weisbecker 
2377fef20d9cSFrederic Weisbecker precision:
2378fef20d9cSFrederic Weisbecker 	/* get the precision */
2379fef20d9cSFrederic Weisbecker 	spec->precision = -1;
2380fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
2381fef20d9cSFrederic Weisbecker 		++fmt;
2382fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
2383fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
2384fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
2385fef20d9cSFrederic Weisbecker 				spec->precision = 0;
2386fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
2387fef20d9cSFrederic Weisbecker 			/* it's the next argument */
2388adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
2389fef20d9cSFrederic Weisbecker 			return ++fmt - start;
2390fef20d9cSFrederic Weisbecker 		}
2391fef20d9cSFrederic Weisbecker 	}
2392fef20d9cSFrederic Weisbecker 
2393fef20d9cSFrederic Weisbecker qualifier:
2394fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
2395d0484193SRasmus Villemoes 	qualifier = 0;
239675fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
23975b5e0928SAlexey Dobriyan 	    *fmt == 'z' || *fmt == 't') {
2398d0484193SRasmus Villemoes 		qualifier = *fmt++;
2399d0484193SRasmus Villemoes 		if (unlikely(qualifier == *fmt)) {
2400d0484193SRasmus Villemoes 			if (qualifier == 'l') {
2401d0484193SRasmus Villemoes 				qualifier = 'L';
2402fef20d9cSFrederic Weisbecker 				++fmt;
2403d0484193SRasmus Villemoes 			} else if (qualifier == 'h') {
2404d0484193SRasmus Villemoes 				qualifier = 'H';
2405a4e94ef0SZhaolei 				++fmt;
2406a4e94ef0SZhaolei 			}
2407fef20d9cSFrederic Weisbecker 		}
2408fef20d9cSFrederic Weisbecker 	}
2409fef20d9cSFrederic Weisbecker 
2410fef20d9cSFrederic Weisbecker 	/* default base */
2411fef20d9cSFrederic Weisbecker 	spec->base = 10;
2412fef20d9cSFrederic Weisbecker 	switch (*fmt) {
2413fef20d9cSFrederic Weisbecker 	case 'c':
2414fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
2415fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2416fef20d9cSFrederic Weisbecker 
2417fef20d9cSFrederic Weisbecker 	case 's':
2418fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
2419fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2420fef20d9cSFrederic Weisbecker 
2421fef20d9cSFrederic Weisbecker 	case 'p':
2422fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
2423ffbfed03SRasmus Villemoes 		return ++fmt - start;
2424fef20d9cSFrederic Weisbecker 
2425fef20d9cSFrederic Weisbecker 	case '%':
2426fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
2427fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2428fef20d9cSFrederic Weisbecker 
2429fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
2430fef20d9cSFrederic Weisbecker 	case 'o':
2431fef20d9cSFrederic Weisbecker 		spec->base = 8;
2432fef20d9cSFrederic Weisbecker 		break;
2433fef20d9cSFrederic Weisbecker 
2434fef20d9cSFrederic Weisbecker 	case 'x':
2435fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
24367e6bd6f3SAndy Shevchenko 		/* fall through */
2437fef20d9cSFrederic Weisbecker 
2438fef20d9cSFrederic Weisbecker 	case 'X':
2439fef20d9cSFrederic Weisbecker 		spec->base = 16;
2440fef20d9cSFrederic Weisbecker 		break;
2441fef20d9cSFrederic Weisbecker 
2442fef20d9cSFrederic Weisbecker 	case 'd':
2443fef20d9cSFrederic Weisbecker 	case 'i':
244439e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
2445fef20d9cSFrederic Weisbecker 	case 'u':
2446fef20d9cSFrederic Weisbecker 		break;
2447fef20d9cSFrederic Weisbecker 
2448708d96fdSRyan Mallon 	case 'n':
2449708d96fdSRyan Mallon 		/*
2450b006f19bSRasmus Villemoes 		 * Since %n poses a greater security risk than
2451b006f19bSRasmus Villemoes 		 * utility, treat it as any other invalid or
2452b006f19bSRasmus Villemoes 		 * unsupported format specifier.
2453708d96fdSRyan Mallon 		 */
2454708d96fdSRyan Mallon 		/* Fall-through */
2455708d96fdSRyan Mallon 
2456fef20d9cSFrederic Weisbecker 	default:
2457b006f19bSRasmus Villemoes 		WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2458fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
2459fef20d9cSFrederic Weisbecker 		return fmt - start;
2460fef20d9cSFrederic Weisbecker 	}
2461fef20d9cSFrederic Weisbecker 
2462d0484193SRasmus Villemoes 	if (qualifier == 'L')
2463fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
2464d0484193SRasmus Villemoes 	else if (qualifier == 'l') {
246551be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
246651be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
24675b5e0928SAlexey Dobriyan 	} else if (qualifier == 'z') {
2468fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
2469d0484193SRasmus Villemoes 	} else if (qualifier == 't') {
2470fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
2471d0484193SRasmus Villemoes 	} else if (qualifier == 'H') {
247251be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
247351be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2474d0484193SRasmus Villemoes 	} else if (qualifier == 'h') {
247551be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
247651be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2477fef20d9cSFrederic Weisbecker 	} else {
247851be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
247951be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2480fef20d9cSFrederic Weisbecker 	}
2481fef20d9cSFrederic Weisbecker 
2482fef20d9cSFrederic Weisbecker 	return ++fmt - start;
248378a8bf69SLinus Torvalds }
248478a8bf69SLinus Torvalds 
24854d72ba01SRasmus Villemoes static void
24864d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width)
24874d72ba01SRasmus Villemoes {
24884d72ba01SRasmus Villemoes 	spec->field_width = width;
24894d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
24904d72ba01SRasmus Villemoes 		spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
24914d72ba01SRasmus Villemoes 	}
24924d72ba01SRasmus Villemoes }
24934d72ba01SRasmus Villemoes 
24944d72ba01SRasmus Villemoes static void
24954d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec)
24964d72ba01SRasmus Villemoes {
24974d72ba01SRasmus Villemoes 	spec->precision = prec;
24984d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
24994d72ba01SRasmus Villemoes 		spec->precision = clamp(prec, 0, PRECISION_MAX);
25004d72ba01SRasmus Villemoes 	}
25014d72ba01SRasmus Villemoes }
25024d72ba01SRasmus Villemoes 
25031da177e4SLinus Torvalds /**
25041da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
25051da177e4SLinus Torvalds  * @buf: The buffer to place the result into
25061da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
25071da177e4SLinus Torvalds  * @fmt: The format string to use
25081da177e4SLinus Torvalds  * @args: Arguments for the format string
25091da177e4SLinus Torvalds  *
2510d7ec9a05SRasmus Villemoes  * This function generally follows C99 vsnprintf, but has some
2511d7ec9a05SRasmus Villemoes  * extensions and a few limitations:
2512d7ec9a05SRasmus Villemoes  *
25136cc89134Smchehab@s-opensource.com  *  - ``%n`` is unsupported
25146cc89134Smchehab@s-opensource.com  *  - ``%p*`` is handled by pointer()
251520036fdcSAndi Kleen  *
251627e7c0e8SJonathan Corbet  * See pointer() or Documentation/core-api/printk-formats.rst for more
25175e4ee7b1SMartin Kletzander  * extensive description.
25185e4ee7b1SMartin Kletzander  *
25195e4ee7b1SMartin Kletzander  * **Please update the documentation in both places when making changes**
252080f548e0SAndrew Morton  *
25211da177e4SLinus Torvalds  * The return value is the number of characters which would
25221da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
25231da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
25241da177e4SLinus Torvalds  * number of characters written into @buf as return value
252572fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
25261da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
25271da177e4SLinus Torvalds  * string is truncated.
25281da177e4SLinus Torvalds  *
2529ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
25301da177e4SLinus Torvalds  */
25311da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
25321da177e4SLinus Torvalds {
25331da177e4SLinus Torvalds 	unsigned long long num;
2534d4be151bSAndré Goddard Rosa 	char *str, *end;
2535fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
25361da177e4SLinus Torvalds 
2537f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
2538f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
25392aa2f9e2SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
25401da177e4SLinus Torvalds 		return 0;
25411da177e4SLinus Torvalds 
25421da177e4SLinus Torvalds 	str = buf;
2543f796937aSJeremy Fitzhardinge 	end = buf + size;
25441da177e4SLinus Torvalds 
2545f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
2546f796937aSJeremy Fitzhardinge 	if (end < buf) {
25471da177e4SLinus Torvalds 		end = ((void *)-1);
2548f796937aSJeremy Fitzhardinge 		size = end - buf;
25491da177e4SLinus Torvalds 	}
25501da177e4SLinus Torvalds 
2551fef20d9cSFrederic Weisbecker 	while (*fmt) {
2552fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2553d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2554fef20d9cSFrederic Weisbecker 
2555fef20d9cSFrederic Weisbecker 		fmt += read;
2556fef20d9cSFrederic Weisbecker 
2557fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2558fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2559fef20d9cSFrederic Weisbecker 			int copy = read;
2560fef20d9cSFrederic Weisbecker 			if (str < end) {
2561fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2562fef20d9cSFrederic Weisbecker 					copy = end - str;
2563fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2564fef20d9cSFrederic Weisbecker 			}
2565fef20d9cSFrederic Weisbecker 			str += read;
2566fef20d9cSFrederic Weisbecker 			break;
25671da177e4SLinus Torvalds 		}
25681da177e4SLinus Torvalds 
2569ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
25704d72ba01SRasmus Villemoes 			set_field_width(&spec, va_arg(args, int));
2571fef20d9cSFrederic Weisbecker 			break;
25721da177e4SLinus Torvalds 
2573fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
25744d72ba01SRasmus Villemoes 			set_precision(&spec, va_arg(args, int));
2575fef20d9cSFrederic Weisbecker 			break;
25761da177e4SLinus Torvalds 
2577d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2578d4be151bSAndré Goddard Rosa 			char c;
2579d4be151bSAndré Goddard Rosa 
2580fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2581fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
2582f796937aSJeremy Fitzhardinge 					if (str < end)
25831da177e4SLinus Torvalds 						*str = ' ';
25841da177e4SLinus Torvalds 					++str;
2585fef20d9cSFrederic Weisbecker 
25861da177e4SLinus Torvalds 				}
25871da177e4SLinus Torvalds 			}
25881da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
2589f796937aSJeremy Fitzhardinge 			if (str < end)
25901da177e4SLinus Torvalds 				*str = c;
25911da177e4SLinus Torvalds 			++str;
2592fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
2593f796937aSJeremy Fitzhardinge 				if (str < end)
25941da177e4SLinus Torvalds 					*str = ' ';
25951da177e4SLinus Torvalds 				++str;
25961da177e4SLinus Torvalds 			}
2597fef20d9cSFrederic Weisbecker 			break;
2598d4be151bSAndré Goddard Rosa 		}
25991da177e4SLinus Torvalds 
2600fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
2601fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
2602fef20d9cSFrederic Weisbecker 			break;
26031da177e4SLinus Torvalds 
2604fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2605ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, va_arg(args, void *),
2606fef20d9cSFrederic Weisbecker 				      spec);
2607fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
26084d8a743cSLinus Torvalds 				fmt++;
2609fef20d9cSFrederic Weisbecker 			break;
26101da177e4SLinus Torvalds 
2611fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2612f796937aSJeremy Fitzhardinge 			if (str < end)
26131da177e4SLinus Torvalds 				*str = '%';
26141da177e4SLinus Torvalds 			++str;
26151da177e4SLinus Torvalds 			break;
26161da177e4SLinus Torvalds 
2617fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
2618b006f19bSRasmus Villemoes 			/*
2619b006f19bSRasmus Villemoes 			 * Presumably the arguments passed gcc's type
2620b006f19bSRasmus Villemoes 			 * checking, but there is no safe or sane way
2621b006f19bSRasmus Villemoes 			 * for us to continue parsing the format and
2622b006f19bSRasmus Villemoes 			 * fetching from the va_list; the remaining
2623b006f19bSRasmus Villemoes 			 * specifiers and arguments would be out of
2624b006f19bSRasmus Villemoes 			 * sync.
2625b006f19bSRasmus Villemoes 			 */
2626b006f19bSRasmus Villemoes 			goto out;
2627fef20d9cSFrederic Weisbecker 
2628fef20d9cSFrederic Weisbecker 		default:
2629fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2630fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2631fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
2632fef20d9cSFrederic Weisbecker 				break;
2633fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2634fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
2635fef20d9cSFrederic Weisbecker 				break;
2636fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2637fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
2638fef20d9cSFrederic Weisbecker 				break;
2639fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2640ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
2641ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
2642ef124960SJason Gunthorpe 				else
2643fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
2644fef20d9cSFrederic Weisbecker 				break;
2645fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2646fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
2647fef20d9cSFrederic Weisbecker 				break;
2648a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2649a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
2650a4e94ef0SZhaolei 				break;
2651a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2652a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
2653a4e94ef0SZhaolei 				break;
2654fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2655fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
2656fef20d9cSFrederic Weisbecker 				break;
2657fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2658fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
2659fef20d9cSFrederic Weisbecker 				break;
266039e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
266139e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
2662fef20d9cSFrederic Weisbecker 				break;
2663fef20d9cSFrederic Weisbecker 			default:
2664fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
26651da177e4SLinus Torvalds 			}
2666fef20d9cSFrederic Weisbecker 
2667fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
26681da177e4SLinus Torvalds 		}
2669fef20d9cSFrederic Weisbecker 	}
2670fef20d9cSFrederic Weisbecker 
2671b006f19bSRasmus Villemoes out:
2672f796937aSJeremy Fitzhardinge 	if (size > 0) {
2673f796937aSJeremy Fitzhardinge 		if (str < end)
26741da177e4SLinus Torvalds 			*str = '\0';
2675f796937aSJeremy Fitzhardinge 		else
26760a6047eeSLinus Torvalds 			end[-1] = '\0';
2677f796937aSJeremy Fitzhardinge 	}
2678fef20d9cSFrederic Weisbecker 
2679f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
26801da177e4SLinus Torvalds 	return str-buf;
2681fef20d9cSFrederic Weisbecker 
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
26841da177e4SLinus Torvalds 
26851da177e4SLinus Torvalds /**
26861da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
26871da177e4SLinus Torvalds  * @buf: The buffer to place the result into
26881da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
26891da177e4SLinus Torvalds  * @fmt: The format string to use
26901da177e4SLinus Torvalds  * @args: Arguments for the format string
26911da177e4SLinus Torvalds  *
26921da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
2693b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
26941da177e4SLinus Torvalds  * returns 0.
26951da177e4SLinus Torvalds  *
2696ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
269720036fdcSAndi Kleen  *
269820036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
26991da177e4SLinus Torvalds  */
27001da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
27011da177e4SLinus Torvalds {
27021da177e4SLinus Torvalds 	int i;
27031da177e4SLinus Torvalds 
27041da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
27057b9186f5SAndré Goddard Rosa 
2706b921c69fSAnton Arapov 	if (likely(i < size))
2707b921c69fSAnton Arapov 		return i;
2708b921c69fSAnton Arapov 	if (size != 0)
2709b921c69fSAnton Arapov 		return size - 1;
2710b921c69fSAnton Arapov 	return 0;
27111da177e4SLinus Torvalds }
27121da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds /**
27151da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
27161da177e4SLinus Torvalds  * @buf: The buffer to place the result into
27171da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
27181da177e4SLinus Torvalds  * @fmt: The format string to use
27191da177e4SLinus Torvalds  * @...: Arguments for the format string
27201da177e4SLinus Torvalds  *
27211da177e4SLinus Torvalds  * The return value is the number of characters which would be
27221da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
27231da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
27241da177e4SLinus Torvalds  * @size, the resulting string is truncated.
272520036fdcSAndi Kleen  *
272620036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
27271da177e4SLinus Torvalds  */
27281da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
27291da177e4SLinus Torvalds {
27301da177e4SLinus Torvalds 	va_list args;
27311da177e4SLinus Torvalds 	int i;
27321da177e4SLinus Torvalds 
27331da177e4SLinus Torvalds 	va_start(args, fmt);
27341da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
27351da177e4SLinus Torvalds 	va_end(args);
27367b9186f5SAndré Goddard Rosa 
27371da177e4SLinus Torvalds 	return i;
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
27401da177e4SLinus Torvalds 
27411da177e4SLinus Torvalds /**
27421da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
27431da177e4SLinus Torvalds  * @buf: The buffer to place the result into
27441da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
27451da177e4SLinus Torvalds  * @fmt: The format string to use
27461da177e4SLinus Torvalds  * @...: Arguments for the format string
27471da177e4SLinus Torvalds  *
27481da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
2749b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
27501da177e4SLinus Torvalds  */
27511da177e4SLinus Torvalds 
27521da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
27531da177e4SLinus Torvalds {
27541da177e4SLinus Torvalds 	va_list args;
27551da177e4SLinus Torvalds 	int i;
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds 	va_start(args, fmt);
2758b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
27591da177e4SLinus Torvalds 	va_end(args);
27607b9186f5SAndré Goddard Rosa 
2761b903c0b8SChangli Gao 	return i;
27621da177e4SLinus Torvalds }
27631da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
27641da177e4SLinus Torvalds 
27651da177e4SLinus Torvalds /**
27661da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
27671da177e4SLinus Torvalds  * @buf: The buffer to place the result into
27681da177e4SLinus Torvalds  * @fmt: The format string to use
27691da177e4SLinus Torvalds  * @args: Arguments for the format string
27701da177e4SLinus Torvalds  *
27711da177e4SLinus Torvalds  * The function returns the number of characters written
277272fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
27731da177e4SLinus Torvalds  * buffer overflows.
27741da177e4SLinus Torvalds  *
2775ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
277620036fdcSAndi Kleen  *
277720036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
27781da177e4SLinus Torvalds  */
27791da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
27801da177e4SLinus Torvalds {
27811da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
27821da177e4SLinus Torvalds }
27831da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
27841da177e4SLinus Torvalds 
27851da177e4SLinus Torvalds /**
27861da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
27871da177e4SLinus Torvalds  * @buf: The buffer to place the result into
27881da177e4SLinus Torvalds  * @fmt: The format string to use
27891da177e4SLinus Torvalds  * @...: Arguments for the format string
27901da177e4SLinus Torvalds  *
27911da177e4SLinus Torvalds  * The function returns the number of characters written
279272fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
27931da177e4SLinus Torvalds  * buffer overflows.
279420036fdcSAndi Kleen  *
279520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
27961da177e4SLinus Torvalds  */
27971da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
27981da177e4SLinus Torvalds {
27991da177e4SLinus Torvalds 	va_list args;
28001da177e4SLinus Torvalds 	int i;
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	va_start(args, fmt);
28031da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
28041da177e4SLinus Torvalds 	va_end(args);
28057b9186f5SAndré Goddard Rosa 
28061da177e4SLinus Torvalds 	return i;
28071da177e4SLinus Torvalds }
28081da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
28091da177e4SLinus Torvalds 
28104370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
28114370aa4aSLai Jiangshan /*
28124370aa4aSLai Jiangshan  * bprintf service:
28134370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
28144370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
28154370aa4aSLai Jiangshan  */
28164370aa4aSLai Jiangshan 
28174370aa4aSLai Jiangshan /**
28184370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
28194370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
28204370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
28214370aa4aSLai Jiangshan  * @fmt: The format string to use
28224370aa4aSLai Jiangshan  * @args: Arguments for the format string
28234370aa4aSLai Jiangshan  *
28244370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
2825da3dae54SMasanari Iida  * is skipped.
28264370aa4aSLai Jiangshan  *
28274370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
28284370aa4aSLai Jiangshan  * the given input.
28294370aa4aSLai Jiangshan  *
28304370aa4aSLai Jiangshan  * NOTE:
28314370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
28324370aa4aSLai Jiangshan  * valid for bstr_printf().
28334370aa4aSLai Jiangshan  */
28344370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
28354370aa4aSLai Jiangshan {
2836fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
28374370aa4aSLai Jiangshan 	char *str, *end;
2838841a915dSSteven Rostedt (VMware) 	int width;
28394370aa4aSLai Jiangshan 
28404370aa4aSLai Jiangshan 	str = (char *)bin_buf;
28414370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
28424370aa4aSLai Jiangshan 
28434370aa4aSLai Jiangshan #define save_arg(type)							\
2844841a915dSSteven Rostedt (VMware) ({									\
28454370aa4aSLai Jiangshan 	unsigned long long value;					\
2846841a915dSSteven Rostedt (VMware) 	if (sizeof(type) == 8) {					\
2847841a915dSSteven Rostedt (VMware) 		unsigned long long val8;				\
28484370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
2849841a915dSSteven Rostedt (VMware) 		val8 = va_arg(args, unsigned long long);		\
28504370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
2851841a915dSSteven Rostedt (VMware) 			*(u32 *)str = *(u32 *)&val8;			\
2852841a915dSSteven Rostedt (VMware) 			*(u32 *)(str + 4) = *((u32 *)&val8 + 1);	\
28534370aa4aSLai Jiangshan 		}							\
2854841a915dSSteven Rostedt (VMware) 		value = val8;						\
28554370aa4aSLai Jiangshan 	} else {							\
2856841a915dSSteven Rostedt (VMware) 		unsigned int val4;					\
28574370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
2858841a915dSSteven Rostedt (VMware) 		val4 = va_arg(args, int);				\
28594370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
2860841a915dSSteven Rostedt (VMware) 			*(typeof(type) *)str = (type)(long)val4;	\
2861841a915dSSteven Rostedt (VMware) 		value = (unsigned long long)val4;			\
28624370aa4aSLai Jiangshan 	}								\
28634370aa4aSLai Jiangshan 	str += sizeof(type);						\
2864841a915dSSteven Rostedt (VMware) 	value;								\
2865841a915dSSteven Rostedt (VMware) })
28664370aa4aSLai Jiangshan 
2867fef20d9cSFrederic Weisbecker 	while (*fmt) {
2868d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
28694370aa4aSLai Jiangshan 
2870fef20d9cSFrederic Weisbecker 		fmt += read;
2871fef20d9cSFrederic Weisbecker 
2872fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2873fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
2874d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
2875fef20d9cSFrederic Weisbecker 			break;
2876b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
2877b006f19bSRasmus Villemoes 			goto out;
2878fef20d9cSFrederic Weisbecker 
2879ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2880fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2881841a915dSSteven Rostedt (VMware) 			width = (int)save_arg(int);
2882841a915dSSteven Rostedt (VMware) 			/* Pointers may require the width */
2883841a915dSSteven Rostedt (VMware) 			if (*fmt == 'p')
2884841a915dSSteven Rostedt (VMware) 				set_field_width(&spec, width);
2885fef20d9cSFrederic Weisbecker 			break;
28864370aa4aSLai Jiangshan 
2887fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
28884370aa4aSLai Jiangshan 			save_arg(char);
2889fef20d9cSFrederic Weisbecker 			break;
2890fef20d9cSFrederic Weisbecker 
2891fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
28924370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
28933e5903ebSPetr Mladek 			const char *err_msg;
28944370aa4aSLai Jiangshan 			size_t len;
28956c356634SAndré Goddard Rosa 
28963e5903ebSPetr Mladek 			err_msg = check_pointer_msg(save_str);
28973e5903ebSPetr Mladek 			if (err_msg)
28983e5903ebSPetr Mladek 				save_str = err_msg;
28993e5903ebSPetr Mladek 
29006c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
29016c356634SAndré Goddard Rosa 			if (str + len < end)
29026c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
29036c356634SAndré Goddard Rosa 			str += len;
2904fef20d9cSFrederic Weisbecker 			break;
29054370aa4aSLai Jiangshan 		}
2906fef20d9cSFrederic Weisbecker 
2907fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2908841a915dSSteven Rostedt (VMware) 			/* Dereferenced pointers must be done now */
2909841a915dSSteven Rostedt (VMware) 			switch (*fmt) {
2910841a915dSSteven Rostedt (VMware) 			/* Dereference of functions is still OK */
2911841a915dSSteven Rostedt (VMware) 			case 'S':
2912841a915dSSteven Rostedt (VMware) 			case 's':
29131e6338cfSSteven Rostedt (VMware) 			case 'x':
29141e6338cfSSteven Rostedt (VMware) 			case 'K':
291557f5677eSRasmus Villemoes 			case 'e':
29164370aa4aSLai Jiangshan 				save_arg(void *);
2917841a915dSSteven Rostedt (VMware) 				break;
2918841a915dSSteven Rostedt (VMware) 			default:
2919841a915dSSteven Rostedt (VMware) 				if (!isalnum(*fmt)) {
2920841a915dSSteven Rostedt (VMware) 					save_arg(void *);
2921841a915dSSteven Rostedt (VMware) 					break;
2922841a915dSSteven Rostedt (VMware) 				}
2923841a915dSSteven Rostedt (VMware) 				str = pointer(fmt, str, end, va_arg(args, void *),
2924841a915dSSteven Rostedt (VMware) 					      spec);
2925841a915dSSteven Rostedt (VMware) 				if (str + 1 < end)
2926841a915dSSteven Rostedt (VMware) 					*str++ = '\0';
2927841a915dSSteven Rostedt (VMware) 				else
2928841a915dSSteven Rostedt (VMware) 					end[-1] = '\0'; /* Must be nul terminated */
2929841a915dSSteven Rostedt (VMware) 			}
29304370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2931fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
29324370aa4aSLai Jiangshan 				fmt++;
2933fef20d9cSFrederic Weisbecker 			break;
2934fef20d9cSFrederic Weisbecker 
2935fef20d9cSFrederic Weisbecker 		default:
2936fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2937fef20d9cSFrederic Weisbecker 
2938fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2939fef20d9cSFrederic Weisbecker 				save_arg(long long);
2940fef20d9cSFrederic Weisbecker 				break;
2941fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2942fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2943fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
2944fef20d9cSFrederic Weisbecker 				break;
2945fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2946fef20d9cSFrederic Weisbecker 				save_arg(size_t);
2947fef20d9cSFrederic Weisbecker 				break;
2948fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2949fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
2950fef20d9cSFrederic Weisbecker 				break;
2951a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2952a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2953a4e94ef0SZhaolei 				save_arg(char);
2954a4e94ef0SZhaolei 				break;
2955fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2956fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2957fef20d9cSFrederic Weisbecker 				save_arg(short);
2958fef20d9cSFrederic Weisbecker 				break;
2959fef20d9cSFrederic Weisbecker 			default:
2960fef20d9cSFrederic Weisbecker 				save_arg(int);
2961fef20d9cSFrederic Weisbecker 			}
2962fef20d9cSFrederic Weisbecker 		}
2963fef20d9cSFrederic Weisbecker 	}
2964fef20d9cSFrederic Weisbecker 
2965b006f19bSRasmus Villemoes out:
29667b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2967fef20d9cSFrederic Weisbecker #undef save_arg
29684370aa4aSLai Jiangshan }
29694370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
29704370aa4aSLai Jiangshan 
29714370aa4aSLai Jiangshan /**
29724370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
29734370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
29744370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
29754370aa4aSLai Jiangshan  * @fmt: The format string to use
29764370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
29774370aa4aSLai Jiangshan  *
29784370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
29794370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
29804370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
29814370aa4aSLai Jiangshan  *
29824370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
29830efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
29844370aa4aSLai Jiangshan  *
29854370aa4aSLai Jiangshan  * The return value is the number of characters which would
29864370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
29874370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
29884370aa4aSLai Jiangshan  * number of characters written into @buf as return value
29894370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
29904370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
29914370aa4aSLai Jiangshan  * string is truncated.
29924370aa4aSLai Jiangshan  */
29934370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
29944370aa4aSLai Jiangshan {
2995fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
2996d4be151bSAndré Goddard Rosa 	char *str, *end;
2997d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
29984370aa4aSLai Jiangshan 
2999762abb51SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
30004370aa4aSLai Jiangshan 		return 0;
30014370aa4aSLai Jiangshan 
30024370aa4aSLai Jiangshan 	str = buf;
30034370aa4aSLai Jiangshan 	end = buf + size;
30044370aa4aSLai Jiangshan 
30054370aa4aSLai Jiangshan #define get_arg(type)							\
30064370aa4aSLai Jiangshan ({									\
30074370aa4aSLai Jiangshan 	typeof(type) value;						\
30084370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
30094370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
30104370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
30114370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
30124370aa4aSLai Jiangshan 	} else {							\
30134370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
30144370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
30154370aa4aSLai Jiangshan 	}								\
30164370aa4aSLai Jiangshan 	args += sizeof(type);						\
30174370aa4aSLai Jiangshan 	value;								\
30184370aa4aSLai Jiangshan })
30194370aa4aSLai Jiangshan 
30204370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
30214370aa4aSLai Jiangshan 	if (end < buf) {
30224370aa4aSLai Jiangshan 		end = ((void *)-1);
30234370aa4aSLai Jiangshan 		size = end - buf;
30244370aa4aSLai Jiangshan 	}
30254370aa4aSLai Jiangshan 
3026fef20d9cSFrederic Weisbecker 	while (*fmt) {
3027fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
3028d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
3029fef20d9cSFrederic Weisbecker 
3030fef20d9cSFrederic Weisbecker 		fmt += read;
3031fef20d9cSFrederic Weisbecker 
3032fef20d9cSFrederic Weisbecker 		switch (spec.type) {
3033fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
3034fef20d9cSFrederic Weisbecker 			int copy = read;
3035fef20d9cSFrederic Weisbecker 			if (str < end) {
3036fef20d9cSFrederic Weisbecker 				if (copy > end - str)
3037fef20d9cSFrederic Weisbecker 					copy = end - str;
3038fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
3039fef20d9cSFrederic Weisbecker 			}
3040fef20d9cSFrederic Weisbecker 			str += read;
3041fef20d9cSFrederic Weisbecker 			break;
30424370aa4aSLai Jiangshan 		}
30434370aa4aSLai Jiangshan 
3044ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
30454d72ba01SRasmus Villemoes 			set_field_width(&spec, get_arg(int));
3046fef20d9cSFrederic Weisbecker 			break;
30474370aa4aSLai Jiangshan 
3048fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
30494d72ba01SRasmus Villemoes 			set_precision(&spec, get_arg(int));
3050fef20d9cSFrederic Weisbecker 			break;
30514370aa4aSLai Jiangshan 
3052d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
3053d4be151bSAndré Goddard Rosa 			char c;
3054d4be151bSAndré Goddard Rosa 
3055fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
3056fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
30574370aa4aSLai Jiangshan 					if (str < end)
30584370aa4aSLai Jiangshan 						*str = ' ';
30594370aa4aSLai Jiangshan 					++str;
30604370aa4aSLai Jiangshan 				}
30614370aa4aSLai Jiangshan 			}
30624370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
30634370aa4aSLai Jiangshan 			if (str < end)
30644370aa4aSLai Jiangshan 				*str = c;
30654370aa4aSLai Jiangshan 			++str;
3066fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
30674370aa4aSLai Jiangshan 				if (str < end)
30684370aa4aSLai Jiangshan 					*str = ' ';
30694370aa4aSLai Jiangshan 				++str;
30704370aa4aSLai Jiangshan 			}
3071fef20d9cSFrederic Weisbecker 			break;
3072d4be151bSAndré Goddard Rosa 		}
30734370aa4aSLai Jiangshan 
3074fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
30754370aa4aSLai Jiangshan 			const char *str_arg = args;
3076d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
3077fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
3078fef20d9cSFrederic Weisbecker 			break;
30794370aa4aSLai Jiangshan 		}
30804370aa4aSLai Jiangshan 
3081841a915dSSteven Rostedt (VMware) 		case FORMAT_TYPE_PTR: {
3082841a915dSSteven Rostedt (VMware) 			bool process = false;
3083841a915dSSteven Rostedt (VMware) 			int copy, len;
3084841a915dSSteven Rostedt (VMware) 			/* Non function dereferences were already done */
3085841a915dSSteven Rostedt (VMware) 			switch (*fmt) {
3086841a915dSSteven Rostedt (VMware) 			case 'S':
3087841a915dSSteven Rostedt (VMware) 			case 's':
3088841a915dSSteven Rostedt (VMware) 			case 'F':
3089841a915dSSteven Rostedt (VMware) 			case 'f':
30901e6338cfSSteven Rostedt (VMware) 			case 'x':
30911e6338cfSSteven Rostedt (VMware) 			case 'K':
309257f5677eSRasmus Villemoes 			case 'e':
3093841a915dSSteven Rostedt (VMware) 				process = true;
3094841a915dSSteven Rostedt (VMware) 				break;
3095841a915dSSteven Rostedt (VMware) 			default:
3096841a915dSSteven Rostedt (VMware) 				if (!isalnum(*fmt)) {
3097841a915dSSteven Rostedt (VMware) 					process = true;
3098841a915dSSteven Rostedt (VMware) 					break;
3099841a915dSSteven Rostedt (VMware) 				}
3100841a915dSSteven Rostedt (VMware) 				/* Pointer dereference was already processed */
3101841a915dSSteven Rostedt (VMware) 				if (str < end) {
3102841a915dSSteven Rostedt (VMware) 					len = copy = strlen(args);
3103841a915dSSteven Rostedt (VMware) 					if (copy > end - str)
3104841a915dSSteven Rostedt (VMware) 						copy = end - str;
3105841a915dSSteven Rostedt (VMware) 					memcpy(str, args, copy);
3106841a915dSSteven Rostedt (VMware) 					str += len;
310762165600SSteven Rostedt (VMware) 					args += len + 1;
3108841a915dSSteven Rostedt (VMware) 				}
3109841a915dSSteven Rostedt (VMware) 			}
3110841a915dSSteven Rostedt (VMware) 			if (process)
3111ffbfed03SRasmus Villemoes 				str = pointer(fmt, str, end, get_arg(void *), spec);
3112841a915dSSteven Rostedt (VMware) 
3113fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
31144370aa4aSLai Jiangshan 				fmt++;
3115fef20d9cSFrederic Weisbecker 			break;
3116841a915dSSteven Rostedt (VMware) 		}
31174370aa4aSLai Jiangshan 
3118fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
31194370aa4aSLai Jiangshan 			if (str < end)
31204370aa4aSLai Jiangshan 				*str = '%';
31214370aa4aSLai Jiangshan 			++str;
3122fef20d9cSFrederic Weisbecker 			break;
3123fef20d9cSFrederic Weisbecker 
3124b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
3125b006f19bSRasmus Villemoes 			goto out;
3126b006f19bSRasmus Villemoes 
3127d4be151bSAndré Goddard Rosa 		default: {
3128d4be151bSAndré Goddard Rosa 			unsigned long long num;
3129d4be151bSAndré Goddard Rosa 
3130fef20d9cSFrederic Weisbecker 			switch (spec.type) {
3131fef20d9cSFrederic Weisbecker 
3132fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
31334370aa4aSLai Jiangshan 				num = get_arg(long long);
3134fef20d9cSFrederic Weisbecker 				break;
3135fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
3136fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
3137fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
3138fef20d9cSFrederic Weisbecker 				break;
3139fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
31404370aa4aSLai Jiangshan 				num = get_arg(size_t);
3141fef20d9cSFrederic Weisbecker 				break;
3142fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
31434370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
3144fef20d9cSFrederic Weisbecker 				break;
3145a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
3146a4e94ef0SZhaolei 				num = get_arg(unsigned char);
3147a4e94ef0SZhaolei 				break;
3148a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
3149a4e94ef0SZhaolei 				num = get_arg(signed char);
3150a4e94ef0SZhaolei 				break;
3151fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
3152fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
3153fef20d9cSFrederic Weisbecker 				break;
3154fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
3155fef20d9cSFrederic Weisbecker 				num = get_arg(short);
3156fef20d9cSFrederic Weisbecker 				break;
3157fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
31584370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
3159fef20d9cSFrederic Weisbecker 				break;
3160fef20d9cSFrederic Weisbecker 			default:
3161fef20d9cSFrederic Weisbecker 				num = get_arg(int);
31624370aa4aSLai Jiangshan 			}
3163fef20d9cSFrederic Weisbecker 
3164fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
3165d4be151bSAndré Goddard Rosa 		} /* default: */
3166d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
3167d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
3168fef20d9cSFrederic Weisbecker 
3169b006f19bSRasmus Villemoes out:
31704370aa4aSLai Jiangshan 	if (size > 0) {
31714370aa4aSLai Jiangshan 		if (str < end)
31724370aa4aSLai Jiangshan 			*str = '\0';
31734370aa4aSLai Jiangshan 		else
31744370aa4aSLai Jiangshan 			end[-1] = '\0';
31754370aa4aSLai Jiangshan 	}
3176fef20d9cSFrederic Weisbecker 
31774370aa4aSLai Jiangshan #undef get_arg
31784370aa4aSLai Jiangshan 
31794370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
31804370aa4aSLai Jiangshan 	return str - buf;
31814370aa4aSLai Jiangshan }
31824370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
31834370aa4aSLai Jiangshan 
31844370aa4aSLai Jiangshan /**
31854370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
31864370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
31874370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
31884370aa4aSLai Jiangshan  * @fmt: The format string to use
31894370aa4aSLai Jiangshan  * @...: Arguments for the format string
31904370aa4aSLai Jiangshan  *
31914370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
31924370aa4aSLai Jiangshan  * into @bin_buf.
31934370aa4aSLai Jiangshan  */
31944370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
31954370aa4aSLai Jiangshan {
31964370aa4aSLai Jiangshan 	va_list args;
31974370aa4aSLai Jiangshan 	int ret;
31984370aa4aSLai Jiangshan 
31994370aa4aSLai Jiangshan 	va_start(args, fmt);
32004370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
32014370aa4aSLai Jiangshan 	va_end(args);
32027b9186f5SAndré Goddard Rosa 
32034370aa4aSLai Jiangshan 	return ret;
32044370aa4aSLai Jiangshan }
32054370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
32064370aa4aSLai Jiangshan 
32074370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
32084370aa4aSLai Jiangshan 
32091da177e4SLinus Torvalds /**
32101da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
32111da177e4SLinus Torvalds  * @buf:	input buffer
32121da177e4SLinus Torvalds  * @fmt:	format of buffer
32131da177e4SLinus Torvalds  * @args:	arguments
32141da177e4SLinus Torvalds  */
32151da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
32161da177e4SLinus Torvalds {
32171da177e4SLinus Torvalds 	const char *str = buf;
32181da177e4SLinus Torvalds 	char *next;
32191da177e4SLinus Torvalds 	char digit;
32201da177e4SLinus Torvalds 	int num = 0;
3221ef0658f3SJoe Perches 	u8 qualifier;
322253809751SJan Beulich 	unsigned int base;
322353809751SJan Beulich 	union {
322453809751SJan Beulich 		long long s;
322553809751SJan Beulich 		unsigned long long u;
322653809751SJan Beulich 	} val;
3227ef0658f3SJoe Perches 	s16 field_width;
3228d4be151bSAndré Goddard Rosa 	bool is_sign;
32291da177e4SLinus Torvalds 
3230da99075cSJan Beulich 	while (*fmt) {
32311da177e4SLinus Torvalds 		/* skip any white space in format */
32321da177e4SLinus Torvalds 		/* white space in format matchs any amount of
32331da177e4SLinus Torvalds 		 * white space, including none, in the input.
32341da177e4SLinus Torvalds 		 */
32351da177e4SLinus Torvalds 		if (isspace(*fmt)) {
3236e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
3237e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
32381da177e4SLinus Torvalds 		}
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
32411da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
32421da177e4SLinus Torvalds 			if (*fmt++ != *str++)
32431da177e4SLinus Torvalds 				break;
32441da177e4SLinus Torvalds 			continue;
32451da177e4SLinus Torvalds 		}
32461da177e4SLinus Torvalds 
32471da177e4SLinus Torvalds 		if (!*fmt)
32481da177e4SLinus Torvalds 			break;
32491da177e4SLinus Torvalds 		++fmt;
32501da177e4SLinus Torvalds 
32511da177e4SLinus Torvalds 		/* skip this conversion.
32521da177e4SLinus Torvalds 		 * advance both strings to next white space
32531da177e4SLinus Torvalds 		 */
32541da177e4SLinus Torvalds 		if (*fmt == '*') {
3255da99075cSJan Beulich 			if (!*str)
3256da99075cSJan Beulich 				break;
3257f9310b2fSJessica Yu 			while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3258f9310b2fSJessica Yu 				/* '%*[' not yet supported, invalid format */
3259f9310b2fSJessica Yu 				if (*fmt == '[')
3260f9310b2fSJessica Yu 					return num;
32611da177e4SLinus Torvalds 				fmt++;
3262f9310b2fSJessica Yu 			}
32631da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
32641da177e4SLinus Torvalds 				str++;
32651da177e4SLinus Torvalds 			continue;
32661da177e4SLinus Torvalds 		}
32671da177e4SLinus Torvalds 
32681da177e4SLinus Torvalds 		/* get field width */
32691da177e4SLinus Torvalds 		field_width = -1;
327053809751SJan Beulich 		if (isdigit(*fmt)) {
32711da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
327253809751SJan Beulich 			if (field_width <= 0)
327353809751SJan Beulich 				break;
327453809751SJan Beulich 		}
32751da177e4SLinus Torvalds 
32761da177e4SLinus Torvalds 		/* get conversion qualifier */
32771da177e4SLinus Torvalds 		qualifier = -1;
327875fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
32795b5e0928SAlexey Dobriyan 		    *fmt == 'z') {
32801da177e4SLinus Torvalds 			qualifier = *fmt++;
32811da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
32821da177e4SLinus Torvalds 				if (qualifier == 'h') {
32831da177e4SLinus Torvalds 					qualifier = 'H';
32841da177e4SLinus Torvalds 					fmt++;
32851da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
32861da177e4SLinus Torvalds 					qualifier = 'L';
32871da177e4SLinus Torvalds 					fmt++;
32881da177e4SLinus Torvalds 				}
32891da177e4SLinus Torvalds 			}
32901da177e4SLinus Torvalds 		}
32911da177e4SLinus Torvalds 
3292da99075cSJan Beulich 		if (!*fmt)
3293da99075cSJan Beulich 			break;
3294da99075cSJan Beulich 
3295da99075cSJan Beulich 		if (*fmt == 'n') {
3296da99075cSJan Beulich 			/* return number of characters read so far */
3297da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
3298da99075cSJan Beulich 			++fmt;
3299da99075cSJan Beulich 			continue;
3300da99075cSJan Beulich 		}
3301da99075cSJan Beulich 
3302da99075cSJan Beulich 		if (!*str)
33031da177e4SLinus Torvalds 			break;
33041da177e4SLinus Torvalds 
3305d4be151bSAndré Goddard Rosa 		base = 10;
33063f623ebaSFabian Frederick 		is_sign = false;
3307d4be151bSAndré Goddard Rosa 
33081da177e4SLinus Torvalds 		switch (*fmt++) {
33091da177e4SLinus Torvalds 		case 'c':
33101da177e4SLinus Torvalds 		{
33111da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
33121da177e4SLinus Torvalds 			if (field_width == -1)
33131da177e4SLinus Torvalds 				field_width = 1;
33141da177e4SLinus Torvalds 			do {
33151da177e4SLinus Torvalds 				*s++ = *str++;
33161da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
33171da177e4SLinus Torvalds 			num++;
33181da177e4SLinus Torvalds 		}
33191da177e4SLinus Torvalds 		continue;
33201da177e4SLinus Torvalds 		case 's':
33211da177e4SLinus Torvalds 		{
33221da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
33231da177e4SLinus Torvalds 			if (field_width == -1)
33244be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
33251da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
3326e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
33271da177e4SLinus Torvalds 
33281da177e4SLinus Torvalds 			/* now copy until next white space */
33297b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
33301da177e4SLinus Torvalds 				*s++ = *str++;
33311da177e4SLinus Torvalds 			*s = '\0';
33321da177e4SLinus Torvalds 			num++;
33331da177e4SLinus Torvalds 		}
33341da177e4SLinus Torvalds 		continue;
3335f9310b2fSJessica Yu 		/*
3336f9310b2fSJessica Yu 		 * Warning: This implementation of the '[' conversion specifier
3337f9310b2fSJessica Yu 		 * deviates from its glibc counterpart in the following ways:
3338f9310b2fSJessica Yu 		 * (1) It does NOT support ranges i.e. '-' is NOT a special
3339f9310b2fSJessica Yu 		 *     character
3340f9310b2fSJessica Yu 		 * (2) It cannot match the closing bracket ']' itself
3341f9310b2fSJessica Yu 		 * (3) A field width is required
3342f9310b2fSJessica Yu 		 * (4) '%*[' (discard matching input) is currently not supported
3343f9310b2fSJessica Yu 		 *
3344f9310b2fSJessica Yu 		 * Example usage:
3345f9310b2fSJessica Yu 		 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3346f9310b2fSJessica Yu 		 *		buf1, buf2, buf3);
3347f9310b2fSJessica Yu 		 * if (ret < 3)
3348f9310b2fSJessica Yu 		 *    // etc..
3349f9310b2fSJessica Yu 		 */
3350f9310b2fSJessica Yu 		case '[':
3351f9310b2fSJessica Yu 		{
3352f9310b2fSJessica Yu 			char *s = (char *)va_arg(args, char *);
3353f9310b2fSJessica Yu 			DECLARE_BITMAP(set, 256) = {0};
3354f9310b2fSJessica Yu 			unsigned int len = 0;
3355f9310b2fSJessica Yu 			bool negate = (*fmt == '^');
3356f9310b2fSJessica Yu 
3357f9310b2fSJessica Yu 			/* field width is required */
3358f9310b2fSJessica Yu 			if (field_width == -1)
3359f9310b2fSJessica Yu 				return num;
3360f9310b2fSJessica Yu 
3361f9310b2fSJessica Yu 			if (negate)
3362f9310b2fSJessica Yu 				++fmt;
3363f9310b2fSJessica Yu 
3364f9310b2fSJessica Yu 			for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3365f9310b2fSJessica Yu 				set_bit((u8)*fmt, set);
3366f9310b2fSJessica Yu 
3367f9310b2fSJessica Yu 			/* no ']' or no character set found */
3368f9310b2fSJessica Yu 			if (!*fmt || !len)
3369f9310b2fSJessica Yu 				return num;
3370f9310b2fSJessica Yu 			++fmt;
3371f9310b2fSJessica Yu 
3372f9310b2fSJessica Yu 			if (negate) {
3373f9310b2fSJessica Yu 				bitmap_complement(set, set, 256);
3374f9310b2fSJessica Yu 				/* exclude null '\0' byte */
3375f9310b2fSJessica Yu 				clear_bit(0, set);
3376f9310b2fSJessica Yu 			}
3377f9310b2fSJessica Yu 
3378f9310b2fSJessica Yu 			/* match must be non-empty */
3379f9310b2fSJessica Yu 			if (!test_bit((u8)*str, set))
3380f9310b2fSJessica Yu 				return num;
3381f9310b2fSJessica Yu 
3382f9310b2fSJessica Yu 			while (test_bit((u8)*str, set) && field_width--)
3383f9310b2fSJessica Yu 				*s++ = *str++;
3384f9310b2fSJessica Yu 			*s = '\0';
3385f9310b2fSJessica Yu 			++num;
3386f9310b2fSJessica Yu 		}
3387f9310b2fSJessica Yu 		continue;
33881da177e4SLinus Torvalds 		case 'o':
33891da177e4SLinus Torvalds 			base = 8;
33901da177e4SLinus Torvalds 			break;
33911da177e4SLinus Torvalds 		case 'x':
33921da177e4SLinus Torvalds 		case 'X':
33931da177e4SLinus Torvalds 			base = 16;
33941da177e4SLinus Torvalds 			break;
33951da177e4SLinus Torvalds 		case 'i':
33961da177e4SLinus Torvalds 			base = 0;
33977e6bd6f3SAndy Shevchenko 			/* fall through */
33981da177e4SLinus Torvalds 		case 'd':
33993f623ebaSFabian Frederick 			is_sign = true;
34007e6bd6f3SAndy Shevchenko 			/* fall through */
34011da177e4SLinus Torvalds 		case 'u':
34021da177e4SLinus Torvalds 			break;
34031da177e4SLinus Torvalds 		case '%':
34041da177e4SLinus Torvalds 			/* looking for '%' in str */
34051da177e4SLinus Torvalds 			if (*str++ != '%')
34061da177e4SLinus Torvalds 				return num;
34071da177e4SLinus Torvalds 			continue;
34081da177e4SLinus Torvalds 		default:
34091da177e4SLinus Torvalds 			/* invalid format; stop here */
34101da177e4SLinus Torvalds 			return num;
34111da177e4SLinus Torvalds 		}
34121da177e4SLinus Torvalds 
34131da177e4SLinus Torvalds 		/* have some sort of integer conversion.
34141da177e4SLinus Torvalds 		 * first, skip white space in buffer.
34151da177e4SLinus Torvalds 		 */
3416e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
34171da177e4SLinus Torvalds 
34181da177e4SLinus Torvalds 		digit = *str;
34191da177e4SLinus Torvalds 		if (is_sign && digit == '-')
34201da177e4SLinus Torvalds 			digit = *(str + 1);
34211da177e4SLinus Torvalds 
34221da177e4SLinus Torvalds 		if (!digit
34231da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
34241da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
34251da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
34261da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
34271da177e4SLinus Torvalds 			break;
34281da177e4SLinus Torvalds 
342953809751SJan Beulich 		if (is_sign)
343053809751SJan Beulich 			val.s = qualifier != 'L' ?
343153809751SJan Beulich 				simple_strtol(str, &next, base) :
343253809751SJan Beulich 				simple_strtoll(str, &next, base);
343353809751SJan Beulich 		else
343453809751SJan Beulich 			val.u = qualifier != 'L' ?
343553809751SJan Beulich 				simple_strtoul(str, &next, base) :
343653809751SJan Beulich 				simple_strtoull(str, &next, base);
343753809751SJan Beulich 
343853809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
343953809751SJan Beulich 			if (base == 0)
344053809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
344153809751SJan Beulich 			while (next - str > field_width) {
344253809751SJan Beulich 				if (is_sign)
344353809751SJan Beulich 					val.s = div_s64(val.s, base);
344453809751SJan Beulich 				else
344553809751SJan Beulich 					val.u = div_u64(val.u, base);
344653809751SJan Beulich 				--next;
344753809751SJan Beulich 			}
344853809751SJan Beulich 		}
344953809751SJan Beulich 
34501da177e4SLinus Torvalds 		switch (qualifier) {
34511da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
345253809751SJan Beulich 			if (is_sign)
345353809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
345453809751SJan Beulich 			else
345553809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
34561da177e4SLinus Torvalds 			break;
34571da177e4SLinus Torvalds 		case 'h':
345853809751SJan Beulich 			if (is_sign)
345953809751SJan Beulich 				*va_arg(args, short *) = val.s;
346053809751SJan Beulich 			else
346153809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
34621da177e4SLinus Torvalds 			break;
34631da177e4SLinus Torvalds 		case 'l':
346453809751SJan Beulich 			if (is_sign)
346553809751SJan Beulich 				*va_arg(args, long *) = val.s;
346653809751SJan Beulich 			else
346753809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
34681da177e4SLinus Torvalds 			break;
34691da177e4SLinus Torvalds 		case 'L':
347053809751SJan Beulich 			if (is_sign)
347153809751SJan Beulich 				*va_arg(args, long long *) = val.s;
347253809751SJan Beulich 			else
347353809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
34741da177e4SLinus Torvalds 			break;
34751da177e4SLinus Torvalds 		case 'z':
347653809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
34771da177e4SLinus Torvalds 			break;
34781da177e4SLinus Torvalds 		default:
347953809751SJan Beulich 			if (is_sign)
348053809751SJan Beulich 				*va_arg(args, int *) = val.s;
348153809751SJan Beulich 			else
348253809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
34831da177e4SLinus Torvalds 			break;
34841da177e4SLinus Torvalds 		}
34851da177e4SLinus Torvalds 		num++;
34861da177e4SLinus Torvalds 
34871da177e4SLinus Torvalds 		if (!next)
34881da177e4SLinus Torvalds 			break;
34891da177e4SLinus Torvalds 		str = next;
34901da177e4SLinus Torvalds 	}
3491c6b40d16SJohannes Berg 
34921da177e4SLinus Torvalds 	return num;
34931da177e4SLinus Torvalds }
34941da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
34951da177e4SLinus Torvalds 
34961da177e4SLinus Torvalds /**
34971da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
34981da177e4SLinus Torvalds  * @buf:	input buffer
34991da177e4SLinus Torvalds  * @fmt:	formatting of buffer
35001da177e4SLinus Torvalds  * @...:	resulting arguments
35011da177e4SLinus Torvalds  */
35021da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
35031da177e4SLinus Torvalds {
35041da177e4SLinus Torvalds 	va_list args;
35051da177e4SLinus Torvalds 	int i;
35061da177e4SLinus Torvalds 
35071da177e4SLinus Torvalds 	va_start(args, fmt);
35081da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
35091da177e4SLinus Torvalds 	va_end(args);
35107b9186f5SAndré Goddard Rosa 
35111da177e4SLinus Torvalds 	return i;
35121da177e4SLinus Torvalds }
35131da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
3514