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> 20ef27ac18SRasmus Villemoes #include <linux/build_bug.h> 210d1d7a55SStephen Boyd #include <linux/clk.h> 22900cca29SGeert Uytterhoeven #include <linux/clk-provider.h> 238bc3bcc9SPaul Gortmaker #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ 241da177e4SLinus Torvalds #include <linux/types.h> 251da177e4SLinus Torvalds #include <linux/string.h> 261da177e4SLinus Torvalds #include <linux/ctype.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 280fe1ef24SLinus Torvalds #include <linux/kallsyms.h> 2953809751SJan Beulich #include <linux/math64.h> 300fe1ef24SLinus Torvalds #include <linux/uaccess.h> 31332d2e78SLinus Torvalds #include <linux/ioport.h> 324b6ccca7SAl Viro #include <linux/dcache.h> 33312b4e22SRyan Mallon #include <linux/cred.h> 344d42c447SAndy Shevchenko #include <linux/rtc.h> 352b1b0d66SAndy Shevchenko #include <linux/uuid.h> 36ce4fecf1SPantelis Antoniou #include <linux/of.h> 378a27f7c9SJoe Perches #include <net/addrconf.h> 38ad67b74dSTobin C. Harding #include <linux/siphash.h> 39ad67b74dSTobin C. Harding #include <linux/compiler.h> 401031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 411031bc58SDmitry Monakhov #include <linux/blkdev.h> 421031bc58SDmitry Monakhov #endif 431da177e4SLinus Torvalds 44edf14cdbSVlastimil Babka #include "../mm/internal.h" /* For the trace_print_flags arrays */ 45edf14cdbSVlastimil Babka 464e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 477c43d9a3SRasmus Villemoes #include <asm/byteorder.h> /* cpu_to_le16 */ 481da177e4SLinus Torvalds 4971dca95dSAndy Shevchenko #include <linux/string_helpers.h> 501dff46d6SAlexey Dobriyan #include "kstrtox.h" 51aa46a63eSHarvey Harrison 521da177e4SLinus Torvalds /** 531da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 541da177e4SLinus Torvalds * @cp: The start of the string 551da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 561da177e4SLinus Torvalds * @base: The number base to use 57462e4711SEldad Zack * 58462e4711SEldad Zack * This function is obsolete. Please use kstrtoull instead. 591da177e4SLinus Torvalds */ 601da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 611da177e4SLinus Torvalds { 621dff46d6SAlexey Dobriyan unsigned long long result; 631dff46d6SAlexey Dobriyan unsigned int rv; 641da177e4SLinus Torvalds 651dff46d6SAlexey Dobriyan cp = _parse_integer_fixup_radix(cp, &base); 661dff46d6SAlexey Dobriyan rv = _parse_integer(cp, base, &result); 671dff46d6SAlexey Dobriyan /* FIXME */ 681dff46d6SAlexey Dobriyan cp += (rv & ~KSTRTOX_OVERFLOW); 69aa46a63eSHarvey Harrison 701da177e4SLinus Torvalds if (endp) 711da177e4SLinus Torvalds *endp = (char *)cp; 727b9186f5SAndré Goddard Rosa 731da177e4SLinus Torvalds return result; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /** 78922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 79922ac25cSAndré Goddard Rosa * @cp: The start of the string 80922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 81922ac25cSAndré Goddard Rosa * @base: The number base to use 82462e4711SEldad Zack * 83462e4711SEldad Zack * This function is obsolete. Please use kstrtoul instead. 84922ac25cSAndré Goddard Rosa */ 85922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 86922ac25cSAndré Goddard Rosa { 87922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 88922ac25cSAndré Goddard Rosa } 89922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 90922ac25cSAndré Goddard Rosa 91922ac25cSAndré Goddard Rosa /** 92922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 93922ac25cSAndré Goddard Rosa * @cp: The start of the string 94922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 95922ac25cSAndré Goddard Rosa * @base: The number base to use 96462e4711SEldad Zack * 97462e4711SEldad Zack * This function is obsolete. Please use kstrtol instead. 98922ac25cSAndré Goddard Rosa */ 99922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 100922ac25cSAndré Goddard Rosa { 101922ac25cSAndré Goddard Rosa if (*cp == '-') 102922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 103922ac25cSAndré Goddard Rosa 104922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 105922ac25cSAndré Goddard Rosa } 106922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 107922ac25cSAndré Goddard Rosa 108922ac25cSAndré Goddard Rosa /** 1091da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1101da177e4SLinus Torvalds * @cp: The start of the string 1111da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1121da177e4SLinus Torvalds * @base: The number base to use 113462e4711SEldad Zack * 114462e4711SEldad Zack * This function is obsolete. Please use kstrtoll instead. 1151da177e4SLinus Torvalds */ 1161da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds if (*cp == '-') 1191da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1207b9186f5SAndré Goddard Rosa 1211da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1221da177e4SLinus Torvalds } 12398d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1241da177e4SLinus Torvalds 125cf3b429bSJoe Perches static noinline_for_stack 126cf3b429bSJoe Perches int skip_atoi(const char **s) 1271da177e4SLinus Torvalds { 1281da177e4SLinus Torvalds int i = 0; 1291da177e4SLinus Torvalds 13043e5b666SRasmus Villemoes do { 1311da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 13243e5b666SRasmus Villemoes } while (isdigit(**s)); 1337b9186f5SAndré Goddard Rosa 1341da177e4SLinus Torvalds return i; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1377c43d9a3SRasmus Villemoes /* 1387c43d9a3SRasmus Villemoes * Decimal conversion is by far the most typical, and is used for 1397c43d9a3SRasmus Villemoes * /proc and /sys data. This directly impacts e.g. top performance 1407c43d9a3SRasmus Villemoes * with many processes running. We optimize it for speed by emitting 1417c43d9a3SRasmus Villemoes * two characters at a time, using a 200 byte lookup table. This 1427c43d9a3SRasmus Villemoes * roughly halves the number of multiplications compared to computing 1437c43d9a3SRasmus Villemoes * the digits one at a time. Implementation strongly inspired by the 1447c43d9a3SRasmus Villemoes * previous version, which in turn used ideas described at 1457c43d9a3SRasmus Villemoes * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 1467c43d9a3SRasmus Villemoes * from the author, Douglas W. Jones). 1477c43d9a3SRasmus Villemoes * 1487c43d9a3SRasmus Villemoes * It turns out there is precisely one 26 bit fixed-point 1497c43d9a3SRasmus Villemoes * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 1507c43d9a3SRasmus Villemoes * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 1517c43d9a3SRasmus Villemoes * range happens to be somewhat larger (x <= 1073741898), but that's 1527c43d9a3SRasmus Villemoes * irrelevant for our purpose. 1537c43d9a3SRasmus Villemoes * 1547c43d9a3SRasmus Villemoes * For dividing a number in the range [10^4, 10^6-1] by 100, we still 1557c43d9a3SRasmus Villemoes * need a 32x32->64 bit multiply, so we simply use the same constant. 1567c43d9a3SRasmus Villemoes * 1577c43d9a3SRasmus Villemoes * For dividing a number in the range [100, 10^4-1] by 100, there are 1587c43d9a3SRasmus Villemoes * several options. The simplest is (x * 0x147b) >> 19, which is valid 1597c43d9a3SRasmus Villemoes * for all x <= 43698. 160133fd9f5SDenys Vlasenko */ 1614277eeddSDenis Vlasenko 1627c43d9a3SRasmus Villemoes static const u16 decpair[100] = { 1637c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 1647c43d9a3SRasmus Villemoes _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 1657c43d9a3SRasmus Villemoes _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 1667c43d9a3SRasmus Villemoes _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 1677c43d9a3SRasmus Villemoes _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 1687c43d9a3SRasmus Villemoes _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 1697c43d9a3SRasmus Villemoes _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 1707c43d9a3SRasmus Villemoes _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 1717c43d9a3SRasmus Villemoes _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 1727c43d9a3SRasmus Villemoes _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 1737c43d9a3SRasmus Villemoes _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 1747c43d9a3SRasmus Villemoes #undef _ 1757c43d9a3SRasmus Villemoes }; 1764277eeddSDenis Vlasenko 1777b9186f5SAndré Goddard Rosa /* 1787c43d9a3SRasmus Villemoes * This will print a single '0' even if r == 0, since we would 179675cf53cSRasmus Villemoes * immediately jump to out_r where two 0s would be written but only 180675cf53cSRasmus Villemoes * one of them accounted for in buf. This is needed by ip4_string 181675cf53cSRasmus Villemoes * below. All other callers pass a non-zero value of r. 182133fd9f5SDenys Vlasenko */ 183133fd9f5SDenys Vlasenko static noinline_for_stack 184133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 185133fd9f5SDenys Vlasenko { 186133fd9f5SDenys Vlasenko unsigned q; 1874277eeddSDenis Vlasenko 1887c43d9a3SRasmus Villemoes /* 1 <= r < 10^8 */ 1897c43d9a3SRasmus Villemoes if (r < 100) 1907c43d9a3SRasmus Villemoes goto out_r; 191cb239d0aSGeorge Spelvin 1927c43d9a3SRasmus Villemoes /* 100 <= r < 10^8 */ 1937c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 1947c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 1957c43d9a3SRasmus Villemoes buf += 2; 1967c43d9a3SRasmus Villemoes 1977c43d9a3SRasmus Villemoes /* 1 <= q < 10^6 */ 1987c43d9a3SRasmus Villemoes if (q < 100) 1997c43d9a3SRasmus Villemoes goto out_q; 2007c43d9a3SRasmus Villemoes 2017c43d9a3SRasmus Villemoes /* 100 <= q < 10^6 */ 2027c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2037c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2047c43d9a3SRasmus Villemoes buf += 2; 2057c43d9a3SRasmus Villemoes 2067c43d9a3SRasmus Villemoes /* 1 <= r < 10^4 */ 2077c43d9a3SRasmus Villemoes if (r < 100) 2087c43d9a3SRasmus Villemoes goto out_r; 2097c43d9a3SRasmus Villemoes 2107c43d9a3SRasmus Villemoes /* 100 <= r < 10^4 */ 2117c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2127c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2137c43d9a3SRasmus Villemoes buf += 2; 2147c43d9a3SRasmus Villemoes out_q: 2157c43d9a3SRasmus Villemoes /* 1 <= q < 100 */ 2167c43d9a3SRasmus Villemoes r = q; 2177c43d9a3SRasmus Villemoes out_r: 2187c43d9a3SRasmus Villemoes /* 1 <= r < 100 */ 2197c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r]; 220675cf53cSRasmus Villemoes buf += r < 10 ? 1 : 2; 221133fd9f5SDenys Vlasenko return buf; 222133fd9f5SDenys Vlasenko } 223133fd9f5SDenys Vlasenko 2247c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 2257c43d9a3SRasmus Villemoes static noinline_for_stack 2267c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r) 2277c43d9a3SRasmus Villemoes { 2287c43d9a3SRasmus Villemoes unsigned q; 229133fd9f5SDenys Vlasenko 2307c43d9a3SRasmus Villemoes /* 0 <= r < 10^8 */ 2317c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2327c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2337c43d9a3SRasmus Villemoes buf += 2; 234133fd9f5SDenys Vlasenko 2357c43d9a3SRasmus Villemoes /* 0 <= q < 10^6 */ 2367c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2377c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2387c43d9a3SRasmus Villemoes buf += 2; 239133fd9f5SDenys Vlasenko 2407c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2417c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2427c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2437c43d9a3SRasmus Villemoes buf += 2; 2447c43d9a3SRasmus Villemoes 2457c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2467c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2477c43d9a3SRasmus Villemoes buf += 2; 2487c43d9a3SRasmus Villemoes return buf; 2497c43d9a3SRasmus Villemoes } 2507c43d9a3SRasmus Villemoes 2517c43d9a3SRasmus Villemoes static noinline_for_stack 252133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 253133fd9f5SDenys Vlasenko { 254133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 2557c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2567c43d9a3SRasmus Villemoes /* 1 <= n <= 1.6e11 */ 2577c43d9a3SRasmus Villemoes if (n >= 100*1000*1000) 2587c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2597c43d9a3SRasmus Villemoes /* 1 <= n < 1e8 */ 260133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 261133fd9f5SDenys Vlasenko } 262133fd9f5SDenys Vlasenko 2637c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 264133fd9f5SDenys Vlasenko 2657c43d9a3SRasmus Villemoes static void 2667c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r) 267133fd9f5SDenys Vlasenko { 2687c43d9a3SRasmus Villemoes unsigned q; 2697c43d9a3SRasmus Villemoes 2707c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2717c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2727c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2737c43d9a3SRasmus Villemoes buf += 2; 2747c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2757c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2762359172aSGeorge Spelvin } 2772359172aSGeorge Spelvin 2782359172aSGeorge Spelvin /* 2792359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 2802359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 2812359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 2822359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 2837c43d9a3SRasmus Villemoes * (second call in the put_dec code, assuming n is all-ones). 2842359172aSGeorge Spelvin */ 2857c43d9a3SRasmus Villemoes static noinline_for_stack 2862359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 2872359172aSGeorge Spelvin { 2882359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 2892359172aSGeorge Spelvin 2902359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 2912359172aSGeorge Spelvin return q; 292133fd9f5SDenys Vlasenko } 293133fd9f5SDenys Vlasenko 294133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 295133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 296133fd9f5SDenys Vlasenko * (with permission from the author). 297133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 298133fd9f5SDenys Vlasenko */ 299133fd9f5SDenys Vlasenko static 300133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 301133fd9f5SDenys Vlasenko { 302133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 303133fd9f5SDenys Vlasenko 304133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 305133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 306133fd9f5SDenys Vlasenko 307133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 308133fd9f5SDenys Vlasenko h = (n >> 32); 309133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 310133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 311133fd9f5SDenys Vlasenko 3127c43d9a3SRasmus Villemoes /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 3137c43d9a3SRasmus Villemoes = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 314133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 3152359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 316133fd9f5SDenys Vlasenko 3172359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 3182359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 319133fd9f5SDenys Vlasenko 3202359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3212359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 322133fd9f5SDenys Vlasenko 3232359172aSGeorge Spelvin q += 281 * d3; 3242359172aSGeorge Spelvin buf += 12; 3252359172aSGeorge Spelvin if (q) 3262359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3272359172aSGeorge Spelvin else while (buf[-1] == '0') 328133fd9f5SDenys Vlasenko --buf; 3297b9186f5SAndré Goddard Rosa 3304277eeddSDenis Vlasenko return buf; 3314277eeddSDenis Vlasenko } 332133fd9f5SDenys Vlasenko 333133fd9f5SDenys Vlasenko #endif 3344277eeddSDenis Vlasenko 3351ac101a5SKAMEZAWA Hiroyuki /* 3361ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3371ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3381ac101a5SKAMEZAWA Hiroyuki * 3391ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3401ac101a5SKAMEZAWA Hiroyuki */ 341d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) 3421ac101a5SKAMEZAWA Hiroyuki { 3437c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 3447c43d9a3SRasmus Villemoes char tmp[sizeof(num) * 3] __aligned(2); 3451ac101a5SKAMEZAWA Hiroyuki int idx, len; 3461ac101a5SKAMEZAWA Hiroyuki 347133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 348133fd9f5SDenys Vlasenko if (num <= 9) { 349133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 350133fd9f5SDenys Vlasenko len = 1; 351133fd9f5SDenys Vlasenko } else { 3521ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 353133fd9f5SDenys Vlasenko } 3541ac101a5SKAMEZAWA Hiroyuki 355d1be35cbSAndrei Vagin if (len > size || width > size) 3561ac101a5SKAMEZAWA Hiroyuki return 0; 357d1be35cbSAndrei Vagin 358d1be35cbSAndrei Vagin if (width > len) { 359d1be35cbSAndrei Vagin width = width - len; 360d1be35cbSAndrei Vagin for (idx = 0; idx < width; idx++) 361d1be35cbSAndrei Vagin buf[idx] = ' '; 362d1be35cbSAndrei Vagin } else { 363d1be35cbSAndrei Vagin width = 0; 364d1be35cbSAndrei Vagin } 365d1be35cbSAndrei Vagin 3661ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 367d1be35cbSAndrei Vagin buf[idx + width] = tmp[len - idx - 1]; 368d1be35cbSAndrei Vagin 369d1be35cbSAndrei Vagin return len + width; 3701ac101a5SKAMEZAWA Hiroyuki } 3711ac101a5SKAMEZAWA Hiroyuki 37251be17dfSRasmus Villemoes #define SIGN 1 /* unsigned/signed, must be 1 */ 373d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */ 3741da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3751da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 376d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 377b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 378b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 3791da177e4SLinus Torvalds 380fef20d9cSFrederic Weisbecker enum format_type { 381fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 382ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 383fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 384fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 385fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 386fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 387fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 388fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 389fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 390fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 391fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 392a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 393a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 394fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 395fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 396fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 397fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 398fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 399fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 400fef20d9cSFrederic Weisbecker }; 401fef20d9cSFrederic Weisbecker 402fef20d9cSFrederic Weisbecker struct printf_spec { 403d0484193SRasmus Villemoes unsigned int type:8; /* format_type enum */ 404d0484193SRasmus Villemoes signed int field_width:24; /* width of output field */ 405d0484193SRasmus Villemoes unsigned int flags:8; /* flags to number() */ 406d0484193SRasmus Villemoes unsigned int base:8; /* number base, 8, 10 or 16 only */ 407d0484193SRasmus Villemoes signed int precision:16; /* # of digits/chars */ 408d0484193SRasmus Villemoes } __packed; 409ef27ac18SRasmus Villemoes static_assert(sizeof(struct printf_spec) == 8); 410ef27ac18SRasmus Villemoes 4114d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1) 4124d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1) 413fef20d9cSFrederic Weisbecker 414cf3b429bSJoe Perches static noinline_for_stack 415cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 416fef20d9cSFrederic Weisbecker struct printf_spec spec) 4171da177e4SLinus Torvalds { 4187c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 4197c43d9a3SRasmus Villemoes char tmp[3 * sizeof(num)] __aligned(2); 4209b706aeeSDenys Vlasenko char sign; 4219b706aeeSDenys Vlasenko char locase; 422fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4231da177e4SLinus Torvalds int i; 4247c203422SPierre Carrier bool is_zero = num == 0LL; 4251c7a8e62SRasmus Villemoes int field_width = spec.field_width; 4261c7a8e62SRasmus Villemoes int precision = spec.precision; 4271da177e4SLinus Torvalds 4289b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4299b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 430fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 431fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 432fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4331da177e4SLinus Torvalds sign = 0; 434fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4351da177e4SLinus Torvalds if ((signed long long)num < 0) { 4361da177e4SLinus Torvalds sign = '-'; 4371da177e4SLinus Torvalds num = -(signed long long)num; 4381c7a8e62SRasmus Villemoes field_width--; 439fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4401da177e4SLinus Torvalds sign = '+'; 4411c7a8e62SRasmus Villemoes field_width--; 442fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4431da177e4SLinus Torvalds sign = ' '; 4441c7a8e62SRasmus Villemoes field_width--; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds } 447b39a7340SDenis Vlasenko if (need_pfx) { 448fef20d9cSFrederic Weisbecker if (spec.base == 16) 4491c7a8e62SRasmus Villemoes field_width -= 2; 4507c203422SPierre Carrier else if (!is_zero) 4511c7a8e62SRasmus Villemoes field_width--; 4521da177e4SLinus Torvalds } 453b39a7340SDenis Vlasenko 454b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4551da177e4SLinus Torvalds i = 0; 456133fd9f5SDenys Vlasenko if (num < spec.base) 4573ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase; 458fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 459fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 460b39a7340SDenis Vlasenko int shift = 3; 4617b9186f5SAndré Goddard Rosa 4627b9186f5SAndré Goddard Rosa if (spec.base == 16) 4637b9186f5SAndré Goddard Rosa shift = 4; 464b39a7340SDenis Vlasenko do { 4653ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 466b39a7340SDenis Vlasenko num >>= shift; 467b39a7340SDenis Vlasenko } while (num); 4684277eeddSDenis Vlasenko } else { /* base 10 */ 4694277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4704277eeddSDenis Vlasenko } 471b39a7340SDenis Vlasenko 472b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 4731c7a8e62SRasmus Villemoes if (i > precision) 4741c7a8e62SRasmus Villemoes precision = i; 475b39a7340SDenis Vlasenko /* leading space padding */ 4761c7a8e62SRasmus Villemoes field_width -= precision; 47751be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) { 4781c7a8e62SRasmus Villemoes while (--field_width >= 0) { 479f796937aSJeremy Fitzhardinge if (buf < end) 4801da177e4SLinus Torvalds *buf = ' '; 4811da177e4SLinus Torvalds ++buf; 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds } 484b39a7340SDenis Vlasenko /* sign */ 4851da177e4SLinus Torvalds if (sign) { 486f796937aSJeremy Fitzhardinge if (buf < end) 4871da177e4SLinus Torvalds *buf = sign; 4881da177e4SLinus Torvalds ++buf; 4891da177e4SLinus Torvalds } 490b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 491b39a7340SDenis Vlasenko if (need_pfx) { 4927c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 493f796937aSJeremy Fitzhardinge if (buf < end) 4941da177e4SLinus Torvalds *buf = '0'; 4951da177e4SLinus Torvalds ++buf; 4967c203422SPierre Carrier } 497fef20d9cSFrederic Weisbecker if (spec.base == 16) { 498f796937aSJeremy Fitzhardinge if (buf < end) 4999b706aeeSDenys Vlasenko *buf = ('X' | locase); 5001da177e4SLinus Torvalds ++buf; 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds } 503b39a7340SDenis Vlasenko /* zero or space padding */ 504fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 505d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD); 506d1c1b121SRasmus Villemoes BUILD_BUG_ON(' ' + ZEROPAD != '0'); 5071c7a8e62SRasmus Villemoes while (--field_width >= 0) { 508f796937aSJeremy Fitzhardinge if (buf < end) 5091da177e4SLinus Torvalds *buf = c; 5101da177e4SLinus Torvalds ++buf; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds } 513b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 5141c7a8e62SRasmus Villemoes while (i <= --precision) { 515f796937aSJeremy Fitzhardinge if (buf < end) 5161da177e4SLinus Torvalds *buf = '0'; 5171da177e4SLinus Torvalds ++buf; 5181da177e4SLinus Torvalds } 519b39a7340SDenis Vlasenko /* actual digits of result */ 520b39a7340SDenis Vlasenko while (--i >= 0) { 521f796937aSJeremy Fitzhardinge if (buf < end) 5221da177e4SLinus Torvalds *buf = tmp[i]; 5231da177e4SLinus Torvalds ++buf; 5241da177e4SLinus Torvalds } 525b39a7340SDenis Vlasenko /* trailing space padding */ 5261c7a8e62SRasmus Villemoes while (--field_width >= 0) { 527f796937aSJeremy Fitzhardinge if (buf < end) 5281da177e4SLinus Torvalds *buf = ' '; 5291da177e4SLinus Torvalds ++buf; 5301da177e4SLinus Torvalds } 5317b9186f5SAndré Goddard Rosa 5321da177e4SLinus Torvalds return buf; 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5353cab1e71SAndy Shevchenko static noinline_for_stack 5363cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size) 5373cab1e71SAndy Shevchenko { 5383cab1e71SAndy Shevchenko struct printf_spec spec; 5393cab1e71SAndy Shevchenko 5403cab1e71SAndy Shevchenko spec.type = FORMAT_TYPE_PTR; 5413cab1e71SAndy Shevchenko spec.field_width = 2 + 2 * size; /* 0x + hex */ 5423cab1e71SAndy Shevchenko spec.flags = SPECIAL | SMALL | ZEROPAD; 5433cab1e71SAndy Shevchenko spec.base = 16; 5443cab1e71SAndy Shevchenko spec.precision = -1; 5453cab1e71SAndy Shevchenko 5463cab1e71SAndy Shevchenko return number(buf, end, num, spec); 5473cab1e71SAndy Shevchenko } 5483cab1e71SAndy Shevchenko 549cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces) 5504b6ccca7SAl Viro { 5514b6ccca7SAl Viro size_t size; 5524b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5534b6ccca7SAl Viro return; 5544b6ccca7SAl Viro size = end - buf; 5554b6ccca7SAl Viro if (size <= spaces) { 5564b6ccca7SAl Viro memset(buf, ' ', size); 5574b6ccca7SAl Viro return; 5584b6ccca7SAl Viro } 5594b6ccca7SAl Viro if (len) { 5604b6ccca7SAl Viro if (len > size - spaces) 5614b6ccca7SAl Viro len = size - spaces; 5624b6ccca7SAl Viro memmove(buf + spaces, buf, len); 5634b6ccca7SAl Viro } 5644b6ccca7SAl Viro memset(buf, ' ', spaces); 5654b6ccca7SAl Viro } 5664b6ccca7SAl Viro 567cfccde04SRasmus Villemoes /* 568cfccde04SRasmus Villemoes * Handle field width padding for a string. 569cfccde04SRasmus Villemoes * @buf: current buffer position 570cfccde04SRasmus Villemoes * @n: length of string 571cfccde04SRasmus Villemoes * @end: end of output buffer 572cfccde04SRasmus Villemoes * @spec: for field width and flags 573cfccde04SRasmus Villemoes * Returns: new buffer position after padding. 574cfccde04SRasmus Villemoes */ 575cfccde04SRasmus Villemoes static noinline_for_stack 576cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec) 577cfccde04SRasmus Villemoes { 578cfccde04SRasmus Villemoes unsigned spaces; 579cfccde04SRasmus Villemoes 580cfccde04SRasmus Villemoes if (likely(n >= spec.field_width)) 581cfccde04SRasmus Villemoes return buf; 582cfccde04SRasmus Villemoes /* we want to pad the sucker */ 583cfccde04SRasmus Villemoes spaces = spec.field_width - n; 584cfccde04SRasmus Villemoes if (!(spec.flags & LEFT)) { 585cfccde04SRasmus Villemoes move_right(buf - n, end, n, spaces); 586cfccde04SRasmus Villemoes return buf + spaces; 587cfccde04SRasmus Villemoes } 588cfccde04SRasmus Villemoes while (spaces--) { 589cfccde04SRasmus Villemoes if (buf < end) 590cfccde04SRasmus Villemoes *buf = ' '; 591cfccde04SRasmus Villemoes ++buf; 592cfccde04SRasmus Villemoes } 593cfccde04SRasmus Villemoes return buf; 594cfccde04SRasmus Villemoes } 595cfccde04SRasmus Villemoes 596*d529ac41SPetr Mladek /* Handle string from a well known address. */ 597*d529ac41SPetr Mladek static char *string_nocheck(char *buf, char *end, const char *s, 598*d529ac41SPetr Mladek struct printf_spec spec) 59995508cfaSRasmus Villemoes { 60034fc8b90SRasmus Villemoes int len = 0; 60134fc8b90SRasmus Villemoes size_t lim = spec.precision; 60295508cfaSRasmus Villemoes 60334fc8b90SRasmus Villemoes while (lim--) { 60434fc8b90SRasmus Villemoes char c = *s++; 60534fc8b90SRasmus Villemoes if (!c) 60634fc8b90SRasmus Villemoes break; 60795508cfaSRasmus Villemoes if (buf < end) 60834fc8b90SRasmus Villemoes *buf = c; 60995508cfaSRasmus Villemoes ++buf; 61034fc8b90SRasmus Villemoes ++len; 61195508cfaSRasmus Villemoes } 61234fc8b90SRasmus Villemoes return widen_string(buf, len, end, spec); 61395508cfaSRasmus Villemoes } 61495508cfaSRasmus Villemoes 61595508cfaSRasmus Villemoes static noinline_for_stack 616*d529ac41SPetr Mladek char *string(char *buf, char *end, const char *s, 617*d529ac41SPetr Mladek struct printf_spec spec) 618*d529ac41SPetr Mladek { 619*d529ac41SPetr Mladek if ((unsigned long)s < PAGE_SIZE) 620*d529ac41SPetr Mladek s = "(null)"; 621*d529ac41SPetr Mladek 622*d529ac41SPetr Mladek return string_nocheck(buf, end, s, spec); 623*d529ac41SPetr Mladek } 624*d529ac41SPetr Mladek 6259073dac1SGeert Uytterhoeven char *pointer_string(char *buf, char *end, const void *ptr, 6269073dac1SGeert Uytterhoeven struct printf_spec spec) 6279073dac1SGeert Uytterhoeven { 6289073dac1SGeert Uytterhoeven spec.base = 16; 6299073dac1SGeert Uytterhoeven spec.flags |= SMALL; 6309073dac1SGeert Uytterhoeven if (spec.field_width == -1) { 6319073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr); 6329073dac1SGeert Uytterhoeven spec.flags |= ZEROPAD; 6339073dac1SGeert Uytterhoeven } 6349073dac1SGeert Uytterhoeven 6359073dac1SGeert Uytterhoeven return number(buf, end, (unsigned long int)ptr, spec); 6369073dac1SGeert Uytterhoeven } 6379073dac1SGeert Uytterhoeven 6389073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */ 6399073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init; 6409073dac1SGeert Uytterhoeven 6419073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str) 6429073dac1SGeert Uytterhoeven { 6439073dac1SGeert Uytterhoeven debug_boot_weak_hash = 1; 6449073dac1SGeert Uytterhoeven pr_info("debug_boot_weak_hash enabled\n"); 6459073dac1SGeert Uytterhoeven return 0; 6469073dac1SGeert Uytterhoeven } 6479073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); 6489073dac1SGeert Uytterhoeven 6499073dac1SGeert Uytterhoeven static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key); 6509073dac1SGeert Uytterhoeven static siphash_key_t ptr_key __read_mostly; 6519073dac1SGeert Uytterhoeven 6529073dac1SGeert Uytterhoeven static void enable_ptr_key_workfn(struct work_struct *work) 6539073dac1SGeert Uytterhoeven { 6549073dac1SGeert Uytterhoeven get_random_bytes(&ptr_key, sizeof(ptr_key)); 6559073dac1SGeert Uytterhoeven /* Needs to run from preemptible context */ 6569073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 6579073dac1SGeert Uytterhoeven } 6589073dac1SGeert Uytterhoeven 6599073dac1SGeert Uytterhoeven static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn); 6609073dac1SGeert Uytterhoeven 6619073dac1SGeert Uytterhoeven static void fill_random_ptr_key(struct random_ready_callback *unused) 6629073dac1SGeert Uytterhoeven { 6639073dac1SGeert Uytterhoeven /* This may be in an interrupt handler. */ 6649073dac1SGeert Uytterhoeven queue_work(system_unbound_wq, &enable_ptr_key_work); 6659073dac1SGeert Uytterhoeven } 6669073dac1SGeert Uytterhoeven 6679073dac1SGeert Uytterhoeven static struct random_ready_callback random_ready = { 6689073dac1SGeert Uytterhoeven .func = fill_random_ptr_key 6699073dac1SGeert Uytterhoeven }; 6709073dac1SGeert Uytterhoeven 6719073dac1SGeert Uytterhoeven static int __init initialize_ptr_random(void) 6729073dac1SGeert Uytterhoeven { 6739073dac1SGeert Uytterhoeven int key_size = sizeof(ptr_key); 6749073dac1SGeert Uytterhoeven int ret; 6759073dac1SGeert Uytterhoeven 6769073dac1SGeert Uytterhoeven /* Use hw RNG if available. */ 6779073dac1SGeert Uytterhoeven if (get_random_bytes_arch(&ptr_key, key_size) == key_size) { 6789073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 6799073dac1SGeert Uytterhoeven return 0; 6809073dac1SGeert Uytterhoeven } 6819073dac1SGeert Uytterhoeven 6829073dac1SGeert Uytterhoeven ret = add_random_ready_callback(&random_ready); 6839073dac1SGeert Uytterhoeven if (!ret) { 6849073dac1SGeert Uytterhoeven return 0; 6859073dac1SGeert Uytterhoeven } else if (ret == -EALREADY) { 6869073dac1SGeert Uytterhoeven /* This is in preemptible context */ 6879073dac1SGeert Uytterhoeven enable_ptr_key_workfn(&enable_ptr_key_work); 6889073dac1SGeert Uytterhoeven return 0; 6899073dac1SGeert Uytterhoeven } 6909073dac1SGeert Uytterhoeven 6919073dac1SGeert Uytterhoeven return ret; 6929073dac1SGeert Uytterhoeven } 6939073dac1SGeert Uytterhoeven early_initcall(initialize_ptr_random); 6949073dac1SGeert Uytterhoeven 6959073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */ 6969073dac1SGeert Uytterhoeven static char *ptr_to_id(char *buf, char *end, const void *ptr, 6979073dac1SGeert Uytterhoeven struct printf_spec spec) 6989073dac1SGeert Uytterhoeven { 6999073dac1SGeert Uytterhoeven const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; 7009073dac1SGeert Uytterhoeven unsigned long hashval; 7019073dac1SGeert Uytterhoeven 7029073dac1SGeert Uytterhoeven /* When debugging early boot use non-cryptographically secure hash. */ 7039073dac1SGeert Uytterhoeven if (unlikely(debug_boot_weak_hash)) { 7049073dac1SGeert Uytterhoeven hashval = hash_long((unsigned long)ptr, 32); 7059073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec); 7069073dac1SGeert Uytterhoeven } 7079073dac1SGeert Uytterhoeven 7089073dac1SGeert Uytterhoeven if (static_branch_unlikely(¬_filled_random_ptr_key)) { 7099073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr); 7109073dac1SGeert Uytterhoeven /* string length must be less than default_width */ 711*d529ac41SPetr Mladek return string_nocheck(buf, end, str, spec); 7129073dac1SGeert Uytterhoeven } 7139073dac1SGeert Uytterhoeven 7149073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT 7159073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); 7169073dac1SGeert Uytterhoeven /* 7179073dac1SGeert Uytterhoeven * Mask off the first 32 bits, this makes explicit that we have 7189073dac1SGeert Uytterhoeven * modified the address (and 32 bits is plenty for a unique ID). 7199073dac1SGeert Uytterhoeven */ 7209073dac1SGeert Uytterhoeven hashval = hashval & 0xffffffff; 7219073dac1SGeert Uytterhoeven #else 7229073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); 7239073dac1SGeert Uytterhoeven #endif 7249073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec); 7259073dac1SGeert Uytterhoeven } 7269073dac1SGeert Uytterhoeven 7276eea242fSPetr Mladek int kptr_restrict __read_mostly; 7286eea242fSPetr Mladek 7296eea242fSPetr Mladek static noinline_for_stack 7306eea242fSPetr Mladek char *restricted_pointer(char *buf, char *end, const void *ptr, 7316eea242fSPetr Mladek struct printf_spec spec) 7326eea242fSPetr Mladek { 7336eea242fSPetr Mladek switch (kptr_restrict) { 7346eea242fSPetr Mladek case 0: 7351ac2f978SPetr Mladek /* Handle as %p, hash and do _not_ leak addresses. */ 7361ac2f978SPetr Mladek return ptr_to_id(buf, end, ptr, spec); 7376eea242fSPetr Mladek case 1: { 7386eea242fSPetr Mladek const struct cred *cred; 7396eea242fSPetr Mladek 7406eea242fSPetr Mladek /* 7416eea242fSPetr Mladek * kptr_restrict==1 cannot be used in IRQ context 7426eea242fSPetr Mladek * because its test for CAP_SYSLOG would be meaningless. 7436eea242fSPetr Mladek */ 7446eea242fSPetr Mladek if (in_irq() || in_serving_softirq() || in_nmi()) { 7456eea242fSPetr Mladek if (spec.field_width == -1) 7466eea242fSPetr Mladek spec.field_width = 2 * sizeof(ptr); 747*d529ac41SPetr Mladek return string_nocheck(buf, end, "pK-error", spec); 7486eea242fSPetr Mladek } 7496eea242fSPetr Mladek 7506eea242fSPetr Mladek /* 7516eea242fSPetr Mladek * Only print the real pointer value if the current 7526eea242fSPetr Mladek * process has CAP_SYSLOG and is running with the 7536eea242fSPetr Mladek * same credentials it started with. This is because 7546eea242fSPetr Mladek * access to files is checked at open() time, but %pK 7556eea242fSPetr Mladek * checks permission at read() time. We don't want to 7566eea242fSPetr Mladek * leak pointer values if a binary opens a file using 7576eea242fSPetr Mladek * %pK and then elevates privileges before reading it. 7586eea242fSPetr Mladek */ 7596eea242fSPetr Mladek cred = current_cred(); 7606eea242fSPetr Mladek if (!has_capability_noaudit(current, CAP_SYSLOG) || 7616eea242fSPetr Mladek !uid_eq(cred->euid, cred->uid) || 7626eea242fSPetr Mladek !gid_eq(cred->egid, cred->gid)) 7636eea242fSPetr Mladek ptr = NULL; 7646eea242fSPetr Mladek break; 7656eea242fSPetr Mladek } 7666eea242fSPetr Mladek case 2: 7676eea242fSPetr Mladek default: 7686eea242fSPetr Mladek /* Always print 0's for %pK */ 7696eea242fSPetr Mladek ptr = NULL; 7706eea242fSPetr Mladek break; 7716eea242fSPetr Mladek } 7726eea242fSPetr Mladek 7736eea242fSPetr Mladek return pointer_string(buf, end, ptr, spec); 7746eea242fSPetr Mladek } 7756eea242fSPetr Mladek 7769073dac1SGeert Uytterhoeven static noinline_for_stack 7774b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 7784b6ccca7SAl Viro const char *fmt) 7794b6ccca7SAl Viro { 7804b6ccca7SAl Viro const char *array[4], *s; 7814b6ccca7SAl Viro const struct dentry *p; 7824b6ccca7SAl Viro int depth; 7834b6ccca7SAl Viro int i, n; 7844b6ccca7SAl Viro 7854b6ccca7SAl Viro switch (fmt[1]) { 7864b6ccca7SAl Viro case '2': case '3': case '4': 7874b6ccca7SAl Viro depth = fmt[1] - '0'; 7884b6ccca7SAl Viro break; 7894b6ccca7SAl Viro default: 7904b6ccca7SAl Viro depth = 1; 7914b6ccca7SAl Viro } 7924b6ccca7SAl Viro 7934b6ccca7SAl Viro rcu_read_lock(); 7944b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 7956aa7de05SMark Rutland p = READ_ONCE(d->d_parent); 7966aa7de05SMark Rutland array[i] = READ_ONCE(d->d_name.name); 7974b6ccca7SAl Viro if (p == d) { 7984b6ccca7SAl Viro if (i) 7994b6ccca7SAl Viro array[i] = ""; 8004b6ccca7SAl Viro i++; 8014b6ccca7SAl Viro break; 8024b6ccca7SAl Viro } 8034b6ccca7SAl Viro } 8044b6ccca7SAl Viro s = array[--i]; 8054b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 8064b6ccca7SAl Viro char c = *s++; 8074b6ccca7SAl Viro if (!c) { 8084b6ccca7SAl Viro if (!i) 8094b6ccca7SAl Viro break; 8104b6ccca7SAl Viro c = '/'; 8114b6ccca7SAl Viro s = array[--i]; 8124b6ccca7SAl Viro } 8134b6ccca7SAl Viro if (buf < end) 8144b6ccca7SAl Viro *buf = c; 8154b6ccca7SAl Viro } 8164b6ccca7SAl Viro rcu_read_unlock(); 817cfccde04SRasmus Villemoes return widen_string(buf, n, end, spec); 8184b6ccca7SAl Viro } 8194b6ccca7SAl Viro 8201031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 8211031bc58SDmitry Monakhov static noinline_for_stack 8221031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev, 8231031bc58SDmitry Monakhov struct printf_spec spec, const char *fmt) 8241031bc58SDmitry Monakhov { 8251031bc58SDmitry Monakhov struct gendisk *hd = bdev->bd_disk; 8261031bc58SDmitry Monakhov 8271031bc58SDmitry Monakhov buf = string(buf, end, hd->disk_name, spec); 8281031bc58SDmitry Monakhov if (bdev->bd_part->partno) { 8291031bc58SDmitry Monakhov if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { 8301031bc58SDmitry Monakhov if (buf < end) 8311031bc58SDmitry Monakhov *buf = 'p'; 8321031bc58SDmitry Monakhov buf++; 8331031bc58SDmitry Monakhov } 8341031bc58SDmitry Monakhov buf = number(buf, end, bdev->bd_part->partno, spec); 8351031bc58SDmitry Monakhov } 8361031bc58SDmitry Monakhov return buf; 8371031bc58SDmitry Monakhov } 8381031bc58SDmitry Monakhov #endif 8391031bc58SDmitry Monakhov 840cf3b429bSJoe Perches static noinline_for_stack 841cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 842b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 8430fe1ef24SLinus Torvalds { 844b0d33c2bSJoe Perches unsigned long value; 8450fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 8460fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 847b0d33c2bSJoe Perches #endif 848b0d33c2bSJoe Perches 849b0d33c2bSJoe Perches if (fmt[1] == 'R') 850b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 851b0d33c2bSJoe Perches value = (unsigned long)ptr; 852b0d33c2bSJoe Perches 853b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 854b0d33c2bSJoe Perches if (*fmt == 'B') 8550f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 856b0d33c2bSJoe Perches else if (*fmt != 'f' && *fmt != 's') 8570fe1ef24SLinus Torvalds sprint_symbol(sym, value); 8580c8b946eSFrederic Weisbecker else 8594796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 8607b9186f5SAndré Goddard Rosa 861*d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 8620fe1ef24SLinus Torvalds #else 8633cab1e71SAndy Shevchenko return special_hex_number(buf, end, value, sizeof(void *)); 8640fe1ef24SLinus Torvalds #endif 8650fe1ef24SLinus Torvalds } 8660fe1ef24SLinus Torvalds 867abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = { 868abd4fe62SAndy Shevchenko .field_width = -1, 869abd4fe62SAndy Shevchenko .precision = -1, 870abd4fe62SAndy Shevchenko }; 871abd4fe62SAndy Shevchenko 87254433973SAndy Shevchenko static const struct printf_spec default_flag_spec = { 87354433973SAndy Shevchenko .base = 16, 87454433973SAndy Shevchenko .precision = -1, 87554433973SAndy Shevchenko .flags = SPECIAL | SMALL, 87654433973SAndy Shevchenko }; 87754433973SAndy Shevchenko 878ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = { 879ce0b4910SAndy Shevchenko .base = 10, 880ce0b4910SAndy Shevchenko .precision = -1, 881ce0b4910SAndy Shevchenko }; 882ce0b4910SAndy Shevchenko 8834d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = { 8844d42c447SAndy Shevchenko .base = 10, 8854d42c447SAndy Shevchenko .field_width = 2, 8864d42c447SAndy Shevchenko .precision = -1, 8874d42c447SAndy Shevchenko .flags = ZEROPAD, 8884d42c447SAndy Shevchenko }; 8894d42c447SAndy Shevchenko 8904d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = { 8914d42c447SAndy Shevchenko .base = 10, 8924d42c447SAndy Shevchenko .field_width = 4, 8934d42c447SAndy Shevchenko .precision = -1, 8944d42c447SAndy Shevchenko .flags = ZEROPAD, 8954d42c447SAndy Shevchenko }; 8964d42c447SAndy Shevchenko 897cf3b429bSJoe Perches static noinline_for_stack 898cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 899fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 900332d2e78SLinus Torvalds { 901332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 90228405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 903332d2e78SLinus Torvalds #endif 904332d2e78SLinus Torvalds 905332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 90628405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 907332d2e78SLinus Torvalds #endif 9084da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 909fef20d9cSFrederic Weisbecker .base = 16, 9104da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 911fef20d9cSFrederic Weisbecker .precision = -1, 912fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 913fef20d9cSFrederic Weisbecker }; 9144da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 9154da0b66cSBjorn Helgaas .base = 16, 9164da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 9174da0b66cSBjorn Helgaas .precision = -1, 9184da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 9194da0b66cSBjorn Helgaas }; 9200f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 9210f4050c7SBjorn Helgaas .base = 16, 9220f4050c7SBjorn Helgaas .field_width = 2, 9230f4050c7SBjorn Helgaas .precision = -1, 9240f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 9250f4050c7SBjorn Helgaas }; 9264da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 927fd95541eSBjorn Helgaas .field_width = -1, 928fd95541eSBjorn Helgaas .precision = 10, 929fd95541eSBjorn Helgaas .flags = LEFT, 930fd95541eSBjorn Helgaas }; 931c7dabef8SBjorn Helgaas 932c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 933c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 934c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 935c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 9369d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 937c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 938c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 939c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 940c7dabef8SBjorn Helgaas 941332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 942c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 9434da0b66cSBjorn Helgaas const struct printf_spec *specp; 944332d2e78SLinus Torvalds 945332d2e78SLinus Torvalds *p++ = '['; 9464da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 947*d529ac41SPetr Mladek p = string_nocheck(p, pend, "io ", str_spec); 9484da0b66cSBjorn Helgaas specp = &io_spec; 9494da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 950*d529ac41SPetr Mladek p = string_nocheck(p, pend, "mem ", str_spec); 9514da0b66cSBjorn Helgaas specp = &mem_spec; 9524da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 953*d529ac41SPetr Mladek p = string_nocheck(p, pend, "irq ", str_spec); 954ce0b4910SAndy Shevchenko specp = &default_dec_spec; 9554da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 956*d529ac41SPetr Mladek p = string_nocheck(p, pend, "dma ", str_spec); 957ce0b4910SAndy Shevchenko specp = &default_dec_spec; 9580f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 959*d529ac41SPetr Mladek p = string_nocheck(p, pend, "bus ", str_spec); 9600f4050c7SBjorn Helgaas specp = &bus_spec; 9614da0b66cSBjorn Helgaas } else { 962*d529ac41SPetr Mladek p = string_nocheck(p, pend, "??? ", str_spec); 9634da0b66cSBjorn Helgaas specp = &mem_spec; 964c7dabef8SBjorn Helgaas decode = 0; 965fd95541eSBjorn Helgaas } 966d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 967*d529ac41SPetr Mladek p = string_nocheck(p, pend, "size ", str_spec); 968d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 969d19cb803SBjorn Helgaas } else { 9704da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 971c91d3376SBjorn Helgaas if (res->start != res->end) { 972332d2e78SLinus Torvalds *p++ = '-'; 9734da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 974c91d3376SBjorn Helgaas } 975d19cb803SBjorn Helgaas } 976c7dabef8SBjorn Helgaas if (decode) { 977fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 978*d529ac41SPetr Mladek p = string_nocheck(p, pend, " 64bit", str_spec); 979fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 980*d529ac41SPetr Mladek p = string_nocheck(p, pend, " pref", str_spec); 9819d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 982*d529ac41SPetr Mladek p = string_nocheck(p, pend, " window", str_spec); 983fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 984*d529ac41SPetr Mladek p = string_nocheck(p, pend, " disabled", str_spec); 985c7dabef8SBjorn Helgaas } else { 986*d529ac41SPetr Mladek p = string_nocheck(p, pend, " flags ", str_spec); 98754433973SAndy Shevchenko p = number(p, pend, res->flags, default_flag_spec); 988fd95541eSBjorn Helgaas } 989332d2e78SLinus Torvalds *p++ = ']'; 990c7dabef8SBjorn Helgaas *p = '\0'; 991332d2e78SLinus Torvalds 992*d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 993332d2e78SLinus Torvalds } 994332d2e78SLinus Torvalds 995cf3b429bSJoe Perches static noinline_for_stack 99631550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 99731550a16SAndy Shevchenko const char *fmt) 99831550a16SAndy Shevchenko { 999360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 100031550a16SAndy Shevchenko negative value, fallback to the default */ 100131550a16SAndy Shevchenko char separator; 100231550a16SAndy Shevchenko 100331550a16SAndy Shevchenko if (spec.field_width == 0) 100431550a16SAndy Shevchenko /* nothing to print */ 100531550a16SAndy Shevchenko return buf; 100631550a16SAndy Shevchenko 100731550a16SAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 100831550a16SAndy Shevchenko /* NULL pointer */ 100931550a16SAndy Shevchenko return string(buf, end, NULL, spec); 101031550a16SAndy Shevchenko 101131550a16SAndy Shevchenko switch (fmt[1]) { 101231550a16SAndy Shevchenko case 'C': 101331550a16SAndy Shevchenko separator = ':'; 101431550a16SAndy Shevchenko break; 101531550a16SAndy Shevchenko case 'D': 101631550a16SAndy Shevchenko separator = '-'; 101731550a16SAndy Shevchenko break; 101831550a16SAndy Shevchenko case 'N': 101931550a16SAndy Shevchenko separator = 0; 102031550a16SAndy Shevchenko break; 102131550a16SAndy Shevchenko default: 102231550a16SAndy Shevchenko separator = ' '; 102331550a16SAndy Shevchenko break; 102431550a16SAndy Shevchenko } 102531550a16SAndy Shevchenko 102631550a16SAndy Shevchenko if (spec.field_width > 0) 102731550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 102831550a16SAndy Shevchenko 10299c98f235SRasmus Villemoes for (i = 0; i < len; ++i) { 10309c98f235SRasmus Villemoes if (buf < end) 10319c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]); 10329c98f235SRasmus Villemoes ++buf; 10339c98f235SRasmus Villemoes if (buf < end) 10349c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]); 10359c98f235SRasmus Villemoes ++buf; 103631550a16SAndy Shevchenko 10379c98f235SRasmus Villemoes if (separator && i != len - 1) { 10389c98f235SRasmus Villemoes if (buf < end) 10399c98f235SRasmus Villemoes *buf = separator; 10409c98f235SRasmus Villemoes ++buf; 10419c98f235SRasmus Villemoes } 104231550a16SAndy Shevchenko } 104331550a16SAndy Shevchenko 104431550a16SAndy Shevchenko return buf; 104531550a16SAndy Shevchenko } 104631550a16SAndy Shevchenko 104731550a16SAndy Shevchenko static noinline_for_stack 1048dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap, 1049dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1050dbc760bcSTejun Heo { 1051dbc760bcSTejun Heo const int CHUNKSZ = 32; 1052dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1053dbc760bcSTejun Heo int i, chunksz; 1054dbc760bcSTejun Heo bool first = true; 1055dbc760bcSTejun Heo 1056dbc760bcSTejun Heo /* reused to print numbers */ 1057dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 1058dbc760bcSTejun Heo 1059dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1); 1060dbc760bcSTejun Heo if (chunksz == 0) 1061dbc760bcSTejun Heo chunksz = CHUNKSZ; 1062dbc760bcSTejun Heo 1063dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 1064dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) { 1065dbc760bcSTejun Heo u32 chunkmask, val; 1066dbc760bcSTejun Heo int word, bit; 1067dbc760bcSTejun Heo 1068dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1); 1069dbc760bcSTejun Heo word = i / BITS_PER_LONG; 1070dbc760bcSTejun Heo bit = i % BITS_PER_LONG; 1071dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask; 1072dbc760bcSTejun Heo 1073dbc760bcSTejun Heo if (!first) { 1074dbc760bcSTejun Heo if (buf < end) 1075dbc760bcSTejun Heo *buf = ','; 1076dbc760bcSTejun Heo buf++; 1077dbc760bcSTejun Heo } 1078dbc760bcSTejun Heo first = false; 1079dbc760bcSTejun Heo 1080dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4); 1081dbc760bcSTejun Heo buf = number(buf, end, val, spec); 1082dbc760bcSTejun Heo 1083dbc760bcSTejun Heo chunksz = CHUNKSZ; 1084dbc760bcSTejun Heo } 1085dbc760bcSTejun Heo return buf; 1086dbc760bcSTejun Heo } 1087dbc760bcSTejun Heo 1088dbc760bcSTejun Heo static noinline_for_stack 1089dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, 1090dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1091dbc760bcSTejun Heo { 1092dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1093dbc760bcSTejun Heo /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 1094dbc760bcSTejun Heo int cur, rbot, rtop; 1095dbc760bcSTejun Heo bool first = true; 1096dbc760bcSTejun Heo 1097dbc760bcSTejun Heo rbot = cur = find_first_bit(bitmap, nr_bits); 1098dbc760bcSTejun Heo while (cur < nr_bits) { 1099dbc760bcSTejun Heo rtop = cur; 1100dbc760bcSTejun Heo cur = find_next_bit(bitmap, nr_bits, cur + 1); 1101dbc760bcSTejun Heo if (cur < nr_bits && cur <= rtop + 1) 1102dbc760bcSTejun Heo continue; 1103dbc760bcSTejun Heo 1104dbc760bcSTejun Heo if (!first) { 1105dbc760bcSTejun Heo if (buf < end) 1106dbc760bcSTejun Heo *buf = ','; 1107dbc760bcSTejun Heo buf++; 1108dbc760bcSTejun Heo } 1109dbc760bcSTejun Heo first = false; 1110dbc760bcSTejun Heo 1111ce0b4910SAndy Shevchenko buf = number(buf, end, rbot, default_dec_spec); 1112dbc760bcSTejun Heo if (rbot < rtop) { 1113dbc760bcSTejun Heo if (buf < end) 1114dbc760bcSTejun Heo *buf = '-'; 1115dbc760bcSTejun Heo buf++; 1116dbc760bcSTejun Heo 1117ce0b4910SAndy Shevchenko buf = number(buf, end, rtop, default_dec_spec); 1118dbc760bcSTejun Heo } 1119dbc760bcSTejun Heo 1120dbc760bcSTejun Heo rbot = cur; 1121dbc760bcSTejun Heo } 1122dbc760bcSTejun Heo return buf; 1123dbc760bcSTejun Heo } 1124dbc760bcSTejun Heo 1125dbc760bcSTejun Heo static noinline_for_stack 1126cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 11278a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 1128dd45c9cfSHarvey Harrison { 11298a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 1130dd45c9cfSHarvey Harrison char *p = mac_addr; 1131dd45c9cfSHarvey Harrison int i; 1132bc7259a2SJoe Perches char separator; 113376597ff9SAndrei Emeltchenko bool reversed = false; 1134bc7259a2SJoe Perches 113576597ff9SAndrei Emeltchenko switch (fmt[1]) { 113676597ff9SAndrei Emeltchenko case 'F': 1137bc7259a2SJoe Perches separator = '-'; 113876597ff9SAndrei Emeltchenko break; 113976597ff9SAndrei Emeltchenko 114076597ff9SAndrei Emeltchenko case 'R': 114176597ff9SAndrei Emeltchenko reversed = true; 114276597ff9SAndrei Emeltchenko /* fall through */ 114376597ff9SAndrei Emeltchenko 114476597ff9SAndrei Emeltchenko default: 1145bc7259a2SJoe Perches separator = ':'; 114676597ff9SAndrei Emeltchenko break; 1147bc7259a2SJoe Perches } 1148dd45c9cfSHarvey Harrison 1149dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 115076597ff9SAndrei Emeltchenko if (reversed) 115176597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 115276597ff9SAndrei Emeltchenko else 115355036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 115476597ff9SAndrei Emeltchenko 11558a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 1156bc7259a2SJoe Perches *p++ = separator; 1157dd45c9cfSHarvey Harrison } 1158dd45c9cfSHarvey Harrison *p = '\0'; 1159dd45c9cfSHarvey Harrison 1160*d529ac41SPetr Mladek return string_nocheck(buf, end, mac_addr, spec); 1161dd45c9cfSHarvey Harrison } 1162dd45c9cfSHarvey Harrison 1163cf3b429bSJoe Perches static noinline_for_stack 1164cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 1165689afa7dSHarvey Harrison { 1166689afa7dSHarvey Harrison int i; 11670159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 11680159f24eSJoe Perches int index; 11690159f24eSJoe Perches int step; 1170689afa7dSHarvey Harrison 11710159f24eSJoe Perches switch (fmt[2]) { 11720159f24eSJoe Perches case 'h': 11730159f24eSJoe Perches #ifdef __BIG_ENDIAN 11740159f24eSJoe Perches index = 0; 11750159f24eSJoe Perches step = 1; 11760159f24eSJoe Perches #else 11770159f24eSJoe Perches index = 3; 11780159f24eSJoe Perches step = -1; 11790159f24eSJoe Perches #endif 11800159f24eSJoe Perches break; 11810159f24eSJoe Perches case 'l': 11820159f24eSJoe Perches index = 3; 11830159f24eSJoe Perches step = -1; 11840159f24eSJoe Perches break; 11850159f24eSJoe Perches case 'n': 11860159f24eSJoe Perches case 'b': 11870159f24eSJoe Perches default: 11880159f24eSJoe Perches index = 0; 11890159f24eSJoe Perches step = 1; 11900159f24eSJoe Perches break; 11910159f24eSJoe Perches } 11928a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 11937c43d9a3SRasmus Villemoes char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 1194133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 11958a27f7c9SJoe Perches if (leading_zeros) { 11968a27f7c9SJoe Perches if (digits < 3) 11978a27f7c9SJoe Perches *p++ = '0'; 11988a27f7c9SJoe Perches if (digits < 2) 11998a27f7c9SJoe Perches *p++ = '0'; 12008a27f7c9SJoe Perches } 12018a27f7c9SJoe Perches /* reverse the digits in the quad */ 12028a27f7c9SJoe Perches while (digits--) 12038a27f7c9SJoe Perches *p++ = temp[digits]; 12048a27f7c9SJoe Perches if (i < 3) 12058a27f7c9SJoe Perches *p++ = '.'; 12060159f24eSJoe Perches index += step; 12078a27f7c9SJoe Perches } 12088a27f7c9SJoe Perches *p = '\0'; 12097b9186f5SAndré Goddard Rosa 12108a27f7c9SJoe Perches return p; 12118a27f7c9SJoe Perches } 12128a27f7c9SJoe Perches 1213cf3b429bSJoe Perches static noinline_for_stack 1214cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 12158a27f7c9SJoe Perches { 12167b9186f5SAndré Goddard Rosa int i, j, range; 12178a27f7c9SJoe Perches unsigned char zerolength[8]; 12188a27f7c9SJoe Perches int longest = 1; 12198a27f7c9SJoe Perches int colonpos = -1; 12208a27f7c9SJoe Perches u16 word; 12217b9186f5SAndré Goddard Rosa u8 hi, lo; 12228a27f7c9SJoe Perches bool needcolon = false; 1223eb78cd26SJoe Perches bool useIPv4; 1224eb78cd26SJoe Perches struct in6_addr in6; 1225eb78cd26SJoe Perches 1226eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 1227eb78cd26SJoe Perches 1228eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 12298a27f7c9SJoe Perches 12308a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 12318a27f7c9SJoe Perches 12328a27f7c9SJoe Perches if (useIPv4) 12338a27f7c9SJoe Perches range = 6; 12348a27f7c9SJoe Perches else 12358a27f7c9SJoe Perches range = 8; 12368a27f7c9SJoe Perches 12378a27f7c9SJoe Perches /* find position of longest 0 run */ 12388a27f7c9SJoe Perches for (i = 0; i < range; i++) { 12398a27f7c9SJoe Perches for (j = i; j < range; j++) { 1240eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 12418a27f7c9SJoe Perches break; 12428a27f7c9SJoe Perches zerolength[i]++; 12438a27f7c9SJoe Perches } 12448a27f7c9SJoe Perches } 12458a27f7c9SJoe Perches for (i = 0; i < range; i++) { 12468a27f7c9SJoe Perches if (zerolength[i] > longest) { 12478a27f7c9SJoe Perches longest = zerolength[i]; 12488a27f7c9SJoe Perches colonpos = i; 12498a27f7c9SJoe Perches } 12508a27f7c9SJoe Perches } 125129cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 125229cf519eSJoe Perches colonpos = -1; 12538a27f7c9SJoe Perches 12548a27f7c9SJoe Perches /* emit address */ 12558a27f7c9SJoe Perches for (i = 0; i < range; i++) { 12568a27f7c9SJoe Perches if (i == colonpos) { 12578a27f7c9SJoe Perches if (needcolon || i == 0) 12588a27f7c9SJoe Perches *p++ = ':'; 12598a27f7c9SJoe Perches *p++ = ':'; 12608a27f7c9SJoe Perches needcolon = false; 12618a27f7c9SJoe Perches i += longest - 1; 12628a27f7c9SJoe Perches continue; 12638a27f7c9SJoe Perches } 12648a27f7c9SJoe Perches if (needcolon) { 12658a27f7c9SJoe Perches *p++ = ':'; 12668a27f7c9SJoe Perches needcolon = false; 12678a27f7c9SJoe Perches } 12688a27f7c9SJoe Perches /* hex u16 without leading 0s */ 1269eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 12708a27f7c9SJoe Perches hi = word >> 8; 12718a27f7c9SJoe Perches lo = word & 0xff; 12728a27f7c9SJoe Perches if (hi) { 12738a27f7c9SJoe Perches if (hi > 0x0f) 127455036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 12758a27f7c9SJoe Perches else 12768a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 127755036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 12788a27f7c9SJoe Perches } 1279b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 128055036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 12818a27f7c9SJoe Perches else 12828a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 12838a27f7c9SJoe Perches needcolon = true; 12848a27f7c9SJoe Perches } 12858a27f7c9SJoe Perches 12868a27f7c9SJoe Perches if (useIPv4) { 12878a27f7c9SJoe Perches if (needcolon) 12888a27f7c9SJoe Perches *p++ = ':'; 12890159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 12908a27f7c9SJoe Perches } 12918a27f7c9SJoe Perches *p = '\0'; 12927b9186f5SAndré Goddard Rosa 12938a27f7c9SJoe Perches return p; 12948a27f7c9SJoe Perches } 12958a27f7c9SJoe Perches 1296cf3b429bSJoe Perches static noinline_for_stack 1297cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 12988a27f7c9SJoe Perches { 12998a27f7c9SJoe Perches int i; 13007b9186f5SAndré Goddard Rosa 1301689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 130255036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 130355036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 13048a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 1305689afa7dSHarvey Harrison *p++ = ':'; 1306689afa7dSHarvey Harrison } 1307689afa7dSHarvey Harrison *p = '\0'; 13087b9186f5SAndré Goddard Rosa 13098a27f7c9SJoe Perches return p; 13108a27f7c9SJoe Perches } 13118a27f7c9SJoe Perches 1312cf3b429bSJoe Perches static noinline_for_stack 1313cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 13148a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 13158a27f7c9SJoe Perches { 13168a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 13178a27f7c9SJoe Perches 13188a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 1319eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 13208a27f7c9SJoe Perches else 1321eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 1322689afa7dSHarvey Harrison 1323*d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 1324689afa7dSHarvey Harrison } 1325689afa7dSHarvey Harrison 1326cf3b429bSJoe Perches static noinline_for_stack 1327cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 13288a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 13294aa99606SHarvey Harrison { 13308a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 13314aa99606SHarvey Harrison 13320159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 13334aa99606SHarvey Harrison 1334*d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 13354aa99606SHarvey Harrison } 13364aa99606SHarvey Harrison 1337cf3b429bSJoe Perches static noinline_for_stack 133810679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 133910679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 134010679643SDaniel Borkmann { 134110679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 134210679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 134310679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 134410679643SDaniel Borkmann sizeof("%1234567890")]; 134510679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 134610679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 134710679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 134810679643SDaniel Borkmann u8 off = 0; 134910679643SDaniel Borkmann 135010679643SDaniel Borkmann fmt++; 135110679643SDaniel Borkmann while (isalpha(*++fmt)) { 135210679643SDaniel Borkmann switch (*fmt) { 135310679643SDaniel Borkmann case 'p': 135410679643SDaniel Borkmann have_p = true; 135510679643SDaniel Borkmann break; 135610679643SDaniel Borkmann case 'f': 135710679643SDaniel Borkmann have_f = true; 135810679643SDaniel Borkmann break; 135910679643SDaniel Borkmann case 's': 136010679643SDaniel Borkmann have_s = true; 136110679643SDaniel Borkmann break; 136210679643SDaniel Borkmann case 'c': 136310679643SDaniel Borkmann have_c = true; 136410679643SDaniel Borkmann break; 136510679643SDaniel Borkmann } 136610679643SDaniel Borkmann } 136710679643SDaniel Borkmann 136810679643SDaniel Borkmann if (have_p || have_s || have_f) { 136910679643SDaniel Borkmann *p = '['; 137010679643SDaniel Borkmann off = 1; 137110679643SDaniel Borkmann } 137210679643SDaniel Borkmann 137310679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 137410679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 137510679643SDaniel Borkmann else 137610679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 137710679643SDaniel Borkmann 137810679643SDaniel Borkmann if (have_p || have_s || have_f) 137910679643SDaniel Borkmann *p++ = ']'; 138010679643SDaniel Borkmann 138110679643SDaniel Borkmann if (have_p) { 138210679643SDaniel Borkmann *p++ = ':'; 138310679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 138410679643SDaniel Borkmann } 138510679643SDaniel Borkmann if (have_f) { 138610679643SDaniel Borkmann *p++ = '/'; 138710679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 138810679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 138910679643SDaniel Borkmann } 139010679643SDaniel Borkmann if (have_s) { 139110679643SDaniel Borkmann *p++ = '%'; 139210679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 139310679643SDaniel Borkmann } 139410679643SDaniel Borkmann *p = '\0'; 139510679643SDaniel Borkmann 1396*d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 139710679643SDaniel Borkmann } 139810679643SDaniel Borkmann 139910679643SDaniel Borkmann static noinline_for_stack 140010679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 140110679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 140210679643SDaniel Borkmann { 140310679643SDaniel Borkmann bool have_p = false; 140410679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 140510679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 140610679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 140710679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 140810679643SDaniel Borkmann 140910679643SDaniel Borkmann fmt++; 141010679643SDaniel Borkmann while (isalpha(*++fmt)) { 141110679643SDaniel Borkmann switch (*fmt) { 141210679643SDaniel Borkmann case 'p': 141310679643SDaniel Borkmann have_p = true; 141410679643SDaniel Borkmann break; 141510679643SDaniel Borkmann case 'h': 141610679643SDaniel Borkmann case 'l': 141710679643SDaniel Borkmann case 'n': 141810679643SDaniel Borkmann case 'b': 141910679643SDaniel Borkmann fmt4[2] = *fmt; 142010679643SDaniel Borkmann break; 142110679643SDaniel Borkmann } 142210679643SDaniel Borkmann } 142310679643SDaniel Borkmann 142410679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 142510679643SDaniel Borkmann if (have_p) { 142610679643SDaniel Borkmann *p++ = ':'; 142710679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 142810679643SDaniel Borkmann } 142910679643SDaniel Borkmann *p = '\0'; 143010679643SDaniel Borkmann 1431*d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 143210679643SDaniel Borkmann } 143310679643SDaniel Borkmann 143410679643SDaniel Borkmann static noinline_for_stack 143571dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 143671dca95dSAndy Shevchenko const char *fmt) 143771dca95dSAndy Shevchenko { 143871dca95dSAndy Shevchenko bool found = true; 143971dca95dSAndy Shevchenko int count = 1; 144071dca95dSAndy Shevchenko unsigned int flags = 0; 144171dca95dSAndy Shevchenko int len; 144271dca95dSAndy Shevchenko 144371dca95dSAndy Shevchenko if (spec.field_width == 0) 144471dca95dSAndy Shevchenko return buf; /* nothing to print */ 144571dca95dSAndy Shevchenko 144671dca95dSAndy Shevchenko if (ZERO_OR_NULL_PTR(addr)) 144771dca95dSAndy Shevchenko return string(buf, end, NULL, spec); /* NULL pointer */ 144871dca95dSAndy Shevchenko 144971dca95dSAndy Shevchenko 145071dca95dSAndy Shevchenko do { 145171dca95dSAndy Shevchenko switch (fmt[count++]) { 145271dca95dSAndy Shevchenko case 'a': 145371dca95dSAndy Shevchenko flags |= ESCAPE_ANY; 145471dca95dSAndy Shevchenko break; 145571dca95dSAndy Shevchenko case 'c': 145671dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL; 145771dca95dSAndy Shevchenko break; 145871dca95dSAndy Shevchenko case 'h': 145971dca95dSAndy Shevchenko flags |= ESCAPE_HEX; 146071dca95dSAndy Shevchenko break; 146171dca95dSAndy Shevchenko case 'n': 146271dca95dSAndy Shevchenko flags |= ESCAPE_NULL; 146371dca95dSAndy Shevchenko break; 146471dca95dSAndy Shevchenko case 'o': 146571dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL; 146671dca95dSAndy Shevchenko break; 146771dca95dSAndy Shevchenko case 'p': 146871dca95dSAndy Shevchenko flags |= ESCAPE_NP; 146971dca95dSAndy Shevchenko break; 147071dca95dSAndy Shevchenko case 's': 147171dca95dSAndy Shevchenko flags |= ESCAPE_SPACE; 147271dca95dSAndy Shevchenko break; 147371dca95dSAndy Shevchenko default: 147471dca95dSAndy Shevchenko found = false; 147571dca95dSAndy Shevchenko break; 147671dca95dSAndy Shevchenko } 147771dca95dSAndy Shevchenko } while (found); 147871dca95dSAndy Shevchenko 147971dca95dSAndy Shevchenko if (!flags) 148071dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP; 148171dca95dSAndy Shevchenko 148271dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width; 148371dca95dSAndy Shevchenko 148441416f23SRasmus Villemoes /* 148541416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to 148641416f23SRasmus Villemoes * the given buffer, and returns the total size of the output 148741416f23SRasmus Villemoes * had the buffer been big enough. 148841416f23SRasmus Villemoes */ 148941416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 149071dca95dSAndy Shevchenko 149171dca95dSAndy Shevchenko return buf; 149271dca95dSAndy Shevchenko } 149371dca95dSAndy Shevchenko 149471dca95dSAndy Shevchenko static noinline_for_stack 1495cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 14969ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 14979ac6e44eSJoe Perches { 14982b1b0d66SAndy Shevchenko char uuid[UUID_STRING_LEN + 1]; 14999ac6e44eSJoe Perches char *p = uuid; 15009ac6e44eSJoe Perches int i; 1501f9727a17SChristoph Hellwig const u8 *index = uuid_index; 15029ac6e44eSJoe Perches bool uc = false; 15039ac6e44eSJoe Perches 15049ac6e44eSJoe Perches switch (*(++fmt)) { 15059ac6e44eSJoe Perches case 'L': 15069ac6e44eSJoe Perches uc = true; /* fall-through */ 15079ac6e44eSJoe Perches case 'l': 1508f9727a17SChristoph Hellwig index = guid_index; 15099ac6e44eSJoe Perches break; 15109ac6e44eSJoe Perches case 'B': 15119ac6e44eSJoe Perches uc = true; 15129ac6e44eSJoe Perches break; 15139ac6e44eSJoe Perches } 15149ac6e44eSJoe Perches 15159ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 1516aa4ea1c3SAndy Shevchenko if (uc) 1517aa4ea1c3SAndy Shevchenko p = hex_byte_pack_upper(p, addr[index[i]]); 1518aa4ea1c3SAndy Shevchenko else 151955036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 15209ac6e44eSJoe Perches switch (i) { 15219ac6e44eSJoe Perches case 3: 15229ac6e44eSJoe Perches case 5: 15239ac6e44eSJoe Perches case 7: 15249ac6e44eSJoe Perches case 9: 15259ac6e44eSJoe Perches *p++ = '-'; 15269ac6e44eSJoe Perches break; 15279ac6e44eSJoe Perches } 15289ac6e44eSJoe Perches } 15299ac6e44eSJoe Perches 15309ac6e44eSJoe Perches *p = 0; 15319ac6e44eSJoe Perches 1532*d529ac41SPetr Mladek return string_nocheck(buf, end, uuid, spec); 15339ac6e44eSJoe Perches } 15349ac6e44eSJoe Perches 15355b17aecfSAndy Shevchenko static noinline_for_stack 1536431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr, 1537431bca24SGeert Uytterhoeven struct printf_spec spec, const char *fmt) 1538c8f44affSMichał Mirosław { 15395b17aecfSAndy Shevchenko unsigned long long num; 15405b17aecfSAndy Shevchenko int size; 15415b17aecfSAndy Shevchenko 15425b17aecfSAndy Shevchenko switch (fmt[1]) { 15435b17aecfSAndy Shevchenko case 'F': 15445b17aecfSAndy Shevchenko num = *(const netdev_features_t *)addr; 15455b17aecfSAndy Shevchenko size = sizeof(netdev_features_t); 15465b17aecfSAndy Shevchenko break; 15475b17aecfSAndy Shevchenko default: 1548431bca24SGeert Uytterhoeven return ptr_to_id(buf, end, addr, spec); 15495b17aecfSAndy Shevchenko } 1550c8f44affSMichał Mirosław 15513cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1552c8f44affSMichał Mirosław } 1553c8f44affSMichał Mirosław 1554aaf07621SJoe Perches static noinline_for_stack 15553cab1e71SAndy Shevchenko char *address_val(char *buf, char *end, const void *addr, const char *fmt) 1556aaf07621SJoe Perches { 1557aaf07621SJoe Perches unsigned long long num; 15583cab1e71SAndy Shevchenko int size; 1559aaf07621SJoe Perches 1560aaf07621SJoe Perches switch (fmt[1]) { 1561aaf07621SJoe Perches case 'd': 1562aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 15633cab1e71SAndy Shevchenko size = sizeof(dma_addr_t); 1564aaf07621SJoe Perches break; 1565aaf07621SJoe Perches case 'p': 1566aaf07621SJoe Perches default: 1567aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 15683cab1e71SAndy Shevchenko size = sizeof(phys_addr_t); 1569aaf07621SJoe Perches break; 1570aaf07621SJoe Perches } 1571aaf07621SJoe Perches 15723cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1573aaf07621SJoe Perches } 1574aaf07621SJoe Perches 1575900cca29SGeert Uytterhoeven static noinline_for_stack 15764d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) 15774d42c447SAndy Shevchenko { 15784d42c447SAndy Shevchenko int year = tm->tm_year + (r ? 0 : 1900); 15794d42c447SAndy Shevchenko int mon = tm->tm_mon + (r ? 0 : 1); 15804d42c447SAndy Shevchenko 15814d42c447SAndy Shevchenko buf = number(buf, end, year, default_dec04_spec); 15824d42c447SAndy Shevchenko if (buf < end) 15834d42c447SAndy Shevchenko *buf = '-'; 15844d42c447SAndy Shevchenko buf++; 15854d42c447SAndy Shevchenko 15864d42c447SAndy Shevchenko buf = number(buf, end, mon, default_dec02_spec); 15874d42c447SAndy Shevchenko if (buf < end) 15884d42c447SAndy Shevchenko *buf = '-'; 15894d42c447SAndy Shevchenko buf++; 15904d42c447SAndy Shevchenko 15914d42c447SAndy Shevchenko return number(buf, end, tm->tm_mday, default_dec02_spec); 15924d42c447SAndy Shevchenko } 15934d42c447SAndy Shevchenko 15944d42c447SAndy Shevchenko static noinline_for_stack 15954d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) 15964d42c447SAndy Shevchenko { 15974d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_hour, default_dec02_spec); 15984d42c447SAndy Shevchenko if (buf < end) 15994d42c447SAndy Shevchenko *buf = ':'; 16004d42c447SAndy Shevchenko buf++; 16014d42c447SAndy Shevchenko 16024d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_min, default_dec02_spec); 16034d42c447SAndy Shevchenko if (buf < end) 16044d42c447SAndy Shevchenko *buf = ':'; 16054d42c447SAndy Shevchenko buf++; 16064d42c447SAndy Shevchenko 16074d42c447SAndy Shevchenko return number(buf, end, tm->tm_sec, default_dec02_spec); 16084d42c447SAndy Shevchenko } 16094d42c447SAndy Shevchenko 16104d42c447SAndy Shevchenko static noinline_for_stack 16114d42c447SAndy Shevchenko char *rtc_str(char *buf, char *end, const struct rtc_time *tm, const char *fmt) 16124d42c447SAndy Shevchenko { 16134d42c447SAndy Shevchenko bool have_t = true, have_d = true; 16144d42c447SAndy Shevchenko bool raw = false; 16154d42c447SAndy Shevchenko int count = 2; 16164d42c447SAndy Shevchenko 16174d42c447SAndy Shevchenko switch (fmt[count]) { 16184d42c447SAndy Shevchenko case 'd': 16194d42c447SAndy Shevchenko have_t = false; 16204d42c447SAndy Shevchenko count++; 16214d42c447SAndy Shevchenko break; 16224d42c447SAndy Shevchenko case 't': 16234d42c447SAndy Shevchenko have_d = false; 16244d42c447SAndy Shevchenko count++; 16254d42c447SAndy Shevchenko break; 16264d42c447SAndy Shevchenko } 16274d42c447SAndy Shevchenko 16284d42c447SAndy Shevchenko raw = fmt[count] == 'r'; 16294d42c447SAndy Shevchenko 16304d42c447SAndy Shevchenko if (have_d) 16314d42c447SAndy Shevchenko buf = date_str(buf, end, tm, raw); 16324d42c447SAndy Shevchenko if (have_d && have_t) { 16334d42c447SAndy Shevchenko /* Respect ISO 8601 */ 16344d42c447SAndy Shevchenko if (buf < end) 16354d42c447SAndy Shevchenko *buf = 'T'; 16364d42c447SAndy Shevchenko buf++; 16374d42c447SAndy Shevchenko } 16384d42c447SAndy Shevchenko if (have_t) 16394d42c447SAndy Shevchenko buf = time_str(buf, end, tm, raw); 16404d42c447SAndy Shevchenko 16414d42c447SAndy Shevchenko return buf; 16424d42c447SAndy Shevchenko } 16434d42c447SAndy Shevchenko 16444d42c447SAndy Shevchenko static noinline_for_stack 16454d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, 16464d42c447SAndy Shevchenko const char *fmt) 16474d42c447SAndy Shevchenko { 16484d42c447SAndy Shevchenko switch (fmt[1]) { 16494d42c447SAndy Shevchenko case 'R': 16504d42c447SAndy Shevchenko return rtc_str(buf, end, (const struct rtc_time *)ptr, fmt); 16514d42c447SAndy Shevchenko default: 16524d42c447SAndy Shevchenko return ptr_to_id(buf, end, ptr, spec); 16534d42c447SAndy Shevchenko } 16544d42c447SAndy Shevchenko } 16554d42c447SAndy Shevchenko 16564d42c447SAndy Shevchenko static noinline_for_stack 1657900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1658900cca29SGeert Uytterhoeven const char *fmt) 1659900cca29SGeert Uytterhoeven { 1660900cca29SGeert Uytterhoeven if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk) 1661900cca29SGeert Uytterhoeven return string(buf, end, NULL, spec); 1662900cca29SGeert Uytterhoeven 1663900cca29SGeert Uytterhoeven switch (fmt[1]) { 1664900cca29SGeert Uytterhoeven case 'n': 1665900cca29SGeert Uytterhoeven default: 1666900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1667900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1668900cca29SGeert Uytterhoeven #else 1669ec12bc29SGeert Uytterhoeven return ptr_to_id(buf, end, clk, spec); 1670900cca29SGeert Uytterhoeven #endif 1671900cca29SGeert Uytterhoeven } 1672900cca29SGeert Uytterhoeven } 1673900cca29SGeert Uytterhoeven 1674edf14cdbSVlastimil Babka static 1675edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags, 1676edf14cdbSVlastimil Babka const struct trace_print_flags *names) 1677edf14cdbSVlastimil Babka { 1678edf14cdbSVlastimil Babka unsigned long mask; 1679edf14cdbSVlastimil Babka 1680edf14cdbSVlastimil Babka for ( ; flags && names->name; names++) { 1681edf14cdbSVlastimil Babka mask = names->mask; 1682edf14cdbSVlastimil Babka if ((flags & mask) != mask) 1683edf14cdbSVlastimil Babka continue; 1684edf14cdbSVlastimil Babka 1685abd4fe62SAndy Shevchenko buf = string(buf, end, names->name, default_str_spec); 1686edf14cdbSVlastimil Babka 1687edf14cdbSVlastimil Babka flags &= ~mask; 1688edf14cdbSVlastimil Babka if (flags) { 1689edf14cdbSVlastimil Babka if (buf < end) 1690edf14cdbSVlastimil Babka *buf = '|'; 1691edf14cdbSVlastimil Babka buf++; 1692edf14cdbSVlastimil Babka } 1693edf14cdbSVlastimil Babka } 1694edf14cdbSVlastimil Babka 1695edf14cdbSVlastimil Babka if (flags) 169654433973SAndy Shevchenko buf = number(buf, end, flags, default_flag_spec); 1697edf14cdbSVlastimil Babka 1698edf14cdbSVlastimil Babka return buf; 1699edf14cdbSVlastimil Babka } 1700edf14cdbSVlastimil Babka 1701edf14cdbSVlastimil Babka static noinline_for_stack 1702edf14cdbSVlastimil Babka char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt) 1703edf14cdbSVlastimil Babka { 1704edf14cdbSVlastimil Babka unsigned long flags; 1705edf14cdbSVlastimil Babka const struct trace_print_flags *names; 1706edf14cdbSVlastimil Babka 1707edf14cdbSVlastimil Babka switch (fmt[1]) { 1708edf14cdbSVlastimil Babka case 'p': 1709edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1710edf14cdbSVlastimil Babka /* Remove zone id */ 1711edf14cdbSVlastimil Babka flags &= (1UL << NR_PAGEFLAGS) - 1; 1712edf14cdbSVlastimil Babka names = pageflag_names; 1713edf14cdbSVlastimil Babka break; 1714edf14cdbSVlastimil Babka case 'v': 1715edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1716edf14cdbSVlastimil Babka names = vmaflag_names; 1717edf14cdbSVlastimil Babka break; 1718edf14cdbSVlastimil Babka case 'g': 1719edf14cdbSVlastimil Babka flags = *(gfp_t *)flags_ptr; 1720edf14cdbSVlastimil Babka names = gfpflag_names; 1721edf14cdbSVlastimil Babka break; 1722edf14cdbSVlastimil Babka default: 1723edf14cdbSVlastimil Babka WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]); 1724edf14cdbSVlastimil Babka return buf; 1725edf14cdbSVlastimil Babka } 1726edf14cdbSVlastimil Babka 1727edf14cdbSVlastimil Babka return format_flags(buf, end, flags, names); 1728edf14cdbSVlastimil Babka } 1729edf14cdbSVlastimil Babka 1730ce4fecf1SPantelis Antoniou static const char *device_node_name_for_depth(const struct device_node *np, int depth) 1731ce4fecf1SPantelis Antoniou { 1732ce4fecf1SPantelis Antoniou for ( ; np && depth; depth--) 1733ce4fecf1SPantelis Antoniou np = np->parent; 1734ce4fecf1SPantelis Antoniou 1735ce4fecf1SPantelis Antoniou return kbasename(np->full_name); 1736ce4fecf1SPantelis Antoniou } 1737ce4fecf1SPantelis Antoniou 1738ce4fecf1SPantelis Antoniou static noinline_for_stack 1739ce4fecf1SPantelis Antoniou char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end) 1740ce4fecf1SPantelis Antoniou { 1741ce4fecf1SPantelis Antoniou int depth; 1742ce4fecf1SPantelis Antoniou const struct device_node *parent = np->parent; 1743ce4fecf1SPantelis Antoniou 1744ce4fecf1SPantelis Antoniou /* special case for root node */ 1745ce4fecf1SPantelis Antoniou if (!parent) 1746*d529ac41SPetr Mladek return string_nocheck(buf, end, "/", default_str_spec); 1747ce4fecf1SPantelis Antoniou 1748ce4fecf1SPantelis Antoniou for (depth = 0; parent->parent; depth++) 1749ce4fecf1SPantelis Antoniou parent = parent->parent; 1750ce4fecf1SPantelis Antoniou 1751ce4fecf1SPantelis Antoniou for ( ; depth >= 0; depth--) { 1752*d529ac41SPetr Mladek buf = string_nocheck(buf, end, "/", default_str_spec); 1753ce4fecf1SPantelis Antoniou buf = string(buf, end, device_node_name_for_depth(np, depth), 1754abd4fe62SAndy Shevchenko default_str_spec); 1755ce4fecf1SPantelis Antoniou } 1756ce4fecf1SPantelis Antoniou return buf; 1757ce4fecf1SPantelis Antoniou } 1758ce4fecf1SPantelis Antoniou 1759ce4fecf1SPantelis Antoniou static noinline_for_stack 1760ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn, 1761ce4fecf1SPantelis Antoniou struct printf_spec spec, const char *fmt) 1762ce4fecf1SPantelis Antoniou { 1763ce4fecf1SPantelis Antoniou char tbuf[sizeof("xxxx") + 1]; 1764ce4fecf1SPantelis Antoniou const char *p; 1765ce4fecf1SPantelis Antoniou int ret; 1766ce4fecf1SPantelis Antoniou char *buf_start = buf; 1767ce4fecf1SPantelis Antoniou struct property *prop; 1768ce4fecf1SPantelis Antoniou bool has_mult, pass; 1769ce4fecf1SPantelis Antoniou static const struct printf_spec num_spec = { 1770ce4fecf1SPantelis Antoniou .flags = SMALL, 1771ce4fecf1SPantelis Antoniou .field_width = -1, 1772ce4fecf1SPantelis Antoniou .precision = -1, 1773ce4fecf1SPantelis Antoniou .base = 10, 1774ce4fecf1SPantelis Antoniou }; 1775ce4fecf1SPantelis Antoniou 1776ce4fecf1SPantelis Antoniou struct printf_spec str_spec = spec; 1777ce4fecf1SPantelis Antoniou str_spec.field_width = -1; 1778ce4fecf1SPantelis Antoniou 1779ce4fecf1SPantelis Antoniou if (!IS_ENABLED(CONFIG_OF)) 1780*d529ac41SPetr Mladek return string_nocheck(buf, end, "(!OF)", spec); 1781ce4fecf1SPantelis Antoniou 1782ce4fecf1SPantelis Antoniou if ((unsigned long)dn < PAGE_SIZE) 1783*d529ac41SPetr Mladek return string_nocheck(buf, end, "(null)", spec); 1784ce4fecf1SPantelis Antoniou 1785ce4fecf1SPantelis Antoniou /* simple case without anything any more format specifiers */ 1786ce4fecf1SPantelis Antoniou fmt++; 1787ce4fecf1SPantelis Antoniou if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 1788ce4fecf1SPantelis Antoniou fmt = "f"; 1789ce4fecf1SPantelis Antoniou 1790ce4fecf1SPantelis Antoniou for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 17916d0a70a2SRob Herring int precision; 1792ce4fecf1SPantelis Antoniou if (pass) { 1793ce4fecf1SPantelis Antoniou if (buf < end) 1794ce4fecf1SPantelis Antoniou *buf = ':'; 1795ce4fecf1SPantelis Antoniou buf++; 1796ce4fecf1SPantelis Antoniou } 1797ce4fecf1SPantelis Antoniou 1798ce4fecf1SPantelis Antoniou switch (*fmt) { 1799ce4fecf1SPantelis Antoniou case 'f': /* full_name */ 1800ce4fecf1SPantelis Antoniou buf = device_node_gen_full_name(dn, buf, end); 1801ce4fecf1SPantelis Antoniou break; 1802ce4fecf1SPantelis Antoniou case 'n': /* name */ 18036d0a70a2SRob Herring p = kbasename(of_node_full_name(dn)); 18046d0a70a2SRob Herring precision = str_spec.precision; 18056d0a70a2SRob Herring str_spec.precision = strchrnul(p, '@') - p; 18066d0a70a2SRob Herring buf = string(buf, end, p, str_spec); 18076d0a70a2SRob Herring str_spec.precision = precision; 1808ce4fecf1SPantelis Antoniou break; 1809ce4fecf1SPantelis Antoniou case 'p': /* phandle */ 1810ce4fecf1SPantelis Antoniou buf = number(buf, end, (unsigned int)dn->phandle, num_spec); 1811ce4fecf1SPantelis Antoniou break; 1812ce4fecf1SPantelis Antoniou case 'P': /* path-spec */ 1813ce4fecf1SPantelis Antoniou p = kbasename(of_node_full_name(dn)); 1814ce4fecf1SPantelis Antoniou if (!p[1]) 1815ce4fecf1SPantelis Antoniou p = "/"; 1816ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 1817ce4fecf1SPantelis Antoniou break; 1818ce4fecf1SPantelis Antoniou case 'F': /* flags */ 1819ce4fecf1SPantelis Antoniou tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 1820ce4fecf1SPantelis Antoniou tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 1821ce4fecf1SPantelis Antoniou tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 1822ce4fecf1SPantelis Antoniou tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 1823ce4fecf1SPantelis Antoniou tbuf[4] = 0; 1824*d529ac41SPetr Mladek buf = string_nocheck(buf, end, tbuf, str_spec); 1825ce4fecf1SPantelis Antoniou break; 1826ce4fecf1SPantelis Antoniou case 'c': /* major compatible string */ 1827ce4fecf1SPantelis Antoniou ret = of_property_read_string(dn, "compatible", &p); 1828ce4fecf1SPantelis Antoniou if (!ret) 1829ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 1830ce4fecf1SPantelis Antoniou break; 1831ce4fecf1SPantelis Antoniou case 'C': /* full compatible string */ 1832ce4fecf1SPantelis Antoniou has_mult = false; 1833ce4fecf1SPantelis Antoniou of_property_for_each_string(dn, "compatible", prop, p) { 1834ce4fecf1SPantelis Antoniou if (has_mult) 1835*d529ac41SPetr Mladek buf = string_nocheck(buf, end, ",", str_spec); 1836*d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 1837ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 1838*d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 1839ce4fecf1SPantelis Antoniou 1840ce4fecf1SPantelis Antoniou has_mult = true; 1841ce4fecf1SPantelis Antoniou } 1842ce4fecf1SPantelis Antoniou break; 1843ce4fecf1SPantelis Antoniou default: 1844ce4fecf1SPantelis Antoniou break; 1845ce4fecf1SPantelis Antoniou } 1846ce4fecf1SPantelis Antoniou } 1847ce4fecf1SPantelis Antoniou 1848ce4fecf1SPantelis Antoniou return widen_string(buf, buf - buf_start, end, spec); 1849ce4fecf1SPantelis Antoniou } 1850ce4fecf1SPantelis Antoniou 18514d8a743cSLinus Torvalds /* 18524d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 18534d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 18544d8a743cSLinus Torvalds * specifiers. 18554d8a743cSLinus Torvalds * 18560b523769SJoe Perches * Please update scripts/checkpatch.pl when adding/removing conversion 18570b523769SJoe Perches * characters. (Search for "check for vsprintf extension"). 18580b523769SJoe Perches * 1859332d2e78SLinus Torvalds * Right now we handle: 18600fe1ef24SLinus Torvalds * 1861cdb7e52dSSergey Senozhatsky * - 'S' For symbolic direct pointers (or function descriptors) with offset 1862cdb7e52dSSergey Senozhatsky * - 's' For symbolic direct pointers (or function descriptors) without offset 1863cdb7e52dSSergey Senozhatsky * - 'F' Same as 'S' 1864cdb7e52dSSergey Senozhatsky * - 'f' Same as 's' 1865b0d33c2bSJoe Perches * - '[FfSs]R' as above with __builtin_extract_return_addr() translation 18660f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 1867c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 1868c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 1869dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 1870dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 1871dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 1872dbc760bcSTejun Heo * range-list format instead of hex format 1873dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 1874dd45c9cfSHarvey Harrison * usual colon-separated hex notation 18758a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 1876bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 1877c8e00060SJoe Perches * with a dash-separated hex notation 18787c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 18798a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 18808a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 18818a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 188210679643SDaniel Borkmann * [S][pfs] 188310679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 188410679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 18858a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 18868a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 18878a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 188810679643SDaniel Borkmann * [S][pfs] 188910679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 189010679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 189110679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 189210679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 189329cf519eSJoe Perches * http://tools.ietf.org/html/rfc5952 189471dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 189571dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 189671dca95dSAndy Shevchenko * details): 189771dca95dSAndy Shevchenko * a - ESCAPE_ANY 189871dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 189971dca95dSAndy Shevchenko * h - ESCAPE_HEX 190071dca95dSAndy Shevchenko * n - ESCAPE_NULL 190171dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 190271dca95dSAndy Shevchenko * p - ESCAPE_NP 190371dca95dSAndy Shevchenko * s - ESCAPE_SPACE 190471dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 19059ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 19069ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 19079ac6e44eSJoe Perches * Options for %pU are: 19089ac6e44eSJoe Perches * b big endian lower case hex (default) 19099ac6e44eSJoe Perches * B big endian UPPER case hex 19109ac6e44eSJoe Perches * l little endian lower case hex 19119ac6e44eSJoe Perches * L little endian UPPER case hex 19129ac6e44eSJoe Perches * big endian output byte order is: 19139ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 19149ac6e44eSJoe Perches * little endian output byte order is: 19159ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 19167db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 19177db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 19187db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 19197db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 19207db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 1921455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 1922c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 192331550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 192431550a16SAndy Shevchenko * a certain separator (' ' by default): 192531550a16SAndy Shevchenko * C colon 192631550a16SAndy Shevchenko * D dash 192731550a16SAndy Shevchenko * N no separator 192831550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 192931550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 1930aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 1931aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 1932c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 1933c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 19341031bc58SDmitry Monakhov * - 'g' For block_device name (gendisk + partition number) 19354d42c447SAndy Shevchenko * - 't[R][dt][r]' For time and date as represented: 19364d42c447SAndy Shevchenko * R struct rtc_time 1937900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 1938900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 1939900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 1940900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 1941edf14cdbSVlastimil Babka * - 'G' For flags to be printed as a collection of symbolic strings that would 1942edf14cdbSVlastimil Babka * construct the specific value. Supported flags given by option: 1943edf14cdbSVlastimil Babka * p page flags (see struct page) given as pointer to unsigned long 1944edf14cdbSVlastimil Babka * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 1945edf14cdbSVlastimil Babka * v vma flags (VM_*) given as pointer to unsigned long 1946ce4fecf1SPantelis Antoniou * - 'OF[fnpPcCF]' For a device tree object 1947ce4fecf1SPantelis Antoniou * Without any optional arguments prints the full_name 1948ce4fecf1SPantelis Antoniou * f device node full_name 1949ce4fecf1SPantelis Antoniou * n device node name 1950ce4fecf1SPantelis Antoniou * p device node phandle 1951ce4fecf1SPantelis Antoniou * P device node path spec (name + @unit) 1952ce4fecf1SPantelis Antoniou * F device node flags 1953ce4fecf1SPantelis Antoniou * c major compatible string 1954ce4fecf1SPantelis Antoniou * C full compatible string 19557b1924a1STobin C. Harding * - 'x' For printing the address. Equivalent to "%lx". 19567b1924a1STobin C. Harding * 1957b3ed2321STobin C. Harding * ** When making changes please also update: 1958b3ed2321STobin C. Harding * Documentation/core-api/printk-formats.rst 19599ac6e44eSJoe Perches * 1960ad67b74dSTobin C. Harding * Note: The default behaviour (unadorned %p) is to hash the address, 1961ad67b74dSTobin C. Harding * rendering it useful as a unique identifier. 19624d8a743cSLinus Torvalds */ 1963cf3b429bSJoe Perches static noinline_for_stack 1964cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 1965fef20d9cSFrederic Weisbecker struct printf_spec spec) 196678a8bf69SLinus Torvalds { 196780c9eb46SRasmus Villemoes const int default_width = 2 * sizeof(void *); 1968725fe002SGrant Likely 19693a129cc2SAdam Borowski if (!ptr && *fmt != 'K' && *fmt != 'x') { 19705e057981SJoe Perches /* 19715e057981SJoe Perches * Print (null) with the same width as a pointer so it makes 19725e057981SJoe Perches * tabular output look nice. 19735e057981SJoe Perches */ 19745e057981SJoe Perches if (spec.field_width == -1) 1975725fe002SGrant Likely spec.field_width = default_width; 1976*d529ac41SPetr Mladek return string_nocheck(buf, end, "(null)", spec); 19775e057981SJoe Perches } 1978d97106abSLinus Torvalds 19790fe1ef24SLinus Torvalds switch (*fmt) { 19800fe1ef24SLinus Torvalds case 'F': 19810c8b946eSFrederic Weisbecker case 'f': 19820fe1ef24SLinus Torvalds case 'S': 19839ac6e44eSJoe Perches case 's': 198404b8eb7aSSergey Senozhatsky ptr = dereference_symbol_descriptor(ptr); 198504b8eb7aSSergey Senozhatsky /* Fallthrough */ 19860f77a8d3SNamhyung Kim case 'B': 1987b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 1988332d2e78SLinus Torvalds case 'R': 1989c7dabef8SBjorn Helgaas case 'r': 1990fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 199131550a16SAndy Shevchenko case 'h': 199231550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 1993dbc760bcSTejun Heo case 'b': 1994dbc760bcSTejun Heo switch (fmt[1]) { 1995dbc760bcSTejun Heo case 'l': 1996dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 1997dbc760bcSTejun Heo default: 1998dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 1999dbc760bcSTejun Heo } 20008a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 20018a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 200276597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 200376597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 20048a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 20058a27f7c9SJoe Perches case 'I': /* Formatted IP supported 20068a27f7c9SJoe Perches * 4: 1.2.3.4 20078a27f7c9SJoe Perches * 6: 0001:0203:...:0708 20088a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 20098a27f7c9SJoe Perches */ 20108a27f7c9SJoe Perches case 'i': /* Contiguous: 20118a27f7c9SJoe Perches * 4: 001.002.003.004 20128a27f7c9SJoe Perches * 6: 000102...0f 20138a27f7c9SJoe Perches */ 20148a27f7c9SJoe Perches switch (fmt[1]) { 20158a27f7c9SJoe Perches case '6': 20168a27f7c9SJoe Perches return ip6_addr_string(buf, end, ptr, spec, fmt); 20178a27f7c9SJoe Perches case '4': 20188a27f7c9SJoe Perches return ip4_addr_string(buf, end, ptr, spec, fmt); 201910679643SDaniel Borkmann case 'S': { 202010679643SDaniel Borkmann const union { 202110679643SDaniel Borkmann struct sockaddr raw; 202210679643SDaniel Borkmann struct sockaddr_in v4; 202310679643SDaniel Borkmann struct sockaddr_in6 v6; 202410679643SDaniel Borkmann } *sa = ptr; 202510679643SDaniel Borkmann 202610679643SDaniel Borkmann switch (sa->raw.sa_family) { 202710679643SDaniel Borkmann case AF_INET: 202810679643SDaniel Borkmann return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 202910679643SDaniel Borkmann case AF_INET6: 203010679643SDaniel Borkmann return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 203110679643SDaniel Borkmann default: 2032*d529ac41SPetr Mladek return string_nocheck(buf, end, "(invalid address)", spec); 203310679643SDaniel Borkmann }} 20348a27f7c9SJoe Perches } 20354aa99606SHarvey Harrison break; 203671dca95dSAndy Shevchenko case 'E': 203771dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 20389ac6e44eSJoe Perches case 'U': 20399ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 20407db6f5fbSJoe Perches case 'V': 20415756b76eSJan Beulich { 20425756b76eSJan Beulich va_list va; 20435756b76eSJan Beulich 20445756b76eSJan Beulich va_copy(va, *((struct va_format *)ptr)->va); 20455756b76eSJan Beulich buf += vsnprintf(buf, end > buf ? end - buf : 0, 20465756b76eSJan Beulich ((struct va_format *)ptr)->fmt, va); 20475756b76eSJan Beulich va_end(va); 20485756b76eSJan Beulich return buf; 20495756b76eSJan Beulich } 2050455cd5abSDan Rosenberg case 'K': 205157e73442STobin C. Harding return restricted_pointer(buf, end, ptr, spec); 2052c8f44affSMichał Mirosław case 'N': 2053431bca24SGeert Uytterhoeven return netdev_bits(buf, end, ptr, spec, fmt); 20547d799210SStepan Moskovchenko case 'a': 20553cab1e71SAndy Shevchenko return address_val(buf, end, ptr, fmt); 20564b6ccca7SAl Viro case 'd': 20574b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 20584d42c447SAndy Shevchenko case 't': 20594d42c447SAndy Shevchenko return time_and_date(buf, end, ptr, spec, fmt); 2060900cca29SGeert Uytterhoeven case 'C': 2061900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 20624b6ccca7SAl Viro case 'D': 20634b6ccca7SAl Viro return dentry_name(buf, end, 20644b6ccca7SAl Viro ((const struct file *)ptr)->f_path.dentry, 20654b6ccca7SAl Viro spec, fmt); 20661031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 20671031bc58SDmitry Monakhov case 'g': 20681031bc58SDmitry Monakhov return bdev_name(buf, end, ptr, spec, fmt); 20691031bc58SDmitry Monakhov #endif 20701031bc58SDmitry Monakhov 2071edf14cdbSVlastimil Babka case 'G': 2072edf14cdbSVlastimil Babka return flags_string(buf, end, ptr, fmt); 2073ce4fecf1SPantelis Antoniou case 'O': 2074ce4fecf1SPantelis Antoniou switch (fmt[1]) { 2075ce4fecf1SPantelis Antoniou case 'F': 2076ce4fecf1SPantelis Antoniou return device_node_string(buf, end, ptr, spec, fmt + 1); 2077ce4fecf1SPantelis Antoniou } 2078554ec508SBart Van Assche break; 20797b1924a1STobin C. Harding case 'x': 20807b1924a1STobin C. Harding return pointer_string(buf, end, ptr, spec); 20810fe1ef24SLinus Torvalds } 2082fef20d9cSFrederic Weisbecker 2083ad67b74dSTobin C. Harding /* default is to _not_ leak addresses, hash before printing */ 2084ad67b74dSTobin C. Harding return ptr_to_id(buf, end, ptr, spec); 2085fef20d9cSFrederic Weisbecker } 2086fef20d9cSFrederic Weisbecker 2087fef20d9cSFrederic Weisbecker /* 2088fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 2089fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 2090fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 2091fef20d9cSFrederic Weisbecker * to go on the next call). 2092fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 2093fef20d9cSFrederic Weisbecker * 2094fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 2095fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 2096fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 20975b5e0928SAlexey Dobriyan * 'Z' changed to 'z' --adobriyan 2017-01-25 2098fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 2099fef20d9cSFrederic Weisbecker * 2100fef20d9cSFrederic Weisbecker * @fmt: the format string 2101fef20d9cSFrederic Weisbecker * @type of the token returned 2102fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 2103fef20d9cSFrederic Weisbecker * @field_width: overwritten width 2104fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 2105fef20d9cSFrederic Weisbecker * @precision: precision of a number 2106fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 2107fef20d9cSFrederic Weisbecker */ 2108cf3b429bSJoe Perches static noinline_for_stack 2109cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 2110fef20d9cSFrederic Weisbecker { 2111fef20d9cSFrederic Weisbecker const char *start = fmt; 2112d0484193SRasmus Villemoes char qualifier; 2113fef20d9cSFrederic Weisbecker 2114fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 2115ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 2116fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 2117fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 2118fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 2119fef20d9cSFrederic Weisbecker } 2120fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2121fef20d9cSFrederic Weisbecker goto precision; 2122fef20d9cSFrederic Weisbecker } 2123fef20d9cSFrederic Weisbecker 2124fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 2125fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 2126fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2127fef20d9cSFrederic Weisbecker spec->precision = 0; 2128fef20d9cSFrederic Weisbecker 2129fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2130fef20d9cSFrederic Weisbecker goto qualifier; 2131fef20d9cSFrederic Weisbecker } 2132fef20d9cSFrederic Weisbecker 2133fef20d9cSFrederic Weisbecker /* By default */ 2134fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2135fef20d9cSFrederic Weisbecker 2136fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 2137fef20d9cSFrederic Weisbecker if (*fmt == '%') 2138fef20d9cSFrederic Weisbecker break; 2139fef20d9cSFrederic Weisbecker } 2140fef20d9cSFrederic Weisbecker 2141fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 2142fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 2143fef20d9cSFrederic Weisbecker return fmt - start; 2144fef20d9cSFrederic Weisbecker 2145fef20d9cSFrederic Weisbecker /* Process flags */ 2146fef20d9cSFrederic Weisbecker spec->flags = 0; 2147fef20d9cSFrederic Weisbecker 2148fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 2149fef20d9cSFrederic Weisbecker bool found = true; 2150fef20d9cSFrederic Weisbecker 2151fef20d9cSFrederic Weisbecker ++fmt; 2152fef20d9cSFrederic Weisbecker 2153fef20d9cSFrederic Weisbecker switch (*fmt) { 2154fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 2155fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 2156fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 2157fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 2158fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 2159fef20d9cSFrederic Weisbecker default: found = false; 2160fef20d9cSFrederic Weisbecker } 2161fef20d9cSFrederic Weisbecker 2162fef20d9cSFrederic Weisbecker if (!found) 2163fef20d9cSFrederic Weisbecker break; 2164fef20d9cSFrederic Weisbecker } 2165fef20d9cSFrederic Weisbecker 2166fef20d9cSFrederic Weisbecker /* get field width */ 2167fef20d9cSFrederic Weisbecker spec->field_width = -1; 2168fef20d9cSFrederic Weisbecker 2169fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 2170fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 2171fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 2172fef20d9cSFrederic Weisbecker /* it's the next argument */ 2173ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 2174fef20d9cSFrederic Weisbecker return ++fmt - start; 2175fef20d9cSFrederic Weisbecker } 2176fef20d9cSFrederic Weisbecker 2177fef20d9cSFrederic Weisbecker precision: 2178fef20d9cSFrederic Weisbecker /* get the precision */ 2179fef20d9cSFrederic Weisbecker spec->precision = -1; 2180fef20d9cSFrederic Weisbecker if (*fmt == '.') { 2181fef20d9cSFrederic Weisbecker ++fmt; 2182fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 2183fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 2184fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2185fef20d9cSFrederic Weisbecker spec->precision = 0; 2186fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 2187fef20d9cSFrederic Weisbecker /* it's the next argument */ 2188adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 2189fef20d9cSFrederic Weisbecker return ++fmt - start; 2190fef20d9cSFrederic Weisbecker } 2191fef20d9cSFrederic Weisbecker } 2192fef20d9cSFrederic Weisbecker 2193fef20d9cSFrederic Weisbecker qualifier: 2194fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 2195d0484193SRasmus Villemoes qualifier = 0; 219675fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 21975b5e0928SAlexey Dobriyan *fmt == 'z' || *fmt == 't') { 2198d0484193SRasmus Villemoes qualifier = *fmt++; 2199d0484193SRasmus Villemoes if (unlikely(qualifier == *fmt)) { 2200d0484193SRasmus Villemoes if (qualifier == 'l') { 2201d0484193SRasmus Villemoes qualifier = 'L'; 2202fef20d9cSFrederic Weisbecker ++fmt; 2203d0484193SRasmus Villemoes } else if (qualifier == 'h') { 2204d0484193SRasmus Villemoes qualifier = 'H'; 2205a4e94ef0SZhaolei ++fmt; 2206a4e94ef0SZhaolei } 2207fef20d9cSFrederic Weisbecker } 2208fef20d9cSFrederic Weisbecker } 2209fef20d9cSFrederic Weisbecker 2210fef20d9cSFrederic Weisbecker /* default base */ 2211fef20d9cSFrederic Weisbecker spec->base = 10; 2212fef20d9cSFrederic Weisbecker switch (*fmt) { 2213fef20d9cSFrederic Weisbecker case 'c': 2214fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 2215fef20d9cSFrederic Weisbecker return ++fmt - start; 2216fef20d9cSFrederic Weisbecker 2217fef20d9cSFrederic Weisbecker case 's': 2218fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 2219fef20d9cSFrederic Weisbecker return ++fmt - start; 2220fef20d9cSFrederic Weisbecker 2221fef20d9cSFrederic Weisbecker case 'p': 2222fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 2223ffbfed03SRasmus Villemoes return ++fmt - start; 2224fef20d9cSFrederic Weisbecker 2225fef20d9cSFrederic Weisbecker case '%': 2226fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 2227fef20d9cSFrederic Weisbecker return ++fmt - start; 2228fef20d9cSFrederic Weisbecker 2229fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 2230fef20d9cSFrederic Weisbecker case 'o': 2231fef20d9cSFrederic Weisbecker spec->base = 8; 2232fef20d9cSFrederic Weisbecker break; 2233fef20d9cSFrederic Weisbecker 2234fef20d9cSFrederic Weisbecker case 'x': 2235fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 22367e6bd6f3SAndy Shevchenko /* fall through */ 2237fef20d9cSFrederic Weisbecker 2238fef20d9cSFrederic Weisbecker case 'X': 2239fef20d9cSFrederic Weisbecker spec->base = 16; 2240fef20d9cSFrederic Weisbecker break; 2241fef20d9cSFrederic Weisbecker 2242fef20d9cSFrederic Weisbecker case 'd': 2243fef20d9cSFrederic Weisbecker case 'i': 224439e874f8SFrederic Weisbecker spec->flags |= SIGN; 2245fef20d9cSFrederic Weisbecker case 'u': 2246fef20d9cSFrederic Weisbecker break; 2247fef20d9cSFrederic Weisbecker 2248708d96fdSRyan Mallon case 'n': 2249708d96fdSRyan Mallon /* 2250b006f19bSRasmus Villemoes * Since %n poses a greater security risk than 2251b006f19bSRasmus Villemoes * utility, treat it as any other invalid or 2252b006f19bSRasmus Villemoes * unsupported format specifier. 2253708d96fdSRyan Mallon */ 2254708d96fdSRyan Mallon /* Fall-through */ 2255708d96fdSRyan Mallon 2256fef20d9cSFrederic Weisbecker default: 2257b006f19bSRasmus Villemoes WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 2258fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 2259fef20d9cSFrederic Weisbecker return fmt - start; 2260fef20d9cSFrederic Weisbecker } 2261fef20d9cSFrederic Weisbecker 2262d0484193SRasmus Villemoes if (qualifier == 'L') 2263fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 2264d0484193SRasmus Villemoes else if (qualifier == 'l') { 226551be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 226651be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 22675b5e0928SAlexey Dobriyan } else if (qualifier == 'z') { 2268fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 2269d0484193SRasmus Villemoes } else if (qualifier == 't') { 2270fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 2271d0484193SRasmus Villemoes } else if (qualifier == 'H') { 227251be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 227351be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 2274d0484193SRasmus Villemoes } else if (qualifier == 'h') { 227551be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 227651be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 2277fef20d9cSFrederic Weisbecker } else { 227851be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 227951be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 2280fef20d9cSFrederic Weisbecker } 2281fef20d9cSFrederic Weisbecker 2282fef20d9cSFrederic Weisbecker return ++fmt - start; 228378a8bf69SLinus Torvalds } 228478a8bf69SLinus Torvalds 22854d72ba01SRasmus Villemoes static void 22864d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width) 22874d72ba01SRasmus Villemoes { 22884d72ba01SRasmus Villemoes spec->field_width = width; 22894d72ba01SRasmus Villemoes if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 22904d72ba01SRasmus Villemoes spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 22914d72ba01SRasmus Villemoes } 22924d72ba01SRasmus Villemoes } 22934d72ba01SRasmus Villemoes 22944d72ba01SRasmus Villemoes static void 22954d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec) 22964d72ba01SRasmus Villemoes { 22974d72ba01SRasmus Villemoes spec->precision = prec; 22984d72ba01SRasmus Villemoes if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 22994d72ba01SRasmus Villemoes spec->precision = clamp(prec, 0, PRECISION_MAX); 23004d72ba01SRasmus Villemoes } 23014d72ba01SRasmus Villemoes } 23024d72ba01SRasmus Villemoes 23031da177e4SLinus Torvalds /** 23041da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 23051da177e4SLinus Torvalds * @buf: The buffer to place the result into 23061da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 23071da177e4SLinus Torvalds * @fmt: The format string to use 23081da177e4SLinus Torvalds * @args: Arguments for the format string 23091da177e4SLinus Torvalds * 2310d7ec9a05SRasmus Villemoes * This function generally follows C99 vsnprintf, but has some 2311d7ec9a05SRasmus Villemoes * extensions and a few limitations: 2312d7ec9a05SRasmus Villemoes * 23136cc89134Smchehab@s-opensource.com * - ``%n`` is unsupported 23146cc89134Smchehab@s-opensource.com * - ``%p*`` is handled by pointer() 231520036fdcSAndi Kleen * 231627e7c0e8SJonathan Corbet * See pointer() or Documentation/core-api/printk-formats.rst for more 23175e4ee7b1SMartin Kletzander * extensive description. 23185e4ee7b1SMartin Kletzander * 23195e4ee7b1SMartin Kletzander * **Please update the documentation in both places when making changes** 232080f548e0SAndrew Morton * 23211da177e4SLinus Torvalds * The return value is the number of characters which would 23221da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 23231da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 23241da177e4SLinus Torvalds * number of characters written into @buf as return value 232572fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 23261da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 23271da177e4SLinus Torvalds * string is truncated. 23281da177e4SLinus Torvalds * 2329ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 23301da177e4SLinus Torvalds */ 23311da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 23321da177e4SLinus Torvalds { 23331da177e4SLinus Torvalds unsigned long long num; 2334d4be151bSAndré Goddard Rosa char *str, *end; 2335fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 23361da177e4SLinus Torvalds 2337f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 2338f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 23392aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 23401da177e4SLinus Torvalds return 0; 23411da177e4SLinus Torvalds 23421da177e4SLinus Torvalds str = buf; 2343f796937aSJeremy Fitzhardinge end = buf + size; 23441da177e4SLinus Torvalds 2345f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 2346f796937aSJeremy Fitzhardinge if (end < buf) { 23471da177e4SLinus Torvalds end = ((void *)-1); 2348f796937aSJeremy Fitzhardinge size = end - buf; 23491da177e4SLinus Torvalds } 23501da177e4SLinus Torvalds 2351fef20d9cSFrederic Weisbecker while (*fmt) { 2352fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2353d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2354fef20d9cSFrederic Weisbecker 2355fef20d9cSFrederic Weisbecker fmt += read; 2356fef20d9cSFrederic Weisbecker 2357fef20d9cSFrederic Weisbecker switch (spec.type) { 2358fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2359fef20d9cSFrederic Weisbecker int copy = read; 2360fef20d9cSFrederic Weisbecker if (str < end) { 2361fef20d9cSFrederic Weisbecker if (copy > end - str) 2362fef20d9cSFrederic Weisbecker copy = end - str; 2363fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2364fef20d9cSFrederic Weisbecker } 2365fef20d9cSFrederic Weisbecker str += read; 2366fef20d9cSFrederic Weisbecker break; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds 2369ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 23704d72ba01SRasmus Villemoes set_field_width(&spec, va_arg(args, int)); 2371fef20d9cSFrederic Weisbecker break; 23721da177e4SLinus Torvalds 2373fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 23744d72ba01SRasmus Villemoes set_precision(&spec, va_arg(args, int)); 2375fef20d9cSFrederic Weisbecker break; 23761da177e4SLinus Torvalds 2377d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2378d4be151bSAndré Goddard Rosa char c; 2379d4be151bSAndré Goddard Rosa 2380fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2381fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2382f796937aSJeremy Fitzhardinge if (str < end) 23831da177e4SLinus Torvalds *str = ' '; 23841da177e4SLinus Torvalds ++str; 2385fef20d9cSFrederic Weisbecker 23861da177e4SLinus Torvalds } 23871da177e4SLinus Torvalds } 23881da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 2389f796937aSJeremy Fitzhardinge if (str < end) 23901da177e4SLinus Torvalds *str = c; 23911da177e4SLinus Torvalds ++str; 2392fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2393f796937aSJeremy Fitzhardinge if (str < end) 23941da177e4SLinus Torvalds *str = ' '; 23951da177e4SLinus Torvalds ++str; 23961da177e4SLinus Torvalds } 2397fef20d9cSFrederic Weisbecker break; 2398d4be151bSAndré Goddard Rosa } 23991da177e4SLinus Torvalds 2400fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 2401fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 2402fef20d9cSFrederic Weisbecker break; 24031da177e4SLinus Torvalds 2404fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2405ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 2406fef20d9cSFrederic Weisbecker spec); 2407fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 24084d8a743cSLinus Torvalds fmt++; 2409fef20d9cSFrederic Weisbecker break; 24101da177e4SLinus Torvalds 2411fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2412f796937aSJeremy Fitzhardinge if (str < end) 24131da177e4SLinus Torvalds *str = '%'; 24141da177e4SLinus Torvalds ++str; 24151da177e4SLinus Torvalds break; 24161da177e4SLinus Torvalds 2417fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 2418b006f19bSRasmus Villemoes /* 2419b006f19bSRasmus Villemoes * Presumably the arguments passed gcc's type 2420b006f19bSRasmus Villemoes * checking, but there is no safe or sane way 2421b006f19bSRasmus Villemoes * for us to continue parsing the format and 2422b006f19bSRasmus Villemoes * fetching from the va_list; the remaining 2423b006f19bSRasmus Villemoes * specifiers and arguments would be out of 2424b006f19bSRasmus Villemoes * sync. 2425b006f19bSRasmus Villemoes */ 2426b006f19bSRasmus Villemoes goto out; 2427fef20d9cSFrederic Weisbecker 2428fef20d9cSFrederic Weisbecker default: 2429fef20d9cSFrederic Weisbecker switch (spec.type) { 2430fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2431fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 2432fef20d9cSFrederic Weisbecker break; 2433fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2434fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 2435fef20d9cSFrederic Weisbecker break; 2436fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2437fef20d9cSFrederic Weisbecker num = va_arg(args, long); 2438fef20d9cSFrederic Weisbecker break; 2439fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2440ef124960SJason Gunthorpe if (spec.flags & SIGN) 2441ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 2442ef124960SJason Gunthorpe else 2443fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 2444fef20d9cSFrederic Weisbecker break; 2445fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2446fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 2447fef20d9cSFrederic Weisbecker break; 2448a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2449a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 2450a4e94ef0SZhaolei break; 2451a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2452a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 2453a4e94ef0SZhaolei break; 2454fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2455fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 2456fef20d9cSFrederic Weisbecker break; 2457fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2458fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 2459fef20d9cSFrederic Weisbecker break; 246039e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 246139e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 2462fef20d9cSFrederic Weisbecker break; 2463fef20d9cSFrederic Weisbecker default: 2464fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 24651da177e4SLinus Torvalds } 2466fef20d9cSFrederic Weisbecker 2467fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 24681da177e4SLinus Torvalds } 2469fef20d9cSFrederic Weisbecker } 2470fef20d9cSFrederic Weisbecker 2471b006f19bSRasmus Villemoes out: 2472f796937aSJeremy Fitzhardinge if (size > 0) { 2473f796937aSJeremy Fitzhardinge if (str < end) 24741da177e4SLinus Torvalds *str = '\0'; 2475f796937aSJeremy Fitzhardinge else 24760a6047eeSLinus Torvalds end[-1] = '\0'; 2477f796937aSJeremy Fitzhardinge } 2478fef20d9cSFrederic Weisbecker 2479f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 24801da177e4SLinus Torvalds return str-buf; 2481fef20d9cSFrederic Weisbecker 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 24841da177e4SLinus Torvalds 24851da177e4SLinus Torvalds /** 24861da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 24871da177e4SLinus Torvalds * @buf: The buffer to place the result into 24881da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 24891da177e4SLinus Torvalds * @fmt: The format string to use 24901da177e4SLinus Torvalds * @args: Arguments for the format string 24911da177e4SLinus Torvalds * 24921da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2493b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 24941da177e4SLinus Torvalds * returns 0. 24951da177e4SLinus Torvalds * 2496ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 249720036fdcSAndi Kleen * 249820036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 24991da177e4SLinus Torvalds */ 25001da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 25011da177e4SLinus Torvalds { 25021da177e4SLinus Torvalds int i; 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 25057b9186f5SAndré Goddard Rosa 2506b921c69fSAnton Arapov if (likely(i < size)) 2507b921c69fSAnton Arapov return i; 2508b921c69fSAnton Arapov if (size != 0) 2509b921c69fSAnton Arapov return size - 1; 2510b921c69fSAnton Arapov return 0; 25111da177e4SLinus Torvalds } 25121da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds /** 25151da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 25161da177e4SLinus Torvalds * @buf: The buffer to place the result into 25171da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 25181da177e4SLinus Torvalds * @fmt: The format string to use 25191da177e4SLinus Torvalds * @...: Arguments for the format string 25201da177e4SLinus Torvalds * 25211da177e4SLinus Torvalds * The return value is the number of characters which would be 25221da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 25231da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 25241da177e4SLinus Torvalds * @size, the resulting string is truncated. 252520036fdcSAndi Kleen * 252620036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 25271da177e4SLinus Torvalds */ 25281da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 25291da177e4SLinus Torvalds { 25301da177e4SLinus Torvalds va_list args; 25311da177e4SLinus Torvalds int i; 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds va_start(args, fmt); 25341da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 25351da177e4SLinus Torvalds va_end(args); 25367b9186f5SAndré Goddard Rosa 25371da177e4SLinus Torvalds return i; 25381da177e4SLinus Torvalds } 25391da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 25401da177e4SLinus Torvalds 25411da177e4SLinus Torvalds /** 25421da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 25431da177e4SLinus Torvalds * @buf: The buffer to place the result into 25441da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 25451da177e4SLinus Torvalds * @fmt: The format string to use 25461da177e4SLinus Torvalds * @...: Arguments for the format string 25471da177e4SLinus Torvalds * 25481da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2549b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 25501da177e4SLinus Torvalds */ 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 25531da177e4SLinus Torvalds { 25541da177e4SLinus Torvalds va_list args; 25551da177e4SLinus Torvalds int i; 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds va_start(args, fmt); 2558b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 25591da177e4SLinus Torvalds va_end(args); 25607b9186f5SAndré Goddard Rosa 2561b903c0b8SChangli Gao return i; 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 25641da177e4SLinus Torvalds 25651da177e4SLinus Torvalds /** 25661da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 25671da177e4SLinus Torvalds * @buf: The buffer to place the result into 25681da177e4SLinus Torvalds * @fmt: The format string to use 25691da177e4SLinus Torvalds * @args: Arguments for the format string 25701da177e4SLinus Torvalds * 25711da177e4SLinus Torvalds * The function returns the number of characters written 257272fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 25731da177e4SLinus Torvalds * buffer overflows. 25741da177e4SLinus Torvalds * 2575ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 257620036fdcSAndi Kleen * 257720036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 25781da177e4SLinus Torvalds */ 25791da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 25801da177e4SLinus Torvalds { 25811da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 25821da177e4SLinus Torvalds } 25831da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds /** 25861da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 25871da177e4SLinus Torvalds * @buf: The buffer to place the result into 25881da177e4SLinus Torvalds * @fmt: The format string to use 25891da177e4SLinus Torvalds * @...: Arguments for the format string 25901da177e4SLinus Torvalds * 25911da177e4SLinus Torvalds * The function returns the number of characters written 259272fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 25931da177e4SLinus Torvalds * buffer overflows. 259420036fdcSAndi Kleen * 259520036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 25961da177e4SLinus Torvalds */ 25971da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 25981da177e4SLinus Torvalds { 25991da177e4SLinus Torvalds va_list args; 26001da177e4SLinus Torvalds int i; 26011da177e4SLinus Torvalds 26021da177e4SLinus Torvalds va_start(args, fmt); 26031da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 26041da177e4SLinus Torvalds va_end(args); 26057b9186f5SAndré Goddard Rosa 26061da177e4SLinus Torvalds return i; 26071da177e4SLinus Torvalds } 26081da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 26091da177e4SLinus Torvalds 26104370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 26114370aa4aSLai Jiangshan /* 26124370aa4aSLai Jiangshan * bprintf service: 26134370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 26144370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 26154370aa4aSLai Jiangshan */ 26164370aa4aSLai Jiangshan 26174370aa4aSLai Jiangshan /** 26184370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 26194370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 26204370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 26214370aa4aSLai Jiangshan * @fmt: The format string to use 26224370aa4aSLai Jiangshan * @args: Arguments for the format string 26234370aa4aSLai Jiangshan * 26244370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 2625da3dae54SMasanari Iida * is skipped. 26264370aa4aSLai Jiangshan * 26274370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 26284370aa4aSLai Jiangshan * the given input. 26294370aa4aSLai Jiangshan * 26304370aa4aSLai Jiangshan * NOTE: 26314370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 26324370aa4aSLai Jiangshan * valid for bstr_printf(). 26334370aa4aSLai Jiangshan */ 26344370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 26354370aa4aSLai Jiangshan { 2636fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 26374370aa4aSLai Jiangshan char *str, *end; 2638841a915dSSteven Rostedt (VMware) int width; 26394370aa4aSLai Jiangshan 26404370aa4aSLai Jiangshan str = (char *)bin_buf; 26414370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 26424370aa4aSLai Jiangshan 26434370aa4aSLai Jiangshan #define save_arg(type) \ 2644841a915dSSteven Rostedt (VMware) ({ \ 26454370aa4aSLai Jiangshan unsigned long long value; \ 2646841a915dSSteven Rostedt (VMware) if (sizeof(type) == 8) { \ 2647841a915dSSteven Rostedt (VMware) unsigned long long val8; \ 26484370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 2649841a915dSSteven Rostedt (VMware) val8 = va_arg(args, unsigned long long); \ 26504370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 2651841a915dSSteven Rostedt (VMware) *(u32 *)str = *(u32 *)&val8; \ 2652841a915dSSteven Rostedt (VMware) *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 26534370aa4aSLai Jiangshan } \ 2654841a915dSSteven Rostedt (VMware) value = val8; \ 26554370aa4aSLai Jiangshan } else { \ 2656841a915dSSteven Rostedt (VMware) unsigned int val4; \ 26574370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 2658841a915dSSteven Rostedt (VMware) val4 = va_arg(args, int); \ 26594370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 2660841a915dSSteven Rostedt (VMware) *(typeof(type) *)str = (type)(long)val4; \ 2661841a915dSSteven Rostedt (VMware) value = (unsigned long long)val4; \ 26624370aa4aSLai Jiangshan } \ 26634370aa4aSLai Jiangshan str += sizeof(type); \ 2664841a915dSSteven Rostedt (VMware) value; \ 2665841a915dSSteven Rostedt (VMware) }) 26664370aa4aSLai Jiangshan 2667fef20d9cSFrederic Weisbecker while (*fmt) { 2668d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 26694370aa4aSLai Jiangshan 2670fef20d9cSFrederic Weisbecker fmt += read; 2671fef20d9cSFrederic Weisbecker 2672fef20d9cSFrederic Weisbecker switch (spec.type) { 2673fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 2674d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 2675fef20d9cSFrederic Weisbecker break; 2676b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2677b006f19bSRasmus Villemoes goto out; 2678fef20d9cSFrederic Weisbecker 2679ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2680fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2681841a915dSSteven Rostedt (VMware) width = (int)save_arg(int); 2682841a915dSSteven Rostedt (VMware) /* Pointers may require the width */ 2683841a915dSSteven Rostedt (VMware) if (*fmt == 'p') 2684841a915dSSteven Rostedt (VMware) set_field_width(&spec, width); 2685fef20d9cSFrederic Weisbecker break; 26864370aa4aSLai Jiangshan 2687fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 26884370aa4aSLai Jiangshan save_arg(char); 2689fef20d9cSFrederic Weisbecker break; 2690fef20d9cSFrederic Weisbecker 2691fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 26924370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 26934370aa4aSLai Jiangshan size_t len; 26946c356634SAndré Goddard Rosa 26954370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 26964370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 26970f4f81dcSAndré Goddard Rosa save_str = "(null)"; 26986c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 26996c356634SAndré Goddard Rosa if (str + len < end) 27006c356634SAndré Goddard Rosa memcpy(str, save_str, len); 27016c356634SAndré Goddard Rosa str += len; 2702fef20d9cSFrederic Weisbecker break; 27034370aa4aSLai Jiangshan } 2704fef20d9cSFrederic Weisbecker 2705fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2706841a915dSSteven Rostedt (VMware) /* Dereferenced pointers must be done now */ 2707841a915dSSteven Rostedt (VMware) switch (*fmt) { 2708841a915dSSteven Rostedt (VMware) /* Dereference of functions is still OK */ 2709841a915dSSteven Rostedt (VMware) case 'S': 2710841a915dSSteven Rostedt (VMware) case 's': 2711841a915dSSteven Rostedt (VMware) case 'F': 2712841a915dSSteven Rostedt (VMware) case 'f': 27131e6338cfSSteven Rostedt (VMware) case 'x': 27141e6338cfSSteven Rostedt (VMware) case 'K': 27154370aa4aSLai Jiangshan save_arg(void *); 2716841a915dSSteven Rostedt (VMware) break; 2717841a915dSSteven Rostedt (VMware) default: 2718841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 2719841a915dSSteven Rostedt (VMware) save_arg(void *); 2720841a915dSSteven Rostedt (VMware) break; 2721841a915dSSteven Rostedt (VMware) } 2722841a915dSSteven Rostedt (VMware) str = pointer(fmt, str, end, va_arg(args, void *), 2723841a915dSSteven Rostedt (VMware) spec); 2724841a915dSSteven Rostedt (VMware) if (str + 1 < end) 2725841a915dSSteven Rostedt (VMware) *str++ = '\0'; 2726841a915dSSteven Rostedt (VMware) else 2727841a915dSSteven Rostedt (VMware) end[-1] = '\0'; /* Must be nul terminated */ 2728841a915dSSteven Rostedt (VMware) } 27294370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2730fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 27314370aa4aSLai Jiangshan fmt++; 2732fef20d9cSFrederic Weisbecker break; 2733fef20d9cSFrederic Weisbecker 2734fef20d9cSFrederic Weisbecker default: 2735fef20d9cSFrederic Weisbecker switch (spec.type) { 2736fef20d9cSFrederic Weisbecker 2737fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2738fef20d9cSFrederic Weisbecker save_arg(long long); 2739fef20d9cSFrederic Weisbecker break; 2740fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2741fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2742fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2743fef20d9cSFrederic Weisbecker break; 2744fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2745fef20d9cSFrederic Weisbecker save_arg(size_t); 2746fef20d9cSFrederic Weisbecker break; 2747fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2748fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2749fef20d9cSFrederic Weisbecker break; 2750a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2751a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2752a4e94ef0SZhaolei save_arg(char); 2753a4e94ef0SZhaolei break; 2754fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2755fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2756fef20d9cSFrederic Weisbecker save_arg(short); 2757fef20d9cSFrederic Weisbecker break; 2758fef20d9cSFrederic Weisbecker default: 2759fef20d9cSFrederic Weisbecker save_arg(int); 2760fef20d9cSFrederic Weisbecker } 2761fef20d9cSFrederic Weisbecker } 2762fef20d9cSFrederic Weisbecker } 2763fef20d9cSFrederic Weisbecker 2764b006f19bSRasmus Villemoes out: 27657b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2766fef20d9cSFrederic Weisbecker #undef save_arg 27674370aa4aSLai Jiangshan } 27684370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 27694370aa4aSLai Jiangshan 27704370aa4aSLai Jiangshan /** 27714370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 27724370aa4aSLai Jiangshan * @buf: The buffer to place the result into 27734370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 27744370aa4aSLai Jiangshan * @fmt: The format string to use 27754370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 27764370aa4aSLai Jiangshan * 27774370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 27784370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 27794370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 27804370aa4aSLai Jiangshan * 27814370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 27820efb4d20SSteven Rostedt * see vsnprintf comment for details. 27834370aa4aSLai Jiangshan * 27844370aa4aSLai Jiangshan * The return value is the number of characters which would 27854370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 27864370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 27874370aa4aSLai Jiangshan * number of characters written into @buf as return value 27884370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 27894370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 27904370aa4aSLai Jiangshan * string is truncated. 27914370aa4aSLai Jiangshan */ 27924370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 27934370aa4aSLai Jiangshan { 2794fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 2795d4be151bSAndré Goddard Rosa char *str, *end; 2796d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 27974370aa4aSLai Jiangshan 2798762abb51SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 27994370aa4aSLai Jiangshan return 0; 28004370aa4aSLai Jiangshan 28014370aa4aSLai Jiangshan str = buf; 28024370aa4aSLai Jiangshan end = buf + size; 28034370aa4aSLai Jiangshan 28044370aa4aSLai Jiangshan #define get_arg(type) \ 28054370aa4aSLai Jiangshan ({ \ 28064370aa4aSLai Jiangshan typeof(type) value; \ 28074370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 28084370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 28094370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 28104370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 28114370aa4aSLai Jiangshan } else { \ 28124370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 28134370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 28144370aa4aSLai Jiangshan } \ 28154370aa4aSLai Jiangshan args += sizeof(type); \ 28164370aa4aSLai Jiangshan value; \ 28174370aa4aSLai Jiangshan }) 28184370aa4aSLai Jiangshan 28194370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 28204370aa4aSLai Jiangshan if (end < buf) { 28214370aa4aSLai Jiangshan end = ((void *)-1); 28224370aa4aSLai Jiangshan size = end - buf; 28234370aa4aSLai Jiangshan } 28244370aa4aSLai Jiangshan 2825fef20d9cSFrederic Weisbecker while (*fmt) { 2826fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2827d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2828fef20d9cSFrederic Weisbecker 2829fef20d9cSFrederic Weisbecker fmt += read; 2830fef20d9cSFrederic Weisbecker 2831fef20d9cSFrederic Weisbecker switch (spec.type) { 2832fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2833fef20d9cSFrederic Weisbecker int copy = read; 2834fef20d9cSFrederic Weisbecker if (str < end) { 2835fef20d9cSFrederic Weisbecker if (copy > end - str) 2836fef20d9cSFrederic Weisbecker copy = end - str; 2837fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2838fef20d9cSFrederic Weisbecker } 2839fef20d9cSFrederic Weisbecker str += read; 2840fef20d9cSFrederic Weisbecker break; 28414370aa4aSLai Jiangshan } 28424370aa4aSLai Jiangshan 2843ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 28444d72ba01SRasmus Villemoes set_field_width(&spec, get_arg(int)); 2845fef20d9cSFrederic Weisbecker break; 28464370aa4aSLai Jiangshan 2847fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 28484d72ba01SRasmus Villemoes set_precision(&spec, get_arg(int)); 2849fef20d9cSFrederic Weisbecker break; 28504370aa4aSLai Jiangshan 2851d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2852d4be151bSAndré Goddard Rosa char c; 2853d4be151bSAndré Goddard Rosa 2854fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2855fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 28564370aa4aSLai Jiangshan if (str < end) 28574370aa4aSLai Jiangshan *str = ' '; 28584370aa4aSLai Jiangshan ++str; 28594370aa4aSLai Jiangshan } 28604370aa4aSLai Jiangshan } 28614370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 28624370aa4aSLai Jiangshan if (str < end) 28634370aa4aSLai Jiangshan *str = c; 28644370aa4aSLai Jiangshan ++str; 2865fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 28664370aa4aSLai Jiangshan if (str < end) 28674370aa4aSLai Jiangshan *str = ' '; 28684370aa4aSLai Jiangshan ++str; 28694370aa4aSLai Jiangshan } 2870fef20d9cSFrederic Weisbecker break; 2871d4be151bSAndré Goddard Rosa } 28724370aa4aSLai Jiangshan 2873fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 28744370aa4aSLai Jiangshan const char *str_arg = args; 2875d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 2876fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 2877fef20d9cSFrederic Weisbecker break; 28784370aa4aSLai Jiangshan } 28794370aa4aSLai Jiangshan 2880841a915dSSteven Rostedt (VMware) case FORMAT_TYPE_PTR: { 2881841a915dSSteven Rostedt (VMware) bool process = false; 2882841a915dSSteven Rostedt (VMware) int copy, len; 2883841a915dSSteven Rostedt (VMware) /* Non function dereferences were already done */ 2884841a915dSSteven Rostedt (VMware) switch (*fmt) { 2885841a915dSSteven Rostedt (VMware) case 'S': 2886841a915dSSteven Rostedt (VMware) case 's': 2887841a915dSSteven Rostedt (VMware) case 'F': 2888841a915dSSteven Rostedt (VMware) case 'f': 28891e6338cfSSteven Rostedt (VMware) case 'x': 28901e6338cfSSteven Rostedt (VMware) case 'K': 2891841a915dSSteven Rostedt (VMware) process = true; 2892841a915dSSteven Rostedt (VMware) break; 2893841a915dSSteven Rostedt (VMware) default: 2894841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 2895841a915dSSteven Rostedt (VMware) process = true; 2896841a915dSSteven Rostedt (VMware) break; 2897841a915dSSteven Rostedt (VMware) } 2898841a915dSSteven Rostedt (VMware) /* Pointer dereference was already processed */ 2899841a915dSSteven Rostedt (VMware) if (str < end) { 2900841a915dSSteven Rostedt (VMware) len = copy = strlen(args); 2901841a915dSSteven Rostedt (VMware) if (copy > end - str) 2902841a915dSSteven Rostedt (VMware) copy = end - str; 2903841a915dSSteven Rostedt (VMware) memcpy(str, args, copy); 2904841a915dSSteven Rostedt (VMware) str += len; 290562165600SSteven Rostedt (VMware) args += len + 1; 2906841a915dSSteven Rostedt (VMware) } 2907841a915dSSteven Rostedt (VMware) } 2908841a915dSSteven Rostedt (VMware) if (process) 2909ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 2910841a915dSSteven Rostedt (VMware) 2911fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 29124370aa4aSLai Jiangshan fmt++; 2913fef20d9cSFrederic Weisbecker break; 2914841a915dSSteven Rostedt (VMware) } 29154370aa4aSLai Jiangshan 2916fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 29174370aa4aSLai Jiangshan if (str < end) 29184370aa4aSLai Jiangshan *str = '%'; 29194370aa4aSLai Jiangshan ++str; 2920fef20d9cSFrederic Weisbecker break; 2921fef20d9cSFrederic Weisbecker 2922b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2923b006f19bSRasmus Villemoes goto out; 2924b006f19bSRasmus Villemoes 2925d4be151bSAndré Goddard Rosa default: { 2926d4be151bSAndré Goddard Rosa unsigned long long num; 2927d4be151bSAndré Goddard Rosa 2928fef20d9cSFrederic Weisbecker switch (spec.type) { 2929fef20d9cSFrederic Weisbecker 2930fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 29314370aa4aSLai Jiangshan num = get_arg(long long); 2932fef20d9cSFrederic Weisbecker break; 2933fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2934fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2935fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 2936fef20d9cSFrederic Weisbecker break; 2937fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 29384370aa4aSLai Jiangshan num = get_arg(size_t); 2939fef20d9cSFrederic Weisbecker break; 2940fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 29414370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 2942fef20d9cSFrederic Weisbecker break; 2943a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2944a4e94ef0SZhaolei num = get_arg(unsigned char); 2945a4e94ef0SZhaolei break; 2946a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2947a4e94ef0SZhaolei num = get_arg(signed char); 2948a4e94ef0SZhaolei break; 2949fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2950fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 2951fef20d9cSFrederic Weisbecker break; 2952fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2953fef20d9cSFrederic Weisbecker num = get_arg(short); 2954fef20d9cSFrederic Weisbecker break; 2955fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 29564370aa4aSLai Jiangshan num = get_arg(unsigned int); 2957fef20d9cSFrederic Weisbecker break; 2958fef20d9cSFrederic Weisbecker default: 2959fef20d9cSFrederic Weisbecker num = get_arg(int); 29604370aa4aSLai Jiangshan } 2961fef20d9cSFrederic Weisbecker 2962fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 2963d4be151bSAndré Goddard Rosa } /* default: */ 2964d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 2965d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 2966fef20d9cSFrederic Weisbecker 2967b006f19bSRasmus Villemoes out: 29684370aa4aSLai Jiangshan if (size > 0) { 29694370aa4aSLai Jiangshan if (str < end) 29704370aa4aSLai Jiangshan *str = '\0'; 29714370aa4aSLai Jiangshan else 29724370aa4aSLai Jiangshan end[-1] = '\0'; 29734370aa4aSLai Jiangshan } 2974fef20d9cSFrederic Weisbecker 29754370aa4aSLai Jiangshan #undef get_arg 29764370aa4aSLai Jiangshan 29774370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 29784370aa4aSLai Jiangshan return str - buf; 29794370aa4aSLai Jiangshan } 29804370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 29814370aa4aSLai Jiangshan 29824370aa4aSLai Jiangshan /** 29834370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 29844370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 29854370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 29864370aa4aSLai Jiangshan * @fmt: The format string to use 29874370aa4aSLai Jiangshan * @...: Arguments for the format string 29884370aa4aSLai Jiangshan * 29894370aa4aSLai Jiangshan * The function returns the number of words(u32) written 29904370aa4aSLai Jiangshan * into @bin_buf. 29914370aa4aSLai Jiangshan */ 29924370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 29934370aa4aSLai Jiangshan { 29944370aa4aSLai Jiangshan va_list args; 29954370aa4aSLai Jiangshan int ret; 29964370aa4aSLai Jiangshan 29974370aa4aSLai Jiangshan va_start(args, fmt); 29984370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 29994370aa4aSLai Jiangshan va_end(args); 30007b9186f5SAndré Goddard Rosa 30014370aa4aSLai Jiangshan return ret; 30024370aa4aSLai Jiangshan } 30034370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 30044370aa4aSLai Jiangshan 30054370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 30064370aa4aSLai Jiangshan 30071da177e4SLinus Torvalds /** 30081da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 30091da177e4SLinus Torvalds * @buf: input buffer 30101da177e4SLinus Torvalds * @fmt: format of buffer 30111da177e4SLinus Torvalds * @args: arguments 30121da177e4SLinus Torvalds */ 30131da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 30141da177e4SLinus Torvalds { 30151da177e4SLinus Torvalds const char *str = buf; 30161da177e4SLinus Torvalds char *next; 30171da177e4SLinus Torvalds char digit; 30181da177e4SLinus Torvalds int num = 0; 3019ef0658f3SJoe Perches u8 qualifier; 302053809751SJan Beulich unsigned int base; 302153809751SJan Beulich union { 302253809751SJan Beulich long long s; 302353809751SJan Beulich unsigned long long u; 302453809751SJan Beulich } val; 3025ef0658f3SJoe Perches s16 field_width; 3026d4be151bSAndré Goddard Rosa bool is_sign; 30271da177e4SLinus Torvalds 3028da99075cSJan Beulich while (*fmt) { 30291da177e4SLinus Torvalds /* skip any white space in format */ 30301da177e4SLinus Torvalds /* white space in format matchs any amount of 30311da177e4SLinus Torvalds * white space, including none, in the input. 30321da177e4SLinus Torvalds */ 30331da177e4SLinus Torvalds if (isspace(*fmt)) { 3034e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 3035e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 30361da177e4SLinus Torvalds } 30371da177e4SLinus Torvalds 30381da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 30391da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 30401da177e4SLinus Torvalds if (*fmt++ != *str++) 30411da177e4SLinus Torvalds break; 30421da177e4SLinus Torvalds continue; 30431da177e4SLinus Torvalds } 30441da177e4SLinus Torvalds 30451da177e4SLinus Torvalds if (!*fmt) 30461da177e4SLinus Torvalds break; 30471da177e4SLinus Torvalds ++fmt; 30481da177e4SLinus Torvalds 30491da177e4SLinus Torvalds /* skip this conversion. 30501da177e4SLinus Torvalds * advance both strings to next white space 30511da177e4SLinus Torvalds */ 30521da177e4SLinus Torvalds if (*fmt == '*') { 3053da99075cSJan Beulich if (!*str) 3054da99075cSJan Beulich break; 3055f9310b2fSJessica Yu while (!isspace(*fmt) && *fmt != '%' && *fmt) { 3056f9310b2fSJessica Yu /* '%*[' not yet supported, invalid format */ 3057f9310b2fSJessica Yu if (*fmt == '[') 3058f9310b2fSJessica Yu return num; 30591da177e4SLinus Torvalds fmt++; 3060f9310b2fSJessica Yu } 30611da177e4SLinus Torvalds while (!isspace(*str) && *str) 30621da177e4SLinus Torvalds str++; 30631da177e4SLinus Torvalds continue; 30641da177e4SLinus Torvalds } 30651da177e4SLinus Torvalds 30661da177e4SLinus Torvalds /* get field width */ 30671da177e4SLinus Torvalds field_width = -1; 306853809751SJan Beulich if (isdigit(*fmt)) { 30691da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 307053809751SJan Beulich if (field_width <= 0) 307153809751SJan Beulich break; 307253809751SJan Beulich } 30731da177e4SLinus Torvalds 30741da177e4SLinus Torvalds /* get conversion qualifier */ 30751da177e4SLinus Torvalds qualifier = -1; 307675fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 30775b5e0928SAlexey Dobriyan *fmt == 'z') { 30781da177e4SLinus Torvalds qualifier = *fmt++; 30791da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 30801da177e4SLinus Torvalds if (qualifier == 'h') { 30811da177e4SLinus Torvalds qualifier = 'H'; 30821da177e4SLinus Torvalds fmt++; 30831da177e4SLinus Torvalds } else if (qualifier == 'l') { 30841da177e4SLinus Torvalds qualifier = 'L'; 30851da177e4SLinus Torvalds fmt++; 30861da177e4SLinus Torvalds } 30871da177e4SLinus Torvalds } 30881da177e4SLinus Torvalds } 30891da177e4SLinus Torvalds 3090da99075cSJan Beulich if (!*fmt) 3091da99075cSJan Beulich break; 3092da99075cSJan Beulich 3093da99075cSJan Beulich if (*fmt == 'n') { 3094da99075cSJan Beulich /* return number of characters read so far */ 3095da99075cSJan Beulich *va_arg(args, int *) = str - buf; 3096da99075cSJan Beulich ++fmt; 3097da99075cSJan Beulich continue; 3098da99075cSJan Beulich } 3099da99075cSJan Beulich 3100da99075cSJan Beulich if (!*str) 31011da177e4SLinus Torvalds break; 31021da177e4SLinus Torvalds 3103d4be151bSAndré Goddard Rosa base = 10; 31043f623ebaSFabian Frederick is_sign = false; 3105d4be151bSAndré Goddard Rosa 31061da177e4SLinus Torvalds switch (*fmt++) { 31071da177e4SLinus Torvalds case 'c': 31081da177e4SLinus Torvalds { 31091da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 31101da177e4SLinus Torvalds if (field_width == -1) 31111da177e4SLinus Torvalds field_width = 1; 31121da177e4SLinus Torvalds do { 31131da177e4SLinus Torvalds *s++ = *str++; 31141da177e4SLinus Torvalds } while (--field_width > 0 && *str); 31151da177e4SLinus Torvalds num++; 31161da177e4SLinus Torvalds } 31171da177e4SLinus Torvalds continue; 31181da177e4SLinus Torvalds case 's': 31191da177e4SLinus Torvalds { 31201da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 31211da177e4SLinus Torvalds if (field_width == -1) 31224be929beSAlexey Dobriyan field_width = SHRT_MAX; 31231da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 3124e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 31251da177e4SLinus Torvalds 31261da177e4SLinus Torvalds /* now copy until next white space */ 31277b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 31281da177e4SLinus Torvalds *s++ = *str++; 31291da177e4SLinus Torvalds *s = '\0'; 31301da177e4SLinus Torvalds num++; 31311da177e4SLinus Torvalds } 31321da177e4SLinus Torvalds continue; 3133f9310b2fSJessica Yu /* 3134f9310b2fSJessica Yu * Warning: This implementation of the '[' conversion specifier 3135f9310b2fSJessica Yu * deviates from its glibc counterpart in the following ways: 3136f9310b2fSJessica Yu * (1) It does NOT support ranges i.e. '-' is NOT a special 3137f9310b2fSJessica Yu * character 3138f9310b2fSJessica Yu * (2) It cannot match the closing bracket ']' itself 3139f9310b2fSJessica Yu * (3) A field width is required 3140f9310b2fSJessica Yu * (4) '%*[' (discard matching input) is currently not supported 3141f9310b2fSJessica Yu * 3142f9310b2fSJessica Yu * Example usage: 3143f9310b2fSJessica Yu * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3144f9310b2fSJessica Yu * buf1, buf2, buf3); 3145f9310b2fSJessica Yu * if (ret < 3) 3146f9310b2fSJessica Yu * // etc.. 3147f9310b2fSJessica Yu */ 3148f9310b2fSJessica Yu case '[': 3149f9310b2fSJessica Yu { 3150f9310b2fSJessica Yu char *s = (char *)va_arg(args, char *); 3151f9310b2fSJessica Yu DECLARE_BITMAP(set, 256) = {0}; 3152f9310b2fSJessica Yu unsigned int len = 0; 3153f9310b2fSJessica Yu bool negate = (*fmt == '^'); 3154f9310b2fSJessica Yu 3155f9310b2fSJessica Yu /* field width is required */ 3156f9310b2fSJessica Yu if (field_width == -1) 3157f9310b2fSJessica Yu return num; 3158f9310b2fSJessica Yu 3159f9310b2fSJessica Yu if (negate) 3160f9310b2fSJessica Yu ++fmt; 3161f9310b2fSJessica Yu 3162f9310b2fSJessica Yu for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 3163f9310b2fSJessica Yu set_bit((u8)*fmt, set); 3164f9310b2fSJessica Yu 3165f9310b2fSJessica Yu /* no ']' or no character set found */ 3166f9310b2fSJessica Yu if (!*fmt || !len) 3167f9310b2fSJessica Yu return num; 3168f9310b2fSJessica Yu ++fmt; 3169f9310b2fSJessica Yu 3170f9310b2fSJessica Yu if (negate) { 3171f9310b2fSJessica Yu bitmap_complement(set, set, 256); 3172f9310b2fSJessica Yu /* exclude null '\0' byte */ 3173f9310b2fSJessica Yu clear_bit(0, set); 3174f9310b2fSJessica Yu } 3175f9310b2fSJessica Yu 3176f9310b2fSJessica Yu /* match must be non-empty */ 3177f9310b2fSJessica Yu if (!test_bit((u8)*str, set)) 3178f9310b2fSJessica Yu return num; 3179f9310b2fSJessica Yu 3180f9310b2fSJessica Yu while (test_bit((u8)*str, set) && field_width--) 3181f9310b2fSJessica Yu *s++ = *str++; 3182f9310b2fSJessica Yu *s = '\0'; 3183f9310b2fSJessica Yu ++num; 3184f9310b2fSJessica Yu } 3185f9310b2fSJessica Yu continue; 31861da177e4SLinus Torvalds case 'o': 31871da177e4SLinus Torvalds base = 8; 31881da177e4SLinus Torvalds break; 31891da177e4SLinus Torvalds case 'x': 31901da177e4SLinus Torvalds case 'X': 31911da177e4SLinus Torvalds base = 16; 31921da177e4SLinus Torvalds break; 31931da177e4SLinus Torvalds case 'i': 31941da177e4SLinus Torvalds base = 0; 31957e6bd6f3SAndy Shevchenko /* fall through */ 31961da177e4SLinus Torvalds case 'd': 31973f623ebaSFabian Frederick is_sign = true; 31987e6bd6f3SAndy Shevchenko /* fall through */ 31991da177e4SLinus Torvalds case 'u': 32001da177e4SLinus Torvalds break; 32011da177e4SLinus Torvalds case '%': 32021da177e4SLinus Torvalds /* looking for '%' in str */ 32031da177e4SLinus Torvalds if (*str++ != '%') 32041da177e4SLinus Torvalds return num; 32051da177e4SLinus Torvalds continue; 32061da177e4SLinus Torvalds default: 32071da177e4SLinus Torvalds /* invalid format; stop here */ 32081da177e4SLinus Torvalds return num; 32091da177e4SLinus Torvalds } 32101da177e4SLinus Torvalds 32111da177e4SLinus Torvalds /* have some sort of integer conversion. 32121da177e4SLinus Torvalds * first, skip white space in buffer. 32131da177e4SLinus Torvalds */ 3214e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 32151da177e4SLinus Torvalds 32161da177e4SLinus Torvalds digit = *str; 32171da177e4SLinus Torvalds if (is_sign && digit == '-') 32181da177e4SLinus Torvalds digit = *(str + 1); 32191da177e4SLinus Torvalds 32201da177e4SLinus Torvalds if (!digit 32211da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 32221da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 32231da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 32241da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 32251da177e4SLinus Torvalds break; 32261da177e4SLinus Torvalds 322753809751SJan Beulich if (is_sign) 322853809751SJan Beulich val.s = qualifier != 'L' ? 322953809751SJan Beulich simple_strtol(str, &next, base) : 323053809751SJan Beulich simple_strtoll(str, &next, base); 323153809751SJan Beulich else 323253809751SJan Beulich val.u = qualifier != 'L' ? 323353809751SJan Beulich simple_strtoul(str, &next, base) : 323453809751SJan Beulich simple_strtoull(str, &next, base); 323553809751SJan Beulich 323653809751SJan Beulich if (field_width > 0 && next - str > field_width) { 323753809751SJan Beulich if (base == 0) 323853809751SJan Beulich _parse_integer_fixup_radix(str, &base); 323953809751SJan Beulich while (next - str > field_width) { 324053809751SJan Beulich if (is_sign) 324153809751SJan Beulich val.s = div_s64(val.s, base); 324253809751SJan Beulich else 324353809751SJan Beulich val.u = div_u64(val.u, base); 324453809751SJan Beulich --next; 324553809751SJan Beulich } 324653809751SJan Beulich } 324753809751SJan Beulich 32481da177e4SLinus Torvalds switch (qualifier) { 32491da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 325053809751SJan Beulich if (is_sign) 325153809751SJan Beulich *va_arg(args, signed char *) = val.s; 325253809751SJan Beulich else 325353809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 32541da177e4SLinus Torvalds break; 32551da177e4SLinus Torvalds case 'h': 325653809751SJan Beulich if (is_sign) 325753809751SJan Beulich *va_arg(args, short *) = val.s; 325853809751SJan Beulich else 325953809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 32601da177e4SLinus Torvalds break; 32611da177e4SLinus Torvalds case 'l': 326253809751SJan Beulich if (is_sign) 326353809751SJan Beulich *va_arg(args, long *) = val.s; 326453809751SJan Beulich else 326553809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 32661da177e4SLinus Torvalds break; 32671da177e4SLinus Torvalds case 'L': 326853809751SJan Beulich if (is_sign) 326953809751SJan Beulich *va_arg(args, long long *) = val.s; 327053809751SJan Beulich else 327153809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 32721da177e4SLinus Torvalds break; 32731da177e4SLinus Torvalds case 'z': 327453809751SJan Beulich *va_arg(args, size_t *) = val.u; 32751da177e4SLinus Torvalds break; 32761da177e4SLinus Torvalds default: 327753809751SJan Beulich if (is_sign) 327853809751SJan Beulich *va_arg(args, int *) = val.s; 327953809751SJan Beulich else 328053809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 32811da177e4SLinus Torvalds break; 32821da177e4SLinus Torvalds } 32831da177e4SLinus Torvalds num++; 32841da177e4SLinus Torvalds 32851da177e4SLinus Torvalds if (!next) 32861da177e4SLinus Torvalds break; 32871da177e4SLinus Torvalds str = next; 32881da177e4SLinus Torvalds } 3289c6b40d16SJohannes Berg 32901da177e4SLinus Torvalds return num; 32911da177e4SLinus Torvalds } 32921da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 32931da177e4SLinus Torvalds 32941da177e4SLinus Torvalds /** 32951da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 32961da177e4SLinus Torvalds * @buf: input buffer 32971da177e4SLinus Torvalds * @fmt: formatting of buffer 32981da177e4SLinus Torvalds * @...: resulting arguments 32991da177e4SLinus Torvalds */ 33001da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 33011da177e4SLinus Torvalds { 33021da177e4SLinus Torvalds va_list args; 33031da177e4SLinus Torvalds int i; 33041da177e4SLinus Torvalds 33051da177e4SLinus Torvalds va_start(args, fmt); 33061da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 33071da177e4SLinus Torvalds va_end(args); 33087b9186f5SAndré Goddard Rosa 33091da177e4SLinus Torvalds return i; 33101da177e4SLinus Torvalds } 33111da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 3312