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> 377daac5b2SAndy Shevchenko #include <linux/time.h> 382b1b0d66SAndy Shevchenko #include <linux/uuid.h> 39ce4fecf1SPantelis Antoniou #include <linux/of.h> 408a27f7c9SJoe Perches #include <net/addrconf.h> 41ad67b74dSTobin C. Harding #include <linux/siphash.h> 42ad67b74dSTobin C. Harding #include <linux/compiler.h> 43a92eb762SSakari Ailus #include <linux/property.h> 441031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 451031bc58SDmitry Monakhov #include <linux/blkdev.h> 461031bc58SDmitry Monakhov #endif 471da177e4SLinus Torvalds 48edf14cdbSVlastimil Babka #include "../mm/internal.h" /* For the trace_print_flags arrays */ 49edf14cdbSVlastimil Babka 504e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 517c43d9a3SRasmus Villemoes #include <asm/byteorder.h> /* cpu_to_le16 */ 521da177e4SLinus Torvalds 5371dca95dSAndy Shevchenko #include <linux/string_helpers.h> 541dff46d6SAlexey Dobriyan #include "kstrtox.h" 55aa46a63eSHarvey Harrison 561da177e4SLinus Torvalds /** 571da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 581da177e4SLinus Torvalds * @cp: The start of the string 591da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 601da177e4SLinus Torvalds * @base: The number base to use 61462e4711SEldad Zack * 62e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoull instead. 631da177e4SLinus Torvalds */ 641da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 651da177e4SLinus Torvalds { 661dff46d6SAlexey Dobriyan unsigned long long result; 671dff46d6SAlexey Dobriyan unsigned int rv; 681da177e4SLinus Torvalds 691dff46d6SAlexey Dobriyan cp = _parse_integer_fixup_radix(cp, &base); 701dff46d6SAlexey Dobriyan rv = _parse_integer(cp, base, &result); 711dff46d6SAlexey Dobriyan /* FIXME */ 721dff46d6SAlexey Dobriyan cp += (rv & ~KSTRTOX_OVERFLOW); 73aa46a63eSHarvey Harrison 741da177e4SLinus Torvalds if (endp) 751da177e4SLinus Torvalds *endp = (char *)cp; 767b9186f5SAndré Goddard Rosa 771da177e4SLinus Torvalds return result; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /** 82922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 83922ac25cSAndré Goddard Rosa * @cp: The start of the string 84922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 85922ac25cSAndré Goddard Rosa * @base: The number base to use 86462e4711SEldad Zack * 87e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoul instead. 88922ac25cSAndré Goddard Rosa */ 89922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 90922ac25cSAndré Goddard Rosa { 91922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 92922ac25cSAndré Goddard Rosa } 93922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 94922ac25cSAndré Goddard Rosa 95922ac25cSAndré Goddard Rosa /** 96922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 97922ac25cSAndré Goddard Rosa * @cp: The start of the string 98922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 99922ac25cSAndré Goddard Rosa * @base: The number base to use 100462e4711SEldad Zack * 101e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtol instead. 102922ac25cSAndré Goddard Rosa */ 103922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 104922ac25cSAndré Goddard Rosa { 105922ac25cSAndré Goddard Rosa if (*cp == '-') 106922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 107922ac25cSAndré Goddard Rosa 108922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 109922ac25cSAndré Goddard Rosa } 110922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 111922ac25cSAndré Goddard Rosa 112922ac25cSAndré Goddard Rosa /** 1131da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 1141da177e4SLinus Torvalds * @cp: The start of the string 1151da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 1161da177e4SLinus Torvalds * @base: The number base to use 117462e4711SEldad Zack * 118e8cc2b97SAndy Shevchenko * This function has caveats. Please use kstrtoll instead. 1191da177e4SLinus Torvalds */ 1201da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds if (*cp == '-') 1231da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 1247b9186f5SAndré Goddard Rosa 1251da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 1261da177e4SLinus Torvalds } 12798d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1281da177e4SLinus Torvalds 129cf3b429bSJoe Perches static noinline_for_stack 130cf3b429bSJoe Perches int skip_atoi(const char **s) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds int i = 0; 1331da177e4SLinus Torvalds 13443e5b666SRasmus Villemoes do { 1351da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 13643e5b666SRasmus Villemoes } while (isdigit(**s)); 1377b9186f5SAndré Goddard Rosa 1381da177e4SLinus Torvalds return i; 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 1417c43d9a3SRasmus Villemoes /* 1427c43d9a3SRasmus Villemoes * Decimal conversion is by far the most typical, and is used for 1437c43d9a3SRasmus Villemoes * /proc and /sys data. This directly impacts e.g. top performance 1447c43d9a3SRasmus Villemoes * with many processes running. We optimize it for speed by emitting 1457c43d9a3SRasmus Villemoes * two characters at a time, using a 200 byte lookup table. This 1467c43d9a3SRasmus Villemoes * roughly halves the number of multiplications compared to computing 1477c43d9a3SRasmus Villemoes * the digits one at a time. Implementation strongly inspired by the 1487c43d9a3SRasmus Villemoes * previous version, which in turn used ideas described at 1497c43d9a3SRasmus Villemoes * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission 1507c43d9a3SRasmus Villemoes * from the author, Douglas W. Jones). 1517c43d9a3SRasmus Villemoes * 1527c43d9a3SRasmus Villemoes * It turns out there is precisely one 26 bit fixed-point 1537c43d9a3SRasmus Villemoes * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32 1547c43d9a3SRasmus Villemoes * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual 1557c43d9a3SRasmus Villemoes * range happens to be somewhat larger (x <= 1073741898), but that's 1567c43d9a3SRasmus Villemoes * irrelevant for our purpose. 1577c43d9a3SRasmus Villemoes * 1587c43d9a3SRasmus Villemoes * For dividing a number in the range [10^4, 10^6-1] by 100, we still 1597c43d9a3SRasmus Villemoes * need a 32x32->64 bit multiply, so we simply use the same constant. 1607c43d9a3SRasmus Villemoes * 1617c43d9a3SRasmus Villemoes * For dividing a number in the range [100, 10^4-1] by 100, there are 1627c43d9a3SRasmus Villemoes * several options. The simplest is (x * 0x147b) >> 19, which is valid 1637c43d9a3SRasmus Villemoes * for all x <= 43698. 164133fd9f5SDenys Vlasenko */ 1654277eeddSDenis Vlasenko 1667c43d9a3SRasmus Villemoes static const u16 decpair[100] = { 1677c43d9a3SRasmus Villemoes #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030) 1687c43d9a3SRasmus Villemoes _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9), 1697c43d9a3SRasmus Villemoes _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19), 1707c43d9a3SRasmus Villemoes _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29), 1717c43d9a3SRasmus Villemoes _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39), 1727c43d9a3SRasmus Villemoes _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49), 1737c43d9a3SRasmus Villemoes _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59), 1747c43d9a3SRasmus Villemoes _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69), 1757c43d9a3SRasmus Villemoes _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79), 1767c43d9a3SRasmus Villemoes _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89), 1777c43d9a3SRasmus Villemoes _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99), 1787c43d9a3SRasmus Villemoes #undef _ 1797c43d9a3SRasmus Villemoes }; 1804277eeddSDenis Vlasenko 1817b9186f5SAndré Goddard Rosa /* 1827c43d9a3SRasmus Villemoes * This will print a single '0' even if r == 0, since we would 183675cf53cSRasmus Villemoes * immediately jump to out_r where two 0s would be written but only 184675cf53cSRasmus Villemoes * one of them accounted for in buf. This is needed by ip4_string 185675cf53cSRasmus Villemoes * below. All other callers pass a non-zero value of r. 186133fd9f5SDenys Vlasenko */ 187133fd9f5SDenys Vlasenko static noinline_for_stack 188133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 189133fd9f5SDenys Vlasenko { 190133fd9f5SDenys Vlasenko unsigned q; 1914277eeddSDenis Vlasenko 1927c43d9a3SRasmus Villemoes /* 1 <= r < 10^8 */ 1937c43d9a3SRasmus Villemoes if (r < 100) 1947c43d9a3SRasmus Villemoes goto out_r; 195cb239d0aSGeorge Spelvin 1967c43d9a3SRasmus Villemoes /* 100 <= r < 10^8 */ 1977c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 1987c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 1997c43d9a3SRasmus Villemoes buf += 2; 2007c43d9a3SRasmus Villemoes 2017c43d9a3SRasmus Villemoes /* 1 <= q < 10^6 */ 2027c43d9a3SRasmus Villemoes if (q < 100) 2037c43d9a3SRasmus Villemoes goto out_q; 2047c43d9a3SRasmus Villemoes 2057c43d9a3SRasmus Villemoes /* 100 <= q < 10^6 */ 2067c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2077c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2087c43d9a3SRasmus Villemoes buf += 2; 2097c43d9a3SRasmus Villemoes 2107c43d9a3SRasmus Villemoes /* 1 <= r < 10^4 */ 2117c43d9a3SRasmus Villemoes if (r < 100) 2127c43d9a3SRasmus Villemoes goto out_r; 2137c43d9a3SRasmus Villemoes 2147c43d9a3SRasmus Villemoes /* 100 <= r < 10^4 */ 2157c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2167c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2177c43d9a3SRasmus Villemoes buf += 2; 2187c43d9a3SRasmus Villemoes out_q: 2197c43d9a3SRasmus Villemoes /* 1 <= q < 100 */ 2207c43d9a3SRasmus Villemoes r = q; 2217c43d9a3SRasmus Villemoes out_r: 2227c43d9a3SRasmus Villemoes /* 1 <= r < 100 */ 2237c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r]; 224675cf53cSRasmus Villemoes buf += r < 10 ? 1 : 2; 225133fd9f5SDenys Vlasenko return buf; 226133fd9f5SDenys Vlasenko } 227133fd9f5SDenys Vlasenko 2287c43d9a3SRasmus Villemoes #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64 2297c43d9a3SRasmus Villemoes static noinline_for_stack 2307c43d9a3SRasmus Villemoes char *put_dec_full8(char *buf, unsigned r) 2317c43d9a3SRasmus Villemoes { 2327c43d9a3SRasmus Villemoes unsigned q; 233133fd9f5SDenys Vlasenko 2347c43d9a3SRasmus Villemoes /* 0 <= r < 10^8 */ 2357c43d9a3SRasmus Villemoes q = (r * (u64)0x28f5c29) >> 32; 2367c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2377c43d9a3SRasmus Villemoes buf += 2; 238133fd9f5SDenys Vlasenko 2397c43d9a3SRasmus Villemoes /* 0 <= q < 10^6 */ 2407c43d9a3SRasmus Villemoes r = (q * (u64)0x28f5c29) >> 32; 2417c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q - 100*r]; 2427c43d9a3SRasmus Villemoes buf += 2; 243133fd9f5SDenys Vlasenko 2447c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2457c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2467c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2477c43d9a3SRasmus Villemoes buf += 2; 2487c43d9a3SRasmus Villemoes 2497c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2507c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2517c43d9a3SRasmus Villemoes buf += 2; 2527c43d9a3SRasmus Villemoes return buf; 2537c43d9a3SRasmus Villemoes } 2547c43d9a3SRasmus Villemoes 2557c43d9a3SRasmus Villemoes static noinline_for_stack 256133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 257133fd9f5SDenys Vlasenko { 258133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 2597c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2607c43d9a3SRasmus Villemoes /* 1 <= n <= 1.6e11 */ 2617c43d9a3SRasmus Villemoes if (n >= 100*1000*1000) 2627c43d9a3SRasmus Villemoes buf = put_dec_full8(buf, do_div(n, 100*1000*1000)); 2637c43d9a3SRasmus Villemoes /* 1 <= n < 1e8 */ 264133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 265133fd9f5SDenys Vlasenko } 266133fd9f5SDenys Vlasenko 2677c43d9a3SRasmus Villemoes #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64 268133fd9f5SDenys Vlasenko 2697c43d9a3SRasmus Villemoes static void 2707c43d9a3SRasmus Villemoes put_dec_full4(char *buf, unsigned r) 271133fd9f5SDenys Vlasenko { 2727c43d9a3SRasmus Villemoes unsigned q; 2737c43d9a3SRasmus Villemoes 2747c43d9a3SRasmus Villemoes /* 0 <= r < 10^4 */ 2757c43d9a3SRasmus Villemoes q = (r * 0x147b) >> 19; 2767c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[r - 100*q]; 2777c43d9a3SRasmus Villemoes buf += 2; 2787c43d9a3SRasmus Villemoes /* 0 <= q < 100 */ 2797c43d9a3SRasmus Villemoes *((u16 *)buf) = decpair[q]; 2802359172aSGeorge Spelvin } 2812359172aSGeorge Spelvin 2822359172aSGeorge Spelvin /* 2832359172aSGeorge Spelvin * Call put_dec_full4 on x % 10000, return x / 10000. 2842359172aSGeorge Spelvin * The approximation x/10000 == (x * 0x346DC5D7) >> 43 2852359172aSGeorge Spelvin * holds for all x < 1,128,869,999. The largest value this 2862359172aSGeorge Spelvin * helper will ever be asked to convert is 1,125,520,955. 2877c43d9a3SRasmus Villemoes * (second call in the put_dec code, assuming n is all-ones). 2882359172aSGeorge Spelvin */ 2897c43d9a3SRasmus Villemoes static noinline_for_stack 2902359172aSGeorge Spelvin unsigned put_dec_helper4(char *buf, unsigned x) 2912359172aSGeorge Spelvin { 2922359172aSGeorge Spelvin uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43; 2932359172aSGeorge Spelvin 2942359172aSGeorge Spelvin put_dec_full4(buf, x - q * 10000); 2952359172aSGeorge Spelvin return q; 296133fd9f5SDenys Vlasenko } 297133fd9f5SDenys Vlasenko 298133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 299133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 300133fd9f5SDenys Vlasenko * (with permission from the author). 301133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 302133fd9f5SDenys Vlasenko */ 303133fd9f5SDenys Vlasenko static 304133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 305133fd9f5SDenys Vlasenko { 306133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 307133fd9f5SDenys Vlasenko 308133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 309133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 310133fd9f5SDenys Vlasenko 311133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 312133fd9f5SDenys Vlasenko h = (n >> 32); 313133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 314133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 315133fd9f5SDenys Vlasenko 3167c43d9a3SRasmus Villemoes /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0 3177c43d9a3SRasmus Villemoes = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ 318133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 3192359172aSGeorge Spelvin q = put_dec_helper4(buf, q); 320133fd9f5SDenys Vlasenko 3212359172aSGeorge Spelvin q += 7671 * d3 + 9496 * d2 + 6 * d1; 3222359172aSGeorge Spelvin q = put_dec_helper4(buf+4, q); 323133fd9f5SDenys Vlasenko 3242359172aSGeorge Spelvin q += 4749 * d3 + 42 * d2; 3252359172aSGeorge Spelvin q = put_dec_helper4(buf+8, q); 326133fd9f5SDenys Vlasenko 3272359172aSGeorge Spelvin q += 281 * d3; 3282359172aSGeorge Spelvin buf += 12; 3292359172aSGeorge Spelvin if (q) 3302359172aSGeorge Spelvin buf = put_dec_trunc8(buf, q); 3312359172aSGeorge Spelvin else while (buf[-1] == '0') 332133fd9f5SDenys Vlasenko --buf; 3337b9186f5SAndré Goddard Rosa 3344277eeddSDenis Vlasenko return buf; 3354277eeddSDenis Vlasenko } 336133fd9f5SDenys Vlasenko 337133fd9f5SDenys Vlasenko #endif 3384277eeddSDenis Vlasenko 3391ac101a5SKAMEZAWA Hiroyuki /* 3401ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3411ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3421ac101a5SKAMEZAWA Hiroyuki * 3431ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3441ac101a5SKAMEZAWA Hiroyuki */ 345d1be35cbSAndrei Vagin int num_to_str(char *buf, int size, unsigned long long num, unsigned int width) 3461ac101a5SKAMEZAWA Hiroyuki { 3477c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 3487c43d9a3SRasmus Villemoes char tmp[sizeof(num) * 3] __aligned(2); 3491ac101a5SKAMEZAWA Hiroyuki int idx, len; 3501ac101a5SKAMEZAWA Hiroyuki 351133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 352133fd9f5SDenys Vlasenko if (num <= 9) { 353133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 354133fd9f5SDenys Vlasenko len = 1; 355133fd9f5SDenys Vlasenko } else { 3561ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 357133fd9f5SDenys Vlasenko } 3581ac101a5SKAMEZAWA Hiroyuki 359d1be35cbSAndrei Vagin if (len > size || width > size) 3601ac101a5SKAMEZAWA Hiroyuki return 0; 361d1be35cbSAndrei Vagin 362d1be35cbSAndrei Vagin if (width > len) { 363d1be35cbSAndrei Vagin width = width - len; 364d1be35cbSAndrei Vagin for (idx = 0; idx < width; idx++) 365d1be35cbSAndrei Vagin buf[idx] = ' '; 366d1be35cbSAndrei Vagin } else { 367d1be35cbSAndrei Vagin width = 0; 368d1be35cbSAndrei Vagin } 369d1be35cbSAndrei Vagin 3701ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 371d1be35cbSAndrei Vagin buf[idx + width] = tmp[len - idx - 1]; 372d1be35cbSAndrei Vagin 373d1be35cbSAndrei Vagin return len + width; 3741ac101a5SKAMEZAWA Hiroyuki } 3751ac101a5SKAMEZAWA Hiroyuki 37651be17dfSRasmus Villemoes #define SIGN 1 /* unsigned/signed, must be 1 */ 377d1c1b121SRasmus Villemoes #define LEFT 2 /* left justified */ 3781da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3791da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 380d1c1b121SRasmus Villemoes #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */ 381b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 382b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 3831da177e4SLinus Torvalds 384b886690dSAndy Shevchenko static_assert(ZEROPAD == ('0' - ' ')); 385b886690dSAndy Shevchenko static_assert(SMALL == ' '); 386b886690dSAndy Shevchenko 387fef20d9cSFrederic Weisbecker enum format_type { 388fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 389ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 390fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 391fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 392fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 393fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 394fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 395fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 396fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 397fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 398fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 399a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 400a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 401fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 402fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 403fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 404fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 405fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 406fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 407fef20d9cSFrederic Weisbecker }; 408fef20d9cSFrederic Weisbecker 409fef20d9cSFrederic Weisbecker struct printf_spec { 410d0484193SRasmus Villemoes unsigned int type:8; /* format_type enum */ 411d0484193SRasmus Villemoes signed int field_width:24; /* width of output field */ 412d0484193SRasmus Villemoes unsigned int flags:8; /* flags to number() */ 413d0484193SRasmus Villemoes unsigned int base:8; /* number base, 8, 10 or 16 only */ 414d0484193SRasmus Villemoes signed int precision:16; /* # of digits/chars */ 415d0484193SRasmus Villemoes } __packed; 416ef27ac18SRasmus Villemoes static_assert(sizeof(struct printf_spec) == 8); 417ef27ac18SRasmus Villemoes 4184d72ba01SRasmus Villemoes #define FIELD_WIDTH_MAX ((1 << 23) - 1) 4194d72ba01SRasmus Villemoes #define PRECISION_MAX ((1 << 15) - 1) 420fef20d9cSFrederic Weisbecker 421cf3b429bSJoe Perches static noinline_for_stack 422cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 423fef20d9cSFrederic Weisbecker struct printf_spec spec) 4241da177e4SLinus Torvalds { 4257c43d9a3SRasmus Villemoes /* put_dec requires 2-byte alignment of the buffer. */ 4267c43d9a3SRasmus Villemoes char tmp[3 * sizeof(num)] __aligned(2); 4279b706aeeSDenys Vlasenko char sign; 4289b706aeeSDenys Vlasenko char locase; 429fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 4301da177e4SLinus Torvalds int i; 4317c203422SPierre Carrier bool is_zero = num == 0LL; 4321c7a8e62SRasmus Villemoes int field_width = spec.field_width; 4331c7a8e62SRasmus Villemoes int precision = spec.precision; 4341da177e4SLinus Torvalds 4359b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 4369b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 437fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 438fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 439fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 4401da177e4SLinus Torvalds sign = 0; 441fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 4421da177e4SLinus Torvalds if ((signed long long)num < 0) { 4431da177e4SLinus Torvalds sign = '-'; 4441da177e4SLinus Torvalds num = -(signed long long)num; 4451c7a8e62SRasmus Villemoes field_width--; 446fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4471da177e4SLinus Torvalds sign = '+'; 4481c7a8e62SRasmus Villemoes field_width--; 449fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4501da177e4SLinus Torvalds sign = ' '; 4511c7a8e62SRasmus Villemoes field_width--; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds } 454b39a7340SDenis Vlasenko if (need_pfx) { 455fef20d9cSFrederic Weisbecker if (spec.base == 16) 4561c7a8e62SRasmus Villemoes field_width -= 2; 4577c203422SPierre Carrier else if (!is_zero) 4581c7a8e62SRasmus Villemoes field_width--; 4591da177e4SLinus Torvalds } 460b39a7340SDenis Vlasenko 461b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4621da177e4SLinus Torvalds i = 0; 463133fd9f5SDenys Vlasenko if (num < spec.base) 4643ea8d440SRasmus Villemoes tmp[i++] = hex_asc_upper[num] | locase; 465fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 466fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 467b39a7340SDenis Vlasenko int shift = 3; 4687b9186f5SAndré Goddard Rosa 4697b9186f5SAndré Goddard Rosa if (spec.base == 16) 4707b9186f5SAndré Goddard Rosa shift = 4; 471b39a7340SDenis Vlasenko do { 4723ea8d440SRasmus Villemoes tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase); 473b39a7340SDenis Vlasenko num >>= shift; 474b39a7340SDenis Vlasenko } while (num); 4754277eeddSDenis Vlasenko } else { /* base 10 */ 4764277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4774277eeddSDenis Vlasenko } 478b39a7340SDenis Vlasenko 479b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 4801c7a8e62SRasmus Villemoes if (i > precision) 4811c7a8e62SRasmus Villemoes precision = i; 482b39a7340SDenis Vlasenko /* leading space padding */ 4831c7a8e62SRasmus Villemoes field_width -= precision; 48451be17dfSRasmus Villemoes if (!(spec.flags & (ZEROPAD | LEFT))) { 4851c7a8e62SRasmus Villemoes while (--field_width >= 0) { 486f796937aSJeremy Fitzhardinge if (buf < end) 4871da177e4SLinus Torvalds *buf = ' '; 4881da177e4SLinus Torvalds ++buf; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds } 491b39a7340SDenis Vlasenko /* sign */ 4921da177e4SLinus Torvalds if (sign) { 493f796937aSJeremy Fitzhardinge if (buf < end) 4941da177e4SLinus Torvalds *buf = sign; 4951da177e4SLinus Torvalds ++buf; 4961da177e4SLinus Torvalds } 497b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 498b39a7340SDenis Vlasenko if (need_pfx) { 4997c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 500f796937aSJeremy Fitzhardinge if (buf < end) 5011da177e4SLinus Torvalds *buf = '0'; 5021da177e4SLinus Torvalds ++buf; 5037c203422SPierre Carrier } 504fef20d9cSFrederic Weisbecker if (spec.base == 16) { 505f796937aSJeremy Fitzhardinge if (buf < end) 5069b706aeeSDenys Vlasenko *buf = ('X' | locase); 5071da177e4SLinus Torvalds ++buf; 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds } 510b39a7340SDenis Vlasenko /* zero or space padding */ 511fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 512d1c1b121SRasmus Villemoes char c = ' ' + (spec.flags & ZEROPAD); 513b886690dSAndy Shevchenko 5141c7a8e62SRasmus Villemoes while (--field_width >= 0) { 515f796937aSJeremy Fitzhardinge if (buf < end) 5161da177e4SLinus Torvalds *buf = c; 5171da177e4SLinus Torvalds ++buf; 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds } 520b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 5211c7a8e62SRasmus Villemoes while (i <= --precision) { 522f796937aSJeremy Fitzhardinge if (buf < end) 5231da177e4SLinus Torvalds *buf = '0'; 5241da177e4SLinus Torvalds ++buf; 5251da177e4SLinus Torvalds } 526b39a7340SDenis Vlasenko /* actual digits of result */ 527b39a7340SDenis Vlasenko while (--i >= 0) { 528f796937aSJeremy Fitzhardinge if (buf < end) 5291da177e4SLinus Torvalds *buf = tmp[i]; 5301da177e4SLinus Torvalds ++buf; 5311da177e4SLinus Torvalds } 532b39a7340SDenis Vlasenko /* trailing space padding */ 5331c7a8e62SRasmus Villemoes while (--field_width >= 0) { 534f796937aSJeremy Fitzhardinge if (buf < end) 5351da177e4SLinus Torvalds *buf = ' '; 5361da177e4SLinus Torvalds ++buf; 5371da177e4SLinus Torvalds } 5387b9186f5SAndré Goddard Rosa 5391da177e4SLinus Torvalds return buf; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5423cab1e71SAndy Shevchenko static noinline_for_stack 5433cab1e71SAndy Shevchenko char *special_hex_number(char *buf, char *end, unsigned long long num, int size) 5443cab1e71SAndy Shevchenko { 5453cab1e71SAndy Shevchenko struct printf_spec spec; 5463cab1e71SAndy Shevchenko 5473cab1e71SAndy Shevchenko spec.type = FORMAT_TYPE_PTR; 5483cab1e71SAndy Shevchenko spec.field_width = 2 + 2 * size; /* 0x + hex */ 5493cab1e71SAndy Shevchenko spec.flags = SPECIAL | SMALL | ZEROPAD; 5503cab1e71SAndy Shevchenko spec.base = 16; 5513cab1e71SAndy Shevchenko spec.precision = -1; 5523cab1e71SAndy Shevchenko 5533cab1e71SAndy Shevchenko return number(buf, end, num, spec); 5543cab1e71SAndy Shevchenko } 5553cab1e71SAndy Shevchenko 556cfccde04SRasmus Villemoes static void move_right(char *buf, char *end, unsigned len, unsigned spaces) 5574b6ccca7SAl Viro { 5584b6ccca7SAl Viro size_t size; 5594b6ccca7SAl Viro if (buf >= end) /* nowhere to put anything */ 5604b6ccca7SAl Viro return; 5614b6ccca7SAl Viro size = end - buf; 5624b6ccca7SAl Viro if (size <= spaces) { 5634b6ccca7SAl Viro memset(buf, ' ', size); 5644b6ccca7SAl Viro return; 5654b6ccca7SAl Viro } 5664b6ccca7SAl Viro if (len) { 5674b6ccca7SAl Viro if (len > size - spaces) 5684b6ccca7SAl Viro len = size - spaces; 5694b6ccca7SAl Viro memmove(buf + spaces, buf, len); 5704b6ccca7SAl Viro } 5714b6ccca7SAl Viro memset(buf, ' ', spaces); 5724b6ccca7SAl Viro } 5734b6ccca7SAl Viro 574cfccde04SRasmus Villemoes /* 575cfccde04SRasmus Villemoes * Handle field width padding for a string. 576cfccde04SRasmus Villemoes * @buf: current buffer position 577cfccde04SRasmus Villemoes * @n: length of string 578cfccde04SRasmus Villemoes * @end: end of output buffer 579cfccde04SRasmus Villemoes * @spec: for field width and flags 580cfccde04SRasmus Villemoes * Returns: new buffer position after padding. 581cfccde04SRasmus Villemoes */ 582cfccde04SRasmus Villemoes static noinline_for_stack 583cfccde04SRasmus Villemoes char *widen_string(char *buf, int n, char *end, struct printf_spec spec) 584cfccde04SRasmus Villemoes { 585cfccde04SRasmus Villemoes unsigned spaces; 586cfccde04SRasmus Villemoes 587cfccde04SRasmus Villemoes if (likely(n >= spec.field_width)) 588cfccde04SRasmus Villemoes return buf; 589cfccde04SRasmus Villemoes /* we want to pad the sucker */ 590cfccde04SRasmus Villemoes spaces = spec.field_width - n; 591cfccde04SRasmus Villemoes if (!(spec.flags & LEFT)) { 592cfccde04SRasmus Villemoes move_right(buf - n, end, n, spaces); 593cfccde04SRasmus Villemoes return buf + spaces; 594cfccde04SRasmus Villemoes } 595cfccde04SRasmus Villemoes while (spaces--) { 596cfccde04SRasmus Villemoes if (buf < end) 597cfccde04SRasmus Villemoes *buf = ' '; 598cfccde04SRasmus Villemoes ++buf; 599cfccde04SRasmus Villemoes } 600cfccde04SRasmus Villemoes return buf; 601cfccde04SRasmus Villemoes } 602cfccde04SRasmus Villemoes 603d529ac41SPetr Mladek /* Handle string from a well known address. */ 604d529ac41SPetr Mladek static char *string_nocheck(char *buf, char *end, const char *s, 605d529ac41SPetr Mladek struct printf_spec spec) 60695508cfaSRasmus Villemoes { 60734fc8b90SRasmus Villemoes int len = 0; 608b314dd49SYoungmin Nam int lim = spec.precision; 60995508cfaSRasmus Villemoes 61034fc8b90SRasmus Villemoes while (lim--) { 61134fc8b90SRasmus Villemoes char c = *s++; 61234fc8b90SRasmus Villemoes if (!c) 61334fc8b90SRasmus Villemoes break; 61495508cfaSRasmus Villemoes if (buf < end) 61534fc8b90SRasmus Villemoes *buf = c; 61695508cfaSRasmus Villemoes ++buf; 61734fc8b90SRasmus Villemoes ++len; 61895508cfaSRasmus Villemoes } 61934fc8b90SRasmus Villemoes return widen_string(buf, len, end, spec); 62095508cfaSRasmus Villemoes } 62195508cfaSRasmus Villemoes 62257f5677eSRasmus Villemoes static char *err_ptr(char *buf, char *end, void *ptr, 62357f5677eSRasmus Villemoes struct printf_spec spec) 62457f5677eSRasmus Villemoes { 62557f5677eSRasmus Villemoes int err = PTR_ERR(ptr); 62657f5677eSRasmus Villemoes const char *sym = errname(err); 62757f5677eSRasmus Villemoes 62857f5677eSRasmus Villemoes if (sym) 62957f5677eSRasmus Villemoes return string_nocheck(buf, end, sym, spec); 63057f5677eSRasmus Villemoes 63157f5677eSRasmus Villemoes /* 63257f5677eSRasmus Villemoes * Somebody passed ERR_PTR(-1234) or some other non-existing 63357f5677eSRasmus Villemoes * Efoo - or perhaps CONFIG_SYMBOLIC_ERRNAME=n. Fall back to 63457f5677eSRasmus Villemoes * printing it as its decimal representation. 63557f5677eSRasmus Villemoes */ 63657f5677eSRasmus Villemoes spec.flags |= SIGN; 63757f5677eSRasmus Villemoes spec.base = 10; 63857f5677eSRasmus Villemoes return number(buf, end, err, spec); 63957f5677eSRasmus Villemoes } 64057f5677eSRasmus Villemoes 641c8c3b584SPetr Mladek /* Be careful: error messages must fit into the given buffer. */ 642c8c3b584SPetr Mladek static char *error_string(char *buf, char *end, const char *s, 643c8c3b584SPetr Mladek struct printf_spec spec) 644c8c3b584SPetr Mladek { 645c8c3b584SPetr Mladek /* 646c8c3b584SPetr Mladek * Hard limit to avoid a completely insane messages. It actually 647c8c3b584SPetr Mladek * works pretty well because most error messages are in 648c8c3b584SPetr Mladek * the many pointer format modifiers. 649c8c3b584SPetr Mladek */ 650c8c3b584SPetr Mladek if (spec.precision == -1) 651c8c3b584SPetr Mladek spec.precision = 2 * sizeof(void *); 652c8c3b584SPetr Mladek 653c8c3b584SPetr Mladek return string_nocheck(buf, end, s, spec); 654c8c3b584SPetr Mladek } 655c8c3b584SPetr Mladek 6563e5903ebSPetr Mladek /* 6572ac5a3bfSPetr Mladek * Do not call any complex external code here. Nested printk()/vsprintf() 6582ac5a3bfSPetr Mladek * might cause infinite loops. Failures might break printk() and would 6592ac5a3bfSPetr Mladek * be hard to debug. 6603e5903ebSPetr Mladek */ 6613e5903ebSPetr Mladek static const char *check_pointer_msg(const void *ptr) 6623e5903ebSPetr Mladek { 6633e5903ebSPetr Mladek if (!ptr) 6643e5903ebSPetr Mladek return "(null)"; 6653e5903ebSPetr Mladek 6662ac5a3bfSPetr Mladek if ((unsigned long)ptr < PAGE_SIZE || IS_ERR_VALUE(ptr)) 6673e5903ebSPetr Mladek return "(efault)"; 6683e5903ebSPetr Mladek 6693e5903ebSPetr Mladek return NULL; 6703e5903ebSPetr Mladek } 6713e5903ebSPetr Mladek 6723e5903ebSPetr Mladek static int check_pointer(char **buf, char *end, const void *ptr, 6733e5903ebSPetr Mladek struct printf_spec spec) 6743e5903ebSPetr Mladek { 6753e5903ebSPetr Mladek const char *err_msg; 6763e5903ebSPetr Mladek 6773e5903ebSPetr Mladek err_msg = check_pointer_msg(ptr); 6783e5903ebSPetr Mladek if (err_msg) { 679c8c3b584SPetr Mladek *buf = error_string(*buf, end, err_msg, spec); 6803e5903ebSPetr Mladek return -EFAULT; 6813e5903ebSPetr Mladek } 6823e5903ebSPetr Mladek 6833e5903ebSPetr Mladek return 0; 6843e5903ebSPetr Mladek } 6853e5903ebSPetr Mladek 68695508cfaSRasmus Villemoes static noinline_for_stack 687d529ac41SPetr Mladek char *string(char *buf, char *end, const char *s, 688d529ac41SPetr Mladek struct printf_spec spec) 689d529ac41SPetr Mladek { 6903e5903ebSPetr Mladek if (check_pointer(&buf, end, s, spec)) 6913e5903ebSPetr Mladek return buf; 692d529ac41SPetr Mladek 693d529ac41SPetr Mladek return string_nocheck(buf, end, s, spec); 694d529ac41SPetr Mladek } 695d529ac41SPetr Mladek 696ce9d3eceSYueHaibing static char *pointer_string(char *buf, char *end, 697ce9d3eceSYueHaibing const void *ptr, 6989073dac1SGeert Uytterhoeven struct printf_spec spec) 6999073dac1SGeert Uytterhoeven { 7009073dac1SGeert Uytterhoeven spec.base = 16; 7019073dac1SGeert Uytterhoeven spec.flags |= SMALL; 7029073dac1SGeert Uytterhoeven if (spec.field_width == -1) { 7039073dac1SGeert Uytterhoeven spec.field_width = 2 * sizeof(ptr); 7049073dac1SGeert Uytterhoeven spec.flags |= ZEROPAD; 7059073dac1SGeert Uytterhoeven } 7069073dac1SGeert Uytterhoeven 7079073dac1SGeert Uytterhoeven return number(buf, end, (unsigned long int)ptr, spec); 7089073dac1SGeert Uytterhoeven } 7099073dac1SGeert Uytterhoeven 7109073dac1SGeert Uytterhoeven /* Make pointers available for printing early in the boot sequence. */ 7119073dac1SGeert Uytterhoeven static int debug_boot_weak_hash __ro_after_init; 7129073dac1SGeert Uytterhoeven 7139073dac1SGeert Uytterhoeven static int __init debug_boot_weak_hash_enable(char *str) 7149073dac1SGeert Uytterhoeven { 7159073dac1SGeert Uytterhoeven debug_boot_weak_hash = 1; 7169073dac1SGeert Uytterhoeven pr_info("debug_boot_weak_hash enabled\n"); 7179073dac1SGeert Uytterhoeven return 0; 7189073dac1SGeert Uytterhoeven } 7199073dac1SGeert Uytterhoeven early_param("debug_boot_weak_hash", debug_boot_weak_hash_enable); 7209073dac1SGeert Uytterhoeven 7219073dac1SGeert Uytterhoeven static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key); 7229073dac1SGeert Uytterhoeven static siphash_key_t ptr_key __read_mostly; 7239073dac1SGeert Uytterhoeven 7249073dac1SGeert Uytterhoeven static void enable_ptr_key_workfn(struct work_struct *work) 7259073dac1SGeert Uytterhoeven { 7269073dac1SGeert Uytterhoeven get_random_bytes(&ptr_key, sizeof(ptr_key)); 7279073dac1SGeert Uytterhoeven /* Needs to run from preemptible context */ 7289073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 7299073dac1SGeert Uytterhoeven } 7309073dac1SGeert Uytterhoeven 7319073dac1SGeert Uytterhoeven static DECLARE_WORK(enable_ptr_key_work, enable_ptr_key_workfn); 7329073dac1SGeert Uytterhoeven 7339073dac1SGeert Uytterhoeven static void fill_random_ptr_key(struct random_ready_callback *unused) 7349073dac1SGeert Uytterhoeven { 7359073dac1SGeert Uytterhoeven /* This may be in an interrupt handler. */ 7369073dac1SGeert Uytterhoeven queue_work(system_unbound_wq, &enable_ptr_key_work); 7379073dac1SGeert Uytterhoeven } 7389073dac1SGeert Uytterhoeven 7399073dac1SGeert Uytterhoeven static struct random_ready_callback random_ready = { 7409073dac1SGeert Uytterhoeven .func = fill_random_ptr_key 7419073dac1SGeert Uytterhoeven }; 7429073dac1SGeert Uytterhoeven 7439073dac1SGeert Uytterhoeven static int __init initialize_ptr_random(void) 7449073dac1SGeert Uytterhoeven { 7459073dac1SGeert Uytterhoeven int key_size = sizeof(ptr_key); 7469073dac1SGeert Uytterhoeven int ret; 7479073dac1SGeert Uytterhoeven 7489073dac1SGeert Uytterhoeven /* Use hw RNG if available. */ 7499073dac1SGeert Uytterhoeven if (get_random_bytes_arch(&ptr_key, key_size) == key_size) { 7509073dac1SGeert Uytterhoeven static_branch_disable(¬_filled_random_ptr_key); 7519073dac1SGeert Uytterhoeven return 0; 7529073dac1SGeert Uytterhoeven } 7539073dac1SGeert Uytterhoeven 7549073dac1SGeert Uytterhoeven ret = add_random_ready_callback(&random_ready); 7559073dac1SGeert Uytterhoeven if (!ret) { 7569073dac1SGeert Uytterhoeven return 0; 7579073dac1SGeert Uytterhoeven } else if (ret == -EALREADY) { 7589073dac1SGeert Uytterhoeven /* This is in preemptible context */ 7599073dac1SGeert Uytterhoeven enable_ptr_key_workfn(&enable_ptr_key_work); 7609073dac1SGeert Uytterhoeven return 0; 7619073dac1SGeert Uytterhoeven } 7629073dac1SGeert Uytterhoeven 7639073dac1SGeert Uytterhoeven return ret; 7649073dac1SGeert Uytterhoeven } 7659073dac1SGeert Uytterhoeven early_initcall(initialize_ptr_random); 7669073dac1SGeert Uytterhoeven 7679073dac1SGeert Uytterhoeven /* Maps a pointer to a 32 bit unique identifier. */ 768e4dcad20SJoel Fernandes (Google) static inline int __ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 7699073dac1SGeert Uytterhoeven { 7709073dac1SGeert Uytterhoeven unsigned long hashval; 7719073dac1SGeert Uytterhoeven 772e4dcad20SJoel Fernandes (Google) if (static_branch_unlikely(¬_filled_random_ptr_key)) 773e4dcad20SJoel Fernandes (Google) return -EAGAIN; 7749073dac1SGeert Uytterhoeven 7759073dac1SGeert Uytterhoeven #ifdef CONFIG_64BIT 7769073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u64((u64)ptr, &ptr_key); 7779073dac1SGeert Uytterhoeven /* 7789073dac1SGeert Uytterhoeven * Mask off the first 32 bits, this makes explicit that we have 7799073dac1SGeert Uytterhoeven * modified the address (and 32 bits is plenty for a unique ID). 7809073dac1SGeert Uytterhoeven */ 7819073dac1SGeert Uytterhoeven hashval = hashval & 0xffffffff; 7829073dac1SGeert Uytterhoeven #else 7839073dac1SGeert Uytterhoeven hashval = (unsigned long)siphash_1u32((u32)ptr, &ptr_key); 7849073dac1SGeert Uytterhoeven #endif 785e4dcad20SJoel Fernandes (Google) *hashval_out = hashval; 786e4dcad20SJoel Fernandes (Google) return 0; 787e4dcad20SJoel Fernandes (Google) } 788e4dcad20SJoel Fernandes (Google) 789e4dcad20SJoel Fernandes (Google) int ptr_to_hashval(const void *ptr, unsigned long *hashval_out) 790e4dcad20SJoel Fernandes (Google) { 791e4dcad20SJoel Fernandes (Google) return __ptr_to_hashval(ptr, hashval_out); 792e4dcad20SJoel Fernandes (Google) } 793e4dcad20SJoel Fernandes (Google) 794e4dcad20SJoel Fernandes (Google) static char *ptr_to_id(char *buf, char *end, const void *ptr, 795e4dcad20SJoel Fernandes (Google) struct printf_spec spec) 796e4dcad20SJoel Fernandes (Google) { 797e4dcad20SJoel Fernandes (Google) const char *str = sizeof(ptr) == 8 ? "(____ptrval____)" : "(ptrval)"; 798e4dcad20SJoel Fernandes (Google) unsigned long hashval; 799e4dcad20SJoel Fernandes (Google) int ret; 800e4dcad20SJoel Fernandes (Google) 8017bd57fbcSIlya Dryomov /* 8027bd57fbcSIlya Dryomov * Print the real pointer value for NULL and error pointers, 8037bd57fbcSIlya Dryomov * as they are not actual addresses. 8047bd57fbcSIlya Dryomov */ 8057bd57fbcSIlya Dryomov if (IS_ERR_OR_NULL(ptr)) 8067bd57fbcSIlya Dryomov return pointer_string(buf, end, ptr, spec); 8077bd57fbcSIlya Dryomov 808e4dcad20SJoel Fernandes (Google) /* When debugging early boot use non-cryptographically secure hash. */ 809e4dcad20SJoel Fernandes (Google) if (unlikely(debug_boot_weak_hash)) { 810e4dcad20SJoel Fernandes (Google) hashval = hash_long((unsigned long)ptr, 32); 811e4dcad20SJoel Fernandes (Google) return pointer_string(buf, end, (const void *)hashval, spec); 812e4dcad20SJoel Fernandes (Google) } 813e4dcad20SJoel Fernandes (Google) 814e4dcad20SJoel Fernandes (Google) ret = __ptr_to_hashval(ptr, &hashval); 815e4dcad20SJoel Fernandes (Google) if (ret) { 816e4dcad20SJoel Fernandes (Google) spec.field_width = 2 * sizeof(ptr); 817e4dcad20SJoel Fernandes (Google) /* string length must be less than default_width */ 818e4dcad20SJoel Fernandes (Google) return error_string(buf, end, str, spec); 819e4dcad20SJoel Fernandes (Google) } 820e4dcad20SJoel Fernandes (Google) 8219073dac1SGeert Uytterhoeven return pointer_string(buf, end, (const void *)hashval, spec); 8229073dac1SGeert Uytterhoeven } 8239073dac1SGeert Uytterhoeven 8246eea242fSPetr Mladek int kptr_restrict __read_mostly; 8256eea242fSPetr Mladek 8266eea242fSPetr Mladek static noinline_for_stack 8276eea242fSPetr Mladek char *restricted_pointer(char *buf, char *end, const void *ptr, 8286eea242fSPetr Mladek struct printf_spec spec) 8296eea242fSPetr Mladek { 8306eea242fSPetr Mladek switch (kptr_restrict) { 8316eea242fSPetr Mladek case 0: 8321ac2f978SPetr Mladek /* Handle as %p, hash and do _not_ leak addresses. */ 8331ac2f978SPetr Mladek return ptr_to_id(buf, end, ptr, spec); 8346eea242fSPetr Mladek case 1: { 8356eea242fSPetr Mladek const struct cred *cred; 8366eea242fSPetr Mladek 8376eea242fSPetr Mladek /* 8386eea242fSPetr Mladek * kptr_restrict==1 cannot be used in IRQ context 8396eea242fSPetr Mladek * because its test for CAP_SYSLOG would be meaningless. 8406eea242fSPetr Mladek */ 8416eea242fSPetr Mladek if (in_irq() || in_serving_softirq() || in_nmi()) { 8426eea242fSPetr Mladek if (spec.field_width == -1) 8436eea242fSPetr Mladek spec.field_width = 2 * sizeof(ptr); 844c8c3b584SPetr Mladek return error_string(buf, end, "pK-error", spec); 8456eea242fSPetr Mladek } 8466eea242fSPetr Mladek 8476eea242fSPetr Mladek /* 8486eea242fSPetr Mladek * Only print the real pointer value if the current 8496eea242fSPetr Mladek * process has CAP_SYSLOG and is running with the 8506eea242fSPetr Mladek * same credentials it started with. This is because 8516eea242fSPetr Mladek * access to files is checked at open() time, but %pK 8526eea242fSPetr Mladek * checks permission at read() time. We don't want to 8536eea242fSPetr Mladek * leak pointer values if a binary opens a file using 8546eea242fSPetr Mladek * %pK and then elevates privileges before reading it. 8556eea242fSPetr Mladek */ 8566eea242fSPetr Mladek cred = current_cred(); 8576eea242fSPetr Mladek if (!has_capability_noaudit(current, CAP_SYSLOG) || 8586eea242fSPetr Mladek !uid_eq(cred->euid, cred->uid) || 8596eea242fSPetr Mladek !gid_eq(cred->egid, cred->gid)) 8606eea242fSPetr Mladek ptr = NULL; 8616eea242fSPetr Mladek break; 8626eea242fSPetr Mladek } 8636eea242fSPetr Mladek case 2: 8646eea242fSPetr Mladek default: 8656eea242fSPetr Mladek /* Always print 0's for %pK */ 8666eea242fSPetr Mladek ptr = NULL; 8676eea242fSPetr Mladek break; 8686eea242fSPetr Mladek } 8696eea242fSPetr Mladek 8706eea242fSPetr Mladek return pointer_string(buf, end, ptr, spec); 8716eea242fSPetr Mladek } 8726eea242fSPetr Mladek 8739073dac1SGeert Uytterhoeven static noinline_for_stack 8744b6ccca7SAl Viro char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 8754b6ccca7SAl Viro const char *fmt) 8764b6ccca7SAl Viro { 8774b6ccca7SAl Viro const char *array[4], *s; 8784b6ccca7SAl Viro const struct dentry *p; 8794b6ccca7SAl Viro int depth; 8804b6ccca7SAl Viro int i, n; 8814b6ccca7SAl Viro 8824b6ccca7SAl Viro switch (fmt[1]) { 8834b6ccca7SAl Viro case '2': case '3': case '4': 8844b6ccca7SAl Viro depth = fmt[1] - '0'; 8854b6ccca7SAl Viro break; 8864b6ccca7SAl Viro default: 8874b6ccca7SAl Viro depth = 1; 8884b6ccca7SAl Viro } 8894b6ccca7SAl Viro 8904b6ccca7SAl Viro rcu_read_lock(); 8914b6ccca7SAl Viro for (i = 0; i < depth; i++, d = p) { 8923e5903ebSPetr Mladek if (check_pointer(&buf, end, d, spec)) { 8933e5903ebSPetr Mladek rcu_read_unlock(); 8943e5903ebSPetr Mladek return buf; 8953e5903ebSPetr Mladek } 8963e5903ebSPetr Mladek 8976aa7de05SMark Rutland p = READ_ONCE(d->d_parent); 8986aa7de05SMark Rutland array[i] = READ_ONCE(d->d_name.name); 8994b6ccca7SAl Viro if (p == d) { 9004b6ccca7SAl Viro if (i) 9014b6ccca7SAl Viro array[i] = ""; 9024b6ccca7SAl Viro i++; 9034b6ccca7SAl Viro break; 9044b6ccca7SAl Viro } 9054b6ccca7SAl Viro } 9064b6ccca7SAl Viro s = array[--i]; 9074b6ccca7SAl Viro for (n = 0; n != spec.precision; n++, buf++) { 9084b6ccca7SAl Viro char c = *s++; 9094b6ccca7SAl Viro if (!c) { 9104b6ccca7SAl Viro if (!i) 9114b6ccca7SAl Viro break; 9124b6ccca7SAl Viro c = '/'; 9134b6ccca7SAl Viro s = array[--i]; 9144b6ccca7SAl Viro } 9154b6ccca7SAl Viro if (buf < end) 9164b6ccca7SAl Viro *buf = c; 9174b6ccca7SAl Viro } 9184b6ccca7SAl Viro rcu_read_unlock(); 919cfccde04SRasmus Villemoes return widen_string(buf, n, end, spec); 9204b6ccca7SAl Viro } 9214b6ccca7SAl Viro 92236594b31SJia He static noinline_for_stack 92336594b31SJia He char *file_dentry_name(char *buf, char *end, const struct file *f, 92436594b31SJia He struct printf_spec spec, const char *fmt) 92536594b31SJia He { 92636594b31SJia He if (check_pointer(&buf, end, f, spec)) 92736594b31SJia He return buf; 92836594b31SJia He 92936594b31SJia He return dentry_name(buf, end, f->f_path.dentry, spec, fmt); 93036594b31SJia He } 9311031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 9321031bc58SDmitry Monakhov static noinline_for_stack 9331031bc58SDmitry Monakhov char *bdev_name(char *buf, char *end, struct block_device *bdev, 9341031bc58SDmitry Monakhov struct printf_spec spec, const char *fmt) 9351031bc58SDmitry Monakhov { 9363e5903ebSPetr Mladek struct gendisk *hd; 9371031bc58SDmitry Monakhov 9383e5903ebSPetr Mladek if (check_pointer(&buf, end, bdev, spec)) 9393e5903ebSPetr Mladek return buf; 9403e5903ebSPetr Mladek 9413e5903ebSPetr Mladek hd = bdev->bd_disk; 9421031bc58SDmitry Monakhov buf = string(buf, end, hd->disk_name, spec); 9431031bc58SDmitry Monakhov if (bdev->bd_part->partno) { 9441031bc58SDmitry Monakhov if (isdigit(hd->disk_name[strlen(hd->disk_name)-1])) { 9451031bc58SDmitry Monakhov if (buf < end) 9461031bc58SDmitry Monakhov *buf = 'p'; 9471031bc58SDmitry Monakhov buf++; 9481031bc58SDmitry Monakhov } 9491031bc58SDmitry Monakhov buf = number(buf, end, bdev->bd_part->partno, spec); 9501031bc58SDmitry Monakhov } 9511031bc58SDmitry Monakhov return buf; 9521031bc58SDmitry Monakhov } 9531031bc58SDmitry Monakhov #endif 9541031bc58SDmitry Monakhov 955cf3b429bSJoe Perches static noinline_for_stack 956cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 957b0d33c2bSJoe Perches struct printf_spec spec, const char *fmt) 9580fe1ef24SLinus Torvalds { 959b0d33c2bSJoe Perches unsigned long value; 9600fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 9610fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 962b0d33c2bSJoe Perches #endif 963b0d33c2bSJoe Perches 964b0d33c2bSJoe Perches if (fmt[1] == 'R') 965b0d33c2bSJoe Perches ptr = __builtin_extract_return_addr(ptr); 966b0d33c2bSJoe Perches value = (unsigned long)ptr; 967b0d33c2bSJoe Perches 968b0d33c2bSJoe Perches #ifdef CONFIG_KALLSYMS 969b0d33c2bSJoe Perches if (*fmt == 'B') 9700f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 9719af77064SSakari Ailus else if (*fmt != 's') 9720fe1ef24SLinus Torvalds sprint_symbol(sym, value); 9730c8b946eSFrederic Weisbecker else 9744796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 9757b9186f5SAndré Goddard Rosa 976d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 9770fe1ef24SLinus Torvalds #else 9783cab1e71SAndy Shevchenko return special_hex_number(buf, end, value, sizeof(void *)); 9790fe1ef24SLinus Torvalds #endif 9800fe1ef24SLinus Torvalds } 9810fe1ef24SLinus Torvalds 982abd4fe62SAndy Shevchenko static const struct printf_spec default_str_spec = { 983abd4fe62SAndy Shevchenko .field_width = -1, 984abd4fe62SAndy Shevchenko .precision = -1, 985abd4fe62SAndy Shevchenko }; 986abd4fe62SAndy Shevchenko 98754433973SAndy Shevchenko static const struct printf_spec default_flag_spec = { 98854433973SAndy Shevchenko .base = 16, 98954433973SAndy Shevchenko .precision = -1, 99054433973SAndy Shevchenko .flags = SPECIAL | SMALL, 99154433973SAndy Shevchenko }; 99254433973SAndy Shevchenko 993ce0b4910SAndy Shevchenko static const struct printf_spec default_dec_spec = { 994ce0b4910SAndy Shevchenko .base = 10, 995ce0b4910SAndy Shevchenko .precision = -1, 996ce0b4910SAndy Shevchenko }; 997ce0b4910SAndy Shevchenko 9984d42c447SAndy Shevchenko static const struct printf_spec default_dec02_spec = { 9994d42c447SAndy Shevchenko .base = 10, 10004d42c447SAndy Shevchenko .field_width = 2, 10014d42c447SAndy Shevchenko .precision = -1, 10024d42c447SAndy Shevchenko .flags = ZEROPAD, 10034d42c447SAndy Shevchenko }; 10044d42c447SAndy Shevchenko 10054d42c447SAndy Shevchenko static const struct printf_spec default_dec04_spec = { 10064d42c447SAndy Shevchenko .base = 10, 10074d42c447SAndy Shevchenko .field_width = 4, 10084d42c447SAndy Shevchenko .precision = -1, 10094d42c447SAndy Shevchenko .flags = ZEROPAD, 10104d42c447SAndy Shevchenko }; 10114d42c447SAndy Shevchenko 1012cf3b429bSJoe Perches static noinline_for_stack 1013cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 1014fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 1015332d2e78SLinus Torvalds { 1016332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 101728405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 1018332d2e78SLinus Torvalds #endif 1019332d2e78SLinus Torvalds 1020332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 102128405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 1022332d2e78SLinus Torvalds #endif 10234da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 1024fef20d9cSFrederic Weisbecker .base = 16, 10254da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 1026fef20d9cSFrederic Weisbecker .precision = -1, 1027fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 1028fef20d9cSFrederic Weisbecker }; 10294da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 10304da0b66cSBjorn Helgaas .base = 16, 10314da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 10324da0b66cSBjorn Helgaas .precision = -1, 10334da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 10344da0b66cSBjorn Helgaas }; 10350f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 10360f4050c7SBjorn Helgaas .base = 16, 10370f4050c7SBjorn Helgaas .field_width = 2, 10380f4050c7SBjorn Helgaas .precision = -1, 10390f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 10400f4050c7SBjorn Helgaas }; 10414da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 1042fd95541eSBjorn Helgaas .field_width = -1, 1043fd95541eSBjorn Helgaas .precision = 10, 1044fd95541eSBjorn Helgaas .flags = LEFT, 1045fd95541eSBjorn Helgaas }; 1046c7dabef8SBjorn Helgaas 1047c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 1048c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 1049c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 1050c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 10519d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 1052c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 1053c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 1054c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 1055c7dabef8SBjorn Helgaas 1056332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 1057c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 10584da0b66cSBjorn Helgaas const struct printf_spec *specp; 1059332d2e78SLinus Torvalds 10603e5903ebSPetr Mladek if (check_pointer(&buf, end, res, spec)) 10613e5903ebSPetr Mladek return buf; 10623e5903ebSPetr Mladek 1063332d2e78SLinus Torvalds *p++ = '['; 10644da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 1065d529ac41SPetr Mladek p = string_nocheck(p, pend, "io ", str_spec); 10664da0b66cSBjorn Helgaas specp = &io_spec; 10674da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 1068d529ac41SPetr Mladek p = string_nocheck(p, pend, "mem ", str_spec); 10694da0b66cSBjorn Helgaas specp = &mem_spec; 10704da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 1071d529ac41SPetr Mladek p = string_nocheck(p, pend, "irq ", str_spec); 1072ce0b4910SAndy Shevchenko specp = &default_dec_spec; 10734da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 1074d529ac41SPetr Mladek p = string_nocheck(p, pend, "dma ", str_spec); 1075ce0b4910SAndy Shevchenko specp = &default_dec_spec; 10760f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 1077d529ac41SPetr Mladek p = string_nocheck(p, pend, "bus ", str_spec); 10780f4050c7SBjorn Helgaas specp = &bus_spec; 10794da0b66cSBjorn Helgaas } else { 1080d529ac41SPetr Mladek p = string_nocheck(p, pend, "??? ", str_spec); 10814da0b66cSBjorn Helgaas specp = &mem_spec; 1082c7dabef8SBjorn Helgaas decode = 0; 1083fd95541eSBjorn Helgaas } 1084d19cb803SBjorn Helgaas if (decode && res->flags & IORESOURCE_UNSET) { 1085d529ac41SPetr Mladek p = string_nocheck(p, pend, "size ", str_spec); 1086d19cb803SBjorn Helgaas p = number(p, pend, resource_size(res), *specp); 1087d19cb803SBjorn Helgaas } else { 10884da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 1089c91d3376SBjorn Helgaas if (res->start != res->end) { 1090332d2e78SLinus Torvalds *p++ = '-'; 10914da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 1092c91d3376SBjorn Helgaas } 1093d19cb803SBjorn Helgaas } 1094c7dabef8SBjorn Helgaas if (decode) { 1095fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 1096d529ac41SPetr Mladek p = string_nocheck(p, pend, " 64bit", str_spec); 1097fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 1098d529ac41SPetr Mladek p = string_nocheck(p, pend, " pref", str_spec); 10999d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 1100d529ac41SPetr Mladek p = string_nocheck(p, pend, " window", str_spec); 1101fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 1102d529ac41SPetr Mladek p = string_nocheck(p, pend, " disabled", str_spec); 1103c7dabef8SBjorn Helgaas } else { 1104d529ac41SPetr Mladek p = string_nocheck(p, pend, " flags ", str_spec); 110554433973SAndy Shevchenko p = number(p, pend, res->flags, default_flag_spec); 1106fd95541eSBjorn Helgaas } 1107332d2e78SLinus Torvalds *p++ = ']'; 1108c7dabef8SBjorn Helgaas *p = '\0'; 1109332d2e78SLinus Torvalds 1110d529ac41SPetr Mladek return string_nocheck(buf, end, sym, spec); 1111332d2e78SLinus Torvalds } 1112332d2e78SLinus Torvalds 1113cf3b429bSJoe Perches static noinline_for_stack 111431550a16SAndy Shevchenko char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 111531550a16SAndy Shevchenko const char *fmt) 111631550a16SAndy Shevchenko { 1117360603a1SSteven Rostedt int i, len = 1; /* if we pass '%ph[CDN]', field width remains 111831550a16SAndy Shevchenko negative value, fallback to the default */ 111931550a16SAndy Shevchenko char separator; 112031550a16SAndy Shevchenko 112131550a16SAndy Shevchenko if (spec.field_width == 0) 112231550a16SAndy Shevchenko /* nothing to print */ 112331550a16SAndy Shevchenko return buf; 112431550a16SAndy Shevchenko 11253e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 11263e5903ebSPetr Mladek return buf; 112731550a16SAndy Shevchenko 112831550a16SAndy Shevchenko switch (fmt[1]) { 112931550a16SAndy Shevchenko case 'C': 113031550a16SAndy Shevchenko separator = ':'; 113131550a16SAndy Shevchenko break; 113231550a16SAndy Shevchenko case 'D': 113331550a16SAndy Shevchenko separator = '-'; 113431550a16SAndy Shevchenko break; 113531550a16SAndy Shevchenko case 'N': 113631550a16SAndy Shevchenko separator = 0; 113731550a16SAndy Shevchenko break; 113831550a16SAndy Shevchenko default: 113931550a16SAndy Shevchenko separator = ' '; 114031550a16SAndy Shevchenko break; 114131550a16SAndy Shevchenko } 114231550a16SAndy Shevchenko 114331550a16SAndy Shevchenko if (spec.field_width > 0) 114431550a16SAndy Shevchenko len = min_t(int, spec.field_width, 64); 114531550a16SAndy Shevchenko 11469c98f235SRasmus Villemoes for (i = 0; i < len; ++i) { 11479c98f235SRasmus Villemoes if (buf < end) 11489c98f235SRasmus Villemoes *buf = hex_asc_hi(addr[i]); 11499c98f235SRasmus Villemoes ++buf; 11509c98f235SRasmus Villemoes if (buf < end) 11519c98f235SRasmus Villemoes *buf = hex_asc_lo(addr[i]); 11529c98f235SRasmus Villemoes ++buf; 115331550a16SAndy Shevchenko 11549c98f235SRasmus Villemoes if (separator && i != len - 1) { 11559c98f235SRasmus Villemoes if (buf < end) 11569c98f235SRasmus Villemoes *buf = separator; 11579c98f235SRasmus Villemoes ++buf; 11589c98f235SRasmus Villemoes } 115931550a16SAndy Shevchenko } 116031550a16SAndy Shevchenko 116131550a16SAndy Shevchenko return buf; 116231550a16SAndy Shevchenko } 116331550a16SAndy Shevchenko 116431550a16SAndy Shevchenko static noinline_for_stack 1165dbc760bcSTejun Heo char *bitmap_string(char *buf, char *end, unsigned long *bitmap, 1166dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1167dbc760bcSTejun Heo { 1168dbc760bcSTejun Heo const int CHUNKSZ = 32; 1169dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1170dbc760bcSTejun Heo int i, chunksz; 1171dbc760bcSTejun Heo bool first = true; 1172dbc760bcSTejun Heo 11733e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec)) 11743e5903ebSPetr Mladek return buf; 11753e5903ebSPetr Mladek 1176dbc760bcSTejun Heo /* reused to print numbers */ 1177dbc760bcSTejun Heo spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 }; 1178dbc760bcSTejun Heo 1179dbc760bcSTejun Heo chunksz = nr_bits & (CHUNKSZ - 1); 1180dbc760bcSTejun Heo if (chunksz == 0) 1181dbc760bcSTejun Heo chunksz = CHUNKSZ; 1182dbc760bcSTejun Heo 1183dbc760bcSTejun Heo i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ; 1184dbc760bcSTejun Heo for (; i >= 0; i -= CHUNKSZ) { 1185dbc760bcSTejun Heo u32 chunkmask, val; 1186dbc760bcSTejun Heo int word, bit; 1187dbc760bcSTejun Heo 1188dbc760bcSTejun Heo chunkmask = ((1ULL << chunksz) - 1); 1189dbc760bcSTejun Heo word = i / BITS_PER_LONG; 1190dbc760bcSTejun Heo bit = i % BITS_PER_LONG; 1191dbc760bcSTejun Heo val = (bitmap[word] >> bit) & chunkmask; 1192dbc760bcSTejun Heo 1193dbc760bcSTejun Heo if (!first) { 1194dbc760bcSTejun Heo if (buf < end) 1195dbc760bcSTejun Heo *buf = ','; 1196dbc760bcSTejun Heo buf++; 1197dbc760bcSTejun Heo } 1198dbc760bcSTejun Heo first = false; 1199dbc760bcSTejun Heo 1200dbc760bcSTejun Heo spec.field_width = DIV_ROUND_UP(chunksz, 4); 1201dbc760bcSTejun Heo buf = number(buf, end, val, spec); 1202dbc760bcSTejun Heo 1203dbc760bcSTejun Heo chunksz = CHUNKSZ; 1204dbc760bcSTejun Heo } 1205dbc760bcSTejun Heo return buf; 1206dbc760bcSTejun Heo } 1207dbc760bcSTejun Heo 1208dbc760bcSTejun Heo static noinline_for_stack 1209dbc760bcSTejun Heo char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap, 1210dbc760bcSTejun Heo struct printf_spec spec, const char *fmt) 1211dbc760bcSTejun Heo { 1212dbc760bcSTejun Heo int nr_bits = max_t(int, spec.field_width, 0); 1213dbc760bcSTejun Heo /* current bit is 'cur', most recently seen range is [rbot, rtop] */ 1214dbc760bcSTejun Heo int cur, rbot, rtop; 1215dbc760bcSTejun Heo bool first = true; 1216dbc760bcSTejun Heo 12173e5903ebSPetr Mladek if (check_pointer(&buf, end, bitmap, spec)) 12183e5903ebSPetr Mladek return buf; 12193e5903ebSPetr Mladek 1220dbc760bcSTejun Heo rbot = cur = find_first_bit(bitmap, nr_bits); 1221dbc760bcSTejun Heo while (cur < nr_bits) { 1222dbc760bcSTejun Heo rtop = cur; 1223dbc760bcSTejun Heo cur = find_next_bit(bitmap, nr_bits, cur + 1); 1224dbc760bcSTejun Heo if (cur < nr_bits && cur <= rtop + 1) 1225dbc760bcSTejun Heo continue; 1226dbc760bcSTejun Heo 1227dbc760bcSTejun Heo if (!first) { 1228dbc760bcSTejun Heo if (buf < end) 1229dbc760bcSTejun Heo *buf = ','; 1230dbc760bcSTejun Heo buf++; 1231dbc760bcSTejun Heo } 1232dbc760bcSTejun Heo first = false; 1233dbc760bcSTejun Heo 1234ce0b4910SAndy Shevchenko buf = number(buf, end, rbot, default_dec_spec); 1235dbc760bcSTejun Heo if (rbot < rtop) { 1236dbc760bcSTejun Heo if (buf < end) 1237dbc760bcSTejun Heo *buf = '-'; 1238dbc760bcSTejun Heo buf++; 1239dbc760bcSTejun Heo 1240ce0b4910SAndy Shevchenko buf = number(buf, end, rtop, default_dec_spec); 1241dbc760bcSTejun Heo } 1242dbc760bcSTejun Heo 1243dbc760bcSTejun Heo rbot = cur; 1244dbc760bcSTejun Heo } 1245dbc760bcSTejun Heo return buf; 1246dbc760bcSTejun Heo } 1247dbc760bcSTejun Heo 1248dbc760bcSTejun Heo static noinline_for_stack 1249cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 12508a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 1251dd45c9cfSHarvey Harrison { 12528a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 1253dd45c9cfSHarvey Harrison char *p = mac_addr; 1254dd45c9cfSHarvey Harrison int i; 1255bc7259a2SJoe Perches char separator; 125676597ff9SAndrei Emeltchenko bool reversed = false; 1257bc7259a2SJoe Perches 12583e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 12593e5903ebSPetr Mladek return buf; 12603e5903ebSPetr Mladek 126176597ff9SAndrei Emeltchenko switch (fmt[1]) { 126276597ff9SAndrei Emeltchenko case 'F': 1263bc7259a2SJoe Perches separator = '-'; 126476597ff9SAndrei Emeltchenko break; 126576597ff9SAndrei Emeltchenko 126676597ff9SAndrei Emeltchenko case 'R': 126776597ff9SAndrei Emeltchenko reversed = true; 126876597ff9SAndrei Emeltchenko /* fall through */ 126976597ff9SAndrei Emeltchenko 127076597ff9SAndrei Emeltchenko default: 1271bc7259a2SJoe Perches separator = ':'; 127276597ff9SAndrei Emeltchenko break; 1273bc7259a2SJoe Perches } 1274dd45c9cfSHarvey Harrison 1275dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 127676597ff9SAndrei Emeltchenko if (reversed) 127776597ff9SAndrei Emeltchenko p = hex_byte_pack(p, addr[5 - i]); 127876597ff9SAndrei Emeltchenko else 127955036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 128076597ff9SAndrei Emeltchenko 12818a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 1282bc7259a2SJoe Perches *p++ = separator; 1283dd45c9cfSHarvey Harrison } 1284dd45c9cfSHarvey Harrison *p = '\0'; 1285dd45c9cfSHarvey Harrison 1286d529ac41SPetr Mladek return string_nocheck(buf, end, mac_addr, spec); 1287dd45c9cfSHarvey Harrison } 1288dd45c9cfSHarvey Harrison 1289cf3b429bSJoe Perches static noinline_for_stack 1290cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 1291689afa7dSHarvey Harrison { 1292689afa7dSHarvey Harrison int i; 12930159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 12940159f24eSJoe Perches int index; 12950159f24eSJoe Perches int step; 1296689afa7dSHarvey Harrison 12970159f24eSJoe Perches switch (fmt[2]) { 12980159f24eSJoe Perches case 'h': 12990159f24eSJoe Perches #ifdef __BIG_ENDIAN 13000159f24eSJoe Perches index = 0; 13010159f24eSJoe Perches step = 1; 13020159f24eSJoe Perches #else 13030159f24eSJoe Perches index = 3; 13040159f24eSJoe Perches step = -1; 13050159f24eSJoe Perches #endif 13060159f24eSJoe Perches break; 13070159f24eSJoe Perches case 'l': 13080159f24eSJoe Perches index = 3; 13090159f24eSJoe Perches step = -1; 13100159f24eSJoe Perches break; 13110159f24eSJoe Perches case 'n': 13120159f24eSJoe Perches case 'b': 13130159f24eSJoe Perches default: 13140159f24eSJoe Perches index = 0; 13150159f24eSJoe Perches step = 1; 13160159f24eSJoe Perches break; 13170159f24eSJoe Perches } 13188a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 13197c43d9a3SRasmus Villemoes char temp[4] __aligned(2); /* hold each IP quad in reverse order */ 1320133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 13218a27f7c9SJoe Perches if (leading_zeros) { 13228a27f7c9SJoe Perches if (digits < 3) 13238a27f7c9SJoe Perches *p++ = '0'; 13248a27f7c9SJoe Perches if (digits < 2) 13258a27f7c9SJoe Perches *p++ = '0'; 13268a27f7c9SJoe Perches } 13278a27f7c9SJoe Perches /* reverse the digits in the quad */ 13288a27f7c9SJoe Perches while (digits--) 13298a27f7c9SJoe Perches *p++ = temp[digits]; 13308a27f7c9SJoe Perches if (i < 3) 13318a27f7c9SJoe Perches *p++ = '.'; 13320159f24eSJoe Perches index += step; 13338a27f7c9SJoe Perches } 13348a27f7c9SJoe Perches *p = '\0'; 13357b9186f5SAndré Goddard Rosa 13368a27f7c9SJoe Perches return p; 13378a27f7c9SJoe Perches } 13388a27f7c9SJoe Perches 1339cf3b429bSJoe Perches static noinline_for_stack 1340cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 13418a27f7c9SJoe Perches { 13427b9186f5SAndré Goddard Rosa int i, j, range; 13438a27f7c9SJoe Perches unsigned char zerolength[8]; 13448a27f7c9SJoe Perches int longest = 1; 13458a27f7c9SJoe Perches int colonpos = -1; 13468a27f7c9SJoe Perches u16 word; 13477b9186f5SAndré Goddard Rosa u8 hi, lo; 13488a27f7c9SJoe Perches bool needcolon = false; 1349eb78cd26SJoe Perches bool useIPv4; 1350eb78cd26SJoe Perches struct in6_addr in6; 1351eb78cd26SJoe Perches 1352eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 1353eb78cd26SJoe Perches 1354eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 13558a27f7c9SJoe Perches 13568a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 13578a27f7c9SJoe Perches 13588a27f7c9SJoe Perches if (useIPv4) 13598a27f7c9SJoe Perches range = 6; 13608a27f7c9SJoe Perches else 13618a27f7c9SJoe Perches range = 8; 13628a27f7c9SJoe Perches 13638a27f7c9SJoe Perches /* find position of longest 0 run */ 13648a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13658a27f7c9SJoe Perches for (j = i; j < range; j++) { 1366eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 13678a27f7c9SJoe Perches break; 13688a27f7c9SJoe Perches zerolength[i]++; 13698a27f7c9SJoe Perches } 13708a27f7c9SJoe Perches } 13718a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13728a27f7c9SJoe Perches if (zerolength[i] > longest) { 13738a27f7c9SJoe Perches longest = zerolength[i]; 13748a27f7c9SJoe Perches colonpos = i; 13758a27f7c9SJoe Perches } 13768a27f7c9SJoe Perches } 137729cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 137829cf519eSJoe Perches colonpos = -1; 13798a27f7c9SJoe Perches 13808a27f7c9SJoe Perches /* emit address */ 13818a27f7c9SJoe Perches for (i = 0; i < range; i++) { 13828a27f7c9SJoe Perches if (i == colonpos) { 13838a27f7c9SJoe Perches if (needcolon || i == 0) 13848a27f7c9SJoe Perches *p++ = ':'; 13858a27f7c9SJoe Perches *p++ = ':'; 13868a27f7c9SJoe Perches needcolon = false; 13878a27f7c9SJoe Perches i += longest - 1; 13888a27f7c9SJoe Perches continue; 13898a27f7c9SJoe Perches } 13908a27f7c9SJoe Perches if (needcolon) { 13918a27f7c9SJoe Perches *p++ = ':'; 13928a27f7c9SJoe Perches needcolon = false; 13938a27f7c9SJoe Perches } 13948a27f7c9SJoe Perches /* hex u16 without leading 0s */ 1395eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 13968a27f7c9SJoe Perches hi = word >> 8; 13978a27f7c9SJoe Perches lo = word & 0xff; 13988a27f7c9SJoe Perches if (hi) { 13998a27f7c9SJoe Perches if (hi > 0x0f) 140055036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 14018a27f7c9SJoe Perches else 14028a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 140355036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 14048a27f7c9SJoe Perches } 1405b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 140655036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 14078a27f7c9SJoe Perches else 14088a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 14098a27f7c9SJoe Perches needcolon = true; 14108a27f7c9SJoe Perches } 14118a27f7c9SJoe Perches 14128a27f7c9SJoe Perches if (useIPv4) { 14138a27f7c9SJoe Perches if (needcolon) 14148a27f7c9SJoe Perches *p++ = ':'; 14150159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 14168a27f7c9SJoe Perches } 14178a27f7c9SJoe Perches *p = '\0'; 14187b9186f5SAndré Goddard Rosa 14198a27f7c9SJoe Perches return p; 14208a27f7c9SJoe Perches } 14218a27f7c9SJoe Perches 1422cf3b429bSJoe Perches static noinline_for_stack 1423cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 14248a27f7c9SJoe Perches { 14258a27f7c9SJoe Perches int i; 14267b9186f5SAndré Goddard Rosa 1427689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 142855036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 142955036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 14308a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 1431689afa7dSHarvey Harrison *p++ = ':'; 1432689afa7dSHarvey Harrison } 1433689afa7dSHarvey Harrison *p = '\0'; 14347b9186f5SAndré Goddard Rosa 14358a27f7c9SJoe Perches return p; 14368a27f7c9SJoe Perches } 14378a27f7c9SJoe Perches 1438cf3b429bSJoe Perches static noinline_for_stack 1439cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 14408a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 14418a27f7c9SJoe Perches { 14428a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 14438a27f7c9SJoe Perches 14448a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 1445eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 14468a27f7c9SJoe Perches else 1447eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 1448689afa7dSHarvey Harrison 1449d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 1450689afa7dSHarvey Harrison } 1451689afa7dSHarvey Harrison 1452cf3b429bSJoe Perches static noinline_for_stack 1453cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 14548a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 14554aa99606SHarvey Harrison { 14568a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 14574aa99606SHarvey Harrison 14580159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 14594aa99606SHarvey Harrison 1460d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 14614aa99606SHarvey Harrison } 14624aa99606SHarvey Harrison 1463cf3b429bSJoe Perches static noinline_for_stack 146410679643SDaniel Borkmann char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa, 146510679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 146610679643SDaniel Borkmann { 146710679643SDaniel Borkmann bool have_p = false, have_s = false, have_f = false, have_c = false; 146810679643SDaniel Borkmann char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") + 146910679643SDaniel Borkmann sizeof(":12345") + sizeof("/123456789") + 147010679643SDaniel Borkmann sizeof("%1234567890")]; 147110679643SDaniel Borkmann char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr); 147210679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin6_addr; 147310679643SDaniel Borkmann char fmt6[2] = { fmt[0], '6' }; 147410679643SDaniel Borkmann u8 off = 0; 147510679643SDaniel Borkmann 147610679643SDaniel Borkmann fmt++; 147710679643SDaniel Borkmann while (isalpha(*++fmt)) { 147810679643SDaniel Borkmann switch (*fmt) { 147910679643SDaniel Borkmann case 'p': 148010679643SDaniel Borkmann have_p = true; 148110679643SDaniel Borkmann break; 148210679643SDaniel Borkmann case 'f': 148310679643SDaniel Borkmann have_f = true; 148410679643SDaniel Borkmann break; 148510679643SDaniel Borkmann case 's': 148610679643SDaniel Borkmann have_s = true; 148710679643SDaniel Borkmann break; 148810679643SDaniel Borkmann case 'c': 148910679643SDaniel Borkmann have_c = true; 149010679643SDaniel Borkmann break; 149110679643SDaniel Borkmann } 149210679643SDaniel Borkmann } 149310679643SDaniel Borkmann 149410679643SDaniel Borkmann if (have_p || have_s || have_f) { 149510679643SDaniel Borkmann *p = '['; 149610679643SDaniel Borkmann off = 1; 149710679643SDaniel Borkmann } 149810679643SDaniel Borkmann 149910679643SDaniel Borkmann if (fmt6[0] == 'I' && have_c) 150010679643SDaniel Borkmann p = ip6_compressed_string(ip6_addr + off, addr); 150110679643SDaniel Borkmann else 150210679643SDaniel Borkmann p = ip6_string(ip6_addr + off, addr, fmt6); 150310679643SDaniel Borkmann 150410679643SDaniel Borkmann if (have_p || have_s || have_f) 150510679643SDaniel Borkmann *p++ = ']'; 150610679643SDaniel Borkmann 150710679643SDaniel Borkmann if (have_p) { 150810679643SDaniel Borkmann *p++ = ':'; 150910679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin6_port), spec); 151010679643SDaniel Borkmann } 151110679643SDaniel Borkmann if (have_f) { 151210679643SDaniel Borkmann *p++ = '/'; 151310679643SDaniel Borkmann p = number(p, pend, ntohl(sa->sin6_flowinfo & 151410679643SDaniel Borkmann IPV6_FLOWINFO_MASK), spec); 151510679643SDaniel Borkmann } 151610679643SDaniel Borkmann if (have_s) { 151710679643SDaniel Borkmann *p++ = '%'; 151810679643SDaniel Borkmann p = number(p, pend, sa->sin6_scope_id, spec); 151910679643SDaniel Borkmann } 152010679643SDaniel Borkmann *p = '\0'; 152110679643SDaniel Borkmann 1522d529ac41SPetr Mladek return string_nocheck(buf, end, ip6_addr, spec); 152310679643SDaniel Borkmann } 152410679643SDaniel Borkmann 152510679643SDaniel Borkmann static noinline_for_stack 152610679643SDaniel Borkmann char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa, 152710679643SDaniel Borkmann struct printf_spec spec, const char *fmt) 152810679643SDaniel Borkmann { 152910679643SDaniel Borkmann bool have_p = false; 153010679643SDaniel Borkmann char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")]; 153110679643SDaniel Borkmann char *pend = ip4_addr + sizeof(ip4_addr); 153210679643SDaniel Borkmann const u8 *addr = (const u8 *) &sa->sin_addr.s_addr; 153310679643SDaniel Borkmann char fmt4[3] = { fmt[0], '4', 0 }; 153410679643SDaniel Borkmann 153510679643SDaniel Borkmann fmt++; 153610679643SDaniel Borkmann while (isalpha(*++fmt)) { 153710679643SDaniel Borkmann switch (*fmt) { 153810679643SDaniel Borkmann case 'p': 153910679643SDaniel Borkmann have_p = true; 154010679643SDaniel Borkmann break; 154110679643SDaniel Borkmann case 'h': 154210679643SDaniel Borkmann case 'l': 154310679643SDaniel Borkmann case 'n': 154410679643SDaniel Borkmann case 'b': 154510679643SDaniel Borkmann fmt4[2] = *fmt; 154610679643SDaniel Borkmann break; 154710679643SDaniel Borkmann } 154810679643SDaniel Borkmann } 154910679643SDaniel Borkmann 155010679643SDaniel Borkmann p = ip4_string(ip4_addr, addr, fmt4); 155110679643SDaniel Borkmann if (have_p) { 155210679643SDaniel Borkmann *p++ = ':'; 155310679643SDaniel Borkmann p = number(p, pend, ntohs(sa->sin_port), spec); 155410679643SDaniel Borkmann } 155510679643SDaniel Borkmann *p = '\0'; 155610679643SDaniel Borkmann 1557d529ac41SPetr Mladek return string_nocheck(buf, end, ip4_addr, spec); 155810679643SDaniel Borkmann } 155910679643SDaniel Borkmann 156010679643SDaniel Borkmann static noinline_for_stack 1561f00cc102SPetr Mladek char *ip_addr_string(char *buf, char *end, const void *ptr, 1562f00cc102SPetr Mladek struct printf_spec spec, const char *fmt) 1563f00cc102SPetr Mladek { 15640b74d4d7SPetr Mladek char *err_fmt_msg; 15650b74d4d7SPetr Mladek 15663e5903ebSPetr Mladek if (check_pointer(&buf, end, ptr, spec)) 15673e5903ebSPetr Mladek return buf; 15683e5903ebSPetr Mladek 1569f00cc102SPetr Mladek switch (fmt[1]) { 1570f00cc102SPetr Mladek case '6': 1571f00cc102SPetr Mladek return ip6_addr_string(buf, end, ptr, spec, fmt); 1572f00cc102SPetr Mladek case '4': 1573f00cc102SPetr Mladek return ip4_addr_string(buf, end, ptr, spec, fmt); 1574f00cc102SPetr Mladek case 'S': { 1575f00cc102SPetr Mladek const union { 1576f00cc102SPetr Mladek struct sockaddr raw; 1577f00cc102SPetr Mladek struct sockaddr_in v4; 1578f00cc102SPetr Mladek struct sockaddr_in6 v6; 1579f00cc102SPetr Mladek } *sa = ptr; 1580f00cc102SPetr Mladek 1581f00cc102SPetr Mladek switch (sa->raw.sa_family) { 1582f00cc102SPetr Mladek case AF_INET: 1583f00cc102SPetr Mladek return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt); 1584f00cc102SPetr Mladek case AF_INET6: 1585f00cc102SPetr Mladek return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt); 1586f00cc102SPetr Mladek default: 1587c8c3b584SPetr Mladek return error_string(buf, end, "(einval)", spec); 1588f00cc102SPetr Mladek }} 1589f00cc102SPetr Mladek } 1590f00cc102SPetr Mladek 15910b74d4d7SPetr Mladek err_fmt_msg = fmt[0] == 'i' ? "(%pi?)" : "(%pI?)"; 1592c8c3b584SPetr Mladek return error_string(buf, end, err_fmt_msg, spec); 1593f00cc102SPetr Mladek } 1594f00cc102SPetr Mladek 1595f00cc102SPetr Mladek static noinline_for_stack 159671dca95dSAndy Shevchenko char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec, 159771dca95dSAndy Shevchenko const char *fmt) 159871dca95dSAndy Shevchenko { 159971dca95dSAndy Shevchenko bool found = true; 160071dca95dSAndy Shevchenko int count = 1; 160171dca95dSAndy Shevchenko unsigned int flags = 0; 160271dca95dSAndy Shevchenko int len; 160371dca95dSAndy Shevchenko 160471dca95dSAndy Shevchenko if (spec.field_width == 0) 160571dca95dSAndy Shevchenko return buf; /* nothing to print */ 160671dca95dSAndy Shevchenko 16073e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 16083e5903ebSPetr Mladek return buf; 160971dca95dSAndy Shevchenko 161071dca95dSAndy Shevchenko do { 161171dca95dSAndy Shevchenko switch (fmt[count++]) { 161271dca95dSAndy Shevchenko case 'a': 161371dca95dSAndy Shevchenko flags |= ESCAPE_ANY; 161471dca95dSAndy Shevchenko break; 161571dca95dSAndy Shevchenko case 'c': 161671dca95dSAndy Shevchenko flags |= ESCAPE_SPECIAL; 161771dca95dSAndy Shevchenko break; 161871dca95dSAndy Shevchenko case 'h': 161971dca95dSAndy Shevchenko flags |= ESCAPE_HEX; 162071dca95dSAndy Shevchenko break; 162171dca95dSAndy Shevchenko case 'n': 162271dca95dSAndy Shevchenko flags |= ESCAPE_NULL; 162371dca95dSAndy Shevchenko break; 162471dca95dSAndy Shevchenko case 'o': 162571dca95dSAndy Shevchenko flags |= ESCAPE_OCTAL; 162671dca95dSAndy Shevchenko break; 162771dca95dSAndy Shevchenko case 'p': 162871dca95dSAndy Shevchenko flags |= ESCAPE_NP; 162971dca95dSAndy Shevchenko break; 163071dca95dSAndy Shevchenko case 's': 163171dca95dSAndy Shevchenko flags |= ESCAPE_SPACE; 163271dca95dSAndy Shevchenko break; 163371dca95dSAndy Shevchenko default: 163471dca95dSAndy Shevchenko found = false; 163571dca95dSAndy Shevchenko break; 163671dca95dSAndy Shevchenko } 163771dca95dSAndy Shevchenko } while (found); 163871dca95dSAndy Shevchenko 163971dca95dSAndy Shevchenko if (!flags) 164071dca95dSAndy Shevchenko flags = ESCAPE_ANY_NP; 164171dca95dSAndy Shevchenko 164271dca95dSAndy Shevchenko len = spec.field_width < 0 ? 1 : spec.field_width; 164371dca95dSAndy Shevchenko 164441416f23SRasmus Villemoes /* 164541416f23SRasmus Villemoes * string_escape_mem() writes as many characters as it can to 164641416f23SRasmus Villemoes * the given buffer, and returns the total size of the output 164741416f23SRasmus Villemoes * had the buffer been big enough. 164841416f23SRasmus Villemoes */ 164941416f23SRasmus Villemoes buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL); 165071dca95dSAndy Shevchenko 165171dca95dSAndy Shevchenko return buf; 165271dca95dSAndy Shevchenko } 165371dca95dSAndy Shevchenko 16543e5903ebSPetr Mladek static char *va_format(char *buf, char *end, struct va_format *va_fmt, 16553e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 165645c3e93dSPetr Mladek { 165745c3e93dSPetr Mladek va_list va; 165845c3e93dSPetr Mladek 16593e5903ebSPetr Mladek if (check_pointer(&buf, end, va_fmt, spec)) 16603e5903ebSPetr Mladek return buf; 16613e5903ebSPetr Mladek 166245c3e93dSPetr Mladek va_copy(va, *va_fmt->va); 166345c3e93dSPetr Mladek buf += vsnprintf(buf, end > buf ? end - buf : 0, va_fmt->fmt, va); 166445c3e93dSPetr Mladek va_end(va); 166545c3e93dSPetr Mladek 166645c3e93dSPetr Mladek return buf; 166745c3e93dSPetr Mladek } 166845c3e93dSPetr Mladek 166971dca95dSAndy Shevchenko static noinline_for_stack 1670cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 16719ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 16729ac6e44eSJoe Perches { 16732b1b0d66SAndy Shevchenko char uuid[UUID_STRING_LEN + 1]; 16749ac6e44eSJoe Perches char *p = uuid; 16759ac6e44eSJoe Perches int i; 1676f9727a17SChristoph Hellwig const u8 *index = uuid_index; 16779ac6e44eSJoe Perches bool uc = false; 16789ac6e44eSJoe Perches 16793e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 16803e5903ebSPetr Mladek return buf; 16813e5903ebSPetr Mladek 16829ac6e44eSJoe Perches switch (*(++fmt)) { 16839ac6e44eSJoe Perches case 'L': 16849ac6e44eSJoe Perches uc = true; /* fall-through */ 16859ac6e44eSJoe Perches case 'l': 1686f9727a17SChristoph Hellwig index = guid_index; 16879ac6e44eSJoe Perches break; 16889ac6e44eSJoe Perches case 'B': 16899ac6e44eSJoe Perches uc = true; 16909ac6e44eSJoe Perches break; 16919ac6e44eSJoe Perches } 16929ac6e44eSJoe Perches 16939ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 1694aa4ea1c3SAndy Shevchenko if (uc) 1695aa4ea1c3SAndy Shevchenko p = hex_byte_pack_upper(p, addr[index[i]]); 1696aa4ea1c3SAndy Shevchenko else 169755036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 16989ac6e44eSJoe Perches switch (i) { 16999ac6e44eSJoe Perches case 3: 17009ac6e44eSJoe Perches case 5: 17019ac6e44eSJoe Perches case 7: 17029ac6e44eSJoe Perches case 9: 17039ac6e44eSJoe Perches *p++ = '-'; 17049ac6e44eSJoe Perches break; 17059ac6e44eSJoe Perches } 17069ac6e44eSJoe Perches } 17079ac6e44eSJoe Perches 17089ac6e44eSJoe Perches *p = 0; 17099ac6e44eSJoe Perches 1710d529ac41SPetr Mladek return string_nocheck(buf, end, uuid, spec); 17119ac6e44eSJoe Perches } 17129ac6e44eSJoe Perches 17135b17aecfSAndy Shevchenko static noinline_for_stack 1714431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr, 1715431bca24SGeert Uytterhoeven struct printf_spec spec, const char *fmt) 1716c8f44affSMichał Mirosław { 17175b17aecfSAndy Shevchenko unsigned long long num; 17185b17aecfSAndy Shevchenko int size; 17195b17aecfSAndy Shevchenko 17203e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 17213e5903ebSPetr Mladek return buf; 17223e5903ebSPetr Mladek 17235b17aecfSAndy Shevchenko switch (fmt[1]) { 17245b17aecfSAndy Shevchenko case 'F': 17255b17aecfSAndy Shevchenko num = *(const netdev_features_t *)addr; 17265b17aecfSAndy Shevchenko size = sizeof(netdev_features_t); 17275b17aecfSAndy Shevchenko break; 17285b17aecfSAndy Shevchenko default: 1729c8c3b584SPetr Mladek return error_string(buf, end, "(%pN?)", spec); 17305b17aecfSAndy Shevchenko } 1731c8f44affSMichał Mirosław 17323cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1733c8f44affSMichał Mirosław } 1734c8f44affSMichał Mirosław 1735aaf07621SJoe Perches static noinline_for_stack 17363e5903ebSPetr Mladek char *address_val(char *buf, char *end, const void *addr, 17373e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 1738aaf07621SJoe Perches { 1739aaf07621SJoe Perches unsigned long long num; 17403cab1e71SAndy Shevchenko int size; 1741aaf07621SJoe Perches 17423e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 17433e5903ebSPetr Mladek return buf; 17443e5903ebSPetr Mladek 1745aaf07621SJoe Perches switch (fmt[1]) { 1746aaf07621SJoe Perches case 'd': 1747aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 17483cab1e71SAndy Shevchenko size = sizeof(dma_addr_t); 1749aaf07621SJoe Perches break; 1750aaf07621SJoe Perches case 'p': 1751aaf07621SJoe Perches default: 1752aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 17533cab1e71SAndy Shevchenko size = sizeof(phys_addr_t); 1754aaf07621SJoe Perches break; 1755aaf07621SJoe Perches } 1756aaf07621SJoe Perches 17573cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1758aaf07621SJoe Perches } 1759aaf07621SJoe Perches 1760900cca29SGeert Uytterhoeven static noinline_for_stack 17614d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) 17624d42c447SAndy Shevchenko { 17634d42c447SAndy Shevchenko int year = tm->tm_year + (r ? 0 : 1900); 17644d42c447SAndy Shevchenko int mon = tm->tm_mon + (r ? 0 : 1); 17654d42c447SAndy Shevchenko 17664d42c447SAndy Shevchenko buf = number(buf, end, year, default_dec04_spec); 17674d42c447SAndy Shevchenko if (buf < end) 17684d42c447SAndy Shevchenko *buf = '-'; 17694d42c447SAndy Shevchenko buf++; 17704d42c447SAndy Shevchenko 17714d42c447SAndy Shevchenko buf = number(buf, end, mon, default_dec02_spec); 17724d42c447SAndy Shevchenko if (buf < end) 17734d42c447SAndy Shevchenko *buf = '-'; 17744d42c447SAndy Shevchenko buf++; 17754d42c447SAndy Shevchenko 17764d42c447SAndy Shevchenko return number(buf, end, tm->tm_mday, default_dec02_spec); 17774d42c447SAndy Shevchenko } 17784d42c447SAndy Shevchenko 17794d42c447SAndy Shevchenko static noinline_for_stack 17804d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) 17814d42c447SAndy Shevchenko { 17824d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_hour, default_dec02_spec); 17834d42c447SAndy Shevchenko if (buf < end) 17844d42c447SAndy Shevchenko *buf = ':'; 17854d42c447SAndy Shevchenko buf++; 17864d42c447SAndy Shevchenko 17874d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_min, default_dec02_spec); 17884d42c447SAndy Shevchenko if (buf < end) 17894d42c447SAndy Shevchenko *buf = ':'; 17904d42c447SAndy Shevchenko buf++; 17914d42c447SAndy Shevchenko 17924d42c447SAndy Shevchenko return number(buf, end, tm->tm_sec, default_dec02_spec); 17934d42c447SAndy Shevchenko } 17944d42c447SAndy Shevchenko 17954d42c447SAndy Shevchenko static noinline_for_stack 17963e5903ebSPetr Mladek char *rtc_str(char *buf, char *end, const struct rtc_time *tm, 17973e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 17984d42c447SAndy Shevchenko { 17994d42c447SAndy Shevchenko bool have_t = true, have_d = true; 18004d42c447SAndy Shevchenko bool raw = false; 18014d42c447SAndy Shevchenko int count = 2; 18024d42c447SAndy Shevchenko 18033e5903ebSPetr Mladek if (check_pointer(&buf, end, tm, spec)) 18043e5903ebSPetr Mladek return buf; 18053e5903ebSPetr Mladek 18064d42c447SAndy Shevchenko switch (fmt[count]) { 18074d42c447SAndy Shevchenko case 'd': 18084d42c447SAndy Shevchenko have_t = false; 18094d42c447SAndy Shevchenko count++; 18104d42c447SAndy Shevchenko break; 18114d42c447SAndy Shevchenko case 't': 18124d42c447SAndy Shevchenko have_d = false; 18134d42c447SAndy Shevchenko count++; 18144d42c447SAndy Shevchenko break; 18154d42c447SAndy Shevchenko } 18164d42c447SAndy Shevchenko 18174d42c447SAndy Shevchenko raw = fmt[count] == 'r'; 18184d42c447SAndy Shevchenko 18194d42c447SAndy Shevchenko if (have_d) 18204d42c447SAndy Shevchenko buf = date_str(buf, end, tm, raw); 18214d42c447SAndy Shevchenko if (have_d && have_t) { 18224d42c447SAndy Shevchenko /* Respect ISO 8601 */ 18234d42c447SAndy Shevchenko if (buf < end) 18244d42c447SAndy Shevchenko *buf = 'T'; 18254d42c447SAndy Shevchenko buf++; 18264d42c447SAndy Shevchenko } 18274d42c447SAndy Shevchenko if (have_t) 18284d42c447SAndy Shevchenko buf = time_str(buf, end, tm, raw); 18294d42c447SAndy Shevchenko 18304d42c447SAndy Shevchenko return buf; 18314d42c447SAndy Shevchenko } 18324d42c447SAndy Shevchenko 18334d42c447SAndy Shevchenko static noinline_for_stack 18347daac5b2SAndy Shevchenko char *time64_str(char *buf, char *end, const time64_t time, 18357daac5b2SAndy Shevchenko struct printf_spec spec, const char *fmt) 18367daac5b2SAndy Shevchenko { 18377daac5b2SAndy Shevchenko struct rtc_time rtc_time; 18387daac5b2SAndy Shevchenko struct tm tm; 18397daac5b2SAndy Shevchenko 18407daac5b2SAndy Shevchenko time64_to_tm(time, 0, &tm); 18417daac5b2SAndy Shevchenko 18427daac5b2SAndy Shevchenko rtc_time.tm_sec = tm.tm_sec; 18437daac5b2SAndy Shevchenko rtc_time.tm_min = tm.tm_min; 18447daac5b2SAndy Shevchenko rtc_time.tm_hour = tm.tm_hour; 18457daac5b2SAndy Shevchenko rtc_time.tm_mday = tm.tm_mday; 18467daac5b2SAndy Shevchenko rtc_time.tm_mon = tm.tm_mon; 18477daac5b2SAndy Shevchenko rtc_time.tm_year = tm.tm_year; 18487daac5b2SAndy Shevchenko rtc_time.tm_wday = tm.tm_wday; 18497daac5b2SAndy Shevchenko rtc_time.tm_yday = tm.tm_yday; 18507daac5b2SAndy Shevchenko 18517daac5b2SAndy Shevchenko rtc_time.tm_isdst = 0; 18527daac5b2SAndy Shevchenko 18537daac5b2SAndy Shevchenko return rtc_str(buf, end, &rtc_time, spec, fmt); 18547daac5b2SAndy Shevchenko } 18557daac5b2SAndy Shevchenko 18567daac5b2SAndy Shevchenko static noinline_for_stack 18574d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, 18584d42c447SAndy Shevchenko const char *fmt) 18594d42c447SAndy Shevchenko { 18604d42c447SAndy Shevchenko switch (fmt[1]) { 18614d42c447SAndy Shevchenko case 'R': 18623e5903ebSPetr Mladek return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); 18637daac5b2SAndy Shevchenko case 'T': 18647daac5b2SAndy Shevchenko return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); 18654d42c447SAndy Shevchenko default: 18667daac5b2SAndy Shevchenko return error_string(buf, end, "(%pt?)", spec); 18674d42c447SAndy Shevchenko } 18684d42c447SAndy Shevchenko } 18694d42c447SAndy Shevchenko 18704d42c447SAndy Shevchenko static noinline_for_stack 1871900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1872900cca29SGeert Uytterhoeven const char *fmt) 1873900cca29SGeert Uytterhoeven { 18740b74d4d7SPetr Mladek if (!IS_ENABLED(CONFIG_HAVE_CLK)) 1875c8c3b584SPetr Mladek return error_string(buf, end, "(%pC?)", spec); 18760b74d4d7SPetr Mladek 18773e5903ebSPetr Mladek if (check_pointer(&buf, end, clk, spec)) 18783e5903ebSPetr Mladek return buf; 1879900cca29SGeert Uytterhoeven 1880900cca29SGeert Uytterhoeven switch (fmt[1]) { 1881900cca29SGeert Uytterhoeven case 'n': 1882900cca29SGeert Uytterhoeven default: 1883900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1884900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1885900cca29SGeert Uytterhoeven #else 18864ca96aa9SGeert Uytterhoeven return ptr_to_id(buf, end, clk, spec); 1887900cca29SGeert Uytterhoeven #endif 1888900cca29SGeert Uytterhoeven } 1889900cca29SGeert Uytterhoeven } 1890900cca29SGeert Uytterhoeven 1891edf14cdbSVlastimil Babka static 1892edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags, 1893edf14cdbSVlastimil Babka const struct trace_print_flags *names) 1894edf14cdbSVlastimil Babka { 1895edf14cdbSVlastimil Babka unsigned long mask; 1896edf14cdbSVlastimil Babka 1897edf14cdbSVlastimil Babka for ( ; flags && names->name; names++) { 1898edf14cdbSVlastimil Babka mask = names->mask; 1899edf14cdbSVlastimil Babka if ((flags & mask) != mask) 1900edf14cdbSVlastimil Babka continue; 1901edf14cdbSVlastimil Babka 1902abd4fe62SAndy Shevchenko buf = string(buf, end, names->name, default_str_spec); 1903edf14cdbSVlastimil Babka 1904edf14cdbSVlastimil Babka flags &= ~mask; 1905edf14cdbSVlastimil Babka if (flags) { 1906edf14cdbSVlastimil Babka if (buf < end) 1907edf14cdbSVlastimil Babka *buf = '|'; 1908edf14cdbSVlastimil Babka buf++; 1909edf14cdbSVlastimil Babka } 1910edf14cdbSVlastimil Babka } 1911edf14cdbSVlastimil Babka 1912edf14cdbSVlastimil Babka if (flags) 191354433973SAndy Shevchenko buf = number(buf, end, flags, default_flag_spec); 1914edf14cdbSVlastimil Babka 1915edf14cdbSVlastimil Babka return buf; 1916edf14cdbSVlastimil Babka } 1917edf14cdbSVlastimil Babka 1918edf14cdbSVlastimil Babka static noinline_for_stack 19190b74d4d7SPetr Mladek char *flags_string(char *buf, char *end, void *flags_ptr, 19200b74d4d7SPetr Mladek struct printf_spec spec, const char *fmt) 1921edf14cdbSVlastimil Babka { 1922edf14cdbSVlastimil Babka unsigned long flags; 1923edf14cdbSVlastimil Babka const struct trace_print_flags *names; 1924edf14cdbSVlastimil Babka 19253e5903ebSPetr Mladek if (check_pointer(&buf, end, flags_ptr, spec)) 19263e5903ebSPetr Mladek return buf; 19273e5903ebSPetr Mladek 1928edf14cdbSVlastimil Babka switch (fmt[1]) { 1929edf14cdbSVlastimil Babka case 'p': 1930edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1931edf14cdbSVlastimil Babka /* Remove zone id */ 1932edf14cdbSVlastimil Babka flags &= (1UL << NR_PAGEFLAGS) - 1; 1933edf14cdbSVlastimil Babka names = pageflag_names; 1934edf14cdbSVlastimil Babka break; 1935edf14cdbSVlastimil Babka case 'v': 1936edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1937edf14cdbSVlastimil Babka names = vmaflag_names; 1938edf14cdbSVlastimil Babka break; 1939edf14cdbSVlastimil Babka case 'g': 1940edf14cdbSVlastimil Babka flags = *(gfp_t *)flags_ptr; 1941edf14cdbSVlastimil Babka names = gfpflag_names; 1942edf14cdbSVlastimil Babka break; 1943edf14cdbSVlastimil Babka default: 1944c8c3b584SPetr Mladek return error_string(buf, end, "(%pG?)", spec); 1945edf14cdbSVlastimil Babka } 1946edf14cdbSVlastimil Babka 1947edf14cdbSVlastimil Babka return format_flags(buf, end, flags, names); 1948edf14cdbSVlastimil Babka } 1949edf14cdbSVlastimil Babka 1950ce4fecf1SPantelis Antoniou static noinline_for_stack 1951a92eb762SSakari Ailus char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, 1952a92eb762SSakari Ailus char *end) 1953ce4fecf1SPantelis Antoniou { 1954ce4fecf1SPantelis Antoniou int depth; 1955ce4fecf1SPantelis Antoniou 1956a92eb762SSakari Ailus /* Loop starting from the root node to the current node. */ 1957a92eb762SSakari Ailus for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { 1958a92eb762SSakari Ailus struct fwnode_handle *__fwnode = 1959a92eb762SSakari Ailus fwnode_get_nth_parent(fwnode, depth); 1960ce4fecf1SPantelis Antoniou 1961a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name_prefix(__fwnode), 1962abd4fe62SAndy Shevchenko default_str_spec); 1963a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name(__fwnode), 1964a92eb762SSakari Ailus default_str_spec); 1965a92eb762SSakari Ailus 1966a92eb762SSakari Ailus fwnode_handle_put(__fwnode); 1967ce4fecf1SPantelis Antoniou } 1968a92eb762SSakari Ailus 1969ce4fecf1SPantelis Antoniou return buf; 1970ce4fecf1SPantelis Antoniou } 1971ce4fecf1SPantelis Antoniou 1972ce4fecf1SPantelis Antoniou static noinline_for_stack 1973ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn, 1974ce4fecf1SPantelis Antoniou struct printf_spec spec, const char *fmt) 1975ce4fecf1SPantelis Antoniou { 1976ce4fecf1SPantelis Antoniou char tbuf[sizeof("xxxx") + 1]; 1977ce4fecf1SPantelis Antoniou const char *p; 1978ce4fecf1SPantelis Antoniou int ret; 1979ce4fecf1SPantelis Antoniou char *buf_start = buf; 1980ce4fecf1SPantelis Antoniou struct property *prop; 1981ce4fecf1SPantelis Antoniou bool has_mult, pass; 1982ce4fecf1SPantelis Antoniou 1983ce4fecf1SPantelis Antoniou struct printf_spec str_spec = spec; 1984ce4fecf1SPantelis Antoniou str_spec.field_width = -1; 1985ce4fecf1SPantelis Antoniou 198683abc5a7SSakari Ailus if (fmt[0] != 'F') 198783abc5a7SSakari Ailus return error_string(buf, end, "(%pO?)", spec); 198883abc5a7SSakari Ailus 1989ce4fecf1SPantelis Antoniou if (!IS_ENABLED(CONFIG_OF)) 1990c8c3b584SPetr Mladek return error_string(buf, end, "(%pOF?)", spec); 1991ce4fecf1SPantelis Antoniou 19923e5903ebSPetr Mladek if (check_pointer(&buf, end, dn, spec)) 19933e5903ebSPetr Mladek return buf; 1994ce4fecf1SPantelis Antoniou 1995ce4fecf1SPantelis Antoniou /* simple case without anything any more format specifiers */ 1996ce4fecf1SPantelis Antoniou fmt++; 1997ce4fecf1SPantelis Antoniou if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 1998ce4fecf1SPantelis Antoniou fmt = "f"; 1999ce4fecf1SPantelis Antoniou 2000ce4fecf1SPantelis Antoniou for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 20016d0a70a2SRob Herring int precision; 2002ce4fecf1SPantelis Antoniou if (pass) { 2003ce4fecf1SPantelis Antoniou if (buf < end) 2004ce4fecf1SPantelis Antoniou *buf = ':'; 2005ce4fecf1SPantelis Antoniou buf++; 2006ce4fecf1SPantelis Antoniou } 2007ce4fecf1SPantelis Antoniou 2008ce4fecf1SPantelis Antoniou switch (*fmt) { 2009ce4fecf1SPantelis Antoniou case 'f': /* full_name */ 2010a92eb762SSakari Ailus buf = fwnode_full_name_string(of_fwnode_handle(dn), buf, 2011a92eb762SSakari Ailus end); 2012ce4fecf1SPantelis Antoniou break; 2013ce4fecf1SPantelis Antoniou case 'n': /* name */ 2014a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn)); 20156d0a70a2SRob Herring precision = str_spec.precision; 20166d0a70a2SRob Herring str_spec.precision = strchrnul(p, '@') - p; 20176d0a70a2SRob Herring buf = string(buf, end, p, str_spec); 20186d0a70a2SRob Herring str_spec.precision = precision; 2019ce4fecf1SPantelis Antoniou break; 2020ce4fecf1SPantelis Antoniou case 'p': /* phandle */ 2021*09ceb8d7SAndy Shevchenko buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec); 2022ce4fecf1SPantelis Antoniou break; 2023ce4fecf1SPantelis Antoniou case 'P': /* path-spec */ 2024a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn)); 2025ce4fecf1SPantelis Antoniou if (!p[1]) 2026ce4fecf1SPantelis Antoniou p = "/"; 2027ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2028ce4fecf1SPantelis Antoniou break; 2029ce4fecf1SPantelis Antoniou case 'F': /* flags */ 2030ce4fecf1SPantelis Antoniou tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 2031ce4fecf1SPantelis Antoniou tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 2032ce4fecf1SPantelis Antoniou tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 2033ce4fecf1SPantelis Antoniou tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 2034ce4fecf1SPantelis Antoniou tbuf[4] = 0; 2035d529ac41SPetr Mladek buf = string_nocheck(buf, end, tbuf, str_spec); 2036ce4fecf1SPantelis Antoniou break; 2037ce4fecf1SPantelis Antoniou case 'c': /* major compatible string */ 2038ce4fecf1SPantelis Antoniou ret = of_property_read_string(dn, "compatible", &p); 2039ce4fecf1SPantelis Antoniou if (!ret) 2040ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2041ce4fecf1SPantelis Antoniou break; 2042ce4fecf1SPantelis Antoniou case 'C': /* full compatible string */ 2043ce4fecf1SPantelis Antoniou has_mult = false; 2044ce4fecf1SPantelis Antoniou of_property_for_each_string(dn, "compatible", prop, p) { 2045ce4fecf1SPantelis Antoniou if (has_mult) 2046d529ac41SPetr Mladek buf = string_nocheck(buf, end, ",", str_spec); 2047d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 2048ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2049d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 2050ce4fecf1SPantelis Antoniou 2051ce4fecf1SPantelis Antoniou has_mult = true; 2052ce4fecf1SPantelis Antoniou } 2053ce4fecf1SPantelis Antoniou break; 2054ce4fecf1SPantelis Antoniou default: 2055ce4fecf1SPantelis Antoniou break; 2056ce4fecf1SPantelis Antoniou } 2057ce4fecf1SPantelis Antoniou } 2058ce4fecf1SPantelis Antoniou 2059ce4fecf1SPantelis Antoniou return widen_string(buf, buf - buf_start, end, spec); 2060ce4fecf1SPantelis Antoniou } 2061ce4fecf1SPantelis Antoniou 20623bd32d6aSSakari Ailus static noinline_for_stack 20633bd32d6aSSakari Ailus char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, 2064798cc27aSPetr Mladek struct printf_spec spec, const char *fmt) 2065798cc27aSPetr Mladek { 20663bd32d6aSSakari Ailus struct printf_spec str_spec = spec; 20673bd32d6aSSakari Ailus char *buf_start = buf; 20683bd32d6aSSakari Ailus 20693bd32d6aSSakari Ailus str_spec.field_width = -1; 20703bd32d6aSSakari Ailus 20713bd32d6aSSakari Ailus if (*fmt != 'w') 20723bd32d6aSSakari Ailus return error_string(buf, end, "(%pf?)", spec); 20733bd32d6aSSakari Ailus 20743bd32d6aSSakari Ailus if (check_pointer(&buf, end, fwnode, spec)) 20753bd32d6aSSakari Ailus return buf; 20763bd32d6aSSakari Ailus 20773bd32d6aSSakari Ailus fmt++; 20783bd32d6aSSakari Ailus 20793bd32d6aSSakari Ailus switch (*fmt) { 20803bd32d6aSSakari Ailus case 'P': /* name */ 20813bd32d6aSSakari Ailus buf = string(buf, end, fwnode_get_name(fwnode), str_spec); 20823bd32d6aSSakari Ailus break; 20833bd32d6aSSakari Ailus case 'f': /* full_name */ 20843bd32d6aSSakari Ailus default: 20853bd32d6aSSakari Ailus buf = fwnode_full_name_string(fwnode, buf, end); 20863bd32d6aSSakari Ailus break; 2087798cc27aSPetr Mladek } 2088798cc27aSPetr Mladek 20893bd32d6aSSakari Ailus return widen_string(buf, buf - buf_start, end, spec); 2090798cc27aSPetr Mladek } 2091798cc27aSPetr Mladek 20924d8a743cSLinus Torvalds /* 20934d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 20944d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 20954d8a743cSLinus Torvalds * specifiers. 20964d8a743cSLinus Torvalds * 20970b523769SJoe Perches * Please update scripts/checkpatch.pl when adding/removing conversion 20980b523769SJoe Perches * characters. (Search for "check for vsprintf extension"). 20990b523769SJoe Perches * 2100332d2e78SLinus Torvalds * Right now we handle: 21010fe1ef24SLinus Torvalds * 2102cdb7e52dSSergey Senozhatsky * - 'S' For symbolic direct pointers (or function descriptors) with offset 2103cdb7e52dSSergey Senozhatsky * - 's' For symbolic direct pointers (or function descriptors) without offset 21049af77064SSakari Ailus * - '[Ss]R' as above with __builtin_extract_return_addr() translation 21051586c5aeSSakari Ailus * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of 21061586c5aeSSakari Ailus * %ps and %pS. Be careful when re-using these specifiers. 21070f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 2108c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 2109c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 2110dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 2111dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 2112dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 2113dbc760bcSTejun Heo * range-list format instead of hex format 2114dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 2115dd45c9cfSHarvey Harrison * usual colon-separated hex notation 21168a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 2117bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 2118c8e00060SJoe Perches * with a dash-separated hex notation 21197c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 21208a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 21218a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 21228a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 212310679643SDaniel Borkmann * [S][pfs] 212410679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 212510679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 21268a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 21278a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 21288a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 212910679643SDaniel Borkmann * [S][pfs] 213010679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 213110679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 213210679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 213310679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 21348eda94bdSAlexander A. Klimov * https://tools.ietf.org/html/rfc5952 213571dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 213671dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 213771dca95dSAndy Shevchenko * details): 213871dca95dSAndy Shevchenko * a - ESCAPE_ANY 213971dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 214071dca95dSAndy Shevchenko * h - ESCAPE_HEX 214171dca95dSAndy Shevchenko * n - ESCAPE_NULL 214271dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 214371dca95dSAndy Shevchenko * p - ESCAPE_NP 214471dca95dSAndy Shevchenko * s - ESCAPE_SPACE 214571dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 21469ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 21479ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 21489ac6e44eSJoe Perches * Options for %pU are: 21499ac6e44eSJoe Perches * b big endian lower case hex (default) 21509ac6e44eSJoe Perches * B big endian UPPER case hex 21519ac6e44eSJoe Perches * l little endian lower case hex 21529ac6e44eSJoe Perches * L little endian UPPER case hex 21539ac6e44eSJoe Perches * big endian output byte order is: 21549ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 21559ac6e44eSJoe Perches * little endian output byte order is: 21569ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 21577db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 21587db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 21597db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 21607db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 21617db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 2162455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 2163c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 216431550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 216531550a16SAndy Shevchenko * a certain separator (' ' by default): 216631550a16SAndy Shevchenko * C colon 216731550a16SAndy Shevchenko * D dash 216831550a16SAndy Shevchenko * N no separator 216931550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 217031550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 2171aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 2172aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 2173c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 2174c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 21751031bc58SDmitry Monakhov * - 'g' For block_device name (gendisk + partition number) 21767daac5b2SAndy Shevchenko * - 't[RT][dt][r]' For time and date as represented by: 21774d42c447SAndy Shevchenko * R struct rtc_time 21787daac5b2SAndy Shevchenko * T time64_t 2179900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 2180900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2181900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 2182900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2183edf14cdbSVlastimil Babka * - 'G' For flags to be printed as a collection of symbolic strings that would 2184edf14cdbSVlastimil Babka * construct the specific value. Supported flags given by option: 2185edf14cdbSVlastimil Babka * p page flags (see struct page) given as pointer to unsigned long 2186edf14cdbSVlastimil Babka * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 2187edf14cdbSVlastimil Babka * v vma flags (VM_*) given as pointer to unsigned long 2188ce4fecf1SPantelis Antoniou * - 'OF[fnpPcCF]' For a device tree object 2189ce4fecf1SPantelis Antoniou * Without any optional arguments prints the full_name 2190ce4fecf1SPantelis Antoniou * f device node full_name 2191ce4fecf1SPantelis Antoniou * n device node name 2192ce4fecf1SPantelis Antoniou * p device node phandle 2193ce4fecf1SPantelis Antoniou * P device node path spec (name + @unit) 2194ce4fecf1SPantelis Antoniou * F device node flags 2195ce4fecf1SPantelis Antoniou * c major compatible string 2196ce4fecf1SPantelis Antoniou * C full compatible string 21973bd32d6aSSakari Ailus * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer 21983bd32d6aSSakari Ailus * Without an option prints the full name of the node 21993bd32d6aSSakari Ailus * f full name 22003bd32d6aSSakari Ailus * P node name, including a possible unit address 22017b1924a1STobin C. Harding * - 'x' For printing the address. Equivalent to "%lx". 2202b2a5212fSDaniel Borkmann * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of 2203b2a5212fSDaniel Borkmann * bpf_trace_printk() where [ku] prefix specifies either kernel (k) 2204b2a5212fSDaniel Borkmann * or user (u) memory to probe, and: 2205b2a5212fSDaniel Borkmann * s a string, equivalent to "%s" on direct vsnprintf() use 22067b1924a1STobin C. Harding * 2207b3ed2321STobin C. Harding * ** When making changes please also update: 2208b3ed2321STobin C. Harding * Documentation/core-api/printk-formats.rst 22099ac6e44eSJoe Perches * 2210ad67b74dSTobin C. Harding * Note: The default behaviour (unadorned %p) is to hash the address, 2211ad67b74dSTobin C. Harding * rendering it useful as a unique identifier. 22124d8a743cSLinus Torvalds */ 2213cf3b429bSJoe Perches static noinline_for_stack 2214cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 2215fef20d9cSFrederic Weisbecker struct printf_spec spec) 221678a8bf69SLinus Torvalds { 22170fe1ef24SLinus Torvalds switch (*fmt) { 22180fe1ef24SLinus Torvalds case 'S': 22199ac6e44eSJoe Perches case 's': 222004b8eb7aSSergey Senozhatsky ptr = dereference_symbol_descriptor(ptr); 222104b8eb7aSSergey Senozhatsky /* Fallthrough */ 22220f77a8d3SNamhyung Kim case 'B': 2223b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 2224332d2e78SLinus Torvalds case 'R': 2225c7dabef8SBjorn Helgaas case 'r': 2226fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 222731550a16SAndy Shevchenko case 'h': 222831550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 2229dbc760bcSTejun Heo case 'b': 2230dbc760bcSTejun Heo switch (fmt[1]) { 2231dbc760bcSTejun Heo case 'l': 2232dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 2233dbc760bcSTejun Heo default: 2234dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 2235dbc760bcSTejun Heo } 22368a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 22378a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 223876597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 223976597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 22408a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 22418a27f7c9SJoe Perches case 'I': /* Formatted IP supported 22428a27f7c9SJoe Perches * 4: 1.2.3.4 22438a27f7c9SJoe Perches * 6: 0001:0203:...:0708 22448a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 22458a27f7c9SJoe Perches */ 22468a27f7c9SJoe Perches case 'i': /* Contiguous: 22478a27f7c9SJoe Perches * 4: 001.002.003.004 22488a27f7c9SJoe Perches * 6: 000102...0f 22498a27f7c9SJoe Perches */ 2250f00cc102SPetr Mladek return ip_addr_string(buf, end, ptr, spec, fmt); 225171dca95dSAndy Shevchenko case 'E': 225271dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 22539ac6e44eSJoe Perches case 'U': 22549ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 22557db6f5fbSJoe Perches case 'V': 22563e5903ebSPetr Mladek return va_format(buf, end, ptr, spec, fmt); 2257455cd5abSDan Rosenberg case 'K': 225857e73442STobin C. Harding return restricted_pointer(buf, end, ptr, spec); 2259c8f44affSMichał Mirosław case 'N': 2260431bca24SGeert Uytterhoeven return netdev_bits(buf, end, ptr, spec, fmt); 22617d799210SStepan Moskovchenko case 'a': 22623e5903ebSPetr Mladek return address_val(buf, end, ptr, spec, fmt); 22634b6ccca7SAl Viro case 'd': 22644b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 22654d42c447SAndy Shevchenko case 't': 22664d42c447SAndy Shevchenko return time_and_date(buf, end, ptr, spec, fmt); 2267900cca29SGeert Uytterhoeven case 'C': 2268900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 22694b6ccca7SAl Viro case 'D': 227036594b31SJia He return file_dentry_name(buf, end, ptr, spec, fmt); 22711031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 22721031bc58SDmitry Monakhov case 'g': 22731031bc58SDmitry Monakhov return bdev_name(buf, end, ptr, spec, fmt); 22741031bc58SDmitry Monakhov #endif 22751031bc58SDmitry Monakhov 2276edf14cdbSVlastimil Babka case 'G': 22770b74d4d7SPetr Mladek return flags_string(buf, end, ptr, spec, fmt); 2278ce4fecf1SPantelis Antoniou case 'O': 227983abc5a7SSakari Ailus return device_node_string(buf, end, ptr, spec, fmt + 1); 22803bd32d6aSSakari Ailus case 'f': 22813bd32d6aSSakari Ailus return fwnode_string(buf, end, ptr, spec, fmt + 1); 22827b1924a1STobin C. Harding case 'x': 22837b1924a1STobin C. Harding return pointer_string(buf, end, ptr, spec); 228457f5677eSRasmus Villemoes case 'e': 228557f5677eSRasmus Villemoes /* %pe with a non-ERR_PTR gets treated as plain %p */ 228657f5677eSRasmus Villemoes if (!IS_ERR(ptr)) 228757f5677eSRasmus Villemoes break; 228857f5677eSRasmus Villemoes return err_ptr(buf, end, ptr, spec); 2289b2a5212fSDaniel Borkmann case 'u': 2290b2a5212fSDaniel Borkmann case 'k': 2291b2a5212fSDaniel Borkmann switch (fmt[1]) { 2292b2a5212fSDaniel Borkmann case 's': 2293b2a5212fSDaniel Borkmann return string(buf, end, ptr, spec); 2294b2a5212fSDaniel Borkmann default: 2295b2a5212fSDaniel Borkmann return error_string(buf, end, "(einval)", spec); 2296b2a5212fSDaniel Borkmann } 22970fe1ef24SLinus Torvalds } 2298fef20d9cSFrederic Weisbecker 2299ad67b74dSTobin C. Harding /* default is to _not_ leak addresses, hash before printing */ 2300ad67b74dSTobin C. Harding return ptr_to_id(buf, end, ptr, spec); 2301fef20d9cSFrederic Weisbecker } 2302fef20d9cSFrederic Weisbecker 2303fef20d9cSFrederic Weisbecker /* 2304fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 2305fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 2306fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 2307fef20d9cSFrederic Weisbecker * to go on the next call). 2308fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 2309fef20d9cSFrederic Weisbecker * 2310fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 2311fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 2312fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 23135b5e0928SAlexey Dobriyan * 'Z' changed to 'z' --adobriyan 2017-01-25 2314fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 2315fef20d9cSFrederic Weisbecker * 2316fef20d9cSFrederic Weisbecker * @fmt: the format string 2317fef20d9cSFrederic Weisbecker * @type of the token returned 2318fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 2319fef20d9cSFrederic Weisbecker * @field_width: overwritten width 2320fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 2321fef20d9cSFrederic Weisbecker * @precision: precision of a number 2322fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 2323fef20d9cSFrederic Weisbecker */ 2324cf3b429bSJoe Perches static noinline_for_stack 2325cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 2326fef20d9cSFrederic Weisbecker { 2327fef20d9cSFrederic Weisbecker const char *start = fmt; 2328d0484193SRasmus Villemoes char qualifier; 2329fef20d9cSFrederic Weisbecker 2330fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 2331ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 2332fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 2333fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 2334fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 2335fef20d9cSFrederic Weisbecker } 2336fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2337fef20d9cSFrederic Weisbecker goto precision; 2338fef20d9cSFrederic Weisbecker } 2339fef20d9cSFrederic Weisbecker 2340fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 2341fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 2342fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2343fef20d9cSFrederic Weisbecker spec->precision = 0; 2344fef20d9cSFrederic Weisbecker 2345fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2346fef20d9cSFrederic Weisbecker goto qualifier; 2347fef20d9cSFrederic Weisbecker } 2348fef20d9cSFrederic Weisbecker 2349fef20d9cSFrederic Weisbecker /* By default */ 2350fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2351fef20d9cSFrederic Weisbecker 2352fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 2353fef20d9cSFrederic Weisbecker if (*fmt == '%') 2354fef20d9cSFrederic Weisbecker break; 2355fef20d9cSFrederic Weisbecker } 2356fef20d9cSFrederic Weisbecker 2357fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 2358fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 2359fef20d9cSFrederic Weisbecker return fmt - start; 2360fef20d9cSFrederic Weisbecker 2361fef20d9cSFrederic Weisbecker /* Process flags */ 2362fef20d9cSFrederic Weisbecker spec->flags = 0; 2363fef20d9cSFrederic Weisbecker 2364fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 2365fef20d9cSFrederic Weisbecker bool found = true; 2366fef20d9cSFrederic Weisbecker 2367fef20d9cSFrederic Weisbecker ++fmt; 2368fef20d9cSFrederic Weisbecker 2369fef20d9cSFrederic Weisbecker switch (*fmt) { 2370fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 2371fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 2372fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 2373fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 2374fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 2375fef20d9cSFrederic Weisbecker default: found = false; 2376fef20d9cSFrederic Weisbecker } 2377fef20d9cSFrederic Weisbecker 2378fef20d9cSFrederic Weisbecker if (!found) 2379fef20d9cSFrederic Weisbecker break; 2380fef20d9cSFrederic Weisbecker } 2381fef20d9cSFrederic Weisbecker 2382fef20d9cSFrederic Weisbecker /* get field width */ 2383fef20d9cSFrederic Weisbecker spec->field_width = -1; 2384fef20d9cSFrederic Weisbecker 2385fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 2386fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 2387fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 2388fef20d9cSFrederic Weisbecker /* it's the next argument */ 2389ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 2390fef20d9cSFrederic Weisbecker return ++fmt - start; 2391fef20d9cSFrederic Weisbecker } 2392fef20d9cSFrederic Weisbecker 2393fef20d9cSFrederic Weisbecker precision: 2394fef20d9cSFrederic Weisbecker /* get the precision */ 2395fef20d9cSFrederic Weisbecker spec->precision = -1; 2396fef20d9cSFrederic Weisbecker if (*fmt == '.') { 2397fef20d9cSFrederic Weisbecker ++fmt; 2398fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 2399fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 2400fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2401fef20d9cSFrederic Weisbecker spec->precision = 0; 2402fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 2403fef20d9cSFrederic Weisbecker /* it's the next argument */ 2404adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 2405fef20d9cSFrederic Weisbecker return ++fmt - start; 2406fef20d9cSFrederic Weisbecker } 2407fef20d9cSFrederic Weisbecker } 2408fef20d9cSFrederic Weisbecker 2409fef20d9cSFrederic Weisbecker qualifier: 2410fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 2411d0484193SRasmus Villemoes qualifier = 0; 241275fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 24135b5e0928SAlexey Dobriyan *fmt == 'z' || *fmt == 't') { 2414d0484193SRasmus Villemoes qualifier = *fmt++; 2415d0484193SRasmus Villemoes if (unlikely(qualifier == *fmt)) { 2416d0484193SRasmus Villemoes if (qualifier == 'l') { 2417d0484193SRasmus Villemoes qualifier = 'L'; 2418fef20d9cSFrederic Weisbecker ++fmt; 2419d0484193SRasmus Villemoes } else if (qualifier == 'h') { 2420d0484193SRasmus Villemoes qualifier = 'H'; 2421a4e94ef0SZhaolei ++fmt; 2422a4e94ef0SZhaolei } 2423fef20d9cSFrederic Weisbecker } 2424fef20d9cSFrederic Weisbecker } 2425fef20d9cSFrederic Weisbecker 2426fef20d9cSFrederic Weisbecker /* default base */ 2427fef20d9cSFrederic Weisbecker spec->base = 10; 2428fef20d9cSFrederic Weisbecker switch (*fmt) { 2429fef20d9cSFrederic Weisbecker case 'c': 2430fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 2431fef20d9cSFrederic Weisbecker return ++fmt - start; 2432fef20d9cSFrederic Weisbecker 2433fef20d9cSFrederic Weisbecker case 's': 2434fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 2435fef20d9cSFrederic Weisbecker return ++fmt - start; 2436fef20d9cSFrederic Weisbecker 2437fef20d9cSFrederic Weisbecker case 'p': 2438fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 2439ffbfed03SRasmus Villemoes return ++fmt - start; 2440fef20d9cSFrederic Weisbecker 2441fef20d9cSFrederic Weisbecker case '%': 2442fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 2443fef20d9cSFrederic Weisbecker return ++fmt - start; 2444fef20d9cSFrederic Weisbecker 2445fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 2446fef20d9cSFrederic Weisbecker case 'o': 2447fef20d9cSFrederic Weisbecker spec->base = 8; 2448fef20d9cSFrederic Weisbecker break; 2449fef20d9cSFrederic Weisbecker 2450fef20d9cSFrederic Weisbecker case 'x': 2451fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 24527e6bd6f3SAndy Shevchenko /* fall through */ 2453fef20d9cSFrederic Weisbecker 2454fef20d9cSFrederic Weisbecker case 'X': 2455fef20d9cSFrederic Weisbecker spec->base = 16; 2456fef20d9cSFrederic Weisbecker break; 2457fef20d9cSFrederic Weisbecker 2458fef20d9cSFrederic Weisbecker case 'd': 2459fef20d9cSFrederic Weisbecker case 'i': 246039e874f8SFrederic Weisbecker spec->flags |= SIGN; 2461fef20d9cSFrederic Weisbecker case 'u': 2462fef20d9cSFrederic Weisbecker break; 2463fef20d9cSFrederic Weisbecker 2464708d96fdSRyan Mallon case 'n': 2465708d96fdSRyan Mallon /* 2466b006f19bSRasmus Villemoes * Since %n poses a greater security risk than 2467b006f19bSRasmus Villemoes * utility, treat it as any other invalid or 2468b006f19bSRasmus Villemoes * unsupported format specifier. 2469708d96fdSRyan Mallon */ 2470708d96fdSRyan Mallon /* Fall-through */ 2471708d96fdSRyan Mallon 2472fef20d9cSFrederic Weisbecker default: 2473b006f19bSRasmus Villemoes WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 2474fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 2475fef20d9cSFrederic Weisbecker return fmt - start; 2476fef20d9cSFrederic Weisbecker } 2477fef20d9cSFrederic Weisbecker 2478d0484193SRasmus Villemoes if (qualifier == 'L') 2479fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 2480d0484193SRasmus Villemoes else if (qualifier == 'l') { 248151be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 248251be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 24835b5e0928SAlexey Dobriyan } else if (qualifier == 'z') { 2484fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 2485d0484193SRasmus Villemoes } else if (qualifier == 't') { 2486fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 2487d0484193SRasmus Villemoes } else if (qualifier == 'H') { 248851be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 248951be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 2490d0484193SRasmus Villemoes } else if (qualifier == 'h') { 249151be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 249251be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 2493fef20d9cSFrederic Weisbecker } else { 249451be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 249551be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 2496fef20d9cSFrederic Weisbecker } 2497fef20d9cSFrederic Weisbecker 2498fef20d9cSFrederic Weisbecker return ++fmt - start; 249978a8bf69SLinus Torvalds } 250078a8bf69SLinus Torvalds 25014d72ba01SRasmus Villemoes static void 25024d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width) 25034d72ba01SRasmus Villemoes { 25044d72ba01SRasmus Villemoes spec->field_width = width; 25054d72ba01SRasmus Villemoes if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 25064d72ba01SRasmus Villemoes spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 25074d72ba01SRasmus Villemoes } 25084d72ba01SRasmus Villemoes } 25094d72ba01SRasmus Villemoes 25104d72ba01SRasmus Villemoes static void 25114d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec) 25124d72ba01SRasmus Villemoes { 25134d72ba01SRasmus Villemoes spec->precision = prec; 25144d72ba01SRasmus Villemoes if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 25154d72ba01SRasmus Villemoes spec->precision = clamp(prec, 0, PRECISION_MAX); 25164d72ba01SRasmus Villemoes } 25174d72ba01SRasmus Villemoes } 25184d72ba01SRasmus Villemoes 25191da177e4SLinus Torvalds /** 25201da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 25211da177e4SLinus Torvalds * @buf: The buffer to place the result into 25221da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 25231da177e4SLinus Torvalds * @fmt: The format string to use 25241da177e4SLinus Torvalds * @args: Arguments for the format string 25251da177e4SLinus Torvalds * 2526d7ec9a05SRasmus Villemoes * This function generally follows C99 vsnprintf, but has some 2527d7ec9a05SRasmus Villemoes * extensions and a few limitations: 2528d7ec9a05SRasmus Villemoes * 25296cc89134Smchehab@s-opensource.com * - ``%n`` is unsupported 25306cc89134Smchehab@s-opensource.com * - ``%p*`` is handled by pointer() 253120036fdcSAndi Kleen * 253227e7c0e8SJonathan Corbet * See pointer() or Documentation/core-api/printk-formats.rst for more 25335e4ee7b1SMartin Kletzander * extensive description. 25345e4ee7b1SMartin Kletzander * 25355e4ee7b1SMartin Kletzander * **Please update the documentation in both places when making changes** 253680f548e0SAndrew Morton * 25371da177e4SLinus Torvalds * The return value is the number of characters which would 25381da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 25391da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 25401da177e4SLinus Torvalds * number of characters written into @buf as return value 254172fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 25421da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 25431da177e4SLinus Torvalds * string is truncated. 25441da177e4SLinus Torvalds * 2545ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 25461da177e4SLinus Torvalds */ 25471da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 25481da177e4SLinus Torvalds { 25491da177e4SLinus Torvalds unsigned long long num; 2550d4be151bSAndré Goddard Rosa char *str, *end; 2551fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 25521da177e4SLinus Torvalds 2553f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 2554f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 25552aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 25561da177e4SLinus Torvalds return 0; 25571da177e4SLinus Torvalds 25581da177e4SLinus Torvalds str = buf; 2559f796937aSJeremy Fitzhardinge end = buf + size; 25601da177e4SLinus Torvalds 2561f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 2562f796937aSJeremy Fitzhardinge if (end < buf) { 25631da177e4SLinus Torvalds end = ((void *)-1); 2564f796937aSJeremy Fitzhardinge size = end - buf; 25651da177e4SLinus Torvalds } 25661da177e4SLinus Torvalds 2567fef20d9cSFrederic Weisbecker while (*fmt) { 2568fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2569d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2570fef20d9cSFrederic Weisbecker 2571fef20d9cSFrederic Weisbecker fmt += read; 2572fef20d9cSFrederic Weisbecker 2573fef20d9cSFrederic Weisbecker switch (spec.type) { 2574fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2575fef20d9cSFrederic Weisbecker int copy = read; 2576fef20d9cSFrederic Weisbecker if (str < end) { 2577fef20d9cSFrederic Weisbecker if (copy > end - str) 2578fef20d9cSFrederic Weisbecker copy = end - str; 2579fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2580fef20d9cSFrederic Weisbecker } 2581fef20d9cSFrederic Weisbecker str += read; 2582fef20d9cSFrederic Weisbecker break; 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 2585ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 25864d72ba01SRasmus Villemoes set_field_width(&spec, va_arg(args, int)); 2587fef20d9cSFrederic Weisbecker break; 25881da177e4SLinus Torvalds 2589fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 25904d72ba01SRasmus Villemoes set_precision(&spec, va_arg(args, int)); 2591fef20d9cSFrederic Weisbecker break; 25921da177e4SLinus Torvalds 2593d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2594d4be151bSAndré Goddard Rosa char c; 2595d4be151bSAndré Goddard Rosa 2596fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2597fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2598f796937aSJeremy Fitzhardinge if (str < end) 25991da177e4SLinus Torvalds *str = ' '; 26001da177e4SLinus Torvalds ++str; 2601fef20d9cSFrederic Weisbecker 26021da177e4SLinus Torvalds } 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 2605f796937aSJeremy Fitzhardinge if (str < end) 26061da177e4SLinus Torvalds *str = c; 26071da177e4SLinus Torvalds ++str; 2608fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2609f796937aSJeremy Fitzhardinge if (str < end) 26101da177e4SLinus Torvalds *str = ' '; 26111da177e4SLinus Torvalds ++str; 26121da177e4SLinus Torvalds } 2613fef20d9cSFrederic Weisbecker break; 2614d4be151bSAndré Goddard Rosa } 26151da177e4SLinus Torvalds 2616fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 2617fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 2618fef20d9cSFrederic Weisbecker break; 26191da177e4SLinus Torvalds 2620fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2621ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 2622fef20d9cSFrederic Weisbecker spec); 2623fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 26244d8a743cSLinus Torvalds fmt++; 2625fef20d9cSFrederic Weisbecker break; 26261da177e4SLinus Torvalds 2627fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2628f796937aSJeremy Fitzhardinge if (str < end) 26291da177e4SLinus Torvalds *str = '%'; 26301da177e4SLinus Torvalds ++str; 26311da177e4SLinus Torvalds break; 26321da177e4SLinus Torvalds 2633fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 2634b006f19bSRasmus Villemoes /* 2635b006f19bSRasmus Villemoes * Presumably the arguments passed gcc's type 2636b006f19bSRasmus Villemoes * checking, but there is no safe or sane way 2637b006f19bSRasmus Villemoes * for us to continue parsing the format and 2638b006f19bSRasmus Villemoes * fetching from the va_list; the remaining 2639b006f19bSRasmus Villemoes * specifiers and arguments would be out of 2640b006f19bSRasmus Villemoes * sync. 2641b006f19bSRasmus Villemoes */ 2642b006f19bSRasmus Villemoes goto out; 2643fef20d9cSFrederic Weisbecker 2644fef20d9cSFrederic Weisbecker default: 2645fef20d9cSFrederic Weisbecker switch (spec.type) { 2646fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2647fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 2648fef20d9cSFrederic Weisbecker break; 2649fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2650fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 2651fef20d9cSFrederic Weisbecker break; 2652fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2653fef20d9cSFrederic Weisbecker num = va_arg(args, long); 2654fef20d9cSFrederic Weisbecker break; 2655fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2656ef124960SJason Gunthorpe if (spec.flags & SIGN) 2657ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 2658ef124960SJason Gunthorpe else 2659fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 2660fef20d9cSFrederic Weisbecker break; 2661fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2662fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 2663fef20d9cSFrederic Weisbecker break; 2664a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2665a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 2666a4e94ef0SZhaolei break; 2667a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2668a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 2669a4e94ef0SZhaolei break; 2670fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2671fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 2672fef20d9cSFrederic Weisbecker break; 2673fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2674fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 2675fef20d9cSFrederic Weisbecker break; 267639e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 267739e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 2678fef20d9cSFrederic Weisbecker break; 2679fef20d9cSFrederic Weisbecker default: 2680fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 26811da177e4SLinus Torvalds } 2682fef20d9cSFrederic Weisbecker 2683fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 26841da177e4SLinus Torvalds } 2685fef20d9cSFrederic Weisbecker } 2686fef20d9cSFrederic Weisbecker 2687b006f19bSRasmus Villemoes out: 2688f796937aSJeremy Fitzhardinge if (size > 0) { 2689f796937aSJeremy Fitzhardinge if (str < end) 26901da177e4SLinus Torvalds *str = '\0'; 2691f796937aSJeremy Fitzhardinge else 26920a6047eeSLinus Torvalds end[-1] = '\0'; 2693f796937aSJeremy Fitzhardinge } 2694fef20d9cSFrederic Weisbecker 2695f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 26961da177e4SLinus Torvalds return str-buf; 2697fef20d9cSFrederic Weisbecker 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 27001da177e4SLinus Torvalds 27011da177e4SLinus Torvalds /** 27021da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 27031da177e4SLinus Torvalds * @buf: The buffer to place the result into 27041da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 27051da177e4SLinus Torvalds * @fmt: The format string to use 27061da177e4SLinus Torvalds * @args: Arguments for the format string 27071da177e4SLinus Torvalds * 27081da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2709b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 27101da177e4SLinus Torvalds * returns 0. 27111da177e4SLinus Torvalds * 2712ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 271320036fdcSAndi Kleen * 271420036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 27151da177e4SLinus Torvalds */ 27161da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 27171da177e4SLinus Torvalds { 27181da177e4SLinus Torvalds int i; 27191da177e4SLinus Torvalds 27201da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 27217b9186f5SAndré Goddard Rosa 2722b921c69fSAnton Arapov if (likely(i < size)) 2723b921c69fSAnton Arapov return i; 2724b921c69fSAnton Arapov if (size != 0) 2725b921c69fSAnton Arapov return size - 1; 2726b921c69fSAnton Arapov return 0; 27271da177e4SLinus Torvalds } 27281da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 27291da177e4SLinus Torvalds 27301da177e4SLinus Torvalds /** 27311da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 27321da177e4SLinus Torvalds * @buf: The buffer to place the result into 27331da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 27341da177e4SLinus Torvalds * @fmt: The format string to use 27351da177e4SLinus Torvalds * @...: Arguments for the format string 27361da177e4SLinus Torvalds * 27371da177e4SLinus Torvalds * The return value is the number of characters which would be 27381da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 27391da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 27401da177e4SLinus Torvalds * @size, the resulting string is truncated. 274120036fdcSAndi Kleen * 274220036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 27431da177e4SLinus Torvalds */ 27441da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 27451da177e4SLinus Torvalds { 27461da177e4SLinus Torvalds va_list args; 27471da177e4SLinus Torvalds int i; 27481da177e4SLinus Torvalds 27491da177e4SLinus Torvalds va_start(args, fmt); 27501da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 27511da177e4SLinus Torvalds va_end(args); 27527b9186f5SAndré Goddard Rosa 27531da177e4SLinus Torvalds return i; 27541da177e4SLinus Torvalds } 27551da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 27561da177e4SLinus Torvalds 27571da177e4SLinus Torvalds /** 27581da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 27591da177e4SLinus Torvalds * @buf: The buffer to place the result into 27601da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 27611da177e4SLinus Torvalds * @fmt: The format string to use 27621da177e4SLinus Torvalds * @...: Arguments for the format string 27631da177e4SLinus Torvalds * 27641da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2765b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 27661da177e4SLinus Torvalds */ 27671da177e4SLinus Torvalds 27681da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 27691da177e4SLinus Torvalds { 27701da177e4SLinus Torvalds va_list args; 27711da177e4SLinus Torvalds int i; 27721da177e4SLinus Torvalds 27731da177e4SLinus Torvalds va_start(args, fmt); 2774b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 27751da177e4SLinus Torvalds va_end(args); 27767b9186f5SAndré Goddard Rosa 2777b903c0b8SChangli Gao return i; 27781da177e4SLinus Torvalds } 27791da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 27801da177e4SLinus Torvalds 27811da177e4SLinus Torvalds /** 27821da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 27831da177e4SLinus Torvalds * @buf: The buffer to place the result into 27841da177e4SLinus Torvalds * @fmt: The format string to use 27851da177e4SLinus Torvalds * @args: Arguments for the format string 27861da177e4SLinus Torvalds * 27871da177e4SLinus Torvalds * The function returns the number of characters written 278872fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 27891da177e4SLinus Torvalds * buffer overflows. 27901da177e4SLinus Torvalds * 2791ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 279220036fdcSAndi Kleen * 279320036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 27941da177e4SLinus Torvalds */ 27951da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 27961da177e4SLinus Torvalds { 27971da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds /** 28021da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 28031da177e4SLinus Torvalds * @buf: The buffer to place the result into 28041da177e4SLinus Torvalds * @fmt: The format string to use 28051da177e4SLinus Torvalds * @...: Arguments for the format string 28061da177e4SLinus Torvalds * 28071da177e4SLinus Torvalds * The function returns the number of characters written 280872fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 28091da177e4SLinus Torvalds * buffer overflows. 281020036fdcSAndi Kleen * 281120036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 28121da177e4SLinus Torvalds */ 28131da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 28141da177e4SLinus Torvalds { 28151da177e4SLinus Torvalds va_list args; 28161da177e4SLinus Torvalds int i; 28171da177e4SLinus Torvalds 28181da177e4SLinus Torvalds va_start(args, fmt); 28191da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 28201da177e4SLinus Torvalds va_end(args); 28217b9186f5SAndré Goddard Rosa 28221da177e4SLinus Torvalds return i; 28231da177e4SLinus Torvalds } 28241da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 28251da177e4SLinus Torvalds 28264370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 28274370aa4aSLai Jiangshan /* 28284370aa4aSLai Jiangshan * bprintf service: 28294370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 28304370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 28314370aa4aSLai Jiangshan */ 28324370aa4aSLai Jiangshan 28334370aa4aSLai Jiangshan /** 28344370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 28354370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 28364370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 28374370aa4aSLai Jiangshan * @fmt: The format string to use 28384370aa4aSLai Jiangshan * @args: Arguments for the format string 28394370aa4aSLai Jiangshan * 28404370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 2841da3dae54SMasanari Iida * is skipped. 28424370aa4aSLai Jiangshan * 28434370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 28444370aa4aSLai Jiangshan * the given input. 28454370aa4aSLai Jiangshan * 28464370aa4aSLai Jiangshan * NOTE: 28474370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 28484370aa4aSLai Jiangshan * valid for bstr_printf(). 28494370aa4aSLai Jiangshan */ 28504370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 28514370aa4aSLai Jiangshan { 2852fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 28534370aa4aSLai Jiangshan char *str, *end; 2854841a915dSSteven Rostedt (VMware) int width; 28554370aa4aSLai Jiangshan 28564370aa4aSLai Jiangshan str = (char *)bin_buf; 28574370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 28584370aa4aSLai Jiangshan 28594370aa4aSLai Jiangshan #define save_arg(type) \ 2860841a915dSSteven Rostedt (VMware) ({ \ 28614370aa4aSLai Jiangshan unsigned long long value; \ 2862841a915dSSteven Rostedt (VMware) if (sizeof(type) == 8) { \ 2863841a915dSSteven Rostedt (VMware) unsigned long long val8; \ 28644370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 2865841a915dSSteven Rostedt (VMware) val8 = va_arg(args, unsigned long long); \ 28664370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 2867841a915dSSteven Rostedt (VMware) *(u32 *)str = *(u32 *)&val8; \ 2868841a915dSSteven Rostedt (VMware) *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 28694370aa4aSLai Jiangshan } \ 2870841a915dSSteven Rostedt (VMware) value = val8; \ 28714370aa4aSLai Jiangshan } else { \ 2872841a915dSSteven Rostedt (VMware) unsigned int val4; \ 28734370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 2874841a915dSSteven Rostedt (VMware) val4 = va_arg(args, int); \ 28754370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 2876841a915dSSteven Rostedt (VMware) *(typeof(type) *)str = (type)(long)val4; \ 2877841a915dSSteven Rostedt (VMware) value = (unsigned long long)val4; \ 28784370aa4aSLai Jiangshan } \ 28794370aa4aSLai Jiangshan str += sizeof(type); \ 2880841a915dSSteven Rostedt (VMware) value; \ 2881841a915dSSteven Rostedt (VMware) }) 28824370aa4aSLai Jiangshan 2883fef20d9cSFrederic Weisbecker while (*fmt) { 2884d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 28854370aa4aSLai Jiangshan 2886fef20d9cSFrederic Weisbecker fmt += read; 2887fef20d9cSFrederic Weisbecker 2888fef20d9cSFrederic Weisbecker switch (spec.type) { 2889fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 2890d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 2891fef20d9cSFrederic Weisbecker break; 2892b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2893b006f19bSRasmus Villemoes goto out; 2894fef20d9cSFrederic Weisbecker 2895ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2896fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2897841a915dSSteven Rostedt (VMware) width = (int)save_arg(int); 2898841a915dSSteven Rostedt (VMware) /* Pointers may require the width */ 2899841a915dSSteven Rostedt (VMware) if (*fmt == 'p') 2900841a915dSSteven Rostedt (VMware) set_field_width(&spec, width); 2901fef20d9cSFrederic Weisbecker break; 29024370aa4aSLai Jiangshan 2903fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 29044370aa4aSLai Jiangshan save_arg(char); 2905fef20d9cSFrederic Weisbecker break; 2906fef20d9cSFrederic Weisbecker 2907fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 29084370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 29093e5903ebSPetr Mladek const char *err_msg; 29104370aa4aSLai Jiangshan size_t len; 29116c356634SAndré Goddard Rosa 29123e5903ebSPetr Mladek err_msg = check_pointer_msg(save_str); 29133e5903ebSPetr Mladek if (err_msg) 29143e5903ebSPetr Mladek save_str = err_msg; 29153e5903ebSPetr Mladek 29166c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 29176c356634SAndré Goddard Rosa if (str + len < end) 29186c356634SAndré Goddard Rosa memcpy(str, save_str, len); 29196c356634SAndré Goddard Rosa str += len; 2920fef20d9cSFrederic Weisbecker break; 29214370aa4aSLai Jiangshan } 2922fef20d9cSFrederic Weisbecker 2923fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2924841a915dSSteven Rostedt (VMware) /* Dereferenced pointers must be done now */ 2925841a915dSSteven Rostedt (VMware) switch (*fmt) { 2926841a915dSSteven Rostedt (VMware) /* Dereference of functions is still OK */ 2927841a915dSSteven Rostedt (VMware) case 'S': 2928841a915dSSteven Rostedt (VMware) case 's': 29291e6338cfSSteven Rostedt (VMware) case 'x': 29301e6338cfSSteven Rostedt (VMware) case 'K': 293157f5677eSRasmus Villemoes case 'e': 29324370aa4aSLai Jiangshan save_arg(void *); 2933841a915dSSteven Rostedt (VMware) break; 2934841a915dSSteven Rostedt (VMware) default: 2935841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 2936841a915dSSteven Rostedt (VMware) save_arg(void *); 2937841a915dSSteven Rostedt (VMware) break; 2938841a915dSSteven Rostedt (VMware) } 2939841a915dSSteven Rostedt (VMware) str = pointer(fmt, str, end, va_arg(args, void *), 2940841a915dSSteven Rostedt (VMware) spec); 2941841a915dSSteven Rostedt (VMware) if (str + 1 < end) 2942841a915dSSteven Rostedt (VMware) *str++ = '\0'; 2943841a915dSSteven Rostedt (VMware) else 2944841a915dSSteven Rostedt (VMware) end[-1] = '\0'; /* Must be nul terminated */ 2945841a915dSSteven Rostedt (VMware) } 29464370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2947fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 29484370aa4aSLai Jiangshan fmt++; 2949fef20d9cSFrederic Weisbecker break; 2950fef20d9cSFrederic Weisbecker 2951fef20d9cSFrederic Weisbecker default: 2952fef20d9cSFrederic Weisbecker switch (spec.type) { 2953fef20d9cSFrederic Weisbecker 2954fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2955fef20d9cSFrederic Weisbecker save_arg(long long); 2956fef20d9cSFrederic Weisbecker break; 2957fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2958fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2959fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2960fef20d9cSFrederic Weisbecker break; 2961fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2962fef20d9cSFrederic Weisbecker save_arg(size_t); 2963fef20d9cSFrederic Weisbecker break; 2964fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2965fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 2966fef20d9cSFrederic Weisbecker break; 2967a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2968a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2969a4e94ef0SZhaolei save_arg(char); 2970a4e94ef0SZhaolei break; 2971fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2972fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2973fef20d9cSFrederic Weisbecker save_arg(short); 2974fef20d9cSFrederic Weisbecker break; 2975fef20d9cSFrederic Weisbecker default: 2976fef20d9cSFrederic Weisbecker save_arg(int); 2977fef20d9cSFrederic Weisbecker } 2978fef20d9cSFrederic Weisbecker } 2979fef20d9cSFrederic Weisbecker } 2980fef20d9cSFrederic Weisbecker 2981b006f19bSRasmus Villemoes out: 29827b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2983fef20d9cSFrederic Weisbecker #undef save_arg 29844370aa4aSLai Jiangshan } 29854370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 29864370aa4aSLai Jiangshan 29874370aa4aSLai Jiangshan /** 29884370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 29894370aa4aSLai Jiangshan * @buf: The buffer to place the result into 29904370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 29914370aa4aSLai Jiangshan * @fmt: The format string to use 29924370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 29934370aa4aSLai Jiangshan * 29944370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 29954370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 29964370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 29974370aa4aSLai Jiangshan * 29984370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 29990efb4d20SSteven Rostedt * see vsnprintf comment for details. 30004370aa4aSLai Jiangshan * 30014370aa4aSLai Jiangshan * The return value is the number of characters which would 30024370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 30034370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 30044370aa4aSLai Jiangshan * number of characters written into @buf as return value 30054370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 30064370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 30074370aa4aSLai Jiangshan * string is truncated. 30084370aa4aSLai Jiangshan */ 30094370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 30104370aa4aSLai Jiangshan { 3011fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 3012d4be151bSAndré Goddard Rosa char *str, *end; 3013d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 30144370aa4aSLai Jiangshan 3015762abb51SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 30164370aa4aSLai Jiangshan return 0; 30174370aa4aSLai Jiangshan 30184370aa4aSLai Jiangshan str = buf; 30194370aa4aSLai Jiangshan end = buf + size; 30204370aa4aSLai Jiangshan 30214370aa4aSLai Jiangshan #define get_arg(type) \ 30224370aa4aSLai Jiangshan ({ \ 30234370aa4aSLai Jiangshan typeof(type) value; \ 30244370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 30254370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 30264370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 30274370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 30284370aa4aSLai Jiangshan } else { \ 30294370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 30304370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 30314370aa4aSLai Jiangshan } \ 30324370aa4aSLai Jiangshan args += sizeof(type); \ 30334370aa4aSLai Jiangshan value; \ 30344370aa4aSLai Jiangshan }) 30354370aa4aSLai Jiangshan 30364370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 30374370aa4aSLai Jiangshan if (end < buf) { 30384370aa4aSLai Jiangshan end = ((void *)-1); 30394370aa4aSLai Jiangshan size = end - buf; 30404370aa4aSLai Jiangshan } 30414370aa4aSLai Jiangshan 3042fef20d9cSFrederic Weisbecker while (*fmt) { 3043fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 3044d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 3045fef20d9cSFrederic Weisbecker 3046fef20d9cSFrederic Weisbecker fmt += read; 3047fef20d9cSFrederic Weisbecker 3048fef20d9cSFrederic Weisbecker switch (spec.type) { 3049fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 3050fef20d9cSFrederic Weisbecker int copy = read; 3051fef20d9cSFrederic Weisbecker if (str < end) { 3052fef20d9cSFrederic Weisbecker if (copy > end - str) 3053fef20d9cSFrederic Weisbecker copy = end - str; 3054fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 3055fef20d9cSFrederic Weisbecker } 3056fef20d9cSFrederic Weisbecker str += read; 3057fef20d9cSFrederic Weisbecker break; 30584370aa4aSLai Jiangshan } 30594370aa4aSLai Jiangshan 3060ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 30614d72ba01SRasmus Villemoes set_field_width(&spec, get_arg(int)); 3062fef20d9cSFrederic Weisbecker break; 30634370aa4aSLai Jiangshan 3064fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 30654d72ba01SRasmus Villemoes set_precision(&spec, get_arg(int)); 3066fef20d9cSFrederic Weisbecker break; 30674370aa4aSLai Jiangshan 3068d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 3069d4be151bSAndré Goddard Rosa char c; 3070d4be151bSAndré Goddard Rosa 3071fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 3072fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 30734370aa4aSLai Jiangshan if (str < end) 30744370aa4aSLai Jiangshan *str = ' '; 30754370aa4aSLai Jiangshan ++str; 30764370aa4aSLai Jiangshan } 30774370aa4aSLai Jiangshan } 30784370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 30794370aa4aSLai Jiangshan if (str < end) 30804370aa4aSLai Jiangshan *str = c; 30814370aa4aSLai Jiangshan ++str; 3082fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 30834370aa4aSLai Jiangshan if (str < end) 30844370aa4aSLai Jiangshan *str = ' '; 30854370aa4aSLai Jiangshan ++str; 30864370aa4aSLai Jiangshan } 3087fef20d9cSFrederic Weisbecker break; 3088d4be151bSAndré Goddard Rosa } 30894370aa4aSLai Jiangshan 3090fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 30914370aa4aSLai Jiangshan const char *str_arg = args; 3092d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 3093fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 3094fef20d9cSFrederic Weisbecker break; 30954370aa4aSLai Jiangshan } 30964370aa4aSLai Jiangshan 3097841a915dSSteven Rostedt (VMware) case FORMAT_TYPE_PTR: { 3098841a915dSSteven Rostedt (VMware) bool process = false; 3099841a915dSSteven Rostedt (VMware) int copy, len; 3100841a915dSSteven Rostedt (VMware) /* Non function dereferences were already done */ 3101841a915dSSteven Rostedt (VMware) switch (*fmt) { 3102841a915dSSteven Rostedt (VMware) case 'S': 3103841a915dSSteven Rostedt (VMware) case 's': 3104841a915dSSteven Rostedt (VMware) case 'F': 3105841a915dSSteven Rostedt (VMware) case 'f': 31061e6338cfSSteven Rostedt (VMware) case 'x': 31071e6338cfSSteven Rostedt (VMware) case 'K': 310857f5677eSRasmus Villemoes case 'e': 3109841a915dSSteven Rostedt (VMware) process = true; 3110841a915dSSteven Rostedt (VMware) break; 3111841a915dSSteven Rostedt (VMware) default: 3112841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 3113841a915dSSteven Rostedt (VMware) process = true; 3114841a915dSSteven Rostedt (VMware) break; 3115841a915dSSteven Rostedt (VMware) } 3116841a915dSSteven Rostedt (VMware) /* Pointer dereference was already processed */ 3117841a915dSSteven Rostedt (VMware) if (str < end) { 3118841a915dSSteven Rostedt (VMware) len = copy = strlen(args); 3119841a915dSSteven Rostedt (VMware) if (copy > end - str) 3120841a915dSSteven Rostedt (VMware) copy = end - str; 3121841a915dSSteven Rostedt (VMware) memcpy(str, args, copy); 3122841a915dSSteven Rostedt (VMware) str += len; 312362165600SSteven Rostedt (VMware) args += len + 1; 3124841a915dSSteven Rostedt (VMware) } 3125841a915dSSteven Rostedt (VMware) } 3126841a915dSSteven Rostedt (VMware) if (process) 3127ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 3128841a915dSSteven Rostedt (VMware) 3129fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 31304370aa4aSLai Jiangshan fmt++; 3131fef20d9cSFrederic Weisbecker break; 3132841a915dSSteven Rostedt (VMware) } 31334370aa4aSLai Jiangshan 3134fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 31354370aa4aSLai Jiangshan if (str < end) 31364370aa4aSLai Jiangshan *str = '%'; 31374370aa4aSLai Jiangshan ++str; 3138fef20d9cSFrederic Weisbecker break; 3139fef20d9cSFrederic Weisbecker 3140b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 3141b006f19bSRasmus Villemoes goto out; 3142b006f19bSRasmus Villemoes 3143d4be151bSAndré Goddard Rosa default: { 3144d4be151bSAndré Goddard Rosa unsigned long long num; 3145d4be151bSAndré Goddard Rosa 3146fef20d9cSFrederic Weisbecker switch (spec.type) { 3147fef20d9cSFrederic Weisbecker 3148fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 31494370aa4aSLai Jiangshan num = get_arg(long long); 3150fef20d9cSFrederic Weisbecker break; 3151fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 3152fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 3153fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 3154fef20d9cSFrederic Weisbecker break; 3155fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 31564370aa4aSLai Jiangshan num = get_arg(size_t); 3157fef20d9cSFrederic Weisbecker break; 3158fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 31594370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 3160fef20d9cSFrederic Weisbecker break; 3161a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 3162a4e94ef0SZhaolei num = get_arg(unsigned char); 3163a4e94ef0SZhaolei break; 3164a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 3165a4e94ef0SZhaolei num = get_arg(signed char); 3166a4e94ef0SZhaolei break; 3167fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 3168fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 3169fef20d9cSFrederic Weisbecker break; 3170fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 3171fef20d9cSFrederic Weisbecker num = get_arg(short); 3172fef20d9cSFrederic Weisbecker break; 3173fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 31744370aa4aSLai Jiangshan num = get_arg(unsigned int); 3175fef20d9cSFrederic Weisbecker break; 3176fef20d9cSFrederic Weisbecker default: 3177fef20d9cSFrederic Weisbecker num = get_arg(int); 31784370aa4aSLai Jiangshan } 3179fef20d9cSFrederic Weisbecker 3180fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 3181d4be151bSAndré Goddard Rosa } /* default: */ 3182d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 3183d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 3184fef20d9cSFrederic Weisbecker 3185b006f19bSRasmus Villemoes out: 31864370aa4aSLai Jiangshan if (size > 0) { 31874370aa4aSLai Jiangshan if (str < end) 31884370aa4aSLai Jiangshan *str = '\0'; 31894370aa4aSLai Jiangshan else 31904370aa4aSLai Jiangshan end[-1] = '\0'; 31914370aa4aSLai Jiangshan } 3192fef20d9cSFrederic Weisbecker 31934370aa4aSLai Jiangshan #undef get_arg 31944370aa4aSLai Jiangshan 31954370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 31964370aa4aSLai Jiangshan return str - buf; 31974370aa4aSLai Jiangshan } 31984370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 31994370aa4aSLai Jiangshan 32004370aa4aSLai Jiangshan /** 32014370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 32024370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 32034370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 32044370aa4aSLai Jiangshan * @fmt: The format string to use 32054370aa4aSLai Jiangshan * @...: Arguments for the format string 32064370aa4aSLai Jiangshan * 32074370aa4aSLai Jiangshan * The function returns the number of words(u32) written 32084370aa4aSLai Jiangshan * into @bin_buf. 32094370aa4aSLai Jiangshan */ 32104370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 32114370aa4aSLai Jiangshan { 32124370aa4aSLai Jiangshan va_list args; 32134370aa4aSLai Jiangshan int ret; 32144370aa4aSLai Jiangshan 32154370aa4aSLai Jiangshan va_start(args, fmt); 32164370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 32174370aa4aSLai Jiangshan va_end(args); 32187b9186f5SAndré Goddard Rosa 32194370aa4aSLai Jiangshan return ret; 32204370aa4aSLai Jiangshan } 32214370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 32224370aa4aSLai Jiangshan 32234370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 32244370aa4aSLai Jiangshan 32251da177e4SLinus Torvalds /** 32261da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 32271da177e4SLinus Torvalds * @buf: input buffer 32281da177e4SLinus Torvalds * @fmt: format of buffer 32291da177e4SLinus Torvalds * @args: arguments 32301da177e4SLinus Torvalds */ 32311da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 32321da177e4SLinus Torvalds { 32331da177e4SLinus Torvalds const char *str = buf; 32341da177e4SLinus Torvalds char *next; 32351da177e4SLinus Torvalds char digit; 32361da177e4SLinus Torvalds int num = 0; 3237ef0658f3SJoe Perches u8 qualifier; 323853809751SJan Beulich unsigned int base; 323953809751SJan Beulich union { 324053809751SJan Beulich long long s; 324153809751SJan Beulich unsigned long long u; 324253809751SJan Beulich } val; 3243ef0658f3SJoe Perches s16 field_width; 3244d4be151bSAndré Goddard Rosa bool is_sign; 32451da177e4SLinus Torvalds 3246da99075cSJan Beulich while (*fmt) { 32471da177e4SLinus Torvalds /* skip any white space in format */ 32481da177e4SLinus Torvalds /* white space in format matchs any amount of 32491da177e4SLinus Torvalds * white space, including none, in the input. 32501da177e4SLinus Torvalds */ 32511da177e4SLinus Torvalds if (isspace(*fmt)) { 3252e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 3253e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 32541da177e4SLinus Torvalds } 32551da177e4SLinus Torvalds 32561da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 32571da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 32581da177e4SLinus Torvalds if (*fmt++ != *str++) 32591da177e4SLinus Torvalds break; 32601da177e4SLinus Torvalds continue; 32611da177e4SLinus Torvalds } 32621da177e4SLinus Torvalds 32631da177e4SLinus Torvalds if (!*fmt) 32641da177e4SLinus Torvalds break; 32651da177e4SLinus Torvalds ++fmt; 32661da177e4SLinus Torvalds 32671da177e4SLinus Torvalds /* skip this conversion. 32681da177e4SLinus Torvalds * advance both strings to next white space 32691da177e4SLinus Torvalds */ 32701da177e4SLinus Torvalds if (*fmt == '*') { 3271da99075cSJan Beulich if (!*str) 3272da99075cSJan Beulich break; 3273f9310b2fSJessica Yu while (!isspace(*fmt) && *fmt != '%' && *fmt) { 3274f9310b2fSJessica Yu /* '%*[' not yet supported, invalid format */ 3275f9310b2fSJessica Yu if (*fmt == '[') 3276f9310b2fSJessica Yu return num; 32771da177e4SLinus Torvalds fmt++; 3278f9310b2fSJessica Yu } 32791da177e4SLinus Torvalds while (!isspace(*str) && *str) 32801da177e4SLinus Torvalds str++; 32811da177e4SLinus Torvalds continue; 32821da177e4SLinus Torvalds } 32831da177e4SLinus Torvalds 32841da177e4SLinus Torvalds /* get field width */ 32851da177e4SLinus Torvalds field_width = -1; 328653809751SJan Beulich if (isdigit(*fmt)) { 32871da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 328853809751SJan Beulich if (field_width <= 0) 328953809751SJan Beulich break; 329053809751SJan Beulich } 32911da177e4SLinus Torvalds 32921da177e4SLinus Torvalds /* get conversion qualifier */ 32931da177e4SLinus Torvalds qualifier = -1; 329475fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 32955b5e0928SAlexey Dobriyan *fmt == 'z') { 32961da177e4SLinus Torvalds qualifier = *fmt++; 32971da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 32981da177e4SLinus Torvalds if (qualifier == 'h') { 32991da177e4SLinus Torvalds qualifier = 'H'; 33001da177e4SLinus Torvalds fmt++; 33011da177e4SLinus Torvalds } else if (qualifier == 'l') { 33021da177e4SLinus Torvalds qualifier = 'L'; 33031da177e4SLinus Torvalds fmt++; 33041da177e4SLinus Torvalds } 33051da177e4SLinus Torvalds } 33061da177e4SLinus Torvalds } 33071da177e4SLinus Torvalds 3308da99075cSJan Beulich if (!*fmt) 3309da99075cSJan Beulich break; 3310da99075cSJan Beulich 3311da99075cSJan Beulich if (*fmt == 'n') { 3312da99075cSJan Beulich /* return number of characters read so far */ 3313da99075cSJan Beulich *va_arg(args, int *) = str - buf; 3314da99075cSJan Beulich ++fmt; 3315da99075cSJan Beulich continue; 3316da99075cSJan Beulich } 3317da99075cSJan Beulich 3318da99075cSJan Beulich if (!*str) 33191da177e4SLinus Torvalds break; 33201da177e4SLinus Torvalds 3321d4be151bSAndré Goddard Rosa base = 10; 33223f623ebaSFabian Frederick is_sign = false; 3323d4be151bSAndré Goddard Rosa 33241da177e4SLinus Torvalds switch (*fmt++) { 33251da177e4SLinus Torvalds case 'c': 33261da177e4SLinus Torvalds { 33271da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 33281da177e4SLinus Torvalds if (field_width == -1) 33291da177e4SLinus Torvalds field_width = 1; 33301da177e4SLinus Torvalds do { 33311da177e4SLinus Torvalds *s++ = *str++; 33321da177e4SLinus Torvalds } while (--field_width > 0 && *str); 33331da177e4SLinus Torvalds num++; 33341da177e4SLinus Torvalds } 33351da177e4SLinus Torvalds continue; 33361da177e4SLinus Torvalds case 's': 33371da177e4SLinus Torvalds { 33381da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 33391da177e4SLinus Torvalds if (field_width == -1) 33404be929beSAlexey Dobriyan field_width = SHRT_MAX; 33411da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 3342e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 33431da177e4SLinus Torvalds 33441da177e4SLinus Torvalds /* now copy until next white space */ 33457b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 33461da177e4SLinus Torvalds *s++ = *str++; 33471da177e4SLinus Torvalds *s = '\0'; 33481da177e4SLinus Torvalds num++; 33491da177e4SLinus Torvalds } 33501da177e4SLinus Torvalds continue; 3351f9310b2fSJessica Yu /* 3352f9310b2fSJessica Yu * Warning: This implementation of the '[' conversion specifier 3353f9310b2fSJessica Yu * deviates from its glibc counterpart in the following ways: 3354f9310b2fSJessica Yu * (1) It does NOT support ranges i.e. '-' is NOT a special 3355f9310b2fSJessica Yu * character 3356f9310b2fSJessica Yu * (2) It cannot match the closing bracket ']' itself 3357f9310b2fSJessica Yu * (3) A field width is required 3358f9310b2fSJessica Yu * (4) '%*[' (discard matching input) is currently not supported 3359f9310b2fSJessica Yu * 3360f9310b2fSJessica Yu * Example usage: 3361f9310b2fSJessica Yu * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3362f9310b2fSJessica Yu * buf1, buf2, buf3); 3363f9310b2fSJessica Yu * if (ret < 3) 3364f9310b2fSJessica Yu * // etc.. 3365f9310b2fSJessica Yu */ 3366f9310b2fSJessica Yu case '[': 3367f9310b2fSJessica Yu { 3368f9310b2fSJessica Yu char *s = (char *)va_arg(args, char *); 3369f9310b2fSJessica Yu DECLARE_BITMAP(set, 256) = {0}; 3370f9310b2fSJessica Yu unsigned int len = 0; 3371f9310b2fSJessica Yu bool negate = (*fmt == '^'); 3372f9310b2fSJessica Yu 3373f9310b2fSJessica Yu /* field width is required */ 3374f9310b2fSJessica Yu if (field_width == -1) 3375f9310b2fSJessica Yu return num; 3376f9310b2fSJessica Yu 3377f9310b2fSJessica Yu if (negate) 3378f9310b2fSJessica Yu ++fmt; 3379f9310b2fSJessica Yu 3380f9310b2fSJessica Yu for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 3381f9310b2fSJessica Yu set_bit((u8)*fmt, set); 3382f9310b2fSJessica Yu 3383f9310b2fSJessica Yu /* no ']' or no character set found */ 3384f9310b2fSJessica Yu if (!*fmt || !len) 3385f9310b2fSJessica Yu return num; 3386f9310b2fSJessica Yu ++fmt; 3387f9310b2fSJessica Yu 3388f9310b2fSJessica Yu if (negate) { 3389f9310b2fSJessica Yu bitmap_complement(set, set, 256); 3390f9310b2fSJessica Yu /* exclude null '\0' byte */ 3391f9310b2fSJessica Yu clear_bit(0, set); 3392f9310b2fSJessica Yu } 3393f9310b2fSJessica Yu 3394f9310b2fSJessica Yu /* match must be non-empty */ 3395f9310b2fSJessica Yu if (!test_bit((u8)*str, set)) 3396f9310b2fSJessica Yu return num; 3397f9310b2fSJessica Yu 3398f9310b2fSJessica Yu while (test_bit((u8)*str, set) && field_width--) 3399f9310b2fSJessica Yu *s++ = *str++; 3400f9310b2fSJessica Yu *s = '\0'; 3401f9310b2fSJessica Yu ++num; 3402f9310b2fSJessica Yu } 3403f9310b2fSJessica Yu continue; 34041da177e4SLinus Torvalds case 'o': 34051da177e4SLinus Torvalds base = 8; 34061da177e4SLinus Torvalds break; 34071da177e4SLinus Torvalds case 'x': 34081da177e4SLinus Torvalds case 'X': 34091da177e4SLinus Torvalds base = 16; 34101da177e4SLinus Torvalds break; 34111da177e4SLinus Torvalds case 'i': 34121da177e4SLinus Torvalds base = 0; 34137e6bd6f3SAndy Shevchenko /* fall through */ 34141da177e4SLinus Torvalds case 'd': 34153f623ebaSFabian Frederick is_sign = true; 34167e6bd6f3SAndy Shevchenko /* fall through */ 34171da177e4SLinus Torvalds case 'u': 34181da177e4SLinus Torvalds break; 34191da177e4SLinus Torvalds case '%': 34201da177e4SLinus Torvalds /* looking for '%' in str */ 34211da177e4SLinus Torvalds if (*str++ != '%') 34221da177e4SLinus Torvalds return num; 34231da177e4SLinus Torvalds continue; 34241da177e4SLinus Torvalds default: 34251da177e4SLinus Torvalds /* invalid format; stop here */ 34261da177e4SLinus Torvalds return num; 34271da177e4SLinus Torvalds } 34281da177e4SLinus Torvalds 34291da177e4SLinus Torvalds /* have some sort of integer conversion. 34301da177e4SLinus Torvalds * first, skip white space in buffer. 34311da177e4SLinus Torvalds */ 3432e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 34331da177e4SLinus Torvalds 34341da177e4SLinus Torvalds digit = *str; 34351da177e4SLinus Torvalds if (is_sign && digit == '-') 34361da177e4SLinus Torvalds digit = *(str + 1); 34371da177e4SLinus Torvalds 34381da177e4SLinus Torvalds if (!digit 34391da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 34401da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 34411da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 34421da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 34431da177e4SLinus Torvalds break; 34441da177e4SLinus Torvalds 344553809751SJan Beulich if (is_sign) 344653809751SJan Beulich val.s = qualifier != 'L' ? 344753809751SJan Beulich simple_strtol(str, &next, base) : 344853809751SJan Beulich simple_strtoll(str, &next, base); 344953809751SJan Beulich else 345053809751SJan Beulich val.u = qualifier != 'L' ? 345153809751SJan Beulich simple_strtoul(str, &next, base) : 345253809751SJan Beulich simple_strtoull(str, &next, base); 345353809751SJan Beulich 345453809751SJan Beulich if (field_width > 0 && next - str > field_width) { 345553809751SJan Beulich if (base == 0) 345653809751SJan Beulich _parse_integer_fixup_radix(str, &base); 345753809751SJan Beulich while (next - str > field_width) { 345853809751SJan Beulich if (is_sign) 345953809751SJan Beulich val.s = div_s64(val.s, base); 346053809751SJan Beulich else 346153809751SJan Beulich val.u = div_u64(val.u, base); 346253809751SJan Beulich --next; 346353809751SJan Beulich } 346453809751SJan Beulich } 346553809751SJan Beulich 34661da177e4SLinus Torvalds switch (qualifier) { 34671da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 346853809751SJan Beulich if (is_sign) 346953809751SJan Beulich *va_arg(args, signed char *) = val.s; 347053809751SJan Beulich else 347153809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 34721da177e4SLinus Torvalds break; 34731da177e4SLinus Torvalds case 'h': 347453809751SJan Beulich if (is_sign) 347553809751SJan Beulich *va_arg(args, short *) = val.s; 347653809751SJan Beulich else 347753809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 34781da177e4SLinus Torvalds break; 34791da177e4SLinus Torvalds case 'l': 348053809751SJan Beulich if (is_sign) 348153809751SJan Beulich *va_arg(args, long *) = val.s; 348253809751SJan Beulich else 348353809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 34841da177e4SLinus Torvalds break; 34851da177e4SLinus Torvalds case 'L': 348653809751SJan Beulich if (is_sign) 348753809751SJan Beulich *va_arg(args, long long *) = val.s; 348853809751SJan Beulich else 348953809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 34901da177e4SLinus Torvalds break; 34911da177e4SLinus Torvalds case 'z': 349253809751SJan Beulich *va_arg(args, size_t *) = val.u; 34931da177e4SLinus Torvalds break; 34941da177e4SLinus Torvalds default: 349553809751SJan Beulich if (is_sign) 349653809751SJan Beulich *va_arg(args, int *) = val.s; 349753809751SJan Beulich else 349853809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 34991da177e4SLinus Torvalds break; 35001da177e4SLinus Torvalds } 35011da177e4SLinus Torvalds num++; 35021da177e4SLinus Torvalds 35031da177e4SLinus Torvalds if (!next) 35041da177e4SLinus Torvalds break; 35051da177e4SLinus Torvalds str = next; 35061da177e4SLinus Torvalds } 3507c6b40d16SJohannes Berg 35081da177e4SLinus Torvalds return num; 35091da177e4SLinus Torvalds } 35101da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 35111da177e4SLinus Torvalds 35121da177e4SLinus Torvalds /** 35131da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 35141da177e4SLinus Torvalds * @buf: input buffer 35151da177e4SLinus Torvalds * @fmt: formatting of buffer 35161da177e4SLinus Torvalds * @...: resulting arguments 35171da177e4SLinus Torvalds */ 35181da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 35191da177e4SLinus Torvalds { 35201da177e4SLinus Torvalds va_list args; 35211da177e4SLinus Torvalds int i; 35221da177e4SLinus Torvalds 35231da177e4SLinus Torvalds va_start(args, fmt); 35241da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 35251da177e4SLinus Torvalds va_end(args); 35267b9186f5SAndré Goddard Rosa 35271da177e4SLinus Torvalds return i; 35281da177e4SLinus Torvalds } 35291da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 3530