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 <[email protected]>
151da177e4SLinus Torvalds * - changed to provide snprintf and vsnprintf functions
161da177e4SLinus Torvalds * So Feb 1 16:51:32 CET 2004 Juergen Quade <[email protected]>
171da177e4SLinus Torvalds * - scnprintf and vscnprintf
181da177e4SLinus Torvalds */
191da177e4SLinus Torvalds
20c0891ac1SAlexey Dobriyan #include <linux/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>
3739ced19bSAndy Shevchenko #include <linux/sprintf.h>
387daac5b2SAndy Shevchenko #include <linux/time.h>
392b1b0d66SAndy Shevchenko #include <linux/uuid.h>
40ce4fecf1SPantelis Antoniou #include <linux/of.h>
418a27f7c9SJoe Perches #include <net/addrconf.h>
42ad67b74dSTobin C. Harding #include <linux/siphash.h>
43ad67b74dSTobin C. Harding #include <linux/compiler.h>
44a92eb762SSakari Ailus #include <linux/property.h>
45898f1e5cSJason A. Donenfeld #include <linux/notifier.h>
461031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
471031bc58SDmitry Monakhov #include <linux/blkdev.h>
481031bc58SDmitry Monakhov #endif
491da177e4SLinus Torvalds
50edf14cdbSVlastimil Babka #include "../mm/internal.h" /* For the trace_print_flags arrays */
51edf14cdbSVlastimil Babka
524e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */
537c43d9a3SRasmus Villemoes #include <asm/byteorder.h> /* cpu_to_le16 */
545f60d5f6SAl Viro #include <linux/unaligned.h>
551da177e4SLinus Torvalds
5671dca95dSAndy Shevchenko #include <linux/string_helpers.h>
571dff46d6SAlexey Dobriyan #include "kstrtox.h"
58aa46a63eSHarvey Harrison
5984842911SChristophe Leroy /* Disable pointer hashing if requested */
6084842911SChristophe Leroy bool no_hash_pointers __ro_after_init;
6184842911SChristophe Leroy EXPORT_SYMBOL_GPL(no_hash_pointers);
6284842911SChristophe Leroy
6372fcce70SAlexey Dobriyan noinline
simple_strntoull(const char * startp,char ** endp,unsigned int base,size_t max_chars)6472fcce70SAlexey Dobriyan static unsigned long long simple_strntoull(const char *startp, char **endp, unsigned int base, size_t max_chars)
65900fdc45SRichard Fitzgerald {
66900fdc45SRichard Fitzgerald const char *cp;
67900fdc45SRichard Fitzgerald unsigned long long result = 0ULL;
68900fdc45SRichard Fitzgerald size_t prefix_chars;
69900fdc45SRichard Fitzgerald unsigned int rv;
70900fdc45SRichard Fitzgerald
71900fdc45SRichard Fitzgerald cp = _parse_integer_fixup_radix(startp, &base);
72900fdc45SRichard Fitzgerald prefix_chars = cp - startp;
73900fdc45SRichard Fitzgerald if (prefix_chars < max_chars) {
74900fdc45SRichard Fitzgerald rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars);
75900fdc45SRichard Fitzgerald /* FIXME */
76900fdc45SRichard Fitzgerald cp += (rv & ~KSTRTOX_OVERFLOW);
77900fdc45SRichard Fitzgerald } else {
78900fdc45SRichard Fitzgerald /* Field too short for prefix + digit, skip over without converting */
79900fdc45SRichard Fitzgerald cp = startp + max_chars;
80900fdc45SRichard Fitzgerald }
81900fdc45SRichard Fitzgerald
82900fdc45SRichard Fitzgerald if (endp)
83900fdc45SRichard Fitzgerald *endp = (char *)cp;
84900fdc45SRichard Fitzgerald
85900fdc45SRichard Fitzgerald return result;
86900fdc45SRichard Fitzgerald }
87900fdc45SRichard Fitzgerald
881da177e4SLinus Torvalds /**
891da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long
901da177e4SLinus Torvalds * @cp: The start of the string
911da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here
921da177e4SLinus Torvalds * @base: The number base to use
93462e4711SEldad Zack *
94e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoull instead.
951da177e4SLinus Torvalds */
96ad65dcefSAlexey Dobriyan noinline
simple_strtoull(const char * cp,char ** endp,unsigned int base)971da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
981da177e4SLinus Torvalds {
9972fcce70SAlexey Dobriyan return simple_strntoull(cp, endp, base, INT_MAX);
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull);
1021da177e4SLinus Torvalds
1031da177e4SLinus Torvalds /**
104922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long
105922ac25cSAndré Goddard Rosa * @cp: The start of the string
106922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here
107922ac25cSAndré Goddard Rosa * @base: The number base to use
108462e4711SEldad Zack *
109e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoul instead.
110922ac25cSAndré Goddard Rosa */
simple_strtoul(const char * cp,char ** endp,unsigned int base)111922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
112922ac25cSAndré Goddard Rosa {
113922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base);
114922ac25cSAndré Goddard Rosa }
115922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul);
116922ac25cSAndré Goddard Rosa
simple_strntoul(const char * cp,char ** endp,unsigned int base,size_t max_chars)117fcc15500SDavid Disseldorp unsigned long simple_strntoul(const char *cp, char **endp, unsigned int base,
118fcc15500SDavid Disseldorp size_t max_chars)
119fcc15500SDavid Disseldorp {
120fcc15500SDavid Disseldorp return simple_strntoull(cp, endp, base, max_chars);
121fcc15500SDavid Disseldorp }
122fcc15500SDavid Disseldorp EXPORT_SYMBOL(simple_strntoul);
123fcc15500SDavid Disseldorp
124922ac25cSAndré Goddard Rosa /**
125922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long
126922ac25cSAndré Goddard Rosa * @cp: The start of the string
127922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here
128922ac25cSAndré Goddard Rosa * @base: The number base to use
129462e4711SEldad Zack *
130e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtol instead.
131922ac25cSAndré Goddard Rosa */
simple_strtol(const char * cp,char ** endp,unsigned int base)132922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base)
133922ac25cSAndré Goddard Rosa {
134922ac25cSAndré Goddard Rosa if (*cp == '-')
135922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base);
136922ac25cSAndré Goddard Rosa
137922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base);
138922ac25cSAndré Goddard Rosa }
139922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol);
140922ac25cSAndré Goddard Rosa
14172fcce70SAlexey Dobriyan noinline
simple_strntoll(const char * cp,char ** endp,unsigned int base,size_t max_chars)14272fcce70SAlexey Dobriyan static long long simple_strntoll(const char *cp, char **endp, unsigned int base, size_t max_chars)
143900fdc45SRichard Fitzgerald {
144900fdc45SRichard Fitzgerald /*
145900fdc45SRichard Fitzgerald * simple_strntoull() safely handles receiving max_chars==0 in the
146900fdc45SRichard Fitzgerald * case cp[0] == '-' && max_chars == 1.
147900fdc45SRichard Fitzgerald * If max_chars == 0 we can drop through and pass it to simple_strntoull()
148900fdc45SRichard Fitzgerald * and the content of *cp is irrelevant.
149900fdc45SRichard Fitzgerald */
150900fdc45SRichard Fitzgerald if (*cp == '-' && max_chars > 0)
15172fcce70SAlexey Dobriyan return -simple_strntoull(cp + 1, endp, base, max_chars - 1);
152900fdc45SRichard Fitzgerald
15372fcce70SAlexey Dobriyan return simple_strntoull(cp, endp, base, max_chars);
154900fdc45SRichard Fitzgerald }
155900fdc45SRichard Fitzgerald
156922ac25cSAndré Goddard Rosa /**
1571da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long
1581da177e4SLinus Torvalds * @cp: The start of the string
1591da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here
1601da177e4SLinus Torvalds * @base: The number base to use
161462e4711SEldad Zack *
162e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoll instead.
1631da177e4SLinus Torvalds */
simple_strtoll(const char * cp,char ** endp,unsigned int base)1641da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1651da177e4SLinus Torvalds {
16672fcce70SAlexey Dobriyan return simple_strntoll(cp, endp, base, INT_MAX);
1671da177e4SLinus Torvalds }
16898d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll);
1691da177e4SLinus Torvalds
skip_atoi(const char ** s)170f372b225SLinus Torvalds static inline int skip_atoi(const char **s)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds int i = 0;
1731da177e4SLinus Torvalds
17443e5b666SRasmus Villemoes do {
1751da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0';
17643e5b666SRasmus Villemoes } while (isdigit(**s));
1777b9186f5SAndré Goddard Rosa
1781da177e4SLinus Torvalds return i;
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds
1817c43d9a3SRasmus Villemoes /*
1827c43d9a3SRasmus Villemoes * Decimal conversion is by far the most typical, and is used for
1837c43d9a3SRasmus Villemoes * /proc and /sys data. This directly impacts e.g. top performance
1847c43d9a3SRasmus Villemoes * with many processes running. We optimize it for speed by emitting
1857c43d9a3SRasmus Villemoes * two characters at a time, using a 200 byte lookup table. This
1867c43d9a3SRasmus Villemoes * roughly halves the number of multiplications compared to computing
1877c43d9a3SRasmus Villemoes * the digits one at a time. Implementation strongly inspired by the
1887c43d9a3SRasmus Villemoes * previous version, which in turn used ideas described at
1897c43d9a3SRasmus Villemoes * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
1907c43d9a3SRasmus Villemoes * from the author, Douglas W. Jones).
1917c43d9a3SRasmus Villemoes *
1927c43d9a3SRasmus Villemoes * It turns out there is precisely one 26 bit fixed-point
1937c43d9a3SRasmus Villemoes * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
1947c43d9a3SRasmus Villemoes * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
1957c43d9a3SRasmus Villemoes * range happens to be somewhat larger (x <= 1073741898), but that's
1967c43d9a3SRasmus Villemoes * irrelevant for our purpose.
1977c43d9a3SRasmus Villemoes *
1987c43d9a3SRasmus Villemoes * For dividing a number in the range [10^4, 10^6-1] by 100, we still
1997c43d9a3SRasmus Villemoes * need a 32x32->64 bit multiply, so we simply use the same constant.
2007c43d9a3SRasmus Villemoes *
2017c43d9a3SRasmus Villemoes * For dividing a number in the range [100, 10^4-1] by 100, there are
2027c43d9a3SRasmus Villemoes * several options. The simplest is (x * 0x147b) >> 19, which is valid
2037c43d9a3SRasmus Villemoes * for all x <= 43698.
204133fd9f5SDenys Vlasenko */
2054277eeddSDenis Vlasenko
2067c43d9a3SRasmus Villemoes static const u16 decpair[100] = {
2077c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
2087c43d9a3SRasmus Villemoes _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
2097c43d9a3SRasmus Villemoes _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
2107c43d9a3SRasmus Villemoes _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
2117c43d9a3SRasmus Villemoes _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
2127c43d9a3SRasmus Villemoes _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
2137c43d9a3SRasmus Villemoes _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
2147c43d9a3SRasmus Villemoes _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
2157c43d9a3SRasmus Villemoes _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
2167c43d9a3SRasmus Villemoes _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
2177c43d9a3SRasmus Villemoes _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
2187c43d9a3SRasmus Villemoes #undef _
2197c43d9a3SRasmus Villemoes };
2204277eeddSDenis Vlasenko
2217b9186f5SAndré Goddard Rosa /*
2227c43d9a3SRasmus Villemoes * This will print a single '0' even if r == 0, since we would
223675cf53cSRasmus Villemoes * immediately jump to out_r where two 0s would be written but only
224675cf53cSRasmus Villemoes * one of them accounted for in buf. This is needed by ip4_string
225675cf53cSRasmus Villemoes * below. All other callers pass a non-zero value of r.
226133fd9f5SDenys Vlasenko */
227133fd9f5SDenys Vlasenko static noinline_for_stack
put_dec_trunc8(char * buf,unsigned r)228133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r)
229133fd9f5SDenys Vlasenko {
230133fd9f5SDenys Vlasenko unsigned q;
2314277eeddSDenis Vlasenko
2327c43d9a3SRasmus Villemoes /* 1 <= r < 10^8 */
2337c43d9a3SRasmus Villemoes if (r < 100)
2347c43d9a3SRasmus Villemoes goto out_r;
235cb239d0aSGeorge Spelvin
2367c43d9a3SRasmus Villemoes /* 100 <= r < 10^8 */
2377c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32;
2387c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q];
2397c43d9a3SRasmus Villemoes buf += 2;
2407c43d9a3SRasmus Villemoes
2417c43d9a3SRasmus Villemoes /* 1 <= q < 10^6 */
2427c43d9a3SRasmus Villemoes if (q < 100)
2437c43d9a3SRasmus Villemoes goto out_q;
2447c43d9a3SRasmus Villemoes
2457c43d9a3SRasmus Villemoes /* 100 <= q < 10^6 */
2467c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32;
2477c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r];
2487c43d9a3SRasmus Villemoes buf += 2;
2497c43d9a3SRasmus Villemoes
2507c43d9a3SRasmus Villemoes /* 1 <= r < 10^4 */
2517c43d9a3SRasmus Villemoes if (r < 100)
2527c43d9a3SRasmus Villemoes goto out_r;
2537c43d9a3SRasmus Villemoes
2547c43d9a3SRasmus Villemoes /* 100 <= r < 10^4 */
2557c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19;
2567c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q];
2577c43d9a3SRasmus Villemoes buf += 2;
2587c43d9a3SRasmus Villemoes out_q:
2597c43d9a3SRasmus Villemoes /* 1 <= q < 100 */
2607c43d9a3SRasmus Villemoes r = q;
2617c43d9a3SRasmus Villemoes out_r:
2627c43d9a3SRasmus Villemoes /* 1 <= r < 100 */
2637c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r];
264675cf53cSRasmus Villemoes buf += r < 10 ? 1 : 2;
265133fd9f5SDenys Vlasenko return buf;
266133fd9f5SDenys Vlasenko }
267133fd9f5SDenys Vlasenko
2687c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
2697c43d9a3SRasmus Villemoes static noinline_for_stack
put_dec_full8(char * buf,unsigned r)2707c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r)
2717c43d9a3SRasmus Villemoes {
2727c43d9a3SRasmus Villemoes unsigned q;
273133fd9f5SDenys Vlasenko
2747c43d9a3SRasmus Villemoes /* 0 <= r < 10^8 */
2757c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32;
2767c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q];
2777c43d9a3SRasmus Villemoes buf += 2;
278133fd9f5SDenys Vlasenko
2797c43d9a3SRasmus Villemoes /* 0 <= q < 10^6 */
2807c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32;
2817c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r];
2827c43d9a3SRasmus Villemoes buf += 2;
283133fd9f5SDenys Vlasenko
2847c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */
2857c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19;
2867c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q];
2877c43d9a3SRasmus Villemoes buf += 2;
2887c43d9a3SRasmus Villemoes
2897c43d9a3SRasmus Villemoes /* 0 <= q < 100 */
2907c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q];
2917c43d9a3SRasmus Villemoes buf += 2;
2927c43d9a3SRasmus Villemoes return buf;
2937c43d9a3SRasmus Villemoes }
2947c43d9a3SRasmus Villemoes
2957c43d9a3SRasmus Villemoes static noinline_for_stack
put_dec(char * buf,unsigned long long n)296133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
297133fd9f5SDenys Vlasenko {
298133fd9f5SDenys Vlasenko if (n >= 100*1000*1000)
2997c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
3007c43d9a3SRasmus Villemoes /* 1 <= n <= 1.6e11 */
3017c43d9a3SRasmus Villemoes if (n >= 100*1000*1000)
3027c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
3037c43d9a3SRasmus Villemoes /* 1 <= n < 1e8 */
304133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n);
305133fd9f5SDenys Vlasenko }
306133fd9f5SDenys Vlasenko
3077c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
308133fd9f5SDenys Vlasenko
3097c43d9a3SRasmus Villemoes static void
put_dec_full4(char * buf,unsigned r)3107c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r)
311133fd9f5SDenys Vlasenko {
3127c43d9a3SRasmus Villemoes unsigned q;
3137c43d9a3SRasmus Villemoes
3147c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */
3157c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19;
3167c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q];
3177c43d9a3SRasmus Villemoes buf += 2;
3187c43d9a3SRasmus Villemoes /* 0 <= q < 100 */
3197c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q];
3202359172aSGeorge Spelvin }
3212359172aSGeorge Spelvin
3222359172aSGeorge Spelvin /*
3232359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000.
3242359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43
3252359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this
3262359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955.
3277c43d9a3SRasmus Villemoes * (second call in the put_dec code, assuming n is all-ones).
3282359172aSGeorge Spelvin */
3297c43d9a3SRasmus Villemoes static noinline_for_stack
put_dec_helper4(char * buf,unsigned x)3302359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x)
3312359172aSGeorge Spelvin {
3322359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
3332359172aSGeorge Spelvin
3342359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000);
3352359172aSGeorge Spelvin return q;
336133fd9f5SDenys Vlasenko }
337133fd9f5SDenys Vlasenko
338133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at
339133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
340133fd9f5SDenys Vlasenko * (with permission from the author).
341133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines.
342133fd9f5SDenys Vlasenko */
343133fd9f5SDenys Vlasenko static
put_dec(char * buf,unsigned long long n)344133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n)
345133fd9f5SDenys Vlasenko {
346133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h;
347133fd9f5SDenys Vlasenko
348133fd9f5SDenys Vlasenko if (n < 100*1000*1000)
349133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n);
350133fd9f5SDenys Vlasenko
351133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
352133fd9f5SDenys Vlasenko h = (n >> 32);
353133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff;
354133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */
355133fd9f5SDenys Vlasenko
3567c43d9a3SRasmus Villemoes /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
3577c43d9a3SRasmus Villemoes = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
358133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
3592359172aSGeorge Spelvin q = put_dec_helper4(buf, q);
360133fd9f5SDenys Vlasenko
3612359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1;
3622359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q);
363133fd9f5SDenys Vlasenko
3642359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2;
3652359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q);
366133fd9f5SDenys Vlasenko
3672359172aSGeorge Spelvin q += 281 * d3;
3682359172aSGeorge Spelvin buf += 12;
3692359172aSGeorge Spelvin if (q)
3702359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q);
3712359172aSGeorge Spelvin else while (buf[-1] == '0')
372133fd9f5SDenys Vlasenko --buf;
3737b9186f5SAndré Goddard Rosa
3744277eeddSDenis Vlasenko return buf;
3754277eeddSDenis Vlasenko }
376133fd9f5SDenys Vlasenko
377133fd9f5SDenys Vlasenko #endif
3784277eeddSDenis Vlasenko
3791ac101a5SKAMEZAWA Hiroyuki /*
3801ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string.
3811ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0.
3821ac101a5SKAMEZAWA Hiroyuki *
3831ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code.
3841ac101a5SKAMEZAWA Hiroyuki */
num_to_str(char * buf,int size,unsigned long long num,unsigned int width)385d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
3861ac101a5SKAMEZAWA Hiroyuki {
3877c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */
3887c43d9a3SRasmus Villemoes char tmp[sizeof(num) * 3] __aligned(2);
3891ac101a5SKAMEZAWA Hiroyuki int idx, len;
3901ac101a5SKAMEZAWA Hiroyuki
391133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
392133fd9f5SDenys Vlasenko if (num <= 9) {
393133fd9f5SDenys Vlasenko tmp[0] = '0' + num;
394133fd9f5SDenys Vlasenko len = 1;
395133fd9f5SDenys Vlasenko } else {
3961ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp;
397133fd9f5SDenys Vlasenko }
3981ac101a5SKAMEZAWA Hiroyuki
399d1be35cbSAndrei Vagin if (len > size || width > size)
4001ac101a5SKAMEZAWA Hiroyuki return 0;
401d1be35cbSAndrei Vagin
402d1be35cbSAndrei Vagin if (width > len) {
403d1be35cbSAndrei Vagin width = width - len;
404d1be35cbSAndrei Vagin for (idx = 0; idx < width; idx++)
405d1be35cbSAndrei Vagin buf[idx] = ' ';
406d1be35cbSAndrei Vagin } else {
407d1be35cbSAndrei Vagin width = 0;
408d1be35cbSAndrei Vagin }
409d1be35cbSAndrei Vagin
4101ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx)
411d1be35cbSAndrei Vagin buf[idx + width] = tmp[len - idx - 1];
412d1be35cbSAndrei Vagin
413d1be35cbSAndrei Vagin return len + width;
4141ac101a5SKAMEZAWA Hiroyuki }
4151ac101a5SKAMEZAWA Hiroyuki
416be503db4SLinus Torvalds #define SIGN 1 /* unsigned/signed */
417d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */
4181da177e4SLinus Torvalds #define PLUS 4 /* show plus */
4191da177e4SLinus Torvalds #define SPACE 8 /* space if plus */
420d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
421b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
422b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
4231da177e4SLinus Torvalds
424b886690dSAndy Shevchenko static_assert(ZEROPAD == ('0' - ' '));
42524a1dffbSAndy Shevchenko static_assert(SMALL == ('a' ^ 'A'));
426b886690dSAndy Shevchenko
427938df695SLinus Torvalds enum format_state {
428938df695SLinus Torvalds FORMAT_STATE_NONE, /* Just a string part */
4298d4826ccSLinus Torvalds FORMAT_STATE_NUM,
430938df695SLinus Torvalds FORMAT_STATE_WIDTH,
431938df695SLinus Torvalds FORMAT_STATE_PRECISION,
432938df695SLinus Torvalds FORMAT_STATE_CHAR,
433938df695SLinus Torvalds FORMAT_STATE_STR,
434938df695SLinus Torvalds FORMAT_STATE_PTR,
435938df695SLinus Torvalds FORMAT_STATE_PERCENT_CHAR,
436938df695SLinus Torvalds FORMAT_STATE_INVALID,
437fef20d9cSFrederic Weisbecker };
438fef20d9cSFrederic Weisbecker
439fef20d9cSFrederic Weisbecker struct printf_spec {
440938df695SLinus Torvalds unsigned char flags; /* flags to number() */
441938df695SLinus Torvalds unsigned char base; /* number base, 8, 10 or 16 only */
442938df695SLinus Torvalds short precision; /* # of digits/chars */
443938df695SLinus Torvalds int field_width; /* width of output field */
444d0484193SRasmus Villemoes } __packed;
445ef27ac18SRasmus Villemoes static_assert(sizeof(struct printf_spec) == 8);
446ef27ac18SRasmus Villemoes
4474d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1)
4484d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1)
449fef20d9cSFrederic Weisbecker
450cf3b429bSJoe Perches static noinline_for_stack
number(char * buf,char * end,unsigned long long num,struct printf_spec spec)451cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num,
452fef20d9cSFrederic Weisbecker struct printf_spec spec)
4531da177e4SLinus Torvalds {
4547c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */
4557c43d9a3SRasmus Villemoes char tmp[3 * sizeof(num)] __aligned(2);
4569b706aeeSDenys Vlasenko char sign;
4579b706aeeSDenys Vlasenko char locase;
458fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
4591da177e4SLinus Torvalds int i;
4607c203422SPierre Carrier bool is_zero = num == 0LL;
4611c7a8e62SRasmus Villemoes int field_width = spec.field_width;
4621c7a8e62SRasmus Villemoes int precision = spec.precision;
4631da177e4SLinus Torvalds
4649b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase'
4659b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */
466fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL);
467fef20d9cSFrederic Weisbecker if (spec.flags & LEFT)
468fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD;
4691da177e4SLinus Torvalds sign = 0;
470fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) {
4711da177e4SLinus Torvalds if ((signed long long)num < 0) {
4721da177e4SLinus Torvalds sign = '-';
4731da177e4SLinus Torvalds num = -(signed long long)num;
4741c7a8e62SRasmus Villemoes field_width--;
475fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) {
4761da177e4SLinus Torvalds sign = '+';
4771c7a8e62SRasmus Villemoes field_width--;
478fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) {
4791da177e4SLinus Torvalds sign = ' ';
4801c7a8e62SRasmus Villemoes field_width--;
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds }
483b39a7340SDenis Vlasenko if (need_pfx) {
484fef20d9cSFrederic Weisbecker if (spec.base == 16)
4851c7a8e62SRasmus Villemoes field_width -= 2;
4867c203422SPierre Carrier else if (!is_zero)
4871c7a8e62SRasmus Villemoes field_width--;
4881da177e4SLinus Torvalds }
489b39a7340SDenis Vlasenko
490b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */
4911da177e4SLinus Torvalds i = 0;
492133fd9f5SDenys Vlasenko if (num < spec.base)
4933ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase;
494fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */
495fef20d9cSFrederic Weisbecker int mask = spec.base - 1;
496b39a7340SDenis Vlasenko int shift = 3;
4977b9186f5SAndré Goddard Rosa
4987b9186f5SAndré Goddard Rosa if (spec.base == 16)
4997b9186f5SAndré Goddard Rosa shift = 4;
500b39a7340SDenis Vlasenko do {
5013ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
502b39a7340SDenis Vlasenko num >>= shift;
503b39a7340SDenis Vlasenko } while (num);
5044277eeddSDenis Vlasenko } else { /* base 10 */
5054277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp;
5064277eeddSDenis Vlasenko }
507b39a7340SDenis Vlasenko
508b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */
5091c7a8e62SRasmus Villemoes if (i > precision)
5101c7a8e62SRasmus Villemoes precision = i;
511b39a7340SDenis Vlasenko /* leading space padding */
5121c7a8e62SRasmus Villemoes field_width -= precision;
51351be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) {
5141c7a8e62SRasmus Villemoes while (--field_width >= 0) {
515f796937aSJeremy Fitzhardinge if (buf < end)
5161da177e4SLinus Torvalds *buf = ' ';
5171da177e4SLinus Torvalds ++buf;
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds }
520b39a7340SDenis Vlasenko /* sign */
5211da177e4SLinus Torvalds if (sign) {
522f796937aSJeremy Fitzhardinge if (buf < end)
5231da177e4SLinus Torvalds *buf = sign;
5241da177e4SLinus Torvalds ++buf;
5251da177e4SLinus Torvalds }
526b39a7340SDenis Vlasenko /* "0x" / "0" prefix */
527b39a7340SDenis Vlasenko if (need_pfx) {
5287c203422SPierre Carrier if (spec.base == 16 || !is_zero) {
529f796937aSJeremy Fitzhardinge if (buf < end)
5301da177e4SLinus Torvalds *buf = '0';
5311da177e4SLinus Torvalds ++buf;
5327c203422SPierre Carrier }
533fef20d9cSFrederic Weisbecker if (spec.base == 16) {
534f796937aSJeremy Fitzhardinge if (buf < end)
5359b706aeeSDenys Vlasenko *buf = ('X' | locase);
5361da177e4SLinus Torvalds ++buf;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds }
539b39a7340SDenis Vlasenko /* zero or space padding */
540fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) {
541d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD);
542b886690dSAndy Shevchenko
5431c7a8e62SRasmus Villemoes while (--field_width >= 0) {
544f796937aSJeremy Fitzhardinge if (buf < end)
5451da177e4SLinus Torvalds *buf = c;
5461da177e4SLinus Torvalds ++buf;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds }
549b39a7340SDenis Vlasenko /* hmm even more zero padding? */
5501c7a8e62SRasmus Villemoes while (i <= --precision) {
551f796937aSJeremy Fitzhardinge if (buf < end)
5521da177e4SLinus Torvalds *buf = '0';
5531da177e4SLinus Torvalds ++buf;
5541da177e4SLinus Torvalds }
555b39a7340SDenis Vlasenko /* actual digits of result */
556b39a7340SDenis Vlasenko while (--i >= 0) {
557f796937aSJeremy Fitzhardinge if (buf < end)
5581da177e4SLinus Torvalds *buf = tmp[i];
5591da177e4SLinus Torvalds ++buf;
5601da177e4SLinus Torvalds }
561b39a7340SDenis Vlasenko /* trailing space padding */
5621c7a8e62SRasmus Villemoes while (--field_width >= 0) {
563f796937aSJeremy Fitzhardinge if (buf < end)
5641da177e4SLinus Torvalds *buf = ' ';
5651da177e4SLinus Torvalds ++buf;
5661da177e4SLinus Torvalds }
5677b9186f5SAndré Goddard Rosa
5681da177e4SLinus Torvalds return buf;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds
5713cab1e71SAndy Shevchenko static noinline_for_stack
special_hex_number(char * buf,char * end,unsigned long long num,int size)5723cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
5733cab1e71SAndy Shevchenko {
5743cab1e71SAndy Shevchenko struct printf_spec spec;
5753cab1e71SAndy Shevchenko
5763cab1e71SAndy Shevchenko spec.field_width = 2 + 2 * size; /* 0x + hex */
5773cab1e71SAndy Shevchenko spec.flags = SPECIAL | SMALL | ZEROPAD;
5783cab1e71SAndy Shevchenko spec.base = 16;
5793cab1e71SAndy Shevchenko spec.precision = -1;
5803cab1e71SAndy Shevchenko
5813cab1e71SAndy Shevchenko return number(buf, end, num, spec);
5823cab1e71SAndy Shevchenko }
5833cab1e71SAndy Shevchenko
move_right(char * buf,char * end,unsigned len,unsigned spaces)584cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
5854b6ccca7SAl Viro {
5864b6ccca7SAl Viro size_t size;
5874b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */
5884b6ccca7SAl Viro return;
5894b6ccca7SAl Viro size = end - buf;
5904b6ccca7SAl Viro if (size <= spaces) {
5914b6ccca7SAl Viro memset(buf, ' ', size);
5924b6ccca7SAl Viro return;
5934b6ccca7SAl Viro }
5944b6ccca7SAl Viro if (len) {
5954b6ccca7SAl Viro if (len > size - spaces)
5964b6ccca7SAl Viro len = size - spaces;
5974b6ccca7SAl Viro memmove(buf + spaces, buf, len);
5984b6ccca7SAl Viro }
5994b6ccca7SAl Viro memset(buf, ' ', spaces);
6004b6ccca7SAl Viro }
6014b6ccca7SAl Viro
602cfccde04SRasmus Villemoes /*
603cfccde04SRasmus Villemoes * Handle field width padding for a string.
604cfccde04SRasmus Villemoes * @buf: current buffer position
605cfccde04SRasmus Villemoes * @n: length of string
606cfccde04SRasmus Villemoes * @end: end of output buffer
607cfccde04SRasmus Villemoes * @spec: for field width and flags
608cfccde04SRasmus Villemoes * Returns: new buffer position after padding.
609cfccde04SRasmus Villemoes */
610cfccde04SRasmus Villemoes static noinline_for_stack
widen_string(char * buf,int n,char * end,struct printf_spec spec)611cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
612cfccde04SRasmus Villemoes {
613cfccde04SRasmus Villemoes unsigned spaces;
614cfccde04SRasmus Villemoes
615cfccde04SRasmus Villemoes if (likely(n >= spec.field_width))
616cfccde04SRasmus Villemoes return buf;
617cfccde04SRasmus Villemoes /* we want to pad the sucker */
618cfccde04SRasmus Villemoes spaces = spec.field_width - n;
619cfccde04SRasmus Villemoes if (!(spec.flags & LEFT)) {
620cfccde04SRasmus Villemoes move_right(buf - n, end, n, spaces);
621cfccde04SRasmus Villemoes return buf + spaces;
622cfccde04SRasmus Villemoes }
623cfccde04SRasmus Villemoes while (spaces--) {
624cfccde04SRasmus Villemoes if (buf < end)
625cfccde04SRasmus Villemoes *buf = ' ';
626cfccde04SRasmus Villemoes ++buf;
627cfccde04SRasmus Villemoes }
628cfccde04SRasmus Villemoes return buf;
629cfccde04SRasmus Villemoes }
630cfccde04SRasmus Villemoes
631d529ac41SPetr Mladek /* Handle string from a well known address. */
string_nocheck(char * buf,char * end,const char * s,struct printf_spec spec)632d529ac41SPetr Mladek static char *string_nocheck(char *buf, char *end, const char *s,
633d529ac41SPetr Mladek struct printf_spec spec)
63495508cfaSRasmus Villemoes {
63534fc8b90SRasmus Villemoes int len = 0;
636b314dd49SYoungmin Nam int lim = spec.precision;
63795508cfaSRasmus Villemoes
63834fc8b90SRasmus Villemoes while (lim--) {
63934fc8b90SRasmus Villemoes char c = *s++;
64034fc8b90SRasmus Villemoes if (!c)
64134fc8b90SRasmus Villemoes break;
64295508cfaSRasmus Villemoes if (buf < end)
64334fc8b90SRasmus Villemoes *buf = c;
64495508cfaSRasmus Villemoes ++buf;
64534fc8b90SRasmus Villemoes ++len;
64695508cfaSRasmus Villemoes }
64734fc8b90SRasmus Villemoes return widen_string(buf, len, end, spec);
64895508cfaSRasmus Villemoes }
64995508cfaSRasmus Villemoes
err_ptr(char * buf,char * end,void * ptr,struct printf_spec spec)65057f5677eSRasmus Villemoes static char *err_ptr(char *buf, char *end, void *ptr,
65157f5677eSRasmus Villemoes struct printf_spec spec)
65257f5677eSRasmus Villemoes {
65357f5677eSRasmus Villemoes int err = PTR_ERR(ptr);
65457f5677eSRasmus Villemoes const char *sym = errname(err);
65557f5677eSRasmus Villemoes
65657f5677eSRasmus Villemoes if (sym)
65757f5677eSRasmus Villemoes return string_nocheck(buf, end, sym, spec);
65857f5677eSRasmus Villemoes
65957f5677eSRasmus Villemoes /*
66057f5677eSRasmus Villemoes * Somebody passed ERR_PTR(-1234) or some other non-existing
66157f5677eSRasmus Villemoes * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to
66257f5677eSRasmus Villemoes * printing it as its decimal representation.
66357f5677eSRasmus Villemoes */
66457f5677eSRasmus Villemoes spec.flags |= SIGN;
66557f5677eSRasmus Villemoes spec.base = 10;
66657f5677eSRasmus Villemoes return number(buf, end, err, spec);
66757f5677eSRasmus Villemoes }
66857f5677eSRasmus Villemoes
669c8c3b584SPetr Mladek /* Be careful: error messages must fit into the given buffer. */
error_string(char * buf,char * end,const char * s,struct printf_spec spec)670c8c3b584SPetr Mladek static char *error_string(char *buf, char *end, const char *s,
671c8c3b584SPetr Mladek struct printf_spec spec)
672c8c3b584SPetr Mladek {
673c8c3b584SPetr Mladek /*
674c8c3b584SPetr Mladek * Hard limit to avoid a completely insane messages. It actually
675c8c3b584SPetr Mladek * works pretty well because most error messages are in
676c8c3b584SPetr Mladek * the many pointer format modifiers.
677c8c3b584SPetr Mladek */
678c8c3b584SPetr Mladek if (spec.precision == -1)
679c8c3b584SPetr Mladek spec.precision = 2 * sizeof(void *);
680c8c3b584SPetr Mladek
681c8c3b584SPetr Mladek return string_nocheck(buf, end, s, spec);
682c8c3b584SPetr Mladek }
683c8c3b584SPetr Mladek
6843e5903ebSPetr Mladek /*
6852ac5a3bfSPetr Mladek * Do not call any complex external code here. Nested printk()/vsprintf()
6862ac5a3bfSPetr Mladek * might cause infinite loops. Failures might break printk() and would
6872ac5a3bfSPetr Mladek * be hard to debug.
6883e5903ebSPetr Mladek */
check_pointer_msg(const void * ptr)6893e5903ebSPetr Mladek static const char *check_pointer_msg(const void *ptr)
6903e5903ebSPetr Mladek {
6913e5903ebSPetr Mladek if (!ptr)
6923e5903ebSPetr Mladek return "(null)";
6933e5903ebSPetr Mladek
6942ac5a3bfSPetr Mladek if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr))
6953e5903ebSPetr Mladek return "(efault)";
6963e5903ebSPetr Mladek
6973e5903ebSPetr Mladek return NULL;
6983e5903ebSPetr Mladek }
6993e5903ebSPetr Mladek
check_pointer(char ** buf,char * end,const void * ptr,struct printf_spec spec)7003e5903ebSPetr Mladek static int check_pointer(char **buf, char *end, const void *ptr,
7013e5903ebSPetr Mladek struct printf_spec spec)
7023e5903ebSPetr Mladek {
7033e5903ebSPetr Mladek const char *err_msg;
7043e5903ebSPetr Mladek
7053e5903ebSPetr Mladek err_msg = check_pointer_msg(ptr);
7063e5903ebSPetr Mladek if (err_msg) {
707c8c3b584SPetr Mladek *buf = error_string(*buf, end, err_msg, spec);
7083e5903ebSPetr Mladek return -EFAULT;
7093e5903ebSPetr Mladek }
7103e5903ebSPetr Mladek
7113e5903ebSPetr Mladek return 0;
7123e5903ebSPetr Mladek }
7133e5903ebSPetr Mladek
71495508cfaSRasmus Villemoes static noinline_for_stack
string(char * buf,char * end,const char * s,struct printf_spec spec)715d529ac41SPetr Mladek char *string(char *buf, char *end, const char *s,
716d529ac41SPetr Mladek struct printf_spec spec)
717d529ac41SPetr Mladek {
7183e5903ebSPetr Mladek if (check_pointer(&buf, end, s, spec))
7193e5903ebSPetr Mladek return buf;
720d529ac41SPetr Mladek
721d529ac41SPetr Mladek return string_nocheck(buf, end, s, spec);
722d529ac41SPetr Mladek }
723d529ac41SPetr Mladek
pointer_string(char * buf,char * end,const void * ptr,struct printf_spec spec)724ce9d3eceSYueHaibing static char *pointer_string(char *buf, char *end,
725ce9d3eceSYueHaibing const void *ptr,
7269073dac1SGeert Uytterhoeven struct printf_spec spec)
7279073dac1SGeert Uytterhoeven {
7289073dac1SGeert Uytterhoeven spec.base = 16;
7299073dac1SGeert Uytterhoeven spec.flags |= SMALL;
7309073dac1SGeert Uytterhoeven if (spec.field_width == -1) {
7319073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr);
7329073dac1SGeert Uytterhoeven spec.flags |= ZEROPAD;
7339073dac1SGeert Uytterhoeven }
7349073dac1SGeert Uytterhoeven
7359073dac1SGeert Uytterhoeven return number(buf, end, (unsigned long int)ptr, spec);
7369073dac1SGeert Uytterhoeven }
7379073dac1SGeert Uytterhoeven
7389073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */
7399073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init;
7409073dac1SGeert Uytterhoeven
debug_boot_weak_hash_enable(char * str)7419073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str)
7429073dac1SGeert Uytterhoeven {
7439073dac1SGeert Uytterhoeven debug_boot_weak_hash = 1;
7449073dac1SGeert Uytterhoeven pr_info("debug_boot_weak_hash enabled\n");
7459073dac1SGeert Uytterhoeven return 0;
7469073dac1SGeert Uytterhoeven }
7479073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable);
7489073dac1SGeert Uytterhoeven
749e4279b59SSebastian Andrzej Siewior static bool filled_random_ptr_key __read_mostly;
7506f0ac3b5SSebastian Andrzej Siewior static siphash_key_t ptr_key __read_mostly;
7519073dac1SGeert Uytterhoeven
fill_ptr_key(struct notifier_block * nb,unsigned long action,void * data)752898f1e5cSJason A. Donenfeld static int fill_ptr_key(struct notifier_block *nb, unsigned long action, void *data)
7539073dac1SGeert Uytterhoeven {
7546f0ac3b5SSebastian Andrzej Siewior get_random_bytes(&ptr_key, sizeof(ptr_key));
7556f0ac3b5SSebastian Andrzej Siewior
7566f0ac3b5SSebastian Andrzej Siewior /* Pairs with smp_rmb() before reading ptr_key. */
7576f0ac3b5SSebastian Andrzej Siewior smp_wmb();
7586f0ac3b5SSebastian Andrzej Siewior WRITE_ONCE(filled_random_ptr_key, true);
759898f1e5cSJason A. Donenfeld return NOTIFY_DONE;
7606f0ac3b5SSebastian Andrzej Siewior }
7616f0ac3b5SSebastian Andrzej Siewior
vsprintf_init_hashval(void)7626f0ac3b5SSebastian Andrzej Siewior static int __init vsprintf_init_hashval(void)
7636f0ac3b5SSebastian Andrzej Siewior {
764898f1e5cSJason A. Donenfeld static struct notifier_block fill_ptr_key_nb = { .notifier_call = fill_ptr_key };
765898f1e5cSJason A. Donenfeld execute_with_initialized_rng(&fill_ptr_key_nb);
7666f0ac3b5SSebastian Andrzej Siewior return 0;
7676f0ac3b5SSebastian Andrzej Siewior }
subsys_initcall(vsprintf_init_hashval)7686f0ac3b5SSebastian Andrzej Siewior subsys_initcall(vsprintf_init_hashval)
7699073dac1SGeert Uytterhoeven
7709073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */
771e4dcad20SJoel Fernandes (Google) static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
7729073dac1SGeert Uytterhoeven {
7739073dac1SGeert Uytterhoeven unsigned long hashval;
7749073dac1SGeert Uytterhoeven
7756f0ac3b5SSebastian Andrzej Siewior if (!READ_ONCE(filled_random_ptr_key))
7766f0ac3b5SSebastian Andrzej Siewior return -EBUSY;
7776701de6cSJason A. Donenfeld
778e4279b59SSebastian Andrzej Siewior /* Pairs with smp_wmb() after writing ptr_key. */
779e4279b59SSebastian Andrzej Siewior smp_rmb();
7806701de6cSJason A. Donenfeld
7819073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT
7829073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key);
7839073dac1SGeert Uytterhoeven /*
7849073dac1SGeert Uytterhoeven * Mask off the first 32 bits, this makes explicit that we have
7859073dac1SGeert Uytterhoeven * modified the address (and 32 bits is plenty for a unique ID).
7869073dac1SGeert Uytterhoeven */
7879073dac1SGeert Uytterhoeven hashval = hashval & 0xffffffff;
7889073dac1SGeert Uytterhoeven #else
7899073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key);
7909073dac1SGeert Uytterhoeven #endif
791e4dcad20SJoel Fernandes (Google) *hashval_out = hashval;
792e4dcad20SJoel Fernandes (Google) return 0;
793e4dcad20SJoel Fernandes (Google) }
794e4dcad20SJoel Fernandes (Google)
ptr_to_hashval(const void * ptr,unsigned long * hashval_out)795e4dcad20SJoel Fernandes (Google) int ptr_to_hashval(const void *ptr, unsigned long *hashval_out)
796e4dcad20SJoel Fernandes (Google) {
797e4dcad20SJoel Fernandes (Google) return __ptr_to_hashval(ptr, hashval_out);
798e4dcad20SJoel Fernandes (Google) }
799e4dcad20SJoel Fernandes (Google)
ptr_to_id(char * buf,char * end,const void * ptr,struct printf_spec spec)800e4dcad20SJoel Fernandes (Google) static char *ptr_to_id(char *buf, char *end, const void *ptr,
801e4dcad20SJoel Fernandes (Google) struct printf_spec spec)
802e4dcad20SJoel Fernandes (Google) {
803e4dcad20SJoel Fernandes (Google) const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)";
804e4dcad20SJoel Fernandes (Google) unsigned long hashval;
805e4dcad20SJoel Fernandes (Google) int ret;
806e4dcad20SJoel Fernandes (Google)
8077bd57fbcSIlya Dryomov /*
8087bd57fbcSIlya Dryomov * Print the real pointer value for NULL and error pointers,
8097bd57fbcSIlya Dryomov * as they are not actual addresses.
8107bd57fbcSIlya Dryomov */
8117bd57fbcSIlya Dryomov if (IS_ERR_OR_NULL(ptr))
8127bd57fbcSIlya Dryomov return pointer_string(buf, end, ptr, spec);
8137bd57fbcSIlya Dryomov
814e4dcad20SJoel Fernandes (Google) /* When debugging early boot use non-cryptographically secure hash. */
815e4dcad20SJoel Fernandes (Google) if (unlikely(debug_boot_weak_hash)) {
816e4dcad20SJoel Fernandes (Google) hashval = hash_long((unsigned long)ptr, 32);
817e4dcad20SJoel Fernandes (Google) return pointer_string(buf, end, (const void *)hashval, spec);
818e4dcad20SJoel Fernandes (Google) }
819e4dcad20SJoel Fernandes (Google)
820e4dcad20SJoel Fernandes (Google) ret = __ptr_to_hashval(ptr, &hashval);
821e4dcad20SJoel Fernandes (Google) if (ret) {
822e4dcad20SJoel Fernandes (Google) spec.field_width = 2 * sizeof(ptr);
823e4dcad20SJoel Fernandes (Google) /* string length must be less than default_width */
824e4dcad20SJoel Fernandes (Google) return error_string(buf, end, str, spec);
825e4dcad20SJoel Fernandes (Google) }
826e4dcad20SJoel Fernandes (Google)
8279073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec);
8289073dac1SGeert Uytterhoeven }
8299073dac1SGeert Uytterhoeven
default_pointer(char * buf,char * end,const void * ptr,struct printf_spec spec)83084842911SChristophe Leroy static char *default_pointer(char *buf, char *end, const void *ptr,
83184842911SChristophe Leroy struct printf_spec spec)
83284842911SChristophe Leroy {
83384842911SChristophe Leroy /*
83484842911SChristophe Leroy * default is to _not_ leak addresses, so hash before printing,
83584842911SChristophe Leroy * unless no_hash_pointers is specified on the command line.
83684842911SChristophe Leroy */
83784842911SChristophe Leroy if (unlikely(no_hash_pointers))
83884842911SChristophe Leroy return pointer_string(buf, end, ptr, spec);
83984842911SChristophe Leroy
84084842911SChristophe Leroy return ptr_to_id(buf, end, ptr, spec);
84184842911SChristophe Leroy }
84284842911SChristophe Leroy
8436eea242fSPetr Mladek int kptr_restrict __read_mostly;
8446eea242fSPetr Mladek
8456eea242fSPetr Mladek static noinline_for_stack
restricted_pointer(char * buf,char * end,const void * ptr,struct printf_spec spec)8466eea242fSPetr Mladek char *restricted_pointer(char *buf, char *end, const void *ptr,
8476eea242fSPetr Mladek struct printf_spec spec)
8486eea242fSPetr Mladek {
8496eea242fSPetr Mladek switch (kptr_restrict) {
8506eea242fSPetr Mladek case 0:
8511ac2f978SPetr Mladek /* Handle as %p, hash and do _not_ leak addresses. */
85284842911SChristophe Leroy return default_pointer(buf, end, ptr, spec);
8536eea242fSPetr Mladek case 1: {
8546eea242fSPetr Mladek const struct cred *cred;
8556eea242fSPetr Mladek
8566eea242fSPetr Mladek /*
8576eea242fSPetr Mladek * kptr_restrict==1 cannot be used in IRQ context
8586eea242fSPetr Mladek * because its test for CAP_SYSLOG would be meaningless.
8596eea242fSPetr Mladek */
86066283a8fSye xingchen if (in_hardirq() || in_serving_softirq() || in_nmi()) {
8616eea242fSPetr Mladek if (spec.field_width == -1)
8626eea242fSPetr Mladek spec.field_width = 2 * sizeof(ptr);
863c8c3b584SPetr Mladek return error_string(buf, end, "pK-error", spec);
8646eea242fSPetr Mladek }
8656eea242fSPetr Mladek
8666eea242fSPetr Mladek /*
8676eea242fSPetr Mladek * Only print the real pointer value if the current
8686eea242fSPetr Mladek * process has CAP_SYSLOG and is running with the
8696eea242fSPetr Mladek * same credentials it started with. This is because
8706eea242fSPetr Mladek * access to files is checked at open() time, but %pK
8716eea242fSPetr Mladek * checks permission at read() time. We don't want to
8726eea242fSPetr Mladek * leak pointer values if a binary opens a file using
8736eea242fSPetr Mladek * %pK and then elevates privileges before reading it.
8746eea242fSPetr Mladek */
8756eea242fSPetr Mladek cred = current_cred();
8766eea242fSPetr Mladek if (!has_capability_noaudit(current, CAP_SYSLOG) ||
8776eea242fSPetr Mladek !uid_eq(cred->euid, cred->uid) ||
8786eea242fSPetr Mladek !gid_eq(cred->egid, cred->gid))
8796eea242fSPetr Mladek ptr = NULL;
8806eea242fSPetr Mladek break;
8816eea242fSPetr Mladek }
8826eea242fSPetr Mladek case 2:
8836eea242fSPetr Mladek default:
8846eea242fSPetr Mladek /* Always print 0's for %pK */
8856eea242fSPetr Mladek ptr = NULL;
8866eea242fSPetr Mladek break;
8876eea242fSPetr Mladek }
8886eea242fSPetr Mladek
8896eea242fSPetr Mladek return pointer_string(buf, end, ptr, spec);
8906eea242fSPetr Mladek }
8916eea242fSPetr Mladek
8929073dac1SGeert Uytterhoeven static noinline_for_stack
dentry_name(char * buf,char * end,const struct dentry * d,struct printf_spec spec,const char * fmt)8934b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
8944b6ccca7SAl Viro const char *fmt)
8954b6ccca7SAl Viro {
8964b6ccca7SAl Viro const char *array[4], *s;
8974b6ccca7SAl Viro const struct dentry *p;
8984b6ccca7SAl Viro int depth;
8994b6ccca7SAl Viro int i, n;
9004b6ccca7SAl Viro
9014b6ccca7SAl Viro switch (fmt[1]) {
9024b6ccca7SAl Viro case '2': case '3': case '4':
9034b6ccca7SAl Viro depth = fmt[1] - '0';
9044b6ccca7SAl Viro break;
9054b6ccca7SAl Viro default:
9064b6ccca7SAl Viro depth = 1;
9074b6ccca7SAl Viro }
9084b6ccca7SAl Viro
9094b6ccca7SAl Viro rcu_read_lock();
9104b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) {
9113e5903ebSPetr Mladek if (check_pointer(&buf, end, d, spec)) {
9123e5903ebSPetr Mladek rcu_read_unlock();
9133e5903ebSPetr Mladek return buf;
9143e5903ebSPetr Mladek }
9153e5903ebSPetr Mladek
9166aa7de05SMark Rutland p = READ_ONCE(d->d_parent);
9176aa7de05SMark Rutland array[i] = READ_ONCE(d->d_name.name);
9184b6ccca7SAl Viro if (p == d) {
9194b6ccca7SAl Viro if (i)
9204b6ccca7SAl Viro array[i] = "";
9214b6ccca7SAl Viro i++;
9224b6ccca7SAl Viro break;
9234b6ccca7SAl Viro }
9244b6ccca7SAl Viro }
9254b6ccca7SAl Viro s = array[--i];
9264b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) {
9274b6ccca7SAl Viro char c = *s++;
9284b6ccca7SAl Viro if (!c) {
9294b6ccca7SAl Viro if (!i)
9304b6ccca7SAl Viro break;
9314b6ccca7SAl Viro c = '/';
9324b6ccca7SAl Viro s = array[--i];
9334b6ccca7SAl Viro }
9344b6ccca7SAl Viro if (buf < end)
9354b6ccca7SAl Viro *buf = c;
9364b6ccca7SAl Viro }
9374b6ccca7SAl Viro rcu_read_unlock();
938cfccde04SRasmus Villemoes return widen_string(buf, n, end, spec);
9394b6ccca7SAl Viro }
9404b6ccca7SAl Viro
94136594b31SJia He static noinline_for_stack
file_dentry_name(char * buf,char * end,const struct file * f,struct printf_spec spec,const char * fmt)94236594b31SJia He char *file_dentry_name(char *buf, char *end, const struct file *f,
94336594b31SJia He struct printf_spec spec, const char *fmt)
94436594b31SJia He {
94536594b31SJia He if (check_pointer(&buf, end, f, spec))
94636594b31SJia He return buf;
94736594b31SJia He
94836594b31SJia He return dentry_name(buf, end, f->f_path.dentry, spec, fmt);
94936594b31SJia He }
9501031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK
9511031bc58SDmitry Monakhov static noinline_for_stack
bdev_name(char * buf,char * end,struct block_device * bdev,struct printf_spec spec,const char * fmt)9521031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev,
9531031bc58SDmitry Monakhov struct printf_spec spec, const char *fmt)
9541031bc58SDmitry Monakhov {
9553e5903ebSPetr Mladek struct gendisk *hd;
9561031bc58SDmitry Monakhov
9573e5903ebSPetr Mladek if (check_pointer(&buf, end, bdev, spec))
9583e5903ebSPetr Mladek return buf;
9593e5903ebSPetr Mladek
9603e5903ebSPetr Mladek hd = bdev->bd_disk;
9611031bc58SDmitry Monakhov buf = string(buf, end, hd->disk_name, spec);
9623f9b8fb4SAl Viro if (bdev_is_partition(bdev)) {
9631031bc58SDmitry Monakhov if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) {
9641031bc58SDmitry Monakhov if (buf < end)
9651031bc58SDmitry Monakhov *buf = 'p';
9661031bc58SDmitry Monakhov buf++;
9671031bc58SDmitry Monakhov }
968b8c873edSAl Viro buf = number(buf, end, bdev_partno(bdev), spec);
9691031bc58SDmitry Monakhov }
9701031bc58SDmitry Monakhov return buf;
9711031bc58SDmitry Monakhov }
9721031bc58SDmitry Monakhov #endif
9731031bc58SDmitry Monakhov
974cf3b429bSJoe Perches static noinline_for_stack
symbol_string(char * buf,char * end,void * ptr,struct printf_spec spec,const char * fmt)975cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr,
976b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt)
9770fe1ef24SLinus Torvalds {
978b0d33c2bSJoe Perches unsigned long value;
9790fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS
9800fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN];
981b0d33c2bSJoe Perches #endif
982b0d33c2bSJoe Perches
983b0d33c2bSJoe Perches if (fmt[1] == 'R')
984b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr);
985b0d33c2bSJoe Perches value = (unsigned long)ptr;
986b0d33c2bSJoe Perches
987b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS
9889294523eSStephen Boyd if (*fmt == 'B' && fmt[1] == 'b')
9899294523eSStephen Boyd sprint_backtrace_build_id(sym, value);
9909294523eSStephen Boyd else if (*fmt == 'B')
9910f77a8d3SNamhyung Kim sprint_backtrace(sym, value);
9929294523eSStephen Boyd else if (*fmt == 'S' && (fmt[1] == 'b' || (fmt[1] == 'R' && fmt[2] == 'b')))
9939294523eSStephen Boyd sprint_symbol_build_id(sym, value);
9949af77064SSakari Ailus else if (*fmt != 's')
9950fe1ef24SLinus Torvalds sprint_symbol(sym, value);
9960c8b946eSFrederic Weisbecker else
9974796dd20SStephen Boyd sprint_symbol_no_offset(sym, value);
9987b9186f5SAndré Goddard Rosa
999d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec);
10000fe1ef24SLinus Torvalds #else
10013cab1e71SAndy Shevchenko return special_hex_number(buf, end, value, sizeof(void *));
10020fe1ef24SLinus Torvalds #endif
10030fe1ef24SLinus Torvalds }
10040fe1ef24SLinus Torvalds
1005abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = {
1006abd4fe62SAndy Shevchenko .field_width = -1,
1007abd4fe62SAndy Shevchenko .precision = -1,
1008abd4fe62SAndy Shevchenko };
1009abd4fe62SAndy Shevchenko
101054433973SAndy Shevchenko static const struct printf_spec default_flag_spec = {
101154433973SAndy Shevchenko .base = 16,
101254433973SAndy Shevchenko .precision = -1,
101354433973SAndy Shevchenko .flags = SPECIAL | SMALL,
101454433973SAndy Shevchenko };
101554433973SAndy Shevchenko
1016ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = {
1017ce0b4910SAndy Shevchenko .base = 10,
1018ce0b4910SAndy Shevchenko .precision = -1,
1019ce0b4910SAndy Shevchenko };
1020ce0b4910SAndy Shevchenko
10214d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = {
10224d42c447SAndy Shevchenko .base = 10,
10234d42c447SAndy Shevchenko .field_width = 2,
10244d42c447SAndy Shevchenko .precision = -1,
10254d42c447SAndy Shevchenko .flags = ZEROPAD,
10264d42c447SAndy Shevchenko };
10274d42c447SAndy Shevchenko
10284d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = {
10294d42c447SAndy Shevchenko .base = 10,
10304d42c447SAndy Shevchenko .field_width = 4,
10314d42c447SAndy Shevchenko .precision = -1,
10324d42c447SAndy Shevchenko .flags = ZEROPAD,
10334d42c447SAndy Shevchenko };
10344d42c447SAndy Shevchenko
1035cf3b429bSJoe Perches static noinline_for_stack
hex_range(char * buf,char * end,u64 start_val,u64 end_val,struct printf_spec spec)103642619747SIra Weiny char *hex_range(char *buf, char *end, u64 start_val, u64 end_val,
103742619747SIra Weiny struct printf_spec spec)
103842619747SIra Weiny {
103942619747SIra Weiny buf = number(buf, end, start_val, spec);
104042619747SIra Weiny if (start_val == end_val)
104142619747SIra Weiny return buf;
104242619747SIra Weiny
104342619747SIra Weiny if (buf < end)
104442619747SIra Weiny *buf = '-';
104542619747SIra Weiny ++buf;
104642619747SIra Weiny return number(buf, end, end_val, spec);
104742619747SIra Weiny }
104842619747SIra Weiny
104942619747SIra Weiny static noinline_for_stack
resource_string(char * buf,char * end,struct resource * res,struct printf_spec spec,const char * fmt)1050cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res,
1051fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt)
1052332d2e78SLinus Torvalds {
1053332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE
105428405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6
1055332d2e78SLinus Torvalds #endif
1056332d2e78SLinus Torvalds
1057332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE
105828405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10
1059332d2e78SLinus Torvalds #endif
10604da0b66cSBjorn Helgaas static const struct printf_spec io_spec = {
1061fef20d9cSFrederic Weisbecker .base = 16,
10624da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE,
1063fef20d9cSFrederic Weisbecker .precision = -1,
1064fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD,
1065fef20d9cSFrederic Weisbecker };
10664da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = {
10674da0b66cSBjorn Helgaas .base = 16,
10684da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE,
10694da0b66cSBjorn Helgaas .precision = -1,
10704da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD,
10714da0b66cSBjorn Helgaas };
10720f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = {
10730f4050c7SBjorn Helgaas .base = 16,
10740f4050c7SBjorn Helgaas .field_width = 2,
10750f4050c7SBjorn Helgaas .precision = -1,
10760f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD,
10770f4050c7SBjorn Helgaas };
10784da0b66cSBjorn Helgaas static const struct printf_spec str_spec = {
1079fd95541eSBjorn Helgaas .field_width = -1,
1080fd95541eSBjorn Helgaas .precision = 10,
1081fd95541eSBjorn Helgaas .flags = LEFT,
1082fd95541eSBjorn Helgaas };
1083c7dabef8SBjorn Helgaas
1084c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
1085c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
1086c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
1087c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
10889d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
1089c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
1090cb04e8b1SLinus Torvalds char sym[MAX(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
1091c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
1092c7dabef8SBjorn Helgaas
1093332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym);
1094c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0;
10954da0b66cSBjorn Helgaas const struct printf_spec *specp;
1096332d2e78SLinus Torvalds
10973e5903ebSPetr Mladek if (check_pointer(&buf, end, res, spec))
10983e5903ebSPetr Mladek return buf;
10993e5903ebSPetr Mladek
1100332d2e78SLinus Torvalds *p++ = '[';
11014da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) {
1102d529ac41SPetr Mladek p = string_nocheck(p, pend, "io ", str_spec);
11034da0b66cSBjorn Helgaas specp = &io_spec;
11044da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) {
1105d529ac41SPetr Mladek p = string_nocheck(p, pend, "mem ", str_spec);
11064da0b66cSBjorn Helgaas specp = &mem_spec;
11074da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) {
1108d529ac41SPetr Mladek p = string_nocheck(p, pend, "irq ", str_spec);
1109ce0b4910SAndy Shevchenko specp = &default_dec_spec;
11104da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) {
1111d529ac41SPetr Mladek p = string_nocheck(p, pend, "dma ", str_spec);
1112ce0b4910SAndy Shevchenko specp = &default_dec_spec;
11130f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) {
1114d529ac41SPetr Mladek p = string_nocheck(p, pend, "bus ", str_spec);
11150f4050c7SBjorn Helgaas specp = &bus_spec;
11164da0b66cSBjorn Helgaas } else {
1117d529ac41SPetr Mladek p = string_nocheck(p, pend, "??? ", str_spec);
11184da0b66cSBjorn Helgaas specp = &mem_spec;
1119c7dabef8SBjorn Helgaas decode = 0;
1120fd95541eSBjorn Helgaas }
1121d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) {
1122d529ac41SPetr Mladek p = string_nocheck(p, pend, "size ", str_spec);
1123d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp);
1124d19cb803SBjorn Helgaas } else {
112542619747SIra Weiny p = hex_range(p, pend, res->start, res->end, *specp);
1126d19cb803SBjorn Helgaas }
1127c7dabef8SBjorn Helgaas if (decode) {
1128fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64)
1129d529ac41SPetr Mladek p = string_nocheck(p, pend, " 64bit", str_spec);
1130fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH)
1131d529ac41SPetr Mladek p = string_nocheck(p, pend, " pref", str_spec);
11329d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW)
1133d529ac41SPetr Mladek p = string_nocheck(p, pend, " window", str_spec);
1134fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED)
1135d529ac41SPetr Mladek p = string_nocheck(p, pend, " disabled", str_spec);
1136c7dabef8SBjorn Helgaas } else {
1137d529ac41SPetr Mladek p = string_nocheck(p, pend, " flags ", str_spec);
113854433973SAndy Shevchenko p = number(p, pend, res->flags, default_flag_spec);
1139fd95541eSBjorn Helgaas }
1140332d2e78SLinus Torvalds *p++ = ']';
1141c7dabef8SBjorn Helgaas *p = '\0';
1142332d2e78SLinus Torvalds
1143d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec);
1144332d2e78SLinus Torvalds }
1145332d2e78SLinus Torvalds
1146cf3b429bSJoe Perches static noinline_for_stack
range_string(char * buf,char * end,const struct range * range,struct printf_spec spec,const char * fmt)114742619747SIra Weiny char *range_string(char *buf, char *end, const struct range *range,
114842619747SIra Weiny struct printf_spec spec, const char *fmt)
114942619747SIra Weiny {
115042619747SIra Weiny char sym[sizeof("[range 0x0123456789abcdef-0x0123456789abcdef]")];
115142619747SIra Weiny char *p = sym, *pend = sym + sizeof(sym);
115242619747SIra Weiny
115342619747SIra Weiny struct printf_spec range_spec = {
115442619747SIra Weiny .field_width = 2 + 2 * sizeof(range->start), /* 0x + 2 * 8 */
115542619747SIra Weiny .flags = SPECIAL | SMALL | ZEROPAD,
115642619747SIra Weiny .base = 16,
115742619747SIra Weiny .precision = -1,
115842619747SIra Weiny };
115942619747SIra Weiny
116042619747SIra Weiny if (check_pointer(&buf, end, range, spec))
116142619747SIra Weiny return buf;
116242619747SIra Weiny
116342619747SIra Weiny p = string_nocheck(p, pend, "[range ", default_str_spec);
116442619747SIra Weiny p = hex_range(p, pend, range->start, range->end, range_spec);
116542619747SIra Weiny *p++ = ']';
116642619747SIra Weiny *p = '\0';
116742619747SIra Weiny
116842619747SIra Weiny return string_nocheck(buf, end, sym, spec);
116942619747SIra Weiny }
117042619747SIra Weiny
117142619747SIra Weiny static noinline_for_stack
hex_string(char * buf,char * end,u8 * addr,struct printf_spec spec,const char * fmt)117231550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
117331550a16SAndy Shevchenko const char *fmt)
117431550a16SAndy Shevchenko {
1175360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains
117631550a16SAndy Shevchenko negative value, fallback to the default */
117731550a16SAndy Shevchenko char separator;
117831550a16SAndy Shevchenko
117931550a16SAndy Shevchenko if (spec.field_width == 0)
118031550a16SAndy Shevchenko /* nothing to print */
118131550a16SAndy Shevchenko return buf;
118231550a16SAndy Shevchenko
11833e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec))
11843e5903ebSPetr Mladek return buf;
118531550a16SAndy Shevchenko
118631550a16SAndy Shevchenko switch (fmt[1]) {
118731550a16SAndy Shevchenko case 'C':
118831550a16SAndy Shevchenko separator = ':';
118931550a16SAndy Shevchenko break;
119031550a16SAndy Shevchenko case 'D':
119131550a16SAndy Shevchenko separator = '-';
119231550a16SAndy Shevchenko break;
119331550a16SAndy Shevchenko case 'N':
119431550a16SAndy Shevchenko separator = 0;
119531550a16SAndy Shevchenko break;
119631550a16SAndy Shevchenko default:
119731550a16SAndy Shevchenko separator = ' ';
119831550a16SAndy Shevchenko break;
119931550a16SAndy Shevchenko }
120031550a16SAndy Shevchenko
120131550a16SAndy Shevchenko if (spec.field_width > 0)
120231550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64);
120331550a16SAndy Shevchenko
12049c98f235SRasmus Villemoes for (i = 0; i < len; ++i) {
12059c98f235SRasmus Villemoes if (buf < end)
12069c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]);
12079c98f235SRasmus Villemoes ++buf;
12089c98f235SRasmus Villemoes if (buf < end)
12099c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]);
12109c98f235SRasmus Villemoes ++buf;
121131550a16SAndy Shevchenko
12129c98f235SRasmus Villemoes if (separator && i != len - 1) {
12139c98f235SRasmus Villemoes if (buf < end)
12149c98f235SRasmus Villemoes *buf = separator;
12159c98f235SRasmus Villemoes ++buf;
12169c98f235SRasmus Villemoes }
121731550a16SAndy Shevchenko }
121831550a16SAndy Shevchenko
121931550a16SAndy Shevchenko return buf;
122031550a16SAndy Shevchenko }
122131550a16SAndy Shevchenko
122231550a16SAndy Shevchenko static noinline_for_stack
bitmap_string(char * buf,char * end,const unsigned long * bitmap,struct printf_spec spec,const char * fmt)1223dc453dd8SJian Shen char *bitmap_string(char *buf, char *end, const unsigned long *bitmap,
1224dbc760bcSTejun Heo struct printf_spec spec, const char *fmt)
1225dbc760bcSTejun Heo {
1226dbc760bcSTejun Heo const int CHUNKSZ = 32;
1227dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0);
1228dbc760bcSTejun Heo int i, chunksz;
1229dbc760bcSTejun Heo bool first = true;
1230dbc760bcSTejun Heo
12313e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec))
12323e5903ebSPetr Mladek return buf;
12333e5903ebSPetr Mladek
1234dbc760bcSTejun Heo /* reused to print numbers */
1235dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
1236dbc760bcSTejun Heo
1237dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1);
1238dbc760bcSTejun Heo if (chunksz == 0)
1239dbc760bcSTejun Heo chunksz = CHUNKSZ;
1240dbc760bcSTejun Heo
1241dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
1242dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) {
1243dbc760bcSTejun Heo u32 chunkmask, val;
1244dbc760bcSTejun Heo int word, bit;
1245dbc760bcSTejun Heo
1246dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1);
1247dbc760bcSTejun Heo word = i / BITS_PER_LONG;
1248dbc760bcSTejun Heo bit = i % BITS_PER_LONG;
1249dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask;
1250dbc760bcSTejun Heo
1251dbc760bcSTejun Heo if (!first) {
1252dbc760bcSTejun Heo if (buf < end)
1253dbc760bcSTejun Heo *buf = ',';
1254dbc760bcSTejun Heo buf++;
1255dbc760bcSTejun Heo }
1256dbc760bcSTejun Heo first = false;
1257dbc760bcSTejun Heo
1258dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4);
1259dbc760bcSTejun Heo buf = number(buf, end, val, spec);
1260dbc760bcSTejun Heo
1261dbc760bcSTejun Heo chunksz = CHUNKSZ;
1262dbc760bcSTejun Heo }
1263dbc760bcSTejun Heo return buf;
1264dbc760bcSTejun Heo }
1265dbc760bcSTejun Heo
1266dbc760bcSTejun Heo static noinline_for_stack
bitmap_list_string(char * buf,char * end,const unsigned long * bitmap,struct printf_spec spec,const char * fmt)1267dc453dd8SJian Shen char *bitmap_list_string(char *buf, char *end, const unsigned long *bitmap,
1268dbc760bcSTejun Heo struct printf_spec spec, const char *fmt)
1269dbc760bcSTejun Heo {
1270dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0);
1271dbc760bcSTejun Heo bool first = true;
127215325b4fSYury Norov int rbot, rtop;
1273dbc760bcSTejun Heo
12743e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec))
12753e5903ebSPetr Mladek return buf;
12763e5903ebSPetr Mladek
127715325b4fSYury Norov for_each_set_bitrange(rbot, rtop, bitmap, nr_bits) {
1278dbc760bcSTejun Heo if (!first) {
1279dbc760bcSTejun Heo if (buf < end)
1280dbc760bcSTejun Heo *buf = ',';
1281dbc760bcSTejun Heo buf++;
1282dbc760bcSTejun Heo }
1283dbc760bcSTejun Heo first = false;
1284dbc760bcSTejun Heo
1285ce0b4910SAndy Shevchenko buf = number(buf, end, rbot, default_dec_spec);
128615325b4fSYury Norov if (rtop == rbot + 1)
128715325b4fSYury Norov continue;
128815325b4fSYury Norov
1289dbc760bcSTejun Heo if (buf < end)
1290dbc760bcSTejun Heo *buf = '-';
129115325b4fSYury Norov buf = number(++buf, end, rtop - 1, default_dec_spec);
1292dbc760bcSTejun Heo }
1293dbc760bcSTejun Heo return buf;
1294dbc760bcSTejun Heo }
1295dbc760bcSTejun Heo
1296dbc760bcSTejun Heo static noinline_for_stack
mac_address_string(char * buf,char * end,u8 * addr,struct printf_spec spec,const char * fmt)1297cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr,
12988a27f7c9SJoe Perches struct printf_spec spec, const char *fmt)
1299dd45c9cfSHarvey Harrison {
13008a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
1301dd45c9cfSHarvey Harrison char *p = mac_addr;
1302dd45c9cfSHarvey Harrison int i;
1303bc7259a2SJoe Perches char separator;
130476597ff9SAndrei Emeltchenko bool reversed = false;
1305bc7259a2SJoe Perches
13063e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec))
13073e5903ebSPetr Mladek return buf;
13083e5903ebSPetr Mladek
130976597ff9SAndrei Emeltchenko switch (fmt[1]) {
131076597ff9SAndrei Emeltchenko case 'F':
1311bc7259a2SJoe Perches separator = '-';
131276597ff9SAndrei Emeltchenko break;
131376597ff9SAndrei Emeltchenko
131476597ff9SAndrei Emeltchenko case 'R':
131576597ff9SAndrei Emeltchenko reversed = true;
13164c1ca831SNick Desaulniers fallthrough;
131776597ff9SAndrei Emeltchenko
131876597ff9SAndrei Emeltchenko default:
1319bc7259a2SJoe Perches separator = ':';
132076597ff9SAndrei Emeltchenko break;
1321bc7259a2SJoe Perches }
1322dd45c9cfSHarvey Harrison
1323dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) {
132476597ff9SAndrei Emeltchenko if (reversed)
132576597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]);
132676597ff9SAndrei Emeltchenko else
132755036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]);
132876597ff9SAndrei Emeltchenko
13298a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5)
1330bc7259a2SJoe Perches *p++ = separator;
1331dd45c9cfSHarvey Harrison }
1332dd45c9cfSHarvey Harrison *p = '\0';
1333dd45c9cfSHarvey Harrison
1334d529ac41SPetr Mladek return string_nocheck(buf, end, mac_addr, spec);
1335dd45c9cfSHarvey Harrison }
1336dd45c9cfSHarvey Harrison
1337cf3b429bSJoe Perches static noinline_for_stack
ip4_string(char * p,const u8 * addr,const char * fmt)1338cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt)
1339689afa7dSHarvey Harrison {
1340689afa7dSHarvey Harrison int i;
13410159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i');
13420159f24eSJoe Perches int index;
13430159f24eSJoe Perches int step;
1344689afa7dSHarvey Harrison
13450159f24eSJoe Perches switch (fmt[2]) {
13460159f24eSJoe Perches case 'h':
13470159f24eSJoe Perches #ifdef __BIG_ENDIAN
13480159f24eSJoe Perches index = 0;
13490159f24eSJoe Perches step = 1;
13500159f24eSJoe Perches #else
13510159f24eSJoe Perches index = 3;
13520159f24eSJoe Perches step = -1;
13530159f24eSJoe Perches #endif
13540159f24eSJoe Perches break;
13550159f24eSJoe Perches case 'l':
13560159f24eSJoe Perches index = 3;
13570159f24eSJoe Perches step = -1;
13580159f24eSJoe Perches break;
13590159f24eSJoe Perches case 'n':
13600159f24eSJoe Perches case 'b':
13610159f24eSJoe Perches default:
13620159f24eSJoe Perches index = 0;
13630159f24eSJoe Perches step = 1;
13640159f24eSJoe Perches break;
13650159f24eSJoe Perches }
13668a27f7c9SJoe Perches for (i = 0; i < 4; i++) {
13677c43d9a3SRasmus Villemoes char temp[4] __aligned(2); /* hold each IP quad in reverse order */
1368133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp;
13698a27f7c9SJoe Perches if (leading_zeros) {
13708a27f7c9SJoe Perches if (digits < 3)
13718a27f7c9SJoe Perches *p++ = '0';
13728a27f7c9SJoe Perches if (digits < 2)
13738a27f7c9SJoe Perches *p++ = '0';
13748a27f7c9SJoe Perches }
13758a27f7c9SJoe Perches /* reverse the digits in the quad */
13768a27f7c9SJoe Perches while (digits--)
13778a27f7c9SJoe Perches *p++ = temp[digits];
13788a27f7c9SJoe Perches if (i < 3)
13798a27f7c9SJoe Perches *p++ = '.';
13800159f24eSJoe Perches index += step;
13818a27f7c9SJoe Perches }
13828a27f7c9SJoe Perches *p = '\0';
13837b9186f5SAndré Goddard Rosa
13848a27f7c9SJoe Perches return p;
13858a27f7c9SJoe Perches }
13868a27f7c9SJoe Perches
1387cf3b429bSJoe Perches static noinline_for_stack
ip6_compressed_string(char * p,const char * addr)1388cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr)
13898a27f7c9SJoe Perches {
13907b9186f5SAndré Goddard Rosa int i, j, range;
13918a27f7c9SJoe Perches unsigned char zerolength[8];
13928a27f7c9SJoe Perches int longest = 1;
13938a27f7c9SJoe Perches int colonpos = -1;
13948a27f7c9SJoe Perches u16 word;
13957b9186f5SAndré Goddard Rosa u8 hi, lo;
13968a27f7c9SJoe Perches bool needcolon = false;
1397eb78cd26SJoe Perches bool useIPv4;
1398eb78cd26SJoe Perches struct in6_addr in6;
1399eb78cd26SJoe Perches
1400eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr));
1401eb78cd26SJoe Perches
1402eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
14038a27f7c9SJoe Perches
14048a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength));
14058a27f7c9SJoe Perches
14068a27f7c9SJoe Perches if (useIPv4)
14078a27f7c9SJoe Perches range = 6;
14088a27f7c9SJoe Perches else
14098a27f7c9SJoe Perches range = 8;
14108a27f7c9SJoe Perches
14118a27f7c9SJoe Perches /* find position of longest 0 run */
14128a27f7c9SJoe Perches for (i = 0; i < range; i++) {
14138a27f7c9SJoe Perches for (j = i; j < range; j++) {
1414eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0)
14158a27f7c9SJoe Perches break;
14168a27f7c9SJoe Perches zerolength[i]++;
14178a27f7c9SJoe Perches }
14188a27f7c9SJoe Perches }
14198a27f7c9SJoe Perches for (i = 0; i < range; i++) {
14208a27f7c9SJoe Perches if (zerolength[i] > longest) {
14218a27f7c9SJoe Perches longest = zerolength[i];
14228a27f7c9SJoe Perches colonpos = i;
14238a27f7c9SJoe Perches }
14248a27f7c9SJoe Perches }
142529cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */
142629cf519eSJoe Perches colonpos = -1;
14278a27f7c9SJoe Perches
14288a27f7c9SJoe Perches /* emit address */
14298a27f7c9SJoe Perches for (i = 0; i < range; i++) {
14308a27f7c9SJoe Perches if (i == colonpos) {
14318a27f7c9SJoe Perches if (needcolon || i == 0)
14328a27f7c9SJoe Perches *p++ = ':';
14338a27f7c9SJoe Perches *p++ = ':';
14348a27f7c9SJoe Perches needcolon = false;
14358a27f7c9SJoe Perches i += longest - 1;
14368a27f7c9SJoe Perches continue;
14378a27f7c9SJoe Perches }
14388a27f7c9SJoe Perches if (needcolon) {
14398a27f7c9SJoe Perches *p++ = ':';
14408a27f7c9SJoe Perches needcolon = false;
14418a27f7c9SJoe Perches }
14428a27f7c9SJoe Perches /* hex u16 without leading 0s */
1443eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]);
14448a27f7c9SJoe Perches hi = word >> 8;
14458a27f7c9SJoe Perches lo = word & 0xff;
14468a27f7c9SJoe Perches if (hi) {
14478a27f7c9SJoe Perches if (hi > 0x0f)
144855036ba7SAndy Shevchenko p = hex_byte_pack(p, hi);
14498a27f7c9SJoe Perches else
14508a27f7c9SJoe Perches *p++ = hex_asc_lo(hi);
145155036ba7SAndy Shevchenko p = hex_byte_pack(p, lo);
14528a27f7c9SJoe Perches }
1453b5ff992bSAndré Goddard Rosa else if (lo > 0x0f)
145455036ba7SAndy Shevchenko p = hex_byte_pack(p, lo);
14558a27f7c9SJoe Perches else
14568a27f7c9SJoe Perches *p++ = hex_asc_lo(lo);
14578a27f7c9SJoe Perches needcolon = true;
14588a27f7c9SJoe Perches }
14598a27f7c9SJoe Perches
14608a27f7c9SJoe Perches if (useIPv4) {
14618a27f7c9SJoe Perches if (needcolon)
14628a27f7c9SJoe Perches *p++ = ':';
14630159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4");
14648a27f7c9SJoe Perches }
14658a27f7c9SJoe Perches *p = '\0';
14667b9186f5SAndré Goddard Rosa
14678a27f7c9SJoe Perches return p;
14688a27f7c9SJoe Perches }
14698a27f7c9SJoe Perches
1470cf3b429bSJoe Perches static noinline_for_stack
ip6_string(char * p,const char * addr,const char * fmt)1471cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt)
14728a27f7c9SJoe Perches {
14738a27f7c9SJoe Perches int i;
14747b9186f5SAndré Goddard Rosa
1475689afa7dSHarvey Harrison for (i = 0; i < 8; i++) {
147655036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++);
147755036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++);
14788a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7)
1479689afa7dSHarvey Harrison *p++ = ':';
1480689afa7dSHarvey Harrison }
1481689afa7dSHarvey Harrison *p = '\0';
14827b9186f5SAndré Goddard Rosa
14838a27f7c9SJoe Perches return p;
14848a27f7c9SJoe Perches }
14858a27f7c9SJoe Perches
1486cf3b429bSJoe Perches static noinline_for_stack
ip6_addr_string(char * buf,char * end,const u8 * addr,struct printf_spec spec,const char * fmt)1487cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr,
14888a27f7c9SJoe Perches struct printf_spec spec, const char *fmt)
14898a27f7c9SJoe Perches {
14908a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
14918a27f7c9SJoe Perches
14928a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c')
1493eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr);
14948a27f7c9SJoe Perches else
1495eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt);
1496689afa7dSHarvey Harrison
1497d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec);
1498689afa7dSHarvey Harrison }
1499689afa7dSHarvey Harrison
1500cf3b429bSJoe Perches static noinline_for_stack
ip4_addr_string(char * buf,char * end,const u8 * addr,struct printf_spec spec,const char * fmt)1501cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr,
15028a27f7c9SJoe Perches struct printf_spec spec, const char *fmt)
15034aa99606SHarvey Harrison {
15048a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")];
15054aa99606SHarvey Harrison
15060159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt);
15074aa99606SHarvey Harrison
1508d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec);
15094aa99606SHarvey Harrison }
15104aa99606SHarvey Harrison
1511cf3b429bSJoe Perches static noinline_for_stack
ip6_addr_string_sa(char * buf,char * end,const struct sockaddr_in6 * sa,struct printf_spec spec,const char * fmt)151210679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
151310679643SDaniel Borkmann struct printf_spec spec, const char *fmt)
151410679643SDaniel Borkmann {
151510679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false;
151610679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
151710679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") +
151810679643SDaniel Borkmann sizeof("%1234567890")];
151910679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
152010679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr;
152110679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' };
152210679643SDaniel Borkmann u8 off = 0;
152310679643SDaniel Borkmann
152410679643SDaniel Borkmann fmt++;
152510679643SDaniel Borkmann while (isalpha(*++fmt)) {
152610679643SDaniel Borkmann switch (*fmt) {
152710679643SDaniel Borkmann case 'p':
152810679643SDaniel Borkmann have_p = true;
152910679643SDaniel Borkmann break;
153010679643SDaniel Borkmann case 'f':
153110679643SDaniel Borkmann have_f = true;
153210679643SDaniel Borkmann break;
153310679643SDaniel Borkmann case 's':
153410679643SDaniel Borkmann have_s = true;
153510679643SDaniel Borkmann break;
153610679643SDaniel Borkmann case 'c':
153710679643SDaniel Borkmann have_c = true;
153810679643SDaniel Borkmann break;
153910679643SDaniel Borkmann }
154010679643SDaniel Borkmann }
154110679643SDaniel Borkmann
154210679643SDaniel Borkmann if (have_p || have_s || have_f) {
154310679643SDaniel Borkmann *p = '[';
154410679643SDaniel Borkmann off = 1;
154510679643SDaniel Borkmann }
154610679643SDaniel Borkmann
154710679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c)
154810679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr);
154910679643SDaniel Borkmann else
155010679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6);
155110679643SDaniel Borkmann
155210679643SDaniel Borkmann if (have_p || have_s || have_f)
155310679643SDaniel Borkmann *p++ = ']';
155410679643SDaniel Borkmann
155510679643SDaniel Borkmann if (have_p) {
155610679643SDaniel Borkmann *p++ = ':';
155710679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec);
155810679643SDaniel Borkmann }
155910679643SDaniel Borkmann if (have_f) {
156010679643SDaniel Borkmann *p++ = '/';
156110679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo &
156210679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec);
156310679643SDaniel Borkmann }
156410679643SDaniel Borkmann if (have_s) {
156510679643SDaniel Borkmann *p++ = '%';
156610679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec);
156710679643SDaniel Borkmann }
156810679643SDaniel Borkmann *p = '\0';
156910679643SDaniel Borkmann
1570d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec);
157110679643SDaniel Borkmann }
157210679643SDaniel Borkmann
157310679643SDaniel Borkmann static noinline_for_stack
ip4_addr_string_sa(char * buf,char * end,const struct sockaddr_in * sa,struct printf_spec spec,const char * fmt)157410679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
157510679643SDaniel Borkmann struct printf_spec spec, const char *fmt)
157610679643SDaniel Borkmann {
157710679643SDaniel Borkmann bool have_p = false;
157810679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
157910679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr);
158010679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
158110679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 };
158210679643SDaniel Borkmann
158310679643SDaniel Borkmann fmt++;
158410679643SDaniel Borkmann while (isalpha(*++fmt)) {
158510679643SDaniel Borkmann switch (*fmt) {
158610679643SDaniel Borkmann case 'p':
158710679643SDaniel Borkmann have_p = true;
158810679643SDaniel Borkmann break;
158910679643SDaniel Borkmann case 'h':
159010679643SDaniel Borkmann case 'l':
159110679643SDaniel Borkmann case 'n':
159210679643SDaniel Borkmann case 'b':
159310679643SDaniel Borkmann fmt4[2] = *fmt;
159410679643SDaniel Borkmann break;
159510679643SDaniel Borkmann }
159610679643SDaniel Borkmann }
159710679643SDaniel Borkmann
159810679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4);
159910679643SDaniel Borkmann if (have_p) {
160010679643SDaniel Borkmann *p++ = ':';
160110679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec);
160210679643SDaniel Borkmann }
160310679643SDaniel Borkmann *p = '\0';
160410679643SDaniel Borkmann
1605d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec);
160610679643SDaniel Borkmann }
160710679643SDaniel Borkmann
160810679643SDaniel Borkmann static noinline_for_stack
ip_addr_string(char * buf,char * end,const void * ptr,struct printf_spec spec,const char * fmt)1609f00cc102SPetr Mladek char *ip_addr_string(char *buf, char *end, const void *ptr,
1610f00cc102SPetr Mladek struct printf_spec spec, const char *fmt)
1611f00cc102SPetr Mladek {
16120b74d4d7SPetr Mladek char *err_fmt_msg;
16130b74d4d7SPetr Mladek
16143e5903ebSPetr Mladek if (check_pointer(&buf, end, ptr, spec))
16153e5903ebSPetr Mladek return buf;
16163e5903ebSPetr Mladek
1617f00cc102SPetr Mladek switch (fmt[1]) {
1618f00cc102SPetr Mladek case '6':
1619f00cc102SPetr Mladek return ip6_addr_string(buf, end, ptr, spec, fmt);
1620f00cc102SPetr Mladek case '4':
1621f00cc102SPetr Mladek return ip4_addr_string(buf, end, ptr, spec, fmt);
1622f00cc102SPetr Mladek case 'S': {
1623f00cc102SPetr Mladek const union {
1624f00cc102SPetr Mladek struct sockaddr raw;
1625f00cc102SPetr Mladek struct sockaddr_in v4;
1626f00cc102SPetr Mladek struct sockaddr_in6 v6;
1627f00cc102SPetr Mladek } *sa = ptr;
1628f00cc102SPetr Mladek
1629f00cc102SPetr Mladek switch (sa->raw.sa_family) {
1630f00cc102SPetr Mladek case AF_INET:
1631f00cc102SPetr Mladek return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
1632f00cc102SPetr Mladek case AF_INET6:
1633f00cc102SPetr Mladek return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
1634f00cc102SPetr Mladek default:
1635c8c3b584SPetr Mladek return error_string(buf, end, "(einval)", spec);
1636f00cc102SPetr Mladek }}
1637f00cc102SPetr Mladek }
1638f00cc102SPetr Mladek
16390b74d4d7SPetr Mladek err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)";
1640c8c3b584SPetr Mladek return error_string(buf, end, err_fmt_msg, spec);
1641f00cc102SPetr Mladek }
1642f00cc102SPetr Mladek
1643f00cc102SPetr Mladek static noinline_for_stack
escaped_string(char * buf,char * end,u8 * addr,struct printf_spec spec,const char * fmt)164471dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
164571dca95dSAndy Shevchenko const char *fmt)
164671dca95dSAndy Shevchenko {
164771dca95dSAndy Shevchenko bool found = true;
164871dca95dSAndy Shevchenko int count = 1;
164971dca95dSAndy Shevchenko unsigned int flags = 0;
165071dca95dSAndy Shevchenko int len;
165171dca95dSAndy Shevchenko
165271dca95dSAndy Shevchenko if (spec.field_width == 0)
165371dca95dSAndy Shevchenko return buf; /* nothing to print */
165471dca95dSAndy Shevchenko
16553e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec))
16563e5903ebSPetr Mladek return buf;
165771dca95dSAndy Shevchenko
165871dca95dSAndy Shevchenko do {
165971dca95dSAndy Shevchenko switch (fmt[count++]) {
166071dca95dSAndy Shevchenko case 'a':
166171dca95dSAndy Shevchenko flags |= ESCAPE_ANY;
166271dca95dSAndy Shevchenko break;
166371dca95dSAndy Shevchenko case 'c':
166471dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL;
166571dca95dSAndy Shevchenko break;
166671dca95dSAndy Shevchenko case 'h':
166771dca95dSAndy Shevchenko flags |= ESCAPE_HEX;
166871dca95dSAndy Shevchenko break;
166971dca95dSAndy Shevchenko case 'n':
167071dca95dSAndy Shevchenko flags |= ESCAPE_NULL;
167171dca95dSAndy Shevchenko break;
167271dca95dSAndy Shevchenko case 'o':
167371dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL;
167471dca95dSAndy Shevchenko break;
167571dca95dSAndy Shevchenko case 'p':
167671dca95dSAndy Shevchenko flags |= ESCAPE_NP;
167771dca95dSAndy Shevchenko break;
167871dca95dSAndy Shevchenko case 's':
167971dca95dSAndy Shevchenko flags |= ESCAPE_SPACE;
168071dca95dSAndy Shevchenko break;
168171dca95dSAndy Shevchenko default:
168271dca95dSAndy Shevchenko found = false;
168371dca95dSAndy Shevchenko break;
168471dca95dSAndy Shevchenko }
168571dca95dSAndy Shevchenko } while (found);
168671dca95dSAndy Shevchenko
168771dca95dSAndy Shevchenko if (!flags)
168871dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP;
168971dca95dSAndy Shevchenko
169071dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width;
169171dca95dSAndy Shevchenko
169241416f23SRasmus Villemoes /*
169341416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to
169441416f23SRasmus Villemoes * the given buffer, and returns the total size of the output
169541416f23SRasmus Villemoes * had the buffer been big enough.
169641416f23SRasmus Villemoes */
169741416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
169871dca95dSAndy Shevchenko
169971dca95dSAndy Shevchenko return buf;
170071dca95dSAndy Shevchenko }
170171dca95dSAndy Shevchenko
1702*bd67c1c3SAndy Shevchenko #pragma GCC diagnostic push
1703*bd67c1c3SAndy Shevchenko #ifndef __clang__
1704*bd67c1c3SAndy Shevchenko #pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
1705*bd67c1c3SAndy Shevchenko #endif
va_format(char * buf,char * end,struct va_format * va_fmt,struct printf_spec spec)17063e5903ebSPetr Mladek static char *va_format(char *buf, char *end, struct va_format *va_fmt,
1707a1aea76aSAndy Shevchenko struct printf_spec spec)
170845c3e93dSPetr Mladek {
170945c3e93dSPetr Mladek va_list va;
171045c3e93dSPetr Mladek
17113e5903ebSPetr Mladek if (check_pointer(&buf, end, va_fmt, spec))
17123e5903ebSPetr Mladek return buf;
17133e5903ebSPetr Mladek
171445c3e93dSPetr Mladek va_copy(va, *va_fmt->va);
171545c3e93dSPetr Mladek buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va);
171645c3e93dSPetr Mladek va_end(va);
171745c3e93dSPetr Mladek
171845c3e93dSPetr Mladek return buf;
171945c3e93dSPetr Mladek }
1720*bd67c1c3SAndy Shevchenko #pragma GCC diagnostic pop
172145c3e93dSPetr Mladek
172271dca95dSAndy Shevchenko static noinline_for_stack
uuid_string(char * buf,char * end,const u8 * addr,struct printf_spec spec,const char * fmt)1723cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr,
17249ac6e44eSJoe Perches struct printf_spec spec, const char *fmt)
17259ac6e44eSJoe Perches {
17262b1b0d66SAndy Shevchenko char uuid[UUID_STRING_LEN + 1];
17279ac6e44eSJoe Perches char *p = uuid;
17289ac6e44eSJoe Perches int i;
1729f9727a17SChristoph Hellwig const u8 *index = uuid_index;
17309ac6e44eSJoe Perches bool uc = false;
17319ac6e44eSJoe Perches
17323e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec))
17333e5903ebSPetr Mladek return buf;
17343e5903ebSPetr Mladek
17359ac6e44eSJoe Perches switch (*(++fmt)) {
17369ac6e44eSJoe Perches case 'L':
1737df561f66SGustavo A. R. Silva uc = true;
17384c1ca831SNick Desaulniers fallthrough;
17399ac6e44eSJoe Perches case 'l':
1740f9727a17SChristoph Hellwig index = guid_index;
17419ac6e44eSJoe Perches break;
17429ac6e44eSJoe Perches case 'B':
17439ac6e44eSJoe Perches uc = true;
17449ac6e44eSJoe Perches break;
17459ac6e44eSJoe Perches }
17469ac6e44eSJoe Perches
17479ac6e44eSJoe Perches for (i = 0; i < 16; i++) {
1748aa4ea1c3SAndy Shevchenko if (uc)
1749aa4ea1c3SAndy Shevchenko p = hex_byte_pack_upper(p, addr[index[i]]);
1750aa4ea1c3SAndy Shevchenko else
175155036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]);
17529ac6e44eSJoe Perches switch (i) {
17539ac6e44eSJoe Perches case 3:
17549ac6e44eSJoe Perches case 5:
17559ac6e44eSJoe Perches case 7:
17569ac6e44eSJoe Perches case 9:
17579ac6e44eSJoe Perches *p++ = '-';
17589ac6e44eSJoe Perches break;
17599ac6e44eSJoe Perches }
17609ac6e44eSJoe Perches }
17619ac6e44eSJoe Perches
17629ac6e44eSJoe Perches *p = 0;
17639ac6e44eSJoe Perches
1764d529ac41SPetr Mladek return string_nocheck(buf, end, uuid, spec);
17659ac6e44eSJoe Perches }
17669ac6e44eSJoe Perches
17675b17aecfSAndy Shevchenko static noinline_for_stack
netdev_bits(char * buf,char * end,const void * addr,struct printf_spec spec,const char * fmt)1768431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr,
1769431bca24SGeert Uytterhoeven struct printf_spec spec, const char *fmt)
1770c8f44affSMichał Mirosław {
17715b17aecfSAndy Shevchenko unsigned long long num;
17725b17aecfSAndy Shevchenko int size;
17735b17aecfSAndy Shevchenko
17743e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec))
17753e5903ebSPetr Mladek return buf;
17763e5903ebSPetr Mladek
17775b17aecfSAndy Shevchenko switch (fmt[1]) {
17785b17aecfSAndy Shevchenko case 'F':
17795b17aecfSAndy Shevchenko num = *(const netdev_features_t *)addr;
17805b17aecfSAndy Shevchenko size = sizeof(netdev_features_t);
17815b17aecfSAndy Shevchenko break;
17825b17aecfSAndy Shevchenko default:
1783c8c3b584SPetr Mladek return error_string(buf, end, "(%pN?)", spec);
17845b17aecfSAndy Shevchenko }
1785c8f44affSMichał Mirosław
17863cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size);
1787c8f44affSMichał Mirosław }
1788c8f44affSMichał Mirosław
1789aaf07621SJoe Perches static noinline_for_stack
fourcc_string(char * buf,char * end,const u32 * fourcc,struct printf_spec spec,const char * fmt)1790af612e43SSakari Ailus char *fourcc_string(char *buf, char *end, const u32 *fourcc,
1791af612e43SSakari Ailus struct printf_spec spec, const char *fmt)
1792af612e43SSakari Ailus {
1793af612e43SSakari Ailus char output[sizeof("0123 little-endian (0x01234567)")];
1794af612e43SSakari Ailus char *p = output;
1795af612e43SSakari Ailus unsigned int i;
1796d75b26f8SAndy Shevchenko u32 orig, val;
1797af612e43SSakari Ailus
1798af612e43SSakari Ailus if (fmt[1] != 'c' || fmt[2] != 'c')
1799af612e43SSakari Ailus return error_string(buf, end, "(%p4?)", spec);
1800af612e43SSakari Ailus
1801af612e43SSakari Ailus if (check_pointer(&buf, end, fourcc, spec))
1802af612e43SSakari Ailus return buf;
1803af612e43SSakari Ailus
1804d75b26f8SAndy Shevchenko orig = get_unaligned(fourcc);
1805d75b26f8SAndy Shevchenko val = orig & ~BIT(31);
1806af612e43SSakari Ailus
1807d75b26f8SAndy Shevchenko for (i = 0; i < sizeof(u32); i++) {
1808af612e43SSakari Ailus unsigned char c = val >> (i * 8);
1809af612e43SSakari Ailus
1810af612e43SSakari Ailus /* Print non-control ASCII characters as-is, dot otherwise */
1811af612e43SSakari Ailus *p++ = isascii(c) && isprint(c) ? c : '.';
1812af612e43SSakari Ailus }
1813af612e43SSakari Ailus
1814f74a08fcSAndy Shevchenko *p++ = ' ';
1815d75b26f8SAndy Shevchenko strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
1816af612e43SSakari Ailus p += strlen(p);
1817af612e43SSakari Ailus
1818af612e43SSakari Ailus *p++ = ' ';
1819af612e43SSakari Ailus *p++ = '(';
1820d75b26f8SAndy Shevchenko p = special_hex_number(p, output + sizeof(output) - 2, orig, sizeof(u32));
1821af612e43SSakari Ailus *p++ = ')';
1822af612e43SSakari Ailus *p = '\0';
1823af612e43SSakari Ailus
1824af612e43SSakari Ailus return string(buf, end, output, spec);
1825af612e43SSakari Ailus }
1826af612e43SSakari Ailus
1827af612e43SSakari Ailus static noinline_for_stack
address_val(char * buf,char * end,const void * addr,struct printf_spec spec,const char * fmt)18283e5903ebSPetr Mladek char *address_val(char *buf, char *end, const void *addr,
18293e5903ebSPetr Mladek struct printf_spec spec, const char *fmt)
1830aaf07621SJoe Perches {
1831aaf07621SJoe Perches unsigned long long num;
18323cab1e71SAndy Shevchenko int size;
1833aaf07621SJoe Perches
18343e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec))
18353e5903ebSPetr Mladek return buf;
18363e5903ebSPetr Mladek
1837aaf07621SJoe Perches switch (fmt[1]) {
1838aaf07621SJoe Perches case 'd':
1839aaf07621SJoe Perches num = *(const dma_addr_t *)addr;
18403cab1e71SAndy Shevchenko size = sizeof(dma_addr_t);
1841aaf07621SJoe Perches break;
1842aaf07621SJoe Perches case 'p':
1843aaf07621SJoe Perches default:
1844aaf07621SJoe Perches num = *(const phys_addr_t *)addr;
18453cab1e71SAndy Shevchenko size = sizeof(phys_addr_t);
1846aaf07621SJoe Perches break;
1847aaf07621SJoe Perches }
1848aaf07621SJoe Perches
18493cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size);
1850aaf07621SJoe Perches }
1851aaf07621SJoe Perches
1852900cca29SGeert Uytterhoeven static noinline_for_stack
date_str(char * buf,char * end,const struct rtc_time * tm,bool r)18534d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r)
18544d42c447SAndy Shevchenko {
18554d42c447SAndy Shevchenko int year = tm->tm_year + (r ? 0 : 1900);
18564d42c447SAndy Shevchenko int mon = tm->tm_mon + (r ? 0 : 1);
18574d42c447SAndy Shevchenko
18584d42c447SAndy Shevchenko buf = number(buf, end, year, default_dec04_spec);
18594d42c447SAndy Shevchenko if (buf < end)
18604d42c447SAndy Shevchenko *buf = '-';
18614d42c447SAndy Shevchenko buf++;
18624d42c447SAndy Shevchenko
18634d42c447SAndy Shevchenko buf = number(buf, end, mon, default_dec02_spec);
18644d42c447SAndy Shevchenko if (buf < end)
18654d42c447SAndy Shevchenko *buf = '-';
18664d42c447SAndy Shevchenko buf++;
18674d42c447SAndy Shevchenko
18684d42c447SAndy Shevchenko return number(buf, end, tm->tm_mday, default_dec02_spec);
18694d42c447SAndy Shevchenko }
18704d42c447SAndy Shevchenko
18714d42c447SAndy Shevchenko static noinline_for_stack
time_str(char * buf,char * end,const struct rtc_time * tm,bool r)18724d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r)
18734d42c447SAndy Shevchenko {
18744d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_hour, default_dec02_spec);
18754d42c447SAndy Shevchenko if (buf < end)
18764d42c447SAndy Shevchenko *buf = ':';
18774d42c447SAndy Shevchenko buf++;
18784d42c447SAndy Shevchenko
18794d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_min, default_dec02_spec);
18804d42c447SAndy Shevchenko if (buf < end)
18814d42c447SAndy Shevchenko *buf = ':';
18824d42c447SAndy Shevchenko buf++;
18834d42c447SAndy Shevchenko
18844d42c447SAndy Shevchenko return number(buf, end, tm->tm_sec, default_dec02_spec);
18854d42c447SAndy Shevchenko }
18864d42c447SAndy Shevchenko
18874d42c447SAndy Shevchenko static noinline_for_stack
rtc_str(char * buf,char * end,const struct rtc_time * tm,struct printf_spec spec,const char * fmt)18883e5903ebSPetr Mladek char *rtc_str(char *buf, char *end, const struct rtc_time *tm,
18893e5903ebSPetr Mladek struct printf_spec spec, const char *fmt)
18904d42c447SAndy Shevchenko {
18914d42c447SAndy Shevchenko bool have_t = true, have_d = true;
189220bc8c1eSAndy Shevchenko bool raw = false, iso8601_separator = true;
189320bc8c1eSAndy Shevchenko bool found = true;
18944d42c447SAndy Shevchenko int count = 2;
18954d42c447SAndy Shevchenko
18963e5903ebSPetr Mladek if (check_pointer(&buf, end, tm, spec))
18973e5903ebSPetr Mladek return buf;
18983e5903ebSPetr Mladek
18994d42c447SAndy Shevchenko switch (fmt[count]) {
19004d42c447SAndy Shevchenko case 'd':
19014d42c447SAndy Shevchenko have_t = false;
19024d42c447SAndy Shevchenko count++;
19034d42c447SAndy Shevchenko break;
19044d42c447SAndy Shevchenko case 't':
19054d42c447SAndy Shevchenko have_d = false;
19064d42c447SAndy Shevchenko count++;
19074d42c447SAndy Shevchenko break;
19084d42c447SAndy Shevchenko }
19094d42c447SAndy Shevchenko
191020bc8c1eSAndy Shevchenko do {
191120bc8c1eSAndy Shevchenko switch (fmt[count++]) {
191220bc8c1eSAndy Shevchenko case 'r':
191320bc8c1eSAndy Shevchenko raw = true;
191420bc8c1eSAndy Shevchenko break;
191520bc8c1eSAndy Shevchenko case 's':
191620bc8c1eSAndy Shevchenko iso8601_separator = false;
191720bc8c1eSAndy Shevchenko break;
191820bc8c1eSAndy Shevchenko default:
191920bc8c1eSAndy Shevchenko found = false;
192020bc8c1eSAndy Shevchenko break;
192120bc8c1eSAndy Shevchenko }
192220bc8c1eSAndy Shevchenko } while (found);
19234d42c447SAndy Shevchenko
19244d42c447SAndy Shevchenko if (have_d)
19254d42c447SAndy Shevchenko buf = date_str(buf, end, tm, raw);
19264d42c447SAndy Shevchenko if (have_d && have_t) {
19274d42c447SAndy Shevchenko if (buf < end)
192820bc8c1eSAndy Shevchenko *buf = iso8601_separator ? 'T' : ' ';
19294d42c447SAndy Shevchenko buf++;
19304d42c447SAndy Shevchenko }
19314d42c447SAndy Shevchenko if (have_t)
19324d42c447SAndy Shevchenko buf = time_str(buf, end, tm, raw);
19334d42c447SAndy Shevchenko
19344d42c447SAndy Shevchenko return buf;
19354d42c447SAndy Shevchenko }
19364d42c447SAndy Shevchenko
19374d42c447SAndy Shevchenko static noinline_for_stack
time64_str(char * buf,char * end,const time64_t time,struct printf_spec spec,const char * fmt)19387daac5b2SAndy Shevchenko char *time64_str(char *buf, char *end, const time64_t time,
19397daac5b2SAndy Shevchenko struct printf_spec spec, const char *fmt)
19407daac5b2SAndy Shevchenko {
19417daac5b2SAndy Shevchenko struct rtc_time rtc_time;
19427daac5b2SAndy Shevchenko struct tm tm;
19437daac5b2SAndy Shevchenko
19447daac5b2SAndy Shevchenko time64_to_tm(time, 0, &tm);
19457daac5b2SAndy Shevchenko
19467daac5b2SAndy Shevchenko rtc_time.tm_sec = tm.tm_sec;
19477daac5b2SAndy Shevchenko rtc_time.tm_min = tm.tm_min;
19487daac5b2SAndy Shevchenko rtc_time.tm_hour = tm.tm_hour;
19497daac5b2SAndy Shevchenko rtc_time.tm_mday = tm.tm_mday;
19507daac5b2SAndy Shevchenko rtc_time.tm_mon = tm.tm_mon;
19517daac5b2SAndy Shevchenko rtc_time.tm_year = tm.tm_year;
19527daac5b2SAndy Shevchenko rtc_time.tm_wday = tm.tm_wday;
19537daac5b2SAndy Shevchenko rtc_time.tm_yday = tm.tm_yday;
19547daac5b2SAndy Shevchenko
19557daac5b2SAndy Shevchenko rtc_time.tm_isdst = 0;
19567daac5b2SAndy Shevchenko
19577daac5b2SAndy Shevchenko return rtc_str(buf, end, &rtc_time, spec, fmt);
19587daac5b2SAndy Shevchenko }
19597daac5b2SAndy Shevchenko
19607daac5b2SAndy Shevchenko static noinline_for_stack
time_and_date(char * buf,char * end,void * ptr,struct printf_spec spec,const char * fmt)19614d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec,
19624d42c447SAndy Shevchenko const char *fmt)
19634d42c447SAndy Shevchenko {
19644d42c447SAndy Shevchenko switch (fmt[1]) {
19654d42c447SAndy Shevchenko case 'R':
19663e5903ebSPetr Mladek return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt);
19677daac5b2SAndy Shevchenko case 'T':
19687daac5b2SAndy Shevchenko return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt);
19694d42c447SAndy Shevchenko default:
19707daac5b2SAndy Shevchenko return error_string(buf, end, "(%pt?)", spec);
19714d42c447SAndy Shevchenko }
19724d42c447SAndy Shevchenko }
19734d42c447SAndy Shevchenko
19744d42c447SAndy Shevchenko static noinline_for_stack
clock(char * buf,char * end,struct clk * clk,struct printf_spec spec,const char * fmt)1975900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1976900cca29SGeert Uytterhoeven const char *fmt)
1977900cca29SGeert Uytterhoeven {
19780b74d4d7SPetr Mladek if (!IS_ENABLED(CONFIG_HAVE_CLK))
1979c8c3b584SPetr Mladek return error_string(buf, end, "(%pC?)", spec);
19800b74d4d7SPetr Mladek
19813e5903ebSPetr Mladek if (check_pointer(&buf, end, clk, spec))
19823e5903ebSPetr Mladek return buf;
1983900cca29SGeert Uytterhoeven
1984900cca29SGeert Uytterhoeven switch (fmt[1]) {
1985900cca29SGeert Uytterhoeven case 'n':
1986900cca29SGeert Uytterhoeven default:
1987900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK
1988900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec);
1989900cca29SGeert Uytterhoeven #else
19904ca96aa9SGeert Uytterhoeven return ptr_to_id(buf, end, clk, spec);
1991900cca29SGeert Uytterhoeven #endif
1992900cca29SGeert Uytterhoeven }
1993900cca29SGeert Uytterhoeven }
1994900cca29SGeert Uytterhoeven
1995edf14cdbSVlastimil Babka static
format_flags(char * buf,char * end,unsigned long flags,const struct trace_print_flags * names)1996edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags,
1997edf14cdbSVlastimil Babka const struct trace_print_flags *names)
1998edf14cdbSVlastimil Babka {
1999edf14cdbSVlastimil Babka unsigned long mask;
2000edf14cdbSVlastimil Babka
2001edf14cdbSVlastimil Babka for ( ; flags && names->name; names++) {
2002edf14cdbSVlastimil Babka mask = names->mask;
2003edf14cdbSVlastimil Babka if ((flags & mask) != mask)
2004edf14cdbSVlastimil Babka continue;
2005edf14cdbSVlastimil Babka
2006abd4fe62SAndy Shevchenko buf = string(buf, end, names->name, default_str_spec);
2007edf14cdbSVlastimil Babka
2008edf14cdbSVlastimil Babka flags &= ~mask;
2009edf14cdbSVlastimil Babka if (flags) {
2010edf14cdbSVlastimil Babka if (buf < end)
2011edf14cdbSVlastimil Babka *buf = '|';
2012edf14cdbSVlastimil Babka buf++;
2013edf14cdbSVlastimil Babka }
2014edf14cdbSVlastimil Babka }
2015edf14cdbSVlastimil Babka
2016edf14cdbSVlastimil Babka if (flags)
201754433973SAndy Shevchenko buf = number(buf, end, flags, default_flag_spec);
2018edf14cdbSVlastimil Babka
2019edf14cdbSVlastimil Babka return buf;
2020edf14cdbSVlastimil Babka }
2021edf14cdbSVlastimil Babka
2022c244297aSYafang Shao struct page_flags_fields {
2023c244297aSYafang Shao int width;
2024c244297aSYafang Shao int shift;
2025c244297aSYafang Shao int mask;
2026c244297aSYafang Shao const struct printf_spec *spec;
2027c244297aSYafang Shao const char *name;
2028c244297aSYafang Shao };
2029c244297aSYafang Shao
2030c244297aSYafang Shao static const struct page_flags_fields pff[] = {
2031c244297aSYafang Shao {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK,
2032c244297aSYafang Shao &default_dec_spec, "section"},
2033c244297aSYafang Shao {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK,
2034c244297aSYafang Shao &default_dec_spec, "node"},
2035c244297aSYafang Shao {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK,
2036c244297aSYafang Shao &default_dec_spec, "zone"},
2037c244297aSYafang Shao {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK,
2038c244297aSYafang Shao &default_flag_spec, "lastcpupid"},
2039c244297aSYafang Shao {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK,
2040c244297aSYafang Shao &default_flag_spec, "kasantag"},
2041c244297aSYafang Shao };
2042c244297aSYafang Shao
2043c244297aSYafang Shao static
format_page_flags(char * buf,char * end,unsigned long flags)2044c244297aSYafang Shao char *format_page_flags(char *buf, char *end, unsigned long flags)
2045c244297aSYafang Shao {
204641c961b9SMuchun Song unsigned long main_flags = flags & PAGEFLAGS_MASK;
2047c244297aSYafang Shao bool append = false;
2048c244297aSYafang Shao int i;
2049c244297aSYafang Shao
205023efd080SMatthew Wilcox (Oracle) buf = number(buf, end, flags, default_flag_spec);
205123efd080SMatthew Wilcox (Oracle) if (buf < end)
205223efd080SMatthew Wilcox (Oracle) *buf = '(';
205323efd080SMatthew Wilcox (Oracle) buf++;
205423efd080SMatthew Wilcox (Oracle)
2055c244297aSYafang Shao /* Page flags from the main area. */
2056c244297aSYafang Shao if (main_flags) {
2057c244297aSYafang Shao buf = format_flags(buf, end, main_flags, pageflag_names);
2058c244297aSYafang Shao append = true;
2059c244297aSYafang Shao }
2060c244297aSYafang Shao
2061c244297aSYafang Shao /* Page flags from the fields area */
2062c244297aSYafang Shao for (i = 0; i < ARRAY_SIZE(pff); i++) {
2063c244297aSYafang Shao /* Skip undefined fields. */
2064c244297aSYafang Shao if (!pff[i].width)
2065c244297aSYafang Shao continue;
2066c244297aSYafang Shao
2067c244297aSYafang Shao /* Format: Flag Name + '=' (equals sign) + Number + '|' (separator) */
2068c244297aSYafang Shao if (append) {
2069c244297aSYafang Shao if (buf < end)
2070c244297aSYafang Shao *buf = '|';
2071c244297aSYafang Shao buf++;
2072c244297aSYafang Shao }
2073c244297aSYafang Shao
2074c244297aSYafang Shao buf = string(buf, end, pff[i].name, default_str_spec);
2075c244297aSYafang Shao if (buf < end)
2076c244297aSYafang Shao *buf = '=';
2077c244297aSYafang Shao buf++;
2078c244297aSYafang Shao buf = number(buf, end, (flags >> pff[i].shift) & pff[i].mask,
2079c244297aSYafang Shao *pff[i].spec);
2080c244297aSYafang Shao
2081c244297aSYafang Shao append = true;
2082c244297aSYafang Shao }
208323efd080SMatthew Wilcox (Oracle) if (buf < end)
208423efd080SMatthew Wilcox (Oracle) *buf = ')';
208523efd080SMatthew Wilcox (Oracle) buf++;
2086c244297aSYafang Shao
2087c244297aSYafang Shao return buf;
2088c244297aSYafang Shao }
2089c244297aSYafang Shao
2090edf14cdbSVlastimil Babka static noinline_for_stack
flags_string(char * buf,char * end,void * flags_ptr,struct printf_spec spec,const char * fmt)20910b74d4d7SPetr Mladek char *flags_string(char *buf, char *end, void *flags_ptr,
20920b74d4d7SPetr Mladek struct printf_spec spec, const char *fmt)
2093edf14cdbSVlastimil Babka {
2094edf14cdbSVlastimil Babka unsigned long flags;
2095edf14cdbSVlastimil Babka const struct trace_print_flags *names;
2096edf14cdbSVlastimil Babka
20973e5903ebSPetr Mladek if (check_pointer(&buf, end, flags_ptr, spec))
20983e5903ebSPetr Mladek return buf;
20993e5903ebSPetr Mladek
2100edf14cdbSVlastimil Babka switch (fmt[1]) {
2101edf14cdbSVlastimil Babka case 'p':
2102c244297aSYafang Shao return format_page_flags(buf, end, *(unsigned long *)flags_ptr);
2103edf14cdbSVlastimil Babka case 'v':
2104edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr;
2105edf14cdbSVlastimil Babka names = vmaflag_names;
2106edf14cdbSVlastimil Babka break;
2107edf14cdbSVlastimil Babka case 'g':
210830d497a0SAndy Shevchenko flags = (__force unsigned long)(*(gfp_t *)flags_ptr);
2109edf14cdbSVlastimil Babka names = gfpflag_names;
2110edf14cdbSVlastimil Babka break;
2111edf14cdbSVlastimil Babka default:
2112c8c3b584SPetr Mladek return error_string(buf, end, "(%pG?)", spec);
2113edf14cdbSVlastimil Babka }
2114edf14cdbSVlastimil Babka
2115edf14cdbSVlastimil Babka return format_flags(buf, end, flags, names);
2116edf14cdbSVlastimil Babka }
2117edf14cdbSVlastimil Babka
2118ce4fecf1SPantelis Antoniou static noinline_for_stack
fwnode_full_name_string(struct fwnode_handle * fwnode,char * buf,char * end)2119a92eb762SSakari Ailus char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
2120a92eb762SSakari Ailus char *end)
2121ce4fecf1SPantelis Antoniou {
2122ce4fecf1SPantelis Antoniou int depth;
2123ce4fecf1SPantelis Antoniou
2124a92eb762SSakari Ailus /* Loop starting from the root node to the current node. */
2125a92eb762SSakari Ailus for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) {
21265c47251eSHerve Codina /*
21275c47251eSHerve Codina * Only get a reference for other nodes (i.e. parent nodes).
21285c47251eSHerve Codina * fwnode refcount may be 0 here.
21295c47251eSHerve Codina */
21305c47251eSHerve Codina struct fwnode_handle *__fwnode = depth ?
21315c47251eSHerve Codina fwnode_get_nth_parent(fwnode, depth) : fwnode;
2132ce4fecf1SPantelis Antoniou
2133a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name_prefix(__fwnode),
2134abd4fe62SAndy Shevchenko default_str_spec);
2135a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name(__fwnode),
2136a92eb762SSakari Ailus default_str_spec);
2137a92eb762SSakari Ailus
21385c47251eSHerve Codina if (depth)
2139a92eb762SSakari Ailus fwnode_handle_put(__fwnode);
2140ce4fecf1SPantelis Antoniou }
2141a92eb762SSakari Ailus
2142ce4fecf1SPantelis Antoniou return buf;
2143ce4fecf1SPantelis Antoniou }
2144ce4fecf1SPantelis Antoniou
2145ce4fecf1SPantelis Antoniou static noinline_for_stack
device_node_string(char * buf,char * end,struct device_node * dn,struct printf_spec spec,const char * fmt)2146ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn,
2147ce4fecf1SPantelis Antoniou struct printf_spec spec, const char *fmt)
2148ce4fecf1SPantelis Antoniou {
2149ce4fecf1SPantelis Antoniou char tbuf[sizeof("xxxx") + 1];
2150ce4fecf1SPantelis Antoniou const char *p;
2151ce4fecf1SPantelis Antoniou int ret;
2152ce4fecf1SPantelis Antoniou char *buf_start = buf;
2153ce4fecf1SPantelis Antoniou struct property *prop;
2154ce4fecf1SPantelis Antoniou bool has_mult, pass;
2155ce4fecf1SPantelis Antoniou
2156ce4fecf1SPantelis Antoniou struct printf_spec str_spec = spec;
2157ce4fecf1SPantelis Antoniou str_spec.field_width = -1;
2158ce4fecf1SPantelis Antoniou
215983abc5a7SSakari Ailus if (fmt[0] != 'F')
216083abc5a7SSakari Ailus return error_string(buf, end, "(%pO?)", spec);
216183abc5a7SSakari Ailus
2162ce4fecf1SPantelis Antoniou if (!IS_ENABLED(CONFIG_OF))
2163c8c3b584SPetr Mladek return error_string(buf, end, "(%pOF?)", spec);
2164ce4fecf1SPantelis Antoniou
21653e5903ebSPetr Mladek if (check_pointer(&buf, end, dn, spec))
21663e5903ebSPetr Mladek return buf;
2167ce4fecf1SPantelis Antoniou
2168ce4fecf1SPantelis Antoniou /* simple case without anything any more format specifiers */
2169ce4fecf1SPantelis Antoniou fmt++;
2170ce4fecf1SPantelis Antoniou if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0)
2171ce4fecf1SPantelis Antoniou fmt = "f";
2172ce4fecf1SPantelis Antoniou
2173ce4fecf1SPantelis Antoniou for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) {
21746d0a70a2SRob Herring int precision;
2175ce4fecf1SPantelis Antoniou if (pass) {
2176ce4fecf1SPantelis Antoniou if (buf < end)
2177ce4fecf1SPantelis Antoniou *buf = ':';
2178ce4fecf1SPantelis Antoniou buf++;
2179ce4fecf1SPantelis Antoniou }
2180ce4fecf1SPantelis Antoniou
2181ce4fecf1SPantelis Antoniou switch (*fmt) {
2182ce4fecf1SPantelis Antoniou case 'f': /* full_name */
2183a92eb762SSakari Ailus buf = fwnode_full_name_string(of_fwnode_handle(dn), buf,
2184a92eb762SSakari Ailus end);
2185ce4fecf1SPantelis Antoniou break;
2186ce4fecf1SPantelis Antoniou case 'n': /* name */
2187a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn));
21886d0a70a2SRob Herring precision = str_spec.precision;
21896d0a70a2SRob Herring str_spec.precision = strchrnul(p, '@') - p;
21906d0a70a2SRob Herring buf = string(buf, end, p, str_spec);
21916d0a70a2SRob Herring str_spec.precision = precision;
2192ce4fecf1SPantelis Antoniou break;
2193ce4fecf1SPantelis Antoniou case 'p': /* phandle */
219409ceb8d7SAndy Shevchenko buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec);
2195ce4fecf1SPantelis Antoniou break;
2196ce4fecf1SPantelis Antoniou case 'P': /* path-spec */
2197a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn));
2198ce4fecf1SPantelis Antoniou if (!p[1])
2199ce4fecf1SPantelis Antoniou p = "/";
2200ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec);
2201ce4fecf1SPantelis Antoniou break;
2202ce4fecf1SPantelis Antoniou case 'F': /* flags */
2203ce4fecf1SPantelis Antoniou tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-';
2204ce4fecf1SPantelis Antoniou tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-';
2205ce4fecf1SPantelis Antoniou tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-';
2206ce4fecf1SPantelis Antoniou tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-';
2207ce4fecf1SPantelis Antoniou tbuf[4] = 0;
2208d529ac41SPetr Mladek buf = string_nocheck(buf, end, tbuf, str_spec);
2209ce4fecf1SPantelis Antoniou break;
2210ce4fecf1SPantelis Antoniou case 'c': /* major compatible string */
2211ce4fecf1SPantelis Antoniou ret = of_property_read_string(dn, "compatible", &p);
2212ce4fecf1SPantelis Antoniou if (!ret)
2213ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec);
2214ce4fecf1SPantelis Antoniou break;
2215ce4fecf1SPantelis Antoniou case 'C': /* full compatible string */
2216ce4fecf1SPantelis Antoniou has_mult = false;
2217ce4fecf1SPantelis Antoniou of_property_for_each_string(dn, "compatible", prop, p) {
2218ce4fecf1SPantelis Antoniou if (has_mult)
2219d529ac41SPetr Mladek buf = string_nocheck(buf, end, ",", str_spec);
2220d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec);
2221ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec);
2222d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec);
2223ce4fecf1SPantelis Antoniou
2224ce4fecf1SPantelis Antoniou has_mult = true;
2225ce4fecf1SPantelis Antoniou }
2226ce4fecf1SPantelis Antoniou break;
2227ce4fecf1SPantelis Antoniou default:
2228ce4fecf1SPantelis Antoniou break;
2229ce4fecf1SPantelis Antoniou }
2230ce4fecf1SPantelis Antoniou }
2231ce4fecf1SPantelis Antoniou
2232ce4fecf1SPantelis Antoniou return widen_string(buf, buf - buf_start, end, spec);
2233ce4fecf1SPantelis Antoniou }
2234ce4fecf1SPantelis Antoniou
22353bd32d6aSSakari Ailus static noinline_for_stack
fwnode_string(char * buf,char * end,struct fwnode_handle * fwnode,struct printf_spec spec,const char * fmt)22363bd32d6aSSakari Ailus char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode,
2237798cc27aSPetr Mladek struct printf_spec spec, const char *fmt)
2238798cc27aSPetr Mladek {
22393bd32d6aSSakari Ailus struct printf_spec str_spec = spec;
22403bd32d6aSSakari Ailus char *buf_start = buf;
22413bd32d6aSSakari Ailus
22423bd32d6aSSakari Ailus str_spec.field_width = -1;
22433bd32d6aSSakari Ailus
22443bd32d6aSSakari Ailus if (*fmt != 'w')
22453bd32d6aSSakari Ailus return error_string(buf, end, "(%pf?)", spec);
22463bd32d6aSSakari Ailus
22473bd32d6aSSakari Ailus if (check_pointer(&buf, end, fwnode, spec))
22483bd32d6aSSakari Ailus return buf;
22493bd32d6aSSakari Ailus
22503bd32d6aSSakari Ailus fmt++;
22513bd32d6aSSakari Ailus
22523bd32d6aSSakari Ailus switch (*fmt) {
22533bd32d6aSSakari Ailus case 'P': /* name */
22543bd32d6aSSakari Ailus buf = string(buf, end, fwnode_get_name(fwnode), str_spec);
22553bd32d6aSSakari Ailus break;
22563bd32d6aSSakari Ailus case 'f': /* full_name */
22573bd32d6aSSakari Ailus default:
22583bd32d6aSSakari Ailus buf = fwnode_full_name_string(fwnode, buf, end);
22593bd32d6aSSakari Ailus break;
2260798cc27aSPetr Mladek }
2261798cc27aSPetr Mladek
22623bd32d6aSSakari Ailus return widen_string(buf, buf - buf_start, end, spec);
2263798cc27aSPetr Mladek }
2264798cc27aSPetr Mladek
226542619747SIra Weiny static noinline_for_stack
resource_or_range(const char * fmt,char * buf,char * end,void * ptr,struct printf_spec spec)226642619747SIra Weiny char *resource_or_range(const char *fmt, char *buf, char *end, void *ptr,
226742619747SIra Weiny struct printf_spec spec)
226842619747SIra Weiny {
226942619747SIra Weiny if (*fmt == 'r' && fmt[1] == 'a')
227042619747SIra Weiny return range_string(buf, end, ptr, spec, fmt);
227142619747SIra Weiny return resource_string(buf, end, ptr, spec, fmt);
227242619747SIra Weiny }
227342619747SIra Weiny
no_hash_pointers_enable(char * str)227479270291SStephen Boyd int __init no_hash_pointers_enable(char *str)
22755ead723aSTimur Tabi {
22769f961c2eSMarco Elver if (no_hash_pointers)
22779f961c2eSMarco Elver return 0;
22789f961c2eSMarco Elver
22795ead723aSTimur Tabi no_hash_pointers = true;
22805ead723aSTimur Tabi
22815ead723aSTimur Tabi pr_warn("**********************************************************\n");
22825ead723aSTimur Tabi pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
22835ead723aSTimur Tabi pr_warn("** **\n");
22845ead723aSTimur Tabi pr_warn("** This system shows unhashed kernel memory addresses **\n");
22855ead723aSTimur Tabi pr_warn("** via the console, logs, and other interfaces. This **\n");
22865ead723aSTimur Tabi pr_warn("** might reduce the security of your system. **\n");
22875ead723aSTimur Tabi pr_warn("** **\n");
22885ead723aSTimur Tabi pr_warn("** If you see this message and you are not debugging **\n");
22895ead723aSTimur Tabi pr_warn("** the kernel, report this immediately to your system **\n");
22905ead723aSTimur Tabi pr_warn("** administrator! **\n");
22915ead723aSTimur Tabi pr_warn("** **\n");
22925ead723aSTimur Tabi pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
22935ead723aSTimur Tabi pr_warn("**********************************************************\n");
22945ead723aSTimur Tabi
22955ead723aSTimur Tabi return 0;
22965ead723aSTimur Tabi }
22975ead723aSTimur Tabi early_param("no_hash_pointers", no_hash_pointers_enable);
22985ead723aSTimur Tabi
2299787983daSGary Guo /*
2300787983daSGary Guo * Show a '%p' thing. A kernel extension is that the '%p' is followed
2301787983daSGary Guo * by an extra set of alphanumeric characters that are extended format
23024d8a743cSLinus Torvalds * specifiers.
23034d8a743cSLinus Torvalds *
23044d8a743cSLinus Torvalds * Please update scripts/checkpatch.pl when adding/removing conversion
23054d8a743cSLinus Torvalds * characters. (Search for "check for vsprintf extension").
23064d8a743cSLinus Torvalds *
23070b523769SJoe Perches * Right now we handle:
23080b523769SJoe Perches *
23090b523769SJoe Perches * - 'S' For symbolic direct pointers (or function descriptors) with offset
2310332d2e78SLinus Torvalds * - 's' For symbolic direct pointers (or function descriptors) without offset
23110fe1ef24SLinus Torvalds * - '[Ss]R' as above with __builtin_extract_return_addr() translation
2312cdb7e52dSSergey Senozhatsky * - 'S[R]b' as above with module build ID (for use in backtraces)
2313cdb7e52dSSergey Senozhatsky * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of
23149af77064SSakari Ailus * %ps and %pS. Be careful when re-using these specifiers.
23159294523eSStephen Boyd * - 'B' For backtraced symbolic direct pointers with offset
23161586c5aeSSakari Ailus * - 'Bb' as above with module build ID (for use in backtraces)
23171586c5aeSSakari Ailus * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
23180f77a8d3SNamhyung Kim * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
23199294523eSStephen Boyd * - 'ra' For struct ranges, e.g., [range 0x0000000000000000 - 0x00000000000000ff]
2320c7dabef8SBjorn Helgaas * - 'b[l]' For a bitmap, the number of bits is determined by the field
2321c7dabef8SBjorn Helgaas * width which must be explicitly specified either as part of the
232242619747SIra Weiny * format string '%32b[l]' or through '%*b[l]', [l] selects
2323dbc760bcSTejun Heo * range-list format instead of hex format
2324dbc760bcSTejun Heo * - 'M' For a 6-byte MAC address, it prints the address in the
2325dbc760bcSTejun Heo * usual colon-separated hex notation
2326dbc760bcSTejun Heo * - 'm' For a 6-byte MAC address, it prints the hex address without colons
2327dd45c9cfSHarvey Harrison * - 'MF' For a 6-byte MAC FDDI address, it prints the address
2328dd45c9cfSHarvey Harrison * with a dash-separated hex notation
23298a27f7c9SJoe Perches * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
2330bc7259a2SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
2331c8e00060SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
23327c59154eSAndy Shevchenko * IPv6 uses colon separated network-order 16 bit hex with leading 0's
23338a27f7c9SJoe Perches * [S][pfs]
23348a27f7c9SJoe Perches * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
23358a27f7c9SJoe Perches * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
233610679643SDaniel Borkmann * - 'i' [46] for 'raw' IPv4/IPv6 addresses
233710679643SDaniel Borkmann * IPv6 omits the colons (01020304...0f)
233810679643SDaniel Borkmann * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
23398a27f7c9SJoe Perches * [S][pfs]
23408a27f7c9SJoe Perches * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
23418a27f7c9SJoe Perches * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
234210679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
234310679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by
234410679643SDaniel Borkmann * https://tools.ietf.org/html/rfc5952
234510679643SDaniel Borkmann * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
234610679643SDaniel Borkmann * of the following flags (see string_escape_mem() for the
23478eda94bdSAlexander A. Klimov * details):
234871dca95dSAndy Shevchenko * a - ESCAPE_ANY
234971dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL
235071dca95dSAndy Shevchenko * h - ESCAPE_HEX
235171dca95dSAndy Shevchenko * n - ESCAPE_NULL
235271dca95dSAndy Shevchenko * o - ESCAPE_OCTAL
235371dca95dSAndy Shevchenko * p - ESCAPE_NP
235471dca95dSAndy Shevchenko * s - ESCAPE_SPACE
235571dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used.
235671dca95dSAndy Shevchenko * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
235771dca95dSAndy Shevchenko * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
235871dca95dSAndy Shevchenko * Options for %pU are:
23599ac6e44eSJoe Perches * b big endian lower case hex (default)
23609ac6e44eSJoe Perches * B big endian UPPER case hex
23619ac6e44eSJoe Perches * l little endian lower case hex
23629ac6e44eSJoe Perches * L little endian UPPER case hex
23639ac6e44eSJoe Perches * big endian output byte order is:
23649ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
23659ac6e44eSJoe Perches * little endian output byte order is:
23669ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
23679ac6e44eSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *,
23689ac6e44eSJoe Perches * call vsnprintf(->format, *->va_list).
23699ac6e44eSJoe Perches * Implements a "recursive vsnprintf".
23707db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the
23717db6f5fbSJoe Perches * correctness of the format string and va_list arguments.
23727db6f5fbSJoe Perches * - 'K' For a kernel pointer that should be hidden from unprivileged users.
23737db6f5fbSJoe Perches * Use only for procfs, sysfs and similar files, not printk(); please
23747db6f5fbSJoe Perches * read the documentation (path below) first.
2375a48849e2SVlastimil Babka * - 'NF' For a netdev_features_t
2376a48849e2SVlastimil Babka * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value.
2377a48849e2SVlastimil Babka * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
2378c8f44affSMichał Mirosław * a certain separator (' ' by default):
2379af612e43SSakari Ailus * C colon
238031550a16SAndy Shevchenko * D dash
238131550a16SAndy Shevchenko * N no separator
238231550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider
238331550a16SAndy Shevchenko * to use print_hex_dump() for the larger input.
238431550a16SAndy Shevchenko * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
238531550a16SAndy Shevchenko * (default assumed to be phys_addr_t, passed by reference)
238631550a16SAndy Shevchenko * - 'd[234]' For a dentry name (optionally 2-4 last components)
2387aaf07621SJoe Perches * - 'D[234]' Same as 'd' but for a struct file
2388aaf07621SJoe Perches * - 'g' For block_device name (gendisk + partition number)
2389c0d92a57SOlof Johansson * - 't[RT][dt][r][s]' For time and date as represented by:
2390c0d92a57SOlof Johansson * R struct rtc_time
23911031bc58SDmitry Monakhov * T time64_t
239220bc8c1eSAndy Shevchenko * - 'C' For a clock, it prints the name (Common Clock Framework) or address
23934d42c447SAndy Shevchenko * (legacy clock framework) of the clock
23947daac5b2SAndy Shevchenko * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
2395900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock
2396900cca29SGeert Uytterhoeven * - 'G' For flags to be printed as a collection of symbolic strings that would
2397900cca29SGeert Uytterhoeven * construct the specific value. Supported flags given by option:
2398900cca29SGeert Uytterhoeven * p page flags (see struct page) given as pointer to unsigned long
2399edf14cdbSVlastimil Babka * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
2400edf14cdbSVlastimil Babka * v vma flags (VM_*) given as pointer to unsigned long
2401edf14cdbSVlastimil Babka * - 'OF[fnpPcCF]' For a device tree object
2402edf14cdbSVlastimil Babka * Without any optional arguments prints the full_name
2403edf14cdbSVlastimil Babka * f device node full_name
2404ce4fecf1SPantelis Antoniou * n device node name
2405ce4fecf1SPantelis Antoniou * p device node phandle
2406ce4fecf1SPantelis Antoniou * P device node path spec (name + @unit)
2407ce4fecf1SPantelis Antoniou * F device node flags
2408ce4fecf1SPantelis Antoniou * c major compatible string
2409ce4fecf1SPantelis Antoniou * C full compatible string
2410ce4fecf1SPantelis Antoniou * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer
2411ce4fecf1SPantelis Antoniou * Without an option prints the full name of the node
2412ce4fecf1SPantelis Antoniou * f full name
24133bd32d6aSSakari Ailus * P node name, including a possible unit address
24143bd32d6aSSakari Ailus * - 'x' For printing the address unmodified. Equivalent to "%lx".
24153bd32d6aSSakari Ailus * Please read the documentation (path below) before using!
24163bd32d6aSSakari Ailus * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of
2417a48849e2SVlastimil Babka * bpf_trace_printk() where [ku] prefix specifies either kernel (k)
2418a48849e2SVlastimil Babka * or user (u) memory to probe, and:
2419b2a5212fSDaniel Borkmann * s a string, equivalent to "%s" on direct vsnprintf() use
2420b2a5212fSDaniel Borkmann *
2421b2a5212fSDaniel Borkmann * ** When making changes please also update:
2422b2a5212fSDaniel Borkmann * Documentation/core-api/printk-formats.rst
24237b1924a1STobin C. Harding *
2424b3ed2321STobin C. Harding * Note: The default behaviour (unadorned %p) is to hash the address,
2425b3ed2321STobin C. Harding * rendering it useful as a unique identifier.
24269ac6e44eSJoe Perches *
2427ad67b74dSTobin C. Harding * There is also a '%pA' format specifier, but it is only intended to be used
2428ad67b74dSTobin C. Harding * from Rust code to format core::fmt::Arguments. Do *not* use it from C.
2429787983daSGary Guo * See rust/kernel/print.rs for details.
2430787983daSGary Guo */
2431787983daSGary Guo static noinline_for_stack
pointer(const char * fmt,char * buf,char * end,void * ptr,struct printf_spec spec)2432787983daSGary Guo char *pointer(const char *fmt, char *buf, char *end, void *ptr,
24334d8a743cSLinus Torvalds struct printf_spec spec)
2434cf3b429bSJoe Perches {
2435cf3b429bSJoe Perches switch (*fmt) {
2436fef20d9cSFrederic Weisbecker case 'S':
243778a8bf69SLinus Torvalds case 's':
24380fe1ef24SLinus Torvalds ptr = dereference_symbol_descriptor(ptr);
24390fe1ef24SLinus Torvalds fallthrough;
24409ac6e44eSJoe Perches case 'B':
244104b8eb7aSSergey Senozhatsky return symbol_string(buf, end, ptr, spec, fmt);
24424c1ca831SNick Desaulniers case 'R':
24430f77a8d3SNamhyung Kim case 'r':
2444b0d33c2bSJoe Perches return resource_or_range(fmt, buf, end, ptr, spec);
2445332d2e78SLinus Torvalds case 'h':
2446c7dabef8SBjorn Helgaas return hex_string(buf, end, ptr, spec, fmt);
244742619747SIra Weiny case 'b':
244831550a16SAndy Shevchenko switch (fmt[1]) {
244931550a16SAndy Shevchenko case 'l':
2450dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt);
2451dbc760bcSTejun Heo default:
2452dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt);
2453dbc760bcSTejun Heo }
2454dbc760bcSTejun Heo case 'M': /* Colon separated: 00:01:02:03:04:05 */
2455dbc760bcSTejun Heo case 'm': /* Contiguous: 000102030405 */
2456dbc760bcSTejun Heo /* [mM]F (FDDI) */
24578a27f7c9SJoe Perches /* [mM]R (Reverse order; Bluetooth) */
24588a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt);
245976597ff9SAndrei Emeltchenko case 'I': /* Formatted IP supported
246076597ff9SAndrei Emeltchenko * 4: 1.2.3.4
24618a27f7c9SJoe Perches * 6: 0001:0203:...:0708
24628a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4
24638a27f7c9SJoe Perches */
24648a27f7c9SJoe Perches case 'i': /* Contiguous:
24658a27f7c9SJoe Perches * 4: 001.002.003.004
24668a27f7c9SJoe Perches * 6: 000102...0f
24678a27f7c9SJoe Perches */
24688a27f7c9SJoe Perches return ip_addr_string(buf, end, ptr, spec, fmt);
24698a27f7c9SJoe Perches case 'E':
24708a27f7c9SJoe Perches return escaped_string(buf, end, ptr, spec, fmt);
2471f00cc102SPetr Mladek case 'U':
247271dca95dSAndy Shevchenko return uuid_string(buf, end, ptr, spec, fmt);
247371dca95dSAndy Shevchenko case 'V':
24749ac6e44eSJoe Perches return va_format(buf, end, ptr, spec);
24759ac6e44eSJoe Perches case 'K':
24767db6f5fbSJoe Perches return restricted_pointer(buf, end, ptr, spec);
2477a1aea76aSAndy Shevchenko case 'N':
2478455cd5abSDan Rosenberg return netdev_bits(buf, end, ptr, spec, fmt);
247957e73442STobin C. Harding case '4':
2480c8f44affSMichał Mirosław return fourcc_string(buf, end, ptr, spec, fmt);
2481431bca24SGeert Uytterhoeven case 'a':
2482af612e43SSakari Ailus return address_val(buf, end, ptr, spec, fmt);
2483af612e43SSakari Ailus case 'd':
24847d799210SStepan Moskovchenko return dentry_name(buf, end, ptr, spec, fmt);
24853e5903ebSPetr Mladek case 't':
24864b6ccca7SAl Viro return time_and_date(buf, end, ptr, spec, fmt);
24874b6ccca7SAl Viro case 'C':
24884d42c447SAndy Shevchenko return clock(buf, end, ptr, spec, fmt);
24894d42c447SAndy Shevchenko case 'D':
2490900cca29SGeert Uytterhoeven return file_dentry_name(buf, end, ptr, spec, fmt);
2491900cca29SGeert Uytterhoeven #ifdef CONFIG_BLOCK
24924b6ccca7SAl Viro case 'g':
249336594b31SJia He return bdev_name(buf, end, ptr, spec, fmt);
24941031bc58SDmitry Monakhov #endif
24951031bc58SDmitry Monakhov
24961031bc58SDmitry Monakhov case 'G':
24971031bc58SDmitry Monakhov return flags_string(buf, end, ptr, spec, fmt);
24981031bc58SDmitry Monakhov case 'O':
2499edf14cdbSVlastimil Babka return device_node_string(buf, end, ptr, spec, fmt + 1);
25000b74d4d7SPetr Mladek case 'f':
2501ce4fecf1SPantelis Antoniou return fwnode_string(buf, end, ptr, spec, fmt + 1);
250283abc5a7SSakari Ailus case 'A':
25033bd32d6aSSakari Ailus if (!IS_ENABLED(CONFIG_RUST)) {
25043bd32d6aSSakari Ailus WARN_ONCE(1, "Please remove %%pA from non-Rust code\n");
2505787983daSGary Guo return error_string(buf, end, "(%pA?)", spec);
2506787983daSGary Guo }
2507787983daSGary Guo return rust_fmt_argument(buf, end, ptr);
2508787983daSGary Guo case 'x':
2509787983daSGary Guo return pointer_string(buf, end, ptr, spec);
2510787983daSGary Guo case 'e':
25117b1924a1STobin C. Harding /* %pe with a non-ERR_PTR gets treated as plain %p */
25127b1924a1STobin C. Harding if (!IS_ERR(ptr))
251357f5677eSRasmus Villemoes return default_pointer(buf, end, ptr, spec);
251457f5677eSRasmus Villemoes return err_ptr(buf, end, ptr, spec);
251557f5677eSRasmus Villemoes case 'u':
251684842911SChristophe Leroy case 'k':
251757f5677eSRasmus Villemoes switch (fmt[1]) {
2518b2a5212fSDaniel Borkmann case 's':
2519b2a5212fSDaniel Borkmann return string(buf, end, ptr, spec);
2520b2a5212fSDaniel Borkmann default:
2521b2a5212fSDaniel Borkmann return error_string(buf, end, "(einval)", spec);
2522b2a5212fSDaniel Borkmann }
2523b2a5212fSDaniel Borkmann default:
2524b2a5212fSDaniel Borkmann return default_pointer(buf, end, ptr, spec);
2525b2a5212fSDaniel Borkmann }
252684842911SChristophe Leroy }
252784842911SChristophe Leroy
25280fe1ef24SLinus Torvalds struct fmt {
2529fef20d9cSFrederic Weisbecker const char *str;
2530fef20d9cSFrederic Weisbecker unsigned char state; // enum format_state
2531938df695SLinus Torvalds unsigned char size; // size of numbers
2532938df695SLinus Torvalds };
25338d4826ccSLinus Torvalds
25348d4826ccSLinus Torvalds #define SPEC_CHAR(x, flag) [(x)-32] = flag
spec_flag(unsigned char c)2535938df695SLinus Torvalds static unsigned char spec_flag(unsigned char c)
2536938df695SLinus Torvalds {
2537312f48b2SLinus Torvalds static const unsigned char spec_flag_array[] = {
2538312f48b2SLinus Torvalds SPEC_CHAR(' ', SPACE),
2539312f48b2SLinus Torvalds SPEC_CHAR('#', SPECIAL),
2540312f48b2SLinus Torvalds SPEC_CHAR('+', PLUS),
2541312f48b2SLinus Torvalds SPEC_CHAR('-', LEFT),
2542312f48b2SLinus Torvalds SPEC_CHAR('0', ZEROPAD),
2543312f48b2SLinus Torvalds };
2544312f48b2SLinus Torvalds c -= 32;
2545312f48b2SLinus Torvalds return (c < sizeof(spec_flag_array)) ? spec_flag_array[c] : 0;
2546312f48b2SLinus Torvalds }
2547312f48b2SLinus Torvalds
2548312f48b2SLinus Torvalds /*
2549312f48b2SLinus Torvalds * Helper function to decode printf style format.
2550312f48b2SLinus Torvalds * Each call decode a token from the format and return the
2551fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants
2552fef20d9cSFrederic Weisbecker * to go on the next call).
2553fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters
2554fef20d9cSFrederic Weisbecker *
2555fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields
2556fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H.
2557fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99
2558fef20d9cSFrederic Weisbecker * 'Z' changed to 'z' --adobriyan 2017-01-25
2559fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t
2560fef20d9cSFrederic Weisbecker *
25615b5e0928SAlexey Dobriyan * @fmt: the format string
2562fef20d9cSFrederic Weisbecker * @type of the token returned
2563fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens..
2564fef20d9cSFrederic Weisbecker * @field_width: overwritten width
2565fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...)
2566fef20d9cSFrederic Weisbecker * @precision: precision of a number
2567fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...)
2568fef20d9cSFrederic Weisbecker */
2569fef20d9cSFrederic Weisbecker static noinline_for_stack
format_decode(struct fmt fmt,struct printf_spec * spec)2570fef20d9cSFrederic Weisbecker struct fmt format_decode(struct fmt fmt, struct printf_spec *spec)
2571fef20d9cSFrederic Weisbecker {
2572cf3b429bSJoe Perches const char *start = fmt.str;
2573938df695SLinus Torvalds char flag;
2574fef20d9cSFrederic Weisbecker
2575938df695SLinus Torvalds /* we finished early by reading the field width */
2576614d1346SLinus Torvalds if (unlikely(fmt.state == FORMAT_STATE_WIDTH)) {
2577fef20d9cSFrederic Weisbecker if (spec->field_width < 0) {
2578fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width;
25792b76e39fSLinus Torvalds spec->flags |= LEFT;
2580fef20d9cSFrederic Weisbecker }
2581fef20d9cSFrederic Weisbecker fmt.state = FORMAT_STATE_NONE;
2582fef20d9cSFrederic Weisbecker goto precision;
2583fef20d9cSFrederic Weisbecker }
2584938df695SLinus Torvalds
2585fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */
2586fef20d9cSFrederic Weisbecker if (unlikely(fmt.state == FORMAT_STATE_PRECISION)) {
2587fef20d9cSFrederic Weisbecker if (spec->precision < 0)
2588fef20d9cSFrederic Weisbecker spec->precision = 0;
25892b76e39fSLinus Torvalds
2590fef20d9cSFrederic Weisbecker fmt.state = FORMAT_STATE_NONE;
2591fef20d9cSFrederic Weisbecker goto qualifier;
2592fef20d9cSFrederic Weisbecker }
2593938df695SLinus Torvalds
2594fef20d9cSFrederic Weisbecker /* By default */
2595fef20d9cSFrederic Weisbecker fmt.state = FORMAT_STATE_NONE;
2596fef20d9cSFrederic Weisbecker
2597fef20d9cSFrederic Weisbecker for (; *fmt.str ; fmt.str++) {
2598938df695SLinus Torvalds if (*fmt.str == '%')
2599fef20d9cSFrederic Weisbecker break;
2600938df695SLinus Torvalds }
2601938df695SLinus Torvalds
2602fef20d9cSFrederic Weisbecker /* Return the current non-format string */
2603fef20d9cSFrederic Weisbecker if (fmt.str != start || !*fmt.str)
2604fef20d9cSFrederic Weisbecker return fmt;
2605fef20d9cSFrederic Weisbecker
2606938df695SLinus Torvalds /* Process flags. This also skips the first '%' */
26079e0e6d8aSLinus Torvalds spec->flags = 0;
2608fef20d9cSFrederic Weisbecker do {
2609312f48b2SLinus Torvalds /* this also skips first '%' */
2610fef20d9cSFrederic Weisbecker flag = spec_flag(*++fmt.str);
2611312f48b2SLinus Torvalds spec->flags |= flag;
2612312f48b2SLinus Torvalds } while (flag);
2613312f48b2SLinus Torvalds
2614312f48b2SLinus Torvalds /* get field width */
2615312f48b2SLinus Torvalds spec->field_width = -1;
2616fef20d9cSFrederic Weisbecker
2617fef20d9cSFrederic Weisbecker if (isdigit(*fmt.str))
2618fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt.str);
2619fef20d9cSFrederic Weisbecker else if (unlikely(*fmt.str == '*')) {
2620938df695SLinus Torvalds /* it's the next argument */
2621938df695SLinus Torvalds fmt.state = FORMAT_STATE_WIDTH;
26222b76e39fSLinus Torvalds fmt.str++;
2623fef20d9cSFrederic Weisbecker return fmt;
2624938df695SLinus Torvalds }
2625938df695SLinus Torvalds
2626938df695SLinus Torvalds precision:
2627fef20d9cSFrederic Weisbecker /* get the precision */
2628fef20d9cSFrederic Weisbecker spec->precision = -1;
2629fef20d9cSFrederic Weisbecker if (unlikely(*fmt.str == '.')) {
2630fef20d9cSFrederic Weisbecker fmt.str++;
2631fef20d9cSFrederic Weisbecker if (isdigit(*fmt.str)) {
26322b76e39fSLinus Torvalds spec->precision = skip_atoi(&fmt.str);
2633938df695SLinus Torvalds if (spec->precision < 0)
2634938df695SLinus Torvalds spec->precision = 0;
2635938df695SLinus Torvalds } else if (*fmt.str == '*') {
2636fef20d9cSFrederic Weisbecker /* it's the next argument */
2637fef20d9cSFrederic Weisbecker fmt.state = FORMAT_STATE_PRECISION;
2638938df695SLinus Torvalds fmt.str++;
2639fef20d9cSFrederic Weisbecker return fmt;
2640938df695SLinus Torvalds }
2641938df695SLinus Torvalds }
2642938df695SLinus Torvalds
2643fef20d9cSFrederic Weisbecker qualifier:
2644fef20d9cSFrederic Weisbecker /* Set up default numeric format */
2645fef20d9cSFrederic Weisbecker spec->base = 10;
2646fef20d9cSFrederic Weisbecker fmt.state = FORMAT_STATE_NUM;
2647614d1346SLinus Torvalds fmt.size = sizeof(int);
2648fef20d9cSFrederic Weisbecker static const struct format_state {
26498d4826ccSLinus Torvalds unsigned char state;
26508d4826ccSLinus Torvalds unsigned char size;
2651614d1346SLinus Torvalds unsigned char flags_or_double_size;
2652614d1346SLinus Torvalds unsigned char base;
26538d4826ccSLinus Torvalds } lookup_state[256] = {
26548d4826ccSLinus Torvalds // Length
2655614d1346SLinus Torvalds ['l'] = { 0, sizeof(long), sizeof(long long) },
2656614d1346SLinus Torvalds ['L'] = { 0, sizeof(long long) },
26578d4826ccSLinus Torvalds ['h'] = { 0, sizeof(short), sizeof(char) },
26588d4826ccSLinus Torvalds ['H'] = { 0, sizeof(char) }, // Questionable historical
26598d4826ccSLinus Torvalds ['z'] = { 0, sizeof(size_t) },
26608d4826ccSLinus Torvalds ['t'] = { 0, sizeof(ptrdiff_t) },
26618d4826ccSLinus Torvalds
26628d4826ccSLinus Torvalds // Non-numeric formats
26638d4826ccSLinus Torvalds ['c'] = { FORMAT_STATE_CHAR },
2664fef20d9cSFrederic Weisbecker ['s'] = { FORMAT_STATE_STR },
2665614d1346SLinus Torvalds ['p'] = { FORMAT_STATE_PTR },
2666614d1346SLinus Torvalds ['%'] = { FORMAT_STATE_PERCENT_CHAR },
2667614d1346SLinus Torvalds
2668614d1346SLinus Torvalds // Numerics
2669614d1346SLinus Torvalds ['o'] = { FORMAT_STATE_NUM, 0, 0, 8 },
2670fef20d9cSFrederic Weisbecker ['x'] = { FORMAT_STATE_NUM, 0, SMALL, 16 },
2671614d1346SLinus Torvalds ['X'] = { FORMAT_STATE_NUM, 0, 0, 16 },
26728d4826ccSLinus Torvalds ['d'] = { FORMAT_STATE_NUM, 0, SIGN, 10 },
26738d4826ccSLinus Torvalds ['i'] = { FORMAT_STATE_NUM, 0, SIGN, 10 },
26748d4826ccSLinus Torvalds ['u'] = { FORMAT_STATE_NUM, 0, 0, 10, },
26758d4826ccSLinus Torvalds
26768d4826ccSLinus Torvalds /*
26778d4826ccSLinus Torvalds * Since %n poses a greater security risk than
2678fef20d9cSFrederic Weisbecker * utility, treat it as any other invalid or
2679708d96fdSRyan Mallon * unsupported format specifier.
2680b006f19bSRasmus Villemoes */
2681b006f19bSRasmus Villemoes };
2682b006f19bSRasmus Villemoes
2683708d96fdSRyan Mallon const struct format_state *p = lookup_state + (u8)*fmt.str;
2684614d1346SLinus Torvalds if (p->size) {
2685708d96fdSRyan Mallon fmt.size = p->size;
2686614d1346SLinus Torvalds if (p->flags_or_double_size && fmt.str[0] == fmt.str[1]) {
26878d4826ccSLinus Torvalds fmt.size = p->flags_or_double_size;
26888d4826ccSLinus Torvalds fmt.str++;
26898d4826ccSLinus Torvalds }
26908d4826ccSLinus Torvalds fmt.str++;
2691614d1346SLinus Torvalds p = lookup_state + *fmt.str;
2692614d1346SLinus Torvalds }
2693614d1346SLinus Torvalds if (p->state) {
2694614d1346SLinus Torvalds if (p->base)
2695614d1346SLinus Torvalds spec->base = p->base;
2696614d1346SLinus Torvalds spec->flags |= p->flags_or_double_size;
2697ecdc475eSLinus Torvalds fmt.state = p->state;
26988d4826ccSLinus Torvalds fmt.str++;
26998d4826ccSLinus Torvalds return fmt;
2700614d1346SLinus Torvalds }
2701614d1346SLinus Torvalds
27029e0e6d8aSLinus Torvalds WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt.str);
2703fef20d9cSFrederic Weisbecker fmt.state = FORMAT_STATE_INVALID;
2704fef20d9cSFrederic Weisbecker return fmt;
2705614d1346SLinus Torvalds }
2706614d1346SLinus Torvalds
2707938df695SLinus Torvalds static void
set_field_width(struct printf_spec * spec,int width)270878a8bf69SLinus Torvalds set_field_width(struct printf_spec *spec, int width)
270978a8bf69SLinus Torvalds {
27104d72ba01SRasmus Villemoes spec->field_width = width;
27114d72ba01SRasmus Villemoes if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
27124d72ba01SRasmus Villemoes spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
27134d72ba01SRasmus Villemoes }
27144d72ba01SRasmus Villemoes }
27154d72ba01SRasmus Villemoes
27164d72ba01SRasmus Villemoes static void
set_precision(struct printf_spec * spec,int prec)27174d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec)
27184d72ba01SRasmus Villemoes {
27194d72ba01SRasmus Villemoes spec->precision = prec;
27204d72ba01SRasmus Villemoes if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
27214d72ba01SRasmus Villemoes spec->precision = clamp(prec, 0, PRECISION_MAX);
27224d72ba01SRasmus Villemoes }
27234d72ba01SRasmus Villemoes }
27244d72ba01SRasmus Villemoes
27254d72ba01SRasmus Villemoes /*
27264d72ba01SRasmus Villemoes * Turn a 1/2/4-byte value into a 64-bit one for printing: truncate
27274d72ba01SRasmus Villemoes * as necessary and deal with signedness.
2728be503db4SLinus Torvalds *
2729be503db4SLinus Torvalds * 'size' is the size of the value in bytes.
2730be503db4SLinus Torvalds */
convert_num_spec(unsigned int val,int size,struct printf_spec spec)2731be503db4SLinus Torvalds static unsigned long long convert_num_spec(unsigned int val, int size, struct printf_spec spec)
2732938df695SLinus Torvalds {
2733be503db4SLinus Torvalds unsigned int shift = 32 - size*8;
2734938df695SLinus Torvalds
2735be503db4SLinus Torvalds val <<= shift;
2736938df695SLinus Torvalds if (!(spec.flags & SIGN))
2737be503db4SLinus Torvalds return val >> shift;
2738be503db4SLinus Torvalds return (int)val >> shift;
2739be503db4SLinus Torvalds }
2740be503db4SLinus Torvalds
2741be503db4SLinus Torvalds /**
2742be503db4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer
2743be503db4SLinus Torvalds * @buf: The buffer to place the result into
27441da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space
27451da177e4SLinus Torvalds * @fmt_str: The format string to use
27461da177e4SLinus Torvalds * @args: Arguments for the format string
27471da177e4SLinus Torvalds *
2748fa47906fSLinus Torvalds * This function generally follows C99 vsnprintf, but has some
27491da177e4SLinus Torvalds * extensions and a few limitations:
27501da177e4SLinus Torvalds *
2751d7ec9a05SRasmus Villemoes * - ``%n`` is unsupported
2752d7ec9a05SRasmus Villemoes * - ``%p*`` is handled by pointer()
2753d7ec9a05SRasmus Villemoes *
27546cc89134S[email protected] * See pointer() or Documentation/core-api/printk-formats.rst for more
27556cc89134S[email protected] * extensive description.
275620036fdcSAndi Kleen *
275727e7c0e8SJonathan Corbet * **Please update the documentation in both places when making changes**
27585e4ee7b1SMartin Kletzander *
27595e4ee7b1SMartin Kletzander * The return value is the number of characters which would
27605e4ee7b1SMartin Kletzander * be generated for the given input, excluding the trailing
276180f548e0SAndrew Morton * '\0', as per ISO C99. If you want to have the exact
27621da177e4SLinus Torvalds * number of characters written into @buf as return value
27631da177e4SLinus Torvalds * (not including the trailing '\0'), use vscnprintf(). If the
27641da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting
27651da177e4SLinus Torvalds * string is truncated.
276672fd4a35SRobert P. J. Day *
27671da177e4SLinus Torvalds * If you're not already dealing with a va_list consider using snprintf().
27681da177e4SLinus Torvalds */
vsnprintf(char * buf,size_t size,const char * fmt_str,va_list args)27691da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt_str, va_list args)
2770ba1835ebSUwe Kleine-König {
27711da177e4SLinus Torvalds char *str, *end;
2772938df695SLinus Torvalds struct printf_spec spec = {0};
27731da177e4SLinus Torvalds struct fmt fmt = {
2774d4be151bSAndré Goddard Rosa .str = fmt_str,
2775fef20d9cSFrederic Weisbecker .state = FORMAT_STATE_NONE,
2776938df695SLinus Torvalds };
2777938df695SLinus Torvalds
2778938df695SLinus Torvalds /* Reject out-of-range values early. Large positive sizes are
2779938df695SLinus Torvalds used for unknown buffer sizes. */
27801da177e4SLinus Torvalds if (WARN_ON_ONCE(size > INT_MAX))
2781f796937aSJeremy Fitzhardinge return 0;
2782f796937aSJeremy Fitzhardinge
27832aa2f9e2SRasmus Villemoes str = buf;
27841da177e4SLinus Torvalds end = buf + size;
27851da177e4SLinus Torvalds
27861da177e4SLinus Torvalds /* Make sure end is always >= buf */
2787f796937aSJeremy Fitzhardinge if (end < buf) {
27881da177e4SLinus Torvalds end = ((void *)-1);
2789f796937aSJeremy Fitzhardinge size = end - buf;
2790f796937aSJeremy Fitzhardinge }
27911da177e4SLinus Torvalds
2792f796937aSJeremy Fitzhardinge while (*fmt.str) {
27931da177e4SLinus Torvalds const char *old_fmt = fmt.str;
27941da177e4SLinus Torvalds
2795938df695SLinus Torvalds fmt = format_decode(fmt, &spec);
2796938df695SLinus Torvalds
2797fef20d9cSFrederic Weisbecker switch (fmt.state) {
27989e0e6d8aSLinus Torvalds case FORMAT_STATE_NONE: {
2799fef20d9cSFrederic Weisbecker int read = fmt.str - old_fmt;
2800938df695SLinus Torvalds if (str < end) {
2801938df695SLinus Torvalds int copy = read;
2802938df695SLinus Torvalds if (copy > end - str)
2803fef20d9cSFrederic Weisbecker copy = end - str;
28049e0e6d8aSLinus Torvalds memcpy(str, old_fmt, copy);
2805fef20d9cSFrederic Weisbecker }
2806fef20d9cSFrederic Weisbecker str += read;
2807fef20d9cSFrederic Weisbecker continue;
2808fef20d9cSFrederic Weisbecker }
2809fef20d9cSFrederic Weisbecker
281003d23941SLinus Torvalds case FORMAT_STATE_NUM: {
28111da177e4SLinus Torvalds unsigned long long num;
28121da177e4SLinus Torvalds if (fmt.size <= sizeof(int))
28138d4826ccSLinus Torvalds num = convert_num_spec(va_arg(args, int), fmt.size, spec);
28148d4826ccSLinus Torvalds else
28158d4826ccSLinus Torvalds num = va_arg(args, long long);
28168d4826ccSLinus Torvalds str = number(str, end, num, spec);
28178d4826ccSLinus Torvalds continue;
28188d4826ccSLinus Torvalds }
28198d4826ccSLinus Torvalds
28208d4826ccSLinus Torvalds case FORMAT_STATE_WIDTH:
28218d4826ccSLinus Torvalds set_field_width(&spec, va_arg(args, int));
28228d4826ccSLinus Torvalds continue;
2823938df695SLinus Torvalds
28244d72ba01SRasmus Villemoes case FORMAT_STATE_PRECISION:
282503d23941SLinus Torvalds set_precision(&spec, va_arg(args, int));
28261da177e4SLinus Torvalds continue;
2827938df695SLinus Torvalds
28284d72ba01SRasmus Villemoes case FORMAT_STATE_CHAR: {
282903d23941SLinus Torvalds char c;
28301da177e4SLinus Torvalds
2831938df695SLinus Torvalds if (!(spec.flags & LEFT)) {
2832d4be151bSAndré Goddard Rosa while (--spec.field_width > 0) {
2833d4be151bSAndré Goddard Rosa if (str < end)
2834fef20d9cSFrederic Weisbecker *str = ' ';
2835fef20d9cSFrederic Weisbecker ++str;
2836f796937aSJeremy Fitzhardinge
28371da177e4SLinus Torvalds }
28381da177e4SLinus Torvalds }
2839fef20d9cSFrederic Weisbecker c = (unsigned char) va_arg(args, int);
28401da177e4SLinus Torvalds if (str < end)
28411da177e4SLinus Torvalds *str = c;
28421da177e4SLinus Torvalds ++str;
2843f796937aSJeremy Fitzhardinge while (--spec.field_width > 0) {
28441da177e4SLinus Torvalds if (str < end)
28451da177e4SLinus Torvalds *str = ' ';
2846fef20d9cSFrederic Weisbecker ++str;
2847f796937aSJeremy Fitzhardinge }
28481da177e4SLinus Torvalds continue;
28491da177e4SLinus Torvalds }
28501da177e4SLinus Torvalds
285103d23941SLinus Torvalds case FORMAT_STATE_STR:
2852d4be151bSAndré Goddard Rosa str = string(str, end, va_arg(args, char *), spec);
28531da177e4SLinus Torvalds continue;
2854938df695SLinus Torvalds
2855fef20d9cSFrederic Weisbecker case FORMAT_STATE_PTR:
285603d23941SLinus Torvalds str = pointer(fmt.str, str, end, va_arg(args, void *),
28571da177e4SLinus Torvalds spec);
2858938df695SLinus Torvalds while (isalnum(*fmt.str))
2859938df695SLinus Torvalds fmt.str++;
2860fef20d9cSFrederic Weisbecker continue;
2861938df695SLinus Torvalds
2862938df695SLinus Torvalds case FORMAT_STATE_PERCENT_CHAR:
286303d23941SLinus Torvalds if (str < end)
28641da177e4SLinus Torvalds *str = '%';
2865938df695SLinus Torvalds ++str;
2866f796937aSJeremy Fitzhardinge continue;
28671da177e4SLinus Torvalds
28681da177e4SLinus Torvalds default:
286903d23941SLinus Torvalds /*
28701da177e4SLinus Torvalds * Presumably the arguments passed gcc's type
28718d4826ccSLinus Torvalds * checking, but there is no safe or sane way
2872b006f19bSRasmus Villemoes * for us to continue parsing the format and
2873b006f19bSRasmus Villemoes * fetching from the va_list; the remaining
2874b006f19bSRasmus Villemoes * specifiers and arguments would be out of
2875b006f19bSRasmus Villemoes * sync.
2876b006f19bSRasmus Villemoes */
2877b006f19bSRasmus Villemoes goto out;
2878b006f19bSRasmus Villemoes }
2879b006f19bSRasmus Villemoes }
2880b006f19bSRasmus Villemoes
288103d23941SLinus Torvalds out:
28821da177e4SLinus Torvalds if (size > 0) {
2883fef20d9cSFrederic Weisbecker if (str < end)
2884b006f19bSRasmus Villemoes *str = '\0';
2885f796937aSJeremy Fitzhardinge else
2886f796937aSJeremy Fitzhardinge end[-1] = '\0';
28871da177e4SLinus Torvalds }
2888f796937aSJeremy Fitzhardinge
28890a6047eeSLinus Torvalds /* the trailing null byte doesn't count towards the total */
2890f796937aSJeremy Fitzhardinge return str-buf;
2891fef20d9cSFrederic Weisbecker
2892f796937aSJeremy Fitzhardinge }
28931da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf);
2894fef20d9cSFrederic Weisbecker
28951da177e4SLinus Torvalds /**
28961da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer
28971da177e4SLinus Torvalds * @buf: The buffer to place the result into
28981da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space
28991da177e4SLinus Torvalds * @fmt: The format string to use
29001da177e4SLinus Torvalds * @args: Arguments for the format string
29011da177e4SLinus Torvalds *
29021da177e4SLinus Torvalds * The return value is the number of characters which have been written into
29031da177e4SLinus Torvalds * the @buf not including the trailing '\0'. If @size is == 0 the function
29041da177e4SLinus Torvalds * returns 0.
29051da177e4SLinus Torvalds *
2906b921c69fSAnton Arapov * If you're not already dealing with a va_list consider using scnprintf().
29071da177e4SLinus Torvalds *
29081da177e4SLinus Torvalds * See the vsnprintf() documentation for format string extensions over C99.
2909ba1835ebSUwe Kleine-König */
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)291020036fdcSAndi Kleen int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
291120036fdcSAndi Kleen {
29121da177e4SLinus Torvalds int i;
29131da177e4SLinus Torvalds
29141da177e4SLinus Torvalds if (unlikely(!size))
29151da177e4SLinus Torvalds return 0;
29161da177e4SLinus Torvalds
2917ef62c8ffSWaiman Long i = vsnprintf(buf, size, fmt, args);
2918ef62c8ffSWaiman Long
2919ef62c8ffSWaiman Long if (likely(i < size))
29201da177e4SLinus Torvalds return i;
29217b9186f5SAndré Goddard Rosa
2922b921c69fSAnton Arapov return size - 1;
2923b921c69fSAnton Arapov }
2924ef62c8ffSWaiman Long EXPORT_SYMBOL(vscnprintf);
2925b921c69fSAnton Arapov
29261da177e4SLinus Torvalds /**
29271da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer
29281da177e4SLinus Torvalds * @buf: The buffer to place the result into
29291da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space
29301da177e4SLinus Torvalds * @fmt: The format string to use
29311da177e4SLinus Torvalds * @...: Arguments for the format string
29321da177e4SLinus Torvalds *
29331da177e4SLinus Torvalds * The return value is the number of characters which would be
29341da177e4SLinus Torvalds * generated for the given input, excluding the trailing null,
29351da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to
29361da177e4SLinus Torvalds * @size, the resulting string is truncated.
29371da177e4SLinus Torvalds *
29381da177e4SLinus Torvalds * See the vsnprintf() documentation for format string extensions over C99.
29391da177e4SLinus Torvalds */
snprintf(char * buf,size_t size,const char * fmt,...)294020036fdcSAndi Kleen int snprintf(char *buf, size_t size, const char *fmt, ...)
294120036fdcSAndi Kleen {
29421da177e4SLinus Torvalds va_list args;
29431da177e4SLinus Torvalds int i;
29441da177e4SLinus Torvalds
29451da177e4SLinus Torvalds va_start(args, fmt);
29461da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args);
29471da177e4SLinus Torvalds va_end(args);
29481da177e4SLinus Torvalds
29491da177e4SLinus Torvalds return i;
29501da177e4SLinus Torvalds }
29517b9186f5SAndré Goddard Rosa EXPORT_SYMBOL(snprintf);
29521da177e4SLinus Torvalds
29531da177e4SLinus Torvalds /**
29541da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer
29551da177e4SLinus Torvalds * @buf: The buffer to place the result into
29561da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space
29571da177e4SLinus Torvalds * @fmt: The format string to use
29581da177e4SLinus Torvalds * @...: Arguments for the format string
29591da177e4SLinus Torvalds *
29601da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including
29611da177e4SLinus Torvalds * the trailing '\0'. If @size is == 0 the function returns 0.
29621da177e4SLinus Torvalds */
29631da177e4SLinus Torvalds
scnprintf(char * buf,size_t size,const char * fmt,...)2964b903c0b8SChangli Gao int scnprintf(char *buf, size_t size, const char *fmt, ...)
29651da177e4SLinus Torvalds {
29661da177e4SLinus Torvalds va_list args;
29671da177e4SLinus Torvalds int i;
29681da177e4SLinus Torvalds
29691da177e4SLinus Torvalds va_start(args, fmt);
29701da177e4SLinus Torvalds i = vscnprintf(buf, size, fmt, args);
29711da177e4SLinus Torvalds va_end(args);
29721da177e4SLinus Torvalds
2973b921c69fSAnton Arapov return i;
29741da177e4SLinus Torvalds }
29757b9186f5SAndré Goddard Rosa EXPORT_SYMBOL(scnprintf);
2976b903c0b8SChangli Gao
29771da177e4SLinus Torvalds /**
29781da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer
29791da177e4SLinus Torvalds * @buf: The buffer to place the result into
29801da177e4SLinus Torvalds * @fmt: The format string to use
29811da177e4SLinus Torvalds * @args: Arguments for the format string
29821da177e4SLinus Torvalds *
29831da177e4SLinus Torvalds * The function returns the number of characters written
29841da177e4SLinus Torvalds * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
29851da177e4SLinus Torvalds * buffer overflows.
29861da177e4SLinus Torvalds *
298772fd4a35SRobert P. J. Day * If you're not already dealing with a va_list consider using sprintf().
29881da177e4SLinus Torvalds *
29891da177e4SLinus Torvalds * See the vsnprintf() documentation for format string extensions over C99.
2990ba1835ebSUwe Kleine-König */
vsprintf(char * buf,const char * fmt,va_list args)299120036fdcSAndi Kleen int vsprintf(char *buf, const char *fmt, va_list args)
299220036fdcSAndi Kleen {
29931da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args);
29941da177e4SLinus Torvalds }
29951da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf);
29961da177e4SLinus Torvalds
29971da177e4SLinus Torvalds /**
29981da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer
29991da177e4SLinus Torvalds * @buf: The buffer to place the result into
30001da177e4SLinus Torvalds * @fmt: The format string to use
30011da177e4SLinus Torvalds * @...: Arguments for the format string
30021da177e4SLinus Torvalds *
30031da177e4SLinus Torvalds * The function returns the number of characters written
30041da177e4SLinus Torvalds * into @buf. Use snprintf() or scnprintf() in order to avoid
30051da177e4SLinus Torvalds * buffer overflows.
30061da177e4SLinus Torvalds *
300772fd4a35SRobert P. J. Day * See the vsnprintf() documentation for format string extensions over C99.
30081da177e4SLinus Torvalds */
sprintf(char * buf,const char * fmt,...)300920036fdcSAndi Kleen int sprintf(char *buf, const char *fmt, ...)
301020036fdcSAndi Kleen {
30111da177e4SLinus Torvalds va_list args;
30121da177e4SLinus Torvalds int i;
30131da177e4SLinus Torvalds
30141da177e4SLinus Torvalds va_start(args, fmt);
30151da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args);
30161da177e4SLinus Torvalds va_end(args);
30171da177e4SLinus Torvalds
30181da177e4SLinus Torvalds return i;
30191da177e4SLinus Torvalds }
30207b9186f5SAndré Goddard Rosa EXPORT_SYMBOL(sprintf);
30211da177e4SLinus Torvalds
30221da177e4SLinus Torvalds #ifdef CONFIG_BINARY_PRINTF
30231da177e4SLinus Torvalds /*
30241da177e4SLinus Torvalds * bprintf service:
30254370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data
30264370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string
30274370aa4aSLai Jiangshan */
30284370aa4aSLai Jiangshan
30294370aa4aSLai Jiangshan /**
30304370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer
30314370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value
30324370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters)
30334370aa4aSLai Jiangshan * @fmt_str: The format string to use
30344370aa4aSLai Jiangshan * @args: Arguments for the format string
30354370aa4aSLai Jiangshan *
3036fa47906fSLinus Torvalds * The format follows C99 vsnprintf, except %n is ignored, and its argument
30374370aa4aSLai Jiangshan * is skipped.
30384370aa4aSLai Jiangshan *
30394370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for
3040da3dae54SMasanari Iida * the given input.
30414370aa4aSLai Jiangshan *
30424370aa4aSLai Jiangshan * NOTE:
30434370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT
30444370aa4aSLai Jiangshan * valid for bstr_printf().
30454370aa4aSLai Jiangshan */
vbin_printf(u32 * bin_buf,size_t size,const char * fmt_str,va_list args)30464370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt_str, va_list args)
30474370aa4aSLai Jiangshan {
30484370aa4aSLai Jiangshan struct fmt fmt = {
3049938df695SLinus Torvalds .str = fmt_str,
30504370aa4aSLai Jiangshan .state = FORMAT_STATE_NONE,
3051938df695SLinus Torvalds };
3052938df695SLinus Torvalds struct printf_spec spec = {0};
3053938df695SLinus Torvalds char *str, *end;
3054938df695SLinus Torvalds int width;
3055fef20d9cSFrederic Weisbecker
30564370aa4aSLai Jiangshan str = (char *)bin_buf;
3057841a915dSSteven Rostedt (VMware) end = (char *)(bin_buf + size);
30584370aa4aSLai Jiangshan
30594370aa4aSLai Jiangshan #define save_arg(type) \
30604370aa4aSLai Jiangshan ({ \
30614370aa4aSLai Jiangshan unsigned long long value; \
30624370aa4aSLai Jiangshan if (sizeof(type) == 8) { \
3063841a915dSSteven Rostedt (VMware) unsigned long long val8; \
30644370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \
3065841a915dSSteven Rostedt (VMware) val8 = va_arg(args, unsigned long long); \
3066841a915dSSteven Rostedt (VMware) if (str + sizeof(type) <= end) { \
30674370aa4aSLai Jiangshan *(u32 *)str = *(u32 *)&val8; \
3068841a915dSSteven Rostedt (VMware) *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \
30694370aa4aSLai Jiangshan } \
3070841a915dSSteven Rostedt (VMware) value = val8; \
3071841a915dSSteven Rostedt (VMware) } else { \
30724370aa4aSLai Jiangshan unsigned int val4; \
3073841a915dSSteven Rostedt (VMware) str = PTR_ALIGN(str, sizeof(type)); \
30744370aa4aSLai Jiangshan val4 = va_arg(args, int); \
3075841a915dSSteven Rostedt (VMware) if (str + sizeof(type) <= end) \
30764370aa4aSLai Jiangshan *(typeof(type) *)str = (type)(long)val4; \
3077841a915dSSteven Rostedt (VMware) value = (unsigned long long)val4; \
30784370aa4aSLai Jiangshan } \
3079841a915dSSteven Rostedt (VMware) str += sizeof(type); \
3080841a915dSSteven Rostedt (VMware) value; \
30814370aa4aSLai Jiangshan })
30824370aa4aSLai Jiangshan
3083841a915dSSteven Rostedt (VMware) while (*fmt.str) {
3084841a915dSSteven Rostedt (VMware) fmt = format_decode(fmt, &spec);
30854370aa4aSLai Jiangshan
3086938df695SLinus Torvalds switch (fmt.state) {
30879e0e6d8aSLinus Torvalds case FORMAT_STATE_NONE:
3088fef20d9cSFrederic Weisbecker case FORMAT_STATE_PERCENT_CHAR:
3089938df695SLinus Torvalds break;
3090938df695SLinus Torvalds case FORMAT_STATE_INVALID:
3091938df695SLinus Torvalds goto out;
3092fef20d9cSFrederic Weisbecker
3093938df695SLinus Torvalds case FORMAT_STATE_WIDTH:
3094b006f19bSRasmus Villemoes case FORMAT_STATE_PRECISION:
3095fef20d9cSFrederic Weisbecker width = (int)save_arg(int);
3096938df695SLinus Torvalds /* Pointers may require the width */
3097938df695SLinus Torvalds if (*fmt.str == 'p')
3098841a915dSSteven Rostedt (VMware) set_field_width(&spec, width);
3099841a915dSSteven Rostedt (VMware) break;
3100938df695SLinus Torvalds
3101841a915dSSteven Rostedt (VMware) case FORMAT_STATE_CHAR:
3102fef20d9cSFrederic Weisbecker save_arg(char);
31034370aa4aSLai Jiangshan break;
3104938df695SLinus Torvalds
31054370aa4aSLai Jiangshan case FORMAT_STATE_STR: {
3106fef20d9cSFrederic Weisbecker const char *save_str = va_arg(args, char *);
3107fef20d9cSFrederic Weisbecker const char *err_msg;
3108938df695SLinus Torvalds size_t len;
31094370aa4aSLai Jiangshan
31103e5903ebSPetr Mladek err_msg = check_pointer_msg(save_str);
31114370aa4aSLai Jiangshan if (err_msg)
31126c356634SAndré Goddard Rosa save_str = err_msg;
31133e5903ebSPetr Mladek
31143e5903ebSPetr Mladek len = strlen(save_str) + 1;
31153e5903ebSPetr Mladek if (str + len < end)
31163e5903ebSPetr Mladek memcpy(str, save_str, len);
31176c356634SAndré Goddard Rosa str += len;
31186c356634SAndré Goddard Rosa break;
31196c356634SAndré Goddard Rosa }
31206c356634SAndré Goddard Rosa
3121fef20d9cSFrederic Weisbecker case FORMAT_STATE_PTR:
31224370aa4aSLai Jiangshan /* Dereferenced pointers must be done now */
3123fef20d9cSFrederic Weisbecker switch (*fmt.str) {
3124938df695SLinus Torvalds /* Dereference of functions is still OK */
3125841a915dSSteven Rostedt (VMware) case 'S':
3126938df695SLinus Torvalds case 's':
3127841a915dSSteven Rostedt (VMware) case 'x':
3128841a915dSSteven Rostedt (VMware) case 'K':
3129841a915dSSteven Rostedt (VMware) case 'e':
31301e6338cfSSteven Rostedt (VMware) save_arg(void *);
31311e6338cfSSteven Rostedt (VMware) break;
313257f5677eSRasmus Villemoes default:
31334370aa4aSLai Jiangshan if (!isalnum(*fmt.str)) {
3134841a915dSSteven Rostedt (VMware) save_arg(void *);
3135841a915dSSteven Rostedt (VMware) break;
3136938df695SLinus Torvalds }
3137841a915dSSteven Rostedt (VMware) str = pointer(fmt.str, str, end, va_arg(args, void *),
3138841a915dSSteven Rostedt (VMware) spec);
3139841a915dSSteven Rostedt (VMware) if (str + 1 < end)
3140938df695SLinus Torvalds *str++ = '\0';
3141841a915dSSteven Rostedt (VMware) else
3142841a915dSSteven Rostedt (VMware) end[-1] = '\0'; /* Must be nul terminated */
3143841a915dSSteven Rostedt (VMware) }
3144841a915dSSteven Rostedt (VMware) /* skip all alphanumeric pointer suffixes */
3145841a915dSSteven Rostedt (VMware) while (isalnum(*fmt.str))
3146841a915dSSteven Rostedt (VMware) fmt.str++;
31474370aa4aSLai Jiangshan break;
3148938df695SLinus Torvalds
3149938df695SLinus Torvalds case FORMAT_STATE_NUM:
3150fef20d9cSFrederic Weisbecker if (fmt.size > sizeof(int)) {
3151fef20d9cSFrederic Weisbecker save_arg(long long);
31528d4826ccSLinus Torvalds } else {
31534c538044SLinus Torvalds save_arg(int);
3154fef20d9cSFrederic Weisbecker }
31554c538044SLinus Torvalds }
3156fef20d9cSFrederic Weisbecker }
3157fef20d9cSFrederic Weisbecker
3158fef20d9cSFrederic Weisbecker out:
31598d4826ccSLinus Torvalds return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
3160fef20d9cSFrederic Weisbecker #undef save_arg
3161b006f19bSRasmus Villemoes }
31627b9186f5SAndré Goddard Rosa EXPORT_SYMBOL_GPL(vbin_printf);
3163fef20d9cSFrederic Weisbecker
31644370aa4aSLai Jiangshan /**
31654370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer
31664370aa4aSLai Jiangshan * @buf: The buffer to place the result into
31674370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space
31684370aa4aSLai Jiangshan * @fmt_str: The format string to use
31694370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string
31704370aa4aSLai Jiangshan *
3171fa47906fSLinus Torvalds * This function like C99 vsnprintf, but the difference is that vsnprintf gets
31724370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
31734370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf.
31744370aa4aSLai Jiangshan *
31754370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions:
31764370aa4aSLai Jiangshan * see vsnprintf comment for details.
31774370aa4aSLai Jiangshan *
31784370aa4aSLai Jiangshan * The return value is the number of characters which would
31790efb4d20SSteven Rostedt * be generated for the given input, excluding the trailing
31804370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact
31814370aa4aSLai Jiangshan * number of characters written into @buf as return value
31824370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the
31834370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting
31844370aa4aSLai Jiangshan * string is truncated.
31854370aa4aSLai Jiangshan */
bstr_printf(char * buf,size_t size,const char * fmt_str,const u32 * bin_buf)31864370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt_str, const u32 *bin_buf)
31874370aa4aSLai Jiangshan {
31884370aa4aSLai Jiangshan struct fmt fmt = {
3189938df695SLinus Torvalds .str = fmt_str,
31904370aa4aSLai Jiangshan .state = FORMAT_STATE_NONE,
3191938df695SLinus Torvalds };
3192938df695SLinus Torvalds struct printf_spec spec = {0};
3193938df695SLinus Torvalds char *str, *end;
3194938df695SLinus Torvalds const char *args = (const char *)bin_buf;
3195fef20d9cSFrederic Weisbecker
3196d4be151bSAndré Goddard Rosa if (WARN_ON_ONCE(size > INT_MAX))
3197d4be151bSAndré Goddard Rosa return 0;
31984370aa4aSLai Jiangshan
3199762abb51SRasmus Villemoes str = buf;
32004370aa4aSLai Jiangshan end = buf + size;
32014370aa4aSLai Jiangshan
32024370aa4aSLai Jiangshan #define get_arg(type) \
32034370aa4aSLai Jiangshan ({ \
32044370aa4aSLai Jiangshan typeof(type) value; \
32054370aa4aSLai Jiangshan if (sizeof(type) == 8) { \
32064370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \
32074370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \
32084370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \
32094370aa4aSLai Jiangshan } else { \
32104370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \
32114370aa4aSLai Jiangshan value = *(typeof(type) *)args; \
32124370aa4aSLai Jiangshan } \
32134370aa4aSLai Jiangshan args += sizeof(type); \
32144370aa4aSLai Jiangshan value; \
32154370aa4aSLai Jiangshan })
32164370aa4aSLai Jiangshan
32174370aa4aSLai Jiangshan /* Make sure end is always >= buf */
32184370aa4aSLai Jiangshan if (end < buf) {
32194370aa4aSLai Jiangshan end = ((void *)-1);
32204370aa4aSLai Jiangshan size = end - buf;
32214370aa4aSLai Jiangshan }
32224370aa4aSLai Jiangshan
32234370aa4aSLai Jiangshan while (*fmt.str) {
32244370aa4aSLai Jiangshan const char *old_fmt = fmt.str;
32254370aa4aSLai Jiangshan unsigned long long num;
3226938df695SLinus Torvalds
3227938df695SLinus Torvalds fmt = format_decode(fmt, &spec);
322803d23941SLinus Torvalds switch (fmt.state) {
3229fef20d9cSFrederic Weisbecker case FORMAT_STATE_NONE: {
32309e0e6d8aSLinus Torvalds int read = fmt.str - old_fmt;
3231938df695SLinus Torvalds if (str < end) {
3232938df695SLinus Torvalds int copy = read;
3233938df695SLinus Torvalds if (copy > end - str)
3234fef20d9cSFrederic Weisbecker copy = end - str;
32359e0e6d8aSLinus Torvalds memcpy(str, old_fmt, copy);
3236fef20d9cSFrederic Weisbecker }
3237fef20d9cSFrederic Weisbecker str += read;
3238fef20d9cSFrederic Weisbecker continue;
3239fef20d9cSFrederic Weisbecker }
3240fef20d9cSFrederic Weisbecker
324103d23941SLinus Torvalds case FORMAT_STATE_WIDTH:
32424370aa4aSLai Jiangshan set_field_width(&spec, get_arg(int));
32434370aa4aSLai Jiangshan continue;
3244938df695SLinus Torvalds
32454d72ba01SRasmus Villemoes case FORMAT_STATE_PRECISION:
324603d23941SLinus Torvalds set_precision(&spec, get_arg(int));
32474370aa4aSLai Jiangshan continue;
3248938df695SLinus Torvalds
32494d72ba01SRasmus Villemoes case FORMAT_STATE_CHAR: {
325003d23941SLinus Torvalds char c;
32514370aa4aSLai Jiangshan
3252938df695SLinus Torvalds if (!(spec.flags & LEFT)) {
3253d4be151bSAndré Goddard Rosa while (--spec.field_width > 0) {
3254d4be151bSAndré Goddard Rosa if (str < end)
3255fef20d9cSFrederic Weisbecker *str = ' ';
3256fef20d9cSFrederic Weisbecker ++str;
32574370aa4aSLai Jiangshan }
32584370aa4aSLai Jiangshan }
32594370aa4aSLai Jiangshan c = (unsigned char) get_arg(char);
32604370aa4aSLai Jiangshan if (str < end)
32614370aa4aSLai Jiangshan *str = c;
32624370aa4aSLai Jiangshan ++str;
32634370aa4aSLai Jiangshan while (--spec.field_width > 0) {
32644370aa4aSLai Jiangshan if (str < end)
32654370aa4aSLai Jiangshan *str = ' ';
3266fef20d9cSFrederic Weisbecker ++str;
32674370aa4aSLai Jiangshan }
32684370aa4aSLai Jiangshan continue;
32694370aa4aSLai Jiangshan }
32704370aa4aSLai Jiangshan
327103d23941SLinus Torvalds case FORMAT_STATE_STR: {
3272d4be151bSAndré Goddard Rosa const char *str_arg = args;
32734370aa4aSLai Jiangshan args += strlen(str_arg) + 1;
3274938df695SLinus Torvalds str = string(str, end, (char *)str_arg, spec);
32754370aa4aSLai Jiangshan continue;
3276d4be151bSAndré Goddard Rosa }
3277fef20d9cSFrederic Weisbecker
327803d23941SLinus Torvalds case FORMAT_STATE_PTR: {
32794370aa4aSLai Jiangshan bool process = false;
32804370aa4aSLai Jiangshan int copy, len;
3281938df695SLinus Torvalds /* Non function dereferences were already done */
3282841a915dSSteven Rostedt (VMware) switch (*fmt.str) {
3283841a915dSSteven Rostedt (VMware) case 'S':
3284841a915dSSteven Rostedt (VMware) case 's':
3285938df695SLinus Torvalds case 'x':
3286841a915dSSteven Rostedt (VMware) case 'K':
3287841a915dSSteven Rostedt (VMware) case 'e':
32881e6338cfSSteven Rostedt (VMware) process = true;
32891e6338cfSSteven Rostedt (VMware) break;
329057f5677eSRasmus Villemoes default:
3291841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt.str)) {
3292841a915dSSteven Rostedt (VMware) process = true;
3293841a915dSSteven Rostedt (VMware) break;
3294938df695SLinus Torvalds }
3295841a915dSSteven Rostedt (VMware) /* Pointer dereference was already processed */
3296841a915dSSteven Rostedt (VMware) if (str < end) {
3297841a915dSSteven Rostedt (VMware) len = copy = strlen(args);
3298841a915dSSteven Rostedt (VMware) if (copy > end - str)
3299841a915dSSteven Rostedt (VMware) copy = end - str;
3300841a915dSSteven Rostedt (VMware) memcpy(str, args, copy);
3301841a915dSSteven Rostedt (VMware) str += len;
3302841a915dSSteven Rostedt (VMware) args += len + 1;
3303841a915dSSteven Rostedt (VMware) }
3304841a915dSSteven Rostedt (VMware) }
330562165600SSteven Rostedt (VMware) if (process)
3306841a915dSSteven Rostedt (VMware) str = pointer(fmt.str, str, end, get_arg(void *), spec);
3307841a915dSSteven Rostedt (VMware)
3308841a915dSSteven Rostedt (VMware) while (isalnum(*fmt.str))
3309938df695SLinus Torvalds fmt.str++;
3310841a915dSSteven Rostedt (VMware) continue;
3311938df695SLinus Torvalds }
3312938df695SLinus Torvalds
331303d23941SLinus Torvalds case FORMAT_STATE_PERCENT_CHAR:
3314841a915dSSteven Rostedt (VMware) if (str < end)
33154370aa4aSLai Jiangshan *str = '%';
3316938df695SLinus Torvalds ++str;
33174370aa4aSLai Jiangshan continue;
33184370aa4aSLai Jiangshan
33194370aa4aSLai Jiangshan case FORMAT_STATE_INVALID:
332003d23941SLinus Torvalds goto out;
3321fef20d9cSFrederic Weisbecker
3322938df695SLinus Torvalds case FORMAT_STATE_NUM:
3323b006f19bSRasmus Villemoes if (fmt.size > sizeof(int)) {
3324b006f19bSRasmus Villemoes num = get_arg(long long);
33258d4826ccSLinus Torvalds } else {
33264c538044SLinus Torvalds num = convert_num_spec(get_arg(int), fmt.size, spec);
33274370aa4aSLai Jiangshan }
33284c538044SLinus Torvalds str = number(str, end, num, spec);
33298d4826ccSLinus Torvalds continue;
33304370aa4aSLai Jiangshan }
3331fef20d9cSFrederic Weisbecker } /* while(*fmt.str) */
33324c538044SLinus Torvalds
33334c538044SLinus Torvalds out:
3334938df695SLinus Torvalds if (size > 0) {
3335fef20d9cSFrederic Weisbecker if (str < end)
3336b006f19bSRasmus Villemoes *str = '\0';
33374370aa4aSLai Jiangshan else
33384370aa4aSLai Jiangshan end[-1] = '\0';
33394370aa4aSLai Jiangshan }
33404370aa4aSLai Jiangshan
33414370aa4aSLai Jiangshan #undef get_arg
33424370aa4aSLai Jiangshan
3343fef20d9cSFrederic Weisbecker /* the trailing null byte doesn't count towards the total */
33444370aa4aSLai Jiangshan return str - buf;
33454370aa4aSLai Jiangshan }
33464370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf);
33474370aa4aSLai Jiangshan
33484370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */
33494370aa4aSLai Jiangshan
33504370aa4aSLai Jiangshan /**
33514370aa4aSLai Jiangshan * vsscanf - Unformat a buffer into a list of arguments
33524370aa4aSLai Jiangshan * @buf: input buffer
33531da177e4SLinus Torvalds * @fmt: format of buffer
33541da177e4SLinus Torvalds * @args: arguments
33551da177e4SLinus Torvalds */
vsscanf(const char * buf,const char * fmt,va_list args)33561da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args)
33571da177e4SLinus Torvalds {
33581da177e4SLinus Torvalds const char *str = buf;
33591da177e4SLinus Torvalds char *next;
33601da177e4SLinus Torvalds char digit;
33611da177e4SLinus Torvalds int num = 0;
33621da177e4SLinus Torvalds u8 qualifier;
33631da177e4SLinus Torvalds unsigned int base;
33641da177e4SLinus Torvalds union {
3365ef0658f3SJoe Perches long long s;
336653809751SJan Beulich unsigned long long u;
336753809751SJan Beulich } val;
336853809751SJan Beulich s16 field_width;
336953809751SJan Beulich bool is_sign;
337053809751SJan Beulich
3371ef0658f3SJoe Perches while (*fmt) {
3372d4be151bSAndré Goddard Rosa /* skip any white space in format */
33731da177e4SLinus Torvalds /* white space in format matches any amount of
3374da99075cSJan Beulich * white space, including none, in the input.
33751da177e4SLinus Torvalds */
33769dbbc3b9SZhen Lei if (isspace(*fmt)) {
33771da177e4SLinus Torvalds fmt = skip_spaces(++fmt);
33781da177e4SLinus Torvalds str = skip_spaces(str);
33791da177e4SLinus Torvalds }
3380e7d2860bSAndré Goddard Rosa
3381e7d2860bSAndré Goddard Rosa /* anything that is not a conversion must match exactly */
33821da177e4SLinus Torvalds if (*fmt != '%' && *fmt) {
33831da177e4SLinus Torvalds if (*fmt++ != *str++)
33841da177e4SLinus Torvalds break;
33851da177e4SLinus Torvalds continue;
33861da177e4SLinus Torvalds }
33871da177e4SLinus Torvalds
33881da177e4SLinus Torvalds if (!*fmt)
33891da177e4SLinus Torvalds break;
33901da177e4SLinus Torvalds ++fmt;
33911da177e4SLinus Torvalds
33921da177e4SLinus Torvalds /* skip this conversion.
33931da177e4SLinus Torvalds * advance both strings to next white space
33941da177e4SLinus Torvalds */
33951da177e4SLinus Torvalds if (*fmt == '*') {
33961da177e4SLinus Torvalds if (!*str)
33971da177e4SLinus Torvalds break;
33981da177e4SLinus Torvalds while (!isspace(*fmt) && *fmt != '%' && *fmt) {
3399da99075cSJan Beulich /* '%*[' not yet supported, invalid format */
3400da99075cSJan Beulich if (*fmt == '[')
3401f9310b2fSJessica Yu return num;
3402f9310b2fSJessica Yu fmt++;
3403f9310b2fSJessica Yu }
3404f9310b2fSJessica Yu while (!isspace(*str) && *str)
34051da177e4SLinus Torvalds str++;
3406f9310b2fSJessica Yu continue;
34071da177e4SLinus Torvalds }
34081da177e4SLinus Torvalds
34091da177e4SLinus Torvalds /* get field width */
34101da177e4SLinus Torvalds field_width = -1;
34111da177e4SLinus Torvalds if (isdigit(*fmt)) {
34121da177e4SLinus Torvalds field_width = skip_atoi(&fmt);
34131da177e4SLinus Torvalds if (field_width <= 0)
341453809751SJan Beulich break;
34151da177e4SLinus Torvalds }
341653809751SJan Beulich
341753809751SJan Beulich /* get conversion qualifier */
341853809751SJan Beulich qualifier = -1;
34191da177e4SLinus Torvalds if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
34201da177e4SLinus Torvalds *fmt == 'z') {
34211da177e4SLinus Torvalds qualifier = *fmt++;
342275fb8f26SAndy Shevchenko if (unlikely(qualifier == *fmt)) {
34235b5e0928SAlexey Dobriyan if (qualifier == 'h') {
34241da177e4SLinus Torvalds qualifier = 'H';
34251da177e4SLinus Torvalds fmt++;
34261da177e4SLinus Torvalds } else if (qualifier == 'l') {
34271da177e4SLinus Torvalds qualifier = 'L';
34281da177e4SLinus Torvalds fmt++;
34291da177e4SLinus Torvalds }
34301da177e4SLinus Torvalds }
34311da177e4SLinus Torvalds }
34321da177e4SLinus Torvalds
34331da177e4SLinus Torvalds if (!*fmt)
34341da177e4SLinus Torvalds break;
34351da177e4SLinus Torvalds
3436da99075cSJan Beulich if (*fmt == 'n') {
3437da99075cSJan Beulich /* return number of characters read so far */
3438da99075cSJan Beulich *va_arg(args, int *) = str - buf;
3439da99075cSJan Beulich ++fmt;
3440da99075cSJan Beulich continue;
3441da99075cSJan Beulich }
3442da99075cSJan Beulich
3443da99075cSJan Beulich if (!*str)
3444da99075cSJan Beulich break;
3445da99075cSJan Beulich
3446da99075cSJan Beulich base = 10;
34471da177e4SLinus Torvalds is_sign = false;
34481da177e4SLinus Torvalds
3449d4be151bSAndré Goddard Rosa switch (*fmt++) {
34503f623ebaSFabian Frederick case 'c':
3451d4be151bSAndré Goddard Rosa {
34521da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*);
34531da177e4SLinus Torvalds if (field_width == -1)
34541da177e4SLinus Torvalds field_width = 1;
34551da177e4SLinus Torvalds do {
34561da177e4SLinus Torvalds *s++ = *str++;
34571da177e4SLinus Torvalds } while (--field_width > 0 && *str);
34581da177e4SLinus Torvalds num++;
34591da177e4SLinus Torvalds }
34601da177e4SLinus Torvalds continue;
34611da177e4SLinus Torvalds case 's':
34621da177e4SLinus Torvalds {
34631da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *);
34641da177e4SLinus Torvalds if (field_width == -1)
34651da177e4SLinus Torvalds field_width = SHRT_MAX;
34661da177e4SLinus Torvalds /* first, skip leading white space in buffer */
34671da177e4SLinus Torvalds str = skip_spaces(str);
34684be929beSAlexey Dobriyan
34691da177e4SLinus Torvalds /* now copy until next white space */
3470e7d2860bSAndré Goddard Rosa while (*str && !isspace(*str) && field_width--)
34711da177e4SLinus Torvalds *s++ = *str++;
34721da177e4SLinus Torvalds *s = '\0';
34737b9186f5SAndré Goddard Rosa num++;
34741da177e4SLinus Torvalds }
34751da177e4SLinus Torvalds continue;
34761da177e4SLinus Torvalds /*
34771da177e4SLinus Torvalds * Warning: This implementation of the '[' conversion specifier
34781da177e4SLinus Torvalds * deviates from its glibc counterpart in the following ways:
3479f9310b2fSJessica Yu * (1) It does NOT support ranges i.e. '-' is NOT a special
3480f9310b2fSJessica Yu * character
3481f9310b2fSJessica Yu * (2) It cannot match the closing bracket ']' itself
3482f9310b2fSJessica Yu * (3) A field width is required
3483f9310b2fSJessica Yu * (4) '%*[' (discard matching input) is currently not supported
3484f9310b2fSJessica Yu *
3485f9310b2fSJessica Yu * Example usage:
3486f9310b2fSJessica Yu * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
3487f9310b2fSJessica Yu * buf1, buf2, buf3);
3488f9310b2fSJessica Yu * if (ret < 3)
3489f9310b2fSJessica Yu * // etc..
3490f9310b2fSJessica Yu */
3491f9310b2fSJessica Yu case '[':
3492f9310b2fSJessica Yu {
3493f9310b2fSJessica Yu char *s = (char *)va_arg(args, char *);
3494f9310b2fSJessica Yu DECLARE_BITMAP(set, 256) = {0};
3495f9310b2fSJessica Yu unsigned int len = 0;
3496f9310b2fSJessica Yu bool negate = (*fmt == '^');
3497f9310b2fSJessica Yu
3498f9310b2fSJessica Yu /* field width is required */
3499f9310b2fSJessica Yu if (field_width == -1)
3500f9310b2fSJessica Yu return num;
3501f9310b2fSJessica Yu
3502f9310b2fSJessica Yu if (negate)
3503f9310b2fSJessica Yu ++fmt;
3504f9310b2fSJessica Yu
3505f9310b2fSJessica Yu for ( ; *fmt && *fmt != ']'; ++fmt, ++len)
3506f9310b2fSJessica Yu __set_bit((u8)*fmt, set);
3507f9310b2fSJessica Yu
3508f9310b2fSJessica Yu /* no ']' or no character set found */
350952e68cd6SChristophe JAILLET if (!*fmt || !len)
3510f9310b2fSJessica Yu return num;
3511f9310b2fSJessica Yu ++fmt;
3512f9310b2fSJessica Yu
3513f9310b2fSJessica Yu if (negate) {
3514f9310b2fSJessica Yu bitmap_complement(set, set, 256);
3515f9310b2fSJessica Yu /* exclude null '\0' byte */
3516f9310b2fSJessica Yu __clear_bit(0, set);
3517f9310b2fSJessica Yu }
3518f9310b2fSJessica Yu
351952e68cd6SChristophe JAILLET /* match must be non-empty */
3520f9310b2fSJessica Yu if (!test_bit((u8)*str, set))
3521f9310b2fSJessica Yu return num;
3522f9310b2fSJessica Yu
3523f9310b2fSJessica Yu while (test_bit((u8)*str, set) && field_width--)
3524f9310b2fSJessica Yu *s++ = *str++;
3525f9310b2fSJessica Yu *s = '\0';
3526f9310b2fSJessica Yu ++num;
3527f9310b2fSJessica Yu }
3528f9310b2fSJessica Yu continue;
3529f9310b2fSJessica Yu case 'o':
3530f9310b2fSJessica Yu base = 8;
3531f9310b2fSJessica Yu break;
35321da177e4SLinus Torvalds case 'x':
35331da177e4SLinus Torvalds case 'X':
35341da177e4SLinus Torvalds base = 16;
35351da177e4SLinus Torvalds break;
35361da177e4SLinus Torvalds case 'i':
35371da177e4SLinus Torvalds base = 0;
35381da177e4SLinus Torvalds fallthrough;
35391da177e4SLinus Torvalds case 'd':
35401da177e4SLinus Torvalds is_sign = true;
35414c1ca831SNick Desaulniers fallthrough;
35421da177e4SLinus Torvalds case 'u':
35433f623ebaSFabian Frederick break;
35444c1ca831SNick Desaulniers case '%':
35451da177e4SLinus Torvalds /* looking for '%' in str */
35461da177e4SLinus Torvalds if (*str++ != '%')
35471da177e4SLinus Torvalds return num;
35481da177e4SLinus Torvalds continue;
35491da177e4SLinus Torvalds default:
35501da177e4SLinus Torvalds /* invalid format; stop here */
35511da177e4SLinus Torvalds return num;
35521da177e4SLinus Torvalds }
35531da177e4SLinus Torvalds
35541da177e4SLinus Torvalds /* have some sort of integer conversion.
35551da177e4SLinus Torvalds * first, skip white space in buffer.
35561da177e4SLinus Torvalds */
35571da177e4SLinus Torvalds str = skip_spaces(str);
35581da177e4SLinus Torvalds
35591da177e4SLinus Torvalds digit = *str;
3560e7d2860bSAndré Goddard Rosa if (is_sign && digit == '-') {
35611da177e4SLinus Torvalds if (field_width == 1)
35621da177e4SLinus Torvalds break;
356311b3dda5SRichard Fitzgerald
356411b3dda5SRichard Fitzgerald digit = *(str + 1);
356511b3dda5SRichard Fitzgerald }
356611b3dda5SRichard Fitzgerald
35671da177e4SLinus Torvalds if (!digit
356811b3dda5SRichard Fitzgerald || (base == 16 && !isxdigit(digit))
35691da177e4SLinus Torvalds || (base == 10 && !isdigit(digit))
35701da177e4SLinus Torvalds || (base == 8 && !isodigit(digit))
35711da177e4SLinus Torvalds || (base == 0 && !isdigit(digit)))
35721da177e4SLinus Torvalds break;
357348e1a66fSAndy Shevchenko
35741da177e4SLinus Torvalds if (is_sign)
35751da177e4SLinus Torvalds val.s = simple_strntoll(str, &next, base,
35761da177e4SLinus Torvalds field_width >= 0 ? field_width : INT_MAX);
357753809751SJan Beulich else
357872fcce70SAlexey Dobriyan val.u = simple_strntoull(str, &next, base,
357972fcce70SAlexey Dobriyan field_width >= 0 ? field_width : INT_MAX);
358053809751SJan Beulich
358172fcce70SAlexey Dobriyan switch (qualifier) {
358272fcce70SAlexey Dobriyan case 'H': /* that's 'hh' in format */
358353809751SJan Beulich if (is_sign)
35841da177e4SLinus Torvalds *va_arg(args, signed char *) = val.s;
35851da177e4SLinus Torvalds else
358653809751SJan Beulich *va_arg(args, unsigned char *) = val.u;
358753809751SJan Beulich break;
358853809751SJan Beulich case 'h':
358953809751SJan Beulich if (is_sign)
35901da177e4SLinus Torvalds *va_arg(args, short *) = val.s;
35911da177e4SLinus Torvalds else
359253809751SJan Beulich *va_arg(args, unsigned short *) = val.u;
359353809751SJan Beulich break;
359453809751SJan Beulich case 'l':
359553809751SJan Beulich if (is_sign)
35961da177e4SLinus Torvalds *va_arg(args, long *) = val.s;
35971da177e4SLinus Torvalds else
359853809751SJan Beulich *va_arg(args, unsigned long *) = val.u;
359953809751SJan Beulich break;
360053809751SJan Beulich case 'L':
360153809751SJan Beulich if (is_sign)
36021da177e4SLinus Torvalds *va_arg(args, long long *) = val.s;
36031da177e4SLinus Torvalds else
360453809751SJan Beulich *va_arg(args, unsigned long long *) = val.u;
360553809751SJan Beulich break;
360653809751SJan Beulich case 'z':
360753809751SJan Beulich *va_arg(args, size_t *) = val.u;
36081da177e4SLinus Torvalds break;
36091da177e4SLinus Torvalds default:
361053809751SJan Beulich if (is_sign)
36111da177e4SLinus Torvalds *va_arg(args, int *) = val.s;
36121da177e4SLinus Torvalds else
361353809751SJan Beulich *va_arg(args, unsigned int *) = val.u;
361453809751SJan Beulich break;
361553809751SJan Beulich }
361653809751SJan Beulich num++;
36171da177e4SLinus Torvalds
36181da177e4SLinus Torvalds if (!next)
36191da177e4SLinus Torvalds break;
36201da177e4SLinus Torvalds str = next;
36211da177e4SLinus Torvalds }
36221da177e4SLinus Torvalds
36231da177e4SLinus Torvalds return num;
36241da177e4SLinus Torvalds }
3625c6b40d16SJohannes Berg EXPORT_SYMBOL(vsscanf);
36261da177e4SLinus Torvalds
36271da177e4SLinus Torvalds /**
36281da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments
36291da177e4SLinus Torvalds * @buf: input buffer
36301da177e4SLinus Torvalds * @fmt: formatting of buffer
36311da177e4SLinus Torvalds * @...: resulting arguments
36321da177e4SLinus Torvalds */
sscanf(const char * buf,const char * fmt,...)36331da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...)
36341da177e4SLinus Torvalds {
36351da177e4SLinus Torvalds va_list args;
36361da177e4SLinus Torvalds int i;
36371da177e4SLinus Torvalds
36381da177e4SLinus Torvalds va_start(args, fmt);
36391da177e4SLinus Torvalds i = vsscanf(buf, fmt, args);
36401da177e4SLinus Torvalds va_end(args);
36411da177e4SLinus Torvalds
36421da177e4SLinus Torvalds return i;
36431da177e4SLinus Torvalds }
36447b9186f5SAndré Goddard Rosa EXPORT_SYMBOL(sscanf);
36451da177e4SLinus Torvalds