1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/lib/vsprintf.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ 91da177e4SLinus Torvalds /* 101da177e4SLinus Torvalds * Wirzenius wrote this portably, Torvalds fucked it up :-) 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds /* 141da177e4SLinus Torvalds * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> 151da177e4SLinus Torvalds * - changed to provide snprintf and vsnprintf functions 161da177e4SLinus Torvalds * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> 171da177e4SLinus Torvalds * - scnprintf and vscnprintf 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 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> 377daac5b2SAndy Shevchenko #include <linux/time.h> 382b1b0d66SAndy Shevchenko #include <linux/uuid.h> 39ce4fecf1SPantelis Antoniou #include <linux/of.h> 408a27f7c9SJoe Perches #include <net/addrconf.h> 41ad67b74dSTobin C. Harding #include <linux/siphash.h> 42ad67b74dSTobin C. Harding #include <linux/compiler.h> 43a92eb762SSakari Ailus #include <linux/property.h> 44*898f1e5cSJason A. Donenfeld #include <linux/notifier.h> 451031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 461031bc58SDmitry Monakhov #include <linux/blkdev.h> 471031bc58SDmitry Monakhov #endif 481da177e4SLinus Torvalds 49edf14cdbSVlastimil Babka #include "../mm/internal.h" /* For the trace_print_flags arrays */ 50edf14cdbSVlastimil Babka 514e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 527c43d9a3SRasmus Villemoes #include <asm/byteorder.h> /* cpu_to_le16 */ 53d75b26f8SAndy Shevchenko #include <asm/unaligned.h> 541da177e4SLinus Torvalds 5571dca95dSAndy Shevchenko #include <linux/string_helpers.h> 561dff46d6SAlexey Dobriyan #include "kstrtox.h" 57aa46a63eSHarvey Harrison 5884842911SChristophe Leroy /* Disable pointer hashing if requested */ 5984842911SChristophe Leroy bool no_hash_pointers __ro_after_init; 6084842911SChristophe Leroy EXPORT_SYMBOL_GPL(no_hash_pointers); 6184842911SChristophe Leroy 62839b395eSAlexey Dobriyan static noinline unsigned long long simple_strntoull(const char *startp, size_t max_chars, char **endp, unsigned int base) 63900fdc45SRichard Fitzgerald { 64900fdc45SRichard Fitzgerald const char *cp; 65900fdc45SRichard Fitzgerald unsigned long long result = 0ULL; 66900fdc45SRichard Fitzgerald size_t prefix_chars; 67900fdc45SRichard Fitzgerald unsigned int rv; 68900fdc45SRichard Fitzgerald 69900fdc45SRichard Fitzgerald cp = _parse_integer_fixup_radix(startp, &base); 70900fdc45SRichard Fitzgerald prefix_chars = cp - startp; 71900fdc45SRichard Fitzgerald if (prefix_chars < max_chars) { 72900fdc45SRichard Fitzgerald rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars); 73900fdc45SRichard Fitzgerald /* FIXME */ 74900fdc45SRichard Fitzgerald cp += (rv & ~KSTRTOX_OVERFLOW); 75900fdc45SRichard Fitzgerald } else { 76900fdc45SRichard Fitzgerald /* Field too short for prefix + digit, skip over without converting */ 77900fdc45SRichard Fitzgerald cp = startp + max_chars; 78900fdc45SRichard Fitzgerald } 79900fdc45SRichard Fitzgerald 80900fdc45SRichard Fitzgerald if (endp) 81900fdc45SRichard Fitzgerald *endp = (char *)cp; 82900fdc45SRichard Fitzgerald 83900fdc45SRichard Fitzgerald return result; 84900fdc45SRichard Fitzgerald } 85900fdc45SRichard Fitzgerald 861da177e4SLinus Torvalds /** 871da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 881da177e4SLinus Torvalds * @cp: The start of the string 891da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 901da177e4SLinus Torvalds * @base: The number base to use 91462e4711SEldad Zack * 92e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoull instead. 931da177e4SLinus Torvalds */ 94ad65dcefSAlexey Dobriyan noinline 951da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 961da177e4SLinus Torvalds { 97900fdc45SRichard Fitzgerald return simple_strntoull(cp, INT_MAX, endp, base); 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /** 102922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 103922ac25cSAndré Goddard Rosa * @cp: The start of the string 104922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 105922ac25cSAndré Goddard Rosa * @base: The number base to use 106462e4711SEldad Zack * 107e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoul instead. 108922ac25cSAndré Goddard Rosa */ 109922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 110922ac25cSAndré Goddard Rosa { 111922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 112922ac25cSAndré Goddard Rosa } 113922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 114922ac25cSAndré Goddard Rosa 115922ac25cSAndré Goddard Rosa /** 116922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 117922ac25cSAndré Goddard Rosa * @cp: The start of the string 118922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 119922ac25cSAndré Goddard Rosa * @base: The number base to use 120462e4711SEldad Zack * 121e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtol instead. 122922ac25cSAndré Goddard Rosa */ 123922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 124922ac25cSAndré Goddard Rosa { 125922ac25cSAndré Goddard Rosa if (*cp == '-') 126922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 127922ac25cSAndré Goddard Rosa 128922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 129922ac25cSAndré Goddard Rosa } 130922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 131922ac25cSAndré Goddard Rosa 132900fdc45SRichard Fitzgerald static long long simple_strntoll(const char *cp, size_t max_chars, char **endp, 133900fdc45SRichard Fitzgerald unsigned int base) 134900fdc45SRichard Fitzgerald { 135900fdc45SRichard Fitzgerald /* 136900fdc45SRichard Fitzgerald * simple_strntoull() safely handles receiving max_chars==0 in the 137900fdc45SRichard Fitzgerald * case cp[0] == '-' && max_chars == 1. 138900fdc45SRichard Fitzgerald * If max_chars == 0 we can drop through and pass it to simple_strntoull() 139900fdc45SRichard Fitzgerald * and the content of *cp is irrelevant. 140900fdc45SRichard Fitzgerald */ 141900fdc45SRichard Fitzgerald if (*cp == '-' && max_chars > 0) 142900fdc45SRichard Fitzgerald return -simple_strntoull(cp + 1, max_chars - 1, endp, base); 143900fdc45SRichard Fitzgerald 144900fdc45SRichard Fitzgerald return simple_strntoull(cp, max_chars, endp, base); 145900fdc45SRichard Fitzgerald } 146900fdc45SRichard Fitzgerald 147922ac25cSAndré Goddard Rosa /** 1481da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1491da177e4SLinus Torvalds * @cp: The start of the string 1501da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1511da177e4SLinus Torvalds * @base: The number base to use 152462e4711SEldad Zack * 153e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoll instead. 1541da177e4SLinus Torvalds */ 1551da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1561da177e4SLinus Torvalds { 157900fdc45SRichard Fitzgerald return simple_strntoll(cp, INT_MAX, endp, base); 1581da177e4SLinus Torvalds } 15998d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1601da177e4SLinus Torvalds 161cf3b429bSJoe Perches static noinline_for_stack 162cf3b429bSJoe Perches int skip_atoi(const char **s) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds int i = 0; 1651da177e4SLinus Torvalds 16643e5b666SRasmus Villemoes do { 1671da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 16843e5b666SRasmus Villemoes } while (isdigit(**s)); 1697b9186f5SAndré Goddard Rosa 1701da177e4SLinus Torvalds return i; 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1737c43d9a3SRasmus Villemoes /* 1747c43d9a3SRasmus Villemoes * Decimal conversion is by far the most typical, and is used for 1757c43d9a3SRasmus Villemoes * /proc and /sys data. This directly impacts e.g. top performance 1767c43d9a3SRasmus Villemoes * with many processes running. We optimize it for speed by emitting 1777c43d9a3SRasmus Villemoes * two characters at a time, using a 200 byte lookup table. This 1787c43d9a3SRasmus Villemoes * roughly halves the number of multiplications compared to computing 1797c43d9a3SRasmus Villemoes * the digits one at a time. Implementation strongly inspired by the 1807c43d9a3SRasmus Villemoes * previous version, which in turn used ideas described at 1817c43d9a3SRasmus Villemoes * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 1827c43d9a3SRasmus Villemoes * from the author, Douglas W. Jones). 1837c43d9a3SRasmus Villemoes * 1847c43d9a3SRasmus Villemoes * It turns out there is precisely one 26 bit fixed-point 1857c43d9a3SRasmus Villemoes * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 1867c43d9a3SRasmus Villemoes * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 1877c43d9a3SRasmus Villemoes * range happens to be somewhat larger (x <= 1073741898), but that's 1887c43d9a3SRasmus Villemoes * irrelevant for our purpose. 1897c43d9a3SRasmus Villemoes * 1907c43d9a3SRasmus Villemoes * For dividing a number in the range [10^4, 10^6-1] by 100, we still 1917c43d9a3SRasmus Villemoes * need a 32x32->64 bit multiply, so we simply use the same constant. 1927c43d9a3SRasmus Villemoes * 1937c43d9a3SRasmus Villemoes * For dividing a number in the range [100, 10^4-1] by 100, there are 1947c43d9a3SRasmus Villemoes * several options. The simplest is (x * 0x147b) >> 19, which is valid 1957c43d9a3SRasmus Villemoes * for all x <= 43698. 196133fd9f5SDenys Vlasenko */ 1974277eeddSDenis Vlasenko 1987c43d9a3SRasmus Villemoes static const u16 decpair[100] = { 1997c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 2007c43d9a3SRasmus Villemoes _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 2017c43d9a3SRasmus Villemoes _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 2027c43d9a3SRasmus Villemoes _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 2037c43d9a3SRasmus Villemoes _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 2047c43d9a3SRasmus Villemoes _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 2057c43d9a3SRasmus Villemoes _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 2067c43d9a3SRasmus Villemoes _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 2077c43d9a3SRasmus Villemoes _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 2087c43d9a3SRasmus Villemoes _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 2097c43d9a3SRasmus Villemoes _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 2107c43d9a3SRasmus Villemoes #undef _ 2117c43d9a3SRasmus Villemoes }; 2124277eeddSDenis Vlasenko 2137b9186f5SAndré Goddard Rosa /* 2147c43d9a3SRasmus Villemoes * This will print a single '0' even if r == 0, since we would 215675cf53cSRasmus Villemoes * immediately jump to out_r where two 0s would be written but only 216675cf53cSRasmus Villemoes * one of them accounted for in buf. This is needed by ip4_string 217675cf53cSRasmus Villemoes * below. All other callers pass a non-zero value of r. 218133fd9f5SDenys Vlasenko */ 219133fd9f5SDenys Vlasenko static noinline_for_stack 220133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 221133fd9f5SDenys Vlasenko { 222133fd9f5SDenys Vlasenko unsigned q; 2234277eeddSDenis Vlasenko 2247c43d9a3SRasmus Villemoes /* 1 <= r < 10^8 */ 2257c43d9a3SRasmus Villemoes if (r < 100) 2267c43d9a3SRasmus Villemoes goto out_r; 227cb239d0aSGeorge Spelvin 2287c43d9a3SRasmus Villemoes /* 100 <= r < 10^8 */ 2297c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2307c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2317c43d9a3SRasmus Villemoes buf += 2; 2327c43d9a3SRasmus Villemoes 2337c43d9a3SRasmus Villemoes /* 1 <= q < 10^6 */ 2347c43d9a3SRasmus Villemoes if (q < 100) 2357c43d9a3SRasmus Villemoes goto out_q; 2367c43d9a3SRasmus Villemoes 2377c43d9a3SRasmus Villemoes /* 100 <= q < 10^6 */ 2387c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2397c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2407c43d9a3SRasmus Villemoes buf += 2; 2417c43d9a3SRasmus Villemoes 2427c43d9a3SRasmus Villemoes /* 1 <= r < 10^4 */ 2437c43d9a3SRasmus Villemoes if (r < 100) 2447c43d9a3SRasmus Villemoes goto out_r; 2457c43d9a3SRasmus Villemoes 2467c43d9a3SRasmus Villemoes /* 100 <= r < 10^4 */ 2477c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2487c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2497c43d9a3SRasmus Villemoes buf += 2; 2507c43d9a3SRasmus Villemoes out_q: 2517c43d9a3SRasmus Villemoes /* 1 <= q < 100 */ 2527c43d9a3SRasmus Villemoes r = q; 2537c43d9a3SRasmus Villemoes out_r: 2547c43d9a3SRasmus Villemoes /* 1 <= r < 100 */ 2557c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r]; 256675cf53cSRasmus Villemoes buf += r < 10 ? 1 : 2; 257133fd9f5SDenys Vlasenko return buf; 258133fd9f5SDenys Vlasenko } 259133fd9f5SDenys Vlasenko 2607c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 2617c43d9a3SRasmus Villemoes static noinline_for_stack 2627c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r) 2637c43d9a3SRasmus Villemoes { 2647c43d9a3SRasmus Villemoes unsigned q; 265133fd9f5SDenys Vlasenko 2667c43d9a3SRasmus Villemoes /* 0 <= r < 10^8 */ 2677c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2687c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2697c43d9a3SRasmus Villemoes buf += 2; 270133fd9f5SDenys Vlasenko 2717c43d9a3SRasmus Villemoes /* 0 <= q < 10^6 */ 2727c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2737c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2747c43d9a3SRasmus Villemoes buf += 2; 275133fd9f5SDenys Vlasenko 2767c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2777c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2787c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2797c43d9a3SRasmus Villemoes buf += 2; 2807c43d9a3SRasmus Villemoes 2817c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2827c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2837c43d9a3SRasmus Villemoes buf += 2; 2847c43d9a3SRasmus Villemoes return buf; 2857c43d9a3SRasmus Villemoes } 2867c43d9a3SRasmus Villemoes 2877c43d9a3SRasmus Villemoes static noinline_for_stack 288133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 289133fd9f5SDenys Vlasenko { 290133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 2917c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2927c43d9a3SRasmus Villemoes /* 1 <= n <= 1.6e11 */ 2937c43d9a3SRasmus Villemoes if (n >= 100*1000*1000) 2947c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2957c43d9a3SRasmus Villemoes /* 1 <= n < 1e8 */ 296133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 297133fd9f5SDenys Vlasenko } 298133fd9f5SDenys Vlasenko 2997c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 300133fd9f5SDenys Vlasenko 3017c43d9a3SRasmus Villemoes static void 3027c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r) 303133fd9f5SDenys Vlasenko { 3047c43d9a3SRasmus Villemoes unsigned q; 3057c43d9a3SRasmus Villemoes 3067c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 3077c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 3087c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 3097c43d9a3SRasmus Villemoes buf += 2; 3107c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 3117c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 3122359172aSGeorge Spelvin } 3132359172aSGeorge Spelvin 3142359172aSGeorge Spelvin /* 3152359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 3162359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 3172359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 3182359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 3197c43d9a3SRasmus Villemoes * (second call in the put_dec code, assuming n is all-ones). 3202359172aSGeorge Spelvin */ 3217c43d9a3SRasmus Villemoes static noinline_for_stack 3222359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 3232359172aSGeorge Spelvin { 3242359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 3252359172aSGeorge Spelvin 3262359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 3272359172aSGeorge Spelvin return q; 328133fd9f5SDenys Vlasenko } 329133fd9f5SDenys Vlasenko 330133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 331133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 332133fd9f5SDenys Vlasenko * (with permission from the author). 333133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 334133fd9f5SDenys Vlasenko */ 335133fd9f5SDenys Vlasenko static 336133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 337133fd9f5SDenys Vlasenko { 338133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 339133fd9f5SDenys Vlasenko 340133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 341133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 342133fd9f5SDenys Vlasenko 343133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 344133fd9f5SDenys Vlasenko h = (n >> 32); 345133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 346133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 347133fd9f5SDenys Vlasenko 3487c43d9a3SRasmus Villemoes /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 3497c43d9a3SRasmus Villemoes = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 350133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 3512359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 352133fd9f5SDenys Vlasenko 3532359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 3542359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 355133fd9f5SDenys Vlasenko 3562359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3572359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 358133fd9f5SDenys Vlasenko 3592359172aSGeorge Spelvin q += 281 * d3; 3602359172aSGeorge Spelvin buf += 12; 3612359172aSGeorge Spelvin if (q) 3622359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3632359172aSGeorge Spelvin else while (buf[-1] == '0') 364133fd9f5SDenys Vlasenko --buf; 3657b9186f5SAndré Goddard Rosa 3664277eeddSDenis Vlasenko return buf; 3674277eeddSDenis Vlasenko } 368133fd9f5SDenys Vlasenko 369133fd9f5SDenys Vlasenko #endif 3704277eeddSDenis Vlasenko 3711ac101a5SKAMEZAWA Hiroyuki /* 3721ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3731ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3741ac101a5SKAMEZAWA Hiroyuki * 3751ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3761ac101a5SKAMEZAWA Hiroyuki */ 377d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) 3781ac101a5SKAMEZAWA Hiroyuki { 3797c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 3807c43d9a3SRasmus Villemoes char tmp[sizeof(num) * 3] __aligned(2); 3811ac101a5SKAMEZAWA Hiroyuki int idx, len; 3821ac101a5SKAMEZAWA Hiroyuki 383133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 384133fd9f5SDenys Vlasenko if (num <= 9) { 385133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 386133fd9f5SDenys Vlasenko len = 1; 387133fd9f5SDenys Vlasenko } else { 3881ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 389133fd9f5SDenys Vlasenko } 3901ac101a5SKAMEZAWA Hiroyuki 391d1be35cbSAndrei Vagin if (len > size || width > size) 3921ac101a5SKAMEZAWA Hiroyuki return 0; 393d1be35cbSAndrei Vagin 394d1be35cbSAndrei Vagin if (width > len) { 395d1be35cbSAndrei Vagin width = width - len; 396d1be35cbSAndrei Vagin for (idx = 0; idx < width; idx++) 397d1be35cbSAndrei Vagin buf[idx] = ' '; 398d1be35cbSAndrei Vagin } else { 399d1be35cbSAndrei Vagin width = 0; 400d1be35cbSAndrei Vagin } 401d1be35cbSAndrei Vagin 4021ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 403d1be35cbSAndrei Vagin buf[idx + width] = tmp[len - idx - 1]; 404d1be35cbSAndrei Vagin 405d1be35cbSAndrei Vagin return len + width; 4061ac101a5SKAMEZAWA Hiroyuki } 4071ac101a5SKAMEZAWA Hiroyuki 40851be17dfSRasmus Villemoes #define SIGN 1 /* unsigned/signed, must be 1 */ 409d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */ 4101da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 4111da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 412d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 413b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 414b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 4151da177e4SLinus Torvalds 41624a1dffbSAndy Shevchenko static_assert(SIGN == 1); 417b886690dSAndy Shevchenko static_assert(ZEROPAD == ('0' - ' ')); 41824a1dffbSAndy Shevchenko static_assert(SMALL == ('a' ^ 'A')); 419b886690dSAndy Shevchenko 420fef20d9cSFrederic Weisbecker enum format_type { 421fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 422ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 423fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 424fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 425fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 426fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 427fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 428fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 429fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 430fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 431fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 432a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 433a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 434fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 435fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 436fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 437fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 438fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 439fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 440fef20d9cSFrederic Weisbecker }; 441fef20d9cSFrederic Weisbecker 442fef20d9cSFrederic Weisbecker struct printf_spec { 443d0484193SRasmus Villemoes unsigned int type:8; /* format_type enum */ 444d0484193SRasmus Villemoes signed int field_width:24; /* width of output field */ 445d0484193SRasmus Villemoes unsigned int flags:8; /* flags to number() */ 446d0484193SRasmus Villemoes unsigned int base:8; /* number base, 8, 10 or 16 only */ 447d0484193SRasmus Villemoes signed int precision:16; /* # of digits/chars */ 448d0484193SRasmus Villemoes } __packed; 449ef27ac18SRasmus Villemoes static_assert(sizeof(struct printf_spec) == 8); 450ef27ac18SRasmus Villemoes 4514d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1) 4524d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1) 453fef20d9cSFrederic Weisbecker 454cf3b429bSJoe Perches static noinline_for_stack 455cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 456fef20d9cSFrederic Weisbecker struct printf_spec spec) 4571da177e4SLinus Torvalds { 4587c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 4597c43d9a3SRasmus Villemoes char tmp[3 * sizeof(num)] __aligned(2); 4609b706aeeSDenys Vlasenko char sign; 4619b706aeeSDenys Vlasenko char locase; 462fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4631da177e4SLinus Torvalds int i; 4647c203422SPierre Carrier bool is_zero = num == 0LL; 4651c7a8e62SRasmus Villemoes int field_width = spec.field_width; 4661c7a8e62SRasmus Villemoes int precision = spec.precision; 4671da177e4SLinus Torvalds 4689b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4699b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 470fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 471fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 472fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4731da177e4SLinus Torvalds sign = 0; 474fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4751da177e4SLinus Torvalds if ((signed long long)num < 0) { 4761da177e4SLinus Torvalds sign = '-'; 4771da177e4SLinus Torvalds num = -(signed long long)num; 4781c7a8e62SRasmus Villemoes field_width--; 479fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4801da177e4SLinus Torvalds sign = '+'; 4811c7a8e62SRasmus Villemoes field_width--; 482fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4831da177e4SLinus Torvalds sign = ' '; 4841c7a8e62SRasmus Villemoes field_width--; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds } 487b39a7340SDenis Vlasenko if (need_pfx) { 488fef20d9cSFrederic Weisbecker if (spec.base == 16) 4891c7a8e62SRasmus Villemoes field_width -= 2; 4907c203422SPierre Carrier else if (!is_zero) 4911c7a8e62SRasmus Villemoes field_width--; 4921da177e4SLinus Torvalds } 493b39a7340SDenis Vlasenko 494b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4951da177e4SLinus Torvalds i = 0; 496133fd9f5SDenys Vlasenko if (num < spec.base) 4973ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase; 498fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 499fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 500b39a7340SDenis Vlasenko int shift = 3; 5017b9186f5SAndré Goddard Rosa 5027b9186f5SAndré Goddard Rosa if (spec.base == 16) 5037b9186f5SAndré Goddard Rosa shift = 4; 504b39a7340SDenis Vlasenko do { 5053ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 506b39a7340SDenis Vlasenko num >>= shift; 507b39a7340SDenis Vlasenko } while (num); 5084277eeddSDenis Vlasenko } else { /* base 10 */ 5094277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 5104277eeddSDenis Vlasenko } 511b39a7340SDenis Vlasenko 512b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 5131c7a8e62SRasmus Villemoes if (i > precision) 5141c7a8e62SRasmus Villemoes precision = i; 515b39a7340SDenis Vlasenko /* leading space padding */ 5161c7a8e62SRasmus Villemoes field_width -= precision; 51751be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) { 5181c7a8e62SRasmus Villemoes while (--field_width >= 0) { 519f796937aSJeremy Fitzhardinge if (buf < end) 5201da177e4SLinus Torvalds *buf = ' '; 5211da177e4SLinus Torvalds ++buf; 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds } 524b39a7340SDenis Vlasenko /* sign */ 5251da177e4SLinus Torvalds if (sign) { 526f796937aSJeremy Fitzhardinge if (buf < end) 5271da177e4SLinus Torvalds *buf = sign; 5281da177e4SLinus Torvalds ++buf; 5291da177e4SLinus Torvalds } 530b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 531b39a7340SDenis Vlasenko if (need_pfx) { 5327c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 533f796937aSJeremy Fitzhardinge if (buf < end) 5341da177e4SLinus Torvalds *buf = '0'; 5351da177e4SLinus Torvalds ++buf; 5367c203422SPierre Carrier } 537fef20d9cSFrederic Weisbecker if (spec.base == 16) { 538f796937aSJeremy Fitzhardinge if (buf < end) 5399b706aeeSDenys Vlasenko *buf = ('X' | locase); 5401da177e4SLinus Torvalds ++buf; 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds } 543b39a7340SDenis Vlasenko /* zero or space padding */ 544fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 545d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD); 546b886690dSAndy Shevchenko 5471c7a8e62SRasmus Villemoes while (--field_width >= 0) { 548f796937aSJeremy Fitzhardinge if (buf < end) 5491da177e4SLinus Torvalds *buf = c; 5501da177e4SLinus Torvalds ++buf; 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds } 553b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 5541c7a8e62SRasmus Villemoes while (i <= --precision) { 555f796937aSJeremy Fitzhardinge if (buf < end) 5561da177e4SLinus Torvalds *buf = '0'; 5571da177e4SLinus Torvalds ++buf; 5581da177e4SLinus Torvalds } 559b39a7340SDenis Vlasenko /* actual digits of result */ 560b39a7340SDenis Vlasenko while (--i >= 0) { 561f796937aSJeremy Fitzhardinge if (buf < end) 5621da177e4SLinus Torvalds *buf = tmp[i]; 5631da177e4SLinus Torvalds ++buf; 5641da177e4SLinus Torvalds } 565b39a7340SDenis Vlasenko /* trailing space padding */ 5661c7a8e62SRasmus Villemoes while (--field_width >= 0) { 567f796937aSJeremy Fitzhardinge if (buf < end) 5681da177e4SLinus Torvalds *buf = ' '; 5691da177e4SLinus Torvalds ++buf; 5701da177e4SLinus Torvalds } 5717b9186f5SAndré Goddard Rosa 5721da177e4SLinus Torvalds return buf; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5753cab1e71SAndy Shevchenko static noinline_for_stack 5763cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size) 5773cab1e71SAndy Shevchenko { 5783cab1e71SAndy Shevchenko struct printf_spec spec; 5793cab1e71SAndy Shevchenko 5803cab1e71SAndy Shevchenko spec.type = FORMAT_TYPE_PTR; 5813cab1e71SAndy Shevchenko spec.field_width = 2 + 2 * size; /* 0x + hex */ 5823cab1e71SAndy Shevchenko spec.flags = SPECIAL | SMALL | ZEROPAD; 5833cab1e71SAndy Shevchenko spec.base = 16; 5843cab1e71SAndy Shevchenko spec.precision = -1; 5853cab1e71SAndy Shevchenko 5863cab1e71SAndy Shevchenko return number(buf, end, num, spec); 5873cab1e71SAndy Shevchenko } 5883cab1e71SAndy Shevchenko 589cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces) 5904b6ccca7SAl Viro { 5914b6ccca7SAl Viro size_t size; 5924b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5934b6ccca7SAl Viro return; 5944b6ccca7SAl Viro size = end - buf; 5954b6ccca7SAl Viro if (size <= spaces) { 5964b6ccca7SAl Viro memset(buf, ' ', size); 5974b6ccca7SAl Viro return; 5984b6ccca7SAl Viro } 5994b6ccca7SAl Viro if (len) { 6004b6ccca7SAl Viro if (len > size - spaces) 6014b6ccca7SAl Viro len = size - spaces; 6024b6ccca7SAl Viro memmove(buf + spaces, buf, len); 6034b6ccca7SAl Viro } 6044b6ccca7SAl Viro memset(buf, ' ', spaces); 6054b6ccca7SAl Viro } 6064b6ccca7SAl Viro 607cfccde04SRasmus Villemoes /* 608cfccde04SRasmus Villemoes * Handle field width padding for a string. 609cfccde04SRasmus Villemoes * @buf: current buffer position 610cfccde04SRasmus Villemoes * @n: length of string 611cfccde04SRasmus Villemoes * @end: end of output buffer 612cfccde04SRasmus Villemoes * @spec: for field width and flags 613cfccde04SRasmus Villemoes * Returns: new buffer position after padding. 614cfccde04SRasmus Villemoes */ 615cfccde04SRasmus Villemoes static noinline_for_stack 616cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec) 617cfccde04SRasmus Villemoes { 618cfccde04SRasmus Villemoes unsigned spaces; 619cfccde04SRasmus Villemoes 620cfccde04SRasmus Villemoes if (likely(n >= spec.field_width)) 621cfccde04SRasmus Villemoes return buf; 622cfccde04SRasmus Villemoes /* we want to pad the sucker */ 623cfccde04SRasmus Villemoes spaces = spec.field_width - n; 624cfccde04SRasmus Villemoes if (!(spec.flags & LEFT)) { 625cfccde04SRasmus Villemoes move_right(buf - n, end, n, spaces); 626cfccde04SRasmus Villemoes return buf + spaces; 627cfccde04SRasmus Villemoes } 628cfccde04SRasmus Villemoes while (spaces--) { 629cfccde04SRasmus Villemoes if (buf < end) 630cfccde04SRasmus Villemoes *buf = ' '; 631cfccde04SRasmus Villemoes ++buf; 632cfccde04SRasmus Villemoes } 633cfccde04SRasmus Villemoes return buf; 634cfccde04SRasmus Villemoes } 635cfccde04SRasmus Villemoes 636d529ac41SPetr Mladek /* Handle string from a well known address. */ 637d529ac41SPetr Mladek static char *string_nocheck(char *buf, char *end, const char *s, 638d529ac41SPetr Mladek struct printf_spec spec) 63995508cfaSRasmus Villemoes { 64034fc8b90SRasmus Villemoes int len = 0; 641b314dd49SYoungmin Nam int lim = spec.precision; 64295508cfaSRasmus Villemoes 64334fc8b90SRasmus Villemoes while (lim--) { 64434fc8b90SRasmus Villemoes char c = *s++; 64534fc8b90SRasmus Villemoes if (!c) 64634fc8b90SRasmus Villemoes break; 64795508cfaSRasmus Villemoes if (buf < end) 64834fc8b90SRasmus Villemoes *buf = c; 64995508cfaSRasmus Villemoes ++buf; 65034fc8b90SRasmus Villemoes ++len; 65195508cfaSRasmus Villemoes } 65234fc8b90SRasmus Villemoes return widen_string(buf, len, end, spec); 65395508cfaSRasmus Villemoes } 65495508cfaSRasmus Villemoes 65557f5677eSRasmus Villemoes static char *err_ptr(char *buf, char *end, void *ptr, 65657f5677eSRasmus Villemoes struct printf_spec spec) 65757f5677eSRasmus Villemoes { 65857f5677eSRasmus Villemoes int err = PTR_ERR(ptr); 65957f5677eSRasmus Villemoes const char *sym = errname(err); 66057f5677eSRasmus Villemoes 66157f5677eSRasmus Villemoes if (sym) 66257f5677eSRasmus Villemoes return string_nocheck(buf, end, sym, spec); 66357f5677eSRasmus Villemoes 66457f5677eSRasmus Villemoes /* 66557f5677eSRasmus Villemoes * Somebody passed ERR_PTR(-1234) or some other non-existing 66657f5677eSRasmus Villemoes * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to 66757f5677eSRasmus Villemoes * printing it as its decimal representation. 66857f5677eSRasmus Villemoes */ 66957f5677eSRasmus Villemoes spec.flags |= SIGN; 67057f5677eSRasmus Villemoes spec.base = 10; 67157f5677eSRasmus Villemoes return number(buf, end, err, spec); 67257f5677eSRasmus Villemoes } 67357f5677eSRasmus Villemoes 674c8c3b584SPetr Mladek /* Be careful: error messages must fit into the given buffer. */ 675c8c3b584SPetr Mladek static char *error_string(char *buf, char *end, const char *s, 676c8c3b584SPetr Mladek struct printf_spec spec) 677c8c3b584SPetr Mladek { 678c8c3b584SPetr Mladek /* 679c8c3b584SPetr Mladek * Hard limit to avoid a completely insane messages. It actually 680c8c3b584SPetr Mladek * works pretty well because most error messages are in 681c8c3b584SPetr Mladek * the many pointer format modifiers. 682c8c3b584SPetr Mladek */ 683c8c3b584SPetr Mladek if (spec.precision == -1) 684c8c3b584SPetr Mladek spec.precision = 2 * sizeof(void *); 685c8c3b584SPetr Mladek 686c8c3b584SPetr Mladek return string_nocheck(buf, end, s, spec); 687c8c3b584SPetr Mladek } 688c8c3b584SPetr Mladek 6893e5903ebSPetr Mladek /* 6902ac5a3bfSPetr Mladek * Do not call any complex external code here. Nested printk()/vsprintf() 6912ac5a3bfSPetr Mladek * might cause infinite loops. Failures might break printk() and would 6922ac5a3bfSPetr Mladek * be hard to debug. 6933e5903ebSPetr Mladek */ 6943e5903ebSPetr Mladek static const char *check_pointer_msg(const void *ptr) 6953e5903ebSPetr Mladek { 6963e5903ebSPetr Mladek if (!ptr) 6973e5903ebSPetr Mladek return "(null)"; 6983e5903ebSPetr Mladek 6992ac5a3bfSPetr Mladek if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr)) 7003e5903ebSPetr Mladek return "(efault)"; 7013e5903ebSPetr Mladek 7023e5903ebSPetr Mladek return NULL; 7033e5903ebSPetr Mladek } 7043e5903ebSPetr Mladek 7053e5903ebSPetr Mladek static int check_pointer(char **buf, char *end, const void *ptr, 7063e5903ebSPetr Mladek struct printf_spec spec) 7073e5903ebSPetr Mladek { 7083e5903ebSPetr Mladek const char *err_msg; 7093e5903ebSPetr Mladek 7103e5903ebSPetr Mladek err_msg = check_pointer_msg(ptr); 7113e5903ebSPetr Mladek if (err_msg) { 712c8c3b584SPetr Mladek *buf = error_string(*buf, end, err_msg, spec); 7133e5903ebSPetr Mladek return -EFAULT; 7143e5903ebSPetr Mladek } 7153e5903ebSPetr Mladek 7163e5903ebSPetr Mladek return 0; 7173e5903ebSPetr Mladek } 7183e5903ebSPetr Mladek 71995508cfaSRasmus Villemoes static noinline_for_stack 720d529ac41SPetr Mladek char *string(char *buf, char *end, const char *s, 721d529ac41SPetr Mladek struct printf_spec spec) 722d529ac41SPetr Mladek { 7233e5903ebSPetr Mladek if (check_pointer(&buf, end, s, spec)) 7243e5903ebSPetr Mladek return buf; 725d529ac41SPetr Mladek 726d529ac41SPetr Mladek return string_nocheck(buf, end, s, spec); 727d529ac41SPetr Mladek } 728d529ac41SPetr Mladek 729ce9d3eceSYueHaibing static char *pointer_string(char *buf, char *end, 730ce9d3eceSYueHaibing const void *ptr, 7319073dac1SGeert Uytterhoeven struct printf_spec spec) 7329073dac1SGeert Uytterhoeven { 7339073dac1SGeert Uytterhoeven spec.base = 16; 7349073dac1SGeert Uytterhoeven spec.flags |= SMALL; 7359073dac1SGeert Uytterhoeven if (spec.field_width == -1) { 7369073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr); 7379073dac1SGeert Uytterhoeven spec.flags |= ZEROPAD; 7389073dac1SGeert Uytterhoeven } 7399073dac1SGeert Uytterhoeven 7409073dac1SGeert Uytterhoeven return number(buf, end, (unsigned long int)ptr, spec); 7419073dac1SGeert Uytterhoeven } 7429073dac1SGeert Uytterhoeven 7439073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */ 7449073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init; 7459073dac1SGeert Uytterhoeven 7469073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str) 7479073dac1SGeert Uytterhoeven { 7489073dac1SGeert Uytterhoeven debug_boot_weak_hash = 1; 7499073dac1SGeert Uytterhoeven pr_info("debug_boot_weak_hash enabled\n"); 7509073dac1SGeert Uytterhoeven return 0; 7519073dac1SGeert Uytterhoeven } 7529073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); 7539073dac1SGeert Uytterhoeven 754e4279b59SSebastian Andrzej Siewior static bool filled_random_ptr_key __read_mostly; 7556f0ac3b5SSebastian Andrzej Siewior static siphash_key_t ptr_key __read_mostly; 7569073dac1SGeert Uytterhoeven 757*898f1e5cSJason A. Donenfeld static int fill_ptr_key(struct notifier_block *nb, unsigned long action, void *data) 7589073dac1SGeert Uytterhoeven { 7596f0ac3b5SSebastian Andrzej Siewior get_random_bytes(&ptr_key, sizeof(ptr_key)); 7606f0ac3b5SSebastian Andrzej Siewior 7616f0ac3b5SSebastian Andrzej Siewior /* Pairs with smp_rmb() before reading ptr_key. */ 7626f0ac3b5SSebastian Andrzej Siewior smp_wmb(); 7636f0ac3b5SSebastian Andrzej Siewior WRITE_ONCE(filled_random_ptr_key, true); 764*898f1e5cSJason A. Donenfeld return NOTIFY_DONE; 7656f0ac3b5SSebastian Andrzej Siewior } 7666f0ac3b5SSebastian Andrzej Siewior 7676f0ac3b5SSebastian Andrzej Siewior static int __init vsprintf_init_hashval(void) 7686f0ac3b5SSebastian Andrzej Siewior { 769*898f1e5cSJason A. Donenfeld static struct notifier_block fill_ptr_key_nb = { .notifier_call = fill_ptr_key }; 770*898f1e5cSJason A. Donenfeld execute_with_initialized_rng(&fill_ptr_key_nb); 7716f0ac3b5SSebastian Andrzej Siewior return 0; 7726f0ac3b5SSebastian Andrzej Siewior } 7736f0ac3b5SSebastian Andrzej Siewior subsys_initcall(vsprintf_init_hashval) 7749073dac1SGeert Uytterhoeven 7759073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */ 776e4dcad20SJoel Fernandes (Google) static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 7779073dac1SGeert Uytterhoeven { 7789073dac1SGeert Uytterhoeven unsigned long hashval; 7799073dac1SGeert Uytterhoeven 7806f0ac3b5SSebastian Andrzej Siewior if (!READ_ONCE(filled_random_ptr_key)) 7816f0ac3b5SSebastian Andrzej Siewior return -EBUSY; 7826701de6cSJason A. Donenfeld 783e4279b59SSebastian Andrzej Siewior /* Pairs with smp_wmb() after writing ptr_key. */ 784e4279b59SSebastian Andrzej Siewior smp_rmb(); 7856701de6cSJason A. Donenfeld 7869073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT 7879073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); 7889073dac1SGeert Uytterhoeven /* 7899073dac1SGeert Uytterhoeven * Mask off the first 32 bits, this makes explicit that we have 7909073dac1SGeert Uytterhoeven * modified the address (and 32 bits is plenty for a unique ID). 7919073dac1SGeert Uytterhoeven */ 7929073dac1SGeert Uytterhoeven hashval = hashval & 0xffffffff; 7939073dac1SGeert Uytterhoeven #else 7949073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); 7959073dac1SGeert Uytterhoeven #endif 796e4dcad20SJoel Fernandes (Google) *hashval_out = hashval; 797e4dcad20SJoel Fernandes (Google) return 0; 798e4dcad20SJoel Fernandes (Google) } 799e4dcad20SJoel Fernandes (Google) 800e4dcad20SJoel Fernandes (Google) int ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 801e4dcad20SJoel Fernandes (Google) { 802e4dcad20SJoel Fernandes (Google) return __ptr_to_hashval(ptr, hashval_out); 803e4dcad20SJoel Fernandes (Google) } 804e4dcad20SJoel Fernandes (Google) 805e4dcad20SJoel Fernandes (Google) static char *ptr_to_id(char *buf, char *end, const void *ptr, 806e4dcad20SJoel Fernandes (Google) struct printf_spec spec) 807e4dcad20SJoel Fernandes (Google) { 808e4dcad20SJoel Fernandes (Google) const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; 809e4dcad20SJoel Fernandes (Google) unsigned long hashval; 810e4dcad20SJoel Fernandes (Google) int ret; 811e4dcad20SJoel Fernandes (Google) 8127bd57fbcSIlya Dryomov /* 8137bd57fbcSIlya Dryomov * Print the real pointer value for NULL and error pointers, 8147bd57fbcSIlya Dryomov * as they are not actual addresses. 8157bd57fbcSIlya Dryomov */ 8167bd57fbcSIlya Dryomov if (IS_ERR_OR_NULL(ptr)) 8177bd57fbcSIlya Dryomov return pointer_string(buf, end, ptr, spec); 8187bd57fbcSIlya Dryomov 819e4dcad20SJoel Fernandes (Google) /* When debugging early boot use non-cryptographically secure hash. */ 820e4dcad20SJoel Fernandes (Google) if (unlikely(debug_boot_weak_hash)) { 821e4dcad20SJoel Fernandes (Google) hashval = hash_long((unsigned long)ptr, 32); 822e4dcad20SJoel Fernandes (Google) return pointer_string(buf, end, (const void *)hashval, spec); 823e4dcad20SJoel Fernandes (Google) } 824e4dcad20SJoel Fernandes (Google) 825e4dcad20SJoel Fernandes (Google) ret = __ptr_to_hashval(ptr, &hashval); 826e4dcad20SJoel Fernandes (Google) if (ret) { 827e4dcad20SJoel Fernandes (Google) spec.field_width = 2 * sizeof(ptr); 828e4dcad20SJoel Fernandes (Google) /* string length must be less than default_width */ 829e4dcad20SJoel Fernandes (Google) return error_string(buf, end, str, spec); 830e4dcad20SJoel Fernandes (Google) } 831e4dcad20SJoel Fernandes (Google) 8329073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec); 8339073dac1SGeert Uytterhoeven } 8349073dac1SGeert Uytterhoeven 83584842911SChristophe Leroy static char *default_pointer(char *buf, char *end, const void *ptr, 83684842911SChristophe Leroy struct printf_spec spec) 83784842911SChristophe Leroy { 83884842911SChristophe Leroy /* 83984842911SChristophe Leroy * default is to _not_ leak addresses, so hash before printing, 84084842911SChristophe Leroy * unless no_hash_pointers is specified on the command line. 84184842911SChristophe Leroy */ 84284842911SChristophe Leroy if (unlikely(no_hash_pointers)) 84384842911SChristophe Leroy return pointer_string(buf, end, ptr, spec); 84484842911SChristophe Leroy 84584842911SChristophe Leroy return ptr_to_id(buf, end, ptr, spec); 84684842911SChristophe Leroy } 84784842911SChristophe Leroy 8486eea242fSPetr Mladek int kptr_restrict __read_mostly; 8496eea242fSPetr Mladek 8506eea242fSPetr Mladek static noinline_for_stack 8516eea242fSPetr Mladek char *restricted_pointer(char *buf, char *end, const void *ptr, 8526eea242fSPetr Mladek struct printf_spec spec) 8536eea242fSPetr Mladek { 8546eea242fSPetr Mladek switch (kptr_restrict) { 8556eea242fSPetr Mladek case 0: 8561ac2f978SPetr Mladek /* Handle as %p, hash and do _not_ leak addresses. */ 85784842911SChristophe Leroy return default_pointer(buf, end, ptr, spec); 8586eea242fSPetr Mladek case 1: { 8596eea242fSPetr Mladek const struct cred *cred; 8606eea242fSPetr Mladek 8616eea242fSPetr Mladek /* 8626eea242fSPetr Mladek * kptr_restrict==1 cannot be used in IRQ context 8636eea242fSPetr Mladek * because its test for CAP_SYSLOG would be meaningless. 8646eea242fSPetr Mladek */ 8656eea242fSPetr Mladek if (in_irq() || in_serving_softirq() || in_nmi()) { 8666eea242fSPetr Mladek if (spec.field_width == -1) 8676eea242fSPetr Mladek spec.field_width = 2 * sizeof(ptr); 868c8c3b584SPetr Mladek return error_string(buf, end, "pK-error", spec); 8696eea242fSPetr Mladek } 8706eea242fSPetr Mladek 8716eea242fSPetr Mladek /* 8726eea242fSPetr Mladek * Only print the real pointer value if the current 8736eea242fSPetr Mladek * process has CAP_SYSLOG and is running with the 8746eea242fSPetr Mladek * same credentials it started with. This is because 8756eea242fSPetr Mladek * access to files is checked at open() time, but %pK 8766eea242fSPetr Mladek * checks permission at read() time. We don't want to 8776eea242fSPetr Mladek * leak pointer values if a binary opens a file using 8786eea242fSPetr Mladek * %pK and then elevates privileges before reading it. 8796eea242fSPetr Mladek */ 8806eea242fSPetr Mladek cred = current_cred(); 8816eea242fSPetr Mladek if (!has_capability_noaudit(current, CAP_SYSLOG) || 8826eea242fSPetr Mladek !uid_eq(cred->euid, cred->uid) || 8836eea242fSPetr Mladek !gid_eq(cred->egid, cred->gid)) 8846eea242fSPetr Mladek ptr = NULL; 8856eea242fSPetr Mladek break; 8866eea242fSPetr Mladek } 8876eea242fSPetr Mladek case 2: 8886eea242fSPetr Mladek default: 8896eea242fSPetr Mladek /* Always print 0's for %pK */ 8906eea242fSPetr Mladek ptr = NULL; 8916eea242fSPetr Mladek break; 8926eea242fSPetr Mladek } 8936eea242fSPetr Mladek 8946eea242fSPetr Mladek return pointer_string(buf, end, ptr, spec); 8956eea242fSPetr Mladek } 8966eea242fSPetr Mladek 8979073dac1SGeert Uytterhoeven static noinline_for_stack 8984b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 8994b6ccca7SAl Viro const char *fmt) 9004b6ccca7SAl Viro { 9014b6ccca7SAl Viro const char *array[4], *s; 9024b6ccca7SAl Viro const struct dentry *p; 9034b6ccca7SAl Viro int depth; 9044b6ccca7SAl Viro int i, n; 9054b6ccca7SAl Viro 9064b6ccca7SAl Viro switch (fmt[1]) { 9074b6ccca7SAl Viro case '2': case '3': case '4': 9084b6ccca7SAl Viro depth = fmt[1] - '0'; 9094b6ccca7SAl Viro break; 9104b6ccca7SAl Viro default: 9114b6ccca7SAl Viro depth = 1; 9124b6ccca7SAl Viro } 9134b6ccca7SAl Viro 9144b6ccca7SAl Viro rcu_read_lock(); 9154b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 9163e5903ebSPetr Mladek if (check_pointer(&buf, end, d, spec)) { 9173e5903ebSPetr Mladek rcu_read_unlock(); 9183e5903ebSPetr Mladek return buf; 9193e5903ebSPetr Mladek } 9203e5903ebSPetr Mladek 9216aa7de05SMark Rutland p = READ_ONCE(d->d_parent); 9226aa7de05SMark Rutland array[i] = READ_ONCE(d->d_name.name); 9234b6ccca7SAl Viro if (p == d) { 9244b6ccca7SAl Viro if (i) 9254b6ccca7SAl Viro array[i] = ""; 9264b6ccca7SAl Viro i++; 9274b6ccca7SAl Viro break; 9284b6ccca7SAl Viro } 9294b6ccca7SAl Viro } 9304b6ccca7SAl Viro s = array[--i]; 9314b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 9324b6ccca7SAl Viro char c = *s++; 9334b6ccca7SAl Viro if (!c) { 9344b6ccca7SAl Viro if (!i) 9354b6ccca7SAl Viro break; 9364b6ccca7SAl Viro c = '/'; 9374b6ccca7SAl Viro s = array[--i]; 9384b6ccca7SAl Viro } 9394b6ccca7SAl Viro if (buf < end) 9404b6ccca7SAl Viro *buf = c; 9414b6ccca7SAl Viro } 9424b6ccca7SAl Viro rcu_read_unlock(); 943cfccde04SRasmus Villemoes return widen_string(buf, n, end, spec); 9444b6ccca7SAl Viro } 9454b6ccca7SAl Viro 94636594b31SJia He static noinline_for_stack 94736594b31SJia He char *file_dentry_name(char *buf, char *end, const struct file *f, 94836594b31SJia He struct printf_spec spec, const char *fmt) 94936594b31SJia He { 95036594b31SJia He if (check_pointer(&buf, end, f, spec)) 95136594b31SJia He return buf; 95236594b31SJia He 95336594b31SJia He return dentry_name(buf, end, f->f_path.dentry, spec, fmt); 95436594b31SJia He } 9551031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 9561031bc58SDmitry Monakhov static noinline_for_stack 9571031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev, 9581031bc58SDmitry Monakhov struct printf_spec spec, const char *fmt) 9591031bc58SDmitry Monakhov { 9603e5903ebSPetr Mladek struct gendisk *hd; 9611031bc58SDmitry Monakhov 9623e5903ebSPetr Mladek if (check_pointer(&buf, end, bdev, spec)) 9633e5903ebSPetr Mladek return buf; 9643e5903ebSPetr Mladek 9653e5903ebSPetr Mladek hd = bdev->bd_disk; 9661031bc58SDmitry Monakhov buf = string(buf, end, hd->disk_name, spec); 967700cd59dSChristoph Hellwig if (bdev->bd_partno) { 9681031bc58SDmitry Monakhov if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { 9691031bc58SDmitry Monakhov if (buf < end) 9701031bc58SDmitry Monakhov *buf = 'p'; 9711031bc58SDmitry Monakhov buf++; 9721031bc58SDmitry Monakhov } 973700cd59dSChristoph Hellwig buf = number(buf, end, bdev->bd_partno, spec); 9741031bc58SDmitry Monakhov } 9751031bc58SDmitry Monakhov return buf; 9761031bc58SDmitry Monakhov } 9771031bc58SDmitry Monakhov #endif 9781031bc58SDmitry Monakhov 979cf3b429bSJoe Perches static noinline_for_stack 980cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 981b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 9820fe1ef24SLinus Torvalds { 983b0d33c2bSJoe Perches unsigned long value; 9840fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 9850fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 986b0d33c2bSJoe Perches #endif 987b0d33c2bSJoe Perches 988b0d33c2bSJoe Perches if (fmt[1] == 'R') 989b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 990b0d33c2bSJoe Perches value = (unsigned long)ptr; 991b0d33c2bSJoe Perches 992b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 9939294523eSStephen Boyd if (*fmt == 'B' && fmt[1] == 'b') 9949294523eSStephen Boyd sprint_backtrace_build_id(sym, value); 9959294523eSStephen Boyd else if (*fmt == 'B') 9960f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 9979294523eSStephen Boyd else if (*fmt == 'S' && (fmt[1] == 'b' || (fmt[1] == 'R' && fmt[2] == 'b'))) 9989294523eSStephen Boyd sprint_symbol_build_id(sym, value); 9999af77064SSakari Ailus else if (*fmt != 's') 10000fe1ef24SLinus Torvalds sprint_symbol(sym, value); 10010c8b946eSFrederic Weisbecker else 10024796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 10037b9186f5SAndré Goddard Rosa 1004d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 10050fe1ef24SLinus Torvalds #else 10063cab1e71SAndy Shevchenko return special_hex_number(buf, end, value, sizeof(void *)); 10070fe1ef24SLinus Torvalds #endif 10080fe1ef24SLinus Torvalds } 10090fe1ef24SLinus Torvalds 1010abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = { 1011abd4fe62SAndy Shevchenko .field_width = -1, 1012abd4fe62SAndy Shevchenko .precision = -1, 1013abd4fe62SAndy Shevchenko }; 1014abd4fe62SAndy Shevchenko 101554433973SAndy Shevchenko static const struct printf_spec default_flag_spec = { 101654433973SAndy Shevchenko .base = 16, 101754433973SAndy Shevchenko .precision = -1, 101854433973SAndy Shevchenko .flags = SPECIAL | SMALL, 101954433973SAndy Shevchenko }; 102054433973SAndy Shevchenko 1021ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = { 1022ce0b4910SAndy Shevchenko .base = 10, 1023ce0b4910SAndy Shevchenko .precision = -1, 1024ce0b4910SAndy Shevchenko }; 1025ce0b4910SAndy Shevchenko 10264d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = { 10274d42c447SAndy Shevchenko .base = 10, 10284d42c447SAndy Shevchenko .field_width = 2, 10294d42c447SAndy Shevchenko .precision = -1, 10304d42c447SAndy Shevchenko .flags = ZEROPAD, 10314d42c447SAndy Shevchenko }; 10324d42c447SAndy Shevchenko 10334d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = { 10344d42c447SAndy Shevchenko .base = 10, 10354d42c447SAndy Shevchenko .field_width = 4, 10364d42c447SAndy Shevchenko .precision = -1, 10374d42c447SAndy Shevchenko .flags = ZEROPAD, 10384d42c447SAndy Shevchenko }; 10394d42c447SAndy Shevchenko 1040cf3b429bSJoe Perches static noinline_for_stack 1041cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 1042fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 1043332d2e78SLinus Torvalds { 1044332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 104528405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 1046332d2e78SLinus Torvalds #endif 1047332d2e78SLinus Torvalds 1048332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 104928405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 1050332d2e78SLinus Torvalds #endif 10514da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 1052fef20d9cSFrederic Weisbecker .base = 16, 10534da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 1054fef20d9cSFrederic Weisbecker .precision = -1, 1055fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 1056fef20d9cSFrederic Weisbecker }; 10574da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 10584da0b66cSBjorn Helgaas .base = 16, 10594da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 10604da0b66cSBjorn Helgaas .precision = -1, 10614da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 10624da0b66cSBjorn Helgaas }; 10630f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 10640f4050c7SBjorn Helgaas .base = 16, 10650f4050c7SBjorn Helgaas .field_width = 2, 10660f4050c7SBjorn Helgaas .precision = -1, 10670f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 10680f4050c7SBjorn Helgaas }; 10694da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 1070fd95541eSBjorn Helgaas .field_width = -1, 1071fd95541eSBjorn Helgaas .precision = 10, 1072fd95541eSBjorn Helgaas .flags = LEFT, 1073fd95541eSBjorn Helgaas }; 1074c7dabef8SBjorn Helgaas 1075c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 1076c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 1077c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 1078c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 10799d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 1080c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 1081c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 1082c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 1083c7dabef8SBjorn Helgaas 1084332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 1085c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 10864da0b66cSBjorn Helgaas const struct printf_spec *specp; 1087332d2e78SLinus Torvalds 10883e5903ebSPetr Mladek if (check_pointer(&buf, end, res, spec)) 10893e5903ebSPetr Mladek return buf; 10903e5903ebSPetr Mladek 1091332d2e78SLinus Torvalds *p++ = '['; 10924da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 1093d529ac41SPetr Mladek p = string_nocheck(p, pend, "io ", str_spec); 10944da0b66cSBjorn Helgaas specp = &io_spec; 10954da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 1096d529ac41SPetr Mladek p = string_nocheck(p, pend, "mem ", str_spec); 10974da0b66cSBjorn Helgaas specp = &mem_spec; 10984da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 1099d529ac41SPetr Mladek p = string_nocheck(p, pend, "irq ", str_spec); 1100ce0b4910SAndy Shevchenko specp = &default_dec_spec; 11014da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 1102d529ac41SPetr Mladek p = string_nocheck(p, pend, "dma ", str_spec); 1103ce0b4910SAndy Shevchenko specp = &default_dec_spec; 11040f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 1105d529ac41SPetr Mladek p = string_nocheck(p, pend, "bus ", str_spec); 11060f4050c7SBjorn Helgaas specp = &bus_spec; 11074da0b66cSBjorn Helgaas } else { 1108d529ac41SPetr Mladek p = string_nocheck(p, pend, "??? ", str_spec); 11094da0b66cSBjorn Helgaas specp = &mem_spec; 1110c7dabef8SBjorn Helgaas decode = 0; 1111fd95541eSBjorn Helgaas } 1112d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 1113d529ac41SPetr Mladek p = string_nocheck(p, pend, "size ", str_spec); 1114d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 1115d19cb803SBjorn Helgaas } else { 11164da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 1117c91d3376SBjorn Helgaas if (res->start != res->end) { 1118332d2e78SLinus Torvalds *p++ = '-'; 11194da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 1120c91d3376SBjorn Helgaas } 1121d19cb803SBjorn Helgaas } 1122c7dabef8SBjorn Helgaas if (decode) { 1123fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 1124d529ac41SPetr Mladek p = string_nocheck(p, pend, " 64bit", str_spec); 1125fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 1126d529ac41SPetr Mladek p = string_nocheck(p, pend, " pref", str_spec); 11279d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 1128d529ac41SPetr Mladek p = string_nocheck(p, pend, " window", str_spec); 1129fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 1130d529ac41SPetr Mladek p = string_nocheck(p, pend, " disabled", str_spec); 1131c7dabef8SBjorn Helgaas } else { 1132d529ac41SPetr Mladek p = string_nocheck(p, pend, " flags ", str_spec); 113354433973SAndy Shevchenko p = number(p, pend, res->flags, default_flag_spec); 1134fd95541eSBjorn Helgaas } 1135332d2e78SLinus Torvalds *p++ = ']'; 1136c7dabef8SBjorn Helgaas *p = '\0'; 1137332d2e78SLinus Torvalds 1138d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 1139332d2e78SLinus Torvalds } 1140332d2e78SLinus Torvalds 1141cf3b429bSJoe Perches static noinline_for_stack 114231550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 114331550a16SAndy Shevchenko const char *fmt) 114431550a16SAndy Shevchenko { 1145360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 114631550a16SAndy Shevchenko negative value, fallback to the default */ 114731550a16SAndy Shevchenko char separator; 114831550a16SAndy Shevchenko 114931550a16SAndy Shevchenko if (spec.field_width == 0) 115031550a16SAndy Shevchenko /* nothing to print */ 115131550a16SAndy Shevchenko return buf; 115231550a16SAndy Shevchenko 11533e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 11543e5903ebSPetr Mladek return buf; 115531550a16SAndy Shevchenko 115631550a16SAndy Shevchenko switch (fmt[1]) { 115731550a16SAndy Shevchenko case 'C': 115831550a16SAndy Shevchenko separator = ':'; 115931550a16SAndy Shevchenko break; 116031550a16SAndy Shevchenko case 'D': 116131550a16SAndy Shevchenko separator = '-'; 116231550a16SAndy Shevchenko break; 116331550a16SAndy Shevchenko case 'N': 116431550a16SAndy Shevchenko separator = 0; 116531550a16SAndy Shevchenko break; 116631550a16SAndy Shevchenko default: 116731550a16SAndy Shevchenko separator = ' '; 116831550a16SAndy Shevchenko break; 116931550a16SAndy Shevchenko } 117031550a16SAndy Shevchenko 117131550a16SAndy Shevchenko if (spec.field_width > 0) 117231550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 117331550a16SAndy Shevchenko 11749c98f235SRasmus Villemoes for (i = 0; i < len; ++i) { 11759c98f235SRasmus Villemoes if (buf < end) 11769c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]); 11779c98f235SRasmus Villemoes ++buf; 11789c98f235SRasmus Villemoes if (buf < end) 11799c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]); 11809c98f235SRasmus Villemoes ++buf; 118131550a16SAndy Shevchenko 11829c98f235SRasmus Villemoes if (separator && i != len - 1) { 11839c98f235SRasmus Villemoes if (buf < end) 11849c98f235SRasmus Villemoes *buf = separator; 11859c98f235SRasmus Villemoes ++buf; 11869c98f235SRasmus Villemoes } 118731550a16SAndy Shevchenko } 118831550a16SAndy Shevchenko 118931550a16SAndy Shevchenko return buf; 119031550a16SAndy Shevchenko } 119131550a16SAndy Shevchenko 119231550a16SAndy Shevchenko static noinline_for_stack 1193dc453dd8SJian Shen char *bitmap_string(char *buf, char *end, const unsigned long *bitmap, 1194dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1195dbc760bcSTejun Heo { 1196dbc760bcSTejun Heo const int CHUNKSZ = 32; 1197dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1198dbc760bcSTejun Heo int i, chunksz; 1199dbc760bcSTejun Heo bool first = true; 1200dbc760bcSTejun Heo 12013e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec)) 12023e5903ebSPetr Mladek return buf; 12033e5903ebSPetr Mladek 1204dbc760bcSTejun Heo /* reused to print numbers */ 1205dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 1206dbc760bcSTejun Heo 1207dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1); 1208dbc760bcSTejun Heo if (chunksz == 0) 1209dbc760bcSTejun Heo chunksz = CHUNKSZ; 1210dbc760bcSTejun Heo 1211dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 1212dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) { 1213dbc760bcSTejun Heo u32 chunkmask, val; 1214dbc760bcSTejun Heo int word, bit; 1215dbc760bcSTejun Heo 1216dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1); 1217dbc760bcSTejun Heo word = i / BITS_PER_LONG; 1218dbc760bcSTejun Heo bit = i % BITS_PER_LONG; 1219dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask; 1220dbc760bcSTejun Heo 1221dbc760bcSTejun Heo if (!first) { 1222dbc760bcSTejun Heo if (buf < end) 1223dbc760bcSTejun Heo *buf = ','; 1224dbc760bcSTejun Heo buf++; 1225dbc760bcSTejun Heo } 1226dbc760bcSTejun Heo first = false; 1227dbc760bcSTejun Heo 1228dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4); 1229dbc760bcSTejun Heo buf = number(buf, end, val, spec); 1230dbc760bcSTejun Heo 1231dbc760bcSTejun Heo chunksz = CHUNKSZ; 1232dbc760bcSTejun Heo } 1233dbc760bcSTejun Heo return buf; 1234dbc760bcSTejun Heo } 1235dbc760bcSTejun Heo 1236dbc760bcSTejun Heo static noinline_for_stack 1237dc453dd8SJian Shen char *bitmap_list_string(char *buf, char *end, const unsigned long *bitmap, 1238dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1239dbc760bcSTejun Heo { 1240dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1241dbc760bcSTejun Heo bool first = true; 124215325b4fSYury Norov int rbot, rtop; 1243dbc760bcSTejun Heo 12443e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec)) 12453e5903ebSPetr Mladek return buf; 12463e5903ebSPetr Mladek 124715325b4fSYury Norov for_each_set_bitrange(rbot, rtop, bitmap, nr_bits) { 1248dbc760bcSTejun Heo if (!first) { 1249dbc760bcSTejun Heo if (buf < end) 1250dbc760bcSTejun Heo *buf = ','; 1251dbc760bcSTejun Heo buf++; 1252dbc760bcSTejun Heo } 1253dbc760bcSTejun Heo first = false; 1254dbc760bcSTejun Heo 1255ce0b4910SAndy Shevchenko buf = number(buf, end, rbot, default_dec_spec); 125615325b4fSYury Norov if (rtop == rbot + 1) 125715325b4fSYury Norov continue; 125815325b4fSYury Norov 1259dbc760bcSTejun Heo if (buf < end) 1260dbc760bcSTejun Heo *buf = '-'; 126115325b4fSYury Norov buf = number(++buf, end, rtop - 1, default_dec_spec); 1262dbc760bcSTejun Heo } 1263dbc760bcSTejun Heo return buf; 1264dbc760bcSTejun Heo } 1265dbc760bcSTejun Heo 1266dbc760bcSTejun Heo static noinline_for_stack 1267cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 12688a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 1269dd45c9cfSHarvey Harrison { 12708a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 1271dd45c9cfSHarvey Harrison char *p = mac_addr; 1272dd45c9cfSHarvey Harrison int i; 1273bc7259a2SJoe Perches char separator; 127476597ff9SAndrei Emeltchenko bool reversed = false; 1275bc7259a2SJoe Perches 12763e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 12773e5903ebSPetr Mladek return buf; 12783e5903ebSPetr Mladek 127976597ff9SAndrei Emeltchenko switch (fmt[1]) { 128076597ff9SAndrei Emeltchenko case 'F': 1281bc7259a2SJoe Perches separator = '-'; 128276597ff9SAndrei Emeltchenko break; 128376597ff9SAndrei Emeltchenko 128476597ff9SAndrei Emeltchenko case 'R': 128576597ff9SAndrei Emeltchenko reversed = true; 12864c1ca831SNick Desaulniers fallthrough; 128776597ff9SAndrei Emeltchenko 128876597ff9SAndrei Emeltchenko default: 1289bc7259a2SJoe Perches separator = ':'; 129076597ff9SAndrei Emeltchenko break; 1291bc7259a2SJoe Perches } 1292dd45c9cfSHarvey Harrison 1293dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 129476597ff9SAndrei Emeltchenko if (reversed) 129576597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 129676597ff9SAndrei Emeltchenko else 129755036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 129876597ff9SAndrei Emeltchenko 12998a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 1300bc7259a2SJoe Perches *p++ = separator; 1301dd45c9cfSHarvey Harrison } 1302dd45c9cfSHarvey Harrison *p = '\0'; 1303dd45c9cfSHarvey Harrison 1304d529ac41SPetr Mladek return string_nocheck(buf, end, mac_addr, spec); 1305dd45c9cfSHarvey Harrison } 1306dd45c9cfSHarvey Harrison 1307cf3b429bSJoe Perches static noinline_for_stack 1308cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 1309689afa7dSHarvey Harrison { 1310689afa7dSHarvey Harrison int i; 13110159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 13120159f24eSJoe Perches int index; 13130159f24eSJoe Perches int step; 1314689afa7dSHarvey Harrison 13150159f24eSJoe Perches switch (fmt[2]) { 13160159f24eSJoe Perches case 'h': 13170159f24eSJoe Perches #ifdef __BIG_ENDIAN 13180159f24eSJoe Perches index = 0; 13190159f24eSJoe Perches step = 1; 13200159f24eSJoe Perches #else 13210159f24eSJoe Perches index = 3; 13220159f24eSJoe Perches step = -1; 13230159f24eSJoe Perches #endif 13240159f24eSJoe Perches break; 13250159f24eSJoe Perches case 'l': 13260159f24eSJoe Perches index = 3; 13270159f24eSJoe Perches step = -1; 13280159f24eSJoe Perches break; 13290159f24eSJoe Perches case 'n': 13300159f24eSJoe Perches case 'b': 13310159f24eSJoe Perches default: 13320159f24eSJoe Perches index = 0; 13330159f24eSJoe Perches step = 1; 13340159f24eSJoe Perches break; 13350159f24eSJoe Perches } 13368a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 13377c43d9a3SRasmus Villemoes char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 1338133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 13398a27f7c9SJoe Perches if (leading_zeros) { 13408a27f7c9SJoe Perches if (digits < 3) 13418a27f7c9SJoe Perches *p++ = '0'; 13428a27f7c9SJoe Perches if (digits < 2) 13438a27f7c9SJoe Perches *p++ = '0'; 13448a27f7c9SJoe Perches } 13458a27f7c9SJoe Perches /* reverse the digits in the quad */ 13468a27f7c9SJoe Perches while (digits--) 13478a27f7c9SJoe Perches *p++ = temp[digits]; 13488a27f7c9SJoe Perches if (i < 3) 13498a27f7c9SJoe Perches *p++ = '.'; 13500159f24eSJoe Perches index += step; 13518a27f7c9SJoe Perches } 13528a27f7c9SJoe Perches *p = '\0'; 13537b9186f5SAndré Goddard Rosa 13548a27f7c9SJoe Perches return p; 13558a27f7c9SJoe Perches } 13568a27f7c9SJoe Perches 1357cf3b429bSJoe Perches static noinline_for_stack 1358cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 13598a27f7c9SJoe Perches { 13607b9186f5SAndré Goddard Rosa int i, j, range; 13618a27f7c9SJoe Perches unsigned char zerolength[8]; 13628a27f7c9SJoe Perches int longest = 1; 13638a27f7c9SJoe Perches int colonpos = -1; 13648a27f7c9SJoe Perches u16 word; 13657b9186f5SAndré Goddard Rosa u8 hi, lo; 13668a27f7c9SJoe Perches bool needcolon = false; 1367eb78cd26SJoe Perches bool useIPv4; 1368eb78cd26SJoe Perches struct in6_addr in6; 1369eb78cd26SJoe Perches 1370eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 1371eb78cd26SJoe Perches 1372eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 13738a27f7c9SJoe Perches 13748a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 13758a27f7c9SJoe Perches 13768a27f7c9SJoe Perches if (useIPv4) 13778a27f7c9SJoe Perches range = 6; 13788a27f7c9SJoe Perches else 13798a27f7c9SJoe Perches range = 8; 13808a27f7c9SJoe Perches 13818a27f7c9SJoe Perches /* find position of longest 0 run */ 13828a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13838a27f7c9SJoe Perches for (j = i; j < range; j++) { 1384eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 13858a27f7c9SJoe Perches break; 13868a27f7c9SJoe Perches zerolength[i]++; 13878a27f7c9SJoe Perches } 13888a27f7c9SJoe Perches } 13898a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13908a27f7c9SJoe Perches if (zerolength[i] > longest) { 13918a27f7c9SJoe Perches longest = zerolength[i]; 13928a27f7c9SJoe Perches colonpos = i; 13938a27f7c9SJoe Perches } 13948a27f7c9SJoe Perches } 139529cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 139629cf519eSJoe Perches colonpos = -1; 13978a27f7c9SJoe Perches 13988a27f7c9SJoe Perches /* emit address */ 13998a27f7c9SJoe Perches for (i = 0; i < range; i++) { 14008a27f7c9SJoe Perches if (i == colonpos) { 14018a27f7c9SJoe Perches if (needcolon || i == 0) 14028a27f7c9SJoe Perches *p++ = ':'; 14038a27f7c9SJoe Perches *p++ = ':'; 14048a27f7c9SJoe Perches needcolon = false; 14058a27f7c9SJoe Perches i += longest - 1; 14068a27f7c9SJoe Perches continue; 14078a27f7c9SJoe Perches } 14088a27f7c9SJoe Perches if (needcolon) { 14098a27f7c9SJoe Perches *p++ = ':'; 14108a27f7c9SJoe Perches needcolon = false; 14118a27f7c9SJoe Perches } 14128a27f7c9SJoe Perches /* hex u16 without leading 0s */ 1413eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 14148a27f7c9SJoe Perches hi = word >> 8; 14158a27f7c9SJoe Perches lo = word & 0xff; 14168a27f7c9SJoe Perches if (hi) { 14178a27f7c9SJoe Perches if (hi > 0x0f) 141855036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 14198a27f7c9SJoe Perches else 14208a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 142155036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 14228a27f7c9SJoe Perches } 1423b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 142455036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 14258a27f7c9SJoe Perches else 14268a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 14278a27f7c9SJoe Perches needcolon = true; 14288a27f7c9SJoe Perches } 14298a27f7c9SJoe Perches 14308a27f7c9SJoe Perches if (useIPv4) { 14318a27f7c9SJoe Perches if (needcolon) 14328a27f7c9SJoe Perches *p++ = ':'; 14330159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 14348a27f7c9SJoe Perches } 14358a27f7c9SJoe Perches *p = '\0'; 14367b9186f5SAndré Goddard Rosa 14378a27f7c9SJoe Perches return p; 14388a27f7c9SJoe Perches } 14398a27f7c9SJoe Perches 1440cf3b429bSJoe Perches static noinline_for_stack 1441cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 14428a27f7c9SJoe Perches { 14438a27f7c9SJoe Perches int i; 14447b9186f5SAndré Goddard Rosa 1445689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 144655036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 144755036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 14488a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 1449689afa7dSHarvey Harrison *p++ = ':'; 1450689afa7dSHarvey Harrison } 1451689afa7dSHarvey Harrison *p = '\0'; 14527b9186f5SAndré Goddard Rosa 14538a27f7c9SJoe Perches return p; 14548a27f7c9SJoe Perches } 14558a27f7c9SJoe Perches 1456cf3b429bSJoe Perches static noinline_for_stack 1457cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 14588a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 14598a27f7c9SJoe Perches { 14608a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 14618a27f7c9SJoe Perches 14628a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 1463eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 14648a27f7c9SJoe Perches else 1465eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 1466689afa7dSHarvey Harrison 1467d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 1468689afa7dSHarvey Harrison } 1469689afa7dSHarvey Harrison 1470cf3b429bSJoe Perches static noinline_for_stack 1471cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 14728a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 14734aa99606SHarvey Harrison { 14748a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 14754aa99606SHarvey Harrison 14760159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 14774aa99606SHarvey Harrison 1478d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 14794aa99606SHarvey Harrison } 14804aa99606SHarvey Harrison 1481cf3b429bSJoe Perches static noinline_for_stack 148210679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 148310679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 148410679643SDaniel Borkmann { 148510679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 148610679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 148710679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 148810679643SDaniel Borkmann sizeof("%1234567890")]; 148910679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 149010679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 149110679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 149210679643SDaniel Borkmann u8 off = 0; 149310679643SDaniel Borkmann 149410679643SDaniel Borkmann fmt++; 149510679643SDaniel Borkmann while (isalpha(*++fmt)) { 149610679643SDaniel Borkmann switch (*fmt) { 149710679643SDaniel Borkmann case 'p': 149810679643SDaniel Borkmann have_p = true; 149910679643SDaniel Borkmann break; 150010679643SDaniel Borkmann case 'f': 150110679643SDaniel Borkmann have_f = true; 150210679643SDaniel Borkmann break; 150310679643SDaniel Borkmann case 's': 150410679643SDaniel Borkmann have_s = true; 150510679643SDaniel Borkmann break; 150610679643SDaniel Borkmann case 'c': 150710679643SDaniel Borkmann have_c = true; 150810679643SDaniel Borkmann break; 150910679643SDaniel Borkmann } 151010679643SDaniel Borkmann } 151110679643SDaniel Borkmann 151210679643SDaniel Borkmann if (have_p || have_s || have_f) { 151310679643SDaniel Borkmann *p = '['; 151410679643SDaniel Borkmann off = 1; 151510679643SDaniel Borkmann } 151610679643SDaniel Borkmann 151710679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 151810679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 151910679643SDaniel Borkmann else 152010679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 152110679643SDaniel Borkmann 152210679643SDaniel Borkmann if (have_p || have_s || have_f) 152310679643SDaniel Borkmann *p++ = ']'; 152410679643SDaniel Borkmann 152510679643SDaniel Borkmann if (have_p) { 152610679643SDaniel Borkmann *p++ = ':'; 152710679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 152810679643SDaniel Borkmann } 152910679643SDaniel Borkmann if (have_f) { 153010679643SDaniel Borkmann *p++ = '/'; 153110679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 153210679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 153310679643SDaniel Borkmann } 153410679643SDaniel Borkmann if (have_s) { 153510679643SDaniel Borkmann *p++ = '%'; 153610679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 153710679643SDaniel Borkmann } 153810679643SDaniel Borkmann *p = '\0'; 153910679643SDaniel Borkmann 1540d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 154110679643SDaniel Borkmann } 154210679643SDaniel Borkmann 154310679643SDaniel Borkmann static noinline_for_stack 154410679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 154510679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 154610679643SDaniel Borkmann { 154710679643SDaniel Borkmann bool have_p = false; 154810679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 154910679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 155010679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 155110679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 155210679643SDaniel Borkmann 155310679643SDaniel Borkmann fmt++; 155410679643SDaniel Borkmann while (isalpha(*++fmt)) { 155510679643SDaniel Borkmann switch (*fmt) { 155610679643SDaniel Borkmann case 'p': 155710679643SDaniel Borkmann have_p = true; 155810679643SDaniel Borkmann break; 155910679643SDaniel Borkmann case 'h': 156010679643SDaniel Borkmann case 'l': 156110679643SDaniel Borkmann case 'n': 156210679643SDaniel Borkmann case 'b': 156310679643SDaniel Borkmann fmt4[2] = *fmt; 156410679643SDaniel Borkmann break; 156510679643SDaniel Borkmann } 156610679643SDaniel Borkmann } 156710679643SDaniel Borkmann 156810679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 156910679643SDaniel Borkmann if (have_p) { 157010679643SDaniel Borkmann *p++ = ':'; 157110679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 157210679643SDaniel Borkmann } 157310679643SDaniel Borkmann *p = '\0'; 157410679643SDaniel Borkmann 1575d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 157610679643SDaniel Borkmann } 157710679643SDaniel Borkmann 157810679643SDaniel Borkmann static noinline_for_stack 1579f00cc102SPetr Mladek char *ip_addr_string(char *buf, char *end, const void *ptr, 1580f00cc102SPetr Mladek struct printf_spec spec, const char *fmt) 1581f00cc102SPetr Mladek { 15820b74d4d7SPetr Mladek char *err_fmt_msg; 15830b74d4d7SPetr Mladek 15843e5903ebSPetr Mladek if (check_pointer(&buf, end, ptr, spec)) 15853e5903ebSPetr Mladek return buf; 15863e5903ebSPetr Mladek 1587f00cc102SPetr Mladek switch (fmt[1]) { 1588f00cc102SPetr Mladek case '6': 1589f00cc102SPetr Mladek return ip6_addr_string(buf, end, ptr, spec, fmt); 1590f00cc102SPetr Mladek case '4': 1591f00cc102SPetr Mladek return ip4_addr_string(buf, end, ptr, spec, fmt); 1592f00cc102SPetr Mladek case 'S': { 1593f00cc102SPetr Mladek const union { 1594f00cc102SPetr Mladek struct sockaddr raw; 1595f00cc102SPetr Mladek struct sockaddr_in v4; 1596f00cc102SPetr Mladek struct sockaddr_in6 v6; 1597f00cc102SPetr Mladek } *sa = ptr; 1598f00cc102SPetr Mladek 1599f00cc102SPetr Mladek switch (sa->raw.sa_family) { 1600f00cc102SPetr Mladek case AF_INET: 1601f00cc102SPetr Mladek return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 1602f00cc102SPetr Mladek case AF_INET6: 1603f00cc102SPetr Mladek return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 1604f00cc102SPetr Mladek default: 1605c8c3b584SPetr Mladek return error_string(buf, end, "(einval)", spec); 1606f00cc102SPetr Mladek }} 1607f00cc102SPetr Mladek } 1608f00cc102SPetr Mladek 16090b74d4d7SPetr Mladek err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)"; 1610c8c3b584SPetr Mladek return error_string(buf, end, err_fmt_msg, spec); 1611f00cc102SPetr Mladek } 1612f00cc102SPetr Mladek 1613f00cc102SPetr Mladek static noinline_for_stack 161471dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 161571dca95dSAndy Shevchenko const char *fmt) 161671dca95dSAndy Shevchenko { 161771dca95dSAndy Shevchenko bool found = true; 161871dca95dSAndy Shevchenko int count = 1; 161971dca95dSAndy Shevchenko unsigned int flags = 0; 162071dca95dSAndy Shevchenko int len; 162171dca95dSAndy Shevchenko 162271dca95dSAndy Shevchenko if (spec.field_width == 0) 162371dca95dSAndy Shevchenko return buf; /* nothing to print */ 162471dca95dSAndy Shevchenko 16253e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 16263e5903ebSPetr Mladek return buf; 162771dca95dSAndy Shevchenko 162871dca95dSAndy Shevchenko do { 162971dca95dSAndy Shevchenko switch (fmt[count++]) { 163071dca95dSAndy Shevchenko case 'a': 163171dca95dSAndy Shevchenko flags |= ESCAPE_ANY; 163271dca95dSAndy Shevchenko break; 163371dca95dSAndy Shevchenko case 'c': 163471dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL; 163571dca95dSAndy Shevchenko break; 163671dca95dSAndy Shevchenko case 'h': 163771dca95dSAndy Shevchenko flags |= ESCAPE_HEX; 163871dca95dSAndy Shevchenko break; 163971dca95dSAndy Shevchenko case 'n': 164071dca95dSAndy Shevchenko flags |= ESCAPE_NULL; 164171dca95dSAndy Shevchenko break; 164271dca95dSAndy Shevchenko case 'o': 164371dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL; 164471dca95dSAndy Shevchenko break; 164571dca95dSAndy Shevchenko case 'p': 164671dca95dSAndy Shevchenko flags |= ESCAPE_NP; 164771dca95dSAndy Shevchenko break; 164871dca95dSAndy Shevchenko case 's': 164971dca95dSAndy Shevchenko flags |= ESCAPE_SPACE; 165071dca95dSAndy Shevchenko break; 165171dca95dSAndy Shevchenko default: 165271dca95dSAndy Shevchenko found = false; 165371dca95dSAndy Shevchenko break; 165471dca95dSAndy Shevchenko } 165571dca95dSAndy Shevchenko } while (found); 165671dca95dSAndy Shevchenko 165771dca95dSAndy Shevchenko if (!flags) 165871dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP; 165971dca95dSAndy Shevchenko 166071dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width; 166171dca95dSAndy Shevchenko 166241416f23SRasmus Villemoes /* 166341416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to 166441416f23SRasmus Villemoes * the given buffer, and returns the total size of the output 166541416f23SRasmus Villemoes * had the buffer been big enough. 166641416f23SRasmus Villemoes */ 166741416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 166871dca95dSAndy Shevchenko 166971dca95dSAndy Shevchenko return buf; 167071dca95dSAndy Shevchenko } 167171dca95dSAndy Shevchenko 16723e5903ebSPetr Mladek static char *va_format(char *buf, char *end, struct va_format *va_fmt, 16733e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 167445c3e93dSPetr Mladek { 167545c3e93dSPetr Mladek va_list va; 167645c3e93dSPetr Mladek 16773e5903ebSPetr Mladek if (check_pointer(&buf, end, va_fmt, spec)) 16783e5903ebSPetr Mladek return buf; 16793e5903ebSPetr Mladek 168045c3e93dSPetr Mladek va_copy(va, *va_fmt->va); 168145c3e93dSPetr Mladek buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va); 168245c3e93dSPetr Mladek va_end(va); 168345c3e93dSPetr Mladek 168445c3e93dSPetr Mladek return buf; 168545c3e93dSPetr Mladek } 168645c3e93dSPetr Mladek 168771dca95dSAndy Shevchenko static noinline_for_stack 1688cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 16899ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 16909ac6e44eSJoe Perches { 16912b1b0d66SAndy Shevchenko char uuid[UUID_STRING_LEN + 1]; 16929ac6e44eSJoe Perches char *p = uuid; 16939ac6e44eSJoe Perches int i; 1694f9727a17SChristoph Hellwig const u8 *index = uuid_index; 16959ac6e44eSJoe Perches bool uc = false; 16969ac6e44eSJoe Perches 16973e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 16983e5903ebSPetr Mladek return buf; 16993e5903ebSPetr Mladek 17009ac6e44eSJoe Perches switch (*(++fmt)) { 17019ac6e44eSJoe Perches case 'L': 1702df561f66SGustavo A. R. Silva uc = true; 17034c1ca831SNick Desaulniers fallthrough; 17049ac6e44eSJoe Perches case 'l': 1705f9727a17SChristoph Hellwig index = guid_index; 17069ac6e44eSJoe Perches break; 17079ac6e44eSJoe Perches case 'B': 17089ac6e44eSJoe Perches uc = true; 17099ac6e44eSJoe Perches break; 17109ac6e44eSJoe Perches } 17119ac6e44eSJoe Perches 17129ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 1713aa4ea1c3SAndy Shevchenko if (uc) 1714aa4ea1c3SAndy Shevchenko p = hex_byte_pack_upper(p, addr[index[i]]); 1715aa4ea1c3SAndy Shevchenko else 171655036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 17179ac6e44eSJoe Perches switch (i) { 17189ac6e44eSJoe Perches case 3: 17199ac6e44eSJoe Perches case 5: 17209ac6e44eSJoe Perches case 7: 17219ac6e44eSJoe Perches case 9: 17229ac6e44eSJoe Perches *p++ = '-'; 17239ac6e44eSJoe Perches break; 17249ac6e44eSJoe Perches } 17259ac6e44eSJoe Perches } 17269ac6e44eSJoe Perches 17279ac6e44eSJoe Perches *p = 0; 17289ac6e44eSJoe Perches 1729d529ac41SPetr Mladek return string_nocheck(buf, end, uuid, spec); 17309ac6e44eSJoe Perches } 17319ac6e44eSJoe Perches 17325b17aecfSAndy Shevchenko static noinline_for_stack 1733431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr, 1734431bca24SGeert Uytterhoeven struct printf_spec spec, const char *fmt) 1735c8f44affSMichał Mirosław { 17365b17aecfSAndy Shevchenko unsigned long long num; 17375b17aecfSAndy Shevchenko int size; 17385b17aecfSAndy Shevchenko 17393e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 17403e5903ebSPetr Mladek return buf; 17413e5903ebSPetr Mladek 17425b17aecfSAndy Shevchenko switch (fmt[1]) { 17435b17aecfSAndy Shevchenko case 'F': 17445b17aecfSAndy Shevchenko num = *(const netdev_features_t *)addr; 17455b17aecfSAndy Shevchenko size = sizeof(netdev_features_t); 17465b17aecfSAndy Shevchenko break; 17475b17aecfSAndy Shevchenko default: 1748c8c3b584SPetr Mladek return error_string(buf, end, "(%pN?)", spec); 17495b17aecfSAndy Shevchenko } 1750c8f44affSMichał Mirosław 17513cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1752c8f44affSMichał Mirosław } 1753c8f44affSMichał Mirosław 1754aaf07621SJoe Perches static noinline_for_stack 1755af612e43SSakari Ailus char *fourcc_string(char *buf, char *end, const u32 *fourcc, 1756af612e43SSakari Ailus struct printf_spec spec, const char *fmt) 1757af612e43SSakari Ailus { 1758af612e43SSakari Ailus char output[sizeof("0123 little-endian (0x01234567)")]; 1759af612e43SSakari Ailus char *p = output; 1760af612e43SSakari Ailus unsigned int i; 1761d75b26f8SAndy Shevchenko u32 orig, val; 1762af612e43SSakari Ailus 1763af612e43SSakari Ailus if (fmt[1] != 'c' || fmt[2] != 'c') 1764af612e43SSakari Ailus return error_string(buf, end, "(%p4?)", spec); 1765af612e43SSakari Ailus 1766af612e43SSakari Ailus if (check_pointer(&buf, end, fourcc, spec)) 1767af612e43SSakari Ailus return buf; 1768af612e43SSakari Ailus 1769d75b26f8SAndy Shevchenko orig = get_unaligned(fourcc); 1770d75b26f8SAndy Shevchenko val = orig & ~BIT(31); 1771af612e43SSakari Ailus 1772d75b26f8SAndy Shevchenko for (i = 0; i < sizeof(u32); i++) { 1773af612e43SSakari Ailus unsigned char c = val >> (i * 8); 1774af612e43SSakari Ailus 1775af612e43SSakari Ailus /* Print non-control ASCII characters as-is, dot otherwise */ 1776af612e43SSakari Ailus *p++ = isascii(c) && isprint(c) ? c : '.'; 1777af612e43SSakari Ailus } 1778af612e43SSakari Ailus 1779f74a08fcSAndy Shevchenko *p++ = ' '; 1780d75b26f8SAndy Shevchenko strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian"); 1781af612e43SSakari Ailus p += strlen(p); 1782af612e43SSakari Ailus 1783af612e43SSakari Ailus *p++ = ' '; 1784af612e43SSakari Ailus *p++ = '('; 1785d75b26f8SAndy Shevchenko p = special_hex_number(p, output + sizeof(output) - 2, orig, sizeof(u32)); 1786af612e43SSakari Ailus *p++ = ')'; 1787af612e43SSakari Ailus *p = '\0'; 1788af612e43SSakari Ailus 1789af612e43SSakari Ailus return string(buf, end, output, spec); 1790af612e43SSakari Ailus } 1791af612e43SSakari Ailus 1792af612e43SSakari Ailus static noinline_for_stack 17933e5903ebSPetr Mladek char *address_val(char *buf, char *end, const void *addr, 17943e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 1795aaf07621SJoe Perches { 1796aaf07621SJoe Perches unsigned long long num; 17973cab1e71SAndy Shevchenko int size; 1798aaf07621SJoe Perches 17993e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 18003e5903ebSPetr Mladek return buf; 18013e5903ebSPetr Mladek 1802aaf07621SJoe Perches switch (fmt[1]) { 1803aaf07621SJoe Perches case 'd': 1804aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 18053cab1e71SAndy Shevchenko size = sizeof(dma_addr_t); 1806aaf07621SJoe Perches break; 1807aaf07621SJoe Perches case 'p': 1808aaf07621SJoe Perches default: 1809aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 18103cab1e71SAndy Shevchenko size = sizeof(phys_addr_t); 1811aaf07621SJoe Perches break; 1812aaf07621SJoe Perches } 1813aaf07621SJoe Perches 18143cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1815aaf07621SJoe Perches } 1816aaf07621SJoe Perches 1817900cca29SGeert Uytterhoeven static noinline_for_stack 18184d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) 18194d42c447SAndy Shevchenko { 18204d42c447SAndy Shevchenko int year = tm->tm_year + (r ? 0 : 1900); 18214d42c447SAndy Shevchenko int mon = tm->tm_mon + (r ? 0 : 1); 18224d42c447SAndy Shevchenko 18234d42c447SAndy Shevchenko buf = number(buf, end, year, default_dec04_spec); 18244d42c447SAndy Shevchenko if (buf < end) 18254d42c447SAndy Shevchenko *buf = '-'; 18264d42c447SAndy Shevchenko buf++; 18274d42c447SAndy Shevchenko 18284d42c447SAndy Shevchenko buf = number(buf, end, mon, default_dec02_spec); 18294d42c447SAndy Shevchenko if (buf < end) 18304d42c447SAndy Shevchenko *buf = '-'; 18314d42c447SAndy Shevchenko buf++; 18324d42c447SAndy Shevchenko 18334d42c447SAndy Shevchenko return number(buf, end, tm->tm_mday, default_dec02_spec); 18344d42c447SAndy Shevchenko } 18354d42c447SAndy Shevchenko 18364d42c447SAndy Shevchenko static noinline_for_stack 18374d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) 18384d42c447SAndy Shevchenko { 18394d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_hour, default_dec02_spec); 18404d42c447SAndy Shevchenko if (buf < end) 18414d42c447SAndy Shevchenko *buf = ':'; 18424d42c447SAndy Shevchenko buf++; 18434d42c447SAndy Shevchenko 18444d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_min, default_dec02_spec); 18454d42c447SAndy Shevchenko if (buf < end) 18464d42c447SAndy Shevchenko *buf = ':'; 18474d42c447SAndy Shevchenko buf++; 18484d42c447SAndy Shevchenko 18494d42c447SAndy Shevchenko return number(buf, end, tm->tm_sec, default_dec02_spec); 18504d42c447SAndy Shevchenko } 18514d42c447SAndy Shevchenko 18524d42c447SAndy Shevchenko static noinline_for_stack 18533e5903ebSPetr Mladek char *rtc_str(char *buf, char *end, const struct rtc_time *tm, 18543e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 18554d42c447SAndy Shevchenko { 18564d42c447SAndy Shevchenko bool have_t = true, have_d = true; 185720bc8c1eSAndy Shevchenko bool raw = false, iso8601_separator = true; 185820bc8c1eSAndy Shevchenko bool found = true; 18594d42c447SAndy Shevchenko int count = 2; 18604d42c447SAndy Shevchenko 18613e5903ebSPetr Mladek if (check_pointer(&buf, end, tm, spec)) 18623e5903ebSPetr Mladek return buf; 18633e5903ebSPetr Mladek 18644d42c447SAndy Shevchenko switch (fmt[count]) { 18654d42c447SAndy Shevchenko case 'd': 18664d42c447SAndy Shevchenko have_t = false; 18674d42c447SAndy Shevchenko count++; 18684d42c447SAndy Shevchenko break; 18694d42c447SAndy Shevchenko case 't': 18704d42c447SAndy Shevchenko have_d = false; 18714d42c447SAndy Shevchenko count++; 18724d42c447SAndy Shevchenko break; 18734d42c447SAndy Shevchenko } 18744d42c447SAndy Shevchenko 187520bc8c1eSAndy Shevchenko do { 187620bc8c1eSAndy Shevchenko switch (fmt[count++]) { 187720bc8c1eSAndy Shevchenko case 'r': 187820bc8c1eSAndy Shevchenko raw = true; 187920bc8c1eSAndy Shevchenko break; 188020bc8c1eSAndy Shevchenko case 's': 188120bc8c1eSAndy Shevchenko iso8601_separator = false; 188220bc8c1eSAndy Shevchenko break; 188320bc8c1eSAndy Shevchenko default: 188420bc8c1eSAndy Shevchenko found = false; 188520bc8c1eSAndy Shevchenko break; 188620bc8c1eSAndy Shevchenko } 188720bc8c1eSAndy Shevchenko } while (found); 18884d42c447SAndy Shevchenko 18894d42c447SAndy Shevchenko if (have_d) 18904d42c447SAndy Shevchenko buf = date_str(buf, end, tm, raw); 18914d42c447SAndy Shevchenko if (have_d && have_t) { 18924d42c447SAndy Shevchenko if (buf < end) 189320bc8c1eSAndy Shevchenko *buf = iso8601_separator ? 'T' : ' '; 18944d42c447SAndy Shevchenko buf++; 18954d42c447SAndy Shevchenko } 18964d42c447SAndy Shevchenko if (have_t) 18974d42c447SAndy Shevchenko buf = time_str(buf, end, tm, raw); 18984d42c447SAndy Shevchenko 18994d42c447SAndy Shevchenko return buf; 19004d42c447SAndy Shevchenko } 19014d42c447SAndy Shevchenko 19024d42c447SAndy Shevchenko static noinline_for_stack 19037daac5b2SAndy Shevchenko char *time64_str(char *buf, char *end, const time64_t time, 19047daac5b2SAndy Shevchenko struct printf_spec spec, const char *fmt) 19057daac5b2SAndy Shevchenko { 19067daac5b2SAndy Shevchenko struct rtc_time rtc_time; 19077daac5b2SAndy Shevchenko struct tm tm; 19087daac5b2SAndy Shevchenko 19097daac5b2SAndy Shevchenko time64_to_tm(time, 0, &tm); 19107daac5b2SAndy Shevchenko 19117daac5b2SAndy Shevchenko rtc_time.tm_sec = tm.tm_sec; 19127daac5b2SAndy Shevchenko rtc_time.tm_min = tm.tm_min; 19137daac5b2SAndy Shevchenko rtc_time.tm_hour = tm.tm_hour; 19147daac5b2SAndy Shevchenko rtc_time.tm_mday = tm.tm_mday; 19157daac5b2SAndy Shevchenko rtc_time.tm_mon = tm.tm_mon; 19167daac5b2SAndy Shevchenko rtc_time.tm_year = tm.tm_year; 19177daac5b2SAndy Shevchenko rtc_time.tm_wday = tm.tm_wday; 19187daac5b2SAndy Shevchenko rtc_time.tm_yday = tm.tm_yday; 19197daac5b2SAndy Shevchenko 19207daac5b2SAndy Shevchenko rtc_time.tm_isdst = 0; 19217daac5b2SAndy Shevchenko 19227daac5b2SAndy Shevchenko return rtc_str(buf, end, &rtc_time, spec, fmt); 19237daac5b2SAndy Shevchenko } 19247daac5b2SAndy Shevchenko 19257daac5b2SAndy Shevchenko static noinline_for_stack 19264d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, 19274d42c447SAndy Shevchenko const char *fmt) 19284d42c447SAndy Shevchenko { 19294d42c447SAndy Shevchenko switch (fmt[1]) { 19304d42c447SAndy Shevchenko case 'R': 19313e5903ebSPetr Mladek return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); 19327daac5b2SAndy Shevchenko case 'T': 19337daac5b2SAndy Shevchenko return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); 19344d42c447SAndy Shevchenko default: 19357daac5b2SAndy Shevchenko return error_string(buf, end, "(%pt?)", spec); 19364d42c447SAndy Shevchenko } 19374d42c447SAndy Shevchenko } 19384d42c447SAndy Shevchenko 19394d42c447SAndy Shevchenko static noinline_for_stack 1940900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1941900cca29SGeert Uytterhoeven const char *fmt) 1942900cca29SGeert Uytterhoeven { 19430b74d4d7SPetr Mladek if (!IS_ENABLED(CONFIG_HAVE_CLK)) 1944c8c3b584SPetr Mladek return error_string(buf, end, "(%pC?)", spec); 19450b74d4d7SPetr Mladek 19463e5903ebSPetr Mladek if (check_pointer(&buf, end, clk, spec)) 19473e5903ebSPetr Mladek return buf; 1948900cca29SGeert Uytterhoeven 1949900cca29SGeert Uytterhoeven switch (fmt[1]) { 1950900cca29SGeert Uytterhoeven case 'n': 1951900cca29SGeert Uytterhoeven default: 1952900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1953900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1954900cca29SGeert Uytterhoeven #else 19554ca96aa9SGeert Uytterhoeven return ptr_to_id(buf, end, clk, spec); 1956900cca29SGeert Uytterhoeven #endif 1957900cca29SGeert Uytterhoeven } 1958900cca29SGeert Uytterhoeven } 1959900cca29SGeert Uytterhoeven 1960edf14cdbSVlastimil Babka static 1961edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags, 1962edf14cdbSVlastimil Babka const struct trace_print_flags *names) 1963edf14cdbSVlastimil Babka { 1964edf14cdbSVlastimil Babka unsigned long mask; 1965edf14cdbSVlastimil Babka 1966edf14cdbSVlastimil Babka for ( ; flags && names->name; names++) { 1967edf14cdbSVlastimil Babka mask = names->mask; 1968edf14cdbSVlastimil Babka if ((flags & mask) != mask) 1969edf14cdbSVlastimil Babka continue; 1970edf14cdbSVlastimil Babka 1971abd4fe62SAndy Shevchenko buf = string(buf, end, names->name, default_str_spec); 1972edf14cdbSVlastimil Babka 1973edf14cdbSVlastimil Babka flags &= ~mask; 1974edf14cdbSVlastimil Babka if (flags) { 1975edf14cdbSVlastimil Babka if (buf < end) 1976edf14cdbSVlastimil Babka *buf = '|'; 1977edf14cdbSVlastimil Babka buf++; 1978edf14cdbSVlastimil Babka } 1979edf14cdbSVlastimil Babka } 1980edf14cdbSVlastimil Babka 1981edf14cdbSVlastimil Babka if (flags) 198254433973SAndy Shevchenko buf = number(buf, end, flags, default_flag_spec); 1983edf14cdbSVlastimil Babka 1984edf14cdbSVlastimil Babka return buf; 1985edf14cdbSVlastimil Babka } 1986edf14cdbSVlastimil Babka 1987c244297aSYafang Shao struct page_flags_fields { 1988c244297aSYafang Shao int width; 1989c244297aSYafang Shao int shift; 1990c244297aSYafang Shao int mask; 1991c244297aSYafang Shao const struct printf_spec *spec; 1992c244297aSYafang Shao const char *name; 1993c244297aSYafang Shao }; 1994c244297aSYafang Shao 1995c244297aSYafang Shao static const struct page_flags_fields pff[] = { 1996c244297aSYafang Shao {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK, 1997c244297aSYafang Shao &default_dec_spec, "section"}, 1998c244297aSYafang Shao {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK, 1999c244297aSYafang Shao &default_dec_spec, "node"}, 2000c244297aSYafang Shao {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK, 2001c244297aSYafang Shao &default_dec_spec, "zone"}, 2002c244297aSYafang Shao {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK, 2003c244297aSYafang Shao &default_flag_spec, "lastcpupid"}, 2004c244297aSYafang Shao {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK, 2005c244297aSYafang Shao &default_flag_spec, "kasantag"}, 2006c244297aSYafang Shao }; 2007c244297aSYafang Shao 2008c244297aSYafang Shao static 2009c244297aSYafang Shao char *format_page_flags(char *buf, char *end, unsigned long flags) 2010c244297aSYafang Shao { 201141c961b9SMuchun Song unsigned long main_flags = flags & PAGEFLAGS_MASK; 2012c244297aSYafang Shao bool append = false; 2013c244297aSYafang Shao int i; 2014c244297aSYafang Shao 201523efd080SMatthew Wilcox (Oracle) buf = number(buf, end, flags, default_flag_spec); 201623efd080SMatthew Wilcox (Oracle) if (buf < end) 201723efd080SMatthew Wilcox (Oracle) *buf = '('; 201823efd080SMatthew Wilcox (Oracle) buf++; 201923efd080SMatthew Wilcox (Oracle) 2020c244297aSYafang Shao /* Page flags from the main area. */ 2021c244297aSYafang Shao if (main_flags) { 2022c244297aSYafang Shao buf = format_flags(buf, end, main_flags, pageflag_names); 2023c244297aSYafang Shao append = true; 2024c244297aSYafang Shao } 2025c244297aSYafang Shao 2026c244297aSYafang Shao /* Page flags from the fields area */ 2027c244297aSYafang Shao for (i = 0; i < ARRAY_SIZE(pff); i++) { 2028c244297aSYafang Shao /* Skip undefined fields. */ 2029c244297aSYafang Shao if (!pff[i].width) 2030c244297aSYafang Shao continue; 2031c244297aSYafang Shao 2032c244297aSYafang Shao /* Format: Flag Name + '=' (equals sign) + Number + '|' (separator) */ 2033c244297aSYafang Shao if (append) { 2034c244297aSYafang Shao if (buf < end) 2035c244297aSYafang Shao *buf = '|'; 2036c244297aSYafang Shao buf++; 2037c244297aSYafang Shao } 2038c244297aSYafang Shao 2039c244297aSYafang Shao buf = string(buf, end, pff[i].name, default_str_spec); 2040c244297aSYafang Shao if (buf < end) 2041c244297aSYafang Shao *buf = '='; 2042c244297aSYafang Shao buf++; 2043c244297aSYafang Shao buf = number(buf, end, (flags >> pff[i].shift) & pff[i].mask, 2044c244297aSYafang Shao *pff[i].spec); 2045c244297aSYafang Shao 2046c244297aSYafang Shao append = true; 2047c244297aSYafang Shao } 204823efd080SMatthew Wilcox (Oracle) if (buf < end) 204923efd080SMatthew Wilcox (Oracle) *buf = ')'; 205023efd080SMatthew Wilcox (Oracle) buf++; 2051c244297aSYafang Shao 2052c244297aSYafang Shao return buf; 2053c244297aSYafang Shao } 2054c244297aSYafang Shao 2055edf14cdbSVlastimil Babka static noinline_for_stack 20560b74d4d7SPetr Mladek char *flags_string(char *buf, char *end, void *flags_ptr, 20570b74d4d7SPetr Mladek struct printf_spec spec, const char *fmt) 2058edf14cdbSVlastimil Babka { 2059edf14cdbSVlastimil Babka unsigned long flags; 2060edf14cdbSVlastimil Babka const struct trace_print_flags *names; 2061edf14cdbSVlastimil Babka 20623e5903ebSPetr Mladek if (check_pointer(&buf, end, flags_ptr, spec)) 20633e5903ebSPetr Mladek return buf; 20643e5903ebSPetr Mladek 2065edf14cdbSVlastimil Babka switch (fmt[1]) { 2066edf14cdbSVlastimil Babka case 'p': 2067c244297aSYafang Shao return format_page_flags(buf, end, *(unsigned long *)flags_ptr); 2068edf14cdbSVlastimil Babka case 'v': 2069edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 2070edf14cdbSVlastimil Babka names = vmaflag_names; 2071edf14cdbSVlastimil Babka break; 2072edf14cdbSVlastimil Babka case 'g': 207330d497a0SAndy Shevchenko flags = (__force unsigned long)(*(gfp_t *)flags_ptr); 2074edf14cdbSVlastimil Babka names = gfpflag_names; 2075edf14cdbSVlastimil Babka break; 2076edf14cdbSVlastimil Babka default: 2077c8c3b584SPetr Mladek return error_string(buf, end, "(%pG?)", spec); 2078edf14cdbSVlastimil Babka } 2079edf14cdbSVlastimil Babka 2080edf14cdbSVlastimil Babka return format_flags(buf, end, flags, names); 2081edf14cdbSVlastimil Babka } 2082edf14cdbSVlastimil Babka 2083ce4fecf1SPantelis Antoniou static noinline_for_stack 2084a92eb762SSakari Ailus char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, 2085a92eb762SSakari Ailus char *end) 2086ce4fecf1SPantelis Antoniou { 2087ce4fecf1SPantelis Antoniou int depth; 2088ce4fecf1SPantelis Antoniou 2089a92eb762SSakari Ailus /* Loop starting from the root node to the current node. */ 2090a92eb762SSakari Ailus for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { 2091a92eb762SSakari Ailus struct fwnode_handle *__fwnode = 2092a92eb762SSakari Ailus fwnode_get_nth_parent(fwnode, depth); 2093ce4fecf1SPantelis Antoniou 2094a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name_prefix(__fwnode), 2095abd4fe62SAndy Shevchenko default_str_spec); 2096a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name(__fwnode), 2097a92eb762SSakari Ailus default_str_spec); 2098a92eb762SSakari Ailus 2099a92eb762SSakari Ailus fwnode_handle_put(__fwnode); 2100ce4fecf1SPantelis Antoniou } 2101a92eb762SSakari Ailus 2102ce4fecf1SPantelis Antoniou return buf; 2103ce4fecf1SPantelis Antoniou } 2104ce4fecf1SPantelis Antoniou 2105ce4fecf1SPantelis Antoniou static noinline_for_stack 2106ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn, 2107ce4fecf1SPantelis Antoniou struct printf_spec spec, const char *fmt) 2108ce4fecf1SPantelis Antoniou { 2109ce4fecf1SPantelis Antoniou char tbuf[sizeof("xxxx") + 1]; 2110ce4fecf1SPantelis Antoniou const char *p; 2111ce4fecf1SPantelis Antoniou int ret; 2112ce4fecf1SPantelis Antoniou char *buf_start = buf; 2113ce4fecf1SPantelis Antoniou struct property *prop; 2114ce4fecf1SPantelis Antoniou bool has_mult, pass; 2115ce4fecf1SPantelis Antoniou 2116ce4fecf1SPantelis Antoniou struct printf_spec str_spec = spec; 2117ce4fecf1SPantelis Antoniou str_spec.field_width = -1; 2118ce4fecf1SPantelis Antoniou 211983abc5a7SSakari Ailus if (fmt[0] != 'F') 212083abc5a7SSakari Ailus return error_string(buf, end, "(%pO?)", spec); 212183abc5a7SSakari Ailus 2122ce4fecf1SPantelis Antoniou if (!IS_ENABLED(CONFIG_OF)) 2123c8c3b584SPetr Mladek return error_string(buf, end, "(%pOF?)", spec); 2124ce4fecf1SPantelis Antoniou 21253e5903ebSPetr Mladek if (check_pointer(&buf, end, dn, spec)) 21263e5903ebSPetr Mladek return buf; 2127ce4fecf1SPantelis Antoniou 2128ce4fecf1SPantelis Antoniou /* simple case without anything any more format specifiers */ 2129ce4fecf1SPantelis Antoniou fmt++; 2130ce4fecf1SPantelis Antoniou if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 2131ce4fecf1SPantelis Antoniou fmt = "f"; 2132ce4fecf1SPantelis Antoniou 2133ce4fecf1SPantelis Antoniou for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 21346d0a70a2SRob Herring int precision; 2135ce4fecf1SPantelis Antoniou if (pass) { 2136ce4fecf1SPantelis Antoniou if (buf < end) 2137ce4fecf1SPantelis Antoniou *buf = ':'; 2138ce4fecf1SPantelis Antoniou buf++; 2139ce4fecf1SPantelis Antoniou } 2140ce4fecf1SPantelis Antoniou 2141ce4fecf1SPantelis Antoniou switch (*fmt) { 2142ce4fecf1SPantelis Antoniou case 'f': /* full_name */ 2143a92eb762SSakari Ailus buf = fwnode_full_name_string(of_fwnode_handle(dn), buf, 2144a92eb762SSakari Ailus end); 2145ce4fecf1SPantelis Antoniou break; 2146ce4fecf1SPantelis Antoniou case 'n': /* name */ 2147a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn)); 21486d0a70a2SRob Herring precision = str_spec.precision; 21496d0a70a2SRob Herring str_spec.precision = strchrnul(p, '@') - p; 21506d0a70a2SRob Herring buf = string(buf, end, p, str_spec); 21516d0a70a2SRob Herring str_spec.precision = precision; 2152ce4fecf1SPantelis Antoniou break; 2153ce4fecf1SPantelis Antoniou case 'p': /* phandle */ 215409ceb8d7SAndy Shevchenko buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec); 2155ce4fecf1SPantelis Antoniou break; 2156ce4fecf1SPantelis Antoniou case 'P': /* path-spec */ 2157a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn)); 2158ce4fecf1SPantelis Antoniou if (!p[1]) 2159ce4fecf1SPantelis Antoniou p = "/"; 2160ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2161ce4fecf1SPantelis Antoniou break; 2162ce4fecf1SPantelis Antoniou case 'F': /* flags */ 2163ce4fecf1SPantelis Antoniou tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 2164ce4fecf1SPantelis Antoniou tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 2165ce4fecf1SPantelis Antoniou tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 2166ce4fecf1SPantelis Antoniou tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 2167ce4fecf1SPantelis Antoniou tbuf[4] = 0; 2168d529ac41SPetr Mladek buf = string_nocheck(buf, end, tbuf, str_spec); 2169ce4fecf1SPantelis Antoniou break; 2170ce4fecf1SPantelis Antoniou case 'c': /* major compatible string */ 2171ce4fecf1SPantelis Antoniou ret = of_property_read_string(dn, "compatible", &p); 2172ce4fecf1SPantelis Antoniou if (!ret) 2173ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2174ce4fecf1SPantelis Antoniou break; 2175ce4fecf1SPantelis Antoniou case 'C': /* full compatible string */ 2176ce4fecf1SPantelis Antoniou has_mult = false; 2177ce4fecf1SPantelis Antoniou of_property_for_each_string(dn, "compatible", prop, p) { 2178ce4fecf1SPantelis Antoniou if (has_mult) 2179d529ac41SPetr Mladek buf = string_nocheck(buf, end, ",", str_spec); 2180d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 2181ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2182d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 2183ce4fecf1SPantelis Antoniou 2184ce4fecf1SPantelis Antoniou has_mult = true; 2185ce4fecf1SPantelis Antoniou } 2186ce4fecf1SPantelis Antoniou break; 2187ce4fecf1SPantelis Antoniou default: 2188ce4fecf1SPantelis Antoniou break; 2189ce4fecf1SPantelis Antoniou } 2190ce4fecf1SPantelis Antoniou } 2191ce4fecf1SPantelis Antoniou 2192ce4fecf1SPantelis Antoniou return widen_string(buf, buf - buf_start, end, spec); 2193ce4fecf1SPantelis Antoniou } 2194ce4fecf1SPantelis Antoniou 21953bd32d6aSSakari Ailus static noinline_for_stack 21963bd32d6aSSakari Ailus char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, 2197798cc27aSPetr Mladek struct printf_spec spec, const char *fmt) 2198798cc27aSPetr Mladek { 21993bd32d6aSSakari Ailus struct printf_spec str_spec = spec; 22003bd32d6aSSakari Ailus char *buf_start = buf; 22013bd32d6aSSakari Ailus 22023bd32d6aSSakari Ailus str_spec.field_width = -1; 22033bd32d6aSSakari Ailus 22043bd32d6aSSakari Ailus if (*fmt != 'w') 22053bd32d6aSSakari Ailus return error_string(buf, end, "(%pf?)", spec); 22063bd32d6aSSakari Ailus 22073bd32d6aSSakari Ailus if (check_pointer(&buf, end, fwnode, spec)) 22083bd32d6aSSakari Ailus return buf; 22093bd32d6aSSakari Ailus 22103bd32d6aSSakari Ailus fmt++; 22113bd32d6aSSakari Ailus 22123bd32d6aSSakari Ailus switch (*fmt) { 22133bd32d6aSSakari Ailus case 'P': /* name */ 22143bd32d6aSSakari Ailus buf = string(buf, end, fwnode_get_name(fwnode), str_spec); 22153bd32d6aSSakari Ailus break; 22163bd32d6aSSakari Ailus case 'f': /* full_name */ 22173bd32d6aSSakari Ailus default: 22183bd32d6aSSakari Ailus buf = fwnode_full_name_string(fwnode, buf, end); 22193bd32d6aSSakari Ailus break; 2220798cc27aSPetr Mladek } 2221798cc27aSPetr Mladek 22223bd32d6aSSakari Ailus return widen_string(buf, buf - buf_start, end, spec); 2223798cc27aSPetr Mladek } 2224798cc27aSPetr Mladek 222579270291SStephen Boyd int __init no_hash_pointers_enable(char *str) 22265ead723aSTimur Tabi { 22279f961c2eSMarco Elver if (no_hash_pointers) 22289f961c2eSMarco Elver return 0; 22299f961c2eSMarco Elver 22305ead723aSTimur Tabi no_hash_pointers = true; 22315ead723aSTimur Tabi 22325ead723aSTimur Tabi pr_warn("**********************************************************\n"); 22335ead723aSTimur Tabi pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 22345ead723aSTimur Tabi pr_warn("** **\n"); 22355ead723aSTimur Tabi pr_warn("** This system shows unhashed kernel memory addresses **\n"); 22365ead723aSTimur Tabi pr_warn("** via the console, logs, and other interfaces. This **\n"); 22375ead723aSTimur Tabi pr_warn("** might reduce the security of your system. **\n"); 22385ead723aSTimur Tabi pr_warn("** **\n"); 22395ead723aSTimur Tabi pr_warn("** If you see this message and you are not debugging **\n"); 22405ead723aSTimur Tabi pr_warn("** the kernel, report this immediately to your system **\n"); 22415ead723aSTimur Tabi pr_warn("** administrator! **\n"); 22425ead723aSTimur Tabi pr_warn("** **\n"); 22435ead723aSTimur Tabi pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 22445ead723aSTimur Tabi pr_warn("**********************************************************\n"); 22455ead723aSTimur Tabi 22465ead723aSTimur Tabi return 0; 22475ead723aSTimur Tabi } 22485ead723aSTimur Tabi early_param("no_hash_pointers", no_hash_pointers_enable); 22495ead723aSTimur Tabi 2250787983daSGary Guo /* Used for Rust formatting ('%pA'). */ 2251787983daSGary Guo char *rust_fmt_argument(char *buf, char *end, void *ptr); 2252787983daSGary Guo 22534d8a743cSLinus Torvalds /* 22544d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 22554d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 22564d8a743cSLinus Torvalds * specifiers. 22574d8a743cSLinus Torvalds * 22580b523769SJoe Perches * Please update scripts/checkpatch.pl when adding/removing conversion 22590b523769SJoe Perches * characters. (Search for "check for vsprintf extension"). 22600b523769SJoe Perches * 2261332d2e78SLinus Torvalds * Right now we handle: 22620fe1ef24SLinus Torvalds * 2263cdb7e52dSSergey Senozhatsky * - 'S' For symbolic direct pointers (or function descriptors) with offset 2264cdb7e52dSSergey Senozhatsky * - 's' For symbolic direct pointers (or function descriptors) without offset 22659af77064SSakari Ailus * - '[Ss]R' as above with __builtin_extract_return_addr() translation 22669294523eSStephen Boyd * - 'S[R]b' as above with module build ID (for use in backtraces) 22671586c5aeSSakari Ailus * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of 22681586c5aeSSakari Ailus * %ps and %pS. Be careful when re-using these specifiers. 22690f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 22709294523eSStephen Boyd * - 'Bb' as above with module build ID (for use in backtraces) 2271c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 2272c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 2273dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 2274dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 2275dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 2276dbc760bcSTejun Heo * range-list format instead of hex format 2277dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 2278dd45c9cfSHarvey Harrison * usual colon-separated hex notation 22798a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 2280bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 2281c8e00060SJoe Perches * with a dash-separated hex notation 22827c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 22838a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 22848a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 22858a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 228610679643SDaniel Borkmann * [S][pfs] 228710679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 228810679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 22898a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 22908a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 22918a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 229210679643SDaniel Borkmann * [S][pfs] 229310679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 229410679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 229510679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 229610679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 22978eda94bdSAlexander A. Klimov * https://tools.ietf.org/html/rfc5952 229871dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 229971dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 230071dca95dSAndy Shevchenko * details): 230171dca95dSAndy Shevchenko * a - ESCAPE_ANY 230271dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 230371dca95dSAndy Shevchenko * h - ESCAPE_HEX 230471dca95dSAndy Shevchenko * n - ESCAPE_NULL 230571dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 230671dca95dSAndy Shevchenko * p - ESCAPE_NP 230771dca95dSAndy Shevchenko * s - ESCAPE_SPACE 230871dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 23099ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 23109ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 23119ac6e44eSJoe Perches * Options for %pU are: 23129ac6e44eSJoe Perches * b big endian lower case hex (default) 23139ac6e44eSJoe Perches * B big endian UPPER case hex 23149ac6e44eSJoe Perches * l little endian lower case hex 23159ac6e44eSJoe Perches * L little endian UPPER case hex 23169ac6e44eSJoe Perches * big endian output byte order is: 23179ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 23189ac6e44eSJoe Perches * little endian output byte order is: 23199ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 23207db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 23217db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 23227db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 23237db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 23247db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 2325a48849e2SVlastimil Babka * - 'K' For a kernel pointer that should be hidden from unprivileged users. 2326a48849e2SVlastimil Babka * Use only for procfs, sysfs and similar files, not printk(); please 2327a48849e2SVlastimil Babka * read the documentation (path below) first. 2328c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 2329af612e43SSakari Ailus * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value. 233031550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 233131550a16SAndy Shevchenko * a certain separator (' ' by default): 233231550a16SAndy Shevchenko * C colon 233331550a16SAndy Shevchenko * D dash 233431550a16SAndy Shevchenko * N no separator 233531550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 233631550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 2337aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 2338aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 2339c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 2340c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 23411031bc58SDmitry Monakhov * - 'g' For block_device name (gendisk + partition number) 234220bc8c1eSAndy Shevchenko * - 't[RT][dt][r][s]' For time and date as represented by: 23434d42c447SAndy Shevchenko * R struct rtc_time 23447daac5b2SAndy Shevchenko * T time64_t 2345900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 2346900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2347900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 2348900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2349edf14cdbSVlastimil Babka * - 'G' For flags to be printed as a collection of symbolic strings that would 2350edf14cdbSVlastimil Babka * construct the specific value. Supported flags given by option: 2351edf14cdbSVlastimil Babka * p page flags (see struct page) given as pointer to unsigned long 2352edf14cdbSVlastimil Babka * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 2353edf14cdbSVlastimil Babka * v vma flags (VM_*) given as pointer to unsigned long 2354ce4fecf1SPantelis Antoniou * - 'OF[fnpPcCF]' For a device tree object 2355ce4fecf1SPantelis Antoniou * Without any optional arguments prints the full_name 2356ce4fecf1SPantelis Antoniou * f device node full_name 2357ce4fecf1SPantelis Antoniou * n device node name 2358ce4fecf1SPantelis Antoniou * p device node phandle 2359ce4fecf1SPantelis Antoniou * P device node path spec (name + @unit) 2360ce4fecf1SPantelis Antoniou * F device node flags 2361ce4fecf1SPantelis Antoniou * c major compatible string 2362ce4fecf1SPantelis Antoniou * C full compatible string 23633bd32d6aSSakari Ailus * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer 23643bd32d6aSSakari Ailus * Without an option prints the full name of the node 23653bd32d6aSSakari Ailus * f full name 23663bd32d6aSSakari Ailus * P node name, including a possible unit address 2367a48849e2SVlastimil Babka * - 'x' For printing the address unmodified. Equivalent to "%lx". 2368a48849e2SVlastimil Babka * Please read the documentation (path below) before using! 2369b2a5212fSDaniel Borkmann * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of 2370b2a5212fSDaniel Borkmann * bpf_trace_printk() where [ku] prefix specifies either kernel (k) 2371b2a5212fSDaniel Borkmann * or user (u) memory to probe, and: 2372b2a5212fSDaniel Borkmann * s a string, equivalent to "%s" on direct vsnprintf() use 23737b1924a1STobin C. Harding * 2374b3ed2321STobin C. Harding * ** When making changes please also update: 2375b3ed2321STobin C. Harding * Documentation/core-api/printk-formats.rst 23769ac6e44eSJoe Perches * 2377ad67b74dSTobin C. Harding * Note: The default behaviour (unadorned %p) is to hash the address, 2378ad67b74dSTobin C. Harding * rendering it useful as a unique identifier. 2379787983daSGary Guo * 2380787983daSGary Guo * There is also a '%pA' format specifier, but it is only intended to be used 2381787983daSGary Guo * from Rust code to format core::fmt::Arguments. Do *not* use it from C. 2382787983daSGary Guo * See rust/kernel/print.rs for details. 23834d8a743cSLinus Torvalds */ 2384cf3b429bSJoe Perches static noinline_for_stack 2385cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 2386fef20d9cSFrederic Weisbecker struct printf_spec spec) 238778a8bf69SLinus Torvalds { 23880fe1ef24SLinus Torvalds switch (*fmt) { 23890fe1ef24SLinus Torvalds case 'S': 23909ac6e44eSJoe Perches case 's': 239104b8eb7aSSergey Senozhatsky ptr = dereference_symbol_descriptor(ptr); 23924c1ca831SNick Desaulniers fallthrough; 23930f77a8d3SNamhyung Kim case 'B': 2394b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 2395332d2e78SLinus Torvalds case 'R': 2396c7dabef8SBjorn Helgaas case 'r': 2397fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 239831550a16SAndy Shevchenko case 'h': 239931550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 2400dbc760bcSTejun Heo case 'b': 2401dbc760bcSTejun Heo switch (fmt[1]) { 2402dbc760bcSTejun Heo case 'l': 2403dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 2404dbc760bcSTejun Heo default: 2405dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 2406dbc760bcSTejun Heo } 24078a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 24088a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 240976597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 241076597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 24118a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 24128a27f7c9SJoe Perches case 'I': /* Formatted IP supported 24138a27f7c9SJoe Perches * 4: 1.2.3.4 24148a27f7c9SJoe Perches * 6: 0001:0203:...:0708 24158a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 24168a27f7c9SJoe Perches */ 24178a27f7c9SJoe Perches case 'i': /* Contiguous: 24188a27f7c9SJoe Perches * 4: 001.002.003.004 24198a27f7c9SJoe Perches * 6: 000102...0f 24208a27f7c9SJoe Perches */ 2421f00cc102SPetr Mladek return ip_addr_string(buf, end, ptr, spec, fmt); 242271dca95dSAndy Shevchenko case 'E': 242371dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 24249ac6e44eSJoe Perches case 'U': 24259ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 24267db6f5fbSJoe Perches case 'V': 24273e5903ebSPetr Mladek return va_format(buf, end, ptr, spec, fmt); 2428455cd5abSDan Rosenberg case 'K': 242957e73442STobin C. Harding return restricted_pointer(buf, end, ptr, spec); 2430c8f44affSMichał Mirosław case 'N': 2431431bca24SGeert Uytterhoeven return netdev_bits(buf, end, ptr, spec, fmt); 2432af612e43SSakari Ailus case '4': 2433af612e43SSakari Ailus return fourcc_string(buf, end, ptr, spec, fmt); 24347d799210SStepan Moskovchenko case 'a': 24353e5903ebSPetr Mladek return address_val(buf, end, ptr, spec, fmt); 24364b6ccca7SAl Viro case 'd': 24374b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 24384d42c447SAndy Shevchenko case 't': 24394d42c447SAndy Shevchenko return time_and_date(buf, end, ptr, spec, fmt); 2440900cca29SGeert Uytterhoeven case 'C': 2441900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 24424b6ccca7SAl Viro case 'D': 244336594b31SJia He return file_dentry_name(buf, end, ptr, spec, fmt); 24441031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 24451031bc58SDmitry Monakhov case 'g': 24461031bc58SDmitry Monakhov return bdev_name(buf, end, ptr, spec, fmt); 24471031bc58SDmitry Monakhov #endif 24481031bc58SDmitry Monakhov 2449edf14cdbSVlastimil Babka case 'G': 24500b74d4d7SPetr Mladek return flags_string(buf, end, ptr, spec, fmt); 2451ce4fecf1SPantelis Antoniou case 'O': 245283abc5a7SSakari Ailus return device_node_string(buf, end, ptr, spec, fmt + 1); 24533bd32d6aSSakari Ailus case 'f': 24543bd32d6aSSakari Ailus return fwnode_string(buf, end, ptr, spec, fmt + 1); 2455787983daSGary Guo case 'A': 2456787983daSGary Guo if (!IS_ENABLED(CONFIG_RUST)) { 2457787983daSGary Guo WARN_ONCE(1, "Please remove %%pA from non-Rust code\n"); 2458787983daSGary Guo return error_string(buf, end, "(%pA?)", spec); 2459787983daSGary Guo } 2460787983daSGary Guo return rust_fmt_argument(buf, end, ptr); 24617b1924a1STobin C. Harding case 'x': 24627b1924a1STobin C. Harding return pointer_string(buf, end, ptr, spec); 246357f5677eSRasmus Villemoes case 'e': 246457f5677eSRasmus Villemoes /* %pe with a non-ERR_PTR gets treated as plain %p */ 246557f5677eSRasmus Villemoes if (!IS_ERR(ptr)) 246684842911SChristophe Leroy return default_pointer(buf, end, ptr, spec); 246757f5677eSRasmus Villemoes return err_ptr(buf, end, ptr, spec); 2468b2a5212fSDaniel Borkmann case 'u': 2469b2a5212fSDaniel Borkmann case 'k': 2470b2a5212fSDaniel Borkmann switch (fmt[1]) { 2471b2a5212fSDaniel Borkmann case 's': 2472b2a5212fSDaniel Borkmann return string(buf, end, ptr, spec); 2473b2a5212fSDaniel Borkmann default: 2474b2a5212fSDaniel Borkmann return error_string(buf, end, "(einval)", spec); 2475b2a5212fSDaniel Borkmann } 247684842911SChristophe Leroy default: 247784842911SChristophe Leroy return default_pointer(buf, end, ptr, spec); 24780fe1ef24SLinus Torvalds } 2479fef20d9cSFrederic Weisbecker } 2480fef20d9cSFrederic Weisbecker 2481fef20d9cSFrederic Weisbecker /* 2482fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 2483fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 2484fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 2485fef20d9cSFrederic Weisbecker * to go on the next call). 2486fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 2487fef20d9cSFrederic Weisbecker * 2488fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 2489fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 2490fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 24915b5e0928SAlexey Dobriyan * 'Z' changed to 'z' --adobriyan 2017-01-25 2492fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 2493fef20d9cSFrederic Weisbecker * 2494fef20d9cSFrederic Weisbecker * @fmt: the format string 2495fef20d9cSFrederic Weisbecker * @type of the token returned 2496fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 2497fef20d9cSFrederic Weisbecker * @field_width: overwritten width 2498fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 2499fef20d9cSFrederic Weisbecker * @precision: precision of a number 2500fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 2501fef20d9cSFrederic Weisbecker */ 2502cf3b429bSJoe Perches static noinline_for_stack 2503cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 2504fef20d9cSFrederic Weisbecker { 2505fef20d9cSFrederic Weisbecker const char *start = fmt; 2506d0484193SRasmus Villemoes char qualifier; 2507fef20d9cSFrederic Weisbecker 2508fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 2509ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 2510fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 2511fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 2512fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 2513fef20d9cSFrederic Weisbecker } 2514fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2515fef20d9cSFrederic Weisbecker goto precision; 2516fef20d9cSFrederic Weisbecker } 2517fef20d9cSFrederic Weisbecker 2518fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 2519fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 2520fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2521fef20d9cSFrederic Weisbecker spec->precision = 0; 2522fef20d9cSFrederic Weisbecker 2523fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2524fef20d9cSFrederic Weisbecker goto qualifier; 2525fef20d9cSFrederic Weisbecker } 2526fef20d9cSFrederic Weisbecker 2527fef20d9cSFrederic Weisbecker /* By default */ 2528fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2529fef20d9cSFrederic Weisbecker 2530fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 2531fef20d9cSFrederic Weisbecker if (*fmt == '%') 2532fef20d9cSFrederic Weisbecker break; 2533fef20d9cSFrederic Weisbecker } 2534fef20d9cSFrederic Weisbecker 2535fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 2536fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 2537fef20d9cSFrederic Weisbecker return fmt - start; 2538fef20d9cSFrederic Weisbecker 2539fef20d9cSFrederic Weisbecker /* Process flags */ 2540fef20d9cSFrederic Weisbecker spec->flags = 0; 2541fef20d9cSFrederic Weisbecker 2542fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 2543fef20d9cSFrederic Weisbecker bool found = true; 2544fef20d9cSFrederic Weisbecker 2545fef20d9cSFrederic Weisbecker ++fmt; 2546fef20d9cSFrederic Weisbecker 2547fef20d9cSFrederic Weisbecker switch (*fmt) { 2548fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 2549fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 2550fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 2551fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 2552fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 2553fef20d9cSFrederic Weisbecker default: found = false; 2554fef20d9cSFrederic Weisbecker } 2555fef20d9cSFrederic Weisbecker 2556fef20d9cSFrederic Weisbecker if (!found) 2557fef20d9cSFrederic Weisbecker break; 2558fef20d9cSFrederic Weisbecker } 2559fef20d9cSFrederic Weisbecker 2560fef20d9cSFrederic Weisbecker /* get field width */ 2561fef20d9cSFrederic Weisbecker spec->field_width = -1; 2562fef20d9cSFrederic Weisbecker 2563fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 2564fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 2565fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 2566fef20d9cSFrederic Weisbecker /* it's the next argument */ 2567ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 2568fef20d9cSFrederic Weisbecker return ++fmt - start; 2569fef20d9cSFrederic Weisbecker } 2570fef20d9cSFrederic Weisbecker 2571fef20d9cSFrederic Weisbecker precision: 2572fef20d9cSFrederic Weisbecker /* get the precision */ 2573fef20d9cSFrederic Weisbecker spec->precision = -1; 2574fef20d9cSFrederic Weisbecker if (*fmt == '.') { 2575fef20d9cSFrederic Weisbecker ++fmt; 2576fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 2577fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 2578fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2579fef20d9cSFrederic Weisbecker spec->precision = 0; 2580fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 2581fef20d9cSFrederic Weisbecker /* it's the next argument */ 2582adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 2583fef20d9cSFrederic Weisbecker return ++fmt - start; 2584fef20d9cSFrederic Weisbecker } 2585fef20d9cSFrederic Weisbecker } 2586fef20d9cSFrederic Weisbecker 2587fef20d9cSFrederic Weisbecker qualifier: 2588fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 2589d0484193SRasmus Villemoes qualifier = 0; 259075fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 25915b5e0928SAlexey Dobriyan *fmt == 'z' || *fmt == 't') { 2592d0484193SRasmus Villemoes qualifier = *fmt++; 2593d0484193SRasmus Villemoes if (unlikely(qualifier == *fmt)) { 2594d0484193SRasmus Villemoes if (qualifier == 'l') { 2595d0484193SRasmus Villemoes qualifier = 'L'; 2596fef20d9cSFrederic Weisbecker ++fmt; 2597d0484193SRasmus Villemoes } else if (qualifier == 'h') { 2598d0484193SRasmus Villemoes qualifier = 'H'; 2599a4e94ef0SZhaolei ++fmt; 2600a4e94ef0SZhaolei } 2601fef20d9cSFrederic Weisbecker } 2602fef20d9cSFrederic Weisbecker } 2603fef20d9cSFrederic Weisbecker 2604fef20d9cSFrederic Weisbecker /* default base */ 2605fef20d9cSFrederic Weisbecker spec->base = 10; 2606fef20d9cSFrederic Weisbecker switch (*fmt) { 2607fef20d9cSFrederic Weisbecker case 'c': 2608fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 2609fef20d9cSFrederic Weisbecker return ++fmt - start; 2610fef20d9cSFrederic Weisbecker 2611fef20d9cSFrederic Weisbecker case 's': 2612fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 2613fef20d9cSFrederic Weisbecker return ++fmt - start; 2614fef20d9cSFrederic Weisbecker 2615fef20d9cSFrederic Weisbecker case 'p': 2616fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 2617ffbfed03SRasmus Villemoes return ++fmt - start; 2618fef20d9cSFrederic Weisbecker 2619fef20d9cSFrederic Weisbecker case '%': 2620fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 2621fef20d9cSFrederic Weisbecker return ++fmt - start; 2622fef20d9cSFrederic Weisbecker 2623fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 2624fef20d9cSFrederic Weisbecker case 'o': 2625fef20d9cSFrederic Weisbecker spec->base = 8; 2626fef20d9cSFrederic Weisbecker break; 2627fef20d9cSFrederic Weisbecker 2628fef20d9cSFrederic Weisbecker case 'x': 2629fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 26304c1ca831SNick Desaulniers fallthrough; 2631fef20d9cSFrederic Weisbecker 2632fef20d9cSFrederic Weisbecker case 'X': 2633fef20d9cSFrederic Weisbecker spec->base = 16; 2634fef20d9cSFrederic Weisbecker break; 2635fef20d9cSFrederic Weisbecker 2636fef20d9cSFrederic Weisbecker case 'd': 2637fef20d9cSFrederic Weisbecker case 'i': 263839e874f8SFrederic Weisbecker spec->flags |= SIGN; 263936f9ff9eSGustavo A. R. Silva break; 2640fef20d9cSFrederic Weisbecker case 'u': 2641fef20d9cSFrederic Weisbecker break; 2642fef20d9cSFrederic Weisbecker 2643708d96fdSRyan Mallon case 'n': 2644708d96fdSRyan Mallon /* 2645b006f19bSRasmus Villemoes * Since %n poses a greater security risk than 2646b006f19bSRasmus Villemoes * utility, treat it as any other invalid or 2647b006f19bSRasmus Villemoes * unsupported format specifier. 2648708d96fdSRyan Mallon */ 26494c1ca831SNick Desaulniers fallthrough; 2650708d96fdSRyan Mallon 2651fef20d9cSFrederic Weisbecker default: 2652b006f19bSRasmus Villemoes WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 2653fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 2654fef20d9cSFrederic Weisbecker return fmt - start; 2655fef20d9cSFrederic Weisbecker } 2656fef20d9cSFrederic Weisbecker 2657d0484193SRasmus Villemoes if (qualifier == 'L') 2658fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 2659d0484193SRasmus Villemoes else if (qualifier == 'l') { 266051be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 266151be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 26625b5e0928SAlexey Dobriyan } else if (qualifier == 'z') { 2663fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 2664d0484193SRasmus Villemoes } else if (qualifier == 't') { 2665fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 2666d0484193SRasmus Villemoes } else if (qualifier == 'H') { 266751be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 266851be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 2669d0484193SRasmus Villemoes } else if (qualifier == 'h') { 267051be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 267151be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 2672fef20d9cSFrederic Weisbecker } else { 267351be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 267451be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 2675fef20d9cSFrederic Weisbecker } 2676fef20d9cSFrederic Weisbecker 2677fef20d9cSFrederic Weisbecker return ++fmt - start; 267878a8bf69SLinus Torvalds } 267978a8bf69SLinus Torvalds 26804d72ba01SRasmus Villemoes static void 26814d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width) 26824d72ba01SRasmus Villemoes { 26834d72ba01SRasmus Villemoes spec->field_width = width; 26844d72ba01SRasmus Villemoes if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 26854d72ba01SRasmus Villemoes spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 26864d72ba01SRasmus Villemoes } 26874d72ba01SRasmus Villemoes } 26884d72ba01SRasmus Villemoes 26894d72ba01SRasmus Villemoes static void 26904d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec) 26914d72ba01SRasmus Villemoes { 26924d72ba01SRasmus Villemoes spec->precision = prec; 26934d72ba01SRasmus Villemoes if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 26944d72ba01SRasmus Villemoes spec->precision = clamp(prec, 0, PRECISION_MAX); 26954d72ba01SRasmus Villemoes } 26964d72ba01SRasmus Villemoes } 26974d72ba01SRasmus Villemoes 26981da177e4SLinus Torvalds /** 26991da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 27001da177e4SLinus Torvalds * @buf: The buffer to place the result into 27011da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 27021da177e4SLinus Torvalds * @fmt: The format string to use 27031da177e4SLinus Torvalds * @args: Arguments for the format string 27041da177e4SLinus Torvalds * 2705d7ec9a05SRasmus Villemoes * This function generally follows C99 vsnprintf, but has some 2706d7ec9a05SRasmus Villemoes * extensions and a few limitations: 2707d7ec9a05SRasmus Villemoes * 27086cc89134Smchehab@s-opensource.com * - ``%n`` is unsupported 27096cc89134Smchehab@s-opensource.com * - ``%p*`` is handled by pointer() 271020036fdcSAndi Kleen * 271127e7c0e8SJonathan Corbet * See pointer() or Documentation/core-api/printk-formats.rst for more 27125e4ee7b1SMartin Kletzander * extensive description. 27135e4ee7b1SMartin Kletzander * 27145e4ee7b1SMartin Kletzander * **Please update the documentation in both places when making changes** 271580f548e0SAndrew Morton * 27161da177e4SLinus Torvalds * The return value is the number of characters which would 27171da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 27181da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 27191da177e4SLinus Torvalds * number of characters written into @buf as return value 272072fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 27211da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 27221da177e4SLinus Torvalds * string is truncated. 27231da177e4SLinus Torvalds * 2724ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 27251da177e4SLinus Torvalds */ 27261da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 27271da177e4SLinus Torvalds { 27281da177e4SLinus Torvalds unsigned long long num; 2729d4be151bSAndré Goddard Rosa char *str, *end; 2730fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 27311da177e4SLinus Torvalds 2732f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 2733f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 27342aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 27351da177e4SLinus Torvalds return 0; 27361da177e4SLinus Torvalds 27371da177e4SLinus Torvalds str = buf; 2738f796937aSJeremy Fitzhardinge end = buf + size; 27391da177e4SLinus Torvalds 2740f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 2741f796937aSJeremy Fitzhardinge if (end < buf) { 27421da177e4SLinus Torvalds end = ((void *)-1); 2743f796937aSJeremy Fitzhardinge size = end - buf; 27441da177e4SLinus Torvalds } 27451da177e4SLinus Torvalds 2746fef20d9cSFrederic Weisbecker while (*fmt) { 2747fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2748d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2749fef20d9cSFrederic Weisbecker 2750fef20d9cSFrederic Weisbecker fmt += read; 2751fef20d9cSFrederic Weisbecker 2752fef20d9cSFrederic Weisbecker switch (spec.type) { 2753fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2754fef20d9cSFrederic Weisbecker int copy = read; 2755fef20d9cSFrederic Weisbecker if (str < end) { 2756fef20d9cSFrederic Weisbecker if (copy > end - str) 2757fef20d9cSFrederic Weisbecker copy = end - str; 2758fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2759fef20d9cSFrederic Weisbecker } 2760fef20d9cSFrederic Weisbecker str += read; 2761fef20d9cSFrederic Weisbecker break; 27621da177e4SLinus Torvalds } 27631da177e4SLinus Torvalds 2764ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 27654d72ba01SRasmus Villemoes set_field_width(&spec, va_arg(args, int)); 2766fef20d9cSFrederic Weisbecker break; 27671da177e4SLinus Torvalds 2768fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 27694d72ba01SRasmus Villemoes set_precision(&spec, va_arg(args, int)); 2770fef20d9cSFrederic Weisbecker break; 27711da177e4SLinus Torvalds 2772d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2773d4be151bSAndré Goddard Rosa char c; 2774d4be151bSAndré Goddard Rosa 2775fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2776fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2777f796937aSJeremy Fitzhardinge if (str < end) 27781da177e4SLinus Torvalds *str = ' '; 27791da177e4SLinus Torvalds ++str; 2780fef20d9cSFrederic Weisbecker 27811da177e4SLinus Torvalds } 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 2784f796937aSJeremy Fitzhardinge if (str < end) 27851da177e4SLinus Torvalds *str = c; 27861da177e4SLinus Torvalds ++str; 2787fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2788f796937aSJeremy Fitzhardinge if (str < end) 27891da177e4SLinus Torvalds *str = ' '; 27901da177e4SLinus Torvalds ++str; 27911da177e4SLinus Torvalds } 2792fef20d9cSFrederic Weisbecker break; 2793d4be151bSAndré Goddard Rosa } 27941da177e4SLinus Torvalds 2795fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 2796fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 2797fef20d9cSFrederic Weisbecker break; 27981da177e4SLinus Torvalds 2799fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2800ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 2801fef20d9cSFrederic Weisbecker spec); 2802fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 28034d8a743cSLinus Torvalds fmt++; 2804fef20d9cSFrederic Weisbecker break; 28051da177e4SLinus Torvalds 2806fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2807f796937aSJeremy Fitzhardinge if (str < end) 28081da177e4SLinus Torvalds *str = '%'; 28091da177e4SLinus Torvalds ++str; 28101da177e4SLinus Torvalds break; 28111da177e4SLinus Torvalds 2812fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 2813b006f19bSRasmus Villemoes /* 2814b006f19bSRasmus Villemoes * Presumably the arguments passed gcc's type 2815b006f19bSRasmus Villemoes * checking, but there is no safe or sane way 2816b006f19bSRasmus Villemoes * for us to continue parsing the format and 2817b006f19bSRasmus Villemoes * fetching from the va_list; the remaining 2818b006f19bSRasmus Villemoes * specifiers and arguments would be out of 2819b006f19bSRasmus Villemoes * sync. 2820b006f19bSRasmus Villemoes */ 2821b006f19bSRasmus Villemoes goto out; 2822fef20d9cSFrederic Weisbecker 2823fef20d9cSFrederic Weisbecker default: 2824fef20d9cSFrederic Weisbecker switch (spec.type) { 2825fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2826fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 2827fef20d9cSFrederic Weisbecker break; 2828fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2829fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 2830fef20d9cSFrederic Weisbecker break; 2831fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2832fef20d9cSFrederic Weisbecker num = va_arg(args, long); 2833fef20d9cSFrederic Weisbecker break; 2834fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2835ef124960SJason Gunthorpe if (spec.flags & SIGN) 2836ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 2837ef124960SJason Gunthorpe else 2838fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 2839fef20d9cSFrederic Weisbecker break; 2840fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2841fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 2842fef20d9cSFrederic Weisbecker break; 2843a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2844a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 2845a4e94ef0SZhaolei break; 2846a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2847a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 2848a4e94ef0SZhaolei break; 2849fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2850fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 2851fef20d9cSFrederic Weisbecker break; 2852fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2853fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 2854fef20d9cSFrederic Weisbecker break; 285539e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 285639e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 2857fef20d9cSFrederic Weisbecker break; 2858fef20d9cSFrederic Weisbecker default: 2859fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 28601da177e4SLinus Torvalds } 2861fef20d9cSFrederic Weisbecker 2862fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 28631da177e4SLinus Torvalds } 2864fef20d9cSFrederic Weisbecker } 2865fef20d9cSFrederic Weisbecker 2866b006f19bSRasmus Villemoes out: 2867f796937aSJeremy Fitzhardinge if (size > 0) { 2868f796937aSJeremy Fitzhardinge if (str < end) 28691da177e4SLinus Torvalds *str = '\0'; 2870f796937aSJeremy Fitzhardinge else 28710a6047eeSLinus Torvalds end[-1] = '\0'; 2872f796937aSJeremy Fitzhardinge } 2873fef20d9cSFrederic Weisbecker 2874f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 28751da177e4SLinus Torvalds return str-buf; 2876fef20d9cSFrederic Weisbecker 28771da177e4SLinus Torvalds } 28781da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds /** 28811da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 28821da177e4SLinus Torvalds * @buf: The buffer to place the result into 28831da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 28841da177e4SLinus Torvalds * @fmt: The format string to use 28851da177e4SLinus Torvalds * @args: Arguments for the format string 28861da177e4SLinus Torvalds * 28871da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2888b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 28891da177e4SLinus Torvalds * returns 0. 28901da177e4SLinus Torvalds * 2891ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 289220036fdcSAndi Kleen * 289320036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 28941da177e4SLinus Torvalds */ 28951da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 28961da177e4SLinus Torvalds { 28971da177e4SLinus Torvalds int i; 28981da177e4SLinus Torvalds 2899ef62c8ffSWaiman Long if (unlikely(!size)) 2900ef62c8ffSWaiman Long return 0; 2901ef62c8ffSWaiman Long 29021da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 29037b9186f5SAndré Goddard Rosa 2904b921c69fSAnton Arapov if (likely(i < size)) 2905b921c69fSAnton Arapov return i; 2906ef62c8ffSWaiman Long 2907b921c69fSAnton Arapov return size - 1; 29081da177e4SLinus Torvalds } 29091da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 29101da177e4SLinus Torvalds 29111da177e4SLinus Torvalds /** 29121da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 29131da177e4SLinus Torvalds * @buf: The buffer to place the result into 29141da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 29151da177e4SLinus Torvalds * @fmt: The format string to use 29161da177e4SLinus Torvalds * @...: Arguments for the format string 29171da177e4SLinus Torvalds * 29181da177e4SLinus Torvalds * The return value is the number of characters which would be 29191da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 29201da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 29211da177e4SLinus Torvalds * @size, the resulting string is truncated. 292220036fdcSAndi Kleen * 292320036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 29241da177e4SLinus Torvalds */ 29251da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 29261da177e4SLinus Torvalds { 29271da177e4SLinus Torvalds va_list args; 29281da177e4SLinus Torvalds int i; 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds va_start(args, fmt); 29311da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 29321da177e4SLinus Torvalds va_end(args); 29337b9186f5SAndré Goddard Rosa 29341da177e4SLinus Torvalds return i; 29351da177e4SLinus Torvalds } 29361da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 29371da177e4SLinus Torvalds 29381da177e4SLinus Torvalds /** 29391da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 29401da177e4SLinus Torvalds * @buf: The buffer to place the result into 29411da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 29421da177e4SLinus Torvalds * @fmt: The format string to use 29431da177e4SLinus Torvalds * @...: Arguments for the format string 29441da177e4SLinus Torvalds * 29451da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2946b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 29471da177e4SLinus Torvalds */ 29481da177e4SLinus Torvalds 29491da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 29501da177e4SLinus Torvalds { 29511da177e4SLinus Torvalds va_list args; 29521da177e4SLinus Torvalds int i; 29531da177e4SLinus Torvalds 29541da177e4SLinus Torvalds va_start(args, fmt); 2955b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 29561da177e4SLinus Torvalds va_end(args); 29577b9186f5SAndré Goddard Rosa 2958b903c0b8SChangli Gao return i; 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 29611da177e4SLinus Torvalds 29621da177e4SLinus Torvalds /** 29631da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 29641da177e4SLinus Torvalds * @buf: The buffer to place the result into 29651da177e4SLinus Torvalds * @fmt: The format string to use 29661da177e4SLinus Torvalds * @args: Arguments for the format string 29671da177e4SLinus Torvalds * 29681da177e4SLinus Torvalds * The function returns the number of characters written 296972fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 29701da177e4SLinus Torvalds * buffer overflows. 29711da177e4SLinus Torvalds * 2972ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 297320036fdcSAndi Kleen * 297420036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 29751da177e4SLinus Torvalds */ 29761da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 29771da177e4SLinus Torvalds { 29781da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 29791da177e4SLinus Torvalds } 29801da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 29811da177e4SLinus Torvalds 29821da177e4SLinus Torvalds /** 29831da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 29841da177e4SLinus Torvalds * @buf: The buffer to place the result into 29851da177e4SLinus Torvalds * @fmt: The format string to use 29861da177e4SLinus Torvalds * @...: Arguments for the format string 29871da177e4SLinus Torvalds * 29881da177e4SLinus Torvalds * The function returns the number of characters written 298972fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 29901da177e4SLinus Torvalds * buffer overflows. 299120036fdcSAndi Kleen * 299220036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 29931da177e4SLinus Torvalds */ 29941da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 29951da177e4SLinus Torvalds { 29961da177e4SLinus Torvalds va_list args; 29971da177e4SLinus Torvalds int i; 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds va_start(args, fmt); 30001da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 30011da177e4SLinus Torvalds va_end(args); 30027b9186f5SAndré Goddard Rosa 30031da177e4SLinus Torvalds return i; 30041da177e4SLinus Torvalds } 30051da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 30061da177e4SLinus Torvalds 30074370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 30084370aa4aSLai Jiangshan /* 30094370aa4aSLai Jiangshan * bprintf service: 30104370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 30114370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 30124370aa4aSLai Jiangshan */ 30134370aa4aSLai Jiangshan 30144370aa4aSLai Jiangshan /** 30154370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 30164370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 30174370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 30184370aa4aSLai Jiangshan * @fmt: The format string to use 30194370aa4aSLai Jiangshan * @args: Arguments for the format string 30204370aa4aSLai Jiangshan * 30214370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 3022da3dae54SMasanari Iida * is skipped. 30234370aa4aSLai Jiangshan * 30244370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 30254370aa4aSLai Jiangshan * the given input. 30264370aa4aSLai Jiangshan * 30274370aa4aSLai Jiangshan * NOTE: 30284370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 30294370aa4aSLai Jiangshan * valid for bstr_printf(). 30304370aa4aSLai Jiangshan */ 30314370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 30324370aa4aSLai Jiangshan { 3033fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 30344370aa4aSLai Jiangshan char *str, *end; 3035841a915dSSteven Rostedt (VMware) int width; 30364370aa4aSLai Jiangshan 30374370aa4aSLai Jiangshan str = (char *)bin_buf; 30384370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 30394370aa4aSLai Jiangshan 30404370aa4aSLai Jiangshan #define save_arg(type) \ 3041841a915dSSteven Rostedt (VMware) ({ \ 30424370aa4aSLai Jiangshan unsigned long long value; \ 3043841a915dSSteven Rostedt (VMware) if (sizeof(type) == 8) { \ 3044841a915dSSteven Rostedt (VMware) unsigned long long val8; \ 30454370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 3046841a915dSSteven Rostedt (VMware) val8 = va_arg(args, unsigned long long); \ 30474370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 3048841a915dSSteven Rostedt (VMware) *(u32 *)str = *(u32 *)&val8; \ 3049841a915dSSteven Rostedt (VMware) *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 30504370aa4aSLai Jiangshan } \ 3051841a915dSSteven Rostedt (VMware) value = val8; \ 30524370aa4aSLai Jiangshan } else { \ 3053841a915dSSteven Rostedt (VMware) unsigned int val4; \ 30544370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 3055841a915dSSteven Rostedt (VMware) val4 = va_arg(args, int); \ 30564370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 3057841a915dSSteven Rostedt (VMware) *(typeof(type) *)str = (type)(long)val4; \ 3058841a915dSSteven Rostedt (VMware) value = (unsigned long long)val4; \ 30594370aa4aSLai Jiangshan } \ 30604370aa4aSLai Jiangshan str += sizeof(type); \ 3061841a915dSSteven Rostedt (VMware) value; \ 3062841a915dSSteven Rostedt (VMware) }) 30634370aa4aSLai Jiangshan 3064fef20d9cSFrederic Weisbecker while (*fmt) { 3065d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 30664370aa4aSLai Jiangshan 3067fef20d9cSFrederic Weisbecker fmt += read; 3068fef20d9cSFrederic Weisbecker 3069fef20d9cSFrederic Weisbecker switch (spec.type) { 3070fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 3071d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 3072fef20d9cSFrederic Weisbecker break; 3073b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 3074b006f19bSRasmus Villemoes goto out; 3075fef20d9cSFrederic Weisbecker 3076ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 3077fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 3078841a915dSSteven Rostedt (VMware) width = (int)save_arg(int); 3079841a915dSSteven Rostedt (VMware) /* Pointers may require the width */ 3080841a915dSSteven Rostedt (VMware) if (*fmt == 'p') 3081841a915dSSteven Rostedt (VMware) set_field_width(&spec, width); 3082fef20d9cSFrederic Weisbecker break; 30834370aa4aSLai Jiangshan 3084fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 30854370aa4aSLai Jiangshan save_arg(char); 3086fef20d9cSFrederic Weisbecker break; 3087fef20d9cSFrederic Weisbecker 3088fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 30894370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 30903e5903ebSPetr Mladek const char *err_msg; 30914370aa4aSLai Jiangshan size_t len; 30926c356634SAndré Goddard Rosa 30933e5903ebSPetr Mladek err_msg = check_pointer_msg(save_str); 30943e5903ebSPetr Mladek if (err_msg) 30953e5903ebSPetr Mladek save_str = err_msg; 30963e5903ebSPetr Mladek 30976c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 30986c356634SAndré Goddard Rosa if (str + len < end) 30996c356634SAndré Goddard Rosa memcpy(str, save_str, len); 31006c356634SAndré Goddard Rosa str += len; 3101fef20d9cSFrederic Weisbecker break; 31024370aa4aSLai Jiangshan } 3103fef20d9cSFrederic Weisbecker 3104fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 3105841a915dSSteven Rostedt (VMware) /* Dereferenced pointers must be done now */ 3106841a915dSSteven Rostedt (VMware) switch (*fmt) { 3107841a915dSSteven Rostedt (VMware) /* Dereference of functions is still OK */ 3108841a915dSSteven Rostedt (VMware) case 'S': 3109841a915dSSteven Rostedt (VMware) case 's': 31101e6338cfSSteven Rostedt (VMware) case 'x': 31111e6338cfSSteven Rostedt (VMware) case 'K': 311257f5677eSRasmus Villemoes case 'e': 31134370aa4aSLai Jiangshan save_arg(void *); 3114841a915dSSteven Rostedt (VMware) break; 3115841a915dSSteven Rostedt (VMware) default: 3116841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 3117841a915dSSteven Rostedt (VMware) save_arg(void *); 3118841a915dSSteven Rostedt (VMware) break; 3119841a915dSSteven Rostedt (VMware) } 3120841a915dSSteven Rostedt (VMware) str = pointer(fmt, str, end, va_arg(args, void *), 3121841a915dSSteven Rostedt (VMware) spec); 3122841a915dSSteven Rostedt (VMware) if (str + 1 < end) 3123841a915dSSteven Rostedt (VMware) *str++ = '\0'; 3124841a915dSSteven Rostedt (VMware) else 3125841a915dSSteven Rostedt (VMware) end[-1] = '\0'; /* Must be nul terminated */ 3126841a915dSSteven Rostedt (VMware) } 31274370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 3128fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 31294370aa4aSLai Jiangshan fmt++; 3130fef20d9cSFrederic Weisbecker break; 3131fef20d9cSFrederic Weisbecker 3132fef20d9cSFrederic Weisbecker default: 3133fef20d9cSFrederic Weisbecker switch (spec.type) { 3134fef20d9cSFrederic Weisbecker 3135fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 3136fef20d9cSFrederic Weisbecker save_arg(long long); 3137fef20d9cSFrederic Weisbecker break; 3138fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 3139fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 3140fef20d9cSFrederic Weisbecker save_arg(unsigned long); 3141fef20d9cSFrederic Weisbecker break; 3142fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 3143fef20d9cSFrederic Weisbecker save_arg(size_t); 3144fef20d9cSFrederic Weisbecker break; 3145fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 3146fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 3147fef20d9cSFrederic Weisbecker break; 3148a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 3149a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 3150a4e94ef0SZhaolei save_arg(char); 3151a4e94ef0SZhaolei break; 3152fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 3153fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 3154fef20d9cSFrederic Weisbecker save_arg(short); 3155fef20d9cSFrederic Weisbecker break; 3156fef20d9cSFrederic Weisbecker default: 3157fef20d9cSFrederic Weisbecker save_arg(int); 3158fef20d9cSFrederic Weisbecker } 3159fef20d9cSFrederic Weisbecker } 3160fef20d9cSFrederic Weisbecker } 3161fef20d9cSFrederic Weisbecker 3162b006f19bSRasmus Villemoes out: 31637b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 3164fef20d9cSFrederic Weisbecker #undef save_arg 31654370aa4aSLai Jiangshan } 31664370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 31674370aa4aSLai Jiangshan 31684370aa4aSLai Jiangshan /** 31694370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 31704370aa4aSLai Jiangshan * @buf: The buffer to place the result into 31714370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 31724370aa4aSLai Jiangshan * @fmt: The format string to use 31734370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 31744370aa4aSLai Jiangshan * 31754370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 31764370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 31774370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 31784370aa4aSLai Jiangshan * 31794370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 31800efb4d20SSteven Rostedt * see vsnprintf comment for details. 31814370aa4aSLai Jiangshan * 31824370aa4aSLai Jiangshan * The return value is the number of characters which would 31834370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 31844370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 31854370aa4aSLai Jiangshan * number of characters written into @buf as return value 31864370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 31874370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 31884370aa4aSLai Jiangshan * string is truncated. 31894370aa4aSLai Jiangshan */ 31904370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 31914370aa4aSLai Jiangshan { 3192fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 3193d4be151bSAndré Goddard Rosa char *str, *end; 3194d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 31954370aa4aSLai Jiangshan 3196762abb51SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 31974370aa4aSLai Jiangshan return 0; 31984370aa4aSLai Jiangshan 31994370aa4aSLai Jiangshan 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 3223fef20d9cSFrederic Weisbecker while (*fmt) { 3224fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 3225d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 3226fef20d9cSFrederic Weisbecker 3227fef20d9cSFrederic Weisbecker fmt += read; 3228fef20d9cSFrederic Weisbecker 3229fef20d9cSFrederic Weisbecker switch (spec.type) { 3230fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 3231fef20d9cSFrederic Weisbecker int copy = read; 3232fef20d9cSFrederic Weisbecker if (str < end) { 3233fef20d9cSFrederic Weisbecker if (copy > end - str) 3234fef20d9cSFrederic Weisbecker copy = end - str; 3235fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 3236fef20d9cSFrederic Weisbecker } 3237fef20d9cSFrederic Weisbecker str += read; 3238fef20d9cSFrederic Weisbecker break; 32394370aa4aSLai Jiangshan } 32404370aa4aSLai Jiangshan 3241ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 32424d72ba01SRasmus Villemoes set_field_width(&spec, get_arg(int)); 3243fef20d9cSFrederic Weisbecker break; 32444370aa4aSLai Jiangshan 3245fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 32464d72ba01SRasmus Villemoes set_precision(&spec, get_arg(int)); 3247fef20d9cSFrederic Weisbecker break; 32484370aa4aSLai Jiangshan 3249d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 3250d4be151bSAndré Goddard Rosa char c; 3251d4be151bSAndré Goddard Rosa 3252fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 3253fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 32544370aa4aSLai Jiangshan if (str < end) 32554370aa4aSLai Jiangshan *str = ' '; 32564370aa4aSLai Jiangshan ++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; 3263fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 32644370aa4aSLai Jiangshan if (str < end) 32654370aa4aSLai Jiangshan *str = ' '; 32664370aa4aSLai Jiangshan ++str; 32674370aa4aSLai Jiangshan } 3268fef20d9cSFrederic Weisbecker break; 3269d4be151bSAndré Goddard Rosa } 32704370aa4aSLai Jiangshan 3271fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 32724370aa4aSLai Jiangshan const char *str_arg = args; 3273d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 3274fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 3275fef20d9cSFrederic Weisbecker break; 32764370aa4aSLai Jiangshan } 32774370aa4aSLai Jiangshan 3278841a915dSSteven Rostedt (VMware) case FORMAT_TYPE_PTR: { 3279841a915dSSteven Rostedt (VMware) bool process = false; 3280841a915dSSteven Rostedt (VMware) int copy, len; 3281841a915dSSteven Rostedt (VMware) /* Non function dereferences were already done */ 3282841a915dSSteven Rostedt (VMware) switch (*fmt) { 3283841a915dSSteven Rostedt (VMware) case 'S': 3284841a915dSSteven Rostedt (VMware) case 's': 32851e6338cfSSteven Rostedt (VMware) case 'x': 32861e6338cfSSteven Rostedt (VMware) case 'K': 328757f5677eSRasmus Villemoes case 'e': 3288841a915dSSteven Rostedt (VMware) process = true; 3289841a915dSSteven Rostedt (VMware) break; 3290841a915dSSteven Rostedt (VMware) default: 3291841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 3292841a915dSSteven Rostedt (VMware) process = true; 3293841a915dSSteven Rostedt (VMware) break; 3294841a915dSSteven Rostedt (VMware) } 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; 330262165600SSteven Rostedt (VMware) args += len + 1; 3303841a915dSSteven Rostedt (VMware) } 3304841a915dSSteven Rostedt (VMware) } 3305841a915dSSteven Rostedt (VMware) if (process) 3306ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 3307841a915dSSteven Rostedt (VMware) 3308fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 33094370aa4aSLai Jiangshan fmt++; 3310fef20d9cSFrederic Weisbecker break; 3311841a915dSSteven Rostedt (VMware) } 33124370aa4aSLai Jiangshan 3313fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 33144370aa4aSLai Jiangshan if (str < end) 33154370aa4aSLai Jiangshan *str = '%'; 33164370aa4aSLai Jiangshan ++str; 3317fef20d9cSFrederic Weisbecker break; 3318fef20d9cSFrederic Weisbecker 3319b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 3320b006f19bSRasmus Villemoes goto out; 3321b006f19bSRasmus Villemoes 3322d4be151bSAndré Goddard Rosa default: { 3323d4be151bSAndré Goddard Rosa unsigned long long num; 3324d4be151bSAndré Goddard Rosa 3325fef20d9cSFrederic Weisbecker switch (spec.type) { 3326fef20d9cSFrederic Weisbecker 3327fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 33284370aa4aSLai Jiangshan num = get_arg(long long); 3329fef20d9cSFrederic Weisbecker break; 3330fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 3331fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 3332fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 3333fef20d9cSFrederic Weisbecker break; 3334fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 33354370aa4aSLai Jiangshan num = get_arg(size_t); 3336fef20d9cSFrederic Weisbecker break; 3337fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 33384370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 3339fef20d9cSFrederic Weisbecker break; 3340a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 3341a4e94ef0SZhaolei num = get_arg(unsigned char); 3342a4e94ef0SZhaolei break; 3343a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 3344a4e94ef0SZhaolei num = get_arg(signed char); 3345a4e94ef0SZhaolei break; 3346fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 3347fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 3348fef20d9cSFrederic Weisbecker break; 3349fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 3350fef20d9cSFrederic Weisbecker num = get_arg(short); 3351fef20d9cSFrederic Weisbecker break; 3352fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 33534370aa4aSLai Jiangshan num = get_arg(unsigned int); 3354fef20d9cSFrederic Weisbecker break; 3355fef20d9cSFrederic Weisbecker default: 3356fef20d9cSFrederic Weisbecker num = get_arg(int); 33574370aa4aSLai Jiangshan } 3358fef20d9cSFrederic Weisbecker 3359fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 3360d4be151bSAndré Goddard Rosa } /* default: */ 3361d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 3362d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 3363fef20d9cSFrederic Weisbecker 3364b006f19bSRasmus Villemoes out: 33654370aa4aSLai Jiangshan if (size > 0) { 33664370aa4aSLai Jiangshan if (str < end) 33674370aa4aSLai Jiangshan *str = '\0'; 33684370aa4aSLai Jiangshan else 33694370aa4aSLai Jiangshan end[-1] = '\0'; 33704370aa4aSLai Jiangshan } 3371fef20d9cSFrederic Weisbecker 33724370aa4aSLai Jiangshan #undef get_arg 33734370aa4aSLai Jiangshan 33744370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 33754370aa4aSLai Jiangshan return str - buf; 33764370aa4aSLai Jiangshan } 33774370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 33784370aa4aSLai Jiangshan 33794370aa4aSLai Jiangshan /** 33804370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 33814370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 33824370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 33834370aa4aSLai Jiangshan * @fmt: The format string to use 33844370aa4aSLai Jiangshan * @...: Arguments for the format string 33854370aa4aSLai Jiangshan * 33864370aa4aSLai Jiangshan * The function returns the number of words(u32) written 33874370aa4aSLai Jiangshan * into @bin_buf. 33884370aa4aSLai Jiangshan */ 33894370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 33904370aa4aSLai Jiangshan { 33914370aa4aSLai Jiangshan va_list args; 33924370aa4aSLai Jiangshan int ret; 33934370aa4aSLai Jiangshan 33944370aa4aSLai Jiangshan va_start(args, fmt); 33954370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 33964370aa4aSLai Jiangshan va_end(args); 33977b9186f5SAndré Goddard Rosa 33984370aa4aSLai Jiangshan return ret; 33994370aa4aSLai Jiangshan } 34004370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 34014370aa4aSLai Jiangshan 34024370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 34034370aa4aSLai Jiangshan 34041da177e4SLinus Torvalds /** 34051da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 34061da177e4SLinus Torvalds * @buf: input buffer 34071da177e4SLinus Torvalds * @fmt: format of buffer 34081da177e4SLinus Torvalds * @args: arguments 34091da177e4SLinus Torvalds */ 34101da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 34111da177e4SLinus Torvalds { 34121da177e4SLinus Torvalds const char *str = buf; 34131da177e4SLinus Torvalds char *next; 34141da177e4SLinus Torvalds char digit; 34151da177e4SLinus Torvalds int num = 0; 3416ef0658f3SJoe Perches u8 qualifier; 341753809751SJan Beulich unsigned int base; 341853809751SJan Beulich union { 341953809751SJan Beulich long long s; 342053809751SJan Beulich unsigned long long u; 342153809751SJan Beulich } val; 3422ef0658f3SJoe Perches s16 field_width; 3423d4be151bSAndré Goddard Rosa bool is_sign; 34241da177e4SLinus Torvalds 3425da99075cSJan Beulich while (*fmt) { 34261da177e4SLinus Torvalds /* skip any white space in format */ 34279dbbc3b9SZhen Lei /* white space in format matches any amount of 34281da177e4SLinus Torvalds * white space, including none, in the input. 34291da177e4SLinus Torvalds */ 34301da177e4SLinus Torvalds if (isspace(*fmt)) { 3431e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 3432e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 34331da177e4SLinus Torvalds } 34341da177e4SLinus Torvalds 34351da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 34361da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 34371da177e4SLinus Torvalds if (*fmt++ != *str++) 34381da177e4SLinus Torvalds break; 34391da177e4SLinus Torvalds continue; 34401da177e4SLinus Torvalds } 34411da177e4SLinus Torvalds 34421da177e4SLinus Torvalds if (!*fmt) 34431da177e4SLinus Torvalds break; 34441da177e4SLinus Torvalds ++fmt; 34451da177e4SLinus Torvalds 34461da177e4SLinus Torvalds /* skip this conversion. 34471da177e4SLinus Torvalds * advance both strings to next white space 34481da177e4SLinus Torvalds */ 34491da177e4SLinus Torvalds if (*fmt == '*') { 3450da99075cSJan Beulich if (!*str) 3451da99075cSJan Beulich break; 3452f9310b2fSJessica Yu while (!isspace(*fmt) && *fmt != '%' && *fmt) { 3453f9310b2fSJessica Yu /* '%*[' not yet supported, invalid format */ 3454f9310b2fSJessica Yu if (*fmt == '[') 3455f9310b2fSJessica Yu return num; 34561da177e4SLinus Torvalds fmt++; 3457f9310b2fSJessica Yu } 34581da177e4SLinus Torvalds while (!isspace(*str) && *str) 34591da177e4SLinus Torvalds str++; 34601da177e4SLinus Torvalds continue; 34611da177e4SLinus Torvalds } 34621da177e4SLinus Torvalds 34631da177e4SLinus Torvalds /* get field width */ 34641da177e4SLinus Torvalds field_width = -1; 346553809751SJan Beulich if (isdigit(*fmt)) { 34661da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 346753809751SJan Beulich if (field_width <= 0) 346853809751SJan Beulich break; 346953809751SJan Beulich } 34701da177e4SLinus Torvalds 34711da177e4SLinus Torvalds /* get conversion qualifier */ 34721da177e4SLinus Torvalds qualifier = -1; 347375fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 34745b5e0928SAlexey Dobriyan *fmt == 'z') { 34751da177e4SLinus Torvalds qualifier = *fmt++; 34761da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 34771da177e4SLinus Torvalds if (qualifier == 'h') { 34781da177e4SLinus Torvalds qualifier = 'H'; 34791da177e4SLinus Torvalds fmt++; 34801da177e4SLinus Torvalds } else if (qualifier == 'l') { 34811da177e4SLinus Torvalds qualifier = 'L'; 34821da177e4SLinus Torvalds fmt++; 34831da177e4SLinus Torvalds } 34841da177e4SLinus Torvalds } 34851da177e4SLinus Torvalds } 34861da177e4SLinus Torvalds 3487da99075cSJan Beulich if (!*fmt) 3488da99075cSJan Beulich break; 3489da99075cSJan Beulich 3490da99075cSJan Beulich if (*fmt == 'n') { 3491da99075cSJan Beulich /* return number of characters read so far */ 3492da99075cSJan Beulich *va_arg(args, int *) = str - buf; 3493da99075cSJan Beulich ++fmt; 3494da99075cSJan Beulich continue; 3495da99075cSJan Beulich } 3496da99075cSJan Beulich 3497da99075cSJan Beulich if (!*str) 34981da177e4SLinus Torvalds break; 34991da177e4SLinus Torvalds 3500d4be151bSAndré Goddard Rosa base = 10; 35013f623ebaSFabian Frederick is_sign = false; 3502d4be151bSAndré Goddard Rosa 35031da177e4SLinus Torvalds switch (*fmt++) { 35041da177e4SLinus Torvalds case 'c': 35051da177e4SLinus Torvalds { 35061da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 35071da177e4SLinus Torvalds if (field_width == -1) 35081da177e4SLinus Torvalds field_width = 1; 35091da177e4SLinus Torvalds do { 35101da177e4SLinus Torvalds *s++ = *str++; 35111da177e4SLinus Torvalds } while (--field_width > 0 && *str); 35121da177e4SLinus Torvalds num++; 35131da177e4SLinus Torvalds } 35141da177e4SLinus Torvalds continue; 35151da177e4SLinus Torvalds case 's': 35161da177e4SLinus Torvalds { 35171da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 35181da177e4SLinus Torvalds if (field_width == -1) 35194be929beSAlexey Dobriyan field_width = SHRT_MAX; 35201da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 3521e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 35221da177e4SLinus Torvalds 35231da177e4SLinus Torvalds /* now copy until next white space */ 35247b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 35251da177e4SLinus Torvalds *s++ = *str++; 35261da177e4SLinus Torvalds *s = '\0'; 35271da177e4SLinus Torvalds num++; 35281da177e4SLinus Torvalds } 35291da177e4SLinus Torvalds continue; 3530f9310b2fSJessica Yu /* 3531f9310b2fSJessica Yu * Warning: This implementation of the '[' conversion specifier 3532f9310b2fSJessica Yu * deviates from its glibc counterpart in the following ways: 3533f9310b2fSJessica Yu * (1) It does NOT support ranges i.e. '-' is NOT a special 3534f9310b2fSJessica Yu * character 3535f9310b2fSJessica Yu * (2) It cannot match the closing bracket ']' itself 3536f9310b2fSJessica Yu * (3) A field width is required 3537f9310b2fSJessica Yu * (4) '%*[' (discard matching input) is currently not supported 3538f9310b2fSJessica Yu * 3539f9310b2fSJessica Yu * Example usage: 3540f9310b2fSJessica Yu * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3541f9310b2fSJessica Yu * buf1, buf2, buf3); 3542f9310b2fSJessica Yu * if (ret < 3) 3543f9310b2fSJessica Yu * // etc.. 3544f9310b2fSJessica Yu */ 3545f9310b2fSJessica Yu case '[': 3546f9310b2fSJessica Yu { 3547f9310b2fSJessica Yu char *s = (char *)va_arg(args, char *); 3548f9310b2fSJessica Yu DECLARE_BITMAP(set, 256) = {0}; 3549f9310b2fSJessica Yu unsigned int len = 0; 3550f9310b2fSJessica Yu bool negate = (*fmt == '^'); 3551f9310b2fSJessica Yu 3552f9310b2fSJessica Yu /* field width is required */ 3553f9310b2fSJessica Yu if (field_width == -1) 3554f9310b2fSJessica Yu return num; 3555f9310b2fSJessica Yu 3556f9310b2fSJessica Yu if (negate) 3557f9310b2fSJessica Yu ++fmt; 3558f9310b2fSJessica Yu 3559f9310b2fSJessica Yu for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 356052e68cd6SChristophe JAILLET __set_bit((u8)*fmt, set); 3561f9310b2fSJessica Yu 3562f9310b2fSJessica Yu /* no ']' or no character set found */ 3563f9310b2fSJessica Yu if (!*fmt || !len) 3564f9310b2fSJessica Yu return num; 3565f9310b2fSJessica Yu ++fmt; 3566f9310b2fSJessica Yu 3567f9310b2fSJessica Yu if (negate) { 3568f9310b2fSJessica Yu bitmap_complement(set, set, 256); 3569f9310b2fSJessica Yu /* exclude null '\0' byte */ 357052e68cd6SChristophe JAILLET __clear_bit(0, set); 3571f9310b2fSJessica Yu } 3572f9310b2fSJessica Yu 3573f9310b2fSJessica Yu /* match must be non-empty */ 3574f9310b2fSJessica Yu if (!test_bit((u8)*str, set)) 3575f9310b2fSJessica Yu return num; 3576f9310b2fSJessica Yu 3577f9310b2fSJessica Yu while (test_bit((u8)*str, set) && field_width--) 3578f9310b2fSJessica Yu *s++ = *str++; 3579f9310b2fSJessica Yu *s = '\0'; 3580f9310b2fSJessica Yu ++num; 3581f9310b2fSJessica Yu } 3582f9310b2fSJessica Yu continue; 35831da177e4SLinus Torvalds case 'o': 35841da177e4SLinus Torvalds base = 8; 35851da177e4SLinus Torvalds break; 35861da177e4SLinus Torvalds case 'x': 35871da177e4SLinus Torvalds case 'X': 35881da177e4SLinus Torvalds base = 16; 35891da177e4SLinus Torvalds break; 35901da177e4SLinus Torvalds case 'i': 35911da177e4SLinus Torvalds base = 0; 35924c1ca831SNick Desaulniers fallthrough; 35931da177e4SLinus Torvalds case 'd': 35943f623ebaSFabian Frederick is_sign = true; 35954c1ca831SNick Desaulniers fallthrough; 35961da177e4SLinus Torvalds case 'u': 35971da177e4SLinus Torvalds break; 35981da177e4SLinus Torvalds case '%': 35991da177e4SLinus Torvalds /* looking for '%' in str */ 36001da177e4SLinus Torvalds if (*str++ != '%') 36011da177e4SLinus Torvalds return num; 36021da177e4SLinus Torvalds continue; 36031da177e4SLinus Torvalds default: 36041da177e4SLinus Torvalds /* invalid format; stop here */ 36051da177e4SLinus Torvalds return num; 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds 36081da177e4SLinus Torvalds /* have some sort of integer conversion. 36091da177e4SLinus Torvalds * first, skip white space in buffer. 36101da177e4SLinus Torvalds */ 3611e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 36121da177e4SLinus Torvalds 36131da177e4SLinus Torvalds digit = *str; 361411b3dda5SRichard Fitzgerald if (is_sign && digit == '-') { 361511b3dda5SRichard Fitzgerald if (field_width == 1) 361611b3dda5SRichard Fitzgerald break; 361711b3dda5SRichard Fitzgerald 36181da177e4SLinus Torvalds digit = *(str + 1); 361911b3dda5SRichard Fitzgerald } 36201da177e4SLinus Torvalds 36211da177e4SLinus Torvalds if (!digit 36221da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 36231da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 36241da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 36251da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 36261da177e4SLinus Torvalds break; 36271da177e4SLinus Torvalds 362853809751SJan Beulich if (is_sign) 3629900fdc45SRichard Fitzgerald val.s = simple_strntoll(str, 3630900fdc45SRichard Fitzgerald field_width >= 0 ? field_width : INT_MAX, 3631900fdc45SRichard Fitzgerald &next, base); 363253809751SJan Beulich else 3633900fdc45SRichard Fitzgerald val.u = simple_strntoull(str, 3634900fdc45SRichard Fitzgerald field_width >= 0 ? field_width : INT_MAX, 3635900fdc45SRichard Fitzgerald &next, base); 363653809751SJan Beulich 36371da177e4SLinus Torvalds switch (qualifier) { 36381da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 363953809751SJan Beulich if (is_sign) 364053809751SJan Beulich *va_arg(args, signed char *) = val.s; 364153809751SJan Beulich else 364253809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 36431da177e4SLinus Torvalds break; 36441da177e4SLinus Torvalds case 'h': 364553809751SJan Beulich if (is_sign) 364653809751SJan Beulich *va_arg(args, short *) = val.s; 364753809751SJan Beulich else 364853809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 36491da177e4SLinus Torvalds break; 36501da177e4SLinus Torvalds case 'l': 365153809751SJan Beulich if (is_sign) 365253809751SJan Beulich *va_arg(args, long *) = val.s; 365353809751SJan Beulich else 365453809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 36551da177e4SLinus Torvalds break; 36561da177e4SLinus Torvalds case 'L': 365753809751SJan Beulich if (is_sign) 365853809751SJan Beulich *va_arg(args, long long *) = val.s; 365953809751SJan Beulich else 366053809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 36611da177e4SLinus Torvalds break; 36621da177e4SLinus Torvalds case 'z': 366353809751SJan Beulich *va_arg(args, size_t *) = val.u; 36641da177e4SLinus Torvalds break; 36651da177e4SLinus Torvalds default: 366653809751SJan Beulich if (is_sign) 366753809751SJan Beulich *va_arg(args, int *) = val.s; 366853809751SJan Beulich else 366953809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 36701da177e4SLinus Torvalds break; 36711da177e4SLinus Torvalds } 36721da177e4SLinus Torvalds num++; 36731da177e4SLinus Torvalds 36741da177e4SLinus Torvalds if (!next) 36751da177e4SLinus Torvalds break; 36761da177e4SLinus Torvalds str = next; 36771da177e4SLinus Torvalds } 3678c6b40d16SJohannes Berg 36791da177e4SLinus Torvalds return num; 36801da177e4SLinus Torvalds } 36811da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 36821da177e4SLinus Torvalds 36831da177e4SLinus Torvalds /** 36841da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 36851da177e4SLinus Torvalds * @buf: input buffer 36861da177e4SLinus Torvalds * @fmt: formatting of buffer 36871da177e4SLinus Torvalds * @...: resulting arguments 36881da177e4SLinus Torvalds */ 36891da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 36901da177e4SLinus Torvalds { 36911da177e4SLinus Torvalds va_list args; 36921da177e4SLinus Torvalds int i; 36931da177e4SLinus Torvalds 36941da177e4SLinus Torvalds va_start(args, fmt); 36951da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 36961da177e4SLinus Torvalds va_end(args); 36977b9186f5SAndré Goddard Rosa 36981da177e4SLinus Torvalds return i; 36991da177e4SLinus Torvalds } 37001da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 3701