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> 441031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 451031bc58SDmitry Monakhov #include <linux/blkdev.h> 461031bc58SDmitry Monakhov #endif 471da177e4SLinus Torvalds 48edf14cdbSVlastimil Babka #include "../mm/internal.h" /* For the trace_print_flags arrays */ 49edf14cdbSVlastimil Babka 504e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 517c43d9a3SRasmus Villemoes #include <asm/byteorder.h> /* cpu_to_le16 */ 521da177e4SLinus Torvalds 5371dca95dSAndy Shevchenko #include <linux/string_helpers.h> 541dff46d6SAlexey Dobriyan #include "kstrtox.h" 55aa46a63eSHarvey Harrison 56*84842911SChristophe Leroy /* Disable pointer hashing if requested */ 57*84842911SChristophe Leroy bool no_hash_pointers __ro_after_init; 58*84842911SChristophe Leroy EXPORT_SYMBOL_GPL(no_hash_pointers); 59*84842911SChristophe Leroy 60839b395eSAlexey Dobriyan static noinline unsigned long long simple_strntoull(const char *startp, size_t max_chars, char **endp, unsigned int base) 61900fdc45SRichard Fitzgerald { 62900fdc45SRichard Fitzgerald const char *cp; 63900fdc45SRichard Fitzgerald unsigned long long result = 0ULL; 64900fdc45SRichard Fitzgerald size_t prefix_chars; 65900fdc45SRichard Fitzgerald unsigned int rv; 66900fdc45SRichard Fitzgerald 67900fdc45SRichard Fitzgerald cp = _parse_integer_fixup_radix(startp, &base); 68900fdc45SRichard Fitzgerald prefix_chars = cp - startp; 69900fdc45SRichard Fitzgerald if (prefix_chars < max_chars) { 70900fdc45SRichard Fitzgerald rv = _parse_integer_limit(cp, base, &result, max_chars - prefix_chars); 71900fdc45SRichard Fitzgerald /* FIXME */ 72900fdc45SRichard Fitzgerald cp += (rv & ~KSTRTOX_OVERFLOW); 73900fdc45SRichard Fitzgerald } else { 74900fdc45SRichard Fitzgerald /* Field too short for prefix + digit, skip over without converting */ 75900fdc45SRichard Fitzgerald cp = startp + max_chars; 76900fdc45SRichard Fitzgerald } 77900fdc45SRichard Fitzgerald 78900fdc45SRichard Fitzgerald if (endp) 79900fdc45SRichard Fitzgerald *endp = (char *)cp; 80900fdc45SRichard Fitzgerald 81900fdc45SRichard Fitzgerald return result; 82900fdc45SRichard Fitzgerald } 83900fdc45SRichard Fitzgerald 841da177e4SLinus Torvalds /** 851da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 861da177e4SLinus Torvalds * @cp: The start of the string 871da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 881da177e4SLinus Torvalds * @base: The number base to use 89462e4711SEldad Zack * 90e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoull instead. 911da177e4SLinus Torvalds */ 92ad65dcefSAlexey Dobriyan noinline 931da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 941da177e4SLinus Torvalds { 95900fdc45SRichard Fitzgerald return simple_strntoull(cp, INT_MAX, endp, base); 961da177e4SLinus Torvalds } 971da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /** 100922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 101922ac25cSAndré Goddard Rosa * @cp: The start of the string 102922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 103922ac25cSAndré Goddard Rosa * @base: The number base to use 104462e4711SEldad Zack * 105e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoul instead. 106922ac25cSAndré Goddard Rosa */ 107922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 108922ac25cSAndré Goddard Rosa { 109922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 110922ac25cSAndré Goddard Rosa } 111922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 112922ac25cSAndré Goddard Rosa 113922ac25cSAndré Goddard Rosa /** 114922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 115922ac25cSAndré Goddard Rosa * @cp: The start of the string 116922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 117922ac25cSAndré Goddard Rosa * @base: The number base to use 118462e4711SEldad Zack * 119e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtol instead. 120922ac25cSAndré Goddard Rosa */ 121922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 122922ac25cSAndré Goddard Rosa { 123922ac25cSAndré Goddard Rosa if (*cp == '-') 124922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 125922ac25cSAndré Goddard Rosa 126922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 127922ac25cSAndré Goddard Rosa } 128922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 129922ac25cSAndré Goddard Rosa 130900fdc45SRichard Fitzgerald static long long simple_strntoll(const char *cp, size_t max_chars, char **endp, 131900fdc45SRichard Fitzgerald unsigned int base) 132900fdc45SRichard Fitzgerald { 133900fdc45SRichard Fitzgerald /* 134900fdc45SRichard Fitzgerald * simple_strntoull() safely handles receiving max_chars==0 in the 135900fdc45SRichard Fitzgerald * case cp[0] == '-' && max_chars == 1. 136900fdc45SRichard Fitzgerald * If max_chars == 0 we can drop through and pass it to simple_strntoull() 137900fdc45SRichard Fitzgerald * and the content of *cp is irrelevant. 138900fdc45SRichard Fitzgerald */ 139900fdc45SRichard Fitzgerald if (*cp == '-' && max_chars > 0) 140900fdc45SRichard Fitzgerald return -simple_strntoull(cp + 1, max_chars - 1, endp, base); 141900fdc45SRichard Fitzgerald 142900fdc45SRichard Fitzgerald return simple_strntoull(cp, max_chars, endp, base); 143900fdc45SRichard Fitzgerald } 144900fdc45SRichard Fitzgerald 145922ac25cSAndré Goddard Rosa /** 1461da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1471da177e4SLinus Torvalds * @cp: The start of the string 1481da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1491da177e4SLinus Torvalds * @base: The number base to use 150462e4711SEldad Zack * 151e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoll instead. 1521da177e4SLinus Torvalds */ 1531da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1541da177e4SLinus Torvalds { 155900fdc45SRichard Fitzgerald return simple_strntoll(cp, INT_MAX, endp, base); 1561da177e4SLinus Torvalds } 15798d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1581da177e4SLinus Torvalds 159cf3b429bSJoe Perches static noinline_for_stack 160cf3b429bSJoe Perches int skip_atoi(const char **s) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds int i = 0; 1631da177e4SLinus Torvalds 16443e5b666SRasmus Villemoes do { 1651da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 16643e5b666SRasmus Villemoes } while (isdigit(**s)); 1677b9186f5SAndré Goddard Rosa 1681da177e4SLinus Torvalds return i; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 1717c43d9a3SRasmus Villemoes /* 1727c43d9a3SRasmus Villemoes * Decimal conversion is by far the most typical, and is used for 1737c43d9a3SRasmus Villemoes * /proc and /sys data. This directly impacts e.g. top performance 1747c43d9a3SRasmus Villemoes * with many processes running. We optimize it for speed by emitting 1757c43d9a3SRasmus Villemoes * two characters at a time, using a 200 byte lookup table. This 1767c43d9a3SRasmus Villemoes * roughly halves the number of multiplications compared to computing 1777c43d9a3SRasmus Villemoes * the digits one at a time. Implementation strongly inspired by the 1787c43d9a3SRasmus Villemoes * previous version, which in turn used ideas described at 1797c43d9a3SRasmus Villemoes * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 1807c43d9a3SRasmus Villemoes * from the author, Douglas W. Jones). 1817c43d9a3SRasmus Villemoes * 1827c43d9a3SRasmus Villemoes * It turns out there is precisely one 26 bit fixed-point 1837c43d9a3SRasmus Villemoes * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 1847c43d9a3SRasmus Villemoes * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 1857c43d9a3SRasmus Villemoes * range happens to be somewhat larger (x <= 1073741898), but that's 1867c43d9a3SRasmus Villemoes * irrelevant for our purpose. 1877c43d9a3SRasmus Villemoes * 1887c43d9a3SRasmus Villemoes * For dividing a number in the range [10^4, 10^6-1] by 100, we still 1897c43d9a3SRasmus Villemoes * need a 32x32->64 bit multiply, so we simply use the same constant. 1907c43d9a3SRasmus Villemoes * 1917c43d9a3SRasmus Villemoes * For dividing a number in the range [100, 10^4-1] by 100, there are 1927c43d9a3SRasmus Villemoes * several options. The simplest is (x * 0x147b) >> 19, which is valid 1937c43d9a3SRasmus Villemoes * for all x <= 43698. 194133fd9f5SDenys Vlasenko */ 1954277eeddSDenis Vlasenko 1967c43d9a3SRasmus Villemoes static const u16 decpair[100] = { 1977c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 1987c43d9a3SRasmus Villemoes _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 1997c43d9a3SRasmus Villemoes _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 2007c43d9a3SRasmus Villemoes _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 2017c43d9a3SRasmus Villemoes _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 2027c43d9a3SRasmus Villemoes _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 2037c43d9a3SRasmus Villemoes _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 2047c43d9a3SRasmus Villemoes _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 2057c43d9a3SRasmus Villemoes _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 2067c43d9a3SRasmus Villemoes _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 2077c43d9a3SRasmus Villemoes _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 2087c43d9a3SRasmus Villemoes #undef _ 2097c43d9a3SRasmus Villemoes }; 2104277eeddSDenis Vlasenko 2117b9186f5SAndré Goddard Rosa /* 2127c43d9a3SRasmus Villemoes * This will print a single '0' even if r == 0, since we would 213675cf53cSRasmus Villemoes * immediately jump to out_r where two 0s would be written but only 214675cf53cSRasmus Villemoes * one of them accounted for in buf. This is needed by ip4_string 215675cf53cSRasmus Villemoes * below. All other callers pass a non-zero value of r. 216133fd9f5SDenys Vlasenko */ 217133fd9f5SDenys Vlasenko static noinline_for_stack 218133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 219133fd9f5SDenys Vlasenko { 220133fd9f5SDenys Vlasenko unsigned q; 2214277eeddSDenis Vlasenko 2227c43d9a3SRasmus Villemoes /* 1 <= r < 10^8 */ 2237c43d9a3SRasmus Villemoes if (r < 100) 2247c43d9a3SRasmus Villemoes goto out_r; 225cb239d0aSGeorge Spelvin 2267c43d9a3SRasmus Villemoes /* 100 <= r < 10^8 */ 2277c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2287c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2297c43d9a3SRasmus Villemoes buf += 2; 2307c43d9a3SRasmus Villemoes 2317c43d9a3SRasmus Villemoes /* 1 <= q < 10^6 */ 2327c43d9a3SRasmus Villemoes if (q < 100) 2337c43d9a3SRasmus Villemoes goto out_q; 2347c43d9a3SRasmus Villemoes 2357c43d9a3SRasmus Villemoes /* 100 <= q < 10^6 */ 2367c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2377c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2387c43d9a3SRasmus Villemoes buf += 2; 2397c43d9a3SRasmus Villemoes 2407c43d9a3SRasmus Villemoes /* 1 <= r < 10^4 */ 2417c43d9a3SRasmus Villemoes if (r < 100) 2427c43d9a3SRasmus Villemoes goto out_r; 2437c43d9a3SRasmus Villemoes 2447c43d9a3SRasmus Villemoes /* 100 <= r < 10^4 */ 2457c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2467c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2477c43d9a3SRasmus Villemoes buf += 2; 2487c43d9a3SRasmus Villemoes out_q: 2497c43d9a3SRasmus Villemoes /* 1 <= q < 100 */ 2507c43d9a3SRasmus Villemoes r = q; 2517c43d9a3SRasmus Villemoes out_r: 2527c43d9a3SRasmus Villemoes /* 1 <= r < 100 */ 2537c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r]; 254675cf53cSRasmus Villemoes buf += r < 10 ? 1 : 2; 255133fd9f5SDenys Vlasenko return buf; 256133fd9f5SDenys Vlasenko } 257133fd9f5SDenys Vlasenko 2587c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 2597c43d9a3SRasmus Villemoes static noinline_for_stack 2607c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r) 2617c43d9a3SRasmus Villemoes { 2627c43d9a3SRasmus Villemoes unsigned q; 263133fd9f5SDenys Vlasenko 2647c43d9a3SRasmus Villemoes /* 0 <= r < 10^8 */ 2657c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2667c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2677c43d9a3SRasmus Villemoes buf += 2; 268133fd9f5SDenys Vlasenko 2697c43d9a3SRasmus Villemoes /* 0 <= q < 10^6 */ 2707c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2717c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2727c43d9a3SRasmus Villemoes buf += 2; 273133fd9f5SDenys Vlasenko 2747c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2757c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2767c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2777c43d9a3SRasmus Villemoes buf += 2; 2787c43d9a3SRasmus Villemoes 2797c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2807c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2817c43d9a3SRasmus Villemoes buf += 2; 2827c43d9a3SRasmus Villemoes return buf; 2837c43d9a3SRasmus Villemoes } 2847c43d9a3SRasmus Villemoes 2857c43d9a3SRasmus Villemoes static noinline_for_stack 286133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 287133fd9f5SDenys Vlasenko { 288133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 2897c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2907c43d9a3SRasmus Villemoes /* 1 <= n <= 1.6e11 */ 2917c43d9a3SRasmus Villemoes if (n >= 100*1000*1000) 2927c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2937c43d9a3SRasmus Villemoes /* 1 <= n < 1e8 */ 294133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 295133fd9f5SDenys Vlasenko } 296133fd9f5SDenys Vlasenko 2977c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 298133fd9f5SDenys Vlasenko 2997c43d9a3SRasmus Villemoes static void 3007c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r) 301133fd9f5SDenys Vlasenko { 3027c43d9a3SRasmus Villemoes unsigned q; 3037c43d9a3SRasmus Villemoes 3047c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 3057c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 3067c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 3077c43d9a3SRasmus Villemoes buf += 2; 3087c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 3097c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 3102359172aSGeorge Spelvin } 3112359172aSGeorge Spelvin 3122359172aSGeorge Spelvin /* 3132359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 3142359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 3152359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 3162359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 3177c43d9a3SRasmus Villemoes * (second call in the put_dec code, assuming n is all-ones). 3182359172aSGeorge Spelvin */ 3197c43d9a3SRasmus Villemoes static noinline_for_stack 3202359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 3212359172aSGeorge Spelvin { 3222359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 3232359172aSGeorge Spelvin 3242359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 3252359172aSGeorge Spelvin return q; 326133fd9f5SDenys Vlasenko } 327133fd9f5SDenys Vlasenko 328133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 329133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 330133fd9f5SDenys Vlasenko * (with permission from the author). 331133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 332133fd9f5SDenys Vlasenko */ 333133fd9f5SDenys Vlasenko static 334133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 335133fd9f5SDenys Vlasenko { 336133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 337133fd9f5SDenys Vlasenko 338133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 339133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 340133fd9f5SDenys Vlasenko 341133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 342133fd9f5SDenys Vlasenko h = (n >> 32); 343133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 344133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 345133fd9f5SDenys Vlasenko 3467c43d9a3SRasmus Villemoes /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 3477c43d9a3SRasmus Villemoes = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 348133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 3492359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 350133fd9f5SDenys Vlasenko 3512359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 3522359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 353133fd9f5SDenys Vlasenko 3542359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3552359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 356133fd9f5SDenys Vlasenko 3572359172aSGeorge Spelvin q += 281 * d3; 3582359172aSGeorge Spelvin buf += 12; 3592359172aSGeorge Spelvin if (q) 3602359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3612359172aSGeorge Spelvin else while (buf[-1] == '0') 362133fd9f5SDenys Vlasenko --buf; 3637b9186f5SAndré Goddard Rosa 3644277eeddSDenis Vlasenko return buf; 3654277eeddSDenis Vlasenko } 366133fd9f5SDenys Vlasenko 367133fd9f5SDenys Vlasenko #endif 3684277eeddSDenis Vlasenko 3691ac101a5SKAMEZAWA Hiroyuki /* 3701ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3711ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3721ac101a5SKAMEZAWA Hiroyuki * 3731ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3741ac101a5SKAMEZAWA Hiroyuki */ 375d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) 3761ac101a5SKAMEZAWA Hiroyuki { 3777c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 3787c43d9a3SRasmus Villemoes char tmp[sizeof(num) * 3] __aligned(2); 3791ac101a5SKAMEZAWA Hiroyuki int idx, len; 3801ac101a5SKAMEZAWA Hiroyuki 381133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 382133fd9f5SDenys Vlasenko if (num <= 9) { 383133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 384133fd9f5SDenys Vlasenko len = 1; 385133fd9f5SDenys Vlasenko } else { 3861ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 387133fd9f5SDenys Vlasenko } 3881ac101a5SKAMEZAWA Hiroyuki 389d1be35cbSAndrei Vagin if (len > size || width > size) 3901ac101a5SKAMEZAWA Hiroyuki return 0; 391d1be35cbSAndrei Vagin 392d1be35cbSAndrei Vagin if (width > len) { 393d1be35cbSAndrei Vagin width = width - len; 394d1be35cbSAndrei Vagin for (idx = 0; idx < width; idx++) 395d1be35cbSAndrei Vagin buf[idx] = ' '; 396d1be35cbSAndrei Vagin } else { 397d1be35cbSAndrei Vagin width = 0; 398d1be35cbSAndrei Vagin } 399d1be35cbSAndrei Vagin 4001ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 401d1be35cbSAndrei Vagin buf[idx + width] = tmp[len - idx - 1]; 402d1be35cbSAndrei Vagin 403d1be35cbSAndrei Vagin return len + width; 4041ac101a5SKAMEZAWA Hiroyuki } 4051ac101a5SKAMEZAWA Hiroyuki 40651be17dfSRasmus Villemoes #define SIGN 1 /* unsigned/signed, must be 1 */ 407d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */ 4081da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 4091da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 410d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 411b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 412b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 4131da177e4SLinus Torvalds 41424a1dffbSAndy Shevchenko static_assert(SIGN == 1); 415b886690dSAndy Shevchenko static_assert(ZEROPAD == ('0' - ' ')); 41624a1dffbSAndy Shevchenko static_assert(SMALL == ('a' ^ 'A')); 417b886690dSAndy Shevchenko 418fef20d9cSFrederic Weisbecker enum format_type { 419fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 420ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 421fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 422fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 423fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 424fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 425fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 426fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 427fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 428fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 429fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 430a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 431a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 432fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 433fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 434fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 435fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 436fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 437fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 438fef20d9cSFrederic Weisbecker }; 439fef20d9cSFrederic Weisbecker 440fef20d9cSFrederic Weisbecker struct printf_spec { 441d0484193SRasmus Villemoes unsigned int type:8; /* format_type enum */ 442d0484193SRasmus Villemoes signed int field_width:24; /* width of output field */ 443d0484193SRasmus Villemoes unsigned int flags:8; /* flags to number() */ 444d0484193SRasmus Villemoes unsigned int base:8; /* number base, 8, 10 or 16 only */ 445d0484193SRasmus Villemoes signed int precision:16; /* # of digits/chars */ 446d0484193SRasmus Villemoes } __packed; 447ef27ac18SRasmus Villemoes static_assert(sizeof(struct printf_spec) == 8); 448ef27ac18SRasmus Villemoes 4494d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1) 4504d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1) 451fef20d9cSFrederic Weisbecker 452cf3b429bSJoe Perches static noinline_for_stack 453cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 454fef20d9cSFrederic Weisbecker struct printf_spec spec) 4551da177e4SLinus Torvalds { 4567c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 4577c43d9a3SRasmus Villemoes char tmp[3 * sizeof(num)] __aligned(2); 4589b706aeeSDenys Vlasenko char sign; 4599b706aeeSDenys Vlasenko char locase; 460fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4611da177e4SLinus Torvalds int i; 4627c203422SPierre Carrier bool is_zero = num == 0LL; 4631c7a8e62SRasmus Villemoes int field_width = spec.field_width; 4641c7a8e62SRasmus Villemoes int precision = spec.precision; 4651da177e4SLinus Torvalds 4669b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4679b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 468fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 469fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 470fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4711da177e4SLinus Torvalds sign = 0; 472fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4731da177e4SLinus Torvalds if ((signed long long)num < 0) { 4741da177e4SLinus Torvalds sign = '-'; 4751da177e4SLinus Torvalds num = -(signed long long)num; 4761c7a8e62SRasmus Villemoes field_width--; 477fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4781da177e4SLinus Torvalds sign = '+'; 4791c7a8e62SRasmus Villemoes field_width--; 480fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4811da177e4SLinus Torvalds sign = ' '; 4821c7a8e62SRasmus Villemoes field_width--; 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds } 485b39a7340SDenis Vlasenko if (need_pfx) { 486fef20d9cSFrederic Weisbecker if (spec.base == 16) 4871c7a8e62SRasmus Villemoes field_width -= 2; 4887c203422SPierre Carrier else if (!is_zero) 4891c7a8e62SRasmus Villemoes field_width--; 4901da177e4SLinus Torvalds } 491b39a7340SDenis Vlasenko 492b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4931da177e4SLinus Torvalds i = 0; 494133fd9f5SDenys Vlasenko if (num < spec.base) 4953ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase; 496fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 497fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 498b39a7340SDenis Vlasenko int shift = 3; 4997b9186f5SAndré Goddard Rosa 5007b9186f5SAndré Goddard Rosa if (spec.base == 16) 5017b9186f5SAndré Goddard Rosa shift = 4; 502b39a7340SDenis Vlasenko do { 5033ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 504b39a7340SDenis Vlasenko num >>= shift; 505b39a7340SDenis Vlasenko } while (num); 5064277eeddSDenis Vlasenko } else { /* base 10 */ 5074277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 5084277eeddSDenis Vlasenko } 509b39a7340SDenis Vlasenko 510b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 5111c7a8e62SRasmus Villemoes if (i > precision) 5121c7a8e62SRasmus Villemoes precision = i; 513b39a7340SDenis Vlasenko /* leading space padding */ 5141c7a8e62SRasmus Villemoes field_width -= precision; 51551be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) { 5161c7a8e62SRasmus Villemoes while (--field_width >= 0) { 517f796937aSJeremy Fitzhardinge if (buf < end) 5181da177e4SLinus Torvalds *buf = ' '; 5191da177e4SLinus Torvalds ++buf; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds } 522b39a7340SDenis Vlasenko /* sign */ 5231da177e4SLinus Torvalds if (sign) { 524f796937aSJeremy Fitzhardinge if (buf < end) 5251da177e4SLinus Torvalds *buf = sign; 5261da177e4SLinus Torvalds ++buf; 5271da177e4SLinus Torvalds } 528b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 529b39a7340SDenis Vlasenko if (need_pfx) { 5307c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 531f796937aSJeremy Fitzhardinge if (buf < end) 5321da177e4SLinus Torvalds *buf = '0'; 5331da177e4SLinus Torvalds ++buf; 5347c203422SPierre Carrier } 535fef20d9cSFrederic Weisbecker if (spec.base == 16) { 536f796937aSJeremy Fitzhardinge if (buf < end) 5379b706aeeSDenys Vlasenko *buf = ('X' | locase); 5381da177e4SLinus Torvalds ++buf; 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds } 541b39a7340SDenis Vlasenko /* zero or space padding */ 542fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 543d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD); 544b886690dSAndy Shevchenko 5451c7a8e62SRasmus Villemoes while (--field_width >= 0) { 546f796937aSJeremy Fitzhardinge if (buf < end) 5471da177e4SLinus Torvalds *buf = c; 5481da177e4SLinus Torvalds ++buf; 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds } 551b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 5521c7a8e62SRasmus Villemoes while (i <= --precision) { 553f796937aSJeremy Fitzhardinge if (buf < end) 5541da177e4SLinus Torvalds *buf = '0'; 5551da177e4SLinus Torvalds ++buf; 5561da177e4SLinus Torvalds } 557b39a7340SDenis Vlasenko /* actual digits of result */ 558b39a7340SDenis Vlasenko while (--i >= 0) { 559f796937aSJeremy Fitzhardinge if (buf < end) 5601da177e4SLinus Torvalds *buf = tmp[i]; 5611da177e4SLinus Torvalds ++buf; 5621da177e4SLinus Torvalds } 563b39a7340SDenis Vlasenko /* trailing space padding */ 5641c7a8e62SRasmus Villemoes while (--field_width >= 0) { 565f796937aSJeremy Fitzhardinge if (buf < end) 5661da177e4SLinus Torvalds *buf = ' '; 5671da177e4SLinus Torvalds ++buf; 5681da177e4SLinus Torvalds } 5697b9186f5SAndré Goddard Rosa 5701da177e4SLinus Torvalds return buf; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5733cab1e71SAndy Shevchenko static noinline_for_stack 5743cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size) 5753cab1e71SAndy Shevchenko { 5763cab1e71SAndy Shevchenko struct printf_spec spec; 5773cab1e71SAndy Shevchenko 5783cab1e71SAndy Shevchenko spec.type = FORMAT_TYPE_PTR; 5793cab1e71SAndy Shevchenko spec.field_width = 2 + 2 * size; /* 0x + hex */ 5803cab1e71SAndy Shevchenko spec.flags = SPECIAL | SMALL | ZEROPAD; 5813cab1e71SAndy Shevchenko spec.base = 16; 5823cab1e71SAndy Shevchenko spec.precision = -1; 5833cab1e71SAndy Shevchenko 5843cab1e71SAndy Shevchenko return number(buf, end, num, spec); 5853cab1e71SAndy Shevchenko } 5863cab1e71SAndy Shevchenko 587cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces) 5884b6ccca7SAl Viro { 5894b6ccca7SAl Viro size_t size; 5904b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5914b6ccca7SAl Viro return; 5924b6ccca7SAl Viro size = end - buf; 5934b6ccca7SAl Viro if (size <= spaces) { 5944b6ccca7SAl Viro memset(buf, ' ', size); 5954b6ccca7SAl Viro return; 5964b6ccca7SAl Viro } 5974b6ccca7SAl Viro if (len) { 5984b6ccca7SAl Viro if (len > size - spaces) 5994b6ccca7SAl Viro len = size - spaces; 6004b6ccca7SAl Viro memmove(buf + spaces, buf, len); 6014b6ccca7SAl Viro } 6024b6ccca7SAl Viro memset(buf, ' ', spaces); 6034b6ccca7SAl Viro } 6044b6ccca7SAl Viro 605cfccde04SRasmus Villemoes /* 606cfccde04SRasmus Villemoes * Handle field width padding for a string. 607cfccde04SRasmus Villemoes * @buf: current buffer position 608cfccde04SRasmus Villemoes * @n: length of string 609cfccde04SRasmus Villemoes * @end: end of output buffer 610cfccde04SRasmus Villemoes * @spec: for field width and flags 611cfccde04SRasmus Villemoes * Returns: new buffer position after padding. 612cfccde04SRasmus Villemoes */ 613cfccde04SRasmus Villemoes static noinline_for_stack 614cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec) 615cfccde04SRasmus Villemoes { 616cfccde04SRasmus Villemoes unsigned spaces; 617cfccde04SRasmus Villemoes 618cfccde04SRasmus Villemoes if (likely(n >= spec.field_width)) 619cfccde04SRasmus Villemoes return buf; 620cfccde04SRasmus Villemoes /* we want to pad the sucker */ 621cfccde04SRasmus Villemoes spaces = spec.field_width - n; 622cfccde04SRasmus Villemoes if (!(spec.flags & LEFT)) { 623cfccde04SRasmus Villemoes move_right(buf - n, end, n, spaces); 624cfccde04SRasmus Villemoes return buf + spaces; 625cfccde04SRasmus Villemoes } 626cfccde04SRasmus Villemoes while (spaces--) { 627cfccde04SRasmus Villemoes if (buf < end) 628cfccde04SRasmus Villemoes *buf = ' '; 629cfccde04SRasmus Villemoes ++buf; 630cfccde04SRasmus Villemoes } 631cfccde04SRasmus Villemoes return buf; 632cfccde04SRasmus Villemoes } 633cfccde04SRasmus Villemoes 634d529ac41SPetr Mladek /* Handle string from a well known address. */ 635d529ac41SPetr Mladek static char *string_nocheck(char *buf, char *end, const char *s, 636d529ac41SPetr Mladek struct printf_spec spec) 63795508cfaSRasmus Villemoes { 63834fc8b90SRasmus Villemoes int len = 0; 639b314dd49SYoungmin Nam int lim = spec.precision; 64095508cfaSRasmus Villemoes 64134fc8b90SRasmus Villemoes while (lim--) { 64234fc8b90SRasmus Villemoes char c = *s++; 64334fc8b90SRasmus Villemoes if (!c) 64434fc8b90SRasmus Villemoes break; 64595508cfaSRasmus Villemoes if (buf < end) 64634fc8b90SRasmus Villemoes *buf = c; 64795508cfaSRasmus Villemoes ++buf; 64834fc8b90SRasmus Villemoes ++len; 64995508cfaSRasmus Villemoes } 65034fc8b90SRasmus Villemoes return widen_string(buf, len, end, spec); 65195508cfaSRasmus Villemoes } 65295508cfaSRasmus Villemoes 65357f5677eSRasmus Villemoes static char *err_ptr(char *buf, char *end, void *ptr, 65457f5677eSRasmus Villemoes struct printf_spec spec) 65557f5677eSRasmus Villemoes { 65657f5677eSRasmus Villemoes int err = PTR_ERR(ptr); 65757f5677eSRasmus Villemoes const char *sym = errname(err); 65857f5677eSRasmus Villemoes 65957f5677eSRasmus Villemoes if (sym) 66057f5677eSRasmus Villemoes return string_nocheck(buf, end, sym, spec); 66157f5677eSRasmus Villemoes 66257f5677eSRasmus Villemoes /* 66357f5677eSRasmus Villemoes * Somebody passed ERR_PTR(-1234) or some other non-existing 66457f5677eSRasmus Villemoes * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to 66557f5677eSRasmus Villemoes * printing it as its decimal representation. 66657f5677eSRasmus Villemoes */ 66757f5677eSRasmus Villemoes spec.flags |= SIGN; 66857f5677eSRasmus Villemoes spec.base = 10; 66957f5677eSRasmus Villemoes return number(buf, end, err, spec); 67057f5677eSRasmus Villemoes } 67157f5677eSRasmus Villemoes 672c8c3b584SPetr Mladek /* Be careful: error messages must fit into the given buffer. */ 673c8c3b584SPetr Mladek static char *error_string(char *buf, char *end, const char *s, 674c8c3b584SPetr Mladek struct printf_spec spec) 675c8c3b584SPetr Mladek { 676c8c3b584SPetr Mladek /* 677c8c3b584SPetr Mladek * Hard limit to avoid a completely insane messages. It actually 678c8c3b584SPetr Mladek * works pretty well because most error messages are in 679c8c3b584SPetr Mladek * the many pointer format modifiers. 680c8c3b584SPetr Mladek */ 681c8c3b584SPetr Mladek if (spec.precision == -1) 682c8c3b584SPetr Mladek spec.precision = 2 * sizeof(void *); 683c8c3b584SPetr Mladek 684c8c3b584SPetr Mladek return string_nocheck(buf, end, s, spec); 685c8c3b584SPetr Mladek } 686c8c3b584SPetr Mladek 6873e5903ebSPetr Mladek /* 6882ac5a3bfSPetr Mladek * Do not call any complex external code here. Nested printk()/vsprintf() 6892ac5a3bfSPetr Mladek * might cause infinite loops. Failures might break printk() and would 6902ac5a3bfSPetr Mladek * be hard to debug. 6913e5903ebSPetr Mladek */ 6923e5903ebSPetr Mladek static const char *check_pointer_msg(const void *ptr) 6933e5903ebSPetr Mladek { 6943e5903ebSPetr Mladek if (!ptr) 6953e5903ebSPetr Mladek return "(null)"; 6963e5903ebSPetr Mladek 6972ac5a3bfSPetr Mladek if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr)) 6983e5903ebSPetr Mladek return "(efault)"; 6993e5903ebSPetr Mladek 7003e5903ebSPetr Mladek return NULL; 7013e5903ebSPetr Mladek } 7023e5903ebSPetr Mladek 7033e5903ebSPetr Mladek static int check_pointer(char **buf, char *end, const void *ptr, 7043e5903ebSPetr Mladek struct printf_spec spec) 7053e5903ebSPetr Mladek { 7063e5903ebSPetr Mladek const char *err_msg; 7073e5903ebSPetr Mladek 7083e5903ebSPetr Mladek err_msg = check_pointer_msg(ptr); 7093e5903ebSPetr Mladek if (err_msg) { 710c8c3b584SPetr Mladek *buf = error_string(*buf, end, err_msg, spec); 7113e5903ebSPetr Mladek return -EFAULT; 7123e5903ebSPetr Mladek } 7133e5903ebSPetr Mladek 7143e5903ebSPetr Mladek return 0; 7153e5903ebSPetr Mladek } 7163e5903ebSPetr Mladek 71795508cfaSRasmus Villemoes static noinline_for_stack 718d529ac41SPetr Mladek char *string(char *buf, char *end, const char *s, 719d529ac41SPetr Mladek struct printf_spec spec) 720d529ac41SPetr Mladek { 7213e5903ebSPetr Mladek if (check_pointer(&buf, end, s, spec)) 7223e5903ebSPetr Mladek return buf; 723d529ac41SPetr Mladek 724d529ac41SPetr Mladek return string_nocheck(buf, end, s, spec); 725d529ac41SPetr Mladek } 726d529ac41SPetr Mladek 727ce9d3eceSYueHaibing static char *pointer_string(char *buf, char *end, 728ce9d3eceSYueHaibing const void *ptr, 7299073dac1SGeert Uytterhoeven struct printf_spec spec) 7309073dac1SGeert Uytterhoeven { 7319073dac1SGeert Uytterhoeven spec.base = 16; 7329073dac1SGeert Uytterhoeven spec.flags |= SMALL; 7339073dac1SGeert Uytterhoeven if (spec.field_width == -1) { 7349073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr); 7359073dac1SGeert Uytterhoeven spec.flags |= ZEROPAD; 7369073dac1SGeert Uytterhoeven } 7379073dac1SGeert Uytterhoeven 7389073dac1SGeert Uytterhoeven return number(buf, end, (unsigned long int)ptr, spec); 7399073dac1SGeert Uytterhoeven } 7409073dac1SGeert Uytterhoeven 7419073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */ 7429073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init; 7439073dac1SGeert Uytterhoeven 7449073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str) 7459073dac1SGeert Uytterhoeven { 7469073dac1SGeert Uytterhoeven debug_boot_weak_hash = 1; 7479073dac1SGeert Uytterhoeven pr_info("debug_boot_weak_hash enabled\n"); 7489073dac1SGeert Uytterhoeven return 0; 7499073dac1SGeert Uytterhoeven } 7509073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); 7519073dac1SGeert Uytterhoeven 7529073dac1SGeert Uytterhoeven static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key); 7539073dac1SGeert Uytterhoeven static siphash_key_t ptr_key __read_mostly; 7549073dac1SGeert Uytterhoeven 7559073dac1SGeert Uytterhoeven static void enable_ptr_key_workfn(struct work_struct *work) 7569073dac1SGeert Uytterhoeven { 7579073dac1SGeert Uytterhoeven get_random_bytes(&ptr_key, sizeof(ptr_key)); 7589073dac1SGeert Uytterhoeven /* Needs to run from preemptible context */ 7599073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 7609073dac1SGeert Uytterhoeven } 7619073dac1SGeert Uytterhoeven 7629073dac1SGeert Uytterhoeven static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn); 7639073dac1SGeert Uytterhoeven 7649073dac1SGeert Uytterhoeven static void fill_random_ptr_key(struct random_ready_callback *unused) 7659073dac1SGeert Uytterhoeven { 7669073dac1SGeert Uytterhoeven /* This may be in an interrupt handler. */ 7679073dac1SGeert Uytterhoeven queue_work(system_unbound_wq, &enable_ptr_key_work); 7689073dac1SGeert Uytterhoeven } 7699073dac1SGeert Uytterhoeven 7709073dac1SGeert Uytterhoeven static struct random_ready_callback random_ready = { 7719073dac1SGeert Uytterhoeven .func = fill_random_ptr_key 7729073dac1SGeert Uytterhoeven }; 7739073dac1SGeert Uytterhoeven 7749073dac1SGeert Uytterhoeven static int __init initialize_ptr_random(void) 7759073dac1SGeert Uytterhoeven { 7769073dac1SGeert Uytterhoeven int key_size = sizeof(ptr_key); 7779073dac1SGeert Uytterhoeven int ret; 7789073dac1SGeert Uytterhoeven 7799073dac1SGeert Uytterhoeven /* Use hw RNG if available. */ 7809073dac1SGeert Uytterhoeven if (get_random_bytes_arch(&ptr_key, key_size) == key_size) { 7819073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 7829073dac1SGeert Uytterhoeven return 0; 7839073dac1SGeert Uytterhoeven } 7849073dac1SGeert Uytterhoeven 7859073dac1SGeert Uytterhoeven ret = add_random_ready_callback(&random_ready); 7869073dac1SGeert Uytterhoeven if (!ret) { 7879073dac1SGeert Uytterhoeven return 0; 7889073dac1SGeert Uytterhoeven } else if (ret == -EALREADY) { 7899073dac1SGeert Uytterhoeven /* This is in preemptible context */ 7909073dac1SGeert Uytterhoeven enable_ptr_key_workfn(&enable_ptr_key_work); 7919073dac1SGeert Uytterhoeven return 0; 7929073dac1SGeert Uytterhoeven } 7939073dac1SGeert Uytterhoeven 7949073dac1SGeert Uytterhoeven return ret; 7959073dac1SGeert Uytterhoeven } 7969073dac1SGeert Uytterhoeven early_initcall(initialize_ptr_random); 7979073dac1SGeert Uytterhoeven 7989073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */ 799e4dcad20SJoel Fernandes (Google) static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 8009073dac1SGeert Uytterhoeven { 8019073dac1SGeert Uytterhoeven unsigned long hashval; 8029073dac1SGeert Uytterhoeven 803e4dcad20SJoel Fernandes (Google) if (static_branch_unlikely(¬_filled_random_ptr_key)) 804e4dcad20SJoel Fernandes (Google) return -EAGAIN; 8059073dac1SGeert Uytterhoeven 8069073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT 8079073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); 8089073dac1SGeert Uytterhoeven /* 8099073dac1SGeert Uytterhoeven * Mask off the first 32 bits, this makes explicit that we have 8109073dac1SGeert Uytterhoeven * modified the address (and 32 bits is plenty for a unique ID). 8119073dac1SGeert Uytterhoeven */ 8129073dac1SGeert Uytterhoeven hashval = hashval & 0xffffffff; 8139073dac1SGeert Uytterhoeven #else 8149073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); 8159073dac1SGeert Uytterhoeven #endif 816e4dcad20SJoel Fernandes (Google) *hashval_out = hashval; 817e4dcad20SJoel Fernandes (Google) return 0; 818e4dcad20SJoel Fernandes (Google) } 819e4dcad20SJoel Fernandes (Google) 820e4dcad20SJoel Fernandes (Google) int ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 821e4dcad20SJoel Fernandes (Google) { 822e4dcad20SJoel Fernandes (Google) return __ptr_to_hashval(ptr, hashval_out); 823e4dcad20SJoel Fernandes (Google) } 824e4dcad20SJoel Fernandes (Google) 825e4dcad20SJoel Fernandes (Google) static char *ptr_to_id(char *buf, char *end, const void *ptr, 826e4dcad20SJoel Fernandes (Google) struct printf_spec spec) 827e4dcad20SJoel Fernandes (Google) { 828e4dcad20SJoel Fernandes (Google) const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; 829e4dcad20SJoel Fernandes (Google) unsigned long hashval; 830e4dcad20SJoel Fernandes (Google) int ret; 831e4dcad20SJoel Fernandes (Google) 8327bd57fbcSIlya Dryomov /* 8337bd57fbcSIlya Dryomov * Print the real pointer value for NULL and error pointers, 8347bd57fbcSIlya Dryomov * as they are not actual addresses. 8357bd57fbcSIlya Dryomov */ 8367bd57fbcSIlya Dryomov if (IS_ERR_OR_NULL(ptr)) 8377bd57fbcSIlya Dryomov return pointer_string(buf, end, ptr, spec); 8387bd57fbcSIlya Dryomov 839e4dcad20SJoel Fernandes (Google) /* When debugging early boot use non-cryptographically secure hash. */ 840e4dcad20SJoel Fernandes (Google) if (unlikely(debug_boot_weak_hash)) { 841e4dcad20SJoel Fernandes (Google) hashval = hash_long((unsigned long)ptr, 32); 842e4dcad20SJoel Fernandes (Google) return pointer_string(buf, end, (const void *)hashval, spec); 843e4dcad20SJoel Fernandes (Google) } 844e4dcad20SJoel Fernandes (Google) 845e4dcad20SJoel Fernandes (Google) ret = __ptr_to_hashval(ptr, &hashval); 846e4dcad20SJoel Fernandes (Google) if (ret) { 847e4dcad20SJoel Fernandes (Google) spec.field_width = 2 * sizeof(ptr); 848e4dcad20SJoel Fernandes (Google) /* string length must be less than default_width */ 849e4dcad20SJoel Fernandes (Google) return error_string(buf, end, str, spec); 850e4dcad20SJoel Fernandes (Google) } 851e4dcad20SJoel Fernandes (Google) 8529073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec); 8539073dac1SGeert Uytterhoeven } 8549073dac1SGeert Uytterhoeven 855*84842911SChristophe Leroy static char *default_pointer(char *buf, char *end, const void *ptr, 856*84842911SChristophe Leroy struct printf_spec spec) 857*84842911SChristophe Leroy { 858*84842911SChristophe Leroy /* 859*84842911SChristophe Leroy * default is to _not_ leak addresses, so hash before printing, 860*84842911SChristophe Leroy * unless no_hash_pointers is specified on the command line. 861*84842911SChristophe Leroy */ 862*84842911SChristophe Leroy if (unlikely(no_hash_pointers)) 863*84842911SChristophe Leroy return pointer_string(buf, end, ptr, spec); 864*84842911SChristophe Leroy 865*84842911SChristophe Leroy return ptr_to_id(buf, end, ptr, spec); 866*84842911SChristophe Leroy } 867*84842911SChristophe Leroy 8686eea242fSPetr Mladek int kptr_restrict __read_mostly; 8696eea242fSPetr Mladek 8706eea242fSPetr Mladek static noinline_for_stack 8716eea242fSPetr Mladek char *restricted_pointer(char *buf, char *end, const void *ptr, 8726eea242fSPetr Mladek struct printf_spec spec) 8736eea242fSPetr Mladek { 8746eea242fSPetr Mladek switch (kptr_restrict) { 8756eea242fSPetr Mladek case 0: 8761ac2f978SPetr Mladek /* Handle as %p, hash and do _not_ leak addresses. */ 877*84842911SChristophe Leroy return default_pointer(buf, end, ptr, spec); 8786eea242fSPetr Mladek case 1: { 8796eea242fSPetr Mladek const struct cred *cred; 8806eea242fSPetr Mladek 8816eea242fSPetr Mladek /* 8826eea242fSPetr Mladek * kptr_restrict==1 cannot be used in IRQ context 8836eea242fSPetr Mladek * because its test for CAP_SYSLOG would be meaningless. 8846eea242fSPetr Mladek */ 8856eea242fSPetr Mladek if (in_irq() || in_serving_softirq() || in_nmi()) { 8866eea242fSPetr Mladek if (spec.field_width == -1) 8876eea242fSPetr Mladek spec.field_width = 2 * sizeof(ptr); 888c8c3b584SPetr Mladek return error_string(buf, end, "pK-error", spec); 8896eea242fSPetr Mladek } 8906eea242fSPetr Mladek 8916eea242fSPetr Mladek /* 8926eea242fSPetr Mladek * Only print the real pointer value if the current 8936eea242fSPetr Mladek * process has CAP_SYSLOG and is running with the 8946eea242fSPetr Mladek * same credentials it started with. This is because 8956eea242fSPetr Mladek * access to files is checked at open() time, but %pK 8966eea242fSPetr Mladek * checks permission at read() time. We don't want to 8976eea242fSPetr Mladek * leak pointer values if a binary opens a file using 8986eea242fSPetr Mladek * %pK and then elevates privileges before reading it. 8996eea242fSPetr Mladek */ 9006eea242fSPetr Mladek cred = current_cred(); 9016eea242fSPetr Mladek if (!has_capability_noaudit(current, CAP_SYSLOG) || 9026eea242fSPetr Mladek !uid_eq(cred->euid, cred->uid) || 9036eea242fSPetr Mladek !gid_eq(cred->egid, cred->gid)) 9046eea242fSPetr Mladek ptr = NULL; 9056eea242fSPetr Mladek break; 9066eea242fSPetr Mladek } 9076eea242fSPetr Mladek case 2: 9086eea242fSPetr Mladek default: 9096eea242fSPetr Mladek /* Always print 0's for %pK */ 9106eea242fSPetr Mladek ptr = NULL; 9116eea242fSPetr Mladek break; 9126eea242fSPetr Mladek } 9136eea242fSPetr Mladek 9146eea242fSPetr Mladek return pointer_string(buf, end, ptr, spec); 9156eea242fSPetr Mladek } 9166eea242fSPetr Mladek 9179073dac1SGeert Uytterhoeven static noinline_for_stack 9184b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 9194b6ccca7SAl Viro const char *fmt) 9204b6ccca7SAl Viro { 9214b6ccca7SAl Viro const char *array[4], *s; 9224b6ccca7SAl Viro const struct dentry *p; 9234b6ccca7SAl Viro int depth; 9244b6ccca7SAl Viro int i, n; 9254b6ccca7SAl Viro 9264b6ccca7SAl Viro switch (fmt[1]) { 9274b6ccca7SAl Viro case '2': case '3': case '4': 9284b6ccca7SAl Viro depth = fmt[1] - '0'; 9294b6ccca7SAl Viro break; 9304b6ccca7SAl Viro default: 9314b6ccca7SAl Viro depth = 1; 9324b6ccca7SAl Viro } 9334b6ccca7SAl Viro 9344b6ccca7SAl Viro rcu_read_lock(); 9354b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 9363e5903ebSPetr Mladek if (check_pointer(&buf, end, d, spec)) { 9373e5903ebSPetr Mladek rcu_read_unlock(); 9383e5903ebSPetr Mladek return buf; 9393e5903ebSPetr Mladek } 9403e5903ebSPetr Mladek 9416aa7de05SMark Rutland p = READ_ONCE(d->d_parent); 9426aa7de05SMark Rutland array[i] = READ_ONCE(d->d_name.name); 9434b6ccca7SAl Viro if (p == d) { 9444b6ccca7SAl Viro if (i) 9454b6ccca7SAl Viro array[i] = ""; 9464b6ccca7SAl Viro i++; 9474b6ccca7SAl Viro break; 9484b6ccca7SAl Viro } 9494b6ccca7SAl Viro } 9504b6ccca7SAl Viro s = array[--i]; 9514b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 9524b6ccca7SAl Viro char c = *s++; 9534b6ccca7SAl Viro if (!c) { 9544b6ccca7SAl Viro if (!i) 9554b6ccca7SAl Viro break; 9564b6ccca7SAl Viro c = '/'; 9574b6ccca7SAl Viro s = array[--i]; 9584b6ccca7SAl Viro } 9594b6ccca7SAl Viro if (buf < end) 9604b6ccca7SAl Viro *buf = c; 9614b6ccca7SAl Viro } 9624b6ccca7SAl Viro rcu_read_unlock(); 963cfccde04SRasmus Villemoes return widen_string(buf, n, end, spec); 9644b6ccca7SAl Viro } 9654b6ccca7SAl Viro 96636594b31SJia He static noinline_for_stack 96736594b31SJia He char *file_dentry_name(char *buf, char *end, const struct file *f, 96836594b31SJia He struct printf_spec spec, const char *fmt) 96936594b31SJia He { 97036594b31SJia He if (check_pointer(&buf, end, f, spec)) 97136594b31SJia He return buf; 97236594b31SJia He 97336594b31SJia He return dentry_name(buf, end, f->f_path.dentry, spec, fmt); 97436594b31SJia He } 9751031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 9761031bc58SDmitry Monakhov static noinline_for_stack 9771031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev, 9781031bc58SDmitry Monakhov struct printf_spec spec, const char *fmt) 9791031bc58SDmitry Monakhov { 9803e5903ebSPetr Mladek struct gendisk *hd; 9811031bc58SDmitry Monakhov 9823e5903ebSPetr Mladek if (check_pointer(&buf, end, bdev, spec)) 9833e5903ebSPetr Mladek return buf; 9843e5903ebSPetr Mladek 9853e5903ebSPetr Mladek hd = bdev->bd_disk; 9861031bc58SDmitry Monakhov buf = string(buf, end, hd->disk_name, spec); 987700cd59dSChristoph Hellwig if (bdev->bd_partno) { 9881031bc58SDmitry Monakhov if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { 9891031bc58SDmitry Monakhov if (buf < end) 9901031bc58SDmitry Monakhov *buf = 'p'; 9911031bc58SDmitry Monakhov buf++; 9921031bc58SDmitry Monakhov } 993700cd59dSChristoph Hellwig buf = number(buf, end, bdev->bd_partno, spec); 9941031bc58SDmitry Monakhov } 9951031bc58SDmitry Monakhov return buf; 9961031bc58SDmitry Monakhov } 9971031bc58SDmitry Monakhov #endif 9981031bc58SDmitry Monakhov 999cf3b429bSJoe Perches static noinline_for_stack 1000cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 1001b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 10020fe1ef24SLinus Torvalds { 1003b0d33c2bSJoe Perches unsigned long value; 10040fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 10050fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 1006b0d33c2bSJoe Perches #endif 1007b0d33c2bSJoe Perches 1008b0d33c2bSJoe Perches if (fmt[1] == 'R') 1009b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 1010b0d33c2bSJoe Perches value = (unsigned long)ptr; 1011b0d33c2bSJoe Perches 1012b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 10139294523eSStephen Boyd if (*fmt == 'B' && fmt[1] == 'b') 10149294523eSStephen Boyd sprint_backtrace_build_id(sym, value); 10159294523eSStephen Boyd else if (*fmt == 'B') 10160f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 10179294523eSStephen Boyd else if (*fmt == 'S' && (fmt[1] == 'b' || (fmt[1] == 'R' && fmt[2] == 'b'))) 10189294523eSStephen Boyd sprint_symbol_build_id(sym, value); 10199af77064SSakari Ailus else if (*fmt != 's') 10200fe1ef24SLinus Torvalds sprint_symbol(sym, value); 10210c8b946eSFrederic Weisbecker else 10224796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 10237b9186f5SAndré Goddard Rosa 1024d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 10250fe1ef24SLinus Torvalds #else 10263cab1e71SAndy Shevchenko return special_hex_number(buf, end, value, sizeof(void *)); 10270fe1ef24SLinus Torvalds #endif 10280fe1ef24SLinus Torvalds } 10290fe1ef24SLinus Torvalds 1030abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = { 1031abd4fe62SAndy Shevchenko .field_width = -1, 1032abd4fe62SAndy Shevchenko .precision = -1, 1033abd4fe62SAndy Shevchenko }; 1034abd4fe62SAndy Shevchenko 103554433973SAndy Shevchenko static const struct printf_spec default_flag_spec = { 103654433973SAndy Shevchenko .base = 16, 103754433973SAndy Shevchenko .precision = -1, 103854433973SAndy Shevchenko .flags = SPECIAL | SMALL, 103954433973SAndy Shevchenko }; 104054433973SAndy Shevchenko 1041ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = { 1042ce0b4910SAndy Shevchenko .base = 10, 1043ce0b4910SAndy Shevchenko .precision = -1, 1044ce0b4910SAndy Shevchenko }; 1045ce0b4910SAndy Shevchenko 10464d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = { 10474d42c447SAndy Shevchenko .base = 10, 10484d42c447SAndy Shevchenko .field_width = 2, 10494d42c447SAndy Shevchenko .precision = -1, 10504d42c447SAndy Shevchenko .flags = ZEROPAD, 10514d42c447SAndy Shevchenko }; 10524d42c447SAndy Shevchenko 10534d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = { 10544d42c447SAndy Shevchenko .base = 10, 10554d42c447SAndy Shevchenko .field_width = 4, 10564d42c447SAndy Shevchenko .precision = -1, 10574d42c447SAndy Shevchenko .flags = ZEROPAD, 10584d42c447SAndy Shevchenko }; 10594d42c447SAndy Shevchenko 1060cf3b429bSJoe Perches static noinline_for_stack 1061cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 1062fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 1063332d2e78SLinus Torvalds { 1064332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 106528405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 1066332d2e78SLinus Torvalds #endif 1067332d2e78SLinus Torvalds 1068332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 106928405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 1070332d2e78SLinus Torvalds #endif 10714da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 1072fef20d9cSFrederic Weisbecker .base = 16, 10734da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 1074fef20d9cSFrederic Weisbecker .precision = -1, 1075fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 1076fef20d9cSFrederic Weisbecker }; 10774da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 10784da0b66cSBjorn Helgaas .base = 16, 10794da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 10804da0b66cSBjorn Helgaas .precision = -1, 10814da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 10824da0b66cSBjorn Helgaas }; 10830f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 10840f4050c7SBjorn Helgaas .base = 16, 10850f4050c7SBjorn Helgaas .field_width = 2, 10860f4050c7SBjorn Helgaas .precision = -1, 10870f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 10880f4050c7SBjorn Helgaas }; 10894da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 1090fd95541eSBjorn Helgaas .field_width = -1, 1091fd95541eSBjorn Helgaas .precision = 10, 1092fd95541eSBjorn Helgaas .flags = LEFT, 1093fd95541eSBjorn Helgaas }; 1094c7dabef8SBjorn Helgaas 1095c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 1096c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 1097c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 1098c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 10999d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 1100c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 1101c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 1102c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 1103c7dabef8SBjorn Helgaas 1104332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 1105c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 11064da0b66cSBjorn Helgaas const struct printf_spec *specp; 1107332d2e78SLinus Torvalds 11083e5903ebSPetr Mladek if (check_pointer(&buf, end, res, spec)) 11093e5903ebSPetr Mladek return buf; 11103e5903ebSPetr Mladek 1111332d2e78SLinus Torvalds *p++ = '['; 11124da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 1113d529ac41SPetr Mladek p = string_nocheck(p, pend, "io ", str_spec); 11144da0b66cSBjorn Helgaas specp = &io_spec; 11154da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 1116d529ac41SPetr Mladek p = string_nocheck(p, pend, "mem ", str_spec); 11174da0b66cSBjorn Helgaas specp = &mem_spec; 11184da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 1119d529ac41SPetr Mladek p = string_nocheck(p, pend, "irq ", str_spec); 1120ce0b4910SAndy Shevchenko specp = &default_dec_spec; 11214da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 1122d529ac41SPetr Mladek p = string_nocheck(p, pend, "dma ", str_spec); 1123ce0b4910SAndy Shevchenko specp = &default_dec_spec; 11240f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 1125d529ac41SPetr Mladek p = string_nocheck(p, pend, "bus ", str_spec); 11260f4050c7SBjorn Helgaas specp = &bus_spec; 11274da0b66cSBjorn Helgaas } else { 1128d529ac41SPetr Mladek p = string_nocheck(p, pend, "??? ", str_spec); 11294da0b66cSBjorn Helgaas specp = &mem_spec; 1130c7dabef8SBjorn Helgaas decode = 0; 1131fd95541eSBjorn Helgaas } 1132d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 1133d529ac41SPetr Mladek p = string_nocheck(p, pend, "size ", str_spec); 1134d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 1135d19cb803SBjorn Helgaas } else { 11364da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 1137c91d3376SBjorn Helgaas if (res->start != res->end) { 1138332d2e78SLinus Torvalds *p++ = '-'; 11394da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 1140c91d3376SBjorn Helgaas } 1141d19cb803SBjorn Helgaas } 1142c7dabef8SBjorn Helgaas if (decode) { 1143fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 1144d529ac41SPetr Mladek p = string_nocheck(p, pend, " 64bit", str_spec); 1145fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 1146d529ac41SPetr Mladek p = string_nocheck(p, pend, " pref", str_spec); 11479d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 1148d529ac41SPetr Mladek p = string_nocheck(p, pend, " window", str_spec); 1149fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 1150d529ac41SPetr Mladek p = string_nocheck(p, pend, " disabled", str_spec); 1151c7dabef8SBjorn Helgaas } else { 1152d529ac41SPetr Mladek p = string_nocheck(p, pend, " flags ", str_spec); 115354433973SAndy Shevchenko p = number(p, pend, res->flags, default_flag_spec); 1154fd95541eSBjorn Helgaas } 1155332d2e78SLinus Torvalds *p++ = ']'; 1156c7dabef8SBjorn Helgaas *p = '\0'; 1157332d2e78SLinus Torvalds 1158d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 1159332d2e78SLinus Torvalds } 1160332d2e78SLinus Torvalds 1161cf3b429bSJoe Perches static noinline_for_stack 116231550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 116331550a16SAndy Shevchenko const char *fmt) 116431550a16SAndy Shevchenko { 1165360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 116631550a16SAndy Shevchenko negative value, fallback to the default */ 116731550a16SAndy Shevchenko char separator; 116831550a16SAndy Shevchenko 116931550a16SAndy Shevchenko if (spec.field_width == 0) 117031550a16SAndy Shevchenko /* nothing to print */ 117131550a16SAndy Shevchenko return buf; 117231550a16SAndy Shevchenko 11733e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 11743e5903ebSPetr Mladek return buf; 117531550a16SAndy Shevchenko 117631550a16SAndy Shevchenko switch (fmt[1]) { 117731550a16SAndy Shevchenko case 'C': 117831550a16SAndy Shevchenko separator = ':'; 117931550a16SAndy Shevchenko break; 118031550a16SAndy Shevchenko case 'D': 118131550a16SAndy Shevchenko separator = '-'; 118231550a16SAndy Shevchenko break; 118331550a16SAndy Shevchenko case 'N': 118431550a16SAndy Shevchenko separator = 0; 118531550a16SAndy Shevchenko break; 118631550a16SAndy Shevchenko default: 118731550a16SAndy Shevchenko separator = ' '; 118831550a16SAndy Shevchenko break; 118931550a16SAndy Shevchenko } 119031550a16SAndy Shevchenko 119131550a16SAndy Shevchenko if (spec.field_width > 0) 119231550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 119331550a16SAndy Shevchenko 11949c98f235SRasmus Villemoes for (i = 0; i < len; ++i) { 11959c98f235SRasmus Villemoes if (buf < end) 11969c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]); 11979c98f235SRasmus Villemoes ++buf; 11989c98f235SRasmus Villemoes if (buf < end) 11999c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]); 12009c98f235SRasmus Villemoes ++buf; 120131550a16SAndy Shevchenko 12029c98f235SRasmus Villemoes if (separator && i != len - 1) { 12039c98f235SRasmus Villemoes if (buf < end) 12049c98f235SRasmus Villemoes *buf = separator; 12059c98f235SRasmus Villemoes ++buf; 12069c98f235SRasmus Villemoes } 120731550a16SAndy Shevchenko } 120831550a16SAndy Shevchenko 120931550a16SAndy Shevchenko return buf; 121031550a16SAndy Shevchenko } 121131550a16SAndy Shevchenko 121231550a16SAndy Shevchenko static noinline_for_stack 1213dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap, 1214dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1215dbc760bcSTejun Heo { 1216dbc760bcSTejun Heo const int CHUNKSZ = 32; 1217dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1218dbc760bcSTejun Heo int i, chunksz; 1219dbc760bcSTejun Heo bool first = true; 1220dbc760bcSTejun Heo 12213e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec)) 12223e5903ebSPetr Mladek return buf; 12233e5903ebSPetr Mladek 1224dbc760bcSTejun Heo /* reused to print numbers */ 1225dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 1226dbc760bcSTejun Heo 1227dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1); 1228dbc760bcSTejun Heo if (chunksz == 0) 1229dbc760bcSTejun Heo chunksz = CHUNKSZ; 1230dbc760bcSTejun Heo 1231dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 1232dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) { 1233dbc760bcSTejun Heo u32 chunkmask, val; 1234dbc760bcSTejun Heo int word, bit; 1235dbc760bcSTejun Heo 1236dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1); 1237dbc760bcSTejun Heo word = i / BITS_PER_LONG; 1238dbc760bcSTejun Heo bit = i % BITS_PER_LONG; 1239dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask; 1240dbc760bcSTejun Heo 1241dbc760bcSTejun Heo if (!first) { 1242dbc760bcSTejun Heo if (buf < end) 1243dbc760bcSTejun Heo *buf = ','; 1244dbc760bcSTejun Heo buf++; 1245dbc760bcSTejun Heo } 1246dbc760bcSTejun Heo first = false; 1247dbc760bcSTejun Heo 1248dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4); 1249dbc760bcSTejun Heo buf = number(buf, end, val, spec); 1250dbc760bcSTejun Heo 1251dbc760bcSTejun Heo chunksz = CHUNKSZ; 1252dbc760bcSTejun Heo } 1253dbc760bcSTejun Heo return buf; 1254dbc760bcSTejun Heo } 1255dbc760bcSTejun Heo 1256dbc760bcSTejun Heo static noinline_for_stack 1257dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, 1258dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1259dbc760bcSTejun Heo { 1260dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1261dbc760bcSTejun Heo /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 1262dbc760bcSTejun Heo int cur, rbot, rtop; 1263dbc760bcSTejun Heo bool first = true; 1264dbc760bcSTejun Heo 12653e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec)) 12663e5903ebSPetr Mladek return buf; 12673e5903ebSPetr Mladek 1268dbc760bcSTejun Heo rbot = cur = find_first_bit(bitmap, nr_bits); 1269dbc760bcSTejun Heo while (cur < nr_bits) { 1270dbc760bcSTejun Heo rtop = cur; 1271dbc760bcSTejun Heo cur = find_next_bit(bitmap, nr_bits, cur + 1); 1272dbc760bcSTejun Heo if (cur < nr_bits && cur <= rtop + 1) 1273dbc760bcSTejun Heo continue; 1274dbc760bcSTejun Heo 1275dbc760bcSTejun Heo if (!first) { 1276dbc760bcSTejun Heo if (buf < end) 1277dbc760bcSTejun Heo *buf = ','; 1278dbc760bcSTejun Heo buf++; 1279dbc760bcSTejun Heo } 1280dbc760bcSTejun Heo first = false; 1281dbc760bcSTejun Heo 1282ce0b4910SAndy Shevchenko buf = number(buf, end, rbot, default_dec_spec); 1283dbc760bcSTejun Heo if (rbot < rtop) { 1284dbc760bcSTejun Heo if (buf < end) 1285dbc760bcSTejun Heo *buf = '-'; 1286dbc760bcSTejun Heo buf++; 1287dbc760bcSTejun Heo 1288ce0b4910SAndy Shevchenko buf = number(buf, end, rtop, default_dec_spec); 1289dbc760bcSTejun Heo } 1290dbc760bcSTejun Heo 1291dbc760bcSTejun Heo rbot = cur; 1292dbc760bcSTejun Heo } 1293dbc760bcSTejun Heo return buf; 1294dbc760bcSTejun Heo } 1295dbc760bcSTejun Heo 1296dbc760bcSTejun Heo static noinline_for_stack 1297cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 12988a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 1299dd45c9cfSHarvey Harrison { 13008a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 1301dd45c9cfSHarvey Harrison char *p = mac_addr; 1302dd45c9cfSHarvey Harrison int i; 1303bc7259a2SJoe Perches char separator; 130476597ff9SAndrei Emeltchenko bool reversed = false; 1305bc7259a2SJoe Perches 13063e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 13073e5903ebSPetr Mladek return buf; 13083e5903ebSPetr Mladek 130976597ff9SAndrei Emeltchenko switch (fmt[1]) { 131076597ff9SAndrei Emeltchenko case 'F': 1311bc7259a2SJoe Perches separator = '-'; 131276597ff9SAndrei Emeltchenko break; 131376597ff9SAndrei Emeltchenko 131476597ff9SAndrei Emeltchenko case 'R': 131576597ff9SAndrei Emeltchenko reversed = true; 13164c1ca831SNick Desaulniers fallthrough; 131776597ff9SAndrei Emeltchenko 131876597ff9SAndrei Emeltchenko default: 1319bc7259a2SJoe Perches separator = ':'; 132076597ff9SAndrei Emeltchenko break; 1321bc7259a2SJoe Perches } 1322dd45c9cfSHarvey Harrison 1323dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 132476597ff9SAndrei Emeltchenko if (reversed) 132576597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 132676597ff9SAndrei Emeltchenko else 132755036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 132876597ff9SAndrei Emeltchenko 13298a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 1330bc7259a2SJoe Perches *p++ = separator; 1331dd45c9cfSHarvey Harrison } 1332dd45c9cfSHarvey Harrison *p = '\0'; 1333dd45c9cfSHarvey Harrison 1334d529ac41SPetr Mladek return string_nocheck(buf, end, mac_addr, spec); 1335dd45c9cfSHarvey Harrison } 1336dd45c9cfSHarvey Harrison 1337cf3b429bSJoe Perches static noinline_for_stack 1338cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 1339689afa7dSHarvey Harrison { 1340689afa7dSHarvey Harrison int i; 13410159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 13420159f24eSJoe Perches int index; 13430159f24eSJoe Perches int step; 1344689afa7dSHarvey Harrison 13450159f24eSJoe Perches switch (fmt[2]) { 13460159f24eSJoe Perches case 'h': 13470159f24eSJoe Perches #ifdef __BIG_ENDIAN 13480159f24eSJoe Perches index = 0; 13490159f24eSJoe Perches step = 1; 13500159f24eSJoe Perches #else 13510159f24eSJoe Perches index = 3; 13520159f24eSJoe Perches step = -1; 13530159f24eSJoe Perches #endif 13540159f24eSJoe Perches break; 13550159f24eSJoe Perches case 'l': 13560159f24eSJoe Perches index = 3; 13570159f24eSJoe Perches step = -1; 13580159f24eSJoe Perches break; 13590159f24eSJoe Perches case 'n': 13600159f24eSJoe Perches case 'b': 13610159f24eSJoe Perches default: 13620159f24eSJoe Perches index = 0; 13630159f24eSJoe Perches step = 1; 13640159f24eSJoe Perches break; 13650159f24eSJoe Perches } 13668a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 13677c43d9a3SRasmus Villemoes char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 1368133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 13698a27f7c9SJoe Perches if (leading_zeros) { 13708a27f7c9SJoe Perches if (digits < 3) 13718a27f7c9SJoe Perches *p++ = '0'; 13728a27f7c9SJoe Perches if (digits < 2) 13738a27f7c9SJoe Perches *p++ = '0'; 13748a27f7c9SJoe Perches } 13758a27f7c9SJoe Perches /* reverse the digits in the quad */ 13768a27f7c9SJoe Perches while (digits--) 13778a27f7c9SJoe Perches *p++ = temp[digits]; 13788a27f7c9SJoe Perches if (i < 3) 13798a27f7c9SJoe Perches *p++ = '.'; 13800159f24eSJoe Perches index += step; 13818a27f7c9SJoe Perches } 13828a27f7c9SJoe Perches *p = '\0'; 13837b9186f5SAndré Goddard Rosa 13848a27f7c9SJoe Perches return p; 13858a27f7c9SJoe Perches } 13868a27f7c9SJoe Perches 1387cf3b429bSJoe Perches static noinline_for_stack 1388cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 13898a27f7c9SJoe Perches { 13907b9186f5SAndré Goddard Rosa int i, j, range; 13918a27f7c9SJoe Perches unsigned char zerolength[8]; 13928a27f7c9SJoe Perches int longest = 1; 13938a27f7c9SJoe Perches int colonpos = -1; 13948a27f7c9SJoe Perches u16 word; 13957b9186f5SAndré Goddard Rosa u8 hi, lo; 13968a27f7c9SJoe Perches bool needcolon = false; 1397eb78cd26SJoe Perches bool useIPv4; 1398eb78cd26SJoe Perches struct in6_addr in6; 1399eb78cd26SJoe Perches 1400eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 1401eb78cd26SJoe Perches 1402eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 14038a27f7c9SJoe Perches 14048a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 14058a27f7c9SJoe Perches 14068a27f7c9SJoe Perches if (useIPv4) 14078a27f7c9SJoe Perches range = 6; 14088a27f7c9SJoe Perches else 14098a27f7c9SJoe Perches range = 8; 14108a27f7c9SJoe Perches 14118a27f7c9SJoe Perches /* find position of longest 0 run */ 14128a27f7c9SJoe Perches for (i = 0; i < range; i++) { 14138a27f7c9SJoe Perches for (j = i; j < range; j++) { 1414eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 14158a27f7c9SJoe Perches break; 14168a27f7c9SJoe Perches zerolength[i]++; 14178a27f7c9SJoe Perches } 14188a27f7c9SJoe Perches } 14198a27f7c9SJoe Perches for (i = 0; i < range; i++) { 14208a27f7c9SJoe Perches if (zerolength[i] > longest) { 14218a27f7c9SJoe Perches longest = zerolength[i]; 14228a27f7c9SJoe Perches colonpos = i; 14238a27f7c9SJoe Perches } 14248a27f7c9SJoe Perches } 142529cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 142629cf519eSJoe Perches colonpos = -1; 14278a27f7c9SJoe Perches 14288a27f7c9SJoe Perches /* emit address */ 14298a27f7c9SJoe Perches for (i = 0; i < range; i++) { 14308a27f7c9SJoe Perches if (i == colonpos) { 14318a27f7c9SJoe Perches if (needcolon || i == 0) 14328a27f7c9SJoe Perches *p++ = ':'; 14338a27f7c9SJoe Perches *p++ = ':'; 14348a27f7c9SJoe Perches needcolon = false; 14358a27f7c9SJoe Perches i += longest - 1; 14368a27f7c9SJoe Perches continue; 14378a27f7c9SJoe Perches } 14388a27f7c9SJoe Perches if (needcolon) { 14398a27f7c9SJoe Perches *p++ = ':'; 14408a27f7c9SJoe Perches needcolon = false; 14418a27f7c9SJoe Perches } 14428a27f7c9SJoe Perches /* hex u16 without leading 0s */ 1443eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 14448a27f7c9SJoe Perches hi = word >> 8; 14458a27f7c9SJoe Perches lo = word & 0xff; 14468a27f7c9SJoe Perches if (hi) { 14478a27f7c9SJoe Perches if (hi > 0x0f) 144855036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 14498a27f7c9SJoe Perches else 14508a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 145155036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 14528a27f7c9SJoe Perches } 1453b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 145455036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 14558a27f7c9SJoe Perches else 14568a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 14578a27f7c9SJoe Perches needcolon = true; 14588a27f7c9SJoe Perches } 14598a27f7c9SJoe Perches 14608a27f7c9SJoe Perches if (useIPv4) { 14618a27f7c9SJoe Perches if (needcolon) 14628a27f7c9SJoe Perches *p++ = ':'; 14630159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 14648a27f7c9SJoe Perches } 14658a27f7c9SJoe Perches *p = '\0'; 14667b9186f5SAndré Goddard Rosa 14678a27f7c9SJoe Perches return p; 14688a27f7c9SJoe Perches } 14698a27f7c9SJoe Perches 1470cf3b429bSJoe Perches static noinline_for_stack 1471cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 14728a27f7c9SJoe Perches { 14738a27f7c9SJoe Perches int i; 14747b9186f5SAndré Goddard Rosa 1475689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 147655036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 147755036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 14788a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 1479689afa7dSHarvey Harrison *p++ = ':'; 1480689afa7dSHarvey Harrison } 1481689afa7dSHarvey Harrison *p = '\0'; 14827b9186f5SAndré Goddard Rosa 14838a27f7c9SJoe Perches return p; 14848a27f7c9SJoe Perches } 14858a27f7c9SJoe Perches 1486cf3b429bSJoe Perches static noinline_for_stack 1487cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 14888a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 14898a27f7c9SJoe Perches { 14908a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 14918a27f7c9SJoe Perches 14928a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 1493eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 14948a27f7c9SJoe Perches else 1495eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 1496689afa7dSHarvey Harrison 1497d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 1498689afa7dSHarvey Harrison } 1499689afa7dSHarvey Harrison 1500cf3b429bSJoe Perches static noinline_for_stack 1501cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 15028a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 15034aa99606SHarvey Harrison { 15048a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 15054aa99606SHarvey Harrison 15060159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 15074aa99606SHarvey Harrison 1508d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 15094aa99606SHarvey Harrison } 15104aa99606SHarvey Harrison 1511cf3b429bSJoe Perches static noinline_for_stack 151210679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 151310679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 151410679643SDaniel Borkmann { 151510679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 151610679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 151710679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 151810679643SDaniel Borkmann sizeof("%1234567890")]; 151910679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 152010679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 152110679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 152210679643SDaniel Borkmann u8 off = 0; 152310679643SDaniel Borkmann 152410679643SDaniel Borkmann fmt++; 152510679643SDaniel Borkmann while (isalpha(*++fmt)) { 152610679643SDaniel Borkmann switch (*fmt) { 152710679643SDaniel Borkmann case 'p': 152810679643SDaniel Borkmann have_p = true; 152910679643SDaniel Borkmann break; 153010679643SDaniel Borkmann case 'f': 153110679643SDaniel Borkmann have_f = true; 153210679643SDaniel Borkmann break; 153310679643SDaniel Borkmann case 's': 153410679643SDaniel Borkmann have_s = true; 153510679643SDaniel Borkmann break; 153610679643SDaniel Borkmann case 'c': 153710679643SDaniel Borkmann have_c = true; 153810679643SDaniel Borkmann break; 153910679643SDaniel Borkmann } 154010679643SDaniel Borkmann } 154110679643SDaniel Borkmann 154210679643SDaniel Borkmann if (have_p || have_s || have_f) { 154310679643SDaniel Borkmann *p = '['; 154410679643SDaniel Borkmann off = 1; 154510679643SDaniel Borkmann } 154610679643SDaniel Borkmann 154710679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 154810679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 154910679643SDaniel Borkmann else 155010679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 155110679643SDaniel Borkmann 155210679643SDaniel Borkmann if (have_p || have_s || have_f) 155310679643SDaniel Borkmann *p++ = ']'; 155410679643SDaniel Borkmann 155510679643SDaniel Borkmann if (have_p) { 155610679643SDaniel Borkmann *p++ = ':'; 155710679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 155810679643SDaniel Borkmann } 155910679643SDaniel Borkmann if (have_f) { 156010679643SDaniel Borkmann *p++ = '/'; 156110679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 156210679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 156310679643SDaniel Borkmann } 156410679643SDaniel Borkmann if (have_s) { 156510679643SDaniel Borkmann *p++ = '%'; 156610679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 156710679643SDaniel Borkmann } 156810679643SDaniel Borkmann *p = '\0'; 156910679643SDaniel Borkmann 1570d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 157110679643SDaniel Borkmann } 157210679643SDaniel Borkmann 157310679643SDaniel Borkmann static noinline_for_stack 157410679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 157510679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 157610679643SDaniel Borkmann { 157710679643SDaniel Borkmann bool have_p = false; 157810679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 157910679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 158010679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 158110679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 158210679643SDaniel Borkmann 158310679643SDaniel Borkmann fmt++; 158410679643SDaniel Borkmann while (isalpha(*++fmt)) { 158510679643SDaniel Borkmann switch (*fmt) { 158610679643SDaniel Borkmann case 'p': 158710679643SDaniel Borkmann have_p = true; 158810679643SDaniel Borkmann break; 158910679643SDaniel Borkmann case 'h': 159010679643SDaniel Borkmann case 'l': 159110679643SDaniel Borkmann case 'n': 159210679643SDaniel Borkmann case 'b': 159310679643SDaniel Borkmann fmt4[2] = *fmt; 159410679643SDaniel Borkmann break; 159510679643SDaniel Borkmann } 159610679643SDaniel Borkmann } 159710679643SDaniel Borkmann 159810679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 159910679643SDaniel Borkmann if (have_p) { 160010679643SDaniel Borkmann *p++ = ':'; 160110679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 160210679643SDaniel Borkmann } 160310679643SDaniel Borkmann *p = '\0'; 160410679643SDaniel Borkmann 1605d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 160610679643SDaniel Borkmann } 160710679643SDaniel Borkmann 160810679643SDaniel Borkmann static noinline_for_stack 1609f00cc102SPetr Mladek char *ip_addr_string(char *buf, char *end, const void *ptr, 1610f00cc102SPetr Mladek struct printf_spec spec, const char *fmt) 1611f00cc102SPetr Mladek { 16120b74d4d7SPetr Mladek char *err_fmt_msg; 16130b74d4d7SPetr Mladek 16143e5903ebSPetr Mladek if (check_pointer(&buf, end, ptr, spec)) 16153e5903ebSPetr Mladek return buf; 16163e5903ebSPetr Mladek 1617f00cc102SPetr Mladek switch (fmt[1]) { 1618f00cc102SPetr Mladek case '6': 1619f00cc102SPetr Mladek return ip6_addr_string(buf, end, ptr, spec, fmt); 1620f00cc102SPetr Mladek case '4': 1621f00cc102SPetr Mladek return ip4_addr_string(buf, end, ptr, spec, fmt); 1622f00cc102SPetr Mladek case 'S': { 1623f00cc102SPetr Mladek const union { 1624f00cc102SPetr Mladek struct sockaddr raw; 1625f00cc102SPetr Mladek struct sockaddr_in v4; 1626f00cc102SPetr Mladek struct sockaddr_in6 v6; 1627f00cc102SPetr Mladek } *sa = ptr; 1628f00cc102SPetr Mladek 1629f00cc102SPetr Mladek switch (sa->raw.sa_family) { 1630f00cc102SPetr Mladek case AF_INET: 1631f00cc102SPetr Mladek return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 1632f00cc102SPetr Mladek case AF_INET6: 1633f00cc102SPetr Mladek return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 1634f00cc102SPetr Mladek default: 1635c8c3b584SPetr Mladek return error_string(buf, end, "(einval)", spec); 1636f00cc102SPetr Mladek }} 1637f00cc102SPetr Mladek } 1638f00cc102SPetr Mladek 16390b74d4d7SPetr Mladek err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)"; 1640c8c3b584SPetr Mladek return error_string(buf, end, err_fmt_msg, spec); 1641f00cc102SPetr Mladek } 1642f00cc102SPetr Mladek 1643f00cc102SPetr Mladek static noinline_for_stack 164471dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 164571dca95dSAndy Shevchenko const char *fmt) 164671dca95dSAndy Shevchenko { 164771dca95dSAndy Shevchenko bool found = true; 164871dca95dSAndy Shevchenko int count = 1; 164971dca95dSAndy Shevchenko unsigned int flags = 0; 165071dca95dSAndy Shevchenko int len; 165171dca95dSAndy Shevchenko 165271dca95dSAndy Shevchenko if (spec.field_width == 0) 165371dca95dSAndy Shevchenko return buf; /* nothing to print */ 165471dca95dSAndy Shevchenko 16553e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 16563e5903ebSPetr Mladek return buf; 165771dca95dSAndy Shevchenko 165871dca95dSAndy Shevchenko do { 165971dca95dSAndy Shevchenko switch (fmt[count++]) { 166071dca95dSAndy Shevchenko case 'a': 166171dca95dSAndy Shevchenko flags |= ESCAPE_ANY; 166271dca95dSAndy Shevchenko break; 166371dca95dSAndy Shevchenko case 'c': 166471dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL; 166571dca95dSAndy Shevchenko break; 166671dca95dSAndy Shevchenko case 'h': 166771dca95dSAndy Shevchenko flags |= ESCAPE_HEX; 166871dca95dSAndy Shevchenko break; 166971dca95dSAndy Shevchenko case 'n': 167071dca95dSAndy Shevchenko flags |= ESCAPE_NULL; 167171dca95dSAndy Shevchenko break; 167271dca95dSAndy Shevchenko case 'o': 167371dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL; 167471dca95dSAndy Shevchenko break; 167571dca95dSAndy Shevchenko case 'p': 167671dca95dSAndy Shevchenko flags |= ESCAPE_NP; 167771dca95dSAndy Shevchenko break; 167871dca95dSAndy Shevchenko case 's': 167971dca95dSAndy Shevchenko flags |= ESCAPE_SPACE; 168071dca95dSAndy Shevchenko break; 168171dca95dSAndy Shevchenko default: 168271dca95dSAndy Shevchenko found = false; 168371dca95dSAndy Shevchenko break; 168471dca95dSAndy Shevchenko } 168571dca95dSAndy Shevchenko } while (found); 168671dca95dSAndy Shevchenko 168771dca95dSAndy Shevchenko if (!flags) 168871dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP; 168971dca95dSAndy Shevchenko 169071dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width; 169171dca95dSAndy Shevchenko 169241416f23SRasmus Villemoes /* 169341416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to 169441416f23SRasmus Villemoes * the given buffer, and returns the total size of the output 169541416f23SRasmus Villemoes * had the buffer been big enough. 169641416f23SRasmus Villemoes */ 169741416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 169871dca95dSAndy Shevchenko 169971dca95dSAndy Shevchenko return buf; 170071dca95dSAndy Shevchenko } 170171dca95dSAndy Shevchenko 17023e5903ebSPetr Mladek static char *va_format(char *buf, char *end, struct va_format *va_fmt, 17033e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 170445c3e93dSPetr Mladek { 170545c3e93dSPetr Mladek va_list va; 170645c3e93dSPetr Mladek 17073e5903ebSPetr Mladek if (check_pointer(&buf, end, va_fmt, spec)) 17083e5903ebSPetr Mladek return buf; 17093e5903ebSPetr Mladek 171045c3e93dSPetr Mladek va_copy(va, *va_fmt->va); 171145c3e93dSPetr Mladek buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va); 171245c3e93dSPetr Mladek va_end(va); 171345c3e93dSPetr Mladek 171445c3e93dSPetr Mladek return buf; 171545c3e93dSPetr Mladek } 171645c3e93dSPetr Mladek 171771dca95dSAndy Shevchenko static noinline_for_stack 1718cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 17199ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 17209ac6e44eSJoe Perches { 17212b1b0d66SAndy Shevchenko char uuid[UUID_STRING_LEN + 1]; 17229ac6e44eSJoe Perches char *p = uuid; 17239ac6e44eSJoe Perches int i; 1724f9727a17SChristoph Hellwig const u8 *index = uuid_index; 17259ac6e44eSJoe Perches bool uc = false; 17269ac6e44eSJoe Perches 17273e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 17283e5903ebSPetr Mladek return buf; 17293e5903ebSPetr Mladek 17309ac6e44eSJoe Perches switch (*(++fmt)) { 17319ac6e44eSJoe Perches case 'L': 1732df561f66SGustavo A. R. Silva uc = true; 17334c1ca831SNick Desaulniers fallthrough; 17349ac6e44eSJoe Perches case 'l': 1735f9727a17SChristoph Hellwig index = guid_index; 17369ac6e44eSJoe Perches break; 17379ac6e44eSJoe Perches case 'B': 17389ac6e44eSJoe Perches uc = true; 17399ac6e44eSJoe Perches break; 17409ac6e44eSJoe Perches } 17419ac6e44eSJoe Perches 17429ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 1743aa4ea1c3SAndy Shevchenko if (uc) 1744aa4ea1c3SAndy Shevchenko p = hex_byte_pack_upper(p, addr[index[i]]); 1745aa4ea1c3SAndy Shevchenko else 174655036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 17479ac6e44eSJoe Perches switch (i) { 17489ac6e44eSJoe Perches case 3: 17499ac6e44eSJoe Perches case 5: 17509ac6e44eSJoe Perches case 7: 17519ac6e44eSJoe Perches case 9: 17529ac6e44eSJoe Perches *p++ = '-'; 17539ac6e44eSJoe Perches break; 17549ac6e44eSJoe Perches } 17559ac6e44eSJoe Perches } 17569ac6e44eSJoe Perches 17579ac6e44eSJoe Perches *p = 0; 17589ac6e44eSJoe Perches 1759d529ac41SPetr Mladek return string_nocheck(buf, end, uuid, spec); 17609ac6e44eSJoe Perches } 17619ac6e44eSJoe Perches 17625b17aecfSAndy Shevchenko static noinline_for_stack 1763431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr, 1764431bca24SGeert Uytterhoeven struct printf_spec spec, const char *fmt) 1765c8f44affSMichał Mirosław { 17665b17aecfSAndy Shevchenko unsigned long long num; 17675b17aecfSAndy Shevchenko int size; 17685b17aecfSAndy Shevchenko 17693e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 17703e5903ebSPetr Mladek return buf; 17713e5903ebSPetr Mladek 17725b17aecfSAndy Shevchenko switch (fmt[1]) { 17735b17aecfSAndy Shevchenko case 'F': 17745b17aecfSAndy Shevchenko num = *(const netdev_features_t *)addr; 17755b17aecfSAndy Shevchenko size = sizeof(netdev_features_t); 17765b17aecfSAndy Shevchenko break; 17775b17aecfSAndy Shevchenko default: 1778c8c3b584SPetr Mladek return error_string(buf, end, "(%pN?)", spec); 17795b17aecfSAndy Shevchenko } 1780c8f44affSMichał Mirosław 17813cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1782c8f44affSMichał Mirosław } 1783c8f44affSMichał Mirosław 1784aaf07621SJoe Perches static noinline_for_stack 1785af612e43SSakari Ailus char *fourcc_string(char *buf, char *end, const u32 *fourcc, 1786af612e43SSakari Ailus struct printf_spec spec, const char *fmt) 1787af612e43SSakari Ailus { 1788af612e43SSakari Ailus char output[sizeof("0123 little-endian (0x01234567)")]; 1789af612e43SSakari Ailus char *p = output; 1790af612e43SSakari Ailus unsigned int i; 1791af612e43SSakari Ailus u32 val; 1792af612e43SSakari Ailus 1793af612e43SSakari Ailus if (fmt[1] != 'c' || fmt[2] != 'c') 1794af612e43SSakari Ailus return error_string(buf, end, "(%p4?)", spec); 1795af612e43SSakari Ailus 1796af612e43SSakari Ailus if (check_pointer(&buf, end, fourcc, spec)) 1797af612e43SSakari Ailus return buf; 1798af612e43SSakari Ailus 1799af612e43SSakari Ailus val = *fourcc & ~BIT(31); 1800af612e43SSakari Ailus 1801af612e43SSakari Ailus for (i = 0; i < sizeof(*fourcc); i++) { 1802af612e43SSakari Ailus unsigned char c = val >> (i * 8); 1803af612e43SSakari Ailus 1804af612e43SSakari Ailus /* Print non-control ASCII characters as-is, dot otherwise */ 1805af612e43SSakari Ailus *p++ = isascii(c) && isprint(c) ? c : '.'; 1806af612e43SSakari Ailus } 1807af612e43SSakari Ailus 1808af612e43SSakari Ailus strcpy(p, *fourcc & BIT(31) ? " big-endian" : " little-endian"); 1809af612e43SSakari Ailus p += strlen(p); 1810af612e43SSakari Ailus 1811af612e43SSakari Ailus *p++ = ' '; 1812af612e43SSakari Ailus *p++ = '('; 1813af612e43SSakari Ailus p = special_hex_number(p, output + sizeof(output) - 2, *fourcc, sizeof(u32)); 1814af612e43SSakari Ailus *p++ = ')'; 1815af612e43SSakari Ailus *p = '\0'; 1816af612e43SSakari Ailus 1817af612e43SSakari Ailus return string(buf, end, output, spec); 1818af612e43SSakari Ailus } 1819af612e43SSakari Ailus 1820af612e43SSakari Ailus static noinline_for_stack 18213e5903ebSPetr Mladek char *address_val(char *buf, char *end, const void *addr, 18223e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 1823aaf07621SJoe Perches { 1824aaf07621SJoe Perches unsigned long long num; 18253cab1e71SAndy Shevchenko int size; 1826aaf07621SJoe Perches 18273e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 18283e5903ebSPetr Mladek return buf; 18293e5903ebSPetr Mladek 1830aaf07621SJoe Perches switch (fmt[1]) { 1831aaf07621SJoe Perches case 'd': 1832aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 18333cab1e71SAndy Shevchenko size = sizeof(dma_addr_t); 1834aaf07621SJoe Perches break; 1835aaf07621SJoe Perches case 'p': 1836aaf07621SJoe Perches default: 1837aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 18383cab1e71SAndy Shevchenko size = sizeof(phys_addr_t); 1839aaf07621SJoe Perches break; 1840aaf07621SJoe Perches } 1841aaf07621SJoe Perches 18423cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1843aaf07621SJoe Perches } 1844aaf07621SJoe Perches 1845900cca29SGeert Uytterhoeven static noinline_for_stack 18464d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) 18474d42c447SAndy Shevchenko { 18484d42c447SAndy Shevchenko int year = tm->tm_year + (r ? 0 : 1900); 18494d42c447SAndy Shevchenko int mon = tm->tm_mon + (r ? 0 : 1); 18504d42c447SAndy Shevchenko 18514d42c447SAndy Shevchenko buf = number(buf, end, year, default_dec04_spec); 18524d42c447SAndy Shevchenko if (buf < end) 18534d42c447SAndy Shevchenko *buf = '-'; 18544d42c447SAndy Shevchenko buf++; 18554d42c447SAndy Shevchenko 18564d42c447SAndy Shevchenko buf = number(buf, end, mon, default_dec02_spec); 18574d42c447SAndy Shevchenko if (buf < end) 18584d42c447SAndy Shevchenko *buf = '-'; 18594d42c447SAndy Shevchenko buf++; 18604d42c447SAndy Shevchenko 18614d42c447SAndy Shevchenko return number(buf, end, tm->tm_mday, default_dec02_spec); 18624d42c447SAndy Shevchenko } 18634d42c447SAndy Shevchenko 18644d42c447SAndy Shevchenko static noinline_for_stack 18654d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) 18664d42c447SAndy Shevchenko { 18674d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_hour, default_dec02_spec); 18684d42c447SAndy Shevchenko if (buf < end) 18694d42c447SAndy Shevchenko *buf = ':'; 18704d42c447SAndy Shevchenko buf++; 18714d42c447SAndy Shevchenko 18724d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_min, default_dec02_spec); 18734d42c447SAndy Shevchenko if (buf < end) 18744d42c447SAndy Shevchenko *buf = ':'; 18754d42c447SAndy Shevchenko buf++; 18764d42c447SAndy Shevchenko 18774d42c447SAndy Shevchenko return number(buf, end, tm->tm_sec, default_dec02_spec); 18784d42c447SAndy Shevchenko } 18794d42c447SAndy Shevchenko 18804d42c447SAndy Shevchenko static noinline_for_stack 18813e5903ebSPetr Mladek char *rtc_str(char *buf, char *end, const struct rtc_time *tm, 18823e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 18834d42c447SAndy Shevchenko { 18844d42c447SAndy Shevchenko bool have_t = true, have_d = true; 188520bc8c1eSAndy Shevchenko bool raw = false, iso8601_separator = true; 188620bc8c1eSAndy Shevchenko bool found = true; 18874d42c447SAndy Shevchenko int count = 2; 18884d42c447SAndy Shevchenko 18893e5903ebSPetr Mladek if (check_pointer(&buf, end, tm, spec)) 18903e5903ebSPetr Mladek return buf; 18913e5903ebSPetr Mladek 18924d42c447SAndy Shevchenko switch (fmt[count]) { 18934d42c447SAndy Shevchenko case 'd': 18944d42c447SAndy Shevchenko have_t = false; 18954d42c447SAndy Shevchenko count++; 18964d42c447SAndy Shevchenko break; 18974d42c447SAndy Shevchenko case 't': 18984d42c447SAndy Shevchenko have_d = false; 18994d42c447SAndy Shevchenko count++; 19004d42c447SAndy Shevchenko break; 19014d42c447SAndy Shevchenko } 19024d42c447SAndy Shevchenko 190320bc8c1eSAndy Shevchenko do { 190420bc8c1eSAndy Shevchenko switch (fmt[count++]) { 190520bc8c1eSAndy Shevchenko case 'r': 190620bc8c1eSAndy Shevchenko raw = true; 190720bc8c1eSAndy Shevchenko break; 190820bc8c1eSAndy Shevchenko case 's': 190920bc8c1eSAndy Shevchenko iso8601_separator = false; 191020bc8c1eSAndy Shevchenko break; 191120bc8c1eSAndy Shevchenko default: 191220bc8c1eSAndy Shevchenko found = false; 191320bc8c1eSAndy Shevchenko break; 191420bc8c1eSAndy Shevchenko } 191520bc8c1eSAndy Shevchenko } while (found); 19164d42c447SAndy Shevchenko 19174d42c447SAndy Shevchenko if (have_d) 19184d42c447SAndy Shevchenko buf = date_str(buf, end, tm, raw); 19194d42c447SAndy Shevchenko if (have_d && have_t) { 19204d42c447SAndy Shevchenko if (buf < end) 192120bc8c1eSAndy Shevchenko *buf = iso8601_separator ? 'T' : ' '; 19224d42c447SAndy Shevchenko buf++; 19234d42c447SAndy Shevchenko } 19244d42c447SAndy Shevchenko if (have_t) 19254d42c447SAndy Shevchenko buf = time_str(buf, end, tm, raw); 19264d42c447SAndy Shevchenko 19274d42c447SAndy Shevchenko return buf; 19284d42c447SAndy Shevchenko } 19294d42c447SAndy Shevchenko 19304d42c447SAndy Shevchenko static noinline_for_stack 19317daac5b2SAndy Shevchenko char *time64_str(char *buf, char *end, const time64_t time, 19327daac5b2SAndy Shevchenko struct printf_spec spec, const char *fmt) 19337daac5b2SAndy Shevchenko { 19347daac5b2SAndy Shevchenko struct rtc_time rtc_time; 19357daac5b2SAndy Shevchenko struct tm tm; 19367daac5b2SAndy Shevchenko 19377daac5b2SAndy Shevchenko time64_to_tm(time, 0, &tm); 19387daac5b2SAndy Shevchenko 19397daac5b2SAndy Shevchenko rtc_time.tm_sec = tm.tm_sec; 19407daac5b2SAndy Shevchenko rtc_time.tm_min = tm.tm_min; 19417daac5b2SAndy Shevchenko rtc_time.tm_hour = tm.tm_hour; 19427daac5b2SAndy Shevchenko rtc_time.tm_mday = tm.tm_mday; 19437daac5b2SAndy Shevchenko rtc_time.tm_mon = tm.tm_mon; 19447daac5b2SAndy Shevchenko rtc_time.tm_year = tm.tm_year; 19457daac5b2SAndy Shevchenko rtc_time.tm_wday = tm.tm_wday; 19467daac5b2SAndy Shevchenko rtc_time.tm_yday = tm.tm_yday; 19477daac5b2SAndy Shevchenko 19487daac5b2SAndy Shevchenko rtc_time.tm_isdst = 0; 19497daac5b2SAndy Shevchenko 19507daac5b2SAndy Shevchenko return rtc_str(buf, end, &rtc_time, spec, fmt); 19517daac5b2SAndy Shevchenko } 19527daac5b2SAndy Shevchenko 19537daac5b2SAndy Shevchenko static noinline_for_stack 19544d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, 19554d42c447SAndy Shevchenko const char *fmt) 19564d42c447SAndy Shevchenko { 19574d42c447SAndy Shevchenko switch (fmt[1]) { 19584d42c447SAndy Shevchenko case 'R': 19593e5903ebSPetr Mladek return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); 19607daac5b2SAndy Shevchenko case 'T': 19617daac5b2SAndy Shevchenko return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); 19624d42c447SAndy Shevchenko default: 19637daac5b2SAndy Shevchenko return error_string(buf, end, "(%pt?)", spec); 19644d42c447SAndy Shevchenko } 19654d42c447SAndy Shevchenko } 19664d42c447SAndy Shevchenko 19674d42c447SAndy Shevchenko static noinline_for_stack 1968900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1969900cca29SGeert Uytterhoeven const char *fmt) 1970900cca29SGeert Uytterhoeven { 19710b74d4d7SPetr Mladek if (!IS_ENABLED(CONFIG_HAVE_CLK)) 1972c8c3b584SPetr Mladek return error_string(buf, end, "(%pC?)", spec); 19730b74d4d7SPetr Mladek 19743e5903ebSPetr Mladek if (check_pointer(&buf, end, clk, spec)) 19753e5903ebSPetr Mladek return buf; 1976900cca29SGeert Uytterhoeven 1977900cca29SGeert Uytterhoeven switch (fmt[1]) { 1978900cca29SGeert Uytterhoeven case 'n': 1979900cca29SGeert Uytterhoeven default: 1980900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1981900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1982900cca29SGeert Uytterhoeven #else 19834ca96aa9SGeert Uytterhoeven return ptr_to_id(buf, end, clk, spec); 1984900cca29SGeert Uytterhoeven #endif 1985900cca29SGeert Uytterhoeven } 1986900cca29SGeert Uytterhoeven } 1987900cca29SGeert Uytterhoeven 1988edf14cdbSVlastimil Babka static 1989edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags, 1990edf14cdbSVlastimil Babka const struct trace_print_flags *names) 1991edf14cdbSVlastimil Babka { 1992edf14cdbSVlastimil Babka unsigned long mask; 1993edf14cdbSVlastimil Babka 1994edf14cdbSVlastimil Babka for ( ; flags && names->name; names++) { 1995edf14cdbSVlastimil Babka mask = names->mask; 1996edf14cdbSVlastimil Babka if ((flags & mask) != mask) 1997edf14cdbSVlastimil Babka continue; 1998edf14cdbSVlastimil Babka 1999abd4fe62SAndy Shevchenko buf = string(buf, end, names->name, default_str_spec); 2000edf14cdbSVlastimil Babka 2001edf14cdbSVlastimil Babka flags &= ~mask; 2002edf14cdbSVlastimil Babka if (flags) { 2003edf14cdbSVlastimil Babka if (buf < end) 2004edf14cdbSVlastimil Babka *buf = '|'; 2005edf14cdbSVlastimil Babka buf++; 2006edf14cdbSVlastimil Babka } 2007edf14cdbSVlastimil Babka } 2008edf14cdbSVlastimil Babka 2009edf14cdbSVlastimil Babka if (flags) 201054433973SAndy Shevchenko buf = number(buf, end, flags, default_flag_spec); 2011edf14cdbSVlastimil Babka 2012edf14cdbSVlastimil Babka return buf; 2013edf14cdbSVlastimil Babka } 2014edf14cdbSVlastimil Babka 2015c244297aSYafang Shao struct page_flags_fields { 2016c244297aSYafang Shao int width; 2017c244297aSYafang Shao int shift; 2018c244297aSYafang Shao int mask; 2019c244297aSYafang Shao const struct printf_spec *spec; 2020c244297aSYafang Shao const char *name; 2021c244297aSYafang Shao }; 2022c244297aSYafang Shao 2023c244297aSYafang Shao static const struct page_flags_fields pff[] = { 2024c244297aSYafang Shao {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK, 2025c244297aSYafang Shao &default_dec_spec, "section"}, 2026c244297aSYafang Shao {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK, 2027c244297aSYafang Shao &default_dec_spec, "node"}, 2028c244297aSYafang Shao {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK, 2029c244297aSYafang Shao &default_dec_spec, "zone"}, 2030c244297aSYafang Shao {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK, 2031c244297aSYafang Shao &default_flag_spec, "lastcpupid"}, 2032c244297aSYafang Shao {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK, 2033c244297aSYafang Shao &default_flag_spec, "kasantag"}, 2034c244297aSYafang Shao }; 2035c244297aSYafang Shao 2036c244297aSYafang Shao static 2037c244297aSYafang Shao char *format_page_flags(char *buf, char *end, unsigned long flags) 2038c244297aSYafang Shao { 203941c961b9SMuchun Song unsigned long main_flags = flags & PAGEFLAGS_MASK; 2040c244297aSYafang Shao bool append = false; 2041c244297aSYafang Shao int i; 2042c244297aSYafang Shao 204323efd080SMatthew Wilcox (Oracle) buf = number(buf, end, flags, default_flag_spec); 204423efd080SMatthew Wilcox (Oracle) if (buf < end) 204523efd080SMatthew Wilcox (Oracle) *buf = '('; 204623efd080SMatthew Wilcox (Oracle) buf++; 204723efd080SMatthew Wilcox (Oracle) 2048c244297aSYafang Shao /* Page flags from the main area. */ 2049c244297aSYafang Shao if (main_flags) { 2050c244297aSYafang Shao buf = format_flags(buf, end, main_flags, pageflag_names); 2051c244297aSYafang Shao append = true; 2052c244297aSYafang Shao } 2053c244297aSYafang Shao 2054c244297aSYafang Shao /* Page flags from the fields area */ 2055c244297aSYafang Shao for (i = 0; i < ARRAY_SIZE(pff); i++) { 2056c244297aSYafang Shao /* Skip undefined fields. */ 2057c244297aSYafang Shao if (!pff[i].width) 2058c244297aSYafang Shao continue; 2059c244297aSYafang Shao 2060c244297aSYafang Shao /* Format: Flag Name + '=' (equals sign) + Number + '|' (separator) */ 2061c244297aSYafang Shao if (append) { 2062c244297aSYafang Shao if (buf < end) 2063c244297aSYafang Shao *buf = '|'; 2064c244297aSYafang Shao buf++; 2065c244297aSYafang Shao } 2066c244297aSYafang Shao 2067c244297aSYafang Shao buf = string(buf, end, pff[i].name, default_str_spec); 2068c244297aSYafang Shao if (buf < end) 2069c244297aSYafang Shao *buf = '='; 2070c244297aSYafang Shao buf++; 2071c244297aSYafang Shao buf = number(buf, end, (flags >> pff[i].shift) & pff[i].mask, 2072c244297aSYafang Shao *pff[i].spec); 2073c244297aSYafang Shao 2074c244297aSYafang Shao append = true; 2075c244297aSYafang Shao } 207623efd080SMatthew Wilcox (Oracle) if (buf < end) 207723efd080SMatthew Wilcox (Oracle) *buf = ')'; 207823efd080SMatthew Wilcox (Oracle) buf++; 2079c244297aSYafang Shao 2080c244297aSYafang Shao return buf; 2081c244297aSYafang Shao } 2082c244297aSYafang Shao 2083edf14cdbSVlastimil Babka static noinline_for_stack 20840b74d4d7SPetr Mladek char *flags_string(char *buf, char *end, void *flags_ptr, 20850b74d4d7SPetr Mladek struct printf_spec spec, const char *fmt) 2086edf14cdbSVlastimil Babka { 2087edf14cdbSVlastimil Babka unsigned long flags; 2088edf14cdbSVlastimil Babka const struct trace_print_flags *names; 2089edf14cdbSVlastimil Babka 20903e5903ebSPetr Mladek if (check_pointer(&buf, end, flags_ptr, spec)) 20913e5903ebSPetr Mladek return buf; 20923e5903ebSPetr Mladek 2093edf14cdbSVlastimil Babka switch (fmt[1]) { 2094edf14cdbSVlastimil Babka case 'p': 2095c244297aSYafang Shao return format_page_flags(buf, end, *(unsigned long *)flags_ptr); 2096edf14cdbSVlastimil Babka case 'v': 2097edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 2098edf14cdbSVlastimil Babka names = vmaflag_names; 2099edf14cdbSVlastimil Babka break; 2100edf14cdbSVlastimil Babka case 'g': 210130d497a0SAndy Shevchenko flags = (__force unsigned long)(*(gfp_t *)flags_ptr); 2102edf14cdbSVlastimil Babka names = gfpflag_names; 2103edf14cdbSVlastimil Babka break; 2104edf14cdbSVlastimil Babka default: 2105c8c3b584SPetr Mladek return error_string(buf, end, "(%pG?)", spec); 2106edf14cdbSVlastimil Babka } 2107edf14cdbSVlastimil Babka 2108edf14cdbSVlastimil Babka return format_flags(buf, end, flags, names); 2109edf14cdbSVlastimil Babka } 2110edf14cdbSVlastimil Babka 2111ce4fecf1SPantelis Antoniou static noinline_for_stack 2112a92eb762SSakari Ailus char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, 2113a92eb762SSakari Ailus char *end) 2114ce4fecf1SPantelis Antoniou { 2115ce4fecf1SPantelis Antoniou int depth; 2116ce4fecf1SPantelis Antoniou 2117a92eb762SSakari Ailus /* Loop starting from the root node to the current node. */ 2118a92eb762SSakari Ailus for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { 2119a92eb762SSakari Ailus struct fwnode_handle *__fwnode = 2120a92eb762SSakari Ailus fwnode_get_nth_parent(fwnode, depth); 2121ce4fecf1SPantelis Antoniou 2122a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name_prefix(__fwnode), 2123abd4fe62SAndy Shevchenko default_str_spec); 2124a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name(__fwnode), 2125a92eb762SSakari Ailus default_str_spec); 2126a92eb762SSakari Ailus 2127a92eb762SSakari Ailus fwnode_handle_put(__fwnode); 2128ce4fecf1SPantelis Antoniou } 2129a92eb762SSakari Ailus 2130ce4fecf1SPantelis Antoniou return buf; 2131ce4fecf1SPantelis Antoniou } 2132ce4fecf1SPantelis Antoniou 2133ce4fecf1SPantelis Antoniou static noinline_for_stack 2134ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn, 2135ce4fecf1SPantelis Antoniou struct printf_spec spec, const char *fmt) 2136ce4fecf1SPantelis Antoniou { 2137ce4fecf1SPantelis Antoniou char tbuf[sizeof("xxxx") + 1]; 2138ce4fecf1SPantelis Antoniou const char *p; 2139ce4fecf1SPantelis Antoniou int ret; 2140ce4fecf1SPantelis Antoniou char *buf_start = buf; 2141ce4fecf1SPantelis Antoniou struct property *prop; 2142ce4fecf1SPantelis Antoniou bool has_mult, pass; 2143ce4fecf1SPantelis Antoniou 2144ce4fecf1SPantelis Antoniou struct printf_spec str_spec = spec; 2145ce4fecf1SPantelis Antoniou str_spec.field_width = -1; 2146ce4fecf1SPantelis Antoniou 214783abc5a7SSakari Ailus if (fmt[0] != 'F') 214883abc5a7SSakari Ailus return error_string(buf, end, "(%pO?)", spec); 214983abc5a7SSakari Ailus 2150ce4fecf1SPantelis Antoniou if (!IS_ENABLED(CONFIG_OF)) 2151c8c3b584SPetr Mladek return error_string(buf, end, "(%pOF?)", spec); 2152ce4fecf1SPantelis Antoniou 21533e5903ebSPetr Mladek if (check_pointer(&buf, end, dn, spec)) 21543e5903ebSPetr Mladek return buf; 2155ce4fecf1SPantelis Antoniou 2156ce4fecf1SPantelis Antoniou /* simple case without anything any more format specifiers */ 2157ce4fecf1SPantelis Antoniou fmt++; 2158ce4fecf1SPantelis Antoniou if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 2159ce4fecf1SPantelis Antoniou fmt = "f"; 2160ce4fecf1SPantelis Antoniou 2161ce4fecf1SPantelis Antoniou for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 21626d0a70a2SRob Herring int precision; 2163ce4fecf1SPantelis Antoniou if (pass) { 2164ce4fecf1SPantelis Antoniou if (buf < end) 2165ce4fecf1SPantelis Antoniou *buf = ':'; 2166ce4fecf1SPantelis Antoniou buf++; 2167ce4fecf1SPantelis Antoniou } 2168ce4fecf1SPantelis Antoniou 2169ce4fecf1SPantelis Antoniou switch (*fmt) { 2170ce4fecf1SPantelis Antoniou case 'f': /* full_name */ 2171a92eb762SSakari Ailus buf = fwnode_full_name_string(of_fwnode_handle(dn), buf, 2172a92eb762SSakari Ailus end); 2173ce4fecf1SPantelis Antoniou break; 2174ce4fecf1SPantelis Antoniou case 'n': /* name */ 2175a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn)); 21766d0a70a2SRob Herring precision = str_spec.precision; 21776d0a70a2SRob Herring str_spec.precision = strchrnul(p, '@') - p; 21786d0a70a2SRob Herring buf = string(buf, end, p, str_spec); 21796d0a70a2SRob Herring str_spec.precision = precision; 2180ce4fecf1SPantelis Antoniou break; 2181ce4fecf1SPantelis Antoniou case 'p': /* phandle */ 218209ceb8d7SAndy Shevchenko buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec); 2183ce4fecf1SPantelis Antoniou break; 2184ce4fecf1SPantelis Antoniou case 'P': /* path-spec */ 2185a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn)); 2186ce4fecf1SPantelis Antoniou if (!p[1]) 2187ce4fecf1SPantelis Antoniou p = "/"; 2188ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2189ce4fecf1SPantelis Antoniou break; 2190ce4fecf1SPantelis Antoniou case 'F': /* flags */ 2191ce4fecf1SPantelis Antoniou tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 2192ce4fecf1SPantelis Antoniou tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 2193ce4fecf1SPantelis Antoniou tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 2194ce4fecf1SPantelis Antoniou tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 2195ce4fecf1SPantelis Antoniou tbuf[4] = 0; 2196d529ac41SPetr Mladek buf = string_nocheck(buf, end, tbuf, str_spec); 2197ce4fecf1SPantelis Antoniou break; 2198ce4fecf1SPantelis Antoniou case 'c': /* major compatible string */ 2199ce4fecf1SPantelis Antoniou ret = of_property_read_string(dn, "compatible", &p); 2200ce4fecf1SPantelis Antoniou if (!ret) 2201ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2202ce4fecf1SPantelis Antoniou break; 2203ce4fecf1SPantelis Antoniou case 'C': /* full compatible string */ 2204ce4fecf1SPantelis Antoniou has_mult = false; 2205ce4fecf1SPantelis Antoniou of_property_for_each_string(dn, "compatible", prop, p) { 2206ce4fecf1SPantelis Antoniou if (has_mult) 2207d529ac41SPetr Mladek buf = string_nocheck(buf, end, ",", str_spec); 2208d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 2209ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2210d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 2211ce4fecf1SPantelis Antoniou 2212ce4fecf1SPantelis Antoniou has_mult = true; 2213ce4fecf1SPantelis Antoniou } 2214ce4fecf1SPantelis Antoniou break; 2215ce4fecf1SPantelis Antoniou default: 2216ce4fecf1SPantelis Antoniou break; 2217ce4fecf1SPantelis Antoniou } 2218ce4fecf1SPantelis Antoniou } 2219ce4fecf1SPantelis Antoniou 2220ce4fecf1SPantelis Antoniou return widen_string(buf, buf - buf_start, end, spec); 2221ce4fecf1SPantelis Antoniou } 2222ce4fecf1SPantelis Antoniou 22233bd32d6aSSakari Ailus static noinline_for_stack 22243bd32d6aSSakari Ailus char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, 2225798cc27aSPetr Mladek struct printf_spec spec, const char *fmt) 2226798cc27aSPetr Mladek { 22273bd32d6aSSakari Ailus struct printf_spec str_spec = spec; 22283bd32d6aSSakari Ailus char *buf_start = buf; 22293bd32d6aSSakari Ailus 22303bd32d6aSSakari Ailus str_spec.field_width = -1; 22313bd32d6aSSakari Ailus 22323bd32d6aSSakari Ailus if (*fmt != 'w') 22333bd32d6aSSakari Ailus return error_string(buf, end, "(%pf?)", spec); 22343bd32d6aSSakari Ailus 22353bd32d6aSSakari Ailus if (check_pointer(&buf, end, fwnode, spec)) 22363bd32d6aSSakari Ailus return buf; 22373bd32d6aSSakari Ailus 22383bd32d6aSSakari Ailus fmt++; 22393bd32d6aSSakari Ailus 22403bd32d6aSSakari Ailus switch (*fmt) { 22413bd32d6aSSakari Ailus case 'P': /* name */ 22423bd32d6aSSakari Ailus buf = string(buf, end, fwnode_get_name(fwnode), str_spec); 22433bd32d6aSSakari Ailus break; 22443bd32d6aSSakari Ailus case 'f': /* full_name */ 22453bd32d6aSSakari Ailus default: 22463bd32d6aSSakari Ailus buf = fwnode_full_name_string(fwnode, buf, end); 22473bd32d6aSSakari Ailus break; 2248798cc27aSPetr Mladek } 2249798cc27aSPetr Mladek 22503bd32d6aSSakari Ailus return widen_string(buf, buf - buf_start, end, spec); 2251798cc27aSPetr Mladek } 2252798cc27aSPetr Mladek 225379270291SStephen Boyd int __init no_hash_pointers_enable(char *str) 22545ead723aSTimur Tabi { 22559f961c2eSMarco Elver if (no_hash_pointers) 22569f961c2eSMarco Elver return 0; 22579f961c2eSMarco Elver 22585ead723aSTimur Tabi no_hash_pointers = true; 22595ead723aSTimur Tabi 22605ead723aSTimur Tabi pr_warn("**********************************************************\n"); 22615ead723aSTimur Tabi pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 22625ead723aSTimur Tabi pr_warn("** **\n"); 22635ead723aSTimur Tabi pr_warn("** This system shows unhashed kernel memory addresses **\n"); 22645ead723aSTimur Tabi pr_warn("** via the console, logs, and other interfaces. This **\n"); 22655ead723aSTimur Tabi pr_warn("** might reduce the security of your system. **\n"); 22665ead723aSTimur Tabi pr_warn("** **\n"); 22675ead723aSTimur Tabi pr_warn("** If you see this message and you are not debugging **\n"); 22685ead723aSTimur Tabi pr_warn("** the kernel, report this immediately to your system **\n"); 22695ead723aSTimur Tabi pr_warn("** administrator! **\n"); 22705ead723aSTimur Tabi pr_warn("** **\n"); 22715ead723aSTimur Tabi pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 22725ead723aSTimur Tabi pr_warn("**********************************************************\n"); 22735ead723aSTimur Tabi 22745ead723aSTimur Tabi return 0; 22755ead723aSTimur Tabi } 22765ead723aSTimur Tabi early_param("no_hash_pointers", no_hash_pointers_enable); 22775ead723aSTimur Tabi 22784d8a743cSLinus Torvalds /* 22794d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 22804d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 22814d8a743cSLinus Torvalds * specifiers. 22824d8a743cSLinus Torvalds * 22830b523769SJoe Perches * Please update scripts/checkpatch.pl when adding/removing conversion 22840b523769SJoe Perches * characters. (Search for "check for vsprintf extension"). 22850b523769SJoe Perches * 2286332d2e78SLinus Torvalds * Right now we handle: 22870fe1ef24SLinus Torvalds * 2288cdb7e52dSSergey Senozhatsky * - 'S' For symbolic direct pointers (or function descriptors) with offset 2289cdb7e52dSSergey Senozhatsky * - 's' For symbolic direct pointers (or function descriptors) without offset 22909af77064SSakari Ailus * - '[Ss]R' as above with __builtin_extract_return_addr() translation 22919294523eSStephen Boyd * - 'S[R]b' as above with module build ID (for use in backtraces) 22921586c5aeSSakari Ailus * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of 22931586c5aeSSakari Ailus * %ps and %pS. Be careful when re-using these specifiers. 22940f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 22959294523eSStephen Boyd * - 'Bb' as above with module build ID (for use in backtraces) 2296c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 2297c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 2298dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 2299dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 2300dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 2301dbc760bcSTejun Heo * range-list format instead of hex format 2302dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 2303dd45c9cfSHarvey Harrison * usual colon-separated hex notation 23048a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 2305bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 2306c8e00060SJoe Perches * with a dash-separated hex notation 23077c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 23088a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 23098a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 23108a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 231110679643SDaniel Borkmann * [S][pfs] 231210679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 231310679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 23148a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 23158a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 23168a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 231710679643SDaniel Borkmann * [S][pfs] 231810679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 231910679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 232010679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 232110679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 23228eda94bdSAlexander A. Klimov * https://tools.ietf.org/html/rfc5952 232371dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 232471dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 232571dca95dSAndy Shevchenko * details): 232671dca95dSAndy Shevchenko * a - ESCAPE_ANY 232771dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 232871dca95dSAndy Shevchenko * h - ESCAPE_HEX 232971dca95dSAndy Shevchenko * n - ESCAPE_NULL 233071dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 233171dca95dSAndy Shevchenko * p - ESCAPE_NP 233271dca95dSAndy Shevchenko * s - ESCAPE_SPACE 233371dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 23349ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 23359ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 23369ac6e44eSJoe Perches * Options for %pU are: 23379ac6e44eSJoe Perches * b big endian lower case hex (default) 23389ac6e44eSJoe Perches * B big endian UPPER case hex 23399ac6e44eSJoe Perches * l little endian lower case hex 23409ac6e44eSJoe Perches * L little endian UPPER case hex 23419ac6e44eSJoe Perches * big endian output byte order is: 23429ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 23439ac6e44eSJoe Perches * little endian output byte order is: 23449ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 23457db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 23467db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 23477db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 23487db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 23497db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 2350a48849e2SVlastimil Babka * - 'K' For a kernel pointer that should be hidden from unprivileged users. 2351a48849e2SVlastimil Babka * Use only for procfs, sysfs and similar files, not printk(); please 2352a48849e2SVlastimil Babka * read the documentation (path below) first. 2353c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 2354af612e43SSakari Ailus * - '4cc' V4L2 or DRM FourCC code, with endianness and raw numerical value. 235531550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 235631550a16SAndy Shevchenko * a certain separator (' ' by default): 235731550a16SAndy Shevchenko * C colon 235831550a16SAndy Shevchenko * D dash 235931550a16SAndy Shevchenko * N no separator 236031550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 236131550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 2362aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 2363aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 2364c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 2365c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 23661031bc58SDmitry Monakhov * - 'g' For block_device name (gendisk + partition number) 236720bc8c1eSAndy Shevchenko * - 't[RT][dt][r][s]' For time and date as represented by: 23684d42c447SAndy Shevchenko * R struct rtc_time 23697daac5b2SAndy Shevchenko * T time64_t 2370900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 2371900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2372900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 2373900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2374edf14cdbSVlastimil Babka * - 'G' For flags to be printed as a collection of symbolic strings that would 2375edf14cdbSVlastimil Babka * construct the specific value. Supported flags given by option: 2376edf14cdbSVlastimil Babka * p page flags (see struct page) given as pointer to unsigned long 2377edf14cdbSVlastimil Babka * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 2378edf14cdbSVlastimil Babka * v vma flags (VM_*) given as pointer to unsigned long 2379ce4fecf1SPantelis Antoniou * - 'OF[fnpPcCF]' For a device tree object 2380ce4fecf1SPantelis Antoniou * Without any optional arguments prints the full_name 2381ce4fecf1SPantelis Antoniou * f device node full_name 2382ce4fecf1SPantelis Antoniou * n device node name 2383ce4fecf1SPantelis Antoniou * p device node phandle 2384ce4fecf1SPantelis Antoniou * P device node path spec (name + @unit) 2385ce4fecf1SPantelis Antoniou * F device node flags 2386ce4fecf1SPantelis Antoniou * c major compatible string 2387ce4fecf1SPantelis Antoniou * C full compatible string 23883bd32d6aSSakari Ailus * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer 23893bd32d6aSSakari Ailus * Without an option prints the full name of the node 23903bd32d6aSSakari Ailus * f full name 23913bd32d6aSSakari Ailus * P node name, including a possible unit address 2392a48849e2SVlastimil Babka * - 'x' For printing the address unmodified. Equivalent to "%lx". 2393a48849e2SVlastimil Babka * Please read the documentation (path below) before using! 2394b2a5212fSDaniel Borkmann * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of 2395b2a5212fSDaniel Borkmann * bpf_trace_printk() where [ku] prefix specifies either kernel (k) 2396b2a5212fSDaniel Borkmann * or user (u) memory to probe, and: 2397b2a5212fSDaniel Borkmann * s a string, equivalent to "%s" on direct vsnprintf() use 23987b1924a1STobin C. Harding * 2399b3ed2321STobin C. Harding * ** When making changes please also update: 2400b3ed2321STobin C. Harding * Documentation/core-api/printk-formats.rst 24019ac6e44eSJoe Perches * 2402ad67b74dSTobin C. Harding * Note: The default behaviour (unadorned %p) is to hash the address, 2403ad67b74dSTobin C. Harding * rendering it useful as a unique identifier. 24044d8a743cSLinus Torvalds */ 2405cf3b429bSJoe Perches static noinline_for_stack 2406cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 2407fef20d9cSFrederic Weisbecker struct printf_spec spec) 240878a8bf69SLinus Torvalds { 24090fe1ef24SLinus Torvalds switch (*fmt) { 24100fe1ef24SLinus Torvalds case 'S': 24119ac6e44eSJoe Perches case 's': 241204b8eb7aSSergey Senozhatsky ptr = dereference_symbol_descriptor(ptr); 24134c1ca831SNick Desaulniers fallthrough; 24140f77a8d3SNamhyung Kim case 'B': 2415b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 2416332d2e78SLinus Torvalds case 'R': 2417c7dabef8SBjorn Helgaas case 'r': 2418fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 241931550a16SAndy Shevchenko case 'h': 242031550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 2421dbc760bcSTejun Heo case 'b': 2422dbc760bcSTejun Heo switch (fmt[1]) { 2423dbc760bcSTejun Heo case 'l': 2424dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 2425dbc760bcSTejun Heo default: 2426dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 2427dbc760bcSTejun Heo } 24288a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 24298a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 243076597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 243176597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 24328a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 24338a27f7c9SJoe Perches case 'I': /* Formatted IP supported 24348a27f7c9SJoe Perches * 4: 1.2.3.4 24358a27f7c9SJoe Perches * 6: 0001:0203:...:0708 24368a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 24378a27f7c9SJoe Perches */ 24388a27f7c9SJoe Perches case 'i': /* Contiguous: 24398a27f7c9SJoe Perches * 4: 001.002.003.004 24408a27f7c9SJoe Perches * 6: 000102...0f 24418a27f7c9SJoe Perches */ 2442f00cc102SPetr Mladek return ip_addr_string(buf, end, ptr, spec, fmt); 244371dca95dSAndy Shevchenko case 'E': 244471dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 24459ac6e44eSJoe Perches case 'U': 24469ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 24477db6f5fbSJoe Perches case 'V': 24483e5903ebSPetr Mladek return va_format(buf, end, ptr, spec, fmt); 2449455cd5abSDan Rosenberg case 'K': 245057e73442STobin C. Harding return restricted_pointer(buf, end, ptr, spec); 2451c8f44affSMichał Mirosław case 'N': 2452431bca24SGeert Uytterhoeven return netdev_bits(buf, end, ptr, spec, fmt); 2453af612e43SSakari Ailus case '4': 2454af612e43SSakari Ailus return fourcc_string(buf, end, ptr, spec, fmt); 24557d799210SStepan Moskovchenko case 'a': 24563e5903ebSPetr Mladek return address_val(buf, end, ptr, spec, fmt); 24574b6ccca7SAl Viro case 'd': 24584b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 24594d42c447SAndy Shevchenko case 't': 24604d42c447SAndy Shevchenko return time_and_date(buf, end, ptr, spec, fmt); 2461900cca29SGeert Uytterhoeven case 'C': 2462900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 24634b6ccca7SAl Viro case 'D': 246436594b31SJia He return file_dentry_name(buf, end, ptr, spec, fmt); 24651031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 24661031bc58SDmitry Monakhov case 'g': 24671031bc58SDmitry Monakhov return bdev_name(buf, end, ptr, spec, fmt); 24681031bc58SDmitry Monakhov #endif 24691031bc58SDmitry Monakhov 2470edf14cdbSVlastimil Babka case 'G': 24710b74d4d7SPetr Mladek return flags_string(buf, end, ptr, spec, fmt); 2472ce4fecf1SPantelis Antoniou case 'O': 247383abc5a7SSakari Ailus return device_node_string(buf, end, ptr, spec, fmt + 1); 24743bd32d6aSSakari Ailus case 'f': 24753bd32d6aSSakari Ailus return fwnode_string(buf, end, ptr, spec, fmt + 1); 24767b1924a1STobin C. Harding case 'x': 24777b1924a1STobin C. Harding return pointer_string(buf, end, ptr, spec); 247857f5677eSRasmus Villemoes case 'e': 247957f5677eSRasmus Villemoes /* %pe with a non-ERR_PTR gets treated as plain %p */ 248057f5677eSRasmus Villemoes if (!IS_ERR(ptr)) 2481*84842911SChristophe Leroy return default_pointer(buf, end, ptr, spec); 248257f5677eSRasmus Villemoes return err_ptr(buf, end, ptr, spec); 2483b2a5212fSDaniel Borkmann case 'u': 2484b2a5212fSDaniel Borkmann case 'k': 2485b2a5212fSDaniel Borkmann switch (fmt[1]) { 2486b2a5212fSDaniel Borkmann case 's': 2487b2a5212fSDaniel Borkmann return string(buf, end, ptr, spec); 2488b2a5212fSDaniel Borkmann default: 2489b2a5212fSDaniel Borkmann return error_string(buf, end, "(einval)", spec); 2490b2a5212fSDaniel Borkmann } 2491*84842911SChristophe Leroy default: 2492*84842911SChristophe Leroy return default_pointer(buf, end, ptr, spec); 24930fe1ef24SLinus Torvalds } 2494fef20d9cSFrederic Weisbecker } 2495fef20d9cSFrederic Weisbecker 2496fef20d9cSFrederic Weisbecker /* 2497fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 2498fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 2499fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 2500fef20d9cSFrederic Weisbecker * to go on the next call). 2501fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 2502fef20d9cSFrederic Weisbecker * 2503fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 2504fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 2505fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 25065b5e0928SAlexey Dobriyan * 'Z' changed to 'z' --adobriyan 2017-01-25 2507fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 2508fef20d9cSFrederic Weisbecker * 2509fef20d9cSFrederic Weisbecker * @fmt: the format string 2510fef20d9cSFrederic Weisbecker * @type of the token returned 2511fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 2512fef20d9cSFrederic Weisbecker * @field_width: overwritten width 2513fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 2514fef20d9cSFrederic Weisbecker * @precision: precision of a number 2515fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 2516fef20d9cSFrederic Weisbecker */ 2517cf3b429bSJoe Perches static noinline_for_stack 2518cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 2519fef20d9cSFrederic Weisbecker { 2520fef20d9cSFrederic Weisbecker const char *start = fmt; 2521d0484193SRasmus Villemoes char qualifier; 2522fef20d9cSFrederic Weisbecker 2523fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 2524ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 2525fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 2526fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 2527fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 2528fef20d9cSFrederic Weisbecker } 2529fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2530fef20d9cSFrederic Weisbecker goto precision; 2531fef20d9cSFrederic Weisbecker } 2532fef20d9cSFrederic Weisbecker 2533fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 2534fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 2535fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2536fef20d9cSFrederic Weisbecker spec->precision = 0; 2537fef20d9cSFrederic Weisbecker 2538fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2539fef20d9cSFrederic Weisbecker goto qualifier; 2540fef20d9cSFrederic Weisbecker } 2541fef20d9cSFrederic Weisbecker 2542fef20d9cSFrederic Weisbecker /* By default */ 2543fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2544fef20d9cSFrederic Weisbecker 2545fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 2546fef20d9cSFrederic Weisbecker if (*fmt == '%') 2547fef20d9cSFrederic Weisbecker break; 2548fef20d9cSFrederic Weisbecker } 2549fef20d9cSFrederic Weisbecker 2550fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 2551fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 2552fef20d9cSFrederic Weisbecker return fmt - start; 2553fef20d9cSFrederic Weisbecker 2554fef20d9cSFrederic Weisbecker /* Process flags */ 2555fef20d9cSFrederic Weisbecker spec->flags = 0; 2556fef20d9cSFrederic Weisbecker 2557fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 2558fef20d9cSFrederic Weisbecker bool found = true; 2559fef20d9cSFrederic Weisbecker 2560fef20d9cSFrederic Weisbecker ++fmt; 2561fef20d9cSFrederic Weisbecker 2562fef20d9cSFrederic Weisbecker switch (*fmt) { 2563fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 2564fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 2565fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 2566fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 2567fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 2568fef20d9cSFrederic Weisbecker default: found = false; 2569fef20d9cSFrederic Weisbecker } 2570fef20d9cSFrederic Weisbecker 2571fef20d9cSFrederic Weisbecker if (!found) 2572fef20d9cSFrederic Weisbecker break; 2573fef20d9cSFrederic Weisbecker } 2574fef20d9cSFrederic Weisbecker 2575fef20d9cSFrederic Weisbecker /* get field width */ 2576fef20d9cSFrederic Weisbecker spec->field_width = -1; 2577fef20d9cSFrederic Weisbecker 2578fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 2579fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 2580fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 2581fef20d9cSFrederic Weisbecker /* it's the next argument */ 2582ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 2583fef20d9cSFrederic Weisbecker return ++fmt - start; 2584fef20d9cSFrederic Weisbecker } 2585fef20d9cSFrederic Weisbecker 2586fef20d9cSFrederic Weisbecker precision: 2587fef20d9cSFrederic Weisbecker /* get the precision */ 2588fef20d9cSFrederic Weisbecker spec->precision = -1; 2589fef20d9cSFrederic Weisbecker if (*fmt == '.') { 2590fef20d9cSFrederic Weisbecker ++fmt; 2591fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 2592fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 2593fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2594fef20d9cSFrederic Weisbecker spec->precision = 0; 2595fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 2596fef20d9cSFrederic Weisbecker /* it's the next argument */ 2597adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 2598fef20d9cSFrederic Weisbecker return ++fmt - start; 2599fef20d9cSFrederic Weisbecker } 2600fef20d9cSFrederic Weisbecker } 2601fef20d9cSFrederic Weisbecker 2602fef20d9cSFrederic Weisbecker qualifier: 2603fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 2604d0484193SRasmus Villemoes qualifier = 0; 260575fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 26065b5e0928SAlexey Dobriyan *fmt == 'z' || *fmt == 't') { 2607d0484193SRasmus Villemoes qualifier = *fmt++; 2608d0484193SRasmus Villemoes if (unlikely(qualifier == *fmt)) { 2609d0484193SRasmus Villemoes if (qualifier == 'l') { 2610d0484193SRasmus Villemoes qualifier = 'L'; 2611fef20d9cSFrederic Weisbecker ++fmt; 2612d0484193SRasmus Villemoes } else if (qualifier == 'h') { 2613d0484193SRasmus Villemoes qualifier = 'H'; 2614a4e94ef0SZhaolei ++fmt; 2615a4e94ef0SZhaolei } 2616fef20d9cSFrederic Weisbecker } 2617fef20d9cSFrederic Weisbecker } 2618fef20d9cSFrederic Weisbecker 2619fef20d9cSFrederic Weisbecker /* default base */ 2620fef20d9cSFrederic Weisbecker spec->base = 10; 2621fef20d9cSFrederic Weisbecker switch (*fmt) { 2622fef20d9cSFrederic Weisbecker case 'c': 2623fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 2624fef20d9cSFrederic Weisbecker return ++fmt - start; 2625fef20d9cSFrederic Weisbecker 2626fef20d9cSFrederic Weisbecker case 's': 2627fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 2628fef20d9cSFrederic Weisbecker return ++fmt - start; 2629fef20d9cSFrederic Weisbecker 2630fef20d9cSFrederic Weisbecker case 'p': 2631fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 2632ffbfed03SRasmus Villemoes return ++fmt - start; 2633fef20d9cSFrederic Weisbecker 2634fef20d9cSFrederic Weisbecker case '%': 2635fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 2636fef20d9cSFrederic Weisbecker return ++fmt - start; 2637fef20d9cSFrederic Weisbecker 2638fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 2639fef20d9cSFrederic Weisbecker case 'o': 2640fef20d9cSFrederic Weisbecker spec->base = 8; 2641fef20d9cSFrederic Weisbecker break; 2642fef20d9cSFrederic Weisbecker 2643fef20d9cSFrederic Weisbecker case 'x': 2644fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 26454c1ca831SNick Desaulniers fallthrough; 2646fef20d9cSFrederic Weisbecker 2647fef20d9cSFrederic Weisbecker case 'X': 2648fef20d9cSFrederic Weisbecker spec->base = 16; 2649fef20d9cSFrederic Weisbecker break; 2650fef20d9cSFrederic Weisbecker 2651fef20d9cSFrederic Weisbecker case 'd': 2652fef20d9cSFrederic Weisbecker case 'i': 265339e874f8SFrederic Weisbecker spec->flags |= SIGN; 265436f9ff9eSGustavo A. R. Silva break; 2655fef20d9cSFrederic Weisbecker case 'u': 2656fef20d9cSFrederic Weisbecker break; 2657fef20d9cSFrederic Weisbecker 2658708d96fdSRyan Mallon case 'n': 2659708d96fdSRyan Mallon /* 2660b006f19bSRasmus Villemoes * Since %n poses a greater security risk than 2661b006f19bSRasmus Villemoes * utility, treat it as any other invalid or 2662b006f19bSRasmus Villemoes * unsupported format specifier. 2663708d96fdSRyan Mallon */ 26644c1ca831SNick Desaulniers fallthrough; 2665708d96fdSRyan Mallon 2666fef20d9cSFrederic Weisbecker default: 2667b006f19bSRasmus Villemoes WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 2668fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 2669fef20d9cSFrederic Weisbecker return fmt - start; 2670fef20d9cSFrederic Weisbecker } 2671fef20d9cSFrederic Weisbecker 2672d0484193SRasmus Villemoes if (qualifier == 'L') 2673fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 2674d0484193SRasmus Villemoes else if (qualifier == 'l') { 267551be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 267651be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 26775b5e0928SAlexey Dobriyan } else if (qualifier == 'z') { 2678fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 2679d0484193SRasmus Villemoes } else if (qualifier == 't') { 2680fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 2681d0484193SRasmus Villemoes } else if (qualifier == 'H') { 268251be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 268351be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 2684d0484193SRasmus Villemoes } else if (qualifier == 'h') { 268551be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 268651be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 2687fef20d9cSFrederic Weisbecker } else { 268851be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 268951be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 2690fef20d9cSFrederic Weisbecker } 2691fef20d9cSFrederic Weisbecker 2692fef20d9cSFrederic Weisbecker return ++fmt - start; 269378a8bf69SLinus Torvalds } 269478a8bf69SLinus Torvalds 26954d72ba01SRasmus Villemoes static void 26964d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width) 26974d72ba01SRasmus Villemoes { 26984d72ba01SRasmus Villemoes spec->field_width = width; 26994d72ba01SRasmus Villemoes if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 27004d72ba01SRasmus Villemoes spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 27014d72ba01SRasmus Villemoes } 27024d72ba01SRasmus Villemoes } 27034d72ba01SRasmus Villemoes 27044d72ba01SRasmus Villemoes static void 27054d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec) 27064d72ba01SRasmus Villemoes { 27074d72ba01SRasmus Villemoes spec->precision = prec; 27084d72ba01SRasmus Villemoes if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 27094d72ba01SRasmus Villemoes spec->precision = clamp(prec, 0, PRECISION_MAX); 27104d72ba01SRasmus Villemoes } 27114d72ba01SRasmus Villemoes } 27124d72ba01SRasmus Villemoes 27131da177e4SLinus Torvalds /** 27141da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 27151da177e4SLinus Torvalds * @buf: The buffer to place the result into 27161da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 27171da177e4SLinus Torvalds * @fmt: The format string to use 27181da177e4SLinus Torvalds * @args: Arguments for the format string 27191da177e4SLinus Torvalds * 2720d7ec9a05SRasmus Villemoes * This function generally follows C99 vsnprintf, but has some 2721d7ec9a05SRasmus Villemoes * extensions and a few limitations: 2722d7ec9a05SRasmus Villemoes * 27236cc89134Smchehab@s-opensource.com * - ``%n`` is unsupported 27246cc89134Smchehab@s-opensource.com * - ``%p*`` is handled by pointer() 272520036fdcSAndi Kleen * 272627e7c0e8SJonathan Corbet * See pointer() or Documentation/core-api/printk-formats.rst for more 27275e4ee7b1SMartin Kletzander * extensive description. 27285e4ee7b1SMartin Kletzander * 27295e4ee7b1SMartin Kletzander * **Please update the documentation in both places when making changes** 273080f548e0SAndrew Morton * 27311da177e4SLinus Torvalds * The return value is the number of characters which would 27321da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 27331da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 27341da177e4SLinus Torvalds * number of characters written into @buf as return value 273572fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 27361da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 27371da177e4SLinus Torvalds * string is truncated. 27381da177e4SLinus Torvalds * 2739ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 27401da177e4SLinus Torvalds */ 27411da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 27421da177e4SLinus Torvalds { 27431da177e4SLinus Torvalds unsigned long long num; 2744d4be151bSAndré Goddard Rosa char *str, *end; 2745fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 27461da177e4SLinus Torvalds 2747f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 2748f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 27492aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 27501da177e4SLinus Torvalds return 0; 27511da177e4SLinus Torvalds 27521da177e4SLinus Torvalds str = buf; 2753f796937aSJeremy Fitzhardinge end = buf + size; 27541da177e4SLinus Torvalds 2755f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 2756f796937aSJeremy Fitzhardinge if (end < buf) { 27571da177e4SLinus Torvalds end = ((void *)-1); 2758f796937aSJeremy Fitzhardinge size = end - buf; 27591da177e4SLinus Torvalds } 27601da177e4SLinus Torvalds 2761fef20d9cSFrederic Weisbecker while (*fmt) { 2762fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2763d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2764fef20d9cSFrederic Weisbecker 2765fef20d9cSFrederic Weisbecker fmt += read; 2766fef20d9cSFrederic Weisbecker 2767fef20d9cSFrederic Weisbecker switch (spec.type) { 2768fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2769fef20d9cSFrederic Weisbecker int copy = read; 2770fef20d9cSFrederic Weisbecker if (str < end) { 2771fef20d9cSFrederic Weisbecker if (copy > end - str) 2772fef20d9cSFrederic Weisbecker copy = end - str; 2773fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2774fef20d9cSFrederic Weisbecker } 2775fef20d9cSFrederic Weisbecker str += read; 2776fef20d9cSFrederic Weisbecker break; 27771da177e4SLinus Torvalds } 27781da177e4SLinus Torvalds 2779ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 27804d72ba01SRasmus Villemoes set_field_width(&spec, va_arg(args, int)); 2781fef20d9cSFrederic Weisbecker break; 27821da177e4SLinus Torvalds 2783fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 27844d72ba01SRasmus Villemoes set_precision(&spec, va_arg(args, int)); 2785fef20d9cSFrederic Weisbecker break; 27861da177e4SLinus Torvalds 2787d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2788d4be151bSAndré Goddard Rosa char c; 2789d4be151bSAndré Goddard Rosa 2790fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2791fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2792f796937aSJeremy Fitzhardinge if (str < end) 27931da177e4SLinus Torvalds *str = ' '; 27941da177e4SLinus Torvalds ++str; 2795fef20d9cSFrederic Weisbecker 27961da177e4SLinus Torvalds } 27971da177e4SLinus Torvalds } 27981da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 2799f796937aSJeremy Fitzhardinge if (str < end) 28001da177e4SLinus Torvalds *str = c; 28011da177e4SLinus Torvalds ++str; 2802fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2803f796937aSJeremy Fitzhardinge if (str < end) 28041da177e4SLinus Torvalds *str = ' '; 28051da177e4SLinus Torvalds ++str; 28061da177e4SLinus Torvalds } 2807fef20d9cSFrederic Weisbecker break; 2808d4be151bSAndré Goddard Rosa } 28091da177e4SLinus Torvalds 2810fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 2811fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 2812fef20d9cSFrederic Weisbecker break; 28131da177e4SLinus Torvalds 2814fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2815ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 2816fef20d9cSFrederic Weisbecker spec); 2817fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 28184d8a743cSLinus Torvalds fmt++; 2819fef20d9cSFrederic Weisbecker break; 28201da177e4SLinus Torvalds 2821fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2822f796937aSJeremy Fitzhardinge if (str < end) 28231da177e4SLinus Torvalds *str = '%'; 28241da177e4SLinus Torvalds ++str; 28251da177e4SLinus Torvalds break; 28261da177e4SLinus Torvalds 2827fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 2828b006f19bSRasmus Villemoes /* 2829b006f19bSRasmus Villemoes * Presumably the arguments passed gcc's type 2830b006f19bSRasmus Villemoes * checking, but there is no safe or sane way 2831b006f19bSRasmus Villemoes * for us to continue parsing the format and 2832b006f19bSRasmus Villemoes * fetching from the va_list; the remaining 2833b006f19bSRasmus Villemoes * specifiers and arguments would be out of 2834b006f19bSRasmus Villemoes * sync. 2835b006f19bSRasmus Villemoes */ 2836b006f19bSRasmus Villemoes goto out; 2837fef20d9cSFrederic Weisbecker 2838fef20d9cSFrederic Weisbecker default: 2839fef20d9cSFrederic Weisbecker switch (spec.type) { 2840fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2841fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 2842fef20d9cSFrederic Weisbecker break; 2843fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2844fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 2845fef20d9cSFrederic Weisbecker break; 2846fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2847fef20d9cSFrederic Weisbecker num = va_arg(args, long); 2848fef20d9cSFrederic Weisbecker break; 2849fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2850ef124960SJason Gunthorpe if (spec.flags & SIGN) 2851ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 2852ef124960SJason Gunthorpe else 2853fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 2854fef20d9cSFrederic Weisbecker break; 2855fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2856fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 2857fef20d9cSFrederic Weisbecker break; 2858a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2859a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 2860a4e94ef0SZhaolei break; 2861a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2862a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 2863a4e94ef0SZhaolei break; 2864fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2865fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 2866fef20d9cSFrederic Weisbecker break; 2867fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2868fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 2869fef20d9cSFrederic Weisbecker break; 287039e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 287139e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 2872fef20d9cSFrederic Weisbecker break; 2873fef20d9cSFrederic Weisbecker default: 2874fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 28751da177e4SLinus Torvalds } 2876fef20d9cSFrederic Weisbecker 2877fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 28781da177e4SLinus Torvalds } 2879fef20d9cSFrederic Weisbecker } 2880fef20d9cSFrederic Weisbecker 2881b006f19bSRasmus Villemoes out: 2882f796937aSJeremy Fitzhardinge if (size > 0) { 2883f796937aSJeremy Fitzhardinge if (str < end) 28841da177e4SLinus Torvalds *str = '\0'; 2885f796937aSJeremy Fitzhardinge else 28860a6047eeSLinus Torvalds end[-1] = '\0'; 2887f796937aSJeremy Fitzhardinge } 2888fef20d9cSFrederic Weisbecker 2889f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 28901da177e4SLinus Torvalds return str-buf; 2891fef20d9cSFrederic Weisbecker 28921da177e4SLinus Torvalds } 28931da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 28941da177e4SLinus Torvalds 28951da177e4SLinus Torvalds /** 28961da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 28971da177e4SLinus Torvalds * @buf: The buffer to place the result into 28981da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 28991da177e4SLinus Torvalds * @fmt: The format string to use 29001da177e4SLinus Torvalds * @args: Arguments for the format string 29011da177e4SLinus Torvalds * 29021da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2903b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 29041da177e4SLinus Torvalds * returns 0. 29051da177e4SLinus Torvalds * 2906ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 290720036fdcSAndi Kleen * 290820036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 29091da177e4SLinus Torvalds */ 29101da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 29111da177e4SLinus Torvalds { 29121da177e4SLinus Torvalds int i; 29131da177e4SLinus Torvalds 29141da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 29157b9186f5SAndré Goddard Rosa 2916b921c69fSAnton Arapov if (likely(i < size)) 2917b921c69fSAnton Arapov return i; 2918b921c69fSAnton Arapov if (size != 0) 2919b921c69fSAnton Arapov return size - 1; 2920b921c69fSAnton Arapov return 0; 29211da177e4SLinus Torvalds } 29221da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 29231da177e4SLinus Torvalds 29241da177e4SLinus Torvalds /** 29251da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 29261da177e4SLinus Torvalds * @buf: The buffer to place the result into 29271da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 29281da177e4SLinus Torvalds * @fmt: The format string to use 29291da177e4SLinus Torvalds * @...: Arguments for the format string 29301da177e4SLinus Torvalds * 29311da177e4SLinus Torvalds * The return value is the number of characters which would be 29321da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 29331da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 29341da177e4SLinus Torvalds * @size, the resulting string is truncated. 293520036fdcSAndi Kleen * 293620036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 29371da177e4SLinus Torvalds */ 29381da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 29391da177e4SLinus Torvalds { 29401da177e4SLinus Torvalds va_list args; 29411da177e4SLinus Torvalds int i; 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds va_start(args, fmt); 29441da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 29451da177e4SLinus Torvalds va_end(args); 29467b9186f5SAndré Goddard Rosa 29471da177e4SLinus Torvalds return i; 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 29501da177e4SLinus Torvalds 29511da177e4SLinus Torvalds /** 29521da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 29531da177e4SLinus Torvalds * @buf: The buffer to place the result into 29541da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 29551da177e4SLinus Torvalds * @fmt: The format string to use 29561da177e4SLinus Torvalds * @...: Arguments for the format string 29571da177e4SLinus Torvalds * 29581da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2959b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 29601da177e4SLinus Torvalds */ 29611da177e4SLinus Torvalds 29621da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 29631da177e4SLinus Torvalds { 29641da177e4SLinus Torvalds va_list args; 29651da177e4SLinus Torvalds int i; 29661da177e4SLinus Torvalds 29671da177e4SLinus Torvalds va_start(args, fmt); 2968b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 29691da177e4SLinus Torvalds va_end(args); 29707b9186f5SAndré Goddard Rosa 2971b903c0b8SChangli Gao return i; 29721da177e4SLinus Torvalds } 29731da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 29741da177e4SLinus Torvalds 29751da177e4SLinus Torvalds /** 29761da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 29771da177e4SLinus Torvalds * @buf: The buffer to place the result into 29781da177e4SLinus Torvalds * @fmt: The format string to use 29791da177e4SLinus Torvalds * @args: Arguments for the format string 29801da177e4SLinus Torvalds * 29811da177e4SLinus Torvalds * The function returns the number of characters written 298272fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 29831da177e4SLinus Torvalds * buffer overflows. 29841da177e4SLinus Torvalds * 2985ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 298620036fdcSAndi Kleen * 298720036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 29881da177e4SLinus Torvalds */ 29891da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 29901da177e4SLinus Torvalds { 29911da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 29921da177e4SLinus Torvalds } 29931da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 29941da177e4SLinus Torvalds 29951da177e4SLinus Torvalds /** 29961da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 29971da177e4SLinus Torvalds * @buf: The buffer to place the result into 29981da177e4SLinus Torvalds * @fmt: The format string to use 29991da177e4SLinus Torvalds * @...: Arguments for the format string 30001da177e4SLinus Torvalds * 30011da177e4SLinus Torvalds * The function returns the number of characters written 300272fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 30031da177e4SLinus Torvalds * buffer overflows. 300420036fdcSAndi Kleen * 300520036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 30061da177e4SLinus Torvalds */ 30071da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 30081da177e4SLinus Torvalds { 30091da177e4SLinus Torvalds va_list args; 30101da177e4SLinus Torvalds int i; 30111da177e4SLinus Torvalds 30121da177e4SLinus Torvalds va_start(args, fmt); 30131da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 30141da177e4SLinus Torvalds va_end(args); 30157b9186f5SAndré Goddard Rosa 30161da177e4SLinus Torvalds return i; 30171da177e4SLinus Torvalds } 30181da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 30191da177e4SLinus Torvalds 30204370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 30214370aa4aSLai Jiangshan /* 30224370aa4aSLai Jiangshan * bprintf service: 30234370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 30244370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 30254370aa4aSLai Jiangshan */ 30264370aa4aSLai Jiangshan 30274370aa4aSLai Jiangshan /** 30284370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 30294370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 30304370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 30314370aa4aSLai Jiangshan * @fmt: The format string to use 30324370aa4aSLai Jiangshan * @args: Arguments for the format string 30334370aa4aSLai Jiangshan * 30344370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 3035da3dae54SMasanari Iida * is skipped. 30364370aa4aSLai Jiangshan * 30374370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 30384370aa4aSLai Jiangshan * the given input. 30394370aa4aSLai Jiangshan * 30404370aa4aSLai Jiangshan * NOTE: 30414370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 30424370aa4aSLai Jiangshan * valid for bstr_printf(). 30434370aa4aSLai Jiangshan */ 30444370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 30454370aa4aSLai Jiangshan { 3046fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 30474370aa4aSLai Jiangshan char *str, *end; 3048841a915dSSteven Rostedt (VMware) int width; 30494370aa4aSLai Jiangshan 30504370aa4aSLai Jiangshan str = (char *)bin_buf; 30514370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 30524370aa4aSLai Jiangshan 30534370aa4aSLai Jiangshan #define save_arg(type) \ 3054841a915dSSteven Rostedt (VMware) ({ \ 30554370aa4aSLai Jiangshan unsigned long long value; \ 3056841a915dSSteven Rostedt (VMware) if (sizeof(type) == 8) { \ 3057841a915dSSteven Rostedt (VMware) unsigned long long val8; \ 30584370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 3059841a915dSSteven Rostedt (VMware) val8 = va_arg(args, unsigned long long); \ 30604370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 3061841a915dSSteven Rostedt (VMware) *(u32 *)str = *(u32 *)&val8; \ 3062841a915dSSteven Rostedt (VMware) *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 30634370aa4aSLai Jiangshan } \ 3064841a915dSSteven Rostedt (VMware) value = val8; \ 30654370aa4aSLai Jiangshan } else { \ 3066841a915dSSteven Rostedt (VMware) unsigned int val4; \ 30674370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 3068841a915dSSteven Rostedt (VMware) val4 = va_arg(args, int); \ 30694370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 3070841a915dSSteven Rostedt (VMware) *(typeof(type) *)str = (type)(long)val4; \ 3071841a915dSSteven Rostedt (VMware) value = (unsigned long long)val4; \ 30724370aa4aSLai Jiangshan } \ 30734370aa4aSLai Jiangshan str += sizeof(type); \ 3074841a915dSSteven Rostedt (VMware) value; \ 3075841a915dSSteven Rostedt (VMware) }) 30764370aa4aSLai Jiangshan 3077fef20d9cSFrederic Weisbecker while (*fmt) { 3078d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 30794370aa4aSLai Jiangshan 3080fef20d9cSFrederic Weisbecker fmt += read; 3081fef20d9cSFrederic Weisbecker 3082fef20d9cSFrederic Weisbecker switch (spec.type) { 3083fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 3084d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 3085fef20d9cSFrederic Weisbecker break; 3086b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 3087b006f19bSRasmus Villemoes goto out; 3088fef20d9cSFrederic Weisbecker 3089ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 3090fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 3091841a915dSSteven Rostedt (VMware) width = (int)save_arg(int); 3092841a915dSSteven Rostedt (VMware) /* Pointers may require the width */ 3093841a915dSSteven Rostedt (VMware) if (*fmt == 'p') 3094841a915dSSteven Rostedt (VMware) set_field_width(&spec, width); 3095fef20d9cSFrederic Weisbecker break; 30964370aa4aSLai Jiangshan 3097fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 30984370aa4aSLai Jiangshan save_arg(char); 3099fef20d9cSFrederic Weisbecker break; 3100fef20d9cSFrederic Weisbecker 3101fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 31024370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 31033e5903ebSPetr Mladek const char *err_msg; 31044370aa4aSLai Jiangshan size_t len; 31056c356634SAndré Goddard Rosa 31063e5903ebSPetr Mladek err_msg = check_pointer_msg(save_str); 31073e5903ebSPetr Mladek if (err_msg) 31083e5903ebSPetr Mladek save_str = err_msg; 31093e5903ebSPetr Mladek 31106c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 31116c356634SAndré Goddard Rosa if (str + len < end) 31126c356634SAndré Goddard Rosa memcpy(str, save_str, len); 31136c356634SAndré Goddard Rosa str += len; 3114fef20d9cSFrederic Weisbecker break; 31154370aa4aSLai Jiangshan } 3116fef20d9cSFrederic Weisbecker 3117fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 3118841a915dSSteven Rostedt (VMware) /* Dereferenced pointers must be done now */ 3119841a915dSSteven Rostedt (VMware) switch (*fmt) { 3120841a915dSSteven Rostedt (VMware) /* Dereference of functions is still OK */ 3121841a915dSSteven Rostedt (VMware) case 'S': 3122841a915dSSteven Rostedt (VMware) case 's': 31231e6338cfSSteven Rostedt (VMware) case 'x': 31241e6338cfSSteven Rostedt (VMware) case 'K': 312557f5677eSRasmus Villemoes case 'e': 31264370aa4aSLai Jiangshan save_arg(void *); 3127841a915dSSteven Rostedt (VMware) break; 3128841a915dSSteven Rostedt (VMware) default: 3129841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 3130841a915dSSteven Rostedt (VMware) save_arg(void *); 3131841a915dSSteven Rostedt (VMware) break; 3132841a915dSSteven Rostedt (VMware) } 3133841a915dSSteven Rostedt (VMware) str = pointer(fmt, str, end, va_arg(args, void *), 3134841a915dSSteven Rostedt (VMware) spec); 3135841a915dSSteven Rostedt (VMware) if (str + 1 < end) 3136841a915dSSteven Rostedt (VMware) *str++ = '\0'; 3137841a915dSSteven Rostedt (VMware) else 3138841a915dSSteven Rostedt (VMware) end[-1] = '\0'; /* Must be nul terminated */ 3139841a915dSSteven Rostedt (VMware) } 31404370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 3141fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 31424370aa4aSLai Jiangshan fmt++; 3143fef20d9cSFrederic Weisbecker break; 3144fef20d9cSFrederic Weisbecker 3145fef20d9cSFrederic Weisbecker default: 3146fef20d9cSFrederic Weisbecker switch (spec.type) { 3147fef20d9cSFrederic Weisbecker 3148fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 3149fef20d9cSFrederic Weisbecker save_arg(long long); 3150fef20d9cSFrederic Weisbecker break; 3151fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 3152fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 3153fef20d9cSFrederic Weisbecker save_arg(unsigned long); 3154fef20d9cSFrederic Weisbecker break; 3155fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 3156fef20d9cSFrederic Weisbecker save_arg(size_t); 3157fef20d9cSFrederic Weisbecker break; 3158fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 3159fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 3160fef20d9cSFrederic Weisbecker break; 3161a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 3162a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 3163a4e94ef0SZhaolei save_arg(char); 3164a4e94ef0SZhaolei break; 3165fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 3166fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 3167fef20d9cSFrederic Weisbecker save_arg(short); 3168fef20d9cSFrederic Weisbecker break; 3169fef20d9cSFrederic Weisbecker default: 3170fef20d9cSFrederic Weisbecker save_arg(int); 3171fef20d9cSFrederic Weisbecker } 3172fef20d9cSFrederic Weisbecker } 3173fef20d9cSFrederic Weisbecker } 3174fef20d9cSFrederic Weisbecker 3175b006f19bSRasmus Villemoes out: 31767b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 3177fef20d9cSFrederic Weisbecker #undef save_arg 31784370aa4aSLai Jiangshan } 31794370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 31804370aa4aSLai Jiangshan 31814370aa4aSLai Jiangshan /** 31824370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 31834370aa4aSLai Jiangshan * @buf: The buffer to place the result into 31844370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 31854370aa4aSLai Jiangshan * @fmt: The format string to use 31864370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 31874370aa4aSLai Jiangshan * 31884370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 31894370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 31904370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 31914370aa4aSLai Jiangshan * 31924370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 31930efb4d20SSteven Rostedt * see vsnprintf comment for details. 31944370aa4aSLai Jiangshan * 31954370aa4aSLai Jiangshan * The return value is the number of characters which would 31964370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 31974370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 31984370aa4aSLai Jiangshan * number of characters written into @buf as return value 31994370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 32004370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 32014370aa4aSLai Jiangshan * string is truncated. 32024370aa4aSLai Jiangshan */ 32034370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 32044370aa4aSLai Jiangshan { 3205fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 3206d4be151bSAndré Goddard Rosa char *str, *end; 3207d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 32084370aa4aSLai Jiangshan 3209762abb51SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 32104370aa4aSLai Jiangshan return 0; 32114370aa4aSLai Jiangshan 32124370aa4aSLai Jiangshan str = buf; 32134370aa4aSLai Jiangshan end = buf + size; 32144370aa4aSLai Jiangshan 32154370aa4aSLai Jiangshan #define get_arg(type) \ 32164370aa4aSLai Jiangshan ({ \ 32174370aa4aSLai Jiangshan typeof(type) value; \ 32184370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 32194370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 32204370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 32214370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 32224370aa4aSLai Jiangshan } else { \ 32234370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 32244370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 32254370aa4aSLai Jiangshan } \ 32264370aa4aSLai Jiangshan args += sizeof(type); \ 32274370aa4aSLai Jiangshan value; \ 32284370aa4aSLai Jiangshan }) 32294370aa4aSLai Jiangshan 32304370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 32314370aa4aSLai Jiangshan if (end < buf) { 32324370aa4aSLai Jiangshan end = ((void *)-1); 32334370aa4aSLai Jiangshan size = end - buf; 32344370aa4aSLai Jiangshan } 32354370aa4aSLai Jiangshan 3236fef20d9cSFrederic Weisbecker while (*fmt) { 3237fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 3238d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 3239fef20d9cSFrederic Weisbecker 3240fef20d9cSFrederic Weisbecker fmt += read; 3241fef20d9cSFrederic Weisbecker 3242fef20d9cSFrederic Weisbecker switch (spec.type) { 3243fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 3244fef20d9cSFrederic Weisbecker int copy = read; 3245fef20d9cSFrederic Weisbecker if (str < end) { 3246fef20d9cSFrederic Weisbecker if (copy > end - str) 3247fef20d9cSFrederic Weisbecker copy = end - str; 3248fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 3249fef20d9cSFrederic Weisbecker } 3250fef20d9cSFrederic Weisbecker str += read; 3251fef20d9cSFrederic Weisbecker break; 32524370aa4aSLai Jiangshan } 32534370aa4aSLai Jiangshan 3254ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 32554d72ba01SRasmus Villemoes set_field_width(&spec, get_arg(int)); 3256fef20d9cSFrederic Weisbecker break; 32574370aa4aSLai Jiangshan 3258fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 32594d72ba01SRasmus Villemoes set_precision(&spec, get_arg(int)); 3260fef20d9cSFrederic Weisbecker break; 32614370aa4aSLai Jiangshan 3262d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 3263d4be151bSAndré Goddard Rosa char c; 3264d4be151bSAndré Goddard Rosa 3265fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 3266fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 32674370aa4aSLai Jiangshan if (str < end) 32684370aa4aSLai Jiangshan *str = ' '; 32694370aa4aSLai Jiangshan ++str; 32704370aa4aSLai Jiangshan } 32714370aa4aSLai Jiangshan } 32724370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 32734370aa4aSLai Jiangshan if (str < end) 32744370aa4aSLai Jiangshan *str = c; 32754370aa4aSLai Jiangshan ++str; 3276fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 32774370aa4aSLai Jiangshan if (str < end) 32784370aa4aSLai Jiangshan *str = ' '; 32794370aa4aSLai Jiangshan ++str; 32804370aa4aSLai Jiangshan } 3281fef20d9cSFrederic Weisbecker break; 3282d4be151bSAndré Goddard Rosa } 32834370aa4aSLai Jiangshan 3284fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 32854370aa4aSLai Jiangshan const char *str_arg = args; 3286d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 3287fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 3288fef20d9cSFrederic Weisbecker break; 32894370aa4aSLai Jiangshan } 32904370aa4aSLai Jiangshan 3291841a915dSSteven Rostedt (VMware) case FORMAT_TYPE_PTR: { 3292841a915dSSteven Rostedt (VMware) bool process = false; 3293841a915dSSteven Rostedt (VMware) int copy, len; 3294841a915dSSteven Rostedt (VMware) /* Non function dereferences were already done */ 3295841a915dSSteven Rostedt (VMware) switch (*fmt) { 3296841a915dSSteven Rostedt (VMware) case 'S': 3297841a915dSSteven Rostedt (VMware) case 's': 32981e6338cfSSteven Rostedt (VMware) case 'x': 32991e6338cfSSteven Rostedt (VMware) case 'K': 330057f5677eSRasmus Villemoes case 'e': 3301841a915dSSteven Rostedt (VMware) process = true; 3302841a915dSSteven Rostedt (VMware) break; 3303841a915dSSteven Rostedt (VMware) default: 3304841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 3305841a915dSSteven Rostedt (VMware) process = true; 3306841a915dSSteven Rostedt (VMware) break; 3307841a915dSSteven Rostedt (VMware) } 3308841a915dSSteven Rostedt (VMware) /* Pointer dereference was already processed */ 3309841a915dSSteven Rostedt (VMware) if (str < end) { 3310841a915dSSteven Rostedt (VMware) len = copy = strlen(args); 3311841a915dSSteven Rostedt (VMware) if (copy > end - str) 3312841a915dSSteven Rostedt (VMware) copy = end - str; 3313841a915dSSteven Rostedt (VMware) memcpy(str, args, copy); 3314841a915dSSteven Rostedt (VMware) str += len; 331562165600SSteven Rostedt (VMware) args += len + 1; 3316841a915dSSteven Rostedt (VMware) } 3317841a915dSSteven Rostedt (VMware) } 3318841a915dSSteven Rostedt (VMware) if (process) 3319ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 3320841a915dSSteven Rostedt (VMware) 3321fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 33224370aa4aSLai Jiangshan fmt++; 3323fef20d9cSFrederic Weisbecker break; 3324841a915dSSteven Rostedt (VMware) } 33254370aa4aSLai Jiangshan 3326fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 33274370aa4aSLai Jiangshan if (str < end) 33284370aa4aSLai Jiangshan *str = '%'; 33294370aa4aSLai Jiangshan ++str; 3330fef20d9cSFrederic Weisbecker break; 3331fef20d9cSFrederic Weisbecker 3332b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 3333b006f19bSRasmus Villemoes goto out; 3334b006f19bSRasmus Villemoes 3335d4be151bSAndré Goddard Rosa default: { 3336d4be151bSAndré Goddard Rosa unsigned long long num; 3337d4be151bSAndré Goddard Rosa 3338fef20d9cSFrederic Weisbecker switch (spec.type) { 3339fef20d9cSFrederic Weisbecker 3340fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 33414370aa4aSLai Jiangshan num = get_arg(long long); 3342fef20d9cSFrederic Weisbecker break; 3343fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 3344fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 3345fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 3346fef20d9cSFrederic Weisbecker break; 3347fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 33484370aa4aSLai Jiangshan num = get_arg(size_t); 3349fef20d9cSFrederic Weisbecker break; 3350fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 33514370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 3352fef20d9cSFrederic Weisbecker break; 3353a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 3354a4e94ef0SZhaolei num = get_arg(unsigned char); 3355a4e94ef0SZhaolei break; 3356a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 3357a4e94ef0SZhaolei num = get_arg(signed char); 3358a4e94ef0SZhaolei break; 3359fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 3360fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 3361fef20d9cSFrederic Weisbecker break; 3362fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 3363fef20d9cSFrederic Weisbecker num = get_arg(short); 3364fef20d9cSFrederic Weisbecker break; 3365fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 33664370aa4aSLai Jiangshan num = get_arg(unsigned int); 3367fef20d9cSFrederic Weisbecker break; 3368fef20d9cSFrederic Weisbecker default: 3369fef20d9cSFrederic Weisbecker num = get_arg(int); 33704370aa4aSLai Jiangshan } 3371fef20d9cSFrederic Weisbecker 3372fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 3373d4be151bSAndré Goddard Rosa } /* default: */ 3374d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 3375d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 3376fef20d9cSFrederic Weisbecker 3377b006f19bSRasmus Villemoes out: 33784370aa4aSLai Jiangshan if (size > 0) { 33794370aa4aSLai Jiangshan if (str < end) 33804370aa4aSLai Jiangshan *str = '\0'; 33814370aa4aSLai Jiangshan else 33824370aa4aSLai Jiangshan end[-1] = '\0'; 33834370aa4aSLai Jiangshan } 3384fef20d9cSFrederic Weisbecker 33854370aa4aSLai Jiangshan #undef get_arg 33864370aa4aSLai Jiangshan 33874370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 33884370aa4aSLai Jiangshan return str - buf; 33894370aa4aSLai Jiangshan } 33904370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 33914370aa4aSLai Jiangshan 33924370aa4aSLai Jiangshan /** 33934370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 33944370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 33954370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 33964370aa4aSLai Jiangshan * @fmt: The format string to use 33974370aa4aSLai Jiangshan * @...: Arguments for the format string 33984370aa4aSLai Jiangshan * 33994370aa4aSLai Jiangshan * The function returns the number of words(u32) written 34004370aa4aSLai Jiangshan * into @bin_buf. 34014370aa4aSLai Jiangshan */ 34024370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 34034370aa4aSLai Jiangshan { 34044370aa4aSLai Jiangshan va_list args; 34054370aa4aSLai Jiangshan int ret; 34064370aa4aSLai Jiangshan 34074370aa4aSLai Jiangshan va_start(args, fmt); 34084370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 34094370aa4aSLai Jiangshan va_end(args); 34107b9186f5SAndré Goddard Rosa 34114370aa4aSLai Jiangshan return ret; 34124370aa4aSLai Jiangshan } 34134370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 34144370aa4aSLai Jiangshan 34154370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 34164370aa4aSLai Jiangshan 34171da177e4SLinus Torvalds /** 34181da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 34191da177e4SLinus Torvalds * @buf: input buffer 34201da177e4SLinus Torvalds * @fmt: format of buffer 34211da177e4SLinus Torvalds * @args: arguments 34221da177e4SLinus Torvalds */ 34231da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 34241da177e4SLinus Torvalds { 34251da177e4SLinus Torvalds const char *str = buf; 34261da177e4SLinus Torvalds char *next; 34271da177e4SLinus Torvalds char digit; 34281da177e4SLinus Torvalds int num = 0; 3429ef0658f3SJoe Perches u8 qualifier; 343053809751SJan Beulich unsigned int base; 343153809751SJan Beulich union { 343253809751SJan Beulich long long s; 343353809751SJan Beulich unsigned long long u; 343453809751SJan Beulich } val; 3435ef0658f3SJoe Perches s16 field_width; 3436d4be151bSAndré Goddard Rosa bool is_sign; 34371da177e4SLinus Torvalds 3438da99075cSJan Beulich while (*fmt) { 34391da177e4SLinus Torvalds /* skip any white space in format */ 34409dbbc3b9SZhen Lei /* white space in format matches any amount of 34411da177e4SLinus Torvalds * white space, including none, in the input. 34421da177e4SLinus Torvalds */ 34431da177e4SLinus Torvalds if (isspace(*fmt)) { 3444e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 3445e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 34461da177e4SLinus Torvalds } 34471da177e4SLinus Torvalds 34481da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 34491da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 34501da177e4SLinus Torvalds if (*fmt++ != *str++) 34511da177e4SLinus Torvalds break; 34521da177e4SLinus Torvalds continue; 34531da177e4SLinus Torvalds } 34541da177e4SLinus Torvalds 34551da177e4SLinus Torvalds if (!*fmt) 34561da177e4SLinus Torvalds break; 34571da177e4SLinus Torvalds ++fmt; 34581da177e4SLinus Torvalds 34591da177e4SLinus Torvalds /* skip this conversion. 34601da177e4SLinus Torvalds * advance both strings to next white space 34611da177e4SLinus Torvalds */ 34621da177e4SLinus Torvalds if (*fmt == '*') { 3463da99075cSJan Beulich if (!*str) 3464da99075cSJan Beulich break; 3465f9310b2fSJessica Yu while (!isspace(*fmt) && *fmt != '%' && *fmt) { 3466f9310b2fSJessica Yu /* '%*[' not yet supported, invalid format */ 3467f9310b2fSJessica Yu if (*fmt == '[') 3468f9310b2fSJessica Yu return num; 34691da177e4SLinus Torvalds fmt++; 3470f9310b2fSJessica Yu } 34711da177e4SLinus Torvalds while (!isspace(*str) && *str) 34721da177e4SLinus Torvalds str++; 34731da177e4SLinus Torvalds continue; 34741da177e4SLinus Torvalds } 34751da177e4SLinus Torvalds 34761da177e4SLinus Torvalds /* get field width */ 34771da177e4SLinus Torvalds field_width = -1; 347853809751SJan Beulich if (isdigit(*fmt)) { 34791da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 348053809751SJan Beulich if (field_width <= 0) 348153809751SJan Beulich break; 348253809751SJan Beulich } 34831da177e4SLinus Torvalds 34841da177e4SLinus Torvalds /* get conversion qualifier */ 34851da177e4SLinus Torvalds qualifier = -1; 348675fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 34875b5e0928SAlexey Dobriyan *fmt == 'z') { 34881da177e4SLinus Torvalds qualifier = *fmt++; 34891da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 34901da177e4SLinus Torvalds if (qualifier == 'h') { 34911da177e4SLinus Torvalds qualifier = 'H'; 34921da177e4SLinus Torvalds fmt++; 34931da177e4SLinus Torvalds } else if (qualifier == 'l') { 34941da177e4SLinus Torvalds qualifier = 'L'; 34951da177e4SLinus Torvalds fmt++; 34961da177e4SLinus Torvalds } 34971da177e4SLinus Torvalds } 34981da177e4SLinus Torvalds } 34991da177e4SLinus Torvalds 3500da99075cSJan Beulich if (!*fmt) 3501da99075cSJan Beulich break; 3502da99075cSJan Beulich 3503da99075cSJan Beulich if (*fmt == 'n') { 3504da99075cSJan Beulich /* return number of characters read so far */ 3505da99075cSJan Beulich *va_arg(args, int *) = str - buf; 3506da99075cSJan Beulich ++fmt; 3507da99075cSJan Beulich continue; 3508da99075cSJan Beulich } 3509da99075cSJan Beulich 3510da99075cSJan Beulich if (!*str) 35111da177e4SLinus Torvalds break; 35121da177e4SLinus Torvalds 3513d4be151bSAndré Goddard Rosa base = 10; 35143f623ebaSFabian Frederick is_sign = false; 3515d4be151bSAndré Goddard Rosa 35161da177e4SLinus Torvalds switch (*fmt++) { 35171da177e4SLinus Torvalds case 'c': 35181da177e4SLinus Torvalds { 35191da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 35201da177e4SLinus Torvalds if (field_width == -1) 35211da177e4SLinus Torvalds field_width = 1; 35221da177e4SLinus Torvalds do { 35231da177e4SLinus Torvalds *s++ = *str++; 35241da177e4SLinus Torvalds } while (--field_width > 0 && *str); 35251da177e4SLinus Torvalds num++; 35261da177e4SLinus Torvalds } 35271da177e4SLinus Torvalds continue; 35281da177e4SLinus Torvalds case 's': 35291da177e4SLinus Torvalds { 35301da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 35311da177e4SLinus Torvalds if (field_width == -1) 35324be929beSAlexey Dobriyan field_width = SHRT_MAX; 35331da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 3534e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 35351da177e4SLinus Torvalds 35361da177e4SLinus Torvalds /* now copy until next white space */ 35377b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 35381da177e4SLinus Torvalds *s++ = *str++; 35391da177e4SLinus Torvalds *s = '\0'; 35401da177e4SLinus Torvalds num++; 35411da177e4SLinus Torvalds } 35421da177e4SLinus Torvalds continue; 3543f9310b2fSJessica Yu /* 3544f9310b2fSJessica Yu * Warning: This implementation of the '[' conversion specifier 3545f9310b2fSJessica Yu * deviates from its glibc counterpart in the following ways: 3546f9310b2fSJessica Yu * (1) It does NOT support ranges i.e. '-' is NOT a special 3547f9310b2fSJessica Yu * character 3548f9310b2fSJessica Yu * (2) It cannot match the closing bracket ']' itself 3549f9310b2fSJessica Yu * (3) A field width is required 3550f9310b2fSJessica Yu * (4) '%*[' (discard matching input) is currently not supported 3551f9310b2fSJessica Yu * 3552f9310b2fSJessica Yu * Example usage: 3553f9310b2fSJessica Yu * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3554f9310b2fSJessica Yu * buf1, buf2, buf3); 3555f9310b2fSJessica Yu * if (ret < 3) 3556f9310b2fSJessica Yu * // etc.. 3557f9310b2fSJessica Yu */ 3558f9310b2fSJessica Yu case '[': 3559f9310b2fSJessica Yu { 3560f9310b2fSJessica Yu char *s = (char *)va_arg(args, char *); 3561f9310b2fSJessica Yu DECLARE_BITMAP(set, 256) = {0}; 3562f9310b2fSJessica Yu unsigned int len = 0; 3563f9310b2fSJessica Yu bool negate = (*fmt == '^'); 3564f9310b2fSJessica Yu 3565f9310b2fSJessica Yu /* field width is required */ 3566f9310b2fSJessica Yu if (field_width == -1) 3567f9310b2fSJessica Yu return num; 3568f9310b2fSJessica Yu 3569f9310b2fSJessica Yu if (negate) 3570f9310b2fSJessica Yu ++fmt; 3571f9310b2fSJessica Yu 3572f9310b2fSJessica Yu for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 357352e68cd6SChristophe JAILLET __set_bit((u8)*fmt, set); 3574f9310b2fSJessica Yu 3575f9310b2fSJessica Yu /* no ']' or no character set found */ 3576f9310b2fSJessica Yu if (!*fmt || !len) 3577f9310b2fSJessica Yu return num; 3578f9310b2fSJessica Yu ++fmt; 3579f9310b2fSJessica Yu 3580f9310b2fSJessica Yu if (negate) { 3581f9310b2fSJessica Yu bitmap_complement(set, set, 256); 3582f9310b2fSJessica Yu /* exclude null '\0' byte */ 358352e68cd6SChristophe JAILLET __clear_bit(0, set); 3584f9310b2fSJessica Yu } 3585f9310b2fSJessica Yu 3586f9310b2fSJessica Yu /* match must be non-empty */ 3587f9310b2fSJessica Yu if (!test_bit((u8)*str, set)) 3588f9310b2fSJessica Yu return num; 3589f9310b2fSJessica Yu 3590f9310b2fSJessica Yu while (test_bit((u8)*str, set) && field_width--) 3591f9310b2fSJessica Yu *s++ = *str++; 3592f9310b2fSJessica Yu *s = '\0'; 3593f9310b2fSJessica Yu ++num; 3594f9310b2fSJessica Yu } 3595f9310b2fSJessica Yu continue; 35961da177e4SLinus Torvalds case 'o': 35971da177e4SLinus Torvalds base = 8; 35981da177e4SLinus Torvalds break; 35991da177e4SLinus Torvalds case 'x': 36001da177e4SLinus Torvalds case 'X': 36011da177e4SLinus Torvalds base = 16; 36021da177e4SLinus Torvalds break; 36031da177e4SLinus Torvalds case 'i': 36041da177e4SLinus Torvalds base = 0; 36054c1ca831SNick Desaulniers fallthrough; 36061da177e4SLinus Torvalds case 'd': 36073f623ebaSFabian Frederick is_sign = true; 36084c1ca831SNick Desaulniers fallthrough; 36091da177e4SLinus Torvalds case 'u': 36101da177e4SLinus Torvalds break; 36111da177e4SLinus Torvalds case '%': 36121da177e4SLinus Torvalds /* looking for '%' in str */ 36131da177e4SLinus Torvalds if (*str++ != '%') 36141da177e4SLinus Torvalds return num; 36151da177e4SLinus Torvalds continue; 36161da177e4SLinus Torvalds default: 36171da177e4SLinus Torvalds /* invalid format; stop here */ 36181da177e4SLinus Torvalds return num; 36191da177e4SLinus Torvalds } 36201da177e4SLinus Torvalds 36211da177e4SLinus Torvalds /* have some sort of integer conversion. 36221da177e4SLinus Torvalds * first, skip white space in buffer. 36231da177e4SLinus Torvalds */ 3624e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 36251da177e4SLinus Torvalds 36261da177e4SLinus Torvalds digit = *str; 362711b3dda5SRichard Fitzgerald if (is_sign && digit == '-') { 362811b3dda5SRichard Fitzgerald if (field_width == 1) 362911b3dda5SRichard Fitzgerald break; 363011b3dda5SRichard Fitzgerald 36311da177e4SLinus Torvalds digit = *(str + 1); 363211b3dda5SRichard Fitzgerald } 36331da177e4SLinus Torvalds 36341da177e4SLinus Torvalds if (!digit 36351da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 36361da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 36371da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 36381da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 36391da177e4SLinus Torvalds break; 36401da177e4SLinus Torvalds 364153809751SJan Beulich if (is_sign) 3642900fdc45SRichard Fitzgerald val.s = simple_strntoll(str, 3643900fdc45SRichard Fitzgerald field_width >= 0 ? field_width : INT_MAX, 3644900fdc45SRichard Fitzgerald &next, base); 364553809751SJan Beulich else 3646900fdc45SRichard Fitzgerald val.u = simple_strntoull(str, 3647900fdc45SRichard Fitzgerald field_width >= 0 ? field_width : INT_MAX, 3648900fdc45SRichard Fitzgerald &next, base); 364953809751SJan Beulich 36501da177e4SLinus Torvalds switch (qualifier) { 36511da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 365253809751SJan Beulich if (is_sign) 365353809751SJan Beulich *va_arg(args, signed char *) = val.s; 365453809751SJan Beulich else 365553809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 36561da177e4SLinus Torvalds break; 36571da177e4SLinus Torvalds case 'h': 365853809751SJan Beulich if (is_sign) 365953809751SJan Beulich *va_arg(args, short *) = val.s; 366053809751SJan Beulich else 366153809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 36621da177e4SLinus Torvalds break; 36631da177e4SLinus Torvalds case 'l': 366453809751SJan Beulich if (is_sign) 366553809751SJan Beulich *va_arg(args, long *) = val.s; 366653809751SJan Beulich else 366753809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 36681da177e4SLinus Torvalds break; 36691da177e4SLinus Torvalds case 'L': 367053809751SJan Beulich if (is_sign) 367153809751SJan Beulich *va_arg(args, long long *) = val.s; 367253809751SJan Beulich else 367353809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 36741da177e4SLinus Torvalds break; 36751da177e4SLinus Torvalds case 'z': 367653809751SJan Beulich *va_arg(args, size_t *) = val.u; 36771da177e4SLinus Torvalds break; 36781da177e4SLinus Torvalds default: 367953809751SJan Beulich if (is_sign) 368053809751SJan Beulich *va_arg(args, int *) = val.s; 368153809751SJan Beulich else 368253809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 36831da177e4SLinus Torvalds break; 36841da177e4SLinus Torvalds } 36851da177e4SLinus Torvalds num++; 36861da177e4SLinus Torvalds 36871da177e4SLinus Torvalds if (!next) 36881da177e4SLinus Torvalds break; 36891da177e4SLinus Torvalds str = next; 36901da177e4SLinus Torvalds } 3691c6b40d16SJohannes Berg 36921da177e4SLinus Torvalds return num; 36931da177e4SLinus Torvalds } 36941da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 36951da177e4SLinus Torvalds 36961da177e4SLinus Torvalds /** 36971da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 36981da177e4SLinus Torvalds * @buf: input buffer 36991da177e4SLinus Torvalds * @fmt: formatting of buffer 37001da177e4SLinus Torvalds * @...: resulting arguments 37011da177e4SLinus Torvalds */ 37021da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 37031da177e4SLinus Torvalds { 37041da177e4SLinus Torvalds va_list args; 37051da177e4SLinus Torvalds int i; 37061da177e4SLinus Torvalds 37071da177e4SLinus Torvalds va_start(args, fmt); 37081da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 37091da177e4SLinus Torvalds va_end(args); 37107b9186f5SAndré Goddard Rosa 37111da177e4SLinus Torvalds return i; 37121da177e4SLinus Torvalds } 37131da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 3714