1*457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/lib/vsprintf.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ 91da177e4SLinus Torvalds /* 101da177e4SLinus Torvalds * Wirzenius wrote this portably, Torvalds fucked it up :-) 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds /* 141da177e4SLinus Torvalds * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> 151da177e4SLinus Torvalds * - changed to provide snprintf and vsnprintf functions 161da177e4SLinus Torvalds * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> 171da177e4SLinus Torvalds * - scnprintf and vscnprintf 181da177e4SLinus Torvalds */ 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #include <stdarg.h> 21ef27ac18SRasmus Villemoes #include <linux/build_bug.h> 220d1d7a55SStephen Boyd #include <linux/clk.h> 23900cca29SGeert Uytterhoeven #include <linux/clk-provider.h> 248bc3bcc9SPaul Gortmaker #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ 251da177e4SLinus Torvalds #include <linux/types.h> 261da177e4SLinus Torvalds #include <linux/string.h> 271da177e4SLinus Torvalds #include <linux/ctype.h> 281da177e4SLinus Torvalds #include <linux/kernel.h> 290fe1ef24SLinus Torvalds #include <linux/kallsyms.h> 3053809751SJan Beulich #include <linux/math64.h> 310fe1ef24SLinus Torvalds #include <linux/uaccess.h> 32332d2e78SLinus Torvalds #include <linux/ioport.h> 334b6ccca7SAl Viro #include <linux/dcache.h> 34312b4e22SRyan Mallon #include <linux/cred.h> 354d42c447SAndy Shevchenko #include <linux/rtc.h> 362b1b0d66SAndy Shevchenko #include <linux/uuid.h> 37ce4fecf1SPantelis Antoniou #include <linux/of.h> 388a27f7c9SJoe Perches #include <net/addrconf.h> 39ad67b74dSTobin C. Harding #include <linux/siphash.h> 40ad67b74dSTobin C. Harding #include <linux/compiler.h> 411031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 421031bc58SDmitry Monakhov #include <linux/blkdev.h> 431031bc58SDmitry Monakhov #endif 441da177e4SLinus Torvalds 45edf14cdbSVlastimil Babka #include "../mm/internal.h" /* For the trace_print_flags arrays */ 46edf14cdbSVlastimil Babka 474e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 487c43d9a3SRasmus Villemoes #include <asm/byteorder.h> /* cpu_to_le16 */ 491da177e4SLinus Torvalds 5071dca95dSAndy Shevchenko #include <linux/string_helpers.h> 511dff46d6SAlexey Dobriyan #include "kstrtox.h" 52aa46a63eSHarvey Harrison 531da177e4SLinus Torvalds /** 541da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 551da177e4SLinus Torvalds * @cp: The start of the string 561da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 571da177e4SLinus Torvalds * @base: The number base to use 58462e4711SEldad Zack * 59462e4711SEldad Zack * This function is obsolete. Please use kstrtoull instead. 601da177e4SLinus Torvalds */ 611da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 621da177e4SLinus Torvalds { 631dff46d6SAlexey Dobriyan unsigned long long result; 641dff46d6SAlexey Dobriyan unsigned int rv; 651da177e4SLinus Torvalds 661dff46d6SAlexey Dobriyan cp = _parse_integer_fixup_radix(cp, &base); 671dff46d6SAlexey Dobriyan rv = _parse_integer(cp, base, &result); 681dff46d6SAlexey Dobriyan /* FIXME */ 691dff46d6SAlexey Dobriyan cp += (rv & ~KSTRTOX_OVERFLOW); 70aa46a63eSHarvey Harrison 711da177e4SLinus Torvalds if (endp) 721da177e4SLinus Torvalds *endp = (char *)cp; 737b9186f5SAndré Goddard Rosa 741da177e4SLinus Torvalds return result; 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /** 79922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 80922ac25cSAndré Goddard Rosa * @cp: The start of the string 81922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 82922ac25cSAndré Goddard Rosa * @base: The number base to use 83462e4711SEldad Zack * 84462e4711SEldad Zack * This function is obsolete. Please use kstrtoul instead. 85922ac25cSAndré Goddard Rosa */ 86922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 87922ac25cSAndré Goddard Rosa { 88922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 89922ac25cSAndré Goddard Rosa } 90922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 91922ac25cSAndré Goddard Rosa 92922ac25cSAndré Goddard Rosa /** 93922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 94922ac25cSAndré Goddard Rosa * @cp: The start of the string 95922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 96922ac25cSAndré Goddard Rosa * @base: The number base to use 97462e4711SEldad Zack * 98462e4711SEldad Zack * This function is obsolete. Please use kstrtol instead. 99922ac25cSAndré Goddard Rosa */ 100922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 101922ac25cSAndré Goddard Rosa { 102922ac25cSAndré Goddard Rosa if (*cp == '-') 103922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 104922ac25cSAndré Goddard Rosa 105922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 106922ac25cSAndré Goddard Rosa } 107922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 108922ac25cSAndré Goddard Rosa 109922ac25cSAndré Goddard Rosa /** 1101da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1111da177e4SLinus Torvalds * @cp: The start of the string 1121da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1131da177e4SLinus Torvalds * @base: The number base to use 114462e4711SEldad Zack * 115462e4711SEldad Zack * This function is obsolete. Please use kstrtoll instead. 1161da177e4SLinus Torvalds */ 1171da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds if (*cp == '-') 1201da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1217b9186f5SAndré Goddard Rosa 1221da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1231da177e4SLinus Torvalds } 12498d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1251da177e4SLinus Torvalds 126cf3b429bSJoe Perches static noinline_for_stack 127cf3b429bSJoe Perches int skip_atoi(const char **s) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds int i = 0; 1301da177e4SLinus Torvalds 13143e5b666SRasmus Villemoes do { 1321da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 13343e5b666SRasmus Villemoes } while (isdigit(**s)); 1347b9186f5SAndré Goddard Rosa 1351da177e4SLinus Torvalds return i; 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 1387c43d9a3SRasmus Villemoes /* 1397c43d9a3SRasmus Villemoes * Decimal conversion is by far the most typical, and is used for 1407c43d9a3SRasmus Villemoes * /proc and /sys data. This directly impacts e.g. top performance 1417c43d9a3SRasmus Villemoes * with many processes running. We optimize it for speed by emitting 1427c43d9a3SRasmus Villemoes * two characters at a time, using a 200 byte lookup table. This 1437c43d9a3SRasmus Villemoes * roughly halves the number of multiplications compared to computing 1447c43d9a3SRasmus Villemoes * the digits one at a time. Implementation strongly inspired by the 1457c43d9a3SRasmus Villemoes * previous version, which in turn used ideas described at 1467c43d9a3SRasmus Villemoes * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 1477c43d9a3SRasmus Villemoes * from the author, Douglas W. Jones). 1487c43d9a3SRasmus Villemoes * 1497c43d9a3SRasmus Villemoes * It turns out there is precisely one 26 bit fixed-point 1507c43d9a3SRasmus Villemoes * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 1517c43d9a3SRasmus Villemoes * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 1527c43d9a3SRasmus Villemoes * range happens to be somewhat larger (x <= 1073741898), but that's 1537c43d9a3SRasmus Villemoes * irrelevant for our purpose. 1547c43d9a3SRasmus Villemoes * 1557c43d9a3SRasmus Villemoes * For dividing a number in the range [10^4, 10^6-1] by 100, we still 1567c43d9a3SRasmus Villemoes * need a 32x32->64 bit multiply, so we simply use the same constant. 1577c43d9a3SRasmus Villemoes * 1587c43d9a3SRasmus Villemoes * For dividing a number in the range [100, 10^4-1] by 100, there are 1597c43d9a3SRasmus Villemoes * several options. The simplest is (x * 0x147b) >> 19, which is valid 1607c43d9a3SRasmus Villemoes * for all x <= 43698. 161133fd9f5SDenys Vlasenko */ 1624277eeddSDenis Vlasenko 1637c43d9a3SRasmus Villemoes static const u16 decpair[100] = { 1647c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 1657c43d9a3SRasmus Villemoes _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 1667c43d9a3SRasmus Villemoes _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 1677c43d9a3SRasmus Villemoes _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 1687c43d9a3SRasmus Villemoes _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 1697c43d9a3SRasmus Villemoes _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 1707c43d9a3SRasmus Villemoes _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 1717c43d9a3SRasmus Villemoes _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 1727c43d9a3SRasmus Villemoes _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 1737c43d9a3SRasmus Villemoes _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 1747c43d9a3SRasmus Villemoes _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 1757c43d9a3SRasmus Villemoes #undef _ 1767c43d9a3SRasmus Villemoes }; 1774277eeddSDenis Vlasenko 1787b9186f5SAndré Goddard Rosa /* 1797c43d9a3SRasmus Villemoes * This will print a single '0' even if r == 0, since we would 180675cf53cSRasmus Villemoes * immediately jump to out_r where two 0s would be written but only 181675cf53cSRasmus Villemoes * one of them accounted for in buf. This is needed by ip4_string 182675cf53cSRasmus Villemoes * below. All other callers pass a non-zero value of r. 183133fd9f5SDenys Vlasenko */ 184133fd9f5SDenys Vlasenko static noinline_for_stack 185133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 186133fd9f5SDenys Vlasenko { 187133fd9f5SDenys Vlasenko unsigned q; 1884277eeddSDenis Vlasenko 1897c43d9a3SRasmus Villemoes /* 1 <= r < 10^8 */ 1907c43d9a3SRasmus Villemoes if (r < 100) 1917c43d9a3SRasmus Villemoes goto out_r; 192cb239d0aSGeorge Spelvin 1937c43d9a3SRasmus Villemoes /* 100 <= r < 10^8 */ 1947c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 1957c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 1967c43d9a3SRasmus Villemoes buf += 2; 1977c43d9a3SRasmus Villemoes 1987c43d9a3SRasmus Villemoes /* 1 <= q < 10^6 */ 1997c43d9a3SRasmus Villemoes if (q < 100) 2007c43d9a3SRasmus Villemoes goto out_q; 2017c43d9a3SRasmus Villemoes 2027c43d9a3SRasmus Villemoes /* 100 <= q < 10^6 */ 2037c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2047c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2057c43d9a3SRasmus Villemoes buf += 2; 2067c43d9a3SRasmus Villemoes 2077c43d9a3SRasmus Villemoes /* 1 <= r < 10^4 */ 2087c43d9a3SRasmus Villemoes if (r < 100) 2097c43d9a3SRasmus Villemoes goto out_r; 2107c43d9a3SRasmus Villemoes 2117c43d9a3SRasmus Villemoes /* 100 <= r < 10^4 */ 2127c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2137c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2147c43d9a3SRasmus Villemoes buf += 2; 2157c43d9a3SRasmus Villemoes out_q: 2167c43d9a3SRasmus Villemoes /* 1 <= q < 100 */ 2177c43d9a3SRasmus Villemoes r = q; 2187c43d9a3SRasmus Villemoes out_r: 2197c43d9a3SRasmus Villemoes /* 1 <= r < 100 */ 2207c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r]; 221675cf53cSRasmus Villemoes buf += r < 10 ? 1 : 2; 222133fd9f5SDenys Vlasenko return buf; 223133fd9f5SDenys Vlasenko } 224133fd9f5SDenys Vlasenko 2257c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 2267c43d9a3SRasmus Villemoes static noinline_for_stack 2277c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r) 2287c43d9a3SRasmus Villemoes { 2297c43d9a3SRasmus Villemoes unsigned q; 230133fd9f5SDenys Vlasenko 2317c43d9a3SRasmus Villemoes /* 0 <= r < 10^8 */ 2327c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2337c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2347c43d9a3SRasmus Villemoes buf += 2; 235133fd9f5SDenys Vlasenko 2367c43d9a3SRasmus Villemoes /* 0 <= q < 10^6 */ 2377c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2387c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2397c43d9a3SRasmus Villemoes buf += 2; 240133fd9f5SDenys Vlasenko 2417c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2427c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2437c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2447c43d9a3SRasmus Villemoes buf += 2; 2457c43d9a3SRasmus Villemoes 2467c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2477c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2487c43d9a3SRasmus Villemoes buf += 2; 2497c43d9a3SRasmus Villemoes return buf; 2507c43d9a3SRasmus Villemoes } 2517c43d9a3SRasmus Villemoes 2527c43d9a3SRasmus Villemoes static noinline_for_stack 253133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 254133fd9f5SDenys Vlasenko { 255133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 2567c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2577c43d9a3SRasmus Villemoes /* 1 <= n <= 1.6e11 */ 2587c43d9a3SRasmus Villemoes if (n >= 100*1000*1000) 2597c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2607c43d9a3SRasmus Villemoes /* 1 <= n < 1e8 */ 261133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 262133fd9f5SDenys Vlasenko } 263133fd9f5SDenys Vlasenko 2647c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 265133fd9f5SDenys Vlasenko 2667c43d9a3SRasmus Villemoes static void 2677c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r) 268133fd9f5SDenys Vlasenko { 2697c43d9a3SRasmus Villemoes unsigned q; 2707c43d9a3SRasmus Villemoes 2717c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2727c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2737c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2747c43d9a3SRasmus Villemoes buf += 2; 2757c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2767c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2772359172aSGeorge Spelvin } 2782359172aSGeorge Spelvin 2792359172aSGeorge Spelvin /* 2802359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 2812359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 2822359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 2832359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 2847c43d9a3SRasmus Villemoes * (second call in the put_dec code, assuming n is all-ones). 2852359172aSGeorge Spelvin */ 2867c43d9a3SRasmus Villemoes static noinline_for_stack 2872359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 2882359172aSGeorge Spelvin { 2892359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 2902359172aSGeorge Spelvin 2912359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 2922359172aSGeorge Spelvin return q; 293133fd9f5SDenys Vlasenko } 294133fd9f5SDenys Vlasenko 295133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 296133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 297133fd9f5SDenys Vlasenko * (with permission from the author). 298133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 299133fd9f5SDenys Vlasenko */ 300133fd9f5SDenys Vlasenko static 301133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 302133fd9f5SDenys Vlasenko { 303133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 304133fd9f5SDenys Vlasenko 305133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 306133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 307133fd9f5SDenys Vlasenko 308133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 309133fd9f5SDenys Vlasenko h = (n >> 32); 310133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 311133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 312133fd9f5SDenys Vlasenko 3137c43d9a3SRasmus Villemoes /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 3147c43d9a3SRasmus Villemoes = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 315133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 3162359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 317133fd9f5SDenys Vlasenko 3182359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 3192359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 320133fd9f5SDenys Vlasenko 3212359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3222359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 323133fd9f5SDenys Vlasenko 3242359172aSGeorge Spelvin q += 281 * d3; 3252359172aSGeorge Spelvin buf += 12; 3262359172aSGeorge Spelvin if (q) 3272359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3282359172aSGeorge Spelvin else while (buf[-1] == '0') 329133fd9f5SDenys Vlasenko --buf; 3307b9186f5SAndré Goddard Rosa 3314277eeddSDenis Vlasenko return buf; 3324277eeddSDenis Vlasenko } 333133fd9f5SDenys Vlasenko 334133fd9f5SDenys Vlasenko #endif 3354277eeddSDenis Vlasenko 3361ac101a5SKAMEZAWA Hiroyuki /* 3371ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3381ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3391ac101a5SKAMEZAWA Hiroyuki * 3401ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3411ac101a5SKAMEZAWA Hiroyuki */ 342d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) 3431ac101a5SKAMEZAWA Hiroyuki { 3447c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 3457c43d9a3SRasmus Villemoes char tmp[sizeof(num) * 3] __aligned(2); 3461ac101a5SKAMEZAWA Hiroyuki int idx, len; 3471ac101a5SKAMEZAWA Hiroyuki 348133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 349133fd9f5SDenys Vlasenko if (num <= 9) { 350133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 351133fd9f5SDenys Vlasenko len = 1; 352133fd9f5SDenys Vlasenko } else { 3531ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 354133fd9f5SDenys Vlasenko } 3551ac101a5SKAMEZAWA Hiroyuki 356d1be35cbSAndrei Vagin if (len > size || width > size) 3571ac101a5SKAMEZAWA Hiroyuki return 0; 358d1be35cbSAndrei Vagin 359d1be35cbSAndrei Vagin if (width > len) { 360d1be35cbSAndrei Vagin width = width - len; 361d1be35cbSAndrei Vagin for (idx = 0; idx < width; idx++) 362d1be35cbSAndrei Vagin buf[idx] = ' '; 363d1be35cbSAndrei Vagin } else { 364d1be35cbSAndrei Vagin width = 0; 365d1be35cbSAndrei Vagin } 366d1be35cbSAndrei Vagin 3671ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 368d1be35cbSAndrei Vagin buf[idx + width] = tmp[len - idx - 1]; 369d1be35cbSAndrei Vagin 370d1be35cbSAndrei Vagin return len + width; 3711ac101a5SKAMEZAWA Hiroyuki } 3721ac101a5SKAMEZAWA Hiroyuki 37351be17dfSRasmus Villemoes #define SIGN 1 /* unsigned/signed, must be 1 */ 374d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */ 3751da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3761da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 377d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 378b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 379b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 3801da177e4SLinus Torvalds 381fef20d9cSFrederic Weisbecker enum format_type { 382fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 383ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 384fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 385fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 386fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 387fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 388fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 389fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 390fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 391fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 392fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 393a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 394a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 395fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 396fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 397fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 398fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 399fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 400fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 401fef20d9cSFrederic Weisbecker }; 402fef20d9cSFrederic Weisbecker 403fef20d9cSFrederic Weisbecker struct printf_spec { 404d0484193SRasmus Villemoes unsigned int type:8; /* format_type enum */ 405d0484193SRasmus Villemoes signed int field_width:24; /* width of output field */ 406d0484193SRasmus Villemoes unsigned int flags:8; /* flags to number() */ 407d0484193SRasmus Villemoes unsigned int base:8; /* number base, 8, 10 or 16 only */ 408d0484193SRasmus Villemoes signed int precision:16; /* # of digits/chars */ 409d0484193SRasmus Villemoes } __packed; 410ef27ac18SRasmus Villemoes static_assert(sizeof(struct printf_spec) == 8); 411ef27ac18SRasmus Villemoes 4124d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1) 4134d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1) 414fef20d9cSFrederic Weisbecker 415cf3b429bSJoe Perches static noinline_for_stack 416cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 417fef20d9cSFrederic Weisbecker struct printf_spec spec) 4181da177e4SLinus Torvalds { 4197c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 4207c43d9a3SRasmus Villemoes char tmp[3 * sizeof(num)] __aligned(2); 4219b706aeeSDenys Vlasenko char sign; 4229b706aeeSDenys Vlasenko char locase; 423fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4241da177e4SLinus Torvalds int i; 4257c203422SPierre Carrier bool is_zero = num == 0LL; 4261c7a8e62SRasmus Villemoes int field_width = spec.field_width; 4271c7a8e62SRasmus Villemoes int precision = spec.precision; 4281da177e4SLinus Torvalds 4299b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4309b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 431fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 432fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 433fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4341da177e4SLinus Torvalds sign = 0; 435fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4361da177e4SLinus Torvalds if ((signed long long)num < 0) { 4371da177e4SLinus Torvalds sign = '-'; 4381da177e4SLinus Torvalds num = -(signed long long)num; 4391c7a8e62SRasmus Villemoes field_width--; 440fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4411da177e4SLinus Torvalds sign = '+'; 4421c7a8e62SRasmus Villemoes field_width--; 443fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4441da177e4SLinus Torvalds sign = ' '; 4451c7a8e62SRasmus Villemoes field_width--; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds } 448b39a7340SDenis Vlasenko if (need_pfx) { 449fef20d9cSFrederic Weisbecker if (spec.base == 16) 4501c7a8e62SRasmus Villemoes field_width -= 2; 4517c203422SPierre Carrier else if (!is_zero) 4521c7a8e62SRasmus Villemoes field_width--; 4531da177e4SLinus Torvalds } 454b39a7340SDenis Vlasenko 455b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4561da177e4SLinus Torvalds i = 0; 457133fd9f5SDenys Vlasenko if (num < spec.base) 4583ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase; 459fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 460fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 461b39a7340SDenis Vlasenko int shift = 3; 4627b9186f5SAndré Goddard Rosa 4637b9186f5SAndré Goddard Rosa if (spec.base == 16) 4647b9186f5SAndré Goddard Rosa shift = 4; 465b39a7340SDenis Vlasenko do { 4663ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 467b39a7340SDenis Vlasenko num >>= shift; 468b39a7340SDenis Vlasenko } while (num); 4694277eeddSDenis Vlasenko } else { /* base 10 */ 4704277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4714277eeddSDenis Vlasenko } 472b39a7340SDenis Vlasenko 473b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 4741c7a8e62SRasmus Villemoes if (i > precision) 4751c7a8e62SRasmus Villemoes precision = i; 476b39a7340SDenis Vlasenko /* leading space padding */ 4771c7a8e62SRasmus Villemoes field_width -= precision; 47851be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) { 4791c7a8e62SRasmus Villemoes while (--field_width >= 0) { 480f796937aSJeremy Fitzhardinge if (buf < end) 4811da177e4SLinus Torvalds *buf = ' '; 4821da177e4SLinus Torvalds ++buf; 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds } 485b39a7340SDenis Vlasenko /* sign */ 4861da177e4SLinus Torvalds if (sign) { 487f796937aSJeremy Fitzhardinge if (buf < end) 4881da177e4SLinus Torvalds *buf = sign; 4891da177e4SLinus Torvalds ++buf; 4901da177e4SLinus Torvalds } 491b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 492b39a7340SDenis Vlasenko if (need_pfx) { 4937c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 494f796937aSJeremy Fitzhardinge if (buf < end) 4951da177e4SLinus Torvalds *buf = '0'; 4961da177e4SLinus Torvalds ++buf; 4977c203422SPierre Carrier } 498fef20d9cSFrederic Weisbecker if (spec.base == 16) { 499f796937aSJeremy Fitzhardinge if (buf < end) 5009b706aeeSDenys Vlasenko *buf = ('X' | locase); 5011da177e4SLinus Torvalds ++buf; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds } 504b39a7340SDenis Vlasenko /* zero or space padding */ 505fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 506d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD); 507d1c1b121SRasmus Villemoes BUILD_BUG_ON(' ' + ZEROPAD != '0'); 5081c7a8e62SRasmus Villemoes while (--field_width >= 0) { 509f796937aSJeremy Fitzhardinge if (buf < end) 5101da177e4SLinus Torvalds *buf = c; 5111da177e4SLinus Torvalds ++buf; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds } 514b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 5151c7a8e62SRasmus Villemoes while (i <= --precision) { 516f796937aSJeremy Fitzhardinge if (buf < end) 5171da177e4SLinus Torvalds *buf = '0'; 5181da177e4SLinus Torvalds ++buf; 5191da177e4SLinus Torvalds } 520b39a7340SDenis Vlasenko /* actual digits of result */ 521b39a7340SDenis Vlasenko while (--i >= 0) { 522f796937aSJeremy Fitzhardinge if (buf < end) 5231da177e4SLinus Torvalds *buf = tmp[i]; 5241da177e4SLinus Torvalds ++buf; 5251da177e4SLinus Torvalds } 526b39a7340SDenis Vlasenko /* trailing space padding */ 5271c7a8e62SRasmus Villemoes while (--field_width >= 0) { 528f796937aSJeremy Fitzhardinge if (buf < end) 5291da177e4SLinus Torvalds *buf = ' '; 5301da177e4SLinus Torvalds ++buf; 5311da177e4SLinus Torvalds } 5327b9186f5SAndré Goddard Rosa 5331da177e4SLinus Torvalds return buf; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 5363cab1e71SAndy Shevchenko static noinline_for_stack 5373cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size) 5383cab1e71SAndy Shevchenko { 5393cab1e71SAndy Shevchenko struct printf_spec spec; 5403cab1e71SAndy Shevchenko 5413cab1e71SAndy Shevchenko spec.type = FORMAT_TYPE_PTR; 5423cab1e71SAndy Shevchenko spec.field_width = 2 + 2 * size; /* 0x + hex */ 5433cab1e71SAndy Shevchenko spec.flags = SPECIAL | SMALL | ZEROPAD; 5443cab1e71SAndy Shevchenko spec.base = 16; 5453cab1e71SAndy Shevchenko spec.precision = -1; 5463cab1e71SAndy Shevchenko 5473cab1e71SAndy Shevchenko return number(buf, end, num, spec); 5483cab1e71SAndy Shevchenko } 5493cab1e71SAndy Shevchenko 550cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces) 5514b6ccca7SAl Viro { 5524b6ccca7SAl Viro size_t size; 5534b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5544b6ccca7SAl Viro return; 5554b6ccca7SAl Viro size = end - buf; 5564b6ccca7SAl Viro if (size <= spaces) { 5574b6ccca7SAl Viro memset(buf, ' ', size); 5584b6ccca7SAl Viro return; 5594b6ccca7SAl Viro } 5604b6ccca7SAl Viro if (len) { 5614b6ccca7SAl Viro if (len > size - spaces) 5624b6ccca7SAl Viro len = size - spaces; 5634b6ccca7SAl Viro memmove(buf + spaces, buf, len); 5644b6ccca7SAl Viro } 5654b6ccca7SAl Viro memset(buf, ' ', spaces); 5664b6ccca7SAl Viro } 5674b6ccca7SAl Viro 568cfccde04SRasmus Villemoes /* 569cfccde04SRasmus Villemoes * Handle field width padding for a string. 570cfccde04SRasmus Villemoes * @buf: current buffer position 571cfccde04SRasmus Villemoes * @n: length of string 572cfccde04SRasmus Villemoes * @end: end of output buffer 573cfccde04SRasmus Villemoes * @spec: for field width and flags 574cfccde04SRasmus Villemoes * Returns: new buffer position after padding. 575cfccde04SRasmus Villemoes */ 576cfccde04SRasmus Villemoes static noinline_for_stack 577cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec) 578cfccde04SRasmus Villemoes { 579cfccde04SRasmus Villemoes unsigned spaces; 580cfccde04SRasmus Villemoes 581cfccde04SRasmus Villemoes if (likely(n >= spec.field_width)) 582cfccde04SRasmus Villemoes return buf; 583cfccde04SRasmus Villemoes /* we want to pad the sucker */ 584cfccde04SRasmus Villemoes spaces = spec.field_width - n; 585cfccde04SRasmus Villemoes if (!(spec.flags & LEFT)) { 586cfccde04SRasmus Villemoes move_right(buf - n, end, n, spaces); 587cfccde04SRasmus Villemoes return buf + spaces; 588cfccde04SRasmus Villemoes } 589cfccde04SRasmus Villemoes while (spaces--) { 590cfccde04SRasmus Villemoes if (buf < end) 591cfccde04SRasmus Villemoes *buf = ' '; 592cfccde04SRasmus Villemoes ++buf; 593cfccde04SRasmus Villemoes } 594cfccde04SRasmus Villemoes return buf; 595cfccde04SRasmus Villemoes } 596cfccde04SRasmus Villemoes 597d529ac41SPetr Mladek /* Handle string from a well known address. */ 598d529ac41SPetr Mladek static char *string_nocheck(char *buf, char *end, const char *s, 599d529ac41SPetr Mladek struct printf_spec spec) 60095508cfaSRasmus Villemoes { 60134fc8b90SRasmus Villemoes int len = 0; 60234fc8b90SRasmus Villemoes size_t lim = spec.precision; 60395508cfaSRasmus Villemoes 60434fc8b90SRasmus Villemoes while (lim--) { 60534fc8b90SRasmus Villemoes char c = *s++; 60634fc8b90SRasmus Villemoes if (!c) 60734fc8b90SRasmus Villemoes break; 60895508cfaSRasmus Villemoes if (buf < end) 60934fc8b90SRasmus Villemoes *buf = c; 61095508cfaSRasmus Villemoes ++buf; 61134fc8b90SRasmus Villemoes ++len; 61295508cfaSRasmus Villemoes } 61334fc8b90SRasmus Villemoes return widen_string(buf, len, end, spec); 61495508cfaSRasmus Villemoes } 61595508cfaSRasmus Villemoes 616c8c3b584SPetr Mladek /* Be careful: error messages must fit into the given buffer. */ 617c8c3b584SPetr Mladek static char *error_string(char *buf, char *end, const char *s, 618c8c3b584SPetr Mladek struct printf_spec spec) 619c8c3b584SPetr Mladek { 620c8c3b584SPetr Mladek /* 621c8c3b584SPetr Mladek * Hard limit to avoid a completely insane messages. It actually 622c8c3b584SPetr Mladek * works pretty well because most error messages are in 623c8c3b584SPetr Mladek * the many pointer format modifiers. 624c8c3b584SPetr Mladek */ 625c8c3b584SPetr Mladek if (spec.precision == -1) 626c8c3b584SPetr Mladek spec.precision = 2 * sizeof(void *); 627c8c3b584SPetr Mladek 628c8c3b584SPetr Mladek return string_nocheck(buf, end, s, spec); 629c8c3b584SPetr Mladek } 630c8c3b584SPetr Mladek 6313e5903ebSPetr Mladek /* 6322ac5a3bfSPetr Mladek * Do not call any complex external code here. Nested printk()/vsprintf() 6332ac5a3bfSPetr Mladek * might cause infinite loops. Failures might break printk() and would 6342ac5a3bfSPetr Mladek * be hard to debug. 6353e5903ebSPetr Mladek */ 6363e5903ebSPetr Mladek static const char *check_pointer_msg(const void *ptr) 6373e5903ebSPetr Mladek { 6383e5903ebSPetr Mladek if (!ptr) 6393e5903ebSPetr Mladek return "(null)"; 6403e5903ebSPetr Mladek 6412ac5a3bfSPetr Mladek if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr)) 6423e5903ebSPetr Mladek return "(efault)"; 6433e5903ebSPetr Mladek 6443e5903ebSPetr Mladek return NULL; 6453e5903ebSPetr Mladek } 6463e5903ebSPetr Mladek 6473e5903ebSPetr Mladek static int check_pointer(char **buf, char *end, const void *ptr, 6483e5903ebSPetr Mladek struct printf_spec spec) 6493e5903ebSPetr Mladek { 6503e5903ebSPetr Mladek const char *err_msg; 6513e5903ebSPetr Mladek 6523e5903ebSPetr Mladek err_msg = check_pointer_msg(ptr); 6533e5903ebSPetr Mladek if (err_msg) { 654c8c3b584SPetr Mladek *buf = error_string(*buf, end, err_msg, spec); 6553e5903ebSPetr Mladek return -EFAULT; 6563e5903ebSPetr Mladek } 6573e5903ebSPetr Mladek 6583e5903ebSPetr Mladek return 0; 6593e5903ebSPetr Mladek } 6603e5903ebSPetr Mladek 66195508cfaSRasmus Villemoes static noinline_for_stack 662d529ac41SPetr Mladek char *string(char *buf, char *end, const char *s, 663d529ac41SPetr Mladek struct printf_spec spec) 664d529ac41SPetr Mladek { 6653e5903ebSPetr Mladek if (check_pointer(&buf, end, s, spec)) 6663e5903ebSPetr Mladek return buf; 667d529ac41SPetr Mladek 668d529ac41SPetr Mladek return string_nocheck(buf, end, s, spec); 669d529ac41SPetr Mladek } 670d529ac41SPetr Mladek 671ce9d3eceSYueHaibing static char *pointer_string(char *buf, char *end, 672ce9d3eceSYueHaibing const void *ptr, 6739073dac1SGeert Uytterhoeven struct printf_spec spec) 6749073dac1SGeert Uytterhoeven { 6759073dac1SGeert Uytterhoeven spec.base = 16; 6769073dac1SGeert Uytterhoeven spec.flags |= SMALL; 6779073dac1SGeert Uytterhoeven if (spec.field_width == -1) { 6789073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr); 6799073dac1SGeert Uytterhoeven spec.flags |= ZEROPAD; 6809073dac1SGeert Uytterhoeven } 6819073dac1SGeert Uytterhoeven 6829073dac1SGeert Uytterhoeven return number(buf, end, (unsigned long int)ptr, spec); 6839073dac1SGeert Uytterhoeven } 6849073dac1SGeert Uytterhoeven 6859073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */ 6869073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init; 6879073dac1SGeert Uytterhoeven 6889073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str) 6899073dac1SGeert Uytterhoeven { 6909073dac1SGeert Uytterhoeven debug_boot_weak_hash = 1; 6919073dac1SGeert Uytterhoeven pr_info("debug_boot_weak_hash enabled\n"); 6929073dac1SGeert Uytterhoeven return 0; 6939073dac1SGeert Uytterhoeven } 6949073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); 6959073dac1SGeert Uytterhoeven 6969073dac1SGeert Uytterhoeven static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key); 6979073dac1SGeert Uytterhoeven static siphash_key_t ptr_key __read_mostly; 6989073dac1SGeert Uytterhoeven 6999073dac1SGeert Uytterhoeven static void enable_ptr_key_workfn(struct work_struct *work) 7009073dac1SGeert Uytterhoeven { 7019073dac1SGeert Uytterhoeven get_random_bytes(&ptr_key, sizeof(ptr_key)); 7029073dac1SGeert Uytterhoeven /* Needs to run from preemptible context */ 7039073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 7049073dac1SGeert Uytterhoeven } 7059073dac1SGeert Uytterhoeven 7069073dac1SGeert Uytterhoeven static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn); 7079073dac1SGeert Uytterhoeven 7089073dac1SGeert Uytterhoeven static void fill_random_ptr_key(struct random_ready_callback *unused) 7099073dac1SGeert Uytterhoeven { 7109073dac1SGeert Uytterhoeven /* This may be in an interrupt handler. */ 7119073dac1SGeert Uytterhoeven queue_work(system_unbound_wq, &enable_ptr_key_work); 7129073dac1SGeert Uytterhoeven } 7139073dac1SGeert Uytterhoeven 7149073dac1SGeert Uytterhoeven static struct random_ready_callback random_ready = { 7159073dac1SGeert Uytterhoeven .func = fill_random_ptr_key 7169073dac1SGeert Uytterhoeven }; 7179073dac1SGeert Uytterhoeven 7189073dac1SGeert Uytterhoeven static int __init initialize_ptr_random(void) 7199073dac1SGeert Uytterhoeven { 7209073dac1SGeert Uytterhoeven int key_size = sizeof(ptr_key); 7219073dac1SGeert Uytterhoeven int ret; 7229073dac1SGeert Uytterhoeven 7239073dac1SGeert Uytterhoeven /* Use hw RNG if available. */ 7249073dac1SGeert Uytterhoeven if (get_random_bytes_arch(&ptr_key, key_size) == key_size) { 7259073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 7269073dac1SGeert Uytterhoeven return 0; 7279073dac1SGeert Uytterhoeven } 7289073dac1SGeert Uytterhoeven 7299073dac1SGeert Uytterhoeven ret = add_random_ready_callback(&random_ready); 7309073dac1SGeert Uytterhoeven if (!ret) { 7319073dac1SGeert Uytterhoeven return 0; 7329073dac1SGeert Uytterhoeven } else if (ret == -EALREADY) { 7339073dac1SGeert Uytterhoeven /* This is in preemptible context */ 7349073dac1SGeert Uytterhoeven enable_ptr_key_workfn(&enable_ptr_key_work); 7359073dac1SGeert Uytterhoeven return 0; 7369073dac1SGeert Uytterhoeven } 7379073dac1SGeert Uytterhoeven 7389073dac1SGeert Uytterhoeven return ret; 7399073dac1SGeert Uytterhoeven } 7409073dac1SGeert Uytterhoeven early_initcall(initialize_ptr_random); 7419073dac1SGeert Uytterhoeven 7429073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */ 7439073dac1SGeert Uytterhoeven static char *ptr_to_id(char *buf, char *end, const void *ptr, 7449073dac1SGeert Uytterhoeven struct printf_spec spec) 7459073dac1SGeert Uytterhoeven { 7469073dac1SGeert Uytterhoeven const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; 7479073dac1SGeert Uytterhoeven unsigned long hashval; 7489073dac1SGeert Uytterhoeven 7499073dac1SGeert Uytterhoeven /* When debugging early boot use non-cryptographically secure hash. */ 7509073dac1SGeert Uytterhoeven if (unlikely(debug_boot_weak_hash)) { 7519073dac1SGeert Uytterhoeven hashval = hash_long((unsigned long)ptr, 32); 7529073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec); 7539073dac1SGeert Uytterhoeven } 7549073dac1SGeert Uytterhoeven 7559073dac1SGeert Uytterhoeven if (static_branch_unlikely(¬_filled_random_ptr_key)) { 7569073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr); 7579073dac1SGeert Uytterhoeven /* string length must be less than default_width */ 758c8c3b584SPetr Mladek return error_string(buf, end, str, spec); 7599073dac1SGeert Uytterhoeven } 7609073dac1SGeert Uytterhoeven 7619073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT 7629073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); 7639073dac1SGeert Uytterhoeven /* 7649073dac1SGeert Uytterhoeven * Mask off the first 32 bits, this makes explicit that we have 7659073dac1SGeert Uytterhoeven * modified the address (and 32 bits is plenty for a unique ID). 7669073dac1SGeert Uytterhoeven */ 7679073dac1SGeert Uytterhoeven hashval = hashval & 0xffffffff; 7689073dac1SGeert Uytterhoeven #else 7699073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); 7709073dac1SGeert Uytterhoeven #endif 7719073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec); 7729073dac1SGeert Uytterhoeven } 7739073dac1SGeert Uytterhoeven 7746eea242fSPetr Mladek int kptr_restrict __read_mostly; 7756eea242fSPetr Mladek 7766eea242fSPetr Mladek static noinline_for_stack 7776eea242fSPetr Mladek char *restricted_pointer(char *buf, char *end, const void *ptr, 7786eea242fSPetr Mladek struct printf_spec spec) 7796eea242fSPetr Mladek { 7806eea242fSPetr Mladek switch (kptr_restrict) { 7816eea242fSPetr Mladek case 0: 7821ac2f978SPetr Mladek /* Handle as %p, hash and do _not_ leak addresses. */ 7831ac2f978SPetr Mladek return ptr_to_id(buf, end, ptr, spec); 7846eea242fSPetr Mladek case 1: { 7856eea242fSPetr Mladek const struct cred *cred; 7866eea242fSPetr Mladek 7876eea242fSPetr Mladek /* 7886eea242fSPetr Mladek * kptr_restrict==1 cannot be used in IRQ context 7896eea242fSPetr Mladek * because its test for CAP_SYSLOG would be meaningless. 7906eea242fSPetr Mladek */ 7916eea242fSPetr Mladek if (in_irq() || in_serving_softirq() || in_nmi()) { 7926eea242fSPetr Mladek if (spec.field_width == -1) 7936eea242fSPetr Mladek spec.field_width = 2 * sizeof(ptr); 794c8c3b584SPetr Mladek return error_string(buf, end, "pK-error", spec); 7956eea242fSPetr Mladek } 7966eea242fSPetr Mladek 7976eea242fSPetr Mladek /* 7986eea242fSPetr Mladek * Only print the real pointer value if the current 7996eea242fSPetr Mladek * process has CAP_SYSLOG and is running with the 8006eea242fSPetr Mladek * same credentials it started with. This is because 8016eea242fSPetr Mladek * access to files is checked at open() time, but %pK 8026eea242fSPetr Mladek * checks permission at read() time. We don't want to 8036eea242fSPetr Mladek * leak pointer values if a binary opens a file using 8046eea242fSPetr Mladek * %pK and then elevates privileges before reading it. 8056eea242fSPetr Mladek */ 8066eea242fSPetr Mladek cred = current_cred(); 8076eea242fSPetr Mladek if (!has_capability_noaudit(current, CAP_SYSLOG) || 8086eea242fSPetr Mladek !uid_eq(cred->euid, cred->uid) || 8096eea242fSPetr Mladek !gid_eq(cred->egid, cred->gid)) 8106eea242fSPetr Mladek ptr = NULL; 8116eea242fSPetr Mladek break; 8126eea242fSPetr Mladek } 8136eea242fSPetr Mladek case 2: 8146eea242fSPetr Mladek default: 8156eea242fSPetr Mladek /* Always print 0's for %pK */ 8166eea242fSPetr Mladek ptr = NULL; 8176eea242fSPetr Mladek break; 8186eea242fSPetr Mladek } 8196eea242fSPetr Mladek 8206eea242fSPetr Mladek return pointer_string(buf, end, ptr, spec); 8216eea242fSPetr Mladek } 8226eea242fSPetr Mladek 8239073dac1SGeert Uytterhoeven static noinline_for_stack 8244b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 8254b6ccca7SAl Viro const char *fmt) 8264b6ccca7SAl Viro { 8274b6ccca7SAl Viro const char *array[4], *s; 8284b6ccca7SAl Viro const struct dentry *p; 8294b6ccca7SAl Viro int depth; 8304b6ccca7SAl Viro int i, n; 8314b6ccca7SAl Viro 8324b6ccca7SAl Viro switch (fmt[1]) { 8334b6ccca7SAl Viro case '2': case '3': case '4': 8344b6ccca7SAl Viro depth = fmt[1] - '0'; 8354b6ccca7SAl Viro break; 8364b6ccca7SAl Viro default: 8374b6ccca7SAl Viro depth = 1; 8384b6ccca7SAl Viro } 8394b6ccca7SAl Viro 8404b6ccca7SAl Viro rcu_read_lock(); 8414b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 8423e5903ebSPetr Mladek if (check_pointer(&buf, end, d, spec)) { 8433e5903ebSPetr Mladek rcu_read_unlock(); 8443e5903ebSPetr Mladek return buf; 8453e5903ebSPetr Mladek } 8463e5903ebSPetr Mladek 8476aa7de05SMark Rutland p = READ_ONCE(d->d_parent); 8486aa7de05SMark Rutland array[i] = READ_ONCE(d->d_name.name); 8494b6ccca7SAl Viro if (p == d) { 8504b6ccca7SAl Viro if (i) 8514b6ccca7SAl Viro array[i] = ""; 8524b6ccca7SAl Viro i++; 8534b6ccca7SAl Viro break; 8544b6ccca7SAl Viro } 8554b6ccca7SAl Viro } 8564b6ccca7SAl Viro s = array[--i]; 8574b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 8584b6ccca7SAl Viro char c = *s++; 8594b6ccca7SAl Viro if (!c) { 8604b6ccca7SAl Viro if (!i) 8614b6ccca7SAl Viro break; 8624b6ccca7SAl Viro c = '/'; 8634b6ccca7SAl Viro s = array[--i]; 8644b6ccca7SAl Viro } 8654b6ccca7SAl Viro if (buf < end) 8664b6ccca7SAl Viro *buf = c; 8674b6ccca7SAl Viro } 8684b6ccca7SAl Viro rcu_read_unlock(); 869cfccde04SRasmus Villemoes return widen_string(buf, n, end, spec); 8704b6ccca7SAl Viro } 8714b6ccca7SAl Viro 8721031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 8731031bc58SDmitry Monakhov static noinline_for_stack 8741031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev, 8751031bc58SDmitry Monakhov struct printf_spec spec, const char *fmt) 8761031bc58SDmitry Monakhov { 8773e5903ebSPetr Mladek struct gendisk *hd; 8781031bc58SDmitry Monakhov 8793e5903ebSPetr Mladek if (check_pointer(&buf, end, bdev, spec)) 8803e5903ebSPetr Mladek return buf; 8813e5903ebSPetr Mladek 8823e5903ebSPetr Mladek hd = bdev->bd_disk; 8831031bc58SDmitry Monakhov buf = string(buf, end, hd->disk_name, spec); 8841031bc58SDmitry Monakhov if (bdev->bd_part->partno) { 8851031bc58SDmitry Monakhov if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { 8861031bc58SDmitry Monakhov if (buf < end) 8871031bc58SDmitry Monakhov *buf = 'p'; 8881031bc58SDmitry Monakhov buf++; 8891031bc58SDmitry Monakhov } 8901031bc58SDmitry Monakhov buf = number(buf, end, bdev->bd_part->partno, spec); 8911031bc58SDmitry Monakhov } 8921031bc58SDmitry Monakhov return buf; 8931031bc58SDmitry Monakhov } 8941031bc58SDmitry Monakhov #endif 8951031bc58SDmitry Monakhov 896cf3b429bSJoe Perches static noinline_for_stack 897cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 898b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 8990fe1ef24SLinus Torvalds { 900b0d33c2bSJoe Perches unsigned long value; 9010fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 9020fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 903b0d33c2bSJoe Perches #endif 904b0d33c2bSJoe Perches 905b0d33c2bSJoe Perches if (fmt[1] == 'R') 906b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 907b0d33c2bSJoe Perches value = (unsigned long)ptr; 908b0d33c2bSJoe Perches 909b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 910b0d33c2bSJoe Perches if (*fmt == 'B') 9110f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 912b0d33c2bSJoe Perches else if (*fmt != 'f' && *fmt != 's') 9130fe1ef24SLinus Torvalds sprint_symbol(sym, value); 9140c8b946eSFrederic Weisbecker else 9154796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 9167b9186f5SAndré Goddard Rosa 917d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 9180fe1ef24SLinus Torvalds #else 9193cab1e71SAndy Shevchenko return special_hex_number(buf, end, value, sizeof(void *)); 9200fe1ef24SLinus Torvalds #endif 9210fe1ef24SLinus Torvalds } 9220fe1ef24SLinus Torvalds 923abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = { 924abd4fe62SAndy Shevchenko .field_width = -1, 925abd4fe62SAndy Shevchenko .precision = -1, 926abd4fe62SAndy Shevchenko }; 927abd4fe62SAndy Shevchenko 92854433973SAndy Shevchenko static const struct printf_spec default_flag_spec = { 92954433973SAndy Shevchenko .base = 16, 93054433973SAndy Shevchenko .precision = -1, 93154433973SAndy Shevchenko .flags = SPECIAL | SMALL, 93254433973SAndy Shevchenko }; 93354433973SAndy Shevchenko 934ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = { 935ce0b4910SAndy Shevchenko .base = 10, 936ce0b4910SAndy Shevchenko .precision = -1, 937ce0b4910SAndy Shevchenko }; 938ce0b4910SAndy Shevchenko 9394d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = { 9404d42c447SAndy Shevchenko .base = 10, 9414d42c447SAndy Shevchenko .field_width = 2, 9424d42c447SAndy Shevchenko .precision = -1, 9434d42c447SAndy Shevchenko .flags = ZEROPAD, 9444d42c447SAndy Shevchenko }; 9454d42c447SAndy Shevchenko 9464d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = { 9474d42c447SAndy Shevchenko .base = 10, 9484d42c447SAndy Shevchenko .field_width = 4, 9494d42c447SAndy Shevchenko .precision = -1, 9504d42c447SAndy Shevchenko .flags = ZEROPAD, 9514d42c447SAndy Shevchenko }; 9524d42c447SAndy Shevchenko 953cf3b429bSJoe Perches static noinline_for_stack 954cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 955fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 956332d2e78SLinus Torvalds { 957332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 95828405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 959332d2e78SLinus Torvalds #endif 960332d2e78SLinus Torvalds 961332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 96228405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 963332d2e78SLinus Torvalds #endif 9644da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 965fef20d9cSFrederic Weisbecker .base = 16, 9664da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 967fef20d9cSFrederic Weisbecker .precision = -1, 968fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 969fef20d9cSFrederic Weisbecker }; 9704da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 9714da0b66cSBjorn Helgaas .base = 16, 9724da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 9734da0b66cSBjorn Helgaas .precision = -1, 9744da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 9754da0b66cSBjorn Helgaas }; 9760f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 9770f4050c7SBjorn Helgaas .base = 16, 9780f4050c7SBjorn Helgaas .field_width = 2, 9790f4050c7SBjorn Helgaas .precision = -1, 9800f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 9810f4050c7SBjorn Helgaas }; 9824da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 983fd95541eSBjorn Helgaas .field_width = -1, 984fd95541eSBjorn Helgaas .precision = 10, 985fd95541eSBjorn Helgaas .flags = LEFT, 986fd95541eSBjorn Helgaas }; 987c7dabef8SBjorn Helgaas 988c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 989c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 990c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 991c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 9929d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 993c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 994c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 995c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 996c7dabef8SBjorn Helgaas 997332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 998c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 9994da0b66cSBjorn Helgaas const struct printf_spec *specp; 1000332d2e78SLinus Torvalds 10013e5903ebSPetr Mladek if (check_pointer(&buf, end, res, spec)) 10023e5903ebSPetr Mladek return buf; 10033e5903ebSPetr Mladek 1004332d2e78SLinus Torvalds *p++ = '['; 10054da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 1006d529ac41SPetr Mladek p = string_nocheck(p, pend, "io ", str_spec); 10074da0b66cSBjorn Helgaas specp = &io_spec; 10084da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 1009d529ac41SPetr Mladek p = string_nocheck(p, pend, "mem ", str_spec); 10104da0b66cSBjorn Helgaas specp = &mem_spec; 10114da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 1012d529ac41SPetr Mladek p = string_nocheck(p, pend, "irq ", str_spec); 1013ce0b4910SAndy Shevchenko specp = &default_dec_spec; 10144da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 1015d529ac41SPetr Mladek p = string_nocheck(p, pend, "dma ", str_spec); 1016ce0b4910SAndy Shevchenko specp = &default_dec_spec; 10170f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 1018d529ac41SPetr Mladek p = string_nocheck(p, pend, "bus ", str_spec); 10190f4050c7SBjorn Helgaas specp = &bus_spec; 10204da0b66cSBjorn Helgaas } else { 1021d529ac41SPetr Mladek p = string_nocheck(p, pend, "??? ", str_spec); 10224da0b66cSBjorn Helgaas specp = &mem_spec; 1023c7dabef8SBjorn Helgaas decode = 0; 1024fd95541eSBjorn Helgaas } 1025d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 1026d529ac41SPetr Mladek p = string_nocheck(p, pend, "size ", str_spec); 1027d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 1028d19cb803SBjorn Helgaas } else { 10294da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 1030c91d3376SBjorn Helgaas if (res->start != res->end) { 1031332d2e78SLinus Torvalds *p++ = '-'; 10324da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 1033c91d3376SBjorn Helgaas } 1034d19cb803SBjorn Helgaas } 1035c7dabef8SBjorn Helgaas if (decode) { 1036fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 1037d529ac41SPetr Mladek p = string_nocheck(p, pend, " 64bit", str_spec); 1038fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 1039d529ac41SPetr Mladek p = string_nocheck(p, pend, " pref", str_spec); 10409d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 1041d529ac41SPetr Mladek p = string_nocheck(p, pend, " window", str_spec); 1042fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 1043d529ac41SPetr Mladek p = string_nocheck(p, pend, " disabled", str_spec); 1044c7dabef8SBjorn Helgaas } else { 1045d529ac41SPetr Mladek p = string_nocheck(p, pend, " flags ", str_spec); 104654433973SAndy Shevchenko p = number(p, pend, res->flags, default_flag_spec); 1047fd95541eSBjorn Helgaas } 1048332d2e78SLinus Torvalds *p++ = ']'; 1049c7dabef8SBjorn Helgaas *p = '\0'; 1050332d2e78SLinus Torvalds 1051d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 1052332d2e78SLinus Torvalds } 1053332d2e78SLinus Torvalds 1054cf3b429bSJoe Perches static noinline_for_stack 105531550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 105631550a16SAndy Shevchenko const char *fmt) 105731550a16SAndy Shevchenko { 1058360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 105931550a16SAndy Shevchenko negative value, fallback to the default */ 106031550a16SAndy Shevchenko char separator; 106131550a16SAndy Shevchenko 106231550a16SAndy Shevchenko if (spec.field_width == 0) 106331550a16SAndy Shevchenko /* nothing to print */ 106431550a16SAndy Shevchenko return buf; 106531550a16SAndy Shevchenko 10663e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 10673e5903ebSPetr Mladek return buf; 106831550a16SAndy Shevchenko 106931550a16SAndy Shevchenko switch (fmt[1]) { 107031550a16SAndy Shevchenko case 'C': 107131550a16SAndy Shevchenko separator = ':'; 107231550a16SAndy Shevchenko break; 107331550a16SAndy Shevchenko case 'D': 107431550a16SAndy Shevchenko separator = '-'; 107531550a16SAndy Shevchenko break; 107631550a16SAndy Shevchenko case 'N': 107731550a16SAndy Shevchenko separator = 0; 107831550a16SAndy Shevchenko break; 107931550a16SAndy Shevchenko default: 108031550a16SAndy Shevchenko separator = ' '; 108131550a16SAndy Shevchenko break; 108231550a16SAndy Shevchenko } 108331550a16SAndy Shevchenko 108431550a16SAndy Shevchenko if (spec.field_width > 0) 108531550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 108631550a16SAndy Shevchenko 10879c98f235SRasmus Villemoes for (i = 0; i < len; ++i) { 10889c98f235SRasmus Villemoes if (buf < end) 10899c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]); 10909c98f235SRasmus Villemoes ++buf; 10919c98f235SRasmus Villemoes if (buf < end) 10929c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]); 10939c98f235SRasmus Villemoes ++buf; 109431550a16SAndy Shevchenko 10959c98f235SRasmus Villemoes if (separator && i != len - 1) { 10969c98f235SRasmus Villemoes if (buf < end) 10979c98f235SRasmus Villemoes *buf = separator; 10989c98f235SRasmus Villemoes ++buf; 10999c98f235SRasmus Villemoes } 110031550a16SAndy Shevchenko } 110131550a16SAndy Shevchenko 110231550a16SAndy Shevchenko return buf; 110331550a16SAndy Shevchenko } 110431550a16SAndy Shevchenko 110531550a16SAndy Shevchenko static noinline_for_stack 1106dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap, 1107dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1108dbc760bcSTejun Heo { 1109dbc760bcSTejun Heo const int CHUNKSZ = 32; 1110dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1111dbc760bcSTejun Heo int i, chunksz; 1112dbc760bcSTejun Heo bool first = true; 1113dbc760bcSTejun Heo 11143e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec)) 11153e5903ebSPetr Mladek return buf; 11163e5903ebSPetr Mladek 1117dbc760bcSTejun Heo /* reused to print numbers */ 1118dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 1119dbc760bcSTejun Heo 1120dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1); 1121dbc760bcSTejun Heo if (chunksz == 0) 1122dbc760bcSTejun Heo chunksz = CHUNKSZ; 1123dbc760bcSTejun Heo 1124dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 1125dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) { 1126dbc760bcSTejun Heo u32 chunkmask, val; 1127dbc760bcSTejun Heo int word, bit; 1128dbc760bcSTejun Heo 1129dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1); 1130dbc760bcSTejun Heo word = i / BITS_PER_LONG; 1131dbc760bcSTejun Heo bit = i % BITS_PER_LONG; 1132dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask; 1133dbc760bcSTejun Heo 1134dbc760bcSTejun Heo if (!first) { 1135dbc760bcSTejun Heo if (buf < end) 1136dbc760bcSTejun Heo *buf = ','; 1137dbc760bcSTejun Heo buf++; 1138dbc760bcSTejun Heo } 1139dbc760bcSTejun Heo first = false; 1140dbc760bcSTejun Heo 1141dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4); 1142dbc760bcSTejun Heo buf = number(buf, end, val, spec); 1143dbc760bcSTejun Heo 1144dbc760bcSTejun Heo chunksz = CHUNKSZ; 1145dbc760bcSTejun Heo } 1146dbc760bcSTejun Heo return buf; 1147dbc760bcSTejun Heo } 1148dbc760bcSTejun Heo 1149dbc760bcSTejun Heo static noinline_for_stack 1150dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, 1151dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1152dbc760bcSTejun Heo { 1153dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1154dbc760bcSTejun Heo /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 1155dbc760bcSTejun Heo int cur, rbot, rtop; 1156dbc760bcSTejun Heo bool first = true; 1157dbc760bcSTejun Heo 11583e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec)) 11593e5903ebSPetr Mladek return buf; 11603e5903ebSPetr Mladek 1161dbc760bcSTejun Heo rbot = cur = find_first_bit(bitmap, nr_bits); 1162dbc760bcSTejun Heo while (cur < nr_bits) { 1163dbc760bcSTejun Heo rtop = cur; 1164dbc760bcSTejun Heo cur = find_next_bit(bitmap, nr_bits, cur + 1); 1165dbc760bcSTejun Heo if (cur < nr_bits && cur <= rtop + 1) 1166dbc760bcSTejun Heo continue; 1167dbc760bcSTejun Heo 1168dbc760bcSTejun Heo if (!first) { 1169dbc760bcSTejun Heo if (buf < end) 1170dbc760bcSTejun Heo *buf = ','; 1171dbc760bcSTejun Heo buf++; 1172dbc760bcSTejun Heo } 1173dbc760bcSTejun Heo first = false; 1174dbc760bcSTejun Heo 1175ce0b4910SAndy Shevchenko buf = number(buf, end, rbot, default_dec_spec); 1176dbc760bcSTejun Heo if (rbot < rtop) { 1177dbc760bcSTejun Heo if (buf < end) 1178dbc760bcSTejun Heo *buf = '-'; 1179dbc760bcSTejun Heo buf++; 1180dbc760bcSTejun Heo 1181ce0b4910SAndy Shevchenko buf = number(buf, end, rtop, default_dec_spec); 1182dbc760bcSTejun Heo } 1183dbc760bcSTejun Heo 1184dbc760bcSTejun Heo rbot = cur; 1185dbc760bcSTejun Heo } 1186dbc760bcSTejun Heo return buf; 1187dbc760bcSTejun Heo } 1188dbc760bcSTejun Heo 1189dbc760bcSTejun Heo static noinline_for_stack 1190cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 11918a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 1192dd45c9cfSHarvey Harrison { 11938a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 1194dd45c9cfSHarvey Harrison char *p = mac_addr; 1195dd45c9cfSHarvey Harrison int i; 1196bc7259a2SJoe Perches char separator; 119776597ff9SAndrei Emeltchenko bool reversed = false; 1198bc7259a2SJoe Perches 11993e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 12003e5903ebSPetr Mladek return buf; 12013e5903ebSPetr Mladek 120276597ff9SAndrei Emeltchenko switch (fmt[1]) { 120376597ff9SAndrei Emeltchenko case 'F': 1204bc7259a2SJoe Perches separator = '-'; 120576597ff9SAndrei Emeltchenko break; 120676597ff9SAndrei Emeltchenko 120776597ff9SAndrei Emeltchenko case 'R': 120876597ff9SAndrei Emeltchenko reversed = true; 120976597ff9SAndrei Emeltchenko /* fall through */ 121076597ff9SAndrei Emeltchenko 121176597ff9SAndrei Emeltchenko default: 1212bc7259a2SJoe Perches separator = ':'; 121376597ff9SAndrei Emeltchenko break; 1214bc7259a2SJoe Perches } 1215dd45c9cfSHarvey Harrison 1216dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 121776597ff9SAndrei Emeltchenko if (reversed) 121876597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 121976597ff9SAndrei Emeltchenko else 122055036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 122176597ff9SAndrei Emeltchenko 12228a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 1223bc7259a2SJoe Perches *p++ = separator; 1224dd45c9cfSHarvey Harrison } 1225dd45c9cfSHarvey Harrison *p = '\0'; 1226dd45c9cfSHarvey Harrison 1227d529ac41SPetr Mladek return string_nocheck(buf, end, mac_addr, spec); 1228dd45c9cfSHarvey Harrison } 1229dd45c9cfSHarvey Harrison 1230cf3b429bSJoe Perches static noinline_for_stack 1231cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 1232689afa7dSHarvey Harrison { 1233689afa7dSHarvey Harrison int i; 12340159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 12350159f24eSJoe Perches int index; 12360159f24eSJoe Perches int step; 1237689afa7dSHarvey Harrison 12380159f24eSJoe Perches switch (fmt[2]) { 12390159f24eSJoe Perches case 'h': 12400159f24eSJoe Perches #ifdef __BIG_ENDIAN 12410159f24eSJoe Perches index = 0; 12420159f24eSJoe Perches step = 1; 12430159f24eSJoe Perches #else 12440159f24eSJoe Perches index = 3; 12450159f24eSJoe Perches step = -1; 12460159f24eSJoe Perches #endif 12470159f24eSJoe Perches break; 12480159f24eSJoe Perches case 'l': 12490159f24eSJoe Perches index = 3; 12500159f24eSJoe Perches step = -1; 12510159f24eSJoe Perches break; 12520159f24eSJoe Perches case 'n': 12530159f24eSJoe Perches case 'b': 12540159f24eSJoe Perches default: 12550159f24eSJoe Perches index = 0; 12560159f24eSJoe Perches step = 1; 12570159f24eSJoe Perches break; 12580159f24eSJoe Perches } 12598a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 12607c43d9a3SRasmus Villemoes char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 1261133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 12628a27f7c9SJoe Perches if (leading_zeros) { 12638a27f7c9SJoe Perches if (digits < 3) 12648a27f7c9SJoe Perches *p++ = '0'; 12658a27f7c9SJoe Perches if (digits < 2) 12668a27f7c9SJoe Perches *p++ = '0'; 12678a27f7c9SJoe Perches } 12688a27f7c9SJoe Perches /* reverse the digits in the quad */ 12698a27f7c9SJoe Perches while (digits--) 12708a27f7c9SJoe Perches *p++ = temp[digits]; 12718a27f7c9SJoe Perches if (i < 3) 12728a27f7c9SJoe Perches *p++ = '.'; 12730159f24eSJoe Perches index += step; 12748a27f7c9SJoe Perches } 12758a27f7c9SJoe Perches *p = '\0'; 12767b9186f5SAndré Goddard Rosa 12778a27f7c9SJoe Perches return p; 12788a27f7c9SJoe Perches } 12798a27f7c9SJoe Perches 1280cf3b429bSJoe Perches static noinline_for_stack 1281cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 12828a27f7c9SJoe Perches { 12837b9186f5SAndré Goddard Rosa int i, j, range; 12848a27f7c9SJoe Perches unsigned char zerolength[8]; 12858a27f7c9SJoe Perches int longest = 1; 12868a27f7c9SJoe Perches int colonpos = -1; 12878a27f7c9SJoe Perches u16 word; 12887b9186f5SAndré Goddard Rosa u8 hi, lo; 12898a27f7c9SJoe Perches bool needcolon = false; 1290eb78cd26SJoe Perches bool useIPv4; 1291eb78cd26SJoe Perches struct in6_addr in6; 1292eb78cd26SJoe Perches 1293eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 1294eb78cd26SJoe Perches 1295eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 12968a27f7c9SJoe Perches 12978a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 12988a27f7c9SJoe Perches 12998a27f7c9SJoe Perches if (useIPv4) 13008a27f7c9SJoe Perches range = 6; 13018a27f7c9SJoe Perches else 13028a27f7c9SJoe Perches range = 8; 13038a27f7c9SJoe Perches 13048a27f7c9SJoe Perches /* find position of longest 0 run */ 13058a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13068a27f7c9SJoe Perches for (j = i; j < range; j++) { 1307eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 13088a27f7c9SJoe Perches break; 13098a27f7c9SJoe Perches zerolength[i]++; 13108a27f7c9SJoe Perches } 13118a27f7c9SJoe Perches } 13128a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13138a27f7c9SJoe Perches if (zerolength[i] > longest) { 13148a27f7c9SJoe Perches longest = zerolength[i]; 13158a27f7c9SJoe Perches colonpos = i; 13168a27f7c9SJoe Perches } 13178a27f7c9SJoe Perches } 131829cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 131929cf519eSJoe Perches colonpos = -1; 13208a27f7c9SJoe Perches 13218a27f7c9SJoe Perches /* emit address */ 13228a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13238a27f7c9SJoe Perches if (i == colonpos) { 13248a27f7c9SJoe Perches if (needcolon || i == 0) 13258a27f7c9SJoe Perches *p++ = ':'; 13268a27f7c9SJoe Perches *p++ = ':'; 13278a27f7c9SJoe Perches needcolon = false; 13288a27f7c9SJoe Perches i += longest - 1; 13298a27f7c9SJoe Perches continue; 13308a27f7c9SJoe Perches } 13318a27f7c9SJoe Perches if (needcolon) { 13328a27f7c9SJoe Perches *p++ = ':'; 13338a27f7c9SJoe Perches needcolon = false; 13348a27f7c9SJoe Perches } 13358a27f7c9SJoe Perches /* hex u16 without leading 0s */ 1336eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 13378a27f7c9SJoe Perches hi = word >> 8; 13388a27f7c9SJoe Perches lo = word & 0xff; 13398a27f7c9SJoe Perches if (hi) { 13408a27f7c9SJoe Perches if (hi > 0x0f) 134155036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 13428a27f7c9SJoe Perches else 13438a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 134455036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 13458a27f7c9SJoe Perches } 1346b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 134755036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 13488a27f7c9SJoe Perches else 13498a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 13508a27f7c9SJoe Perches needcolon = true; 13518a27f7c9SJoe Perches } 13528a27f7c9SJoe Perches 13538a27f7c9SJoe Perches if (useIPv4) { 13548a27f7c9SJoe Perches if (needcolon) 13558a27f7c9SJoe Perches *p++ = ':'; 13560159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 13578a27f7c9SJoe Perches } 13588a27f7c9SJoe Perches *p = '\0'; 13597b9186f5SAndré Goddard Rosa 13608a27f7c9SJoe Perches return p; 13618a27f7c9SJoe Perches } 13628a27f7c9SJoe Perches 1363cf3b429bSJoe Perches static noinline_for_stack 1364cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 13658a27f7c9SJoe Perches { 13668a27f7c9SJoe Perches int i; 13677b9186f5SAndré Goddard Rosa 1368689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 136955036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 137055036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 13718a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 1372689afa7dSHarvey Harrison *p++ = ':'; 1373689afa7dSHarvey Harrison } 1374689afa7dSHarvey Harrison *p = '\0'; 13757b9186f5SAndré Goddard Rosa 13768a27f7c9SJoe Perches return p; 13778a27f7c9SJoe Perches } 13788a27f7c9SJoe Perches 1379cf3b429bSJoe Perches static noinline_for_stack 1380cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 13818a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 13828a27f7c9SJoe Perches { 13838a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 13848a27f7c9SJoe Perches 13858a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 1386eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 13878a27f7c9SJoe Perches else 1388eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 1389689afa7dSHarvey Harrison 1390d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 1391689afa7dSHarvey Harrison } 1392689afa7dSHarvey Harrison 1393cf3b429bSJoe Perches static noinline_for_stack 1394cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 13958a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 13964aa99606SHarvey Harrison { 13978a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 13984aa99606SHarvey Harrison 13990159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 14004aa99606SHarvey Harrison 1401d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 14024aa99606SHarvey Harrison } 14034aa99606SHarvey Harrison 1404cf3b429bSJoe Perches static noinline_for_stack 140510679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 140610679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 140710679643SDaniel Borkmann { 140810679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 140910679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 141010679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 141110679643SDaniel Borkmann sizeof("%1234567890")]; 141210679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 141310679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 141410679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 141510679643SDaniel Borkmann u8 off = 0; 141610679643SDaniel Borkmann 141710679643SDaniel Borkmann fmt++; 141810679643SDaniel Borkmann while (isalpha(*++fmt)) { 141910679643SDaniel Borkmann switch (*fmt) { 142010679643SDaniel Borkmann case 'p': 142110679643SDaniel Borkmann have_p = true; 142210679643SDaniel Borkmann break; 142310679643SDaniel Borkmann case 'f': 142410679643SDaniel Borkmann have_f = true; 142510679643SDaniel Borkmann break; 142610679643SDaniel Borkmann case 's': 142710679643SDaniel Borkmann have_s = true; 142810679643SDaniel Borkmann break; 142910679643SDaniel Borkmann case 'c': 143010679643SDaniel Borkmann have_c = true; 143110679643SDaniel Borkmann break; 143210679643SDaniel Borkmann } 143310679643SDaniel Borkmann } 143410679643SDaniel Borkmann 143510679643SDaniel Borkmann if (have_p || have_s || have_f) { 143610679643SDaniel Borkmann *p = '['; 143710679643SDaniel Borkmann off = 1; 143810679643SDaniel Borkmann } 143910679643SDaniel Borkmann 144010679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 144110679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 144210679643SDaniel Borkmann else 144310679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 144410679643SDaniel Borkmann 144510679643SDaniel Borkmann if (have_p || have_s || have_f) 144610679643SDaniel Borkmann *p++ = ']'; 144710679643SDaniel Borkmann 144810679643SDaniel Borkmann if (have_p) { 144910679643SDaniel Borkmann *p++ = ':'; 145010679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 145110679643SDaniel Borkmann } 145210679643SDaniel Borkmann if (have_f) { 145310679643SDaniel Borkmann *p++ = '/'; 145410679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 145510679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 145610679643SDaniel Borkmann } 145710679643SDaniel Borkmann if (have_s) { 145810679643SDaniel Borkmann *p++ = '%'; 145910679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 146010679643SDaniel Borkmann } 146110679643SDaniel Borkmann *p = '\0'; 146210679643SDaniel Borkmann 1463d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 146410679643SDaniel Borkmann } 146510679643SDaniel Borkmann 146610679643SDaniel Borkmann static noinline_for_stack 146710679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 146810679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 146910679643SDaniel Borkmann { 147010679643SDaniel Borkmann bool have_p = false; 147110679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 147210679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 147310679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 147410679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 147510679643SDaniel Borkmann 147610679643SDaniel Borkmann fmt++; 147710679643SDaniel Borkmann while (isalpha(*++fmt)) { 147810679643SDaniel Borkmann switch (*fmt) { 147910679643SDaniel Borkmann case 'p': 148010679643SDaniel Borkmann have_p = true; 148110679643SDaniel Borkmann break; 148210679643SDaniel Borkmann case 'h': 148310679643SDaniel Borkmann case 'l': 148410679643SDaniel Borkmann case 'n': 148510679643SDaniel Borkmann case 'b': 148610679643SDaniel Borkmann fmt4[2] = *fmt; 148710679643SDaniel Borkmann break; 148810679643SDaniel Borkmann } 148910679643SDaniel Borkmann } 149010679643SDaniel Borkmann 149110679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 149210679643SDaniel Borkmann if (have_p) { 149310679643SDaniel Borkmann *p++ = ':'; 149410679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 149510679643SDaniel Borkmann } 149610679643SDaniel Borkmann *p = '\0'; 149710679643SDaniel Borkmann 1498d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 149910679643SDaniel Borkmann } 150010679643SDaniel Borkmann 150110679643SDaniel Borkmann static noinline_for_stack 1502f00cc102SPetr Mladek char *ip_addr_string(char *buf, char *end, const void *ptr, 1503f00cc102SPetr Mladek struct printf_spec spec, const char *fmt) 1504f00cc102SPetr Mladek { 15050b74d4d7SPetr Mladek char *err_fmt_msg; 15060b74d4d7SPetr Mladek 15073e5903ebSPetr Mladek if (check_pointer(&buf, end, ptr, spec)) 15083e5903ebSPetr Mladek return buf; 15093e5903ebSPetr Mladek 1510f00cc102SPetr Mladek switch (fmt[1]) { 1511f00cc102SPetr Mladek case '6': 1512f00cc102SPetr Mladek return ip6_addr_string(buf, end, ptr, spec, fmt); 1513f00cc102SPetr Mladek case '4': 1514f00cc102SPetr Mladek return ip4_addr_string(buf, end, ptr, spec, fmt); 1515f00cc102SPetr Mladek case 'S': { 1516f00cc102SPetr Mladek const union { 1517f00cc102SPetr Mladek struct sockaddr raw; 1518f00cc102SPetr Mladek struct sockaddr_in v4; 1519f00cc102SPetr Mladek struct sockaddr_in6 v6; 1520f00cc102SPetr Mladek } *sa = ptr; 1521f00cc102SPetr Mladek 1522f00cc102SPetr Mladek switch (sa->raw.sa_family) { 1523f00cc102SPetr Mladek case AF_INET: 1524f00cc102SPetr Mladek return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 1525f00cc102SPetr Mladek case AF_INET6: 1526f00cc102SPetr Mladek return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 1527f00cc102SPetr Mladek default: 1528c8c3b584SPetr Mladek return error_string(buf, end, "(einval)", spec); 1529f00cc102SPetr Mladek }} 1530f00cc102SPetr Mladek } 1531f00cc102SPetr Mladek 15320b74d4d7SPetr Mladek err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)"; 1533c8c3b584SPetr Mladek return error_string(buf, end, err_fmt_msg, spec); 1534f00cc102SPetr Mladek } 1535f00cc102SPetr Mladek 1536f00cc102SPetr Mladek static noinline_for_stack 153771dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 153871dca95dSAndy Shevchenko const char *fmt) 153971dca95dSAndy Shevchenko { 154071dca95dSAndy Shevchenko bool found = true; 154171dca95dSAndy Shevchenko int count = 1; 154271dca95dSAndy Shevchenko unsigned int flags = 0; 154371dca95dSAndy Shevchenko int len; 154471dca95dSAndy Shevchenko 154571dca95dSAndy Shevchenko if (spec.field_width == 0) 154671dca95dSAndy Shevchenko return buf; /* nothing to print */ 154771dca95dSAndy Shevchenko 15483e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 15493e5903ebSPetr Mladek return buf; 155071dca95dSAndy Shevchenko 155171dca95dSAndy Shevchenko do { 155271dca95dSAndy Shevchenko switch (fmt[count++]) { 155371dca95dSAndy Shevchenko case 'a': 155471dca95dSAndy Shevchenko flags |= ESCAPE_ANY; 155571dca95dSAndy Shevchenko break; 155671dca95dSAndy Shevchenko case 'c': 155771dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL; 155871dca95dSAndy Shevchenko break; 155971dca95dSAndy Shevchenko case 'h': 156071dca95dSAndy Shevchenko flags |= ESCAPE_HEX; 156171dca95dSAndy Shevchenko break; 156271dca95dSAndy Shevchenko case 'n': 156371dca95dSAndy Shevchenko flags |= ESCAPE_NULL; 156471dca95dSAndy Shevchenko break; 156571dca95dSAndy Shevchenko case 'o': 156671dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL; 156771dca95dSAndy Shevchenko break; 156871dca95dSAndy Shevchenko case 'p': 156971dca95dSAndy Shevchenko flags |= ESCAPE_NP; 157071dca95dSAndy Shevchenko break; 157171dca95dSAndy Shevchenko case 's': 157271dca95dSAndy Shevchenko flags |= ESCAPE_SPACE; 157371dca95dSAndy Shevchenko break; 157471dca95dSAndy Shevchenko default: 157571dca95dSAndy Shevchenko found = false; 157671dca95dSAndy Shevchenko break; 157771dca95dSAndy Shevchenko } 157871dca95dSAndy Shevchenko } while (found); 157971dca95dSAndy Shevchenko 158071dca95dSAndy Shevchenko if (!flags) 158171dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP; 158271dca95dSAndy Shevchenko 158371dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width; 158471dca95dSAndy Shevchenko 158541416f23SRasmus Villemoes /* 158641416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to 158741416f23SRasmus Villemoes * the given buffer, and returns the total size of the output 158841416f23SRasmus Villemoes * had the buffer been big enough. 158941416f23SRasmus Villemoes */ 159041416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 159171dca95dSAndy Shevchenko 159271dca95dSAndy Shevchenko return buf; 159371dca95dSAndy Shevchenko } 159471dca95dSAndy Shevchenko 15953e5903ebSPetr Mladek static char *va_format(char *buf, char *end, struct va_format *va_fmt, 15963e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 159745c3e93dSPetr Mladek { 159845c3e93dSPetr Mladek va_list va; 159945c3e93dSPetr Mladek 16003e5903ebSPetr Mladek if (check_pointer(&buf, end, va_fmt, spec)) 16013e5903ebSPetr Mladek return buf; 16023e5903ebSPetr Mladek 160345c3e93dSPetr Mladek va_copy(va, *va_fmt->va); 160445c3e93dSPetr Mladek buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va); 160545c3e93dSPetr Mladek va_end(va); 160645c3e93dSPetr Mladek 160745c3e93dSPetr Mladek return buf; 160845c3e93dSPetr Mladek } 160945c3e93dSPetr Mladek 161071dca95dSAndy Shevchenko static noinline_for_stack 1611cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 16129ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 16139ac6e44eSJoe Perches { 16142b1b0d66SAndy Shevchenko char uuid[UUID_STRING_LEN + 1]; 16159ac6e44eSJoe Perches char *p = uuid; 16169ac6e44eSJoe Perches int i; 1617f9727a17SChristoph Hellwig const u8 *index = uuid_index; 16189ac6e44eSJoe Perches bool uc = false; 16199ac6e44eSJoe Perches 16203e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 16213e5903ebSPetr Mladek return buf; 16223e5903ebSPetr Mladek 16239ac6e44eSJoe Perches switch (*(++fmt)) { 16249ac6e44eSJoe Perches case 'L': 16259ac6e44eSJoe Perches uc = true; /* fall-through */ 16269ac6e44eSJoe Perches case 'l': 1627f9727a17SChristoph Hellwig index = guid_index; 16289ac6e44eSJoe Perches break; 16299ac6e44eSJoe Perches case 'B': 16309ac6e44eSJoe Perches uc = true; 16319ac6e44eSJoe Perches break; 16329ac6e44eSJoe Perches } 16339ac6e44eSJoe Perches 16349ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 1635aa4ea1c3SAndy Shevchenko if (uc) 1636aa4ea1c3SAndy Shevchenko p = hex_byte_pack_upper(p, addr[index[i]]); 1637aa4ea1c3SAndy Shevchenko else 163855036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 16399ac6e44eSJoe Perches switch (i) { 16409ac6e44eSJoe Perches case 3: 16419ac6e44eSJoe Perches case 5: 16429ac6e44eSJoe Perches case 7: 16439ac6e44eSJoe Perches case 9: 16449ac6e44eSJoe Perches *p++ = '-'; 16459ac6e44eSJoe Perches break; 16469ac6e44eSJoe Perches } 16479ac6e44eSJoe Perches } 16489ac6e44eSJoe Perches 16499ac6e44eSJoe Perches *p = 0; 16509ac6e44eSJoe Perches 1651d529ac41SPetr Mladek return string_nocheck(buf, end, uuid, spec); 16529ac6e44eSJoe Perches } 16539ac6e44eSJoe Perches 16545b17aecfSAndy Shevchenko static noinline_for_stack 1655431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr, 1656431bca24SGeert Uytterhoeven struct printf_spec spec, const char *fmt) 1657c8f44affSMichał Mirosław { 16585b17aecfSAndy Shevchenko unsigned long long num; 16595b17aecfSAndy Shevchenko int size; 16605b17aecfSAndy Shevchenko 16613e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 16623e5903ebSPetr Mladek return buf; 16633e5903ebSPetr Mladek 16645b17aecfSAndy Shevchenko switch (fmt[1]) { 16655b17aecfSAndy Shevchenko case 'F': 16665b17aecfSAndy Shevchenko num = *(const netdev_features_t *)addr; 16675b17aecfSAndy Shevchenko size = sizeof(netdev_features_t); 16685b17aecfSAndy Shevchenko break; 16695b17aecfSAndy Shevchenko default: 1670c8c3b584SPetr Mladek return error_string(buf, end, "(%pN?)", spec); 16715b17aecfSAndy Shevchenko } 1672c8f44affSMichał Mirosław 16733cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1674c8f44affSMichał Mirosław } 1675c8f44affSMichał Mirosław 1676aaf07621SJoe Perches static noinline_for_stack 16773e5903ebSPetr Mladek char *address_val(char *buf, char *end, const void *addr, 16783e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 1679aaf07621SJoe Perches { 1680aaf07621SJoe Perches unsigned long long num; 16813cab1e71SAndy Shevchenko int size; 1682aaf07621SJoe Perches 16833e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 16843e5903ebSPetr Mladek return buf; 16853e5903ebSPetr Mladek 1686aaf07621SJoe Perches switch (fmt[1]) { 1687aaf07621SJoe Perches case 'd': 1688aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 16893cab1e71SAndy Shevchenko size = sizeof(dma_addr_t); 1690aaf07621SJoe Perches break; 1691aaf07621SJoe Perches case 'p': 1692aaf07621SJoe Perches default: 1693aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 16943cab1e71SAndy Shevchenko size = sizeof(phys_addr_t); 1695aaf07621SJoe Perches break; 1696aaf07621SJoe Perches } 1697aaf07621SJoe Perches 16983cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1699aaf07621SJoe Perches } 1700aaf07621SJoe Perches 1701900cca29SGeert Uytterhoeven static noinline_for_stack 17024d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) 17034d42c447SAndy Shevchenko { 17044d42c447SAndy Shevchenko int year = tm->tm_year + (r ? 0 : 1900); 17054d42c447SAndy Shevchenko int mon = tm->tm_mon + (r ? 0 : 1); 17064d42c447SAndy Shevchenko 17074d42c447SAndy Shevchenko buf = number(buf, end, year, default_dec04_spec); 17084d42c447SAndy Shevchenko if (buf < end) 17094d42c447SAndy Shevchenko *buf = '-'; 17104d42c447SAndy Shevchenko buf++; 17114d42c447SAndy Shevchenko 17124d42c447SAndy Shevchenko buf = number(buf, end, mon, default_dec02_spec); 17134d42c447SAndy Shevchenko if (buf < end) 17144d42c447SAndy Shevchenko *buf = '-'; 17154d42c447SAndy Shevchenko buf++; 17164d42c447SAndy Shevchenko 17174d42c447SAndy Shevchenko return number(buf, end, tm->tm_mday, default_dec02_spec); 17184d42c447SAndy Shevchenko } 17194d42c447SAndy Shevchenko 17204d42c447SAndy Shevchenko static noinline_for_stack 17214d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) 17224d42c447SAndy Shevchenko { 17234d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_hour, default_dec02_spec); 17244d42c447SAndy Shevchenko if (buf < end) 17254d42c447SAndy Shevchenko *buf = ':'; 17264d42c447SAndy Shevchenko buf++; 17274d42c447SAndy Shevchenko 17284d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_min, default_dec02_spec); 17294d42c447SAndy Shevchenko if (buf < end) 17304d42c447SAndy Shevchenko *buf = ':'; 17314d42c447SAndy Shevchenko buf++; 17324d42c447SAndy Shevchenko 17334d42c447SAndy Shevchenko return number(buf, end, tm->tm_sec, default_dec02_spec); 17344d42c447SAndy Shevchenko } 17354d42c447SAndy Shevchenko 17364d42c447SAndy Shevchenko static noinline_for_stack 17373e5903ebSPetr Mladek char *rtc_str(char *buf, char *end, const struct rtc_time *tm, 17383e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 17394d42c447SAndy Shevchenko { 17404d42c447SAndy Shevchenko bool have_t = true, have_d = true; 17414d42c447SAndy Shevchenko bool raw = false; 17424d42c447SAndy Shevchenko int count = 2; 17434d42c447SAndy Shevchenko 17443e5903ebSPetr Mladek if (check_pointer(&buf, end, tm, spec)) 17453e5903ebSPetr Mladek return buf; 17463e5903ebSPetr Mladek 17474d42c447SAndy Shevchenko switch (fmt[count]) { 17484d42c447SAndy Shevchenko case 'd': 17494d42c447SAndy Shevchenko have_t = false; 17504d42c447SAndy Shevchenko count++; 17514d42c447SAndy Shevchenko break; 17524d42c447SAndy Shevchenko case 't': 17534d42c447SAndy Shevchenko have_d = false; 17544d42c447SAndy Shevchenko count++; 17554d42c447SAndy Shevchenko break; 17564d42c447SAndy Shevchenko } 17574d42c447SAndy Shevchenko 17584d42c447SAndy Shevchenko raw = fmt[count] == 'r'; 17594d42c447SAndy Shevchenko 17604d42c447SAndy Shevchenko if (have_d) 17614d42c447SAndy Shevchenko buf = date_str(buf, end, tm, raw); 17624d42c447SAndy Shevchenko if (have_d && have_t) { 17634d42c447SAndy Shevchenko /* Respect ISO 8601 */ 17644d42c447SAndy Shevchenko if (buf < end) 17654d42c447SAndy Shevchenko *buf = 'T'; 17664d42c447SAndy Shevchenko buf++; 17674d42c447SAndy Shevchenko } 17684d42c447SAndy Shevchenko if (have_t) 17694d42c447SAndy Shevchenko buf = time_str(buf, end, tm, raw); 17704d42c447SAndy Shevchenko 17714d42c447SAndy Shevchenko return buf; 17724d42c447SAndy Shevchenko } 17734d42c447SAndy Shevchenko 17744d42c447SAndy Shevchenko static noinline_for_stack 17754d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, 17764d42c447SAndy Shevchenko const char *fmt) 17774d42c447SAndy Shevchenko { 17784d42c447SAndy Shevchenko switch (fmt[1]) { 17794d42c447SAndy Shevchenko case 'R': 17803e5903ebSPetr Mladek return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); 17814d42c447SAndy Shevchenko default: 1782c8c3b584SPetr Mladek return error_string(buf, end, "(%ptR?)", spec); 17834d42c447SAndy Shevchenko } 17844d42c447SAndy Shevchenko } 17854d42c447SAndy Shevchenko 17864d42c447SAndy Shevchenko static noinline_for_stack 1787900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1788900cca29SGeert Uytterhoeven const char *fmt) 1789900cca29SGeert Uytterhoeven { 17900b74d4d7SPetr Mladek if (!IS_ENABLED(CONFIG_HAVE_CLK)) 1791c8c3b584SPetr Mladek return error_string(buf, end, "(%pC?)", spec); 17920b74d4d7SPetr Mladek 17933e5903ebSPetr Mladek if (check_pointer(&buf, end, clk, spec)) 17943e5903ebSPetr Mladek return buf; 1795900cca29SGeert Uytterhoeven 1796900cca29SGeert Uytterhoeven switch (fmt[1]) { 1797900cca29SGeert Uytterhoeven case 'n': 1798900cca29SGeert Uytterhoeven default: 1799900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1800900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1801900cca29SGeert Uytterhoeven #else 1802c8c3b584SPetr Mladek return error_string(buf, end, "(%pC?)", spec); 1803900cca29SGeert Uytterhoeven #endif 1804900cca29SGeert Uytterhoeven } 1805900cca29SGeert Uytterhoeven } 1806900cca29SGeert Uytterhoeven 1807edf14cdbSVlastimil Babka static 1808edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags, 1809edf14cdbSVlastimil Babka const struct trace_print_flags *names) 1810edf14cdbSVlastimil Babka { 1811edf14cdbSVlastimil Babka unsigned long mask; 1812edf14cdbSVlastimil Babka 1813edf14cdbSVlastimil Babka for ( ; flags && names->name; names++) { 1814edf14cdbSVlastimil Babka mask = names->mask; 1815edf14cdbSVlastimil Babka if ((flags & mask) != mask) 1816edf14cdbSVlastimil Babka continue; 1817edf14cdbSVlastimil Babka 1818abd4fe62SAndy Shevchenko buf = string(buf, end, names->name, default_str_spec); 1819edf14cdbSVlastimil Babka 1820edf14cdbSVlastimil Babka flags &= ~mask; 1821edf14cdbSVlastimil Babka if (flags) { 1822edf14cdbSVlastimil Babka if (buf < end) 1823edf14cdbSVlastimil Babka *buf = '|'; 1824edf14cdbSVlastimil Babka buf++; 1825edf14cdbSVlastimil Babka } 1826edf14cdbSVlastimil Babka } 1827edf14cdbSVlastimil Babka 1828edf14cdbSVlastimil Babka if (flags) 182954433973SAndy Shevchenko buf = number(buf, end, flags, default_flag_spec); 1830edf14cdbSVlastimil Babka 1831edf14cdbSVlastimil Babka return buf; 1832edf14cdbSVlastimil Babka } 1833edf14cdbSVlastimil Babka 1834edf14cdbSVlastimil Babka static noinline_for_stack 18350b74d4d7SPetr Mladek char *flags_string(char *buf, char *end, void *flags_ptr, 18360b74d4d7SPetr Mladek struct printf_spec spec, const char *fmt) 1837edf14cdbSVlastimil Babka { 1838edf14cdbSVlastimil Babka unsigned long flags; 1839edf14cdbSVlastimil Babka const struct trace_print_flags *names; 1840edf14cdbSVlastimil Babka 18413e5903ebSPetr Mladek if (check_pointer(&buf, end, flags_ptr, spec)) 18423e5903ebSPetr Mladek return buf; 18433e5903ebSPetr Mladek 1844edf14cdbSVlastimil Babka switch (fmt[1]) { 1845edf14cdbSVlastimil Babka case 'p': 1846edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1847edf14cdbSVlastimil Babka /* Remove zone id */ 1848edf14cdbSVlastimil Babka flags &= (1UL << NR_PAGEFLAGS) - 1; 1849edf14cdbSVlastimil Babka names = pageflag_names; 1850edf14cdbSVlastimil Babka break; 1851edf14cdbSVlastimil Babka case 'v': 1852edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1853edf14cdbSVlastimil Babka names = vmaflag_names; 1854edf14cdbSVlastimil Babka break; 1855edf14cdbSVlastimil Babka case 'g': 1856edf14cdbSVlastimil Babka flags = *(gfp_t *)flags_ptr; 1857edf14cdbSVlastimil Babka names = gfpflag_names; 1858edf14cdbSVlastimil Babka break; 1859edf14cdbSVlastimil Babka default: 1860c8c3b584SPetr Mladek return error_string(buf, end, "(%pG?)", spec); 1861edf14cdbSVlastimil Babka } 1862edf14cdbSVlastimil Babka 1863edf14cdbSVlastimil Babka return format_flags(buf, end, flags, names); 1864edf14cdbSVlastimil Babka } 1865edf14cdbSVlastimil Babka 1866ce4fecf1SPantelis Antoniou static const char *device_node_name_for_depth(const struct device_node *np, int depth) 1867ce4fecf1SPantelis Antoniou { 1868ce4fecf1SPantelis Antoniou for ( ; np && depth; depth--) 1869ce4fecf1SPantelis Antoniou np = np->parent; 1870ce4fecf1SPantelis Antoniou 1871ce4fecf1SPantelis Antoniou return kbasename(np->full_name); 1872ce4fecf1SPantelis Antoniou } 1873ce4fecf1SPantelis Antoniou 1874ce4fecf1SPantelis Antoniou static noinline_for_stack 1875ce4fecf1SPantelis Antoniou char *device_node_gen_full_name(const struct device_node *np, char *buf, char *end) 1876ce4fecf1SPantelis Antoniou { 1877ce4fecf1SPantelis Antoniou int depth; 1878ce4fecf1SPantelis Antoniou const struct device_node *parent = np->parent; 1879ce4fecf1SPantelis Antoniou 1880ce4fecf1SPantelis Antoniou /* special case for root node */ 1881ce4fecf1SPantelis Antoniou if (!parent) 1882d529ac41SPetr Mladek return string_nocheck(buf, end, "/", default_str_spec); 1883ce4fecf1SPantelis Antoniou 1884ce4fecf1SPantelis Antoniou for (depth = 0; parent->parent; depth++) 1885ce4fecf1SPantelis Antoniou parent = parent->parent; 1886ce4fecf1SPantelis Antoniou 1887ce4fecf1SPantelis Antoniou for ( ; depth >= 0; depth--) { 1888d529ac41SPetr Mladek buf = string_nocheck(buf, end, "/", default_str_spec); 1889ce4fecf1SPantelis Antoniou buf = string(buf, end, device_node_name_for_depth(np, depth), 1890abd4fe62SAndy Shevchenko default_str_spec); 1891ce4fecf1SPantelis Antoniou } 1892ce4fecf1SPantelis Antoniou return buf; 1893ce4fecf1SPantelis Antoniou } 1894ce4fecf1SPantelis Antoniou 1895ce4fecf1SPantelis Antoniou static noinline_for_stack 1896ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn, 1897ce4fecf1SPantelis Antoniou struct printf_spec spec, const char *fmt) 1898ce4fecf1SPantelis Antoniou { 1899ce4fecf1SPantelis Antoniou char tbuf[sizeof("xxxx") + 1]; 1900ce4fecf1SPantelis Antoniou const char *p; 1901ce4fecf1SPantelis Antoniou int ret; 1902ce4fecf1SPantelis Antoniou char *buf_start = buf; 1903ce4fecf1SPantelis Antoniou struct property *prop; 1904ce4fecf1SPantelis Antoniou bool has_mult, pass; 1905ce4fecf1SPantelis Antoniou static const struct printf_spec num_spec = { 1906ce4fecf1SPantelis Antoniou .flags = SMALL, 1907ce4fecf1SPantelis Antoniou .field_width = -1, 1908ce4fecf1SPantelis Antoniou .precision = -1, 1909ce4fecf1SPantelis Antoniou .base = 10, 1910ce4fecf1SPantelis Antoniou }; 1911ce4fecf1SPantelis Antoniou 1912ce4fecf1SPantelis Antoniou struct printf_spec str_spec = spec; 1913ce4fecf1SPantelis Antoniou str_spec.field_width = -1; 1914ce4fecf1SPantelis Antoniou 1915ce4fecf1SPantelis Antoniou if (!IS_ENABLED(CONFIG_OF)) 1916c8c3b584SPetr Mladek return error_string(buf, end, "(%pOF?)", spec); 1917ce4fecf1SPantelis Antoniou 19183e5903ebSPetr Mladek if (check_pointer(&buf, end, dn, spec)) 19193e5903ebSPetr Mladek return buf; 1920ce4fecf1SPantelis Antoniou 1921ce4fecf1SPantelis Antoniou /* simple case without anything any more format specifiers */ 1922ce4fecf1SPantelis Antoniou fmt++; 1923ce4fecf1SPantelis Antoniou if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 1924ce4fecf1SPantelis Antoniou fmt = "f"; 1925ce4fecf1SPantelis Antoniou 1926ce4fecf1SPantelis Antoniou for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 19276d0a70a2SRob Herring int precision; 1928ce4fecf1SPantelis Antoniou if (pass) { 1929ce4fecf1SPantelis Antoniou if (buf < end) 1930ce4fecf1SPantelis Antoniou *buf = ':'; 1931ce4fecf1SPantelis Antoniou buf++; 1932ce4fecf1SPantelis Antoniou } 1933ce4fecf1SPantelis Antoniou 1934ce4fecf1SPantelis Antoniou switch (*fmt) { 1935ce4fecf1SPantelis Antoniou case 'f': /* full_name */ 1936ce4fecf1SPantelis Antoniou buf = device_node_gen_full_name(dn, buf, end); 1937ce4fecf1SPantelis Antoniou break; 1938ce4fecf1SPantelis Antoniou case 'n': /* name */ 19396d0a70a2SRob Herring p = kbasename(of_node_full_name(dn)); 19406d0a70a2SRob Herring precision = str_spec.precision; 19416d0a70a2SRob Herring str_spec.precision = strchrnul(p, '@') - p; 19426d0a70a2SRob Herring buf = string(buf, end, p, str_spec); 19436d0a70a2SRob Herring str_spec.precision = precision; 1944ce4fecf1SPantelis Antoniou break; 1945ce4fecf1SPantelis Antoniou case 'p': /* phandle */ 1946ce4fecf1SPantelis Antoniou buf = number(buf, end, (unsigned int)dn->phandle, num_spec); 1947ce4fecf1SPantelis Antoniou break; 1948ce4fecf1SPantelis Antoniou case 'P': /* path-spec */ 1949ce4fecf1SPantelis Antoniou p = kbasename(of_node_full_name(dn)); 1950ce4fecf1SPantelis Antoniou if (!p[1]) 1951ce4fecf1SPantelis Antoniou p = "/"; 1952ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 1953ce4fecf1SPantelis Antoniou break; 1954ce4fecf1SPantelis Antoniou case 'F': /* flags */ 1955ce4fecf1SPantelis Antoniou tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 1956ce4fecf1SPantelis Antoniou tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 1957ce4fecf1SPantelis Antoniou tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 1958ce4fecf1SPantelis Antoniou tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 1959ce4fecf1SPantelis Antoniou tbuf[4] = 0; 1960d529ac41SPetr Mladek buf = string_nocheck(buf, end, tbuf, str_spec); 1961ce4fecf1SPantelis Antoniou break; 1962ce4fecf1SPantelis Antoniou case 'c': /* major compatible string */ 1963ce4fecf1SPantelis Antoniou ret = of_property_read_string(dn, "compatible", &p); 1964ce4fecf1SPantelis Antoniou if (!ret) 1965ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 1966ce4fecf1SPantelis Antoniou break; 1967ce4fecf1SPantelis Antoniou case 'C': /* full compatible string */ 1968ce4fecf1SPantelis Antoniou has_mult = false; 1969ce4fecf1SPantelis Antoniou of_property_for_each_string(dn, "compatible", prop, p) { 1970ce4fecf1SPantelis Antoniou if (has_mult) 1971d529ac41SPetr Mladek buf = string_nocheck(buf, end, ",", str_spec); 1972d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 1973ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 1974d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 1975ce4fecf1SPantelis Antoniou 1976ce4fecf1SPantelis Antoniou has_mult = true; 1977ce4fecf1SPantelis Antoniou } 1978ce4fecf1SPantelis Antoniou break; 1979ce4fecf1SPantelis Antoniou default: 1980ce4fecf1SPantelis Antoniou break; 1981ce4fecf1SPantelis Antoniou } 1982ce4fecf1SPantelis Antoniou } 1983ce4fecf1SPantelis Antoniou 1984ce4fecf1SPantelis Antoniou return widen_string(buf, buf - buf_start, end, spec); 1985ce4fecf1SPantelis Antoniou } 1986ce4fecf1SPantelis Antoniou 1987798cc27aSPetr Mladek static char *kobject_string(char *buf, char *end, void *ptr, 1988798cc27aSPetr Mladek struct printf_spec spec, const char *fmt) 1989798cc27aSPetr Mladek { 1990798cc27aSPetr Mladek switch (fmt[1]) { 1991798cc27aSPetr Mladek case 'F': 1992798cc27aSPetr Mladek return device_node_string(buf, end, ptr, spec, fmt + 1); 1993798cc27aSPetr Mladek } 1994798cc27aSPetr Mladek 1995c8c3b584SPetr Mladek return error_string(buf, end, "(%pO?)", spec); 1996798cc27aSPetr Mladek } 1997798cc27aSPetr Mladek 19984d8a743cSLinus Torvalds /* 19994d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 20004d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 20014d8a743cSLinus Torvalds * specifiers. 20024d8a743cSLinus Torvalds * 20030b523769SJoe Perches * Please update scripts/checkpatch.pl when adding/removing conversion 20040b523769SJoe Perches * characters. (Search for "check for vsprintf extension"). 20050b523769SJoe Perches * 2006332d2e78SLinus Torvalds * Right now we handle: 20070fe1ef24SLinus Torvalds * 2008cdb7e52dSSergey Senozhatsky * - 'S' For symbolic direct pointers (or function descriptors) with offset 2009cdb7e52dSSergey Senozhatsky * - 's' For symbolic direct pointers (or function descriptors) without offset 2010cdb7e52dSSergey Senozhatsky * - 'F' Same as 'S' 2011cdb7e52dSSergey Senozhatsky * - 'f' Same as 's' 2012b0d33c2bSJoe Perches * - '[FfSs]R' as above with __builtin_extract_return_addr() translation 20130f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 2014c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 2015c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 2016dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 2017dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 2018dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 2019dbc760bcSTejun Heo * range-list format instead of hex format 2020dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 2021dd45c9cfSHarvey Harrison * usual colon-separated hex notation 20228a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 2023bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 2024c8e00060SJoe Perches * with a dash-separated hex notation 20257c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 20268a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 20278a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 20288a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 202910679643SDaniel Borkmann * [S][pfs] 203010679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 203110679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 20328a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 20338a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 20348a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 203510679643SDaniel Borkmann * [S][pfs] 203610679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 203710679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 203810679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 203910679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 204029cf519eSJoe Perches * http://tools.ietf.org/html/rfc5952 204171dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 204271dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 204371dca95dSAndy Shevchenko * details): 204471dca95dSAndy Shevchenko * a - ESCAPE_ANY 204571dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 204671dca95dSAndy Shevchenko * h - ESCAPE_HEX 204771dca95dSAndy Shevchenko * n - ESCAPE_NULL 204871dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 204971dca95dSAndy Shevchenko * p - ESCAPE_NP 205071dca95dSAndy Shevchenko * s - ESCAPE_SPACE 205171dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 20529ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 20539ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 20549ac6e44eSJoe Perches * Options for %pU are: 20559ac6e44eSJoe Perches * b big endian lower case hex (default) 20569ac6e44eSJoe Perches * B big endian UPPER case hex 20579ac6e44eSJoe Perches * l little endian lower case hex 20589ac6e44eSJoe Perches * L little endian UPPER case hex 20599ac6e44eSJoe Perches * big endian output byte order is: 20609ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 20619ac6e44eSJoe Perches * little endian output byte order is: 20629ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 20637db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 20647db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 20657db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 20667db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 20677db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 2068455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 2069c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 207031550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 207131550a16SAndy Shevchenko * a certain separator (' ' by default): 207231550a16SAndy Shevchenko * C colon 207331550a16SAndy Shevchenko * D dash 207431550a16SAndy Shevchenko * N no separator 207531550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 207631550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 2077aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 2078aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 2079c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 2080c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 20811031bc58SDmitry Monakhov * - 'g' For block_device name (gendisk + partition number) 20824d42c447SAndy Shevchenko * - 't[R][dt][r]' For time and date as represented: 20834d42c447SAndy Shevchenko * R struct rtc_time 2084900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 2085900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2086900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 2087900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2088edf14cdbSVlastimil Babka * - 'G' For flags to be printed as a collection of symbolic strings that would 2089edf14cdbSVlastimil Babka * construct the specific value. Supported flags given by option: 2090edf14cdbSVlastimil Babka * p page flags (see struct page) given as pointer to unsigned long 2091edf14cdbSVlastimil Babka * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 2092edf14cdbSVlastimil Babka * v vma flags (VM_*) given as pointer to unsigned long 2093ce4fecf1SPantelis Antoniou * - 'OF[fnpPcCF]' For a device tree object 2094ce4fecf1SPantelis Antoniou * Without any optional arguments prints the full_name 2095ce4fecf1SPantelis Antoniou * f device node full_name 2096ce4fecf1SPantelis Antoniou * n device node name 2097ce4fecf1SPantelis Antoniou * p device node phandle 2098ce4fecf1SPantelis Antoniou * P device node path spec (name + @unit) 2099ce4fecf1SPantelis Antoniou * F device node flags 2100ce4fecf1SPantelis Antoniou * c major compatible string 2101ce4fecf1SPantelis Antoniou * C full compatible string 21027b1924a1STobin C. Harding * - 'x' For printing the address. Equivalent to "%lx". 21037b1924a1STobin C. Harding * 2104b3ed2321STobin C. Harding * ** When making changes please also update: 2105b3ed2321STobin C. Harding * Documentation/core-api/printk-formats.rst 21069ac6e44eSJoe Perches * 2107ad67b74dSTobin C. Harding * Note: The default behaviour (unadorned %p) is to hash the address, 2108ad67b74dSTobin C. Harding * rendering it useful as a unique identifier. 21094d8a743cSLinus Torvalds */ 2110cf3b429bSJoe Perches static noinline_for_stack 2111cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 2112fef20d9cSFrederic Weisbecker struct printf_spec spec) 211378a8bf69SLinus Torvalds { 21140fe1ef24SLinus Torvalds switch (*fmt) { 21150fe1ef24SLinus Torvalds case 'F': 21160c8b946eSFrederic Weisbecker case 'f': 21170fe1ef24SLinus Torvalds case 'S': 21189ac6e44eSJoe Perches case 's': 211904b8eb7aSSergey Senozhatsky ptr = dereference_symbol_descriptor(ptr); 212004b8eb7aSSergey Senozhatsky /* Fallthrough */ 21210f77a8d3SNamhyung Kim case 'B': 2122b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 2123332d2e78SLinus Torvalds case 'R': 2124c7dabef8SBjorn Helgaas case 'r': 2125fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 212631550a16SAndy Shevchenko case 'h': 212731550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 2128dbc760bcSTejun Heo case 'b': 2129dbc760bcSTejun Heo switch (fmt[1]) { 2130dbc760bcSTejun Heo case 'l': 2131dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 2132dbc760bcSTejun Heo default: 2133dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 2134dbc760bcSTejun Heo } 21358a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 21368a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 213776597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 213876597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 21398a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 21408a27f7c9SJoe Perches case 'I': /* Formatted IP supported 21418a27f7c9SJoe Perches * 4: 1.2.3.4 21428a27f7c9SJoe Perches * 6: 0001:0203:...:0708 21438a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 21448a27f7c9SJoe Perches */ 21458a27f7c9SJoe Perches case 'i': /* Contiguous: 21468a27f7c9SJoe Perches * 4: 001.002.003.004 21478a27f7c9SJoe Perches * 6: 000102...0f 21488a27f7c9SJoe Perches */ 2149f00cc102SPetr Mladek return ip_addr_string(buf, end, ptr, spec, fmt); 215071dca95dSAndy Shevchenko case 'E': 215171dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 21529ac6e44eSJoe Perches case 'U': 21539ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 21547db6f5fbSJoe Perches case 'V': 21553e5903ebSPetr Mladek return va_format(buf, end, ptr, spec, fmt); 2156455cd5abSDan Rosenberg case 'K': 215757e73442STobin C. Harding return restricted_pointer(buf, end, ptr, spec); 2158c8f44affSMichał Mirosław case 'N': 2159431bca24SGeert Uytterhoeven return netdev_bits(buf, end, ptr, spec, fmt); 21607d799210SStepan Moskovchenko case 'a': 21613e5903ebSPetr Mladek return address_val(buf, end, ptr, spec, fmt); 21624b6ccca7SAl Viro case 'd': 21634b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 21644d42c447SAndy Shevchenko case 't': 21654d42c447SAndy Shevchenko return time_and_date(buf, end, ptr, spec, fmt); 2166900cca29SGeert Uytterhoeven case 'C': 2167900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 21684b6ccca7SAl Viro case 'D': 21694b6ccca7SAl Viro return dentry_name(buf, end, 21704b6ccca7SAl Viro ((const struct file *)ptr)->f_path.dentry, 21714b6ccca7SAl Viro spec, fmt); 21721031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 21731031bc58SDmitry Monakhov case 'g': 21741031bc58SDmitry Monakhov return bdev_name(buf, end, ptr, spec, fmt); 21751031bc58SDmitry Monakhov #endif 21761031bc58SDmitry Monakhov 2177edf14cdbSVlastimil Babka case 'G': 21780b74d4d7SPetr Mladek return flags_string(buf, end, ptr, spec, fmt); 2179ce4fecf1SPantelis Antoniou case 'O': 2180798cc27aSPetr Mladek return kobject_string(buf, end, ptr, spec, fmt); 21817b1924a1STobin C. Harding case 'x': 21827b1924a1STobin C. Harding return pointer_string(buf, end, ptr, spec); 21830fe1ef24SLinus Torvalds } 2184fef20d9cSFrederic Weisbecker 2185ad67b74dSTobin C. Harding /* default is to _not_ leak addresses, hash before printing */ 2186ad67b74dSTobin C. Harding return ptr_to_id(buf, end, ptr, spec); 2187fef20d9cSFrederic Weisbecker } 2188fef20d9cSFrederic Weisbecker 2189fef20d9cSFrederic Weisbecker /* 2190fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 2191fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 2192fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 2193fef20d9cSFrederic Weisbecker * to go on the next call). 2194fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 2195fef20d9cSFrederic Weisbecker * 2196fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 2197fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 2198fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 21995b5e0928SAlexey Dobriyan * 'Z' changed to 'z' --adobriyan 2017-01-25 2200fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 2201fef20d9cSFrederic Weisbecker * 2202fef20d9cSFrederic Weisbecker * @fmt: the format string 2203fef20d9cSFrederic Weisbecker * @type of the token returned 2204fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 2205fef20d9cSFrederic Weisbecker * @field_width: overwritten width 2206fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 2207fef20d9cSFrederic Weisbecker * @precision: precision of a number 2208fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 2209fef20d9cSFrederic Weisbecker */ 2210cf3b429bSJoe Perches static noinline_for_stack 2211cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 2212fef20d9cSFrederic Weisbecker { 2213fef20d9cSFrederic Weisbecker const char *start = fmt; 2214d0484193SRasmus Villemoes char qualifier; 2215fef20d9cSFrederic Weisbecker 2216fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 2217ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 2218fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 2219fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 2220fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 2221fef20d9cSFrederic Weisbecker } 2222fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2223fef20d9cSFrederic Weisbecker goto precision; 2224fef20d9cSFrederic Weisbecker } 2225fef20d9cSFrederic Weisbecker 2226fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 2227fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 2228fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2229fef20d9cSFrederic Weisbecker spec->precision = 0; 2230fef20d9cSFrederic Weisbecker 2231fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2232fef20d9cSFrederic Weisbecker goto qualifier; 2233fef20d9cSFrederic Weisbecker } 2234fef20d9cSFrederic Weisbecker 2235fef20d9cSFrederic Weisbecker /* By default */ 2236fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2237fef20d9cSFrederic Weisbecker 2238fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 2239fef20d9cSFrederic Weisbecker if (*fmt == '%') 2240fef20d9cSFrederic Weisbecker break; 2241fef20d9cSFrederic Weisbecker } 2242fef20d9cSFrederic Weisbecker 2243fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 2244fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 2245fef20d9cSFrederic Weisbecker return fmt - start; 2246fef20d9cSFrederic Weisbecker 2247fef20d9cSFrederic Weisbecker /* Process flags */ 2248fef20d9cSFrederic Weisbecker spec->flags = 0; 2249fef20d9cSFrederic Weisbecker 2250fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 2251fef20d9cSFrederic Weisbecker bool found = true; 2252fef20d9cSFrederic Weisbecker 2253fef20d9cSFrederic Weisbecker ++fmt; 2254fef20d9cSFrederic Weisbecker 2255fef20d9cSFrederic Weisbecker switch (*fmt) { 2256fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 2257fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 2258fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 2259fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 2260fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 2261fef20d9cSFrederic Weisbecker default: found = false; 2262fef20d9cSFrederic Weisbecker } 2263fef20d9cSFrederic Weisbecker 2264fef20d9cSFrederic Weisbecker if (!found) 2265fef20d9cSFrederic Weisbecker break; 2266fef20d9cSFrederic Weisbecker } 2267fef20d9cSFrederic Weisbecker 2268fef20d9cSFrederic Weisbecker /* get field width */ 2269fef20d9cSFrederic Weisbecker spec->field_width = -1; 2270fef20d9cSFrederic Weisbecker 2271fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 2272fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 2273fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 2274fef20d9cSFrederic Weisbecker /* it's the next argument */ 2275ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 2276fef20d9cSFrederic Weisbecker return ++fmt - start; 2277fef20d9cSFrederic Weisbecker } 2278fef20d9cSFrederic Weisbecker 2279fef20d9cSFrederic Weisbecker precision: 2280fef20d9cSFrederic Weisbecker /* get the precision */ 2281fef20d9cSFrederic Weisbecker spec->precision = -1; 2282fef20d9cSFrederic Weisbecker if (*fmt == '.') { 2283fef20d9cSFrederic Weisbecker ++fmt; 2284fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 2285fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 2286fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2287fef20d9cSFrederic Weisbecker spec->precision = 0; 2288fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 2289fef20d9cSFrederic Weisbecker /* it's the next argument */ 2290adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 2291fef20d9cSFrederic Weisbecker return ++fmt - start; 2292fef20d9cSFrederic Weisbecker } 2293fef20d9cSFrederic Weisbecker } 2294fef20d9cSFrederic Weisbecker 2295fef20d9cSFrederic Weisbecker qualifier: 2296fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 2297d0484193SRasmus Villemoes qualifier = 0; 229875fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 22995b5e0928SAlexey Dobriyan *fmt == 'z' || *fmt == 't') { 2300d0484193SRasmus Villemoes qualifier = *fmt++; 2301d0484193SRasmus Villemoes if (unlikely(qualifier == *fmt)) { 2302d0484193SRasmus Villemoes if (qualifier == 'l') { 2303d0484193SRasmus Villemoes qualifier = 'L'; 2304fef20d9cSFrederic Weisbecker ++fmt; 2305d0484193SRasmus Villemoes } else if (qualifier == 'h') { 2306d0484193SRasmus Villemoes qualifier = 'H'; 2307a4e94ef0SZhaolei ++fmt; 2308a4e94ef0SZhaolei } 2309fef20d9cSFrederic Weisbecker } 2310fef20d9cSFrederic Weisbecker } 2311fef20d9cSFrederic Weisbecker 2312fef20d9cSFrederic Weisbecker /* default base */ 2313fef20d9cSFrederic Weisbecker spec->base = 10; 2314fef20d9cSFrederic Weisbecker switch (*fmt) { 2315fef20d9cSFrederic Weisbecker case 'c': 2316fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 2317fef20d9cSFrederic Weisbecker return ++fmt - start; 2318fef20d9cSFrederic Weisbecker 2319fef20d9cSFrederic Weisbecker case 's': 2320fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 2321fef20d9cSFrederic Weisbecker return ++fmt - start; 2322fef20d9cSFrederic Weisbecker 2323fef20d9cSFrederic Weisbecker case 'p': 2324fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 2325ffbfed03SRasmus Villemoes return ++fmt - start; 2326fef20d9cSFrederic Weisbecker 2327fef20d9cSFrederic Weisbecker case '%': 2328fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 2329fef20d9cSFrederic Weisbecker return ++fmt - start; 2330fef20d9cSFrederic Weisbecker 2331fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 2332fef20d9cSFrederic Weisbecker case 'o': 2333fef20d9cSFrederic Weisbecker spec->base = 8; 2334fef20d9cSFrederic Weisbecker break; 2335fef20d9cSFrederic Weisbecker 2336fef20d9cSFrederic Weisbecker case 'x': 2337fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 23387e6bd6f3SAndy Shevchenko /* fall through */ 2339fef20d9cSFrederic Weisbecker 2340fef20d9cSFrederic Weisbecker case 'X': 2341fef20d9cSFrederic Weisbecker spec->base = 16; 2342fef20d9cSFrederic Weisbecker break; 2343fef20d9cSFrederic Weisbecker 2344fef20d9cSFrederic Weisbecker case 'd': 2345fef20d9cSFrederic Weisbecker case 'i': 234639e874f8SFrederic Weisbecker spec->flags |= SIGN; 2347fef20d9cSFrederic Weisbecker case 'u': 2348fef20d9cSFrederic Weisbecker break; 2349fef20d9cSFrederic Weisbecker 2350708d96fdSRyan Mallon case 'n': 2351708d96fdSRyan Mallon /* 2352b006f19bSRasmus Villemoes * Since %n poses a greater security risk than 2353b006f19bSRasmus Villemoes * utility, treat it as any other invalid or 2354b006f19bSRasmus Villemoes * unsupported format specifier. 2355708d96fdSRyan Mallon */ 2356708d96fdSRyan Mallon /* Fall-through */ 2357708d96fdSRyan Mallon 2358fef20d9cSFrederic Weisbecker default: 2359b006f19bSRasmus Villemoes WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 2360fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 2361fef20d9cSFrederic Weisbecker return fmt - start; 2362fef20d9cSFrederic Weisbecker } 2363fef20d9cSFrederic Weisbecker 2364d0484193SRasmus Villemoes if (qualifier == 'L') 2365fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 2366d0484193SRasmus Villemoes else if (qualifier == 'l') { 236751be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 236851be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 23695b5e0928SAlexey Dobriyan } else if (qualifier == 'z') { 2370fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 2371d0484193SRasmus Villemoes } else if (qualifier == 't') { 2372fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 2373d0484193SRasmus Villemoes } else if (qualifier == 'H') { 237451be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 237551be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 2376d0484193SRasmus Villemoes } else if (qualifier == 'h') { 237751be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 237851be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 2379fef20d9cSFrederic Weisbecker } else { 238051be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 238151be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 2382fef20d9cSFrederic Weisbecker } 2383fef20d9cSFrederic Weisbecker 2384fef20d9cSFrederic Weisbecker return ++fmt - start; 238578a8bf69SLinus Torvalds } 238678a8bf69SLinus Torvalds 23874d72ba01SRasmus Villemoes static void 23884d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width) 23894d72ba01SRasmus Villemoes { 23904d72ba01SRasmus Villemoes spec->field_width = width; 23914d72ba01SRasmus Villemoes if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 23924d72ba01SRasmus Villemoes spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 23934d72ba01SRasmus Villemoes } 23944d72ba01SRasmus Villemoes } 23954d72ba01SRasmus Villemoes 23964d72ba01SRasmus Villemoes static void 23974d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec) 23984d72ba01SRasmus Villemoes { 23994d72ba01SRasmus Villemoes spec->precision = prec; 24004d72ba01SRasmus Villemoes if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 24014d72ba01SRasmus Villemoes spec->precision = clamp(prec, 0, PRECISION_MAX); 24024d72ba01SRasmus Villemoes } 24034d72ba01SRasmus Villemoes } 24044d72ba01SRasmus Villemoes 24051da177e4SLinus Torvalds /** 24061da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 24071da177e4SLinus Torvalds * @buf: The buffer to place the result into 24081da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 24091da177e4SLinus Torvalds * @fmt: The format string to use 24101da177e4SLinus Torvalds * @args: Arguments for the format string 24111da177e4SLinus Torvalds * 2412d7ec9a05SRasmus Villemoes * This function generally follows C99 vsnprintf, but has some 2413d7ec9a05SRasmus Villemoes * extensions and a few limitations: 2414d7ec9a05SRasmus Villemoes * 24156cc89134Smchehab@s-opensource.com * - ``%n`` is unsupported 24166cc89134Smchehab@s-opensource.com * - ``%p*`` is handled by pointer() 241720036fdcSAndi Kleen * 241827e7c0e8SJonathan Corbet * See pointer() or Documentation/core-api/printk-formats.rst for more 24195e4ee7b1SMartin Kletzander * extensive description. 24205e4ee7b1SMartin Kletzander * 24215e4ee7b1SMartin Kletzander * **Please update the documentation in both places when making changes** 242280f548e0SAndrew Morton * 24231da177e4SLinus Torvalds * The return value is the number of characters which would 24241da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 24251da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 24261da177e4SLinus Torvalds * number of characters written into @buf as return value 242772fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 24281da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 24291da177e4SLinus Torvalds * string is truncated. 24301da177e4SLinus Torvalds * 2431ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 24321da177e4SLinus Torvalds */ 24331da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 24341da177e4SLinus Torvalds { 24351da177e4SLinus Torvalds unsigned long long num; 2436d4be151bSAndré Goddard Rosa char *str, *end; 2437fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 24381da177e4SLinus Torvalds 2439f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 2440f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 24412aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 24421da177e4SLinus Torvalds return 0; 24431da177e4SLinus Torvalds 24441da177e4SLinus Torvalds str = buf; 2445f796937aSJeremy Fitzhardinge end = buf + size; 24461da177e4SLinus Torvalds 2447f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 2448f796937aSJeremy Fitzhardinge if (end < buf) { 24491da177e4SLinus Torvalds end = ((void *)-1); 2450f796937aSJeremy Fitzhardinge size = end - buf; 24511da177e4SLinus Torvalds } 24521da177e4SLinus Torvalds 2453fef20d9cSFrederic Weisbecker while (*fmt) { 2454fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2455d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2456fef20d9cSFrederic Weisbecker 2457fef20d9cSFrederic Weisbecker fmt += read; 2458fef20d9cSFrederic Weisbecker 2459fef20d9cSFrederic Weisbecker switch (spec.type) { 2460fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2461fef20d9cSFrederic Weisbecker int copy = read; 2462fef20d9cSFrederic Weisbecker if (str < end) { 2463fef20d9cSFrederic Weisbecker if (copy > end - str) 2464fef20d9cSFrederic Weisbecker copy = end - str; 2465fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2466fef20d9cSFrederic Weisbecker } 2467fef20d9cSFrederic Weisbecker str += read; 2468fef20d9cSFrederic Weisbecker break; 24691da177e4SLinus Torvalds } 24701da177e4SLinus Torvalds 2471ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 24724d72ba01SRasmus Villemoes set_field_width(&spec, va_arg(args, int)); 2473fef20d9cSFrederic Weisbecker break; 24741da177e4SLinus Torvalds 2475fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 24764d72ba01SRasmus Villemoes set_precision(&spec, va_arg(args, int)); 2477fef20d9cSFrederic Weisbecker break; 24781da177e4SLinus Torvalds 2479d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2480d4be151bSAndré Goddard Rosa char c; 2481d4be151bSAndré Goddard Rosa 2482fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2483fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2484f796937aSJeremy Fitzhardinge if (str < end) 24851da177e4SLinus Torvalds *str = ' '; 24861da177e4SLinus Torvalds ++str; 2487fef20d9cSFrederic Weisbecker 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds } 24901da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 2491f796937aSJeremy Fitzhardinge if (str < end) 24921da177e4SLinus Torvalds *str = c; 24931da177e4SLinus Torvalds ++str; 2494fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2495f796937aSJeremy Fitzhardinge if (str < end) 24961da177e4SLinus Torvalds *str = ' '; 24971da177e4SLinus Torvalds ++str; 24981da177e4SLinus Torvalds } 2499fef20d9cSFrederic Weisbecker break; 2500d4be151bSAndré Goddard Rosa } 25011da177e4SLinus Torvalds 2502fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 2503fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 2504fef20d9cSFrederic Weisbecker break; 25051da177e4SLinus Torvalds 2506fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2507ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 2508fef20d9cSFrederic Weisbecker spec); 2509fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 25104d8a743cSLinus Torvalds fmt++; 2511fef20d9cSFrederic Weisbecker break; 25121da177e4SLinus Torvalds 2513fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2514f796937aSJeremy Fitzhardinge if (str < end) 25151da177e4SLinus Torvalds *str = '%'; 25161da177e4SLinus Torvalds ++str; 25171da177e4SLinus Torvalds break; 25181da177e4SLinus Torvalds 2519fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 2520b006f19bSRasmus Villemoes /* 2521b006f19bSRasmus Villemoes * Presumably the arguments passed gcc's type 2522b006f19bSRasmus Villemoes * checking, but there is no safe or sane way 2523b006f19bSRasmus Villemoes * for us to continue parsing the format and 2524b006f19bSRasmus Villemoes * fetching from the va_list; the remaining 2525b006f19bSRasmus Villemoes * specifiers and arguments would be out of 2526b006f19bSRasmus Villemoes * sync. 2527b006f19bSRasmus Villemoes */ 2528b006f19bSRasmus Villemoes goto out; 2529fef20d9cSFrederic Weisbecker 2530fef20d9cSFrederic Weisbecker default: 2531fef20d9cSFrederic Weisbecker switch (spec.type) { 2532fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2533fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 2534fef20d9cSFrederic Weisbecker break; 2535fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2536fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 2537fef20d9cSFrederic Weisbecker break; 2538fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2539fef20d9cSFrederic Weisbecker num = va_arg(args, long); 2540fef20d9cSFrederic Weisbecker break; 2541fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2542ef124960SJason Gunthorpe if (spec.flags & SIGN) 2543ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 2544ef124960SJason Gunthorpe else 2545fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 2546fef20d9cSFrederic Weisbecker break; 2547fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2548fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 2549fef20d9cSFrederic Weisbecker break; 2550a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2551a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 2552a4e94ef0SZhaolei break; 2553a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2554a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 2555a4e94ef0SZhaolei break; 2556fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2557fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 2558fef20d9cSFrederic Weisbecker break; 2559fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2560fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 2561fef20d9cSFrederic Weisbecker break; 256239e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 256339e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 2564fef20d9cSFrederic Weisbecker break; 2565fef20d9cSFrederic Weisbecker default: 2566fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 25671da177e4SLinus Torvalds } 2568fef20d9cSFrederic Weisbecker 2569fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 25701da177e4SLinus Torvalds } 2571fef20d9cSFrederic Weisbecker } 2572fef20d9cSFrederic Weisbecker 2573b006f19bSRasmus Villemoes out: 2574f796937aSJeremy Fitzhardinge if (size > 0) { 2575f796937aSJeremy Fitzhardinge if (str < end) 25761da177e4SLinus Torvalds *str = '\0'; 2577f796937aSJeremy Fitzhardinge else 25780a6047eeSLinus Torvalds end[-1] = '\0'; 2579f796937aSJeremy Fitzhardinge } 2580fef20d9cSFrederic Weisbecker 2581f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 25821da177e4SLinus Torvalds return str-buf; 2583fef20d9cSFrederic Weisbecker 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 25861da177e4SLinus Torvalds 25871da177e4SLinus Torvalds /** 25881da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 25891da177e4SLinus Torvalds * @buf: The buffer to place the result into 25901da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 25911da177e4SLinus Torvalds * @fmt: The format string to use 25921da177e4SLinus Torvalds * @args: Arguments for the format string 25931da177e4SLinus Torvalds * 25941da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2595b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 25961da177e4SLinus Torvalds * returns 0. 25971da177e4SLinus Torvalds * 2598ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 259920036fdcSAndi Kleen * 260020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 26011da177e4SLinus Torvalds */ 26021da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 26031da177e4SLinus Torvalds { 26041da177e4SLinus Torvalds int i; 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 26077b9186f5SAndré Goddard Rosa 2608b921c69fSAnton Arapov if (likely(i < size)) 2609b921c69fSAnton Arapov return i; 2610b921c69fSAnton Arapov if (size != 0) 2611b921c69fSAnton Arapov return size - 1; 2612b921c69fSAnton Arapov return 0; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds /** 26171da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 26181da177e4SLinus Torvalds * @buf: The buffer to place the result into 26191da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 26201da177e4SLinus Torvalds * @fmt: The format string to use 26211da177e4SLinus Torvalds * @...: Arguments for the format string 26221da177e4SLinus Torvalds * 26231da177e4SLinus Torvalds * The return value is the number of characters which would be 26241da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 26251da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 26261da177e4SLinus Torvalds * @size, the resulting string is truncated. 262720036fdcSAndi Kleen * 262820036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 26291da177e4SLinus Torvalds */ 26301da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 26311da177e4SLinus Torvalds { 26321da177e4SLinus Torvalds va_list args; 26331da177e4SLinus Torvalds int i; 26341da177e4SLinus Torvalds 26351da177e4SLinus Torvalds va_start(args, fmt); 26361da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 26371da177e4SLinus Torvalds va_end(args); 26387b9186f5SAndré Goddard Rosa 26391da177e4SLinus Torvalds return i; 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds /** 26441da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 26451da177e4SLinus Torvalds * @buf: The buffer to place the result into 26461da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 26471da177e4SLinus Torvalds * @fmt: The format string to use 26481da177e4SLinus Torvalds * @...: Arguments for the format string 26491da177e4SLinus Torvalds * 26501da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2651b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 26521da177e4SLinus Torvalds */ 26531da177e4SLinus Torvalds 26541da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 26551da177e4SLinus Torvalds { 26561da177e4SLinus Torvalds va_list args; 26571da177e4SLinus Torvalds int i; 26581da177e4SLinus Torvalds 26591da177e4SLinus Torvalds va_start(args, fmt); 2660b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 26611da177e4SLinus Torvalds va_end(args); 26627b9186f5SAndré Goddard Rosa 2663b903c0b8SChangli Gao return i; 26641da177e4SLinus Torvalds } 26651da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds /** 26681da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 26691da177e4SLinus Torvalds * @buf: The buffer to place the result into 26701da177e4SLinus Torvalds * @fmt: The format string to use 26711da177e4SLinus Torvalds * @args: Arguments for the format string 26721da177e4SLinus Torvalds * 26731da177e4SLinus Torvalds * The function returns the number of characters written 267472fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 26751da177e4SLinus Torvalds * buffer overflows. 26761da177e4SLinus Torvalds * 2677ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 267820036fdcSAndi Kleen * 267920036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 26801da177e4SLinus Torvalds */ 26811da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 26821da177e4SLinus Torvalds { 26831da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 26861da177e4SLinus Torvalds 26871da177e4SLinus Torvalds /** 26881da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 26891da177e4SLinus Torvalds * @buf: The buffer to place the result into 26901da177e4SLinus Torvalds * @fmt: The format string to use 26911da177e4SLinus Torvalds * @...: Arguments for the format string 26921da177e4SLinus Torvalds * 26931da177e4SLinus Torvalds * The function returns the number of characters written 269472fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 26951da177e4SLinus Torvalds * buffer overflows. 269620036fdcSAndi Kleen * 269720036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 26981da177e4SLinus Torvalds */ 26991da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 27001da177e4SLinus Torvalds { 27011da177e4SLinus Torvalds va_list args; 27021da177e4SLinus Torvalds int i; 27031da177e4SLinus Torvalds 27041da177e4SLinus Torvalds va_start(args, fmt); 27051da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 27061da177e4SLinus Torvalds va_end(args); 27077b9186f5SAndré Goddard Rosa 27081da177e4SLinus Torvalds return i; 27091da177e4SLinus Torvalds } 27101da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 27111da177e4SLinus Torvalds 27124370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 27134370aa4aSLai Jiangshan /* 27144370aa4aSLai Jiangshan * bprintf service: 27154370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 27164370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 27174370aa4aSLai Jiangshan */ 27184370aa4aSLai Jiangshan 27194370aa4aSLai Jiangshan /** 27204370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 27214370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 27224370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 27234370aa4aSLai Jiangshan * @fmt: The format string to use 27244370aa4aSLai Jiangshan * @args: Arguments for the format string 27254370aa4aSLai Jiangshan * 27264370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 2727da3dae54SMasanari Iida * is skipped. 27284370aa4aSLai Jiangshan * 27294370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 27304370aa4aSLai Jiangshan * the given input. 27314370aa4aSLai Jiangshan * 27324370aa4aSLai Jiangshan * NOTE: 27334370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 27344370aa4aSLai Jiangshan * valid for bstr_printf(). 27354370aa4aSLai Jiangshan */ 27364370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 27374370aa4aSLai Jiangshan { 2738fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 27394370aa4aSLai Jiangshan char *str, *end; 2740841a915dSSteven Rostedt (VMware) int width; 27414370aa4aSLai Jiangshan 27424370aa4aSLai Jiangshan str = (char *)bin_buf; 27434370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 27444370aa4aSLai Jiangshan 27454370aa4aSLai Jiangshan #define save_arg(type) \ 2746841a915dSSteven Rostedt (VMware) ({ \ 27474370aa4aSLai Jiangshan unsigned long long value; \ 2748841a915dSSteven Rostedt (VMware) if (sizeof(type) == 8) { \ 2749841a915dSSteven Rostedt (VMware) unsigned long long val8; \ 27504370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 2751841a915dSSteven Rostedt (VMware) val8 = va_arg(args, unsigned long long); \ 27524370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 2753841a915dSSteven Rostedt (VMware) *(u32 *)str = *(u32 *)&val8; \ 2754841a915dSSteven Rostedt (VMware) *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 27554370aa4aSLai Jiangshan } \ 2756841a915dSSteven Rostedt (VMware) value = val8; \ 27574370aa4aSLai Jiangshan } else { \ 2758841a915dSSteven Rostedt (VMware) unsigned int val4; \ 27594370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 2760841a915dSSteven Rostedt (VMware) val4 = va_arg(args, int); \ 27614370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 2762841a915dSSteven Rostedt (VMware) *(typeof(type) *)str = (type)(long)val4; \ 2763841a915dSSteven Rostedt (VMware) value = (unsigned long long)val4; \ 27644370aa4aSLai Jiangshan } \ 27654370aa4aSLai Jiangshan str += sizeof(type); \ 2766841a915dSSteven Rostedt (VMware) value; \ 2767841a915dSSteven Rostedt (VMware) }) 27684370aa4aSLai Jiangshan 2769fef20d9cSFrederic Weisbecker while (*fmt) { 2770d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 27714370aa4aSLai Jiangshan 2772fef20d9cSFrederic Weisbecker fmt += read; 2773fef20d9cSFrederic Weisbecker 2774fef20d9cSFrederic Weisbecker switch (spec.type) { 2775fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 2776d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 2777fef20d9cSFrederic Weisbecker break; 2778b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2779b006f19bSRasmus Villemoes goto out; 2780fef20d9cSFrederic Weisbecker 2781ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2782fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2783841a915dSSteven Rostedt (VMware) width = (int)save_arg(int); 2784841a915dSSteven Rostedt (VMware) /* Pointers may require the width */ 2785841a915dSSteven Rostedt (VMware) if (*fmt == 'p') 2786841a915dSSteven Rostedt (VMware) set_field_width(&spec, width); 2787fef20d9cSFrederic Weisbecker break; 27884370aa4aSLai Jiangshan 2789fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 27904370aa4aSLai Jiangshan save_arg(char); 2791fef20d9cSFrederic Weisbecker break; 2792fef20d9cSFrederic Weisbecker 2793fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 27944370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 27953e5903ebSPetr Mladek const char *err_msg; 27964370aa4aSLai Jiangshan size_t len; 27976c356634SAndré Goddard Rosa 27983e5903ebSPetr Mladek err_msg = check_pointer_msg(save_str); 27993e5903ebSPetr Mladek if (err_msg) 28003e5903ebSPetr Mladek save_str = err_msg; 28013e5903ebSPetr Mladek 28026c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 28036c356634SAndré Goddard Rosa if (str + len < end) 28046c356634SAndré Goddard Rosa memcpy(str, save_str, len); 28056c356634SAndré Goddard Rosa str += len; 2806fef20d9cSFrederic Weisbecker break; 28074370aa4aSLai Jiangshan } 2808fef20d9cSFrederic Weisbecker 2809fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2810841a915dSSteven Rostedt (VMware) /* Dereferenced pointers must be done now */ 2811841a915dSSteven Rostedt (VMware) switch (*fmt) { 2812841a915dSSteven Rostedt (VMware) /* Dereference of functions is still OK */ 2813841a915dSSteven Rostedt (VMware) case 'S': 2814841a915dSSteven Rostedt (VMware) case 's': 2815841a915dSSteven Rostedt (VMware) case 'F': 2816841a915dSSteven Rostedt (VMware) case 'f': 28171e6338cfSSteven Rostedt (VMware) case 'x': 28181e6338cfSSteven Rostedt (VMware) case 'K': 28194370aa4aSLai Jiangshan save_arg(void *); 2820841a915dSSteven Rostedt (VMware) break; 2821841a915dSSteven Rostedt (VMware) default: 2822841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 2823841a915dSSteven Rostedt (VMware) save_arg(void *); 2824841a915dSSteven Rostedt (VMware) break; 2825841a915dSSteven Rostedt (VMware) } 2826841a915dSSteven Rostedt (VMware) str = pointer(fmt, str, end, va_arg(args, void *), 2827841a915dSSteven Rostedt (VMware) spec); 2828841a915dSSteven Rostedt (VMware) if (str + 1 < end) 2829841a915dSSteven Rostedt (VMware) *str++ = '\0'; 2830841a915dSSteven Rostedt (VMware) else 2831841a915dSSteven Rostedt (VMware) end[-1] = '\0'; /* Must be nul terminated */ 2832841a915dSSteven Rostedt (VMware) } 28334370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2834fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 28354370aa4aSLai Jiangshan fmt++; 2836fef20d9cSFrederic Weisbecker break; 2837fef20d9cSFrederic Weisbecker 2838fef20d9cSFrederic Weisbecker default: 2839fef20d9cSFrederic Weisbecker switch (spec.type) { 2840fef20d9cSFrederic Weisbecker 2841fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2842fef20d9cSFrederic Weisbecker save_arg(long long); 2843fef20d9cSFrederic Weisbecker break; 2844fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2845fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2846fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2847fef20d9cSFrederic Weisbecker break; 2848fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2849fef20d9cSFrederic Weisbecker save_arg(size_t); 2850fef20d9cSFrederic Weisbecker break; 2851fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2852fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2853fef20d9cSFrederic Weisbecker break; 2854a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2855a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2856a4e94ef0SZhaolei save_arg(char); 2857a4e94ef0SZhaolei break; 2858fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2859fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2860fef20d9cSFrederic Weisbecker save_arg(short); 2861fef20d9cSFrederic Weisbecker break; 2862fef20d9cSFrederic Weisbecker default: 2863fef20d9cSFrederic Weisbecker save_arg(int); 2864fef20d9cSFrederic Weisbecker } 2865fef20d9cSFrederic Weisbecker } 2866fef20d9cSFrederic Weisbecker } 2867fef20d9cSFrederic Weisbecker 2868b006f19bSRasmus Villemoes out: 28697b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2870fef20d9cSFrederic Weisbecker #undef save_arg 28714370aa4aSLai Jiangshan } 28724370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 28734370aa4aSLai Jiangshan 28744370aa4aSLai Jiangshan /** 28754370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 28764370aa4aSLai Jiangshan * @buf: The buffer to place the result into 28774370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 28784370aa4aSLai Jiangshan * @fmt: The format string to use 28794370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 28804370aa4aSLai Jiangshan * 28814370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 28824370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 28834370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 28844370aa4aSLai Jiangshan * 28854370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 28860efb4d20SSteven Rostedt * see vsnprintf comment for details. 28874370aa4aSLai Jiangshan * 28884370aa4aSLai Jiangshan * The return value is the number of characters which would 28894370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 28904370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 28914370aa4aSLai Jiangshan * number of characters written into @buf as return value 28924370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 28934370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 28944370aa4aSLai Jiangshan * string is truncated. 28954370aa4aSLai Jiangshan */ 28964370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 28974370aa4aSLai Jiangshan { 2898fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 2899d4be151bSAndré Goddard Rosa char *str, *end; 2900d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 29014370aa4aSLai Jiangshan 2902762abb51SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 29034370aa4aSLai Jiangshan return 0; 29044370aa4aSLai Jiangshan 29054370aa4aSLai Jiangshan str = buf; 29064370aa4aSLai Jiangshan end = buf + size; 29074370aa4aSLai Jiangshan 29084370aa4aSLai Jiangshan #define get_arg(type) \ 29094370aa4aSLai Jiangshan ({ \ 29104370aa4aSLai Jiangshan typeof(type) value; \ 29114370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 29124370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 29134370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 29144370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 29154370aa4aSLai Jiangshan } else { \ 29164370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 29174370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 29184370aa4aSLai Jiangshan } \ 29194370aa4aSLai Jiangshan args += sizeof(type); \ 29204370aa4aSLai Jiangshan value; \ 29214370aa4aSLai Jiangshan }) 29224370aa4aSLai Jiangshan 29234370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 29244370aa4aSLai Jiangshan if (end < buf) { 29254370aa4aSLai Jiangshan end = ((void *)-1); 29264370aa4aSLai Jiangshan size = end - buf; 29274370aa4aSLai Jiangshan } 29284370aa4aSLai Jiangshan 2929fef20d9cSFrederic Weisbecker while (*fmt) { 2930fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2931d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2932fef20d9cSFrederic Weisbecker 2933fef20d9cSFrederic Weisbecker fmt += read; 2934fef20d9cSFrederic Weisbecker 2935fef20d9cSFrederic Weisbecker switch (spec.type) { 2936fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2937fef20d9cSFrederic Weisbecker int copy = read; 2938fef20d9cSFrederic Weisbecker if (str < end) { 2939fef20d9cSFrederic Weisbecker if (copy > end - str) 2940fef20d9cSFrederic Weisbecker copy = end - str; 2941fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2942fef20d9cSFrederic Weisbecker } 2943fef20d9cSFrederic Weisbecker str += read; 2944fef20d9cSFrederic Weisbecker break; 29454370aa4aSLai Jiangshan } 29464370aa4aSLai Jiangshan 2947ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 29484d72ba01SRasmus Villemoes set_field_width(&spec, get_arg(int)); 2949fef20d9cSFrederic Weisbecker break; 29504370aa4aSLai Jiangshan 2951fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 29524d72ba01SRasmus Villemoes set_precision(&spec, get_arg(int)); 2953fef20d9cSFrederic Weisbecker break; 29544370aa4aSLai Jiangshan 2955d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2956d4be151bSAndré Goddard Rosa char c; 2957d4be151bSAndré Goddard Rosa 2958fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2959fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 29604370aa4aSLai Jiangshan if (str < end) 29614370aa4aSLai Jiangshan *str = ' '; 29624370aa4aSLai Jiangshan ++str; 29634370aa4aSLai Jiangshan } 29644370aa4aSLai Jiangshan } 29654370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 29664370aa4aSLai Jiangshan if (str < end) 29674370aa4aSLai Jiangshan *str = c; 29684370aa4aSLai Jiangshan ++str; 2969fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 29704370aa4aSLai Jiangshan if (str < end) 29714370aa4aSLai Jiangshan *str = ' '; 29724370aa4aSLai Jiangshan ++str; 29734370aa4aSLai Jiangshan } 2974fef20d9cSFrederic Weisbecker break; 2975d4be151bSAndré Goddard Rosa } 29764370aa4aSLai Jiangshan 2977fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 29784370aa4aSLai Jiangshan const char *str_arg = args; 2979d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 2980fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 2981fef20d9cSFrederic Weisbecker break; 29824370aa4aSLai Jiangshan } 29834370aa4aSLai Jiangshan 2984841a915dSSteven Rostedt (VMware) case FORMAT_TYPE_PTR: { 2985841a915dSSteven Rostedt (VMware) bool process = false; 2986841a915dSSteven Rostedt (VMware) int copy, len; 2987841a915dSSteven Rostedt (VMware) /* Non function dereferences were already done */ 2988841a915dSSteven Rostedt (VMware) switch (*fmt) { 2989841a915dSSteven Rostedt (VMware) case 'S': 2990841a915dSSteven Rostedt (VMware) case 's': 2991841a915dSSteven Rostedt (VMware) case 'F': 2992841a915dSSteven Rostedt (VMware) case 'f': 29931e6338cfSSteven Rostedt (VMware) case 'x': 29941e6338cfSSteven Rostedt (VMware) case 'K': 2995841a915dSSteven Rostedt (VMware) process = true; 2996841a915dSSteven Rostedt (VMware) break; 2997841a915dSSteven Rostedt (VMware) default: 2998841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 2999841a915dSSteven Rostedt (VMware) process = true; 3000841a915dSSteven Rostedt (VMware) break; 3001841a915dSSteven Rostedt (VMware) } 3002841a915dSSteven Rostedt (VMware) /* Pointer dereference was already processed */ 3003841a915dSSteven Rostedt (VMware) if (str < end) { 3004841a915dSSteven Rostedt (VMware) len = copy = strlen(args); 3005841a915dSSteven Rostedt (VMware) if (copy > end - str) 3006841a915dSSteven Rostedt (VMware) copy = end - str; 3007841a915dSSteven Rostedt (VMware) memcpy(str, args, copy); 3008841a915dSSteven Rostedt (VMware) str += len; 300962165600SSteven Rostedt (VMware) args += len + 1; 3010841a915dSSteven Rostedt (VMware) } 3011841a915dSSteven Rostedt (VMware) } 3012841a915dSSteven Rostedt (VMware) if (process) 3013ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 3014841a915dSSteven Rostedt (VMware) 3015fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 30164370aa4aSLai Jiangshan fmt++; 3017fef20d9cSFrederic Weisbecker break; 3018841a915dSSteven Rostedt (VMware) } 30194370aa4aSLai Jiangshan 3020fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 30214370aa4aSLai Jiangshan if (str < end) 30224370aa4aSLai Jiangshan *str = '%'; 30234370aa4aSLai Jiangshan ++str; 3024fef20d9cSFrederic Weisbecker break; 3025fef20d9cSFrederic Weisbecker 3026b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 3027b006f19bSRasmus Villemoes goto out; 3028b006f19bSRasmus Villemoes 3029d4be151bSAndré Goddard Rosa default: { 3030d4be151bSAndré Goddard Rosa unsigned long long num; 3031d4be151bSAndré Goddard Rosa 3032fef20d9cSFrederic Weisbecker switch (spec.type) { 3033fef20d9cSFrederic Weisbecker 3034fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 30354370aa4aSLai Jiangshan num = get_arg(long long); 3036fef20d9cSFrederic Weisbecker break; 3037fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 3038fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 3039fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 3040fef20d9cSFrederic Weisbecker break; 3041fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 30424370aa4aSLai Jiangshan num = get_arg(size_t); 3043fef20d9cSFrederic Weisbecker break; 3044fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 30454370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 3046fef20d9cSFrederic Weisbecker break; 3047a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 3048a4e94ef0SZhaolei num = get_arg(unsigned char); 3049a4e94ef0SZhaolei break; 3050a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 3051a4e94ef0SZhaolei num = get_arg(signed char); 3052a4e94ef0SZhaolei break; 3053fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 3054fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 3055fef20d9cSFrederic Weisbecker break; 3056fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 3057fef20d9cSFrederic Weisbecker num = get_arg(short); 3058fef20d9cSFrederic Weisbecker break; 3059fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 30604370aa4aSLai Jiangshan num = get_arg(unsigned int); 3061fef20d9cSFrederic Weisbecker break; 3062fef20d9cSFrederic Weisbecker default: 3063fef20d9cSFrederic Weisbecker num = get_arg(int); 30644370aa4aSLai Jiangshan } 3065fef20d9cSFrederic Weisbecker 3066fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 3067d4be151bSAndré Goddard Rosa } /* default: */ 3068d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 3069d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 3070fef20d9cSFrederic Weisbecker 3071b006f19bSRasmus Villemoes out: 30724370aa4aSLai Jiangshan if (size > 0) { 30734370aa4aSLai Jiangshan if (str < end) 30744370aa4aSLai Jiangshan *str = '\0'; 30754370aa4aSLai Jiangshan else 30764370aa4aSLai Jiangshan end[-1] = '\0'; 30774370aa4aSLai Jiangshan } 3078fef20d9cSFrederic Weisbecker 30794370aa4aSLai Jiangshan #undef get_arg 30804370aa4aSLai Jiangshan 30814370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 30824370aa4aSLai Jiangshan return str - buf; 30834370aa4aSLai Jiangshan } 30844370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 30854370aa4aSLai Jiangshan 30864370aa4aSLai Jiangshan /** 30874370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 30884370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 30894370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 30904370aa4aSLai Jiangshan * @fmt: The format string to use 30914370aa4aSLai Jiangshan * @...: Arguments for the format string 30924370aa4aSLai Jiangshan * 30934370aa4aSLai Jiangshan * The function returns the number of words(u32) written 30944370aa4aSLai Jiangshan * into @bin_buf. 30954370aa4aSLai Jiangshan */ 30964370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 30974370aa4aSLai Jiangshan { 30984370aa4aSLai Jiangshan va_list args; 30994370aa4aSLai Jiangshan int ret; 31004370aa4aSLai Jiangshan 31014370aa4aSLai Jiangshan va_start(args, fmt); 31024370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 31034370aa4aSLai Jiangshan va_end(args); 31047b9186f5SAndré Goddard Rosa 31054370aa4aSLai Jiangshan return ret; 31064370aa4aSLai Jiangshan } 31074370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 31084370aa4aSLai Jiangshan 31094370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 31104370aa4aSLai Jiangshan 31111da177e4SLinus Torvalds /** 31121da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 31131da177e4SLinus Torvalds * @buf: input buffer 31141da177e4SLinus Torvalds * @fmt: format of buffer 31151da177e4SLinus Torvalds * @args: arguments 31161da177e4SLinus Torvalds */ 31171da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 31181da177e4SLinus Torvalds { 31191da177e4SLinus Torvalds const char *str = buf; 31201da177e4SLinus Torvalds char *next; 31211da177e4SLinus Torvalds char digit; 31221da177e4SLinus Torvalds int num = 0; 3123ef0658f3SJoe Perches u8 qualifier; 312453809751SJan Beulich unsigned int base; 312553809751SJan Beulich union { 312653809751SJan Beulich long long s; 312753809751SJan Beulich unsigned long long u; 312853809751SJan Beulich } val; 3129ef0658f3SJoe Perches s16 field_width; 3130d4be151bSAndré Goddard Rosa bool is_sign; 31311da177e4SLinus Torvalds 3132da99075cSJan Beulich while (*fmt) { 31331da177e4SLinus Torvalds /* skip any white space in format */ 31341da177e4SLinus Torvalds /* white space in format matchs any amount of 31351da177e4SLinus Torvalds * white space, including none, in the input. 31361da177e4SLinus Torvalds */ 31371da177e4SLinus Torvalds if (isspace(*fmt)) { 3138e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 3139e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 31401da177e4SLinus Torvalds } 31411da177e4SLinus Torvalds 31421da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 31431da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 31441da177e4SLinus Torvalds if (*fmt++ != *str++) 31451da177e4SLinus Torvalds break; 31461da177e4SLinus Torvalds continue; 31471da177e4SLinus Torvalds } 31481da177e4SLinus Torvalds 31491da177e4SLinus Torvalds if (!*fmt) 31501da177e4SLinus Torvalds break; 31511da177e4SLinus Torvalds ++fmt; 31521da177e4SLinus Torvalds 31531da177e4SLinus Torvalds /* skip this conversion. 31541da177e4SLinus Torvalds * advance both strings to next white space 31551da177e4SLinus Torvalds */ 31561da177e4SLinus Torvalds if (*fmt == '*') { 3157da99075cSJan Beulich if (!*str) 3158da99075cSJan Beulich break; 3159f9310b2fSJessica Yu while (!isspace(*fmt) && *fmt != '%' && *fmt) { 3160f9310b2fSJessica Yu /* '%*[' not yet supported, invalid format */ 3161f9310b2fSJessica Yu if (*fmt == '[') 3162f9310b2fSJessica Yu return num; 31631da177e4SLinus Torvalds fmt++; 3164f9310b2fSJessica Yu } 31651da177e4SLinus Torvalds while (!isspace(*str) && *str) 31661da177e4SLinus Torvalds str++; 31671da177e4SLinus Torvalds continue; 31681da177e4SLinus Torvalds } 31691da177e4SLinus Torvalds 31701da177e4SLinus Torvalds /* get field width */ 31711da177e4SLinus Torvalds field_width = -1; 317253809751SJan Beulich if (isdigit(*fmt)) { 31731da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 317453809751SJan Beulich if (field_width <= 0) 317553809751SJan Beulich break; 317653809751SJan Beulich } 31771da177e4SLinus Torvalds 31781da177e4SLinus Torvalds /* get conversion qualifier */ 31791da177e4SLinus Torvalds qualifier = -1; 318075fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 31815b5e0928SAlexey Dobriyan *fmt == 'z') { 31821da177e4SLinus Torvalds qualifier = *fmt++; 31831da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 31841da177e4SLinus Torvalds if (qualifier == 'h') { 31851da177e4SLinus Torvalds qualifier = 'H'; 31861da177e4SLinus Torvalds fmt++; 31871da177e4SLinus Torvalds } else if (qualifier == 'l') { 31881da177e4SLinus Torvalds qualifier = 'L'; 31891da177e4SLinus Torvalds fmt++; 31901da177e4SLinus Torvalds } 31911da177e4SLinus Torvalds } 31921da177e4SLinus Torvalds } 31931da177e4SLinus Torvalds 3194da99075cSJan Beulich if (!*fmt) 3195da99075cSJan Beulich break; 3196da99075cSJan Beulich 3197da99075cSJan Beulich if (*fmt == 'n') { 3198da99075cSJan Beulich /* return number of characters read so far */ 3199da99075cSJan Beulich *va_arg(args, int *) = str - buf; 3200da99075cSJan Beulich ++fmt; 3201da99075cSJan Beulich continue; 3202da99075cSJan Beulich } 3203da99075cSJan Beulich 3204da99075cSJan Beulich if (!*str) 32051da177e4SLinus Torvalds break; 32061da177e4SLinus Torvalds 3207d4be151bSAndré Goddard Rosa base = 10; 32083f623ebaSFabian Frederick is_sign = false; 3209d4be151bSAndré Goddard Rosa 32101da177e4SLinus Torvalds switch (*fmt++) { 32111da177e4SLinus Torvalds case 'c': 32121da177e4SLinus Torvalds { 32131da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 32141da177e4SLinus Torvalds if (field_width == -1) 32151da177e4SLinus Torvalds field_width = 1; 32161da177e4SLinus Torvalds do { 32171da177e4SLinus Torvalds *s++ = *str++; 32181da177e4SLinus Torvalds } while (--field_width > 0 && *str); 32191da177e4SLinus Torvalds num++; 32201da177e4SLinus Torvalds } 32211da177e4SLinus Torvalds continue; 32221da177e4SLinus Torvalds case 's': 32231da177e4SLinus Torvalds { 32241da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 32251da177e4SLinus Torvalds if (field_width == -1) 32264be929beSAlexey Dobriyan field_width = SHRT_MAX; 32271da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 3228e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 32291da177e4SLinus Torvalds 32301da177e4SLinus Torvalds /* now copy until next white space */ 32317b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 32321da177e4SLinus Torvalds *s++ = *str++; 32331da177e4SLinus Torvalds *s = '\0'; 32341da177e4SLinus Torvalds num++; 32351da177e4SLinus Torvalds } 32361da177e4SLinus Torvalds continue; 3237f9310b2fSJessica Yu /* 3238f9310b2fSJessica Yu * Warning: This implementation of the '[' conversion specifier 3239f9310b2fSJessica Yu * deviates from its glibc counterpart in the following ways: 3240f9310b2fSJessica Yu * (1) It does NOT support ranges i.e. '-' is NOT a special 3241f9310b2fSJessica Yu * character 3242f9310b2fSJessica Yu * (2) It cannot match the closing bracket ']' itself 3243f9310b2fSJessica Yu * (3) A field width is required 3244f9310b2fSJessica Yu * (4) '%*[' (discard matching input) is currently not supported 3245f9310b2fSJessica Yu * 3246f9310b2fSJessica Yu * Example usage: 3247f9310b2fSJessica Yu * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3248f9310b2fSJessica Yu * buf1, buf2, buf3); 3249f9310b2fSJessica Yu * if (ret < 3) 3250f9310b2fSJessica Yu * // etc.. 3251f9310b2fSJessica Yu */ 3252f9310b2fSJessica Yu case '[': 3253f9310b2fSJessica Yu { 3254f9310b2fSJessica Yu char *s = (char *)va_arg(args, char *); 3255f9310b2fSJessica Yu DECLARE_BITMAP(set, 256) = {0}; 3256f9310b2fSJessica Yu unsigned int len = 0; 3257f9310b2fSJessica Yu bool negate = (*fmt == '^'); 3258f9310b2fSJessica Yu 3259f9310b2fSJessica Yu /* field width is required */ 3260f9310b2fSJessica Yu if (field_width == -1) 3261f9310b2fSJessica Yu return num; 3262f9310b2fSJessica Yu 3263f9310b2fSJessica Yu if (negate) 3264f9310b2fSJessica Yu ++fmt; 3265f9310b2fSJessica Yu 3266f9310b2fSJessica Yu for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 3267f9310b2fSJessica Yu set_bit((u8)*fmt, set); 3268f9310b2fSJessica Yu 3269f9310b2fSJessica Yu /* no ']' or no character set found */ 3270f9310b2fSJessica Yu if (!*fmt || !len) 3271f9310b2fSJessica Yu return num; 3272f9310b2fSJessica Yu ++fmt; 3273f9310b2fSJessica Yu 3274f9310b2fSJessica Yu if (negate) { 3275f9310b2fSJessica Yu bitmap_complement(set, set, 256); 3276f9310b2fSJessica Yu /* exclude null '\0' byte */ 3277f9310b2fSJessica Yu clear_bit(0, set); 3278f9310b2fSJessica Yu } 3279f9310b2fSJessica Yu 3280f9310b2fSJessica Yu /* match must be non-empty */ 3281f9310b2fSJessica Yu if (!test_bit((u8)*str, set)) 3282f9310b2fSJessica Yu return num; 3283f9310b2fSJessica Yu 3284f9310b2fSJessica Yu while (test_bit((u8)*str, set) && field_width--) 3285f9310b2fSJessica Yu *s++ = *str++; 3286f9310b2fSJessica Yu *s = '\0'; 3287f9310b2fSJessica Yu ++num; 3288f9310b2fSJessica Yu } 3289f9310b2fSJessica Yu continue; 32901da177e4SLinus Torvalds case 'o': 32911da177e4SLinus Torvalds base = 8; 32921da177e4SLinus Torvalds break; 32931da177e4SLinus Torvalds case 'x': 32941da177e4SLinus Torvalds case 'X': 32951da177e4SLinus Torvalds base = 16; 32961da177e4SLinus Torvalds break; 32971da177e4SLinus Torvalds case 'i': 32981da177e4SLinus Torvalds base = 0; 32997e6bd6f3SAndy Shevchenko /* fall through */ 33001da177e4SLinus Torvalds case 'd': 33013f623ebaSFabian Frederick is_sign = true; 33027e6bd6f3SAndy Shevchenko /* fall through */ 33031da177e4SLinus Torvalds case 'u': 33041da177e4SLinus Torvalds break; 33051da177e4SLinus Torvalds case '%': 33061da177e4SLinus Torvalds /* looking for '%' in str */ 33071da177e4SLinus Torvalds if (*str++ != '%') 33081da177e4SLinus Torvalds return num; 33091da177e4SLinus Torvalds continue; 33101da177e4SLinus Torvalds default: 33111da177e4SLinus Torvalds /* invalid format; stop here */ 33121da177e4SLinus Torvalds return num; 33131da177e4SLinus Torvalds } 33141da177e4SLinus Torvalds 33151da177e4SLinus Torvalds /* have some sort of integer conversion. 33161da177e4SLinus Torvalds * first, skip white space in buffer. 33171da177e4SLinus Torvalds */ 3318e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 33191da177e4SLinus Torvalds 33201da177e4SLinus Torvalds digit = *str; 33211da177e4SLinus Torvalds if (is_sign && digit == '-') 33221da177e4SLinus Torvalds digit = *(str + 1); 33231da177e4SLinus Torvalds 33241da177e4SLinus Torvalds if (!digit 33251da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 33261da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 33271da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 33281da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 33291da177e4SLinus Torvalds break; 33301da177e4SLinus Torvalds 333153809751SJan Beulich if (is_sign) 333253809751SJan Beulich val.s = qualifier != 'L' ? 333353809751SJan Beulich simple_strtol(str, &next, base) : 333453809751SJan Beulich simple_strtoll(str, &next, base); 333553809751SJan Beulich else 333653809751SJan Beulich val.u = qualifier != 'L' ? 333753809751SJan Beulich simple_strtoul(str, &next, base) : 333853809751SJan Beulich simple_strtoull(str, &next, base); 333953809751SJan Beulich 334053809751SJan Beulich if (field_width > 0 && next - str > field_width) { 334153809751SJan Beulich if (base == 0) 334253809751SJan Beulich _parse_integer_fixup_radix(str, &base); 334353809751SJan Beulich while (next - str > field_width) { 334453809751SJan Beulich if (is_sign) 334553809751SJan Beulich val.s = div_s64(val.s, base); 334653809751SJan Beulich else 334753809751SJan Beulich val.u = div_u64(val.u, base); 334853809751SJan Beulich --next; 334953809751SJan Beulich } 335053809751SJan Beulich } 335153809751SJan Beulich 33521da177e4SLinus Torvalds switch (qualifier) { 33531da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 335453809751SJan Beulich if (is_sign) 335553809751SJan Beulich *va_arg(args, signed char *) = val.s; 335653809751SJan Beulich else 335753809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 33581da177e4SLinus Torvalds break; 33591da177e4SLinus Torvalds case 'h': 336053809751SJan Beulich if (is_sign) 336153809751SJan Beulich *va_arg(args, short *) = val.s; 336253809751SJan Beulich else 336353809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 33641da177e4SLinus Torvalds break; 33651da177e4SLinus Torvalds case 'l': 336653809751SJan Beulich if (is_sign) 336753809751SJan Beulich *va_arg(args, long *) = val.s; 336853809751SJan Beulich else 336953809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 33701da177e4SLinus Torvalds break; 33711da177e4SLinus Torvalds case 'L': 337253809751SJan Beulich if (is_sign) 337353809751SJan Beulich *va_arg(args, long long *) = val.s; 337453809751SJan Beulich else 337553809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 33761da177e4SLinus Torvalds break; 33771da177e4SLinus Torvalds case 'z': 337853809751SJan Beulich *va_arg(args, size_t *) = val.u; 33791da177e4SLinus Torvalds break; 33801da177e4SLinus Torvalds default: 338153809751SJan Beulich if (is_sign) 338253809751SJan Beulich *va_arg(args, int *) = val.s; 338353809751SJan Beulich else 338453809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 33851da177e4SLinus Torvalds break; 33861da177e4SLinus Torvalds } 33871da177e4SLinus Torvalds num++; 33881da177e4SLinus Torvalds 33891da177e4SLinus Torvalds if (!next) 33901da177e4SLinus Torvalds break; 33911da177e4SLinus Torvalds str = next; 33921da177e4SLinus Torvalds } 3393c6b40d16SJohannes Berg 33941da177e4SLinus Torvalds return num; 33951da177e4SLinus Torvalds } 33961da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 33971da177e4SLinus Torvalds 33981da177e4SLinus Torvalds /** 33991da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 34001da177e4SLinus Torvalds * @buf: input buffer 34011da177e4SLinus Torvalds * @fmt: formatting of buffer 34021da177e4SLinus Torvalds * @...: resulting arguments 34031da177e4SLinus Torvalds */ 34041da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 34051da177e4SLinus Torvalds { 34061da177e4SLinus Torvalds va_list args; 34071da177e4SLinus Torvalds int i; 34081da177e4SLinus Torvalds 34091da177e4SLinus Torvalds va_start(args, fmt); 34101da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 34111da177e4SLinus Torvalds va_end(args); 34127b9186f5SAndré Goddard Rosa 34131da177e4SLinus Torvalds return i; 34141da177e4SLinus Torvalds } 34151da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 3416