11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/lib/vsprintf.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * Wirzenius wrote this portably, Torvalds fucked it up :-) 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> 141da177e4SLinus Torvalds * - changed to provide snprintf and vsnprintf functions 151da177e4SLinus Torvalds * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> 161da177e4SLinus Torvalds * - scnprintf and vscnprintf 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <stdarg.h> 200d1d7a55SStephen Boyd #include <linux/clk.h> 21900cca29SGeert Uytterhoeven #include <linux/clk-provider.h> 228bc3bcc9SPaul Gortmaker #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ 231da177e4SLinus Torvalds #include <linux/types.h> 241da177e4SLinus Torvalds #include <linux/string.h> 251da177e4SLinus Torvalds #include <linux/ctype.h> 261da177e4SLinus Torvalds #include <linux/kernel.h> 270fe1ef24SLinus Torvalds #include <linux/kallsyms.h> 2853809751SJan Beulich #include <linux/math64.h> 290fe1ef24SLinus Torvalds #include <linux/uaccess.h> 30332d2e78SLinus Torvalds #include <linux/ioport.h> 314b6ccca7SAl Viro #include <linux/dcache.h> 32312b4e22SRyan Mallon #include <linux/cred.h> 332b1b0d66SAndy Shevchenko #include <linux/uuid.h> 34ce4fecf1SPantelis Antoniou #include <linux/of.h> 358a27f7c9SJoe Perches #include <net/addrconf.h> 36ad67b74dSTobin C. Harding #include <linux/siphash.h> 37ad67b74dSTobin C. Harding #include <linux/compiler.h> 381031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 391031bc58SDmitry Monakhov #include <linux/blkdev.h> 401031bc58SDmitry Monakhov #endif 411da177e4SLinus Torvalds 42edf14cdbSVlastimil Babka #include "../mm/internal.h" /* For the trace_print_flags arrays */ 43edf14cdbSVlastimil Babka 444e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 457c43d9a3SRasmus Villemoes #include <asm/byteorder.h> /* cpu_to_le16 */ 461da177e4SLinus Torvalds 4771dca95dSAndy Shevchenko #include <linux/string_helpers.h> 481dff46d6SAlexey Dobriyan #include "kstrtox.h" 49aa46a63eSHarvey Harrison 501da177e4SLinus Torvalds /** 511da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 521da177e4SLinus Torvalds * @cp: The start of the string 531da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 541da177e4SLinus Torvalds * @base: The number base to use 55462e4711SEldad Zack * 56462e4711SEldad Zack * This function is obsolete. Please use kstrtoull instead. 571da177e4SLinus Torvalds */ 581da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 591da177e4SLinus Torvalds { 601dff46d6SAlexey Dobriyan unsigned long long result; 611dff46d6SAlexey Dobriyan unsigned int rv; 621da177e4SLinus Torvalds 631dff46d6SAlexey Dobriyan cp = _parse_integer_fixup_radix(cp, &base); 641dff46d6SAlexey Dobriyan rv = _parse_integer(cp, base, &result); 651dff46d6SAlexey Dobriyan /* FIXME */ 661dff46d6SAlexey Dobriyan cp += (rv & ~KSTRTOX_OVERFLOW); 67aa46a63eSHarvey Harrison 681da177e4SLinus Torvalds if (endp) 691da177e4SLinus Torvalds *endp = (char *)cp; 707b9186f5SAndré Goddard Rosa 711da177e4SLinus Torvalds return result; 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /** 76922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 77922ac25cSAndré Goddard Rosa * @cp: The start of the string 78922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 79922ac25cSAndré Goddard Rosa * @base: The number base to use 80462e4711SEldad Zack * 81462e4711SEldad Zack * This function is obsolete. Please use kstrtoul instead. 82922ac25cSAndré Goddard Rosa */ 83922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 84922ac25cSAndré Goddard Rosa { 85922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 86922ac25cSAndré Goddard Rosa } 87922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 88922ac25cSAndré Goddard Rosa 89922ac25cSAndré Goddard Rosa /** 90922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 91922ac25cSAndré Goddard Rosa * @cp: The start of the string 92922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 93922ac25cSAndré Goddard Rosa * @base: The number base to use 94462e4711SEldad Zack * 95462e4711SEldad Zack * This function is obsolete. Please use kstrtol instead. 96922ac25cSAndré Goddard Rosa */ 97922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 98922ac25cSAndré Goddard Rosa { 99922ac25cSAndré Goddard Rosa if (*cp == '-') 100922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 101922ac25cSAndré Goddard Rosa 102922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 103922ac25cSAndré Goddard Rosa } 104922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 105922ac25cSAndré Goddard Rosa 106922ac25cSAndré Goddard Rosa /** 1071da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1081da177e4SLinus Torvalds * @cp: The start of the string 1091da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1101da177e4SLinus Torvalds * @base: The number base to use 111462e4711SEldad Zack * 112462e4711SEldad Zack * This function is obsolete. Please use kstrtoll instead. 1131da177e4SLinus Torvalds */ 1141da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1151da177e4SLinus Torvalds { 1161da177e4SLinus Torvalds if (*cp == '-') 1171da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1187b9186f5SAndré Goddard Rosa 1191da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1201da177e4SLinus Torvalds } 12198d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1221da177e4SLinus Torvalds 123cf3b429bSJoe Perches static noinline_for_stack 124cf3b429bSJoe Perches int skip_atoi(const char **s) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds int i = 0; 1271da177e4SLinus Torvalds 12843e5b666SRasmus Villemoes do { 1291da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 13043e5b666SRasmus Villemoes } while (isdigit(**s)); 1317b9186f5SAndré Goddard Rosa 1321da177e4SLinus Torvalds return i; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1357c43d9a3SRasmus Villemoes /* 1367c43d9a3SRasmus Villemoes * Decimal conversion is by far the most typical, and is used for 1377c43d9a3SRasmus Villemoes * /proc and /sys data. This directly impacts e.g. top performance 1387c43d9a3SRasmus Villemoes * with many processes running. We optimize it for speed by emitting 1397c43d9a3SRasmus Villemoes * two characters at a time, using a 200 byte lookup table. This 1407c43d9a3SRasmus Villemoes * roughly halves the number of multiplications compared to computing 1417c43d9a3SRasmus Villemoes * the digits one at a time. Implementation strongly inspired by the 1427c43d9a3SRasmus Villemoes * previous version, which in turn used ideas described at 1437c43d9a3SRasmus Villemoes * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 1447c43d9a3SRasmus Villemoes * from the author, Douglas W. Jones). 1457c43d9a3SRasmus Villemoes * 1467c43d9a3SRasmus Villemoes * It turns out there is precisely one 26 bit fixed-point 1477c43d9a3SRasmus Villemoes * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 1487c43d9a3SRasmus Villemoes * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 1497c43d9a3SRasmus Villemoes * range happens to be somewhat larger (x <= 1073741898), but that's 1507c43d9a3SRasmus Villemoes * irrelevant for our purpose. 1517c43d9a3SRasmus Villemoes * 1527c43d9a3SRasmus Villemoes * For dividing a number in the range [10^4, 10^6-1] by 100, we still 1537c43d9a3SRasmus Villemoes * need a 32x32->64 bit multiply, so we simply use the same constant. 1547c43d9a3SRasmus Villemoes * 1557c43d9a3SRasmus Villemoes * For dividing a number in the range [100, 10^4-1] by 100, there are 1567c43d9a3SRasmus Villemoes * several options. The simplest is (x * 0x147b) >> 19, which is valid 1577c43d9a3SRasmus Villemoes * for all x <= 43698. 158133fd9f5SDenys Vlasenko */ 1594277eeddSDenis Vlasenko 1607c43d9a3SRasmus Villemoes static const u16 decpair[100] = { 1617c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 1627c43d9a3SRasmus Villemoes _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 1637c43d9a3SRasmus Villemoes _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 1647c43d9a3SRasmus Villemoes _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 1657c43d9a3SRasmus Villemoes _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 1667c43d9a3SRasmus Villemoes _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 1677c43d9a3SRasmus Villemoes _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 1687c43d9a3SRasmus Villemoes _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 1697c43d9a3SRasmus Villemoes _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 1707c43d9a3SRasmus Villemoes _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 1717c43d9a3SRasmus Villemoes _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 1727c43d9a3SRasmus Villemoes #undef _ 1737c43d9a3SRasmus Villemoes }; 1744277eeddSDenis Vlasenko 1757b9186f5SAndré Goddard Rosa /* 1767c43d9a3SRasmus Villemoes * This will print a single '0' even if r == 0, since we would 177675cf53cSRasmus Villemoes * immediately jump to out_r where two 0s would be written but only 178675cf53cSRasmus Villemoes * one of them accounted for in buf. This is needed by ip4_string 179675cf53cSRasmus Villemoes * below. All other callers pass a non-zero value of r. 180133fd9f5SDenys Vlasenko */ 181133fd9f5SDenys Vlasenko static noinline_for_stack 182133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 183133fd9f5SDenys Vlasenko { 184133fd9f5SDenys Vlasenko unsigned q; 1854277eeddSDenis Vlasenko 1867c43d9a3SRasmus Villemoes /* 1 <= r < 10^8 */ 1877c43d9a3SRasmus Villemoes if (r < 100) 1887c43d9a3SRasmus Villemoes goto out_r; 189cb239d0aSGeorge Spelvin 1907c43d9a3SRasmus Villemoes /* 100 <= r < 10^8 */ 1917c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 1927c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 1937c43d9a3SRasmus Villemoes buf += 2; 1947c43d9a3SRasmus Villemoes 1957c43d9a3SRasmus Villemoes /* 1 <= q < 10^6 */ 1967c43d9a3SRasmus Villemoes if (q < 100) 1977c43d9a3SRasmus Villemoes goto out_q; 1987c43d9a3SRasmus Villemoes 1997c43d9a3SRasmus Villemoes /* 100 <= q < 10^6 */ 2007c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2017c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2027c43d9a3SRasmus Villemoes buf += 2; 2037c43d9a3SRasmus Villemoes 2047c43d9a3SRasmus Villemoes /* 1 <= r < 10^4 */ 2057c43d9a3SRasmus Villemoes if (r < 100) 2067c43d9a3SRasmus Villemoes goto out_r; 2077c43d9a3SRasmus Villemoes 2087c43d9a3SRasmus Villemoes /* 100 <= r < 10^4 */ 2097c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2107c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2117c43d9a3SRasmus Villemoes buf += 2; 2127c43d9a3SRasmus Villemoes out_q: 2137c43d9a3SRasmus Villemoes /* 1 <= q < 100 */ 2147c43d9a3SRasmus Villemoes r = q; 2157c43d9a3SRasmus Villemoes out_r: 2167c43d9a3SRasmus Villemoes /* 1 <= r < 100 */ 2177c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r]; 218675cf53cSRasmus Villemoes buf += r < 10 ? 1 : 2; 219133fd9f5SDenys Vlasenko return buf; 220133fd9f5SDenys Vlasenko } 221133fd9f5SDenys Vlasenko 2227c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 2237c43d9a3SRasmus Villemoes static noinline_for_stack 2247c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r) 2257c43d9a3SRasmus Villemoes { 2267c43d9a3SRasmus Villemoes unsigned q; 227133fd9f5SDenys Vlasenko 2287c43d9a3SRasmus Villemoes /* 0 <= r < 10^8 */ 2297c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2307c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2317c43d9a3SRasmus Villemoes buf += 2; 232133fd9f5SDenys Vlasenko 2337c43d9a3SRasmus Villemoes /* 0 <= q < 10^6 */ 2347c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2357c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2367c43d9a3SRasmus Villemoes buf += 2; 237133fd9f5SDenys Vlasenko 2387c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2397c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2407c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2417c43d9a3SRasmus Villemoes buf += 2; 2427c43d9a3SRasmus Villemoes 2437c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2447c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2457c43d9a3SRasmus Villemoes buf += 2; 2467c43d9a3SRasmus Villemoes return buf; 2477c43d9a3SRasmus Villemoes } 2487c43d9a3SRasmus Villemoes 2497c43d9a3SRasmus Villemoes static noinline_for_stack 250133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 251133fd9f5SDenys Vlasenko { 252133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 2537c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2547c43d9a3SRasmus Villemoes /* 1 <= n <= 1.6e11 */ 2557c43d9a3SRasmus Villemoes if (n >= 100*1000*1000) 2567c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2577c43d9a3SRasmus Villemoes /* 1 <= n < 1e8 */ 258133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 259133fd9f5SDenys Vlasenko } 260133fd9f5SDenys Vlasenko 2617c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 262133fd9f5SDenys Vlasenko 2637c43d9a3SRasmus Villemoes static void 2647c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r) 265133fd9f5SDenys Vlasenko { 2667c43d9a3SRasmus Villemoes unsigned q; 2677c43d9a3SRasmus Villemoes 2687c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2697c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2707c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2717c43d9a3SRasmus Villemoes buf += 2; 2727c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2737c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2742359172aSGeorge Spelvin } 2752359172aSGeorge Spelvin 2762359172aSGeorge Spelvin /* 2772359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 2782359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 2792359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 2802359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 2817c43d9a3SRasmus Villemoes * (second call in the put_dec code, assuming n is all-ones). 2822359172aSGeorge Spelvin */ 2837c43d9a3SRasmus Villemoes static noinline_for_stack 2842359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 2852359172aSGeorge Spelvin { 2862359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 2872359172aSGeorge Spelvin 2882359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 2892359172aSGeorge Spelvin return q; 290133fd9f5SDenys Vlasenko } 291133fd9f5SDenys Vlasenko 292133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 293133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 294133fd9f5SDenys Vlasenko * (with permission from the author). 295133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 296133fd9f5SDenys Vlasenko */ 297133fd9f5SDenys Vlasenko static 298133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 299133fd9f5SDenys Vlasenko { 300133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 301133fd9f5SDenys Vlasenko 302133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 303133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 304133fd9f5SDenys Vlasenko 305133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 306133fd9f5SDenys Vlasenko h = (n >> 32); 307133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 308133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 309133fd9f5SDenys Vlasenko 3107c43d9a3SRasmus Villemoes /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 3117c43d9a3SRasmus Villemoes = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 312133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 3132359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 314133fd9f5SDenys Vlasenko 3152359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 3162359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 317133fd9f5SDenys Vlasenko 3182359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3192359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 320133fd9f5SDenys Vlasenko 3212359172aSGeorge Spelvin q += 281 * d3; 3222359172aSGeorge Spelvin buf += 12; 3232359172aSGeorge Spelvin if (q) 3242359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3252359172aSGeorge Spelvin else while (buf[-1] == '0') 326133fd9f5SDenys Vlasenko --buf; 3277b9186f5SAndré Goddard Rosa 3284277eeddSDenis Vlasenko return buf; 3294277eeddSDenis Vlasenko } 330133fd9f5SDenys Vlasenko 331133fd9f5SDenys Vlasenko #endif 3324277eeddSDenis Vlasenko 3331ac101a5SKAMEZAWA Hiroyuki /* 3341ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3351ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3361ac101a5SKAMEZAWA Hiroyuki * 3371ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3381ac101a5SKAMEZAWA Hiroyuki */ 339d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) 3401ac101a5SKAMEZAWA Hiroyuki { 3417c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 3427c43d9a3SRasmus Villemoes char tmp[sizeof(num) * 3] __aligned(2); 3431ac101a5SKAMEZAWA Hiroyuki int idx, len; 3441ac101a5SKAMEZAWA Hiroyuki 345133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 346133fd9f5SDenys Vlasenko if (num <= 9) { 347133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 348133fd9f5SDenys Vlasenko len = 1; 349133fd9f5SDenys Vlasenko } else { 3501ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 351133fd9f5SDenys Vlasenko } 3521ac101a5SKAMEZAWA Hiroyuki 353d1be35cbSAndrei Vagin if (len > size || width > size) 3541ac101a5SKAMEZAWA Hiroyuki return 0; 355d1be35cbSAndrei Vagin 356d1be35cbSAndrei Vagin if (width > len) { 357d1be35cbSAndrei Vagin width = width - len; 358d1be35cbSAndrei Vagin for (idx = 0; idx < width; idx++) 359d1be35cbSAndrei Vagin buf[idx] = ' '; 360d1be35cbSAndrei Vagin } else { 361d1be35cbSAndrei Vagin width = 0; 362d1be35cbSAndrei Vagin } 363d1be35cbSAndrei Vagin 3641ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 365d1be35cbSAndrei Vagin buf[idx + width] = tmp[len - idx - 1]; 366d1be35cbSAndrei Vagin 367d1be35cbSAndrei Vagin return len + width; 3681ac101a5SKAMEZAWA Hiroyuki } 3691ac101a5SKAMEZAWA Hiroyuki 37051be17dfSRasmus Villemoes #define SIGN 1 /* unsigned/signed, must be 1 */ 371d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */ 3721da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3731da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 374d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 375b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 376b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 3771da177e4SLinus Torvalds 378fef20d9cSFrederic Weisbecker enum format_type { 379fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 380ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 381fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 382fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 383fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 384fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 385fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 386fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 387fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 388fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 389fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 390a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 391a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 392fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 393fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 394fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 395fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 396fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 397fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 398fef20d9cSFrederic Weisbecker }; 399fef20d9cSFrederic Weisbecker 400fef20d9cSFrederic Weisbecker struct printf_spec { 401d0484193SRasmus Villemoes unsigned int type:8; /* format_type enum */ 402d0484193SRasmus Villemoes signed int field_width:24; /* width of output field */ 403d0484193SRasmus Villemoes unsigned int flags:8; /* flags to number() */ 404d0484193SRasmus Villemoes unsigned int base:8; /* number base, 8, 10 or 16 only */ 405d0484193SRasmus Villemoes signed int precision:16; /* # of digits/chars */ 406d0484193SRasmus Villemoes } __packed; 4074d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1) 4084d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1) 409fef20d9cSFrederic Weisbecker 410cf3b429bSJoe Perches static noinline_for_stack 411cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 412fef20d9cSFrederic Weisbecker struct printf_spec spec) 4131da177e4SLinus Torvalds { 4147c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 4157c43d9a3SRasmus Villemoes char tmp[3 * sizeof(num)] __aligned(2); 4169b706aeeSDenys Vlasenko char sign; 4179b706aeeSDenys Vlasenko char locase; 418fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4191da177e4SLinus Torvalds int i; 4207c203422SPierre Carrier bool is_zero = num == 0LL; 4211c7a8e62SRasmus Villemoes int field_width = spec.field_width; 4221c7a8e62SRasmus Villemoes int precision = spec.precision; 4231da177e4SLinus Torvalds 424d0484193SRasmus Villemoes BUILD_BUG_ON(sizeof(struct printf_spec) != 8); 425d0484193SRasmus Villemoes 4269b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4279b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 428fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 429fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 430fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4311da177e4SLinus Torvalds sign = 0; 432fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4331da177e4SLinus Torvalds if ((signed long long)num < 0) { 4341da177e4SLinus Torvalds sign = '-'; 4351da177e4SLinus Torvalds num = -(signed long long)num; 4361c7a8e62SRasmus Villemoes field_width--; 437fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4381da177e4SLinus Torvalds sign = '+'; 4391c7a8e62SRasmus Villemoes field_width--; 440fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4411da177e4SLinus Torvalds sign = ' '; 4421c7a8e62SRasmus Villemoes field_width--; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds } 445b39a7340SDenis Vlasenko if (need_pfx) { 446fef20d9cSFrederic Weisbecker if (spec.base == 16) 4471c7a8e62SRasmus Villemoes field_width -= 2; 4487c203422SPierre Carrier else if (!is_zero) 4491c7a8e62SRasmus Villemoes field_width--; 4501da177e4SLinus Torvalds } 451b39a7340SDenis Vlasenko 452b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4531da177e4SLinus Torvalds i = 0; 454133fd9f5SDenys Vlasenko if (num < spec.base) 4553ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase; 456fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 457fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 458b39a7340SDenis Vlasenko int shift = 3; 4597b9186f5SAndré Goddard Rosa 4607b9186f5SAndré Goddard Rosa if (spec.base == 16) 4617b9186f5SAndré Goddard Rosa shift = 4; 462b39a7340SDenis Vlasenko do { 4633ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 464b39a7340SDenis Vlasenko num >>= shift; 465b39a7340SDenis Vlasenko } while (num); 4664277eeddSDenis Vlasenko } else { /* base 10 */ 4674277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4684277eeddSDenis Vlasenko } 469b39a7340SDenis Vlasenko 470b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 4711c7a8e62SRasmus Villemoes if (i > precision) 4721c7a8e62SRasmus Villemoes precision = i; 473b39a7340SDenis Vlasenko /* leading space padding */ 4741c7a8e62SRasmus Villemoes field_width -= precision; 47551be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) { 4761c7a8e62SRasmus Villemoes while (--field_width >= 0) { 477f796937aSJeremy Fitzhardinge if (buf < end) 4781da177e4SLinus Torvalds *buf = ' '; 4791da177e4SLinus Torvalds ++buf; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds } 482b39a7340SDenis Vlasenko /* sign */ 4831da177e4SLinus Torvalds if (sign) { 484f796937aSJeremy Fitzhardinge if (buf < end) 4851da177e4SLinus Torvalds *buf = sign; 4861da177e4SLinus Torvalds ++buf; 4871da177e4SLinus Torvalds } 488b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 489b39a7340SDenis Vlasenko if (need_pfx) { 4907c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 491f796937aSJeremy Fitzhardinge if (buf < end) 4921da177e4SLinus Torvalds *buf = '0'; 4931da177e4SLinus Torvalds ++buf; 4947c203422SPierre Carrier } 495fef20d9cSFrederic Weisbecker if (spec.base == 16) { 496f796937aSJeremy Fitzhardinge if (buf < end) 4979b706aeeSDenys Vlasenko *buf = ('X' | locase); 4981da177e4SLinus Torvalds ++buf; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds } 501b39a7340SDenis Vlasenko /* zero or space padding */ 502fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 503d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD); 504d1c1b121SRasmus Villemoes BUILD_BUG_ON(' ' + ZEROPAD != '0'); 5051c7a8e62SRasmus Villemoes while (--field_width >= 0) { 506f796937aSJeremy Fitzhardinge if (buf < end) 5071da177e4SLinus Torvalds *buf = c; 5081da177e4SLinus Torvalds ++buf; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds } 511b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 5121c7a8e62SRasmus Villemoes while (i <= --precision) { 513f796937aSJeremy Fitzhardinge if (buf < end) 5141da177e4SLinus Torvalds *buf = '0'; 5151da177e4SLinus Torvalds ++buf; 5161da177e4SLinus Torvalds } 517b39a7340SDenis Vlasenko /* actual digits of result */ 518b39a7340SDenis Vlasenko while (--i >= 0) { 519f796937aSJeremy Fitzhardinge if (buf < end) 5201da177e4SLinus Torvalds *buf = tmp[i]; 5211da177e4SLinus Torvalds ++buf; 5221da177e4SLinus Torvalds } 523b39a7340SDenis Vlasenko /* trailing space padding */ 5241c7a8e62SRasmus Villemoes while (--field_width >= 0) { 525f796937aSJeremy Fitzhardinge if (buf < end) 5261da177e4SLinus Torvalds *buf = ' '; 5271da177e4SLinus Torvalds ++buf; 5281da177e4SLinus Torvalds } 5297b9186f5SAndré Goddard Rosa 5301da177e4SLinus Torvalds return buf; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5333cab1e71SAndy Shevchenko static noinline_for_stack 5343cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size) 5353cab1e71SAndy Shevchenko { 5363cab1e71SAndy Shevchenko struct printf_spec spec; 5373cab1e71SAndy Shevchenko 5383cab1e71SAndy Shevchenko spec.type = FORMAT_TYPE_PTR; 5393cab1e71SAndy Shevchenko spec.field_width = 2 + 2 * size; /* 0x + hex */ 5403cab1e71SAndy Shevchenko spec.flags = SPECIAL | SMALL | ZEROPAD; 5413cab1e71SAndy Shevchenko spec.base = 16; 5423cab1e71SAndy Shevchenko spec.precision = -1; 5433cab1e71SAndy Shevchenko 5443cab1e71SAndy Shevchenko return number(buf, end, num, spec); 5453cab1e71SAndy Shevchenko } 5463cab1e71SAndy Shevchenko 547cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces) 5484b6ccca7SAl Viro { 5494b6ccca7SAl Viro size_t size; 5504b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5514b6ccca7SAl Viro return; 5524b6ccca7SAl Viro size = end - buf; 5534b6ccca7SAl Viro if (size <= spaces) { 5544b6ccca7SAl Viro memset(buf, ' ', size); 5554b6ccca7SAl Viro return; 5564b6ccca7SAl Viro } 5574b6ccca7SAl Viro if (len) { 5584b6ccca7SAl Viro if (len > size - spaces) 5594b6ccca7SAl Viro len = size - spaces; 5604b6ccca7SAl Viro memmove(buf + spaces, buf, len); 5614b6ccca7SAl Viro } 5624b6ccca7SAl Viro memset(buf, ' ', spaces); 5634b6ccca7SAl Viro } 5644b6ccca7SAl Viro 565cfccde04SRasmus Villemoes /* 566cfccde04SRasmus Villemoes * Handle field width padding for a string. 567cfccde04SRasmus Villemoes * @buf: current buffer position 568cfccde04SRasmus Villemoes * @n: length of string 569cfccde04SRasmus Villemoes * @end: end of output buffer 570cfccde04SRasmus Villemoes * @spec: for field width and flags 571cfccde04SRasmus Villemoes * Returns: new buffer position after padding. 572cfccde04SRasmus Villemoes */ 573cfccde04SRasmus Villemoes static noinline_for_stack 574cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec) 575cfccde04SRasmus Villemoes { 576cfccde04SRasmus Villemoes unsigned spaces; 577cfccde04SRasmus Villemoes 578cfccde04SRasmus Villemoes if (likely(n >= spec.field_width)) 579cfccde04SRasmus Villemoes return buf; 580cfccde04SRasmus Villemoes /* we want to pad the sucker */ 581cfccde04SRasmus Villemoes spaces = spec.field_width - n; 582cfccde04SRasmus Villemoes if (!(spec.flags & LEFT)) { 583cfccde04SRasmus Villemoes move_right(buf - n, end, n, spaces); 584cfccde04SRasmus Villemoes return buf + spaces; 585cfccde04SRasmus Villemoes } 586cfccde04SRasmus Villemoes while (spaces--) { 587cfccde04SRasmus Villemoes if (buf < end) 588cfccde04SRasmus Villemoes *buf = ' '; 589cfccde04SRasmus Villemoes ++buf; 590cfccde04SRasmus Villemoes } 591cfccde04SRasmus Villemoes return buf; 592cfccde04SRasmus Villemoes } 593cfccde04SRasmus Villemoes 5944b6ccca7SAl Viro static noinline_for_stack 59595508cfaSRasmus Villemoes char *string(char *buf, char *end, const char *s, struct printf_spec spec) 59695508cfaSRasmus Villemoes { 59734fc8b90SRasmus Villemoes int len = 0; 59834fc8b90SRasmus Villemoes size_t lim = spec.precision; 59995508cfaSRasmus Villemoes 60095508cfaSRasmus Villemoes if ((unsigned long)s < PAGE_SIZE) 60195508cfaSRasmus Villemoes s = "(null)"; 60295508cfaSRasmus Villemoes 60334fc8b90SRasmus Villemoes while (lim--) { 60434fc8b90SRasmus Villemoes char c = *s++; 60534fc8b90SRasmus Villemoes if (!c) 60634fc8b90SRasmus Villemoes break; 60795508cfaSRasmus Villemoes if (buf < end) 60834fc8b90SRasmus Villemoes *buf = c; 60995508cfaSRasmus Villemoes ++buf; 61034fc8b90SRasmus Villemoes ++len; 61195508cfaSRasmus Villemoes } 61234fc8b90SRasmus Villemoes return widen_string(buf, len, end, spec); 61395508cfaSRasmus Villemoes } 61495508cfaSRasmus Villemoes 61595508cfaSRasmus Villemoes static noinline_for_stack 6169073dac1SGeert Uytterhoeven char *pointer_string(char *buf, char *end, const void *ptr, 6179073dac1SGeert Uytterhoeven struct printf_spec spec) 6189073dac1SGeert Uytterhoeven { 6199073dac1SGeert Uytterhoeven spec.base = 16; 6209073dac1SGeert Uytterhoeven spec.flags |= SMALL; 6219073dac1SGeert Uytterhoeven if (spec.field_width == -1) { 6229073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr); 6239073dac1SGeert Uytterhoeven spec.flags |= ZEROPAD; 6249073dac1SGeert Uytterhoeven } 6259073dac1SGeert Uytterhoeven 6269073dac1SGeert Uytterhoeven return number(buf, end, (unsigned long int)ptr, spec); 6279073dac1SGeert Uytterhoeven } 6289073dac1SGeert Uytterhoeven 6299073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */ 6309073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init; 6319073dac1SGeert Uytterhoeven 6329073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str) 6339073dac1SGeert Uytterhoeven { 6349073dac1SGeert Uytterhoeven debug_boot_weak_hash = 1; 6359073dac1SGeert Uytterhoeven pr_info("debug_boot_weak_hash enabled\n"); 6369073dac1SGeert Uytterhoeven return 0; 6379073dac1SGeert Uytterhoeven } 6389073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); 6399073dac1SGeert Uytterhoeven 6409073dac1SGeert Uytterhoeven static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key); 6419073dac1SGeert Uytterhoeven static siphash_key_t ptr_key __read_mostly; 6429073dac1SGeert Uytterhoeven 6439073dac1SGeert Uytterhoeven static void enable_ptr_key_workfn(struct work_struct *work) 6449073dac1SGeert Uytterhoeven { 6459073dac1SGeert Uytterhoeven get_random_bytes(&ptr_key, sizeof(ptr_key)); 6469073dac1SGeert Uytterhoeven /* Needs to run from preemptible context */ 6479073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 6489073dac1SGeert Uytterhoeven } 6499073dac1SGeert Uytterhoeven 6509073dac1SGeert Uytterhoeven static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn); 6519073dac1SGeert Uytterhoeven 6529073dac1SGeert Uytterhoeven static void fill_random_ptr_key(struct random_ready_callback *unused) 6539073dac1SGeert Uytterhoeven { 6549073dac1SGeert Uytterhoeven /* This may be in an interrupt handler. */ 6559073dac1SGeert Uytterhoeven queue_work(system_unbound_wq, &enable_ptr_key_work); 6569073dac1SGeert Uytterhoeven } 6579073dac1SGeert Uytterhoeven 6589073dac1SGeert Uytterhoeven static struct random_ready_callback random_ready = { 6599073dac1SGeert Uytterhoeven .func = fill_random_ptr_key 6609073dac1SGeert Uytterhoeven }; 6619073dac1SGeert Uytterhoeven 6629073dac1SGeert Uytterhoeven static int __init initialize_ptr_random(void) 6639073dac1SGeert Uytterhoeven { 6649073dac1SGeert Uytterhoeven int key_size = sizeof(ptr_key); 6659073dac1SGeert Uytterhoeven int ret; 6669073dac1SGeert Uytterhoeven 6679073dac1SGeert Uytterhoeven /* Use hw RNG if available. */ 6689073dac1SGeert Uytterhoeven if (get_random_bytes_arch(&ptr_key, key_size) == key_size) { 6699073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 6709073dac1SGeert Uytterhoeven return 0; 6719073dac1SGeert Uytterhoeven } 6729073dac1SGeert Uytterhoeven 6739073dac1SGeert Uytterhoeven ret = add_random_ready_callback(&random_ready); 6749073dac1SGeert Uytterhoeven if (!ret) { 6759073dac1SGeert Uytterhoeven return 0; 6769073dac1SGeert Uytterhoeven } else if (ret == -EALREADY) { 6779073dac1SGeert Uytterhoeven /* This is in preemptible context */ 6789073dac1SGeert Uytterhoeven enable_ptr_key_workfn(&enable_ptr_key_work); 6799073dac1SGeert Uytterhoeven return 0; 6809073dac1SGeert Uytterhoeven } 6819073dac1SGeert Uytterhoeven 6829073dac1SGeert Uytterhoeven return ret; 6839073dac1SGeert Uytterhoeven } 6849073dac1SGeert Uytterhoeven early_initcall(initialize_ptr_random); 6859073dac1SGeert Uytterhoeven 6869073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */ 6879073dac1SGeert Uytterhoeven static char *ptr_to_id(char *buf, char *end, const void *ptr, 6889073dac1SGeert Uytterhoeven struct printf_spec spec) 6899073dac1SGeert Uytterhoeven { 6909073dac1SGeert Uytterhoeven const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; 6919073dac1SGeert Uytterhoeven unsigned long hashval; 6929073dac1SGeert Uytterhoeven 6939073dac1SGeert Uytterhoeven /* When debugging early boot use non-cryptographically secure hash. */ 6949073dac1SGeert Uytterhoeven if (unlikely(debug_boot_weak_hash)) { 6959073dac1SGeert Uytterhoeven hashval = hash_long((unsigned long)ptr, 32); 6969073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec); 6979073dac1SGeert Uytterhoeven } 6989073dac1SGeert Uytterhoeven 6999073dac1SGeert Uytterhoeven if (static_branch_unlikely(¬_filled_random_ptr_key)) { 7009073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr); 7019073dac1SGeert Uytterhoeven /* string length must be less than default_width */ 7029073dac1SGeert Uytterhoeven return string(buf, end, str, spec); 7039073dac1SGeert Uytterhoeven } 7049073dac1SGeert Uytterhoeven 7059073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT 7069073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); 7079073dac1SGeert Uytterhoeven /* 7089073dac1SGeert Uytterhoeven * Mask off the first 32 bits, this makes explicit that we have 7099073dac1SGeert Uytterhoeven * modified the address (and 32 bits is plenty for a unique ID). 7109073dac1SGeert Uytterhoeven */ 7119073dac1SGeert Uytterhoeven hashval = hashval & 0xffffffff; 7129073dac1SGeert Uytterhoeven #else 7139073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); 7149073dac1SGeert Uytterhoeven #endif 7159073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec); 7169073dac1SGeert Uytterhoeven } 7179073dac1SGeert Uytterhoeven 7189073dac1SGeert Uytterhoeven static noinline_for_stack 7194b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 7204b6ccca7SAl Viro const char *fmt) 7214b6ccca7SAl Viro { 7224b6ccca7SAl Viro const char *array[4], *s; 7234b6ccca7SAl Viro const struct dentry *p; 7244b6ccca7SAl Viro int depth; 7254b6ccca7SAl Viro int i, n; 7264b6ccca7SAl Viro 7274b6ccca7SAl Viro switch (fmt[1]) { 7284b6ccca7SAl Viro case '2': case '3': case '4': 7294b6ccca7SAl Viro depth = fmt[1] - '0'; 7304b6ccca7SAl Viro break; 7314b6ccca7SAl Viro default: 7324b6ccca7SAl Viro depth = 1; 7334b6ccca7SAl Viro } 7344b6ccca7SAl Viro 7354b6ccca7SAl Viro rcu_read_lock(); 7364b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 7376aa7de05SMark Rutland p = READ_ONCE(d->d_parent); 7386aa7de05SMark Rutland array[i] = READ_ONCE(d->d_name.name); 7394b6ccca7SAl Viro if (p == d) { 7404b6ccca7SAl Viro if (i) 7414b6ccca7SAl Viro array[i] = ""; 7424b6ccca7SAl Viro i++; 7434b6ccca7SAl Viro break; 7444b6ccca7SAl Viro } 7454b6ccca7SAl Viro } 7464b6ccca7SAl Viro s = array[--i]; 7474b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 7484b6ccca7SAl Viro char c = *s++; 7494b6ccca7SAl Viro if (!c) { 7504b6ccca7SAl Viro if (!i) 7514b6ccca7SAl Viro break; 7524b6ccca7SAl Viro c = '/'; 7534b6ccca7SAl Viro s = array[--i]; 7544b6ccca7SAl Viro } 7554b6ccca7SAl Viro if (buf < end) 7564b6ccca7SAl Viro *buf = c; 7574b6ccca7SAl Viro } 7584b6ccca7SAl Viro rcu_read_unlock(); 759cfccde04SRasmus Villemoes return widen_string(buf, n, end, spec); 7604b6ccca7SAl Viro } 7614b6ccca7SAl Viro 7621031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 7631031bc58SDmitry Monakhov static noinline_for_stack 7641031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev, 7651031bc58SDmitry Monakhov struct printf_spec spec, const char *fmt) 7661031bc58SDmitry Monakhov { 7671031bc58SDmitry Monakhov struct gendisk *hd = bdev->bd_disk; 7681031bc58SDmitry Monakhov 7691031bc58SDmitry Monakhov buf = string(buf, end, hd->disk_name, spec); 7701031bc58SDmitry Monakhov if (bdev->bd_part->partno) { 7711031bc58SDmitry Monakhov if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { 7721031bc58SDmitry Monakhov if (buf < end) 7731031bc58SDmitry Monakhov *buf = 'p'; 7741031bc58SDmitry Monakhov buf++; 7751031bc58SDmitry Monakhov } 7761031bc58SDmitry Monakhov buf = number(buf, end, bdev->bd_part->partno, spec); 7771031bc58SDmitry Monakhov } 7781031bc58SDmitry Monakhov return buf; 7791031bc58SDmitry Monakhov } 7801031bc58SDmitry Monakhov #endif 7811031bc58SDmitry Monakhov 782cf3b429bSJoe Perches static noinline_for_stack 783cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 784b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 7850fe1ef24SLinus Torvalds { 786b0d33c2bSJoe Perches unsigned long value; 7870fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 7880fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 789b0d33c2bSJoe Perches #endif 790b0d33c2bSJoe Perches 791b0d33c2bSJoe Perches if (fmt[1] == 'R') 792b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 793b0d33c2bSJoe Perches value = (unsigned long)ptr; 794b0d33c2bSJoe Perches 795b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 796b0d33c2bSJoe Perches if (*fmt == 'B') 7970f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 798b0d33c2bSJoe Perches else if (*fmt != 'f' && *fmt != 's') 7990fe1ef24SLinus Torvalds sprint_symbol(sym, value); 8000c8b946eSFrederic Weisbecker else 8014796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 8027b9186f5SAndré Goddard Rosa 803fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 8040fe1ef24SLinus Torvalds #else 8053cab1e71SAndy Shevchenko return special_hex_number(buf, end, value, sizeof(void *)); 8060fe1ef24SLinus Torvalds #endif 8070fe1ef24SLinus Torvalds } 8080fe1ef24SLinus Torvalds 809abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = { 810abd4fe62SAndy Shevchenko .field_width = -1, 811abd4fe62SAndy Shevchenko .precision = -1, 812abd4fe62SAndy Shevchenko }; 813abd4fe62SAndy Shevchenko 81454433973SAndy Shevchenko static const struct printf_spec default_flag_spec = { 81554433973SAndy Shevchenko .base = 16, 81654433973SAndy Shevchenko .precision = -1, 81754433973SAndy Shevchenko .flags = SPECIAL | SMALL, 81854433973SAndy Shevchenko }; 81954433973SAndy Shevchenko 820ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = { 821ce0b4910SAndy Shevchenko .base = 10, 822ce0b4910SAndy Shevchenko .precision = -1, 823ce0b4910SAndy Shevchenko }; 824ce0b4910SAndy Shevchenko 825cf3b429bSJoe Perches static noinline_for_stack 826cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 827fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 828332d2e78SLinus Torvalds { 829332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 83028405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 831332d2e78SLinus Torvalds #endif 832332d2e78SLinus Torvalds 833332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 83428405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 835332d2e78SLinus Torvalds #endif 8364da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 837fef20d9cSFrederic Weisbecker .base = 16, 8384da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 839fef20d9cSFrederic Weisbecker .precision = -1, 840fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 841fef20d9cSFrederic Weisbecker }; 8424da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 8434da0b66cSBjorn Helgaas .base = 16, 8444da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 8454da0b66cSBjorn Helgaas .precision = -1, 8464da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 8474da0b66cSBjorn Helgaas }; 8480f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 8490f4050c7SBjorn Helgaas .base = 16, 8500f4050c7SBjorn Helgaas .field_width = 2, 8510f4050c7SBjorn Helgaas .precision = -1, 8520f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 8530f4050c7SBjorn Helgaas }; 8544da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 855fd95541eSBjorn Helgaas .field_width = -1, 856fd95541eSBjorn Helgaas .precision = 10, 857fd95541eSBjorn Helgaas .flags = LEFT, 858fd95541eSBjorn Helgaas }; 859c7dabef8SBjorn Helgaas 860c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 861c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 862c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 863c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 8649d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 865c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 866c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 867c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 868c7dabef8SBjorn Helgaas 869332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 870c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 8714da0b66cSBjorn Helgaas const struct printf_spec *specp; 872332d2e78SLinus Torvalds 873332d2e78SLinus Torvalds *p++ = '['; 8744da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 875fd95541eSBjorn Helgaas p = string(p, pend, "io ", str_spec); 8764da0b66cSBjorn Helgaas specp = &io_spec; 8774da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 878fd95541eSBjorn Helgaas p = string(p, pend, "mem ", str_spec); 8794da0b66cSBjorn Helgaas specp = &mem_spec; 8804da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 881fd95541eSBjorn Helgaas p = string(p, pend, "irq ", str_spec); 882ce0b4910SAndy Shevchenko specp = &default_dec_spec; 8834da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 884fd95541eSBjorn Helgaas p = string(p, pend, "dma ", str_spec); 885ce0b4910SAndy Shevchenko specp = &default_dec_spec; 8860f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 8870f4050c7SBjorn Helgaas p = string(p, pend, "bus ", str_spec); 8880f4050c7SBjorn Helgaas specp = &bus_spec; 8894da0b66cSBjorn Helgaas } else { 890c7dabef8SBjorn Helgaas p = string(p, pend, "??? ", str_spec); 8914da0b66cSBjorn Helgaas specp = &mem_spec; 892c7dabef8SBjorn Helgaas decode = 0; 893fd95541eSBjorn Helgaas } 894d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 895d19cb803SBjorn Helgaas p = string(p, pend, "size ", str_spec); 896d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 897d19cb803SBjorn Helgaas } else { 8984da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 899c91d3376SBjorn Helgaas if (res->start != res->end) { 900332d2e78SLinus Torvalds *p++ = '-'; 9014da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 902c91d3376SBjorn Helgaas } 903d19cb803SBjorn Helgaas } 904c7dabef8SBjorn Helgaas if (decode) { 905fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 906fd95541eSBjorn Helgaas p = string(p, pend, " 64bit", str_spec); 907fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 908fd95541eSBjorn Helgaas p = string(p, pend, " pref", str_spec); 9099d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 9109d7cca04SBjorn Helgaas p = string(p, pend, " window", str_spec); 911fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 912fd95541eSBjorn Helgaas p = string(p, pend, " disabled", str_spec); 913c7dabef8SBjorn Helgaas } else { 914fd95541eSBjorn Helgaas p = string(p, pend, " flags ", str_spec); 91554433973SAndy Shevchenko p = number(p, pend, res->flags, default_flag_spec); 916fd95541eSBjorn Helgaas } 917332d2e78SLinus Torvalds *p++ = ']'; 918c7dabef8SBjorn Helgaas *p = '\0'; 919332d2e78SLinus Torvalds 920fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 921332d2e78SLinus Torvalds } 922332d2e78SLinus Torvalds 923cf3b429bSJoe Perches static noinline_for_stack 92431550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 92531550a16SAndy Shevchenko const char *fmt) 92631550a16SAndy Shevchenko { 927360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 92831550a16SAndy Shevchenko negative value, fallback to the default */ 92931550a16SAndy Shevchenko char separator; 93031550a16SAndy Shevchenko 93131550a16SAndy Shevchenko if (spec.field_width == 0) 93231550a16SAndy Shevchenko /* nothing to print */ 93331550a16SAndy Shevchenko return buf; 93431550a16SAndy Shevchenko 93531550a16SAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 93631550a16SAndy Shevchenko /* NULL pointer */ 93731550a16SAndy Shevchenko return string(buf, end, NULL, spec); 93831550a16SAndy Shevchenko 93931550a16SAndy Shevchenko switch (fmt[1]) { 94031550a16SAndy Shevchenko case 'C': 94131550a16SAndy Shevchenko separator = ':'; 94231550a16SAndy Shevchenko break; 94331550a16SAndy Shevchenko case 'D': 94431550a16SAndy Shevchenko separator = '-'; 94531550a16SAndy Shevchenko break; 94631550a16SAndy Shevchenko case 'N': 94731550a16SAndy Shevchenko separator = 0; 94831550a16SAndy Shevchenko break; 94931550a16SAndy Shevchenko default: 95031550a16SAndy Shevchenko separator = ' '; 95131550a16SAndy Shevchenko break; 95231550a16SAndy Shevchenko } 95331550a16SAndy Shevchenko 95431550a16SAndy Shevchenko if (spec.field_width > 0) 95531550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 95631550a16SAndy Shevchenko 9579c98f235SRasmus Villemoes for (i = 0; i < len; ++i) { 9589c98f235SRasmus Villemoes if (buf < end) 9599c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]); 9609c98f235SRasmus Villemoes ++buf; 9619c98f235SRasmus Villemoes if (buf < end) 9629c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]); 9639c98f235SRasmus Villemoes ++buf; 96431550a16SAndy Shevchenko 9659c98f235SRasmus Villemoes if (separator && i != len - 1) { 9669c98f235SRasmus Villemoes if (buf < end) 9679c98f235SRasmus Villemoes *buf = separator; 9689c98f235SRasmus Villemoes ++buf; 9699c98f235SRasmus Villemoes } 97031550a16SAndy Shevchenko } 97131550a16SAndy Shevchenko 97231550a16SAndy Shevchenko return buf; 97331550a16SAndy Shevchenko } 97431550a16SAndy Shevchenko 97531550a16SAndy Shevchenko static noinline_for_stack 976dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap, 977dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 978dbc760bcSTejun Heo { 979dbc760bcSTejun Heo const int CHUNKSZ = 32; 980dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 981dbc760bcSTejun Heo int i, chunksz; 982dbc760bcSTejun Heo bool first = true; 983dbc760bcSTejun Heo 984dbc760bcSTejun Heo /* reused to print numbers */ 985dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 986dbc760bcSTejun Heo 987dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1); 988dbc760bcSTejun Heo if (chunksz == 0) 989dbc760bcSTejun Heo chunksz = CHUNKSZ; 990dbc760bcSTejun Heo 991dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 992dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) { 993dbc760bcSTejun Heo u32 chunkmask, val; 994dbc760bcSTejun Heo int word, bit; 995dbc760bcSTejun Heo 996dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1); 997dbc760bcSTejun Heo word = i / BITS_PER_LONG; 998dbc760bcSTejun Heo bit = i % BITS_PER_LONG; 999dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask; 1000dbc760bcSTejun Heo 1001dbc760bcSTejun Heo if (!first) { 1002dbc760bcSTejun Heo if (buf < end) 1003dbc760bcSTejun Heo *buf = ','; 1004dbc760bcSTejun Heo buf++; 1005dbc760bcSTejun Heo } 1006dbc760bcSTejun Heo first = false; 1007dbc760bcSTejun Heo 1008dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4); 1009dbc760bcSTejun Heo buf = number(buf, end, val, spec); 1010dbc760bcSTejun Heo 1011dbc760bcSTejun Heo chunksz = CHUNKSZ; 1012dbc760bcSTejun Heo } 1013dbc760bcSTejun Heo return buf; 1014dbc760bcSTejun Heo } 1015dbc760bcSTejun Heo 1016dbc760bcSTejun Heo static noinline_for_stack 1017dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, 1018dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1019dbc760bcSTejun Heo { 1020dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1021dbc760bcSTejun Heo /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 1022dbc760bcSTejun Heo int cur, rbot, rtop; 1023dbc760bcSTejun Heo bool first = true; 1024dbc760bcSTejun Heo 1025dbc760bcSTejun Heo rbot = cur = find_first_bit(bitmap, nr_bits); 1026dbc760bcSTejun Heo while (cur < nr_bits) { 1027dbc760bcSTejun Heo rtop = cur; 1028dbc760bcSTejun Heo cur = find_next_bit(bitmap, nr_bits, cur + 1); 1029dbc760bcSTejun Heo if (cur < nr_bits && cur <= rtop + 1) 1030dbc760bcSTejun Heo continue; 1031dbc760bcSTejun Heo 1032dbc760bcSTejun Heo if (!first) { 1033dbc760bcSTejun Heo if (buf < end) 1034dbc760bcSTejun Heo *buf = ','; 1035dbc760bcSTejun Heo buf++; 1036dbc760bcSTejun Heo } 1037dbc760bcSTejun Heo first = false; 1038dbc760bcSTejun Heo 1039ce0b4910SAndy Shevchenko buf = number(buf, end, rbot, default_dec_spec); 1040dbc760bcSTejun Heo if (rbot < rtop) { 1041dbc760bcSTejun Heo if (buf < end) 1042dbc760bcSTejun Heo *buf = '-'; 1043dbc760bcSTejun Heo buf++; 1044dbc760bcSTejun Heo 1045ce0b4910SAndy Shevchenko buf = number(buf, end, rtop, default_dec_spec); 1046dbc760bcSTejun Heo } 1047dbc760bcSTejun Heo 1048dbc760bcSTejun Heo rbot = cur; 1049dbc760bcSTejun Heo } 1050dbc760bcSTejun Heo return buf; 1051dbc760bcSTejun Heo } 1052dbc760bcSTejun Heo 1053dbc760bcSTejun Heo static noinline_for_stack 1054cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 10558a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 1056dd45c9cfSHarvey Harrison { 10578a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 1058dd45c9cfSHarvey Harrison char *p = mac_addr; 1059dd45c9cfSHarvey Harrison int i; 1060bc7259a2SJoe Perches char separator; 106176597ff9SAndrei Emeltchenko bool reversed = false; 1062bc7259a2SJoe Perches 106376597ff9SAndrei Emeltchenko switch (fmt[1]) { 106476597ff9SAndrei Emeltchenko case 'F': 1065bc7259a2SJoe Perches separator = '-'; 106676597ff9SAndrei Emeltchenko break; 106776597ff9SAndrei Emeltchenko 106876597ff9SAndrei Emeltchenko case 'R': 106976597ff9SAndrei Emeltchenko reversed = true; 107076597ff9SAndrei Emeltchenko /* fall through */ 107176597ff9SAndrei Emeltchenko 107276597ff9SAndrei Emeltchenko default: 1073bc7259a2SJoe Perches separator = ':'; 107476597ff9SAndrei Emeltchenko break; 1075bc7259a2SJoe Perches } 1076dd45c9cfSHarvey Harrison 1077dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 107876597ff9SAndrei Emeltchenko if (reversed) 107976597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 108076597ff9SAndrei Emeltchenko else 108155036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 108276597ff9SAndrei Emeltchenko 10838a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 1084bc7259a2SJoe Perches *p++ = separator; 1085dd45c9cfSHarvey Harrison } 1086dd45c9cfSHarvey Harrison *p = '\0'; 1087dd45c9cfSHarvey Harrison 1088fef20d9cSFrederic Weisbecker return string(buf, end, mac_addr, spec); 1089dd45c9cfSHarvey Harrison } 1090dd45c9cfSHarvey Harrison 1091cf3b429bSJoe Perches static noinline_for_stack 1092cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 1093689afa7dSHarvey Harrison { 1094689afa7dSHarvey Harrison int i; 10950159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 10960159f24eSJoe Perches int index; 10970159f24eSJoe Perches int step; 1098689afa7dSHarvey Harrison 10990159f24eSJoe Perches switch (fmt[2]) { 11000159f24eSJoe Perches case 'h': 11010159f24eSJoe Perches #ifdef __BIG_ENDIAN 11020159f24eSJoe Perches index = 0; 11030159f24eSJoe Perches step = 1; 11040159f24eSJoe Perches #else 11050159f24eSJoe Perches index = 3; 11060159f24eSJoe Perches step = -1; 11070159f24eSJoe Perches #endif 11080159f24eSJoe Perches break; 11090159f24eSJoe Perches case 'l': 11100159f24eSJoe Perches index = 3; 11110159f24eSJoe Perches step = -1; 11120159f24eSJoe Perches break; 11130159f24eSJoe Perches case 'n': 11140159f24eSJoe Perches case 'b': 11150159f24eSJoe Perches default: 11160159f24eSJoe Perches index = 0; 11170159f24eSJoe Perches step = 1; 11180159f24eSJoe Perches break; 11190159f24eSJoe Perches } 11208a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 11217c43d9a3SRasmus Villemoes char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 1122133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 11238a27f7c9SJoe Perches if (leading_zeros) { 11248a27f7c9SJoe Perches if (digits < 3) 11258a27f7c9SJoe Perches *p++ = '0'; 11268a27f7c9SJoe Perches if (digits < 2) 11278a27f7c9SJoe Perches *p++ = '0'; 11288a27f7c9SJoe Perches } 11298a27f7c9SJoe Perches /* reverse the digits in the quad */ 11308a27f7c9SJoe Perches while (digits--) 11318a27f7c9SJoe Perches *p++ = temp[digits]; 11328a27f7c9SJoe Perches if (i < 3) 11338a27f7c9SJoe Perches *p++ = '.'; 11340159f24eSJoe Perches index += step; 11358a27f7c9SJoe Perches } 11368a27f7c9SJoe Perches *p = '\0'; 11377b9186f5SAndré Goddard Rosa 11388a27f7c9SJoe Perches return p; 11398a27f7c9SJoe Perches } 11408a27f7c9SJoe Perches 1141cf3b429bSJoe Perches static noinline_for_stack 1142cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 11438a27f7c9SJoe Perches { 11447b9186f5SAndré Goddard Rosa int i, j, range; 11458a27f7c9SJoe Perches unsigned char zerolength[8]; 11468a27f7c9SJoe Perches int longest = 1; 11478a27f7c9SJoe Perches int colonpos = -1; 11488a27f7c9SJoe Perches u16 word; 11497b9186f5SAndré Goddard Rosa u8 hi, lo; 11508a27f7c9SJoe Perches bool needcolon = false; 1151eb78cd26SJoe Perches bool useIPv4; 1152eb78cd26SJoe Perches struct in6_addr in6; 1153eb78cd26SJoe Perches 1154eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 1155eb78cd26SJoe Perches 1156eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 11578a27f7c9SJoe Perches 11588a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 11598a27f7c9SJoe Perches 11608a27f7c9SJoe Perches if (useIPv4) 11618a27f7c9SJoe Perches range = 6; 11628a27f7c9SJoe Perches else 11638a27f7c9SJoe Perches range = 8; 11648a27f7c9SJoe Perches 11658a27f7c9SJoe Perches /* find position of longest 0 run */ 11668a27f7c9SJoe Perches for (i = 0; i < range; i++) { 11678a27f7c9SJoe Perches for (j = i; j < range; j++) { 1168eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 11698a27f7c9SJoe Perches break; 11708a27f7c9SJoe Perches zerolength[i]++; 11718a27f7c9SJoe Perches } 11728a27f7c9SJoe Perches } 11738a27f7c9SJoe Perches for (i = 0; i < range; i++) { 11748a27f7c9SJoe Perches if (zerolength[i] > longest) { 11758a27f7c9SJoe Perches longest = zerolength[i]; 11768a27f7c9SJoe Perches colonpos = i; 11778a27f7c9SJoe Perches } 11788a27f7c9SJoe Perches } 117929cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 118029cf519eSJoe Perches colonpos = -1; 11818a27f7c9SJoe Perches 11828a27f7c9SJoe Perches /* emit address */ 11838a27f7c9SJoe Perches for (i = 0; i < range; i++) { 11848a27f7c9SJoe Perches if (i == colonpos) { 11858a27f7c9SJoe Perches if (needcolon || i == 0) 11868a27f7c9SJoe Perches *p++ = ':'; 11878a27f7c9SJoe Perches *p++ = ':'; 11888a27f7c9SJoe Perches needcolon = false; 11898a27f7c9SJoe Perches i += longest - 1; 11908a27f7c9SJoe Perches continue; 11918a27f7c9SJoe Perches } 11928a27f7c9SJoe Perches if (needcolon) { 11938a27f7c9SJoe Perches *p++ = ':'; 11948a27f7c9SJoe Perches needcolon = false; 11958a27f7c9SJoe Perches } 11968a27f7c9SJoe Perches /* hex u16 without leading 0s */ 1197eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 11988a27f7c9SJoe Perches hi = word >> 8; 11998a27f7c9SJoe Perches lo = word & 0xff; 12008a27f7c9SJoe Perches if (hi) { 12018a27f7c9SJoe Perches if (hi > 0x0f) 120255036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 12038a27f7c9SJoe Perches else 12048a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 120555036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 12068a27f7c9SJoe Perches } 1207b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 120855036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 12098a27f7c9SJoe Perches else 12108a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 12118a27f7c9SJoe Perches needcolon = true; 12128a27f7c9SJoe Perches } 12138a27f7c9SJoe Perches 12148a27f7c9SJoe Perches if (useIPv4) { 12158a27f7c9SJoe Perches if (needcolon) 12168a27f7c9SJoe Perches *p++ = ':'; 12170159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 12188a27f7c9SJoe Perches } 12198a27f7c9SJoe Perches *p = '\0'; 12207b9186f5SAndré Goddard Rosa 12218a27f7c9SJoe Perches return p; 12228a27f7c9SJoe Perches } 12238a27f7c9SJoe Perches 1224cf3b429bSJoe Perches static noinline_for_stack 1225cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 12268a27f7c9SJoe Perches { 12278a27f7c9SJoe Perches int i; 12287b9186f5SAndré Goddard Rosa 1229689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 123055036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 123155036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 12328a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 1233689afa7dSHarvey Harrison *p++ = ':'; 1234689afa7dSHarvey Harrison } 1235689afa7dSHarvey Harrison *p = '\0'; 12367b9186f5SAndré Goddard Rosa 12378a27f7c9SJoe Perches return p; 12388a27f7c9SJoe Perches } 12398a27f7c9SJoe Perches 1240cf3b429bSJoe Perches static noinline_for_stack 1241cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 12428a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 12438a27f7c9SJoe Perches { 12448a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 12458a27f7c9SJoe Perches 12468a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 1247eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 12488a27f7c9SJoe Perches else 1249eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 1250689afa7dSHarvey Harrison 1251fef20d9cSFrederic Weisbecker return string(buf, end, ip6_addr, spec); 1252689afa7dSHarvey Harrison } 1253689afa7dSHarvey Harrison 1254cf3b429bSJoe Perches static noinline_for_stack 1255cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 12568a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 12574aa99606SHarvey Harrison { 12588a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 12594aa99606SHarvey Harrison 12600159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 12614aa99606SHarvey Harrison 1262fef20d9cSFrederic Weisbecker return string(buf, end, ip4_addr, spec); 12634aa99606SHarvey Harrison } 12644aa99606SHarvey Harrison 1265cf3b429bSJoe Perches static noinline_for_stack 126610679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 126710679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 126810679643SDaniel Borkmann { 126910679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 127010679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 127110679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 127210679643SDaniel Borkmann sizeof("%1234567890")]; 127310679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 127410679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 127510679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 127610679643SDaniel Borkmann u8 off = 0; 127710679643SDaniel Borkmann 127810679643SDaniel Borkmann fmt++; 127910679643SDaniel Borkmann while (isalpha(*++fmt)) { 128010679643SDaniel Borkmann switch (*fmt) { 128110679643SDaniel Borkmann case 'p': 128210679643SDaniel Borkmann have_p = true; 128310679643SDaniel Borkmann break; 128410679643SDaniel Borkmann case 'f': 128510679643SDaniel Borkmann have_f = true; 128610679643SDaniel Borkmann break; 128710679643SDaniel Borkmann case 's': 128810679643SDaniel Borkmann have_s = true; 128910679643SDaniel Borkmann break; 129010679643SDaniel Borkmann case 'c': 129110679643SDaniel Borkmann have_c = true; 129210679643SDaniel Borkmann break; 129310679643SDaniel Borkmann } 129410679643SDaniel Borkmann } 129510679643SDaniel Borkmann 129610679643SDaniel Borkmann if (have_p || have_s || have_f) { 129710679643SDaniel Borkmann *p = '['; 129810679643SDaniel Borkmann off = 1; 129910679643SDaniel Borkmann } 130010679643SDaniel Borkmann 130110679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 130210679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 130310679643SDaniel Borkmann else 130410679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 130510679643SDaniel Borkmann 130610679643SDaniel Borkmann if (have_p || have_s || have_f) 130710679643SDaniel Borkmann *p++ = ']'; 130810679643SDaniel Borkmann 130910679643SDaniel Borkmann if (have_p) { 131010679643SDaniel Borkmann *p++ = ':'; 131110679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 131210679643SDaniel Borkmann } 131310679643SDaniel Borkmann if (have_f) { 131410679643SDaniel Borkmann *p++ = '/'; 131510679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 131610679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 131710679643SDaniel Borkmann } 131810679643SDaniel Borkmann if (have_s) { 131910679643SDaniel Borkmann *p++ = '%'; 132010679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 132110679643SDaniel Borkmann } 132210679643SDaniel Borkmann *p = '\0'; 132310679643SDaniel Borkmann 132410679643SDaniel Borkmann return string(buf, end, ip6_addr, spec); 132510679643SDaniel Borkmann } 132610679643SDaniel Borkmann 132710679643SDaniel Borkmann static noinline_for_stack 132810679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 132910679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 133010679643SDaniel Borkmann { 133110679643SDaniel Borkmann bool have_p = false; 133210679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 133310679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 133410679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 133510679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 133610679643SDaniel Borkmann 133710679643SDaniel Borkmann fmt++; 133810679643SDaniel Borkmann while (isalpha(*++fmt)) { 133910679643SDaniel Borkmann switch (*fmt) { 134010679643SDaniel Borkmann case 'p': 134110679643SDaniel Borkmann have_p = true; 134210679643SDaniel Borkmann break; 134310679643SDaniel Borkmann case 'h': 134410679643SDaniel Borkmann case 'l': 134510679643SDaniel Borkmann case 'n': 134610679643SDaniel Borkmann case 'b': 134710679643SDaniel Borkmann fmt4[2] = *fmt; 134810679643SDaniel Borkmann break; 134910679643SDaniel Borkmann } 135010679643SDaniel Borkmann } 135110679643SDaniel Borkmann 135210679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 135310679643SDaniel Borkmann if (have_p) { 135410679643SDaniel Borkmann *p++ = ':'; 135510679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 135610679643SDaniel Borkmann } 135710679643SDaniel Borkmann *p = '\0'; 135810679643SDaniel Borkmann 135910679643SDaniel Borkmann return string(buf, end, ip4_addr, spec); 136010679643SDaniel Borkmann } 136110679643SDaniel Borkmann 136210679643SDaniel Borkmann static noinline_for_stack 136371dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 136471dca95dSAndy Shevchenko const char *fmt) 136571dca95dSAndy Shevchenko { 136671dca95dSAndy Shevchenko bool found = true; 136771dca95dSAndy Shevchenko int count = 1; 136871dca95dSAndy Shevchenko unsigned int flags = 0; 136971dca95dSAndy Shevchenko int len; 137071dca95dSAndy Shevchenko 137171dca95dSAndy Shevchenko if (spec.field_width == 0) 137271dca95dSAndy Shevchenko return buf; /* nothing to print */ 137371dca95dSAndy Shevchenko 137471dca95dSAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 137571dca95dSAndy Shevchenko return string(buf, end, NULL, spec); /* NULL pointer */ 137671dca95dSAndy Shevchenko 137771dca95dSAndy Shevchenko 137871dca95dSAndy Shevchenko do { 137971dca95dSAndy Shevchenko switch (fmt[count++]) { 138071dca95dSAndy Shevchenko case 'a': 138171dca95dSAndy Shevchenko flags |= ESCAPE_ANY; 138271dca95dSAndy Shevchenko break; 138371dca95dSAndy Shevchenko case 'c': 138471dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL; 138571dca95dSAndy Shevchenko break; 138671dca95dSAndy Shevchenko case 'h': 138771dca95dSAndy Shevchenko flags |= ESCAPE_HEX; 138871dca95dSAndy Shevchenko break; 138971dca95dSAndy Shevchenko case 'n': 139071dca95dSAndy Shevchenko flags |= ESCAPE_NULL; 139171dca95dSAndy Shevchenko break; 139271dca95dSAndy Shevchenko case 'o': 139371dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL; 139471dca95dSAndy Shevchenko break; 139571dca95dSAndy Shevchenko case 'p': 139671dca95dSAndy Shevchenko flags |= ESCAPE_NP; 139771dca95dSAndy Shevchenko break; 139871dca95dSAndy Shevchenko case 's': 139971dca95dSAndy Shevchenko flags |= ESCAPE_SPACE; 140071dca95dSAndy Shevchenko break; 140171dca95dSAndy Shevchenko default: 140271dca95dSAndy Shevchenko found = false; 140371dca95dSAndy Shevchenko break; 140471dca95dSAndy Shevchenko } 140571dca95dSAndy Shevchenko } while (found); 140671dca95dSAndy Shevchenko 140771dca95dSAndy Shevchenko if (!flags) 140871dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP; 140971dca95dSAndy Shevchenko 141071dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width; 141171dca95dSAndy Shevchenko 141241416f23SRasmus Villemoes /* 141341416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to 141441416f23SRasmus Villemoes * the given buffer, and returns the total size of the output 141541416f23SRasmus Villemoes * had the buffer been big enough. 141641416f23SRasmus Villemoes */ 141741416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 141871dca95dSAndy Shevchenko 141971dca95dSAndy Shevchenko return buf; 142071dca95dSAndy Shevchenko } 142171dca95dSAndy Shevchenko 142271dca95dSAndy Shevchenko static noinline_for_stack 1423cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 14249ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 14259ac6e44eSJoe Perches { 14262b1b0d66SAndy Shevchenko char uuid[UUID_STRING_LEN + 1]; 14279ac6e44eSJoe Perches char *p = uuid; 14289ac6e44eSJoe Perches int i; 1429f9727a17SChristoph Hellwig const u8 *index = uuid_index; 14309ac6e44eSJoe Perches bool uc = false; 14319ac6e44eSJoe Perches 14329ac6e44eSJoe Perches switch (*(++fmt)) { 14339ac6e44eSJoe Perches case 'L': 14349ac6e44eSJoe Perches uc = true; /* fall-through */ 14359ac6e44eSJoe Perches case 'l': 1436f9727a17SChristoph Hellwig index = guid_index; 14379ac6e44eSJoe Perches break; 14389ac6e44eSJoe Perches case 'B': 14399ac6e44eSJoe Perches uc = true; 14409ac6e44eSJoe Perches break; 14419ac6e44eSJoe Perches } 14429ac6e44eSJoe Perches 14439ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 1444aa4ea1c3SAndy Shevchenko if (uc) 1445aa4ea1c3SAndy Shevchenko p = hex_byte_pack_upper(p, addr[index[i]]); 1446aa4ea1c3SAndy Shevchenko else 144755036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 14489ac6e44eSJoe Perches switch (i) { 14499ac6e44eSJoe Perches case 3: 14509ac6e44eSJoe Perches case 5: 14519ac6e44eSJoe Perches case 7: 14529ac6e44eSJoe Perches case 9: 14539ac6e44eSJoe Perches *p++ = '-'; 14549ac6e44eSJoe Perches break; 14559ac6e44eSJoe Perches } 14569ac6e44eSJoe Perches } 14579ac6e44eSJoe Perches 14589ac6e44eSJoe Perches *p = 0; 14599ac6e44eSJoe Perches 14609ac6e44eSJoe Perches return string(buf, end, uuid, spec); 14619ac6e44eSJoe Perches } 14629ac6e44eSJoe Perches 146357e73442STobin C. Harding int kptr_restrict __read_mostly; 146457e73442STobin C. Harding 146557e73442STobin C. Harding static noinline_for_stack 146657e73442STobin C. Harding char *restricted_pointer(char *buf, char *end, const void *ptr, 146757e73442STobin C. Harding struct printf_spec spec) 146857e73442STobin C. Harding { 146957e73442STobin C. Harding switch (kptr_restrict) { 147057e73442STobin C. Harding case 0: 147157e73442STobin C. Harding /* Always print %pK values */ 147257e73442STobin C. Harding break; 147357e73442STobin C. Harding case 1: { 147457e73442STobin C. Harding const struct cred *cred; 147557e73442STobin C. Harding 147657e73442STobin C. Harding /* 147757e73442STobin C. Harding * kptr_restrict==1 cannot be used in IRQ context 147857e73442STobin C. Harding * because its test for CAP_SYSLOG would be meaningless. 147957e73442STobin C. Harding */ 1480496a9a5fSAndy Shevchenko if (in_irq() || in_serving_softirq() || in_nmi()) { 1481496a9a5fSAndy Shevchenko if (spec.field_width == -1) 1482496a9a5fSAndy Shevchenko spec.field_width = 2 * sizeof(ptr); 148357e73442STobin C. Harding return string(buf, end, "pK-error", spec); 1484496a9a5fSAndy Shevchenko } 148557e73442STobin C. Harding 148657e73442STobin C. Harding /* 148757e73442STobin C. Harding * Only print the real pointer value if the current 148857e73442STobin C. Harding * process has CAP_SYSLOG and is running with the 148957e73442STobin C. Harding * same credentials it started with. This is because 149057e73442STobin C. Harding * access to files is checked at open() time, but %pK 149157e73442STobin C. Harding * checks permission at read() time. We don't want to 149257e73442STobin C. Harding * leak pointer values if a binary opens a file using 149357e73442STobin C. Harding * %pK and then elevates privileges before reading it. 149457e73442STobin C. Harding */ 149557e73442STobin C. Harding cred = current_cred(); 149657e73442STobin C. Harding if (!has_capability_noaudit(current, CAP_SYSLOG) || 149757e73442STobin C. Harding !uid_eq(cred->euid, cred->uid) || 149857e73442STobin C. Harding !gid_eq(cred->egid, cred->gid)) 149957e73442STobin C. Harding ptr = NULL; 150057e73442STobin C. Harding break; 150157e73442STobin C. Harding } 150257e73442STobin C. Harding case 2: 150357e73442STobin C. Harding default: 150457e73442STobin C. Harding /* Always print 0's for %pK */ 150557e73442STobin C. Harding ptr = NULL; 150657e73442STobin C. Harding break; 150757e73442STobin C. Harding } 150857e73442STobin C. Harding 1509496a9a5fSAndy Shevchenko return pointer_string(buf, end, ptr, spec); 151057e73442STobin C. Harding } 151157e73442STobin C. Harding 15125b17aecfSAndy Shevchenko static noinline_for_stack 1513*431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr, 1514*431bca24SGeert Uytterhoeven struct printf_spec spec, const char *fmt) 1515c8f44affSMichał Mirosław { 15165b17aecfSAndy Shevchenko unsigned long long num; 15175b17aecfSAndy Shevchenko int size; 15185b17aecfSAndy Shevchenko 15195b17aecfSAndy Shevchenko switch (fmt[1]) { 15205b17aecfSAndy Shevchenko case 'F': 15215b17aecfSAndy Shevchenko num = *(const netdev_features_t *)addr; 15225b17aecfSAndy Shevchenko size = sizeof(netdev_features_t); 15235b17aecfSAndy Shevchenko break; 15245b17aecfSAndy Shevchenko default: 1525*431bca24SGeert Uytterhoeven return ptr_to_id(buf, end, addr, spec); 15265b17aecfSAndy Shevchenko } 1527c8f44affSMichał Mirosław 15283cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1529c8f44affSMichał Mirosław } 1530c8f44affSMichał Mirosław 1531aaf07621SJoe Perches static noinline_for_stack 15323cab1e71SAndy Shevchenko char *address_val(char *buf, char *end, const void *addr, const char *fmt) 1533aaf07621SJoe Perches { 1534aaf07621SJoe Perches unsigned long long num; 15353cab1e71SAndy Shevchenko int size; 1536aaf07621SJoe Perches 1537aaf07621SJoe Perches switch (fmt[1]) { 1538aaf07621SJoe Perches case 'd': 1539aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 15403cab1e71SAndy Shevchenko size = sizeof(dma_addr_t); 1541aaf07621SJoe Perches break; 1542aaf07621SJoe Perches case 'p': 1543aaf07621SJoe Perches default: 1544aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 15453cab1e71SAndy Shevchenko size = sizeof(phys_addr_t); 1546aaf07621SJoe Perches break; 1547aaf07621SJoe Perches } 1548aaf07621SJoe Perches 15493cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1550aaf07621SJoe Perches } 1551aaf07621SJoe Perches 1552900cca29SGeert Uytterhoeven static noinline_for_stack 1553900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1554900cca29SGeert Uytterhoeven const char *fmt) 1555900cca29SGeert Uytterhoeven { 1556900cca29SGeert Uytterhoeven if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk) 1557900cca29SGeert Uytterhoeven return string(buf, end, NULL, spec); 1558900cca29SGeert Uytterhoeven 1559900cca29SGeert Uytterhoeven switch (fmt[1]) { 1560900cca29SGeert Uytterhoeven case 'n': 1561900cca29SGeert Uytterhoeven default: 1562900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1563900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1564900cca29SGeert Uytterhoeven #else 1565ec12bc29SGeert Uytterhoeven return ptr_to_id(buf, end, clk, spec); 1566900cca29SGeert Uytterhoeven #endif 1567900cca29SGeert Uytterhoeven } 1568900cca29SGeert Uytterhoeven } 1569900cca29SGeert Uytterhoeven 1570edf14cdbSVlastimil Babka static 1571edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags, 1572edf14cdbSVlastimil Babka const struct trace_print_flags *names) 1573edf14cdbSVlastimil Babka { 1574edf14cdbSVlastimil Babka unsigned long mask; 1575edf14cdbSVlastimil Babka 1576edf14cdbSVlastimil Babka for ( ; flags && names->name; names++) { 1577edf14cdbSVlastimil Babka mask = names->mask; 1578edf14cdbSVlastimil Babka if ((flags & mask) != mask) 1579edf14cdbSVlastimil Babka continue; 1580edf14cdbSVlastimil Babka 1581abd4fe62SAndy Shevchenko buf = string(buf, end, names->name, default_str_spec); 1582edf14cdbSVlastimil Babka 1583edf14cdbSVlastimil Babka flags &= ~mask; 1584edf14cdbSVlastimil Babka if (flags) { 1585edf14cdbSVlastimil Babka if (buf < end) 1586edf14cdbSVlastimil Babka *buf = '|'; 1587edf14cdbSVlastimil Babka buf++; 1588edf14cdbSVlastimil Babka } 1589edf14cdbSVlastimil Babka } 1590edf14cdbSVlastimil Babka 1591edf14cdbSVlastimil Babka if (flags) 159254433973SAndy Shevchenko buf = number(buf, end, flags, default_flag_spec); 1593edf14cdbSVlastimil Babka 1594edf14cdbSVlastimil Babka return buf; 1595edf14cdbSVlastimil Babka } 1596edf14cdbSVlastimil Babka 1597edf14cdbSVlastimil Babka static noinline_for_stack 1598edf14cdbSVlastimil Babka char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt) 1599edf14cdbSVlastimil Babka { 1600edf14cdbSVlastimil Babka unsigned long flags; 1601edf14cdbSVlastimil Babka const struct trace_print_flags *names; 1602edf14cdbSVlastimil Babka 1603edf14cdbSVlastimil Babka switch (fmt[1]) { 1604edf14cdbSVlastimil Babka case 'p': 1605edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1606edf14cdbSVlastimil Babka /* Remove zone id */ 1607edf14cdbSVlastimil Babka flags &= (1UL << NR_PAGEFLAGS) - 1; 1608edf14cdbSVlastimil Babka names = pageflag_names; 1609edf14cdbSVlastimil Babka break; 1610edf14cdbSVlastimil Babka case 'v': 1611edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1612edf14cdbSVlastimil Babka names = vmaflag_names; 1613edf14cdbSVlastimil Babka break; 1614edf14cdbSVlastimil Babka case 'g': 1615edf14cdbSVlastimil Babka flags = *(gfp_t *)flags_ptr; 1616edf14cdbSVlastimil Babka names = gfpflag_names; 1617edf14cdbSVlastimil Babka break; 1618edf14cdbSVlastimil Babka default: 1619edf14cdbSVlastimil Babka WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]); 1620edf14cdbSVlastimil Babka return buf; 1621edf14cdbSVlastimil Babka } 1622edf14cdbSVlastimil Babka 1623edf14cdbSVlastimil Babka return format_flags(buf, end, flags, names); 1624edf14cdbSVlastimil Babka } 1625edf14cdbSVlastimil Babka 1626ce4fecf1SPantelis Antoniou static const char *device_node_name_for_depth(const struct device_node *np, int depth) 1627ce4fecf1SPantelis Antoniou { 1628ce4fecf1SPantelis Antoniou for ( ; np && depth; depth--) 1629ce4fecf1SPantelis Antoniou np = np->parent; 1630ce4fecf1SPantelis Antoniou 1631ce4fecf1SPantelis Antoniou return kbasename(np->full_name); 1632ce4fecf1SPantelis Antoniou } 1633ce4fecf1SPantelis Antoniou 1634ce4fecf1SPantelis Antoniou static noinline_for_stack 1635ce4fecf1SPantelis Antoniou char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end) 1636ce4fecf1SPantelis Antoniou { 1637ce4fecf1SPantelis Antoniou int depth; 1638ce4fecf1SPantelis Antoniou const struct device_node *parent = np->parent; 1639ce4fecf1SPantelis Antoniou 1640ce4fecf1SPantelis Antoniou /* special case for root node */ 1641ce4fecf1SPantelis Antoniou if (!parent) 1642abd4fe62SAndy Shevchenko return string(buf, end, "/", default_str_spec); 1643ce4fecf1SPantelis Antoniou 1644ce4fecf1SPantelis Antoniou for (depth = 0; parent->parent; depth++) 1645ce4fecf1SPantelis Antoniou parent = parent->parent; 1646ce4fecf1SPantelis Antoniou 1647ce4fecf1SPantelis Antoniou for ( ; depth >= 0; depth--) { 1648abd4fe62SAndy Shevchenko buf = string(buf, end, "/", default_str_spec); 1649ce4fecf1SPantelis Antoniou buf = string(buf, end, device_node_name_for_depth(np, depth), 1650abd4fe62SAndy Shevchenko default_str_spec); 1651ce4fecf1SPantelis Antoniou } 1652ce4fecf1SPantelis Antoniou return buf; 1653ce4fecf1SPantelis Antoniou } 1654ce4fecf1SPantelis Antoniou 1655ce4fecf1SPantelis Antoniou static noinline_for_stack 1656ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn, 1657ce4fecf1SPantelis Antoniou struct printf_spec spec, const char *fmt) 1658ce4fecf1SPantelis Antoniou { 1659ce4fecf1SPantelis Antoniou char tbuf[sizeof("xxxx") + 1]; 1660ce4fecf1SPantelis Antoniou const char *p; 1661ce4fecf1SPantelis Antoniou int ret; 1662ce4fecf1SPantelis Antoniou char *buf_start = buf; 1663ce4fecf1SPantelis Antoniou struct property *prop; 1664ce4fecf1SPantelis Antoniou bool has_mult, pass; 1665ce4fecf1SPantelis Antoniou static const struct printf_spec num_spec = { 1666ce4fecf1SPantelis Antoniou .flags = SMALL, 1667ce4fecf1SPantelis Antoniou .field_width = -1, 1668ce4fecf1SPantelis Antoniou .precision = -1, 1669ce4fecf1SPantelis Antoniou .base = 10, 1670ce4fecf1SPantelis Antoniou }; 1671ce4fecf1SPantelis Antoniou 1672ce4fecf1SPantelis Antoniou struct printf_spec str_spec = spec; 1673ce4fecf1SPantelis Antoniou str_spec.field_width = -1; 1674ce4fecf1SPantelis Antoniou 1675ce4fecf1SPantelis Antoniou if (!IS_ENABLED(CONFIG_OF)) 1676ce4fecf1SPantelis Antoniou return string(buf, end, "(!OF)", spec); 1677ce4fecf1SPantelis Antoniou 1678ce4fecf1SPantelis Antoniou if ((unsigned long)dn < PAGE_SIZE) 1679ce4fecf1SPantelis Antoniou return string(buf, end, "(null)", spec); 1680ce4fecf1SPantelis Antoniou 1681ce4fecf1SPantelis Antoniou /* simple case without anything any more format specifiers */ 1682ce4fecf1SPantelis Antoniou fmt++; 1683ce4fecf1SPantelis Antoniou if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 1684ce4fecf1SPantelis Antoniou fmt = "f"; 1685ce4fecf1SPantelis Antoniou 1686ce4fecf1SPantelis Antoniou for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 1687ce4fecf1SPantelis Antoniou if (pass) { 1688ce4fecf1SPantelis Antoniou if (buf < end) 1689ce4fecf1SPantelis Antoniou *buf = ':'; 1690ce4fecf1SPantelis Antoniou buf++; 1691ce4fecf1SPantelis Antoniou } 1692ce4fecf1SPantelis Antoniou 1693ce4fecf1SPantelis Antoniou switch (*fmt) { 1694ce4fecf1SPantelis Antoniou case 'f': /* full_name */ 1695ce4fecf1SPantelis Antoniou buf = device_node_gen_full_name(dn, buf, end); 1696ce4fecf1SPantelis Antoniou break; 1697ce4fecf1SPantelis Antoniou case 'n': /* name */ 1698ce4fecf1SPantelis Antoniou buf = string(buf, end, dn->name, str_spec); 1699ce4fecf1SPantelis Antoniou break; 1700ce4fecf1SPantelis Antoniou case 'p': /* phandle */ 1701ce4fecf1SPantelis Antoniou buf = number(buf, end, (unsigned int)dn->phandle, num_spec); 1702ce4fecf1SPantelis Antoniou break; 1703ce4fecf1SPantelis Antoniou case 'P': /* path-spec */ 1704ce4fecf1SPantelis Antoniou p = kbasename(of_node_full_name(dn)); 1705ce4fecf1SPantelis Antoniou if (!p[1]) 1706ce4fecf1SPantelis Antoniou p = "/"; 1707ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 1708ce4fecf1SPantelis Antoniou break; 1709ce4fecf1SPantelis Antoniou case 'F': /* flags */ 1710ce4fecf1SPantelis Antoniou tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 1711ce4fecf1SPantelis Antoniou tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 1712ce4fecf1SPantelis Antoniou tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 1713ce4fecf1SPantelis Antoniou tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 1714ce4fecf1SPantelis Antoniou tbuf[4] = 0; 1715ce4fecf1SPantelis Antoniou buf = string(buf, end, tbuf, str_spec); 1716ce4fecf1SPantelis Antoniou break; 1717ce4fecf1SPantelis Antoniou case 'c': /* major compatible string */ 1718ce4fecf1SPantelis Antoniou ret = of_property_read_string(dn, "compatible", &p); 1719ce4fecf1SPantelis Antoniou if (!ret) 1720ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 1721ce4fecf1SPantelis Antoniou break; 1722ce4fecf1SPantelis Antoniou case 'C': /* full compatible string */ 1723ce4fecf1SPantelis Antoniou has_mult = false; 1724ce4fecf1SPantelis Antoniou of_property_for_each_string(dn, "compatible", prop, p) { 1725ce4fecf1SPantelis Antoniou if (has_mult) 1726ce4fecf1SPantelis Antoniou buf = string(buf, end, ",", str_spec); 1727ce4fecf1SPantelis Antoniou buf = string(buf, end, "\"", str_spec); 1728ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 1729ce4fecf1SPantelis Antoniou buf = string(buf, end, "\"", str_spec); 1730ce4fecf1SPantelis Antoniou 1731ce4fecf1SPantelis Antoniou has_mult = true; 1732ce4fecf1SPantelis Antoniou } 1733ce4fecf1SPantelis Antoniou break; 1734ce4fecf1SPantelis Antoniou default: 1735ce4fecf1SPantelis Antoniou break; 1736ce4fecf1SPantelis Antoniou } 1737ce4fecf1SPantelis Antoniou } 1738ce4fecf1SPantelis Antoniou 1739ce4fecf1SPantelis Antoniou return widen_string(buf, buf - buf_start, end, spec); 1740ce4fecf1SPantelis Antoniou } 1741ce4fecf1SPantelis Antoniou 17424d8a743cSLinus Torvalds /* 17434d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 17444d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 17454d8a743cSLinus Torvalds * specifiers. 17464d8a743cSLinus Torvalds * 17470b523769SJoe Perches * Please update scripts/checkpatch.pl when adding/removing conversion 17480b523769SJoe Perches * characters. (Search for "check for vsprintf extension"). 17490b523769SJoe Perches * 1750332d2e78SLinus Torvalds * Right now we handle: 17510fe1ef24SLinus Torvalds * 1752cdb7e52dSSergey Senozhatsky * - 'S' For symbolic direct pointers (or function descriptors) with offset 1753cdb7e52dSSergey Senozhatsky * - 's' For symbolic direct pointers (or function descriptors) without offset 1754cdb7e52dSSergey Senozhatsky * - 'F' Same as 'S' 1755cdb7e52dSSergey Senozhatsky * - 'f' Same as 's' 1756b0d33c2bSJoe Perches * - '[FfSs]R' as above with __builtin_extract_return_addr() translation 17570f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 1758c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 1759c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 1760dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 1761dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 1762dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 1763dbc760bcSTejun Heo * range-list format instead of hex format 1764dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 1765dd45c9cfSHarvey Harrison * usual colon-separated hex notation 17668a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 1767bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 1768c8e00060SJoe Perches * with a dash-separated hex notation 17697c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 17708a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 17718a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 17728a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 177310679643SDaniel Borkmann * [S][pfs] 177410679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 177510679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 17768a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 17778a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 17788a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 177910679643SDaniel Borkmann * [S][pfs] 178010679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 178110679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 178210679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 178310679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 178429cf519eSJoe Perches * http://tools.ietf.org/html/rfc5952 178571dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 178671dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 178771dca95dSAndy Shevchenko * details): 178871dca95dSAndy Shevchenko * a - ESCAPE_ANY 178971dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 179071dca95dSAndy Shevchenko * h - ESCAPE_HEX 179171dca95dSAndy Shevchenko * n - ESCAPE_NULL 179271dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 179371dca95dSAndy Shevchenko * p - ESCAPE_NP 179471dca95dSAndy Shevchenko * s - ESCAPE_SPACE 179571dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 17969ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 17979ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 17989ac6e44eSJoe Perches * Options for %pU are: 17999ac6e44eSJoe Perches * b big endian lower case hex (default) 18009ac6e44eSJoe Perches * B big endian UPPER case hex 18019ac6e44eSJoe Perches * l little endian lower case hex 18029ac6e44eSJoe Perches * L little endian UPPER case hex 18039ac6e44eSJoe Perches * big endian output byte order is: 18049ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 18059ac6e44eSJoe Perches * little endian output byte order is: 18069ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 18077db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 18087db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 18097db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 18107db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 18117db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 1812455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 1813c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 181431550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 181531550a16SAndy Shevchenko * a certain separator (' ' by default): 181631550a16SAndy Shevchenko * C colon 181731550a16SAndy Shevchenko * D dash 181831550a16SAndy Shevchenko * N no separator 181931550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 182031550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 1821aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 1822aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 1823c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 1824c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 18251031bc58SDmitry Monakhov * - 'g' For block_device name (gendisk + partition number) 1826900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 1827900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 1828900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 1829900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 1830900cca29SGeert Uytterhoeven * - 'Cr' For a clock, it prints the current rate of the clock 1831edf14cdbSVlastimil Babka * - 'G' For flags to be printed as a collection of symbolic strings that would 1832edf14cdbSVlastimil Babka * construct the specific value. Supported flags given by option: 1833edf14cdbSVlastimil Babka * p page flags (see struct page) given as pointer to unsigned long 1834edf14cdbSVlastimil Babka * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 1835edf14cdbSVlastimil Babka * v vma flags (VM_*) given as pointer to unsigned long 1836ce4fecf1SPantelis Antoniou * - 'O' For a kobject based struct. Must be one of the following: 1837ce4fecf1SPantelis Antoniou * - 'OF[fnpPcCF]' For a device tree object 1838ce4fecf1SPantelis Antoniou * Without any optional arguments prints the full_name 1839ce4fecf1SPantelis Antoniou * f device node full_name 1840ce4fecf1SPantelis Antoniou * n device node name 1841ce4fecf1SPantelis Antoniou * p device node phandle 1842ce4fecf1SPantelis Antoniou * P device node path spec (name + @unit) 1843ce4fecf1SPantelis Antoniou * F device node flags 1844ce4fecf1SPantelis Antoniou * c major compatible string 1845ce4fecf1SPantelis Antoniou * C full compatible string 18465e4ee7b1SMartin Kletzander * 18477b1924a1STobin C. Harding * - 'x' For printing the address. Equivalent to "%lx". 18487b1924a1STobin C. Harding * 1849b3ed2321STobin C. Harding * ** When making changes please also update: 1850b3ed2321STobin C. Harding * Documentation/core-api/printk-formats.rst 18519ac6e44eSJoe Perches * 1852ad67b74dSTobin C. Harding * Note: The default behaviour (unadorned %p) is to hash the address, 1853ad67b74dSTobin C. Harding * rendering it useful as a unique identifier. 18544d8a743cSLinus Torvalds */ 1855cf3b429bSJoe Perches static noinline_for_stack 1856cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 1857fef20d9cSFrederic Weisbecker struct printf_spec spec) 185878a8bf69SLinus Torvalds { 185980c9eb46SRasmus Villemoes const int default_width = 2 * sizeof(void *); 1860725fe002SGrant Likely 18613a129cc2SAdam Borowski if (!ptr && *fmt != 'K' && *fmt != 'x') { 18625e057981SJoe Perches /* 18635e057981SJoe Perches * Print (null) with the same width as a pointer so it makes 18645e057981SJoe Perches * tabular output look nice. 18655e057981SJoe Perches */ 18665e057981SJoe Perches if (spec.field_width == -1) 1867725fe002SGrant Likely spec.field_width = default_width; 1868fef20d9cSFrederic Weisbecker return string(buf, end, "(null)", spec); 18695e057981SJoe Perches } 1870d97106abSLinus Torvalds 18710fe1ef24SLinus Torvalds switch (*fmt) { 18720fe1ef24SLinus Torvalds case 'F': 18730c8b946eSFrederic Weisbecker case 'f': 18740fe1ef24SLinus Torvalds case 'S': 18759ac6e44eSJoe Perches case 's': 187604b8eb7aSSergey Senozhatsky ptr = dereference_symbol_descriptor(ptr); 187704b8eb7aSSergey Senozhatsky /* Fallthrough */ 18780f77a8d3SNamhyung Kim case 'B': 1879b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 1880332d2e78SLinus Torvalds case 'R': 1881c7dabef8SBjorn Helgaas case 'r': 1882fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 188331550a16SAndy Shevchenko case 'h': 188431550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 1885dbc760bcSTejun Heo case 'b': 1886dbc760bcSTejun Heo switch (fmt[1]) { 1887dbc760bcSTejun Heo case 'l': 1888dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 1889dbc760bcSTejun Heo default: 1890dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 1891dbc760bcSTejun Heo } 18928a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 18938a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 189476597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 189576597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 18968a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 18978a27f7c9SJoe Perches case 'I': /* Formatted IP supported 18988a27f7c9SJoe Perches * 4: 1.2.3.4 18998a27f7c9SJoe Perches * 6: 0001:0203:...:0708 19008a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 19018a27f7c9SJoe Perches */ 19028a27f7c9SJoe Perches case 'i': /* Contiguous: 19038a27f7c9SJoe Perches * 4: 001.002.003.004 19048a27f7c9SJoe Perches * 6: 000102...0f 19058a27f7c9SJoe Perches */ 19068a27f7c9SJoe Perches switch (fmt[1]) { 19078a27f7c9SJoe Perches case '6': 19088a27f7c9SJoe Perches return ip6_addr_string(buf, end, ptr, spec, fmt); 19098a27f7c9SJoe Perches case '4': 19108a27f7c9SJoe Perches return ip4_addr_string(buf, end, ptr, spec, fmt); 191110679643SDaniel Borkmann case 'S': { 191210679643SDaniel Borkmann const union { 191310679643SDaniel Borkmann struct sockaddr raw; 191410679643SDaniel Borkmann struct sockaddr_in v4; 191510679643SDaniel Borkmann struct sockaddr_in6 v6; 191610679643SDaniel Borkmann } *sa = ptr; 191710679643SDaniel Borkmann 191810679643SDaniel Borkmann switch (sa->raw.sa_family) { 191910679643SDaniel Borkmann case AF_INET: 192010679643SDaniel Borkmann return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 192110679643SDaniel Borkmann case AF_INET6: 192210679643SDaniel Borkmann return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 192310679643SDaniel Borkmann default: 192410679643SDaniel Borkmann return string(buf, end, "(invalid address)", spec); 192510679643SDaniel Borkmann }} 19268a27f7c9SJoe Perches } 19274aa99606SHarvey Harrison break; 192871dca95dSAndy Shevchenko case 'E': 192971dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 19309ac6e44eSJoe Perches case 'U': 19319ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 19327db6f5fbSJoe Perches case 'V': 19335756b76eSJan Beulich { 19345756b76eSJan Beulich va_list va; 19355756b76eSJan Beulich 19365756b76eSJan Beulich va_copy(va, *((struct va_format *)ptr)->va); 19375756b76eSJan Beulich buf += vsnprintf(buf, end > buf ? end - buf : 0, 19385756b76eSJan Beulich ((struct va_format *)ptr)->fmt, va); 19395756b76eSJan Beulich va_end(va); 19405756b76eSJan Beulich return buf; 19415756b76eSJan Beulich } 1942455cd5abSDan Rosenberg case 'K': 1943ef0010a3SLinus Torvalds if (!kptr_restrict) 1944ef0010a3SLinus Torvalds break; 194557e73442STobin C. Harding return restricted_pointer(buf, end, ptr, spec); 1946c8f44affSMichał Mirosław case 'N': 1947*431bca24SGeert Uytterhoeven return netdev_bits(buf, end, ptr, spec, fmt); 19487d799210SStepan Moskovchenko case 'a': 19493cab1e71SAndy Shevchenko return address_val(buf, end, ptr, fmt); 19504b6ccca7SAl Viro case 'd': 19514b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 1952900cca29SGeert Uytterhoeven case 'C': 1953900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 19544b6ccca7SAl Viro case 'D': 19554b6ccca7SAl Viro return dentry_name(buf, end, 19564b6ccca7SAl Viro ((const struct file *)ptr)->f_path.dentry, 19574b6ccca7SAl Viro spec, fmt); 19581031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 19591031bc58SDmitry Monakhov case 'g': 19601031bc58SDmitry Monakhov return bdev_name(buf, end, ptr, spec, fmt); 19611031bc58SDmitry Monakhov #endif 19621031bc58SDmitry Monakhov 1963edf14cdbSVlastimil Babka case 'G': 1964edf14cdbSVlastimil Babka return flags_string(buf, end, ptr, fmt); 1965ce4fecf1SPantelis Antoniou case 'O': 1966ce4fecf1SPantelis Antoniou switch (fmt[1]) { 1967ce4fecf1SPantelis Antoniou case 'F': 1968ce4fecf1SPantelis Antoniou return device_node_string(buf, end, ptr, spec, fmt + 1); 1969ce4fecf1SPantelis Antoniou } 1970554ec508SBart Van Assche break; 19717b1924a1STobin C. Harding case 'x': 19727b1924a1STobin C. Harding return pointer_string(buf, end, ptr, spec); 19730fe1ef24SLinus Torvalds } 1974fef20d9cSFrederic Weisbecker 1975ad67b74dSTobin C. Harding /* default is to _not_ leak addresses, hash before printing */ 1976ad67b74dSTobin C. Harding return ptr_to_id(buf, end, ptr, spec); 1977fef20d9cSFrederic Weisbecker } 1978fef20d9cSFrederic Weisbecker 1979fef20d9cSFrederic Weisbecker /* 1980fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 1981fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 1982fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 1983fef20d9cSFrederic Weisbecker * to go on the next call). 1984fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 1985fef20d9cSFrederic Weisbecker * 1986fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 1987fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 1988fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 19895b5e0928SAlexey Dobriyan * 'Z' changed to 'z' --adobriyan 2017-01-25 1990fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 1991fef20d9cSFrederic Weisbecker * 1992fef20d9cSFrederic Weisbecker * @fmt: the format string 1993fef20d9cSFrederic Weisbecker * @type of the token returned 1994fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 1995fef20d9cSFrederic Weisbecker * @field_width: overwritten width 1996fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 1997fef20d9cSFrederic Weisbecker * @precision: precision of a number 1998fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 1999fef20d9cSFrederic Weisbecker */ 2000cf3b429bSJoe Perches static noinline_for_stack 2001cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 2002fef20d9cSFrederic Weisbecker { 2003fef20d9cSFrederic Weisbecker const char *start = fmt; 2004d0484193SRasmus Villemoes char qualifier; 2005fef20d9cSFrederic Weisbecker 2006fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 2007ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 2008fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 2009fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 2010fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 2011fef20d9cSFrederic Weisbecker } 2012fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2013fef20d9cSFrederic Weisbecker goto precision; 2014fef20d9cSFrederic Weisbecker } 2015fef20d9cSFrederic Weisbecker 2016fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 2017fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 2018fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2019fef20d9cSFrederic Weisbecker spec->precision = 0; 2020fef20d9cSFrederic Weisbecker 2021fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2022fef20d9cSFrederic Weisbecker goto qualifier; 2023fef20d9cSFrederic Weisbecker } 2024fef20d9cSFrederic Weisbecker 2025fef20d9cSFrederic Weisbecker /* By default */ 2026fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2027fef20d9cSFrederic Weisbecker 2028fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 2029fef20d9cSFrederic Weisbecker if (*fmt == '%') 2030fef20d9cSFrederic Weisbecker break; 2031fef20d9cSFrederic Weisbecker } 2032fef20d9cSFrederic Weisbecker 2033fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 2034fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 2035fef20d9cSFrederic Weisbecker return fmt - start; 2036fef20d9cSFrederic Weisbecker 2037fef20d9cSFrederic Weisbecker /* Process flags */ 2038fef20d9cSFrederic Weisbecker spec->flags = 0; 2039fef20d9cSFrederic Weisbecker 2040fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 2041fef20d9cSFrederic Weisbecker bool found = true; 2042fef20d9cSFrederic Weisbecker 2043fef20d9cSFrederic Weisbecker ++fmt; 2044fef20d9cSFrederic Weisbecker 2045fef20d9cSFrederic Weisbecker switch (*fmt) { 2046fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 2047fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 2048fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 2049fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 2050fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 2051fef20d9cSFrederic Weisbecker default: found = false; 2052fef20d9cSFrederic Weisbecker } 2053fef20d9cSFrederic Weisbecker 2054fef20d9cSFrederic Weisbecker if (!found) 2055fef20d9cSFrederic Weisbecker break; 2056fef20d9cSFrederic Weisbecker } 2057fef20d9cSFrederic Weisbecker 2058fef20d9cSFrederic Weisbecker /* get field width */ 2059fef20d9cSFrederic Weisbecker spec->field_width = -1; 2060fef20d9cSFrederic Weisbecker 2061fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 2062fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 2063fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 2064fef20d9cSFrederic Weisbecker /* it's the next argument */ 2065ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 2066fef20d9cSFrederic Weisbecker return ++fmt - start; 2067fef20d9cSFrederic Weisbecker } 2068fef20d9cSFrederic Weisbecker 2069fef20d9cSFrederic Weisbecker precision: 2070fef20d9cSFrederic Weisbecker /* get the precision */ 2071fef20d9cSFrederic Weisbecker spec->precision = -1; 2072fef20d9cSFrederic Weisbecker if (*fmt == '.') { 2073fef20d9cSFrederic Weisbecker ++fmt; 2074fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 2075fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 2076fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2077fef20d9cSFrederic Weisbecker spec->precision = 0; 2078fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 2079fef20d9cSFrederic Weisbecker /* it's the next argument */ 2080adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 2081fef20d9cSFrederic Weisbecker return ++fmt - start; 2082fef20d9cSFrederic Weisbecker } 2083fef20d9cSFrederic Weisbecker } 2084fef20d9cSFrederic Weisbecker 2085fef20d9cSFrederic Weisbecker qualifier: 2086fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 2087d0484193SRasmus Villemoes qualifier = 0; 208875fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 20895b5e0928SAlexey Dobriyan *fmt == 'z' || *fmt == 't') { 2090d0484193SRasmus Villemoes qualifier = *fmt++; 2091d0484193SRasmus Villemoes if (unlikely(qualifier == *fmt)) { 2092d0484193SRasmus Villemoes if (qualifier == 'l') { 2093d0484193SRasmus Villemoes qualifier = 'L'; 2094fef20d9cSFrederic Weisbecker ++fmt; 2095d0484193SRasmus Villemoes } else if (qualifier == 'h') { 2096d0484193SRasmus Villemoes qualifier = 'H'; 2097a4e94ef0SZhaolei ++fmt; 2098a4e94ef0SZhaolei } 2099fef20d9cSFrederic Weisbecker } 2100fef20d9cSFrederic Weisbecker } 2101fef20d9cSFrederic Weisbecker 2102fef20d9cSFrederic Weisbecker /* default base */ 2103fef20d9cSFrederic Weisbecker spec->base = 10; 2104fef20d9cSFrederic Weisbecker switch (*fmt) { 2105fef20d9cSFrederic Weisbecker case 'c': 2106fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 2107fef20d9cSFrederic Weisbecker return ++fmt - start; 2108fef20d9cSFrederic Weisbecker 2109fef20d9cSFrederic Weisbecker case 's': 2110fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 2111fef20d9cSFrederic Weisbecker return ++fmt - start; 2112fef20d9cSFrederic Weisbecker 2113fef20d9cSFrederic Weisbecker case 'p': 2114fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 2115ffbfed03SRasmus Villemoes return ++fmt - start; 2116fef20d9cSFrederic Weisbecker 2117fef20d9cSFrederic Weisbecker case '%': 2118fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 2119fef20d9cSFrederic Weisbecker return ++fmt - start; 2120fef20d9cSFrederic Weisbecker 2121fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 2122fef20d9cSFrederic Weisbecker case 'o': 2123fef20d9cSFrederic Weisbecker spec->base = 8; 2124fef20d9cSFrederic Weisbecker break; 2125fef20d9cSFrederic Weisbecker 2126fef20d9cSFrederic Weisbecker case 'x': 2127fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 21287e6bd6f3SAndy Shevchenko /* fall through */ 2129fef20d9cSFrederic Weisbecker 2130fef20d9cSFrederic Weisbecker case 'X': 2131fef20d9cSFrederic Weisbecker spec->base = 16; 2132fef20d9cSFrederic Weisbecker break; 2133fef20d9cSFrederic Weisbecker 2134fef20d9cSFrederic Weisbecker case 'd': 2135fef20d9cSFrederic Weisbecker case 'i': 213639e874f8SFrederic Weisbecker spec->flags |= SIGN; 2137fef20d9cSFrederic Weisbecker case 'u': 2138fef20d9cSFrederic Weisbecker break; 2139fef20d9cSFrederic Weisbecker 2140708d96fdSRyan Mallon case 'n': 2141708d96fdSRyan Mallon /* 2142b006f19bSRasmus Villemoes * Since %n poses a greater security risk than 2143b006f19bSRasmus Villemoes * utility, treat it as any other invalid or 2144b006f19bSRasmus Villemoes * unsupported format specifier. 2145708d96fdSRyan Mallon */ 2146708d96fdSRyan Mallon /* Fall-through */ 2147708d96fdSRyan Mallon 2148fef20d9cSFrederic Weisbecker default: 2149b006f19bSRasmus Villemoes WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 2150fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 2151fef20d9cSFrederic Weisbecker return fmt - start; 2152fef20d9cSFrederic Weisbecker } 2153fef20d9cSFrederic Weisbecker 2154d0484193SRasmus Villemoes if (qualifier == 'L') 2155fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 2156d0484193SRasmus Villemoes else if (qualifier == 'l') { 215751be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 215851be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 21595b5e0928SAlexey Dobriyan } else if (qualifier == 'z') { 2160fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 2161d0484193SRasmus Villemoes } else if (qualifier == 't') { 2162fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 2163d0484193SRasmus Villemoes } else if (qualifier == 'H') { 216451be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 216551be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 2166d0484193SRasmus Villemoes } else if (qualifier == 'h') { 216751be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 216851be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 2169fef20d9cSFrederic Weisbecker } else { 217051be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 217151be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 2172fef20d9cSFrederic Weisbecker } 2173fef20d9cSFrederic Weisbecker 2174fef20d9cSFrederic Weisbecker return ++fmt - start; 217578a8bf69SLinus Torvalds } 217678a8bf69SLinus Torvalds 21774d72ba01SRasmus Villemoes static void 21784d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width) 21794d72ba01SRasmus Villemoes { 21804d72ba01SRasmus Villemoes spec->field_width = width; 21814d72ba01SRasmus Villemoes if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 21824d72ba01SRasmus Villemoes spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 21834d72ba01SRasmus Villemoes } 21844d72ba01SRasmus Villemoes } 21854d72ba01SRasmus Villemoes 21864d72ba01SRasmus Villemoes static void 21874d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec) 21884d72ba01SRasmus Villemoes { 21894d72ba01SRasmus Villemoes spec->precision = prec; 21904d72ba01SRasmus Villemoes if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 21914d72ba01SRasmus Villemoes spec->precision = clamp(prec, 0, PRECISION_MAX); 21924d72ba01SRasmus Villemoes } 21934d72ba01SRasmus Villemoes } 21944d72ba01SRasmus Villemoes 21951da177e4SLinus Torvalds /** 21961da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 21971da177e4SLinus Torvalds * @buf: The buffer to place the result into 21981da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 21991da177e4SLinus Torvalds * @fmt: The format string to use 22001da177e4SLinus Torvalds * @args: Arguments for the format string 22011da177e4SLinus Torvalds * 2202d7ec9a05SRasmus Villemoes * This function generally follows C99 vsnprintf, but has some 2203d7ec9a05SRasmus Villemoes * extensions and a few limitations: 2204d7ec9a05SRasmus Villemoes * 22056cc89134Smchehab@s-opensource.com * - ``%n`` is unsupported 22066cc89134Smchehab@s-opensource.com * - ``%p*`` is handled by pointer() 220720036fdcSAndi Kleen * 220827e7c0e8SJonathan Corbet * See pointer() or Documentation/core-api/printk-formats.rst for more 22095e4ee7b1SMartin Kletzander * extensive description. 22105e4ee7b1SMartin Kletzander * 22115e4ee7b1SMartin Kletzander * **Please update the documentation in both places when making changes** 221280f548e0SAndrew Morton * 22131da177e4SLinus Torvalds * The return value is the number of characters which would 22141da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 22151da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 22161da177e4SLinus Torvalds * number of characters written into @buf as return value 221772fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 22181da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 22191da177e4SLinus Torvalds * string is truncated. 22201da177e4SLinus Torvalds * 2221ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 22221da177e4SLinus Torvalds */ 22231da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 22241da177e4SLinus Torvalds { 22251da177e4SLinus Torvalds unsigned long long num; 2226d4be151bSAndré Goddard Rosa char *str, *end; 2227fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 22281da177e4SLinus Torvalds 2229f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 2230f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 22312aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 22321da177e4SLinus Torvalds return 0; 22331da177e4SLinus Torvalds 22341da177e4SLinus Torvalds str = buf; 2235f796937aSJeremy Fitzhardinge end = buf + size; 22361da177e4SLinus Torvalds 2237f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 2238f796937aSJeremy Fitzhardinge if (end < buf) { 22391da177e4SLinus Torvalds end = ((void *)-1); 2240f796937aSJeremy Fitzhardinge size = end - buf; 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 2243fef20d9cSFrederic Weisbecker while (*fmt) { 2244fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2245d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2246fef20d9cSFrederic Weisbecker 2247fef20d9cSFrederic Weisbecker fmt += read; 2248fef20d9cSFrederic Weisbecker 2249fef20d9cSFrederic Weisbecker switch (spec.type) { 2250fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2251fef20d9cSFrederic Weisbecker int copy = read; 2252fef20d9cSFrederic Weisbecker if (str < end) { 2253fef20d9cSFrederic Weisbecker if (copy > end - str) 2254fef20d9cSFrederic Weisbecker copy = end - str; 2255fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2256fef20d9cSFrederic Weisbecker } 2257fef20d9cSFrederic Weisbecker str += read; 2258fef20d9cSFrederic Weisbecker break; 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 2261ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 22624d72ba01SRasmus Villemoes set_field_width(&spec, va_arg(args, int)); 2263fef20d9cSFrederic Weisbecker break; 22641da177e4SLinus Torvalds 2265fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 22664d72ba01SRasmus Villemoes set_precision(&spec, va_arg(args, int)); 2267fef20d9cSFrederic Weisbecker break; 22681da177e4SLinus Torvalds 2269d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2270d4be151bSAndré Goddard Rosa char c; 2271d4be151bSAndré Goddard Rosa 2272fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2273fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2274f796937aSJeremy Fitzhardinge if (str < end) 22751da177e4SLinus Torvalds *str = ' '; 22761da177e4SLinus Torvalds ++str; 2277fef20d9cSFrederic Weisbecker 22781da177e4SLinus Torvalds } 22791da177e4SLinus Torvalds } 22801da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 2281f796937aSJeremy Fitzhardinge if (str < end) 22821da177e4SLinus Torvalds *str = c; 22831da177e4SLinus Torvalds ++str; 2284fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2285f796937aSJeremy Fitzhardinge if (str < end) 22861da177e4SLinus Torvalds *str = ' '; 22871da177e4SLinus Torvalds ++str; 22881da177e4SLinus Torvalds } 2289fef20d9cSFrederic Weisbecker break; 2290d4be151bSAndré Goddard Rosa } 22911da177e4SLinus Torvalds 2292fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 2293fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 2294fef20d9cSFrederic Weisbecker break; 22951da177e4SLinus Torvalds 2296fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2297ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 2298fef20d9cSFrederic Weisbecker spec); 2299fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 23004d8a743cSLinus Torvalds fmt++; 2301fef20d9cSFrederic Weisbecker break; 23021da177e4SLinus Torvalds 2303fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2304f796937aSJeremy Fitzhardinge if (str < end) 23051da177e4SLinus Torvalds *str = '%'; 23061da177e4SLinus Torvalds ++str; 23071da177e4SLinus Torvalds break; 23081da177e4SLinus Torvalds 2309fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 2310b006f19bSRasmus Villemoes /* 2311b006f19bSRasmus Villemoes * Presumably the arguments passed gcc's type 2312b006f19bSRasmus Villemoes * checking, but there is no safe or sane way 2313b006f19bSRasmus Villemoes * for us to continue parsing the format and 2314b006f19bSRasmus Villemoes * fetching from the va_list; the remaining 2315b006f19bSRasmus Villemoes * specifiers and arguments would be out of 2316b006f19bSRasmus Villemoes * sync. 2317b006f19bSRasmus Villemoes */ 2318b006f19bSRasmus Villemoes goto out; 2319fef20d9cSFrederic Weisbecker 2320fef20d9cSFrederic Weisbecker default: 2321fef20d9cSFrederic Weisbecker switch (spec.type) { 2322fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2323fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 2324fef20d9cSFrederic Weisbecker break; 2325fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2326fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 2327fef20d9cSFrederic Weisbecker break; 2328fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2329fef20d9cSFrederic Weisbecker num = va_arg(args, long); 2330fef20d9cSFrederic Weisbecker break; 2331fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2332ef124960SJason Gunthorpe if (spec.flags & SIGN) 2333ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 2334ef124960SJason Gunthorpe else 2335fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 2336fef20d9cSFrederic Weisbecker break; 2337fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2338fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 2339fef20d9cSFrederic Weisbecker break; 2340a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2341a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 2342a4e94ef0SZhaolei break; 2343a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2344a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 2345a4e94ef0SZhaolei break; 2346fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2347fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 2348fef20d9cSFrederic Weisbecker break; 2349fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2350fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 2351fef20d9cSFrederic Weisbecker break; 235239e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 235339e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 2354fef20d9cSFrederic Weisbecker break; 2355fef20d9cSFrederic Weisbecker default: 2356fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 23571da177e4SLinus Torvalds } 2358fef20d9cSFrederic Weisbecker 2359fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 23601da177e4SLinus Torvalds } 2361fef20d9cSFrederic Weisbecker } 2362fef20d9cSFrederic Weisbecker 2363b006f19bSRasmus Villemoes out: 2364f796937aSJeremy Fitzhardinge if (size > 0) { 2365f796937aSJeremy Fitzhardinge if (str < end) 23661da177e4SLinus Torvalds *str = '\0'; 2367f796937aSJeremy Fitzhardinge else 23680a6047eeSLinus Torvalds end[-1] = '\0'; 2369f796937aSJeremy Fitzhardinge } 2370fef20d9cSFrederic Weisbecker 2371f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 23721da177e4SLinus Torvalds return str-buf; 2373fef20d9cSFrederic Weisbecker 23741da177e4SLinus Torvalds } 23751da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 23761da177e4SLinus Torvalds 23771da177e4SLinus Torvalds /** 23781da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 23791da177e4SLinus Torvalds * @buf: The buffer to place the result into 23801da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 23811da177e4SLinus Torvalds * @fmt: The format string to use 23821da177e4SLinus Torvalds * @args: Arguments for the format string 23831da177e4SLinus Torvalds * 23841da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2385b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 23861da177e4SLinus Torvalds * returns 0. 23871da177e4SLinus Torvalds * 2388ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 238920036fdcSAndi Kleen * 239020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 23911da177e4SLinus Torvalds */ 23921da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 23931da177e4SLinus Torvalds { 23941da177e4SLinus Torvalds int i; 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 23977b9186f5SAndré Goddard Rosa 2398b921c69fSAnton Arapov if (likely(i < size)) 2399b921c69fSAnton Arapov return i; 2400b921c69fSAnton Arapov if (size != 0) 2401b921c69fSAnton Arapov return size - 1; 2402b921c69fSAnton Arapov return 0; 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 24051da177e4SLinus Torvalds 24061da177e4SLinus Torvalds /** 24071da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 24081da177e4SLinus Torvalds * @buf: The buffer to place the result into 24091da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 24101da177e4SLinus Torvalds * @fmt: The format string to use 24111da177e4SLinus Torvalds * @...: Arguments for the format string 24121da177e4SLinus Torvalds * 24131da177e4SLinus Torvalds * The return value is the number of characters which would be 24141da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 24151da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 24161da177e4SLinus Torvalds * @size, the resulting string is truncated. 241720036fdcSAndi Kleen * 241820036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 24191da177e4SLinus Torvalds */ 24201da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 24211da177e4SLinus Torvalds { 24221da177e4SLinus Torvalds va_list args; 24231da177e4SLinus Torvalds int i; 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds va_start(args, fmt); 24261da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 24271da177e4SLinus Torvalds va_end(args); 24287b9186f5SAndré Goddard Rosa 24291da177e4SLinus Torvalds return i; 24301da177e4SLinus Torvalds } 24311da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds /** 24341da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 24351da177e4SLinus Torvalds * @buf: The buffer to place the result into 24361da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 24371da177e4SLinus Torvalds * @fmt: The format string to use 24381da177e4SLinus Torvalds * @...: Arguments for the format string 24391da177e4SLinus Torvalds * 24401da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2441b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 24421da177e4SLinus Torvalds */ 24431da177e4SLinus Torvalds 24441da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 24451da177e4SLinus Torvalds { 24461da177e4SLinus Torvalds va_list args; 24471da177e4SLinus Torvalds int i; 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds va_start(args, fmt); 2450b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 24511da177e4SLinus Torvalds va_end(args); 24527b9186f5SAndré Goddard Rosa 2453b903c0b8SChangli Gao return i; 24541da177e4SLinus Torvalds } 24551da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds /** 24581da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 24591da177e4SLinus Torvalds * @buf: The buffer to place the result into 24601da177e4SLinus Torvalds * @fmt: The format string to use 24611da177e4SLinus Torvalds * @args: Arguments for the format string 24621da177e4SLinus Torvalds * 24631da177e4SLinus Torvalds * The function returns the number of characters written 246472fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 24651da177e4SLinus Torvalds * buffer overflows. 24661da177e4SLinus Torvalds * 2467ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 246820036fdcSAndi Kleen * 246920036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 24701da177e4SLinus Torvalds */ 24711da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 24721da177e4SLinus Torvalds { 24731da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 24741da177e4SLinus Torvalds } 24751da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds /** 24781da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 24791da177e4SLinus Torvalds * @buf: The buffer to place the result into 24801da177e4SLinus Torvalds * @fmt: The format string to use 24811da177e4SLinus Torvalds * @...: Arguments for the format string 24821da177e4SLinus Torvalds * 24831da177e4SLinus Torvalds * The function returns the number of characters written 248472fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 24851da177e4SLinus Torvalds * buffer overflows. 248620036fdcSAndi Kleen * 248720036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 24881da177e4SLinus Torvalds */ 24891da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 24901da177e4SLinus Torvalds { 24911da177e4SLinus Torvalds va_list args; 24921da177e4SLinus Torvalds int i; 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds va_start(args, fmt); 24951da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 24961da177e4SLinus Torvalds va_end(args); 24977b9186f5SAndré Goddard Rosa 24981da177e4SLinus Torvalds return i; 24991da177e4SLinus Torvalds } 25001da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 25011da177e4SLinus Torvalds 25024370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 25034370aa4aSLai Jiangshan /* 25044370aa4aSLai Jiangshan * bprintf service: 25054370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 25064370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 25074370aa4aSLai Jiangshan */ 25084370aa4aSLai Jiangshan 25094370aa4aSLai Jiangshan /** 25104370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 25114370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 25124370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 25134370aa4aSLai Jiangshan * @fmt: The format string to use 25144370aa4aSLai Jiangshan * @args: Arguments for the format string 25154370aa4aSLai Jiangshan * 25164370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 2517da3dae54SMasanari Iida * is skipped. 25184370aa4aSLai Jiangshan * 25194370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 25204370aa4aSLai Jiangshan * the given input. 25214370aa4aSLai Jiangshan * 25224370aa4aSLai Jiangshan * NOTE: 25234370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 25244370aa4aSLai Jiangshan * valid for bstr_printf(). 25254370aa4aSLai Jiangshan */ 25264370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 25274370aa4aSLai Jiangshan { 2528fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 25294370aa4aSLai Jiangshan char *str, *end; 2530841a915dSSteven Rostedt (VMware) int width; 25314370aa4aSLai Jiangshan 25324370aa4aSLai Jiangshan str = (char *)bin_buf; 25334370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 25344370aa4aSLai Jiangshan 25354370aa4aSLai Jiangshan #define save_arg(type) \ 2536841a915dSSteven Rostedt (VMware) ({ \ 25374370aa4aSLai Jiangshan unsigned long long value; \ 2538841a915dSSteven Rostedt (VMware) if (sizeof(type) == 8) { \ 2539841a915dSSteven Rostedt (VMware) unsigned long long val8; \ 25404370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 2541841a915dSSteven Rostedt (VMware) val8 = va_arg(args, unsigned long long); \ 25424370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 2543841a915dSSteven Rostedt (VMware) *(u32 *)str = *(u32 *)&val8; \ 2544841a915dSSteven Rostedt (VMware) *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 25454370aa4aSLai Jiangshan } \ 2546841a915dSSteven Rostedt (VMware) value = val8; \ 25474370aa4aSLai Jiangshan } else { \ 2548841a915dSSteven Rostedt (VMware) unsigned int val4; \ 25494370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 2550841a915dSSteven Rostedt (VMware) val4 = va_arg(args, int); \ 25514370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 2552841a915dSSteven Rostedt (VMware) *(typeof(type) *)str = (type)(long)val4; \ 2553841a915dSSteven Rostedt (VMware) value = (unsigned long long)val4; \ 25544370aa4aSLai Jiangshan } \ 25554370aa4aSLai Jiangshan str += sizeof(type); \ 2556841a915dSSteven Rostedt (VMware) value; \ 2557841a915dSSteven Rostedt (VMware) }) 25584370aa4aSLai Jiangshan 2559fef20d9cSFrederic Weisbecker while (*fmt) { 2560d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 25614370aa4aSLai Jiangshan 2562fef20d9cSFrederic Weisbecker fmt += read; 2563fef20d9cSFrederic Weisbecker 2564fef20d9cSFrederic Weisbecker switch (spec.type) { 2565fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 2566d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 2567fef20d9cSFrederic Weisbecker break; 2568b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2569b006f19bSRasmus Villemoes goto out; 2570fef20d9cSFrederic Weisbecker 2571ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2572fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2573841a915dSSteven Rostedt (VMware) width = (int)save_arg(int); 2574841a915dSSteven Rostedt (VMware) /* Pointers may require the width */ 2575841a915dSSteven Rostedt (VMware) if (*fmt == 'p') 2576841a915dSSteven Rostedt (VMware) set_field_width(&spec, width); 2577fef20d9cSFrederic Weisbecker break; 25784370aa4aSLai Jiangshan 2579fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 25804370aa4aSLai Jiangshan save_arg(char); 2581fef20d9cSFrederic Weisbecker break; 2582fef20d9cSFrederic Weisbecker 2583fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 25844370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 25854370aa4aSLai Jiangshan size_t len; 25866c356634SAndré Goddard Rosa 25874370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 25884370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 25890f4f81dcSAndré Goddard Rosa save_str = "(null)"; 25906c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 25916c356634SAndré Goddard Rosa if (str + len < end) 25926c356634SAndré Goddard Rosa memcpy(str, save_str, len); 25936c356634SAndré Goddard Rosa str += len; 2594fef20d9cSFrederic Weisbecker break; 25954370aa4aSLai Jiangshan } 2596fef20d9cSFrederic Weisbecker 2597fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2598841a915dSSteven Rostedt (VMware) /* Dereferenced pointers must be done now */ 2599841a915dSSteven Rostedt (VMware) switch (*fmt) { 2600841a915dSSteven Rostedt (VMware) /* Dereference of functions is still OK */ 2601841a915dSSteven Rostedt (VMware) case 'S': 2602841a915dSSteven Rostedt (VMware) case 's': 2603841a915dSSteven Rostedt (VMware) case 'F': 2604841a915dSSteven Rostedt (VMware) case 'f': 26051e6338cfSSteven Rostedt (VMware) case 'x': 26061e6338cfSSteven Rostedt (VMware) case 'K': 26074370aa4aSLai Jiangshan save_arg(void *); 2608841a915dSSteven Rostedt (VMware) break; 2609841a915dSSteven Rostedt (VMware) default: 2610841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 2611841a915dSSteven Rostedt (VMware) save_arg(void *); 2612841a915dSSteven Rostedt (VMware) break; 2613841a915dSSteven Rostedt (VMware) } 2614841a915dSSteven Rostedt (VMware) str = pointer(fmt, str, end, va_arg(args, void *), 2615841a915dSSteven Rostedt (VMware) spec); 2616841a915dSSteven Rostedt (VMware) if (str + 1 < end) 2617841a915dSSteven Rostedt (VMware) *str++ = '\0'; 2618841a915dSSteven Rostedt (VMware) else 2619841a915dSSteven Rostedt (VMware) end[-1] = '\0'; /* Must be nul terminated */ 2620841a915dSSteven Rostedt (VMware) } 26214370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2622fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 26234370aa4aSLai Jiangshan fmt++; 2624fef20d9cSFrederic Weisbecker break; 2625fef20d9cSFrederic Weisbecker 2626fef20d9cSFrederic Weisbecker default: 2627fef20d9cSFrederic Weisbecker switch (spec.type) { 2628fef20d9cSFrederic Weisbecker 2629fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2630fef20d9cSFrederic Weisbecker save_arg(long long); 2631fef20d9cSFrederic Weisbecker break; 2632fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2633fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2634fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2635fef20d9cSFrederic Weisbecker break; 2636fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2637fef20d9cSFrederic Weisbecker save_arg(size_t); 2638fef20d9cSFrederic Weisbecker break; 2639fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2640fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2641fef20d9cSFrederic Weisbecker break; 2642a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2643a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2644a4e94ef0SZhaolei save_arg(char); 2645a4e94ef0SZhaolei break; 2646fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2647fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2648fef20d9cSFrederic Weisbecker save_arg(short); 2649fef20d9cSFrederic Weisbecker break; 2650fef20d9cSFrederic Weisbecker default: 2651fef20d9cSFrederic Weisbecker save_arg(int); 2652fef20d9cSFrederic Weisbecker } 2653fef20d9cSFrederic Weisbecker } 2654fef20d9cSFrederic Weisbecker } 2655fef20d9cSFrederic Weisbecker 2656b006f19bSRasmus Villemoes out: 26577b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2658fef20d9cSFrederic Weisbecker #undef save_arg 26594370aa4aSLai Jiangshan } 26604370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 26614370aa4aSLai Jiangshan 26624370aa4aSLai Jiangshan /** 26634370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 26644370aa4aSLai Jiangshan * @buf: The buffer to place the result into 26654370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 26664370aa4aSLai Jiangshan * @fmt: The format string to use 26674370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 26684370aa4aSLai Jiangshan * 26694370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 26704370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 26714370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 26724370aa4aSLai Jiangshan * 26734370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 26740efb4d20SSteven Rostedt * see vsnprintf comment for details. 26754370aa4aSLai Jiangshan * 26764370aa4aSLai Jiangshan * The return value is the number of characters which would 26774370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 26784370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 26794370aa4aSLai Jiangshan * number of characters written into @buf as return value 26804370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 26814370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 26824370aa4aSLai Jiangshan * string is truncated. 26834370aa4aSLai Jiangshan */ 26844370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 26854370aa4aSLai Jiangshan { 2686fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 2687d4be151bSAndré Goddard Rosa char *str, *end; 2688d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 26894370aa4aSLai Jiangshan 2690762abb51SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 26914370aa4aSLai Jiangshan return 0; 26924370aa4aSLai Jiangshan 26934370aa4aSLai Jiangshan str = buf; 26944370aa4aSLai Jiangshan end = buf + size; 26954370aa4aSLai Jiangshan 26964370aa4aSLai Jiangshan #define get_arg(type) \ 26974370aa4aSLai Jiangshan ({ \ 26984370aa4aSLai Jiangshan typeof(type) value; \ 26994370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 27004370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 27014370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 27024370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 27034370aa4aSLai Jiangshan } else { \ 27044370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 27054370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 27064370aa4aSLai Jiangshan } \ 27074370aa4aSLai Jiangshan args += sizeof(type); \ 27084370aa4aSLai Jiangshan value; \ 27094370aa4aSLai Jiangshan }) 27104370aa4aSLai Jiangshan 27114370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 27124370aa4aSLai Jiangshan if (end < buf) { 27134370aa4aSLai Jiangshan end = ((void *)-1); 27144370aa4aSLai Jiangshan size = end - buf; 27154370aa4aSLai Jiangshan } 27164370aa4aSLai Jiangshan 2717fef20d9cSFrederic Weisbecker while (*fmt) { 2718fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2719d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2720fef20d9cSFrederic Weisbecker 2721fef20d9cSFrederic Weisbecker fmt += read; 2722fef20d9cSFrederic Weisbecker 2723fef20d9cSFrederic Weisbecker switch (spec.type) { 2724fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2725fef20d9cSFrederic Weisbecker int copy = read; 2726fef20d9cSFrederic Weisbecker if (str < end) { 2727fef20d9cSFrederic Weisbecker if (copy > end - str) 2728fef20d9cSFrederic Weisbecker copy = end - str; 2729fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2730fef20d9cSFrederic Weisbecker } 2731fef20d9cSFrederic Weisbecker str += read; 2732fef20d9cSFrederic Weisbecker break; 27334370aa4aSLai Jiangshan } 27344370aa4aSLai Jiangshan 2735ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 27364d72ba01SRasmus Villemoes set_field_width(&spec, get_arg(int)); 2737fef20d9cSFrederic Weisbecker break; 27384370aa4aSLai Jiangshan 2739fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 27404d72ba01SRasmus Villemoes set_precision(&spec, get_arg(int)); 2741fef20d9cSFrederic Weisbecker break; 27424370aa4aSLai Jiangshan 2743d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2744d4be151bSAndré Goddard Rosa char c; 2745d4be151bSAndré Goddard Rosa 2746fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2747fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 27484370aa4aSLai Jiangshan if (str < end) 27494370aa4aSLai Jiangshan *str = ' '; 27504370aa4aSLai Jiangshan ++str; 27514370aa4aSLai Jiangshan } 27524370aa4aSLai Jiangshan } 27534370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 27544370aa4aSLai Jiangshan if (str < end) 27554370aa4aSLai Jiangshan *str = c; 27564370aa4aSLai Jiangshan ++str; 2757fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 27584370aa4aSLai Jiangshan if (str < end) 27594370aa4aSLai Jiangshan *str = ' '; 27604370aa4aSLai Jiangshan ++str; 27614370aa4aSLai Jiangshan } 2762fef20d9cSFrederic Weisbecker break; 2763d4be151bSAndré Goddard Rosa } 27644370aa4aSLai Jiangshan 2765fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 27664370aa4aSLai Jiangshan const char *str_arg = args; 2767d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 2768fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 2769fef20d9cSFrederic Weisbecker break; 27704370aa4aSLai Jiangshan } 27714370aa4aSLai Jiangshan 2772841a915dSSteven Rostedt (VMware) case FORMAT_TYPE_PTR: { 2773841a915dSSteven Rostedt (VMware) bool process = false; 2774841a915dSSteven Rostedt (VMware) int copy, len; 2775841a915dSSteven Rostedt (VMware) /* Non function dereferences were already done */ 2776841a915dSSteven Rostedt (VMware) switch (*fmt) { 2777841a915dSSteven Rostedt (VMware) case 'S': 2778841a915dSSteven Rostedt (VMware) case 's': 2779841a915dSSteven Rostedt (VMware) case 'F': 2780841a915dSSteven Rostedt (VMware) case 'f': 27811e6338cfSSteven Rostedt (VMware) case 'x': 27821e6338cfSSteven Rostedt (VMware) case 'K': 2783841a915dSSteven Rostedt (VMware) process = true; 2784841a915dSSteven Rostedt (VMware) break; 2785841a915dSSteven Rostedt (VMware) default: 2786841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 2787841a915dSSteven Rostedt (VMware) process = true; 2788841a915dSSteven Rostedt (VMware) break; 2789841a915dSSteven Rostedt (VMware) } 2790841a915dSSteven Rostedt (VMware) /* Pointer dereference was already processed */ 2791841a915dSSteven Rostedt (VMware) if (str < end) { 2792841a915dSSteven Rostedt (VMware) len = copy = strlen(args); 2793841a915dSSteven Rostedt (VMware) if (copy > end - str) 2794841a915dSSteven Rostedt (VMware) copy = end - str; 2795841a915dSSteven Rostedt (VMware) memcpy(str, args, copy); 2796841a915dSSteven Rostedt (VMware) str += len; 2797841a915dSSteven Rostedt (VMware) args += len; 2798841a915dSSteven Rostedt (VMware) } 2799841a915dSSteven Rostedt (VMware) } 2800841a915dSSteven Rostedt (VMware) if (process) 2801ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 2802841a915dSSteven Rostedt (VMware) 2803fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 28044370aa4aSLai Jiangshan fmt++; 2805fef20d9cSFrederic Weisbecker break; 2806841a915dSSteven Rostedt (VMware) } 28074370aa4aSLai Jiangshan 2808fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 28094370aa4aSLai Jiangshan if (str < end) 28104370aa4aSLai Jiangshan *str = '%'; 28114370aa4aSLai Jiangshan ++str; 2812fef20d9cSFrederic Weisbecker break; 2813fef20d9cSFrederic Weisbecker 2814b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2815b006f19bSRasmus Villemoes goto out; 2816b006f19bSRasmus Villemoes 2817d4be151bSAndré Goddard Rosa default: { 2818d4be151bSAndré Goddard Rosa unsigned long long num; 2819d4be151bSAndré Goddard Rosa 2820fef20d9cSFrederic Weisbecker switch (spec.type) { 2821fef20d9cSFrederic Weisbecker 2822fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 28234370aa4aSLai Jiangshan num = get_arg(long long); 2824fef20d9cSFrederic Weisbecker break; 2825fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2826fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2827fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 2828fef20d9cSFrederic Weisbecker break; 2829fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 28304370aa4aSLai Jiangshan num = get_arg(size_t); 2831fef20d9cSFrederic Weisbecker break; 2832fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 28334370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 2834fef20d9cSFrederic Weisbecker break; 2835a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2836a4e94ef0SZhaolei num = get_arg(unsigned char); 2837a4e94ef0SZhaolei break; 2838a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2839a4e94ef0SZhaolei num = get_arg(signed char); 2840a4e94ef0SZhaolei break; 2841fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2842fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 2843fef20d9cSFrederic Weisbecker break; 2844fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2845fef20d9cSFrederic Weisbecker num = get_arg(short); 2846fef20d9cSFrederic Weisbecker break; 2847fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 28484370aa4aSLai Jiangshan num = get_arg(unsigned int); 2849fef20d9cSFrederic Weisbecker break; 2850fef20d9cSFrederic Weisbecker default: 2851fef20d9cSFrederic Weisbecker num = get_arg(int); 28524370aa4aSLai Jiangshan } 2853fef20d9cSFrederic Weisbecker 2854fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 2855d4be151bSAndré Goddard Rosa } /* default: */ 2856d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 2857d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 2858fef20d9cSFrederic Weisbecker 2859b006f19bSRasmus Villemoes out: 28604370aa4aSLai Jiangshan if (size > 0) { 28614370aa4aSLai Jiangshan if (str < end) 28624370aa4aSLai Jiangshan *str = '\0'; 28634370aa4aSLai Jiangshan else 28644370aa4aSLai Jiangshan end[-1] = '\0'; 28654370aa4aSLai Jiangshan } 2866fef20d9cSFrederic Weisbecker 28674370aa4aSLai Jiangshan #undef get_arg 28684370aa4aSLai Jiangshan 28694370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 28704370aa4aSLai Jiangshan return str - buf; 28714370aa4aSLai Jiangshan } 28724370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 28734370aa4aSLai Jiangshan 28744370aa4aSLai Jiangshan /** 28754370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 28764370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 28774370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 28784370aa4aSLai Jiangshan * @fmt: The format string to use 28794370aa4aSLai Jiangshan * @...: Arguments for the format string 28804370aa4aSLai Jiangshan * 28814370aa4aSLai Jiangshan * The function returns the number of words(u32) written 28824370aa4aSLai Jiangshan * into @bin_buf. 28834370aa4aSLai Jiangshan */ 28844370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 28854370aa4aSLai Jiangshan { 28864370aa4aSLai Jiangshan va_list args; 28874370aa4aSLai Jiangshan int ret; 28884370aa4aSLai Jiangshan 28894370aa4aSLai Jiangshan va_start(args, fmt); 28904370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 28914370aa4aSLai Jiangshan va_end(args); 28927b9186f5SAndré Goddard Rosa 28934370aa4aSLai Jiangshan return ret; 28944370aa4aSLai Jiangshan } 28954370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 28964370aa4aSLai Jiangshan 28974370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 28984370aa4aSLai Jiangshan 28991da177e4SLinus Torvalds /** 29001da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 29011da177e4SLinus Torvalds * @buf: input buffer 29021da177e4SLinus Torvalds * @fmt: format of buffer 29031da177e4SLinus Torvalds * @args: arguments 29041da177e4SLinus Torvalds */ 29051da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 29061da177e4SLinus Torvalds { 29071da177e4SLinus Torvalds const char *str = buf; 29081da177e4SLinus Torvalds char *next; 29091da177e4SLinus Torvalds char digit; 29101da177e4SLinus Torvalds int num = 0; 2911ef0658f3SJoe Perches u8 qualifier; 291253809751SJan Beulich unsigned int base; 291353809751SJan Beulich union { 291453809751SJan Beulich long long s; 291553809751SJan Beulich unsigned long long u; 291653809751SJan Beulich } val; 2917ef0658f3SJoe Perches s16 field_width; 2918d4be151bSAndré Goddard Rosa bool is_sign; 29191da177e4SLinus Torvalds 2920da99075cSJan Beulich while (*fmt) { 29211da177e4SLinus Torvalds /* skip any white space in format */ 29221da177e4SLinus Torvalds /* white space in format matchs any amount of 29231da177e4SLinus Torvalds * white space, including none, in the input. 29241da177e4SLinus Torvalds */ 29251da177e4SLinus Torvalds if (isspace(*fmt)) { 2926e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 2927e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 29281da177e4SLinus Torvalds } 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 29311da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 29321da177e4SLinus Torvalds if (*fmt++ != *str++) 29331da177e4SLinus Torvalds break; 29341da177e4SLinus Torvalds continue; 29351da177e4SLinus Torvalds } 29361da177e4SLinus Torvalds 29371da177e4SLinus Torvalds if (!*fmt) 29381da177e4SLinus Torvalds break; 29391da177e4SLinus Torvalds ++fmt; 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds /* skip this conversion. 29421da177e4SLinus Torvalds * advance both strings to next white space 29431da177e4SLinus Torvalds */ 29441da177e4SLinus Torvalds if (*fmt == '*') { 2945da99075cSJan Beulich if (!*str) 2946da99075cSJan Beulich break; 2947f9310b2fSJessica Yu while (!isspace(*fmt) && *fmt != '%' && *fmt) { 2948f9310b2fSJessica Yu /* '%*[' not yet supported, invalid format */ 2949f9310b2fSJessica Yu if (*fmt == '[') 2950f9310b2fSJessica Yu return num; 29511da177e4SLinus Torvalds fmt++; 2952f9310b2fSJessica Yu } 29531da177e4SLinus Torvalds while (!isspace(*str) && *str) 29541da177e4SLinus Torvalds str++; 29551da177e4SLinus Torvalds continue; 29561da177e4SLinus Torvalds } 29571da177e4SLinus Torvalds 29581da177e4SLinus Torvalds /* get field width */ 29591da177e4SLinus Torvalds field_width = -1; 296053809751SJan Beulich if (isdigit(*fmt)) { 29611da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 296253809751SJan Beulich if (field_width <= 0) 296353809751SJan Beulich break; 296453809751SJan Beulich } 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds /* get conversion qualifier */ 29671da177e4SLinus Torvalds qualifier = -1; 296875fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 29695b5e0928SAlexey Dobriyan *fmt == 'z') { 29701da177e4SLinus Torvalds qualifier = *fmt++; 29711da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 29721da177e4SLinus Torvalds if (qualifier == 'h') { 29731da177e4SLinus Torvalds qualifier = 'H'; 29741da177e4SLinus Torvalds fmt++; 29751da177e4SLinus Torvalds } else if (qualifier == 'l') { 29761da177e4SLinus Torvalds qualifier = 'L'; 29771da177e4SLinus Torvalds fmt++; 29781da177e4SLinus Torvalds } 29791da177e4SLinus Torvalds } 29801da177e4SLinus Torvalds } 29811da177e4SLinus Torvalds 2982da99075cSJan Beulich if (!*fmt) 2983da99075cSJan Beulich break; 2984da99075cSJan Beulich 2985da99075cSJan Beulich if (*fmt == 'n') { 2986da99075cSJan Beulich /* return number of characters read so far */ 2987da99075cSJan Beulich *va_arg(args, int *) = str - buf; 2988da99075cSJan Beulich ++fmt; 2989da99075cSJan Beulich continue; 2990da99075cSJan Beulich } 2991da99075cSJan Beulich 2992da99075cSJan Beulich if (!*str) 29931da177e4SLinus Torvalds break; 29941da177e4SLinus Torvalds 2995d4be151bSAndré Goddard Rosa base = 10; 29963f623ebaSFabian Frederick is_sign = false; 2997d4be151bSAndré Goddard Rosa 29981da177e4SLinus Torvalds switch (*fmt++) { 29991da177e4SLinus Torvalds case 'c': 30001da177e4SLinus Torvalds { 30011da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 30021da177e4SLinus Torvalds if (field_width == -1) 30031da177e4SLinus Torvalds field_width = 1; 30041da177e4SLinus Torvalds do { 30051da177e4SLinus Torvalds *s++ = *str++; 30061da177e4SLinus Torvalds } while (--field_width > 0 && *str); 30071da177e4SLinus Torvalds num++; 30081da177e4SLinus Torvalds } 30091da177e4SLinus Torvalds continue; 30101da177e4SLinus Torvalds case 's': 30111da177e4SLinus Torvalds { 30121da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 30131da177e4SLinus Torvalds if (field_width == -1) 30144be929beSAlexey Dobriyan field_width = SHRT_MAX; 30151da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 3016e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 30171da177e4SLinus Torvalds 30181da177e4SLinus Torvalds /* now copy until next white space */ 30197b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 30201da177e4SLinus Torvalds *s++ = *str++; 30211da177e4SLinus Torvalds *s = '\0'; 30221da177e4SLinus Torvalds num++; 30231da177e4SLinus Torvalds } 30241da177e4SLinus Torvalds continue; 3025f9310b2fSJessica Yu /* 3026f9310b2fSJessica Yu * Warning: This implementation of the '[' conversion specifier 3027f9310b2fSJessica Yu * deviates from its glibc counterpart in the following ways: 3028f9310b2fSJessica Yu * (1) It does NOT support ranges i.e. '-' is NOT a special 3029f9310b2fSJessica Yu * character 3030f9310b2fSJessica Yu * (2) It cannot match the closing bracket ']' itself 3031f9310b2fSJessica Yu * (3) A field width is required 3032f9310b2fSJessica Yu * (4) '%*[' (discard matching input) is currently not supported 3033f9310b2fSJessica Yu * 3034f9310b2fSJessica Yu * Example usage: 3035f9310b2fSJessica Yu * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3036f9310b2fSJessica Yu * buf1, buf2, buf3); 3037f9310b2fSJessica Yu * if (ret < 3) 3038f9310b2fSJessica Yu * // etc.. 3039f9310b2fSJessica Yu */ 3040f9310b2fSJessica Yu case '[': 3041f9310b2fSJessica Yu { 3042f9310b2fSJessica Yu char *s = (char *)va_arg(args, char *); 3043f9310b2fSJessica Yu DECLARE_BITMAP(set, 256) = {0}; 3044f9310b2fSJessica Yu unsigned int len = 0; 3045f9310b2fSJessica Yu bool negate = (*fmt == '^'); 3046f9310b2fSJessica Yu 3047f9310b2fSJessica Yu /* field width is required */ 3048f9310b2fSJessica Yu if (field_width == -1) 3049f9310b2fSJessica Yu return num; 3050f9310b2fSJessica Yu 3051f9310b2fSJessica Yu if (negate) 3052f9310b2fSJessica Yu ++fmt; 3053f9310b2fSJessica Yu 3054f9310b2fSJessica Yu for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 3055f9310b2fSJessica Yu set_bit((u8)*fmt, set); 3056f9310b2fSJessica Yu 3057f9310b2fSJessica Yu /* no ']' or no character set found */ 3058f9310b2fSJessica Yu if (!*fmt || !len) 3059f9310b2fSJessica Yu return num; 3060f9310b2fSJessica Yu ++fmt; 3061f9310b2fSJessica Yu 3062f9310b2fSJessica Yu if (negate) { 3063f9310b2fSJessica Yu bitmap_complement(set, set, 256); 3064f9310b2fSJessica Yu /* exclude null '\0' byte */ 3065f9310b2fSJessica Yu clear_bit(0, set); 3066f9310b2fSJessica Yu } 3067f9310b2fSJessica Yu 3068f9310b2fSJessica Yu /* match must be non-empty */ 3069f9310b2fSJessica Yu if (!test_bit((u8)*str, set)) 3070f9310b2fSJessica Yu return num; 3071f9310b2fSJessica Yu 3072f9310b2fSJessica Yu while (test_bit((u8)*str, set) && field_width--) 3073f9310b2fSJessica Yu *s++ = *str++; 3074f9310b2fSJessica Yu *s = '\0'; 3075f9310b2fSJessica Yu ++num; 3076f9310b2fSJessica Yu } 3077f9310b2fSJessica Yu continue; 30781da177e4SLinus Torvalds case 'o': 30791da177e4SLinus Torvalds base = 8; 30801da177e4SLinus Torvalds break; 30811da177e4SLinus Torvalds case 'x': 30821da177e4SLinus Torvalds case 'X': 30831da177e4SLinus Torvalds base = 16; 30841da177e4SLinus Torvalds break; 30851da177e4SLinus Torvalds case 'i': 30861da177e4SLinus Torvalds base = 0; 30877e6bd6f3SAndy Shevchenko /* fall through */ 30881da177e4SLinus Torvalds case 'd': 30893f623ebaSFabian Frederick is_sign = true; 30907e6bd6f3SAndy Shevchenko /* fall through */ 30911da177e4SLinus Torvalds case 'u': 30921da177e4SLinus Torvalds break; 30931da177e4SLinus Torvalds case '%': 30941da177e4SLinus Torvalds /* looking for '%' in str */ 30951da177e4SLinus Torvalds if (*str++ != '%') 30961da177e4SLinus Torvalds return num; 30971da177e4SLinus Torvalds continue; 30981da177e4SLinus Torvalds default: 30991da177e4SLinus Torvalds /* invalid format; stop here */ 31001da177e4SLinus Torvalds return num; 31011da177e4SLinus Torvalds } 31021da177e4SLinus Torvalds 31031da177e4SLinus Torvalds /* have some sort of integer conversion. 31041da177e4SLinus Torvalds * first, skip white space in buffer. 31051da177e4SLinus Torvalds */ 3106e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 31071da177e4SLinus Torvalds 31081da177e4SLinus Torvalds digit = *str; 31091da177e4SLinus Torvalds if (is_sign && digit == '-') 31101da177e4SLinus Torvalds digit = *(str + 1); 31111da177e4SLinus Torvalds 31121da177e4SLinus Torvalds if (!digit 31131da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 31141da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 31151da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 31161da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 31171da177e4SLinus Torvalds break; 31181da177e4SLinus Torvalds 311953809751SJan Beulich if (is_sign) 312053809751SJan Beulich val.s = qualifier != 'L' ? 312153809751SJan Beulich simple_strtol(str, &next, base) : 312253809751SJan Beulich simple_strtoll(str, &next, base); 312353809751SJan Beulich else 312453809751SJan Beulich val.u = qualifier != 'L' ? 312553809751SJan Beulich simple_strtoul(str, &next, base) : 312653809751SJan Beulich simple_strtoull(str, &next, base); 312753809751SJan Beulich 312853809751SJan Beulich if (field_width > 0 && next - str > field_width) { 312953809751SJan Beulich if (base == 0) 313053809751SJan Beulich _parse_integer_fixup_radix(str, &base); 313153809751SJan Beulich while (next - str > field_width) { 313253809751SJan Beulich if (is_sign) 313353809751SJan Beulich val.s = div_s64(val.s, base); 313453809751SJan Beulich else 313553809751SJan Beulich val.u = div_u64(val.u, base); 313653809751SJan Beulich --next; 313753809751SJan Beulich } 313853809751SJan Beulich } 313953809751SJan Beulich 31401da177e4SLinus Torvalds switch (qualifier) { 31411da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 314253809751SJan Beulich if (is_sign) 314353809751SJan Beulich *va_arg(args, signed char *) = val.s; 314453809751SJan Beulich else 314553809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 31461da177e4SLinus Torvalds break; 31471da177e4SLinus Torvalds case 'h': 314853809751SJan Beulich if (is_sign) 314953809751SJan Beulich *va_arg(args, short *) = val.s; 315053809751SJan Beulich else 315153809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 31521da177e4SLinus Torvalds break; 31531da177e4SLinus Torvalds case 'l': 315453809751SJan Beulich if (is_sign) 315553809751SJan Beulich *va_arg(args, long *) = val.s; 315653809751SJan Beulich else 315753809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 31581da177e4SLinus Torvalds break; 31591da177e4SLinus Torvalds case 'L': 316053809751SJan Beulich if (is_sign) 316153809751SJan Beulich *va_arg(args, long long *) = val.s; 316253809751SJan Beulich else 316353809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 31641da177e4SLinus Torvalds break; 31651da177e4SLinus Torvalds case 'z': 316653809751SJan Beulich *va_arg(args, size_t *) = val.u; 31671da177e4SLinus Torvalds break; 31681da177e4SLinus Torvalds default: 316953809751SJan Beulich if (is_sign) 317053809751SJan Beulich *va_arg(args, int *) = val.s; 317153809751SJan Beulich else 317253809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 31731da177e4SLinus Torvalds break; 31741da177e4SLinus Torvalds } 31751da177e4SLinus Torvalds num++; 31761da177e4SLinus Torvalds 31771da177e4SLinus Torvalds if (!next) 31781da177e4SLinus Torvalds break; 31791da177e4SLinus Torvalds str = next; 31801da177e4SLinus Torvalds } 3181c6b40d16SJohannes Berg 31821da177e4SLinus Torvalds return num; 31831da177e4SLinus Torvalds } 31841da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 31851da177e4SLinus Torvalds 31861da177e4SLinus Torvalds /** 31871da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 31881da177e4SLinus Torvalds * @buf: input buffer 31891da177e4SLinus Torvalds * @fmt: formatting of buffer 31901da177e4SLinus Torvalds * @...: resulting arguments 31911da177e4SLinus Torvalds */ 31921da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 31931da177e4SLinus Torvalds { 31941da177e4SLinus Torvalds va_list args; 31951da177e4SLinus Torvalds int i; 31961da177e4SLinus Torvalds 31971da177e4SLinus Torvalds va_start(args, fmt); 31981da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 31991da177e4SLinus Torvalds va_end(args); 32007b9186f5SAndré Goddard Rosa 32011da177e4SLinus Torvalds return i; 32021da177e4SLinus Torvalds } 32031da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 3204