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); 943700cd59dSChristoph Hellwig if (bdev->bd_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 } 949700cd59dSChristoph Hellwig buf = number(buf, end, bdev->bd_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; 12684c1ca831SNick Desaulniers fallthrough; 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': 1684df561f66SGustavo A. R. Silva uc = true; 16854c1ca831SNick Desaulniers fallthrough; 16869ac6e44eSJoe Perches case 'l': 1687f9727a17SChristoph Hellwig index = guid_index; 16889ac6e44eSJoe Perches break; 16899ac6e44eSJoe Perches case 'B': 16909ac6e44eSJoe Perches uc = true; 16919ac6e44eSJoe Perches break; 16929ac6e44eSJoe Perches } 16939ac6e44eSJoe Perches 16949ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 1695aa4ea1c3SAndy Shevchenko if (uc) 1696aa4ea1c3SAndy Shevchenko p = hex_byte_pack_upper(p, addr[index[i]]); 1697aa4ea1c3SAndy Shevchenko else 169855036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 16999ac6e44eSJoe Perches switch (i) { 17009ac6e44eSJoe Perches case 3: 17019ac6e44eSJoe Perches case 5: 17029ac6e44eSJoe Perches case 7: 17039ac6e44eSJoe Perches case 9: 17049ac6e44eSJoe Perches *p++ = '-'; 17059ac6e44eSJoe Perches break; 17069ac6e44eSJoe Perches } 17079ac6e44eSJoe Perches } 17089ac6e44eSJoe Perches 17099ac6e44eSJoe Perches *p = 0; 17109ac6e44eSJoe Perches 1711d529ac41SPetr Mladek return string_nocheck(buf, end, uuid, spec); 17129ac6e44eSJoe Perches } 17139ac6e44eSJoe Perches 17145b17aecfSAndy Shevchenko static noinline_for_stack 1715431bca24SGeert Uytterhoeven char *netdev_bits(char *buf, char *end, const void *addr, 1716431bca24SGeert Uytterhoeven struct printf_spec spec, const char *fmt) 1717c8f44affSMichał Mirosław { 17185b17aecfSAndy Shevchenko unsigned long long num; 17195b17aecfSAndy Shevchenko int size; 17205b17aecfSAndy Shevchenko 17213e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 17223e5903ebSPetr Mladek return buf; 17233e5903ebSPetr Mladek 17245b17aecfSAndy Shevchenko switch (fmt[1]) { 17255b17aecfSAndy Shevchenko case 'F': 17265b17aecfSAndy Shevchenko num = *(const netdev_features_t *)addr; 17275b17aecfSAndy Shevchenko size = sizeof(netdev_features_t); 17285b17aecfSAndy Shevchenko break; 17295b17aecfSAndy Shevchenko default: 1730c8c3b584SPetr Mladek return error_string(buf, end, "(%pN?)", spec); 17315b17aecfSAndy Shevchenko } 1732c8f44affSMichał Mirosław 17333cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1734c8f44affSMichał Mirosław } 1735c8f44affSMichał Mirosław 1736aaf07621SJoe Perches static noinline_for_stack 17373e5903ebSPetr Mladek char *address_val(char *buf, char *end, const void *addr, 17383e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 1739aaf07621SJoe Perches { 1740aaf07621SJoe Perches unsigned long long num; 17413cab1e71SAndy Shevchenko int size; 1742aaf07621SJoe Perches 17433e5903ebSPetr Mladek if (check_pointer(&buf, end, addr, spec)) 17443e5903ebSPetr Mladek return buf; 17453e5903ebSPetr Mladek 1746aaf07621SJoe Perches switch (fmt[1]) { 1747aaf07621SJoe Perches case 'd': 1748aaf07621SJoe Perches num = *(const dma_addr_t *)addr; 17493cab1e71SAndy Shevchenko size = sizeof(dma_addr_t); 1750aaf07621SJoe Perches break; 1751aaf07621SJoe Perches case 'p': 1752aaf07621SJoe Perches default: 1753aaf07621SJoe Perches num = *(const phys_addr_t *)addr; 17543cab1e71SAndy Shevchenko size = sizeof(phys_addr_t); 1755aaf07621SJoe Perches break; 1756aaf07621SJoe Perches } 1757aaf07621SJoe Perches 17583cab1e71SAndy Shevchenko return special_hex_number(buf, end, num, size); 1759aaf07621SJoe Perches } 1760aaf07621SJoe Perches 1761900cca29SGeert Uytterhoeven static noinline_for_stack 17624d42c447SAndy Shevchenko char *date_str(char *buf, char *end, const struct rtc_time *tm, bool r) 17634d42c447SAndy Shevchenko { 17644d42c447SAndy Shevchenko int year = tm->tm_year + (r ? 0 : 1900); 17654d42c447SAndy Shevchenko int mon = tm->tm_mon + (r ? 0 : 1); 17664d42c447SAndy Shevchenko 17674d42c447SAndy Shevchenko buf = number(buf, end, year, default_dec04_spec); 17684d42c447SAndy Shevchenko if (buf < end) 17694d42c447SAndy Shevchenko *buf = '-'; 17704d42c447SAndy Shevchenko buf++; 17714d42c447SAndy Shevchenko 17724d42c447SAndy Shevchenko buf = number(buf, end, mon, default_dec02_spec); 17734d42c447SAndy Shevchenko if (buf < end) 17744d42c447SAndy Shevchenko *buf = '-'; 17754d42c447SAndy Shevchenko buf++; 17764d42c447SAndy Shevchenko 17774d42c447SAndy Shevchenko return number(buf, end, tm->tm_mday, default_dec02_spec); 17784d42c447SAndy Shevchenko } 17794d42c447SAndy Shevchenko 17804d42c447SAndy Shevchenko static noinline_for_stack 17814d42c447SAndy Shevchenko char *time_str(char *buf, char *end, const struct rtc_time *tm, bool r) 17824d42c447SAndy Shevchenko { 17834d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_hour, default_dec02_spec); 17844d42c447SAndy Shevchenko if (buf < end) 17854d42c447SAndy Shevchenko *buf = ':'; 17864d42c447SAndy Shevchenko buf++; 17874d42c447SAndy Shevchenko 17884d42c447SAndy Shevchenko buf = number(buf, end, tm->tm_min, default_dec02_spec); 17894d42c447SAndy Shevchenko if (buf < end) 17904d42c447SAndy Shevchenko *buf = ':'; 17914d42c447SAndy Shevchenko buf++; 17924d42c447SAndy Shevchenko 17934d42c447SAndy Shevchenko return number(buf, end, tm->tm_sec, default_dec02_spec); 17944d42c447SAndy Shevchenko } 17954d42c447SAndy Shevchenko 17964d42c447SAndy Shevchenko static noinline_for_stack 17973e5903ebSPetr Mladek char *rtc_str(char *buf, char *end, const struct rtc_time *tm, 17983e5903ebSPetr Mladek struct printf_spec spec, const char *fmt) 17994d42c447SAndy Shevchenko { 18004d42c447SAndy Shevchenko bool have_t = true, have_d = true; 18014d42c447SAndy Shevchenko bool raw = false; 18024d42c447SAndy Shevchenko int count = 2; 18034d42c447SAndy Shevchenko 18043e5903ebSPetr Mladek if (check_pointer(&buf, end, tm, spec)) 18053e5903ebSPetr Mladek return buf; 18063e5903ebSPetr Mladek 18074d42c447SAndy Shevchenko switch (fmt[count]) { 18084d42c447SAndy Shevchenko case 'd': 18094d42c447SAndy Shevchenko have_t = false; 18104d42c447SAndy Shevchenko count++; 18114d42c447SAndy Shevchenko break; 18124d42c447SAndy Shevchenko case 't': 18134d42c447SAndy Shevchenko have_d = false; 18144d42c447SAndy Shevchenko count++; 18154d42c447SAndy Shevchenko break; 18164d42c447SAndy Shevchenko } 18174d42c447SAndy Shevchenko 18184d42c447SAndy Shevchenko raw = fmt[count] == 'r'; 18194d42c447SAndy Shevchenko 18204d42c447SAndy Shevchenko if (have_d) 18214d42c447SAndy Shevchenko buf = date_str(buf, end, tm, raw); 18224d42c447SAndy Shevchenko if (have_d && have_t) { 18234d42c447SAndy Shevchenko /* Respect ISO 8601 */ 18244d42c447SAndy Shevchenko if (buf < end) 18254d42c447SAndy Shevchenko *buf = 'T'; 18264d42c447SAndy Shevchenko buf++; 18274d42c447SAndy Shevchenko } 18284d42c447SAndy Shevchenko if (have_t) 18294d42c447SAndy Shevchenko buf = time_str(buf, end, tm, raw); 18304d42c447SAndy Shevchenko 18314d42c447SAndy Shevchenko return buf; 18324d42c447SAndy Shevchenko } 18334d42c447SAndy Shevchenko 18344d42c447SAndy Shevchenko static noinline_for_stack 18357daac5b2SAndy Shevchenko char *time64_str(char *buf, char *end, const time64_t time, 18367daac5b2SAndy Shevchenko struct printf_spec spec, const char *fmt) 18377daac5b2SAndy Shevchenko { 18387daac5b2SAndy Shevchenko struct rtc_time rtc_time; 18397daac5b2SAndy Shevchenko struct tm tm; 18407daac5b2SAndy Shevchenko 18417daac5b2SAndy Shevchenko time64_to_tm(time, 0, &tm); 18427daac5b2SAndy Shevchenko 18437daac5b2SAndy Shevchenko rtc_time.tm_sec = tm.tm_sec; 18447daac5b2SAndy Shevchenko rtc_time.tm_min = tm.tm_min; 18457daac5b2SAndy Shevchenko rtc_time.tm_hour = tm.tm_hour; 18467daac5b2SAndy Shevchenko rtc_time.tm_mday = tm.tm_mday; 18477daac5b2SAndy Shevchenko rtc_time.tm_mon = tm.tm_mon; 18487daac5b2SAndy Shevchenko rtc_time.tm_year = tm.tm_year; 18497daac5b2SAndy Shevchenko rtc_time.tm_wday = tm.tm_wday; 18507daac5b2SAndy Shevchenko rtc_time.tm_yday = tm.tm_yday; 18517daac5b2SAndy Shevchenko 18527daac5b2SAndy Shevchenko rtc_time.tm_isdst = 0; 18537daac5b2SAndy Shevchenko 18547daac5b2SAndy Shevchenko return rtc_str(buf, end, &rtc_time, spec, fmt); 18557daac5b2SAndy Shevchenko } 18567daac5b2SAndy Shevchenko 18577daac5b2SAndy Shevchenko static noinline_for_stack 18584d42c447SAndy Shevchenko char *time_and_date(char *buf, char *end, void *ptr, struct printf_spec spec, 18594d42c447SAndy Shevchenko const char *fmt) 18604d42c447SAndy Shevchenko { 18614d42c447SAndy Shevchenko switch (fmt[1]) { 18624d42c447SAndy Shevchenko case 'R': 18633e5903ebSPetr Mladek return rtc_str(buf, end, (const struct rtc_time *)ptr, spec, fmt); 18647daac5b2SAndy Shevchenko case 'T': 18657daac5b2SAndy Shevchenko return time64_str(buf, end, *(const time64_t *)ptr, spec, fmt); 18664d42c447SAndy Shevchenko default: 18677daac5b2SAndy Shevchenko return error_string(buf, end, "(%pt?)", spec); 18684d42c447SAndy Shevchenko } 18694d42c447SAndy Shevchenko } 18704d42c447SAndy Shevchenko 18714d42c447SAndy Shevchenko static noinline_for_stack 1872900cca29SGeert Uytterhoeven char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec, 1873900cca29SGeert Uytterhoeven const char *fmt) 1874900cca29SGeert Uytterhoeven { 18750b74d4d7SPetr Mladek if (!IS_ENABLED(CONFIG_HAVE_CLK)) 1876c8c3b584SPetr Mladek return error_string(buf, end, "(%pC?)", spec); 18770b74d4d7SPetr Mladek 18783e5903ebSPetr Mladek if (check_pointer(&buf, end, clk, spec)) 18793e5903ebSPetr Mladek return buf; 1880900cca29SGeert Uytterhoeven 1881900cca29SGeert Uytterhoeven switch (fmt[1]) { 1882900cca29SGeert Uytterhoeven case 'n': 1883900cca29SGeert Uytterhoeven default: 1884900cca29SGeert Uytterhoeven #ifdef CONFIG_COMMON_CLK 1885900cca29SGeert Uytterhoeven return string(buf, end, __clk_get_name(clk), spec); 1886900cca29SGeert Uytterhoeven #else 18874ca96aa9SGeert Uytterhoeven return ptr_to_id(buf, end, clk, spec); 1888900cca29SGeert Uytterhoeven #endif 1889900cca29SGeert Uytterhoeven } 1890900cca29SGeert Uytterhoeven } 1891900cca29SGeert Uytterhoeven 1892edf14cdbSVlastimil Babka static 1893edf14cdbSVlastimil Babka char *format_flags(char *buf, char *end, unsigned long flags, 1894edf14cdbSVlastimil Babka const struct trace_print_flags *names) 1895edf14cdbSVlastimil Babka { 1896edf14cdbSVlastimil Babka unsigned long mask; 1897edf14cdbSVlastimil Babka 1898edf14cdbSVlastimil Babka for ( ; flags && names->name; names++) { 1899edf14cdbSVlastimil Babka mask = names->mask; 1900edf14cdbSVlastimil Babka if ((flags & mask) != mask) 1901edf14cdbSVlastimil Babka continue; 1902edf14cdbSVlastimil Babka 1903abd4fe62SAndy Shevchenko buf = string(buf, end, names->name, default_str_spec); 1904edf14cdbSVlastimil Babka 1905edf14cdbSVlastimil Babka flags &= ~mask; 1906edf14cdbSVlastimil Babka if (flags) { 1907edf14cdbSVlastimil Babka if (buf < end) 1908edf14cdbSVlastimil Babka *buf = '|'; 1909edf14cdbSVlastimil Babka buf++; 1910edf14cdbSVlastimil Babka } 1911edf14cdbSVlastimil Babka } 1912edf14cdbSVlastimil Babka 1913edf14cdbSVlastimil Babka if (flags) 191454433973SAndy Shevchenko buf = number(buf, end, flags, default_flag_spec); 1915edf14cdbSVlastimil Babka 1916edf14cdbSVlastimil Babka return buf; 1917edf14cdbSVlastimil Babka } 1918edf14cdbSVlastimil Babka 1919edf14cdbSVlastimil Babka static noinline_for_stack 19200b74d4d7SPetr Mladek char *flags_string(char *buf, char *end, void *flags_ptr, 19210b74d4d7SPetr Mladek struct printf_spec spec, const char *fmt) 1922edf14cdbSVlastimil Babka { 1923edf14cdbSVlastimil Babka unsigned long flags; 1924edf14cdbSVlastimil Babka const struct trace_print_flags *names; 1925edf14cdbSVlastimil Babka 19263e5903ebSPetr Mladek if (check_pointer(&buf, end, flags_ptr, spec)) 19273e5903ebSPetr Mladek return buf; 19283e5903ebSPetr Mladek 1929edf14cdbSVlastimil Babka switch (fmt[1]) { 1930edf14cdbSVlastimil Babka case 'p': 1931edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1932edf14cdbSVlastimil Babka /* Remove zone id */ 1933edf14cdbSVlastimil Babka flags &= (1UL << NR_PAGEFLAGS) - 1; 1934edf14cdbSVlastimil Babka names = pageflag_names; 1935edf14cdbSVlastimil Babka break; 1936edf14cdbSVlastimil Babka case 'v': 1937edf14cdbSVlastimil Babka flags = *(unsigned long *)flags_ptr; 1938edf14cdbSVlastimil Babka names = vmaflag_names; 1939edf14cdbSVlastimil Babka break; 1940edf14cdbSVlastimil Babka case 'g': 194130d497a0SAndy Shevchenko flags = (__force unsigned long)(*(gfp_t *)flags_ptr); 1942edf14cdbSVlastimil Babka names = gfpflag_names; 1943edf14cdbSVlastimil Babka break; 1944edf14cdbSVlastimil Babka default: 1945c8c3b584SPetr Mladek return error_string(buf, end, "(%pG?)", spec); 1946edf14cdbSVlastimil Babka } 1947edf14cdbSVlastimil Babka 1948edf14cdbSVlastimil Babka return format_flags(buf, end, flags, names); 1949edf14cdbSVlastimil Babka } 1950edf14cdbSVlastimil Babka 1951ce4fecf1SPantelis Antoniou static noinline_for_stack 1952a92eb762SSakari Ailus char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf, 1953a92eb762SSakari Ailus char *end) 1954ce4fecf1SPantelis Antoniou { 1955ce4fecf1SPantelis Antoniou int depth; 1956ce4fecf1SPantelis Antoniou 1957a92eb762SSakari Ailus /* Loop starting from the root node to the current node. */ 1958a92eb762SSakari Ailus for (depth = fwnode_count_parents(fwnode); depth >= 0; depth--) { 1959a92eb762SSakari Ailus struct fwnode_handle *__fwnode = 1960a92eb762SSakari Ailus fwnode_get_nth_parent(fwnode, depth); 1961ce4fecf1SPantelis Antoniou 1962a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name_prefix(__fwnode), 1963abd4fe62SAndy Shevchenko default_str_spec); 1964a92eb762SSakari Ailus buf = string(buf, end, fwnode_get_name(__fwnode), 1965a92eb762SSakari Ailus default_str_spec); 1966a92eb762SSakari Ailus 1967a92eb762SSakari Ailus fwnode_handle_put(__fwnode); 1968ce4fecf1SPantelis Antoniou } 1969a92eb762SSakari Ailus 1970ce4fecf1SPantelis Antoniou return buf; 1971ce4fecf1SPantelis Antoniou } 1972ce4fecf1SPantelis Antoniou 1973ce4fecf1SPantelis Antoniou static noinline_for_stack 1974ce4fecf1SPantelis Antoniou char *device_node_string(char *buf, char *end, struct device_node *dn, 1975ce4fecf1SPantelis Antoniou struct printf_spec spec, const char *fmt) 1976ce4fecf1SPantelis Antoniou { 1977ce4fecf1SPantelis Antoniou char tbuf[sizeof("xxxx") + 1]; 1978ce4fecf1SPantelis Antoniou const char *p; 1979ce4fecf1SPantelis Antoniou int ret; 1980ce4fecf1SPantelis Antoniou char *buf_start = buf; 1981ce4fecf1SPantelis Antoniou struct property *prop; 1982ce4fecf1SPantelis Antoniou bool has_mult, pass; 1983ce4fecf1SPantelis Antoniou 1984ce4fecf1SPantelis Antoniou struct printf_spec str_spec = spec; 1985ce4fecf1SPantelis Antoniou str_spec.field_width = -1; 1986ce4fecf1SPantelis Antoniou 198783abc5a7SSakari Ailus if (fmt[0] != 'F') 198883abc5a7SSakari Ailus return error_string(buf, end, "(%pO?)", spec); 198983abc5a7SSakari Ailus 1990ce4fecf1SPantelis Antoniou if (!IS_ENABLED(CONFIG_OF)) 1991c8c3b584SPetr Mladek return error_string(buf, end, "(%pOF?)", spec); 1992ce4fecf1SPantelis Antoniou 19933e5903ebSPetr Mladek if (check_pointer(&buf, end, dn, spec)) 19943e5903ebSPetr Mladek return buf; 1995ce4fecf1SPantelis Antoniou 1996ce4fecf1SPantelis Antoniou /* simple case without anything any more format specifiers */ 1997ce4fecf1SPantelis Antoniou fmt++; 1998ce4fecf1SPantelis Antoniou if (fmt[0] == '\0' || strcspn(fmt,"fnpPFcC") > 0) 1999ce4fecf1SPantelis Antoniou fmt = "f"; 2000ce4fecf1SPantelis Antoniou 2001ce4fecf1SPantelis Antoniou for (pass = false; strspn(fmt,"fnpPFcC"); fmt++, pass = true) { 20026d0a70a2SRob Herring int precision; 2003ce4fecf1SPantelis Antoniou if (pass) { 2004ce4fecf1SPantelis Antoniou if (buf < end) 2005ce4fecf1SPantelis Antoniou *buf = ':'; 2006ce4fecf1SPantelis Antoniou buf++; 2007ce4fecf1SPantelis Antoniou } 2008ce4fecf1SPantelis Antoniou 2009ce4fecf1SPantelis Antoniou switch (*fmt) { 2010ce4fecf1SPantelis Antoniou case 'f': /* full_name */ 2011a92eb762SSakari Ailus buf = fwnode_full_name_string(of_fwnode_handle(dn), buf, 2012a92eb762SSakari Ailus end); 2013ce4fecf1SPantelis Antoniou break; 2014ce4fecf1SPantelis Antoniou case 'n': /* name */ 2015a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn)); 20166d0a70a2SRob Herring precision = str_spec.precision; 20176d0a70a2SRob Herring str_spec.precision = strchrnul(p, '@') - p; 20186d0a70a2SRob Herring buf = string(buf, end, p, str_spec); 20196d0a70a2SRob Herring str_spec.precision = precision; 2020ce4fecf1SPantelis Antoniou break; 2021ce4fecf1SPantelis Antoniou case 'p': /* phandle */ 202209ceb8d7SAndy Shevchenko buf = number(buf, end, (unsigned int)dn->phandle, default_dec_spec); 2023ce4fecf1SPantelis Antoniou break; 2024ce4fecf1SPantelis Antoniou case 'P': /* path-spec */ 2025a92eb762SSakari Ailus p = fwnode_get_name(of_fwnode_handle(dn)); 2026ce4fecf1SPantelis Antoniou if (!p[1]) 2027ce4fecf1SPantelis Antoniou p = "/"; 2028ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2029ce4fecf1SPantelis Antoniou break; 2030ce4fecf1SPantelis Antoniou case 'F': /* flags */ 2031ce4fecf1SPantelis Antoniou tbuf[0] = of_node_check_flag(dn, OF_DYNAMIC) ? 'D' : '-'; 2032ce4fecf1SPantelis Antoniou tbuf[1] = of_node_check_flag(dn, OF_DETACHED) ? 'd' : '-'; 2033ce4fecf1SPantelis Antoniou tbuf[2] = of_node_check_flag(dn, OF_POPULATED) ? 'P' : '-'; 2034ce4fecf1SPantelis Antoniou tbuf[3] = of_node_check_flag(dn, OF_POPULATED_BUS) ? 'B' : '-'; 2035ce4fecf1SPantelis Antoniou tbuf[4] = 0; 2036d529ac41SPetr Mladek buf = string_nocheck(buf, end, tbuf, str_spec); 2037ce4fecf1SPantelis Antoniou break; 2038ce4fecf1SPantelis Antoniou case 'c': /* major compatible string */ 2039ce4fecf1SPantelis Antoniou ret = of_property_read_string(dn, "compatible", &p); 2040ce4fecf1SPantelis Antoniou if (!ret) 2041ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2042ce4fecf1SPantelis Antoniou break; 2043ce4fecf1SPantelis Antoniou case 'C': /* full compatible string */ 2044ce4fecf1SPantelis Antoniou has_mult = false; 2045ce4fecf1SPantelis Antoniou of_property_for_each_string(dn, "compatible", prop, p) { 2046ce4fecf1SPantelis Antoniou if (has_mult) 2047d529ac41SPetr Mladek buf = string_nocheck(buf, end, ",", str_spec); 2048d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 2049ce4fecf1SPantelis Antoniou buf = string(buf, end, p, str_spec); 2050d529ac41SPetr Mladek buf = string_nocheck(buf, end, "\"", str_spec); 2051ce4fecf1SPantelis Antoniou 2052ce4fecf1SPantelis Antoniou has_mult = true; 2053ce4fecf1SPantelis Antoniou } 2054ce4fecf1SPantelis Antoniou break; 2055ce4fecf1SPantelis Antoniou default: 2056ce4fecf1SPantelis Antoniou break; 2057ce4fecf1SPantelis Antoniou } 2058ce4fecf1SPantelis Antoniou } 2059ce4fecf1SPantelis Antoniou 2060ce4fecf1SPantelis Antoniou return widen_string(buf, buf - buf_start, end, spec); 2061ce4fecf1SPantelis Antoniou } 2062ce4fecf1SPantelis Antoniou 20633bd32d6aSSakari Ailus static noinline_for_stack 20643bd32d6aSSakari Ailus char *fwnode_string(char *buf, char *end, struct fwnode_handle *fwnode, 2065798cc27aSPetr Mladek struct printf_spec spec, const char *fmt) 2066798cc27aSPetr Mladek { 20673bd32d6aSSakari Ailus struct printf_spec str_spec = spec; 20683bd32d6aSSakari Ailus char *buf_start = buf; 20693bd32d6aSSakari Ailus 20703bd32d6aSSakari Ailus str_spec.field_width = -1; 20713bd32d6aSSakari Ailus 20723bd32d6aSSakari Ailus if (*fmt != 'w') 20733bd32d6aSSakari Ailus return error_string(buf, end, "(%pf?)", spec); 20743bd32d6aSSakari Ailus 20753bd32d6aSSakari Ailus if (check_pointer(&buf, end, fwnode, spec)) 20763bd32d6aSSakari Ailus return buf; 20773bd32d6aSSakari Ailus 20783bd32d6aSSakari Ailus fmt++; 20793bd32d6aSSakari Ailus 20803bd32d6aSSakari Ailus switch (*fmt) { 20813bd32d6aSSakari Ailus case 'P': /* name */ 20823bd32d6aSSakari Ailus buf = string(buf, end, fwnode_get_name(fwnode), str_spec); 20833bd32d6aSSakari Ailus break; 20843bd32d6aSSakari Ailus case 'f': /* full_name */ 20853bd32d6aSSakari Ailus default: 20863bd32d6aSSakari Ailus buf = fwnode_full_name_string(fwnode, buf, end); 20873bd32d6aSSakari Ailus break; 2088798cc27aSPetr Mladek } 2089798cc27aSPetr Mladek 20903bd32d6aSSakari Ailus return widen_string(buf, buf - buf_start, end, spec); 2091798cc27aSPetr Mladek } 2092798cc27aSPetr Mladek 2093*5ead723aSTimur Tabi /* Disable pointer hashing if requested */ 2094*5ead723aSTimur Tabi bool no_hash_pointers __ro_after_init; 2095*5ead723aSTimur Tabi EXPORT_SYMBOL_GPL(no_hash_pointers); 2096*5ead723aSTimur Tabi 2097*5ead723aSTimur Tabi static int __init no_hash_pointers_enable(char *str) 2098*5ead723aSTimur Tabi { 2099*5ead723aSTimur Tabi no_hash_pointers = true; 2100*5ead723aSTimur Tabi 2101*5ead723aSTimur Tabi pr_warn("**********************************************************\n"); 2102*5ead723aSTimur Tabi pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 2103*5ead723aSTimur Tabi pr_warn("** **\n"); 2104*5ead723aSTimur Tabi pr_warn("** This system shows unhashed kernel memory addresses **\n"); 2105*5ead723aSTimur Tabi pr_warn("** via the console, logs, and other interfaces. This **\n"); 2106*5ead723aSTimur Tabi pr_warn("** might reduce the security of your system. **\n"); 2107*5ead723aSTimur Tabi pr_warn("** **\n"); 2108*5ead723aSTimur Tabi pr_warn("** If you see this message and you are not debugging **\n"); 2109*5ead723aSTimur Tabi pr_warn("** the kernel, report this immediately to your system **\n"); 2110*5ead723aSTimur Tabi pr_warn("** administrator! **\n"); 2111*5ead723aSTimur Tabi pr_warn("** **\n"); 2112*5ead723aSTimur Tabi pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n"); 2113*5ead723aSTimur Tabi pr_warn("**********************************************************\n"); 2114*5ead723aSTimur Tabi 2115*5ead723aSTimur Tabi return 0; 2116*5ead723aSTimur Tabi } 2117*5ead723aSTimur Tabi early_param("no_hash_pointers", no_hash_pointers_enable); 2118*5ead723aSTimur Tabi 21194d8a743cSLinus Torvalds /* 21204d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 21214d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 21224d8a743cSLinus Torvalds * specifiers. 21234d8a743cSLinus Torvalds * 21240b523769SJoe Perches * Please update scripts/checkpatch.pl when adding/removing conversion 21250b523769SJoe Perches * characters. (Search for "check for vsprintf extension"). 21260b523769SJoe Perches * 2127332d2e78SLinus Torvalds * Right now we handle: 21280fe1ef24SLinus Torvalds * 2129cdb7e52dSSergey Senozhatsky * - 'S' For symbolic direct pointers (or function descriptors) with offset 2130cdb7e52dSSergey Senozhatsky * - 's' For symbolic direct pointers (or function descriptors) without offset 21319af77064SSakari Ailus * - '[Ss]R' as above with __builtin_extract_return_addr() translation 21321586c5aeSSakari Ailus * - '[Ff]' %pf and %pF were obsoleted and later removed in favor of 21331586c5aeSSakari Ailus * %ps and %pS. Be careful when re-using these specifiers. 21340f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 2135c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 2136c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 2137dbc760bcSTejun Heo * - 'b[l]' For a bitmap, the number of bits is determined by the field 2138dbc760bcSTejun Heo * width which must be explicitly specified either as part of the 2139dbc760bcSTejun Heo * format string '%32b[l]' or through '%*b[l]', [l] selects 2140dbc760bcSTejun Heo * range-list format instead of hex format 2141dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 2142dd45c9cfSHarvey Harrison * usual colon-separated hex notation 21438a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 2144bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 2145c8e00060SJoe Perches * with a dash-separated hex notation 21467c59154eSAndy Shevchenko * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth) 21478a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 21488a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 21498a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 215010679643SDaniel Borkmann * [S][pfs] 215110679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 215210679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 21538a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 21548a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 21558a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 215610679643SDaniel Borkmann * [S][pfs] 215710679643SDaniel Borkmann * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to 215810679643SDaniel Borkmann * [4] or [6] and is able to print port [p], flowinfo [f], scope [s] 215910679643SDaniel Borkmann * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order 216010679643SDaniel Borkmann * - 'I[6S]c' for IPv6 addresses printed as specified by 21618eda94bdSAlexander A. Klimov * https://tools.ietf.org/html/rfc5952 216271dca95dSAndy Shevchenko * - 'E[achnops]' For an escaped buffer, where rules are defined by combination 216371dca95dSAndy Shevchenko * of the following flags (see string_escape_mem() for the 216471dca95dSAndy Shevchenko * details): 216571dca95dSAndy Shevchenko * a - ESCAPE_ANY 216671dca95dSAndy Shevchenko * c - ESCAPE_SPECIAL 216771dca95dSAndy Shevchenko * h - ESCAPE_HEX 216871dca95dSAndy Shevchenko * n - ESCAPE_NULL 216971dca95dSAndy Shevchenko * o - ESCAPE_OCTAL 217071dca95dSAndy Shevchenko * p - ESCAPE_NP 217171dca95dSAndy Shevchenko * s - ESCAPE_SPACE 217271dca95dSAndy Shevchenko * By default ESCAPE_ANY_NP is used. 21739ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 21749ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 21759ac6e44eSJoe Perches * Options for %pU are: 21769ac6e44eSJoe Perches * b big endian lower case hex (default) 21779ac6e44eSJoe Perches * B big endian UPPER case hex 21789ac6e44eSJoe Perches * l little endian lower case hex 21799ac6e44eSJoe Perches * L little endian UPPER case hex 21809ac6e44eSJoe Perches * big endian output byte order is: 21819ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 21829ac6e44eSJoe Perches * little endian output byte order is: 21839ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 21847db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 21857db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 21867db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 21877db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 21887db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 2189455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 2190c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 219131550a16SAndy Shevchenko * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with 219231550a16SAndy Shevchenko * a certain separator (' ' by default): 219331550a16SAndy Shevchenko * C colon 219431550a16SAndy Shevchenko * D dash 219531550a16SAndy Shevchenko * N no separator 219631550a16SAndy Shevchenko * The maximum supported length is 64 bytes of the input. Consider 219731550a16SAndy Shevchenko * to use print_hex_dump() for the larger input. 2198aaf07621SJoe Perches * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives 2199aaf07621SJoe Perches * (default assumed to be phys_addr_t, passed by reference) 2200c0d92a57SOlof Johansson * - 'd[234]' For a dentry name (optionally 2-4 last components) 2201c0d92a57SOlof Johansson * - 'D[234]' Same as 'd' but for a struct file 22021031bc58SDmitry Monakhov * - 'g' For block_device name (gendisk + partition number) 22037daac5b2SAndy Shevchenko * - 't[RT][dt][r]' For time and date as represented by: 22044d42c447SAndy Shevchenko * R struct rtc_time 22057daac5b2SAndy Shevchenko * T time64_t 2206900cca29SGeert Uytterhoeven * - 'C' For a clock, it prints the name (Common Clock Framework) or address 2207900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2208900cca29SGeert Uytterhoeven * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 2209900cca29SGeert Uytterhoeven * (legacy clock framework) of the clock 2210edf14cdbSVlastimil Babka * - 'G' For flags to be printed as a collection of symbolic strings that would 2211edf14cdbSVlastimil Babka * construct the specific value. Supported flags given by option: 2212edf14cdbSVlastimil Babka * p page flags (see struct page) given as pointer to unsigned long 2213edf14cdbSVlastimil Babka * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t 2214edf14cdbSVlastimil Babka * v vma flags (VM_*) given as pointer to unsigned long 2215ce4fecf1SPantelis Antoniou * - 'OF[fnpPcCF]' For a device tree object 2216ce4fecf1SPantelis Antoniou * Without any optional arguments prints the full_name 2217ce4fecf1SPantelis Antoniou * f device node full_name 2218ce4fecf1SPantelis Antoniou * n device node name 2219ce4fecf1SPantelis Antoniou * p device node phandle 2220ce4fecf1SPantelis Antoniou * P device node path spec (name + @unit) 2221ce4fecf1SPantelis Antoniou * F device node flags 2222ce4fecf1SPantelis Antoniou * c major compatible string 2223ce4fecf1SPantelis Antoniou * C full compatible string 22243bd32d6aSSakari Ailus * - 'fw[fP]' For a firmware node (struct fwnode_handle) pointer 22253bd32d6aSSakari Ailus * Without an option prints the full name of the node 22263bd32d6aSSakari Ailus * f full name 22273bd32d6aSSakari Ailus * P node name, including a possible unit address 22287b1924a1STobin C. Harding * - 'x' For printing the address. Equivalent to "%lx". 2229b2a5212fSDaniel Borkmann * - '[ku]s' For a BPF/tracing related format specifier, e.g. used out of 2230b2a5212fSDaniel Borkmann * bpf_trace_printk() where [ku] prefix specifies either kernel (k) 2231b2a5212fSDaniel Borkmann * or user (u) memory to probe, and: 2232b2a5212fSDaniel Borkmann * s a string, equivalent to "%s" on direct vsnprintf() use 22337b1924a1STobin C. Harding * 2234b3ed2321STobin C. Harding * ** When making changes please also update: 2235b3ed2321STobin C. Harding * Documentation/core-api/printk-formats.rst 22369ac6e44eSJoe Perches * 2237ad67b74dSTobin C. Harding * Note: The default behaviour (unadorned %p) is to hash the address, 2238ad67b74dSTobin C. Harding * rendering it useful as a unique identifier. 22394d8a743cSLinus Torvalds */ 2240cf3b429bSJoe Perches static noinline_for_stack 2241cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 2242fef20d9cSFrederic Weisbecker struct printf_spec spec) 224378a8bf69SLinus Torvalds { 22440fe1ef24SLinus Torvalds switch (*fmt) { 22450fe1ef24SLinus Torvalds case 'S': 22469ac6e44eSJoe Perches case 's': 224704b8eb7aSSergey Senozhatsky ptr = dereference_symbol_descriptor(ptr); 22484c1ca831SNick Desaulniers fallthrough; 22490f77a8d3SNamhyung Kim case 'B': 2250b0d33c2bSJoe Perches return symbol_string(buf, end, ptr, spec, fmt); 2251332d2e78SLinus Torvalds case 'R': 2252c7dabef8SBjorn Helgaas case 'r': 2253fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 225431550a16SAndy Shevchenko case 'h': 225531550a16SAndy Shevchenko return hex_string(buf, end, ptr, spec, fmt); 2256dbc760bcSTejun Heo case 'b': 2257dbc760bcSTejun Heo switch (fmt[1]) { 2258dbc760bcSTejun Heo case 'l': 2259dbc760bcSTejun Heo return bitmap_list_string(buf, end, ptr, spec, fmt); 2260dbc760bcSTejun Heo default: 2261dbc760bcSTejun Heo return bitmap_string(buf, end, ptr, spec, fmt); 2262dbc760bcSTejun Heo } 22638a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 22648a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 226576597ff9SAndrei Emeltchenko /* [mM]F (FDDI) */ 226676597ff9SAndrei Emeltchenko /* [mM]R (Reverse order; Bluetooth) */ 22678a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 22688a27f7c9SJoe Perches case 'I': /* Formatted IP supported 22698a27f7c9SJoe Perches * 4: 1.2.3.4 22708a27f7c9SJoe Perches * 6: 0001:0203:...:0708 22718a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 22728a27f7c9SJoe Perches */ 22738a27f7c9SJoe Perches case 'i': /* Contiguous: 22748a27f7c9SJoe Perches * 4: 001.002.003.004 22758a27f7c9SJoe Perches * 6: 000102...0f 22768a27f7c9SJoe Perches */ 2277f00cc102SPetr Mladek return ip_addr_string(buf, end, ptr, spec, fmt); 227871dca95dSAndy Shevchenko case 'E': 227971dca95dSAndy Shevchenko return escaped_string(buf, end, ptr, spec, fmt); 22809ac6e44eSJoe Perches case 'U': 22819ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 22827db6f5fbSJoe Perches case 'V': 22833e5903ebSPetr Mladek return va_format(buf, end, ptr, spec, fmt); 2284455cd5abSDan Rosenberg case 'K': 228557e73442STobin C. Harding return restricted_pointer(buf, end, ptr, spec); 2286c8f44affSMichał Mirosław case 'N': 2287431bca24SGeert Uytterhoeven return netdev_bits(buf, end, ptr, spec, fmt); 22887d799210SStepan Moskovchenko case 'a': 22893e5903ebSPetr Mladek return address_val(buf, end, ptr, spec, fmt); 22904b6ccca7SAl Viro case 'd': 22914b6ccca7SAl Viro return dentry_name(buf, end, ptr, spec, fmt); 22924d42c447SAndy Shevchenko case 't': 22934d42c447SAndy Shevchenko return time_and_date(buf, end, ptr, spec, fmt); 2294900cca29SGeert Uytterhoeven case 'C': 2295900cca29SGeert Uytterhoeven return clock(buf, end, ptr, spec, fmt); 22964b6ccca7SAl Viro case 'D': 229736594b31SJia He return file_dentry_name(buf, end, ptr, spec, fmt); 22981031bc58SDmitry Monakhov #ifdef CONFIG_BLOCK 22991031bc58SDmitry Monakhov case 'g': 23001031bc58SDmitry Monakhov return bdev_name(buf, end, ptr, spec, fmt); 23011031bc58SDmitry Monakhov #endif 23021031bc58SDmitry Monakhov 2303edf14cdbSVlastimil Babka case 'G': 23040b74d4d7SPetr Mladek return flags_string(buf, end, ptr, spec, fmt); 2305ce4fecf1SPantelis Antoniou case 'O': 230683abc5a7SSakari Ailus return device_node_string(buf, end, ptr, spec, fmt + 1); 23073bd32d6aSSakari Ailus case 'f': 23083bd32d6aSSakari Ailus return fwnode_string(buf, end, ptr, spec, fmt + 1); 23097b1924a1STobin C. Harding case 'x': 23107b1924a1STobin C. Harding return pointer_string(buf, end, ptr, spec); 231157f5677eSRasmus Villemoes case 'e': 231257f5677eSRasmus Villemoes /* %pe with a non-ERR_PTR gets treated as plain %p */ 231357f5677eSRasmus Villemoes if (!IS_ERR(ptr)) 231457f5677eSRasmus Villemoes break; 231557f5677eSRasmus Villemoes return err_ptr(buf, end, ptr, spec); 2316b2a5212fSDaniel Borkmann case 'u': 2317b2a5212fSDaniel Borkmann case 'k': 2318b2a5212fSDaniel Borkmann switch (fmt[1]) { 2319b2a5212fSDaniel Borkmann case 's': 2320b2a5212fSDaniel Borkmann return string(buf, end, ptr, spec); 2321b2a5212fSDaniel Borkmann default: 2322b2a5212fSDaniel Borkmann return error_string(buf, end, "(einval)", spec); 2323b2a5212fSDaniel Borkmann } 23240fe1ef24SLinus Torvalds } 2325fef20d9cSFrederic Weisbecker 2326*5ead723aSTimur Tabi /* 2327*5ead723aSTimur Tabi * default is to _not_ leak addresses, so hash before printing, 2328*5ead723aSTimur Tabi * unless no_hash_pointers is specified on the command line. 2329*5ead723aSTimur Tabi */ 2330*5ead723aSTimur Tabi if (unlikely(no_hash_pointers)) 2331*5ead723aSTimur Tabi return pointer_string(buf, end, ptr, spec); 2332*5ead723aSTimur Tabi else 2333ad67b74dSTobin C. Harding return ptr_to_id(buf, end, ptr, spec); 2334fef20d9cSFrederic Weisbecker } 2335fef20d9cSFrederic Weisbecker 2336fef20d9cSFrederic Weisbecker /* 2337fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 2338fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 2339fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 2340fef20d9cSFrederic Weisbecker * to go on the next call). 2341fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 2342fef20d9cSFrederic Weisbecker * 2343fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 2344fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 2345fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 23465b5e0928SAlexey Dobriyan * 'Z' changed to 'z' --adobriyan 2017-01-25 2347fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 2348fef20d9cSFrederic Weisbecker * 2349fef20d9cSFrederic Weisbecker * @fmt: the format string 2350fef20d9cSFrederic Weisbecker * @type of the token returned 2351fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 2352fef20d9cSFrederic Weisbecker * @field_width: overwritten width 2353fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 2354fef20d9cSFrederic Weisbecker * @precision: precision of a number 2355fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 2356fef20d9cSFrederic Weisbecker */ 2357cf3b429bSJoe Perches static noinline_for_stack 2358cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 2359fef20d9cSFrederic Weisbecker { 2360fef20d9cSFrederic Weisbecker const char *start = fmt; 2361d0484193SRasmus Villemoes char qualifier; 2362fef20d9cSFrederic Weisbecker 2363fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 2364ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 2365fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 2366fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 2367fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 2368fef20d9cSFrederic Weisbecker } 2369fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2370fef20d9cSFrederic Weisbecker goto precision; 2371fef20d9cSFrederic Weisbecker } 2372fef20d9cSFrederic Weisbecker 2373fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 2374fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 2375fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2376fef20d9cSFrederic Weisbecker spec->precision = 0; 2377fef20d9cSFrederic Weisbecker 2378fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2379fef20d9cSFrederic Weisbecker goto qualifier; 2380fef20d9cSFrederic Weisbecker } 2381fef20d9cSFrederic Weisbecker 2382fef20d9cSFrederic Weisbecker /* By default */ 2383fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 2384fef20d9cSFrederic Weisbecker 2385fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 2386fef20d9cSFrederic Weisbecker if (*fmt == '%') 2387fef20d9cSFrederic Weisbecker break; 2388fef20d9cSFrederic Weisbecker } 2389fef20d9cSFrederic Weisbecker 2390fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 2391fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 2392fef20d9cSFrederic Weisbecker return fmt - start; 2393fef20d9cSFrederic Weisbecker 2394fef20d9cSFrederic Weisbecker /* Process flags */ 2395fef20d9cSFrederic Weisbecker spec->flags = 0; 2396fef20d9cSFrederic Weisbecker 2397fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 2398fef20d9cSFrederic Weisbecker bool found = true; 2399fef20d9cSFrederic Weisbecker 2400fef20d9cSFrederic Weisbecker ++fmt; 2401fef20d9cSFrederic Weisbecker 2402fef20d9cSFrederic Weisbecker switch (*fmt) { 2403fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 2404fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 2405fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 2406fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 2407fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 2408fef20d9cSFrederic Weisbecker default: found = false; 2409fef20d9cSFrederic Weisbecker } 2410fef20d9cSFrederic Weisbecker 2411fef20d9cSFrederic Weisbecker if (!found) 2412fef20d9cSFrederic Weisbecker break; 2413fef20d9cSFrederic Weisbecker } 2414fef20d9cSFrederic Weisbecker 2415fef20d9cSFrederic Weisbecker /* get field width */ 2416fef20d9cSFrederic Weisbecker spec->field_width = -1; 2417fef20d9cSFrederic Weisbecker 2418fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 2419fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 2420fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 2421fef20d9cSFrederic Weisbecker /* it's the next argument */ 2422ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 2423fef20d9cSFrederic Weisbecker return ++fmt - start; 2424fef20d9cSFrederic Weisbecker } 2425fef20d9cSFrederic Weisbecker 2426fef20d9cSFrederic Weisbecker precision: 2427fef20d9cSFrederic Weisbecker /* get the precision */ 2428fef20d9cSFrederic Weisbecker spec->precision = -1; 2429fef20d9cSFrederic Weisbecker if (*fmt == '.') { 2430fef20d9cSFrederic Weisbecker ++fmt; 2431fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 2432fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 2433fef20d9cSFrederic Weisbecker if (spec->precision < 0) 2434fef20d9cSFrederic Weisbecker spec->precision = 0; 2435fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 2436fef20d9cSFrederic Weisbecker /* it's the next argument */ 2437adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 2438fef20d9cSFrederic Weisbecker return ++fmt - start; 2439fef20d9cSFrederic Weisbecker } 2440fef20d9cSFrederic Weisbecker } 2441fef20d9cSFrederic Weisbecker 2442fef20d9cSFrederic Weisbecker qualifier: 2443fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 2444d0484193SRasmus Villemoes qualifier = 0; 244575fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 24465b5e0928SAlexey Dobriyan *fmt == 'z' || *fmt == 't') { 2447d0484193SRasmus Villemoes qualifier = *fmt++; 2448d0484193SRasmus Villemoes if (unlikely(qualifier == *fmt)) { 2449d0484193SRasmus Villemoes if (qualifier == 'l') { 2450d0484193SRasmus Villemoes qualifier = 'L'; 2451fef20d9cSFrederic Weisbecker ++fmt; 2452d0484193SRasmus Villemoes } else if (qualifier == 'h') { 2453d0484193SRasmus Villemoes qualifier = 'H'; 2454a4e94ef0SZhaolei ++fmt; 2455a4e94ef0SZhaolei } 2456fef20d9cSFrederic Weisbecker } 2457fef20d9cSFrederic Weisbecker } 2458fef20d9cSFrederic Weisbecker 2459fef20d9cSFrederic Weisbecker /* default base */ 2460fef20d9cSFrederic Weisbecker spec->base = 10; 2461fef20d9cSFrederic Weisbecker switch (*fmt) { 2462fef20d9cSFrederic Weisbecker case 'c': 2463fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 2464fef20d9cSFrederic Weisbecker return ++fmt - start; 2465fef20d9cSFrederic Weisbecker 2466fef20d9cSFrederic Weisbecker case 's': 2467fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 2468fef20d9cSFrederic Weisbecker return ++fmt - start; 2469fef20d9cSFrederic Weisbecker 2470fef20d9cSFrederic Weisbecker case 'p': 2471fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 2472ffbfed03SRasmus Villemoes return ++fmt - start; 2473fef20d9cSFrederic Weisbecker 2474fef20d9cSFrederic Weisbecker case '%': 2475fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 2476fef20d9cSFrederic Weisbecker return ++fmt - start; 2477fef20d9cSFrederic Weisbecker 2478fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 2479fef20d9cSFrederic Weisbecker case 'o': 2480fef20d9cSFrederic Weisbecker spec->base = 8; 2481fef20d9cSFrederic Weisbecker break; 2482fef20d9cSFrederic Weisbecker 2483fef20d9cSFrederic Weisbecker case 'x': 2484fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 24854c1ca831SNick Desaulniers fallthrough; 2486fef20d9cSFrederic Weisbecker 2487fef20d9cSFrederic Weisbecker case 'X': 2488fef20d9cSFrederic Weisbecker spec->base = 16; 2489fef20d9cSFrederic Weisbecker break; 2490fef20d9cSFrederic Weisbecker 2491fef20d9cSFrederic Weisbecker case 'd': 2492fef20d9cSFrederic Weisbecker case 'i': 249339e874f8SFrederic Weisbecker spec->flags |= SIGN; 249436f9ff9eSGustavo A. R. Silva break; 2495fef20d9cSFrederic Weisbecker case 'u': 2496fef20d9cSFrederic Weisbecker break; 2497fef20d9cSFrederic Weisbecker 2498708d96fdSRyan Mallon case 'n': 2499708d96fdSRyan Mallon /* 2500b006f19bSRasmus Villemoes * Since %n poses a greater security risk than 2501b006f19bSRasmus Villemoes * utility, treat it as any other invalid or 2502b006f19bSRasmus Villemoes * unsupported format specifier. 2503708d96fdSRyan Mallon */ 25044c1ca831SNick Desaulniers fallthrough; 2505708d96fdSRyan Mallon 2506fef20d9cSFrederic Weisbecker default: 2507b006f19bSRasmus Villemoes WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt); 2508fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 2509fef20d9cSFrederic Weisbecker return fmt - start; 2510fef20d9cSFrederic Weisbecker } 2511fef20d9cSFrederic Weisbecker 2512d0484193SRasmus Villemoes if (qualifier == 'L') 2513fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 2514d0484193SRasmus Villemoes else if (qualifier == 'l') { 251551be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 251651be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 25175b5e0928SAlexey Dobriyan } else if (qualifier == 'z') { 2518fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 2519d0484193SRasmus Villemoes } else if (qualifier == 't') { 2520fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 2521d0484193SRasmus Villemoes } else if (qualifier == 'H') { 252251be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 252351be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 2524d0484193SRasmus Villemoes } else if (qualifier == 'h') { 252551be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 252651be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 2527fef20d9cSFrederic Weisbecker } else { 252851be17dfSRasmus Villemoes BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT); 252951be17dfSRasmus Villemoes spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN); 2530fef20d9cSFrederic Weisbecker } 2531fef20d9cSFrederic Weisbecker 2532fef20d9cSFrederic Weisbecker return ++fmt - start; 253378a8bf69SLinus Torvalds } 253478a8bf69SLinus Torvalds 25354d72ba01SRasmus Villemoes static void 25364d72ba01SRasmus Villemoes set_field_width(struct printf_spec *spec, int width) 25374d72ba01SRasmus Villemoes { 25384d72ba01SRasmus Villemoes spec->field_width = width; 25394d72ba01SRasmus Villemoes if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) { 25404d72ba01SRasmus Villemoes spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX); 25414d72ba01SRasmus Villemoes } 25424d72ba01SRasmus Villemoes } 25434d72ba01SRasmus Villemoes 25444d72ba01SRasmus Villemoes static void 25454d72ba01SRasmus Villemoes set_precision(struct printf_spec *spec, int prec) 25464d72ba01SRasmus Villemoes { 25474d72ba01SRasmus Villemoes spec->precision = prec; 25484d72ba01SRasmus Villemoes if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) { 25494d72ba01SRasmus Villemoes spec->precision = clamp(prec, 0, PRECISION_MAX); 25504d72ba01SRasmus Villemoes } 25514d72ba01SRasmus Villemoes } 25524d72ba01SRasmus Villemoes 25531da177e4SLinus Torvalds /** 25541da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 25551da177e4SLinus Torvalds * @buf: The buffer to place the result into 25561da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 25571da177e4SLinus Torvalds * @fmt: The format string to use 25581da177e4SLinus Torvalds * @args: Arguments for the format string 25591da177e4SLinus Torvalds * 2560d7ec9a05SRasmus Villemoes * This function generally follows C99 vsnprintf, but has some 2561d7ec9a05SRasmus Villemoes * extensions and a few limitations: 2562d7ec9a05SRasmus Villemoes * 25636cc89134Smchehab@s-opensource.com * - ``%n`` is unsupported 25646cc89134Smchehab@s-opensource.com * - ``%p*`` is handled by pointer() 256520036fdcSAndi Kleen * 256627e7c0e8SJonathan Corbet * See pointer() or Documentation/core-api/printk-formats.rst for more 25675e4ee7b1SMartin Kletzander * extensive description. 25685e4ee7b1SMartin Kletzander * 25695e4ee7b1SMartin Kletzander * **Please update the documentation in both places when making changes** 257080f548e0SAndrew Morton * 25711da177e4SLinus Torvalds * The return value is the number of characters which would 25721da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 25731da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 25741da177e4SLinus Torvalds * number of characters written into @buf as return value 257572fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 25761da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 25771da177e4SLinus Torvalds * string is truncated. 25781da177e4SLinus Torvalds * 2579ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 25801da177e4SLinus Torvalds */ 25811da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 25821da177e4SLinus Torvalds { 25831da177e4SLinus Torvalds unsigned long long num; 2584d4be151bSAndré Goddard Rosa char *str, *end; 2585fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 25861da177e4SLinus Torvalds 2587f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 2588f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 25892aa2f9e2SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 25901da177e4SLinus Torvalds return 0; 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds str = buf; 2593f796937aSJeremy Fitzhardinge end = buf + size; 25941da177e4SLinus Torvalds 2595f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 2596f796937aSJeremy Fitzhardinge if (end < buf) { 25971da177e4SLinus Torvalds end = ((void *)-1); 2598f796937aSJeremy Fitzhardinge size = end - buf; 25991da177e4SLinus Torvalds } 26001da177e4SLinus Torvalds 2601fef20d9cSFrederic Weisbecker while (*fmt) { 2602fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 2603d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 2604fef20d9cSFrederic Weisbecker 2605fef20d9cSFrederic Weisbecker fmt += read; 2606fef20d9cSFrederic Weisbecker 2607fef20d9cSFrederic Weisbecker switch (spec.type) { 2608fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 2609fef20d9cSFrederic Weisbecker int copy = read; 2610fef20d9cSFrederic Weisbecker if (str < end) { 2611fef20d9cSFrederic Weisbecker if (copy > end - str) 2612fef20d9cSFrederic Weisbecker copy = end - str; 2613fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 2614fef20d9cSFrederic Weisbecker } 2615fef20d9cSFrederic Weisbecker str += read; 2616fef20d9cSFrederic Weisbecker break; 26171da177e4SLinus Torvalds } 26181da177e4SLinus Torvalds 2619ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 26204d72ba01SRasmus Villemoes set_field_width(&spec, va_arg(args, int)); 2621fef20d9cSFrederic Weisbecker break; 26221da177e4SLinus Torvalds 2623fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 26244d72ba01SRasmus Villemoes set_precision(&spec, va_arg(args, int)); 2625fef20d9cSFrederic Weisbecker break; 26261da177e4SLinus Torvalds 2627d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 2628d4be151bSAndré Goddard Rosa char c; 2629d4be151bSAndré Goddard Rosa 2630fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 2631fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2632f796937aSJeremy Fitzhardinge if (str < end) 26331da177e4SLinus Torvalds *str = ' '; 26341da177e4SLinus Torvalds ++str; 2635fef20d9cSFrederic Weisbecker 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds } 26381da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 2639f796937aSJeremy Fitzhardinge if (str < end) 26401da177e4SLinus Torvalds *str = c; 26411da177e4SLinus Torvalds ++str; 2642fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 2643f796937aSJeremy Fitzhardinge if (str < end) 26441da177e4SLinus Torvalds *str = ' '; 26451da177e4SLinus Torvalds ++str; 26461da177e4SLinus Torvalds } 2647fef20d9cSFrederic Weisbecker break; 2648d4be151bSAndré Goddard Rosa } 26491da177e4SLinus Torvalds 2650fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 2651fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 2652fef20d9cSFrederic Weisbecker break; 26531da177e4SLinus Torvalds 2654fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2655ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, va_arg(args, void *), 2656fef20d9cSFrederic Weisbecker spec); 2657fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 26584d8a743cSLinus Torvalds fmt++; 2659fef20d9cSFrederic Weisbecker break; 26601da177e4SLinus Torvalds 2661fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 2662f796937aSJeremy Fitzhardinge if (str < end) 26631da177e4SLinus Torvalds *str = '%'; 26641da177e4SLinus Torvalds ++str; 26651da177e4SLinus Torvalds break; 26661da177e4SLinus Torvalds 2667fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 2668b006f19bSRasmus Villemoes /* 2669b006f19bSRasmus Villemoes * Presumably the arguments passed gcc's type 2670b006f19bSRasmus Villemoes * checking, but there is no safe or sane way 2671b006f19bSRasmus Villemoes * for us to continue parsing the format and 2672b006f19bSRasmus Villemoes * fetching from the va_list; the remaining 2673b006f19bSRasmus Villemoes * specifiers and arguments would be out of 2674b006f19bSRasmus Villemoes * sync. 2675b006f19bSRasmus Villemoes */ 2676b006f19bSRasmus Villemoes goto out; 2677fef20d9cSFrederic Weisbecker 2678fef20d9cSFrederic Weisbecker default: 2679fef20d9cSFrederic Weisbecker switch (spec.type) { 2680fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2681fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 2682fef20d9cSFrederic Weisbecker break; 2683fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2684fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 2685fef20d9cSFrederic Weisbecker break; 2686fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2687fef20d9cSFrederic Weisbecker num = va_arg(args, long); 2688fef20d9cSFrederic Weisbecker break; 2689fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2690ef124960SJason Gunthorpe if (spec.flags & SIGN) 2691ef124960SJason Gunthorpe num = va_arg(args, ssize_t); 2692ef124960SJason Gunthorpe else 2693fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 2694fef20d9cSFrederic Weisbecker break; 2695fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2696fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 2697fef20d9cSFrederic Weisbecker break; 2698a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 2699a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 2700a4e94ef0SZhaolei break; 2701a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 2702a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 2703a4e94ef0SZhaolei break; 2704fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 2705fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 2706fef20d9cSFrederic Weisbecker break; 2707fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 2708fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 2709fef20d9cSFrederic Weisbecker break; 271039e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 271139e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 2712fef20d9cSFrederic Weisbecker break; 2713fef20d9cSFrederic Weisbecker default: 2714fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 27151da177e4SLinus Torvalds } 2716fef20d9cSFrederic Weisbecker 2717fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 27181da177e4SLinus Torvalds } 2719fef20d9cSFrederic Weisbecker } 2720fef20d9cSFrederic Weisbecker 2721b006f19bSRasmus Villemoes out: 2722f796937aSJeremy Fitzhardinge if (size > 0) { 2723f796937aSJeremy Fitzhardinge if (str < end) 27241da177e4SLinus Torvalds *str = '\0'; 2725f796937aSJeremy Fitzhardinge else 27260a6047eeSLinus Torvalds end[-1] = '\0'; 2727f796937aSJeremy Fitzhardinge } 2728fef20d9cSFrederic Weisbecker 2729f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 27301da177e4SLinus Torvalds return str-buf; 2731fef20d9cSFrederic Weisbecker 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 27341da177e4SLinus Torvalds 27351da177e4SLinus Torvalds /** 27361da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 27371da177e4SLinus Torvalds * @buf: The buffer to place the result into 27381da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 27391da177e4SLinus Torvalds * @fmt: The format string to use 27401da177e4SLinus Torvalds * @args: Arguments for the format string 27411da177e4SLinus Torvalds * 27421da177e4SLinus Torvalds * The return value is the number of characters which have been written into 2743b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 27441da177e4SLinus Torvalds * returns 0. 27451da177e4SLinus Torvalds * 2746ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 274720036fdcSAndi Kleen * 274820036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 27491da177e4SLinus Torvalds */ 27501da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 27511da177e4SLinus Torvalds { 27521da177e4SLinus Torvalds int i; 27531da177e4SLinus Torvalds 27541da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 27557b9186f5SAndré Goddard Rosa 2756b921c69fSAnton Arapov if (likely(i < size)) 2757b921c69fSAnton Arapov return i; 2758b921c69fSAnton Arapov if (size != 0) 2759b921c69fSAnton Arapov return size - 1; 2760b921c69fSAnton Arapov return 0; 27611da177e4SLinus Torvalds } 27621da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 27631da177e4SLinus Torvalds 27641da177e4SLinus Torvalds /** 27651da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 27661da177e4SLinus Torvalds * @buf: The buffer to place the result into 27671da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 27681da177e4SLinus Torvalds * @fmt: The format string to use 27691da177e4SLinus Torvalds * @...: Arguments for the format string 27701da177e4SLinus Torvalds * 27711da177e4SLinus Torvalds * The return value is the number of characters which would be 27721da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 27731da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 27741da177e4SLinus Torvalds * @size, the resulting string is truncated. 277520036fdcSAndi Kleen * 277620036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 27771da177e4SLinus Torvalds */ 27781da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 27791da177e4SLinus Torvalds { 27801da177e4SLinus Torvalds va_list args; 27811da177e4SLinus Torvalds int i; 27821da177e4SLinus Torvalds 27831da177e4SLinus Torvalds va_start(args, fmt); 27841da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 27851da177e4SLinus Torvalds va_end(args); 27867b9186f5SAndré Goddard Rosa 27871da177e4SLinus Torvalds return i; 27881da177e4SLinus Torvalds } 27891da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds /** 27921da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 27931da177e4SLinus Torvalds * @buf: The buffer to place the result into 27941da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 27951da177e4SLinus Torvalds * @fmt: The format string to use 27961da177e4SLinus Torvalds * @...: Arguments for the format string 27971da177e4SLinus Torvalds * 27981da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 2799b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 28001da177e4SLinus Torvalds */ 28011da177e4SLinus Torvalds 28021da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 28031da177e4SLinus Torvalds { 28041da177e4SLinus Torvalds va_list args; 28051da177e4SLinus Torvalds int i; 28061da177e4SLinus Torvalds 28071da177e4SLinus Torvalds va_start(args, fmt); 2808b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 28091da177e4SLinus Torvalds va_end(args); 28107b9186f5SAndré Goddard Rosa 2811b903c0b8SChangli Gao return i; 28121da177e4SLinus Torvalds } 28131da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds /** 28161da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 28171da177e4SLinus Torvalds * @buf: The buffer to place the result into 28181da177e4SLinus Torvalds * @fmt: The format string to use 28191da177e4SLinus Torvalds * @args: Arguments for the format string 28201da177e4SLinus Torvalds * 28211da177e4SLinus Torvalds * The function returns the number of characters written 282272fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 28231da177e4SLinus Torvalds * buffer overflows. 28241da177e4SLinus Torvalds * 2825ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 282620036fdcSAndi Kleen * 282720036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 28281da177e4SLinus Torvalds */ 28291da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 28301da177e4SLinus Torvalds { 28311da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 28321da177e4SLinus Torvalds } 28331da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds /** 28361da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 28371da177e4SLinus Torvalds * @buf: The buffer to place the result into 28381da177e4SLinus Torvalds * @fmt: The format string to use 28391da177e4SLinus Torvalds * @...: Arguments for the format string 28401da177e4SLinus Torvalds * 28411da177e4SLinus Torvalds * The function returns the number of characters written 284272fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 28431da177e4SLinus Torvalds * buffer overflows. 284420036fdcSAndi Kleen * 284520036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 28461da177e4SLinus Torvalds */ 28471da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 28481da177e4SLinus Torvalds { 28491da177e4SLinus Torvalds va_list args; 28501da177e4SLinus Torvalds int i; 28511da177e4SLinus Torvalds 28521da177e4SLinus Torvalds va_start(args, fmt); 28531da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 28541da177e4SLinus Torvalds va_end(args); 28557b9186f5SAndré Goddard Rosa 28561da177e4SLinus Torvalds return i; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 28591da177e4SLinus Torvalds 28604370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 28614370aa4aSLai Jiangshan /* 28624370aa4aSLai Jiangshan * bprintf service: 28634370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 28644370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 28654370aa4aSLai Jiangshan */ 28664370aa4aSLai Jiangshan 28674370aa4aSLai Jiangshan /** 28684370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 28694370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 28704370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 28714370aa4aSLai Jiangshan * @fmt: The format string to use 28724370aa4aSLai Jiangshan * @args: Arguments for the format string 28734370aa4aSLai Jiangshan * 28744370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 2875da3dae54SMasanari Iida * is skipped. 28764370aa4aSLai Jiangshan * 28774370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 28784370aa4aSLai Jiangshan * the given input. 28794370aa4aSLai Jiangshan * 28804370aa4aSLai Jiangshan * NOTE: 28814370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 28824370aa4aSLai Jiangshan * valid for bstr_printf(). 28834370aa4aSLai Jiangshan */ 28844370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 28854370aa4aSLai Jiangshan { 2886fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 28874370aa4aSLai Jiangshan char *str, *end; 2888841a915dSSteven Rostedt (VMware) int width; 28894370aa4aSLai Jiangshan 28904370aa4aSLai Jiangshan str = (char *)bin_buf; 28914370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 28924370aa4aSLai Jiangshan 28934370aa4aSLai Jiangshan #define save_arg(type) \ 2894841a915dSSteven Rostedt (VMware) ({ \ 28954370aa4aSLai Jiangshan unsigned long long value; \ 2896841a915dSSteven Rostedt (VMware) if (sizeof(type) == 8) { \ 2897841a915dSSteven Rostedt (VMware) unsigned long long val8; \ 28984370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 2899841a915dSSteven Rostedt (VMware) val8 = va_arg(args, unsigned long long); \ 29004370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 2901841a915dSSteven Rostedt (VMware) *(u32 *)str = *(u32 *)&val8; \ 2902841a915dSSteven Rostedt (VMware) *(u32 *)(str + 4) = *((u32 *)&val8 + 1); \ 29034370aa4aSLai Jiangshan } \ 2904841a915dSSteven Rostedt (VMware) value = val8; \ 29054370aa4aSLai Jiangshan } else { \ 2906841a915dSSteven Rostedt (VMware) unsigned int val4; \ 29074370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 2908841a915dSSteven Rostedt (VMware) val4 = va_arg(args, int); \ 29094370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 2910841a915dSSteven Rostedt (VMware) *(typeof(type) *)str = (type)(long)val4; \ 2911841a915dSSteven Rostedt (VMware) value = (unsigned long long)val4; \ 29124370aa4aSLai Jiangshan } \ 29134370aa4aSLai Jiangshan str += sizeof(type); \ 2914841a915dSSteven Rostedt (VMware) value; \ 2915841a915dSSteven Rostedt (VMware) }) 29164370aa4aSLai Jiangshan 2917fef20d9cSFrederic Weisbecker while (*fmt) { 2918d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 29194370aa4aSLai Jiangshan 2920fef20d9cSFrederic Weisbecker fmt += read; 2921fef20d9cSFrederic Weisbecker 2922fef20d9cSFrederic Weisbecker switch (spec.type) { 2923fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 2924d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 2925fef20d9cSFrederic Weisbecker break; 2926b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 2927b006f19bSRasmus Villemoes goto out; 2928fef20d9cSFrederic Weisbecker 2929ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 2930fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 2931841a915dSSteven Rostedt (VMware) width = (int)save_arg(int); 2932841a915dSSteven Rostedt (VMware) /* Pointers may require the width */ 2933841a915dSSteven Rostedt (VMware) if (*fmt == 'p') 2934841a915dSSteven Rostedt (VMware) set_field_width(&spec, width); 2935fef20d9cSFrederic Weisbecker break; 29364370aa4aSLai Jiangshan 2937fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 29384370aa4aSLai Jiangshan save_arg(char); 2939fef20d9cSFrederic Weisbecker break; 2940fef20d9cSFrederic Weisbecker 2941fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 29424370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 29433e5903ebSPetr Mladek const char *err_msg; 29444370aa4aSLai Jiangshan size_t len; 29456c356634SAndré Goddard Rosa 29463e5903ebSPetr Mladek err_msg = check_pointer_msg(save_str); 29473e5903ebSPetr Mladek if (err_msg) 29483e5903ebSPetr Mladek save_str = err_msg; 29493e5903ebSPetr Mladek 29506c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 29516c356634SAndré Goddard Rosa if (str + len < end) 29526c356634SAndré Goddard Rosa memcpy(str, save_str, len); 29536c356634SAndré Goddard Rosa str += len; 2954fef20d9cSFrederic Weisbecker break; 29554370aa4aSLai Jiangshan } 2956fef20d9cSFrederic Weisbecker 2957fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 2958841a915dSSteven Rostedt (VMware) /* Dereferenced pointers must be done now */ 2959841a915dSSteven Rostedt (VMware) switch (*fmt) { 2960841a915dSSteven Rostedt (VMware) /* Dereference of functions is still OK */ 2961841a915dSSteven Rostedt (VMware) case 'S': 2962841a915dSSteven Rostedt (VMware) case 's': 29631e6338cfSSteven Rostedt (VMware) case 'x': 29641e6338cfSSteven Rostedt (VMware) case 'K': 296557f5677eSRasmus Villemoes case 'e': 29664370aa4aSLai Jiangshan save_arg(void *); 2967841a915dSSteven Rostedt (VMware) break; 2968841a915dSSteven Rostedt (VMware) default: 2969841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 2970841a915dSSteven Rostedt (VMware) save_arg(void *); 2971841a915dSSteven Rostedt (VMware) break; 2972841a915dSSteven Rostedt (VMware) } 2973841a915dSSteven Rostedt (VMware) str = pointer(fmt, str, end, va_arg(args, void *), 2974841a915dSSteven Rostedt (VMware) spec); 2975841a915dSSteven Rostedt (VMware) if (str + 1 < end) 2976841a915dSSteven Rostedt (VMware) *str++ = '\0'; 2977841a915dSSteven Rostedt (VMware) else 2978841a915dSSteven Rostedt (VMware) end[-1] = '\0'; /* Must be nul terminated */ 2979841a915dSSteven Rostedt (VMware) } 29804370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 2981fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 29824370aa4aSLai Jiangshan fmt++; 2983fef20d9cSFrederic Weisbecker break; 2984fef20d9cSFrederic Weisbecker 2985fef20d9cSFrederic Weisbecker default: 2986fef20d9cSFrederic Weisbecker switch (spec.type) { 2987fef20d9cSFrederic Weisbecker 2988fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 2989fef20d9cSFrederic Weisbecker save_arg(long long); 2990fef20d9cSFrederic Weisbecker break; 2991fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 2992fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 2993fef20d9cSFrederic Weisbecker save_arg(unsigned long); 2994fef20d9cSFrederic Weisbecker break; 2995fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 2996fef20d9cSFrederic Weisbecker save_arg(size_t); 2997fef20d9cSFrederic Weisbecker break; 2998fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 2999fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 3000fef20d9cSFrederic Weisbecker break; 3001a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 3002a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 3003a4e94ef0SZhaolei save_arg(char); 3004a4e94ef0SZhaolei break; 3005fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 3006fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 3007fef20d9cSFrederic Weisbecker save_arg(short); 3008fef20d9cSFrederic Weisbecker break; 3009fef20d9cSFrederic Weisbecker default: 3010fef20d9cSFrederic Weisbecker save_arg(int); 3011fef20d9cSFrederic Weisbecker } 3012fef20d9cSFrederic Weisbecker } 3013fef20d9cSFrederic Weisbecker } 3014fef20d9cSFrederic Weisbecker 3015b006f19bSRasmus Villemoes out: 30167b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 3017fef20d9cSFrederic Weisbecker #undef save_arg 30184370aa4aSLai Jiangshan } 30194370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 30204370aa4aSLai Jiangshan 30214370aa4aSLai Jiangshan /** 30224370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 30234370aa4aSLai Jiangshan * @buf: The buffer to place the result into 30244370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 30254370aa4aSLai Jiangshan * @fmt: The format string to use 30264370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 30274370aa4aSLai Jiangshan * 30284370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 30294370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 30304370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 30314370aa4aSLai Jiangshan * 30324370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 30330efb4d20SSteven Rostedt * see vsnprintf comment for details. 30344370aa4aSLai Jiangshan * 30354370aa4aSLai Jiangshan * The return value is the number of characters which would 30364370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 30374370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 30384370aa4aSLai Jiangshan * number of characters written into @buf as return value 30394370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 30404370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 30414370aa4aSLai Jiangshan * string is truncated. 30424370aa4aSLai Jiangshan */ 30434370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 30444370aa4aSLai Jiangshan { 3045fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 3046d4be151bSAndré Goddard Rosa char *str, *end; 3047d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 30484370aa4aSLai Jiangshan 3049762abb51SRasmus Villemoes if (WARN_ON_ONCE(size > INT_MAX)) 30504370aa4aSLai Jiangshan return 0; 30514370aa4aSLai Jiangshan 30524370aa4aSLai Jiangshan str = buf; 30534370aa4aSLai Jiangshan end = buf + size; 30544370aa4aSLai Jiangshan 30554370aa4aSLai Jiangshan #define get_arg(type) \ 30564370aa4aSLai Jiangshan ({ \ 30574370aa4aSLai Jiangshan typeof(type) value; \ 30584370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 30594370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 30604370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 30614370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 30624370aa4aSLai Jiangshan } else { \ 30634370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 30644370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 30654370aa4aSLai Jiangshan } \ 30664370aa4aSLai Jiangshan args += sizeof(type); \ 30674370aa4aSLai Jiangshan value; \ 30684370aa4aSLai Jiangshan }) 30694370aa4aSLai Jiangshan 30704370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 30714370aa4aSLai Jiangshan if (end < buf) { 30724370aa4aSLai Jiangshan end = ((void *)-1); 30734370aa4aSLai Jiangshan size = end - buf; 30744370aa4aSLai Jiangshan } 30754370aa4aSLai Jiangshan 3076fef20d9cSFrederic Weisbecker while (*fmt) { 3077fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 3078d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 3079fef20d9cSFrederic Weisbecker 3080fef20d9cSFrederic Weisbecker fmt += read; 3081fef20d9cSFrederic Weisbecker 3082fef20d9cSFrederic Weisbecker switch (spec.type) { 3083fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 3084fef20d9cSFrederic Weisbecker int copy = read; 3085fef20d9cSFrederic Weisbecker if (str < end) { 3086fef20d9cSFrederic Weisbecker if (copy > end - str) 3087fef20d9cSFrederic Weisbecker copy = end - str; 3088fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 3089fef20d9cSFrederic Weisbecker } 3090fef20d9cSFrederic Weisbecker str += read; 3091fef20d9cSFrederic Weisbecker break; 30924370aa4aSLai Jiangshan } 30934370aa4aSLai Jiangshan 3094ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 30954d72ba01SRasmus Villemoes set_field_width(&spec, get_arg(int)); 3096fef20d9cSFrederic Weisbecker break; 30974370aa4aSLai Jiangshan 3098fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 30994d72ba01SRasmus Villemoes set_precision(&spec, get_arg(int)); 3100fef20d9cSFrederic Weisbecker break; 31014370aa4aSLai Jiangshan 3102d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 3103d4be151bSAndré Goddard Rosa char c; 3104d4be151bSAndré Goddard Rosa 3105fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 3106fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 31074370aa4aSLai Jiangshan if (str < end) 31084370aa4aSLai Jiangshan *str = ' '; 31094370aa4aSLai Jiangshan ++str; 31104370aa4aSLai Jiangshan } 31114370aa4aSLai Jiangshan } 31124370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 31134370aa4aSLai Jiangshan if (str < end) 31144370aa4aSLai Jiangshan *str = c; 31154370aa4aSLai Jiangshan ++str; 3116fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 31174370aa4aSLai Jiangshan if (str < end) 31184370aa4aSLai Jiangshan *str = ' '; 31194370aa4aSLai Jiangshan ++str; 31204370aa4aSLai Jiangshan } 3121fef20d9cSFrederic Weisbecker break; 3122d4be151bSAndré Goddard Rosa } 31234370aa4aSLai Jiangshan 3124fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 31254370aa4aSLai Jiangshan const char *str_arg = args; 3126d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 3127fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 3128fef20d9cSFrederic Weisbecker break; 31294370aa4aSLai Jiangshan } 31304370aa4aSLai Jiangshan 3131841a915dSSteven Rostedt (VMware) case FORMAT_TYPE_PTR: { 3132841a915dSSteven Rostedt (VMware) bool process = false; 3133841a915dSSteven Rostedt (VMware) int copy, len; 3134841a915dSSteven Rostedt (VMware) /* Non function dereferences were already done */ 3135841a915dSSteven Rostedt (VMware) switch (*fmt) { 3136841a915dSSteven Rostedt (VMware) case 'S': 3137841a915dSSteven Rostedt (VMware) case 's': 3138841a915dSSteven Rostedt (VMware) case 'F': 3139841a915dSSteven Rostedt (VMware) case 'f': 31401e6338cfSSteven Rostedt (VMware) case 'x': 31411e6338cfSSteven Rostedt (VMware) case 'K': 314257f5677eSRasmus Villemoes case 'e': 3143841a915dSSteven Rostedt (VMware) process = true; 3144841a915dSSteven Rostedt (VMware) break; 3145841a915dSSteven Rostedt (VMware) default: 3146841a915dSSteven Rostedt (VMware) if (!isalnum(*fmt)) { 3147841a915dSSteven Rostedt (VMware) process = true; 3148841a915dSSteven Rostedt (VMware) break; 3149841a915dSSteven Rostedt (VMware) } 3150841a915dSSteven Rostedt (VMware) /* Pointer dereference was already processed */ 3151841a915dSSteven Rostedt (VMware) if (str < end) { 3152841a915dSSteven Rostedt (VMware) len = copy = strlen(args); 3153841a915dSSteven Rostedt (VMware) if (copy > end - str) 3154841a915dSSteven Rostedt (VMware) copy = end - str; 3155841a915dSSteven Rostedt (VMware) memcpy(str, args, copy); 3156841a915dSSteven Rostedt (VMware) str += len; 315762165600SSteven Rostedt (VMware) args += len + 1; 3158841a915dSSteven Rostedt (VMware) } 3159841a915dSSteven Rostedt (VMware) } 3160841a915dSSteven Rostedt (VMware) if (process) 3161ffbfed03SRasmus Villemoes str = pointer(fmt, str, end, get_arg(void *), spec); 3162841a915dSSteven Rostedt (VMware) 3163fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 31644370aa4aSLai Jiangshan fmt++; 3165fef20d9cSFrederic Weisbecker break; 3166841a915dSSteven Rostedt (VMware) } 31674370aa4aSLai Jiangshan 3168fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 31694370aa4aSLai Jiangshan if (str < end) 31704370aa4aSLai Jiangshan *str = '%'; 31714370aa4aSLai Jiangshan ++str; 3172fef20d9cSFrederic Weisbecker break; 3173fef20d9cSFrederic Weisbecker 3174b006f19bSRasmus Villemoes case FORMAT_TYPE_INVALID: 3175b006f19bSRasmus Villemoes goto out; 3176b006f19bSRasmus Villemoes 3177d4be151bSAndré Goddard Rosa default: { 3178d4be151bSAndré Goddard Rosa unsigned long long num; 3179d4be151bSAndré Goddard Rosa 3180fef20d9cSFrederic Weisbecker switch (spec.type) { 3181fef20d9cSFrederic Weisbecker 3182fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 31834370aa4aSLai Jiangshan num = get_arg(long long); 3184fef20d9cSFrederic Weisbecker break; 3185fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 3186fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 3187fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 3188fef20d9cSFrederic Weisbecker break; 3189fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 31904370aa4aSLai Jiangshan num = get_arg(size_t); 3191fef20d9cSFrederic Weisbecker break; 3192fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 31934370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 3194fef20d9cSFrederic Weisbecker break; 3195a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 3196a4e94ef0SZhaolei num = get_arg(unsigned char); 3197a4e94ef0SZhaolei break; 3198a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 3199a4e94ef0SZhaolei num = get_arg(signed char); 3200a4e94ef0SZhaolei break; 3201fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 3202fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 3203fef20d9cSFrederic Weisbecker break; 3204fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 3205fef20d9cSFrederic Weisbecker num = get_arg(short); 3206fef20d9cSFrederic Weisbecker break; 3207fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 32084370aa4aSLai Jiangshan num = get_arg(unsigned int); 3209fef20d9cSFrederic Weisbecker break; 3210fef20d9cSFrederic Weisbecker default: 3211fef20d9cSFrederic Weisbecker num = get_arg(int); 32124370aa4aSLai Jiangshan } 3213fef20d9cSFrederic Weisbecker 3214fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 3215d4be151bSAndré Goddard Rosa } /* default: */ 3216d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 3217d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 3218fef20d9cSFrederic Weisbecker 3219b006f19bSRasmus Villemoes out: 32204370aa4aSLai Jiangshan if (size > 0) { 32214370aa4aSLai Jiangshan if (str < end) 32224370aa4aSLai Jiangshan *str = '\0'; 32234370aa4aSLai Jiangshan else 32244370aa4aSLai Jiangshan end[-1] = '\0'; 32254370aa4aSLai Jiangshan } 3226fef20d9cSFrederic Weisbecker 32274370aa4aSLai Jiangshan #undef get_arg 32284370aa4aSLai Jiangshan 32294370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 32304370aa4aSLai Jiangshan return str - buf; 32314370aa4aSLai Jiangshan } 32324370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 32334370aa4aSLai Jiangshan 32344370aa4aSLai Jiangshan /** 32354370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 32364370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 32374370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 32384370aa4aSLai Jiangshan * @fmt: The format string to use 32394370aa4aSLai Jiangshan * @...: Arguments for the format string 32404370aa4aSLai Jiangshan * 32414370aa4aSLai Jiangshan * The function returns the number of words(u32) written 32424370aa4aSLai Jiangshan * into @bin_buf. 32434370aa4aSLai Jiangshan */ 32444370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 32454370aa4aSLai Jiangshan { 32464370aa4aSLai Jiangshan va_list args; 32474370aa4aSLai Jiangshan int ret; 32484370aa4aSLai Jiangshan 32494370aa4aSLai Jiangshan va_start(args, fmt); 32504370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 32514370aa4aSLai Jiangshan va_end(args); 32527b9186f5SAndré Goddard Rosa 32534370aa4aSLai Jiangshan return ret; 32544370aa4aSLai Jiangshan } 32554370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 32564370aa4aSLai Jiangshan 32574370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 32584370aa4aSLai Jiangshan 32591da177e4SLinus Torvalds /** 32601da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 32611da177e4SLinus Torvalds * @buf: input buffer 32621da177e4SLinus Torvalds * @fmt: format of buffer 32631da177e4SLinus Torvalds * @args: arguments 32641da177e4SLinus Torvalds */ 32651da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 32661da177e4SLinus Torvalds { 32671da177e4SLinus Torvalds const char *str = buf; 32681da177e4SLinus Torvalds char *next; 32691da177e4SLinus Torvalds char digit; 32701da177e4SLinus Torvalds int num = 0; 3271ef0658f3SJoe Perches u8 qualifier; 327253809751SJan Beulich unsigned int base; 327353809751SJan Beulich union { 327453809751SJan Beulich long long s; 327553809751SJan Beulich unsigned long long u; 327653809751SJan Beulich } val; 3277ef0658f3SJoe Perches s16 field_width; 3278d4be151bSAndré Goddard Rosa bool is_sign; 32791da177e4SLinus Torvalds 3280da99075cSJan Beulich while (*fmt) { 32811da177e4SLinus Torvalds /* skip any white space in format */ 32821da177e4SLinus Torvalds /* white space in format matchs any amount of 32831da177e4SLinus Torvalds * white space, including none, in the input. 32841da177e4SLinus Torvalds */ 32851da177e4SLinus Torvalds if (isspace(*fmt)) { 3286e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 3287e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 32881da177e4SLinus Torvalds } 32891da177e4SLinus Torvalds 32901da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 32911da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 32921da177e4SLinus Torvalds if (*fmt++ != *str++) 32931da177e4SLinus Torvalds break; 32941da177e4SLinus Torvalds continue; 32951da177e4SLinus Torvalds } 32961da177e4SLinus Torvalds 32971da177e4SLinus Torvalds if (!*fmt) 32981da177e4SLinus Torvalds break; 32991da177e4SLinus Torvalds ++fmt; 33001da177e4SLinus Torvalds 33011da177e4SLinus Torvalds /* skip this conversion. 33021da177e4SLinus Torvalds * advance both strings to next white space 33031da177e4SLinus Torvalds */ 33041da177e4SLinus Torvalds if (*fmt == '*') { 3305da99075cSJan Beulich if (!*str) 3306da99075cSJan Beulich break; 3307f9310b2fSJessica Yu while (!isspace(*fmt) && *fmt != '%' && *fmt) { 3308f9310b2fSJessica Yu /* '%*[' not yet supported, invalid format */ 3309f9310b2fSJessica Yu if (*fmt == '[') 3310f9310b2fSJessica Yu return num; 33111da177e4SLinus Torvalds fmt++; 3312f9310b2fSJessica Yu } 33131da177e4SLinus Torvalds while (!isspace(*str) && *str) 33141da177e4SLinus Torvalds str++; 33151da177e4SLinus Torvalds continue; 33161da177e4SLinus Torvalds } 33171da177e4SLinus Torvalds 33181da177e4SLinus Torvalds /* get field width */ 33191da177e4SLinus Torvalds field_width = -1; 332053809751SJan Beulich if (isdigit(*fmt)) { 33211da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 332253809751SJan Beulich if (field_width <= 0) 332353809751SJan Beulich break; 332453809751SJan Beulich } 33251da177e4SLinus Torvalds 33261da177e4SLinus Torvalds /* get conversion qualifier */ 33271da177e4SLinus Torvalds qualifier = -1; 332875fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 33295b5e0928SAlexey Dobriyan *fmt == 'z') { 33301da177e4SLinus Torvalds qualifier = *fmt++; 33311da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 33321da177e4SLinus Torvalds if (qualifier == 'h') { 33331da177e4SLinus Torvalds qualifier = 'H'; 33341da177e4SLinus Torvalds fmt++; 33351da177e4SLinus Torvalds } else if (qualifier == 'l') { 33361da177e4SLinus Torvalds qualifier = 'L'; 33371da177e4SLinus Torvalds fmt++; 33381da177e4SLinus Torvalds } 33391da177e4SLinus Torvalds } 33401da177e4SLinus Torvalds } 33411da177e4SLinus Torvalds 3342da99075cSJan Beulich if (!*fmt) 3343da99075cSJan Beulich break; 3344da99075cSJan Beulich 3345da99075cSJan Beulich if (*fmt == 'n') { 3346da99075cSJan Beulich /* return number of characters read so far */ 3347da99075cSJan Beulich *va_arg(args, int *) = str - buf; 3348da99075cSJan Beulich ++fmt; 3349da99075cSJan Beulich continue; 3350da99075cSJan Beulich } 3351da99075cSJan Beulich 3352da99075cSJan Beulich if (!*str) 33531da177e4SLinus Torvalds break; 33541da177e4SLinus Torvalds 3355d4be151bSAndré Goddard Rosa base = 10; 33563f623ebaSFabian Frederick is_sign = false; 3357d4be151bSAndré Goddard Rosa 33581da177e4SLinus Torvalds switch (*fmt++) { 33591da177e4SLinus Torvalds case 'c': 33601da177e4SLinus Torvalds { 33611da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 33621da177e4SLinus Torvalds if (field_width == -1) 33631da177e4SLinus Torvalds field_width = 1; 33641da177e4SLinus Torvalds do { 33651da177e4SLinus Torvalds *s++ = *str++; 33661da177e4SLinus Torvalds } while (--field_width > 0 && *str); 33671da177e4SLinus Torvalds num++; 33681da177e4SLinus Torvalds } 33691da177e4SLinus Torvalds continue; 33701da177e4SLinus Torvalds case 's': 33711da177e4SLinus Torvalds { 33721da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 33731da177e4SLinus Torvalds if (field_width == -1) 33744be929beSAlexey Dobriyan field_width = SHRT_MAX; 33751da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 3376e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 33771da177e4SLinus Torvalds 33781da177e4SLinus Torvalds /* now copy until next white space */ 33797b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 33801da177e4SLinus Torvalds *s++ = *str++; 33811da177e4SLinus Torvalds *s = '\0'; 33821da177e4SLinus Torvalds num++; 33831da177e4SLinus Torvalds } 33841da177e4SLinus Torvalds continue; 3385f9310b2fSJessica Yu /* 3386f9310b2fSJessica Yu * Warning: This implementation of the '[' conversion specifier 3387f9310b2fSJessica Yu * deviates from its glibc counterpart in the following ways: 3388f9310b2fSJessica Yu * (1) It does NOT support ranges i.e. '-' is NOT a special 3389f9310b2fSJessica Yu * character 3390f9310b2fSJessica Yu * (2) It cannot match the closing bracket ']' itself 3391f9310b2fSJessica Yu * (3) A field width is required 3392f9310b2fSJessica Yu * (4) '%*[' (discard matching input) is currently not supported 3393f9310b2fSJessica Yu * 3394f9310b2fSJessica Yu * Example usage: 3395f9310b2fSJessica Yu * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]", 3396f9310b2fSJessica Yu * buf1, buf2, buf3); 3397f9310b2fSJessica Yu * if (ret < 3) 3398f9310b2fSJessica Yu * // etc.. 3399f9310b2fSJessica Yu */ 3400f9310b2fSJessica Yu case '[': 3401f9310b2fSJessica Yu { 3402f9310b2fSJessica Yu char *s = (char *)va_arg(args, char *); 3403f9310b2fSJessica Yu DECLARE_BITMAP(set, 256) = {0}; 3404f9310b2fSJessica Yu unsigned int len = 0; 3405f9310b2fSJessica Yu bool negate = (*fmt == '^'); 3406f9310b2fSJessica Yu 3407f9310b2fSJessica Yu /* field width is required */ 3408f9310b2fSJessica Yu if (field_width == -1) 3409f9310b2fSJessica Yu return num; 3410f9310b2fSJessica Yu 3411f9310b2fSJessica Yu if (negate) 3412f9310b2fSJessica Yu ++fmt; 3413f9310b2fSJessica Yu 3414f9310b2fSJessica Yu for ( ; *fmt && *fmt != ']'; ++fmt, ++len) 3415f9310b2fSJessica Yu set_bit((u8)*fmt, set); 3416f9310b2fSJessica Yu 3417f9310b2fSJessica Yu /* no ']' or no character set found */ 3418f9310b2fSJessica Yu if (!*fmt || !len) 3419f9310b2fSJessica Yu return num; 3420f9310b2fSJessica Yu ++fmt; 3421f9310b2fSJessica Yu 3422f9310b2fSJessica Yu if (negate) { 3423f9310b2fSJessica Yu bitmap_complement(set, set, 256); 3424f9310b2fSJessica Yu /* exclude null '\0' byte */ 3425f9310b2fSJessica Yu clear_bit(0, set); 3426f9310b2fSJessica Yu } 3427f9310b2fSJessica Yu 3428f9310b2fSJessica Yu /* match must be non-empty */ 3429f9310b2fSJessica Yu if (!test_bit((u8)*str, set)) 3430f9310b2fSJessica Yu return num; 3431f9310b2fSJessica Yu 3432f9310b2fSJessica Yu while (test_bit((u8)*str, set) && field_width--) 3433f9310b2fSJessica Yu *s++ = *str++; 3434f9310b2fSJessica Yu *s = '\0'; 3435f9310b2fSJessica Yu ++num; 3436f9310b2fSJessica Yu } 3437f9310b2fSJessica Yu continue; 34381da177e4SLinus Torvalds case 'o': 34391da177e4SLinus Torvalds base = 8; 34401da177e4SLinus Torvalds break; 34411da177e4SLinus Torvalds case 'x': 34421da177e4SLinus Torvalds case 'X': 34431da177e4SLinus Torvalds base = 16; 34441da177e4SLinus Torvalds break; 34451da177e4SLinus Torvalds case 'i': 34461da177e4SLinus Torvalds base = 0; 34474c1ca831SNick Desaulniers fallthrough; 34481da177e4SLinus Torvalds case 'd': 34493f623ebaSFabian Frederick is_sign = true; 34504c1ca831SNick Desaulniers fallthrough; 34511da177e4SLinus Torvalds case 'u': 34521da177e4SLinus Torvalds break; 34531da177e4SLinus Torvalds case '%': 34541da177e4SLinus Torvalds /* looking for '%' in str */ 34551da177e4SLinus Torvalds if (*str++ != '%') 34561da177e4SLinus Torvalds return num; 34571da177e4SLinus Torvalds continue; 34581da177e4SLinus Torvalds default: 34591da177e4SLinus Torvalds /* invalid format; stop here */ 34601da177e4SLinus Torvalds return num; 34611da177e4SLinus Torvalds } 34621da177e4SLinus Torvalds 34631da177e4SLinus Torvalds /* have some sort of integer conversion. 34641da177e4SLinus Torvalds * first, skip white space in buffer. 34651da177e4SLinus Torvalds */ 3466e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 34671da177e4SLinus Torvalds 34681da177e4SLinus Torvalds digit = *str; 34691da177e4SLinus Torvalds if (is_sign && digit == '-') 34701da177e4SLinus Torvalds digit = *(str + 1); 34711da177e4SLinus Torvalds 34721da177e4SLinus Torvalds if (!digit 34731da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 34741da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 34751da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 34761da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 34771da177e4SLinus Torvalds break; 34781da177e4SLinus Torvalds 347953809751SJan Beulich if (is_sign) 348053809751SJan Beulich val.s = qualifier != 'L' ? 348153809751SJan Beulich simple_strtol(str, &next, base) : 348253809751SJan Beulich simple_strtoll(str, &next, base); 348353809751SJan Beulich else 348453809751SJan Beulich val.u = qualifier != 'L' ? 348553809751SJan Beulich simple_strtoul(str, &next, base) : 348653809751SJan Beulich simple_strtoull(str, &next, base); 348753809751SJan Beulich 348853809751SJan Beulich if (field_width > 0 && next - str > field_width) { 348953809751SJan Beulich if (base == 0) 349053809751SJan Beulich _parse_integer_fixup_radix(str, &base); 349153809751SJan Beulich while (next - str > field_width) { 349253809751SJan Beulich if (is_sign) 349353809751SJan Beulich val.s = div_s64(val.s, base); 349453809751SJan Beulich else 349553809751SJan Beulich val.u = div_u64(val.u, base); 349653809751SJan Beulich --next; 349753809751SJan Beulich } 349853809751SJan Beulich } 349953809751SJan Beulich 35001da177e4SLinus Torvalds switch (qualifier) { 35011da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 350253809751SJan Beulich if (is_sign) 350353809751SJan Beulich *va_arg(args, signed char *) = val.s; 350453809751SJan Beulich else 350553809751SJan Beulich *va_arg(args, unsigned char *) = val.u; 35061da177e4SLinus Torvalds break; 35071da177e4SLinus Torvalds case 'h': 350853809751SJan Beulich if (is_sign) 350953809751SJan Beulich *va_arg(args, short *) = val.s; 351053809751SJan Beulich else 351153809751SJan Beulich *va_arg(args, unsigned short *) = val.u; 35121da177e4SLinus Torvalds break; 35131da177e4SLinus Torvalds case 'l': 351453809751SJan Beulich if (is_sign) 351553809751SJan Beulich *va_arg(args, long *) = val.s; 351653809751SJan Beulich else 351753809751SJan Beulich *va_arg(args, unsigned long *) = val.u; 35181da177e4SLinus Torvalds break; 35191da177e4SLinus Torvalds case 'L': 352053809751SJan Beulich if (is_sign) 352153809751SJan Beulich *va_arg(args, long long *) = val.s; 352253809751SJan Beulich else 352353809751SJan Beulich *va_arg(args, unsigned long long *) = val.u; 35241da177e4SLinus Torvalds break; 35251da177e4SLinus Torvalds case 'z': 352653809751SJan Beulich *va_arg(args, size_t *) = val.u; 35271da177e4SLinus Torvalds break; 35281da177e4SLinus Torvalds default: 352953809751SJan Beulich if (is_sign) 353053809751SJan Beulich *va_arg(args, int *) = val.s; 353153809751SJan Beulich else 353253809751SJan Beulich *va_arg(args, unsigned int *) = val.u; 35331da177e4SLinus Torvalds break; 35341da177e4SLinus Torvalds } 35351da177e4SLinus Torvalds num++; 35361da177e4SLinus Torvalds 35371da177e4SLinus Torvalds if (!next) 35381da177e4SLinus Torvalds break; 35391da177e4SLinus Torvalds str = next; 35401da177e4SLinus Torvalds } 3541c6b40d16SJohannes Berg 35421da177e4SLinus Torvalds return num; 35431da177e4SLinus Torvalds } 35441da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 35451da177e4SLinus Torvalds 35461da177e4SLinus Torvalds /** 35471da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 35481da177e4SLinus Torvalds * @buf: input buffer 35491da177e4SLinus Torvalds * @fmt: formatting of buffer 35501da177e4SLinus Torvalds * @...: resulting arguments 35511da177e4SLinus Torvalds */ 35521da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 35531da177e4SLinus Torvalds { 35541da177e4SLinus Torvalds va_list args; 35551da177e4SLinus Torvalds int i; 35561da177e4SLinus Torvalds 35571da177e4SLinus Torvalds va_start(args, fmt); 35581da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 35591da177e4SLinus Torvalds va_end(args); 35607b9186f5SAndré Goddard Rosa 35611da177e4SLinus Torvalds return i; 35621da177e4SLinus Torvalds } 35631da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 3564