xref: /openbmc/linux/lib/vsprintf.c (revision af612e43de6d27df51a7636229be7d33fe3ab7b5)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/lib/vsprintf.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds  * Wirzenius wrote this portably, Torvalds fucked it up :-)
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds /*
141da177e4SLinus Torvalds  * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
151da177e4SLinus Torvalds  * - changed to provide snprintf and vsnprintf functions
161da177e4SLinus Torvalds  * So Feb  1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
171da177e4SLinus Torvalds  * - scnprintf and vscnprintf
181da177e4SLinus Torvalds  */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <stdarg.h>
21ef27ac18SRasmus Villemoes #include <linux/build_bug.h>
220d1d7a55SStephen Boyd #include <linux/clk.h>
23900cca29SGeert Uytterhoeven #include <linux/clk-provider.h>
2457f5677eSRasmus Villemoes #include <linux/errname.h>
258bc3bcc9SPaul Gortmaker #include <linux/module.h>	/* for KSYM_SYMBOL_LEN */
261da177e4SLinus Torvalds #include <linux/types.h>
271da177e4SLinus Torvalds #include <linux/string.h>
281da177e4SLinus Torvalds #include <linux/ctype.h>
291da177e4SLinus Torvalds #include <linux/kernel.h>
300fe1ef24SLinus Torvalds #include <linux/kallsyms.h>
3153809751SJan Beulich #include <linux/math64.h>
320fe1ef24SLinus Torvalds #include <linux/uaccess.h>
33332d2e78SLinus Torvalds #include <linux/ioport.h>
344b6ccca7SAl Viro #include <linux/dcache.h>
35312b4e22SRyan Mallon #include <linux/cred.h>
364d42c447SAndy Shevchenko #include <linux/rtc.h>
377daac5b2SAndy Shevchenko #include <linux/time.h>
382b1b0d66SAndy Shevchenko #include <linux/uuid.h>
39ce4fecf1SPantelis Antoniou #include <linux/of.h>
408a27f7c9SJoe Perches #include <net/addrconf.h>
41ad67b74dSTobin C. Harding #include <linux/siphash.h>
42ad67b74dSTobin C. Harding #include <linux/compiler.h>
43a92eb762SSakari Ailus #include <linux/property.h>
441031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
451031bc58SDmitry Monakhov #include <linux/blkdev.h>
461031bc58SDmitry Monakhov #endif
471da177e4SLinus Torvalds 
48edf14cdbSVlastimil Babka #include "../mm/internal.h"	/* For the trace_print_flags arrays */
49edf14cdbSVlastimil Babka 
504e57b681STim Schmielau #include <asm/page.h>		/* for PAGE_SIZE */
517c43d9a3SRasmus Villemoes #include <asm/byteorder.h>	/* cpu_to_le16 */
521da177e4SLinus Torvalds 
5371dca95dSAndy Shevchenko #include <linux/string_helpers.h>
541dff46d6SAlexey Dobriyan #include "kstrtox.h"
55aa46a63eSHarvey Harrison 
561da177e4SLinus Torvalds /**
571da177e4SLinus Torvalds  * simple_strtoull - convert a string to an unsigned long long
581da177e4SLinus Torvalds  * @cp: The start of the string
591da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
601da177e4SLinus Torvalds  * @base: The number base to use
61462e4711SEldad Zack  *
62e8cc2b97SAndy Shevchenko  * This function has caveats. Please use kstrtoull instead.
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
651da177e4SLinus Torvalds {
661dff46d6SAlexey Dobriyan 	unsigned long long result;
671dff46d6SAlexey Dobriyan 	unsigned int rv;
681da177e4SLinus Torvalds 
691dff46d6SAlexey Dobriyan 	cp = _parse_integer_fixup_radix(cp, &base);
701dff46d6SAlexey Dobriyan 	rv = _parse_integer(cp, base, &result);
711dff46d6SAlexey Dobriyan 	/* FIXME */
721dff46d6SAlexey Dobriyan 	cp += (rv & ~KSTRTOX_OVERFLOW);
73aa46a63eSHarvey Harrison 
741da177e4SLinus Torvalds 	if (endp)
751da177e4SLinus Torvalds 		*endp = (char *)cp;
767b9186f5SAndré Goddard Rosa 
771da177e4SLinus Torvalds 	return result;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /**
82922ac25cSAndré Goddard Rosa  * simple_strtoul - convert a string to an unsigned long
83922ac25cSAndré Goddard Rosa  * @cp: The start of the string
84922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
85922ac25cSAndré Goddard Rosa  * @base: The number base to use
86462e4711SEldad Zack  *
87e8cc2b97SAndy Shevchenko  * This function has caveats. Please use kstrtoul instead.
88922ac25cSAndré Goddard Rosa  */
89922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
90922ac25cSAndré Goddard Rosa {
91922ac25cSAndré Goddard Rosa 	return simple_strtoull(cp, endp, base);
92922ac25cSAndré Goddard Rosa }
93922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
94922ac25cSAndré Goddard Rosa 
95922ac25cSAndré Goddard Rosa /**
96922ac25cSAndré Goddard Rosa  * simple_strtol - convert a string to a signed long
97922ac25cSAndré Goddard Rosa  * @cp: The start of the string
98922ac25cSAndré Goddard Rosa  * @endp: A pointer to the end of the parsed string will be placed here
99922ac25cSAndré Goddard Rosa  * @base: The number base to use
100462e4711SEldad Zack  *
101e8cc2b97SAndy Shevchenko  * This function has caveats. Please use kstrtol instead.
102922ac25cSAndré Goddard Rosa  */
103922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
104922ac25cSAndré Goddard Rosa {
105922ac25cSAndré Goddard Rosa 	if (*cp == '-')
106922ac25cSAndré Goddard Rosa 		return -simple_strtoul(cp + 1, endp, base);
107922ac25cSAndré Goddard Rosa 
108922ac25cSAndré Goddard Rosa 	return simple_strtoul(cp, endp, base);
109922ac25cSAndré Goddard Rosa }
110922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
111922ac25cSAndré Goddard Rosa 
112922ac25cSAndré Goddard Rosa /**
1131da177e4SLinus Torvalds  * simple_strtoll - convert a string to a signed long long
1141da177e4SLinus Torvalds  * @cp: The start of the string
1151da177e4SLinus Torvalds  * @endp: A pointer to the end of the parsed string will be placed here
1161da177e4SLinus Torvalds  * @base: The number base to use
117462e4711SEldad Zack  *
118e8cc2b97SAndy Shevchenko  * This function has caveats. Please use kstrtoll instead.
1191da177e4SLinus Torvalds  */
1201da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	if (*cp == '-')
1231da177e4SLinus Torvalds 		return -simple_strtoull(cp + 1, endp, base);
1247b9186f5SAndré Goddard Rosa 
1251da177e4SLinus Torvalds 	return simple_strtoull(cp, endp, base);
1261da177e4SLinus Torvalds }
12798d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1281da177e4SLinus Torvalds 
129cf3b429bSJoe Perches static noinline_for_stack
130cf3b429bSJoe Perches int skip_atoi(const char **s)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	int i = 0;
1331da177e4SLinus Torvalds 
13443e5b666SRasmus Villemoes 	do {
1351da177e4SLinus Torvalds 		i = i*10 + *((*s)++) - '0';
13643e5b666SRasmus Villemoes 	} while (isdigit(**s));
1377b9186f5SAndré Goddard Rosa 
1381da177e4SLinus Torvalds 	return i;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1417c43d9a3SRasmus Villemoes /*
1427c43d9a3SRasmus Villemoes  * Decimal conversion is by far the most typical, and is used for
1437c43d9a3SRasmus Villemoes  * /proc and /sys data. This directly impacts e.g. top performance
1447c43d9a3SRasmus Villemoes  * with many processes running. We optimize it for speed by emitting
1457c43d9a3SRasmus Villemoes  * two characters at a time, using a 200 byte lookup table. This
1467c43d9a3SRasmus Villemoes  * roughly halves the number of multiplications compared to computing
1477c43d9a3SRasmus Villemoes  * the digits one at a time. Implementation strongly inspired by the
1487c43d9a3SRasmus Villemoes  * previous version, which in turn used ideas described at
1497c43d9a3SRasmus Villemoes  * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
1507c43d9a3SRasmus Villemoes  * from the author, Douglas W. Jones).
1517c43d9a3SRasmus Villemoes  *
1527c43d9a3SRasmus Villemoes  * It turns out there is precisely one 26 bit fixed-point
1537c43d9a3SRasmus Villemoes  * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
1547c43d9a3SRasmus Villemoes  * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
1557c43d9a3SRasmus Villemoes  * range happens to be somewhat larger (x <= 1073741898), but that's
1567c43d9a3SRasmus Villemoes  * irrelevant for our purpose.
1577c43d9a3SRasmus Villemoes  *
1587c43d9a3SRasmus Villemoes  * For dividing a number in the range [10^4, 10^6-1] by 100, we still
1597c43d9a3SRasmus Villemoes  * need a 32x32->64 bit multiply, so we simply use the same constant.
1607c43d9a3SRasmus Villemoes  *
1617c43d9a3SRasmus Villemoes  * For dividing a number in the range [100, 10^4-1] by 100, there are
1627c43d9a3SRasmus Villemoes  * several options. The simplest is (x * 0x147b) >> 19, which is valid
1637c43d9a3SRasmus Villemoes  * for all x <= 43698.
164133fd9f5SDenys Vlasenko  */
1654277eeddSDenis Vlasenko 
1667c43d9a3SRasmus Villemoes static const u16 decpair[100] = {
1677c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
1687c43d9a3SRasmus Villemoes 	_( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
1697c43d9a3SRasmus Villemoes 	_(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
1707c43d9a3SRasmus Villemoes 	_(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
1717c43d9a3SRasmus Villemoes 	_(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
1727c43d9a3SRasmus Villemoes 	_(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
1737c43d9a3SRasmus Villemoes 	_(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
1747c43d9a3SRasmus Villemoes 	_(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
1757c43d9a3SRasmus Villemoes 	_(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
1767c43d9a3SRasmus Villemoes 	_(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
1777c43d9a3SRasmus Villemoes 	_(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
1787c43d9a3SRasmus Villemoes #undef _
1797c43d9a3SRasmus Villemoes };
1804277eeddSDenis Vlasenko 
1817b9186f5SAndré Goddard Rosa /*
1827c43d9a3SRasmus Villemoes  * This will print a single '0' even if r == 0, since we would
183675cf53cSRasmus Villemoes  * immediately jump to out_r where two 0s would be written but only
184675cf53cSRasmus Villemoes  * one of them accounted for in buf. This is needed by ip4_string
185675cf53cSRasmus Villemoes  * below. All other callers pass a non-zero value of r.
186133fd9f5SDenys Vlasenko */
187133fd9f5SDenys Vlasenko static noinline_for_stack
188133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
189133fd9f5SDenys Vlasenko {
190133fd9f5SDenys Vlasenko 	unsigned q;
1914277eeddSDenis Vlasenko 
1927c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^8 */
1937c43d9a3SRasmus Villemoes 	if (r < 100)
1947c43d9a3SRasmus Villemoes 		goto out_r;
195cb239d0aSGeorge Spelvin 
1967c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^8 */
1977c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
1987c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
1997c43d9a3SRasmus Villemoes 	buf += 2;
2007c43d9a3SRasmus Villemoes 
2017c43d9a3SRasmus Villemoes 	/* 1 <= q < 10^6 */
2027c43d9a3SRasmus Villemoes 	if (q < 100)
2037c43d9a3SRasmus Villemoes 		goto out_q;
2047c43d9a3SRasmus Villemoes 
2057c43d9a3SRasmus Villemoes 	/*  100 <= q < 10^6 */
2067c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
2077c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
2087c43d9a3SRasmus Villemoes 	buf += 2;
2097c43d9a3SRasmus Villemoes 
2107c43d9a3SRasmus Villemoes 	/* 1 <= r < 10^4 */
2117c43d9a3SRasmus Villemoes 	if (r < 100)
2127c43d9a3SRasmus Villemoes 		goto out_r;
2137c43d9a3SRasmus Villemoes 
2147c43d9a3SRasmus Villemoes 	/* 100 <= r < 10^4 */
2157c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2167c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2177c43d9a3SRasmus Villemoes 	buf += 2;
2187c43d9a3SRasmus Villemoes out_q:
2197c43d9a3SRasmus Villemoes 	/* 1 <= q < 100 */
2207c43d9a3SRasmus Villemoes 	r = q;
2217c43d9a3SRasmus Villemoes out_r:
2227c43d9a3SRasmus Villemoes 	/* 1 <= r < 100 */
2237c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r];
224675cf53cSRasmus Villemoes 	buf += r < 10 ? 1 : 2;
225133fd9f5SDenys Vlasenko 	return buf;
226133fd9f5SDenys Vlasenko }
227133fd9f5SDenys Vlasenko 
2287c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
2297c43d9a3SRasmus Villemoes static noinline_for_stack
2307c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r)
2317c43d9a3SRasmus Villemoes {
2327c43d9a3SRasmus Villemoes 	unsigned q;
233133fd9f5SDenys Vlasenko 
2347c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^8 */
2357c43d9a3SRasmus Villemoes 	q = (r * (u64)0x28f5c29) >> 32;
2367c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2377c43d9a3SRasmus Villemoes 	buf += 2;
238133fd9f5SDenys Vlasenko 
2397c43d9a3SRasmus Villemoes 	/* 0 <= q < 10^6 */
2407c43d9a3SRasmus Villemoes 	r = (q * (u64)0x28f5c29) >> 32;
2417c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q - 100*r];
2427c43d9a3SRasmus Villemoes 	buf += 2;
243133fd9f5SDenys Vlasenko 
2447c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2457c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2467c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2477c43d9a3SRasmus Villemoes 	buf += 2;
2487c43d9a3SRasmus Villemoes 
2497c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2507c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2517c43d9a3SRasmus Villemoes 	buf += 2;
2527c43d9a3SRasmus Villemoes 	return buf;
2537c43d9a3SRasmus Villemoes }
2547c43d9a3SRasmus Villemoes 
2557c43d9a3SRasmus Villemoes static noinline_for_stack
256133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
257133fd9f5SDenys Vlasenko {
258133fd9f5SDenys Vlasenko 	if (n >= 100*1000*1000)
2597c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2607c43d9a3SRasmus Villemoes 	/* 1 <= n <= 1.6e11 */
2617c43d9a3SRasmus Villemoes 	if (n >= 100*1000*1000)
2627c43d9a3SRasmus Villemoes 		buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
2637c43d9a3SRasmus Villemoes 	/* 1 <= n < 1e8 */
264133fd9f5SDenys Vlasenko 	return put_dec_trunc8(buf, n);
265133fd9f5SDenys Vlasenko }
266133fd9f5SDenys Vlasenko 
2677c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
268133fd9f5SDenys Vlasenko 
2697c43d9a3SRasmus Villemoes static void
2707c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r)
271133fd9f5SDenys Vlasenko {
2727c43d9a3SRasmus Villemoes 	unsigned q;
2737c43d9a3SRasmus Villemoes 
2747c43d9a3SRasmus Villemoes 	/* 0 <= r < 10^4 */
2757c43d9a3SRasmus Villemoes 	q = (r * 0x147b) >> 19;
2767c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[r - 100*q];
2777c43d9a3SRasmus Villemoes 	buf += 2;
2787c43d9a3SRasmus Villemoes 	/* 0 <= q < 100 */
2797c43d9a3SRasmus Villemoes 	*((u16 *)buf) = decpair[q];
2802359172aSGeorge Spelvin }
2812359172aSGeorge Spelvin 
2822359172aSGeorge Spelvin /*
2832359172aSGeorge Spelvin  * Call put_dec_full4 on x % 10000, return x / 10000.
2842359172aSGeorge Spelvin  * The approximation x/10000 == (x * 0x346DC5D7) >> 43
2852359172aSGeorge Spelvin  * holds for all x < 1,128,869,999.  The largest value this
2862359172aSGeorge Spelvin  * helper will ever be asked to convert is 1,125,520,955.
2877c43d9a3SRasmus Villemoes  * (second call in the put_dec code, assuming n is all-ones).
2882359172aSGeorge Spelvin  */
2897c43d9a3SRasmus Villemoes static noinline_for_stack
2902359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
2912359172aSGeorge Spelvin {
2922359172aSGeorge Spelvin         uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
2932359172aSGeorge Spelvin 
2942359172aSGeorge Spelvin         put_dec_full4(buf, x - q * 10000);
2952359172aSGeorge Spelvin         return q;
296133fd9f5SDenys Vlasenko }
297133fd9f5SDenys Vlasenko 
298133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
299133fd9f5SDenys Vlasenko  * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
300133fd9f5SDenys Vlasenko  * (with permission from the author).
301133fd9f5SDenys Vlasenko  * Performs no 64-bit division and hence should be fast on 32-bit machines.
302133fd9f5SDenys Vlasenko  */
303133fd9f5SDenys Vlasenko static
304133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
305133fd9f5SDenys Vlasenko {
306133fd9f5SDenys Vlasenko 	uint32_t d3, d2, d1, q, h;
307133fd9f5SDenys Vlasenko 
308133fd9f5SDenys Vlasenko 	if (n < 100*1000*1000)
309133fd9f5SDenys Vlasenko 		return put_dec_trunc8(buf, n);
310133fd9f5SDenys Vlasenko 
311133fd9f5SDenys Vlasenko 	d1  = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
312133fd9f5SDenys Vlasenko 	h   = (n >> 32);
313133fd9f5SDenys Vlasenko 	d2  = (h      ) & 0xffff;
314133fd9f5SDenys Vlasenko 	d3  = (h >> 16); /* implicit "& 0xffff" */
315133fd9f5SDenys Vlasenko 
3167c43d9a3SRasmus Villemoes 	/* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
3177c43d9a3SRasmus Villemoes 	     = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
318133fd9f5SDenys Vlasenko 	q   = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
3192359172aSGeorge Spelvin 	q = put_dec_helper4(buf, q);
320133fd9f5SDenys Vlasenko 
3212359172aSGeorge Spelvin 	q += 7671 * d3 + 9496 * d2 + 6 * d1;
3222359172aSGeorge Spelvin 	q = put_dec_helper4(buf+4, q);
323133fd9f5SDenys Vlasenko 
3242359172aSGeorge Spelvin 	q += 4749 * d3 + 42 * d2;
3252359172aSGeorge Spelvin 	q = put_dec_helper4(buf+8, q);
326133fd9f5SDenys Vlasenko 
3272359172aSGeorge Spelvin 	q += 281 * d3;
3282359172aSGeorge Spelvin 	buf += 12;
3292359172aSGeorge Spelvin 	if (q)
3302359172aSGeorge Spelvin 		buf = put_dec_trunc8(buf, q);
3312359172aSGeorge Spelvin 	else while (buf[-1] == '0')
332133fd9f5SDenys Vlasenko 		--buf;
3337b9186f5SAndré Goddard Rosa 
3344277eeddSDenis Vlasenko 	return buf;
3354277eeddSDenis Vlasenko }
336133fd9f5SDenys Vlasenko 
337133fd9f5SDenys Vlasenko #endif
3384277eeddSDenis Vlasenko 
3391ac101a5SKAMEZAWA Hiroyuki /*
3401ac101a5SKAMEZAWA Hiroyuki  * Convert passed number to decimal string.
3411ac101a5SKAMEZAWA Hiroyuki  * Returns the length of string.  On buffer overflow, returns 0.
3421ac101a5SKAMEZAWA Hiroyuki  *
3431ac101a5SKAMEZAWA Hiroyuki  * If speed is not important, use snprintf(). It's easy to read the code.
3441ac101a5SKAMEZAWA Hiroyuki  */
345d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
3461ac101a5SKAMEZAWA Hiroyuki {
3477c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
3487c43d9a3SRasmus Villemoes 	char tmp[sizeof(num) * 3] __aligned(2);
3491ac101a5SKAMEZAWA Hiroyuki 	int idx, len;
3501ac101a5SKAMEZAWA Hiroyuki 
351133fd9f5SDenys Vlasenko 	/* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
352133fd9f5SDenys Vlasenko 	if (num <= 9) {
353133fd9f5SDenys Vlasenko 		tmp[0] = '0' + num;
354133fd9f5SDenys Vlasenko 		len = 1;
355133fd9f5SDenys Vlasenko 	} else {
3561ac101a5SKAMEZAWA Hiroyuki 		len = put_dec(tmp, num) - tmp;
357133fd9f5SDenys Vlasenko 	}
3581ac101a5SKAMEZAWA Hiroyuki 
359d1be35cbSAndrei Vagin 	if (len > size || width > size)
3601ac101a5SKAMEZAWA Hiroyuki 		return 0;
361d1be35cbSAndrei Vagin 
362d1be35cbSAndrei Vagin 	if (width > len) {
363d1be35cbSAndrei Vagin 		width = width - len;
364d1be35cbSAndrei Vagin 		for (idx = 0; idx < width; idx++)
365d1be35cbSAndrei Vagin 			buf[idx] = ' ';
366d1be35cbSAndrei Vagin 	} else {
367d1be35cbSAndrei Vagin 		width = 0;
368d1be35cbSAndrei Vagin 	}
369d1be35cbSAndrei Vagin 
3701ac101a5SKAMEZAWA Hiroyuki 	for (idx = 0; idx < len; ++idx)
371d1be35cbSAndrei Vagin 		buf[idx + width] = tmp[len - idx - 1];
372d1be35cbSAndrei Vagin 
373d1be35cbSAndrei Vagin 	return len + width;
3741ac101a5SKAMEZAWA Hiroyuki }
3751ac101a5SKAMEZAWA Hiroyuki 
37651be17dfSRasmus Villemoes #define SIGN	1		/* unsigned/signed, must be 1 */
377d1c1b121SRasmus Villemoes #define LEFT	2		/* left justified */
3781da177e4SLinus Torvalds #define PLUS	4		/* show plus */
3791da177e4SLinus Torvalds #define SPACE	8		/* space if plus */
380d1c1b121SRasmus Villemoes #define ZEROPAD	16		/* pad with zero, must be 16 == '0' - ' ' */
381b89dc5d6SBjorn Helgaas #define SMALL	32		/* use lowercase in hex (must be 32 == 0x20) */
382b89dc5d6SBjorn Helgaas #define SPECIAL	64		/* prefix hex with "0x", octal with "0" */
3831da177e4SLinus Torvalds 
384b886690dSAndy Shevchenko static_assert(ZEROPAD == ('0' - ' '));
385b886690dSAndy Shevchenko static_assert(SMALL == ' ');
386b886690dSAndy Shevchenko 
387fef20d9cSFrederic Weisbecker enum format_type {
388fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_NONE, /* Just a string part */
389ed681a91SVegard Nossum 	FORMAT_TYPE_WIDTH,
390fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PRECISION,
391fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_CHAR,
392fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_STR,
393fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTR,
394fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PERCENT_CHAR,
395fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INVALID,
396fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG_LONG,
397fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_ULONG,
398fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_LONG,
399a4e94ef0SZhaolei 	FORMAT_TYPE_UBYTE,
400a4e94ef0SZhaolei 	FORMAT_TYPE_BYTE,
401fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_USHORT,
402fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SHORT,
403fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_UINT,
404fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_INT,
405fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_SIZE_T,
406fef20d9cSFrederic Weisbecker 	FORMAT_TYPE_PTRDIFF
407fef20d9cSFrederic Weisbecker };
408fef20d9cSFrederic Weisbecker 
409fef20d9cSFrederic Weisbecker struct printf_spec {
410d0484193SRasmus Villemoes 	unsigned int	type:8;		/* format_type enum */
411d0484193SRasmus Villemoes 	signed int	field_width:24;	/* width of output field */
412d0484193SRasmus Villemoes 	unsigned int	flags:8;	/* flags to number() */
413d0484193SRasmus Villemoes 	unsigned int	base:8;		/* number base, 8, 10 or 16 only */
414d0484193SRasmus Villemoes 	signed int	precision:16;	/* # of digits/chars */
415d0484193SRasmus Villemoes } __packed;
416ef27ac18SRasmus Villemoes static_assert(sizeof(struct printf_spec) == 8);
417ef27ac18SRasmus Villemoes 
4184d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1)
4194d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1)
420fef20d9cSFrederic Weisbecker 
421cf3b429bSJoe Perches static noinline_for_stack
422cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
423fef20d9cSFrederic Weisbecker 	     struct printf_spec spec)
4241da177e4SLinus Torvalds {
4257c43d9a3SRasmus Villemoes 	/* put_dec requires 2-byte alignment of the buffer. */
4267c43d9a3SRasmus Villemoes 	char tmp[3 * sizeof(num)] __aligned(2);
4279b706aeeSDenys Vlasenko 	char sign;
4289b706aeeSDenys Vlasenko 	char locase;
429fef20d9cSFrederic Weisbecker 	int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4301da177e4SLinus Torvalds 	int i;
4317c203422SPierre Carrier 	bool is_zero = num == 0LL;
4321c7a8e62SRasmus Villemoes 	int field_width = spec.field_width;
4331c7a8e62SRasmus Villemoes 	int precision = spec.precision;
4341da177e4SLinus Torvalds 
4359b706aeeSDenys Vlasenko 	/* locase = 0 or 0x20. ORing digits or letters with 'locase'
4369b706aeeSDenys Vlasenko 	 * produces same digits or (maybe lowercased) letters */
437fef20d9cSFrederic Weisbecker 	locase = (spec.flags & SMALL);
438fef20d9cSFrederic Weisbecker 	if (spec.flags & LEFT)
439fef20d9cSFrederic Weisbecker 		spec.flags &= ~ZEROPAD;
4401da177e4SLinus Torvalds 	sign = 0;
441fef20d9cSFrederic Weisbecker 	if (spec.flags & SIGN) {
4421da177e4SLinus Torvalds 		if ((signed long long)num < 0) {
4431da177e4SLinus Torvalds 			sign = '-';
4441da177e4SLinus Torvalds 			num = -(signed long long)num;
4451c7a8e62SRasmus Villemoes 			field_width--;
446fef20d9cSFrederic Weisbecker 		} else if (spec.flags & PLUS) {
4471da177e4SLinus Torvalds 			sign = '+';
4481c7a8e62SRasmus Villemoes 			field_width--;
449fef20d9cSFrederic Weisbecker 		} else if (spec.flags & SPACE) {
4501da177e4SLinus Torvalds 			sign = ' ';
4511c7a8e62SRasmus Villemoes 			field_width--;
4521da177e4SLinus Torvalds 		}
4531da177e4SLinus Torvalds 	}
454b39a7340SDenis Vlasenko 	if (need_pfx) {
455fef20d9cSFrederic Weisbecker 		if (spec.base == 16)
4561c7a8e62SRasmus Villemoes 			field_width -= 2;
4577c203422SPierre Carrier 		else if (!is_zero)
4581c7a8e62SRasmus Villemoes 			field_width--;
4591da177e4SLinus Torvalds 	}
460b39a7340SDenis Vlasenko 
461b39a7340SDenis Vlasenko 	/* generate full string in tmp[], in reverse order */
4621da177e4SLinus Torvalds 	i = 0;
463133fd9f5SDenys Vlasenko 	if (num < spec.base)
4643ea8d440SRasmus Villemoes 		tmp[i++] = hex_asc_upper[num] | locase;
465fef20d9cSFrederic Weisbecker 	else if (spec.base != 10) { /* 8 or 16 */
466fef20d9cSFrederic Weisbecker 		int mask = spec.base - 1;
467b39a7340SDenis Vlasenko 		int shift = 3;
4687b9186f5SAndré Goddard Rosa 
4697b9186f5SAndré Goddard Rosa 		if (spec.base == 16)
4707b9186f5SAndré Goddard Rosa 			shift = 4;
471b39a7340SDenis Vlasenko 		do {
4723ea8d440SRasmus Villemoes 			tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
473b39a7340SDenis Vlasenko 			num >>= shift;
474b39a7340SDenis Vlasenko 		} while (num);
4754277eeddSDenis Vlasenko 	} else { /* base 10 */
4764277eeddSDenis Vlasenko 		i = put_dec(tmp, num) - tmp;
4774277eeddSDenis Vlasenko 	}
478b39a7340SDenis Vlasenko 
479b39a7340SDenis Vlasenko 	/* printing 100 using %2d gives "100", not "00" */
4801c7a8e62SRasmus Villemoes 	if (i > precision)
4811c7a8e62SRasmus Villemoes 		precision = i;
482b39a7340SDenis Vlasenko 	/* leading space padding */
4831c7a8e62SRasmus Villemoes 	field_width -= precision;
48451be17dfSRasmus Villemoes 	if (!(spec.flags & (ZEROPAD | LEFT))) {
4851c7a8e62SRasmus Villemoes 		while (--field_width >= 0) {
486f796937aSJeremy Fitzhardinge 			if (buf < end)
4871da177e4SLinus Torvalds 				*buf = ' ';
4881da177e4SLinus Torvalds 			++buf;
4891da177e4SLinus Torvalds 		}
4901da177e4SLinus Torvalds 	}
491b39a7340SDenis Vlasenko 	/* sign */
4921da177e4SLinus Torvalds 	if (sign) {
493f796937aSJeremy Fitzhardinge 		if (buf < end)
4941da177e4SLinus Torvalds 			*buf = sign;
4951da177e4SLinus Torvalds 		++buf;
4961da177e4SLinus Torvalds 	}
497b39a7340SDenis Vlasenko 	/* "0x" / "0" prefix */
498b39a7340SDenis Vlasenko 	if (need_pfx) {
4997c203422SPierre Carrier 		if (spec.base == 16 || !is_zero) {
500f796937aSJeremy Fitzhardinge 			if (buf < end)
5011da177e4SLinus Torvalds 				*buf = '0';
5021da177e4SLinus Torvalds 			++buf;
5037c203422SPierre Carrier 		}
504fef20d9cSFrederic Weisbecker 		if (spec.base == 16) {
505f796937aSJeremy Fitzhardinge 			if (buf < end)
5069b706aeeSDenys Vlasenko 				*buf = ('X' | locase);
5071da177e4SLinus Torvalds 			++buf;
5081da177e4SLinus Torvalds 		}
5091da177e4SLinus Torvalds 	}
510b39a7340SDenis Vlasenko 	/* zero or space padding */
511fef20d9cSFrederic Weisbecker 	if (!(spec.flags & LEFT)) {
512d1c1b121SRasmus Villemoes 		char c = ' ' + (spec.flags & ZEROPAD);
513b886690dSAndy Shevchenko 
5141c7a8e62SRasmus Villemoes 		while (--field_width >= 0) {
515f796937aSJeremy Fitzhardinge 			if (buf < end)
5161da177e4SLinus Torvalds 				*buf = c;
5171da177e4SLinus Torvalds 			++buf;
5181da177e4SLinus Torvalds 		}
5191da177e4SLinus Torvalds 	}
520b39a7340SDenis Vlasenko 	/* hmm even more zero padding? */
5211c7a8e62SRasmus Villemoes 	while (i <= --precision) {
522f796937aSJeremy Fitzhardinge 		if (buf < end)
5231da177e4SLinus Torvalds 			*buf = '0';
5241da177e4SLinus Torvalds 		++buf;
5251da177e4SLinus Torvalds 	}
526b39a7340SDenis Vlasenko 	/* actual digits of result */
527b39a7340SDenis Vlasenko 	while (--i >= 0) {
528f796937aSJeremy Fitzhardinge 		if (buf < end)
5291da177e4SLinus Torvalds 			*buf = tmp[i];
5301da177e4SLinus Torvalds 		++buf;
5311da177e4SLinus Torvalds 	}
532b39a7340SDenis Vlasenko 	/* trailing space padding */
5331c7a8e62SRasmus Villemoes 	while (--field_width >= 0) {
534f796937aSJeremy Fitzhardinge 		if (buf < end)
5351da177e4SLinus Torvalds 			*buf = ' ';
5361da177e4SLinus Torvalds 		++buf;
5371da177e4SLinus Torvalds 	}
5387b9186f5SAndré Goddard Rosa 
5391da177e4SLinus Torvalds 	return buf;
5401da177e4SLinus Torvalds }
5411da177e4SLinus Torvalds 
5423cab1e71SAndy Shevchenko static noinline_for_stack
5433cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
5443cab1e71SAndy Shevchenko {
5453cab1e71SAndy Shevchenko 	struct printf_spec spec;
5463cab1e71SAndy Shevchenko 
5473cab1e71SAndy Shevchenko 	spec.type = FORMAT_TYPE_PTR;
5483cab1e71SAndy Shevchenko 	spec.field_width = 2 + 2 * size;	/* 0x + hex */
5493cab1e71SAndy Shevchenko 	spec.flags = SPECIAL | SMALL | ZEROPAD;
5503cab1e71SAndy Shevchenko 	spec.base = 16;
5513cab1e71SAndy Shevchenko 	spec.precision = -1;
5523cab1e71SAndy Shevchenko 
5533cab1e71SAndy Shevchenko 	return number(buf, end, num, spec);
5543cab1e71SAndy Shevchenko }
5553cab1e71SAndy Shevchenko 
556cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
5574b6ccca7SAl Viro {
5584b6ccca7SAl Viro 	size_t size;
5594b6ccca7SAl Viro 	if (buf >= end)	/* nowhere to put anything */
5604b6ccca7SAl Viro 		return;
5614b6ccca7SAl Viro 	size = end - buf;
5624b6ccca7SAl Viro 	if (size <= spaces) {
5634b6ccca7SAl Viro 		memset(buf, ' ', size);
5644b6ccca7SAl Viro 		return;
5654b6ccca7SAl Viro 	}
5664b6ccca7SAl Viro 	if (len) {
5674b6ccca7SAl Viro 		if (len > size - spaces)
5684b6ccca7SAl Viro 			len = size - spaces;
5694b6ccca7SAl Viro 		memmove(buf + spaces, buf, len);
5704b6ccca7SAl Viro 	}
5714b6ccca7SAl Viro 	memset(buf, ' ', spaces);
5724b6ccca7SAl Viro }
5734b6ccca7SAl Viro 
574cfccde04SRasmus Villemoes /*
575cfccde04SRasmus Villemoes  * Handle field width padding for a string.
576cfccde04SRasmus Villemoes  * @buf: current buffer position
577cfccde04SRasmus Villemoes  * @n: length of string
578cfccde04SRasmus Villemoes  * @end: end of output buffer
579cfccde04SRasmus Villemoes  * @spec: for field width and flags
580cfccde04SRasmus Villemoes  * Returns: new buffer position after padding.
581cfccde04SRasmus Villemoes  */
582cfccde04SRasmus Villemoes static noinline_for_stack
583cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
584cfccde04SRasmus Villemoes {
585cfccde04SRasmus Villemoes 	unsigned spaces;
586cfccde04SRasmus Villemoes 
587cfccde04SRasmus Villemoes 	if (likely(n >= spec.field_width))
588cfccde04SRasmus Villemoes 		return buf;
589cfccde04SRasmus Villemoes 	/* we want to pad the sucker */
590cfccde04SRasmus Villemoes 	spaces = spec.field_width - n;
591cfccde04SRasmus Villemoes 	if (!(spec.flags & LEFT)) {
592cfccde04SRasmus Villemoes 		move_right(buf - n, end, n, spaces);
593cfccde04SRasmus Villemoes 		return buf + spaces;
594cfccde04SRasmus Villemoes 	}
595cfccde04SRasmus Villemoes 	while (spaces--) {
596cfccde04SRasmus Villemoes 		if (buf < end)
597cfccde04SRasmus Villemoes 			*buf = ' ';
598cfccde04SRasmus Villemoes 		++buf;
599cfccde04SRasmus Villemoes 	}
600cfccde04SRasmus Villemoes 	return buf;
601cfccde04SRasmus Villemoes }
602cfccde04SRasmus Villemoes 
603d529ac41SPetr Mladek /* Handle string from a well known address. */
604d529ac41SPetr Mladek static char *string_nocheck(char *buf, char *end, const char *s,
605d529ac41SPetr Mladek 			    struct printf_spec spec)
60695508cfaSRasmus Villemoes {
60734fc8b90SRasmus Villemoes 	int len = 0;
608b314dd49SYoungmin Nam 	int lim = spec.precision;
60995508cfaSRasmus Villemoes 
61034fc8b90SRasmus Villemoes 	while (lim--) {
61134fc8b90SRasmus Villemoes 		char c = *s++;
61234fc8b90SRasmus Villemoes 		if (!c)
61334fc8b90SRasmus Villemoes 			break;
61495508cfaSRasmus Villemoes 		if (buf < end)
61534fc8b90SRasmus Villemoes 			*buf = c;
61695508cfaSRasmus Villemoes 		++buf;
61734fc8b90SRasmus Villemoes 		++len;
61895508cfaSRasmus Villemoes 	}
61934fc8b90SRasmus Villemoes 	return widen_string(buf, len, end, spec);
62095508cfaSRasmus Villemoes }
62195508cfaSRasmus Villemoes 
62257f5677eSRasmus Villemoes static char *err_ptr(char *buf, char *end, void *ptr,
62357f5677eSRasmus Villemoes 		     struct printf_spec spec)
62457f5677eSRasmus Villemoes {
62557f5677eSRasmus Villemoes 	int err = PTR_ERR(ptr);
62657f5677eSRasmus Villemoes 	const char *sym = errname(err);
62757f5677eSRasmus Villemoes 
62857f5677eSRasmus Villemoes 	if (sym)
62957f5677eSRasmus Villemoes 		return string_nocheck(buf, end, sym, spec);
63057f5677eSRasmus Villemoes 
63157f5677eSRasmus Villemoes 	/*
63257f5677eSRasmus Villemoes 	 * Somebody passed ERR_PTR(-1234) or some other non-existing
63357f5677eSRasmus Villemoes 	 * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
63457f5677eSRasmus Villemoes 	 * printing it as its decimal representation.
63557f5677eSRasmus Villemoes 	 */
63657f5677eSRasmus Villemoes 	spec.flags |= SIGN;
63757f5677eSRasmus Villemoes 	spec.base = 10;
63857f5677eSRasmus Villemoes 	return number(buf, end, err, spec);
63957f5677eSRasmus Villemoes }
64057f5677eSRasmus Villemoes 
641c8c3b584SPetr Mladek /* Be careful: error messages must fit into the given buffer. */
642c8c3b584SPetr Mladek static char *error_string(char *buf, char *end, const char *s,
643c8c3b584SPetr Mladek 			  struct printf_spec spec)
644c8c3b584SPetr Mladek {
645c8c3b584SPetr Mladek 	/*
646c8c3b584SPetr Mladek 	 * Hard limit to avoid a completely insane messages. It actually
647c8c3b584SPetr Mladek 	 * works pretty well because most error messages are in
648c8c3b584SPetr Mladek 	 * the many pointer format modifiers.
649c8c3b584SPetr Mladek 	 */
650c8c3b584SPetr Mladek 	if (spec.precision == -1)
651c8c3b584SPetr Mladek 		spec.precision = 2 * sizeof(void *);
652c8c3b584SPetr Mladek 
653c8c3b584SPetr Mladek 	return string_nocheck(buf, end, s, spec);
654c8c3b584SPetr Mladek }
655c8c3b584SPetr Mladek 
6563e5903ebSPetr Mladek /*
6572ac5a3bfSPetr Mladek  * Do not call any complex external code here. Nested printk()/vsprintf()
6582ac5a3bfSPetr Mladek  * might cause infinite loops. Failures might break printk() and would
6592ac5a3bfSPetr Mladek  * be hard to debug.
6603e5903ebSPetr Mladek  */
6613e5903ebSPetr Mladek static const char *check_pointer_msg(const void *ptr)
6623e5903ebSPetr Mladek {
6633e5903ebSPetr Mladek 	if (!ptr)
6643e5903ebSPetr Mladek 		return "(null)";
6653e5903ebSPetr Mladek 
6662ac5a3bfSPetr Mladek 	if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
6673e5903ebSPetr Mladek 		return "(efault)";
6683e5903ebSPetr Mladek 
6693e5903ebSPetr Mladek 	return NULL;
6703e5903ebSPetr Mladek }
6713e5903ebSPetr Mladek 
6723e5903ebSPetr Mladek static int check_pointer(char **buf, char *end, const void *ptr,
6733e5903ebSPetr Mladek 			 struct printf_spec spec)
6743e5903ebSPetr Mladek {
6753e5903ebSPetr Mladek 	const char *err_msg;
6763e5903ebSPetr Mladek 
6773e5903ebSPetr Mladek 	err_msg = check_pointer_msg(ptr);
6783e5903ebSPetr Mladek 	if (err_msg) {
679c8c3b584SPetr Mladek 		*buf = error_string(*buf, end, err_msg, spec);
6803e5903ebSPetr Mladek 		return -EFAULT;
6813e5903ebSPetr Mladek 	}
6823e5903ebSPetr Mladek 
6833e5903ebSPetr Mladek 	return 0;
6843e5903ebSPetr Mladek }
6853e5903ebSPetr Mladek 
68695508cfaSRasmus Villemoes static noinline_for_stack
687d529ac41SPetr Mladek char *string(char *buf, char *end, const char *s,
688d529ac41SPetr Mladek 	     struct printf_spec spec)
689d529ac41SPetr Mladek {
6903e5903ebSPetr Mladek 	if (check_pointer(&buf, end, s, spec))
6913e5903ebSPetr Mladek 		return buf;
692d529ac41SPetr Mladek 
693d529ac41SPetr Mladek 	return string_nocheck(buf, end, s, spec);
694d529ac41SPetr Mladek }
695d529ac41SPetr Mladek 
696ce9d3eceSYueHaibing static char *pointer_string(char *buf, char *end,
697ce9d3eceSYueHaibing 			    const void *ptr,
6989073dac1SGeert Uytterhoeven 			    struct printf_spec spec)
6999073dac1SGeert Uytterhoeven {
7009073dac1SGeert Uytterhoeven 	spec.base = 16;
7019073dac1SGeert Uytterhoeven 	spec.flags |= SMALL;
7029073dac1SGeert Uytterhoeven 	if (spec.field_width == -1) {
7039073dac1SGeert Uytterhoeven 		spec.field_width = 2 * sizeof(ptr);
7049073dac1SGeert Uytterhoeven 		spec.flags |= ZEROPAD;
7059073dac1SGeert Uytterhoeven 	}
7069073dac1SGeert Uytterhoeven 
7079073dac1SGeert Uytterhoeven 	return number(buf, end, (unsigned long int)ptr, spec);
7089073dac1SGeert Uytterhoeven }
7099073dac1SGeert Uytterhoeven 
7109073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */
7119073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init;
7129073dac1SGeert Uytterhoeven 
7139073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str)
7149073dac1SGeert Uytterhoeven {
7159073dac1SGeert Uytterhoeven 	debug_boot_weak_hash = 1;
7169073dac1SGeert Uytterhoeven 	pr_info("debug_boot_weak_hash enabled\n");
7179073dac1SGeert Uytterhoeven 	return 0;
7189073dac1SGeert Uytterhoeven }
7199073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
7209073dac1SGeert Uytterhoeven 
7219073dac1SGeert Uytterhoeven static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key);
7229073dac1SGeert Uytterhoeven static siphash_key_t ptr_key __read_mostly;
7239073dac1SGeert Uytterhoeven 
7249073dac1SGeert Uytterhoeven static void enable_ptr_key_workfn(struct work_struct *work)
7259073dac1SGeert Uytterhoeven {
7269073dac1SGeert Uytterhoeven 	get_random_bytes(&ptr_key, sizeof(ptr_key));
7279073dac1SGeert Uytterhoeven 	/* Needs to run from preemptible context */
7289073dac1SGeert Uytterhoeven 	static_branch_disable(&not_filled_random_ptr_key);
7299073dac1SGeert Uytterhoeven }
7309073dac1SGeert Uytterhoeven 
7319073dac1SGeert Uytterhoeven static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn);
7329073dac1SGeert Uytterhoeven 
7339073dac1SGeert Uytterhoeven static void fill_random_ptr_key(struct random_ready_callback *unused)
7349073dac1SGeert Uytterhoeven {
7359073dac1SGeert Uytterhoeven 	/* This may be in an interrupt handler. */
7369073dac1SGeert Uytterhoeven 	queue_work(system_unbound_wq, &enable_ptr_key_work);
7379073dac1SGeert Uytterhoeven }
7389073dac1SGeert Uytterhoeven 
7399073dac1SGeert Uytterhoeven static struct random_ready_callback random_ready = {
7409073dac1SGeert Uytterhoeven 	.func = fill_random_ptr_key
7419073dac1SGeert Uytterhoeven };
7429073dac1SGeert Uytterhoeven 
7439073dac1SGeert Uytterhoeven static int __init initialize_ptr_random(void)
7449073dac1SGeert Uytterhoeven {
7459073dac1SGeert Uytterhoeven 	int key_size = sizeof(ptr_key);
7469073dac1SGeert Uytterhoeven 	int ret;
7479073dac1SGeert Uytterhoeven 
7489073dac1SGeert Uytterhoeven 	/* Use hw RNG if available. */
7499073dac1SGeert Uytterhoeven 	if (get_random_bytes_arch(&ptr_key, key_size) == key_size) {
7509073dac1SGeert Uytterhoeven 		static_branch_disable(&not_filled_random_ptr_key);
7519073dac1SGeert Uytterhoeven 		return 0;
7529073dac1SGeert Uytterhoeven 	}
7539073dac1SGeert Uytterhoeven 
7549073dac1SGeert Uytterhoeven 	ret = add_random_ready_callback(&random_ready);
7559073dac1SGeert Uytterhoeven 	if (!ret) {
7569073dac1SGeert Uytterhoeven 		return 0;
7579073dac1SGeert Uytterhoeven 	} else if (ret == -EALREADY) {
7589073dac1SGeert Uytterhoeven 		/* This is in preemptible context */
7599073dac1SGeert Uytterhoeven 		enable_ptr_key_workfn(&enable_ptr_key_work);
7609073dac1SGeert Uytterhoeven 		return 0;
7619073dac1SGeert Uytterhoeven 	}
7629073dac1SGeert Uytterhoeven 
7639073dac1SGeert Uytterhoeven 	return ret;
7649073dac1SGeert Uytterhoeven }
7659073dac1SGeert Uytterhoeven early_initcall(initialize_ptr_random);
7669073dac1SGeert Uytterhoeven 
7679073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */
768e4dcad20SJoel Fernandes (Google) static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
7699073dac1SGeert Uytterhoeven {
7709073dac1SGeert Uytterhoeven 	unsigned long hashval;
7719073dac1SGeert Uytterhoeven 
772e4dcad20SJoel Fernandes (Google) 	if (static_branch_unlikely(&not_filled_random_ptr_key))
773e4dcad20SJoel Fernandes (Google) 		return -EAGAIN;
7749073dac1SGeert Uytterhoeven 
7759073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT
7769073dac1SGeert Uytterhoeven 	hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
7779073dac1SGeert Uytterhoeven 	/*
7789073dac1SGeert Uytterhoeven 	 * Mask off the first 32 bits, this makes explicit that we have
7799073dac1SGeert Uytterhoeven 	 * modified the address (and 32 bits is plenty for a unique ID).
7809073dac1SGeert Uytterhoeven 	 */
7819073dac1SGeert Uytterhoeven 	hashval = hashval & 0xffffffff;
7829073dac1SGeert Uytterhoeven #else
7839073dac1SGeert Uytterhoeven 	hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
7849073dac1SGeert Uytterhoeven #endif
785e4dcad20SJoel Fernandes (Google) 	*hashval_out = hashval;
786e4dcad20SJoel Fernandes (Google) 	return 0;
787e4dcad20SJoel Fernandes (Google) }
788e4dcad20SJoel Fernandes (Google) 
789e4dcad20SJoel Fernandes (Google) int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
790e4dcad20SJoel Fernandes (Google) {
791e4dcad20SJoel Fernandes (Google) 	return __ptr_to_hashval(ptr, hashval_out);
792e4dcad20SJoel Fernandes (Google) }
793e4dcad20SJoel Fernandes (Google) 
794e4dcad20SJoel Fernandes (Google) static char *ptr_to_id(char *buf, char *end, const void *ptr,
795e4dcad20SJoel Fernandes (Google) 		       struct printf_spec spec)
796e4dcad20SJoel Fernandes (Google) {
797e4dcad20SJoel Fernandes (Google) 	const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
798e4dcad20SJoel Fernandes (Google) 	unsigned long hashval;
799e4dcad20SJoel Fernandes (Google) 	int ret;
800e4dcad20SJoel Fernandes (Google) 
8017bd57fbcSIlya Dryomov 	/*
8027bd57fbcSIlya Dryomov 	 * Print the real pointer value for NULL and error pointers,
8037bd57fbcSIlya Dryomov 	 * as they are not actual addresses.
8047bd57fbcSIlya Dryomov 	 */
8057bd57fbcSIlya Dryomov 	if (IS_ERR_OR_NULL(ptr))
8067bd57fbcSIlya Dryomov 		return pointer_string(buf, end, ptr, spec);
8077bd57fbcSIlya Dryomov 
808e4dcad20SJoel Fernandes (Google) 	/* When debugging early boot use non-cryptographically secure hash. */
809e4dcad20SJoel Fernandes (Google) 	if (unlikely(debug_boot_weak_hash)) {
810e4dcad20SJoel Fernandes (Google) 		hashval = hash_long((unsigned long)ptr, 32);
811e4dcad20SJoel Fernandes (Google) 		return pointer_string(buf, end, (const void *)hashval, spec);
812e4dcad20SJoel Fernandes (Google) 	}
813e4dcad20SJoel Fernandes (Google) 
814e4dcad20SJoel Fernandes (Google) 	ret = __ptr_to_hashval(ptr, &hashval);
815e4dcad20SJoel Fernandes (Google) 	if (ret) {
816e4dcad20SJoel Fernandes (Google) 		spec.field_width = 2 * sizeof(ptr);
817e4dcad20SJoel Fernandes (Google) 		/* string length must be less than default_width */
818e4dcad20SJoel Fernandes (Google) 		return error_string(buf, end, str, spec);
819e4dcad20SJoel Fernandes (Google) 	}
820e4dcad20SJoel Fernandes (Google) 
8219073dac1SGeert Uytterhoeven 	return pointer_string(buf, end, (const void *)hashval, spec);
8229073dac1SGeert Uytterhoeven }
8239073dac1SGeert Uytterhoeven 
8246eea242fSPetr Mladek int kptr_restrict __read_mostly;
8256eea242fSPetr Mladek 
8266eea242fSPetr Mladek static noinline_for_stack
8276eea242fSPetr Mladek char *restricted_pointer(char *buf, char *end, const void *ptr,
8286eea242fSPetr Mladek 			 struct printf_spec spec)
8296eea242fSPetr Mladek {
8306eea242fSPetr Mladek 	switch (kptr_restrict) {
8316eea242fSPetr Mladek 	case 0:
8321ac2f978SPetr Mladek 		/* Handle as %p, hash and do _not_ leak addresses. */
8331ac2f978SPetr Mladek 		return ptr_to_id(buf, end, ptr, spec);
8346eea242fSPetr Mladek 	case 1: {
8356eea242fSPetr Mladek 		const struct cred *cred;
8366eea242fSPetr Mladek 
8376eea242fSPetr Mladek 		/*
8386eea242fSPetr Mladek 		 * kptr_restrict==1 cannot be used in IRQ context
8396eea242fSPetr Mladek 		 * because its test for CAP_SYSLOG would be meaningless.
8406eea242fSPetr Mladek 		 */
8416eea242fSPetr Mladek 		if (in_irq() || in_serving_softirq() || in_nmi()) {
8426eea242fSPetr Mladek 			if (spec.field_width == -1)
8436eea242fSPetr Mladek 				spec.field_width = 2 * sizeof(ptr);
844c8c3b584SPetr Mladek 			return error_string(buf, end, "pK-error", spec);
8456eea242fSPetr Mladek 		}
8466eea242fSPetr Mladek 
8476eea242fSPetr Mladek 		/*
8486eea242fSPetr Mladek 		 * Only print the real pointer value if the current
8496eea242fSPetr Mladek 		 * process has CAP_SYSLOG and is running with the
8506eea242fSPetr Mladek 		 * same credentials it started with. This is because
8516eea242fSPetr Mladek 		 * access to files is checked at open() time, but %pK
8526eea242fSPetr Mladek 		 * checks permission at read() time. We don't want to
8536eea242fSPetr Mladek 		 * leak pointer values if a binary opens a file using
8546eea242fSPetr Mladek 		 * %pK and then elevates privileges before reading it.
8556eea242fSPetr Mladek 		 */
8566eea242fSPetr Mladek 		cred = current_cred();
8576eea242fSPetr Mladek 		if (!has_capability_noaudit(current, CAP_SYSLOG) ||
8586eea242fSPetr Mladek 		    !uid_eq(cred->euid, cred->uid) ||
8596eea242fSPetr Mladek 		    !gid_eq(cred->egid, cred->gid))
8606eea242fSPetr Mladek 			ptr = NULL;
8616eea242fSPetr Mladek 		break;
8626eea242fSPetr Mladek 	}
8636eea242fSPetr Mladek 	case 2:
8646eea242fSPetr Mladek 	default:
8656eea242fSPetr Mladek 		/* Always print 0's for %pK */
8666eea242fSPetr Mladek 		ptr = NULL;
8676eea242fSPetr Mladek 		break;
8686eea242fSPetr Mladek 	}
8696eea242fSPetr Mladek 
8706eea242fSPetr Mladek 	return pointer_string(buf, end, ptr, spec);
8716eea242fSPetr Mladek }
8726eea242fSPetr Mladek 
8739073dac1SGeert Uytterhoeven static noinline_for_stack
8744b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
8754b6ccca7SAl Viro 		  const char *fmt)
8764b6ccca7SAl Viro {
8774b6ccca7SAl Viro 	const char *array[4], *s;
8784b6ccca7SAl Viro 	const struct dentry *p;
8794b6ccca7SAl Viro 	int depth;
8804b6ccca7SAl Viro 	int i, n;
8814b6ccca7SAl Viro 
8824b6ccca7SAl Viro 	switch (fmt[1]) {
8834b6ccca7SAl Viro 		case '2': case '3': case '4':
8844b6ccca7SAl Viro 			depth = fmt[1] - '0';
8854b6ccca7SAl Viro 			break;
8864b6ccca7SAl Viro 		default:
8874b6ccca7SAl Viro 			depth = 1;
8884b6ccca7SAl Viro 	}
8894b6ccca7SAl Viro 
8904b6ccca7SAl Viro 	rcu_read_lock();
8914b6ccca7SAl Viro 	for (i = 0; i < depth; i++, d = p) {
8923e5903ebSPetr Mladek 		if (check_pointer(&buf, end, d, spec)) {
8933e5903ebSPetr Mladek 			rcu_read_unlock();
8943e5903ebSPetr Mladek 			return buf;
8953e5903ebSPetr Mladek 		}
8963e5903ebSPetr Mladek 
8976aa7de05SMark Rutland 		p = READ_ONCE(d->d_parent);
8986aa7de05SMark Rutland 		array[i] = READ_ONCE(d->d_name.name);
8994b6ccca7SAl Viro 		if (p == d) {
9004b6ccca7SAl Viro 			if (i)
9014b6ccca7SAl Viro 				array[i] = "";
9024b6ccca7SAl Viro 			i++;
9034b6ccca7SAl Viro 			break;
9044b6ccca7SAl Viro 		}
9054b6ccca7SAl Viro 	}
9064b6ccca7SAl Viro 	s = array[--i];
9074b6ccca7SAl Viro 	for (n = 0; n != spec.precision; n++, buf++) {
9084b6ccca7SAl Viro 		char c = *s++;
9094b6ccca7SAl Viro 		if (!c) {
9104b6ccca7SAl Viro 			if (!i)
9114b6ccca7SAl Viro 				break;
9124b6ccca7SAl Viro 			c = '/';
9134b6ccca7SAl Viro 			s = array[--i];
9144b6ccca7SAl Viro 		}
9154b6ccca7SAl Viro 		if (buf < end)
9164b6ccca7SAl Viro 			*buf = c;
9174b6ccca7SAl Viro 	}
9184b6ccca7SAl Viro 	rcu_read_unlock();
919cfccde04SRasmus Villemoes 	return widen_string(buf, n, end, spec);
9204b6ccca7SAl Viro }
9214b6ccca7SAl Viro 
92236594b31SJia He static noinline_for_stack
92336594b31SJia He char *file_dentry_name(char *buf, char *end, const struct file *f,
92436594b31SJia He 			struct printf_spec spec, const char *fmt)
92536594b31SJia He {
92636594b31SJia He 	if (check_pointer(&buf, end, f, spec))
92736594b31SJia He 		return buf;
92836594b31SJia He 
92936594b31SJia He 	return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
93036594b31SJia He }
9311031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
9321031bc58SDmitry Monakhov static noinline_for_stack
9331031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev,
9341031bc58SDmitry Monakhov 		struct printf_spec spec, const char *fmt)
9351031bc58SDmitry Monakhov {
9363e5903ebSPetr Mladek 	struct gendisk *hd;
9371031bc58SDmitry Monakhov 
9383e5903ebSPetr Mladek 	if (check_pointer(&buf, end, bdev, spec))
9393e5903ebSPetr Mladek 		return buf;
9403e5903ebSPetr Mladek 
9413e5903ebSPetr Mladek 	hd = bdev->bd_disk;
9421031bc58SDmitry Monakhov 	buf = string(buf, end, hd->disk_name, spec);
943700cd59dSChristoph Hellwig 	if (bdev->bd_partno) {
9441031bc58SDmitry Monakhov 		if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
9451031bc58SDmitry Monakhov 			if (buf < end)
9461031bc58SDmitry Monakhov 				*buf = 'p';
9471031bc58SDmitry Monakhov 			buf++;
9481031bc58SDmitry Monakhov 		}
949700cd59dSChristoph Hellwig 		buf = number(buf, end, bdev->bd_partno, spec);
9501031bc58SDmitry Monakhov 	}
9511031bc58SDmitry Monakhov 	return buf;
9521031bc58SDmitry Monakhov }
9531031bc58SDmitry Monakhov #endif
9541031bc58SDmitry Monakhov 
955cf3b429bSJoe Perches static noinline_for_stack
956cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
957b0d33c2bSJoe Perches 		    struct printf_spec spec, const char *fmt)
9580fe1ef24SLinus Torvalds {
959b0d33c2bSJoe Perches 	unsigned long value;
9600fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
9610fe1ef24SLinus Torvalds 	char sym[KSYM_SYMBOL_LEN];
962b0d33c2bSJoe Perches #endif
963b0d33c2bSJoe Perches 
964b0d33c2bSJoe Perches 	if (fmt[1] == 'R')
965b0d33c2bSJoe Perches 		ptr = __builtin_extract_return_addr(ptr);
966b0d33c2bSJoe Perches 	value = (unsigned long)ptr;
967b0d33c2bSJoe Perches 
968b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
969b0d33c2bSJoe Perches 	if (*fmt == 'B')
9700f77a8d3SNamhyung Kim 		sprint_backtrace(sym, value);
9719af77064SSakari Ailus 	else if (*fmt != 's')
9720fe1ef24SLinus Torvalds 		sprint_symbol(sym, value);
9730c8b946eSFrederic Weisbecker 	else
9744796dd20SStephen Boyd 		sprint_symbol_no_offset(sym, value);
9757b9186f5SAndré Goddard Rosa 
976d529ac41SPetr Mladek 	return string_nocheck(buf, end, sym, spec);
9770fe1ef24SLinus Torvalds #else
9783cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, value, sizeof(void *));
9790fe1ef24SLinus Torvalds #endif
9800fe1ef24SLinus Torvalds }
9810fe1ef24SLinus Torvalds 
982abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = {
983abd4fe62SAndy Shevchenko 	.field_width = -1,
984abd4fe62SAndy Shevchenko 	.precision = -1,
985abd4fe62SAndy Shevchenko };
986abd4fe62SAndy Shevchenko 
98754433973SAndy Shevchenko static const struct printf_spec default_flag_spec = {
98854433973SAndy Shevchenko 	.base = 16,
98954433973SAndy Shevchenko 	.precision = -1,
99054433973SAndy Shevchenko 	.flags = SPECIAL | SMALL,
99154433973SAndy Shevchenko };
99254433973SAndy Shevchenko 
993ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = {
994ce0b4910SAndy Shevchenko 	.base = 10,
995ce0b4910SAndy Shevchenko 	.precision = -1,
996ce0b4910SAndy Shevchenko };
997ce0b4910SAndy Shevchenko 
9984d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = {
9994d42c447SAndy Shevchenko 	.base = 10,
10004d42c447SAndy Shevchenko 	.field_width = 2,
10014d42c447SAndy Shevchenko 	.precision = -1,
10024d42c447SAndy Shevchenko 	.flags = ZEROPAD,
10034d42c447SAndy Shevchenko };
10044d42c447SAndy Shevchenko 
10054d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = {
10064d42c447SAndy Shevchenko 	.base = 10,
10074d42c447SAndy Shevchenko 	.field_width = 4,
10084d42c447SAndy Shevchenko 	.precision = -1,
10094d42c447SAndy Shevchenko 	.flags = ZEROPAD,
10104d42c447SAndy Shevchenko };
10114d42c447SAndy Shevchenko 
1012cf3b429bSJoe Perches static noinline_for_stack
1013cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
1014fd95541eSBjorn Helgaas 		      struct printf_spec spec, const char *fmt)
1015332d2e78SLinus Torvalds {
1016332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
101728405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE	6
1018332d2e78SLinus Torvalds #endif
1019332d2e78SLinus Torvalds 
1020332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
102128405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE	10
1022332d2e78SLinus Torvalds #endif
10234da0b66cSBjorn Helgaas 	static const struct printf_spec io_spec = {
1024fef20d9cSFrederic Weisbecker 		.base = 16,
10254da0b66cSBjorn Helgaas 		.field_width = IO_RSRC_PRINTK_SIZE,
1026fef20d9cSFrederic Weisbecker 		.precision = -1,
1027fef20d9cSFrederic Weisbecker 		.flags = SPECIAL | SMALL | ZEROPAD,
1028fef20d9cSFrederic Weisbecker 	};
10294da0b66cSBjorn Helgaas 	static const struct printf_spec mem_spec = {
10304da0b66cSBjorn Helgaas 		.base = 16,
10314da0b66cSBjorn Helgaas 		.field_width = MEM_RSRC_PRINTK_SIZE,
10324da0b66cSBjorn Helgaas 		.precision = -1,
10334da0b66cSBjorn Helgaas 		.flags = SPECIAL | SMALL | ZEROPAD,
10344da0b66cSBjorn Helgaas 	};
10350f4050c7SBjorn Helgaas 	static const struct printf_spec bus_spec = {
10360f4050c7SBjorn Helgaas 		.base = 16,
10370f4050c7SBjorn Helgaas 		.field_width = 2,
10380f4050c7SBjorn Helgaas 		.precision = -1,
10390f4050c7SBjorn Helgaas 		.flags = SMALL | ZEROPAD,
10400f4050c7SBjorn Helgaas 	};
10414da0b66cSBjorn Helgaas 	static const struct printf_spec str_spec = {
1042fd95541eSBjorn Helgaas 		.field_width = -1,
1043fd95541eSBjorn Helgaas 		.precision = 10,
1044fd95541eSBjorn Helgaas 		.flags = LEFT,
1045fd95541eSBjorn Helgaas 	};
1046c7dabef8SBjorn Helgaas 
1047c7dabef8SBjorn Helgaas 	/* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1048c7dabef8SBjorn Helgaas 	 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1049c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE		((2 * sizeof(resource_size_t)) + 4)
1050c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE		(2 * sizeof(res->flags))
10519d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE	sizeof("[mem - 64bit pref window disabled]")
1052c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE		sizeof("[mem - flags 0x]")
1053c7dabef8SBjorn Helgaas 	char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1054c7dabef8SBjorn Helgaas 		     2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1055c7dabef8SBjorn Helgaas 
1056332d2e78SLinus Torvalds 	char *p = sym, *pend = sym + sizeof(sym);
1057c7dabef8SBjorn Helgaas 	int decode = (fmt[0] == 'R') ? 1 : 0;
10584da0b66cSBjorn Helgaas 	const struct printf_spec *specp;
1059332d2e78SLinus Torvalds 
10603e5903ebSPetr Mladek 	if (check_pointer(&buf, end, res, spec))
10613e5903ebSPetr Mladek 		return buf;
10623e5903ebSPetr Mladek 
1063332d2e78SLinus Torvalds 	*p++ = '[';
10644da0b66cSBjorn Helgaas 	if (res->flags & IORESOURCE_IO) {
1065d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "io  ", str_spec);
10664da0b66cSBjorn Helgaas 		specp = &io_spec;
10674da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_MEM) {
1068d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "mem ", str_spec);
10694da0b66cSBjorn Helgaas 		specp = &mem_spec;
10704da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_IRQ) {
1071d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "irq ", str_spec);
1072ce0b4910SAndy Shevchenko 		specp = &default_dec_spec;
10734da0b66cSBjorn Helgaas 	} else if (res->flags & IORESOURCE_DMA) {
1074d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "dma ", str_spec);
1075ce0b4910SAndy Shevchenko 		specp = &default_dec_spec;
10760f4050c7SBjorn Helgaas 	} else if (res->flags & IORESOURCE_BUS) {
1077d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "bus ", str_spec);
10780f4050c7SBjorn Helgaas 		specp = &bus_spec;
10794da0b66cSBjorn Helgaas 	} else {
1080d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "??? ", str_spec);
10814da0b66cSBjorn Helgaas 		specp = &mem_spec;
1082c7dabef8SBjorn Helgaas 		decode = 0;
1083fd95541eSBjorn Helgaas 	}
1084d19cb803SBjorn Helgaas 	if (decode && res->flags & IORESOURCE_UNSET) {
1085d529ac41SPetr Mladek 		p = string_nocheck(p, pend, "size ", str_spec);
1086d19cb803SBjorn Helgaas 		p = number(p, pend, resource_size(res), *specp);
1087d19cb803SBjorn Helgaas 	} else {
10884da0b66cSBjorn Helgaas 		p = number(p, pend, res->start, *specp);
1089c91d3376SBjorn Helgaas 		if (res->start != res->end) {
1090332d2e78SLinus Torvalds 			*p++ = '-';
10914da0b66cSBjorn Helgaas 			p = number(p, pend, res->end, *specp);
1092c91d3376SBjorn Helgaas 		}
1093d19cb803SBjorn Helgaas 	}
1094c7dabef8SBjorn Helgaas 	if (decode) {
1095fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_MEM_64)
1096d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " 64bit", str_spec);
1097fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_PREFETCH)
1098d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " pref", str_spec);
10999d7cca04SBjorn Helgaas 		if (res->flags & IORESOURCE_WINDOW)
1100d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " window", str_spec);
1101fd95541eSBjorn Helgaas 		if (res->flags & IORESOURCE_DISABLED)
1102d529ac41SPetr Mladek 			p = string_nocheck(p, pend, " disabled", str_spec);
1103c7dabef8SBjorn Helgaas 	} else {
1104d529ac41SPetr Mladek 		p = string_nocheck(p, pend, " flags ", str_spec);
110554433973SAndy Shevchenko 		p = number(p, pend, res->flags, default_flag_spec);
1106fd95541eSBjorn Helgaas 	}
1107332d2e78SLinus Torvalds 	*p++ = ']';
1108c7dabef8SBjorn Helgaas 	*p = '\0';
1109332d2e78SLinus Torvalds 
1110d529ac41SPetr Mladek 	return string_nocheck(buf, end, sym, spec);
1111332d2e78SLinus Torvalds }
1112332d2e78SLinus Torvalds 
1113cf3b429bSJoe Perches static noinline_for_stack
111431550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
111531550a16SAndy Shevchenko 		 const char *fmt)
111631550a16SAndy Shevchenko {
1117360603a1SSteven Rostedt 	int i, len = 1;		/* if we pass '%ph[CDN]', field width remains
111831550a16SAndy Shevchenko 				   negative value, fallback to the default */
111931550a16SAndy Shevchenko 	char separator;
112031550a16SAndy Shevchenko 
112131550a16SAndy Shevchenko 	if (spec.field_width == 0)
112231550a16SAndy Shevchenko 		/* nothing to print */
112331550a16SAndy Shevchenko 		return buf;
112431550a16SAndy Shevchenko 
11253e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
11263e5903ebSPetr Mladek 		return buf;
112731550a16SAndy Shevchenko 
112831550a16SAndy Shevchenko 	switch (fmt[1]) {
112931550a16SAndy Shevchenko 	case 'C':
113031550a16SAndy Shevchenko 		separator = ':';
113131550a16SAndy Shevchenko 		break;
113231550a16SAndy Shevchenko 	case 'D':
113331550a16SAndy Shevchenko 		separator = '-';
113431550a16SAndy Shevchenko 		break;
113531550a16SAndy Shevchenko 	case 'N':
113631550a16SAndy Shevchenko 		separator = 0;
113731550a16SAndy Shevchenko 		break;
113831550a16SAndy Shevchenko 	default:
113931550a16SAndy Shevchenko 		separator = ' ';
114031550a16SAndy Shevchenko 		break;
114131550a16SAndy Shevchenko 	}
114231550a16SAndy Shevchenko 
114331550a16SAndy Shevchenko 	if (spec.field_width > 0)
114431550a16SAndy Shevchenko 		len = min_t(int, spec.field_width, 64);
114531550a16SAndy Shevchenko 
11469c98f235SRasmus Villemoes 	for (i = 0; i < len; ++i) {
11479c98f235SRasmus Villemoes 		if (buf < end)
11489c98f235SRasmus Villemoes 			*buf = hex_asc_hi(addr[i]);
11499c98f235SRasmus Villemoes 		++buf;
11509c98f235SRasmus Villemoes 		if (buf < end)
11519c98f235SRasmus Villemoes 			*buf = hex_asc_lo(addr[i]);
11529c98f235SRasmus Villemoes 		++buf;
115331550a16SAndy Shevchenko 
11549c98f235SRasmus Villemoes 		if (separator && i != len - 1) {
11559c98f235SRasmus Villemoes 			if (buf < end)
11569c98f235SRasmus Villemoes 				*buf = separator;
11579c98f235SRasmus Villemoes 			++buf;
11589c98f235SRasmus Villemoes 		}
115931550a16SAndy Shevchenko 	}
116031550a16SAndy Shevchenko 
116131550a16SAndy Shevchenko 	return buf;
116231550a16SAndy Shevchenko }
116331550a16SAndy Shevchenko 
116431550a16SAndy Shevchenko static noinline_for_stack
1165dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
1166dbc760bcSTejun Heo 		    struct printf_spec spec, const char *fmt)
1167dbc760bcSTejun Heo {
1168dbc760bcSTejun Heo 	const int CHUNKSZ = 32;
1169dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
1170dbc760bcSTejun Heo 	int i, chunksz;
1171dbc760bcSTejun Heo 	bool first = true;
1172dbc760bcSTejun Heo 
11733e5903ebSPetr Mladek 	if (check_pointer(&buf, end, bitmap, spec))
11743e5903ebSPetr Mladek 		return buf;
11753e5903ebSPetr Mladek 
1176dbc760bcSTejun Heo 	/* reused to print numbers */
1177dbc760bcSTejun Heo 	spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1178dbc760bcSTejun Heo 
1179dbc760bcSTejun Heo 	chunksz = nr_bits & (CHUNKSZ - 1);
1180dbc760bcSTejun Heo 	if (chunksz == 0)
1181dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
1182dbc760bcSTejun Heo 
1183dbc760bcSTejun Heo 	i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1184dbc760bcSTejun Heo 	for (; i >= 0; i -= CHUNKSZ) {
1185dbc760bcSTejun Heo 		u32 chunkmask, val;
1186dbc760bcSTejun Heo 		int word, bit;
1187dbc760bcSTejun Heo 
1188dbc760bcSTejun Heo 		chunkmask = ((1ULL << chunksz) - 1);
1189dbc760bcSTejun Heo 		word = i / BITS_PER_LONG;
1190dbc760bcSTejun Heo 		bit = i % BITS_PER_LONG;
1191dbc760bcSTejun Heo 		val = (bitmap[word] >> bit) & chunkmask;
1192dbc760bcSTejun Heo 
1193dbc760bcSTejun Heo 		if (!first) {
1194dbc760bcSTejun Heo 			if (buf < end)
1195dbc760bcSTejun Heo 				*buf = ',';
1196dbc760bcSTejun Heo 			buf++;
1197dbc760bcSTejun Heo 		}
1198dbc760bcSTejun Heo 		first = false;
1199dbc760bcSTejun Heo 
1200dbc760bcSTejun Heo 		spec.field_width = DIV_ROUND_UP(chunksz, 4);
1201dbc760bcSTejun Heo 		buf = number(buf, end, val, spec);
1202dbc760bcSTejun Heo 
1203dbc760bcSTejun Heo 		chunksz = CHUNKSZ;
1204dbc760bcSTejun Heo 	}
1205dbc760bcSTejun Heo 	return buf;
1206dbc760bcSTejun Heo }
1207dbc760bcSTejun Heo 
1208dbc760bcSTejun Heo static noinline_for_stack
1209dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
1210dbc760bcSTejun Heo 			 struct printf_spec spec, const char *fmt)
1211dbc760bcSTejun Heo {
1212dbc760bcSTejun Heo 	int nr_bits = max_t(int, spec.field_width, 0);
1213dbc760bcSTejun Heo 	/* current bit is 'cur', most recently seen range is [rbot, rtop] */
1214dbc760bcSTejun Heo 	int cur, rbot, rtop;
1215dbc760bcSTejun Heo 	bool first = true;
1216dbc760bcSTejun Heo 
12173e5903ebSPetr Mladek 	if (check_pointer(&buf, end, bitmap, spec))
12183e5903ebSPetr Mladek 		return buf;
12193e5903ebSPetr Mladek 
1220dbc760bcSTejun Heo 	rbot = cur = find_first_bit(bitmap, nr_bits);
1221dbc760bcSTejun Heo 	while (cur < nr_bits) {
1222dbc760bcSTejun Heo 		rtop = cur;
1223dbc760bcSTejun Heo 		cur = find_next_bit(bitmap, nr_bits, cur + 1);
1224dbc760bcSTejun Heo 		if (cur < nr_bits && cur <= rtop + 1)
1225dbc760bcSTejun Heo 			continue;
1226dbc760bcSTejun Heo 
1227dbc760bcSTejun Heo 		if (!first) {
1228dbc760bcSTejun Heo 			if (buf < end)
1229dbc760bcSTejun Heo 				*buf = ',';
1230dbc760bcSTejun Heo 			buf++;
1231dbc760bcSTejun Heo 		}
1232dbc760bcSTejun Heo 		first = false;
1233dbc760bcSTejun Heo 
1234ce0b4910SAndy Shevchenko 		buf = number(buf, end, rbot, default_dec_spec);
1235dbc760bcSTejun Heo 		if (rbot < rtop) {
1236dbc760bcSTejun Heo 			if (buf < end)
1237dbc760bcSTejun Heo 				*buf = '-';
1238dbc760bcSTejun Heo 			buf++;
1239dbc760bcSTejun Heo 
1240ce0b4910SAndy Shevchenko 			buf = number(buf, end, rtop, default_dec_spec);
1241dbc760bcSTejun Heo 		}
1242dbc760bcSTejun Heo 
1243dbc760bcSTejun Heo 		rbot = cur;
1244dbc760bcSTejun Heo 	}
1245dbc760bcSTejun Heo 	return buf;
1246dbc760bcSTejun Heo }
1247dbc760bcSTejun Heo 
1248dbc760bcSTejun Heo static noinline_for_stack
1249cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
12508a27f7c9SJoe Perches 			 struct printf_spec spec, const char *fmt)
1251dd45c9cfSHarvey Harrison {
12528a27f7c9SJoe Perches 	char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
1253dd45c9cfSHarvey Harrison 	char *p = mac_addr;
1254dd45c9cfSHarvey Harrison 	int i;
1255bc7259a2SJoe Perches 	char separator;
125676597ff9SAndrei Emeltchenko 	bool reversed = false;
1257bc7259a2SJoe Perches 
12583e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
12593e5903ebSPetr Mladek 		return buf;
12603e5903ebSPetr Mladek 
126176597ff9SAndrei Emeltchenko 	switch (fmt[1]) {
126276597ff9SAndrei Emeltchenko 	case 'F':
1263bc7259a2SJoe Perches 		separator = '-';
126476597ff9SAndrei Emeltchenko 		break;
126576597ff9SAndrei Emeltchenko 
126676597ff9SAndrei Emeltchenko 	case 'R':
126776597ff9SAndrei Emeltchenko 		reversed = true;
12684c1ca831SNick Desaulniers 		fallthrough;
126976597ff9SAndrei Emeltchenko 
127076597ff9SAndrei Emeltchenko 	default:
1271bc7259a2SJoe Perches 		separator = ':';
127276597ff9SAndrei Emeltchenko 		break;
1273bc7259a2SJoe Perches 	}
1274dd45c9cfSHarvey Harrison 
1275dd45c9cfSHarvey Harrison 	for (i = 0; i < 6; i++) {
127676597ff9SAndrei Emeltchenko 		if (reversed)
127776597ff9SAndrei Emeltchenko 			p = hex_byte_pack(p, addr[5 - i]);
127876597ff9SAndrei Emeltchenko 		else
127955036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[i]);
128076597ff9SAndrei Emeltchenko 
12818a27f7c9SJoe Perches 		if (fmt[0] == 'M' && i != 5)
1282bc7259a2SJoe Perches 			*p++ = separator;
1283dd45c9cfSHarvey Harrison 	}
1284dd45c9cfSHarvey Harrison 	*p = '\0';
1285dd45c9cfSHarvey Harrison 
1286d529ac41SPetr Mladek 	return string_nocheck(buf, end, mac_addr, spec);
1287dd45c9cfSHarvey Harrison }
1288dd45c9cfSHarvey Harrison 
1289cf3b429bSJoe Perches static noinline_for_stack
1290cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
1291689afa7dSHarvey Harrison {
1292689afa7dSHarvey Harrison 	int i;
12930159f24eSJoe Perches 	bool leading_zeros = (fmt[0] == 'i');
12940159f24eSJoe Perches 	int index;
12950159f24eSJoe Perches 	int step;
1296689afa7dSHarvey Harrison 
12970159f24eSJoe Perches 	switch (fmt[2]) {
12980159f24eSJoe Perches 	case 'h':
12990159f24eSJoe Perches #ifdef __BIG_ENDIAN
13000159f24eSJoe Perches 		index = 0;
13010159f24eSJoe Perches 		step = 1;
13020159f24eSJoe Perches #else
13030159f24eSJoe Perches 		index = 3;
13040159f24eSJoe Perches 		step = -1;
13050159f24eSJoe Perches #endif
13060159f24eSJoe Perches 		break;
13070159f24eSJoe Perches 	case 'l':
13080159f24eSJoe Perches 		index = 3;
13090159f24eSJoe Perches 		step = -1;
13100159f24eSJoe Perches 		break;
13110159f24eSJoe Perches 	case 'n':
13120159f24eSJoe Perches 	case 'b':
13130159f24eSJoe Perches 	default:
13140159f24eSJoe Perches 		index = 0;
13150159f24eSJoe Perches 		step = 1;
13160159f24eSJoe Perches 		break;
13170159f24eSJoe Perches 	}
13188a27f7c9SJoe Perches 	for (i = 0; i < 4; i++) {
13197c43d9a3SRasmus Villemoes 		char temp[4] __aligned(2);	/* hold each IP quad in reverse order */
1320133fd9f5SDenys Vlasenko 		int digits = put_dec_trunc8(temp, addr[index]) - temp;
13218a27f7c9SJoe Perches 		if (leading_zeros) {
13228a27f7c9SJoe Perches 			if (digits < 3)
13238a27f7c9SJoe Perches 				*p++ = '0';
13248a27f7c9SJoe Perches 			if (digits < 2)
13258a27f7c9SJoe Perches 				*p++ = '0';
13268a27f7c9SJoe Perches 		}
13278a27f7c9SJoe Perches 		/* reverse the digits in the quad */
13288a27f7c9SJoe Perches 		while (digits--)
13298a27f7c9SJoe Perches 			*p++ = temp[digits];
13308a27f7c9SJoe Perches 		if (i < 3)
13318a27f7c9SJoe Perches 			*p++ = '.';
13320159f24eSJoe Perches 		index += step;
13338a27f7c9SJoe Perches 	}
13348a27f7c9SJoe Perches 	*p = '\0';
13357b9186f5SAndré Goddard Rosa 
13368a27f7c9SJoe Perches 	return p;
13378a27f7c9SJoe Perches }
13388a27f7c9SJoe Perches 
1339cf3b429bSJoe Perches static noinline_for_stack
1340cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
13418a27f7c9SJoe Perches {
13427b9186f5SAndré Goddard Rosa 	int i, j, range;
13438a27f7c9SJoe Perches 	unsigned char zerolength[8];
13448a27f7c9SJoe Perches 	int longest = 1;
13458a27f7c9SJoe Perches 	int colonpos = -1;
13468a27f7c9SJoe Perches 	u16 word;
13477b9186f5SAndré Goddard Rosa 	u8 hi, lo;
13488a27f7c9SJoe Perches 	bool needcolon = false;
1349eb78cd26SJoe Perches 	bool useIPv4;
1350eb78cd26SJoe Perches 	struct in6_addr in6;
1351eb78cd26SJoe Perches 
1352eb78cd26SJoe Perches 	memcpy(&in6, addr, sizeof(struct in6_addr));
1353eb78cd26SJoe Perches 
1354eb78cd26SJoe Perches 	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
13558a27f7c9SJoe Perches 
13568a27f7c9SJoe Perches 	memset(zerolength, 0, sizeof(zerolength));
13578a27f7c9SJoe Perches 
13588a27f7c9SJoe Perches 	if (useIPv4)
13598a27f7c9SJoe Perches 		range = 6;
13608a27f7c9SJoe Perches 	else
13618a27f7c9SJoe Perches 		range = 8;
13628a27f7c9SJoe Perches 
13638a27f7c9SJoe Perches 	/* find position of longest 0 run */
13648a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
13658a27f7c9SJoe Perches 		for (j = i; j < range; j++) {
1366eb78cd26SJoe Perches 			if (in6.s6_addr16[j] != 0)
13678a27f7c9SJoe Perches 				break;
13688a27f7c9SJoe Perches 			zerolength[i]++;
13698a27f7c9SJoe Perches 		}
13708a27f7c9SJoe Perches 	}
13718a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
13728a27f7c9SJoe Perches 		if (zerolength[i] > longest) {
13738a27f7c9SJoe Perches 			longest = zerolength[i];
13748a27f7c9SJoe Perches 			colonpos = i;
13758a27f7c9SJoe Perches 		}
13768a27f7c9SJoe Perches 	}
137729cf519eSJoe Perches 	if (longest == 1)		/* don't compress a single 0 */
137829cf519eSJoe Perches 		colonpos = -1;
13798a27f7c9SJoe Perches 
13808a27f7c9SJoe Perches 	/* emit address */
13818a27f7c9SJoe Perches 	for (i = 0; i < range; i++) {
13828a27f7c9SJoe Perches 		if (i == colonpos) {
13838a27f7c9SJoe Perches 			if (needcolon || i == 0)
13848a27f7c9SJoe Perches 				*p++ = ':';
13858a27f7c9SJoe Perches 			*p++ = ':';
13868a27f7c9SJoe Perches 			needcolon = false;
13878a27f7c9SJoe Perches 			i += longest - 1;
13888a27f7c9SJoe Perches 			continue;
13898a27f7c9SJoe Perches 		}
13908a27f7c9SJoe Perches 		if (needcolon) {
13918a27f7c9SJoe Perches 			*p++ = ':';
13928a27f7c9SJoe Perches 			needcolon = false;
13938a27f7c9SJoe Perches 		}
13948a27f7c9SJoe Perches 		/* hex u16 without leading 0s */
1395eb78cd26SJoe Perches 		word = ntohs(in6.s6_addr16[i]);
13968a27f7c9SJoe Perches 		hi = word >> 8;
13978a27f7c9SJoe Perches 		lo = word & 0xff;
13988a27f7c9SJoe Perches 		if (hi) {
13998a27f7c9SJoe Perches 			if (hi > 0x0f)
140055036ba7SAndy Shevchenko 				p = hex_byte_pack(p, hi);
14018a27f7c9SJoe Perches 			else
14028a27f7c9SJoe Perches 				*p++ = hex_asc_lo(hi);
140355036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
14048a27f7c9SJoe Perches 		}
1405b5ff992bSAndré Goddard Rosa 		else if (lo > 0x0f)
140655036ba7SAndy Shevchenko 			p = hex_byte_pack(p, lo);
14078a27f7c9SJoe Perches 		else
14088a27f7c9SJoe Perches 			*p++ = hex_asc_lo(lo);
14098a27f7c9SJoe Perches 		needcolon = true;
14108a27f7c9SJoe Perches 	}
14118a27f7c9SJoe Perches 
14128a27f7c9SJoe Perches 	if (useIPv4) {
14138a27f7c9SJoe Perches 		if (needcolon)
14148a27f7c9SJoe Perches 			*p++ = ':';
14150159f24eSJoe Perches 		p = ip4_string(p, &in6.s6_addr[12], "I4");
14168a27f7c9SJoe Perches 	}
14178a27f7c9SJoe Perches 	*p = '\0';
14187b9186f5SAndré Goddard Rosa 
14198a27f7c9SJoe Perches 	return p;
14208a27f7c9SJoe Perches }
14218a27f7c9SJoe Perches 
1422cf3b429bSJoe Perches static noinline_for_stack
1423cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
14248a27f7c9SJoe Perches {
14258a27f7c9SJoe Perches 	int i;
14267b9186f5SAndré Goddard Rosa 
1427689afa7dSHarvey Harrison 	for (i = 0; i < 8; i++) {
142855036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
142955036ba7SAndy Shevchenko 		p = hex_byte_pack(p, *addr++);
14308a27f7c9SJoe Perches 		if (fmt[0] == 'I' && i != 7)
1431689afa7dSHarvey Harrison 			*p++ = ':';
1432689afa7dSHarvey Harrison 	}
1433689afa7dSHarvey Harrison 	*p = '\0';
14347b9186f5SAndré Goddard Rosa 
14358a27f7c9SJoe Perches 	return p;
14368a27f7c9SJoe Perches }
14378a27f7c9SJoe Perches 
1438cf3b429bSJoe Perches static noinline_for_stack
1439cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
14408a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
14418a27f7c9SJoe Perches {
14428a27f7c9SJoe Perches 	char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
14438a27f7c9SJoe Perches 
14448a27f7c9SJoe Perches 	if (fmt[0] == 'I' && fmt[2] == 'c')
1445eb78cd26SJoe Perches 		ip6_compressed_string(ip6_addr, addr);
14468a27f7c9SJoe Perches 	else
1447eb78cd26SJoe Perches 		ip6_string(ip6_addr, addr, fmt);
1448689afa7dSHarvey Harrison 
1449d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip6_addr, spec);
1450689afa7dSHarvey Harrison }
1451689afa7dSHarvey Harrison 
1452cf3b429bSJoe Perches static noinline_for_stack
1453cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
14548a27f7c9SJoe Perches 		      struct printf_spec spec, const char *fmt)
14554aa99606SHarvey Harrison {
14568a27f7c9SJoe Perches 	char ip4_addr[sizeof("255.255.255.255")];
14574aa99606SHarvey Harrison 
14580159f24eSJoe Perches 	ip4_string(ip4_addr, addr, fmt);
14594aa99606SHarvey Harrison 
1460d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip4_addr, spec);
14614aa99606SHarvey Harrison }
14624aa99606SHarvey Harrison 
1463cf3b429bSJoe Perches static noinline_for_stack
146410679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
146510679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
146610679643SDaniel Borkmann {
146710679643SDaniel Borkmann 	bool have_p = false, have_s = false, have_f = false, have_c = false;
146810679643SDaniel Borkmann 	char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
146910679643SDaniel Borkmann 		      sizeof(":12345") + sizeof("/123456789") +
147010679643SDaniel Borkmann 		      sizeof("%1234567890")];
147110679643SDaniel Borkmann 	char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
147210679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin6_addr;
147310679643SDaniel Borkmann 	char fmt6[2] = { fmt[0], '6' };
147410679643SDaniel Borkmann 	u8 off = 0;
147510679643SDaniel Borkmann 
147610679643SDaniel Borkmann 	fmt++;
147710679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
147810679643SDaniel Borkmann 		switch (*fmt) {
147910679643SDaniel Borkmann 		case 'p':
148010679643SDaniel Borkmann 			have_p = true;
148110679643SDaniel Borkmann 			break;
148210679643SDaniel Borkmann 		case 'f':
148310679643SDaniel Borkmann 			have_f = true;
148410679643SDaniel Borkmann 			break;
148510679643SDaniel Borkmann 		case 's':
148610679643SDaniel Borkmann 			have_s = true;
148710679643SDaniel Borkmann 			break;
148810679643SDaniel Borkmann 		case 'c':
148910679643SDaniel Borkmann 			have_c = true;
149010679643SDaniel Borkmann 			break;
149110679643SDaniel Borkmann 		}
149210679643SDaniel Borkmann 	}
149310679643SDaniel Borkmann 
149410679643SDaniel Borkmann 	if (have_p || have_s || have_f) {
149510679643SDaniel Borkmann 		*p = '[';
149610679643SDaniel Borkmann 		off = 1;
149710679643SDaniel Borkmann 	}
149810679643SDaniel Borkmann 
149910679643SDaniel Borkmann 	if (fmt6[0] == 'I' && have_c)
150010679643SDaniel Borkmann 		p = ip6_compressed_string(ip6_addr + off, addr);
150110679643SDaniel Borkmann 	else
150210679643SDaniel Borkmann 		p = ip6_string(ip6_addr + off, addr, fmt6);
150310679643SDaniel Borkmann 
150410679643SDaniel Borkmann 	if (have_p || have_s || have_f)
150510679643SDaniel Borkmann 		*p++ = ']';
150610679643SDaniel Borkmann 
150710679643SDaniel Borkmann 	if (have_p) {
150810679643SDaniel Borkmann 		*p++ = ':';
150910679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin6_port), spec);
151010679643SDaniel Borkmann 	}
151110679643SDaniel Borkmann 	if (have_f) {
151210679643SDaniel Borkmann 		*p++ = '/';
151310679643SDaniel Borkmann 		p = number(p, pend, ntohl(sa->sin6_flowinfo &
151410679643SDaniel Borkmann 					  IPV6_FLOWINFO_MASK), spec);
151510679643SDaniel Borkmann 	}
151610679643SDaniel Borkmann 	if (have_s) {
151710679643SDaniel Borkmann 		*p++ = '%';
151810679643SDaniel Borkmann 		p = number(p, pend, sa->sin6_scope_id, spec);
151910679643SDaniel Borkmann 	}
152010679643SDaniel Borkmann 	*p = '\0';
152110679643SDaniel Borkmann 
1522d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip6_addr, spec);
152310679643SDaniel Borkmann }
152410679643SDaniel Borkmann 
152510679643SDaniel Borkmann static noinline_for_stack
152610679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
152710679643SDaniel Borkmann 			 struct printf_spec spec, const char *fmt)
152810679643SDaniel Borkmann {
152910679643SDaniel Borkmann 	bool have_p = false;
153010679643SDaniel Borkmann 	char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
153110679643SDaniel Borkmann 	char *pend = ip4_addr + sizeof(ip4_addr);
153210679643SDaniel Borkmann 	const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
153310679643SDaniel Borkmann 	char fmt4[3] = { fmt[0], '4', 0 };
153410679643SDaniel Borkmann 
153510679643SDaniel Borkmann 	fmt++;
153610679643SDaniel Borkmann 	while (isalpha(*++fmt)) {
153710679643SDaniel Borkmann 		switch (*fmt) {
153810679643SDaniel Borkmann 		case 'p':
153910679643SDaniel Borkmann 			have_p = true;
154010679643SDaniel Borkmann 			break;
154110679643SDaniel Borkmann 		case 'h':
154210679643SDaniel Borkmann 		case 'l':
154310679643SDaniel Borkmann 		case 'n':
154410679643SDaniel Borkmann 		case 'b':
154510679643SDaniel Borkmann 			fmt4[2] = *fmt;
154610679643SDaniel Borkmann 			break;
154710679643SDaniel Borkmann 		}
154810679643SDaniel Borkmann 	}
154910679643SDaniel Borkmann 
155010679643SDaniel Borkmann 	p = ip4_string(ip4_addr, addr, fmt4);
155110679643SDaniel Borkmann 	if (have_p) {
155210679643SDaniel Borkmann 		*p++ = ':';
155310679643SDaniel Borkmann 		p = number(p, pend, ntohs(sa->sin_port), spec);
155410679643SDaniel Borkmann 	}
155510679643SDaniel Borkmann 	*p = '\0';
155610679643SDaniel Borkmann 
1557d529ac41SPetr Mladek 	return string_nocheck(buf, end, ip4_addr, spec);
155810679643SDaniel Borkmann }
155910679643SDaniel Borkmann 
156010679643SDaniel Borkmann static noinline_for_stack
1561f00cc102SPetr Mladek char *ip_addr_string(char *buf, char *end, const void *ptr,
1562f00cc102SPetr Mladek 		     struct printf_spec spec, const char *fmt)
1563f00cc102SPetr Mladek {
15640b74d4d7SPetr Mladek 	char *err_fmt_msg;
15650b74d4d7SPetr Mladek 
15663e5903ebSPetr Mladek 	if (check_pointer(&buf, end, ptr, spec))
15673e5903ebSPetr Mladek 		return buf;
15683e5903ebSPetr Mladek 
1569f00cc102SPetr Mladek 	switch (fmt[1]) {
1570f00cc102SPetr Mladek 	case '6':
1571f00cc102SPetr Mladek 		return ip6_addr_string(buf, end, ptr, spec, fmt);
1572f00cc102SPetr Mladek 	case '4':
1573f00cc102SPetr Mladek 		return ip4_addr_string(buf, end, ptr, spec, fmt);
1574f00cc102SPetr Mladek 	case 'S': {
1575f00cc102SPetr Mladek 		const union {
1576f00cc102SPetr Mladek 			struct sockaddr		raw;
1577f00cc102SPetr Mladek 			struct sockaddr_in	v4;
1578f00cc102SPetr Mladek 			struct sockaddr_in6	v6;
1579f00cc102SPetr Mladek 		} *sa = ptr;
1580f00cc102SPetr Mladek 
1581f00cc102SPetr Mladek 		switch (sa->raw.sa_family) {
1582f00cc102SPetr Mladek 		case AF_INET:
1583f00cc102SPetr Mladek 			return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1584f00cc102SPetr Mladek 		case AF_INET6:
1585f00cc102SPetr Mladek 			return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1586f00cc102SPetr Mladek 		default:
1587c8c3b584SPetr Mladek 			return error_string(buf, end, "(einval)", spec);
1588f00cc102SPetr Mladek 		}}
1589f00cc102SPetr Mladek 	}
1590f00cc102SPetr Mladek 
15910b74d4d7SPetr Mladek 	err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
1592c8c3b584SPetr Mladek 	return error_string(buf, end, err_fmt_msg, spec);
1593f00cc102SPetr Mladek }
1594f00cc102SPetr Mladek 
1595f00cc102SPetr Mladek static noinline_for_stack
159671dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
159771dca95dSAndy Shevchenko 		     const char *fmt)
159871dca95dSAndy Shevchenko {
159971dca95dSAndy Shevchenko 	bool found = true;
160071dca95dSAndy Shevchenko 	int count = 1;
160171dca95dSAndy Shevchenko 	unsigned int flags = 0;
160271dca95dSAndy Shevchenko 	int len;
160371dca95dSAndy Shevchenko 
160471dca95dSAndy Shevchenko 	if (spec.field_width == 0)
160571dca95dSAndy Shevchenko 		return buf;				/* nothing to print */
160671dca95dSAndy Shevchenko 
16073e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
16083e5903ebSPetr Mladek 		return buf;
160971dca95dSAndy Shevchenko 
161071dca95dSAndy Shevchenko 	do {
161171dca95dSAndy Shevchenko 		switch (fmt[count++]) {
161271dca95dSAndy Shevchenko 		case 'a':
161371dca95dSAndy Shevchenko 			flags |= ESCAPE_ANY;
161471dca95dSAndy Shevchenko 			break;
161571dca95dSAndy Shevchenko 		case 'c':
161671dca95dSAndy Shevchenko 			flags |= ESCAPE_SPECIAL;
161771dca95dSAndy Shevchenko 			break;
161871dca95dSAndy Shevchenko 		case 'h':
161971dca95dSAndy Shevchenko 			flags |= ESCAPE_HEX;
162071dca95dSAndy Shevchenko 			break;
162171dca95dSAndy Shevchenko 		case 'n':
162271dca95dSAndy Shevchenko 			flags |= ESCAPE_NULL;
162371dca95dSAndy Shevchenko 			break;
162471dca95dSAndy Shevchenko 		case 'o':
162571dca95dSAndy Shevchenko 			flags |= ESCAPE_OCTAL;
162671dca95dSAndy Shevchenko 			break;
162771dca95dSAndy Shevchenko 		case 'p':
162871dca95dSAndy Shevchenko 			flags |= ESCAPE_NP;
162971dca95dSAndy Shevchenko 			break;
163071dca95dSAndy Shevchenko 		case 's':
163171dca95dSAndy Shevchenko 			flags |= ESCAPE_SPACE;
163271dca95dSAndy Shevchenko 			break;
163371dca95dSAndy Shevchenko 		default:
163471dca95dSAndy Shevchenko 			found = false;
163571dca95dSAndy Shevchenko 			break;
163671dca95dSAndy Shevchenko 		}
163771dca95dSAndy Shevchenko 	} while (found);
163871dca95dSAndy Shevchenko 
163971dca95dSAndy Shevchenko 	if (!flags)
164071dca95dSAndy Shevchenko 		flags = ESCAPE_ANY_NP;
164171dca95dSAndy Shevchenko 
164271dca95dSAndy Shevchenko 	len = spec.field_width < 0 ? 1 : spec.field_width;
164371dca95dSAndy Shevchenko 
164441416f23SRasmus Villemoes 	/*
164541416f23SRasmus Villemoes 	 * string_escape_mem() writes as many characters as it can to
164641416f23SRasmus Villemoes 	 * the given buffer, and returns the total size of the output
164741416f23SRasmus Villemoes 	 * had the buffer been big enough.
164841416f23SRasmus Villemoes 	 */
164941416f23SRasmus Villemoes 	buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
165071dca95dSAndy Shevchenko 
165171dca95dSAndy Shevchenko 	return buf;
165271dca95dSAndy Shevchenko }
165371dca95dSAndy Shevchenko 
16543e5903ebSPetr Mladek static char *va_format(char *buf, char *end, struct va_format *va_fmt,
16553e5903ebSPetr Mladek 		       struct printf_spec spec, const char *fmt)
165645c3e93dSPetr Mladek {
165745c3e93dSPetr Mladek 	va_list va;
165845c3e93dSPetr Mladek 
16593e5903ebSPetr Mladek 	if (check_pointer(&buf, end, va_fmt, spec))
16603e5903ebSPetr Mladek 		return buf;
16613e5903ebSPetr Mladek 
166245c3e93dSPetr Mladek 	va_copy(va, *va_fmt->va);
166345c3e93dSPetr Mladek 	buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
166445c3e93dSPetr Mladek 	va_end(va);
166545c3e93dSPetr Mladek 
166645c3e93dSPetr Mladek 	return buf;
166745c3e93dSPetr Mladek }
166845c3e93dSPetr Mladek 
166971dca95dSAndy Shevchenko static noinline_for_stack
1670cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
16719ac6e44eSJoe Perches 		  struct printf_spec spec, const char *fmt)
16729ac6e44eSJoe Perches {
16732b1b0d66SAndy Shevchenko 	char uuid[UUID_STRING_LEN + 1];
16749ac6e44eSJoe Perches 	char *p = uuid;
16759ac6e44eSJoe Perches 	int i;
1676f9727a17SChristoph Hellwig 	const u8 *index = uuid_index;
16779ac6e44eSJoe Perches 	bool uc = false;
16789ac6e44eSJoe Perches 
16793e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
16803e5903ebSPetr Mladek 		return buf;
16813e5903ebSPetr Mladek 
16829ac6e44eSJoe Perches 	switch (*(++fmt)) {
16839ac6e44eSJoe Perches 	case 'L':
1684df561f66SGustavo A. R. Silva 		uc = true;
16854c1ca831SNick Desaulniers 		fallthrough;
16869ac6e44eSJoe Perches 	case 'l':
1687f9727a17SChristoph Hellwig 		index = guid_index;
16889ac6e44eSJoe Perches 		break;
16899ac6e44eSJoe Perches 	case 'B':
16909ac6e44eSJoe Perches 		uc = true;
16919ac6e44eSJoe Perches 		break;
16929ac6e44eSJoe Perches 	}
16939ac6e44eSJoe Perches 
16949ac6e44eSJoe Perches 	for (i = 0; i < 16; i++) {
1695aa4ea1c3SAndy Shevchenko 		if (uc)
1696aa4ea1c3SAndy Shevchenko 			p = hex_byte_pack_upper(p, addr[index[i]]);
1697aa4ea1c3SAndy Shevchenko 		else
169855036ba7SAndy Shevchenko 			p = hex_byte_pack(p, addr[index[i]]);
16999ac6e44eSJoe Perches 		switch (i) {
17009ac6e44eSJoe Perches 		case 3:
17019ac6e44eSJoe Perches 		case 5:
17029ac6e44eSJoe Perches 		case 7:
17039ac6e44eSJoe Perches 		case 9:
17049ac6e44eSJoe Perches 			*p++ = '-';
17059ac6e44eSJoe Perches 			break;
17069ac6e44eSJoe Perches 		}
17079ac6e44eSJoe Perches 	}
17089ac6e44eSJoe Perches 
17099ac6e44eSJoe Perches 	*p = 0;
17109ac6e44eSJoe Perches 
1711d529ac41SPetr Mladek 	return string_nocheck(buf, end, uuid, spec);
17129ac6e44eSJoe Perches }
17139ac6e44eSJoe Perches 
17145b17aecfSAndy Shevchenko static noinline_for_stack
1715431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr,
1716431bca24SGeert Uytterhoeven 		  struct printf_spec spec,  const char *fmt)
1717c8f44affSMichał Mirosław {
17185b17aecfSAndy Shevchenko 	unsigned long long num;
17195b17aecfSAndy Shevchenko 	int size;
17205b17aecfSAndy Shevchenko 
17213e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
17223e5903ebSPetr Mladek 		return buf;
17233e5903ebSPetr Mladek 
17245b17aecfSAndy Shevchenko 	switch (fmt[1]) {
17255b17aecfSAndy Shevchenko 	case 'F':
17265b17aecfSAndy Shevchenko 		num = *(const netdev_features_t *)addr;
17275b17aecfSAndy Shevchenko 		size = sizeof(netdev_features_t);
17285b17aecfSAndy Shevchenko 		break;
17295b17aecfSAndy Shevchenko 	default:
1730c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pN?)", spec);
17315b17aecfSAndy Shevchenko 	}
1732c8f44affSMichał Mirosław 
17333cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, num, size);
1734c8f44affSMichał Mirosław }
1735c8f44affSMichał Mirosław 
1736aaf07621SJoe Perches static noinline_for_stack
1737*af612e43SSakari Ailus char *fourcc_string(char *buf, char *end, const u32 *fourcc,
1738*af612e43SSakari Ailus 		    struct printf_spec spec, const char *fmt)
1739*af612e43SSakari Ailus {
1740*af612e43SSakari Ailus 	char output[sizeof("0123 little-endian (0x01234567)")];
1741*af612e43SSakari Ailus 	char *p = output;
1742*af612e43SSakari Ailus 	unsigned int i;
1743*af612e43SSakari Ailus 	u32 val;
1744*af612e43SSakari Ailus 
1745*af612e43SSakari Ailus 	if (fmt[1] != 'c' || fmt[2] != 'c')
1746*af612e43SSakari Ailus 		return error_string(buf, end, "(%p4?)", spec);
1747*af612e43SSakari Ailus 
1748*af612e43SSakari Ailus 	if (check_pointer(&buf, end, fourcc, spec))
1749*af612e43SSakari Ailus 		return buf;
1750*af612e43SSakari Ailus 
1751*af612e43SSakari Ailus 	val = *fourcc & ~BIT(31);
1752*af612e43SSakari Ailus 
1753*af612e43SSakari Ailus 	for (i = 0; i < sizeof(*fourcc); i++) {
1754*af612e43SSakari Ailus 		unsigned char c = val >> (i * 8);
1755*af612e43SSakari Ailus 
1756*af612e43SSakari Ailus 		/* Print non-control ASCII characters as-is, dot otherwise */
1757*af612e43SSakari Ailus 		*p++ = isascii(c) && isprint(c) ? c : '.';
1758*af612e43SSakari Ailus 	}
1759*af612e43SSakari Ailus 
1760*af612e43SSakari Ailus 	strcpy(p, *fourcc & BIT(31) ? " big-endian" : " little-endian");
1761*af612e43SSakari Ailus 	p += strlen(p);
1762*af612e43SSakari Ailus 
1763*af612e43SSakari Ailus 	*p++ = ' ';
1764*af612e43SSakari Ailus 	*p++ = '(';
1765*af612e43SSakari Ailus 	p = special_hex_number(p, output + sizeof(output) - 2, *fourcc, sizeof(u32));
1766*af612e43SSakari Ailus 	*p++ = ')';
1767*af612e43SSakari Ailus 	*p = '\0';
1768*af612e43SSakari Ailus 
1769*af612e43SSakari Ailus 	return string(buf, end, output, spec);
1770*af612e43SSakari Ailus }
1771*af612e43SSakari Ailus 
1772*af612e43SSakari Ailus static noinline_for_stack
17733e5903ebSPetr Mladek char *address_val(char *buf, char *end, const void *addr,
17743e5903ebSPetr Mladek 		  struct printf_spec spec, const char *fmt)
1775aaf07621SJoe Perches {
1776aaf07621SJoe Perches 	unsigned long long num;
17773cab1e71SAndy Shevchenko 	int size;
1778aaf07621SJoe Perches 
17793e5903ebSPetr Mladek 	if (check_pointer(&buf, end, addr, spec))
17803e5903ebSPetr Mladek 		return buf;
17813e5903ebSPetr Mladek 
1782aaf07621SJoe Perches 	switch (fmt[1]) {
1783aaf07621SJoe Perches 	case 'd':
1784aaf07621SJoe Perches 		num = *(const dma_addr_t *)addr;
17853cab1e71SAndy Shevchenko 		size = sizeof(dma_addr_t);
1786aaf07621SJoe Perches 		break;
1787aaf07621SJoe Perches 	case 'p':
1788aaf07621SJoe Perches 	default:
1789aaf07621SJoe Perches 		num = *(const phys_addr_t *)addr;
17903cab1e71SAndy Shevchenko 		size = sizeof(phys_addr_t);
1791aaf07621SJoe Perches 		break;
1792aaf07621SJoe Perches 	}
1793aaf07621SJoe Perches 
17943cab1e71SAndy Shevchenko 	return special_hex_number(buf, end, num, size);
1795aaf07621SJoe Perches }
1796aaf07621SJoe Perches 
1797900cca29SGeert Uytterhoeven static noinline_for_stack
17984d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
17994d42c447SAndy Shevchenko {
18004d42c447SAndy Shevchenko 	int year = tm->tm_year + (r ? 0 : 1900);
18014d42c447SAndy Shevchenko 	int mon = tm->tm_mon + (r ? 0 : 1);
18024d42c447SAndy Shevchenko 
18034d42c447SAndy Shevchenko 	buf = number(buf, end, year, default_dec04_spec);
18044d42c447SAndy Shevchenko 	if (buf < end)
18054d42c447SAndy Shevchenko 		*buf = '-';
18064d42c447SAndy Shevchenko 	buf++;
18074d42c447SAndy Shevchenko 
18084d42c447SAndy Shevchenko 	buf = number(buf, end, mon, default_dec02_spec);
18094d42c447SAndy Shevchenko 	if (buf < end)
18104d42c447SAndy Shevchenko 		*buf = '-';
18114d42c447SAndy Shevchenko 	buf++;
18124d42c447SAndy Shevchenko 
18134d42c447SAndy Shevchenko 	return number(buf, end, tm->tm_mday, default_dec02_spec);
18144d42c447SAndy Shevchenko }
18154d42c447SAndy Shevchenko 
18164d42c447SAndy Shevchenko static noinline_for_stack
18174d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
18184d42c447SAndy Shevchenko {
18194d42c447SAndy Shevchenko 	buf = number(buf, end, tm->tm_hour, default_dec02_spec);
18204d42c447SAndy Shevchenko 	if (buf < end)
18214d42c447SAndy Shevchenko 		*buf = ':';
18224d42c447SAndy Shevchenko 	buf++;
18234d42c447SAndy Shevchenko 
18244d42c447SAndy Shevchenko 	buf = number(buf, end, tm->tm_min, default_dec02_spec);
18254d42c447SAndy Shevchenko 	if (buf < end)
18264d42c447SAndy Shevchenko 		*buf = ':';
18274d42c447SAndy Shevchenko 	buf++;
18284d42c447SAndy Shevchenko 
18294d42c447SAndy Shevchenko 	return number(buf, end, tm->tm_sec, default_dec02_spec);
18304d42c447SAndy Shevchenko }
18314d42c447SAndy Shevchenko 
18324d42c447SAndy Shevchenko static noinline_for_stack
18333e5903ebSPetr Mladek char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
18343e5903ebSPetr Mladek 	      struct printf_spec spec, const char *fmt)
18354d42c447SAndy Shevchenko {
18364d42c447SAndy Shevchenko 	bool have_t = true, have_d = true;
18374d42c447SAndy Shevchenko 	bool raw = false;
18384d42c447SAndy Shevchenko 	int count = 2;
18394d42c447SAndy Shevchenko 
18403e5903ebSPetr Mladek 	if (check_pointer(&buf, end, tm, spec))
18413e5903ebSPetr Mladek 		return buf;
18423e5903ebSPetr Mladek 
18434d42c447SAndy Shevchenko 	switch (fmt[count]) {
18444d42c447SAndy Shevchenko 	case 'd':
18454d42c447SAndy Shevchenko 		have_t = false;
18464d42c447SAndy Shevchenko 		count++;
18474d42c447SAndy Shevchenko 		break;
18484d42c447SAndy Shevchenko 	case 't':
18494d42c447SAndy Shevchenko 		have_d = false;
18504d42c447SAndy Shevchenko 		count++;
18514d42c447SAndy Shevchenko 		break;
18524d42c447SAndy Shevchenko 	}
18534d42c447SAndy Shevchenko 
18544d42c447SAndy Shevchenko 	raw = fmt[count] == 'r';
18554d42c447SAndy Shevchenko 
18564d42c447SAndy Shevchenko 	if (have_d)
18574d42c447SAndy Shevchenko 		buf = date_str(buf, end, tm, raw);
18584d42c447SAndy Shevchenko 	if (have_d && have_t) {
18594d42c447SAndy Shevchenko 		/* Respect ISO 8601 */
18604d42c447SAndy Shevchenko 		if (buf < end)
18614d42c447SAndy Shevchenko 			*buf = 'T';
18624d42c447SAndy Shevchenko 		buf++;
18634d42c447SAndy Shevchenko 	}
18644d42c447SAndy Shevchenko 	if (have_t)
18654d42c447SAndy Shevchenko 		buf = time_str(buf, end, tm, raw);
18664d42c447SAndy Shevchenko 
18674d42c447SAndy Shevchenko 	return buf;
18684d42c447SAndy Shevchenko }
18694d42c447SAndy Shevchenko 
18704d42c447SAndy Shevchenko static noinline_for_stack
18717daac5b2SAndy Shevchenko char *time64_str(char *buf, char *end, const time64_t time,
18727daac5b2SAndy Shevchenko 		 struct printf_spec spec, const char *fmt)
18737daac5b2SAndy Shevchenko {
18747daac5b2SAndy Shevchenko 	struct rtc_time rtc_time;
18757daac5b2SAndy Shevchenko 	struct tm tm;
18767daac5b2SAndy Shevchenko 
18777daac5b2SAndy Shevchenko 	time64_to_tm(time, 0, &tm);
18787daac5b2SAndy Shevchenko 
18797daac5b2SAndy Shevchenko 	rtc_time.tm_sec = tm.tm_sec;
18807daac5b2SAndy Shevchenko 	rtc_time.tm_min = tm.tm_min;
18817daac5b2SAndy Shevchenko 	rtc_time.tm_hour = tm.tm_hour;
18827daac5b2SAndy Shevchenko 	rtc_time.tm_mday = tm.tm_mday;
18837daac5b2SAndy Shevchenko 	rtc_time.tm_mon = tm.tm_mon;
18847daac5b2SAndy Shevchenko 	rtc_time.tm_year = tm.tm_year;
18857daac5b2SAndy Shevchenko 	rtc_time.tm_wday = tm.tm_wday;
18867daac5b2SAndy Shevchenko 	rtc_time.tm_yday = tm.tm_yday;
18877daac5b2SAndy Shevchenko 
18887daac5b2SAndy Shevchenko 	rtc_time.tm_isdst = 0;
18897daac5b2SAndy Shevchenko 
18907daac5b2SAndy Shevchenko 	return rtc_str(buf, end, &rtc_time, spec, fmt);
18917daac5b2SAndy Shevchenko }
18927daac5b2SAndy Shevchenko 
18937daac5b2SAndy Shevchenko static noinline_for_stack
18944d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
18954d42c447SAndy Shevchenko 		    const char *fmt)
18964d42c447SAndy Shevchenko {
18974d42c447SAndy Shevchenko 	switch (fmt[1]) {
18984d42c447SAndy Shevchenko 	case 'R':
18993e5903ebSPetr Mladek 		return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
19007daac5b2SAndy Shevchenko 	case 'T':
19017daac5b2SAndy Shevchenko 		return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt);
19024d42c447SAndy Shevchenko 	default:
19037daac5b2SAndy Shevchenko 		return error_string(buf, end, "(%pt?)", spec);
19044d42c447SAndy Shevchenko 	}
19054d42c447SAndy Shevchenko }
19064d42c447SAndy Shevchenko 
19074d42c447SAndy Shevchenko static noinline_for_stack
1908900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1909900cca29SGeert Uytterhoeven 	    const char *fmt)
1910900cca29SGeert Uytterhoeven {
19110b74d4d7SPetr Mladek 	if (!IS_ENABLED(CONFIG_HAVE_CLK))
1912c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pC?)", spec);
19130b74d4d7SPetr Mladek 
19143e5903ebSPetr Mladek 	if (check_pointer(&buf, end, clk, spec))
19153e5903ebSPetr Mladek 		return buf;
1916900cca29SGeert Uytterhoeven 
1917900cca29SGeert Uytterhoeven 	switch (fmt[1]) {
1918900cca29SGeert Uytterhoeven 	case 'n':
1919900cca29SGeert Uytterhoeven 	default:
1920900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK
1921900cca29SGeert Uytterhoeven 		return string(buf, end, __clk_get_name(clk), spec);
1922900cca29SGeert Uytterhoeven #else
19234ca96aa9SGeert Uytterhoeven 		return ptr_to_id(buf, end, clk, spec);
1924900cca29SGeert Uytterhoeven #endif
1925900cca29SGeert Uytterhoeven 	}
1926900cca29SGeert Uytterhoeven }
1927900cca29SGeert Uytterhoeven 
1928edf14cdbSVlastimil Babka static
1929edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags,
1930edf14cdbSVlastimil Babka 					const struct trace_print_flags *names)
1931edf14cdbSVlastimil Babka {
1932edf14cdbSVlastimil Babka 	unsigned long mask;
1933edf14cdbSVlastimil Babka 
1934edf14cdbSVlastimil Babka 	for ( ; flags && names->name; names++) {
1935edf14cdbSVlastimil Babka 		mask = names->mask;
1936edf14cdbSVlastimil Babka 		if ((flags & mask) != mask)
1937edf14cdbSVlastimil Babka 			continue;
1938edf14cdbSVlastimil Babka 
1939abd4fe62SAndy Shevchenko 		buf = string(buf, end, names->name, default_str_spec);
1940edf14cdbSVlastimil Babka 
1941edf14cdbSVlastimil Babka 		flags &= ~mask;
1942edf14cdbSVlastimil Babka 		if (flags) {
1943edf14cdbSVlastimil Babka 			if (buf < end)
1944edf14cdbSVlastimil Babka 				*buf = '|';
1945edf14cdbSVlastimil Babka 			buf++;
1946edf14cdbSVlastimil Babka 		}
1947edf14cdbSVlastimil Babka 	}
1948edf14cdbSVlastimil Babka 
1949edf14cdbSVlastimil Babka 	if (flags)
195054433973SAndy Shevchenko 		buf = number(buf, end, flags, default_flag_spec);
1951edf14cdbSVlastimil Babka 
1952edf14cdbSVlastimil Babka 	return buf;
1953edf14cdbSVlastimil Babka }
1954edf14cdbSVlastimil Babka 
1955edf14cdbSVlastimil Babka static noinline_for_stack
19560b74d4d7SPetr Mladek char *flags_string(char *buf, char *end, void *flags_ptr,
19570b74d4d7SPetr Mladek 		   struct printf_spec spec, const char *fmt)
1958edf14cdbSVlastimil Babka {
1959edf14cdbSVlastimil Babka 	unsigned long flags;
1960edf14cdbSVlastimil Babka 	const struct trace_print_flags *names;
1961edf14cdbSVlastimil Babka 
19623e5903ebSPetr Mladek 	if (check_pointer(&buf, end, flags_ptr, spec))
19633e5903ebSPetr Mladek 		return buf;
19643e5903ebSPetr Mladek 
1965edf14cdbSVlastimil Babka 	switch (fmt[1]) {
1966edf14cdbSVlastimil Babka 	case 'p':
1967edf14cdbSVlastimil Babka 		flags = *(unsigned long *)flags_ptr;
1968edf14cdbSVlastimil Babka 		/* Remove zone id */
1969edf14cdbSVlastimil Babka 		flags &= (1UL << NR_PAGEFLAGS) - 1;
1970edf14cdbSVlastimil Babka 		names = pageflag_names;
1971edf14cdbSVlastimil Babka 		break;
1972edf14cdbSVlastimil Babka 	case 'v':
1973edf14cdbSVlastimil Babka 		flags = *(unsigned long *)flags_ptr;
1974edf14cdbSVlastimil Babka 		names = vmaflag_names;
1975edf14cdbSVlastimil Babka 		break;
1976edf14cdbSVlastimil Babka 	case 'g':
197730d497a0SAndy Shevchenko 		flags = (__force unsigned long)(*(gfp_t *)flags_ptr);
1978edf14cdbSVlastimil Babka 		names = gfpflag_names;
1979edf14cdbSVlastimil Babka 		break;
1980edf14cdbSVlastimil Babka 	default:
1981c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pG?)", spec);
1982edf14cdbSVlastimil Babka 	}
1983edf14cdbSVlastimil Babka 
1984edf14cdbSVlastimil Babka 	return format_flags(buf, end, flags, names);
1985edf14cdbSVlastimil Babka }
1986edf14cdbSVlastimil Babka 
1987ce4fecf1SPantelis Antoniou static noinline_for_stack
1988a92eb762SSakari Ailus char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
1989a92eb762SSakari Ailus 			      char *end)
1990ce4fecf1SPantelis Antoniou {
1991ce4fecf1SPantelis Antoniou 	int depth;
1992ce4fecf1SPantelis Antoniou 
1993a92eb762SSakari Ailus 	/* Loop starting from the root node to the current node. */
1994a92eb762SSakari Ailus 	for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
1995a92eb762SSakari Ailus 		struct fwnode_handle *__fwnode =
1996a92eb762SSakari Ailus 			fwnode_get_nth_parent(fwnode, depth);
1997ce4fecf1SPantelis Antoniou 
1998a92eb762SSakari Ailus 		buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
1999abd4fe62SAndy Shevchenko 			     default_str_spec);
2000a92eb762SSakari Ailus 		buf = string(buf, end, fwnode_get_name(__fwnode),
2001a92eb762SSakari Ailus 			     default_str_spec);
2002a92eb762SSakari Ailus 
2003a92eb762SSakari Ailus 		fwnode_handle_put(__fwnode);
2004ce4fecf1SPantelis Antoniou 	}
2005a92eb762SSakari Ailus 
2006ce4fecf1SPantelis Antoniou 	return buf;
2007ce4fecf1SPantelis Antoniou }
2008ce4fecf1SPantelis Antoniou 
2009ce4fecf1SPantelis Antoniou static noinline_for_stack
2010ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn,
2011ce4fecf1SPantelis Antoniou 			 struct printf_spec spec, const char *fmt)
2012ce4fecf1SPantelis Antoniou {
2013ce4fecf1SPantelis Antoniou 	char tbuf[sizeof("xxxx") + 1];
2014ce4fecf1SPantelis Antoniou 	const char *p;
2015ce4fecf1SPantelis Antoniou 	int ret;
2016ce4fecf1SPantelis Antoniou 	char *buf_start = buf;
2017ce4fecf1SPantelis Antoniou 	struct property *prop;
2018ce4fecf1SPantelis Antoniou 	bool has_mult, pass;
2019ce4fecf1SPantelis Antoniou 
2020ce4fecf1SPantelis Antoniou 	struct printf_spec str_spec = spec;
2021ce4fecf1SPantelis Antoniou 	str_spec.field_width = -1;
2022ce4fecf1SPantelis Antoniou 
202383abc5a7SSakari Ailus 	if (fmt[0] != 'F')
202483abc5a7SSakari Ailus 		return error_string(buf, end, "(%pO?)", spec);
202583abc5a7SSakari Ailus 
2026ce4fecf1SPantelis Antoniou 	if (!IS_ENABLED(CONFIG_OF))
2027c8c3b584SPetr Mladek 		return error_string(buf, end, "(%pOF?)", spec);
2028ce4fecf1SPantelis Antoniou 
20293e5903ebSPetr Mladek 	if (check_pointer(&buf, end, dn, spec))
20303e5903ebSPetr Mladek 		return buf;
2031ce4fecf1SPantelis Antoniou 
2032ce4fecf1SPantelis Antoniou 	/* simple case without anything any more format specifiers */
2033ce4fecf1SPantelis Antoniou 	fmt++;
2034ce4fecf1SPantelis Antoniou 	if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
2035ce4fecf1SPantelis Antoniou 		fmt = "f";
2036ce4fecf1SPantelis Antoniou 
2037ce4fecf1SPantelis Antoniou 	for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
20386d0a70a2SRob Herring 		int precision;
2039ce4fecf1SPantelis Antoniou 		if (pass) {
2040ce4fecf1SPantelis Antoniou 			if (buf < end)
2041ce4fecf1SPantelis Antoniou 				*buf = ':';
2042ce4fecf1SPantelis Antoniou 			buf++;
2043ce4fecf1SPantelis Antoniou 		}
2044ce4fecf1SPantelis Antoniou 
2045ce4fecf1SPantelis Antoniou 		switch (*fmt) {
2046ce4fecf1SPantelis Antoniou 		case 'f':	/* full_name */
2047a92eb762SSakari Ailus 			buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
2048a92eb762SSakari Ailus 						      end);
2049ce4fecf1SPantelis Antoniou 			break;
2050ce4fecf1SPantelis Antoniou 		case 'n':	/* name */
2051a92eb762SSakari Ailus 			p = fwnode_get_name(of_fwnode_handle(dn));
20526d0a70a2SRob Herring 			precision = str_spec.precision;
20536d0a70a2SRob Herring 			str_spec.precision = strchrnul(p, '@') - p;
20546d0a70a2SRob Herring 			buf = string(buf, end, p, str_spec);
20556d0a70a2SRob Herring 			str_spec.precision = precision;
2056ce4fecf1SPantelis Antoniou 			break;
2057ce4fecf1SPantelis Antoniou 		case 'p':	/* phandle */
205809ceb8d7SAndy Shevchenko 			buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec);
2059ce4fecf1SPantelis Antoniou 			break;
2060ce4fecf1SPantelis Antoniou 		case 'P':	/* path-spec */
2061a92eb762SSakari Ailus 			p = fwnode_get_name(of_fwnode_handle(dn));
2062ce4fecf1SPantelis Antoniou 			if (!p[1])
2063ce4fecf1SPantelis Antoniou 				p = "/";
2064ce4fecf1SPantelis Antoniou 			buf = string(buf, end, p, str_spec);
2065ce4fecf1SPantelis Antoniou 			break;
2066ce4fecf1SPantelis Antoniou 		case 'F':	/* flags */
2067ce4fecf1SPantelis Antoniou 			tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
2068ce4fecf1SPantelis Antoniou 			tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
2069ce4fecf1SPantelis Antoniou 			tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
2070ce4fecf1SPantelis Antoniou 			tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
2071ce4fecf1SPantelis Antoniou 			tbuf[4] = 0;
2072d529ac41SPetr Mladek 			buf = string_nocheck(buf, end, tbuf, str_spec);
2073ce4fecf1SPantelis Antoniou 			break;
2074ce4fecf1SPantelis Antoniou 		case 'c':	/* major compatible string */
2075ce4fecf1SPantelis Antoniou 			ret = of_property_read_string(dn, "compatible", &p);
2076ce4fecf1SPantelis Antoniou 			if (!ret)
2077ce4fecf1SPantelis Antoniou 				buf = string(buf, end, p, str_spec);
2078ce4fecf1SPantelis Antoniou 			break;
2079ce4fecf1SPantelis Antoniou 		case 'C':	/* full compatible string */
2080ce4fecf1SPantelis Antoniou 			has_mult = false;
2081ce4fecf1SPantelis Antoniou 			of_property_for_each_string(dn, "compatible", prop, p) {
2082ce4fecf1SPantelis Antoniou 				if (has_mult)
2083d529ac41SPetr Mladek 					buf = string_nocheck(buf, end, ",", str_spec);
2084d529ac41SPetr Mladek 				buf = string_nocheck(buf, end, "\"", str_spec);
2085ce4fecf1SPantelis Antoniou 				buf = string(buf, end, p, str_spec);
2086d529ac41SPetr Mladek 				buf = string_nocheck(buf, end, "\"", str_spec);
2087ce4fecf1SPantelis Antoniou 
2088ce4fecf1SPantelis Antoniou 				has_mult = true;
2089ce4fecf1SPantelis Antoniou 			}
2090ce4fecf1SPantelis Antoniou 			break;
2091ce4fecf1SPantelis Antoniou 		default:
2092ce4fecf1SPantelis Antoniou 			break;
2093ce4fecf1SPantelis Antoniou 		}
2094ce4fecf1SPantelis Antoniou 	}
2095ce4fecf1SPantelis Antoniou 
2096ce4fecf1SPantelis Antoniou 	return widen_string(buf, buf - buf_start, end, spec);
2097ce4fecf1SPantelis Antoniou }
2098ce4fecf1SPantelis Antoniou 
20993bd32d6aSSakari Ailus static noinline_for_stack
21003bd32d6aSSakari Ailus char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2101798cc27aSPetr Mladek 		    struct printf_spec spec, const char *fmt)
2102798cc27aSPetr Mladek {
21033bd32d6aSSakari Ailus 	struct printf_spec str_spec = spec;
21043bd32d6aSSakari Ailus 	char *buf_start = buf;
21053bd32d6aSSakari Ailus 
21063bd32d6aSSakari Ailus 	str_spec.field_width = -1;
21073bd32d6aSSakari Ailus 
21083bd32d6aSSakari Ailus 	if (*fmt != 'w')
21093bd32d6aSSakari Ailus 		return error_string(buf, end, "(%pf?)", spec);
21103bd32d6aSSakari Ailus 
21113bd32d6aSSakari Ailus 	if (check_pointer(&buf, end, fwnode, spec))
21123bd32d6aSSakari Ailus 		return buf;
21133bd32d6aSSakari Ailus 
21143bd32d6aSSakari Ailus 	fmt++;
21153bd32d6aSSakari Ailus 
21163bd32d6aSSakari Ailus 	switch (*fmt) {
21173bd32d6aSSakari Ailus 	case 'P':	/* name */
21183bd32d6aSSakari Ailus 		buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
21193bd32d6aSSakari Ailus 		break;
21203bd32d6aSSakari Ailus 	case 'f':	/* full_name */
21213bd32d6aSSakari Ailus 	default:
21223bd32d6aSSakari Ailus 		buf = fwnode_full_name_string(fwnode, buf, end);
21233bd32d6aSSakari Ailus 		break;
2124798cc27aSPetr Mladek 	}
2125798cc27aSPetr Mladek 
21263bd32d6aSSakari Ailus 	return widen_string(buf, buf - buf_start, end, spec);
2127798cc27aSPetr Mladek }
2128798cc27aSPetr Mladek 
21294d8a743cSLinus Torvalds /*
21304d8a743cSLinus Torvalds  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
21314d8a743cSLinus Torvalds  * by an extra set of alphanumeric characters that are extended format
21324d8a743cSLinus Torvalds  * specifiers.
21334d8a743cSLinus Torvalds  *
21340b523769SJoe Perches  * Please update scripts/checkpatch.pl when adding/removing conversion
21350b523769SJoe Perches  * characters.  (Search for "check for vsprintf extension").
21360b523769SJoe Perches  *
2137332d2e78SLinus Torvalds  * Right now we handle:
21380fe1ef24SLinus Torvalds  *
2139cdb7e52dSSergey Senozhatsky  * - 'S' For symbolic direct pointers (or function descriptors) with offset
2140cdb7e52dSSergey Senozhatsky  * - 's' For symbolic direct pointers (or function descriptors) without offset
21419af77064SSakari Ailus  * - '[Ss]R' as above with __builtin_extract_return_addr() translation
21421586c5aeSSakari Ailus  * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
21431586c5aeSSakari Ailus  *	    %ps and %pS. Be careful when re-using these specifiers.
21440f77a8d3SNamhyung Kim  * - 'B' For backtraced symbolic direct pointers with offset
2145c7dabef8SBjorn Helgaas  * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
2146c7dabef8SBjorn Helgaas  * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
2147dbc760bcSTejun Heo  * - 'b[l]' For a bitmap, the number of bits is determined by the field
2148dbc760bcSTejun Heo  *       width which must be explicitly specified either as part of the
2149dbc760bcSTejun Heo  *       format string '%32b[l]' or through '%*b[l]', [l] selects
2150dbc760bcSTejun Heo  *       range-list format instead of hex format
2151dd45c9cfSHarvey Harrison  * - 'M' For a 6-byte MAC address, it prints the address in the
2152dd45c9cfSHarvey Harrison  *       usual colon-separated hex notation
21538a27f7c9SJoe Perches  * - 'm' For a 6-byte MAC address, it prints the hex address without colons
2154bc7259a2SJoe Perches  * - 'MF' For a 6-byte MAC FDDI address, it prints the address
2155c8e00060SJoe Perches  *       with a dash-separated hex notation
21567c59154eSAndy Shevchenko  * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
21578a27f7c9SJoe Perches  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
21588a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
21598a27f7c9SJoe Perches  *       IPv6 uses colon separated network-order 16 bit hex with leading 0's
216010679643SDaniel Borkmann  *       [S][pfs]
216110679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
216210679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
21638a27f7c9SJoe Perches  * - 'i' [46] for 'raw' IPv4/IPv6 addresses
21648a27f7c9SJoe Perches  *       IPv6 omits the colons (01020304...0f)
21658a27f7c9SJoe Perches  *       IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
216610679643SDaniel Borkmann  *       [S][pfs]
216710679643SDaniel Borkmann  *       Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
216810679643SDaniel Borkmann  *       [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
216910679643SDaniel Borkmann  * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
217010679643SDaniel Borkmann  * - 'I[6S]c' for IPv6 addresses printed as specified by
21718eda94bdSAlexander A. Klimov  *       https://tools.ietf.org/html/rfc5952
217271dca95dSAndy Shevchenko  * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
217371dca95dSAndy Shevchenko  *                of the following flags (see string_escape_mem() for the
217471dca95dSAndy Shevchenko  *                details):
217571dca95dSAndy Shevchenko  *                  a - ESCAPE_ANY
217671dca95dSAndy Shevchenko  *                  c - ESCAPE_SPECIAL
217771dca95dSAndy Shevchenko  *                  h - ESCAPE_HEX
217871dca95dSAndy Shevchenko  *                  n - ESCAPE_NULL
217971dca95dSAndy Shevchenko  *                  o - ESCAPE_OCTAL
218071dca95dSAndy Shevchenko  *                  p - ESCAPE_NP
218171dca95dSAndy Shevchenko  *                  s - ESCAPE_SPACE
218271dca95dSAndy Shevchenko  *                By default ESCAPE_ANY_NP is used.
21839ac6e44eSJoe Perches  * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
21849ac6e44eSJoe Perches  *       "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
21859ac6e44eSJoe Perches  *       Options for %pU are:
21869ac6e44eSJoe Perches  *         b big endian lower case hex (default)
21879ac6e44eSJoe Perches  *         B big endian UPPER case hex
21889ac6e44eSJoe Perches  *         l little endian lower case hex
21899ac6e44eSJoe Perches  *         L little endian UPPER case hex
21909ac6e44eSJoe Perches  *           big endian output byte order is:
21919ac6e44eSJoe Perches  *             [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
21929ac6e44eSJoe Perches  *           little endian output byte order is:
21939ac6e44eSJoe Perches  *             [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
21947db6f5fbSJoe Perches  * - 'V' For a struct va_format which contains a format string * and va_list *,
21957db6f5fbSJoe Perches  *       call vsnprintf(->format, *->va_list).
21967db6f5fbSJoe Perches  *       Implements a "recursive vsnprintf".
21977db6f5fbSJoe Perches  *       Do not use this feature without some mechanism to verify the
21987db6f5fbSJoe Perches  *       correctness of the format string and va_list arguments.
2199455cd5abSDan Rosenberg  * - 'K' For a kernel pointer that should be hidden from unprivileged users
2200c8f44affSMichał Mirosław  * - 'NF' For a netdev_features_t
2201*af612e43SSakari Ailus  * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value.
220231550a16SAndy Shevchenko  * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
220331550a16SAndy Shevchenko  *            a certain separator (' ' by default):
220431550a16SAndy Shevchenko  *              C colon
220531550a16SAndy Shevchenko  *              D dash
220631550a16SAndy Shevchenko  *              N no separator
220731550a16SAndy Shevchenko  *            The maximum supported length is 64 bytes of the input. Consider
220831550a16SAndy Shevchenko  *            to use print_hex_dump() for the larger input.
2209aaf07621SJoe Perches  * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
2210aaf07621SJoe Perches  *           (default assumed to be phys_addr_t, passed by reference)
2211c0d92a57SOlof Johansson  * - 'd[234]' For a dentry name (optionally 2-4 last components)
2212c0d92a57SOlof Johansson  * - 'D[234]' Same as 'd' but for a struct file
22131031bc58SDmitry Monakhov  * - 'g' For block_device name (gendisk + partition number)
22147daac5b2SAndy Shevchenko  * - 't[RT][dt][r]' For time and date as represented by:
22154d42c447SAndy Shevchenko  *      R    struct rtc_time
22167daac5b2SAndy Shevchenko  *      T    time64_t
2217900cca29SGeert Uytterhoeven  * - 'C' For a clock, it prints the name (Common Clock Framework) or address
2218900cca29SGeert Uytterhoeven  *       (legacy clock framework) of the clock
2219900cca29SGeert Uytterhoeven  * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2220900cca29SGeert Uytterhoeven  *        (legacy clock framework) of the clock
2221edf14cdbSVlastimil Babka  * - 'G' For flags to be printed as a collection of symbolic strings that would
2222edf14cdbSVlastimil Babka  *       construct the specific value. Supported flags given by option:
2223edf14cdbSVlastimil Babka  *       p page flags (see struct page) given as pointer to unsigned long
2224edf14cdbSVlastimil Babka  *       g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2225edf14cdbSVlastimil Babka  *       v vma flags (VM_*) given as pointer to unsigned long
2226ce4fecf1SPantelis Antoniou  * - 'OF[fnpPcCF]'  For a device tree object
2227ce4fecf1SPantelis Antoniou  *                  Without any optional arguments prints the full_name
2228ce4fecf1SPantelis Antoniou  *                  f device node full_name
2229ce4fecf1SPantelis Antoniou  *                  n device node name
2230ce4fecf1SPantelis Antoniou  *                  p device node phandle
2231ce4fecf1SPantelis Antoniou  *                  P device node path spec (name + @unit)
2232ce4fecf1SPantelis Antoniou  *                  F device node flags
2233ce4fecf1SPantelis Antoniou  *                  c major compatible string
2234ce4fecf1SPantelis Antoniou  *                  C full compatible string
22353bd32d6aSSakari Ailus  * - 'fw[fP]'	For a firmware node (struct fwnode_handle) pointer
22363bd32d6aSSakari Ailus  *		Without an option prints the full name of the node
22373bd32d6aSSakari Ailus  *		f full name
22383bd32d6aSSakari Ailus  *		P node name, including a possible unit address
22397b1924a1STobin C. Harding  * - 'x' For printing the address. Equivalent to "%lx".
2240b2a5212fSDaniel Borkmann  * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2241b2a5212fSDaniel Borkmann  *           bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2242b2a5212fSDaniel Borkmann  *           or user (u) memory to probe, and:
2243b2a5212fSDaniel Borkmann  *              s a string, equivalent to "%s" on direct vsnprintf() use
22447b1924a1STobin C. Harding  *
2245b3ed2321STobin C. Harding  * ** When making changes please also update:
2246b3ed2321STobin C. Harding  *	Documentation/core-api/printk-formats.rst
22479ac6e44eSJoe Perches  *
2248ad67b74dSTobin C. Harding  * Note: The default behaviour (unadorned %p) is to hash the address,
2249ad67b74dSTobin C. Harding  * rendering it useful as a unique identifier.
22504d8a743cSLinus Torvalds  */
2251cf3b429bSJoe Perches static noinline_for_stack
2252cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr,
2253fef20d9cSFrederic Weisbecker 	      struct printf_spec spec)
225478a8bf69SLinus Torvalds {
22550fe1ef24SLinus Torvalds 	switch (*fmt) {
22560fe1ef24SLinus Torvalds 	case 'S':
22579ac6e44eSJoe Perches 	case 's':
225804b8eb7aSSergey Senozhatsky 		ptr = dereference_symbol_descriptor(ptr);
22594c1ca831SNick Desaulniers 		fallthrough;
22600f77a8d3SNamhyung Kim 	case 'B':
2261b0d33c2bSJoe Perches 		return symbol_string(buf, end, ptr, spec, fmt);
2262332d2e78SLinus Torvalds 	case 'R':
2263c7dabef8SBjorn Helgaas 	case 'r':
2264fd95541eSBjorn Helgaas 		return resource_string(buf, end, ptr, spec, fmt);
226531550a16SAndy Shevchenko 	case 'h':
226631550a16SAndy Shevchenko 		return hex_string(buf, end, ptr, spec, fmt);
2267dbc760bcSTejun Heo 	case 'b':
2268dbc760bcSTejun Heo 		switch (fmt[1]) {
2269dbc760bcSTejun Heo 		case 'l':
2270dbc760bcSTejun Heo 			return bitmap_list_string(buf, end, ptr, spec, fmt);
2271dbc760bcSTejun Heo 		default:
2272dbc760bcSTejun Heo 			return bitmap_string(buf, end, ptr, spec, fmt);
2273dbc760bcSTejun Heo 		}
22748a27f7c9SJoe Perches 	case 'M':			/* Colon separated: 00:01:02:03:04:05 */
22758a27f7c9SJoe Perches 	case 'm':			/* Contiguous: 000102030405 */
227676597ff9SAndrei Emeltchenko 					/* [mM]F (FDDI) */
227776597ff9SAndrei Emeltchenko 					/* [mM]R (Reverse order; Bluetooth) */
22788a27f7c9SJoe Perches 		return mac_address_string(buf, end, ptr, spec, fmt);
22798a27f7c9SJoe Perches 	case 'I':			/* Formatted IP supported
22808a27f7c9SJoe Perches 					 * 4:	1.2.3.4
22818a27f7c9SJoe Perches 					 * 6:	0001:0203:...:0708
22828a27f7c9SJoe Perches 					 * 6c:	1::708 or 1::1.2.3.4
22838a27f7c9SJoe Perches 					 */
22848a27f7c9SJoe Perches 	case 'i':			/* Contiguous:
22858a27f7c9SJoe Perches 					 * 4:	001.002.003.004
22868a27f7c9SJoe Perches 					 * 6:   000102...0f
22878a27f7c9SJoe Perches 					 */
2288f00cc102SPetr Mladek 		return ip_addr_string(buf, end, ptr, spec, fmt);
228971dca95dSAndy Shevchenko 	case 'E':
229071dca95dSAndy Shevchenko 		return escaped_string(buf, end, ptr, spec, fmt);
22919ac6e44eSJoe Perches 	case 'U':
22929ac6e44eSJoe Perches 		return uuid_string(buf, end, ptr, spec, fmt);
22937db6f5fbSJoe Perches 	case 'V':
22943e5903ebSPetr Mladek 		return va_format(buf, end, ptr, spec, fmt);
2295455cd5abSDan Rosenberg 	case 'K':
229657e73442STobin C. Harding 		return restricted_pointer(buf, end, ptr, spec);
2297c8f44affSMichał Mirosław 	case 'N':
2298431bca24SGeert Uytterhoeven 		return netdev_bits(buf, end, ptr, spec, fmt);
2299*af612e43SSakari Ailus 	case '4':
2300*af612e43SSakari Ailus 		return fourcc_string(buf, end, ptr, spec, fmt);
23017d799210SStepan Moskovchenko 	case 'a':
23023e5903ebSPetr Mladek 		return address_val(buf, end, ptr, spec, fmt);
23034b6ccca7SAl Viro 	case 'd':
23044b6ccca7SAl Viro 		return dentry_name(buf, end, ptr, spec, fmt);
23054d42c447SAndy Shevchenko 	case 't':
23064d42c447SAndy Shevchenko 		return time_and_date(buf, end, ptr, spec, fmt);
2307900cca29SGeert Uytterhoeven 	case 'C':
2308900cca29SGeert Uytterhoeven 		return clock(buf, end, ptr, spec, fmt);
23094b6ccca7SAl Viro 	case 'D':
231036594b31SJia He 		return file_dentry_name(buf, end, ptr, spec, fmt);
23111031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
23121031bc58SDmitry Monakhov 	case 'g':
23131031bc58SDmitry Monakhov 		return bdev_name(buf, end, ptr, spec, fmt);
23141031bc58SDmitry Monakhov #endif
23151031bc58SDmitry Monakhov 
2316edf14cdbSVlastimil Babka 	case 'G':
23170b74d4d7SPetr Mladek 		return flags_string(buf, end, ptr, spec, fmt);
2318ce4fecf1SPantelis Antoniou 	case 'O':
231983abc5a7SSakari Ailus 		return device_node_string(buf, end, ptr, spec, fmt + 1);
23203bd32d6aSSakari Ailus 	case 'f':
23213bd32d6aSSakari Ailus 		return fwnode_string(buf, end, ptr, spec, fmt + 1);
23227b1924a1STobin C. Harding 	case 'x':
23237b1924a1STobin C. Harding 		return pointer_string(buf, end, ptr, spec);
232457f5677eSRasmus Villemoes 	case 'e':
232557f5677eSRasmus Villemoes 		/* %pe with a non-ERR_PTR gets treated as plain %p */
232657f5677eSRasmus Villemoes 		if (!IS_ERR(ptr))
232757f5677eSRasmus Villemoes 			break;
232857f5677eSRasmus Villemoes 		return err_ptr(buf, end, ptr, spec);
2329b2a5212fSDaniel Borkmann 	case 'u':
2330b2a5212fSDaniel Borkmann 	case 'k':
2331b2a5212fSDaniel Borkmann 		switch (fmt[1]) {
2332b2a5212fSDaniel Borkmann 		case 's':
2333b2a5212fSDaniel Borkmann 			return string(buf, end, ptr, spec);
2334b2a5212fSDaniel Borkmann 		default:
2335b2a5212fSDaniel Borkmann 			return error_string(buf, end, "(einval)", spec);
2336b2a5212fSDaniel Borkmann 		}
23370fe1ef24SLinus Torvalds 	}
2338fef20d9cSFrederic Weisbecker 
2339ad67b74dSTobin C. Harding 	/* default is to _not_ leak addresses, hash before printing */
2340ad67b74dSTobin C. Harding 	return ptr_to_id(buf, end, ptr, spec);
2341fef20d9cSFrederic Weisbecker }
2342fef20d9cSFrederic Weisbecker 
2343fef20d9cSFrederic Weisbecker /*
2344fef20d9cSFrederic Weisbecker  * Helper function to decode printf style format.
2345fef20d9cSFrederic Weisbecker  * Each call decode a token from the format and return the
2346fef20d9cSFrederic Weisbecker  * number of characters read (or likely the delta where it wants
2347fef20d9cSFrederic Weisbecker  * to go on the next call).
2348fef20d9cSFrederic Weisbecker  * The decoded token is returned through the parameters
2349fef20d9cSFrederic Weisbecker  *
2350fef20d9cSFrederic Weisbecker  * 'h', 'l', or 'L' for integer fields
2351fef20d9cSFrederic Weisbecker  * 'z' support added 23/7/1999 S.H.
2352fef20d9cSFrederic Weisbecker  * 'z' changed to 'Z' --davidm 1/25/99
23535b5e0928SAlexey Dobriyan  * 'Z' changed to 'z' --adobriyan 2017-01-25
2354fef20d9cSFrederic Weisbecker  * 't' added for ptrdiff_t
2355fef20d9cSFrederic Weisbecker  *
2356fef20d9cSFrederic Weisbecker  * @fmt: the format string
2357fef20d9cSFrederic Weisbecker  * @type of the token returned
2358fef20d9cSFrederic Weisbecker  * @flags: various flags such as +, -, # tokens..
2359fef20d9cSFrederic Weisbecker  * @field_width: overwritten width
2360fef20d9cSFrederic Weisbecker  * @base: base of the number (octal, hex, ...)
2361fef20d9cSFrederic Weisbecker  * @precision: precision of a number
2362fef20d9cSFrederic Weisbecker  * @qualifier: qualifier of a number (long, size_t, ...)
2363fef20d9cSFrederic Weisbecker  */
2364cf3b429bSJoe Perches static noinline_for_stack
2365cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec)
2366fef20d9cSFrederic Weisbecker {
2367fef20d9cSFrederic Weisbecker 	const char *start = fmt;
2368d0484193SRasmus Villemoes 	char qualifier;
2369fef20d9cSFrederic Weisbecker 
2370fef20d9cSFrederic Weisbecker 	/* we finished early by reading the field width */
2371ed681a91SVegard Nossum 	if (spec->type == FORMAT_TYPE_WIDTH) {
2372fef20d9cSFrederic Weisbecker 		if (spec->field_width < 0) {
2373fef20d9cSFrederic Weisbecker 			spec->field_width = -spec->field_width;
2374fef20d9cSFrederic Weisbecker 			spec->flags |= LEFT;
2375fef20d9cSFrederic Weisbecker 		}
2376fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
2377fef20d9cSFrederic Weisbecker 		goto precision;
2378fef20d9cSFrederic Weisbecker 	}
2379fef20d9cSFrederic Weisbecker 
2380fef20d9cSFrederic Weisbecker 	/* we finished early by reading the precision */
2381fef20d9cSFrederic Weisbecker 	if (spec->type == FORMAT_TYPE_PRECISION) {
2382fef20d9cSFrederic Weisbecker 		if (spec->precision < 0)
2383fef20d9cSFrederic Weisbecker 			spec->precision = 0;
2384fef20d9cSFrederic Weisbecker 
2385fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_NONE;
2386fef20d9cSFrederic Weisbecker 		goto qualifier;
2387fef20d9cSFrederic Weisbecker 	}
2388fef20d9cSFrederic Weisbecker 
2389fef20d9cSFrederic Weisbecker 	/* By default */
2390fef20d9cSFrederic Weisbecker 	spec->type = FORMAT_TYPE_NONE;
2391fef20d9cSFrederic Weisbecker 
2392fef20d9cSFrederic Weisbecker 	for (; *fmt ; ++fmt) {
2393fef20d9cSFrederic Weisbecker 		if (*fmt == '%')
2394fef20d9cSFrederic Weisbecker 			break;
2395fef20d9cSFrederic Weisbecker 	}
2396fef20d9cSFrederic Weisbecker 
2397fef20d9cSFrederic Weisbecker 	/* Return the current non-format string */
2398fef20d9cSFrederic Weisbecker 	if (fmt != start || !*fmt)
2399fef20d9cSFrederic Weisbecker 		return fmt - start;
2400fef20d9cSFrederic Weisbecker 
2401fef20d9cSFrederic Weisbecker 	/* Process flags */
2402fef20d9cSFrederic Weisbecker 	spec->flags = 0;
2403fef20d9cSFrederic Weisbecker 
2404fef20d9cSFrederic Weisbecker 	while (1) { /* this also skips first '%' */
2405fef20d9cSFrederic Weisbecker 		bool found = true;
2406fef20d9cSFrederic Weisbecker 
2407fef20d9cSFrederic Weisbecker 		++fmt;
2408fef20d9cSFrederic Weisbecker 
2409fef20d9cSFrederic Weisbecker 		switch (*fmt) {
2410fef20d9cSFrederic Weisbecker 		case '-': spec->flags |= LEFT;    break;
2411fef20d9cSFrederic Weisbecker 		case '+': spec->flags |= PLUS;    break;
2412fef20d9cSFrederic Weisbecker 		case ' ': spec->flags |= SPACE;   break;
2413fef20d9cSFrederic Weisbecker 		case '#': spec->flags |= SPECIAL; break;
2414fef20d9cSFrederic Weisbecker 		case '0': spec->flags |= ZEROPAD; break;
2415fef20d9cSFrederic Weisbecker 		default:  found = false;
2416fef20d9cSFrederic Weisbecker 		}
2417fef20d9cSFrederic Weisbecker 
2418fef20d9cSFrederic Weisbecker 		if (!found)
2419fef20d9cSFrederic Weisbecker 			break;
2420fef20d9cSFrederic Weisbecker 	}
2421fef20d9cSFrederic Weisbecker 
2422fef20d9cSFrederic Weisbecker 	/* get field width */
2423fef20d9cSFrederic Weisbecker 	spec->field_width = -1;
2424fef20d9cSFrederic Weisbecker 
2425fef20d9cSFrederic Weisbecker 	if (isdigit(*fmt))
2426fef20d9cSFrederic Weisbecker 		spec->field_width = skip_atoi(&fmt);
2427fef20d9cSFrederic Weisbecker 	else if (*fmt == '*') {
2428fef20d9cSFrederic Weisbecker 		/* it's the next argument */
2429ed681a91SVegard Nossum 		spec->type = FORMAT_TYPE_WIDTH;
2430fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2431fef20d9cSFrederic Weisbecker 	}
2432fef20d9cSFrederic Weisbecker 
2433fef20d9cSFrederic Weisbecker precision:
2434fef20d9cSFrederic Weisbecker 	/* get the precision */
2435fef20d9cSFrederic Weisbecker 	spec->precision = -1;
2436fef20d9cSFrederic Weisbecker 	if (*fmt == '.') {
2437fef20d9cSFrederic Weisbecker 		++fmt;
2438fef20d9cSFrederic Weisbecker 		if (isdigit(*fmt)) {
2439fef20d9cSFrederic Weisbecker 			spec->precision = skip_atoi(&fmt);
2440fef20d9cSFrederic Weisbecker 			if (spec->precision < 0)
2441fef20d9cSFrederic Weisbecker 				spec->precision = 0;
2442fef20d9cSFrederic Weisbecker 		} else if (*fmt == '*') {
2443fef20d9cSFrederic Weisbecker 			/* it's the next argument */
2444adf26f84SVegard Nossum 			spec->type = FORMAT_TYPE_PRECISION;
2445fef20d9cSFrederic Weisbecker 			return ++fmt - start;
2446fef20d9cSFrederic Weisbecker 		}
2447fef20d9cSFrederic Weisbecker 	}
2448fef20d9cSFrederic Weisbecker 
2449fef20d9cSFrederic Weisbecker qualifier:
2450fef20d9cSFrederic Weisbecker 	/* get the conversion qualifier */
2451d0484193SRasmus Villemoes 	qualifier = 0;
245275fb8f26SAndy Shevchenko 	if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
24535b5e0928SAlexey Dobriyan 	    *fmt == 'z' || *fmt == 't') {
2454d0484193SRasmus Villemoes 		qualifier = *fmt++;
2455d0484193SRasmus Villemoes 		if (unlikely(qualifier == *fmt)) {
2456d0484193SRasmus Villemoes 			if (qualifier == 'l') {
2457d0484193SRasmus Villemoes 				qualifier = 'L';
2458fef20d9cSFrederic Weisbecker 				++fmt;
2459d0484193SRasmus Villemoes 			} else if (qualifier == 'h') {
2460d0484193SRasmus Villemoes 				qualifier = 'H';
2461a4e94ef0SZhaolei 				++fmt;
2462a4e94ef0SZhaolei 			}
2463fef20d9cSFrederic Weisbecker 		}
2464fef20d9cSFrederic Weisbecker 	}
2465fef20d9cSFrederic Weisbecker 
2466fef20d9cSFrederic Weisbecker 	/* default base */
2467fef20d9cSFrederic Weisbecker 	spec->base = 10;
2468fef20d9cSFrederic Weisbecker 	switch (*fmt) {
2469fef20d9cSFrederic Weisbecker 	case 'c':
2470fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_CHAR;
2471fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2472fef20d9cSFrederic Weisbecker 
2473fef20d9cSFrederic Weisbecker 	case 's':
2474fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_STR;
2475fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2476fef20d9cSFrederic Weisbecker 
2477fef20d9cSFrederic Weisbecker 	case 'p':
2478fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTR;
2479ffbfed03SRasmus Villemoes 		return ++fmt - start;
2480fef20d9cSFrederic Weisbecker 
2481fef20d9cSFrederic Weisbecker 	case '%':
2482fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PERCENT_CHAR;
2483fef20d9cSFrederic Weisbecker 		return ++fmt - start;
2484fef20d9cSFrederic Weisbecker 
2485fef20d9cSFrederic Weisbecker 	/* integer number formats - set up the flags and "break" */
2486fef20d9cSFrederic Weisbecker 	case 'o':
2487fef20d9cSFrederic Weisbecker 		spec->base = 8;
2488fef20d9cSFrederic Weisbecker 		break;
2489fef20d9cSFrederic Weisbecker 
2490fef20d9cSFrederic Weisbecker 	case 'x':
2491fef20d9cSFrederic Weisbecker 		spec->flags |= SMALL;
24924c1ca831SNick Desaulniers 		fallthrough;
2493fef20d9cSFrederic Weisbecker 
2494fef20d9cSFrederic Weisbecker 	case 'X':
2495fef20d9cSFrederic Weisbecker 		spec->base = 16;
2496fef20d9cSFrederic Weisbecker 		break;
2497fef20d9cSFrederic Weisbecker 
2498fef20d9cSFrederic Weisbecker 	case 'd':
2499fef20d9cSFrederic Weisbecker 	case 'i':
250039e874f8SFrederic Weisbecker 		spec->flags |= SIGN;
250136f9ff9eSGustavo A. R. Silva 		break;
2502fef20d9cSFrederic Weisbecker 	case 'u':
2503fef20d9cSFrederic Weisbecker 		break;
2504fef20d9cSFrederic Weisbecker 
2505708d96fdSRyan Mallon 	case 'n':
2506708d96fdSRyan Mallon 		/*
2507b006f19bSRasmus Villemoes 		 * Since %n poses a greater security risk than
2508b006f19bSRasmus Villemoes 		 * utility, treat it as any other invalid or
2509b006f19bSRasmus Villemoes 		 * unsupported format specifier.
2510708d96fdSRyan Mallon 		 */
25114c1ca831SNick Desaulniers 		fallthrough;
2512708d96fdSRyan Mallon 
2513fef20d9cSFrederic Weisbecker 	default:
2514b006f19bSRasmus Villemoes 		WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
2515fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_INVALID;
2516fef20d9cSFrederic Weisbecker 		return fmt - start;
2517fef20d9cSFrederic Weisbecker 	}
2518fef20d9cSFrederic Weisbecker 
2519d0484193SRasmus Villemoes 	if (qualifier == 'L')
2520fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_LONG_LONG;
2521d0484193SRasmus Villemoes 	else if (qualifier == 'l') {
252251be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
252351be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
25245b5e0928SAlexey Dobriyan 	} else if (qualifier == 'z') {
2525fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_SIZE_T;
2526d0484193SRasmus Villemoes 	} else if (qualifier == 't') {
2527fef20d9cSFrederic Weisbecker 		spec->type = FORMAT_TYPE_PTRDIFF;
2528d0484193SRasmus Villemoes 	} else if (qualifier == 'H') {
252951be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
253051be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
2531d0484193SRasmus Villemoes 	} else if (qualifier == 'h') {
253251be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
253351be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
2534fef20d9cSFrederic Weisbecker 	} else {
253551be17dfSRasmus Villemoes 		BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
253651be17dfSRasmus Villemoes 		spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
2537fef20d9cSFrederic Weisbecker 	}
2538fef20d9cSFrederic Weisbecker 
2539fef20d9cSFrederic Weisbecker 	return ++fmt - start;
254078a8bf69SLinus Torvalds }
254178a8bf69SLinus Torvalds 
25424d72ba01SRasmus Villemoes static void
25434d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width)
25444d72ba01SRasmus Villemoes {
25454d72ba01SRasmus Villemoes 	spec->field_width = width;
25464d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
25474d72ba01SRasmus Villemoes 		spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
25484d72ba01SRasmus Villemoes 	}
25494d72ba01SRasmus Villemoes }
25504d72ba01SRasmus Villemoes 
25514d72ba01SRasmus Villemoes static void
25524d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec)
25534d72ba01SRasmus Villemoes {
25544d72ba01SRasmus Villemoes 	spec->precision = prec;
25554d72ba01SRasmus Villemoes 	if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
25564d72ba01SRasmus Villemoes 		spec->precision = clamp(prec, 0, PRECISION_MAX);
25574d72ba01SRasmus Villemoes 	}
25584d72ba01SRasmus Villemoes }
25594d72ba01SRasmus Villemoes 
25601da177e4SLinus Torvalds /**
25611da177e4SLinus Torvalds  * vsnprintf - Format a string and place it in a buffer
25621da177e4SLinus Torvalds  * @buf: The buffer to place the result into
25631da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
25641da177e4SLinus Torvalds  * @fmt: The format string to use
25651da177e4SLinus Torvalds  * @args: Arguments for the format string
25661da177e4SLinus Torvalds  *
2567d7ec9a05SRasmus Villemoes  * This function generally follows C99 vsnprintf, but has some
2568d7ec9a05SRasmus Villemoes  * extensions and a few limitations:
2569d7ec9a05SRasmus Villemoes  *
25706cc89134Smchehab@s-opensource.com  *  - ``%n`` is unsupported
25716cc89134Smchehab@s-opensource.com  *  - ``%p*`` is handled by pointer()
257220036fdcSAndi Kleen  *
257327e7c0e8SJonathan Corbet  * See pointer() or Documentation/core-api/printk-formats.rst for more
25745e4ee7b1SMartin Kletzander  * extensive description.
25755e4ee7b1SMartin Kletzander  *
25765e4ee7b1SMartin Kletzander  * **Please update the documentation in both places when making changes**
257780f548e0SAndrew Morton  *
25781da177e4SLinus Torvalds  * The return value is the number of characters which would
25791da177e4SLinus Torvalds  * be generated for the given input, excluding the trailing
25801da177e4SLinus Torvalds  * '\0', as per ISO C99. If you want to have the exact
25811da177e4SLinus Torvalds  * number of characters written into @buf as return value
258272fd4a35SRobert P. J. Day  * (not including the trailing '\0'), use vscnprintf(). If the
25831da177e4SLinus Torvalds  * return is greater than or equal to @size, the resulting
25841da177e4SLinus Torvalds  * string is truncated.
25851da177e4SLinus Torvalds  *
2586ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using snprintf().
25871da177e4SLinus Torvalds  */
25881da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
25891da177e4SLinus Torvalds {
25901da177e4SLinus Torvalds 	unsigned long long num;
2591d4be151bSAndré Goddard Rosa 	char *str, *end;
2592fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
25931da177e4SLinus Torvalds 
2594f796937aSJeremy Fitzhardinge 	/* Reject out-of-range values early.  Large positive sizes are
2595f796937aSJeremy Fitzhardinge 	   used for unknown buffer sizes. */
25962aa2f9e2SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
25971da177e4SLinus Torvalds 		return 0;
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 	str = buf;
2600f796937aSJeremy Fitzhardinge 	end = buf + size;
26011da177e4SLinus Torvalds 
2602f796937aSJeremy Fitzhardinge 	/* Make sure end is always >= buf */
2603f796937aSJeremy Fitzhardinge 	if (end < buf) {
26041da177e4SLinus Torvalds 		end = ((void *)-1);
2605f796937aSJeremy Fitzhardinge 		size = end - buf;
26061da177e4SLinus Torvalds 	}
26071da177e4SLinus Torvalds 
2608fef20d9cSFrederic Weisbecker 	while (*fmt) {
2609fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
2610d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
2611fef20d9cSFrederic Weisbecker 
2612fef20d9cSFrederic Weisbecker 		fmt += read;
2613fef20d9cSFrederic Weisbecker 
2614fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2615fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
2616fef20d9cSFrederic Weisbecker 			int copy = read;
2617fef20d9cSFrederic Weisbecker 			if (str < end) {
2618fef20d9cSFrederic Weisbecker 				if (copy > end - str)
2619fef20d9cSFrederic Weisbecker 					copy = end - str;
2620fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
2621fef20d9cSFrederic Weisbecker 			}
2622fef20d9cSFrederic Weisbecker 			str += read;
2623fef20d9cSFrederic Weisbecker 			break;
26241da177e4SLinus Torvalds 		}
26251da177e4SLinus Torvalds 
2626ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
26274d72ba01SRasmus Villemoes 			set_field_width(&spec, va_arg(args, int));
2628fef20d9cSFrederic Weisbecker 			break;
26291da177e4SLinus Torvalds 
2630fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
26314d72ba01SRasmus Villemoes 			set_precision(&spec, va_arg(args, int));
2632fef20d9cSFrederic Weisbecker 			break;
26331da177e4SLinus Torvalds 
2634d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
2635d4be151bSAndré Goddard Rosa 			char c;
2636d4be151bSAndré Goddard Rosa 
2637fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
2638fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
2639f796937aSJeremy Fitzhardinge 					if (str < end)
26401da177e4SLinus Torvalds 						*str = ' ';
26411da177e4SLinus Torvalds 					++str;
2642fef20d9cSFrederic Weisbecker 
26431da177e4SLinus Torvalds 				}
26441da177e4SLinus Torvalds 			}
26451da177e4SLinus Torvalds 			c = (unsigned char) va_arg(args, int);
2646f796937aSJeremy Fitzhardinge 			if (str < end)
26471da177e4SLinus Torvalds 				*str = c;
26481da177e4SLinus Torvalds 			++str;
2649fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
2650f796937aSJeremy Fitzhardinge 				if (str < end)
26511da177e4SLinus Torvalds 					*str = ' ';
26521da177e4SLinus Torvalds 				++str;
26531da177e4SLinus Torvalds 			}
2654fef20d9cSFrederic Weisbecker 			break;
2655d4be151bSAndré Goddard Rosa 		}
26561da177e4SLinus Torvalds 
2657fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR:
2658fef20d9cSFrederic Weisbecker 			str = string(str, end, va_arg(args, char *), spec);
2659fef20d9cSFrederic Weisbecker 			break;
26601da177e4SLinus Torvalds 
2661fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2662ffbfed03SRasmus Villemoes 			str = pointer(fmt, str, end, va_arg(args, void *),
2663fef20d9cSFrederic Weisbecker 				      spec);
2664fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
26654d8a743cSLinus Torvalds 				fmt++;
2666fef20d9cSFrederic Weisbecker 			break;
26671da177e4SLinus Torvalds 
2668fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
2669f796937aSJeremy Fitzhardinge 			if (str < end)
26701da177e4SLinus Torvalds 				*str = '%';
26711da177e4SLinus Torvalds 			++str;
26721da177e4SLinus Torvalds 			break;
26731da177e4SLinus Torvalds 
2674fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_INVALID:
2675b006f19bSRasmus Villemoes 			/*
2676b006f19bSRasmus Villemoes 			 * Presumably the arguments passed gcc's type
2677b006f19bSRasmus Villemoes 			 * checking, but there is no safe or sane way
2678b006f19bSRasmus Villemoes 			 * for us to continue parsing the format and
2679b006f19bSRasmus Villemoes 			 * fetching from the va_list; the remaining
2680b006f19bSRasmus Villemoes 			 * specifiers and arguments would be out of
2681b006f19bSRasmus Villemoes 			 * sync.
2682b006f19bSRasmus Villemoes 			 */
2683b006f19bSRasmus Villemoes 			goto out;
2684fef20d9cSFrederic Weisbecker 
2685fef20d9cSFrederic Weisbecker 		default:
2686fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2687fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2688fef20d9cSFrederic Weisbecker 				num = va_arg(args, long long);
2689fef20d9cSFrederic Weisbecker 				break;
2690fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2691fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned long);
2692fef20d9cSFrederic Weisbecker 				break;
2693fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
2694fef20d9cSFrederic Weisbecker 				num = va_arg(args, long);
2695fef20d9cSFrederic Weisbecker 				break;
2696fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
2697ef124960SJason Gunthorpe 				if (spec.flags & SIGN)
2698ef124960SJason Gunthorpe 					num = va_arg(args, ssize_t);
2699ef124960SJason Gunthorpe 				else
2700fef20d9cSFrederic Weisbecker 					num = va_arg(args, size_t);
2701fef20d9cSFrederic Weisbecker 				break;
2702fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
2703fef20d9cSFrederic Weisbecker 				num = va_arg(args, ptrdiff_t);
2704fef20d9cSFrederic Weisbecker 				break;
2705a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
2706a4e94ef0SZhaolei 				num = (unsigned char) va_arg(args, int);
2707a4e94ef0SZhaolei 				break;
2708a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
2709a4e94ef0SZhaolei 				num = (signed char) va_arg(args, int);
2710a4e94ef0SZhaolei 				break;
2711fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
2712fef20d9cSFrederic Weisbecker 				num = (unsigned short) va_arg(args, int);
2713fef20d9cSFrederic Weisbecker 				break;
2714fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
2715fef20d9cSFrederic Weisbecker 				num = (short) va_arg(args, int);
2716fef20d9cSFrederic Weisbecker 				break;
271739e874f8SFrederic Weisbecker 			case FORMAT_TYPE_INT:
271839e874f8SFrederic Weisbecker 				num = (int) va_arg(args, int);
2719fef20d9cSFrederic Weisbecker 				break;
2720fef20d9cSFrederic Weisbecker 			default:
2721fef20d9cSFrederic Weisbecker 				num = va_arg(args, unsigned int);
27221da177e4SLinus Torvalds 			}
2723fef20d9cSFrederic Weisbecker 
2724fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
27251da177e4SLinus Torvalds 		}
2726fef20d9cSFrederic Weisbecker 	}
2727fef20d9cSFrederic Weisbecker 
2728b006f19bSRasmus Villemoes out:
2729f796937aSJeremy Fitzhardinge 	if (size > 0) {
2730f796937aSJeremy Fitzhardinge 		if (str < end)
27311da177e4SLinus Torvalds 			*str = '\0';
2732f796937aSJeremy Fitzhardinge 		else
27330a6047eeSLinus Torvalds 			end[-1] = '\0';
2734f796937aSJeremy Fitzhardinge 	}
2735fef20d9cSFrederic Weisbecker 
2736f796937aSJeremy Fitzhardinge 	/* the trailing null byte doesn't count towards the total */
27371da177e4SLinus Torvalds 	return str-buf;
2738fef20d9cSFrederic Weisbecker 
27391da177e4SLinus Torvalds }
27401da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds /**
27431da177e4SLinus Torvalds  * vscnprintf - Format a string and place it in a buffer
27441da177e4SLinus Torvalds  * @buf: The buffer to place the result into
27451da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
27461da177e4SLinus Torvalds  * @fmt: The format string to use
27471da177e4SLinus Torvalds  * @args: Arguments for the format string
27481da177e4SLinus Torvalds  *
27491da177e4SLinus Torvalds  * The return value is the number of characters which have been written into
2750b921c69fSAnton Arapov  * the @buf not including the trailing '\0'. If @size is == 0 the function
27511da177e4SLinus Torvalds  * returns 0.
27521da177e4SLinus Torvalds  *
2753ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using scnprintf().
275420036fdcSAndi Kleen  *
275520036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
27561da177e4SLinus Torvalds  */
27571da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
27581da177e4SLinus Torvalds {
27591da177e4SLinus Torvalds 	int i;
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
27627b9186f5SAndré Goddard Rosa 
2763b921c69fSAnton Arapov 	if (likely(i < size))
2764b921c69fSAnton Arapov 		return i;
2765b921c69fSAnton Arapov 	if (size != 0)
2766b921c69fSAnton Arapov 		return size - 1;
2767b921c69fSAnton Arapov 	return 0;
27681da177e4SLinus Torvalds }
27691da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf);
27701da177e4SLinus Torvalds 
27711da177e4SLinus Torvalds /**
27721da177e4SLinus Torvalds  * snprintf - Format a string and place it in a buffer
27731da177e4SLinus Torvalds  * @buf: The buffer to place the result into
27741da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
27751da177e4SLinus Torvalds  * @fmt: The format string to use
27761da177e4SLinus Torvalds  * @...: Arguments for the format string
27771da177e4SLinus Torvalds  *
27781da177e4SLinus Torvalds  * The return value is the number of characters which would be
27791da177e4SLinus Torvalds  * generated for the given input, excluding the trailing null,
27801da177e4SLinus Torvalds  * as per ISO C99.  If the return is greater than or equal to
27811da177e4SLinus Torvalds  * @size, the resulting string is truncated.
278220036fdcSAndi Kleen  *
278320036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
27841da177e4SLinus Torvalds  */
27851da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...)
27861da177e4SLinus Torvalds {
27871da177e4SLinus Torvalds 	va_list args;
27881da177e4SLinus Torvalds 	int i;
27891da177e4SLinus Torvalds 
27901da177e4SLinus Torvalds 	va_start(args, fmt);
27911da177e4SLinus Torvalds 	i = vsnprintf(buf, size, fmt, args);
27921da177e4SLinus Torvalds 	va_end(args);
27937b9186f5SAndré Goddard Rosa 
27941da177e4SLinus Torvalds 	return i;
27951da177e4SLinus Torvalds }
27961da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf);
27971da177e4SLinus Torvalds 
27981da177e4SLinus Torvalds /**
27991da177e4SLinus Torvalds  * scnprintf - Format a string and place it in a buffer
28001da177e4SLinus Torvalds  * @buf: The buffer to place the result into
28011da177e4SLinus Torvalds  * @size: The size of the buffer, including the trailing null space
28021da177e4SLinus Torvalds  * @fmt: The format string to use
28031da177e4SLinus Torvalds  * @...: Arguments for the format string
28041da177e4SLinus Torvalds  *
28051da177e4SLinus Torvalds  * The return value is the number of characters written into @buf not including
2806b903c0b8SChangli Gao  * the trailing '\0'. If @size is == 0 the function returns 0.
28071da177e4SLinus Torvalds  */
28081da177e4SLinus Torvalds 
28091da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...)
28101da177e4SLinus Torvalds {
28111da177e4SLinus Torvalds 	va_list args;
28121da177e4SLinus Torvalds 	int i;
28131da177e4SLinus Torvalds 
28141da177e4SLinus Torvalds 	va_start(args, fmt);
2815b921c69fSAnton Arapov 	i = vscnprintf(buf, size, fmt, args);
28161da177e4SLinus Torvalds 	va_end(args);
28177b9186f5SAndré Goddard Rosa 
2818b903c0b8SChangli Gao 	return i;
28191da177e4SLinus Torvalds }
28201da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf);
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds /**
28231da177e4SLinus Torvalds  * vsprintf - Format a string and place it in a buffer
28241da177e4SLinus Torvalds  * @buf: The buffer to place the result into
28251da177e4SLinus Torvalds  * @fmt: The format string to use
28261da177e4SLinus Torvalds  * @args: Arguments for the format string
28271da177e4SLinus Torvalds  *
28281da177e4SLinus Torvalds  * The function returns the number of characters written
282972fd4a35SRobert P. J. Day  * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
28301da177e4SLinus Torvalds  * buffer overflows.
28311da177e4SLinus Torvalds  *
2832ba1835ebSUwe Kleine-König  * If you're not already dealing with a va_list consider using sprintf().
283320036fdcSAndi Kleen  *
283420036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
28351da177e4SLinus Torvalds  */
28361da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args)
28371da177e4SLinus Torvalds {
28381da177e4SLinus Torvalds 	return vsnprintf(buf, INT_MAX, fmt, args);
28391da177e4SLinus Torvalds }
28401da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
28411da177e4SLinus Torvalds 
28421da177e4SLinus Torvalds /**
28431da177e4SLinus Torvalds  * sprintf - Format a string and place it in a buffer
28441da177e4SLinus Torvalds  * @buf: The buffer to place the result into
28451da177e4SLinus Torvalds  * @fmt: The format string to use
28461da177e4SLinus Torvalds  * @...: Arguments for the format string
28471da177e4SLinus Torvalds  *
28481da177e4SLinus Torvalds  * The function returns the number of characters written
284972fd4a35SRobert P. J. Day  * into @buf. Use snprintf() or scnprintf() in order to avoid
28501da177e4SLinus Torvalds  * buffer overflows.
285120036fdcSAndi Kleen  *
285220036fdcSAndi Kleen  * See the vsnprintf() documentation for format string extensions over C99.
28531da177e4SLinus Torvalds  */
28541da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...)
28551da177e4SLinus Torvalds {
28561da177e4SLinus Torvalds 	va_list args;
28571da177e4SLinus Torvalds 	int i;
28581da177e4SLinus Torvalds 
28591da177e4SLinus Torvalds 	va_start(args, fmt);
28601da177e4SLinus Torvalds 	i = vsnprintf(buf, INT_MAX, fmt, args);
28611da177e4SLinus Torvalds 	va_end(args);
28627b9186f5SAndré Goddard Rosa 
28631da177e4SLinus Torvalds 	return i;
28641da177e4SLinus Torvalds }
28651da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf);
28661da177e4SLinus Torvalds 
28674370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF
28684370aa4aSLai Jiangshan /*
28694370aa4aSLai Jiangshan  * bprintf service:
28704370aa4aSLai Jiangshan  * vbin_printf() - VA arguments to binary data
28714370aa4aSLai Jiangshan  * bstr_printf() - Binary data to text string
28724370aa4aSLai Jiangshan  */
28734370aa4aSLai Jiangshan 
28744370aa4aSLai Jiangshan /**
28754370aa4aSLai Jiangshan  * vbin_printf - Parse a format string and place args' binary value in a buffer
28764370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
28774370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
28784370aa4aSLai Jiangshan  * @fmt: The format string to use
28794370aa4aSLai Jiangshan  * @args: Arguments for the format string
28804370aa4aSLai Jiangshan  *
28814370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, except %n is ignored, and its argument
2882da3dae54SMasanari Iida  * is skipped.
28834370aa4aSLai Jiangshan  *
28844370aa4aSLai Jiangshan  * The return value is the number of words(32bits) which would be generated for
28854370aa4aSLai Jiangshan  * the given input.
28864370aa4aSLai Jiangshan  *
28874370aa4aSLai Jiangshan  * NOTE:
28884370aa4aSLai Jiangshan  * If the return value is greater than @size, the resulting bin_buf is NOT
28894370aa4aSLai Jiangshan  * valid for bstr_printf().
28904370aa4aSLai Jiangshan  */
28914370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
28924370aa4aSLai Jiangshan {
2893fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
28944370aa4aSLai Jiangshan 	char *str, *end;
2895841a915dSSteven Rostedt (VMware) 	int width;
28964370aa4aSLai Jiangshan 
28974370aa4aSLai Jiangshan 	str = (char *)bin_buf;
28984370aa4aSLai Jiangshan 	end = (char *)(bin_buf + size);
28994370aa4aSLai Jiangshan 
29004370aa4aSLai Jiangshan #define save_arg(type)							\
2901841a915dSSteven Rostedt (VMware) ({									\
29024370aa4aSLai Jiangshan 	unsigned long long value;					\
2903841a915dSSteven Rostedt (VMware) 	if (sizeof(type) == 8) {					\
2904841a915dSSteven Rostedt (VMware) 		unsigned long long val8;				\
29054370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(u32));			\
2906841a915dSSteven Rostedt (VMware) 		val8 = va_arg(args, unsigned long long);		\
29074370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end) {			\
2908841a915dSSteven Rostedt (VMware) 			*(u32 *)str = *(u32 *)&val8;			\
2909841a915dSSteven Rostedt (VMware) 			*(u32 *)(str + 4) = *((u32 *)&val8 + 1);	\
29104370aa4aSLai Jiangshan 		}							\
2911841a915dSSteven Rostedt (VMware) 		value = val8;						\
29124370aa4aSLai Jiangshan 	} else {							\
2913841a915dSSteven Rostedt (VMware) 		unsigned int val4;					\
29144370aa4aSLai Jiangshan 		str = PTR_ALIGN(str, sizeof(type));			\
2915841a915dSSteven Rostedt (VMware) 		val4 = va_arg(args, int);				\
29164370aa4aSLai Jiangshan 		if (str + sizeof(type) <= end)				\
2917841a915dSSteven Rostedt (VMware) 			*(typeof(type) *)str = (type)(long)val4;	\
2918841a915dSSteven Rostedt (VMware) 		value = (unsigned long long)val4;			\
29194370aa4aSLai Jiangshan 	}								\
29204370aa4aSLai Jiangshan 	str += sizeof(type);						\
2921841a915dSSteven Rostedt (VMware) 	value;								\
2922841a915dSSteven Rostedt (VMware) })
29234370aa4aSLai Jiangshan 
2924fef20d9cSFrederic Weisbecker 	while (*fmt) {
2925d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
29264370aa4aSLai Jiangshan 
2927fef20d9cSFrederic Weisbecker 		fmt += read;
2928fef20d9cSFrederic Weisbecker 
2929fef20d9cSFrederic Weisbecker 		switch (spec.type) {
2930fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE:
2931d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_PERCENT_CHAR:
2932fef20d9cSFrederic Weisbecker 			break;
2933b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
2934b006f19bSRasmus Villemoes 			goto out;
2935fef20d9cSFrederic Weisbecker 
2936ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
2937fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
2938841a915dSSteven Rostedt (VMware) 			width = (int)save_arg(int);
2939841a915dSSteven Rostedt (VMware) 			/* Pointers may require the width */
2940841a915dSSteven Rostedt (VMware) 			if (*fmt == 'p')
2941841a915dSSteven Rostedt (VMware) 				set_field_width(&spec, width);
2942fef20d9cSFrederic Weisbecker 			break;
29434370aa4aSLai Jiangshan 
2944fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_CHAR:
29454370aa4aSLai Jiangshan 			save_arg(char);
2946fef20d9cSFrederic Weisbecker 			break;
2947fef20d9cSFrederic Weisbecker 
2948fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
29494370aa4aSLai Jiangshan 			const char *save_str = va_arg(args, char *);
29503e5903ebSPetr Mladek 			const char *err_msg;
29514370aa4aSLai Jiangshan 			size_t len;
29526c356634SAndré Goddard Rosa 
29533e5903ebSPetr Mladek 			err_msg = check_pointer_msg(save_str);
29543e5903ebSPetr Mladek 			if (err_msg)
29553e5903ebSPetr Mladek 				save_str = err_msg;
29563e5903ebSPetr Mladek 
29576c356634SAndré Goddard Rosa 			len = strlen(save_str) + 1;
29586c356634SAndré Goddard Rosa 			if (str + len < end)
29596c356634SAndré Goddard Rosa 				memcpy(str, save_str, len);
29606c356634SAndré Goddard Rosa 			str += len;
2961fef20d9cSFrederic Weisbecker 			break;
29624370aa4aSLai Jiangshan 		}
2963fef20d9cSFrederic Weisbecker 
2964fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PTR:
2965841a915dSSteven Rostedt (VMware) 			/* Dereferenced pointers must be done now */
2966841a915dSSteven Rostedt (VMware) 			switch (*fmt) {
2967841a915dSSteven Rostedt (VMware) 			/* Dereference of functions is still OK */
2968841a915dSSteven Rostedt (VMware) 			case 'S':
2969841a915dSSteven Rostedt (VMware) 			case 's':
29701e6338cfSSteven Rostedt (VMware) 			case 'x':
29711e6338cfSSteven Rostedt (VMware) 			case 'K':
297257f5677eSRasmus Villemoes 			case 'e':
29734370aa4aSLai Jiangshan 				save_arg(void *);
2974841a915dSSteven Rostedt (VMware) 				break;
2975841a915dSSteven Rostedt (VMware) 			default:
2976841a915dSSteven Rostedt (VMware) 				if (!isalnum(*fmt)) {
2977841a915dSSteven Rostedt (VMware) 					save_arg(void *);
2978841a915dSSteven Rostedt (VMware) 					break;
2979841a915dSSteven Rostedt (VMware) 				}
2980841a915dSSteven Rostedt (VMware) 				str = pointer(fmt, str, end, va_arg(args, void *),
2981841a915dSSteven Rostedt (VMware) 					      spec);
2982841a915dSSteven Rostedt (VMware) 				if (str + 1 < end)
2983841a915dSSteven Rostedt (VMware) 					*str++ = '\0';
2984841a915dSSteven Rostedt (VMware) 				else
2985841a915dSSteven Rostedt (VMware) 					end[-1] = '\0'; /* Must be nul terminated */
2986841a915dSSteven Rostedt (VMware) 			}
29874370aa4aSLai Jiangshan 			/* skip all alphanumeric pointer suffixes */
2988fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
29894370aa4aSLai Jiangshan 				fmt++;
2990fef20d9cSFrederic Weisbecker 			break;
2991fef20d9cSFrederic Weisbecker 
2992fef20d9cSFrederic Weisbecker 		default:
2993fef20d9cSFrederic Weisbecker 			switch (spec.type) {
2994fef20d9cSFrederic Weisbecker 
2995fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
2996fef20d9cSFrederic Weisbecker 				save_arg(long long);
2997fef20d9cSFrederic Weisbecker 				break;
2998fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
2999fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
3000fef20d9cSFrederic Weisbecker 				save_arg(unsigned long);
3001fef20d9cSFrederic Weisbecker 				break;
3002fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
3003fef20d9cSFrederic Weisbecker 				save_arg(size_t);
3004fef20d9cSFrederic Weisbecker 				break;
3005fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
3006fef20d9cSFrederic Weisbecker 				save_arg(ptrdiff_t);
3007fef20d9cSFrederic Weisbecker 				break;
3008a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
3009a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
3010a4e94ef0SZhaolei 				save_arg(char);
3011a4e94ef0SZhaolei 				break;
3012fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
3013fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
3014fef20d9cSFrederic Weisbecker 				save_arg(short);
3015fef20d9cSFrederic Weisbecker 				break;
3016fef20d9cSFrederic Weisbecker 			default:
3017fef20d9cSFrederic Weisbecker 				save_arg(int);
3018fef20d9cSFrederic Weisbecker 			}
3019fef20d9cSFrederic Weisbecker 		}
3020fef20d9cSFrederic Weisbecker 	}
3021fef20d9cSFrederic Weisbecker 
3022b006f19bSRasmus Villemoes out:
30237b9186f5SAndré Goddard Rosa 	return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
3024fef20d9cSFrederic Weisbecker #undef save_arg
30254370aa4aSLai Jiangshan }
30264370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf);
30274370aa4aSLai Jiangshan 
30284370aa4aSLai Jiangshan /**
30294370aa4aSLai Jiangshan  * bstr_printf - Format a string from binary arguments and place it in a buffer
30304370aa4aSLai Jiangshan  * @buf: The buffer to place the result into
30314370aa4aSLai Jiangshan  * @size: The size of the buffer, including the trailing null space
30324370aa4aSLai Jiangshan  * @fmt: The format string to use
30334370aa4aSLai Jiangshan  * @bin_buf: Binary arguments for the format string
30344370aa4aSLai Jiangshan  *
30354370aa4aSLai Jiangshan  * This function like C99 vsnprintf, but the difference is that vsnprintf gets
30364370aa4aSLai Jiangshan  * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
30374370aa4aSLai Jiangshan  * a binary buffer that generated by vbin_printf.
30384370aa4aSLai Jiangshan  *
30394370aa4aSLai Jiangshan  * The format follows C99 vsnprintf, but has some extensions:
30400efb4d20SSteven Rostedt  *  see vsnprintf comment for details.
30414370aa4aSLai Jiangshan  *
30424370aa4aSLai Jiangshan  * The return value is the number of characters which would
30434370aa4aSLai Jiangshan  * be generated for the given input, excluding the trailing
30444370aa4aSLai Jiangshan  * '\0', as per ISO C99. If you want to have the exact
30454370aa4aSLai Jiangshan  * number of characters written into @buf as return value
30464370aa4aSLai Jiangshan  * (not including the trailing '\0'), use vscnprintf(). If the
30474370aa4aSLai Jiangshan  * return is greater than or equal to @size, the resulting
30484370aa4aSLai Jiangshan  * string is truncated.
30494370aa4aSLai Jiangshan  */
30504370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
30514370aa4aSLai Jiangshan {
3052fef20d9cSFrederic Weisbecker 	struct printf_spec spec = {0};
3053d4be151bSAndré Goddard Rosa 	char *str, *end;
3054d4be151bSAndré Goddard Rosa 	const char *args = (const char *)bin_buf;
30554370aa4aSLai Jiangshan 
3056762abb51SRasmus Villemoes 	if (WARN_ON_ONCE(size > INT_MAX))
30574370aa4aSLai Jiangshan 		return 0;
30584370aa4aSLai Jiangshan 
30594370aa4aSLai Jiangshan 	str = buf;
30604370aa4aSLai Jiangshan 	end = buf + size;
30614370aa4aSLai Jiangshan 
30624370aa4aSLai Jiangshan #define get_arg(type)							\
30634370aa4aSLai Jiangshan ({									\
30644370aa4aSLai Jiangshan 	typeof(type) value;						\
30654370aa4aSLai Jiangshan 	if (sizeof(type) == 8) {					\
30664370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(u32));			\
30674370aa4aSLai Jiangshan 		*(u32 *)&value = *(u32 *)args;				\
30684370aa4aSLai Jiangshan 		*((u32 *)&value + 1) = *(u32 *)(args + 4);		\
30694370aa4aSLai Jiangshan 	} else {							\
30704370aa4aSLai Jiangshan 		args = PTR_ALIGN(args, sizeof(type));			\
30714370aa4aSLai Jiangshan 		value = *(typeof(type) *)args;				\
30724370aa4aSLai Jiangshan 	}								\
30734370aa4aSLai Jiangshan 	args += sizeof(type);						\
30744370aa4aSLai Jiangshan 	value;								\
30754370aa4aSLai Jiangshan })
30764370aa4aSLai Jiangshan 
30774370aa4aSLai Jiangshan 	/* Make sure end is always >= buf */
30784370aa4aSLai Jiangshan 	if (end < buf) {
30794370aa4aSLai Jiangshan 		end = ((void *)-1);
30804370aa4aSLai Jiangshan 		size = end - buf;
30814370aa4aSLai Jiangshan 	}
30824370aa4aSLai Jiangshan 
3083fef20d9cSFrederic Weisbecker 	while (*fmt) {
3084fef20d9cSFrederic Weisbecker 		const char *old_fmt = fmt;
3085d4be151bSAndré Goddard Rosa 		int read = format_decode(fmt, &spec);
3086fef20d9cSFrederic Weisbecker 
3087fef20d9cSFrederic Weisbecker 		fmt += read;
3088fef20d9cSFrederic Weisbecker 
3089fef20d9cSFrederic Weisbecker 		switch (spec.type) {
3090fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_NONE: {
3091fef20d9cSFrederic Weisbecker 			int copy = read;
3092fef20d9cSFrederic Weisbecker 			if (str < end) {
3093fef20d9cSFrederic Weisbecker 				if (copy > end - str)
3094fef20d9cSFrederic Weisbecker 					copy = end - str;
3095fef20d9cSFrederic Weisbecker 				memcpy(str, old_fmt, copy);
3096fef20d9cSFrederic Weisbecker 			}
3097fef20d9cSFrederic Weisbecker 			str += read;
3098fef20d9cSFrederic Weisbecker 			break;
30994370aa4aSLai Jiangshan 		}
31004370aa4aSLai Jiangshan 
3101ed681a91SVegard Nossum 		case FORMAT_TYPE_WIDTH:
31024d72ba01SRasmus Villemoes 			set_field_width(&spec, get_arg(int));
3103fef20d9cSFrederic Weisbecker 			break;
31044370aa4aSLai Jiangshan 
3105fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PRECISION:
31064d72ba01SRasmus Villemoes 			set_precision(&spec, get_arg(int));
3107fef20d9cSFrederic Weisbecker 			break;
31084370aa4aSLai Jiangshan 
3109d4be151bSAndré Goddard Rosa 		case FORMAT_TYPE_CHAR: {
3110d4be151bSAndré Goddard Rosa 			char c;
3111d4be151bSAndré Goddard Rosa 
3112fef20d9cSFrederic Weisbecker 			if (!(spec.flags & LEFT)) {
3113fef20d9cSFrederic Weisbecker 				while (--spec.field_width > 0) {
31144370aa4aSLai Jiangshan 					if (str < end)
31154370aa4aSLai Jiangshan 						*str = ' ';
31164370aa4aSLai Jiangshan 					++str;
31174370aa4aSLai Jiangshan 				}
31184370aa4aSLai Jiangshan 			}
31194370aa4aSLai Jiangshan 			c = (unsigned char) get_arg(char);
31204370aa4aSLai Jiangshan 			if (str < end)
31214370aa4aSLai Jiangshan 				*str = c;
31224370aa4aSLai Jiangshan 			++str;
3123fef20d9cSFrederic Weisbecker 			while (--spec.field_width > 0) {
31244370aa4aSLai Jiangshan 				if (str < end)
31254370aa4aSLai Jiangshan 					*str = ' ';
31264370aa4aSLai Jiangshan 				++str;
31274370aa4aSLai Jiangshan 			}
3128fef20d9cSFrederic Weisbecker 			break;
3129d4be151bSAndré Goddard Rosa 		}
31304370aa4aSLai Jiangshan 
3131fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_STR: {
31324370aa4aSLai Jiangshan 			const char *str_arg = args;
3133d4be151bSAndré Goddard Rosa 			args += strlen(str_arg) + 1;
3134fef20d9cSFrederic Weisbecker 			str = string(str, end, (char *)str_arg, spec);
3135fef20d9cSFrederic Weisbecker 			break;
31364370aa4aSLai Jiangshan 		}
31374370aa4aSLai Jiangshan 
3138841a915dSSteven Rostedt (VMware) 		case FORMAT_TYPE_PTR: {
3139841a915dSSteven Rostedt (VMware) 			bool process = false;
3140841a915dSSteven Rostedt (VMware) 			int copy, len;
3141841a915dSSteven Rostedt (VMware) 			/* Non function dereferences were already done */
3142841a915dSSteven Rostedt (VMware) 			switch (*fmt) {
3143841a915dSSteven Rostedt (VMware) 			case 'S':
3144841a915dSSteven Rostedt (VMware) 			case 's':
3145841a915dSSteven Rostedt (VMware) 			case 'F':
3146841a915dSSteven Rostedt (VMware) 			case 'f':
31471e6338cfSSteven Rostedt (VMware) 			case 'x':
31481e6338cfSSteven Rostedt (VMware) 			case 'K':
314957f5677eSRasmus Villemoes 			case 'e':
3150841a915dSSteven Rostedt (VMware) 				process = true;
3151841a915dSSteven Rostedt (VMware) 				break;
3152841a915dSSteven Rostedt (VMware) 			default:
3153841a915dSSteven Rostedt (VMware) 				if (!isalnum(*fmt)) {
3154841a915dSSteven Rostedt (VMware) 					process = true;
3155841a915dSSteven Rostedt (VMware) 					break;
3156841a915dSSteven Rostedt (VMware) 				}
3157841a915dSSteven Rostedt (VMware) 				/* Pointer dereference was already processed */
3158841a915dSSteven Rostedt (VMware) 				if (str < end) {
3159841a915dSSteven Rostedt (VMware) 					len = copy = strlen(args);
3160841a915dSSteven Rostedt (VMware) 					if (copy > end - str)
3161841a915dSSteven Rostedt (VMware) 						copy = end - str;
3162841a915dSSteven Rostedt (VMware) 					memcpy(str, args, copy);
3163841a915dSSteven Rostedt (VMware) 					str += len;
316462165600SSteven Rostedt (VMware) 					args += len + 1;
3165841a915dSSteven Rostedt (VMware) 				}
3166841a915dSSteven Rostedt (VMware) 			}
3167841a915dSSteven Rostedt (VMware) 			if (process)
3168ffbfed03SRasmus Villemoes 				str = pointer(fmt, str, end, get_arg(void *), spec);
3169841a915dSSteven Rostedt (VMware) 
3170fef20d9cSFrederic Weisbecker 			while (isalnum(*fmt))
31714370aa4aSLai Jiangshan 				fmt++;
3172fef20d9cSFrederic Weisbecker 			break;
3173841a915dSSteven Rostedt (VMware) 		}
31744370aa4aSLai Jiangshan 
3175fef20d9cSFrederic Weisbecker 		case FORMAT_TYPE_PERCENT_CHAR:
31764370aa4aSLai Jiangshan 			if (str < end)
31774370aa4aSLai Jiangshan 				*str = '%';
31784370aa4aSLai Jiangshan 			++str;
3179fef20d9cSFrederic Weisbecker 			break;
3180fef20d9cSFrederic Weisbecker 
3181b006f19bSRasmus Villemoes 		case FORMAT_TYPE_INVALID:
3182b006f19bSRasmus Villemoes 			goto out;
3183b006f19bSRasmus Villemoes 
3184d4be151bSAndré Goddard Rosa 		default: {
3185d4be151bSAndré Goddard Rosa 			unsigned long long num;
3186d4be151bSAndré Goddard Rosa 
3187fef20d9cSFrederic Weisbecker 			switch (spec.type) {
3188fef20d9cSFrederic Weisbecker 
3189fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG_LONG:
31904370aa4aSLai Jiangshan 				num = get_arg(long long);
3191fef20d9cSFrederic Weisbecker 				break;
3192fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_ULONG:
3193fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_LONG:
3194fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned long);
3195fef20d9cSFrederic Weisbecker 				break;
3196fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SIZE_T:
31974370aa4aSLai Jiangshan 				num = get_arg(size_t);
3198fef20d9cSFrederic Weisbecker 				break;
3199fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_PTRDIFF:
32004370aa4aSLai Jiangshan 				num = get_arg(ptrdiff_t);
3201fef20d9cSFrederic Weisbecker 				break;
3202a4e94ef0SZhaolei 			case FORMAT_TYPE_UBYTE:
3203a4e94ef0SZhaolei 				num = get_arg(unsigned char);
3204a4e94ef0SZhaolei 				break;
3205a4e94ef0SZhaolei 			case FORMAT_TYPE_BYTE:
3206a4e94ef0SZhaolei 				num = get_arg(signed char);
3207a4e94ef0SZhaolei 				break;
3208fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_USHORT:
3209fef20d9cSFrederic Weisbecker 				num = get_arg(unsigned short);
3210fef20d9cSFrederic Weisbecker 				break;
3211fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_SHORT:
3212fef20d9cSFrederic Weisbecker 				num = get_arg(short);
3213fef20d9cSFrederic Weisbecker 				break;
3214fef20d9cSFrederic Weisbecker 			case FORMAT_TYPE_UINT:
32154370aa4aSLai Jiangshan 				num = get_arg(unsigned int);
3216fef20d9cSFrederic Weisbecker 				break;
3217fef20d9cSFrederic Weisbecker 			default:
3218fef20d9cSFrederic Weisbecker 				num = get_arg(int);
32194370aa4aSLai Jiangshan 			}
3220fef20d9cSFrederic Weisbecker 
3221fef20d9cSFrederic Weisbecker 			str = number(str, end, num, spec);
3222d4be151bSAndré Goddard Rosa 		} /* default: */
3223d4be151bSAndré Goddard Rosa 		} /* switch(spec.type) */
3224d4be151bSAndré Goddard Rosa 	} /* while(*fmt) */
3225fef20d9cSFrederic Weisbecker 
3226b006f19bSRasmus Villemoes out:
32274370aa4aSLai Jiangshan 	if (size > 0) {
32284370aa4aSLai Jiangshan 		if (str < end)
32294370aa4aSLai Jiangshan 			*str = '\0';
32304370aa4aSLai Jiangshan 		else
32314370aa4aSLai Jiangshan 			end[-1] = '\0';
32324370aa4aSLai Jiangshan 	}
3233fef20d9cSFrederic Weisbecker 
32344370aa4aSLai Jiangshan #undef get_arg
32354370aa4aSLai Jiangshan 
32364370aa4aSLai Jiangshan 	/* the trailing null byte doesn't count towards the total */
32374370aa4aSLai Jiangshan 	return str - buf;
32384370aa4aSLai Jiangshan }
32394370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
32404370aa4aSLai Jiangshan 
32414370aa4aSLai Jiangshan /**
32424370aa4aSLai Jiangshan  * bprintf - Parse a format string and place args' binary value in a buffer
32434370aa4aSLai Jiangshan  * @bin_buf: The buffer to place args' binary value
32444370aa4aSLai Jiangshan  * @size: The size of the buffer(by words(32bits), not characters)
32454370aa4aSLai Jiangshan  * @fmt: The format string to use
32464370aa4aSLai Jiangshan  * @...: Arguments for the format string
32474370aa4aSLai Jiangshan  *
32484370aa4aSLai Jiangshan  * The function returns the number of words(u32) written
32494370aa4aSLai Jiangshan  * into @bin_buf.
32504370aa4aSLai Jiangshan  */
32514370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
32524370aa4aSLai Jiangshan {
32534370aa4aSLai Jiangshan 	va_list args;
32544370aa4aSLai Jiangshan 	int ret;
32554370aa4aSLai Jiangshan 
32564370aa4aSLai Jiangshan 	va_start(args, fmt);
32574370aa4aSLai Jiangshan 	ret = vbin_printf(bin_buf, size, fmt, args);
32584370aa4aSLai Jiangshan 	va_end(args);
32597b9186f5SAndré Goddard Rosa 
32604370aa4aSLai Jiangshan 	return ret;
32614370aa4aSLai Jiangshan }
32624370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf);
32634370aa4aSLai Jiangshan 
32644370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
32654370aa4aSLai Jiangshan 
32661da177e4SLinus Torvalds /**
32671da177e4SLinus Torvalds  * vsscanf - Unformat a buffer into a list of arguments
32681da177e4SLinus Torvalds  * @buf:	input buffer
32691da177e4SLinus Torvalds  * @fmt:	format of buffer
32701da177e4SLinus Torvalds  * @args:	arguments
32711da177e4SLinus Torvalds  */
32721da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
32731da177e4SLinus Torvalds {
32741da177e4SLinus Torvalds 	const char *str = buf;
32751da177e4SLinus Torvalds 	char *next;
32761da177e4SLinus Torvalds 	char digit;
32771da177e4SLinus Torvalds 	int num = 0;
3278ef0658f3SJoe Perches 	u8 qualifier;
327953809751SJan Beulich 	unsigned int base;
328053809751SJan Beulich 	union {
328153809751SJan Beulich 		long long s;
328253809751SJan Beulich 		unsigned long long u;
328353809751SJan Beulich 	} val;
3284ef0658f3SJoe Perches 	s16 field_width;
3285d4be151bSAndré Goddard Rosa 	bool is_sign;
32861da177e4SLinus Torvalds 
3287da99075cSJan Beulich 	while (*fmt) {
32881da177e4SLinus Torvalds 		/* skip any white space in format */
32891da177e4SLinus Torvalds 		/* white space in format matchs any amount of
32901da177e4SLinus Torvalds 		 * white space, including none, in the input.
32911da177e4SLinus Torvalds 		 */
32921da177e4SLinus Torvalds 		if (isspace(*fmt)) {
3293e7d2860bSAndré Goddard Rosa 			fmt = skip_spaces(++fmt);
3294e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
32951da177e4SLinus Torvalds 		}
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds 		/* anything that is not a conversion must match exactly */
32981da177e4SLinus Torvalds 		if (*fmt != '%' && *fmt) {
32991da177e4SLinus Torvalds 			if (*fmt++ != *str++)
33001da177e4SLinus Torvalds 				break;
33011da177e4SLinus Torvalds 			continue;
33021da177e4SLinus Torvalds 		}
33031da177e4SLinus Torvalds 
33041da177e4SLinus Torvalds 		if (!*fmt)
33051da177e4SLinus Torvalds 			break;
33061da177e4SLinus Torvalds 		++fmt;
33071da177e4SLinus Torvalds 
33081da177e4SLinus Torvalds 		/* skip this conversion.
33091da177e4SLinus Torvalds 		 * advance both strings to next white space
33101da177e4SLinus Torvalds 		 */
33111da177e4SLinus Torvalds 		if (*fmt == '*') {
3312da99075cSJan Beulich 			if (!*str)
3313da99075cSJan Beulich 				break;
3314f9310b2fSJessica Yu 			while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3315f9310b2fSJessica Yu 				/* '%*[' not yet supported, invalid format */
3316f9310b2fSJessica Yu 				if (*fmt == '[')
3317f9310b2fSJessica Yu 					return num;
33181da177e4SLinus Torvalds 				fmt++;
3319f9310b2fSJessica Yu 			}
33201da177e4SLinus Torvalds 			while (!isspace(*str) && *str)
33211da177e4SLinus Torvalds 				str++;
33221da177e4SLinus Torvalds 			continue;
33231da177e4SLinus Torvalds 		}
33241da177e4SLinus Torvalds 
33251da177e4SLinus Torvalds 		/* get field width */
33261da177e4SLinus Torvalds 		field_width = -1;
332753809751SJan Beulich 		if (isdigit(*fmt)) {
33281da177e4SLinus Torvalds 			field_width = skip_atoi(&fmt);
332953809751SJan Beulich 			if (field_width <= 0)
333053809751SJan Beulich 				break;
333153809751SJan Beulich 		}
33321da177e4SLinus Torvalds 
33331da177e4SLinus Torvalds 		/* get conversion qualifier */
33341da177e4SLinus Torvalds 		qualifier = -1;
333575fb8f26SAndy Shevchenko 		if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
33365b5e0928SAlexey Dobriyan 		    *fmt == 'z') {
33371da177e4SLinus Torvalds 			qualifier = *fmt++;
33381da177e4SLinus Torvalds 			if (unlikely(qualifier == *fmt)) {
33391da177e4SLinus Torvalds 				if (qualifier == 'h') {
33401da177e4SLinus Torvalds 					qualifier = 'H';
33411da177e4SLinus Torvalds 					fmt++;
33421da177e4SLinus Torvalds 				} else if (qualifier == 'l') {
33431da177e4SLinus Torvalds 					qualifier = 'L';
33441da177e4SLinus Torvalds 					fmt++;
33451da177e4SLinus Torvalds 				}
33461da177e4SLinus Torvalds 			}
33471da177e4SLinus Torvalds 		}
33481da177e4SLinus Torvalds 
3349da99075cSJan Beulich 		if (!*fmt)
3350da99075cSJan Beulich 			break;
3351da99075cSJan Beulich 
3352da99075cSJan Beulich 		if (*fmt == 'n') {
3353da99075cSJan Beulich 			/* return number of characters read so far */
3354da99075cSJan Beulich 			*va_arg(args, int *) = str - buf;
3355da99075cSJan Beulich 			++fmt;
3356da99075cSJan Beulich 			continue;
3357da99075cSJan Beulich 		}
3358da99075cSJan Beulich 
3359da99075cSJan Beulich 		if (!*str)
33601da177e4SLinus Torvalds 			break;
33611da177e4SLinus Torvalds 
3362d4be151bSAndré Goddard Rosa 		base = 10;
33633f623ebaSFabian Frederick 		is_sign = false;
3364d4be151bSAndré Goddard Rosa 
33651da177e4SLinus Torvalds 		switch (*fmt++) {
33661da177e4SLinus Torvalds 		case 'c':
33671da177e4SLinus Torvalds 		{
33681da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char*);
33691da177e4SLinus Torvalds 			if (field_width == -1)
33701da177e4SLinus Torvalds 				field_width = 1;
33711da177e4SLinus Torvalds 			do {
33721da177e4SLinus Torvalds 				*s++ = *str++;
33731da177e4SLinus Torvalds 			} while (--field_width > 0 && *str);
33741da177e4SLinus Torvalds 			num++;
33751da177e4SLinus Torvalds 		}
33761da177e4SLinus Torvalds 		continue;
33771da177e4SLinus Torvalds 		case 's':
33781da177e4SLinus Torvalds 		{
33791da177e4SLinus Torvalds 			char *s = (char *)va_arg(args, char *);
33801da177e4SLinus Torvalds 			if (field_width == -1)
33814be929beSAlexey Dobriyan 				field_width = SHRT_MAX;
33821da177e4SLinus Torvalds 			/* first, skip leading white space in buffer */
3383e7d2860bSAndré Goddard Rosa 			str = skip_spaces(str);
33841da177e4SLinus Torvalds 
33851da177e4SLinus Torvalds 			/* now copy until next white space */
33867b9186f5SAndré Goddard Rosa 			while (*str && !isspace(*str) && field_width--)
33871da177e4SLinus Torvalds 				*s++ = *str++;
33881da177e4SLinus Torvalds 			*s = '\0';
33891da177e4SLinus Torvalds 			num++;
33901da177e4SLinus Torvalds 		}
33911da177e4SLinus Torvalds 		continue;
3392f9310b2fSJessica Yu 		/*
3393f9310b2fSJessica Yu 		 * Warning: This implementation of the '[' conversion specifier
3394f9310b2fSJessica Yu 		 * deviates from its glibc counterpart in the following ways:
3395f9310b2fSJessica Yu 		 * (1) It does NOT support ranges i.e. '-' is NOT a special
3396f9310b2fSJessica Yu 		 *     character
3397f9310b2fSJessica Yu 		 * (2) It cannot match the closing bracket ']' itself
3398f9310b2fSJessica Yu 		 * (3) A field width is required
3399f9310b2fSJessica Yu 		 * (4) '%*[' (discard matching input) is currently not supported
3400f9310b2fSJessica Yu 		 *
3401f9310b2fSJessica Yu 		 * Example usage:
3402f9310b2fSJessica Yu 		 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3403f9310b2fSJessica Yu 		 *		buf1, buf2, buf3);
3404f9310b2fSJessica Yu 		 * if (ret < 3)
3405f9310b2fSJessica Yu 		 *    // etc..
3406f9310b2fSJessica Yu 		 */
3407f9310b2fSJessica Yu 		case '[':
3408f9310b2fSJessica Yu 		{
3409f9310b2fSJessica Yu 			char *s = (char *)va_arg(args, char *);
3410f9310b2fSJessica Yu 			DECLARE_BITMAP(set, 256) = {0};
3411f9310b2fSJessica Yu 			unsigned int len = 0;
3412f9310b2fSJessica Yu 			bool negate = (*fmt == '^');
3413f9310b2fSJessica Yu 
3414f9310b2fSJessica Yu 			/* field width is required */
3415f9310b2fSJessica Yu 			if (field_width == -1)
3416f9310b2fSJessica Yu 				return num;
3417f9310b2fSJessica Yu 
3418f9310b2fSJessica Yu 			if (negate)
3419f9310b2fSJessica Yu 				++fmt;
3420f9310b2fSJessica Yu 
3421f9310b2fSJessica Yu 			for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3422f9310b2fSJessica Yu 				set_bit((u8)*fmt, set);
3423f9310b2fSJessica Yu 
3424f9310b2fSJessica Yu 			/* no ']' or no character set found */
3425f9310b2fSJessica Yu 			if (!*fmt || !len)
3426f9310b2fSJessica Yu 				return num;
3427f9310b2fSJessica Yu 			++fmt;
3428f9310b2fSJessica Yu 
3429f9310b2fSJessica Yu 			if (negate) {
3430f9310b2fSJessica Yu 				bitmap_complement(set, set, 256);
3431f9310b2fSJessica Yu 				/* exclude null '\0' byte */
3432f9310b2fSJessica Yu 				clear_bit(0, set);
3433f9310b2fSJessica Yu 			}
3434f9310b2fSJessica Yu 
3435f9310b2fSJessica Yu 			/* match must be non-empty */
3436f9310b2fSJessica Yu 			if (!test_bit((u8)*str, set))
3437f9310b2fSJessica Yu 				return num;
3438f9310b2fSJessica Yu 
3439f9310b2fSJessica Yu 			while (test_bit((u8)*str, set) && field_width--)
3440f9310b2fSJessica Yu 				*s++ = *str++;
3441f9310b2fSJessica Yu 			*s = '\0';
3442f9310b2fSJessica Yu 			++num;
3443f9310b2fSJessica Yu 		}
3444f9310b2fSJessica Yu 		continue;
34451da177e4SLinus Torvalds 		case 'o':
34461da177e4SLinus Torvalds 			base = 8;
34471da177e4SLinus Torvalds 			break;
34481da177e4SLinus Torvalds 		case 'x':
34491da177e4SLinus Torvalds 		case 'X':
34501da177e4SLinus Torvalds 			base = 16;
34511da177e4SLinus Torvalds 			break;
34521da177e4SLinus Torvalds 		case 'i':
34531da177e4SLinus Torvalds 			base = 0;
34544c1ca831SNick Desaulniers 			fallthrough;
34551da177e4SLinus Torvalds 		case 'd':
34563f623ebaSFabian Frederick 			is_sign = true;
34574c1ca831SNick Desaulniers 			fallthrough;
34581da177e4SLinus Torvalds 		case 'u':
34591da177e4SLinus Torvalds 			break;
34601da177e4SLinus Torvalds 		case '%':
34611da177e4SLinus Torvalds 			/* looking for '%' in str */
34621da177e4SLinus Torvalds 			if (*str++ != '%')
34631da177e4SLinus Torvalds 				return num;
34641da177e4SLinus Torvalds 			continue;
34651da177e4SLinus Torvalds 		default:
34661da177e4SLinus Torvalds 			/* invalid format; stop here */
34671da177e4SLinus Torvalds 			return num;
34681da177e4SLinus Torvalds 		}
34691da177e4SLinus Torvalds 
34701da177e4SLinus Torvalds 		/* have some sort of integer conversion.
34711da177e4SLinus Torvalds 		 * first, skip white space in buffer.
34721da177e4SLinus Torvalds 		 */
3473e7d2860bSAndré Goddard Rosa 		str = skip_spaces(str);
34741da177e4SLinus Torvalds 
34751da177e4SLinus Torvalds 		digit = *str;
34761da177e4SLinus Torvalds 		if (is_sign && digit == '-')
34771da177e4SLinus Torvalds 			digit = *(str + 1);
34781da177e4SLinus Torvalds 
34791da177e4SLinus Torvalds 		if (!digit
34801da177e4SLinus Torvalds 		    || (base == 16 && !isxdigit(digit))
34811da177e4SLinus Torvalds 		    || (base == 10 && !isdigit(digit))
34821da177e4SLinus Torvalds 		    || (base == 8 && (!isdigit(digit) || digit > '7'))
34831da177e4SLinus Torvalds 		    || (base == 0 && !isdigit(digit)))
34841da177e4SLinus Torvalds 			break;
34851da177e4SLinus Torvalds 
348653809751SJan Beulich 		if (is_sign)
348753809751SJan Beulich 			val.s = qualifier != 'L' ?
348853809751SJan Beulich 				simple_strtol(str, &next, base) :
348953809751SJan Beulich 				simple_strtoll(str, &next, base);
349053809751SJan Beulich 		else
349153809751SJan Beulich 			val.u = qualifier != 'L' ?
349253809751SJan Beulich 				simple_strtoul(str, &next, base) :
349353809751SJan Beulich 				simple_strtoull(str, &next, base);
349453809751SJan Beulich 
349553809751SJan Beulich 		if (field_width > 0 && next - str > field_width) {
349653809751SJan Beulich 			if (base == 0)
349753809751SJan Beulich 				_parse_integer_fixup_radix(str, &base);
349853809751SJan Beulich 			while (next - str > field_width) {
349953809751SJan Beulich 				if (is_sign)
350053809751SJan Beulich 					val.s = div_s64(val.s, base);
350153809751SJan Beulich 				else
350253809751SJan Beulich 					val.u = div_u64(val.u, base);
350353809751SJan Beulich 				--next;
350453809751SJan Beulich 			}
350553809751SJan Beulich 		}
350653809751SJan Beulich 
35071da177e4SLinus Torvalds 		switch (qualifier) {
35081da177e4SLinus Torvalds 		case 'H':	/* that's 'hh' in format */
350953809751SJan Beulich 			if (is_sign)
351053809751SJan Beulich 				*va_arg(args, signed char *) = val.s;
351153809751SJan Beulich 			else
351253809751SJan Beulich 				*va_arg(args, unsigned char *) = val.u;
35131da177e4SLinus Torvalds 			break;
35141da177e4SLinus Torvalds 		case 'h':
351553809751SJan Beulich 			if (is_sign)
351653809751SJan Beulich 				*va_arg(args, short *) = val.s;
351753809751SJan Beulich 			else
351853809751SJan Beulich 				*va_arg(args, unsigned short *) = val.u;
35191da177e4SLinus Torvalds 			break;
35201da177e4SLinus Torvalds 		case 'l':
352153809751SJan Beulich 			if (is_sign)
352253809751SJan Beulich 				*va_arg(args, long *) = val.s;
352353809751SJan Beulich 			else
352453809751SJan Beulich 				*va_arg(args, unsigned long *) = val.u;
35251da177e4SLinus Torvalds 			break;
35261da177e4SLinus Torvalds 		case 'L':
352753809751SJan Beulich 			if (is_sign)
352853809751SJan Beulich 				*va_arg(args, long long *) = val.s;
352953809751SJan Beulich 			else
353053809751SJan Beulich 				*va_arg(args, unsigned long long *) = val.u;
35311da177e4SLinus Torvalds 			break;
35321da177e4SLinus Torvalds 		case 'z':
353353809751SJan Beulich 			*va_arg(args, size_t *) = val.u;
35341da177e4SLinus Torvalds 			break;
35351da177e4SLinus Torvalds 		default:
353653809751SJan Beulich 			if (is_sign)
353753809751SJan Beulich 				*va_arg(args, int *) = val.s;
353853809751SJan Beulich 			else
353953809751SJan Beulich 				*va_arg(args, unsigned int *) = val.u;
35401da177e4SLinus Torvalds 			break;
35411da177e4SLinus Torvalds 		}
35421da177e4SLinus Torvalds 		num++;
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds 		if (!next)
35451da177e4SLinus Torvalds 			break;
35461da177e4SLinus Torvalds 		str = next;
35471da177e4SLinus Torvalds 	}
3548c6b40d16SJohannes Berg 
35491da177e4SLinus Torvalds 	return num;
35501da177e4SLinus Torvalds }
35511da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf);
35521da177e4SLinus Torvalds 
35531da177e4SLinus Torvalds /**
35541da177e4SLinus Torvalds  * sscanf - Unformat a buffer into a list of arguments
35551da177e4SLinus Torvalds  * @buf:	input buffer
35561da177e4SLinus Torvalds  * @fmt:	formatting of buffer
35571da177e4SLinus Torvalds  * @...:	resulting arguments
35581da177e4SLinus Torvalds  */
35591da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
35601da177e4SLinus Torvalds {
35611da177e4SLinus Torvalds 	va_list args;
35621da177e4SLinus Torvalds 	int i;
35631da177e4SLinus Torvalds 
35641da177e4SLinus Torvalds 	va_start(args, fmt);
35651da177e4SLinus Torvalds 	i = vsscanf(buf, fmt, args);
35661da177e4SLinus Torvalds 	va_end(args);
35677b9186f5SAndré Goddard Rosa 
35681da177e4SLinus Torvalds 	return i;
35691da177e4SLinus Torvalds }
35701da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf);
3571