11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/lib/vsprintf.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * Wirzenius wrote this portably, Torvalds fucked it up :-) 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> 141da177e4SLinus Torvalds * - changed to provide snprintf and vsnprintf functions 151da177e4SLinus Torvalds * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> 161da177e4SLinus Torvalds * - scnprintf and vscnprintf 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <stdarg.h> 200d1d7a55SStephen Boyd #include <linux/clk.h> 21900cca29SGeert Uytterhoeven #include <linux/clk-provider.h> 228bc3bcc9SPaul Gortmaker #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ 231da177e4SLinus Torvalds #include <linux/types.h> 241da177e4SLinus Torvalds #include <linux/string.h> 251da177e4SLinus Torvalds #include <linux/ctype.h> 261da177e4SLinus Torvalds #include <linux/kernel.h> 270fe1ef24SLinus Torvalds #include <linux/kallsyms.h> 2853809751SJan Beulich #include <linux/math64.h> 290fe1ef24SLinus Torvalds #include <linux/uaccess.h> 30332d2e78SLinus Torvalds #include <linux/ioport.h> 314b6ccca7SAl Viro #include <linux/dcache.h> 32312b4e22SRyan Mallon #include <linux/cred.h> 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 14515e4ee7b1SMartin Kletzander * - 'n' For ignored argument 14525e4ee7b1SMartin Kletzander * 14535e4ee7b1SMartin Kletzander * ** Please update also Documentation/printk-formats.txt when making changes ** 14549ac6e44eSJoe Perches * 1455332d2e78SLinus Torvalds * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 1456332d2e78SLinus Torvalds * function pointers are really function descriptors, which contain a 1457332d2e78SLinus Torvalds * pointer to the real address. 14584d8a743cSLinus Torvalds */ 1459cf3b429bSJoe Perches static noinline_for_stack 1460cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 1461fef20d9cSFrederic Weisbecker struct printf_spec spec) 146278a8bf69SLinus Torvalds { 1463*80c9eb46SRasmus Villemoes const int default_width = 2 * sizeof(void *); 1464725fe002SGrant Likely 14659f36e2c4SKees Cook if (!ptr && *fmt != 'K') { 14665e057981SJoe Perches /* 14675e057981SJoe Perches * Print (null) with the same width as a pointer so it makes 14685e057981SJoe Perches * tabular output look nice. 14695e057981SJoe Perches */ 14705e057981SJoe Perches if (spec.field_width == -1) 1471725fe002SGrant Likely spec.field_width = default_width; 1472fef20d9cSFrederic Weisbecker return string(buf, end, "(null)", spec); 14735e057981SJoe Perches } 1474d97106abSLinus Torvalds 14750fe1ef24SLinus Torvalds switch (*fmt) { 14760fe1ef24SLinus Torvalds case 'F': 14770c8b946eSFrederic Weisbecker case 'f': 14780fe1ef24SLinus Torvalds ptr = dereference_function_descriptor(ptr); 14790fe1ef24SLinus Torvalds /* Fallthrough */ 14800fe1ef24SLinus Torvalds case 'S': 14819ac6e44eSJoe Perches case 's': 14820f77a8d3SNamhyung Kim case 'B': 1483b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 1484332d2e78SLinus Torvalds case 'R': 1485c7dabef8SBjorn Helgaas case 'r': 1486fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 148731550a16SAndy Shevchenko case 'h': 148831550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 1489dbc760bcSTejun Heo case 'b': 1490dbc760bcSTejun Heo switch (fmt[1]) { 1491dbc760bcSTejun Heo case 'l': 1492dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 1493dbc760bcSTejun Heo default: 1494dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 1495dbc760bcSTejun Heo } 14968a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 14978a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 149876597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 149976597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 15008a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 15018a27f7c9SJoe Perches case 'I': /* Formatted IP supported 15028a27f7c9SJoe Perches * 4: 1.2.3.4 15038a27f7c9SJoe Perches * 6: 0001:0203:...:0708 15048a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 15058a27f7c9SJoe Perches */ 15068a27f7c9SJoe Perches case 'i': /* Contiguous: 15078a27f7c9SJoe Perches * 4: 001.002.003.004 15088a27f7c9SJoe Perches * 6: 000102...0f 15098a27f7c9SJoe Perches */ 15108a27f7c9SJoe Perches switch (fmt[1]) { 15118a27f7c9SJoe Perches case '6': 15128a27f7c9SJoe Perches return ip6_addr_string(buf, end, ptr, spec, fmt); 15138a27f7c9SJoe Perches case '4': 15148a27f7c9SJoe Perches return ip4_addr_string(buf, end, ptr, spec, fmt); 151510679643SDaniel Borkmann case 'S': { 151610679643SDaniel Borkmann const union { 151710679643SDaniel Borkmann struct sockaddr raw; 151810679643SDaniel Borkmann struct sockaddr_in v4; 151910679643SDaniel Borkmann struct sockaddr_in6 v6; 152010679643SDaniel Borkmann } *sa = ptr; 152110679643SDaniel Borkmann 152210679643SDaniel Borkmann switch (sa->raw.sa_family) { 152310679643SDaniel Borkmann case AF_INET: 152410679643SDaniel Borkmann return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 152510679643SDaniel Borkmann case AF_INET6: 152610679643SDaniel Borkmann return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 152710679643SDaniel Borkmann default: 152810679643SDaniel Borkmann return string(buf, end, "(invalid address)", spec); 152910679643SDaniel Borkmann }} 15308a27f7c9SJoe Perches } 15314aa99606SHarvey Harrison break; 153271dca95dSAndy Shevchenko case 'E': 153371dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 15349ac6e44eSJoe Perches case 'U': 15359ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 15367db6f5fbSJoe Perches case 'V': 15375756b76eSJan Beulich { 15385756b76eSJan Beulich va_list va; 15395756b76eSJan Beulich 15405756b76eSJan Beulich va_copy(va, *((struct va_format *)ptr)->va); 15415756b76eSJan Beulich buf += vsnprintf(buf, end > buf ? end - buf : 0, 15425756b76eSJan Beulich ((struct va_format *)ptr)->fmt, va); 15435756b76eSJan Beulich va_end(va); 15445756b76eSJan Beulich return buf; 15455756b76eSJan Beulich } 1546455cd5abSDan Rosenberg case 'K': 1547455cd5abSDan Rosenberg /* 1548455cd5abSDan Rosenberg * %pK cannot be used in IRQ context because its test 1549455cd5abSDan Rosenberg * for CAP_SYSLOG would be meaningless. 1550455cd5abSDan Rosenberg */ 15513715c530SDan Rosenberg if (kptr_restrict && (in_irq() || in_serving_softirq() || 15523715c530SDan Rosenberg in_nmi())) { 1553455cd5abSDan Rosenberg if (spec.field_width == -1) 1554725fe002SGrant Likely spec.field_width = default_width; 1555455cd5abSDan Rosenberg return string(buf, end, "pK-error", spec); 1556455cd5abSDan Rosenberg } 1557312b4e22SRyan Mallon 1558312b4e22SRyan Mallon switch (kptr_restrict) { 1559312b4e22SRyan Mallon case 0: 1560312b4e22SRyan Mallon /* Always print %pK values */ 1561312b4e22SRyan Mallon break; 1562312b4e22SRyan Mallon case 1: { 1563312b4e22SRyan Mallon /* 1564312b4e22SRyan Mallon * Only print the real pointer value if the current 1565312b4e22SRyan Mallon * process has CAP_SYSLOG and is running with the 1566312b4e22SRyan Mallon * same credentials it started with. This is because 1567312b4e22SRyan Mallon * access to files is checked at open() time, but %pK 1568312b4e22SRyan Mallon * checks permission at read() time. We don't want to 1569312b4e22SRyan Mallon * leak pointer values if a binary opens a file using 1570312b4e22SRyan Mallon * %pK and then elevates privileges before reading it. 1571312b4e22SRyan Mallon */ 1572312b4e22SRyan Mallon const struct cred *cred = current_cred(); 1573312b4e22SRyan Mallon 1574312b4e22SRyan Mallon if (!has_capability_noaudit(current, CAP_SYSLOG) || 1575312b4e22SRyan Mallon !uid_eq(cred->euid, cred->uid) || 1576312b4e22SRyan Mallon !gid_eq(cred->egid, cred->gid)) 157726297607SJoe Perches ptr = NULL; 157826297607SJoe Perches break; 1579312b4e22SRyan Mallon } 1580312b4e22SRyan Mallon case 2: 1581312b4e22SRyan Mallon default: 1582312b4e22SRyan Mallon /* Always print 0's for %pK */ 1583312b4e22SRyan Mallon ptr = NULL; 1584312b4e22SRyan Mallon break; 1585312b4e22SRyan Mallon } 1586312b4e22SRyan Mallon break; 1587312b4e22SRyan Mallon 1588c8f44affSMichał Mirosław case 'N': 1589c8f44affSMichał Mirosław switch (fmt[1]) { 1590c8f44affSMichał Mirosław case 'F': 1591c8f44affSMichał Mirosław return netdev_feature_string(buf, end, ptr, spec); 1592c8f44affSMichał Mirosław } 1593c8f44affSMichał Mirosław break; 15947d799210SStepan Moskovchenko case 'a': 1595aaf07621SJoe Perches return address_val(buf, end, ptr, spec, fmt); 15964b6ccca7SAl Viro case 'd': 15974b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 1598900cca29SGeert Uytterhoeven case 'C': 1599900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 16004b6ccca7SAl Viro case 'D': 16014b6ccca7SAl Viro return dentry_name(buf, end, 16024b6ccca7SAl Viro ((const struct file *)ptr)->f_path.dentry, 16034b6ccca7SAl Viro spec, fmt); 16040fe1ef24SLinus Torvalds } 1605fef20d9cSFrederic Weisbecker spec.flags |= SMALL; 1606fef20d9cSFrederic Weisbecker if (spec.field_width == -1) { 1607725fe002SGrant Likely spec.field_width = default_width; 1608fef20d9cSFrederic Weisbecker spec.flags |= ZEROPAD; 160978a8bf69SLinus Torvalds } 1610fef20d9cSFrederic Weisbecker spec.base = 16; 1611fef20d9cSFrederic Weisbecker 1612fef20d9cSFrederic Weisbecker return number(buf, end, (unsigned long) ptr, spec); 1613fef20d9cSFrederic Weisbecker } 1614fef20d9cSFrederic Weisbecker 1615fef20d9cSFrederic Weisbecker /* 1616fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 1617fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 1618fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 1619fef20d9cSFrederic Weisbecker * to go on the next call). 1620fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 1621fef20d9cSFrederic Weisbecker * 1622fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 1623fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 1624fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 1625fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 1626fef20d9cSFrederic Weisbecker * 1627fef20d9cSFrederic Weisbecker * @fmt: the format string 1628fef20d9cSFrederic Weisbecker * @type of the token returned 1629fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 1630fef20d9cSFrederic Weisbecker * @field_width: overwritten width 1631fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 1632fef20d9cSFrederic Weisbecker * @precision: precision of a number 1633fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 1634fef20d9cSFrederic Weisbecker */ 1635cf3b429bSJoe Perches static noinline_for_stack 1636cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 1637fef20d9cSFrederic Weisbecker { 1638fef20d9cSFrederic Weisbecker const char *start = fmt; 1639fef20d9cSFrederic Weisbecker 1640fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 1641ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 1642fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 1643fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 1644fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 1645fef20d9cSFrederic Weisbecker } 1646fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1647fef20d9cSFrederic Weisbecker goto precision; 1648fef20d9cSFrederic Weisbecker } 1649fef20d9cSFrederic Weisbecker 1650fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 1651fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 1652fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1653fef20d9cSFrederic Weisbecker spec->precision = 0; 1654fef20d9cSFrederic Weisbecker 1655fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1656fef20d9cSFrederic Weisbecker goto qualifier; 1657fef20d9cSFrederic Weisbecker } 1658fef20d9cSFrederic Weisbecker 1659fef20d9cSFrederic Weisbecker /* By default */ 1660fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1661fef20d9cSFrederic Weisbecker 1662fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 1663fef20d9cSFrederic Weisbecker if (*fmt == '%') 1664fef20d9cSFrederic Weisbecker break; 1665fef20d9cSFrederic Weisbecker } 1666fef20d9cSFrederic Weisbecker 1667fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 1668fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 1669fef20d9cSFrederic Weisbecker return fmt - start; 1670fef20d9cSFrederic Weisbecker 1671fef20d9cSFrederic Weisbecker /* Process flags */ 1672fef20d9cSFrederic Weisbecker spec->flags = 0; 1673fef20d9cSFrederic Weisbecker 1674fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 1675fef20d9cSFrederic Weisbecker bool found = true; 1676fef20d9cSFrederic Weisbecker 1677fef20d9cSFrederic Weisbecker ++fmt; 1678fef20d9cSFrederic Weisbecker 1679fef20d9cSFrederic Weisbecker switch (*fmt) { 1680fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 1681fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 1682fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 1683fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 1684fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 1685fef20d9cSFrederic Weisbecker default: found = false; 1686fef20d9cSFrederic Weisbecker } 1687fef20d9cSFrederic Weisbecker 1688fef20d9cSFrederic Weisbecker if (!found) 1689fef20d9cSFrederic Weisbecker break; 1690fef20d9cSFrederic Weisbecker } 1691fef20d9cSFrederic Weisbecker 1692fef20d9cSFrederic Weisbecker /* get field width */ 1693fef20d9cSFrederic Weisbecker spec->field_width = -1; 1694fef20d9cSFrederic Weisbecker 1695fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 1696fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 1697fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 1698fef20d9cSFrederic Weisbecker /* it's the next argument */ 1699ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 1700fef20d9cSFrederic Weisbecker return ++fmt - start; 1701fef20d9cSFrederic Weisbecker } 1702fef20d9cSFrederic Weisbecker 1703fef20d9cSFrederic Weisbecker precision: 1704fef20d9cSFrederic Weisbecker /* get the precision */ 1705fef20d9cSFrederic Weisbecker spec->precision = -1; 1706fef20d9cSFrederic Weisbecker if (*fmt == '.') { 1707fef20d9cSFrederic Weisbecker ++fmt; 1708fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 1709fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 1710fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1711fef20d9cSFrederic Weisbecker spec->precision = 0; 1712fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 1713fef20d9cSFrederic Weisbecker /* it's the next argument */ 1714adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 1715fef20d9cSFrederic Weisbecker return ++fmt - start; 1716fef20d9cSFrederic Weisbecker } 1717fef20d9cSFrederic Weisbecker } 1718fef20d9cSFrederic Weisbecker 1719fef20d9cSFrederic Weisbecker qualifier: 1720fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 1721fef20d9cSFrederic Weisbecker spec->qualifier = -1; 172275fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 172375fb8f26SAndy Shevchenko _tolower(*fmt) == 'z' || *fmt == 't') { 1724a4e94ef0SZhaolei spec->qualifier = *fmt++; 1725a4e94ef0SZhaolei if (unlikely(spec->qualifier == *fmt)) { 1726a4e94ef0SZhaolei if (spec->qualifier == 'l') { 1727fef20d9cSFrederic Weisbecker spec->qualifier = 'L'; 1728fef20d9cSFrederic Weisbecker ++fmt; 1729a4e94ef0SZhaolei } else if (spec->qualifier == 'h') { 1730a4e94ef0SZhaolei spec->qualifier = 'H'; 1731a4e94ef0SZhaolei ++fmt; 1732a4e94ef0SZhaolei } 1733fef20d9cSFrederic Weisbecker } 1734fef20d9cSFrederic Weisbecker } 1735fef20d9cSFrederic Weisbecker 1736fef20d9cSFrederic Weisbecker /* default base */ 1737fef20d9cSFrederic Weisbecker spec->base = 10; 1738fef20d9cSFrederic Weisbecker switch (*fmt) { 1739fef20d9cSFrederic Weisbecker case 'c': 1740fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 1741fef20d9cSFrederic Weisbecker return ++fmt - start; 1742fef20d9cSFrederic Weisbecker 1743fef20d9cSFrederic Weisbecker case 's': 1744fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 1745fef20d9cSFrederic Weisbecker return ++fmt - start; 1746fef20d9cSFrederic Weisbecker 1747fef20d9cSFrederic Weisbecker case 'p': 1748fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 1749ffbfed03SRasmus Villemoes return ++fmt - start; 1750fef20d9cSFrederic Weisbecker 1751fef20d9cSFrederic Weisbecker case '%': 1752fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 1753fef20d9cSFrederic Weisbecker return ++fmt - start; 1754fef20d9cSFrederic Weisbecker 1755fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 1756fef20d9cSFrederic Weisbecker case 'o': 1757fef20d9cSFrederic Weisbecker spec->base = 8; 1758fef20d9cSFrederic Weisbecker break; 1759fef20d9cSFrederic Weisbecker 1760fef20d9cSFrederic Weisbecker case 'x': 1761fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 1762fef20d9cSFrederic Weisbecker 1763fef20d9cSFrederic Weisbecker case 'X': 1764fef20d9cSFrederic Weisbecker spec->base = 16; 1765fef20d9cSFrederic Weisbecker break; 1766fef20d9cSFrederic Weisbecker 1767fef20d9cSFrederic Weisbecker case 'd': 1768fef20d9cSFrederic Weisbecker case 'i': 176939e874f8SFrederic Weisbecker spec->flags |= SIGN; 1770fef20d9cSFrederic Weisbecker case 'u': 1771fef20d9cSFrederic Weisbecker break; 1772fef20d9cSFrederic Weisbecker 1773708d96fdSRyan Mallon case 'n': 1774708d96fdSRyan Mallon /* 1775b006f19bSRasmus Villemoes * Since %n poses a greater security risk than 1776b006f19bSRasmus Villemoes * utility, treat it as any other invalid or 1777b006f19bSRasmus Villemoes * unsupported format specifier. 1778708d96fdSRyan Mallon */ 1779708d96fdSRyan Mallon /* Fall-through */ 1780708d96fdSRyan Mallon 1781fef20d9cSFrederic Weisbecker default: 1782b006f19bSRasmus Villemoes WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 1783fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 1784fef20d9cSFrederic Weisbecker return fmt - start; 1785fef20d9cSFrederic Weisbecker } 1786fef20d9cSFrederic Weisbecker 1787fef20d9cSFrederic Weisbecker if (spec->qualifier == 'L') 1788fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 1789fef20d9cSFrederic Weisbecker else if (spec->qualifier == 'l') { 179051be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 179151be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 179275fb8f26SAndy Shevchenko } else if (_tolower(spec->qualifier) == 'z') { 1793fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 1794fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 't') { 1795fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 1796a4e94ef0SZhaolei } else if (spec->qualifier == 'H') { 179751be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 179851be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 1799fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 'h') { 180051be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 180151be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 1802fef20d9cSFrederic Weisbecker } else { 180351be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 180451be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 1805fef20d9cSFrederic Weisbecker } 1806fef20d9cSFrederic Weisbecker 1807fef20d9cSFrederic Weisbecker return ++fmt - start; 180878a8bf69SLinus Torvalds } 180978a8bf69SLinus Torvalds 18101da177e4SLinus Torvalds /** 18111da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 18121da177e4SLinus Torvalds * @buf: The buffer to place the result into 18131da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 18141da177e4SLinus Torvalds * @fmt: The format string to use 18151da177e4SLinus Torvalds * @args: Arguments for the format string 18161da177e4SLinus Torvalds * 181720036fdcSAndi Kleen * This function follows C99 vsnprintf, but has some extensions: 18180efb4d20SSteven Rostedt * %n is ignored 18195e4ee7b1SMartin Kletzander * %p* is handled by pointer() 182020036fdcSAndi Kleen * 18215e4ee7b1SMartin Kletzander * See pointer() or Documentation/printk-formats.txt for more 18225e4ee7b1SMartin Kletzander * extensive description. 18235e4ee7b1SMartin Kletzander * 18245e4ee7b1SMartin Kletzander * ** Please update the documentation in both places when making changes ** 182580f548e0SAndrew Morton * 18261da177e4SLinus Torvalds * The return value is the number of characters which would 18271da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 18281da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 18291da177e4SLinus Torvalds * number of characters written into @buf as return value 183072fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 18311da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 18321da177e4SLinus Torvalds * string is truncated. 18331da177e4SLinus Torvalds * 1834ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 18351da177e4SLinus Torvalds */ 18361da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 18371da177e4SLinus Torvalds { 18381da177e4SLinus Torvalds unsigned long long num; 1839d4be151bSAndré Goddard Rosa char *str, *end; 1840fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 18411da177e4SLinus Torvalds 1842f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 1843f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 18442aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 18451da177e4SLinus Torvalds return 0; 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds str = buf; 1848f796937aSJeremy Fitzhardinge end = buf + size; 18491da177e4SLinus Torvalds 1850f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 1851f796937aSJeremy Fitzhardinge if (end < buf) { 18521da177e4SLinus Torvalds end = ((void *)-1); 1853f796937aSJeremy Fitzhardinge size = end - buf; 18541da177e4SLinus Torvalds } 18551da177e4SLinus Torvalds 1856fef20d9cSFrederic Weisbecker while (*fmt) { 1857fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1858d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 1859fef20d9cSFrederic Weisbecker 1860fef20d9cSFrederic Weisbecker fmt += read; 1861fef20d9cSFrederic Weisbecker 1862fef20d9cSFrederic Weisbecker switch (spec.type) { 1863fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1864fef20d9cSFrederic Weisbecker int copy = read; 1865fef20d9cSFrederic Weisbecker if (str < end) { 1866fef20d9cSFrederic Weisbecker if (copy > end - str) 1867fef20d9cSFrederic Weisbecker copy = end - str; 1868fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1869fef20d9cSFrederic Weisbecker } 1870fef20d9cSFrederic Weisbecker str += read; 1871fef20d9cSFrederic Weisbecker break; 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 1874ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1875fef20d9cSFrederic Weisbecker spec.field_width = va_arg(args, int); 1876fef20d9cSFrederic Weisbecker break; 18771da177e4SLinus Torvalds 1878fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1879fef20d9cSFrederic Weisbecker spec.precision = va_arg(args, int); 1880fef20d9cSFrederic Weisbecker break; 18811da177e4SLinus Torvalds 1882d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 1883d4be151bSAndré Goddard Rosa char c; 1884d4be151bSAndré Goddard Rosa 1885fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1886fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1887f796937aSJeremy Fitzhardinge if (str < end) 18881da177e4SLinus Torvalds *str = ' '; 18891da177e4SLinus Torvalds ++str; 1890fef20d9cSFrederic Weisbecker 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 1894f796937aSJeremy Fitzhardinge if (str < end) 18951da177e4SLinus Torvalds *str = c; 18961da177e4SLinus Torvalds ++str; 1897fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1898f796937aSJeremy Fitzhardinge if (str < end) 18991da177e4SLinus Torvalds *str = ' '; 19001da177e4SLinus Torvalds ++str; 19011da177e4SLinus Torvalds } 1902fef20d9cSFrederic Weisbecker break; 1903d4be151bSAndré Goddard Rosa } 19041da177e4SLinus Torvalds 1905fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 1906fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 1907fef20d9cSFrederic Weisbecker break; 19081da177e4SLinus Torvalds 1909fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1910ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 1911fef20d9cSFrederic Weisbecker spec); 1912fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 19134d8a743cSLinus Torvalds fmt++; 1914fef20d9cSFrederic Weisbecker break; 19151da177e4SLinus Torvalds 1916fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1917f796937aSJeremy Fitzhardinge if (str < end) 19181da177e4SLinus Torvalds *str = '%'; 19191da177e4SLinus Torvalds ++str; 19201da177e4SLinus Torvalds break; 19211da177e4SLinus Torvalds 1922fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 1923b006f19bSRasmus Villemoes /* 1924b006f19bSRasmus Villemoes * Presumably the arguments passed gcc's type 1925b006f19bSRasmus Villemoes * checking, but there is no safe or sane way 1926b006f19bSRasmus Villemoes * for us to continue parsing the format and 1927b006f19bSRasmus Villemoes * fetching from the va_list; the remaining 1928b006f19bSRasmus Villemoes * specifiers and arguments would be out of 1929b006f19bSRasmus Villemoes * sync. 1930b006f19bSRasmus Villemoes */ 1931b006f19bSRasmus Villemoes goto out; 1932fef20d9cSFrederic Weisbecker 1933fef20d9cSFrederic Weisbecker default: 1934fef20d9cSFrederic Weisbecker switch (spec.type) { 1935fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1936fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 1937fef20d9cSFrederic Weisbecker break; 1938fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1939fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 1940fef20d9cSFrederic Weisbecker break; 1941fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1942fef20d9cSFrederic Weisbecker num = va_arg(args, long); 1943fef20d9cSFrederic Weisbecker break; 1944fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1945ef124960SJason Gunthorpe if (spec.flags & SIGN) 1946ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 1947ef124960SJason Gunthorpe else 1948fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 1949fef20d9cSFrederic Weisbecker break; 1950fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1951fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 1952fef20d9cSFrederic Weisbecker break; 1953a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1954a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 1955a4e94ef0SZhaolei break; 1956a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1957a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 1958a4e94ef0SZhaolei break; 1959fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1960fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 1961fef20d9cSFrederic Weisbecker break; 1962fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1963fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 1964fef20d9cSFrederic Weisbecker break; 196539e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 196639e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 1967fef20d9cSFrederic Weisbecker break; 1968fef20d9cSFrederic Weisbecker default: 1969fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 19701da177e4SLinus Torvalds } 1971fef20d9cSFrederic Weisbecker 1972fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 19731da177e4SLinus Torvalds } 1974fef20d9cSFrederic Weisbecker } 1975fef20d9cSFrederic Weisbecker 1976b006f19bSRasmus Villemoes out: 1977f796937aSJeremy Fitzhardinge if (size > 0) { 1978f796937aSJeremy Fitzhardinge if (str < end) 19791da177e4SLinus Torvalds *str = '\0'; 1980f796937aSJeremy Fitzhardinge else 19810a6047eeSLinus Torvalds end[-1] = '\0'; 1982f796937aSJeremy Fitzhardinge } 1983fef20d9cSFrederic Weisbecker 1984f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 19851da177e4SLinus Torvalds return str-buf; 1986fef20d9cSFrederic Weisbecker 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds /** 19911da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 19921da177e4SLinus Torvalds * @buf: The buffer to place the result into 19931da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 19941da177e4SLinus Torvalds * @fmt: The format string to use 19951da177e4SLinus Torvalds * @args: Arguments for the format string 19961da177e4SLinus Torvalds * 19971da177e4SLinus Torvalds * The return value is the number of characters which have been written into 1998b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 19991da177e4SLinus Torvalds * returns 0. 20001da177e4SLinus Torvalds * 2001ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 200220036fdcSAndi Kleen * 200320036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 20041da177e4SLinus Torvalds */ 20051da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 20061da177e4SLinus Torvalds { 20071da177e4SLinus Torvalds int i; 20081da177e4SLinus Torvalds 20091da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 20107b9186f5SAndré Goddard Rosa 2011b921c69fSAnton Arapov if (likely(i < size)) 2012b921c69fSAnton Arapov return i; 2013b921c69fSAnton Arapov if (size != 0) 2014b921c69fSAnton Arapov return size - 1; 2015b921c69fSAnton Arapov return 0; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 20181da177e4SLinus Torvalds 20191da177e4SLinus Torvalds /** 20201da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 20211da177e4SLinus Torvalds * @buf: The buffer to place the result into 20221da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 20231da177e4SLinus Torvalds * @fmt: The format string to use 20241da177e4SLinus Torvalds * @...: Arguments for the format string 20251da177e4SLinus Torvalds * 20261da177e4SLinus Torvalds * The return value is the number of characters which would be 20271da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 20281da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 20291da177e4SLinus Torvalds * @size, the resulting string is truncated. 203020036fdcSAndi Kleen * 203120036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 20321da177e4SLinus Torvalds */ 20331da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 20341da177e4SLinus Torvalds { 20351da177e4SLinus Torvalds va_list args; 20361da177e4SLinus Torvalds int i; 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds va_start(args, fmt); 20391da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 20401da177e4SLinus Torvalds va_end(args); 20417b9186f5SAndré Goddard Rosa 20421da177e4SLinus Torvalds return i; 20431da177e4SLinus Torvalds } 20441da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds /** 20471da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 20481da177e4SLinus Torvalds * @buf: The buffer to place the result into 20491da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 20501da177e4SLinus Torvalds * @fmt: The format string to use 20511da177e4SLinus Torvalds * @...: Arguments for the format string 20521da177e4SLinus Torvalds * 20531da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2054b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 20551da177e4SLinus Torvalds */ 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 20581da177e4SLinus Torvalds { 20591da177e4SLinus Torvalds va_list args; 20601da177e4SLinus Torvalds int i; 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds va_start(args, fmt); 2063b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 20641da177e4SLinus Torvalds va_end(args); 20657b9186f5SAndré Goddard Rosa 2066b903c0b8SChangli Gao return i; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds /** 20711da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 20721da177e4SLinus Torvalds * @buf: The buffer to place the result into 20731da177e4SLinus Torvalds * @fmt: The format string to use 20741da177e4SLinus Torvalds * @args: Arguments for the format string 20751da177e4SLinus Torvalds * 20761da177e4SLinus Torvalds * The function returns the number of characters written 207772fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 20781da177e4SLinus Torvalds * buffer overflows. 20791da177e4SLinus Torvalds * 2080ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 208120036fdcSAndi Kleen * 208220036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 20831da177e4SLinus Torvalds */ 20841da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 20851da177e4SLinus Torvalds { 20861da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 20891da177e4SLinus Torvalds 20901da177e4SLinus Torvalds /** 20911da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 20921da177e4SLinus Torvalds * @buf: The buffer to place the result into 20931da177e4SLinus Torvalds * @fmt: The format string to use 20941da177e4SLinus Torvalds * @...: Arguments for the format string 20951da177e4SLinus Torvalds * 20961da177e4SLinus Torvalds * The function returns the number of characters written 209772fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 20981da177e4SLinus Torvalds * buffer overflows. 209920036fdcSAndi Kleen * 210020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 21011da177e4SLinus Torvalds */ 21021da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 21031da177e4SLinus Torvalds { 21041da177e4SLinus Torvalds va_list args; 21051da177e4SLinus Torvalds int i; 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds va_start(args, fmt); 21081da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 21091da177e4SLinus Torvalds va_end(args); 21107b9186f5SAndré Goddard Rosa 21111da177e4SLinus Torvalds return i; 21121da177e4SLinus Torvalds } 21131da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 21141da177e4SLinus Torvalds 21154370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 21164370aa4aSLai Jiangshan /* 21174370aa4aSLai Jiangshan * bprintf service: 21184370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 21194370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 21204370aa4aSLai Jiangshan */ 21214370aa4aSLai Jiangshan 21224370aa4aSLai Jiangshan /** 21234370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 21244370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 21254370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 21264370aa4aSLai Jiangshan * @fmt: The format string to use 21274370aa4aSLai Jiangshan * @args: Arguments for the format string 21284370aa4aSLai Jiangshan * 21294370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 2130da3dae54SMasanari Iida * is skipped. 21314370aa4aSLai Jiangshan * 21324370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 21334370aa4aSLai Jiangshan * the given input. 21344370aa4aSLai Jiangshan * 21354370aa4aSLai Jiangshan * NOTE: 21364370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 21374370aa4aSLai Jiangshan * valid for bstr_printf(). 21384370aa4aSLai Jiangshan */ 21394370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 21404370aa4aSLai Jiangshan { 2141fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 21424370aa4aSLai Jiangshan char *str, *end; 21434370aa4aSLai Jiangshan 21444370aa4aSLai Jiangshan str = (char *)bin_buf; 21454370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 21464370aa4aSLai Jiangshan 21474370aa4aSLai Jiangshan #define save_arg(type) \ 21484370aa4aSLai Jiangshan do { \ 21494370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 21504370aa4aSLai Jiangshan unsigned long long value; \ 21514370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 21524370aa4aSLai Jiangshan value = va_arg(args, unsigned long long); \ 21534370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 21544370aa4aSLai Jiangshan *(u32 *)str = *(u32 *)&value; \ 21554370aa4aSLai Jiangshan *(u32 *)(str + 4) = *((u32 *)&value + 1); \ 21564370aa4aSLai Jiangshan } \ 21574370aa4aSLai Jiangshan } else { \ 21584370aa4aSLai Jiangshan unsigned long value; \ 21594370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 21604370aa4aSLai Jiangshan value = va_arg(args, int); \ 21614370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 21624370aa4aSLai Jiangshan *(typeof(type) *)str = (type)value; \ 21634370aa4aSLai Jiangshan } \ 21644370aa4aSLai Jiangshan str += sizeof(type); \ 21654370aa4aSLai Jiangshan } while (0) 21664370aa4aSLai Jiangshan 2167fef20d9cSFrederic Weisbecker while (*fmt) { 2168d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 21694370aa4aSLai Jiangshan 2170fef20d9cSFrederic Weisbecker fmt += read; 2171fef20d9cSFrederic Weisbecker 2172fef20d9cSFrederic Weisbecker switch (spec.type) { 2173fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 2174d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 2175fef20d9cSFrederic Weisbecker break; 2176b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2177b006f19bSRasmus Villemoes goto out; 2178fef20d9cSFrederic Weisbecker 2179ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2180fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 21814370aa4aSLai Jiangshan save_arg(int); 2182fef20d9cSFrederic Weisbecker break; 21834370aa4aSLai Jiangshan 2184fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 21854370aa4aSLai Jiangshan save_arg(char); 2186fef20d9cSFrederic Weisbecker break; 2187fef20d9cSFrederic Weisbecker 2188fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 21894370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 21904370aa4aSLai Jiangshan size_t len; 21916c356634SAndré Goddard Rosa 21924370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 21934370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 21940f4f81dcSAndré Goddard Rosa save_str = "(null)"; 21956c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 21966c356634SAndré Goddard Rosa if (str + len < end) 21976c356634SAndré Goddard Rosa memcpy(str, save_str, len); 21986c356634SAndré Goddard Rosa str += len; 2199fef20d9cSFrederic Weisbecker break; 22004370aa4aSLai Jiangshan } 2201fef20d9cSFrederic Weisbecker 2202fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 22034370aa4aSLai Jiangshan save_arg(void *); 22044370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2205fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 22064370aa4aSLai Jiangshan fmt++; 2207fef20d9cSFrederic Weisbecker break; 2208fef20d9cSFrederic Weisbecker 2209fef20d9cSFrederic Weisbecker default: 2210fef20d9cSFrederic Weisbecker switch (spec.type) { 2211fef20d9cSFrederic Weisbecker 2212fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2213fef20d9cSFrederic Weisbecker save_arg(long long); 2214fef20d9cSFrederic Weisbecker break; 2215fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2216fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2217fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2218fef20d9cSFrederic Weisbecker break; 2219fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2220fef20d9cSFrederic Weisbecker save_arg(size_t); 2221fef20d9cSFrederic Weisbecker break; 2222fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2223fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2224fef20d9cSFrederic Weisbecker break; 2225a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2226a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2227a4e94ef0SZhaolei save_arg(char); 2228a4e94ef0SZhaolei break; 2229fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2230fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2231fef20d9cSFrederic Weisbecker save_arg(short); 2232fef20d9cSFrederic Weisbecker break; 2233fef20d9cSFrederic Weisbecker default: 2234fef20d9cSFrederic Weisbecker save_arg(int); 2235fef20d9cSFrederic Weisbecker } 2236fef20d9cSFrederic Weisbecker } 2237fef20d9cSFrederic Weisbecker } 2238fef20d9cSFrederic Weisbecker 2239b006f19bSRasmus Villemoes out: 22407b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2241fef20d9cSFrederic Weisbecker #undef save_arg 22424370aa4aSLai Jiangshan } 22434370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 22444370aa4aSLai Jiangshan 22454370aa4aSLai Jiangshan /** 22464370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 22474370aa4aSLai Jiangshan * @buf: The buffer to place the result into 22484370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 22494370aa4aSLai Jiangshan * @fmt: The format string to use 22504370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 22514370aa4aSLai Jiangshan * 22524370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 22534370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 22544370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 22554370aa4aSLai Jiangshan * 22564370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 22570efb4d20SSteven Rostedt * see vsnprintf comment for details. 22584370aa4aSLai Jiangshan * 22594370aa4aSLai Jiangshan * The return value is the number of characters which would 22604370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 22614370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 22624370aa4aSLai Jiangshan * number of characters written into @buf as return value 22634370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 22644370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 22654370aa4aSLai Jiangshan * string is truncated. 22664370aa4aSLai Jiangshan */ 22674370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 22684370aa4aSLai Jiangshan { 2269fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 2270d4be151bSAndré Goddard Rosa char *str, *end; 2271d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 22724370aa4aSLai Jiangshan 2273762abb51SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 22744370aa4aSLai Jiangshan return 0; 22754370aa4aSLai Jiangshan 22764370aa4aSLai Jiangshan str = buf; 22774370aa4aSLai Jiangshan end = buf + size; 22784370aa4aSLai Jiangshan 22794370aa4aSLai Jiangshan #define get_arg(type) \ 22804370aa4aSLai Jiangshan ({ \ 22814370aa4aSLai Jiangshan typeof(type) value; \ 22824370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 22834370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 22844370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 22854370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 22864370aa4aSLai Jiangshan } else { \ 22874370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 22884370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 22894370aa4aSLai Jiangshan } \ 22904370aa4aSLai Jiangshan args += sizeof(type); \ 22914370aa4aSLai Jiangshan value; \ 22924370aa4aSLai Jiangshan }) 22934370aa4aSLai Jiangshan 22944370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 22954370aa4aSLai Jiangshan if (end < buf) { 22964370aa4aSLai Jiangshan end = ((void *)-1); 22974370aa4aSLai Jiangshan size = end - buf; 22984370aa4aSLai Jiangshan } 22994370aa4aSLai Jiangshan 2300fef20d9cSFrederic Weisbecker while (*fmt) { 2301fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2302d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2303fef20d9cSFrederic Weisbecker 2304fef20d9cSFrederic Weisbecker fmt += read; 2305fef20d9cSFrederic Weisbecker 2306fef20d9cSFrederic Weisbecker switch (spec.type) { 2307fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2308fef20d9cSFrederic Weisbecker int copy = read; 2309fef20d9cSFrederic Weisbecker if (str < end) { 2310fef20d9cSFrederic Weisbecker if (copy > end - str) 2311fef20d9cSFrederic Weisbecker copy = end - str; 2312fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2313fef20d9cSFrederic Weisbecker } 2314fef20d9cSFrederic Weisbecker str += read; 2315fef20d9cSFrederic Weisbecker break; 23164370aa4aSLai Jiangshan } 23174370aa4aSLai Jiangshan 2318ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2319fef20d9cSFrederic Weisbecker spec.field_width = get_arg(int); 2320fef20d9cSFrederic Weisbecker break; 23214370aa4aSLai Jiangshan 2322fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2323fef20d9cSFrederic Weisbecker spec.precision = get_arg(int); 2324fef20d9cSFrederic Weisbecker break; 23254370aa4aSLai Jiangshan 2326d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2327d4be151bSAndré Goddard Rosa char c; 2328d4be151bSAndré Goddard Rosa 2329fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2330fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 23314370aa4aSLai Jiangshan if (str < end) 23324370aa4aSLai Jiangshan *str = ' '; 23334370aa4aSLai Jiangshan ++str; 23344370aa4aSLai Jiangshan } 23354370aa4aSLai Jiangshan } 23364370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 23374370aa4aSLai Jiangshan if (str < end) 23384370aa4aSLai Jiangshan *str = c; 23394370aa4aSLai Jiangshan ++str; 2340fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 23414370aa4aSLai Jiangshan if (str < end) 23424370aa4aSLai Jiangshan *str = ' '; 23434370aa4aSLai Jiangshan ++str; 23444370aa4aSLai Jiangshan } 2345fef20d9cSFrederic Weisbecker break; 2346d4be151bSAndré Goddard Rosa } 23474370aa4aSLai Jiangshan 2348fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 23494370aa4aSLai Jiangshan const char *str_arg = args; 2350d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 2351fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 2352fef20d9cSFrederic Weisbecker break; 23534370aa4aSLai Jiangshan } 23544370aa4aSLai Jiangshan 2355fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2356ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 2357fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 23584370aa4aSLai Jiangshan fmt++; 2359fef20d9cSFrederic Weisbecker break; 23604370aa4aSLai Jiangshan 2361fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 23624370aa4aSLai Jiangshan if (str < end) 23634370aa4aSLai Jiangshan *str = '%'; 23644370aa4aSLai Jiangshan ++str; 2365fef20d9cSFrederic Weisbecker break; 2366fef20d9cSFrederic Weisbecker 2367b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2368b006f19bSRasmus Villemoes goto out; 2369b006f19bSRasmus Villemoes 2370d4be151bSAndré Goddard Rosa default: { 2371d4be151bSAndré Goddard Rosa unsigned long long num; 2372d4be151bSAndré Goddard Rosa 2373fef20d9cSFrederic Weisbecker switch (spec.type) { 2374fef20d9cSFrederic Weisbecker 2375fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 23764370aa4aSLai Jiangshan num = get_arg(long long); 2377fef20d9cSFrederic Weisbecker break; 2378fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2379fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2380fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 2381fef20d9cSFrederic Weisbecker break; 2382fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 23834370aa4aSLai Jiangshan num = get_arg(size_t); 2384fef20d9cSFrederic Weisbecker break; 2385fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 23864370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 2387fef20d9cSFrederic Weisbecker break; 2388a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2389a4e94ef0SZhaolei num = get_arg(unsigned char); 2390a4e94ef0SZhaolei break; 2391a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2392a4e94ef0SZhaolei num = get_arg(signed char); 2393a4e94ef0SZhaolei break; 2394fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2395fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 2396fef20d9cSFrederic Weisbecker break; 2397fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2398fef20d9cSFrederic Weisbecker num = get_arg(short); 2399fef20d9cSFrederic Weisbecker break; 2400fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 24014370aa4aSLai Jiangshan num = get_arg(unsigned int); 2402fef20d9cSFrederic Weisbecker break; 2403fef20d9cSFrederic Weisbecker default: 2404fef20d9cSFrederic Weisbecker num = get_arg(int); 24054370aa4aSLai Jiangshan } 2406fef20d9cSFrederic Weisbecker 2407fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 2408d4be151bSAndré Goddard Rosa } /* default: */ 2409d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 2410d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 2411fef20d9cSFrederic Weisbecker 2412b006f19bSRasmus Villemoes out: 24134370aa4aSLai Jiangshan if (size > 0) { 24144370aa4aSLai Jiangshan if (str < end) 24154370aa4aSLai Jiangshan *str = '\0'; 24164370aa4aSLai Jiangshan else 24174370aa4aSLai Jiangshan end[-1] = '\0'; 24184370aa4aSLai Jiangshan } 2419fef20d9cSFrederic Weisbecker 24204370aa4aSLai Jiangshan #undef get_arg 24214370aa4aSLai Jiangshan 24224370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 24234370aa4aSLai Jiangshan return str - buf; 24244370aa4aSLai Jiangshan } 24254370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 24264370aa4aSLai Jiangshan 24274370aa4aSLai Jiangshan /** 24284370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 24294370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 24304370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 24314370aa4aSLai Jiangshan * @fmt: The format string to use 24324370aa4aSLai Jiangshan * @...: Arguments for the format string 24334370aa4aSLai Jiangshan * 24344370aa4aSLai Jiangshan * The function returns the number of words(u32) written 24354370aa4aSLai Jiangshan * into @bin_buf. 24364370aa4aSLai Jiangshan */ 24374370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 24384370aa4aSLai Jiangshan { 24394370aa4aSLai Jiangshan va_list args; 24404370aa4aSLai Jiangshan int ret; 24414370aa4aSLai Jiangshan 24424370aa4aSLai Jiangshan va_start(args, fmt); 24434370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 24444370aa4aSLai Jiangshan va_end(args); 24457b9186f5SAndré Goddard Rosa 24464370aa4aSLai Jiangshan return ret; 24474370aa4aSLai Jiangshan } 24484370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 24494370aa4aSLai Jiangshan 24504370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 24514370aa4aSLai Jiangshan 24521da177e4SLinus Torvalds /** 24531da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 24541da177e4SLinus Torvalds * @buf: input buffer 24551da177e4SLinus Torvalds * @fmt: format of buffer 24561da177e4SLinus Torvalds * @args: arguments 24571da177e4SLinus Torvalds */ 24581da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 24591da177e4SLinus Torvalds { 24601da177e4SLinus Torvalds const char *str = buf; 24611da177e4SLinus Torvalds char *next; 24621da177e4SLinus Torvalds char digit; 24631da177e4SLinus Torvalds int num = 0; 2464ef0658f3SJoe Perches u8 qualifier; 246553809751SJan Beulich unsigned int base; 246653809751SJan Beulich union { 246753809751SJan Beulich long long s; 246853809751SJan Beulich unsigned long long u; 246953809751SJan Beulich } val; 2470ef0658f3SJoe Perches s16 field_width; 2471d4be151bSAndré Goddard Rosa bool is_sign; 24721da177e4SLinus Torvalds 2473da99075cSJan Beulich while (*fmt) { 24741da177e4SLinus Torvalds /* skip any white space in format */ 24751da177e4SLinus Torvalds /* white space in format matchs any amount of 24761da177e4SLinus Torvalds * white space, including none, in the input. 24771da177e4SLinus Torvalds */ 24781da177e4SLinus Torvalds if (isspace(*fmt)) { 2479e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 2480e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 24811da177e4SLinus Torvalds } 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 24841da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 24851da177e4SLinus Torvalds if (*fmt++ != *str++) 24861da177e4SLinus Torvalds break; 24871da177e4SLinus Torvalds continue; 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds if (!*fmt) 24911da177e4SLinus Torvalds break; 24921da177e4SLinus Torvalds ++fmt; 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds /* skip this conversion. 24951da177e4SLinus Torvalds * advance both strings to next white space 24961da177e4SLinus Torvalds */ 24971da177e4SLinus Torvalds if (*fmt == '*') { 2498da99075cSJan Beulich if (!*str) 2499da99075cSJan Beulich break; 25008fccae2cSAndy Spencer while (!isspace(*fmt) && *fmt != '%' && *fmt) 25011da177e4SLinus Torvalds fmt++; 25021da177e4SLinus Torvalds while (!isspace(*str) && *str) 25031da177e4SLinus Torvalds str++; 25041da177e4SLinus Torvalds continue; 25051da177e4SLinus Torvalds } 25061da177e4SLinus Torvalds 25071da177e4SLinus Torvalds /* get field width */ 25081da177e4SLinus Torvalds field_width = -1; 250953809751SJan Beulich if (isdigit(*fmt)) { 25101da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 251153809751SJan Beulich if (field_width <= 0) 251253809751SJan Beulich break; 251353809751SJan Beulich } 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds /* get conversion qualifier */ 25161da177e4SLinus Torvalds qualifier = -1; 251775fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 251875fb8f26SAndy Shevchenko _tolower(*fmt) == 'z') { 25191da177e4SLinus Torvalds qualifier = *fmt++; 25201da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 25211da177e4SLinus Torvalds if (qualifier == 'h') { 25221da177e4SLinus Torvalds qualifier = 'H'; 25231da177e4SLinus Torvalds fmt++; 25241da177e4SLinus Torvalds } else if (qualifier == 'l') { 25251da177e4SLinus Torvalds qualifier = 'L'; 25261da177e4SLinus Torvalds fmt++; 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds } 25291da177e4SLinus Torvalds } 25301da177e4SLinus Torvalds 2531da99075cSJan Beulich if (!*fmt) 2532da99075cSJan Beulich break; 2533da99075cSJan Beulich 2534da99075cSJan Beulich if (*fmt == 'n') { 2535da99075cSJan Beulich /* return number of characters read so far */ 2536da99075cSJan Beulich *va_arg(args, int *) = str - buf; 2537da99075cSJan Beulich ++fmt; 2538da99075cSJan Beulich continue; 2539da99075cSJan Beulich } 2540da99075cSJan Beulich 2541da99075cSJan Beulich if (!*str) 25421da177e4SLinus Torvalds break; 25431da177e4SLinus Torvalds 2544d4be151bSAndré Goddard Rosa base = 10; 25453f623ebaSFabian Frederick is_sign = false; 2546d4be151bSAndré Goddard Rosa 25471da177e4SLinus Torvalds switch (*fmt++) { 25481da177e4SLinus Torvalds case 'c': 25491da177e4SLinus Torvalds { 25501da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 25511da177e4SLinus Torvalds if (field_width == -1) 25521da177e4SLinus Torvalds field_width = 1; 25531da177e4SLinus Torvalds do { 25541da177e4SLinus Torvalds *s++ = *str++; 25551da177e4SLinus Torvalds } while (--field_width > 0 && *str); 25561da177e4SLinus Torvalds num++; 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds continue; 25591da177e4SLinus Torvalds case 's': 25601da177e4SLinus Torvalds { 25611da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 25621da177e4SLinus Torvalds if (field_width == -1) 25634be929beSAlexey Dobriyan field_width = SHRT_MAX; 25641da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 2565e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds /* now copy until next white space */ 25687b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 25691da177e4SLinus Torvalds *s++ = *str++; 25701da177e4SLinus Torvalds *s = '\0'; 25711da177e4SLinus Torvalds num++; 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds continue; 25741da177e4SLinus Torvalds case 'o': 25751da177e4SLinus Torvalds base = 8; 25761da177e4SLinus Torvalds break; 25771da177e4SLinus Torvalds case 'x': 25781da177e4SLinus Torvalds case 'X': 25791da177e4SLinus Torvalds base = 16; 25801da177e4SLinus Torvalds break; 25811da177e4SLinus Torvalds case 'i': 25821da177e4SLinus Torvalds base = 0; 25831da177e4SLinus Torvalds case 'd': 25843f623ebaSFabian Frederick is_sign = true; 25851da177e4SLinus Torvalds case 'u': 25861da177e4SLinus Torvalds break; 25871da177e4SLinus Torvalds case '%': 25881da177e4SLinus Torvalds /* looking for '%' in str */ 25891da177e4SLinus Torvalds if (*str++ != '%') 25901da177e4SLinus Torvalds return num; 25911da177e4SLinus Torvalds continue; 25921da177e4SLinus Torvalds default: 25931da177e4SLinus Torvalds /* invalid format; stop here */ 25941da177e4SLinus Torvalds return num; 25951da177e4SLinus Torvalds } 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds /* have some sort of integer conversion. 25981da177e4SLinus Torvalds * first, skip white space in buffer. 25991da177e4SLinus Torvalds */ 2600e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 26011da177e4SLinus Torvalds 26021da177e4SLinus Torvalds digit = *str; 26031da177e4SLinus Torvalds if (is_sign && digit == '-') 26041da177e4SLinus Torvalds digit = *(str + 1); 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds if (!digit 26071da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 26081da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 26091da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 26101da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 26111da177e4SLinus Torvalds break; 26121da177e4SLinus Torvalds 261353809751SJan Beulich if (is_sign) 261453809751SJan Beulich val.s = qualifier != 'L' ? 261553809751SJan Beulich simple_strtol(str, &next, base) : 261653809751SJan Beulich simple_strtoll(str, &next, base); 261753809751SJan Beulich else 261853809751SJan Beulich val.u = qualifier != 'L' ? 261953809751SJan Beulich simple_strtoul(str, &next, base) : 262053809751SJan Beulich simple_strtoull(str, &next, base); 262153809751SJan Beulich 262253809751SJan Beulich if (field_width > 0 && next - str > field_width) { 262353809751SJan Beulich if (base == 0) 262453809751SJan Beulich _parse_integer_fixup_radix(str, &base); 262553809751SJan Beulich while (next - str > field_width) { 262653809751SJan Beulich if (is_sign) 262753809751SJan Beulich val.s = div_s64(val.s, base); 262853809751SJan Beulich else 262953809751SJan Beulich val.u = div_u64(val.u, base); 263053809751SJan Beulich --next; 263153809751SJan Beulich } 263253809751SJan Beulich } 263353809751SJan Beulich 26341da177e4SLinus Torvalds switch (qualifier) { 26351da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 263653809751SJan Beulich if (is_sign) 263753809751SJan Beulich *va_arg(args, signed char *) = val.s; 263853809751SJan Beulich else 263953809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 26401da177e4SLinus Torvalds break; 26411da177e4SLinus Torvalds case 'h': 264253809751SJan Beulich if (is_sign) 264353809751SJan Beulich *va_arg(args, short *) = val.s; 264453809751SJan Beulich else 264553809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 26461da177e4SLinus Torvalds break; 26471da177e4SLinus Torvalds case 'l': 264853809751SJan Beulich if (is_sign) 264953809751SJan Beulich *va_arg(args, long *) = val.s; 265053809751SJan Beulich else 265153809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 26521da177e4SLinus Torvalds break; 26531da177e4SLinus Torvalds case 'L': 265453809751SJan Beulich if (is_sign) 265553809751SJan Beulich *va_arg(args, long long *) = val.s; 265653809751SJan Beulich else 265753809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 26581da177e4SLinus Torvalds break; 26591da177e4SLinus Torvalds case 'Z': 26601da177e4SLinus Torvalds case 'z': 266153809751SJan Beulich *va_arg(args, size_t *) = val.u; 26621da177e4SLinus Torvalds break; 26631da177e4SLinus Torvalds default: 266453809751SJan Beulich if (is_sign) 266553809751SJan Beulich *va_arg(args, int *) = val.s; 266653809751SJan Beulich else 266753809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 26681da177e4SLinus Torvalds break; 26691da177e4SLinus Torvalds } 26701da177e4SLinus Torvalds num++; 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds if (!next) 26731da177e4SLinus Torvalds break; 26741da177e4SLinus Torvalds str = next; 26751da177e4SLinus Torvalds } 2676c6b40d16SJohannes Berg 26771da177e4SLinus Torvalds return num; 26781da177e4SLinus Torvalds } 26791da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 26801da177e4SLinus Torvalds 26811da177e4SLinus Torvalds /** 26821da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 26831da177e4SLinus Torvalds * @buf: input buffer 26841da177e4SLinus Torvalds * @fmt: formatting of buffer 26851da177e4SLinus Torvalds * @...: resulting arguments 26861da177e4SLinus Torvalds */ 26871da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 26881da177e4SLinus Torvalds { 26891da177e4SLinus Torvalds va_list args; 26901da177e4SLinus Torvalds int i; 26911da177e4SLinus Torvalds 26921da177e4SLinus Torvalds va_start(args, fmt); 26931da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 26941da177e4SLinus Torvalds va_end(args); 26957b9186f5SAndré Goddard Rosa 26961da177e4SLinus Torvalds return i; 26971da177e4SLinus Torvalds } 26981da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 2699