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> 348a27f7c9SJoe Perches #include <net/addrconf.h> 351031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 361031bc58SDmitry Monakhov #include <linux/blkdev.h> 371031bc58SDmitry Monakhov #endif 381da177e4SLinus Torvalds 39edf14cdbSVlastimil Babka #include "../mm/internal.h" /* For the trace_print_flags arrays */ 40edf14cdbSVlastimil Babka 414e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 42deac93dfSJames Bottomley #include <asm/sections.h> /* for dereference_function_descriptor() */ 437c43d9a3SRasmus Villemoes #include <asm/byteorder.h> /* cpu_to_le16 */ 441da177e4SLinus Torvalds 4571dca95dSAndy Shevchenko #include <linux/string_helpers.h> 461dff46d6SAlexey Dobriyan #include "kstrtox.h" 47aa46a63eSHarvey Harrison 481da177e4SLinus Torvalds /** 491da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 501da177e4SLinus Torvalds * @cp: The start of the string 511da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 521da177e4SLinus Torvalds * @base: The number base to use 53462e4711SEldad Zack * 54462e4711SEldad Zack * This function is obsolete. Please use kstrtoull instead. 551da177e4SLinus Torvalds */ 561da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 571da177e4SLinus Torvalds { 581dff46d6SAlexey Dobriyan unsigned long long result; 591dff46d6SAlexey Dobriyan unsigned int rv; 601da177e4SLinus Torvalds 611dff46d6SAlexey Dobriyan cp = _parse_integer_fixup_radix(cp, &base); 621dff46d6SAlexey Dobriyan rv = _parse_integer(cp, base, &result); 631dff46d6SAlexey Dobriyan /* FIXME */ 641dff46d6SAlexey Dobriyan cp += (rv & ~KSTRTOX_OVERFLOW); 65aa46a63eSHarvey Harrison 661da177e4SLinus Torvalds if (endp) 671da177e4SLinus Torvalds *endp = (char *)cp; 687b9186f5SAndré Goddard Rosa 691da177e4SLinus Torvalds return result; 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /** 74922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 75922ac25cSAndré Goddard Rosa * @cp: The start of the string 76922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 77922ac25cSAndré Goddard Rosa * @base: The number base to use 78462e4711SEldad Zack * 79462e4711SEldad Zack * This function is obsolete. Please use kstrtoul instead. 80922ac25cSAndré Goddard Rosa */ 81922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 82922ac25cSAndré Goddard Rosa { 83922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 84922ac25cSAndré Goddard Rosa } 85922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 86922ac25cSAndré Goddard Rosa 87922ac25cSAndré Goddard Rosa /** 88922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 89922ac25cSAndré Goddard Rosa * @cp: The start of the string 90922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 91922ac25cSAndré Goddard Rosa * @base: The number base to use 92462e4711SEldad Zack * 93462e4711SEldad Zack * This function is obsolete. Please use kstrtol instead. 94922ac25cSAndré Goddard Rosa */ 95922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 96922ac25cSAndré Goddard Rosa { 97922ac25cSAndré Goddard Rosa if (*cp == '-') 98922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 99922ac25cSAndré Goddard Rosa 100922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 101922ac25cSAndré Goddard Rosa } 102922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 103922ac25cSAndré Goddard Rosa 104922ac25cSAndré Goddard Rosa /** 1051da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1061da177e4SLinus Torvalds * @cp: The start of the string 1071da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1081da177e4SLinus Torvalds * @base: The number base to use 109462e4711SEldad Zack * 110462e4711SEldad Zack * This function is obsolete. Please use kstrtoll instead. 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds if (*cp == '-') 1151da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1167b9186f5SAndré Goddard Rosa 1171da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1181da177e4SLinus Torvalds } 11998d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1201da177e4SLinus Torvalds 121cf3b429bSJoe Perches static noinline_for_stack 122cf3b429bSJoe Perches int skip_atoi(const char **s) 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds int i = 0; 1251da177e4SLinus Torvalds 12643e5b666SRasmus Villemoes do { 1271da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 12843e5b666SRasmus Villemoes } while (isdigit(**s)); 1297b9186f5SAndré Goddard Rosa 1301da177e4SLinus Torvalds return i; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 1337c43d9a3SRasmus Villemoes /* 1347c43d9a3SRasmus Villemoes * Decimal conversion is by far the most typical, and is used for 1357c43d9a3SRasmus Villemoes * /proc and /sys data. This directly impacts e.g. top performance 1367c43d9a3SRasmus Villemoes * with many processes running. We optimize it for speed by emitting 1377c43d9a3SRasmus Villemoes * two characters at a time, using a 200 byte lookup table. This 1387c43d9a3SRasmus Villemoes * roughly halves the number of multiplications compared to computing 1397c43d9a3SRasmus Villemoes * the digits one at a time. Implementation strongly inspired by the 1407c43d9a3SRasmus Villemoes * previous version, which in turn used ideas described at 1417c43d9a3SRasmus Villemoes * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 1427c43d9a3SRasmus Villemoes * from the author, Douglas W. Jones). 1437c43d9a3SRasmus Villemoes * 1447c43d9a3SRasmus Villemoes * It turns out there is precisely one 26 bit fixed-point 1457c43d9a3SRasmus Villemoes * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 1467c43d9a3SRasmus Villemoes * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 1477c43d9a3SRasmus Villemoes * range happens to be somewhat larger (x <= 1073741898), but that's 1487c43d9a3SRasmus Villemoes * irrelevant for our purpose. 1497c43d9a3SRasmus Villemoes * 1507c43d9a3SRasmus Villemoes * For dividing a number in the range [10^4, 10^6-1] by 100, we still 1517c43d9a3SRasmus Villemoes * need a 32x32->64 bit multiply, so we simply use the same constant. 1527c43d9a3SRasmus Villemoes * 1537c43d9a3SRasmus Villemoes * For dividing a number in the range [100, 10^4-1] by 100, there are 1547c43d9a3SRasmus Villemoes * several options. The simplest is (x * 0x147b) >> 19, which is valid 1557c43d9a3SRasmus Villemoes * for all x <= 43698. 156133fd9f5SDenys Vlasenko */ 1574277eeddSDenis Vlasenko 1587c43d9a3SRasmus Villemoes static const u16 decpair[100] = { 1597c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 1607c43d9a3SRasmus Villemoes _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 1617c43d9a3SRasmus Villemoes _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 1627c43d9a3SRasmus Villemoes _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 1637c43d9a3SRasmus Villemoes _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 1647c43d9a3SRasmus Villemoes _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 1657c43d9a3SRasmus Villemoes _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 1667c43d9a3SRasmus Villemoes _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 1677c43d9a3SRasmus Villemoes _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 1687c43d9a3SRasmus Villemoes _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 1697c43d9a3SRasmus Villemoes _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 1707c43d9a3SRasmus Villemoes #undef _ 1717c43d9a3SRasmus Villemoes }; 1724277eeddSDenis Vlasenko 1737b9186f5SAndré Goddard Rosa /* 1747c43d9a3SRasmus Villemoes * This will print a single '0' even if r == 0, since we would 175675cf53cSRasmus Villemoes * immediately jump to out_r where two 0s would be written but only 176675cf53cSRasmus Villemoes * one of them accounted for in buf. This is needed by ip4_string 177675cf53cSRasmus Villemoes * below. All other callers pass a non-zero value of r. 178133fd9f5SDenys Vlasenko */ 179133fd9f5SDenys Vlasenko static noinline_for_stack 180133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 181133fd9f5SDenys Vlasenko { 182133fd9f5SDenys Vlasenko unsigned q; 1834277eeddSDenis Vlasenko 1847c43d9a3SRasmus Villemoes /* 1 <= r < 10^8 */ 1857c43d9a3SRasmus Villemoes if (r < 100) 1867c43d9a3SRasmus Villemoes goto out_r; 187cb239d0aSGeorge Spelvin 1887c43d9a3SRasmus Villemoes /* 100 <= r < 10^8 */ 1897c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 1907c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 1917c43d9a3SRasmus Villemoes buf += 2; 1927c43d9a3SRasmus Villemoes 1937c43d9a3SRasmus Villemoes /* 1 <= q < 10^6 */ 1947c43d9a3SRasmus Villemoes if (q < 100) 1957c43d9a3SRasmus Villemoes goto out_q; 1967c43d9a3SRasmus Villemoes 1977c43d9a3SRasmus Villemoes /* 100 <= q < 10^6 */ 1987c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 1997c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2007c43d9a3SRasmus Villemoes buf += 2; 2017c43d9a3SRasmus Villemoes 2027c43d9a3SRasmus Villemoes /* 1 <= r < 10^4 */ 2037c43d9a3SRasmus Villemoes if (r < 100) 2047c43d9a3SRasmus Villemoes goto out_r; 2057c43d9a3SRasmus Villemoes 2067c43d9a3SRasmus Villemoes /* 100 <= r < 10^4 */ 2077c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2087c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2097c43d9a3SRasmus Villemoes buf += 2; 2107c43d9a3SRasmus Villemoes out_q: 2117c43d9a3SRasmus Villemoes /* 1 <= q < 100 */ 2127c43d9a3SRasmus Villemoes r = q; 2137c43d9a3SRasmus Villemoes out_r: 2147c43d9a3SRasmus Villemoes /* 1 <= r < 100 */ 2157c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r]; 216675cf53cSRasmus Villemoes buf += r < 10 ? 1 : 2; 217133fd9f5SDenys Vlasenko return buf; 218133fd9f5SDenys Vlasenko } 219133fd9f5SDenys Vlasenko 2207c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 2217c43d9a3SRasmus Villemoes static noinline_for_stack 2227c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r) 2237c43d9a3SRasmus Villemoes { 2247c43d9a3SRasmus Villemoes unsigned q; 225133fd9f5SDenys Vlasenko 2267c43d9a3SRasmus Villemoes /* 0 <= r < 10^8 */ 2277c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2287c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2297c43d9a3SRasmus Villemoes buf += 2; 230133fd9f5SDenys Vlasenko 2317c43d9a3SRasmus Villemoes /* 0 <= q < 10^6 */ 2327c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2337c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2347c43d9a3SRasmus Villemoes buf += 2; 235133fd9f5SDenys Vlasenko 2367c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2377c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2387c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2397c43d9a3SRasmus Villemoes buf += 2; 2407c43d9a3SRasmus Villemoes 2417c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2427c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2437c43d9a3SRasmus Villemoes buf += 2; 2447c43d9a3SRasmus Villemoes return buf; 2457c43d9a3SRasmus Villemoes } 2467c43d9a3SRasmus Villemoes 2477c43d9a3SRasmus Villemoes static noinline_for_stack 248133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 249133fd9f5SDenys Vlasenko { 250133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 2517c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2527c43d9a3SRasmus Villemoes /* 1 <= n <= 1.6e11 */ 2537c43d9a3SRasmus Villemoes if (n >= 100*1000*1000) 2547c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2557c43d9a3SRasmus Villemoes /* 1 <= n < 1e8 */ 256133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 257133fd9f5SDenys Vlasenko } 258133fd9f5SDenys Vlasenko 2597c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 260133fd9f5SDenys Vlasenko 2617c43d9a3SRasmus Villemoes static void 2627c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r) 263133fd9f5SDenys Vlasenko { 2647c43d9a3SRasmus Villemoes unsigned q; 2657c43d9a3SRasmus Villemoes 2667c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2677c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2687c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2697c43d9a3SRasmus Villemoes buf += 2; 2707c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2717c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2722359172aSGeorge Spelvin } 2732359172aSGeorge Spelvin 2742359172aSGeorge Spelvin /* 2752359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 2762359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 2772359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 2782359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 2797c43d9a3SRasmus Villemoes * (second call in the put_dec code, assuming n is all-ones). 2802359172aSGeorge Spelvin */ 2817c43d9a3SRasmus Villemoes static noinline_for_stack 2822359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 2832359172aSGeorge Spelvin { 2842359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 2852359172aSGeorge Spelvin 2862359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 2872359172aSGeorge Spelvin return q; 288133fd9f5SDenys Vlasenko } 289133fd9f5SDenys Vlasenko 290133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 291133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 292133fd9f5SDenys Vlasenko * (with permission from the author). 293133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 294133fd9f5SDenys Vlasenko */ 295133fd9f5SDenys Vlasenko static 296133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 297133fd9f5SDenys Vlasenko { 298133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 299133fd9f5SDenys Vlasenko 300133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 301133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 302133fd9f5SDenys Vlasenko 303133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 304133fd9f5SDenys Vlasenko h = (n >> 32); 305133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 306133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 307133fd9f5SDenys Vlasenko 3087c43d9a3SRasmus Villemoes /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 3097c43d9a3SRasmus Villemoes = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 310133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 3112359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 312133fd9f5SDenys Vlasenko 3132359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 3142359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 315133fd9f5SDenys Vlasenko 3162359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3172359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 318133fd9f5SDenys Vlasenko 3192359172aSGeorge Spelvin q += 281 * d3; 3202359172aSGeorge Spelvin buf += 12; 3212359172aSGeorge Spelvin if (q) 3222359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3232359172aSGeorge Spelvin else while (buf[-1] == '0') 324133fd9f5SDenys Vlasenko --buf; 3257b9186f5SAndré Goddard Rosa 3264277eeddSDenis Vlasenko return buf; 3274277eeddSDenis Vlasenko } 328133fd9f5SDenys Vlasenko 329133fd9f5SDenys Vlasenko #endif 3304277eeddSDenis Vlasenko 3311ac101a5SKAMEZAWA Hiroyuki /* 3321ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3331ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3341ac101a5SKAMEZAWA Hiroyuki * 3351ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3361ac101a5SKAMEZAWA Hiroyuki */ 3371ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num) 3381ac101a5SKAMEZAWA Hiroyuki { 3397c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 3407c43d9a3SRasmus Villemoes char tmp[sizeof(num) * 3] __aligned(2); 3411ac101a5SKAMEZAWA Hiroyuki int idx, len; 3421ac101a5SKAMEZAWA Hiroyuki 343133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 344133fd9f5SDenys Vlasenko if (num <= 9) { 345133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 346133fd9f5SDenys Vlasenko len = 1; 347133fd9f5SDenys Vlasenko } else { 3481ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 349133fd9f5SDenys Vlasenko } 3501ac101a5SKAMEZAWA Hiroyuki 3511ac101a5SKAMEZAWA Hiroyuki if (len > size) 3521ac101a5SKAMEZAWA Hiroyuki return 0; 3531ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 3541ac101a5SKAMEZAWA Hiroyuki buf[idx] = tmp[len - idx - 1]; 3551ac101a5SKAMEZAWA Hiroyuki return len; 3561ac101a5SKAMEZAWA Hiroyuki } 3571ac101a5SKAMEZAWA Hiroyuki 35851be17dfSRasmus Villemoes #define SIGN 1 /* unsigned/signed, must be 1 */ 359d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */ 3601da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3611da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 362d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 363b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 364b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 3651da177e4SLinus Torvalds 366fef20d9cSFrederic Weisbecker enum format_type { 367fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 368ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 369fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 370fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 371fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 372fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 373fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 374fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 375fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 376fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 377fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 378a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 379a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 380fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 381fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 382fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 383fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 384fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 385fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 386fef20d9cSFrederic Weisbecker }; 387fef20d9cSFrederic Weisbecker 388fef20d9cSFrederic Weisbecker struct printf_spec { 389d0484193SRasmus Villemoes unsigned int type:8; /* format_type enum */ 390d0484193SRasmus Villemoes signed int field_width:24; /* width of output field */ 391d0484193SRasmus Villemoes unsigned int flags:8; /* flags to number() */ 392d0484193SRasmus Villemoes unsigned int base:8; /* number base, 8, 10 or 16 only */ 393d0484193SRasmus Villemoes signed int precision:16; /* # of digits/chars */ 394d0484193SRasmus Villemoes } __packed; 3954d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1) 3964d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1) 397fef20d9cSFrederic Weisbecker 398cf3b429bSJoe Perches static noinline_for_stack 399cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 400fef20d9cSFrederic Weisbecker struct printf_spec spec) 4011da177e4SLinus Torvalds { 4027c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 4037c43d9a3SRasmus Villemoes char tmp[3 * sizeof(num)] __aligned(2); 4049b706aeeSDenys Vlasenko char sign; 4059b706aeeSDenys Vlasenko char locase; 406fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4071da177e4SLinus Torvalds int i; 4087c203422SPierre Carrier bool is_zero = num == 0LL; 4091c7a8e62SRasmus Villemoes int field_width = spec.field_width; 4101c7a8e62SRasmus Villemoes int precision = spec.precision; 4111da177e4SLinus Torvalds 412d0484193SRasmus Villemoes BUILD_BUG_ON(sizeof(struct printf_spec) != 8); 413d0484193SRasmus Villemoes 4149b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4159b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 416fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 417fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 418fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4191da177e4SLinus Torvalds sign = 0; 420fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4211da177e4SLinus Torvalds if ((signed long long)num < 0) { 4221da177e4SLinus Torvalds sign = '-'; 4231da177e4SLinus Torvalds num = -(signed long long)num; 4241c7a8e62SRasmus Villemoes field_width--; 425fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4261da177e4SLinus Torvalds sign = '+'; 4271c7a8e62SRasmus Villemoes field_width--; 428fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4291da177e4SLinus Torvalds sign = ' '; 4301c7a8e62SRasmus Villemoes field_width--; 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds } 433b39a7340SDenis Vlasenko if (need_pfx) { 434fef20d9cSFrederic Weisbecker if (spec.base == 16) 4351c7a8e62SRasmus Villemoes field_width -= 2; 4367c203422SPierre Carrier else if (!is_zero) 4371c7a8e62SRasmus Villemoes field_width--; 4381da177e4SLinus Torvalds } 439b39a7340SDenis Vlasenko 440b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4411da177e4SLinus Torvalds i = 0; 442133fd9f5SDenys Vlasenko if (num < spec.base) 4433ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase; 444fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 445fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 446b39a7340SDenis Vlasenko int shift = 3; 4477b9186f5SAndré Goddard Rosa 4487b9186f5SAndré Goddard Rosa if (spec.base == 16) 4497b9186f5SAndré Goddard Rosa shift = 4; 450b39a7340SDenis Vlasenko do { 4513ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 452b39a7340SDenis Vlasenko num >>= shift; 453b39a7340SDenis Vlasenko } while (num); 4544277eeddSDenis Vlasenko } else { /* base 10 */ 4554277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4564277eeddSDenis Vlasenko } 457b39a7340SDenis Vlasenko 458b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 4591c7a8e62SRasmus Villemoes if (i > precision) 4601c7a8e62SRasmus Villemoes precision = i; 461b39a7340SDenis Vlasenko /* leading space padding */ 4621c7a8e62SRasmus Villemoes field_width -= precision; 46351be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) { 4641c7a8e62SRasmus Villemoes while (--field_width >= 0) { 465f796937aSJeremy Fitzhardinge if (buf < end) 4661da177e4SLinus Torvalds *buf = ' '; 4671da177e4SLinus Torvalds ++buf; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds } 470b39a7340SDenis Vlasenko /* sign */ 4711da177e4SLinus Torvalds if (sign) { 472f796937aSJeremy Fitzhardinge if (buf < end) 4731da177e4SLinus Torvalds *buf = sign; 4741da177e4SLinus Torvalds ++buf; 4751da177e4SLinus Torvalds } 476b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 477b39a7340SDenis Vlasenko if (need_pfx) { 4787c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 479f796937aSJeremy Fitzhardinge if (buf < end) 4801da177e4SLinus Torvalds *buf = '0'; 4811da177e4SLinus Torvalds ++buf; 4827c203422SPierre Carrier } 483fef20d9cSFrederic Weisbecker if (spec.base == 16) { 484f796937aSJeremy Fitzhardinge if (buf < end) 4859b706aeeSDenys Vlasenko *buf = ('X' | locase); 4861da177e4SLinus Torvalds ++buf; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds } 489b39a7340SDenis Vlasenko /* zero or space padding */ 490fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 491d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD); 492d1c1b121SRasmus Villemoes BUILD_BUG_ON(' ' + ZEROPAD != '0'); 4931c7a8e62SRasmus Villemoes while (--field_width >= 0) { 494f796937aSJeremy Fitzhardinge if (buf < end) 4951da177e4SLinus Torvalds *buf = c; 4961da177e4SLinus Torvalds ++buf; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds } 499b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 5001c7a8e62SRasmus Villemoes while (i <= --precision) { 501f796937aSJeremy Fitzhardinge if (buf < end) 5021da177e4SLinus Torvalds *buf = '0'; 5031da177e4SLinus Torvalds ++buf; 5041da177e4SLinus Torvalds } 505b39a7340SDenis Vlasenko /* actual digits of result */ 506b39a7340SDenis Vlasenko while (--i >= 0) { 507f796937aSJeremy Fitzhardinge if (buf < end) 5081da177e4SLinus Torvalds *buf = tmp[i]; 5091da177e4SLinus Torvalds ++buf; 5101da177e4SLinus Torvalds } 511b39a7340SDenis Vlasenko /* trailing space padding */ 5121c7a8e62SRasmus Villemoes while (--field_width >= 0) { 513f796937aSJeremy Fitzhardinge if (buf < end) 5141da177e4SLinus Torvalds *buf = ' '; 5151da177e4SLinus Torvalds ++buf; 5161da177e4SLinus Torvalds } 5177b9186f5SAndré Goddard Rosa 5181da177e4SLinus Torvalds return buf; 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds 5213cab1e71SAndy Shevchenko static noinline_for_stack 5223cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size) 5233cab1e71SAndy Shevchenko { 5243cab1e71SAndy Shevchenko struct printf_spec spec; 5253cab1e71SAndy Shevchenko 5263cab1e71SAndy Shevchenko spec.type = FORMAT_TYPE_PTR; 5273cab1e71SAndy Shevchenko spec.field_width = 2 + 2 * size; /* 0x + hex */ 5283cab1e71SAndy Shevchenko spec.flags = SPECIAL | SMALL | ZEROPAD; 5293cab1e71SAndy Shevchenko spec.base = 16; 5303cab1e71SAndy Shevchenko spec.precision = -1; 5313cab1e71SAndy Shevchenko 5323cab1e71SAndy Shevchenko return number(buf, end, num, spec); 5333cab1e71SAndy Shevchenko } 5343cab1e71SAndy Shevchenko 535cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces) 5364b6ccca7SAl Viro { 5374b6ccca7SAl Viro size_t size; 5384b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5394b6ccca7SAl Viro return; 5404b6ccca7SAl Viro size = end - buf; 5414b6ccca7SAl Viro if (size <= spaces) { 5424b6ccca7SAl Viro memset(buf, ' ', size); 5434b6ccca7SAl Viro return; 5444b6ccca7SAl Viro } 5454b6ccca7SAl Viro if (len) { 5464b6ccca7SAl Viro if (len > size - spaces) 5474b6ccca7SAl Viro len = size - spaces; 5484b6ccca7SAl Viro memmove(buf + spaces, buf, len); 5494b6ccca7SAl Viro } 5504b6ccca7SAl Viro memset(buf, ' ', spaces); 5514b6ccca7SAl Viro } 5524b6ccca7SAl Viro 553cfccde04SRasmus Villemoes /* 554cfccde04SRasmus Villemoes * Handle field width padding for a string. 555cfccde04SRasmus Villemoes * @buf: current buffer position 556cfccde04SRasmus Villemoes * @n: length of string 557cfccde04SRasmus Villemoes * @end: end of output buffer 558cfccde04SRasmus Villemoes * @spec: for field width and flags 559cfccde04SRasmus Villemoes * Returns: new buffer position after padding. 560cfccde04SRasmus Villemoes */ 561cfccde04SRasmus Villemoes static noinline_for_stack 562cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec) 563cfccde04SRasmus Villemoes { 564cfccde04SRasmus Villemoes unsigned spaces; 565cfccde04SRasmus Villemoes 566cfccde04SRasmus Villemoes if (likely(n >= spec.field_width)) 567cfccde04SRasmus Villemoes return buf; 568cfccde04SRasmus Villemoes /* we want to pad the sucker */ 569cfccde04SRasmus Villemoes spaces = spec.field_width - n; 570cfccde04SRasmus Villemoes if (!(spec.flags & LEFT)) { 571cfccde04SRasmus Villemoes move_right(buf - n, end, n, spaces); 572cfccde04SRasmus Villemoes return buf + spaces; 573cfccde04SRasmus Villemoes } 574cfccde04SRasmus Villemoes while (spaces--) { 575cfccde04SRasmus Villemoes if (buf < end) 576cfccde04SRasmus Villemoes *buf = ' '; 577cfccde04SRasmus Villemoes ++buf; 578cfccde04SRasmus Villemoes } 579cfccde04SRasmus Villemoes return buf; 580cfccde04SRasmus Villemoes } 581cfccde04SRasmus Villemoes 5824b6ccca7SAl Viro static noinline_for_stack 58395508cfaSRasmus Villemoes char *string(char *buf, char *end, const char *s, struct printf_spec spec) 58495508cfaSRasmus Villemoes { 58534fc8b90SRasmus Villemoes int len = 0; 58634fc8b90SRasmus Villemoes size_t lim = spec.precision; 58795508cfaSRasmus Villemoes 58895508cfaSRasmus Villemoes if ((unsigned long)s < PAGE_SIZE) 58995508cfaSRasmus Villemoes s = "(null)"; 59095508cfaSRasmus Villemoes 59134fc8b90SRasmus Villemoes while (lim--) { 59234fc8b90SRasmus Villemoes char c = *s++; 59334fc8b90SRasmus Villemoes if (!c) 59434fc8b90SRasmus Villemoes break; 59595508cfaSRasmus Villemoes if (buf < end) 59634fc8b90SRasmus Villemoes *buf = c; 59795508cfaSRasmus Villemoes ++buf; 59834fc8b90SRasmus Villemoes ++len; 59995508cfaSRasmus Villemoes } 60034fc8b90SRasmus Villemoes return widen_string(buf, len, end, spec); 60195508cfaSRasmus Villemoes } 60295508cfaSRasmus Villemoes 60395508cfaSRasmus Villemoes static noinline_for_stack 6044b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 6054b6ccca7SAl Viro const char *fmt) 6064b6ccca7SAl Viro { 6074b6ccca7SAl Viro const char *array[4], *s; 6084b6ccca7SAl Viro const struct dentry *p; 6094b6ccca7SAl Viro int depth; 6104b6ccca7SAl Viro int i, n; 6114b6ccca7SAl Viro 6124b6ccca7SAl Viro switch (fmt[1]) { 6134b6ccca7SAl Viro case '2': case '3': case '4': 6144b6ccca7SAl Viro depth = fmt[1] - '0'; 6154b6ccca7SAl Viro break; 6164b6ccca7SAl Viro default: 6174b6ccca7SAl Viro depth = 1; 6184b6ccca7SAl Viro } 6194b6ccca7SAl Viro 6204b6ccca7SAl Viro rcu_read_lock(); 6214b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 6224b6ccca7SAl Viro p = ACCESS_ONCE(d->d_parent); 6234b6ccca7SAl Viro array[i] = ACCESS_ONCE(d->d_name.name); 6244b6ccca7SAl Viro if (p == d) { 6254b6ccca7SAl Viro if (i) 6264b6ccca7SAl Viro array[i] = ""; 6274b6ccca7SAl Viro i++; 6284b6ccca7SAl Viro break; 6294b6ccca7SAl Viro } 6304b6ccca7SAl Viro } 6314b6ccca7SAl Viro s = array[--i]; 6324b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 6334b6ccca7SAl Viro char c = *s++; 6344b6ccca7SAl Viro if (!c) { 6354b6ccca7SAl Viro if (!i) 6364b6ccca7SAl Viro break; 6374b6ccca7SAl Viro c = '/'; 6384b6ccca7SAl Viro s = array[--i]; 6394b6ccca7SAl Viro } 6404b6ccca7SAl Viro if (buf < end) 6414b6ccca7SAl Viro *buf = c; 6424b6ccca7SAl Viro } 6434b6ccca7SAl Viro rcu_read_unlock(); 644cfccde04SRasmus Villemoes return widen_string(buf, n, end, spec); 6454b6ccca7SAl Viro } 6464b6ccca7SAl Viro 6471031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 6481031bc58SDmitry Monakhov static noinline_for_stack 6491031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev, 6501031bc58SDmitry Monakhov struct printf_spec spec, const char *fmt) 6511031bc58SDmitry Monakhov { 6521031bc58SDmitry Monakhov struct gendisk *hd = bdev->bd_disk; 6531031bc58SDmitry Monakhov 6541031bc58SDmitry Monakhov buf = string(buf, end, hd->disk_name, spec); 6551031bc58SDmitry Monakhov if (bdev->bd_part->partno) { 6561031bc58SDmitry Monakhov if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { 6571031bc58SDmitry Monakhov if (buf < end) 6581031bc58SDmitry Monakhov *buf = 'p'; 6591031bc58SDmitry Monakhov buf++; 6601031bc58SDmitry Monakhov } 6611031bc58SDmitry Monakhov buf = number(buf, end, bdev->bd_part->partno, spec); 6621031bc58SDmitry Monakhov } 6631031bc58SDmitry Monakhov return buf; 6641031bc58SDmitry Monakhov } 6651031bc58SDmitry Monakhov #endif 6661031bc58SDmitry Monakhov 667cf3b429bSJoe Perches static noinline_for_stack 668cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 669b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 6700fe1ef24SLinus Torvalds { 671b0d33c2bSJoe Perches unsigned long value; 6720fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 6730fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 674b0d33c2bSJoe Perches #endif 675b0d33c2bSJoe Perches 676b0d33c2bSJoe Perches if (fmt[1] == 'R') 677b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 678b0d33c2bSJoe Perches value = (unsigned long)ptr; 679b0d33c2bSJoe Perches 680b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 681b0d33c2bSJoe Perches if (*fmt == 'B') 6820f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 683b0d33c2bSJoe Perches else if (*fmt != 'f' && *fmt != 's') 6840fe1ef24SLinus Torvalds sprint_symbol(sym, value); 6850c8b946eSFrederic Weisbecker else 6864796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 6877b9186f5SAndré Goddard Rosa 688fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 6890fe1ef24SLinus Torvalds #else 6903cab1e71SAndy Shevchenko return special_hex_number(buf, end, value, sizeof(void *)); 6910fe1ef24SLinus Torvalds #endif 6920fe1ef24SLinus Torvalds } 6930fe1ef24SLinus Torvalds 694cf3b429bSJoe Perches static noinline_for_stack 695cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 696fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 697332d2e78SLinus Torvalds { 698332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 69928405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 700332d2e78SLinus Torvalds #endif 701332d2e78SLinus Torvalds 702332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 70328405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 704332d2e78SLinus Torvalds #endif 7054da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 706fef20d9cSFrederic Weisbecker .base = 16, 7074da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 708fef20d9cSFrederic Weisbecker .precision = -1, 709fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 710fef20d9cSFrederic Weisbecker }; 7114da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 7124da0b66cSBjorn Helgaas .base = 16, 7134da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 7144da0b66cSBjorn Helgaas .precision = -1, 7154da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 7164da0b66cSBjorn Helgaas }; 7170f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 7180f4050c7SBjorn Helgaas .base = 16, 7190f4050c7SBjorn Helgaas .field_width = 2, 7200f4050c7SBjorn Helgaas .precision = -1, 7210f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 7220f4050c7SBjorn Helgaas }; 7234da0b66cSBjorn Helgaas static const struct printf_spec dec_spec = { 724c91d3376SBjorn Helgaas .base = 10, 725c91d3376SBjorn Helgaas .precision = -1, 726c91d3376SBjorn Helgaas .flags = 0, 727c91d3376SBjorn Helgaas }; 7284da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 729fd95541eSBjorn Helgaas .field_width = -1, 730fd95541eSBjorn Helgaas .precision = 10, 731fd95541eSBjorn Helgaas .flags = LEFT, 732fd95541eSBjorn Helgaas }; 7334da0b66cSBjorn Helgaas static const struct printf_spec flag_spec = { 734fd95541eSBjorn Helgaas .base = 16, 735fd95541eSBjorn Helgaas .precision = -1, 736fd95541eSBjorn Helgaas .flags = SPECIAL | SMALL, 737fd95541eSBjorn Helgaas }; 738c7dabef8SBjorn Helgaas 739c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 740c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 741c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 742c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 7439d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 744c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 745c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 746c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 747c7dabef8SBjorn Helgaas 748332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 749c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 7504da0b66cSBjorn Helgaas const struct printf_spec *specp; 751332d2e78SLinus Torvalds 752332d2e78SLinus Torvalds *p++ = '['; 7534da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 754fd95541eSBjorn Helgaas p = string(p, pend, "io ", str_spec); 7554da0b66cSBjorn Helgaas specp = &io_spec; 7564da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 757fd95541eSBjorn Helgaas p = string(p, pend, "mem ", str_spec); 7584da0b66cSBjorn Helgaas specp = &mem_spec; 7594da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 760fd95541eSBjorn Helgaas p = string(p, pend, "irq ", str_spec); 7614da0b66cSBjorn Helgaas specp = &dec_spec; 7624da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 763fd95541eSBjorn Helgaas p = string(p, pend, "dma ", str_spec); 7644da0b66cSBjorn Helgaas specp = &dec_spec; 7650f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 7660f4050c7SBjorn Helgaas p = string(p, pend, "bus ", str_spec); 7670f4050c7SBjorn Helgaas specp = &bus_spec; 7684da0b66cSBjorn Helgaas } else { 769c7dabef8SBjorn Helgaas p = string(p, pend, "??? ", str_spec); 7704da0b66cSBjorn Helgaas specp = &mem_spec; 771c7dabef8SBjorn Helgaas decode = 0; 772fd95541eSBjorn Helgaas } 773d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 774d19cb803SBjorn Helgaas p = string(p, pend, "size ", str_spec); 775d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 776d19cb803SBjorn Helgaas } else { 7774da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 778c91d3376SBjorn Helgaas if (res->start != res->end) { 779332d2e78SLinus Torvalds *p++ = '-'; 7804da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 781c91d3376SBjorn Helgaas } 782d19cb803SBjorn Helgaas } 783c7dabef8SBjorn Helgaas if (decode) { 784fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 785fd95541eSBjorn Helgaas p = string(p, pend, " 64bit", str_spec); 786fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 787fd95541eSBjorn Helgaas p = string(p, pend, " pref", str_spec); 7889d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 7899d7cca04SBjorn Helgaas p = string(p, pend, " window", str_spec); 790fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 791fd95541eSBjorn Helgaas p = string(p, pend, " disabled", str_spec); 792c7dabef8SBjorn Helgaas } else { 793fd95541eSBjorn Helgaas p = string(p, pend, " flags ", str_spec); 794c7dabef8SBjorn Helgaas p = number(p, pend, res->flags, flag_spec); 795fd95541eSBjorn Helgaas } 796332d2e78SLinus Torvalds *p++ = ']'; 797c7dabef8SBjorn Helgaas *p = '\0'; 798332d2e78SLinus Torvalds 799fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 800332d2e78SLinus Torvalds } 801332d2e78SLinus Torvalds 802cf3b429bSJoe Perches static noinline_for_stack 80331550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 80431550a16SAndy Shevchenko const char *fmt) 80531550a16SAndy Shevchenko { 806360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 80731550a16SAndy Shevchenko negative value, fallback to the default */ 80831550a16SAndy Shevchenko char separator; 80931550a16SAndy Shevchenko 81031550a16SAndy Shevchenko if (spec.field_width == 0) 81131550a16SAndy Shevchenko /* nothing to print */ 81231550a16SAndy Shevchenko return buf; 81331550a16SAndy Shevchenko 81431550a16SAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 81531550a16SAndy Shevchenko /* NULL pointer */ 81631550a16SAndy Shevchenko return string(buf, end, NULL, spec); 81731550a16SAndy Shevchenko 81831550a16SAndy Shevchenko switch (fmt[1]) { 81931550a16SAndy Shevchenko case 'C': 82031550a16SAndy Shevchenko separator = ':'; 82131550a16SAndy Shevchenko break; 82231550a16SAndy Shevchenko case 'D': 82331550a16SAndy Shevchenko separator = '-'; 82431550a16SAndy Shevchenko break; 82531550a16SAndy Shevchenko case 'N': 82631550a16SAndy Shevchenko separator = 0; 82731550a16SAndy Shevchenko break; 82831550a16SAndy Shevchenko default: 82931550a16SAndy Shevchenko separator = ' '; 83031550a16SAndy Shevchenko break; 83131550a16SAndy Shevchenko } 83231550a16SAndy Shevchenko 83331550a16SAndy Shevchenko if (spec.field_width > 0) 83431550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 83531550a16SAndy Shevchenko 8369c98f235SRasmus Villemoes for (i = 0; i < len; ++i) { 8379c98f235SRasmus Villemoes if (buf < end) 8389c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]); 8399c98f235SRasmus Villemoes ++buf; 8409c98f235SRasmus Villemoes if (buf < end) 8419c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]); 8429c98f235SRasmus Villemoes ++buf; 84331550a16SAndy Shevchenko 8449c98f235SRasmus Villemoes if (separator && i != len - 1) { 8459c98f235SRasmus Villemoes if (buf < end) 8469c98f235SRasmus Villemoes *buf = separator; 8479c98f235SRasmus Villemoes ++buf; 8489c98f235SRasmus Villemoes } 84931550a16SAndy Shevchenko } 85031550a16SAndy Shevchenko 85131550a16SAndy Shevchenko return buf; 85231550a16SAndy Shevchenko } 85331550a16SAndy Shevchenko 85431550a16SAndy Shevchenko static noinline_for_stack 855dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap, 856dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 857dbc760bcSTejun Heo { 858dbc760bcSTejun Heo const int CHUNKSZ = 32; 859dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 860dbc760bcSTejun Heo int i, chunksz; 861dbc760bcSTejun Heo bool first = true; 862dbc760bcSTejun Heo 863dbc760bcSTejun Heo /* reused to print numbers */ 864dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 865dbc760bcSTejun Heo 866dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1); 867dbc760bcSTejun Heo if (chunksz == 0) 868dbc760bcSTejun Heo chunksz = CHUNKSZ; 869dbc760bcSTejun Heo 870dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 871dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) { 872dbc760bcSTejun Heo u32 chunkmask, val; 873dbc760bcSTejun Heo int word, bit; 874dbc760bcSTejun Heo 875dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1); 876dbc760bcSTejun Heo word = i / BITS_PER_LONG; 877dbc760bcSTejun Heo bit = i % BITS_PER_LONG; 878dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask; 879dbc760bcSTejun Heo 880dbc760bcSTejun Heo if (!first) { 881dbc760bcSTejun Heo if (buf < end) 882dbc760bcSTejun Heo *buf = ','; 883dbc760bcSTejun Heo buf++; 884dbc760bcSTejun Heo } 885dbc760bcSTejun Heo first = false; 886dbc760bcSTejun Heo 887dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4); 888dbc760bcSTejun Heo buf = number(buf, end, val, spec); 889dbc760bcSTejun Heo 890dbc760bcSTejun Heo chunksz = CHUNKSZ; 891dbc760bcSTejun Heo } 892dbc760bcSTejun Heo return buf; 893dbc760bcSTejun Heo } 894dbc760bcSTejun Heo 895dbc760bcSTejun Heo static noinline_for_stack 896dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, 897dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 898dbc760bcSTejun Heo { 899dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 900dbc760bcSTejun Heo /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 901dbc760bcSTejun Heo int cur, rbot, rtop; 902dbc760bcSTejun Heo bool first = true; 903dbc760bcSTejun Heo 904dbc760bcSTejun Heo /* reused to print numbers */ 905dbc760bcSTejun Heo spec = (struct printf_spec){ .base = 10 }; 906dbc760bcSTejun Heo 907dbc760bcSTejun Heo rbot = cur = find_first_bit(bitmap, nr_bits); 908dbc760bcSTejun Heo while (cur < nr_bits) { 909dbc760bcSTejun Heo rtop = cur; 910dbc760bcSTejun Heo cur = find_next_bit(bitmap, nr_bits, cur + 1); 911dbc760bcSTejun Heo if (cur < nr_bits && cur <= rtop + 1) 912dbc760bcSTejun Heo continue; 913dbc760bcSTejun Heo 914dbc760bcSTejun Heo if (!first) { 915dbc760bcSTejun Heo if (buf < end) 916dbc760bcSTejun Heo *buf = ','; 917dbc760bcSTejun Heo buf++; 918dbc760bcSTejun Heo } 919dbc760bcSTejun Heo first = false; 920dbc760bcSTejun Heo 921dbc760bcSTejun Heo buf = number(buf, end, rbot, spec); 922dbc760bcSTejun Heo if (rbot < rtop) { 923dbc760bcSTejun Heo if (buf < end) 924dbc760bcSTejun Heo *buf = '-'; 925dbc760bcSTejun Heo buf++; 926dbc760bcSTejun Heo 927dbc760bcSTejun Heo buf = number(buf, end, rtop, spec); 928dbc760bcSTejun Heo } 929dbc760bcSTejun Heo 930dbc760bcSTejun Heo rbot = cur; 931dbc760bcSTejun Heo } 932dbc760bcSTejun Heo return buf; 933dbc760bcSTejun Heo } 934dbc760bcSTejun Heo 935dbc760bcSTejun Heo static noinline_for_stack 936cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 9378a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 938dd45c9cfSHarvey Harrison { 9398a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 940dd45c9cfSHarvey Harrison char *p = mac_addr; 941dd45c9cfSHarvey Harrison int i; 942bc7259a2SJoe Perches char separator; 94376597ff9SAndrei Emeltchenko bool reversed = false; 944bc7259a2SJoe Perches 94576597ff9SAndrei Emeltchenko switch (fmt[1]) { 94676597ff9SAndrei Emeltchenko case 'F': 947bc7259a2SJoe Perches separator = '-'; 94876597ff9SAndrei Emeltchenko break; 94976597ff9SAndrei Emeltchenko 95076597ff9SAndrei Emeltchenko case 'R': 95176597ff9SAndrei Emeltchenko reversed = true; 95276597ff9SAndrei Emeltchenko /* fall through */ 95376597ff9SAndrei Emeltchenko 95476597ff9SAndrei Emeltchenko default: 955bc7259a2SJoe Perches separator = ':'; 95676597ff9SAndrei Emeltchenko break; 957bc7259a2SJoe Perches } 958dd45c9cfSHarvey Harrison 959dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 96076597ff9SAndrei Emeltchenko if (reversed) 96176597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 96276597ff9SAndrei Emeltchenko else 96355036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 96476597ff9SAndrei Emeltchenko 9658a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 966bc7259a2SJoe Perches *p++ = separator; 967dd45c9cfSHarvey Harrison } 968dd45c9cfSHarvey Harrison *p = '\0'; 969dd45c9cfSHarvey Harrison 970fef20d9cSFrederic Weisbecker return string(buf, end, mac_addr, spec); 971dd45c9cfSHarvey Harrison } 972dd45c9cfSHarvey Harrison 973cf3b429bSJoe Perches static noinline_for_stack 974cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 975689afa7dSHarvey Harrison { 976689afa7dSHarvey Harrison int i; 9770159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 9780159f24eSJoe Perches int index; 9790159f24eSJoe Perches int step; 980689afa7dSHarvey Harrison 9810159f24eSJoe Perches switch (fmt[2]) { 9820159f24eSJoe Perches case 'h': 9830159f24eSJoe Perches #ifdef __BIG_ENDIAN 9840159f24eSJoe Perches index = 0; 9850159f24eSJoe Perches step = 1; 9860159f24eSJoe Perches #else 9870159f24eSJoe Perches index = 3; 9880159f24eSJoe Perches step = -1; 9890159f24eSJoe Perches #endif 9900159f24eSJoe Perches break; 9910159f24eSJoe Perches case 'l': 9920159f24eSJoe Perches index = 3; 9930159f24eSJoe Perches step = -1; 9940159f24eSJoe Perches break; 9950159f24eSJoe Perches case 'n': 9960159f24eSJoe Perches case 'b': 9970159f24eSJoe Perches default: 9980159f24eSJoe Perches index = 0; 9990159f24eSJoe Perches step = 1; 10000159f24eSJoe Perches break; 10010159f24eSJoe Perches } 10028a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 10037c43d9a3SRasmus Villemoes char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 1004133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 10058a27f7c9SJoe Perches if (leading_zeros) { 10068a27f7c9SJoe Perches if (digits < 3) 10078a27f7c9SJoe Perches *p++ = '0'; 10088a27f7c9SJoe Perches if (digits < 2) 10098a27f7c9SJoe Perches *p++ = '0'; 10108a27f7c9SJoe Perches } 10118a27f7c9SJoe Perches /* reverse the digits in the quad */ 10128a27f7c9SJoe Perches while (digits--) 10138a27f7c9SJoe Perches *p++ = temp[digits]; 10148a27f7c9SJoe Perches if (i < 3) 10158a27f7c9SJoe Perches *p++ = '.'; 10160159f24eSJoe Perches index += step; 10178a27f7c9SJoe Perches } 10188a27f7c9SJoe Perches *p = '\0'; 10197b9186f5SAndré Goddard Rosa 10208a27f7c9SJoe Perches return p; 10218a27f7c9SJoe Perches } 10228a27f7c9SJoe Perches 1023cf3b429bSJoe Perches static noinline_for_stack 1024cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 10258a27f7c9SJoe Perches { 10267b9186f5SAndré Goddard Rosa int i, j, range; 10278a27f7c9SJoe Perches unsigned char zerolength[8]; 10288a27f7c9SJoe Perches int longest = 1; 10298a27f7c9SJoe Perches int colonpos = -1; 10308a27f7c9SJoe Perches u16 word; 10317b9186f5SAndré Goddard Rosa u8 hi, lo; 10328a27f7c9SJoe Perches bool needcolon = false; 1033eb78cd26SJoe Perches bool useIPv4; 1034eb78cd26SJoe Perches struct in6_addr in6; 1035eb78cd26SJoe Perches 1036eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 1037eb78cd26SJoe Perches 1038eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 10398a27f7c9SJoe Perches 10408a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 10418a27f7c9SJoe Perches 10428a27f7c9SJoe Perches if (useIPv4) 10438a27f7c9SJoe Perches range = 6; 10448a27f7c9SJoe Perches else 10458a27f7c9SJoe Perches range = 8; 10468a27f7c9SJoe Perches 10478a27f7c9SJoe Perches /* find position of longest 0 run */ 10488a27f7c9SJoe Perches for (i = 0; i < range; i++) { 10498a27f7c9SJoe Perches for (j = i; j < range; j++) { 1050eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 10518a27f7c9SJoe Perches break; 10528a27f7c9SJoe Perches zerolength[i]++; 10538a27f7c9SJoe Perches } 10548a27f7c9SJoe Perches } 10558a27f7c9SJoe Perches for (i = 0; i < range; i++) { 10568a27f7c9SJoe Perches if (zerolength[i] > longest) { 10578a27f7c9SJoe Perches longest = zerolength[i]; 10588a27f7c9SJoe Perches colonpos = i; 10598a27f7c9SJoe Perches } 10608a27f7c9SJoe Perches } 106129cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 106229cf519eSJoe Perches colonpos = -1; 10638a27f7c9SJoe Perches 10648a27f7c9SJoe Perches /* emit address */ 10658a27f7c9SJoe Perches for (i = 0; i < range; i++) { 10668a27f7c9SJoe Perches if (i == colonpos) { 10678a27f7c9SJoe Perches if (needcolon || i == 0) 10688a27f7c9SJoe Perches *p++ = ':'; 10698a27f7c9SJoe Perches *p++ = ':'; 10708a27f7c9SJoe Perches needcolon = false; 10718a27f7c9SJoe Perches i += longest - 1; 10728a27f7c9SJoe Perches continue; 10738a27f7c9SJoe Perches } 10748a27f7c9SJoe Perches if (needcolon) { 10758a27f7c9SJoe Perches *p++ = ':'; 10768a27f7c9SJoe Perches needcolon = false; 10778a27f7c9SJoe Perches } 10788a27f7c9SJoe Perches /* hex u16 without leading 0s */ 1079eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 10808a27f7c9SJoe Perches hi = word >> 8; 10818a27f7c9SJoe Perches lo = word & 0xff; 10828a27f7c9SJoe Perches if (hi) { 10838a27f7c9SJoe Perches if (hi > 0x0f) 108455036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 10858a27f7c9SJoe Perches else 10868a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 108755036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 10888a27f7c9SJoe Perches } 1089b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 109055036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 10918a27f7c9SJoe Perches else 10928a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 10938a27f7c9SJoe Perches needcolon = true; 10948a27f7c9SJoe Perches } 10958a27f7c9SJoe Perches 10968a27f7c9SJoe Perches if (useIPv4) { 10978a27f7c9SJoe Perches if (needcolon) 10988a27f7c9SJoe Perches *p++ = ':'; 10990159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 11008a27f7c9SJoe Perches } 11018a27f7c9SJoe Perches *p = '\0'; 11027b9186f5SAndré Goddard Rosa 11038a27f7c9SJoe Perches return p; 11048a27f7c9SJoe Perches } 11058a27f7c9SJoe Perches 1106cf3b429bSJoe Perches static noinline_for_stack 1107cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 11088a27f7c9SJoe Perches { 11098a27f7c9SJoe Perches int i; 11107b9186f5SAndré Goddard Rosa 1111689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 111255036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 111355036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 11148a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 1115689afa7dSHarvey Harrison *p++ = ':'; 1116689afa7dSHarvey Harrison } 1117689afa7dSHarvey Harrison *p = '\0'; 11187b9186f5SAndré Goddard Rosa 11198a27f7c9SJoe Perches return p; 11208a27f7c9SJoe Perches } 11218a27f7c9SJoe Perches 1122cf3b429bSJoe Perches static noinline_for_stack 1123cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 11248a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 11258a27f7c9SJoe Perches { 11268a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 11278a27f7c9SJoe Perches 11288a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 1129eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 11308a27f7c9SJoe Perches else 1131eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 1132689afa7dSHarvey Harrison 1133fef20d9cSFrederic Weisbecker return string(buf, end, ip6_addr, spec); 1134689afa7dSHarvey Harrison } 1135689afa7dSHarvey Harrison 1136cf3b429bSJoe Perches static noinline_for_stack 1137cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 11388a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 11394aa99606SHarvey Harrison { 11408a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 11414aa99606SHarvey Harrison 11420159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 11434aa99606SHarvey Harrison 1144fef20d9cSFrederic Weisbecker return string(buf, end, ip4_addr, spec); 11454aa99606SHarvey Harrison } 11464aa99606SHarvey Harrison 1147cf3b429bSJoe Perches static noinline_for_stack 114810679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 114910679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 115010679643SDaniel Borkmann { 115110679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 115210679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 115310679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 115410679643SDaniel Borkmann sizeof("%1234567890")]; 115510679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 115610679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 115710679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 115810679643SDaniel Borkmann u8 off = 0; 115910679643SDaniel Borkmann 116010679643SDaniel Borkmann fmt++; 116110679643SDaniel Borkmann while (isalpha(*++fmt)) { 116210679643SDaniel Borkmann switch (*fmt) { 116310679643SDaniel Borkmann case 'p': 116410679643SDaniel Borkmann have_p = true; 116510679643SDaniel Borkmann break; 116610679643SDaniel Borkmann case 'f': 116710679643SDaniel Borkmann have_f = true; 116810679643SDaniel Borkmann break; 116910679643SDaniel Borkmann case 's': 117010679643SDaniel Borkmann have_s = true; 117110679643SDaniel Borkmann break; 117210679643SDaniel Borkmann case 'c': 117310679643SDaniel Borkmann have_c = true; 117410679643SDaniel Borkmann break; 117510679643SDaniel Borkmann } 117610679643SDaniel Borkmann } 117710679643SDaniel Borkmann 117810679643SDaniel Borkmann if (have_p || have_s || have_f) { 117910679643SDaniel Borkmann *p = '['; 118010679643SDaniel Borkmann off = 1; 118110679643SDaniel Borkmann } 118210679643SDaniel Borkmann 118310679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 118410679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 118510679643SDaniel Borkmann else 118610679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 118710679643SDaniel Borkmann 118810679643SDaniel Borkmann if (have_p || have_s || have_f) 118910679643SDaniel Borkmann *p++ = ']'; 119010679643SDaniel Borkmann 119110679643SDaniel Borkmann if (have_p) { 119210679643SDaniel Borkmann *p++ = ':'; 119310679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 119410679643SDaniel Borkmann } 119510679643SDaniel Borkmann if (have_f) { 119610679643SDaniel Borkmann *p++ = '/'; 119710679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 119810679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 119910679643SDaniel Borkmann } 120010679643SDaniel Borkmann if (have_s) { 120110679643SDaniel Borkmann *p++ = '%'; 120210679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 120310679643SDaniel Borkmann } 120410679643SDaniel Borkmann *p = '\0'; 120510679643SDaniel Borkmann 120610679643SDaniel Borkmann return string(buf, end, ip6_addr, spec); 120710679643SDaniel Borkmann } 120810679643SDaniel Borkmann 120910679643SDaniel Borkmann static noinline_for_stack 121010679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 121110679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 121210679643SDaniel Borkmann { 121310679643SDaniel Borkmann bool have_p = false; 121410679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 121510679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 121610679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 121710679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 121810679643SDaniel Borkmann 121910679643SDaniel Borkmann fmt++; 122010679643SDaniel Borkmann while (isalpha(*++fmt)) { 122110679643SDaniel Borkmann switch (*fmt) { 122210679643SDaniel Borkmann case 'p': 122310679643SDaniel Borkmann have_p = true; 122410679643SDaniel Borkmann break; 122510679643SDaniel Borkmann case 'h': 122610679643SDaniel Borkmann case 'l': 122710679643SDaniel Borkmann case 'n': 122810679643SDaniel Borkmann case 'b': 122910679643SDaniel Borkmann fmt4[2] = *fmt; 123010679643SDaniel Borkmann break; 123110679643SDaniel Borkmann } 123210679643SDaniel Borkmann } 123310679643SDaniel Borkmann 123410679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 123510679643SDaniel Borkmann if (have_p) { 123610679643SDaniel Borkmann *p++ = ':'; 123710679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 123810679643SDaniel Borkmann } 123910679643SDaniel Borkmann *p = '\0'; 124010679643SDaniel Borkmann 124110679643SDaniel Borkmann return string(buf, end, ip4_addr, spec); 124210679643SDaniel Borkmann } 124310679643SDaniel Borkmann 124410679643SDaniel Borkmann static noinline_for_stack 124571dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 124671dca95dSAndy Shevchenko const char *fmt) 124771dca95dSAndy Shevchenko { 124871dca95dSAndy Shevchenko bool found = true; 124971dca95dSAndy Shevchenko int count = 1; 125071dca95dSAndy Shevchenko unsigned int flags = 0; 125171dca95dSAndy Shevchenko int len; 125271dca95dSAndy Shevchenko 125371dca95dSAndy Shevchenko if (spec.field_width == 0) 125471dca95dSAndy Shevchenko return buf; /* nothing to print */ 125571dca95dSAndy Shevchenko 125671dca95dSAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 125771dca95dSAndy Shevchenko return string(buf, end, NULL, spec); /* NULL pointer */ 125871dca95dSAndy Shevchenko 125971dca95dSAndy Shevchenko 126071dca95dSAndy Shevchenko do { 126171dca95dSAndy Shevchenko switch (fmt[count++]) { 126271dca95dSAndy Shevchenko case 'a': 126371dca95dSAndy Shevchenko flags |= ESCAPE_ANY; 126471dca95dSAndy Shevchenko break; 126571dca95dSAndy Shevchenko case 'c': 126671dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL; 126771dca95dSAndy Shevchenko break; 126871dca95dSAndy Shevchenko case 'h': 126971dca95dSAndy Shevchenko flags |= ESCAPE_HEX; 127071dca95dSAndy Shevchenko break; 127171dca95dSAndy Shevchenko case 'n': 127271dca95dSAndy Shevchenko flags |= ESCAPE_NULL; 127371dca95dSAndy Shevchenko break; 127471dca95dSAndy Shevchenko case 'o': 127571dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL; 127671dca95dSAndy Shevchenko break; 127771dca95dSAndy Shevchenko case 'p': 127871dca95dSAndy Shevchenko flags |= ESCAPE_NP; 127971dca95dSAndy Shevchenko break; 128071dca95dSAndy Shevchenko case 's': 128171dca95dSAndy Shevchenko flags |= ESCAPE_SPACE; 128271dca95dSAndy Shevchenko break; 128371dca95dSAndy Shevchenko default: 128471dca95dSAndy Shevchenko found = false; 128571dca95dSAndy Shevchenko break; 128671dca95dSAndy Shevchenko } 128771dca95dSAndy Shevchenko } while (found); 128871dca95dSAndy Shevchenko 128971dca95dSAndy Shevchenko if (!flags) 129071dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP; 129171dca95dSAndy Shevchenko 129271dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width; 129371dca95dSAndy Shevchenko 129441416f23SRasmus Villemoes /* 129541416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to 129641416f23SRasmus Villemoes * the given buffer, and returns the total size of the output 129741416f23SRasmus Villemoes * had the buffer been big enough. 129841416f23SRasmus Villemoes */ 129941416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 130071dca95dSAndy Shevchenko 130171dca95dSAndy Shevchenko return buf; 130271dca95dSAndy Shevchenko } 130371dca95dSAndy Shevchenko 130471dca95dSAndy Shevchenko static noinline_for_stack 1305cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 13069ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 13079ac6e44eSJoe Perches { 13082b1b0d66SAndy Shevchenko char uuid[UUID_STRING_LEN + 1]; 13099ac6e44eSJoe Perches char *p = uuid; 13109ac6e44eSJoe Perches int i; 13112b1b0d66SAndy Shevchenko const u8 *index = uuid_be_index; 13129ac6e44eSJoe Perches bool uc = false; 13139ac6e44eSJoe Perches 13149ac6e44eSJoe Perches switch (*(++fmt)) { 13159ac6e44eSJoe Perches case 'L': 13169ac6e44eSJoe Perches uc = true; /* fall-through */ 13179ac6e44eSJoe Perches case 'l': 13182b1b0d66SAndy Shevchenko index = uuid_le_index; 13199ac6e44eSJoe Perches break; 13209ac6e44eSJoe Perches case 'B': 13219ac6e44eSJoe Perches uc = true; 13229ac6e44eSJoe Perches break; 13239ac6e44eSJoe Perches } 13249ac6e44eSJoe Perches 13259ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 1326aa4ea1c3SAndy Shevchenko if (uc) 1327aa4ea1c3SAndy Shevchenko p = hex_byte_pack_upper(p, addr[index[i]]); 1328aa4ea1c3SAndy Shevchenko else 132955036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 13309ac6e44eSJoe Perches switch (i) { 13319ac6e44eSJoe Perches case 3: 13329ac6e44eSJoe Perches case 5: 13339ac6e44eSJoe Perches case 7: 13349ac6e44eSJoe Perches case 9: 13359ac6e44eSJoe Perches *p++ = '-'; 13369ac6e44eSJoe Perches break; 13379ac6e44eSJoe Perches } 13389ac6e44eSJoe Perches } 13399ac6e44eSJoe Perches 13409ac6e44eSJoe Perches *p = 0; 13419ac6e44eSJoe Perches 13429ac6e44eSJoe Perches return string(buf, end, uuid, spec); 13439ac6e44eSJoe Perches } 13449ac6e44eSJoe Perches 13455b17aecfSAndy Shevchenko static noinline_for_stack 13465b17aecfSAndy Shevchenko char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt) 1347c8f44affSMichał Mirosław { 13485b17aecfSAndy Shevchenko unsigned long long num; 13495b17aecfSAndy Shevchenko int size; 13505b17aecfSAndy Shevchenko 13515b17aecfSAndy Shevchenko switch (fmt[1]) { 13525b17aecfSAndy Shevchenko case 'F': 13535b17aecfSAndy Shevchenko num = *(const netdev_features_t *)addr; 13545b17aecfSAndy Shevchenko size = sizeof(netdev_features_t); 13555b17aecfSAndy Shevchenko break; 13565b17aecfSAndy Shevchenko default: 13575b17aecfSAndy Shevchenko num = (unsigned long)addr; 13585b17aecfSAndy Shevchenko size = sizeof(unsigned long); 13595b17aecfSAndy Shevchenko break; 13605b17aecfSAndy Shevchenko } 1361c8f44affSMichał Mirosław 13623cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1363c8f44affSMichał Mirosław } 1364c8f44affSMichał Mirosław 1365aaf07621SJoe Perches static noinline_for_stack 13663cab1e71SAndy Shevchenko char *address_val(char *buf, char *end, const void *addr, const char *fmt) 1367aaf07621SJoe Perches { 1368aaf07621SJoe Perches unsigned long long num; 13693cab1e71SAndy Shevchenko int size; 1370aaf07621SJoe Perches 1371aaf07621SJoe Perches switch (fmt[1]) { 1372aaf07621SJoe Perches case 'd': 1373aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 13743cab1e71SAndy Shevchenko size = sizeof(dma_addr_t); 1375aaf07621SJoe Perches break; 1376aaf07621SJoe Perches case 'p': 1377aaf07621SJoe Perches default: 1378aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 13793cab1e71SAndy Shevchenko size = sizeof(phys_addr_t); 1380aaf07621SJoe Perches break; 1381aaf07621SJoe Perches } 1382aaf07621SJoe Perches 13833cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1384aaf07621SJoe Perches } 1385aaf07621SJoe Perches 1386900cca29SGeert Uytterhoeven static noinline_for_stack 1387900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1388900cca29SGeert Uytterhoeven const char *fmt) 1389900cca29SGeert Uytterhoeven { 1390900cca29SGeert Uytterhoeven if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk) 1391900cca29SGeert Uytterhoeven return string(buf, end, NULL, spec); 1392900cca29SGeert Uytterhoeven 1393900cca29SGeert Uytterhoeven switch (fmt[1]) { 1394900cca29SGeert Uytterhoeven case 'r': 1395900cca29SGeert Uytterhoeven return number(buf, end, clk_get_rate(clk), spec); 1396900cca29SGeert Uytterhoeven 1397900cca29SGeert Uytterhoeven case 'n': 1398900cca29SGeert Uytterhoeven default: 1399900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1400900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1401900cca29SGeert Uytterhoeven #else 14023cab1e71SAndy Shevchenko return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long)); 1403900cca29SGeert Uytterhoeven #endif 1404900cca29SGeert Uytterhoeven } 1405900cca29SGeert Uytterhoeven } 1406900cca29SGeert Uytterhoeven 1407edf14cdbSVlastimil Babka static 1408edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags, 1409edf14cdbSVlastimil Babka const struct trace_print_flags *names) 1410edf14cdbSVlastimil Babka { 1411edf14cdbSVlastimil Babka unsigned long mask; 1412edf14cdbSVlastimil Babka const struct printf_spec strspec = { 1413edf14cdbSVlastimil Babka .field_width = -1, 1414edf14cdbSVlastimil Babka .precision = -1, 1415edf14cdbSVlastimil Babka }; 1416edf14cdbSVlastimil Babka const struct printf_spec numspec = { 1417edf14cdbSVlastimil Babka .flags = SPECIAL|SMALL, 1418edf14cdbSVlastimil Babka .field_width = -1, 1419edf14cdbSVlastimil Babka .precision = -1, 1420edf14cdbSVlastimil Babka .base = 16, 1421edf14cdbSVlastimil Babka }; 1422edf14cdbSVlastimil Babka 1423edf14cdbSVlastimil Babka for ( ; flags && names->name; names++) { 1424edf14cdbSVlastimil Babka mask = names->mask; 1425edf14cdbSVlastimil Babka if ((flags & mask) != mask) 1426edf14cdbSVlastimil Babka continue; 1427edf14cdbSVlastimil Babka 1428edf14cdbSVlastimil Babka buf = string(buf, end, names->name, strspec); 1429edf14cdbSVlastimil Babka 1430edf14cdbSVlastimil Babka flags &= ~mask; 1431edf14cdbSVlastimil Babka if (flags) { 1432edf14cdbSVlastimil Babka if (buf < end) 1433edf14cdbSVlastimil Babka *buf = '|'; 1434edf14cdbSVlastimil Babka buf++; 1435edf14cdbSVlastimil Babka } 1436edf14cdbSVlastimil Babka } 1437edf14cdbSVlastimil Babka 1438edf14cdbSVlastimil Babka if (flags) 1439edf14cdbSVlastimil Babka buf = number(buf, end, flags, numspec); 1440edf14cdbSVlastimil Babka 1441edf14cdbSVlastimil Babka return buf; 1442edf14cdbSVlastimil Babka } 1443edf14cdbSVlastimil Babka 1444edf14cdbSVlastimil Babka static noinline_for_stack 1445edf14cdbSVlastimil Babka char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt) 1446edf14cdbSVlastimil Babka { 1447edf14cdbSVlastimil Babka unsigned long flags; 1448edf14cdbSVlastimil Babka const struct trace_print_flags *names; 1449edf14cdbSVlastimil Babka 1450edf14cdbSVlastimil Babka switch (fmt[1]) { 1451edf14cdbSVlastimil Babka case 'p': 1452edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1453edf14cdbSVlastimil Babka /* Remove zone id */ 1454edf14cdbSVlastimil Babka flags &= (1UL << NR_PAGEFLAGS) - 1; 1455edf14cdbSVlastimil Babka names = pageflag_names; 1456edf14cdbSVlastimil Babka break; 1457edf14cdbSVlastimil Babka case 'v': 1458edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1459edf14cdbSVlastimil Babka names = vmaflag_names; 1460edf14cdbSVlastimil Babka break; 1461edf14cdbSVlastimil Babka case 'g': 1462edf14cdbSVlastimil Babka flags = *(gfp_t *)flags_ptr; 1463edf14cdbSVlastimil Babka names = gfpflag_names; 1464edf14cdbSVlastimil Babka break; 1465edf14cdbSVlastimil Babka default: 1466edf14cdbSVlastimil Babka WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]); 1467edf14cdbSVlastimil Babka return buf; 1468edf14cdbSVlastimil Babka } 1469edf14cdbSVlastimil Babka 1470edf14cdbSVlastimil Babka return format_flags(buf, end, flags, names); 1471edf14cdbSVlastimil Babka } 1472edf14cdbSVlastimil Babka 1473411f05f1SIngo Molnar int kptr_restrict __read_mostly; 1474455cd5abSDan Rosenberg 14754d8a743cSLinus Torvalds /* 14764d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 14774d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 14784d8a743cSLinus Torvalds * specifiers. 14794d8a743cSLinus Torvalds * 1480*0b523769SJoe Perches * Please update scripts/checkpatch.pl when adding/removing conversion 1481*0b523769SJoe Perches * characters. (Search for "check for vsprintf extension"). 1482*0b523769SJoe Perches * 1483332d2e78SLinus Torvalds * Right now we handle: 14840fe1ef24SLinus Torvalds * 14850c8b946eSFrederic Weisbecker * - 'F' For symbolic function descriptor pointers with offset 14860c8b946eSFrederic Weisbecker * - 'f' For simple symbolic function names without offset 14870efb4d20SSteven Rostedt * - 'S' For symbolic direct pointers with offset 14880efb4d20SSteven Rostedt * - 's' For symbolic direct pointers without offset 1489b0d33c2bSJoe Perches * - '[FfSs]R' as above with __builtin_extract_return_addr() translation 14900f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 1491c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 1492c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 1493dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 1494dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 1495dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 1496dbc760bcSTejun Heo * range-list format instead of hex format 1497dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 1498dd45c9cfSHarvey Harrison * usual colon-separated hex notation 14998a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 1500bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 1501c8e00060SJoe Perches * with a dash-separated hex notation 15027c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 15038a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 15048a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 15058a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 150610679643SDaniel Borkmann * [S][pfs] 150710679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 150810679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 15098a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 15108a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 15118a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 151210679643SDaniel Borkmann * [S][pfs] 151310679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 151410679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 151510679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 151610679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 151729cf519eSJoe Perches * http://tools.ietf.org/html/rfc5952 151871dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 151971dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 152071dca95dSAndy Shevchenko * details): 152171dca95dSAndy Shevchenko * a - ESCAPE_ANY 152271dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 152371dca95dSAndy Shevchenko * h - ESCAPE_HEX 152471dca95dSAndy Shevchenko * n - ESCAPE_NULL 152571dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 152671dca95dSAndy Shevchenko * p - ESCAPE_NP 152771dca95dSAndy Shevchenko * s - ESCAPE_SPACE 152871dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 15299ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 15309ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 15319ac6e44eSJoe Perches * Options for %pU are: 15329ac6e44eSJoe Perches * b big endian lower case hex (default) 15339ac6e44eSJoe Perches * B big endian UPPER case hex 15349ac6e44eSJoe Perches * l little endian lower case hex 15359ac6e44eSJoe Perches * L little endian UPPER case hex 15369ac6e44eSJoe Perches * big endian output byte order is: 15379ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 15389ac6e44eSJoe Perches * little endian output byte order is: 15399ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 15407db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 15417db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 15427db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 15437db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 15447db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 1545455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 1546c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 154731550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 154831550a16SAndy Shevchenko * a certain separator (' ' by default): 154931550a16SAndy Shevchenko * C colon 155031550a16SAndy Shevchenko * D dash 155131550a16SAndy Shevchenko * N no separator 155231550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 155331550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 1554aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 1555aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 1556c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 1557c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 15581031bc58SDmitry Monakhov * - 'g' For block_device name (gendisk + partition number) 1559900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 1560900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 1561900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 1562900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 1563900cca29SGeert Uytterhoeven * - 'Cr' For a clock, it prints the current rate of the clock 1564edf14cdbSVlastimil Babka * - 'G' For flags to be printed as a collection of symbolic strings that would 1565edf14cdbSVlastimil Babka * construct the specific value. Supported flags given by option: 1566edf14cdbSVlastimil Babka * p page flags (see struct page) given as pointer to unsigned long 1567edf14cdbSVlastimil Babka * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 1568edf14cdbSVlastimil Babka * v vma flags (VM_*) given as pointer to unsigned long 15695e4ee7b1SMartin Kletzander * 15705e4ee7b1SMartin Kletzander * ** Please update also Documentation/printk-formats.txt when making changes ** 15719ac6e44eSJoe Perches * 1572332d2e78SLinus Torvalds * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 1573332d2e78SLinus Torvalds * function pointers are really function descriptors, which contain a 1574332d2e78SLinus Torvalds * pointer to the real address. 15754d8a743cSLinus Torvalds */ 1576cf3b429bSJoe Perches static noinline_for_stack 1577cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 1578fef20d9cSFrederic Weisbecker struct printf_spec spec) 157978a8bf69SLinus Torvalds { 158080c9eb46SRasmus Villemoes const int default_width = 2 * sizeof(void *); 1581725fe002SGrant Likely 15829f36e2c4SKees Cook if (!ptr && *fmt != 'K') { 15835e057981SJoe Perches /* 15845e057981SJoe Perches * Print (null) with the same width as a pointer so it makes 15855e057981SJoe Perches * tabular output look nice. 15865e057981SJoe Perches */ 15875e057981SJoe Perches if (spec.field_width == -1) 1588725fe002SGrant Likely spec.field_width = default_width; 1589fef20d9cSFrederic Weisbecker return string(buf, end, "(null)", spec); 15905e057981SJoe Perches } 1591d97106abSLinus Torvalds 15920fe1ef24SLinus Torvalds switch (*fmt) { 15930fe1ef24SLinus Torvalds case 'F': 15940c8b946eSFrederic Weisbecker case 'f': 15950fe1ef24SLinus Torvalds ptr = dereference_function_descriptor(ptr); 15960fe1ef24SLinus Torvalds /* Fallthrough */ 15970fe1ef24SLinus Torvalds case 'S': 15989ac6e44eSJoe Perches case 's': 15990f77a8d3SNamhyung Kim case 'B': 1600b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 1601332d2e78SLinus Torvalds case 'R': 1602c7dabef8SBjorn Helgaas case 'r': 1603fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 160431550a16SAndy Shevchenko case 'h': 160531550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 1606dbc760bcSTejun Heo case 'b': 1607dbc760bcSTejun Heo switch (fmt[1]) { 1608dbc760bcSTejun Heo case 'l': 1609dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 1610dbc760bcSTejun Heo default: 1611dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 1612dbc760bcSTejun Heo } 16138a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 16148a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 161576597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 161676597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 16178a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 16188a27f7c9SJoe Perches case 'I': /* Formatted IP supported 16198a27f7c9SJoe Perches * 4: 1.2.3.4 16208a27f7c9SJoe Perches * 6: 0001:0203:...:0708 16218a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 16228a27f7c9SJoe Perches */ 16238a27f7c9SJoe Perches case 'i': /* Contiguous: 16248a27f7c9SJoe Perches * 4: 001.002.003.004 16258a27f7c9SJoe Perches * 6: 000102...0f 16268a27f7c9SJoe Perches */ 16278a27f7c9SJoe Perches switch (fmt[1]) { 16288a27f7c9SJoe Perches case '6': 16298a27f7c9SJoe Perches return ip6_addr_string(buf, end, ptr, spec, fmt); 16308a27f7c9SJoe Perches case '4': 16318a27f7c9SJoe Perches return ip4_addr_string(buf, end, ptr, spec, fmt); 163210679643SDaniel Borkmann case 'S': { 163310679643SDaniel Borkmann const union { 163410679643SDaniel Borkmann struct sockaddr raw; 163510679643SDaniel Borkmann struct sockaddr_in v4; 163610679643SDaniel Borkmann struct sockaddr_in6 v6; 163710679643SDaniel Borkmann } *sa = ptr; 163810679643SDaniel Borkmann 163910679643SDaniel Borkmann switch (sa->raw.sa_family) { 164010679643SDaniel Borkmann case AF_INET: 164110679643SDaniel Borkmann return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 164210679643SDaniel Borkmann case AF_INET6: 164310679643SDaniel Borkmann return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 164410679643SDaniel Borkmann default: 164510679643SDaniel Borkmann return string(buf, end, "(invalid address)", spec); 164610679643SDaniel Borkmann }} 16478a27f7c9SJoe Perches } 16484aa99606SHarvey Harrison break; 164971dca95dSAndy Shevchenko case 'E': 165071dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 16519ac6e44eSJoe Perches case 'U': 16529ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 16537db6f5fbSJoe Perches case 'V': 16545756b76eSJan Beulich { 16555756b76eSJan Beulich va_list va; 16565756b76eSJan Beulich 16575756b76eSJan Beulich va_copy(va, *((struct va_format *)ptr)->va); 16585756b76eSJan Beulich buf += vsnprintf(buf, end > buf ? end - buf : 0, 16595756b76eSJan Beulich ((struct va_format *)ptr)->fmt, va); 16605756b76eSJan Beulich va_end(va); 16615756b76eSJan Beulich return buf; 16625756b76eSJan Beulich } 1663455cd5abSDan Rosenberg case 'K': 1664312b4e22SRyan Mallon switch (kptr_restrict) { 1665312b4e22SRyan Mallon case 0: 1666312b4e22SRyan Mallon /* Always print %pK values */ 1667312b4e22SRyan Mallon break; 1668312b4e22SRyan Mallon case 1: { 16697eb39129SJason A. Donenfeld const struct cred *cred; 16707eb39129SJason A. Donenfeld 16717eb39129SJason A. Donenfeld /* 16727eb39129SJason A. Donenfeld * kptr_restrict==1 cannot be used in IRQ context 16737eb39129SJason A. Donenfeld * because its test for CAP_SYSLOG would be meaningless. 16747eb39129SJason A. Donenfeld */ 16757eb39129SJason A. Donenfeld if (in_irq() || in_serving_softirq() || in_nmi()) { 16767eb39129SJason A. Donenfeld if (spec.field_width == -1) 16777eb39129SJason A. Donenfeld spec.field_width = default_width; 16787eb39129SJason A. Donenfeld return string(buf, end, "pK-error", spec); 16797eb39129SJason A. Donenfeld } 16807eb39129SJason A. Donenfeld 1681312b4e22SRyan Mallon /* 1682312b4e22SRyan Mallon * Only print the real pointer value if the current 1683312b4e22SRyan Mallon * process has CAP_SYSLOG and is running with the 1684312b4e22SRyan Mallon * same credentials it started with. This is because 1685312b4e22SRyan Mallon * access to files is checked at open() time, but %pK 1686312b4e22SRyan Mallon * checks permission at read() time. We don't want to 1687312b4e22SRyan Mallon * leak pointer values if a binary opens a file using 1688312b4e22SRyan Mallon * %pK and then elevates privileges before reading it. 1689312b4e22SRyan Mallon */ 16907eb39129SJason A. Donenfeld cred = current_cred(); 1691312b4e22SRyan Mallon if (!has_capability_noaudit(current, CAP_SYSLOG) || 1692312b4e22SRyan Mallon !uid_eq(cred->euid, cred->uid) || 1693312b4e22SRyan Mallon !gid_eq(cred->egid, cred->gid)) 169426297607SJoe Perches ptr = NULL; 169526297607SJoe Perches break; 1696312b4e22SRyan Mallon } 1697312b4e22SRyan Mallon case 2: 1698312b4e22SRyan Mallon default: 1699312b4e22SRyan Mallon /* Always print 0's for %pK */ 1700312b4e22SRyan Mallon ptr = NULL; 1701312b4e22SRyan Mallon break; 1702312b4e22SRyan Mallon } 1703312b4e22SRyan Mallon break; 1704312b4e22SRyan Mallon 1705c8f44affSMichał Mirosław case 'N': 17065b17aecfSAndy Shevchenko return netdev_bits(buf, end, ptr, fmt); 17077d799210SStepan Moskovchenko case 'a': 17083cab1e71SAndy Shevchenko return address_val(buf, end, ptr, fmt); 17094b6ccca7SAl Viro case 'd': 17104b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 1711900cca29SGeert Uytterhoeven case 'C': 1712900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 17134b6ccca7SAl Viro case 'D': 17144b6ccca7SAl Viro return dentry_name(buf, end, 17154b6ccca7SAl Viro ((const struct file *)ptr)->f_path.dentry, 17164b6ccca7SAl Viro spec, fmt); 17171031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 17181031bc58SDmitry Monakhov case 'g': 17191031bc58SDmitry Monakhov return bdev_name(buf, end, ptr, spec, fmt); 17201031bc58SDmitry Monakhov #endif 17211031bc58SDmitry Monakhov 1722edf14cdbSVlastimil Babka case 'G': 1723edf14cdbSVlastimil Babka return flags_string(buf, end, ptr, fmt); 17240fe1ef24SLinus Torvalds } 1725fef20d9cSFrederic Weisbecker spec.flags |= SMALL; 1726fef20d9cSFrederic Weisbecker if (spec.field_width == -1) { 1727725fe002SGrant Likely spec.field_width = default_width; 1728fef20d9cSFrederic Weisbecker spec.flags |= ZEROPAD; 172978a8bf69SLinus Torvalds } 1730fef20d9cSFrederic Weisbecker spec.base = 16; 1731fef20d9cSFrederic Weisbecker 1732fef20d9cSFrederic Weisbecker return number(buf, end, (unsigned long) ptr, spec); 1733fef20d9cSFrederic Weisbecker } 1734fef20d9cSFrederic Weisbecker 1735fef20d9cSFrederic Weisbecker /* 1736fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 1737fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 1738fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 1739fef20d9cSFrederic Weisbecker * to go on the next call). 1740fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 1741fef20d9cSFrederic Weisbecker * 1742fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 1743fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 1744fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 17455b5e0928SAlexey Dobriyan * 'Z' changed to 'z' --adobriyan 2017-01-25 1746fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 1747fef20d9cSFrederic Weisbecker * 1748fef20d9cSFrederic Weisbecker * @fmt: the format string 1749fef20d9cSFrederic Weisbecker * @type of the token returned 1750fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 1751fef20d9cSFrederic Weisbecker * @field_width: overwritten width 1752fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 1753fef20d9cSFrederic Weisbecker * @precision: precision of a number 1754fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 1755fef20d9cSFrederic Weisbecker */ 1756cf3b429bSJoe Perches static noinline_for_stack 1757cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 1758fef20d9cSFrederic Weisbecker { 1759fef20d9cSFrederic Weisbecker const char *start = fmt; 1760d0484193SRasmus Villemoes char qualifier; 1761fef20d9cSFrederic Weisbecker 1762fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 1763ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 1764fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 1765fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 1766fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 1767fef20d9cSFrederic Weisbecker } 1768fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1769fef20d9cSFrederic Weisbecker goto precision; 1770fef20d9cSFrederic Weisbecker } 1771fef20d9cSFrederic Weisbecker 1772fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 1773fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 1774fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1775fef20d9cSFrederic Weisbecker spec->precision = 0; 1776fef20d9cSFrederic Weisbecker 1777fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1778fef20d9cSFrederic Weisbecker goto qualifier; 1779fef20d9cSFrederic Weisbecker } 1780fef20d9cSFrederic Weisbecker 1781fef20d9cSFrederic Weisbecker /* By default */ 1782fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1783fef20d9cSFrederic Weisbecker 1784fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 1785fef20d9cSFrederic Weisbecker if (*fmt == '%') 1786fef20d9cSFrederic Weisbecker break; 1787fef20d9cSFrederic Weisbecker } 1788fef20d9cSFrederic Weisbecker 1789fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 1790fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 1791fef20d9cSFrederic Weisbecker return fmt - start; 1792fef20d9cSFrederic Weisbecker 1793fef20d9cSFrederic Weisbecker /* Process flags */ 1794fef20d9cSFrederic Weisbecker spec->flags = 0; 1795fef20d9cSFrederic Weisbecker 1796fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 1797fef20d9cSFrederic Weisbecker bool found = true; 1798fef20d9cSFrederic Weisbecker 1799fef20d9cSFrederic Weisbecker ++fmt; 1800fef20d9cSFrederic Weisbecker 1801fef20d9cSFrederic Weisbecker switch (*fmt) { 1802fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 1803fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 1804fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 1805fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 1806fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 1807fef20d9cSFrederic Weisbecker default: found = false; 1808fef20d9cSFrederic Weisbecker } 1809fef20d9cSFrederic Weisbecker 1810fef20d9cSFrederic Weisbecker if (!found) 1811fef20d9cSFrederic Weisbecker break; 1812fef20d9cSFrederic Weisbecker } 1813fef20d9cSFrederic Weisbecker 1814fef20d9cSFrederic Weisbecker /* get field width */ 1815fef20d9cSFrederic Weisbecker spec->field_width = -1; 1816fef20d9cSFrederic Weisbecker 1817fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 1818fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 1819fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 1820fef20d9cSFrederic Weisbecker /* it's the next argument */ 1821ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 1822fef20d9cSFrederic Weisbecker return ++fmt - start; 1823fef20d9cSFrederic Weisbecker } 1824fef20d9cSFrederic Weisbecker 1825fef20d9cSFrederic Weisbecker precision: 1826fef20d9cSFrederic Weisbecker /* get the precision */ 1827fef20d9cSFrederic Weisbecker spec->precision = -1; 1828fef20d9cSFrederic Weisbecker if (*fmt == '.') { 1829fef20d9cSFrederic Weisbecker ++fmt; 1830fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 1831fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 1832fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1833fef20d9cSFrederic Weisbecker spec->precision = 0; 1834fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 1835fef20d9cSFrederic Weisbecker /* it's the next argument */ 1836adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 1837fef20d9cSFrederic Weisbecker return ++fmt - start; 1838fef20d9cSFrederic Weisbecker } 1839fef20d9cSFrederic Weisbecker } 1840fef20d9cSFrederic Weisbecker 1841fef20d9cSFrederic Weisbecker qualifier: 1842fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 1843d0484193SRasmus Villemoes qualifier = 0; 184475fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 18455b5e0928SAlexey Dobriyan *fmt == 'z' || *fmt == 't') { 1846d0484193SRasmus Villemoes qualifier = *fmt++; 1847d0484193SRasmus Villemoes if (unlikely(qualifier == *fmt)) { 1848d0484193SRasmus Villemoes if (qualifier == 'l') { 1849d0484193SRasmus Villemoes qualifier = 'L'; 1850fef20d9cSFrederic Weisbecker ++fmt; 1851d0484193SRasmus Villemoes } else if (qualifier == 'h') { 1852d0484193SRasmus Villemoes qualifier = 'H'; 1853a4e94ef0SZhaolei ++fmt; 1854a4e94ef0SZhaolei } 1855fef20d9cSFrederic Weisbecker } 1856fef20d9cSFrederic Weisbecker } 1857fef20d9cSFrederic Weisbecker 1858fef20d9cSFrederic Weisbecker /* default base */ 1859fef20d9cSFrederic Weisbecker spec->base = 10; 1860fef20d9cSFrederic Weisbecker switch (*fmt) { 1861fef20d9cSFrederic Weisbecker case 'c': 1862fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 1863fef20d9cSFrederic Weisbecker return ++fmt - start; 1864fef20d9cSFrederic Weisbecker 1865fef20d9cSFrederic Weisbecker case 's': 1866fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 1867fef20d9cSFrederic Weisbecker return ++fmt - start; 1868fef20d9cSFrederic Weisbecker 1869fef20d9cSFrederic Weisbecker case 'p': 1870fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 1871ffbfed03SRasmus Villemoes return ++fmt - start; 1872fef20d9cSFrederic Weisbecker 1873fef20d9cSFrederic Weisbecker case '%': 1874fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 1875fef20d9cSFrederic Weisbecker return ++fmt - start; 1876fef20d9cSFrederic Weisbecker 1877fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 1878fef20d9cSFrederic Weisbecker case 'o': 1879fef20d9cSFrederic Weisbecker spec->base = 8; 1880fef20d9cSFrederic Weisbecker break; 1881fef20d9cSFrederic Weisbecker 1882fef20d9cSFrederic Weisbecker case 'x': 1883fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 1884fef20d9cSFrederic Weisbecker 1885fef20d9cSFrederic Weisbecker case 'X': 1886fef20d9cSFrederic Weisbecker spec->base = 16; 1887fef20d9cSFrederic Weisbecker break; 1888fef20d9cSFrederic Weisbecker 1889fef20d9cSFrederic Weisbecker case 'd': 1890fef20d9cSFrederic Weisbecker case 'i': 189139e874f8SFrederic Weisbecker spec->flags |= SIGN; 1892fef20d9cSFrederic Weisbecker case 'u': 1893fef20d9cSFrederic Weisbecker break; 1894fef20d9cSFrederic Weisbecker 1895708d96fdSRyan Mallon case 'n': 1896708d96fdSRyan Mallon /* 1897b006f19bSRasmus Villemoes * Since %n poses a greater security risk than 1898b006f19bSRasmus Villemoes * utility, treat it as any other invalid or 1899b006f19bSRasmus Villemoes * unsupported format specifier. 1900708d96fdSRyan Mallon */ 1901708d96fdSRyan Mallon /* Fall-through */ 1902708d96fdSRyan Mallon 1903fef20d9cSFrederic Weisbecker default: 1904b006f19bSRasmus Villemoes WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 1905fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 1906fef20d9cSFrederic Weisbecker return fmt - start; 1907fef20d9cSFrederic Weisbecker } 1908fef20d9cSFrederic Weisbecker 1909d0484193SRasmus Villemoes if (qualifier == 'L') 1910fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 1911d0484193SRasmus Villemoes else if (qualifier == 'l') { 191251be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 191351be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 19145b5e0928SAlexey Dobriyan } else if (qualifier == 'z') { 1915fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 1916d0484193SRasmus Villemoes } else if (qualifier == 't') { 1917fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 1918d0484193SRasmus Villemoes } else if (qualifier == 'H') { 191951be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 192051be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 1921d0484193SRasmus Villemoes } else if (qualifier == 'h') { 192251be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 192351be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 1924fef20d9cSFrederic Weisbecker } else { 192551be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 192651be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 1927fef20d9cSFrederic Weisbecker } 1928fef20d9cSFrederic Weisbecker 1929fef20d9cSFrederic Weisbecker return ++fmt - start; 193078a8bf69SLinus Torvalds } 193178a8bf69SLinus Torvalds 19324d72ba01SRasmus Villemoes static void 19334d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width) 19344d72ba01SRasmus Villemoes { 19354d72ba01SRasmus Villemoes spec->field_width = width; 19364d72ba01SRasmus Villemoes if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 19374d72ba01SRasmus Villemoes spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 19384d72ba01SRasmus Villemoes } 19394d72ba01SRasmus Villemoes } 19404d72ba01SRasmus Villemoes 19414d72ba01SRasmus Villemoes static void 19424d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec) 19434d72ba01SRasmus Villemoes { 19444d72ba01SRasmus Villemoes spec->precision = prec; 19454d72ba01SRasmus Villemoes if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 19464d72ba01SRasmus Villemoes spec->precision = clamp(prec, 0, PRECISION_MAX); 19474d72ba01SRasmus Villemoes } 19484d72ba01SRasmus Villemoes } 19494d72ba01SRasmus Villemoes 19501da177e4SLinus Torvalds /** 19511da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 19521da177e4SLinus Torvalds * @buf: The buffer to place the result into 19531da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 19541da177e4SLinus Torvalds * @fmt: The format string to use 19551da177e4SLinus Torvalds * @args: Arguments for the format string 19561da177e4SLinus Torvalds * 1957d7ec9a05SRasmus Villemoes * This function generally follows C99 vsnprintf, but has some 1958d7ec9a05SRasmus Villemoes * extensions and a few limitations: 1959d7ec9a05SRasmus Villemoes * 19606cc89134Smchehab@s-opensource.com * - ``%n`` is unsupported 19616cc89134Smchehab@s-opensource.com * - ``%p*`` is handled by pointer() 196220036fdcSAndi Kleen * 19635e4ee7b1SMartin Kletzander * See pointer() or Documentation/printk-formats.txt for more 19645e4ee7b1SMartin Kletzander * extensive description. 19655e4ee7b1SMartin Kletzander * 19665e4ee7b1SMartin Kletzander * **Please update the documentation in both places when making changes** 196780f548e0SAndrew Morton * 19681da177e4SLinus Torvalds * The return value is the number of characters which would 19691da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 19701da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 19711da177e4SLinus Torvalds * number of characters written into @buf as return value 197272fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 19731da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 19741da177e4SLinus Torvalds * string is truncated. 19751da177e4SLinus Torvalds * 1976ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 19771da177e4SLinus Torvalds */ 19781da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 19791da177e4SLinus Torvalds { 19801da177e4SLinus Torvalds unsigned long long num; 1981d4be151bSAndré Goddard Rosa char *str, *end; 1982fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 19831da177e4SLinus Torvalds 1984f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 1985f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 19862aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 19871da177e4SLinus Torvalds return 0; 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds str = buf; 1990f796937aSJeremy Fitzhardinge end = buf + size; 19911da177e4SLinus Torvalds 1992f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 1993f796937aSJeremy Fitzhardinge if (end < buf) { 19941da177e4SLinus Torvalds end = ((void *)-1); 1995f796937aSJeremy Fitzhardinge size = end - buf; 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 1998fef20d9cSFrederic Weisbecker while (*fmt) { 1999fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2000d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2001fef20d9cSFrederic Weisbecker 2002fef20d9cSFrederic Weisbecker fmt += read; 2003fef20d9cSFrederic Weisbecker 2004fef20d9cSFrederic Weisbecker switch (spec.type) { 2005fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2006fef20d9cSFrederic Weisbecker int copy = read; 2007fef20d9cSFrederic Weisbecker if (str < end) { 2008fef20d9cSFrederic Weisbecker if (copy > end - str) 2009fef20d9cSFrederic Weisbecker copy = end - str; 2010fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2011fef20d9cSFrederic Weisbecker } 2012fef20d9cSFrederic Weisbecker str += read; 2013fef20d9cSFrederic Weisbecker break; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 2016ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 20174d72ba01SRasmus Villemoes set_field_width(&spec, va_arg(args, int)); 2018fef20d9cSFrederic Weisbecker break; 20191da177e4SLinus Torvalds 2020fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 20214d72ba01SRasmus Villemoes set_precision(&spec, va_arg(args, int)); 2022fef20d9cSFrederic Weisbecker break; 20231da177e4SLinus Torvalds 2024d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2025d4be151bSAndré Goddard Rosa char c; 2026d4be151bSAndré Goddard Rosa 2027fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2028fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2029f796937aSJeremy Fitzhardinge if (str < end) 20301da177e4SLinus Torvalds *str = ' '; 20311da177e4SLinus Torvalds ++str; 2032fef20d9cSFrederic Weisbecker 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 2036f796937aSJeremy Fitzhardinge if (str < end) 20371da177e4SLinus Torvalds *str = c; 20381da177e4SLinus Torvalds ++str; 2039fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2040f796937aSJeremy Fitzhardinge if (str < end) 20411da177e4SLinus Torvalds *str = ' '; 20421da177e4SLinus Torvalds ++str; 20431da177e4SLinus Torvalds } 2044fef20d9cSFrederic Weisbecker break; 2045d4be151bSAndré Goddard Rosa } 20461da177e4SLinus Torvalds 2047fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 2048fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 2049fef20d9cSFrederic Weisbecker break; 20501da177e4SLinus Torvalds 2051fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2052ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 2053fef20d9cSFrederic Weisbecker spec); 2054fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 20554d8a743cSLinus Torvalds fmt++; 2056fef20d9cSFrederic Weisbecker break; 20571da177e4SLinus Torvalds 2058fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2059f796937aSJeremy Fitzhardinge if (str < end) 20601da177e4SLinus Torvalds *str = '%'; 20611da177e4SLinus Torvalds ++str; 20621da177e4SLinus Torvalds break; 20631da177e4SLinus Torvalds 2064fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 2065b006f19bSRasmus Villemoes /* 2066b006f19bSRasmus Villemoes * Presumably the arguments passed gcc's type 2067b006f19bSRasmus Villemoes * checking, but there is no safe or sane way 2068b006f19bSRasmus Villemoes * for us to continue parsing the format and 2069b006f19bSRasmus Villemoes * fetching from the va_list; the remaining 2070b006f19bSRasmus Villemoes * specifiers and arguments would be out of 2071b006f19bSRasmus Villemoes * sync. 2072b006f19bSRasmus Villemoes */ 2073b006f19bSRasmus Villemoes goto out; 2074fef20d9cSFrederic Weisbecker 2075fef20d9cSFrederic Weisbecker default: 2076fef20d9cSFrederic Weisbecker switch (spec.type) { 2077fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2078fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 2079fef20d9cSFrederic Weisbecker break; 2080fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2081fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 2082fef20d9cSFrederic Weisbecker break; 2083fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2084fef20d9cSFrederic Weisbecker num = va_arg(args, long); 2085fef20d9cSFrederic Weisbecker break; 2086fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2087ef124960SJason Gunthorpe if (spec.flags & SIGN) 2088ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 2089ef124960SJason Gunthorpe else 2090fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 2091fef20d9cSFrederic Weisbecker break; 2092fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2093fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 2094fef20d9cSFrederic Weisbecker break; 2095a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2096a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 2097a4e94ef0SZhaolei break; 2098a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2099a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 2100a4e94ef0SZhaolei break; 2101fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2102fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 2103fef20d9cSFrederic Weisbecker break; 2104fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2105fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 2106fef20d9cSFrederic Weisbecker break; 210739e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 210839e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 2109fef20d9cSFrederic Weisbecker break; 2110fef20d9cSFrederic Weisbecker default: 2111fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 21121da177e4SLinus Torvalds } 2113fef20d9cSFrederic Weisbecker 2114fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 21151da177e4SLinus Torvalds } 2116fef20d9cSFrederic Weisbecker } 2117fef20d9cSFrederic Weisbecker 2118b006f19bSRasmus Villemoes out: 2119f796937aSJeremy Fitzhardinge if (size > 0) { 2120f796937aSJeremy Fitzhardinge if (str < end) 21211da177e4SLinus Torvalds *str = '\0'; 2122f796937aSJeremy Fitzhardinge else 21230a6047eeSLinus Torvalds end[-1] = '\0'; 2124f796937aSJeremy Fitzhardinge } 2125fef20d9cSFrederic Weisbecker 2126f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 21271da177e4SLinus Torvalds return str-buf; 2128fef20d9cSFrederic Weisbecker 21291da177e4SLinus Torvalds } 21301da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds /** 21331da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 21341da177e4SLinus Torvalds * @buf: The buffer to place the result into 21351da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 21361da177e4SLinus Torvalds * @fmt: The format string to use 21371da177e4SLinus Torvalds * @args: Arguments for the format string 21381da177e4SLinus Torvalds * 21391da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2140b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 21411da177e4SLinus Torvalds * returns 0. 21421da177e4SLinus Torvalds * 2143ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 214420036fdcSAndi Kleen * 214520036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 21461da177e4SLinus Torvalds */ 21471da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 21481da177e4SLinus Torvalds { 21491da177e4SLinus Torvalds int i; 21501da177e4SLinus Torvalds 21511da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 21527b9186f5SAndré Goddard Rosa 2153b921c69fSAnton Arapov if (likely(i < size)) 2154b921c69fSAnton Arapov return i; 2155b921c69fSAnton Arapov if (size != 0) 2156b921c69fSAnton Arapov return size - 1; 2157b921c69fSAnton Arapov return 0; 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds /** 21621da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 21631da177e4SLinus Torvalds * @buf: The buffer to place the result into 21641da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 21651da177e4SLinus Torvalds * @fmt: The format string to use 21661da177e4SLinus Torvalds * @...: Arguments for the format string 21671da177e4SLinus Torvalds * 21681da177e4SLinus Torvalds * The return value is the number of characters which would be 21691da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 21701da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 21711da177e4SLinus Torvalds * @size, the resulting string is truncated. 217220036fdcSAndi Kleen * 217320036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 21741da177e4SLinus Torvalds */ 21751da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 21761da177e4SLinus Torvalds { 21771da177e4SLinus Torvalds va_list args; 21781da177e4SLinus Torvalds int i; 21791da177e4SLinus Torvalds 21801da177e4SLinus Torvalds va_start(args, fmt); 21811da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 21821da177e4SLinus Torvalds va_end(args); 21837b9186f5SAndré Goddard Rosa 21841da177e4SLinus Torvalds return i; 21851da177e4SLinus Torvalds } 21861da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds /** 21891da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 21901da177e4SLinus Torvalds * @buf: The buffer to place the result into 21911da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 21921da177e4SLinus Torvalds * @fmt: The format string to use 21931da177e4SLinus Torvalds * @...: Arguments for the format string 21941da177e4SLinus Torvalds * 21951da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2196b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 21971da177e4SLinus Torvalds */ 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 22001da177e4SLinus Torvalds { 22011da177e4SLinus Torvalds va_list args; 22021da177e4SLinus Torvalds int i; 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds va_start(args, fmt); 2205b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 22061da177e4SLinus Torvalds va_end(args); 22077b9186f5SAndré Goddard Rosa 2208b903c0b8SChangli Gao return i; 22091da177e4SLinus Torvalds } 22101da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds /** 22131da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 22141da177e4SLinus Torvalds * @buf: The buffer to place the result into 22151da177e4SLinus Torvalds * @fmt: The format string to use 22161da177e4SLinus Torvalds * @args: Arguments for the format string 22171da177e4SLinus Torvalds * 22181da177e4SLinus Torvalds * The function returns the number of characters written 221972fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 22201da177e4SLinus Torvalds * buffer overflows. 22211da177e4SLinus Torvalds * 2222ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 222320036fdcSAndi Kleen * 222420036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 22251da177e4SLinus Torvalds */ 22261da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 22271da177e4SLinus Torvalds { 22281da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 22291da177e4SLinus Torvalds } 22301da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 22311da177e4SLinus Torvalds 22321da177e4SLinus Torvalds /** 22331da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 22341da177e4SLinus Torvalds * @buf: The buffer to place the result into 22351da177e4SLinus Torvalds * @fmt: The format string to use 22361da177e4SLinus Torvalds * @...: Arguments for the format string 22371da177e4SLinus Torvalds * 22381da177e4SLinus Torvalds * The function returns the number of characters written 223972fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 22401da177e4SLinus Torvalds * buffer overflows. 224120036fdcSAndi Kleen * 224220036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 22431da177e4SLinus Torvalds */ 22441da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 22451da177e4SLinus Torvalds { 22461da177e4SLinus Torvalds va_list args; 22471da177e4SLinus Torvalds int i; 22481da177e4SLinus Torvalds 22491da177e4SLinus Torvalds va_start(args, fmt); 22501da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 22511da177e4SLinus Torvalds va_end(args); 22527b9186f5SAndré Goddard Rosa 22531da177e4SLinus Torvalds return i; 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 22561da177e4SLinus Torvalds 22574370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 22584370aa4aSLai Jiangshan /* 22594370aa4aSLai Jiangshan * bprintf service: 22604370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 22614370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 22624370aa4aSLai Jiangshan */ 22634370aa4aSLai Jiangshan 22644370aa4aSLai Jiangshan /** 22654370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 22664370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 22674370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 22684370aa4aSLai Jiangshan * @fmt: The format string to use 22694370aa4aSLai Jiangshan * @args: Arguments for the format string 22704370aa4aSLai Jiangshan * 22714370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 2272da3dae54SMasanari Iida * is skipped. 22734370aa4aSLai Jiangshan * 22744370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 22754370aa4aSLai Jiangshan * the given input. 22764370aa4aSLai Jiangshan * 22774370aa4aSLai Jiangshan * NOTE: 22784370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 22794370aa4aSLai Jiangshan * valid for bstr_printf(). 22804370aa4aSLai Jiangshan */ 22814370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 22824370aa4aSLai Jiangshan { 2283fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 22844370aa4aSLai Jiangshan char *str, *end; 22854370aa4aSLai Jiangshan 22864370aa4aSLai Jiangshan str = (char *)bin_buf; 22874370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 22884370aa4aSLai Jiangshan 22894370aa4aSLai Jiangshan #define save_arg(type) \ 22904370aa4aSLai Jiangshan do { \ 22914370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 22924370aa4aSLai Jiangshan unsigned long long value; \ 22934370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 22944370aa4aSLai Jiangshan value = va_arg(args, unsigned long long); \ 22954370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 22964370aa4aSLai Jiangshan *(u32 *)str = *(u32 *)&value; \ 22974370aa4aSLai Jiangshan *(u32 *)(str + 4) = *((u32 *)&value + 1); \ 22984370aa4aSLai Jiangshan } \ 22994370aa4aSLai Jiangshan } else { \ 23004370aa4aSLai Jiangshan unsigned long value; \ 23014370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 23024370aa4aSLai Jiangshan value = va_arg(args, int); \ 23034370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 23044370aa4aSLai Jiangshan *(typeof(type) *)str = (type)value; \ 23054370aa4aSLai Jiangshan } \ 23064370aa4aSLai Jiangshan str += sizeof(type); \ 23074370aa4aSLai Jiangshan } while (0) 23084370aa4aSLai Jiangshan 2309fef20d9cSFrederic Weisbecker while (*fmt) { 2310d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 23114370aa4aSLai Jiangshan 2312fef20d9cSFrederic Weisbecker fmt += read; 2313fef20d9cSFrederic Weisbecker 2314fef20d9cSFrederic Weisbecker switch (spec.type) { 2315fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 2316d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 2317fef20d9cSFrederic Weisbecker break; 2318b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2319b006f19bSRasmus Villemoes goto out; 2320fef20d9cSFrederic Weisbecker 2321ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2322fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 23234370aa4aSLai Jiangshan save_arg(int); 2324fef20d9cSFrederic Weisbecker break; 23254370aa4aSLai Jiangshan 2326fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 23274370aa4aSLai Jiangshan save_arg(char); 2328fef20d9cSFrederic Weisbecker break; 2329fef20d9cSFrederic Weisbecker 2330fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 23314370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 23324370aa4aSLai Jiangshan size_t len; 23336c356634SAndré Goddard Rosa 23344370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 23354370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 23360f4f81dcSAndré Goddard Rosa save_str = "(null)"; 23376c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 23386c356634SAndré Goddard Rosa if (str + len < end) 23396c356634SAndré Goddard Rosa memcpy(str, save_str, len); 23406c356634SAndré Goddard Rosa str += len; 2341fef20d9cSFrederic Weisbecker break; 23424370aa4aSLai Jiangshan } 2343fef20d9cSFrederic Weisbecker 2344fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 23454370aa4aSLai Jiangshan save_arg(void *); 23464370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2347fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 23484370aa4aSLai Jiangshan fmt++; 2349fef20d9cSFrederic Weisbecker break; 2350fef20d9cSFrederic Weisbecker 2351fef20d9cSFrederic Weisbecker default: 2352fef20d9cSFrederic Weisbecker switch (spec.type) { 2353fef20d9cSFrederic Weisbecker 2354fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2355fef20d9cSFrederic Weisbecker save_arg(long long); 2356fef20d9cSFrederic Weisbecker break; 2357fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2358fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2359fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2360fef20d9cSFrederic Weisbecker break; 2361fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2362fef20d9cSFrederic Weisbecker save_arg(size_t); 2363fef20d9cSFrederic Weisbecker break; 2364fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2365fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2366fef20d9cSFrederic Weisbecker break; 2367a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2368a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2369a4e94ef0SZhaolei save_arg(char); 2370a4e94ef0SZhaolei break; 2371fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2372fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2373fef20d9cSFrederic Weisbecker save_arg(short); 2374fef20d9cSFrederic Weisbecker break; 2375fef20d9cSFrederic Weisbecker default: 2376fef20d9cSFrederic Weisbecker save_arg(int); 2377fef20d9cSFrederic Weisbecker } 2378fef20d9cSFrederic Weisbecker } 2379fef20d9cSFrederic Weisbecker } 2380fef20d9cSFrederic Weisbecker 2381b006f19bSRasmus Villemoes out: 23827b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2383fef20d9cSFrederic Weisbecker #undef save_arg 23844370aa4aSLai Jiangshan } 23854370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 23864370aa4aSLai Jiangshan 23874370aa4aSLai Jiangshan /** 23884370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 23894370aa4aSLai Jiangshan * @buf: The buffer to place the result into 23904370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 23914370aa4aSLai Jiangshan * @fmt: The format string to use 23924370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 23934370aa4aSLai Jiangshan * 23944370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 23954370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 23964370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 23974370aa4aSLai Jiangshan * 23984370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 23990efb4d20SSteven Rostedt * see vsnprintf comment for details. 24004370aa4aSLai Jiangshan * 24014370aa4aSLai Jiangshan * The return value is the number of characters which would 24024370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 24034370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 24044370aa4aSLai Jiangshan * number of characters written into @buf as return value 24054370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 24064370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 24074370aa4aSLai Jiangshan * string is truncated. 24084370aa4aSLai Jiangshan */ 24094370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 24104370aa4aSLai Jiangshan { 2411fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 2412d4be151bSAndré Goddard Rosa char *str, *end; 2413d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 24144370aa4aSLai Jiangshan 2415762abb51SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 24164370aa4aSLai Jiangshan return 0; 24174370aa4aSLai Jiangshan 24184370aa4aSLai Jiangshan str = buf; 24194370aa4aSLai Jiangshan end = buf + size; 24204370aa4aSLai Jiangshan 24214370aa4aSLai Jiangshan #define get_arg(type) \ 24224370aa4aSLai Jiangshan ({ \ 24234370aa4aSLai Jiangshan typeof(type) value; \ 24244370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 24254370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 24264370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 24274370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 24284370aa4aSLai Jiangshan } else { \ 24294370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 24304370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 24314370aa4aSLai Jiangshan } \ 24324370aa4aSLai Jiangshan args += sizeof(type); \ 24334370aa4aSLai Jiangshan value; \ 24344370aa4aSLai Jiangshan }) 24354370aa4aSLai Jiangshan 24364370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 24374370aa4aSLai Jiangshan if (end < buf) { 24384370aa4aSLai Jiangshan end = ((void *)-1); 24394370aa4aSLai Jiangshan size = end - buf; 24404370aa4aSLai Jiangshan } 24414370aa4aSLai Jiangshan 2442fef20d9cSFrederic Weisbecker while (*fmt) { 2443fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2444d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2445fef20d9cSFrederic Weisbecker 2446fef20d9cSFrederic Weisbecker fmt += read; 2447fef20d9cSFrederic Weisbecker 2448fef20d9cSFrederic Weisbecker switch (spec.type) { 2449fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2450fef20d9cSFrederic Weisbecker int copy = read; 2451fef20d9cSFrederic Weisbecker if (str < end) { 2452fef20d9cSFrederic Weisbecker if (copy > end - str) 2453fef20d9cSFrederic Weisbecker copy = end - str; 2454fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2455fef20d9cSFrederic Weisbecker } 2456fef20d9cSFrederic Weisbecker str += read; 2457fef20d9cSFrederic Weisbecker break; 24584370aa4aSLai Jiangshan } 24594370aa4aSLai Jiangshan 2460ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 24614d72ba01SRasmus Villemoes set_field_width(&spec, get_arg(int)); 2462fef20d9cSFrederic Weisbecker break; 24634370aa4aSLai Jiangshan 2464fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 24654d72ba01SRasmus Villemoes set_precision(&spec, get_arg(int)); 2466fef20d9cSFrederic Weisbecker break; 24674370aa4aSLai Jiangshan 2468d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2469d4be151bSAndré Goddard Rosa char c; 2470d4be151bSAndré Goddard Rosa 2471fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2472fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 24734370aa4aSLai Jiangshan if (str < end) 24744370aa4aSLai Jiangshan *str = ' '; 24754370aa4aSLai Jiangshan ++str; 24764370aa4aSLai Jiangshan } 24774370aa4aSLai Jiangshan } 24784370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 24794370aa4aSLai Jiangshan if (str < end) 24804370aa4aSLai Jiangshan *str = c; 24814370aa4aSLai Jiangshan ++str; 2482fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 24834370aa4aSLai Jiangshan if (str < end) 24844370aa4aSLai Jiangshan *str = ' '; 24854370aa4aSLai Jiangshan ++str; 24864370aa4aSLai Jiangshan } 2487fef20d9cSFrederic Weisbecker break; 2488d4be151bSAndré Goddard Rosa } 24894370aa4aSLai Jiangshan 2490fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 24914370aa4aSLai Jiangshan const char *str_arg = args; 2492d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 2493fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 2494fef20d9cSFrederic Weisbecker break; 24954370aa4aSLai Jiangshan } 24964370aa4aSLai Jiangshan 2497fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2498ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 2499fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 25004370aa4aSLai Jiangshan fmt++; 2501fef20d9cSFrederic Weisbecker break; 25024370aa4aSLai Jiangshan 2503fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 25044370aa4aSLai Jiangshan if (str < end) 25054370aa4aSLai Jiangshan *str = '%'; 25064370aa4aSLai Jiangshan ++str; 2507fef20d9cSFrederic Weisbecker break; 2508fef20d9cSFrederic Weisbecker 2509b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2510b006f19bSRasmus Villemoes goto out; 2511b006f19bSRasmus Villemoes 2512d4be151bSAndré Goddard Rosa default: { 2513d4be151bSAndré Goddard Rosa unsigned long long num; 2514d4be151bSAndré Goddard Rosa 2515fef20d9cSFrederic Weisbecker switch (spec.type) { 2516fef20d9cSFrederic Weisbecker 2517fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 25184370aa4aSLai Jiangshan num = get_arg(long long); 2519fef20d9cSFrederic Weisbecker break; 2520fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2521fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2522fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 2523fef20d9cSFrederic Weisbecker break; 2524fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 25254370aa4aSLai Jiangshan num = get_arg(size_t); 2526fef20d9cSFrederic Weisbecker break; 2527fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 25284370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 2529fef20d9cSFrederic Weisbecker break; 2530a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2531a4e94ef0SZhaolei num = get_arg(unsigned char); 2532a4e94ef0SZhaolei break; 2533a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2534a4e94ef0SZhaolei num = get_arg(signed char); 2535a4e94ef0SZhaolei break; 2536fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2537fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 2538fef20d9cSFrederic Weisbecker break; 2539fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2540fef20d9cSFrederic Weisbecker num = get_arg(short); 2541fef20d9cSFrederic Weisbecker break; 2542fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 25434370aa4aSLai Jiangshan num = get_arg(unsigned int); 2544fef20d9cSFrederic Weisbecker break; 2545fef20d9cSFrederic Weisbecker default: 2546fef20d9cSFrederic Weisbecker num = get_arg(int); 25474370aa4aSLai Jiangshan } 2548fef20d9cSFrederic Weisbecker 2549fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 2550d4be151bSAndré Goddard Rosa } /* default: */ 2551d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 2552d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 2553fef20d9cSFrederic Weisbecker 2554b006f19bSRasmus Villemoes out: 25554370aa4aSLai Jiangshan if (size > 0) { 25564370aa4aSLai Jiangshan if (str < end) 25574370aa4aSLai Jiangshan *str = '\0'; 25584370aa4aSLai Jiangshan else 25594370aa4aSLai Jiangshan end[-1] = '\0'; 25604370aa4aSLai Jiangshan } 2561fef20d9cSFrederic Weisbecker 25624370aa4aSLai Jiangshan #undef get_arg 25634370aa4aSLai Jiangshan 25644370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 25654370aa4aSLai Jiangshan return str - buf; 25664370aa4aSLai Jiangshan } 25674370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 25684370aa4aSLai Jiangshan 25694370aa4aSLai Jiangshan /** 25704370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 25714370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 25724370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 25734370aa4aSLai Jiangshan * @fmt: The format string to use 25744370aa4aSLai Jiangshan * @...: Arguments for the format string 25754370aa4aSLai Jiangshan * 25764370aa4aSLai Jiangshan * The function returns the number of words(u32) written 25774370aa4aSLai Jiangshan * into @bin_buf. 25784370aa4aSLai Jiangshan */ 25794370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 25804370aa4aSLai Jiangshan { 25814370aa4aSLai Jiangshan va_list args; 25824370aa4aSLai Jiangshan int ret; 25834370aa4aSLai Jiangshan 25844370aa4aSLai Jiangshan va_start(args, fmt); 25854370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 25864370aa4aSLai Jiangshan va_end(args); 25877b9186f5SAndré Goddard Rosa 25884370aa4aSLai Jiangshan return ret; 25894370aa4aSLai Jiangshan } 25904370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 25914370aa4aSLai Jiangshan 25924370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 25934370aa4aSLai Jiangshan 25941da177e4SLinus Torvalds /** 25951da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 25961da177e4SLinus Torvalds * @buf: input buffer 25971da177e4SLinus Torvalds * @fmt: format of buffer 25981da177e4SLinus Torvalds * @args: arguments 25991da177e4SLinus Torvalds */ 26001da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 26011da177e4SLinus Torvalds { 26021da177e4SLinus Torvalds const char *str = buf; 26031da177e4SLinus Torvalds char *next; 26041da177e4SLinus Torvalds char digit; 26051da177e4SLinus Torvalds int num = 0; 2606ef0658f3SJoe Perches u8 qualifier; 260753809751SJan Beulich unsigned int base; 260853809751SJan Beulich union { 260953809751SJan Beulich long long s; 261053809751SJan Beulich unsigned long long u; 261153809751SJan Beulich } val; 2612ef0658f3SJoe Perches s16 field_width; 2613d4be151bSAndré Goddard Rosa bool is_sign; 26141da177e4SLinus Torvalds 2615da99075cSJan Beulich while (*fmt) { 26161da177e4SLinus Torvalds /* skip any white space in format */ 26171da177e4SLinus Torvalds /* white space in format matchs any amount of 26181da177e4SLinus Torvalds * white space, including none, in the input. 26191da177e4SLinus Torvalds */ 26201da177e4SLinus Torvalds if (isspace(*fmt)) { 2621e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 2622e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 26261da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 26271da177e4SLinus Torvalds if (*fmt++ != *str++) 26281da177e4SLinus Torvalds break; 26291da177e4SLinus Torvalds continue; 26301da177e4SLinus Torvalds } 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds if (!*fmt) 26331da177e4SLinus Torvalds break; 26341da177e4SLinus Torvalds ++fmt; 26351da177e4SLinus Torvalds 26361da177e4SLinus Torvalds /* skip this conversion. 26371da177e4SLinus Torvalds * advance both strings to next white space 26381da177e4SLinus Torvalds */ 26391da177e4SLinus Torvalds if (*fmt == '*') { 2640da99075cSJan Beulich if (!*str) 2641da99075cSJan Beulich break; 2642f9310b2fSJessica Yu while (!isspace(*fmt) && *fmt != '%' && *fmt) { 2643f9310b2fSJessica Yu /* '%*[' not yet supported, invalid format */ 2644f9310b2fSJessica Yu if (*fmt == '[') 2645f9310b2fSJessica Yu return num; 26461da177e4SLinus Torvalds fmt++; 2647f9310b2fSJessica Yu } 26481da177e4SLinus Torvalds while (!isspace(*str) && *str) 26491da177e4SLinus Torvalds str++; 26501da177e4SLinus Torvalds continue; 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds /* get field width */ 26541da177e4SLinus Torvalds field_width = -1; 265553809751SJan Beulich if (isdigit(*fmt)) { 26561da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 265753809751SJan Beulich if (field_width <= 0) 265853809751SJan Beulich break; 265953809751SJan Beulich } 26601da177e4SLinus Torvalds 26611da177e4SLinus Torvalds /* get conversion qualifier */ 26621da177e4SLinus Torvalds qualifier = -1; 266375fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 26645b5e0928SAlexey Dobriyan *fmt == 'z') { 26651da177e4SLinus Torvalds qualifier = *fmt++; 26661da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 26671da177e4SLinus Torvalds if (qualifier == 'h') { 26681da177e4SLinus Torvalds qualifier = 'H'; 26691da177e4SLinus Torvalds fmt++; 26701da177e4SLinus Torvalds } else if (qualifier == 'l') { 26711da177e4SLinus Torvalds qualifier = 'L'; 26721da177e4SLinus Torvalds fmt++; 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds } 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 2677da99075cSJan Beulich if (!*fmt) 2678da99075cSJan Beulich break; 2679da99075cSJan Beulich 2680da99075cSJan Beulich if (*fmt == 'n') { 2681da99075cSJan Beulich /* return number of characters read so far */ 2682da99075cSJan Beulich *va_arg(args, int *) = str - buf; 2683da99075cSJan Beulich ++fmt; 2684da99075cSJan Beulich continue; 2685da99075cSJan Beulich } 2686da99075cSJan Beulich 2687da99075cSJan Beulich if (!*str) 26881da177e4SLinus Torvalds break; 26891da177e4SLinus Torvalds 2690d4be151bSAndré Goddard Rosa base = 10; 26913f623ebaSFabian Frederick is_sign = false; 2692d4be151bSAndré Goddard Rosa 26931da177e4SLinus Torvalds switch (*fmt++) { 26941da177e4SLinus Torvalds case 'c': 26951da177e4SLinus Torvalds { 26961da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 26971da177e4SLinus Torvalds if (field_width == -1) 26981da177e4SLinus Torvalds field_width = 1; 26991da177e4SLinus Torvalds do { 27001da177e4SLinus Torvalds *s++ = *str++; 27011da177e4SLinus Torvalds } while (--field_width > 0 && *str); 27021da177e4SLinus Torvalds num++; 27031da177e4SLinus Torvalds } 27041da177e4SLinus Torvalds continue; 27051da177e4SLinus Torvalds case 's': 27061da177e4SLinus Torvalds { 27071da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 27081da177e4SLinus Torvalds if (field_width == -1) 27094be929beSAlexey Dobriyan field_width = SHRT_MAX; 27101da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 2711e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds /* now copy until next white space */ 27147b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 27151da177e4SLinus Torvalds *s++ = *str++; 27161da177e4SLinus Torvalds *s = '\0'; 27171da177e4SLinus Torvalds num++; 27181da177e4SLinus Torvalds } 27191da177e4SLinus Torvalds continue; 2720f9310b2fSJessica Yu /* 2721f9310b2fSJessica Yu * Warning: This implementation of the '[' conversion specifier 2722f9310b2fSJessica Yu * deviates from its glibc counterpart in the following ways: 2723f9310b2fSJessica Yu * (1) It does NOT support ranges i.e. '-' is NOT a special 2724f9310b2fSJessica Yu * character 2725f9310b2fSJessica Yu * (2) It cannot match the closing bracket ']' itself 2726f9310b2fSJessica Yu * (3) A field width is required 2727f9310b2fSJessica Yu * (4) '%*[' (discard matching input) is currently not supported 2728f9310b2fSJessica Yu * 2729f9310b2fSJessica Yu * Example usage: 2730f9310b2fSJessica Yu * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 2731f9310b2fSJessica Yu * buf1, buf2, buf3); 2732f9310b2fSJessica Yu * if (ret < 3) 2733f9310b2fSJessica Yu * // etc.. 2734f9310b2fSJessica Yu */ 2735f9310b2fSJessica Yu case '[': 2736f9310b2fSJessica Yu { 2737f9310b2fSJessica Yu char *s = (char *)va_arg(args, char *); 2738f9310b2fSJessica Yu DECLARE_BITMAP(set, 256) = {0}; 2739f9310b2fSJessica Yu unsigned int len = 0; 2740f9310b2fSJessica Yu bool negate = (*fmt == '^'); 2741f9310b2fSJessica Yu 2742f9310b2fSJessica Yu /* field width is required */ 2743f9310b2fSJessica Yu if (field_width == -1) 2744f9310b2fSJessica Yu return num; 2745f9310b2fSJessica Yu 2746f9310b2fSJessica Yu if (negate) 2747f9310b2fSJessica Yu ++fmt; 2748f9310b2fSJessica Yu 2749f9310b2fSJessica Yu for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 2750f9310b2fSJessica Yu set_bit((u8)*fmt, set); 2751f9310b2fSJessica Yu 2752f9310b2fSJessica Yu /* no ']' or no character set found */ 2753f9310b2fSJessica Yu if (!*fmt || !len) 2754f9310b2fSJessica Yu return num; 2755f9310b2fSJessica Yu ++fmt; 2756f9310b2fSJessica Yu 2757f9310b2fSJessica Yu if (negate) { 2758f9310b2fSJessica Yu bitmap_complement(set, set, 256); 2759f9310b2fSJessica Yu /* exclude null '\0' byte */ 2760f9310b2fSJessica Yu clear_bit(0, set); 2761f9310b2fSJessica Yu } 2762f9310b2fSJessica Yu 2763f9310b2fSJessica Yu /* match must be non-empty */ 2764f9310b2fSJessica Yu if (!test_bit((u8)*str, set)) 2765f9310b2fSJessica Yu return num; 2766f9310b2fSJessica Yu 2767f9310b2fSJessica Yu while (test_bit((u8)*str, set) && field_width--) 2768f9310b2fSJessica Yu *s++ = *str++; 2769f9310b2fSJessica Yu *s = '\0'; 2770f9310b2fSJessica Yu ++num; 2771f9310b2fSJessica Yu } 2772f9310b2fSJessica Yu continue; 27731da177e4SLinus Torvalds case 'o': 27741da177e4SLinus Torvalds base = 8; 27751da177e4SLinus Torvalds break; 27761da177e4SLinus Torvalds case 'x': 27771da177e4SLinus Torvalds case 'X': 27781da177e4SLinus Torvalds base = 16; 27791da177e4SLinus Torvalds break; 27801da177e4SLinus Torvalds case 'i': 27811da177e4SLinus Torvalds base = 0; 27821da177e4SLinus Torvalds case 'd': 27833f623ebaSFabian Frederick is_sign = true; 27841da177e4SLinus Torvalds case 'u': 27851da177e4SLinus Torvalds break; 27861da177e4SLinus Torvalds case '%': 27871da177e4SLinus Torvalds /* looking for '%' in str */ 27881da177e4SLinus Torvalds if (*str++ != '%') 27891da177e4SLinus Torvalds return num; 27901da177e4SLinus Torvalds continue; 27911da177e4SLinus Torvalds default: 27921da177e4SLinus Torvalds /* invalid format; stop here */ 27931da177e4SLinus Torvalds return num; 27941da177e4SLinus Torvalds } 27951da177e4SLinus Torvalds 27961da177e4SLinus Torvalds /* have some sort of integer conversion. 27971da177e4SLinus Torvalds * first, skip white space in buffer. 27981da177e4SLinus Torvalds */ 2799e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds digit = *str; 28021da177e4SLinus Torvalds if (is_sign && digit == '-') 28031da177e4SLinus Torvalds digit = *(str + 1); 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds if (!digit 28061da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 28071da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 28081da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 28091da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 28101da177e4SLinus Torvalds break; 28111da177e4SLinus Torvalds 281253809751SJan Beulich if (is_sign) 281353809751SJan Beulich val.s = qualifier != 'L' ? 281453809751SJan Beulich simple_strtol(str, &next, base) : 281553809751SJan Beulich simple_strtoll(str, &next, base); 281653809751SJan Beulich else 281753809751SJan Beulich val.u = qualifier != 'L' ? 281853809751SJan Beulich simple_strtoul(str, &next, base) : 281953809751SJan Beulich simple_strtoull(str, &next, base); 282053809751SJan Beulich 282153809751SJan Beulich if (field_width > 0 && next - str > field_width) { 282253809751SJan Beulich if (base == 0) 282353809751SJan Beulich _parse_integer_fixup_radix(str, &base); 282453809751SJan Beulich while (next - str > field_width) { 282553809751SJan Beulich if (is_sign) 282653809751SJan Beulich val.s = div_s64(val.s, base); 282753809751SJan Beulich else 282853809751SJan Beulich val.u = div_u64(val.u, base); 282953809751SJan Beulich --next; 283053809751SJan Beulich } 283153809751SJan Beulich } 283253809751SJan Beulich 28331da177e4SLinus Torvalds switch (qualifier) { 28341da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 283553809751SJan Beulich if (is_sign) 283653809751SJan Beulich *va_arg(args, signed char *) = val.s; 283753809751SJan Beulich else 283853809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 28391da177e4SLinus Torvalds break; 28401da177e4SLinus Torvalds case 'h': 284153809751SJan Beulich if (is_sign) 284253809751SJan Beulich *va_arg(args, short *) = val.s; 284353809751SJan Beulich else 284453809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 28451da177e4SLinus Torvalds break; 28461da177e4SLinus Torvalds case 'l': 284753809751SJan Beulich if (is_sign) 284853809751SJan Beulich *va_arg(args, long *) = val.s; 284953809751SJan Beulich else 285053809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 28511da177e4SLinus Torvalds break; 28521da177e4SLinus Torvalds case 'L': 285353809751SJan Beulich if (is_sign) 285453809751SJan Beulich *va_arg(args, long long *) = val.s; 285553809751SJan Beulich else 285653809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 28571da177e4SLinus Torvalds break; 28581da177e4SLinus Torvalds case 'z': 285953809751SJan Beulich *va_arg(args, size_t *) = val.u; 28601da177e4SLinus Torvalds break; 28611da177e4SLinus Torvalds default: 286253809751SJan Beulich if (is_sign) 286353809751SJan Beulich *va_arg(args, int *) = val.s; 286453809751SJan Beulich else 286553809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 28661da177e4SLinus Torvalds break; 28671da177e4SLinus Torvalds } 28681da177e4SLinus Torvalds num++; 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds if (!next) 28711da177e4SLinus Torvalds break; 28721da177e4SLinus Torvalds str = next; 28731da177e4SLinus Torvalds } 2874c6b40d16SJohannes Berg 28751da177e4SLinus Torvalds return num; 28761da177e4SLinus Torvalds } 28771da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 28781da177e4SLinus Torvalds 28791da177e4SLinus Torvalds /** 28801da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 28811da177e4SLinus Torvalds * @buf: input buffer 28821da177e4SLinus Torvalds * @fmt: formatting of buffer 28831da177e4SLinus Torvalds * @...: resulting arguments 28841da177e4SLinus Torvalds */ 28851da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 28861da177e4SLinus Torvalds { 28871da177e4SLinus Torvalds va_list args; 28881da177e4SLinus Torvalds int i; 28891da177e4SLinus Torvalds 28901da177e4SLinus Torvalds va_start(args, fmt); 28911da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 28921da177e4SLinus Torvalds va_end(args); 28937b9186f5SAndré Goddard Rosa 28941da177e4SLinus Torvalds return i; 28951da177e4SLinus Torvalds } 28961da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 2897