1457c8996SThomas 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> 2457f5677eSRasmus Villemoes #include <linux/errname.h> 258bc3bcc9SPaul Gortmaker #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ 261da177e4SLinus Torvalds #include <linux/types.h> 271da177e4SLinus Torvalds #include <linux/string.h> 281da177e4SLinus Torvalds #include <linux/ctype.h> 291da177e4SLinus Torvalds #include <linux/kernel.h> 300fe1ef24SLinus Torvalds #include <linux/kallsyms.h> 3153809751SJan Beulich #include <linux/math64.h> 320fe1ef24SLinus Torvalds #include <linux/uaccess.h> 33332d2e78SLinus Torvalds #include <linux/ioport.h> 344b6ccca7SAl Viro #include <linux/dcache.h> 35312b4e22SRyan Mallon #include <linux/cred.h> 364d42c447SAndy Shevchenko #include <linux/rtc.h> 372b1b0d66SAndy Shevchenko #include <linux/uuid.h> 38ce4fecf1SPantelis Antoniou #include <linux/of.h> 398a27f7c9SJoe Perches #include <net/addrconf.h> 40ad67b74dSTobin C. Harding #include <linux/siphash.h> 41ad67b74dSTobin C. Harding #include <linux/compiler.h> 42a92eb762SSakari Ailus #include <linux/property.h> 431031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 441031bc58SDmitry Monakhov #include <linux/blkdev.h> 451031bc58SDmitry Monakhov #endif 461da177e4SLinus Torvalds 47edf14cdbSVlastimil Babka #include "../mm/internal.h" /* For the trace_print_flags arrays */ 48edf14cdbSVlastimil Babka 494e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 507c43d9a3SRasmus Villemoes #include <asm/byteorder.h> /* cpu_to_le16 */ 511da177e4SLinus Torvalds 5271dca95dSAndy Shevchenko #include <linux/string_helpers.h> 531dff46d6SAlexey Dobriyan #include "kstrtox.h" 54aa46a63eSHarvey Harrison 551da177e4SLinus Torvalds /** 561da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 571da177e4SLinus Torvalds * @cp: The start of the string 581da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 591da177e4SLinus Torvalds * @base: The number base to use 60462e4711SEldad Zack * 61*e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoull instead. 621da177e4SLinus Torvalds */ 631da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 641da177e4SLinus Torvalds { 651dff46d6SAlexey Dobriyan unsigned long long result; 661dff46d6SAlexey Dobriyan unsigned int rv; 671da177e4SLinus Torvalds 681dff46d6SAlexey Dobriyan cp = _parse_integer_fixup_radix(cp, &base); 691dff46d6SAlexey Dobriyan rv = _parse_integer(cp, base, &result); 701dff46d6SAlexey Dobriyan /* FIXME */ 711dff46d6SAlexey Dobriyan cp += (rv & ~KSTRTOX_OVERFLOW); 72aa46a63eSHarvey Harrison 731da177e4SLinus Torvalds if (endp) 741da177e4SLinus Torvalds *endp = (char *)cp; 757b9186f5SAndré Goddard Rosa 761da177e4SLinus Torvalds return result; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /** 81922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 82922ac25cSAndré Goddard Rosa * @cp: The start of the string 83922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 84922ac25cSAndré Goddard Rosa * @base: The number base to use 85462e4711SEldad Zack * 86*e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoul instead. 87922ac25cSAndré Goddard Rosa */ 88922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 89922ac25cSAndré Goddard Rosa { 90922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 91922ac25cSAndré Goddard Rosa } 92922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 93922ac25cSAndré Goddard Rosa 94922ac25cSAndré Goddard Rosa /** 95922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 96922ac25cSAndré Goddard Rosa * @cp: The start of the string 97922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 98922ac25cSAndré Goddard Rosa * @base: The number base to use 99462e4711SEldad Zack * 100*e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtol instead. 101922ac25cSAndré Goddard Rosa */ 102922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 103922ac25cSAndré Goddard Rosa { 104922ac25cSAndré Goddard Rosa if (*cp == '-') 105922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 106922ac25cSAndré Goddard Rosa 107922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 108922ac25cSAndré Goddard Rosa } 109922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 110922ac25cSAndré Goddard Rosa 111922ac25cSAndré Goddard Rosa /** 1121da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1131da177e4SLinus Torvalds * @cp: The start of the string 1141da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1151da177e4SLinus Torvalds * @base: The number base to use 116462e4711SEldad Zack * 117*e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoll instead. 1181da177e4SLinus Torvalds */ 1191da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1201da177e4SLinus Torvalds { 1211da177e4SLinus Torvalds if (*cp == '-') 1221da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1237b9186f5SAndré Goddard Rosa 1241da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1251da177e4SLinus Torvalds } 12698d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1271da177e4SLinus Torvalds 128cf3b429bSJoe Perches static noinline_for_stack 129cf3b429bSJoe Perches int skip_atoi(const char **s) 1301da177e4SLinus Torvalds { 1311da177e4SLinus Torvalds int i = 0; 1321da177e4SLinus Torvalds 13343e5b666SRasmus Villemoes do { 1341da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 13543e5b666SRasmus Villemoes } while (isdigit(**s)); 1367b9186f5SAndré Goddard Rosa 1371da177e4SLinus Torvalds return i; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1407c43d9a3SRasmus Villemoes /* 1417c43d9a3SRasmus Villemoes * Decimal conversion is by far the most typical, and is used for 1427c43d9a3SRasmus Villemoes * /proc and /sys data. This directly impacts e.g. top performance 1437c43d9a3SRasmus Villemoes * with many processes running. We optimize it for speed by emitting 1447c43d9a3SRasmus Villemoes * two characters at a time, using a 200 byte lookup table. This 1457c43d9a3SRasmus Villemoes * roughly halves the number of multiplications compared to computing 1467c43d9a3SRasmus Villemoes * the digits one at a time. Implementation strongly inspired by the 1477c43d9a3SRasmus Villemoes * previous version, which in turn used ideas described at 1487c43d9a3SRasmus Villemoes * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 1497c43d9a3SRasmus Villemoes * from the author, Douglas W. Jones). 1507c43d9a3SRasmus Villemoes * 1517c43d9a3SRasmus Villemoes * It turns out there is precisely one 26 bit fixed-point 1527c43d9a3SRasmus Villemoes * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 1537c43d9a3SRasmus Villemoes * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 1547c43d9a3SRasmus Villemoes * range happens to be somewhat larger (x <= 1073741898), but that's 1557c43d9a3SRasmus Villemoes * irrelevant for our purpose. 1567c43d9a3SRasmus Villemoes * 1577c43d9a3SRasmus Villemoes * For dividing a number in the range [10^4, 10^6-1] by 100, we still 1587c43d9a3SRasmus Villemoes * need a 32x32->64 bit multiply, so we simply use the same constant. 1597c43d9a3SRasmus Villemoes * 1607c43d9a3SRasmus Villemoes * For dividing a number in the range [100, 10^4-1] by 100, there are 1617c43d9a3SRasmus Villemoes * several options. The simplest is (x * 0x147b) >> 19, which is valid 1627c43d9a3SRasmus Villemoes * for all x <= 43698. 163133fd9f5SDenys Vlasenko */ 1644277eeddSDenis Vlasenko 1657c43d9a3SRasmus Villemoes static const u16 decpair[100] = { 1667c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 1677c43d9a3SRasmus Villemoes _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 1687c43d9a3SRasmus Villemoes _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 1697c43d9a3SRasmus Villemoes _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 1707c43d9a3SRasmus Villemoes _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 1717c43d9a3SRasmus Villemoes _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 1727c43d9a3SRasmus Villemoes _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 1737c43d9a3SRasmus Villemoes _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 1747c43d9a3SRasmus Villemoes _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 1757c43d9a3SRasmus Villemoes _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 1767c43d9a3SRasmus Villemoes _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 1777c43d9a3SRasmus Villemoes #undef _ 1787c43d9a3SRasmus Villemoes }; 1794277eeddSDenis Vlasenko 1807b9186f5SAndré Goddard Rosa /* 1817c43d9a3SRasmus Villemoes * This will print a single '0' even if r == 0, since we would 182675cf53cSRasmus Villemoes * immediately jump to out_r where two 0s would be written but only 183675cf53cSRasmus Villemoes * one of them accounted for in buf. This is needed by ip4_string 184675cf53cSRasmus Villemoes * below. All other callers pass a non-zero value of r. 185133fd9f5SDenys Vlasenko */ 186133fd9f5SDenys Vlasenko static noinline_for_stack 187133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 188133fd9f5SDenys Vlasenko { 189133fd9f5SDenys Vlasenko unsigned q; 1904277eeddSDenis Vlasenko 1917c43d9a3SRasmus Villemoes /* 1 <= r < 10^8 */ 1927c43d9a3SRasmus Villemoes if (r < 100) 1937c43d9a3SRasmus Villemoes goto out_r; 194cb239d0aSGeorge Spelvin 1957c43d9a3SRasmus Villemoes /* 100 <= r < 10^8 */ 1967c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 1977c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 1987c43d9a3SRasmus Villemoes buf += 2; 1997c43d9a3SRasmus Villemoes 2007c43d9a3SRasmus Villemoes /* 1 <= q < 10^6 */ 2017c43d9a3SRasmus Villemoes if (q < 100) 2027c43d9a3SRasmus Villemoes goto out_q; 2037c43d9a3SRasmus Villemoes 2047c43d9a3SRasmus Villemoes /* 100 <= q < 10^6 */ 2057c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2067c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2077c43d9a3SRasmus Villemoes buf += 2; 2087c43d9a3SRasmus Villemoes 2097c43d9a3SRasmus Villemoes /* 1 <= r < 10^4 */ 2107c43d9a3SRasmus Villemoes if (r < 100) 2117c43d9a3SRasmus Villemoes goto out_r; 2127c43d9a3SRasmus Villemoes 2137c43d9a3SRasmus Villemoes /* 100 <= r < 10^4 */ 2147c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2157c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2167c43d9a3SRasmus Villemoes buf += 2; 2177c43d9a3SRasmus Villemoes out_q: 2187c43d9a3SRasmus Villemoes /* 1 <= q < 100 */ 2197c43d9a3SRasmus Villemoes r = q; 2207c43d9a3SRasmus Villemoes out_r: 2217c43d9a3SRasmus Villemoes /* 1 <= r < 100 */ 2227c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r]; 223675cf53cSRasmus Villemoes buf += r < 10 ? 1 : 2; 224133fd9f5SDenys Vlasenko return buf; 225133fd9f5SDenys Vlasenko } 226133fd9f5SDenys Vlasenko 2277c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 2287c43d9a3SRasmus Villemoes static noinline_for_stack 2297c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r) 2307c43d9a3SRasmus Villemoes { 2317c43d9a3SRasmus Villemoes unsigned q; 232133fd9f5SDenys Vlasenko 2337c43d9a3SRasmus Villemoes /* 0 <= r < 10^8 */ 2347c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2357c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2367c43d9a3SRasmus Villemoes buf += 2; 237133fd9f5SDenys Vlasenko 2387c43d9a3SRasmus Villemoes /* 0 <= q < 10^6 */ 2397c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2407c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2417c43d9a3SRasmus Villemoes buf += 2; 242133fd9f5SDenys Vlasenko 2437c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2447c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2457c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2467c43d9a3SRasmus Villemoes buf += 2; 2477c43d9a3SRasmus Villemoes 2487c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2497c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2507c43d9a3SRasmus Villemoes buf += 2; 2517c43d9a3SRasmus Villemoes return buf; 2527c43d9a3SRasmus Villemoes } 2537c43d9a3SRasmus Villemoes 2547c43d9a3SRasmus Villemoes static noinline_for_stack 255133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 256133fd9f5SDenys Vlasenko { 257133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 2587c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2597c43d9a3SRasmus Villemoes /* 1 <= n <= 1.6e11 */ 2607c43d9a3SRasmus Villemoes if (n >= 100*1000*1000) 2617c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2627c43d9a3SRasmus Villemoes /* 1 <= n < 1e8 */ 263133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 264133fd9f5SDenys Vlasenko } 265133fd9f5SDenys Vlasenko 2667c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 267133fd9f5SDenys Vlasenko 2687c43d9a3SRasmus Villemoes static void 2697c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r) 270133fd9f5SDenys Vlasenko { 2717c43d9a3SRasmus Villemoes unsigned q; 2727c43d9a3SRasmus Villemoes 2737c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2747c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2757c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2767c43d9a3SRasmus Villemoes buf += 2; 2777c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2787c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2792359172aSGeorge Spelvin } 2802359172aSGeorge Spelvin 2812359172aSGeorge Spelvin /* 2822359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 2832359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 2842359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 2852359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 2867c43d9a3SRasmus Villemoes * (second call in the put_dec code, assuming n is all-ones). 2872359172aSGeorge Spelvin */ 2887c43d9a3SRasmus Villemoes static noinline_for_stack 2892359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 2902359172aSGeorge Spelvin { 2912359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 2922359172aSGeorge Spelvin 2932359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 2942359172aSGeorge Spelvin return q; 295133fd9f5SDenys Vlasenko } 296133fd9f5SDenys Vlasenko 297133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 298133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 299133fd9f5SDenys Vlasenko * (with permission from the author). 300133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 301133fd9f5SDenys Vlasenko */ 302133fd9f5SDenys Vlasenko static 303133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 304133fd9f5SDenys Vlasenko { 305133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 306133fd9f5SDenys Vlasenko 307133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 308133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 309133fd9f5SDenys Vlasenko 310133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 311133fd9f5SDenys Vlasenko h = (n >> 32); 312133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 313133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 314133fd9f5SDenys Vlasenko 3157c43d9a3SRasmus Villemoes /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 3167c43d9a3SRasmus Villemoes = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 317133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 3182359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 319133fd9f5SDenys Vlasenko 3202359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 3212359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 322133fd9f5SDenys Vlasenko 3232359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3242359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 325133fd9f5SDenys Vlasenko 3262359172aSGeorge Spelvin q += 281 * d3; 3272359172aSGeorge Spelvin buf += 12; 3282359172aSGeorge Spelvin if (q) 3292359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3302359172aSGeorge Spelvin else while (buf[-1] == '0') 331133fd9f5SDenys Vlasenko --buf; 3327b9186f5SAndré Goddard Rosa 3334277eeddSDenis Vlasenko return buf; 3344277eeddSDenis Vlasenko } 335133fd9f5SDenys Vlasenko 336133fd9f5SDenys Vlasenko #endif 3374277eeddSDenis Vlasenko 3381ac101a5SKAMEZAWA Hiroyuki /* 3391ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3401ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3411ac101a5SKAMEZAWA Hiroyuki * 3421ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3431ac101a5SKAMEZAWA Hiroyuki */ 344d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) 3451ac101a5SKAMEZAWA Hiroyuki { 3467c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 3477c43d9a3SRasmus Villemoes char tmp[sizeof(num) * 3] __aligned(2); 3481ac101a5SKAMEZAWA Hiroyuki int idx, len; 3491ac101a5SKAMEZAWA Hiroyuki 350133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 351133fd9f5SDenys Vlasenko if (num <= 9) { 352133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 353133fd9f5SDenys Vlasenko len = 1; 354133fd9f5SDenys Vlasenko } else { 3551ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 356133fd9f5SDenys Vlasenko } 3571ac101a5SKAMEZAWA Hiroyuki 358d1be35cbSAndrei Vagin if (len > size || width > size) 3591ac101a5SKAMEZAWA Hiroyuki return 0; 360d1be35cbSAndrei Vagin 361d1be35cbSAndrei Vagin if (width > len) { 362d1be35cbSAndrei Vagin width = width - len; 363d1be35cbSAndrei Vagin for (idx = 0; idx < width; idx++) 364d1be35cbSAndrei Vagin buf[idx] = ' '; 365d1be35cbSAndrei Vagin } else { 366d1be35cbSAndrei Vagin width = 0; 367d1be35cbSAndrei Vagin } 368d1be35cbSAndrei Vagin 3691ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 370d1be35cbSAndrei Vagin buf[idx + width] = tmp[len - idx - 1]; 371d1be35cbSAndrei Vagin 372d1be35cbSAndrei Vagin return len + width; 3731ac101a5SKAMEZAWA Hiroyuki } 3741ac101a5SKAMEZAWA Hiroyuki 37551be17dfSRasmus Villemoes #define SIGN 1 /* unsigned/signed, must be 1 */ 376d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */ 3771da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3781da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 379d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 380b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 381b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 3821da177e4SLinus Torvalds 383fef20d9cSFrederic Weisbecker enum format_type { 384fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 385ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 386fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 387fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 388fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 389fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 390fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 391fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 392fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 393fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 394fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 395a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 396a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 397fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 398fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 399fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 400fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 401fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 402fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 403fef20d9cSFrederic Weisbecker }; 404fef20d9cSFrederic Weisbecker 405fef20d9cSFrederic Weisbecker struct printf_spec { 406d0484193SRasmus Villemoes unsigned int type:8; /* format_type enum */ 407d0484193SRasmus Villemoes signed int field_width:24; /* width of output field */ 408d0484193SRasmus Villemoes unsigned int flags:8; /* flags to number() */ 409d0484193SRasmus Villemoes unsigned int base:8; /* number base, 8, 10 or 16 only */ 410d0484193SRasmus Villemoes signed int precision:16; /* # of digits/chars */ 411d0484193SRasmus Villemoes } __packed; 412ef27ac18SRasmus Villemoes static_assert(sizeof(struct printf_spec) == 8); 413ef27ac18SRasmus Villemoes 4144d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1) 4154d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1) 416fef20d9cSFrederic Weisbecker 417cf3b429bSJoe Perches static noinline_for_stack 418cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 419fef20d9cSFrederic Weisbecker struct printf_spec spec) 4201da177e4SLinus Torvalds { 4217c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 4227c43d9a3SRasmus Villemoes char tmp[3 * sizeof(num)] __aligned(2); 4239b706aeeSDenys Vlasenko char sign; 4249b706aeeSDenys Vlasenko char locase; 425fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4261da177e4SLinus Torvalds int i; 4277c203422SPierre Carrier bool is_zero = num == 0LL; 4281c7a8e62SRasmus Villemoes int field_width = spec.field_width; 4291c7a8e62SRasmus Villemoes int precision = spec.precision; 4301da177e4SLinus Torvalds 4319b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4329b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 433fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 434fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 435fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4361da177e4SLinus Torvalds sign = 0; 437fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4381da177e4SLinus Torvalds if ((signed long long)num < 0) { 4391da177e4SLinus Torvalds sign = '-'; 4401da177e4SLinus Torvalds num = -(signed long long)num; 4411c7a8e62SRasmus Villemoes field_width--; 442fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4431da177e4SLinus Torvalds sign = '+'; 4441c7a8e62SRasmus Villemoes field_width--; 445fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4461da177e4SLinus Torvalds sign = ' '; 4471c7a8e62SRasmus Villemoes field_width--; 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds } 450b39a7340SDenis Vlasenko if (need_pfx) { 451fef20d9cSFrederic Weisbecker if (spec.base == 16) 4521c7a8e62SRasmus Villemoes field_width -= 2; 4537c203422SPierre Carrier else if (!is_zero) 4541c7a8e62SRasmus Villemoes field_width--; 4551da177e4SLinus Torvalds } 456b39a7340SDenis Vlasenko 457b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4581da177e4SLinus Torvalds i = 0; 459133fd9f5SDenys Vlasenko if (num < spec.base) 4603ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase; 461fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 462fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 463b39a7340SDenis Vlasenko int shift = 3; 4647b9186f5SAndré Goddard Rosa 4657b9186f5SAndré Goddard Rosa if (spec.base == 16) 4667b9186f5SAndré Goddard Rosa shift = 4; 467b39a7340SDenis Vlasenko do { 4683ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 469b39a7340SDenis Vlasenko num >>= shift; 470b39a7340SDenis Vlasenko } while (num); 4714277eeddSDenis Vlasenko } else { /* base 10 */ 4724277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4734277eeddSDenis Vlasenko } 474b39a7340SDenis Vlasenko 475b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 4761c7a8e62SRasmus Villemoes if (i > precision) 4771c7a8e62SRasmus Villemoes precision = i; 478b39a7340SDenis Vlasenko /* leading space padding */ 4791c7a8e62SRasmus Villemoes field_width -= precision; 48051be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) { 4811c7a8e62SRasmus Villemoes while (--field_width >= 0) { 482f796937aSJeremy Fitzhardinge if (buf < end) 4831da177e4SLinus Torvalds *buf = ' '; 4841da177e4SLinus Torvalds ++buf; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds } 487b39a7340SDenis Vlasenko /* sign */ 4881da177e4SLinus Torvalds if (sign) { 489f796937aSJeremy Fitzhardinge if (buf < end) 4901da177e4SLinus Torvalds *buf = sign; 4911da177e4SLinus Torvalds ++buf; 4921da177e4SLinus Torvalds } 493b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 494b39a7340SDenis Vlasenko if (need_pfx) { 4957c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 496f796937aSJeremy Fitzhardinge if (buf < end) 4971da177e4SLinus Torvalds *buf = '0'; 4981da177e4SLinus Torvalds ++buf; 4997c203422SPierre Carrier } 500fef20d9cSFrederic Weisbecker if (spec.base == 16) { 501f796937aSJeremy Fitzhardinge if (buf < end) 5029b706aeeSDenys Vlasenko *buf = ('X' | locase); 5031da177e4SLinus Torvalds ++buf; 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds } 506b39a7340SDenis Vlasenko /* zero or space padding */ 507fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 508d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD); 509d1c1b121SRasmus Villemoes BUILD_BUG_ON(' ' + ZEROPAD != '0'); 5101c7a8e62SRasmus Villemoes while (--field_width >= 0) { 511f796937aSJeremy Fitzhardinge if (buf < end) 5121da177e4SLinus Torvalds *buf = c; 5131da177e4SLinus Torvalds ++buf; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds } 516b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 5171c7a8e62SRasmus Villemoes while (i <= --precision) { 518f796937aSJeremy Fitzhardinge if (buf < end) 5191da177e4SLinus Torvalds *buf = '0'; 5201da177e4SLinus Torvalds ++buf; 5211da177e4SLinus Torvalds } 522b39a7340SDenis Vlasenko /* actual digits of result */ 523b39a7340SDenis Vlasenko while (--i >= 0) { 524f796937aSJeremy Fitzhardinge if (buf < end) 5251da177e4SLinus Torvalds *buf = tmp[i]; 5261da177e4SLinus Torvalds ++buf; 5271da177e4SLinus Torvalds } 528b39a7340SDenis Vlasenko /* trailing space padding */ 5291c7a8e62SRasmus Villemoes while (--field_width >= 0) { 530f796937aSJeremy Fitzhardinge if (buf < end) 5311da177e4SLinus Torvalds *buf = ' '; 5321da177e4SLinus Torvalds ++buf; 5331da177e4SLinus Torvalds } 5347b9186f5SAndré Goddard Rosa 5351da177e4SLinus Torvalds return buf; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5383cab1e71SAndy Shevchenko static noinline_for_stack 5393cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size) 5403cab1e71SAndy Shevchenko { 5413cab1e71SAndy Shevchenko struct printf_spec spec; 5423cab1e71SAndy Shevchenko 5433cab1e71SAndy Shevchenko spec.type = FORMAT_TYPE_PTR; 5443cab1e71SAndy Shevchenko spec.field_width = 2 + 2 * size; /* 0x + hex */ 5453cab1e71SAndy Shevchenko spec.flags = SPECIAL | SMALL | ZEROPAD; 5463cab1e71SAndy Shevchenko spec.base = 16; 5473cab1e71SAndy Shevchenko spec.precision = -1; 5483cab1e71SAndy Shevchenko 5493cab1e71SAndy Shevchenko return number(buf, end, num, spec); 5503cab1e71SAndy Shevchenko } 5513cab1e71SAndy Shevchenko 552cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces) 5534b6ccca7SAl Viro { 5544b6ccca7SAl Viro size_t size; 5554b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5564b6ccca7SAl Viro return; 5574b6ccca7SAl Viro size = end - buf; 5584b6ccca7SAl Viro if (size <= spaces) { 5594b6ccca7SAl Viro memset(buf, ' ', size); 5604b6ccca7SAl Viro return; 5614b6ccca7SAl Viro } 5624b6ccca7SAl Viro if (len) { 5634b6ccca7SAl Viro if (len > size - spaces) 5644b6ccca7SAl Viro len = size - spaces; 5654b6ccca7SAl Viro memmove(buf + spaces, buf, len); 5664b6ccca7SAl Viro } 5674b6ccca7SAl Viro memset(buf, ' ', spaces); 5684b6ccca7SAl Viro } 5694b6ccca7SAl Viro 570cfccde04SRasmus Villemoes /* 571cfccde04SRasmus Villemoes * Handle field width padding for a string. 572cfccde04SRasmus Villemoes * @buf: current buffer position 573cfccde04SRasmus Villemoes * @n: length of string 574cfccde04SRasmus Villemoes * @end: end of output buffer 575cfccde04SRasmus Villemoes * @spec: for field width and flags 576cfccde04SRasmus Villemoes * Returns: new buffer position after padding. 577cfccde04SRasmus Villemoes */ 578cfccde04SRasmus Villemoes static noinline_for_stack 579cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec) 580cfccde04SRasmus Villemoes { 581cfccde04SRasmus Villemoes unsigned spaces; 582cfccde04SRasmus Villemoes 583cfccde04SRasmus Villemoes if (likely(n >= spec.field_width)) 584cfccde04SRasmus Villemoes return buf; 585cfccde04SRasmus Villemoes /* we want to pad the sucker */ 586cfccde04SRasmus Villemoes spaces = spec.field_width - n; 587cfccde04SRasmus Villemoes if (!(spec.flags & LEFT)) { 588cfccde04SRasmus Villemoes move_right(buf - n, end, n, spaces); 589cfccde04SRasmus Villemoes return buf + spaces; 590cfccde04SRasmus Villemoes } 591cfccde04SRasmus Villemoes while (spaces--) { 592cfccde04SRasmus Villemoes if (buf < end) 593cfccde04SRasmus Villemoes *buf = ' '; 594cfccde04SRasmus Villemoes ++buf; 595cfccde04SRasmus Villemoes } 596cfccde04SRasmus Villemoes return buf; 597cfccde04SRasmus Villemoes } 598cfccde04SRasmus Villemoes 599d529ac41SPetr Mladek /* Handle string from a well known address. */ 600d529ac41SPetr Mladek static char *string_nocheck(char *buf, char *end, const char *s, 601d529ac41SPetr Mladek struct printf_spec spec) 60295508cfaSRasmus Villemoes { 60334fc8b90SRasmus Villemoes int len = 0; 604b314dd49SYoungmin Nam int lim = spec.precision; 60595508cfaSRasmus Villemoes 60634fc8b90SRasmus Villemoes while (lim--) { 60734fc8b90SRasmus Villemoes char c = *s++; 60834fc8b90SRasmus Villemoes if (!c) 60934fc8b90SRasmus Villemoes break; 61095508cfaSRasmus Villemoes if (buf < end) 61134fc8b90SRasmus Villemoes *buf = c; 61295508cfaSRasmus Villemoes ++buf; 61334fc8b90SRasmus Villemoes ++len; 61495508cfaSRasmus Villemoes } 61534fc8b90SRasmus Villemoes return widen_string(buf, len, end, spec); 61695508cfaSRasmus Villemoes } 61795508cfaSRasmus Villemoes 61857f5677eSRasmus Villemoes static char *err_ptr(char *buf, char *end, void *ptr, 61957f5677eSRasmus Villemoes struct printf_spec spec) 62057f5677eSRasmus Villemoes { 62157f5677eSRasmus Villemoes int err = PTR_ERR(ptr); 62257f5677eSRasmus Villemoes const char *sym = errname(err); 62357f5677eSRasmus Villemoes 62457f5677eSRasmus Villemoes if (sym) 62557f5677eSRasmus Villemoes return string_nocheck(buf, end, sym, spec); 62657f5677eSRasmus Villemoes 62757f5677eSRasmus Villemoes /* 62857f5677eSRasmus Villemoes * Somebody passed ERR_PTR(-1234) or some other non-existing 62957f5677eSRasmus Villemoes * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to 63057f5677eSRasmus Villemoes * printing it as its decimal representation. 63157f5677eSRasmus Villemoes */ 63257f5677eSRasmus Villemoes spec.flags |= SIGN; 63357f5677eSRasmus Villemoes spec.base = 10; 63457f5677eSRasmus Villemoes return number(buf, end, err, spec); 63557f5677eSRasmus Villemoes } 63657f5677eSRasmus Villemoes 637c8c3b584SPetr Mladek /* Be careful: error messages must fit into the given buffer. */ 638c8c3b584SPetr Mladek static char *error_string(char *buf, char *end, const char *s, 639c8c3b584SPetr Mladek struct printf_spec spec) 640c8c3b584SPetr Mladek { 641c8c3b584SPetr Mladek /* 642c8c3b584SPetr Mladek * Hard limit to avoid a completely insane messages. It actually 643c8c3b584SPetr Mladek * works pretty well because most error messages are in 644c8c3b584SPetr Mladek * the many pointer format modifiers. 645c8c3b584SPetr Mladek */ 646c8c3b584SPetr Mladek if (spec.precision == -1) 647c8c3b584SPetr Mladek spec.precision = 2 * sizeof(void *); 648c8c3b584SPetr Mladek 649c8c3b584SPetr Mladek return string_nocheck(buf, end, s, spec); 650c8c3b584SPetr Mladek } 651c8c3b584SPetr Mladek 6523e5903ebSPetr Mladek /* 6532ac5a3bfSPetr Mladek * Do not call any complex external code here. Nested printk()/vsprintf() 6542ac5a3bfSPetr Mladek * might cause infinite loops. Failures might break printk() and would 6552ac5a3bfSPetr Mladek * be hard to debug. 6563e5903ebSPetr Mladek */ 6573e5903ebSPetr Mladek static const char *check_pointer_msg(const void *ptr) 6583e5903ebSPetr Mladek { 6593e5903ebSPetr Mladek if (!ptr) 6603e5903ebSPetr Mladek return "(null)"; 6613e5903ebSPetr Mladek 6622ac5a3bfSPetr Mladek if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr)) 6633e5903ebSPetr Mladek return "(efault)"; 6643e5903ebSPetr Mladek 6653e5903ebSPetr Mladek return NULL; 6663e5903ebSPetr Mladek } 6673e5903ebSPetr Mladek 6683e5903ebSPetr Mladek static int check_pointer(char **buf, char *end, const void *ptr, 6693e5903ebSPetr Mladek struct printf_spec spec) 6703e5903ebSPetr Mladek { 6713e5903ebSPetr Mladek const char *err_msg; 6723e5903ebSPetr Mladek 6733e5903ebSPetr Mladek err_msg = check_pointer_msg(ptr); 6743e5903ebSPetr Mladek if (err_msg) { 675c8c3b584SPetr Mladek *buf = error_string(*buf, end, err_msg, spec); 6763e5903ebSPetr Mladek return -EFAULT; 6773e5903ebSPetr Mladek } 6783e5903ebSPetr Mladek 6793e5903ebSPetr Mladek return 0; 6803e5903ebSPetr Mladek } 6813e5903ebSPetr Mladek 68295508cfaSRasmus Villemoes static noinline_for_stack 683d529ac41SPetr Mladek char *string(char *buf, char *end, const char *s, 684d529ac41SPetr Mladek struct printf_spec spec) 685d529ac41SPetr Mladek { 6863e5903ebSPetr Mladek if (check_pointer(&buf, end, s, spec)) 6873e5903ebSPetr Mladek return buf; 688d529ac41SPetr Mladek 689d529ac41SPetr Mladek return string_nocheck(buf, end, s, spec); 690d529ac41SPetr Mladek } 691d529ac41SPetr Mladek 692ce9d3eceSYueHaibing static char *pointer_string(char *buf, char *end, 693ce9d3eceSYueHaibing const void *ptr, 6949073dac1SGeert Uytterhoeven struct printf_spec spec) 6959073dac1SGeert Uytterhoeven { 6969073dac1SGeert Uytterhoeven spec.base = 16; 6979073dac1SGeert Uytterhoeven spec.flags |= SMALL; 6989073dac1SGeert Uytterhoeven if (spec.field_width == -1) { 6999073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr); 7009073dac1SGeert Uytterhoeven spec.flags |= ZEROPAD; 7019073dac1SGeert Uytterhoeven } 7029073dac1SGeert Uytterhoeven 7039073dac1SGeert Uytterhoeven return number(buf, end, (unsigned long int)ptr, spec); 7049073dac1SGeert Uytterhoeven } 7059073dac1SGeert Uytterhoeven 7069073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */ 7079073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init; 7089073dac1SGeert Uytterhoeven 7099073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str) 7109073dac1SGeert Uytterhoeven { 7119073dac1SGeert Uytterhoeven debug_boot_weak_hash = 1; 7129073dac1SGeert Uytterhoeven pr_info("debug_boot_weak_hash enabled\n"); 7139073dac1SGeert Uytterhoeven return 0; 7149073dac1SGeert Uytterhoeven } 7159073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); 7169073dac1SGeert Uytterhoeven 7179073dac1SGeert Uytterhoeven static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key); 7189073dac1SGeert Uytterhoeven static siphash_key_t ptr_key __read_mostly; 7199073dac1SGeert Uytterhoeven 7209073dac1SGeert Uytterhoeven static void enable_ptr_key_workfn(struct work_struct *work) 7219073dac1SGeert Uytterhoeven { 7229073dac1SGeert Uytterhoeven get_random_bytes(&ptr_key, sizeof(ptr_key)); 7239073dac1SGeert Uytterhoeven /* Needs to run from preemptible context */ 7249073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 7259073dac1SGeert Uytterhoeven } 7269073dac1SGeert Uytterhoeven 7279073dac1SGeert Uytterhoeven static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn); 7289073dac1SGeert Uytterhoeven 7299073dac1SGeert Uytterhoeven static void fill_random_ptr_key(struct random_ready_callback *unused) 7309073dac1SGeert Uytterhoeven { 7319073dac1SGeert Uytterhoeven /* This may be in an interrupt handler. */ 7329073dac1SGeert Uytterhoeven queue_work(system_unbound_wq, &enable_ptr_key_work); 7339073dac1SGeert Uytterhoeven } 7349073dac1SGeert Uytterhoeven 7359073dac1SGeert Uytterhoeven static struct random_ready_callback random_ready = { 7369073dac1SGeert Uytterhoeven .func = fill_random_ptr_key 7379073dac1SGeert Uytterhoeven }; 7389073dac1SGeert Uytterhoeven 7399073dac1SGeert Uytterhoeven static int __init initialize_ptr_random(void) 7409073dac1SGeert Uytterhoeven { 7419073dac1SGeert Uytterhoeven int key_size = sizeof(ptr_key); 7429073dac1SGeert Uytterhoeven int ret; 7439073dac1SGeert Uytterhoeven 7449073dac1SGeert Uytterhoeven /* Use hw RNG if available. */ 7459073dac1SGeert Uytterhoeven if (get_random_bytes_arch(&ptr_key, key_size) == key_size) { 7469073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 7479073dac1SGeert Uytterhoeven return 0; 7489073dac1SGeert Uytterhoeven } 7499073dac1SGeert Uytterhoeven 7509073dac1SGeert Uytterhoeven ret = add_random_ready_callback(&random_ready); 7519073dac1SGeert Uytterhoeven if (!ret) { 7529073dac1SGeert Uytterhoeven return 0; 7539073dac1SGeert Uytterhoeven } else if (ret == -EALREADY) { 7549073dac1SGeert Uytterhoeven /* This is in preemptible context */ 7559073dac1SGeert Uytterhoeven enable_ptr_key_workfn(&enable_ptr_key_work); 7569073dac1SGeert Uytterhoeven return 0; 7579073dac1SGeert Uytterhoeven } 7589073dac1SGeert Uytterhoeven 7599073dac1SGeert Uytterhoeven return ret; 7609073dac1SGeert Uytterhoeven } 7619073dac1SGeert Uytterhoeven early_initcall(initialize_ptr_random); 7629073dac1SGeert Uytterhoeven 7639073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */ 764e4dcad20SJoel Fernandes (Google) static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 7659073dac1SGeert Uytterhoeven { 7669073dac1SGeert Uytterhoeven unsigned long hashval; 7679073dac1SGeert Uytterhoeven 768e4dcad20SJoel Fernandes (Google) if (static_branch_unlikely(¬_filled_random_ptr_key)) 769e4dcad20SJoel Fernandes (Google) return -EAGAIN; 7709073dac1SGeert Uytterhoeven 7719073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT 7729073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); 7739073dac1SGeert Uytterhoeven /* 7749073dac1SGeert Uytterhoeven * Mask off the first 32 bits, this makes explicit that we have 7759073dac1SGeert Uytterhoeven * modified the address (and 32 bits is plenty for a unique ID). 7769073dac1SGeert Uytterhoeven */ 7779073dac1SGeert Uytterhoeven hashval = hashval & 0xffffffff; 7789073dac1SGeert Uytterhoeven #else 7799073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); 7809073dac1SGeert Uytterhoeven #endif 781e4dcad20SJoel Fernandes (Google) *hashval_out = hashval; 782e4dcad20SJoel Fernandes (Google) return 0; 783e4dcad20SJoel Fernandes (Google) } 784e4dcad20SJoel Fernandes (Google) 785e4dcad20SJoel Fernandes (Google) int ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 786e4dcad20SJoel Fernandes (Google) { 787e4dcad20SJoel Fernandes (Google) return __ptr_to_hashval(ptr, hashval_out); 788e4dcad20SJoel Fernandes (Google) } 789e4dcad20SJoel Fernandes (Google) 790e4dcad20SJoel Fernandes (Google) static char *ptr_to_id(char *buf, char *end, const void *ptr, 791e4dcad20SJoel Fernandes (Google) struct printf_spec spec) 792e4dcad20SJoel Fernandes (Google) { 793e4dcad20SJoel Fernandes (Google) const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; 794e4dcad20SJoel Fernandes (Google) unsigned long hashval; 795e4dcad20SJoel Fernandes (Google) int ret; 796e4dcad20SJoel Fernandes (Google) 797e4dcad20SJoel Fernandes (Google) /* When debugging early boot use non-cryptographically secure hash. */ 798e4dcad20SJoel Fernandes (Google) if (unlikely(debug_boot_weak_hash)) { 799e4dcad20SJoel Fernandes (Google) hashval = hash_long((unsigned long)ptr, 32); 800e4dcad20SJoel Fernandes (Google) return pointer_string(buf, end, (const void *)hashval, spec); 801e4dcad20SJoel Fernandes (Google) } 802e4dcad20SJoel Fernandes (Google) 803e4dcad20SJoel Fernandes (Google) ret = __ptr_to_hashval(ptr, &hashval); 804e4dcad20SJoel Fernandes (Google) if (ret) { 805e4dcad20SJoel Fernandes (Google) spec.field_width = 2 * sizeof(ptr); 806e4dcad20SJoel Fernandes (Google) /* string length must be less than default_width */ 807e4dcad20SJoel Fernandes (Google) return error_string(buf, end, str, spec); 808e4dcad20SJoel Fernandes (Google) } 809e4dcad20SJoel Fernandes (Google) 8109073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec); 8119073dac1SGeert Uytterhoeven } 8129073dac1SGeert Uytterhoeven 8136eea242fSPetr Mladek int kptr_restrict __read_mostly; 8146eea242fSPetr Mladek 8156eea242fSPetr Mladek static noinline_for_stack 8166eea242fSPetr Mladek char *restricted_pointer(char *buf, char *end, const void *ptr, 8176eea242fSPetr Mladek struct printf_spec spec) 8186eea242fSPetr Mladek { 8196eea242fSPetr Mladek switch (kptr_restrict) { 8206eea242fSPetr Mladek case 0: 8211ac2f978SPetr Mladek /* Handle as %p, hash and do _not_ leak addresses. */ 8221ac2f978SPetr Mladek return ptr_to_id(buf, end, ptr, spec); 8236eea242fSPetr Mladek case 1: { 8246eea242fSPetr Mladek const struct cred *cred; 8256eea242fSPetr Mladek 8266eea242fSPetr Mladek /* 8276eea242fSPetr Mladek * kptr_restrict==1 cannot be used in IRQ context 8286eea242fSPetr Mladek * because its test for CAP_SYSLOG would be meaningless. 8296eea242fSPetr Mladek */ 8306eea242fSPetr Mladek if (in_irq() || in_serving_softirq() || in_nmi()) { 8316eea242fSPetr Mladek if (spec.field_width == -1) 8326eea242fSPetr Mladek spec.field_width = 2 * sizeof(ptr); 833c8c3b584SPetr Mladek return error_string(buf, end, "pK-error", spec); 8346eea242fSPetr Mladek } 8356eea242fSPetr Mladek 8366eea242fSPetr Mladek /* 8376eea242fSPetr Mladek * Only print the real pointer value if the current 8386eea242fSPetr Mladek * process has CAP_SYSLOG and is running with the 8396eea242fSPetr Mladek * same credentials it started with. This is because 8406eea242fSPetr Mladek * access to files is checked at open() time, but %pK 8416eea242fSPetr Mladek * checks permission at read() time. We don't want to 8426eea242fSPetr Mladek * leak pointer values if a binary opens a file using 8436eea242fSPetr Mladek * %pK and then elevates privileges before reading it. 8446eea242fSPetr Mladek */ 8456eea242fSPetr Mladek cred = current_cred(); 8466eea242fSPetr Mladek if (!has_capability_noaudit(current, CAP_SYSLOG) || 8476eea242fSPetr Mladek !uid_eq(cred->euid, cred->uid) || 8486eea242fSPetr Mladek !gid_eq(cred->egid, cred->gid)) 8496eea242fSPetr Mladek ptr = NULL; 8506eea242fSPetr Mladek break; 8516eea242fSPetr Mladek } 8526eea242fSPetr Mladek case 2: 8536eea242fSPetr Mladek default: 8546eea242fSPetr Mladek /* Always print 0's for %pK */ 8556eea242fSPetr Mladek ptr = NULL; 8566eea242fSPetr Mladek break; 8576eea242fSPetr Mladek } 8586eea242fSPetr Mladek 8596eea242fSPetr Mladek return pointer_string(buf, end, ptr, spec); 8606eea242fSPetr Mladek } 8616eea242fSPetr Mladek 8629073dac1SGeert Uytterhoeven static noinline_for_stack 8634b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 8644b6ccca7SAl Viro const char *fmt) 8654b6ccca7SAl Viro { 8664b6ccca7SAl Viro const char *array[4], *s; 8674b6ccca7SAl Viro const struct dentry *p; 8684b6ccca7SAl Viro int depth; 8694b6ccca7SAl Viro int i, n; 8704b6ccca7SAl Viro 8714b6ccca7SAl Viro switch (fmt[1]) { 8724b6ccca7SAl Viro case '2': case '3': case '4': 8734b6ccca7SAl Viro depth = fmt[1] - '0'; 8744b6ccca7SAl Viro break; 8754b6ccca7SAl Viro default: 8764b6ccca7SAl Viro depth = 1; 8774b6ccca7SAl Viro } 8784b6ccca7SAl Viro 8794b6ccca7SAl Viro rcu_read_lock(); 8804b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 8813e5903ebSPetr Mladek if (check_pointer(&buf, end, d, spec)) { 8823e5903ebSPetr Mladek rcu_read_unlock(); 8833e5903ebSPetr Mladek return buf; 8843e5903ebSPetr Mladek } 8853e5903ebSPetr Mladek 8866aa7de05SMark Rutland p = READ_ONCE(d->d_parent); 8876aa7de05SMark Rutland array[i] = READ_ONCE(d->d_name.name); 8884b6ccca7SAl Viro if (p == d) { 8894b6ccca7SAl Viro if (i) 8904b6ccca7SAl Viro array[i] = ""; 8914b6ccca7SAl Viro i++; 8924b6ccca7SAl Viro break; 8934b6ccca7SAl Viro } 8944b6ccca7SAl Viro } 8954b6ccca7SAl Viro s = array[--i]; 8964b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 8974b6ccca7SAl Viro char c = *s++; 8984b6ccca7SAl Viro if (!c) { 8994b6ccca7SAl Viro if (!i) 9004b6ccca7SAl Viro break; 9014b6ccca7SAl Viro c = '/'; 9024b6ccca7SAl Viro s = array[--i]; 9034b6ccca7SAl Viro } 9044b6ccca7SAl Viro if (buf < end) 9054b6ccca7SAl Viro *buf = c; 9064b6ccca7SAl Viro } 9074b6ccca7SAl Viro rcu_read_unlock(); 908cfccde04SRasmus Villemoes return widen_string(buf, n, end, spec); 9094b6ccca7SAl Viro } 9104b6ccca7SAl Viro 91136594b31SJia He static noinline_for_stack 91236594b31SJia He char *file_dentry_name(char *buf, char *end, const struct file *f, 91336594b31SJia He struct printf_spec spec, const char *fmt) 91436594b31SJia He { 91536594b31SJia He if (check_pointer(&buf, end, f, spec)) 91636594b31SJia He return buf; 91736594b31SJia He 91836594b31SJia He return dentry_name(buf, end, f->f_path.dentry, spec, fmt); 91936594b31SJia He } 9201031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 9211031bc58SDmitry Monakhov static noinline_for_stack 9221031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev, 9231031bc58SDmitry Monakhov struct printf_spec spec, const char *fmt) 9241031bc58SDmitry Monakhov { 9253e5903ebSPetr Mladek struct gendisk *hd; 9261031bc58SDmitry Monakhov 9273e5903ebSPetr Mladek if (check_pointer(&buf, end, bdev, spec)) 9283e5903ebSPetr Mladek return buf; 9293e5903ebSPetr Mladek 9303e5903ebSPetr Mladek hd = bdev->bd_disk; 9311031bc58SDmitry Monakhov buf = string(buf, end, hd->disk_name, spec); 9321031bc58SDmitry Monakhov if (bdev->bd_part->partno) { 9331031bc58SDmitry Monakhov if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { 9341031bc58SDmitry Monakhov if (buf < end) 9351031bc58SDmitry Monakhov *buf = 'p'; 9361031bc58SDmitry Monakhov buf++; 9371031bc58SDmitry Monakhov } 9381031bc58SDmitry Monakhov buf = number(buf, end, bdev->bd_part->partno, spec); 9391031bc58SDmitry Monakhov } 9401031bc58SDmitry Monakhov return buf; 9411031bc58SDmitry Monakhov } 9421031bc58SDmitry Monakhov #endif 9431031bc58SDmitry Monakhov 944cf3b429bSJoe Perches static noinline_for_stack 945cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 946b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 9470fe1ef24SLinus Torvalds { 948b0d33c2bSJoe Perches unsigned long value; 9490fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 9500fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 951b0d33c2bSJoe Perches #endif 952b0d33c2bSJoe Perches 953b0d33c2bSJoe Perches if (fmt[1] == 'R') 954b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 955b0d33c2bSJoe Perches value = (unsigned long)ptr; 956b0d33c2bSJoe Perches 957b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 958b0d33c2bSJoe Perches if (*fmt == 'B') 9590f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 9609af77064SSakari Ailus else if (*fmt != 's') 9610fe1ef24SLinus Torvalds sprint_symbol(sym, value); 9620c8b946eSFrederic Weisbecker else 9634796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 9647b9186f5SAndré Goddard Rosa 965d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 9660fe1ef24SLinus Torvalds #else 9673cab1e71SAndy Shevchenko return special_hex_number(buf, end, value, sizeof(void *)); 9680fe1ef24SLinus Torvalds #endif 9690fe1ef24SLinus Torvalds } 9700fe1ef24SLinus Torvalds 971abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = { 972abd4fe62SAndy Shevchenko .field_width = -1, 973abd4fe62SAndy Shevchenko .precision = -1, 974abd4fe62SAndy Shevchenko }; 975abd4fe62SAndy Shevchenko 97654433973SAndy Shevchenko static const struct printf_spec default_flag_spec = { 97754433973SAndy Shevchenko .base = 16, 97854433973SAndy Shevchenko .precision = -1, 97954433973SAndy Shevchenko .flags = SPECIAL | SMALL, 98054433973SAndy Shevchenko }; 98154433973SAndy Shevchenko 982ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = { 983ce0b4910SAndy Shevchenko .base = 10, 984ce0b4910SAndy Shevchenko .precision = -1, 985ce0b4910SAndy Shevchenko }; 986ce0b4910SAndy Shevchenko 9874d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = { 9884d42c447SAndy Shevchenko .base = 10, 9894d42c447SAndy Shevchenko .field_width = 2, 9904d42c447SAndy Shevchenko .precision = -1, 9914d42c447SAndy Shevchenko .flags = ZEROPAD, 9924d42c447SAndy Shevchenko }; 9934d42c447SAndy Shevchenko 9944d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = { 9954d42c447SAndy Shevchenko .base = 10, 9964d42c447SAndy Shevchenko .field_width = 4, 9974d42c447SAndy Shevchenko .precision = -1, 9984d42c447SAndy Shevchenko .flags = ZEROPAD, 9994d42c447SAndy Shevchenko }; 10004d42c447SAndy Shevchenko 1001cf3b429bSJoe Perches static noinline_for_stack 1002cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 1003fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 1004332d2e78SLinus Torvalds { 1005332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 100628405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 1007332d2e78SLinus Torvalds #endif 1008332d2e78SLinus Torvalds 1009332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 101028405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 1011332d2e78SLinus Torvalds #endif 10124da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 1013fef20d9cSFrederic Weisbecker .base = 16, 10144da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 1015fef20d9cSFrederic Weisbecker .precision = -1, 1016fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 1017fef20d9cSFrederic Weisbecker }; 10184da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 10194da0b66cSBjorn Helgaas .base = 16, 10204da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 10214da0b66cSBjorn Helgaas .precision = -1, 10224da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 10234da0b66cSBjorn Helgaas }; 10240f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 10250f4050c7SBjorn Helgaas .base = 16, 10260f4050c7SBjorn Helgaas .field_width = 2, 10270f4050c7SBjorn Helgaas .precision = -1, 10280f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 10290f4050c7SBjorn Helgaas }; 10304da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 1031fd95541eSBjorn Helgaas .field_width = -1, 1032fd95541eSBjorn Helgaas .precision = 10, 1033fd95541eSBjorn Helgaas .flags = LEFT, 1034fd95541eSBjorn Helgaas }; 1035c7dabef8SBjorn Helgaas 1036c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 1037c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 1038c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 1039c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 10409d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 1041c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 1042c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 1043c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 1044c7dabef8SBjorn Helgaas 1045332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 1046c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 10474da0b66cSBjorn Helgaas const struct printf_spec *specp; 1048332d2e78SLinus Torvalds 10493e5903ebSPetr Mladek if (check_pointer(&buf, end, res, spec)) 10503e5903ebSPetr Mladek return buf; 10513e5903ebSPetr Mladek 1052332d2e78SLinus Torvalds *p++ = '['; 10534da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 1054d529ac41SPetr Mladek p = string_nocheck(p, pend, "io ", str_spec); 10554da0b66cSBjorn Helgaas specp = &io_spec; 10564da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 1057d529ac41SPetr Mladek p = string_nocheck(p, pend, "mem ", str_spec); 10584da0b66cSBjorn Helgaas specp = &mem_spec; 10594da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 1060d529ac41SPetr Mladek p = string_nocheck(p, pend, "irq ", str_spec); 1061ce0b4910SAndy Shevchenko specp = &default_dec_spec; 10624da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 1063d529ac41SPetr Mladek p = string_nocheck(p, pend, "dma ", str_spec); 1064ce0b4910SAndy Shevchenko specp = &default_dec_spec; 10650f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 1066d529ac41SPetr Mladek p = string_nocheck(p, pend, "bus ", str_spec); 10670f4050c7SBjorn Helgaas specp = &bus_spec; 10684da0b66cSBjorn Helgaas } else { 1069d529ac41SPetr Mladek p = string_nocheck(p, pend, "??? ", str_spec); 10704da0b66cSBjorn Helgaas specp = &mem_spec; 1071c7dabef8SBjorn Helgaas decode = 0; 1072fd95541eSBjorn Helgaas } 1073d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 1074d529ac41SPetr Mladek p = string_nocheck(p, pend, "size ", str_spec); 1075d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 1076d19cb803SBjorn Helgaas } else { 10774da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 1078c91d3376SBjorn Helgaas if (res->start != res->end) { 1079332d2e78SLinus Torvalds *p++ = '-'; 10804da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 1081c91d3376SBjorn Helgaas } 1082d19cb803SBjorn Helgaas } 1083c7dabef8SBjorn Helgaas if (decode) { 1084fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 1085d529ac41SPetr Mladek p = string_nocheck(p, pend, " 64bit", str_spec); 1086fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 1087d529ac41SPetr Mladek p = string_nocheck(p, pend, " pref", str_spec); 10889d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 1089d529ac41SPetr Mladek p = string_nocheck(p, pend, " window", str_spec); 1090fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 1091d529ac41SPetr Mladek p = string_nocheck(p, pend, " disabled", str_spec); 1092c7dabef8SBjorn Helgaas } else { 1093d529ac41SPetr Mladek p = string_nocheck(p, pend, " flags ", str_spec); 109454433973SAndy Shevchenko p = number(p, pend, res->flags, default_flag_spec); 1095fd95541eSBjorn Helgaas } 1096332d2e78SLinus Torvalds *p++ = ']'; 1097c7dabef8SBjorn Helgaas *p = '\0'; 1098332d2e78SLinus Torvalds 1099d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 1100332d2e78SLinus Torvalds } 1101332d2e78SLinus Torvalds 1102cf3b429bSJoe Perches static noinline_for_stack 110331550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 110431550a16SAndy Shevchenko const char *fmt) 110531550a16SAndy Shevchenko { 1106360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 110731550a16SAndy Shevchenko negative value, fallback to the default */ 110831550a16SAndy Shevchenko char separator; 110931550a16SAndy Shevchenko 111031550a16SAndy Shevchenko if (spec.field_width == 0) 111131550a16SAndy Shevchenko /* nothing to print */ 111231550a16SAndy Shevchenko return buf; 111331550a16SAndy Shevchenko 11143e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 11153e5903ebSPetr Mladek return buf; 111631550a16SAndy Shevchenko 111731550a16SAndy Shevchenko switch (fmt[1]) { 111831550a16SAndy Shevchenko case 'C': 111931550a16SAndy Shevchenko separator = ':'; 112031550a16SAndy Shevchenko break; 112131550a16SAndy Shevchenko case 'D': 112231550a16SAndy Shevchenko separator = '-'; 112331550a16SAndy Shevchenko break; 112431550a16SAndy Shevchenko case 'N': 112531550a16SAndy Shevchenko separator = 0; 112631550a16SAndy Shevchenko break; 112731550a16SAndy Shevchenko default: 112831550a16SAndy Shevchenko separator = ' '; 112931550a16SAndy Shevchenko break; 113031550a16SAndy Shevchenko } 113131550a16SAndy Shevchenko 113231550a16SAndy Shevchenko if (spec.field_width > 0) 113331550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 113431550a16SAndy Shevchenko 11359c98f235SRasmus Villemoes for (i = 0; i < len; ++i) { 11369c98f235SRasmus Villemoes if (buf < end) 11379c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]); 11389c98f235SRasmus Villemoes ++buf; 11399c98f235SRasmus Villemoes if (buf < end) 11409c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]); 11419c98f235SRasmus Villemoes ++buf; 114231550a16SAndy Shevchenko 11439c98f235SRasmus Villemoes if (separator && i != len - 1) { 11449c98f235SRasmus Villemoes if (buf < end) 11459c98f235SRasmus Villemoes *buf = separator; 11469c98f235SRasmus Villemoes ++buf; 11479c98f235SRasmus Villemoes } 114831550a16SAndy Shevchenko } 114931550a16SAndy Shevchenko 115031550a16SAndy Shevchenko return buf; 115131550a16SAndy Shevchenko } 115231550a16SAndy Shevchenko 115331550a16SAndy Shevchenko static noinline_for_stack 1154dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap, 1155dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1156dbc760bcSTejun Heo { 1157dbc760bcSTejun Heo const int CHUNKSZ = 32; 1158dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1159dbc760bcSTejun Heo int i, chunksz; 1160dbc760bcSTejun Heo bool first = true; 1161dbc760bcSTejun Heo 11623e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec)) 11633e5903ebSPetr Mladek return buf; 11643e5903ebSPetr Mladek 1165dbc760bcSTejun Heo /* reused to print numbers */ 1166dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 1167dbc760bcSTejun Heo 1168dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1); 1169dbc760bcSTejun Heo if (chunksz == 0) 1170dbc760bcSTejun Heo chunksz = CHUNKSZ; 1171dbc760bcSTejun Heo 1172dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 1173dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) { 1174dbc760bcSTejun Heo u32 chunkmask, val; 1175dbc760bcSTejun Heo int word, bit; 1176dbc760bcSTejun Heo 1177dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1); 1178dbc760bcSTejun Heo word = i / BITS_PER_LONG; 1179dbc760bcSTejun Heo bit = i % BITS_PER_LONG; 1180dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask; 1181dbc760bcSTejun Heo 1182dbc760bcSTejun Heo if (!first) { 1183dbc760bcSTejun Heo if (buf < end) 1184dbc760bcSTejun Heo *buf = ','; 1185dbc760bcSTejun Heo buf++; 1186dbc760bcSTejun Heo } 1187dbc760bcSTejun Heo first = false; 1188dbc760bcSTejun Heo 1189dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4); 1190dbc760bcSTejun Heo buf = number(buf, end, val, spec); 1191dbc760bcSTejun Heo 1192dbc760bcSTejun Heo chunksz = CHUNKSZ; 1193dbc760bcSTejun Heo } 1194dbc760bcSTejun Heo return buf; 1195dbc760bcSTejun Heo } 1196dbc760bcSTejun Heo 1197dbc760bcSTejun Heo static noinline_for_stack 1198dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, 1199dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1200dbc760bcSTejun Heo { 1201dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1202dbc760bcSTejun Heo /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 1203dbc760bcSTejun Heo int cur, rbot, rtop; 1204dbc760bcSTejun Heo bool first = true; 1205dbc760bcSTejun Heo 12063e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec)) 12073e5903ebSPetr Mladek return buf; 12083e5903ebSPetr Mladek 1209dbc760bcSTejun Heo rbot = cur = find_first_bit(bitmap, nr_bits); 1210dbc760bcSTejun Heo while (cur < nr_bits) { 1211dbc760bcSTejun Heo rtop = cur; 1212dbc760bcSTejun Heo cur = find_next_bit(bitmap, nr_bits, cur + 1); 1213dbc760bcSTejun Heo if (cur < nr_bits && cur <= rtop + 1) 1214dbc760bcSTejun Heo continue; 1215dbc760bcSTejun Heo 1216dbc760bcSTejun Heo if (!first) { 1217dbc760bcSTejun Heo if (buf < end) 1218dbc760bcSTejun Heo *buf = ','; 1219dbc760bcSTejun Heo buf++; 1220dbc760bcSTejun Heo } 1221dbc760bcSTejun Heo first = false; 1222dbc760bcSTejun Heo 1223ce0b4910SAndy Shevchenko buf = number(buf, end, rbot, default_dec_spec); 1224dbc760bcSTejun Heo if (rbot < rtop) { 1225dbc760bcSTejun Heo if (buf < end) 1226dbc760bcSTejun Heo *buf = '-'; 1227dbc760bcSTejun Heo buf++; 1228dbc760bcSTejun Heo 1229ce0b4910SAndy Shevchenko buf = number(buf, end, rtop, default_dec_spec); 1230dbc760bcSTejun Heo } 1231dbc760bcSTejun Heo 1232dbc760bcSTejun Heo rbot = cur; 1233dbc760bcSTejun Heo } 1234dbc760bcSTejun Heo return buf; 1235dbc760bcSTejun Heo } 1236dbc760bcSTejun Heo 1237dbc760bcSTejun Heo static noinline_for_stack 1238cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 12398a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 1240dd45c9cfSHarvey Harrison { 12418a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 1242dd45c9cfSHarvey Harrison char *p = mac_addr; 1243dd45c9cfSHarvey Harrison int i; 1244bc7259a2SJoe Perches char separator; 124576597ff9SAndrei Emeltchenko bool reversed = false; 1246bc7259a2SJoe Perches 12473e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 12483e5903ebSPetr Mladek return buf; 12493e5903ebSPetr Mladek 125076597ff9SAndrei Emeltchenko switch (fmt[1]) { 125176597ff9SAndrei Emeltchenko case 'F': 1252bc7259a2SJoe Perches separator = '-'; 125376597ff9SAndrei Emeltchenko break; 125476597ff9SAndrei Emeltchenko 125576597ff9SAndrei Emeltchenko case 'R': 125676597ff9SAndrei Emeltchenko reversed = true; 125776597ff9SAndrei Emeltchenko /* fall through */ 125876597ff9SAndrei Emeltchenko 125976597ff9SAndrei Emeltchenko default: 1260bc7259a2SJoe Perches separator = ':'; 126176597ff9SAndrei Emeltchenko break; 1262bc7259a2SJoe Perches } 1263dd45c9cfSHarvey Harrison 1264dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 126576597ff9SAndrei Emeltchenko if (reversed) 126676597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 126776597ff9SAndrei Emeltchenko else 126855036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 126976597ff9SAndrei Emeltchenko 12708a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 1271bc7259a2SJoe Perches *p++ = separator; 1272dd45c9cfSHarvey Harrison } 1273dd45c9cfSHarvey Harrison *p = '\0'; 1274dd45c9cfSHarvey Harrison 1275d529ac41SPetr Mladek return string_nocheck(buf, end, mac_addr, spec); 1276dd45c9cfSHarvey Harrison } 1277dd45c9cfSHarvey Harrison 1278cf3b429bSJoe Perches static noinline_for_stack 1279cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 1280689afa7dSHarvey Harrison { 1281689afa7dSHarvey Harrison int i; 12820159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 12830159f24eSJoe Perches int index; 12840159f24eSJoe Perches int step; 1285689afa7dSHarvey Harrison 12860159f24eSJoe Perches switch (fmt[2]) { 12870159f24eSJoe Perches case 'h': 12880159f24eSJoe Perches #ifdef __BIG_ENDIAN 12890159f24eSJoe Perches index = 0; 12900159f24eSJoe Perches step = 1; 12910159f24eSJoe Perches #else 12920159f24eSJoe Perches index = 3; 12930159f24eSJoe Perches step = -1; 12940159f24eSJoe Perches #endif 12950159f24eSJoe Perches break; 12960159f24eSJoe Perches case 'l': 12970159f24eSJoe Perches index = 3; 12980159f24eSJoe Perches step = -1; 12990159f24eSJoe Perches break; 13000159f24eSJoe Perches case 'n': 13010159f24eSJoe Perches case 'b': 13020159f24eSJoe Perches default: 13030159f24eSJoe Perches index = 0; 13040159f24eSJoe Perches step = 1; 13050159f24eSJoe Perches break; 13060159f24eSJoe Perches } 13078a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 13087c43d9a3SRasmus Villemoes char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 1309133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 13108a27f7c9SJoe Perches if (leading_zeros) { 13118a27f7c9SJoe Perches if (digits < 3) 13128a27f7c9SJoe Perches *p++ = '0'; 13138a27f7c9SJoe Perches if (digits < 2) 13148a27f7c9SJoe Perches *p++ = '0'; 13158a27f7c9SJoe Perches } 13168a27f7c9SJoe Perches /* reverse the digits in the quad */ 13178a27f7c9SJoe Perches while (digits--) 13188a27f7c9SJoe Perches *p++ = temp[digits]; 13198a27f7c9SJoe Perches if (i < 3) 13208a27f7c9SJoe Perches *p++ = '.'; 13210159f24eSJoe Perches index += step; 13228a27f7c9SJoe Perches } 13238a27f7c9SJoe Perches *p = '\0'; 13247b9186f5SAndré Goddard Rosa 13258a27f7c9SJoe Perches return p; 13268a27f7c9SJoe Perches } 13278a27f7c9SJoe Perches 1328cf3b429bSJoe Perches static noinline_for_stack 1329cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 13308a27f7c9SJoe Perches { 13317b9186f5SAndré Goddard Rosa int i, j, range; 13328a27f7c9SJoe Perches unsigned char zerolength[8]; 13338a27f7c9SJoe Perches int longest = 1; 13348a27f7c9SJoe Perches int colonpos = -1; 13358a27f7c9SJoe Perches u16 word; 13367b9186f5SAndré Goddard Rosa u8 hi, lo; 13378a27f7c9SJoe Perches bool needcolon = false; 1338eb78cd26SJoe Perches bool useIPv4; 1339eb78cd26SJoe Perches struct in6_addr in6; 1340eb78cd26SJoe Perches 1341eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 1342eb78cd26SJoe Perches 1343eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 13448a27f7c9SJoe Perches 13458a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 13468a27f7c9SJoe Perches 13478a27f7c9SJoe Perches if (useIPv4) 13488a27f7c9SJoe Perches range = 6; 13498a27f7c9SJoe Perches else 13508a27f7c9SJoe Perches range = 8; 13518a27f7c9SJoe Perches 13528a27f7c9SJoe Perches /* find position of longest 0 run */ 13538a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13548a27f7c9SJoe Perches for (j = i; j < range; j++) { 1355eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 13568a27f7c9SJoe Perches break; 13578a27f7c9SJoe Perches zerolength[i]++; 13588a27f7c9SJoe Perches } 13598a27f7c9SJoe Perches } 13608a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13618a27f7c9SJoe Perches if (zerolength[i] > longest) { 13628a27f7c9SJoe Perches longest = zerolength[i]; 13638a27f7c9SJoe Perches colonpos = i; 13648a27f7c9SJoe Perches } 13658a27f7c9SJoe Perches } 136629cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 136729cf519eSJoe Perches colonpos = -1; 13688a27f7c9SJoe Perches 13698a27f7c9SJoe Perches /* emit address */ 13708a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13718a27f7c9SJoe Perches if (i == colonpos) { 13728a27f7c9SJoe Perches if (needcolon || i == 0) 13738a27f7c9SJoe Perches *p++ = ':'; 13748a27f7c9SJoe Perches *p++ = ':'; 13758a27f7c9SJoe Perches needcolon = false; 13768a27f7c9SJoe Perches i += longest - 1; 13778a27f7c9SJoe Perches continue; 13788a27f7c9SJoe Perches } 13798a27f7c9SJoe Perches if (needcolon) { 13808a27f7c9SJoe Perches *p++ = ':'; 13818a27f7c9SJoe Perches needcolon = false; 13828a27f7c9SJoe Perches } 13838a27f7c9SJoe Perches /* hex u16 without leading 0s */ 1384eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 13858a27f7c9SJoe Perches hi = word >> 8; 13868a27f7c9SJoe Perches lo = word & 0xff; 13878a27f7c9SJoe Perches if (hi) { 13888a27f7c9SJoe Perches if (hi > 0x0f) 138955036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 13908a27f7c9SJoe Perches else 13918a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 139255036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 13938a27f7c9SJoe Perches } 1394b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 139555036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 13968a27f7c9SJoe Perches else 13978a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 13988a27f7c9SJoe Perches needcolon = true; 13998a27f7c9SJoe Perches } 14008a27f7c9SJoe Perches 14018a27f7c9SJoe Perches if (useIPv4) { 14028a27f7c9SJoe Perches if (needcolon) 14038a27f7c9SJoe Perches *p++ = ':'; 14040159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 14058a27f7c9SJoe Perches } 14068a27f7c9SJoe Perches *p = '\0'; 14077b9186f5SAndré Goddard Rosa 14088a27f7c9SJoe Perches return p; 14098a27f7c9SJoe Perches } 14108a27f7c9SJoe Perches 1411cf3b429bSJoe Perches static noinline_for_stack 1412cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 14138a27f7c9SJoe Perches { 14148a27f7c9SJoe Perches int i; 14157b9186f5SAndré Goddard Rosa 1416689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 141755036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 141855036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 14198a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 1420689afa7dSHarvey Harrison *p++ = ':'; 1421689afa7dSHarvey Harrison } 1422689afa7dSHarvey Harrison *p = '\0'; 14237b9186f5SAndré Goddard Rosa 14248a27f7c9SJoe Perches return p; 14258a27f7c9SJoe Perches } 14268a27f7c9SJoe Perches 1427cf3b429bSJoe Perches static noinline_for_stack 1428cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 14298a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 14308a27f7c9SJoe Perches { 14318a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 14328a27f7c9SJoe Perches 14338a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 1434eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 14358a27f7c9SJoe Perches else 1436eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 1437689afa7dSHarvey Harrison 1438d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 1439689afa7dSHarvey Harrison } 1440689afa7dSHarvey Harrison 1441cf3b429bSJoe Perches static noinline_for_stack 1442cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 14438a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 14444aa99606SHarvey Harrison { 14458a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 14464aa99606SHarvey Harrison 14470159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 14484aa99606SHarvey Harrison 1449d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 14504aa99606SHarvey Harrison } 14514aa99606SHarvey Harrison 1452cf3b429bSJoe Perches static noinline_for_stack 145310679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 145410679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 145510679643SDaniel Borkmann { 145610679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 145710679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 145810679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 145910679643SDaniel Borkmann sizeof("%1234567890")]; 146010679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 146110679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 146210679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 146310679643SDaniel Borkmann u8 off = 0; 146410679643SDaniel Borkmann 146510679643SDaniel Borkmann fmt++; 146610679643SDaniel Borkmann while (isalpha(*++fmt)) { 146710679643SDaniel Borkmann switch (*fmt) { 146810679643SDaniel Borkmann case 'p': 146910679643SDaniel Borkmann have_p = true; 147010679643SDaniel Borkmann break; 147110679643SDaniel Borkmann case 'f': 147210679643SDaniel Borkmann have_f = true; 147310679643SDaniel Borkmann break; 147410679643SDaniel Borkmann case 's': 147510679643SDaniel Borkmann have_s = true; 147610679643SDaniel Borkmann break; 147710679643SDaniel Borkmann case 'c': 147810679643SDaniel Borkmann have_c = true; 147910679643SDaniel Borkmann break; 148010679643SDaniel Borkmann } 148110679643SDaniel Borkmann } 148210679643SDaniel Borkmann 148310679643SDaniel Borkmann if (have_p || have_s || have_f) { 148410679643SDaniel Borkmann *p = '['; 148510679643SDaniel Borkmann off = 1; 148610679643SDaniel Borkmann } 148710679643SDaniel Borkmann 148810679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 148910679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 149010679643SDaniel Borkmann else 149110679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 149210679643SDaniel Borkmann 149310679643SDaniel Borkmann if (have_p || have_s || have_f) 149410679643SDaniel Borkmann *p++ = ']'; 149510679643SDaniel Borkmann 149610679643SDaniel Borkmann if (have_p) { 149710679643SDaniel Borkmann *p++ = ':'; 149810679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 149910679643SDaniel Borkmann } 150010679643SDaniel Borkmann if (have_f) { 150110679643SDaniel Borkmann *p++ = '/'; 150210679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 150310679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 150410679643SDaniel Borkmann } 150510679643SDaniel Borkmann if (have_s) { 150610679643SDaniel Borkmann *p++ = '%'; 150710679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 150810679643SDaniel Borkmann } 150910679643SDaniel Borkmann *p = '\0'; 151010679643SDaniel Borkmann 1511d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 151210679643SDaniel Borkmann } 151310679643SDaniel Borkmann 151410679643SDaniel Borkmann static noinline_for_stack 151510679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 151610679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 151710679643SDaniel Borkmann { 151810679643SDaniel Borkmann bool have_p = false; 151910679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 152010679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 152110679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 152210679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 152310679643SDaniel Borkmann 152410679643SDaniel Borkmann fmt++; 152510679643SDaniel Borkmann while (isalpha(*++fmt)) { 152610679643SDaniel Borkmann switch (*fmt) { 152710679643SDaniel Borkmann case 'p': 152810679643SDaniel Borkmann have_p = true; 152910679643SDaniel Borkmann break; 153010679643SDaniel Borkmann case 'h': 153110679643SDaniel Borkmann case 'l': 153210679643SDaniel Borkmann case 'n': 153310679643SDaniel Borkmann case 'b': 153410679643SDaniel Borkmann fmt4[2] = *fmt; 153510679643SDaniel Borkmann break; 153610679643SDaniel Borkmann } 153710679643SDaniel Borkmann } 153810679643SDaniel Borkmann 153910679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 154010679643SDaniel Borkmann if (have_p) { 154110679643SDaniel Borkmann *p++ = ':'; 154210679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 154310679643SDaniel Borkmann } 154410679643SDaniel Borkmann *p = '\0'; 154510679643SDaniel Borkmann 1546d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 154710679643SDaniel Borkmann } 154810679643SDaniel Borkmann 154910679643SDaniel Borkmann static noinline_for_stack 1550f00cc102SPetr Mladek char *ip_addr_string(char *buf, char *end, const void *ptr, 1551f00cc102SPetr Mladek struct printf_spec spec, const char *fmt) 1552f00cc102SPetr Mladek { 15530b74d4d7SPetr Mladek char *err_fmt_msg; 15540b74d4d7SPetr Mladek 15553e5903ebSPetr Mladek if (check_pointer(&buf, end, ptr, spec)) 15563e5903ebSPetr Mladek return buf; 15573e5903ebSPetr Mladek 1558f00cc102SPetr Mladek switch (fmt[1]) { 1559f00cc102SPetr Mladek case '6': 1560f00cc102SPetr Mladek return ip6_addr_string(buf, end, ptr, spec, fmt); 1561f00cc102SPetr Mladek case '4': 1562f00cc102SPetr Mladek return ip4_addr_string(buf, end, ptr, spec, fmt); 1563f00cc102SPetr Mladek case 'S': { 1564f00cc102SPetr Mladek const union { 1565f00cc102SPetr Mladek struct sockaddr raw; 1566f00cc102SPetr Mladek struct sockaddr_in v4; 1567f00cc102SPetr Mladek struct sockaddr_in6 v6; 1568f00cc102SPetr Mladek } *sa = ptr; 1569f00cc102SPetr Mladek 1570f00cc102SPetr Mladek switch (sa->raw.sa_family) { 1571f00cc102SPetr Mladek case AF_INET: 1572f00cc102SPetr Mladek return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 1573f00cc102SPetr Mladek case AF_INET6: 1574f00cc102SPetr Mladek return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 1575f00cc102SPetr Mladek default: 1576c8c3b584SPetr Mladek return error_string(buf, end, "(einval)", spec); 1577f00cc102SPetr Mladek }} 1578f00cc102SPetr Mladek } 1579f00cc102SPetr Mladek 15800b74d4d7SPetr Mladek err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)"; 1581c8c3b584SPetr Mladek return error_string(buf, end, err_fmt_msg, spec); 1582f00cc102SPetr Mladek } 1583f00cc102SPetr Mladek 1584f00cc102SPetr Mladek static noinline_for_stack 158571dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 158671dca95dSAndy Shevchenko const char *fmt) 158771dca95dSAndy Shevchenko { 158871dca95dSAndy Shevchenko bool found = true; 158971dca95dSAndy Shevchenko int count = 1; 159071dca95dSAndy Shevchenko unsigned int flags = 0; 159171dca95dSAndy Shevchenko int len; 159271dca95dSAndy Shevchenko 159371dca95dSAndy Shevchenko if (spec.field_width == 0) 159471dca95dSAndy Shevchenko return buf; /* nothing to print */ 159571dca95dSAndy Shevchenko 15963e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 15973e5903ebSPetr Mladek return buf; 159871dca95dSAndy Shevchenko 159971dca95dSAndy Shevchenko do { 160071dca95dSAndy Shevchenko switch (fmt[count++]) { 160171dca95dSAndy Shevchenko case 'a': 160271dca95dSAndy Shevchenko flags |= ESCAPE_ANY; 160371dca95dSAndy Shevchenko break; 160471dca95dSAndy Shevchenko case 'c': 160571dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL; 160671dca95dSAndy Shevchenko break; 160771dca95dSAndy Shevchenko case 'h': 160871dca95dSAndy Shevchenko flags |= ESCAPE_HEX; 160971dca95dSAndy Shevchenko break; 161071dca95dSAndy Shevchenko case 'n': 161171dca95dSAndy Shevchenko flags |= ESCAPE_NULL; 161271dca95dSAndy Shevchenko break; 161371dca95dSAndy Shevchenko case 'o': 161471dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL; 161571dca95dSAndy Shevchenko break; 161671dca95dSAndy Shevchenko case 'p': 161771dca95dSAndy Shevchenko flags |= ESCAPE_NP; 161871dca95dSAndy Shevchenko break; 161971dca95dSAndy Shevchenko case 's': 162071dca95dSAndy Shevchenko flags |= ESCAPE_SPACE; 162171dca95dSAndy Shevchenko break; 162271dca95dSAndy Shevchenko default: 162371dca95dSAndy Shevchenko found = false; 162471dca95dSAndy Shevchenko break; 162571dca95dSAndy Shevchenko } 162671dca95dSAndy Shevchenko } while (found); 162771dca95dSAndy Shevchenko 162871dca95dSAndy Shevchenko if (!flags) 162971dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP; 163071dca95dSAndy Shevchenko 163171dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width; 163271dca95dSAndy Shevchenko 163341416f23SRasmus Villemoes /* 163441416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to 163541416f23SRasmus Villemoes * the given buffer, and returns the total size of the output 163641416f23SRasmus Villemoes * had the buffer been big enough. 163741416f23SRasmus Villemoes */ 163841416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 163971dca95dSAndy Shevchenko 164071dca95dSAndy Shevchenko return buf; 164171dca95dSAndy Shevchenko } 164271dca95dSAndy Shevchenko 16433e5903ebSPetr Mladek static char *va_format(char *buf, char *end, struct va_format *va_fmt, 16443e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 164545c3e93dSPetr Mladek { 164645c3e93dSPetr Mladek va_list va; 164745c3e93dSPetr Mladek 16483e5903ebSPetr Mladek if (check_pointer(&buf, end, va_fmt, spec)) 16493e5903ebSPetr Mladek return buf; 16503e5903ebSPetr Mladek 165145c3e93dSPetr Mladek va_copy(va, *va_fmt->va); 165245c3e93dSPetr Mladek buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va); 165345c3e93dSPetr Mladek va_end(va); 165445c3e93dSPetr Mladek 165545c3e93dSPetr Mladek return buf; 165645c3e93dSPetr Mladek } 165745c3e93dSPetr Mladek 165871dca95dSAndy Shevchenko static noinline_for_stack 1659cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 16609ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 16619ac6e44eSJoe Perches { 16622b1b0d66SAndy Shevchenko char uuid[UUID_STRING_LEN + 1]; 16639ac6e44eSJoe Perches char *p = uuid; 16649ac6e44eSJoe Perches int i; 1665f9727a17SChristoph Hellwig const u8 *index = uuid_index; 16669ac6e44eSJoe Perches bool uc = false; 16679ac6e44eSJoe Perches 16683e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 16693e5903ebSPetr Mladek return buf; 16703e5903ebSPetr Mladek 16719ac6e44eSJoe Perches switch (*(++fmt)) { 16729ac6e44eSJoe Perches case 'L': 16739ac6e44eSJoe Perches uc = true; /* fall-through */ 16749ac6e44eSJoe Perches case 'l': 1675f9727a17SChristoph Hellwig index = guid_index; 16769ac6e44eSJoe Perches break; 16779ac6e44eSJoe Perches case 'B': 16789ac6e44eSJoe Perches uc = true; 16799ac6e44eSJoe Perches break; 16809ac6e44eSJoe Perches } 16819ac6e44eSJoe Perches 16829ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 1683aa4ea1c3SAndy Shevchenko if (uc) 1684aa4ea1c3SAndy Shevchenko p = hex_byte_pack_upper(p, addr[index[i]]); 1685aa4ea1c3SAndy Shevchenko else 168655036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 16879ac6e44eSJoe Perches switch (i) { 16889ac6e44eSJoe Perches case 3: 16899ac6e44eSJoe Perches case 5: 16909ac6e44eSJoe Perches case 7: 16919ac6e44eSJoe Perches case 9: 16929ac6e44eSJoe Perches *p++ = '-'; 16939ac6e44eSJoe Perches break; 16949ac6e44eSJoe Perches } 16959ac6e44eSJoe Perches } 16969ac6e44eSJoe Perches 16979ac6e44eSJoe Perches *p = 0; 16989ac6e44eSJoe Perches 1699d529ac41SPetr Mladek return string_nocheck(buf, end, uuid, spec); 17009ac6e44eSJoe Perches } 17019ac6e44eSJoe Perches 17025b17aecfSAndy Shevchenko static noinline_for_stack 1703431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr, 1704431bca24SGeert Uytterhoeven struct printf_spec spec, const char *fmt) 1705c8f44affSMichał Mirosław { 17065b17aecfSAndy Shevchenko unsigned long long num; 17075b17aecfSAndy Shevchenko int size; 17085b17aecfSAndy Shevchenko 17093e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 17103e5903ebSPetr Mladek return buf; 17113e5903ebSPetr Mladek 17125b17aecfSAndy Shevchenko switch (fmt[1]) { 17135b17aecfSAndy Shevchenko case 'F': 17145b17aecfSAndy Shevchenko num = *(const netdev_features_t *)addr; 17155b17aecfSAndy Shevchenko size = sizeof(netdev_features_t); 17165b17aecfSAndy Shevchenko break; 17175b17aecfSAndy Shevchenko default: 1718c8c3b584SPetr Mladek return error_string(buf, end, "(%pN?)", spec); 17195b17aecfSAndy Shevchenko } 1720c8f44affSMichał Mirosław 17213cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1722c8f44affSMichał Mirosław } 1723c8f44affSMichał Mirosław 1724aaf07621SJoe Perches static noinline_for_stack 17253e5903ebSPetr Mladek char *address_val(char *buf, char *end, const void *addr, 17263e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 1727aaf07621SJoe Perches { 1728aaf07621SJoe Perches unsigned long long num; 17293cab1e71SAndy Shevchenko int size; 1730aaf07621SJoe Perches 17313e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 17323e5903ebSPetr Mladek return buf; 17333e5903ebSPetr Mladek 1734aaf07621SJoe Perches switch (fmt[1]) { 1735aaf07621SJoe Perches case 'd': 1736aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 17373cab1e71SAndy Shevchenko size = sizeof(dma_addr_t); 1738aaf07621SJoe Perches break; 1739aaf07621SJoe Perches case 'p': 1740aaf07621SJoe Perches default: 1741aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 17423cab1e71SAndy Shevchenko size = sizeof(phys_addr_t); 1743aaf07621SJoe Perches break; 1744aaf07621SJoe Perches } 1745aaf07621SJoe Perches 17463cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1747aaf07621SJoe Perches } 1748aaf07621SJoe Perches 1749900cca29SGeert Uytterhoeven static noinline_for_stack 17504d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) 17514d42c447SAndy Shevchenko { 17524d42c447SAndy Shevchenko int year = tm->tm_year + (r ? 0 : 1900); 17534d42c447SAndy Shevchenko int mon = tm->tm_mon + (r ? 0 : 1); 17544d42c447SAndy Shevchenko 17554d42c447SAndy Shevchenko buf = number(buf, end, year, default_dec04_spec); 17564d42c447SAndy Shevchenko if (buf < end) 17574d42c447SAndy Shevchenko *buf = '-'; 17584d42c447SAndy Shevchenko buf++; 17594d42c447SAndy Shevchenko 17604d42c447SAndy Shevchenko buf = number(buf, end, mon, default_dec02_spec); 17614d42c447SAndy Shevchenko if (buf < end) 17624d42c447SAndy Shevchenko *buf = '-'; 17634d42c447SAndy Shevchenko buf++; 17644d42c447SAndy Shevchenko 17654d42c447SAndy Shevchenko return number(buf, end, tm->tm_mday, default_dec02_spec); 17664d42c447SAndy Shevchenko } 17674d42c447SAndy Shevchenko 17684d42c447SAndy Shevchenko static noinline_for_stack 17694d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) 17704d42c447SAndy Shevchenko { 17714d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_hour, default_dec02_spec); 17724d42c447SAndy Shevchenko if (buf < end) 17734d42c447SAndy Shevchenko *buf = ':'; 17744d42c447SAndy Shevchenko buf++; 17754d42c447SAndy Shevchenko 17764d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_min, default_dec02_spec); 17774d42c447SAndy Shevchenko if (buf < end) 17784d42c447SAndy Shevchenko *buf = ':'; 17794d42c447SAndy Shevchenko buf++; 17804d42c447SAndy Shevchenko 17814d42c447SAndy Shevchenko return number(buf, end, tm->tm_sec, default_dec02_spec); 17824d42c447SAndy Shevchenko } 17834d42c447SAndy Shevchenko 17844d42c447SAndy Shevchenko static noinline_for_stack 17853e5903ebSPetr Mladek char *rtc_str(char *buf, char *end, const struct rtc_time *tm, 17863e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 17874d42c447SAndy Shevchenko { 17884d42c447SAndy Shevchenko bool have_t = true, have_d = true; 17894d42c447SAndy Shevchenko bool raw = false; 17904d42c447SAndy Shevchenko int count = 2; 17914d42c447SAndy Shevchenko 17923e5903ebSPetr Mladek if (check_pointer(&buf, end, tm, spec)) 17933e5903ebSPetr Mladek return buf; 17943e5903ebSPetr Mladek 17954d42c447SAndy Shevchenko switch (fmt[count]) { 17964d42c447SAndy Shevchenko case 'd': 17974d42c447SAndy Shevchenko have_t = false; 17984d42c447SAndy Shevchenko count++; 17994d42c447SAndy Shevchenko break; 18004d42c447SAndy Shevchenko case 't': 18014d42c447SAndy Shevchenko have_d = false; 18024d42c447SAndy Shevchenko count++; 18034d42c447SAndy Shevchenko break; 18044d42c447SAndy Shevchenko } 18054d42c447SAndy Shevchenko 18064d42c447SAndy Shevchenko raw = fmt[count] == 'r'; 18074d42c447SAndy Shevchenko 18084d42c447SAndy Shevchenko if (have_d) 18094d42c447SAndy Shevchenko buf = date_str(buf, end, tm, raw); 18104d42c447SAndy Shevchenko if (have_d && have_t) { 18114d42c447SAndy Shevchenko /* Respect ISO 8601 */ 18124d42c447SAndy Shevchenko if (buf < end) 18134d42c447SAndy Shevchenko *buf = 'T'; 18144d42c447SAndy Shevchenko buf++; 18154d42c447SAndy Shevchenko } 18164d42c447SAndy Shevchenko if (have_t) 18174d42c447SAndy Shevchenko buf = time_str(buf, end, tm, raw); 18184d42c447SAndy Shevchenko 18194d42c447SAndy Shevchenko return buf; 18204d42c447SAndy Shevchenko } 18214d42c447SAndy Shevchenko 18224d42c447SAndy Shevchenko static noinline_for_stack 18234d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, 18244d42c447SAndy Shevchenko const char *fmt) 18254d42c447SAndy Shevchenko { 18264d42c447SAndy Shevchenko switch (fmt[1]) { 18274d42c447SAndy Shevchenko case 'R': 18283e5903ebSPetr Mladek return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); 18294d42c447SAndy Shevchenko default: 1830c8c3b584SPetr Mladek return error_string(buf, end, "(%ptR?)", spec); 18314d42c447SAndy Shevchenko } 18324d42c447SAndy Shevchenko } 18334d42c447SAndy Shevchenko 18344d42c447SAndy Shevchenko static noinline_for_stack 1835900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1836900cca29SGeert Uytterhoeven const char *fmt) 1837900cca29SGeert Uytterhoeven { 18380b74d4d7SPetr Mladek if (!IS_ENABLED(CONFIG_HAVE_CLK)) 1839c8c3b584SPetr Mladek return error_string(buf, end, "(%pC?)", spec); 18400b74d4d7SPetr Mladek 18413e5903ebSPetr Mladek if (check_pointer(&buf, end, clk, spec)) 18423e5903ebSPetr Mladek return buf; 1843900cca29SGeert Uytterhoeven 1844900cca29SGeert Uytterhoeven switch (fmt[1]) { 1845900cca29SGeert Uytterhoeven case 'n': 1846900cca29SGeert Uytterhoeven default: 1847900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1848900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1849900cca29SGeert Uytterhoeven #else 18504ca96aa9SGeert Uytterhoeven return ptr_to_id(buf, end, clk, spec); 1851900cca29SGeert Uytterhoeven #endif 1852900cca29SGeert Uytterhoeven } 1853900cca29SGeert Uytterhoeven } 1854900cca29SGeert Uytterhoeven 1855edf14cdbSVlastimil Babka static 1856edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags, 1857edf14cdbSVlastimil Babka const struct trace_print_flags *names) 1858edf14cdbSVlastimil Babka { 1859edf14cdbSVlastimil Babka unsigned long mask; 1860edf14cdbSVlastimil Babka 1861edf14cdbSVlastimil Babka for ( ; flags && names->name; names++) { 1862edf14cdbSVlastimil Babka mask = names->mask; 1863edf14cdbSVlastimil Babka if ((flags & mask) != mask) 1864edf14cdbSVlastimil Babka continue; 1865edf14cdbSVlastimil Babka 1866abd4fe62SAndy Shevchenko buf = string(buf, end, names->name, default_str_spec); 1867edf14cdbSVlastimil Babka 1868edf14cdbSVlastimil Babka flags &= ~mask; 1869edf14cdbSVlastimil Babka if (flags) { 1870edf14cdbSVlastimil Babka if (buf < end) 1871edf14cdbSVlastimil Babka *buf = '|'; 1872edf14cdbSVlastimil Babka buf++; 1873edf14cdbSVlastimil Babka } 1874edf14cdbSVlastimil Babka } 1875edf14cdbSVlastimil Babka 1876edf14cdbSVlastimil Babka if (flags) 187754433973SAndy Shevchenko buf = number(buf, end, flags, default_flag_spec); 1878edf14cdbSVlastimil Babka 1879edf14cdbSVlastimil Babka return buf; 1880edf14cdbSVlastimil Babka } 1881edf14cdbSVlastimil Babka 1882edf14cdbSVlastimil Babka static noinline_for_stack 18830b74d4d7SPetr Mladek char *flags_string(char *buf, char *end, void *flags_ptr, 18840b74d4d7SPetr Mladek struct printf_spec spec, const char *fmt) 1885edf14cdbSVlastimil Babka { 1886edf14cdbSVlastimil Babka unsigned long flags; 1887edf14cdbSVlastimil Babka const struct trace_print_flags *names; 1888edf14cdbSVlastimil Babka 18893e5903ebSPetr Mladek if (check_pointer(&buf, end, flags_ptr, spec)) 18903e5903ebSPetr Mladek return buf; 18913e5903ebSPetr Mladek 1892edf14cdbSVlastimil Babka switch (fmt[1]) { 1893edf14cdbSVlastimil Babka case 'p': 1894edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1895edf14cdbSVlastimil Babka /* Remove zone id */ 1896edf14cdbSVlastimil Babka flags &= (1UL << NR_PAGEFLAGS) - 1; 1897edf14cdbSVlastimil Babka names = pageflag_names; 1898edf14cdbSVlastimil Babka break; 1899edf14cdbSVlastimil Babka case 'v': 1900edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1901edf14cdbSVlastimil Babka names = vmaflag_names; 1902edf14cdbSVlastimil Babka break; 1903edf14cdbSVlastimil Babka case 'g': 1904edf14cdbSVlastimil Babka flags = *(gfp_t *)flags_ptr; 1905edf14cdbSVlastimil Babka names = gfpflag_names; 1906edf14cdbSVlastimil Babka break; 1907edf14cdbSVlastimil Babka default: 1908c8c3b584SPetr Mladek return error_string(buf, end, "(%pG?)", spec); 1909edf14cdbSVlastimil Babka } 1910edf14cdbSVlastimil Babka 1911edf14cdbSVlastimil Babka return format_flags(buf, end, flags, names); 1912edf14cdbSVlastimil Babka } 1913edf14cdbSVlastimil Babka 1914ce4fecf1SPantelis Antoniou static noinline_for_stack 1915a92eb762SSakari Ailus char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, 1916a92eb762SSakari Ailus char *end) 1917ce4fecf1SPantelis Antoniou { 1918ce4fecf1SPantelis Antoniou int depth; 1919ce4fecf1SPantelis Antoniou 1920a92eb762SSakari Ailus /* Loop starting from the root node to the current node. */ 1921a92eb762SSakari Ailus for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { 1922a92eb762SSakari Ailus struct fwnode_handle *__fwnode = 1923a92eb762SSakari Ailus fwnode_get_nth_parent(fwnode, depth); 1924ce4fecf1SPantelis Antoniou 1925a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name_prefix(__fwnode), 1926abd4fe62SAndy Shevchenko default_str_spec); 1927a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name(__fwnode), 1928a92eb762SSakari Ailus default_str_spec); 1929a92eb762SSakari Ailus 1930a92eb762SSakari Ailus fwnode_handle_put(__fwnode); 1931ce4fecf1SPantelis Antoniou } 1932a92eb762SSakari Ailus 1933ce4fecf1SPantelis Antoniou return buf; 1934ce4fecf1SPantelis Antoniou } 1935ce4fecf1SPantelis Antoniou 1936ce4fecf1SPantelis Antoniou static noinline_for_stack 1937ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn, 1938ce4fecf1SPantelis Antoniou struct printf_spec spec, const char *fmt) 1939ce4fecf1SPantelis Antoniou { 1940ce4fecf1SPantelis Antoniou char tbuf[sizeof("xxxx") + 1]; 1941ce4fecf1SPantelis Antoniou const char *p; 1942ce4fecf1SPantelis Antoniou int ret; 1943ce4fecf1SPantelis Antoniou char *buf_start = buf; 1944ce4fecf1SPantelis Antoniou struct property *prop; 1945ce4fecf1SPantelis Antoniou bool has_mult, pass; 1946ce4fecf1SPantelis Antoniou static const struct printf_spec num_spec = { 1947ce4fecf1SPantelis Antoniou .flags = SMALL, 1948ce4fecf1SPantelis Antoniou .field_width = -1, 1949ce4fecf1SPantelis Antoniou .precision = -1, 1950ce4fecf1SPantelis Antoniou .base = 10, 1951ce4fecf1SPantelis Antoniou }; 1952ce4fecf1SPantelis Antoniou 1953ce4fecf1SPantelis Antoniou struct printf_spec str_spec = spec; 1954ce4fecf1SPantelis Antoniou str_spec.field_width = -1; 1955ce4fecf1SPantelis Antoniou 195683abc5a7SSakari Ailus if (fmt[0] != 'F') 195783abc5a7SSakari Ailus return error_string(buf, end, "(%pO?)", spec); 195883abc5a7SSakari Ailus 1959ce4fecf1SPantelis Antoniou if (!IS_ENABLED(CONFIG_OF)) 1960c8c3b584SPetr Mladek return error_string(buf, end, "(%pOF?)", spec); 1961ce4fecf1SPantelis Antoniou 19623e5903ebSPetr Mladek if (check_pointer(&buf, end, dn, spec)) 19633e5903ebSPetr Mladek return buf; 1964ce4fecf1SPantelis Antoniou 1965ce4fecf1SPantelis Antoniou /* simple case without anything any more format specifiers */ 1966ce4fecf1SPantelis Antoniou fmt++; 1967ce4fecf1SPantelis Antoniou if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 1968ce4fecf1SPantelis Antoniou fmt = "f"; 1969ce4fecf1SPantelis Antoniou 1970ce4fecf1SPantelis Antoniou for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 19716d0a70a2SRob Herring int precision; 1972ce4fecf1SPantelis Antoniou if (pass) { 1973ce4fecf1SPantelis Antoniou if (buf < end) 1974ce4fecf1SPantelis Antoniou *buf = ':'; 1975ce4fecf1SPantelis Antoniou buf++; 1976ce4fecf1SPantelis Antoniou } 1977ce4fecf1SPantelis Antoniou 1978ce4fecf1SPantelis Antoniou switch (*fmt) { 1979ce4fecf1SPantelis Antoniou case 'f': /* full_name */ 1980a92eb762SSakari Ailus buf = fwnode_full_name_string(of_fwnode_handle(dn), buf, 1981a92eb762SSakari Ailus end); 1982ce4fecf1SPantelis Antoniou break; 1983ce4fecf1SPantelis Antoniou case 'n': /* name */ 1984a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn)); 19856d0a70a2SRob Herring precision = str_spec.precision; 19866d0a70a2SRob Herring str_spec.precision = strchrnul(p, '@') - p; 19876d0a70a2SRob Herring buf = string(buf, end, p, str_spec); 19886d0a70a2SRob Herring str_spec.precision = precision; 1989ce4fecf1SPantelis Antoniou break; 1990ce4fecf1SPantelis Antoniou case 'p': /* phandle */ 1991ce4fecf1SPantelis Antoniou buf = number(buf, end, (unsigned int)dn->phandle, num_spec); 1992ce4fecf1SPantelis Antoniou break; 1993ce4fecf1SPantelis Antoniou case 'P': /* path-spec */ 1994a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn)); 1995ce4fecf1SPantelis Antoniou if (!p[1]) 1996ce4fecf1SPantelis Antoniou p = "/"; 1997ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 1998ce4fecf1SPantelis Antoniou break; 1999ce4fecf1SPantelis Antoniou case 'F': /* flags */ 2000ce4fecf1SPantelis Antoniou tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 2001ce4fecf1SPantelis Antoniou tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 2002ce4fecf1SPantelis Antoniou tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 2003ce4fecf1SPantelis Antoniou tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 2004ce4fecf1SPantelis Antoniou tbuf[4] = 0; 2005d529ac41SPetr Mladek buf = string_nocheck(buf, end, tbuf, str_spec); 2006ce4fecf1SPantelis Antoniou break; 2007ce4fecf1SPantelis Antoniou case 'c': /* major compatible string */ 2008ce4fecf1SPantelis Antoniou ret = of_property_read_string(dn, "compatible", &p); 2009ce4fecf1SPantelis Antoniou if (!ret) 2010ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2011ce4fecf1SPantelis Antoniou break; 2012ce4fecf1SPantelis Antoniou case 'C': /* full compatible string */ 2013ce4fecf1SPantelis Antoniou has_mult = false; 2014ce4fecf1SPantelis Antoniou of_property_for_each_string(dn, "compatible", prop, p) { 2015ce4fecf1SPantelis Antoniou if (has_mult) 2016d529ac41SPetr Mladek buf = string_nocheck(buf, end, ",", str_spec); 2017d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 2018ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2019d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 2020ce4fecf1SPantelis Antoniou 2021ce4fecf1SPantelis Antoniou has_mult = true; 2022ce4fecf1SPantelis Antoniou } 2023ce4fecf1SPantelis Antoniou break; 2024ce4fecf1SPantelis Antoniou default: 2025ce4fecf1SPantelis Antoniou break; 2026ce4fecf1SPantelis Antoniou } 2027ce4fecf1SPantelis Antoniou } 2028ce4fecf1SPantelis Antoniou 2029ce4fecf1SPantelis Antoniou return widen_string(buf, buf - buf_start, end, spec); 2030ce4fecf1SPantelis Antoniou } 2031ce4fecf1SPantelis Antoniou 20323bd32d6aSSakari Ailus static noinline_for_stack 20333bd32d6aSSakari Ailus char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, 2034798cc27aSPetr Mladek struct printf_spec spec, const char *fmt) 2035798cc27aSPetr Mladek { 20363bd32d6aSSakari Ailus struct printf_spec str_spec = spec; 20373bd32d6aSSakari Ailus char *buf_start = buf; 20383bd32d6aSSakari Ailus 20393bd32d6aSSakari Ailus str_spec.field_width = -1; 20403bd32d6aSSakari Ailus 20413bd32d6aSSakari Ailus if (*fmt != 'w') 20423bd32d6aSSakari Ailus return error_string(buf, end, "(%pf?)", spec); 20433bd32d6aSSakari Ailus 20443bd32d6aSSakari Ailus if (check_pointer(&buf, end, fwnode, spec)) 20453bd32d6aSSakari Ailus return buf; 20463bd32d6aSSakari Ailus 20473bd32d6aSSakari Ailus fmt++; 20483bd32d6aSSakari Ailus 20493bd32d6aSSakari Ailus switch (*fmt) { 20503bd32d6aSSakari Ailus case 'P': /* name */ 20513bd32d6aSSakari Ailus buf = string(buf, end, fwnode_get_name(fwnode), str_spec); 20523bd32d6aSSakari Ailus break; 20533bd32d6aSSakari Ailus case 'f': /* full_name */ 20543bd32d6aSSakari Ailus default: 20553bd32d6aSSakari Ailus buf = fwnode_full_name_string(fwnode, buf, end); 20563bd32d6aSSakari Ailus break; 2057798cc27aSPetr Mladek } 2058798cc27aSPetr Mladek 20593bd32d6aSSakari Ailus return widen_string(buf, buf - buf_start, end, spec); 2060798cc27aSPetr Mladek } 2061798cc27aSPetr Mladek 20624d8a743cSLinus Torvalds /* 20634d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 20644d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 20654d8a743cSLinus Torvalds * specifiers. 20664d8a743cSLinus Torvalds * 20670b523769SJoe Perches * Please update scripts/checkpatch.pl when adding/removing conversion 20680b523769SJoe Perches * characters. (Search for "check for vsprintf extension"). 20690b523769SJoe Perches * 2070332d2e78SLinus Torvalds * Right now we handle: 20710fe1ef24SLinus Torvalds * 2072cdb7e52dSSergey Senozhatsky * - 'S' For symbolic direct pointers (or function descriptors) with offset 2073cdb7e52dSSergey Senozhatsky * - 's' For symbolic direct pointers (or function descriptors) without offset 20749af77064SSakari Ailus * - '[Ss]R' as above with __builtin_extract_return_addr() translation 20751586c5aeSSakari Ailus * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of 20761586c5aeSSakari Ailus * %ps and %pS. Be careful when re-using these specifiers. 20770f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 2078c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 2079c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 2080dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 2081dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 2082dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 2083dbc760bcSTejun Heo * range-list format instead of hex format 2084dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 2085dd45c9cfSHarvey Harrison * usual colon-separated hex notation 20868a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 2087bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 2088c8e00060SJoe Perches * with a dash-separated hex notation 20897c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 20908a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 20918a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 20928a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 209310679643SDaniel Borkmann * [S][pfs] 209410679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 209510679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 20968a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 20978a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 20988a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 209910679643SDaniel Borkmann * [S][pfs] 210010679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 210110679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 210210679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 210310679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 210429cf519eSJoe Perches * http://tools.ietf.org/html/rfc5952 210571dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 210671dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 210771dca95dSAndy Shevchenko * details): 210871dca95dSAndy Shevchenko * a - ESCAPE_ANY 210971dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 211071dca95dSAndy Shevchenko * h - ESCAPE_HEX 211171dca95dSAndy Shevchenko * n - ESCAPE_NULL 211271dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 211371dca95dSAndy Shevchenko * p - ESCAPE_NP 211471dca95dSAndy Shevchenko * s - ESCAPE_SPACE 211571dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 21169ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 21179ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 21189ac6e44eSJoe Perches * Options for %pU are: 21199ac6e44eSJoe Perches * b big endian lower case hex (default) 21209ac6e44eSJoe Perches * B big endian UPPER case hex 21219ac6e44eSJoe Perches * l little endian lower case hex 21229ac6e44eSJoe Perches * L little endian UPPER case hex 21239ac6e44eSJoe Perches * big endian output byte order is: 21249ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 21259ac6e44eSJoe Perches * little endian output byte order is: 21269ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 21277db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 21287db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 21297db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 21307db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 21317db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 2132455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 2133c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 213431550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 213531550a16SAndy Shevchenko * a certain separator (' ' by default): 213631550a16SAndy Shevchenko * C colon 213731550a16SAndy Shevchenko * D dash 213831550a16SAndy Shevchenko * N no separator 213931550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 214031550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 2141aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 2142aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 2143c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 2144c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 21451031bc58SDmitry Monakhov * - 'g' For block_device name (gendisk + partition number) 21464d42c447SAndy Shevchenko * - 't[R][dt][r]' For time and date as represented: 21474d42c447SAndy Shevchenko * R struct rtc_time 2148900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 2149900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2150900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 2151900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2152edf14cdbSVlastimil Babka * - 'G' For flags to be printed as a collection of symbolic strings that would 2153edf14cdbSVlastimil Babka * construct the specific value. Supported flags given by option: 2154edf14cdbSVlastimil Babka * p page flags (see struct page) given as pointer to unsigned long 2155edf14cdbSVlastimil Babka * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 2156edf14cdbSVlastimil Babka * v vma flags (VM_*) given as pointer to unsigned long 2157ce4fecf1SPantelis Antoniou * - 'OF[fnpPcCF]' For a device tree object 2158ce4fecf1SPantelis Antoniou * Without any optional arguments prints the full_name 2159ce4fecf1SPantelis Antoniou * f device node full_name 2160ce4fecf1SPantelis Antoniou * n device node name 2161ce4fecf1SPantelis Antoniou * p device node phandle 2162ce4fecf1SPantelis Antoniou * P device node path spec (name + @unit) 2163ce4fecf1SPantelis Antoniou * F device node flags 2164ce4fecf1SPantelis Antoniou * c major compatible string 2165ce4fecf1SPantelis Antoniou * C full compatible string 21663bd32d6aSSakari Ailus * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer 21673bd32d6aSSakari Ailus * Without an option prints the full name of the node 21683bd32d6aSSakari Ailus * f full name 21693bd32d6aSSakari Ailus * P node name, including a possible unit address 21707b1924a1STobin C. Harding * - 'x' For printing the address. Equivalent to "%lx". 21717b1924a1STobin C. Harding * 2172b3ed2321STobin C. Harding * ** When making changes please also update: 2173b3ed2321STobin C. Harding * Documentation/core-api/printk-formats.rst 21749ac6e44eSJoe Perches * 2175ad67b74dSTobin C. Harding * Note: The default behaviour (unadorned %p) is to hash the address, 2176ad67b74dSTobin C. Harding * rendering it useful as a unique identifier. 21774d8a743cSLinus Torvalds */ 2178cf3b429bSJoe Perches static noinline_for_stack 2179cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 2180fef20d9cSFrederic Weisbecker struct printf_spec spec) 218178a8bf69SLinus Torvalds { 21820fe1ef24SLinus Torvalds switch (*fmt) { 21830fe1ef24SLinus Torvalds case 'S': 21849ac6e44eSJoe Perches case 's': 218504b8eb7aSSergey Senozhatsky ptr = dereference_symbol_descriptor(ptr); 218604b8eb7aSSergey Senozhatsky /* Fallthrough */ 21870f77a8d3SNamhyung Kim case 'B': 2188b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 2189332d2e78SLinus Torvalds case 'R': 2190c7dabef8SBjorn Helgaas case 'r': 2191fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 219231550a16SAndy Shevchenko case 'h': 219331550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 2194dbc760bcSTejun Heo case 'b': 2195dbc760bcSTejun Heo switch (fmt[1]) { 2196dbc760bcSTejun Heo case 'l': 2197dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 2198dbc760bcSTejun Heo default: 2199dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 2200dbc760bcSTejun Heo } 22018a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 22028a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 220376597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 220476597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 22058a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 22068a27f7c9SJoe Perches case 'I': /* Formatted IP supported 22078a27f7c9SJoe Perches * 4: 1.2.3.4 22088a27f7c9SJoe Perches * 6: 0001:0203:...:0708 22098a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 22108a27f7c9SJoe Perches */ 22118a27f7c9SJoe Perches case 'i': /* Contiguous: 22128a27f7c9SJoe Perches * 4: 001.002.003.004 22138a27f7c9SJoe Perches * 6: 000102...0f 22148a27f7c9SJoe Perches */ 2215f00cc102SPetr Mladek return ip_addr_string(buf, end, ptr, spec, fmt); 221671dca95dSAndy Shevchenko case 'E': 221771dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 22189ac6e44eSJoe Perches case 'U': 22199ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 22207db6f5fbSJoe Perches case 'V': 22213e5903ebSPetr Mladek return va_format(buf, end, ptr, spec, fmt); 2222455cd5abSDan Rosenberg case 'K': 222357e73442STobin C. Harding return restricted_pointer(buf, end, ptr, spec); 2224c8f44affSMichał Mirosław case 'N': 2225431bca24SGeert Uytterhoeven return netdev_bits(buf, end, ptr, spec, fmt); 22267d799210SStepan Moskovchenko case 'a': 22273e5903ebSPetr Mladek return address_val(buf, end, ptr, spec, fmt); 22284b6ccca7SAl Viro case 'd': 22294b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 22304d42c447SAndy Shevchenko case 't': 22314d42c447SAndy Shevchenko return time_and_date(buf, end, ptr, spec, fmt); 2232900cca29SGeert Uytterhoeven case 'C': 2233900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 22344b6ccca7SAl Viro case 'D': 223536594b31SJia He return file_dentry_name(buf, end, ptr, spec, fmt); 22361031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 22371031bc58SDmitry Monakhov case 'g': 22381031bc58SDmitry Monakhov return bdev_name(buf, end, ptr, spec, fmt); 22391031bc58SDmitry Monakhov #endif 22401031bc58SDmitry Monakhov 2241edf14cdbSVlastimil Babka case 'G': 22420b74d4d7SPetr Mladek return flags_string(buf, end, ptr, spec, fmt); 2243ce4fecf1SPantelis Antoniou case 'O': 224483abc5a7SSakari Ailus return device_node_string(buf, end, ptr, spec, fmt + 1); 22453bd32d6aSSakari Ailus case 'f': 22463bd32d6aSSakari Ailus return fwnode_string(buf, end, ptr, spec, fmt + 1); 22477b1924a1STobin C. Harding case 'x': 22487b1924a1STobin C. Harding return pointer_string(buf, end, ptr, spec); 224957f5677eSRasmus Villemoes case 'e': 225057f5677eSRasmus Villemoes /* %pe with a non-ERR_PTR gets treated as plain %p */ 225157f5677eSRasmus Villemoes if (!IS_ERR(ptr)) 225257f5677eSRasmus Villemoes break; 225357f5677eSRasmus Villemoes return err_ptr(buf, end, ptr, spec); 22540fe1ef24SLinus Torvalds } 2255fef20d9cSFrederic Weisbecker 2256ad67b74dSTobin C. Harding /* default is to _not_ leak addresses, hash before printing */ 2257ad67b74dSTobin C. Harding return ptr_to_id(buf, end, ptr, spec); 2258fef20d9cSFrederic Weisbecker } 2259fef20d9cSFrederic Weisbecker 2260fef20d9cSFrederic Weisbecker /* 2261fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 2262fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 2263fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 2264fef20d9cSFrederic Weisbecker * to go on the next call). 2265fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 2266fef20d9cSFrederic Weisbecker * 2267fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 2268fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 2269fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 22705b5e0928SAlexey Dobriyan * 'Z' changed to 'z' --adobriyan 2017-01-25 2271fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 2272fef20d9cSFrederic Weisbecker * 2273fef20d9cSFrederic Weisbecker * @fmt: the format string 2274fef20d9cSFrederic Weisbecker * @type of the token returned 2275fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 2276fef20d9cSFrederic Weisbecker * @field_width: overwritten width 2277fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 2278fef20d9cSFrederic Weisbecker * @precision: precision of a number 2279fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 2280fef20d9cSFrederic Weisbecker */ 2281cf3b429bSJoe Perches static noinline_for_stack 2282cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 2283fef20d9cSFrederic Weisbecker { 2284fef20d9cSFrederic Weisbecker const char *start = fmt; 2285d0484193SRasmus Villemoes char qualifier; 2286fef20d9cSFrederic Weisbecker 2287fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 2288ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 2289fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 2290fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 2291fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 2292fef20d9cSFrederic Weisbecker } 2293fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2294fef20d9cSFrederic Weisbecker goto precision; 2295fef20d9cSFrederic Weisbecker } 2296fef20d9cSFrederic Weisbecker 2297fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 2298fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 2299fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2300fef20d9cSFrederic Weisbecker spec->precision = 0; 2301fef20d9cSFrederic Weisbecker 2302fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2303fef20d9cSFrederic Weisbecker goto qualifier; 2304fef20d9cSFrederic Weisbecker } 2305fef20d9cSFrederic Weisbecker 2306fef20d9cSFrederic Weisbecker /* By default */ 2307fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2308fef20d9cSFrederic Weisbecker 2309fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 2310fef20d9cSFrederic Weisbecker if (*fmt == '%') 2311fef20d9cSFrederic Weisbecker break; 2312fef20d9cSFrederic Weisbecker } 2313fef20d9cSFrederic Weisbecker 2314fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 2315fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 2316fef20d9cSFrederic Weisbecker return fmt - start; 2317fef20d9cSFrederic Weisbecker 2318fef20d9cSFrederic Weisbecker /* Process flags */ 2319fef20d9cSFrederic Weisbecker spec->flags = 0; 2320fef20d9cSFrederic Weisbecker 2321fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 2322fef20d9cSFrederic Weisbecker bool found = true; 2323fef20d9cSFrederic Weisbecker 2324fef20d9cSFrederic Weisbecker ++fmt; 2325fef20d9cSFrederic Weisbecker 2326fef20d9cSFrederic Weisbecker switch (*fmt) { 2327fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 2328fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 2329fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 2330fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 2331fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 2332fef20d9cSFrederic Weisbecker default: found = false; 2333fef20d9cSFrederic Weisbecker } 2334fef20d9cSFrederic Weisbecker 2335fef20d9cSFrederic Weisbecker if (!found) 2336fef20d9cSFrederic Weisbecker break; 2337fef20d9cSFrederic Weisbecker } 2338fef20d9cSFrederic Weisbecker 2339fef20d9cSFrederic Weisbecker /* get field width */ 2340fef20d9cSFrederic Weisbecker spec->field_width = -1; 2341fef20d9cSFrederic Weisbecker 2342fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 2343fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 2344fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 2345fef20d9cSFrederic Weisbecker /* it's the next argument */ 2346ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 2347fef20d9cSFrederic Weisbecker return ++fmt - start; 2348fef20d9cSFrederic Weisbecker } 2349fef20d9cSFrederic Weisbecker 2350fef20d9cSFrederic Weisbecker precision: 2351fef20d9cSFrederic Weisbecker /* get the precision */ 2352fef20d9cSFrederic Weisbecker spec->precision = -1; 2353fef20d9cSFrederic Weisbecker if (*fmt == '.') { 2354fef20d9cSFrederic Weisbecker ++fmt; 2355fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 2356fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 2357fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2358fef20d9cSFrederic Weisbecker spec->precision = 0; 2359fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 2360fef20d9cSFrederic Weisbecker /* it's the next argument */ 2361adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 2362fef20d9cSFrederic Weisbecker return ++fmt - start; 2363fef20d9cSFrederic Weisbecker } 2364fef20d9cSFrederic Weisbecker } 2365fef20d9cSFrederic Weisbecker 2366fef20d9cSFrederic Weisbecker qualifier: 2367fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 2368d0484193SRasmus Villemoes qualifier = 0; 236975fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 23705b5e0928SAlexey Dobriyan *fmt == 'z' || *fmt == 't') { 2371d0484193SRasmus Villemoes qualifier = *fmt++; 2372d0484193SRasmus Villemoes if (unlikely(qualifier == *fmt)) { 2373d0484193SRasmus Villemoes if (qualifier == 'l') { 2374d0484193SRasmus Villemoes qualifier = 'L'; 2375fef20d9cSFrederic Weisbecker ++fmt; 2376d0484193SRasmus Villemoes } else if (qualifier == 'h') { 2377d0484193SRasmus Villemoes qualifier = 'H'; 2378a4e94ef0SZhaolei ++fmt; 2379a4e94ef0SZhaolei } 2380fef20d9cSFrederic Weisbecker } 2381fef20d9cSFrederic Weisbecker } 2382fef20d9cSFrederic Weisbecker 2383fef20d9cSFrederic Weisbecker /* default base */ 2384fef20d9cSFrederic Weisbecker spec->base = 10; 2385fef20d9cSFrederic Weisbecker switch (*fmt) { 2386fef20d9cSFrederic Weisbecker case 'c': 2387fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 2388fef20d9cSFrederic Weisbecker return ++fmt - start; 2389fef20d9cSFrederic Weisbecker 2390fef20d9cSFrederic Weisbecker case 's': 2391fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 2392fef20d9cSFrederic Weisbecker return ++fmt - start; 2393fef20d9cSFrederic Weisbecker 2394fef20d9cSFrederic Weisbecker case 'p': 2395fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 2396ffbfed03SRasmus Villemoes return ++fmt - start; 2397fef20d9cSFrederic Weisbecker 2398fef20d9cSFrederic Weisbecker case '%': 2399fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 2400fef20d9cSFrederic Weisbecker return ++fmt - start; 2401fef20d9cSFrederic Weisbecker 2402fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 2403fef20d9cSFrederic Weisbecker case 'o': 2404fef20d9cSFrederic Weisbecker spec->base = 8; 2405fef20d9cSFrederic Weisbecker break; 2406fef20d9cSFrederic Weisbecker 2407fef20d9cSFrederic Weisbecker case 'x': 2408fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 24097e6bd6f3SAndy Shevchenko /* fall through */ 2410fef20d9cSFrederic Weisbecker 2411fef20d9cSFrederic Weisbecker case 'X': 2412fef20d9cSFrederic Weisbecker spec->base = 16; 2413fef20d9cSFrederic Weisbecker break; 2414fef20d9cSFrederic Weisbecker 2415fef20d9cSFrederic Weisbecker case 'd': 2416fef20d9cSFrederic Weisbecker case 'i': 241739e874f8SFrederic Weisbecker spec->flags |= SIGN; 2418fef20d9cSFrederic Weisbecker case 'u': 2419fef20d9cSFrederic Weisbecker break; 2420fef20d9cSFrederic Weisbecker 2421708d96fdSRyan Mallon case 'n': 2422708d96fdSRyan Mallon /* 2423b006f19bSRasmus Villemoes * Since %n poses a greater security risk than 2424b006f19bSRasmus Villemoes * utility, treat it as any other invalid or 2425b006f19bSRasmus Villemoes * unsupported format specifier. 2426708d96fdSRyan Mallon */ 2427708d96fdSRyan Mallon /* Fall-through */ 2428708d96fdSRyan Mallon 2429fef20d9cSFrederic Weisbecker default: 2430b006f19bSRasmus Villemoes WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 2431fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 2432fef20d9cSFrederic Weisbecker return fmt - start; 2433fef20d9cSFrederic Weisbecker } 2434fef20d9cSFrederic Weisbecker 2435d0484193SRasmus Villemoes if (qualifier == 'L') 2436fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 2437d0484193SRasmus Villemoes else if (qualifier == 'l') { 243851be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 243951be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 24405b5e0928SAlexey Dobriyan } else if (qualifier == 'z') { 2441fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 2442d0484193SRasmus Villemoes } else if (qualifier == 't') { 2443fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 2444d0484193SRasmus Villemoes } else if (qualifier == 'H') { 244551be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 244651be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 2447d0484193SRasmus Villemoes } else if (qualifier == 'h') { 244851be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 244951be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 2450fef20d9cSFrederic Weisbecker } else { 245151be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 245251be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 2453fef20d9cSFrederic Weisbecker } 2454fef20d9cSFrederic Weisbecker 2455fef20d9cSFrederic Weisbecker return ++fmt - start; 245678a8bf69SLinus Torvalds } 245778a8bf69SLinus Torvalds 24584d72ba01SRasmus Villemoes static void 24594d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width) 24604d72ba01SRasmus Villemoes { 24614d72ba01SRasmus Villemoes spec->field_width = width; 24624d72ba01SRasmus Villemoes if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 24634d72ba01SRasmus Villemoes spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 24644d72ba01SRasmus Villemoes } 24654d72ba01SRasmus Villemoes } 24664d72ba01SRasmus Villemoes 24674d72ba01SRasmus Villemoes static void 24684d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec) 24694d72ba01SRasmus Villemoes { 24704d72ba01SRasmus Villemoes spec->precision = prec; 24714d72ba01SRasmus Villemoes if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 24724d72ba01SRasmus Villemoes spec->precision = clamp(prec, 0, PRECISION_MAX); 24734d72ba01SRasmus Villemoes } 24744d72ba01SRasmus Villemoes } 24754d72ba01SRasmus Villemoes 24761da177e4SLinus Torvalds /** 24771da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 24781da177e4SLinus Torvalds * @buf: The buffer to place the result into 24791da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 24801da177e4SLinus Torvalds * @fmt: The format string to use 24811da177e4SLinus Torvalds * @args: Arguments for the format string 24821da177e4SLinus Torvalds * 2483d7ec9a05SRasmus Villemoes * This function generally follows C99 vsnprintf, but has some 2484d7ec9a05SRasmus Villemoes * extensions and a few limitations: 2485d7ec9a05SRasmus Villemoes * 24866cc89134Smchehab@s-opensource.com * - ``%n`` is unsupported 24876cc89134Smchehab@s-opensource.com * - ``%p*`` is handled by pointer() 248820036fdcSAndi Kleen * 248927e7c0e8SJonathan Corbet * See pointer() or Documentation/core-api/printk-formats.rst for more 24905e4ee7b1SMartin Kletzander * extensive description. 24915e4ee7b1SMartin Kletzander * 24925e4ee7b1SMartin Kletzander * **Please update the documentation in both places when making changes** 249380f548e0SAndrew Morton * 24941da177e4SLinus Torvalds * The return value is the number of characters which would 24951da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 24961da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 24971da177e4SLinus Torvalds * number of characters written into @buf as return value 249872fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 24991da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 25001da177e4SLinus Torvalds * string is truncated. 25011da177e4SLinus Torvalds * 2502ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 25031da177e4SLinus Torvalds */ 25041da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 25051da177e4SLinus Torvalds { 25061da177e4SLinus Torvalds unsigned long long num; 2507d4be151bSAndré Goddard Rosa char *str, *end; 2508fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 25091da177e4SLinus Torvalds 2510f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 2511f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 25122aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 25131da177e4SLinus Torvalds return 0; 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds str = buf; 2516f796937aSJeremy Fitzhardinge end = buf + size; 25171da177e4SLinus Torvalds 2518f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 2519f796937aSJeremy Fitzhardinge if (end < buf) { 25201da177e4SLinus Torvalds end = ((void *)-1); 2521f796937aSJeremy Fitzhardinge size = end - buf; 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds 2524fef20d9cSFrederic Weisbecker while (*fmt) { 2525fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2526d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2527fef20d9cSFrederic Weisbecker 2528fef20d9cSFrederic Weisbecker fmt += read; 2529fef20d9cSFrederic Weisbecker 2530fef20d9cSFrederic Weisbecker switch (spec.type) { 2531fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2532fef20d9cSFrederic Weisbecker int copy = read; 2533fef20d9cSFrederic Weisbecker if (str < end) { 2534fef20d9cSFrederic Weisbecker if (copy > end - str) 2535fef20d9cSFrederic Weisbecker copy = end - str; 2536fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2537fef20d9cSFrederic Weisbecker } 2538fef20d9cSFrederic Weisbecker str += read; 2539fef20d9cSFrederic Weisbecker break; 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 2542ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 25434d72ba01SRasmus Villemoes set_field_width(&spec, va_arg(args, int)); 2544fef20d9cSFrederic Weisbecker break; 25451da177e4SLinus Torvalds 2546fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 25474d72ba01SRasmus Villemoes set_precision(&spec, va_arg(args, int)); 2548fef20d9cSFrederic Weisbecker break; 25491da177e4SLinus Torvalds 2550d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2551d4be151bSAndré Goddard Rosa char c; 2552d4be151bSAndré Goddard Rosa 2553fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2554fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2555f796937aSJeremy Fitzhardinge if (str < end) 25561da177e4SLinus Torvalds *str = ' '; 25571da177e4SLinus Torvalds ++str; 2558fef20d9cSFrederic Weisbecker 25591da177e4SLinus Torvalds } 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 2562f796937aSJeremy Fitzhardinge if (str < end) 25631da177e4SLinus Torvalds *str = c; 25641da177e4SLinus Torvalds ++str; 2565fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2566f796937aSJeremy Fitzhardinge if (str < end) 25671da177e4SLinus Torvalds *str = ' '; 25681da177e4SLinus Torvalds ++str; 25691da177e4SLinus Torvalds } 2570fef20d9cSFrederic Weisbecker break; 2571d4be151bSAndré Goddard Rosa } 25721da177e4SLinus Torvalds 2573fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 2574fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 2575fef20d9cSFrederic Weisbecker break; 25761da177e4SLinus Torvalds 2577fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2578ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 2579fef20d9cSFrederic Weisbecker spec); 2580fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 25814d8a743cSLinus Torvalds fmt++; 2582fef20d9cSFrederic Weisbecker break; 25831da177e4SLinus Torvalds 2584fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2585f796937aSJeremy Fitzhardinge if (str < end) 25861da177e4SLinus Torvalds *str = '%'; 25871da177e4SLinus Torvalds ++str; 25881da177e4SLinus Torvalds break; 25891da177e4SLinus Torvalds 2590fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 2591b006f19bSRasmus Villemoes /* 2592b006f19bSRasmus Villemoes * Presumably the arguments passed gcc's type 2593b006f19bSRasmus Villemoes * checking, but there is no safe or sane way 2594b006f19bSRasmus Villemoes * for us to continue parsing the format and 2595b006f19bSRasmus Villemoes * fetching from the va_list; the remaining 2596b006f19bSRasmus Villemoes * specifiers and arguments would be out of 2597b006f19bSRasmus Villemoes * sync. 2598b006f19bSRasmus Villemoes */ 2599b006f19bSRasmus Villemoes goto out; 2600fef20d9cSFrederic Weisbecker 2601fef20d9cSFrederic Weisbecker default: 2602fef20d9cSFrederic Weisbecker switch (spec.type) { 2603fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2604fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 2605fef20d9cSFrederic Weisbecker break; 2606fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2607fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 2608fef20d9cSFrederic Weisbecker break; 2609fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2610fef20d9cSFrederic Weisbecker num = va_arg(args, long); 2611fef20d9cSFrederic Weisbecker break; 2612fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2613ef124960SJason Gunthorpe if (spec.flags & SIGN) 2614ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 2615ef124960SJason Gunthorpe else 2616fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 2617fef20d9cSFrederic Weisbecker break; 2618fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2619fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 2620fef20d9cSFrederic Weisbecker break; 2621a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2622a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 2623a4e94ef0SZhaolei break; 2624a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2625a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 2626a4e94ef0SZhaolei break; 2627fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2628fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 2629fef20d9cSFrederic Weisbecker break; 2630fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2631fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 2632fef20d9cSFrederic Weisbecker break; 263339e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 263439e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 2635fef20d9cSFrederic Weisbecker break; 2636fef20d9cSFrederic Weisbecker default: 2637fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 26381da177e4SLinus Torvalds } 2639fef20d9cSFrederic Weisbecker 2640fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 26411da177e4SLinus Torvalds } 2642fef20d9cSFrederic Weisbecker } 2643fef20d9cSFrederic Weisbecker 2644b006f19bSRasmus Villemoes out: 2645f796937aSJeremy Fitzhardinge if (size > 0) { 2646f796937aSJeremy Fitzhardinge if (str < end) 26471da177e4SLinus Torvalds *str = '\0'; 2648f796937aSJeremy Fitzhardinge else 26490a6047eeSLinus Torvalds end[-1] = '\0'; 2650f796937aSJeremy Fitzhardinge } 2651fef20d9cSFrederic Weisbecker 2652f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 26531da177e4SLinus Torvalds return str-buf; 2654fef20d9cSFrederic Weisbecker 26551da177e4SLinus Torvalds } 26561da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds /** 26591da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 26601da177e4SLinus Torvalds * @buf: The buffer to place the result into 26611da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 26621da177e4SLinus Torvalds * @fmt: The format string to use 26631da177e4SLinus Torvalds * @args: Arguments for the format string 26641da177e4SLinus Torvalds * 26651da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2666b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 26671da177e4SLinus Torvalds * returns 0. 26681da177e4SLinus Torvalds * 2669ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 267020036fdcSAndi Kleen * 267120036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 26721da177e4SLinus Torvalds */ 26731da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 26741da177e4SLinus Torvalds { 26751da177e4SLinus Torvalds int i; 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 26787b9186f5SAndré Goddard Rosa 2679b921c69fSAnton Arapov if (likely(i < size)) 2680b921c69fSAnton Arapov return i; 2681b921c69fSAnton Arapov if (size != 0) 2682b921c69fSAnton Arapov return size - 1; 2683b921c69fSAnton Arapov return 0; 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 26861da177e4SLinus Torvalds 26871da177e4SLinus Torvalds /** 26881da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 26891da177e4SLinus Torvalds * @buf: The buffer to place the result into 26901da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 26911da177e4SLinus Torvalds * @fmt: The format string to use 26921da177e4SLinus Torvalds * @...: Arguments for the format string 26931da177e4SLinus Torvalds * 26941da177e4SLinus Torvalds * The return value is the number of characters which would be 26951da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 26961da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 26971da177e4SLinus Torvalds * @size, the resulting string is truncated. 269820036fdcSAndi Kleen * 269920036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 27001da177e4SLinus Torvalds */ 27011da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 27021da177e4SLinus Torvalds { 27031da177e4SLinus Torvalds va_list args; 27041da177e4SLinus Torvalds int i; 27051da177e4SLinus Torvalds 27061da177e4SLinus Torvalds va_start(args, fmt); 27071da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 27081da177e4SLinus Torvalds va_end(args); 27097b9186f5SAndré Goddard Rosa 27101da177e4SLinus Torvalds return i; 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds /** 27151da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 27161da177e4SLinus Torvalds * @buf: The buffer to place the result into 27171da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 27181da177e4SLinus Torvalds * @fmt: The format string to use 27191da177e4SLinus Torvalds * @...: Arguments for the format string 27201da177e4SLinus Torvalds * 27211da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2722b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 27231da177e4SLinus Torvalds */ 27241da177e4SLinus Torvalds 27251da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 27261da177e4SLinus Torvalds { 27271da177e4SLinus Torvalds va_list args; 27281da177e4SLinus Torvalds int i; 27291da177e4SLinus Torvalds 27301da177e4SLinus Torvalds va_start(args, fmt); 2731b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 27321da177e4SLinus Torvalds va_end(args); 27337b9186f5SAndré Goddard Rosa 2734b903c0b8SChangli Gao return i; 27351da177e4SLinus Torvalds } 27361da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 27371da177e4SLinus Torvalds 27381da177e4SLinus Torvalds /** 27391da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 27401da177e4SLinus Torvalds * @buf: The buffer to place the result into 27411da177e4SLinus Torvalds * @fmt: The format string to use 27421da177e4SLinus Torvalds * @args: Arguments for the format string 27431da177e4SLinus Torvalds * 27441da177e4SLinus Torvalds * The function returns the number of characters written 274572fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 27461da177e4SLinus Torvalds * buffer overflows. 27471da177e4SLinus Torvalds * 2748ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 274920036fdcSAndi Kleen * 275020036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 27511da177e4SLinus Torvalds */ 27521da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 27531da177e4SLinus Torvalds { 27541da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds /** 27591da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 27601da177e4SLinus Torvalds * @buf: The buffer to place the result into 27611da177e4SLinus Torvalds * @fmt: The format string to use 27621da177e4SLinus Torvalds * @...: Arguments for the format string 27631da177e4SLinus Torvalds * 27641da177e4SLinus Torvalds * The function returns the number of characters written 276572fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 27661da177e4SLinus Torvalds * buffer overflows. 276720036fdcSAndi Kleen * 276820036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 27691da177e4SLinus Torvalds */ 27701da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 27711da177e4SLinus Torvalds { 27721da177e4SLinus Torvalds va_list args; 27731da177e4SLinus Torvalds int i; 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds va_start(args, fmt); 27761da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 27771da177e4SLinus Torvalds va_end(args); 27787b9186f5SAndré Goddard Rosa 27791da177e4SLinus Torvalds return i; 27801da177e4SLinus Torvalds } 27811da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 27821da177e4SLinus Torvalds 27834370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 27844370aa4aSLai Jiangshan /* 27854370aa4aSLai Jiangshan * bprintf service: 27864370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 27874370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 27884370aa4aSLai Jiangshan */ 27894370aa4aSLai Jiangshan 27904370aa4aSLai Jiangshan /** 27914370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 27924370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 27934370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 27944370aa4aSLai Jiangshan * @fmt: The format string to use 27954370aa4aSLai Jiangshan * @args: Arguments for the format string 27964370aa4aSLai Jiangshan * 27974370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 2798da3dae54SMasanari Iida * is skipped. 27994370aa4aSLai Jiangshan * 28004370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 28014370aa4aSLai Jiangshan * the given input. 28024370aa4aSLai Jiangshan * 28034370aa4aSLai Jiangshan * NOTE: 28044370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 28054370aa4aSLai Jiangshan * valid for bstr_printf(). 28064370aa4aSLai Jiangshan */ 28074370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 28084370aa4aSLai Jiangshan { 2809fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 28104370aa4aSLai Jiangshan char *str, *end; 2811841a915dSSteven Rostedt (VMware) int width; 28124370aa4aSLai Jiangshan 28134370aa4aSLai Jiangshan str = (char *)bin_buf; 28144370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 28154370aa4aSLai Jiangshan 28164370aa4aSLai Jiangshan #define save_arg(type) \ 2817841a915dSSteven Rostedt (VMware) ({ \ 28184370aa4aSLai Jiangshan unsigned long long value; \ 2819841a915dSSteven Rostedt (VMware) if (sizeof(type) == 8) { \ 2820841a915dSSteven Rostedt (VMware) unsigned long long val8; \ 28214370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 2822841a915dSSteven Rostedt (VMware) val8 = va_arg(args, unsigned long long); \ 28234370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 2824841a915dSSteven Rostedt (VMware) *(u32 *)str = *(u32 *)&val8; \ 2825841a915dSSteven Rostedt (VMware) *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 28264370aa4aSLai Jiangshan } \ 2827841a915dSSteven Rostedt (VMware) value = val8; \ 28284370aa4aSLai Jiangshan } else { \ 2829841a915dSSteven Rostedt (VMware) unsigned int val4; \ 28304370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 2831841a915dSSteven Rostedt (VMware) val4 = va_arg(args, int); \ 28324370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 2833841a915dSSteven Rostedt (VMware) *(typeof(type) *)str = (type)(long)val4; \ 2834841a915dSSteven Rostedt (VMware) value = (unsigned long long)val4; \ 28354370aa4aSLai Jiangshan } \ 28364370aa4aSLai Jiangshan str += sizeof(type); \ 2837841a915dSSteven Rostedt (VMware) value; \ 2838841a915dSSteven Rostedt (VMware) }) 28394370aa4aSLai Jiangshan 2840fef20d9cSFrederic Weisbecker while (*fmt) { 2841d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 28424370aa4aSLai Jiangshan 2843fef20d9cSFrederic Weisbecker fmt += read; 2844fef20d9cSFrederic Weisbecker 2845fef20d9cSFrederic Weisbecker switch (spec.type) { 2846fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 2847d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 2848fef20d9cSFrederic Weisbecker break; 2849b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2850b006f19bSRasmus Villemoes goto out; 2851fef20d9cSFrederic Weisbecker 2852ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2853fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2854841a915dSSteven Rostedt (VMware) width = (int)save_arg(int); 2855841a915dSSteven Rostedt (VMware) /* Pointers may require the width */ 2856841a915dSSteven Rostedt (VMware) if (*fmt == 'p') 2857841a915dSSteven Rostedt (VMware) set_field_width(&spec, width); 2858fef20d9cSFrederic Weisbecker break; 28594370aa4aSLai Jiangshan 2860fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 28614370aa4aSLai Jiangshan save_arg(char); 2862fef20d9cSFrederic Weisbecker break; 2863fef20d9cSFrederic Weisbecker 2864fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 28654370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 28663e5903ebSPetr Mladek const char *err_msg; 28674370aa4aSLai Jiangshan size_t len; 28686c356634SAndré Goddard Rosa 28693e5903ebSPetr Mladek err_msg = check_pointer_msg(save_str); 28703e5903ebSPetr Mladek if (err_msg) 28713e5903ebSPetr Mladek save_str = err_msg; 28723e5903ebSPetr Mladek 28736c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 28746c356634SAndré Goddard Rosa if (str + len < end) 28756c356634SAndré Goddard Rosa memcpy(str, save_str, len); 28766c356634SAndré Goddard Rosa str += len; 2877fef20d9cSFrederic Weisbecker break; 28784370aa4aSLai Jiangshan } 2879fef20d9cSFrederic Weisbecker 2880fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2881841a915dSSteven Rostedt (VMware) /* Dereferenced pointers must be done now */ 2882841a915dSSteven Rostedt (VMware) switch (*fmt) { 2883841a915dSSteven Rostedt (VMware) /* Dereference of functions is still OK */ 2884841a915dSSteven Rostedt (VMware) case 'S': 2885841a915dSSteven Rostedt (VMware) case 's': 28861e6338cfSSteven Rostedt (VMware) case 'x': 28871e6338cfSSteven Rostedt (VMware) case 'K': 288857f5677eSRasmus Villemoes case 'e': 28894370aa4aSLai Jiangshan save_arg(void *); 2890841a915dSSteven Rostedt (VMware) break; 2891841a915dSSteven Rostedt (VMware) default: 2892841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 2893841a915dSSteven Rostedt (VMware) save_arg(void *); 2894841a915dSSteven Rostedt (VMware) break; 2895841a915dSSteven Rostedt (VMware) } 2896841a915dSSteven Rostedt (VMware) str = pointer(fmt, str, end, va_arg(args, void *), 2897841a915dSSteven Rostedt (VMware) spec); 2898841a915dSSteven Rostedt (VMware) if (str + 1 < end) 2899841a915dSSteven Rostedt (VMware) *str++ = '\0'; 2900841a915dSSteven Rostedt (VMware) else 2901841a915dSSteven Rostedt (VMware) end[-1] = '\0'; /* Must be nul terminated */ 2902841a915dSSteven Rostedt (VMware) } 29034370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2904fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 29054370aa4aSLai Jiangshan fmt++; 2906fef20d9cSFrederic Weisbecker break; 2907fef20d9cSFrederic Weisbecker 2908fef20d9cSFrederic Weisbecker default: 2909fef20d9cSFrederic Weisbecker switch (spec.type) { 2910fef20d9cSFrederic Weisbecker 2911fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2912fef20d9cSFrederic Weisbecker save_arg(long long); 2913fef20d9cSFrederic Weisbecker break; 2914fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2915fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2916fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2917fef20d9cSFrederic Weisbecker break; 2918fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2919fef20d9cSFrederic Weisbecker save_arg(size_t); 2920fef20d9cSFrederic Weisbecker break; 2921fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2922fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2923fef20d9cSFrederic Weisbecker break; 2924a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2925a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2926a4e94ef0SZhaolei save_arg(char); 2927a4e94ef0SZhaolei break; 2928fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2929fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2930fef20d9cSFrederic Weisbecker save_arg(short); 2931fef20d9cSFrederic Weisbecker break; 2932fef20d9cSFrederic Weisbecker default: 2933fef20d9cSFrederic Weisbecker save_arg(int); 2934fef20d9cSFrederic Weisbecker } 2935fef20d9cSFrederic Weisbecker } 2936fef20d9cSFrederic Weisbecker } 2937fef20d9cSFrederic Weisbecker 2938b006f19bSRasmus Villemoes out: 29397b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2940fef20d9cSFrederic Weisbecker #undef save_arg 29414370aa4aSLai Jiangshan } 29424370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 29434370aa4aSLai Jiangshan 29444370aa4aSLai Jiangshan /** 29454370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 29464370aa4aSLai Jiangshan * @buf: The buffer to place the result into 29474370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 29484370aa4aSLai Jiangshan * @fmt: The format string to use 29494370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 29504370aa4aSLai Jiangshan * 29514370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 29524370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 29534370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 29544370aa4aSLai Jiangshan * 29554370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 29560efb4d20SSteven Rostedt * see vsnprintf comment for details. 29574370aa4aSLai Jiangshan * 29584370aa4aSLai Jiangshan * The return value is the number of characters which would 29594370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 29604370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 29614370aa4aSLai Jiangshan * number of characters written into @buf as return value 29624370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 29634370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 29644370aa4aSLai Jiangshan * string is truncated. 29654370aa4aSLai Jiangshan */ 29664370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 29674370aa4aSLai Jiangshan { 2968fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 2969d4be151bSAndré Goddard Rosa char *str, *end; 2970d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 29714370aa4aSLai Jiangshan 2972762abb51SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 29734370aa4aSLai Jiangshan return 0; 29744370aa4aSLai Jiangshan 29754370aa4aSLai Jiangshan str = buf; 29764370aa4aSLai Jiangshan end = buf + size; 29774370aa4aSLai Jiangshan 29784370aa4aSLai Jiangshan #define get_arg(type) \ 29794370aa4aSLai Jiangshan ({ \ 29804370aa4aSLai Jiangshan typeof(type) value; \ 29814370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 29824370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 29834370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 29844370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 29854370aa4aSLai Jiangshan } else { \ 29864370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 29874370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 29884370aa4aSLai Jiangshan } \ 29894370aa4aSLai Jiangshan args += sizeof(type); \ 29904370aa4aSLai Jiangshan value; \ 29914370aa4aSLai Jiangshan }) 29924370aa4aSLai Jiangshan 29934370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 29944370aa4aSLai Jiangshan if (end < buf) { 29954370aa4aSLai Jiangshan end = ((void *)-1); 29964370aa4aSLai Jiangshan size = end - buf; 29974370aa4aSLai Jiangshan } 29984370aa4aSLai Jiangshan 2999fef20d9cSFrederic Weisbecker while (*fmt) { 3000fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 3001d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 3002fef20d9cSFrederic Weisbecker 3003fef20d9cSFrederic Weisbecker fmt += read; 3004fef20d9cSFrederic Weisbecker 3005fef20d9cSFrederic Weisbecker switch (spec.type) { 3006fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 3007fef20d9cSFrederic Weisbecker int copy = read; 3008fef20d9cSFrederic Weisbecker if (str < end) { 3009fef20d9cSFrederic Weisbecker if (copy > end - str) 3010fef20d9cSFrederic Weisbecker copy = end - str; 3011fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 3012fef20d9cSFrederic Weisbecker } 3013fef20d9cSFrederic Weisbecker str += read; 3014fef20d9cSFrederic Weisbecker break; 30154370aa4aSLai Jiangshan } 30164370aa4aSLai Jiangshan 3017ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 30184d72ba01SRasmus Villemoes set_field_width(&spec, get_arg(int)); 3019fef20d9cSFrederic Weisbecker break; 30204370aa4aSLai Jiangshan 3021fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 30224d72ba01SRasmus Villemoes set_precision(&spec, get_arg(int)); 3023fef20d9cSFrederic Weisbecker break; 30244370aa4aSLai Jiangshan 3025d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 3026d4be151bSAndré Goddard Rosa char c; 3027d4be151bSAndré Goddard Rosa 3028fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 3029fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 30304370aa4aSLai Jiangshan if (str < end) 30314370aa4aSLai Jiangshan *str = ' '; 30324370aa4aSLai Jiangshan ++str; 30334370aa4aSLai Jiangshan } 30344370aa4aSLai Jiangshan } 30354370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 30364370aa4aSLai Jiangshan if (str < end) 30374370aa4aSLai Jiangshan *str = c; 30384370aa4aSLai Jiangshan ++str; 3039fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 30404370aa4aSLai Jiangshan if (str < end) 30414370aa4aSLai Jiangshan *str = ' '; 30424370aa4aSLai Jiangshan ++str; 30434370aa4aSLai Jiangshan } 3044fef20d9cSFrederic Weisbecker break; 3045d4be151bSAndré Goddard Rosa } 30464370aa4aSLai Jiangshan 3047fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 30484370aa4aSLai Jiangshan const char *str_arg = args; 3049d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 3050fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 3051fef20d9cSFrederic Weisbecker break; 30524370aa4aSLai Jiangshan } 30534370aa4aSLai Jiangshan 3054841a915dSSteven Rostedt (VMware) case FORMAT_TYPE_PTR: { 3055841a915dSSteven Rostedt (VMware) bool process = false; 3056841a915dSSteven Rostedt (VMware) int copy, len; 3057841a915dSSteven Rostedt (VMware) /* Non function dereferences were already done */ 3058841a915dSSteven Rostedt (VMware) switch (*fmt) { 3059841a915dSSteven Rostedt (VMware) case 'S': 3060841a915dSSteven Rostedt (VMware) case 's': 3061841a915dSSteven Rostedt (VMware) case 'F': 3062841a915dSSteven Rostedt (VMware) case 'f': 30631e6338cfSSteven Rostedt (VMware) case 'x': 30641e6338cfSSteven Rostedt (VMware) case 'K': 306557f5677eSRasmus Villemoes case 'e': 3066841a915dSSteven Rostedt (VMware) process = true; 3067841a915dSSteven Rostedt (VMware) break; 3068841a915dSSteven Rostedt (VMware) default: 3069841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 3070841a915dSSteven Rostedt (VMware) process = true; 3071841a915dSSteven Rostedt (VMware) break; 3072841a915dSSteven Rostedt (VMware) } 3073841a915dSSteven Rostedt (VMware) /* Pointer dereference was already processed */ 3074841a915dSSteven Rostedt (VMware) if (str < end) { 3075841a915dSSteven Rostedt (VMware) len = copy = strlen(args); 3076841a915dSSteven Rostedt (VMware) if (copy > end - str) 3077841a915dSSteven Rostedt (VMware) copy = end - str; 3078841a915dSSteven Rostedt (VMware) memcpy(str, args, copy); 3079841a915dSSteven Rostedt (VMware) str += len; 308062165600SSteven Rostedt (VMware) args += len + 1; 3081841a915dSSteven Rostedt (VMware) } 3082841a915dSSteven Rostedt (VMware) } 3083841a915dSSteven Rostedt (VMware) if (process) 3084ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 3085841a915dSSteven Rostedt (VMware) 3086fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 30874370aa4aSLai Jiangshan fmt++; 3088fef20d9cSFrederic Weisbecker break; 3089841a915dSSteven Rostedt (VMware) } 30904370aa4aSLai Jiangshan 3091fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 30924370aa4aSLai Jiangshan if (str < end) 30934370aa4aSLai Jiangshan *str = '%'; 30944370aa4aSLai Jiangshan ++str; 3095fef20d9cSFrederic Weisbecker break; 3096fef20d9cSFrederic Weisbecker 3097b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 3098b006f19bSRasmus Villemoes goto out; 3099b006f19bSRasmus Villemoes 3100d4be151bSAndré Goddard Rosa default: { 3101d4be151bSAndré Goddard Rosa unsigned long long num; 3102d4be151bSAndré Goddard Rosa 3103fef20d9cSFrederic Weisbecker switch (spec.type) { 3104fef20d9cSFrederic Weisbecker 3105fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 31064370aa4aSLai Jiangshan num = get_arg(long long); 3107fef20d9cSFrederic Weisbecker break; 3108fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 3109fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 3110fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 3111fef20d9cSFrederic Weisbecker break; 3112fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 31134370aa4aSLai Jiangshan num = get_arg(size_t); 3114fef20d9cSFrederic Weisbecker break; 3115fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 31164370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 3117fef20d9cSFrederic Weisbecker break; 3118a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 3119a4e94ef0SZhaolei num = get_arg(unsigned char); 3120a4e94ef0SZhaolei break; 3121a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 3122a4e94ef0SZhaolei num = get_arg(signed char); 3123a4e94ef0SZhaolei break; 3124fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 3125fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 3126fef20d9cSFrederic Weisbecker break; 3127fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 3128fef20d9cSFrederic Weisbecker num = get_arg(short); 3129fef20d9cSFrederic Weisbecker break; 3130fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 31314370aa4aSLai Jiangshan num = get_arg(unsigned int); 3132fef20d9cSFrederic Weisbecker break; 3133fef20d9cSFrederic Weisbecker default: 3134fef20d9cSFrederic Weisbecker num = get_arg(int); 31354370aa4aSLai Jiangshan } 3136fef20d9cSFrederic Weisbecker 3137fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 3138d4be151bSAndré Goddard Rosa } /* default: */ 3139d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 3140d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 3141fef20d9cSFrederic Weisbecker 3142b006f19bSRasmus Villemoes out: 31434370aa4aSLai Jiangshan if (size > 0) { 31444370aa4aSLai Jiangshan if (str < end) 31454370aa4aSLai Jiangshan *str = '\0'; 31464370aa4aSLai Jiangshan else 31474370aa4aSLai Jiangshan end[-1] = '\0'; 31484370aa4aSLai Jiangshan } 3149fef20d9cSFrederic Weisbecker 31504370aa4aSLai Jiangshan #undef get_arg 31514370aa4aSLai Jiangshan 31524370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 31534370aa4aSLai Jiangshan return str - buf; 31544370aa4aSLai Jiangshan } 31554370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 31564370aa4aSLai Jiangshan 31574370aa4aSLai Jiangshan /** 31584370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 31594370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 31604370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 31614370aa4aSLai Jiangshan * @fmt: The format string to use 31624370aa4aSLai Jiangshan * @...: Arguments for the format string 31634370aa4aSLai Jiangshan * 31644370aa4aSLai Jiangshan * The function returns the number of words(u32) written 31654370aa4aSLai Jiangshan * into @bin_buf. 31664370aa4aSLai Jiangshan */ 31674370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 31684370aa4aSLai Jiangshan { 31694370aa4aSLai Jiangshan va_list args; 31704370aa4aSLai Jiangshan int ret; 31714370aa4aSLai Jiangshan 31724370aa4aSLai Jiangshan va_start(args, fmt); 31734370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 31744370aa4aSLai Jiangshan va_end(args); 31757b9186f5SAndré Goddard Rosa 31764370aa4aSLai Jiangshan return ret; 31774370aa4aSLai Jiangshan } 31784370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 31794370aa4aSLai Jiangshan 31804370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 31814370aa4aSLai Jiangshan 31821da177e4SLinus Torvalds /** 31831da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 31841da177e4SLinus Torvalds * @buf: input buffer 31851da177e4SLinus Torvalds * @fmt: format of buffer 31861da177e4SLinus Torvalds * @args: arguments 31871da177e4SLinus Torvalds */ 31881da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 31891da177e4SLinus Torvalds { 31901da177e4SLinus Torvalds const char *str = buf; 31911da177e4SLinus Torvalds char *next; 31921da177e4SLinus Torvalds char digit; 31931da177e4SLinus Torvalds int num = 0; 3194ef0658f3SJoe Perches u8 qualifier; 319553809751SJan Beulich unsigned int base; 319653809751SJan Beulich union { 319753809751SJan Beulich long long s; 319853809751SJan Beulich unsigned long long u; 319953809751SJan Beulich } val; 3200ef0658f3SJoe Perches s16 field_width; 3201d4be151bSAndré Goddard Rosa bool is_sign; 32021da177e4SLinus Torvalds 3203da99075cSJan Beulich while (*fmt) { 32041da177e4SLinus Torvalds /* skip any white space in format */ 32051da177e4SLinus Torvalds /* white space in format matchs any amount of 32061da177e4SLinus Torvalds * white space, including none, in the input. 32071da177e4SLinus Torvalds */ 32081da177e4SLinus Torvalds if (isspace(*fmt)) { 3209e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 3210e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds 32131da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 32141da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 32151da177e4SLinus Torvalds if (*fmt++ != *str++) 32161da177e4SLinus Torvalds break; 32171da177e4SLinus Torvalds continue; 32181da177e4SLinus Torvalds } 32191da177e4SLinus Torvalds 32201da177e4SLinus Torvalds if (!*fmt) 32211da177e4SLinus Torvalds break; 32221da177e4SLinus Torvalds ++fmt; 32231da177e4SLinus Torvalds 32241da177e4SLinus Torvalds /* skip this conversion. 32251da177e4SLinus Torvalds * advance both strings to next white space 32261da177e4SLinus Torvalds */ 32271da177e4SLinus Torvalds if (*fmt == '*') { 3228da99075cSJan Beulich if (!*str) 3229da99075cSJan Beulich break; 3230f9310b2fSJessica Yu while (!isspace(*fmt) && *fmt != '%' && *fmt) { 3231f9310b2fSJessica Yu /* '%*[' not yet supported, invalid format */ 3232f9310b2fSJessica Yu if (*fmt == '[') 3233f9310b2fSJessica Yu return num; 32341da177e4SLinus Torvalds fmt++; 3235f9310b2fSJessica Yu } 32361da177e4SLinus Torvalds while (!isspace(*str) && *str) 32371da177e4SLinus Torvalds str++; 32381da177e4SLinus Torvalds continue; 32391da177e4SLinus Torvalds } 32401da177e4SLinus Torvalds 32411da177e4SLinus Torvalds /* get field width */ 32421da177e4SLinus Torvalds field_width = -1; 324353809751SJan Beulich if (isdigit(*fmt)) { 32441da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 324553809751SJan Beulich if (field_width <= 0) 324653809751SJan Beulich break; 324753809751SJan Beulich } 32481da177e4SLinus Torvalds 32491da177e4SLinus Torvalds /* get conversion qualifier */ 32501da177e4SLinus Torvalds qualifier = -1; 325175fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 32525b5e0928SAlexey Dobriyan *fmt == 'z') { 32531da177e4SLinus Torvalds qualifier = *fmt++; 32541da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 32551da177e4SLinus Torvalds if (qualifier == 'h') { 32561da177e4SLinus Torvalds qualifier = 'H'; 32571da177e4SLinus Torvalds fmt++; 32581da177e4SLinus Torvalds } else if (qualifier == 'l') { 32591da177e4SLinus Torvalds qualifier = 'L'; 32601da177e4SLinus Torvalds fmt++; 32611da177e4SLinus Torvalds } 32621da177e4SLinus Torvalds } 32631da177e4SLinus Torvalds } 32641da177e4SLinus Torvalds 3265da99075cSJan Beulich if (!*fmt) 3266da99075cSJan Beulich break; 3267da99075cSJan Beulich 3268da99075cSJan Beulich if (*fmt == 'n') { 3269da99075cSJan Beulich /* return number of characters read so far */ 3270da99075cSJan Beulich *va_arg(args, int *) = str - buf; 3271da99075cSJan Beulich ++fmt; 3272da99075cSJan Beulich continue; 3273da99075cSJan Beulich } 3274da99075cSJan Beulich 3275da99075cSJan Beulich if (!*str) 32761da177e4SLinus Torvalds break; 32771da177e4SLinus Torvalds 3278d4be151bSAndré Goddard Rosa base = 10; 32793f623ebaSFabian Frederick is_sign = false; 3280d4be151bSAndré Goddard Rosa 32811da177e4SLinus Torvalds switch (*fmt++) { 32821da177e4SLinus Torvalds case 'c': 32831da177e4SLinus Torvalds { 32841da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 32851da177e4SLinus Torvalds if (field_width == -1) 32861da177e4SLinus Torvalds field_width = 1; 32871da177e4SLinus Torvalds do { 32881da177e4SLinus Torvalds *s++ = *str++; 32891da177e4SLinus Torvalds } while (--field_width > 0 && *str); 32901da177e4SLinus Torvalds num++; 32911da177e4SLinus Torvalds } 32921da177e4SLinus Torvalds continue; 32931da177e4SLinus Torvalds case 's': 32941da177e4SLinus Torvalds { 32951da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 32961da177e4SLinus Torvalds if (field_width == -1) 32974be929beSAlexey Dobriyan field_width = SHRT_MAX; 32981da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 3299e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 33001da177e4SLinus Torvalds 33011da177e4SLinus Torvalds /* now copy until next white space */ 33027b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 33031da177e4SLinus Torvalds *s++ = *str++; 33041da177e4SLinus Torvalds *s = '\0'; 33051da177e4SLinus Torvalds num++; 33061da177e4SLinus Torvalds } 33071da177e4SLinus Torvalds continue; 3308f9310b2fSJessica Yu /* 3309f9310b2fSJessica Yu * Warning: This implementation of the '[' conversion specifier 3310f9310b2fSJessica Yu * deviates from its glibc counterpart in the following ways: 3311f9310b2fSJessica Yu * (1) It does NOT support ranges i.e. '-' is NOT a special 3312f9310b2fSJessica Yu * character 3313f9310b2fSJessica Yu * (2) It cannot match the closing bracket ']' itself 3314f9310b2fSJessica Yu * (3) A field width is required 3315f9310b2fSJessica Yu * (4) '%*[' (discard matching input) is currently not supported 3316f9310b2fSJessica Yu * 3317f9310b2fSJessica Yu * Example usage: 3318f9310b2fSJessica Yu * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3319f9310b2fSJessica Yu * buf1, buf2, buf3); 3320f9310b2fSJessica Yu * if (ret < 3) 3321f9310b2fSJessica Yu * // etc.. 3322f9310b2fSJessica Yu */ 3323f9310b2fSJessica Yu case '[': 3324f9310b2fSJessica Yu { 3325f9310b2fSJessica Yu char *s = (char *)va_arg(args, char *); 3326f9310b2fSJessica Yu DECLARE_BITMAP(set, 256) = {0}; 3327f9310b2fSJessica Yu unsigned int len = 0; 3328f9310b2fSJessica Yu bool negate = (*fmt == '^'); 3329f9310b2fSJessica Yu 3330f9310b2fSJessica Yu /* field width is required */ 3331f9310b2fSJessica Yu if (field_width == -1) 3332f9310b2fSJessica Yu return num; 3333f9310b2fSJessica Yu 3334f9310b2fSJessica Yu if (negate) 3335f9310b2fSJessica Yu ++fmt; 3336f9310b2fSJessica Yu 3337f9310b2fSJessica Yu for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 3338f9310b2fSJessica Yu set_bit((u8)*fmt, set); 3339f9310b2fSJessica Yu 3340f9310b2fSJessica Yu /* no ']' or no character set found */ 3341f9310b2fSJessica Yu if (!*fmt || !len) 3342f9310b2fSJessica Yu return num; 3343f9310b2fSJessica Yu ++fmt; 3344f9310b2fSJessica Yu 3345f9310b2fSJessica Yu if (negate) { 3346f9310b2fSJessica Yu bitmap_complement(set, set, 256); 3347f9310b2fSJessica Yu /* exclude null '\0' byte */ 3348f9310b2fSJessica Yu clear_bit(0, set); 3349f9310b2fSJessica Yu } 3350f9310b2fSJessica Yu 3351f9310b2fSJessica Yu /* match must be non-empty */ 3352f9310b2fSJessica Yu if (!test_bit((u8)*str, set)) 3353f9310b2fSJessica Yu return num; 3354f9310b2fSJessica Yu 3355f9310b2fSJessica Yu while (test_bit((u8)*str, set) && field_width--) 3356f9310b2fSJessica Yu *s++ = *str++; 3357f9310b2fSJessica Yu *s = '\0'; 3358f9310b2fSJessica Yu ++num; 3359f9310b2fSJessica Yu } 3360f9310b2fSJessica Yu continue; 33611da177e4SLinus Torvalds case 'o': 33621da177e4SLinus Torvalds base = 8; 33631da177e4SLinus Torvalds break; 33641da177e4SLinus Torvalds case 'x': 33651da177e4SLinus Torvalds case 'X': 33661da177e4SLinus Torvalds base = 16; 33671da177e4SLinus Torvalds break; 33681da177e4SLinus Torvalds case 'i': 33691da177e4SLinus Torvalds base = 0; 33707e6bd6f3SAndy Shevchenko /* fall through */ 33711da177e4SLinus Torvalds case 'd': 33723f623ebaSFabian Frederick is_sign = true; 33737e6bd6f3SAndy Shevchenko /* fall through */ 33741da177e4SLinus Torvalds case 'u': 33751da177e4SLinus Torvalds break; 33761da177e4SLinus Torvalds case '%': 33771da177e4SLinus Torvalds /* looking for '%' in str */ 33781da177e4SLinus Torvalds if (*str++ != '%') 33791da177e4SLinus Torvalds return num; 33801da177e4SLinus Torvalds continue; 33811da177e4SLinus Torvalds default: 33821da177e4SLinus Torvalds /* invalid format; stop here */ 33831da177e4SLinus Torvalds return num; 33841da177e4SLinus Torvalds } 33851da177e4SLinus Torvalds 33861da177e4SLinus Torvalds /* have some sort of integer conversion. 33871da177e4SLinus Torvalds * first, skip white space in buffer. 33881da177e4SLinus Torvalds */ 3389e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 33901da177e4SLinus Torvalds 33911da177e4SLinus Torvalds digit = *str; 33921da177e4SLinus Torvalds if (is_sign && digit == '-') 33931da177e4SLinus Torvalds digit = *(str + 1); 33941da177e4SLinus Torvalds 33951da177e4SLinus Torvalds if (!digit 33961da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 33971da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 33981da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 33991da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 34001da177e4SLinus Torvalds break; 34011da177e4SLinus Torvalds 340253809751SJan Beulich if (is_sign) 340353809751SJan Beulich val.s = qualifier != 'L' ? 340453809751SJan Beulich simple_strtol(str, &next, base) : 340553809751SJan Beulich simple_strtoll(str, &next, base); 340653809751SJan Beulich else 340753809751SJan Beulich val.u = qualifier != 'L' ? 340853809751SJan Beulich simple_strtoul(str, &next, base) : 340953809751SJan Beulich simple_strtoull(str, &next, base); 341053809751SJan Beulich 341153809751SJan Beulich if (field_width > 0 && next - str > field_width) { 341253809751SJan Beulich if (base == 0) 341353809751SJan Beulich _parse_integer_fixup_radix(str, &base); 341453809751SJan Beulich while (next - str > field_width) { 341553809751SJan Beulich if (is_sign) 341653809751SJan Beulich val.s = div_s64(val.s, base); 341753809751SJan Beulich else 341853809751SJan Beulich val.u = div_u64(val.u, base); 341953809751SJan Beulich --next; 342053809751SJan Beulich } 342153809751SJan Beulich } 342253809751SJan Beulich 34231da177e4SLinus Torvalds switch (qualifier) { 34241da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 342553809751SJan Beulich if (is_sign) 342653809751SJan Beulich *va_arg(args, signed char *) = val.s; 342753809751SJan Beulich else 342853809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 34291da177e4SLinus Torvalds break; 34301da177e4SLinus Torvalds case 'h': 343153809751SJan Beulich if (is_sign) 343253809751SJan Beulich *va_arg(args, short *) = val.s; 343353809751SJan Beulich else 343453809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 34351da177e4SLinus Torvalds break; 34361da177e4SLinus Torvalds case 'l': 343753809751SJan Beulich if (is_sign) 343853809751SJan Beulich *va_arg(args, long *) = val.s; 343953809751SJan Beulich else 344053809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 34411da177e4SLinus Torvalds break; 34421da177e4SLinus Torvalds case 'L': 344353809751SJan Beulich if (is_sign) 344453809751SJan Beulich *va_arg(args, long long *) = val.s; 344553809751SJan Beulich else 344653809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 34471da177e4SLinus Torvalds break; 34481da177e4SLinus Torvalds case 'z': 344953809751SJan Beulich *va_arg(args, size_t *) = val.u; 34501da177e4SLinus Torvalds break; 34511da177e4SLinus Torvalds default: 345253809751SJan Beulich if (is_sign) 345353809751SJan Beulich *va_arg(args, int *) = val.s; 345453809751SJan Beulich else 345553809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 34561da177e4SLinus Torvalds break; 34571da177e4SLinus Torvalds } 34581da177e4SLinus Torvalds num++; 34591da177e4SLinus Torvalds 34601da177e4SLinus Torvalds if (!next) 34611da177e4SLinus Torvalds break; 34621da177e4SLinus Torvalds str = next; 34631da177e4SLinus Torvalds } 3464c6b40d16SJohannes Berg 34651da177e4SLinus Torvalds return num; 34661da177e4SLinus Torvalds } 34671da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 34681da177e4SLinus Torvalds 34691da177e4SLinus Torvalds /** 34701da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 34711da177e4SLinus Torvalds * @buf: input buffer 34721da177e4SLinus Torvalds * @fmt: formatting of buffer 34731da177e4SLinus Torvalds * @...: resulting arguments 34741da177e4SLinus Torvalds */ 34751da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 34761da177e4SLinus Torvalds { 34771da177e4SLinus Torvalds va_list args; 34781da177e4SLinus Torvalds int i; 34791da177e4SLinus Torvalds 34801da177e4SLinus Torvalds va_start(args, fmt); 34811da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 34821da177e4SLinus Torvalds va_end(args); 34837b9186f5SAndré Goddard Rosa 34841da177e4SLinus Torvalds return i; 34851da177e4SLinus Torvalds } 34861da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 3487