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> 20*0d1d7a55SStephen 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> 338a27f7c9SJoe Perches #include <net/addrconf.h> 341da177e4SLinus Torvalds 354e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 36deac93dfSJames Bottomley #include <asm/sections.h> /* for dereference_function_descriptor() */ 377c43d9a3SRasmus Villemoes #include <asm/byteorder.h> /* cpu_to_le16 */ 381da177e4SLinus Torvalds 3971dca95dSAndy Shevchenko #include <linux/string_helpers.h> 401dff46d6SAlexey Dobriyan #include "kstrtox.h" 41aa46a63eSHarvey Harrison 421da177e4SLinus Torvalds /** 431da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 441da177e4SLinus Torvalds * @cp: The start of the string 451da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 461da177e4SLinus Torvalds * @base: The number base to use 47462e4711SEldad Zack * 48462e4711SEldad Zack * This function is obsolete. Please use kstrtoull instead. 491da177e4SLinus Torvalds */ 501da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 511da177e4SLinus Torvalds { 521dff46d6SAlexey Dobriyan unsigned long long result; 531dff46d6SAlexey Dobriyan unsigned int rv; 541da177e4SLinus Torvalds 551dff46d6SAlexey Dobriyan cp = _parse_integer_fixup_radix(cp, &base); 561dff46d6SAlexey Dobriyan rv = _parse_integer(cp, base, &result); 571dff46d6SAlexey Dobriyan /* FIXME */ 581dff46d6SAlexey Dobriyan cp += (rv & ~KSTRTOX_OVERFLOW); 59aa46a63eSHarvey Harrison 601da177e4SLinus Torvalds if (endp) 611da177e4SLinus Torvalds *endp = (char *)cp; 627b9186f5SAndré Goddard Rosa 631da177e4SLinus Torvalds return result; 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /** 68922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 69922ac25cSAndré Goddard Rosa * @cp: The start of the string 70922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 71922ac25cSAndré Goddard Rosa * @base: The number base to use 72462e4711SEldad Zack * 73462e4711SEldad Zack * This function is obsolete. Please use kstrtoul instead. 74922ac25cSAndré Goddard Rosa */ 75922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 76922ac25cSAndré Goddard Rosa { 77922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 78922ac25cSAndré Goddard Rosa } 79922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 80922ac25cSAndré Goddard Rosa 81922ac25cSAndré Goddard Rosa /** 82922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 83922ac25cSAndré Goddard Rosa * @cp: The start of the string 84922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 85922ac25cSAndré Goddard Rosa * @base: The number base to use 86462e4711SEldad Zack * 87462e4711SEldad Zack * This function is obsolete. Please use kstrtol instead. 88922ac25cSAndré Goddard Rosa */ 89922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 90922ac25cSAndré Goddard Rosa { 91922ac25cSAndré Goddard Rosa if (*cp == '-') 92922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 93922ac25cSAndré Goddard Rosa 94922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 95922ac25cSAndré Goddard Rosa } 96922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 97922ac25cSAndré Goddard Rosa 98922ac25cSAndré Goddard Rosa /** 991da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1001da177e4SLinus Torvalds * @cp: The start of the string 1011da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1021da177e4SLinus Torvalds * @base: The number base to use 103462e4711SEldad Zack * 104462e4711SEldad Zack * This function is obsolete. Please use kstrtoll instead. 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds if (*cp == '-') 1091da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1107b9186f5SAndré Goddard Rosa 1111da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1121da177e4SLinus Torvalds } 11398d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1141da177e4SLinus Torvalds 115cf3b429bSJoe Perches static noinline_for_stack 116cf3b429bSJoe Perches int skip_atoi(const char **s) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds int i = 0; 1191da177e4SLinus Torvalds 12043e5b666SRasmus Villemoes do { 1211da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 12243e5b666SRasmus Villemoes } while (isdigit(**s)); 1237b9186f5SAndré Goddard Rosa 1241da177e4SLinus Torvalds return i; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 1277c43d9a3SRasmus Villemoes /* 1287c43d9a3SRasmus Villemoes * Decimal conversion is by far the most typical, and is used for 1297c43d9a3SRasmus Villemoes * /proc and /sys data. This directly impacts e.g. top performance 1307c43d9a3SRasmus Villemoes * with many processes running. We optimize it for speed by emitting 1317c43d9a3SRasmus Villemoes * two characters at a time, using a 200 byte lookup table. This 1327c43d9a3SRasmus Villemoes * roughly halves the number of multiplications compared to computing 1337c43d9a3SRasmus Villemoes * the digits one at a time. Implementation strongly inspired by the 1347c43d9a3SRasmus Villemoes * previous version, which in turn used ideas described at 1357c43d9a3SRasmus Villemoes * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 1367c43d9a3SRasmus Villemoes * from the author, Douglas W. Jones). 1377c43d9a3SRasmus Villemoes * 1387c43d9a3SRasmus Villemoes * It turns out there is precisely one 26 bit fixed-point 1397c43d9a3SRasmus Villemoes * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 1407c43d9a3SRasmus Villemoes * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 1417c43d9a3SRasmus Villemoes * range happens to be somewhat larger (x <= 1073741898), but that's 1427c43d9a3SRasmus Villemoes * irrelevant for our purpose. 1437c43d9a3SRasmus Villemoes * 1447c43d9a3SRasmus Villemoes * For dividing a number in the range [10^4, 10^6-1] by 100, we still 1457c43d9a3SRasmus Villemoes * need a 32x32->64 bit multiply, so we simply use the same constant. 1467c43d9a3SRasmus Villemoes * 1477c43d9a3SRasmus Villemoes * For dividing a number in the range [100, 10^4-1] by 100, there are 1487c43d9a3SRasmus Villemoes * several options. The simplest is (x * 0x147b) >> 19, which is valid 1497c43d9a3SRasmus Villemoes * for all x <= 43698. 150133fd9f5SDenys Vlasenko */ 1514277eeddSDenis Vlasenko 1527c43d9a3SRasmus Villemoes static const u16 decpair[100] = { 1537c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 1547c43d9a3SRasmus Villemoes _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 1557c43d9a3SRasmus Villemoes _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 1567c43d9a3SRasmus Villemoes _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 1577c43d9a3SRasmus Villemoes _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 1587c43d9a3SRasmus Villemoes _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 1597c43d9a3SRasmus Villemoes _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 1607c43d9a3SRasmus Villemoes _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 1617c43d9a3SRasmus Villemoes _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 1627c43d9a3SRasmus Villemoes _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 1637c43d9a3SRasmus Villemoes _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 1647c43d9a3SRasmus Villemoes #undef _ 1657c43d9a3SRasmus Villemoes }; 1664277eeddSDenis Vlasenko 1677b9186f5SAndré Goddard Rosa /* 1687c43d9a3SRasmus Villemoes * This will print a single '0' even if r == 0, since we would 169675cf53cSRasmus Villemoes * immediately jump to out_r where two 0s would be written but only 170675cf53cSRasmus Villemoes * one of them accounted for in buf. This is needed by ip4_string 171675cf53cSRasmus Villemoes * below. All other callers pass a non-zero value of r. 172133fd9f5SDenys Vlasenko */ 173133fd9f5SDenys Vlasenko static noinline_for_stack 174133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 175133fd9f5SDenys Vlasenko { 176133fd9f5SDenys Vlasenko unsigned q; 1774277eeddSDenis Vlasenko 1787c43d9a3SRasmus Villemoes /* 1 <= r < 10^8 */ 1797c43d9a3SRasmus Villemoes if (r < 100) 1807c43d9a3SRasmus Villemoes goto out_r; 181cb239d0aSGeorge Spelvin 1827c43d9a3SRasmus Villemoes /* 100 <= r < 10^8 */ 1837c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 1847c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 1857c43d9a3SRasmus Villemoes buf += 2; 1867c43d9a3SRasmus Villemoes 1877c43d9a3SRasmus Villemoes /* 1 <= q < 10^6 */ 1887c43d9a3SRasmus Villemoes if (q < 100) 1897c43d9a3SRasmus Villemoes goto out_q; 1907c43d9a3SRasmus Villemoes 1917c43d9a3SRasmus Villemoes /* 100 <= q < 10^6 */ 1927c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 1937c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 1947c43d9a3SRasmus Villemoes buf += 2; 1957c43d9a3SRasmus Villemoes 1967c43d9a3SRasmus Villemoes /* 1 <= r < 10^4 */ 1977c43d9a3SRasmus Villemoes if (r < 100) 1987c43d9a3SRasmus Villemoes goto out_r; 1997c43d9a3SRasmus Villemoes 2007c43d9a3SRasmus Villemoes /* 100 <= r < 10^4 */ 2017c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2027c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2037c43d9a3SRasmus Villemoes buf += 2; 2047c43d9a3SRasmus Villemoes out_q: 2057c43d9a3SRasmus Villemoes /* 1 <= q < 100 */ 2067c43d9a3SRasmus Villemoes r = q; 2077c43d9a3SRasmus Villemoes out_r: 2087c43d9a3SRasmus Villemoes /* 1 <= r < 100 */ 2097c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r]; 210675cf53cSRasmus Villemoes buf += r < 10 ? 1 : 2; 211133fd9f5SDenys Vlasenko return buf; 212133fd9f5SDenys Vlasenko } 213133fd9f5SDenys Vlasenko 2147c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 2157c43d9a3SRasmus Villemoes static noinline_for_stack 2167c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r) 2177c43d9a3SRasmus Villemoes { 2187c43d9a3SRasmus Villemoes unsigned q; 219133fd9f5SDenys Vlasenko 2207c43d9a3SRasmus Villemoes /* 0 <= r < 10^8 */ 2217c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2227c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2237c43d9a3SRasmus Villemoes buf += 2; 224133fd9f5SDenys Vlasenko 2257c43d9a3SRasmus Villemoes /* 0 <= q < 10^6 */ 2267c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2277c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2287c43d9a3SRasmus Villemoes buf += 2; 229133fd9f5SDenys Vlasenko 2307c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2317c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2327c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2337c43d9a3SRasmus Villemoes buf += 2; 2347c43d9a3SRasmus Villemoes 2357c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2367c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2377c43d9a3SRasmus Villemoes buf += 2; 2387c43d9a3SRasmus Villemoes return buf; 2397c43d9a3SRasmus Villemoes } 2407c43d9a3SRasmus Villemoes 2417c43d9a3SRasmus Villemoes static noinline_for_stack 242133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 243133fd9f5SDenys Vlasenko { 244133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 2457c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2467c43d9a3SRasmus Villemoes /* 1 <= n <= 1.6e11 */ 2477c43d9a3SRasmus Villemoes if (n >= 100*1000*1000) 2487c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2497c43d9a3SRasmus Villemoes /* 1 <= n < 1e8 */ 250133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 251133fd9f5SDenys Vlasenko } 252133fd9f5SDenys Vlasenko 2537c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 254133fd9f5SDenys Vlasenko 2557c43d9a3SRasmus Villemoes static void 2567c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r) 257133fd9f5SDenys Vlasenko { 2587c43d9a3SRasmus Villemoes unsigned q; 2597c43d9a3SRasmus Villemoes 2607c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2617c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2627c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2637c43d9a3SRasmus Villemoes buf += 2; 2647c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2657c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2662359172aSGeorge Spelvin } 2672359172aSGeorge Spelvin 2682359172aSGeorge Spelvin /* 2692359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 2702359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 2712359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 2722359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 2737c43d9a3SRasmus Villemoes * (second call in the put_dec code, assuming n is all-ones). 2742359172aSGeorge Spelvin */ 2757c43d9a3SRasmus Villemoes static noinline_for_stack 2762359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 2772359172aSGeorge Spelvin { 2782359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 2792359172aSGeorge Spelvin 2802359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 2812359172aSGeorge Spelvin return q; 282133fd9f5SDenys Vlasenko } 283133fd9f5SDenys Vlasenko 284133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 285133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 286133fd9f5SDenys Vlasenko * (with permission from the author). 287133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 288133fd9f5SDenys Vlasenko */ 289133fd9f5SDenys Vlasenko static 290133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 291133fd9f5SDenys Vlasenko { 292133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 293133fd9f5SDenys Vlasenko 294133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 295133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 296133fd9f5SDenys Vlasenko 297133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 298133fd9f5SDenys Vlasenko h = (n >> 32); 299133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 300133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 301133fd9f5SDenys Vlasenko 3027c43d9a3SRasmus Villemoes /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 3037c43d9a3SRasmus Villemoes = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 304133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 3052359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 306133fd9f5SDenys Vlasenko 3072359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 3082359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 309133fd9f5SDenys Vlasenko 3102359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3112359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 312133fd9f5SDenys Vlasenko 3132359172aSGeorge Spelvin q += 281 * d3; 3142359172aSGeorge Spelvin buf += 12; 3152359172aSGeorge Spelvin if (q) 3162359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3172359172aSGeorge Spelvin else while (buf[-1] == '0') 318133fd9f5SDenys Vlasenko --buf; 3197b9186f5SAndré Goddard Rosa 3204277eeddSDenis Vlasenko return buf; 3214277eeddSDenis Vlasenko } 322133fd9f5SDenys Vlasenko 323133fd9f5SDenys Vlasenko #endif 3244277eeddSDenis Vlasenko 3251ac101a5SKAMEZAWA Hiroyuki /* 3261ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3271ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3281ac101a5SKAMEZAWA Hiroyuki * 3291ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3301ac101a5SKAMEZAWA Hiroyuki */ 3311ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num) 3321ac101a5SKAMEZAWA Hiroyuki { 3337c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 3347c43d9a3SRasmus Villemoes char tmp[sizeof(num) * 3] __aligned(2); 3351ac101a5SKAMEZAWA Hiroyuki int idx, len; 3361ac101a5SKAMEZAWA Hiroyuki 337133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 338133fd9f5SDenys Vlasenko if (num <= 9) { 339133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 340133fd9f5SDenys Vlasenko len = 1; 341133fd9f5SDenys Vlasenko } else { 3421ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 343133fd9f5SDenys Vlasenko } 3441ac101a5SKAMEZAWA Hiroyuki 3451ac101a5SKAMEZAWA Hiroyuki if (len > size) 3461ac101a5SKAMEZAWA Hiroyuki return 0; 3471ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 3481ac101a5SKAMEZAWA Hiroyuki buf[idx] = tmp[len - idx - 1]; 3491ac101a5SKAMEZAWA Hiroyuki return len; 3501ac101a5SKAMEZAWA Hiroyuki } 3511ac101a5SKAMEZAWA Hiroyuki 35251be17dfSRasmus Villemoes #define SIGN 1 /* unsigned/signed, must be 1 */ 353d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */ 3541da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3551da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 356d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 357b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 358b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 3591da177e4SLinus Torvalds 360fef20d9cSFrederic Weisbecker enum format_type { 361fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 362ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 363fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 364fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 365fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 366fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 367fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 368fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 369fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 370fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 371fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 372a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 373a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 374fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 375fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 376fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 377fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 378fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 379fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 380fef20d9cSFrederic Weisbecker }; 381fef20d9cSFrederic Weisbecker 382fef20d9cSFrederic Weisbecker struct printf_spec { 3834e310fdaSJoe Perches u8 type; /* format_type enum */ 384ef0658f3SJoe Perches u8 flags; /* flags to number() */ 3854e310fdaSJoe Perches u8 base; /* number base, 8, 10 or 16 only */ 3864e310fdaSJoe Perches u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ 3874e310fdaSJoe Perches s16 field_width; /* width of output field */ 3884e310fdaSJoe Perches s16 precision; /* # of digits/chars */ 389fef20d9cSFrederic Weisbecker }; 390fef20d9cSFrederic Weisbecker 391cf3b429bSJoe Perches static noinline_for_stack 392cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 393fef20d9cSFrederic Weisbecker struct printf_spec spec) 3941da177e4SLinus Torvalds { 3957c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 3967c43d9a3SRasmus Villemoes char tmp[3 * sizeof(num)] __aligned(2); 3979b706aeeSDenys Vlasenko char sign; 3989b706aeeSDenys Vlasenko char locase; 399fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4001da177e4SLinus Torvalds int i; 4017c203422SPierre Carrier bool is_zero = num == 0LL; 4021da177e4SLinus Torvalds 4039b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4049b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 405fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 406fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 407fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4081da177e4SLinus Torvalds sign = 0; 409fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4101da177e4SLinus Torvalds if ((signed long long)num < 0) { 4111da177e4SLinus Torvalds sign = '-'; 4121da177e4SLinus Torvalds num = -(signed long long)num; 413fef20d9cSFrederic Weisbecker spec.field_width--; 414fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4151da177e4SLinus Torvalds sign = '+'; 416fef20d9cSFrederic Weisbecker spec.field_width--; 417fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4181da177e4SLinus Torvalds sign = ' '; 419fef20d9cSFrederic Weisbecker spec.field_width--; 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds } 422b39a7340SDenis Vlasenko if (need_pfx) { 423fef20d9cSFrederic Weisbecker if (spec.base == 16) 4247c203422SPierre Carrier spec.field_width -= 2; 4257c203422SPierre Carrier else if (!is_zero) 426fef20d9cSFrederic Weisbecker spec.field_width--; 4271da177e4SLinus Torvalds } 428b39a7340SDenis Vlasenko 429b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4301da177e4SLinus Torvalds i = 0; 431133fd9f5SDenys Vlasenko if (num < spec.base) 4323ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase; 433fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 434fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 435b39a7340SDenis Vlasenko int shift = 3; 4367b9186f5SAndré Goddard Rosa 4377b9186f5SAndré Goddard Rosa if (spec.base == 16) 4387b9186f5SAndré Goddard Rosa shift = 4; 439b39a7340SDenis Vlasenko do { 4403ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 441b39a7340SDenis Vlasenko num >>= shift; 442b39a7340SDenis Vlasenko } while (num); 4434277eeddSDenis Vlasenko } else { /* base 10 */ 4444277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4454277eeddSDenis Vlasenko } 446b39a7340SDenis Vlasenko 447b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 448fef20d9cSFrederic Weisbecker if (i > spec.precision) 449fef20d9cSFrederic Weisbecker spec.precision = i; 450b39a7340SDenis Vlasenko /* leading space padding */ 451fef20d9cSFrederic Weisbecker spec.field_width -= spec.precision; 45251be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) { 453fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 454f796937aSJeremy Fitzhardinge if (buf < end) 4551da177e4SLinus Torvalds *buf = ' '; 4561da177e4SLinus Torvalds ++buf; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds } 459b39a7340SDenis Vlasenko /* sign */ 4601da177e4SLinus Torvalds if (sign) { 461f796937aSJeremy Fitzhardinge if (buf < end) 4621da177e4SLinus Torvalds *buf = sign; 4631da177e4SLinus Torvalds ++buf; 4641da177e4SLinus Torvalds } 465b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 466b39a7340SDenis Vlasenko if (need_pfx) { 4677c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 468f796937aSJeremy Fitzhardinge if (buf < end) 4691da177e4SLinus Torvalds *buf = '0'; 4701da177e4SLinus Torvalds ++buf; 4717c203422SPierre Carrier } 472fef20d9cSFrederic Weisbecker if (spec.base == 16) { 473f796937aSJeremy Fitzhardinge if (buf < end) 4749b706aeeSDenys Vlasenko *buf = ('X' | locase); 4751da177e4SLinus Torvalds ++buf; 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds } 478b39a7340SDenis Vlasenko /* zero or space padding */ 479fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 480d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD); 481d1c1b121SRasmus Villemoes BUILD_BUG_ON(' ' + ZEROPAD != '0'); 482fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 483f796937aSJeremy Fitzhardinge if (buf < end) 4841da177e4SLinus Torvalds *buf = c; 4851da177e4SLinus Torvalds ++buf; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds } 488b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 489fef20d9cSFrederic Weisbecker while (i <= --spec.precision) { 490f796937aSJeremy Fitzhardinge if (buf < end) 4911da177e4SLinus Torvalds *buf = '0'; 4921da177e4SLinus Torvalds ++buf; 4931da177e4SLinus Torvalds } 494b39a7340SDenis Vlasenko /* actual digits of result */ 495b39a7340SDenis Vlasenko while (--i >= 0) { 496f796937aSJeremy Fitzhardinge if (buf < end) 4971da177e4SLinus Torvalds *buf = tmp[i]; 4981da177e4SLinus Torvalds ++buf; 4991da177e4SLinus Torvalds } 500b39a7340SDenis Vlasenko /* trailing space padding */ 501fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 502f796937aSJeremy Fitzhardinge if (buf < end) 5031da177e4SLinus Torvalds *buf = ' '; 5041da177e4SLinus Torvalds ++buf; 5051da177e4SLinus Torvalds } 5067b9186f5SAndré Goddard Rosa 5071da177e4SLinus Torvalds return buf; 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 510cf3b429bSJoe Perches static noinline_for_stack 511cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec) 5120f9bfa56SLinus Torvalds { 5130f9bfa56SLinus Torvalds int len, i; 5140f9bfa56SLinus Torvalds 5150f9bfa56SLinus Torvalds if ((unsigned long)s < PAGE_SIZE) 5160f4f81dcSAndré Goddard Rosa s = "(null)"; 5170f9bfa56SLinus Torvalds 518fef20d9cSFrederic Weisbecker len = strnlen(s, spec.precision); 5190f9bfa56SLinus Torvalds 520fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 521fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5220f9bfa56SLinus Torvalds if (buf < end) 5230f9bfa56SLinus Torvalds *buf = ' '; 5240f9bfa56SLinus Torvalds ++buf; 5250f9bfa56SLinus Torvalds } 5260f9bfa56SLinus Torvalds } 5270f9bfa56SLinus Torvalds for (i = 0; i < len; ++i) { 5280f9bfa56SLinus Torvalds if (buf < end) 5290f9bfa56SLinus Torvalds *buf = *s; 5300f9bfa56SLinus Torvalds ++buf; ++s; 5310f9bfa56SLinus Torvalds } 532fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5330f9bfa56SLinus Torvalds if (buf < end) 5340f9bfa56SLinus Torvalds *buf = ' '; 5350f9bfa56SLinus Torvalds ++buf; 5360f9bfa56SLinus Torvalds } 5377b9186f5SAndré Goddard Rosa 5380f9bfa56SLinus Torvalds return buf; 5390f9bfa56SLinus Torvalds } 5400f9bfa56SLinus Torvalds 5414b6ccca7SAl Viro static void widen(char *buf, char *end, unsigned len, unsigned spaces) 5424b6ccca7SAl Viro { 5434b6ccca7SAl Viro size_t size; 5444b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5454b6ccca7SAl Viro return; 5464b6ccca7SAl Viro size = end - buf; 5474b6ccca7SAl Viro if (size <= spaces) { 5484b6ccca7SAl Viro memset(buf, ' ', size); 5494b6ccca7SAl Viro return; 5504b6ccca7SAl Viro } 5514b6ccca7SAl Viro if (len) { 5524b6ccca7SAl Viro if (len > size - spaces) 5534b6ccca7SAl Viro len = size - spaces; 5544b6ccca7SAl Viro memmove(buf + spaces, buf, len); 5554b6ccca7SAl Viro } 5564b6ccca7SAl Viro memset(buf, ' ', spaces); 5574b6ccca7SAl Viro } 5584b6ccca7SAl Viro 5594b6ccca7SAl Viro static noinline_for_stack 5604b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 5614b6ccca7SAl Viro const char *fmt) 5624b6ccca7SAl Viro { 5634b6ccca7SAl Viro const char *array[4], *s; 5644b6ccca7SAl Viro const struct dentry *p; 5654b6ccca7SAl Viro int depth; 5664b6ccca7SAl Viro int i, n; 5674b6ccca7SAl Viro 5684b6ccca7SAl Viro switch (fmt[1]) { 5694b6ccca7SAl Viro case '2': case '3': case '4': 5704b6ccca7SAl Viro depth = fmt[1] - '0'; 5714b6ccca7SAl Viro break; 5724b6ccca7SAl Viro default: 5734b6ccca7SAl Viro depth = 1; 5744b6ccca7SAl Viro } 5754b6ccca7SAl Viro 5764b6ccca7SAl Viro rcu_read_lock(); 5774b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 5784b6ccca7SAl Viro p = ACCESS_ONCE(d->d_parent); 5794b6ccca7SAl Viro array[i] = ACCESS_ONCE(d->d_name.name); 5804b6ccca7SAl Viro if (p == d) { 5814b6ccca7SAl Viro if (i) 5824b6ccca7SAl Viro array[i] = ""; 5834b6ccca7SAl Viro i++; 5844b6ccca7SAl Viro break; 5854b6ccca7SAl Viro } 5864b6ccca7SAl Viro } 5874b6ccca7SAl Viro s = array[--i]; 5884b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 5894b6ccca7SAl Viro char c = *s++; 5904b6ccca7SAl Viro if (!c) { 5914b6ccca7SAl Viro if (!i) 5924b6ccca7SAl Viro break; 5934b6ccca7SAl Viro c = '/'; 5944b6ccca7SAl Viro s = array[--i]; 5954b6ccca7SAl Viro } 5964b6ccca7SAl Viro if (buf < end) 5974b6ccca7SAl Viro *buf = c; 5984b6ccca7SAl Viro } 5994b6ccca7SAl Viro rcu_read_unlock(); 6004b6ccca7SAl Viro if (n < spec.field_width) { 6014b6ccca7SAl Viro /* we want to pad the sucker */ 6024b6ccca7SAl Viro unsigned spaces = spec.field_width - n; 6034b6ccca7SAl Viro if (!(spec.flags & LEFT)) { 6044b6ccca7SAl Viro widen(buf - n, end, n, spaces); 6054b6ccca7SAl Viro return buf + spaces; 6064b6ccca7SAl Viro } 6074b6ccca7SAl Viro while (spaces--) { 6084b6ccca7SAl Viro if (buf < end) 6094b6ccca7SAl Viro *buf = ' '; 6104b6ccca7SAl Viro ++buf; 6114b6ccca7SAl Viro } 6124b6ccca7SAl Viro } 6134b6ccca7SAl Viro return buf; 6144b6ccca7SAl Viro } 6154b6ccca7SAl Viro 616cf3b429bSJoe Perches static noinline_for_stack 617cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 618b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 6190fe1ef24SLinus Torvalds { 620b0d33c2bSJoe Perches unsigned long value; 6210fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 6220fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 623b0d33c2bSJoe Perches #endif 624b0d33c2bSJoe Perches 625b0d33c2bSJoe Perches if (fmt[1] == 'R') 626b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 627b0d33c2bSJoe Perches value = (unsigned long)ptr; 628b0d33c2bSJoe Perches 629b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 630b0d33c2bSJoe Perches if (*fmt == 'B') 6310f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 632b0d33c2bSJoe Perches else if (*fmt != 'f' && *fmt != 's') 6330fe1ef24SLinus Torvalds sprint_symbol(sym, value); 6340c8b946eSFrederic Weisbecker else 6354796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 6367b9186f5SAndré Goddard Rosa 637fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 6380fe1ef24SLinus Torvalds #else 639fef20d9cSFrederic Weisbecker spec.field_width = 2 * sizeof(void *); 640fef20d9cSFrederic Weisbecker spec.flags |= SPECIAL | SMALL | ZEROPAD; 641fef20d9cSFrederic Weisbecker spec.base = 16; 6427b9186f5SAndré Goddard Rosa 643fef20d9cSFrederic Weisbecker return number(buf, end, value, spec); 6440fe1ef24SLinus Torvalds #endif 6450fe1ef24SLinus Torvalds } 6460fe1ef24SLinus Torvalds 647cf3b429bSJoe Perches static noinline_for_stack 648cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 649fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 650332d2e78SLinus Torvalds { 651332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 65228405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 653332d2e78SLinus Torvalds #endif 654332d2e78SLinus Torvalds 655332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 65628405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 657332d2e78SLinus Torvalds #endif 6584da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 659fef20d9cSFrederic Weisbecker .base = 16, 6604da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 661fef20d9cSFrederic Weisbecker .precision = -1, 662fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 663fef20d9cSFrederic Weisbecker }; 6644da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 6654da0b66cSBjorn Helgaas .base = 16, 6664da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 6674da0b66cSBjorn Helgaas .precision = -1, 6684da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 6694da0b66cSBjorn Helgaas }; 6700f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 6710f4050c7SBjorn Helgaas .base = 16, 6720f4050c7SBjorn Helgaas .field_width = 2, 6730f4050c7SBjorn Helgaas .precision = -1, 6740f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 6750f4050c7SBjorn Helgaas }; 6764da0b66cSBjorn Helgaas static const struct printf_spec dec_spec = { 677c91d3376SBjorn Helgaas .base = 10, 678c91d3376SBjorn Helgaas .precision = -1, 679c91d3376SBjorn Helgaas .flags = 0, 680c91d3376SBjorn Helgaas }; 6814da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 682fd95541eSBjorn Helgaas .field_width = -1, 683fd95541eSBjorn Helgaas .precision = 10, 684fd95541eSBjorn Helgaas .flags = LEFT, 685fd95541eSBjorn Helgaas }; 6864da0b66cSBjorn Helgaas static const struct printf_spec flag_spec = { 687fd95541eSBjorn Helgaas .base = 16, 688fd95541eSBjorn Helgaas .precision = -1, 689fd95541eSBjorn Helgaas .flags = SPECIAL | SMALL, 690fd95541eSBjorn Helgaas }; 691c7dabef8SBjorn Helgaas 692c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 693c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 694c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 695c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 6969d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 697c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 698c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 699c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 700c7dabef8SBjorn Helgaas 701332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 702c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 7034da0b66cSBjorn Helgaas const struct printf_spec *specp; 704332d2e78SLinus Torvalds 705332d2e78SLinus Torvalds *p++ = '['; 7064da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 707fd95541eSBjorn Helgaas p = string(p, pend, "io ", str_spec); 7084da0b66cSBjorn Helgaas specp = &io_spec; 7094da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 710fd95541eSBjorn Helgaas p = string(p, pend, "mem ", str_spec); 7114da0b66cSBjorn Helgaas specp = &mem_spec; 7124da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 713fd95541eSBjorn Helgaas p = string(p, pend, "irq ", str_spec); 7144da0b66cSBjorn Helgaas specp = &dec_spec; 7154da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 716fd95541eSBjorn Helgaas p = string(p, pend, "dma ", str_spec); 7174da0b66cSBjorn Helgaas specp = &dec_spec; 7180f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 7190f4050c7SBjorn Helgaas p = string(p, pend, "bus ", str_spec); 7200f4050c7SBjorn Helgaas specp = &bus_spec; 7214da0b66cSBjorn Helgaas } else { 722c7dabef8SBjorn Helgaas p = string(p, pend, "??? ", str_spec); 7234da0b66cSBjorn Helgaas specp = &mem_spec; 724c7dabef8SBjorn Helgaas decode = 0; 725fd95541eSBjorn Helgaas } 726d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 727d19cb803SBjorn Helgaas p = string(p, pend, "size ", str_spec); 728d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 729d19cb803SBjorn Helgaas } else { 7304da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 731c91d3376SBjorn Helgaas if (res->start != res->end) { 732332d2e78SLinus Torvalds *p++ = '-'; 7334da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 734c91d3376SBjorn Helgaas } 735d19cb803SBjorn Helgaas } 736c7dabef8SBjorn Helgaas if (decode) { 737fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 738fd95541eSBjorn Helgaas p = string(p, pend, " 64bit", str_spec); 739fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 740fd95541eSBjorn Helgaas p = string(p, pend, " pref", str_spec); 7419d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 7429d7cca04SBjorn Helgaas p = string(p, pend, " window", str_spec); 743fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 744fd95541eSBjorn Helgaas p = string(p, pend, " disabled", str_spec); 745c7dabef8SBjorn Helgaas } else { 746fd95541eSBjorn Helgaas p = string(p, pend, " flags ", str_spec); 747c7dabef8SBjorn Helgaas p = number(p, pend, res->flags, flag_spec); 748fd95541eSBjorn Helgaas } 749332d2e78SLinus Torvalds *p++ = ']'; 750c7dabef8SBjorn Helgaas *p = '\0'; 751332d2e78SLinus Torvalds 752fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 753332d2e78SLinus Torvalds } 754332d2e78SLinus Torvalds 755cf3b429bSJoe Perches static noinline_for_stack 75631550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 75731550a16SAndy Shevchenko const char *fmt) 75831550a16SAndy Shevchenko { 759360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 76031550a16SAndy Shevchenko negative value, fallback to the default */ 76131550a16SAndy Shevchenko char separator; 76231550a16SAndy Shevchenko 76331550a16SAndy Shevchenko if (spec.field_width == 0) 76431550a16SAndy Shevchenko /* nothing to print */ 76531550a16SAndy Shevchenko return buf; 76631550a16SAndy Shevchenko 76731550a16SAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 76831550a16SAndy Shevchenko /* NULL pointer */ 76931550a16SAndy Shevchenko return string(buf, end, NULL, spec); 77031550a16SAndy Shevchenko 77131550a16SAndy Shevchenko switch (fmt[1]) { 77231550a16SAndy Shevchenko case 'C': 77331550a16SAndy Shevchenko separator = ':'; 77431550a16SAndy Shevchenko break; 77531550a16SAndy Shevchenko case 'D': 77631550a16SAndy Shevchenko separator = '-'; 77731550a16SAndy Shevchenko break; 77831550a16SAndy Shevchenko case 'N': 77931550a16SAndy Shevchenko separator = 0; 78031550a16SAndy Shevchenko break; 78131550a16SAndy Shevchenko default: 78231550a16SAndy Shevchenko separator = ' '; 78331550a16SAndy Shevchenko break; 78431550a16SAndy Shevchenko } 78531550a16SAndy Shevchenko 78631550a16SAndy Shevchenko if (spec.field_width > 0) 78731550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 78831550a16SAndy Shevchenko 7899c98f235SRasmus Villemoes for (i = 0; i < len; ++i) { 7909c98f235SRasmus Villemoes if (buf < end) 7919c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]); 7929c98f235SRasmus Villemoes ++buf; 7939c98f235SRasmus Villemoes if (buf < end) 7949c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]); 7959c98f235SRasmus Villemoes ++buf; 79631550a16SAndy Shevchenko 7979c98f235SRasmus Villemoes if (separator && i != len - 1) { 7989c98f235SRasmus Villemoes if (buf < end) 7999c98f235SRasmus Villemoes *buf = separator; 8009c98f235SRasmus Villemoes ++buf; 8019c98f235SRasmus Villemoes } 80231550a16SAndy Shevchenko } 80331550a16SAndy Shevchenko 80431550a16SAndy Shevchenko return buf; 80531550a16SAndy Shevchenko } 80631550a16SAndy Shevchenko 80731550a16SAndy Shevchenko static noinline_for_stack 808dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap, 809dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 810dbc760bcSTejun Heo { 811dbc760bcSTejun Heo const int CHUNKSZ = 32; 812dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 813dbc760bcSTejun Heo int i, chunksz; 814dbc760bcSTejun Heo bool first = true; 815dbc760bcSTejun Heo 816dbc760bcSTejun Heo /* reused to print numbers */ 817dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 818dbc760bcSTejun Heo 819dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1); 820dbc760bcSTejun Heo if (chunksz == 0) 821dbc760bcSTejun Heo chunksz = CHUNKSZ; 822dbc760bcSTejun Heo 823dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 824dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) { 825dbc760bcSTejun Heo u32 chunkmask, val; 826dbc760bcSTejun Heo int word, bit; 827dbc760bcSTejun Heo 828dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1); 829dbc760bcSTejun Heo word = i / BITS_PER_LONG; 830dbc760bcSTejun Heo bit = i % BITS_PER_LONG; 831dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask; 832dbc760bcSTejun Heo 833dbc760bcSTejun Heo if (!first) { 834dbc760bcSTejun Heo if (buf < end) 835dbc760bcSTejun Heo *buf = ','; 836dbc760bcSTejun Heo buf++; 837dbc760bcSTejun Heo } 838dbc760bcSTejun Heo first = false; 839dbc760bcSTejun Heo 840dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4); 841dbc760bcSTejun Heo buf = number(buf, end, val, spec); 842dbc760bcSTejun Heo 843dbc760bcSTejun Heo chunksz = CHUNKSZ; 844dbc760bcSTejun Heo } 845dbc760bcSTejun Heo return buf; 846dbc760bcSTejun Heo } 847dbc760bcSTejun Heo 848dbc760bcSTejun Heo static noinline_for_stack 849dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, 850dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 851dbc760bcSTejun Heo { 852dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 853dbc760bcSTejun Heo /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 854dbc760bcSTejun Heo int cur, rbot, rtop; 855dbc760bcSTejun Heo bool first = true; 856dbc760bcSTejun Heo 857dbc760bcSTejun Heo /* reused to print numbers */ 858dbc760bcSTejun Heo spec = (struct printf_spec){ .base = 10 }; 859dbc760bcSTejun Heo 860dbc760bcSTejun Heo rbot = cur = find_first_bit(bitmap, nr_bits); 861dbc760bcSTejun Heo while (cur < nr_bits) { 862dbc760bcSTejun Heo rtop = cur; 863dbc760bcSTejun Heo cur = find_next_bit(bitmap, nr_bits, cur + 1); 864dbc760bcSTejun Heo if (cur < nr_bits && cur <= rtop + 1) 865dbc760bcSTejun Heo continue; 866dbc760bcSTejun Heo 867dbc760bcSTejun Heo if (!first) { 868dbc760bcSTejun Heo if (buf < end) 869dbc760bcSTejun Heo *buf = ','; 870dbc760bcSTejun Heo buf++; 871dbc760bcSTejun Heo } 872dbc760bcSTejun Heo first = false; 873dbc760bcSTejun Heo 874dbc760bcSTejun Heo buf = number(buf, end, rbot, spec); 875dbc760bcSTejun Heo if (rbot < rtop) { 876dbc760bcSTejun Heo if (buf < end) 877dbc760bcSTejun Heo *buf = '-'; 878dbc760bcSTejun Heo buf++; 879dbc760bcSTejun Heo 880dbc760bcSTejun Heo buf = number(buf, end, rtop, spec); 881dbc760bcSTejun Heo } 882dbc760bcSTejun Heo 883dbc760bcSTejun Heo rbot = cur; 884dbc760bcSTejun Heo } 885dbc760bcSTejun Heo return buf; 886dbc760bcSTejun Heo } 887dbc760bcSTejun Heo 888dbc760bcSTejun Heo static noinline_for_stack 889cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 8908a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 891dd45c9cfSHarvey Harrison { 8928a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 893dd45c9cfSHarvey Harrison char *p = mac_addr; 894dd45c9cfSHarvey Harrison int i; 895bc7259a2SJoe Perches char separator; 89676597ff9SAndrei Emeltchenko bool reversed = false; 897bc7259a2SJoe Perches 89876597ff9SAndrei Emeltchenko switch (fmt[1]) { 89976597ff9SAndrei Emeltchenko case 'F': 900bc7259a2SJoe Perches separator = '-'; 90176597ff9SAndrei Emeltchenko break; 90276597ff9SAndrei Emeltchenko 90376597ff9SAndrei Emeltchenko case 'R': 90476597ff9SAndrei Emeltchenko reversed = true; 90576597ff9SAndrei Emeltchenko /* fall through */ 90676597ff9SAndrei Emeltchenko 90776597ff9SAndrei Emeltchenko default: 908bc7259a2SJoe Perches separator = ':'; 90976597ff9SAndrei Emeltchenko break; 910bc7259a2SJoe Perches } 911dd45c9cfSHarvey Harrison 912dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 91376597ff9SAndrei Emeltchenko if (reversed) 91476597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 91576597ff9SAndrei Emeltchenko else 91655036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 91776597ff9SAndrei Emeltchenko 9188a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 919bc7259a2SJoe Perches *p++ = separator; 920dd45c9cfSHarvey Harrison } 921dd45c9cfSHarvey Harrison *p = '\0'; 922dd45c9cfSHarvey Harrison 923fef20d9cSFrederic Weisbecker return string(buf, end, mac_addr, spec); 924dd45c9cfSHarvey Harrison } 925dd45c9cfSHarvey Harrison 926cf3b429bSJoe Perches static noinline_for_stack 927cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 928689afa7dSHarvey Harrison { 929689afa7dSHarvey Harrison int i; 9300159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 9310159f24eSJoe Perches int index; 9320159f24eSJoe Perches int step; 933689afa7dSHarvey Harrison 9340159f24eSJoe Perches switch (fmt[2]) { 9350159f24eSJoe Perches case 'h': 9360159f24eSJoe Perches #ifdef __BIG_ENDIAN 9370159f24eSJoe Perches index = 0; 9380159f24eSJoe Perches step = 1; 9390159f24eSJoe Perches #else 9400159f24eSJoe Perches index = 3; 9410159f24eSJoe Perches step = -1; 9420159f24eSJoe Perches #endif 9430159f24eSJoe Perches break; 9440159f24eSJoe Perches case 'l': 9450159f24eSJoe Perches index = 3; 9460159f24eSJoe Perches step = -1; 9470159f24eSJoe Perches break; 9480159f24eSJoe Perches case 'n': 9490159f24eSJoe Perches case 'b': 9500159f24eSJoe Perches default: 9510159f24eSJoe Perches index = 0; 9520159f24eSJoe Perches step = 1; 9530159f24eSJoe Perches break; 9540159f24eSJoe Perches } 9558a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 9567c43d9a3SRasmus Villemoes char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 957133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 9588a27f7c9SJoe Perches if (leading_zeros) { 9598a27f7c9SJoe Perches if (digits < 3) 9608a27f7c9SJoe Perches *p++ = '0'; 9618a27f7c9SJoe Perches if (digits < 2) 9628a27f7c9SJoe Perches *p++ = '0'; 9638a27f7c9SJoe Perches } 9648a27f7c9SJoe Perches /* reverse the digits in the quad */ 9658a27f7c9SJoe Perches while (digits--) 9668a27f7c9SJoe Perches *p++ = temp[digits]; 9678a27f7c9SJoe Perches if (i < 3) 9688a27f7c9SJoe Perches *p++ = '.'; 9690159f24eSJoe Perches index += step; 9708a27f7c9SJoe Perches } 9718a27f7c9SJoe Perches *p = '\0'; 9727b9186f5SAndré Goddard Rosa 9738a27f7c9SJoe Perches return p; 9748a27f7c9SJoe Perches } 9758a27f7c9SJoe Perches 976cf3b429bSJoe Perches static noinline_for_stack 977cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 9788a27f7c9SJoe Perches { 9797b9186f5SAndré Goddard Rosa int i, j, range; 9808a27f7c9SJoe Perches unsigned char zerolength[8]; 9818a27f7c9SJoe Perches int longest = 1; 9828a27f7c9SJoe Perches int colonpos = -1; 9838a27f7c9SJoe Perches u16 word; 9847b9186f5SAndré Goddard Rosa u8 hi, lo; 9858a27f7c9SJoe Perches bool needcolon = false; 986eb78cd26SJoe Perches bool useIPv4; 987eb78cd26SJoe Perches struct in6_addr in6; 988eb78cd26SJoe Perches 989eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 990eb78cd26SJoe Perches 991eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 9928a27f7c9SJoe Perches 9938a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 9948a27f7c9SJoe Perches 9958a27f7c9SJoe Perches if (useIPv4) 9968a27f7c9SJoe Perches range = 6; 9978a27f7c9SJoe Perches else 9988a27f7c9SJoe Perches range = 8; 9998a27f7c9SJoe Perches 10008a27f7c9SJoe Perches /* find position of longest 0 run */ 10018a27f7c9SJoe Perches for (i = 0; i < range; i++) { 10028a27f7c9SJoe Perches for (j = i; j < range; j++) { 1003eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 10048a27f7c9SJoe Perches break; 10058a27f7c9SJoe Perches zerolength[i]++; 10068a27f7c9SJoe Perches } 10078a27f7c9SJoe Perches } 10088a27f7c9SJoe Perches for (i = 0; i < range; i++) { 10098a27f7c9SJoe Perches if (zerolength[i] > longest) { 10108a27f7c9SJoe Perches longest = zerolength[i]; 10118a27f7c9SJoe Perches colonpos = i; 10128a27f7c9SJoe Perches } 10138a27f7c9SJoe Perches } 101429cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 101529cf519eSJoe Perches colonpos = -1; 10168a27f7c9SJoe Perches 10178a27f7c9SJoe Perches /* emit address */ 10188a27f7c9SJoe Perches for (i = 0; i < range; i++) { 10198a27f7c9SJoe Perches if (i == colonpos) { 10208a27f7c9SJoe Perches if (needcolon || i == 0) 10218a27f7c9SJoe Perches *p++ = ':'; 10228a27f7c9SJoe Perches *p++ = ':'; 10238a27f7c9SJoe Perches needcolon = false; 10248a27f7c9SJoe Perches i += longest - 1; 10258a27f7c9SJoe Perches continue; 10268a27f7c9SJoe Perches } 10278a27f7c9SJoe Perches if (needcolon) { 10288a27f7c9SJoe Perches *p++ = ':'; 10298a27f7c9SJoe Perches needcolon = false; 10308a27f7c9SJoe Perches } 10318a27f7c9SJoe Perches /* hex u16 without leading 0s */ 1032eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 10338a27f7c9SJoe Perches hi = word >> 8; 10348a27f7c9SJoe Perches lo = word & 0xff; 10358a27f7c9SJoe Perches if (hi) { 10368a27f7c9SJoe Perches if (hi > 0x0f) 103755036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 10388a27f7c9SJoe Perches else 10398a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 104055036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 10418a27f7c9SJoe Perches } 1042b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 104355036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 10448a27f7c9SJoe Perches else 10458a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 10468a27f7c9SJoe Perches needcolon = true; 10478a27f7c9SJoe Perches } 10488a27f7c9SJoe Perches 10498a27f7c9SJoe Perches if (useIPv4) { 10508a27f7c9SJoe Perches if (needcolon) 10518a27f7c9SJoe Perches *p++ = ':'; 10520159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 10538a27f7c9SJoe Perches } 10548a27f7c9SJoe Perches *p = '\0'; 10557b9186f5SAndré Goddard Rosa 10568a27f7c9SJoe Perches return p; 10578a27f7c9SJoe Perches } 10588a27f7c9SJoe Perches 1059cf3b429bSJoe Perches static noinline_for_stack 1060cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 10618a27f7c9SJoe Perches { 10628a27f7c9SJoe Perches int i; 10637b9186f5SAndré Goddard Rosa 1064689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 106555036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 106655036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 10678a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 1068689afa7dSHarvey Harrison *p++ = ':'; 1069689afa7dSHarvey Harrison } 1070689afa7dSHarvey Harrison *p = '\0'; 10717b9186f5SAndré Goddard Rosa 10728a27f7c9SJoe Perches return p; 10738a27f7c9SJoe Perches } 10748a27f7c9SJoe Perches 1075cf3b429bSJoe Perches static noinline_for_stack 1076cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 10778a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 10788a27f7c9SJoe Perches { 10798a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 10808a27f7c9SJoe Perches 10818a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 1082eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 10838a27f7c9SJoe Perches else 1084eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 1085689afa7dSHarvey Harrison 1086fef20d9cSFrederic Weisbecker return string(buf, end, ip6_addr, spec); 1087689afa7dSHarvey Harrison } 1088689afa7dSHarvey Harrison 1089cf3b429bSJoe Perches static noinline_for_stack 1090cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 10918a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 10924aa99606SHarvey Harrison { 10938a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 10944aa99606SHarvey Harrison 10950159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 10964aa99606SHarvey Harrison 1097fef20d9cSFrederic Weisbecker return string(buf, end, ip4_addr, spec); 10984aa99606SHarvey Harrison } 10994aa99606SHarvey Harrison 1100cf3b429bSJoe Perches static noinline_for_stack 110110679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 110210679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 110310679643SDaniel Borkmann { 110410679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 110510679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 110610679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 110710679643SDaniel Borkmann sizeof("%1234567890")]; 110810679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 110910679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 111010679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 111110679643SDaniel Borkmann u8 off = 0; 111210679643SDaniel Borkmann 111310679643SDaniel Borkmann fmt++; 111410679643SDaniel Borkmann while (isalpha(*++fmt)) { 111510679643SDaniel Borkmann switch (*fmt) { 111610679643SDaniel Borkmann case 'p': 111710679643SDaniel Borkmann have_p = true; 111810679643SDaniel Borkmann break; 111910679643SDaniel Borkmann case 'f': 112010679643SDaniel Borkmann have_f = true; 112110679643SDaniel Borkmann break; 112210679643SDaniel Borkmann case 's': 112310679643SDaniel Borkmann have_s = true; 112410679643SDaniel Borkmann break; 112510679643SDaniel Borkmann case 'c': 112610679643SDaniel Borkmann have_c = true; 112710679643SDaniel Borkmann break; 112810679643SDaniel Borkmann } 112910679643SDaniel Borkmann } 113010679643SDaniel Borkmann 113110679643SDaniel Borkmann if (have_p || have_s || have_f) { 113210679643SDaniel Borkmann *p = '['; 113310679643SDaniel Borkmann off = 1; 113410679643SDaniel Borkmann } 113510679643SDaniel Borkmann 113610679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 113710679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 113810679643SDaniel Borkmann else 113910679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 114010679643SDaniel Borkmann 114110679643SDaniel Borkmann if (have_p || have_s || have_f) 114210679643SDaniel Borkmann *p++ = ']'; 114310679643SDaniel Borkmann 114410679643SDaniel Borkmann if (have_p) { 114510679643SDaniel Borkmann *p++ = ':'; 114610679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 114710679643SDaniel Borkmann } 114810679643SDaniel Borkmann if (have_f) { 114910679643SDaniel Borkmann *p++ = '/'; 115010679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 115110679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 115210679643SDaniel Borkmann } 115310679643SDaniel Borkmann if (have_s) { 115410679643SDaniel Borkmann *p++ = '%'; 115510679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 115610679643SDaniel Borkmann } 115710679643SDaniel Borkmann *p = '\0'; 115810679643SDaniel Borkmann 115910679643SDaniel Borkmann return string(buf, end, ip6_addr, spec); 116010679643SDaniel Borkmann } 116110679643SDaniel Borkmann 116210679643SDaniel Borkmann static noinline_for_stack 116310679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 116410679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 116510679643SDaniel Borkmann { 116610679643SDaniel Borkmann bool have_p = false; 116710679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 116810679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 116910679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 117010679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 117110679643SDaniel Borkmann 117210679643SDaniel Borkmann fmt++; 117310679643SDaniel Borkmann while (isalpha(*++fmt)) { 117410679643SDaniel Borkmann switch (*fmt) { 117510679643SDaniel Borkmann case 'p': 117610679643SDaniel Borkmann have_p = true; 117710679643SDaniel Borkmann break; 117810679643SDaniel Borkmann case 'h': 117910679643SDaniel Borkmann case 'l': 118010679643SDaniel Borkmann case 'n': 118110679643SDaniel Borkmann case 'b': 118210679643SDaniel Borkmann fmt4[2] = *fmt; 118310679643SDaniel Borkmann break; 118410679643SDaniel Borkmann } 118510679643SDaniel Borkmann } 118610679643SDaniel Borkmann 118710679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 118810679643SDaniel Borkmann if (have_p) { 118910679643SDaniel Borkmann *p++ = ':'; 119010679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 119110679643SDaniel Borkmann } 119210679643SDaniel Borkmann *p = '\0'; 119310679643SDaniel Borkmann 119410679643SDaniel Borkmann return string(buf, end, ip4_addr, spec); 119510679643SDaniel Borkmann } 119610679643SDaniel Borkmann 119710679643SDaniel Borkmann static noinline_for_stack 119871dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 119971dca95dSAndy Shevchenko const char *fmt) 120071dca95dSAndy Shevchenko { 120171dca95dSAndy Shevchenko bool found = true; 120271dca95dSAndy Shevchenko int count = 1; 120371dca95dSAndy Shevchenko unsigned int flags = 0; 120471dca95dSAndy Shevchenko int len; 120571dca95dSAndy Shevchenko 120671dca95dSAndy Shevchenko if (spec.field_width == 0) 120771dca95dSAndy Shevchenko return buf; /* nothing to print */ 120871dca95dSAndy Shevchenko 120971dca95dSAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 121071dca95dSAndy Shevchenko return string(buf, end, NULL, spec); /* NULL pointer */ 121171dca95dSAndy Shevchenko 121271dca95dSAndy Shevchenko 121371dca95dSAndy Shevchenko do { 121471dca95dSAndy Shevchenko switch (fmt[count++]) { 121571dca95dSAndy Shevchenko case 'a': 121671dca95dSAndy Shevchenko flags |= ESCAPE_ANY; 121771dca95dSAndy Shevchenko break; 121871dca95dSAndy Shevchenko case 'c': 121971dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL; 122071dca95dSAndy Shevchenko break; 122171dca95dSAndy Shevchenko case 'h': 122271dca95dSAndy Shevchenko flags |= ESCAPE_HEX; 122371dca95dSAndy Shevchenko break; 122471dca95dSAndy Shevchenko case 'n': 122571dca95dSAndy Shevchenko flags |= ESCAPE_NULL; 122671dca95dSAndy Shevchenko break; 122771dca95dSAndy Shevchenko case 'o': 122871dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL; 122971dca95dSAndy Shevchenko break; 123071dca95dSAndy Shevchenko case 'p': 123171dca95dSAndy Shevchenko flags |= ESCAPE_NP; 123271dca95dSAndy Shevchenko break; 123371dca95dSAndy Shevchenko case 's': 123471dca95dSAndy Shevchenko flags |= ESCAPE_SPACE; 123571dca95dSAndy Shevchenko break; 123671dca95dSAndy Shevchenko default: 123771dca95dSAndy Shevchenko found = false; 123871dca95dSAndy Shevchenko break; 123971dca95dSAndy Shevchenko } 124071dca95dSAndy Shevchenko } while (found); 124171dca95dSAndy Shevchenko 124271dca95dSAndy Shevchenko if (!flags) 124371dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP; 124471dca95dSAndy Shevchenko 124571dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width; 124671dca95dSAndy Shevchenko 124741416f23SRasmus Villemoes /* 124841416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to 124941416f23SRasmus Villemoes * the given buffer, and returns the total size of the output 125041416f23SRasmus Villemoes * had the buffer been big enough. 125141416f23SRasmus Villemoes */ 125241416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 125371dca95dSAndy Shevchenko 125471dca95dSAndy Shevchenko return buf; 125571dca95dSAndy Shevchenko } 125671dca95dSAndy Shevchenko 125771dca95dSAndy Shevchenko static noinline_for_stack 1258cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 12599ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 12609ac6e44eSJoe Perches { 12619ac6e44eSJoe Perches char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; 12629ac6e44eSJoe Perches char *p = uuid; 12639ac6e44eSJoe Perches int i; 12649ac6e44eSJoe Perches static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 12659ac6e44eSJoe Perches static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; 12669ac6e44eSJoe Perches const u8 *index = be; 12679ac6e44eSJoe Perches bool uc = false; 12689ac6e44eSJoe Perches 12699ac6e44eSJoe Perches switch (*(++fmt)) { 12709ac6e44eSJoe Perches case 'L': 12719ac6e44eSJoe Perches uc = true; /* fall-through */ 12729ac6e44eSJoe Perches case 'l': 12739ac6e44eSJoe Perches index = le; 12749ac6e44eSJoe Perches break; 12759ac6e44eSJoe Perches case 'B': 12769ac6e44eSJoe Perches uc = true; 12779ac6e44eSJoe Perches break; 12789ac6e44eSJoe Perches } 12799ac6e44eSJoe Perches 12809ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 128155036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 12829ac6e44eSJoe Perches switch (i) { 12839ac6e44eSJoe Perches case 3: 12849ac6e44eSJoe Perches case 5: 12859ac6e44eSJoe Perches case 7: 12869ac6e44eSJoe Perches case 9: 12879ac6e44eSJoe Perches *p++ = '-'; 12889ac6e44eSJoe Perches break; 12899ac6e44eSJoe Perches } 12909ac6e44eSJoe Perches } 12919ac6e44eSJoe Perches 12929ac6e44eSJoe Perches *p = 0; 12939ac6e44eSJoe Perches 12949ac6e44eSJoe Perches if (uc) { 12959ac6e44eSJoe Perches p = uuid; 12969ac6e44eSJoe Perches do { 12979ac6e44eSJoe Perches *p = toupper(*p); 12989ac6e44eSJoe Perches } while (*(++p)); 12999ac6e44eSJoe Perches } 13009ac6e44eSJoe Perches 13019ac6e44eSJoe Perches return string(buf, end, uuid, spec); 13029ac6e44eSJoe Perches } 13039ac6e44eSJoe Perches 1304c8f44affSMichał Mirosław static 1305c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr, 1306c8f44affSMichał Mirosław struct printf_spec spec) 1307c8f44affSMichał Mirosław { 1308c8f44affSMichał Mirosław spec.flags |= SPECIAL | SMALL | ZEROPAD; 1309c8f44affSMichał Mirosław if (spec.field_width == -1) 1310c8f44affSMichał Mirosław spec.field_width = 2 + 2 * sizeof(netdev_features_t); 1311c8f44affSMichał Mirosław spec.base = 16; 1312c8f44affSMichał Mirosław 1313c8f44affSMichał Mirosław return number(buf, end, *(const netdev_features_t *)addr, spec); 1314c8f44affSMichał Mirosław } 1315c8f44affSMichał Mirosław 1316aaf07621SJoe Perches static noinline_for_stack 1317aaf07621SJoe Perches char *address_val(char *buf, char *end, const void *addr, 1318aaf07621SJoe Perches struct printf_spec spec, const char *fmt) 1319aaf07621SJoe Perches { 1320aaf07621SJoe Perches unsigned long long num; 1321aaf07621SJoe Perches 1322aaf07621SJoe Perches spec.flags |= SPECIAL | SMALL | ZEROPAD; 1323aaf07621SJoe Perches spec.base = 16; 1324aaf07621SJoe Perches 1325aaf07621SJoe Perches switch (fmt[1]) { 1326aaf07621SJoe Perches case 'd': 1327aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 1328aaf07621SJoe Perches spec.field_width = sizeof(dma_addr_t) * 2 + 2; 1329aaf07621SJoe Perches break; 1330aaf07621SJoe Perches case 'p': 1331aaf07621SJoe Perches default: 1332aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 1333aaf07621SJoe Perches spec.field_width = sizeof(phys_addr_t) * 2 + 2; 1334aaf07621SJoe Perches break; 1335aaf07621SJoe Perches } 1336aaf07621SJoe Perches 1337aaf07621SJoe Perches return number(buf, end, num, spec); 1338aaf07621SJoe Perches } 1339aaf07621SJoe Perches 1340900cca29SGeert Uytterhoeven static noinline_for_stack 1341900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1342900cca29SGeert Uytterhoeven const char *fmt) 1343900cca29SGeert Uytterhoeven { 1344900cca29SGeert Uytterhoeven if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk) 1345900cca29SGeert Uytterhoeven return string(buf, end, NULL, spec); 1346900cca29SGeert Uytterhoeven 1347900cca29SGeert Uytterhoeven switch (fmt[1]) { 1348900cca29SGeert Uytterhoeven case 'r': 1349900cca29SGeert Uytterhoeven return number(buf, end, clk_get_rate(clk), spec); 1350900cca29SGeert Uytterhoeven 1351900cca29SGeert Uytterhoeven case 'n': 1352900cca29SGeert Uytterhoeven default: 1353900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1354900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1355900cca29SGeert Uytterhoeven #else 1356900cca29SGeert Uytterhoeven spec.base = 16; 1357900cca29SGeert Uytterhoeven spec.field_width = sizeof(unsigned long) * 2 + 2; 1358900cca29SGeert Uytterhoeven spec.flags |= SPECIAL | SMALL | ZEROPAD; 1359900cca29SGeert Uytterhoeven return number(buf, end, (unsigned long)clk, spec); 1360900cca29SGeert Uytterhoeven #endif 1361900cca29SGeert Uytterhoeven } 1362900cca29SGeert Uytterhoeven } 1363900cca29SGeert Uytterhoeven 1364411f05f1SIngo Molnar int kptr_restrict __read_mostly; 1365455cd5abSDan Rosenberg 13664d8a743cSLinus Torvalds /* 13674d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 13684d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 13694d8a743cSLinus Torvalds * specifiers. 13704d8a743cSLinus Torvalds * 1371332d2e78SLinus Torvalds * Right now we handle: 13720fe1ef24SLinus Torvalds * 13730c8b946eSFrederic Weisbecker * - 'F' For symbolic function descriptor pointers with offset 13740c8b946eSFrederic Weisbecker * - 'f' For simple symbolic function names without offset 13750efb4d20SSteven Rostedt * - 'S' For symbolic direct pointers with offset 13760efb4d20SSteven Rostedt * - 's' For symbolic direct pointers without offset 1377b0d33c2bSJoe Perches * - '[FfSs]R' as above with __builtin_extract_return_addr() translation 13780f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 1379c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 1380c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 1381dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 1382dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 1383dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 1384dbc760bcSTejun Heo * range-list format instead of hex format 1385dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 1386dd45c9cfSHarvey Harrison * usual colon-separated hex notation 13878a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 1388bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 1389c8e00060SJoe Perches * with a dash-separated hex notation 13907c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 13918a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 13928a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 13938a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 139410679643SDaniel Borkmann * [S][pfs] 139510679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 139610679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 13978a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 13988a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 13998a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 140010679643SDaniel Borkmann * [S][pfs] 140110679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 140210679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 140310679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 140410679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 140529cf519eSJoe Perches * http://tools.ietf.org/html/rfc5952 140671dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 140771dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 140871dca95dSAndy Shevchenko * details): 140971dca95dSAndy Shevchenko * a - ESCAPE_ANY 141071dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 141171dca95dSAndy Shevchenko * h - ESCAPE_HEX 141271dca95dSAndy Shevchenko * n - ESCAPE_NULL 141371dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 141471dca95dSAndy Shevchenko * p - ESCAPE_NP 141571dca95dSAndy Shevchenko * s - ESCAPE_SPACE 141671dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 14179ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 14189ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 14199ac6e44eSJoe Perches * Options for %pU are: 14209ac6e44eSJoe Perches * b big endian lower case hex (default) 14219ac6e44eSJoe Perches * B big endian UPPER case hex 14229ac6e44eSJoe Perches * l little endian lower case hex 14239ac6e44eSJoe Perches * L little endian UPPER case hex 14249ac6e44eSJoe Perches * big endian output byte order is: 14259ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 14269ac6e44eSJoe Perches * little endian output byte order is: 14279ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 14287db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 14297db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 14307db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 14317db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 14327db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 1433455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 1434c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 143531550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 143631550a16SAndy Shevchenko * a certain separator (' ' by default): 143731550a16SAndy Shevchenko * C colon 143831550a16SAndy Shevchenko * D dash 143931550a16SAndy Shevchenko * N no separator 144031550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 144131550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 1442aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 1443aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 1444c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 1445c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 1446900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 1447900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 1448900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 1449900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 1450900cca29SGeert Uytterhoeven * - 'Cr' For a clock, it prints the current rate of the clock 14519ac6e44eSJoe Perches * 1452332d2e78SLinus Torvalds * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 1453332d2e78SLinus Torvalds * function pointers are really function descriptors, which contain a 1454332d2e78SLinus Torvalds * pointer to the real address. 14554d8a743cSLinus Torvalds */ 1456cf3b429bSJoe Perches static noinline_for_stack 1457cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 1458fef20d9cSFrederic Weisbecker struct printf_spec spec) 145978a8bf69SLinus Torvalds { 1460725fe002SGrant Likely int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); 1461725fe002SGrant Likely 14629f36e2c4SKees Cook if (!ptr && *fmt != 'K') { 14635e057981SJoe Perches /* 14645e057981SJoe Perches * Print (null) with the same width as a pointer so it makes 14655e057981SJoe Perches * tabular output look nice. 14665e057981SJoe Perches */ 14675e057981SJoe Perches if (spec.field_width == -1) 1468725fe002SGrant Likely spec.field_width = default_width; 1469fef20d9cSFrederic Weisbecker return string(buf, end, "(null)", spec); 14705e057981SJoe Perches } 1471d97106abSLinus Torvalds 14720fe1ef24SLinus Torvalds switch (*fmt) { 14730fe1ef24SLinus Torvalds case 'F': 14740c8b946eSFrederic Weisbecker case 'f': 14750fe1ef24SLinus Torvalds ptr = dereference_function_descriptor(ptr); 14760fe1ef24SLinus Torvalds /* Fallthrough */ 14770fe1ef24SLinus Torvalds case 'S': 14789ac6e44eSJoe Perches case 's': 14790f77a8d3SNamhyung Kim case 'B': 1480b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 1481332d2e78SLinus Torvalds case 'R': 1482c7dabef8SBjorn Helgaas case 'r': 1483fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 148431550a16SAndy Shevchenko case 'h': 148531550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 1486dbc760bcSTejun Heo case 'b': 1487dbc760bcSTejun Heo switch (fmt[1]) { 1488dbc760bcSTejun Heo case 'l': 1489dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 1490dbc760bcSTejun Heo default: 1491dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 1492dbc760bcSTejun Heo } 14938a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 14948a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 149576597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 149676597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 14978a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 14988a27f7c9SJoe Perches case 'I': /* Formatted IP supported 14998a27f7c9SJoe Perches * 4: 1.2.3.4 15008a27f7c9SJoe Perches * 6: 0001:0203:...:0708 15018a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 15028a27f7c9SJoe Perches */ 15038a27f7c9SJoe Perches case 'i': /* Contiguous: 15048a27f7c9SJoe Perches * 4: 001.002.003.004 15058a27f7c9SJoe Perches * 6: 000102...0f 15068a27f7c9SJoe Perches */ 15078a27f7c9SJoe Perches switch (fmt[1]) { 15088a27f7c9SJoe Perches case '6': 15098a27f7c9SJoe Perches return ip6_addr_string(buf, end, ptr, spec, fmt); 15108a27f7c9SJoe Perches case '4': 15118a27f7c9SJoe Perches return ip4_addr_string(buf, end, ptr, spec, fmt); 151210679643SDaniel Borkmann case 'S': { 151310679643SDaniel Borkmann const union { 151410679643SDaniel Borkmann struct sockaddr raw; 151510679643SDaniel Borkmann struct sockaddr_in v4; 151610679643SDaniel Borkmann struct sockaddr_in6 v6; 151710679643SDaniel Borkmann } *sa = ptr; 151810679643SDaniel Borkmann 151910679643SDaniel Borkmann switch (sa->raw.sa_family) { 152010679643SDaniel Borkmann case AF_INET: 152110679643SDaniel Borkmann return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 152210679643SDaniel Borkmann case AF_INET6: 152310679643SDaniel Borkmann return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 152410679643SDaniel Borkmann default: 152510679643SDaniel Borkmann return string(buf, end, "(invalid address)", spec); 152610679643SDaniel Borkmann }} 15278a27f7c9SJoe Perches } 15284aa99606SHarvey Harrison break; 152971dca95dSAndy Shevchenko case 'E': 153071dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 15319ac6e44eSJoe Perches case 'U': 15329ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 15337db6f5fbSJoe Perches case 'V': 15345756b76eSJan Beulich { 15355756b76eSJan Beulich va_list va; 15365756b76eSJan Beulich 15375756b76eSJan Beulich va_copy(va, *((struct va_format *)ptr)->va); 15385756b76eSJan Beulich buf += vsnprintf(buf, end > buf ? end - buf : 0, 15395756b76eSJan Beulich ((struct va_format *)ptr)->fmt, va); 15405756b76eSJan Beulich va_end(va); 15415756b76eSJan Beulich return buf; 15425756b76eSJan Beulich } 1543455cd5abSDan Rosenberg case 'K': 1544455cd5abSDan Rosenberg /* 1545455cd5abSDan Rosenberg * %pK cannot be used in IRQ context because its test 1546455cd5abSDan Rosenberg * for CAP_SYSLOG would be meaningless. 1547455cd5abSDan Rosenberg */ 15483715c530SDan Rosenberg if (kptr_restrict && (in_irq() || in_serving_softirq() || 15493715c530SDan Rosenberg in_nmi())) { 1550455cd5abSDan Rosenberg if (spec.field_width == -1) 1551725fe002SGrant Likely spec.field_width = default_width; 1552455cd5abSDan Rosenberg return string(buf, end, "pK-error", spec); 1553455cd5abSDan Rosenberg } 1554312b4e22SRyan Mallon 1555312b4e22SRyan Mallon switch (kptr_restrict) { 1556312b4e22SRyan Mallon case 0: 1557312b4e22SRyan Mallon /* Always print %pK values */ 1558312b4e22SRyan Mallon break; 1559312b4e22SRyan Mallon case 1: { 1560312b4e22SRyan Mallon /* 1561312b4e22SRyan Mallon * Only print the real pointer value if the current 1562312b4e22SRyan Mallon * process has CAP_SYSLOG and is running with the 1563312b4e22SRyan Mallon * same credentials it started with. This is because 1564312b4e22SRyan Mallon * access to files is checked at open() time, but %pK 1565312b4e22SRyan Mallon * checks permission at read() time. We don't want to 1566312b4e22SRyan Mallon * leak pointer values if a binary opens a file using 1567312b4e22SRyan Mallon * %pK and then elevates privileges before reading it. 1568312b4e22SRyan Mallon */ 1569312b4e22SRyan Mallon const struct cred *cred = current_cred(); 1570312b4e22SRyan Mallon 1571312b4e22SRyan Mallon if (!has_capability_noaudit(current, CAP_SYSLOG) || 1572312b4e22SRyan Mallon !uid_eq(cred->euid, cred->uid) || 1573312b4e22SRyan Mallon !gid_eq(cred->egid, cred->gid)) 157426297607SJoe Perches ptr = NULL; 157526297607SJoe Perches break; 1576312b4e22SRyan Mallon } 1577312b4e22SRyan Mallon case 2: 1578312b4e22SRyan Mallon default: 1579312b4e22SRyan Mallon /* Always print 0's for %pK */ 1580312b4e22SRyan Mallon ptr = NULL; 1581312b4e22SRyan Mallon break; 1582312b4e22SRyan Mallon } 1583312b4e22SRyan Mallon break; 1584312b4e22SRyan Mallon 1585c8f44affSMichał Mirosław case 'N': 1586c8f44affSMichał Mirosław switch (fmt[1]) { 1587c8f44affSMichał Mirosław case 'F': 1588c8f44affSMichał Mirosław return netdev_feature_string(buf, end, ptr, spec); 1589c8f44affSMichał Mirosław } 1590c8f44affSMichał Mirosław break; 15917d799210SStepan Moskovchenko case 'a': 1592aaf07621SJoe Perches return address_val(buf, end, ptr, spec, fmt); 15934b6ccca7SAl Viro case 'd': 15944b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 1595900cca29SGeert Uytterhoeven case 'C': 1596900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 15974b6ccca7SAl Viro case 'D': 15984b6ccca7SAl Viro return dentry_name(buf, end, 15994b6ccca7SAl Viro ((const struct file *)ptr)->f_path.dentry, 16004b6ccca7SAl Viro spec, fmt); 16010fe1ef24SLinus Torvalds } 1602fef20d9cSFrederic Weisbecker spec.flags |= SMALL; 1603fef20d9cSFrederic Weisbecker if (spec.field_width == -1) { 1604725fe002SGrant Likely spec.field_width = default_width; 1605fef20d9cSFrederic Weisbecker spec.flags |= ZEROPAD; 160678a8bf69SLinus Torvalds } 1607fef20d9cSFrederic Weisbecker spec.base = 16; 1608fef20d9cSFrederic Weisbecker 1609fef20d9cSFrederic Weisbecker return number(buf, end, (unsigned long) ptr, spec); 1610fef20d9cSFrederic Weisbecker } 1611fef20d9cSFrederic Weisbecker 1612fef20d9cSFrederic Weisbecker /* 1613fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 1614fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 1615fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 1616fef20d9cSFrederic Weisbecker * to go on the next call). 1617fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 1618fef20d9cSFrederic Weisbecker * 1619fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 1620fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 1621fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 1622fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 1623fef20d9cSFrederic Weisbecker * 1624fef20d9cSFrederic Weisbecker * @fmt: the format string 1625fef20d9cSFrederic Weisbecker * @type of the token returned 1626fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 1627fef20d9cSFrederic Weisbecker * @field_width: overwritten width 1628fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 1629fef20d9cSFrederic Weisbecker * @precision: precision of a number 1630fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 1631fef20d9cSFrederic Weisbecker */ 1632cf3b429bSJoe Perches static noinline_for_stack 1633cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 1634fef20d9cSFrederic Weisbecker { 1635fef20d9cSFrederic Weisbecker const char *start = fmt; 1636fef20d9cSFrederic Weisbecker 1637fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 1638ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 1639fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 1640fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 1641fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 1642fef20d9cSFrederic Weisbecker } 1643fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1644fef20d9cSFrederic Weisbecker goto precision; 1645fef20d9cSFrederic Weisbecker } 1646fef20d9cSFrederic Weisbecker 1647fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 1648fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 1649fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1650fef20d9cSFrederic Weisbecker spec->precision = 0; 1651fef20d9cSFrederic Weisbecker 1652fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1653fef20d9cSFrederic Weisbecker goto qualifier; 1654fef20d9cSFrederic Weisbecker } 1655fef20d9cSFrederic Weisbecker 1656fef20d9cSFrederic Weisbecker /* By default */ 1657fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1658fef20d9cSFrederic Weisbecker 1659fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 1660fef20d9cSFrederic Weisbecker if (*fmt == '%') 1661fef20d9cSFrederic Weisbecker break; 1662fef20d9cSFrederic Weisbecker } 1663fef20d9cSFrederic Weisbecker 1664fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 1665fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 1666fef20d9cSFrederic Weisbecker return fmt - start; 1667fef20d9cSFrederic Weisbecker 1668fef20d9cSFrederic Weisbecker /* Process flags */ 1669fef20d9cSFrederic Weisbecker spec->flags = 0; 1670fef20d9cSFrederic Weisbecker 1671fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 1672fef20d9cSFrederic Weisbecker bool found = true; 1673fef20d9cSFrederic Weisbecker 1674fef20d9cSFrederic Weisbecker ++fmt; 1675fef20d9cSFrederic Weisbecker 1676fef20d9cSFrederic Weisbecker switch (*fmt) { 1677fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 1678fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 1679fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 1680fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 1681fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 1682fef20d9cSFrederic Weisbecker default: found = false; 1683fef20d9cSFrederic Weisbecker } 1684fef20d9cSFrederic Weisbecker 1685fef20d9cSFrederic Weisbecker if (!found) 1686fef20d9cSFrederic Weisbecker break; 1687fef20d9cSFrederic Weisbecker } 1688fef20d9cSFrederic Weisbecker 1689fef20d9cSFrederic Weisbecker /* get field width */ 1690fef20d9cSFrederic Weisbecker spec->field_width = -1; 1691fef20d9cSFrederic Weisbecker 1692fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 1693fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 1694fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 1695fef20d9cSFrederic Weisbecker /* it's the next argument */ 1696ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 1697fef20d9cSFrederic Weisbecker return ++fmt - start; 1698fef20d9cSFrederic Weisbecker } 1699fef20d9cSFrederic Weisbecker 1700fef20d9cSFrederic Weisbecker precision: 1701fef20d9cSFrederic Weisbecker /* get the precision */ 1702fef20d9cSFrederic Weisbecker spec->precision = -1; 1703fef20d9cSFrederic Weisbecker if (*fmt == '.') { 1704fef20d9cSFrederic Weisbecker ++fmt; 1705fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 1706fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 1707fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1708fef20d9cSFrederic Weisbecker spec->precision = 0; 1709fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 1710fef20d9cSFrederic Weisbecker /* it's the next argument */ 1711adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 1712fef20d9cSFrederic Weisbecker return ++fmt - start; 1713fef20d9cSFrederic Weisbecker } 1714fef20d9cSFrederic Weisbecker } 1715fef20d9cSFrederic Weisbecker 1716fef20d9cSFrederic Weisbecker qualifier: 1717fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 1718fef20d9cSFrederic Weisbecker spec->qualifier = -1; 171975fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 172075fb8f26SAndy Shevchenko _tolower(*fmt) == 'z' || *fmt == 't') { 1721a4e94ef0SZhaolei spec->qualifier = *fmt++; 1722a4e94ef0SZhaolei if (unlikely(spec->qualifier == *fmt)) { 1723a4e94ef0SZhaolei if (spec->qualifier == 'l') { 1724fef20d9cSFrederic Weisbecker spec->qualifier = 'L'; 1725fef20d9cSFrederic Weisbecker ++fmt; 1726a4e94ef0SZhaolei } else if (spec->qualifier == 'h') { 1727a4e94ef0SZhaolei spec->qualifier = 'H'; 1728a4e94ef0SZhaolei ++fmt; 1729a4e94ef0SZhaolei } 1730fef20d9cSFrederic Weisbecker } 1731fef20d9cSFrederic Weisbecker } 1732fef20d9cSFrederic Weisbecker 1733fef20d9cSFrederic Weisbecker /* default base */ 1734fef20d9cSFrederic Weisbecker spec->base = 10; 1735fef20d9cSFrederic Weisbecker switch (*fmt) { 1736fef20d9cSFrederic Weisbecker case 'c': 1737fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 1738fef20d9cSFrederic Weisbecker return ++fmt - start; 1739fef20d9cSFrederic Weisbecker 1740fef20d9cSFrederic Weisbecker case 's': 1741fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 1742fef20d9cSFrederic Weisbecker return ++fmt - start; 1743fef20d9cSFrederic Weisbecker 1744fef20d9cSFrederic Weisbecker case 'p': 1745fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 1746ffbfed03SRasmus Villemoes return ++fmt - start; 1747fef20d9cSFrederic Weisbecker 1748fef20d9cSFrederic Weisbecker case '%': 1749fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 1750fef20d9cSFrederic Weisbecker return ++fmt - start; 1751fef20d9cSFrederic Weisbecker 1752fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 1753fef20d9cSFrederic Weisbecker case 'o': 1754fef20d9cSFrederic Weisbecker spec->base = 8; 1755fef20d9cSFrederic Weisbecker break; 1756fef20d9cSFrederic Weisbecker 1757fef20d9cSFrederic Weisbecker case 'x': 1758fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 1759fef20d9cSFrederic Weisbecker 1760fef20d9cSFrederic Weisbecker case 'X': 1761fef20d9cSFrederic Weisbecker spec->base = 16; 1762fef20d9cSFrederic Weisbecker break; 1763fef20d9cSFrederic Weisbecker 1764fef20d9cSFrederic Weisbecker case 'd': 1765fef20d9cSFrederic Weisbecker case 'i': 176639e874f8SFrederic Weisbecker spec->flags |= SIGN; 1767fef20d9cSFrederic Weisbecker case 'u': 1768fef20d9cSFrederic Weisbecker break; 1769fef20d9cSFrederic Weisbecker 1770708d96fdSRyan Mallon case 'n': 1771708d96fdSRyan Mallon /* 1772708d96fdSRyan Mallon * Since %n poses a greater security risk than utility, treat 1773708d96fdSRyan Mallon * it as an invalid format specifier. Warn about its use so 1774708d96fdSRyan Mallon * that new instances don't get added. 1775708d96fdSRyan Mallon */ 1776708d96fdSRyan Mallon WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt); 1777708d96fdSRyan Mallon /* Fall-through */ 1778708d96fdSRyan Mallon 1779fef20d9cSFrederic Weisbecker default: 1780fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 1781fef20d9cSFrederic Weisbecker return fmt - start; 1782fef20d9cSFrederic Weisbecker } 1783fef20d9cSFrederic Weisbecker 1784fef20d9cSFrederic Weisbecker if (spec->qualifier == 'L') 1785fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 1786fef20d9cSFrederic Weisbecker else if (spec->qualifier == 'l') { 178751be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 178851be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 178975fb8f26SAndy Shevchenko } else if (_tolower(spec->qualifier) == 'z') { 1790fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 1791fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 't') { 1792fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 1793a4e94ef0SZhaolei } else if (spec->qualifier == 'H') { 179451be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 179551be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 1796fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 'h') { 179751be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 179851be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 1799fef20d9cSFrederic Weisbecker } else { 180051be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 180151be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 1802fef20d9cSFrederic Weisbecker } 1803fef20d9cSFrederic Weisbecker 1804fef20d9cSFrederic Weisbecker return ++fmt - start; 180578a8bf69SLinus Torvalds } 180678a8bf69SLinus Torvalds 18071da177e4SLinus Torvalds /** 18081da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 18091da177e4SLinus Torvalds * @buf: The buffer to place the result into 18101da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 18111da177e4SLinus Torvalds * @fmt: The format string to use 18121da177e4SLinus Torvalds * @args: Arguments for the format string 18131da177e4SLinus Torvalds * 181420036fdcSAndi Kleen * This function follows C99 vsnprintf, but has some extensions: 181591adcd2cSSteven Rostedt * %pS output the name of a text symbol with offset 181691adcd2cSSteven Rostedt * %ps output the name of a text symbol without offset 18170c8b946eSFrederic Weisbecker * %pF output the name of a function pointer with its offset 18180c8b946eSFrederic Weisbecker * %pf output the name of a function pointer without its offset 18190f77a8d3SNamhyung Kim * %pB output the name of a backtrace symbol with its offset 18208a79503aSUwe Kleine-König * %pR output the address range in a struct resource with decoded flags 18218a79503aSUwe Kleine-König * %pr output the address range in a struct resource with raw flags 1822dbc760bcSTejun Heo * %pb output the bitmap with field width as the number of bits 1823dbc760bcSTejun Heo * %pbl output the bitmap as range list with field width as the number of bits 18248a79503aSUwe Kleine-König * %pM output a 6-byte MAC address with colons 18257c59154eSAndy Shevchenko * %pMR output a 6-byte MAC address with colons in reversed order 18267c59154eSAndy Shevchenko * %pMF output a 6-byte MAC address with dashes 18278a79503aSUwe Kleine-König * %pm output a 6-byte MAC address without colons 18287c59154eSAndy Shevchenko * %pmR output a 6-byte MAC address without colons in reversed order 18298a79503aSUwe Kleine-König * %pI4 print an IPv4 address without leading zeros 18308a79503aSUwe Kleine-König * %pi4 print an IPv4 address with leading zeros 18318a79503aSUwe Kleine-König * %pI6 print an IPv6 address with colons 18328a79503aSUwe Kleine-König * %pi6 print an IPv6 address without colons 1833f996f208SJan Engelhardt * %pI6c print an IPv6 address as specified by RFC 5952 183410679643SDaniel Borkmann * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address 183510679643SDaniel Borkmann * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address 18368a79503aSUwe Kleine-König * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper 18378a79503aSUwe Kleine-König * case. 183871dca95dSAndy Shevchenko * %*pE[achnops] print an escaped buffer 183931550a16SAndy Shevchenko * %*ph[CDN] a variable-length hex string with a separator (supports up to 64 184031550a16SAndy Shevchenko * bytes of the input) 1841900cca29SGeert Uytterhoeven * %pC output the name (Common Clock Framework) or address (legacy clock 1842900cca29SGeert Uytterhoeven * framework) of a clock 1843900cca29SGeert Uytterhoeven * %pCn output the name (Common Clock Framework) or address (legacy clock 1844900cca29SGeert Uytterhoeven * framework) of a clock 1845900cca29SGeert Uytterhoeven * %pCr output the current rate of a clock 18460efb4d20SSteven Rostedt * %n is ignored 184720036fdcSAndi Kleen * 184880f548e0SAndrew Morton * ** Please update Documentation/printk-formats.txt when making changes ** 184980f548e0SAndrew Morton * 18501da177e4SLinus Torvalds * The return value is the number of characters which would 18511da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 18521da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 18531da177e4SLinus Torvalds * number of characters written into @buf as return value 185472fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 18551da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 18561da177e4SLinus Torvalds * string is truncated. 18571da177e4SLinus Torvalds * 1858ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 18591da177e4SLinus Torvalds */ 18601da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 18611da177e4SLinus Torvalds { 18621da177e4SLinus Torvalds unsigned long long num; 1863d4be151bSAndré Goddard Rosa char *str, *end; 1864fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 18651da177e4SLinus Torvalds 1866f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 1867f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 18682aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 18691da177e4SLinus Torvalds return 0; 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds str = buf; 1872f796937aSJeremy Fitzhardinge end = buf + size; 18731da177e4SLinus Torvalds 1874f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 1875f796937aSJeremy Fitzhardinge if (end < buf) { 18761da177e4SLinus Torvalds end = ((void *)-1); 1877f796937aSJeremy Fitzhardinge size = end - buf; 18781da177e4SLinus Torvalds } 18791da177e4SLinus Torvalds 1880fef20d9cSFrederic Weisbecker while (*fmt) { 1881fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1882d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 1883fef20d9cSFrederic Weisbecker 1884fef20d9cSFrederic Weisbecker fmt += read; 1885fef20d9cSFrederic Weisbecker 1886fef20d9cSFrederic Weisbecker switch (spec.type) { 1887fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1888fef20d9cSFrederic Weisbecker int copy = read; 1889fef20d9cSFrederic Weisbecker if (str < end) { 1890fef20d9cSFrederic Weisbecker if (copy > end - str) 1891fef20d9cSFrederic Weisbecker copy = end - str; 1892fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1893fef20d9cSFrederic Weisbecker } 1894fef20d9cSFrederic Weisbecker str += read; 1895fef20d9cSFrederic Weisbecker break; 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds 1898ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1899fef20d9cSFrederic Weisbecker spec.field_width = va_arg(args, int); 1900fef20d9cSFrederic Weisbecker break; 19011da177e4SLinus Torvalds 1902fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1903fef20d9cSFrederic Weisbecker spec.precision = va_arg(args, int); 1904fef20d9cSFrederic Weisbecker break; 19051da177e4SLinus Torvalds 1906d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 1907d4be151bSAndré Goddard Rosa char c; 1908d4be151bSAndré Goddard Rosa 1909fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1910fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1911f796937aSJeremy Fitzhardinge if (str < end) 19121da177e4SLinus Torvalds *str = ' '; 19131da177e4SLinus Torvalds ++str; 1914fef20d9cSFrederic Weisbecker 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 1918f796937aSJeremy Fitzhardinge if (str < end) 19191da177e4SLinus Torvalds *str = c; 19201da177e4SLinus Torvalds ++str; 1921fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1922f796937aSJeremy Fitzhardinge if (str < end) 19231da177e4SLinus Torvalds *str = ' '; 19241da177e4SLinus Torvalds ++str; 19251da177e4SLinus Torvalds } 1926fef20d9cSFrederic Weisbecker break; 1927d4be151bSAndré Goddard Rosa } 19281da177e4SLinus Torvalds 1929fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 1930fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 1931fef20d9cSFrederic Weisbecker break; 19321da177e4SLinus Torvalds 1933fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1934ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 1935fef20d9cSFrederic Weisbecker spec); 1936fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 19374d8a743cSLinus Torvalds fmt++; 1938fef20d9cSFrederic Weisbecker break; 19391da177e4SLinus Torvalds 1940fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1941f796937aSJeremy Fitzhardinge if (str < end) 19421da177e4SLinus Torvalds *str = '%'; 19431da177e4SLinus Torvalds ++str; 19441da177e4SLinus Torvalds break; 19451da177e4SLinus Torvalds 1946fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 1947f796937aSJeremy Fitzhardinge if (str < end) 19481da177e4SLinus Torvalds *str = '%'; 19491da177e4SLinus Torvalds ++str; 1950fef20d9cSFrederic Weisbecker break; 1951fef20d9cSFrederic Weisbecker 1952fef20d9cSFrederic Weisbecker default: 1953fef20d9cSFrederic Weisbecker switch (spec.type) { 1954fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1955fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 1956fef20d9cSFrederic Weisbecker break; 1957fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1958fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 1959fef20d9cSFrederic Weisbecker break; 1960fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1961fef20d9cSFrederic Weisbecker num = va_arg(args, long); 1962fef20d9cSFrederic Weisbecker break; 1963fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1964ef124960SJason Gunthorpe if (spec.flags & SIGN) 1965ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 1966ef124960SJason Gunthorpe else 1967fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 1968fef20d9cSFrederic Weisbecker break; 1969fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1970fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 1971fef20d9cSFrederic Weisbecker break; 1972a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1973a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 1974a4e94ef0SZhaolei break; 1975a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1976a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 1977a4e94ef0SZhaolei break; 1978fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1979fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 1980fef20d9cSFrederic Weisbecker break; 1981fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1982fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 1983fef20d9cSFrederic Weisbecker break; 198439e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 198539e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 1986fef20d9cSFrederic Weisbecker break; 1987fef20d9cSFrederic Weisbecker default: 1988fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 19891da177e4SLinus Torvalds } 1990fef20d9cSFrederic Weisbecker 1991fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 19921da177e4SLinus Torvalds } 1993fef20d9cSFrederic Weisbecker } 1994fef20d9cSFrederic Weisbecker 1995f796937aSJeremy Fitzhardinge if (size > 0) { 1996f796937aSJeremy Fitzhardinge if (str < end) 19971da177e4SLinus Torvalds *str = '\0'; 1998f796937aSJeremy Fitzhardinge else 19990a6047eeSLinus Torvalds end[-1] = '\0'; 2000f796937aSJeremy Fitzhardinge } 2001fef20d9cSFrederic Weisbecker 2002f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 20031da177e4SLinus Torvalds return str-buf; 2004fef20d9cSFrederic Weisbecker 20051da177e4SLinus Torvalds } 20061da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds /** 20091da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 20101da177e4SLinus Torvalds * @buf: The buffer to place the result into 20111da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 20121da177e4SLinus Torvalds * @fmt: The format string to use 20131da177e4SLinus Torvalds * @args: Arguments for the format string 20141da177e4SLinus Torvalds * 20151da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2016b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 20171da177e4SLinus Torvalds * returns 0. 20181da177e4SLinus Torvalds * 2019ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 202020036fdcSAndi Kleen * 202120036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 20221da177e4SLinus Torvalds */ 20231da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 20241da177e4SLinus Torvalds { 20251da177e4SLinus Torvalds int i; 20261da177e4SLinus Torvalds 20271da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 20287b9186f5SAndré Goddard Rosa 2029b921c69fSAnton Arapov if (likely(i < size)) 2030b921c69fSAnton Arapov return i; 2031b921c69fSAnton Arapov if (size != 0) 2032b921c69fSAnton Arapov return size - 1; 2033b921c69fSAnton Arapov return 0; 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 20361da177e4SLinus Torvalds 20371da177e4SLinus Torvalds /** 20381da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 20391da177e4SLinus Torvalds * @buf: The buffer to place the result into 20401da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 20411da177e4SLinus Torvalds * @fmt: The format string to use 20421da177e4SLinus Torvalds * @...: Arguments for the format string 20431da177e4SLinus Torvalds * 20441da177e4SLinus Torvalds * The return value is the number of characters which would be 20451da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 20461da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 20471da177e4SLinus Torvalds * @size, the resulting string is truncated. 204820036fdcSAndi Kleen * 204920036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 20501da177e4SLinus Torvalds */ 20511da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 20521da177e4SLinus Torvalds { 20531da177e4SLinus Torvalds va_list args; 20541da177e4SLinus Torvalds int i; 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds va_start(args, fmt); 20571da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 20581da177e4SLinus Torvalds va_end(args); 20597b9186f5SAndré Goddard Rosa 20601da177e4SLinus Torvalds return i; 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds /** 20651da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 20661da177e4SLinus Torvalds * @buf: The buffer to place the result into 20671da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 20681da177e4SLinus Torvalds * @fmt: The format string to use 20691da177e4SLinus Torvalds * @...: Arguments for the format string 20701da177e4SLinus Torvalds * 20711da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2072b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 20731da177e4SLinus Torvalds */ 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 20761da177e4SLinus Torvalds { 20771da177e4SLinus Torvalds va_list args; 20781da177e4SLinus Torvalds int i; 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds va_start(args, fmt); 2081b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 20821da177e4SLinus Torvalds va_end(args); 20837b9186f5SAndré Goddard Rosa 2084b903c0b8SChangli Gao return i; 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 20871da177e4SLinus Torvalds 20881da177e4SLinus Torvalds /** 20891da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 20901da177e4SLinus Torvalds * @buf: The buffer to place the result into 20911da177e4SLinus Torvalds * @fmt: The format string to use 20921da177e4SLinus Torvalds * @args: Arguments for the format string 20931da177e4SLinus Torvalds * 20941da177e4SLinus Torvalds * The function returns the number of characters written 209572fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 20961da177e4SLinus Torvalds * buffer overflows. 20971da177e4SLinus Torvalds * 2098ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 209920036fdcSAndi Kleen * 210020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 21011da177e4SLinus Torvalds */ 21021da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 21031da177e4SLinus Torvalds { 21041da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds /** 21091da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 21101da177e4SLinus Torvalds * @buf: The buffer to place the result into 21111da177e4SLinus Torvalds * @fmt: The format string to use 21121da177e4SLinus Torvalds * @...: Arguments for the format string 21131da177e4SLinus Torvalds * 21141da177e4SLinus Torvalds * The function returns the number of characters written 211572fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 21161da177e4SLinus Torvalds * buffer overflows. 211720036fdcSAndi Kleen * 211820036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 21191da177e4SLinus Torvalds */ 21201da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 21211da177e4SLinus Torvalds { 21221da177e4SLinus Torvalds va_list args; 21231da177e4SLinus Torvalds int i; 21241da177e4SLinus Torvalds 21251da177e4SLinus Torvalds va_start(args, fmt); 21261da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 21271da177e4SLinus Torvalds va_end(args); 21287b9186f5SAndré Goddard Rosa 21291da177e4SLinus Torvalds return i; 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 21321da177e4SLinus Torvalds 21334370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 21344370aa4aSLai Jiangshan /* 21354370aa4aSLai Jiangshan * bprintf service: 21364370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 21374370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 21384370aa4aSLai Jiangshan */ 21394370aa4aSLai Jiangshan 21404370aa4aSLai Jiangshan /** 21414370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 21424370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 21434370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 21444370aa4aSLai Jiangshan * @fmt: The format string to use 21454370aa4aSLai Jiangshan * @args: Arguments for the format string 21464370aa4aSLai Jiangshan * 21474370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 2148da3dae54SMasanari Iida * is skipped. 21494370aa4aSLai Jiangshan * 21504370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 21514370aa4aSLai Jiangshan * the given input. 21524370aa4aSLai Jiangshan * 21534370aa4aSLai Jiangshan * NOTE: 21544370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 21554370aa4aSLai Jiangshan * valid for bstr_printf(). 21564370aa4aSLai Jiangshan */ 21574370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 21584370aa4aSLai Jiangshan { 2159fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 21604370aa4aSLai Jiangshan char *str, *end; 21614370aa4aSLai Jiangshan 21624370aa4aSLai Jiangshan str = (char *)bin_buf; 21634370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 21644370aa4aSLai Jiangshan 21654370aa4aSLai Jiangshan #define save_arg(type) \ 21664370aa4aSLai Jiangshan do { \ 21674370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 21684370aa4aSLai Jiangshan unsigned long long value; \ 21694370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 21704370aa4aSLai Jiangshan value = va_arg(args, unsigned long long); \ 21714370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 21724370aa4aSLai Jiangshan *(u32 *)str = *(u32 *)&value; \ 21734370aa4aSLai Jiangshan *(u32 *)(str + 4) = *((u32 *)&value + 1); \ 21744370aa4aSLai Jiangshan } \ 21754370aa4aSLai Jiangshan } else { \ 21764370aa4aSLai Jiangshan unsigned long value; \ 21774370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 21784370aa4aSLai Jiangshan value = va_arg(args, int); \ 21794370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 21804370aa4aSLai Jiangshan *(typeof(type) *)str = (type)value; \ 21814370aa4aSLai Jiangshan } \ 21824370aa4aSLai Jiangshan str += sizeof(type); \ 21834370aa4aSLai Jiangshan } while (0) 21844370aa4aSLai Jiangshan 2185fef20d9cSFrederic Weisbecker while (*fmt) { 2186d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 21874370aa4aSLai Jiangshan 2188fef20d9cSFrederic Weisbecker fmt += read; 2189fef20d9cSFrederic Weisbecker 2190fef20d9cSFrederic Weisbecker switch (spec.type) { 2191fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 2192d4be151bSAndré Goddard Rosa case FORMAT_TYPE_INVALID: 2193d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 2194fef20d9cSFrederic Weisbecker break; 2195fef20d9cSFrederic Weisbecker 2196ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2197fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 21984370aa4aSLai Jiangshan save_arg(int); 2199fef20d9cSFrederic Weisbecker break; 22004370aa4aSLai Jiangshan 2201fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 22024370aa4aSLai Jiangshan save_arg(char); 2203fef20d9cSFrederic Weisbecker break; 2204fef20d9cSFrederic Weisbecker 2205fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 22064370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 22074370aa4aSLai Jiangshan size_t len; 22086c356634SAndré Goddard Rosa 22094370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 22104370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 22110f4f81dcSAndré Goddard Rosa save_str = "(null)"; 22126c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 22136c356634SAndré Goddard Rosa if (str + len < end) 22146c356634SAndré Goddard Rosa memcpy(str, save_str, len); 22156c356634SAndré Goddard Rosa str += len; 2216fef20d9cSFrederic Weisbecker break; 22174370aa4aSLai Jiangshan } 2218fef20d9cSFrederic Weisbecker 2219fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 22204370aa4aSLai Jiangshan save_arg(void *); 22214370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2222fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 22234370aa4aSLai Jiangshan fmt++; 2224fef20d9cSFrederic Weisbecker break; 2225fef20d9cSFrederic Weisbecker 2226fef20d9cSFrederic Weisbecker default: 2227fef20d9cSFrederic Weisbecker switch (spec.type) { 2228fef20d9cSFrederic Weisbecker 2229fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2230fef20d9cSFrederic Weisbecker save_arg(long long); 2231fef20d9cSFrederic Weisbecker break; 2232fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2233fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2234fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2235fef20d9cSFrederic Weisbecker break; 2236fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2237fef20d9cSFrederic Weisbecker save_arg(size_t); 2238fef20d9cSFrederic Weisbecker break; 2239fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2240fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2241fef20d9cSFrederic Weisbecker break; 2242a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2243a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2244a4e94ef0SZhaolei save_arg(char); 2245a4e94ef0SZhaolei break; 2246fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2247fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2248fef20d9cSFrederic Weisbecker save_arg(short); 2249fef20d9cSFrederic Weisbecker break; 2250fef20d9cSFrederic Weisbecker default: 2251fef20d9cSFrederic Weisbecker save_arg(int); 2252fef20d9cSFrederic Weisbecker } 2253fef20d9cSFrederic Weisbecker } 2254fef20d9cSFrederic Weisbecker } 2255fef20d9cSFrederic Weisbecker 22567b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2257fef20d9cSFrederic Weisbecker #undef save_arg 22584370aa4aSLai Jiangshan } 22594370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 22604370aa4aSLai Jiangshan 22614370aa4aSLai Jiangshan /** 22624370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 22634370aa4aSLai Jiangshan * @buf: The buffer to place the result into 22644370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 22654370aa4aSLai Jiangshan * @fmt: The format string to use 22664370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 22674370aa4aSLai Jiangshan * 22684370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 22694370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 22704370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 22714370aa4aSLai Jiangshan * 22724370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 22730efb4d20SSteven Rostedt * see vsnprintf comment for details. 22744370aa4aSLai Jiangshan * 22754370aa4aSLai Jiangshan * The return value is the number of characters which would 22764370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 22774370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 22784370aa4aSLai Jiangshan * number of characters written into @buf as return value 22794370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 22804370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 22814370aa4aSLai Jiangshan * string is truncated. 22824370aa4aSLai Jiangshan */ 22834370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 22844370aa4aSLai Jiangshan { 2285fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 2286d4be151bSAndré Goddard Rosa char *str, *end; 2287d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 22884370aa4aSLai Jiangshan 22892f30b1f9SMarcin Slusarz if (WARN_ON_ONCE((int) size < 0)) 22904370aa4aSLai Jiangshan return 0; 22914370aa4aSLai Jiangshan 22924370aa4aSLai Jiangshan str = buf; 22934370aa4aSLai Jiangshan end = buf + size; 22944370aa4aSLai Jiangshan 22954370aa4aSLai Jiangshan #define get_arg(type) \ 22964370aa4aSLai Jiangshan ({ \ 22974370aa4aSLai Jiangshan typeof(type) value; \ 22984370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 22994370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 23004370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 23014370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 23024370aa4aSLai Jiangshan } else { \ 23034370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 23044370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 23054370aa4aSLai Jiangshan } \ 23064370aa4aSLai Jiangshan args += sizeof(type); \ 23074370aa4aSLai Jiangshan value; \ 23084370aa4aSLai Jiangshan }) 23094370aa4aSLai Jiangshan 23104370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 23114370aa4aSLai Jiangshan if (end < buf) { 23124370aa4aSLai Jiangshan end = ((void *)-1); 23134370aa4aSLai Jiangshan size = end - buf; 23144370aa4aSLai Jiangshan } 23154370aa4aSLai Jiangshan 2316fef20d9cSFrederic Weisbecker while (*fmt) { 2317fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2318d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2319fef20d9cSFrederic Weisbecker 2320fef20d9cSFrederic Weisbecker fmt += read; 2321fef20d9cSFrederic Weisbecker 2322fef20d9cSFrederic Weisbecker switch (spec.type) { 2323fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2324fef20d9cSFrederic Weisbecker int copy = read; 2325fef20d9cSFrederic Weisbecker if (str < end) { 2326fef20d9cSFrederic Weisbecker if (copy > end - str) 2327fef20d9cSFrederic Weisbecker copy = end - str; 2328fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2329fef20d9cSFrederic Weisbecker } 2330fef20d9cSFrederic Weisbecker str += read; 2331fef20d9cSFrederic Weisbecker break; 23324370aa4aSLai Jiangshan } 23334370aa4aSLai Jiangshan 2334ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2335fef20d9cSFrederic Weisbecker spec.field_width = get_arg(int); 2336fef20d9cSFrederic Weisbecker break; 23374370aa4aSLai Jiangshan 2338fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2339fef20d9cSFrederic Weisbecker spec.precision = get_arg(int); 2340fef20d9cSFrederic Weisbecker break; 23414370aa4aSLai Jiangshan 2342d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2343d4be151bSAndré Goddard Rosa char c; 2344d4be151bSAndré Goddard Rosa 2345fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2346fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 23474370aa4aSLai Jiangshan if (str < end) 23484370aa4aSLai Jiangshan *str = ' '; 23494370aa4aSLai Jiangshan ++str; 23504370aa4aSLai Jiangshan } 23514370aa4aSLai Jiangshan } 23524370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 23534370aa4aSLai Jiangshan if (str < end) 23544370aa4aSLai Jiangshan *str = c; 23554370aa4aSLai Jiangshan ++str; 2356fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 23574370aa4aSLai Jiangshan if (str < end) 23584370aa4aSLai Jiangshan *str = ' '; 23594370aa4aSLai Jiangshan ++str; 23604370aa4aSLai Jiangshan } 2361fef20d9cSFrederic Weisbecker break; 2362d4be151bSAndré Goddard Rosa } 23634370aa4aSLai Jiangshan 2364fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 23654370aa4aSLai Jiangshan const char *str_arg = args; 2366d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 2367fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 2368fef20d9cSFrederic Weisbecker break; 23694370aa4aSLai Jiangshan } 23704370aa4aSLai Jiangshan 2371fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2372ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 2373fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 23744370aa4aSLai Jiangshan fmt++; 2375fef20d9cSFrederic Weisbecker break; 23764370aa4aSLai Jiangshan 2377fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2378fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 23794370aa4aSLai Jiangshan if (str < end) 23804370aa4aSLai Jiangshan *str = '%'; 23814370aa4aSLai Jiangshan ++str; 2382fef20d9cSFrederic Weisbecker break; 2383fef20d9cSFrederic Weisbecker 2384d4be151bSAndré Goddard Rosa default: { 2385d4be151bSAndré Goddard Rosa unsigned long long num; 2386d4be151bSAndré Goddard Rosa 2387fef20d9cSFrederic Weisbecker switch (spec.type) { 2388fef20d9cSFrederic Weisbecker 2389fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 23904370aa4aSLai Jiangshan num = get_arg(long long); 2391fef20d9cSFrederic Weisbecker break; 2392fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2393fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2394fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 2395fef20d9cSFrederic Weisbecker break; 2396fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 23974370aa4aSLai Jiangshan num = get_arg(size_t); 2398fef20d9cSFrederic Weisbecker break; 2399fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 24004370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 2401fef20d9cSFrederic Weisbecker break; 2402a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2403a4e94ef0SZhaolei num = get_arg(unsigned char); 2404a4e94ef0SZhaolei break; 2405a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2406a4e94ef0SZhaolei num = get_arg(signed char); 2407a4e94ef0SZhaolei break; 2408fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2409fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 2410fef20d9cSFrederic Weisbecker break; 2411fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2412fef20d9cSFrederic Weisbecker num = get_arg(short); 2413fef20d9cSFrederic Weisbecker break; 2414fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 24154370aa4aSLai Jiangshan num = get_arg(unsigned int); 2416fef20d9cSFrederic Weisbecker break; 2417fef20d9cSFrederic Weisbecker default: 2418fef20d9cSFrederic Weisbecker num = get_arg(int); 24194370aa4aSLai Jiangshan } 2420fef20d9cSFrederic Weisbecker 2421fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 2422d4be151bSAndré Goddard Rosa } /* default: */ 2423d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 2424d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 2425fef20d9cSFrederic Weisbecker 24264370aa4aSLai Jiangshan if (size > 0) { 24274370aa4aSLai Jiangshan if (str < end) 24284370aa4aSLai Jiangshan *str = '\0'; 24294370aa4aSLai Jiangshan else 24304370aa4aSLai Jiangshan end[-1] = '\0'; 24314370aa4aSLai Jiangshan } 2432fef20d9cSFrederic Weisbecker 24334370aa4aSLai Jiangshan #undef get_arg 24344370aa4aSLai Jiangshan 24354370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 24364370aa4aSLai Jiangshan return str - buf; 24374370aa4aSLai Jiangshan } 24384370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 24394370aa4aSLai Jiangshan 24404370aa4aSLai Jiangshan /** 24414370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 24424370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 24434370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 24444370aa4aSLai Jiangshan * @fmt: The format string to use 24454370aa4aSLai Jiangshan * @...: Arguments for the format string 24464370aa4aSLai Jiangshan * 24474370aa4aSLai Jiangshan * The function returns the number of words(u32) written 24484370aa4aSLai Jiangshan * into @bin_buf. 24494370aa4aSLai Jiangshan */ 24504370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 24514370aa4aSLai Jiangshan { 24524370aa4aSLai Jiangshan va_list args; 24534370aa4aSLai Jiangshan int ret; 24544370aa4aSLai Jiangshan 24554370aa4aSLai Jiangshan va_start(args, fmt); 24564370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 24574370aa4aSLai Jiangshan va_end(args); 24587b9186f5SAndré Goddard Rosa 24594370aa4aSLai Jiangshan return ret; 24604370aa4aSLai Jiangshan } 24614370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 24624370aa4aSLai Jiangshan 24634370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 24644370aa4aSLai Jiangshan 24651da177e4SLinus Torvalds /** 24661da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 24671da177e4SLinus Torvalds * @buf: input buffer 24681da177e4SLinus Torvalds * @fmt: format of buffer 24691da177e4SLinus Torvalds * @args: arguments 24701da177e4SLinus Torvalds */ 24711da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 24721da177e4SLinus Torvalds { 24731da177e4SLinus Torvalds const char *str = buf; 24741da177e4SLinus Torvalds char *next; 24751da177e4SLinus Torvalds char digit; 24761da177e4SLinus Torvalds int num = 0; 2477ef0658f3SJoe Perches u8 qualifier; 247853809751SJan Beulich unsigned int base; 247953809751SJan Beulich union { 248053809751SJan Beulich long long s; 248153809751SJan Beulich unsigned long long u; 248253809751SJan Beulich } val; 2483ef0658f3SJoe Perches s16 field_width; 2484d4be151bSAndré Goddard Rosa bool is_sign; 24851da177e4SLinus Torvalds 2486da99075cSJan Beulich while (*fmt) { 24871da177e4SLinus Torvalds /* skip any white space in format */ 24881da177e4SLinus Torvalds /* white space in format matchs any amount of 24891da177e4SLinus Torvalds * white space, including none, in the input. 24901da177e4SLinus Torvalds */ 24911da177e4SLinus Torvalds if (isspace(*fmt)) { 2492e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 2493e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 24941da177e4SLinus Torvalds } 24951da177e4SLinus Torvalds 24961da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 24971da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 24981da177e4SLinus Torvalds if (*fmt++ != *str++) 24991da177e4SLinus Torvalds break; 25001da177e4SLinus Torvalds continue; 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds 25031da177e4SLinus Torvalds if (!*fmt) 25041da177e4SLinus Torvalds break; 25051da177e4SLinus Torvalds ++fmt; 25061da177e4SLinus Torvalds 25071da177e4SLinus Torvalds /* skip this conversion. 25081da177e4SLinus Torvalds * advance both strings to next white space 25091da177e4SLinus Torvalds */ 25101da177e4SLinus Torvalds if (*fmt == '*') { 2511da99075cSJan Beulich if (!*str) 2512da99075cSJan Beulich break; 25138fccae2cSAndy Spencer while (!isspace(*fmt) && *fmt != '%' && *fmt) 25141da177e4SLinus Torvalds fmt++; 25151da177e4SLinus Torvalds while (!isspace(*str) && *str) 25161da177e4SLinus Torvalds str++; 25171da177e4SLinus Torvalds continue; 25181da177e4SLinus Torvalds } 25191da177e4SLinus Torvalds 25201da177e4SLinus Torvalds /* get field width */ 25211da177e4SLinus Torvalds field_width = -1; 252253809751SJan Beulich if (isdigit(*fmt)) { 25231da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 252453809751SJan Beulich if (field_width <= 0) 252553809751SJan Beulich break; 252653809751SJan Beulich } 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds /* get conversion qualifier */ 25291da177e4SLinus Torvalds qualifier = -1; 253075fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 253175fb8f26SAndy Shevchenko _tolower(*fmt) == 'z') { 25321da177e4SLinus Torvalds qualifier = *fmt++; 25331da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 25341da177e4SLinus Torvalds if (qualifier == 'h') { 25351da177e4SLinus Torvalds qualifier = 'H'; 25361da177e4SLinus Torvalds fmt++; 25371da177e4SLinus Torvalds } else if (qualifier == 'l') { 25381da177e4SLinus Torvalds qualifier = 'L'; 25391da177e4SLinus Torvalds fmt++; 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds } 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 2544da99075cSJan Beulich if (!*fmt) 2545da99075cSJan Beulich break; 2546da99075cSJan Beulich 2547da99075cSJan Beulich if (*fmt == 'n') { 2548da99075cSJan Beulich /* return number of characters read so far */ 2549da99075cSJan Beulich *va_arg(args, int *) = str - buf; 2550da99075cSJan Beulich ++fmt; 2551da99075cSJan Beulich continue; 2552da99075cSJan Beulich } 2553da99075cSJan Beulich 2554da99075cSJan Beulich if (!*str) 25551da177e4SLinus Torvalds break; 25561da177e4SLinus Torvalds 2557d4be151bSAndré Goddard Rosa base = 10; 25583f623ebaSFabian Frederick is_sign = false; 2559d4be151bSAndré Goddard Rosa 25601da177e4SLinus Torvalds switch (*fmt++) { 25611da177e4SLinus Torvalds case 'c': 25621da177e4SLinus Torvalds { 25631da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 25641da177e4SLinus Torvalds if (field_width == -1) 25651da177e4SLinus Torvalds field_width = 1; 25661da177e4SLinus Torvalds do { 25671da177e4SLinus Torvalds *s++ = *str++; 25681da177e4SLinus Torvalds } while (--field_width > 0 && *str); 25691da177e4SLinus Torvalds num++; 25701da177e4SLinus Torvalds } 25711da177e4SLinus Torvalds continue; 25721da177e4SLinus Torvalds case 's': 25731da177e4SLinus Torvalds { 25741da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 25751da177e4SLinus Torvalds if (field_width == -1) 25764be929beSAlexey Dobriyan field_width = SHRT_MAX; 25771da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 2578e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 25791da177e4SLinus Torvalds 25801da177e4SLinus Torvalds /* now copy until next white space */ 25817b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 25821da177e4SLinus Torvalds *s++ = *str++; 25831da177e4SLinus Torvalds *s = '\0'; 25841da177e4SLinus Torvalds num++; 25851da177e4SLinus Torvalds } 25861da177e4SLinus Torvalds continue; 25871da177e4SLinus Torvalds case 'o': 25881da177e4SLinus Torvalds base = 8; 25891da177e4SLinus Torvalds break; 25901da177e4SLinus Torvalds case 'x': 25911da177e4SLinus Torvalds case 'X': 25921da177e4SLinus Torvalds base = 16; 25931da177e4SLinus Torvalds break; 25941da177e4SLinus Torvalds case 'i': 25951da177e4SLinus Torvalds base = 0; 25961da177e4SLinus Torvalds case 'd': 25973f623ebaSFabian Frederick is_sign = true; 25981da177e4SLinus Torvalds case 'u': 25991da177e4SLinus Torvalds break; 26001da177e4SLinus Torvalds case '%': 26011da177e4SLinus Torvalds /* looking for '%' in str */ 26021da177e4SLinus Torvalds if (*str++ != '%') 26031da177e4SLinus Torvalds return num; 26041da177e4SLinus Torvalds continue; 26051da177e4SLinus Torvalds default: 26061da177e4SLinus Torvalds /* invalid format; stop here */ 26071da177e4SLinus Torvalds return num; 26081da177e4SLinus Torvalds } 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds /* have some sort of integer conversion. 26111da177e4SLinus Torvalds * first, skip white space in buffer. 26121da177e4SLinus Torvalds */ 2613e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds digit = *str; 26161da177e4SLinus Torvalds if (is_sign && digit == '-') 26171da177e4SLinus Torvalds digit = *(str + 1); 26181da177e4SLinus Torvalds 26191da177e4SLinus Torvalds if (!digit 26201da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 26211da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 26221da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 26231da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 26241da177e4SLinus Torvalds break; 26251da177e4SLinus Torvalds 262653809751SJan Beulich if (is_sign) 262753809751SJan Beulich val.s = qualifier != 'L' ? 262853809751SJan Beulich simple_strtol(str, &next, base) : 262953809751SJan Beulich simple_strtoll(str, &next, base); 263053809751SJan Beulich else 263153809751SJan Beulich val.u = qualifier != 'L' ? 263253809751SJan Beulich simple_strtoul(str, &next, base) : 263353809751SJan Beulich simple_strtoull(str, &next, base); 263453809751SJan Beulich 263553809751SJan Beulich if (field_width > 0 && next - str > field_width) { 263653809751SJan Beulich if (base == 0) 263753809751SJan Beulich _parse_integer_fixup_radix(str, &base); 263853809751SJan Beulich while (next - str > field_width) { 263953809751SJan Beulich if (is_sign) 264053809751SJan Beulich val.s = div_s64(val.s, base); 264153809751SJan Beulich else 264253809751SJan Beulich val.u = div_u64(val.u, base); 264353809751SJan Beulich --next; 264453809751SJan Beulich } 264553809751SJan Beulich } 264653809751SJan Beulich 26471da177e4SLinus Torvalds switch (qualifier) { 26481da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 264953809751SJan Beulich if (is_sign) 265053809751SJan Beulich *va_arg(args, signed char *) = val.s; 265153809751SJan Beulich else 265253809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 26531da177e4SLinus Torvalds break; 26541da177e4SLinus Torvalds case 'h': 265553809751SJan Beulich if (is_sign) 265653809751SJan Beulich *va_arg(args, short *) = val.s; 265753809751SJan Beulich else 265853809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 26591da177e4SLinus Torvalds break; 26601da177e4SLinus Torvalds case 'l': 266153809751SJan Beulich if (is_sign) 266253809751SJan Beulich *va_arg(args, long *) = val.s; 266353809751SJan Beulich else 266453809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 26651da177e4SLinus Torvalds break; 26661da177e4SLinus Torvalds case 'L': 266753809751SJan Beulich if (is_sign) 266853809751SJan Beulich *va_arg(args, long long *) = val.s; 266953809751SJan Beulich else 267053809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 26711da177e4SLinus Torvalds break; 26721da177e4SLinus Torvalds case 'Z': 26731da177e4SLinus Torvalds case 'z': 267453809751SJan Beulich *va_arg(args, size_t *) = val.u; 26751da177e4SLinus Torvalds break; 26761da177e4SLinus Torvalds default: 267753809751SJan Beulich if (is_sign) 267853809751SJan Beulich *va_arg(args, int *) = val.s; 267953809751SJan Beulich else 268053809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 26811da177e4SLinus Torvalds break; 26821da177e4SLinus Torvalds } 26831da177e4SLinus Torvalds num++; 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds if (!next) 26861da177e4SLinus Torvalds break; 26871da177e4SLinus Torvalds str = next; 26881da177e4SLinus Torvalds } 2689c6b40d16SJohannes Berg 26901da177e4SLinus Torvalds return num; 26911da177e4SLinus Torvalds } 26921da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 26931da177e4SLinus Torvalds 26941da177e4SLinus Torvalds /** 26951da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 26961da177e4SLinus Torvalds * @buf: input buffer 26971da177e4SLinus Torvalds * @fmt: formatting of buffer 26981da177e4SLinus Torvalds * @...: resulting arguments 26991da177e4SLinus Torvalds */ 27001da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 27011da177e4SLinus Torvalds { 27021da177e4SLinus Torvalds va_list args; 27031da177e4SLinus Torvalds int i; 27041da177e4SLinus Torvalds 27051da177e4SLinus Torvalds va_start(args, fmt); 27061da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 27071da177e4SLinus Torvalds va_end(args); 27087b9186f5SAndré Goddard Rosa 27091da177e4SLinus Torvalds return i; 27101da177e4SLinus Torvalds } 27111da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 2712